diff options
Diffstat (limited to 'src/state.cpp')
-rw-r--r-- | src/state.cpp | 102 |
1 files changed, 51 insertions, 51 deletions
diff --git a/src/state.cpp b/src/state.cpp index 9898812..d6dfb89 100644 --- a/src/state.cpp +++ b/src/state.cpp | |||
@@ -49,34 +49,34 @@ THE SOFTWARE. | |||
49 | // | 49 | // |
50 | // Upvalues: [1]: original 'require' function | 50 | // Upvalues: [1]: original 'require' function |
51 | // | 51 | // |
52 | [[nodiscard]] static int luaG_new_require(lua_State* L) | 52 | [[nodiscard]] static int luaG_new_require(lua_State* L_) |
53 | { | 53 | { |
54 | int rc; | 54 | int rc; |
55 | int const args = lua_gettop( L); // args | 55 | int const args = lua_gettop( L_); // args |
56 | Universe* U = universe_get( L); | 56 | Universe* U = universe_get( L_); |
57 | //char const* modname = luaL_checkstring( L, 1); | 57 | //char const* modname = luaL_checkstring( L, 1); |
58 | 58 | ||
59 | STACK_GROW( L, 1); | 59 | STACK_GROW( L_, 1); |
60 | 60 | ||
61 | lua_pushvalue( L, lua_upvalueindex( 1)); // args require | 61 | lua_pushvalue( L_, lua_upvalueindex( 1)); // args require |
62 | lua_insert( L, 1); // require args | 62 | lua_insert( L_, 1); // require args |
63 | 63 | ||
64 | // Using 'lua_pcall()' to catch errors; otherwise a failing 'require' would | 64 | // Using 'lua_pcall()' to catch errors; otherwise a failing 'require' would |
65 | // leave us locked, blocking any future 'require' calls from other lanes. | 65 | // leave us locked, blocking any future 'require' calls from other lanes. |
66 | 66 | ||
67 | U->require_cs.lock(); | 67 | U->require_cs.lock(); |
68 | // starting with Lua 5.4, require may return a second optional value, so we need LUA_MULTRET | 68 | // starting with Lua 5.4, require may return a second optional value, so we need LUA_MULTRET |
69 | rc = lua_pcall( L, args, LUA_MULTRET, 0 /*errfunc*/ ); // err|result(s) | 69 | rc = lua_pcall( L_, args, LUA_MULTRET, 0 /*errfunc*/ ); // err|result(s) |
70 | U->require_cs.unlock(); | 70 | U->require_cs.unlock(); |
71 | 71 | ||
72 | // the required module (or an error message) is left on the stack as returned value by original require function | 72 | // the required module (or an error message) is left on the stack as returned value by original require function |
73 | 73 | ||
74 | if (rc != LUA_OK) // LUA_ERRRUN / LUA_ERRMEM ? | 74 | if (rc != LUA_OK) // LUA_ERRRUN / LUA_ERRMEM ? |
75 | { | 75 | { |
76 | raise_lua_error(L); | 76 | raise_lua_error(L_); |
77 | } | 77 | } |
78 | // should be 1 for Lua <= 5.3, 1 or 2 starting with Lua 5.4 | 78 | // should be 1 for Lua <= 5.3, 1 or 2 starting with Lua 5.4 |
79 | return lua_gettop(L); // result(s) | 79 | return lua_gettop(L_); // result(s) |
80 | } | 80 | } |
81 | 81 | ||
82 | // ################################################################################################# | 82 | // ################################################################################################# |
@@ -84,38 +84,38 @@ THE SOFTWARE. | |||
84 | /* | 84 | /* |
85 | * Serialize calls to 'require', if it exists | 85 | * Serialize calls to 'require', if it exists |
86 | */ | 86 | */ |
87 | void serialize_require(DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L) | 87 | void serialize_require(DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L_) |
88 | { | 88 | { |
89 | STACK_GROW(L, 1); | 89 | STACK_GROW(L_, 1); |
90 | STACK_CHECK_START_REL(L, 0); | 90 | STACK_CHECK_START_REL(L_, 0); |
91 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "serializing require()\n" INDENT_END)); | 91 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "serializing require()\n" INDENT_END)); |
92 | 92 | ||
93 | // Check 'require' is there and not already wrapped; if not, do nothing | 93 | // Check 'require' is there and not already wrapped; if not, do nothing |
94 | // | 94 | // |
95 | lua_getglobal(L, "require"); | 95 | lua_getglobal(L_, "require"); |
96 | if (lua_isfunction(L, -1) && lua_tocfunction(L, -1) != luaG_new_require) | 96 | if (lua_isfunction(L_, -1) && lua_tocfunction(L_, -1) != luaG_new_require) |
97 | { | 97 | { |
98 | // [-1]: original 'require' function | 98 | // [-1]: original 'require' function |
99 | lua_pushcclosure(L, luaG_new_require, 1 /*upvalues*/); | 99 | lua_pushcclosure(L_, luaG_new_require, 1 /*upvalues*/); |
100 | lua_setglobal(L, "require"); | 100 | lua_setglobal(L_, "require"); |
101 | } | 101 | } |
102 | else | 102 | else |
103 | { | 103 | { |
104 | // [-1]: nil | 104 | // [-1]: nil |
105 | lua_pop(L, 1); | 105 | lua_pop(L_, 1); |
106 | } | 106 | } |
107 | 107 | ||
108 | STACK_CHECK(L, 0); | 108 | STACK_CHECK(L_, 0); |
109 | } | 109 | } |
110 | 110 | ||
111 | // ################################################################################################# | 111 | // ################################################################################################# |
112 | 112 | ||
113 | /*---=== luaG_newstate ===---*/ | 113 | /*---=== luaG_newstate ===---*/ |
114 | 114 | ||
115 | [[nodiscard]] static int require_lanes_core(lua_State* L) | 115 | [[nodiscard]] static int require_lanes_core(lua_State* L_) |
116 | { | 116 | { |
117 | // leaves a copy of 'lanes.core' module table on the stack | 117 | // leaves a copy of 'lanes.core' module table on the stack |
118 | luaL_requiref( L, "lanes.core", luaopen_lanes_core, 0); | 118 | luaL_requiref( L_, "lanes.core", luaopen_lanes_core, 0); |
119 | return 1; | 119 | return 1; |
120 | } | 120 | } |
121 | 121 | ||
@@ -159,7 +159,7 @@ static luaL_Reg const libs[] = | |||
159 | 159 | ||
160 | // ################################################################################################# | 160 | // ################################################################################################# |
161 | 161 | ||
162 | static void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L, char const* name_, size_t len_) | 162 | static void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L_, char const* name_, size_t len_) |
163 | { | 163 | { |
164 | for (int i{ 0 }; libs[i].name; ++i) | 164 | for (int i{ 0 }; libs[i].name; ++i) |
165 | { | 165 | { |
@@ -171,16 +171,16 @@ static void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L, char const | |||
171 | { | 171 | { |
172 | bool const isLanesCore{ libfunc == require_lanes_core }; // don't want to create a global for "lanes.core" | 172 | bool const isLanesCore{ libfunc == require_lanes_core }; // don't want to create a global for "lanes.core" |
173 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "opening %.*s library\n" INDENT_END, (int) len_, name_)); | 173 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "opening %.*s library\n" INDENT_END, (int) len_, name_)); |
174 | STACK_CHECK_START_REL(L, 0); | 174 | STACK_CHECK_START_REL(L_, 0); |
175 | // open the library as if through require(), and create a global as well if necessary (the library table is left on the stack) | 175 | // open the library as if through require(), and create a global as well if necessary (the library table is left on the stack) |
176 | luaL_requiref( L, name_, libfunc, !isLanesCore); | 176 | luaL_requiref( L_, name_, libfunc, !isLanesCore); |
177 | // lanes.core doesn't declare a global, so scan it here and now | 177 | // lanes.core doesn't declare a global, so scan it here and now |
178 | if (isLanesCore == true) | 178 | if (isLanesCore == true) |
179 | { | 179 | { |
180 | populate_func_lookup_table( L, -1, name_); | 180 | populate_func_lookup_table( L_, -1, name_); |
181 | } | 181 | } |
182 | lua_pop( L, 1); | 182 | lua_pop( L_, 1); |
183 | STACK_CHECK( L, 0); | 183 | STACK_CHECK( L_, 0); |
184 | } | 184 | } |
185 | break; | 185 | break; |
186 | } | 186 | } |
@@ -208,33 +208,33 @@ static void copy_one_time_settings(Universe* U, SourceState L1, DestState L2) | |||
208 | raise_luaL_error(L1, "failed to copy settings when loading lanes.core"); | 208 | raise_luaL_error(L1, "failed to copy settings when loading lanes.core"); |
209 | } | 209 | } |
210 | // set L2:_R[kConfigRegKey] = settings | 210 | // set L2:_R[kConfigRegKey] = settings |
211 | kConfigRegKey.setValue(L2, [](lua_State* L) { lua_insert(L, -2); }); // config | 211 | kConfigRegKey.setValue(L2, [](lua_State* L_) { lua_insert(L_, -2); }); // config |
212 | STACK_CHECK(L2, 0); | 212 | STACK_CHECK(L2, 0); |
213 | STACK_CHECK(L1, 0); | 213 | STACK_CHECK(L1, 0); |
214 | } | 214 | } |
215 | 215 | ||
216 | // ################################################################################################# | 216 | // ################################################################################################# |
217 | 217 | ||
218 | void initialize_on_state_create( Universe* U, lua_State* L) | 218 | void initialize_on_state_create( Universe* U, lua_State* L_) |
219 | { | 219 | { |
220 | STACK_CHECK_START_REL(L, 1); // settings | 220 | STACK_CHECK_START_REL(L_, 1); // settings |
221 | lua_getfield(L, -1, "on_state_create"); // settings on_state_create|nil | 221 | lua_getfield(L_, -1, "on_state_create"); // settings on_state_create|nil |
222 | if (!lua_isnil(L, -1)) | 222 | if (!lua_isnil(L_, -1)) |
223 | { | 223 | { |
224 | // store C function pointer in an internal variable | 224 | // store C function pointer in an internal variable |
225 | U->on_state_create_func = lua_tocfunction(L, -1); // settings on_state_create | 225 | U->on_state_create_func = lua_tocfunction(L_, -1); // settings on_state_create |
226 | if (U->on_state_create_func != nullptr) | 226 | if (U->on_state_create_func != nullptr) |
227 | { | 227 | { |
228 | // make sure the function doesn't have upvalues | 228 | // make sure the function doesn't have upvalues |
229 | char const* upname = lua_getupvalue(L, -1, 1); // settings on_state_create upval? | 229 | char const* upname = lua_getupvalue(L_, -1, 1); // settings on_state_create upval? |
230 | if (upname != nullptr) // should be "" for C functions with upvalues if any | 230 | if (upname != nullptr) // should be "" for C functions with upvalues if any |
231 | { | 231 | { |
232 | raise_luaL_error(L, "on_state_create shouldn't have upvalues"); | 232 | raise_luaL_error(L_, "on_state_create shouldn't have upvalues"); |
233 | } | 233 | } |
234 | // remove this C function from the config table so that it doesn't cause problems | 234 | // remove this C function from the config table so that it doesn't cause problems |
235 | // when we transfer the config table in newly created Lua states | 235 | // when we transfer the config table in newly created Lua states |
236 | lua_pushnil(L); // settings on_state_create nil | 236 | lua_pushnil(L_); // settings on_state_create nil |
237 | lua_setfield(L, -3, "on_state_create"); // settings on_state_create | 237 | lua_setfield(L_, -3, "on_state_create"); // settings on_state_create |
238 | } | 238 | } |
239 | else | 239 | else |
240 | { | 240 | { |
@@ -242,8 +242,8 @@ void initialize_on_state_create( Universe* U, lua_State* L) | |||
242 | U->on_state_create_func = (lua_CFunction) initialize_on_state_create; | 242 | U->on_state_create_func = (lua_CFunction) initialize_on_state_create; |
243 | } | 243 | } |
244 | } | 244 | } |
245 | lua_pop(L, 1); // settings | 245 | lua_pop(L_, 1); // settings |
246 | STACK_CHECK(L, 1); | 246 | STACK_CHECK(L_, 1); |
247 | } | 247 | } |
248 | 248 | ||
249 | // ################################################################################################# | 249 | // ################################################################################################# |
@@ -281,16 +281,16 @@ lua_State* create_state(Universe* U, lua_State* from_) | |||
281 | 281 | ||
282 | // ################################################################################################# | 282 | // ################################################################################################# |
283 | 283 | ||
284 | void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMode mode_) | 284 | void call_on_state_create(Universe* U, lua_State* L_, lua_State* from_, LookupMode mode_) |
285 | { | 285 | { |
286 | if (U->on_state_create_func != nullptr) | 286 | if (U->on_state_create_func != nullptr) |
287 | { | 287 | { |
288 | STACK_CHECK_START_REL(L, 0); | 288 | STACK_CHECK_START_REL(L_, 0); |
289 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "calling on_state_create()\n" INDENT_END)); | 289 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "calling on_state_create()\n" INDENT_END)); |
290 | if (U->on_state_create_func != (lua_CFunction) initialize_on_state_create) | 290 | if (U->on_state_create_func != (lua_CFunction) initialize_on_state_create) |
291 | { | 291 | { |
292 | // C function: recreate a closure in the new state, bypassing the lookup scheme | 292 | // C function: recreate a closure in the new state, bypassing the lookup scheme |
293 | lua_pushcfunction(L, U->on_state_create_func); // on_state_create() | 293 | lua_pushcfunction(L_, U->on_state_create_func); // on_state_create() |
294 | } | 294 | } |
295 | else // Lua function located in the config table, copied when we opened "lanes.core" | 295 | else // Lua function located in the config table, copied when we opened "lanes.core" |
296 | { | 296 | { |
@@ -298,21 +298,21 @@ void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMod | |||
298 | { | 298 | { |
299 | // if attempting to call in a keeper state, do nothing because the function doesn't exist there | 299 | // if attempting to call in a keeper state, do nothing because the function doesn't exist there |
300 | // this doesn't count as an error though | 300 | // this doesn't count as an error though |
301 | STACK_CHECK(L, 0); | 301 | STACK_CHECK(L_, 0); |
302 | return; | 302 | return; |
303 | } | 303 | } |
304 | kConfigRegKey.pushValue(L); // {} | 304 | kConfigRegKey.pushValue(L_); // {} |
305 | STACK_CHECK(L, 1); | 305 | STACK_CHECK(L_, 1); |
306 | lua_getfield(L, -1, "on_state_create"); // {} on_state_create() | 306 | lua_getfield(L_, -1, "on_state_create"); // {} on_state_create() |
307 | lua_remove(L, -2); // on_state_create() | 307 | lua_remove(L_, -2); // on_state_create() |
308 | } | 308 | } |
309 | STACK_CHECK(L, 1); | 309 | STACK_CHECK(L_, 1); |
310 | // capture error and raise it in caller state | 310 | // capture error and raise it in caller state |
311 | if (lua_pcall(L, 0, 0, 0) != LUA_OK) | 311 | if (lua_pcall(L_, 0, 0, 0) != LUA_OK) |
312 | { | 312 | { |
313 | raise_luaL_error(from_, "on_state_create failed: \"%s\"", lua_isstring(L, -1) ? lua_tostring(L, -1) : lua_typename(L, lua_type(L, -1))); | 313 | raise_luaL_error(from_, "on_state_create failed: \"%s\"", lua_isstring(L_, -1) ? lua_tostring(L_, -1) : lua_typename(L_, lua_type(L_, -1))); |
314 | } | 314 | } |
315 | STACK_CHECK(L, 0); | 315 | STACK_CHECK(L_, 0); |
316 | } | 316 | } |
317 | } | 317 | } |
318 | 318 | ||
@@ -344,7 +344,7 @@ lua_State* luaG_newstate(Universe* U, SourceState from_, char const* libs_) | |||
344 | STACK_CHECK(L, 0); | 344 | STACK_CHECK(L, 0); |
345 | 345 | ||
346 | // we'll need this every time we transfer some C function from/to this state | 346 | // we'll need this every time we transfer some C function from/to this state |
347 | kLookupRegKey.setValue(L, [](lua_State* L) { lua_newtable(L); }); | 347 | kLookupRegKey.setValue(L, [](lua_State* L_) { lua_newtable(L_); }); |
348 | STACK_CHECK(L, 0); | 348 | STACK_CHECK(L, 0); |
349 | 349 | ||
350 | // neither libs (not even 'base') nor special init func: we are done | 350 | // neither libs (not even 'base') nor special init func: we are done |