diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index d7f56c6a..3f72f775 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -41,7 +41,6 @@ pub struct Context<'a> { defined_identifiers: HashMap, exported_classes: Option>, - function_table_needed: bool, memory: MemoryId, /// A map of the name of npm dependencies we've loaded so far to the path @@ -95,7 +94,6 @@ impl<'a> Context<'a> { .delete_typed::() .unwrap(), module, - function_table_needed: false, memory, npm_dependencies: Default::default(), }) @@ -207,18 +205,6 @@ impl<'a> Context<'a> { needs_manual_start = self.unstart_start_function(); } - // If our JS glue needs to access the function table, then do so here. - // JS closure shim generation may access the function table as an - // example, but if there's no closures in the module there's no need to - // export it! - if self.function_table_needed { - let id = match self.module.tables.main_function_table()? { - Some(id) => id, - None => bail!("no function table found in module"), - }; - self.module.exports.add("__wbg_function_table", id); - } - // After all we've done, especially // `unexport_unused_internal_exports()`, we probably have a bunch of // garbage in the module that's no longer necessary, so delete @@ -2121,6 +2107,18 @@ impl<'a> Context<'a> { ", ); } + + fn export_function_table(&mut self) -> Result<(), Error> { + if !self.should_write_global("wbg-function-table") { + return Ok(()) + } + let id = match self.module.tables.main_function_table()? { + Some(id) => id, + None => bail!("no function table found in module"), + }; + self.module.exports.add("__wbg_function_table", id); + Ok(()) + } } fn generate_identifier(name: &str, used_names: &mut HashMap) -> String { diff --git a/crates/cli-support/src/js/rust2js.rs b/crates/cli-support/src/js/rust2js.rs index 6053fa3d..8d33c550 100644 --- a/crates/cli-support/src/js/rust2js.rs +++ b/crates/cli-support/src/js/rust2js.rs @@ -333,7 +333,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> { .process(f, &None)? .finish("function", "this.f") }; - self.cx.function_table_needed = true; + self.cx.export_function_table()?; self.global_idx(); self.prelude(&format!( "\ @@ -787,7 +787,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> { .process(&closure.function, &None)? .finish("function", "f") }; - self.cx.function_table_needed = true; + self.cx.export_function_table()?; let body = format!( " const f = wasm.__wbg_function_table.get({}); @@ -1147,7 +1147,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> { Intrinsic::FunctionTable => { assert_eq!(self.js_arguments.len(), 0); - self.cx.function_table_needed = true; + self.cx.export_function_table()?; format!("wasm.__wbg_function_table") }