pub struct Module { /* private fields */ }
Expand description
A parsed and validated WebAssembly module.
Implementations§
Source§impl Module
impl Module
Sourcepub fn new(engine: &Engine, wasm: impl AsRef<[u8]>) -> Result<Self, Error>
pub fn new(engine: &Engine, wasm: impl AsRef<[u8]>) -> Result<Self, Error>
Creates a new Wasm Module
from the given Wasm bytecode buffer.
§Note
- This parses, validates and translates the buffered Wasm bytecode.
- The
wasm
may be encoded as WebAssembly binary (.wasm
) or as WebAssembly text format (.wat
).
§Errors
- If the Wasm bytecode is malformed or fails to validate.
- If the Wasm bytecode violates restrictions
set in the
Config
used by theengine
. - If Wasmi cannot translate the Wasm bytecode.
Sourcepub fn new_streaming(engine: &Engine, stream: impl Read) -> Result<Self, Error>
pub fn new_streaming(engine: &Engine, stream: impl Read) -> Result<Self, Error>
Creates a new Wasm Module
from the given Wasm bytecode stream.
§Note
This parses, validates and translates the Wasm bytecode yielded by stream
.
§Errors
- If the Wasm bytecode is malformed or fails to validate.
- If the Wasm bytecode violates restrictions
set in the
Config
used by theengine
. - If Wasmi cannot translate the Wasm bytecode.
Sourcepub unsafe fn new_unchecked(engine: &Engine, wasm: &[u8]) -> Result<Self, Error>
pub unsafe fn new_unchecked(engine: &Engine, wasm: &[u8]) -> Result<Self, Error>
Creates a new Wasm Module
from the given Wasm bytecode buffer.
§Note
This parses and translates the buffered Wasm bytecode.
§Safety
- This does not validate the Wasm bytecode.
- It is the caller’s responsibility that the Wasm bytecode is valid.
- It is the caller’s responsibility that the Wasm bytecode adheres
to the restrictions set by the used
Config
of theengine
. - Violating the above rules is undefined behavior.
§Errors
- If the Wasm bytecode is malformed or contains invalid sections.
- If the Wasm bytecode fails to be compiled by Wasmi.
Sourcepub unsafe fn new_streaming_unchecked(
engine: &Engine,
stream: impl Read,
) -> Result<Self, Error>
pub unsafe fn new_streaming_unchecked( engine: &Engine, stream: impl Read, ) -> Result<Self, Error>
Creates a new Wasm Module
from the given byte stream.
§Note
This parses and translates the Wasm bytecode yielded by stream
.
§Safety
- This does not validate the Wasm bytecode.
- It is the caller’s responsibility that the Wasm bytecode is valid.
- It is the caller’s responsibility that the Wasm bytecode adheres
to the restrictions set by the used
Config
of theengine
. - Violating the above rules is undefined behavior.
§Errors
- If the Wasm bytecode is malformed or contains invalid sections.
- If the Wasm bytecode fails to be compiled by Wasmi.
Sourcepub fn validate(engine: &Engine, wasm: &[u8]) -> Result<(), Error>
pub fn validate(engine: &Engine, wasm: &[u8]) -> Result<(), Error>
Validates wasm
as a WebAssembly binary given the configuration (via Config
) in engine
.
This function performs Wasm validation of the binary input WebAssembly module and
returns either Ok`` or
Err`` depending on the results of the validation.
The Config
of the engine
is used for Wasm validation which indicates which WebAssembly
features are valid and invalid for the validation.
§Note
- The input
wasm
must be in binary form, the text format is not accepted by this function. - This will only validate the
wasm
but not try to translate it. ThereforeModule::new
might still fail if translation of the Wasm binary input fails to translate via the WasmiEngine
. - Validation automatically happens as part of
Module::new
.
§Errors
If Wasm validation for wasm
fails for the given Config
provided via engine
.
Sourcepub fn imports(&self) -> ModuleImportsIter<'_> ⓘ
pub fn imports(&self) -> ModuleImportsIter<'_> ⓘ
Returns an iterator over the imports of the Module
.
Sourcepub fn exports(&self) -> ModuleExportsIter<'_> ⓘ
pub fn exports(&self) -> ModuleExportsIter<'_> ⓘ
Returns an iterator over the exports of the Module
.
Sourcepub fn get_export(&self, name: &str) -> Option<ExternType>
pub fn get_export(&self, name: &str) -> Option<ExternType>
Sourcepub fn custom_sections(&self) -> CustomSectionsIter<'_> ⓘ
pub fn custom_sections(&self) -> CustomSectionsIter<'_> ⓘ
Returns an iterator yielding the custom sections of the Wasm Module
.
§Note
The returned iterator will yield no items if Config::ignore_custom_sections
is set to true
even if the original Wasm module contains custom sections.