diff options
Diffstat (limited to 'src/tools.cpp')
-rw-r--r-- | src/tools.cpp | 95 |
1 files changed, 49 insertions, 46 deletions
diff --git a/src/tools.cpp b/src/tools.cpp index df7602e..ac5f7c5 100644 --- a/src/tools.cpp +++ b/src/tools.cpp | |||
@@ -359,9 +359,9 @@ static void update_lookup_entry( DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* | |||
359 | 359 | ||
360 | size_t prevNameLength, newNameLength; | 360 | size_t prevNameLength, newNameLength; |
361 | char const* prevName; | 361 | char const* prevName; |
362 | DEBUGSPEW_CODE( char const *newName); | 362 | DEBUGSPEW_CODE(char const *newName); |
363 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "update_lookup_entry()\n" INDENT_END)); | 363 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "update_lookup_entry()\n" INDENT_END)); |
364 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 364 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
365 | 365 | ||
366 | STACK_CHECK_START_REL(L, 0); | 366 | STACK_CHECK_START_REL(L, 0); |
367 | // first, raise an error if the function is already known | 367 | // first, raise an error if the function is already known |
@@ -420,8 +420,8 @@ static void update_lookup_entry( DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* | |||
420 | lua_rawseti( L, fqn, _depth); // ... {bfc} k | 420 | lua_rawseti( L, fqn, _depth); // ... {bfc} k |
421 | } | 421 | } |
422 | -- _depth; | 422 | -- _depth; |
423 | STACK_CHECK( L, -1); | 423 | STACK_CHECK(L, -1); |
424 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 424 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
425 | } | 425 | } |
426 | 426 | ||
427 | // ################################################################################################# | 427 | // ################################################################################################# |
@@ -435,8 +435,8 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) | |||
435 | int const cache = _ctx_base + 2; | 435 | int const cache = _ctx_base + 2; |
436 | // we need to remember subtables to process them after functions encountered at the current depth (breadth-first search) | 436 | // we need to remember subtables to process them after functions encountered at the current depth (breadth-first search) |
437 | int const breadth_first_cache = lua_gettop( L) + 1; | 437 | int const breadth_first_cache = lua_gettop( L) + 1; |
438 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "populate_func_lookup_table_recur()\n" INDENT_END)); | 438 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "populate_func_lookup_table_recur()\n" INDENT_END)); |
439 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 439 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
440 | 440 | ||
441 | STACK_GROW( L, 6); | 441 | STACK_GROW( L, 6); |
442 | // slot _i contains a table where we search for functions (or a full userdata with a metatable) | 442 | // slot _i contains a table where we search for functions (or a full userdata with a metatable) |
@@ -457,8 +457,8 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) | |||
457 | STACK_CHECK( L, 0); | 457 | STACK_CHECK( L, 0); |
458 | if( visit_count > 0) | 458 | if( visit_count > 0) |
459 | { | 459 | { |
460 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "already visited\n" INDENT_END)); | 460 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "already visited\n" INDENT_END)); |
461 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 461 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
462 | return; | 462 | return; |
463 | } | 463 | } |
464 | 464 | ||
@@ -513,7 +513,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) | |||
513 | { | 513 | { |
514 | DEBUGSPEW_CODE( char const* key = (lua_type( L, -2) == LUA_TSTRING) ? lua_tostring( L, -2) : "not a string"); | 514 | DEBUGSPEW_CODE( char const* key = (lua_type( L, -2) == LUA_TSTRING) ? lua_tostring( L, -2) : "not a string"); |
515 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "table '%s'\n" INDENT_END, key)); | 515 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "table '%s'\n" INDENT_END, key)); |
516 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 516 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
517 | // un-visit this table in case we do need to process it | 517 | // un-visit this table in case we do need to process it |
518 | lua_pushvalue( L, -1); // ... {_i} {bfc} k {} {} | 518 | lua_pushvalue( L, -1); // ... {_i} {bfc} k {} {} |
519 | lua_rawget( L, cache); // ... {_i} {bfc} k {} n | 519 | lua_rawget( L, cache); // ... {_i} {bfc} k {} n |
@@ -536,7 +536,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) | |||
536 | populate_func_lookup_table_recur( DEBUGSPEW_PARAM_COMMA( U) L, _ctx_base, lua_gettop( L), _depth); | 536 | populate_func_lookup_table_recur( DEBUGSPEW_PARAM_COMMA( U) L, _ctx_base, lua_gettop( L), _depth); |
537 | lua_pop( L, 1); // ... {_i} {bfc} k | 537 | lua_pop( L, 1); // ... {_i} {bfc} k |
538 | STACK_CHECK( L, 2); | 538 | STACK_CHECK( L, 2); |
539 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 539 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
540 | } | 540 | } |
541 | // remove table name from fqn stack | 541 | // remove table name from fqn stack |
542 | lua_pushnil( L); // ... {_i} {bfc} nil | 542 | lua_pushnil( L); // ... {_i} {bfc} nil |
@@ -546,7 +546,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) | |||
546 | lua_pop( L, 1); // ... {_i} | 546 | lua_pop( L, 1); // ... {_i} |
547 | STACK_CHECK( L, 0); | 547 | STACK_CHECK( L, 0); |
548 | // we are done // ... {_i} {bfc} | 548 | // we are done // ... {_i} {bfc} |
549 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 549 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
550 | } | 550 | } |
551 | 551 | ||
552 | // ################################################################################################# | 552 | // ################################################################################################# |
@@ -561,7 +561,7 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_) | |||
561 | int start_depth = 0; | 561 | int start_depth = 0; |
562 | DEBUGSPEW_CODE( Universe* U = universe_get( L)); | 562 | DEBUGSPEW_CODE( Universe* U = universe_get( L)); |
563 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, name_ ? name_ : "nullptr")); | 563 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, name_ ? name_ : "nullptr")); |
564 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 564 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
565 | STACK_GROW( L, 3); | 565 | STACK_GROW( L, 3); |
566 | STACK_CHECK_START_REL(L, 0); | 566 | STACK_CHECK_START_REL(L, 0); |
567 | LOOKUP_REGKEY.pushValue(L); // {} | 567 | LOOKUP_REGKEY.pushValue(L); // {} |
@@ -612,7 +612,7 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_) | |||
612 | (void) luaL_error( L, "unsupported module type %s", lua_typename( L, lua_type( L, in_base))); | 612 | (void) luaL_error( L, "unsupported module type %s", lua_typename( L, lua_type( L, in_base))); |
613 | } | 613 | } |
614 | STACK_CHECK( L, 0); | 614 | STACK_CHECK( L, 0); |
615 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 615 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
616 | } | 616 | } |
617 | 617 | ||
618 | // ################################################################################################# | 618 | // ################################################################################################# |
@@ -1774,10 +1774,10 @@ static bool inter_copy_function(Universe* U, lua_State* L2, int L2_cache_i, lua_ | |||
1774 | } | 1774 | } |
1775 | else // regular function | 1775 | else // regular function |
1776 | { | 1776 | { |
1777 | DEBUGSPEW_CODE( fprintf( stderr, "FUNCTION %s\n", upName_)); | 1777 | DEBUGSPEW_CODE(fprintf( stderr, "FUNCTION %s\n", upName_)); |
1778 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 1778 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1779 | copy_cached_func( U, L2, L2_cache_i, L, source_i_, mode_, upName_); // ... f | 1779 | copy_cached_func( U, L2, L2_cache_i, L, source_i_, mode_, upName_); // ... f |
1780 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 1780 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
1781 | } | 1781 | } |
1782 | STACK_CHECK( L2, 1); | 1782 | STACK_CHECK( L2, 1); |
1783 | STACK_CHECK( L, 0); | 1783 | STACK_CHECK( L, 0); |
@@ -1868,7 +1868,7 @@ bool inter_copy_one(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, in | |||
1868 | STACK_CHECK_START_REL(L2, 0); // L // L2 | 1868 | STACK_CHECK_START_REL(L2, 0); // L // L2 |
1869 | 1869 | ||
1870 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "inter_copy_one()\n" INDENT_END)); | 1870 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "inter_copy_one()\n" INDENT_END)); |
1871 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 1871 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1872 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%s %s: " INDENT_END, lua_type_names[val_type], vt_names[static_cast<int>(vt_)])); | 1872 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%s %s: " INDENT_END, lua_type_names[val_type], vt_names[static_cast<int>(vt_)])); |
1873 | 1873 | ||
1874 | // Non-POD can be skipped if its metatable contains { __lanesignore = true } | 1874 | // Non-POD can be skipped if its metatable contains { __lanesignore = true } |
@@ -1967,7 +1967,7 @@ bool inter_copy_one(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, in | |||
1967 | break; | 1967 | break; |
1968 | } | 1968 | } |
1969 | 1969 | ||
1970 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 1970 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
1971 | 1971 | ||
1972 | STACK_CHECK( L2, ret ? 1 : 0); | 1972 | STACK_CHECK( L2, ret ? 1 : 0); |
1973 | STACK_CHECK( L, 0); | 1973 | STACK_CHECK( L, 0); |
@@ -1991,13 +1991,13 @@ int luaG_inter_copy(Universe* U, lua_State* L, lua_State* L2, int n, LookupMode | |||
1991 | bool copyok{ true }; | 1991 | bool copyok{ true }; |
1992 | 1992 | ||
1993 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_inter_copy()\n" INDENT_END)); | 1993 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_inter_copy()\n" INDENT_END)); |
1994 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 1994 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
1995 | 1995 | ||
1996 | if( n > top_L) | 1996 | if( n > top_L) |
1997 | { | 1997 | { |
1998 | // requesting to copy more than is available? | 1998 | // requesting to copy more than is available? |
1999 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END)); | 1999 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END)); |
2000 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 2000 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
2001 | return -1; | 2001 | return -1; |
2002 | } | 2002 | } |
2003 | 2003 | ||
@@ -2026,7 +2026,7 @@ int luaG_inter_copy(Universe* U, lua_State* L, lua_State* L2, int n, LookupMode | |||
2026 | } | 2026 | } |
2027 | STACK_CHECK( L, 0); | 2027 | STACK_CHECK( L, 0); |
2028 | 2028 | ||
2029 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 2029 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
2030 | 2030 | ||
2031 | if( copyok) | 2031 | if( copyok) |
2032 | { | 2032 | { |
@@ -2051,54 +2051,57 @@ int luaG_inter_move(Universe* U, lua_State* L, lua_State* L2, int n, LookupMode | |||
2051 | return ret; | 2051 | return ret; |
2052 | } | 2052 | } |
2053 | 2053 | ||
2054 | int luaG_inter_copy_package( Universe* U, lua_State* L, lua_State* L2, int package_idx_, LookupMode mode_) | 2054 | int luaG_inter_copy_package(Universe* U, lua_State* L, lua_State* L2, int package_idx_, LookupMode mode_) |
2055 | { | 2055 | { |
2056 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END)); | 2056 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END)); |
2057 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 2057 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
2058 | // package | 2058 | // package |
2059 | STACK_CHECK_START_REL(L, 0); | 2059 | STACK_CHECK_START_REL(L, 0); |
2060 | STACK_CHECK_START_REL(L2, 0); | 2060 | STACK_CHECK_START_REL(L2, 0); |
2061 | package_idx_ = lua_absindex( L, package_idx_); | 2061 | package_idx_ = lua_absindex(L, package_idx_); |
2062 | if( lua_type( L, package_idx_) != LUA_TTABLE) | 2062 | if (lua_type(L, package_idx_) != LUA_TTABLE) |
2063 | { | 2063 | { |
2064 | lua_pushfstring( L, "expected package as table, got %s", luaL_typename( L, package_idx_)); | 2064 | lua_pushfstring(L, "expected package as table, got %s", luaL_typename(L, package_idx_)); |
2065 | STACK_CHECK( L, 1); | 2065 | STACK_CHECK(L, 1); |
2066 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later | 2066 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later |
2067 | return (mode_ == LookupMode::LaneBody) ? lua_error(L) : 1; | 2067 | return (mode_ == LookupMode::LaneBody) ? lua_error(L) : 1; |
2068 | } | 2068 | } |
2069 | lua_getglobal( L2, "package"); | 2069 | lua_getglobal(L2, "package"); |
2070 | if( !lua_isnil( L2, -1)) // package library not loaded: do nothing | 2070 | if (!lua_isnil(L2, -1)) // package library not loaded: do nothing |
2071 | { | 2071 | { |
2072 | int i; | ||
2073 | // package.loaders is renamed package.searchers in Lua 5.2 | 2072 | // package.loaders is renamed package.searchers in Lua 5.2 |
2074 | // but don't copy it anyway, as the function names change depending on the slot index! | 2073 | // but don't copy it anyway, as the function names change depending on the slot index! |
2075 | // users should provide an on_state_create function to setup custom loaders instead | 2074 | // users should provide an on_state_create function to setup custom loaders instead |
2076 | // don't copy package.preload in keeper states (they don't know how to translate functions) | 2075 | // don't copy package.preload in keeper states (they don't know how to translate functions) |
2077 | char const* entries[] = { "path", "cpath", (mode_ == LookupMode::LaneBody) ? "preload" : nullptr /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, nullptr }; | 2076 | char const* entries[] = { "path", "cpath", (mode_ == LookupMode::LaneBody) ? "preload" : nullptr /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, nullptr }; |
2078 | for( i = 0; entries[i]; ++ i) | 2077 | for (char const* const entry : entries) |
2079 | { | 2078 | { |
2080 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "package.%s\n" INDENT_END, entries[i])); | 2079 | if (!entry) |
2081 | lua_getfield( L, package_idx_, entries[i]); | 2080 | { |
2082 | if( lua_isnil( L, -1)) | 2081 | continue; |
2082 | } | ||
2083 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END, entry)); | ||
2084 | lua_getfield(L, package_idx_, entry); | ||
2085 | if (lua_isnil(L, -1)) | ||
2083 | { | 2086 | { |
2084 | lua_pop( L, 1); | 2087 | lua_pop(L, 1); |
2085 | } | 2088 | } |
2086 | else | 2089 | else |
2087 | { | 2090 | { |
2088 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 2091 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); |
2089 | luaG_inter_move( U, L, L2, 1, mode_); // moves the entry to L2 | 2092 | luaG_inter_move(U, L, L2, 1, mode_); // moves the entry to L2 |
2090 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 2093 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
2091 | lua_setfield( L2, -2, entries[i]); // set package[entries[i]] | 2094 | lua_setfield(L2, -2, entry); // set package[entry] |
2092 | } | 2095 | } |
2093 | } | 2096 | } |
2094 | } | 2097 | } |
2095 | else | 2098 | else |
2096 | { | 2099 | { |
2097 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "'package' not loaded, nothing to do\n" INDENT_END)); | 2100 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "'package' not loaded, nothing to do\n" INDENT_END)); |
2098 | } | 2101 | } |
2099 | lua_pop( L2, 1); | 2102 | lua_pop(L2, 1); |
2100 | STACK_CHECK( L2, 0); | 2103 | STACK_CHECK(L2, 0); |
2101 | STACK_CHECK( L, 0); | 2104 | STACK_CHECK(L, 0); |
2102 | DEBUGSPEW_CODE( -- U->debugspew_indent_depth); | 2105 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); |
2103 | return 0; | 2106 | return 0; |
2104 | } | 2107 | } |