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
15 changes: 15 additions & 0 deletions lib/lua/vm/argument_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,19 @@ defmodule Lua.VM.ArgumentError do
got: got
)
end

@doc """
Builds the PUC-Lua "wrong number of arguments to 'X'" runtime error.

This is not a bad-argument error — it is the top-level message PUC-Lua's
`luaL_error(L, "wrong number of arguments to '%s'", name)` emits when a
variadic stdlib function is called with too few or too many positional
arguments. Returns a `Lua.VM.RuntimeError` so callers can `raise` it
directly:

raise ArgumentError.wrong_number_of_arguments("insert")
"""
def wrong_number_of_arguments(function_name) do
Lua.VM.RuntimeError.exception(value: "wrong number of arguments to '#{function_name}'")
end
end
4 changes: 2 additions & 2 deletions lib/lua/vm/executor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2321,7 +2321,7 @@ defmodule Lua.VM.Executor do
{:ok, nb} <- to_number(b) do
cond do
is_integer(nb) and nb == 0 ->
raise RuntimeError, value: "attempt to divide by zero", line: line, source: source
raise RuntimeError, value: "attempt to perform 'n//0'", line: line, source: source

is_integer(na) and is_integer(nb) ->
Numeric.to_signed_int64(lua_idiv(na, nb))
Expand Down Expand Up @@ -2349,7 +2349,7 @@ defmodule Lua.VM.Executor do
{:ok, nb} <- to_number(b) do
cond do
is_integer(nb) and nb == 0 ->
raise RuntimeError, value: "attempt to perform modulo by zero", line: line, source: source
raise RuntimeError, value: "attempt to perform 'n%0'", line: line, source: source

is_integer(na) and is_integer(nb) ->
Numeric.to_signed_int64(na - lua_idiv(na, nb) * nb)
Expand Down
3 changes: 2 additions & 1 deletion lib/lua/vm/stdlib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ defmodule Lua.VM.Stdlib do
|> install_library(Lua.VM.Stdlib.String)
|> install_library(Lua.VM.Stdlib.Math)
|> install_library(Lua.VM.Stdlib.Table)
|> install_library(Lua.VM.Stdlib.Utf8)
|> install_library(Lua.VM.Stdlib.Debug)
|> preload_stdlib_modules()
|> install_unpack_alias()
Expand Down Expand Up @@ -98,7 +99,7 @@ defmodule Lua.VM.Stdlib do
# table, debug), so this pass is a safety net for any future stdlib tables
# that may be added as globals before this call.
defp preload_stdlib_modules(state) do
modules = ["string", "math", "table", "debug"]
modules = ["string", "math", "table", "utf8", "debug"]

Enum.reduce(modules, state, fn name, acc ->
case State.get_global(acc, name) do
Expand Down
2 changes: 1 addition & 1 deletion lib/lua/vm/stdlib/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule Lua.VM.Stdlib.Table do
end

defp table_insert(args, _state) when length(args) > 3 do
raise Lua.VM.RuntimeError, value: "wrong number of arguments to 'insert'"
raise ArgumentError.wrong_number_of_arguments("insert")
end

defp table_insert([tref | _], _state) do
Expand Down
Loading
Loading