Example #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.");
        }
Example #2
0
        public void SetVariable(int index, object value)
        {
            var definition = _variableDefinitions[index];

            _variablesMutable[index] = TypeSupport.Convert(value, Resolver.Type(definition.VariableType, GenericScope));
        }