Ejemplo n.º 1
0
        private object InterpretCall(GenericScope genericScope, MethodBase method, MethodDefinition definition, object target, IReadOnlyList <object> arguments)
        {
            var context     = new CilHandlerContext(genericScope, method, definition, target, arguments ?? Empty <object> .Array, _resolver, _invoker);
            var instruction = definition.Body.Instructions[0];

            var returnType = (method as MethodInfo)?.ReturnType;

            while (instruction != null)
            {
                if (instruction.OpCode == OpCodes.Ret)
                {
                    var result = (object)null;
                    if (returnType != null && returnType != typeof(void))
                    {
                        result = TypeSupport.Convert(context.Stack.Pop(), returnType);
                    }

                    if (context.Stack.Count > 0)
                    {
                        throw new Exception($"Unbalanced stack on return: {context.Stack.Count} extra items.");
                    }

                    return(result);
                }

                context.NextInstruction = instruction.Next;
                InterpretInstruction(instruction, context);
                instruction = context.NextInstruction;
            }

            throw new Exception($"Failed to reach a 'ret' instruction.");
        }
Ejemplo n.º 2
0
        public CilHandlerContext(GenericScope genericScope, MethodBase method, MethodDefinition definition, object target, IReadOnlyList <object> arguments, Resolver resolver, MethodInvoker invoker)
        {
            _variableDefinitions = definition.Body.Variables.OrderBy(v => v.Index).ToList();
            var variables = new List <object>(new object[_variableDefinitions.Count]);

            Variables         = variables;
            _variablesMutable = variables;

            Resolver     = resolver;
            Invoker      = invoker;
            Method       = method;
            Definition   = definition;
            Target       = target;
            Arguments    = arguments;
            GenericScope = genericScope;
        }
        //NB: This one might break, there was talk about refactoring all for loops to while loops behind the scenes
        protected override CrawlSyntaxNode VisitForLoop(ForLoopNode forLoop)
        {
            GenericScope scope = new GenericScope(new[]
            {
                new KeyValuePair <string, TypeInformation>(
                    forLoop.LoopVariable.Value,
                    new TypeInformation(
                        forLoop.Loopvariable.ActualType,
                        ProtectionLevel.NotApplicable,
                        forLoop.LoopVariable.Interval.a,
                        DeclaringScope.MethodLike)
                    ),
            });

            ForLoopNode afterVisit = (ForLoopNode)base.VisitForLoop(forLoop);

            return(afterVisit.WithScope(scope));
        }
        protected override CrawlSyntaxNode VisitMethodDecleration(MethodDeclerationNode methodDecleration)
        {
            IScope containingScope = methodDecleration.FindFirstScope();

            GenericScope scope = new GenericScope(
                methodDecleration
                .Parameters
                .Select(
                    (parameter, index) =>
                    new KeyValuePair <string, TypeInformation>(parameter.Identifier.Value,
                                                               new TypeInformation(
                                                                   parameter.ParameterType.ActualType,
                                                                   ProtectionLevel.NotApplicable,
                                                                   parameter.Interval.b
                                                                   )
                                                               )
                    )
                );

            MethodDeclerationNode node = (MethodDeclerationNode)base.VisitMethodDecleration(methodDecleration);

            return(node.WithScope(scope));
        }