[][src]Function wasm_bindgen::anyref_heap_live_count

pub fn anyref_heap_live_count() -> u32

Get the count of live anyrefs / 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 anyrefs / 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 anyrefs / JsValues that end up in wasm-bindgen's heap. It does not count borrowed anyrefs / JsValues that are on its stack.

For example, these JsValues are accounted for:

This example is not tested
#[wasm_bindgen]
pub fn my_function(this_is_counted: JsValue) {
    let also_counted = JsValue::from_str("hi");
    assert!(wasm_bindgen::anyref_heap_live_count() >= 2);
}

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

This example is not tested
#[wasm_bindgen]
pub fn my_other_function(this_is_not_counted: &JsValue) {
    // ...
}