コード例 #1
0
ファイル: IsinstHandler.cs プロジェクト: ashmind/Cilin
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var type     = context.Resolver.Type((TypeReference)instruction.Operand, context.GenericScope);
            var instance = context.Stack.Pop();

            context.Stack.Push(type.IsInstanceOfType(instance) ? instance : null);
        }
コード例 #2
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var array = (Array)ObjectWrapper.UnwrapIfRequired(context.Stack.Pop());
            var value = array.GetLength(0);

            context.Stack.Push(value);
        }
コード例 #3
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var method = context.Resolver.Method((MethodReference)instruction.Operand, context.GenericScope);

            var parameters = method.GetParameters();
            var arguments  = new object[parameters.Length];

            for (var i = parameters.Length - 1; i >= 0; i--)
            {
                arguments[i] = TypeSupport.Convert(context.Stack.Pop(), parameters[i].ParameterType);
            }

            if (instruction.OpCode == OpCodes.Newobj)
            {
                var instance = context.Invoker.Invoke(method, null, arguments, context.Method);
                context.Stack.Push(instance);
                return;
            }

            var target = (object)null;

            if (!method.IsStatic)
            {
                target = TypeSupport.Convert(context.Stack.Pop(), method.DeclaringType);
            }

            var result = context.Invoker.Invoke(method, target, arguments, context.Method);

            if (!method.IsConstructor && ((MethodInfo)method).ReturnType != typeof(void))
            {
                context.Stack.Push(result);
            }
        }
コード例 #4
0
ファイル: MathHandler.cs プロジェクト: ashmind/Cilin
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var right = context.Stack.Pop();
            var left  = context.Stack.Pop();

            context.Stack.Push(Calculate(instruction, left, right));
        }
コード例 #5
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var right = context.Stack.Pop();
            var left  = context.Stack.Pop();

            context.Stack.Push(Compare(instruction, left, right) ? 1 : 0);
        }
コード例 #6
0
        private bool IsBinaryConditionTrue(Func <object, object, bool> isTrue, CilHandlerContext context)
        {
            var right = context.Stack.Pop();
            var left  = context.Stack.Pop();

            return(isTrue(left, right));
        }
コード例 #7
0
        private bool IsConditionTrue(Instruction instruction, CilHandlerContext context)
        {
            switch (instruction.OpCode.Code)
            {
            case Code.Br:
            case Code.Br_S:
                return(true);

            case Code.Brtrue:
            case Code.Brtrue_S:
                return(TypeSupport.Convert <bool>(context.Stack.Pop()));

            case Code.Brfalse:
            case Code.Brfalse_S:
                return(!TypeSupport.Convert <bool>(context.Stack.Pop()));

            case Code.Beq_S: return(IsBinaryConditionTrue(Primitives.Equal, context));

            case Code.Bne_Un_S: return(IsBinaryConditionTrue(Primitives.NotEqual, context));

            case Code.Blt_S: return(IsBinaryConditionTrue(Primitives.IsLessThan, context));

            case Code.Ble_S: return(IsBinaryConditionTrue(Primitives.IsLessThanOrEqual, context));

            default:
                throw new NotImplementedException();
            }
        }
コード例 #8
0
ファイル: NewArrHandler.cs プロジェクト: ashmind/Cilin
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var length    = (int)Primitives.Convert(context.Stack.Pop(), typeof(int));
            var arrayType = context.Resolver.Type(new ArrayType((TypeReference)instruction.Operand), context.GenericScope);

            context.Stack.Push(TypeSupport.CreateArray(arrayType, length));
        }
コード例 #9
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var index = TypeSupport.Convert <IntPtr>(context.Stack.Pop());
            var array = (Array)ObjectWrapper.UnwrapIfRequired(context.Stack.Pop());

            var value = array.GetValue(index.ToInt64());

            context.Stack.Push(value);
        }
コード例 #10
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            int index;

            if (!SpecialMap.TryGetValue(instruction.OpCode, out index))
            {
                index = (int)instruction.Operand;
            }

            context.Stack.Push(GetArgumentValueOrThis(context, index));
        }
