Skip to content

Commit 4bf2d7e

Browse files
fix: logic for stubbing outgoing http and fetch-event (#268)
* fix: logic for stubbing outgoing http and fetch-event This commit fixes one bug and refactors the code to be more granular in the case of differing feature sets. The reality is more nuanced -- as we must stub wasi:http/types differently depending on whether fetch-event is separately enabled. * fix: improve comment explaining stubbing Co-authored-by: Till Schneidereit <till@tillschneidereit.net> --------- Co-authored-by: Till Schneidereit <till@tillschneidereit.net>
1 parent e5a38cd commit 4bf2d7e

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,25 @@ pub fn stub_wasi(
139139
stub_stdio(&mut module)?;
140140
}
141141

142-
if !features.contains(&Feature::Http) && !features.contains(&Feature::FetchEvent) {
143-
stub_http(&mut module)?;
142+
match (
143+
features.contains(&Feature::Http),
144+
features.contains(&Feature::FetchEvent),
145+
) {
146+
// If both are disabled, then disable all HTTP related imports
147+
(false, false) => {
148+
stub_http_types(&mut module)?;
149+
stub_http_outgoing(&mut module)?;
150+
}
151+
// If HTTP is disabled but fetch-event is enabled we want to stub only the `wasi:http/outgoing-handler`
152+
// and leave the rest of `wasi:http/types` in place for StarlingMonkey's implementation of `FetchEvent` to use.
153+
//
154+
// Note that we cannot *know* that the user will make use of fetch-event, but we must be prepared
155+
// for it, as the feature is enabled.
156+
(false, true) => {
157+
stub_http_outgoing(&mut module)?;
158+
}
159+
// For all other cases we can avoid stubbing
160+
_ => {}
144161
}
145162

146163
let has_io = features.contains(&Feature::Clocks)
@@ -380,7 +397,17 @@ fn stub_stdio(module: &mut Module) -> Result<()> {
380397
Ok(())
381398
}
382399

383-
fn stub_http(module: &mut Module) -> Result<()> {
400+
fn stub_http_outgoing(module: &mut Module) -> Result<()> {
401+
stub_wasi_imports(
402+
module,
403+
"wasi:http/outgoing-handler",
404+
"handle",
405+
unreachable_stub,
406+
)?;
407+
Ok(())
408+
}
409+
410+
fn stub_http_types(module: &mut Module) -> Result<()> {
384411
stub_wasi_imports(
385412
module,
386413
"wasi:http/types",
@@ -753,12 +780,6 @@ fn stub_http(module: &mut Module) -> Result<()> {
753780
"[method]future-incoming-response.get",
754781
unreachable_stub,
755782
)?;
756-
stub_wasi_imports(
757-
module,
758-
"wasi:http/outgoing-handler",
759-
"handle",
760-
unreachable_stub,
761-
)?;
762783
Ok(())
763784
}
764785

0 commit comments

Comments
 (0)