Skip to content

Commit 9af62ea

Browse files
feat(moonbit): migrate generated manifests to modern formats
1 parent ac8ff51 commit 9af62ea

18 files changed

Lines changed: 159 additions & 75 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ wasm-tools component new target/gen.wasm -o target/gen.component.wasm
407407
You may use `--gen-dir` to specify which package should be responsible for the exportation. The default is `gen` as mentioned above.
408408
This can be useful having one project that exports multiple worlds.
409409

410+
Generated projects use `moon.mod` and `moon.pkg`, with WebAssembly export mappings
411+
configured in the export package's `moon.pkg`.
412+
410413
When using `wit-bindgen moonbit`, you may use `--derive-show` or `--derive-eq` to derive `Show` or `Eq` traits for all types.
411414
You may also use `--derive-error`, which will make types containing `Error` as error types in MoonBit.
412415

@@ -424,7 +427,7 @@ toolchain are mixed with new bindings, components may still compile but fail at
424427
runtime with corrupted strings/bytes or traps.
425428

426429
You will find the files to be modified with the name `**/stub.mbt`.
427-
To avoid touching the files during regeneration (including `moon.pkg.json` or `moon.mod.json`) you may use `--ignore-stub`.
430+
To avoid touching the files during regeneration (including `moon.pkg` or `moon.mod`) you may use `--ignore-stub`.
428431

429432
/!\ MoonBit is still evolving, so please check out the [Weekly Updates](https://www.moonbitlang.com/weekly-updates/) for any breaking changes or deprecations.
430433

‎crates/moonbit/src/async/moon.pkg‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {
2+
"moonbitlang/core/deque" @deque,
3+
"moonbitlang/core/ref" @ref,
4+
"moonbitlang/core/set" @set,
5+
}
6+
7+
warnings = "-44"
8+
supported_targets = "wasm"

‎crates/moonbit/src/async/moon.pkg.json‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎crates/moonbit/src/async_support.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ const ASYNC_SEMAPHORE: &str = include_str!("./async/semaphore.mbt");
295295
const ASYNC_TASK: &str = include_str!("./async/task.mbt");
296296
const ASYNC_TASK_GROUP: &str = include_str!("./async/task_group.mbt");
297297
const ASYNC_TRAIT: &str = include_str!("./async/trait.mbt");
298-
const ASYNC_PKG: &str = include_str!("./async/moon.pkg.json");
298+
const ASYNC_PKG: &str = include_str!("./async/moon.pkg");
299299

300300
struct Segment<'a> {
301301
name: &'a str,
@@ -463,10 +463,7 @@ impl AsyncSupport {
463463
s.src.as_bytes(),
464464
);
465465
});
466-
files.push(
467-
&format!("{ASYNC_CORE_DIR}/moon.pkg.json"),
468-
ASYNC_PKG.as_bytes(),
469-
);
466+
files.push(&format!("{ASYNC_CORE_DIR}/moon.pkg"), ASYNC_PKG.as_bytes());
470467
}
471468
}
472469

