Use raw function of form "int(lua_State*)" -- this is called a "raw C function", and matches the type for lua_CFunction
| 11 | // -- this is called a "raw C function", |
| 12 | // and matches the type for lua_CFunction |
| 13 | int LoadFileRequire(lua_State* L) { |
| 14 | // use sol2 stack API to pull |
| 15 | // "first argument" |
| 16 | std::string path = sol::stack::get<std::string>(L, 1); |
| 17 | |
| 18 | if (path == "a") { |
| 19 | std::string script = R"( |
| 20 | print("Hello from module land!") |
| 21 | test = 123 |
| 22 | return "bananas" |
| 23 | )"; |
| 24 | // load "module", but don't run it |
| 25 | luaL_loadbuffer( |
| 26 | L, script.data(), script.size(), path.c_str()); |
| 27 | // returning 1 object left on Lua stack: |
| 28 | // a function that, when called, executes the script |
| 29 | // (this is what lua_loadX/luaL_loadX functions return |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | sol::stack::push( |
| 34 | L, "This is not the module you're looking for!"); |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | int main() { |
| 39 | std::cout << "=== require override behavior ===" |