Skip to content

Commit

Permalink
Merge pull request #13 from moonbitlang/support-more-wasm1-memory-config
Browse files Browse the repository at this point in the history
Support more wasm1 memory config
  • Loading branch information
lijunchen authored Jul 16, 2024
2 parents a6dd704 + a7ac175 commit 7f1980d
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 5 deletions.
2 changes: 2 additions & 0 deletions crates/moon/tests/test_cases/import_memory.in/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.mooncakes/
1 change: 1 addition & 0 deletions crates/moon/tests/test_cases/import_memory.in/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# username/hello
3 changes: 3 additions & 0 deletions crates/moon/tests/test_cases/import_memory.in/lib/hello.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn hello() -> String {
"Hello, world!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test "hello" {
if hello() != "Hello, world!" {
raise "hello() != \"Hello, world!\""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions crates/moon/tests/test_cases/import_memory.in/main/main.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main {
println(@lib.hello())
}
15 changes: 15 additions & 0 deletions crates/moon/tests/test_cases/import_memory.in/main/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"is-main": true,
"import": [
"username/hello/lib"
],
"link": {
"wasm": {
"heap-start-address": 65536,
"import-memory": {
"module": "xxx",
"name": "yyy"
}
}
}
}
9 changes: 9 additions & 0 deletions crates/moon/tests/test_cases/import_memory.in/moon.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "username/hello",
"version": "0.1.0",
"readme": "README.md",
"repository": "",
"license": "",
"keywords": [],
"description": ""
}
23 changes: 23 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"#]],
);
}
35 changes: 35 additions & 0 deletions crates/moonbuild/src/gen/gen_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>> = match moonc_opt.link_opt.target_backend {
moonutil::common::TargetBackend::Wasm => item
.link
Expand Down Expand Up @@ -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
Expand Down
37 changes: 32 additions & 5 deletions crates/moonutil/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub enum PkgJSONImportItem {
#[serde(untagged)]
pub enum BoolOrLink {
Bool(bool),
Link(Link),
Link(Box<Link>),
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -145,7 +145,34 @@ pub struct MoonPkgJSON {
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LinkConfig {
pub struct ImportMemory {
pub module: String,
pub name: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WasmLinkConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub exports: Option<Vec<String>>,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "heap-start-address")]
pub heap_start_address: Option<u32>,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "import-memory")]
pub import_memory: Option<ImportMemory>,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "export-memory-name")]
pub export_memory_name: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<Vec<String>>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WasmGcLinkConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub exports: Option<Vec<String>>,

Expand Down Expand Up @@ -191,11 +218,11 @@ impl JsFormat {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Link {
#[serde(skip_serializing_if = "Option::is_none")]
pub wasm: Option<LinkConfig>,
pub wasm: Option<WasmLinkConfig>,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "wasm-gc")]
pub wasm_gc: Option<LinkConfig>,
pub wasm_gc: Option<WasmGcLinkConfig>,

#[serde(skip_serializing_if = "Option::is_none")]
pub js: Option<JsLinkConfig>,
Expand Down Expand Up @@ -369,7 +396,7 @@ pub fn convert_pkg_json_to_package(j: MoonPkgJSON) -> anyhow::Result<MoonPkg> {
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,
Expand Down

0 comments on commit 7f1980d

Please sign in to comment.