‎crates/moonbit/src/lib.rs‎

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct Opts {
5353
#[cfg_attr(feature = "clap", arg(long, default_value_t = false))]
5454
pub ignore_stub: bool,
5555

56-
/// Whether or not to generate moon.mod.json ; useful if the project is part of a larger project
56+
/// Whether or not to generate moon.mod ; useful if the project is part of a larger project
5757
#[cfg_attr(feature = "clap", arg(long, default_value_t = false))]
5858
pub ignore_module_file: bool,
5959

@@ -156,41 +156,34 @@ impl MoonBit {
156156
}
157157

158158
fn write_moon_pkg(&self, moon_pkg: &mut Source, imports: Option<&Imports>, link: bool) {
159-
// Disable warning for invalid inline wasm
160-
moon_pkg.push_str("{\n\"warn-list\": \"-44\"");
161159
// Dependencies
162160
if let Some(imports) = imports {
163-
moon_pkg.push_str(",\n\"import\": [\n");
164-
moon_pkg.indent(1);
161+
moon_pkg.push_str("import {\n");
165162
let mut deps = imports
166163
.packages
167164
.iter()
168-
.map(|(k, v)| {
169-
format!(
170-
"{{ \"path\" : \"{}/{}\", \"alias\" : \"{}\" }}",
171-
self.project_name,
172-
k.replace(".", "/"),
173-
v
174-
)
175-
})
165+
.map(|(k, v)| format!("\"{}/{}\" @{},", self.project_name, k.replace(".", "/"), v))
176166
.collect::<Vec<_>>();
177167
deps.sort();
178-
uwrite!(moon_pkg, "{}", deps.join(",\n"));
179-
moon_pkg.deindent(1);
180-
moon_pkg.push_str("\n]");
168+
uwriteln!(moon_pkg, "{}", deps.join("\n"));
169+
moon_pkg.push_str("}\n\n");
181170
}
171+
// Disable warning for invalid inline wasm.
172+
moon_pkg.push_str("warnings = \"-44\"\n");
182173
let imports_async_core =
183174
imports.is_some_and(|imports| imports.packages.contains_key(ASYNC_CORE_DIR));
184175
if imports_async_core || (link && self.async_support.is_required()) {
185-
moon_pkg.push_str(",\n\"supported-targets\": \"+wasm\"");
176+
moon_pkg.push_str("supported_targets = \"wasm\"\n");
186177
}
187178
// Link target
188179
if link {
189180
let memory_name = self.pkg_resolver.resolve.wasm_export_name(
190181
ManglingAndAbi::Legacy(LiftLowerAbi::Sync),
191182
WasmExport::Memory,
192183
);
193-
moon_pkg.push_str(",\n\"link\": {\n\"wasm\": {\n");
184+
moon_pkg.push_str("\npkgtype(kind: \"foreign_library\")\n\noptions(\n");
185+
moon_pkg.indent(1);
186+
moon_pkg.push_str("link: {\n\"wasm\": {\n");
194187
moon_pkg.push_str(&format!("\"export-memory-name\": \"{memory_name}\",\n"));
195188
moon_pkg.push_str("\"heap-start-address\": 16,\n");
196189
moon_pkg.push_str("\"exports\": [\n");
@@ -210,9 +203,10 @@ impl MoonBit {
210203
exports.sort();
211204
uwrite!(moon_pkg, "{}", exports.join(",\n"));
212205
moon_pkg.deindent(1);
213-
moon_pkg.push_str("\n]\n}\n}\n");
206+
moon_pkg.push_str("\n],\n},\n},\n");
207+
moon_pkg.deindent(1);
208+
moon_pkg.push_str(")\n");
214209
}
215-
moon_pkg.push_str("\n}\n");
216210
}
217211
}
218212

@@ -227,7 +221,7 @@ impl MoonBit {
227221
/// - `preprocess`: Initialize generator-wide state (package resolver,
228222
/// project name, and size/align information) for the current world.
229223
/// - `import_interface` / `export_interface`: Generate per-interface
230-
/// sources, FFI glue, README documentation and `moon.pkg.json` metadata.
224+
/// sources, FFI glue, README documentation and `moon.pkg` metadata.
231225
/// - `import_funcs` / `export_funcs` / `import_types`: Collect and accumulate
232226
/// world-level functions and types (the `$root` module) into fragments
233227
/// that are later written out by `finish_imports` or `finish`.
@@ -313,14 +307,14 @@ impl WorldGenerator for MoonBit {
313307
}
314308
files.push(&format!("{directory}/ffi.mbt"), indent(&ffi).as_bytes());
315309

316-
// moon.pkg.json
310+
// moon.pkg
317311
let mut moon_pkg = Source::default();
318312
self.write_moon_pkg(
319313
&mut moon_pkg,
320314
self.pkg_resolver.package_import.get(&name),
321315
false,
322316
);
323-
files.push(&format!("{directory}/moon.pkg.json"), moon_pkg.as_bytes());
317+
files.push(&format!("{directory}/moon.pkg"), moon_pkg.as_bytes());
324318
}
325319

326320
Ok(())
@@ -397,14 +391,14 @@ impl WorldGenerator for MoonBit {
397391
&format!("{directory}/ffi_import.mbt"),
398392
indent(&ffi).as_bytes(),
399393
);
400-
// moon.pkg.json
394+
// moon.pkg
401395
let mut moon_pkg = Source::default();
402396
self.write_moon_pkg(
403397
&mut moon_pkg,
404398
self.pkg_resolver.package_import.get(&name),
405399
false,
406400
);
407-
files.push(&format!("{directory}/moon.pkg.json"), moon_pkg.as_bytes());
401+
files.push(&format!("{directory}/moon.pkg"), moon_pkg.as_bytes());
408402
Ok(())
409403
}
410404

