public static int callConstructor(IntPtr luaState) { ObjectTranslator objectTranslator = ObjectTranslator.FromState(luaState); MethodCache methodCache = default(MethodCache); object rawNetObject = objectTranslator.getRawNetObject(luaState, 1); if (rawNetObject == null || !(rawNetObject is IReflect)) { LuaDLL.luaL_error(luaState, "trying to call constructor on an invalid type reference"); LuaDLL.lua_pushnil(luaState); return(1); } IReflect reflect = (IReflect)rawNetObject; LuaDLL.lua_remove(luaState, 1); ConstructorInfo[] constructors = reflect.UnderlyingSystemType.GetConstructors(); ConstructorInfo[] array = constructors; for (int i = 0; i < array.Length; i++) { ConstructorInfo constructorInfo = array[i]; bool flag = objectTranslator.metaFunctions.matchParameters(luaState, constructorInfo, ref methodCache); if (flag) { try { objectTranslator.push(luaState, constructorInfo.Invoke(methodCache.args)); } catch (TargetInvocationException e) { objectTranslator.metaFunctions.ThrowError(luaState, e); LuaDLL.lua_pushnil(luaState); } catch { LuaDLL.lua_pushnil(luaState); } return(1); } } string arg = (constructors.Length != 0) ? constructors[0].Name : "unknown"; LuaDLL.luaL_error(luaState, string.Format("{0} does not contain constructor({1}) argument match", reflect.UnderlyingSystemType, arg)); LuaDLL.lua_pushnil(luaState); return(1); }
private void pushNewObject(IntPtr luaState, object o, int index, string metatable) { LuaDLL.lua_getref(luaState, this.weakTableRef); LuaDLL.luanet_newudata(luaState, index); if (metatable == "luaNet_metatable") { Type type = o.GetType(); ObjectTranslator.PushMetaTable(luaState, o.GetType()); if (LuaDLL.lua_isnil(luaState, -1)) { string assemblyQualifiedName = type.AssemblyQualifiedName; Debugger.Log("<color=green>Create not wrap ulua type:" + assemblyQualifiedName + "</color>", new object[0]); LuaDLL.lua_settop(luaState, -2); LuaDLL.luaL_newmetatable(luaState, assemblyQualifiedName); LuaDLL.lua_pushstring(luaState, "cache"); LuaDLL.lua_newtable(luaState); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushlightuserdata(luaState, LuaDLL.luanet_gettag()); LuaDLL.lua_pushnumber(luaState, 1.0); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction"); LuaDLL.lua_rawget(luaState, LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__gc"); LuaDLL.lua_pushstdcallcfunction(luaState, this.metaFunctions.gcFunction, 0); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__tostring"); LuaDLL.lua_pushstdcallcfunction(luaState, this.metaFunctions.toStringFunction, 0); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__newindex"); LuaDLL.lua_pushstdcallcfunction(luaState, this.metaFunctions.newindexFunction, 0); LuaDLL.lua_rawset(luaState, -3); } } else { LuaDLL.luaL_getmetatable(luaState, metatable); } LuaDLL.lua_setmetatable(luaState, -2); LuaDLL.lua_pushvalue(luaState, -1); LuaDLL.lua_rawseti(luaState, -3, index); LuaDLL.lua_remove(luaState, -2); }
/* * __call metafunction of type references. Searches for and calls * a constructor for the type. Returns nil if the constructor is not * found or if the arguments are invalid. Throws an error if the constructor * generates an exception. */ private int callConstructor(IntPtr luaState) { MethodCache validConstructor = new MethodCache(); IReflect klass; object obj = translator.getRawNetObject(luaState, 1); if (obj == null || !(obj is IReflect)) { translator.throwError(luaState, "trying to call constructor on an invalid type reference"); LuaDLL.lua_pushnil(luaState); return(1); } else { klass = (IReflect)obj; } LuaDLL.lua_remove(luaState, 1); ConstructorInfo[] constructors = klass.UnderlyingSystemType.GetConstructors(); foreach (ConstructorInfo constructor in constructors) { bool isConstructor = matchParameters(luaState, constructor, ref validConstructor); if (isConstructor) { try { translator.push(luaState, constructor.Invoke(validConstructor.args)); } catch (TargetInvocationException e) { translator.throwError(luaState, e.InnerException); LuaDLL.lua_pushnil(luaState); } catch { LuaDLL.lua_pushnil(luaState); } return(1); } } translator.throwError(luaState, "can't find constructor to match arguments"); LuaDLL.lua_pushnil(luaState); return(1); }
public object getAsNetObject(IntPtr luaState, int stackPos) { object obj = this.translator.getRawNetObject(luaState, stackPos); if (obj == null && LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE && LuaDLL.luaL_getmetafield(luaState, stackPos, "__index") != LuaTypes.LUA_TNIL) { if (LuaDLL.luaL_checkmetatable(luaState, -1)) { LuaDLL.lua_insert(luaState, stackPos); LuaDLL.lua_remove(luaState, stackPos + 1); obj = this.translator.getNetObject(luaState, stackPos); } else { LuaDLL.lua_settop(luaState, -2); } } return(obj); }
/* * Pushes a CLR object into the Lua stack as an userdata * with the provided metatable */ public void pushObject(IntPtr luaState, object o, string metatable) { int index = -1; // Pushes nil if (o == null) { LuaDLL.lua_pushnil(luaState); return; } // Object already in the list of Lua objects? Push the stored reference. bool found = (!o.GetType().IsValueType) && objectsBackMap.TryGetValue(o, out index); if (found) { LuaDLL.luaL_getmetatable(luaState, "luaNet_objects"); LuaDLL.lua_rawgeti(luaState, -1, index); // Note: starting with lua5.1 the garbage collector may remove weak reference items (such as our luaNet_objects values) when the initial GC sweep // occurs, but the actual call of the __gc finalizer for that object may not happen until a little while later. During that window we might call // this routine and find the element missing from luaNet_objects, but collectObject() has not yet been called. In that case, we go ahead and call collect // object here // did we find a non nil object in our table? if not, we need to call collect object LuaTypes type = LuaDLL.lua_type(luaState, -1); if (type != LuaTypes.LUA_TNIL) { LuaDLL.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack return; } // MetaFunctions.dumpStack(this, luaState); LuaDLL.lua_remove(luaState, -1); // remove the nil object value LuaDLL.lua_remove(luaState, -1); // remove the metatable collectObject(o, index); // Remove from both our tables and fall out to get a new ID } index = addObject(o); pushNewObject(luaState, o, index, metatable); }
public void PushNewValueObject(IntPtr luaState, object o, int index, string metatable) { if (metatable == "luaNet_metatable") { // Gets or creates the metatable for the object's type LuaDLL.luaL_getmetatable(luaState, o.GetType().AssemblyQualifiedName); if (LuaDLL.lua_isnil(luaState, -1)) { LuaDLL.lua_settop(luaState, -2); LuaDLL.luaL_newmetatable(luaState, o.GetType().AssemblyQualifiedName); LuaDLL.lua_pushstring(luaState, "cache"); LuaDLL.lua_newtable(luaState); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushlightuserdata(luaState, LuaDLL.luanet_gettag()); LuaDLL.lua_pushnumber(luaState, 1); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction"); LuaDLL.lua_rawget(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__gc"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__tostring"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__newindex"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction); LuaDLL.lua_rawset(luaState, -3); } } else { LuaDLL.luaL_getmetatable(luaState, metatable); } LuaDLL.luanet_newudata(luaState, index); LuaDLL.lua_pushvalue(luaState, -2); LuaDLL.lua_remove(luaState, -3); LuaDLL.lua_setmetatable(luaState, -2); }
public void PushCachedObject(IntPtr luaState, int index) { LuaDLL.luaL_getmetatable(luaState, "luaNet_objects"); LuaTypes type = LuaDLL.lua_type(luaState, -1); LuaDLL.lua_rawgeti(luaState, -1, index); type = LuaDLL.lua_type(luaState, -1); if (type != LuaTypes.LUA_TNIL) { LuaDLL.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack } else { LuaDLL.lua_remove(luaState, -1); // remove the nil object value LuaDLL.lua_remove(luaState, -1); // remove the metatable UnityEngine.Debug.LogError("cache object is lost"); } }
public object getAsObject(IntPtr luaState, int stackPos) { if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE) { if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index")) { if (LuaDLL.luaL_checkmetatable(luaState, -1)) { LuaDLL.lua_insert(luaState, stackPos); LuaDLL.lua_remove(luaState, stackPos + 1); } else { LuaDLL.lua_settop(luaState, -2); } } } object obj = translator.getObject(luaState, stackPos); return(obj); }
public static void SetSearcher(IntPtr L, LuaCSFunction loader) { int top = LuaDLL.lua_gettop(L); LuaDLL.wlua_getglobal(L, "package"); //package LuaDLL.wlua_getfield(L, -1, "searchers"); //package,searchers LuaDLL.wlua_getglobal(L, "cswrapfunc"); //package,searchers,wrap LuaDLL.wLua_wrapfunction(L, -1, loader); //package,searchers,wrap,loader LuaDLL.wlua_pushcclosure(L, LuaFuncs.searcher, 1); //package,searchers,wrap,searcher LuaDLL.lua_remove(L, -2); //package,searchers,searcher int searchersIndex = LuaDLL.lua_gettop(L) - 1; for (int e = (int)LuaDLL.lua_rawlen(L, searchersIndex) + 1; e > 1; e--) { LuaDLL.lua_rawgeti(L, searchersIndex, e - 1); //package,searchers,searcher,value LuaDLL.lua_rawseti(L, searchersIndex, e); //package,searchers,searcher } LuaDLL.lua_rawseti(L, searchersIndex, 1); //package,searchers LuaDLL.lua_settop(L, top); }
/* * Pushes a new object into the Lua stack with the provided * metatable */ private void pushNewObject(IntPtr luaState, object o, int index, string metatable) { CreateMetaTable(luaState, o, metatable); // Stores the object index in the Lua list and pushes the index into the Lua stack //if (!o.GetType().IsValueType) //{ LuaDLL.luaL_getmetatable(luaState, "luaNet_objects"); LuaDLL.luanet_newudata(luaState, index); LuaDLL.lua_pushvalue(luaState, -3); LuaDLL.lua_remove(luaState, -4); LuaDLL.lua_setmetatable(luaState, -2); LuaDLL.lua_pushvalue(luaState, -1); LuaDLL.lua_rawseti(luaState, -3, index); LuaDLL.lua_remove(luaState, -2); //} //else //{ // LuaDLL.luanet_newudata(luaState, index); // LuaDLL.lua_pushvalue(luaState, -2); // LuaDLL.lua_remove(luaState, -3); // LuaDLL.lua_setmetatable(luaState, -2); //} }
/* * __call metafunction of CLR delegates, retrieves and calls the delegate. */ private int runFunctionDelegate(KopiLua.Lua.lua_State luaState) { KopiLua.Lua.lua_CFunction func = (KopiLua.Lua.lua_CFunction)translator.getRawNetObject(luaState, 1); LuaDLL.lua_remove(luaState, 1); return(func(luaState)); }
public void LuaRemove(int index) { LuaDLL.lua_remove(L, index); }
/* * Calls the method. Receives the arguments from the Lua stack * and returns values in it. */ public int call(IntPtr luaState) { MethodBase methodToCall = method; object targetObject = target; bool failedCall = true; int nReturnValues = 0; if (!LuaDLL.lua_checkstack(luaState, 5)) { throw new LuaException("Lua stack overflow"); } bool isStatic = (bindingType & BindingFlags.Static) == BindingFlags.Static; SetPendingException(null); if (methodToCall == null) // Method from name { if (isStatic) { targetObject = null; } else { targetObject = extractTarget(luaState, 1); } //LuaDLL.lua_remove(luaState,1); // Pops the receiver if (lastCalledMethod.cachedMethod != null) // Cached? { int numStackToSkip = isStatic ? 0 : 1; // If this is an instance invoe we will have an extra arg on the stack for the targetObject int numArgsPassed = LuaDLL.lua_gettop(luaState) - numStackToSkip; if (numArgsPassed == lastCalledMethod.argTypes.Length) // No. of args match? { if (!LuaDLL.lua_checkstack(luaState, lastCalledMethod.outList.Length + 6)) { throw new LuaException("Lua stack overflow"); } try { for (int i = 0; i < lastCalledMethod.argTypes.Length; i++) { lastCalledMethod.args[lastCalledMethod.argTypes[i].index] = lastCalledMethod.argTypes[i].extractValue(luaState, i + 1 + numStackToSkip); if (lastCalledMethod.args[lastCalledMethod.argTypes[i].index] == null && !LuaDLL.lua_isnil(luaState, i + 1 + numStackToSkip)) { throw new LuaException("argument number " + (i + 1) + " is invalid"); } } if ((bindingType & BindingFlags.Static) == BindingFlags.Static) { translator.push(luaState, lastCalledMethod.cachedMethod.Invoke(null, lastCalledMethod.args)); } else { if (lastCalledMethod.cachedMethod.IsConstructor) { translator.push(luaState, ((ConstructorInfo)lastCalledMethod.cachedMethod).Invoke(lastCalledMethod.args)); } else { translator.push(luaState, lastCalledMethod.cachedMethod.Invoke(targetObject, lastCalledMethod.args)); } } failedCall = false; } catch (TargetInvocationException e) { // Failure of method invocation return(SetPendingException(e.GetBaseException())); } catch (Exception e) { if (members.Length == 1) // Is the method overloaded? // No, throw error { return(SetPendingException(e)); } } } } // Cache miss if (failedCall) { // System.Diagnostics.Debug.WriteLine("cache miss on " + methodName); // If we are running an instance variable, we can now pop the targetObject from the stack if (!isStatic) { if (targetObject == null) { translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", methodName)); LuaDLL.lua_pushnil(luaState); return(1); } LuaDLL.lua_remove(luaState, 1); // Pops the receiver } bool hasMatch = false; string candidateName = null; foreach (MemberInfo member in members) { candidateName = member.ReflectedType.Name + "." + member.Name; MethodBase m = (MethodInfo)member; bool isMethod = translator.matchParameters(luaState, m, ref lastCalledMethod); if (isMethod) { hasMatch = true; break; } } if (!hasMatch) { string msg = (candidateName == null) ? "invalid arguments to method call" : ("invalid arguments to method: " + candidateName); translator.throwError(luaState, msg); LuaDLL.lua_pushnil(luaState); return(1); } } } else // Method from MethodBase instance { if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null) { targetObject = extractTarget(luaState, 1); LuaDLL.lua_remove(luaState, 1); // Pops the receiver } if (!translator.matchParameters(luaState, methodToCall, ref lastCalledMethod)) { translator.throwError(luaState, "invalid arguments to method call"); LuaDLL.lua_pushnil(luaState); return(1); } } if (failedCall) { if (!LuaDLL.lua_checkstack(luaState, lastCalledMethod.outList.Length + 6)) { throw new LuaException("Lua stack overflow"); } try { if (isStatic) { translator.push(luaState, lastCalledMethod.cachedMethod.Invoke(null, lastCalledMethod.args)); } else { if (lastCalledMethod.cachedMethod.IsConstructor) { translator.push(luaState, ((ConstructorInfo)lastCalledMethod.cachedMethod).Invoke(lastCalledMethod.args)); } else { translator.push(luaState, lastCalledMethod.cachedMethod.Invoke(targetObject, lastCalledMethod.args)); } } } catch (TargetInvocationException e) { return(SetPendingException(e.GetBaseException())); } catch (Exception e) { return(SetPendingException(e)); } } // Pushes out and ref return values for (int index = 0; index < lastCalledMethod.outList.Length; index++) { nReturnValues++; //for(int i=0;i<lastCalledMethod.outList.Length;i++) translator.push(luaState, lastCalledMethod.args[lastCalledMethod.outList[index]]); } return(nReturnValues < 1 ? 1 : nReturnValues); }
//public static int luaL_dostring(IntPtr luaState, string chunk) // { // int result = LuaDLL.luaL_loadstring(luaState, chunk); // if (result != 0) // return result; // return LuaDLL.lua_pcall(luaState, 0, LUA_MULTRET, 0); // } public static void wlua_pushcfunction(IntPtr L, LuaCSFunction f) { LuaDLL.wlua_getglobal(L, "cswrapfunc"); //wrap LuaDLL.wLua_wrapfunction(L, -1, f); //wrap,f0 LuaDLL.lua_remove(L, -2); //f0 }
//L: [namespace Table] public bool AddObject(IntPtr L, System.Object obj, string metatable /* nullable */) { IntPtr userdata = IntPtr.Zero; if (luaObjs.TryGetValue(obj, out userdata)) { LuaDLL.lua_rawgeti(L, LuaDLL.LUA_REGISTRYINDEX, weakRefForUserData); //namespace,reftable LuaDLL.lua_pushlightuserdata(L, userdata); //namespace,reftable,userdataKey LuaDLL.lua_rawget(L, -2); //namespace,reftable,userdata LuaDLL.lua_remove(L, -2); //namespace,userdata if (LuaDLL.lua_isuserdata(L, -1)) { return(true); } else { LuaDLL.lua_pop(L, 1); //namespace } } userdata = LuaDLL.lua_newuserdata(L, 1); //namespace,obj if (metatable == null) { Type type = obj.GetType(); while (type != null) { LuaDLL.wlua_getfield(L, -2, type.Name); //namespace,obj,typet if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 1); //namespace,obj type = type.BaseType; continue; } if (LuaDLL.lua_istable(L, -1)) { metatable = type.Name; break; } else { LuaDLL.lua_pop(L, 2); //namespace throw new LuaException(L, "metatable must be a table:" + type.Name); } } } else { LuaDLL.wlua_getfield(L, -2, metatable); //namespace,obj,typet if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 2); //namespace throw new LuaException(L, "failed to find metatable:" + metatable); } } //namespace,obj,typet LuaDLL.lua_setmetatable(L, -2); //namespace,obj objs[userdata.ToInt64()] = obj; luaObjs[obj] = userdata; LuaDLL.lua_rawgeti(L, LuaDLL.LUA_REGISTRYINDEX, weakRefForUserData); //namespace,obj,reftable LuaDLL.lua_pushlightuserdata(L, userdata); //namespace,obj,reftable,userdatakey LuaDLL.lua_pushvalue(L, -3); //namespace,obj,reftable,userdatakey,obj LuaDLL.lua_rawset(L, -3); //namespace,obj,reftable LuaDLL.lua_pop(L, 1); //namespace,obj return(true); }
/* * Calls the method. Receives the arguments from the Lua stack * and returns values in it. */ public int call(KopiLua.Lua.lua_State luaState) { MethodBase methodToCall = _Method; object targetObject = _Target; bool failedCall = true; int nReturnValues = 0; if (!LuaDLL.lua_checkstack(luaState, 5)) { throw new LuaException("Lua stack overflow"); } bool isStatic = (_BindingType & BindingFlags.Static) == BindingFlags.Static; SetPendingException(null); if (methodToCall == null) // Method from name { if (isStatic) { targetObject = null; } else { targetObject = _ExtractTarget(luaState, 1); } //LuaDLL.lua_remove(luaState,1); // Pops the receiver if (_LastCalledMethod.cachedMethod != null) // Cached? { int numStackToSkip = isStatic ? 0 : 1; // If this is an instance invoe we will have an extra arg on the stack for the targetObject int numArgsPassed = LuaDLL.lua_gettop(luaState) - numStackToSkip; MethodBase method = _LastCalledMethod.cachedMethod; if (numArgsPassed == _LastCalledMethod.argTypes.Length) // No. of args match? { if (!LuaDLL.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6)) { throw new LuaException("Lua stack overflow"); } object[] args = _LastCalledMethod.args; try { for (int i = 0; i < _LastCalledMethod.argTypes.Length; i++) { MethodArgs type = _LastCalledMethod.argTypes[i]; object luaParamValue = type.extractValue(luaState, i + 1 + numStackToSkip); if (_LastCalledMethod.argTypes[i].isParamsArray) { args[type.index] = _Translator.tableToArray(luaParamValue, type.paramsArrayType); } else { args[type.index] = luaParamValue; } if (args[type.index] == null && !LuaDLL.lua_isnil(luaState, i + 1 + numStackToSkip)) { throw new LuaException("argument number " + (i + 1) + " is invalid"); } } if ((_BindingType & BindingFlags.Static) == BindingFlags.Static) { _Translator.push(luaState, method.Invoke(null, args)); } else { if (_LastCalledMethod.cachedMethod.IsConstructor) { _Translator.push(luaState, ((ConstructorInfo)method).Invoke(args)); } else { _Translator.push(luaState, method.Invoke(targetObject, args)); } } failedCall = false; } catch (TargetInvocationException e) { // Failure of method invocation return(SetPendingException(e.GetBaseException())); } catch (Exception e) { if (_Members.Length == 1) // Is the method overloaded? // No, throw error { return(SetPendingException(e)); } } } } // Cache miss if (failedCall) { // System.Diagnostics.Debug.WriteLine("cache miss on " + methodName); // If we are running an instance variable, we can now pop the targetObject from the stack if (!isStatic) { if (targetObject == null) { _Translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", _MethodName)); LuaDLL.lua_pushnil(luaState); return(1); } LuaDLL.lua_remove(luaState, 1); // Pops the receiver } bool hasMatch = false; string candidateName = null; foreach (MemberInfo member in _Members) { candidateName = member.ReflectedType.Name + "." + member.Name; MethodBase m = (MethodInfo)member; bool isMethod = _Translator.matchParameters(luaState, m, ref _LastCalledMethod); if (isMethod) { hasMatch = true; break; } } if (!hasMatch) { string msg = (candidateName == null) ? "invalid arguments to method call" : ("invalid arguments to method: " + candidateName); _Translator.throwError(luaState, msg); LuaDLL.lua_pushnil(luaState); return(1); } } } else // Method from MethodBase instance { if (methodToCall.ContainsGenericParameters) { // bool isMethod = //* not used _Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod); if (methodToCall.IsGenericMethodDefinition) { //need to make a concrete type of the generic method definition List <Type> typeArgs = new List <Type>(); foreach (object arg in _LastCalledMethod.args) { typeArgs.Add(arg.GetType()); } MethodInfo concreteMethod = (methodToCall as MethodInfo).MakeGenericMethod(typeArgs.ToArray()); _Translator.push(luaState, concreteMethod.Invoke(targetObject, _LastCalledMethod.args)); failedCall = false; } else if (methodToCall.ContainsGenericParameters) { _Translator.throwError(luaState, "unable to invoke method on generic class as the current method is an open generic method"); LuaDLL.lua_pushnil(luaState); return(1); } } else { if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null) { targetObject = _ExtractTarget(luaState, 1); LuaDLL.lua_remove(luaState, 1); // Pops the receiver } if (!_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod)) { _Translator.throwError(luaState, "invalid arguments to method call"); LuaDLL.lua_pushnil(luaState); return(1); } } } if (failedCall) { if (!LuaDLL.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6)) { throw new LuaException("Lua stack overflow"); } try { if (isStatic) { _Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(null, _LastCalledMethod.args)); } else { if (_LastCalledMethod.cachedMethod.IsConstructor) { _Translator.push(luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke(_LastCalledMethod.args)); } else { _Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(targetObject, _LastCalledMethod.args)); } } } catch (TargetInvocationException e) { return(SetPendingException(e.GetBaseException())); } catch (Exception e) { return(SetPendingException(e)); } } // Pushes out and ref return values for (int index = 0; index < _LastCalledMethod.outList.Length; index++) { nReturnValues++; _Translator.push(luaState, _LastCalledMethod.args[_LastCalledMethod.outList[index]]); } //by isSingle 2010-09-10 11:26:31 //Desc: // if not return void,we need add 1, // or we will lost the function's return value // when call dotnet function like "int foo(arg1,out arg2,out arg3)" in lua code if (!_LastCalledMethod.IsReturnVoid && nReturnValues > 0) { nReturnValues++; } return(nReturnValues < 1 ? 1 : nReturnValues); }
public int call(IntPtr luaState) { MethodBase method = this._Method; object obj = this._Target; bool flag = true; int num = 0; if (!LuaDLL.lua_checkstack(luaState, 5)) { throw new LuaException("Lua stack overflow"); } bool flag2 = (this._BindingType & BindingFlags.Static) == BindingFlags.Static; this.SetPendingException(null); if (method == null) { if (flag2) { obj = null; } else { obj = this._ExtractTarget(luaState, 1); } if (this._LastCalledMethod.cachedMethod != null) { int num2 = (!flag2) ? 1 : 0; int num3 = LuaDLL.lua_gettop(luaState) - num2; MethodBase cachedMethod = this._LastCalledMethod.cachedMethod; if (num3 == this._LastCalledMethod.argTypes.Length) { if (!LuaDLL.lua_checkstack(luaState, this._LastCalledMethod.outList.Length + 6)) { throw new LuaException("Lua stack overflow"); } object[] args = this._LastCalledMethod.args; try { for (int i = 0; i < this._LastCalledMethod.argTypes.Length; i++) { MethodArgs methodArgs = this._LastCalledMethod.argTypes[i]; object obj2 = methodArgs.extractValue(luaState, i + 1 + num2); if (this._LastCalledMethod.argTypes[i].isParamsArray) { args[methodArgs.index] = this._Translator.tableToArray(obj2, methodArgs.paramsArrayType); } else { args[methodArgs.index] = obj2; } if (args[methodArgs.index] == null && !LuaDLL.lua_isnil(luaState, i + 1 + num2)) { throw new LuaException("argument number " + (i + 1) + " is invalid"); } } if ((this._BindingType & BindingFlags.Static) == BindingFlags.Static) { this._Translator.push(luaState, cachedMethod.Invoke(null, args)); } else if (this._LastCalledMethod.cachedMethod.IsConstructor) { this._Translator.push(luaState, ((ConstructorInfo)cachedMethod).Invoke(args)); } else { this._Translator.push(luaState, cachedMethod.Invoke(obj, args)); } flag = false; } catch (TargetInvocationException ex) { int result = this.SetPendingException(ex.GetBaseException()); return(result); } catch (Exception pendingException) { if (this._Members.Length == 1) { int result = this.SetPendingException(pendingException); return(result); } } } } if (flag) { if (!flag2) { if (obj == null) { this._Translator.throwError(luaState, string.Format("instance method '{0}' requires a non null target object", this._MethodName)); LuaDLL.lua_pushnil(luaState); return(1); } LuaDLL.lua_remove(luaState, 1); } bool flag3 = false; string text = null; MemberInfo[] members = this._Members; for (int j = 0; j < members.Length; j++) { MemberInfo memberInfo = members[j]; text = memberInfo.ReflectedType.Name + "." + memberInfo.Name; MethodBase method2 = (MethodInfo)memberInfo; bool flag4 = this._Translator.matchParameters(luaState, method2, ref this._LastCalledMethod); if (flag4) { flag3 = true; break; } } if (!flag3) { string message = (text != null) ? ("invalid arguments to method: " + text) : "invalid arguments to method call"; LuaDLL.luaL_error(luaState, message); LuaDLL.lua_pushnil(luaState); this.ClearCachedArgs(); return(1); } } } else if (method.ContainsGenericParameters) { this._Translator.matchParameters(luaState, method, ref this._LastCalledMethod); if (method.IsGenericMethodDefinition) { List <Type> list = new List <Type>(); object[] args2 = this._LastCalledMethod.args; for (int k = 0; k < args2.Length; k++) { object obj3 = args2[k]; list.Add(obj3.GetType()); } MethodInfo methodInfo = (method as MethodInfo).MakeGenericMethod(list.ToArray()); this._Translator.push(luaState, methodInfo.Invoke(obj, this._LastCalledMethod.args)); flag = false; } else if (method.ContainsGenericParameters) { LuaDLL.luaL_error(luaState, "unable to invoke method on generic class as the current method is an open generic method"); LuaDLL.lua_pushnil(luaState); this.ClearCachedArgs(); return(1); } } else { if (!method.IsStatic && !method.IsConstructor && obj == null) { obj = this._ExtractTarget(luaState, 1); LuaDLL.lua_remove(luaState, 1); } if (!this._Translator.matchParameters(luaState, method, ref this._LastCalledMethod)) { LuaDLL.luaL_error(luaState, "invalid arguments to method call"); LuaDLL.lua_pushnil(luaState); this.ClearCachedArgs(); return(1); } } if (flag) { if (!LuaDLL.lua_checkstack(luaState, this._LastCalledMethod.outList.Length + 6)) { this.ClearCachedArgs(); throw new LuaException("Lua stack overflow"); } try { if (flag2) { this._Translator.push(luaState, this._LastCalledMethod.cachedMethod.Invoke(null, this._LastCalledMethod.args)); } else if (this._LastCalledMethod.cachedMethod.IsConstructor) { this._Translator.push(luaState, ((ConstructorInfo)this._LastCalledMethod.cachedMethod).Invoke(this._LastCalledMethod.args)); } else { this._Translator.push(luaState, this._LastCalledMethod.cachedMethod.Invoke(obj, this._LastCalledMethod.args)); } } catch (TargetInvocationException ex2) { this.ClearCachedArgs(); int result = this.SetPendingException(ex2.GetBaseException()); return(result); } catch (Exception pendingException2) { this.ClearCachedArgs(); int result = this.SetPendingException(pendingException2); return(result); } } for (int l = 0; l < this._LastCalledMethod.outList.Length; l++) { num++; this._Translator.push(luaState, this._LastCalledMethod.args[this._LastCalledMethod.outList[l]]); } if (!this._LastCalledMethod.IsReturnVoid && num > 0) { num++; } this.ClearCachedArgs(); return((num >= 1) ? num : 1); }