libsql_wasi/
lib.rs

1pub mod error;
2pub mod memory;
3mod vfs;
4
5use wasmtime::{Engine, Instance, Linker, Module, Store};
6use wasmtime_wasi::{WasiCtx, WasiCtxBuilder};
7
8pub use error::Error;
9pub type Result<T> = std::result::Result<T, Error>;
10
11pub type State = WasiCtx;
12
13pub fn new_linker(engine: &Engine) -> Result<Linker<State>> {
14    let mut linker = Linker::new(engine);
15    vfs::link(&mut linker)?;
16    wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
17    Ok(linker)
18}
19
20pub fn instantiate(
21    linker: &Linker<State>,
22    libsql_wasm_path: impl AsRef<std::path::Path>,
23) -> Result<(Store<State>, Instance)> {
24    let wasi_ctx = WasiCtxBuilder::new()
25        .inherit_stdio()
26        .inherit_args()
27        .map_err(|e| crate::error::Error::InternalError(Box::new(e)))?
28        .build();
29
30    let libsql_module = Module::from_file(linker.engine(), libsql_wasm_path.as_ref())?;
31
32    let mut store = Store::new(linker.engine(), wasi_ctx);
33    let instance = linker.instantiate(&mut store, &libsql_module)?;
34
35    Ok((store, instance))
36}