diff --git a/examples/early_exit.rs b/examples/early_exit.rs index ca59eca3c14..4bfbc6bad3e 100644 --- a/examples/early_exit.rs +++ b/examples/early_exit.rs @@ -59,7 +59,7 @@ fn main() -> anyhow::Result<()> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine()); - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. @@ -74,7 +74,7 @@ fn main() -> anyhow::Result<()> { // Create an import object. let import_object = imports! { "env" => { - "early_exit" => Function::new_native(&mut store, &ctx, early_exit), + "early_exit" => Function::new_native(&mut store, &env, early_exit), } }; diff --git a/examples/engine_headless.rs b/examples/engine_headless.rs index 1b552cba64a..16aabfb5b9b 100644 --- a/examples/engine_headless.rs +++ b/examples/engine_headless.rs @@ -107,7 +107,7 @@ fn main() -> Result<(), Box> { // We create a headless Universal engine. let engine = Universal::headless().engine(); let mut store = Store::new_with_engine(&engine); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Deserializing module..."); // Here we go. diff --git a/examples/errors.rs b/examples/errors.rs index c5af0919f52..d28698f8051 100644 --- a/examples/errors.rs +++ b/examples/errors.rs @@ -40,7 +40,7 @@ fn main() -> Result<(), Box> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/exports_function.rs b/examples/exports_function.rs index fe4f1677a5f..e703976a44c 100644 --- a/examples/exports_function.rs +++ b/examples/exports_function.rs @@ -41,7 +41,7 @@ fn main() -> Result<(), Box> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/exports_global.rs b/examples/exports_global.rs index 49466ac47e9..4b8f765573e 100644 --- a/examples/exports_global.rs +++ b/examples/exports_global.rs @@ -41,7 +41,7 @@ fn main() -> Result<(), Box> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/exports_memory.rs b/examples/exports_memory.rs index ada6243dab3..9d0f74726da 100644 --- a/examples/exports_memory.rs +++ b/examples/exports_memory.rs @@ -38,7 +38,7 @@ fn main() -> Result<(), Box> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/features.rs b/examples/features.rs index ba4db2942d7..292c5c2cebc 100644 --- a/examples/features.rs +++ b/examples/features.rs @@ -40,7 +40,7 @@ fn main() -> anyhow::Result<()> { // Now, let's define the store, and compile the module. let mut store = Store::new_with_engine(&engine.engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let module = Module::new(&store, wasm_bytes)?; // Finally, let's instantiate the module, and execute something diff --git a/examples/imports_exports.rs b/examples/imports_exports.rs index c667ef6a76f..eb154317e3d 100644 --- a/examples/imports_exports.rs +++ b/examples/imports_exports.rs @@ -45,7 +45,7 @@ fn main() -> Result<(), Box> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. @@ -60,7 +60,7 @@ fn main() -> Result<(), Box> { // covered in more detail in other examples. println!("Creating the imported function..."); let host_function_signature = FunctionType::new(vec![], vec![Type::I32]); - let host_function = Function::new(&mut store, &ctx, &host_function_signature, |_ctx, _args| { + let host_function = Function::new(&mut store, &env, &host_function_signature, |_ctx, _args| { Ok(vec![Value::I32(42)]) }); diff --git a/examples/imports_function_env.rs b/examples/imports_function_env.rs index c6d5dfe062e..7eee4b02245 100644 --- a/examples/imports_function_env.rs +++ b/examples/imports_function_env.rs @@ -88,7 +88,7 @@ fn main() -> Result<(), Box> { *counter_ref } - let mut ctx = FunctionEnv::new( + let mut env = FunctionEnv::new( &mut store, Env { counter: shared_counter.clone(), @@ -98,8 +98,8 @@ fn main() -> Result<(), Box> { // Create an import object. let import_object = imports! { "env" => { - "get_counter" => Function::new_native(&mut store, &ctx, get_counter), - "add_to_counter" => Function::new_native(&mut store, &ctx, add_to_counter), + "get_counter" => Function::new_native(&mut store, &env, get_counter), + "add_to_counter" => Function::new_native(&mut store, &env, add_to_counter), } }; diff --git a/examples/imports_global.rs b/examples/imports_global.rs index 65665959461..87e9318b711 100644 --- a/examples/imports_global.rs +++ b/examples/imports_global.rs @@ -41,7 +41,7 @@ fn main() -> Result<(), Box> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/instance.rs b/examples/instance.rs index ddd4ffe786d..2e102ea65bd 100644 --- a/examples/instance.rs +++ b/examples/instance.rs @@ -40,7 +40,7 @@ fn main() -> Result<(), Box> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/memory.rs b/examples/memory.rs index 0719fa414b9..1d2b6b7a82d 100644 --- a/examples/memory.rs +++ b/examples/memory.rs @@ -60,7 +60,7 @@ fn main() -> anyhow::Result<()> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/metering.rs b/examples/metering.rs index 71981e31c05..8f16e7adeec 100644 --- a/examples/metering.rs +++ b/examples/metering.rs @@ -71,7 +71,7 @@ fn main() -> anyhow::Result<()> { // We use our previously create compiler configuration // with the Universal engine. let mut store = Store::new_with_engine(&Universal::new(compiler_config).engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/table.rs b/examples/table.rs index 190045343c8..2250ba1a194 100644 --- a/examples/table.rs +++ b/examples/table.rs @@ -53,7 +53,7 @@ fn main() -> anyhow::Result<()> { // We set up our store with an engine and a compiler. let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine()); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); // Then compile our Wasm. let module = Module::new(&store, wasm_bytes)?; let import_object = imports! {}; @@ -89,7 +89,7 @@ fn main() -> anyhow::Result<()> { // == Setting elements in a table == // We first construct a `Function` over our host_callback. - let func = Function::new_native(&mut store, &ctx, host_callback); + let func = Function::new_native(&mut store, &env, host_callback); // And set table index 1 of that table to the host_callback `Function`. guest_table.set(&mut store, 1, func.into())?; @@ -103,7 +103,7 @@ fn main() -> anyhow::Result<()> { // == Growing a table == // We again construct a `Function` over our host_callback. - let func = Function::new_native(&mut store, &ctx, host_callback); + let func = Function::new_native(&mut store, &env, host_callback); // And grow the table by 3 elements, filling in our host_callback in all the // new elements of the table. @@ -134,7 +134,7 @@ fn main() -> anyhow::Result<()> { assert_eq!(result, 18); // Now overwrite index 0 with our host_callback. - let func = Function::new_native(&mut store, &ctx, host_callback); + let func = Function::new_native(&mut store, &env, host_callback); guest_table.set(&mut store, 0, func.into())?; // And verify that it does what we expect. let result = call_via_table.call(&mut store, 0, 2, 7)?; diff --git a/examples/tunables_limit_memory.rs b/examples/tunables_limit_memory.rs index c04073f15f6..10e7c25bd92 100644 --- a/examples/tunables_limit_memory.rs +++ b/examples/tunables_limit_memory.rs @@ -146,7 +146,7 @@ fn main() -> Result<(), Box> { // Create a store, that holds the engine and our custom tunables let mut store = Store::new_with_tunables(&engine, tunables); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); let module = Module::new(&store, wasm_bytes)?; diff --git a/lib/api/src/js/error.rs b/lib/api/src/js/error.rs index 5a25c99d88e..f334b4a1cec 100644 --- a/lib/api/src/js/error.rs +++ b/lib/api/src/js/error.rs @@ -163,10 +163,10 @@ pub enum InstantiationError { #[cfg_attr(feature = "std", error(transparent))] Start(RuntimeError), - /// Import from a different [`Context`]. - /// This error occurs when an import from a different context is used. - #[cfg_attr(feature = "std", error("cannot mix imports from different contexts"))] - BadContext, + /// Import from a different [`Store`]. + /// This error occurs when an import from a different store is used. + #[cfg_attr(feature = "std", error("cannot mix imports from different stores"))] + DifferentStores, /// A generic error occured while invoking API functions #[cfg_attr(feature = "std", error(transparent))] diff --git a/lib/api/src/js/externals/function.rs b/lib/api/src/js/externals/function.rs index b53140f56f9..fa6ee4aa4e2 100644 --- a/lib/api/src/js/externals/function.rs +++ b/lib/api/src/js/externals/function.rs @@ -183,7 +183,7 @@ impl Function { /// ``` pub fn new_native( store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, func: F, ) -> Self where @@ -242,13 +242,13 @@ impl Function { /// ``` /// # use wasmer::{Function, FunctionEnv, FunctionEnvMut, Store, Type}; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// - /// let f = Function::new_native(&store, &ctx, sum); + /// let f = Function::new_native(&store, &env, sum); /// /// assert_eq!(f.param_arity(&store), 2); /// ``` @@ -263,13 +263,13 @@ impl Function { /// ``` /// # use wasmer::{Function, FunctionEnv, FunctionEnvMut, Store, Type}; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// - /// let f = Function::new_native(&store, &ctx, sum); + /// let f = Function::new_native(&store, &env, sum); /// /// assert_eq!(f.result_arity(&store), 1); /// ``` @@ -314,7 +314,7 @@ impl Function { let arr = js_sys::Array::new_with_length(params.len() as u32); // let raw_ctx = ctx.as_raw() as *mut u8; - // let mut ctx = unsafe { FunctionEnvMut::from_raw(raw_ctx as *mut StoreInner<()>) }; + // let mut env = unsafe { FunctionEnvMut::from_raw(raw_ctx as *mut StoreInner<()>) }; for (i, param) in params.iter().enumerate() { let js_value = param.as_jsvalue(&store.as_store_ref()); diff --git a/lib/api/src/js/module.rs b/lib/api/src/js/module.rs index 026e370feb3..209f46dfd46 100644 --- a/lib/api/src/js/module.rs +++ b/lib/api/src/js/module.rs @@ -224,13 +224,14 @@ impl Module { store: &mut impl AsStoreMut, imports: &Imports, ) -> Result<(StoreHandle, Vec), RuntimeError> { - // Ensure all imports come from the same context. + // Ensure all imports come from the same store. if imports .into_iter() .any(|(_, import)| !import.is_from_store(store)) { - // FIXME is RuntimeError::User appropriate? - return Err(RuntimeError::user(Box::new(InstantiationError::BadContext))); + return Err(RuntimeError::user(Box::new( + InstantiationError::DifferentStores, + ))); } let imports_object = js_sys::Object::new(); let mut import_externs: Vec = vec![]; diff --git a/lib/api/src/sys/externals/function.rs b/lib/api/src/sys/externals/function.rs index 88fd4f25e15..7c21ee747e7 100644 --- a/lib/api/src/sys/externals/function.rs +++ b/lib/api/src/sys/externals/function.rs @@ -52,11 +52,11 @@ impl Function { /// ``` /// # use wasmer::{Function, FunctionEnv, FunctionType, Type, Store, Value}; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # /// let signature = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]); /// - /// let f = Function::new(&mut store, &ctx, &signature, |_ctx, args| { + /// let f = Function::new(&mut store, &env, &signature, |_ctx, args| { /// let sum = args[0].unwrap_i32() + args[1].unwrap_i32(); /// Ok(vec![Value::I32(sum)]) /// }); @@ -67,18 +67,18 @@ impl Function { /// ``` /// # use wasmer::{Function, FunctionEnv, FunctionType, Type, Store, Value}; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # /// const I32_I32_TO_I32: ([Type; 2], [Type; 1]) = ([Type::I32, Type::I32], [Type::I32]); /// - /// let f = Function::new(&mut store, &ctx, I32_I32_TO_I32, |_ctx, args| { + /// let f = Function::new(&mut store, &env, I32_I32_TO_I32, |_ctx, args| { /// let sum = args[0].unwrap_i32() + args[1].unwrap_i32(); /// Ok(vec![Value::I32(sum)]) /// }); /// ``` pub fn new( store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, ty: FT, func: F, ) -> Self @@ -91,7 +91,7 @@ impl Function { { let function_type = ty.into(); let func_ty = function_type.clone(); - let func_env = ctx.clone(); + let func_env = env.clone(); let raw_store = store.as_store_mut().as_raw() as *mut u8; let wrapper = move |values_vec: *mut RawValue| -> Result<(), RuntimeError> { unsafe { @@ -169,17 +169,17 @@ impl Function { /// ``` /// # use wasmer::{Store, Function, FunctionEnv, FunctionEnvMut}; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// - /// let f = Function::new_native(&mut store, &ctx, sum); + /// let f = Function::new_native(&mut store, &env, sum); /// ``` pub fn new_native( store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, func: F, ) -> Self where @@ -191,7 +191,7 @@ impl Function { let host_data = Box::new(StaticFunction { raw_store: store.as_store_mut().as_raw() as *mut u8, - env: ctx.clone(), + env: env.clone(), func, }); let function_type = FunctionType::new(Args::wasm_types(), Rets::wasm_types()); @@ -230,13 +230,13 @@ impl Function { /// ``` /// # use wasmer::{Function, FunctionEnv, FunctionEnvMut, Store, Type}; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// - /// let f = Function::new_native(&mut store, &ctx, sum); + /// let f = Function::new_native(&mut store, &env, sum); /// /// assert_eq!(f.ty(&mut store).params(), vec![Type::I32, Type::I32]); /// assert_eq!(f.ty(&mut store).results(), vec![Type::I32]); @@ -330,13 +330,13 @@ impl Function { /// ``` /// # use wasmer::{Function, FunctionEnv, FunctionEnvMut, Store, Type}; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// - /// let f = Function::new_native(&mut store, &ctx, sum); + /// let f = Function::new_native(&mut store, &env, sum); /// /// assert_eq!(f.param_arity(&mut store), 2); /// ``` @@ -351,13 +351,13 @@ impl Function { /// ``` /// # use wasmer::{Function, FunctionEnv, FunctionEnvMut, Store, Type}; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// - /// let f = Function::new_native(&mut store, &ctx, sum); + /// let f = Function::new_native(&mut store, &env, sum); /// /// assert_eq!(f.result_arity(&mut store), 1); /// ``` @@ -379,7 +379,7 @@ impl Function { /// # use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, Type, Value}; /// # use wasmer::FunctionEnv; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # let wasm_bytes = wat2wasm(r#" /// # (module /// # (func (export "sum") (param $x i32) (param $y i32) (result i32) @@ -450,7 +450,7 @@ impl Function { /// # use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, Type, TypedFunction, Value}; /// # use wasmer::FunctionEnv; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # let wasm_bytes = wat2wasm(r#" /// # (module /// # (func (export "sum") (param $x i32) (param $y i32) (result i32) @@ -478,7 +478,7 @@ impl Function { /// # use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, Type, TypedFunction, Value}; /// # use wasmer::FunctionEnv; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # let wasm_bytes = wat2wasm(r#" /// # (module /// # (func (export "sum") (param $x i32) (param $y i32) (result i32) @@ -504,7 +504,7 @@ impl Function { /// # use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, Type, TypedFunction, Value}; /// # use wasmer::FunctionEnv; /// # let mut store = Store::default(); - /// # let ctx = FunctionEnv::new(&mut store, ()); + /// # let env = FunctionEnv::new(&mut store, ()); /// # let wasm_bytes = wat2wasm(r#" /// # (module /// # (func (export "sum") (param $x i32) (param $y i32) (result i32) @@ -1279,7 +1279,7 @@ mod inner { #[test] fn test_from_array() { let mut store = Store::default(); - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); assert_eq!(<()>::from_array(&mut ctx, []), ()); assert_eq!(::from_array(&mut ctx, [RawValue{i32: 1}]), (1i32)); assert_eq!(<(i32, i64)>::from_array(&mut ctx, [RawValue{i32:1}, RawValue{i64:2}]), (1i32, 2i64)); @@ -1297,7 +1297,7 @@ mod inner { #[test] fn test_into_array() { let mut store = Store::default(); - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); assert_eq!(().into_array(&mut ctx), [0i128; 0]); assert_eq!((1i32).into_array(&mut ctx), [1i32]); assert_eq!((1i32, 2i64).into_array(&mut ctx), [RawValue{i32: 1}, RawValue{i64: 2}]); @@ -1317,7 +1317,7 @@ mod inner { #[test] fn test_from_c_struct() { let mut store = Store::default(); - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); assert_eq!(<()>::from_c_struct(&mut ctx, S0()), ()); assert_eq!(::from_c_struct(&mut ctx, S1(1)), (1i32)); assert_eq!(<(i32, i64)>::from_c_struct(&mut ctx, S2(1, 2)), (1i32, 2i64)); @@ -1378,7 +1378,7 @@ mod inner { #[test] fn test_function_types() { let mut store = Store::default(); - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); use wasmer_types::FunctionType; assert_eq!( StaticFunction::new(func).ty(&mut store), diff --git a/lib/api/src/sys/externals/memory.rs b/lib/api/src/sys/externals/memory.rs index ae682740452..7219e62f727 100644 --- a/lib/api/src/sys/externals/memory.rs +++ b/lib/api/src/sys/externals/memory.rs @@ -110,7 +110,10 @@ impl Memory { #[allow(clippy::mut_from_ref)] #[doc(hidden)] pub unsafe fn data_unchecked_mut(&self, ctx: &impl AsStoreRef) -> &mut [u8] { - slice::from_raw_parts_mut(self.buffer(ctx).base, self.buffer(ctx).len) + slice::from_raw_parts_mut( + self.buffer(ctx).base, + self.buffer(ctx).len.try_into().unwrap(), + ) } /// Returns the size (in [`Pages`]) of the `Memory`. diff --git a/lib/api/src/sys/imports.rs b/lib/api/src/sys/imports.rs index c567dab17a0..a93171a3115 100644 --- a/lib/api/src/sys/imports.rs +++ b/lib/api/src/sys/imports.rs @@ -17,9 +17,9 @@ use wasmer_types::ImportError; /// # Usage: /// ```no_run /// use wasmer::{Store, Exports, Module, Instance, imports, Imports, Function, FunctionEnv, FunctionEnvMut}; -/// # fn foo_test(mut ctx: FunctionEnv<()>, mut store: &mut Store, module: Module) { +/// # fn foo_test(mut env: FunctionEnv<()>, mut store: &mut Store, module: Module) { /// -/// let host_fn = Function::new_native(&mut store, &ctx, foo); +/// let host_fn = Function::new_native(&mut store, &env, foo); /// let import_object: Imports = imports! { /// "env" => { /// "foo" => host_fn, @@ -303,7 +303,7 @@ mod test { use crate::sys::FunctionEnvMut; let mut store: Store = Default::default(); - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); fn func(_ctx: FunctionEnvMut<()>, arg: i32) -> i32 { arg + 1 @@ -311,42 +311,42 @@ mod test { let _ = imports! { "env" => { - "func" => Function::new_native(&mut store, &ctx, func), + "func" => Function::new_native(&mut store, &env, func), }, }; let _ = imports! { "env" => { - "func" => Function::new_native(&mut store, &ctx, func), + "func" => Function::new_native(&mut store, &env, func), } }; let _ = imports! { "env" => { - "func" => Function::new_native(&mut store, &ctx, func), + "func" => Function::new_native(&mut store, &env, func), }, "abc" => { - "def" => Function::new_native(&mut store, &ctx, func), + "def" => Function::new_native(&mut store, &env, func), } }; let _ = imports! { "env" => { - "func" => Function::new_native(&mut store, &ctx, func) + "func" => Function::new_native(&mut store, &env, func) }, }; let _ = imports! { "env" => { - "func" => Function::new_native(&mut store, &ctx, func) + "func" => Function::new_native(&mut store, &env, func) } }; let _ = imports! { "env" => { - "func1" => Function::new_native(&mut store, &ctx, func), - "func2" => Function::new_native(&mut store, &ctx, func) + "func1" => Function::new_native(&mut store, &env, func), + "func2" => Function::new_native(&mut store, &env, func) } }; let _ = imports! { "env" => { - "func1" => Function::new_native(&mut store, &ctx, func), - "func2" => Function::new_native(&mut store, &ctx, func), + "func1" => Function::new_native(&mut store, &env, func), + "func2" => Function::new_native(&mut store, &env, func), } }; } diff --git a/lib/api/src/sys/instance.rs b/lib/api/src/sys/instance.rs index 7e0102a17c5..bcc4d6beb55 100644 --- a/lib/api/src/sys/instance.rs +++ b/lib/api/src/sys/instance.rs @@ -62,10 +62,10 @@ pub enum InstantiationError { #[error("missing required CPU features: {0:?}")] CpuFeature(String), - /// Import from a different [`Context`]. - /// This error occurs when an import from a different context is used. - #[error("cannot mix imports from different contexts")] - BadContext, + /// Import from a different [`Store`]. + /// This error occurs when an import from a different store is used. + #[error("cannot mix imports from different stores")] + DifferentStores, } impl From for InstantiationError { diff --git a/lib/api/src/sys/module.rs b/lib/api/src/sys/module.rs index 9796ea944d1..ccd7c1d4299 100644 --- a/lib/api/src/sys/module.rs +++ b/lib/api/src/sys/module.rs @@ -291,7 +291,7 @@ impl Module { // Ensure all imports come from the same context. for import in imports { if !import.is_from_store(store) { - return Err(InstantiationError::BadContext); + return Err(InstantiationError::DifferentStores); } } let mut store_mut = store.as_store_mut(); diff --git a/lib/api/tests/js_externals.rs b/lib/api/tests/js_externals.rs index 5b34c981084..a17aae4b76d 100644 --- a/lib/api/tests/js_externals.rs +++ b/lib/api/tests/js_externals.rs @@ -6,7 +6,7 @@ mod js { #[wasm_bindgen_test] fn global_new() { let mut store = Store::default(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let global = Global::new(&mut store, Value::I32(10)); assert_eq!( global.ty(&store), @@ -29,7 +29,7 @@ mod js { #[wasm_bindgen_test] fn global_get() { let mut store = Store::default(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let global_i32 = Global::new(&mut store, Value::I32(10)); assert_eq!(global_i32.get(&store), Value::I32(10)); // 64-bit values are not yet fully supported in some versions of Node @@ -46,7 +46,7 @@ mod js { #[wasm_bindgen_test] fn global_set() { let mut store = Store::default(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let global_i32 = Global::new(&mut store, Value::I32(10)); // Set on a constant should error assert!(global_i32.set(&mut store, Value::I32(20)).is_err()); @@ -63,13 +63,13 @@ mod js { #[wasm_bindgen_test] fn table_new() { let mut store = Store::default(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let table_type = TableType { ty: Type::FuncRef, minimum: 0, maximum: None, }; - let f = Function::new_native(&mut store, &ctx, |_: FunctionEnvMut<'_, ()>| {}); + let f = Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, ()>| {}); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f))).unwrap(); assert_eq!(table.ty(&store), table_type); @@ -91,13 +91,13 @@ mod js { // #[ignore] // fn table_get() -> Result<()> { // let mut store = Store::default(); - // let mut ctx = FunctionEnv::new(&mut store, ()); + // let mut env = FunctionEnv::new(&mut store, ()); // let table_type = TableType { // ty: Type::FuncRef, // minimum: 0, // maximum: Some(1), // }; - // let f = Function::new(&mut store, &ctx, |num: i32| num + 1); + // let f = Function::new(&mut store, &env, |num: i32| num + 1); // let table = Table::new(&store, table_type, Value::FuncRef(Some(f.clone())))?; // assert_eq!(*table.ty(&store), table_type); // let _elem = table.get(0).unwrap(); @@ -115,13 +115,13 @@ mod js { // #[test] // fn table_grow() -> Result<()> { // let mut store = Store::default(); - // let mut ctx = FunctionEnv::new(&mut store, ()); + // let mut env = FunctionEnv::new(&mut store, ()); // let table_type = TableType { // ty: Type::FuncRef, // minimum: 0, // maximum: Some(10), // }; - // let f = Function::new(&mut store, &ctx, |num: i32| num + 1); + // let f = Function::new(&mut store, &env, |num: i32| num + 1); // let table = Table::new(&store, table_type, Value::FuncRef(Some(f.clone())))?; // // Growing to a bigger maximum should return None // let old_len = table.grow(12, Value::FuncRef(Some(f.clone()))); @@ -144,7 +144,7 @@ mod js { #[wasm_bindgen_test] fn memory_new() { let mut store = Store::default(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let memory_type = MemoryType { shared: false, minimum: Pages(0), @@ -158,7 +158,7 @@ mod js { #[wasm_bindgen_test] fn memory_grow() { let mut store = Store::default(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let desc = MemoryType::new(Pages(10), Some(Pages(16)), false); let memory = Memory::new(&mut store, desc).unwrap(); @@ -182,21 +182,21 @@ mod js { #[wasm_bindgen_test] fn function_new() { let mut store = Store::default(); - let mut ctx = FunctionEnv::new(&mut store, ()); - let function = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<'_, ()>| {}); + let mut env = FunctionEnv::new(&mut store, ()); + let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<'_, ()>| {}); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![], vec![]) ); let function = - Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<'_, ()>, _a: i32| {}); + Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<'_, ()>, _a: i32| {}); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![Type::I32], vec![]) ); let function = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut<'_, ()>, _a: i32, _b: i64, _c: f32, _d: f64| {}, ); assert_eq!( @@ -204,7 +204,7 @@ mod js { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) ); let function = - Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<'_, ()>| -> i32 { + Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<'_, ()>| -> i32 { 1 }); assert_eq!( @@ -213,7 +213,7 @@ mod js { ); let function = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut<'_, ()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, ); assert_eq!( @@ -229,22 +229,22 @@ mod js { struct MyEnv {} let my_env = MyEnv {}; - let mut ctx = FunctionEnv::new(&mut store, my_env); + let mut env = FunctionEnv::new(&mut store, my_env); - let function = Function::new_native(&mut store, &ctx, |_: FunctionEnvMut<'_, MyEnv>| {}); + let function = Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, MyEnv>| {}); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![], vec![]) ); let function = - Function::new_native(&mut store, &ctx, |_: FunctionEnvMut<'_, MyEnv>, _a: i32| {}); + Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, MyEnv>, _a: i32| {}); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![Type::I32], vec![]) ); let function = Function::new_native( &mut store, - &ctx, + &env, |_: FunctionEnvMut<'_, MyEnv>, _a: i32, _b: i64, _c: f32, _d: f64| {}, ); assert_eq!( @@ -252,7 +252,7 @@ mod js { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) ); let function = - Function::new_native(&mut store, &ctx, |_: FunctionEnvMut<'_, MyEnv>| -> i32 { + Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, MyEnv>| -> i32 { 1 }); assert_eq!( @@ -261,7 +261,7 @@ mod js { ); let function = Function::new_native( &mut store, - &ctx, + &env, |_: FunctionEnvMut<'_, MyEnv>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, ); assert_eq!( @@ -273,13 +273,13 @@ mod js { #[wasm_bindgen_test] fn function_new_dynamic() { let mut store = Store::default(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); // Using &FunctionType signature let function_type = FunctionType::new(vec![], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); @@ -287,7 +287,7 @@ mod js { let function_type = FunctionType::new(vec![Type::I32], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); @@ -296,7 +296,7 @@ mod js { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); @@ -304,7 +304,7 @@ mod js { let function_type = FunctionType::new(vec![], vec![Type::I32]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); @@ -313,7 +313,7 @@ mod js { FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); @@ -323,7 +323,7 @@ mod js { let function_type = ([Type::V128], [Type::I32, Type::F32, Type::F64]); let function = Function::new( &mut store, - &ctx, + &env, function_type, |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); @@ -341,13 +341,13 @@ mod js { struct MyEnv {} let my_env = MyEnv {}; - let mut ctx = FunctionEnv::new(&mut store, my_env); + let mut env = FunctionEnv::new(&mut store, my_env); // Using &FunctionType signature let function_type = FunctionType::new(vec![], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); @@ -355,7 +355,7 @@ mod js { let function_type = FunctionType::new(vec![Type::I32], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); @@ -364,7 +364,7 @@ mod js { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); @@ -372,7 +372,7 @@ mod js { let function_type = FunctionType::new(vec![], vec![Type::I32]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); @@ -381,7 +381,7 @@ mod js { FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); @@ -391,7 +391,7 @@ mod js { let function_type = ([Type::V128], [Type::I32, Type::F32, Type::F64]); let function = Function::new( &mut store, - &ctx, + &env, function_type, |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); @@ -405,15 +405,15 @@ mod js { #[wasm_bindgen_test] fn native_function_works() { let mut store = Store::default(); - let mut ctx = FunctionEnv::new(&mut store, ()); - let function = Function::new_native(&mut store, &ctx, |_: FunctionEnvMut<'_, ()>| {}); + let mut env = FunctionEnv::new(&mut store, ()); + let function = Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, ()>| {}); let typed_function: TypedFunction<(), ()> = function.native(&mut store).unwrap(); let result = typed_function.call(&mut store); assert!(result.is_ok()); let function = Function::new_native( &mut store, - &ctx, + &env, |_: FunctionEnvMut<'_, ()>, a: i32| -> i32 { a + 1 }, ); let typed_function: TypedFunction = function.native(&mut store).unwrap(); @@ -422,21 +422,21 @@ mod js { // fn rust_abi(a: i32, b: i64, c: f32, d: f64) -> u64 { // (a as u64 * 1000) + (b as u64 * 100) + (c as u64 * 10) + (d as u64) // } - // let function = Function::new(&mut store, &ctx, rust_abi); + // let function = Function::new(&mut store, &env, rust_abi); // let typed_function: TypedFunction<(i32, i64, f32, f64), u64> = function.native(&mut store).unwrap(); // assert_eq!(typed_function.call(8, 4, 1.5, 5.).unwrap(), 8415); let function = - Function::new_native(&mut store, &ctx, |_: FunctionEnvMut<'_, ()>| -> i32 { 1 }); + Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, ()>| -> i32 { 1 }); let typed_function: TypedFunction<(), i32> = function.native(&mut store).unwrap(); assert_eq!(typed_function.call(&mut store).unwrap(), 1); let function = - Function::new_native(&mut store, &ctx, |_: FunctionEnvMut<'_, ()>, _a: i32| {}); + Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, ()>, _a: i32| {}); let typed_function: TypedFunction = function.native(&mut store).unwrap(); assert!(typed_function.call(&mut store, 4).is_ok()); - // let function = Function::new(&mut store, &ctx, || -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }); + // let function = Function::new(&mut store, &env, || -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }); // let typed_function: TypedFunction<(), (i32, i64, f32, f64)> = function.native(&mut store).unwrap(); // assert_eq!(typed_function.call().unwrap(), (1, 2, 3.0, 4.0)); } @@ -444,7 +444,7 @@ mod js { #[wasm_bindgen_test] fn function_outlives_instance() { let mut store = Store::default(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let wat = r#"(module (type $sum_t (func (param i32 i32) (result i32))) (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32) diff --git a/lib/api/tests/js_instance.rs b/lib/api/tests/js_instance.rs index 1941ed8a95b..605e6ebac89 100644 --- a/lib/api/tests/js_instance.rs +++ b/lib/api/tests/js_instance.rs @@ -41,7 +41,7 @@ mod js { .unwrap(); let import_object = imports! {}; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &import_object).unwrap(); let memory = instance.exports.get_memory("mem").unwrap(); @@ -81,7 +81,7 @@ mod js { .unwrap(); let import_object = imports! {}; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &import_object).unwrap(); let get_magic = instance.exports.get_function("get_magic").unwrap(); @@ -121,11 +121,11 @@ mod js { ))], }) .unwrap(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let imported = Function::new(&mut store, &ctx, imported_signature, |_env, args| { + let imported = Function::new(&mut store, &env, imported_signature, |_env, args| { log!("Calling `imported`..."); let result = args[0].unwrap_i32() * 2; log!("Result of `imported`: {:?}", result); @@ -238,10 +238,10 @@ mod js { multiplier: i32, } - let mut ctx = FunctionEnv::new(&mut store, Env { multiplier: 3 }); + let mut env = FunctionEnv::new(&mut store, Env { multiplier: 3 }); let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let imported = Function::new(&mut store, &ctx, &imported_signature, |ctx, args| { + let imported = Function::new(&mut store, &env, &imported_signature, |ctx, args| { log!("Calling `imported`..."); let result = args[0].unwrap_i32() * ctx.data().multiplier; log!("Result of `imported`: {:?}", result); @@ -293,8 +293,8 @@ mod js { return arg + 1; } - let mut ctx = FunctionEnv::new(&mut store, ()); - let imported = Function::new_native(&mut store, &ctx, imported_fn); + let mut env = FunctionEnv::new(&mut store, ()); + let imported = Function::new_native(&mut store, &env, imported_fn); let import_object = imports! { "env" => { @@ -348,9 +348,9 @@ mod js { return ctx.data().multiplier * arg; } - let mut ctx = FunctionEnv::new(&mut store, Env { multiplier: 3 }); + let mut env = FunctionEnv::new(&mut store, Env { multiplier: 3 }); - let imported = Function::new_native(&mut store, &ctx, imported_fn); + let imported = Function::new_native(&mut store, &env, imported_fn); let import_object = imports! { "env" => { @@ -403,18 +403,18 @@ mod js { fn imported_fn(ctx: FunctionEnvMut<'_, Env>, arg: u32) -> u32 { let memory: &Memory = ctx.data().memory.as_ref().unwrap(); - let memory_val = memory.uint8view(&ctx).get_index(0); + let memory_val = memory.uint8view(&env).get_index(0); return (memory_val as u32) * ctx.data().multiplier * arg; } - let mut ctx = FunctionEnv::new( + let mut env = FunctionEnv::new( &mut store, Env { multiplier: 3, memory: None, }, ); - let imported = Function::new_native(&mut store, &ctx, imported_fn); + let imported = Function::new_native(&mut store, &env, imported_fn); let import_object = imports! { "env" => { @@ -454,7 +454,7 @@ mod js { multiplier: u32, } - let mut ctx = FunctionEnv::new(&mut store, Env { multiplier: 3 }); + let mut env = FunctionEnv::new(&mut store, Env { multiplier: 3 }); fn imported_fn( ctx: FunctionEnvMut<'_, Env>, @@ -465,7 +465,7 @@ mod js { } let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let imported = Function::new(&mut store, &ctx, imported_signature, imported_fn); + let imported = Function::new(&mut store, &env, imported_signature, imported_fn); let expected = vec![Val::I32(12)].into_boxed_slice(); assert_eq!(imported.call(&mut store, &[Val::I32(4)]), Ok(expected)); @@ -511,12 +511,12 @@ mod js { args: &[Val], ) -> Result, RuntimeError> { let memory: &Memory = ctx.data().memory.as_ref().unwrap(); - let memory_val = memory.uint8view(&ctx).get_index(0); + let memory_val = memory.uint8view(&env).get_index(0); let value = (memory_val as u32) * ctx.data().multiplier * args[0].unwrap_i32() as u32; return Ok(vec![Val::I32(value as _)]); } - let mut ctx = FunctionEnv::new( + let mut env = FunctionEnv::new( &mut store, Env { multiplier: 3, @@ -525,7 +525,7 @@ mod js { ); let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let imported = Function::new(&mut store, &ctx, imported_signature, imported_fn); + let imported = Function::new(&mut store, &env, imported_signature, imported_fn); let import_object = imports! { "env" => { @@ -585,7 +585,7 @@ mod js { ], }) .unwrap(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let global = Global::new_mut(&mut store, Value::I32(0)); let import_object = imports! { "" => { @@ -633,11 +633,11 @@ mod js { a + b } - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! { "env" => { - "sum" => Function::new_native(&mut store, &ctx, sum), + "sum" => Function::new_native(&mut store, &env, sum), } }; @@ -673,11 +673,11 @@ mod js { fn early_exit(_: FunctionEnvMut<'_, ()>) { panic!("Do panic") } - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! { "env" => { - "early_exit" => Function::new_native(&mut store, &ctx, early_exit), + "early_exit" => Function::new_native(&mut store, &env, early_exit), } }; let instance = Instance::new(&mut store, &module, &import_object).unwrap(); @@ -721,7 +721,7 @@ mod js { ) .unwrap(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); use std::fmt; @@ -742,7 +742,7 @@ mod js { let import_object = imports! { "env" => { - "early_exit" => Function::new_native(&mut store, &ctx, early_exit), + "early_exit" => Function::new_native(&mut store, &env, early_exit), } }; @@ -802,7 +802,7 @@ mod js { .unwrap(); let import_object = imports! {}; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let result = Instance::new(&mut store, &module, &import_object); let err = result.unwrap_err(); assert!(format!("{:?}", err).contains("zero")) diff --git a/lib/api/tests/sys_externals.rs b/lib/api/tests/sys_externals.rs index 461786e2483..26be6969193 100644 --- a/lib/api/tests/sys_externals.rs +++ b/lib/api/tests/sys_externals.rs @@ -69,8 +69,8 @@ mod sys { minimum: 0, maximum: None, }; - let ctx = FunctionEnv::new(&mut store, ()); - let f = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>| {}); + let env = FunctionEnv::new(&mut store, ()); + let f = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>| {}); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f)))?; assert_eq!(table.ty(&mut store), table_type); @@ -90,13 +90,13 @@ mod sys { #[ignore] fn table_get() -> Result<()> { let mut store = Store::default(); - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); let table_type = TableType { ty: Type::FuncRef, minimum: 0, maximum: Some(1), }; - let f = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>, num: i32| { + let f = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, num: i32| { num + 1 }); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f)))?; @@ -116,13 +116,13 @@ mod sys { #[test] fn table_grow() -> Result<()> { let mut store = Store::default(); - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); let table_type = TableType { ty: Type::FuncRef, minimum: 0, maximum: Some(10), }; - let f = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>, num: i32| { + let f = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, num: i32| { num + 1 }); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f.clone())))?; @@ -189,21 +189,21 @@ mod sys { #[test] fn function_new() -> Result<()> { let mut store = Store::default(); - let ctx = FunctionEnv::new(&mut store, ()); - let function = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>| {}); + let env = FunctionEnv::new(&mut store, ()); + let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![]) ); let function = - Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>, _a: i32| {}); + Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, _a: i32| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![Type::I32], vec![]) ); let function = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut<()>, _a: i32, _b: i64, _c: f32, _d: f64| {}, ); assert_eq!( @@ -211,14 +211,14 @@ mod sys { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) ); let function = - Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>| -> i32 { 1 }); + Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>| -> i32 { 1 }); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![Type::I32]) ); let function = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut<()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, ); assert_eq!( @@ -235,21 +235,21 @@ mod sys { struct MyEnv {} let my_env = MyEnv {}; - let ctx = FunctionEnv::new(&mut store, my_env); - let function = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut| {}); + let env = FunctionEnv::new(&mut store, my_env); + let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![]) ); let function = - Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut, _a: i32| {}); + Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut, _a: i32| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![Type::I32], vec![]) ); let function = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut, _a: i32, _b: i64, _c: f32, _d: f64| {}, ); assert_eq!( @@ -257,14 +257,14 @@ mod sys { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) ); let function = - Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut| -> i32 { 1 }); + Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut| -> i32 { 1 }); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![Type::I32]) ); let function = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, ); assert_eq!( @@ -277,13 +277,13 @@ mod sys { #[test] fn function_new_dynamic() -> Result<()> { let mut store = Store::default(); - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); // Using &FunctionType signature let function_type = FunctionType::new(vec![], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -291,7 +291,7 @@ mod sys { let function_type = FunctionType::new(vec![Type::I32], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -300,7 +300,7 @@ mod sys { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -308,7 +308,7 @@ mod sys { let function_type = FunctionType::new(vec![], vec![Type::I32]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -317,7 +317,7 @@ mod sys { FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -327,7 +327,7 @@ mod sys { let function_type = ([Type::V128], [Type::I32, Type::F32, Type::F64]); let function = Function::new( &mut store, - &ctx, + &env, function_type, |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -346,13 +346,13 @@ mod sys { #[derive(Clone)] struct MyEnv {} let my_env = MyEnv {}; - let ctx = FunctionEnv::new(&mut store, my_env); + let env = FunctionEnv::new(&mut store, my_env); // Using &FunctionType signature let function_type = FunctionType::new(vec![], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); @@ -360,7 +360,7 @@ mod sys { let function_type = FunctionType::new(vec![Type::I32], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); @@ -369,7 +369,7 @@ mod sys { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); @@ -377,7 +377,7 @@ mod sys { let function_type = FunctionType::new(vec![], vec![Type::I32]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); @@ -386,7 +386,7 @@ mod sys { FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]); let function = Function::new( &mut store, - &ctx, + &env, &function_type, |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); @@ -396,7 +396,7 @@ mod sys { let function_type = ([Type::V128], [Type::I32, Type::F32, Type::F64]); let function = Function::new( &mut store, - &ctx, + &env, function_type, |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); @@ -412,35 +412,35 @@ mod sys { // #[test] // fn native_function_works() -> Result<()> { // let mut store = Store::default(); - // let ctx = FunctionEnv::new(&mut store, ()); - // let function = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>| {}); + // let env = FunctionEnv::new(&mut store, ()); + // let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>| {}); // let native_function: TypedFunction<(), ()> = function.native(&mut store).unwrap(); // let result = native_function.call(&mut store); // assert!(result.is_ok()); // let function = - // Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>, a: i32| -> i32 { a + 1 }); + // Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, a: i32| -> i32 { a + 1 }); // let native_function: TypedFunction = function.native(&mut store).unwrap(); // assert_eq!(native_function.call(&mut store, 3).unwrap(), 4); // fn rust_abi(_ctx: FunctionEnvMut<()>, a: i32, b: i64, c: f32, d: f64) -> u64 { // (a as u64 * 1000) + (b as u64 * 100) + (c as u64 * 10) + (d as u64) // } - // let function = Function::new_native(&mut store, &ctx, rust_abi); + // let function = Function::new_native(&mut store, &env, rust_abi); // let native_function: TypedFunction<(i32, i64, f32, f64), u64> = // function.native(&mut store).unwrap(); // assert_eq!(native_function.call(&mut store, 8, 4, 1.5, 5.).unwrap(), 8415); - // let function = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>| -> i32 { 1 }); + // let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>| -> i32 { 1 }); // let native_function: TypedFunction<(), i32> = function.native(&mut store).unwrap(); // assert_eq!(native_function.call(&mut store).unwrap(), 1); - // let function = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>, _a: i32| {}); + // let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, _a: i32| {}); // let native_function: TypedFunction = function.native(&mut store).unwrap(); // assert!(native_function.call(&mut store, 4).is_ok()); // let function = - // Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>| -> (i32, i64, f32, f64) { + // Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>| -> (i32, i64, f32, f64) { // (1, 2, 3.0, 4.0) // }); // let native_function: TypedFunction<(), (i32, i64, f32, f64)> = @@ -453,7 +453,7 @@ mod sys { // #[test] // fn function_outlives_instance() -> Result<()> { // let mut store = Store::default(); - // let ctx = FunctionEnv::new(&mut store, ()); + // let env = FunctionEnv::new(&mut store, ()); // let wat = r#"(module // (type $sum_t (func (param i32 i32) (result i32))) // (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32) @@ -481,7 +481,7 @@ mod sys { // #[test] // fn weak_instance_ref_externs_after_instance() -> Result<()> { // let mut store = Store::default(); - // let ctx = FunctionEnv::new(&mut store, ()); + // let env = FunctionEnv::new(&mut store, ()); // let wat = r#"(module // (memory (export "mem") 1) // (type $sum_t (func (param i32 i32) (result i32))) @@ -524,7 +524,7 @@ mod sys { // val: 5, // memory: None, // }; - // let ctx = FunctionEnv::new(&mut store, env); + // let env = FunctionEnv::new(&mut store, env); // let result = host_function(ctx.as_context_mut(), 7, 9); // assert_eq!(result, 21); diff --git a/lib/api/tests/sys_instance.rs b/lib/api/tests/sys_instance.rs index 2f5d04d70bf..234bffe93b4 100644 --- a/lib/api/tests/sys_instance.rs +++ b/lib/api/tests/sys_instance.rs @@ -62,10 +62,10 @@ mod sys { // We create the environment let env = Env { multiplier: 3 }; // We move the environment to the store, so it can be used by the `Function` - let ctx = FunctionEnv::new(&mut store, env); + let env = FunctionEnv::new(&mut store, env); let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let imported = Function::new(&mut store, &ctx, imported_signature, imported_fn); + let imported = Function::new(&mut store, &env, imported_signature, imported_fn); let expected = vec![Value::I32(12)].into_boxed_slice(); let result = imported.call(&mut store, &[Value::I32(4)])?; diff --git a/lib/api/tests/sys_module.rs b/lib/api/tests/sys_module.rs index 5da47dce1ed..551e8aaae70 100644 --- a/lib/api/tests/sys_module.rs +++ b/lib/api/tests/sys_module.rs @@ -191,38 +191,38 @@ mod sys { (call 7 (i32.const -1))) )"#; let module = Module::new(&store, wat)?; - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); let imports = imports! { "host" => { - "host_func1" => Function::new_native(&mut store, &ctx,|_ctx: FunctionEnvMut<()>, p: u64| { + "host_func1" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: u64| { println!("host_func1: Found number {}", p); assert_eq!(p, u64::max_value()); }), - "host_func2" => Function::new_native(&mut store, &ctx,|_ctx: FunctionEnvMut<()>, p: u32| { + "host_func2" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: u32| { println!("host_func2: Found number {}", p); assert_eq!(p, u32::max_value()); }), - "host_func3" => Function::new_native(&mut store, &ctx,|_ctx: FunctionEnvMut<()>, p: i64| { + "host_func3" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: i64| { println!("host_func3: Found number {}", p); assert_eq!(p, -1); }), - "host_func4" => Function::new_native(&mut store, &ctx,|_ctx: FunctionEnvMut<()>, p: i32| { + "host_func4" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: i32| { println!("host_func4: Found number {}", p); assert_eq!(p, -1); }), - "host_func5" => Function::new_native(&mut store, &ctx,|_ctx: FunctionEnvMut<()>, p: i16| { + "host_func5" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: i16| { println!("host_func5: Found number {}", p); assert_eq!(p, -1); }), - "host_func6" => Function::new_native(&mut store, &ctx,|_ctx: FunctionEnvMut<()>, p: u16| { + "host_func6" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: u16| { println!("host_func6: Found number {}", p); assert_eq!(p, u16::max_value()); }), - "host_func7" => Function::new_native(&mut store, &ctx,|_ctx: FunctionEnvMut<()>, p: i8| { + "host_func7" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: i8| { println!("host_func7: Found number {}", p); assert_eq!(p, -1); }), - "host_func8" => Function::new_native(&mut store, &ctx,|_ctx: FunctionEnvMut<()>, p: u8| { + "host_func8" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: u8| { println!("host_func8: Found number {}", p); assert_eq!(p, u8::max_value()); }), diff --git a/lib/api/tests/sys_reference_types.rs b/lib/api/tests/sys_reference_types.rs index ebbfd57988f..b31b32b4b78 100644 --- a/lib/api/tests/sys_reference_types.rs +++ b/lib/api/tests/sys_reference_types.rs @@ -24,10 +24,10 @@ mod sys { #[derive(Clone, Debug)] pub struct Env(Arc); let env = Env(Arc::new(AtomicBool::new(false))); - let ctx = FunctionEnv::new(&mut store, env); + let env = FunctionEnv::new(&mut store, env); let imports = imports! { "env" => { - "func_ref_identity" => Function::new(&mut store, &ctx, FunctionType::new([Type::FuncRef], [Type::FuncRef]), |_ctx: FunctionEnvMut, values: &[Value]| -> Result, _> { + "func_ref_identity" => Function::new(&mut store, &env, FunctionType::new([Type::FuncRef], [Type::FuncRef]), |_ctx: FunctionEnvMut, values: &[Value]| -> Result, _> { Ok(vec![values[0].clone()]) }) }, @@ -44,14 +44,14 @@ mod sys { } let func_to_call = - Function::new_native(&mut store, &ctx, |mut ctx: FunctionEnvMut| -> i32 { + Function::new_native(&mut store, &env, |mut ctx: FunctionEnvMut| -> i32 { ctx.data_mut().0.store(true, Ordering::SeqCst); 343 }); let call_set_value: &Function = instance.exports.get_function("call_set_value")?; let results: Box<[Value]> = call_set_value.call(&mut store, &[Value::FuncRef(Some(func_to_call))])?; - assert!(ctx + assert!(env .as_mut(&mut store.as_store_mut()) .0 .load(Ordering::SeqCst)); @@ -81,7 +81,7 @@ mod sys { (call $func_ref_call (ref.func $product))) )"#; let module = Module::new(&store, wat)?; - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); fn func_ref_call( mut ctx: FunctionEnvMut<()>, values: &[Value], @@ -96,11 +96,11 @@ mod sys { "env" => { "func_ref_call" => Function::new( &mut store, - &ctx, + &env, FunctionType::new([Type::FuncRef], [Type::I32]), func_ref_call ), - // "func_ref_call_native" => Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>, f: Function| -> Result { + // "func_ref_call_native" => Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, f: Function| -> Result { // let f: TypedFunction::<(i32, i32), i32> = f.native(&mut store)?; // f.call(&mut store, 7, 9) // }) @@ -112,7 +112,7 @@ mod sys { fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { a + b } - let sum_func = Function::new_native(&mut store, &ctx, sum); + let sum_func = Function::new_native(&mut store, &env, sum); let call_func: &Function = instance.exports.get_function("call_func")?; let result = call_func.call(&mut store, &[Value::FuncRef(Some(sum_func))])?; @@ -151,16 +151,16 @@ mod sys { (call $get_new_extern_ref_native)) )"#; let module = Module::new(&store, wat)?; - let ctx = FunctionEnv::new(&mut store, ()); + let env = FunctionEnv::new(&mut store, ()); let imports = imports! { "env" => { - "extern_ref_identity" => Function::new(&mut store, &ctx, FunctionType::new([Type::ExternRef], [Type::ExternRef]), |_ctx, values| -> Result, _> { + "extern_ref_identity" => Function::new(&mut store, &env, FunctionType::new([Type::ExternRef], [Type::ExternRef]), |_ctx, values| -> Result, _> { Ok(vec![values[0].clone()]) }), - "extern_ref_identity_native" => Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>, er: ExternRef| -> ExternRef { + "extern_ref_identity_native" => Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, er: ExternRef| -> ExternRef { er }), - "get_new_extern_ref" => Function::new(&mut store, &ctx, FunctionType::new([], [Type::ExternRef]), |_ctx, _| -> Result, _> { + "get_new_extern_ref" => Function::new(&mut store, &env, FunctionType::new([], [Type::ExternRef]), |_ctx, _| -> Result, _> { let inner = [("hello".to_string(), "world".to_string()), ("color".to_string(), "orange".to_string())] @@ -170,7 +170,7 @@ mod sys { let new_extern_ref = ExternRef::new(&mut ctx, inner); Ok(vec![Value::ExternRef(new_extern_ref)]) }), - "get_new_extern_ref_native" => Function::new_native(&mut store, &ctx,|_ctx| -> ExternRef { + "get_new_extern_ref_native" => Function::new_native(&mut store, &env,|_ctx| -> ExternRef { let inner = [("hello".to_string(), "world".to_string()), ("color".to_string(), "orange".to_string())] diff --git a/lib/c-api/src/wasm_c_api/instance.rs b/lib/c-api/src/wasm_c_api/instance.rs index 48187c5d43f..c32b44fbe93 100644 --- a/lib/c-api/src/wasm_c_api/instance.rs +++ b/lib/c-api/src/wasm_c_api/instance.rs @@ -80,7 +80,7 @@ pub unsafe extern "C" fn wasm_instance_new( return None; } - Err(e @ InstantiationError::BadContext) => { + Err(e @ InstantiationError::DifferentStores) => { crate::error::update_last_error(e); return None; diff --git a/lib/c-api/src/wasm_c_api/wasi/mod.rs b/lib/c-api/src/wasm_c_api/wasi/mod.rs index 7e2a2e44400..54ed50ed46b 100644 --- a/lib/c-api/src/wasm_c_api/wasi/mod.rs +++ b/lib/c-api/src/wasm_c_api/wasi/mod.rs @@ -405,7 +405,7 @@ mod tests { int main() { wasm_engine_t* engine = wasm_engine_new(); wasm_store_t* store = wasm_store_new(engine); - wasmer_funcenv_t* ctx = wasmer_funcenv_new(store, 0); + wasmer_funcenv_t* env = wasmer_funcenv_new(store, 0); wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module (import \"wasi_unstable\" \"args_get\" (func (param i32 i32) (result i32))))"); @@ -420,7 +420,7 @@ mod tests { wasm_module_delete(module); wasm_byte_vec_delete(&wasm); wasm_byte_vec_delete(&wat); - wasmer_funcenv_delete(ctx); + wasmer_funcenv_delete(env); wasm_store_delete(store); wasm_engine_delete(engine); @@ -438,7 +438,7 @@ mod tests { int main() { wasm_engine_t* engine = wasm_engine_new(); wasm_store_t* store = wasm_store_new(engine); - wasmer_funcenv_t* ctx = wasmer_funcenv_new(store, 0); + wasmer_funcenv_t* env = wasmer_funcenv_new(store, 0); wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module (import \"wasi_snapshot_preview1\" \"args_get\" (func (param i32 i32) (result i32))))"); @@ -453,7 +453,7 @@ mod tests { wasm_module_delete(module); wasm_byte_vec_delete(&wasm); wasm_byte_vec_delete(&wat); - wasmer_funcenv_delete(ctx); + wasmer_funcenv_delete(env); wasm_store_delete(store); wasm_engine_delete(engine); @@ -471,7 +471,7 @@ mod tests { int main() { wasm_engine_t* engine = wasm_engine_new(); wasm_store_t* store = wasm_store_new(engine); - wasmer_funcenv_t* ctx = wasmer_funcenv_new(store, 0); + wasmer_funcenv_t* env = wasmer_funcenv_new(store, 0); wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module (import \"wasi_snpsht_prvw1\" \"args_get\" (func (param i32 i32) (result i32))))"); @@ -486,7 +486,7 @@ mod tests { wasm_module_delete(module); wasm_byte_vec_delete(&wasm); wasm_byte_vec_delete(&wat); - wasmer_funcenv_delete(ctx); + wasmer_funcenv_delete(env); wasm_store_delete(store); wasm_engine_delete(engine); diff --git a/lib/cli/src/commands/run.rs b/lib/cli/src/commands/run.rs index 781f3c420cc..c93a2829a88 100644 --- a/lib/cli/src/commands/run.rs +++ b/lib/cli/src/commands/run.rs @@ -142,13 +142,13 @@ impl Run { bail!("--invoke is not supported with emscripten modules"); } // create an EmEnv with default global - let ctx = FunctionEnv::new(&mut store, EmEnv::new()); - let mut emscripten_globals = EmscriptenGlobals::new(&mut store, &ctx, &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.as_mut(&mut store) + env.as_mut(&mut store) .set_data(&emscripten_globals.data, Default::default()); let import_object = - generate_emscripten_env(&mut store, &ctx, &mut emscripten_globals); + 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) => { @@ -165,7 +165,7 @@ impl Run { run_emscripten_instance( &mut instance, - ctx.into_mut(&mut store), + env.into_mut(&mut store), &mut emscripten_globals, if let Some(cn) = &self.command_name { cn diff --git a/tests/compilers/imports.rs b/tests/compilers/imports.rs index 89b9522548f..558e4fa51bd 100644 --- a/tests/compilers/imports.rs +++ b/tests/compilers/imports.rs @@ -49,26 +49,26 @@ fn get_module(store: &Store) -> Result { fn dynamic_function(config: crate::Config) -> Result<()> { let mut store = config.store(); let module = get_module(&store)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); static HITS: AtomicUsize = AtomicUsize::new(0); let imports = imports! { "host" => { - "0" => Function::new(&mut store, &ctx, FunctionType::new(vec![], vec![]), |_ctx, _values| { + "0" => Function::new(&mut store, &env, FunctionType::new(vec![], vec![]), |_ctx, _values| { assert_eq!(HITS.fetch_add(1, SeqCst), 0); Ok(vec![]) }), - "1" => Function::new(&mut store, &ctx, FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]), |_ctx, values| { + "1" => Function::new(&mut store, &env, FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]), |_ctx, values| { assert_eq!(values[0], Value::I32(0)); assert_eq!(HITS.fetch_add(1, SeqCst), 1); Ok(vec![Value::I32(1)]) }), - "2" => Function::new(&mut store, &ctx, FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]), |_ctx, values| { + "2" => Function::new(&mut store, &env, FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]), |_ctx, values| { assert_eq!(values[0], Value::I32(2)); assert_eq!(values[1], Value::I64(3)); assert_eq!(HITS.fetch_add(1, SeqCst), 2); Ok(vec![]) }), - "3" => Function::new(&mut store, &ctx, FunctionType::new(vec![ValueType::I32, ValueType::I64, ValueType::I32, ValueType::F32, ValueType::F64], vec![]), |_ctx, values| { + "3" => Function::new(&mut store, &env, FunctionType::new(vec![ValueType::I32, ValueType::I64, ValueType::I32, ValueType::F32, ValueType::F64], vec![]), |_ctx, values| { assert_eq!(values[0], Value::I32(100)); assert_eq!(values[1], Value::I64(200)); assert_eq!(values[2], Value::I32(300)); @@ -104,10 +104,10 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> { let env: Env = Env { counter: Arc::new(AtomicUsize::new(0)), }; - let mut ctx = FunctionEnv::new(&mut store, env); + let mut env = FunctionEnv::new(&mut store, env); let f0 = Function::new( &mut store, - &ctx, + &env, FunctionType::new(vec![], vec![]), |ctx, _values| { assert_eq!(ctx.data().fetch_add(1, SeqCst), 0); @@ -116,7 +116,7 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> { ); let f1 = Function::new( &mut store, - &ctx, + &env, FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]), |ctx, values| { assert_eq!(values[0], Value::I32(0)); @@ -126,7 +126,7 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> { ); let f2 = Function::new( &mut store, - &ctx, + &env, FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]), |ctx, values| { assert_eq!(values[0], Value::I32(2)); @@ -137,7 +137,7 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> { ); let f3 = Function::new( &mut store, - &ctx, + &env, FunctionType::new( vec![ ValueType::I32, @@ -170,7 +170,7 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> { }, }, )?; - assert_eq!(ctx.as_mut(&mut store).load(SeqCst), 4); + assert_eq!(env.as_mut(&mut store).load(SeqCst), 4); Ok(()) } @@ -181,18 +181,18 @@ fn static_function(config: crate::Config) -> Result<()> { let module = get_module(&store)?; static HITS: AtomicUsize = AtomicUsize::new(0); - let mut ctx = FunctionEnv::new(&mut store, ()); - let f0 = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<_>| { + let mut env = FunctionEnv::new(&mut store, ()); + let f0 = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>| { assert_eq!(HITS.fetch_add(1, SeqCst), 0); }); - let f1 = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<_>, x: i32| -> i32 { + let f1 = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>, x: i32| -> i32 { assert_eq!(x, 0); assert_eq!(HITS.fetch_add(1, SeqCst), 1); 1 }); let f2 = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut<_>, x: i32, y: i64| { assert_eq!(x, 2); assert_eq!(y, 3); @@ -201,7 +201,7 @@ fn static_function(config: crate::Config) -> Result<()> { ); let f3 = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut<_>, a: i32, b: i64, c: i32, d: f32, e: f64| { assert_eq!(a, 100); assert_eq!(b, 200); @@ -234,13 +234,13 @@ fn static_function_with_results(config: crate::Config) -> Result<()> { let module = get_module(&store)?; static HITS: AtomicUsize = AtomicUsize::new(0); - let mut ctx = FunctionEnv::new(&mut store, ()); - let f0 = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<_>| { + let mut env = FunctionEnv::new(&mut store, ()); + let f0 = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>| { assert_eq!(HITS.fetch_add(1, SeqCst), 0); }); let f1 = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut<_>, x: i32| -> Result { assert_eq!(x, 0); assert_eq!(HITS.fetch_add(1, SeqCst), 1); @@ -249,7 +249,7 @@ fn static_function_with_results(config: crate::Config) -> Result<()> { ); let f2 = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut<_>, x: i32, y: i64| { assert_eq!(x, 2); assert_eq!(y, 3); @@ -258,7 +258,7 @@ fn static_function_with_results(config: crate::Config) -> Result<()> { ); let f3 = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut<_>, a: i32, b: i64, c: i32, d: f32, e: f64| { assert_eq!(a, 100); assert_eq!(b, 200); @@ -300,13 +300,13 @@ fn static_function_with_env(config: crate::Config) -> Result<()> { } let env: Env = Env(Arc::new(AtomicUsize::new(0))); - let mut ctx = FunctionEnv::new(&mut store, env); - let f0 = Function::new_native(&mut store, &ctx, |ctx: FunctionEnvMut| { + let mut env = FunctionEnv::new(&mut store, env); + let f0 = Function::new_native(&mut store, &env, |ctx: FunctionEnvMut| { assert_eq!(ctx.data().fetch_add(1, SeqCst), 0); }); let f1 = Function::new_native( &mut store, - &ctx, + &env, |ctx: FunctionEnvMut, x: i32| -> i32 { assert_eq!(x, 0); assert_eq!(ctx.data().fetch_add(1, SeqCst), 1); @@ -315,7 +315,7 @@ fn static_function_with_env(config: crate::Config) -> Result<()> { ); let f2 = Function::new_native( &mut store, - &ctx, + &env, |ctx: FunctionEnvMut, x: i32, y: i64| { assert_eq!(x, 2); assert_eq!(y, 3); @@ -324,7 +324,7 @@ fn static_function_with_env(config: crate::Config) -> Result<()> { ); let f3 = Function::new_native( &mut store, - &ctx, + &env, |ctx: FunctionEnvMut, a: i32, b: i64, c: i32, d: f32, e: f64| { assert_eq!(a, 100); assert_eq!(b, 200); @@ -346,7 +346,7 @@ fn static_function_with_env(config: crate::Config) -> Result<()> { }, }, )?; - assert_eq!(ctx.as_mut(&mut store).load(SeqCst), 4); + assert_eq!(env.as_mut(&mut store).load(SeqCst), 4); Ok(()) } @@ -363,10 +363,10 @@ fn static_function_that_fails(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &wat)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let f0 = Function::new_native( &mut store, - &ctx, + &env, |_ctx: FunctionEnvMut<_>| -> Result { Err(RuntimeError::new("oops")) }, @@ -419,10 +419,10 @@ fn dynamic_function_with_env_wasmer_env_init_works(config: crate::Config) -> Res } let env: Env = Env { memory: None }; - let mut ctx = FunctionEnv::new(&mut store, env); + let mut env = FunctionEnv::new(&mut store, env); let f0 = Function::new( &mut store, - &ctx, + &env, FunctionType::new(vec![], vec![]), |ctx, _values| { assert!(ctx.data().memory.as_ref().is_some()); @@ -439,7 +439,7 @@ fn dynamic_function_with_env_wasmer_env_init_works(config: crate::Config) -> Res }, )?; let memory = instance.exports.get_memory("memory")?; - ctx.as_mut(&mut store).memory = Some(memory.clone()); + env.as_mut(&mut store).memory = Some(memory.clone()); let f: TypedFunction<(), ()> = instance.exports.get_typed_function(&mut store, "main")?; f.call(&mut store)?; Ok(()) @@ -465,14 +465,14 @@ fn multi_use_host_fn_manages_memory_correctly(config: crate::Config) -> Result<( }*/ let env: Env = Env { memory: None }; - let mut ctx = FunctionEnv::new(&mut store, env); + let mut env = FunctionEnv::new(&mut store, env); fn host_fn(ctx: FunctionEnvMut) { assert!(ctx.data().memory.is_some()); println!("Hello, world!"); } let imports = imports! { "host" => { - "fn" => Function::new_native(&mut store, &ctx, host_fn), + "fn" => Function::new_native(&mut store, &env, host_fn), }, }; let instance1 = Instance::new(&mut store, &module, &imports)?; @@ -480,14 +480,14 @@ fn multi_use_host_fn_manages_memory_correctly(config: crate::Config) -> Result<( { let f1: TypedFunction<(), ()> = instance1.exports.get_typed_function(&mut store, "main")?; let memory = instance1.exports.get_memory("memory")?; - ctx.as_mut(&mut store).memory = Some(memory.clone()); + env.as_mut(&mut store).memory = Some(memory.clone()); f1.call(&mut store)?; } drop(instance1); { let f2: TypedFunction<(), ()> = instance2.exports.get_typed_function(&mut store, "main")?; let memory = instance2.exports.get_memory("memory")?; - ctx.as_mut(&mut store).memory = Some(memory.clone()); + env.as_mut(&mut store).memory = Some(memory.clone()); f2.call(&mut store)?; } drop(instance2); @@ -497,7 +497,7 @@ fn multi_use_host_fn_manages_memory_correctly(config: crate::Config) -> Result<( #[compiler_test(imports)] fn instance_local_memory_lifetime(config: crate::Config) -> Result<()> { let mut store = config.store(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let memory: Memory = { let wat = r#"(module (memory $mem 1) diff --git a/tests/compilers/issues.rs b/tests/compilers/issues.rs index 40e36cf4c18..03a301e8690 100644 --- a/tests/compilers/issues.rs +++ b/tests/compilers/issues.rs @@ -61,12 +61,12 @@ fn issue_2329(mut config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; let env = Env::new(); - let mut ctx = FunctionEnv::new(&mut store, env); + let mut env = FunctionEnv::new(&mut store, env); let imports: Imports = imports! { "env" => { "__read_memory" => Function::new_native( &mut store, - &ctx, + &env, read_memory ), } @@ -188,23 +188,23 @@ fn call_with_static_data_pointers(mut config: crate::Config) -> Result<()> { let module = Module::new(&store, wat)?; let env = Env { memory: None }; - let mut ctx = FunctionEnv::new(&mut store, env); + let mut env = FunctionEnv::new(&mut store, env); let memory = Memory::new( &mut store, MemoryType::new(Pages(1024), Some(Pages(2048)), false), ) .unwrap(); - ctx.as_mut(&mut store).memory = Some(memory.clone()); + env.as_mut(&mut store).memory = Some(memory.clone()); let mut exports = Exports::new(); exports.insert("memory", memory); - exports.insert("banana", Function::new_native(&mut store, &ctx, banana)); - exports.insert("peach", Function::new_native(&mut store, &ctx, peach)); + exports.insert("banana", Function::new_native(&mut store, &env, banana)); + exports.insert("peach", Function::new_native(&mut store, &env, peach)); exports.insert( "chaenomeles", - Function::new_native(&mut store, &ctx, chaenomeles), + Function::new_native(&mut store, &env, chaenomeles), ); - exports.insert("mango", Function::new_native(&mut store, &ctx, mango)); - exports.insert("gas", Function::new_native(&mut store, &ctx, gas)); + exports.insert("mango", Function::new_native(&mut store, &env, mango)); + exports.insert("gas", Function::new_native(&mut store, &env, gas)); let mut imports = Imports::new(); imports.register_namespace("env", exports); let instance = Instance::new(&mut store, &module, &imports)?; @@ -248,7 +248,7 @@ fn regression_gpr_exhaustion_for_calls(mut config: crate::Config) -> Result<()> i32.const 0) (table (;0;) 1 1 funcref)) "#; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let module = Module::new(&store, wat)?; let imports: Imports = imports! {}; let instance = Instance::new(&mut store, &module, &imports)?; diff --git a/tests/compilers/metering.rs b/tests/compilers/metering.rs index 81310e9de33..4df524aad4b 100644 --- a/tests/compilers/metering.rs +++ b/tests/compilers/metering.rs @@ -20,7 +20,7 @@ fn run_add_with_limit(mut config: crate::Config, limit: u64) -> Result<()> { (i32.add (local.get 0) (local.get 1))) )"#; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! {}; @@ -54,7 +54,7 @@ fn run_loop(mut config: crate::Config, limit: u64, iter_count: i32) -> Result<() ) )"#; let module = Module::new(&store, wat).unwrap(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! {}; @@ -156,7 +156,7 @@ fn complex_loop(mut config: crate::Config) -> Result<()> { .middlewares .push(Arc::new(Metering::new(100, cost_always_one))); let mut store = config.store(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let module = Module::new(&store, WAT).unwrap(); diff --git a/tests/compilers/middlewares.rs b/tests/compilers/middlewares.rs index 0036297b433..b7687ba4bf2 100644 --- a/tests/compilers/middlewares.rs +++ b/tests/compilers/middlewares.rs @@ -100,7 +100,7 @@ fn middleware_basic(mut config: crate::Config) -> Result<()> { (local.get 1))) )"#; let module = Module::new(&store, wat).unwrap(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! {}; @@ -125,7 +125,7 @@ fn middleware_one_to_multi(mut config: crate::Config) -> Result<()> { (local.get 1))) )"#; let module = Module::new(&store, wat).unwrap(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! {}; let instance = Instance::new(&mut store, &module, &import_object)?; @@ -150,7 +150,7 @@ fn middleware_multi_to_one(mut config: crate::Config) -> Result<()> { (i32.mul)) )"#; let module = Module::new(&store, wat).unwrap(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! {}; let instance = Instance::new(&mut store, &module, &import_object)?; diff --git a/tests/compilers/native_functions.rs b/tests/compilers/native_functions.rs index 88a737747a8..3f6ecac790c 100644 --- a/tests/compilers/native_functions.rs +++ b/tests/compilers/native_functions.rs @@ -59,11 +59,11 @@ fn native_function_works_for_wasm(config: crate::Config) -> anyhow::Result<()> { (call $multiply (local.get 1) (i32.const 2)))) )"#; let module = Module::new(&store, wat).unwrap(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! { "env" => { - "multiply" => Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<_>, a: i32, b: i32| a * b), + "multiply" => Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>, a: i32, b: i32| a * b), }, }; let instance = Instance::new(&mut store, &module, &import_object)?; @@ -94,9 +94,9 @@ fn native_function_works_for_wasm(config: crate::Config) -> anyhow::Result<()> { #[compiler_test(native_functions)] fn native_host_function_closure_panics(config: crate::Config) { let mut store = config.store(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let state = 3; - Function::new_native(&mut store, &ctx, move |_ctx: FunctionEnvMut<_>, _: i32| { + Function::new_native(&mut store, &env, move |_ctx: FunctionEnvMut<_>, _: i32| { println!("{}", state); }); } @@ -105,11 +105,11 @@ fn native_host_function_closure_panics(config: crate::Config) { fn native_with_env_host_function_closure_panics(config: crate::Config) { let mut store = config.store(); let env: i32 = 4; - let mut ctx = FunctionEnv::new(&mut store, env); + let mut env = FunctionEnv::new(&mut store, env); let state = 3; Function::new_native( &mut store, - &ctx, + &env, move |_ctx: FunctionEnvMut, _: i32| { println!("{}", state); }, @@ -138,28 +138,28 @@ fn non_native_functions_and_closures_with_no_env_work(config: crate::Config) -> )"#; let module = Module::new(&store, wat).unwrap(); let env: i32 = 10; - let mut ctx = FunctionEnv::new(&mut store, env); + let mut env = FunctionEnv::new(&mut store, env); let ty = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]); let captured_by_closure = 20; let import_object = imports! { "env" => { - "multiply1" => Function::new(&mut store, &ctx, &ty, move |_ctx, args| { + "multiply1" => Function::new(&mut store, &env, &ty, move |_ctx, args| { if let (Value::I32(v1), Value::I32(v2)) = (&args[0], &args[1]) { Ok(vec![Value::I32(v1 * v2 * captured_by_closure)]) } else { panic!("Invalid arguments"); } }), - "multiply2" => Function::new(&mut store, &ctx, &ty, move |ctx, args| { + "multiply2" => Function::new(&mut store, &env, &ty, move |ctx, args| { if let (Value::I32(v1), Value::I32(v2)) = (&args[0], &args[1]) { Ok(vec![Value::I32(v1 * v2 * captured_by_closure * ctx.data())]) } else { panic!("Invalid arguments"); } }), - "multiply3" => Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<_>, arg1: i32, arg2: i32| -> i32 + "multiply3" => Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>, arg1: i32, arg2: i32| -> i32 {arg1 * arg2 }), - "multiply4" => Function::new_native(&mut store, &ctx, |ctx: FunctionEnvMut, arg1: i32, arg2: i32| -> i32 + "multiply4" => Function::new_native(&mut store, &env, |ctx: FunctionEnvMut, arg1: i32, arg2: i32| -> i32 {arg1 * arg2 * ctx.data() }), }, }; @@ -186,10 +186,10 @@ fn native_function_works_for_wasm_function_manyparams(config: crate::Config) -> (call $longf (i32.const 1) (i32.const 2) (i32.const 3) (i32.const 4) (i32.const 5) (i32.const 6) (i64.const 7) (i64.const 8) (i32.const 9) (i32.const 0))) )"#; let module = Module::new(&store, wat).unwrap(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! { "env" => { - "longf" => Function::new_native(&mut store, &ctx, long_f), + "longf" => Function::new_native(&mut store, &env, long_f), }, }; @@ -218,7 +218,7 @@ fn native_function_works_for_wasm_function_manyparams_dynamic( config: crate::Config, ) -> anyhow::Result<()> { let mut store = config.store(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let wat = r#"(module (func $longf (import "env" "longf") (param i32 i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i64)) (func (export "longf_pure") (param i32 i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i64) @@ -230,7 +230,7 @@ fn native_function_works_for_wasm_function_manyparams_dynamic( let import_object = imports! { "env" => { - "longf" => Function::new(&mut store, &ctx, FunctionType::new(vec![ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I64 , ValueType::I64 ,ValueType::I32, ValueType::I32], vec![ValueType::I64]), long_f_dynamic), + "longf" => Function::new(&mut store, &env, FunctionType::new(vec![ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I64 , ValueType::I64 ,ValueType::I32, ValueType::I32], vec![ValueType::I64]), long_f_dynamic), }, }; @@ -257,7 +257,7 @@ fn native_function_works_for_wasm_function_manyparams_dynamic( #[compiler_test(native_functions)] fn static_host_function_without_env(config: crate::Config) -> anyhow::Result<()> { let mut store = config.store(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); fn f(_ctx: FunctionEnvMut<()>, a: i32, b: i64, c: f32, d: f64) -> (f64, f32, i64, i32) { (d * 4.0, c * 3.0, b * 2, a * 1) @@ -295,7 +295,7 @@ fn static_host_function_without_env(config: crate::Config) -> anyhow::Result<()> // Native static host function that returns a tuple. { - let f = Function::new_native(&mut store, &ctx, f); + let f = Function::new_native(&mut store, &env, f); let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = f.native(&mut store).unwrap(); let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; @@ -304,7 +304,7 @@ fn static_host_function_without_env(config: crate::Config) -> anyhow::Result<()> // Native static host function that returns a tuple. { - let long_f = Function::new_native(&mut store, &ctx, long_f); + let long_f = Function::new_native(&mut store, &env, long_f); let long_f_native: TypedFunction< (u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), (u32, u64, u32), @@ -315,7 +315,7 @@ fn static_host_function_without_env(config: crate::Config) -> anyhow::Result<()> // Native static host function that returns a result of a tuple. { - let f = Function::new_native(&mut store, &ctx, f_ok); + let f = Function::new_native(&mut store, &env, f_ok); let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = f.native(&mut store).unwrap(); let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; @@ -364,35 +364,35 @@ fn static_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { // Native static host function that returns a tuple. { let env = Env(Arc::new(Mutex::new(100))); - let mut ctx = FunctionEnv::new(&mut store, env); + let mut env = FunctionEnv::new(&mut store, env); - let f = Function::new_native(&mut store, &ctx, f); + let f = Function::new_native(&mut store, &env, f); let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = f.native(&mut store).unwrap(); - assert_eq!(*ctx.as_mut(&mut store).0.lock().unwrap(), 100); + assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 100); let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; assert_eq!(result, (28.0, 15.0, 6, 1)); - assert_eq!(*ctx.as_mut(&mut store).0.lock().unwrap(), 101); + assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 101); } // Native static host function that returns a result of a tuple. { let env = Env(Arc::new(Mutex::new(100))); - let mut ctx = FunctionEnv::new(&mut store, env); + let mut env = FunctionEnv::new(&mut store, env); - let f = Function::new_native(&mut store, &ctx, f_ok); + let f = Function::new_native(&mut store, &env, f_ok); let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = f.native(&mut store).unwrap(); - assert_eq!(*ctx.as_mut(&mut store).0.lock().unwrap(), 100); + assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 100); let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; assert_eq!(result, (28.0, 15.0, 6, 1)); - assert_eq!(*ctx.as_mut(&mut store).0.lock().unwrap(), 101); + assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 101); } Ok(()) @@ -401,10 +401,10 @@ fn static_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { #[compiler_test(native_functions)] fn dynamic_host_function_without_env(config: crate::Config) -> anyhow::Result<()> { let mut store = config.store(); - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let f = Function::new( &mut store, - &ctx, + &env, FunctionType::new( vec![ ValueType::I32, @@ -452,10 +452,10 @@ fn dynamic_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { } let env = Env(Arc::new(Mutex::new(100))); - let mut ctx = FunctionEnv::new(&mut store, env); + let mut env = FunctionEnv::new(&mut store, env); let f = Function::new( &mut store, - &ctx, + &env, FunctionType::new( vec![ ValueType::I32, @@ -488,12 +488,12 @@ fn dynamic_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = f.native(&mut store).unwrap(); - assert_eq!(*ctx.as_mut(&mut store).0.lock().unwrap(), 100); + assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 100); let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; assert_eq!(result, (28.0, 15.0, 6, 1)); - assert_eq!(*ctx.as_mut(&mut store).0.lock().unwrap(), 101); + assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 101); Ok(()) } diff --git a/tests/compilers/serialize.rs b/tests/compilers/serialize.rs index dbf87de8eca..1b796bf58c5 100644 --- a/tests/compilers/serialize.rs +++ b/tests/compilers/serialize.rs @@ -54,8 +54,8 @@ fn test_deserialize(config: crate::Config) -> Result<()> { vec![Type::I32, Type::I64, Type::I32, Type::F32, Type::F64], vec![Type::I64], ); - let mut ctx = FunctionEnv::new(&mut store, ()); - let f0 = Function::new(&mut store, &ctx, &func_type, |_ctx, params| { + let mut env = FunctionEnv::new(&mut store, ()); + let f0 = Function::new(&mut store, &env, &func_type, |_ctx, params| { let param_0: i64 = params[0].unwrap_i32() as i64; let param_1: i64 = params[1].unwrap_i64() as i64; let param_2: i64 = params[2].unwrap_i32() as i64; diff --git a/tests/compilers/traps.rs b/tests/compilers/traps.rs index 8e5e986ac19..fff22c69e05 100644 --- a/tests/compilers/traps.rs +++ b/tests/compilers/traps.rs @@ -14,9 +14,9 @@ fn test_trap_return(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let hello_type = FunctionType::new(vec![], vec![]); - let hello_func = Function::new(&mut store, &ctx, &hello_type, |_ctx, _| { + let hello_func = Function::new(&mut store, &env, &hello_type, |_ctx, _| { Err(RuntimeError::new("test 123")) }); @@ -55,7 +55,7 @@ fn test_trap_trace(config: crate::Config) -> Result<()> { ) "#; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let module = Module::new(&store, wat)?; let instance = Instance::new(&mut store, &module, &imports! {})?; let run_func = instance @@ -96,9 +96,9 @@ fn test_trap_trace_cb(config: crate::Config) -> Result<()> { ) "#; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let fn_type = FunctionType::new(vec![], vec![]); - let fn_func = Function::new(&mut store, &ctx, &fn_type, |_ctx, _| { + let fn_func = Function::new(&mut store, &env, &fn_type, |_ctx, _| { Err(RuntimeError::new("cb throw")) }); @@ -146,7 +146,7 @@ fn test_trap_stack_overflow(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &imports! {})?; let run_func = instance .exports @@ -180,7 +180,7 @@ fn trap_display_pretty(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &imports! {})?; let run_func = instance .exports @@ -217,7 +217,7 @@ fn trap_display_multi_module(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &imports! {})?; let bar = instance.exports.get_function("bar")?.clone(); @@ -272,9 +272,9 @@ fn trap_start_function_import(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &binary)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &ctx, &sig, |_ctx, _| { + let func = Function::new(&mut store, &env, &sig, |_ctx, _| { Err(RuntimeError::new("user trap")) }); let err = Instance::new( @@ -290,7 +290,7 @@ fn trap_start_function_import(config: crate::Config) -> Result<()> { .unwrap(); match err { InstantiationError::Link(_) - | InstantiationError::BadContext + | InstantiationError::DifferentStores | InstantiationError::CpuFeature(_) => { panic!("It should be a start error") } @@ -315,10 +315,10 @@ fn rust_panic_import(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &binary)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &ctx, &sig, |_ctx, _| panic!("this is a panic")); - let f0 = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<_>| { + let func = Function::new(&mut store, &env, &sig, |_ctx, _| panic!("this is a panic")); + let f0 = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>| { panic!("this is another panic") }); let instance = Instance::new( @@ -363,9 +363,9 @@ fn rust_panic_start_function(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &binary)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &ctx, &sig, |_ctx, _| panic!("this is a panic")); + let func = Function::new(&mut store, &env, &sig, |_ctx, _| panic!("this is a panic")); let err = panic::catch_unwind(AssertUnwindSafe(|| { drop(Instance::new( &mut store, @@ -380,7 +380,7 @@ fn rust_panic_start_function(config: crate::Config) -> Result<()> { .unwrap_err(); assert_eq!(err.downcast_ref::<&'static str>(), Some(&"this is a panic")); - let func = Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<_>| { + let func = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>| { panic!("this is another panic") }); let err = panic::catch_unwind(AssertUnwindSafe(|| { @@ -412,7 +412,7 @@ fn mismatched_arguments(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &binary)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &imports! {})?; let func: &Function = instance.exports.get("foo")?; assert_eq!( @@ -452,7 +452,7 @@ fn call_signature_mismatch(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &binary)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let err = Instance::new(&mut store, &module, &imports! {}) .err() .expect("expected error"); @@ -481,7 +481,7 @@ fn start_trap_pretty(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let err = Instance::new(&mut store, &module, &imports! {}) .err() .expect("expected error"); @@ -503,7 +503,7 @@ RuntimeError: unreachable fn present_after_module_drop(config: crate::Config) -> Result<()> { let mut store = config.store(); let module = Module::new(&store, r#"(func (export "foo") unreachable)"#)?; - let mut ctx = FunctionEnv::new(&mut store, ()); + let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &imports! {})?; let func: Function = instance.exports.get_function("foo")?.clone();