Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 65 additions & 9 deletions .agents/plans/A19-error-line-info-native-funcs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
id: A19
title: Stdlib bad-argument raises read source position from process dict
issue: null
pr: null
pr: 215
branch: fix/error-line-info-stdlib
base: main
status: ready
status: review
direction: A
unlocks:
- line numbers on `string.upper(nil)`, `table.insert(t, nil, 5)`, etc.
Expand All @@ -28,14 +28,19 @@ and `coroutine.*`.

## Success criteria

- [ ] `mix test` still passes.
- [ ] `mix test --only lua53` passes 5/29 (no regression).
- [ ] New tests: representative bad-arg raises from each stdlib file
- [x] `mix test` still passes (1577 → 1585, +8 new tests).
- [x] `mix test --only lua53` passes 5/29 (no regression).
- [x] New tests: representative bad-arg raises from each stdlib file
have `:line` and `:source` populated.
- [ ] `string.upper(nil)` → TypeError with line/source.
- [ ] `table.insert(t, nil)` (bad pos) → has line/source.
- [ ] `math.floor("x")` → has line/source.
- [ ] No measurable perf regression on the benchee harness vs A18.
- [x] `string.upper(nil)` → TypeError with line/source.
- [x] `table.insert(t, nil)` (bad pos) → has line/source.
- [x] `math.floor("x")` → has line/source.
- [x] No measurable perf regression on the benchee harness vs A18.
fib(28) and a 1000-iteration string-ops workload measured on
both `main` and this branch with fresh builds: medians within
~1% (within noise floor). Exception modules are off the hot
path; `Process.get` only fires when an exception is actually
being constructed.

## Implementation notes

Expand Down Expand Up @@ -83,3 +88,54 @@ mix test --only lua53
`Lua.VM.Stdlib.some_helper/2` directly from Elixir), `current_position/0`
returns `{nil, nil}` and the message just omits the location. That's
the correct behavior — better than crashing.

## Discoveries

The plan anticipated touching ~50 stdlib raise sites individually with
a `bad_arg!` helper. During implementation, a much cleaner approach
emerged: have the **exception modules themselves** auto-populate
`:line`/`:source` from `Executor.current_position/0` inside
`exception/1` when not given.

This means **zero callsite changes** for the ~80 `ArgumentError`
raises across `math`, `string`, `table`, etc. — they all picked up
line/source for free. Same for the bare `raise RuntimeError, value:
"..."` sites.

Explicit opts still win: every executor raise that already passes
`:line`/`:source` (e.g. divide-by-zero at executor.ex:1864) is
unaffected because of `Keyword.get(opts, :line) || auto_line`.

Two pre-existing oddities surfaced during smoke testing, neither
introduced by this change:

- `table.insert(t, nil, 5)` reports the wrong arg number in its "bad
argument" message (says `#1`, should be `#2` — the `nil` is at
position 2). Logged as a candidate for an A24 sub-plan.
- `setmetatable(5, {})` on line 1 of a script reports `line 0`. Same
`source_line` off-by-one A18 already flagged in its Discoveries.

## What changed

