public ExprJsByRefWrapper(Ctx ctx, Expr expr, ExprVarLocal resultTemp, IEnumerable <Tuple <Expr, Expr> > byRefs)
     : base(ctx)
 {
     this.Expr       = expr;
     this.ResultTemp = resultTemp;
     this.ByRefs     = byRefs;
 }
            public Stmt GetImpl(Ctx ctx)
            {
                var obj          = ctx.MethodParameter(0, "obj");
                var toType       = ctx.MethodParameter(1, "toType");
                var canCastTo    = new ExprVarLocal(ctx, ctx.Type.MakeArray()).Named("canCastTo");
                var mGetType     = ctx.Module.Import(typeof(object).GetMethod("GetType"));
                var getTypeCall  = new ExprCall(ctx, mGetType, obj.Expr).Named("getTypeCall");
                var assignableTo = new ExprJsTypeData(ctx, TypeData.AssignableTo).Named("assignableTo");
                var i            = new ExprVarLocal(ctx, ctx.Int32).Named("i");
                var t            = new ExprVarLocal(ctx, ctx.Type).Named("temp");
                var js           = @"
if (obj == null) return true;
temp = getTypeCall;
if (!temp) return false; // Required because C# objects received via JSON may have null type when type is never refered to in JS
if (temp === toType) return true;
canCastTo = temp.assignableTo;
for (i = canCastTo.length - 1; i >= 0; i--)
    if (canCastTo[i] === toType)
        return true;
return false;
";
                var stmt         = new StmtJsExplicit(ctx, js, obj, toType, canCastTo, getTypeCall, assignableTo, i, t);

                return(stmt);
            }
Exemplo n.º 3
0
        private Stmt StLoc(int idx)
        {
            var expr       = this.InsertConvIfRequired(this.stack.Pop(), this.localTypes[idx]);
            var target     = new ExprVarLocal(this.ctx, expr.Type);
            var assignment = new StmtAssignment(this.ctx, target, expr);

            this.locals[idx] = target;
            return(assignment);
        }
Exemplo n.º 4
0
        private Stmt StArg(int idx)
        {
            var expr       = this.stack.Pop();
            var target     = new ExprVarLocal(this.ctx, expr.Type);
            var assignment = new StmtAssignment(this.ctx, target, expr);

            this.args[idx] = target;
            return(assignment);
        }
Exemplo n.º 5
0
        private Stmt SsaLocalAssignment(Expr expr)
        {
            if (expr.Type == null)
            {
                Debugger.Break();
            }
            var target     = new ExprVarLocal(this.ctx, expr.Type);
            var assignment = new StmtAssignment(this.ctx, target, expr);

            this.stack.Push(target);
            return(assignment);
        }
Exemplo n.º 6
0
        // Only have if, switch, cil, continuation, try - no other type of statement
        // Blocks will only start with cil or try
        // Will end with null, if, switch or continuation
        // If ending in 'if', 'then' and 'else' will both have continuations only
        // 'try' statements will have only 0 or 1 catch clauses

        public static ICode V(ICode ast)
        {
            var v = new VisitorConvertCilToSsa(ast);

            ast = v.Visit(ast);
            foreach (var instResult in v.instResults.Values)
            {
                var var = new ExprVarLocal(ast.Ctx, instResult.Type);
                ast = VisitorReplace.V(ast, instResult, var);
            }
            return(ast);
        }
Exemplo n.º 7
0
        public static Stmt Clear(Ctx ctx)
        {
            var array            = ctx.MethodParameter(0);
            var index            = ctx.MethodParameter(1).Named("index");
            var length           = ctx.MethodParameter(2).Named("length");
            var arrayElementType = ((ArrayType)array.Type).ElementType;
            var i     = new ExprVarLocal(ctx, ctx.Int32).Named("i");
            var value = new ExprDefaultValue(ctx, arrayElementType).Named("value");
            var js    = "for (i=0; i<length; i++) a[index+i]=value;";
            var stmt  = new StmtJsExplicit(ctx, js, i, length, array.Named("a"), index, value);

            return(stmt);
        }
