Beispiel #1
0
        public Instruction LoadCondition(FieldSchema schema, ExpressionWriter.State state)
        {
            if (!DocBuilder.Document.ExpressionLookup.Conditions.TryGetValue(schema, out var expression))
            {
                return(new Instruction.LoadInt32(1));
            }

            return(new Instruction.InstructionWriter(il => new ExpressionWriter(il, state).WriteExpression(expression)));
        }
Beispiel #2
0
        Instruction WithCondition(Instruction onSuccess, FieldSchema schema, ExpressionWriter.State state)
        {
            if (schema.Condition == null)
            {
                return(onSuccess);
            }

            var conditionalInstruction = new Instruction.IfBranch(
                LoadCondition(schema, state),
                onSuccess
                );

            return(conditionalInstruction);
        }
Beispiel #3
0
        public Instruction ConstructField(FieldSchema schema, FieldBuilder builder, ExpressionWriter.State state)
        {
            if (schema.Dimensions.Count > 0)
            {
                return(new Instruction.NoOperation());
            }

            var setFieldInstruction = new Instruction.Set(
                new Instruction.LoadThis(),
                LoadFieldValue(schema),
                new Instruction.SetField(builder)
                );

            return(WithCondition(setFieldInstruction, schema, state));
        }
Beispiel #4
0
        public Instruction ConstructCompound(CompoundSchema schema)
        {
            var baseType     = DocBuilder.SchemaTypes.SchemaTypesByName[schema.Name];
            var templateType = DocBuilder.SchemaTypes.TemplateTypeByName[schema.Name];

            var fields       = DocBuilder.SchemaFields.GetBuildersForType(schema.Name);
            var state        = new ExpressionWriter.State(fields, new string[] { "Argument" });
            var instructions = fields
                               .Select(sb => ConstructField(sb.Item1, sb.Item2, state))
                               .ToArray();

            return(new Instruction.Set(
                       new Instruction.Set(instructions),
                       new Instruction.Return()
                       ));
        }
Beispiel #5
0
        public void TestExpression()
        {
            var name     = new AssemblyName("TestAssembly");
            var assembly = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);

            var module = assembly.DefineDynamicModule("TestAssembly.dll");

            var myClass  = module.DefineType("MyClass");
            var myField  = myClass.DefineField("MyField", typeof(long), FieldAttributes.Public);
            var myMethod = myClass.DefineMethod("MyMethod", MethodAttributes.Public, CallingConventions.HasThis, typeof(long), new Type[0]);

            var expression = new Expression.Operation.Binary()
            {
                LeftOperand  = new Expression.Parameter("Input"),
                RightOperand = new Expression.Literal(10000),
                Operator     = new Syntax.Operator.Numerical.Addition()
            };

            var state = new ExpressionWriter.State()
            {
                FieldByName = new Dictionary <string, FieldInfo>()
                {
                    ["Input"] = myField
                },
                ArgumentsByName = new Dictionary <string, int>()
            };


            var generator = myMethod.GetILGenerator();
            var writer    = new ExpressionWriter(generator, state);

            writer.WriteExpression(expression);
            generator.Emit(OpCodes.Ret);

            var myClassInfo  = myClass.CreateType();
            var myFieldInfo  = myClass.GetField("MyField");
            var myMethodInfo = myClassInfo.GetMethods()[0];

            var myClassInstance = Activator.CreateInstance(myClass);

            myFieldInfo.SetValue(myClassInstance, 3);
            var output = myMethodInfo.Invoke(myClassInstance, new object[0]);

            Assert.AreEqual(output, 10003);
        }