Move Webassembly objects to Store and remove Context

Co-authored-by: ptitSeb <sebastien.chev@gmail.com>
Co-authored-by: Manos Pitsidianakis <manos@wasmer.io>
This commit is contained in:
Syrus Akbary
2022-07-08 15:33:29 -07:00
committed by Manos Pitsidianakis
parent b5ae6399ce
commit a419ccdf52
214 changed files with 5800 additions and 5759 deletions

View File

@@ -7,7 +7,7 @@ use crate::warning;
use anyhow::{anyhow, Context, Result};
use std::path::PathBuf;
use std::str::FromStr;
use wasmer::Context as WasmerContext;
use wasmer::FunctionEnv;
use wasmer::*;
#[cfg(feature = "cache")]
use wasmer_cache::{Cache, FileSystemCache, Hash};
@@ -97,21 +97,17 @@ impl Run {
})
}
fn inner_run<T>(&self, mut ctx: WasmerContext<T>, instance: Instance) -> Result<()> {
let module = self.get_module()?;
fn inner_module_run(&self, mut store: Store, instance: Instance) -> Result<()> {
// If this module exports an _initialize function, run that first.
if let Ok(initialize) = instance.exports.get_function("_initialize") {
initialize
.call(&mut ctx, &[])
.call(&mut store, &[])
.with_context(|| "failed to run _initialize function")?;
}
// Do we want to invoke a function?
if let Some(ref invoke) = self.invoke {
let imports = imports! {};
let instance = Instance::new(&mut ctx, &module, &imports)?;
let result =
self.invoke_function(&mut ctx.as_context_mut(), &instance, invoke, &self.args)?;
let result = self.invoke_function(&mut store, &instance, invoke, &self.args)?;
println!(
"{}",
result
@@ -122,7 +118,7 @@ impl Run {
);
} else {
let start: Function = self.try_find_function(&instance, "_start", &[])?;
let result = start.call(&mut ctx, &[]);
let result = start.call(&mut store, &[]);
#[cfg(feature = "wasi")]
self.wasi.handle_result(result)?;
#[cfg(not(feature = "wasi"))]
@@ -133,7 +129,7 @@ impl Run {
}
fn inner_execute(&self) -> Result<()> {
let module = self.get_module()?;
let (mut store, module) = self.get_store_module()?;
#[cfg(feature = "emscripten")]
{
use wasmer_emscripten::{
@@ -143,18 +139,14 @@ impl Run {
// TODO: refactor this
if is_emscripten_module(&module) {
// create an EmEnv with default global
let mut ctx = WasmerContext::new(module.store(), EmEnv::new());
let mut emscripten_globals = EmscriptenGlobals::new(ctx.as_context_mut(), &module)
let env = FunctionEnv::new(&mut store, EmEnv::new());
let mut emscripten_globals = EmscriptenGlobals::new(&mut store, &env, &module)
.map_err(|e| anyhow!("{}", e))?;
ctx.data_mut()
env.as_mut(&mut store)
.set_data(&emscripten_globals.data, Default::default());
let import_object =
generate_emscripten_env(&mut ctx.as_context_mut(), &mut emscripten_globals);
let mut instance = match Instance::new(
&mut ctx.as_context_mut(),
&module,
&import_object,
) {
generate_emscripten_env(&mut store, &env, &mut emscripten_globals);
let mut instance = match Instance::new(&mut store, &module, &import_object) {
Ok(instance) => instance,
Err(e) => {
let err: Result<(), _> = Err(e);
@@ -170,7 +162,7 @@ impl Run {
run_emscripten_instance(
&mut instance,
ctx.as_context_mut(),
env.into_mut(&mut store),
&mut emscripten_globals,
if let Some(cn) = &self.command_name {
cn
@@ -219,37 +211,35 @@ impl Run {
.map(|f| f.to_string_lossy().to_string())
})
.unwrap_or_default();
let (ctx, instance) = self
let (_ctx, instance) = self
.wasi
.instantiate(&module, program_name, self.args.clone())
.instantiate(&mut store, &module, program_name, self.args.clone())
.with_context(|| "failed to instantiate WASI module")?;
self.inner_run(ctx, instance)
self.inner_module_run(store, instance)
}
// not WASI
_ => {
let mut ctx = WasmerContext::new(module.store(), ());
let instance = Instance::new(&mut ctx, &module, &imports! {})?;
self.inner_run(ctx, instance)
let instance = Instance::new(&mut store, &module, &imports! {})?;
self.inner_module_run(store, instance)
}
}
};
#[cfg(not(feature = "wasi"))]
let ret = {
let mut ctx = WasmerContext::new(module.store(), ());
let instance = Instance::new(&mut ctx, &module, &imports! {})?;
let instance = Instance::new(&mut store, &module, &imports! {})?;
self.inner_run(ctx, instance)
};
ret
}
fn get_module(&self) -> Result<Module> {
fn get_store_module(&self) -> Result<(Store, Module)> {
let contents = std::fs::read(self.path.clone())?;
if wasmer_compiler::UniversalArtifact::is_deserializable(&contents) {
let engine = wasmer_compiler::Universal::headless().engine();
let store = Store::new_with_engine(&engine);
let module = unsafe { Module::deserialize_from_file(&store, &self.path)? };
return Ok(module);
return Ok((store, module));
}
let (store, compiler_type) = self.store.get_store()?;
#[cfg(feature = "cache")]
@@ -270,7 +260,7 @@ impl Run {
// We set the name outside the cache, to make sure we dont cache the name
module.set_name(&self.path.file_name().unwrap_or_default().to_string_lossy());
Ok(module)
Ok((store, module))
}
#[cfg(feature = "cache")]
@@ -375,7 +365,7 @@ impl Run {
fn invoke_function(
&self,
ctx: &mut impl AsContextMut,
ctx: &mut impl AsStoreMut,
instance: &Instance,
invoke: &str,
args: &[String],

View File

@@ -2,7 +2,7 @@ use crate::utils::{parse_envvar, parse_mapdir};
use anyhow::Result;
use std::collections::BTreeSet;
use std::path::PathBuf;
use wasmer::{AsContextMut, Context, Instance, Module, RuntimeError, Value};
use wasmer::{AsStoreMut, FunctionEnv, Instance, Module, RuntimeError, Value};
use wasmer_wasi::{
get_wasi_versions, import_object_for_all_wasi_versions, is_wasix_module, WasiEnv, WasiError,
WasiState, WasiVersion,
@@ -78,10 +78,11 @@ impl Wasi {
/// Helper function for instantiating a module with Wasi imports for the `Run` command.
pub fn instantiate(
&self,
store: &mut impl AsStoreMut,
module: &Module,
program_name: String,
args: Vec<String>,
) -> Result<(Context<WasiEnv>, Instance)> {
) -> Result<(FunctionEnv<WasiEnv>, Instance)> {
let args = args.iter().cloned().map(|arg| arg.into_bytes());
let mut wasi_state_builder = WasiState::new(program_name);
@@ -99,17 +100,16 @@ impl Wasi {
}
}
let wasi_env = wasi_state_builder.finalize()?;
wasi_env.state.fs.is_wasix.store(
let wasi_env = wasi_state_builder.finalize(store)?;
wasi_env.env.as_mut(store).state.fs.is_wasix.store(
is_wasix_module(module),
std::sync::atomic::Ordering::Release,
);
let mut ctx = Context::new(module.store(), wasi_env);
let import_object = import_object_for_all_wasi_versions(&mut ctx.as_context_mut());
let instance = Instance::new(&mut ctx, module, &import_object)?;
let import_object = import_object_for_all_wasi_versions(store, &wasi_env.env);
let instance = Instance::new(store, module, &import_object)?;
let memory = instance.exports.get_memory("memory")?;
ctx.data_mut().set_memory(memory.clone());
Ok((ctx, instance))
wasi_env.data_mut(store).set_memory(memory.clone());
Ok((wasi_env.env, instance))
}
/// Helper function for handling the result of a Wasi _start function.

View File

@@ -3,7 +3,6 @@ use crate::store::StoreOptions;
use anyhow::{Context, Result};
use std::path::PathBuf;
use structopt::StructOpt;
use wasmer::Context as WasmerContext;
use wasmer_wast::Wast as WastSpectest;
#[derive(Debug, StructOpt)]
@@ -29,8 +28,7 @@ impl Wast {
}
fn inner_execute(&self) -> Result<()> {
let (store, _compiler_name) = self.store.get_store()?;
let ctx = WasmerContext::new(&store, ());
let mut wast = WastSpectest::new_with_spectest(ctx);
let mut wast = WastSpectest::new_with_spectest(store);
wast.fail_fast = self.fail_fast;
wast.run_file(&self.path).with_context(|| "tests failed")?;
eprintln!("Wast tests succeeded for `{}`.", self.path.display());