1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Safe, [`wasmi`]/[`wasmtime`]-shaped Rust bindings for the [Wasm3] interpreter.
//!
//! This crate wraps the raw [`wasm3x-sys`] C-API in a small, safe, and efficient
//! API modeled after `wasmi` and `wasmtime`:
//!
//! - [`Engine`] wraps a shared Wasm3 environment and holds the [`Config`].
//! - [`Store`] owns all runtime state of the modules instantiated into it.
//! - [`Module`] is a parsed module that can be instantiated many times.
//! - [`Linker`] provides host functions and instantiates modules.
//! - [`Instance`], [`Func`]/[`TypedFunc`], [`Memory`], and [`Global`] give
//! access to the instantiated module.
//!
//! # Example
//!
//! ```no_run
//! use wasm3x::{Engine, Store, Module, Linker};
//!
//! let engine = Engine::default();
//! let mut store = Store::new(&engine, ());
//! let module = Module::new(&engine, &wasm_bytes()).unwrap();
//! let linker = Linker::<()>::new(&engine);
//! let instance = linker.instantiate_and_start(&mut store, &module).unwrap();
//! let add = instance.get_typed_func::<(i32, i32), i32>(&store, "add").unwrap();
//! assert_eq!(add.call(&mut store, (2, 3)).unwrap(), 5);
//! # fn wasm_bytes() -> Vec<u8> { unimplemented!() }
//! ```
//!
//! # Differences from `wasmi`/`wasmtime` (Wasm3 limitations)
//!
//! - **Function lookup is runtime-global by name.** Wasm3 finds exported
//! functions across all modules loaded into the runtime; there is no
//! per-instance namespace and no lookup by index. This matches the common
//! one-module-per-store usage.
//! - **No import/export enumeration.** Wasm3 does not expose a module's imports
//! or exports, so there is no `Module::imports()`/`exports()`. Host functions
//! are provided through a [`Linker`] and linked by name.
//! - **Host functions receive a [`Caller`] but cannot re-enter Wasm.** A host
//! function gets a [`Caller<'_, T>`](Caller) for `&mut T` host-data and linear
//! [`Memory`] access, but — unlike Wasmtime — it may not call back into Wasm:
//! Wasm3's public call entry points run from the base of the runtime stack and
//! would corrupt the suspended frame. Host functions also return at most one
//! value (Wasm3's signature format encodes a single result); Wasm functions you
//! *call* may be multi-value.
//! - **Light validation.** Wasm3 is not a fully validating runtime; treat
//! [`Module::new`] validation as best-effort.
//! - **Single-threaded.** [`Engine`] and [`Store`] are neither [`Send`] nor
//! [`Sync`].
//!
//! [Wasm3]: https://github.com/wasm3/wasm3
//! [`wasmi`]: https://docs.rs/wasmi
//! [`wasmtime`]: https://docs.rs/wasmtime
//! [`wasm3x-sys`]: https://docs.rs/wasm3x-sys
pub use Caller;
pub use ;
pub use ;
pub use ;
pub use Global;
pub use Instance;
pub use Linker;
pub use Memory;
pub use Module;
pub use ;
pub use ;