1个稳定版本

新版本 24.0.0 2024年8月20日

#28 in #standalone


用于 wasmtime-cli

Apache-2.0 WITH LLVM-exception

2.5MB
40K SLoC

Wasmtime的wasi-runtime-config实现

此crate提供了Wasmtime对wasi-runtime-config API的宿主实现。使用此crate,运行时可以运行调用wasi-runtime-config API的组件,并为组件提供配置变量。

示例

此crate的使用与wasi:cliwasi:http等WASI API实现非常相似。

一个常见的场景是在wasi:cli组件中获取运行时传递的配置。以下是一个独立示例,展示如何完成所有这些操作:

use wasmtime::{
    component::{Linker, ResourceTable},
    Config, Engine, Result, Store,
};
use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi_runtime_config::{WasiRuntimeConfig, WasiRuntimeConfigVariables};

#[tokio::main]
async fn main() -> Result<()> {
    let mut config = Config::new();
    config.async_support(true);
    let engine = Engine::new(&config)?;

    let mut store = Store::new(&engine, Ctx {
        table: ResourceTable::new(),
        wasi_ctx: WasiCtxBuilder::new().build(),
        wasi_runtime_config_vars: WasiRuntimeConfigVariables::from_iter(vec![
            ("config_key1", "value1"),
            ("config_key2", "value2"),
        ]),
    });

    let mut linker = Linker::<Ctx>::new(&engine);
    wasmtime_wasi::add_to_linker_async(&mut linker)?;
    // add `wasi-runtime-config` world's interfaces to the linker
    wasmtime_wasi_runtime_config::add_to_linker(&mut linker, |h: &mut Ctx| {
        WasiRuntimeConfig::from(&h.wasi_runtime_config_vars)
    })?;

    // ... use `linker` to instantiate within `store` ...

    Ok(())
}

struct Ctx {
    table: ResourceTable,
    wasi_ctx: WasiCtx,
    wasi_runtime_config_vars: WasiRuntimeConfigVariables,
}

impl WasiView for Ctx {
    fn table(&mut self) -> &mut ResourceTable { &mut self.table }
    fn ctx(&mut self) -> &mut WasiCtx { &mut self.wasi_ctx }
}

依赖

~20–31MB
~589K SLoC