@@ -454,14 +448,14 @@ impl WorldGenerator for MoonBit {
454448
files.push(&format!("{directory}/top.mbt"), indent(&src).as_bytes());
455449

456450
if !self.opts.ignore_stub {
457-
// moon.pkg.json
451+
// moon.pkg
458452
let mut moon_pkg = Source::default();
459453
self.write_moon_pkg(
460454
&mut moon_pkg,
461455
self.pkg_resolver.package_import.get(&name),
462456
false,
463457
);
464-
files.push(&format!("{directory}/moon.pkg.json"), moon_pkg.as_bytes());
458+
files.push(&format!("{directory}/moon.pkg"), moon_pkg.as_bytes());
465459
}
466460

467461
// FFI
@@ -508,14 +502,14 @@ impl WorldGenerator for MoonBit {
508502
files.push(&format!("{directory}/top.mbt"), indent(&src).as_bytes());
509503

510504
if !self.opts.ignore_stub {
511-
// moon.pkg.json
505+
// moon.pkg
512506
let mut moon_pkg = Source::default();
513507
self.write_moon_pkg(
514508
&mut moon_pkg,
515509
self.pkg_resolver.package_import.get(&name),
516510
false,
517511
);
518-
files.push(&format!("{directory}/moon.pkg.json"), moon_pkg.as_bytes());
512+
files.push(&format!("{directory}/moon.pkg"), moon_pkg.as_bytes());
519513
}
520514

521515
// FFI
@@ -544,10 +538,10 @@ impl WorldGenerator for MoonBit {
544538
let mut body = Source::default();
545539
uwriteln!(
546540
&mut body,
547-
"{{ \"name\": \"{}\", \"preferred-target\": \"wasm\" }}",
541+
"name = {:?}\n\npreferred_target = \"wasm\"",
548542
self.project_name
549543
);
550-
files.push("moon.mod.json", body.as_bytes());
544+
files.push("moon.mod", body.as_bytes());
551545
}
552546

553547
// Export project entry point
@@ -574,8 +568,8 @@ impl WorldGenerator for MoonBit {
574568
true,
575569
);
576570
files.push(
577-
&format!("{}/moon.pkg.json", self.opts.r#gen_dir),
578-
indent(&moon_pkg).as_bytes(),
571+
&format!("{}/moon.pkg", self.opts.r#gen_dir),
572+
moon_pkg.as_bytes(),
579573
);
580574

581575
Ok(())
@@ -3252,8 +3246,11 @@ mod tests {
32523246
.iter()
32533247
.all(|(name, _)| !name.starts_with("async-core/async_"))
32543248
);
3255-
let package = file(&files, "gen/moon.pkg.json");
3256-
assert!(!package.contains("supported-targets"), "{package}");
3249+
let package = file(&files, "gen/moon.pkg");
3250+
assert!(!package.contains("supported_targets"), "{package}");
3251+
let module = file(&files, "moon.mod");
3252+
assert!(module.contains("preferred_target = \"wasm\""), "{module}");
3253+
assert!(files.iter().all(|(name, _)| !name.ends_with(".json")));
32573254
}
32583255

32593256
#[test]
@@ -3352,17 +3349,17 @@ mod tests {
33523349

33533350
let source = file(&files, "interface/a/b/types/top.mbt");
33543351
assert!(source.contains("@async-core.Future[UInt]"), "{source}");
3355-
let package = file(&files, "interface/a/b/types/moon.pkg.json");
3352+
let package = file(&files, "interface/a/b/types/moon.pkg");
33563353
assert!(
3357-
package.contains("\"supported-targets\": \"+wasm\""),
3354+
package.contains("supported_targets = \"wasm\""),
33583355
"{package}"
33593356
);
3360-
let link_package = file(&files, "gen/moon.pkg.json");
3357+
let link_package = file(&files, "gen/moon.pkg");
33613358
assert!(
3362-
link_package.contains("\"supported-targets\": \"+wasm\""),
3359+
link_package.contains("supported_targets = \"wasm\""),
33633360
"{link_package}"
33643361
);
3365-
file(&files, "async-core/moon.pkg.json");
3362+
file(&files, "async-core/moon.pkg");
33663363
file(&files, "async-core/async_trait.mbt");
33673364
}
33683365

‎crates/test/src/moonbit.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ impl LanguageMethods for MoonBit {
5151
let dest_path = compile.bindings_dir.join(&config.path);
5252
std::fs::copy(src_path, dest_path)?;
5353

54-
// Write the moon.pkg.json if provided
54+
// Write the moon.pkg if provided
5555
if let Some(pkg_config) = config.pkg_config {
5656
let dest_path = compile
5757
.bindings_dir
5858
.join(&config.path)
5959
.parent()
6060
.unwrap()
61-
.join("moon.pkg.json");
61+
.join("moon.pkg");
6262
std::fs::write(dest_path, pkg_config)?;
6363
}
6464
}

‎tests/runtime/list-in-variant/runner.mbt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//@ [lang]
22
//@ path = 'gen/world/runner/stub.mbt'
3-
//@ pkg_config = """{ "import": ["test/list-in-variant/interface/test/list-in-variant/to-test"] }"""
3+
//@ pkg_config = '''
4+
//@ import {
5+
//@ "test/list-in-variant/interface/test/list-in-variant/to-test",
6+
//@ }
7+
//@ '''
48

59
///|
610
pub fn run() -> Unit {

‎tests/runtime/moonbit/async-import-cancel/runner.mbt‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
//@ wasmtime-flags = '-Wcomponent-model-async'
22
//@ [lang]
33
//@ path = 'gen/world/runner/stub.mbt'
4-
//@ pkg_config = """{ "supported-targets": "+wasm", "import": [{ "path": "test/async-import-cancel/async-core", "alias": "async-core" }, { "path": "test/async-import-cancel/interface/test/async-import-cancel/pending", "alias": "pending" }, { "path": "test/async-import-cancel/interface/test/async-import-cancel/gate-control", "alias": "gate-control" }] }"""
4+
//@ pkg_config = '''
5+
//@ import {
6+
//@ "test/async-import-cancel/async-core" @async-core,
7+
//@ "test/async-import-cancel/interface/test/async-import-cancel/pending" @pending,
8+
//@ "test/async-import-cancel/interface/test/async-import-cancel/gate-control" @gate-control,
9+
//@ }
10+
//@
11+
//@ supported_targets = "wasm"
12+
//@ '''
513

614
///|
715
async fn settle_leaf_counts(expected_drops : UInt) -> Unit {

‎tests/runtime/moonbit/concurrent-export-isolation/runner.mbt‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
//@ wasmtime-flags = '-Wcomponent-model-async'
22
//@ [lang]
33
//@ path = 'gen/world/runner/stub.mbt'
4-
//@ pkg_config = """{ "warn-list": "-44", "supported-targets": "+wasm", "import": [{ "path": "test/moonbit-concurrent-export-isolation/async-core", "alias": "async-core" }, { "path": "test/moonbit-concurrent-export-isolation/interface/test/moonbit-concurrent-export-isolation/operations", "alias": "operations" }] }"""
4+
//@ pkg_config = '''
5+
//@ import {
6+
//@ "test/moonbit-concurrent-export-isolation/async-core" @async-core,
7+
//@ "test/moonbit-concurrent-export-isolation/interface/test/moonbit-concurrent-export-isolation/operations" @operations,
8+
//@ }
9+
//@
10+
//@ warnings = "-44"
11+
//@
12+
//@ supported_targets = "wasm"
13+
//@ '''
514

615
///|
716
pub async fn run(background_group : @async-core.TaskGroup[Unit]) -> Unit {

‎tests/runtime/moonbit/http-background-hook/runner.mbt‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
//@ wasmtime-flags = '-Wcomponent-model-async -S p3 -S http'
22
//@ [lang]
33
//@ path = 'gen/world/runner/stub.mbt'
4-
//@ pkg_config = """{ "warn-list": "-44", "supported-targets": "+wasm", "import": [{ "path": "example/background-hook/async-core", "alias": "async-core" }, { "path": "example/background-hook/interface/wasi/http/handler", "alias": "handler" }, { "path": "example/background-hook/interface/wasi/http/types", "alias": "types" }] }"""
4+
//@ pkg_config = '''
5+
//@ import {
6+
//@ "example/background-hook/async-core" @async-core,
7+
//@ "example/background-hook/interface/wasi/http/handler" @handler,
8+
//@ "example/background-hook/interface/wasi/http/types" @types,
9+
//@ }
10+
//@
11+
//@ warnings = "-44"
12+
//@
13+
//@ supported_targets = "wasm"
14+
//@ '''
515

616
///|
717
async fn run_hook(payload : Bytes, expected_error : String?) -> Unit {

0 commit comments

Comments
 (0)