Skip to content

Commit cba3cf1

Browse files
committed
Add arrays examples
Signed-off-by: Glenn Lewis <[email protected]>
1 parent 7262c95 commit cba3cf1

File tree

9 files changed

+201
-0
lines changed

9 files changed

+201
-0
lines changed

assets/simulatedExtismSdk.js

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export const importObject = {
125125
log_error: (offset) => console.error(`log_error: ${decodeOffset(offset)}`),
126126
log_warn: (offset) => console.warn(`log_warn: ${decodeOffset(offset)}`),
127127
output_set: (offset) => console.log(decodeOffset(offset)),
128+
error_set: (offset) => console.error(decodeOffset(offset)),
128129
store_u8: (offsetBigInt, byte) => {
129130
const offset = Number(offsetBigInt)
130131
Object.keys(fakeAlloc.buffers).forEach((key) => {

examples/arrays/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# examples/arrays
2+
3+
The `arrays.wasm` plugin can be run from the top-level of the repo by
4+
typing:
5+
6+
```bash
7+
$ ./build.sh
8+
$ ./scripts/python-server.sh
9+
```
10+
11+
Then open your browser window to:
12+
http://localhost:8080/examples/arrays

examples/arrays/arrays.mbt

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/// `progressive_sum_ints` tests passing arrays of ints.
2+
pub fn progressive_sum_ints() -> Int {
3+
let s = @host.input_string()
4+
let jv = try {
5+
@json.parse(s)!
6+
} catch {
7+
e => {
8+
@host.set_error("unable to parse input: \(e)")
9+
return 1 // failure
10+
}
11+
}
12+
//
13+
let ints = match jv.as_array() {
14+
Some(arr) => arr.map(fn(jv) { jv.as_number().unwrap().to_int() })
15+
_ => {
16+
@host.set_error("could not parse array")
17+
return 1 // failure
18+
}
19+
}
20+
let mut sum = 0
21+
ints.eachi(
22+
fn(index, value) {
23+
sum += value
24+
ints[index] = sum
25+
},
26+
)
27+
//
28+
let jv = @jsonutil.to_json(ints)
29+
@host.output_json_value(jv)
30+
0 // success
31+
}
32+
33+
/// `progressive_sum_floats` tests passing arrays of floats.
34+
pub fn progressive_sum_floats() -> Int {
35+
let s = @host.input_string()
36+
let jv = try {
37+
@json.parse(s)!
38+
} catch {
39+
e => {
40+
@host.set_error("unable to parse input: \(e)")
41+
return 1 // failure
42+
}
43+
}
44+
//
45+
let floats = match jv.as_array() {
46+
Some(arr) => arr.map(fn(jv) { jv.as_number().unwrap() })
47+
_ => {
48+
@host.set_error("could not parse array")
49+
return 1 // failure
50+
}
51+
}
52+
let mut sum = 0.0
53+
floats.eachi(
54+
fn(index, value) {
55+
sum += value
56+
floats[index] = sum
57+
},
58+
)
59+
//
60+
let jv = @jsonutil.to_json(floats)
61+
@host.output_json_value(jv)
62+
0 // success
63+
}
64+
65+
/// `progressive_concat_strings` tests passing arrays of strings.
66+
pub fn progressive_concat_strings() -> Int {
67+
let s = @host.input_string()
68+
let jv = try {
69+
@json.parse(s)!
70+
} catch {
71+
e => {
72+
@host.set_error("unable to parse input: \(e)")
73+
return 1 // failure
74+
}
75+
}
76+
//
77+
let strings = match jv.as_array() {
78+
Some(arr) => arr.map(fn(jv) { jv.as_string().unwrap() })
79+
_ => {
80+
@host.set_error("could not parse array")
81+
return 1 // failure
82+
}
83+
}
84+
let parts = []
85+
strings.eachi(
86+
fn(index, value) {
87+
parts.push(value)
88+
strings[index] = parts.join("|")
89+
},
90+
)
91+
//
92+
let jv = @jsonutil.to_json(strings)
93+
@host.output_json_value(jv)
94+
0 // success
95+
}

examples/arrays/index.html

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head></head>
5+
6+
<body>
7+
<script type="module">
8+
import { configs, flush, importObject, inputString } from '/assets/simulatedExtismSdk.js'
9+
10+
const wasmUnderTest = '/target/wasm/release/build/examples/arrays/arrays.wasm'
11+
12+
// WebAssembly.instantiateStreaming(fetch("/target/wasm-gc/release/build/examples/arrays/arrays.wasm"), importObject).then(
13+
WebAssembly.instantiateStreaming(fetch(wasmUnderTest), importObject).then(
14+
(obj) => {
15+
console.log('Using simulated Extism SDK...')
16+
console.log('ints:')
17+
inputString.value = '[0,1,2,3,4,5,6]'
18+
obj.instance.exports['progressive_sum_ints']()
19+
20+
console.log('floats:')
21+
inputString.value = '[0,0.1,0.2,0.3,0.4,0.5,0.6]'
22+
obj.instance.exports['progressive_sum_floats']()
23+
24+
console.log('strings:')
25+
inputString.value = '["0","1","2","3","4","5","6"]'
26+
obj.instance.exports['progressive_concat_strings']()
27+
flush()
28+
}
29+
)
30+
31+
// Next, use the official Extism JavaScript SDK:
32+
// Read the JS SDK docs at: https://extism.github.io/js-sdk/
33+
const extism = await import('https://esm.sh/@extism/extism')
34+
35+
const plugin = await extism.createPlugin(
36+
fetch(wasmUnderTest),
37+
{ useWasi: true }
38+
)
39+
40+
console.log('Using official Extism JavaScript SDK...')
41+
console.log('ints:')
42+
let out = await plugin.call('progressive_sum_ints', '[0,1,2,3,4,5,6]')
43+
console.log(out.text())
44+
45+
console.log('floats:')
46+
out = await plugin.call('progressive_sum_floats', '[0,0.1,0.2,0.3,0.4,0.5,0.6]')
47+
console.log(out.text())
48+
49+
console.log('strings:')
50+
out = await plugin.call('progressive_concat_strings', '["0","1","2","3","4","5","6"]')
51+
console.log(out.text())
52+
</script>
53+
</body>
54+
55+
</html>

examples/arrays/moon.pkg.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"import": [
3+
"extism/moonbit-pdk/pdk/config",
4+
"extism/moonbit-pdk/pdk/host",
5+
"extism/moonbit-pdk/pdk/var",
6+
{
7+
"path": "gmlewis/json",
8+
"alias": "jsonutil"
9+
}
10+
],
11+
"link": {
12+
"wasm": {
13+
"exports": [
14+
"progressive_sum_ints",
15+
"progressive_sum_floats",
16+
"progressive_concat_strings"
17+
],
18+
"export-memory-name": "memory"
19+
},
20+
"wasm-gc": {
21+
"exports": [
22+
"progressive_sum_ints",
23+
"progressive_sum_floats",
24+
"progressive_concat_strings"
25+
],
26+
"export-memory-name": "memory"
27+
}
28+
}
29+
}

run.sh

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
./scripts/count-vowels.sh 'Once upon a dream' && echo && echo
44
./scripts/http-get.sh && echo && echo
55
./scripts/kitchen-sink.sh 'Testing the kitchen sink' && echo && echo
6+
./scripts/arrays-ints.sh '[0,1,2,3,4,5,6]' && echo && echo
7+
./scripts/arrays-floats.sh '[0,0.1,0.2,0.3,0.4,0.5,0.6]' && echo && echo
8+
./scripts/arrays-strings.sh '["0","1","2","3","4","5","6"]' && echo && echo

scripts/arrays-floats.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash -ex
2+
extism call target/wasm/release/build/examples/arrays/arrays.wasm progressive_sum_floats --wasi --input "$@"

scripts/arrays-ints.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash -ex
2+
extism call target/wasm/release/build/examples/arrays/arrays.wasm progressive_sum_ints --wasi --input "$@"

scripts/arrays-strings.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash -ex
2+
extism call target/wasm/release/build/examples/arrays/arrays.wasm progressive_concat_strings --wasi --input "$@"

0 commit comments

Comments
 (0)