diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2025-09-23 15:06:08 +0200 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2025-09-23 15:06:08 +0200 |
| commit | 4f5d5ea60e5b5bd42a3bdab5460c2c0fda365a4d (patch) | |
| tree | 01a2a73ada5a282d218cdc2e9279ea46adafe6fa /unit_tests | |
| parent | 87f910a1affe649d43904c217ebd51aa90303cae (diff) | |
| download | lanes-4f5d5ea60e5b5bd42a3bdab5460c2c0fda365a4d.tar.gz lanes-4f5d5ea60e5b5bd42a3bdab5460c2c0fda365a4d.tar.bz2 lanes-4f5d5ea60e5b5bd42a3bdab5460c2c0fda365a4d.zip | |
Fix embedded tests when PATH is insufficient
* read package.path and use the list of paths to try to load lanes_core.dll
Diffstat (limited to 'unit_tests')
| -rw-r--r-- | unit_tests/embedded_tests.cpp | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/unit_tests/embedded_tests.cpp b/unit_tests/embedded_tests.cpp index 388548d..899530c 100644 --- a/unit_tests/embedded_tests.cpp +++ b/unit_tests/embedded_tests.cpp | |||
| @@ -92,19 +92,6 @@ namespace | |||
| 92 | EmbeddedLuaState() | 92 | EmbeddedLuaState() |
| 93 | : LuaState { LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } } | 93 | : LuaState { LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } } |
| 94 | { | 94 | { |
| 95 | |||
| 96 | if (!hCore) { | ||
| 97 | throw std::logic_error("Could not load lanes_core"); | ||
| 98 | } | ||
| 99 | luaopen_lanes_embedded_t const _p_luaopen_lanes_embedded{ reinterpret_cast<luaopen_lanes_embedded_t>(GetProcAddress(hCore, "luaopen_lanes_embedded")) }; | ||
| 100 | if (!_p_luaopen_lanes_embedded) { | ||
| 101 | throw std::logic_error("Could not bind luaopen_lanes_embedded"); | ||
| 102 | } | ||
| 103 | |||
| 104 | lanes_register = reinterpret_cast<lua_CFunction>(GetProcAddress(hCore, "lanes_register")); | ||
| 105 | if (!lanes_register) { | ||
| 106 | throw std::logic_error("Could not bind lanes_register"); | ||
| 107 | } | ||
| 108 | // need base to make lanes happy | 95 | // need base to make lanes happy |
| 109 | luaL_requiref(L, LUA_GNAME, luaopen_base, 1); | 96 | luaL_requiref(L, LUA_GNAME, luaopen_base, 1); |
| 110 | lua_pop(L, 1); | 97 | lua_pop(L, 1); |
| @@ -125,7 +112,41 @@ namespace | |||
| 125 | lua_pop(L, 1); | 112 | lua_pop(L, 1); |
| 126 | stackCheck(0); | 113 | stackCheck(0); |
| 127 | 114 | ||
| 128 | _p_luaopen_lanes_embedded(L, local::load_lanes_lua); // S: lanes | 115 | if (!hCore) { |
| 116 | // lanes_core not found in PATH. use package.path and try in the listed locations, just like Lua would do | ||
| 117 | lua_getglobal(L, "package"); // S: package | ||
| 118 | std::ignore = luaW_getfield(L, kIdxTop, "cpath"); // S: package package.path | ||
| 119 | std::string_view _package_path{ luaW_tostring(L, kIdxTop) }; | ||
| 120 | lua_pop(L, 2); | ||
| 121 | while (!_package_path.empty() && !hCore) { | ||
| 122 | // extract an element | ||
| 123 | auto _sep{ _package_path.find_first_of(";") }; | ||
| 124 | std::string _path{ _sep == std::string::npos ? _package_path : _package_path.substr(0, _sep) }; | ||
| 125 | (_sep == std::string::npos) ? (void)(_package_path = "") : (_package_path.remove_prefix(_sep + 1)); | ||
| 126 | // replace question mark by "lanes_core" | ||
| 127 | auto _found{ _path.find_first_of("?") }; | ||
| 128 | if (_found != std::string::npos) { | ||
| 129 | _path.replace(_found, 1, "lanes_core"); | ||
| 130 | hCore = LoadLibraryA(_path.c_str()); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | stackCheck(0); | ||
| 135 | |||
| 136 | if (!hCore) { | ||
| 137 | throw std::logic_error("Could not load lanes_core, check PATH"); | ||
| 138 | } | ||
| 139 | luaopen_lanes_embedded_t const _p_luaopen_lanes_embedded{ reinterpret_cast<luaopen_lanes_embedded_t>(GetProcAddress(hCore, "luaopen_lanes_embedded")) }; | ||
| 140 | if (!_p_luaopen_lanes_embedded) { | ||
| 141 | throw std::logic_error("Could not bind luaopen_lanes_embedded"); | ||
| 142 | } | ||
| 143 | |||
| 144 | lanes_register = reinterpret_cast<lua_CFunction>(GetProcAddress(hCore, "lanes_register")); | ||
| 145 | if (!lanes_register) { | ||
| 146 | throw std::logic_error("Could not bind lanes_register"); | ||
| 147 | } | ||
| 148 | |||
| 149 | _p_luaopen_lanes_embedded(L, local::load_lanes_lua); // S: lanes | ||
| 129 | lua_pop(L, 1); | 150 | lua_pop(L, 1); |
| 130 | stackCheck(0); | 151 | stackCheck(0); |
| 131 | } | 152 | } |
