Sha256: 62a93ef160b796149cc7210bdc2785edea33e89e6b952d2316681a61b439ea82

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

use std::path::Path;
use wasi_common::WasiCtx;
use wasmtime::*;

pub fn create_instance(module_name: &str) -> (Store<WasiCtx>, Instance) {
    let wasi = deterministic_wasi_ctx::build_wasi_ctx();
    let engine = Engine::default();
    let mut linker = Linker::new(&engine);
    wasi_common::sync::add_to_linker(&mut linker, |s| s).unwrap();
    let module_path = Path::new("../../target/wasm32-wasip1/debug").join(module_name);
    let module = Module::from_file(&engine, module_path).unwrap();
    let mut store = Store::new(&engine, wasi);
    linker.module(&mut store, "", &module).unwrap();
    let instance = linker.instantiate(&mut store, &module).unwrap();
    (store, instance)
}

pub fn invoke_func<Params, Results>(
    mut store: Store<WasiCtx>,
    instance: Instance,
    func_name: &str,
    params: Params,
) -> Results
where
    Params: WasmParams,
    Results: WasmResults,
{
    let answer = instance
        .get_func(&mut store, func_name)
        .unwrap_or_else(|| panic!("`{}` was not an exported function", func_name));
    let answer = answer.typed::<Params, Results>(&store).unwrap();
    answer.call(&mut store, params).unwrap()
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.29/tests/common/mod.rs