Fix some class import methods and auto gc

The runtime functions are now moved to the `wasm-bindgen` crate and are
auto-gc'd if they don't end up actually being required.
This commit is contained in:
Alex Crichton
2018-02-06 08:58:15 -08:00
parent 28966d9853
commit 56b7fa453a
7 changed files with 95 additions and 102 deletions

View File

@ -10,6 +10,7 @@ pub struct Js<'a> {
pub imports: String,
pub typescript: String,
pub exposed_globals: HashSet<&'static str>,
pub required_internal_exports: HashSet<&'static str>,
pub config: &'a Bindgen,
pub module: &'a mut Module,
pub program: &'a shared::Program,
@ -188,6 +189,7 @@ impl<'a> Js<'a> {
);
self.rewrite_imports(module_name);
self.unexport_unused_internal_exports();
(js, self.typescript.clone())
}
@ -343,6 +345,7 @@ impl<'a> Js<'a> {
destructors.push_str(&format!("\n\
wasm.__wbindgen_free(ptr{i}, len{i});\n\
", i = i));
self.required_internal_exports.insert("__wbindgen_free");
}
}
shared::TYPE_JS_OWNED => {
@ -416,6 +419,9 @@ impl<'a> Js<'a> {
Some(&shared::TYPE_STRING) => {
dst_ts.push_str(": string");
self.expose_get_string_from_wasm();
self.required_internal_exports.insert("__wbindgen_boxed_str_ptr");
self.required_internal_exports.insert("__wbindgen_boxed_str_len");
self.required_internal_exports.insert("__wbindgen_boxed_str_free");
format!("
const ptr = wasm.__wbindgen_boxed_str_ptr(ret);
const len = wasm.__wbindgen_boxed_str_len(ret);
@ -567,6 +573,7 @@ impl<'a> Js<'a> {
wasm.__wbindgen_free(ptr{0}, len{0});
", i));
invocation.push_str(&format!("arg{}", i));
self.required_internal_exports.insert("__wbindgen_free");
}
shared::TYPE_JS_OWNED => {
self.expose_take_object();
@ -666,6 +673,20 @@ impl<'a> Js<'a> {
}
}
fn unexport_unused_internal_exports(&mut self) {
let required = &self.required_internal_exports;
for section in self.module.sections_mut() {
let exports = match *section {
Section::Export(ref mut s) => s,
_ => continue,
};
exports.entries_mut().retain(|export| {
!export.field().starts_with("__wbindgen") ||
required.contains(export.field())
});
}
}
fn expose_drop_ref(&mut self) {
if !self.exposed_globals.insert("drop_ref") {
return
@ -805,6 +826,7 @@ impl<'a> Js<'a> {
if !self.exposed_globals.insert("pass_string_to_wasm") {
return
}
self.required_internal_exports.insert("__wbindgen_malloc");
if self.config.nodejs {
self.globals.push_str(&format!("
function passStringToWasm(arg) {{

View File

@ -3,6 +3,7 @@ extern crate failure;
extern crate parity_wasm;
extern crate wasm_bindgen_shared as shared;
extern crate serde_json;
extern crate wasm_gc;
use std::fs::File;
use std::io::Write;
@ -71,6 +72,7 @@ impl Bindgen {
imports: String::new(),
typescript: format!("/* tslint:disable */\n"),
exposed_globals: Default::default(),
required_internal_exports: Default::default(),
config: &self,
module: &mut module,
program: &program,
@ -87,9 +89,13 @@ impl Bindgen {
}
let wasm_path = out_dir.join(format!("{}_wasm", stem)).with_extension("wasm");
parity_wasm::serialize_to_file(wasm_path, module).map_err(|e| {
let wasm_bytes = parity_wasm::serialize(module).map_err(|e| {
format_err!("{:?}", e)
})?;
let bytes = wasm_gc::Config::new()
.demangle(false)
.gc(&wasm_bytes)?;
File::create(&wasm_path)?.write_all(&bytes)?;
Ok(())
}
}