From 7df3e3ca23e95d171331cb2fb1dfb9b21ff4d565 Mon Sep 17 00:00:00 2001 From: Wolfgang Silbermayr Date: Wed, 3 Aug 2022 11:45:31 +0200 Subject: [PATCH 1/9] Rename {native,typed} functions, reintroduce *_with_env variants --- examples/early_exit.rs | 8 +- examples/engine_headless.rs | 2 +- examples/errors.rs | 4 +- examples/exports_function.rs | 8 +- examples/exports_global.rs | 8 +- examples/exports_memory.rs | 2 +- examples/features.rs | 4 +- examples/hello_world.rs | 11 +- examples/imports_exports.rs | 7 +- examples/imports_function.rs | 34 +- examples/imports_function_env.rs | 6 +- examples/imports_global.rs | 10 +- examples/instance.rs | 4 +- examples/memory.rs | 4 +- examples/metering.rs | 6 +- examples/table.rs | 11 +- examples/tunables_limit_memory.rs | 4 +- lib/api/src/js/externals/function.rs | 87 +- lib/api/src/js/imports.rs | 26 +- lib/api/src/lib.rs | 4 +- lib/api/src/sys/exports.rs | 2 +- lib/api/src/sys/externals/function.rs | 116 ++- lib/api/src/sys/imports.rs | 36 +- lib/api/tests/js_externals.rs | 67 +- lib/api/tests/js_instance.rs | 18 +- lib/api/tests/js_module.rs | 16 +- lib/api/tests/sys_externals.rs | 110 +- lib/api/tests/sys_instance.rs | 2 +- lib/api/tests/sys_module.rs | 18 +- lib/api/tests/sys_reference_types.rs | 20 +- .../src/wasm_c_api/externals/function.rs | 6 +- lib/emscripten/src/lib.rs | 960 +++++++++--------- lib/middlewares/src/metering.rs | 4 +- lib/wasi/src/lib.rs | 640 ++++++------ tests/compilers/imports.rs | 122 +-- tests/compilers/issues.rs | 21 +- tests/compilers/main.rs | 2 +- tests/compilers/serialize.rs | 4 +- tests/compilers/traps.rs | 28 +- ...native_functions.rs => typed_functions.rs} | 150 ++- tests/lib/wast/src/spectest.rs | 30 +- tests/lib/wast/src/wast.rs | 10 +- 42 files changed, 1327 insertions(+), 1305 deletions(-) rename tests/compilers/{native_functions.rs => typed_functions.rs} (72%) diff --git a/examples/early_exit.rs b/examples/early_exit.rs index f582447896f..1a0ec8e37b5 100644 --- a/examples/early_exit.rs +++ b/examples/early_exit.rs @@ -16,10 +16,7 @@ use anyhow::bail; use std::fmt; -use wasmer::{ - imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store, - TypedFunction, -}; +use wasmer::{imports, wat2wasm, Function, FunctionEnvMut, Instance, Module, Store, TypedFunction}; use wasmer_compiler_cranelift::Cranelift; // First we need to create an error type that we'll use to signal the end of execution. @@ -58,7 +55,6 @@ fn main() -> anyhow::Result<()> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new(Cranelift::default()); - let env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. @@ -73,7 +69,7 @@ fn main() -> anyhow::Result<()> { // Create an import object. let import_object = imports! { "env" => { - "early_exit" => Function::new_native(&mut store, &env, early_exit), + "early_exit" => Function::new_typed(&mut store, early_exit), } }; diff --git a/examples/engine_headless.rs b/examples/engine_headless.rs index 6ff9c95d2a0..c0cbf2b334f 100644 --- a/examples/engine_headless.rs +++ b/examples/engine_headless.rs @@ -45,7 +45,7 @@ //! Ready? use tempfile::NamedTempFile; -use wasmer::{imports, wat2wasm, EngineBuilder, FunctionEnv, Instance, Module, Store, Value}; +use wasmer::{imports, wat2wasm, EngineBuilder, Instance, Module, Store, Value}; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { diff --git a/examples/errors.rs b/examples/errors.rs index d2bedfbf69a..c2035cbcd64 100644 --- a/examples/errors.rs +++ b/examples/errors.rs @@ -13,7 +13,7 @@ //! //! Ready? -use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction}; +use wasmer::{imports, wat2wasm, Instance, Module, Store, TypedFunction}; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -61,7 +61,7 @@ fn main() -> Result<(), Box> { let div_by_zero: TypedFunction<(), i32> = instance .exports .get_function("div_by_zero")? - .native(&mut store)?; + .typed(&mut store)?; println!("Calling `div_by_zero` function..."); // Let's call the `div_by_zero` exported function. diff --git a/examples/exports_function.rs b/examples/exports_function.rs index 958a86d094f..4eb9cf5f6c0 100644 --- a/examples/exports_function.rs +++ b/examples/exports_function.rs @@ -17,7 +17,7 @@ //! //! Ready? -use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction, Value}; +use wasmer::{imports, wat2wasm, Instance, Module, Store, TypedFunction, Value}; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -81,17 +81,17 @@ fn main() -> Result<(), Box> { // that's possible with the `TypedFunction` API. The function // will use native Rust values. // - // Note that `native` takes 2 generic parameters: `Args` and + // Note that `typed` takes 2 generic parameters: `Args` and // `Rets`, respectively for the parameters and the results. If // those values don't match the exported function signature, an // error will be raised. - let sum_native: TypedFunction<(i32, i32), i32> = sum.native(&mut store)?; + let sum_typed: TypedFunction<(i32, i32), i32> = sum.typed(&mut store)?; println!("Calling `sum` function (natively)..."); // Let's call the `sum` exported function. The parameters are // statically typed Rust values of type `i32` and `i32`. The // result, in this case particular case, in a unit of type `i32`. - let result = sum_native.call(&mut store, 3, 4)?; + let result = sum_typed.call(&mut store, 3, 4)?; println!("Results: {:?}", result); assert_eq!(result, 7); diff --git a/examples/exports_global.rs b/examples/exports_global.rs index 06921b0d846..86787d878c5 100644 --- a/examples/exports_global.rs +++ b/examples/exports_global.rs @@ -15,9 +15,7 @@ //! //! Ready? -use wasmer::{ - imports, wat2wasm, FunctionEnv, Instance, Module, Mutability, Store, Type, TypedFunction, Value, -}; +use wasmer::{imports, wat2wasm, Instance, Module, Mutability, Store, Type, TypedFunction, Value}; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -92,7 +90,7 @@ fn main() -> Result<(), Box> { let get_one: TypedFunction<(), f32> = instance .exports .get_function("get_one")? - .native(&mut store)?; + .typed(&mut store)?; let one_value = get_one.call(&mut store)?; let some_value = some.get(&mut store); @@ -124,7 +122,7 @@ fn main() -> Result<(), Box> { let set_some: TypedFunction = instance .exports .get_function("set_some")? - .native(&mut store)?; + .typed(&mut store)?; set_some.call(&mut store, 21.0)?; let some_result = some.get(&mut store); println!("`some` value after `set_some`: {:?}", some_result); diff --git a/examples/exports_memory.rs b/examples/exports_memory.rs index c6c3d058ea0..f3b4a7d4b67 100644 --- a/examples/exports_memory.rs +++ b/examples/exports_memory.rs @@ -11,7 +11,7 @@ //! //! Ready? -use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction, WasmPtr}; +use wasmer::{imports, wat2wasm, Instance, Module, Store, TypedFunction, WasmPtr}; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { diff --git a/examples/features.rs b/examples/features.rs index 4f8a800386b..28ea86e7e33 100644 --- a/examples/features.rs +++ b/examples/features.rs @@ -10,9 +10,7 @@ //! //! Ready? -use wasmer::{ - imports, wat2wasm, EngineBuilder, Features, FunctionEnv, Instance, Module, Store, Value, -}; +use wasmer::{imports, wat2wasm, EngineBuilder, Features, Instance, Module, Store, Value}; use wasmer_compiler_cranelift::Cranelift; fn main() -> anyhow::Result<()> { diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 73ad8620a26..0b5221f4733 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -6,10 +6,7 @@ //! cargo run --example hello-world --release --features "cranelift" //! ``` -use wasmer::{ - imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store, - TypedFunction, -}; +use wasmer::{imports, wat2wasm, Function, FunctionEnvMut, Instance, Module, Store, TypedFunction}; use wasmer_compiler_cranelift::Cranelift; fn main() -> anyhow::Result<()> { @@ -52,10 +49,6 @@ fn main() -> anyhow::Result<()> { // A `Module` is a compiled WebAssembly module that isn't ready to execute yet. let module = Module::new(&store, wasm_bytes)?; - // Next we'll set up our `Module` so that we can execute it. First, create - // a `FunctionEnv` in which to instantiate our `Module`. - let context = FunctionEnv::new(&mut store, ()); - // We define a function to act as our "env" "say_hello" function imported in the // Wasm program above. fn say_hello_world(_env: FunctionEnvMut<'_, ()>) { @@ -67,7 +60,7 @@ fn main() -> anyhow::Result<()> { // We use the default namespace "env". "env" => { // And call our function "say_hello". - "say_hello" => Function::new_native(&mut store, &context, say_hello_world), + "say_hello" => Function::new_typed(&mut store, say_hello_world), } }; diff --git a/examples/imports_exports.rs b/examples/imports_exports.rs index c8d18704194..93ac580da19 100644 --- a/examples/imports_exports.rs +++ b/examples/imports_exports.rs @@ -16,8 +16,8 @@ //! Ready? use wasmer::{ - imports, wat2wasm, Function, FunctionEnv, FunctionType, Global, Instance, Memory, Module, - Store, Table, Type, Value, + imports, wat2wasm, Function, FunctionType, Global, Instance, Memory, Module, Store, Table, + Type, Value, }; use wasmer_compiler_cranelift::Cranelift; @@ -44,7 +44,6 @@ fn main() -> Result<(), Box> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new(Cranelift::default()); - let env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); // Let's compile the Wasm module. @@ -59,7 +58,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, |_env, _args| { + let host_function = Function::new(&mut store, &host_function_signature, |_env, _args| { Ok(vec![Value::I32(42)]) }); diff --git a/examples/imports_function.rs b/examples/imports_function.rs index d454cc8d057..a93d936af88 100644 --- a/examples/imports_function.rs +++ b/examples/imports_function.rs @@ -29,12 +29,12 @@ fn main() -> Result<(), Box> { br#" (module (func $multiply_dynamic (import "env" "multiply_dynamic") (param i32) (result i32)) - (func $multiply_native (import "env" "multiply_native") (param i32) (result i32)) + (func $multiply_typed (import "env" "multiply_typed") (param i32) (result i32)) (type $sum_t (func (param i32) (param i32) (result i32))) (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32) (call $multiply_dynamic (local.get $x)) - (call $multiply_native (local.get $y)) + (call $multiply_typed (local.get $y)) i32.add) (export "sum" (func $sum_f))) "#, @@ -45,9 +45,8 @@ fn main() -> Result<(), Box> { // the default provided by Wasmer. // You can use `Store::default()` for that. let mut store = Store::new(Cranelift::default()); - let env1 = FunctionEnv::new(&mut store, ()); struct MyEnv; - let env2 = FunctionEnv::new(&mut store, MyEnv {}); + let env = FunctionEnv::new(&mut store, MyEnv {}); println!("Compiling module..."); // Let's compile the Wasm module. @@ -55,36 +54,31 @@ fn main() -> Result<(), Box> { // Create the functions let multiply_dynamic_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let multiply_dynamic = Function::new( - &mut store, - &env1, - &multiply_dynamic_signature, - |_env, args| { - println!("Calling `multiply_dynamic`..."); + let multiply_dynamic = Function::new(&mut store, &multiply_dynamic_signature, |_env, args| { + println!("Calling `multiply_dynamic`..."); - let result = args[0].unwrap_i32() * 2; + let result = args[0].unwrap_i32() * 2; - println!("Result of `multiply_dynamic`: {:?}", result); + println!("Result of `multiply_dynamic`: {:?}", result); - Ok(vec![Value::I32(result)]) - }, - ); + Ok(vec![Value::I32(result)]) + }); fn multiply(_env: FunctionEnvMut, a: i32) -> i32 { - println!("Calling `multiply_native`..."); + println!("Calling `multiply_typed`..."); let result = a * 3; - println!("Result of `multiply_native`: {:?}", result); + println!("Result of `multiply_typed`: {:?}", result); result } - let multiply_native = Function::new_native(&mut store, &env2, multiply); + let multiply_typed = Function::new_typed_with_env(&mut store, &env, multiply); // Create an import object. let import_object = imports! { "env" => { "multiply_dynamic" => multiply_dynamic, - "multiply_native" => multiply_native, + "multiply_typed" => multiply_typed, } }; @@ -96,7 +90,7 @@ fn main() -> Result<(), Box> { // // The Wasm module exports a function called `sum`. Let's get it. let sum: TypedFunction<(i32, i32), i32> = - instance.exports.get_function("sum")?.native(&mut store)?; + instance.exports.get_function("sum")?.typed(&mut store)?; println!("Calling `sum` function..."); // Let's call the `sum` exported function. It will call each diff --git a/examples/imports_function_env.rs b/examples/imports_function_env.rs index e56e45bbbf6..dc870625e69 100644 --- a/examples/imports_function_env.rs +++ b/examples/imports_function_env.rs @@ -97,8 +97,8 @@ fn main() -> Result<(), Box> { // Create an import object. let import_object = imports! { "env" => { - "get_counter" => Function::new_native(&mut store, &env, get_counter), - "add_to_counter" => Function::new_native(&mut store, &env, add_to_counter), + "get_counter" => Function::new_typed_with_env(&mut store, &env, get_counter), + "add_to_counter" => Function::new_typed_with_env(&mut store, &env, add_to_counter), } }; @@ -112,7 +112,7 @@ fn main() -> Result<(), Box> { let increment_counter_loop: TypedFunction = instance .exports .get_function("increment_counter_loop")? - .native(&mut store)?; + .typed(&mut store)?; let counter_value: i32 = *shared_counter.lock().unwrap(); println!("Initial ounter value: {:?}", counter_value); diff --git a/examples/imports_global.rs b/examples/imports_global.rs index a00c9756431..44a2946c988 100644 --- a/examples/imports_global.rs +++ b/examples/imports_global.rs @@ -15,9 +15,7 @@ //! //! Ready? -use wasmer::{ - imports, wat2wasm, FunctionEnv, Global, Instance, Module, Store, TypedFunction, Value, -}; +use wasmer::{imports, wat2wasm, Global, Instance, Module, Store, TypedFunction, Value}; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -69,11 +67,11 @@ fn main() -> Result<(), Box> { let get_some: TypedFunction<(), f32> = instance .exports .get_function("get_some")? - .native(&mut store)?; + .typed(&mut store)?; let get_other: TypedFunction<(), f32> = instance .exports .get_function("get_other")? - .native(&mut store)?; + .typed(&mut store)?; let some_result = get_some.call(&mut store)?; let other_result = get_other.call(&mut store)?; @@ -106,7 +104,7 @@ fn main() -> Result<(), Box> { let set_other: TypedFunction = instance .exports .get_function("set_other")? - .native(&mut store)?; + .typed(&mut store)?; set_other.call(&mut store, 42.0)?; println!("other value (via Global API): {:?}", other.get(&mut store)); diff --git a/examples/instance.rs b/examples/instance.rs index e563af07e73..fa57d2dc682 100644 --- a/examples/instance.rs +++ b/examples/instance.rs @@ -14,7 +14,7 @@ //! //! Ready? -use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction}; +use wasmer::{imports, wat2wasm, Instance, Module, Store, TypedFunction}; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -62,7 +62,7 @@ fn main() -> Result<(), Box> { let add_one: TypedFunction = instance .exports .get_function("add_one")? - .native(&mut store)?; + .typed(&mut store)?; println!("Calling `add_one` function..."); let result = add_one.call(&mut store, 1)?; diff --git a/examples/memory.rs b/examples/memory.rs index 3cd1815a181..030d014fc74 100644 --- a/examples/memory.rs +++ b/examples/memory.rs @@ -15,9 +15,7 @@ //! Ready? use std::mem; -use wasmer::{ - imports, wat2wasm, Bytes, FunctionEnv, Instance, Module, Pages, Store, TypedFunction, -}; +use wasmer::{imports, wat2wasm, Bytes, Instance, Module, Pages, Store, TypedFunction}; use wasmer_compiler_cranelift::Cranelift; // this example is a work in progress: diff --git a/examples/metering.rs b/examples/metering.rs index 0d37491e0a9..e5d8c70d742 100644 --- a/examples/metering.rs +++ b/examples/metering.rs @@ -18,9 +18,7 @@ use anyhow::bail; use std::sync::Arc; use wasmer::wasmparser::Operator; use wasmer::CompilerConfig; -use wasmer::{ - imports, wat2wasm, EngineBuilder, FunctionEnv, Instance, Module, Store, TypedFunction, -}; +use wasmer::{imports, wat2wasm, EngineBuilder, Instance, Module, Store, TypedFunction}; use wasmer_compiler_cranelift::Cranelift; use wasmer_middlewares::{ metering::{get_remaining_points, set_remaining_points, MeteringPoints}, @@ -91,7 +89,7 @@ fn main() -> anyhow::Result<()> { let add_one: TypedFunction = instance .exports .get_function("add_one")? - .native(&mut store)?; + .typed(&mut store)?; println!("Calling `add_one` function once..."); add_one.call(&mut store, 1)?; diff --git a/examples/table.rs b/examples/table.rs index 17a5b50e666..c3b9b35b7d6 100644 --- a/examples/table.rs +++ b/examples/table.rs @@ -1,6 +1,6 @@ use wasmer::{ - imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store, TableType, - Type, TypedFunction, Value, + imports, wat2wasm, Function, FunctionEnvMut, Instance, Module, Store, TableType, Type, + TypedFunction, Value, }; use wasmer_compiler_cranelift::Cranelift; @@ -52,7 +52,6 @@ fn main() -> anyhow::Result<()> { // We set up our store with an engine and a compiler. let mut store = Store::new(Cranelift::default()); - let env = FunctionEnv::new(&mut store, ()); // Then compile our Wasm. let module = Module::new(&store, wasm_bytes)?; let import_object = imports! {}; @@ -88,7 +87,7 @@ fn main() -> anyhow::Result<()> { // == Setting elements in a table == // We first construct a `Function` over our host_callback. - let func = Function::new_native(&mut store, &env, host_callback); + let func = Function::new_typed(&mut store, host_callback); // And set table index 1 of that table to the host_callback `Function`. guest_table.set(&mut store, 1, func.into())?; @@ -102,7 +101,7 @@ fn main() -> anyhow::Result<()> { // == Growing a table == // We again construct a `Function` over our host_callback. - let func = Function::new_native(&mut store, &env, host_callback); + let func = Function::new_typed(&mut store, host_callback); // And grow the table by 3 elements, filling in our host_callback in all the // new elements of the table. @@ -133,7 +132,7 @@ fn main() -> anyhow::Result<()> { assert_eq!(result, 18); // Now overwrite index 0 with our host_callback. - let func = Function::new_native(&mut store, &env, host_callback); + let func = Function::new_typed(&mut store, host_callback); guest_table.set(&mut store, 0, func.into())?; // And verify that it does what we expect. let result = call_via_table.call(&mut store, 0, 2, 7)?; diff --git a/examples/tunables_limit_memory.rs b/examples/tunables_limit_memory.rs index 2b2f36900ac..104f1acc681 100644 --- a/examples/tunables_limit_memory.rs +++ b/examples/tunables_limit_memory.rs @@ -3,8 +3,8 @@ use std::ptr::NonNull; use wasmer::{ imports, vm::{self, MemoryError, MemoryStyle, TableStyle, VMMemoryDefinition, VMTableDefinition}, - wat2wasm, BaseTunables, FunctionEnv, Instance, Memory, MemoryType, Module, Pages, Store, - TableType, Target, Tunables, + wat2wasm, BaseTunables, Instance, Memory, MemoryType, Module, Pages, Store, TableType, Target, + Tunables, }; use wasmer_compiler_cranelift::Cranelift; diff --git a/lib/api/src/js/externals/function.rs b/lib/api/src/js/externals/function.rs index e9477b39cbe..0b0938df75a 100644 --- a/lib/api/src/js/externals/function.rs +++ b/lib/api/src/js/externals/function.rs @@ -64,7 +64,24 @@ impl Function { /// Creates a new host `Function` (dynamic) with the provided signature. /// /// If you know the signature of the host function at compile time, - /// consider using [`Function::new_native`] for less runtime overhead. + /// consider using [`Function::new_typed`] for less runtime overhead. + pub fn new(store: &mut impl AsStoreMut, ty: FT, func: F) -> Self + where + FT: Into, + F: Fn(&[Value]) -> Result, RuntimeError> + 'static + Send + Sync, + { + let env = FunctionEnv::new(&mut store.as_store_mut(), ()); + let wrapped_func = move |_env: FunctionEnvMut<()>, + args: &[Value]| + -> Result, RuntimeError> { func(args) }; + Self::new_with_env(store, &env, ty, wrapped_func) + } + + /// Creates a new host `Function` (dynamic) with the provided signature. + /// + /// If you know the signature of the host function at compile time, + /// consider using [`Function::new_typed`] or [`Function::new_typed_with_env`] + /// for less runtime overhead. /// /// # Examples /// @@ -74,7 +91,7 @@ impl Function { /// # /// let signature = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]); /// - /// let f = Function::new(&store, &signature, |args| { + /// let f = Function::new_with_env(&store, &signature, |args| { /// let sum = args[0].unwrap_i32() + args[1].unwrap_i32(); /// Ok(vec![Value::I32(sum)]) /// }); @@ -88,13 +105,13 @@ impl Function { /// # /// const I32_I32_TO_I32: ([Type; 2], [Type; 1]) = ([Type::I32, Type::I32], [Type::I32]); /// - /// let f = Function::new(&store, I32_I32_TO_I32, |args| { + /// let f = Function::new_with_env(&store, I32_I32_TO_I32, |args| { /// let sum = args[0].unwrap_i32() + args[1].unwrap_i32(); /// Ok(vec![Value::I32(sum)]) /// }); /// ``` #[allow(clippy::cast_ptr_alignment)] - pub fn new( + pub fn new_with_env( store: &mut impl AsStoreMut, env: &FunctionEnv, ty: FT, @@ -164,7 +181,25 @@ impl Function { Self::from_vm_export(&mut store, vm_function) } - /// Creates a new host `Function` from a native function. + #[deprecated( + since = "3.0.0", + note = "new_native_with_env() has been renamed to new_typed_with_env()." + )] + /// Creates a new host `Function` with an environment from a typed function. + pub fn new_native_with_env( + store: &mut impl AsStoreMut, + env: &FunctionEnv, + func: F, + ) -> Self + where + F: HostFunction, + Args: WasmTypeList, + Rets: WasmTypeList, + { + Self::new_typed_with_env(store, env, func) + } + + /// Creates a new host `Function` from a typed function. /// /// The function signature is automatically retrieved using the /// Rust typing system. @@ -179,9 +214,9 @@ impl Function { /// a + b /// } /// - /// let f = Function::new_native(&store, sum); + /// let f = Function::new_typed_with_env(&store, sum); /// ``` - pub fn new_native( + pub fn new_typed_with_env( store: &mut impl AsStoreMut, env: &FunctionEnv, func: F, @@ -226,7 +261,7 @@ impl Function { /// a + b /// } /// - /// let f = Function::new_native(&store, sum); + /// let f = Function::new_typed(&store, sum); /// /// assert_eq!(f.ty().params(), vec![Type::I32, Type::I32]); /// assert_eq!(f.ty().results(), vec![Type::I32]); @@ -242,13 +277,12 @@ impl Function { /// ``` /// # use wasmer::{Function, FunctionEnv, FunctionEnvMut, Store, Type}; /// # let mut store = Store::default(); - /// # let env = FunctionEnv::new(&mut store, ()); /// # - /// fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { + /// fn sum(a: i32, b: i32) -> i32 { /// a + b /// } /// - /// let f = Function::new_native(&store, &env, sum); + /// let f = Function::new_typed(&store, sum); /// /// assert_eq!(f.param_arity(&store), 2); /// ``` @@ -263,13 +297,12 @@ impl Function { /// ``` /// # use wasmer::{Function, FunctionEnv, FunctionEnvMut, Store, Type}; /// # let mut store = Store::default(); - /// # let env = FunctionEnv::new(&mut store, ()); /// # - /// fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { + /// fn sum(a: i32, b: i32) -> i32 { /// a + b /// } /// - /// let f = Function::new_native(&store, &env, sum); + /// let f = Function::new_typed(&store, sum); /// /// assert_eq!(f.result_arity(&store), 1); /// ``` @@ -362,8 +395,20 @@ impl Function { } } - /// Transform this WebAssembly function into a function with the - /// native ABI. See [`TypedFunction`] to learn more. + #[deprecated(since = "3.0.0", note = "native() has been renamed to typed().")] + pub fn native( + &self, + store: &impl AsStoreRef, + ) -> Result, RuntimeError> + where + Args: WasmTypeList, + Rets: WasmTypeList, + { + self.typed(store) + } + + /// Transform this WebAssembly function into a typed function. + /// See [`TypedFunction`] to learn more. /// /// # Examples /// @@ -383,9 +428,9 @@ impl Function { /// # let instance = Instance::new(&module, &import_object).unwrap(); /// # /// let sum = instance.exports.get_function("sum").unwrap(); - /// let sum_native = sum.native::<(i32, i32), i32>().unwrap(); + /// let sum_typed = sum.typed::<(i32, i32), i32>().unwrap(); /// - /// assert_eq!(sum_native.call(&mut store, 1, 2).unwrap(), 3); + /// assert_eq!(sum_typed.call(&mut store, 1, 2).unwrap(), 3); /// ``` /// /// # Errors @@ -411,7 +456,7 @@ impl Function { /// let sum = instance.exports.get_function("sum").unwrap(); /// /// // This results in an error: `RuntimeError` - /// let sum_native = sum.native::<(i64, i64), i32>(&mut store).unwrap(); + /// let sum_typed = sum.typed::<(i64, i64), i32>(&mut store).unwrap(); /// ``` /// /// If the `Rets` generic parameter does not match the exported function @@ -435,9 +480,9 @@ impl Function { /// let sum = instance.exports.get_function("sum").unwrap(); /// /// // This results in an error: `RuntimeError` - /// let sum_native = sum.native::<(i32, i32), i64>(&mut store).unwrap(); + /// let sum_typed = sum.typed::<(i32, i32), i64>(&mut store).unwrap(); /// ``` - pub fn native( + pub fn typed( &self, store: &impl AsStoreRef, ) -> Result, RuntimeError> diff --git a/lib/api/src/js/imports.rs b/lib/api/src/js/imports.rs index 94866e66842..95a9930995c 100644 --- a/lib/api/src/js/imports.rs +++ b/lib/api/src/js/imports.rs @@ -22,7 +22,7 @@ use std::fmt; /// use wasmer::{Exports, Module, Store, Instance, imports, Imports, Function}; /// # fn foo_test(module: Module, store: Store) { /// -/// let host_fn = Function::new_native(foo); +/// let host_fn = Function::new_typed(foo); /// let import_object: Imports = imports! { /// "env" => { /// "foo" => host_fn, @@ -103,7 +103,7 @@ impl Imports { /// n /// } /// let mut import_object = Imports::new(); - /// import_object.define("env", "foo", Function::new_native(foo)); + /// import_object.define("env", "foo", Function::new_typed(&store, foo)); /// ``` pub fn define(&mut self, ns: &str, name: &str, val: impl Into) { self.map @@ -240,7 +240,7 @@ impl fmt::Debug for Imports { /// /// let import_object = imports! { /// "env" => { -/// "foo" => Function::new_native(&store, foo) +/// "foo" => Function::new_typed(&store, foo) /// }, /// }; /// @@ -331,42 +331,42 @@ mod test { let _ = imports! { "env" => { - "func" => Function::new_native(&store, func), + "func" => Function::new_typed(&store, func), }, }; let _ = imports! { "env" => { - "func" => Function::new_native(&store, func), + "func" => Function::new_typed(&store, func), } }; let _ = imports! { "env" => { - "func" => Function::new_native(&store, func), + "func" => Function::new_typed(&store, func), }, "abc" => { - "def" => Function::new_native(&store, func), + "def" => Function::new_typed(&store, func), } }; let _ = imports! { "env" => { - "func" => Function::new_native(&store, func) + "func" => Function::new_typed(&store, func) }, }; let _ = imports! { "env" => { - "func" => Function::new_native(&store, func) + "func" => Function::new_typed(&store, func) } }; let _ = imports! { "env" => { - "func1" => Function::new_native(&store, func), - "func2" => Function::new_native(&store, func) + "func1" => Function::new_typed(&store, func), + "func2" => Function::new_typed(&store, func) } }; let _ = imports! { "env" => { - "func1" => Function::new_native(&store, func), - "func2" => Function::new_native(&store, func), + "func1" => Function::new_typed(&store, func), + "func2" => Function::new_typed(&store, func), } }; } diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 7592337e8d2..777ace97093 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -147,11 +147,11 @@ //! //! ``` //! # use wasmer::{imports, Function, FunctionEnv, FunctionEnvMut, Memory, MemoryType, Store, Imports}; -//! # fn imports_example(mut env: FunctionEnv<()>, mut store: &mut Store) -> Imports { +//! # fn imports_example(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, &env, |_env: FunctionEnvMut<()>| println!("Hello")), +//! "my_function" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>| println!("Hello")), //! "memory" => memory, //! } //! } diff --git a/lib/api/src/sys/exports.rs b/lib/api/src/sys/exports.rs index 11536b9945a..f744a21c61d 100644 --- a/lib/api/src/sys/exports.rs +++ b/lib/api/src/sys/exports.rs @@ -166,7 +166,7 @@ impl Exports { Rets: WasmTypeList, { self.get_function(name)? - .native(store) + .typed(store) .map_err(|_| ExportError::IncompatibleType) } diff --git a/lib/api/src/sys/externals/function.rs b/lib/api/src/sys/externals/function.rs index 3a0bedd149f..6e43817520a 100644 --- a/lib/api/src/sys/externals/function.rs +++ b/lib/api/src/sys/externals/function.rs @@ -42,11 +42,31 @@ pub struct Function { } impl Function { + /// Creates a new host `Function` (dynamic) with the provided signature. + /// + /// If you know the signature of the host function at compile time, + /// consider using [`Function::new_typed`] for less runtime overhead. + #[cfg(feature = "compiler")] + pub fn new(store: &mut impl AsStoreMut, ty: FT, func: F) -> Self + where + FT: Into, + F: Fn(FunctionEnvMut<()>, &[Value]) -> Result, RuntimeError> + + 'static + + Send + + Sync, + { + let env = FunctionEnv::new(&mut store.as_store_mut(), ()); + Self::new_with_env(store, &env, ty, func) + } + #[cfg(feature = "compiler")] /// Creates a new host `Function` (dynamic) with the provided signature. /// /// If you know the signature of the host function at compile time, - /// consider using [`Function::new_native`] for less runtime overhead. + /// consider using [`Function::new_typed_with_env`] for less runtime overhead. + /// + /// Takes a [`FunctionEnv`] that is passed into func. If that is not required, + /// [`Function::new`] might be an option as well. /// /// # Examples /// @@ -57,7 +77,7 @@ impl Function { /// # /// let signature = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]); /// - /// let f = Function::new(&mut store, &env, &signature, |_env, args| { + /// let f = Function::new_with_env(&mut store, &env, &signature, |_env, args| { /// let sum = args[0].unwrap_i32() + args[1].unwrap_i32(); /// Ok(vec![Value::I32(sum)]) /// }); @@ -72,12 +92,12 @@ 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, |_env, args| { + /// let f = Function::new_with_env(&mut store, &env, I32_I32_TO_I32, |_env, args| { /// let sum = args[0].unwrap_i32() + args[1].unwrap_i32(); /// Ok(vec![Value::I32(sum)]) /// }); /// ``` - pub fn new( + pub fn new_with_env( store: &mut impl AsStoreMut, env: &FunctionEnv, ty: FT, @@ -161,7 +181,53 @@ impl Function { } #[cfg(feature = "compiler")] + #[deprecated( + since = "3.0.0", + note = "new_native() has been renamed to new_typed()." + )] /// Creates a new host `Function` from a native function. + pub fn new_native(store: &mut impl AsStoreMut, func: F) -> Self + where + F: HostFunction<(), Args, Rets> + 'static + Send + Sync, + Args: WasmTypeList, + Rets: WasmTypeList, + { + Self::new_typed(store, func) + } + + #[cfg(feature = "compiler")] + /// Creates a new host `Function` from a native function. + pub fn new_typed(store: &mut impl AsStoreMut, func: F) -> Self + where + F: HostFunction<(), Args, Rets> + 'static + Send + Sync, + Args: WasmTypeList, + Rets: WasmTypeList, + { + let env = FunctionEnv::new(store, ()); + Self::new_typed_with_env(store, &env, func) + } + + #[cfg(feature = "compiler")] + #[deprecated( + since = "3.0.0", + note = "new_native_with_env() has been renamed to new_typed_with_env()." + )] + /// Creates a new host `Function` with an environment from a native function. + pub fn new_native_with_env( + store: &mut impl AsStoreMut, + env: &FunctionEnv, + func: F, + ) -> Self + where + F: HostFunction + 'static + Send + Sync, + Args: WasmTypeList, + Rets: WasmTypeList, + { + Self::new_typed_with_env(store, env, func) + } + + #[cfg(feature = "compiler")] + /// Creates a new host `Function` with an environment from a typed function. /// /// The function signature is automatically retrieved using the /// Rust typing system. @@ -177,9 +243,9 @@ impl Function { /// a + b /// } /// - /// let f = Function::new_native(&mut store, &env, sum); + /// let f = Function::new_typed_with_env(&mut store, &env, sum); /// ``` - pub fn new_native( + pub fn new_typed_with_env( store: &mut impl AsStoreMut, env: &FunctionEnv, func: F, @@ -238,7 +304,7 @@ impl Function { /// a + b /// } /// - /// let f = Function::new_native(&mut store, &env, sum); + /// let f = Function::new_typed_with_env(&mut store, &env, sum); /// /// assert_eq!(f.ty(&mut store).params(), vec![Type::I32, Type::I32]); /// assert_eq!(f.ty(&mut store).results(), vec![Type::I32]); @@ -339,7 +405,7 @@ impl Function { /// a + b /// } /// - /// let f = Function::new_native(&mut store, &env, sum); + /// let f = Function::new_typed_with_env(&mut store, &env, sum); /// /// assert_eq!(f.param_arity(&mut store), 2); /// ``` @@ -360,7 +426,7 @@ impl Function { /// a + b /// } /// - /// let f = Function::new_native(&mut store, &env, sum); + /// let f = Function::new_typed_with_env(&mut store, &env, sum); /// /// assert_eq!(f.result_arity(&mut store), 1); /// ``` @@ -446,8 +512,23 @@ impl Function { } } - /// Transform this WebAssembly function into a function with the - /// native ABI. See [`TypedFunction`] to learn more. + /// Transform this WebAssembly function into a native function. + /// See [`TypedFunction`] to learn more. + #[cfg(feature = "compiler")] + #[deprecated(since = "3.0.0", note = "native() has been renamed to typed().")] + pub fn native( + &self, + store: &impl AsStoreRef, + ) -> Result, RuntimeError> + where + Args: WasmTypeList, + Rets: WasmTypeList, + { + self.typed(store) + } + + /// Transform this WebAssembly function into a typed function. + /// See [`TypedFunction`] to learn more. /// /// # Examples /// @@ -469,9 +550,9 @@ impl Function { /// # let instance = Instance::new(&mut store, &module, &import_object).unwrap(); /// # /// let sum = instance.exports.get_function("sum").unwrap(); - /// let sum_native: TypedFunction<(i32, i32), i32> = sum.native(&mut store).unwrap(); + /// let sum_typed: TypedFunction<(i32, i32), i32> = sum.typed(&mut store).unwrap(); /// - /// assert_eq!(sum_native.call(&mut store, 1, 2).unwrap(), 3); + /// assert_eq!(sum_typed.call(&mut store, 1, 2).unwrap(), 3); /// ``` /// /// # Errors @@ -499,7 +580,7 @@ impl Function { /// let sum = instance.exports.get_function("sum").unwrap(); /// /// // This results in an error: `RuntimeError` - /// let sum_native : TypedFunction<(i64, i64), i32> = sum.native(&mut store).unwrap(); + /// let sum_typed : TypedFunction<(i64, i64), i32> = sum.typed(&mut store).unwrap(); /// ``` /// /// If the `Rets` generic parameter does not match the exported function @@ -525,9 +606,9 @@ impl Function { /// let sum = instance.exports.get_function("sum").unwrap(); /// /// // This results in an error: `RuntimeError` - /// let sum_native: TypedFunction<(i32, i32), i64> = sum.native(&mut store).unwrap(); + /// let sum_typed: TypedFunction<(i32, i32), i64> = sum.typed(&mut store).unwrap(); /// ``` - pub fn native( + pub fn typed( &self, store: &impl AsStoreRef, ) -> Result, RuntimeError> @@ -984,7 +1065,8 @@ mod inner { } /// Represents a low-level Wasm static host function. See - /// `super::Function::new_native` to learn more. + /// [`super::Function::new_typed`] and + /// [`super::Function::new_typed_with_env`] to learn more. pub(crate) struct StaticFunction { pub(crate) raw_store: *mut u8, pub(crate) env: FunctionEnv, diff --git a/lib/api/src/sys/imports.rs b/lib/api/src/sys/imports.rs index 98fdc848c5b..c6e279bf533 100644 --- a/lib/api/src/sys/imports.rs +++ b/lib/api/src/sys/imports.rs @@ -16,10 +16,10 @@ use wasmer_types::ImportError; /// /// # Usage: /// ```no_run -/// use wasmer::{Store, Exports, Module, Instance, imports, Imports, Function, FunctionEnv, FunctionEnvMut}; -/// # fn foo_test(mut env: FunctionEnv<()>, mut store: &mut Store, module: Module) { +/// use wasmer::{Store, Exports, Module, Instance, imports, Imports, Function, FunctionEnvMut}; +/// # fn foo_test(mut store: &mut Store, module: Module) { /// -/// let host_fn = Function::new_native(&mut store, &env, foo); +/// let host_fn = Function::new_typed(&mut store, foo); /// let import_object: Imports = imports! { /// "env" => { /// "foo" => host_fn, @@ -99,13 +99,12 @@ impl Imports { /// ```no_run /// # use wasmer::{FunctionEnv, Store}; /// # let mut store: Store = Default::default(); - /// # let env = FunctionEnv::new(&mut store, ()); /// use wasmer::{StoreMut, Imports, Function, FunctionEnvMut}; /// fn foo(_env: FunctionEnvMut<()>, n: i32) -> i32 { /// n /// } /// let mut import_object = Imports::new(); - /// import_object.define("env", "foo", Function::new_native(&mut store, &env, foo)); + /// import_object.define("env", "foo", Function::new_typed(&mut store, foo)); /// ``` pub fn define(&mut self, ns: &str, name: &str, val: impl Into) { self.map @@ -210,14 +209,13 @@ impl fmt::Debug for Imports { /// # Usage /// /// ``` -/// # use wasmer::{StoreMut, Function, Store, FunctionEnv, FunctionEnvMut}; +/// # use wasmer::{StoreMut, Function, FunctionEnvMut, Store}; /// # let mut store = Store::default(); -/// # let env = FunctionEnv::new(&mut store, ()); /// use wasmer::imports; /// /// let import_object = imports! { /// "env" => { -/// "foo" => Function::new_native(&mut store, &env, foo) +/// "foo" => Function::new_typed(&mut store, foo) /// }, /// }; /// @@ -270,7 +268,6 @@ macro_rules! import_namespace { #[cfg(test)] mod test { - use crate::sys::FunctionEnv; use crate::sys::{AsStoreMut, Global, Store, Value}; use wasmer_types::Type; use wasmer_vm::VMExtern; @@ -303,7 +300,6 @@ mod test { use crate::sys::FunctionEnvMut; let mut store: Store = Default::default(); - let env = FunctionEnv::new(&mut store, ()); fn func(_env: FunctionEnvMut<()>, arg: i32) -> i32 { arg + 1 @@ -311,42 +307,42 @@ mod test { let _ = imports! { "env" => { - "func" => Function::new_native(&mut store, &env, func), + "func" => Function::new_typed(&mut store, func), }, }; let _ = imports! { "env" => { - "func" => Function::new_native(&mut store, &env, func), + "func" => Function::new_typed(&mut store, func), } }; let _ = imports! { "env" => { - "func" => Function::new_native(&mut store, &env, func), + "func" => Function::new_typed(&mut store, func), }, "abc" => { - "def" => Function::new_native(&mut store, &env, func), + "def" => Function::new_typed(&mut store, func), } }; let _ = imports! { "env" => { - "func" => Function::new_native(&mut store, &env, func) + "func" => Function::new_typed(&mut store, func) }, }; let _ = imports! { "env" => { - "func" => Function::new_native(&mut store, &env, func) + "func" => Function::new_typed(&mut store, func) } }; let _ = imports! { "env" => { - "func1" => Function::new_native(&mut store, &env, func), - "func2" => Function::new_native(&mut store, &env, func) + "func1" => Function::new_typed(&mut store, func), + "func2" => Function::new_typed(&mut store, func) } }; let _ = imports! { "env" => { - "func1" => Function::new_native(&mut store, &env, func), - "func2" => Function::new_native(&mut store, &env, func), + "func1" => Function::new_typed(&mut store, func), + "func2" => Function::new_typed(&mut store, func), } }; } diff --git a/lib/api/tests/js_externals.rs b/lib/api/tests/js_externals.rs index 4f8401232f3..980e85aad50 100644 --- a/lib/api/tests/js_externals.rs +++ b/lib/api/tests/js_externals.rs @@ -60,13 +60,12 @@ mod js { #[wasm_bindgen_test] fn table_new() { let mut store = Store::default(); - let env = FunctionEnv::new(&mut store, ()); let table_type = TableType { ty: Type::FuncRef, minimum: 0, maximum: None, }; - let f = Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, ()>| {}); + let f = Function::new_typed(&mut store, || {}); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f))).unwrap(); assert_eq!(table.ty(&store), table_type); @@ -177,40 +176,28 @@ mod js { #[wasm_bindgen_test] fn function_new() { let mut store = Store::default(); - let env = FunctionEnv::new(&mut store, ()); - let function = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<'_, ()>| {}); + let function = Function::new_typed(&mut store, || {}); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![], vec![]) ); - let function = - Function::new_native(&mut store, &env, |_env: FunctionEnvMut<'_, ()>, _a: i32| {}); + let function = Function::new_typed(&mut store, |_a: i32| {}); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![Type::I32], vec![]) ); - let function = Function::new_native( - &mut store, - &env, - |_env: FunctionEnvMut<'_, ()>, _a: i32, _b: i64, _c: f32, _d: f64| {}, - ); + let function = Function::new_typed(&mut store, |_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, |_env: FunctionEnvMut<'_, ()>| -> i32 { - 1 - }); + let function = Function::new_typed(&mut store, || -> i32 { 1 }); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![], vec![Type::I32]) ); - let function = Function::new_native( - &mut store, - &env, - |_env: FunctionEnvMut<'_, ()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, - ); + let function = + Function::new_typed(&mut store, || -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]) @@ -226,18 +213,22 @@ mod js { let my_env = MyEnv {}; let env = FunctionEnv::new(&mut store, my_env); - let function = Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, MyEnv>| {}); + let function = + Function::new_typed_with_env(&mut store, &env, |_: FunctionEnvMut<'_, MyEnv>| {}); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![], vec![]) ); - let function = - Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, MyEnv>, _a: i32| {}); + let function = Function::new_typed_with_env( + &mut store, + &env, + |_: FunctionEnvMut<'_, MyEnv>, _a: i32| {}, + ); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![Type::I32], vec![]) ); - let function = Function::new_native( + let function = Function::new_typed_with_env( &mut store, &env, |_: FunctionEnvMut<'_, MyEnv>, _a: i32, _b: i64, _c: f32, _d: f64| {}, @@ -247,14 +238,14 @@ mod js { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) ); let function = - Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, MyEnv>| -> i32 { + Function::new_typed_with_env(&mut store, &env, |_: FunctionEnvMut<'_, MyEnv>| -> i32 { 1 }); assert_eq!( function.ty(&store).clone(), FunctionType::new(vec![], vec![Type::I32]) ); - let function = Function::new_native( + let function = Function::new_typed_with_env( &mut store, &env, |_: FunctionEnvMut<'_, MyEnv>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, @@ -400,18 +391,14 @@ mod js { #[wasm_bindgen_test] fn native_function_works() { let mut store = Store::default(); - let env = FunctionEnv::new(&mut store, ()); - let function = Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, ()>| {}); - let typed_function: TypedFunction<(), ()> = function.native(&mut store).unwrap(); + let mut env = FunctionEnv::new(&mut store, ()); + let function = Function::new_typed(&mut store, || {}); + let typed_function: TypedFunction<(), ()> = function.typed(&mut store).unwrap(); let result = typed_function.call(&mut store); assert!(result.is_ok()); - let function = Function::new_native( - &mut store, - &env, - |_: FunctionEnvMut<'_, ()>, a: i32| -> i32 { a + 1 }, - ); - let typed_function: TypedFunction = function.native(&mut store).unwrap(); + let function = Function::new_typed(&mut store, |a: i32| -> i32 { a + 1 }); + let typed_function: TypedFunction = function.typed(&mut store).unwrap(); assert_eq!(typed_function.call(&mut store, 3).unwrap(), 4); // fn rust_abi(a: i32, b: i64, c: f32, d: f64) -> u64 { @@ -421,14 +408,12 @@ mod js { // let typed_function: TypedFunction<(i32, i64, f32, f64), u64> = function.native(&mut store).unwrap(); // assert_eq!(typed_function.call(8, 4, 1.5, 5.).unwrap(), 8415); - let function = - Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, ()>| -> i32 { 1 }); - let typed_function: TypedFunction<(), i32> = function.native(&mut store).unwrap(); + let function = Function::new_typed(&mut store, || -> i32 { 1 }); + let typed_function: TypedFunction<(), i32> = function.typed(&mut store).unwrap(); assert_eq!(typed_function.call(&mut store).unwrap(), 1); - let function = - Function::new_native(&mut store, &env, |_: FunctionEnvMut<'_, ()>, _a: i32| {}); - let typed_function: TypedFunction = function.native(&mut store).unwrap(); + let function = Function::new_typed(&mut store, |_a: i32| {}); + let typed_function: TypedFunction = function.typed(&mut store).unwrap(); assert!(typed_function.call(&mut store, 4).is_ok()); // let function = Function::new(&mut store, &env, || -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }); diff --git a/lib/api/tests/js_instance.rs b/lib/api/tests/js_instance.rs index 18e185dfca3..c61519801c1 100644 --- a/lib/api/tests/js_instance.rs +++ b/lib/api/tests/js_instance.rs @@ -291,8 +291,7 @@ mod js { return arg + 1; } - let env = FunctionEnv::new(&mut store, ()); - let imported = Function::new_native(&mut store, &env, imported_fn); + let imported = Function::new_typed(&mut store, imported_fn); let import_object = imports! { "env" => { @@ -348,7 +347,7 @@ mod js { let env = FunctionEnv::new(&mut store, Env { multiplier: 3 }); - let imported = Function::new_native(&mut store, &env, imported_fn); + let imported = Function::new_typed_with_env(&mut store, &env, imported_fn); let import_object = imports! { "env" => { @@ -412,7 +411,7 @@ mod js { memory: None, }, ); - let imported = Function::new_native(&mut store, &env, imported_fn); + let imported = Function::new_typed_with_env(&mut store, &env, imported_fn); let import_object = imports! { "env" => { @@ -630,11 +629,9 @@ mod js { a + b } - let env = FunctionEnv::new(&mut store, ()); - let import_object = imports! { "env" => { - "sum" => Function::new_native(&mut store, &env, sum), + "sum" => Function::new_typed(&mut store, sum), } }; @@ -670,11 +667,10 @@ mod js { fn early_exit(_: FunctionEnvMut<'_, ()>) { panic!("Do panic") } - let env = FunctionEnv::new(&mut store, ()); let import_object = imports! { "env" => { - "early_exit" => Function::new_native(&mut store, &env, early_exit), + "early_exit" => Function::new_typed(&mut store, early_exit), } }; let instance = Instance::new(&mut store, &module, &import_object).unwrap(); @@ -718,8 +714,6 @@ mod js { ) .unwrap(); - let env = FunctionEnv::new(&mut store, ()); - use std::fmt; #[derive(Debug, Clone, Copy)] @@ -739,7 +733,7 @@ mod js { let import_object = imports! { "env" => { - "early_exit" => Function::new_native(&mut store, &env, early_exit), + "early_exit" => Function::new_typed(&mut store, early_exit), } }; diff --git a/lib/api/tests/js_module.rs b/lib/api/tests/js_module.rs index 4a1b36f369a..44b525f1ac4 100644 --- a/lib/api/tests/js_module.rs +++ b/lib/api/tests/js_module.rs @@ -212,35 +212,35 @@ mod js { // let module = Module::new(&store, wat).unwrap(); // let imports = imports! { // "host" => { - // "host_func1" => Function::new_native(&store, |p: u64| { + // "host_func1" => Function::new_typed(&store, |p: u64| { // println!("host_func1: Found number {}", p); // assert_eq!(p, u64::max_value()); // }), - // "host_func2" => Function::new_native(&store, |p: u32| { + // "host_func2" => Function::new_typed(&store, |p: u32| { // println!("host_func2: Found number {}", p); // assert_eq!(p, u32::max_value()); // }), - // "host_func3" => Function::new_native(&store, |p: i64| { + // "host_func3" => Function::new_typed(&store, |p: i64| { // println!("host_func3: Found number {}", p); // assert_eq!(p, -1); // }), - // "host_func4" => Function::new_native(&store, |p: i32| { + // "host_func4" => Function::new_typed(&store, |p: i32| { // println!("host_func4: Found number {}", p); // assert_eq!(p, -1); // }), - // "host_func5" => Function::new_native(&store, |p: i16| { + // "host_func5" => Function::new_typed(&store, |p: i16| { // println!("host_func5: Found number {}", p); // assert_eq!(p, -1); // }), - // "host_func6" => Function::new_native(&store, |p: u16| { + // "host_func6" => Function::new_typed(&store, |p: u16| { // println!("host_func6: Found number {}", p); // assert_eq!(p, u16::max_value()); // }), - // "host_func7" => Function::new_native(&store, |p: i8| { + // "host_func7" => Function::new_typed(&store, |p: i8| { // println!("host_func7: Found number {}", p); // assert_eq!(p, -1); // }), - // "host_func8" => Function::new_native(&store, |p: u8| { + // "host_func8" => Function::new_typed(&store, |p: u8| { // println!("host_func8: Found number {}", p); // assert_eq!(p, u8::max_value()); // }), diff --git a/lib/api/tests/sys_externals.rs b/lib/api/tests/sys_externals.rs index a888d4e4918..710c1bb882e 100644 --- a/lib/api/tests/sys_externals.rs +++ b/lib/api/tests/sys_externals.rs @@ -69,8 +69,7 @@ mod sys { minimum: 0, maximum: None, }; - let env = FunctionEnv::new(&mut store, ()); - let f = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>| {}); + let f = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>| {}); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f)))?; assert_eq!(table.ty(&mut store), table_type); @@ -90,15 +89,12 @@ mod sys { #[ignore] fn table_get() -> Result<()> { let mut store = Store::default(); - let env = FunctionEnv::new(&mut store, ()); let table_type = TableType { ty: Type::FuncRef, minimum: 0, maximum: Some(1), }; - let f = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>, num: i32| { - num + 1 - }); + let f = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, num: i32| num + 1); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f)))?; assert_eq!(table.ty(&mut store), table_type); let _elem = table.get(&mut store, 0).unwrap(); @@ -116,15 +112,12 @@ mod sys { #[test] fn table_grow() -> Result<()> { let mut store = Store::default(); - let env = FunctionEnv::new(&mut store, ()); let table_type = TableType { ty: Type::FuncRef, minimum: 0, maximum: Some(10), }; - let f = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>, num: i32| { - num + 1 - }); + let f = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, num: i32| num + 1); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f.clone())))?; // Growing to a bigger maximum should return None let old_len = table.grow(&mut store, 12, Value::FuncRef(Some(f.clone()))); @@ -189,36 +182,31 @@ mod sys { #[test] fn function_new() -> Result<()> { let mut store = Store::default(); - let env = FunctionEnv::new(&mut store, ()); - let function = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>| {}); + let function = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![]) ); - let function = - Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>, _a: i32| {}); + let function = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, _a: i32| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![Type::I32], vec![]) ); - let function = Function::new_native( + let function = Function::new_typed( &mut store, - &env, |_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, |_env: FunctionEnvMut<()>| -> i32 { 1 }); + let function = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>| -> i32 { 1 }); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![Type::I32]) ); - let function = Function::new_native( + let function = Function::new_typed( &mut store, - &env, |_env: FunctionEnvMut<()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, ); assert_eq!( @@ -236,18 +224,22 @@ mod sys { let my_env = MyEnv {}; let env = FunctionEnv::new(&mut store, my_env); - let function = Function::new_native(&mut store, &env, |_env: FunctionEnvMut| {}); + let function = + Function::new_typed_with_env(&mut store, &env, |_env: FunctionEnvMut| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![]) ); - let function = - Function::new_native(&mut store, &env, |_env: FunctionEnvMut, _a: i32| {}); + let function = Function::new_typed_with_env( + &mut store, + &env, + |_env: FunctionEnvMut, _a: i32| {}, + ); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![Type::I32], vec![]) ); - let function = Function::new_native( + let function = Function::new_typed_with_env( &mut store, &env, |_env: FunctionEnvMut, _a: i32, _b: i64, _c: f32, _d: f64| {}, @@ -257,12 +249,14 @@ mod sys { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) ); let function = - Function::new_native(&mut store, &env, |_env: FunctionEnvMut| -> i32 { 1 }); + Function::new_typed_with_env(&mut store, &env, |_env: FunctionEnvMut| -> i32 { + 1 + }); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![Type::I32]) ); - let function = Function::new_native( + let function = Function::new_typed_with_env( &mut store, &env, |_env: FunctionEnvMut| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, @@ -277,13 +271,11 @@ mod sys { #[test] fn function_new_dynamic() -> Result<()> { let mut store = Store::default(); - let env = FunctionEnv::new(&mut store, ()); // Using &FunctionType signature let function_type = FunctionType::new(vec![], vec![]); let function = Function::new( &mut store, - &env, &function_type, |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -291,7 +283,6 @@ mod sys { let function_type = FunctionType::new(vec![Type::I32], vec![]); let function = Function::new( &mut store, - &env, &function_type, |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -300,7 +291,6 @@ mod sys { FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]); let function = Function::new( &mut store, - &env, &function_type, |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -308,7 +298,6 @@ mod sys { let function_type = FunctionType::new(vec![], vec![Type::I32]); let function = Function::new( &mut store, - &env, &function_type, |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -317,7 +306,6 @@ mod sys { FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]); let function = Function::new( &mut store, - &env, &function_type, |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -327,7 +315,6 @@ mod sys { let function_type = ([Type::V128], [Type::I32, Type::F32, Type::F64]); let function = Function::new( &mut store, - &env, function_type, |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), ); @@ -350,7 +337,7 @@ mod sys { // Using &FunctionType signature let function_type = FunctionType::new(vec![], vec![]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, &function_type, @@ -358,7 +345,7 @@ mod sys { ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32], vec![]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, &function_type, @@ -367,7 +354,7 @@ mod sys { assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, &function_type, @@ -375,7 +362,7 @@ mod sys { ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, &function_type, @@ -384,7 +371,7 @@ mod sys { assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, &function_type, @@ -394,7 +381,7 @@ mod sys { // Using array signature let function_type = ([Type::V128], [Type::I32, Type::F32, Type::F64]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, function_type, @@ -412,40 +399,39 @@ mod sys { // #[test] // fn native_function_works() -> Result<()> { // let mut store = Store::default(); - // let env = FunctionEnv::new(&mut store, ()); - // 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); + // let function = Function::new_typed(&mut store, || {}); + // let typed_function: TypedFunction<(), ()> = function.typed(&mut store).unwrap(); + // let result = typed_function.call(&mut store); // assert!(result.is_ok()); // let function = - // 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); + // Function::new_typed(&mut store, |a: i32| -> i32 { a + 1 }); + // let typed_function: TypedFunction = function.typed(&mut store).unwrap(); + // assert_eq!(typed_function.call(&mut store, 3).unwrap(), 4); - // fn rust_abi(_env: FunctionEnvMut<()>, a: i32, b: i64, c: f32, d: f64) -> u64 { + // fn rust_abi(a: i32, b: i64, c: f32, d: f64) -> u64 { // (a as u64 * 1000) + (b as u64 * 100) + (c as u64 * 10) + (d as u64) // } - // let function = Function::new_native(&mut store, &env, rust_abi); - // let native_function: TypedFunction<(i32, i64, f32, f64), u64> = - // function.native(&mut store).unwrap(); - // assert_eq!(native_function.call(&mut store, 8, 4, 1.5, 5.).unwrap(), 8415); + // let function = Function::new_typed(&mut store, rust_abi); + // let typed_function: TypedFunction<(i32, i64, f32, f64), u64> = + // function.typed(&mut store).unwrap(); + // assert_eq!(typed_function.call(&mut store, 8, 4, 1.5, 5.).unwrap(), 8415); - // 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_typed(&mut store, || -> i32 { 1 }); + // let typed_function: TypedFunction<(), i32> = function.typed(&mut store).unwrap(); + // assert_eq!(typed_function.call(&mut store).unwrap(), 1); - // 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_typed(&mut store, |_a: i32| {}); + // let typed_function: TypedFunction = function.typed(&mut store).unwrap(); + // assert!(typed_function.call(&mut store, 4).is_ok()); // let function = - // Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>| -> (i32, i64, f32, f64) { + // Function::new_typed(&mut store, || -> (i32, i64, f32, f64) { // (1, 2, 3.0, 4.0) // }); - // let native_function: TypedFunction<(), (i32, i64, f32, f64)> = - // function.native(&mut store).unwrap(); - // assert_eq!(native_function.call(&mut store).unwrap(), (1, 2, 3.0, 4.0)); + // let typed_function: TypedFunction<(), (i32, i64, f32, f64)> = + // function.typed(&mut store).unwrap(); + // assert_eq!(typed_function.call(&mut store).unwrap(), (1, 2, 3.0, 4.0)); // Ok(()) // } @@ -453,7 +439,6 @@ mod sys { // #[test] // fn function_outlives_instance() -> Result<()> { // let mut store = Store::default(); - // let env = FunctionEnv::new(&mut store, ()); // let wat = r#"(module // (type $sum_t (func (param i32 i32) (result i32))) // (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32) @@ -481,7 +466,6 @@ mod sys { // #[test] // fn weak_instance_ref_externs_after_instance() -> Result<()> { // let mut store = Store::default(); - // let env = FunctionEnv::new(&mut store, ()); // let wat = r#"(module // (memory (export "mem") 1) // (type $sum_t (func (param i32 i32) (result i32))) diff --git a/lib/api/tests/sys_instance.rs b/lib/api/tests/sys_instance.rs index d5b94297246..c1f51ed65b8 100644 --- a/lib/api/tests/sys_instance.rs +++ b/lib/api/tests/sys_instance.rs @@ -65,7 +65,7 @@ mod sys { let env = FunctionEnv::new(&mut store, env); let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let imported = Function::new(&mut store, &env, imported_signature, imported_fn); + let imported = Function::new_with_env(&mut store, &env, imported_signature, imported_fn); let expected = vec![Value::I32(12)].into_boxed_slice(); let result = imported.call(&mut store, &[Value::I32(4)])?; diff --git a/lib/api/tests/sys_module.rs b/lib/api/tests/sys_module.rs index 3d1834639b9..521ad320741 100644 --- a/lib/api/tests/sys_module.rs +++ b/lib/api/tests/sys_module.rs @@ -1,7 +1,6 @@ #[cfg(feature = "sys")] mod sys { use anyhow::Result; - use wasmer::FunctionEnv; use wasmer::*; #[test] @@ -191,38 +190,37 @@ mod sys { (call 7 (i32.const -1))) )"#; let module = Module::new(&store, wat)?; - let env = FunctionEnv::new(&mut store, ()); let imports = imports! { "host" => { - "host_func1" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: u64| { + "host_func1" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: u64| { println!("host_func1: Found number {}", p); assert_eq!(p, u64::max_value()); }), - "host_func2" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: u32| { + "host_func2" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: u32| { println!("host_func2: Found number {}", p); assert_eq!(p, u32::max_value()); }), - "host_func3" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: i64| { + "host_func3" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: i64| { println!("host_func3: Found number {}", p); assert_eq!(p, -1); }), - "host_func4" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: i32| { + "host_func4" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: i32| { println!("host_func4: Found number {}", p); assert_eq!(p, -1); }), - "host_func5" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: i16| { + "host_func5" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: i16| { println!("host_func5: Found number {}", p); assert_eq!(p, -1); }), - "host_func6" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: u16| { + "host_func6" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: u16| { println!("host_func6: Found number {}", p); assert_eq!(p, u16::max_value()); }), - "host_func7" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: i8| { + "host_func7" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: i8| { println!("host_func7: Found number {}", p); assert_eq!(p, -1); }), - "host_func8" => Function::new_native(&mut store, &env,|_env: FunctionEnvMut<()>, p: u8| { + "host_func8" => Function::new_typed(&mut store, |_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 24eb03f1137..f2cf89b3a81 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]), |_env: FunctionEnvMut, values: &[Value]| -> Result, _> { + "func_ref_identity" => Function::new_with_env(&mut store, &env, FunctionType::new([Type::FuncRef], [Type::FuncRef]), |_env: FunctionEnvMut, values: &[Value]| -> Result, _> { Ok(vec![values[0].clone()]) }) }, @@ -44,7 +44,7 @@ mod sys { } let func_to_call = - Function::new_native(&mut store, &env, |mut env: FunctionEnvMut| -> i32 { + Function::new_typed_with_env(&mut store, &env, |mut env: FunctionEnvMut| -> i32 { env.data_mut().0.store(true, Ordering::SeqCst); 343 }); @@ -88,20 +88,20 @@ mod sys { ) -> 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 env)?; + let f: TypedFunction<(i32, i32), i32> = f.typed(&mut env)?; Ok(vec![Value::I32(f.call(&mut env, 7, 9)?)]) } let imports = imports! { "env" => { - "func_ref_call" => Function::new( + "func_ref_call" => Function::new_with_env( &mut store, &env, FunctionType::new([Type::FuncRef], [Type::I32]), func_ref_call ), - // "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)?; + // "func_ref_call_native" => Function::new_native(&mut store, |f: Function| -> Result { + // let f: TypedFunction::<(i32, i32), i32> = f.typed(&mut store)?; // f.call(&mut store, 7, 9) // }) }, @@ -112,7 +112,7 @@ mod sys { fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { a + b } - let sum_func = Function::new_native(&mut store, &env, sum); + let sum_func = Function::new_typed(&mut store, sum); let call_func: &Function = instance.exports.get_function("call_func")?; let result = call_func.call(&mut store, &[Value::FuncRef(Some(sum_func))])?; @@ -157,7 +157,7 @@ mod sys { "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, |_env: FunctionEnvMut<()>, er: ExternRef| -> ExternRef { + "extern_ref_identity_native" => Function::new_typed(&mut store, |er: ExternRef| -> ExternRef { er }), "get_new_extern_ref" => Function::new(&mut store, &env, FunctionType::new([], [Type::ExternRef]), |_env, _| -> Result, _> { @@ -170,7 +170,7 @@ mod sys { 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,|_env| -> ExternRef { + "get_new_extern_ref_native" => Function::new_native(&mut store, || -> ExternRef { let inner = [("hello".to_string(), "world".to_string()), ("color".to_string(), "orange".to_string())] @@ -292,7 +292,7 @@ mod sys { panic!("Did not find a null func ref in the global"); } - let f = Function::new_native(&store, |arg1: i32, arg2: i32| -> i32 { arg1 + arg2 }); + let f = Function::new_typed(&store, |arg1: i32, arg2: i32| -> i32 { arg1 + arg2 }); fr_global.set(Value::FuncRef(Some(f)))?; diff --git a/lib/c-api/src/wasm_c_api/externals/function.rs b/lib/c-api/src/wasm_c_api/externals/function.rs index 7ed8fa1df11..e72c277edd2 100644 --- a/lib/c-api/src/wasm_c_api/externals/function.rs +++ b/lib/c-api/src/wasm_c_api/externals/function.rs @@ -55,7 +55,7 @@ pub unsafe extern "C" fn wasm_func_new( let func_sig = &function_type.inner().function_type; let num_rets = func_sig.results().len(); - let inner_callback = move |mut _ctx: FunctionEnvMut<'_, FunctionCEnv>, + let inner_callback = move |mut _env: FunctionEnvMut<'_, FunctionCEnv>, args: &[Value]| -> Result, RuntimeError> { let processed_args: wasm_val_vec_t = args @@ -90,7 +90,7 @@ pub unsafe extern "C" fn wasm_func_new( Ok(processed_results) }; let env = FunctionEnv::new(&mut store_mut, FunctionCEnv::default()); - let function = Function::new(&mut store_mut, &env, func_sig, inner_callback); + let function = Function::new_with_env(&mut store_mut, &env, func_sig, inner_callback); Some(Box::new(wasm_func_t { extern_: wasm_extern_t::new(store.inner.clone(), function.into()), })) @@ -179,7 +179,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env( env_finalizer: Arc::new(Mutex::new(env_finalizer)), }, ); - let function = Function::new(&mut store_mut, &env, func_sig, inner_callback); + let function = Function::new_with_env(&mut store_mut, &env, func_sig, inner_callback); Some(Box::new(wasm_func_t { extern_: wasm_extern_t::new(store.inner.clone(), function.into()), })) diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 69cf3068608..01d74db7ded 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -23,9 +23,9 @@ use std::f64; use std::path::PathBuf; use std::sync::{Arc, Mutex, RwLock}; use wasmer::{ - imports, namespace, AsStoreMut, ExportError, Exports, Function, FunctionEnv, FunctionEnvMut, - FunctionType, Global, Imports, Instance, Memory, MemoryType, Module, Pages, RuntimeError, - Table, TableType, TypedFunction, Value, WasmPtr, AsStoreRef, MemoryView, + imports, namespace, AsStoreMut, AsStoreRef, ExportError, Exports, Function, FunctionEnv, + FunctionEnvMut, FunctionType, Global, Imports, Instance, Memory, MemoryType, MemoryView, + Module, Pages, RuntimeError, Table, TableType, TypedFunction, Value, WasmPtr, }; use wasmer_types::Type as ValType; @@ -553,7 +553,7 @@ pub fn emscripten_get_main_func_name<'a>( pub fn emscripten_call_main( instance: &mut Instance, function_name: &str, - mut ctx: FunctionEnvMut, + mut env: FunctionEnvMut, path: &str, args: &[&str], ) -> Result<(), RuntimeError> { @@ -561,18 +561,18 @@ pub fn emscripten_call_main( .exports .get::(function_name) .map_err(|e| RuntimeError::new(e.to_string()))?; - let num_params = main_func.ty(&ctx).params().len(); + let num_params = main_func.ty(&env).params().len(); let _result = match num_params { 2 => { let mut new_args = vec![path]; new_args.extend(args); - let (argc, argv) = store_module_arguments(&mut ctx, new_args); + let (argc, argv) = store_module_arguments(&mut env, new_args); let func: &Function = instance .exports .get(function_name) .map_err(|e| RuntimeError::new(e.to_string()))?; func.call( - &mut ctx, + &mut env, &[Value::I32(argc as i32), Value::I32(argv as i32)], )?; } @@ -581,7 +581,7 @@ pub fn emscripten_call_main( .exports .get(function_name) .map_err(|e| RuntimeError::new(e.to_string()))?; - func.call(&mut ctx, &[])?; + func.call(&mut env, &[])?; } _ => { todo!("Update error type to be able to express this"); @@ -597,247 +597,247 @@ pub fn emscripten_call_main( /// Top level function to execute emscripten pub fn run_emscripten_instance( instance: &mut Instance, - mut ctx: FunctionEnvMut, + mut env: FunctionEnvMut, globals: &mut EmscriptenGlobals, path: &str, args: Vec<&str>, entrypoint: Option, ) -> Result<(), RuntimeError> { - let env = &mut ctx.data_mut(); - env.set_memory(globals.memory.clone()); + env.data_mut().set_memory(globals.memory.clone()); + // get emscripten export let mut emfuncs = EmscriptenFunctions::new(); - if let Ok(func) = instance.exports.get_typed_function(&ctx, "malloc") { + if let Ok(func) = instance.exports.get_typed_function(&env, "malloc") { emfuncs.malloc = Some(func); - } else if let Ok(func) = instance.exports.get_typed_function(&ctx, "_malloc") { + } else if let Ok(func) = instance.exports.get_typed_function(&env, "_malloc") { emfuncs.malloc = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "free") { + if let Ok(func) = instance.exports.get_typed_function(&env, "free") { emfuncs.free = Some(func); - } else if let Ok(func) = instance.exports.get_typed_function(&ctx, "_free") { + } else if let Ok(func) = instance.exports.get_typed_function(&env, "_free") { emfuncs.free = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "memalign") { + if let Ok(func) = instance.exports.get_typed_function(&env, "memalign") { emfuncs.memalign = Some(func); - } else if let Ok(func) = instance.exports.get_typed_function(&ctx, "_memalign") { + } else if let Ok(func) = instance.exports.get_typed_function(&env, "_memalign") { emfuncs.memalign = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "memset") { + if let Ok(func) = instance.exports.get_typed_function(&env, "memset") { emfuncs.memset = Some(func); - } else if let Ok(func) = instance.exports.get_typed_function(&ctx, "_memset") { + } else if let Ok(func) = instance.exports.get_typed_function(&env, "_memset") { emfuncs.memset = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "stackAlloc") { + if let Ok(func) = instance.exports.get_typed_function(&env, "stackAlloc") { emfuncs.stack_alloc = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_i") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_i") { emfuncs.dyn_call_i = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_ii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_ii") { emfuncs.dyn_call_ii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_iii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iii") { emfuncs.dyn_call_iii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_iiii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiii") { emfuncs.dyn_call_iiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_iifi") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iifi") { emfuncs.dyn_call_iifi = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_v") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_v") { emfuncs.dyn_call_v = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_vi") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vi") { emfuncs.dyn_call_vi = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_vii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vii") { emfuncs.dyn_call_vii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viii") { emfuncs.dyn_call_viii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viiii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viiii") { emfuncs.dyn_call_viiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_dii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_dii") { emfuncs.dyn_call_dii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_diiii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_diiii") { emfuncs.dyn_call_diiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_iiiii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiiii") { emfuncs.dyn_call_iiiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_iiiiii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiiiii") { emfuncs.dyn_call_iiiiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_iiiiiii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiiiiii") { emfuncs.dyn_call_iiiiiii = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_iiiiiiii") + .get_typed_function(&env, "dynCall_iiiiiiii") { emfuncs.dyn_call_iiiiiiii = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_iiiiiiiii") + .get_typed_function(&env, "dynCall_iiiiiiiii") { emfuncs.dyn_call_iiiiiiiii = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_iiiiiiiiii") + .get_typed_function(&env, "dynCall_iiiiiiiiii") { emfuncs.dyn_call_iiiiiiiiii = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_iiiiiiiiiii") + .get_typed_function(&env, "dynCall_iiiiiiiiiii") { emfuncs.dyn_call_iiiiiiiiiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_vd") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vd") { emfuncs.dyn_call_vd = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viiiii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viiiii") { emfuncs.dyn_call_viiiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viiiiii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viiiiii") { emfuncs.dyn_call_viiiiii = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_viiiiiii") + .get_typed_function(&env, "dynCall_viiiiiii") { emfuncs.dyn_call_viiiiiii = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_viiiiiiii") + .get_typed_function(&env, "dynCall_viiiiiiii") { emfuncs.dyn_call_viiiiiiii = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_viiiiiiiii") + .get_typed_function(&env, "dynCall_viiiiiiiii") { emfuncs.dyn_call_viiiiiiiii = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_viiiiiiiiii") + .get_typed_function(&env, "dynCall_viiiiiiiiii") { emfuncs.dyn_call_viiiiiiiiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_iij") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iij") { emfuncs.dyn_call_iij = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_iji") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iji") { emfuncs.dyn_call_iji = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_iiji") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiji") { emfuncs.dyn_call_iiji = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_iiijj") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_iiijj") { emfuncs.dyn_call_iiijj = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_j") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_j") { emfuncs.dyn_call_j = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_ji") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_ji") { emfuncs.dyn_call_ji = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_jii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_jii") { emfuncs.dyn_call_jii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_jij") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_jij") { emfuncs.dyn_call_jij = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_jjj") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_jjj") { emfuncs.dyn_call_jjj = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viiij") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viiij") { emfuncs.dyn_call_viiij = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_viiijiiii") + .get_typed_function(&env, "dynCall_viiijiiii") { emfuncs.dyn_call_viiijiiii = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_viiijiiiiii") + .get_typed_function(&env, "dynCall_viiijiiiiii") { emfuncs.dyn_call_viiijiiiiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viij") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viij") { emfuncs.dyn_call_viij = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viiji") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viiji") { emfuncs.dyn_call_viiji = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viijiii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viijiii") { emfuncs.dyn_call_viijiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viijj") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viijj") { emfuncs.dyn_call_viijj = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_vj") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vj") { emfuncs.dyn_call_vj = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_vjji") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vjji") { emfuncs.dyn_call_vjji = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_vij") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vij") { emfuncs.dyn_call_vij = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viji") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viji") { emfuncs.dyn_call_viji = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_vijiii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vijiii") { emfuncs.dyn_call_vijiii = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_vijj") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vijj") { emfuncs.dyn_call_vijj = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viid") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viid") { emfuncs.dyn_call_viid = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_vidd") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_vidd") { emfuncs.dyn_call_vidd = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "dynCall_viidii") { + if let Ok(func) = instance.exports.get_typed_function(&env, "dynCall_viidii") { emfuncs.dyn_call_viidii = Some(func); } if let Ok(func) = instance .exports - .get_typed_function(&ctx, "dynCall_viidddddddd") + .get_typed_function(&env, "dynCall_viidddddddd") { emfuncs.dyn_call_viidddddddd = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "stackSave") { + if let Ok(func) = instance.exports.get_typed_function(&env, "stackSave") { emfuncs.stack_save = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "stackRe&mut ctx") { + if let Ok(func) = instance.exports.get_typed_function(&env, "stackRestore") { emfuncs.stack_restore = Some(func); } - if let Ok(func) = instance.exports.get_typed_function(&ctx, "setThrew") { + if let Ok(func) = instance.exports.get_typed_function(&env, "setThrew") { emfuncs.set_threw = Some(func); } - ctx.data_mut().set_functions(emfuncs); + env.data_mut().set_functions(emfuncs); - set_up_emscripten(&mut ctx, instance)?; + set_up_emscripten(&mut env, instance)?; let main_func_names = ["_main", "main"]; if let Some(ep) = entrypoint.as_ref() { debug!("Running entry point: {}", ep); - emscripten_call_main(instance, ep, ctx, path, &args)?; + emscripten_call_main(instance, ep, env, path, &args)?; } else if let Ok(name) = emscripten_get_main_func_name(instance, &main_func_names) { - emscripten_call_main(instance, name, ctx, path, &args)?; + emscripten_call_main(instance, name, env, path, &args)?; } else { return Err(RuntimeError::new(format!( "No main function found (searched: {main_func_names:?}) and no entrypoint specified" @@ -849,16 +849,16 @@ pub fn run_emscripten_instance( Ok(()) } -fn store_module_arguments(ctx: &mut FunctionEnvMut, args: Vec<&str>) -> (u32, u32) { +fn store_module_arguments(env: &mut FunctionEnvMut, args: Vec<&str>) -> (u32, u32) { let argc = args.len() + 1; let mut args_slice = vec![0; argc]; for (slot, arg) in args_slice[0..argc].iter_mut().zip(args.iter()) { - *slot = unsafe { allocate_cstr_on_stack(&mut ctx.as_mut(), arg).0 }; + *slot = unsafe { allocate_cstr_on_stack(&mut env.as_mut(), arg).0 }; } let (argv_offset, argv_slice): (_, &mut [u32]) = - unsafe { allocate_on_stack(&mut ctx.as_mut(), ((argc) * 4) as u32) }; + unsafe { allocate_on_stack(&mut env.as_mut(), ((argc) * 4) as u32) }; assert!(!argv_slice.is_empty()); for (slot, arg) in argv_slice[0..argc].iter_mut().zip(args_slice.iter()) { *slot = *arg @@ -870,11 +870,11 @@ fn store_module_arguments(ctx: &mut FunctionEnvMut, args: Vec<&str>) -> ( pub fn emscripten_set_up_memory( store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, memory: &Memory, globals: &EmscriptenGlobalsData, ) -> Result<(), String> { - ctx.as_mut(store).set_memory(memory.clone()); + env.as_mut(store).set_memory(memory.clone()); let memory = memory.view(store); let dynamictop_ptr = WasmPtr::::new(globals.dynamictop_ptr).deref(&memory); let dynamic_base = globals.dynamic_base; @@ -914,7 +914,7 @@ pub struct EmscriptenGlobals { impl EmscriptenGlobals { pub fn new( mut store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, module: &Module, /*, static_bump: u32 */ ) -> Result { let mut use_old_abort_on_cannot_grow_memory = false; @@ -977,7 +977,7 @@ impl EmscriptenGlobals { } }; - emscripten_set_up_memory(store, ctx, &memory, &data)?; + emscripten_set_up_memory(store, env, &memory, &data)?; let mut null_function_names = vec![]; for import in module.imports().functions() { @@ -1002,17 +1002,17 @@ impl EmscriptenGlobals { pub fn generate_emscripten_env( mut store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, globals: &mut EmscriptenGlobals, ) -> Imports { let abort_on_cannot_grow_memory_export = if globals.data.use_old_abort_on_cannot_grow_memory { - Function::new_native( + Function::new_typed_with_env( &mut store, - ctx, + env, crate::memory::abort_on_cannot_grow_memory_old, ) } else { - Function::new_native(&mut store, ctx, crate::memory::abort_on_cannot_grow_memory) + Function::new_typed_with_env(&mut store, env, crate::memory::abort_on_cannot_grow_memory) }; let mut env_ns: Exports = namespace! { @@ -1033,427 +1033,427 @@ pub fn generate_emscripten_env( "tempDoublePtr" => Global::new(&mut store, Value::I32(globals.data.temp_double_ptr as i32)), // inet - "_inet_addr" => Function::new_native(&mut store, ctx, crate::inet::addr), + "_inet_addr" => Function::new_typed_with_env(&mut store, env, crate::inet::addr), // IO - "printf" => Function::new_native(&mut store, ctx, crate::io::printf), - "putchar" => Function::new_native(&mut store, ctx, crate::io::putchar), - "___lock" => Function::new_native(&mut store, ctx, crate::lock::___lock), - "___unlock" => Function::new_native(&mut store, ctx, crate::lock::___unlock), - "___wait" => Function::new_native(&mut store, ctx, crate::lock::___wait), - "_flock" => Function::new_native(&mut store, ctx, crate::lock::_flock), - "_chroot" => Function::new_native(&mut store, ctx, crate::io::chroot), - "_getprotobyname" => Function::new_native(&mut store, ctx, crate::io::getprotobyname), - "_getprotobynumber" => Function::new_native(&mut store, ctx, crate::io::getprotobynumber), - "_getpwuid" => Function::new_native(&mut store, ctx, crate::io::getpwuid), - "_sigdelset" => Function::new_native(&mut store, ctx, crate::io::sigdelset), - "_sigfillset" => Function::new_native(&mut store, ctx, crate::io::sigfillset), - "_tzset" => Function::new_native(&mut store, ctx, crate::io::tzset), - "_strptime" => Function::new_native(&mut store, ctx, crate::io::strptime), + "printf" => Function::new_typed_with_env(&mut store, env, crate::io::printf), + "putchar" => Function::new_typed_with_env(&mut store, env, crate::io::putchar), + "___lock" => Function::new_typed_with_env(&mut store, env, crate::lock::___lock), + "___unlock" => Function::new_typed_with_env(&mut store, env, crate::lock::___unlock), + "___wait" => Function::new_typed_with_env(&mut store, env, crate::lock::___wait), + "_flock" => Function::new_typed_with_env(&mut store, env, crate::lock::_flock), + "_chroot" => Function::new_typed_with_env(&mut store, env, crate::io::chroot), + "_getprotobyname" => Function::new_typed_with_env(&mut store, env, crate::io::getprotobyname), + "_getprotobynumber" => Function::new_typed_with_env(&mut store, env, crate::io::getprotobynumber), + "_getpwuid" => Function::new_typed_with_env(&mut store, env, crate::io::getpwuid), + "_sigdelset" => Function::new_typed_with_env(&mut store, env, crate::io::sigdelset), + "_sigfillset" => Function::new_typed_with_env(&mut store, env, crate::io::sigfillset), + "_tzset" => Function::new_typed_with_env(&mut store, env, crate::io::tzset), + "_strptime" => Function::new_typed_with_env(&mut store, env, crate::io::strptime), // exec - "_execvp" => Function::new_native(&mut store, ctx, crate::exec::execvp), - "_execl" => Function::new_native(&mut store, ctx, crate::exec::execl), - "_execle" => Function::new_native(&mut store, ctx, crate::exec::execle), + "_execvp" => Function::new_typed_with_env(&mut store, env, crate::exec::execvp), + "_execl" => Function::new_typed_with_env(&mut store, env, crate::exec::execl), + "_execle" => Function::new_typed_with_env(&mut store, env, crate::exec::execle), // exit - "__exit" => Function::new_native(&mut store, ctx, crate::exit::exit), + "__exit" => Function::new_typed_with_env(&mut store, env, crate::exit::exit), // Env - "___assert_fail" => Function::new_native(&mut store, ctx, crate::env::___assert_fail), - "_getenv" => Function::new_native(&mut store, ctx, crate::env::_getenv), - "_setenv" => Function::new_native(&mut store, ctx, crate::env::_setenv), - "_putenv" => Function::new_native(&mut store, ctx, crate::env::_putenv), - "_unsetenv" => Function::new_native(&mut store, ctx, crate::env::_unsetenv), - "_getpwnam" => Function::new_native(&mut store, ctx, crate::env::_getpwnam), - "_getgrnam" => Function::new_native(&mut store, ctx, crate::env::_getgrnam), - "___buildEnvironment" => Function::new_native(&mut store, ctx, crate::env::___build_environment), - "___setErrNo" => Function::new_native(&mut store, ctx, crate::errno::___seterrno), - "_getpagesize" => Function::new_native(&mut store, ctx, crate::env::_getpagesize), - "_sysconf" => Function::new_native(&mut store, ctx, crate::env::_sysconf), - "_getaddrinfo" => Function::new_native(&mut store, ctx, crate::env::_getaddrinfo), - "_times" => Function::new_native(&mut store, ctx, crate::env::_times), - "_pathconf" => Function::new_native(&mut store, ctx, crate::env::_pathconf), - "_fpathconf" => Function::new_native(&mut store, ctx, crate::env::_fpathconf), + "___assert_fail" => Function::new_typed_with_env(&mut store, env, crate::env::___assert_fail), + "_getenv" => Function::new_typed_with_env(&mut store, env, crate::env::_getenv), + "_setenv" => Function::new_typed_with_env(&mut store, env, crate::env::_setenv), + "_putenv" => Function::new_typed_with_env(&mut store, env, crate::env::_putenv), + "_unsetenv" => Function::new_typed_with_env(&mut store, env, crate::env::_unsetenv), + "_getpwnam" => Function::new_typed_with_env(&mut store, env, crate::env::_getpwnam), + "_getgrnam" => Function::new_typed_with_env(&mut store, env, crate::env::_getgrnam), + "___buildEnvironment" => Function::new_typed_with_env(&mut store, env, crate::env::___build_environment), + "___setErrNo" => Function::new_typed_with_env(&mut store, env, crate::errno::___seterrno), + "_getpagesize" => Function::new_typed_with_env(&mut store, env, crate::env::_getpagesize), + "_sysconf" => Function::new_typed_with_env(&mut store, env, crate::env::_sysconf), + "_getaddrinfo" => Function::new_typed_with_env(&mut store, env, crate::env::_getaddrinfo), + "_times" => Function::new_typed_with_env(&mut store, env, crate::env::_times), + "_pathconf" => Function::new_typed_with_env(&mut store, env, crate::env::_pathconf), + "_fpathconf" => Function::new_typed_with_env(&mut store, env, crate::env::_fpathconf), // Syscalls - "___syscall1" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall1), - "___syscall3" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall3), - "___syscall4" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall4), - "___syscall5" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall5), - "___syscall6" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall6), - "___syscall9" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall9), - "___syscall10" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall10), - "___syscall12" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall12), - "___syscall14" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall14), - "___syscall15" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall15), - "___syscall20" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall20), - "___syscall21" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall21), - "___syscall25" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall25), - "___syscall29" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall29), - "___syscall32" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall32), - "___syscall33" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall33), - "___syscall34" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall34), - "___syscall36" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall36), - "___syscall39" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall39), - "___syscall38" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall38), - "___syscall40" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall40), - "___syscall41" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall41), - "___syscall42" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall42), - "___syscall51" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall51), - "___syscall52" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall52), - "___syscall53" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall53), - "___syscall54" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall54), - "___syscall57" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall57), - "___syscall60" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall60), - "___syscall63" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall63), - "___syscall64" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall64), - "___syscall66" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall66), - "___syscall75" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall75), - "___syscall77" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall77), - "___syscall83" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall83), - "___syscall85" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall85), - "___syscall91" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall91), - "___syscall94" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall94), - "___syscall96" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall96), - "___syscall97" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall97), - "___syscall102" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall102), - "___syscall110" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall110), - "___syscall114" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall114), - "___syscall118" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall118), - "___syscall121" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall121), - "___syscall122" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall122), - "___syscall125" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall125), - "___syscall132" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall132), - "___syscall133" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall133), - "___syscall140" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall140), - "___syscall142" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall142), - "___syscall144" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall144), - "___syscall145" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall145), - "___syscall146" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall146), - "___syscall147" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall147), - "___syscall148" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall148), - "___syscall150" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall150), - "___syscall151" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall151), - "___syscall152" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall152), - "___syscall153" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall153), - "___syscall163" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall163), - "___syscall168" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall168), - "___syscall180" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall180), - "___syscall181" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall181), - "___syscall183" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall183), - "___syscall191" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall191), - "___syscall192" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall192), - "___syscall193" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall193), - "___syscall194" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall194), - "___syscall195" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall195), - "___syscall196" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall196), - "___syscall197" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall197), - "___syscall198" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall198), - "___syscall199" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall199), - "___syscall200" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall200), - "___syscall201" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall201), - "___syscall202" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall202), - "___syscall205" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall205), - "___syscall207" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall207), - "___syscall209" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall209), - "___syscall211" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall211), - "___syscall212" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall212), - "___syscall218" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall218), - "___syscall219" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall219), - "___syscall220" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall220), - "___syscall221" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall221), - "___syscall268" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall268), - "___syscall269" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall269), - "___syscall272" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall272), - "___syscall295" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall295), - "___syscall296" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall296), - "___syscall297" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall297), - "___syscall298" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall298), - "___syscall300" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall300), - "___syscall301" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall301), - "___syscall302" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall302), - "___syscall303" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall303), - "___syscall304" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall304), - "___syscall305" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall305), - "___syscall306" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall306), - "___syscall307" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall307), - "___syscall308" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall308), - "___syscall320" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall320), - "___syscall324" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall324), - "___syscall330" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall330), - "___syscall331" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall331), - "___syscall333" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall333), - "___syscall334" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall334), - "___syscall337" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall337), - "___syscall340" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall340), - "___syscall345" => Function::new_native(&mut store, ctx, crate::syscalls::___syscall345), + "___syscall1" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall1), + "___syscall3" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall3), + "___syscall4" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall4), + "___syscall5" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall5), + "___syscall6" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall6), + "___syscall9" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall9), + "___syscall10" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall10), + "___syscall12" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall12), + "___syscall14" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall14), + "___syscall15" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall15), + "___syscall20" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall20), + "___syscall21" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall21), + "___syscall25" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall25), + "___syscall29" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall29), + "___syscall32" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall32), + "___syscall33" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall33), + "___syscall34" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall34), + "___syscall36" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall36), + "___syscall39" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall39), + "___syscall38" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall38), + "___syscall40" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall40), + "___syscall41" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall41), + "___syscall42" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall42), + "___syscall51" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall51), + "___syscall52" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall52), + "___syscall53" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall53), + "___syscall54" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall54), + "___syscall57" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall57), + "___syscall60" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall60), + "___syscall63" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall63), + "___syscall64" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall64), + "___syscall66" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall66), + "___syscall75" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall75), + "___syscall77" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall77), + "___syscall83" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall83), + "___syscall85" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall85), + "___syscall91" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall91), + "___syscall94" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall94), + "___syscall96" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall96), + "___syscall97" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall97), + "___syscall102" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall102), + "___syscall110" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall110), + "___syscall114" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall114), + "___syscall118" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall118), + "___syscall121" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall121), + "___syscall122" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall122), + "___syscall125" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall125), + "___syscall132" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall132), + "___syscall133" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall133), + "___syscall140" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall140), + "___syscall142" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall142), + "___syscall144" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall144), + "___syscall145" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall145), + "___syscall146" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall146), + "___syscall147" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall147), + "___syscall148" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall148), + "___syscall150" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall150), + "___syscall151" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall151), + "___syscall152" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall152), + "___syscall153" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall153), + "___syscall163" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall163), + "___syscall168" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall168), + "___syscall180" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall180), + "___syscall181" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall181), + "___syscall183" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall183), + "___syscall191" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall191), + "___syscall192" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall192), + "___syscall193" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall193), + "___syscall194" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall194), + "___syscall195" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall195), + "___syscall196" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall196), + "___syscall197" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall197), + "___syscall198" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall198), + "___syscall199" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall199), + "___syscall200" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall200), + "___syscall201" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall201), + "___syscall202" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall202), + "___syscall205" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall205), + "___syscall207" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall207), + "___syscall209" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall209), + "___syscall211" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall211), + "___syscall212" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall212), + "___syscall218" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall218), + "___syscall219" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall219), + "___syscall220" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall220), + "___syscall221" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall221), + "___syscall268" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall268), + "___syscall269" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall269), + "___syscall272" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall272), + "___syscall295" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall295), + "___syscall296" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall296), + "___syscall297" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall297), + "___syscall298" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall298), + "___syscall300" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall300), + "___syscall301" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall301), + "___syscall302" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall302), + "___syscall303" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall303), + "___syscall304" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall304), + "___syscall305" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall305), + "___syscall306" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall306), + "___syscall307" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall307), + "___syscall308" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall308), + "___syscall320" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall320), + "___syscall324" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall324), + "___syscall330" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall330), + "___syscall331" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall331), + "___syscall333" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall333), + "___syscall334" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall334), + "___syscall337" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall337), + "___syscall340" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall340), + "___syscall345" => Function::new_typed_with_env(&mut store, env, crate::syscalls::___syscall345), // Process - "abort" => Function::new_native(&mut store, ctx, crate::process::em_abort), - "_abort" => Function::new_native(&mut store, ctx, crate::process::_abort), - "_prctl" => Function::new_native(&mut store, ctx, crate::process::_prctl), - "abortStackOverflow" => Function::new_native(&mut store, ctx, crate::process::abort_stack_overflow), - "_llvm_trap" => Function::new_native(&mut store, ctx, crate::process::_llvm_trap), - "_fork" => Function::new_native(&mut store, ctx, crate::process::_fork), - "_exit" => Function::new_native(&mut store, ctx, crate::process::_exit), - "_system" => Function::new_native(&mut store, ctx, crate::process::_system), - "_popen" => Function::new_native(&mut store, ctx, crate::process::_popen), - "_endgrent" => Function::new_native(&mut store, ctx, crate::process::_endgrent), - "_execve" => Function::new_native(&mut store, ctx, crate::process::_execve), - "_kill" => Function::new_native(&mut store, ctx, crate::process::_kill), - "_llvm_stackrestore" => Function::new_native(&mut store, ctx, crate::process::_llvm_stackrestore), - "_llvm_stacksave" => Function::new_native(&mut store, ctx, crate::process::_llvm_stacksave), - "_llvm_eh_typeid_for" => Function::new_native(&mut store, ctx, crate::process::_llvm_eh_typeid_for), - "_raise" => Function::new_native(&mut store, ctx, crate::process::_raise), - "_sem_init" => Function::new_native(&mut store, ctx, crate::process::_sem_init), - "_sem_destroy" => Function::new_native(&mut store, ctx, crate::process::_sem_destroy), - "_sem_post" => Function::new_native(&mut store, ctx, crate::process::_sem_post), - "_sem_wait" => Function::new_native(&mut store, ctx, crate::process::_sem_wait), - "_getgrent" => Function::new_native(&mut store, ctx, crate::process::_getgrent), - "_sched_yield" => Function::new_native(&mut store, ctx, crate::process::_sched_yield), - "_setgrent" => Function::new_native(&mut store, ctx, crate::process::_setgrent), - "_setgroups" => Function::new_native(&mut store, ctx, crate::process::_setgroups), - "_setitimer" => Function::new_native(&mut store, ctx, crate::process::_setitimer), - "_usleep" => Function::new_native(&mut store, ctx, crate::process::_usleep), - "_nanosleep" => Function::new_native(&mut store, ctx, crate::process::_nanosleep), - "_utime" => Function::new_native(&mut store, ctx, crate::process::_utime), - "_utimes" => Function::new_native(&mut store, ctx, crate::process::_utimes), - "_wait" => Function::new_native(&mut store, ctx, crate::process::_wait), - "_wait3" => Function::new_native(&mut store, ctx, crate::process::_wait3), - "_wait4" => Function::new_native(&mut store, ctx, crate::process::_wait4), - "_waitid" => Function::new_native(&mut store, ctx, crate::process::_waitid), - "_waitpid" => Function::new_native(&mut store, ctx, crate::process::_waitpid), + "abort" => Function::new_typed_with_env(&mut store, env, crate::process::em_abort), + "_abort" => Function::new_typed_with_env(&mut store, env, crate::process::_abort), + "_prctl" => Function::new_typed_with_env(&mut store, env, crate::process::_prctl), + "abortStackOverflow" => Function::new_typed_with_env(&mut store, env, crate::process::abort_stack_overflow), + "_llvm_trap" => Function::new_typed_with_env(&mut store, env, crate::process::_llvm_trap), + "_fork" => Function::new_typed_with_env(&mut store, env, crate::process::_fork), + "_exit" => Function::new_typed_with_env(&mut store, env, crate::process::_exit), + "_system" => Function::new_typed_with_env(&mut store, env, crate::process::_system), + "_popen" => Function::new_typed_with_env(&mut store, env, crate::process::_popen), + "_endgrent" => Function::new_typed_with_env(&mut store, env, crate::process::_endgrent), + "_execve" => Function::new_typed_with_env(&mut store, env, crate::process::_execve), + "_kill" => Function::new_typed_with_env(&mut store, env, crate::process::_kill), + "_llvm_stackrestore" => Function::new_typed_with_env(&mut store, env, crate::process::_llvm_stackrestore), + "_llvm_stacksave" => Function::new_typed_with_env(&mut store, env, crate::process::_llvm_stacksave), + "_llvm_eh_typeid_for" => Function::new_typed_with_env(&mut store, env, crate::process::_llvm_eh_typeid_for), + "_raise" => Function::new_typed_with_env(&mut store, env, crate::process::_raise), + "_sem_init" => Function::new_typed_with_env(&mut store, env, crate::process::_sem_init), + "_sem_destroy" => Function::new_typed_with_env(&mut store, env, crate::process::_sem_destroy), + "_sem_post" => Function::new_typed_with_env(&mut store, env, crate::process::_sem_post), + "_sem_wait" => Function::new_typed_with_env(&mut store, env, crate::process::_sem_wait), + "_getgrent" => Function::new_typed_with_env(&mut store, env, crate::process::_getgrent), + "_sched_yield" => Function::new_typed_with_env(&mut store, env, crate::process::_sched_yield), + "_setgrent" => Function::new_typed_with_env(&mut store, env, crate::process::_setgrent), + "_setgroups" => Function::new_typed_with_env(&mut store, env, crate::process::_setgroups), + "_setitimer" => Function::new_typed_with_env(&mut store, env, crate::process::_setitimer), + "_usleep" => Function::new_typed_with_env(&mut store, env, crate::process::_usleep), + "_nanosleep" => Function::new_typed_with_env(&mut store, env, crate::process::_nanosleep), + "_utime" => Function::new_typed_with_env(&mut store, env, crate::process::_utime), + "_utimes" => Function::new_typed_with_env(&mut store, env, crate::process::_utimes), + "_wait" => Function::new_typed_with_env(&mut store, env, crate::process::_wait), + "_wait3" => Function::new_typed_with_env(&mut store, env, crate::process::_wait3), + "_wait4" => Function::new_typed_with_env(&mut store, env, crate::process::_wait4), + "_waitid" => Function::new_typed_with_env(&mut store, env, crate::process::_waitid), + "_waitpid" => Function::new_typed_with_env(&mut store, env, crate::process::_waitpid), // Emscripten - "_emscripten_asm_const_i" => Function::new_native(&mut store, ctx, crate::emscripten_target::asm_const_i), - "_emscripten_exit_with_live_runtime" => Function::new_native(&mut store, ctx, crate::emscripten_target::exit_with_live_runtime), + "_emscripten_asm_const_i" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::asm_const_i), + "_emscripten_exit_with_live_runtime" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::exit_with_live_runtime), // Signal - "_sigemptyset" => Function::new_native(&mut store, ctx, crate::signal::_sigemptyset), - "_sigaddset" => Function::new_native(&mut store, ctx, crate::signal::_sigaddset), - "_sigprocmask" => Function::new_native(&mut store, ctx, crate::signal::_sigprocmask), - "_sigaction" => Function::new_native(&mut store, ctx, crate::signal::_sigaction), - "_signal" => Function::new_native(&mut store, ctx, crate::signal::_signal), - "_sigsuspend" => Function::new_native(&mut store, ctx, crate::signal::_sigsuspend), + "_sigemptyset" => Function::new_typed_with_env(&mut store, env, crate::signal::_sigemptyset), + "_sigaddset" => Function::new_typed_with_env(&mut store, env, crate::signal::_sigaddset), + "_sigprocmask" => Function::new_typed_with_env(&mut store, env, crate::signal::_sigprocmask), + "_sigaction" => Function::new_typed_with_env(&mut store, env, crate::signal::_sigaction), + "_signal" => Function::new_typed_with_env(&mut store, env, crate::signal::_signal), + "_sigsuspend" => Function::new_typed_with_env(&mut store, env, crate::signal::_sigsuspend), // Memory "abortOnCannotGrowMemory" => abort_on_cannot_grow_memory_export, - "_emscripten_memcpy_big" => Function::new_native(&mut store, ctx, crate::memory::_emscripten_memcpy_big), - "_emscripten_get_heap_size" => Function::new_native(&mut store, ctx, crate::memory::_emscripten_get_heap_size), - "_emscripten_resize_heap" => Function::new_native(&mut store, ctx, crate::memory::_emscripten_resize_heap), - "enlargeMemory" => Function::new_native(&mut store, ctx, crate::memory::enlarge_memory), - "segfault" => Function::new_native(&mut store, ctx, crate::memory::segfault), - "alignfault" => Function::new_native(&mut store, ctx, crate::memory::alignfault), - "ftfault" => Function::new_native(&mut store, ctx, crate::memory::ftfault), - "getTotalMemory" => Function::new_native(&mut store, ctx, crate::memory::get_total_memory), - "_sbrk" => Function::new_native(&mut store, ctx, crate::memory::sbrk), - "___map_file" => Function::new_native(&mut store, ctx, crate::memory::___map_file), + "_emscripten_memcpy_big" => Function::new_typed_with_env(&mut store, env, crate::memory::_emscripten_memcpy_big), + "_emscripten_get_heap_size" => Function::new_typed_with_env(&mut store, env, crate::memory::_emscripten_get_heap_size), + "_emscripten_resize_heap" => Function::new_typed_with_env(&mut store, env, crate::memory::_emscripten_resize_heap), + "enlargeMemory" => Function::new_typed_with_env(&mut store, env, crate::memory::enlarge_memory), + "segfault" => Function::new_typed_with_env(&mut store, env, crate::memory::segfault), + "alignfault" => Function::new_typed_with_env(&mut store, env, crate::memory::alignfault), + "ftfault" => Function::new_typed_with_env(&mut store, env, crate::memory::ftfault), + "getTotalMemory" => Function::new_typed_with_env(&mut store, env, crate::memory::get_total_memory), + "_sbrk" => Function::new_typed_with_env(&mut store, env, crate::memory::sbrk), + "___map_file" => Function::new_typed_with_env(&mut store, env, crate::memory::___map_file), // Exception - "___cxa_allocate_exception" => Function::new_native(&mut store, ctx, crate::exception::___cxa_allocate_exception), - "___cxa_current_primary_exception" => Function::new_native(&mut store, ctx, crate::exception::___cxa_current_primary_exception), - "___cxa_decrement_exception_refcount" => Function::new_native(&mut store, ctx, crate::exception::___cxa_decrement_exception_refcount), - "___cxa_increment_exception_refcount" => Function::new_native(&mut store, ctx, crate::exception::___cxa_increment_exception_refcount), - "___cxa_rethrow_primary_exception" => Function::new_native(&mut store, ctx, crate::exception::___cxa_rethrow_primary_exception), - "___cxa_throw" => Function::new_native(&mut store, ctx, crate::exception::___cxa_throw), - "___cxa_begin_catch" => Function::new_native(&mut store, ctx, crate::exception::___cxa_begin_catch), - "___cxa_end_catch" => Function::new_native(&mut store, ctx, crate::exception::___cxa_end_catch), - "___cxa_uncaught_exception" => Function::new_native(&mut store, ctx, crate::exception::___cxa_uncaught_exception), - "___cxa_pure_virtual" => Function::new_native(&mut store, ctx, crate::exception::___cxa_pure_virtual), + "___cxa_allocate_exception" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_allocate_exception), + "___cxa_current_primary_exception" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_current_primary_exception), + "___cxa_decrement_exception_refcount" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_decrement_exception_refcount), + "___cxa_increment_exception_refcount" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_increment_exception_refcount), + "___cxa_rethrow_primary_exception" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_rethrow_primary_exception), + "___cxa_throw" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_throw), + "___cxa_begin_catch" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_begin_catch), + "___cxa_end_catch" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_end_catch), + "___cxa_uncaught_exception" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_uncaught_exception), + "___cxa_pure_virtual" => Function::new_typed_with_env(&mut store, env, crate::exception::___cxa_pure_virtual), // Time - "_gettimeofday" => Function::new_native(&mut store, ctx, crate::time::_gettimeofday), - "_clock_getres" => Function::new_native(&mut store, ctx, crate::time::_clock_getres), - "_clock_gettime" => Function::new_native(&mut store, ctx, crate::time::_clock_gettime), - "_clock_settime" => Function::new_native(&mut store, ctx, crate::time::_clock_settime), - "___clock_gettime" => Function::new_native(&mut store, ctx, crate::time::_clock_gettime), - "_clock" => Function::new_native(&mut store, ctx, crate::time::_clock), - "_difftime" => Function::new_native(&mut store, ctx, crate::time::_difftime), - "_asctime" => Function::new_native(&mut store, ctx, crate::time::_asctime), - "_asctime_r" => Function::new_native(&mut store, ctx, crate::time::_asctime_r), - "_localtime" => Function::new_native(&mut store, ctx, crate::time::_localtime), - "_time" => Function::new_native(&mut store, ctx, crate::time::_time), - "_timegm" => Function::new_native(&mut store, ctx, crate::time::_timegm), - "_strftime" => Function::new_native(&mut store, ctx, crate::time::_strftime), - "_strftime_l" => Function::new_native(&mut store, ctx, crate::time::_strftime_l), - "_localtime_r" => Function::new_native(&mut store, ctx, crate::time::_localtime_r), - "_gmtime_r" => Function::new_native(&mut store, ctx, crate::time::_gmtime_r), - "_ctime" => Function::new_native(&mut store, ctx, crate::time::_ctime), - "_ctime_r" => Function::new_native(&mut store, ctx, crate::time::_ctime_r), - "_mktime" => Function::new_native(&mut store, ctx, crate::time::_mktime), - "_gmtime" => Function::new_native(&mut store, ctx, crate::time::_gmtime), + "_gettimeofday" => Function::new_typed_with_env(&mut store, env, crate::time::_gettimeofday), + "_clock_getres" => Function::new_typed_with_env(&mut store, env, crate::time::_clock_getres), + "_clock_gettime" => Function::new_typed_with_env(&mut store, env, crate::time::_clock_gettime), + "_clock_settime" => Function::new_typed_with_env(&mut store, env, crate::time::_clock_settime), + "___clock_gettime" => Function::new_typed_with_env(&mut store, env, crate::time::_clock_gettime), + "_clock" => Function::new_typed_with_env(&mut store, env, crate::time::_clock), + "_difftime" => Function::new_typed_with_env(&mut store, env, crate::time::_difftime), + "_asctime" => Function::new_typed_with_env(&mut store, env, crate::time::_asctime), + "_asctime_r" => Function::new_typed_with_env(&mut store, env, crate::time::_asctime_r), + "_localtime" => Function::new_typed_with_env(&mut store, env, crate::time::_localtime), + "_time" => Function::new_typed_with_env(&mut store, env, crate::time::_time), + "_timegm" => Function::new_typed_with_env(&mut store, env, crate::time::_timegm), + "_strftime" => Function::new_typed_with_env(&mut store, env, crate::time::_strftime), + "_strftime_l" => Function::new_typed_with_env(&mut store, env, crate::time::_strftime_l), + "_localtime_r" => Function::new_typed_with_env(&mut store, env, crate::time::_localtime_r), + "_gmtime_r" => Function::new_typed_with_env(&mut store, env, crate::time::_gmtime_r), + "_ctime" => Function::new_typed_with_env(&mut store, env, crate::time::_ctime), + "_ctime_r" => Function::new_typed_with_env(&mut store, env, crate::time::_ctime_r), + "_mktime" => Function::new_typed_with_env(&mut store, env, crate::time::_mktime), + "_gmtime" => Function::new_typed_with_env(&mut store, env, crate::time::_gmtime), // Math - "sqrt" => Function::new_native(&mut store, ctx, crate::math::sqrt), - "floor" => Function::new_native(&mut store, ctx, crate::math::floor), - "fabs" => Function::new_native(&mut store, ctx, crate::math::fabs), - "f64-rem" => Function::new_native(&mut store, ctx, crate::math::f64_rem), - "_llvm_copysign_f32" => Function::new_native(&mut store, ctx, crate::math::_llvm_copysign_f32), - "_llvm_copysign_f64" => Function::new_native(&mut store, ctx, crate::math::_llvm_copysign_f64), - "_llvm_log10_f64" => Function::new_native(&mut store, ctx, crate::math::_llvm_log10_f64), - "_llvm_log2_f64" => Function::new_native(&mut store, ctx, crate::math::_llvm_log2_f64), - "_llvm_log10_f32" => Function::new_native(&mut store, ctx, crate::math::_llvm_log10_f32), - "_llvm_log2_f32" => Function::new_native(&mut store, ctx, crate::math::_llvm_log2_f64), - "_llvm_sin_f64" => Function::new_native(&mut store, ctx, crate::math::_llvm_sin_f64), - "_llvm_cos_f64" => Function::new_native(&mut store, ctx, crate::math::_llvm_cos_f64), - "_llvm_exp2_f32" => Function::new_native(&mut store, ctx, crate::math::_llvm_exp2_f32), - "_llvm_exp2_f64" => Function::new_native(&mut store, ctx, crate::math::_llvm_exp2_f64), - "_llvm_trunc_f64" => Function::new_native(&mut store, ctx, crate::math::_llvm_trunc_f64), - "_llvm_fma_f64" => Function::new_native(&mut store, ctx, crate::math::_llvm_fma_f64), - "_emscripten_random" => Function::new_native(&mut store, ctx, crate::math::_emscripten_random), + "sqrt" => Function::new_typed_with_env(&mut store, env, crate::math::sqrt), + "floor" => Function::new_typed_with_env(&mut store, env, crate::math::floor), + "fabs" => Function::new_typed_with_env(&mut store, env, crate::math::fabs), + "f64-rem" => Function::new_typed_with_env(&mut store, env, crate::math::f64_rem), + "_llvm_copysign_f32" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_copysign_f32), + "_llvm_copysign_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_copysign_f64), + "_llvm_log10_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_log10_f64), + "_llvm_log2_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_log2_f64), + "_llvm_log10_f32" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_log10_f32), + "_llvm_log2_f32" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_log2_f64), + "_llvm_sin_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_sin_f64), + "_llvm_cos_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_cos_f64), + "_llvm_exp2_f32" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_exp2_f32), + "_llvm_exp2_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_exp2_f64), + "_llvm_trunc_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_trunc_f64), + "_llvm_fma_f64" => Function::new_typed_with_env(&mut store, env, crate::math::_llvm_fma_f64), + "_emscripten_random" => Function::new_typed_with_env(&mut store, env, crate::math::_emscripten_random), // Jump - "__setjmp" => Function::new_native(&mut store, ctx, crate::jmp::__setjmp), - "__longjmp" => Function::new_native(&mut store, ctx, crate::jmp::__longjmp), - "_longjmp" => Function::new_native(&mut store, ctx, crate::jmp::_longjmp), - "_emscripten_longjmp" => Function::new_native(&mut store, ctx, crate::jmp::_longjmp), + "__setjmp" => Function::new_typed_with_env(&mut store, env, crate::jmp::__setjmp), + "__longjmp" => Function::new_typed_with_env(&mut store, env, crate::jmp::__longjmp), + "_longjmp" => Function::new_typed_with_env(&mut store, env, crate::jmp::_longjmp), + "_emscripten_longjmp" => Function::new_typed_with_env(&mut store, env, crate::jmp::_longjmp), // Bitwise - "_llvm_bswap_i64" => Function::new_native(&mut store, ctx, crate::bitwise::_llvm_bswap_i64), + "_llvm_bswap_i64" => Function::new_typed_with_env(&mut store, env, crate::bitwise::_llvm_bswap_i64), // libc - "_execv" => Function::new_native(&mut store, ctx, crate::libc::execv), - "_endpwent" => Function::new_native(&mut store, ctx, crate::libc::endpwent), - "_fexecve" => Function::new_native(&mut store, ctx, crate::libc::fexecve), - "_fpathconf" => Function::new_native(&mut store, ctx, crate::libc::fpathconf), - "_getitimer" => Function::new_native(&mut store, ctx, crate::libc::getitimer), - "_getpwent" => Function::new_native(&mut store, ctx, crate::libc::getpwent), - "_killpg" => Function::new_native(&mut store, ctx, crate::libc::killpg), - "_pathconf" => Function::new_native(&mut store, ctx, crate::libc::pathconf), - "_siginterrupt" => Function::new_native(&mut store, ctx, crate::signal::_siginterrupt), - "_setpwent" => Function::new_native(&mut store, ctx, crate::libc::setpwent), - "_sigismember" => Function::new_native(&mut store, ctx, crate::libc::sigismember), - "_sigpending" => Function::new_native(&mut store, ctx, crate::libc::sigpending), - "___libc_current_sigrtmax" => Function::new_native(&mut store, ctx, crate::libc::current_sigrtmax), - "___libc_current_sigrtmin" => Function::new_native(&mut store, ctx, crate::libc::current_sigrtmin), + "_execv" => Function::new_typed_with_env(&mut store, env, crate::libc::execv), + "_endpwent" => Function::new_typed_with_env(&mut store, env, crate::libc::endpwent), + "_fexecve" => Function::new_typed_with_env(&mut store, env, crate::libc::fexecve), + "_fpathconf" => Function::new_typed_with_env(&mut store, env, crate::libc::fpathconf), + "_getitimer" => Function::new_typed_with_env(&mut store, env, crate::libc::getitimer), + "_getpwent" => Function::new_typed_with_env(&mut store, env, crate::libc::getpwent), + "_killpg" => Function::new_typed_with_env(&mut store, env, crate::libc::killpg), + "_pathconf" => Function::new_typed_with_env(&mut store, env, crate::libc::pathconf), + "_siginterrupt" => Function::new_typed_with_env(&mut store, env, crate::signal::_siginterrupt), + "_setpwent" => Function::new_typed_with_env(&mut store, env, crate::libc::setpwent), + "_sigismember" => Function::new_typed_with_env(&mut store, env, crate::libc::sigismember), + "_sigpending" => Function::new_typed_with_env(&mut store, env, crate::libc::sigpending), + "___libc_current_sigrtmax" => Function::new_typed_with_env(&mut store, env, crate::libc::current_sigrtmax), + "___libc_current_sigrtmin" => Function::new_typed_with_env(&mut store, env, crate::libc::current_sigrtmin), // Linking - "_dlclose" => Function::new_native(&mut store, ctx, crate::linking::_dlclose), - "_dlerror" => Function::new_native(&mut store, ctx, crate::linking::_dlerror), - "_dlopen" => Function::new_native(&mut store, ctx, crate::linking::_dlopen), - "_dlsym" => Function::new_native(&mut store, ctx, crate::linking::_dlsym), + "_dlclose" => Function::new_typed_with_env(&mut store, env, crate::linking::_dlclose), + "_dlerror" => Function::new_typed_with_env(&mut store, env, crate::linking::_dlerror), + "_dlopen" => Function::new_typed_with_env(&mut store, env, crate::linking::_dlopen), + "_dlsym" => Function::new_typed_with_env(&mut store, env, crate::linking::_dlsym), // wasm32-unknown-emscripten - "_alarm" => Function::new_native(&mut store, ctx, crate::emscripten_target::_alarm), - "_atexit" => Function::new_native(&mut store, ctx, crate::emscripten_target::_atexit), - "setTempRet0" => Function::new_native(&mut store, ctx, crate::emscripten_target::setTempRet0), - "getTempRet0" => Function::new_native(&mut store, ctx, crate::emscripten_target::getTempRet0), - "invoke_i" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_i), - "invoke_ii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_ii), - "invoke_iii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iii), - "invoke_iiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iiii), - "invoke_iifi" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iifi), - "invoke_v" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_v), - "invoke_vi" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_vi), - "invoke_vj" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_vj), - "invoke_vjji" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_vjji), - "invoke_vii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_vii), - "invoke_viii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viii), - "invoke_viiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiii), - "__Unwind_Backtrace" => Function::new_native(&mut store, ctx, crate::emscripten_target::__Unwind_Backtrace), - "__Unwind_FindEnclosingFunction" => Function::new_native(&mut store, ctx, crate::emscripten_target::__Unwind_FindEnclosingFunction), - "__Unwind_GetIPInfo" => Function::new_native(&mut store, ctx, crate::emscripten_target::__Unwind_GetIPInfo), - "___cxa_find_matching_catch_2" => Function::new_native(&mut store, ctx, crate::emscripten_target::___cxa_find_matching_catch_2), - "___cxa_find_matching_catch_3" => Function::new_native(&mut store, ctx, crate::emscripten_target::___cxa_find_matching_catch_3), - "___cxa_free_exception" => Function::new_native(&mut store, ctx, crate::emscripten_target::___cxa_free_exception), - "___resumeException" => Function::new_native(&mut store, ctx, crate::emscripten_target::___resumeException), - "_dladdr" => Function::new_native(&mut store, ctx, crate::emscripten_target::_dladdr), - "_pthread_attr_destroy" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_attr_destroy), - "_pthread_attr_getstack" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_attr_getstack), - "_pthread_attr_init" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_attr_init), - "_pthread_attr_setstacksize" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_attr_setstacksize), - "_pthread_cleanup_pop" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_cleanup_pop), - "_pthread_cleanup_push" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_cleanup_push), - "_pthread_cond_destroy" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_cond_destroy), - "_pthread_cond_init" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_cond_init), - "_pthread_cond_signal" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_cond_signal), - "_pthread_cond_timedwait" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_cond_timedwait), - "_pthread_cond_wait" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_cond_wait), - "_pthread_condattr_destroy" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_condattr_destroy), - "_pthread_condattr_init" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_condattr_init), - "_pthread_condattr_setclock" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_condattr_setclock), - "_pthread_create" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_create), - "_pthread_detach" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_detach), - "_pthread_equal" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_equal), - "_pthread_exit" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_exit), - "_pthread_self" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_self), - "_pthread_getattr_np" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_getattr_np), - "_pthread_getspecific" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_getspecific), - "_pthread_join" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_join), - "_pthread_key_create" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_key_create), - "_pthread_mutex_destroy" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_mutex_destroy), - "_pthread_mutex_init" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_mutex_init), - "_pthread_mutexattr_destroy" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_mutexattr_destroy), - "_pthread_mutexattr_init" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_mutexattr_init), - "_pthread_mutexattr_settype" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_mutexattr_settype), - "_pthread_once" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_once), - "_pthread_rwlock_destroy" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_rwlock_destroy), - "_pthread_rwlock_init" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_rwlock_init), - "_pthread_rwlock_rdlock" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_rwlock_rdlock), - "_pthread_rwlock_unlock" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_rwlock_unlock), - "_pthread_rwlock_wrlock" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_rwlock_wrlock), - "_pthread_setcancelstate" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_setcancelstate), - "_pthread_setspecific" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_setspecific), - "_pthread_sigmask" => Function::new_native(&mut store, ctx, crate::pthread::_pthread_sigmask), - "___gxx_personality_v0" => Function::new_native(&mut store, ctx, crate::emscripten_target::___gxx_personality_v0), - "_gai_strerror" => Function::new_native(&mut store, ctx, crate::env::_gai_strerror), - "_getdtablesize" => Function::new_native(&mut store, ctx, crate::emscripten_target::_getdtablesize), - "_gethostbyaddr" => Function::new_native(&mut store, ctx, crate::emscripten_target::_gethostbyaddr), - "_gethostbyname" => Function::new_native(&mut store, ctx, crate::emscripten_target::_gethostbyname), - "_gethostbyname_r" => Function::new_native(&mut store, ctx, crate::emscripten_target::_gethostbyname_r), - "_getloadavg" => Function::new_native(&mut store, ctx, crate::emscripten_target::_getloadavg), - "_getnameinfo" => Function::new_native(&mut store, ctx, crate::emscripten_target::_getnameinfo), - "invoke_dii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_dii), - "invoke_diiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_diiii), - "invoke_iiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iiiii), - "invoke_iiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iiiiii), - "invoke_iiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iiiiiii), - "invoke_iiiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iiiiiiii), - "invoke_iiiiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iiiiiiiii), - "invoke_iiiiiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iiiiiiiiii), - "invoke_iiiiiiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iiiiiiiiiii), - "invoke_vd" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_vd), - "invoke_viiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiiii), - "invoke_viiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiiiii), - "invoke_viiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiiiiii), - "invoke_viiiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiiiiiii), - "invoke_viiiiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiiiiiiii), - "invoke_viiiiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiiiiiiii), - "invoke_viiiiiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiiiiiiiii), - "invoke_iij" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iij), - "invoke_iji" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iji), - "invoke_iiji" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iiji), - "invoke_iiijj" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_iiijj), - "invoke_j" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_j), - "invoke_ji" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_ji), - "invoke_jii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_jii), - "invoke_jij" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_jij), - "invoke_jjj" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_jjj), - "invoke_viiij" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiij), - "invoke_viiijiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiijiiii), - "invoke_viiijiiiiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiijiiiiii), - "invoke_viij" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viij), - "invoke_viiji" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viiji), - "invoke_viijiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viijiii), - "invoke_viijj" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viijj), - "invoke_vij" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_vij), - "invoke_viji" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viji), - "invoke_vijiii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_vijiii), - "invoke_vijj" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_vijj), - "invoke_vidd" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_vidd), - "invoke_viid" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viid), - "invoke_viidii" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viidii), - "invoke_viidddddddd" => Function::new_native(&mut store, ctx, crate::emscripten_target::invoke_viidddddddd), + "_alarm" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_alarm), + "_atexit" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_atexit), + "setTempRet0" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::setTempRet0), + "getTempRet0" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::getTempRet0), + "invoke_i" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_i), + "invoke_ii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_ii), + "invoke_iii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iii), + "invoke_iiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiii), + "invoke_iifi" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iifi), + "invoke_v" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_v), + "invoke_vi" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vi), + "invoke_vj" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vj), + "invoke_vjji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vjji), + "invoke_vii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vii), + "invoke_viii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viii), + "invoke_viiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiii), + "__Unwind_Backtrace" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::__Unwind_Backtrace), + "__Unwind_FindEnclosingFunction" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::__Unwind_FindEnclosingFunction), + "__Unwind_GetIPInfo" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::__Unwind_GetIPInfo), + "___cxa_find_matching_catch_2" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::___cxa_find_matching_catch_2), + "___cxa_find_matching_catch_3" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::___cxa_find_matching_catch_3), + "___cxa_free_exception" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::___cxa_free_exception), + "___resumeException" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::___resumeException), + "_dladdr" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_dladdr), + "_pthread_attr_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_attr_destroy), + "_pthread_attr_getstack" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_attr_getstack), + "_pthread_attr_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_attr_init), + "_pthread_attr_setstacksize" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_attr_setstacksize), + "_pthread_cleanup_pop" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cleanup_pop), + "_pthread_cleanup_push" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cleanup_push), + "_pthread_cond_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cond_destroy), + "_pthread_cond_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cond_init), + "_pthread_cond_signal" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cond_signal), + "_pthread_cond_timedwait" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cond_timedwait), + "_pthread_cond_wait" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_cond_wait), + "_pthread_condattr_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_condattr_destroy), + "_pthread_condattr_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_condattr_init), + "_pthread_condattr_setclock" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_condattr_setclock), + "_pthread_create" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_create), + "_pthread_detach" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_detach), + "_pthread_equal" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_equal), + "_pthread_exit" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_exit), + "_pthread_self" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_self), + "_pthread_getattr_np" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_getattr_np), + "_pthread_getspecific" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_getspecific), + "_pthread_join" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_join), + "_pthread_key_create" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_key_create), + "_pthread_mutex_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_mutex_destroy), + "_pthread_mutex_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_mutex_init), + "_pthread_mutexattr_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_mutexattr_destroy), + "_pthread_mutexattr_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_mutexattr_init), + "_pthread_mutexattr_settype" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_mutexattr_settype), + "_pthread_once" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_once), + "_pthread_rwlock_destroy" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_rwlock_destroy), + "_pthread_rwlock_init" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_rwlock_init), + "_pthread_rwlock_rdlock" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_rwlock_rdlock), + "_pthread_rwlock_unlock" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_rwlock_unlock), + "_pthread_rwlock_wrlock" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_rwlock_wrlock), + "_pthread_setcancelstate" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_setcancelstate), + "_pthread_setspecific" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_setspecific), + "_pthread_sigmask" => Function::new_typed_with_env(&mut store, env, crate::pthread::_pthread_sigmask), + "___gxx_personality_v0" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::___gxx_personality_v0), + "_gai_strerror" => Function::new_typed_with_env(&mut store, env, crate::env::_gai_strerror), + "_getdtablesize" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_getdtablesize), + "_gethostbyaddr" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_gethostbyaddr), + "_gethostbyname" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_gethostbyname), + "_gethostbyname_r" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_gethostbyname_r), + "_getloadavg" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_getloadavg), + "_getnameinfo" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::_getnameinfo), + "invoke_dii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_dii), + "invoke_diiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_diiii), + "invoke_iiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiii), + "invoke_iiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiii), + "invoke_iiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiiii), + "invoke_iiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiiiii), + "invoke_iiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiiiiii), + "invoke_iiiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiiiiiii), + "invoke_iiiiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiiiiiiiiii), + "invoke_vd" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vd), + "invoke_viiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiii), + "invoke_viiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiii), + "invoke_viiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiiii), + "invoke_viiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiiiii), + "invoke_viiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiiiiii), + "invoke_viiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiiiiii), + "invoke_viiiiiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiiiiiiiii), + "invoke_iij" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iij), + "invoke_iji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iji), + "invoke_iiji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiji), + "invoke_iiijj" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_iiijj), + "invoke_j" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_j), + "invoke_ji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_ji), + "invoke_jii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_jii), + "invoke_jij" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_jij), + "invoke_jjj" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_jjj), + "invoke_viiij" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiij), + "invoke_viiijiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiijiiii), + "invoke_viiijiiiiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiijiiiiii), + "invoke_viij" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viij), + "invoke_viiji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viiji), + "invoke_viijiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viijiii), + "invoke_viijj" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viijj), + "invoke_vij" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vij), + "invoke_viji" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viji), + "invoke_vijiii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vijiii), + "invoke_vijj" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vijj), + "invoke_vidd" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_vidd), + "invoke_viid" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viid), + "invoke_viidii" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viidii), + "invoke_viidddddddd" => Function::new_typed_with_env(&mut store, env, crate::emscripten_target::invoke_viidddddddd), // ucontext - "_getcontext" => Function::new_native(&mut store, ctx, crate::ucontext::_getcontext), - "_makecontext" => Function::new_native(&mut store, ctx, crate::ucontext::_makecontext), - "_setcontext" => Function::new_native(&mut store, ctx, crate::ucontext::_setcontext), - "_swapcontext" => Function::new_native(&mut store, ctx, crate::ucontext::_swapcontext), + "_getcontext" => Function::new_typed_with_env(&mut store, env, crate::ucontext::_getcontext), + "_makecontext" => Function::new_typed_with_env(&mut store, env, crate::ucontext::_makecontext), + "_setcontext" => Function::new_typed_with_env(&mut store, env, crate::ucontext::_setcontext), + "_swapcontext" => Function::new_typed_with_env(&mut store, env, crate::ucontext::_swapcontext), // unistd - "_confstr" => Function::new_native(&mut store, ctx, crate::unistd::confstr), + "_confstr" => Function::new_typed_with_env(&mut store, env, crate::unistd::confstr), }; // Compatibility with newer versions of Emscripten @@ -1473,7 +1473,7 @@ pub fn generate_emscripten_env( for null_function_name in globals.null_function_names.iter() { env_ns.insert( null_function_name.as_str(), - Function::new_native(&mut store, ctx, nullfunc), + Function::new_typed_with_env(&mut store, env, nullfunc), ); } @@ -1484,24 +1484,24 @@ pub fn generate_emscripten_env( "Infinity" => Global::new(&mut store, Value::F64(f64::INFINITY)), }, "global.Math" => { - "pow" => Function::new_native(&mut store, ctx, crate::math::pow), - "exp" => Function::new_native(&mut store, ctx, crate::math::exp), - "log" => Function::new_native(&mut store, ctx, crate::math::log), + "pow" => Function::new_typed_with_env(&mut store, env, crate::math::pow), + "exp" => Function::new_typed_with_env(&mut store, env, crate::math::exp), + "log" => Function::new_typed_with_env(&mut store, env, crate::math::log), }, "asm2wasm" => { - "f64-rem" => Function::new_native(&mut store, ctx, crate::math::f64_rem), - "f64-to-int" => Function::new_native(&mut store, ctx, crate::math::f64_to_int), + "f64-rem" => Function::new_typed_with_env(&mut store, env, crate::math::f64_rem), + "f64-to-int" => Function::new_typed_with_env(&mut store, env, crate::math::f64_to_int), }, }; import_object } -pub fn nullfunc(ctx: FunctionEnvMut, _x: u32) { +pub fn nullfunc(env: FunctionEnvMut, _x: u32) { use crate::process::abort_with_message; debug!("emscripten::nullfunc_i {}", _x); abort_with_message( - ctx, + env, "Invalid function pointer. Perhaps this is an invalid value \ (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an \ incorrect type, which will fail? (it is worth building your source files with -Werror (\ diff --git a/lib/middlewares/src/metering.rs b/lib/middlewares/src/metering.rs index 3e786777f87..eea3efb149d 100644 --- a/lib/middlewares/src/metering.rs +++ b/lib/middlewares/src/metering.rs @@ -406,7 +406,7 @@ mod tests { .exports .get_function("add_one") .unwrap() - .native(&store) + .typed(&store) .unwrap(); add_one.call(&mut store, 1).unwrap(); assert_eq!( @@ -447,7 +447,7 @@ mod tests { .exports .get_function("add_one") .unwrap() - .native(&store) + .typed(&store) .unwrap(); // Increase a bit to have enough for 3 calls diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index 2fa44e0f887..ea291537888 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -381,129 +381,129 @@ impl WasiEnv { /// Create an [`Imports`] from a [`Context`] pub fn generate_import_object_from_env( store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, version: WasiVersion, ) -> Imports { match version { - WasiVersion::Snapshot0 => generate_import_object_snapshot0(store, ctx), + WasiVersion::Snapshot0 => generate_import_object_snapshot0(store, env), WasiVersion::Snapshot1 | WasiVersion::Latest => { - generate_import_object_snapshot1(store, ctx) + generate_import_object_snapshot1(store, env) } - WasiVersion::Wasix32v1 => generate_import_object_wasix32_v1(store, ctx), - WasiVersion::Wasix64v1 => generate_import_object_wasix64_v1(store, ctx), + WasiVersion::Wasix32v1 => generate_import_object_wasix32_v1(store, env), + WasiVersion::Wasix64v1 => generate_import_object_wasix64_v1(store, env), } } -fn wasi_unstable_exports(mut store: &mut impl AsStoreMut, ctx: &FunctionEnv) -> Exports { +fn wasi_unstable_exports(mut store: &mut impl AsStoreMut, env: &FunctionEnv) -> Exports { let namespace = namespace! { - "args_get" => Function::new_native(&mut store, ctx, args_get::), - "args_sizes_get" => Function::new_native(&mut store, ctx, args_sizes_get::), - "clock_res_get" => Function::new_native(&mut store, ctx, clock_res_get::), - "clock_time_get" => Function::new_native(&mut store, ctx, clock_time_get::), - "environ_get" => Function::new_native(&mut store, ctx, environ_get::), - "environ_sizes_get" => Function::new_native(&mut store, ctx, environ_sizes_get::), - "fd_advise" => Function::new_native(&mut store, ctx, fd_advise), - "fd_allocate" => Function::new_native(&mut store, ctx, fd_allocate), - "fd_close" => Function::new_native(&mut store, ctx, fd_close), - "fd_datasync" => Function::new_native(&mut store, ctx, fd_datasync), - "fd_fdstat_get" => Function::new_native(&mut store, ctx, fd_fdstat_get::), - "fd_fdstat_set_flags" => Function::new_native(&mut store, ctx, fd_fdstat_set_flags), - "fd_fdstat_set_rights" => Function::new_native(&mut store, ctx, fd_fdstat_set_rights), - "fd_filestat_get" => Function::new_native(&mut store, ctx, legacy::snapshot0::fd_filestat_get), - "fd_filestat_set_size" => Function::new_native(&mut store, ctx, fd_filestat_set_size), - "fd_filestat_set_times" => Function::new_native(&mut store, ctx, fd_filestat_set_times), - "fd_pread" => Function::new_native(&mut store, ctx, fd_pread::), - "fd_prestat_get" => Function::new_native(&mut store, ctx, fd_prestat_get::), - "fd_prestat_dir_name" => Function::new_native(&mut store, ctx, fd_prestat_dir_name::), - "fd_pwrite" => Function::new_native(&mut store, ctx, fd_pwrite::), - "fd_read" => Function::new_native(&mut store, ctx, fd_read::), - "fd_readdir" => Function::new_native(&mut store, ctx, fd_readdir::), - "fd_renumber" => Function::new_native(&mut store, ctx, fd_renumber), - "fd_seek" => Function::new_native(&mut store, ctx, legacy::snapshot0::fd_seek), - "fd_sync" => Function::new_native(&mut store, ctx, fd_sync), - "fd_tell" => Function::new_native(&mut store, ctx, fd_tell::), - "fd_write" => Function::new_native(&mut store, ctx, fd_write::), - "path_create_directory" => Function::new_native(&mut store, ctx, path_create_directory::), - "path_filestat_get" => Function::new_native(&mut store, ctx, legacy::snapshot0::path_filestat_get), - "path_filestat_set_times" => Function::new_native(&mut store, ctx, path_filestat_set_times::), - "path_link" => Function::new_native(&mut store, ctx, path_link::), - "path_open" => Function::new_native(&mut store, ctx, path_open::), - "path_readlink" => Function::new_native(&mut store, ctx, path_readlink::), - "path_remove_directory" => Function::new_native(&mut store, ctx, path_remove_directory::), - "path_rename" => Function::new_native(&mut store, ctx, path_rename::), - "path_symlink" => Function::new_native(&mut store, ctx, path_symlink::), - "path_unlink_file" => Function::new_native(&mut store, ctx, path_unlink_file::), - "poll_oneoff" => Function::new_native(&mut store, ctx, legacy::snapshot0::poll_oneoff), - "proc_exit" => Function::new_native(&mut store, ctx, proc_exit), - "proc_raise" => Function::new_native(&mut store, ctx, proc_raise), - "random_get" => Function::new_native(&mut store, ctx, random_get::), - "sched_yield" => Function::new_native(&mut store, ctx, sched_yield), - "sock_recv" => Function::new_native(&mut store, ctx, sock_recv::), - "sock_send" => Function::new_native(&mut store, ctx, sock_send::), - "sock_shutdown" => Function::new_native(&mut store, ctx, sock_shutdown), + "args_get" => Function::new_typed_with_env(&mut store, env, args_get::), + "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::), + "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::), + "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::), + "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::), + "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::), + "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise), + "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate), + "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close), + "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync), + "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::), + "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags), + "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights), + "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::fd_filestat_get), + "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size), + "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times), + "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::), + "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::), + "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::), + "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::), + "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::), + "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::), + "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber), + "fd_seek" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::fd_seek), + "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync), + "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::), + "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::), + "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::), + "path_filestat_get" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::path_filestat_get), + "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::), + "path_link" => Function::new_typed_with_env(&mut store, env, path_link::), + "path_open" => Function::new_typed_with_env(&mut store, env, path_open::), + "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::), + "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::), + "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::), + "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::), + "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::), + "poll_oneoff" => Function::new_typed_with_env(&mut store, env, legacy::snapshot0::poll_oneoff), + "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit), + "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise), + "random_get" => Function::new_typed_with_env(&mut store, env, random_get::), + "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield), + "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::), + "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::), + "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown), }; namespace } fn wasi_snapshot_preview1_exports( mut store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, ) -> Exports { let namespace = namespace! { - "args_get" => Function::new_native(&mut store, ctx, args_get::), - "args_sizes_get" => Function::new_native(&mut store, ctx, args_sizes_get::), - "clock_res_get" => Function::new_native(&mut store, ctx, clock_res_get::), - "clock_time_get" => Function::new_native(&mut store, ctx, clock_time_get::), - "environ_get" => Function::new_native(&mut store, ctx, environ_get::), - "environ_sizes_get" => Function::new_native(&mut store, ctx, environ_sizes_get::), - "fd_advise" => Function::new_native(&mut store, ctx, fd_advise), - "fd_allocate" => Function::new_native(&mut store, ctx, fd_allocate), - "fd_close" => Function::new_native(&mut store, ctx, fd_close), - "fd_datasync" => Function::new_native(&mut store, ctx, fd_datasync), - "fd_fdstat_get" => Function::new_native(&mut store, ctx, fd_fdstat_get::), - "fd_fdstat_set_flags" => Function::new_native(&mut store, ctx, fd_fdstat_set_flags), - "fd_fdstat_set_rights" => Function::new_native(&mut store, ctx, fd_fdstat_set_rights), - "fd_filestat_get" => Function::new_native(&mut store, ctx, fd_filestat_get::), - "fd_filestat_set_size" => Function::new_native(&mut store, ctx, fd_filestat_set_size), - "fd_filestat_set_times" => Function::new_native(&mut store, ctx, fd_filestat_set_times), - "fd_pread" => Function::new_native(&mut store, ctx, fd_pread::), - "fd_prestat_get" => Function::new_native(&mut store, ctx, fd_prestat_get::), - "fd_prestat_dir_name" => Function::new_native(&mut store, ctx, fd_prestat_dir_name::), - "fd_pwrite" => Function::new_native(&mut store, ctx, fd_pwrite::), - "fd_read" => Function::new_native(&mut store, ctx, fd_read::), - "fd_readdir" => Function::new_native(&mut store, ctx, fd_readdir::), - "fd_renumber" => Function::new_native(&mut store, ctx, fd_renumber), - "fd_seek" => Function::new_native(&mut store, ctx, fd_seek::), - "fd_sync" => Function::new_native(&mut store, ctx, fd_sync), - "fd_tell" => Function::new_native(&mut store, ctx, fd_tell::), - "fd_write" => Function::new_native(&mut store, ctx, fd_write::), - "path_create_directory" => Function::new_native(&mut store, ctx, path_create_directory::), - "path_filestat_get" => Function::new_native(&mut store, ctx, path_filestat_get::), - "path_filestat_set_times" => Function::new_native(&mut store, ctx, path_filestat_set_times::), - "path_link" => Function::new_native(&mut store, ctx, path_link::), - "path_open" => Function::new_native(&mut store, ctx, path_open::), - "path_readlink" => Function::new_native(&mut store, ctx, path_readlink::), - "path_remove_directory" => Function::new_native(&mut store, ctx, path_remove_directory::), - "path_rename" => Function::new_native(&mut store, ctx, path_rename::), - "path_symlink" => Function::new_native(&mut store, ctx, path_symlink::), - "path_unlink_file" => Function::new_native(&mut store, ctx, path_unlink_file::), - "poll_oneoff" => Function::new_native(&mut store, ctx, poll_oneoff::), - "proc_exit" => Function::new_native(&mut store, ctx, proc_exit), - "proc_raise" => Function::new_native(&mut store, ctx, proc_raise), - "random_get" => Function::new_native(&mut store, ctx, random_get::), - "sched_yield" => Function::new_native(&mut store, ctx, sched_yield), - "sock_recv" => Function::new_native(&mut store, ctx, sock_recv::), - "sock_send" => Function::new_native(&mut store, ctx, sock_send::), - "sock_shutdown" => Function::new_native(&mut store, ctx, sock_shutdown), + "args_get" => Function::new_typed_with_env(&mut store, env, args_get::), + "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get::), + "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get::), + "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get::), + "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get::), + "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get::), + "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise), + "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate), + "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close), + "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync), + "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get::), + "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags), + "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights), + "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get::), + "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size), + "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times), + "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread::), + "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get::), + "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name::), + "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite::), + "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read::), + "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir::), + "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber), + "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek::), + "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync), + "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell::), + "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write::), + "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory::), + "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get::), + "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times::), + "path_link" => Function::new_typed_with_env(&mut store, env, path_link::), + "path_open" => Function::new_typed_with_env(&mut store, env, path_open::), + "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink::), + "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory::), + "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename::), + "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink::), + "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file::), + "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff::), + "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit), + "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise), + "random_get" => Function::new_typed_with_env(&mut store, env, random_get::), + "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield), + "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::), + "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::), + "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown), }; namespace } pub fn import_object_for_all_wasi_versions( store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, ) -> Imports { - let wasi_unstable_exports = wasi_unstable_exports(store, ctx); - let wasi_snapshot_preview1_exports = wasi_snapshot_preview1_exports(store, ctx); + let wasi_unstable_exports = wasi_unstable_exports(store, env); + let wasi_snapshot_preview1_exports = wasi_snapshot_preview1_exports(store, env); imports! { "wasi_unstable" => wasi_unstable_exports, "wasi_snapshot_preview1" => wasi_snapshot_preview1_exports, @@ -513,9 +513,9 @@ pub fn import_object_for_all_wasi_versions( /// Combines a state generating function with the import list for legacy WASI fn generate_import_object_snapshot0( store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, ) -> Imports { - let wasi_unstable_exports = wasi_unstable_exports(store, ctx); + let wasi_unstable_exports = wasi_unstable_exports(store, env); imports! { "wasi_unstable" => wasi_unstable_exports } @@ -523,9 +523,9 @@ fn generate_import_object_snapshot0( fn generate_import_object_snapshot1( store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, ) -> Imports { - let wasi_snapshot_preview1_exports = wasi_snapshot_preview1_exports(store, ctx); + let wasi_snapshot_preview1_exports = wasi_snapshot_preview1_exports(store, env); imports! { "wasi_snapshot_preview1" => wasi_snapshot_preview1_exports } @@ -534,236 +534,236 @@ fn generate_import_object_snapshot1( /// Combines a state generating function with the import list for snapshot 1 fn generate_import_object_wasix32_v1( mut store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, ) -> Imports { use self::wasix32::*; imports! { "wasix_32v1" => { - "args_get" => Function::new_native(&mut store, ctx, args_get), - "args_sizes_get" => Function::new_native(&mut store, ctx, args_sizes_get), - "clock_res_get" => Function::new_native(&mut store, ctx, clock_res_get), - "clock_time_get" => Function::new_native(&mut store, ctx, clock_time_get), - "environ_get" => Function::new_native(&mut store, ctx, environ_get), - "environ_sizes_get" => Function::new_native(&mut store, ctx, environ_sizes_get), - "fd_advise" => Function::new_native(&mut store, ctx, fd_advise), - "fd_allocate" => Function::new_native(&mut store, ctx, fd_allocate), - "fd_close" => Function::new_native(&mut store, ctx, fd_close), - "fd_datasync" => Function::new_native(&mut store, ctx, fd_datasync), - "fd_fdstat_get" => Function::new_native(&mut store, ctx, fd_fdstat_get), - "fd_fdstat_set_flags" => Function::new_native(&mut store, ctx, fd_fdstat_set_flags), - "fd_fdstat_set_rights" => Function::new_native(&mut store, ctx, fd_fdstat_set_rights), - "fd_filestat_get" => Function::new_native(&mut store, ctx, fd_filestat_get), - "fd_filestat_set_size" => Function::new_native(&mut store, ctx, fd_filestat_set_size), - "fd_filestat_set_times" => Function::new_native(&mut store, ctx, fd_filestat_set_times), - "fd_pread" => Function::new_native(&mut store, ctx, fd_pread), - "fd_prestat_get" => Function::new_native(&mut store, ctx, fd_prestat_get), - "fd_prestat_dir_name" => Function::new_native(&mut store, ctx, fd_prestat_dir_name), - "fd_pwrite" => Function::new_native(&mut store, ctx, fd_pwrite), - "fd_read" => Function::new_native(&mut store, ctx, fd_read), - "fd_readdir" => Function::new_native(&mut store, ctx, fd_readdir), - "fd_renumber" => Function::new_native(&mut store, ctx, fd_renumber), - "fd_dup" => Function::new_native(&mut store, ctx, fd_dup), - "fd_event" => Function::new_native(&mut store, ctx, fd_event), - "fd_seek" => Function::new_native(&mut store, ctx, fd_seek), - "fd_sync" => Function::new_native(&mut store, ctx, fd_sync), - "fd_tell" => Function::new_native(&mut store, ctx, fd_tell), - "fd_write" => Function::new_native(&mut store, ctx, fd_write), - "fd_pipe" => Function::new_native(&mut store, ctx, fd_pipe), - "path_create_directory" => Function::new_native(&mut store, ctx, path_create_directory), - "path_filestat_get" => Function::new_native(&mut store, ctx, path_filestat_get), - "path_filestat_set_times" => Function::new_native(&mut store, ctx, path_filestat_set_times), - "path_link" => Function::new_native(&mut store, ctx, path_link), - "path_open" => Function::new_native(&mut store, ctx, path_open), - "path_readlink" => Function::new_native(&mut store, ctx, path_readlink), - "path_remove_directory" => Function::new_native(&mut store, ctx, path_remove_directory), - "path_rename" => Function::new_native(&mut store, ctx, path_rename), - "path_symlink" => Function::new_native(&mut store, ctx, path_symlink), - "path_unlink_file" => Function::new_native(&mut store, ctx, path_unlink_file), - "poll_oneoff" => Function::new_native(&mut store, ctx, poll_oneoff), - "proc_exit" => Function::new_native(&mut store, ctx, proc_exit), - "proc_raise" => Function::new_native(&mut store, ctx, proc_raise), - "random_get" => Function::new_native(&mut store, ctx, random_get), - "tty_get" => Function::new_native(&mut store, ctx, tty_get), - "tty_set" => Function::new_native(&mut store, ctx, tty_set), - "getcwd" => Function::new_native(&mut store, ctx, getcwd), - "chdir" => Function::new_native(&mut store, ctx, chdir), - "thread_spawn" => Function::new_native(&mut store, ctx, thread_spawn), - "thread_sleep" => Function::new_native(&mut store, ctx, thread_sleep), - "thread_id" => Function::new_native(&mut store, ctx, thread_id), - "thread_join" => Function::new_native(&mut store, ctx, thread_join), - "thread_parallelism" => Function::new_native(&mut store, ctx, thread_parallelism), - "thread_exit" => Function::new_native(&mut store, ctx, thread_exit), - "sched_yield" => Function::new_native(&mut store, ctx, sched_yield), - "getpid" => Function::new_native(&mut store, ctx, getpid), - "process_spawn" => Function::new_native(&mut store, ctx, process_spawn), - "bus_open_local" => Function::new_native(&mut store, ctx, bus_open_local), - "bus_open_remote" => Function::new_native(&mut store, ctx, bus_open_remote), - "bus_close" => Function::new_native(&mut store, ctx, bus_close), - "bus_call" => Function::new_native(&mut store, ctx, bus_call), - "bus_subcall" => Function::new_native(&mut store, ctx, bus_subcall), - "bus_poll" => Function::new_native(&mut store, ctx, bus_poll), - "call_reply" => Function::new_native(&mut store, ctx, call_reply), - "call_fault" => Function::new_native(&mut store, ctx, call_fault), - "call_close" => Function::new_native(&mut store, ctx, call_close), - "ws_connect" => Function::new_native(&mut store, ctx, ws_connect), - "http_request" => Function::new_native(&mut store, ctx, http_request), - "http_status" => Function::new_native(&mut store, ctx, http_status), - "port_bridge" => Function::new_native(&mut store, ctx, port_bridge), - "port_unbridge" => Function::new_native(&mut store, ctx, port_unbridge), - "port_dhcp_acquire" => Function::new_native(&mut store, ctx, port_dhcp_acquire), - "port_addr_add" => Function::new_native(&mut store, ctx, port_addr_add), - "port_addr_remove" => Function::new_native(&mut store, ctx, port_addr_remove), - "port_addr_clear" => Function::new_native(&mut store, ctx, port_addr_clear), - "port_addr_list" => Function::new_native(&mut store, ctx, port_addr_list), - "port_mac" => Function::new_native(&mut store, ctx, port_mac), - "port_gateway_set" => Function::new_native(&mut store, ctx, port_gateway_set), - "port_route_add" => Function::new_native(&mut store, ctx, port_route_add), - "port_route_remove" => Function::new_native(&mut store, ctx, port_route_remove), - "port_route_clear" => Function::new_native(&mut store, ctx, port_route_clear), - "port_route_list" => Function::new_native(&mut store, ctx, port_route_list), - "sock_status" => Function::new_native(&mut store, ctx, sock_status), - "sock_addr_local" => Function::new_native(&mut store, ctx, sock_addr_local), - "sock_addr_peer" => Function::new_native(&mut store, ctx, sock_addr_peer), - "sock_open" => Function::new_native(&mut store, ctx, sock_open), - "sock_set_opt_flag" => Function::new_native(&mut store, ctx, sock_set_opt_flag), - "sock_get_opt_flag" => Function::new_native(&mut store, ctx, sock_get_opt_flag), - "sock_set_opt_time" => Function::new_native(&mut store, ctx, sock_set_opt_time), - "sock_get_opt_time" => Function::new_native(&mut store, ctx, sock_get_opt_time), - "sock_set_opt_size" => Function::new_native(&mut store, ctx, sock_set_opt_size), - "sock_get_opt_size" => Function::new_native(&mut store, ctx, sock_get_opt_size), - "sock_join_multicast_v4" => Function::new_native(&mut store, ctx, sock_join_multicast_v4), - "sock_leave_multicast_v4" => Function::new_native(&mut store, ctx, sock_leave_multicast_v4), - "sock_join_multicast_v6" => Function::new_native(&mut store, ctx, sock_join_multicast_v6), - "sock_leave_multicast_v6" => Function::new_native(&mut store, ctx, sock_leave_multicast_v6), - "sock_bind" => Function::new_native(&mut store, ctx, sock_bind), - "sock_listen" => Function::new_native(&mut store, ctx, sock_listen), - "sock_accept" => Function::new_native(&mut store, ctx, sock_accept), - "sock_connect" => Function::new_native(&mut store, ctx, sock_connect), - "sock_recv" => Function::new_native(&mut store, ctx, sock_recv), - "sock_recv_from" => Function::new_native(&mut store, ctx, sock_recv_from), - "sock_send" => Function::new_native(&mut store, ctx, sock_send), - "sock_send_to" => Function::new_native(&mut store, ctx, sock_send_to), - "sock_send_file" => Function::new_native(&mut store, ctx, sock_send_file), - "sock_shutdown" => Function::new_native(&mut store, ctx, sock_shutdown), - "resolve" => Function::new_native(&mut store, ctx, resolve), + "args_get" => Function::new_typed_with_env(&mut store, env, args_get), + "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get), + "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get), + "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get), + "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get), + "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get), + "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise), + "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate), + "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close), + "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync), + "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get), + "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags), + "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights), + "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get), + "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size), + "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times), + "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread), + "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get), + "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name), + "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite), + "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read), + "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir), + "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber), + "fd_dup" => Function::new_typed_with_env(&mut store, env, fd_dup), + "fd_event" => Function::new_typed_with_env(&mut store, env, fd_event), + "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek), + "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync), + "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell), + "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write), + "fd_pipe" => Function::new_typed_with_env(&mut store, env, fd_pipe), + "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory), + "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get), + "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times), + "path_link" => Function::new_typed_with_env(&mut store, env, path_link), + "path_open" => Function::new_typed_with_env(&mut store, env, path_open), + "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink), + "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory), + "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename), + "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink), + "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file), + "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff), + "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit), + "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise), + "random_get" => Function::new_typed_with_env(&mut store, env, random_get), + "tty_get" => Function::new_typed_with_env(&mut store, env, tty_get), + "tty_set" => Function::new_typed_with_env(&mut store, env, tty_set), + "getcwd" => Function::new_typed_with_env(&mut store, env, getcwd), + "chdir" => Function::new_typed_with_env(&mut store, env, chdir), + "thread_spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn), + "thread_sleep" => Function::new_typed_with_env(&mut store, env, thread_sleep), + "thread_id" => Function::new_typed_with_env(&mut store, env, thread_id), + "thread_join" => Function::new_typed_with_env(&mut store, env, thread_join), + "thread_parallelism" => Function::new_typed_with_env(&mut store, env, thread_parallelism), + "thread_exit" => Function::new_typed_with_env(&mut store, env, thread_exit), + "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield), + "getpid" => Function::new_typed_with_env(&mut store, env, getpid), + "process_spawn" => Function::new_typed_with_env(&mut store, env, process_spawn), + "bus_open_local" => Function::new_typed_with_env(&mut store, env, bus_open_local), + "bus_open_remote" => Function::new_typed_with_env(&mut store, env, bus_open_remote), + "bus_close" => Function::new_typed_with_env(&mut store, env, bus_close), + "bus_call" => Function::new_typed_with_env(&mut store, env, bus_call), + "bus_subcall" => Function::new_typed_with_env(&mut store, env, bus_subcall), + "bus_poll" => Function::new_typed_with_env(&mut store, env, bus_poll), + "call_reply" => Function::new_typed_with_env(&mut store, env, call_reply), + "call_fault" => Function::new_typed_with_env(&mut store, env, call_fault), + "call_close" => Function::new_typed_with_env(&mut store, env, call_close), + "ws_connect" => Function::new_typed_with_env(&mut store, env, ws_connect), + "http_request" => Function::new_typed_with_env(&mut store, env, http_request), + "http_status" => Function::new_typed_with_env(&mut store, env, http_status), + "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge), + "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge), + "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire), + "port_addr_add" => Function::new_typed_with_env(&mut store, env, port_addr_add), + "port_addr_remove" => Function::new_typed_with_env(&mut store, env, port_addr_remove), + "port_addr_clear" => Function::new_typed_with_env(&mut store, env, port_addr_clear), + "port_addr_list" => Function::new_typed_with_env(&mut store, env, port_addr_list), + "port_mac" => Function::new_typed_with_env(&mut store, env, port_mac), + "port_gateway_set" => Function::new_typed_with_env(&mut store, env, port_gateway_set), + "port_route_add" => Function::new_typed_with_env(&mut store, env, port_route_add), + "port_route_remove" => Function::new_typed_with_env(&mut store, env, port_route_remove), + "port_route_clear" => Function::new_typed_with_env(&mut store, env, port_route_clear), + "port_route_list" => Function::new_typed_with_env(&mut store, env, port_route_list), + "sock_status" => Function::new_typed_with_env(&mut store, env, sock_status), + "sock_addr_local" => Function::new_typed_with_env(&mut store, env, sock_addr_local), + "sock_addr_peer" => Function::new_typed_with_env(&mut store, env, sock_addr_peer), + "sock_open" => Function::new_typed_with_env(&mut store, env, sock_open), + "sock_set_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_set_opt_flag), + "sock_get_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_get_opt_flag), + "sock_set_opt_time" => Function::new_typed_with_env(&mut store, env, sock_set_opt_time), + "sock_get_opt_time" => Function::new_typed_with_env(&mut store, env, sock_get_opt_time), + "sock_set_opt_size" => Function::new_typed_with_env(&mut store, env, sock_set_opt_size), + "sock_get_opt_size" => Function::new_typed_with_env(&mut store, env, sock_get_opt_size), + "sock_join_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v4), + "sock_leave_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v4), + "sock_join_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v6), + "sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6), + "sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind), + "sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen), + "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept), + "sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect), + "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv), + "sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from), + "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send), + "sock_send_to" => Function::new_typed_with_env(&mut store, env, sock_send_to), + "sock_send_file" => Function::new_typed_with_env(&mut store, env, sock_send_file), + "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown), + "resolve" => Function::new_typed_with_env(&mut store, env, resolve), } } } fn generate_import_object_wasix64_v1( mut store: &mut impl AsStoreMut, - ctx: &FunctionEnv, + env: &FunctionEnv, ) -> Imports { use self::wasix64::*; imports! { "wasix_64v1" => { - "args_get" => Function::new_native(&mut store, ctx, args_get), - "args_sizes_get" => Function::new_native(&mut store, ctx, args_sizes_get), - "clock_res_get" => Function::new_native(&mut store, ctx, clock_res_get), - "clock_time_get" => Function::new_native(&mut store, ctx, clock_time_get), - "environ_get" => Function::new_native(&mut store, ctx, environ_get), - "environ_sizes_get" => Function::new_native(&mut store, ctx, environ_sizes_get), - "fd_advise" => Function::new_native(&mut store, ctx, fd_advise), - "fd_allocate" => Function::new_native(&mut store, ctx, fd_allocate), - "fd_close" => Function::new_native(&mut store, ctx, fd_close), - "fd_datasync" => Function::new_native(&mut store, ctx, fd_datasync), - "fd_fdstat_get" => Function::new_native(&mut store, ctx, fd_fdstat_get), - "fd_fdstat_set_flags" => Function::new_native(&mut store, ctx, fd_fdstat_set_flags), - "fd_fdstat_set_rights" => Function::new_native(&mut store, ctx, fd_fdstat_set_rights), - "fd_filestat_get" => Function::new_native(&mut store, ctx, fd_filestat_get), - "fd_filestat_set_size" => Function::new_native(&mut store, ctx, fd_filestat_set_size), - "fd_filestat_set_times" => Function::new_native(&mut store, ctx, fd_filestat_set_times), - "fd_pread" => Function::new_native(&mut store, ctx, fd_pread), - "fd_prestat_get" => Function::new_native(&mut store, ctx, fd_prestat_get), - "fd_prestat_dir_name" => Function::new_native(&mut store, ctx, fd_prestat_dir_name), - "fd_pwrite" => Function::new_native(&mut store, ctx, fd_pwrite), - "fd_read" => Function::new_native(&mut store, ctx, fd_read), - "fd_readdir" => Function::new_native(&mut store, ctx, fd_readdir), - "fd_renumber" => Function::new_native(&mut store, ctx, fd_renumber), - "fd_dup" => Function::new_native(&mut store, ctx, fd_dup), - "fd_event" => Function::new_native(&mut store, ctx, fd_event), - "fd_seek" => Function::new_native(&mut store, ctx, fd_seek), - "fd_sync" => Function::new_native(&mut store, ctx, fd_sync), - "fd_tell" => Function::new_native(&mut store, ctx, fd_tell), - "fd_write" => Function::new_native(&mut store, ctx, fd_write), - "fd_pipe" => Function::new_native(&mut store, ctx, fd_pipe), - "path_create_directory" => Function::new_native(&mut store, ctx, path_create_directory), - "path_filestat_get" => Function::new_native(&mut store, ctx, path_filestat_get), - "path_filestat_set_times" => Function::new_native(&mut store, ctx, path_filestat_set_times), - "path_link" => Function::new_native(&mut store, ctx, path_link), - "path_open" => Function::new_native(&mut store, ctx, path_open), - "path_readlink" => Function::new_native(&mut store, ctx, path_readlink), - "path_remove_directory" => Function::new_native(&mut store, ctx, path_remove_directory), - "path_rename" => Function::new_native(&mut store, ctx, path_rename), - "path_symlink" => Function::new_native(&mut store, ctx, path_symlink), - "path_unlink_file" => Function::new_native(&mut store, ctx, path_unlink_file), - "poll_oneoff" => Function::new_native(&mut store, ctx, poll_oneoff), - "proc_exit" => Function::new_native(&mut store, ctx, proc_exit), - "proc_raise" => Function::new_native(&mut store, ctx, proc_raise), - "random_get" => Function::new_native(&mut store, ctx, random_get), - "tty_get" => Function::new_native(&mut store, ctx, tty_get), - "tty_set" => Function::new_native(&mut store, ctx, tty_set), - "getcwd" => Function::new_native(&mut store, ctx, getcwd), - "chdir" => Function::new_native(&mut store, ctx, chdir), - "thread_spawn" => Function::new_native(&mut store, ctx, thread_spawn), - "thread_sleep" => Function::new_native(&mut store, ctx, thread_sleep), - "thread_id" => Function::new_native(&mut store, ctx, thread_id), - "thread_join" => Function::new_native(&mut store, ctx, thread_join), - "thread_parallelism" => Function::new_native(&mut store, ctx, thread_parallelism), - "thread_exit" => Function::new_native(&mut store, ctx, thread_exit), - "sched_yield" => Function::new_native(&mut store, ctx, sched_yield), - "getpid" => Function::new_native(&mut store, ctx, getpid), - "process_spawn" => Function::new_native(&mut store, ctx, process_spawn), - "bus_open_local" => Function::new_native(&mut store, ctx, bus_open_local), - "bus_open_remote" => Function::new_native(&mut store, ctx, bus_open_remote), - "bus_close" => Function::new_native(&mut store, ctx, bus_close), - "bus_call" => Function::new_native(&mut store, ctx, bus_call), - "bus_subcall" => Function::new_native(&mut store, ctx, bus_subcall), - "bus_poll" => Function::new_native(&mut store, ctx, bus_poll), - "call_reply" => Function::new_native(&mut store, ctx, call_reply), - "call_fault" => Function::new_native(&mut store, ctx, call_fault), - "call_close" => Function::new_native(&mut store, ctx, call_close), - "ws_connect" => Function::new_native(&mut store, ctx, ws_connect), - "http_request" => Function::new_native(&mut store, ctx, http_request), - "http_status" => Function::new_native(&mut store, ctx, http_status), - "port_bridge" => Function::new_native(&mut store, ctx, port_bridge), - "port_unbridge" => Function::new_native(&mut store, ctx, port_unbridge), - "port_dhcp_acquire" => Function::new_native(&mut store, ctx, port_dhcp_acquire), - "port_addr_add" => Function::new_native(&mut store, ctx, port_addr_add), - "port_addr_remove" => Function::new_native(&mut store, ctx, port_addr_remove), - "port_addr_clear" => Function::new_native(&mut store, ctx, port_addr_clear), - "port_addr_list" => Function::new_native(&mut store, ctx, port_addr_list), - "port_mac" => Function::new_native(&mut store, ctx, port_mac), - "port_gateway_set" => Function::new_native(&mut store, ctx, port_gateway_set), - "port_route_add" => Function::new_native(&mut store, ctx, port_route_add), - "port_route_remove" => Function::new_native(&mut store, ctx, port_route_remove), - "port_route_clear" => Function::new_native(&mut store, ctx, port_route_clear), - "port_route_list" => Function::new_native(&mut store, ctx, port_route_list), - "sock_status" => Function::new_native(&mut store, ctx, sock_status), - "sock_addr_local" => Function::new_native(&mut store, ctx, sock_addr_local), - "sock_addr_peer" => Function::new_native(&mut store, ctx, sock_addr_peer), - "sock_open" => Function::new_native(&mut store, ctx, sock_open), - "sock_set_opt_flag" => Function::new_native(&mut store, ctx, sock_set_opt_flag), - "sock_get_opt_flag" => Function::new_native(&mut store, ctx, sock_get_opt_flag), - "sock_set_opt_time" => Function::new_native(&mut store, ctx, sock_set_opt_time), - "sock_get_opt_time" => Function::new_native(&mut store, ctx, sock_get_opt_time), - "sock_set_opt_size" => Function::new_native(&mut store, ctx, sock_set_opt_size), - "sock_get_opt_size" => Function::new_native(&mut store, ctx, sock_get_opt_size), - "sock_join_multicast_v4" => Function::new_native(&mut store, ctx, sock_join_multicast_v4), - "sock_leave_multicast_v4" => Function::new_native(&mut store, ctx, sock_leave_multicast_v4), - "sock_join_multicast_v6" => Function::new_native(&mut store, ctx, sock_join_multicast_v6), - "sock_leave_multicast_v6" => Function::new_native(&mut store, ctx, sock_leave_multicast_v6), - "sock_bind" => Function::new_native(&mut store, ctx, sock_bind), - "sock_listen" => Function::new_native(&mut store, ctx, sock_listen), - "sock_accept" => Function::new_native(&mut store, ctx, sock_accept), - "sock_connect" => Function::new_native(&mut store, ctx, sock_connect), - "sock_recv" => Function::new_native(&mut store, ctx, sock_recv), - "sock_recv_from" => Function::new_native(&mut store, ctx, sock_recv_from), - "sock_send" => Function::new_native(&mut store, ctx, sock_send), - "sock_send_to" => Function::new_native(&mut store, ctx, sock_send_to), - "sock_send_file" => Function::new_native(&mut store, ctx, sock_send_file), - "sock_shutdown" => Function::new_native(&mut store, ctx, sock_shutdown), - "resolve" => Function::new_native(&mut store, ctx, resolve), + "args_get" => Function::new_typed_with_env(&mut store, env, args_get), + "args_sizes_get" => Function::new_typed_with_env(&mut store, env, args_sizes_get), + "clock_res_get" => Function::new_typed_with_env(&mut store, env, clock_res_get), + "clock_time_get" => Function::new_typed_with_env(&mut store, env, clock_time_get), + "environ_get" => Function::new_typed_with_env(&mut store, env, environ_get), + "environ_sizes_get" => Function::new_typed_with_env(&mut store, env, environ_sizes_get), + "fd_advise" => Function::new_typed_with_env(&mut store, env, fd_advise), + "fd_allocate" => Function::new_typed_with_env(&mut store, env, fd_allocate), + "fd_close" => Function::new_typed_with_env(&mut store, env, fd_close), + "fd_datasync" => Function::new_typed_with_env(&mut store, env, fd_datasync), + "fd_fdstat_get" => Function::new_typed_with_env(&mut store, env, fd_fdstat_get), + "fd_fdstat_set_flags" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_flags), + "fd_fdstat_set_rights" => Function::new_typed_with_env(&mut store, env, fd_fdstat_set_rights), + "fd_filestat_get" => Function::new_typed_with_env(&mut store, env, fd_filestat_get), + "fd_filestat_set_size" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_size), + "fd_filestat_set_times" => Function::new_typed_with_env(&mut store, env, fd_filestat_set_times), + "fd_pread" => Function::new_typed_with_env(&mut store, env, fd_pread), + "fd_prestat_get" => Function::new_typed_with_env(&mut store, env, fd_prestat_get), + "fd_prestat_dir_name" => Function::new_typed_with_env(&mut store, env, fd_prestat_dir_name), + "fd_pwrite" => Function::new_typed_with_env(&mut store, env, fd_pwrite), + "fd_read" => Function::new_typed_with_env(&mut store, env, fd_read), + "fd_readdir" => Function::new_typed_with_env(&mut store, env, fd_readdir), + "fd_renumber" => Function::new_typed_with_env(&mut store, env, fd_renumber), + "fd_dup" => Function::new_typed_with_env(&mut store, env, fd_dup), + "fd_event" => Function::new_typed_with_env(&mut store, env, fd_event), + "fd_seek" => Function::new_typed_with_env(&mut store, env, fd_seek), + "fd_sync" => Function::new_typed_with_env(&mut store, env, fd_sync), + "fd_tell" => Function::new_typed_with_env(&mut store, env, fd_tell), + "fd_write" => Function::new_typed_with_env(&mut store, env, fd_write), + "fd_pipe" => Function::new_typed_with_env(&mut store, env, fd_pipe), + "path_create_directory" => Function::new_typed_with_env(&mut store, env, path_create_directory), + "path_filestat_get" => Function::new_typed_with_env(&mut store, env, path_filestat_get), + "path_filestat_set_times" => Function::new_typed_with_env(&mut store, env, path_filestat_set_times), + "path_link" => Function::new_typed_with_env(&mut store, env, path_link), + "path_open" => Function::new_typed_with_env(&mut store, env, path_open), + "path_readlink" => Function::new_typed_with_env(&mut store, env, path_readlink), + "path_remove_directory" => Function::new_typed_with_env(&mut store, env, path_remove_directory), + "path_rename" => Function::new_typed_with_env(&mut store, env, path_rename), + "path_symlink" => Function::new_typed_with_env(&mut store, env, path_symlink), + "path_unlink_file" => Function::new_typed_with_env(&mut store, env, path_unlink_file), + "poll_oneoff" => Function::new_typed_with_env(&mut store, env, poll_oneoff), + "proc_exit" => Function::new_typed_with_env(&mut store, env, proc_exit), + "proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise), + "random_get" => Function::new_typed_with_env(&mut store, env, random_get), + "tty_get" => Function::new_typed_with_env(&mut store, env, tty_get), + "tty_set" => Function::new_typed_with_env(&mut store, env, tty_set), + "getcwd" => Function::new_typed_with_env(&mut store, env, getcwd), + "chdir" => Function::new_typed_with_env(&mut store, env, chdir), + "thread_spawn" => Function::new_typed_with_env(&mut store, env, thread_spawn), + "thread_sleep" => Function::new_typed_with_env(&mut store, env, thread_sleep), + "thread_id" => Function::new_typed_with_env(&mut store, env, thread_id), + "thread_join" => Function::new_typed_with_env(&mut store, env, thread_join), + "thread_parallelism" => Function::new_typed_with_env(&mut store, env, thread_parallelism), + "thread_exit" => Function::new_typed_with_env(&mut store, env, thread_exit), + "sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield), + "getpid" => Function::new_typed_with_env(&mut store, env, getpid), + "process_spawn" => Function::new_typed_with_env(&mut store, env, process_spawn), + "bus_open_local" => Function::new_typed_with_env(&mut store, env, bus_open_local), + "bus_open_remote" => Function::new_typed_with_env(&mut store, env, bus_open_remote), + "bus_close" => Function::new_typed_with_env(&mut store, env, bus_close), + "bus_call" => Function::new_typed_with_env(&mut store, env, bus_call), + "bus_subcall" => Function::new_typed_with_env(&mut store, env, bus_subcall), + "bus_poll" => Function::new_typed_with_env(&mut store, env, bus_poll), + "call_reply" => Function::new_typed_with_env(&mut store, env, call_reply), + "call_fault" => Function::new_typed_with_env(&mut store, env, call_fault), + "call_close" => Function::new_typed_with_env(&mut store, env, call_close), + "ws_connect" => Function::new_typed_with_env(&mut store, env, ws_connect), + "http_request" => Function::new_typed_with_env(&mut store, env, http_request), + "http_status" => Function::new_typed_with_env(&mut store, env, http_status), + "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge), + "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge), + "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire), + "port_addr_add" => Function::new_typed_with_env(&mut store, env, port_addr_add), + "port_addr_remove" => Function::new_typed_with_env(&mut store, env, port_addr_remove), + "port_addr_clear" => Function::new_typed_with_env(&mut store, env, port_addr_clear), + "port_addr_list" => Function::new_typed_with_env(&mut store, env, port_addr_list), + "port_mac" => Function::new_typed_with_env(&mut store, env, port_mac), + "port_gateway_set" => Function::new_typed_with_env(&mut store, env, port_gateway_set), + "port_route_add" => Function::new_typed_with_env(&mut store, env, port_route_add), + "port_route_remove" => Function::new_typed_with_env(&mut store, env, port_route_remove), + "port_route_clear" => Function::new_typed_with_env(&mut store, env, port_route_clear), + "port_route_list" => Function::new_typed_with_env(&mut store, env, port_route_list), + "sock_status" => Function::new_typed_with_env(&mut store, env, sock_status), + "sock_addr_local" => Function::new_typed_with_env(&mut store, env, sock_addr_local), + "sock_addr_peer" => Function::new_typed_with_env(&mut store, env, sock_addr_peer), + "sock_open" => Function::new_typed_with_env(&mut store, env, sock_open), + "sock_set_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_set_opt_flag), + "sock_get_opt_flag" => Function::new_typed_with_env(&mut store, env, sock_get_opt_flag), + "sock_set_opt_time" => Function::new_typed_with_env(&mut store, env, sock_set_opt_time), + "sock_get_opt_time" => Function::new_typed_with_env(&mut store, env, sock_get_opt_time), + "sock_set_opt_size" => Function::new_typed_with_env(&mut store, env, sock_set_opt_size), + "sock_get_opt_size" => Function::new_typed_with_env(&mut store, env, sock_get_opt_size), + "sock_join_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v4), + "sock_leave_multicast_v4" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v4), + "sock_join_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_join_multicast_v6), + "sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6), + "sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind), + "sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen), + "sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept), + "sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect), + "sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv), + "sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from), + "sock_send" => Function::new_typed_with_env(&mut store, env, sock_send), + "sock_send_to" => Function::new_typed_with_env(&mut store, env, sock_send_to), + "sock_send_file" => Function::new_typed_with_env(&mut store, env, sock_send_file), + "sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown), + "resolve" => Function::new_typed_with_env(&mut store, env, resolve), } } } diff --git a/tests/compilers/imports.rs b/tests/compilers/imports.rs index 558e4fa51bd..fafcaa6f10d 100644 --- a/tests/compilers/imports.rs +++ b/tests/compilers/imports.rs @@ -49,26 +49,25 @@ fn get_module(store: &Store) -> Result { fn dynamic_function(config: crate::Config) -> Result<()> { let mut store = config.store(); let module = get_module(&store)?; - let mut env = FunctionEnv::new(&mut store, ()); static HITS: AtomicUsize = AtomicUsize::new(0); let imports = imports! { "host" => { - "0" => Function::new(&mut store, &env, FunctionType::new(vec![], vec![]), |_ctx, _values| { + "0" => Function::new(&mut store, FunctionType::new(vec![], vec![]), |_env, _values| { assert_eq!(HITS.fetch_add(1, SeqCst), 0); Ok(vec![]) }), - "1" => Function::new(&mut store, &env, FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]), |_ctx, values| { + "1" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]), |_env, values| { assert_eq!(values[0], Value::I32(0)); assert_eq!(HITS.fetch_add(1, SeqCst), 1); Ok(vec![Value::I32(1)]) }), - "2" => Function::new(&mut store, &env, FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]), |_ctx, values| { + "2" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]), |_env, values| { assert_eq!(values[0], Value::I32(2)); assert_eq!(values[1], Value::I64(3)); assert_eq!(HITS.fetch_add(1, SeqCst), 2); Ok(vec![]) }), - "3" => Function::new(&mut store, &env, FunctionType::new(vec![ValueType::I32, ValueType::I64, ValueType::I32, ValueType::F32, ValueType::F64], vec![]), |_ctx, values| { + "3" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I64, ValueType::I32, ValueType::F32, ValueType::F64], vec![]), |_env, values| { assert_eq!(values[0], Value::I32(100)); assert_eq!(values[1], Value::I64(200)); assert_eq!(values[2], Value::I32(300)); @@ -105,37 +104,37 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> { counter: Arc::new(AtomicUsize::new(0)), }; let mut env = FunctionEnv::new(&mut store, env); - let f0 = Function::new( + let f0 = Function::new_with_env( &mut store, &env, FunctionType::new(vec![], vec![]), - |ctx, _values| { - assert_eq!(ctx.data().fetch_add(1, SeqCst), 0); + |env, _values| { + assert_eq!(env.data().fetch_add(1, SeqCst), 0); Ok(vec![]) }, ); - let f1 = Function::new( + let f1 = Function::new_with_env( &mut store, &env, FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]), - |ctx, values| { + |env, values| { assert_eq!(values[0], Value::I32(0)); - assert_eq!(ctx.data().fetch_add(1, SeqCst), 1); + assert_eq!(env.data().fetch_add(1, SeqCst), 1); Ok(vec![Value::I32(1)]) }, ); - let f2 = Function::new( + let f2 = Function::new_with_env( &mut store, &env, FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]), - |ctx, values| { + |env, values| { assert_eq!(values[0], Value::I32(2)); assert_eq!(values[1], Value::I64(3)); - assert_eq!(ctx.data().fetch_add(1, SeqCst), 2); + assert_eq!(env.data().fetch_add(1, SeqCst), 2); Ok(vec![]) }, ); - let f3 = Function::new( + let f3 = Function::new_with_env( &mut store, &env, FunctionType::new( @@ -148,13 +147,13 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> { ], vec![], ), - |ctx, values| { + |env, values| { assert_eq!(values[0], Value::I32(100)); assert_eq!(values[1], Value::I64(200)); assert_eq!(values[2], Value::I32(300)); assert_eq!(values[3], Value::F32(400.0)); assert_eq!(values[4], Value::F64(500.0)); - assert_eq!(ctx.data().fetch_add(1, SeqCst), 3); + assert_eq!(env.data().fetch_add(1, SeqCst), 3); Ok(vec![]) }, ); @@ -181,28 +180,22 @@ fn static_function(config: crate::Config) -> Result<()> { let module = get_module(&store)?; static HITS: AtomicUsize = AtomicUsize::new(0); - let mut env = FunctionEnv::new(&mut store, ()); - let f0 = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>| { + let f0 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>| { assert_eq!(HITS.fetch_add(1, SeqCst), 0); }); - let f1 = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>, x: i32| -> i32 { + let f1 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>, x: i32| -> i32 { assert_eq!(x, 0); assert_eq!(HITS.fetch_add(1, SeqCst), 1); 1 }); - let f2 = Function::new_native( - &mut store, - &env, - |_ctx: FunctionEnvMut<_>, x: i32, y: i64| { - assert_eq!(x, 2); - assert_eq!(y, 3); - assert_eq!(HITS.fetch_add(1, SeqCst), 2); - }, - ); - let f3 = Function::new_native( + let f2 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>, x: i32, y: i64| { + assert_eq!(x, 2); + assert_eq!(y, 3); + assert_eq!(HITS.fetch_add(1, SeqCst), 2); + }); + let f3 = Function::new_typed( &mut store, - &env, - |_ctx: FunctionEnvMut<_>, a: i32, b: i64, c: i32, d: f32, e: f64| { + |_env: FunctionEnvMut<_>, a: i32, b: i64, c: i32, d: f32, e: f64| { assert_eq!(a, 100); assert_eq!(b, 200); assert_eq!(c, 300); @@ -234,32 +227,25 @@ fn static_function_with_results(config: crate::Config) -> Result<()> { let module = get_module(&store)?; static HITS: AtomicUsize = AtomicUsize::new(0); - let mut env = FunctionEnv::new(&mut store, ()); - let f0 = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>| { + let f0 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>| { assert_eq!(HITS.fetch_add(1, SeqCst), 0); }); - let f1 = Function::new_native( + let f1 = Function::new_typed( &mut store, - &env, - |_ctx: FunctionEnvMut<_>, x: i32| -> Result { + |_env: FunctionEnvMut<_>, x: i32| -> Result { assert_eq!(x, 0); assert_eq!(HITS.fetch_add(1, SeqCst), 1); Ok(1) }, ); - let f2 = Function::new_native( - &mut store, - &env, - |_ctx: FunctionEnvMut<_>, x: i32, y: i64| { - assert_eq!(x, 2); - assert_eq!(y, 3); - assert_eq!(HITS.fetch_add(1, SeqCst), 2); - }, - ); - let f3 = Function::new_native( + let f2 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>, x: i32, y: i64| { + assert_eq!(x, 2); + assert_eq!(y, 3); + assert_eq!(HITS.fetch_add(1, SeqCst), 2); + }); + let f3 = Function::new_typed( &mut store, - &env, - |_ctx: FunctionEnvMut<_>, a: i32, b: i64, c: i32, d: f32, e: f64| { + |_env: FunctionEnvMut<_>, a: i32, b: i64, c: i32, d: f32, e: f64| { assert_eq!(a, 100); assert_eq!(b, 200); assert_eq!(c, 300); @@ -301,37 +287,37 @@ fn static_function_with_env(config: crate::Config) -> Result<()> { let env: Env = Env(Arc::new(AtomicUsize::new(0))); let mut env = FunctionEnv::new(&mut store, env); - let f0 = Function::new_native(&mut store, &env, |ctx: FunctionEnvMut| { - assert_eq!(ctx.data().fetch_add(1, SeqCst), 0); + let f0 = Function::new_typed_with_env(&mut store, &env, |env: FunctionEnvMut| { + assert_eq!(env.data().fetch_add(1, SeqCst), 0); }); - let f1 = Function::new_native( + let f1 = Function::new_typed_with_env( &mut store, &env, - |ctx: FunctionEnvMut, x: i32| -> i32 { + |env: FunctionEnvMut, x: i32| -> i32 { assert_eq!(x, 0); - assert_eq!(ctx.data().fetch_add(1, SeqCst), 1); + assert_eq!(env.data().fetch_add(1, SeqCst), 1); 1 }, ); - let f2 = Function::new_native( + let f2 = Function::new_typed_with_env( &mut store, &env, - |ctx: FunctionEnvMut, x: i32, y: i64| { + |env: FunctionEnvMut, x: i32, y: i64| { assert_eq!(x, 2); assert_eq!(y, 3); - assert_eq!(ctx.data().fetch_add(1, SeqCst), 2); + assert_eq!(env.data().fetch_add(1, SeqCst), 2); }, ); - let f3 = Function::new_native( + let f3 = Function::new_typed_with_env( &mut store, &env, - |ctx: FunctionEnvMut, a: i32, b: i64, c: i32, d: f32, e: f64| { + |env: FunctionEnvMut, a: i32, b: i64, c: i32, d: f32, e: f64| { assert_eq!(a, 100); assert_eq!(b, 200); assert_eq!(c, 300); assert_eq!(d, 400.0); assert_eq!(e, 500.0); - assert_eq!(ctx.data().fetch_add(1, SeqCst), 3); + assert_eq!(env.data().fetch_add(1, SeqCst), 3); }, ); Instance::new( @@ -364,10 +350,10 @@ fn static_function_that_fails(config: crate::Config) -> Result<()> { let module = Module::new(&store, &wat)?; let mut env = FunctionEnv::new(&mut store, ()); - let f0 = Function::new_native( + let f0 = Function::new_typed_with_env( &mut store, &env, - |_ctx: FunctionEnvMut<_>| -> Result { + |_env: FunctionEnvMut<_>| -> Result { Err(RuntimeError::new("oops")) }, ); @@ -420,12 +406,12 @@ fn dynamic_function_with_env_wasmer_env_init_works(config: crate::Config) -> Res let env: Env = Env { memory: None }; let mut env = FunctionEnv::new(&mut store, env); - let f0 = Function::new( + let f0 = Function::new_with_env( &mut store, &env, FunctionType::new(vec![], vec![]), - |ctx, _values| { - assert!(ctx.data().memory.as_ref().is_some()); + |env, _values| { + assert!(env.data().memory.as_ref().is_some()); Ok(vec![]) }, ); @@ -466,13 +452,13 @@ fn multi_use_host_fn_manages_memory_correctly(config: crate::Config) -> Result<( let env: Env = Env { memory: None }; let mut env = FunctionEnv::new(&mut store, env); - fn host_fn(ctx: FunctionEnvMut) { - assert!(ctx.data().memory.is_some()); + fn host_fn(env: FunctionEnvMut) { + assert!(env.data().memory.is_some()); println!("Hello, world!"); } let imports = imports! { "host" => { - "fn" => Function::new_native(&mut store, &env, host_fn), + "fn" => Function::new_typed_with_env(&mut store, &env, host_fn), }, }; let instance1 = Instance::new(&mut store, &module, &imports)?; diff --git a/tests/compilers/issues.rs b/tests/compilers/issues.rs index 6fed2c724c7..e8d5dfb56a6 100644 --- a/tests/compilers/issues.rs +++ b/tests/compilers/issues.rs @@ -64,7 +64,7 @@ fn issue_2329(mut config: crate::Config) -> Result<()> { let mut env = FunctionEnv::new(&mut store, env); let imports: Imports = imports! { "env" => { - "__read_memory" => Function::new_native( + "__read_memory" => Function::new_typed_with_env( &mut store, &env, read_memory @@ -197,14 +197,23 @@ fn call_with_static_data_pointers(mut config: crate::Config) -> Result<()> { env.as_mut(&mut store).memory = Some(memory.clone()); let mut exports = Exports::new(); exports.insert("memory", memory); - exports.insert("banana", Function::new_native(&mut store, &env, banana)); - exports.insert("peach", Function::new_native(&mut store, &env, peach)); + exports.insert( + "banana", + Function::new_typed_with_env(&mut store, &env, banana), + ); + exports.insert( + "peach", + Function::new_typed_with_env(&mut store, &env, peach), + ); exports.insert( "chaenomeles", - Function::new_native(&mut store, &env, chaenomeles), + Function::new_typed_with_env(&mut store, &env, chaenomeles), + ); + exports.insert( + "mango", + Function::new_typed_with_env(&mut store, &env, mango), ); - exports.insert("mango", Function::new_native(&mut store, &env, mango)); - exports.insert("gas", Function::new_native(&mut store, &env, gas)); + exports.insert("gas", Function::new_typed_with_env(&mut store, &env, gas)); let mut imports = Imports::new(); imports.register_namespace("env", exports); let instance = Instance::new(&mut store, &module, &imports)?; diff --git a/tests/compilers/main.rs b/tests/compilers/main.rs index 67606398ea9..541032c659a 100644 --- a/tests/compilers/main.rs +++ b/tests/compilers/main.rs @@ -12,9 +12,9 @@ mod issues; mod metering; mod middlewares; // mod multi_value_imports; -mod native_functions; mod serialize; mod traps; +mod typed_functions; mod wasi; mod wast; diff --git a/tests/compilers/serialize.rs b/tests/compilers/serialize.rs index 1b796bf58c5..23b62716efe 100644 --- a/tests/compilers/serialize.rs +++ b/tests/compilers/serialize.rs @@ -1,5 +1,4 @@ use anyhow::Result; -use wasmer::FunctionEnv; use wasmer::*; #[compiler_test(serialize)] @@ -54,8 +53,7 @@ fn test_deserialize(config: crate::Config) -> Result<()> { vec![Type::I32, Type::I64, Type::I32, Type::F32, Type::F64], vec![Type::I64], ); - let mut env = FunctionEnv::new(&mut store, ()); - let f0 = Function::new(&mut store, &env, &func_type, |_ctx, params| { + let f0 = Function::new(&mut store, &func_type, |_ctx, params| { let param_0: i64 = params[0].unwrap_i32() as i64; let param_1: i64 = params[1].unwrap_i64() as i64; let param_2: i64 = params[2].unwrap_i32() as i64; diff --git a/tests/compilers/traps.rs b/tests/compilers/traps.rs index fff22c69e05..ace95080e76 100644 --- a/tests/compilers/traps.rs +++ b/tests/compilers/traps.rs @@ -1,6 +1,5 @@ use anyhow::Result; use std::panic::{self, AssertUnwindSafe}; -use wasmer::FunctionEnv; use wasmer::*; #[compiler_test(traps)] @@ -14,9 +13,8 @@ fn test_trap_return(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; - let mut env = FunctionEnv::new(&mut store, ()); let hello_type = FunctionType::new(vec![], vec![]); - let hello_func = Function::new(&mut store, &env, &hello_type, |_ctx, _| { + let hello_func = Function::new(&mut store, &hello_type, |_ctx, _| { Err(RuntimeError::new("test 123")) }); @@ -55,7 +53,6 @@ fn test_trap_trace(config: crate::Config) -> Result<()> { ) "#; - let mut env = FunctionEnv::new(&mut store, ()); let module = Module::new(&store, wat)?; let instance = Instance::new(&mut store, &module, &imports! {})?; let run_func = instance @@ -96,9 +93,8 @@ fn test_trap_trace_cb(config: crate::Config) -> Result<()> { ) "#; - let mut env = FunctionEnv::new(&mut store, ()); let fn_type = FunctionType::new(vec![], vec![]); - let fn_func = Function::new(&mut store, &env, &fn_type, |_ctx, _| { + let fn_func = Function::new(&mut store, &fn_type, |_ctx, _| { Err(RuntimeError::new("cb throw")) }); @@ -146,7 +142,6 @@ fn test_trap_stack_overflow(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; - let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &imports! {})?; let run_func = instance .exports @@ -180,7 +175,6 @@ fn trap_display_pretty(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; - let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &imports! {})?; let run_func = instance .exports @@ -217,7 +211,6 @@ fn trap_display_multi_module(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; - let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &imports! {})?; let bar = instance.exports.get_function("bar")?.clone(); @@ -272,9 +265,8 @@ fn trap_start_function_import(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &binary)?; - let mut env = FunctionEnv::new(&mut store, ()); let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &env, &sig, |_ctx, _| { + let func = Function::new(&mut store, &sig, |_env, _| { Err(RuntimeError::new("user trap")) }); let err = Instance::new( @@ -315,10 +307,9 @@ fn rust_panic_import(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &binary)?; - let mut env = FunctionEnv::new(&mut store, ()); let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &env, &sig, |_ctx, _| panic!("this is a panic")); - let f0 = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>| { + let func = Function::new(&mut store, &sig, |_env, _| panic!("this is a panic")); + let f0 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>| { panic!("this is another panic") }); let instance = Instance::new( @@ -363,9 +354,8 @@ fn rust_panic_start_function(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &binary)?; - let mut env = FunctionEnv::new(&mut store, ()); let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &env, &sig, |_ctx, _| panic!("this is a panic")); + let func = Function::new(&mut store, &sig, |_env, _| panic!("this is a panic")); let err = panic::catch_unwind(AssertUnwindSafe(|| { drop(Instance::new( &mut store, @@ -380,7 +370,7 @@ fn rust_panic_start_function(config: crate::Config) -> Result<()> { .unwrap_err(); assert_eq!(err.downcast_ref::<&'static str>(), Some(&"this is a panic")); - let func = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>| { + let func = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>| { panic!("this is another panic") }); let err = panic::catch_unwind(AssertUnwindSafe(|| { @@ -412,7 +402,6 @@ fn mismatched_arguments(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &binary)?; - let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &imports! {})?; let func: &Function = instance.exports.get("foo")?; assert_eq!( @@ -452,7 +441,6 @@ fn call_signature_mismatch(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &binary)?; - let mut env = FunctionEnv::new(&mut store, ()); let err = Instance::new(&mut store, &module, &imports! {}) .err() .expect("expected error"); @@ -481,7 +469,6 @@ fn start_trap_pretty(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, wat)?; - let mut env = FunctionEnv::new(&mut store, ()); let err = Instance::new(&mut store, &module, &imports! {}) .err() .expect("expected error"); @@ -503,7 +490,6 @@ RuntimeError: unreachable fn present_after_module_drop(config: crate::Config) -> Result<()> { let mut store = config.store(); let module = Module::new(&store, r#"(func (export "foo") unreachable)"#)?; - let mut env = FunctionEnv::new(&mut store, ()); let instance = Instance::new(&mut store, &module, &imports! {})?; let func: Function = instance.exports.get_function("foo")?.clone(); diff --git a/tests/compilers/native_functions.rs b/tests/compilers/typed_functions.rs similarity index 72% rename from tests/compilers/native_functions.rs rename to tests/compilers/typed_functions.rs index 3f6ecac790c..a1a8068f1e9 100644 --- a/tests/compilers/native_functions.rs +++ b/tests/compilers/typed_functions.rs @@ -7,7 +7,7 @@ use wasmer::Type as ValueType; use wasmer::*; fn long_f( - _ctx: FunctionEnvMut<()>, + _env: FunctionEnvMut<()>, a: u32, b: u32, c: u32, @@ -31,7 +31,7 @@ fn long_f( + a as u64 * 1000000000 } -fn long_f_dynamic(_ctx: FunctionEnvMut<()>, values: &[Value]) -> Result, RuntimeError> { +fn long_f_dynamic(_env: FunctionEnvMut<()>, values: &[Value]) -> Result, RuntimeError> { Ok(vec![Value::I64( values[9].unwrap_i32() as i64 + values[8].unwrap_i32() as i64 * 10 @@ -46,8 +46,8 @@ fn long_f_dynamic(_ctx: FunctionEnvMut<()>, values: &[Value]) -> Result anyhow::Result<()> { +#[compiler_test(typed_functions)] +fn typed_function_works_for_wasm(config: crate::Config) -> anyhow::Result<()> { let mut store = config.store(); let wat = r#"(module (func $multiply (import "env" "multiply") (param i32 i32) (result i32)) @@ -63,7 +63,7 @@ fn native_function_works_for_wasm(config: crate::Config) -> anyhow::Result<()> { let import_object = imports! { "env" => { - "multiply" => Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>, a: i32, b: i32| a * b), + "multiply" => Function::new_typed_with_env(&mut store, &env, |_env: FunctionEnvMut<_>, a: i32, b: i32| a * b), }, }; let instance = Instance::new(&mut store, &module, &import_object)?; @@ -83,7 +83,7 @@ fn native_function_works_for_wasm(config: crate::Config) -> anyhow::Result<()> { { let dyn_f: &Function = instance.exports.get("double_then_add")?; - let f: TypedFunction<(i32, i32), i32> = dyn_f.native(&mut store).unwrap(); + let f: TypedFunction<(i32, i32), i32> = dyn_f.typed(&mut store).unwrap(); let result = f.call(&mut store, 4, 6)?; assert_eq!(result, 20); } @@ -91,33 +91,32 @@ fn native_function_works_for_wasm(config: crate::Config) -> anyhow::Result<()> { Ok(()) } -#[compiler_test(native_functions)] -fn native_host_function_closure_panics(config: crate::Config) { +#[compiler_test(typed_functions)] +fn typed_host_function_closure_panics(config: crate::Config) { let mut store = config.store(); - let mut env = FunctionEnv::new(&mut store, ()); let state = 3; - Function::new_native(&mut store, &env, move |_ctx: FunctionEnvMut<_>, _: i32| { + Function::new_typed(&mut store, move |_env: FunctionEnvMut<_>, _: i32| { println!("{}", state); }); } -#[compiler_test(native_functions)] -fn native_with_env_host_function_closure_panics(config: crate::Config) { +#[compiler_test(typed_functions)] +fn typed_with_env_host_function_closure_panics(config: crate::Config) { let mut store = config.store(); let env: i32 = 4; let mut env = FunctionEnv::new(&mut store, env); let state = 3; - Function::new_native( + Function::new_typed_with_env( &mut store, &env, - move |_ctx: FunctionEnvMut, _: i32| { + move |_env: FunctionEnvMut, _: i32| { println!("{}", state); }, ); } -#[compiler_test(native_functions)] -fn non_native_functions_and_closures_with_no_env_work(config: crate::Config) -> anyhow::Result<()> { +#[compiler_test(typed_functions)] +fn non_typed_functions_and_closures_with_no_env_work(config: crate::Config) -> anyhow::Result<()> { let mut store = config.store(); let wat = r#"(module (func $multiply1 (import "env" "multiply1") (param i32 i32) (result i32)) @@ -143,24 +142,24 @@ fn non_native_functions_and_closures_with_no_env_work(config: crate::Config) -> let captured_by_closure = 20; let import_object = imports! { "env" => { - "multiply1" => Function::new(&mut store, &env, &ty, move |_ctx, args| { + "multiply1" => Function::new_with_env(&mut store, &env, &ty, move |_env, args| { if let (Value::I32(v1), Value::I32(v2)) = (&args[0], &args[1]) { Ok(vec![Value::I32(v1 * v2 * captured_by_closure)]) } else { panic!("Invalid arguments"); } }), - "multiply2" => Function::new(&mut store, &env, &ty, move |ctx, args| { + "multiply2" => Function::new_with_env(&mut store, &env, &ty, move |env, args| { if let (Value::I32(v1), Value::I32(v2)) = (&args[0], &args[1]) { - Ok(vec![Value::I32(v1 * v2 * captured_by_closure * ctx.data())]) + Ok(vec![Value::I32(v1 * v2 * captured_by_closure * env.data())]) } else { panic!("Invalid arguments"); } }), - "multiply3" => Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<_>, arg1: i32, arg2: i32| -> i32 + "multiply3" => Function::new_typed_with_env(&mut store, &env, |_env: FunctionEnvMut<_>, arg1: i32, arg2: i32| -> i32 {arg1 * arg2 }), - "multiply4" => Function::new_native(&mut store, &env, |ctx: FunctionEnvMut, arg1: i32, arg2: i32| -> i32 - {arg1 * arg2 * ctx.data() }), + "multiply4" => Function::new_typed_with_env(&mut store, &env, |env: FunctionEnvMut, arg1: i32, arg2: i32| -> i32 + {arg1 * arg2 * env.data() }), }, }; @@ -175,8 +174,8 @@ fn non_native_functions_and_closures_with_no_env_work(config: crate::Config) -> Ok(()) } -#[compiler_test(native_functions)] -fn native_function_works_for_wasm_function_manyparams(config: crate::Config) -> anyhow::Result<()> { +#[compiler_test(typed_functions)] +fn typed_function_works_for_wasm_function_manyparams(config: crate::Config) -> anyhow::Result<()> { let mut store = config.store(); let wat = r#"(module (func $longf (import "env" "longf") (param i32 i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i64)) @@ -186,10 +185,9 @@ fn native_function_works_for_wasm_function_manyparams(config: crate::Config) -> (call $longf (i32.const 1) (i32.const 2) (i32.const 3) (i32.const 4) (i32.const 5) (i32.const 6) (i64.const 7) (i64.const 8) (i32.const 9) (i32.const 0))) )"#; let module = Module::new(&store, wat).unwrap(); - let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! { "env" => { - "longf" => Function::new_native(&mut store, &env, long_f), + "longf" => Function::new_typed(&mut store, long_f), }, }; @@ -197,7 +195,7 @@ fn native_function_works_for_wasm_function_manyparams(config: crate::Config) -> { let dyn_f: &Function = instance.exports.get("longf")?; - let f: TypedFunction<(), i64> = dyn_f.native(&mut store).unwrap(); + let f: TypedFunction<(), i64> = dyn_f.typed(&mut store).unwrap(); let result = f.call(&mut store)?; assert_eq!(result, 1234567890); } @@ -205,7 +203,7 @@ fn native_function_works_for_wasm_function_manyparams(config: crate::Config) -> { let dyn_f: &Function = instance.exports.get("longf_pure")?; let f: TypedFunction<(u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), i64> = - dyn_f.native(&mut store).unwrap(); + dyn_f.typed(&mut store).unwrap(); let result = f.call(&mut store, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)?; assert_eq!(result, 1234567890); } @@ -213,12 +211,11 @@ fn native_function_works_for_wasm_function_manyparams(config: crate::Config) -> Ok(()) } -#[compiler_test(native_functions)] -fn native_function_works_for_wasm_function_manyparams_dynamic( +#[compiler_test(typed_functions)] +fn typed_function_works_for_wasm_function_manyparams_dynamic( config: crate::Config, ) -> anyhow::Result<()> { let mut store = config.store(); - let mut env = FunctionEnv::new(&mut store, ()); let wat = r#"(module (func $longf (import "env" "longf") (param i32 i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i64)) (func (export "longf_pure") (param i32 i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i64) @@ -230,7 +227,7 @@ fn native_function_works_for_wasm_function_manyparams_dynamic( let import_object = imports! { "env" => { - "longf" => Function::new(&mut store, &env, FunctionType::new(vec![ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I64 , ValueType::I64 ,ValueType::I32, ValueType::I32], vec![ValueType::I64]), long_f_dynamic), + "longf" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I64 , ValueType::I64 ,ValueType::I32, ValueType::I32], vec![ValueType::I64]), long_f_dynamic), }, }; @@ -238,7 +235,7 @@ fn native_function_works_for_wasm_function_manyparams_dynamic( { let dyn_f: &Function = instance.exports.get("longf")?; - let f: TypedFunction<(), i64> = dyn_f.native(&mut store).unwrap(); + let f: TypedFunction<(), i64> = dyn_f.typed(&mut store).unwrap(); let result = f.call(&mut store)?; assert_eq!(result, 1234567890); } @@ -246,7 +243,7 @@ fn native_function_works_for_wasm_function_manyparams_dynamic( { let dyn_f: &Function = instance.exports.get("longf_pure")?; let f: TypedFunction<(u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), i64> = - dyn_f.native(&mut store).unwrap(); + dyn_f.typed(&mut store).unwrap(); let result = f.call(&mut store, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)?; assert_eq!(result, 1234567890); } @@ -254,17 +251,16 @@ fn native_function_works_for_wasm_function_manyparams_dynamic( Ok(()) } -#[compiler_test(native_functions)] +#[compiler_test(typed_functions)] fn static_host_function_without_env(config: crate::Config) -> anyhow::Result<()> { let mut store = config.store(); - let mut env = FunctionEnv::new(&mut store, ()); - fn f(_ctx: FunctionEnvMut<()>, a: i32, b: i64, c: f32, d: f64) -> (f64, f32, i64, i32) { + fn f(_env: FunctionEnvMut<()>, a: i32, b: i64, c: f32, d: f64) -> (f64, f32, i64, i32) { (d * 4.0, c * 3.0, b * 2, a * 1) } fn f_ok( - _ctx: FunctionEnvMut<()>, + _env: FunctionEnvMut<()>, a: i32, b: i64, c: f32, @@ -274,7 +270,7 @@ fn static_host_function_without_env(config: crate::Config) -> anyhow::Result<()> } fn long_f( - _ctx: FunctionEnvMut<()>, + _env: FunctionEnvMut<()>, a: u32, b: u32, c: u32, @@ -295,42 +291,42 @@ fn static_host_function_without_env(config: crate::Config) -> anyhow::Result<()> // Native static host function that returns a tuple. { - let f = Function::new_native(&mut store, &env, f); - let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = - f.native(&mut store).unwrap(); - let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; + let f = Function::new_typed(&mut store, f); + let f_typed: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = + f.typed(&mut store).unwrap(); + let result = f_typed.call(&mut store, 1, 3, 5.0, 7.0)?; assert_eq!(result, (28.0, 15.0, 6, 1)); } // Native static host function that returns a tuple. { - let long_f = Function::new_native(&mut store, &env, long_f); - let long_f_native: TypedFunction< + let long_f = Function::new_typed(&mut store, long_f); + let long_f_typed: TypedFunction< (u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), (u32, u64, u32), - > = long_f.native(&mut store).unwrap(); - let result = long_f_native.call(&mut store, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)?; + > = long_f.typed(&mut store).unwrap(); + let result = long_f_typed.call(&mut store, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)?; assert_eq!(result, (654321, 87, 09)); } // Native static host function that returns a result of a tuple. { - let f = Function::new_native(&mut store, &env, f_ok); - let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = - f.native(&mut store).unwrap(); - let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; + let f = Function::new_typed(&mut store, f_ok); + let f_typed: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = + f.typed(&mut store).unwrap(); + let result = f_typed.call(&mut store, 1, 3, 5.0, 7.0)?; assert_eq!(result, (28.0, 15.0, 6, 1)); } Ok(()) } -#[compiler_test(native_functions)] +#[compiler_test(typed_functions)] fn static_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { let mut store = config.store(); - fn f(mut ctx: FunctionEnvMut, a: i32, b: i64, c: f32, d: f64) -> (f64, f32, i64, i32) { - let mut guard = ctx.data_mut().0.lock().unwrap(); + fn f(mut env: FunctionEnvMut, a: i32, b: i64, c: f32, d: f64) -> (f64, f32, i64, i32) { + let mut guard = env.data_mut().0.lock().unwrap(); assert_eq!(*guard, 100); *guard = 101; @@ -338,13 +334,13 @@ fn static_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { } fn f_ok( - mut ctx: FunctionEnvMut, + mut env: FunctionEnvMut, a: i32, b: i64, c: f32, d: f64, ) -> Result<(f64, f32, i64, i32), Infallible> { - let mut guard = ctx.data_mut().0.lock().unwrap(); + let mut guard = env.data_mut().0.lock().unwrap(); assert_eq!(*guard, 100); *guard = 101; @@ -366,13 +362,13 @@ fn static_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { let env = Env(Arc::new(Mutex::new(100))); let mut env = FunctionEnv::new(&mut store, env); - let f = Function::new_native(&mut store, &env, f); - let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = - f.native(&mut store).unwrap(); + let f = Function::new_typed_with_env(&mut store, &env, f); + let f_typed: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = + f.typed(&mut store).unwrap(); assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 100); - let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; + let result = f_typed.call(&mut store, 1, 3, 5.0, 7.0)?; assert_eq!(result, (28.0, 15.0, 6, 1)); assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 101); @@ -383,13 +379,13 @@ fn static_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { let env = Env(Arc::new(Mutex::new(100))); let mut env = FunctionEnv::new(&mut store, env); - let f = Function::new_native(&mut store, &env, f_ok); - let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = - f.native(&mut store).unwrap(); + let f = Function::new_typed_with_env(&mut store, &env, f_ok); + let f_typed: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = + f.typed(&mut store).unwrap(); assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 100); - let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; + let result = f_typed.call(&mut store, 1, 3, 5.0, 7.0)?; assert_eq!(result, (28.0, 15.0, 6, 1)); assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 101); @@ -398,13 +394,11 @@ fn static_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { Ok(()) } -#[compiler_test(native_functions)] +#[compiler_test(typed_functions)] fn dynamic_host_function_without_env(config: crate::Config) -> anyhow::Result<()> { let mut store = config.store(); - let mut env = FunctionEnv::new(&mut store, ()); let f = Function::new( &mut store, - &env, FunctionType::new( vec![ ValueType::I32, @@ -419,7 +413,7 @@ fn dynamic_host_function_without_env(config: crate::Config) -> anyhow::Result<() ValueType::I32, ], ), - |_ctx: FunctionEnvMut<_>, values| { + |_env: FunctionEnvMut<_>, values| { Ok(vec![ Value::F64(values[3].unwrap_f64() * 4.0), Value::F32(values[2].unwrap_f32() * 3.0), @@ -428,16 +422,16 @@ fn dynamic_host_function_without_env(config: crate::Config) -> anyhow::Result<() ]) }, ); - let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = - f.native(&mut store).unwrap(); - let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; + let f_typed: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = + f.typed(&mut store).unwrap(); + let result = f_typed.call(&mut store, 1, 3, 5.0, 7.0)?; assert_eq!(result, (28.0, 15.0, 6, 1)); Ok(()) } -#[compiler_test(native_functions)] +#[compiler_test(typed_functions)] fn dynamic_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { let mut store = config.store(); @@ -453,7 +447,7 @@ fn dynamic_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { let env = Env(Arc::new(Mutex::new(100))); let mut env = FunctionEnv::new(&mut store, env); - let f = Function::new( + let f = Function::new_with_env( &mut store, &env, FunctionType::new( @@ -470,8 +464,8 @@ fn dynamic_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { ValueType::I32, ], ), - |mut ctx, values| { - let mut guard = ctx.data_mut().0.lock().unwrap(); + |mut env, values| { + let mut guard = env.data_mut().0.lock().unwrap(); assert_eq!(*guard, 100); *guard = 101; @@ -485,12 +479,12 @@ fn dynamic_host_function_with_env(config: crate::Config) -> anyhow::Result<()> { }, ); - let f_native: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = - f.native(&mut store).unwrap(); + let f_typed: TypedFunction<(i32, i64, f32, f64), (f64, f32, i64, i32)> = + f.typed(&mut store).unwrap(); assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 100); - let result = f_native.call(&mut store, 1, 3, 5.0, 7.0)?; + let result = f_typed.call(&mut store, 1, 3, 5.0, 7.0)?; assert_eq!(result, (28.0, 15.0, 6, 1)); assert_eq!(*env.as_mut(&mut store).0.lock().unwrap(), 101); diff --git a/tests/lib/wast/src/spectest.rs b/tests/lib/wast/src/spectest.rs index 389a5c4e16c..16e3aea21ed 100644 --- a/tests/lib/wast/src/spectest.rs +++ b/tests/lib/wast/src/spectest.rs @@ -2,30 +2,28 @@ use wasmer::*; /// Return an instance implementing the "spectest" interface used in the /// spec testsuite. -pub fn spectest_importobject(store: &mut Store, context: &FunctionEnv<()>) -> Imports { - let print = Function::new_native(store, context, |_: FunctionEnvMut<()>| {}); - let print_i32 = Function::new_native(store, context, |_: FunctionEnvMut<()>, val: i32| { +pub fn spectest_importobject(store: &mut Store) -> Imports { + let print = Function::new_typed(store, |_: FunctionEnvMut<()>| {}); + let print_i32 = Function::new_typed(store, |_: FunctionEnvMut<()>, val: i32| { println!("{}: i32", val) }); - let print_i64 = Function::new_native(store, context, |_: FunctionEnvMut<()>, val: i64| { + let print_i64 = Function::new_typed(store, |_: FunctionEnvMut<()>, val: i64| { println!("{}: i64", val) }); - let print_f32 = Function::new_native(store, context, |_: FunctionEnvMut<()>, val: f32| { + let print_f32 = Function::new_typed(store, |_: FunctionEnvMut<()>, val: f32| { println!("{}: f32", val) }); - let print_f64 = Function::new_native(store, context, |_: FunctionEnvMut<()>, val: f64| { + let print_f64 = Function::new_typed(store, |_: FunctionEnvMut<()>, val: f64| { println!("{}: f64", val) }); - let print_i32_f32 = - Function::new_native(store, context, |_: FunctionEnvMut<()>, i: i32, f: f32| { - println!("{}: i32", i); - println!("{}: f32", f); - }); - let print_f64_f64 = - Function::new_native(store, context, |_: FunctionEnvMut<()>, f1: f64, f2: f64| { - println!("{}: f64", f1); - println!("{}: f64", f2); - }); + let print_i32_f32 = Function::new_typed(store, |_: FunctionEnvMut<()>, i: i32, f: f32| { + println!("{}: i32", i); + println!("{}: f32", f); + }); + let print_f64_f64 = Function::new_typed(store, |_: FunctionEnvMut<()>, f1: f64, f2: f64| { + println!("{}: f64", f1); + println!("{}: f64", f2); + }); let global_i32 = Global::new(store, Value::I32(666)); let global_i64 = Global::new(store, Value::I64(666)); diff --git a/tests/lib/wast/src/wast.rs b/tests/lib/wast/src/wast.rs index 89fb7e08314..50bc02b3883 100644 --- a/tests/lib/wast/src/wast.rs +++ b/tests/lib/wast/src/wast.rs @@ -26,8 +26,6 @@ pub struct Wast { current_is_allowed_failure: bool, /// The store in which the tests are executing. store: Store, - /// The context in which the tests are executing. - context: FunctionEnv<()>, /// A flag indicating if Wast tests should stop as soon as one test fails. pub fail_fast: bool, /// A flag indicating that assert_trap and assert_exhaustion should be skipped. @@ -37,11 +35,10 @@ pub struct Wast { impl Wast { /// Construct a new instance of `Wast` with a given imports. - pub fn new(store: Store, context: FunctionEnv<()>, import_object: Imports) -> Self { + pub fn new(store: Store, import_object: Imports) -> Self { Self { current: None, store, - context, import_object, allowed_instantiation_failures: HashSet::new(), match_trap_messages: HashMap::new(), @@ -73,9 +70,8 @@ impl Wast { /// Construct a new instance of `Wast` with the spectests imports. pub fn new_with_spectest(mut store: Store) -> Self { - let context = FunctionEnv::new(&mut store, ()); - let import_object = spectest_importobject(&mut store, &context); - Self::new(store, context, import_object) + let import_object = spectest_importobject(&mut store); + Self::new(store, import_object) } fn get_instance(&self, instance_name: Option<&str>) -> Result { From 566c897e573c0d4a823e4eea9058403c034657f9 Mon Sep 17 00:00:00 2001 From: Wolfgang Silbermayr Date: Thu, 4 Aug 2022 07:41:43 +0200 Subject: [PATCH 2/9] =?UTF-8?q?Remove=20FunctionEnvMut=20for=20Function::n?= =?UTF-8?q?ew(=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/imports_exports.rs | 2 +- examples/imports_function.rs | 2 +- lib/api/src/js/externals/function.rs | 1 + lib/api/src/sys/externals/function.rs | 10 +++++----- lib/api/tests/js_externals.rs | 2 +- lib/api/tests/sys_externals.rs | 12 ++++++------ tests/compilers/imports.rs | 8 ++++---- tests/compilers/serialize.rs | 2 +- tests/compilers/traps.rs | 14 +++++--------- tests/compilers/typed_functions.rs | 4 ++-- 10 files changed, 27 insertions(+), 30 deletions(-) diff --git a/examples/imports_exports.rs b/examples/imports_exports.rs index 93ac580da19..193fae4790a 100644 --- a/examples/imports_exports.rs +++ b/examples/imports_exports.rs @@ -58,7 +58,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, &host_function_signature, |_env, _args| { + let host_function = Function::new(&mut store, &host_function_signature, |_args| { Ok(vec![Value::I32(42)]) }); diff --git a/examples/imports_function.rs b/examples/imports_function.rs index a93d936af88..61612b21f85 100644 --- a/examples/imports_function.rs +++ b/examples/imports_function.rs @@ -54,7 +54,7 @@ fn main() -> Result<(), Box> { // Create the functions let multiply_dynamic_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let multiply_dynamic = Function::new(&mut store, &multiply_dynamic_signature, |_env, args| { + let multiply_dynamic = Function::new(&mut store, &multiply_dynamic_signature, |args| { println!("Calling `multiply_dynamic`..."); let result = args[0].unwrap_i32() * 2; diff --git a/lib/api/src/js/externals/function.rs b/lib/api/src/js/externals/function.rs index 0b0938df75a..b8bbba359ee 100644 --- a/lib/api/src/js/externals/function.rs +++ b/lib/api/src/js/externals/function.rs @@ -65,6 +65,7 @@ impl Function { /// /// If you know the signature of the host function at compile time, /// consider using [`Function::new_typed`] for less runtime overhead. + #[cfg(feature = "compiler")] pub fn new(store: &mut impl AsStoreMut, ty: FT, func: F) -> Self where FT: Into, diff --git a/lib/api/src/sys/externals/function.rs b/lib/api/src/sys/externals/function.rs index 6e43817520a..f5419cc2f05 100644 --- a/lib/api/src/sys/externals/function.rs +++ b/lib/api/src/sys/externals/function.rs @@ -50,13 +50,13 @@ impl Function { pub fn new(store: &mut impl AsStoreMut, ty: FT, func: F) -> Self where FT: Into, - F: Fn(FunctionEnvMut<()>, &[Value]) -> Result, RuntimeError> - + 'static - + Send - + Sync, + F: Fn(&[Value]) -> Result, RuntimeError> + 'static + Send + Sync, { let env = FunctionEnv::new(&mut store.as_store_mut(), ()); - Self::new_with_env(store, &env, ty, func) + let wrapped_func = move |_env: FunctionEnvMut<()>, + args: &[Value]| + -> Result, RuntimeError> { func(args) }; + Self::new_with_env(store, &env, ty, wrapped_func) } #[cfg(feature = "compiler")] diff --git a/lib/api/tests/js_externals.rs b/lib/api/tests/js_externals.rs index 980e85aad50..71b81cff0c1 100644 --- a/lib/api/tests/js_externals.rs +++ b/lib/api/tests/js_externals.rs @@ -93,7 +93,7 @@ mod js { // minimum: 0, // maximum: Some(1), // }; - // let f = Function::new(&mut store, &env, |num: i32| num + 1); + // let f = Function::new(&mut store, |num: i32| num + 1); // let table = Table::new(&store, table_type, Value::FuncRef(Some(f.clone())))?; // assert_eq!(*table.ty(&store), table_type); // let _elem = table.get(0).unwrap(); diff --git a/lib/api/tests/sys_externals.rs b/lib/api/tests/sys_externals.rs index 710c1bb882e..9bb6630b8bf 100644 --- a/lib/api/tests/sys_externals.rs +++ b/lib/api/tests/sys_externals.rs @@ -277,14 +277,14 @@ mod sys { let function = Function::new( &mut store, &function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32], vec![]); let function = Function::new( &mut store, &function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = @@ -292,14 +292,14 @@ mod sys { let function = Function::new( &mut store, &function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32]); let function = Function::new( &mut store, &function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = @@ -307,7 +307,7 @@ mod sys { let function = Function::new( &mut store, &function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); @@ -316,7 +316,7 @@ mod sys { let function = Function::new( &mut store, function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).params(), [Type::V128]); assert_eq!( diff --git a/tests/compilers/imports.rs b/tests/compilers/imports.rs index fafcaa6f10d..696a86b8594 100644 --- a/tests/compilers/imports.rs +++ b/tests/compilers/imports.rs @@ -52,22 +52,22 @@ fn dynamic_function(config: crate::Config) -> Result<()> { static HITS: AtomicUsize = AtomicUsize::new(0); let imports = imports! { "host" => { - "0" => Function::new(&mut store, FunctionType::new(vec![], vec![]), |_env, _values| { + "0" => Function::new(&mut store, FunctionType::new(vec![], vec![]), |_values| { assert_eq!(HITS.fetch_add(1, SeqCst), 0); Ok(vec![]) }), - "1" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]), |_env, values| { + "1" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]), |values| { assert_eq!(values[0], Value::I32(0)); assert_eq!(HITS.fetch_add(1, SeqCst), 1); Ok(vec![Value::I32(1)]) }), - "2" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]), |_env, values| { + "2" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]), |values| { assert_eq!(values[0], Value::I32(2)); assert_eq!(values[1], Value::I64(3)); assert_eq!(HITS.fetch_add(1, SeqCst), 2); Ok(vec![]) }), - "3" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I64, ValueType::I32, ValueType::F32, ValueType::F64], vec![]), |_env, values| { + "3" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I64, ValueType::I32, ValueType::F32, ValueType::F64], vec![]), |values| { assert_eq!(values[0], Value::I32(100)); assert_eq!(values[1], Value::I64(200)); assert_eq!(values[2], Value::I32(300)); diff --git a/tests/compilers/serialize.rs b/tests/compilers/serialize.rs index 23b62716efe..2610f83da1e 100644 --- a/tests/compilers/serialize.rs +++ b/tests/compilers/serialize.rs @@ -53,7 +53,7 @@ fn test_deserialize(config: crate::Config) -> Result<()> { vec![Type::I32, Type::I64, Type::I32, Type::F32, Type::F64], vec![Type::I64], ); - let f0 = Function::new(&mut store, &func_type, |_ctx, params| { + let f0 = Function::new(&mut store, &func_type, |params| { let param_0: i64 = params[0].unwrap_i32() as i64; let param_1: i64 = params[1].unwrap_i64() as i64; let param_2: i64 = params[2].unwrap_i32() as i64; diff --git a/tests/compilers/traps.rs b/tests/compilers/traps.rs index ace95080e76..7f7dbc2bd6d 100644 --- a/tests/compilers/traps.rs +++ b/tests/compilers/traps.rs @@ -14,7 +14,7 @@ fn test_trap_return(config: crate::Config) -> Result<()> { let module = Module::new(&store, wat)?; let hello_type = FunctionType::new(vec![], vec![]); - let hello_func = Function::new(&mut store, &hello_type, |_ctx, _| { + let hello_func = Function::new(&mut store, &hello_type, |_| { Err(RuntimeError::new("test 123")) }); @@ -94,9 +94,7 @@ fn test_trap_trace_cb(config: crate::Config) -> Result<()> { "#; let fn_type = FunctionType::new(vec![], vec![]); - let fn_func = Function::new(&mut store, &fn_type, |_ctx, _| { - Err(RuntimeError::new("cb throw")) - }); + let fn_func = Function::new(&mut store, &fn_type, |_| Err(RuntimeError::new("cb throw"))); let module = Module::new(&store, wat)?; let instance = Instance::new( @@ -266,9 +264,7 @@ fn trap_start_function_import(config: crate::Config) -> Result<()> { let module = Module::new(&store, &binary)?; let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &sig, |_env, _| { - Err(RuntimeError::new("user trap")) - }); + let func = Function::new(&mut store, &sig, |_| Err(RuntimeError::new("user trap"))); let err = Instance::new( &mut store, &module, @@ -308,7 +304,7 @@ fn rust_panic_import(config: crate::Config) -> Result<()> { let module = Module::new(&store, &binary)?; let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &sig, |_env, _| panic!("this is a panic")); + let func = Function::new(&mut store, &sig, |_| panic!("this is a panic")); let f0 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>| { panic!("this is another panic") }); @@ -355,7 +351,7 @@ fn rust_panic_start_function(config: crate::Config) -> Result<()> { let module = Module::new(&store, &binary)?; let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &sig, |_env, _| panic!("this is a panic")); + let func = Function::new(&mut store, &sig, |_| panic!("this is a panic")); let err = panic::catch_unwind(AssertUnwindSafe(|| { drop(Instance::new( &mut store, diff --git a/tests/compilers/typed_functions.rs b/tests/compilers/typed_functions.rs index a1a8068f1e9..6cee8ad9110 100644 --- a/tests/compilers/typed_functions.rs +++ b/tests/compilers/typed_functions.rs @@ -31,7 +31,7 @@ fn long_f( + a as u64 * 1000000000 } -fn long_f_dynamic(_env: FunctionEnvMut<()>, values: &[Value]) -> Result, RuntimeError> { +fn long_f_dynamic(values: &[Value]) -> Result, RuntimeError> { Ok(vec![Value::I64( values[9].unwrap_i32() as i64 + values[8].unwrap_i32() as i64 * 10 @@ -413,7 +413,7 @@ fn dynamic_host_function_without_env(config: crate::Config) -> anyhow::Result<() ValueType::I32, ], ), - |_env: FunctionEnvMut<_>, values| { + |values| { Ok(vec![ Value::F64(values[3].unwrap_f64() * 4.0), Value::F32(values[2].unwrap_f32() * 3.0), From 6582db9ba4e0a0914ae5121ff56bdd4b332902d3 Mon Sep 17 00:00:00 2001 From: Wolfgang Silbermayr Date: Thu, 4 Aug 2022 09:48:25 +0200 Subject: [PATCH 3/9] Add missing doc --- lib/api/src/js/externals/function.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/api/src/js/externals/function.rs b/lib/api/src/js/externals/function.rs index b8bbba359ee..c02a3285fca 100644 --- a/lib/api/src/js/externals/function.rs +++ b/lib/api/src/js/externals/function.rs @@ -397,6 +397,7 @@ impl Function { } #[deprecated(since = "3.0.0", note = "native() has been renamed to typed().")] + /// Transform this WebAssembly function into a typed function. pub fn native( &self, store: &impl AsStoreRef, From 89bba319a9fb30c30528f5eae52f0c78ce5c223b Mon Sep 17 00:00:00 2001 From: Wolfgang Silbermayr Date: Thu, 4 Aug 2022 13:16:49 +0200 Subject: [PATCH 4/9] =?UTF-8?q?No=20longer=20require=20FunctionEnvMut=20fo?= =?UTF-8?q?r=20new=5Ftyped(=E2=80=A6)=20callback=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/early_exit.rs | 4 +- examples/hello_world.rs | 4 +- examples/table.rs | 5 +- lib/api/src/js/exports.rs | 2 +- lib/api/src/js/externals/function.rs | 127 +++++++++++++++++++-- lib/api/src/js/native.rs | 10 +- lib/api/src/lib.rs | 2 +- lib/api/src/sys/externals/function.rs | 155 ++++++++++++++++++++++++-- lib/api/src/sys/imports.rs | 9 +- lib/api/tests/js_externals.rs | 32 ++---- lib/api/tests/js_instance.rs | 33 +++--- lib/api/tests/sys_externals.rs | 23 ++-- lib/api/tests/sys_module.rs | 16 +-- lib/api/tests/sys_reference_types.rs | 2 +- tests/compilers/imports.rs | 72 +++++------- tests/compilers/traps.rs | 8 +- tests/compilers/typed_functions.rs | 27 +---- tests/lib/wast/src/spectest.rs | 22 ++-- 18 files changed, 364 insertions(+), 189 deletions(-) diff --git a/examples/early_exit.rs b/examples/early_exit.rs index 1a0ec8e37b5..17d1b2ef9f2 100644 --- a/examples/early_exit.rs +++ b/examples/early_exit.rs @@ -16,7 +16,7 @@ use anyhow::bail; use std::fmt; -use wasmer::{imports, wat2wasm, Function, FunctionEnvMut, Instance, Module, Store, TypedFunction}; +use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, TypedFunction}; use wasmer_compiler_cranelift::Cranelift; // First we need to create an error type that we'll use to signal the end of execution. @@ -61,7 +61,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(_env: FunctionEnvMut<()>) -> Result<(), ExitCode> { + fn early_exit() -> Result<(), ExitCode> { // This is where it happens. Err(ExitCode(1)) } diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 0b5221f4733..35a10f4b2e2 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -6,7 +6,7 @@ //! cargo run --example hello-world --release --features "cranelift" //! ``` -use wasmer::{imports, wat2wasm, Function, FunctionEnvMut, Instance, Module, Store, TypedFunction}; +use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, TypedFunction}; use wasmer_compiler_cranelift::Cranelift; fn main() -> anyhow::Result<()> { @@ -51,7 +51,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(_env: FunctionEnvMut<'_, ()>) { + fn say_hello_world() { println!("Hello, world!") } diff --git a/examples/table.rs b/examples/table.rs index c3b9b35b7d6..65dc3f4f45d 100644 --- a/examples/table.rs +++ b/examples/table.rs @@ -1,11 +1,10 @@ use wasmer::{ - imports, wat2wasm, Function, FunctionEnvMut, Instance, Module, Store, TableType, Type, - TypedFunction, Value, + imports, wat2wasm, Function, Instance, Module, Store, TableType, Type, TypedFunction, Value, }; use wasmer_compiler_cranelift::Cranelift; /// A function we'll call through a table. -fn host_callback(_env: FunctionEnvMut<()>, arg1: i32, arg2: i32) -> i32 { +fn host_callback(arg1: i32, arg2: i32) -> i32 { arg1 + arg2 } diff --git a/lib/api/src/js/exports.rs b/lib/api/src/js/exports.rs index 6c4a8a9deb5..024ab8b9696 100644 --- a/lib/api/src/js/exports.rs +++ b/lib/api/src/js/exports.rs @@ -162,7 +162,7 @@ impl Exports { Rets: WasmTypeList, { self.get_function(name)? - .native(store) + .typed(store) .map_err(|_| ExportError::IncompatibleType) } diff --git a/lib/api/src/js/externals/function.rs b/lib/api/src/js/externals/function.rs index c02a3285fca..3c7bd3017c9 100644 --- a/lib/api/src/js/externals/function.rs +++ b/lib/api/src/js/externals/function.rs @@ -1,4 +1,4 @@ -pub use self::inner::{FromToNativeWasmType, HostFunction, WasmTypeList}; +pub use self::inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, WithoutEnv}; use crate::js::exports::{ExportError, Exportable}; use crate::js::externals::Extern; use crate::js::function_env::FunctionEnvMut; @@ -65,7 +65,6 @@ impl Function { /// /// If you know the signature of the host function at compile time, /// consider using [`Function::new_typed`] for less runtime overhead. - #[cfg(feature = "compiler")] pub fn new(store: &mut impl AsStoreMut, ty: FT, func: F) -> Self where FT: Into, @@ -182,6 +181,49 @@ impl Function { Self::from_vm_export(&mut store, vm_function) } + #[deprecated( + since = "3.0.0", + note = "new_native() has been renamed to new_typed()." + )] + /// Creates a new host `Function` from a native function. + pub fn new_native(store: &mut impl AsStoreMut, func: F) -> Self + where + F: HostFunction<(), Args, Rets, WithoutEnv> + 'static + Send + Sync, + Args: WasmTypeList, + Rets: WasmTypeList, + { + Self::new_typed(store, func) + } + + /// Creates a new host `Function` from a native function. + pub fn new_typed(store: &mut impl AsStoreMut, func: F) -> Self + where + F: HostFunction<(), Args, Rets, WithoutEnv> + 'static + Send + Sync, + Args: WasmTypeList, + Rets: WasmTypeList, + { + let mut store = store.as_store_mut(); + if std::mem::size_of::() != 0 { + Self::closures_unsupported_panic(); + } + let function = inner::Function::::new(func); + let address = function.address() as usize as u32; + + let ft = wasm_bindgen::function_table(); + let as_table = ft.unchecked_ref::(); + let func = as_table.get(address).unwrap(); + + let binded_func = func.bind1( + &JsValue::UNDEFINED, + &JsValue::from_f64(store.as_raw() as *mut u8 as usize as f64), + ); + let ty = function.ty(); + let vm_function = VMFunction::new(binded_func, ty); + Self { + handle: StoreHandle::new(store.objects_mut(), vm_function), + } + } + #[deprecated( since = "3.0.0", note = "new_native_with_env() has been renamed to new_typed_with_env()." @@ -193,7 +235,7 @@ impl Function { func: F, ) -> Self where - F: HostFunction, + F: HostFunction, Args: WasmTypeList, Rets: WasmTypeList, { @@ -223,7 +265,7 @@ impl Function { func: F, ) -> Self where - F: HostFunction, + F: HostFunction, Args: WasmTypeList, Rets: WasmTypeList, { @@ -840,10 +882,11 @@ mod inner { /// can be used as host function. To uphold this statement, it is /// necessary for a function to be transformed into a pointer to /// `VMFunctionBody`. - pub trait HostFunction + pub trait HostFunction where Args: WasmTypeList, Rets: WasmTypeList, + Kind: HostFunctionKind, T: Sized, Self: Sized, { @@ -851,6 +894,27 @@ mod inner { fn function_body_ptr(self) -> *const VMFunctionBody; } + /// Empty trait to specify the kind of `HostFunction`: With or + /// without an environment. + /// + /// This trait is never aimed to be used by a user. It is used by + /// the trait system to automatically generate the appropriate + /// host functions. + #[doc(hidden)] + pub trait HostFunctionKind {} + + /// An empty struct to help Rust typing to determine + /// when a `HostFunction` does have an environment. + pub struct WithEnv; + + impl HostFunctionKind for WithEnv {} + + /// An empty struct to help Rust typing to determine + /// when a `HostFunction` does not have an environment. + pub struct WithoutEnv; + + impl HostFunctionKind for WithoutEnv {} + /// Represents a low-level Wasm static host function. See /// `super::Function::new` and `super::Function::new_env` to learn /// more. @@ -869,9 +933,9 @@ mod inner { { /// Creates a new `Function`. #[allow(dead_code)] - pub fn new(function: F) -> Self + pub fn new(function: F) -> Self where - F: HostFunction, + F: HostFunction, T: Sized, { Self { @@ -1013,10 +1077,11 @@ mod inner { } } - // Implement `HostFunction` for a function that has the same arity than the tuple. + // Implement `HostFunction` for a function with a [`FunctionEnvMut`] that has the same + // arity than the tuple. #[allow(unused_parens)] impl< $( $x, )* Rets, RetsAsResult, T, Func > - HostFunction + HostFunction for Func where @@ -1061,6 +1126,50 @@ mod inner { func_wrapper::< T, $( $x, )* Rets, RetsAsResult, Self > as *const VMFunctionBody } } + + // Implement `HostFunction` for a function that has the same arity than the tuple. + #[allow(unused_parens)] + impl< $( $x, )* Rets, RetsAsResult, Func > + HostFunction<(), ( $( $x ),* ), Rets, WithoutEnv> + for + Func + where + $( $x: FromToNativeWasmType, )* + Rets: WasmTypeList, + RetsAsResult: IntoResult, + Func: Fn($( $x , )*) -> RetsAsResult + 'static, + { + #[allow(non_snake_case)] + fn function_body_ptr(self) -> *const VMFunctionBody { + /// This is a function that wraps the real host + /// function. Its address will be used inside the + /// runtime. + unsafe extern "C" fn func_wrapper<$( $x, )* Rets, RetsAsResult, Func>( store_ptr: usize, $( $x: <$x::Native as NativeWasmType>::Abi, )* ) -> Rets::CStruct + where + $( $x: FromToNativeWasmType, )* + Rets: WasmTypeList, + RetsAsResult: IntoResult, + Func: Fn($( $x , )*) -> RetsAsResult + 'static, + { + // let env: &Env = unsafe { &*(ptr as *const u8 as *const Env) }; + let func: &Func = &*(&() as *const () as *const Func); + let mut store = StoreMut::from_raw(store_ptr as *mut _); + + let result = panic::catch_unwind(AssertUnwindSafe(|| { + func($( FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x)) ),* ).into_result() + })); + + match result { + Ok(Ok(result)) => return result.into_c_struct(&mut store), + #[allow(deprecated)] + Ok(Err(trap)) => RuntimeError::raise(Box::new(trap)), + Err(_panic) => unimplemented!(), + } + } + + func_wrapper::< $( $x, )* Rets, RetsAsResult, Self > as *const VMFunctionBody + } + } }; } diff --git a/lib/api/src/js/native.rs b/lib/api/src/js/native.rs index d2056323d03..0e9066edac8 100644 --- a/lib/api/src/js/native.rs +++ b/lib/api/src/js/native.rs @@ -5,7 +5,7 @@ //! //! ```ignore //! let add_one = instance.exports.get_function("function_name")?; -//! let add_one_native: TypedFunction = add_one.native().unwrap(); +//! let add_one_native: TypedFunction = add_one.typed().unwrap(); //! ``` use std::marker::PhantomData; @@ -37,11 +37,7 @@ where Rets: WasmTypeList, { #[allow(dead_code)] - pub(crate) fn new( - store: &mut impl AsStoreMut, - _env: &FunctionEnv, - vm_function: VMFunction, - ) -> Self { + pub(crate) fn new(store: &mut impl AsStoreMut, vm_function: VMFunction) -> Self { Self { handle: StoreHandle::new(store.as_store_mut().objects_mut(), vm_function), _phantom: PhantomData, @@ -108,7 +104,7 @@ macro_rules! impl_native_traits { { 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(store).map_err(|_| crate::js::exports::ExportError::IncompatibleType) + crate::js::Function::get_self_from_extern(_extern)?.typed(store).map_err(|_| crate::js::exports::ExportError::IncompatibleType) } } }; diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 777ace97093..663903104ba 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -151,7 +151,7 @@ //! let memory = Memory::new(&mut store, MemoryType::new(1, None, false)).unwrap(); //! imports! { //! "env" => { -//! "my_function" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>| println!("Hello")), +//! "my_function" => Function::new_typed(&mut store, || println!("Hello")), //! "memory" => memory, //! } //! } diff --git a/lib/api/src/sys/externals/function.rs b/lib/api/src/sys/externals/function.rs index f5419cc2f05..1aa1c5f03c9 100644 --- a/lib/api/src/sys/externals/function.rs +++ b/lib/api/src/sys/externals/function.rs @@ -7,7 +7,7 @@ use crate::sys::TypedFunction; use crate::{FunctionEnv, FunctionEnvMut, Value}; use inner::StaticFunction; -pub use inner::{FromToNativeWasmType, HostFunction, WasmTypeList}; +pub use inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, WithoutEnv}; use std::cell::UnsafeCell; use std::cmp::max; use std::ffi::c_void; @@ -188,7 +188,7 @@ impl Function { /// Creates a new host `Function` from a native function. pub fn new_native(store: &mut impl AsStoreMut, func: F) -> Self where - F: HostFunction<(), Args, Rets> + 'static + Send + Sync, + F: HostFunction<(), Args, Rets, WithoutEnv> + 'static + Send + Sync, Args: WasmTypeList, Rets: WasmTypeList, { @@ -199,12 +199,44 @@ impl Function { /// Creates a new host `Function` from a native function. pub fn new_typed(store: &mut impl AsStoreMut, func: F) -> Self where - F: HostFunction<(), Args, Rets> + 'static + Send + Sync, + F: HostFunction<(), Args, Rets, WithoutEnv> + 'static + Send + Sync, Args: WasmTypeList, Rets: WasmTypeList, { let env = FunctionEnv::new(store, ()); - Self::new_typed_with_env(store, &env, func) + let host_data = Box::new(StaticFunction { + raw_store: store.as_store_mut().as_raw() as *mut u8, + env, + func, + }); + let function_type = FunctionType::new(Args::wasm_types(), Rets::wasm_types()); + + let func_ptr = >::function_body_ptr(); + let type_index = store + .as_store_mut() + .engine() + .register_signature(&function_type); + let vmctx = VMFunctionContext { + host_env: host_data.as_ref() as *const _ as *mut c_void, + }; + let call_trampoline = + >::call_trampoline_address(); + let anyfunc = VMCallerCheckedAnyfunc { + func_ptr, + type_index, + vmctx, + call_trampoline, + }; + + let vm_function = VMFunction { + anyfunc: MaybeInstanceOwned::Host(Box::new(UnsafeCell::new(anyfunc))), + kind: VMFunctionKind::Static, + signature: function_type, + host_data, + }; + Self { + handle: StoreHandle::new(store.as_store_mut().objects_mut(), vm_function), + } } #[cfg(feature = "compiler")] @@ -219,7 +251,7 @@ impl Function { func: F, ) -> Self where - F: HostFunction + 'static + Send + Sync, + F: HostFunction + 'static + Send + Sync, Args: WasmTypeList, Rets: WasmTypeList, { @@ -251,7 +283,7 @@ impl Function { func: F, ) -> Self where - F: HostFunction + 'static + Send + Sync, + F: HostFunction + 'static + Send + Sync, Args: WasmTypeList, Rets: WasmTypeList, { @@ -264,7 +296,7 @@ impl Function { }); let function_type = FunctionType::new(Args::wasm_types(), Rets::wasm_types()); - let func_ptr = >::function_body_ptr(); + let func_ptr = >::function_body_ptr(); let type_index = store .as_store_mut() .engine() @@ -272,7 +304,8 @@ impl Function { let vmctx = VMFunctionContext { host_env: host_data.as_ref() as *const _ as *mut c_void, }; - let call_trampoline = >::call_trampoline_address(); + let call_trampoline = + >::call_trampoline_address(); let anyfunc = VMCallerCheckedAnyfunc { func_ptr, type_index, @@ -1052,10 +1085,11 @@ mod inner { /// can be used as host function. To uphold this statement, it is /// necessary for a function to be transformed into a pointer to /// `VMFunctionBody`. - pub trait HostFunction + pub trait HostFunction where Args: WasmTypeList, Rets: WasmTypeList, + Kind: HostFunctionKind, { /// Get the pointer to the function body. fn function_body_ptr() -> *const VMFunctionBody; @@ -1064,6 +1098,27 @@ mod inner { fn call_trampoline_address() -> VMTrampoline; } + /// Empty trait to specify the kind of `HostFunction`: With or + /// without an environment. + /// + /// This trait is never aimed to be used by a user. It is used by + /// the trait system to automatically generate the appropriate + /// host functions. + #[doc(hidden)] + pub trait HostFunctionKind {} + + /// An empty struct to help Rust typing to determine + /// when a `HostFunction` does have an environment. + pub struct WithEnv; + + impl HostFunctionKind for WithEnv {} + + /// An empty struct to help Rust typing to determine + /// when a `HostFunction` does not have an environment. + pub struct WithoutEnv; + + impl HostFunctionKind for WithoutEnv {} + /// Represents a low-level Wasm static host function. See /// [`super::Function::new_typed`] and /// [`super::Function::new_typed_with_env`] to learn more. @@ -1189,10 +1244,11 @@ mod inner { } } - // Implement `HostFunction` for a function that has the same arity than the tuple. + // Implement `HostFunction` for a function with a [`FunctionEnvMut`] that has the same + // arity than the tuple. #[allow(unused_parens)] impl< $( $x, )* Rets, RetsAsResult, T: Send + 'static, Func > - HostFunction + HostFunction for Func where @@ -1272,6 +1328,83 @@ mod inner { } } + + // Implement `HostFunction` for a function that has the same arity than the tuple. + #[allow(unused_parens)] + impl< $( $x, )* Rets, RetsAsResult, Func > + HostFunction<(), ( $( $x ),* ), Rets, WithoutEnv> + for + Func + where + $( $x: FromToNativeWasmType, )* + Rets: WasmTypeList, + RetsAsResult: IntoResult, + Func: Fn($( $x , )*) -> RetsAsResult + 'static, + { + #[allow(non_snake_case)] + fn function_body_ptr() -> *const VMFunctionBody { + /// This is a function that wraps the real host + /// function. Its address will be used inside the + /// runtime. + unsafe extern "C" fn func_wrapper<$( $x, )* Rets, RetsAsResult, Func>( env: &StaticFunction, $( $x: <$x::Native as NativeWasmType>::Abi, )* ) -> Rets::CStruct + where + $( $x: FromToNativeWasmType, )* + Rets: WasmTypeList, + RetsAsResult: IntoResult, + Func: Fn($( $x , )*) -> RetsAsResult + 'static, + { + // println!("func wrapper"); + let mut store = StoreMut::from_raw(env.raw_store as *mut _); + let result = on_host_stack(|| { + // println!("func wrapper1"); + panic::catch_unwind(AssertUnwindSafe(|| { + $( + let $x = FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x)); + )* + (env.func)($($x),* ).into_result() + })) + }); + + match result { + Ok(Ok(result)) => return result.into_c_struct(&mut store), + Ok(Err(trap)) => raise_user_trap(Box::new(trap)), + Err(panic) => resume_panic(panic) , + } + } + + func_wrapper::< $( $x, )* Rets, RetsAsResult, Self > as *const VMFunctionBody + } + + #[allow(non_snake_case)] + fn call_trampoline_address() -> VMTrampoline { + unsafe extern "C" fn call_trampoline< + $( $x: FromToNativeWasmType, )* + Rets: WasmTypeList, + >( + vmctx: *mut VMContext, + body: *const VMFunctionBody, + args: *mut RawValue, + ) { + let body: unsafe extern "C" fn( + vmctx: *mut VMContext, + $( $x: <$x::Native as NativeWasmType>::Abi, )* + ) -> Rets::CStruct + = std::mem::transmute(body); + + let mut _n = 0; + $( + let $x = *args.add(_n).cast(); + _n += 1; + )* + + let results = body(vmctx, $( $x ),*); + Rets::write_c_struct_to_ptr(results, args); + } + + call_trampoline::<$( $x, )* Rets> + } + + } }; } diff --git a/lib/api/src/sys/imports.rs b/lib/api/src/sys/imports.rs index c6e279bf533..c9a9f22b917 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(_env: FunctionEnvMut<()>, n: i32) -> i32 { +/// fn foo(n: i32) -> i32 { /// n /// } /// @@ -100,7 +100,7 @@ impl Imports { /// # use wasmer::{FunctionEnv, Store}; /// # let mut store: Store = Default::default(); /// use wasmer::{StoreMut, Imports, Function, FunctionEnvMut}; - /// fn foo(_env: FunctionEnvMut<()>, n: i32) -> i32 { + /// fn foo(n: i32) -> i32 { /// n /// } /// let mut import_object = Imports::new(); @@ -219,7 +219,7 @@ impl fmt::Debug for Imports { /// }, /// }; /// -/// fn foo(_env: FunctionEnvMut<()>, n: i32) -> i32 { +/// fn foo(n: i32) -> i32 { /// n /// } /// ``` @@ -297,11 +297,10 @@ mod test { #[test] fn imports_macro_allows_trailing_comma_and_none() { use crate::sys::Function; - use crate::sys::FunctionEnvMut; let mut store: Store = Default::default(); - fn func(_env: FunctionEnvMut<()>, arg: i32) -> i32 { + fn func(arg: i32) -> i32 { arg + 1 } diff --git a/lib/api/tests/js_externals.rs b/lib/api/tests/js_externals.rs index 71b81cff0c1..4795c1ba49e 100644 --- a/lib/api/tests/js_externals.rs +++ b/lib/api/tests/js_externals.rs @@ -259,49 +259,43 @@ mod js { #[wasm_bindgen_test] fn function_new_dynamic() { let mut store = Store::default(); - let env = FunctionEnv::new(&mut store, ()); // Using &FunctionType signature let function_type = FunctionType::new(vec![], vec![]); let function = Function::new( &mut store, - &env, &function_type, - |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32], vec![]); let function = Function::new( &mut store, - &env, &function_type, - |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]); let function = Function::new( &mut store, - &env, &function_type, - |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32]); let function = Function::new( &mut store, - &env, &function_type, - |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]); let function = Function::new( &mut store, - &env, &function_type, - |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).clone(), function_type); @@ -309,9 +303,8 @@ mod js { let function_type = ([Type::V128], [Type::I32, Type::F32, Type::F64]); let function = Function::new( &mut store, - &env, function_type, - |_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&store).params(), [Type::V128]); assert_eq!( @@ -331,7 +324,7 @@ mod js { // Using &FunctionType signature let function_type = FunctionType::new(vec![], vec![]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, &function_type, @@ -339,7 +332,7 @@ mod js { ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32], vec![]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, &function_type, @@ -348,7 +341,7 @@ mod js { assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, &function_type, @@ -356,7 +349,7 @@ mod js { ); assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, &function_type, @@ -365,7 +358,7 @@ mod js { assert_eq!(function.ty(&store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, &function_type, @@ -375,7 +368,7 @@ mod js { // Using array signature let function_type = ([Type::V128], [Type::I32, Type::F32, Type::F64]); - let function = Function::new( + let function = Function::new_with_env( &mut store, &env, function_type, @@ -391,7 +384,6 @@ mod js { #[wasm_bindgen_test] fn native_function_works() { let mut store = Store::default(); - let mut env = FunctionEnv::new(&mut store, ()); let function = Function::new_typed(&mut store, || {}); let typed_function: TypedFunction<(), ()> = function.typed(&mut store).unwrap(); let result = typed_function.call(&mut store); diff --git a/lib/api/tests/js_instance.rs b/lib/api/tests/js_instance.rs index c61519801c1..4b829ab810d 100644 --- a/lib/api/tests/js_instance.rs +++ b/lib/api/tests/js_instance.rs @@ -119,11 +119,10 @@ mod js { ))], }) .unwrap(); - let env = FunctionEnv::new(&mut store, ()); let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let imported = Function::new(&mut store, &env, imported_signature, |_env, args| { + let imported = Function::new(&mut store, imported_signature, |args| { log!("Calling `imported`..."); let result = args[0].unwrap_i32() * 2; log!("Result of `imported`: {:?}", result); @@ -239,12 +238,13 @@ mod js { let 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, |env, args| { - log!("Calling `imported`..."); - let result = args[0].unwrap_i32() * env.data().multiplier; - log!("Result of `imported`: {:?}", result); - Ok(vec![Value::I32(result)]) - }); + let imported = + Function::new_with_env(&mut store, &env, &imported_signature, |env, args| { + log!("Calling `imported`..."); + let result = args[0].unwrap_i32() * env.data().multiplier; + log!("Result of `imported`: {:?}", result); + Ok(vec![Value::I32(result)]) + }); let import_object = imports! { "env" => { @@ -287,7 +287,7 @@ mod js { }) .unwrap(); - fn imported_fn(_: FunctionEnvMut<'_, ()>, arg: u32) -> u32 { + fn imported_fn(arg: u32) -> u32 { return arg + 1; } @@ -453,16 +453,13 @@ mod js { let env = FunctionEnv::new(&mut store, Env { multiplier: 3 }); - fn imported_fn( - env: FunctionEnvMut<'_, Env>, - args: &[Val], - ) -> Result, RuntimeError> { + fn imported_fn(env: FunctionEnvMut, args: &[Val]) -> Result, RuntimeError> { let value = env.data().multiplier * args[0].unwrap_i32() as u32; return Ok(vec![Val::I32(value as _)]); } let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let imported = Function::new(&mut store, &env, imported_signature, imported_fn); + let imported = Function::new_with_env(&mut store, &env, imported_signature, imported_fn); let expected = vec![Val::I32(12)].into_boxed_slice(); assert_eq!(imported.call(&mut store, &[Val::I32(4)]), Ok(expected)); @@ -522,7 +519,7 @@ mod js { ); let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let imported = Function::new(&mut store, &env, imported_signature, imported_fn); + let imported = Function::new_with_env(&mut store, &env, imported_signature, imported_fn); let import_object = imports! { "env" => { @@ -625,7 +622,7 @@ mod js { ) .unwrap(); - fn sum(_: FunctionEnvMut<'_, ()>, a: i32, b: i32) -> i32 { + fn sum(a: i32, b: i32) -> i32 { a + b } @@ -664,7 +661,7 @@ mod js { ) .unwrap(); - fn early_exit(_: FunctionEnvMut<'_, ()>) { + fn early_exit() { panic!("Do panic") } @@ -727,7 +724,7 @@ mod js { impl std::error::Error for ExitCode {} - fn early_exit(_: FunctionEnvMut<'_, ()>) -> Result<(), ExitCode> { + fn early_exit() -> Result<(), ExitCode> { Err(ExitCode(1)) } diff --git a/lib/api/tests/sys_externals.rs b/lib/api/tests/sys_externals.rs index 9bb6630b8bf..1719dc4b5cb 100644 --- a/lib/api/tests/sys_externals.rs +++ b/lib/api/tests/sys_externals.rs @@ -69,7 +69,7 @@ mod sys { minimum: 0, maximum: None, }; - let f = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>| {}); + let f = Function::new_typed(&mut store, || {}); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f)))?; assert_eq!(table.ty(&mut store), table_type); @@ -94,7 +94,7 @@ mod sys { minimum: 0, maximum: Some(1), }; - let f = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, num: i32| num + 1); + let f = Function::new_typed(&mut store, |num: i32| num + 1); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f)))?; assert_eq!(table.ty(&mut store), table_type); let _elem = table.get(&mut store, 0).unwrap(); @@ -117,7 +117,7 @@ mod sys { minimum: 0, maximum: Some(10), }; - let f = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, num: i32| num + 1); + let f = Function::new_typed(&mut store, |num: i32| num + 1); let table = Table::new(&mut store, table_type, Value::FuncRef(Some(f.clone())))?; // Growing to a bigger maximum should return None let old_len = table.grow(&mut store, 12, Value::FuncRef(Some(f.clone()))); @@ -182,33 +182,28 @@ mod sys { #[test] fn function_new() -> Result<()> { let mut store = Store::default(); - let function = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>| {}); + let function = Function::new_typed(&mut store, || {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![]) ); - let function = Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, _a: i32| {}); + let function = Function::new_typed(&mut store, |_a: i32| {}); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![Type::I32], vec![]) ); - let function = Function::new_typed( - &mut store, - |_env: FunctionEnvMut<()>, _a: i32, _b: i64, _c: f32, _d: f64| {}, - ); + let function = Function::new_typed(&mut store, |_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_typed(&mut store, |_env: FunctionEnvMut<()>| -> i32 { 1 }); + let function = Function::new_typed(&mut store, || -> i32 { 1 }); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![Type::I32]) ); - let function = Function::new_typed( - &mut store, - |_env: FunctionEnvMut<()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, - ); + let function = + Function::new_typed(&mut store, || -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }); assert_eq!( function.ty(&mut store).clone(), FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]) diff --git a/lib/api/tests/sys_module.rs b/lib/api/tests/sys_module.rs index 521ad320741..633fa7a477e 100644 --- a/lib/api/tests/sys_module.rs +++ b/lib/api/tests/sys_module.rs @@ -192,35 +192,35 @@ mod sys { let module = Module::new(&store, wat)?; let imports = imports! { "host" => { - "host_func1" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: u64| { + "host_func1" => Function::new_typed(&mut store, |p: u64| { println!("host_func1: Found number {}", p); assert_eq!(p, u64::max_value()); }), - "host_func2" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: u32| { + "host_func2" => Function::new_typed(&mut store, |p: u32| { println!("host_func2: Found number {}", p); assert_eq!(p, u32::max_value()); }), - "host_func3" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: i64| { + "host_func3" => Function::new_typed(&mut store, |p: i64| { println!("host_func3: Found number {}", p); assert_eq!(p, -1); }), - "host_func4" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: i32| { + "host_func4" => Function::new_typed(&mut store, |p: i32| { println!("host_func4: Found number {}", p); assert_eq!(p, -1); }), - "host_func5" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: i16| { + "host_func5" => Function::new_typed(&mut store, |p: i16| { println!("host_func5: Found number {}", p); assert_eq!(p, -1); }), - "host_func6" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: u16| { + "host_func6" => Function::new_typed(&mut store, |p: u16| { println!("host_func6: Found number {}", p); assert_eq!(p, u16::max_value()); }), - "host_func7" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: i8| { + "host_func7" => Function::new_typed(&mut store, |p: i8| { println!("host_func7: Found number {}", p); assert_eq!(p, -1); }), - "host_func8" => Function::new_typed(&mut store, |_env: FunctionEnvMut<()>, p: u8| { + "host_func8" => Function::new_typed(&mut store, |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 f2cf89b3a81..ca063dbf084 100644 --- a/lib/api/tests/sys_reference_types.rs +++ b/lib/api/tests/sys_reference_types.rs @@ -109,7 +109,7 @@ mod sys { let instance = Instance::new(&mut store, &module, &imports)?; { - fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 { + fn sum(a: i32, b: i32) -> i32 { a + b } let sum_func = Function::new_typed(&mut store, sum); diff --git a/tests/compilers/imports.rs b/tests/compilers/imports.rs index 696a86b8594..60d452ac62f 100644 --- a/tests/compilers/imports.rs +++ b/tests/compilers/imports.rs @@ -180,30 +180,27 @@ fn static_function(config: crate::Config) -> Result<()> { let module = get_module(&store)?; static HITS: AtomicUsize = AtomicUsize::new(0); - let f0 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>| { + let f0 = Function::new_typed(&mut store, || { assert_eq!(HITS.fetch_add(1, SeqCst), 0); }); - let f1 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>, x: i32| -> i32 { + let f1 = Function::new_typed(&mut store, |x: i32| -> i32 { assert_eq!(x, 0); assert_eq!(HITS.fetch_add(1, SeqCst), 1); 1 }); - let f2 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>, x: i32, y: i64| { + let f2 = Function::new_typed(&mut store, |x: i32, y: i64| { assert_eq!(x, 2); assert_eq!(y, 3); assert_eq!(HITS.fetch_add(1, SeqCst), 2); }); - let f3 = Function::new_typed( - &mut store, - |_env: FunctionEnvMut<_>, a: i32, b: i64, c: i32, d: f32, e: f64| { - assert_eq!(a, 100); - assert_eq!(b, 200); - assert_eq!(c, 300); - assert_eq!(d, 400.0); - assert_eq!(e, 500.0); - assert_eq!(HITS.fetch_add(1, SeqCst), 3); - }, - ); + let f3 = Function::new_typed(&mut store, |a: i32, b: i64, c: i32, d: f32, e: f64| { + assert_eq!(a, 100); + assert_eq!(b, 200); + assert_eq!(c, 300); + assert_eq!(d, 400.0); + assert_eq!(e, 500.0); + assert_eq!(HITS.fetch_add(1, SeqCst), 3); + }); Instance::new( &mut store, &module, @@ -227,33 +224,27 @@ fn static_function_with_results(config: crate::Config) -> Result<()> { let module = get_module(&store)?; static HITS: AtomicUsize = AtomicUsize::new(0); - let f0 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>| { + let f0 = Function::new_typed(&mut store, || { assert_eq!(HITS.fetch_add(1, SeqCst), 0); }); - let f1 = Function::new_typed( - &mut store, - |_env: FunctionEnvMut<_>, x: i32| -> Result { - assert_eq!(x, 0); - assert_eq!(HITS.fetch_add(1, SeqCst), 1); - Ok(1) - }, - ); - let f2 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>, x: i32, y: i64| { + let f1 = Function::new_typed(&mut store, |x: i32| -> Result { + assert_eq!(x, 0); + assert_eq!(HITS.fetch_add(1, SeqCst), 1); + Ok(1) + }); + let f2 = Function::new_typed(&mut store, |x: i32, y: i64| { assert_eq!(x, 2); assert_eq!(y, 3); assert_eq!(HITS.fetch_add(1, SeqCst), 2); }); - let f3 = Function::new_typed( - &mut store, - |_env: FunctionEnvMut<_>, a: i32, b: i64, c: i32, d: f32, e: f64| { - assert_eq!(a, 100); - assert_eq!(b, 200); - assert_eq!(c, 300); - assert_eq!(d, 400.0); - assert_eq!(e, 500.0); - assert_eq!(HITS.fetch_add(1, SeqCst), 3); - }, - ); + let f3 = Function::new_typed(&mut store, |a: i32, b: i64, c: i32, d: f32, e: f64| { + assert_eq!(a, 100); + assert_eq!(b, 200); + assert_eq!(c, 300); + assert_eq!(d, 400.0); + assert_eq!(e, 500.0); + assert_eq!(HITS.fetch_add(1, SeqCst), 3); + }); Instance::new( &mut store, &module, @@ -349,14 +340,9 @@ fn static_function_that_fails(config: crate::Config) -> Result<()> { "#; let module = Module::new(&store, &wat)?; - let mut env = FunctionEnv::new(&mut store, ()); - let f0 = Function::new_typed_with_env( - &mut store, - &env, - |_env: FunctionEnvMut<_>| -> Result { - Err(RuntimeError::new("oops")) - }, - ); + let f0 = Function::new_typed(&mut store, || -> Result { + Err(RuntimeError::new("oops")) + }); let result = Instance::new( &mut store, &module, diff --git a/tests/compilers/traps.rs b/tests/compilers/traps.rs index 7f7dbc2bd6d..5c0e6a20d08 100644 --- a/tests/compilers/traps.rs +++ b/tests/compilers/traps.rs @@ -305,9 +305,7 @@ fn rust_panic_import(config: crate::Config) -> Result<()> { let module = Module::new(&store, &binary)?; let sig = FunctionType::new(vec![], vec![]); let func = Function::new(&mut store, &sig, |_| panic!("this is a panic")); - let f0 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>| { - panic!("this is another panic") - }); + let f0 = Function::new_typed(&mut store, || panic!("this is another panic")); let instance = Instance::new( &mut store, &module, @@ -366,9 +364,7 @@ fn rust_panic_start_function(config: crate::Config) -> Result<()> { .unwrap_err(); assert_eq!(err.downcast_ref::<&'static str>(), Some(&"this is a panic")); - let func = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>| { - panic!("this is another panic") - }); + let func = Function::new_typed(&mut store, || panic!("this is another panic")); let err = panic::catch_unwind(AssertUnwindSafe(|| { drop(Instance::new( &mut store, diff --git a/tests/compilers/typed_functions.rs b/tests/compilers/typed_functions.rs index 6cee8ad9110..c6c09b07272 100644 --- a/tests/compilers/typed_functions.rs +++ b/tests/compilers/typed_functions.rs @@ -6,19 +6,7 @@ use wasmer::FunctionEnv; use wasmer::Type as ValueType; use wasmer::*; -fn long_f( - _env: FunctionEnvMut<()>, - a: u32, - b: u32, - c: u32, - d: u32, - e: u32, - f: u16, - g: u64, - h: u64, - i: u16, - j: u32, -) -> u64 { +fn long_f(a: u32, b: u32, c: u32, d: u32, e: u32, f: u16, g: u64, h: u64, i: u16, j: u32) -> u64 { j as u64 + i as u64 * 10 + h * 100 @@ -95,7 +83,7 @@ fn typed_function_works_for_wasm(config: crate::Config) -> anyhow::Result<()> { fn typed_host_function_closure_panics(config: crate::Config) { let mut store = config.store(); let state = 3; - Function::new_typed(&mut store, move |_env: FunctionEnvMut<_>, _: i32| { + Function::new_typed(&mut store, move |_: i32| { println!("{}", state); }); } @@ -255,22 +243,15 @@ fn typed_function_works_for_wasm_function_manyparams_dynamic( fn static_host_function_without_env(config: crate::Config) -> anyhow::Result<()> { let mut store = config.store(); - fn f(_env: FunctionEnvMut<()>, a: i32, b: i64, c: f32, d: f64) -> (f64, f32, i64, i32) { + fn f(a: i32, b: i64, c: f32, d: f64) -> (f64, f32, i64, i32) { (d * 4.0, c * 3.0, b * 2, a * 1) } - fn f_ok( - _env: FunctionEnvMut<()>, - a: i32, - b: i64, - c: f32, - d: f64, - ) -> Result<(f64, f32, i64, i32), Infallible> { + fn f_ok(a: i32, b: i64, c: f32, d: f64) -> Result<(f64, f32, i64, i32), Infallible> { Ok((d * 4.0, c * 3.0, b * 2, a * 1)) } fn long_f( - _env: FunctionEnvMut<()>, a: u32, b: u32, c: u32, diff --git a/tests/lib/wast/src/spectest.rs b/tests/lib/wast/src/spectest.rs index 16e3aea21ed..9c2433ecd48 100644 --- a/tests/lib/wast/src/spectest.rs +++ b/tests/lib/wast/src/spectest.rs @@ -3,24 +3,16 @@ use wasmer::*; /// Return an instance implementing the "spectest" interface used in the /// spec testsuite. pub fn spectest_importobject(store: &mut Store) -> Imports { - let print = Function::new_typed(store, |_: FunctionEnvMut<()>| {}); - let print_i32 = Function::new_typed(store, |_: FunctionEnvMut<()>, val: i32| { - println!("{}: i32", val) - }); - let print_i64 = Function::new_typed(store, |_: FunctionEnvMut<()>, val: i64| { - println!("{}: i64", val) - }); - let print_f32 = Function::new_typed(store, |_: FunctionEnvMut<()>, val: f32| { - println!("{}: f32", val) - }); - let print_f64 = Function::new_typed(store, |_: FunctionEnvMut<()>, val: f64| { - println!("{}: f64", val) - }); - let print_i32_f32 = Function::new_typed(store, |_: FunctionEnvMut<()>, i: i32, f: f32| { + let print = Function::new_typed(store, || {}); + let print_i32 = Function::new_typed(store, |val: i32| println!("{}: i32", val)); + let print_i64 = Function::new_typed(store, |val: i64| println!("{}: i64", val)); + let print_f32 = Function::new_typed(store, |val: f32| println!("{}: f32", val)); + let print_f64 = Function::new_typed(store, |val: f64| println!("{}: f64", val)); + let print_i32_f32 = Function::new_typed(store, |i: i32, f: f32| { println!("{}: i32", i); println!("{}: f32", f); }); - let print_f64_f64 = Function::new_typed(store, |_: FunctionEnvMut<()>, f1: f64, f2: f64| { + let print_f64_f64 = Function::new_typed(store, |f1: f64, f2: f64| { println!("{}: f64", f1); println!("{}: f64", f2); }); From 9c7b4343ea5a38e4c4c1ac03c898be0bb7fd783a Mon Sep 17 00:00:00 2001 From: Wolfgang Silbermayr Date: Thu, 4 Aug 2022 20:39:13 +0200 Subject: [PATCH 5/9] rustfmt --- examples/exports_memory.rs | 6 +- examples/memory.rs | 10 ++-- lib/api/src/js/externals/memory.rs | 23 ++++++-- lib/api/src/js/externals/memory_view.rs | 70 ++++++++++++------------ lib/api/src/js/mem_access.rs | 8 +-- lib/api/src/js/mod.rs | 4 +- lib/api/src/js/ptr.rs | 6 +- lib/api/src/sys/externals/memory.rs | 23 ++++++-- lib/api/src/sys/externals/memory_view.rs | 32 +++-------- lib/api/src/sys/mem_access.rs | 8 +-- lib/api/src/sys/mod.rs | 3 +- lib/api/src/sys/ptr.rs | 11 +--- lib/compiler/src/traits.rs | 2 +- lib/emscripten/src/env/mod.rs | 6 +- lib/emscripten/src/env/unix/mod.rs | 24 +++++--- lib/emscripten/src/env/windows/mod.rs | 4 +- lib/emscripten/src/memory.rs | 7 ++- lib/emscripten/src/syscalls/mod.rs | 26 +++++---- lib/emscripten/src/syscalls/unix.rs | 12 ++-- lib/emscripten/src/syscalls/windows.rs | 2 +- lib/emscripten/src/time.rs | 20 ++++--- lib/emscripten/src/utils.rs | 5 +- lib/emscripten/src/varargs.rs | 3 +- lib/wasi/src/lib.rs | 14 +++-- lib/wasi/src/state/pipe.rs | 2 +- lib/wasi/src/state/socket.rs | 2 +- lib/wasi/src/syscalls/mod.rs | 36 +++++------- 27 files changed, 193 insertions(+), 176 deletions(-) diff --git a/examples/exports_memory.rs b/examples/exports_memory.rs index f3b4a7d4b67..5f8b0a9d217 100644 --- a/examples/exports_memory.rs +++ b/examples/exports_memory.rs @@ -56,7 +56,7 @@ fn main() -> Result<(), Box> { // // The Wasm module exports a memory under "mem". Let's get it. let memory = instance.exports.get_memory("mem")?; - + // Now that we have the exported memory, let's get some // information about it. // @@ -80,9 +80,7 @@ fn main() -> Result<(), Box> { // We will get bytes out of the memory so we need to // decode them into a string. let memory_view = memory.view(&store); - let str = ptr - .read_utf8_string(&memory_view, length as u32) - .unwrap(); + let str = ptr.read_utf8_string(&memory_view, length as u32).unwrap(); println!("Memory contents: {:?}", str); // What about changing the contents of the memory with a more diff --git a/examples/memory.rs b/examples/memory.rs index 030d014fc74..52528e3dcb9 100644 --- a/examples/memory.rs +++ b/examples/memory.rs @@ -80,7 +80,7 @@ fn main() -> anyhow::Result<()> { let set_at: TypedFunction<(i32, i32), ()> = instance.exports.get_typed_function(&mut store, "set_at")?; let memory = instance.exports.get_memory("memory")?; - + // We now have an instance ready to be used. // // We will start by querying the most intersting information @@ -96,7 +96,7 @@ fn main() -> anyhow::Result<()> { assert_eq!(memory_view.size(), Pages::from(1)); assert_eq!(memory_view.size().bytes(), Bytes::from(65536 as usize)); assert_eq!(memory_view.data_size(), 65536); - + // Sometimes, the guest module may also export a function to let you // query the memory. Here we have a `mem_size` function, let's try it: let result = mem_size.call(&mut store)?; @@ -111,14 +111,14 @@ fn main() -> anyhow::Result<()> { // A memory can be grown to allow storing more things into it. Let's // see how we can do that: println!("Growing memory..."); - + // Here we are requesting two more pages for our memory. memory.grow(&mut store, 2)?; - + let memory_view = memory.view(&store); assert_eq!(memory_view.size(), Pages::from(3)); assert_eq!(memory_view.data_size(), 65536 * 3); - + // Now that we know how to query and adjust the size of the memory, // let's see how wa can write to it or read from it. // diff --git a/lib/api/src/js/externals/memory.rs b/lib/api/src/js/externals/memory.rs index d181e6c6d89..6c5229ce92f 100644 --- a/lib/api/src/js/externals/memory.rs +++ b/lib/api/src/js/externals/memory.rs @@ -134,7 +134,7 @@ impl Memory { } /// Creates a view into the memory that then allows for - /// read and write + /// read and write pub fn view(&self, store: &impl AsStoreRef) -> MemoryView { MemoryView::new(self, store) } @@ -240,7 +240,12 @@ impl<'a> MemoryBuffer<'a> { let view = unsafe { &*(self.base) }; if end > view.length().into() { #[cfg(feature = "tracing")] - warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), end, view.length()); + warn!( + "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", + buf.len(), + end, + view.length() + ); return Err(MemoryAccessError::HeapOutOfBounds); } view.subarray(offset as _, end as _) @@ -259,7 +264,12 @@ impl<'a> MemoryBuffer<'a> { let view = unsafe { &*(self.base) }; if end > view.length().into() { #[cfg(feature = "tracing")] - warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), end, view.length()); + warn!( + "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", + buf.len(), + end, + view.length() + ); return Err(MemoryAccessError::HeapOutOfBounds); } let buf_ptr = buf.as_mut_ptr() as *mut u8; @@ -276,7 +286,12 @@ impl<'a> MemoryBuffer<'a> { let view = unsafe { &mut *(self.base) }; if end > view.length().into() { #[cfg(feature = "tracing")] - warn!("attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", data.len(), end, view.length()); + warn!( + "attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", + data.len(), + end, + view.length() + ); return Err(MemoryAccessError::HeapOutOfBounds); } view.subarray(offset as _, end as _).copy_from(data); diff --git a/lib/api/src/js/externals/memory_view.rs b/lib/api/src/js/externals/memory_view.rs index bf2b63e0c99..45863229d5f 100644 --- a/lib/api/src/js/externals/memory_view.rs +++ b/lib/api/src/js/externals/memory_view.rs @@ -9,8 +9,8 @@ use tracing::warn; use wasmer_types::{Bytes, Pages}; -use super::Memory; use super::memory::MemoryBuffer; +use super::Memory; /// A WebAssembly `memory` view. /// @@ -25,8 +25,7 @@ pub struct MemoryView<'a> { marker: PhantomData<&'a Memory>, } -impl<'a> MemoryView<'a> -{ +impl<'a> MemoryView<'a> { pub(crate) fn new(memory: &Memory, store: &impl AsStoreRef) -> Self { let buffer = memory .handle @@ -34,17 +33,12 @@ impl<'a> MemoryView<'a> .memory .buffer(); - let size = js_sys::Reflect::get( - &buffer, - &"byteLength".into(), - ) + let size = js_sys::Reflect::get(&buffer, &"byteLength".into()) .unwrap() .as_f64() .unwrap() as u64; - let view = js_sys::Uint8Array::new( - &buffer, - ); + let view = js_sys::Uint8Array::new(&buffer); Self { view, @@ -94,11 +88,7 @@ impl<'a> MemoryView<'a> /// /// This method is guaranteed to be safe (from the host side) in the face of /// concurrent writes. - pub fn read( - &self, - offset: u64, - data: &mut [u8], - ) -> Result<(), MemoryAccessError> { + pub fn read(&self, offset: u64, data: &mut [u8]) -> Result<(), MemoryAccessError> { let view = &self.view; let offset: u32 = offset.try_into().map_err(|_| MemoryAccessError::Overflow)?; let len: u32 = data @@ -108,7 +98,12 @@ impl<'a> MemoryView<'a> let end = offset.checked_add(len).ok_or(MemoryAccessError::Overflow)?; if end > view.length() { #[cfg(feature = "tracing")] - warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", len, end, view.length()); + warn!( + "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", + len, + end, + view.length() + ); Err(MemoryAccessError::HeapOutOfBounds)?; } view.subarray(offset, end).copy_to(data); @@ -119,15 +114,16 @@ impl<'a> MemoryView<'a> /// /// This method is guaranteed to be safe (from the host side) in the face of /// concurrent writes. - pub fn read_u8( - &self, - offset: u64 - ) -> Result { + pub fn read_u8(&self, offset: u64) -> Result { let view = &self.view; let offset: u32 = offset.try_into().map_err(|_| MemoryAccessError::Overflow)?; if offset >= view.length() { #[cfg(feature = "tracing")] - warn!("attempted to read beyond the bounds of the memory view ({} >= {})", offset, view.length()); + warn!( + "attempted to read beyond the bounds of the memory view ({} >= {})", + offset, + view.length() + ); Err(MemoryAccessError::HeapOutOfBounds)?; } Ok(view.get_index(offset)) @@ -157,7 +153,12 @@ impl<'a> MemoryView<'a> let end = offset.checked_add(len).ok_or(MemoryAccessError::Overflow)?; if end > view.length() { #[cfg(feature = "tracing")] - warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", len, end, view.length()); + warn!( + "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", + len, + end, + view.length() + ); Err(MemoryAccessError::HeapOutOfBounds)?; } @@ -179,11 +180,7 @@ impl<'a> MemoryView<'a> /// /// This method is guaranteed to be safe (from the host side) in the face of /// concurrent reads/writes. - pub fn write( - &self, - offset: u64, - data: &[u8], - ) -> Result<(), MemoryAccessError> { + pub fn write(&self, offset: u64, data: &[u8]) -> Result<(), MemoryAccessError> { let offset: u32 = offset.try_into().map_err(|_| MemoryAccessError::Overflow)?; let len: u32 = data .len() @@ -193,7 +190,12 @@ impl<'a> MemoryView<'a> let end = offset.checked_add(len).ok_or(MemoryAccessError::Overflow)?; if end > view.length() { #[cfg(feature = "tracing")] - warn!("attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", len, end, view.length()); + warn!( + "attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", + len, + end, + view.length() + ); Err(MemoryAccessError::HeapOutOfBounds)?; } view.subarray(offset, end).copy_from(data); @@ -204,16 +206,16 @@ impl<'a> MemoryView<'a> /// /// This method is guaranteed to be safe (from the host side) in the face of /// concurrent writes. - pub fn write_u8( - &self, - offset: u64, - val: u8 - ) -> Result<(), MemoryAccessError> { + pub fn write_u8(&self, offset: u64, val: u8) -> Result<(), MemoryAccessError> { let view = &self.view; let offset: u32 = offset.try_into().map_err(|_| MemoryAccessError::Overflow)?; if offset >= view.length() { #[cfg(feature = "tracing")] - warn!("attempted to write beyond the bounds of the memory view ({} >= {})", offset, view.length()); + warn!( + "attempted to write beyond the bounds of the memory view ({} >= {})", + offset, + view.length() + ); Err(MemoryAccessError::HeapOutOfBounds)?; } view.set_index(offset, val); diff --git a/lib/api/src/js/mem_access.rs b/lib/api/src/js/mem_access.rs index c5e4e1f2658..1e697d4cbfb 100644 --- a/lib/api/src/js/mem_access.rs +++ b/lib/api/src/js/mem_access.rs @@ -1,6 +1,6 @@ use crate::js::externals::memory::MemoryBuffer; use crate::js::RuntimeError; -use crate::js::{MemoryView, Memory32, Memory64, WasmPtr}; +use crate::js::{Memory32, Memory64, MemoryView, WasmPtr}; use std::{ convert::TryInto, fmt, @@ -158,11 +158,7 @@ impl<'a, T: ValueType> WasmSlice<'a, T> { /// /// Returns a `MemoryAccessError` if the slice length overflows. #[inline] - pub fn new( - memory: &'a MemoryView, - offset: u64, - len: u64, - ) -> Result { + pub fn new(memory: &'a MemoryView, offset: u64, len: u64) -> Result { let total_len = len .checked_mul(mem::size_of::() as u64) .ok_or(MemoryAccessError::Overflow)?; diff --git a/lib/api/src/js/mod.rs b/lib/api/src/js/mod.rs index 2cbcfa1db7d..14594839a15 100644 --- a/lib/api/src/js/mod.rs +++ b/lib/api/src/js/mod.rs @@ -48,8 +48,8 @@ pub use crate::js::error::{DeserializeError, InstantiationError, SerializeError} pub use crate::js::export::Export; pub use crate::js::exports::{ExportError, Exportable, Exports, ExportsIterator}; pub use crate::js::externals::{ - Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryView, MemoryError, Table, - WasmTypeList, + Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryError, MemoryView, + Table, WasmTypeList, }; pub use crate::js::function_env::{FunctionEnv, FunctionEnvMut}; pub use crate::js::imports::Imports; diff --git a/lib/api/src/js/ptr.rs b/lib/api/src/js/ptr.rs index 3c0b5c3ca94..b7b812c434a 100644 --- a/lib/api/src/js/ptr.rs +++ b/lib/api/src/js/ptr.rs @@ -149,11 +149,7 @@ impl WasmPtr { /// Writes to the address pointed to by this `WasmPtr` in a memory. #[inline] - pub fn write( - self, - view: &MemoryView, - val: T, - ) -> Result<(), MemoryAccessError> { + pub fn write(self, view: &MemoryView, val: T) -> Result<(), MemoryAccessError> { self.deref(view).write(val) } diff --git a/lib/api/src/sys/externals/memory.rs b/lib/api/src/sys/externals/memory.rs index f01a5dafdf7..eeb1005930a 100644 --- a/lib/api/src/sys/externals/memory.rs +++ b/lib/api/src/sys/externals/memory.rs @@ -78,7 +78,7 @@ impl Memory { } /// Creates a view into the memory that then allows for - /// read and write + /// read and write pub fn view<'a>(&self, store: &'a impl AsStoreRef) -> MemoryView<'a> { MemoryView::new(self, store) } @@ -179,7 +179,12 @@ impl<'a> MemoryBuffer<'a> { .ok_or(MemoryAccessError::Overflow)?; if end > self.len.try_into().unwrap() { #[cfg(feature = "tracing")] - warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), end, self.len); + warn!( + "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", + buf.len(), + end, + self.len + ); return Err(MemoryAccessError::HeapOutOfBounds); } unsafe { @@ -198,7 +203,12 @@ impl<'a> MemoryBuffer<'a> { .ok_or(MemoryAccessError::Overflow)?; if end > self.len.try_into().unwrap() { #[cfg(feature = "tracing")] - warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), end, self.len); + warn!( + "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", + buf.len(), + end, + self.len + ); return Err(MemoryAccessError::HeapOutOfBounds); } let buf_ptr = buf.as_mut_ptr() as *mut u8; @@ -215,7 +225,12 @@ impl<'a> MemoryBuffer<'a> { .ok_or(MemoryAccessError::Overflow)?; if end > self.len.try_into().unwrap() { #[cfg(feature = "tracing")] - warn!("attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", data.len(), end, self.len); + warn!( + "attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", + data.len(), + end, + self.len + ); return Err(MemoryAccessError::HeapOutOfBounds); } unsafe { diff --git a/lib/api/src/sys/externals/memory_view.rs b/lib/api/src/sys/externals/memory_view.rs index dcbfad6dcd5..1ccfc6cba1d 100644 --- a/lib/api/src/sys/externals/memory_view.rs +++ b/lib/api/src/sys/externals/memory_view.rs @@ -6,8 +6,8 @@ use std::mem::MaybeUninit; use std::slice; use wasmer_types::Pages; -use super::Memory; use super::memory::MemoryBuffer; +use super::Memory; /// A WebAssembly `memory` view. /// @@ -21,14 +21,13 @@ pub struct MemoryView<'a> { pub(crate) size: Pages, } -impl<'a> MemoryView<'a> -{ +impl<'a> MemoryView<'a> { pub(crate) fn new(memory: &Memory, store: &'a impl AsStoreRef) -> Self { let size = memory.handle.get(store.as_store_ref().objects()).size(); let definition = memory.handle.get(store.as_store_ref().objects()).vmmemory(); let def = unsafe { definition.as_ref() }; - + Self { buffer: MemoryBuffer { base: def.base, @@ -107,11 +106,7 @@ impl<'a> MemoryView<'a> /// /// This method is guaranteed to be safe (from the host side) in the face of /// concurrent writes. - pub fn read( - &self, - offset: u64, - buf: &mut [u8], - ) -> Result<(), MemoryAccessError> { + pub fn read(&self, offset: u64, buf: &mut [u8]) -> Result<(), MemoryAccessError> { self.buffer.read(offset, buf) } @@ -119,10 +114,7 @@ impl<'a> MemoryView<'a> /// /// This method is guaranteed to be safe (from the host side) in the face of /// concurrent writes. - pub fn read_u8( - &self, - offset: u64 - ) -> Result { + pub fn read_u8(&self, offset: u64) -> Result { let mut buf = [0u8; 1]; self.read(offset, &mut buf)?; Ok(buf[0]) @@ -153,11 +145,7 @@ impl<'a> MemoryView<'a> /// /// This method is guaranteed to be safe (from the host side) in the face of /// concurrent reads/writes. - pub fn write( - &self, - offset: u64, - data: &[u8], - ) -> Result<(), MemoryAccessError> { + pub fn write(&self, offset: u64, data: &[u8]) -> Result<(), MemoryAccessError> { self.buffer.write(offset, data) } @@ -165,12 +153,8 @@ impl<'a> MemoryView<'a> /// /// This method is guaranteed to be safe (from the host side) in the face of /// concurrent writes. - pub fn write_u8( - &self, - offset: u64, - val: u8 - ) -> Result<(), MemoryAccessError> { - let buf = [ val ]; + pub fn write_u8(&self, offset: u64, val: u8) -> Result<(), MemoryAccessError> { + let buf = [val]; self.write(offset, &buf)?; Ok(()) } diff --git a/lib/api/src/sys/mem_access.rs b/lib/api/src/sys/mem_access.rs index e6de517af55..623796f11b0 100644 --- a/lib/api/src/sys/mem_access.rs +++ b/lib/api/src/sys/mem_access.rs @@ -1,6 +1,6 @@ use crate::RuntimeError; #[allow(unused_imports)] -use crate::{Memory, MemoryView, Memory32, Memory64, MemorySize, WasmPtr}; +use crate::{Memory, Memory32, Memory64, MemorySize, MemoryView, WasmPtr}; use std::{ convert::TryInto, fmt, @@ -160,11 +160,7 @@ impl<'a, T: ValueType> WasmSlice<'a, T> { /// /// Returns a `MemoryAccessError` if the slice length overflows. #[inline] - pub fn new( - view: &'a MemoryView, - offset: u64, - len: u64, - ) -> Result { + pub fn new(view: &'a MemoryView, offset: u64, len: u64) -> Result { let total_len = len .checked_mul(mem::size_of::() as u64) .ok_or(MemoryAccessError::Overflow)?; diff --git a/lib/api/src/sys/mod.rs b/lib/api/src/sys/mod.rs index 1e18769b7be..1c272a2a60e 100644 --- a/lib/api/src/sys/mod.rs +++ b/lib/api/src/sys/mod.rs @@ -16,7 +16,8 @@ mod value; pub use crate::sys::exports::{ExportError, Exportable, Exports, ExportsIterator}; pub use crate::sys::extern_ref::ExternRef; pub use crate::sys::externals::{ - Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryView, Table, WasmTypeList, + Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryView, Table, + WasmTypeList, }; pub use crate::sys::function_env::{FunctionEnv, FunctionEnvMut}; pub use crate::sys::imports::Imports; diff --git a/lib/api/src/sys/ptr.rs b/lib/api/src/sys/ptr.rs index ad9f0568e62..f14383946d1 100644 --- a/lib/api/src/sys/ptr.rs +++ b/lib/api/src/sys/ptr.rs @@ -154,11 +154,7 @@ impl WasmPtr { /// Writes to the address pointed to by this `WasmPtr` in a memory. #[inline] - pub fn write( - self, - view: &MemoryView, - val: T, - ) -> Result<(), MemoryAccessError> { + pub fn write(self, view: &MemoryView, val: T) -> Result<(), MemoryAccessError> { self.deref(view).write(val) } @@ -219,10 +215,7 @@ impl WasmPtr { /// This method is safe to call even if the memory is being concurrently /// modified. #[inline] - pub fn read_utf8_string_with_nul( - self, - view: &MemoryView, - ) -> Result { + pub fn read_utf8_string_with_nul(self, view: &MemoryView) -> Result { let vec = self.read_until(view, |&byte| byte == 0)?; Ok(String::from_utf8(vec)?) } diff --git a/lib/compiler/src/traits.rs b/lib/compiler/src/traits.rs index 474ae61a03c..8b0cb748794 100644 --- a/lib/compiler/src/traits.rs +++ b/lib/compiler/src/traits.rs @@ -85,4 +85,4 @@ impl dyn ArtifactCreate + 'static { pub fn downcast_mut(&'_ mut self) -> Option<&'_ mut T> { self.upcast_any_mut().downcast_mut::() } -} \ No newline at end of file +} diff --git a/lib/emscripten/src/env/mod.rs b/lib/emscripten/src/env/mod.rs index 32dfb5247bc..e86e7e55ecd 100644 --- a/lib/emscripten/src/env/mod.rs +++ b/lib/emscripten/src/env/mod.rs @@ -75,7 +75,8 @@ pub fn ___build_environment(mut ctx: FunctionEnvMut, environ: c_int) { debug!("emscripten::___build_environment {}", environ); const MAX_ENV_VALUES: u32 = 64; const TOTAL_ENV_SIZE: u32 = 1024; - let environment = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), environ) as *mut c_int; + let environment = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), environ) as *mut c_int; let (mut pool_offset, env_ptr, mut pool_ptr) = unsafe { let (pool_offset, _pool_slice): (u32, &mut [u8]) = allocate_on_stack(&mut ctx, TOTAL_ENV_SIZE as u32); @@ -136,7 +137,8 @@ pub fn _pathconf(ctx: FunctionEnvMut, path_addr: c_int, name: c_int) -> c "emscripten::_pathconf {} {} - UNIMPLEMENTED", path_addr, name ); - let _path = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), path_addr) as *const c_char; + let _path = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), path_addr) as *const c_char; match name { 0 => 32000, 1 | 2 | 3 => 255, diff --git a/lib/emscripten/src/env/unix/mod.rs b/lib/emscripten/src/env/unix/mod.rs index 50cc32d85d5..8f9ca047b65 100644 --- a/lib/emscripten/src/env/unix/mod.rs +++ b/lib/emscripten/src/env/unix/mod.rs @@ -17,7 +17,8 @@ use wasmer::{FunctionEnvMut, WasmPtr}; pub fn _getenv(mut ctx: FunctionEnvMut, name: i32) -> u32 { debug!("emscripten::_getenv"); - let name_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char; + let name_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char; debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) }); @@ -33,8 +34,10 @@ pub fn _getenv(mut ctx: FunctionEnvMut, name: i32) -> u32 { pub fn _setenv(ctx: FunctionEnvMut, name: c_int, value: c_int, overwrite: c_int) -> c_int { debug!("emscripten::_setenv"); - let name_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char; - let value_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), value) as *const c_char; + let name_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char; + let value_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), value) as *const c_char; debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) }); debug!("=> value({:?})", unsafe { CStr::from_ptr(value_addr) }); @@ -46,7 +49,8 @@ pub fn _setenv(ctx: FunctionEnvMut, name: c_int, value: c_int, overwrite: pub fn _putenv(ctx: FunctionEnvMut, name: c_int) -> c_int { debug!("emscripten::_putenv"); - let name_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char; + let name_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char; debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) }); @@ -57,7 +61,8 @@ pub fn _putenv(ctx: FunctionEnvMut, name: c_int) -> c_int { pub fn _unsetenv(ctx: FunctionEnvMut, name: c_int) -> c_int { debug!("emscripten::_unsetenv"); - let name_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char; + let name_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char; debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) }); @@ -155,9 +160,7 @@ pub fn _gai_strerror(mut ctx: FunctionEnvMut, ecode: i32) -> i32 { let string_on_guest: WasmPtr = call_malloc_with_cast(&mut ctx, bytes.len() as _); let memory = ctx.data().memory_view(0, &ctx); - let writer = string_on_guest - .slice(&memory, bytes.len() as _) - .unwrap(); + let writer = string_on_guest.slice(&memory, bytes.len() as _).unwrap(); for (i, byte) in bytes.iter().enumerate() { writer.index(i as u64).write(*byte as _).unwrap(); } @@ -192,7 +195,10 @@ pub fn _getaddrinfo( let hints = if hints_ptr.is_null() { None } else { - let hints_guest = hints_ptr.deref(&ctx.data().memory_view(0, &ctx)).read().unwrap(); + let hints_guest = hints_ptr + .deref(&ctx.data().memory_view(0, &ctx)) + .read() + .unwrap(); Some(addrinfo { ai_flags: hints_guest.ai_flags, ai_family: hints_guest.ai_family, diff --git a/lib/emscripten/src/env/windows/mod.rs b/lib/emscripten/src/env/windows/mod.rs index 6476f877506..6b88651e6b7 100644 --- a/lib/emscripten/src/env/windows/mod.rs +++ b/lib/emscripten/src/env/windows/mod.rs @@ -73,7 +73,7 @@ pub fn _getpwnam(mut ctx: FunctionEnvMut, name_ptr: c_int) -> c_int { debug!("emscripten::_getpwnam {}", name_ptr); #[cfg(not(feature = "debug"))] let _ = name_ptr; - + #[repr(C)] struct GuestPasswd { pw_name: u32, @@ -108,7 +108,7 @@ pub fn _getgrnam(mut ctx: FunctionEnvMut, name_ptr: c_int) -> c_int { debug!("emscripten::_getgrnam {}", name_ptr); #[cfg(not(feature = "debug"))] let _ = name_ptr; - + #[repr(C)] struct GuestGroup { gr_name: u32, diff --git a/lib/emscripten/src/memory.rs b/lib/emscripten/src/memory.rs index 12b4eb67c62..fd278a55703 100644 --- a/lib/emscripten/src/memory.rs +++ b/lib/emscripten/src/memory.rs @@ -11,7 +11,8 @@ pub fn _emscripten_memcpy_big(ctx: FunctionEnvMut, dest: u32, src: u32, l "emscripten::_emscripten_memcpy_big {}, {}, {}", dest, src, len ); - let dest_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), dest) as *mut c_void; + let dest_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), dest) as *mut c_void; let src_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), src) as *mut c_void; unsafe { memcpy(dest_addr, src_addr, len as size_t); @@ -90,7 +91,7 @@ pub fn sbrk(mut ctx: FunctionEnvMut, increment: i32) -> i32 { .unwrap() .globals .dynamictop_ptr; - + let dynamictop_ptr = WasmPtr::::new(top_ptr); let old_dynamic_top = { let memory = ctx.data().memory_view(0, &ctx); @@ -118,7 +119,7 @@ pub fn sbrk(mut ctx: FunctionEnvMut, increment: i32) -> i32 { } // re-borrow the top ptr let memory = ctx.data().memory_view(0, &ctx); - let dynamictop_ptr = WasmPtr::::new(top_ptr).deref( &memory); + let dynamictop_ptr = WasmPtr::::new(top_ptr).deref(&memory); dynamictop_ptr.write(new_dynamic_top).unwrap(); old_dynamic_top as _ } diff --git a/lib/emscripten/src/syscalls/mod.rs b/lib/emscripten/src/syscalls/mod.rs index f8b526e11a9..fb0d863e5a8 100644 --- a/lib/emscripten/src/syscalls/mod.rs +++ b/lib/emscripten/src/syscalls/mod.rs @@ -80,7 +80,8 @@ pub fn ___syscall4(ctx: FunctionEnvMut, _which: c_int, mut varargs: VarAr let buf: i32 = varargs.get(&ctx); let count: i32 = varargs.get(&ctx); debug!("=> fd: {}, buf: {}, count: {}", fd, buf, count); - let buf_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), buf) as *const c_void; + let buf_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), buf) as *const c_void; unsafe { write(fd, buf_addr, count as _) as i32 } } @@ -384,7 +385,8 @@ pub fn ___syscall192(mut ctx: FunctionEnvMut, _which: c_int, mut varargs: // ENOMEM return -12; } - let real_ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), ptr) as *const u8; + let real_ptr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), ptr) as *const u8; env::call_memset(&mut ctx, ptr, 0, len); for i in 0..(len as usize) { unsafe { @@ -450,9 +452,10 @@ pub fn ___syscall145(ctx: FunctionEnvMut, _which: c_int, mut varargs: Var let guest_iov_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), (iov + i * 8)) as *mut GuestIovec; - let iov_base = - emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), (*guest_iov_addr).iov_base) - as *mut c_void; + let iov_base = emscripten_memory_pointer!( + ctx.data().memory_view(0, &ctx), + (*guest_iov_addr).iov_base + ) as *mut c_void; let iov_len = (*guest_iov_addr).iov_len as _; // debug!("=> iov_addr: {:?}, {:?}", iov_base, iov_len); let curr = read(fd, iov_base, iov_len); @@ -488,9 +491,10 @@ pub fn ___syscall146(ctx: FunctionEnvMut, _which: i32, mut varargs: VarAr let guest_iov_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), (iov + i * 8)) as *mut GuestIovec; - let iov_base = - emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), (*guest_iov_addr).iov_base) - as *const c_void; + let iov_base = emscripten_memory_pointer!( + ctx.data().memory_view(0, &ctx), + (*guest_iov_addr).iov_base + ) as *const c_void; let iov_len = (*guest_iov_addr).iov_len as _; // debug!("=> iov_addr: {:?}, {:?}", iov_base, iov_len); let curr = write(fd, iov_base, iov_len); @@ -518,7 +522,8 @@ pub fn ___syscall191(ctx: FunctionEnvMut, _which: i32, mut varargs: VarAr _resource ); let rlim_emptr: i32 = varargs.get(&ctx); - let rlim_ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rlim_emptr) as *mut u8; + let rlim_ptr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rlim_emptr) as *mut u8; let rlim = unsafe { slice::from_raw_parts_mut(rlim_ptr, 16) }; // set all to RLIM_INIFINTY @@ -722,7 +727,8 @@ pub fn ___syscall340(ctx: FunctionEnvMut, _which: c_int, mut varargs: Var if old_limit != 0 { // just report no limits - let buf_ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), old_limit) as *mut u8; + let buf_ptr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), old_limit) as *mut u8; let buf = unsafe { slice::from_raw_parts_mut(buf_ptr, 16) }; LittleEndian::write_i64(&mut *buf, val); diff --git a/lib/emscripten/src/syscalls/unix.rs b/lib/emscripten/src/syscalls/unix.rs index b709ea18994..bd4a0a81408 100644 --- a/lib/emscripten/src/syscalls/unix.rs +++ b/lib/emscripten/src/syscalls/unix.rs @@ -203,7 +203,8 @@ pub fn ___syscall77(ctx: FunctionEnvMut, _which: c_int, mut varargs: VarA let resource: c_int = varargs.get(&ctx); let rusage_ptr: c_int = varargs.get(&ctx); #[allow(clippy::cast_ptr_alignment)] - let rusage = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rusage_ptr) as *mut rusage; + let rusage = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rusage_ptr) as *mut rusage; assert_eq!(8, mem::align_of_val(&rusage)); unsafe { getrusage(resource, rusage) } } @@ -779,8 +780,7 @@ pub fn ___syscall102(ctx: FunctionEnvMut, _which: c_int, mut varargs: Var let value: u32 = socket_varargs.get(&ctx); let option_len: u32 = socket_varargs.get(&ctx); let value_addr = emscripten_memory_pointer!(memory, value) as _; - let option_len_addr = - emscripten_memory_pointer!(memory, option_len) as *mut socklen_t; + let option_len_addr = emscripten_memory_pointer!(memory, option_len) as *mut socklen_t; unsafe { getsockopt(socket, level, name, value_addr, option_len_addr) } } 16 => { @@ -927,9 +927,11 @@ pub fn ___syscall114(ctx: FunctionEnvMut, _which: c_int, mut varargs: Var let status: u32 = varargs.get(&ctx); let options: c_int = varargs.get(&ctx); let rusage: u32 = varargs.get(&ctx); - let status_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), status) as *mut c_int; + let status_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), status) as *mut c_int; - let rusage_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rusage) as *mut rusage; + let rusage_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rusage) as *mut rusage; let res = unsafe { wait4(pid, status_addr, options, rusage_addr) }; debug!( "=> pid: {}, status: {:?}, options: {}, rusage: {:?} = pid: {}", diff --git a/lib/emscripten/src/syscalls/windows.rs b/lib/emscripten/src/syscalls/windows.rs index f6601d4286b..ab41fe8cf6f 100644 --- a/lib/emscripten/src/syscalls/windows.rs +++ b/lib/emscripten/src/syscalls/windows.rs @@ -28,7 +28,7 @@ pub fn ___syscall5(mut ctx: FunctionEnvMut, which: c_int, mut varargs: Va let flags: i32 = varargs.get(&ctx); let mode: u32 = varargs.get(&ctx); let path_str = unsafe { std::ffi::CStr::from_ptr(real_path).to_str().unwrap() }; - + match path_str { "/dev/urandom" => { // create a fake urandom file for windows, super hacky diff --git a/lib/emscripten/src/time.rs b/lib/emscripten/src/time.rs index 0b77ceec686..8fc45ef004c 100644 --- a/lib/emscripten/src/time.rs +++ b/lib/emscripten/src/time.rs @@ -178,7 +178,8 @@ pub fn _tvset(mut _ctx: FunctionEnvMut) { /// formats time as a C string #[allow(clippy::cast_ptr_alignment)] unsafe fn fmt_time(ctx: FunctionEnvMut, time: u32) -> *const c_char { - let date = &*(emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time) as *mut guest_tm); + let date = + &*(emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time) as *mut guest_tm); let days = vec!["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; let months = vec![ @@ -241,15 +242,17 @@ pub fn _localtime(mut ctx: FunctionEnvMut, time_p: u32) -> c_int { // https://stackoverflow.com/questions/19170721/real-time-awareness-of-timezone-change-in-localtime-vs-localtime-r let timespec = unsafe { - let time_p_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *mut i64; + let time_p_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *mut i64; let seconds = *time_p_addr; time::OffsetDateTime::from_unix_timestamp(seconds) }; unsafe { let tm_struct_offset = env::call_malloc(&mut ctx, mem::size_of::() as _); - let tm_struct_ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), tm_struct_offset) - as *mut guest_tm; + let tm_struct_ptr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), tm_struct_offset) + as *mut guest_tm; // debug!( // ">>>>>>> time = {}, {}, {}, {}, {}, {}, {}, {}", // result_tm.tm_sec, result_tm.tm_min, result_tm.tm_hour, result_tm.tm_mday, @@ -279,7 +282,8 @@ pub fn _localtime_r(ctx: FunctionEnvMut, time_p: u32, result: u32) -> c_i // https://stackoverflow.com/questions/19170721/real-time-awareness-of-timezone-change-in-localtime-vs-localtime-r unsafe { - let seconds = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *const i32; + let seconds = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *const i32; let timespec = time::OffsetDateTime::from_unix_timestamp_nanos(*seconds as _); // debug!( @@ -313,7 +317,8 @@ pub fn _time(ctx: FunctionEnvMut, time_p: u32) -> i32 { debug!("emscripten::_time {}", time_p); unsafe { - let time_p_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *mut i64; + let time_p_addr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *mut i64; libc_time(time_p_addr) as i32 // TODO review i64 } } @@ -403,7 +408,8 @@ pub fn _strftime( #[allow(clippy::cast_ptr_alignment)] let s = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), s_ptr) as *mut c_char; #[allow(clippy::cast_ptr_alignment)] - let format = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), format_ptr) as *const c_char; + let format = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), format_ptr) as *const c_char; #[allow(clippy::cast_ptr_alignment)] let tm = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), tm_ptr) as *const guest_tm; diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index 376653a6b8a..af55a1b046d 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -8,7 +8,7 @@ use std::mem::size_of; use std::os::raw::c_char; use std::path::PathBuf; use std::slice; -use wasmer::{FunctionEnvMut, GlobalInit, Module, Pages, WasmPtr, MemoryView}; +use wasmer::{FunctionEnvMut, GlobalInit, MemoryView, Module, Pages, WasmPtr}; /// We check if a provided module is an Emscripten generated one pub fn is_emscripten_module(module: &Module) -> bool { @@ -204,7 +204,8 @@ pub struct GuestStat { #[allow(clippy::cast_ptr_alignment)] pub unsafe fn copy_stat_into_wasm(ctx: FunctionEnvMut, buf: u32, stat: &stat) { - let stat_ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), buf) as *mut GuestStat; + let stat_ptr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), buf) as *mut GuestStat; (*stat_ptr).st_dev = stat.st_dev as _; (*stat_ptr).__st_dev_padding = 0; (*stat_ptr).__st_ino_truncated = stat.st_ino as _; diff --git a/lib/emscripten/src/varargs.rs b/lib/emscripten/src/varargs.rs index b9ce34c6a57..4d8b96485b0 100644 --- a/lib/emscripten/src/varargs.rs +++ b/lib/emscripten/src/varargs.rs @@ -21,7 +21,8 @@ impl VarArgs { // pub fn getStr<'a>(&mut self, ctx: &mut Ctx) -> &'a CStr { pub fn get_str(&mut self, ctx: &FunctionEnvMut) -> *const c_char { let ptr_addr: u32 = self.get(ctx); - let ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), ptr_addr) as *const c_char; + let ptr = + emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), ptr_addr) as *const c_char; ptr // unsafe { CStr::from_ptr(ptr) } } diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index ea291537888..7fcf37dff39 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -63,8 +63,8 @@ use derivative::*; use std::ops::Deref; use thiserror::Error; use wasmer::{ - imports, namespace, AsStoreMut, Exports, Function, FunctionEnv, Imports, Memory, Memory32, - MemoryAccessError, MemorySize, Module, TypedFunction, MemoryView, AsStoreRef + imports, namespace, AsStoreMut, AsStoreRef, Exports, Function, FunctionEnv, Imports, Memory, + Memory32, MemoryAccessError, MemorySize, MemoryView, Module, TypedFunction, }; pub use runtime::{ @@ -332,7 +332,7 @@ impl WasiEnv { } self.memory = Some(memory); } - + /// Providers safe access to the memory /// (it must be initialized before it can be used) pub fn memory_view<'a>(&'a self, store: &'a impl AsStoreRef) -> MemoryView<'a> { @@ -348,8 +348,12 @@ impl WasiEnv { pub fn state(&self) -> &WasiState { &self.state } - - pub(crate) fn get_memory_and_wasi_state<'a>(&'a self, store: &'a impl AsStoreRef, _mem_index: u32) -> (MemoryView<'a>, &WasiState) { + + pub(crate) fn get_memory_and_wasi_state<'a>( + &'a self, + store: &'a impl AsStoreRef, + _mem_index: u32, + ) -> (MemoryView<'a>, &WasiState) { let memory = self.memory_view(store); let state = self.state.deref(); (memory, state) diff --git a/lib/wasi/src/state/pipe.rs b/lib/wasi/src/state/pipe.rs index b2bab5ff4dc..12c0084d43d 100644 --- a/lib/wasi/src/state/pipe.rs +++ b/lib/wasi/src/state/pipe.rs @@ -6,8 +6,8 @@ use std::io::{self, Read}; use std::ops::DerefMut; use std::sync::mpsc; use std::sync::Mutex; -use wasmer::{MemorySize, MemoryView}; use wasmer::WasmSlice; +use wasmer::{MemorySize, MemoryView}; #[derive(Debug)] pub struct WasiPipe { diff --git a/lib/wasi/src/state/socket.rs b/lib/wasi/src/state/socket.rs index d49739555eb..9f1b9765478 100644 --- a/lib/wasi/src/state/socket.rs +++ b/lib/wasi/src/state/socket.rs @@ -10,7 +10,7 @@ use std::sync::Mutex; use std::time::Duration; #[allow(unused_imports)] use tracing::{debug, error, info, warn}; -use wasmer::{MemorySize, WasmPtr, WasmSlice, MemoryView}; +use wasmer::{MemorySize, MemoryView, WasmPtr, WasmSlice}; use wasmer_vnet::{net_error_into_io_err, TimeType}; use wasmer_vnet::{ IpCidr, IpRoute, SocketHttpRequest, VirtualIcmpSocket, VirtualNetworking, VirtualRawSocket, diff --git a/lib/wasi/src/syscalls/mod.rs b/lib/wasi/src/syscalls/mod.rs index 142389c3aab..4925fb72eb4 100644 --- a/lib/wasi/src/syscalls/mod.rs +++ b/lib/wasi/src/syscalls/mod.rs @@ -47,8 +47,8 @@ use std::sync::{mpsc, Arc}; use std::time::Duration; use tracing::{debug, error, trace, warn}; use wasmer::{ - AsStoreMut, FunctionEnvMut, Memory, Memory32, Memory64, MemorySize, RuntimeError, Value, - WasmPtr, WasmSlice, FunctionEnv, Instance, Module, Extern, MemoryView, + AsStoreMut, Extern, FunctionEnv, FunctionEnvMut, Instance, Memory, Memory32, Memory64, + MemorySize, MemoryView, Module, RuntimeError, Value, WasmPtr, WasmSlice, }; use wasmer_vbus::{FileDescriptor, StdioMode}; use wasmer_vfs::{FsError, VirtualFile}; @@ -891,10 +891,7 @@ pub fn fd_pread( Kind::Dir { .. } | Kind::Root { .. } => return Ok(__WASI_EISDIR), Kind::Symlink { .. } => unimplemented!("Symlinks in wasi::fd_pread"), Kind::Buffer { buffer } => { - wasi_try_ok!( - read_bytes(&buffer[(offset as usize)..], &memory, iovs), - env - ) + wasi_try_ok!(read_bytes(&buffer[(offset as usize)..], &memory, iovs), env) } } } @@ -924,13 +921,14 @@ pub fn fd_prestat_get( let (memory, mut state, inodes) = env.get_memory_and_wasi_state_and_inodes(&ctx, 0); let prestat_ptr = buf.deref(&memory); - wasi_try_mem!(prestat_ptr.write(wasi_try!( - state.fs.prestat_fd(inodes.deref(), fd) - .map_err(|code| { + wasi_try_mem!( + prestat_ptr.write(wasi_try!(state.fs.prestat_fd(inodes.deref(), fd).map_err( + |code| { debug!("fd_prestat_get failed (fd={}) - errno={}", fd, code); code - }) - ))); + } + ))) + ); __WASI_ESUCCESS } @@ -1733,8 +1731,7 @@ pub fn fd_write( counter, wakers, .. } => { let mut val = 0u64.to_ne_bytes(); - let written = - wasi_try_ok!(write_bytes(&mut val[..], &memory, iovs_arr)); + let written = wasi_try_ok!(write_bytes(&mut val[..], &memory, iovs_arr)); if written != val.len() { return Ok(__WASI_EINVAL); } @@ -1754,10 +1751,7 @@ pub fn fd_write( } Kind::Symlink { .. } => unimplemented!("Symlinks in wasi::fd_write"), Kind::Buffer { buffer } => { - wasi_try_ok!( - write_bytes(&mut buffer[offset..], &memory, iovs_arr), - env - ) + wasi_try_ok!(write_bytes(&mut buffer[offset..], &memory, iovs_arr), env) } } }; @@ -2503,8 +2497,7 @@ pub fn path_readlink( } let bytes: Vec<_> = bytes.collect(); - let out = - wasi_try_mem!(buf.slice(&memory, wasi_try!(to_offset::(bytes.len())))); + let out = wasi_try_mem!(buf.slice(&memory, wasi_try!(to_offset::(bytes.len())))); wasi_try_mem!(out.write_slice(&bytes)); // should we null terminate this? @@ -4533,8 +4526,7 @@ pub fn port_route_list( let max_routes: usize = wasi_try!(wasi_try_mem!(nroutes.read()) .try_into() .map_err(|_| __WASI_EINVAL)); - let ref_routes = - wasi_try_mem!(routes.slice(&memory, wasi_try!(to_offset::(max_routes)))); + let ref_routes = wasi_try_mem!(routes.slice(&memory, wasi_try!(to_offset::(max_routes)))); let routes = wasi_try!(env.net().route_list().map_err(net_error_into_wasi_err)); @@ -5265,7 +5257,7 @@ pub fn sock_recv( &ctx, sock, __WASI_RIGHT_SOCK_RECV, - |socket| { socket.recv(& memory, iovs_arr) } + |socket| { socket.recv(&memory, iovs_arr) } )); let bytes_read: M::Offset = wasi_try_ok!(bytes_read.try_into().map_err(|_| __WASI_EOVERFLOW)); From a6022af980af6035e929e5e53523a5f7bb2884e0 Mon Sep 17 00:00:00 2001 From: Wolfgang Silbermayr Date: Thu, 4 Aug 2022 20:54:29 +0200 Subject: [PATCH 6/9] Fix clippy lints --- lib/api/src/sys/externals/memory_view.rs | 2 +- lib/api/src/sys/mem_access.rs | 2 +- lib/emscripten/src/utils.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/api/src/sys/externals/memory_view.rs b/lib/api/src/sys/externals/memory_view.rs index 1ccfc6cba1d..3250f4d6c05 100644 --- a/lib/api/src/sys/externals/memory_view.rs +++ b/lib/api/src/sys/externals/memory_view.rs @@ -96,7 +96,7 @@ impl<'a> MemoryView<'a> { } pub(crate) fn buffer(&'a self) -> MemoryBuffer<'a> { - self.buffer.clone() + self.buffer } /// Safely reads bytes from the memory at the given offset. diff --git a/lib/api/src/sys/mem_access.rs b/lib/api/src/sys/mem_access.rs index 623796f11b0..28204577a22 100644 --- a/lib/api/src/sys/mem_access.rs +++ b/lib/api/src/sys/mem_access.rs @@ -64,7 +64,7 @@ impl<'a, T: ValueType> WasmRef<'a, T> { #[inline] pub fn new(view: &'a MemoryView, offset: u64) -> Self { Self { - buffer: view.buffer.clone(), + buffer: view.buffer, offset, marker: PhantomData, } diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index af55a1b046d..903e1e9a262 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -234,7 +234,7 @@ pub unsafe fn copy_stat_into_wasm(ctx: FunctionEnvMut, buf: u32, stat: &s #[allow(dead_code)] // it's used in `env/windows/mod.rs`. pub fn read_string_from_wasm(memory: &MemoryView, offset: u32) -> String { WasmPtr::::new(offset) - .read_utf8_string_with_nul(&memory) + .read_utf8_string_with_nul(memory) .unwrap() } From 6b39488b694b9e86b4a925b9d4d65e2ec8955364 Mon Sep 17 00:00:00 2001 From: Wolfgang Silbermayr Date: Fri, 5 Aug 2022 09:32:04 +0200 Subject: [PATCH 7/9] =?UTF-8?q?Update=20migration=20documentation=20for=20?= =?UTF-8?q?`*=5Fwith=5Fenv(=E2=80=A6)`=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/migration_to_3.0.0.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/migration_to_3.0.0.md b/docs/migration_to_3.0.0.md index ce939e93334..6530255ae5e 100644 --- a/docs/migration_to_3.0.0.md +++ b/docs/migration_to_3.0.0.md @@ -20,8 +20,13 @@ This version introduces the following changes to make the Wasmer API more ergono 1. `ImportsObject` and the traits `Resolver`, `NamedResolver`, etc have been removed and replaced with a single simple type `Imports`. This reduces the complexity of setting up an `Instance`. The helper macro `imports!` can still be used. 2. The `Store` will keep track of all memory and functions used, removing old tracking and Weak/Strong pointer usage. Every function and memory that can be defined is associated to a specific `Store`, and cannot be mixed with another `Store` -3. `WasmerEnv` and associated traits and macro have been removed. To use a function environment, you will need to create a `FunctionEnv` object and pass it along when you construct the function. The environment can be retrieved from the first argument of the function. All functions now takes a mandatory first argument that is of type `FunctionEnvMut<'_, _>`, with `_` being either nothing `()` or the defined environment type. The function creation `XXX_with_env(...)` don't exist anymore, simply use `Function::new(...)` or `Function::native_new(...)` with the correct `FunctionEnv<_>` type. Because the `WasmerEnv` and all helpers don't exists anymore, you have to import memory yourself, there isn't any per instance initialisation automatically done anymore. It's especially important in wasi use with `WasiEnv`. `Env` can be accessed from a `FunctionEnvMut<'_, WasiEnv>` using `FunctionEnvMut::data()` or `FunctionEnvMut::data_mut()`. -4. The `Engine`s API has been simplified, Instead of the user choosing and setting up an engine explicitly, everything now uses a single engine. All functionalities of the `universal`, `staticlib` and `dylib` engines should be available in this new engine unless explicitly stated as unsupported. +3. `NativeFunc` has been renamed to `TypedFunction`, accordingly the following functions have been renamed: + * `Function::native(…)` → `Function::typed(…)` + * `Function::new_native(…)` → `Function::new_typed(…)` + * `Function::new_native_with_env(…)` → `Function::new_typed_with_env(…)` + The previous variants still exist in order to support the migration, but they have been deprecated. +4. `WasmerEnv` and associated traits and macro have been removed. To use a function environment, you will need to create a `FunctionEnv` object and pass it along when you construct the function. For convenience, these functions also exist in a variant without the environment for simpler use cases that don't need it. For the variants with the environment, it can be retrieved from the first argument of the function. Because the `WasmerEnv` and all helpers don't exists anymore, you have to import memory yourself, there isn't any per instance initialisation automatically done anymore. It's especially important in wasi use with `WasiEnv`. `Env` can be accessed from a `FunctionEnvMut<'_, WasiEnv>` using `FunctionEnvMut::data()` or `FunctionEnvMut::data_mut()`. +5. The `Engine`s API has been simplified, Instead of the user choosing and setting up an engine explicitly, everything now uses a single engine. All functionalities of the `universal`, `staticlib` and `dylib` engines should be available in this new engine unless explicitly stated as unsupported. ## How to use Wasmer 3.0.0 From f8f12654d4b9fa028b492b0c80682457d8b20b19 Mon Sep 17 00:00:00 2001 From: Wolfgang Silbermayr Date: Fri, 5 Aug 2022 09:34:50 +0200 Subject: [PATCH 8/9] Add CHANGELOG entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4437731723..dd0036bb39c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C - [#3048](https://github.com/wasmerio/wasmer/pull/3048) Automatically publish wasmer as "cloudcompiler" package to wapm.dev on every release - [#3075](https://github.com/wasmerio/wasmer/pull/3075) Remove __wbindgen_thread_id +- [#3072](https://github.com/wasmerio/wasmer/pull/3072) Add back `Function::*_with_env(…)` functions + ### Fixed ## 3.0.0-alpha.4 - 2022/07/28 From dcb34971addac8d6e0acc10de189cb0156c08865 Mon Sep 17 00:00:00 2001 From: Wolfgang Silbermayr Date: Fri, 5 Aug 2022 12:52:26 +0200 Subject: [PATCH 9/9] Seal HostFunctionKind trait --- lib/api/src/js/externals/function.rs | 12 +++++++++++- lib/api/src/sys/externals/function.rs | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/api/src/js/externals/function.rs b/lib/api/src/js/externals/function.rs index 3c7bd3017c9..a74a3fa22e0 100644 --- a/lib/api/src/js/externals/function.rs +++ b/lib/api/src/js/externals/function.rs @@ -901,7 +901,7 @@ mod inner { /// the trait system to automatically generate the appropriate /// host functions. #[doc(hidden)] - pub trait HostFunctionKind {} + pub trait HostFunctionKind: private::HostFunctionKindSealed {} /// An empty struct to help Rust typing to determine /// when a `HostFunction` does have an environment. @@ -915,6 +915,16 @@ mod inner { impl HostFunctionKind for WithoutEnv {} + mod private { + //! Sealing the HostFunctionKind because it shouldn't be implemented + //! by any type outside. + //! See: + //! https://rust-lang.github.io/api-guidelines/future-proofing.html#c-sealed + pub trait HostFunctionKindSealed {} + impl HostFunctionKindSealed for super::WithEnv {} + impl HostFunctionKindSealed for super::WithoutEnv {} + } + /// Represents a low-level Wasm static host function. See /// `super::Function::new` and `super::Function::new_env` to learn /// more. diff --git a/lib/api/src/sys/externals/function.rs b/lib/api/src/sys/externals/function.rs index 1aa1c5f03c9..87bfa8237eb 100644 --- a/lib/api/src/sys/externals/function.rs +++ b/lib/api/src/sys/externals/function.rs @@ -1119,6 +1119,16 @@ mod inner { impl HostFunctionKind for WithoutEnv {} + mod private { + //! Sealing the HostFunctionKind because it shouldn't be implemented + //! by any type outside. + //! See: + //! https://rust-lang.github.io/api-guidelines/future-proofing.html#c-sealed + pub trait HostFunctionKindSealed {} + impl HostFunctionKindSealed for super::WithEnv {} + impl HostFunctionKindSealed for super::WithoutEnv {} + } + /// Represents a low-level Wasm static host function. See /// [`super::Function::new_typed`] and /// [`super::Function::new_typed_with_env`] to learn more.