2019-09-06 15:57:44 -07:00

84 lines
15 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 `wasmer_runtime` crate."><meta name="keywords" content="rust, rustlang, rust-lang, wasmer_runtime"><title>wasmer_runtime - 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="https://wasmer.io/static/icons/favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[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='../wasmer_runtime/index.html'><div class='logo-container'><img src='https://avatars3.githubusercontent.com/u/44205449?s=200&v=4' alt='logo'></div></a><p class='location'>Crate wasmer_runtime</p><div class="sidebar-elems"><a id='all-types' href='all.html'><p>See all wasmer_runtime's items</p></a><div class="block items"><ul><li><a href="#modules">Modules</a></li><li><a href="#macros">Macros</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#constants">Constants</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'wasmer_runtime', ty: 'mod', relpath: '../'};</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 js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" 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/wasmer_runtime/lib.rs.html#1-245' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>wasmer_runtime</a></span></h1><div class='docblock'><p>Wasmer-runtime is a library that makes embedding WebAssembly
in your application easy, efficient, and safe.</p>
<h1 id="how-to-use-wasmer-runtime" class="section-header"><a href="#how-to-use-wasmer-runtime">How to use Wasmer-Runtime</a></h1>
<p>The easiest way is to use the <a href="fn.instantiate.html"><code>instantiate</code></a> function to create an <a href="struct.Instance.html"><code>Instance</code></a>.
Then you can use <a href="struct.Instance.html#method.call"><code>call</code></a> or <a href="struct.Instance.html#method.func"><code>func</code></a> and then <a href="struct.Function.html#method.call"><code>call</code></a> to call an exported function safely.</p>
<h2 id="heres-an-example" class="section-header"><a href="#heres-an-example">Here's an example:</a></h2>
<p>Given this WebAssembly:</p>
<pre><code class="language-wat">(module
(type $t0 (func (param i32) (result i32)))
(func $add_one (export &quot;add_one&quot;) (type $t0) (param $p0 i32) (result i32)
get_local $p0
i32.const 1
i32.add))
</code></pre>
<p>compiled into wasm bytecode, we can call the exported &quot;add_one&quot; function:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">static</span> <span class="ident">WASM</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;static</span> [<span class="ident">u8</span>] <span class="op">=</span> <span class="kw-2">&amp;</span>[
<span class="comment">// The module above compiled to bytecode goes here.</span>
<span class="comment">// ...</span>
];
<span class="kw">use</span> <span class="ident">wasmer_runtime</span>::{
<span class="ident">instantiate</span>,
<span class="ident">Value</span>,
<span class="ident">imports</span>,
<span class="ident">error</span>,
<span class="ident">Func</span>,
};
<span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="ident">error</span>::<span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {
<span class="comment">// We&#39;re not importing anything, so make an empty import object.</span>
<span class="kw">let</span> <span class="ident">import_object</span> <span class="op">=</span> <span class="macro">imports</span><span class="macro">!</span> {};
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">instance</span> <span class="op">=</span> <span class="ident">instantiate</span>(<span class="ident">WASM</span>, <span class="kw-2">&amp;</span><span class="ident">import_object</span>)<span class="question-mark">?</span>;
<span class="kw">let</span> <span class="ident">add_one</span>: <span class="ident">Func</span><span class="op">&lt;</span><span class="ident">i32</span>, <span class="ident">i32</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">instance</span>.<span class="ident">func</span>(<span class="string">&quot;add_one&quot;</span>)<span class="question-mark">?</span>;
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="ident">add_one</span>.<span class="ident">call</span>(<span class="number">42</span>)<span class="question-mark">?</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">value</span>, <span class="number">43</span>);
<span class="prelude-val">Ok</span>(())
}</pre></div>
<h1 id="additional-notes" class="section-header"><a href="#additional-notes">Additional Notes:</a></h1>
<p>The <code>wasmer-runtime</code> is build to support compiler multiple backends.
Currently, we support the <a href="https://github.com/CraneStation/cranelift">Cranelift</a> compiler with the <a href="https://crates.io/crates/wasmer-clif-backend"><code>wasmer-clif-backend</code></a> crate.</p>
<p>You can specify the compiler you wish to use with the <a href="fn.compile_with.html"><code>compile_with</code></a> function.</p>
</div><h2 id='modules' class='section-header'><a href="#modules">Modules</a></h2>
<table><tr class='module-item'><td><a class="mod" href="cache/index.html" title='wasmer_runtime::cache mod'>cache</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="mod" href="error/index.html" title='wasmer_runtime::error mod'>error</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="mod" href="memory/index.html" title='wasmer_runtime::memory mod'>memory</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="mod" href="units/index.html" title='wasmer_runtime::units mod'>units</a></td><td class='docblock-short'><p>Various unit types.</p>
</td></tr><tr class='module-item'><td><a class="mod" href="wasm/index.html" title='wasmer_runtime::wasm mod'>wasm</a></td><td class='docblock-short'><p>Various types exposed by the Wasmer Runtime.</p>
</td></tr></table><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
<table><tr class='module-item'><td><a class="macro" href="macro.func.html" title='wasmer_runtime::func macro'>func</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="macro" href="macro.imports.html" title='wasmer_runtime::imports macro'>imports</a></td><td class='docblock-short'><p>Generate an <a href="struct.ImportObject.html"><code>ImportObject</code></a> safely.</p>
</td></tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table><tr class='module-item'><td><a class="struct" href="struct.Ctx.html" title='wasmer_runtime::Ctx struct'>Ctx</a></td><td class='docblock-short'><p>The context of the currently running WebAssembly instance.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.DynFunc.html" title='wasmer_runtime::DynFunc struct'>DynFunc</a></td><td class='docblock-short'><p>A representation of an exported WebAssembly function.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Func.html" title='wasmer_runtime::Func struct'>Func</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="struct" href="struct.Global.html" title='wasmer_runtime::Global struct'>Global</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="struct" href="struct.ImportObject.html" title='wasmer_runtime::ImportObject struct'>ImportObject</a></td><td class='docblock-short'><p>All of the import data used when instantiating.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Instance.html" title='wasmer_runtime::Instance struct'>Instance</a></td><td class='docblock-short'><p>An instantiated WebAssembly module.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Memory.html" title='wasmer_runtime::Memory struct'>Memory</a></td><td class='docblock-short'><p>A shared or unshared wasm linear memory.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.MiddlewareChain.html" title='wasmer_runtime::MiddlewareChain struct'>MiddlewareChain</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="struct" href="struct.Module.html" title='wasmer_runtime::Module struct'>Module</a></td><td class='docblock-short'><p>A compiled WebAssembly module.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.StreamingCompiler.html" title='wasmer_runtime::StreamingCompiler struct'>StreamingCompiler</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="struct" href="struct.Table.html" title='wasmer_runtime::Table struct'>Table</a></td><td class='docblock-short'></td></tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
<table><tr class='module-item'><td><a class="enum" href="enum.Backend.html" title='wasmer_runtime::Backend enum'>Backend</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="enum" href="enum.Export.html" title='wasmer_runtime::Export enum'>Export</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="enum" href="enum.Value.html" title='wasmer_runtime::Value enum'>Value</a></td><td class='docblock-short'><p>Represents a WebAssembly value.</p>
</td></tr></table><h2 id='constants' class='section-header'><a href="#constants">Constants</a></h2>
<table><tr class='module-item'><td><a class="constant" href="constant.VERSION.html" title='wasmer_runtime::VERSION constant'>VERSION</a></td><td class='docblock-short'><p>The current version of this crate</p>
</td></tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
<table><tr class='module-item'><td><a class="fn" href="fn.compile.html" title='wasmer_runtime::compile fn'>compile</a></td><td class='docblock-short'><p>Compile WebAssembly binary code into a <a href="struct.Module.html"><code>Module</code></a>.
This function is useful if it is necessary to
compile a module before it can be instantiated
(otherwise, the <a href="fn.instantiate.html"><code>instantiate</code></a> function should be used).</p>
</td></tr><tr class='module-item'><td><a class="fn" href="fn.compile_with.html" title='wasmer_runtime::compile_with fn'>compile_with</a></td><td class='docblock-short'><p>Compile a <a href="struct.Module.html"><code>Module</code></a> using the provided compiler from
WebAssembly binary code. This function is useful if it
is necessary to a compile a module before it can be instantiated
and must be used if you wish to use a different backend from the default.</p>
</td></tr><tr class='module-item'><td><a class="fn" href="fn.compile_with_config.html" title='wasmer_runtime::compile_with_config fn'>compile_with_config</a></td><td class='docblock-short'><p>The same as <code>compile</code> but takes a <code>CompilerConfig</code> for the purpose of
changing the compiler's behavior</p>
</td></tr><tr class='module-item'><td><a class="fn" href="fn.compile_with_config_with.html" title='wasmer_runtime::compile_with_config_with fn'>compile_with_config_with</a></td><td class='docblock-short'><p>The same as <code>compile_with_config</code> but takes a <code>Compiler</code> for the purpose of
changing the backend.</p>
</td></tr><tr class='module-item'><td><a class="fn" href="fn.compiler_for_backend.html" title='wasmer_runtime::compiler_for_backend fn'>compiler_for_backend</a></td><td class='docblock-short'></td></tr><tr class='module-item'><td><a class="fn" href="fn.default_compiler.html" title='wasmer_runtime::default_compiler fn'>default_compiler</a></td><td class='docblock-short'><p>Get a single instance of the default compiler to use.</p>
</td></tr><tr class='module-item'><td><a class="fn" href="fn.instantiate.html" title='wasmer_runtime::instantiate fn'>instantiate</a></td><td class='docblock-short'><p>Compile and instantiate WebAssembly code without
creating a <a href="struct.Module.html"><code>Module</code></a>.</p>
</td></tr><tr class='module-item'><td><a class="fn" href="fn.validate.html" title='wasmer_runtime::validate fn'>validate</a></td><td class='docblock-short'><p>Perform validation as defined by the
WebAssembly specification. Returns <code>true</code> if validation
succeeded, <code>false</code> if validation failed.</p>
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd></kbd></dt><dd>Move up in search results</dd><dt><kbd></kbd></dt><dd>Move down in search results</dd><dt><kbd></kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "wasmer_runtime";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>