Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Adds importable memory. #42

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions lib/orb/import.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ defmodule Orb.Import do
end
end

defmacro register_memory(global, namespace, min \\ nil) do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why register_memory/3 instead of memory/3?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just seemed like the thing to do, I was frankly pretty tired when I wrote this so perhaps memory/3 would be more appropriate.

quote do
@wasm_imports [%Orb.Import{module: unquote(global), name: unquote(namespace), type: %Orb.Memory{is_imported?: true, min: unquote(min)}}]
end
end

defimpl Orb.ToWat do
def to_wat(%Orb.Import{module: nil, name: name, type: type}, indent) do
[indent, ~S|(import "|, to_string(name), ~S|" |, Orb.ToWat.to_wat(type, ""), ?)]
Expand Down
17 changes: 15 additions & 2 deletions lib/orb/memory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Orb.Memory do
Work with memory: load, store, declare pages & initial data.
"""

defstruct name: "", min: 0, exported_name: "memory"
defstruct name: "", min: 0, exported_name: "memory", is_imported?: false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There‘s a bug from me where we aren’t using the exported_name field in the wat output. So I should fix that.

I think generally it makes sense if you can export xor import, not both. So I’ll have a think about how best to represent that…

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested resolution? Or needs some thinking?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry work has been super busy, I’ll resolve this soon!


@doc false
def new(page_definitions, constants)
Expand Down Expand Up @@ -186,7 +186,7 @@ defmodule Orb.Memory do
end

defimpl Orb.ToWat do
def to_wat(%Orb.Memory{min: min}, indent) do
def to_wat(%Orb.Memory{min: min, is_imported?: is_imported?}, indent) when not is_imported? do
[
indent,
~S{(memory (export "memory")},
Expand All @@ -198,6 +198,19 @@ defmodule Orb.Memory do
"\n"
]
end

def to_wat(%Orb.Memory{is_imported?: true, name: name, min: min}, indent) do
[
indent,
"(memory",
to_string(name),
case min do
nil -> []
min when min >= 0 -> [" ", to_string(min)]
end,
")",
]
end
end

defimpl Orb.ToWasm do
Expand Down
15 changes: 15 additions & 0 deletions test/import_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@ defmodule ImportTest do
)
""" = Orb.to_wat(ImportsExample)
end

defmodule ImportsGlobal do
use Orb

Orb.Import.register_memory(:env, :memory, 1)
end

test "importing memory should work" do

assert """
(module $ImportsGlobal
(import "env" "memory" (memory 1))
)
""" = Orb.to_wat(ImportsGlobal)
end
end