Exemplo n.º 8
0
        public static Stmt GetHashCode(Ctx ctx)
        {
            var acc  = new ExprVarLocal(ctx, ctx.Int32).Named("acc");
            var i    = new ExprVarLocal(ctx, ctx.Int32).Named("i");
            var mask = new ExprLiteral(ctx, 0x7fffffff, ctx.Int32).Named("mask");
            var js   = @"
acc = 5381;
for (i = Math.min(this.length - 1,100); i >= 0; i--)
    acc = ((acc << 5) + acc + this.charCodeAt(i)) & mask;
return acc;";
            var stmt = new StmtJsExplicit(ctx, js, ctx.ThisNamed, acc, i, mask);

            return(stmt);
        }
Exemplo n.º 9
0
 private Expr ConvertToBoolean(Expr e)
 {
     if (e.ExprType == Expr.NodeType.Literal)
     {
         var eLiteral = (ExprLiteral)e;
         if (eLiteral.Type.IsInt32())
         {
             return(new ExprLiteral(e.Ctx, ((int)eLiteral.Value) != 0 ? true : false, e.Ctx.Boolean));
         }
     }
     if (e.ExprType == Expr.NodeType.VarLocal)
     {
         var var = new ExprVarLocal(e.Ctx, e.Ctx.Boolean);
         this.toReplace.Add((ExprVarLocal)e, var);
         return(var);
     }
     throw new InvalidOperationException();
 }
        public void Test()
        {
            var  exprGen = Ctx.ExprGen;
            Expr e;
            IEnumerable <Expr> perms;
            var a = new ExprVarLocal(Ctx, null, "a");
            var b = new ExprVarLocal(Ctx, null, "b");
            var c = new ExprVarLocal(Ctx, null, "c");
            var d = new ExprVarLocal(Ctx, null, "d");

            e     = exprGen.And(a, b);
            perms = VisitorCreatePerms.V(e);
            Assert.That(perms, Is.Unique);
            Assert.That(perms, Is.EquivalentTo(
                            new[] { exprGen.And(a, b), exprGen.And(b, a) }
                            ).Using(Expr.EqComparerExact));

            e     = exprGen.And(a, exprGen.Or(a, b));
            perms = VisitorCreatePerms.V(e);
            Assert.That(perms, Is.Unique);
            Assert.That(perms, Is.EquivalentTo(
                            new[] {
                exprGen.And(a, exprGen.Or(a, b)), exprGen.And(a, exprGen.Or(b, a)),
                exprGen.And(exprGen.Or(a, b), a), exprGen.And(exprGen.Or(b, a), a)
            }
                            ).Using(Expr.EqComparerExact));

            e     = exprGen.And(exprGen.Or(a, b), exprGen.Or(c, d));
            perms = VisitorCreatePerms.V(e);
            Assert.That(perms, Is.Unique);
            Assert.That(perms, Is.EquivalentTo(
                            new[] {
                exprGen.And(exprGen.Or(a, b), exprGen.Or(c, d)), exprGen.And(exprGen.Or(c, d), exprGen.Or(a, b)),
                exprGen.And(exprGen.Or(b, a), exprGen.Or(c, d)), exprGen.And(exprGen.Or(c, d), exprGen.Or(b, a)),
                exprGen.And(exprGen.Or(a, b), exprGen.Or(d, c)), exprGen.And(exprGen.Or(d, c), exprGen.Or(a, b)),
                exprGen.And(exprGen.Or(b, a), exprGen.Or(d, c)), exprGen.And(exprGen.Or(d, c), exprGen.Or(b, a)),
            }
                            ).Using(Expr.EqComparerExact));
        }
Exemplo n.º 11
0
 protected virtual ICode VisitVarLocal(ExprVarLocal e)
 {
     this.ThrowOnNoOverride();
     return(e);
 }
Exemplo n.º 12
0
 protected override ICode VisitVarLocal(ExprVarLocal e)
 {
     this.code.Append(e.ToString());
     return(e);
 }