MCPcopy Create free account
hub / github.com/ThePhD/sol2 / LoadFileRequire

Function LoadFileRequire

examples/source/require_override_behavior.cpp:13–36  ·  view source on GitHub ↗

Use raw function of form "int(lua_State*)" -- this is called a "raw C function", and matches the type for lua_CFunction

Source from the content-addressed store, hash-verified

11// -- this is called a "raw C function",
12// and matches the type for lua_CFunction
13int 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
38int main() {
39 std::cout << "=== require override behavior ==="

Callers

nothing calls this directly

Calls 3

pushFunction · 0.85
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected