Skip to content

Commit

Permalink
Merge pull request #14 from moonbitlang/refactor-runtest
Browse files Browse the repository at this point in the history
clean up
  • Loading branch information
lijunchen authored Jul 16, 2024
2 parents 7f1980d + 4742a09 commit d41c82c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 109 deletions.
132 changes: 62 additions & 70 deletions crates/moonbuild/src/gen/gen_build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{bail, Ok};
use moonutil::common::TargetBackend::*;
use moonutil::module::ModuleDB;
use moonutil::package::{JsFormat, Package};

Expand All @@ -7,7 +8,7 @@ use crate::gen::MiAlias;
use std::path::{Path, PathBuf};
use std::rc::Rc;

use moonutil::common::{MoonbuildOpt, MooncOpt, MOONBITLANG_CORE, MOON_PKG_JSON};
use moonutil::common::{MoonbuildOpt, MooncOpt, TargetBackend, MOONBITLANG_CORE, MOON_PKG_JSON};
use n2::graph::{self as n2graph, Build, BuildIns, BuildOuts, FileLoc};
use n2::load::State;
use n2::smallmap::SmallMap;
Expand Down Expand Up @@ -299,75 +300,11 @@ pub fn gen_link_command(

let mut build = Build::new(loc, ins, outs);

let exports = 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.exports.as_ref()),
moonutil::common::TargetBackend::WasmGC => item
.link
.as_ref()
.and_then(|l| l.wasm_gc.as_ref())
.and_then(|w| w.exports.as_ref()),
moonutil::common::TargetBackend::Js => item
.link
.as_ref()
.and_then(|l| l.js.as_ref())
.and_then(|w| w.exports.as_ref()),
};

let export_memory_name = 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.export_memory_name.as_ref())
.map(|s| s.to_string()),
moonutil::common::TargetBackend::WasmGC => item
.link
.as_ref()
.and_then(|l| l.wasm_gc.as_ref())
.and_then(|w| w.export_memory_name.as_ref())
.map(|s| s.to_string()),
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
.as_ref()
.and_then(|l| l.wasm.as_ref())
.and_then(|w| w.flags.as_ref())
.cloned(),
moonutil::common::TargetBackend::WasmGC => item
.link
.as_ref()
.and_then(|l| l.wasm_gc.as_ref())
.and_then(|w| w.flags.as_ref())
.cloned(),
moonutil::common::TargetBackend::Js => None,
};
let exports = item.exports(moonc_opt.link_opt.target_backend);
let export_memory_name = item.export_memory_name(moonc_opt.link_opt.target_backend);
let heap_start_address = item.heap_start_address(moonc_opt.link_opt.target_backend);
let import_memory = item.import_memory(moonc_opt.link_opt.target_backend);
let link_flags = item.link_flags(moonc_opt.link_opt.target_backend);

let command = CommandBuilder::new("moonc")
.arg("link-core")
Expand Down Expand Up @@ -492,3 +429,58 @@ pub fn gen_n2_build_state(
pools: SmallMap::default(),
})
}

#[rustfmt::skip]
impl LinkDepItem {
pub fn wasm_exports(&self) -> Option<&Vec<String>> { self.link.as_ref()?.wasm.as_ref()?.exports.as_ref() }
pub fn wasm_export_memory_name(&self) -> Option<&String> { self.link.as_ref()?.wasm.as_ref()?.export_memory_name.as_ref() }
pub fn wasm_import_memory(&self) -> Option<&moonutil::package::ImportMemory> { self.link.as_ref()?.wasm.as_ref()?.import_memory.as_ref() }
pub fn wasm_heap_start_address(&self) -> Option<&u32> { self.link.as_ref()?.wasm.as_ref()?.heap_start_address.as_ref() }
pub fn wasm_link_flags(&self) -> Option<&Vec<String>> { self.link.as_ref()?.wasm.as_ref()?.flags.as_ref() }

pub fn wasm_gc_exports(&self) -> Option<&Vec<String>> { self.link.as_ref()?.wasm_gc.as_ref()?.exports.as_ref() }
pub fn wasm_gc_export_memory_name(&self) -> Option<&String> { self.link.as_ref()?.wasm_gc.as_ref()?.export_memory_name.as_ref() }
pub fn wasm_gc_link_flags(&self) -> Option<&Vec<String>> { self.link.as_ref()?.wasm_gc.as_ref()?.flags.as_ref() }

pub fn js_exports(&self) -> Option<&Vec<String>> { self.link.as_ref()?.js.as_ref()?.exports.as_ref() }

pub fn exports(&self, b: TargetBackend) -> Option<&Vec<String>> {
match b {
Wasm => self.wasm_exports(),
WasmGC => self.wasm_gc_exports(),
Js => self.js_exports(),
}
}

pub fn export_memory_name(&self, b: TargetBackend) -> Option<&String> {
match b {
Wasm => self.wasm_export_memory_name(),
WasmGC => self.wasm_gc_export_memory_name(),
Js => None,
}
}

pub fn heap_start_address(&self, b: TargetBackend) -> Option<&u32> {
match b {
Wasm => self.wasm_heap_start_address(),
WasmGC => None,
Js => None,
}
}

pub fn import_memory(&self, b: TargetBackend) -> Option<&moonutil::package::ImportMemory> {
match b {
Wasm => self.wasm_import_memory(),
WasmGC => None,
Js => None,
}
}

pub fn link_flags(&self, b: TargetBackend) -> Option<Vec<String>> {
match b {
Wasm => self.wasm_link_flags().cloned(),
WasmGC => self.wasm_gc_link_flags().cloned(),
Js => None,
}
}
}
39 changes: 0 additions & 39 deletions crates/moonbuild/src/gen/gen_runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,38 +710,6 @@ pub fn gen_runtest_link_command(

let mut build = Build::new(loc, ins, outs);

let export_memory_name = 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.export_memory_name.as_ref())
.map(|s| s.to_string()),
moonutil::common::TargetBackend::WasmGC => item
.link
.as_ref()
.and_then(|l| l.wasm_gc.as_ref())
.and_then(|w| w.export_memory_name.as_ref())
.map(|s| s.to_string()),
moonutil::common::TargetBackend::Js => None,
};

let link_flags: Option<Vec<String>> = 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.flags.as_ref())
.cloned(),
moonutil::common::TargetBackend::WasmGC => item
.link
.as_ref()
.and_then(|l| l.wasm_gc.as_ref())
.and_then(|w| w.flags.as_ref())
.cloned(),
moonutil::common::TargetBackend::Js => None,
};

let command = CommandBuilder::new("moonc")
.arg("link-core")
.arg_with_cond(
Expand All @@ -756,13 +724,6 @@ pub fn gen_runtest_link_command(
.arg("-o")
.arg(&artifact_output_path)
.arg("-test-mode") // always passing -test-mode to allow recover from panic
.lazy_args_with_cond(export_memory_name.is_some(), || {
vec![
"-export-memory-name".to_string(),
export_memory_name.unwrap().to_string(),
]
})
.lazy_args_with_cond(link_flags.is_some(), || link_flags.unwrap())
.args_with_prefix_separator(
item.package_sources
.iter()
Expand Down

0 comments on commit d41c82c

Please sign in to comment.