diff options
Diffstat (limited to 'src/state.cpp')
-rw-r--r-- | src/state.cpp | 56 |
1 files changed, 24 insertions, 32 deletions
diff --git a/src/state.cpp b/src/state.cpp index 90a7c5b..0175449 100644 --- a/src/state.cpp +++ b/src/state.cpp | |||
@@ -55,7 +55,7 @@ THE SOFTWARE. | |||
55 | { | 55 | { |
56 | int const _args{ lua_gettop(L_) }; // L_: args | 56 | int const _args{ lua_gettop(L_) }; // L_: args |
57 | Universe* const _U{ universe_get(L_) }; | 57 | Universe* const _U{ universe_get(L_) }; |
58 | // char const* modname = luaL_checkstring(L_, 1); | 58 | //[[maybe_unused]] std::string_view const _modname{ luaL_checkstringview(L_, 1) }; |
59 | 59 | ||
60 | STACK_GROW(L_, 1); | 60 | STACK_GROW(L_, 1); |
61 | 61 | ||
@@ -88,7 +88,7 @@ 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(U_))); | 91 | DEBUGSPEW_CODE(DebugSpew(U_) << "serializing require()" << std::endl); |
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 | // |
@@ -155,23 +155,23 @@ namespace global | |||
155 | 155 | ||
156 | // ################################################################################################# | 156 | // ################################################################################################# |
157 | 157 | ||
158 | static void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L_, char const* name_, size_t len_) | 158 | static void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L_, std::string_view const& name_) |
159 | { | 159 | { |
160 | for (luaL_Reg const& _entry : global::sLibs) { | 160 | for (luaL_Reg const& _entry : global::sLibs) { |
161 | if (strncmp(name_, _entry.name, len_) == 0) { | 161 | if (name_ == _entry.name) { |
162 | lua_CFunction const _libfunc{ _entry.func }; | 162 | lua_CFunction const _libfunc{ _entry.func }; |
163 | if (!_libfunc) { | 163 | if (!_libfunc) { |
164 | continue; | 164 | break; |
165 | } | 165 | } |
166 | name_ = _entry.name; // note that the provided name_ doesn't necessarily ends with '\0', hence len_ | 166 | std::string_view const _name{ _entry.name }; |
167 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "opening %.*s library\n" INDENT_END(U_), (int) len_, name_)); | 167 | DEBUGSPEW_CODE(DebugSpew(U_) << "opening '" << _name << "' library" << std::endl); |
168 | STACK_CHECK_START_REL(L_, 0); | 168 | STACK_CHECK_START_REL(L_, 0); |
169 | // open the library as if through require(), and create a global as well if necessary (the library table is left on the stack) | 169 | // open the library as if through require(), and create a global as well if necessary (the library table is left on the stack) |
170 | bool const isLanesCore{ _libfunc == require_lanes_core }; // don't want to create a global for "lanes.core" | 170 | bool const isLanesCore{ _libfunc == require_lanes_core }; // don't want to create a global for "lanes.core" |
171 | luaL_requiref(L_, name_, _libfunc, !isLanesCore); // L_: {lib} | 171 | luaL_requiref(L_, _name.data(), _libfunc, !isLanesCore); // L_: {lib} |
172 | // lanes.core doesn't declare a global, so scan it here and now | 172 | // lanes.core doesn't declare a global, so scan it here and now |
173 | if (isLanesCore) { | 173 | if (isLanesCore) { |
174 | populate_func_lookup_table(L_, -1, name_); | 174 | populate_func_lookup_table(L_, -1, _name); |
175 | } | 175 | } |
176 | lua_pop(L_, 1); // L_: | 176 | lua_pop(L_, 1); // L_: |
177 | STACK_CHECK(L_, 0); | 177 | STACK_CHECK(L_, 0); |
@@ -182,14 +182,6 @@ static void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L_, char con | |||
182 | 182 | ||
183 | // ################################################################################################# | 183 | // ################################################################################################# |
184 | 184 | ||
185 | template<size_t N> | ||
186 | static inline void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U_) lua_State* L_, char const (&name_)[N]) | ||
187 | { | ||
188 | open1lib(DEBUGSPEW_PARAM_COMMA(U_) L_, name_, N - 1); | ||
189 | } | ||
190 | |||
191 | // ################################################################################################# | ||
192 | |||
193 | // just like lua_xmove, args are (from, to) | 185 | // just like lua_xmove, args are (from, to) |
194 | static void copy_one_time_settings(Universe* U_, SourceState L1_, DestState L2_) | 186 | static void copy_one_time_settings(Universe* U_, SourceState L1_, DestState L2_) |
195 | { | 187 | { |
@@ -199,7 +191,7 @@ static void copy_one_time_settings(Universe* U_, SourceState L1_, DestState L2_) | |||
199 | STACK_CHECK_START_REL(L1_, 0); | 191 | STACK_CHECK_START_REL(L1_, 0); |
200 | STACK_CHECK_START_REL(L2_, 0); | 192 | STACK_CHECK_START_REL(L2_, 0); |
201 | 193 | ||
202 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END(U_))); | 194 | DEBUGSPEW_CODE(DebugSpew(U_) << "copy_one_time_settings()" << std::endl); |
203 | 195 | ||
204 | kConfigRegKey.pushValue(L1_); // L1_: config | 196 | kConfigRegKey.pushValue(L1_); // L1_: config |
205 | // copy settings from from source to destination registry | 197 | // copy settings from from source to destination registry |
@@ -275,7 +267,7 @@ void CallOnStateCreate(Universe* U_, lua_State* L_, lua_State* from_, LookupMode | |||
275 | { | 267 | { |
276 | if (U_->onStateCreateFunc != nullptr) { | 268 | if (U_->onStateCreateFunc != nullptr) { |
277 | STACK_CHECK_START_REL(L_, 0); | 269 | STACK_CHECK_START_REL(L_, 0); |
278 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "calling on_state_create()\n" INDENT_END(U_))); | 270 | DEBUGSPEW_CODE(DebugSpew(U_) << "calling on_state_create()" << std::endl); |
279 | if (U_->onStateCreateFunc != reinterpret_cast<lua_CFunction>(InitializeOnStateCreate)) { | 271 | if (U_->onStateCreateFunc != reinterpret_cast<lua_CFunction>(InitializeOnStateCreate)) { |
280 | // C function: recreate a closure in the new state, bypassing the lookup scheme | 272 | // C function: recreate a closure in the new state, bypassing the lookup scheme |
281 | lua_pushcfunction(L_, U_->onStateCreateFunc); // on_state_create() | 273 | lua_pushcfunction(L_, U_->onStateCreateFunc); // on_state_create() |
@@ -333,11 +325,11 @@ lua_State* luaG_newstate(Universe* U_, SourceState from_, char const* libs_) | |||
333 | 325 | ||
334 | // neither libs (not even 'base') nor special init func: we are done | 326 | // neither libs (not even 'base') nor special init func: we are done |
335 | if (libs_ == nullptr && U_->onStateCreateFunc == nullptr) { | 327 | if (libs_ == nullptr && U_->onStateCreateFunc == nullptr) { |
336 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_newstate(nullptr)\n" INDENT_END(U_))); | 328 | DEBUGSPEW_CODE(DebugSpew(U_) << "luaG_newstate(nullptr)" << std::endl); |
337 | return _L; | 329 | return _L; |
338 | } | 330 | } |
339 | 331 | ||
340 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_newstate()\n" INDENT_END(U_))); | 332 | DEBUGSPEW_CODE(DebugSpew(U_) << "luaG_newstate()" << std::endl); |
341 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U_ }); | 333 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U_ }); |
342 | 334 | ||
343 | // copy settings (for example because it may contain a Lua on_state_create function) | 335 | // copy settings (for example because it may contain a Lua on_state_create function) |
@@ -351,13 +343,13 @@ lua_State* luaG_newstate(Universe* U_, SourceState from_, char const* libs_) | |||
351 | // special "*" case (mainly to help with LuaJIT compatibility) | 343 | // special "*" case (mainly to help with LuaJIT compatibility) |
352 | // as we are called from luaopen_lanes_core() already, and that would deadlock | 344 | // as we are called from luaopen_lanes_core() already, and that would deadlock |
353 | if (libs_[0] == '*' && libs_[1] == 0) { | 345 | if (libs_[0] == '*' && libs_[1] == 0) { |
354 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "opening ALL standard libraries\n" INDENT_END(U_))); | 346 | DEBUGSPEW_CODE(DebugSpew(U_) << "opening ALL standard libraries" << std::endl); |
355 | luaL_openlibs(_L); | 347 | luaL_openlibs(_L); |
356 | // don't forget lanes.core for regular lane states | 348 | // don't forget lanes.core for regular lane states |
357 | open1lib(DEBUGSPEW_PARAM_COMMA(U_) _L, kLanesCoreLibName); | 349 | open1lib(DEBUGSPEW_PARAM_COMMA(U_) _L, kLanesCoreLibName); |
358 | libs_ = nullptr; // done with libs | 350 | libs_ = nullptr; // done with libs |
359 | } else { | 351 | } else { |
360 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "opening base library\n" INDENT_END(U_))); | 352 | DEBUGSPEW_CODE(DebugSpew(U_) << "opening 'base' library" << std::endl); |
361 | #if LUA_VERSION_NUM >= 502 | 353 | #if LUA_VERSION_NUM >= 502 |
362 | // open base library the same way as in luaL_openlibs() | 354 | // open base library the same way as in luaL_openlibs() |
363 | luaL_requiref(_L, LUA_GNAME, luaopen_base, 1); | 355 | luaL_requiref(_L, LUA_GNAME, luaopen_base, 1); |
@@ -373,17 +365,17 @@ lua_State* luaG_newstate(Universe* U_, SourceState from_, char const* libs_) | |||
373 | 365 | ||
374 | // scan all libraries, open them one by one | 366 | // scan all libraries, open them one by one |
375 | if (libs_) { | 367 | if (libs_) { |
376 | unsigned int len{ 0 }; | 368 | unsigned int _len{ 0 }; |
377 | for (char const* p{ libs_ }; *p; p += len) { | 369 | for (char const* _p{ libs_ }; *_p; _p += _len) { |
378 | // skip delimiters ('.' can be part of name for "lanes.core") | 370 | // skip delimiters ('.' can be part of name for "lanes.core") |
379 | while (*p && !isalnum(*p) && *p != '.') | 371 | while (*_p && !isalnum(*_p) && *_p != '.') |
380 | ++p; | 372 | ++_p; |
381 | // skip name | 373 | // skip name |
382 | len = 0; | 374 | _len = 0; |
383 | while (isalnum(p[len]) || p[len] == '.') | 375 | while (isalnum(_p[_len]) || _p[_len] == '.') |
384 | ++len; | 376 | ++_len; |
385 | // open library | 377 | // open library |
386 | open1lib(DEBUGSPEW_PARAM_COMMA(U_) _L, p, len); | 378 | open1lib(DEBUGSPEW_PARAM_COMMA(U_) _L, { _p, _len }); |
387 | } | 379 | } |
388 | } | 380 | } |
389 | lua_gc(_L, LUA_GCRESTART, 0); | 381 | lua_gc(_L, LUA_GCRESTART, 0); |
@@ -398,7 +390,7 @@ lua_State* luaG_newstate(Universe* U_, SourceState from_, char const* libs_) | |||
398 | // after all this, register everything we find in our name<->function database | 390 | // after all this, register everything we find in our name<->function database |
399 | lua_pushglobaltable(_L); // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack | 391 | lua_pushglobaltable(_L); // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack |
400 | STACK_CHECK(_L, 1); | 392 | STACK_CHECK(_L, 1); |
401 | populate_func_lookup_table(_L, -1, nullptr); | 393 | populate_func_lookup_table(_L, -1, {}); |
402 | 394 | ||
403 | #if 1 && USE_DEBUG_SPEW() | 395 | #if 1 && USE_DEBUG_SPEW() |
404 | // dump the lookup database contents | 396 | // dump the lookup database contents |