PR: [#215](https://github.com/tv-labs/lua/pull/215)

Files touched:

- `lib/lua/vm/argument_error.ex` — added `:line` / `:source` /
`:call_stack` fields, auto-populate from `Executor.current_position/0`,
render through `Lua.VM.ErrorFormatter` for consistency with the
other exception types.
- `lib/lua/vm/runtime_error.ex` — auto-populate when `:line`/`:source`
not given.
- `lib/lua/vm/type_error.ex` — auto-populate.
- `lib/lua/vm/assertion_error.ex` — auto-populate.
- `test/lua/error_messages_test.exs` — 8 new tests under
"stdlib bad-argument raises carry line and source", covering
`string.upper(nil)`, `math.floor("x")`, `table.insert` bad pos,
`select` non-numeric index, `setmetatable` non-table, the defensive
outside-Lua case, the explicit-override case, and a stdlib
`RuntimeError` (`select(0, ...)`).

Test count: 1577 → 1585 (+8). 0 failures.
Suite count: 5/29, unchanged.
Bench: no measurable regression vs main.
47 changes: 34 additions & 13 deletions lib/lua/vm/argument_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ defmodule Lua.VM.ArgumentError do
- `:expected` - What type or value was expected (e.g., "number", "string")
- `:got` - What was actually received (optional, e.g., "nil", "boolean")
- `:details` - Additional details about the error (optional)
- `:line` - Source line where the call originated (auto-populated from
`Lua.VM.Executor.current_position/0` when not given explicitly)
- `:source` - Source name where the call originated (auto-populated)
- `:call_stack` - Call stack frames at the raise site (default `[]`)

## Examples

# Basic type error
# Basic type error — line/source filled in automatically when raised
# from inside a Lua execution.
raise ArgumentError,
function_name: "string.rep",
arg_num: 2,
Expand All @@ -36,31 +41,47 @@ defmodule Lua.VM.ArgumentError do
details: "value out of range"
"""

defexception [:function_name, :arg_num, :expected, :got, :details]
defexception [
:function_name,
:arg_num,
:expected,
:got,
:details,
:line,
:source,
:call_stack
]

@impl true
def exception(opts) do
{auto_line, auto_source} = Lua.VM.Executor.current_position()

function_name = Keyword.fetch!(opts, :function_name)
arg_num = Keyword.get(opts, :arg_num)
expected = Keyword.get(opts, :expected)
got = Keyword.get(opts, :got)
details = Keyword.get(opts, :details)

%__MODULE__{
function_name: function_name,
arg_num: arg_num,
expected: expected,
got: got,
details: details
arg_num: Keyword.get(opts, :arg_num),
expected: Keyword.get(opts, :expected),
got: Keyword.get(opts, :got),
details: Keyword.get(opts, :details),
line: Keyword.get(opts, :line) || auto_line,
source: Keyword.get(opts, :source) || auto_source,
call_stack: Keyword.get(opts, :call_stack, [])
}
end

@impl true
def message(%__MODULE__{function_name: function_name, arg_num: arg_num, expected: expected, got: got, details: details}) do
build_message(function_name, arg_num, expected, got, details)
def message(%__MODULE__{} = e) do
base = build_base(e.function_name, e.arg_num, e.expected, e.got, e.details)

Lua.VM.ErrorFormatter.format(:type_error, base,
source: e.source,
line: e.line,
call_stack: e.call_stack
)
end

defp build_message(function_name, arg_num, expected, got, details) do
defp build_base(function_name, arg_num, expected, got, details) do
base =
if arg_num do
"bad argument ##{arg_num} to '#{function_name}'"
Expand Down
10 changes: 8 additions & 2 deletions lib/lua/vm/assertion_error.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
defmodule Lua.VM.AssertionError do
@moduledoc """
Raised by the Lua `assert()` function when the condition is falsy.

When raised without explicit `:line` / `:source` opts, `exception/1`
populates them from the calling Lua source position via
`Lua.VM.Executor.current_position/0`.
"""

defexception [:value, :source, :message, :call_stack, :line]

@impl true
def exception(opts) do
{auto_line, auto_source} = Lua.VM.Executor.current_position()

value = Keyword.get(opts, :value)
source = Keyword.get(opts, :source)
source = Keyword.get(opts, :source) || auto_source
call_stack = Keyword.get(opts, :call_stack, [])
line = Keyword.get(opts, :line)
line = Keyword.get(opts, :line) || auto_line
message = Keyword.get(opts, :message) || format_message(value, source, line, call_stack)

%__MODULE__{
Expand Down
13 changes: 11 additions & 2 deletions lib/lua/vm/runtime_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ defmodule Lua.VM.RuntimeError do

Carries the original Lua error value, which may be any Lua type (string,
number, table reference, etc.).

When raised without explicit `:line` / `:source` opts (e.g. from a stdlib
bad-argument check), `exception/1` populates them from the calling Lua
source position via `Lua.VM.Executor.current_position/0`. That position
is stashed in the process dictionary at every native-call boundary, so
any raise site reachable from a Lua execution inherits the correct
attribution automatically.
"""

defexception [:value, :source, :message, :call_stack, :line]

@impl true
def exception(opts) do
{auto_line, auto_source} = Lua.VM.Executor.current_position()

value = Keyword.get(opts, :value)
source = Keyword.get(opts, :source)
source = Keyword.get(opts, :source) || auto_source
call_stack = Keyword.get(opts, :call_stack, [])
line = Keyword.get(opts, :line)
line = Keyword.get(opts, :line) || auto_line
message = Keyword.get(opts, :message) || format_message(value, source, line, call_stack)

%__MODULE__{
Expand Down
13 changes: 11 additions & 2 deletions lib/lua/vm/type_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ defmodule Lua.VM.TypeError do
Raised when a Lua operation is applied to a value of the wrong type.

Examples: calling a nil value, calling a number, indexing a boolean.

When raised without explicit `:line` / `:source` opts (e.g. from a stdlib
type check), `exception/1` populates them from the calling Lua source
position via `Lua.VM.Executor.current_position/0`. That position is
stashed in the process dictionary at every native-call boundary, so
any raise site reachable from a Lua execution inherits the correct
attribution automatically.
"""

defexception [:value, :source, :message, :call_stack, :line, :error_kind, :value_type]

@impl true
def exception(opts) do
{auto_line, auto_source} = Lua.VM.Executor.current_position()

value = Keyword.get(opts, :value)
source = Keyword.get(opts, :source)
source = Keyword.get(opts, :source) || auto_source
call_stack = Keyword.get(opts, :call_stack, [])
line = Keyword.get(opts, :line)
line = Keyword.get(opts, :line) || auto_line
error_kind = Keyword.get(opts, :error_kind)
value_type = Keyword.get(opts, :value_type)

Expand Down
Loading
Loading