void ILuaAPI.CallK(int numArgs, int numResults, int context, CSharpFunctionDelegate continueFunc) { Utl.ApiCheck(continueFunc == null || !CI.IsLua, "cannot use continuations inside hooks"); Utl.ApiCheckNumElems(this, numArgs + 1); Utl.ApiCheck(Status == ThreadStatus.LUA_OK, "cannot do calls on non-normal thread"); CheckResults(numArgs, numResults); var func = Stack[Top.Index - (numArgs + 1)]; // need to prepare continuation? if (continueFunc != null && NumNonYieldable == 0) { CI.ContinueFunc = continueFunc; CI.Context = context; D_Call(func, numResults, true); } // no continuation or no yieldable else { D_Call(func, numResults, false); } AdjustResults(numResults); }
/* * __call metafunction of CLR delegates, retrieves and calls the delegate. */ private int runFunctionDelegate(ILuaState luaState) { CSharpFunctionDelegate func = (CSharpFunctionDelegate)translator.getRawNetObject(luaState, 1); LuaDLL.lua_remove(luaState, 1); return(func(luaState)); }
public Lua() { luaState = LuaDLL.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone) //LuaDLL.luaopen_base(luaState); // steffenj: luaopen_* no longer used LuaDLL.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here) LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(luaState, true); LuaDLL.lua_settable(luaState, (int)LuaDef.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(luaState); LuaDLL.lua_setglobal(luaState, "luanet"); LuaDLL.lua_pushglobal(luaState); LuaDLL.lua_getglobal(luaState, "luanet"); LuaDLL.lua_pushstring(luaState, "getmetatable"); LuaDLL.lua_getglobal(luaState, "getmetatable"); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_replaceglobal(luaState); translator = new ObjectTranslator(this, luaState); LuaDLL.lua_replaceglobal(luaState); LuaDLL.luaL_dostring(luaState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new CSharpFunctionDelegate(PanicCallback); LuaDLL.lua_atpanic(luaState, panicCallback); //LuaDLL.lua_atlock(luaState, lockCallback = new CSharpFunctionDelegate(LockCallback)); //LuaDLL.lua_atunlock(luaState, unlockCallback = new CSharpFunctionDelegate(UnlockCallback)); }
internal void SetClLcsValue(CSharpFunctionDelegate v) { #if DEBUG_DUMMY_TVALUE_MODIFY CheckLock(); #endif Tt = (int)LuaType.LUA_TFUNCTION; OValue = v; }
ThreadStatus ILuaAPI.PCallK(int numArgs, int numResults, int errFunc, int context, CSharpFunctionDelegate continueFunc) { Utl.ApiCheck(continueFunc == null || !CI.IsLua, "cannot use continuations inside hooks"); Utl.ApiCheckNumElems(this, numArgs + 1); Utl.ApiCheck(Status == ThreadStatus.LUA_OK, "cannot do calls on non-normal thread"); CheckResults(numArgs, numResults); int func; if (errFunc == 0) { func = 0; } else { StkId addr; if (!Index2Addr(errFunc, out addr)) { Utl.InvalidIndex(); } func = addr.Index; } ThreadStatus status; CallS c = new CallS(); c.L = this; c.FuncIndex = Top.Index - (numArgs + 1); if (continueFunc == null || NumNonYieldable > 0) // no continuation or no yieldable? { c.NumResults = numResults; status = D_PCall(DG_F_Call, ref c, c.FuncIndex, func); } else { int ciIndex = CI.Index; CI.ContinueFunc = continueFunc; CI.Context = context; CI.ExtraIndex = c.FuncIndex; CI.OldAllowHook = AllowHook; CI.OldErrFunc = ErrFunc; ErrFunc = func; CI.CallStatus |= CallStatus.CIST_YPCALL; D_Call(Stack[c.FuncIndex], numResults, true); CallInfo ci = BaseCI[ciIndex]; ci.CallStatus &= ~CallStatus.CIST_YPCALL; ErrFunc = ci.OldErrFunc; status = ThreadStatus.LUA_OK; } AdjustResults(numResults); return(status); }
public GCHandle PushCSharpFunction(CSharpFunctionDelegate f) { GCHandle handle = GCHandle.Alloc(f); IntPtr p = GCHandle.ToIntPtr(handle); LuaAPI.lua_pushlightuserdata(this.luaPtr, p); LuaAPI.lua_pushcclosure(this.luaPtr, this.pfnDispatch, 1); return(handle); }
public GCHandle PushCSharpClosure(CSharpFunctionDelegate f, int n) { GCHandle handle = GCHandle.Alloc(f); IntPtr p = GCHandle.ToIntPtr(handle); LuaAPI.lua_pushlightuserdata(this.m_lua, p); LuaAPI.lua_insert(this.m_lua, -(n + 1)); LuaAPI.lua_pushcclosure(this.m_lua, this.m_pfnDispatch, n + 1); return(handle); }
/////private function //////////////////////////////////////////////////// // void CreateObject<Type>(Type* pObject,string szVarName) where Type:object // { // if(LuaClassName<Type>::Name() == NULL){ // WriteError("lua error:before you add member function, regist the type first.\n"); // return; // } // // PushObject<Type>(pObject); // lua_setglobal(m_lua,szVarName); // } private void SetMetatable(int index, CSharpFunctionDelegate pfnReader, CSharpFunctionDelegate pfnWriter) { LuaApi.PushValue(index); LuaApi.NewTable(); LuaApi.PushString("__index"); LuaApi.PushCSharpFunction(pfnReader); LuaApi.SetTable(-3); LuaApi.PushString("__newindex"); LuaApi.PushCSharpFunction(pfnWriter); LuaApi.SetTable(-3); LuaApi.SetMetaTable(-2); }
public LuaCsClosureValue(CSharpFunctionDelegate f, int numUpvalues) { F = f; Upvals = new StkId[numUpvalues]; for (int i = 0; i < numUpvalues; ++i) { StkId newItem = new StkId(); Upvals[i] = newItem; newItem.SetList(Upvals); newItem.SetIndex(i); } }
public MetaFunctions(ObjectTranslator translator) { this.translator = translator; gcFunction = new CSharpFunctionDelegate(this.collectObject); toStringFunction = new CSharpFunctionDelegate(this.toString); indexFunction = new CSharpFunctionDelegate(this.getMethod); newindexFunction = new CSharpFunctionDelegate(this.setFieldOrProperty); baseIndexFunction = new CSharpFunctionDelegate(this.getBaseMethod); callConstructorFunction = new CSharpFunctionDelegate(this.callConstructor); classIndexFunction = new CSharpFunctionDelegate(this.getClassMethod); classNewindexFunction = new CSharpFunctionDelegate(this.setClassFieldOrProperty); execDelegateFunction = new CSharpFunctionDelegate(this.runFunctionDelegate); test = new CSharpFunctionDelegate(this.testDelegate); }
public void L_RequireF(string moduleName, CSharpFunctionDelegate openFunc, bool global) { API.PushCSharpFunction(openFunc); API.PushString(moduleName); API.Call(1, 1); L_GetSubTable(LuaDef.LUA_REGISTRYINDEX, "_LOADED"); API.PushValue(-2); API.SetField(-2, moduleName); API.Pop(1); if (global) { API.PushValue(-1); API.SetGlobal(moduleName); } }
public void Export(string libName, LuaFuncPair[] funcs, bool global) { CSharpFunctionDelegate newlib = lua => { NameFuncPair[] define = new NameFuncPair[funcs.Length]; for (int i = 0; i < funcs.Length; i++) { define[i] = funcs[i]; } lua.L_NewLib(define); return(1); }; mLuaState.L_RequireF(libName, newlib, global); }
private static void CreateSearchersTable(ILuaState lua) { CSharpFunctionDelegate[] searchers = new CSharpFunctionDelegate[] { SearcherPreload, SearcherLua, }; lua.CreateTable(searchers.Length, 0); for (int i = 0; i < searchers.Length; ++i) { lua.PushValue(-2); // set `package' as upvalue for all searchers lua.PushCSharpClosure(searchers[i], 1); lua.RawSetI(-2, i + 1); } }
public static int LoadMod(ILuaState lua) { var cn = lua.L_CheckString(1); // 第一个参数 try { Type c = Type.GetType(cn, true, true); MethodInfo mi = c.GetMethod("OpenLib", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); CSharpFunctionDelegate openLibFun = Delegate.CreateDelegate(typeof(CSharpFunctionDelegate), mi.GetBaseDefinition()) as CSharpFunctionDelegate; //CSharpFunctionDelegate openLibFun = mi.CreateDelegate(typeof(CSharpFunctionDelegate)) as CSharpFunctionDelegate; LuaScriptDelegate.State.L_RequireF(c.Name + ".cs", openLibFun, false); } catch (Exception e) { Debug.LogError(e.Message); } return(1); }
/// <summary> /// table /// </summary> /// <param name="func"></param> /// <param name="get"></param> /// <param name="set"></param> public void RegVar(string name, CSharpFunctionDelegate get, CSharpFunctionDelegate set) { if (get != null) { API.PushString(".get"); //table .get API.RawGet(-2); //table gettable if (!API.IsTable(-1)) { API.Pop(1); //table API.NewTable(); //table table API.PushString(".get"); //table table .get API.PushValue(-2); //table table .get table API.RawSet(-4); //table table } //设置get函数 API.PushString(name); API.PushCSharpFunction(get); API.RawSet(-3); API.Pop(1); } if (set != null) { API.PushString(".set"); //table .get API.RawGet(-2); //table gettable if (!API.IsTable(-1)) { API.Pop(1); //table API.NewTable(); //table table API.PushString(".set"); //table table .get API.PushValue(-2); //table table .get table API.RawSet(-4); //table table } //设置set函数 API.PushString(name); API.PushCSharpFunction(set); API.RawSet(-3); API.Pop(1); } }
public ObjectTranslator(Lua interpreter, ILuaState luaState) { this.interpreter = interpreter; typeChecker = new CheckType(this); metaFunctions = new MetaFunctions(this); assemblies = new List <Assembly>(); assemblies.Add(Assembly.GetExecutingAssembly()); importTypeFunction = new CSharpFunctionDelegate(this.importType); loadAssemblyFunction = new CSharpFunctionDelegate(this.loadAssembly); registerTableFunction = new CSharpFunctionDelegate(this.registerTable); unregisterTableFunction = new CSharpFunctionDelegate(this.unregisterTable); getMethodSigFunction = new CSharpFunctionDelegate(this.getMethodSignature); getConstructorSigFunction = new CSharpFunctionDelegate(this.getConstructorSignature); createLuaObjectList(luaState); createIndexingMetaFunction(luaState); createBaseClassMetatable(luaState); createClassMetatable(luaState); createFunctionMetatable(luaState); setGlobalFunctions(luaState); }
int ILuaAPI.YieldK(int numResults, int context, CSharpFunctionDelegate continueFunc) { CallInfo ci = CI; Utl.ApiCheckNumElems(this, numResults); if (NumNonYieldable > 0) { if (this != G.MainThread) { G_RunError("attempt to yield across metamethod/C-call boundary"); } else { G_RunError("attempt to yield from outside a coroutine"); } } Status = ThreadStatus.LUA_YIELD; ci.ExtraIndex = ci.FuncIndex; // save current `func' if (ci.IsLua) // inside a hook { Utl.ApiCheck(continueFunc == null, "hooks cannot continue after yielding"); } else { ci.ContinueFunc = continueFunc; if (ci.ContinueFunc != null) // is there a continuation { ci.Context = context; } ci.FuncIndex = Top.Index - (numResults + 1); D_Throw(ThreadStatus.LUA_YIELD); } Utl.Assert((ci.CallStatus & CallStatus.CIST_HOOKED) != 0); // must be inside a hook return(0); }
private static int PairsMeta(ILuaState lua, string method, bool isZero , CSharpFunctionDelegate iter) { if (!lua.L_GetMetaField(1, method)) // no metamethod? { lua.L_CheckType(1, LuaType.LUA_TTABLE); lua.PushCSharpFunction(iter); lua.PushValue(1); if (isZero) { lua.PushInteger(0); } else { lua.PushNil(); } } else { lua.PushValue(1); lua.Call(1, 3); } return(3); }
void ILuaAPI.PushCSharpClosure(CSharpFunctionDelegate f, int n) { if (n == 0) { Top.V.SetClCsValue(new LuaCsClosureValue(f)); } else { // 带 UpValue 的 C# function Utl.ApiCheckNumElems(this, n); Utl.ApiCheck(n <= LuaLimits.MAXUPVAL, "upvalue index too large"); LuaCsClosureValue cscl = new LuaCsClosureValue(f, n); int index = Top.Index - n; Top = Stack[index]; for (int i = 0; i < n; ++i) { cscl.Upvals[i].V.SetObj(ref Stack[index + i].V); } Top.V.SetClCsValue(cscl); } ApiIncrTop(); }
void ILuaAPI.PushCSharpClosure( CSharpFunctionDelegate f, int n ) { if( n == 0 ) { Top.V.SetClCsValue( new LuaCsClosureValue( f ) ); } else { // 带 UpValue 的 C# function Utl.ApiCheckNumElems( this, n ); Utl.ApiCheck( n <= LuaLimits.MAXUPVAL, "upvalue index too large" ); LuaCsClosureValue cscl = new LuaCsClosureValue( f, n ); int index = Top.Index - n; Top = Stack[index]; for(int i=0; i<n; ++i) { cscl.Upvals[i].V.SetObj( ref Stack[index+i].V ); } Top.V.SetClCsValue( cscl ); } ApiIncrTop(); }
public void RegFunction(string funcName, CSharpFunctionDelegate func) { API.PushString(funcName); API.PushCSharpFunction(func); API.RawSet(-3); }
public void SetMetatable(int index, CSharpFunctionDelegate pfnReader, CSharpFunctionDelegate pfnWriter) { if (_lstSetTableFunctions == null) { throw new Exception("Before You Call LuaContex.SetMetatable You Must Call BegTableFunction"); } if (index >= 0) { this.WriteError("LuaContex.SetTableFunction Can Only Have Negative Index."); return; } if (_lua.Type(index) != LuaType.LUA_TTABLE) { this.WriteInfo(String.Format("index:{0} is not a table, can't add meta function.", index)); return; } _lua.NewTable(); if (pfnReader != null) { _lua.PushString("__index"); // GCHandle hPfn = _lua.PushCSharpFunction(pfnReader); // _lua.SetTable(index -2); // _lstSetTableFunctions.Add(hPfn); } if (pfnWriter != null) { _lua.PushString("__newindex"); // GCHandle hPfn = _lua.PushCSharpFunction(pfnWriter); // _lua.SetTable(index - 2); // _lstSetTableFunctions.Add(hPfn); } _lua.SetMetaTable(index - 1); }
public LuaCsClosureValue( CSharpFunctionDelegate f, int numUpvalues ) { F = f; Upvals = new StkId[numUpvalues]; for(int i=0; i<numUpvalues; ++i) { var newItem = new StkId(); Upvals[i] = newItem; newItem.SetList(Upvals); newItem.SetIndex(i); } }
internal void SetClLcsValue(CSharpFunctionDelegate v) { #if DEBUG_DUMMY_TVALUE_MODIFY CheckLock(); #endif Tt = (int)LuaType.LUA_TFUNCTION; NValue = 0.0; UInt64Value = CLOSURE_LCS; OValue = v; }
/* * Pushes a delegate into the stack */ internal void pushFunction(ILuaState luaState, CSharpFunctionDelegate func) { pushObject(luaState, func, "luaNet_function"); }
public NameFuncPair(string name, CSharpFunctionDelegate func) { Name = name; Func = func; }
public void SetTableFunction(int index, string funName, CSharpFunctionDelegate pfn) { if (_lstSetTableFunctions == null) { throw new Exception("Before You Call LuaContex.SetTableFunction You Must Call BegTableFunction"); } if (index >= 0) { this.WriteError("LuaContex.SetTableFunction Can Only Have Negative Index."); return; } if (_lua.Type(index) != LuaType.LUA_TTABLE) { this.WriteError(String.Format("index:{0} is not a table, can't add function.", index)); return; } _lua.PushString(funName); // GCHandle hPfn = _lua.PushCSharpFunction(pfn); // _lua.SetTable(index - 2); // _lstSetTableFunctions.Add(hPfn); }
public void OpenLibrary(string moduleName, CSharpFunctionDelegate openFunc, bool global) { m_luaState.L_RequireF(moduleName, openFunc, global); }
void ILuaAPI.PushCSharpClosure( CSharpFunctionDelegate f, int n ) { if( n == 0 ) { Top.Value = new LuaCSharpClosure( f ); } else { // 带 UpValue 的 C# function Utl.ApiCheckNumElems( this, n ); Utl.ApiCheck( n <= LuaLimits.MAXUPVAL, "upvalue index too large" ); LuaCSharpClosure cscl = new LuaCSharpClosure( f, n ); var src = Top - n; while( src.Index < Top.Index ) cscl.Upvals.Add( src.ValueInc ); Top.Index -= n; Top.Value = cscl; } ApiIncrTop(); }
public void PushCSharpFunction(CSharpFunctionDelegate f) { API.PushCSharpClosure(f, 0); }
internal void pushCSFunction(CSharpFunctionDelegate function) { translator.pushFunction(luaState, function); }
void ILuaAPI.PushCSharpFunction( CSharpFunctionDelegate f ) { API.PushCSharpClosure( f, 0 ); }
public static void lua_atpanic(ILuaState luaState, CSharpFunctionDelegate cb) { //Debug.LogWarning("not implement"); }
void ILuaAPI.PushCSharpFunction(CSharpFunctionDelegate f) { API.PushCSharpClosure(f, 0); }
public LuaCsClosureValue(CSharpFunctionDelegate f) { F = f; }
/* * Pushes the value of a member or a delegate to call it, depending on the type of * the member. Works with static or instance members. * Uses reflection to find members, and stores the reflected MemberInfo object in * a cache (indexed by the type of the object and the name of the member). */ private int getMember(ILuaState luaState, IReflect objType, object obj, string methodName, BindingFlags bindingType) { bool implicitStatic = false; MemberInfo member = null; object cachedMember = checkMemberCache(memberCache, objType, methodName); //object cachedMember=null; if (cachedMember is CSharpFunctionDelegate) { translator.pushFunction(luaState, (CSharpFunctionDelegate)cachedMember); translator.push(luaState, true); return(2); } else if (cachedMember != null) { member = (MemberInfo)cachedMember; } else { //CP: Removed NonPublic binding search MemberInfo[] members = objType.GetMember(methodName, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase /*| BindingFlags.NonPublic*/); if (members.Length > 0) { member = members[0]; } else { // If we can't find any suitable instance members, try to find them as statics - but we only want to allow implicit static // lookups for fields/properties/events -kevinh //CP: Removed NonPublic binding search and made case insensitive members = objType.GetMember(methodName, bindingType | BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase /*| BindingFlags.NonPublic*/); if (members.Length > 0) { member = members[0]; implicitStatic = true; } } } if (member != null) { if (member.MemberType == MemberTypes.Field) { FieldInfo field = (FieldInfo)member; if (cachedMember == null) { setMemberCache(memberCache, objType, methodName, member); } try { translator.push(luaState, field.GetValue(obj)); } catch { LuaDLL.lua_pushnil(luaState); } } else if (member.MemberType == MemberTypes.Property) { PropertyInfo property = (PropertyInfo)member; if (cachedMember == null) { setMemberCache(memberCache, objType, methodName, member); } try { object val = property.GetValue(obj, null); translator.push(luaState, val); } catch (ArgumentException) { // If we can't find the getter in our class, recurse up to the base class and see // if they can help. if (objType is Type && !(((Type)objType) == typeof(object))) { return(getMember(luaState, ((Type)objType).BaseType, obj, methodName, bindingType)); } else { LuaDLL.lua_pushnil(luaState); } } catch (TargetInvocationException e) // Convert this exception into a Lua error { ThrowError(luaState, e); LuaDLL.lua_pushnil(luaState); } } else if (member.MemberType == MemberTypes.Event) { EventInfo eventInfo = (EventInfo)member; if (cachedMember == null) { setMemberCache(memberCache, objType, methodName, member); } translator.push(luaState, new RegisterEventHandler(translator.pendingEvents, obj, eventInfo)); } else if (!implicitStatic) { if (member.MemberType == MemberTypes.NestedType) { // kevinh - added support for finding nested types // cache us if (cachedMember == null) { setMemberCache(memberCache, objType, methodName, member); } // Find the name of our class string name = member.Name; Type dectype = member.DeclaringType; // Build a new long name and try to find the type by name string longname = dectype.FullName + "+" + name; Type nestedType = translator.FindType(longname); translator.pushType(luaState, nestedType); } else { // Member type must be 'method' CSharpFunctionDelegate wrapper = new CSharpFunctionDelegate((new LuaMethodWrapper(translator, objType, methodName, bindingType)).call); if (cachedMember == null) { setMemberCache(memberCache, objType, methodName, wrapper); } translator.pushFunction(luaState, wrapper); translator.push(luaState, true); return(2); } } else { // If we reach this point we found a static method, but can't use it in this context because the user passed in an instance translator.throwError(luaState, "can't pass instance to static method " + methodName); LuaDLL.lua_pushnil(luaState); } } else { // kevinh - we want to throw an exception because meerly returning 'nil' in this case // is not sufficient. valid data members may return nil and therefore there must be some // way to know the member just doesn't exist. translator.throwError(luaState, "unknown member name " + methodName); LuaDLL.lua_pushnil(luaState); } // push false because we are NOT returning a function (see luaIndexFunction) translator.push(luaState, false); return(2); }
public static void lua_pushstdcallcfunction(ILuaState luaState, CSharpFunctionDelegate cb) { luaState.PushCSharpFunction(cb); }
public LuaCSharpClosure( CSharpFunctionDelegate f, int numUpvalues ) { F = f; Upvals = new List<LuaObject>(numUpvalues); }
int ILuaAPI.YieldK( int numResults, int context, CSharpFunctionDelegate continueFunc ) { CallInfo ci = CI; Utl.ApiCheckNumElems( this, numResults ); if( NumNonYieldable > 0 ) { if( this != G.MainThread ) G_RunError( "attempt to yield across metamethod/C-call boundary" ); else G_RunError( "attempt to yield from outside a coroutine" ); } Status = ThreadStatus.LUA_YIELD; ci.ExtraIndex = ci.FuncIndex; // save current `func' if( ci.IsLua ) // inside a hook { Utl.ApiCheck( continueFunc == null, "hooks cannot continue after yielding" ); } else { ci.ContinueFunc = continueFunc; if( ci.ContinueFunc != null ) // is there a continuation { ci.Context = context; } ci.FuncIndex = Top.Index - (numResults + 1); D_Throw( ThreadStatus.LUA_YIELD ); } Utl.Assert( (ci.CallStatus & CallStatus.CIST_HOOKED) != 0 ); // must be inside a hook return 0; }
public LuaCsClosureValue( CSharpFunctionDelegate f ) { F = f; }
void ILuaAPI.CallK( int numArgs, int numResults, int context, CSharpFunctionDelegate continueFunc ) { Utl.ApiCheck( continueFunc == null || !CI.IsLua, "cannot use continuations inside hooks" ); Utl.ApiCheckNumElems( this, numArgs + 1 ); Utl.ApiCheck( Status == ThreadStatus.LUA_OK, "cannot do calls on non-normal thread" ); CheckResults( numArgs, numResults ); var func = Stack[Top.Index - (numArgs+1)]; // need to prepare continuation? if( continueFunc != null && NumNonYieldable == 0 ) { CI.ContinueFunc = continueFunc; CI.Context = context; D_Call( func, numResults, true ); } // no continuation or no yieldable else { D_Call( func, numResults, false ); } AdjustResults( numResults ); }
public LuaCSharpClosure( CSharpFunctionDelegate f ) { F = f; }
ThreadStatus ILuaAPI.PCallK( int numArgs, int numResults, int errFunc, int context, CSharpFunctionDelegate continueFunc ) { Utl.ApiCheck( continueFunc == null || !CI.IsLua, "cannot use continuations inside hooks" ); Utl.ApiCheckNumElems( this, numArgs + 1 ); Utl.ApiCheck( Status == ThreadStatus.LUA_OK, "cannot do calls on non-normal thread" ); CheckResults( numArgs, numResults ); int func; if( errFunc == 0 ) func = 0; else { StkId addr; if( !Index2Addr( errFunc, out addr ) ) Utl.InvalidIndex(); func = addr.Index; } ThreadStatus status; CallS c = new CallS(); c.L = this; c.FuncIndex = Top.Index - (numArgs + 1); if( continueFunc == null || NumNonYieldable > 0 ) // no continuation or no yieldable? { c.NumResults = numResults; status = D_PCall( DG_F_Call, ref c, c.FuncIndex, func ); } else { int ciIndex = CI.Index; CI.ContinueFunc = continueFunc; CI.Context = context; CI.ExtraIndex = c.FuncIndex; CI.OldAllowHook = AllowHook; CI.OldErrFunc = ErrFunc; ErrFunc = func; CI.CallStatus |= CallStatus.CIST_YPCALL; D_Call( Stack[c.FuncIndex], numResults, true ); CallInfo ci = BaseCI[ciIndex]; ci.CallStatus &= ~CallStatus.CIST_YPCALL; ErrFunc = ci.OldErrFunc; status = ThreadStatus.LUA_OK; } AdjustResults( numResults ); return status; }
private static int Lua_DispatchFun(IntPtr L) { CSharpFunctionDelegate target = (CSharpFunctionDelegate)GCHandle.FromIntPtr(LuaAPI.lua_touserdata(L, -1001001)).Target; return(target(LuaState.globalLuaState)); }
private static void CreateSearchersTable( ILuaState lua ) { CSharpFunctionDelegate[] searchers = new CSharpFunctionDelegate[] { SearcherPreload, SearcherLua, }; lua.CreateTable( searchers.Length, 0 ); for( int i=0; i<searchers.Length; ++i ) { lua.PushValue( -2 ); // set `package' as upvalue for all searchers lua.PushCSharpClosure( searchers[i], 1 ); lua.RawSetI( -2, i+1 ); } }
private static int PairsMeta( ILuaState lua, string method, bool isZero , CSharpFunctionDelegate iter ) { if( !lua.L_GetMetaField( 1, method ) ) // no metamethod? { lua.L_CheckType( 1, LuaType.LUA_TTABLE ); lua.PushCSharpFunction( iter ); lua.PushValue( 1 ); if( isZero ) lua.PushInteger( 0 ); else lua.PushNil(); } else { lua.PushValue( 1 ); lua.Call( 1, 3 ); } return 3; }
private void AddGlobalFunction(string funName, CSharpFunctionDelegate pfn,int nParm=0) { if (nParm == 0) { //m_pfnHandles.Add(_lua.PushCSharpFunction(pfn)); _lua.SetGlobal(funName); } else { //m_pfnHandles.Add(_lua.PushCSharpClosure(pfn,nParm)); _lua.SetGlobal(funName); } }