pub fn externref_heap_live_count() -> u32
Expand description

Get the count of live externrefs / JsValues in wasm-bindgen’s heap.

Usage

This is intended for debugging and writing tests.

To write a test that asserts against unnecessarily keeping anrefs / JsValues alive:

  • get an initial live count,

  • perform some series of operations or function calls that should clean up after themselves, and should not keep holding onto externrefs / JsValues after completion,

  • get the final live count,

  • and assert that the initial and final counts are the same.

What is Counted

Note that this only counts the owned externrefs / JsValues that end up in wasm-bindgen’s heap. It does not count borrowed externrefs / JsValues that are on its stack.

For example, these JsValues are accounted for:

#[wasm_bindgen]
pub fn my_function(this_is_counted: JsValue) {
    let also_counted = JsValue::from_str("hi");
    assert!(wasm_bindgen::externref_heap_live_count() >= 2);
}

While this borrowed JsValue ends up on the stack, not the heap, and therefore is not accounted for:

#[wasm_bindgen]
pub fn my_other_function(this_is_not_counted: &JsValue) {
    // ...
}