Skip to content
Open
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
64 changes: 45 additions & 19 deletions lib/lua/vm/value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,42 @@ 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 `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 ->
{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

Expand Down Expand Up @@ -328,6 +345,15 @@ 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: 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)

defp decoded_pair_list?([]), do: true
defp decoded_pair_list?(_), do: false

# --- Decoding (Lua VM → Elixir) ---

@doc """
Expand Down
33 changes: 33 additions & 0 deletions test/lua/vm/value_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,39 @@ 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

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
Expand Down
20 changes: 20 additions & 0 deletions test/lua_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down