Ejemplo n.º 1
0
        public override CilValueReference Store(CilObject obj)
        {
            var address = _nextAddress;

            _dictionary[address] = obj;
            _nextAddress++;
            return(new CilValueReference(address));
        }
Ejemplo n.º 2
0
        public override IValue Unbox(CilObject obj, CilManagedMemory managedMemory, CilProgram program)
        {
            if (obj is CilObjectExternal objExternal)
            {
                if (objExternal.ExternalObject.GetType().IsEnum)
                {
                    var result = Convert.ToInt32(objExternal.ExternalObject);
                    return(new CilValueInt32(result));
                }
            }

            throw new NotImplementedException();
        }
        public override void VisitBoxInstruction(BoxInstruction instruction)
        {
            var cilType = instruction.TypeSpec.GetCilType(_program);

            ControlState.EvaluationStack.PopValue(_program, cilType, out var val);

            CilObject obj = null;

            if (cilType.IsValueType(_program) && !cilType.IsNullable)
            {
                obj = val.Box();
            }

            var objRef = ManagedMemory.Store(obj);

            ControlState.EvaluationStack.PushValue(objRef);

            ControlState.MoveToNextInstruction();
        }
Ejemplo n.º 4
0
        public override void VisitCallVirtualInstruction(CallVirtualInstruction instruction)
        {
            if (!instruction.CallConv.IsInstance)
            {
                throw new System.NotImplementedException();
            }

            var methodArgs = PopMethodArgs(instruction);

            ControlState.EvaluationStack.PopValue(_program, instruction.TypeSpec.GetCilType(_program), out var thisVal);

            var callExternal = true;

            CilClassInstance thisClassInstance = null;
            CilObject        thisObject        = null;

            if (thisVal is CilValueReference thisValRef)
            {
                var thisObj = ManagedMemory.Load(thisValRef);

                if (thisObj is CilClassInstance)
                {
                    thisClassInstance = thisObj as CilClassInstance;
                }
                else
                {
                    thisObject   = thisObj;
                    callExternal = true;
                }
            }
            else
            {
                throw new System.NotImplementedException();
            }

            CilMethod method = null;

            if (thisClassInstance != null)
            {
                var currentClass = thisClassInstance.Class;
                while (true)
                {
                    var possibleMethod = currentClass.Methods.SingleOrDefault(m => m.Name == instruction.MethodName && AreArgumentsAssignable(instruction.SigArgs, m.Arguments));
                    if (possibleMethod != null)
                    {
                        method       = possibleMethod;
                        callExternal = false;
                        break;
                    }

                    currentClass = currentClass.Extends;
                    if (currentClass == null)
                    {
                        callExternal = true;
                        break;
                    }
                }
            }

            if (callExternal)
            {
                var result = CallExternalMethod(instruction, thisVal, methodArgs.ToArray(), null);

                if (!(instruction.ReturnType is CilTypeVoid))
                {
                    var resultVal = instruction.ReturnType.CreateValueFromRuntime(result, ManagedMemory, _program);
                    ControlState.EvaluationStack.PushValue(resultVal);
                }

                ControlState.MoveToNextInstruction();
            }
            else
            {
                var sigArgsWithThis = CompleteSigArgs(instruction, method);
                var argsWithThis    = CompleteArgs(instruction, methodArgs, thisVal);

                var newMethodState = new CilMethodState(method, sigArgsWithThis, argsWithThis, _program);

                ControlState.MoveToNextInstruction();
                ControlState.CallStack.Push(newMethodState);
            }
        }
Ejemplo n.º 5
0
 public override IValue Unbox(CilObject obj, CilManagedMemory managedMemory, CilProgram program)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 6
0
 public abstract CilValueReference Store(CilObject obj);
Ejemplo n.º 7
0
 public abstract IValue Unbox(CilObject obj, CilManagedMemory managedMemory, CilProgram program);