Skip to content

Commit

Permalink
Add Memory.size/0
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Nov 8, 2023
1 parent bbd21c2 commit 5355864
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/orb/instruction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ defmodule Orb.Instruction do
def global_set(type, global_name, value),
do: new(:global_effect, {:global_set, global_name, type}, [value])

def memory_size(), do: new(:memory, :size, [])
def memory_grow(value), do: new(:memory, :grow, [value])

defp type_check_operands(type, operation, operands) do
Expand Down
19 changes: 18 additions & 1 deletion lib/orb/memory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,27 @@ defmodule Orb.Memory do
Orb.Instruction.new(primitive_type, store_instruction, [address, value])
end

@doc """
Returns the number of pages the memory instance currently has. Each page is 64KiB.
To increase the number of pages call `grow!/1`.
https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Memory/Size
```elixir
Memory.size()
```
"""
def size() do
Orb.Instruction.memory_size()
end

@doc """
Increases the size of the memory instance by a specified number of pages. Each page is 64KiB.
It returns the previous size of memory, in pages, if the operation was successful, or returns -1 if the operation failed.
Note: only `0` and `1` are supported in today’s runtimes.
It returns the previous size of memory in pages if the operation was successful, or returns -1 if the operation failed.
https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Memory/Grow
Expand Down
7 changes: 6 additions & 1 deletion test/memory_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ defmodule MemoryTest do
"""
end

test "grow!/1" do
test "size/1 and grow!/1" do
defmodule Grow do
use Orb

defw grow() do
Memory.size()
:drop

Memory.grow!(1)
:drop

Expand All @@ -70,6 +73,8 @@ defmodule MemoryTest do
assert Orb.to_wat(Grow) == """
(module $Grow
(func $grow (export "grow")
(memory.size)
drop
(memory.grow (i32.const 1))
drop
(memory.grow (i32.const 0))
Expand Down

0 comments on commit 5355864

Please sign in to comment.