diff options
Diffstat (limited to '')
-rw-r--r-- | src/intercopycontext.cpp | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 1ccef80..e56b9fd 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp | |||
@@ -106,17 +106,16 @@ THE SOFTWARE. | |||
106 | lua_pop(L1, (mode == LookupMode::FromKeeper) ? 1 : 2); // L1: ... v ... | 106 | lua_pop(L1, (mode == LookupMode::FromKeeper) ? 1 : 2); // L1: ... v ... |
107 | STACK_CHECK(L1, 0); | 107 | STACK_CHECK(L1, 0); |
108 | if (_fqn.empty() && !lua_istable(L1, L1_i)) { // raise an error if we try to send an unknown function (but not for tables) | 108 | if (_fqn.empty() && !lua_istable(L1, L1_i)) { // raise an error if we try to send an unknown function (but not for tables) |
109 | _fqn = std::string_view{}; // just in case | ||
110 | // try to discover the name of the function we want to send | 109 | // try to discover the name of the function we want to send |
111 | kLaneNameRegKey.pushValue(L1); // L1: ... v ... lane_name | 110 | kLaneNameRegKey.pushValue(L1); // L1: ... v ... lane_name |
112 | char const* _from{ lua_tostring(L1, -1) }; | 111 | std::string_view const _from{ luaG_tostring(L1, -1) }; |
113 | lua_pushcfunction(L1, LG_nameof); // L1: ... v ... lane_name LG_nameof | 112 | lua_pushcfunction(L1, LG_nameof); // L1: ... v ... lane_name LG_nameof |
114 | lua_pushvalue(L1, L1_i); // L1: ... v ... lane_name LG_nameof t | 113 | lua_pushvalue(L1, L1_i); // L1: ... v ... lane_name LG_nameof t |
115 | lua_call(L1, 1, 2); // L1: ... v ... lane_name "type" "name"|nil | 114 | lua_call(L1, 1, 2); // L1: ... v ... lane_name "type" "name"|nil |
116 | char const* _typewhat{ (luaG_type(L1, -2) == LuaType::STRING) ? lua_tostring(L1, -2) : luaL_typename(L1, -2) }; | 115 | std::string_view const _typewhat{ (luaG_type(L1, -2) == LuaType::STRING) ? luaG_tostring(L1, -2) : luaG_typename(L1, -2) }; |
117 | // second return value can be nil if the table was not found | 116 | // second return value can be nil if the table was not found |
118 | // probable reason: the function was removed from the source Lua state before Lanes was required. | 117 | // probable reason: the function was removed from the source Lua state before Lanes was required. |
119 | char const *_what, *_gotchaA, *_gotchaB; | 118 | std::string_view _what, _gotchaA, _gotchaB; |
120 | if (lua_isnil(L1, -1)) { | 119 | if (lua_isnil(L1, -1)) { |
121 | _gotchaA = " referenced by"; | 120 | _gotchaA = " referenced by"; |
122 | _gotchaB = "\n(did you remove it from the source Lua state before requiring Lanes?)"; | 121 | _gotchaB = "\n(did you remove it from the source Lua state before requiring Lanes?)"; |
@@ -124,9 +123,9 @@ THE SOFTWARE. | |||
124 | } else { | 123 | } else { |
125 | _gotchaA = ""; | 124 | _gotchaA = ""; |
126 | _gotchaB = ""; | 125 | _gotchaB = ""; |
127 | _what = (luaG_type(L1, -1) == LuaType::STRING) ? lua_tostring(L1, -1) : luaL_typename(L1, -1); | 126 | _what = (luaG_type(L1, -1) == LuaType::STRING) ? luaG_tostring(L1, -1) : luaG_typename(L1, -1); |
128 | } | 127 | } |
129 | raise_luaL_error(L1, "%s%s '%s' not found in %s origin transfer database.%s", _typewhat, _gotchaA, _what, _from ? _from : "main", _gotchaB); | 128 | raise_luaL_error(L1, "%s%s '%s' not found in %s origin transfer database.%s", _typewhat.data(), _gotchaA.data(), _what.data(), _from.empty() ? "main" : _from.data(), _gotchaB.data()); |
130 | } | 129 | } |
131 | STACK_CHECK(L1, 0); | 130 | STACK_CHECK(L1, 0); |
132 | return _fqn; | 131 | return _fqn; |
@@ -283,12 +282,9 @@ void InterCopyContext::copyFunction() const | |||
283 | 282 | ||
284 | // Set upvalues (originally set to 'nil' by 'lua_load') | 283 | // Set upvalues (originally set to 'nil' by 'lua_load') |
285 | for (int const _func_index{ lua_gettop(L2) - _n }; _n > 0; --_n) { | 284 | for (int const _func_index{ lua_gettop(L2) - _n }; _n > 0; --_n) { |
286 | [[maybe_unused]] char const* _upname{ lua_setupvalue(L2, _func_index, _n) }; // L2: ... {cache} ... function | 285 | // assign upvalue, popping it from the stack |
287 | // | 286 | [[maybe_unused]] std::string_view const _upname{ lua_setupvalue(L2, _func_index, _n) };// L2: ... {cache} ... function |
288 | // "assigns the value at the top of the stack to the upvalue and returns its name. | 287 | LUA_ASSERT(L1, !_upname.empty()); // not having enough slots? |
289 | // It also pops the value from the stack." | ||
290 | |||
291 | LUA_ASSERT(L1, _upname); // not having enough slots? | ||
292 | } | 288 | } |
293 | // once all upvalues have been set we are left | 289 | // once all upvalues have been set we are left |
294 | // with the function at the top of the stack // L2: ... {cache} ... function | 290 | // with the function at the top of the stack // L2: ... {cache} ... function |
@@ -328,19 +324,19 @@ void InterCopyContext::lookupNativeFunction() const | |||
328 | // anything other than function or table should not happen! | 324 | // anything other than function or table should not happen! |
329 | if (!lua_isfunction(L2, -1) && !lua_istable(L2, -1)) { | 325 | if (!lua_isfunction(L2, -1) && !lua_istable(L2, -1)) { |
330 | kLaneNameRegKey.pushValue(L1); // L1: ... f ... lane_name | 326 | kLaneNameRegKey.pushValue(L1); // L1: ... f ... lane_name |
331 | char const* const _from{ lua_tostring(L1, -1) }; | 327 | std::string_view const _from{ luaG_tostring(L1, -1) }; |
332 | lua_pop(L1, 1); // L1: ... f ... | 328 | lua_pop(L1, 1); // L1: ... f ... |
333 | kLaneNameRegKey.pushValue(L2); // L1: ... f ... L2: {} f lane_name | 329 | kLaneNameRegKey.pushValue(L2); // L1: ... f ... L2: {} f lane_name |
334 | char const* const _to{ lua_tostring(L2, -1) }; | 330 | std::string_view const _to{ luaG_tostring(L2, -1) }; |
335 | lua_pop(L2, 1); // L2: {} f | 331 | lua_pop(L2, 1); // L2: {} f |
336 | // 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 | 332 | // 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 |
337 | raise_luaL_error( | 333 | raise_luaL_error( |
338 | getErrL(), | 334 | getErrL(), |
339 | "%s%s: function '%s' not found in %s destination transfer database.", | 335 | "%s%s: function '%s' not found in %s destination transfer database.", |
340 | lua_isnil(L2, -1) ? "" : "INTERNAL ERROR IN ", | 336 | lua_isnil(L2, -1) ? "" : "INTERNAL ERROR IN ", |
341 | _from ? _from : "main", | 337 | _from.empty() ? "main" : _from.data(), |
342 | _fqn.data(), | 338 | _fqn.data(), |
343 | _to ? _to : "main"); | 339 | _to.empty() ? "main" : _to.data()); |
344 | return; | 340 | return; |
345 | } | 341 | } |
346 | lua_remove(L2, -2); // L2: f | 342 | lua_remove(L2, -2); // L2: f |
@@ -452,18 +448,18 @@ void InterCopyContext::copyCachedFunction() const | |||
452 | return false; | 448 | return false; |
453 | } else if (!lua_istable(L2, -1)) { // this can happen if someone decides to replace same already registered item (for a example a standard lib function) with a table | 449 | } else if (!lua_istable(L2, -1)) { // this can happen if someone decides to replace same already registered item (for a example a standard lib function) with a table |
454 | kLaneNameRegKey.pushValue(L1); // L1: ... t ... lane_name | 450 | kLaneNameRegKey.pushValue(L1); // L1: ... t ... lane_name |
455 | char const* _from{ lua_tostring(L1, -1) }; | 451 | std::string_view const _from{ luaG_tostring(L1, -1) }; |
456 | lua_pop(L1, 1); // L1: ... t ... | 452 | lua_pop(L1, 1); // L1: ... t ... |
457 | kLaneNameRegKey.pushValue(L2); // L1: ... t ... L2: {} t lane_name | 453 | kLaneNameRegKey.pushValue(L2); // L1: ... t ... L2: {} t lane_name |
458 | char const* _to{ lua_tostring(L2, -1) }; | 454 | std::string_view const _to{ luaG_tostring(L2, -1) }; |
459 | lua_pop(L2, 1); // L1: ... t ... L2: {} t | 455 | lua_pop(L2, 1); // L1: ... t ... L2: {} t |
460 | raise_luaL_error( | 456 | raise_luaL_error( |
461 | getErrL(), | 457 | getErrL(), |
462 | "%s: source table '%s' found as %s in %s destination transfer database.", | 458 | "%s: source table '%s' found as %s in %s destination transfer database.", |
463 | _from ? _from : "main", | 459 | _from.empty() ? "main" : _from.data(), |
464 | _fqn, | 460 | _fqn.data(), |
465 | luaG_typename(L2, -1).data(), | 461 | luaG_typename(L2, -1).data(), |
466 | _to ? _to : "main"); | 462 | _to.empty() ? "main" : _to.data()); |
467 | } | 463 | } |
468 | lua_remove(L2, -2); // L1: ... t ... L2: t | 464 | lua_remove(L2, -2); // L1: ... t ... L2: t |
469 | break; | 465 | break; |
@@ -1260,7 +1256,7 @@ namespace { | |||
1260 | STACK_CHECK(L1, 0); | 1256 | STACK_CHECK(L1, 0); |
1261 | } | 1257 | } |
1262 | if (_result == InterCopyResult::Success) { | 1258 | if (_result == InterCopyResult::Success) { |
1263 | lua_setfield(L2, -2, _entry.data()); // set package[entry] | 1259 | luaG_setfield(L2, -2, _entry); // set package[entry] |
1264 | } else { | 1260 | } else { |
1265 | std::string_view const _msg{ luaG_pushstring(L1, "failed to copy package.%s", _entry.data()) }; | 1261 | std::string_view const _msg{ luaG_pushstring(L1, "failed to copy package.%s", _entry.data()) }; |
1266 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later | 1262 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later |