Implement RFC 11: Redesigning Wasmtime's APIs (#2897)

Implement Wasmtime's new API as designed by RFC 11. This is quite a large commit which has had lots of discussion externally, so for more information it's best to read the RFC thread and the PR thread.
This commit is contained in:
Alex Crichton
2021-06-03 09:10:53 -05:00
committed by GitHub
parent a5a28b1c5b
commit 7a1b7cdf92
233 changed files with 13349 additions and 11997 deletions

View File

@@ -1,8 +1,8 @@
use anyhow::Context;
use std::path::Path;
use wasi_common::pipe::WritePipe;
use wasmtime::{Linker, Module, Store};
use wasmtime_wasi::sync::{Wasi, WasiCtxBuilder};
use wasmtime::{Engine, Linker, Module, Store};
use wasmtime_wasi::sync::{add_to_linker, WasiCtxBuilder};
pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> anyhow::Result<()> {
run(data, bin_name, workspace, false)
@@ -25,7 +25,10 @@ fn run(
let stderr = WritePipe::new_in_memory();
let r = {
let store = Store::default();
let engine = Engine::default();
let module = Module::new(&engine, &data).context("failed to create wasm module")?;
let mut linker = Linker::new(&engine);
add_to_linker(&mut linker, |cx| cx)?;
// Create our wasi context.
// Additionally register any preopened directories if we have them.
@@ -53,16 +56,10 @@ fn run(
// cap-std-sync does not yet support the sync family of fdflags
builder = builder.env("NO_FDFLAGS_SYNC_SUPPORT", "1")?;
let wasi = Wasi::new(&store, builder.build());
let mut linker = Linker::new(&store);
wasi.add_to_linker(&mut linker)?;
let module = Module::new(store.engine(), &data).context("failed to create wasm module")?;
let instance = linker.instantiate(&module)?;
let start = instance.get_typed_func::<(), ()>("_start")?;
start.call(()).map_err(anyhow::Error::from)
let mut store = Store::new(&engine, builder.build());
let instance = linker.instantiate(&mut store, &module)?;
let start = instance.get_typed_func::<(), (), _>(&mut store, "_start")?;
start.call(&mut store, ()).map_err(anyhow::Error::from)
};
match r {

View File

@@ -2,7 +2,7 @@ use anyhow::Context;
use std::path::Path;
use wasi_common::pipe::WritePipe;
use wasmtime::{Config, Engine, Linker, Module, Store};
use wasmtime_wasi::tokio::{Wasi, WasiCtxBuilder};
use wasmtime_wasi::tokio::{add_to_linker, WasiCtxBuilder};
pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> anyhow::Result<()> {
run(data, bin_name, workspace, false)
@@ -31,9 +31,10 @@ fn run(
.block_on(async move {
let mut config = Config::new();
config.async_support(true);
Wasi::add_to_config(&mut config);
let engine = Engine::new(&config)?;
let store = Store::new(&engine);
let module = Module::new(&engine, &data).context("failed to create wasm module")?;
let mut linker = Linker::new(&engine);
add_to_linker(&mut linker, |cx| cx)?;
// Create our wasi context.
let mut builder = WasiCtxBuilder::new();
@@ -62,15 +63,14 @@ fn run(
// does not.
builder = builder.env("NO_FDFLAGS_SYNC_SUPPORT", "1")?;
Wasi::set_context(&store, builder.build())
.map_err(|_| anyhow::anyhow!("wasi set_context failed"))?;
let mut store = Store::new(&engine, builder.build());
let module =
Module::new(store.engine(), &data).context("failed to create wasm module")?;
let linker = Linker::new(&store);
let instance = linker.instantiate_async(&module).await?;
let start = instance.get_typed_func::<(), ()>("_start")?;
start.call_async(()).await.map_err(anyhow::Error::from)
let instance = linker.instantiate_async(&mut store, &module).await?;
let start = instance.get_typed_func::<(), (), _>(&mut store, "_start")?;
start
.call_async(&mut store, ())
.await
.map_err(anyhow::Error::from)
});
match r {