Skip to content

Commit 2f62e7d

Browse files
feat: Support all wasi-0.2.x patch versions in the splicer (#197)
This way, we don't have to update ComponentizeJS for any future minor versions.
1 parent 15df525 commit 2f62e7d

File tree

1 file changed

+35
-23
lines changed
  • crates/spidermonkey-embedding-splicer/src

1 file changed

+35
-23
lines changed

crates/spidermonkey-embedding-splicer/src/splice.rs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use wasmparser::Operator;
1111

1212
use crate::*;
1313

14-
const WASI_VERSIONS: [&str; 4] = ["0.2.0", "0.2.1", "0.2.2", "0.2.3"];
15-
1614
//
1715
// Parses the Spidermonkey binary into section data for reserialization
1816
// into an output binary, and in the process:
@@ -46,27 +44,13 @@ pub fn splice(
4644

4745
// since StarlingMonkey implements CLI Run and incoming handler,
4846
// we override them only if the guest content exports those functions
49-
for wasi_version in WASI_VERSIONS {
50-
let import = format!("wasi:cli/run@{wasi_version}#run");
51-
if exports.iter().any(|(name, _)| *name == import) {
52-
if let Some(run) = module.exports.get_func_by_name(import) {
53-
let expt = module.exports.get_func_by_id(run).unwrap();
54-
module.exports.delete(expt);
55-
module.delete_func(run); // TODO: Look at the intended behaviour here. Need to pass function ID to delete from functions. Was Previously passing Exports ID
56-
}
57-
}
58-
}
59-
60-
for wasi_version in WASI_VERSIONS {
61-
let import = format!("wasi:http/incoming-handler@{wasi_version}#handle");
62-
if exports.iter().any(|(name, _)| *name == import) {
63-
if let Some(serve) = module.exports.get_func_by_name(import) {
64-
let expt = module.exports.get_func_by_id(serve).unwrap();
65-
module.exports.delete(expt);
66-
module.delete_func(serve); // TODO: Look at the intended behaviour here. Same as above comment
67-
}
68-
}
69-
}
47+
remove_if_exported_by_js(&mut module, &exports, "wasi:cli/run@0.2.", "#run");
48+
remove_if_exported_by_js(
49+
&mut module,
50+
&exports,
51+
"wasi:http/incoming-handler@0.2.",
52+
"#handle",
53+
);
7054

7155
// we reencode the WASI world component data, so strip it out from the
7256
// custom section
@@ -88,6 +72,34 @@ pub fn splice(
8872
Ok(module.encode())
8973
}
9074

75+
fn remove_if_exported_by_js(
76+
module: &mut Module,
77+
content_exports: &Vec<(String, CoreFn)>,
78+
name_start: &str,
79+
name_end: &str,
80+
) {
81+
let content_exports_run = content_exports
82+
.iter()
83+
.any(|(name, _)| name.starts_with(name_start) && name.ends_with(name_end));
84+
if content_exports_run {
85+
let exported_run_fn = module
86+
.exports
87+
.iter()
88+
.find(|export| export.name.starts_with(name_start) && export.name.ends_with(name_end))
89+
.unwrap();
90+
let export_id = module
91+
.exports
92+
.get_export_id_by_name(String::from(&exported_run_fn.name))
93+
.unwrap();
94+
let function_id = module
95+
.exports
96+
.get_func_by_name(String::from(&exported_run_fn.name))
97+
.unwrap();
98+
module.exports.delete(export_id);
99+
module.delete_func(function_id);
100+
}
101+
}
102+
91103
fn get_export_fid(module: &Module, expt_id: &ExportsID) -> FunctionID {
92104
let expt = module.exports.get_by_id(*expt_id).unwrap();
93105

0 commit comments

Comments
 (0)