Re-implementing luaNew
Because we will store LuaModuleDef
in LuaModuleExporter
, to access it, we need to find the instance of LuaModuleExporter
. Let us first implement a helper function for this.
Since the exporter is also LuaModule
, it already has an upvalue mechanism, implemented in Chapter 6. LuaModule::pushLuaUpvalues
will push the pointer to the LuaModule
instance as an upvalue. To retrieve it, we can add the following function:
class LuaModuleExporter final : public LuaModule { private: static LuaModuleExporter<T> *getExporter( lua_State *L) { return reinterpret_cast<LuaModuleExporter<T> *>( lua_touserdata(L, lua_upvalueindex(1))); } };
This is the same as the getObj
function in Chapter 6, but now, it is a static...