View Source Orb.Memory (Orb v0.0.44)

Work with memory.

Summary

Functions

Increases the size of the memory instance by a specified number of pages. Each page is 64KiB.

Initializes data in memory. In Wat is (data …)

Load value of type from memory address.

Declare how many 64Kib pages of memory your module needs.

Returns the number of pages the memory instance currently has. Each page is 64KiB.

Store value of type at memory address.

A convenience for swapping two values of type at addresses address_a and address_b in memory.

Functions

Increases the size of the memory instance by a specified number of pages. Each page is 64KiB.

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

Memory.grow!(1)
Link to this macro

initial_data!(offset, value)

View Source (macro)

Initializes data in memory. In Wat is (data …)

Link to this function

load!(type, address, opts \\ [])

View Source

Load value of type from memory address.

Memory.load!(I32, 0x100)
Memory.load!(I32.U8, 0x100)

Examples

iex> use Orb
iex> Memory.load!(I32, 0x100) |> Orb.to_wat()
"(i32.load (i32.const 256))"
iex> Memory.load!(I32.U8, 0x100) |> Orb.to_wat()
"(i32.load8_u (i32.const 256))"
iex> Memory.load!(I32, 0x100, align: 2) |> Orb.to_wat()
"(i32.load align=2 (i32.const 256))"
iex> Memory.load!(I32, 0x100, align: 4) |> Orb.to_wat()
"(i32.load align=4 (i32.const 256))"

iex> use Orb
iex> Memory.load!(I32, 0x100, align: 3)
** (ArgumentError) malformed alignment 3

iex> use Orb
iex> Memory.load!(I32, 0x100, align: 8)
** (ArgumentError) alignment 8 must not be larger than natural 4

iex> use Orb
iex> Memory.load!(I32.U8, 0x100, align: 2)
** (ArgumentError) alignment 2 must not be larger than natural 1
Link to this macro

pages(page_count)

View Source (macro)

Declare how many 64Kib pages of memory your module needs.

Can be called multiple times, with each summed up.

Returns the previous page count, which can be used as a start offset.

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

Memory.size()
Link to this function

store!(type, address, value, opts \\ [])

View Source

Store value of type at memory address.

Memory.store!(I32, 0x100, 42)
Memory.store!(I32.U8, 0x100, ?a)

Examples

iex> use Orb
iex> Memory.store!(I32, 0x100, 42) |> Orb.to_wat()
"(i32.store (i32.const 256) (i32.const 42))"
iex> Memory.store!(I32.U8, 0x100, ?a) |> Orb.to_wat()
"(i32.store8 (i32.const 256) (i32.const 97))"
iex> Memory.store!(I32, 0x100, 42, align: 2) |> Orb.to_wat()
"(i32.store align=2 (i32.const 256) (i32.const 42))"
iex> Memory.store!(I32, 0x100, 42, align: 4) |> Orb.to_wat()
"(i32.store align=4 (i32.const 256) (i32.const 42))"

iex> use Orb
iex> Memory.store!(I32, 0x100, 42, align: 3)
** (ArgumentError) malformed alignment 3

iex> use Orb
iex> Memory.store!(I32, 0x100, 42, align: 8)
** (ArgumentError) alignment 8 must not be larger than natural 4

iex> use Orb
iex> Memory.store!(I32.U8, 0x100, 42, align: 2)
** (ArgumentError) alignment 2 must not be larger than natural 1
Link to this function

swap!(type, address_a, address_b, opts \\ [])

View Source

A convenience for swapping two values of type at addresses address_a and address_b in memory.

Bubble sort is an example of an algorithm which uses a swap.