diff --git a/lib/orb.ex b/lib/orb.ex index 7ace8d6..f80e280 100644 --- a/lib/orb.ex +++ b/lib/orb.ex @@ -475,6 +475,9 @@ defmodule Orb do def __wasm_body__(_), do: [] defoverridable __wasm_body__: 1 + def __wasm_data_defs__(_), do: [] + defoverridable __wasm_data_defs__: 1 + # TODO: rename these to orb_ prefix instead of wasm_ ? Module.put_attribute( __MODULE__, @@ -614,7 +617,7 @@ defmodule Orb do end def __wasm_module__() do - %{body: body, constants: constants, global_definitions: global_definitions} = + %{body: body, constants: constants, global_defs: global_defs, data_defs: data_defs} = Orb.Compiler.run(__MODULE__, @wasm_globals) memory = Memory.new(@wasm_memory, constants) @@ -624,11 +627,11 @@ defmodule Orb do types: @wasm_types |> Enum.reverse() |> List.flatten(), table_size: @wasm_table_allocations |> List.flatten() |> length(), imports: @wasm_imports |> Enum.reverse() |> List.flatten(), - globals: global_definitions, + globals: global_defs, memory: memory, constants: constants, body: body, - data: @wasm_section_data |> Enum.reverse() + data: Enum.reverse(@wasm_section_data) ++ data_defs ) end diff --git a/lib/orb/compiler.ex b/lib/orb/compiler.ex index f816e08..d75cd90 100644 --- a/lib/orb/compiler.ex +++ b/lib/orb/compiler.ex @@ -12,17 +12,24 @@ defmodule Orb.Compiler do Orb.Constants.__begin() # Each global is expanded, possibly looking up with the current compiler context begun above. - global_definitions = + global_defs = globals |> Enum.reverse() |> List.flatten() |> Enum.map(&Orb.Global.expand!/1) body = Orb.ModuleDefinition.get_body_of(mod) + data_defs = mod.__wasm_data_defs__(nil) + Process.delete({Orb, :global_types}) # We’re done. Get all the constant strings that were actually used. constants = Orb.Constants.__read() - %{body: body, constants: constants, global_definitions: global_definitions} + %{ + body: body, + constants: constants, + global_defs: global_defs, + data_defs: data_defs + } after Orb.Constants.__cleanup() end diff --git a/lib/orb/data.ex b/lib/orb/data.ex index 800664c..fb9e443 100644 --- a/lib/orb/data.ex +++ b/lib/orb/data.ex @@ -2,7 +2,7 @@ defmodule Orb.Data do @moduledoc false # TODO: remove nul_terminated - defstruct [:offset, :value, :nul_terminated] + defstruct [:offset, :value, nul_terminated: false] defimpl Orb.ToWat do def to_wat(%Orb.Data{offset: offset, value: value, nul_terminated: nul_terminated}, indent) do diff --git a/lib/orb/memory.ex b/lib/orb/memory.ex index 4918c9a..c41ae87 100644 --- a/lib/orb/memory.ex +++ b/lib/orb/memory.ex @@ -57,7 +57,7 @@ defmodule Orb.Memory do @doc "Initializes data in memory. In Wat is `(data …)`" defmacro initial_data!(offset, value) do quote bind_quoted: [offset: offset, value: value] do - @wasm_section_data %Orb.Data{offset: offset, value: value, nul_terminated: false} + @wasm_section_data %Orb.Data{offset: offset, value: value} end end