Skip to content

Commit

Permalink
Generate Symbol.dispose entries for exposed structs (and define Symbo…
Browse files Browse the repository at this point in the history
…l.dispose if it doesn't exist)
  • Loading branch information
H-Plus-Time committed Sep 18, 2024
1 parent 5372294 commit 905e636
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,10 +1080,15 @@ impl<'a> Context<'a> {
const ptr = this.__destroy_into_raw();
wasm.{}(ptr, 0);
}}
[Symbol.dispose]() {{
this.free();
}}
",
wasm_bindgen_shared::free_function(name),
));
ts_dst.push_str(" free(): void;\n");
ts_dst.push_str(" [Symbol.dispose](): void;\n");
dst.push_str(&class.contents);
ts_dst.push_str(&class.typescript);

Expand Down Expand Up @@ -1486,6 +1491,14 @@ impl<'a> Context<'a> {
Ok(ret)
}

fn expose_symbol_dispose(&mut self) -> Result<(), Error> {
if !self.should_write_global("symbol_dispose") {
return Ok(());
}
self.global(&"if(!Symbol.dispose) { Symbol.dispose = Symbol('Symbol.dispose'); }");
Ok(())
}

fn expose_text_encoder(&mut self) -> Result<(), Error> {
if !self.should_write_global("text_encoder") {
return Ok(());
Expand Down Expand Up @@ -2476,6 +2489,10 @@ impl<'a> Context<'a> {

pub fn generate(&mut self) -> Result<(), Error> {
self.prestore_global_import_identifiers()?;
// conditionally override Symbol.dispose
if !self.aux.structs.is_empty() {
self.expose_symbol_dispose()?;
}
for (id, adapter) in crate::sorted_iter(&self.wit.adapters) {
let instrs = match &adapter.kind {
AdapterKind::Import { .. } => continue,
Expand Down
1 change: 1 addition & 0 deletions crates/cli/tests/reference/builder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
export class ClassBuilder {
free(): void;
[Symbol.dispose](): void;
/**
* @returns {ClassBuilder}
*/
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/tests/reference/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export function __wbg_set_wasm(val) {
}


if(!Symbol.dispose) { Symbol.dispose = Symbol('Symbol.dispose'); }

const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
Expand Down Expand Up @@ -50,6 +52,10 @@ export class ClassBuilder {
const ptr = this.__destroy_into_raw();
wasm.__wbg_classbuilder_free(ptr, 0);
}

[Symbol.dispose]() {
this.free();
}
/**
* @returns {ClassBuilder}
*/
Expand Down
1 change: 1 addition & 0 deletions crates/cli/tests/reference/constructor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
export class ClassConstructor {
free(): void;
[Symbol.dispose](): void;
/**
*/
constructor();
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/tests/reference/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export function __wbg_set_wasm(val) {
}


if(!Symbol.dispose) { Symbol.dispose = Symbol('Symbol.dispose'); }

const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
Expand Down Expand Up @@ -42,6 +44,10 @@ export class ClassConstructor {
const ptr = this.__destroy_into_raw();
wasm.__wbg_classconstructor_free(ptr, 0);
}

[Symbol.dispose]() {
this.free();
}
/**
*/
constructor() {
Expand Down
1 change: 1 addition & 0 deletions crates/cli/tests/reference/raw.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function test1(test: number): number;
*/
export class Test {
free(): void;
[Symbol.dispose](): void;
/**
* @param {number} test
* @returns {Test}
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/tests/reference/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export function __wbg_set_wasm(val) {
}


if(!Symbol.dispose) { Symbol.dispose = Symbol('Symbol.dispose'); }

const heap = new Array(128).fill(undefined);

heap.push(undefined, null, true, false);
Expand Down Expand Up @@ -89,6 +91,10 @@ export class Test {
const ptr = this.__destroy_into_raw();
wasm.__wbg_test_free(ptr, 0);
}

[Symbol.dispose]() {
this.free();
}
/**
* @param {number} test
* @returns {Test}
Expand Down

0 comments on commit 905e636

Please sign in to comment.