diff --git a/examples/arrays/all-three.mbt b/examples/arrays/all-three.mbt new file mode 100644 index 0000000..b90c1e8 --- /dev/null +++ b/examples/arrays/all-three.mbt @@ -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), + } +} diff --git a/examples/arrays/floats.mbt b/examples/arrays/floats.mbt new file mode 100644 index 0000000..a13278a --- /dev/null +++ b/examples/arrays/floats.mbt @@ -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 +} diff --git a/examples/arrays/index.html b/examples/arrays/index.html index a583aa3..6fabfcd 100644 --- a/examples/arrays/index.html +++ b/examples/arrays/index.html @@ -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() } ) @@ -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()) diff --git a/examples/arrays/ints.mbt b/examples/arrays/ints.mbt new file mode 100644 index 0000000..4981d6b --- /dev/null +++ b/examples/arrays/ints.mbt @@ -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 +} diff --git a/examples/arrays/moon.pkg.json b/examples/arrays/moon.pkg.json index 0930402..eff3f36 100644 --- a/examples/arrays/moon.pkg.json +++ b/examples/arrays/moon.pkg.json @@ -8,6 +8,7 @@ "link": { "wasm": { "exports": [ + "all_three_object", "progressive_sum_ints", "progressive_sum_floats", "progressive_concat_strings" @@ -16,6 +17,7 @@ }, "wasm-gc": { "exports": [ + "all_three_object", "progressive_sum_ints", "progressive_sum_floats", "progressive_concat_strings" diff --git a/examples/arrays/arrays.mbt b/examples/arrays/plugin-functions.mbt similarity index 52% rename from examples/arrays/arrays.mbt rename to examples/arrays/plugin-functions.mbt index 9ee0cef..f4aa790 100644 --- a/examples/arrays/arrays.mbt +++ b/examples/arrays/plugin-functions.mbt @@ -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 } @@ -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 } @@ -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 } diff --git a/examples/arrays/strings.mbt b/examples/arrays/strings.mbt new file mode 100644 index 0000000..e65dbb4 --- /dev/null +++ b/examples/arrays/strings.mbt @@ -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 +} diff --git a/moon.mod.json b/moon.mod.json index 328cbb1..ba0a9b2 100644 --- a/moon.mod.json +++ b/moon.mod.json @@ -1,6 +1,6 @@ { "name": "extism/moonbit-pdk", - "version": "0.26.0", + "version": "0.27.0", "deps": { "gmlewis/jsonutil": "0.17.0" }, diff --git a/run.sh b/run.sh index b0b4348..ea79b7b 100755 --- a/run.sh +++ b/run.sh @@ -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 diff --git a/scripts/arrays-object.sh b/scripts/arrays-object.sh new file mode 100755 index 0000000..156c47b --- /dev/null +++ b/scripts/arrays-object.sh @@ -0,0 +1,2 @@ +#!/bin/bash -ex +extism call target/wasm/release/build/examples/arrays/arrays.wasm all_three_object --wasi --input "$@"