Skip to content

Commit 883cda5

Browse files
feat(lang/moonbit): add MoonBit tutorial (#307)
* doc(moonbit): add MoonBit explanation * fix(moonbit): bump tool version and extract code * chore(moonbit): use rust codefence for highlighting * fix(moonbit): apply suggestions * fix(moonbit): update installation section * fix(lang/moonbit): fix numbering of steps --------- Co-authored-by: Victor Adossi <[email protected]>
1 parent 5eaab37 commit 883cda5

File tree

9 files changed

+372
-0
lines changed

9 files changed

+372
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Generated by `wit-bindgen` 0.45.0.
2+
3+
///|
4+
fn init {
5+
let _ = @add.add(1, 2).unwrap_or_error() catch { Overflow => ... }
6+
7+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Generated by `wit-bindgen` 0.45.0. DO NOT EDIT!
2+
3+
///|
4+
pub(all) suberror ComputationError {
5+
Overflow
6+
} derive(Show, Eq)
7+
8+
///|
9+
pub fn add(x : UInt, y : UInt) -> Result[UInt, ComputationError] {
10+
let return_area = @ffi.malloc(8)
11+
wasmImportAdd(x.reinterpret_as_int(), y.reinterpret_as_int(), return_area)
12+
let lifted4 = match @ffi.load8_u(return_area + 0) {
13+
0 => Result::Ok(@ffi.load32(return_area + 4).reinterpret_as_uint())
14+
1 => {
15+
let lifted = match @ffi.load8_u(return_area + 4) {
16+
0 => ComputationError::Overflow
17+
_ => panic()
18+
}
19+
Result::Err(lifted)
20+
}
21+
_ => panic()
22+
}
23+
@ffi.free(return_area)
24+
return lifted4
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package docs:adder@0.1.0;
2+
3+
interface add {
4+
variant computation-error {
5+
overflow
6+
}
7+
add: func(x: u32, y: u32) -> result<u32, computation-error>;
8+
}
9+
10+
world adder {
11+
import add;
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
// link configuration for Wasm backend
3+
"link": {
4+
"wasm": {
5+
"exports": [
6+
// Export for cabi_realloc
7+
"cabi_realloc:cabi_realloc",
8+
// Export per the interface definition
9+
"wasmExportAdd:docs:adder/[email protected]#add"
10+
],
11+
"export-memory-name": "memory",
12+
"heap-start-address": 16
13+
}
14+
},
15+
"import": [
16+
{
17+
"path": "docs/adder/ffi",
18+
"alias": "ffi"
19+
},
20+
{
21+
"path": "docs/adder/gen/interface/docs/adder/add",
22+
"alias": "add"
23+
}
24+
]
25+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Generated by `wit-bindgen` 0.45.0.
2+
3+
///|
4+
pub fn add(x : UInt, y : UInt) -> UInt {
5+
x + y
6+
}

component-model/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- [JavaScript](./language-support/javascript.md)
2424
- [Python](./language-support/python.md)
2525
- [Rust](./language-support/rust.md)
26+
- [MoonBit](./language-support/moonbit.md)
2627
- [WebAssembly Text Format (WAT)](./language-support/wat.md)
2728
- [Other Languages](./language-support/other-languages.md)
2829
- [Running Components](./running-components.md)

component-model/src/introduction.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This documentation is aimed at _users_ of the component model: developers of lib
1919
| [Worlds] | [JavaScript] | |
2020
| | [Python] | |
2121
| | [Rust] | |
22+
| | [MoonBit] | |
2223

2324
[Why Components?]: ./design/why-component-model.md
2425
[Components]: ./design/components.md
@@ -31,6 +32,7 @@ This documentation is aimed at _users_ of the component model: developers of lib
3132
[JavaScript]: ./language-support/javascript.md
3233
[Python]: ./language-support/python.md
3334
[Rust]: ./language-support/rust.md
35+
[MoonBit]: ./language-support/moonbit.md
3436

3537
[Composing]: ./composing-and-distributing/composing.md
3638
[Running]: ./running-components.md

component-model/src/language-support.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ without using a higher-level language front-end.
3737
- [Rust Tooling](./language-support/rust.md)
3838
- [Building a Component with `cargo component`](./language-support/rust.md#building-a-component-with-cargo-component)
3939
- [Running a Component from Rust Applications](./language-support/rust.md#running-a-component-from-rust-appliacations)
40+
- [MoonBit Tooling](./language-support/moonbit.md)
4041
- [WebAssembly Text Format (WAT)](./language-support/wat.md#wat-webassembly-text-format)
4142
- [Building a Component from WAT with `wasm-tools`](./language-support/wat.md#building-a-component-with-wasm-tools)
4243
- [Running a Component with Wasmtime](./language-support/wat.md#running-a-component-with-wasmtime)

0 commit comments

Comments
 (0)