private void Newobj(Instruction instruction) { var methodRef = (MethodReference)instruction.Operand; var typeRef = methodRef.DeclaringType; if (typeRef.IsArray) { var arrayType = (ArrayType)typeRef; var scriptType = ScriptContext.GetType(arrayType.ElementType); var hostArrayType = scriptType.HostType.MakeArrayType(arrayType.Rank); var callArgs = new ScriptCallArguments(methodRef.Parameters.Count, methodRef.Parameters.Select(_ => ScriptContext.GetType(_.ParameterType)).ToArray()); _runtimeContext.PushToStack(Activator.CreateInstance(hostArrayType, callArgs.Arguments)); } else { var scriptType = ScriptContext.GetType(typeRef); var method = scriptType.GetMethod(methodRef); var callArgs = new ScriptCallArguments(method); var scriptObject = new ScriptObject(scriptType); if (scriptType.HostType.IsSubclassOf(typeof(Delegate))) { var scriptDelegate = new ScriptDelegate(callArgs.Arguments[0], (ScriptMethodBase)callArgs.Arguments[1]); var invokeMethod = scriptType.TypeDefinition.Methods.FirstOrDefault(_ => _.Name == "Invoke"); if (invokeMethod != null) { method.Invoke(scriptObject, scriptDelegate, ScriptDelegate.GetInvokePtr(invokeMethod.Parameters.Count)); } } else { method.Invoke(scriptObject, callArgs.Arguments); } _runtimeContext.PushToStack(scriptObject); } }
private void Call(Instruction instruction) { var methodRef = (MethodReference)instruction.Operand; var type = ScriptContext.GetType(methodRef.DeclaringType); var method = type.GetMethod(methodRef, _runtimeContext.CurrentStackFrame.ScriptMethod as ScriptMethodInfo); if (method == null) { return; } var callArgs = new ScriptCallArguments(method); var targetObject = methodRef.HasThis ? _runtimeContext.PopFromStack() : null; var ret = method.Invoke(targetObject, callArgs.Arguments); if (method.HasReturn) { _runtimeContext.PushToStack(ret); } }