From a1bcd19f0d96effacfafba2335499f9f5ed6a603 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 12 Feb 2021 18:48:27 +0000 Subject: [PATCH 01/11] rust: add support for imported functions --- bindings/rust/src/lib.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 66465ae5c..6461fe4e9 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -191,16 +191,45 @@ impl Drop for Instance { } } +unsafe extern "C" fn host_callback( + context: *mut std::os::raw::c_void, + instance: *mut sys::FizzyInstance, + args: *const sys::FizzyValue, + depth: i32, +) -> sys::FizzyExecutionResult { + unimplemented!() +} + +fn create_function_import_list() -> Vec { + let fn_type = sys::FizzyFunctionType { + output: sys::FizzyValueTypeVoid, + inputs: std::ptr::null(), + inputs_size: 0, + }; + let fn_ptr: sys::FizzyExternalFn = None; + let ext_fn = sys::FizzyExternalFunction { + type_: fn_type, + function: Some(host_callback), + context: std::ptr::null_mut(), + }; + vec![sys::FizzyImportedFunction { + module: unsafe { CString::new("env").unwrap().as_ptr() }, + name: unsafe { CString::new("print").unwrap().as_ptr() }, + external_function: ext_fn, + }] +} + impl Module { /// Create an instance of a module. // TODO: support imported functions pub fn instantiate(self) -> Result { let mut err = FizzyErrorBox::new(); + let import_list = create_function_import_list(); let ptr = unsafe { - sys::fizzy_instantiate( + sys::fizzy_resolve_instantiate( self.0.as_ptr(), - std::ptr::null(), - 0, + import_list.as_ptr(), + import_list.len(), std::ptr::null(), std::ptr::null(), std::ptr::null(), From b3a483b91ededde1f3fcbe83b73a4cd080929487 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 11 Mar 2021 13:33:52 +0000 Subject: [PATCH 02/11] f --- bindings/rust/src/lib.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 6461fe4e9..245a38f03 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -197,6 +197,7 @@ unsafe extern "C" fn host_callback( args: *const sys::FizzyValue, depth: i32, ) -> sys::FizzyExecutionResult { + println!("host fuction called!"); unimplemented!() } @@ -1327,4 +1328,31 @@ mod tests { assert!(result.is_err()); assert_eq!(result.err().unwrap(), Error::Trapped); } + + #[test] + fn execute_host_function2() { + /* wat2wasm + (module + (func $print (import "env" "print")) + (func (export "foo") + call $print + ) + ) + */ + let input = hex::decode( + "0061736d01000000010401600000020d0103656e76057072696e7400000302010007070103666f6f00010a0601040010000b").unwrap(); + + let module = parse(&input); + assert!(module.is_ok()); + let instance = module.unwrap().instantiate(); + assert!(instance.is_ok()); + let mut instance = instance.unwrap(); + + // Successful execution. + let result = instance.execute("foo", &[], 0); + assert!(result.is_ok()); + let result = result.unwrap(); + assert!(!result.trapped()); + assert!(result.value().is_none()); + } } From dd639b19044358bb2cc92b4fe1f457fe386aa1b1 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 11 Mar 2021 14:03:54 +0000 Subject: [PATCH 03/11] f --- bindings/rust/src/lib.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 245a38f03..03a3f75a7 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -191,6 +191,12 @@ impl Drop for Instance { } } +struct HostFunction { + module: CString, + name: CString, + index: usize, +} + unsafe extern "C" fn host_callback( context: *mut std::os::raw::c_void, instance: *mut sys::FizzyInstance, @@ -201,7 +207,8 @@ unsafe extern "C" fn host_callback( unimplemented!() } -fn create_function_import_list() -> Vec { +fn create_function_import_list(host_functions: &[HostFunction]) -> Vec { + assert!(host_functions.len() == 1); let fn_type = sys::FizzyFunctionType { output: sys::FizzyValueTypeVoid, inputs: std::ptr::null(), @@ -214,8 +221,8 @@ fn create_function_import_list() -> Vec { context: std::ptr::null_mut(), }; vec![sys::FizzyImportedFunction { - module: unsafe { CString::new("env").unwrap().as_ptr() }, - name: unsafe { CString::new("print").unwrap().as_ptr() }, + module: host_functions[0].module.as_ptr(), + name: host_functions[0].name.as_ptr(), external_function: ext_fn, }] } @@ -225,7 +232,11 @@ impl Module { // TODO: support imported functions pub fn instantiate(self) -> Result { let mut err = FizzyErrorBox::new(); - let import_list = create_function_import_list(); + let import_list = create_function_import_list(&[HostFunction { + module: CString::new("env").unwrap(), + name: CString::new("print").unwrap(), + index: 0, + }]); let ptr = unsafe { sys::fizzy_resolve_instantiate( self.0.as_ptr(), From 17821d8a9c3752468ead3c569e61c232fdda3873 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 11 Mar 2021 14:28:37 +0000 Subject: [PATCH 04/11] f --- bindings/rust/src/lib.rs | 1 - lib/fizzy/capi.cpp | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 03a3f75a7..dcbefd6f2 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -214,7 +214,6 @@ fn create_function_import_list(host_functions: &[HostFunction]) -> Vec #include #include +#include namespace { @@ -641,6 +642,11 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module, set_success(error); return wrap(instance.release()); } + catch (fizzy::instantiate_error const& e) + { + std::cout << e.what() << std::endl; + return nullptr; + } catch (...) { set_error_from_current_exception(error); From aaaf7f1c1e5615085b83300f5646ccb9e48f89ea Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 11 Mar 2021 14:49:55 +0000 Subject: [PATCH 05/11] f --- bindings/rust/src/lib.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index dcbefd6f2..405b10a72 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -194,6 +194,8 @@ impl Drop for Instance { struct HostFunction { module: CString, name: CString, + inputs: Vec, + output: sys::FizzyValueType, index: usize, } @@ -209,10 +211,11 @@ unsafe extern "C" fn host_callback( fn create_function_import_list(host_functions: &[HostFunction]) -> Vec { assert!(host_functions.len() == 1); + let host_function = &host_functions[0]; let fn_type = sys::FizzyFunctionType { - output: sys::FizzyValueTypeVoid, - inputs: std::ptr::null(), - inputs_size: 0, + output: host_function.output, + inputs: host_function.inputs.as_ptr(), + inputs_size: host_function.inputs.len(), }; let ext_fn = sys::FizzyExternalFunction { type_: fn_type, @@ -220,8 +223,8 @@ fn create_function_import_list(host_functions: &[HostFunction]) -> Vec Result { let mut err = FizzyErrorBox::new(); - let import_list = create_function_import_list(&[HostFunction { + let host_fn1 = HostFunction { module: CString::new("env").unwrap(), name: CString::new("print").unwrap(), + inputs: vec![], + output: sys::FizzyValueTypeVoid, index: 0, - }]); + }; + let import_list = create_function_import_list(&[host_fn1]); + println!("{}", import_list.len()); let ptr = unsafe { sys::fizzy_resolve_instantiate( self.0.as_ptr(), From 818beccba6ad533a0018178afd8c2b4455927ce7 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 11 Mar 2021 15:18:37 +0000 Subject: [PATCH 06/11] f --- bindings/rust/src/lib.rs | 11 ++++++++--- lib/fizzy/capi.cpp | 1 + lib/fizzy/instantiate.cpp | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 405b10a72..188b1d954 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -222,6 +222,11 @@ fn create_function_import_list(host_functions: &[HostFunction]) -> Vec Result { let mut err = FizzyErrorBox::new(); let host_fn1 = HostFunction { - module: CString::new("env").unwrap(), - name: CString::new("print").unwrap(), + module: CString::new("env").expect("cstring to work"), + name: CString::new("print").expect("cstring to work"), inputs: vec![], output: sys::FizzyValueTypeVoid, index: 0, }; let import_list = create_function_import_list(&[host_fn1]); - println!("{}", import_list.len()); + println!("{:?} {}", import_list.as_ptr(), import_list.len()); let ptr = unsafe { sys::fizzy_resolve_instantiate( self.0.as_ptr(), diff --git a/lib/fizzy/capi.cpp b/lib/fizzy/capi.cpp index 79cbd630b..30ed1467f 100644 --- a/lib/fizzy/capi.cpp +++ b/lib/fizzy/capi.cpp @@ -276,6 +276,7 @@ inline fizzy::ImportedFunction unwrap(const FizzyImportedFunction& c_imported_fu auto function = unwrap( c_imported_func.external_function.function, c_imported_func.external_function.context); + std::cout << "c_imported_func:" << c_imported_func.module << ":" << c_imported_func.name << std::endl; return {c_imported_func.module, c_imported_func.name, std::move(inputs), output, std::move(function)}; diff --git a/lib/fizzy/instantiate.cpp b/lib/fizzy/instantiate.cpp index c919a1a1c..8b889a087 100644 --- a/lib/fizzy/instantiate.cpp +++ b/lib/fizzy/instantiate.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace fizzy { @@ -261,7 +262,7 @@ ExternalFunction find_imported_function(const std::string& module, const std::st const FuncType& module_func_type, const std::vector& imported_functions) { const auto it = std::find_if(imported_functions.begin(), imported_functions.end(), - [module, name](const auto& func) { return module == func.module && name == func.name; }); + [module, name](const auto& func) { std::cout << func.module << ":" << func.name << std::endl; return module == func.module && name == func.name; }); if (it == imported_functions.end()) { From a28562012178c5153e2644d40c89d43b0ae57350 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 29 Mar 2021 23:57:05 +0100 Subject: [PATCH 07/11] f --- bindings/rust/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 188b1d954..cfce16e38 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -200,10 +200,10 @@ struct HostFunction { } unsafe extern "C" fn host_callback( - context: *mut std::os::raw::c_void, + host_context: *mut std::os::raw::c_void, instance: *mut sys::FizzyInstance, args: *const sys::FizzyValue, - depth: i32, + context: *mut sys::FizzyExecutionContext, ) -> sys::FizzyExecutionResult { println!("host fuction called!"); unimplemented!() @@ -1371,7 +1371,7 @@ mod tests { let mut instance = instance.unwrap(); // Successful execution. - let result = instance.execute("foo", &[], 0); + let result = instance.execute("foo", &[]); assert!(result.is_ok()); let result = result.unwrap(); assert!(!result.trapped()); From e6d92c05182226c16a2478dfa859cc046be6e5bd Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 30 Mar 2021 00:06:53 +0100 Subject: [PATCH 08/11] f --- bindings/rust/src/lib.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index cfce16e38..2df0bee25 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -209,7 +209,7 @@ unsafe extern "C" fn host_callback( unimplemented!() } -fn create_function_import_list(host_functions: &[HostFunction]) -> Vec { +fn create_function_import_list(host_functions: &[&HostFunction]) -> Vec { assert!(host_functions.len() == 1); let host_function = &host_functions[0]; let fn_type = sys::FizzyFunctionType { @@ -222,8 +222,8 @@ fn create_function_import_list(host_functions: &[HostFunction]) -> Vec Date: Tue, 30 Mar 2021 00:09:35 +0100 Subject: [PATCH 09/11] f --- bindings/rust/src/lib.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 2df0bee25..30dd59adc 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -206,10 +206,17 @@ unsafe extern "C" fn host_callback( context: *mut sys::FizzyExecutionContext, ) -> sys::FizzyExecutionResult { println!("host fuction called!"); - unimplemented!() + //unimplemented!() + sys::FizzyExecutionResult { + trapped: false, + has_value: false, + value: sys::FizzyValue { i32: 0 }, + } } -fn create_function_import_list(host_functions: &[&HostFunction]) -> Vec { +fn create_function_import_list( + host_functions: &[&HostFunction], +) -> Vec { assert!(host_functions.len() == 1); let host_function = &host_functions[0]; let fn_type = sys::FizzyFunctionType { @@ -222,11 +229,6 @@ fn create_function_import_list(host_functions: &[&HostFunction]) -> Vec Date: Tue, 24 May 2022 12:11:17 +0200 Subject: [PATCH 10/11] f --- bindings/rust/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 30dd59adc..c15f5800c 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -1325,9 +1325,7 @@ mod tests { assert!(instance.is_err()); assert_eq!( instance.err().unwrap(), - Error::InstantiationFailed( - "module requires 1 imported functions, 0 provided".to_string() - ) + Error::InstantiationFailed("imported function env.adler32 is required".to_string()) ); } From 8763a7f7eb97df6618ceacbf55e6661d3733e38d Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 27 May 2022 17:58:56 +0200 Subject: [PATCH 11/11] f --- bindings/rust/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index c15f5800c..3629ec8e5 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -1373,8 +1373,6 @@ mod tests { // Successful execution. let result = instance.execute("foo", &[]); assert!(result.is_ok()); - let result = result.unwrap(); - assert!(!result.trapped()); - assert!(result.value().is_none()); + assert!(result.unwrap().is_none()); } }