public static void luaL_module(IntPtr L, string libname, Lua.luaL_Reg[] regs, int nup) { if (!string.IsNullOrEmpty(libname)) { getfield(L, libname); /* check whether lib already exists */ if (Lua.lua_isnil(L, -1)) { Lua.lua_pop(L, 1); /* get rid of nil */ Lua.lua_newtable(L); /* create namespace for lib */ getfield(L, "package.loaded"); /* get package.loaded table or create it */ if (Lua.lua_isnil(L, -1)) { Lua.lua_pop(L, 1); Lua.lua_newtable(L); Lua.lua_pushvalue(L, -1); setfield(L, "package.loaded"); } else if (!Lua.lua_istable(L, -1)) Lua.lua_error(L); // @todo fixme , "name conflict for library `%s'", libname); Lua.lua_pushstring(L, libname); Lua.lua_pushvalue(L, -3); Lua.lua_settable(L, -3); /* store namespace in package.loaded table */ Lua.lua_pop(L, 1); /* get rid of package.loaded table */ Lua.lua_pushvalue(L, -1); setfield(L, libname); /* store namespace it in globals table */ } Lua.lua_insert(L, -(nup + 1)); } for (int i = 0; i < regs.Length; i++) { var r = regs[i]; Lua.lua_pushstring(L, r.name); for (int ix = 0; ix < nup; ix++) /* copy upvalues to the top */ Lua.lua_pushvalue(L, -(nup + 1)); Lua.lua_pushcclosure(L, r.func, nup); Lua.lua_settable(L, -(nup + 3)); } Lua.lua_pop(L, nup); /* remove upvalues */ }
public static void RegisterGlobalFunction(IntPtr L, string name, Lua.lua_CFunction f) { new LuaFunction(f).Push(L); Lua.lua_setglobal(L, name); }
public LuaFunction(Lua.lua_CFunction value) { Value = value; }
public void Glue(string objectType, string tableName, Lua.luaL_Reg[] methods, Lua.lua_CFunction createFunction) { Lua.luaL_newmetatable(this, "GLua." + objectType); Lua.lua_pushstring(this, "__index"); Lua.lua_pushvalue(this, -2); /* pushes the metatable */ Lua.lua_settable(this, -3); /* metatable.__index = metatable */ var functions = new List<Lua.luaL_Reg>(); var ent = new Lua.luaL_Reg(); ent.name = "new"; ent.func = createFunction; functions.Add(ent); var gc = new Lua.luaL_Reg(); gc.name = "__gc"; gc.func = GcFunction; var mt = methods.ToList(); mt.Add(gc); /* hack in our __gc automagic */ methods = mt.ToArray(); // Register the methods for referenced objects luaL_module(this, null, methods, 0); // Register the 'static' functions luaL_module(this, tableName, functions.ToArray(), 0); return; }