Skip to content

Commit

Permalink
Replace Orb.importw/1 with Orb.Import.register/1
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Jun 23, 2024
1 parent e07b966 commit 91f951e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
18 changes: 2 additions & 16 deletions lib/orb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1020,23 +1020,9 @@ defmodule Orb do
end

@doc """
Declare functions to be imported from the host environment.
The `mod` is a module using `Orb.Import`. The `namespace` is the local name the import is registered under.
For each function from `mod` an `(import)` instruction is defined in the WebAssembly:
```wasm
(module $example
(import "namespace" "function1" (func $function1 (param $a i32) (result i32)))
(import "namespace" "function2" (func $function2 (param $a i32) (result i32)))
(import "namespace" "function3" (func $function3 (param $a i32) (result i32)))
)
```
See `Orb.Import` for more.
Replaced by `Orb.Import.register/1`.
"""
# TODO: Move this to `Orb.Import`
@deprecated "Use `Orb.Import.register/1` instead"
defmacro importw(mod, namespace) when is_atom(namespace) do
quote do
@wasm_imports (for imp <- unquote(mod).__wasm_imports__(nil) do
Expand Down
24 changes: 24 additions & 0 deletions lib/orb/import.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ defmodule Orb.Import do
end
end

@doc """
Register an import `module` under `namespace`.
The `module` is defined via `use Orb.Import`. The `namespace` is the local name the import is registered under.
For each function defined within `module` an `(import)` instruction is defined in the outputted WebAssembly:
```wasm
(module $example
(import "namespace" "function1" (func $function1 (param $a i32) (result i32)))
(import "namespace" "function2" (func $function2 (param $a i32) (result i32)))
(import "namespace" "function3" (func $function3 (param $a i32) (result i32)))
)
```
"""
@since "0.0.46"

Check warning on line 71 in lib/orb/import.ex

View workflow job for this annotation

GitHub Actions / Build and test (1.15.2, 25.3.2)

module attribute @SInCE was set but never used
defmacro register(module, namespace) when is_atom(namespace) do
quote do
@wasm_imports (for imp <- unquote(module).__wasm_imports__(nil) do
%{imp | module: unquote(namespace)}
end)
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

0 comments on commit 91f951e

Please sign in to comment.