diff options
Diffstat (limited to 'src/state.cpp')
-rw-r--r-- | src/state.cpp | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/src/state.cpp b/src/state.cpp index 1ba5a77..9ad237d 100644 --- a/src/state.cpp +++ b/src/state.cpp | |||
@@ -37,7 +37,7 @@ THE SOFTWARE. | |||
37 | #include "tools.h" | 37 | #include "tools.h" |
38 | #include "universe.h" | 38 | #include "universe.h" |
39 | 39 | ||
40 | // ################################################################################################ | 40 | // ################################################################################################# |
41 | 41 | ||
42 | /*---=== Serialize require ===--- | 42 | /*---=== Serialize require ===--- |
43 | */ | 43 | */ |
@@ -71,7 +71,7 @@ THE SOFTWARE. | |||
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 | } |
@@ -108,7 +108,7 @@ void serialize_require(DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L) | |||
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 | ||
@@ -161,10 +161,9 @@ static luaL_Reg const libs[] = | |||
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 | int i; | 164 | for (int i{ 0 }; libs[i].name; ++i) |
165 | for( i = 0; libs[i].name; ++ i) | ||
166 | { | 165 | { |
167 | if( strncmp( name_, libs[i].name, len_) == 0) | 166 | if (strncmp( name_, libs[i].name, len_) == 0) |
168 | { | 167 | { |
169 | lua_CFunction libfunc = libs[i].func; | 168 | lua_CFunction libfunc = libs[i].func; |
170 | name_ = libs[i].name; // note that the provided name_ doesn't necessarily ends with '\0', hence len_ | 169 | name_ = libs[i].name; // note that the provided name_ doesn't necessarily ends with '\0', hence len_ |
@@ -176,7 +175,7 @@ static void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L, char const | |||
176 | // 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) |
177 | luaL_requiref( L, name_, libfunc, !isLanesCore); | 176 | luaL_requiref( L, name_, libfunc, !isLanesCore); |
178 | // 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 |
179 | if( isLanesCore == true) | 178 | if (isLanesCore == true) |
180 | { | 179 | { |
181 | populate_func_lookup_table( L, -1, name_); | 180 | populate_func_lookup_table( L, -1, name_); |
182 | } | 181 | } |
@@ -220,7 +219,7 @@ void initialize_on_state_create( Universe* U, lua_State* L) | |||
220 | { | 219 | { |
221 | STACK_CHECK_START_REL(L, 1); // settings | 220 | STACK_CHECK_START_REL(L, 1); // settings |
222 | 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 |
223 | if( !lua_isnil(L, -1)) | 222 | if (!lua_isnil(L, -1)) |
224 | { | 223 | { |
225 | // store C function pointer in an internal variable | 224 | // store C function pointer in an internal variable |
226 | 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 |
@@ -371,7 +370,7 @@ lua_State* luaG_newstate(Universe* U, Source from_, char const* libs_) | |||
371 | { | 370 | { |
372 | // special "*" case (mainly to help with LuaJIT compatibility) | 371 | // special "*" case (mainly to help with LuaJIT compatibility) |
373 | // as we are called from luaopen_lanes_core() already, and that would deadlock | 372 | // as we are called from luaopen_lanes_core() already, and that would deadlock |
374 | if( libs_[0] == '*' && libs_[1] == 0) | 373 | if (libs_[0] == '*' && libs_[1] == 0) |
375 | { | 374 | { |
376 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "opening ALL standard libraries\n" INDENT_END)); | 375 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "opening ALL standard libraries\n" INDENT_END)); |
377 | luaL_openlibs( L); | 376 | luaL_openlibs( L); |
@@ -396,26 +395,25 @@ lua_State* luaG_newstate(Universe* U, Source from_, char const* libs_) | |||
396 | STACK_CHECK(L, 0); | 395 | STACK_CHECK(L, 0); |
397 | 396 | ||
398 | // scan all libraries, open them one by one | 397 | // scan all libraries, open them one by one |
399 | if( libs_) | 398 | if (libs_) |
400 | { | 399 | { |
401 | char const* p; | 400 | unsigned int len{ 0 }; |
402 | unsigned int len = 0; | 401 | for (char const* p{ libs_ }; *p; p += len) |
403 | for( p = libs_; *p; p += len) | ||
404 | { | 402 | { |
405 | // skip delimiters ('.' can be part of name for "lanes.core") | 403 | // skip delimiters ('.' can be part of name for "lanes.core") |
406 | while( *p && !isalnum( *p) && *p != '.') | 404 | while( *p && !isalnum(*p) && *p != '.') |
407 | ++ p; | 405 | ++ p; |
408 | // skip name | 406 | // skip name |
409 | len = 0; | 407 | len = 0; |
410 | while( isalnum( p[len]) || p[len] == '.') | 408 | while( isalnum(p[len]) || p[len] == '.') |
411 | ++ len; | 409 | ++ len; |
412 | // open library | 410 | // open library |
413 | open1lib( DEBUGSPEW_PARAM_COMMA( U) L, p, len); | 411 | open1lib(DEBUGSPEW_PARAM_COMMA( U) L, p, len); |
414 | } | 412 | } |
415 | } | 413 | } |
416 | lua_gc( L, LUA_GCRESTART, 0); | 414 | lua_gc(L, LUA_GCRESTART, 0); |
417 | 415 | ||
418 | serialize_require( DEBUGSPEW_PARAM_COMMA( U) L); | 416 | serialize_require(DEBUGSPEW_PARAM_COMMA( U) L); |
419 | 417 | ||
420 | // call this after the base libraries are loaded and GC is restarted | 418 | // call this after the base libraries are loaded and GC is restarted |
421 | // will raise an error in from_ in case of problem | 419 | // will raise an error in from_ in case of problem |
@@ -423,7 +421,7 @@ lua_State* luaG_newstate(Universe* U, Source from_, char const* libs_) | |||
423 | 421 | ||
424 | STACK_CHECK(L, 0); | 422 | STACK_CHECK(L, 0); |
425 | // after all this, register everything we find in our name<->function database | 423 | // after all this, register everything we find in our name<->function database |
426 | lua_pushglobaltable( L); // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack | 424 | lua_pushglobaltable(L); // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack |
427 | STACK_CHECK(L, 1); | 425 | STACK_CHECK(L, 1); |
428 | populate_func_lookup_table(L, -1, nullptr); | 426 | populate_func_lookup_table(L, -1, nullptr); |
429 | 427 | ||