Esempio n. 1
0
 public JsNode VisitTypeIsResolveResult(TypeIsResolveResult res)
 {
     if (Sk.OmitCasts(res.TargetType))
     {
         return(Js.True());
         //var node2 = Visit(res.Input);
         //return node2;
     }
     else if (Sk.NativeCasts(res.TargetType))
     {
         var typeFieldName = Sk.GetJsonTypeFieldName(res.TargetType.GetDefinitionOrArrayType());
         if (typeFieldName != null && Sk.IsJsonMode(res.TargetType.GetDefinitionOrArrayType()))
         {
             return(Js.Parentheses(VisitExpression(res.Input).Or(Js.Json())).Member(typeFieldName).Equal(SkJs.EntityTypeRefToMember(res.TargetType)));
         }
         var node2 = VisitExpression(res.Input).InstanceOf(SkJs.EntityTypeRefToMember(res.TargetType));
         return(node2);
     }
     else
     {
         var node2 = Js.Member("Is").Invoke(VisitExpression(res.Input), SkJs.EntityTypeRefToMember(res.TargetType));
         return(node2);
     }
 }
Esempio n. 2
0
        private TsTypeRef Visit(IType type)
        {
            if (type == null)
            {
                return new TsTypeRef {
                           Name = "void"
                }
            }
            ;
            var typeDef = type.GetDefinition();

            if (typeDef != null)
            {
                var ktc = typeDef.KnownTypeCode;

                if (ktc != KnownTypeCode.None)
                {
                    if (ktc == KnownTypeCode.Void)
                    {
                        return new TsTypeRef {
                                   Name = "void"
                        }
                    }
                    ;
                    else if (ktc == KnownTypeCode.Object)
                    {
                        return new TsTypeRef {
                                   Name = "any"
                        }
                    }
                    ;
                    else if (ktc == KnownTypeCode.String)
                    {
                        return new TsTypeRef {
                                   Name = "string"
                        }
                    }
                    ;
                    else if (ktc == KnownTypeCode.Boolean)
                    {
                        return new TsTypeRef {
                                   Name = "boolean"
                        }
                    }
                    ;
                    else if (ktc == KnownTypeCode.Int32 ||
                             ktc == KnownTypeCode.Int32 ||
                             ktc == KnownTypeCode.Int16 ||
                             ktc == KnownTypeCode.Int64 ||
                             ktc == KnownTypeCode.Double ||
                             ktc == KnownTypeCode.Single ||
                             ktc == KnownTypeCode.SByte ||
                             ktc == KnownTypeCode.Byte ||
                             ktc == KnownTypeCode.UInt16 ||
                             ktc == KnownTypeCode.UInt32 ||
                             ktc == KnownTypeCode.UInt64 ||
                             ktc == KnownTypeCode.Decimal
                             )
                    {
                        return new TsTypeRef {
                                   Name = "number"
                        }
                    }
                    ;
                }
                if (Sk.IsJsonMode(typeDef))
                {
                    return new TsTypeRef {
                               Name = "Object"
                    }
                }
                ;
                if (type.Kind == TypeKind.Delegate && !Sk.IsJsExported(type.GetDefinition()))
                {
                    var func = Visit(type.GetDelegateInvokeMethod());

                    func.Name            = null;
                    func.IsCallSignature = true;
                    return(new TsTypeRef {
                        AnonymousCallSignature = func
                    });
                }
            }
            var tr2 = new TsTypeRef {
                Name = SkJs.GetEntityJsName(type)
            };

            tr2.GenericArguments = type.TypeArguments.Select(Visit).ToList();
            if (tr2.Name == "Array" && tr2.GenericArguments.Count == 0)
            {
                tr2.GenericArguments.Add(new TsTypeRef {
                    Name = "any"
                });
            }
            return(tr2);
        }
