Пример #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.");
        }
Пример #2
0
        private void InterpretInstruction(Instruction instruction, CilHandlerContext context)
        {
            var handler = _handlers.GetValueOrDefault(instruction.OpCode);

            if (handler == null)
            {
                throw new NotImplementedException($"Instruction {instruction.OpCode} is not implemented.");
            }

            handler.Handle(instruction, context);
        }