aboutsummaryrefslogtreecommitdiff
path: root/src/tools.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools.cpp')
-rw-r--r--src/tools.cpp85
1 files changed, 38 insertions, 47 deletions
diff --git a/src/tools.cpp b/src/tools.cpp
index ac5f7c5..07f9ae6 100644
--- a/src/tools.cpp
+++ b/src/tools.cpp
@@ -31,24 +31,12 @@ THE SOFTWARE.
31=============================================================================== 31===============================================================================
32*/ 32*/
33 33
34#include <stdio.h>
35#include <assert.h>
36#include <string.h>
37#include <ctype.h>
38#include <stdlib.h>
39#if !defined(__APPLE__)
40#include <malloc.h>
41#endif // __APPLE__
42
43#include "tools.h" 34#include "tools.h"
44#include "compat.h" 35
45#include "universe.h" 36#include "universe.h"
46#include "keeper.h"
47#include "lanes.h"
48#include "uniquekey.h"
49 37
50// functions implemented in deep.c 38// functions implemented in deep.c
51extern bool copydeep(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, LookupMode mode_, char const* upName_); 39extern bool copydeep(Universe* U, Dest L2, int L2_cache_i, Source L, int i, LookupMode mode_, char const* upName_);
52extern void push_registry_subtable( lua_State* L, UniqueKey key_); 40extern void push_registry_subtable( lua_State* L, UniqueKey key_);
53 41
54DEBUGSPEW_CODE( char const* debugspew_indent = "----+----!----+----!----+----!----+----!----+----!----+----!----+----!----+"); 42DEBUGSPEW_CODE( char const* debugspew_indent = "----+----!----+----!----+----!----+----!----+----!----+----!----+----!----+");
@@ -554,10 +542,10 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U)
554/* 542/*
555 * create a "fully.qualified.name" <-> function equivalence database 543 * create a "fully.qualified.name" <-> function equivalence database
556 */ 544 */
557void populate_func_lookup_table( lua_State* L, int _i, char const* name_) 545void populate_func_lookup_table(lua_State* L, int i_, char const* name_)
558{ 546{
559 int const ctx_base = lua_gettop( L) + 1; 547 int const ctx_base = lua_gettop(L) + 1;
560 int const in_base = lua_absindex( L, _i); 548 int const in_base = lua_absindex(L, i_);
561 int start_depth = 0; 549 int start_depth = 0;
562 DEBUGSPEW_CODE( Universe* U = universe_get( L)); 550 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")); 551 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, name_ ? name_ : "nullptr"));
@@ -765,7 +753,7 @@ static char const* find_lookup_name(lua_State* L, int i, LookupMode mode_, char
765/* 753/*
766 * Push a looked-up table, or nothing if we found nothing 754 * Push a looked-up table, or nothing if we found nothing
767 */ 755 */
768static bool lookup_table(lua_State* L2, lua_State* L, int i, LookupMode mode_, char const* upName_) 756static bool lookup_table(Dest L2, Source L, int i, LookupMode mode_, char const* upName_)
769{ 757{
770 // get the name of the table we want to send 758 // get the name of the table we want to send
771 size_t len; 759 size_t len;
@@ -775,7 +763,7 @@ static bool lookup_table(lua_State* L2, lua_State* L, int i, LookupMode mode_, c
775 return false; 763 return false;
776 } 764 }
777 // push the equivalent table in the destination's stack, retrieved from the lookup table 765 // push the equivalent table in the destination's stack, retrieved from the lookup table
778 STACK_CHECK_START_REL(L2, 0); // L // L2 766 STACK_CHECK_START_REL(L2, 0); // L // L2
779 STACK_GROW( L2, 3); // up to 3 slots are necessary on error 767 STACK_GROW( L2, 3); // up to 3 slots are necessary on error
780 switch( mode_) 768 switch( mode_)
781 { 769 {
@@ -785,34 +773,34 @@ static bool lookup_table(lua_State* L2, lua_State* L, int i, LookupMode mode_, c
785 773
786 case LookupMode::ToKeeper: 774 case LookupMode::ToKeeper:
787 // push a sentinel closure that holds the lookup name as upvalue 775 // push a sentinel closure that holds the lookup name as upvalue
788 lua_pushlstring( L2, fqn, len); // "f.q.n" 776 lua_pushlstring(L2, fqn, len); // "f.q.n"
789 lua_pushcclosure( L2, table_lookup_sentinel, 1); // f 777 lua_pushcclosure(L2, table_lookup_sentinel, 1); // f
790 break; 778 break;
791 779
792 case LookupMode::LaneBody: 780 case LookupMode::LaneBody:
793 case LookupMode::FromKeeper: 781 case LookupMode::FromKeeper:
794 LOOKUP_REGKEY.pushValue(L2); // {} 782 LOOKUP_REGKEY.pushValue(L2); // {}
795 STACK_CHECK( L2, 1); 783 STACK_CHECK(L2, 1);
796 ASSERT_L( lua_istable( L2, -1)); 784 ASSERT_L(lua_istable(L2, -1));
797 lua_pushlstring( L2, fqn, len); // {} "f.q.n" 785 lua_pushlstring(L2, fqn, len); // {} "f.q.n"
798 lua_rawget( L2, -2); // {} t 786 lua_rawget(L2, -2); // {} t
799 // we accept destination lookup failures in the case of transfering the Lanes body function (this will result in the source table being cloned instead) 787 // we accept destination lookup failures in the case of transfering the Lanes body function (this will result in the source table being cloned instead)
800 // but not when we extract something out of a keeper, as there is nothing to clone! 788 // but not when we extract something out of a keeper, as there is nothing to clone!
801 if (lua_isnil(L2, -1) && mode_ == LookupMode::LaneBody) 789 if (lua_isnil(L2, -1) && mode_ == LookupMode::LaneBody)
802 { 790 {
803 lua_pop( L2, 2); // 791 lua_pop(L2, 2); //
804 STACK_CHECK( L2, 0); 792 STACK_CHECK(L2, 0);
805 return false; 793 return false;
806 } 794 }
807 else if( !lua_istable( L2, -1)) 795 else if( !lua_istable(L2, -1))
808 { 796 {
809 char const* from, *to; 797 char const* from, *to;
810 lua_getglobal( L, "decoda_name"); // ... t ... decoda_name 798 lua_getglobal(L, "decoda_name"); // ... t ... decoda_name
811 from = lua_tostring( L, -1); 799 from = lua_tostring(L, -1);
812 lua_pop( L, 1); // ... t ... 800 lua_pop(L, 1); // ... t ...
813 lua_getglobal( L2, "decoda_name"); // {} t decoda_name 801 lua_getglobal(L2, "decoda_name"); // {} t decoda_name
814 to = lua_tostring( L2, -1); 802 to = lua_tostring( L2, -1);
815 lua_pop( L2, 1); // {} t 803 lua_pop(L2, 1); // {} t
816 // when mode_ == LookupMode::FromKeeper, L is a keeper state and L2 is not, therefore L2 is the state where we want to raise the error 804 // when mode_ == LookupMode::FromKeeper, L is a keeper state and L2 is not, therefore L2 is the state where we want to raise the error
817 (void) luaL_error( 805 (void) luaL_error(
818 (mode_ == LookupMode::FromKeeper) ? L2 : L 806 (mode_ == LookupMode::FromKeeper) ? L2 : L
@@ -823,7 +811,7 @@ static bool lookup_table(lua_State* L2, lua_State* L, int i, LookupMode mode_, c
823 ); 811 );
824 return false; 812 return false;
825 } 813 }
826 lua_remove( L2, -2); // t 814 lua_remove(L2, -2); // t
827 break; 815 break;
828 } 816 }
829 STACK_CHECK( L2, 1); 817 STACK_CHECK( L2, 1);
@@ -1194,7 +1182,7 @@ static int buf_writer( lua_State* L, void const* b, size_t size, void* ud)
1194 1182
1195// ################################################################################################# 1183// #################################################################################################
1196 1184
1197static void copy_func(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, LookupMode mode_, char const* upName_) 1185static void copy_func(Universe* U, Dest L2, int L2_cache_i, Source L, int i, LookupMode mode_, char const* upName_)
1198{ 1186{
1199 int n, needToPush; 1187 int n, needToPush;
1200 luaL_Buffer B; 1188 luaL_Buffer B;
@@ -1348,7 +1336,7 @@ static void copy_func(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L,
1348 * 1336 *
1349 * Always pushes a function to 'L2'. 1337 * Always pushes a function to 'L2'.
1350 */ 1338 */
1351static void copy_cached_func(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, LookupMode mode_, char const* upName_) 1339static void copy_cached_func(Universe* U, Dest L2, int L2_cache_i, Source L, int i, LookupMode mode_, char const* upName_)
1352{ 1340{
1353 FuncSubType funcSubType; 1341 FuncSubType funcSubType;
1354 /*lua_CFunction cfunc =*/ luaG_tocfunction( L, i, &funcSubType); // nullptr for LuaJIT-fast && bytecode functions 1342 /*lua_CFunction cfunc =*/ luaG_tocfunction( L, i, &funcSubType); // nullptr for LuaJIT-fast && bytecode functions
@@ -1403,7 +1391,7 @@ static void copy_cached_func(Universe* U, lua_State* L2, int L2_cache_i, lua_Sta
1403 1391
1404// ################################################################################################# 1392// #################################################################################################
1405 1393
1406static bool push_cached_metatable(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, LookupMode mode_, char const* upName_) 1394static bool push_cached_metatable(Universe* U, Dest L2, int L2_cache_i, Source L, int i, LookupMode mode_, char const* upName_)
1407{ 1395{
1408 STACK_CHECK_START_REL(L, 0); 1396 STACK_CHECK_START_REL(L, 0);
1409 if( lua_getmetatable( L, i)) // ... mt 1397 if( lua_getmetatable( L, i)) // ... mt
@@ -1454,7 +1442,7 @@ static bool push_cached_metatable(Universe* U, lua_State* L2, int L2_cache_i, lu
1454 1442
1455// ################################################################################################# 1443// #################################################################################################
1456 1444
1457static void inter_copy_keyvaluepair(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, VT vt_, LookupMode mode_, char const* upName_) 1445static void inter_copy_keyvaluepair(Universe* U, Dest L2, int L2_cache_i, Source L, VT vt_, LookupMode mode_, char const* upName_)
1458{ 1446{
1459 int val_i = lua_gettop(L); 1447 int val_i = lua_gettop(L);
1460 int key_i = val_i - 1; 1448 int key_i = val_i - 1;
@@ -1526,7 +1514,7 @@ static void inter_copy_keyvaluepair(Universe* U, lua_State* L2, int L2_cache_i,
1526*/ 1514*/
1527static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull }; 1515static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull };
1528 1516
1529static bool copyclone(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int source_i_, LookupMode mode_, char const* upName_) 1517static bool copyclone(Universe* U, Dest L2, int L2_cache_i, Source L, int source_i_, LookupMode mode_, char const* upName_)
1530{ 1518{
1531 void* const source = lua_touserdata( L, source_i_); 1519 void* const source = lua_touserdata( L, source_i_);
1532 source_i_ = lua_absindex( L, source_i_); 1520 source_i_ = lua_absindex( L, source_i_);
@@ -1641,7 +1629,7 @@ static bool copyclone(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L,
1641 1629
1642// ################################################################################################# 1630// #################################################################################################
1643 1631
1644static bool inter_copy_userdata(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, VT vt_, LookupMode mode_, char const* upName_) 1632static bool inter_copy_userdata(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_)
1645{ 1633{
1646 STACK_CHECK_START_REL(L, 0); 1634 STACK_CHECK_START_REL(L, 0);
1647 STACK_CHECK_START_REL(L2, 0); 1635 STACK_CHECK_START_REL(L2, 0);
@@ -1691,7 +1679,7 @@ static bool inter_copy_userdata(Universe* U, lua_State* L2, int L2_cache_i, lua_
1691 1679
1692// ################################################################################################# 1680// #################################################################################################
1693 1681
1694static bool inter_copy_function(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int source_i_, VT vt_, LookupMode mode_, char const* upName_) 1682static bool inter_copy_function(Universe* U, Dest L2, int L2_cache_i, Source L, int source_i_, VT vt_, LookupMode mode_, char const* upName_)
1695{ 1683{
1696 if (vt_ == VT::KEY) 1684 if (vt_ == VT::KEY)
1697 { 1685 {
@@ -1786,7 +1774,7 @@ static bool inter_copy_function(Universe* U, lua_State* L2, int L2_cache_i, lua_
1786 1774
1787// ################################################################################################# 1775// #################################################################################################
1788 1776
1789static bool inter_copy_table(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, VT vt_, LookupMode mode_, char const* upName_) 1777static bool inter_copy_table(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_)
1790{ 1778{
1791 if (vt_ == VT::KEY) 1779 if (vt_ == VT::KEY)
1792 { 1780 {
@@ -1858,7 +1846,7 @@ static bool inter_copy_table(Universe* U, lua_State* L2, int L2_cache_i, lua_Sta
1858* 1846*
1859* Returns true if value was pushed, false if its type is non-supported. 1847* Returns true if value was pushed, false if its type is non-supported.
1860*/ 1848*/
1861bool inter_copy_one(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, VT vt_, LookupMode mode_, char const* upName_) 1849bool inter_copy_one(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_)
1862{ 1850{
1863 bool ret{ true }; 1851 bool ret{ true };
1864 int val_type = lua_type( L, i); 1852 int val_type = lua_type( L, i);
@@ -1974,6 +1962,8 @@ bool inter_copy_one(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, in
1974 return ret; 1962 return ret;
1975} 1963}
1976 1964
1965// #################################################################################################
1966
1977/* 1967/*
1978* Akin to 'lua_xmove' but copies values between _any_ Lua states. 1968* Akin to 'lua_xmove' but copies values between _any_ Lua states.
1979* 1969*
@@ -1981,7 +1971,7 @@ bool inter_copy_one(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, in
1981* 1971*
1982* Note: Parameters are in this order ('L' = from first) to be same as 'lua_xmove'. 1972* Note: Parameters are in this order ('L' = from first) to be same as 'lua_xmove'.
1983*/ 1973*/
1984int luaG_inter_copy(Universe* U, lua_State* L, lua_State* L2, int n, LookupMode mode_) 1974int luaG_inter_copy(Universe* U, Source L, Dest L2, int n, LookupMode mode_)
1985{ 1975{
1986 int top_L = lua_gettop(L); // ... {}n 1976 int top_L = lua_gettop(L); // ... {}n
1987 int top_L2 = lua_gettop(L2); // ... 1977 int top_L2 = lua_gettop(L2); // ...
@@ -2043,15 +2033,16 @@ int luaG_inter_copy(Universe* U, lua_State* L, lua_State* L2, int n, LookupMode
2043 return -2; 2033 return -2;
2044} 2034}
2045 2035
2036// #################################################################################################
2046 2037
2047int luaG_inter_move(Universe* U, lua_State* L, lua_State* L2, int n, LookupMode mode_) 2038int luaG_inter_move(Universe* U, Source L, Dest L2, int n, LookupMode mode_)
2048{ 2039{
2049 int ret = luaG_inter_copy( U, L, L2, n, mode_); 2040 int ret = luaG_inter_copy( U, L, L2, n, mode_);
2050 lua_pop( L, (int) n); 2041 lua_pop( L, (int) n);
2051 return ret; 2042 return ret;
2052} 2043}
2053 2044
2054int luaG_inter_copy_package(Universe* U, lua_State* L, lua_State* L2, int package_idx_, LookupMode mode_) 2045int luaG_inter_copy_package(Universe* U, Source L, Dest L2, int package_idx_, LookupMode mode_)
2055{ 2046{
2056 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END)); 2047 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END));
2057 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); 2048 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));