diff options
Diffstat (limited to 'src/state.cpp')
-rw-r--r-- | src/state.cpp | 68 |
1 files changed, 1 insertions, 67 deletions
diff --git a/src/state.cpp b/src/state.cpp index d9f5499..8520346 100644 --- a/src/state.cpp +++ b/src/state.cpp | |||
@@ -43,72 +43,6 @@ THE SOFTWARE. | |||
43 | 43 | ||
44 | // ################################################################################################# | 44 | // ################################################################################################# |
45 | 45 | ||
46 | /*---=== Serialize require ===--- | ||
47 | */ | ||
48 | |||
49 | //--- | ||
50 | // [val,...]= new_require( ... ) | ||
51 | // | ||
52 | // Call 'old_require' but only one lane at a time. | ||
53 | // | ||
54 | // Upvalues: [1]: original 'require' function | ||
55 | // | ||
56 | [[nodiscard]] static int luaG_new_require(lua_State* L_) | ||
57 | { | ||
58 | int const _args{ lua_gettop(L_) }; // L_: args | ||
59 | Universe* const _U{ universe_get(L_) }; | ||
60 | //[[maybe_unused]] std::string_view const _modname{ luaL_checkstringview(L_, 1) }; | ||
61 | |||
62 | STACK_GROW(L_, 1); | ||
63 | |||
64 | lua_pushvalue(L_, lua_upvalueindex(1)); // L_: args require | ||
65 | lua_insert(L_, 1); // L_: require args | ||
66 | |||
67 | // Using 'lua_pcall()' to catch errors; otherwise a failing 'require' would | ||
68 | // leave us locked, blocking any future 'require' calls from other lanes. | ||
69 | |||
70 | _U->requireMutex.lock(); | ||
71 | // starting with Lua 5.4, require may return a second optional value, so we need LUA_MULTRET | ||
72 | LuaError const _rc{ lua_pcall(L_, _args, LUA_MULTRET, 0 /*errfunc*/) }; // L_: err|result(s) | ||
73 | _U->requireMutex.unlock(); | ||
74 | |||
75 | // the required module (or an error message) is left on the stack as returned value by original require function | ||
76 | |||
77 | if (_rc != LuaError::OK) { // LUA_ERRRUN / LUA_ERRMEM ? | ||
78 | raise_lua_error(L_); | ||
79 | } | ||
80 | // should be 1 for Lua <= 5.3, 1 or 2 starting with Lua 5.4 | ||
81 | return lua_gettop(L_); // L_: result(s) | ||
82 | } | ||
83 | |||
84 | // ################################################################################################# | ||
85 | |||
86 | /* | ||
87 | * Serialize calls to 'require', if it exists | ||
88 | */ | ||
89 | void serialize_require(lua_State* L_) | ||
90 | { | ||
91 | STACK_GROW(L_, 1); | ||
92 | STACK_CHECK_START_REL(L_, 0); | ||
93 | DEBUGSPEW_CODE(DebugSpew(universe_get(L_)) << "serializing require()" << std::endl); | ||
94 | |||
95 | // Check 'require' is there and not already wrapped; if not, do nothing | ||
96 | // | ||
97 | lua_getglobal(L_, "require"); | ||
98 | if (lua_isfunction(L_, -1) && lua_tocfunction(L_, -1) != luaG_new_require) { | ||
99 | // [-1]: original 'require' function | ||
100 | lua_pushcclosure(L_, luaG_new_require, 1 /*upvalues*/); | ||
101 | lua_setglobal(L_, "require"); | ||
102 | } else { | ||
103 | // [-1]: nil | ||
104 | lua_pop(L_, 1); | ||
105 | } | ||
106 | |||
107 | STACK_CHECK(L_, 0); | ||
108 | } | ||
109 | |||
110 | // ################################################################################################# | ||
111 | |||
112 | /*---=== luaG_newstate ===---*/ | 46 | /*---=== luaG_newstate ===---*/ |
113 | 47 | ||
114 | [[nodiscard]] static int require_lanes_core(lua_State* L_) | 48 | [[nodiscard]] static int require_lanes_core(lua_State* L_) |
@@ -397,7 +331,7 @@ lua_State* luaG_newstate(Universe* U_, SourceState from_, std::optional<std::str | |||
397 | } | 331 | } |
398 | lua_gc(_L, LUA_GCRESTART, 0); | 332 | lua_gc(_L, LUA_GCRESTART, 0); |
399 | 333 | ||
400 | serialize_require(_L); | 334 | tools::SerializeRequire(_L); |
401 | 335 | ||
402 | // call this after the base libraries are loaded and GC is restarted | 336 | // call this after the base libraries are loaded and GC is restarted |
403 | // will raise an error in from_ in case of problem | 337 | // will raise an error in from_ in case of problem |