Current section

Files

Jump to
orb lib orb constants.ex
Raw

lib/orb/constants.ex

defmodule Orb.Constants do
@moduledoc false
# TODO: decide on non-arbitrary offset, and document it.
defstruct offset: 0xFF, items: [], lookup_table: []
def from_attribute(items, offset \\ 0xFF) do
items =
items
# Module attributes accumulate by prepending
|> Enum.reverse()
|> List.flatten()
|> Enum.uniq()
{lookup_table, _} =
items
|> Enum.map_reduce(offset, fn string, offset ->
{{string, offset}, offset + byte_size(string) + 1}
end)
%__MODULE__{offset: offset, items: items, lookup_table: lookup_table}
end
def lookup(constants, value) do
{_, offset} = List.keyfind!(constants.lookup_table, value, 0)
{:i32_const_string, offset, value}
end
defimpl Orb.ToWat do
def to_wat(%Orb.Constants{lookup_table: lookup_table}, indent) do
for {string, offset} <- lookup_table do
[
indent,
"(data (i32.const ",
to_string(offset),
") ",
?",
string |> String.replace(~S["], ~S[\"]) |> String.replace("\n", ~S"\n"),
?",
")\n"
]
end
end
end
end