Exemplo n.º 1
0
        public static Stmt Equals(Ctx ctx)
        {
            var other = ctx.MethodParameter(0).Named("other");
            var type  = new ExprJsTypeVarName(ctx, ctx.Single).Named("type");

            return(new StmtJsExplicit(ctx, "return other._ === type && this === other.v;", ctx.ThisNamed, other, type));
        }
Exemplo n.º 2
0
        public static Stmt GetType(Ctx ctx)
        {
            var js         = "return typeof(this) == \"string\" ? stringType : this._ || __[this.tagName] || __[this.constructor.name];";
            var stringType = new ExprJsTypeVarName(ctx, ctx.String).Named("stringType");
            var stmt       = new StmtJsExplicit(ctx, js, ctx.ThisNamed, stringType);

            return(stmt);
        }
            public Stmt GetImpl(Ctx ctx)
            {
                var count      = ctx.MethodParameter(0, "count");
                var elType     = ((GenericInstanceMethod)ctx.MRef).GenericArguments[0];
                var arrayType  = elType.MakeArray();
                var elTypeExpr = new ExprJsTypeVarName(ctx, arrayType).Named("type");
                var a          = ctx.Local(arrayType, "a");
                var stmt       = new StmtJsExplicit(ctx, "a = new Array(count); a._ = type; return a;", count, elTypeExpr, a);

                return(stmt);
            }
Exemplo n.º 4
0
        public static Stmt ToArray(Ctx ctx)
        {
            var returnType    = ctx.MRef.ReturnType.FullResolve(ctx);
            var a             = ctx.Local(returnType, "a");
            var arrayFieldRef = ctx.Module.Import(typeof(_List <T>)).EnumResolvedFields().First();
            var array         = new ExprFieldAccess(ctx, ctx.This, arrayFieldRef).Named("array");
            var type          = new ExprJsTypeVarName(ctx, returnType).Named("type");
            var js            = @"
a = array.slice(0);
a._ = type;
return a;
";

            return(new StmtJsExplicit(ctx, js, a, type, array));
        }
            public Stmt GetImpl(Ctx ctx)
            {
                var count        = ctx.MethodParameter(0, "count");
                var elType       = ((GenericInstanceMethod)ctx.MRef).GenericArguments[0];
                var arrayType    = elType.MakeArray();
                var elTypeExpr   = new ExprJsTypeVarName(ctx, arrayType).Named("type");
                var defaultValue = new ExprDefaultValue(ctx, elType).Named("defaultValue");
                var a            = ctx.Local(arrayType, "a");
                var i            = ctx.Local(ctx.Int32, "i");
                var js           = @"
a = new Array(count);
a._ = type;
for (i = count - 1; i >= 0; i--)
    a[i] = defaultValue;
return a;
";
                var stmt         = new StmtJsExplicit(ctx, js, count, elTypeExpr, defaultValue, a, i);

                return(stmt);
            }
Exemplo n.º 6
0
 protected override ICode VisitJsTypeVarName(ExprJsTypeVarName e)
 {
     this.code.AppendFormat("typeof({0})", e.TypeRef.Name);
     return(e);
 }
        public static Expr InitializeArray(ICall call)
        {
            var ctx         = call.Ctx;
            var array       = (ExprVar)call.Args.ElementAt(0);
            var initExpr    = (ExprRuntimeHandle)call.Args.ElementAt(1);
            var initData    = ((FieldDefinition)initExpr.Member).InitialValue;
            var arrayElType = ((ArrayType)array.Type).ElementType;

            int inc;
            Func <int, object> getValue;

            switch (arrayElType.MetadataType)
            {
            case MetadataType.Boolean:
                inc      = 1;
                getValue = i => initData[i] != 0;
                break;

            case MetadataType.Byte:
                inc      = 1;
                getValue = i => initData[i];
                break;

            case MetadataType.SByte:
                inc      = 1;
                getValue = i => (sbyte)initData[i];
                break;

            case MetadataType.Int16:
                inc      = 2;
                getValue = i => BitConverter.ToInt16(initData, i);
                break;

            case MetadataType.Int32:
                inc      = 4;
                getValue = i => BitConverter.ToInt32(initData, i);
                break;

            case MetadataType.Int64:
                inc      = 8;
                getValue = i => BitConverter.ToInt64(initData, i);
                break;

            case MetadataType.UInt16:
                inc      = 2;
                getValue = i => BitConverter.ToUInt16(initData, i);
                break;

            case MetadataType.UInt32:
                inc      = 4;
                getValue = i => BitConverter.ToUInt32(initData, i);
                break;

            case MetadataType.UInt64:
                inc      = 8;
                getValue = i => BitConverter.ToUInt64(initData, i);
                break;

            case MetadataType.Single:
                inc      = 4;
                getValue = i => BitConverter.ToSingle(initData, i);
                break;

            case MetadataType.Double:
                inc      = 8;
                getValue = i => BitConverter.ToDouble(initData, i);
                break;

            case MetadataType.Char:
                inc      = 2;
                getValue = i => (char)BitConverter.ToUInt16(initData, i);
                break;

            default:
                throw new NotImplementedException("Cannot handle: " + arrayElType.MetadataType);
            }
            var values = new List <NamedExpr>();

            for (int i = 0; i < initData.Length; i += inc)
            {
                var value = new ExprLiteral(ctx, getValue(i), arrayElType);
                values.Add(value.Named("v" + i));
            }
            var vStr          = string.Join(",", values.Select(x => x.Name));
            var arrayTypeName = new ExprJsTypeVarName(call.Ctx, array.Type).Named("typeName");
            var js            = "a = [" + vStr + "]; a._ = typeName";

            return(new ExprJsExplicit(call.Ctx, js, array.Type, values.Concat(array.Named("a")).Concat(arrayTypeName)));
        }