Skip to content

Commit ea1f14e

Browse files
Add support for MoonBit (#1020)
* feat: initial commit for moonbit * feat(moonbit): use teavm-java as reference * feat(moonbit): convert some types * feat(moonbit): type enum/variant + cleanup result/tuple * feat(moonbit): type record * fix(moonbit): interface generation * feat(moonbit): clean up interface generator * feat(moonbit): add test and start bindgen * feat(moonbit): wip, bindgen * fix(moonbit): array api * feat(moonbit): adjust test output * feat(moonbit): partial support for lower/lift enum/variant * fix(moonbit): variant cases * misc(moonbit): small rewrites * feat(moonbit): add exportation * fix(moonbit): cleanup return area * feat(moonbit): reorganize generated directory structure fix(moonbit): char conversion and previous bug * feat(moonbit): lower / lift string * fix(moonbit): typo * feat(moonbit): fix qualifier and cleanup directory structure * fix(moonbit): attemp to fix qualifier issue * fix(moonbit): misimplemented instructions * refactor(moonbit): flag representation * fix(moonbit): variant and tuple and release all tests * feat(moonbit): add resource support * fix(moonbit): generation for Option and others * refactor(moonbit): improve generated code * fix(moonbit): add keywords * refactor(moonbit): merge contructor name and type name * fix(moonbit): forgotten import * fix(moonbit): forgotten keywords * refactor(moonbit): move exportations * refactor(moonbit): reorganize files and fix imports * refactor(moonbit): move exportations to gen * fix(moonbit): flag support * fix(moonbit): world type import/export * fix(moonbit): qualifier related issues * refactor(moonbit): cleanup to make logic clear and robust * fix(moonbit): configuration mistake * cleanup: remove warnings * fix(moonbit): configuration and local conflicts * fix(moonbit): separate export and import types * fix(moonbit): pass all tests * fix(moonbit): add resource-drop, cabi-realloc and order imports and docs * misc(moonbit): modify naming convention * feat(moonbit): add derive show option * misc(moonbit): update generated package name and generate doc * fix(moonbit): string length should be char number * misc(moonbit): naming convention * ci(moonbit): add test ci for moonbit * misc(moonbit): change representation for u16 * fix: abi change * misc(moonbit): moon-bit -> moonbit * feat(moonbit): derive eq * misc(moonbit): avoid builtin type names * feat(moonbit): lower and lift string * misc(moonbit): adjust heap start address * feat(moonbit): lower lift bytes * feat(moonbit): wip add core f32 support * fix(moonbit): keyword update * fix(moonbit): reserve space * misc(moonbit): replace inline wasm with builtin functions * feat(moonbit): lift/lower array * ci(moonbit): use powershell for windows installation * misc(moonbit): fix preamble * misc(moonbit): add todo for future work * refactor(moonbit): split stub file * fix(moonbit): allocate return area for exportation functions * misc(moonbit): add documentation * misc(moonbit): ignore stub file option * fix(moonbit): ci check * fix(moonbit): separate ffi
1 parent 0d18c6b commit ea1f14e

File tree

9 files changed

+2760
-1
lines changed

9 files changed

+2760
-1
lines changed

.github/workflows/main.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
fail-fast: false
5959
matrix:
6060
os: [ubuntu-latest, macos-latest, windows-latest]
61-
lang: [c, rust, teavm-java, go, csharp]
61+
lang: [c, rust, teavm-java, go, csharp, moonbit]
6262
runs-on: ${{ matrix.os }}
6363
steps:
6464
- uses: actions/checkout@v4
@@ -84,6 +84,16 @@ jobs:
8484
$LOCALAPPDATA/Microsoft/dotnet/dotnet --info
8585
if: matrix.os == 'windows-latest' && matrix.lang == 'csharp'
8686
87+
- run: |
88+
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
89+
echo "$HOME/.moon/bin" >> $GITHUB_PATH
90+
if: matrix.os != 'windows-latest' && matrix.lang == 'moonbit'
91+
- run: |
92+
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser; irm https://cli.moonbitlang.com/install/powershell.ps1 | iex
93+
"C:\Users\runneradmin\.moon\bin" | Out-File -FilePath $env:GITHUB_PATH -Append
94+
shell: powershell
95+
if: matrix.os == 'windows-latest' && matrix.lang == 'moonbit'
96+
8797
- run: ci/download-teavm.sh
8898
if: matrix.lang == 'teavm-java'
8999
- uses: actions/setup-java@v4
@@ -149,6 +159,7 @@ jobs:
149159
- run: cargo build --no-default-features --features go
150160
- run: cargo build --no-default-features --features csharp
151161
- run: cargo build --no-default-features --features markdown
162+
- run: cargo build --no-default-features --features moonbit
152163

153164
# Feature combos of the `wit-bindgen` crate
154165
- run: cargo build --target wasm32-wasip1 -p wit-bindgen --no-default-features

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ wit-bindgen-teavm-java = { path = 'crates/teavm-java', version = '0.30.0' }
4545
wit-bindgen-go = { path = 'crates/go', version = '0.30.0' }
4646
wit-bindgen-csharp = { path = 'crates/csharp', version = '0.30.0' }
4747
wit-bindgen-markdown = { path = 'crates/markdown', version = '0.30.0' }
48+
wit-bindgen-moonbit = { path = 'crates/moonbit', version = '0.30.0' }
4849
wit-bindgen = { path = 'crates/guest-rust', version = '0.30.0', default-features = false }
4950

5051
[[bin]]
@@ -57,6 +58,7 @@ wit-bindgen-core = { workspace = true }
5758
wit-bindgen-rust = { workspace = true, features = ['clap'], optional = true }
5859
wit-bindgen-c = { workspace = true, features = ['clap'], optional = true }
5960
wit-bindgen-markdown = { workspace = true, features = ['clap'], optional = true }
61+
wit-bindgen-moonbit = { workspace = true, features = ['clap'], optional = true }
6062
wit-bindgen-teavm-java = { workspace = true, features = ['clap'], optional = true }
6163
wit-bindgen-go = { workspace = true, features = ['clap'], optional = true }
6264
wit-bindgen-csharp = { workspace = true, features = ['clap'], optional = true }
@@ -71,6 +73,7 @@ default = [
7173
'teavm-java',
7274
'go',
7375
'csharp',
76+
'moonbit',
7477
]
7578
c = ['dep:wit-bindgen-c']
7679
rust = ['dep:wit-bindgen-rust']
@@ -79,6 +82,7 @@ teavm-java = ['dep:wit-bindgen-teavm-java']
7982
go = ['dep:wit-bindgen-go']
8083
csharp = ['dep:wit-bindgen-csharp']
8184
csharp-mono = ['csharp']
85+
moonbit = ['dep:wit-bindgen-moonbit']
8286

8387
[dev-dependencies]
8488
heck = { workspace = true }

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,25 @@ wasm-tools component new main.embed.wasm --adapt wasi_snapshot_preview1.command.
381381
wasm-tools validate main.component.wasm --features component-model
382382
```
383383

384+
### Guest: MoonBit
385+
386+
MoonBit can be compiled to WebAssembly using [its toolchain](https://moonbitlang.com/download):
387+
388+
```sh
389+
moon build --target wasm # -g to keep symbols
390+
```
391+
392+
The generarted core wasm will be found under `target/wasm/release/build/gen/gen.wasm` by default. Then you can use `wasm-tools` to componentize the module:
393+
394+
```
395+
wasm-tools component embed wit target/wasm/release/build/gen/gen.wasm -o target/gen.wasm
396+
wasm-tools component new target/gen.wasm -o target/gen.component.wasm
397+
```
398+
399+
When using `wit-bindgen moonbit`, you may use `--derive-show` or `--derive-eq` to derive `Show` or `Eq` for all types.
400+
401+
You will find the files to be modified with the name `**/stub.mbt`.
402+
384403
### Guest: Other Languages
385404

386405
Other languages such as JS, Ruby, Python, etc, are hoped to be supported one day

ci/publish.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const CRATES_TO_PUBLISH: &[&str] = &[
2424
"wit-bindgen-csharp",
2525
"wit-bindgen-teavm-java",
2626
"wit-bindgen-markdown",
27+
"wit-bindgen-moonbit",
2728
"wit-bindgen-rust-macro",
2829
"wit-bindgen-rt",
2930
"wit-bindgen",

crates/moonbit/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "wit-bindgen-moonbit"
3+
authors = ["YE Zihang <[email protected]>"]
4+
version = { workspace = true }
5+
edition = { workspace = true }
6+
repository = { workspace = true }
7+
license = { workspace = true }
8+
homepage = 'https://github.com/bytecodealliance/wit-bindgen'
9+
description = """
10+
MoonBit bindings generator for WIT and the component model, typically used
11+
through the `wit-bindgen-cli` crate.
12+
"""
13+
14+
[dependencies]
15+
anyhow = { workspace = true }
16+
wit-bindgen-core = { workspace = true }
17+
wit-component = { workspace = true }
18+
wasm-metadata = { workspace = true }
19+
heck = { workspace = true }
20+
clap = { workspace = true, optional = true }
21+
22+
[dev-dependencies]
23+
test-helpers = { path = '../test-helpers' }

0 commit comments

Comments
 (0)