Esempio n. 3
0
        protected JsBlock ExportConstructorBody(IMethod ctor)
        {
            var            ctorNode = (ConstructorDeclaration)ctor.GetDeclaration();
            BlockStatement ccc      = null;

            if (ctorNode != null)
            {
                ccc = ctorNode.Body;
            }
            //var ccc = ctor.GetDefinition();//.decl as CsConstructor;
            //var ccc = ctor.GetDefinition();
            var block2 = (JsBlock)AstNodeConverter.Visit(ccc);

            if (block2 == null)
            {
                block2 = new JsBlock {
                    Statements = new List <JsStatement>()
                }
            }
            ;
            var ce          = ctor.GetDeclaringTypeDefinition();
            var isClr       = Sk.IsClrType(ce);
            var isPrototype = Sk.IsNativeType(ce);
            var statements  = new List <JsStatement>();

            //instance fields initializations
            if (!Sk.InlineFields(ce))
            {
                var isGlobal = Sk.IsGlobalType(ce);
                var fields   = GetExportedDeclaredAndGeneratedFields(ce, ctor.IsStatic);
                //var fields = ctor.GetDeclaringTypeDefinition().GetFields(null, GetMemberOptions.IgnoreInheritedMembers).Where(t => t.IsStatic() == ctor.IsStatic).ToList();
                //fields = fields.Where(ShouldExportField).ToList();
                //fields.AddRange(GeneratePropertyFields(ctor.DeclaringTypeDefinition, ctor.IsStatic));

                //var props = ctor.GetDeclaringTypeDefinition().GetProperties(null, GetMemberOptions.IgnoreInheritedMembers).Where(t => t.IsStatic() == ctor.IsStatic).ToList();
                //props = props.Where(t=>Sk.IsNativeField(t)).ToList();
                //props = props.Where(t => Sk.IsJsExported(t)).ToList();
                //var fieldsAndProperties = fields.Cast<IEntity>().Concat(props.Cast<IEntity>());
                var initializers = fields.Select(fe => ExportInitializer(fe, null, isGlobal, false)).Cast <JsStatement>().ToList();
                if (initializers.Contains(null))
                {
                    Log.Warn("Some field initializers were not exported");
                }
                statements.AddRange(initializers.Where(t => t != null));
            }

            if (!ctor.IsStatic)
            {
                //base/this ctor invocation
                var invocation = GetConstructorBaseOrThisInvocation2(ctor);
                if (invocation != null)
                {
                    var baseThisCe      = invocation.Member.DeclaringType;
                    var isBaseClr       = Sk.IsClrType(baseThisCe.GetDefinition());
                    var isBasePrototype = Sk.IsNativeType(baseThisCe.GetDefinition()) && !Sk.IsJsonMode(baseThisCe.GetDefinition()) && !Sk.IsGlobalType(baseThisCe.GetDefinition()); //happens when prototype inherits from json
                    if (isBaseClr == isClr && isBasePrototype == isPrototype)                                                                                                        //base and derived are both prototype, or both are clr
                    {
                        var newObjExp2 = AstNodeConverter.VisitExpression(invocation);
                        JsInvocationExpression invocation2;
                        if (newObjExp2 is JsNewObjectExpression)
                        {
                            var newObjExp = (JsNewObjectExpression)newObjExp2;
                            invocation2 = newObjExp.Invocation;
                        }
                        else if (newObjExp2 is JsInvocationExpression)
                        {
                            invocation2 = (JsInvocationExpression)newObjExp2;
                        }
                        else
                        {
                            throw new Exception("Unexpected node: " + newObjExp2);
                        }
                        if (Sk.IsExtJsType(ce))
                        {
                            var invocation3 = Js.This().Member("callParent").Invoke();
                            if (invocation2.Arguments.IsNotNullOrEmpty())
                            {
                                invocation3.Arguments = new List <JsExpression> {
                                    Js.NewJsonArray(invocation2.Arguments.NotNull().ToArray())
                                }
                            }
                            ;
                            statements.Add(invocation3.Statement());
                        }
                        else
                        {
                            JsRefactorer.ToCallWithContext(invocation2, new JsThis());
                            statements.Add(invocation2.Statement());
                        }
                    }
                }
            }
            if (block2.Statements == null)
            {
                block2.Statements = new List <JsStatement>();
            }
            block2.Statements.InsertRange(0, statements);

            return(block2);
        }

        void ExportConstructorParameters(IMethod ctor, JsFunction func)
        {
            var ce   = ctor.GetDeclaringTypeDefinition();
            var list = new List <string>();

            if (!Sk.IgnoreTypeArguments(ce))
            {
                //danel
                var gprms = ce.TypeParameters.ToList();//.GetGenericArguments().Where(ga => ga.isGenericParam()).ToList();
                if (gprms.IsNotNullOrEmpty())
                {
                    var i = 0;
                    foreach (var gprm in gprms)
                    {
                        func.Parameters.Add(gprm.Name);
                        if (!ctor.IsStatic && func.Block != null)
                        {
                            func.Block.Statements.Insert(i, Js.This().Member(gprm.Name).Assign(Js.Member(gprm.Name)).Statement());
                            i++;
                        }
                    }
                }
            }
            var prms = ctor.Parameters;

            if (prms != null)
            {
                func.Parameters.AddRange(prms.Select(t => t.Name));
            }
        }