mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-15 23:50:38 +00:00
Systematic sweep of the Lua 5.1 stdlib delta against what ClassicAPI
already backports found five gaps, all confirmed by in-game probes:
* unpack(list [, i [, j]]) -- 5.0's unpack silently ignored the range
args, so unpack(t, 2) returned the WHOLE table (wrong values, no
error; the only silent gap). Replaced with a port of 5.1's
luaB_unpack: ranges via luaL_getn + lua_rawgeti, so a vararg arg
table's n field keeps its embedded-nil count. Both states.
* string.gsub table replacement -- 5.0 accepts only string/function
and errors on a table. Shim on string.gsub AND the bare gsub global:
a table becomes an adapter closure over the engine's own gsub
(FUN_LUA_STR_GSUB = 0x007FD0E0, pinned by registration-table
position, name string, and decode). Because this engine's add_value
drops a non-string function result (match -> empty) where 5.1 keeps
the match on nil/false, the shim wraps the pattern in one outer
capture (^/$ anchors kept outside) so the closure receives the whole
match and can hand it back for missing keys -- exact 5.1 keep-match.
Patterns with a %1..%9 backref skip the wrap (renumbering hazard);
their missing-key behavior stays the engine's (documented residual).
* collectgarbage 5.1 options -- "count" -> gcinfo, "collect" ->
original collectgarbage(0), "step" -> loop-safe true, stop/restart/
setpause/setstepmul -> no-op 0, numbers forwarded untouched. The
originals are function objects stashed in the state's registry
before the overwrite, so no new engine offsets and each /reload
re-stashes the fresh engine original. Both states.
* table.maxn -- 5.1 luaB_maxn port (largest positive numeric key,
full next-walk so nil holes don't stop it). Both states.
* coroutine.running -- returns the running coroutine, nil on the main
thread. 5.0 has no lua_pushthread, so the thread TValue (tag 8 +
the running L, this build's {tt@0, value@+8} layout) is written to
the stack raw; main-thread detection via the registration-time L.
Verified in-game: ranged/holed/n-field unpack shapes, all four gsub
shapes (populated table, keep-match on missing key, whole-match key,
anchored pattern), maxn past a nil hole, collectgarbage("count"/
"collect"), and coroutine.running from main + inside a wrap.
Docs: five new API.md sections + TOC entries; README Lua list updated.
146 lines
5.9 KiB
C++
146 lines
5.9 KiB
C++
// This file is part of ClassicAPI.
|
|
//
|
|
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
|
|
// of the GNU General Public License as published by the Free Software Foundation, either
|
|
// version 3 of the License, or (at your option) any later version.
|
|
//
|
|
// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
// PURPOSE. See the GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License along with
|
|
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
// `collectgarbage(opt [, arg])` — accept Lua 5.1's string options on the
|
|
// 5.0 collector. 5.0's collectgarbage takes only an optional NUMBER (a KB
|
|
// threshold for the next cycle) and errors on every 5.1 option (verified
|
|
// in-game: `collectgarbage("count")` → "bad argument #1 (number expected,
|
|
// got string)"). Shim dispatch:
|
|
//
|
|
// "count" → KB of Lua memory in use — the engine's own gcinfo()
|
|
// value, the same number 5.1's "count" reports.
|
|
// "collect" → original collectgarbage(0): threshold 0 forces a full
|
|
// cycle, the canonical 5.0 "collect now". Returns 0.
|
|
// "step" → no-op returning true ("cycle finished"): the 5.0 GC
|
|
// cannot step incrementally, and true is the loop-safe
|
|
// sentinel (`repeat until collectgarbage("step")` exits
|
|
// immediately instead of spinning forever).
|
|
// "stop" / "restart" / "setpause" / "setstepmul"
|
|
// → accepted no-ops returning 0 — the 5.0 GC has no toggle
|
|
// or tuning; callers just lose the optimization.
|
|
// number / no argument
|
|
// → forwarded to the original untouched (5.0 threshold
|
|
// semantics preserved for vanilla-era callers).
|
|
//
|
|
// The originals (`collectgarbage`, `gcinfo`) are function objects stashed
|
|
// in the state's registry at registration, BEFORE the global is
|
|
// overwritten — no raw engine address needed, and each /reload re-stashes
|
|
// the freshly re-registered engine original (module callbacks run after
|
|
// the engine's own registration pass). Registered on BOTH states.
|
|
|
|
#include "Game.h"
|
|
|
|
#include <cstring>
|
|
|
|
namespace BaseLib::CollectGarbage {
|
|
|
|
namespace {
|
|
|
|
constexpr const char *kOrigCollect = "__classicapi_orig_collectgarbage";
|
|
constexpr const char *kOrigGcInfo = "__classicapi_orig_gcinfo";
|
|
|
|
// Pushes registry[key]; true when it holds a function (left on the
|
|
// stack), false otherwise (stack unchanged).
|
|
bool PushStashed(void *L, const char *key) {
|
|
Game::Lua::PushString(L, key);
|
|
Game::Lua::RawGet(L, Game::Lua::REGISTRY_INDEX);
|
|
if (Game::Lua::Type(L, -1) == Game::Lua::TYPE_FUNCTION)
|
|
return true;
|
|
Game::Lua::SetTop(L, -2);
|
|
return false;
|
|
}
|
|
|
|
int __fastcall Script_collectgarbage(void *L) {
|
|
if (Game::Lua::Type(L, 1) == Game::Lua::TYPE_STRING) {
|
|
const char *opt = Game::Lua::ToString(L, 1);
|
|
if (std::strcmp(opt, "count") == 0) {
|
|
if (PushStashed(L, kOrigGcInfo)) {
|
|
Game::Lua::Call(L, 0, 1); // gcinfo's first return = KB in use
|
|
return 1;
|
|
}
|
|
Game::Lua::PushNumber(L, 0.0); // no gcinfo on this state
|
|
return 1;
|
|
}
|
|
if (std::strcmp(opt, "collect") == 0) {
|
|
if (PushStashed(L, kOrigCollect)) {
|
|
Game::Lua::PushNumber(L, 0.0); // threshold 0 = collect now
|
|
Game::Lua::Call(L, 1, 0);
|
|
}
|
|
Game::Lua::PushNumber(L, 0.0); // 5.1 returns 0 for "collect"
|
|
return 1;
|
|
}
|
|
if (std::strcmp(opt, "step") == 0) {
|
|
Game::Lua::PushBool(L, true); // "cycle finished" — see header
|
|
return 1;
|
|
}
|
|
if (std::strcmp(opt, "stop") == 0 || std::strcmp(opt, "restart") == 0 ||
|
|
std::strcmp(opt, "setpause") == 0 ||
|
|
std::strcmp(opt, "setstepmul") == 0) {
|
|
Game::Lua::PushNumber(L, 0.0);
|
|
return 1;
|
|
}
|
|
Game::Lua::Error(L, "bad argument #1 to 'collectgarbage' (invalid option '%s')",
|
|
opt);
|
|
return 0; // unreachable
|
|
}
|
|
|
|
// Number or no argument: original 5.0 threshold behavior, forwarded.
|
|
const int n = Game::Lua::GetTop(L);
|
|
if (!PushStashed(L, kOrigCollect))
|
|
return 0; // no original captured — inert
|
|
for (int idx = 1; idx <= n; ++idx)
|
|
Game::Lua::PushValue(L, idx);
|
|
Game::Lua::Call(L, n, Game::Lua::MULTRET);
|
|
return Game::Lua::GetTop(L) - n;
|
|
}
|
|
|
|
// Stashes the function at `_G[name]` under registry[key]; true when a
|
|
// function was actually stashed.
|
|
bool Stash(void *L, const char *name, const char *key) {
|
|
Game::Lua::PushString(L, name);
|
|
Game::Lua::GetTable(L, Game::Lua::GLOBALS_INDEX); // [fn?]
|
|
if (Game::Lua::Type(L, -1) != Game::Lua::TYPE_FUNCTION) {
|
|
Game::Lua::SetTop(L, -2);
|
|
return false;
|
|
}
|
|
Game::Lua::PushString(L, key); // [fn, key]
|
|
Game::Lua::Insert(L, -2); // [key, fn]
|
|
Game::Lua::RawSet(L, Game::Lua::REGISTRY_INDEX);
|
|
return true;
|
|
}
|
|
|
|
void DoRegister(bool glue) {
|
|
void *L = Game::Lua::State();
|
|
if (L == nullptr)
|
|
return;
|
|
// Stash BEFORE overwriting; if the engine original is somehow absent,
|
|
// leave the global untouched rather than installing a dead shim.
|
|
if (!Stash(L, "collectgarbage", kOrigCollect))
|
|
return;
|
|
Stash(L, "gcinfo", kOrigGcInfo); // optional; "count" degrades to 0
|
|
if (glue)
|
|
Game::Lua::RegisterGlueFunction("collectgarbage", &Script_collectgarbage);
|
|
else
|
|
Game::Lua::RegisterGlobalFunction("collectgarbage", &Script_collectgarbage);
|
|
}
|
|
|
|
void RegisterInGame() { DoRegister(false); }
|
|
void RegisterGlue() { DoRegister(true); }
|
|
|
|
const Game::ModuleAutoRegister _autoreg{&RegisterInGame};
|
|
const Game::GlueModuleAutoRegister _autoregGlue{&RegisterGlue};
|
|
|
|
} // namespace
|
|
|
|
} // namespace BaseLib::CollectGarbage
|