int luaopen_upbtable(lua_State *L) { lupb_newlib(L, "upb.table", lupbtable_toplevel_m); // We define these here because they are not public (at least at the moment). lupbtable_setfieldi(L, "CTYPE_PTR", UPB_CTYPE_PTR); lupbtable_setfieldi(L, "CTYPE_INT32", UPB_CTYPE_INT32); lua_pushlightuserdata(L, NULL); lua_setfield(L, -2, "NULL"); return 1; // Return a single Lua value, the package table created above. }
/* We store our module table in the registry, keyed by ptr. * For more info about the motivation/rationale, see this thread: * http://thread.gmane.org/gmane.comp.lang.lua.general/110632 */ bool lupb_openlib(lua_State *L, void *ptr, const char *name, const luaL_Reg *funcs) { /* Lookup cached module table. */ lua_pushlightuserdata(L, ptr); lua_rawget(L, LUA_REGISTRYINDEX); if (!lua_isnil(L, -1)) { return true; } lupb_newlib(L, name, funcs); /* Save module table in cache. */ lua_pushlightuserdata(L, ptr); lua_pushvalue(L, -2); lua_rawset(L, LUA_REGISTRYINDEX); return false; }