Skip to content

Commit

Permalink
Merge pull request #1284 from bettio/fix-elixir-inspect
Browse files Browse the repository at this point in the history
Fix Elixir `inspect()`

Closer to Elixir one (and more reliable), but not yet perfect.
e.g. structs are still printed as maps.

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
  • Loading branch information
bettio committed Sep 25, 2024
2 parents 71988d3 + 36cb1b8 commit 67a8ae1
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ instead
- Fix memory corruption in `unicode:characters_to_binary`
- Fix handling of large literal indexes
- `unicode:characters_to_list`: fixed bogus out_of_memory error on some platforms such as ESP32
- Fix crash in Elixir library when doing `inspect(:atom)`
- General inspect() compliance with Elixir behavior (but there are still some minor differences)

## [0.6.4] - 2024-08-18

Expand Down
77 changes: 67 additions & 10 deletions libs/exavmlib/lib/Kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ defmodule Kernel do
def inspect(term, opts \\ []) when is_list(opts) do
case term do
t when is_atom(t) ->
[?:, atom_to_string(t)]
atom_to_string(t, ":")

t when is_integer(t) ->
:erlang.integer_to_binary(t)

t when is_list(t) ->
# TODO: escape unprintable lists
:erlang.list_to_binary(t)
if is_printable_list(t) do
str = :erlang.list_to_binary(t)
<<?'::utf8, str::binary, ?'::utf8>>
else
[?[ | t |> inspect_join(?])]
|> :erlang.list_to_binary()
end

t when is_pid(t) ->
:erlang.pid_to_list(t)
Expand All @@ -64,15 +69,19 @@ defmodule Kernel do
|> :erlang.list_to_binary()

t when is_binary(t) ->
# TODO: escape unprintable binaries
t
if is_printable_binary(t) do
<<?"::utf8, t::binary, ?"::utf8>>
else
["<<" | t |> :erlang.binary_to_list() |> inspect_join(">>")]
|> :erlang.list_to_binary()
end

t when is_reference(t) ->
:erlang.ref_to_list(t)
|> :erlang.list_to_binary()

t when is_float(t) ->
:erlang.float_to_binary(t)
:erlang.float_to_binary(term, [{:decimals, 17}, :compact])

t when is_map(t) ->
[?%, ?{ | t |> inspect_kv() |> join(?})]
Expand All @@ -88,6 +97,10 @@ defmodule Kernel do
[inspect(e), last]
end

defp inspect_join([h | e], last) when not is_list(e) do
[inspect(h), " | ", inspect(e), last]
end

defp inspect_join([h | t], last) do
[inspect(h), ?,, ?\s | inspect_join(t, last)]
end
Expand Down Expand Up @@ -118,12 +131,56 @@ defmodule Kernel do
)
end

defp atom_to_string(atom) do
# TODO: use unicode rather than plain latin1
# handle spaces and special characters
:erlang.atom_to_binary(atom, :latin1)
defp atom_to_string(atom, prefix \\ "") do
case atom do
true ->
"true"

false ->
"false"

nil ->
"nil"

any_atom ->
case :erlang.atom_to_binary(any_atom) do
<<"Elixir.", displayable::binary>> ->
displayable

other ->
<<prefix::binary, other::binary>>
end
end
end

defp is_printable_list([]), do: false

defp is_printable_list([char]) do
is_printable_ascii(char)
end

defp is_printable_list([char | t]) do
if is_printable_ascii(char) do
is_printable_list(t)
else
false
end
end

defp is_printable_list(_any), do: false

defp is_printable_ascii(char) do
is_integer(char) and char >= 32 and char < 127 and char != ?'
end

defp is_printable_binary(<<>>), do: true

defp is_printable_binary(<<char::utf8, rest::binary>>) when char >= 32 do
is_printable_binary(rest)
end

defp is_printable_binary(_any), do: false

@doc """
Returns the biggest of the two given terms according to
Erlang's term ordering.
Expand Down
61 changes: 61 additions & 0 deletions tests/libs/exavmlib/Tests.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
#

defmodule Tests do
# defstruct [
# :field1,
# field2: 42
# ]

@compile {:no_warn_undefined, :undef}

def start() do
:ok = IO.puts("Running Elixir tests")
:ok = test_enum()
:ok = test_exception()
:ok = test_chars_protocol()
:ok = test_inspect()
:ok = IO.puts("Finished Elixir tests")
end

Expand Down Expand Up @@ -234,7 +240,62 @@ defmodule Tests do
:ok
end

def test_inspect() do
"true" = inspect(true)
"false" = inspect(false)
"nil" = inspect(nil)

":test" = inspect(:test)
":アトム" = inspect(:アトム)
"Test" = inspect(Test)

"5" = inspect(5)
"5.0" = inspect(5.0)

~s[""] = inspect("")
~s["hello"] = inspect("hello")
~s["アトム"] = inspect("アトム")

"<<10>>" = inspect("\n")
"<<0, 1, 2, 3>>" = inspect(<<0, 1, 2, 3>>)
"<<195, 168, 0>>" = inspect(<<195, 168, 0>>)

"[]" = inspect([])
"[0]" = inspect([0])
"[9, 10]" = inspect([9, 10])
~s'["test"]' = inspect(["test"])
"'hello'" = inspect('hello')
"[127]" = inspect([127])
"[104, 101, 108, 108, 248]" = inspect('hellø')

~s([5 | "hello"]) = inspect([5 | "hello"])

"{}" = inspect({})
"{1, 2}" = inspect({1, 2})
"{:test, 1}" = inspect({:test, 1})

"%{}" = inspect(%{})
either("%{a: 1, b: 2}", "%{b: 2, a: 1}", inspect(%{a: 1, b: 2}))
either(~s[%{"a" => 1, "b" => 2}], ~s[%{"b" => 2, "a" => 1}], inspect(%{"a" => 1, "b" => 2}))

# TODO: structs are not yet supported
# either(
# ~s[%#{__MODULE__}{field1: nil, field2: 42}],
# ~s[%#{__MODULE__}{field2: 42, field1: nil}],
# inspect(%__MODULE__{})
# )

:ok
end

defp fact(n) when n < 0, do: :test
defp fact(0), do: 1
defp fact(n), do: fact(n - 1) * n

def either(a, b, value) do
case value do
^a -> a
^b -> b
end
end
end

0 comments on commit 67a8ae1

Please sign in to comment.