Wasm support requires enabling the
wasm feature in your Cargo.toml:WasmStore
AWasmStore manages WebAssembly language instances.
new
&wasmtime::Engine
required
The Wasmtime engine to use for executing WebAssembly code
Ok(WasmStore) on success, or Err(WasmError) if the store could not be created.
load_language
&str
required
The name of the language (for debugging purposes)
&[u8]
required
The bytes of the compiled Wasm file
Ok(Language) on success, or Err(WasmError) if the language could not be loaded.
language_count
Parser Extensions
When thewasm feature is enabled, the Parser struct gains additional methods for working with WebAssembly.
set_wasm_store
WasmStore
required
The Wasm store to use
Ok(()) on success, or Err(LanguageError) if the store could not be set.
The store may be loaded from a different
WasmStore than the one set on the parser, but it must use the same underlying WasmEngine.take_wasm_store
Some(WasmStore) if the parser has a store, or None if it doesn’t.
Language Extensions
When thewasm feature is enabled, the Language struct gains additional methods.
is_wasm
WasmError
Errors that can occur when working with WebAssembly grammars.WasmErrorKind
The kind of error that occurred
String
A human-readable error message
WasmErrorKind
variant
Failed to parse the Wasm file
variant
Failed to compile the Wasm file
variant
Failed to instantiate the Wasm module
variant
An unknown error occurred
Complete Example
Here’s a complete example of using WebAssembly grammars:Using Multiple Wasm Languages
Performance Considerations
While WebAssembly grammars provide flexibility, there are some performance considerations:- Initialization: Loading and compiling Wasm modules has a one-time cost
- Runtime: Wasm-based parsers are generally slightly slower than native parsers
- Memory: Each
WasmStoremaintains its own runtime state
- Reuse
WasmStoreinstances when possible - Load languages once during application startup
- Consider using native grammars for performance-critical applications