private int pushFunctionLut(System.Reflection.MethodInfo mi, string bindName) { int top = LuaDll.lua_gettop(L); // Push global lut to the stack string globalLutName = "__Lua.cs__LUT__"; LuaDll.lua_getfield(L, LuaDll.LUA_REGISTRYINDEX, globalLutName); if (LuaDll.lua_isnil(L, -1)) { // If the global lut does not exist then create it and push it onto the stack. // Pop the nil value LuaDll.lua_pop(L, 1); // Push a new table and set it as the global globalLutName LuaDll.lua_newtable(L); LuaDll.lua_pushvalue(L, -1); // Dupe for setglobal call. LuaDll.lua_setfield(L, LuaDll.LUA_REGISTRYINDEX, globalLutName); } int lut = LuaDll.lua_gettop(L); Debug.Assert(lut == (top + 1)); // Push function lut to the stack. string fullName = mi.ReflectedType.FullName + "." + bindName; LuaDll.lua_getfield(L, lut, fullName); if (LuaDll.lua_isnil(L, -1)) { // If the function lut does not exist then create it. // Pop off the nil result. LuaDll.lua_pop(L, 1); // Push in a new table LuaDll.lua_newtable(L); // Add the name to the table as kFunctionNameKey LuaDll.lua_pushstring(L, bindName); LuaDll.lua_setfield(L, -2, kFunctionNameKey); // Add this to the global lut, keyed on FullName LuaDll.lua_pushvalue(L, -1); LuaDll.lua_setfield(L, lut, fullName); } // Remove the global lut LuaDll.lua_remove(L, lut); // Now we have the function lut at the top of the stack. int result = LuaDll.lua_gettop(L); Debug.Assert(result == top + 1); // Return the stack position of the lut. return(result); }