diff --git a/docs/API.md b/docs/API.md index 42796fc0..ae93c202 100644 --- a/docs/API.md +++ b/docs/API.md @@ -8599,6 +8599,10 @@ link:sub(1, 5) - Writing to a string (`("x").y = 1`) still fails, the same as Lua 5.1. - Works everywhere strings do: in the world, on the login screen, and inside coroutines. +- To turn method resolution off for diagnosis, call + `_classicapi_SetStringMethods(false)`. Strings then error on index, the + stock 1.12 behavior. Use it only to answer "is this backport breaking + this addon?". ### `getfenv` / `setfenv` environment protection diff --git a/src/DllMain.cpp b/src/DllMain.cpp index 5daeab83..db97e0ab 100644 --- a/src/DllMain.cpp +++ b/src/DllMain.cpp @@ -16,6 +16,7 @@ #include "MinHook.h" #include "Offsets.h" #include "event/Custom.h" +#include "luasyntax/StringMethods.h" #include "model/DisplayInfo.h" #include "nameplate/Walk.h" #include "player/NameCache.h" @@ -65,6 +66,12 @@ static bool __fastcall FrameScript_Initialize_h() { // reload destroys every Model frame the jobs point at. Model::DisplayInfo::PrepareForReload(); + // Forget the captured `string` table before the world Lua state resets + // (this hook also fires on /logout, where the state's tables die) — + // string-method resolution fails closed (stock index error) until + // LoadScriptFunctions re-captures. + LuaSyntax::StringMethods::PrepareForReload(); + // Persist the name cache before the engine starts tearing down. // This hook fires on both `/reload` and `/logout` (the engine // re-initializes Lua state in both cases), giving us a clean diff --git a/src/baselib/StringLib.cpp b/src/baselib/StringLib.cpp index c4ef161d..0824028b 100644 --- a/src/baselib/StringLib.cpp +++ b/src/baselib/StringLib.cpp @@ -28,12 +28,11 @@ // it as a direct alias of the engine's `gfind` C function. // `strsplit` is a hand-rolled port of 3.3.5's `strsplit` (see below). // -// NOTE: only the `string.foo(s, ...)` call form works — NOT the -// `("x"):foo(...)` method sugar. WoW's Lua VM strips type-metatables for -// every value except tables/userdata (both `lua_setmetatable` and the VM's -// `luaT_gettmbyobj` hard-return "no metamethod" for strings), so strings have -// no `__index`, and there's no client-side way to add one short of hooking a -// hot VM-core function. This is the long-standing vanilla constraint. +// NOTE: both call forms work — `string.foo(s, ...)` AND the `("x"):foo(...)` +// method sugar. Method calls resolve through this same `string` table via +// LuaSyntax::StringMethods (the Lua 5.1 string-metatable backport), so +// everything registered here is automatically reachable as a method, on +// both states. #include "Game.h" #include "Offsets.h" diff --git a/src/luasyntax/StringMethods.cpp b/src/luasyntax/StringMethods.cpp index 35c800fb..7e807576 100644 --- a/src/luasyntax/StringMethods.cpp +++ b/src/luasyntax/StringMethods.cpp @@ -52,10 +52,21 @@ // (5.1 parity — its metatable pins the open-time table the same way). // Captures re-run per /reload (ModuleAutoRegister) and per glue boot // (GlueModuleAutoRegister), overwriting the anchor so the previous table is -// released. Between a state teardown and its re-capture a slot can be -// stale, but no Lua executes in those windows (engine C init only — and on -// this 5.0 engine no pre-existing code indexes strings anyway, since doing -// so always errored). +// released. Slots are cleared BEFORE their state can die, so a capture can +// never outlive the table it points at: PrepareForReload (called from +// FrameScript_Initialize_h, which fires on /reload AND /logout before the +// world Lua state is reset) clears the game slot, and the in-game +// registration clears the glue slot (once world scripts are loading, the +// glue state is gone; the next glue boot re-captures before any glue Lua +// runs). A cleared slot fails closed — strings error on index exactly as +// stock 1.12 until the re-capture. +// +// Kill switch: `_classicapi_SetStringMethods(false)` (registered on both +// states) makes the hook forward everything to the original resolver — +// exact stock behavior — for "is this backport breaking this addon?" +// diagnosis, mirroring `_classicapi_SetTranspileOption`. + +#include "StringMethods.h" #include "Game.h" #include "Offsets.h" @@ -94,6 +105,11 @@ struct StateSlot { StateSlot g_gameSlot; StateSlot g_glueSlot; +// Runtime kill switch — `_classicapi_SetStringMethods(false)` reverts to +// exact stock behavior (the hook forwards everything). Process-wide; both +// states share it. +bool g_enabled = true; + // Registry key anchoring the captured table against GC (rawset — the // registry has no metatable). One key per state's registry; re-capture // overwrites it, releasing the previously anchored table. @@ -107,7 +123,8 @@ IndexResolve_t g_indexNonTableOriginal = nullptr; void *__fastcall IndexNonTable_h(void *L, const void *obj, void *key, int loop) { // 5.1 backport: a string's `__index` is the `string` library table. - if (obj != nullptr && *static_cast(obj) == Game::Lua::TYPE_STRING) { + if (g_enabled && obj != nullptr && + *static_cast(obj) == Game::Lua::TYPE_STRING) { const void *G = Game::Read(L, OFF_L_GLOBALSTATE); const StateSlot *slot = nullptr; if (g_gameSlot.G == G) @@ -147,12 +164,42 @@ void CaptureFor(StateSlot &slot) { Game::Lua::SetTop(L, -2); // pop } -void RegisterLuaFunctions() { CaptureFor(g_gameSlot); } -void RegisterGlueFunctions() { CaptureFor(g_glueSlot); } +// `_classicapi_SetStringMethods(enabled)` — diagnostic kill switch. While +// false, string index falls through to the original resolver and errors as +// stock 1.12. Use only to answer "is this backport breaking this addon?". +int __fastcall Script_SetStringMethods(void *L) { + g_enabled = Game::Lua::ToBoolean(L, 1) != 0; + return 0; +} + +void RegisterLuaFunctions() { + // World scripts are loading ⇒ the glue state is gone (destroyed on the + // glue→world handoff). Clear its slot so a future glue global_State + // that lands at the same address can never match this stale capture. + g_glueSlot.G = nullptr; + g_glueSlot.tv.value = nullptr; + CaptureFor(g_gameSlot); + Game::Lua::RegisterGlobalFunction("_classicapi_SetStringMethods", + &Script_SetStringMethods); +} +void RegisterGlueFunctions() { + CaptureFor(g_glueSlot); + Game::Lua::RegisterGlueFunction("_classicapi_SetStringMethods", + &Script_SetStringMethods); +} const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; const Game::GlueModuleAutoRegister _glueAutoreg{&RegisterGlueFunctions}; } // namespace +void PrepareForReload() { + // The world Lua state is about to be reset (/reload) or destroyed + // (/logout); its string table may not survive. Fail closed until the + // next LoadScriptFunctions re-capture — a cleared slot means strings + // error on index exactly as stock, and nothing can read a freed table. + g_gameSlot.G = nullptr; + g_gameSlot.tv.value = nullptr; +} + } // namespace LuaSyntax::StringMethods diff --git a/src/luasyntax/StringMethods.h b/src/luasyntax/StringMethods.h new file mode 100644 index 00000000..4f0e55cf --- /dev/null +++ b/src/luasyntax/StringMethods.h @@ -0,0 +1,25 @@ +// 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 . + +#pragma once + +namespace LuaSyntax::StringMethods { + +// Forget the in-game state's captured `string` table. Called from +// FrameScript_Initialize_h BEFORE the engine resets (/reload) or destroys +// (/logout) the world Lua state, so the capture can never outlive the table +// it points at. Fails closed: until the ModuleAutoRegister re-capture at the +// next LoadScriptFunctions, strings error on index exactly as stock 1.12. +void PrepareForReload(); + +} // namespace LuaSyntax::StringMethods