Setting: I\'m using Lua from a C/C++ environment.
I have several lua files on disk. Those are read into memory and some more memory-only lua files become available duri
A pretty straightforward C++ function that would mimic require
could be: (pseudocode)
int my_require(lua_State* state) {
// get the module name
const char* name = lua_tostring(state);
// find if you have such module loaded
if (mymodules.find(name) != mymodules.end())
luaL_loadbuffer(state, buffer, size, name);
// the chunk is now at the top of the stack
lua_call(state)
return 1;
}
Expose this function to Lua as require
and you're good to go.
I'd also like to add that to completely mimic require
's behaviour, you'd probably need to take care of package.loaded
, to avoid the code to be loaded twice.