Skip to content

Commit

Permalink
Add __wasm_data_defs__/1 to allow dynamic data to be initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Aug 29, 2024
1 parent 98db425 commit 01b4b81
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
9 changes: 6 additions & 3 deletions lib/orb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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__,
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
11 changes: 9 additions & 2 deletions lib/orb/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/orb/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/orb/memory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 01b4b81

Please sign in to comment.