make wasmtime_wasi::Wasi a struct which does both snapshots!
This commit is contained in:
@@ -1,7 +1,32 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
pub use wasi_common::{
|
||||
Error, FdFlags, FileCaps, Filestat, OFlags, ReaddirCursor, ReaddirEntity, SystemTimeSpec,
|
||||
WasiCtx, WasiCtxBuilder, WasiDir, WasiFile,
|
||||
};
|
||||
use wasmtime::{Linker, Store};
|
||||
|
||||
pub struct Wasi {
|
||||
preview_1: snapshots::preview_1::Wasi,
|
||||
preview_0: snapshots::preview_0::Wasi,
|
||||
}
|
||||
|
||||
impl Wasi {
|
||||
pub fn new(store: &Store, context: WasiCtx) -> Self {
|
||||
let context = Rc::new(RefCell::new(context));
|
||||
let preview_1 = snapshots::preview_1::Wasi::new(store, context.clone());
|
||||
let preview_0 = snapshots::preview_0::Wasi::new(store, context);
|
||||
Self {
|
||||
preview_1,
|
||||
preview_0,
|
||||
}
|
||||
}
|
||||
pub fn add_to_linker(&self, linker: &mut Linker) -> Result<(), anyhow::Error> {
|
||||
self.preview_1.add_to_linker(linker)?;
|
||||
self.preview_0.add_to_linker(linker)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub mod snapshots {
|
||||
pub mod preview_1 {
|
||||
|
||||
@@ -13,8 +13,7 @@ use std::{
|
||||
use structopt::{clap::AppSettings, StructOpt};
|
||||
use wasi_cap_std_sync::WasiCtxBuilder;
|
||||
use wasmtime::{Engine, Func, Linker, Module, Store, Trap, Val, ValType};
|
||||
use wasmtime_wasi::snapshots::preview_0::Wasi as WasiSnapshot0;
|
||||
use wasmtime_wasi::snapshots::preview_1::Wasi as WasiSnapshot1;
|
||||
use wasmtime_wasi::Wasi;
|
||||
|
||||
#[cfg(feature = "wasi-nn")]
|
||||
use wasmtime_wasi_nn::{WasiNn, WasiNnCtx};
|
||||
@@ -352,9 +351,6 @@ fn populate_with_wasi(
|
||||
argv: &[String],
|
||||
vars: &[(String, String)],
|
||||
) -> Result<()> {
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
// Add the current snapshot to the linker.
|
||||
let mut builder = WasiCtxBuilder::new();
|
||||
builder = builder.inherit_stdio().args(argv)?.envs(vars)?;
|
||||
@@ -363,10 +359,7 @@ fn populate_with_wasi(
|
||||
builder = builder.preopened_dir(dir, name)?;
|
||||
}
|
||||
|
||||
let cx = Rc::new(RefCell::new(builder.build()?));
|
||||
|
||||
WasiSnapshot1::new(linker.store(), cx.clone()).add_to_linker(linker)?;
|
||||
WasiSnapshot0::new(linker.store(), cx).add_to_linker(linker)?;
|
||||
Wasi::new(linker.store(), builder.build()?).add_to_linker(linker)?;
|
||||
|
||||
#[cfg(feature = "wasi-nn")]
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user