diff options
Diffstat (limited to 'src/intercopycontext.cpp')
-rw-r--r-- | src/intercopycontext.cpp | 399 |
1 files changed, 199 insertions, 200 deletions
diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 07fcd77..10d620e 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp | |||
@@ -37,11 +37,11 @@ THE SOFTWARE. | |||
37 | // luckily, this also works with earlier Lua versions | 37 | // luckily, this also works with earlier Lua versions |
38 | [[nodiscard]] static int buf_writer(lua_State* L_, void const* b_, size_t size_, void* ud_) | 38 | [[nodiscard]] static int buf_writer(lua_State* L_, void const* b_, size_t size_, void* ud_) |
39 | { | 39 | { |
40 | luaL_Buffer* const B{ static_cast<luaL_Buffer*>(ud_) }; | 40 | luaL_Buffer* const _B{ static_cast<luaL_Buffer*>(ud_) }; |
41 | if (!B->L) { | 41 | if (!_B->L) { |
42 | luaL_buffinit(L_, B); | 42 | luaL_buffinit(L_, _B); |
43 | } | 43 | } |
44 | luaL_addlstring(B, static_cast<char const*>(b_), size_); | 44 | luaL_addlstring(_B, static_cast<char const*>(b_), size_); |
45 | return 0; | 45 | return 0; |
46 | } | 46 | } |
47 | 47 | ||
@@ -78,12 +78,12 @@ THE SOFTWARE. | |||
78 | STACK_CHECK_START_REL(L_, 0); | 78 | STACK_CHECK_START_REL(L_, 0); |
79 | STACK_GROW(L_, 3); // up to 3 slots are necessary on error | 79 | STACK_GROW(L_, 3); // up to 3 slots are necessary on error |
80 | if (mode_ == LookupMode::FromKeeper) { | 80 | if (mode_ == LookupMode::FromKeeper) { |
81 | lua_CFunction f = lua_tocfunction(L_, i_); // should *always* be one of the function sentinels | 81 | lua_CFunction const _f{ lua_tocfunction(L_, i_) }; // should *always* be one of the function sentinels |
82 | if (f == func_lookup_sentinel || f == table_lookup_sentinel || f == userdata_clone_sentinel) { | 82 | if (_f == func_lookup_sentinel || _f == table_lookup_sentinel || _f == userdata_clone_sentinel) { |
83 | lua_getupvalue(L_, i_, 1); // L_: ... v ... "f.q.n" | 83 | lua_getupvalue(L_, i_, 1); // L_: ... v ... "f.q.n" |
84 | } else { | 84 | } else { |
85 | // if this is not a sentinel, this is some user-created table we wanted to lookup | 85 | // if this is not a sentinel, this is some user-created table we wanted to lookup |
86 | LUA_ASSERT(L_, nullptr == f && lua_istable(L_, i_)); | 86 | LUA_ASSERT(L_, nullptr == _f && lua_istable(L_, i_)); |
87 | // push anything that will convert to nullptr string | 87 | // push anything that will convert to nullptr string |
88 | lua_pushnil(L_); // L_: ... v ... nil | 88 | lua_pushnil(L_); // L_: ... v ... nil |
89 | } | 89 | } |
@@ -95,13 +95,13 @@ THE SOFTWARE. | |||
95 | lua_pushvalue(L_, i_); // L_: ... v ... {} v | 95 | lua_pushvalue(L_, i_); // L_: ... v ... {} v |
96 | lua_rawget(L_, -2); // L_: ... v ... {} "f.q.n" | 96 | lua_rawget(L_, -2); // L_: ... v ... {} "f.q.n" |
97 | } | 97 | } |
98 | char const* fqn{ lua_tolstring(L_, -1, len_) }; | 98 | char const* _fqn{ lua_tolstring(L_, -1, len_) }; |
99 | DEBUGSPEW_CODE(Universe* const U = universe_get(L_)); | 99 | DEBUGSPEW_CODE(Universe* const U = universe_get(L_)); |
100 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "function [C] %s \n" INDENT_END(U), fqn)); | 100 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "function [C] %s \n" INDENT_END(U), _fqn)); |
101 | // popping doesn't invalidate the pointer since this is an interned string gotten from the lookup database | 101 | // popping doesn't invalidate the pointer since this is an interned string gotten from the lookup database |
102 | lua_pop(L_, (mode_ == LookupMode::FromKeeper) ? 1 : 2); // L_: ... v ... | 102 | lua_pop(L_, (mode_ == LookupMode::FromKeeper) ? 1 : 2); // L_: ... v ... |
103 | STACK_CHECK(L_, 0); | 103 | STACK_CHECK(L_, 0); |
104 | if (nullptr == fqn && !lua_istable(L_, i_)) { // raise an error if we try to send an unknown function (but not for tables) | 104 | if (nullptr == _fqn && !lua_istable(L_, i_)) { // raise an error if we try to send an unknown function (but not for tables) |
105 | *len_ = 0; // just in case | 105 | *len_ = 0; // just in case |
106 | // try to discover the name of the function we want to send | 106 | // try to discover the name of the function we want to send |
107 | lua_getglobal(L_, "decoda_name"); // L_: ... v ... decoda_name | 107 | lua_getglobal(L_, "decoda_name"); // L_: ... v ... decoda_name |
@@ -125,7 +125,7 @@ THE SOFTWARE. | |||
125 | raise_luaL_error(L_, "%s%s '%s' not found in %s origin transfer database.%s", typewhat, gotchaA, what, from ? from : "main", gotchaB); | 125 | raise_luaL_error(L_, "%s%s '%s' not found in %s origin transfer database.%s", typewhat, gotchaA, what, from ? from : "main", gotchaB); |
126 | } | 126 | } |
127 | STACK_CHECK(L_, 0); | 127 | STACK_CHECK(L_, 0); |
128 | return fqn; | 128 | return _fqn; |
129 | } | 129 | } |
130 | 130 | ||
131 | // ################################################################################################# | 131 | // ################################################################################################# |
@@ -147,26 +147,26 @@ static constexpr RegistryUniqueKey kMtIdRegKey{ 0xA8895DCF4EC3FE3Cull }; | |||
147 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} | 147 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} |
148 | lua_rawget(L_, -2); // L_: ... _R[kMtIdRegKey] mtk? | 148 | lua_rawget(L_, -2); // L_: ... _R[kMtIdRegKey] mtk? |
149 | 149 | ||
150 | lua_Integer id{ lua_tointeger(L_, -1) }; // 0 for nil | 150 | lua_Integer _id{ lua_tointeger(L_, -1) }; // 0 for nil |
151 | lua_pop(L_, 1); // L_: ... _R[kMtIdRegKey] | 151 | lua_pop(L_, 1); // L_: ... _R[kMtIdRegKey] |
152 | STACK_CHECK(L_, 1); | 152 | STACK_CHECK(L_, 1); |
153 | 153 | ||
154 | if (id == 0) { | 154 | if (_id == 0) { |
155 | id = U_->nextMetatableId.fetch_add(1, std::memory_order_relaxed); | 155 | _id = U_->nextMetatableId.fetch_add(1, std::memory_order_relaxed); |
156 | 156 | ||
157 | // Create two-way references: id_uint <-> table | 157 | // Create two-way references: id_uint <-> table |
158 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} | 158 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} |
159 | lua_pushinteger(L_, id); // L_: ... _R[kMtIdRegKey] {mt} id | 159 | lua_pushinteger(L_, _id); // L_: ... _R[kMtIdRegKey] {mt} id |
160 | lua_rawset(L_, -3); // L_: ... _R[kMtIdRegKey] | 160 | lua_rawset(L_, -3); // L_: ... _R[kMtIdRegKey] |
161 | 161 | ||
162 | lua_pushinteger(L_, id); // L_: ... _R[kMtIdRegKey] id | 162 | lua_pushinteger(L_, _id); // L_: ... _R[kMtIdRegKey] id |
163 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] id {mt} | 163 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] id {mt} |
164 | lua_rawset(L_, -3); // L_: ... _R[kMtIdRegKey] | 164 | lua_rawset(L_, -3); // L_: ... _R[kMtIdRegKey] |
165 | } | 165 | } |
166 | lua_pop(L_, 1); // L_: ... | 166 | lua_pop(L_, 1); // L_: ... |
167 | STACK_CHECK(L_, 0); | 167 | STACK_CHECK(L_, 0); |
168 | 168 | ||
169 | return id; | 169 | return _id; |
170 | } | 170 | } |
171 | 171 | ||
172 | // ################################################################################################# | 172 | // ################################################################################################# |
@@ -181,8 +181,8 @@ void InterCopyContext::copy_func() const | |||
181 | 181 | ||
182 | // 'lua_dump()' needs the function at top of stack | 182 | // 'lua_dump()' needs the function at top of stack |
183 | // if already on top of the stack, no need to push again | 183 | // if already on top of the stack, no need to push again |
184 | bool const needToPush{ L1_i != lua_gettop(L1) }; | 184 | bool const _needToPush{ L1_i != lua_gettop(L1) }; |
185 | if (needToPush) { | 185 | if (_needToPush) { |
186 | lua_pushvalue(L1, L1_i); // L1: ... f | 186 | lua_pushvalue(L1, L1_i); // L1: ... f |
187 | } | 187 | } |
188 | 188 | ||
@@ -191,8 +191,7 @@ void InterCopyContext::copy_func() const | |||
191 | // to the writer" (and we only return 0) | 191 | // to the writer" (and we only return 0) |
192 | // not sure this could ever fail but for memory shortage reasons | 192 | // not sure this could ever fail but for memory shortage reasons |
193 | // last parameter is Lua 5.4-specific (no stripping) | 193 | // last parameter is Lua 5.4-specific (no stripping) |
194 | luaL_Buffer B; | 194 | luaL_Buffer B{}; |
195 | B.L = nullptr; | ||
196 | if (lua504_dump(L1, buf_writer, &B, 0) != 0) { | 195 | if (lua504_dump(L1, buf_writer, &B, 0) != 0) { |
197 | raise_luaL_error(getErrL(), "internal error: function dump failed."); | 196 | raise_luaL_error(getErrL(), "internal error: function dump failed."); |
198 | } | 197 | } |
@@ -201,7 +200,7 @@ void InterCopyContext::copy_func() const | |||
201 | luaL_pushresult(&B); // L1: ... f b | 200 | luaL_pushresult(&B); // L1: ... f b |
202 | 201 | ||
203 | // if not pushed, no need to pop | 202 | // if not pushed, no need to pop |
204 | if (needToPush) { | 203 | if (_needToPush) { |
205 | lua_remove(L1, -2); // L1: ... b | 204 | lua_remove(L1, -2); // L1: ... b |
206 | } | 205 | } |
207 | 206 | ||
@@ -214,18 +213,18 @@ void InterCopyContext::copy_func() const | |||
214 | // stack and start the what string with the character '>'." | 213 | // stack and start the what string with the character '>'." |
215 | // | 214 | // |
216 | { | 215 | { |
217 | lua_Debug ar; | 216 | lua_Debug _ar; |
218 | lua_pushvalue(L1, L1_i); // L1: ... b f | 217 | lua_pushvalue(L1, L1_i); // L1: ... b f |
219 | // fills 'fname' 'namewhat' and 'linedefined', pops function | 218 | // fills 'fname' 'namewhat' and 'linedefined', pops function |
220 | lua_getinfo(L1, ">nS", &ar); // L1: ... b | 219 | lua_getinfo(L1, ">nS", &_ar); // L1: ... b |
221 | fname = ar.namewhat; | 220 | fname = _ar.namewhat; |
222 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "FNAME: %s @ %d" INDENT_END(U), ar.short_src, ar.linedefined)); // just gives nullptr | 221 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "FNAME: %s @ %d" INDENT_END(U), _ar.short_src, _ar.linedefined)); // just gives nullptr |
223 | } | 222 | } |
224 | #endif // LOG_FUNC_INFO | 223 | #endif // LOG_FUNC_INFO |
225 | { | 224 | { |
226 | size_t sz; | 225 | size_t _sz; |
227 | char const* s = lua_tolstring(L1, -1, &sz); // L1: ... b | 226 | char const* _s{ lua_tolstring(L1, -1, &_sz) }; // L1: ... b |
228 | LUA_ASSERT(L1, s && sz); | 227 | LUA_ASSERT(L1, _s && _sz); |
229 | STACK_GROW(L2, 2); | 228 | STACK_GROW(L2, 2); |
230 | // Note: Line numbers seem to be taken precisely from the | 229 | // Note: Line numbers seem to be taken precisely from the |
231 | // original function. 'fname' is not used since the chunk | 230 | // original function. 'fname' is not used since the chunk |
@@ -233,7 +232,7 @@ void InterCopyContext::copy_func() const | |||
233 | // | 232 | // |
234 | // TBD: Can we get the function's original name through, as well? | 233 | // TBD: Can we get the function's original name through, as well? |
235 | // | 234 | // |
236 | if (luaL_loadbuffer(L2, s, sz, fname) != 0) { // L2: ... {cache} ... p function | 235 | if (luaL_loadbuffer(L2, _s, _sz, fname) != 0) { // L2: ... {cache} ... p function |
237 | // chunk is precompiled so only LUA_ERRMEM can happen | 236 | // chunk is precompiled so only LUA_ERRMEM can happen |
238 | // "Otherwise, it pushes an error message" | 237 | // "Otherwise, it pushes an error message" |
239 | // | 238 | // |
@@ -259,15 +258,15 @@ void InterCopyContext::copy_func() const | |||
259 | */ | 258 | */ |
260 | int n{ 0 }; | 259 | int n{ 0 }; |
261 | { | 260 | { |
262 | InterCopyContext c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, {} }; | 261 | InterCopyContext _c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, {} }; |
263 | #if LUA_VERSION_NUM >= 502 | 262 | #if LUA_VERSION_NUM >= 502 |
264 | // Starting with Lua 5.2, each Lua function gets its environment as one of its upvalues (named LUA_ENV, aka "_ENV" by default) | 263 | // Starting with Lua 5.2, each Lua function gets its environment as one of its upvalues (named LUA_ENV, aka "_ENV" by default) |
265 | // Generally this is LUA_RIDX_GLOBALS, which we don't want to copy from the source to the destination state... | 264 | // Generally this is LUA_RIDX_GLOBALS, which we don't want to copy from the source to the destination state... |
266 | // -> if we encounter an upvalue equal to the global table in the source, bind it to the destination's global table | 265 | // -> if we encounter an upvalue equal to the global table in the source, bind it to the destination's global table |
267 | lua_pushglobaltable(L1); // L1: ... _G | 266 | lua_pushglobaltable(L1); // L1: ... _G |
268 | #endif // LUA_VERSION_NUM | 267 | #endif // LUA_VERSION_NUM |
269 | for (n = 0; (c.name = lua_getupvalue(L1, L1_i, 1 + n)) != nullptr; ++n) { // L1: ... _G up[n] | 268 | for (n = 0; (_c.name = lua_getupvalue(L1, L1_i, 1 + n)) != nullptr; ++n) { // L1: ... _G up[n] |
270 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "UPNAME[%d]: %s -> " INDENT_END(U), n, c.name)); | 269 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "UPNAME[%d]: %s -> " INDENT_END(U), n, _c.name)); |
271 | #if LUA_VERSION_NUM >= 502 | 270 | #if LUA_VERSION_NUM >= 502 |
272 | if (lua_rawequal(L1, -1, -2)) { // is the upvalue equal to the global table? | 271 | if (lua_rawequal(L1, -1, -2)) { // is the upvalue equal to the global table? |
273 | DEBUGSPEW_CODE(fprintf(stderr, "pushing destination global scope\n")); | 272 | DEBUGSPEW_CODE(fprintf(stderr, "pushing destination global scope\n")); |
@@ -276,8 +275,8 @@ void InterCopyContext::copy_func() const | |||
276 | #endif // LUA_VERSION_NUM | 275 | #endif // LUA_VERSION_NUM |
277 | { | 276 | { |
278 | DEBUGSPEW_CODE(fprintf(stderr, "copying value\n")); | 277 | DEBUGSPEW_CODE(fprintf(stderr, "copying value\n")); |
279 | c.L1_i = SourceIndex{ lua_gettop(L1) }; | 278 | _c.L1_i = SourceIndex{ lua_gettop(L1) }; |
280 | if (!c.inter_copy_one()) { // L2: ... {cache} ... function <upvalues> | 279 | if (!_c.inter_copy_one()) { // L2: ... {cache} ... function <upvalues> |
281 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); | 280 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); |
282 | } | 281 | } |
283 | } | 282 | } |
@@ -292,13 +291,13 @@ void InterCopyContext::copy_func() const | |||
292 | STACK_CHECK(L1, 0); | 291 | STACK_CHECK(L1, 0); |
293 | 292 | ||
294 | // Set upvalues (originally set to 'nil' by 'lua_load') | 293 | // Set upvalues (originally set to 'nil' by 'lua_load') |
295 | for (int const func_index{ lua_gettop(L2) - n }; n > 0; --n) { | 294 | for (int const _func_index{ lua_gettop(L2) - n }; n > 0; --n) { |
296 | char const* rc{ lua_setupvalue(L2, func_index, n) }; // L2: ... {cache} ... function | 295 | char const* _rc{ lua_setupvalue(L2, _func_index, n) }; // L2: ... {cache} ... function |
297 | // | 296 | // |
298 | // "assigns the value at the top of the stack to the upvalue and returns its name. | 297 | // "assigns the value at the top of the stack to the upvalue and returns its name. |
299 | // It also pops the value from the stack." | 298 | // It also pops the value from the stack." |
300 | 299 | ||
301 | LUA_ASSERT(L1, rc); // not having enough slots? | 300 | LUA_ASSERT(L1, _rc); // not having enough slots? |
302 | } | 301 | } |
303 | // once all upvalues have been set we are left | 302 | // once all upvalues have been set we are left |
304 | // with the function at the top of the stack // L2: ... {cache} ... function | 303 | // with the function at the top of the stack // L2: ... {cache} ... function |
@@ -312,8 +311,8 @@ void InterCopyContext::copy_func() const | |||
312 | void InterCopyContext::lookup_native_func() const | 311 | void InterCopyContext::lookup_native_func() const |
313 | { | 312 | { |
314 | // get the name of the function we want to send | 313 | // get the name of the function we want to send |
315 | size_t len; | 314 | size_t _len; |
316 | char const* const fqn{ find_lookup_name(L1, L1_i, mode, name, &len) }; | 315 | char const* const _fqn{ find_lookup_name(L1, L1_i, mode, name, &_len) }; |
317 | // push the equivalent function in the destination's stack, retrieved from the lookup table | 316 | // push the equivalent function in the destination's stack, retrieved from the lookup table |
318 | STACK_CHECK_START_REL(L2, 0); | 317 | STACK_CHECK_START_REL(L2, 0); |
319 | STACK_GROW(L2, 3); // up to 3 slots are necessary on error | 318 | STACK_GROW(L2, 3); // up to 3 slots are necessary on error |
@@ -324,7 +323,7 @@ void InterCopyContext::lookup_native_func() const | |||
324 | 323 | ||
325 | case LookupMode::ToKeeper: | 324 | case LookupMode::ToKeeper: |
326 | // push a sentinel closure that holds the lookup name as upvalue | 325 | // push a sentinel closure that holds the lookup name as upvalue |
327 | lua_pushlstring(L2, fqn, len); // L1: ... f ... L2: "f.q.n" | 326 | lua_pushlstring(L2, _fqn, _len); // L1: ... f ... L2: "f.q.n" |
328 | lua_pushcclosure(L2, func_lookup_sentinel, 1); // L1: ... f ... L2: f | 327 | lua_pushcclosure(L2, func_lookup_sentinel, 1); // L1: ... f ... L2: f |
329 | break; | 328 | break; |
330 | 329 | ||
@@ -333,25 +332,25 @@ void InterCopyContext::lookup_native_func() const | |||
333 | kLookupRegKey.pushValue(L2); // L1: ... f ... L2: {} | 332 | kLookupRegKey.pushValue(L2); // L1: ... f ... L2: {} |
334 | STACK_CHECK(L2, 1); | 333 | STACK_CHECK(L2, 1); |
335 | LUA_ASSERT(L1, lua_istable(L2, -1)); | 334 | LUA_ASSERT(L1, lua_istable(L2, -1)); |
336 | lua_pushlstring(L2, fqn, len); // L1: ... f ... L2: {} "f.q.n" | 335 | lua_pushlstring(L2, _fqn, _len); // L1: ... f ... L2: {} "f.q.n" |
337 | lua_rawget(L2, -2); // L1: ... f ... L2: {} f | 336 | lua_rawget(L2, -2); // L1: ... f ... L2: {} f |
338 | // nil means we don't know how to transfer stuff: user should do something | 337 | // nil means we don't know how to transfer stuff: user should do something |
339 | // anything other than function or table should not happen! | 338 | // anything other than function or table should not happen! |
340 | if (!lua_isfunction(L2, -1) && !lua_istable(L2, -1)) { | 339 | if (!lua_isfunction(L2, -1) && !lua_istable(L2, -1)) { |
341 | lua_getglobal(L1, "decoda_name"); // L1: ... f ... decoda_name | 340 | lua_getglobal(L1, "decoda_name"); // L1: ... f ... decoda_name |
342 | char const* const from{ lua_tostring(L1, -1) }; | 341 | char const* const _from{ lua_tostring(L1, -1) }; |
343 | lua_pop(L1, 1); // L1: ... f ... | 342 | lua_pop(L1, 1); // L1: ... f ... |
344 | lua_getglobal(L2, "decoda_name"); // L1: ... f ... L2: {} f decoda_name | 343 | lua_getglobal(L2, "decoda_name"); // L1: ... f ... L2: {} f decoda_name |
345 | char const* const to{ lua_tostring(L2, -1) }; | 344 | char const* const _to{ lua_tostring(L2, -1) }; |
346 | lua_pop(L2, 1); // L2: {} f | 345 | lua_pop(L2, 1); // L2: {} f |
347 | // 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 | 346 | // 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 |
348 | raise_luaL_error( | 347 | raise_luaL_error( |
349 | getErrL(), | 348 | getErrL(), |
350 | "%s%s: function '%s' not found in %s destination transfer database.", | 349 | "%s%s: function '%s' not found in %s destination transfer database.", |
351 | lua_isnil(L2, -1) ? "" : "INTERNAL ERROR IN ", | 350 | lua_isnil(L2, -1) ? "" : "INTERNAL ERROR IN ", |
352 | from ? from : "main", | 351 | _from ? _from : "main", |
353 | fqn, | 352 | _fqn, |
354 | to ? to : "main"); | 353 | _to ? _to : "main"); |
355 | return; | 354 | return; |
356 | } | 355 | } |
357 | lua_remove(L2, -2); // L2: f | 356 | lua_remove(L2, -2); // L2: f |
@@ -381,10 +380,10 @@ void InterCopyContext::lookup_native_func() const | |||
381 | // Always pushes a function to 'L2'. | 380 | // Always pushes a function to 'L2'. |
382 | void InterCopyContext::copy_cached_func() const | 381 | void InterCopyContext::copy_cached_func() const |
383 | { | 382 | { |
384 | FuncSubType const funcSubType{ luaG_getfuncsubtype(L1, L1_i) }; | 383 | FuncSubType const _funcSubType{ luaG_getfuncsubtype(L1, L1_i) }; |
385 | if (funcSubType == FuncSubType::Bytecode) { | 384 | if (_funcSubType == FuncSubType::Bytecode) { |
386 | void* const aspointer = const_cast<void*>(lua_topointer(L1, L1_i)); | 385 | void* const _aspointer{ const_cast<void*>(lua_topointer(L1, L1_i)) }; |
387 | // TBD: Merge this and same code for tables | 386 | // TODO: Merge this and same code for tables |
388 | LUA_ASSERT(L1, L2_cache_i != 0); | 387 | LUA_ASSERT(L1, L2_cache_i != 0); |
389 | 388 | ||
390 | STACK_GROW(L2, 2); | 389 | STACK_GROW(L2, 2); |
@@ -397,7 +396,7 @@ void InterCopyContext::copy_cached_func() const | |||
397 | // is only for the duration of a copy (both states are locked). | 396 | // is only for the duration of a copy (both states are locked). |
398 | 397 | ||
399 | // push a light userdata uniquely representing the function | 398 | // push a light userdata uniquely representing the function |
400 | lua_pushlightuserdata(L2, aspointer); // L2: ... {cache} ... p | 399 | lua_pushlightuserdata(L2, _aspointer); // L2: ... {cache} ... p |
401 | 400 | ||
402 | // fprintf( stderr, "<< ID: %s >>\n", lua_tostring( L2, -1)); | 401 | // fprintf( stderr, "<< ID: %s >>\n", lua_tostring( L2, -1)); |
403 | 402 | ||
@@ -430,9 +429,9 @@ void InterCopyContext::copy_cached_func() const | |||
430 | [[nodiscard]] bool InterCopyContext::lookup_table() const | 429 | [[nodiscard]] bool InterCopyContext::lookup_table() const |
431 | { | 430 | { |
432 | // get the name of the table we want to send | 431 | // get the name of the table we want to send |
433 | size_t len; | 432 | size_t _len; |
434 | char const* fqn = find_lookup_name(L1, L1_i, mode, name, &len); | 433 | char const* const _fqn{ find_lookup_name(L1, L1_i, mode, name, &_len) }; |
435 | if (nullptr == fqn) { // name not found, it is some user-created table | 434 | if (nullptr == _fqn) { // name not found, it is some user-created table |
436 | return false; | 435 | return false; |
437 | } | 436 | } |
438 | // push the equivalent table in the destination's stack, retrieved from the lookup table | 437 | // push the equivalent table in the destination's stack, retrieved from the lookup table |
@@ -445,7 +444,7 @@ void InterCopyContext::copy_cached_func() const | |||
445 | 444 | ||
446 | case LookupMode::ToKeeper: | 445 | case LookupMode::ToKeeper: |
447 | // push a sentinel closure that holds the lookup name as upvalue | 446 | // push a sentinel closure that holds the lookup name as upvalue |
448 | lua_pushlstring(L2, fqn, len); // L1: ... t ... L2: "f.q.n" | 447 | lua_pushlstring(L2, _fqn, _len); // L1: ... t ... L2: "f.q.n" |
449 | lua_pushcclosure(L2, table_lookup_sentinel, 1); // L1: ... t ... L2: f | 448 | lua_pushcclosure(L2, table_lookup_sentinel, 1); // L1: ... t ... L2: f |
450 | break; | 449 | break; |
451 | 450 | ||
@@ -454,7 +453,7 @@ void InterCopyContext::copy_cached_func() const | |||
454 | kLookupRegKey.pushValue(L2); // L1: ... t ... L2: {} | 453 | kLookupRegKey.pushValue(L2); // L1: ... t ... L2: {} |
455 | STACK_CHECK(L2, 1); | 454 | STACK_CHECK(L2, 1); |
456 | LUA_ASSERT(L1, lua_istable(L2, -1)); | 455 | LUA_ASSERT(L1, lua_istable(L2, -1)); |
457 | lua_pushlstring(L2, fqn, len); // L2: {} "f.q.n" | 456 | lua_pushlstring(L2, _fqn, _len); // L2: {} "f.q.n" |
458 | lua_rawget(L2, -2); // L2: {} t | 457 | lua_rawget(L2, -2); // L2: {} t |
459 | // we accept destination lookup failures in the case of transfering the Lanes body function (this will result in the source table being cloned instead) | 458 | // we accept destination lookup failures in the case of transfering the Lanes body function (this will result in the source table being cloned instead) |
460 | // but not when we extract something out of a keeper, as there is nothing to clone! | 459 | // but not when we extract something out of a keeper, as there is nothing to clone! |
@@ -473,7 +472,7 @@ void InterCopyContext::copy_cached_func() const | |||
473 | getErrL(), | 472 | getErrL(), |
474 | "%s: source table '%s' found as %s in %s destination transfer database.", | 473 | "%s: source table '%s' found as %s in %s destination transfer database.", |
475 | from ? from : "main", | 474 | from ? from : "main", |
476 | fqn, | 475 | _fqn, |
477 | lua_typename(L2, lua_type_as_enum(L2, -1)), | 476 | lua_typename(L2, lua_type_as_enum(L2, -1)), |
478 | to ? to : "main"); | 477 | to ? to : "main"); |
479 | } | 478 | } |
@@ -488,12 +487,12 @@ void InterCopyContext::copy_cached_func() const | |||
488 | 487 | ||
489 | void InterCopyContext::inter_copy_keyvaluepair() const | 488 | void InterCopyContext::inter_copy_keyvaluepair() const |
490 | { | 489 | { |
491 | SourceIndex const val_i{ lua_gettop(L1) }; | 490 | SourceIndex const _val_i{ lua_gettop(L1) }; |
492 | SourceIndex const key_i{ val_i - 1 }; | 491 | SourceIndex const _key_i{ _val_i - 1 }; |
493 | 492 | ||
494 | // For the key, only basic key types are copied over. others ignored | 493 | // For the key, only basic key types are copied over. others ignored |
495 | InterCopyContext c{ U, L2, L1, L2_cache_i, key_i, VT::KEY, mode, name }; | 494 | InterCopyContext _c{ U, L2, L1, L2_cache_i, _key_i, VT::KEY, mode, name }; |
496 | if (!c.inter_copy_one()) { | 495 | if (!_c.inter_copy_one()) { |
497 | return; | 496 | return; |
498 | // we could raise an error instead of ignoring the table entry, like so: | 497 | // we could raise an error instead of ignoring the table entry, like so: |
499 | // raise_luaL_error(L1, "Unable to copy %s key '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", name, luaL_typename(L1, key_i)); | 498 | // raise_luaL_error(L1, "Unable to copy %s key '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", name, luaL_typename(L1, key_i)); |
@@ -503,44 +502,44 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
503 | char* valPath{ nullptr }; | 502 | char* valPath{ nullptr }; |
504 | if (U->verboseErrors) { | 503 | if (U->verboseErrors) { |
505 | // for debug purposes, let's try to build a useful name | 504 | // for debug purposes, let's try to build a useful name |
506 | if (lua_type(L1, key_i) == LUA_TSTRING) { | 505 | if (lua_type(L1, _key_i) == LUA_TSTRING) { |
507 | char const* key{ lua_tostring(L1, key_i) }; | 506 | char const* key{ lua_tostring(L1, _key_i) }; |
508 | size_t const keyRawLen = lua_rawlen(L1, key_i); | 507 | size_t const keyRawLen = lua_rawlen(L1, _key_i); |
509 | size_t const bufLen = strlen(name) + keyRawLen + 2; | 508 | size_t const bufLen = strlen(name) + keyRawLen + 2; |
510 | valPath = (char*) alloca(bufLen); | 509 | valPath = (char*) alloca(bufLen); |
511 | sprintf(valPath, "%s.%*s", name, (int) keyRawLen, key); | 510 | sprintf(valPath, "%s.%*s", name, (int) keyRawLen, key); |
512 | key = nullptr; | 511 | key = nullptr; |
513 | } | 512 | } |
514 | #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 | 513 | #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 |
515 | else if (lua_isinteger(L1, key_i)) { | 514 | else if (lua_isinteger(L1, _key_i)) { |
516 | lua_Integer const key{ lua_tointeger(L1, key_i) }; | 515 | lua_Integer const key{ lua_tointeger(L1, _key_i) }; |
517 | valPath = (char*) alloca(strlen(name) + 32 + 3); | 516 | valPath = (char*) alloca(strlen(name) + 32 + 3); |
518 | sprintf(valPath, "%s[" LUA_INTEGER_FMT "]", name, key); | 517 | sprintf(valPath, "%s[" LUA_INTEGER_FMT "]", name, key); |
519 | } | 518 | } |
520 | #endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 | 519 | #endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 |
521 | else if (lua_type(L1, key_i) == LUA_TNUMBER) { | 520 | else if (lua_type(L1, _key_i) == LUA_TNUMBER) { |
522 | lua_Number const key{ lua_tonumber(L1, key_i) }; | 521 | lua_Number const key{ lua_tonumber(L1, _key_i) }; |
523 | valPath = (char*) alloca(strlen(name) + 32 + 3); | 522 | valPath = (char*) alloca(strlen(name) + 32 + 3); |
524 | sprintf(valPath, "%s[" LUA_NUMBER_FMT "]", name, key); | 523 | sprintf(valPath, "%s[" LUA_NUMBER_FMT "]", name, key); |
525 | } else if (lua_type(L1, key_i) == LUA_TLIGHTUSERDATA) { | 524 | } else if (lua_type(L1, _key_i) == LUA_TLIGHTUSERDATA) { |
526 | void* const key{ lua_touserdata(L1, key_i) }; | 525 | void* const key{ lua_touserdata(L1, _key_i) }; |
527 | valPath = (char*) alloca(strlen(name) + 16 + 5); | 526 | valPath = (char*) alloca(strlen(name) + 16 + 5); |
528 | sprintf(valPath, "%s[U:%p]", name, key); | 527 | sprintf(valPath, "%s[U:%p]", name, key); |
529 | } else if (lua_type(L1, key_i) == LUA_TBOOLEAN) { | 528 | } else if (lua_type(L1, _key_i) == LUA_TBOOLEAN) { |
530 | int const key{ lua_toboolean(L1, key_i) }; | 529 | int const key{ lua_toboolean(L1, _key_i) }; |
531 | valPath = (char*) alloca(strlen(name) + 8); | 530 | valPath = (char*) alloca(strlen(name) + 8); |
532 | sprintf(valPath, "%s[%s]", name, key ? "true" : "false"); | 531 | sprintf(valPath, "%s[%s]", name, key ? "true" : "false"); |
533 | } | 532 | } |
534 | } | 533 | } |
535 | c.L1_i = SourceIndex{ val_i }; | 534 | _c.L1_i = SourceIndex{ _val_i }; |
536 | // Contents of metatables are copied with cache checking. important to detect loops. | 535 | // Contents of metatables are copied with cache checking. important to detect loops. |
537 | c.vt = VT::NORMAL; | 536 | _c.vt = VT::NORMAL; |
538 | c.name = valPath ? valPath : name; | 537 | _c.name = valPath ? valPath : name; |
539 | if (c.inter_copy_one()) { | 538 | if (_c.inter_copy_one()) { |
540 | LUA_ASSERT(L1, lua_istable(L2, -3)); | 539 | LUA_ASSERT(L1, lua_istable(L2, -3)); |
541 | lua_rawset(L2, -3); // add to table (pops key & val) | 540 | lua_rawset(L2, -3); // add to table (pops key & val) |
542 | } else { | 541 | } else { |
543 | raise_luaL_error(getErrL(), "Unable to copy %s entry '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", valPath, luaL_typename(L1, val_i)); | 542 | raise_luaL_error(getErrL(), "Unable to copy %s entry '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", valPath, luaL_typename(L1, _val_i)); |
544 | } | 543 | } |
545 | } | 544 | } |
546 | 545 | ||
@@ -555,13 +554,13 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
555 | } | 554 | } |
556 | STACK_CHECK(L1, 1); | 555 | STACK_CHECK(L1, 1); |
557 | 556 | ||
558 | lua_Integer const mt_id{ get_mt_id(U, L1, -1) }; // Unique id for the metatable | 557 | lua_Integer const _mt_id{ get_mt_id(U, L1, -1) }; // Unique id for the metatable |
559 | 558 | ||
560 | STACK_CHECK_START_REL(L2, 0); | 559 | STACK_CHECK_START_REL(L2, 0); |
561 | STACK_GROW(L2, 4); | 560 | STACK_GROW(L2, 4); |
562 | // do we already know this metatable? | 561 | // do we already know this metatable? |
563 | std::ignore = kMtIdRegKey.getSubTable(L2, 0, 0); // L2: _R[kMtIdRegKey] | 562 | std::ignore = kMtIdRegKey.getSubTable(L2, 0, 0); // L2: _R[kMtIdRegKey] |
564 | lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] id | 563 | lua_pushinteger(L2, _mt_id); // L2: _R[kMtIdRegKey] id |
565 | lua_rawget(L2, -2); // L2: _R[kMtIdRegKey] mt|nil | 564 | lua_rawget(L2, -2); // L2: _R[kMtIdRegKey] mt|nil |
566 | STACK_CHECK(L2, 2); | 565 | STACK_CHECK(L2, 2); |
567 | 566 | ||
@@ -574,13 +573,13 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
574 | 573 | ||
575 | STACK_CHECK(L2, 2); // L2: _R[kMtIdRegKey] mt | 574 | STACK_CHECK(L2, 2); // L2: _R[kMtIdRegKey] mt |
576 | // mt_id -> metatable | 575 | // mt_id -> metatable |
577 | lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] mt id | 576 | lua_pushinteger(L2, _mt_id); // L2: _R[kMtIdRegKey] mt id |
578 | lua_pushvalue(L2, -2); // L2: _R[kMtIdRegKey] mt id mt | 577 | lua_pushvalue(L2, -2); // L2: _R[kMtIdRegKey] mt id mt |
579 | lua_rawset(L2, -4); // L2: _R[kMtIdRegKey] mt | 578 | lua_rawset(L2, -4); // L2: _R[kMtIdRegKey] mt |
580 | 579 | ||
581 | // metatable -> mt_id | 580 | // metatable -> mt_id |
582 | lua_pushvalue(L2, -1); // L2: _R[kMtIdRegKey] mt mt | 581 | lua_pushvalue(L2, -1); // L2: _R[kMtIdRegKey] mt mt |
583 | lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] mt mt id | 582 | lua_pushinteger(L2, _mt_id); // L2: _R[kMtIdRegKey] mt mt id |
584 | lua_rawset(L2, -4); // L2: _R[kMtIdRegKey] mt | 583 | lua_rawset(L2, -4); // L2: _R[kMtIdRegKey] mt |
585 | STACK_CHECK(L2, 2); | 584 | STACK_CHECK(L2, 2); |
586 | } | 585 | } |
@@ -600,7 +599,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
600 | // Returns true if the table was cached (no need to fill it!); false if it's a virgin. | 599 | // Returns true if the table was cached (no need to fill it!); false if it's a virgin. |
601 | [[nodiscard]] bool InterCopyContext::push_cached_table() const | 600 | [[nodiscard]] bool InterCopyContext::push_cached_table() const |
602 | { | 601 | { |
603 | void const* p{ lua_topointer(L1, L1_i) }; | 602 | void const* const _p{ lua_topointer(L1, L1_i) }; |
604 | 603 | ||
605 | LUA_ASSERT(L1, L2_cache_i != 0); | 604 | LUA_ASSERT(L1, L2_cache_i != 0); |
606 | STACK_GROW(L2, 3); | 605 | STACK_GROW(L2, 3); |
@@ -609,37 +608,37 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
609 | // We don't need to use the from state ('L1') in ID since the life span | 608 | // We don't need to use the from state ('L1') in ID since the life span |
610 | // is only for the duration of a copy (both states are locked). | 609 | // is only for the duration of a copy (both states are locked). |
611 | // push a light userdata uniquely representing the table | 610 | // push a light userdata uniquely representing the table |
612 | lua_pushlightuserdata(L2, const_cast<void*>(p)); // L1: ... t ... L2: ... p | 611 | lua_pushlightuserdata(L2, const_cast<void*>(_p)); // L1: ... t ... L2: ... p |
613 | 612 | ||
614 | // fprintf(stderr, "<< ID: %s >>\n", lua_tostring(L2, -1)); | 613 | // fprintf(stderr, "<< ID: %s >>\n", lua_tostring(L2, -1)); |
615 | 614 | ||
616 | lua_rawget(L2, L2_cache_i); // L1: ... t ... L2: ... {cached|nil} | 615 | lua_rawget(L2, L2_cache_i); // L1: ... t ... L2: ... {cached|nil} |
617 | bool const not_found_in_cache{ lua_isnil(L2, -1) }; | 616 | bool const _not_found_in_cache{ lua_isnil(L2, -1) }; |
618 | if (not_found_in_cache) { | 617 | if (_not_found_in_cache) { |
619 | // create a new entry in the cache | 618 | // create a new entry in the cache |
620 | lua_pop(L2, 1); // L1: ... t ... L2: ... | 619 | lua_pop(L2, 1); // L1: ... t ... L2: ... |
621 | lua_newtable(L2); // L1: ... t ... L2: ... {} | 620 | lua_newtable(L2); // L1: ... t ... L2: ... {} |
622 | lua_pushlightuserdata(L2, const_cast<void*>(p)); // L1: ... t ... L2: ... {} p | 621 | lua_pushlightuserdata(L2, const_cast<void*>(_p)); // L1: ... t ... L2: ... {} p |
623 | lua_pushvalue(L2, -2); // L1: ... t ... L2: ... {} p {} | 622 | lua_pushvalue(L2, -2); // L1: ... t ... L2: ... {} p {} |
624 | lua_rawset(L2, L2_cache_i); // L1: ... t ... L2: ... {} | 623 | lua_rawset(L2, L2_cache_i); // L1: ... t ... L2: ... {} |
625 | } | 624 | } |
626 | STACK_CHECK(L2, 1); | 625 | STACK_CHECK(L2, 1); |
627 | LUA_ASSERT(L1, lua_istable(L2, -1)); | 626 | LUA_ASSERT(L1, lua_istable(L2, -1)); |
628 | return !not_found_in_cache; | 627 | return !_not_found_in_cache; |
629 | } | 628 | } |
630 | 629 | ||
631 | // ################################################################################################# | 630 | // ################################################################################################# |
632 | 631 | ||
633 | [[nodiscard]] bool InterCopyContext::tryCopyClonable() const | 632 | [[nodiscard]] bool InterCopyContext::tryCopyClonable() const |
634 | { | 633 | { |
635 | SourceIndex const L1i{ lua_absindex(L1, L1_i) }; | 634 | SourceIndex const _L1_i{ lua_absindex(L1, L1_i) }; |
636 | void* const source{ lua_touserdata(L1, L1i) }; | 635 | void* const _source{ lua_touserdata(L1, _L1_i) }; |
637 | 636 | ||
638 | STACK_CHECK_START_REL(L1, 0); | 637 | STACK_CHECK_START_REL(L1, 0); |
639 | STACK_CHECK_START_REL(L2, 0); | 638 | STACK_CHECK_START_REL(L2, 0); |
640 | 639 | ||
641 | // Check if the source was already cloned during this copy | 640 | // Check if the source was already cloned during this copy |
642 | lua_pushlightuserdata(L2, source); // L2: ... source | 641 | lua_pushlightuserdata(L2, _source); // L2: ... source |
643 | lua_rawget(L2, L2_cache_i); // L2: ... clone? | 642 | lua_rawget(L2, L2_cache_i); // L2: ... clone? |
644 | if (!lua_isnil(L2, -1)) { | 643 | if (!lua_isnil(L2, -1)) { |
645 | STACK_CHECK(L2, 1); | 644 | STACK_CHECK(L2, 1); |
@@ -650,7 +649,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
650 | STACK_CHECK(L2, 0); | 649 | STACK_CHECK(L2, 0); |
651 | 650 | ||
652 | // no metatable? -> not clonable | 651 | // no metatable? -> not clonable |
653 | if (!lua_getmetatable(L1, L1i)) { // L1: ... mt? | 652 | if (!lua_getmetatable(L1, _L1_i)) { // L1: ... mt? |
654 | STACK_CHECK(L1, 0); | 653 | STACK_CHECK(L1, 0); |
655 | return false; | 654 | return false; |
656 | } | 655 | } |
@@ -666,18 +665,18 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
666 | // we need to copy over the uservalues of the userdata as well | 665 | // we need to copy over the uservalues of the userdata as well |
667 | { | 666 | { |
668 | int const mt{ lua_absindex(L1, -2) }; // L1: ... mt __lanesclone | 667 | int const mt{ lua_absindex(L1, -2) }; // L1: ... mt __lanesclone |
669 | size_t const userdata_size{ lua_rawlen(L1, L1i) }; | 668 | size_t const userdata_size{ lua_rawlen(L1, _L1_i) }; |
670 | // extract all the uservalues, but don't transfer them yet | 669 | // extract all the uservalues, but don't transfer them yet |
671 | int uvi = 0; | 670 | int _uvi{ 0 }; |
672 | while (lua_getiuservalue(L1, L1i, ++uvi) != LUA_TNONE) {} // L1: ... mt __lanesclone [uv]+ nil | 671 | while (lua_getiuservalue(L1, _L1_i, ++_uvi) != LUA_TNONE) {} // L1: ... mt __lanesclone [uv]+ nil |
673 | // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now | 672 | // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now |
674 | lua_pop(L1, 1); // L1: ... mt __lanesclone [uv]+ | 673 | lua_pop(L1, 1); // L1: ... mt __lanesclone [uv]+ |
675 | --uvi; | 674 | --_uvi; |
676 | // create the clone userdata with the required number of uservalue slots | 675 | // create the clone userdata with the required number of uservalue slots |
677 | void* const clone{ lua_newuserdatauv(L2, userdata_size, uvi) }; // L2: ... u | 676 | void* const _clone{ lua_newuserdatauv(L2, userdata_size, _uvi) }; // L2: ... u |
678 | // copy the metatable in the target state, and give it to the clone we put there | 677 | // copy the metatable in the target state, and give it to the clone we put there |
679 | InterCopyContext c{ U, L2, L1, L2_cache_i, SourceIndex{ mt }, VT::NORMAL, mode, name }; | 678 | InterCopyContext _c{ U, L2, L1, L2_cache_i, SourceIndex{ mt }, VT::NORMAL, mode, name }; |
680 | if (c.inter_copy_one()) { // L2: ... u mt|sentinel | 679 | if (_c.inter_copy_one()) { // L2: ... u mt|sentinel |
681 | if (LookupMode::ToKeeper == mode) { // L2: ... u sentinel | 680 | if (LookupMode::ToKeeper == mode) { // L2: ... u sentinel |
682 | LUA_ASSERT(L1, lua_tocfunction(L2, -1) == table_lookup_sentinel); | 681 | LUA_ASSERT(L1, lua_tocfunction(L2, -1) == table_lookup_sentinel); |
683 | // we want to create a new closure with a 'clone sentinel' function, where the upvalues are the userdata and the metatable fqn | 682 | // we want to create a new closure with a 'clone sentinel' function, where the upvalues are the userdata and the metatable fqn |
@@ -694,7 +693,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
694 | raise_luaL_error(getErrL(), "Error copying a metatable"); | 693 | raise_luaL_error(getErrL(), "Error copying a metatable"); |
695 | } | 694 | } |
696 | // first, add the entry in the cache (at this point it is either the actual userdata or the keeper sentinel | 695 | // first, add the entry in the cache (at this point it is either the actual userdata or the keeper sentinel |
697 | lua_pushlightuserdata(L2, source); // L2: ... u source | 696 | lua_pushlightuserdata(L2, _source); // L2: ... u source |
698 | lua_pushvalue(L2, -2); // L2: ... u source u | 697 | lua_pushvalue(L2, -2); // L2: ... u source u |
699 | lua_rawset(L2, L2_cache_i); // L2: ... u | 698 | lua_rawset(L2, L2_cache_i); // L2: ... u |
700 | // make sure we have the userdata now | 699 | // make sure we have the userdata now |
@@ -702,15 +701,15 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
702 | lua_getupvalue(L2, -1, 2); // L2: ... userdata_clone_sentinel u | 701 | lua_getupvalue(L2, -1, 2); // L2: ... userdata_clone_sentinel u |
703 | } | 702 | } |
704 | // assign uservalues | 703 | // assign uservalues |
705 | while (uvi > 0) { | 704 | while (_uvi > 0) { |
706 | c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; | 705 | _c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; |
707 | if (!c.inter_copy_one()) { // L2: ... u uv | 706 | if (!_c.inter_copy_one()) { // L2: ... u uv |
708 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); | 707 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); |
709 | } | 708 | } |
710 | lua_pop(L1, 1); // L1: ... mt __lanesclone [uv]* | 709 | lua_pop(L1, 1); // L1: ... mt __lanesclone [uv]* |
711 | // this pops the value from the stack | 710 | // this pops the value from the stack |
712 | lua_setiuservalue(L2, -2, uvi); // L2: ... u | 711 | lua_setiuservalue(L2, -2, _uvi); // L2: ... u |
713 | --uvi; | 712 | --_uvi; |
714 | } | 713 | } |
715 | // when we are done, all uservalues are popped from the source stack, and we want only the single transferred value in the destination | 714 | // when we are done, all uservalues are popped from the source stack, and we want only the single transferred value in the destination |
716 | if (LookupMode::ToKeeper == mode) { // L2: ... userdata_clone_sentinel u | 715 | if (LookupMode::ToKeeper == mode) { // L2: ... userdata_clone_sentinel u |
@@ -719,8 +718,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
719 | STACK_CHECK(L2, 1); | 718 | STACK_CHECK(L2, 1); |
720 | STACK_CHECK(L1, 2); | 719 | STACK_CHECK(L1, 2); |
721 | // call cloning function in source state to perform the actual memory cloning | 720 | // call cloning function in source state to perform the actual memory cloning |
722 | lua_pushlightuserdata(L1, clone); // L1: ... mt __lanesclone clone | 721 | lua_pushlightuserdata(L1, _clone); // L1: ... mt __lanesclone clone |
723 | lua_pushlightuserdata(L1, source); // L1: ... mt __lanesclone clone source | 722 | lua_pushlightuserdata(L1, _source); // L1: ... mt __lanesclone clone source |
724 | lua_pushinteger(L1, static_cast<lua_Integer>(userdata_size)); // L1: ... mt __lanesclone clone source size | 723 | lua_pushinteger(L1, static_cast<lua_Integer>(userdata_size)); // L1: ... mt __lanesclone clone source size |
725 | lua_call(L1, 3, 0); // L1: ... mt | 724 | lua_call(L1, 3, 0); // L1: ... mt |
726 | STACK_CHECK(L1, 1); | 725 | STACK_CHECK(L1, 1); |
@@ -738,8 +737,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
738 | // Returns false if not a deep userdata, else true (unless an error occured) | 737 | // Returns false if not a deep userdata, else true (unless an error occured) |
739 | [[nodiscard]] bool InterCopyContext::tryCopyDeep() const | 738 | [[nodiscard]] bool InterCopyContext::tryCopyDeep() const |
740 | { | 739 | { |
741 | DeepFactory* const factory{ LookupFactory(L1, L1_i, mode) }; | 740 | DeepFactory* const _factory{ LookupFactory(L1, L1_i, mode) }; |
742 | if (factory == nullptr) { | 741 | if (_factory == nullptr) { |
743 | return false; // not a deep userdata | 742 | return false; // not a deep userdata |
744 | } | 743 | } |
745 | 744 | ||
@@ -747,33 +746,33 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
747 | STACK_CHECK_START_REL(L2, 0); | 746 | STACK_CHECK_START_REL(L2, 0); |
748 | 747 | ||
749 | // extract all uservalues of the source. unfortunately, the only way to know their count is to iterate until we fail | 748 | // extract all uservalues of the source. unfortunately, the only way to know their count is to iterate until we fail |
750 | int nuv = 0; | 749 | int _nuv = 0; |
751 | while (lua_getiuservalue(L1, L1_i, nuv + 1) != LUA_TNONE) { // L1: ... u [uv]* nil | 750 | while (lua_getiuservalue(L1, L1_i, _nuv + 1) != LUA_TNONE) { // L1: ... u [uv]* nil |
752 | ++nuv; | 751 | ++_nuv; |
753 | } | 752 | } |
754 | // last call returned TNONE and pushed nil, that we don't need | 753 | // last call returned TNONE and pushed nil, that we don't need |
755 | lua_pop(L1, 1); // L1: ... u [uv]* | 754 | lua_pop(L1, 1); // L1: ... u [uv]* |
756 | STACK_CHECK(L1, nuv); | 755 | STACK_CHECK(L1, _nuv); |
757 | 756 | ||
758 | DeepPrelude* const u{ *lua_tofulluserdata<DeepPrelude*>(L1, L1_i) }; | 757 | DeepPrelude* const u{ *lua_tofulluserdata<DeepPrelude*>(L1, L1_i) }; |
759 | char const* errmsg{ DeepFactory::PushDeepProxy(L2, u, nuv, mode) }; // L1: ... u [uv]* L2: u | 758 | char const* errmsg{ DeepFactory::PushDeepProxy(L2, u, _nuv, mode) }; // L1: ... u [uv]* L2: u |
760 | if (errmsg != nullptr) { | 759 | if (errmsg != nullptr) { |
761 | raise_luaL_error(getErrL(), errmsg); | 760 | raise_luaL_error(getErrL(), errmsg); |
762 | } | 761 | } |
763 | 762 | ||
764 | // transfer all uservalues of the source in the destination | 763 | // transfer all uservalues of the source in the destination |
765 | { | 764 | { |
766 | InterCopyContext c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, name }; | 765 | InterCopyContext _c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, name }; |
767 | int const clone_i{ lua_gettop(L2) }; | 766 | int const _clone_i{ lua_gettop(L2) }; |
768 | while (nuv) { | 767 | while (_nuv) { |
769 | c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; | 768 | _c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; |
770 | if (!c.inter_copy_one()) { // L1: ... u [uv]* L2: u uv | 769 | if (!_c.inter_copy_one()) { // L1: ... u [uv]* L2: u uv |
771 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); | 770 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); |
772 | } | 771 | } |
773 | lua_pop(L1, 1); // L1: ... u [uv]* | 772 | lua_pop(L1, 1); // L1: ... u [uv]* |
774 | // this pops the value from the stack | 773 | // this pops the value from the stack |
775 | lua_setiuservalue(L2, clone_i, nuv); // L2: u | 774 | lua_setiuservalue(L2, _clone_i, _nuv); // L2: u |
776 | --nuv; | 775 | --_nuv; |
777 | } | 776 | } |
778 | } | 777 | } |
779 | 778 | ||
@@ -787,9 +786,9 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
787 | 786 | ||
788 | [[nodiscard]] bool InterCopyContext::inter_copy_boolean() const | 787 | [[nodiscard]] bool InterCopyContext::inter_copy_boolean() const |
789 | { | 788 | { |
790 | int const v{ lua_toboolean(L1, L1_i) }; | 789 | int const _v{ lua_toboolean(L1, L1_i) }; |
791 | DEBUGSPEW_CODE(fprintf(stderr, "%s\n", v ? "true" : "false")); | 790 | DEBUGSPEW_CODE(fprintf(stderr, "%s\n", _v ? "true" : "false")); |
792 | lua_pushboolean(L2, v); | 791 | lua_pushboolean(L2, _v); |
793 | return true; | 792 | return true; |
794 | } | 793 | } |
795 | 794 | ||
@@ -810,8 +809,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
810 | 809 | ||
811 | // let's see if we already restored this userdata | 810 | // let's see if we already restored this userdata |
812 | lua_getupvalue(L1, L1_i, 2); // L1: ... u | 811 | lua_getupvalue(L1, L1_i, 2); // L1: ... u |
813 | void* source = lua_touserdata(L1, -1); | 812 | void* _source{ lua_touserdata(L1, -1) }; |
814 | lua_pushlightuserdata(L2, source); // L2: ... source | 813 | lua_pushlightuserdata(L2, _source); // L2: ... source |
815 | lua_rawget(L2, L2_cache_i); // L2: ... u? | 814 | lua_rawget(L2, L2_cache_i); // L2: ... u? |
816 | if (!lua_isnil(L2, -1)) { | 815 | if (!lua_isnil(L2, -1)) { |
817 | lua_pop(L1, 1); // L1: ... | 816 | lua_pop(L1, 1); // L1: ... |
@@ -829,22 +828,22 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
829 | } | 828 | } |
830 | // 'L1_i' slot was the proxy closure, but from now on we operate onthe actual userdata we extracted from it | 829 | // 'L1_i' slot was the proxy closure, but from now on we operate onthe actual userdata we extracted from it |
831 | SourceIndex const source_i{ lua_gettop(L1) }; | 830 | SourceIndex const source_i{ lua_gettop(L1) }; |
832 | source = lua_touserdata(L1, -1); | 831 | _source = lua_touserdata(L1, -1); |
833 | void* clone{ nullptr }; | 832 | void* _clone{ nullptr }; |
834 | // get the number of bytes to allocate for the clone | 833 | // get the number of bytes to allocate for the clone |
835 | size_t const userdata_size{ lua_rawlen(L1, -1) }; | 834 | size_t const userdata_size{ lua_rawlen(L1, -1) }; |
836 | { | 835 | { |
837 | // extract uservalues (don't transfer them yet) | 836 | // extract uservalues (don't transfer them yet) |
838 | int uvi = 0; | 837 | int _uvi = 0; |
839 | while (lua_getiuservalue(L1, source_i, ++uvi) != LUA_TNONE) {} // L1: ... u uv | 838 | while (lua_getiuservalue(L1, source_i, ++_uvi) != LUA_TNONE) {} // L1: ... u uv |
840 | // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now | 839 | // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now |
841 | lua_pop(L1, 1); // L1: ... u [uv]* | 840 | lua_pop(L1, 1); // L1: ... u [uv]* |
842 | --uvi; | 841 | --_uvi; |
843 | STACK_CHECK(L1, uvi + 1); | 842 | STACK_CHECK(L1, _uvi + 1); |
844 | // create the clone userdata with the required number of uservalue slots | 843 | // create the clone userdata with the required number of uservalue slots |
845 | clone = lua_newuserdatauv(L2, userdata_size, uvi); // L2: ... mt u | 844 | _clone = lua_newuserdatauv(L2, userdata_size, _uvi); // L2: ... mt u |
846 | // add it in the cache | 845 | // add it in the cache |
847 | lua_pushlightuserdata(L2, source); // L2: ... mt u source | 846 | lua_pushlightuserdata(L2, _source); // L2: ... mt u source |
848 | lua_pushvalue(L2, -2); // L2: ... mt u source u | 847 | lua_pushvalue(L2, -2); // L2: ... mt u source u |
849 | lua_rawset(L2, L2_cache_i); // L2: ... mt u | 848 | lua_rawset(L2, L2_cache_i); // L2: ... mt u |
850 | // set metatable | 849 | // set metatable |
@@ -852,15 +851,15 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
852 | lua_setmetatable(L2, -2); // L2: ... mt u | 851 | lua_setmetatable(L2, -2); // L2: ... mt u |
853 | // transfer and assign uservalues | 852 | // transfer and assign uservalues |
854 | InterCopyContext c{ *this }; | 853 | InterCopyContext c{ *this }; |
855 | while (uvi > 0) { | 854 | while (_uvi > 0) { |
856 | c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; | 855 | c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; |
857 | if (!c.inter_copy_one()) { // L2: ... mt u uv | 856 | if (!c.inter_copy_one()) { // L2: ... mt u uv |
858 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); | 857 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); |
859 | } | 858 | } |
860 | lua_pop(L1, 1); // L1: ... u [uv]* | 859 | lua_pop(L1, 1); // L1: ... u [uv]* |
861 | // this pops the value from the stack | 860 | // this pops the value from the stack |
862 | lua_setiuservalue(L2, -2, uvi); // L2: ... mt u | 861 | lua_setiuservalue(L2, -2, _uvi); // L2: ... mt u |
863 | --uvi; | 862 | --_uvi; |
864 | } | 863 | } |
865 | // when we are done, all uservalues are popped from the stack, we can pop the source as well | 864 | // when we are done, all uservalues are popped from the stack, we can pop the source as well |
866 | lua_pop(L1, 1); // L1: ... | 865 | lua_pop(L1, 1); // L1: ... |
@@ -868,12 +867,12 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
868 | STACK_CHECK(L2, 2); // L2: ... mt u | 867 | STACK_CHECK(L2, 2); // L2: ... mt u |
869 | } | 868 | } |
870 | // perform the custom cloning part | 869 | // perform the custom cloning part |
871 | lua_insert(L2, -2); // L2: ... u mt | 870 | lua_insert(L2, -2); // L2: ... u mt |
872 | // __lanesclone should always exist because we wouldn't be restoring data from a userdata_clone_sentinel closure to begin with | 871 | // __lanesclone should always exist because we wouldn't be restoring data from a userdata_clone_sentinel closure to begin with |
873 | lua_getfield(L2, -1, "__lanesclone"); // L2: ... u mt __lanesclone | 872 | lua_getfield(L2, -1, "__lanesclone"); // L2: ... u mt __lanesclone |
874 | lua_remove(L2, -2); // L2: ... u __lanesclone | 873 | lua_remove(L2, -2); // L2: ... u __lanesclone |
875 | lua_pushlightuserdata(L2, clone); // L2: ... u __lanesclone clone | 874 | lua_pushlightuserdata(L2, _clone); // L2: ... u __lanesclone clone |
876 | lua_pushlightuserdata(L2, source); // L2: ... u __lanesclone clone source | 875 | lua_pushlightuserdata(L2, _source); // L2: ... u __lanesclone clone source |
877 | lua_pushinteger(L2, userdata_size); // L2: ... u __lanesclone clone source size | 876 | lua_pushinteger(L2, userdata_size); // L2: ... u __lanesclone clone source size |
878 | // clone:__lanesclone(dest, source, size) | 877 | // clone:__lanesclone(dest, source, size) |
879 | lua_call(L2, 3, 0); // L2: ... u | 878 | lua_call(L2, 3, 0); // L2: ... u |
@@ -891,9 +890,9 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
891 | 890 | ||
892 | [[nodiscard]] bool InterCopyContext::inter_copy_lightuserdata() const | 891 | [[nodiscard]] bool InterCopyContext::inter_copy_lightuserdata() const |
893 | { | 892 | { |
894 | void* const p{ lua_touserdata(L1, L1_i) }; | 893 | void* const _p{ lua_touserdata(L1, L1_i) }; |
895 | DEBUGSPEW_CODE(fprintf(stderr, "%p\n", p)); | 894 | DEBUGSPEW_CODE(fprintf(stderr, "%p\n", _p)); |
896 | lua_pushlightuserdata(L2, p); | 895 | lua_pushlightuserdata(L2, _p); |
897 | return true; | 896 | return true; |
898 | } | 897 | } |
899 | 898 | ||
@@ -915,15 +914,15 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
915 | // LNUM patch support (keeping integer accuracy) | 914 | // LNUM patch support (keeping integer accuracy) |
916 | #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 | 915 | #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 |
917 | if (lua_isinteger(L1, L1_i)) { | 916 | if (lua_isinteger(L1, L1_i)) { |
918 | lua_Integer const v{ lua_tointeger(L1, L1_i) }; | 917 | lua_Integer const _v{ lua_tointeger(L1, L1_i) }; |
919 | DEBUGSPEW_CODE(fprintf(stderr, LUA_INTEGER_FMT "\n", v)); | 918 | DEBUGSPEW_CODE(fprintf(stderr, LUA_INTEGER_FMT "\n", _v)); |
920 | lua_pushinteger(L2, v); | 919 | lua_pushinteger(L2, _v); |
921 | } else | 920 | } else |
922 | #endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 | 921 | #endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 |
923 | { | 922 | { |
924 | lua_Number const v{ lua_tonumber(L1, L1_i) }; | 923 | lua_Number const _v{ lua_tonumber(L1, L1_i) }; |
925 | DEBUGSPEW_CODE(fprintf(stderr, LUA_NUMBER_FMT "\n", v)); | 924 | DEBUGSPEW_CODE(fprintf(stderr, LUA_NUMBER_FMT "\n", _v)); |
926 | lua_pushnumber(L2, v); | 925 | lua_pushnumber(L2, _v); |
927 | } | 926 | } |
928 | return true; | 927 | return true; |
929 | } | 928 | } |
@@ -932,10 +931,10 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
932 | 931 | ||
933 | [[nodiscard]] bool InterCopyContext::inter_copy_string() const | 932 | [[nodiscard]] bool InterCopyContext::inter_copy_string() const |
934 | { | 933 | { |
935 | size_t len; | 934 | size_t _len; |
936 | char const* const s{ lua_tolstring(L1, L1_i, &len) }; | 935 | char const* const _s{ lua_tolstring(L1, L1_i, &_len) }; |
937 | DEBUGSPEW_CODE(fprintf(stderr, "'%s'\n", s)); | 936 | DEBUGSPEW_CODE(fprintf(stderr, "'%s'\n", _s)); |
938 | lua_pushlstring(L2, s, len); | 937 | lua_pushlstring(L2, _s, _len); |
939 | return true; | 938 | return true; |
940 | } | 939 | } |
941 | 940 | ||
@@ -1029,8 +1028,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
1029 | 1028 | ||
1030 | // Not a deep or clonable full userdata | 1029 | // Not a deep or clonable full userdata |
1031 | if (U->demoteFullUserdata) { // attempt demotion to light userdata | 1030 | if (U->demoteFullUserdata) { // attempt demotion to light userdata |
1032 | void* const lud{ lua_touserdata(L1, L1_i) }; | 1031 | void* const _lud{ lua_touserdata(L1, L1_i) }; |
1033 | lua_pushlightuserdata(L2, lud); | 1032 | lua_pushlightuserdata(L2, _lud); |
1034 | } else { // raise an error | 1033 | } else { // raise an error |
1035 | raise_luaL_error(getErrL(), "can't copy non-deep full userdata across lanes"); | 1034 | raise_luaL_error(getErrL(), "can't copy non-deep full userdata across lanes"); |
1036 | } | 1035 | } |
@@ -1083,16 +1082,16 @@ static char const* vt_names[] = { | |||
1083 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "inter_copy_one()\n" INDENT_END(U))); | 1082 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "inter_copy_one()\n" INDENT_END(U))); |
1084 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1083 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1085 | 1084 | ||
1086 | LuaType val_type{ lua_type_as_enum(L1, L1_i) }; | 1085 | LuaType _val_type{ lua_type_as_enum(L1, L1_i) }; |
1087 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s %s: " INDENT_END(U), lua_type_names[static_cast<int>(val_type)], vt_names[static_cast<int>(vt)])); | 1086 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s %s: " INDENT_END(U), lua_type_names[static_cast<int>(_val_type)], vt_names[static_cast<int>(vt)])); |
1088 | 1087 | ||
1089 | // Non-POD can be skipped if its metatable contains { __lanesignore = true } | 1088 | // Non-POD can be skipped if its metatable contains { __lanesignore = true } |
1090 | if (((1 << static_cast<int>(val_type)) & kPODmask) == 0) { | 1089 | if (((1 << static_cast<int>(_val_type)) & kPODmask) == 0) { |
1091 | if (lua_getmetatable(L1, L1_i)) { // L1: ... mt | 1090 | if (lua_getmetatable(L1, L1_i)) { // L1: ... mt |
1092 | lua_getfield(L1, -1, "__lanesignore"); // L1: ... mt ignore? | 1091 | lua_getfield(L1, -1, "__lanesignore"); // L1: ... mt ignore? |
1093 | if (lua_isboolean(L1, -1) && lua_toboolean(L1, -1)) { | 1092 | if (lua_isboolean(L1, -1) && lua_toboolean(L1, -1)) { |
1094 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "__lanesignore -> LUA_TNIL\n" INDENT_END(U))); | 1093 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "__lanesignore -> LUA_TNIL\n" INDENT_END(U))); |
1095 | val_type = LuaType::NIL; | 1094 | _val_type = LuaType::NIL; |
1096 | } | 1095 | } |
1097 | lua_pop(L1, 2); // L1: ... | 1096 | lua_pop(L1, 2); // L1: ... |
1098 | } | 1097 | } |
@@ -1100,47 +1099,47 @@ static char const* vt_names[] = { | |||
1100 | STACK_CHECK(L1, 0); | 1099 | STACK_CHECK(L1, 0); |
1101 | 1100 | ||
1102 | // Lets push nil to L2 if the object should be ignored | 1101 | // Lets push nil to L2 if the object should be ignored |
1103 | bool ret{ true }; | 1102 | bool _ret{ true }; |
1104 | switch (val_type) { | 1103 | switch (_val_type) { |
1105 | // Basic types allowed both as values, and as table keys | 1104 | // Basic types allowed both as values, and as table keys |
1106 | case LuaType::BOOLEAN: | 1105 | case LuaType::BOOLEAN: |
1107 | ret = inter_copy_boolean(); | 1106 | _ret = inter_copy_boolean(); |
1108 | break; | 1107 | break; |
1109 | case LuaType::NUMBER: | 1108 | case LuaType::NUMBER: |
1110 | ret = inter_copy_number(); | 1109 | _ret = inter_copy_number(); |
1111 | break; | 1110 | break; |
1112 | case LuaType::STRING: | 1111 | case LuaType::STRING: |
1113 | ret = inter_copy_string(); | 1112 | _ret = inter_copy_string(); |
1114 | break; | 1113 | break; |
1115 | case LuaType::LIGHTUSERDATA: | 1114 | case LuaType::LIGHTUSERDATA: |
1116 | ret = inter_copy_lightuserdata(); | 1115 | _ret = inter_copy_lightuserdata(); |
1117 | break; | 1116 | break; |
1118 | 1117 | ||
1119 | // The following types are not allowed as table keys | 1118 | // The following types are not allowed as table keys |
1120 | case LuaType::USERDATA: | 1119 | case LuaType::USERDATA: |
1121 | ret = inter_copy_userdata(); | 1120 | _ret = inter_copy_userdata(); |
1122 | break; | 1121 | break; |
1123 | case LuaType::NIL: | 1122 | case LuaType::NIL: |
1124 | ret = inter_copy_nil(); | 1123 | _ret = inter_copy_nil(); |
1125 | break; | 1124 | break; |
1126 | case LuaType::FUNCTION: | 1125 | case LuaType::FUNCTION: |
1127 | ret = inter_copy_function(); | 1126 | _ret = inter_copy_function(); |
1128 | break; | 1127 | break; |
1129 | case LuaType::TABLE: | 1128 | case LuaType::TABLE: |
1130 | ret = inter_copy_table(); | 1129 | _ret = inter_copy_table(); |
1131 | break; | 1130 | break; |
1132 | 1131 | ||
1133 | // The following types cannot be copied | 1132 | // The following types cannot be copied |
1134 | case LuaType::CDATA: | 1133 | case LuaType::CDATA: |
1135 | [[fallthrough]]; | 1134 | [[fallthrough]]; |
1136 | case LuaType::THREAD: | 1135 | case LuaType::THREAD: |
1137 | ret = false; | 1136 | _ret = false; |
1138 | break; | 1137 | break; |
1139 | } | 1138 | } |
1140 | 1139 | ||
1141 | STACK_CHECK(L2, ret ? 1 : 0); | 1140 | STACK_CHECK(L2, _ret ? 1 : 0); |
1142 | STACK_CHECK(L1, 0); | 1141 | STACK_CHECK(L1, 0); |
1143 | return ret; | 1142 | return _ret; |
1144 | } | 1143 | } |
1145 | 1144 | ||
1146 | // ################################################################################################# | 1145 | // ################################################################################################# |
@@ -1189,30 +1188,30 @@ static char const* vt_names[] = { | |||
1189 | return InterCopyResult::Success; | 1188 | return InterCopyResult::Success; |
1190 | } | 1189 | } |
1191 | 1190 | ||
1192 | InterCopyResult result{ InterCopyResult::Success }; | 1191 | InterCopyResult _result{ InterCopyResult::Success }; |
1193 | // package.loaders is renamed package.searchers in Lua 5.2 | 1192 | // package.loaders is renamed package.searchers in Lua 5.2 |
1194 | // but don't copy it anyway, as the function names change depending on the slot index! | 1193 | // but don't copy it anyway, as the function names change depending on the slot index! |
1195 | // users should provide an on_state_create function to setup custom loaders instead | 1194 | // users should provide an on_state_create function to setup custom loaders instead |
1196 | // don't copy package.preload in keeper states (they don't know how to translate functions) | 1195 | // don't copy package.preload in keeper states (they don't know how to translate functions) |
1197 | char const* entries[] = { "path", "cpath", (mode == LookupMode::LaneBody) ? "preload" : nullptr /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, nullptr }; | 1196 | char const* _entries[] = { "path", "cpath", (mode == LookupMode::LaneBody) ? "preload" : nullptr /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, nullptr }; |
1198 | for (char const* const entry : entries) { | 1197 | for (char const* const _entry : _entries) { |
1199 | if (!entry) { | 1198 | if (!_entry) { |
1200 | continue; | 1199 | continue; |
1201 | } | 1200 | } |
1202 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END(U), entry)); | 1201 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END(U), _entry)); |
1203 | lua_getfield(L1, L1_i, entry); | 1202 | lua_getfield(L1, L1_i, _entry); |
1204 | if (lua_isnil(L1, -1)) { | 1203 | if (lua_isnil(L1, -1)) { |
1205 | lua_pop(L1, 1); | 1204 | lua_pop(L1, 1); |
1206 | } else { | 1205 | } else { |
1207 | { | 1206 | { |
1208 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1207 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1209 | result = inter_move(1); // moves the entry to L2 | 1208 | _result = inter_move(1); // moves the entry to L2 |
1210 | STACK_CHECK(L1, 0); | 1209 | STACK_CHECK(L1, 0); |
1211 | } | 1210 | } |
1212 | if (result == InterCopyResult::Success) { | 1211 | if (_result == InterCopyResult::Success) { |
1213 | lua_setfield(L2, -2, entry); // set package[entry] | 1212 | lua_setfield(L2, -2, _entry); // set package[entry] |
1214 | } else { | 1213 | } else { |
1215 | lua_pushfstring(L1, "failed to copy package entry %s", entry); | 1214 | lua_pushfstring(L1, "failed to copy package entry %s", _entry); |
1216 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later | 1215 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later |
1217 | if (mode == LookupMode::LaneBody) { | 1216 | if (mode == LookupMode::LaneBody) { |
1218 | raise_lua_error(getErrL()); | 1217 | raise_lua_error(getErrL()); |
@@ -1223,7 +1222,7 @@ static char const* vt_names[] = { | |||
1223 | } | 1222 | } |
1224 | } | 1223 | } |
1225 | STACK_CHECK(L1, 0); | 1224 | STACK_CHECK(L1, 0); |
1226 | return result; | 1225 | return _result; |
1227 | } | 1226 | } |
1228 | 1227 | ||
1229 | // ################################################################################################# | 1228 | // ################################################################################################# |
@@ -1237,8 +1236,8 @@ static char const* vt_names[] = { | |||
1237 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "InterCopyContext::inter_copy()\n" INDENT_END(U))); | 1236 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "InterCopyContext::inter_copy()\n" INDENT_END(U))); |
1238 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1237 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1239 | 1238 | ||
1240 | int const top_L1{ lua_gettop(L1) }; | 1239 | int const _top_L1{ lua_gettop(L1) }; |
1241 | if (n_ > top_L1) { | 1240 | if (n_ > _top_L1) { |
1242 | // requesting to copy more than is available? | 1241 | // requesting to copy more than is available? |
1243 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END(U))); | 1242 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END(U))); |
1244 | return InterCopyResult::NotEnoughValues; | 1243 | return InterCopyResult::NotEnoughValues; |
@@ -1252,36 +1251,36 @@ static char const* vt_names[] = { | |||
1252 | * function entries, avoiding the same entries to be passed on as multiple | 1251 | * function entries, avoiding the same entries to be passed on as multiple |
1253 | * copies. ESSENTIAL i.e. for handling upvalue tables in the right manner! | 1252 | * copies. ESSENTIAL i.e. for handling upvalue tables in the right manner! |
1254 | */ | 1253 | */ |
1255 | int const top_L2{ lua_gettop(L2) }; // L2: ... | 1254 | int const _top_L2{ lua_gettop(L2) }; // L2: ... |
1256 | lua_newtable(L2); // L2: ... cache | 1255 | lua_newtable(L2); // L2: ... cache |
1257 | 1256 | ||
1258 | char tmpBuf[16]; | 1257 | char _tmpBuf[16]; |
1259 | char const* const pBuf{ U->verboseErrors ? tmpBuf : "?" }; | 1258 | char const* const _pBuf{ U->verboseErrors ? _tmpBuf : "?" }; |
1260 | InterCopyContext c{ U, L2, L1, CacheIndex{ top_L2 + 1 }, {}, VT::NORMAL, mode, pBuf }; | 1259 | InterCopyContext _c{ U, L2, L1, CacheIndex{ _top_L2 + 1 }, {}, VT::NORMAL, mode, _pBuf }; |
1261 | bool copyok{ true }; | 1260 | bool _copyok{ true }; |
1262 | STACK_CHECK_START_REL(L1, 0); | 1261 | STACK_CHECK_START_REL(L1, 0); |
1263 | for (int i{ top_L1 - n_ + 1 }, j{ 1 }; i <= top_L1; ++i, ++j) { | 1262 | for (int i{ _top_L1 - n_ + 1 }, j{ 1 }; i <= _top_L1; ++i, ++j) { |
1264 | if (U->verboseErrors) { | 1263 | if (U->verboseErrors) { |
1265 | sprintf(tmpBuf, "arg_%d", j); | 1264 | sprintf(_tmpBuf, "arg_%d", j); |
1266 | } | 1265 | } |
1267 | c.L1_i = SourceIndex{ i }; | 1266 | _c.L1_i = SourceIndex{ i }; |
1268 | copyok = c.inter_copy_one(); // L2: ... cache {}n | 1267 | _copyok = _c.inter_copy_one(); // L2: ... cache {}n |
1269 | if (!copyok) { | 1268 | if (!_copyok) { |
1270 | break; | 1269 | break; |
1271 | } | 1270 | } |
1272 | } | 1271 | } |
1273 | STACK_CHECK(L1, 0); | 1272 | STACK_CHECK(L1, 0); |
1274 | 1273 | ||
1275 | if (copyok) { | 1274 | if (_copyok) { |
1276 | STACK_CHECK(L2, n_ + 1); | 1275 | STACK_CHECK(L2, n_ + 1); |
1277 | // Remove the cache table. Persistent caching would cause i.e. multiple | 1276 | // Remove the cache table. Persistent caching would cause i.e. multiple |
1278 | // messages passed in the same table to use the same table also in receiving end. | 1277 | // messages passed in the same table to use the same table also in receiving end. |
1279 | lua_remove(L2, top_L2 + 1); | 1278 | lua_remove(L2, _top_L2 + 1); // L2: ... {}n |
1280 | return InterCopyResult::Success; | 1279 | return InterCopyResult::Success; |
1281 | } | 1280 | } |
1282 | 1281 | ||
1283 | // error -> pop everything from the target state stack | 1282 | // error -> pop everything from the target state stack |
1284 | lua_settop(L2, top_L2); | 1283 | lua_settop(L2, _top_L2); |
1285 | STACK_CHECK(L2, 0); | 1284 | STACK_CHECK(L2, 0); |
1286 | return InterCopyResult::Error; | 1285 | return InterCopyResult::Error; |
1287 | } | 1286 | } |
@@ -1290,7 +1289,7 @@ static char const* vt_names[] = { | |||
1290 | 1289 | ||
1291 | [[nodiscard]] InterCopyResult InterCopyContext::inter_move(int n_) const | 1290 | [[nodiscard]] InterCopyResult InterCopyContext::inter_move(int n_) const |
1292 | { | 1291 | { |
1293 | InterCopyResult const ret{ inter_copy(n_) }; | 1292 | InterCopyResult const _ret{ inter_copy(n_) }; |
1294 | lua_pop(L1, n_); | 1293 | lua_pop(L1, n_); |
1295 | return ret; | 1294 | return _ret; |
1296 | } | 1295 | } |