aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-05-02 18:50:40 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-05-02 18:50:40 +0200
commita2824ef0c87034d535d3b12e6d582dfcf2265f27 (patch)
tree4cd1def6d11df8d5c715b51f338b090bb299b7cd /src
parenteb7c7611524ba2dd2324aa3c72142e6973912286 (diff)
downloadlanes-a2824ef0c87034d535d3b12e6d582dfcf2265f27.tar.gz
lanes-a2824ef0c87034d535d3b12e6d582dfcf2265f27.tar.bz2
lanes-a2824ef0c87034d535d3b12e6d582dfcf2265f27.zip
Remove dependency on _G["package"]
Diffstat (limited to 'src')
-rw-r--r--src/compat.cpp19
-rw-r--r--src/compat.h21
-rw-r--r--src/keeper.cpp7
-rw-r--r--src/tools.cpp5
4 files changed, 44 insertions, 8 deletions
diff --git a/src/compat.cpp b/src/compat.cpp
index 591e19f..6ceed8f 100644
--- a/src/compat.cpp
+++ b/src/compat.cpp
@@ -6,6 +6,23 @@
6#include "macros_and_utils.h" 6#include "macros_and_utils.h"
7 7
8// ################################################################################################# 8// #################################################################################################
9
10// a small helper to obtain the "package" module table from the registry instead of relying on the presence of _G.package
11int luaG_getpackage(lua_State* L_)
12{
13 STACK_CHECK_START_REL(L_, 0);
14 int type{ lua503_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE) }; // L_: _R._LOADED|nil
15 if (type != LUA_TTABLE) { // L_: _R._LOADED|nil
16 STACK_CHECK(L_, 1);
17 return type;
18 }
19 type = lua503_getfield(L_, -1, LUA_LOADLIBNAME); // L_: _R._LOADED package|nil
20 lua_remove(L_, -2); // L_: package|nil
21 STACK_CHECK(L_, 1);
22 return type;
23}
24
25// #################################################################################################
9// ################################################################################################# 26// #################################################################################################
10#if LUA_VERSION_NUM == 501 27#if LUA_VERSION_NUM == 501
11// ################################################################################################# 28// #################################################################################################
@@ -71,7 +88,7 @@ int lua_getiuservalue(lua_State* L_, int idx_, int n_)
71 88
72#if LUA_VERSION_NUM == 501 89#if LUA_VERSION_NUM == 501
73 /* default environment is not a nil (see lua_getfenv) */ 90 /* default environment is not a nil (see lua_getfenv) */
74 lua_getglobal(L_, "package"); 91 lua_getglobal(L_, LUA_LOADLIBNAME);
75 if (lua_rawequal(L_, -2, -1) || lua_rawequal(L_, -2, LUA_GLOBALSINDEX)) { 92 if (lua_rawequal(L_, -2, -1) || lua_rawequal(L_, -2, LUA_GLOBALSINDEX)) {
76 lua_pop(L_, 2); 93 lua_pop(L_, 2);
77 lua_pushnil(L_); 94 lua_pushnil(L_);
diff --git a/src/compat.h b/src/compat.h
index 4816cb7..f98e142 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -100,6 +100,25 @@ inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_u
100 100
101// ################################################################################################# 101// #################################################################################################
102 102
103#if LUA_VERSION_NUM < 503
104// starting with Lua 5.3, lua_getfield returns the type of the value it found
105inline int lua503_getfield(lua_State* L_, int idx_, char const* k_)
106{
107 lua_getfield(L_, idx_, k_);
108 return lua_type(L_, -1);
109}
110
111#else // LUA_VERSION_NUM >= 503
112
113inline int lua503_getfield(lua_State* L_, int idx_, char const* k_)
114{
115 return lua_getfield(L_, idx_, k_);
116}
117
118#endif // LUA_VERSION_NUM >= 503
119
120// #################################################################################################
121
103// wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way 122// wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way
104#if LUA_VERSION_NUM == 503 123#if LUA_VERSION_NUM == 503
105 124
@@ -199,3 +218,5 @@ inline char const* lua_typename(lua_State* L_, LuaType t_)
199{ 218{
200 return lua_typename(L_, static_cast<int>(t_)); 219 return lua_typename(L_, static_cast<int>(t_));
201} 220}
221
222int luaG_getpackage(lua_State* L_);
diff --git a/src/keeper.cpp b/src/keeper.cpp
index 763bcf7..8e76247 100644
--- a/src/keeper.cpp
+++ b/src/keeper.cpp
@@ -651,15 +651,14 @@ void init_keepers(Universe* U_, lua_State* L_)
651 651
652 // make sure 'package' is initialized in keeper states, so that we have require() 652 // make sure 'package' is initialized in keeper states, so that we have require()
653 // this because this is needed when transferring deep userdata object 653 // this because this is needed when transferring deep userdata object
654 luaL_requiref(K, "package", luaopen_package, 1); // L_: settings K: package 654 luaL_requiref(K, LUA_LOADLIBNAME, luaopen_package, 1); // L_: settings K: package
655 lua_pop(K, 1); // L_: settings K: 655 lua_pop(K, 1); // L_: settings K:
656 STACK_CHECK(K, 0); 656 STACK_CHECK(K, 0);
657 serialize_require(DEBUGSPEW_PARAM_COMMA(U_) K); 657 serialize_require(DEBUGSPEW_PARAM_COMMA(U_) K);
658 STACK_CHECK(K, 0); 658 STACK_CHECK(K, 0);
659 659
660 // copy package.path and package.cpath from the source state (TODO: use _R._LOADED.package instead of _G.package) 660 // copy package.path and package.cpath from the source state
661 lua_getglobal(L_, "package"); // L_: settings package K: 661 if (luaG_getpackage(L_) != LUA_TNIL) { // L_: settings package K:
662 if (!lua_isnil(L_, -1)) {
663 // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately 662 // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately
664 InterCopyContext c{ U_, DestState{ K }, SourceState{ L_ }, {}, SourceIndex{ lua_absindex(L_, -1) }, {}, LookupMode::ToKeeper, {} }; 663 InterCopyContext c{ U_, DestState{ K }, SourceState{ L_ }, {}, SourceIndex{ lua_absindex(L_, -1) }, {}, LookupMode::ToKeeper, {} };
665 if (c.inter_copy_package() != InterCopyResult::Success) { // L_: settings ... error_msg K: 664 if (c.inter_copy_package() != InterCopyResult::Success) { // L_: settings ... error_msg K:
diff --git a/src/tools.cpp b/src/tools.cpp
index a55ee75..049a065 100644
--- a/src/tools.cpp
+++ b/src/tools.cpp
@@ -1826,12 +1826,11 @@ void InterCopyContext::inter_copy_keyvaluepair() const
1826 STACK_CHECK(L1, 1); 1826 STACK_CHECK(L1, 1);
1827 // raise the error when copying from lane to lane, else just leave it on the stack to be raised later 1827 // raise the error when copying from lane to lane, else just leave it on the stack to be raised later
1828 if (mode == LookupMode::LaneBody) { 1828 if (mode == LookupMode::LaneBody) {
1829 raise_lua_error(getErrL()); 1829 raise_lua_error(getErrL()); // that's ok, getErrL() is L1 in that case
1830 } 1830 }
1831 return InterCopyResult::Error; 1831 return InterCopyResult::Error;
1832 } 1832 }
1833 lua_getglobal(L2, "package"); // TODO: use _R._LOADED.package instead of _G.package 1833 if (luaG_getpackage(L2) == LUA_TNIL) { // package library not loaded: do nothing
1834 if (lua_isnil(L2, -1)) { // package library not loaded: do nothing
1835 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "'package' not loaded, nothing to do\n" INDENT_END(U))); 1834 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "'package' not loaded, nothing to do\n" INDENT_END(U)));
1836 STACK_CHECK(L1, 0); 1835 STACK_CHECK(L1, 0);
1837 return InterCopyResult::Success; 1836 return InterCopyResult::Success;