Skip to content

pcall returns stringified error objects instead of passing the error value through #334

Closed

Description

@davydog187

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 prefix

Reference 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.RuntimeError already carries the raw error value in its :value field — lua_error (lib/lua/vm/stdlib.ex) raises it verbatim.
  • The stringification happens in extract_error_message/1 in lib/lua/vm/stdlib.ex (pcall/xpcall rescue path), which calls Value.to_string/1 on non-binary values.
  • error's level argument (position prefix control, §6.1) appears unimplemented; the prefix behavior belongs to lua_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 tref error object is now safe — the referenced table's heap entry is preserved.
  • The xpcall message 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions