Skip to content

Commit 2047fa4

Browse files
author
Guy Bedford
authored
Emit component name in core Wasm file names (#408)
1 parent f22e230 commit 2047fa4

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

crates/bindgen-core/src/component.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ pub fn generate(
6262
// Insert all core wasm modules into the generated `Files` which will
6363
// end up getting used in the `generate_instantiate` method.
6464
for (i, module) in modules.iter() {
65-
let i = i.as_u32();
66-
let name = format!("module{i}.wasm");
67-
files.push(&name, module.wasm);
65+
files.push(&gen.core_file_name(name, i), module.wasm);
6866
}
6967

7068
// With all that prep work delegate to `WorldGenerator::generate` here
@@ -100,5 +98,14 @@ pub trait ComponentGenerator: WorldGenerator {
10098
interfaces: &ComponentInterfaces,
10199
);
102100

101+
fn core_file_name(&mut self, name: &str, idx: StaticModuleIndex) -> String {
102+
let i_str = if idx.as_u32() == 0 {
103+
String::from("")
104+
} else {
105+
(idx.as_u32() + 1).to_string()
106+
};
107+
format!("{}.core{i_str}.wasm", name)
108+
}
109+
103110
fn finish_component(&mut self, name: &str, files: &mut Files);
104111
}

crates/gen-host-js/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ impl ComponentGenerator for Js {
220220
// bindings is the actual `instantiate` method itself, created by this
221221
// structure.
222222
let mut instantiator = Instantiator {
223+
name,
223224
src: Source::default(),
224225
gen: self,
225226
modules,
@@ -434,7 +435,7 @@ impl Js {
434435
_fs = _fs || await import('fs/promises');
435436
return WebAssembly.compile(await _fs.readFile(url));
436437
}
437-
return fetch(url).then(WebAssembly.compile);
438+
return fetch(url).then(WebAssembly.compileStreaming);
438439
}
439440
")
440441
} else {
@@ -684,6 +685,7 @@ impl Js {
684685
///
685686
/// This is the main structure for parsing the output of Wasmtime.
686687
struct Instantiator<'a> {
688+
name: &'a str,
687689
src: Source,
688690
gen: &'a mut Js,
689691
modules: &'a PrimaryMap<StaticModuleIndex, ModuleTranslation<'a>>,
@@ -707,6 +709,7 @@ impl Instantiator<'_> {
707709

708710
// Setup the compilation promises
709711
let mut first = true;
712+
let mut multiple = false;
710713
for init in self.component.initializers.iter() {
711714
if let GlobalInitializer::InstantiateModule(InstantiateModule::Static(idx, _)) = init {
712715
// Get the compiled WebAssembly.Module objects in parallel
@@ -715,9 +718,12 @@ impl Instantiator<'_> {
715718
self.src.js.push_str("\n");
716719
}
717720
first = false;
721+
} else {
722+
multiple = true;
718723
}
724+
719725
let local_name = format!("module{}", idx.as_u32());
720-
let name = format!("module{}.wasm", idx.as_u32());
726+
let name = self.gen.core_file_name(&self.name, *idx);
721727
if self.gen.opts.instantiation {
722728
uwrite!(
723729
self.src.js,
@@ -735,7 +741,7 @@ impl Instantiator<'_> {
735741

736742
// To avoid uncaught promise rejection errors, we attach an intermediate
737743
// Promise.all with a rejection handler, if there are multiple promises.
738-
if first == false {
744+
if multiple {
739745
first = true;
740746
self.src.js.push_str("Promise.all([");
741747
for init in self.component.initializers.iter() {

crates/gen-host-wasmtime-py/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ impl ComponentGenerator for WasmtimePy {
452452
);
453453
self.init.indent();
454454
let mut i = Instantiator {
455+
name,
455456
gen: self,
456457
modules,
457458
component,
@@ -496,6 +497,7 @@ impl ComponentGenerator for WasmtimePy {
496497
}
497498

498499
struct Instantiator<'a> {
500+
name: &'a str,
499501
gen: &'a mut WasmtimePy,
500502
modules: &'a PrimaryMap<StaticModuleIndex, ModuleTranslation<'a>>,
501503
instances: PrimaryMap<RuntimeInstanceIndex, StaticModuleIndex>,
@@ -578,12 +580,13 @@ impl<'a> Instantiator<'a> {
578580

579581
fn instantiate_static_module(&mut self, idx: StaticModuleIndex, args: &[CoreDef]) {
580582
let i = self.instances.push(idx);
583+
let core_file_name = self.gen.core_file_name(&self.name, idx);
581584
self.gen.init.pyimport("os", None);
582585

583586
uwriteln!(
584587
self.gen.init,
585-
"path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'module{}.wasm')",
586-
idx.as_u32(),
588+
"path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '{}')",
589+
core_file_name,
587590
);
588591
uwriteln!(
589592
self.gen.init,

0 commit comments

Comments
 (0)