wasm-bindgen/api/wasm_bindgen/fn.externref_heap_live_count.html

42 lines
5.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `externref_heap_live_count` fn in crate `wasm_bindgen`."><meta name="keywords" content="rust, rustlang, rust-lang, externref_heap_live_count"><title>wasm_bindgen::externref_heap_live_count - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="../favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc fn"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a href='../wasm_bindgen/index.html'><div class='logo-container'><img src='../rust-logo.png' alt='logo'></div></a><div class="sidebar-elems"><p class='location'><a href='index.html'>wasm_bindgen</a></p><script>window.sidebarCurrent = {name: 'externref_heap_live_count', ty: 'fn', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"></div><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../src/wasm_bindgen/lib.rs.html#706-708' title='goto source code'>[src]</a></span><span class='in-band'>Function <a href='index.html'>wasm_bindgen</a>::<wbr><a class="fn" href=''>externref_heap_live_count</a></span></h1><pre class='rust fn'>pub fn externref_heap_live_count() -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></pre><div class='docblock'><p>Get the count of live <code>externref</code>s / <code>JsValue</code>s in <code>wasm-bindgen</code>'s heap.</p>
<h2 id="usage" class="section-header"><a href="#usage">Usage</a></h2>
<p>This is intended for debugging and writing tests.</p>
<p>To write a test that asserts against unnecessarily keeping <code>anref</code>s /
<code>JsValue</code>s alive:</p>
<ul>
<li>
<p>get an initial live count,</p>
</li>
<li>
<p>perform some series of operations or function calls that should clean up
after themselves, and should not keep holding onto <code>externref</code>s / <code>JsValue</code>s
after completion,</p>
</li>
<li>
<p>get the final live count,</p>
</li>
<li>
<p>and assert that the initial and final counts are the same.</p>
</li>
</ul>
<h2 id="what-is-counted" class="section-header"><a href="#what-is-counted">What is Counted</a></h2>
<p>Note that this only counts the <em>owned</em> <code>externref</code>s / <code>JsValue</code>s that end up in
<code>wasm-bindgen</code>'s heap. It does not count borrowed <code>externref</code>s / <code>JsValue</code>s
that are on its stack.</p>
<p>For example, these <code>JsValue</code>s are accounted for:</p>
<div class='information'><div class='tooltip ignore'><span class='tooltiptext'>This example is not tested</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered ignore">
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">my_function</span>(<span class="ident">this_is_counted</span>: <span class="ident">JsValue</span>) {
<span class="kw">let</span> <span class="ident">also_counted</span> <span class="op">=</span> <span class="ident">JsValue</span>::<span class="ident">from_str</span>(<span class="string">&quot;hi&quot;</span>);
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">wasm_bindgen</span>::<span class="ident">externref_heap_live_count</span>() <span class="op">&gt;</span><span class="op">=</span> <span class="number">2</span>);
}</pre></div>
<p>While this borrowed <code>JsValue</code> ends up on the stack, not the heap, and
therefore is not accounted for:</p>
<div class='information'><div class='tooltip ignore'><span class='tooltiptext'>This example is not tested</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered ignore">
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">my_other_function</span>(<span class="ident">this_is_not_counted</span>: <span class="kw-2">&amp;</span><span class="ident">JsValue</span>) {
<span class="comment">// ...</span>
}</pre></div>
</div></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../";window.currentCrate = "wasm_bindgen";</script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>