mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 16:10:36 +00:00
Ports Lua 5.2's `xpcall(f, msgh, ...)` shape. This client's xpcall takes only (f, msgh), and its implementation is explicit about the rest: `lua_settop(L, 2)` discards any further arguments, then `lua_pcall(L, 0, ...)` calls f with none. So the argument-forwarding form did not fail loudly -- it called f with nothing, and a function that read its parameters saw nil. (Not a 5.0-vs-5.1 difference: both take (f, msgh). Forwarding arrived in 5.2.) Replacing the base-library function rather than wrapping it in Lua, for the same two reasons `select` and `unpack` are C in this directory. A Lua wrapper would allocate a closure AND an `arg` table per call, putting an allocation on every error-guarded path including per-frame ones; and HookSecureFunc's kUnhookableNames lists xpcall precisely because shadowing a core primitive with a Lua closure diverges from what C-side callers see. lua_pcall wants the callee and its arguments contiguous on top with the handler at a fixed index, but the incoming order puts the handler between them, so: PushValue(2) -> f, msgh, a1..aN, msgh Insert(1) -> msgh, f, msgh, a1..aN Remove(3) -> msgh, f, a1..aN then PCall(top - 2, MULTRET, errfunc = 1), push the status boolean, Insert(2), and return GetTop - 1 so the handler drops out of the result set. 5.2 does the same reshuffle with one lua_rotate, which this Lua has no equivalent of. Purely additive: a two-argument call forwards zero arguments and is the old behavior exactly, so the capturing-closure idiom in ExecuteFrameScript and the plain two-argument call in ItemUtil both keep working untouched. Verified with 25 assertions across both halves. Backward: exact return counts, embedded and trailing nil returns keeping their slots, reference identity, the handler running once and receiving the message, a non-function handler tolerated on the success path, nil f caught, an erroring handler still yielding false, and the load-bearing one -- a two-argument call still passing f zero arguments. Forward: false and embedded/trailing nil arguments surviving, twelve arguments intact, forwarding composing with multiple returns, arguments still arriving on the error path, and the two cases that target the shuffle -- the handler not leaking in as argument 1, and the handler also being passable as an argument. Two incidental confirmations: C-side lua_error does longjmp (the missing-handler case is caught by an enclosing pcall), which the notes had flagged as uncertain; and Lua-level error() still does not unwind, so f runs past it and xpcall reports success with the handler never consulted. Closes #45