object Run(ulong codeAddr, uint key, VMFuncSig sig, object[] arguments) { if (currentCtx != null) { ctxStack.Push(currentCtx); } currentCtx = new VMContext(this); try { Debug.Assert(sig.ParamTypes.Length == arguments.Length); currentCtx.Stack.SetTopPosition((uint)arguments.Length + 1); for (uint i = 0; i < arguments.Length; i++) { currentCtx.Stack[i + 1] = VMSlot.FromObject(arguments[i], sig.ParamTypes[i]); } currentCtx.Stack[(uint)arguments.Length + 1] = new VMSlot { U8 = 1 }; currentCtx.Registers[Constants.REG_K1] = new VMSlot { U4 = key }; currentCtx.Registers[Constants.REG_BP] = new VMSlot { U4 = 0 }; currentCtx.Registers[Constants.REG_SP] = new VMSlot { U4 = (uint)arguments.Length + 1 }; currentCtx.Registers[Constants.REG_IP] = new VMSlot { U8 = codeAddr }; VMDispatcher.Run(currentCtx); Debug.Assert(currentCtx.EHStack.Count == 0); object retVal = null; if (sig.RetType != typeof(void)) { var retSlot = currentCtx.Registers[Constants.REG_R0]; if (Type.GetTypeCode(sig.RetType) == TypeCode.String && retSlot.O == null) { retVal = Data.LookupString(retSlot.U4); } else { retVal = retSlot.ToObject(sig.RetType); } } return(retVal); } finally { currentCtx.Stack.FreeAllLocalloc(); if (ctxStack.Count > 0) { currentCtx = ctxStack.Pop(); } } }
private void Run(ulong codeAddr, uint key, VMFuncSig sig, void *[] arguments, void *retTypedRef) { if (currentCtx != null) { ctxStack.Push(currentCtx); } currentCtx = new VMContext(this); try { Debug.Assert(sig.ParamTypes.Length == arguments.Length); currentCtx.Stack.SetTopPosition((uint)arguments.Length + 1); for (uint i = 0; i < arguments.Length; i++) { var paramType = sig.ParamTypes[i]; if (paramType.IsByRef) { currentCtx.Stack[i + 1] = new VMSlot { O = new TypedRef(arguments[i]) }; } else { var typedRef = *(TypedReference *)arguments[i]; currentCtx.Stack[i + 1] = VMSlot.FromObject(TypedReference.ToObject(typedRef), __reftype(typedRef)); } } currentCtx.Stack[(uint)arguments.Length + 1] = new VMSlot { U8 = 1 }; currentCtx.Registers[Constants.REG_K1] = new VMSlot { U4 = key }; currentCtx.Registers[Constants.REG_BP] = new VMSlot { U4 = 0 }; currentCtx.Registers[Constants.REG_SP] = new VMSlot { U4 = (uint)arguments.Length + 1 }; currentCtx.Registers[Constants.REG_IP] = new VMSlot { U8 = codeAddr }; VMDispatcher.Run(currentCtx); Debug.Assert(currentCtx.EHStack.Count == 0); if (sig.RetType != typeof(void)) { if (sig.RetType.IsByRef) { var retRef = currentCtx.Registers[Constants.REG_R0].O; if (!(retRef is IReference)) { throw new ExecutionEngineException(); } ((IReference)retRef).ToTypedReference(currentCtx, retTypedRef, sig.RetType.GetElementType()); } else { var retSlot = currentCtx.Registers[Constants.REG_R0]; object retVal; if (Type.GetTypeCode(sig.RetType) == TypeCode.String && retSlot.O == null) { retVal = Data.LookupString(retSlot.U4); } else { retVal = retSlot.ToObject(sig.RetType); } TypedReferenceHelpers.SetTypedRef(retVal, retTypedRef); } } } finally { currentCtx.Stack.FreeAllLocalloc(); if (ctxStack.Count > 0) { currentCtx = ctxStack.Pop(); } } }