From cc75523eab0e72af7fdf83a26b75383475343fa7 Mon Sep 17 00:00:00 2001 From: Li Junchen Date: Tue, 16 Jul 2024 16:14:57 +0800 Subject: [PATCH 1/5] internal: separate LinkConfig to WasmLinkConfig and WasmGcLinkConfig --- crates/moonutil/src/package.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/moonutil/src/package.rs b/crates/moonutil/src/package.rs index e020406e..73737bea 100644 --- a/crates/moonutil/src/package.rs +++ b/crates/moonutil/src/package.rs @@ -145,7 +145,20 @@ pub struct MoonPkgJSON { } #[derive(Debug, Serialize, Deserialize, Clone)] -pub struct LinkConfig { +pub struct WasmLinkConfig { + #[serde(skip_serializing_if = "Option::is_none")] + pub exports: Option>, + + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(rename = "export-memory-name")] + pub export_memory_name: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub flags: Option>, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct WasmGcLinkConfig { #[serde(skip_serializing_if = "Option::is_none")] pub exports: Option>, @@ -191,11 +204,11 @@ impl JsFormat { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Link { #[serde(skip_serializing_if = "Option::is_none")] - pub wasm: Option, + pub wasm: Option, #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "wasm-gc")] - pub wasm_gc: Option, + pub wasm_gc: Option, #[serde(skip_serializing_if = "Option::is_none")] pub js: Option, From d0e94ee148af28460e6407df1f4f506619bfef1c Mon Sep 17 00:00:00 2001 From: Li Junchen Date: Tue, 16 Jul 2024 16:18:32 +0800 Subject: [PATCH 2/5] feat: add more memory config in WasmLinkConfig --- crates/moonutil/src/package.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/moonutil/src/package.rs b/crates/moonutil/src/package.rs index 73737bea..aee7cda4 100644 --- a/crates/moonutil/src/package.rs +++ b/crates/moonutil/src/package.rs @@ -144,11 +144,25 @@ pub struct MoonPkgJSON { pub alert_list: Option, } +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ImportMemory { + module: String, + name: String, +} + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct WasmLinkConfig { #[serde(skip_serializing_if = "Option::is_none")] pub exports: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(rename = "heap-start-address")] + pub heap_start_address: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(rename = "import-memory")] + pub import_memory: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "export-memory-name")] pub export_memory_name: Option, From 4d3d7f207a3a442dbbe90f3d19692ea5ae48b9b1 Mon Sep 17 00:00:00 2001 From: Li Junchen Date: Tue, 16 Jul 2024 16:40:04 +0800 Subject: [PATCH 3/5] feat: emit memory related arguments --- crates/moonbuild/src/gen/gen_build.rs | 35 +++++++++++++++++++++++++++ crates/moonutil/src/package.rs | 4 +-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/moonbuild/src/gen/gen_build.rs b/crates/moonbuild/src/gen/gen_build.rs index 7842ead3..309457ae 100644 --- a/crates/moonbuild/src/gen/gen_build.rs +++ b/crates/moonbuild/src/gen/gen_build.rs @@ -333,6 +333,26 @@ pub fn gen_link_command( moonutil::common::TargetBackend::Js => None, }; + let heap_start_address = match moonc_opt.link_opt.target_backend { + moonutil::common::TargetBackend::Wasm => item + .link + .as_ref() + .and_then(|l| l.wasm.as_ref()) + .and_then(|w| w.heap_start_address.as_ref()), + moonutil::common::TargetBackend::WasmGC => None, + moonutil::common::TargetBackend::Js => None, + }; + + let import_memory = match moonc_opt.link_opt.target_backend { + moonutil::common::TargetBackend::Wasm => item + .link + .as_ref() + .and_then(|l| l.wasm.as_ref()) + .and_then(|w| w.import_memory.as_ref()), + moonutil::common::TargetBackend::WasmGC => None, + moonutil::common::TargetBackend::Js => None, + }; + let link_flags: Option> = match moonc_opt.link_opt.target_backend { moonutil::common::TargetBackend::Wasm => item .link @@ -399,6 +419,21 @@ pub fn gen_link_command( export_memory_name.unwrap().to_string(), ] }) + .lazy_args_with_cond(import_memory.is_some(), || { + let im = import_memory.unwrap(); + vec![ + "-import-memory-module".to_string(), + im.module.clone(), + "-import-memory-name".to_string(), + im.name.clone(), + ] + }) + .lazy_args_with_cond(heap_start_address.is_some(), || { + vec![ + "-heap-start-address".to_string(), + heap_start_address.unwrap().to_string(), + ] + }) .lazy_args_with_cond(link_flags.is_some(), || link_flags.unwrap()) .lazy_args_with_cond( moonc_opt.link_opt.target_backend == moonutil::common::TargetBackend::Js diff --git a/crates/moonutil/src/package.rs b/crates/moonutil/src/package.rs index aee7cda4..d8b09a6c 100644 --- a/crates/moonutil/src/package.rs +++ b/crates/moonutil/src/package.rs @@ -146,8 +146,8 @@ pub struct MoonPkgJSON { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ImportMemory { - module: String, - name: String, + pub module: String, + pub name: String, } #[derive(Debug, Serialize, Deserialize, Clone)] From e7d36db23ef28c382af90b5dd08035669e84a16d Mon Sep 17 00:00:00 2001 From: Li Junchen Date: Tue, 16 Jul 2024 16:46:28 +0800 Subject: [PATCH 4/5] add tests --- .../test_cases/import_memory.in/.gitignore | 2 ++ .../test_cases/import_memory.in/README.md | 1 + .../test_cases/import_memory.in/lib/hello.mbt | 3 +++ .../import_memory.in/lib/hello_test.mbt | 5 ++++ .../import_memory.in/lib/moon.pkg.json | 1 + .../test_cases/import_memory.in/main/main.mbt | 3 +++ .../import_memory.in/main/moon.pkg.json | 15 ++++++++++++ .../test_cases/import_memory.in/moon.mod.json | 9 ++++++++ crates/moon/tests/test_cases/mod.rs | 23 +++++++++++++++++++ 9 files changed, 62 insertions(+) create mode 100644 crates/moon/tests/test_cases/import_memory.in/.gitignore create mode 100644 crates/moon/tests/test_cases/import_memory.in/README.md create mode 100644 crates/moon/tests/test_cases/import_memory.in/lib/hello.mbt create mode 100644 crates/moon/tests/test_cases/import_memory.in/lib/hello_test.mbt create mode 100644 crates/moon/tests/test_cases/import_memory.in/lib/moon.pkg.json create mode 100644 crates/moon/tests/test_cases/import_memory.in/main/main.mbt create mode 100644 crates/moon/tests/test_cases/import_memory.in/main/moon.pkg.json create mode 100644 crates/moon/tests/test_cases/import_memory.in/moon.mod.json diff --git a/crates/moon/tests/test_cases/import_memory.in/.gitignore b/crates/moon/tests/test_cases/import_memory.in/.gitignore new file mode 100644 index 00000000..b1283a74 --- /dev/null +++ b/crates/moon/tests/test_cases/import_memory.in/.gitignore @@ -0,0 +1,2 @@ +target/ +.mooncakes/ diff --git a/crates/moon/tests/test_cases/import_memory.in/README.md b/crates/moon/tests/test_cases/import_memory.in/README.md new file mode 100644 index 00000000..ae00983f --- /dev/null +++ b/crates/moon/tests/test_cases/import_memory.in/README.md @@ -0,0 +1 @@ +# username/hello \ No newline at end of file diff --git a/crates/moon/tests/test_cases/import_memory.in/lib/hello.mbt b/crates/moon/tests/test_cases/import_memory.in/lib/hello.mbt new file mode 100644 index 00000000..9012592a --- /dev/null +++ b/crates/moon/tests/test_cases/import_memory.in/lib/hello.mbt @@ -0,0 +1,3 @@ +pub fn hello() -> String { + "Hello, world!" +} diff --git a/crates/moon/tests/test_cases/import_memory.in/lib/hello_test.mbt b/crates/moon/tests/test_cases/import_memory.in/lib/hello_test.mbt new file mode 100644 index 00000000..85944a2c --- /dev/null +++ b/crates/moon/tests/test_cases/import_memory.in/lib/hello_test.mbt @@ -0,0 +1,5 @@ +test "hello" { + if hello() != "Hello, world!" { + raise "hello() != \"Hello, world!\"" + } +} diff --git a/crates/moon/tests/test_cases/import_memory.in/lib/moon.pkg.json b/crates/moon/tests/test_cases/import_memory.in/lib/moon.pkg.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/crates/moon/tests/test_cases/import_memory.in/lib/moon.pkg.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/crates/moon/tests/test_cases/import_memory.in/main/main.mbt b/crates/moon/tests/test_cases/import_memory.in/main/main.mbt new file mode 100644 index 00000000..d25cb935 --- /dev/null +++ b/crates/moon/tests/test_cases/import_memory.in/main/main.mbt @@ -0,0 +1,3 @@ +fn main { + println(@lib.hello()) +} diff --git a/crates/moon/tests/test_cases/import_memory.in/main/moon.pkg.json b/crates/moon/tests/test_cases/import_memory.in/main/moon.pkg.json new file mode 100644 index 00000000..5d1b3e0f --- /dev/null +++ b/crates/moon/tests/test_cases/import_memory.in/main/moon.pkg.json @@ -0,0 +1,15 @@ +{ + "is-main": true, + "import": [ + "username/hello/lib" + ], + "link": { + "wasm": { + "heap-start-address": 65536, + "import-memory": { + "module": "xxx", + "name": "yyy" + } + } + } +} \ No newline at end of file diff --git a/crates/moon/tests/test_cases/import_memory.in/moon.mod.json b/crates/moon/tests/test_cases/import_memory.in/moon.mod.json new file mode 100644 index 00000000..80554ae2 --- /dev/null +++ b/crates/moon/tests/test_cases/import_memory.in/moon.mod.json @@ -0,0 +1,9 @@ +{ + "name": "username/hello", + "version": "0.1.0", + "readme": "README.md", + "repository": "", + "license": "", + "keywords": [], + "description": "" +} \ No newline at end of file diff --git a/crates/moon/tests/test_cases/mod.rs b/crates/moon/tests/test_cases/mod.rs index e9286ed5..6fd537b0 100644 --- a/crates/moon/tests/test_cases/mod.rs +++ b/crates/moon/tests/test_cases/mod.rs @@ -4388,3 +4388,26 @@ fn test_blackbox_failed() { assert!(output.contains("Value _private_hello not found in package \"A\"")); assert!(output.contains("Package \"C\" not found in the loaded packages.")); } + +#[test] +fn test_import_memory_and_heap_start() { + let dir = TestDir::new("import_memory.in"); + check( + &get_stdout_with_args_and_replace_dir( + &dir, + [ + "build", + "--target", + "wasm", + "--dry-run", + "--sort-input", + "--nostd", + ], + ), + expect![[r#" + moonc build-package ./lib/hello.mbt -o ./target/wasm/release/build/lib/lib.core -pkg username/hello/lib -pkg-sources username/hello/lib:./lib -target wasm + moonc build-package ./main/main.mbt -o ./target/wasm/release/build/main/main.core -pkg username/hello/main -is-main -i ./target/wasm/release/build/lib/lib.mi:lib -pkg-sources username/hello/main:./main -target wasm + moonc link-core ./target/wasm/release/build/lib/lib.core ./target/wasm/release/build/main/main.core -main username/hello/main -o ./target/wasm/release/build/main/main.wasm -pkg-sources username/hello/lib:./lib -pkg-sources username/hello/main:./main -target wasm -import-memory-module xxx -import-memory-name yyy -heap-start-address 65536 + "#]], + ); +} From a7ac175e264e14fc4d831b338523bb1853062859 Mon Sep 17 00:00:00 2001 From: Li Junchen Date: Tue, 16 Jul 2024 17:14:35 +0800 Subject: [PATCH 5/5] internal: fix cargo clippy --- crates/moonutil/src/package.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/moonutil/src/package.rs b/crates/moonutil/src/package.rs index d8b09a6c..7a51ef1c 100644 --- a/crates/moonutil/src/package.rs +++ b/crates/moonutil/src/package.rs @@ -103,7 +103,7 @@ pub enum PkgJSONImportItem { #[serde(untagged)] pub enum BoolOrLink { Bool(bool), - Link(Link), + Link(Box), } #[derive(Debug, Serialize, Deserialize)] @@ -396,7 +396,7 @@ pub fn convert_pkg_json_to_package(j: MoonPkgJSON) -> anyhow::Result { link: match j.link { None => None, Some(BoolOrLink::Bool(_)) => None, - Some(BoolOrLink::Link(l)) => Some(l), + Some(BoolOrLink::Link(l)) => Some(*l), }, warn_list: j.warn_list, alert_list: j.alert_list,