Skip to content

Commit

Permalink
Add doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Apr 3, 2024
1 parent 85004c7 commit 3eac8b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 28 additions & 0 deletions lib/wasmex/instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ defmodule Wasmex.Instance do

@doc ~S"""
Reads the value of an exported global.
## Examples
iex> wat = "(module
...> (global $answer i32 (i32.const 42))
...> (export \"answer\" (global $answer))
...> )"
iex> {:ok, store} = Wasmex.Store.new()
iex> {:ok, module} = Wasmex.Module.compile(store, wat)
iex> {:ok, instance} = Wasmex.Instance.new(store, module, %{})
iex> Wasmex.Instance.get_global_value(store, instance, "answer")
{:ok, 42}
iex> Wasmex.Instance.get_global_value(store, instance, "not_a_global")
{:error, "exported global `not_a_global` not found"}
"""
@spec get_global_value(Wasmex.StoreOrCaller.t(), __MODULE__.t(), binary()) ::
{:ok, number()} | {:error, binary()}
Expand All @@ -195,6 +209,20 @@ defmodule Wasmex.Instance do

@doc ~S"""
Sets the value of an exported mutable global.
## Examples
iex> wat = "(module
...> (global $count (mut i32) (i32.const 0))
...> (export \"count\" (global $count))
...> )"
iex> {:ok, store} = Wasmex.Store.new()
iex> {:ok, module} = Wasmex.Module.compile(store, wat)
iex> {:ok, instance} = Wasmex.Instance.new(store, module, %{})
iex> Wasmex.Instance.set_global_value(store, instance, "count", 1)
:ok
iex> Wasmex.Instance.get_global_value(store, instance, "count")
{:ok, 1}
"""
@spec set_global_value(Wasmex.StoreOrCaller.t(), __MODULE__.t(), binary(), number()) ::
{:ok, number()} | {:error, binary()}
Expand Down
4 changes: 2 additions & 2 deletions test/wasmex/instance_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ defmodule Wasmex.InstanceTest do

describe "globals" do
setup do
source = File.read!("#{Path.dirname(__ENV__.file)}/../example_wasm_files/globals.wat")
wat = File.read!("#{Path.dirname(__ENV__.file)}/../example_wasm_files/globals.wat")
{:ok, store} = Wasmex.Store.new()
{:ok, module} = Wasmex.Module.compile(store, source)
{:ok, module} = Wasmex.Module.compile(store, wat)
{:ok, instance} = Wasmex.Instance.new(store, module, %{})
%{instance: instance, store: store}
end
Expand Down

0 comments on commit 3eac8b8

Please sign in to comment.