コード例 #11
0
ファイル: StelemHandler.cs プロジェクト: ashmind/Cilin
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var value = context.Stack.Pop();
            var index = TypeSupport.Convert <IntPtr>(context.Stack.Pop());
            var array = (Array)ObjectWrapper.UnwrapIfRequired(context.Stack.Pop());

            array.SetValue(
                TypeSupport.Convert(value, array.GetType().GetElementType()),
                index.ToInt64()
                );
        }
コード例 #12
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            if (!IsConditionTrue(instruction, context))
            {
                return;
            }

            var target = (Instruction)instruction.Operand;

            context.NextInstruction = target;
        }
コード例 #13
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            int index;

            if (!SpecialMap.TryGetValue(instruction.OpCode, out index))
            {
                index = ((VariableDefinition)instruction.Operand).Index;
            }

            context.Stack.Push(context.Variables[index]);
        }
コード例 #14
0
ファイル: CastclassHandler.cs プロジェクト: ashmind/Cilin
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var type     = context.Resolver.Type((TypeReference)instruction.Operand, context.GenericScope);
            var instance = context.Stack.Pop();

            if (!type.IsInstanceOfType(instance))
            {
                throw new InvalidCastException($"Specified cast is not valid (instance: {instance}, type: {type}).");
            }

            context.Stack.Push(instance);
        }
コード例 #15
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var special = SpecialMap.GetValueOrDefault(instruction.OpCode);

            if (special != null)
            {
                context.Stack.Push(special);
                return;
            }

            context.Stack.Push(instruction.Operand);
        }
コード例 #16
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var value = context.Stack.Pop();

            int index;

            if (!SpecialMap.TryGetValue(instruction.OpCode, out index))
            {
                index = ((VariableDefinition)instruction.Operand).Index;
            }

            context.SetVariable(index, value);
        }
コード例 #17
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var method      = context.Resolver.Method((MethodReference)instruction.Operand, context.GenericScope);
            var interpreted = method as InterpretedMethod;

            if (interpreted != null)
            {
                context.Stack.Push(new NonRuntimeMethodPointer(interpreted));
                return;
            }

            context.Stack.Push(method.MethodHandle.GetFunctionPointer());
        }
コード例 #18
0
        private object GetArgumentValueOrThis(CilHandlerContext context, int index)
        {
            if (context.Definition.HasThis)
            {
                if (index == 0)
                {
                    return(context.Target);
                }

                index -= 1;
            }

            return(context.Arguments[index]);
        }
コード例 #19
0
ファイル: LdfldHandler.cs プロジェクト: ashmind/Cilin
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var field = context.Resolver.Field((FieldReference)instruction.Operand, context.GenericScope);

            var target = (object)null;

            if (instruction.OpCode == OpCodes.Ldfld)
            {
                target = TypeSupport.Convert(context.Stack.Pop(), field.DeclaringType);
            }

            var value = field.GetValue(target);

            context.Stack.Push(value);
        }
コード例 #20
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var reference = (MemberReference)instruction.Operand;

            var typeReference = reference as TypeReference;

            if (typeReference == null)
            {
                throw new NotImplementedException();
            }

            var resolved   = context.Resolver.Type(typeReference, context.GenericScope);
            var nonRuntime = resolved as NonRuntimeType;

            if (nonRuntime != null)
            {
                context.Stack.Push(nonRuntime.TypeHandle);
                return;
            }

            context.Stack.Push(resolved.TypeHandle);
        }
コード例 #21
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var target = (Instruction)instruction.Operand;

            context.NextInstruction = target;
        }
コード例 #22
0
ファイル: DupHandler.cs プロジェクト: ashmind/Cilin
 public void Handle(Instruction instruction, CilHandlerContext context)
 {
     context.Stack.Push(context.Stack.Peek());
 }
コード例 #23
0
 public void Handle(Instruction instruction, CilHandlerContext context)
 {
     context.Stack.Pop();
 }
コード例 #24
0
ファイル: NopHandler.cs プロジェクト: ashmind/Cilin
 public void Handle(Instruction instruction, CilHandlerContext context)
 {
     // nop
 }
コード例 #25
0
        public void Handle(Instruction instruction, CilHandlerContext context)
        {
            var converted = Primitives.Convert(context.Stack.Pop(), TypeMap[instruction.OpCode]);

            context.Stack.Push(converted);
        }