Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,35 @@ pub(super) fn detect_optional_feature_usage(
ctx.uses_fetch = true;
}

// Robust fallback for fetch detection. The ~30 `ctx.uses_fetch` set-sites in
// perry-hir lowering are shape-specific; a minified bundle's `new Headers()`
// / `new Request()` / `fetch(...)` can reach codegen as `Expr::New { class_name:
// "Headers" }` / a `Fetch*` variant (codegen dispatches those to
// `js_headers_new` / `js_request_new` / `js_fetch_with_options`) WITHOUT having
// hit any set-site, leaving `hir_module.uses_fetch` false. The perry-stdlib
// `web-fetch` feature is then stripped, only the no-op runtime stub remains, and
// it returns garbage the caller derefs -> SIGSEGV in `js_object_get_class_id`.
// Mirror the EventEmitter / URL token-grep below: scan the final HIR for the
// fetch web-platform constructors + the dedicated fetch call variants. Over-
// matching only over-links `web-fetch` (a size cost); the rule is zero false
// negatives.
if !ctx.uses_fetch {
let hir_debug: String = format!("{:?}{:?}", &hir_module.init, &hir_module.functions);
if hir_debug.contains("class_name: \"Headers\"")
|| hir_debug.contains("class_name: \"Request\"")
|| hir_debug.contains("class_name: \"Response\"")
|| hir_debug.contains("class_name: \"FormData\"")
|| hir_debug.contains("class_name: \"Blob\"")
|| hir_debug.contains("class_name: \"File\"")
|| hir_debug.contains("FetchWithOptions")
|| hir_debug.contains("FetchGetWithAuth")
|| hir_debug.contains("FetchPostWithAuth")
{
ctx.needs_stdlib = true;
ctx.uses_fetch = true;
}
}

// Issue #76 — auto-link the wasmi host runtime when any module
// references `WebAssembly.*`. Without this the user has to remember
// `--enable-wasm-runtime`; with it the flag is only needed when they
Expand Down
11 changes: 11 additions & 0 deletions crates/perry/src/commands/compile/optimized_libs/freshness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ pub(crate) fn auto_optimized_cross_features(
if ctx.uses_dgram {
cross_features.push("perry-runtime/mod-dgram".to_string());
}
// Compile OUT perry-runtime's no-op fetch stubs (`js_fetch_with_options` /
// `js_headers_new` / `js_request_new`, gated `#[cfg(not(feature =
// "external-fetch-symbols"))]`) whenever the program uses fetch — perry-stdlib's
// `web-fetch` then supplies the REAL impls. Without this both the stub
// (perry-runtime) and the real (perry-stdlib) symbols exist; on the fresh build
// path the stub has won the link and returned garbage the caller derefs ->
// SIGSEGV in `js_object_get_class_id`. Enabling the feature drops the stubs from
// libperry_runtime.a so only the real symbols remain.
if ctx.uses_fetch {
cross_features.push("perry-runtime/external-fetch-symbols".to_string());
}
cross_features
}

Expand Down
Loading