diff --git a/examples/early_exit.rs b/examples/early_exit.rs index 4bfbc6bad3e..c63b8edb5b0 100644 --- a/examples/early_exit.rs +++ b/examples/early_exit.rs @@ -66,7 +66,7 @@ fn main() -> anyhow::Result<()> { let module = Module::new(&store, wasm_bytes)?; // We declare the host function that we'll use to terminate execution. - fn early_exit(_ctx: FunctionEnvMut<()>) -> Result<(), ExitCode> { + fn early_exit(_env: FunctionEnvMut<()>) -> Result<(), ExitCode> { // This is where it happens. Err(ExitCode(1)) } diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 28887d618bc..ae608ec833f 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -59,7 +59,7 @@ fn main() -> anyhow::Result<()> { // We define a function to act as our "env" "say_hello" function imported in the // Wasm program above. - fn say_hello_world(_ctx: FunctionEnvMut<'_, ()>) { + fn say_hello_world(_env: FunctionEnvMut<'_, ()>) { println!("Hello, world!") } diff --git a/examples/imports_exports.rs b/examples/imports_exports.rs index eb154317e3d..21a44401f3e 100644 --- a/examples/imports_exports.rs +++ b/examples/imports_exports.rs @@ -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, &env, &host_function_signature, |_ctx, _args| { + let host_function = Function::new(&mut store, &env, &host_function_signature, |_env, _args| { Ok(vec![Value::I32(42)]) }); diff --git a/examples/imports_function.rs b/examples/imports_function.rs index 0e08360e94d..1a31efc025a 100644 --- a/examples/imports_function.rs +++ b/examples/imports_function.rs @@ -46,9 +46,9 @@ 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 ctx1 = FunctionEnv::new(&mut store, ()); + let mut env1 = FunctionEnv::new(&mut store, ()); struct MyEnv; - let mut ctx2 = FunctionEnv::new(&mut store, MyEnv {}); + let mut env2 = FunctionEnv::new(&mut store, MyEnv {}); println!("Compiling module..."); // Let's compile the Wasm module. @@ -58,9 +58,9 @@ fn main() -> Result<(), Box> { let multiply_dynamic_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); let multiply_dynamic = Function::new( &mut store, - &ctx1, + &env1, &multiply_dynamic_signature, - |_ctx, args| { + |_env, args| { println!("Calling `multiply_dynamic`..."); let result = args[0].unwrap_i32() * 2; @@ -71,7 +71,7 @@ fn main() -> Result<(), Box> { }, ); - fn multiply(_ctx: FunctionEnvMut, a: i32) -> i32 { + fn multiply(_env: FunctionEnvMut, a: i32) -> i32 { println!("Calling `multiply_native`..."); let result = a * 3; @@ -79,7 +79,7 @@ fn main() -> Result<(), Box> { result } - let multiply_native = Function::new_native(&mut store, &ctx2, multiply); + let multiply_native = Function::new_native(&mut store, &env2, multiply); // Create an import object. let import_object = imports! { diff --git a/examples/imports_function_env.rs b/examples/imports_function_env.rs index 7eee4b02245..b28719fefc4 100644 --- a/examples/imports_function_env.rs +++ b/examples/imports_function_env.rs @@ -78,11 +78,11 @@ fn main() -> Result<(), Box> { } // Create the functions - fn get_counter(ctx: FunctionEnvMut) -> i32 { - *ctx.data().counter.lock().unwrap() + fn get_counter(env: FunctionEnvMut) -> i32 { + *env.data().counter.lock().unwrap() } - fn add_to_counter(mut ctx: FunctionEnvMut, add: i32) -> i32 { - let mut counter_ref = ctx.data_mut().counter.lock().unwrap(); + fn add_to_counter(mut env: FunctionEnvMut, add: i32) -> i32 { + let mut counter_ref = env.data_mut().counter.lock().unwrap(); *counter_ref += add; *counter_ref diff --git a/examples/table.rs b/examples/table.rs index 2250ba1a194..9383e8bf397 100644 --- a/examples/table.rs +++ b/examples/table.rs @@ -6,7 +6,7 @@ use wasmer_compiler::Universal; use wasmer_compiler_cranelift::Cranelift; /// A function we'll call through a table. -fn host_callback(_ctx: FunctionEnvMut<()>, arg1: i32, arg2: i32) -> i32 { +fn host_callback(_env: FunctionEnvMut<()>, arg1: i32, arg2: i32) -> i32 { arg1 + arg2 } diff --git a/lib/api/src/js/export.rs b/lib/api/src/js/export.rs index 4899443ce4d..f396e617c53 100644 --- a/lib/api/src/js/export.rs +++ b/lib/api/src/js/export.rs @@ -99,19 +99,22 @@ pub enum Export { impl Export { /// Return the export as a `JSValue`. - pub fn as_jsvalue<'context>(&self, ctx: &'context impl AsStoreRef) -> &'context JsValue { + pub fn as_jsvalue<'context>(&self, store: &'context impl AsStoreRef) -> &'context JsValue { match self { Self::Memory(js_wasm_memory) => js_wasm_memory - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .memory .as_ref(), - Self::Function(js_func) => js_func.get(ctx.as_store_ref().objects()).function.as_ref(), + Self::Function(js_func) => js_func + .get(store.as_store_ref().objects()) + .function + .as_ref(), Self::Table(js_wasm_table) => js_wasm_table - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .table .as_ref(), Self::Global(js_wasm_global) => js_wasm_global - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .global .as_ref(), } @@ -120,14 +123,14 @@ impl Export { /// Convert a `JsValue` into an `Export` within a given `Context`. pub fn from_js_value( val: JsValue, - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, extern_type: ExternType, ) -> Result { match extern_type { ExternType::Memory(memory_type) => { if val.is_instance_of::() { Ok(Self::Memory(InternalStoreHandle::new( - &mut ctx.objects_mut(), + &mut store.objects_mut(), VMMemory::new(val.unchecked_into::(), memory_type), ))) } else { @@ -143,7 +146,7 @@ impl Export { ExternType::Global(global_type) => { if val.is_instance_of::() { Ok(Self::Global(InternalStoreHandle::new( - &mut ctx.objects_mut(), + &mut store.objects_mut(), VMGlobal::new(val.unchecked_into::(), global_type), ))) } else { @@ -153,7 +156,7 @@ impl Export { ExternType::Function(function_type) => { if val.is_instance_of::() { Ok(Self::Function(InternalStoreHandle::new( - &mut ctx.objects_mut(), + &mut store.objects_mut(), VMFunction::new(val.unchecked_into::(), function_type), ))) } else { @@ -163,7 +166,7 @@ impl Export { ExternType::Table(table_type) => { if val.is_instance_of::() { Ok(Self::Table(InternalStoreHandle::new( - &mut ctx.objects_mut(), + &mut store.objects_mut(), VMTable::new(val.unchecked_into::
(), table_type), ))) } else { diff --git a/lib/api/src/js/exports.rs b/lib/api/src/js/exports.rs index 90b5116729b..6c4a8a9deb5 100644 --- a/lib/api/src/js/exports.rs +++ b/lib/api/src/js/exports.rs @@ -141,14 +141,14 @@ impl Exports { /// Get an export as a `TypedFunction`. pub fn get_native_function( &self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, name: &str, ) -> Result, ExportError> where Args: WasmTypeList, Rets: WasmTypeList, { - self.get_typed_function(ctx, name) + self.get_typed_function(store, name) } /// Get an export as a `TypedFunction`. @@ -169,7 +169,7 @@ impl Exports { /// Hack to get this working with nativefunc too pub fn get_with_generics<'a, T, Args, Rets>( &'a self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, name: &str, ) -> Result where @@ -179,7 +179,7 @@ impl Exports { { match self.map.get(name) { None => Err(ExportError::Missing(name.to_string())), - Some(extern_) => T::get_self_from_extern_with_generics(ctx, extern_), + Some(extern_) => T::get_self_from_extern_with_generics(store, extern_), } } @@ -187,7 +187,7 @@ impl Exports { /// This is useful for passing data into Context data, for example. pub fn get_with_generics_weak<'a, T, Args, Rets>( &'a self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, name: &str, ) -> Result where @@ -195,7 +195,7 @@ impl Exports { Rets: WasmTypeList, T: ExportableWithGenerics<'a, Args, Rets>, { - let out: T = self.get_with_generics(ctx, name)?; + let out: T = self.get_with_generics(store, name)?; Ok(out) } @@ -334,7 +334,7 @@ pub trait Exportable<'a>: Sized { pub trait ExportableWithGenerics<'a, Args: WasmTypeList, Rets: WasmTypeList>: Sized { /// Get an export with the given generics. fn get_self_from_extern_with_generics( - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, _extern: &'a Extern, ) -> Result; } @@ -343,7 +343,7 @@ pub trait ExportableWithGenerics<'a, Args: WasmTypeList, Rets: WasmTypeList>: Si /// with empty `Args` and `Rets`. impl<'a, T: Exportable<'a> + Clone + 'static> ExportableWithGenerics<'a, (), ()> for T { fn get_self_from_extern_with_generics( - _ctx: &impl AsStoreRef, + _store: &impl AsStoreRef, _extern: &'a Extern, ) -> Result { T::get_self_from_extern(_extern).map(|i| i.clone()) diff --git a/lib/api/src/js/externals/function.rs b/lib/api/src/js/externals/function.rs index 232f1ad1bb8..f5ee117cef9 100644 --- a/lib/api/src/js/externals/function.rs +++ b/lib/api/src/js/externals/function.rs @@ -96,7 +96,7 @@ impl Function { #[allow(clippy::cast_ptr_alignment)] pub fn new( store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, ty: FT, func: F, ) -> Self @@ -111,46 +111,46 @@ impl Function { let function_type = ty.into(); let func_ty = function_type.clone(); let raw_store = store.as_raw() as *mut u8; - let raw_ctx = ctx.clone(); + let raw_env = env.clone(); let wrapped_func: JsValue = match function_type.results().len() { 0 => Closure::wrap(Box::new(move |args: &Array| { let mut store: StoreMut = unsafe { StoreMut::from_raw(raw_store as _) }; - let mut ctx: FunctionEnvMut = raw_ctx.clone().into_mut(&mut store); + let mut env: FunctionEnvMut = raw_env.clone().into_mut(&mut store); let wasm_arguments = function_type .params() .iter() .enumerate() .map(|(i, param)| param_from_js(param, &args.get(i as u32))) .collect::>(); - let _results = func(ctx, &wasm_arguments)?; + let _results = func(env, &wasm_arguments)?; Ok(()) }) as Box Result<(), JsValue>>) .into_js_value(), 1 => Closure::wrap(Box::new(move |args: &Array| { let mut store: StoreMut = unsafe { StoreMut::from_raw(raw_store as _) }; - let mut ctx: FunctionEnvMut = raw_ctx.clone().into_mut(&mut store); + let mut env: FunctionEnvMut = raw_env.clone().into_mut(&mut store); let wasm_arguments = function_type .params() .iter() .enumerate() .map(|(i, param)| param_from_js(param, &args.get(i as u32))) .collect::>(); - let results = func(ctx, &wasm_arguments)?; + let results = func(env, &wasm_arguments)?; return Ok(result_to_js(&results[0])); }) as Box Result>) .into_js_value(), _n => Closure::wrap(Box::new(move |args: &Array| { let mut store: StoreMut = unsafe { StoreMut::from_raw(raw_store as _) }; - let mut ctx: FunctionEnvMut = raw_ctx.clone().into_mut(&mut store); + let mut env: FunctionEnvMut = raw_env.clone().into_mut(&mut store); let wasm_arguments = function_type .params() .iter() .enumerate() .map(|(i, param)| param_from_js(param, &args.get(i as u32))) .collect::>(); - let results = func(ctx, &wasm_arguments)?; + let results = func(env, &wasm_arguments)?; return Ok(results_to_js_array(&results)); }) as Box Result>) @@ -231,8 +231,8 @@ impl Function { /// assert_eq!(f.ty().params(), vec![Type::I32, Type::I32]); /// assert_eq!(f.ty().results(), vec![Type::I32]); /// ``` - pub fn ty<'context>(&self, ctx: &'context impl AsStoreRef) -> &'context FunctionType { - &self.handle.get(ctx.as_store_ref().objects()).ty + pub fn ty<'context>(&self, store: &'context impl AsStoreRef) -> &'context FunctionType { + &self.handle.get(store.as_store_ref().objects()).ty } /// Returns the number of parameters that this function takes. @@ -244,7 +244,7 @@ impl Function { /// # let mut store = Store::default(); /// # let env = FunctionEnv::new(&mut store, ()); /// # - /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { + /// fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// @@ -252,8 +252,8 @@ impl Function { /// /// assert_eq!(f.param_arity(&store), 2); /// ``` - pub fn param_arity(&self, ctx: &impl AsStoreRef) -> usize { - self.ty(ctx).params().len() + pub fn param_arity(&self, store: &impl AsStoreRef) -> usize { + self.ty(store).params().len() } /// Returns the number of results this function produces. @@ -265,7 +265,7 @@ impl Function { /// # let mut store = Store::default(); /// # let env = FunctionEnv::new(&mut store, ()); /// # - /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { + /// fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// @@ -273,8 +273,8 @@ impl Function { /// /// assert_eq!(f.result_arity(&store), 1); /// ``` - pub fn result_arity(&self, ctx: &impl AsStoreRef) -> usize { - self.ty(ctx).results().len() + pub fn result_arity(&self, store: &impl AsStoreRef) -> usize { + self.ty(store).results().len() } /// Call the `Function` function. @@ -313,8 +313,8 @@ impl Function { ) -> Result, RuntimeError> { let arr = js_sys::Array::new_with_length(params.len() as u32); - // let raw_ctx = ctx.as_raw() as *mut u8; - // let mut env = unsafe { FunctionEnvMut::from_raw(raw_ctx as *mut StoreInner<()>) }; + // let raw_env = env.as_raw() as *mut u8; + // let mut env = unsafe { FunctionEnvMut::from_raw(raw_env as *mut StoreInner<()>) }; for (i, param) in params.iter().enumerate() { let js_value = param.as_jsvalue(&store.as_store_ref()); @@ -345,19 +345,19 @@ impl Function { } } - pub(crate) fn from_vm_export(ctx: &mut impl AsStoreMut, vm_function: VMFunction) -> Self { + pub(crate) fn from_vm_export(store: &mut impl AsStoreMut, vm_function: VMFunction) -> Self { Self { - handle: StoreHandle::new(ctx.objects_mut(), vm_function), + handle: StoreHandle::new(store.objects_mut(), vm_function), } } pub(crate) fn from_vm_extern( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, internal: InternalStoreHandle, ) -> Self { Self { handle: unsafe { - StoreHandle::from_internal(ctx.as_store_ref().objects().id(), internal) + StoreHandle::from_internal(store.as_store_ref().objects().id(), internal) }, } } @@ -439,17 +439,16 @@ impl Function { /// ``` pub fn native( &self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, ) -> Result, RuntimeError> where Args: WasmTypeList, Rets: WasmTypeList, { - let vm_function = self.handle.get(ctx.as_store_ref().objects()); - + let ty = self.ty(store); // type check { - let expected = vm_function.ty.params(); + let expected = ty.params(); let given = Args::wasm_types(); if expected != given { @@ -462,7 +461,7 @@ impl Function { } { - let expected = vm_function.ty.results(); + let expected = ty.results(); let given = Rets::wasm_types(); if expected != given { @@ -484,8 +483,8 @@ impl Function { } /// Checks whether this `Function` can be used with the given context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { - self.handle.store_id() == ctx.as_store_ref().objects().id() + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { + self.handle.store_id() == store.as_store_ref().objects().id() } } @@ -657,7 +656,7 @@ mod inner { /// Constructs `Self` based on an array of values. /// /// # Safety - unsafe fn from_array(ctx: &mut impl AsStoreMut, array: Self::Array) -> Self; + unsafe fn from_array(store: &mut impl AsStoreMut, array: Self::Array) -> Self; /// Constructs `Self` based on a slice of values. /// @@ -668,7 +667,7 @@ mod inner { /// /// # Safety unsafe fn from_slice( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, slice: &[f64], ) -> Result; @@ -676,7 +675,7 @@ mod inner { /// (list) of values. /// /// # Safety - unsafe fn into_array(self, ctx: &mut impl AsStoreMut) -> Self::Array; + unsafe fn into_array(self, store: &mut impl AsStoreMut) -> Self::Array; /// Allocates and return an empty array of type `Array` that /// will hold a tuple (list) of values, usually to hold the @@ -687,13 +686,13 @@ mod inner { /// `CStruct`. /// /// # Safety - unsafe fn from_c_struct(ctx: &mut impl AsStoreMut, c_struct: Self::CStruct) -> Self; + unsafe fn from_c_struct(store: &mut impl AsStoreMut, c_struct: Self::CStruct) -> Self; /// Builds and returns a C struct of type `CStruct` from a /// tuple (list) of values. /// /// # Safety - unsafe fn into_c_struct(self, ctx: &mut impl AsStoreMut) -> Self::CStruct; + unsafe fn into_c_struct(self, store: &mut impl AsStoreMut) -> Self::CStruct; /// Writes the contents of a C struct to an array of `f64`. /// @@ -879,7 +878,7 @@ mod inner { #[allow(unused_mut)] #[allow(clippy::unused_unit)] #[allow(clippy::missing_safety_doc)] - unsafe fn from_array(mut _ctx: &mut impl AsStoreMut, array: Self::Array) -> Self { + unsafe fn from_array(mut _store: &mut impl AsStoreMut, array: Self::Array) -> Self { // Unpack items of the array. #[allow(non_snake_case)] let [ $( $x ),* ] = array; @@ -887,19 +886,19 @@ mod inner { // Build the tuple. ( $( - FromToNativeWasmType::from_native(NativeWasmTypeInto::from_raw(_ctx, $x)) + FromToNativeWasmType::from_native(NativeWasmTypeInto::from_raw(_store, $x)) ),* ) } #[allow(clippy::missing_safety_doc)] - unsafe fn from_slice(ctx: &mut impl AsStoreMut, slice: &[f64]) -> Result { - Ok(Self::from_array(ctx, slice.try_into()?)) + unsafe fn from_slice(store: &mut impl AsStoreMut, slice: &[f64]) -> Result { + Ok(Self::from_array(store, slice.try_into()?)) } #[allow(unused_mut)] #[allow(clippy::missing_safety_doc)] - unsafe fn into_array(self, mut _ctx: &mut impl AsStoreMut) -> Self::Array { + unsafe fn into_array(self, mut _store: &mut impl AsStoreMut) -> Self::Array { // Unpack items of the tuple. #[allow(non_snake_case)] let ( $( $x ),* ) = self; @@ -907,7 +906,7 @@ mod inner { // Build the array. [ $( - FromToNativeWasmType::to_native($x).into_raw(_ctx) + FromToNativeWasmType::to_native($x).into_raw(_store) ),* ] } @@ -920,28 +919,28 @@ mod inner { #[allow(unused_mut)] #[allow(clippy::unused_unit)] #[allow(clippy::missing_safety_doc)] - unsafe fn from_c_struct(mut _ctx: &mut impl AsStoreMut, c_struct: Self::CStruct) -> Self { + unsafe fn from_c_struct(mut _store: &mut impl AsStoreMut, c_struct: Self::CStruct) -> Self { // Unpack items of the C structure. #[allow(non_snake_case)] let $c_struct_name( $( $x ),* ) = c_struct; ( $( - FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(_ctx, $x)) + FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(_store, $x)) ),* ) } #[allow(unused_parens, non_snake_case, unused_mut)] #[allow(clippy::missing_safety_doc)] - unsafe fn into_c_struct(self, mut _ctx: &mut impl AsStoreMut) -> Self::CStruct { + unsafe fn into_c_struct(self, mut _store: &mut impl AsStoreMut) -> Self::CStruct { // Unpack items of the tuple. let ( $( $x ),* ) = self; // Build the C structure. $c_struct_name( $( - FromToNativeWasmType::to_native($x).into_abi(_ctx) + FromToNativeWasmType::to_native($x).into_abi(_store) ),* ) } @@ -1000,8 +999,8 @@ mod inner { let result = panic::catch_unwind(AssertUnwindSafe(|| { let handle: StoreHandle = StoreHandle::from_internal(store2.objects_mut().id(), InternalStoreHandle::from_index(handle_index).unwrap()); - let ctx: FunctionEnvMut = FunctionEnv::from_handle(handle).into_mut(&mut store2); - func(ctx, $( FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x)) ),* ).into_result() + let env: FunctionEnvMut = FunctionEnv::from_handle(handle).into_mut(&mut store2); + func(env, $( FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x)) ),* ).into_result() })); match result { diff --git a/lib/api/src/js/externals/global.rs b/lib/api/src/js/externals/global.rs index 47f4446a254..35e2fa46438 100644 --- a/lib/api/src/js/externals/global.rs +++ b/lib/api/src/js/externals/global.rs @@ -34,8 +34,8 @@ impl Global { /// assert_eq!(g.get(), Value::I32(1)); /// assert_eq!(g.ty().mutability, Mutability::Const); /// ``` - pub fn new(ctx: &mut impl AsStoreMut, val: Value) -> Self { - Self::from_value(ctx, val, Mutability::Const).unwrap() + pub fn new(store: &mut impl AsStoreMut, val: Value) -> Self { + Self::from_value(store, val, Mutability::Const).unwrap() } /// Create a mutable `Global` with the initial value [`Value`]. @@ -51,17 +51,17 @@ impl Global { /// assert_eq!(g.get(), Value::I32(1)); /// assert_eq!(g.ty().mutability, Mutability::Var); /// ``` - pub fn new_mut(ctx: &mut impl AsStoreMut, val: Value) -> Self { - Self::from_value(ctx, val, Mutability::Var).unwrap() + pub fn new_mut(store: &mut impl AsStoreMut, val: Value) -> Self { + Self::from_value(store, val, Mutability::Var).unwrap() } /// Create a `Global` with the initial value [`Value`] and the provided [`Mutability`]. fn from_value( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, val: Value, mutability: Mutability, ) -> Result { - if !val.is_from_store(ctx) { + if !val.is_from_store(store) { return Err(RuntimeError::new( "cross-`WasmerEnv` values are not supported", )); @@ -90,7 +90,7 @@ impl Global { let js_global = JSGlobal::new(&descriptor, &value).unwrap(); let vm_global = VMGlobal::new(js_global, global_ty); - Ok(Self::from_vm_export(ctx, vm_global)) + Ok(Self::from_vm_export(store, vm_global)) } /// Returns the [`GlobalType`] of the `Global`. diff --git a/lib/api/src/js/externals/memory.rs b/lib/api/src/js/externals/memory.rs index 59cc788874a..9103eeb1814 100644 --- a/lib/api/src/js/externals/memory.rs +++ b/lib/api/src/js/externals/memory.rs @@ -100,7 +100,7 @@ impl Memory { /// # /// let m = Memory::new(&store, MemoryType::new(1, None, false)).unwrap(); /// ``` - pub fn new(ctx: &mut impl AsStoreMut, ty: MemoryType) -> Result { + pub fn new(store: &mut impl AsStoreMut, ty: MemoryType) -> Result { let descriptor = js_sys::Object::new(); js_sys::Reflect::set(&descriptor, &"initial".into(), &ty.minimum.0.into()).unwrap(); if let Some(max) = ty.maximum { @@ -112,7 +112,7 @@ impl Memory { .map_err(|_e| MemoryError::Generic("Error while creating the memory".to_owned()))?; let vm_memory = VMMemory::new(js_memory, ty); - Ok(Self::from_vm_export(ctx, vm_memory)) + Ok(Self::from_vm_export(store, vm_memory)) } /// Returns the [`MemoryType`] of the `Memory`. @@ -128,8 +128,8 @@ impl Memory { /// /// assert_eq!(m.ty(), mt); /// ``` - pub fn ty(&self, ctx: &impl AsStoreRef) -> MemoryType { - self.handle.get(ctx.as_store_ref().objects()).ty + pub fn ty(&self, store: &impl AsStoreRef) -> MemoryType { + self.handle.get(store.as_store_ref().objects()).ty } /// Returns the pointer to the raw bytes of the `Memory`. @@ -139,11 +139,11 @@ impl Memory { } /// Returns the size (in bytes) of the `Memory`. - pub fn data_size(&self, ctx: &impl AsStoreRef) -> u64 { + pub fn data_size(&self, store: &impl AsStoreRef) -> u64 { js_sys::Reflect::get( &self .handle - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .memory .buffer(), &"byteLength".into(), @@ -165,11 +165,11 @@ impl Memory { /// /// assert_eq!(m.size(), Pages(1)); /// ``` - pub fn size(&self, ctx: &impl AsStoreRef) -> Pages { + pub fn size(&self, store: &impl AsStoreRef) -> Pages { let bytes = js_sys::Reflect::get( &self .handle - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .memory .buffer(), &"byteLength".into(), @@ -236,40 +236,40 @@ impl Memory { /// Used by tests #[doc(hidden)] - pub fn uint8view(&self, ctx: &impl AsStoreRef) -> js_sys::Uint8Array { + pub fn uint8view(&self, store: &impl AsStoreRef) -> js_sys::Uint8Array { js_sys::Uint8Array::new( &self .handle - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .memory .buffer(), ) } - pub(crate) fn buffer<'a>(&'a self, _ctx: &'a impl AsStoreRef) -> MemoryBuffer<'a> { + pub(crate) fn buffer<'a>(&'a self, _store: &'a impl AsStoreRef) -> MemoryBuffer<'a> { MemoryBuffer { base: &self.view as *const _ as *mut _, marker: PhantomData, } } - pub(crate) fn from_vm_export(ctx: &mut impl AsStoreMut, vm_memory: VMMemory) -> Self { + pub(crate) fn from_vm_export(store: &mut impl AsStoreMut, vm_memory: VMMemory) -> Self { let view = js_sys::Uint8Array::new(&vm_memory.memory.buffer()); Self { - handle: StoreHandle::new(ctx.objects_mut(), vm_memory), + handle: StoreHandle::new(store.objects_mut(), vm_memory), view, } } pub(crate) fn from_vm_extern( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, internal: InternalStoreHandle, ) -> Self { let view = - js_sys::Uint8Array::new(&internal.get(ctx.as_store_ref().objects()).memory.buffer()); + js_sys::Uint8Array::new(&internal.get(store.as_store_ref().objects()).memory.buffer()); Self { handle: unsafe { - StoreHandle::from_internal(ctx.as_store_ref().objects().id(), internal) + StoreHandle::from_internal(store.as_store_ref().objects().id(), internal) }, view, } @@ -284,7 +284,7 @@ impl Memory { /// concurrent writes. pub fn read( &self, - _ctx: &impl AsStoreRef, + _store: &impl AsStoreRef, offset: u64, data: &mut [u8], ) -> Result<(), MemoryAccessError> { @@ -314,7 +314,7 @@ impl Memory { /// concurrent writes. pub fn read_uninit<'a>( &self, - _ctx: &impl AsStoreRef, + _store: &impl AsStoreRef, offset: u64, buf: &'a mut [MaybeUninit], ) -> Result<&'a mut [u8], MemoryAccessError> { @@ -349,7 +349,7 @@ impl Memory { /// concurrent reads/writes. pub fn write( &self, - _ctx: &mut impl AsStoreMut, + _store: &mut impl AsStoreMut, offset: u64, data: &[u8], ) -> Result<(), MemoryAccessError> { @@ -368,8 +368,8 @@ impl Memory { } /// Checks whether this `Global` can be used with the given context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { - self.handle.store_id() == ctx.as_store_ref().objects().id() + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { + self.handle.store_id() == store.as_store_ref().objects().id() } } diff --git a/lib/api/src/js/externals/mod.rs b/lib/api/src/js/externals/mod.rs index ef34a5cc175..a567d7ec991 100644 --- a/lib/api/src/js/externals/mod.rs +++ b/lib/api/src/js/externals/mod.rs @@ -34,32 +34,32 @@ pub enum Extern { impl Extern { /// Return the underlying type of the inner `Extern`. - pub fn ty(&self, ctx: &impl AsStoreRef) -> ExternType { + pub fn ty(&self, store: &impl AsStoreRef) -> ExternType { match self { - Self::Function(ft) => ExternType::Function(ft.ty(ctx).clone()), - Self::Memory(ft) => ExternType::Memory(ft.ty(ctx)), - Self::Table(tt) => ExternType::Table(tt.ty(ctx)), - Self::Global(gt) => ExternType::Global(gt.ty(ctx)), + Self::Function(ft) => ExternType::Function(ft.ty(store).clone()), + Self::Memory(ft) => ExternType::Memory(ft.ty(store)), + Self::Table(tt) => ExternType::Table(tt.ty(store)), + Self::Global(gt) => ExternType::Global(gt.ty(store)), } } /// Create an `Extern` from an `wasmer_compiler::Export`. - pub fn from_vm_export(ctx: &mut impl AsStoreMut, export: Export) -> Self { + pub fn from_vm_export(store: &mut impl AsStoreMut, export: Export) -> Self { match export { - Export::Function(f) => Self::Function(Function::from_vm_extern(ctx, f)), - Export::Memory(m) => Self::Memory(Memory::from_vm_extern(ctx, m)), - Export::Global(g) => Self::Global(Global::from_vm_extern(ctx, g)), - Export::Table(t) => Self::Table(Table::from_vm_extern(ctx, t)), + Export::Function(f) => Self::Function(Function::from_vm_extern(store, f)), + Export::Memory(m) => Self::Memory(Memory::from_vm_extern(store, m)), + Export::Global(g) => Self::Global(Global::from_vm_extern(store, g)), + Export::Table(t) => Self::Table(Table::from_vm_extern(store, t)), } } /// Checks whether this `Extern` can be used with the given context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { match self { - Self::Function(val) => val.is_from_store(ctx), - Self::Memory(val) => val.is_from_store(ctx), - Self::Global(val) => val.is_from_store(ctx), - Self::Table(val) => val.is_from_store(ctx), + Self::Function(val) => val.is_from_store(store), + Self::Memory(val) => val.is_from_store(store), + Self::Global(val) => val.is_from_store(store), + Self::Table(val) => val.is_from_store(store), } } @@ -74,12 +74,12 @@ impl Extern { } impl AsJs for Extern { - fn as_jsvalue(&self, ctx: &impl AsStoreRef) -> wasm_bindgen::JsValue { + fn as_jsvalue(&self, store: &impl AsStoreRef) -> wasm_bindgen::JsValue { match self { - Self::Function(_) => self.to_export().as_jsvalue(ctx), - Self::Global(_) => self.to_export().as_jsvalue(ctx), - Self::Table(_) => self.to_export().as_jsvalue(ctx), - Self::Memory(_) => self.to_export().as_jsvalue(ctx), + Self::Function(_) => self.to_export().as_jsvalue(store), + Self::Global(_) => self.to_export().as_jsvalue(store), + Self::Table(_) => self.to_export().as_jsvalue(store), + Self::Memory(_) => self.to_export().as_jsvalue(store), } .clone() } diff --git a/lib/api/src/js/externals/table.rs b/lib/api/src/js/externals/table.rs index dd9143f2fd7..ec7565a8c5c 100644 --- a/lib/api/src/js/externals/table.rs +++ b/lib/api/src/js/externals/table.rs @@ -25,14 +25,14 @@ fn set_table_item(table: &VMTable, item_index: u32, item: &Function) -> Result<( table.table.set(item_index, item).map_err(|e| e.into()) } -fn get_function(ctx: &mut impl AsStoreMut, val: Value) -> Result { - if !val.is_from_store(ctx) { +fn get_function(store: &mut impl AsStoreMut, val: Value) -> Result { + if !val.is_from_store(store) { return Err(RuntimeError::new("cannot pass Value across contexts")); } match val { Value::FuncRef(Some(ref func)) => Ok(func .handle - .get(&ctx.as_store_ref().objects()) + .get(&store.as_store_ref().objects()) .function .clone() .into()), @@ -49,11 +49,11 @@ impl Table { /// This function will construct the `Table` using the store /// [`BaseTunables`][crate::js::tunables::BaseTunables]. pub fn new( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, ty: TableType, init: Value, ) -> Result { - let mut ctx = ctx; + let mut store = store; let descriptor = js_sys::Object::new(); js_sys::Reflect::set(&descriptor, &"initial".into(), &ty.minimum.into())?; if let Some(max) = ty.maximum { @@ -65,33 +65,33 @@ impl Table { let table = VMTable::new(js_table, ty); let num_elements = table.table.length(); - let func = get_function(&mut ctx, init)?; + let func = get_function(&mut store, init)?; for i in 0..num_elements { set_table_item(&table, i, &func)?; } Ok(Self { - handle: StoreHandle::new(ctx.objects_mut(), table), + handle: StoreHandle::new(store.objects_mut(), table), }) } /// Returns the [`TableType`] of the `Table`. - pub fn ty(&self, ctx: &impl AsStoreRef) -> TableType { - self.handle.get(ctx.as_store_ref().objects()).ty + pub fn ty(&self, store: &impl AsStoreRef) -> TableType { + self.handle.get(store.as_store_ref().objects()).ty } /// Retrieves an element of the table at the provided `index`. - pub fn get(&self, ctx: &mut impl AsStoreMut, index: u32) -> Option { + pub fn get(&self, store: &mut impl AsStoreMut, index: u32) -> Option { if let Some(func) = self .handle - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .table .get(index) .ok() { let ty = FunctionType::new(vec![], vec![]); let vm_function = VMFunction::new(func, ty); - let function = crate::js::externals::Function::from_vm_export(ctx, vm_function); + let function = crate::js::externals::Function::from_vm_export(store, vm_function); Some(Value::FuncRef(Some(function))) } else { None @@ -101,17 +101,20 @@ impl Table { /// Sets an element `val` in the Table at the provided `index`. pub fn set( &self, - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, index: u32, val: Value, ) -> Result<(), RuntimeError> { - let item = get_function(ctx, val)?; - set_table_item(self.handle.get_mut(ctx.objects_mut()), index, &item) + let item = get_function(store, val)?; + set_table_item(self.handle.get_mut(store.objects_mut()), index, &item) } /// Retrieves the size of the `Table` (in elements) - pub fn size(&self, ctx: &impl AsStoreRef) -> u32 { - self.handle.get(ctx.as_store_ref().objects()).table.length() + pub fn size(&self, store: &impl AsStoreRef) -> u32 { + self.handle + .get(store.as_store_ref().objects()) + .table + .length() } /// Grows the size of the `Table` by `delta`, initializating @@ -150,19 +153,19 @@ impl Table { } pub(crate) fn from_vm_extern( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, internal: InternalStoreHandle, ) -> Self { Self { handle: unsafe { - StoreHandle::from_internal(ctx.as_store_ref().objects().id(), internal) + StoreHandle::from_internal(store.as_store_ref().objects().id(), internal) }, } } /// Checks whether this `Table` can be used with the given context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { - self.handle.store_id() == ctx.as_store_ref().objects().id() + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { + self.handle.store_id() == store.as_store_ref().objects().id() } /// Get access to the backing VM value for this extern. This function is for @@ -175,9 +178,9 @@ impl Table { #[doc(hidden)] pub unsafe fn get_vm_table<'context>( &self, - ctx: &'context impl AsStoreRef, + store: &'context impl AsStoreRef, ) -> &'context VMTable { - self.handle.get(ctx.as_store_ref().objects()) + self.handle.get(store.as_store_ref().objects()) } } diff --git a/lib/api/src/js/imports.rs b/lib/api/src/js/imports.rs index 2c43350c5c0..94866e66842 100644 --- a/lib/api/src/js/imports.rs +++ b/lib/api/src/js/imports.rs @@ -151,7 +151,7 @@ impl Imports { } /// Returns the `Imports` as a Javascript `Object` - pub fn as_jsobject(&self, ctx: &impl AsStoreRef) -> js_sys::Object { + pub fn as_jsobject(&self, store: &impl AsStoreRef) -> js_sys::Object { let imports = js_sys::Object::new(); let namespaces: HashMap<&str, Vec<(&str, &Extern)>> = self.map @@ -166,7 +166,7 @@ impl Imports { for (ns, exports) in namespaces.into_iter() { let import_namespace = js_sys::Object::new(); for (name, ext) in exports { - js_sys::Reflect::set(&import_namespace, &name.into(), &ext.as_jsvalue(ctx)) + js_sys::Reflect::set(&import_namespace, &name.into(), &ext.as_jsvalue(store)) .expect("Error while setting into the js namespace object"); } js_sys::Reflect::set(&imports, &ns.into(), &import_namespace.into()) diff --git a/lib/api/src/js/instance.rs b/lib/api/src/js/instance.rs index 181ccff54f9..6d2762adf66 100644 --- a/lib/api/src/js/instance.rs +++ b/lib/api/src/js/instance.rs @@ -61,16 +61,16 @@ impl Instance { /// * Link errors that happen when plugging the imports into the instance /// * Runtime errors that happen when running the module `start` function. pub fn new( - mut ctx: &mut impl AsStoreMut, + mut store: &mut impl AsStoreMut, module: &Module, imports: &Imports, ) -> Result { let import_copy = imports.clone(); let (instance, _imports): (StoreHandle, Vec) = module - .instantiate(&mut ctx, imports) + .instantiate(&mut store, imports) .map_err(|e| InstantiationError::Start(e))?; - let self_instance = Self::from_module_and_instance(ctx, module, instance, import_copy)?; + let self_instance = Self::from_module_and_instance(store, module, instance, import_copy)?; //self_instance.init_envs(&imports.iter().map(Extern::to_export).collect::>())?; Ok(self_instance) } @@ -85,12 +85,12 @@ impl Instance { /// /// *This method is only available when targeting JS environments* pub fn from_module_and_instance( - mut ctx: &mut impl AsStoreMut, + mut store: &mut impl AsStoreMut, module: &Module, instance: StoreHandle, imports: Imports, ) -> Result { - let instance_exports = instance.get(ctx.as_store_ref().objects()).exports(); + let instance_exports = instance.get(store.as_store_ref().objects()).exports(); let exports = module .exports() .map(|export_type| { @@ -104,8 +104,8 @@ impl Instance { )) })?; let export: Export = - Export::from_js_value(js_export, &mut ctx, extern_type)?.into(); - let extern_ = Extern::from_vm_export(&mut ctx, export); + Export::from_js_value(js_export, &mut store, extern_type)?.into(); + let extern_ = Extern::from_vm_export(&mut store, export); Ok((name.to_string(), extern_)) }) .collect::>()?; @@ -125,8 +125,11 @@ impl Instance { /// Returns the inner WebAssembly Instance #[doc(hidden)] - pub fn raw<'context>(&self, ctx: &'context impl AsStoreRef) -> &'context WebAssembly::Instance { - &self._handle.get(ctx.as_store_ref().objects()) + pub fn raw<'context>( + &self, + store: &'context impl AsStoreRef, + ) -> &'context WebAssembly::Instance { + &self._handle.get(store.as_store_ref().objects()) } } diff --git a/lib/api/src/js/js_import_object.rs b/lib/api/src/js/js_import_object.rs index e890c5bc144..1370d8bd336 100644 --- a/lib/api/src/js/js_import_object.rs +++ b/lib/api/src/js/js_import_object.rs @@ -53,7 +53,7 @@ impl JsImportObject { /// ``` pub fn get_export( &self, - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, module: &str, name: &str, ) -> Result { @@ -63,7 +63,11 @@ impl JsImportObject { .module_imports .get(&(module.to_string(), name.to_string())) { - Some(extern_type) => Ok(Export::from_js_value(js_export, ctx, extern_type.clone())?), + Some(extern_type) => Ok(Export::from_js_value( + js_export, + store, + extern_type.clone(), + )?), None => Err(WasmError::Generic(format!( "Name {} not found in module {}", name, module diff --git a/lib/api/src/js/mem_access.rs b/lib/api/src/js/mem_access.rs index f9416b067ab..be476b8bd41 100644 --- a/lib/api/src/js/mem_access.rs +++ b/lib/api/src/js/mem_access.rs @@ -61,9 +61,9 @@ pub struct WasmRef<'a, T: ValueType> { impl<'a, T: ValueType> WasmRef<'a, T> { /// Creates a new `WasmRef` at the given offset in a memory. #[inline] - pub fn new(ctx: &'a impl AsStoreRef, memory: &'a Memory, offset: u64) -> Self { + pub fn new(store: &'a impl AsStoreRef, memory: &'a Memory, offset: u64) -> Self { Self { - buffer: memory.buffer(ctx), + buffer: memory.buffer(store), offset, marker: PhantomData, } @@ -160,7 +160,7 @@ impl<'a, T: ValueType> WasmSlice<'a, T> { /// Returns a `MemoryAccessError` if the slice length overflows. #[inline] pub fn new( - ctx: &'a impl AsStoreRef, + store: &'a impl AsStoreRef, memory: &'a Memory, offset: u64, len: u64, @@ -172,7 +172,7 @@ impl<'a, T: ValueType> WasmSlice<'a, T> { .checked_add(total_len) .ok_or(MemoryAccessError::Overflow)?; Ok(Self { - buffer: memory.buffer(ctx), + buffer: memory.buffer(store), offset, len, marker: PhantomData, diff --git a/lib/api/src/js/native.rs b/lib/api/src/js/native.rs index 588f9618254..6d34ca82425 100644 --- a/lib/api/src/js/native.rs +++ b/lib/api/src/js/native.rs @@ -66,11 +66,11 @@ macro_rules! impl_native_traits { { /// Call the typed func and return results. #[allow(clippy::too_many_arguments)] - pub fn call(&self, mut ctx: &mut impl AsStoreMut, $( $x: $x, )* ) -> Result where + pub fn call(&self, mut store: &mut impl AsStoreMut, $( $x: $x, )* ) -> Result where $( $x: FromToNativeWasmType + crate::js::NativeWasmTypeInto, )* { - let params_list: Vec = vec![ $( JsValue::from_f64($x.into_raw(&mut ctx))),* ]; - let results = self.handle.get(ctx.as_store_ref().objects()).function.apply( + let params_list: Vec = vec![ $( JsValue::from_f64($x.into_raw(&mut store))),* ]; + let results = self.handle.get(store.as_store_ref().objects()).function.apply( &JsValue::UNDEFINED, &Array::from_iter(params_list.iter()) )?; @@ -81,7 +81,7 @@ macro_rules! impl_native_traits { 1 => unsafe { let ty = Rets::wasm_types()[0]; let val = param_from_js(&ty, &results); - *mut_rets = val.as_raw(&mut ctx); + *mut_rets = val.as_raw(&mut store); } _n => { let results: Array = results.into(); @@ -90,12 +90,12 @@ macro_rules! impl_native_traits { unsafe { let val = param_from_js(&ret_type, &ret); let slot = mut_rets.add(i); - *slot = val.as_raw(&mut ctx); + *slot = val.as_raw(&mut store); } } } } - Ok(unsafe { Rets::from_array(ctx, rets_list_array) }) + Ok(unsafe { Rets::from_array(store, rets_list_array) }) } } @@ -106,9 +106,9 @@ macro_rules! impl_native_traits { $( $x: FromToNativeWasmType, )* Rets: WasmTypeList, { - fn get_self_from_extern_with_generics(ctx: &impl AsStoreRef, _extern: &crate::js::externals::Extern) -> Result { + fn get_self_from_extern_with_generics(store: &impl AsStoreRef, _extern: &crate::js::externals::Extern) -> Result { use crate::js::exports::Exportable; - crate::js::Function::get_self_from_extern(_extern)?.native(ctx).map_err(|_| crate::js::exports::ExportError::IncompatibleType) + crate::js::Function::get_self_from_extern(_extern)?.native(store).map_err(|_| crate::js::exports::ExportError::IncompatibleType) } } }; diff --git a/lib/api/src/js/native_type.rs b/lib/api/src/js/native_type.rs index d2a3abc1b68..814abf4c1fb 100644 --- a/lib/api/src/js/native_type.rs +++ b/lib/api/src/js/native_type.rs @@ -11,105 +11,105 @@ use super::store::AsStoreMut; /// types with a context. pub trait NativeWasmTypeInto: NativeWasmType + Sized { #[doc(hidden)] - fn into_abi(self, ctx: &mut impl AsStoreMut) -> Self::Abi; + fn into_abi(self, store: &mut impl AsStoreMut) -> Self::Abi; #[doc(hidden)] - unsafe fn from_abi(ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self; + unsafe fn from_abi(store: &mut impl AsStoreMut, abi: Self::Abi) -> Self; /// Convert self to raw value representation. - fn into_raw(self, ctx: &mut impl AsStoreMut) -> f64; + fn into_raw(self, store: &mut impl AsStoreMut) -> f64; /// Convert to self from raw value representation. /// /// # Safety /// - unsafe fn from_raw(ctx: &mut impl AsStoreMut, raw: f64) -> Self; + unsafe fn from_raw(store: &mut impl AsStoreMut, raw: f64) -> Self; } impl NativeWasmTypeInto for i32 { #[inline] - unsafe fn from_abi(_ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + unsafe fn from_abi(_store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { abi } #[inline] - fn into_abi(self, _ctx: &mut impl AsStoreMut) -> Self::Abi { + fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi { self } #[inline] - fn into_raw(self, _ctx: &mut impl AsStoreMut) -> f64 { + fn into_raw(self, _store: &mut impl AsStoreMut) -> f64 { self.into() } #[inline] - unsafe fn from_raw(_ctx: &mut impl AsStoreMut, raw: f64) -> Self { + unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: f64) -> Self { raw as _ } } impl NativeWasmTypeInto for i64 { #[inline] - unsafe fn from_abi(_ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + unsafe fn from_abi(_store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { abi } #[inline] - fn into_abi(self, _ctx: &mut impl AsStoreMut) -> Self::Abi { + fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi { self } #[inline] - fn into_raw(self, _ctx: &mut impl AsStoreMut) -> f64 { + fn into_raw(self, _store: &mut impl AsStoreMut) -> f64 { self as _ } #[inline] - unsafe fn from_raw(_ctx: &mut impl AsStoreMut, raw: f64) -> Self { + unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: f64) -> Self { raw as _ } } impl NativeWasmTypeInto for f32 { #[inline] - unsafe fn from_abi(_ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + unsafe fn from_abi(_store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { abi } #[inline] - fn into_abi(self, _ctx: &mut impl AsStoreMut) -> Self::Abi { + fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi { self } #[inline] - fn into_raw(self, _ctx: &mut impl AsStoreMut) -> f64 { + fn into_raw(self, _store: &mut impl AsStoreMut) -> f64 { self as _ } #[inline] - unsafe fn from_raw(_ctx: &mut impl AsStoreMut, raw: f64) -> Self { + unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: f64) -> Self { raw as _ } } impl NativeWasmTypeInto for f64 { #[inline] - unsafe fn from_abi(_ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + unsafe fn from_abi(_store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { abi } #[inline] - fn into_abi(self, _ctx: &mut impl AsStoreMut) -> Self::Abi { + fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi { self } #[inline] - fn into_raw(self, _ctx: &mut impl AsStoreMut) -> f64 { + fn into_raw(self, _store: &mut impl AsStoreMut) -> f64 { self } #[inline] - unsafe fn from_raw(_ctx: &mut impl AsStoreMut, raw: f64) -> Self { + unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: f64) -> Self { raw } } diff --git a/lib/api/src/js/ptr.rs b/lib/api/src/js/ptr.rs index 1e2af555f2f..1d3f79ffa56 100644 --- a/lib/api/src/js/ptr.rs +++ b/lib/api/src/js/ptr.rs @@ -138,25 +138,25 @@ impl WasmPtr { /// Creates a `WasmRef` from this `WasmPtr` which allows reading and /// mutating of the value being pointed to. #[inline] - pub fn deref<'a>(self, ctx: &'a impl AsStoreRef, memory: &'a Memory) -> WasmRef<'a, T> { - WasmRef::new(ctx, memory, self.offset.into()) + pub fn deref<'a>(self, store: &'a impl AsStoreRef, memory: &'a Memory) -> WasmRef<'a, T> { + WasmRef::new(store, memory, self.offset.into()) } /// Reads the address pointed to by this `WasmPtr` in a memory. #[inline] - pub fn read(self, ctx: &impl AsStoreRef, memory: &Memory) -> Result { - self.deref(ctx, memory).read() + pub fn read(self, store: &impl AsStoreRef, memory: &Memory) -> Result { + self.deref(store, memory).read() } /// Writes to the address pointed to by this `WasmPtr` in a memory. #[inline] pub fn write( self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, memory: &Memory, val: T, ) -> Result<(), MemoryAccessError> { - self.deref(ctx, memory).write(val) + self.deref(store, memory).write(val) } /// Creates a `WasmSlice` starting at this `WasmPtr` which allows reading @@ -167,11 +167,11 @@ impl WasmPtr { #[inline] pub fn slice<'a>( self, - ctx: &'a impl AsStoreRef, + store: &'a impl AsStoreRef, memory: &'a Memory, len: M::Offset, ) -> Result, MemoryAccessError> { - WasmSlice::new(ctx, memory, self.offset.into(), len.into()) + WasmSlice::new(store, memory, self.offset.into(), len.into()) } /// Reads a sequence of values from this `WasmPtr` until a value that @@ -181,14 +181,14 @@ impl WasmPtr { #[inline] pub fn read_until<'a>( self, - ctx: &'a impl AsStoreRef, + store: &'a impl AsStoreRef, memory: &'a Memory, mut end: impl FnMut(&T) -> bool, ) -> Result, MemoryAccessError> { let mut vec = Vec::new(); for i in 0u64.. { let i = M::Offset::try_from(i).map_err(|_| MemoryAccessError::Overflow)?; - let val = self.add_offset(i)?.deref(ctx, memory).read()?; + let val = self.add_offset(i)?.deref(store, memory).read()?; if end(&val) { break; } @@ -206,11 +206,11 @@ impl WasmPtr { #[inline] pub fn read_utf8_string<'a>( self, - ctx: &'a impl AsStoreRef, + store: &'a impl AsStoreRef, memory: &'a Memory, len: M::Offset, ) -> Result { - let vec = self.slice(ctx, memory, len)?.read_to_vec()?; + let vec = self.slice(store, memory, len)?.read_to_vec()?; Ok(String::from_utf8(vec)?) } @@ -221,10 +221,10 @@ impl WasmPtr { #[inline] pub fn read_utf8_string_with_nul<'a>( self, - ctx: &'a impl AsStoreRef, + store: &'a impl AsStoreRef, memory: &'a Memory, ) -> Result { - let vec = self.read_until(ctx, memory, |&byte| byte == 0)?; + let vec = self.read_until(store, memory, |&byte| byte == 0)?; Ok(String::from_utf8(vec)?) } } diff --git a/lib/api/src/js/store.rs b/lib/api/src/js/store.rs index a2553df7407..59bc2577fd5 100644 --- a/lib/api/src/js/store.rs +++ b/lib/api/src/js/store.rs @@ -218,19 +218,19 @@ mod objects { /// Trait to represent an object managed by a context. This is implemented on /// the VM types managed by the context. pub trait StoreObject: Sized { - fn list(ctx: &StoreObjects) -> &Vec; - fn list_mut(ctx: &mut StoreObjects) -> &mut Vec; + fn list(store: &StoreObjects) -> &Vec; + fn list_mut(store: &mut StoreObjects) -> &mut Vec; } macro_rules! impl_store_object { ($($field:ident => $ty:ty,)*) => { $( impl StoreObject for $ty { - fn list(ctx: &StoreObjects) -> &Vec { - &ctx.$field + fn list(store: &StoreObjects) -> &Vec { + &store.$field } - fn list_mut(ctx: &mut StoreObjects) -> &mut Vec { - &mut ctx.$field + fn list_mut(store: &mut StoreObjects) -> &mut Vec { + &mut store.$field } } )* @@ -318,23 +318,23 @@ mod objects { impl StoreHandle { /// Moves the given object into a context and returns a handle to it. - pub fn new(ctx: &mut StoreObjects, val: T) -> Self { + pub fn new(store: &mut StoreObjects, val: T) -> Self { Self { - id: ctx.id, - internal: InternalStoreHandle::new(ctx, val), + id: store.id, + internal: InternalStoreHandle::new(store, val), } } /// Returns a reference to the object that this handle points to. - pub fn get<'a>(&self, ctx: &'a StoreObjects) -> &'a T { - assert_eq!(self.id, ctx.id, "object used with the wrong context"); - self.internal.get(ctx) + pub fn get<'a>(&self, store: &'a StoreObjects) -> &'a T { + assert_eq!(self.id, store.id, "object used with the wrong context"); + self.internal.get(store) } /// Returns a mutable reference to the object that this handle points to. - pub fn get_mut<'a>(&self, ctx: &'a mut StoreObjects) -> &'a mut T { - assert_eq!(self.id, ctx.id, "object used with the wrong context"); - self.internal.get_mut(ctx) + pub fn get_mut<'a>(&self, store: &'a mut StoreObjects) -> &'a mut T { + assert_eq!(self.id, store.id, "object used with the wrong context"); + self.internal.get_mut(store) } /// Returns the internal handle contains within this handle. @@ -390,8 +390,8 @@ mod objects { impl InternalStoreHandle { /// Moves the given object into a context and returns a handle to it. - pub fn new(ctx: &mut StoreObjects, val: T) -> Self { - let list = T::list_mut(ctx); + pub fn new(store: &mut StoreObjects, val: T) -> Self { + let list = T::list_mut(store); let idx = NonZeroUsize::new(list.len() + 1).unwrap(); list.push(val); Self { @@ -401,13 +401,13 @@ mod objects { } /// Returns a reference to the object that this handle points to. - pub fn get<'a>(&self, ctx: &'a StoreObjects) -> &'a T { - &T::list(ctx)[self.idx.get() - 1] + pub fn get<'a>(&self, store: &'a StoreObjects) -> &'a T { + &T::list(store)[self.idx.get() - 1] } /// Returns a mutable reference to the object that this handle points to. - pub fn get_mut<'a>(&self, ctx: &'a mut StoreObjects) -> &'a mut T { - &mut T::list_mut(ctx)[self.idx.get() - 1] + pub fn get_mut<'a>(&self, store: &'a mut StoreObjects) -> &'a mut T { + &mut T::list_mut(store)[self.idx.get() - 1] } pub(crate) fn index(&self) -> usize { diff --git a/lib/api/src/js/types.rs b/lib/api/src/js/types.rs index ebce0b27808..0e9716073f4 100644 --- a/lib/api/src/js/types.rs +++ b/lib/api/src/js/types.rs @@ -19,7 +19,7 @@ pub use wasmer_types::{ //pub type Value = Value; pub trait AsJs { - fn as_jsvalue(&self, ctx: &impl AsStoreRef) -> JsValue; + fn as_jsvalue(&self, store: &impl AsStoreRef) -> JsValue; } #[inline] @@ -37,7 +37,7 @@ pub fn param_from_js(ty: &ValType, js_val: &JsValue) -> Value { } impl AsJs for Value { - fn as_jsvalue(&self, ctx: &impl AsStoreRef) -> JsValue { + fn as_jsvalue(&self, store: &impl AsStoreRef) -> JsValue { match self { Self::I32(i) => JsValue::from_f64(*i as f64), Self::I64(i) => JsValue::from_f64(*i as f64), @@ -45,7 +45,7 @@ impl AsJs for Value { Self::F64(f) => JsValue::from_f64(*f), Self::FuncRef(Some(func)) => func .handle - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .function .clone() .into(), diff --git a/lib/api/src/js/value.rs b/lib/api/src/js/value.rs index 99188e20d98..a285abe3f13 100644 --- a/lib/api/src/js/value.rs +++ b/lib/api/src/js/value.rs @@ -82,7 +82,7 @@ impl Value { } /// Converts the `Value` into a `f64`. - pub fn as_raw(&self, ctx: &impl AsStoreRef) -> f64 { + pub fn as_raw(&self, store: &impl AsStoreRef) -> f64 { match *self { Self::I32(v) => v as f64, Self::I64(v) => v as f64, @@ -90,7 +90,7 @@ impl Value { Self::F64(v) => v, Self::FuncRef(Some(ref f)) => f .handle - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .function .as_f64() .unwrap_or(0_f64), //TODO is this correct? @@ -105,7 +105,7 @@ impl Value { /// /// # Safety /// - pub unsafe fn from_raw(_ctx: &impl AsStoreRef, ty: Type, raw: f64) -> Self { + pub unsafe fn from_raw(_store: &impl AsStoreRef, ty: Type, raw: f64) -> Self { match ty { Type::I32 => Self::I32(raw as i32), Type::I64 => Self::I64(raw as i64), @@ -116,7 +116,7 @@ impl Value { Type::ExternRef => todo!(), //Self::ExternRef( //{ - //VMExternRef::from_raw(raw).map(|e| ExternRef::from_vm_externref(ctx, e)), + //VMExternRef::from_raw(raw).map(|e| ExternRef::from_vm_externref(store, e)), //), } } @@ -128,7 +128,7 @@ impl Value { /// /// Externref and funcref values are tied to a context and can only be used /// with that context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { match self { Self::I32(_) | Self::I64(_) @@ -136,8 +136,8 @@ impl Value { | Self::F64(_) //| Self::ExternRef(None) | Self::FuncRef(None) => true, - //Self::ExternRef(Some(e)) => e.is_from_store(ctx), - Self::FuncRef(Some(f)) => f.is_from_store(ctx), + //Self::ExternRef(Some(e)) => e.is_from_store(store), + Self::FuncRef(Some(f)) => f.is_from_store(store), } } diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index d1160e5e4b9..6a4686033ff 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -54,7 +54,6 @@ //! "#; //! //! let mut store = Store::default(); -//! let env = FunctionEnv::new(&mut store, ()); //! let module = Module::new(&store, &module_wat)?; //! // The module doesn't import anything, so we create an empty import object. //! let import_object = imports! {}; @@ -152,11 +151,11 @@ //! //! ``` //! # use wasmer::{imports, Function, FunctionEnv, FunctionEnvMut, Memory, MemoryType, Store, Imports}; -//! # fn imports_example(mut ctx: FunctionEnv<()>, mut store: &mut Store) -> Imports { +//! # fn imports_example(mut env: FunctionEnv<()>, mut store: &mut Store) -> Imports { //! let memory = Memory::new(&mut store, MemoryType::new(1, None, false)).unwrap(); //! imports! { //! "env" => { -//! "my_function" => Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>| println!("Hello")), +//! "my_function" => Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>| println!("Hello")), //! "memory" => memory, //! } //! } @@ -168,7 +167,7 @@ //! //! ``` //! # use wasmer::{imports, Instance, FunctionEnv, Memory, TypedFunction, Store}; -//! # fn exports_example(mut ctx: FunctionEnv<()>, mut store: &mut Store, instance: &Instance) -> anyhow::Result<()> { +//! # fn exports_example(mut env: FunctionEnv<()>, mut store: &mut Store, instance: &Instance) -> anyhow::Result<()> { //! let memory = instance.exports.get_memory("memory")?; //! let memory: &Memory = instance.exports.get("some_other_memory")?; //! let add: TypedFunction<(i32, i32), i32> = instance.exports.get_typed_function(&mut store, "add")?; diff --git a/lib/api/src/sys/exports.rs b/lib/api/src/sys/exports.rs index 0a9bce991ba..11536b9945a 100644 --- a/lib/api/src/sys/exports.rs +++ b/lib/api/src/sys/exports.rs @@ -145,20 +145,20 @@ impl Exports { /// Get an export as a `TypedFunction`. pub fn get_native_function( &self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, name: &str, ) -> Result, ExportError> where Args: WasmTypeList, Rets: WasmTypeList, { - self.get_typed_function(ctx, name) + self.get_typed_function(store, name) } /// Get an export as a `TypedFunction`. pub fn get_typed_function( &self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, name: &str, ) -> Result, ExportError> where @@ -166,7 +166,7 @@ impl Exports { Rets: WasmTypeList, { self.get_function(name)? - .native(ctx) + .native(store) .map_err(|_| ExportError::IncompatibleType) } diff --git a/lib/api/src/sys/extern_ref.rs b/lib/api/src/sys/extern_ref.rs index c3ca736a710..4f0fcd9fa83 100644 --- a/lib/api/src/sys/extern_ref.rs +++ b/lib/api/src/sys/extern_ref.rs @@ -13,22 +13,22 @@ pub struct ExternRef { impl ExternRef { /// Make a new extern reference - pub fn new(ctx: &mut impl AsStoreMut, value: T) -> Self + pub fn new(store: &mut impl AsStoreMut, value: T) -> Self where T: Any + Send + Sync + 'static + Sized, { Self { - handle: StoreHandle::new(ctx.objects_mut(), VMExternObj::new(value)), + handle: StoreHandle::new(store.objects_mut(), VMExternObj::new(value)), } } /// Try to downcast to the given value. - pub fn downcast<'a, T>(&self, ctx: &'a impl AsStoreRef) -> Option<&'a T> + pub fn downcast<'a, T>(&self, store: &'a impl AsStoreRef) -> Option<&'a T> where T: Any + Send + Sync + 'static + Sized, { self.handle - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .as_ref() .downcast_ref::() } @@ -38,11 +38,11 @@ impl ExternRef { } pub(crate) unsafe fn from_vm_externref( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, vm_externref: VMExternRef, ) -> Self { Self { - handle: StoreHandle::from_internal(ctx.objects_mut().id(), vm_externref.0), + handle: StoreHandle::from_internal(store.objects_mut().id(), vm_externref.0), } } @@ -53,7 +53,7 @@ impl ExternRef { /// /// Externref and funcref values are tied to a context and can only be used /// with that context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { - self.handle.store_id() == ctx.as_store_ref().objects().id() + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { + self.handle.store_id() == store.as_store_ref().objects().id() } } diff --git a/lib/api/src/sys/externals/function.rs b/lib/api/src/sys/externals/function.rs index 7c21ee747e7..c317b9648ca 100644 --- a/lib/api/src/sys/externals/function.rs +++ b/lib/api/src/sys/externals/function.rs @@ -56,7 +56,7 @@ impl Function { /// # /// let signature = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]); /// - /// let f = Function::new(&mut store, &env, &signature, |_ctx, args| { + /// let f = Function::new(&mut store, &env, &signature, |_env, args| { /// let sum = args[0].unwrap_i32() + args[1].unwrap_i32(); /// Ok(vec![Value::I32(sum)]) /// }); @@ -71,7 +71,7 @@ impl Function { /// # /// const I32_I32_TO_I32: ([Type; 2], [Type; 1]) = ([Type::I32, Type::I32], [Type::I32]); /// - /// let f = Function::new(&mut store, &env, I32_I32_TO_I32, |_ctx, args| { + /// let f = Function::new(&mut store, &env, I32_I32_TO_I32, |_env, args| { /// let sum = args[0].unwrap_i32() + args[1].unwrap_i32(); /// Ok(vec![Value::I32(sum)]) /// }); @@ -171,7 +171,7 @@ impl Function { /// # let mut store = Store::default(); /// # let env = FunctionEnv::new(&mut store, ()); /// # - /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { + /// fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// @@ -187,7 +187,7 @@ impl Function { Args: WasmTypeList, Rets: WasmTypeList, { - // println!("new native {:p}", &new_ctx); + // println!("new native {:p}", &new_env); let host_data = Box::new(StaticFunction { raw_store: store.as_store_mut().as_raw() as *mut u8, @@ -232,7 +232,7 @@ impl Function { /// # let mut store = Store::default(); /// # let env = FunctionEnv::new(&mut store, ()); /// # - /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { + /// fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// @@ -332,7 +332,7 @@ impl Function { /// # let mut store = Store::default(); /// # let env = FunctionEnv::new(&mut store, ()); /// # - /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { + /// fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// @@ -353,7 +353,7 @@ impl Function { /// # let mut store = Store::default(); /// # let env = FunctionEnv::new(&mut store, ()); /// # - /// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { + /// fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { /// a + b /// } /// @@ -530,11 +530,11 @@ impl Function { Args: WasmTypeList, Rets: WasmTypeList, { - let vm_function = self.handle.get(store.as_store_ref().objects()); + let ty = self.ty(store); // type check { - let expected = vm_function.signature.params(); + let expected = ty.params(); let given = Args::wasm_types(); if expected != given { @@ -547,7 +547,7 @@ impl Function { } { - let expected = vm_function.signature.results(); + let expected = ty.results(); let given = Rets::wasm_types(); if expected != given { @@ -694,7 +694,7 @@ mod inner { /// /// This always returns true for primitive types that can be used with /// any context. - fn is_from_store(&self, _ctx: &impl AsStoreRef) -> bool { + fn is_from_store(&self, _store: &impl AsStoreRef) -> bool { true } } @@ -828,7 +828,7 @@ mod inner { /// Constructs `Self` based on an array of values. /// /// # Safety - unsafe fn from_array(ctx: &mut impl AsStoreMut, array: Self::Array) -> Self; + unsafe fn from_array(store: &mut impl AsStoreMut, array: Self::Array) -> Self; /// Constructs `Self` based on a slice of values. /// @@ -839,7 +839,7 @@ mod inner { /// /// # Safety unsafe fn from_slice( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, slice: &[RawValue], ) -> Result; @@ -1133,7 +1133,7 @@ mod inner { $( let $x = FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x)); )* - // println!("func wrapper2 {:p}", *env.raw_ctx); + // println!("func wrapper2 {:p}", *env.raw_env); let store_mut = StoreMut::from_raw(env.raw_store as *mut _); let f_env = FunctionEnvMut { store_mut, @@ -1280,11 +1280,11 @@ mod inner { fn test_from_array() { let mut store = Store::default(); 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)); + assert_eq!(<()>::from_array(&mut env, []), ()); + assert_eq!(::from_array(&mut env, [RawValue{i32: 1}]), (1i32)); + assert_eq!(<(i32, i64)>::from_array(&mut env, [RawValue{i32:1}, RawValue{i64:2}]), (1i32, 2i64)); assert_eq!( - <(i32, i64, f32, f64)>::from_array(&mut ctx, [ + <(i32, i64, f32, f64)>::from_array(&mut env, [ RawValue{i32:1}, RawValue{i64:2}, RawValue{f32: 3.1f32}, @@ -1298,11 +1298,11 @@ mod inner { fn test_into_array() { let mut store = Store::default(); 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}]); + assert_eq!(().into_array(&mut store), [0i128; 0]); + assert_eq!((1i32).into_array(&mut store), [1i32]); + assert_eq!((1i32, 2i64).into_array(&mut store), [RawValue{i32: 1}, RawValue{i64: 2}]); assert_eq!( - (1i32, 2i32, 3.1f32, 4.2f64).into_array(&mut ctx), + (1i32, 2i32, 3.1f32, 4.2f64).into_array(&mut store), [RawValue{i32: 1}, RawValue{i32: 2}, RawValue{ f32: 3.1f32}, RawValue{f64: 4.2f64}] ); } @@ -1318,11 +1318,11 @@ mod inner { fn test_from_c_struct() { let mut store = Store::default(); 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)); + assert_eq!(<()>::from_c_struct(&mut store, S0()), ()); + assert_eq!(::from_c_struct(&mut store, S1(1)), (1i32)); + assert_eq!(<(i32, i64)>::from_c_struct(&mut store, S2(1, 2)), (1i32, 2i64)); assert_eq!( - <(i32, i64, f32, f64)>::from_c_struct(&mut ctx, S4(1, 2, 3.1, 4.2)), + <(i32, i64, f32, f64)>::from_c_struct(&mut store, S4(1, 2, 3.1, 4.2)), (1i32, 2i64, 3.1f32, 4.2f64) ); } diff --git a/lib/api/src/sys/externals/global.rs b/lib/api/src/sys/externals/global.rs index ff35dec5506..ecc9d0eff9d 100644 --- a/lib/api/src/sys/externals/global.rs +++ b/lib/api/src/sys/externals/global.rs @@ -32,8 +32,8 @@ impl Global { /// assert_eq!(g.get(&mut store), Value::I32(1)); /// assert_eq!(g.ty(&mut store).mutability, Mutability::Const); /// ``` - pub fn new(ctx: &mut impl AsStoreMut, val: Value) -> Self { - Self::from_value(ctx, val, Mutability::Const).unwrap() + pub fn new(store: &mut impl AsStoreMut, val: Value) -> Self { + Self::from_value(store, val, Mutability::Const).unwrap() } /// Create a mutable `Global` with the initial value [`Value`]. @@ -49,17 +49,17 @@ impl Global { /// assert_eq!(g.get(&mut store), Value::I32(1)); /// assert_eq!(g.ty(&mut store).mutability, Mutability::Var); /// ``` - pub fn new_mut(ctx: &mut impl AsStoreMut, val: Value) -> Self { - Self::from_value(ctx, val, Mutability::Var).unwrap() + pub fn new_mut(store: &mut impl AsStoreMut, val: Value) -> Self { + Self::from_value(store, val, Mutability::Var).unwrap() } /// Create a `Global` with the initial value [`Value`] and the provided [`Mutability`]. fn from_value( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, val: Value, mutability: Mutability, ) -> Result { - if !val.is_from_store(ctx) { + if !val.is_from_store(store) { return Err(RuntimeError::new( "cross-`Context` values are not supported", )); @@ -69,11 +69,11 @@ impl Global { ty: val.ty(), }); unsafe { - global.vmglobal().as_mut().val = val.as_raw(ctx); + global.vmglobal().as_mut().val = val.as_raw(store); } Ok(Self { - handle: StoreHandle::new(ctx.objects_mut(), global), + handle: StoreHandle::new(store.objects_mut(), global), }) } @@ -91,8 +91,8 @@ impl Global { /// assert_eq!(c.ty(&mut store), GlobalType::new(Type::I32, Mutability::Const)); /// assert_eq!(v.ty(&mut store), GlobalType::new(Type::I64, Mutability::Var)); /// ``` - pub fn ty(&self, ctx: &impl AsStoreRef) -> GlobalType { - *self.handle.get(ctx.as_store_ref().objects()).ty() + pub fn ty(&self, store: &impl AsStoreRef) -> GlobalType { + *self.handle.get(store.as_store_ref().objects()).ty() } /// Retrieves the current value [`Value`] that the Global has. @@ -107,16 +107,16 @@ impl Global { /// /// assert_eq!(g.get(&mut store), Value::I32(1)); /// ``` - pub fn get(&self, ctx: &mut impl AsStoreMut) -> Value { + pub fn get(&self, store: &mut impl AsStoreMut) -> Value { unsafe { let raw = self .handle - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .vmglobal() .as_ref() .val; - let ty = self.handle.get(ctx.as_store_ref().objects()).ty().ty; - Value::from_raw(ctx, ty, raw) + let ty = self.handle.get(store.as_store_ref().objects()).ty().ty; + Value::from_raw(store, ty, raw) } } @@ -161,46 +161,46 @@ impl Global { /// // This results in an error: `RuntimeError`. /// g.set(&mut store, Value::I64(2)).unwrap(); /// ``` - pub fn set(&self, ctx: &mut impl AsStoreMut, val: Value) -> Result<(), RuntimeError> { - if !val.is_from_store(ctx) { + pub fn set(&self, store: &mut impl AsStoreMut, val: Value) -> Result<(), RuntimeError> { + if !val.is_from_store(store) { return Err(RuntimeError::new( "cross-`Context` values are not supported", )); } - if self.ty(ctx).mutability != Mutability::Var { + if self.ty(store).mutability != Mutability::Var { return Err(RuntimeError::new("Attempted to set an immutable global")); } - if val.ty() != self.ty(ctx).ty { + if val.ty() != self.ty(store).ty { return Err(RuntimeError::new(format!( "Attempted to operate on a global of type {expected} as a global of type {found}", - expected = self.ty(ctx).ty, + expected = self.ty(store).ty, found = val.ty(), ))); } unsafe { self.handle - .get_mut(ctx.objects_mut()) + .get_mut(store.objects_mut()) .vmglobal() .as_mut() - .val = val.as_raw(ctx); + .val = val.as_raw(store); } Ok(()) } pub(crate) fn from_vm_extern( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, internal: InternalStoreHandle, ) -> Self { Self { handle: unsafe { - StoreHandle::from_internal(ctx.as_store_ref().objects().id(), internal) + StoreHandle::from_internal(store.as_store_ref().objects().id(), internal) }, } } /// Checks whether this `Global` can be used with the given context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { - self.handle.store_id() == ctx.as_store_ref().objects().id() + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { + self.handle.store_id() == store.as_store_ref().objects().id() } pub(crate) fn to_vm_extern(&self) -> VMExtern { diff --git a/lib/api/src/sys/externals/memory.rs b/lib/api/src/sys/externals/memory.rs index ae682740452..93ae55f1769 100644 --- a/lib/api/src/sys/externals/memory.rs +++ b/lib/api/src/sys/externals/memory.rs @@ -44,14 +44,14 @@ impl Memory { /// # /// let m = Memory::new(&mut store, MemoryType::new(1, None, false)).unwrap(); /// ``` - pub fn new(ctx: &mut impl AsStoreMut, ty: MemoryType) -> Result { - let mut ctx = ctx.as_store_mut(); - let tunables = ctx.tunables(); + pub fn new(store: &mut impl AsStoreMut, ty: MemoryType) -> Result { + let mut store = store.as_store_mut(); + let tunables = store.tunables(); let style = tunables.memory_style(&ty); let memory = tunables.create_host_memory(&ty, &style)?; Ok(Self { - handle: StoreHandle::new(ctx.objects_mut(), memory), + handle: StoreHandle::new(store.objects_mut(), memory), }) } @@ -68,8 +68,8 @@ impl Memory { /// /// assert_eq!(m.ty(&mut store), mt); /// ``` - pub fn ty(&self, ctx: &impl AsStoreRef) -> MemoryType { - self.handle.get(ctx.as_store_ref().objects()).ty() + pub fn ty(&self, store: &impl AsStoreRef) -> MemoryType { + self.handle.get(store.as_store_ref().objects()).ty() } /// Returns the pointer to the raw bytes of the `Memory`. @@ -77,13 +77,13 @@ impl Memory { // This used by wasmer-emscripten and wasmer-c-api, but should be treated // as deprecated and not used in future code. #[doc(hidden)] - pub fn data_ptr(&self, ctx: &impl AsStoreRef) -> *mut u8 { - self.buffer(ctx).base + pub fn data_ptr(&self, store: &impl AsStoreRef) -> *mut u8 { + self.buffer(store).base } /// Returns the size (in bytes) of the `Memory`. - pub fn data_size(&self, ctx: &impl AsStoreRef) -> u64 { - self.buffer(ctx).len.try_into().unwrap() + pub fn data_size(&self, store: &impl AsStoreRef) -> u64 { + self.buffer(store).len.try_into().unwrap() } /// Retrieve a slice of the memory contents. @@ -94,8 +94,8 @@ impl Memory { /// modify the memory contents in any way including by calling a wasm /// function that writes to the memory or by resizing the memory. #[doc(hidden)] - pub unsafe fn data_unchecked(&self, ctx: &impl AsStoreRef) -> &[u8] { - self.data_unchecked_mut(ctx) + pub unsafe fn data_unchecked(&self, store: &impl AsStoreRef) -> &[u8] { + self.data_unchecked_mut(store) } /// Retrieve a mutable slice of the memory contents. @@ -109,8 +109,8 @@ impl Memory { /// by resizing this 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) + pub unsafe fn data_unchecked_mut(&self, store: &impl AsStoreRef) -> &mut [u8] { + slice::from_raw_parts_mut(self.buffer(store).base, self.buffer(store).len) } /// Returns the size (in [`Pages`]) of the `Memory`. @@ -125,8 +125,8 @@ impl Memory { /// /// assert_eq!(m.size(&mut store), Pages(1)); /// ``` - pub fn size(&self, ctx: &impl AsStoreRef) -> Pages { - self.handle.get(ctx.as_store_ref().objects()).size() + pub fn size(&self, store: &impl AsStoreRef) -> Pages { + self.handle.get(store.as_store_ref().objects()).size() } /// Grow memory by the specified amount of WebAssembly [`Pages`] and return @@ -163,13 +163,13 @@ impl Memory { /// ``` pub fn grow( &self, - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, delta: IntoPages, ) -> Result where IntoPages: Into, { - self.handle.get_mut(ctx.objects_mut()).grow(delta.into()) + self.handle.get_mut(store.objects_mut()).grow(delta.into()) } /// Safely reads bytes from the memory at the given offset. @@ -181,11 +181,11 @@ impl Memory { /// concurrent writes. pub fn read( &self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, offset: u64, buf: &mut [u8], ) -> Result<(), MemoryAccessError> { - self.buffer(ctx).read(offset, buf) + self.buffer(store).read(offset, buf) } /// Safely reads bytes from the memory at the given offset. @@ -200,11 +200,11 @@ impl Memory { /// concurrent writes. pub fn read_uninit<'a>( &self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, offset: u64, buf: &'a mut [MaybeUninit], ) -> Result<&'a mut [u8], MemoryAccessError> { - self.buffer(ctx).read_uninit(offset, buf) + self.buffer(store).read_uninit(offset, buf) } /// Safely writes bytes to the memory at the given offset. @@ -216,15 +216,15 @@ impl Memory { /// concurrent reads/writes. pub fn write( &self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, offset: u64, data: &[u8], ) -> Result<(), MemoryAccessError> { - self.buffer(ctx).write(offset, data) + self.buffer(store).write(offset, data) } - pub(crate) fn buffer<'a>(&'a self, ctx: &'a impl AsStoreRef) -> MemoryBuffer<'a> { - let definition = self.handle.get(ctx.as_store_ref().objects()).vmmemory(); + pub(crate) fn buffer<'a>(&'a self, store: &'a impl AsStoreRef) -> MemoryBuffer<'a> { + let definition = self.handle.get(store.as_store_ref().objects()).vmmemory(); let def = unsafe { definition.as_ref() }; MemoryBuffer { base: def.base, @@ -234,19 +234,19 @@ impl Memory { } pub(crate) fn from_vm_extern( - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, internal: InternalStoreHandle, ) -> Self { Self { handle: unsafe { - StoreHandle::from_internal(ctx.as_store_ref().objects().id(), internal) + StoreHandle::from_internal(store.as_store_ref().objects().id(), internal) }, } } /// Checks whether this `Memory` can be used with the given context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { - self.handle.store_id() == ctx.as_store_ref().objects().id() + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { + self.handle.store_id() == store.as_store_ref().objects().id() } pub(crate) fn to_vm_extern(&self) -> VMExtern { diff --git a/lib/api/src/sys/externals/mod.rs b/lib/api/src/sys/externals/mod.rs index da4fff433b7..cd84be24192 100644 --- a/lib/api/src/sys/externals/mod.rs +++ b/lib/api/src/sys/externals/mod.rs @@ -34,32 +34,32 @@ pub enum Extern { impl Extern { /// Return the underlying type of the inner `Extern`. - pub fn ty(&self, ctx: &impl AsStoreRef) -> ExternType { + pub fn ty(&self, store: &impl AsStoreRef) -> ExternType { match self { - Self::Function(ft) => ExternType::Function(ft.ty(ctx)), - Self::Memory(ft) => ExternType::Memory(ft.ty(ctx)), - Self::Table(tt) => ExternType::Table(tt.ty(ctx)), - Self::Global(gt) => ExternType::Global(gt.ty(ctx)), + Self::Function(ft) => ExternType::Function(ft.ty(store)), + Self::Memory(ft) => ExternType::Memory(ft.ty(store)), + Self::Table(tt) => ExternType::Table(tt.ty(store)), + Self::Global(gt) => ExternType::Global(gt.ty(store)), } } /// Create an `Extern` from an `wasmer_engine::Export`. - pub fn from_vm_extern(ctx: &mut impl AsStoreMut, vm_extern: VMExtern) -> Self { + pub fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExtern) -> Self { match vm_extern { - VMExtern::Function(f) => Self::Function(Function::from_vm_extern(ctx, f)), - VMExtern::Memory(m) => Self::Memory(Memory::from_vm_extern(ctx, m)), - VMExtern::Global(g) => Self::Global(Global::from_vm_extern(ctx, g)), - VMExtern::Table(t) => Self::Table(Table::from_vm_extern(ctx, t)), + VMExtern::Function(f) => Self::Function(Function::from_vm_extern(store, f)), + VMExtern::Memory(m) => Self::Memory(Memory::from_vm_extern(store, m)), + VMExtern::Global(g) => Self::Global(Global::from_vm_extern(store, g)), + VMExtern::Table(t) => Self::Table(Table::from_vm_extern(store, t)), } } /// Checks whether this `Extern` can be used with the given context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { match self { - Self::Function(f) => f.is_from_store(ctx), - Self::Global(g) => g.is_from_store(ctx), - Self::Memory(m) => m.is_from_store(ctx), - Self::Table(t) => t.is_from_store(ctx), + Self::Function(f) => f.is_from_store(store), + Self::Global(g) => g.is_from_store(store), + Self::Memory(m) => m.is_from_store(store), + Self::Table(t) => t.is_from_store(store), } } diff --git a/lib/api/src/sys/externals/table.rs b/lib/api/src/sys/externals/table.rs index 7f6a24cb36d..d0e12ecce76 100644 --- a/lib/api/src/sys/externals/table.rs +++ b/lib/api/src/sys/externals/table.rs @@ -29,10 +29,10 @@ fn set_table_item( } fn value_to_table_element( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, val: Value, ) -> Result { - if !val.is_from_store(ctx) { + if !val.is_from_store(store) { return Err(RuntimeError::new("cannot pass Value across contexts")); } Ok(match val { @@ -40,19 +40,19 @@ fn value_to_table_element( wasmer_vm::TableElement::ExternRef(extern_ref.map(|e| e.vm_externref())) } Value::FuncRef(func_ref) => { - wasmer_vm::TableElement::FuncRef(func_ref.map(|f| f.vm_funcref(ctx))) + wasmer_vm::TableElement::FuncRef(func_ref.map(|f| f.vm_funcref(store))) } _ => return Err(RuntimeError::new("val is not reference")), }) } -fn value_from_table_element(ctx: &mut impl AsStoreMut, item: wasmer_vm::TableElement) -> Value { +fn value_from_table_element(store: &mut impl AsStoreMut, item: wasmer_vm::TableElement) -> Value { match item { wasmer_vm::TableElement::FuncRef(funcref) => { - Value::FuncRef(funcref.map(|f| unsafe { Function::from_vm_funcref(ctx, f) })) + Value::FuncRef(funcref.map(|f| unsafe { Function::from_vm_funcref(store, f) })) } wasmer_vm::TableElement::ExternRef(extern_ref) => { - Value::ExternRef(extern_ref.map(|e| unsafe { ExternRef::from_vm_externref(ctx, e) })) + Value::ExternRef(extern_ref.map(|e| unsafe { ExternRef::from_vm_externref(store, e) })) } } } @@ -65,13 +65,13 @@ impl Table { /// This function will construct the `Table` using the store /// [`BaseTunables`][crate::sys::BaseTunables]. pub fn new( - mut ctx: &mut impl AsStoreMut, + mut store: &mut impl AsStoreMut, ty: TableType, init: Value, ) -> Result { - let item = value_to_table_element(&mut ctx, init)?; - let mut ctx = ctx.as_store_mut(); - let tunables = ctx.tunables(); + let item = value_to_table_element(&mut store, init)?; + let mut store = store.as_store_mut(); + let tunables = store.tunables(); let style = tunables.table_style(&ty); let mut table = tunables .create_host_table(&ty, &style) @@ -83,35 +83,35 @@ impl Table { } Ok(Self { - handle: StoreHandle::new(ctx.objects_mut(), table), + handle: StoreHandle::new(store.objects_mut(), table), }) } /// Returns the [`TableType`] of the `Table`. - pub fn ty(&self, ctx: &impl AsStoreRef) -> TableType { - *self.handle.get(ctx.as_store_ref().objects()).ty() + pub fn ty(&self, store: &impl AsStoreRef) -> TableType { + *self.handle.get(store.as_store_ref().objects()).ty() } /// Retrieves an element of the table at the provided `index`. - pub fn get(&self, ctx: &mut impl AsStoreMut, index: u32) -> Option { - let item = self.handle.get(ctx.as_store_ref().objects()).get(index)?; - Some(value_from_table_element(ctx, item)) + pub fn get(&self, store: &mut impl AsStoreMut, index: u32) -> Option { + let item = self.handle.get(store.as_store_ref().objects()).get(index)?; + Some(value_from_table_element(store, item)) } /// Sets an element `val` in the Table at the provided `index`. pub fn set( &self, - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, index: u32, val: Value, ) -> Result<(), RuntimeError> { - let item = value_to_table_element(ctx, val)?; - set_table_item(self.handle.get_mut(ctx.objects_mut()), index, item) + let item = value_to_table_element(store, val)?; + set_table_item(self.handle.get_mut(store.objects_mut()), index, item) } /// Retrieves the size of the `Table` (in elements) - pub fn size(&self, ctx: &impl AsStoreRef) -> u32 { - self.handle.get(ctx.as_store_ref().objects()).size() + pub fn size(&self, store: &impl AsStoreRef) -> u32 { + self.handle.get(store.as_store_ref().objects()).size() } /// Grows the size of the `Table` by `delta`, initializating @@ -125,13 +125,13 @@ impl Table { /// Returns an error if the `delta` is out of bounds for the table. pub fn grow( &self, - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, delta: u32, init: Value, ) -> Result { - let item = value_to_table_element(ctx, init)?; + let item = value_to_table_element(store, init)?; self.handle - .get_mut(ctx.objects_mut()) + .get_mut(store.objects_mut()) .grow(delta, item) .ok_or_else(|| RuntimeError::new(format!("failed to grow table by `{}`", delta))) } @@ -144,7 +144,7 @@ impl Table { /// Returns an error if the range is out of bounds of either the source or /// destination tables. pub fn copy( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, dst_table: &Self, dst_index: u32, src_table: &Self, @@ -156,12 +156,12 @@ impl Table { "cross-`Context` table copies are not supported", )); } - let ctx = ctx; + let store = store; if dst_table.handle.internal_handle() == src_table.handle.internal_handle() { - let table = dst_table.handle.get_mut(ctx.objects_mut()); + let table = dst_table.handle.get_mut(store.objects_mut()); table.copy_within(dst_index, src_index, len) } else { - let (src_table, dst_table) = ctx.objects_mut().get_2_mut( + let (src_table, dst_table) = store.objects_mut().get_2_mut( src_table.handle.internal_handle(), dst_table.handle.internal_handle(), ); @@ -172,19 +172,19 @@ impl Table { } pub(crate) fn from_vm_extern( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, internal: InternalStoreHandle, ) -> Self { Self { handle: unsafe { - StoreHandle::from_internal(ctx.as_store_ref().objects().id(), internal) + StoreHandle::from_internal(store.as_store_ref().objects().id(), internal) }, } } /// Checks whether this `Table` can be used with the given context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { - self.handle.store_id() == ctx.as_store_ref().objects().id() + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { + self.handle.store_id() == store.as_store_ref().objects().id() } pub(crate) fn to_vm_extern(&self) -> VMExtern { diff --git a/lib/api/src/sys/imports.rs b/lib/api/src/sys/imports.rs index a93171a3115..98fdc848c5b 100644 --- a/lib/api/src/sys/imports.rs +++ b/lib/api/src/sys/imports.rs @@ -28,7 +28,7 @@ use wasmer_types::ImportError; /// /// let instance = Instance::new(&mut store, &module, &import_object).expect("Could not instantiate module."); /// -/// fn foo(_ctx: FunctionEnvMut<()>, n: i32) -> i32 { +/// fn foo(_env: FunctionEnvMut<()>, n: i32) -> i32 { /// n /// } /// @@ -101,7 +101,7 @@ impl Imports { /// # let mut store: Store = Default::default(); /// # let env = FunctionEnv::new(&mut store, ()); /// use wasmer::{StoreMut, Imports, Function, FunctionEnvMut}; - /// fn foo(_ctx: FunctionEnvMut<()>, n: i32) -> i32 { + /// fn foo(_env: FunctionEnvMut<()>, n: i32) -> i32 { /// n /// } /// let mut import_object = Imports::new(); @@ -305,7 +305,7 @@ mod test { let mut store: Store = Default::default(); let env = FunctionEnv::new(&mut store, ()); - fn func(_ctx: FunctionEnvMut<()>, arg: i32) -> i32 { + fn func(_env: FunctionEnvMut<()>, arg: i32) -> i32 { arg + 1 } diff --git a/lib/api/src/sys/instance.rs b/lib/api/src/sys/instance.rs index 93bd15a9112..ae1b1118f0f 100644 --- a/lib/api/src/sys/instance.rs +++ b/lib/api/src/sys/instance.rs @@ -110,26 +110,26 @@ impl Instance { /// * Link errors that happen when plugging the imports into the instance /// * Runtime errors that happen when running the module `start` function. pub fn new( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, module: &Module, imports: &Imports, ) -> Result { let imports = imports .imports_for_module(module) .map_err(InstantiationError::Link)?; - let mut handle = module.instantiate(ctx, &imports)?; + let mut handle = module.instantiate(store, &imports)?; let exports = module .exports() .map(|export| { let name = export.name().to_string(); let export = handle.lookup(&name).expect("export"); - let extern_ = Extern::from_vm_extern(ctx, export); + let extern_ = Extern::from_vm_extern(store, export); (name, extern_) }) .collect::(); let instance = Self { - _handle: StoreHandle::new(ctx.objects_mut(), handle), + _handle: StoreHandle::new(store.objects_mut(), handle), module: module.clone(), exports, }; @@ -148,24 +148,24 @@ impl Instance { /// * Link errors that happen when plugging the imports into the instance /// * Runtime errors that happen when running the module `start` function. pub fn new_by_index( - ctx: &mut impl AsStoreMut, + store: &mut impl AsStoreMut, module: &Module, externs: &[Extern], ) -> Result { let imports = externs.to_vec(); - let mut handle = module.instantiate(ctx, &imports)?; + let mut handle = module.instantiate(store, &imports)?; let exports = module .exports() .map(|export| { let name = export.name().to_string(); let export = handle.lookup(&name).expect("export"); - let extern_ = Extern::from_vm_extern(ctx, export); + let extern_ = Extern::from_vm_extern(store, export); (name, extern_) }) .collect::(); let instance = Self { - _handle: StoreHandle::new(ctx.objects_mut(), handle), + _handle: StoreHandle::new(store.objects_mut(), handle), module: module.clone(), exports, }; diff --git a/lib/api/src/sys/mem_access.rs b/lib/api/src/sys/mem_access.rs index debb46c0c68..270e1728c9a 100644 --- a/lib/api/src/sys/mem_access.rs +++ b/lib/api/src/sys/mem_access.rs @@ -62,9 +62,9 @@ pub struct WasmRef<'a, T: ValueType> { impl<'a, T: ValueType> WasmRef<'a, T> { /// Creates a new `WasmRef` at the given offset in a memory. #[inline] - pub fn new(ctx: &'a impl AsStoreRef, memory: &'a Memory, offset: u64) -> Self { + pub fn new(store: &'a impl AsStoreRef, memory: &'a Memory, offset: u64) -> Self { Self { - buffer: memory.buffer(ctx), + buffer: memory.buffer(store), offset, marker: PhantomData, } @@ -161,7 +161,7 @@ impl<'a, T: ValueType> WasmSlice<'a, T> { /// Returns a `MemoryAccessError` if the slice length overflows. #[inline] pub fn new( - ctx: &'a impl AsStoreRef, + store: &'a impl AsStoreRef, memory: &'a Memory, offset: u64, len: u64, @@ -173,7 +173,7 @@ impl<'a, T: ValueType> WasmSlice<'a, T> { .checked_add(total_len) .ok_or(MemoryAccessError::Overflow)?; Ok(Self { - buffer: memory.buffer(ctx), + buffer: memory.buffer(store), offset, len, marker: PhantomData, diff --git a/lib/api/src/sys/native.rs b/lib/api/src/sys/native.rs index b9aab2647db..827931aefd1 100644 --- a/lib/api/src/sys/native.rs +++ b/lib/api/src/sys/native.rs @@ -66,24 +66,24 @@ macro_rules! impl_native_traits { /// Call the typed func and return results. #[allow(unused_mut)] #[allow(clippy::too_many_arguments)] - pub fn call(&self, ctx: &mut impl AsStoreMut, $( $x: $x, )* ) -> Result { + pub fn call(&self, store: &mut impl AsStoreMut, $( $x: $x, )* ) -> Result { let anyfunc = unsafe { *self.func .handle - .get(ctx.as_store_ref().objects()) + .get(store.as_store_ref().objects()) .anyfunc .as_ptr() .as_ref() }; // Ensure all parameters come from the same context. - if $(!FromToNativeWasmType::is_from_store(&$x, ctx) ||)* false { + if $(!FromToNativeWasmType::is_from_store(&$x, store) ||)* false { return Err(RuntimeError::new( "cross-`Context` values are not supported", )); } // TODO: when `const fn` related features mature more, we can declare a single array // of the correct size here. - let mut params_list = [ $( $x.to_native().into_raw(ctx) ),* ]; + let mut params_list = [ $( $x.to_native().into_raw(store) ),* ]; let mut rets_list_array = Rets::empty_array(); let rets_list: &mut [RawValue] = rets_list_array.as_mut(); let using_rets_array; @@ -99,7 +99,7 @@ macro_rules! impl_native_traits { }; unsafe { wasmer_vm::wasmer_call_trampoline( - ctx.as_store_ref().signal_handler(), + store.as_store_ref().signal_handler(), anyfunc.vmctx, anyfunc.call_trampoline, anyfunc.func_ptr, @@ -118,7 +118,7 @@ macro_rules! impl_native_traits { num_rets); } } - Ok(unsafe { Rets::from_array(ctx, rets_list_array) }) + Ok(unsafe { Rets::from_array(store, rets_list_array) }) // TODO: When the Host ABI and Wasm ABI are the same, we could do this instead: // but we can't currently detect whether that's safe. // diff --git a/lib/api/src/sys/native_type.rs b/lib/api/src/sys/native_type.rs index 2b9b54df19e..f8555e6f392 100644 --- a/lib/api/src/sys/native_type.rs +++ b/lib/api/src/sys/native_type.rs @@ -12,127 +12,127 @@ use super::store::AsStoreMut; /// types with a context. pub trait NativeWasmTypeInto: NativeWasmType + Sized { #[doc(hidden)] - fn into_abi(self, ctx: &mut impl AsStoreMut) -> Self::Abi; + fn into_abi(self, store: &mut impl AsStoreMut) -> Self::Abi; #[doc(hidden)] - unsafe fn from_abi(ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self; + unsafe fn from_abi(store: &mut impl AsStoreMut, abi: Self::Abi) -> Self; /// Convert self to raw value representation. - fn into_raw(self, ctx: &mut impl AsStoreMut) -> RawValue; + fn into_raw(self, store: &mut impl AsStoreMut) -> RawValue; /// Convert to self from raw value representation. /// /// # Safety /// - unsafe fn from_raw(ctx: &mut impl AsStoreMut, raw: RawValue) -> Self; + unsafe fn from_raw(store: &mut impl AsStoreMut, raw: RawValue) -> Self; } impl NativeWasmTypeInto for i32 { #[inline] - unsafe fn from_abi(_ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + unsafe fn from_abi(_store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { abi } #[inline] - fn into_abi(self, _ctx: &mut impl AsStoreMut) -> Self::Abi { + fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi { self } #[inline] - fn into_raw(self, _ctx: &mut impl AsStoreMut) -> RawValue { + fn into_raw(self, _store: &mut impl AsStoreMut) -> RawValue { RawValue { i32: self } } #[inline] - unsafe fn from_raw(_ctx: &mut impl AsStoreMut, raw: RawValue) -> Self { + unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: RawValue) -> Self { raw.i32 } } impl NativeWasmTypeInto for i64 { #[inline] - unsafe fn from_abi(_ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + unsafe fn from_abi(_store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { abi } #[inline] - fn into_abi(self, _ctx: &mut impl AsStoreMut) -> Self::Abi { + fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi { self } #[inline] - fn into_raw(self, _ctx: &mut impl AsStoreMut) -> RawValue { + fn into_raw(self, _store: &mut impl AsStoreMut) -> RawValue { RawValue { i64: self } } #[inline] - unsafe fn from_raw(_ctx: &mut impl AsStoreMut, raw: RawValue) -> Self { + unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: RawValue) -> Self { raw.i64 } } impl NativeWasmTypeInto for f32 { #[inline] - unsafe fn from_abi(_ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + unsafe fn from_abi(_store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { abi } #[inline] - fn into_abi(self, _ctx: &mut impl AsStoreMut) -> Self::Abi { + fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi { self } #[inline] - fn into_raw(self, _ctx: &mut impl AsStoreMut) -> RawValue { + fn into_raw(self, _store: &mut impl AsStoreMut) -> RawValue { RawValue { f32: self } } #[inline] - unsafe fn from_raw(_ctx: &mut impl AsStoreMut, raw: RawValue) -> Self { + unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: RawValue) -> Self { raw.f32 } } impl NativeWasmTypeInto for f64 { #[inline] - unsafe fn from_abi(_ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + unsafe fn from_abi(_store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { abi } #[inline] - fn into_abi(self, _ctx: &mut impl AsStoreMut) -> Self::Abi { + fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi { self } #[inline] - fn into_raw(self, _ctx: &mut impl AsStoreMut) -> RawValue { + fn into_raw(self, _store: &mut impl AsStoreMut) -> RawValue { RawValue { f64: self } } #[inline] - unsafe fn from_raw(_ctx: &mut impl AsStoreMut, raw: RawValue) -> Self { + unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: RawValue) -> Self { raw.f64 } } impl NativeWasmTypeInto for u128 { #[inline] - unsafe fn from_abi(_ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + unsafe fn from_abi(_store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { abi } #[inline] - fn into_abi(self, _ctx: &mut impl AsStoreMut) -> Self::Abi { + fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi { self } #[inline] - fn into_raw(self, _ctx: &mut impl AsStoreMut) -> RawValue { + fn into_raw(self, _store: &mut impl AsStoreMut) -> RawValue { RawValue { u128: self } } #[inline] - unsafe fn from_raw(_ctx: &mut impl AsStoreMut, raw: RawValue) -> Self { + unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: RawValue) -> Self { raw.u128 } } @@ -144,24 +144,24 @@ impl NativeWasmType for ExternRef { impl NativeWasmTypeInto for Option { #[inline] - unsafe fn from_abi(ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + unsafe fn from_abi(store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { VMExternRef::from_raw(RawValue { externref: abi }) - .map(|e| ExternRef::from_vm_externref(ctx, e)) + .map(|e| ExternRef::from_vm_externref(store, e)) } #[inline] - fn into_abi(self, _ctx: &mut impl AsStoreMut) -> Self::Abi { + fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi { self.map_or(0, |e| unsafe { e.vm_externref().into_raw().externref }) } #[inline] - fn into_raw(self, _ctx: &mut impl AsStoreMut) -> RawValue { + fn into_raw(self, _store: &mut impl AsStoreMut) -> RawValue { self.map_or(RawValue { externref: 0 }, |e| e.vm_externref().into_raw()) } #[inline] - unsafe fn from_raw(ctx: &mut impl AsStoreMut, raw: RawValue) -> Self { - VMExternRef::from_raw(raw).map(|e| ExternRef::from_vm_externref(ctx, e)) + unsafe fn from_raw(store: &mut impl AsStoreMut, raw: RawValue) -> Self { + VMExternRef::from_raw(raw).map(|e| ExternRef::from_vm_externref(store, e)) } } @@ -172,23 +172,25 @@ impl NativeWasmType for Function { impl NativeWasmTypeInto for Option { #[inline] - unsafe fn from_abi(ctx: &mut impl AsStoreMut, abi: Self::Abi) -> Self { - VMFuncRef::from_raw(RawValue { funcref: abi }).map(|f| Function::from_vm_funcref(ctx, f)) + unsafe fn from_abi(store: &mut impl AsStoreMut, abi: Self::Abi) -> Self { + VMFuncRef::from_raw(RawValue { funcref: abi }).map(|f| Function::from_vm_funcref(store, f)) } #[inline] - fn into_abi(self, ctx: &mut impl AsStoreMut) -> Self::Abi { - self.map_or(0, |f| unsafe { f.vm_funcref(ctx).into_raw().externref }) + fn into_abi(self, store: &mut impl AsStoreMut) -> Self::Abi { + self.map_or(0, |f| unsafe { f.vm_funcref(store).into_raw().externref }) } #[inline] - fn into_raw(self, ctx: &mut impl AsStoreMut) -> RawValue { - self.map_or(RawValue { externref: 0 }, |e| e.vm_funcref(ctx).into_raw()) + fn into_raw(self, store: &mut impl AsStoreMut) -> RawValue { + self.map_or(RawValue { externref: 0 }, |e| { + e.vm_funcref(store).into_raw() + }) } #[inline] - unsafe fn from_raw(ctx: &mut impl AsStoreMut, raw: RawValue) -> Self { - VMFuncRef::from_raw(raw).map(|f| Function::from_vm_funcref(ctx, f)) + unsafe fn from_raw(store: &mut impl AsStoreMut, raw: RawValue) -> Self { + VMFuncRef::from_raw(raw).map(|f| Function::from_vm_funcref(store, f)) } } diff --git a/lib/api/src/sys/ptr.rs b/lib/api/src/sys/ptr.rs index 8a430e6e4dd..d0ebdade26a 100644 --- a/lib/api/src/sys/ptr.rs +++ b/lib/api/src/sys/ptr.rs @@ -22,8 +22,8 @@ pub type WasmPtr64 = WasmPtr; /// # use wasmer::Memory; /// # use wasmer::WasmPtr; /// # use wasmer::FunctionEnvMut; -/// pub fn host_import(mut ctx: FunctionEnvMut<()>, memory: Memory, ptr: WasmPtr) { -/// let derefed_ptr = ptr.deref(&mut ctx, &memory); +/// pub fn host_import(mut env: FunctionEnvMut<()>, memory: Memory, ptr: WasmPtr) { +/// let derefed_ptr = ptr.deref(&mut env, &memory); /// let inner_val: u32 = derefed_ptr.read().expect("pointer in bounds"); /// println!("Got {} from Wasm memory address 0x{:X}", inner_val, ptr.offset()); /// // update the value being pointed to @@ -49,8 +49,8 @@ pub type WasmPtr64 = WasmPtr; /// z: f32 /// } /// -/// fn update_vector_3(mut ctx: FunctionEnvMut<()>, memory: Memory, ptr: WasmPtr) { -/// let derefed_ptr = ptr.deref(&mut ctx, &memory); +/// fn update_vector_3(mut env: FunctionEnvMut<()>, memory: Memory, ptr: WasmPtr) { +/// let derefed_ptr = ptr.deref(&mut env, &memory); /// let mut inner_val: V3 = derefed_ptr.read().expect("pointer in bounds"); /// println!("Got {:?} from Wasm memory address 0x{:X}", inner_val, ptr.offset()); /// // update the value being pointed to @@ -142,25 +142,25 @@ impl WasmPtr { /// Creates a `WasmRef` from this `WasmPtr` which allows reading and /// mutating of the value being pointed to. #[inline] - pub fn deref<'a>(self, ctx: &'a impl AsStoreRef, memory: &'a Memory) -> WasmRef<'a, T> { - WasmRef::new(ctx, memory, self.offset.into()) + pub fn deref<'a>(self, store: &'a impl AsStoreRef, memory: &'a Memory) -> WasmRef<'a, T> { + WasmRef::new(store, memory, self.offset.into()) } /// Reads the address pointed to by this `WasmPtr` in a memory. #[inline] - pub fn read(self, ctx: &impl AsStoreRef, memory: &Memory) -> Result { - self.deref(&ctx, memory).read() + pub fn read(self, store: &impl AsStoreRef, memory: &Memory) -> Result { + self.deref(&store, memory).read() } /// Writes to the address pointed to by this `WasmPtr` in a memory. #[inline] pub fn write( self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, memory: &Memory, val: T, ) -> Result<(), MemoryAccessError> { - self.deref(&ctx, memory).write(val) + self.deref(&store, memory).write(val) } /// Creates a `WasmSlice` starting at this `WasmPtr` which allows reading @@ -171,11 +171,11 @@ impl WasmPtr { #[inline] pub fn slice<'a>( self, - ctx: &'a impl AsStoreRef, + store: &'a impl AsStoreRef, memory: &'a Memory, len: M::Offset, ) -> Result, MemoryAccessError> { - WasmSlice::new(ctx, memory, self.offset.into(), len.into()) + WasmSlice::new(store, memory, self.offset.into(), len.into()) } /// Reads a sequence of values from this `WasmPtr` until a value that @@ -185,14 +185,14 @@ impl WasmPtr { #[inline] pub fn read_until( self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, memory: &Memory, mut end: impl FnMut(&T) -> bool, ) -> Result, MemoryAccessError> { let mut vec = Vec::new(); for i in 0u64.. { let i = M::Offset::try_from(i).map_err(|_| MemoryAccessError::Overflow)?; - let val = self.add_offset(i)?.deref(&ctx, memory).read()?; + let val = self.add_offset(i)?.deref(&store, memory).read()?; if end(&val) { break; } @@ -210,11 +210,11 @@ impl WasmPtr { #[inline] pub fn read_utf8_string( self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, memory: &Memory, len: M::Offset, ) -> Result { - let vec = self.slice(&ctx, memory, len)?.read_to_vec()?; + let vec = self.slice(&store, memory, len)?.read_to_vec()?; Ok(String::from_utf8(vec)?) } @@ -225,10 +225,10 @@ impl WasmPtr { #[inline] pub fn read_utf8_string_with_nul( self, - ctx: &impl AsStoreRef, + store: &impl AsStoreRef, memory: &Memory, ) -> Result { - let vec = self.read_until(ctx, memory, |&byte| byte == 0)?; + let vec = self.read_until(store, memory, |&byte| byte == 0)?; Ok(String::from_utf8(vec)?) } } diff --git a/lib/api/src/sys/value.rs b/lib/api/src/sys/value.rs index d681692fcb3..63521b7b302 100644 --- a/lib/api/src/sys/value.rs +++ b/lib/api/src/sys/value.rs @@ -91,14 +91,14 @@ impl Value { } /// Converts the `Value` into a `RawValue`. - pub fn as_raw(&self, ctx: &impl AsStoreRef) -> RawValue { + pub fn as_raw(&self, store: &impl AsStoreRef) -> RawValue { match *self { Self::I32(i32) => RawValue { i32 }, Self::I64(i64) => RawValue { i64 }, Self::F32(f32) => RawValue { f32 }, Self::F64(f64) => RawValue { f64 }, Self::V128(u128) => RawValue { u128 }, - Self::FuncRef(Some(ref f)) => f.vm_funcref(ctx).into_raw(), + Self::FuncRef(Some(ref f)) => f.vm_funcref(store).into_raw(), Self::FuncRef(None) => RawValue { funcref: 0 }, Self::ExternRef(Some(ref e)) => e.vm_externref().into_raw(), @@ -110,7 +110,7 @@ impl Value { /// /// # Safety /// - pub unsafe fn from_raw(ctx: &mut impl AsStoreMut, ty: Type, raw: RawValue) -> Self { + pub unsafe fn from_raw(store: &mut impl AsStoreMut, ty: Type, raw: RawValue) -> Self { match ty { Type::I32 => Self::I32(raw.i32), Type::I64 => Self::I64(raw.i64), @@ -118,10 +118,10 @@ impl Value { Type::F64 => Self::F64(raw.f64), Type::V128 => Self::V128(raw.u128), Type::FuncRef => { - Self::FuncRef(VMFuncRef::from_raw(raw).map(|f| Function::from_vm_funcref(ctx, f))) + Self::FuncRef(VMFuncRef::from_raw(raw).map(|f| Function::from_vm_funcref(store, f))) } Type::ExternRef => Self::ExternRef( - VMExternRef::from_raw(raw).map(|e| ExternRef::from_vm_externref(ctx, e)), + VMExternRef::from_raw(raw).map(|e| ExternRef::from_vm_externref(store, e)), ), } } @@ -133,7 +133,7 @@ impl Value { /// /// Externref and funcref values are tied to a context and can only be used /// with that context. - pub fn is_from_store(&self, ctx: &impl AsStoreRef) -> bool { + pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { match self { Self::I32(_) | Self::I64(_) @@ -142,8 +142,8 @@ impl Value { | Self::V128(_) | Self::ExternRef(None) | Self::FuncRef(None) => true, - Self::ExternRef(Some(e)) => e.is_from_store(ctx), - Self::FuncRef(Some(f)) => f.is_from_store(ctx), + Self::ExternRef(Some(e)) => e.is_from_store(store), + Self::FuncRef(Some(f)) => f.is_from_store(store), } } diff --git a/lib/api/tests/js_externals.rs b/lib/api/tests/js_externals.rs index a17aae4b76d..c6a35db6372 100644 --- a/lib/api/tests/js_externals.rs +++ b/lib/api/tests/js_externals.rs @@ -183,13 +183,13 @@ mod js { fn function_new() { let mut store = Store::default(); let mut env = FunctionEnv::new(&mut store, ()); - let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<'_, ()>| {}); + let function = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<'_, ()>| {}); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![], vec![]) ); let function = - Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<'_, ()>, _a: i32| {}); + Function::new_native(&mut store, &env, |_env: FunctionEnvMut<'_, ()>, _a: i32| {}); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![Type::I32], vec![]) @@ -197,14 +197,14 @@ mod js { let function = Function::new_native( &mut store, &env, - |_ctx: FunctionEnvMut<'_, ()>, _a: i32, _b: i64, _c: f32, _d: f64| {}, + |_env: FunctionEnvMut<'_, ()>, _a: i32, _b: i64, _c: f32, _d: f64| {}, ); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) ); let function = - Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<'_, ()>| -> i32 { + Function::new_native(&mut store, &env, |_env: FunctionEnvMut<'_, ()>| -> i32 { 1 }); assert_eq!( @@ -214,7 +214,7 @@ mod js { let function = Function::new_native( &mut store, &env, - |_ctx: FunctionEnvMut<'_, ()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, + |_env: FunctionEnvMut<'_, ()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, ); assert_eq!( function.ty(&store).clone(), @@ -281,7 +281,7 @@ mod js { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32], vec![]); @@ -289,7 +289,7 @@ mod js { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = @@ -298,7 +298,7 @@ mod js { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32]); @@ -306,7 +306,7 @@ mod js { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = @@ -315,7 +315,7 @@ mod js { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); @@ -325,7 +325,7 @@ mod js { &mut store, &env, function_type, - |_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).params(), [Type::V128]); assert_eq!( @@ -349,7 +349,7 @@ mod js { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32], vec![]); @@ -357,7 +357,7 @@ mod js { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = @@ -366,7 +366,7 @@ mod js { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32]); @@ -374,7 +374,7 @@ mod js { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = @@ -383,7 +383,7 @@ mod js { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); @@ -393,7 +393,7 @@ mod js { &mut store, &env, function_type, - |_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).params(), [Type::V128]); assert_eq!( diff --git a/lib/api/tests/js_instance.rs b/lib/api/tests/js_instance.rs index c1d6102abe6..286e880e91a 100644 --- a/lib/api/tests/js_instance.rs +++ b/lib/api/tests/js_instance.rs @@ -241,9 +241,9 @@ mod js { 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, &env, &imported_signature, |ctx, args| { + let imported = Function::new(&mut store, &env, &imported_signature, |env, args| { log!("Calling `imported`..."); - let result = args[0].unwrap_i32() * ctx.data().multiplier; + let result = args[0].unwrap_i32() * env.data().multiplier; log!("Result of `imported`: {:?}", result); Ok(vec![Value::I32(result)]) }); @@ -342,10 +342,10 @@ mod js { multiplier: u32, } - fn imported_fn(ctx: FunctionEnvMut<'_, Env>, arg: u32) -> u32 { - log!("inside imported_fn: ctx.data is {:?}", ctx.data()); - // log!("inside call id is {:?}", ctx.as_store_ref().objects().id); - return ctx.data().multiplier * arg; + fn imported_fn(env: FunctionEnvMut<'_, Env>, arg: u32) -> u32 { + log!("inside imported_fn: env.data is {:?}", env.data()); + // log!("inside call id is {:?}", env.as_store_ref().objects().id); + return env.data().multiplier * arg; } let mut env = FunctionEnv::new(&mut store, Env { multiplier: 3 }); @@ -401,10 +401,10 @@ mod js { memory: Option, } - 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); - return (memory_val as u32) * ctx.data().multiplier * arg; + fn imported_fn(env: FunctionEnvMut<'_, Env>, arg: u32) -> u32 { + let memory: &Memory = env.data().memory.as_ref().unwrap(); + let memory_val = memory.uint8view(&env).get_index(0); + return (memory_val as u32) * env.data().multiplier * arg; } let mut env = FunctionEnv::new( @@ -457,10 +457,10 @@ mod js { let mut env = FunctionEnv::new(&mut store, Env { multiplier: 3 }); fn imported_fn( - ctx: FunctionEnvMut<'_, Env>, + env: FunctionEnvMut<'_, Env>, args: &[Val], ) -> Result, RuntimeError> { - let value = ctx.data().multiplier * args[0].unwrap_i32() as u32; + let value = env.data().multiplier * args[0].unwrap_i32() as u32; return Ok(vec![Val::I32(value as _)]); } @@ -507,12 +507,12 @@ mod js { } fn imported_fn( - ctx: FunctionEnvMut<'_, Env>, + env: FunctionEnvMut<'_, Env>, args: &[Val], ) -> Result, RuntimeError> { - let memory: &Memory = ctx.data().memory.as_ref().unwrap(); - let memory_val = memory.uint8view(&ctx).get_index(0); - let value = (memory_val as u32) * ctx.data().multiplier * args[0].unwrap_i32() as u32; + let memory: &Memory = env.data().memory.as_ref().unwrap(); + let memory_val = memory.uint8view(&env).get_index(0); + let value = (memory_val as u32) * env.data().multiplier * args[0].unwrap_i32() as u32; return Ok(vec![Val::I32(value as _)]); } diff --git a/lib/api/tests/sys_externals.rs b/lib/api/tests/sys_externals.rs index 26be6969193..4fd1f3328b0 100644 --- a/lib/api/tests/sys_externals.rs +++ b/lib/api/tests/sys_externals.rs @@ -70,7 +70,7 @@ mod sys { maximum: None, }; let env = FunctionEnv::new(&mut store, ()); - let f = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>| {}); + let f = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>| {}); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f)))?; assert_eq!(table.ty(&mut store), table_type); @@ -96,7 +96,7 @@ mod sys { minimum: 0, maximum: Some(1), }; - let f = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, num: i32| { + let f = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>, num: i32| { num + 1 }); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f)))?; @@ -122,7 +122,7 @@ mod sys { minimum: 0, maximum: Some(10), }; - let f = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, num: i32| { + let f = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>, num: i32| { num + 1 }); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f.clone())))?; @@ -190,13 +190,13 @@ mod sys { fn function_new() -> Result<()> { let mut store = Store::default(); let env = FunctionEnv::new(&mut store, ()); - let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>| {}); + let function = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![]) ); let function = - Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, _a: i32| {}); + Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>, _a: i32| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![Type::I32], vec![]) @@ -204,14 +204,14 @@ mod sys { let function = Function::new_native( &mut store, &env, - |_ctx: FunctionEnvMut<()>, _a: i32, _b: i64, _c: f32, _d: f64| {}, + |_env: FunctionEnvMut<()>, _a: i32, _b: i64, _c: f32, _d: f64| {}, ); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) ); let function = - Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>| -> i32 { 1 }); + Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>| -> i32 { 1 }); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![Type::I32]) @@ -219,7 +219,7 @@ mod sys { let function = Function::new_native( &mut store, &env, - |_ctx: FunctionEnvMut<()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, + |_env: FunctionEnvMut<()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, ); assert_eq!( function.ty(&mut store).clone(), @@ -236,13 +236,13 @@ mod sys { let my_env = MyEnv {}; let env = FunctionEnv::new(&mut store, my_env); - let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut| {}); + let function = Function::new_native(&mut store, &env, |_env: FunctionEnvMut| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![]) ); let function = - Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut, _a: i32| {}); + Function::new_native(&mut store, &env, |_env: FunctionEnvMut, _a: i32| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![Type::I32], vec![]) @@ -250,14 +250,14 @@ mod sys { let function = Function::new_native( &mut store, &env, - |_ctx: FunctionEnvMut, _a: i32, _b: i64, _c: f32, _d: f64| {}, + |_env: FunctionEnvMut, _a: i32, _b: i64, _c: f32, _d: f64| {}, ); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) ); let function = - Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut| -> i32 { 1 }); + Function::new_native(&mut store, &env, |_env: FunctionEnvMut| -> i32 { 1 }); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![Type::I32]) @@ -265,7 +265,7 @@ mod sys { let function = Function::new_native( &mut store, &env, - |_ctx: FunctionEnvMut| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, + |_env: FunctionEnvMut| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, ); assert_eq!( function.ty(&mut store).clone(), @@ -285,7 +285,7 @@ mod sys { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32], vec![]); @@ -293,7 +293,7 @@ mod sys { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = @@ -302,7 +302,7 @@ mod sys { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32]); @@ -310,7 +310,7 @@ mod sys { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = @@ -319,7 +319,7 @@ mod sys { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); @@ -329,7 +329,7 @@ mod sys { &mut store, &env, function_type, - |_ctx: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).params(), [Type::V128]); assert_eq!( @@ -354,7 +354,7 @@ mod sys { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32], vec![]); @@ -362,7 +362,7 @@ mod sys { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = @@ -371,7 +371,7 @@ mod sys { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32]); @@ -379,7 +379,7 @@ mod sys { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = @@ -388,7 +388,7 @@ mod sys { &mut store, &env, &function_type, - |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); @@ -398,7 +398,7 @@ mod sys { &mut store, &env, function_type, - |_ctx: FunctionEnvMut, _values: &[Value]| unimplemented!(), + |_env: FunctionEnvMut, _values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).params(), [Type::V128]); assert_eq!( @@ -413,17 +413,17 @@ mod sys { // fn native_function_works() -> Result<()> { // let mut store = Store::default(); // let env = FunctionEnv::new(&mut store, ()); - // let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>| {}); + // let function = Function::new_native(&mut store, &env, |_env: 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, &env, |_ctx: FunctionEnvMut<()>, a: i32| -> i32 { a + 1 }); + // Function::new_native(&mut store, &env, |_env: 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 { + // fn rust_abi(_env: 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, &env, rust_abi); @@ -431,16 +431,16 @@ mod sys { // 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, &env, |_ctx: FunctionEnvMut<()>| -> i32 { 1 }); + // let function = Function::new_native(&mut store, &env, |_env: 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, &env, |_ctx: FunctionEnvMut<()>, _a: i32| {}); + // let function = Function::new_native(&mut store, &env, |_env: 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, &env, |_ctx: FunctionEnvMut<()>| -> (i32, i64, f32, f64) { + // Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>| -> (i32, i64, f32, f64) { // (1, 2, 3.0, 4.0) // }); // let native_function: TypedFunction<(), (i32, i64, f32, f64)> = @@ -516,8 +516,8 @@ mod sys { // memory: Option, // } - // fn host_function(ctx: FunctionEnvMut, arg1: u32, arg2: u32) -> u32 { - // ctx.data().val + arg1 + arg2 + // fn host_function(env: FunctionEnvMut, arg1: u32, arg2: u32) -> u32 { + // env.data().val + arg1 + arg2 // } // let mut env = MyEnv { diff --git a/lib/api/tests/sys_instance.rs b/lib/api/tests/sys_instance.rs index 234bffe93b4..d5b94297246 100644 --- a/lib/api/tests/sys_instance.rs +++ b/lib/api/tests/sys_instance.rs @@ -52,10 +52,10 @@ mod sys { } fn imported_fn( - ctx: FunctionEnvMut, + env: FunctionEnvMut, args: &[Value], ) -> Result, RuntimeError> { - let value = ctx.data().multiplier * args[0].unwrap_i32() as u32; + let value = env.data().multiplier * args[0].unwrap_i32() as u32; Ok(vec![Value::I32(value as _)]) } diff --git a/lib/api/tests/sys_module.rs b/lib/api/tests/sys_module.rs index 551e8aaae70..3d1834639b9 100644 --- a/lib/api/tests/sys_module.rs +++ b/lib/api/tests/sys_module.rs @@ -194,35 +194,35 @@ mod sys { let env = FunctionEnv::new(&mut store, ()); let imports = imports! { "host" => { - "host_func1" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: u64| { + "host_func1" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: u64| { println!("host_func1: Found number {}", p); assert_eq!(p, u64::max_value()); }), - "host_func2" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: u32| { + "host_func2" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: u32| { println!("host_func2: Found number {}", p); assert_eq!(p, u32::max_value()); }), - "host_func3" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: i64| { + "host_func3" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: i64| { println!("host_func3: Found number {}", p); assert_eq!(p, -1); }), - "host_func4" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: i32| { + "host_func4" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: i32| { println!("host_func4: Found number {}", p); assert_eq!(p, -1); }), - "host_func5" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: i16| { + "host_func5" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: i16| { println!("host_func5: Found number {}", p); assert_eq!(p, -1); }), - "host_func6" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: u16| { + "host_func6" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: u16| { println!("host_func6: Found number {}", p); assert_eq!(p, u16::max_value()); }), - "host_func7" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: i8| { + "host_func7" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: i8| { println!("host_func7: Found number {}", p); assert_eq!(p, -1); }), - "host_func8" => Function::new_native(&mut store, &env,|_ctx: FunctionEnvMut<()>, p: u8| { + "host_func8" => Function::new_native(&mut store, &env,|_env: 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 b31b32b4b78..24eb03f1137 100644 --- a/lib/api/tests/sys_reference_types.rs +++ b/lib/api/tests/sys_reference_types.rs @@ -27,7 +27,7 @@ mod sys { let env = FunctionEnv::new(&mut store, env); let imports = imports! { "env" => { - "func_ref_identity" => Function::new(&mut store, &env, 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]), |_env: FunctionEnvMut, values: &[Value]| -> Result, _> { Ok(vec![values[0].clone()]) }) }, @@ -44,8 +44,8 @@ mod sys { } let func_to_call = - Function::new_native(&mut store, &env, |mut ctx: FunctionEnvMut| -> i32 { - ctx.data_mut().0.store(true, Ordering::SeqCst); + Function::new_native(&mut store, &env, |mut env: FunctionEnvMut| -> i32 { + env.data_mut().0.store(true, Ordering::SeqCst); 343 }); let call_set_value: &Function = instance.exports.get_function("call_set_value")?; @@ -83,13 +83,13 @@ mod sys { let module = Module::new(&store, wat)?; let env = FunctionEnv::new(&mut store, ()); fn func_ref_call( - mut ctx: FunctionEnvMut<()>, + mut env: FunctionEnvMut<()>, values: &[Value], ) -> Result, RuntimeError> { // TODO: look into `Box<[Value]>` being returned breakage let f = values[0].unwrap_funcref().as_ref().unwrap(); - let f: TypedFunction<(i32, i32), i32> = f.native(&mut ctx)?; - Ok(vec![Value::I32(f.call(&mut ctx, 7, 9)?)]) + let f: TypedFunction<(i32, i32), i32> = f.native(&mut env)?; + Ok(vec![Value::I32(f.call(&mut env, 7, 9)?)]) } let imports = imports! { @@ -100,7 +100,7 @@ mod sys { FunctionType::new([Type::FuncRef], [Type::I32]), func_ref_call ), - // "func_ref_call_native" => Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, f: Function| -> Result { + // "func_ref_call_native" => Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>, f: Function| -> Result { // let f: TypedFunction::<(i32, i32), i32> = f.native(&mut store)?; // f.call(&mut store, 7, 9) // }) @@ -109,7 +109,7 @@ mod sys { let instance = Instance::new(&mut store, &module, &imports)?; { - fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { + fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { a + b } let sum_func = Function::new_native(&mut store, &env, sum); @@ -154,23 +154,23 @@ mod sys { let env = FunctionEnv::new(&mut store, ()); let imports = imports! { "env" => { - "extern_ref_identity" => Function::new(&mut store, &env, FunctionType::new([Type::ExternRef], [Type::ExternRef]), |_ctx, values| -> Result, _> { + "extern_ref_identity" => Function::new(&mut store, &env, FunctionType::new([Type::ExternRef], [Type::ExternRef]), |_env, values| -> Result, _> { Ok(vec![values[0].clone()]) }), - "extern_ref_identity_native" => Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<()>, er: ExternRef| -> ExternRef { + "extern_ref_identity_native" => Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>, er: ExternRef| -> ExternRef { er }), - "get_new_extern_ref" => Function::new(&mut store, &env, FunctionType::new([], [Type::ExternRef]), |_ctx, _| -> Result, _> { + "get_new_extern_ref" => Function::new(&mut store, &env, FunctionType::new([], [Type::ExternRef]), |_env, _| -> Result, _> { let inner = [("hello".to_string(), "world".to_string()), ("color".to_string(), "orange".to_string())] .iter() .cloned() .collect::>(); - let new_extern_ref = ExternRef::new(&mut ctx, inner); + let new_extern_ref = ExternRef::new(&mut env, inner); Ok(vec![Value::ExternRef(new_extern_ref)]) }), - "get_new_extern_ref_native" => Function::new_native(&mut store, &env,|_ctx| -> ExternRef { + "get_new_extern_ref_native" => Function::new_native(&mut store, &env,|_env| -> ExternRef { let inner = [("hello".to_string(), "world".to_string()), ("color".to_string(), "orange".to_string())]