Repository navigation
Description
Summary
Lua 5.3 §6.1: error(message) raises an arbitrary Lua value, and pcall returns that value as-is as its second result. This implementation stringifies non-string error objects, and omits the position prefix that reference Lua adds to string errors.
Found while testing #331 (PR #333) — split out as a separate issue since it's about the error value, not state preservation.
Reproduction
{r, _} = Lua.eval!(Lua.new(), "local ok, err = pcall(function() error({code = 1}) end) return ok, type(err), err")
# actual: [false, "string", "table: 0x0000000000000C"]
# expected: [false, "table", <the table; err.code == 1>]
{r, _} = Lua.eval!(Lua.new(), "local ok, err = pcall(function() error(42) end) return ok, type(err), err")
# actual: [false, "string", "42"]
# expected: [false, "number", 42]
{r, _} = Lua.eval!(Lua.new(), "local ok, err = pcall(function() error(\"boom\") end) return ok, type(err), err")
# actual: [false, "string", "boom"]
# expected: [false, "string", "file:1: boom"] -- position prefixReference Lua:
$ lua -e 'local ok, err = pcall(function() error({code = 1}) end); print(ok, type(err), err.code)'
false table 1
$ lua -e 'local ok, err = pcall(function() error(42) end); print(ok, type(err), err)'
false number 42
$ lua -e 'local ok, err = pcall(function() error("boom") end); print(ok, type(err), err)'
false string (command line):1: boom
Why this matters
Structured error objects (error({code = ..., msg = ...})) are the standard Lua idiom for machine-readable errors; callers pattern-match on err.code after pcall. Stringification silently breaks that. The missing position prefix also matters: per §6.1, error adds source:line: to string messages (unless level is 0), and lots of code/tests match on that prefix.
Pointers
Lua.VM.RuntimeErroralready carries the raw error value in its:valuefield —lua_error(lib/lua/vm/stdlib.ex) raises it verbatim.- The stringification happens in
extract_error_message/1inlib/lua/vm/stdlib.ex(pcall/xpcall rescue path), which callsValue.to_string/1on non-binary values. error'slevelargument (position prefix control, §6.1) appears unimplemented; the prefix behavior belongs tolua_error, not to pcall.- Since pcall discards state mutations made before error() (diverges from reference Lua) #331 (PR fix(vm): keep heap effects across protected-call error unwinding #333), the raise-time state survives into pcall's rescue, so returning a
treferror object is now safe — the referenced table's heap entry is preserved. - The
xpcallmessage handler should receive the same untouched value.
Note Lua.VM.RuntimeError's host-facing message formatting ("(error object is a TYPE value)" for non-string/number values) matches PUC-Lua and should stay — this issue is only about the value pcall/xpcall hand back to Lua code.
Activity