diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-04-29 15:55:54 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-04-29 15:55:54 +0200 |
commit | d60a9fb712886880ec9630e744e1258ec3ed19b1 (patch) | |
tree | 068bc002f88c2f555d64218e0db6fb6f7e671d49 /src | |
parent | dd4a6ece589ca49130f2adf29d38af678f1640a9 (diff) | |
download | lanes-d60a9fb712886880ec9630e744e1258ec3ed19b1.tar.gz lanes-d60a9fb712886880ec9630e744e1258ec3ed19b1.tar.bz2 lanes-d60a9fb712886880ec9630e744e1258ec3ed19b1.zip |
Progressively applying the coding rules
Diffstat (limited to 'src')
-rw-r--r-- | src/cancel.cpp | 86 | ||||
-rw-r--r-- | src/cancel.h | 4 | ||||
-rw-r--r-- | src/compat.cpp | 38 | ||||
-rw-r--r-- | src/compat.h | 132 | ||||
-rw-r--r-- | src/deep.cpp | 176 | ||||
-rw-r--r-- | src/keeper.cpp | 58 | ||||
-rw-r--r-- | src/keeper.h | 4 | ||||
-rw-r--r-- | src/lanes.cpp | 50 | ||||
-rw-r--r-- | src/macros_and_utils.h | 12 | ||||
-rw-r--r-- | src/state.cpp | 16 | ||||
-rw-r--r-- | src/tools.cpp | 12 |
11 files changed, 321 insertions, 267 deletions
diff --git a/src/cancel.cpp b/src/cancel.cpp index 82c6def..e80c0b5 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp | |||
@@ -43,14 +43,14 @@ THE SOFTWARE. | |||
43 | // ################################################################################################# | 43 | // ################################################################################################# |
44 | 44 | ||
45 | /* | 45 | /* |
46 | * Check if the thread in question ('L') has been signalled for cancel. | 46 | * Check if the thread in question ('L') has been signalled for cancel. |
47 | * | 47 | * |
48 | * Called by cancellation hooks and/or pending Linda operations (because then | 48 | * Called by cancellation hooks and/or pending Linda operations (because then |
49 | * the check won't affect performance). | 49 | * the check won't affect performance). |
50 | * | 50 | * |
51 | * Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'raise_cancel_error()' called, | 51 | * Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'raise_cancel_error()' called, |
52 | * to make execution of the lane end. | 52 | * to make execution of the lane end. |
53 | */ | 53 | */ |
54 | [[nodiscard]] static inline CancelRequest cancel_test(lua_State* L_) | 54 | [[nodiscard]] static inline CancelRequest cancel_test(lua_State* L_) |
55 | { | 55 | { |
56 | Lane* const lane{ kLanePointerRegKey.readLightUserDataValue<Lane>(L_) }; | 56 | Lane* const lane{ kLanePointerRegKey.readLightUserDataValue<Lane>(L_) }; |
@@ -76,11 +76,10 @@ LUAG_FUNC(cancel_test) | |||
76 | // ################################################################################################# | 76 | // ################################################################################################# |
77 | // ################################################################################################# | 77 | // ################################################################################################# |
78 | 78 | ||
79 | [[nodiscard]] static void cancel_hook(lua_State* L_, [[maybe_unused]] lua_Debug* ar) | 79 | [[nodiscard]] static void cancel_hook(lua_State* L_, [[maybe_unused]] lua_Debug* ar_) |
80 | { | 80 | { |
81 | DEBUGSPEW_CODE(fprintf(stderr, "cancel_hook\n")); | 81 | DEBUGSPEW_CODE(fprintf(stderr, "cancel_hook\n")); |
82 | if (cancel_test(L_) != CancelRequest::None) | 82 | if (cancel_test(L_) != CancelRequest::None) { |
83 | { | ||
84 | lua_sethook(L_, nullptr, 0, 0); | 83 | lua_sethook(L_, nullptr, 0, 0); |
85 | raise_cancel_error(L_); | 84 | raise_cancel_error(L_); |
86 | } | 85 | } |
@@ -108,15 +107,14 @@ LUAG_FUNC(cancel_test) | |||
108 | 107 | ||
109 | // ################################################################################################# | 108 | // ################################################################################################# |
110 | 109 | ||
111 | [[nodiscard]] static CancelResult thread_cancel_soft(Lane* lane_, lua_Duration duration_, bool wake_lane_) | 110 | [[nodiscard]] static CancelResult thread_cancel_soft(Lane* lane_, lua_Duration duration_, bool wakeLane_) |
112 | { | 111 | { |
113 | lane_->cancel_request = CancelRequest::Soft; // it's now signaled to stop | 112 | lane_->cancel_request = CancelRequest::Soft; // it's now signaled to stop |
114 | // negative timeout: we don't want to truly abort the lane, we just want it to react to cancel_test() on its own | 113 | // negative timeout: we don't want to truly abort the lane, we just want it to react to cancel_test() on its own |
115 | if (wake_lane_) // wake the thread so that execution returns from any pending linda operation if desired | 114 | if (wakeLane_) // wake the thread so that execution returns from any pending linda operation if desired |
116 | { | 115 | { |
117 | std::condition_variable* const waiting_on{ lane_->m_waiting_on }; | 116 | std::condition_variable* const waiting_on{ lane_->m_waiting_on }; |
118 | if (lane_->m_status == Lane::Waiting && waiting_on != nullptr) | 117 | if (lane_->m_status == Lane::Waiting && waiting_on != nullptr) { |
119 | { | ||
120 | waiting_on->notify_all(); | 118 | waiting_on->notify_all(); |
121 | } | 119 | } |
122 | } | 120 | } |
@@ -126,15 +124,14 @@ LUAG_FUNC(cancel_test) | |||
126 | 124 | ||
127 | // ################################################################################################# | 125 | // ################################################################################################# |
128 | 126 | ||
129 | [[nodiscard]] static CancelResult thread_cancel_hard(Lane* lane_, lua_Duration duration_, bool wake_lane_) | 127 | [[nodiscard]] static CancelResult thread_cancel_hard(Lane* lane_, lua_Duration duration_, bool wakeLane_) |
130 | { | 128 | { |
131 | lane_->cancel_request = CancelRequest::Hard; // it's now signaled to stop | 129 | lane_->cancel_request = CancelRequest::Hard; // it's now signaled to stop |
132 | //lane_->m_thread.get_stop_source().request_stop(); | 130 | // lane_->m_thread.get_stop_source().request_stop(); |
133 | if (wake_lane_) // wake the thread so that execution returns from any pending linda operation if desired | 131 | if (wakeLane_) // wake the thread so that execution returns from any pending linda operation if desired |
134 | { | 132 | { |
135 | std::condition_variable* waiting_on = lane_->m_waiting_on; | 133 | std::condition_variable* waiting_on = lane_->m_waiting_on; |
136 | if (lane_->m_status == Lane::Waiting && waiting_on != nullptr) | 134 | if (lane_->m_status == Lane::Waiting && waiting_on != nullptr) { |
137 | { | ||
138 | waiting_on->notify_all(); | 135 | waiting_on->notify_all(); |
139 | } | 136 | } |
140 | } | 137 | } |
@@ -145,58 +142,43 @@ LUAG_FUNC(cancel_test) | |||
145 | 142 | ||
146 | // ################################################################################################# | 143 | // ################################################################################################# |
147 | 144 | ||
148 | CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hook_count_, lua_Duration duration_, bool wake_lane_) | 145 | CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hookCount_, lua_Duration duration_, bool wakeLane_) |
149 | { | 146 | { |
150 | // remember that lanes are not transferable: only one thread can cancel a lane, so no multithreading issue here | 147 | // remember that lanes are not transferable: only one thread can cancel a lane, so no multithreading issue here |
151 | // We can read 'lane_->status' without locks, but not wait for it (if Posix no PTHREAD_TIMEDJOIN) | 148 | // We can read 'lane_->status' without locks, but not wait for it (if Posix no PTHREAD_TIMEDJOIN) |
152 | if (lane_->m_status >= Lane::Done) | 149 | if (lane_->m_status >= Lane::Done) { |
153 | { | ||
154 | // say "ok" by default, including when lane is already done | 150 | // say "ok" by default, including when lane is already done |
155 | return CancelResult::Cancelled; | 151 | return CancelResult::Cancelled; |
156 | } | 152 | } |
157 | 153 | ||
158 | // signal the linda the wake up the thread so that it can react to the cancel query | 154 | // signal the linda the wake up the thread so that it can react to the cancel query |
159 | // let us hope we never land here with a pointer on a linda that has been destroyed... | 155 | // let us hope we never land here with a pointer on a linda that has been destroyed... |
160 | if (op_ == CancelOp::Soft) | 156 | if (op_ == CancelOp::Soft) { |
161 | { | 157 | return thread_cancel_soft(lane_, duration_, wakeLane_); |
162 | return thread_cancel_soft(lane_, duration_, wake_lane_); | 158 | } else if (static_cast<int>(op_) > static_cast<int>(CancelOp::Soft)) { |
163 | } | 159 | lua_sethook(lane_->L, cancel_hook, static_cast<int>(op_), hookCount_); |
164 | else if (static_cast<int>(op_) > static_cast<int>(CancelOp::Soft)) | ||
165 | { | ||
166 | lua_sethook(lane_->L, cancel_hook, static_cast<int>(op_), hook_count_); | ||
167 | } | 160 | } |
168 | 161 | ||
169 | return thread_cancel_hard(lane_, duration_, wake_lane_); | 162 | return thread_cancel_hard(lane_, duration_, wakeLane_); |
170 | } | 163 | } |
171 | 164 | ||
172 | // ################################################################################################# | 165 | // ################################################################################################# |
173 | // ################################################################################################# | 166 | // ################################################################################################# |
174 | 167 | ||
175 | CancelOp which_cancel_op(char const* op_string_) | 168 | CancelOp which_cancel_op(char const* opString_) |
176 | { | 169 | { |
177 | CancelOp op{ CancelOp::Invalid }; | 170 | CancelOp op{ CancelOp::Invalid }; |
178 | if (strcmp(op_string_, "hard") == 0) | 171 | if (strcmp(opString_, "hard") == 0) { |
179 | { | ||
180 | op = CancelOp::Hard; | 172 | op = CancelOp::Hard; |
181 | } | 173 | } else if (strcmp(opString_, "soft") == 0) { |
182 | else if (strcmp(op_string_, "soft") == 0) | ||
183 | { | ||
184 | op = CancelOp::Soft; | 174 | op = CancelOp::Soft; |
185 | } | 175 | } else if (strcmp(opString_, "call") == 0) { |
186 | else if (strcmp(op_string_, "call") == 0) | ||
187 | { | ||
188 | op = CancelOp::MaskCall; | 176 | op = CancelOp::MaskCall; |
189 | } | 177 | } else if (strcmp(opString_, "ret") == 0) { |
190 | else if (strcmp(op_string_, "ret") == 0) | ||
191 | { | ||
192 | op = CancelOp::MaskRet; | 178 | op = CancelOp::MaskRet; |
193 | } | 179 | } else if (strcmp(opString_, "line") == 0) { |
194 | else if (strcmp(op_string_, "line") == 0) | ||
195 | { | ||
196 | op = CancelOp::MaskLine; | 180 | op = CancelOp::MaskLine; |
197 | } | 181 | } else if (strcmp(opString_, "count") == 0) { |
198 | else if (strcmp(op_string_, "count") == 0) | ||
199 | { | ||
200 | op = CancelOp::MaskCount; | 182 | op = CancelOp::MaskCount; |
201 | } | 183 | } |
202 | return op; | 184 | return op; |
@@ -206,13 +188,11 @@ CancelOp which_cancel_op(char const* op_string_) | |||
206 | 188 | ||
207 | [[nodiscard]] static CancelOp which_cancel_op(lua_State* L_, int idx_) | 189 | [[nodiscard]] static CancelOp which_cancel_op(lua_State* L_, int idx_) |
208 | { | 190 | { |
209 | if (lua_type(L_, idx_) == LUA_TSTRING) | 191 | if (lua_type(L_, idx_) == LUA_TSTRING) { |
210 | { | ||
211 | char const* const str{ lua_tostring(L_, idx_) }; | 192 | char const* const str{ lua_tostring(L_, idx_) }; |
212 | CancelOp op{ which_cancel_op(str) }; | 193 | CancelOp op{ which_cancel_op(str) }; |
213 | lua_remove(L_, idx_); // argument is processed, remove it | 194 | lua_remove(L_, idx_); // argument is processed, remove it |
214 | if (op == CancelOp::Invalid) | 195 | if (op == CancelOp::Invalid) { |
215 | { | ||
216 | raise_luaL_error(L_, "invalid hook option %s", str); | 196 | raise_luaL_error(L_, "invalid hook option %s", str); |
217 | } | 197 | } |
218 | return op; | 198 | return op; |
diff --git a/src/cancel.h b/src/cancel.h index 59ebefe..3df5252 100644 --- a/src/cancel.h +++ b/src/cancel.h | |||
@@ -48,8 +48,8 @@ enum class CancelOp | |||
48 | // xxh64 of string "kCancelError" generated at https://www.pelock.com/products/hash-calculator | 48 | // xxh64 of string "kCancelError" generated at https://www.pelock.com/products/hash-calculator |
49 | static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel | 49 | static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel |
50 | 50 | ||
51 | [[nodiscard]] CancelOp which_cancel_op(char const* op_string_); | 51 | [[nodiscard]] CancelOp which_cancel_op(char const* opString_); |
52 | [[nodiscard]] CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hook_count_, lua_Duration secs_, bool wake_lindas_); | 52 | [[nodiscard]] CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hookCount_, lua_Duration secs_, bool wakeLane_); |
53 | 53 | ||
54 | [[noreturn]] static inline void raise_cancel_error(lua_State* L_) | 54 | [[noreturn]] static inline void raise_cancel_error(lua_State* L_) |
55 | { | 55 | { |
diff --git a/src/compat.cpp b/src/compat.cpp index 41a206a..591e19f 100644 --- a/src/compat.cpp +++ b/src/compat.cpp | |||
@@ -12,35 +12,35 @@ | |||
12 | // ################################################################################################# | 12 | // ################################################################################################# |
13 | 13 | ||
14 | // Copied from Lua 5.2 loadlib.c | 14 | // Copied from Lua 5.2 loadlib.c |
15 | static int luaL_getsubtable(lua_State* L_, int idx, const char* fname) | 15 | static int luaL_getsubtable(lua_State* L_, int idx_, const char* fname_) |
16 | { | 16 | { |
17 | lua_getfield(L_, idx, fname); | 17 | lua_getfield(L_, idx_, fname_); |
18 | if (lua_istable(L_, -1)) | 18 | if (lua_istable(L_, -1)) |
19 | return 1; /* table already there */ | 19 | return 1; /* table already there */ |
20 | else { | 20 | else { |
21 | lua_pop(L_, 1); /* remove previous result */ | 21 | lua_pop(L_, 1); /* remove previous result */ |
22 | idx = lua_absindex(L_, idx); | 22 | idx_ = lua_absindex(L_, idx_); |
23 | lua_newtable(L_); | 23 | lua_newtable(L_); |
24 | lua_pushvalue(L_, -1); /* copy to be left at top */ | 24 | lua_pushvalue(L_, -1); /* copy to be left at top */ |
25 | lua_setfield(L_, idx, fname); /* assign new table to field */ | 25 | lua_setfield(L_, idx_, fname_); /* assign new table to field */ |
26 | return 0; /* false, because did not find table there */ | 26 | return 0; /* false, because did not find table there */ |
27 | } | 27 | } |
28 | } | 28 | } |
29 | 29 | ||
30 | // ################################################################################################# | 30 | // ################################################################################################# |
31 | 31 | ||
32 | void luaL_requiref(lua_State* L_, const char* modname, lua_CFunction openf, int glb) | 32 | void luaL_requiref(lua_State* L_, const char* modname_, lua_CFunction openf_, int glb_) |
33 | { | 33 | { |
34 | lua_pushcfunction(L_, openf); | 34 | lua_pushcfunction(L_, openf_); |
35 | lua_pushstring(L_, modname); /* argument to open function */ | 35 | lua_pushstring(L_, modname_); /* argument to open function */ |
36 | lua_call(L_, 1, 1); /* open module */ | 36 | lua_call(L_, 1, 1); /* open module */ |
37 | luaL_getsubtable(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); | 37 | luaL_getsubtable(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); |
38 | lua_pushvalue(L_, -2); /* make copy of module (call result) */ | 38 | lua_pushvalue(L_, -2); /* make copy of module (call result) */ |
39 | lua_setfield(L_, -2, modname); /* _LOADED[modname] = module */ | 39 | lua_setfield(L_, -2, modname_); /* _LOADED[modname] = module */ |
40 | lua_pop(L_, 1); /* remove _LOADED table */ | 40 | lua_pop(L_, 1); /* remove _LOADED table */ |
41 | if (glb) { | 41 | if (glb_) { |
42 | lua_pushvalue(L_, -1); /* copy of 'mod' */ | 42 | lua_pushvalue(L_, -1); /* copy of 'mod' */ |
43 | lua_setglobal(L_, modname); /* _G[modname] = module */ | 43 | lua_setglobal(L_, modname_); /* _G[modname] = module */ |
44 | } | 44 | } |
45 | } | 45 | } |
46 | #endif // LUA_VERSION_NUM | 46 | #endif // LUA_VERSION_NUM |
@@ -51,23 +51,23 @@ void luaL_requiref(lua_State* L_, const char* modname, lua_CFunction openf, int | |||
51 | // ################################################################################################# | 51 | // ################################################################################################# |
52 | // ################################################################################################# | 52 | // ################################################################################################# |
53 | 53 | ||
54 | void* lua_newuserdatauv(lua_State* L_, size_t sz, int nuvalue) | 54 | void* lua_newuserdatauv(lua_State* L_, size_t sz_, int nuvalue_) |
55 | { | 55 | { |
56 | LUA_ASSERT(L_, nuvalue <= 1); | 56 | LUA_ASSERT(L_, nuvalue_ <= 1); |
57 | return lua_newuserdata(L_, sz); | 57 | return lua_newuserdata(L_, sz_); |
58 | } | 58 | } |
59 | 59 | ||
60 | // ################################################################################################# | 60 | // ################################################################################################# |
61 | 61 | ||
62 | // push on stack uservalue #n of full userdata at idx | 62 | // push on stack uservalue #n of full userdata at idx |
63 | int lua_getiuservalue(lua_State* L_, int idx, int n) | 63 | int lua_getiuservalue(lua_State* L_, int idx_, int n_) |
64 | { | 64 | { |
65 | // full userdata can have only 1 uservalue before 5.4 | 65 | // full userdata can have only 1 uservalue before 5.4 |
66 | if (n > 1) { | 66 | if (n_ > 1) { |
67 | lua_pushnil(L_); | 67 | lua_pushnil(L_); |
68 | return LUA_TNONE; | 68 | return LUA_TNONE; |
69 | } | 69 | } |
70 | lua_getuservalue(L_, idx); | 70 | lua_getuservalue(L_, idx_); |
71 | 71 | ||
72 | #if LUA_VERSION_NUM == 501 | 72 | #if LUA_VERSION_NUM == 501 |
73 | /* default environment is not a nil (see lua_getfenv) */ | 73 | /* default environment is not a nil (see lua_getfenv) */ |
@@ -88,9 +88,9 @@ int lua_getiuservalue(lua_State* L_, int idx, int n) | |||
88 | 88 | ||
89 | // Pops a value from the stack and sets it as the new n-th user value associated to the full userdata at the given index. | 89 | // Pops a value from the stack and sets it as the new n-th user value associated to the full userdata at the given index. |
90 | // Returns 0 if the userdata does not have that value. | 90 | // Returns 0 if the userdata does not have that value. |
91 | int lua_setiuservalue(lua_State* L_, int idx, int n) | 91 | int lua_setiuservalue(lua_State* L_, int idx_, int n_) |
92 | { | 92 | { |
93 | if (n > 1 | 93 | if (n_ > 1 |
94 | #if LUA_VERSION_NUM == 501 | 94 | #if LUA_VERSION_NUM == 501 |
95 | || lua_type(L_, -1) != LUA_TTABLE | 95 | || lua_type(L_, -1) != LUA_TTABLE |
96 | #endif | 96 | #endif |
@@ -99,7 +99,7 @@ int lua_setiuservalue(lua_State* L_, int idx, int n) | |||
99 | return 0; | 99 | return 0; |
100 | } | 100 | } |
101 | 101 | ||
102 | lua_setuservalue(L_, idx); | 102 | lua_setuservalue(L_, idx_); |
103 | return 1; // I guess anything non-0 is ok | 103 | return 1; // I guess anything non-0 is ok |
104 | } | 104 | } |
105 | 105 | ||
diff --git a/src/compat.h b/src/compat.h index a7ad1f3..4816cb7 100644 --- a/src/compat.h +++ b/src/compat.h | |||
@@ -25,76 +25,150 @@ extern "C" | |||
25 | 25 | ||
26 | // code is now preferring Lua 5.4 API | 26 | // code is now preferring Lua 5.4 API |
27 | 27 | ||
28 | // ################################################################################################# | ||
29 | |||
28 | // add some Lua 5.3-style API when building for Lua 5.1 | 30 | // add some Lua 5.3-style API when building for Lua 5.1 |
29 | #if LUA_VERSION_NUM == 501 | 31 | #if LUA_VERSION_NUM == 501 |
30 | 32 | ||
31 | #define lua501_equal lua_equal | 33 | #define lua501_equal lua_equal |
32 | #define lua_absindex(L, idx) (((idx) >= 0 || (idx) <= LUA_REGISTRYINDEX) ? (idx) : lua_gettop(L) + (idx) + 1) | 34 | inline int lua_absindex(lua_State* L_, int idx_) |
35 | { | ||
36 | return (((idx_) >= 0 || (idx_) <= LUA_REGISTRYINDEX) ? (idx_) : lua_gettop(L_) + (idx_) + 1); | ||
37 | } | ||
33 | #if LUAJIT_VERSION_NUM < 20200 // moonjit is 5.1 plus bits of 5.2 that we don't need to wrap | 38 | #if LUAJIT_VERSION_NUM < 20200 // moonjit is 5.1 plus bits of 5.2 that we don't need to wrap |
34 | #define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX) | 39 | inline void lua_pushglobaltable(lua_State* L_) |
40 | { | ||
41 | lua_pushvalue(L_, LUA_GLOBALSINDEX); | ||
42 | } | ||
35 | #endif // LUAJIT_VERSION_NUM | 43 | #endif // LUAJIT_VERSION_NUM |
36 | #define lua_setuservalue lua_setfenv | 44 | inline int lua_setuservalue(lua_State* L_, int idx_) |
37 | #define lua_getuservalue lua_getfenv | 45 | { |
38 | #define lua_rawlen lua_objlen | 46 | return lua_setfenv(L_, idx_); |
39 | #define luaG_registerlibfuncs(L_, _funcs) luaL_register(L_, nullptr, _funcs) | 47 | } |
48 | inline void lua_getuservalue(lua_State* L_, int idx_) | ||
49 | { | ||
50 | lua_getfenv(L_, idx_); | ||
51 | } | ||
52 | inline size_t lua_rawlen(lua_State* L_, int idx_) | ||
53 | { | ||
54 | return lua_objlen(L_, idx_); | ||
55 | } | ||
56 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) | ||
57 | { | ||
58 | luaL_register(L_, nullptr, funcs_); | ||
59 | } | ||
60 | // keep as macros to be consistent with Lua headers | ||
40 | #define LUA_OK 0 | 61 | #define LUA_OK 0 |
41 | #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value | 62 | #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value |
42 | void luaL_requiref(lua_State* L_, const char* modname, lua_CFunction openf, int glb); // implementation copied from Lua 5.2 sources | 63 | void luaL_requiref(lua_State* L_, const char* modname_, lua_CFunction openf_, int glb_); // implementation copied from Lua 5.2 sources |
43 | #define lua504_dump(L_, writer_, data_, strip_) lua_dump(L_, writer_, data_) | 64 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_unused]] int strip_) |
65 | { | ||
66 | return lua_dump(L_, writer_, data_); | ||
67 | } | ||
44 | #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.1 | 68 | #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.1 |
45 | 69 | ||
46 | #endif // LUA_VERSION_NUM == 501 | 70 | #endif // LUA_VERSION_NUM == 501 |
47 | 71 | ||
72 | // ################################################################################################# | ||
73 | |||
48 | // wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way | 74 | // wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way |
49 | #if LUA_VERSION_NUM == 502 | 75 | #if LUA_VERSION_NUM == 502 |
50 | 76 | ||
51 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h | 77 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h |
52 | #define lua501_equal(L, a, b) lua_compare(L, a, b, LUA_OPEQ) | 78 | inline int lua501_equal(lua_State* L_, int a_, int b_) |
79 | { | ||
80 | return lua_compare(L_, a_, b_, LUA_OPEQ); | ||
81 | } | ||
53 | #endif // lua501_equal | 82 | #endif // lua501_equal |
54 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h | 83 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h |
55 | #define lua_lessthan(L, a, b) lua_compare(L, a, b, LUA_OPLT) | 84 | inline int lua_lessthan(lua_State* L_, int a_, int b_) |
85 | { | ||
86 | return lua_compare(L_, a_, b_, LUA_OPLT); | ||
87 | } | ||
56 | #endif // lua_lessthan | 88 | #endif // lua_lessthan |
57 | #define luaG_registerlibfuncs(L, _funcs) luaL_setfuncs(L, _funcs, 0) | 89 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) |
58 | #define lua504_dump(L, writer, data, strip) lua_dump(L, writer, data) | 90 | { |
91 | luaL_setfuncs(L_, funcs_, 0); | ||
92 | } | ||
93 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_unused]] int strip_) | ||
94 | { | ||
95 | return lua_dump(L_, writer_, data_); | ||
96 | } | ||
59 | #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.2 | 97 | #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.2 |
60 | 98 | ||
61 | #endif // LUA_VERSION_NUM == 502 | 99 | #endif // LUA_VERSION_NUM == 502 |
62 | 100 | ||
101 | // ################################################################################################# | ||
102 | |||
63 | // wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way | 103 | // wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way |
64 | #if LUA_VERSION_NUM == 503 | 104 | #if LUA_VERSION_NUM == 503 |
65 | 105 | ||
66 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h | 106 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h |
67 | #define lua501_equal(L, a, b) lua_compare(L, a, b, LUA_OPEQ) | 107 | inline int lua501_equal(lua_State* L_, int a_, int b_) |
108 | { | ||
109 | return lua_compare(L_, a_, b_, LUA_OPEQ); | ||
110 | } | ||
68 | #endif // lua501_equal | 111 | #endif // lua501_equal |
69 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h | 112 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h |
70 | #define lua_lessthan(L, a, b) lua_compare(L, a, b, LUA_OPLT) | 113 | inline int lua_lessthan(lua_State* L_, int a_, int b_) |
114 | { | ||
115 | return lua_compare(L_, a_, b_, LUA_OPLT); | ||
116 | } | ||
71 | #endif // lua_lessthan | 117 | #endif // lua_lessthan |
72 | #define luaG_registerlibfuncs(L, _funcs) luaL_setfuncs(L, _funcs, 0) | 118 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) |
73 | #define lua504_dump lua_dump | 119 | { |
74 | #define luaL_optint(L, n, d) ((int) luaL_optinteger(L, (n), (d))) | 120 | luaL_setfuncs(L_, funcs_, 0); |
121 | } | ||
122 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) | ||
123 | { | ||
124 | return lua_dump(L_, writer_, data_, strip_); | ||
125 | } | ||
126 | inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) | ||
127 | { | ||
128 | return static_cast<int>(luaL_optinteger(L_, n_, d_)); | ||
129 | } | ||
75 | 130 | ||
76 | #endif // LUA_VERSION_NUM == 503 | 131 | #endif // LUA_VERSION_NUM == 503 |
77 | 132 | ||
133 | // ################################################################################################# | ||
134 | |||
78 | #if LUA_VERSION_NUM < 504 | 135 | #if LUA_VERSION_NUM < 504 |
79 | 136 | ||
80 | void* lua_newuserdatauv(lua_State* L, size_t sz, int nuvalue); | 137 | void* lua_newuserdatauv(lua_State* L_, size_t sz_, int nuvalue_); |
81 | int lua_getiuservalue(lua_State* L, int idx, int n); | 138 | int lua_getiuservalue(lua_State* L_, int idx_, int n_); |
82 | int lua_setiuservalue(lua_State* L, int idx, int n); | 139 | int lua_setiuservalue(lua_State* L_, int idx_, int n_); |
83 | 140 | ||
84 | #endif // LUA_VERSION_NUM < 504 | 141 | #endif // LUA_VERSION_NUM < 504 |
85 | 142 | ||
143 | // ################################################################################################# | ||
144 | |||
86 | // wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way | 145 | // wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way |
87 | #if LUA_VERSION_NUM == 504 | 146 | #if LUA_VERSION_NUM == 504 |
88 | 147 | ||
89 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h | 148 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h |
90 | #define lua501_equal(L, a, b) lua_compare(L, a, b, LUA_OPEQ) | 149 | inline int lua501_equal(lua_State* L_, int a_, int b_) |
150 | { | ||
151 | return lua_compare(L_, a_, b_, LUA_OPEQ); | ||
152 | } | ||
91 | #endif // lua501_equal | 153 | #endif // lua501_equal |
92 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h | 154 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h |
93 | #define lua_lessthan(L, a, b) lua_compare(L, a, b, LUA_OPLT) | 155 | inline int lua_lessthan(lua_State* L_, int a_, int b_) |
156 | { | ||
157 | return lua_compare(L_, a_, b_, LUA_OPLT); | ||
158 | } | ||
94 | #endif // lua_lessthan | 159 | #endif // lua_lessthan |
95 | #define luaG_registerlibfuncs(L, _funcs) luaL_setfuncs(L, _funcs, 0) | 160 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) |
96 | #define lua504_dump lua_dump | 161 | { |
97 | #define luaL_optint(L, n, d) ((int) luaL_optinteger(L, (n), (d))) | 162 | luaL_setfuncs(L_, funcs_, 0); |
163 | } | ||
164 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) | ||
165 | { | ||
166 | return lua_dump(L_, writer_, data_, strip_); | ||
167 | } | ||
168 | inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) | ||
169 | { | ||
170 | return static_cast<int>(luaL_optinteger(L_, n_, d_)); | ||
171 | } | ||
98 | #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.4, we don't care about the actual value | 172 | #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.4, we don't care about the actual value |
99 | 173 | ||
100 | #endif // LUA_VERSION_NUM == 504 | 174 | #endif // LUA_VERSION_NUM == 504 |
@@ -117,11 +191,11 @@ enum class LuaType | |||
117 | CDATA = 10 // LuaJIT CDATA | 191 | CDATA = 10 // LuaJIT CDATA |
118 | }; | 192 | }; |
119 | 193 | ||
120 | inline LuaType lua_type_as_enum(lua_State* L, int idx_) | 194 | inline LuaType lua_type_as_enum(lua_State* L_, int idx_) |
121 | { | 195 | { |
122 | return static_cast<LuaType>(lua_type(L, idx_)); | 196 | return static_cast<LuaType>(lua_type(L_, idx_)); |
123 | } | 197 | } |
124 | inline char const* lua_typename(lua_State* L, LuaType t_) | 198 | inline char const* lua_typename(lua_State* L_, LuaType t_) |
125 | { | 199 | { |
126 | return lua_typename(L, static_cast<int>(t_)); | 200 | return lua_typename(L_, static_cast<int>(t_)); |
127 | } | 201 | } |
diff --git a/src/deep.cpp b/src/deep.cpp index af7e814..13932a4 100644 --- a/src/deep.cpp +++ b/src/deep.cpp | |||
@@ -107,12 +107,12 @@ static void get_deep_lookup(lua_State* L_) | |||
107 | * Return the registered factory for 'index' (deep userdata proxy), | 107 | * Return the registered factory for 'index' (deep userdata proxy), |
108 | * or nullptr if 'index' is not a deep userdata proxy. | 108 | * or nullptr if 'index' is not a deep userdata proxy. |
109 | */ | 109 | */ |
110 | [[nodiscard]] static inline DeepFactory* get_factory(lua_State* L_, int index, LookupMode mode_) | 110 | [[nodiscard]] static inline DeepFactory* get_factory(lua_State* L_, int index_, LookupMode mode_) |
111 | { | 111 | { |
112 | // when looking inside a keeper, we are 100% sure the object is a deep userdata | 112 | // when looking inside a keeper, we are 100% sure the object is a deep userdata |
113 | if (mode_ == LookupMode::FromKeeper) | 113 | if (mode_ == LookupMode::FromKeeper) |
114 | { | 114 | { |
115 | DeepPrelude* const proxy{ *lua_tofulluserdata<DeepPrelude*>(L_, index) }; | 115 | DeepPrelude* const proxy{ *lua_tofulluserdata<DeepPrelude*>(L_, index_) }; |
116 | // we can (and must) cast and fetch the internally stored factory | 116 | // we can (and must) cast and fetch the internally stored factory |
117 | return &proxy->m_factory; | 117 | return &proxy->m_factory; |
118 | } | 118 | } |
@@ -124,7 +124,7 @@ static void get_deep_lookup(lua_State* L_) | |||
124 | STACK_GROW( L_, 1); | 124 | STACK_GROW( L_, 1); |
125 | STACK_CHECK_START_REL(L_, 0); | 125 | STACK_CHECK_START_REL(L_, 0); |
126 | 126 | ||
127 | if (!lua_getmetatable( L_, index)) // deep ... metatable? | 127 | if (!lua_getmetatable( L_, index_)) // deep ... metatable? |
128 | { | 128 | { |
129 | return nullptr; // no metatable: can't be a deep userdata object! | 129 | return nullptr; // no metatable: can't be a deep userdata object! |
130 | } | 130 | } |
@@ -168,11 +168,11 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) | |||
168 | if (isLastRef) | 168 | if (isLastRef) |
169 | { | 169 | { |
170 | // retrieve wrapped __gc | 170 | // retrieve wrapped __gc |
171 | lua_pushvalue( L_, lua_upvalueindex( 1)); // self __gc? | 171 | lua_pushvalue(L_, lua_upvalueindex( 1)); // self __gc? |
172 | if (!lua_isnil( L_, -1)) | 172 | if (!lua_isnil(L_, -1)) |
173 | { | 173 | { |
174 | lua_insert( L_, -2); // __gc self | 174 | lua_insert(L_, -2); // __gc self |
175 | lua_call( L_, 1, 0); // | 175 | lua_call(L_, 1, 0); // |
176 | } | 176 | } |
177 | else | 177 | else |
178 | { | 178 | { |
@@ -195,138 +195,138 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) | |||
195 | * used in this Lua state (metatable, registring it). Otherwise, increments the | 195 | * used in this Lua state (metatable, registring it). Otherwise, increments the |
196 | * reference count. | 196 | * reference count. |
197 | */ | 197 | */ |
198 | char const* DeepFactory::PushDeepProxy(DestState L, DeepPrelude* prelude, int nuv_, LookupMode mode_) | 198 | char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int nuv_, LookupMode mode_) |
199 | { | 199 | { |
200 | // Check if a proxy already exists | 200 | // Check if a proxy already exists |
201 | push_registry_subtable_mode(L, kDeepProxyCacheRegKey, "v"); // DPC | 201 | push_registry_subtable_mode(L_, kDeepProxyCacheRegKey, "v"); // DPC |
202 | lua_pushlightuserdata(L, prelude); // DPC deep | 202 | lua_pushlightuserdata(L_, prelude_); // DPC deep |
203 | lua_rawget(L, -2); // DPC proxy | 203 | lua_rawget(L_, -2); // DPC proxy |
204 | if (!lua_isnil(L, -1)) | 204 | if (!lua_isnil(L_, -1)) |
205 | { | 205 | { |
206 | lua_remove(L, -2); // proxy | 206 | lua_remove(L_, -2); // proxy |
207 | return nullptr; | 207 | return nullptr; |
208 | } | 208 | } |
209 | else | 209 | else |
210 | { | 210 | { |
211 | lua_pop(L, 1); // DPC | 211 | lua_pop(L_, 1); // DPC |
212 | } | 212 | } |
213 | 213 | ||
214 | STACK_GROW(L, 7); | 214 | STACK_GROW(L_, 7); |
215 | STACK_CHECK_START_REL(L, 0); | 215 | STACK_CHECK_START_REL(L_, 0); |
216 | 216 | ||
217 | // a new full userdata, fitted with the specified number of uservalue slots (always 1 for Lua < 5.4) | 217 | // a new full userdata, fitted with the specified number of uservalue slots (always 1 for Lua < 5.4) |
218 | DeepPrelude** const proxy{ lua_newuserdatauv<DeepPrelude*>(L, nuv_) }; // DPC proxy | 218 | DeepPrelude** const proxy{ lua_newuserdatauv<DeepPrelude*>(L_, nuv_) }; // DPC proxy |
219 | LUA_ASSERT(L, proxy); | 219 | LUA_ASSERT(L_, proxy); |
220 | *proxy = prelude; | 220 | *proxy = prelude_; |
221 | prelude->m_refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data | 221 | prelude_->m_refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data |
222 | 222 | ||
223 | // Get/create metatable for 'factory' (in this state) | 223 | // Get/create metatable for 'factory' (in this state) |
224 | DeepFactory& factory = prelude->m_factory; | 224 | DeepFactory& factory = prelude_->m_factory; |
225 | lua_pushlightuserdata( L, std::bit_cast<void*>(&factory)); // DPC proxy factory | 225 | lua_pushlightuserdata(L_, std::bit_cast<void*>(&factory)); // DPC proxy factory |
226 | get_deep_lookup( L); // DPC proxy metatable? | 226 | get_deep_lookup(L_); // DPC proxy metatable? |
227 | 227 | ||
228 | if (lua_isnil( L, -1)) // // No metatable yet. | 228 | if (lua_isnil(L_, -1)) // // No metatable yet. |
229 | { | 229 | { |
230 | lua_pop(L, 1); // DPC proxy | 230 | lua_pop(L_, 1); // DPC proxy |
231 | int const oldtop{ lua_gettop(L) }; | 231 | int const oldtop{ lua_gettop(L_) }; |
232 | // 1 - make one and register it | 232 | // 1 - make one and register it |
233 | if (mode_ != LookupMode::ToKeeper) | 233 | if (mode_ != LookupMode::ToKeeper) |
234 | { | 234 | { |
235 | factory.createMetatable(L); // DPC proxy metatable | 235 | factory.createMetatable(L_); // DPC proxy metatable |
236 | if (lua_gettop(L) - oldtop != 1 || !lua_istable(L, -1)) | 236 | if (lua_gettop(L_) - oldtop != 1 || !lua_istable(L_, -1)) |
237 | { | 237 | { |
238 | // factory didn't push exactly 1 value, or the value it pushed is not a table: ERROR! | 238 | // factory didn't push exactly 1 value, or the value it pushed is not a table: ERROR! |
239 | lua_settop( L, oldtop); // DPC proxy X | 239 | lua_settop(L_, oldtop); // DPC proxy X |
240 | lua_pop( L, 3); // | 240 | lua_pop(L_, 3); // |
241 | return "Bad DeepFactory::createMetatable overload: unexpected pushed value"; | 241 | return "Bad DeepFactory::createMetatable overload: unexpected pushed value"; |
242 | } | 242 | } |
243 | // if the metatable contains a __gc, we will call it from our own | 243 | // if the metatable contains a __gc, we will call it from our own |
244 | lua_getfield( L, -1, "__gc"); // DPC proxy metatable __gc | 244 | lua_getfield(L_, -1, "__gc"); // DPC proxy metatable __gc |
245 | } | 245 | } |
246 | else | 246 | else |
247 | { | 247 | { |
248 | // keepers need a minimal metatable that only contains our own __gc | 248 | // keepers need a minimal metatable that only contains our own __gc |
249 | lua_newtable( L); // DPC proxy metatable | 249 | lua_newtable(L_); // DPC proxy metatable |
250 | lua_pushnil( L); // DPC proxy metatable nil | 250 | lua_pushnil(L_); // DPC proxy metatable nil |
251 | } | 251 | } |
252 | if (lua_isnil( L, -1)) | 252 | if (lua_isnil(L_, -1)) |
253 | { | 253 | { |
254 | // Add our own '__gc' method | 254 | // Add our own '__gc' method |
255 | lua_pop( L, 1); // DPC proxy metatable | 255 | lua_pop(L_, 1); // DPC proxy metatable |
256 | lua_pushcfunction( L, deep_userdata_gc); // DPC proxy metatable deep_userdata_gc | 256 | lua_pushcfunction(L_, deep_userdata_gc); // DPC proxy metatable deep_userdata_gc |
257 | } | 257 | } |
258 | else | 258 | else |
259 | { | 259 | { |
260 | // Add our own '__gc' method wrapping the original | 260 | // Add our own '__gc' method wrapping the original |
261 | lua_pushcclosure( L, deep_userdata_gc, 1); // DPC proxy metatable deep_userdata_gc | 261 | lua_pushcclosure(L_, deep_userdata_gc, 1); // DPC proxy metatable deep_userdata_gc |
262 | } | 262 | } |
263 | lua_setfield( L, -2, "__gc"); // DPC proxy metatable | 263 | lua_setfield(L_, -2, "__gc"); // DPC proxy metatable |
264 | 264 | ||
265 | // Memorize for later rounds | 265 | // Memorize for later rounds |
266 | lua_pushvalue( L, -1); // DPC proxy metatable metatable | 266 | lua_pushvalue(L_, -1); // DPC proxy metatable metatable |
267 | lua_pushlightuserdata(L, std::bit_cast<void*>(&factory)); // DPC proxy metatable metatable factory | 267 | lua_pushlightuserdata(L_, std::bit_cast<void*>(&factory)); // DPC proxy metatable metatable factory |
268 | set_deep_lookup( L); // DPC proxy metatable | 268 | set_deep_lookup(L_); // DPC proxy metatable |
269 | 269 | ||
270 | // 2 - cause the target state to require the module that exported the factory | 270 | // 2 - cause the target state to require the module that exported the factory |
271 | if (char const* const modname{ factory.moduleName() }; modname) // we actually got a module name | 271 | if (char const* const modname{ factory.moduleName() }; modname) // we actually got a module name |
272 | { | 272 | { |
273 | // L.registry._LOADED exists without having registered the 'package' library. | 273 | // L.registry._LOADED exists without having registered the 'package' library. |
274 | lua_getglobal( L, "require"); // DPC proxy metatable require() | 274 | lua_getglobal(L_, "require"); // DPC proxy metatable require() |
275 | // check that the module is already loaded (or being loaded, we are happy either way) | 275 | // check that the module is already loaded (or being loaded, we are happy either way) |
276 | if (lua_isfunction( L, -1)) | 276 | if (lua_isfunction(L_, -1)) |
277 | { | 277 | { |
278 | lua_pushstring( L, modname); // DPC proxy metatable require() "module" | 278 | lua_pushstring(L_, modname); // DPC proxy metatable require() "module" |
279 | lua_getfield( L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // DPC proxy metatable require() "module" _R._LOADED | 279 | lua_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // DPC proxy metatable require() "module" _R._LOADED |
280 | if (lua_istable( L, -1)) | 280 | if (lua_istable(L_, -1)) |
281 | { | 281 | { |
282 | lua_pushvalue( L, -2); // DPC proxy metatable require() "module" _R._LOADED "module" | 282 | lua_pushvalue(L_, -2); // DPC proxy metatable require() "module" _R._LOADED "module" |
283 | lua_rawget( L, -2); // DPC proxy metatable require() "module" _R._LOADED module | 283 | lua_rawget(L_, -2); // DPC proxy metatable require() "module" _R._LOADED module |
284 | int const alreadyloaded = lua_toboolean( L, -1); | 284 | int const alreadyloaded = lua_toboolean(L_, -1); |
285 | if (!alreadyloaded) // not loaded | 285 | if (!alreadyloaded) // not loaded |
286 | { | 286 | { |
287 | int require_result; | 287 | int require_result; |
288 | lua_pop( L, 2); // DPC proxy metatable require() "module" | 288 | lua_pop(L_, 2); // DPC proxy metatable require() "module" |
289 | // require "modname" | 289 | // require "modname" |
290 | require_result = lua_pcall( L, 1, 0, 0); // DPC proxy metatable error? | 290 | require_result = lua_pcall(L_, 1, 0, 0); // DPC proxy metatable error? |
291 | if (require_result != LUA_OK) | 291 | if (require_result != LUA_OK) |
292 | { | 292 | { |
293 | // failed, return the error message | 293 | // failed, return the error message |
294 | lua_pushfstring( L, "error while requiring '%s' identified by DeepFactory::moduleName: ", modname); | 294 | lua_pushfstring(L_, "error while requiring '%s' identified by DeepFactory::moduleName: ", modname); |
295 | lua_insert( L, -2); // DPC proxy metatable prefix error | 295 | lua_insert(L_, -2); // DPC proxy metatable prefix error |
296 | lua_concat( L, 2); // DPC proxy metatable error | 296 | lua_concat(L_, 2); // DPC proxy metatable error |
297 | return lua_tostring( L, -1); | 297 | return lua_tostring(L_, -1); |
298 | } | 298 | } |
299 | } | 299 | } |
300 | else // already loaded, we are happy | 300 | else // already loaded, we are happy |
301 | { | 301 | { |
302 | lua_pop( L, 4); // DPC proxy metatable | 302 | lua_pop(L_, 4); // DPC proxy metatable |
303 | } | 303 | } |
304 | } | 304 | } |
305 | else // no L.registry._LOADED; can this ever happen? | 305 | else // no L.registry._LOADED; can this ever happen? |
306 | { | 306 | { |
307 | lua_pop( L, 6); // | 307 | lua_pop(L_, 6); // |
308 | return "unexpected error while requiring a module identified by DeepFactory::moduleName"; | 308 | return "unexpected error while requiring a module identified by DeepFactory::moduleName"; |
309 | } | 309 | } |
310 | } | 310 | } |
311 | else // a module name, but no require() function :-( | 311 | else // a module name, but no require() function :-( |
312 | { | 312 | { |
313 | lua_pop( L, 4); // | 313 | lua_pop(L_, 4); // |
314 | return "lanes receiving deep userdata should register the 'package' library"; | 314 | return "lanes receiving deep userdata should register the 'package' library"; |
315 | } | 315 | } |
316 | } | 316 | } |
317 | } | 317 | } |
318 | STACK_CHECK(L, 2); // DPC proxy metatable | 318 | STACK_CHECK(L_, 2); // DPC proxy metatable |
319 | LUA_ASSERT(L, lua_type_as_enum(L, -2) == LuaType::USERDATA); | 319 | LUA_ASSERT(L_, lua_type_as_enum(L_, -2) == LuaType::USERDATA); |
320 | LUA_ASSERT(L, lua_istable( L, -1)); | 320 | LUA_ASSERT(L_, lua_istable(L_, -1)); |
321 | lua_setmetatable( L, -2); // DPC proxy | 321 | lua_setmetatable(L_, -2); // DPC proxy |
322 | 322 | ||
323 | // If we're here, we obviously had to create a new proxy, so cache it. | 323 | // If we're here, we obviously had to create a new proxy, so cache it. |
324 | lua_pushlightuserdata( L, prelude); // DPC proxy deep | 324 | lua_pushlightuserdata(L_, prelude_); // DPC proxy deep |
325 | lua_pushvalue( L, -2); // DPC proxy deep proxy | 325 | lua_pushvalue(L_, -2); // DPC proxy deep proxy |
326 | lua_rawset( L, -4); // DPC proxy | 326 | lua_rawset(L_, -4); // DPC proxy |
327 | lua_remove( L, -2); // proxy | 327 | lua_remove(L_, -2); // proxy |
328 | LUA_ASSERT(L, lua_type_as_enum(L, -1) == LuaType::USERDATA); | 328 | LUA_ASSERT(L_, lua_type_as_enum(L_, -1) == LuaType::USERDATA); |
329 | STACK_CHECK(L, 0); | 329 | STACK_CHECK(L_, 0); |
330 | return nullptr; | 330 | return nullptr; |
331 | } | 331 | } |
332 | 332 | ||
@@ -347,40 +347,40 @@ char const* DeepFactory::PushDeepProxy(DestState L, DeepPrelude* prelude, int nu | |||
347 | * | 347 | * |
348 | * Returns: 'proxy' userdata for accessing the deep data via 'DeepFactory::toDeep()' | 348 | * Returns: 'proxy' userdata for accessing the deep data via 'DeepFactory::toDeep()' |
349 | */ | 349 | */ |
350 | int DeepFactory::pushDeepUserdata(DestState L, int nuv_) const | 350 | int DeepFactory::pushDeepUserdata(DestState L_, int nuv_) const |
351 | { | 351 | { |
352 | STACK_GROW( L, 1); | 352 | STACK_GROW(L_, 1); |
353 | STACK_CHECK_START_REL(L, 0); | 353 | STACK_CHECK_START_REL(L_, 0); |
354 | int const oldtop{ lua_gettop(L) }; | 354 | int const oldtop{ lua_gettop(L_) }; |
355 | DeepPrelude* const prelude{ newDeepObjectInternal(L) }; | 355 | DeepPrelude* const prelude{ newDeepObjectInternal(L_) }; |
356 | if (prelude == nullptr) | 356 | if (prelude == nullptr) |
357 | { | 357 | { |
358 | raise_luaL_error(L, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); | 358 | raise_luaL_error(L_, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); |
359 | } | 359 | } |
360 | 360 | ||
361 | if (prelude->m_magic != kDeepVersion) | 361 | if (prelude->m_magic != kDeepVersion) |
362 | { | 362 | { |
363 | // just in case, don't leak the newly allocated deep userdata object | 363 | // just in case, don't leak the newly allocated deep userdata object |
364 | deleteDeepObjectInternal(L, prelude); | 364 | deleteDeepObjectInternal(L_, prelude); |
365 | raise_luaL_error(L, "Bad Deep Factory: kDeepVersion is incorrect, rebuild your implementation with the latest deep implementation"); | 365 | raise_luaL_error(L_, "Bad Deep Factory: kDeepVersion is incorrect, rebuild your implementation with the latest deep implementation"); |
366 | } | 366 | } |
367 | 367 | ||
368 | LUA_ASSERT(L, prelude->m_refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 | 368 | LUA_ASSERT(L_, prelude->m_refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 |
369 | LUA_ASSERT(L, &prelude->m_factory == this); | 369 | LUA_ASSERT(L_, &prelude->m_factory == this); |
370 | 370 | ||
371 | if (lua_gettop( L) - oldtop != 0) | 371 | if (lua_gettop(L_) - oldtop != 0) |
372 | { | 372 | { |
373 | // just in case, don't leak the newly allocated deep userdata object | 373 | // just in case, don't leak the newly allocated deep userdata object |
374 | deleteDeepObjectInternal(L, prelude); | 374 | deleteDeepObjectInternal(L_, prelude); |
375 | raise_luaL_error(L, "Bad DeepFactory::newDeepObjectInternal overload: should not push anything on the stack"); | 375 | raise_luaL_error(L_, "Bad DeepFactory::newDeepObjectInternal overload: should not push anything on the stack"); |
376 | } | 376 | } |
377 | 377 | ||
378 | char const* const errmsg{ DeepFactory::PushDeepProxy(L, prelude, nuv_, LookupMode::LaneBody) }; // proxy | 378 | char const* const errmsg{ DeepFactory::PushDeepProxy(L_, prelude, nuv_, LookupMode::LaneBody) }; // proxy |
379 | if (errmsg != nullptr) | 379 | if (errmsg != nullptr) |
380 | { | 380 | { |
381 | raise_luaL_error(L, errmsg); | 381 | raise_luaL_error(L_, errmsg); |
382 | } | 382 | } |
383 | STACK_CHECK( L, 1); | 383 | STACK_CHECK(L_, 1); |
384 | return 1; | 384 | return 1; |
385 | } | 385 | } |
386 | 386 | ||
@@ -392,24 +392,24 @@ int DeepFactory::pushDeepUserdata(DestState L, int nuv_) const | |||
392 | * Reference count is not changed, and access to the deep userdata is not | 392 | * Reference count is not changed, and access to the deep userdata is not |
393 | * serialized. It is the module's responsibility to prevent conflicting usage. | 393 | * serialized. It is the module's responsibility to prevent conflicting usage. |
394 | */ | 394 | */ |
395 | DeepPrelude* DeepFactory::toDeep(lua_State* L_, int index) const | 395 | DeepPrelude* DeepFactory::toDeep(lua_State* L_, int index_) const |
396 | { | 396 | { |
397 | STACK_CHECK_START_REL(L_, 0); | 397 | STACK_CHECK_START_REL(L_, 0); |
398 | // ensure it is actually a deep userdata we created | 398 | // ensure it is actually a deep userdata we created |
399 | if (get_factory(L_, index, LookupMode::LaneBody) != this) | 399 | if (get_factory(L_, index_, LookupMode::LaneBody) != this) |
400 | { | 400 | { |
401 | return nullptr; // no metatable, or wrong kind | 401 | return nullptr; // no metatable, or wrong kind |
402 | } | 402 | } |
403 | STACK_CHECK(L_, 0); | 403 | STACK_CHECK(L_, 0); |
404 | 404 | ||
405 | DeepPrelude** const proxy{ lua_tofulluserdata<DeepPrelude*>(L_, index) }; | 405 | DeepPrelude** const proxy{ lua_tofulluserdata<DeepPrelude*>(L_, index_) }; |
406 | return *proxy; | 406 | return *proxy; |
407 | } | 407 | } |
408 | 408 | ||
409 | // ################################################################################################# | 409 | // ################################################################################################# |
410 | 410 | ||
411 | /* | 411 | /* |
412 | * Copy deep userdata between two separate Lua states (from L to L2) | 412 | * Copy deep userdata between two separate Lua states (from L1 to L2) |
413 | * | 413 | * |
414 | * Returns: | 414 | * Returns: |
415 | * the id function of the copied value, or nullptr for non-deep userdata | 415 | * the id function of the copied value, or nullptr for non-deep userdata |
diff --git a/src/keeper.cpp b/src/keeper.cpp index 33da736..cbdd0c9 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp | |||
@@ -209,13 +209,13 @@ static void push_table(lua_State* L_, int idx_) | |||
209 | // ################################################################################################# | 209 | // ################################################################################################# |
210 | 210 | ||
211 | // only used by linda:dump() and linda:__towatch() for debugging purposes | 211 | // only used by linda:dump() and linda:__towatch() for debugging purposes |
212 | int keeper_push_linda_storage(Linda& linda_, DestState L) | 212 | int keeper_push_linda_storage(Linda& linda_, DestState L_) |
213 | { | 213 | { |
214 | Keeper* const K{ linda_.whichKeeper() }; | 214 | Keeper* const K{ linda_.whichKeeper() }; |
215 | SourceState const KL{ K ? K->L : nullptr }; | 215 | SourceState const KL{ K ? K->L : nullptr }; |
216 | if (KL == nullptr) | 216 | if (KL == nullptr) |
217 | return 0; | 217 | return 0; |
218 | STACK_GROW(KL, 4); // KEEPER MAIN | 218 | STACK_GROW(KL, 4); // KEEPER MAIN |
219 | STACK_CHECK_START_REL(KL, 0); | 219 | STACK_CHECK_START_REL(KL, 0); |
220 | kFifosRegKey.pushValue(KL); // fifos | 220 | kFifosRegKey.pushValue(KL); // fifos |
221 | lua_pushlightuserdata(KL, &linda_); // fifos ud | 221 | lua_pushlightuserdata(KL, &linda_); // fifos ud |
@@ -228,33 +228,33 @@ int keeper_push_linda_storage(Linda& linda_, DestState L) | |||
228 | return 0; | 228 | return 0; |
229 | } | 229 | } |
230 | // move data from keeper to destination state | 230 | // move data from keeper to destination state |
231 | STACK_GROW(L, 5); | 231 | STACK_GROW(L_, 5); |
232 | STACK_CHECK_START_REL(L, 0); | 232 | STACK_CHECK_START_REL(L_, 0); |
233 | lua_newtable(L); // out | 233 | lua_newtable(L_); // out |
234 | InterCopyContext c{ linda_.U, L, KL, {}, {}, {}, LookupMode::FromKeeper, {} }; | 234 | InterCopyContext c{ linda_.U, L_, KL, {}, {}, {}, LookupMode::FromKeeper, {} }; |
235 | lua_pushnil(KL); // storage nil | 235 | lua_pushnil(KL); // storage nil |
236 | while (lua_next(KL, -2)) // storage key fifo | 236 | while (lua_next(KL, -2)) // storage key fifo |
237 | { | 237 | { |
238 | keeper_fifo* fifo = prepare_fifo_access(KL, -1); // storage key fifotbl | 238 | keeper_fifo* fifo = prepare_fifo_access(KL, -1); // storage key fifotbl |
239 | lua_pushvalue(KL, -2); // storage key fifotbl key | 239 | lua_pushvalue(KL, -2); // storage key fifotbl key |
240 | std::ignore = c.inter_move(1); // storage key fifotbl // out key | 240 | std::ignore = c.inter_move(1); // storage key fifotbl // out key |
241 | STACK_CHECK(L, 2); | 241 | STACK_CHECK(L_, 2); |
242 | lua_newtable(L); // out key keyout | 242 | lua_newtable(L_); // out key keyout |
243 | std::ignore = c.inter_move(1); // storage key // out key keyout fifotbl | 243 | std::ignore = c.inter_move(1); // storage key // out key keyout fifotbl |
244 | lua_pushinteger(L, fifo->first); // out key keyout fifotbl first | 244 | lua_pushinteger(L_, fifo->first); // out key keyout fifotbl first |
245 | STACK_CHECK(L, 5); | 245 | STACK_CHECK(L_, 5); |
246 | lua_setfield(L, -3, "first"); // out key keyout fifotbl | 246 | lua_setfield(L_, -3, "first"); // out key keyout fifotbl |
247 | lua_pushinteger(L, fifo->count); // out key keyout fifobtl count | 247 | lua_pushinteger(L_, fifo->count); // out key keyout fifobtl count |
248 | STACK_CHECK(L, 5); | 248 | STACK_CHECK(L_, 5); |
249 | lua_setfield(L, -3, "count"); // out key keyout fifotbl | 249 | lua_setfield(L_, -3, "count"); // out key keyout fifotbl |
250 | lua_pushinteger(L, fifo->limit); // out key keyout fifotbl limit | 250 | lua_pushinteger(L_, fifo->limit); // out key keyout fifotbl limit |
251 | STACK_CHECK(L, 5); | 251 | STACK_CHECK(L_, 5); |
252 | lua_setfield(L, -3, "limit"); // out key keyout fifotbl | 252 | lua_setfield(L_, -3, "limit"); // out key keyout fifotbl |
253 | lua_setfield(L, -2, "fifo"); // out key keyout | 253 | lua_setfield(L_, -2, "fifo"); // out key keyout |
254 | lua_rawset(L, -3); // out | 254 | lua_rawset(L_, -3); // out |
255 | STACK_CHECK(L, 1); | 255 | STACK_CHECK(L_, 1); |
256 | } | 256 | } |
257 | STACK_CHECK(L, 1); | 257 | STACK_CHECK(L_, 1); |
258 | lua_pop(KL, 1); // | 258 | lua_pop(KL, 1); // |
259 | STACK_CHECK(KL, 0); | 259 | STACK_CHECK(KL, 0); |
260 | return 1; | 260 | return 1; |
@@ -776,10 +776,10 @@ void Linda::releaseKeeper(Keeper* K_) const | |||
776 | 776 | ||
777 | // ################################################################################################# | 777 | // ################################################################################################# |
778 | 778 | ||
779 | void keeper_toggle_nil_sentinels(lua_State* L_, int val_i_, LookupMode const mode_) | 779 | void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mode_) |
780 | { | 780 | { |
781 | int const n{ lua_gettop(L_) }; | 781 | int const n{ lua_gettop(L_) }; |
782 | for (int i = val_i_; i <= n; ++i) | 782 | for (int i = start_; i <= n; ++i) |
783 | { | 783 | { |
784 | if (mode_ == LookupMode::ToKeeper) | 784 | if (mode_ == LookupMode::ToKeeper) |
785 | { | 785 | { |
@@ -811,17 +811,17 @@ void keeper_toggle_nil_sentinels(lua_State* L_, int val_i_, LookupMode const mod | |||
811 | * | 811 | * |
812 | * Returns: number of return values (pushed to 'L'), unset in case of error | 812 | * Returns: number of return values (pushed to 'L'), unset in case of error |
813 | */ | 813 | */ |
814 | KeeperCallResult keeper_call(Universe* U, KeeperState K, keeper_api_t func_, lua_State* L_, void* linda, int starting_index) | 814 | KeeperCallResult keeper_call(Universe* U, KeeperState K, keeper_api_t func_, lua_State* L_, void* linda_, int starting_index_) |
815 | { | 815 | { |
816 | KeeperCallResult result; | 816 | KeeperCallResult result; |
817 | int const args{ starting_index ? (lua_gettop(L_) - starting_index + 1) : 0 }; | 817 | int const args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; |
818 | int const top_K{ lua_gettop(K) }; | 818 | int const top_K{ lua_gettop(K) }; |
819 | // if we didn't do anything wrong, the keeper stack should be clean | 819 | // if we didn't do anything wrong, the keeper stack should be clean |
820 | LUA_ASSERT(L_, lua_gettop(K) == 0); | 820 | LUA_ASSERT(L_, lua_gettop(K) == 0); |
821 | 821 | ||
822 | STACK_GROW(K, 2); | 822 | STACK_GROW(K, 2); |
823 | PUSH_KEEPER_FUNC(K, func_); // func_ | 823 | PUSH_KEEPER_FUNC(K, func_); // func_ |
824 | lua_pushlightuserdata(K, linda); // func_ linda | 824 | lua_pushlightuserdata(K, linda_); // func_ linda |
825 | if ( | 825 | if ( |
826 | (args == 0) || | 826 | (args == 0) || |
827 | (InterCopyContext{ U, DestState{ K }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(args) == InterCopyResult::Success) | 827 | (InterCopyContext{ U, DestState{ K }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(args) == InterCopyResult::Success) |
diff --git a/src/keeper.h b/src/keeper.h index 3740fc5..bf1ba17 100644 --- a/src/keeper.h +++ b/src/keeper.h | |||
@@ -42,8 +42,8 @@ static constexpr UniqueKey kNilSentinel{ 0xC457D4EDDB05B5E4ull, "lanes.null" }; | |||
42 | void init_keepers(Universe* U, lua_State* L_); | 42 | void init_keepers(Universe* U, lua_State* L_); |
43 | void close_keepers(Universe* U); | 43 | void close_keepers(Universe* U); |
44 | 44 | ||
45 | void keeper_toggle_nil_sentinels(lua_State* L_, int val_i_, LookupMode const mode_); | 45 | void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mode_); |
46 | [[nodiscard]] int keeper_push_linda_storage(Linda& linda_, DestState L); | 46 | [[nodiscard]] int keeper_push_linda_storage(Linda& linda_, DestState L_); |
47 | 47 | ||
48 | using keeper_api_t = lua_CFunction; | 48 | using keeper_api_t = lua_CFunction; |
49 | #define KEEPER_API(_op) keepercall_##_op | 49 | #define KEEPER_API(_op) keepercall_##_op |
diff --git a/src/lanes.cpp b/src/lanes.cpp index d87d93e..16c8e47 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp | |||
@@ -208,7 +208,7 @@ static void securize_debug_threadname(lua_State* L_, Lane* lane_) | |||
208 | STACK_GROW(L_, 3); | 208 | STACK_GROW(L_, 3); |
209 | lua_getiuservalue(L_, 1, 1); | 209 | lua_getiuservalue(L_, 1, 1); |
210 | lua_newtable(L_); | 210 | lua_newtable(L_); |
211 | // Lua 5.1 can't do 'lane_->debug_name = lua_pushstring(L, lane_->debug_name);' | 211 | // Lua 5.1 can't do 'lane_->debug_name = lua_pushstring(L_, lane_->debug_name);' |
212 | lua_pushstring(L_, lane_->debug_name); | 212 | lua_pushstring(L_, lane_->debug_name); |
213 | lane_->debug_name = lua_tostring(L_, -1); | 213 | lane_->debug_name = lua_tostring(L_, -1); |
214 | lua_rawset(L_, -3); | 214 | lua_rawset(L_, -3); |
@@ -371,7 +371,7 @@ static void push_stack_trace(lua_State* L_, int rc_, int stk_base_) | |||
371 | if (lua_rc_ != LUA_OK) // we have an error message and an optional stack trace at the bottom of the stack | 371 | if (lua_rc_ != LUA_OK) // we have an error message and an optional stack trace at the bottom of the stack |
372 | { | 372 | { |
373 | LUA_ASSERT(L_, finalizers_index == 2 || finalizers_index == 3); | 373 | LUA_ASSERT(L_, finalizers_index == 2 || finalizers_index == 3); |
374 | //char const* err_msg = lua_tostring(L, 1); | 374 | //char const* err_msg = lua_tostring(L_, 1); |
375 | lua_pushvalue(L_, 1); // ... finalizers lane_error finalizer err_msg | 375 | lua_pushvalue(L_, 1); // ... finalizers lane_error finalizer err_msg |
376 | // note we don't always have a stack trace for example when kCancelError, or when we got an error that doesn't call our handler, such as LUA_ERRMEM | 376 | // note we don't always have a stack trace for example when kCancelError, or when we got an error that doesn't call our handler, such as LUA_ERRMEM |
377 | if (finalizers_index == 3) | 377 | if (finalizers_index == 3) |
@@ -819,18 +819,18 @@ static char const* get_errcode_name( int _code) | |||
819 | } | 819 | } |
820 | #endif // USE_DEBUG_SPEW() | 820 | #endif // USE_DEBUG_SPEW() |
821 | 821 | ||
822 | static void lane_main(Lane* lane) | 822 | static void lane_main(Lane* lane_) |
823 | { | 823 | { |
824 | lua_State* const L{ lane->L }; | 824 | lua_State* const L{ lane_->L }; |
825 | // wait until the launching thread has finished preparing L | 825 | // wait until the launching thread has finished preparing L |
826 | lane->m_ready.wait(); | 826 | lane_->m_ready.wait(); |
827 | int rc{ LUA_ERRRUN }; | 827 | int rc{ LUA_ERRRUN }; |
828 | if (lane->m_status == Lane::Pending) // nothing wrong happened during preparation, we can work | 828 | if (lane_->m_status == Lane::Pending) // nothing wrong happened during preparation, we can work |
829 | { | 829 | { |
830 | // At this point, the lane function and arguments are on the stack | 830 | // At this point, the lane function and arguments are on the stack |
831 | int const nargs{ lua_gettop(L) - 1 }; | 831 | int const nargs{ lua_gettop(L) - 1 }; |
832 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); | 832 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); |
833 | lane->m_status = Lane::Running; // Pending -> Running | 833 | lane_->m_status = Lane::Running; // Pending -> Running |
834 | 834 | ||
835 | // Tie "set_finalizer()" to the state | 835 | // Tie "set_finalizer()" to the state |
836 | lua_pushcfunction(L, LG_set_finalizer); | 836 | lua_pushcfunction(L, LG_set_finalizer); |
@@ -839,7 +839,7 @@ static void lane_main(Lane* lane) | |||
839 | 839 | ||
840 | // Tie "set_debug_threadname()" to the state | 840 | // Tie "set_debug_threadname()" to the state |
841 | // But don't register it in the lookup database because of the Lane pointer upvalue | 841 | // But don't register it in the lookup database because of the Lane pointer upvalue |
842 | lua_pushlightuserdata(L, lane); | 842 | lua_pushlightuserdata(L, lane_); |
843 | lua_pushcclosure(L, LG_set_debug_threadname, 1); | 843 | lua_pushcclosure(L, LG_set_debug_threadname, 1); |
844 | lua_setglobal(L, "set_debug_threadname"); | 844 | lua_setglobal(L, "set_debug_threadname"); |
845 | 845 | ||
@@ -879,24 +879,24 @@ static void lane_main(Lane* lane) | |||
879 | // the finalizer generated an error, and left its own error message [and stack trace] on the stack | 879 | // the finalizer generated an error, and left its own error message [and stack trace] on the stack |
880 | rc = rc2; // we're overruling the earlier script error or normal return | 880 | rc = rc2; // we're overruling the earlier script error or normal return |
881 | } | 881 | } |
882 | lane->m_waiting_on = nullptr; // just in case | 882 | lane_->m_waiting_on = nullptr; // just in case |
883 | if (selfdestruct_remove(lane)) // check and remove (under lock!) | 883 | if (selfdestruct_remove(lane_)) // check and remove (under lock!) |
884 | { | 884 | { |
885 | // We're a free-running thread and no-one's there to clean us up. | 885 | // We're a free-running thread and no-one's there to clean us up. |
886 | lua_close(lane->L); | 886 | lua_close(lane_->L); |
887 | lane->L = nullptr; // just in case | 887 | lane_->L = nullptr; // just in case |
888 | lane->U->selfdestruct_cs.lock(); | 888 | lane_->U->selfdestruct_cs.lock(); |
889 | // done with lua_close(), terminal shutdown sequence may proceed | 889 | // done with lua_close(), terminal shutdown sequence may proceed |
890 | lane->U->selfdestructing_count.fetch_sub(1, std::memory_order_release); | 890 | lane_->U->selfdestructing_count.fetch_sub(1, std::memory_order_release); |
891 | lane->U->selfdestruct_cs.unlock(); | 891 | lane_->U->selfdestruct_cs.unlock(); |
892 | 892 | ||
893 | // we destroy our jthread member from inside the thread body, so we have to detach so that we don't try to join, as this doesn't seem a good idea | 893 | // we destroy our jthread member from inside the thread body, so we have to detach so that we don't try to join, as this doesn't seem a good idea |
894 | lane->m_thread.detach(); | 894 | lane_->m_thread.detach(); |
895 | delete lane; | 895 | delete lane_; |
896 | lane = nullptr; | 896 | lane_ = nullptr; |
897 | } | 897 | } |
898 | } | 898 | } |
899 | if (lane) | 899 | if (lane_) |
900 | { | 900 | { |
901 | // leave results (1..top) or error message + stack trace (1..2) on the stack - master will copy them | 901 | // leave results (1..top) or error message + stack trace (1..2) on the stack - master will copy them |
902 | 902 | ||
@@ -904,9 +904,9 @@ static void lane_main(Lane* lane) | |||
904 | 904 | ||
905 | { | 905 | { |
906 | // 'm_done_mutex' protects the -> Done|Error|Cancelled state change | 906 | // 'm_done_mutex' protects the -> Done|Error|Cancelled state change |
907 | std::lock_guard lock{ lane->m_done_mutex }; | 907 | std::lock_guard lock{ lane_->m_done_mutex }; |
908 | lane->m_status = st; | 908 | lane_->m_status = st; |
909 | lane->m_done_signal.notify_one();// wake up master (while 'lane->m_done_mutex' is on) | 909 | lane_->m_done_signal.notify_one();// wake up master (while 'lane_->m_done_mutex' is on) |
910 | } | 910 | } |
911 | } | 911 | } |
912 | } | 912 | } |
@@ -946,7 +946,7 @@ LUAG_FUNC(register) | |||
946 | // ignore extra parameters, just in case | 946 | // ignore extra parameters, just in case |
947 | lua_settop(L_, 2); | 947 | lua_settop(L_, 2); |
948 | luaL_argcheck(L_, (mod_type == LuaType::TABLE) || (mod_type == LuaType::FUNCTION), 2, "unexpected module type"); | 948 | luaL_argcheck(L_, (mod_type == LuaType::TABLE) || (mod_type == LuaType::FUNCTION), 2, "unexpected module type"); |
949 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); | 949 | DEBUGSPEW_CODE(Universe* U = universe_get(L_)); |
950 | STACK_CHECK_START_REL(L_, 0); // "name" mod_table | 950 | STACK_CHECK_START_REL(L_, 0); // "name" mod_table |
951 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name)); | 951 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name)); |
952 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 952 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
@@ -1747,7 +1747,7 @@ LUAG_FUNC(configure) | |||
1747 | STACK_GROW(L_, 4); | 1747 | STACK_GROW(L_, 4); |
1748 | STACK_CHECK_START_ABS(L_, 1); // settings | 1748 | STACK_CHECK_START_ABS(L_, 1); // settings |
1749 | 1749 | ||
1750 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L)); | 1750 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L_)); |
1751 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1751 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1752 | 1752 | ||
1753 | if (U == nullptr) | 1753 | if (U == nullptr) |
@@ -1898,7 +1898,7 @@ LUAG_FUNC(configure) | |||
1898 | // set _R[kConfigRegKey] = settings | 1898 | // set _R[kConfigRegKey] = settings |
1899 | kConfigRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); | 1899 | kConfigRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); |
1900 | STACK_CHECK(L_, 1); | 1900 | STACK_CHECK(L_, 1); |
1901 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L)); | 1901 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L_)); |
1902 | // Return the settings table | 1902 | // Return the settings table |
1903 | return 1; | 1903 | return 1; |
1904 | } | 1904 | } |
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index dfde550..5d1467a 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h | |||
@@ -78,13 +78,13 @@ template <typename... ARGS> | |||
78 | 78 | ||
79 | #ifdef NDEBUG | 79 | #ifdef NDEBUG |
80 | 80 | ||
81 | #define LUA_ASSERT(L, c) ; // nothing | 81 | #define LUA_ASSERT(L_, c) ; // nothing |
82 | 82 | ||
83 | #define STACK_CHECK_START_REL(L, offset_) | 83 | #define STACK_CHECK_START_REL(L_, offset_) |
84 | #define STACK_CHECK_START_ABS(L, offset_) | 84 | #define STACK_CHECK_START_ABS(L_, offset_) |
85 | #define STACK_CHECK_RESET_REL(L, offset_) | 85 | #define STACK_CHECK_RESET_REL(L_, offset_) |
86 | #define STACK_CHECK_RESET_ABS(L, offset_) | 86 | #define STACK_CHECK_RESET_ABS(L_, offset_) |
87 | #define STACK_CHECK(L, offset_) | 87 | #define STACK_CHECK(L_, offset_) |
88 | 88 | ||
89 | #else // NDEBUG | 89 | #else // NDEBUG |
90 | 90 | ||
diff --git a/src/state.cpp b/src/state.cpp index d6dfb89..e71865d 100644 --- a/src/state.cpp +++ b/src/state.cpp | |||
@@ -52,21 +52,21 @@ THE SOFTWARE. | |||
52 | [[nodiscard]] static int luaG_new_require(lua_State* L_) | 52 | [[nodiscard]] static int luaG_new_require(lua_State* L_) |
53 | { | 53 | { |
54 | int rc; | 54 | int rc; |
55 | int const args = lua_gettop( L_); // args | 55 | int const args = lua_gettop(L_); // args |
56 | Universe* U = universe_get( L_); | 56 | Universe* U = universe_get(L_); |
57 | //char const* modname = luaL_checkstring( L, 1); | 57 | //char const* modname = luaL_checkstring(L_, 1); |
58 | 58 | ||
59 | STACK_GROW( L_, 1); | 59 | STACK_GROW(L_, 1); |
60 | 60 | ||
61 | lua_pushvalue( L_, lua_upvalueindex( 1)); // args require | 61 | lua_pushvalue(L_, lua_upvalueindex( 1)); // args require |
62 | lua_insert( L_, 1); // require args | 62 | lua_insert(L_, 1); // require args |
63 | 63 | ||
64 | // Using 'lua_pcall()' to catch errors; otherwise a failing 'require' would | 64 | // Using 'lua_pcall()' to catch errors; otherwise a failing 'require' would |
65 | // leave us locked, blocking any future 'require' calls from other lanes. | 65 | // leave us locked, blocking any future 'require' calls from other lanes. |
66 | 66 | ||
67 | U->require_cs.lock(); | 67 | U->require_cs.lock(); |
68 | // starting with Lua 5.4, require may return a second optional value, so we need LUA_MULTRET | 68 | // starting with Lua 5.4, require may return a second optional value, so we need LUA_MULTRET |
69 | rc = lua_pcall( L_, args, LUA_MULTRET, 0 /*errfunc*/ ); // err|result(s) | 69 | rc = lua_pcall(L_, args, LUA_MULTRET, 0 /*errfunc*/ ); // err|result(s) |
70 | U->require_cs.unlock(); | 70 | U->require_cs.unlock(); |
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 |
@@ -76,7 +76,7 @@ THE SOFTWARE. | |||
76 | raise_lua_error(L_); | 76 | raise_lua_error(L_); |
77 | } | 77 | } |
78 | // should be 1 for Lua <= 5.3, 1 or 2 starting with Lua 5.4 | 78 | // should be 1 for Lua <= 5.3, 1 or 2 starting with Lua 5.4 |
79 | return lua_gettop(L_); // result(s) | 79 | return lua_gettop(L_); // result(s) |
80 | } | 80 | } |
81 | 81 | ||
82 | // ################################################################################################# | 82 | // ################################################################################################# |
diff --git a/src/tools.cpp b/src/tools.cpp index e11cad5..7b2d153 100644 --- a/src/tools.cpp +++ b/src/tools.cpp | |||
@@ -305,7 +305,7 @@ static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L_ | |||
305 | // based on some sorting order so that we end up with the same name in all databases whatever order the table walk yielded | 305 | // based on some sorting order so that we end up with the same name in all databases whatever order the table walk yielded |
306 | if (prevName != nullptr && (prevNameLength < newNameLength || lua_lessthan(L_, -2, -1))) | 306 | if (prevName != nullptr && (prevNameLength < newNameLength || lua_lessthan(L_, -2, -1))) |
307 | { | 307 | { |
308 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%s '%s' remained named '%s'\n" INDENT_END, lua_typename( L, lua_type( L, -3)), newName, prevName)); | 308 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s '%s' remained named '%s'\n" INDENT_END, lua_typename(L_, lua_type(L_, -3)), newName, prevName)); |
309 | // the previous name is 'smaller' than the one we just generated: keep it! | 309 | // the previous name is 'smaller' than the one we just generated: keep it! |
310 | lua_pop(L_, 3); // ... {bfc} k | 310 | lua_pop(L_, 3); // ... {bfc} k |
311 | } | 311 | } |
@@ -324,7 +324,7 @@ static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L_ | |||
324 | { | 324 | { |
325 | lua_remove(L_, -2); // ... {bfc} k o "f.q.n" | 325 | lua_remove(L_, -2); // ... {bfc} k o "f.q.n" |
326 | } | 326 | } |
327 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%s '%s'\n" INDENT_END, lua_typename(L, lua_type( L, -2)), newName)); | 327 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s '%s'\n" INDENT_END, lua_typename(L_, lua_type(L_, -2)), newName)); |
328 | // prepare the stack for database feed | 328 | // prepare the stack for database feed |
329 | lua_pushvalue(L_, -1); // ... {bfc} k o "f.q.n" "f.q.n" | 329 | lua_pushvalue(L_, -1); // ... {bfc} k o "f.q.n" "f.q.n" |
330 | lua_pushvalue(L_, -3); // ... {bfc} k o "f.q.n" "f.q.n" o | 330 | lua_pushvalue(L_, -3); // ... {bfc} k o "f.q.n" "f.q.n" o |
@@ -428,7 +428,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) | |||
428 | lua_pushnil(L_); // ... {_i} {bfc} nil | 428 | lua_pushnil(L_); // ... {_i} {bfc} nil |
429 | while (lua_next(L_, breadth_first_cache) != 0) // ... {_i} {bfc} k {} | 429 | while (lua_next(L_, breadth_first_cache) != 0) // ... {_i} {bfc} k {} |
430 | { | 430 | { |
431 | DEBUGSPEW_CODE(char const* key = (lua_type(L, -2) == LUA_TSTRING) ? lua_tostring(L, -2) : "not a string"); | 431 | DEBUGSPEW_CODE(char const* key = (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostring(L_, -2) : "not a string"); |
432 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "table '%s'\n" INDENT_END, key)); | 432 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "table '%s'\n" INDENT_END, key)); |
433 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 433 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
434 | // un-visit this table in case we do need to process it | 434 | // un-visit this table in case we do need to process it |
@@ -474,8 +474,8 @@ void populate_func_lookup_table(lua_State* L_, int i_, char const* name_) | |||
474 | int const ctx_base = lua_gettop(L_) + 1; | 474 | int const ctx_base = lua_gettop(L_) + 1; |
475 | int const in_base = lua_absindex(L_, i_); | 475 | int const in_base = lua_absindex(L_, i_); |
476 | int start_depth = 0; | 476 | int start_depth = 0; |
477 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); | 477 | DEBUGSPEW_CODE(Universe* U = universe_get(L_)); |
478 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, name_ ? name_ : "nullptr")); | 478 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L_, name_ ? name_ : "nullptr")); |
479 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 479 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
480 | STACK_GROW(L_, 3); | 480 | STACK_GROW(L_, 3); |
481 | STACK_CHECK_START_REL(L_, 0); | 481 | STACK_CHECK_START_REL(L_, 0); |
@@ -1723,7 +1723,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
1723 | return true; | 1723 | return true; |
1724 | } | 1724 | } |
1725 | 1725 | ||
1726 | /* Check if we've already copied the same table from 'L' (during this transmission), and | 1726 | /* Check if we've already copied the same table from 'L1' (during this transmission), and |
1727 | * reuse the old copy. This allows table upvalues shared by multiple | 1727 | * reuse the old copy. This allows table upvalues shared by multiple |
1728 | * local functions to point to the same table, also in the target. | 1728 | * local functions to point to the same table, also in the target. |
1729 | * Also, this takes care of cyclic tables and multiple references | 1729 | * Also, this takes care of cyclic tables and multiple references |