Skip to content

Commit

Permalink
Add AllThree object to examples/arrays
Browse files Browse the repository at this point in the history
Signed-off-by: Glenn Lewis <[email protected]>
  • Loading branch information
gmlewis committed Jul 19, 2024
1 parent 317f6c9 commit e00914a
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 25 deletions.
24 changes: 24 additions & 0 deletions examples/arrays/all-three.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// `AllThree` represents a JSON object with all three array types.
pub struct AllThree {
ints : Array[Int]
floats : Array[Double]
strings : Array[String]
} derive(Debug, Eq, Show)

pub impl @jsonutil.ToJson for AllThree with to_json(self) {
let fields : Array[(String, @jsonutil.ToJson)] = [
("ints", self.ints),
("floats", self.floats),
("strings", self.strings),
]
@jsonutil.from_entries(fields)
}

/// `process_all_three` processes all three array types.
pub fn process_all_three(in : AllThree) -> AllThree {
{
ints: process_ints(in.ints),
floats: process_floats(in.floats),
strings: process_strings(in.strings),
}
}
11 changes: 11 additions & 0 deletions examples/arrays/floats.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// `process_floats` sums up an array of floats.
pub fn process_floats(floats : Array[Double]) -> Array[Double] {
let mut sum = 0.0
floats.eachi(
fn(index, value) {
sum += value
floats[index] = sum
},
)
floats
}
8 changes: 8 additions & 0 deletions examples/arrays/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
console.log('strings:')
inputString.value = '["0","1","2","3","4","5","6"]'
obj.instance.exports['progressive_concat_strings']()

console.log('all three:')
inputString.value = '{"ints":[0,1,2,3,4,5,6],"floats":[0,0.1,0.2,0.3,0.4,0.5,0.6],"strings":["0","1","2","3","4","5","6"]}'
obj.instance.exports['all_three_object']()
flush()
}
)
Expand All @@ -49,6 +53,10 @@
console.log('strings:')
out = await plugin.call('progressive_concat_strings', '["0","1","2","3","4","5","6"]')
console.log(out.text())

console.log('all three:')
out = await plugin.call('all_three_object', '{"ints":[0,1,2,3,4,5,6],"floats":[0,0.1,0.2,0.3,0.4,0.5,0.6],"strings":["0","1","2","3","4","5","6"]}')
console.log(out.text())
</script>
</body>

Expand Down
11 changes: 11 additions & 0 deletions examples/arrays/ints.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// `process_ints` sums up an array of ints.
pub fn process_ints(ints : Array[Int]) -> Array[Int] {
let mut sum = 0
ints.eachi(
fn(index, value) {
sum += value
ints[index] = sum
},
)
ints
}
2 changes: 2 additions & 0 deletions examples/arrays/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"link": {
"wasm": {
"exports": [
"all_three_object",
"progressive_sum_ints",
"progressive_sum_floats",
"progressive_concat_strings"
Expand All @@ -16,6 +17,7 @@
},
"wasm-gc": {
"exports": [
"all_three_object",
"progressive_sum_ints",
"progressive_sum_floats",
"progressive_concat_strings"
Expand Down
77 changes: 53 additions & 24 deletions examples/arrays/arrays.mbt → examples/arrays/plugin-functions.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ pub fn progressive_sum_ints() -> Int {
return 1 // failure
}
}
let mut sum = 0
ints.eachi(
fn(index, value) {
sum += value
ints[index] = sum
},
)
//
let jv = @jsonutil.to_json(ints)
let result = process_ints(ints)
//
let jv = @jsonutil.to_json(result)
@host.output_json_value(jv)
0 // success
}
Expand All @@ -49,15 +44,10 @@ pub fn progressive_sum_floats() -> Int {
return 1 // failure
}
}
let mut sum = 0.0
floats.eachi(
fn(index, value) {
sum += value
floats[index] = sum
},
)
//
let jv = @jsonutil.to_json(floats)
let result = process_floats(floats)
//
let jv = @jsonutil.to_json(result)
@host.output_json_value(jv)
0 // success
}
Expand All @@ -81,15 +71,54 @@ pub fn progressive_concat_strings() -> Int {
return 1 // failure
}
}
let parts = []
strings.eachi(
fn(index, value) {
parts.push(value)
strings[index] = parts.join("|")
},
)
//
let jv = @jsonutil.to_json(strings)
let result = process_strings(strings)
//
let jv = @jsonutil.to_json(result)
@host.output_json_value(jv)
0 // success
}

/// `all_three_object` tests passing an object of all three arrays.
pub fn all_three_object() -> Int {
let s = @host.input_string()
let jv = try {
@json.parse!(s)
} catch {
e => {
@host.set_error("unable to parse input: \(e)")
return 1 // failure
}
}
//
let all_three : AllThree = match jv {
Object(
{
"ints": Some(Array(ints)),
"floats": Some(Array(floats)),
"strings": Some(Array(strings)),
}
) => {
let ints : Array[Int] = ints.map(fn { n => n.as_number() }).filter(
fn { n => not(n.is_empty()) },
).map(fn { n => n.unwrap().to_int() })
let floats : Array[Double] = floats.map(fn { n => n.as_number() }).filter(
fn { n => not(n.is_empty()) },
).map(fn { n => n.unwrap() })
let strings : Array[String] = strings.map(fn { n => n.as_string() }).filter(
fn { n => not(n.is_empty()) },
).map(fn { n => n.unwrap() })
{ ints, floats, strings }
}
_ => {
@host.set_error("unable to parse input: \(s)")
return 1 // failure
}
}
//
let result = process_all_three(all_three)
//
let jv = @jsonutil.to_json(result)
@host.output_json_value(jv)
0 // success
}
11 changes: 11 additions & 0 deletions examples/arrays/strings.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// `process_strings` concatenates an array of strings.
pub fn process_strings(strings : Array[String]) -> Array[String] {
let parts = []
strings.eachi(
fn(index, value) {
parts.push(value)
strings[index] = parts.join("|")
},
)
strings
}
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "extism/moonbit-pdk",
"version": "0.26.0",
"version": "0.27.0",
"deps": {
"gmlewis/jsonutil": "0.17.0"
},
Expand Down
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
./scripts/arrays-ints.sh '[0,1,2,3,4,5,6]' && echo && echo
./scripts/arrays-floats.sh '[0,0.1,0.2,0.3,0.4,0.5,0.6]' && echo && echo
./scripts/arrays-strings.sh '["0","1","2","3","4","5","6"]' && echo && echo
./scripts/arrays-object.sh '{"ints":[0,1,2,3,4,5,6],"floats":[0,0.1,0.2,0.3,0.4,0.5,0.6],"strings":["0","1","2","3","4","5","6"]}' && echo && echo
2 changes: 2 additions & 0 deletions scripts/arrays-object.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash -ex
extism call target/wasm/release/build/examples/arrays/arrays.wasm all_three_object --wasi --input "$@"

0 comments on commit e00914a

Please sign in to comment.