2018-08-10 23:17:31 +08:00
|
|
|
// Thanks to OrfeasZ for their answer to
|
|
|
|
// an issue for this example!
|
2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2018-08-10 23:17:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <exception>
|
|
|
|
|
|
|
|
// Use raw function of form "int(lua_State*)"
|
|
|
|
// -- this is called a "raw C function",
|
|
|
|
// and matches the type for lua_CFunction
|
|
|
|
int LoadFileRequire(lua_State* L) {
|
2021-03-06 23:14:48 +08:00
|
|
|
// use sol2 stack API to pull
|
2018-08-10 23:17:31 +08:00
|
|
|
// "first argument"
|
|
|
|
std::string path = sol::stack::get<std::string>(L, 1);
|
|
|
|
|
|
|
|
if (path == "a") {
|
|
|
|
std::string script = R"(
|
|
|
|
print("Hello from module land!")
|
|
|
|
test = 123
|
|
|
|
return "bananas"
|
|
|
|
)";
|
|
|
|
// load "module", but don't run it
|
2021-03-06 23:14:48 +08:00
|
|
|
luaL_loadbuffer(
|
|
|
|
L, script.data(), script.size(), path.c_str());
|
2018-08-10 23:17:31 +08:00
|
|
|
// returning 1 object left on Lua stack:
|
|
|
|
// a function that, when called, executes the script
|
|
|
|
// (this is what lua_loadX/luaL_loadX functions return
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
sol::stack::push(
|
|
|
|
L, "This is not the module you're looking for!");
|
2018-08-10 23:17:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "=== require override behavior ==="
|
|
|
|
<< std::endl;
|
2019-05-22 07:17:31 +08:00
|
|
|
|
2018-08-10 23:17:31 +08:00
|
|
|
sol::state lua;
|
|
|
|
// need base for print,
|
|
|
|
// need package for package/searchers/require
|
|
|
|
lua.open_libraries(sol::lib::base, sol::lib::package);
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
lua.clear_package_loaders();
|
2018-08-10 23:17:31 +08:00
|
|
|
lua.add_package_loader(LoadFileRequire);
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
// this will call our function for
|
2018-08-10 23:17:31 +08:00
|
|
|
// the searcher and it will succeed
|
|
|
|
auto a_result = lua.safe_script(R"(
|
|
|
|
local a = require("a")
|
|
|
|
print(a)
|
|
|
|
print(test)
|
2021-03-06 14:03:23 +08:00
|
|
|
)",
|
|
|
|
sol::script_pass_on_error);
|
|
|
|
sol_c_assert(a_result.valid());
|
2018-08-10 23:17:31 +08:00
|
|
|
try {
|
|
|
|
// this will always fail
|
|
|
|
auto b_result = lua.safe_script(R"(
|
|
|
|
local b = require("b")
|
|
|
|
print(b)
|
2021-03-06 14:03:23 +08:00
|
|
|
)",
|
|
|
|
sol::script_throw_on_error);
|
2018-08-10 23:17:31 +08:00
|
|
|
// this will not be executed because of the throw,
|
|
|
|
// but it better be true regardless!
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(!b_result.valid());
|
2018-08-10 23:17:31 +08:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex) {
|
2021-03-06 23:14:48 +08:00
|
|
|
// Whenever sol2 throws an exception from panic,
|
2018-08-10 23:17:31 +08:00
|
|
|
// catch
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "Something went wrong, as expected:\n"
|
|
|
|
<< ex.what() << std::endl;
|
2018-08-10 23:17:31 +08:00
|
|
|
// and CRASH / exit the application
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here something went wrong...!
|
|
|
|
return -1;
|
|
|
|
}
|