예제 #1
0
        public void Quote_RuntimeVariables_Hoisted3()
        {
            var x           = Expression.Parameter(typeof(int));
            var y           = Expression.Parameter(typeof(int));
            var variables   = new ReadOnlyCollection <ParameterExpression>(new[] { y });
            var definitions = new Dictionary <ParameterExpression, StorageKind>
            {
                { y, StorageKind.Hoisted | StorageKind.Boxed } // NB: Using Boxed for LINQ ET compatibility.
            };
            var h = new HoistedLocals(parent: null, variables, definitions);

            var e = Expression.Lambda <Func <int, IRuntimeVariables> >(Expression.RuntimeVariables(x, y), x);

            Assert.AreEqual(typeof(Closure <StrongBox <int> >), h.Closure.ClosureType);

            var c = new Closure <StrongBox <int> > {
                Item1 = new StrongBox <int>(42)
            };
            var q = (Expression <Func <int, IRuntimeVariables> >)RuntimeOpsEx.Quote(e, h, c);
            var f = q.Compile();

            Assert.AreEqual(41, f(41)[0]);
            Assert.AreEqual(42, f(41)[1]);
            Assert.AreEqual(2, f(41).Count);

            var r = f(43);

            r[0] = 44;
            r[1] = 45;
            Assert.AreEqual(44, r[0]);
            Assert.AreEqual(45, r[1]);
            Assert.AreEqual(45, c.Item1.Value);
        }
예제 #2
0
        public void Quote_Hoisted_Parent()
        {
            var x           = Expression.Parameter(typeof(int));
            var variables   = new ReadOnlyCollection <ParameterExpression>(new[] { x });
            var definitions = new Dictionary <ParameterExpression, StorageKind>
            {
                { x, StorageKind.Hoisted | StorageKind.Boxed } // NB: Using Boxed for LINQ ET compatibility.
            };
            var p = new HoistedLocals(parent: null, variables, definitions);
            var h = new HoistedLocals(p, new ReadOnlyCollection <ParameterExpression>(Array.Empty <ParameterExpression>()), new Dictionary <ParameterExpression, StorageKind>());

            var e = Expression.Lambda <Func <int> >(x);

            Assert.AreEqual(typeof(Closure <Closure <StrongBox <int> > >), h.Closure.ClosureType);

            var c = new Closure <Closure <StrongBox <int> > > {
                Item1 = new Closure <StrongBox <int> > {
                    Item1 = new StrongBox <int>(42)
                }
            };
            var q = (Expression <Func <int> >)RuntimeOpsEx.Quote(e, h, c);
            var f = q.Compile();

            Assert.AreEqual(42, f());

            c.Item1.Item1.Value = 43;
            Assert.AreEqual(43, f());
        }
예제 #3
0
        public void Quote_NoHoisted2()
        {
            var h = GetEmptyHoistedLocals();
            var e = (Expression <Func <int, int> >)(x => x);
            var c = new Empty();
            var q = RuntimeOpsEx.Quote(e, h, c);

            Assert.AreSame(e, q);
        }
예제 #4
0
        public void Quote_NoHoisted1()
        {
            var h = GetEmptyHoistedLocals();
            var e = Expression.Lambda <Action>(Expression.Empty());
            var c = new Empty();
            var q = RuntimeOpsEx.Quote(e, h, c);

            Assert.AreSame(e, q);
        }
예제 #5
0
        public void Quote_NoHoisted3()
        {
            var h = GetEmptyHoistedLocals();
            var x = Expression.Parameter(typeof(int));
            var y = Expression.Parameter(typeof(Exception));
            var e = Expression.Lambda <Action>(Expression.Block(new[] { x }, Expression.TryCatch(Expression.Empty(), Expression.Catch(y, Expression.Empty(), Expression.Constant(true)))));
            var c = new Empty();
            var q = RuntimeOpsEx.Quote(e, h, c);

            Assert.AreSame(e, q);
        }
예제 #6
0
        public void Quote_RuntimeVariables_Hoisted2()
        {
            var x           = Expression.Parameter(typeof(int));
            var variables   = new ReadOnlyCollection <ParameterExpression>(Array.Empty <ParameterExpression>());
            var definitions = new Dictionary <ParameterExpression, StorageKind>();
            var h           = new HoistedLocals(parent: null, variables, definitions);

            var e = Expression.Lambda <Func <int, IRuntimeVariables> >(Expression.RuntimeVariables(x), x);

            Assert.AreEqual(typeof(Empty), h.Closure.ClosureType);

            var c = new Empty();
            var q = (Expression <Func <int, IRuntimeVariables> >)RuntimeOpsEx.Quote(e, h, c);
            var f = q.Compile();

            Assert.AreEqual(42, f(42)[0]);
            Assert.AreEqual(1, f(41).Count);

            var r = f(43);

            r[0] = 44;
            Assert.AreEqual(44, r[0]);
        }