From bd3fe0cac583739bc0d7c4b5c8f301bb350abca0 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Fri, 30 Aug 2019 00:19:31 -0400 Subject: Renamed lib to deps so github will ignore it for language stats --- .../examples/source/require_override_behavior.cpp | 76 ---------------------- 1 file changed, 76 deletions(-) delete mode 100644 lib/sol2/examples/source/require_override_behavior.cpp (limited to 'lib/sol2/examples/source/require_override_behavior.cpp') diff --git a/lib/sol2/examples/source/require_override_behavior.cpp b/lib/sol2/examples/source/require_override_behavior.cpp deleted file mode 100644 index 2af8dd0..0000000 --- a/lib/sol2/examples/source/require_override_behavior.cpp +++ /dev/null @@ -1,76 +0,0 @@ -// Thanks to OrfeasZ for their answer to -// an issue for this example! -#define SOL_ALL_SAFETIES_ON 1 -#include - -#include "assert.hpp" - -#include -#include - -// 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) { - // use sol3 stack API to pull - // "first argument" - std::string path = sol::stack::get(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 - luaL_loadbuffer(L, script.data(), script.size(), path.c_str()); - // 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; - } - - sol::stack::push(L, "This is not the module you're looking for!"); - return 1; -} - -int main() { - std::cout << "=== require override behavior ===" << std::endl; - - sol::state lua; - // need base for print, - // need package for package/searchers/require - lua.open_libraries(sol::lib::base, sol::lib::package); - - lua.clear_package_loaders( ); - lua.add_package_loader(LoadFileRequire); - - // this will call our function for - // the searcher and it will succeed - auto a_result = lua.safe_script(R"( - local a = require("a") - print(a) - print(test) - )", sol::script_pass_on_error); - c_assert(a_result.valid()); - try { - // this will always fail - auto b_result = lua.safe_script(R"( - local b = require("b") - print(b) - )", sol::script_throw_on_error); - // this will not be executed because of the throw, - // but it better be true regardless! - c_assert(!b_result.valid()); - } - catch (const std::exception& ex) { - // Whenever sol3 throws an exception from panic, - // catch - std::cout << "Something went wrong, as expected:\n" << ex.what() << std::endl; - // and CRASH / exit the application - return 0; - } - - // If we get here something went wrong...! - return -1; -} -- cgit v1.2.3