diff --git a/crates/cli-support/src/js.rs b/crates/cli-support/src/js.rs index 72f8b875..ee529033 100644 --- a/crates/cli-support/src/js.rs +++ b/crates/cli-support/src/js.rs @@ -2,8 +2,10 @@ use std::collections::{HashSet, HashMap}; use std::fmt::Write; use std::mem; -use shared; use parity_wasm::elements::*; +use parity_wasm; +use shared; +use wasm_gc; use super::Bindgen; @@ -61,6 +63,8 @@ impl<'a> Context<'a> { } pub fn finalize(&mut self, module_name: &str) -> (String, String) { + self.unexport_unused_internal_exports(); + self.gc(); self.write_classes(); { let mut bind = |name: &str, f: &Fn(&mut Self) -> String| { @@ -277,8 +281,8 @@ impl<'a> Context<'a> { footer = self.footer, ); - self.unexport_unused_internal_exports(); self.export_table(); + self.gc(); (js, self.typescript.clone()) } @@ -303,6 +307,7 @@ impl<'a> Context<'a> { let new_name = shared::new_function(&class); if self.wasm_import_needed(&new_name) { + self.expose_add_heap_object(); self.export(&new_name, &format!(" function(ptr) {{ return addHeapObject(new {class}(ptr, token)); @@ -319,6 +324,7 @@ impl<'a> Context<'a> { let new_name = shared::new_function(&class); if self.wasm_import_needed(&new_name) { + self.expose_add_heap_object(); self.export(&new_name, &format!(" function(ptr) {{ return addHeapObject(new {class}(ptr)); @@ -1154,6 +1160,16 @@ impl<'a> Context<'a> { } "); } + + fn gc(&mut self) { + let module = mem::replace(self.module, Module::default()); + let wasm_bytes = parity_wasm::serialize(module).unwrap(); + let bytes = wasm_gc::Config::new() + .demangle(false) + .gc(&wasm_bytes) + .unwrap(); + *self.module = deserialize_buffer(&bytes).unwrap(); + } } impl<'a, 'b> SubContext<'a, 'b> { diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index a8015d40..0060fd92 100644 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -129,10 +129,7 @@ impl Bindgen { let wasm_bytes = parity_wasm::serialize(module).map_err(|e| { Error(format!("{:?}", e)) })?; - let bytes = wasm_gc::Config::new() - .demangle(false) - .gc(&wasm_bytes)?; - File::create(&wasm_path)?.write_all(&bytes)?; + File::create(&wasm_path)?.write_all(&wasm_bytes)?; Ok(()) }