Expand description
§wasmi
This library allows WebAssembly modules to be loaded in binary format and their functions invoked.
§Introduction
WebAssembly (wasm) is a safe, portable and compact format that is designed for efficient execution.
Wasm code is distributed in the form of modules that contains definitions of:
- functions,
- global variables,
- linear memory instances and
- tables.
Each of these definitions can be imported and exported.
In addition to these definitions, modules can define initialization data for their memory or tables. This initialization data can take the
form of segments, copied to given offsets. They can also define a start
function that is automatically executed when the module is loaded.
§Loading and Validation
Before execution, a module must be validated. This process checks that the module is well-formed and makes only allowed operations.
A valid module can’t access memory outside its sandbox, can’t cause stack underflows and can only call functions with correct signatures.
§Instantiation
In order to execute code from a wasm module, it must be instantiated. Instantiation includes the following steps:
- Creating an empty module instance.
- Resolving the definition instances for each declared import in the module.
- Instantiating definitions declared in the module (e.g. allocate global variables, allocate linear memory, etc.).
- Initializing memory and table contents by copying segments into them.
- Executing the
start
function, if any.
After these steps, the module instance is ready to execute functions.
§Execution
It only is allowed to call functions which are exported by the module. Functions can either return a result or trap (e.g. there can’t be linking error in the middle of the function execution). This property is ensured by the validation process.
§Examples
extern crate casper_wasmi;
extern crate wat;
use casper_wasmi::{ModuleInstance, ImportsBuilder, NopExternals, RuntimeValue};
fn main() {
// Parse WAT (WebAssembly Text format) into wasm bytecode.
let wasm_binary: Vec<u8> =
wat::parse_str(
r#"
(module
(func (export "test") (result i32)
i32.const 1337
)
)
"#,
)
.expect("failed to parse wat");
// Load wasm binary and prepare it for instantiation.
let module = casper_wasmi::Module::from_buffer(&wasm_binary)
.expect("failed to load wasm");
// Instantiate a module with empty imports and
// assert that there is no `start` function.
let instance =
ModuleInstance::new(
&module,
&ImportsBuilder::default()
)
.expect("failed to instantiate wasm module")
.assert_no_start();
// Finally, invoke the exported function "test" with no parameters
// and empty external function executor.
assert_eq!(
instance.invoke_export(
"test",
&[],
&mut NopExternals,
).expect("failed to execute export"),
Some(RuntimeValue::I32(1337)),
);
}
Modules§
- memory_
units - WebAssembly-specific sizes and units.
- nan_
preserving_ float - Floating point types that preserve NaN values.
Structs§
- Func
Instance - Runtime representation of a function.
- Func
Invocation - A resumable invocation handle. This struct is returned by
FuncInstance::invoke_resumable
. - FuncRef
- Reference to a function (See
FuncInstance
for details). - Global
Descriptor - Description of a global variable.
- Global
Instance - Runtime representation of a global variable (or
global
for short). - Global
Ref - Reference to a global variable (See
GlobalInstance
for details). - Imports
Builder - Convenience builder of
ImportResolver
. - Memory
Descriptor - Description of a linear memory.
- Memory
Instance - Runtime representation of a linear memory (or
memory
for short). - Memory
Ref - Reference to a memory (See
MemoryInstance
for details). - Module
- Deserialized module prepared for instantiation.
- Module
Instance - A module instance is the runtime representation of a module.
- Module
Ref - Reference to a
ModuleInstance
. - NopExternals
- Implementation of
Externals
that just traps oninvoke_index
. - NotStarted
Module Ref - Mostly instantiated
ModuleRef
. - Runtime
Args - Wrapper around slice of
Value
for using it as an argument list conveniently. - Signature
- Signature of a function.
- Stack
Recycler - Used to recycle stacks instead of allocating them repeatedly.
- Table
Descriptor - Description of a table.
- Table
Instance - Runtime representation of a table.
- Table
Ref - Reference to a table (See
TableInstance
for details).
Enums§
- Error
- Internal interpreter error.
- Extern
Val - An external value is the runtime representation of an entity that can be imported or exported.
- Resumable
Error - A resumable invocation error.
- Runtime
Value - Runtime representation of a value.
- Trap
- Error type which can be thrown by wasm code or by host environment.
- Trap
Code - Error type which can be thrown by wasm code or by host environment.
- Value
Type - Type of a value.
Constants§
- DEFAULT_
CALL_ STACK_ LIMIT - Maximum number of levels on the call stack.
- DEFAULT_
VALUE_ STACK_ LIMIT - Maximum number of bytes on the value stack.
- LINEAR_
MEMORY_ PAGE_ SIZE - Size of a page of linear memory - 64KiB.
Traits§
- Externals
- Trait that allows to implement host functions.
- From
Value - Trait for creating value from a
Value
. - Host
Error - Trait that allows the host to return custom error.
- Import
Resolver - Resolver of a module’s dependencies.
- Little
Endian Convert - Types that can be converted from and to little endian bytes.
- Module
Import Resolver - Version of
ImportResolver
specialized for a single module.