diff --git a/README.md b/README.md index 6c97bd4c..8c348a60 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,7 @@ Transparent engine tweaks — no API to call, they just fix a vanilla limitation | Lua 5.1 syntax | Compiles the Lua 5.1 length (`#`), modulo (`%`), `...`-expression, `0x` hex-literal, and leveled long bracket (`[=[ ]=]`) syntax that vanilla's Lua 5.0 rejects, by rewriting addon source before it compiles. Each addon file also receives its `(name, table)` through `...` (`local name, tbl = ...`). See [Lua 5.1 syntax](docs/API.md#lua-51-syntax). | | Lua 5.1 string methods | Every string value accepts method calls — `("asd"):upper()`, `("%d gold"):format(n)`, `msg:match("^!(%w+)")` — resolving through the `string` table, the way Lua 5.1 works. Vanilla's Lua 5.0 errors with `attempt to index a string value`. Works on literals and variables, in-world and on the login screen, and inside coroutines. See [String methods](docs/API.md#string-methods-supper-sformat). | | Lua 5.1 environment protection | `getfenv` / `setfenv` honor a `__environment` metatable field — the Lua 5.1 sandbox form — in addition to vanilla's raw `__fenv`. See [getfenv / setfenv environment protection](docs/API.md#getfenv--setfenv-environment-protection). | +| Lua 5.1 table lengths | Heals stale 5.0 table lengths: when a table's keys were cleared without `table.setn` (what Lua-5.1-detecting libraries like the Ace2 era's Dewdrop/FuBar skip on purpose), `table.getn` / `insert` / `remove` / `concat` / `sort` return the true border, the Lua 5.1 answer, instead of a stale count. Fixes the silent breakage of 5.1-probing libraries (e.g. minimap-button menus). Vararg `arg` tables (explicit `n` field) and `table.setn` users are unaffected. See [Stale table lengths](docs/API.md#stale-table-lengths-51-healing). | | Multi-flavor & conditional TOC loading | Loads modern multi-flavor addons that ship one folder. Selects a version-specific TOC (`_ClassicAPI.toc` or `_Turtle.toc`) and the matching keybinding file (`Bindings_ClassicAPI.xml` / `Bindings_Turtle.xml`), accepts a comma-separated `## Interface:` version list (compatible when it includes the client version `11200`), and honors per-line `[AllowLoadGameType]` / `[AllowLoadTextLocale]` conditions and `[Family]` / `[Game]` / `[TextLocale]` path variables inside a TOC. See [Conditional and multi-flavor TOC loading](docs/API.md#conditional-and-multi-flavor-toc-loading). | | SavedVariables loaded first | Honors the modern `## LoadSavedVariablesFirst` TOC directive: a flagged addon's SavedVariables load before its Lua runs, so file-scope code sees restored config (instead of vanilla's `nil`). See [SavedVariables loaded first](docs/API.md#savedvariables-loaded-first). | | Modern script-handler arguments | Frame-script handlers receive their values as positional arguments — `OnMouseWheel(self, delta)`, `OnClick(self, button)`, `OnEvent(self, event, ...)`, etc. — the way 5.1+ clients do, so modern addon ports work unmodified. The `this` / `arg1` globals stay set. A handler that declares no parameters is unaffected. A handler that declared a parameter and expected nil now receives its real value. On by default. `SetModernScriptArgs(false)` restores exact vanilla dispatch. See [SetModernScriptArgs](docs/API.md#setmodernscriptargsenable--getmodernscriptargs). | diff --git a/docs/API.md b/docs/API.md index cf1367ad..404f9271 100644 --- a/docs/API.md +++ b/docs/API.md @@ -352,6 +352,7 @@ build instructions. - [`table.wipe(t)`](#tablewipet) - [`table.count(tbl)`](#tablecounttbl) - [`table.maxn(t)`](#tablemaxnt) + - [Stale table lengths (5.1 healing)](#stale-table-lengths-51-healing) - [`Mixin(object, ...)` / `CreateFromMixins(...)`](#mixinobject--createfrommixins) - [`string.match` / `string.gmatch`](#stringmatch--stringgmatch) - [`string.gsub` table replacement](#stringgsub-table-replacement) @@ -8754,6 +8755,32 @@ table.maxn({ a = 1 }) -- 0 Errors on non-table input. +### Stale table lengths (5.1 healing) + +Lua 5.0 stores a table's `table.insert` / `table.getn` length **out of +band**. Clearing a table's keys (`for k in pairs(t) do t[k] = nil end`) +does not reset it — 5.0 code must also call `table.setn(t, 0)`. Lua 5.1 +removed `setn` entirely; lengths are computed from the table itself. + +That difference bites libraries that feature-detect Lua 5.1 (the Ace2 +era's standard probe) and then skip `setn` — correct on real 5.1, but on +this client it used to leave stale lengths: `table.insert` appended past +the cleared slots and menus built from those tables silently broke +(Dewdrop menus behind FuBar minimap buttons, for example). + +ClassicAPI heals the read. When the stored length points at a nil slot +and the table has no explicit `n` field, `table.getn` — and everything +built on it: `table.insert`, `table.remove`, `table.concat`, +`table.sort`, `table.foreachi`, `unpack` — returns the true border, the +same answer Lua 5.1 gives. Two things deliberately keep their old +behavior: + +- A table with an explicit numeric `n` field (the vararg `arg` contract) + keeps its stored count, so trailing nils survive + (`unpack({10, nil, 30, n = 3})` still returns all three slots). +- `table.setn` still works; the heal only changes the answer when the + stored length points past the last populated slot. + ### `Mixin(object, ...)` / `CreateFromMixins(...)` The FrameXML table-mixin primitives, provided as engine C functions. diff --git a/src/table/Length.cpp b/src/table/Length.cpp new file mode 100644 index 00000000..d63f8a2e --- /dev/null +++ b/src/table/Length.cpp @@ -0,0 +1,120 @@ +// 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 . + +// Stale table-length healing — Lua 5.1 border semantics for 5.0's +// out-of-band lengths. +// +// Lua 5.0 stores a table's `table.insert`/`getn` length OUT of band (a +// `t.n` field, else the registry's weak LUA_SIZES table). Clearing a +// table's keys (`for k in pairs(t) do t[k] = nil end`) does NOT reset it — +// 5.0 code must call `table.setn(t, 0)`. Lua 5.1 removed all of that: +// lengths are computed borders, and a cleared table is length 0 +// automatically. +// +// That difference breaks the standard Ace2-era dual-compatibility idiom +// the moment our 5.1 syntax backport makes their probe pass: +// +// local lua51 = loadstring("return function(...) return ... end") +// and true or false +// local table_setn = lua51 and function() end or table.setn +// +// With `lua51` true (the `...`-expression now compiles via +// LuaSyntax::Transpile), `table_setn` is a NO-OP — correct on real 5.1, +// wrong on this VM, whose table functions still read the out-of-band +// length. Cleared-and-reused tables keep stale lengths: `table.insert` +// appends past the nil'd slots and `table.getn` counts entries that no +// longer exist. Verified live: Dewdrop-2.0's menus (the SuperAPI / +// NampowerSettings minimap buttons) break exactly this way, and work +// again when the probe is forced back to 5.0. +// +// Since we made the client PASS 5.1 probes, 5.1 length semantics must +// hold. The engine funnels every length read through ONE function — +// `luaL_getn` (LUAL_GETN, consulted by table.insert / getn / remove / +// concat / sort / foreachi, plus our `unpack`) — so a single co-hook +// closes the gap for every consumer at once: +// +// 1. Reported slot populated (`t[n] ~= nil`) → return n unchanged. The +// healthy path costs one rawgeti. +// 2. `t[n]` nil but `t.n` is a number → return n unchanged. An explicit +// `n` field is the 5.0 vararg-table contract (`arg` = {n=3, holes}), +// where trailing nils are intentional — healing it would corrupt +// vararg counts. +// 3. Otherwise the out-of-band length is stale (keys cleared without +// setn): scan down for the true border, 5.1-style, and return that. +// Read-only — no write-back, so a length read never mutates state; +// the engine's own `table.insert` calls `luaL_setn(n+1)` on the next +// append, which re-syncs the stored length by itself. +// +// `table.setn` itself still works for code that calls it — the heal only +// changes the answer when the stored length points past the border, which +// is the same answer real 5.1 would give. + +#include "Game.h" +#include "Offsets.h" + +namespace Table::Length { + +namespace { + +using LuaLGetN_t = int(__fastcall *)(void *L, int idx); +using RawGetI_t = void(__fastcall *)(void *L, int idx, int n); + +LuaLGetN_t g_getnOriginal = nullptr; + +// True if t[k] (rawgeti) is nil. Balances the stack. +bool SlotIsNil(void *L, int absIdx, int k) { + reinterpret_cast(Offsets::LUA_RAWGETI)(L, absIdx, k); + const bool isNil = Game::Lua::Type(L, -1) == Game::Lua::TYPE_NIL; + Game::Lua::SetTop(L, -2); + return isNil; +} + +int __fastcall LuaLGetN_h(void *L, int idx) { + const int n = g_getnOriginal(L, idx); + if (n <= 0) + return n; + + // Normalize a relative index once — the checks below push/pop around + // the access, and the `t.n` probe has a key on the stack at its + // access point, which would shift a negative index. Pseudo-indices + // (registry and below, <= -10000) pass through unchanged. + int absIdx = idx; + if (idx < 0 && idx > -10000) + absIdx = Game::Lua::GetTop(L) + idx + 1; + + if (!SlotIsNil(L, absIdx, n)) + return n; // healthy — the reported last slot is populated + + // Explicit `t.n` count (vararg-table contract): trailing nils are + // intentional, keep the stored length. + Game::Lua::PushString(L, "n"); + Game::Lua::RawGet(L, absIdx); + const bool hasExplicitN = Game::Lua::Type(L, -1) == Game::Lua::TYPE_NUMBER; + Game::Lua::SetTop(L, -2); + if (hasExplicitN) + return n; + + // Stale out-of-band length — heal to the true border. + int b = n - 1; + while (b > 0 && SlotIsNil(L, absIdx, b)) + --b; + return b; +} + +const Game::HookAutoRegister _hook{Offsets::LUAL_GETN, + reinterpret_cast(&LuaLGetN_h), + reinterpret_cast(&g_getnOriginal)}; + +} // namespace + +} // namespace Table::Length