Skip to content

Commit 3064972

Browse files
authored
Added exported function to the loader wasi wat (#88)
The loader imports the `loader_load_wasi.wat` file which imports `random_get` from WASI. This PR extends the example to export a function which we can call in the host environment that leverages the `random_get` function.
1 parent 1619582 commit 3064972

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

examples/loader.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@
1818
# This imports our `loader_load_python.wat` file which then imports its own
1919
# python file.
2020
assert(loader_load_python.call_python() == 42)
21+
22+
# This imports our `loader_load_wasi.wat`, which in turn imports
23+
# the random_get functionality from the wasi runtime environment
24+
random_value = loader_load_wasi.wasi_random()
25+
assert(random_value >= 0 and random_value < 256)

examples/loader_load_wasi.wat

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
(module
2-
(import "wasi_snapshot_preview1" "random_get" (func (param i32 i32) (result i32)))
3-
)
2+
(import "wasi_snapshot_preview1" "random_get" (func $random_get (param i32 i32) (result i32)))
3+
(memory 1)
4+
(export "memory" (memory 0))
5+
(func $wasi_random (export "wasi_random")
6+
(result i32)
7+
(call $random_get
8+
(i32.const 0) ;; buffer start position
9+
(i32.const 1)) ;; buffer length 1 bytes
10+
;; this bounds our random between 0-255
11+
drop ;; random_get returns an error code
12+
(i32.const 0)
13+
i32.load))

0 commit comments

Comments
 (0)