-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
116 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# examples/add | ||
|
||
The `add.wasm` plugin can be run from the top-level of the repo by | ||
typing: | ||
|
||
```bash | ||
$ ./build.sh | ||
$ ./scripts/add.sh '{"a": 20, "b": 21}' | ||
# => {"sum":41} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
struct Add { | ||
a : Int | ||
b : Int | ||
} | ||
|
||
pub fn Add::from_json(value : @json.JsonValue) -> Add? { | ||
let value = match value.as_object() { | ||
Some(v) => v | ||
_ => return None | ||
} | ||
let a = match value.get("a") { | ||
Some(v) => v | ||
_ => return None | ||
} | ||
let a = a.as_number() | ||
let b = match value.get("b") { | ||
Some(v) => v | ||
_ => return None | ||
} | ||
let b = b.as_number() | ||
match (a, b) { | ||
(Some(a), Some(b)) => Some({ a: a.to_int(), b: b.to_int() }) | ||
_ => None | ||
} | ||
} | ||
|
||
type! ParseError String derive(Show) | ||
|
||
pub fn Add::parse(s : String) -> Add!ParseError { | ||
match @json.parse?(s) { | ||
Ok(jv) => | ||
match Add::from_json(jv) { | ||
Some(value) => value | ||
None => raise ParseError("unable to parse Add \{s}") | ||
} | ||
Err(e) => raise ParseError("unable to parse Add \{s}: \{e}") | ||
} | ||
} | ||
|
||
struct Sum { | ||
sum : Int | ||
} | ||
|
||
pub fn to_json(self : Sum) -> Json { | ||
{ "sum": self.sum.to_json() } | ||
} | ||
|
||
pub fn add() -> Int { | ||
let input = @host.input_string() | ||
let params = try { | ||
Add::parse!(input) | ||
} catch { | ||
ParseError(e) => { | ||
@host.set_error(e) | ||
return 1 | ||
} | ||
} | ||
// | ||
let sum = { sum: params.a + params.b } | ||
let json_value = sum.to_json() | ||
@host.output_json_value(json_value) | ||
0 // success | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"import": [ | ||
"extism/moonbit-pdk/pdk/host" | ||
], | ||
"link": { | ||
"wasm": { | ||
"exports": [ | ||
"add" | ||
], | ||
"export-memory-name": "memory" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash -ex | ||
extism call target/wasm/release/build/examples/add/add.wasm add --wasi --input "$@" |