Skip to content
Open
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
1 change: 1 addition & 0 deletions changelog.d/16005-wasm-global-exception-flag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- wasm: export mutable `__perry_has_exc` and `__perry_arg_sp` globals, replacing per-call `rt.has_exception` checks with `global.get` and factoring unguarded exception unwind helpers.
31 changes: 28 additions & 3 deletions crates/perry-codegen-wasm/src/emit/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,10 @@ impl WasmModuleEmitter {
// reads the counter.
let start_idx = user_func_idx;
let start_type = t_void;
let exc_prop_void_type = self.get_type_idx(vec![ValType::I32], vec![]);
let exc_prop_i64_type = self.get_type_idx(vec![ValType::I32], vec![ValType::I64]);
self.exc_propagate_void_fn = start_idx + 1;
self.exc_propagate_i64_fn = start_idx + 2;

// Issue #1071: build cross-module imported-variable → WASM global map.
// The HIR lowers `import { FOO } from './m'` value reads (where FOO is
Expand Down Expand Up @@ -1255,6 +1259,10 @@ impl WasmModuleEmitter {
// Add a NaN-safe temp global for mem_store_slot (Firefox canonicalizes locals)
self.nan_temp_global = self.num_globals;
self.num_globals += 1;
self.has_exc_global = self.num_globals;
self.num_globals += 1;
self.wasm_depth_global = self.num_globals;
self.num_globals += 1;

// Build the WASM module
let mut wasm_module = Module::new();
Expand Down Expand Up @@ -1382,6 +1390,8 @@ impl WasmModuleEmitter {
}
// _start
func_section.function(start_type);
func_section.function(exc_prop_void_type);
func_section.function(exc_prop_i64_type);
wasm_module.section(&func_section);

// --- Table section (for indirect calls / closures) ---
Expand Down Expand Up @@ -1480,15 +1490,22 @@ impl WasmModuleEmitter {
if self.num_globals > 0 {
let mut global_section = GlobalSection::new();
for g in 0..self.num_globals {
if g == self.nan_temp_global {
// Stack pointer for arg buffer (i32, initialized to 0x10000)
if g == self.nan_temp_global
|| g == self.has_exc_global
|| g == self.wasm_depth_global
{
let init = if g == self.nan_temp_global {
0x10000
} else {
0
};
global_section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&wasm_encoder::ConstExpr::i32_const(0x10000),
&wasm_encoder::ConstExpr::i32_const(init),
);
} else {
// Regular i64 global for module-level variables (NaN-boxed)
Expand Down Expand Up @@ -1521,6 +1538,13 @@ impl WasmModuleEmitter {
export_section.export("_start", ExportKind::Func, start_idx);
export_section.export("memory", ExportKind::Memory, 0);
export_section.export("__indirect_function_table", ExportKind::Table, 0);
export_section.export("__perry_arg_sp", ExportKind::Global, self.nan_temp_global);
export_section.export("__perry_has_exc", ExportKind::Global, self.has_exc_global);
export_section.export(
"__perry_wasm_depth",
ExportKind::Global,
self.wasm_depth_global,
);
// Export all user functions so async JS code can call them by index.
for idx in self.num_imports..start_idx {
export_section.export(&format!("__wasm_func_{}", idx), ExportKind::Func, idx);
Expand Down Expand Up @@ -1919,6 +1943,7 @@ impl WasmModuleEmitter {
func.instruction(&Instruction::End);
code_section.function(&func);
}
self.emit_exc_propagate_helpers(&mut code_section);

wasm_module.section(&code_section);

Expand Down
Loading