Skip to main content
Tree-sitter’s Rust bindings support loading grammar files compiled to WebAssembly. This allows you to use languages without needing to compile them natively.
Wasm support requires enabling the wasm feature in your Cargo.toml:

WasmStore

A WasmStore manages WebAssembly language instances.

new

Create a new Wasm store.
&wasmtime::Engine
required
The Wasmtime engine to use for executing WebAssembly code
Returns: Ok(WasmStore) on success, or Err(WasmError) if the store could not be created.

load_language

Load a language from a Wasm file.
&str
required
The name of the language (for debugging purposes)
&[u8]
required
The bytes of the compiled Wasm file
Returns: Ok(Language) on success, or Err(WasmError) if the language could not be loaded.

language_count

Get the number of languages currently loaded in this store.

Parser Extensions

When the wasm feature is enabled, the Parser struct gains additional methods for working with WebAssembly.

set_wasm_store

Set the Wasm store that the parser should use. This is required before you can parse with a Wasm-based language.
WasmStore
required
The Wasm store to use
Returns: 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

Take ownership of the parser’s Wasm store, removing it from the parser. Returns: Some(WasmStore) if the parser has a store, or None if it doesn’t.

Language Extensions

When the wasm feature is enabled, the Language struct gains additional methods.

is_wasm

Check if this language is loaded from WebAssembly.

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 WasmStore maintains its own runtime state
For best performance:
  • Reuse WasmStore instances when possible
  • Load languages once during application startup
  • Consider using native grammars for performance-critical applications

Error Handling