From 70147fbefc3f54e6a012eb44c3b37f207778e1b1 Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Sun, 23 Aug 2026 00:08:15 -0700 Subject: [PATCH 1/3] fix(vm): raise a clear error when encoding a decoded table Signed-off-by: Sai Asish Y --- lib/lua/vm/value.ex | 60 ++++++++++++++++++++++++++------------ test/lua/vm/value_test.exs | 10 +++++++ 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/lib/lua/vm/value.ex b/lib/lua/vm/value.ex index cd55225d..c6726164 100644 --- a/lib/lua/vm/value.ex +++ b/lib/lua/vm/value.ex @@ -277,25 +277,39 @@ defmodule Lua.VM.Value do end def encode(list, state, fun_wrapper) when is_list(list) do - if keyword_list?(list) do - {data, state} = - Enum.reduce(list, {%{}, state}, fn {k, v}, {data, state} -> - key = Atom.to_string(k) - {encoded_v, state} = encode(v, state, fun_wrapper) - {Map.put(data, key, encoded_v), state} - end) - - State.alloc_table(state, data) - else - {data, state} = - list - |> Enum.with_index(1) - |> Enum.reduce({%{}, state}, fn {v, idx}, {data, state} -> - {encoded_v, state} = encode(v, state, fun_wrapper) - {Map.put(data, idx, encoded_v), state} - end) - - State.alloc_table(state, data) + cond do + keyword_list?(list) -> + {data, state} = + Enum.reduce(list, {%{}, state}, fn {k, v}, {data, state} -> + key = Atom.to_string(k) + {encoded_v, state} = encode(v, state, fun_wrapper) + {Map.put(data, key, encoded_v), state} + end) + + State.alloc_table(state, data) + + # `decode/2` returns a table as a list of `{key, value}` pairs with + # binary/number keys. That shape has no positional encoding (each pair + # is a bare 2-tuple matching no `encode/3` clause), so it would crash + # with a raw `FunctionClauseError`. Refuse it with an actionable message + # instead, mirroring the struct and cyclic-tref clauses above. + decoded_pair_list?(list) -> + raise Lua.RuntimeException, + "cannot encode a list of {key, value} pairs into a Lua value. This is the " <> + "shape `decode/2` produces for a table, and it does not round-trip back " <> + "into the VM. Convert it to a map first (e.g. `Map.new/1`), which encodes " <> + "as a Lua table." + + true -> + {data, state} = + list + |> Enum.with_index(1) + |> Enum.reduce({%{}, state}, fn {v, idx}, {data, state} -> + {encoded_v, state} = encode(v, state, fun_wrapper) + {Map.put(data, idx, encoded_v), state} + end) + + State.alloc_table(state, data) end end @@ -328,6 +342,14 @@ defmodule Lua.VM.Value do defp keyword_list?([]), do: true defp keyword_list?(_), do: false + # A non-empty list where every element is a `{key, value}` pair with a binary + # or number key: exactly what `decode/2` returns for a Lua table. The empty + # list is already handled by `keyword_list?/1`, so this never matches `[]`. + defp decoded_pair_list?([{k, _v} | rest]) when is_binary(k) or is_number(k), do: decoded_pair_list?(rest) + + defp decoded_pair_list?([]), do: true + defp decoded_pair_list?(_), do: false + # --- Decoding (Lua VM → Elixir) --- @doc """ diff --git a/test/lua/vm/value_test.exs b/test/lua/vm/value_test.exs index 35da1881..0d163dcb 100644 --- a/test/lua/vm/value_test.exs +++ b/test/lua/vm/value_test.exs @@ -130,6 +130,16 @@ defmodule Lua.VM.ValueTest do inner_table = Map.fetch!(state.tables, inner_id) assert inner_table.data["x"] == 1 end + + test "refuses a decoded table (list of {key, value} pairs) with a clear error" do + assert_raise Lua.RuntimeException, ~r/list of \{key, value\} pairs/, fn -> + Value.encode([{"x", 1}, {"y", 2}], new_state()) + end + + assert_raise Lua.RuntimeException, ~r/list of \{key, value\} pairs/, fn -> + Value.encode([{1, "a"}, {2, "b"}], new_state()) + end + end end describe "encode_list/2" do From 508cd5c191b4040c25d48c329b7598d4d5c094f4 Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Fri, 25 Sep 2026 09:53:24 -0700 Subject: [PATCH 2/3] use a heredoc for the decoded pair list error Signed-off-by: Sai Asish Y --- lib/lua/vm/value.ex | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/lua/vm/value.ex b/lib/lua/vm/value.ex index c6726164..3f3227de 100644 --- a/lib/lua/vm/value.ex +++ b/lib/lua/vm/value.ex @@ -294,11 +294,12 @@ defmodule Lua.VM.Value do # with a raw `FunctionClauseError`. Refuse it with an actionable message # instead, mirroring the struct and cyclic-tref clauses above. decoded_pair_list?(list) -> - raise Lua.RuntimeException, - "cannot encode a list of {key, value} pairs into a Lua value. This is the " <> - "shape `decode/2` produces for a table, and it does not round-trip back " <> - "into the VM. Convert it to a map first (e.g. `Map.new/1`), which encodes " <> - "as a Lua table." + raise Lua.RuntimeException, """ + cannot encode a list of {key, value} pairs into a Lua value. This is the \ + shape `decode/2` produces for a table, and it does not round-trip back \ + into the VM. Convert it to a map first (e.g. `Map.new/1`), which encodes \ + as a Lua table.\ + """ true -> {data, state} = From 80b9c36e46443cf746da808164a53ac3813f2849 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Wed, 25 Nov 2026 16:14:42 -0500 Subject: [PATCH 3/3] fix(value): make the decoded-table error's advice work for nested tables The message now names Lua.eval!/2 and Lua.decode!/2 instead of the internal decode/2, says Map.new/1 must be applied at every level, and points at decode: false, whose table reference round-trips into set!. The predicate comment no longer claims boolean and table keys match. Tests cover set! with a nested decoded table and the decode: false round trip. --- lib/lua/vm/value.ex | 11 +++++++---- test/lua/vm/value_test.exs | 23 +++++++++++++++++++++++ test/lua_test.exs | 20 ++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/lua/vm/value.ex b/lib/lua/vm/value.ex index 3f3227de..3aa3de8e 100644 --- a/lib/lua/vm/value.ex +++ b/lib/lua/vm/value.ex @@ -296,9 +296,11 @@ defmodule Lua.VM.Value do decoded_pair_list?(list) -> raise Lua.RuntimeException, """ cannot encode a list of {key, value} pairs into a Lua value. This is the \ - shape `decode/2` produces for a table, and it does not round-trip back \ - into the VM. Convert it to a map first (e.g. `Map.new/1`), which encodes \ - as a Lua table.\ + shape `Lua.eval!/2` and `Lua.decode!/2` produce for a table, and it does \ + not round-trip back into the VM. Build a map instead (nested tables need \ + to be maps too, so `Map.new/1` on the top level alone is not enough), or \ + evaluate with `decode: false` to keep the table reference, which can be \ + passed back to `Lua.set!/3` unchanged.\ """ true -> @@ -344,7 +346,8 @@ defmodule Lua.VM.Value do defp keyword_list?(_), do: false # A non-empty list where every element is a `{key, value}` pair with a binary - # or number key: exactly what `decode/2` returns for a Lua table. The empty + # or number key: the common shape `decode/2` returns for a Lua table (boolean + # and table keys decode to other shapes and are not matched). The empty # list is already handled by `keyword_list?/1`, so this never matches `[]`. defp decoded_pair_list?([{k, _v} | rest]) when is_binary(k) or is_number(k), do: decoded_pair_list?(rest) diff --git a/test/lua/vm/value_test.exs b/test/lua/vm/value_test.exs index 0d163dcb..a84bdc5f 100644 --- a/test/lua/vm/value_test.exs +++ b/test/lua/vm/value_test.exs @@ -140,6 +140,29 @@ defmodule Lua.VM.ValueTest do Value.encode([{1, "a"}, {2, "b"}], new_state()) end end + + test "the decoded-table error names public functions and the nested-table caveat" do + error = + assert_raise Lua.RuntimeException, fn -> + Value.encode([{"x", 1}], new_state()) + end + + message = Exception.message(error) + assert message =~ "Lua.eval!/2" + assert message =~ "decode: false" + assert message =~ "nested" + refute message =~ "`decode/2`" + end + + test "refuses a decoded table nested inside a map or a list" do + assert_raise Lua.RuntimeException, ~r/list of \{key, value\} pairs/, fn -> + Value.encode(%{t: [{"b", 1}]}, new_state()) + end + + assert_raise Lua.RuntimeException, ~r/list of \{key, value\} pairs/, fn -> + Value.encode([[{"b", 1}]], new_state()) + end + end end describe "encode_list/2" do diff --git a/test/lua_test.exs b/test/lua_test.exs index 85abca68..18756a5c 100644 --- a/test/lua_test.exs +++ b/test/lua_test.exs @@ -25,6 +25,26 @@ defmodule LuaTest do end end + describe "set! with a decoded table" do + test "refuses a nested decoded table with a clear error" do + {[decoded], _} = Lua.eval!(Lua.new(), "return {a = {b = 1}}") + + assert_raise Lua.RuntimeException, ~r/list of \{key, value\} pairs/, fn -> + Lua.set!(Lua.new(), [:t], decoded) + end + + assert_raise Lua.RuntimeException, ~r/list of \{key, value\} pairs/, fn -> + Lua.set!(Lua.new(), [:t], Map.new(decoded)) + end + end + + test "a table evaluated with decode: false round-trips into set!" do + {[ref], lua} = Lua.eval!(Lua.new(), "return {a = {b = 1}}", decode: false) + + assert {[1], _} = lua |> Lua.set!([:t], ref) |> Lua.eval!("return t.a.b") + end + end + describe "inspect" do test "shows nothing" do lua = Lua.new()