-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perfect for reusable libraries, as their namespacing won’t clash
- Loading branch information
1 parent
be4c6a8
commit 88c4afb
Showing
8 changed files
with
253 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Examples.Arena do | ||
# See https://www.rfleury.com/p/untangling-lifetimes-the-arena-allocator | ||
|
||
use Orb | ||
|
||
defmacro def(name, opts) do | ||
# module_name = Module.concat(__MODULE__, Macro.expand_literals(name, __CALLER__)) | ||
# Module.put_attribute(module_name, :wasm_func_prefix, module_name) | ||
|
||
quote do | ||
require Orb.Memory | ||
page_offset = Orb.Memory.pages(unquote(opts[:pages])) | ||
|
||
offset_global_name = | ||
String.to_atom("#{Macro.inspect_atom(:literal, unquote(name))}.bump_offset") | ||
|
||
global( | ||
do: [ | ||
{offset_global_name, page_offset * 64 * 1024} | ||
] | ||
) | ||
|
||
# defmodule unquote(name) do | ||
# use Orb | ||
|
||
# offset_global_name = unquote(offset_global_name) | ||
|
||
# # https://man7.org/linux/man-pages/man3/alloca.3.html | ||
# defw alloc(byte_count: I32), I32.UnsafePointer do | ||
# push(global_get(unquote(offset_global_name))) do | ||
# # @bump_offset = I32.add(@bump_offset, size) | ||
# end | ||
# end | ||
# end | ||
|
||
module_name = Module.concat(__MODULE__, unquote(name)) | ||
|
||
Module.create(module_name, quote do | ||
use Orb | ||
|
||
# offset_global_name = unquote(offset_global_name) | ||
|
||
set_func_prefix(inspect(unquote(module_name))) | ||
|
||
# @wasm_func_prefix inspect(unquote(module_name)) | ||
# Module.put_attribute(__MODULE__, :wasm_func_prefix, inspect(unquote(module_name))) | ||
# IO.puts("put_attribute") | ||
# IO.inspect(__MODULE__) | ||
# IO.inspect(inspect(unquote(module_name))) | ||
|
||
# https://man7.org/linux/man-pages/man3/alloca.3.html | ||
defw alloc(byte_count: I32), I32.UnsafePointer do | ||
0x0 | ||
# push(global_get(unquote(offset_global_name))) do | ||
# @bump_offset = I32.add(@bump_offset, size) | ||
# end | ||
end | ||
end, unquote(Macro.Env.location(__CALLER__))) | ||
|
||
# alias module_name | ||
# require module_name, as: unquote(name) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule Examples.ArenaTest do | ||
use ExUnit.Case, async: true | ||
|
||
Code.require_file("arena.exs", __DIR__) | ||
alias Examples.Arena | ||
|
||
alias OrbWasmtime.{Instance, Wasm} | ||
|
||
defmodule A do | ||
use Orb | ||
require Arena | ||
|
||
Arena.def(First, pages: 2) | ||
Arena.def(Second, pages: 3) | ||
|
||
Orb.include(A.First) | ||
|
||
defw test() do | ||
A.First.alloc(16) | ||
end | ||
end | ||
|
||
test "add func prefixes" do | ||
assert ~S""" | ||
(module $A | ||
(memory (export "memory") 5) | ||
(global $First.bump_offset (mut i32) (i32.const 0)) | ||
(global $Second.bump_offset (mut i32) (i32.const 131072)) | ||
(func $Examples.ArenaTest.A.First.alloc (param $byte_count i32) (result i32) | ||
(i32.const 0) | ||
) | ||
(func $test (export "test") | ||
(call $Examples.ArenaTest.A.First.alloc (i32.const 16)) | ||
) | ||
) | ||
""" = A.to_wat() | ||
end | ||
end |
Oops, something went wrong.