Handle the function table export on-demand

Don't delay processing until `finalize`, but instead process it as soon
as it's requested to avoid doing too much logic in `finalize`.
This commit is contained in:
Alex Crichton 2019-06-04 08:52:54 -07:00
parent 71209686e9
commit 4eafaeae2d
2 changed files with 15 additions and 17 deletions

View File

@ -41,7 +41,6 @@ pub struct Context<'a> {
defined_identifiers: HashMap<String, usize>,
exported_classes: Option<BTreeMap<String, ExportedClass>>,
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::<WebidlCustomSection>()
.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, usize>) -> String {

View File

@ -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")
}