aboutsummaryrefslogtreecommitdiff
path: root/src/tools.c
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-04-11 15:14:52 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-04-11 15:14:52 +0200
commitadaa36dbec1ce9aaafd61873b9d3d898a8c240cf (patch)
tree4c81e8f5983c3d696a636e2cc433ce7c0a9c3dd8 /src/tools.c
parent1d310e6ecb6e156598337612f16573d9cd284f5e (diff)
downloadlanes-adaa36dbec1ce9aaafd61873b9d3d898a8c240cf.tar.gz
lanes-adaa36dbec1ce9aaafd61873b9d3d898a8c240cf.tar.bz2
lanes-adaa36dbec1ce9aaafd61873b9d3d898a8c240cf.zip
Bring all interesting fixes from the C++ implementation back into the C implementation
Diffstat (limited to 'src/tools.c')
-rw-r--r--src/tools.c50
1 files changed, 33 insertions, 17 deletions
diff --git a/src/tools.c b/src/tools.c
index 80e0f71..c43d8a2 100644
--- a/src/tools.c
+++ b/src/tools.c
@@ -242,8 +242,14 @@ void initialize_allocator_function( Universe* U, lua_State* L)
242 U->internal_allocator.allocF = libc_lua_Alloc; 242 U->internal_allocator.allocF = libc_lua_Alloc;
243 U->internal_allocator.allocUD = NULL; 243 U->internal_allocator.allocUD = NULL;
244 } 244 }
245 else if (U->provide_allocator == luaG_provide_protected_allocator)
246 {
247 // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case.
248 U->internal_allocator.allocF = lua_getallocf(L, &U->internal_allocator.allocUD);
249 }
245 else 250 else
246 { 251 {
252 // no protection required, just use whatever we have as-is.
247 U->internal_allocator = U->protected_allocator.definition; 253 U->internal_allocator = U->protected_allocator.definition;
248 } 254 }
249 } 255 }
@@ -844,8 +850,8 @@ static bool_t lookup_table( lua_State* L2, lua_State* L, uint_t i, LookupMode mo
844 */ 850 */
845static bool_t push_cached_table( lua_State* L2, uint_t L2_cache_i, lua_State* L, uint_t i) 851static bool_t push_cached_table( lua_State* L2, uint_t L2_cache_i, lua_State* L, uint_t i)
846{ 852{
847 bool_t not_found_in_cache; // L2 853 bool_t not_found_in_cache; // L2
848 DECLARE_CONST_UNIQUE_KEY( p, lua_topointer( L, i)); 854 void const* p = lua_topointer( L, i);
849 855
850 ASSERT_L( L2_cache_i != 0); 856 ASSERT_L( L2_cache_i != 0);
851 STACK_GROW( L2, 3); 857 STACK_GROW( L2, 3);
@@ -854,17 +860,17 @@ static bool_t push_cached_table( lua_State* L2, uint_t L2_cache_i, lua_State* L,
854 // We don't need to use the from state ('L') in ID since the life span 860 // We don't need to use the from state ('L') in ID since the life span
855 // is only for the duration of a copy (both states are locked). 861 // is only for the duration of a copy (both states are locked).
856 // push a light userdata uniquely representing the table 862 // push a light userdata uniquely representing the table
857 push_unique_key( L2, p); // ... p 863 lua_pushlightuserdata( L2, (void*) p); // ... p
858 864
859 //fprintf( stderr, "<< ID: %s >>\n", lua_tostring( L2, -1)); 865 //fprintf( stderr, "<< ID: %s >>\n", lua_tostring( L2, -1));
860 866
861 lua_rawget( L2, L2_cache_i); // ... {cached|nil} 867 lua_rawget( L2, L2_cache_i); // ... {cached|nil}
862 not_found_in_cache = lua_isnil( L2, -1); 868 not_found_in_cache = lua_isnil( L2, -1);
863 if( not_found_in_cache) 869 if( not_found_in_cache)
864 { 870 {
865 lua_pop( L2, 1); // ... 871 lua_pop( L2, 1); // ...
866 lua_newtable( L2); // ... {} 872 lua_newtable( L2); // ... {}
867 push_unique_key( L2, p); // ... {} p 873 lua_pushlightuserdata( L2, (void*) p); // ... {} p
868 lua_pushvalue( L2, -2); // ... {} p {} 874 lua_pushvalue( L2, -2); // ... {} p {}
869 lua_rawset( L2, L2_cache_i); // ... {} 875 lua_rawset( L2, L2_cache_i); // ... {}
870 } 876 }
@@ -1446,7 +1452,7 @@ static void inter_copy_keyvaluepair( Universe* U, lua_State* L2, uint_t L2_cache
1446 uint_t key_i = val_i - 1; 1452 uint_t key_i = val_i - 1;
1447 1453
1448 // Only basic key types are copied over; others ignored 1454 // Only basic key types are copied over; others ignored
1449 if( inter_copy_one( U, L2, 0 /*key*/, L, key_i, VT_KEY, mode_, upName_)) 1455 if( inter_copy_one( U, L2, L2_cache_i, L, key_i, VT_KEY, mode_, upName_))
1450 { 1456 {
1451 char* valPath = (char*) upName_; 1457 char* valPath = (char*) upName_;
1452 if( U->verboseErrors) 1458 if( U->verboseErrors)
@@ -1596,7 +1602,10 @@ static bool_t copyclone( Universe* U, lua_State* L2, uint_t L2_cache_i, lua_Stat
1596 // assign uservalues 1602 // assign uservalues
1597 while( uvi > 0) 1603 while( uvi > 0)
1598 { 1604 {
1599 inter_copy_one( U, L2, L2_cache_i, L, lua_absindex( L, -1), VT_NORMAL, mode_, upName_); // ... u uv 1605 if(!inter_copy_one( U, L2, L2_cache_i, L, lua_absindex( L, -1), VT_NORMAL, mode_, upName_)) // ... u uv
1606 {
1607 (void) luaL_error(L, "Cannot copy upvalue type '%s'", luaL_typename(L, -1));
1608 }
1600 lua_pop( L, 1); // ... mt __lanesclone [uv]* 1609 lua_pop( L, 1); // ... mt __lanesclone [uv]*
1601 // this pops the value from the stack 1610 // this pops the value from the stack
1602 lua_setiuservalue( L2, -2, uvi); // ... u 1611 lua_setiuservalue( L2, -2, uvi); // ... u
@@ -1730,7 +1739,10 @@ static bool_t inter_copy_function( Universe* U, lua_State* L2, uint_t L2_cache_i
1730 // transfer and assign uservalues 1739 // transfer and assign uservalues
1731 while( uvi > 0) 1740 while( uvi > 0)
1732 { 1741 {
1733 inter_copy_one( U, L2, L2_cache_i, L, lua_absindex( L, -1), vt, mode_, upName_); // ... mt u uv 1742 if(!inter_copy_one( U, L2, L2_cache_i, L, lua_absindex( L, -1), vt, mode_, upName_)) // ... mt u uv
1743 {
1744 (void) luaL_error(L, "Cannot copy upvalue type '%s'", luaL_typename(L, -1));
1745 }
1734 lua_pop( L, 1); // ... u [uv]* 1746 lua_pop( L, 1); // ... u [uv]*
1735 // this pops the value from the stack 1747 // this pops the value from the stack
1736 lua_setiuservalue( L2, -2, uvi); // ... mt u 1748 lua_setiuservalue( L2, -2, uvi); // ... mt u
@@ -1957,7 +1969,7 @@ bool_t inter_copy_one( Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State*
1957* 1969*
1958* Note: Parameters are in this order ('L' = from first) to be same as 'lua_xmove'. 1970* Note: Parameters are in this order ('L' = from first) to be same as 'lua_xmove'.
1959*/ 1971*/
1960int luaG_inter_copy( Universe* U, lua_State* L, lua_State* L2, uint_t n, LookupMode mode_) 1972InterCopyResult luaG_inter_copy( Universe* U, lua_State* L, lua_State* L2, uint_t n, LookupMode mode_)
1961{ 1973{
1962 uint_t top_L = lua_gettop( L); // ... {}n 1974 uint_t top_L = lua_gettop( L); // ... {}n
1963 uint_t top_L2 = lua_gettop( L2); // ... 1975 uint_t top_L2 = lua_gettop( L2); // ...
@@ -1974,7 +1986,7 @@ int luaG_inter_copy( Universe* U, lua_State* L, lua_State* L2, uint_t n, LookupM
1974 // requesting to copy more than is available? 1986 // requesting to copy more than is available?
1975 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END)); 1987 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END));
1976 DEBUGSPEW_CODE( -- U->debugspew_indent_depth); 1988 DEBUGSPEW_CODE( -- U->debugspew_indent_depth);
1977 return -1; 1989 return eICR_NotEnoughValues;
1978 } 1990 }
1979 1991
1980 STACK_CHECK( L2, 0); 1992 STACK_CHECK( L2, 0);
@@ -2010,24 +2022,24 @@ int luaG_inter_copy( Universe* U, lua_State* L, lua_State* L2, uint_t n, LookupM
2010 // Remove the cache table. Persistent caching would cause i.e. multiple 2022 // Remove the cache table. Persistent caching would cause i.e. multiple
2011 // messages passed in the same table to use the same table also in receiving end. 2023 // messages passed in the same table to use the same table also in receiving end.
2012 lua_remove( L2, top_L2 + 1); 2024 lua_remove( L2, top_L2 + 1);
2013 return 0; 2025 return eICR_Success;
2014 } 2026 }
2015 2027
2016 // error -> pop everything from the target state stack 2028 // error -> pop everything from the target state stack
2017 lua_settop( L2, top_L2); 2029 lua_settop( L2, top_L2);
2018 STACK_END( L2, 0); 2030 STACK_END( L2, 0);
2019 return -2; 2031 return eICR_Error;
2020} 2032}
2021 2033
2022 2034
2023int luaG_inter_move( Universe* U, lua_State* L, lua_State* L2, uint_t n, LookupMode mode_) 2035InterCopyResult luaG_inter_move( Universe* U, lua_State* L, lua_State* L2, uint_t n, LookupMode mode_)
2024{ 2036{
2025 int ret = luaG_inter_copy( U, L, L2, n, mode_); 2037 InterCopyResult ret = luaG_inter_copy( U, L, L2, n, mode_);
2026 lua_pop( L, (int) n); 2038 lua_pop( L, (int) n);
2027 return ret; 2039 return ret;
2028} 2040}
2029 2041
2030int luaG_inter_copy_package( Universe* U, lua_State* L, lua_State* L2, int package_idx_, LookupMode mode_) 2042InterCopyResult luaG_inter_copy_package( Universe* U, lua_State* L, lua_State* L2, int package_idx_, LookupMode mode_)
2031{ 2043{
2032 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END)); 2044 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END));
2033 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); 2045 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth);
@@ -2040,7 +2052,11 @@ int luaG_inter_copy_package( Universe* U, lua_State* L, lua_State* L2, int packa
2040 lua_pushfstring( L, "expected package as table, got %s", luaL_typename( L, package_idx_)); 2052 lua_pushfstring( L, "expected package as table, got %s", luaL_typename( L, package_idx_));
2041 STACK_MID( L, 1); 2053 STACK_MID( L, 1);
2042 // raise the error when copying from lane to lane, else just leave it on the stack to be raised later 2054 // raise the error when copying from lane to lane, else just leave it on the stack to be raised later
2043 return ( mode_ == eLM_LaneBody) ? lua_error( L) : 1; 2055 if (mode_ == eLM_LaneBody)
2056 {
2057 lua_error(L); // doesn't return
2058 }
2059 return eICR_Error;
2044 } 2060 }
2045 lua_getglobal( L2, "package"); 2061 lua_getglobal( L2, "package");
2046 if( !lua_isnil( L2, -1)) // package library not loaded: do nothing 2062 if( !lua_isnil( L2, -1)) // package library not loaded: do nothing
@@ -2076,5 +2092,5 @@ int luaG_inter_copy_package( Universe* U, lua_State* L, lua_State* L2, int packa
2076 STACK_END( L2, 0); 2092 STACK_END( L2, 0);
2077 STACK_END( L, 0); 2093 STACK_END( L, 0);
2078 DEBUGSPEW_CODE( -- U->debugspew_indent_depth); 2094 DEBUGSPEW_CODE( -- U->debugspew_indent_depth);
2079 return 0; 2095 return eICR_Success;
2080} 2096}