From 880bb6f87e665fa46eeb2c8aee1c187582c4c937 Mon Sep 17 00:00:00 2001 From: vms Date: Tue, 2 Jun 2020 01:02:22 +0300 Subject: [PATCH] cargo fmt all the things --- wit_fce/examples/export_test/src/lib.rs | 1 + wit_fce/examples/export_test/src/mem.rs | 1 - wit_fce/examples/export_test/src/result.rs | 1 - wit_fce/examples/ipfs_node/src/lib.rs | 17 +++----- wit_fce/examples/ipfs_node/src/mem.rs | 3 +- wit_fce/examples/ipfs_node/src/result.rs | 7 ++-- wit_fce/examples/ipfs_rpc/src/lib.rs | 11 +++-- wit_fce/examples/ipfs_rpc/src/mem.rs | 3 +- wit_fce/examples/ipfs_rpc/src/result.rs | 7 ++-- wit_fce/src/instance/wit_function.rs | 1 + wit_fce/src/instance/wit_module.rs | 48 +++++++++++----------- 11 files changed, 45 insertions(+), 55 deletions(-) diff --git a/wit_fce/examples/export_test/src/lib.rs b/wit_fce/examples/export_test/src/lib.rs index 9e9db848..9646e714 100644 --- a/wit_fce/examples/export_test/src/lib.rs +++ b/wit_fce/examples/export_test/src/lib.rs @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#![allow(clippy::missing_safety_doc)] mod mem; mod result; diff --git a/wit_fce/examples/export_test/src/mem.rs b/wit_fce/examples/export_test/src/mem.rs index e39eb89a..4dd7abd8 100644 --- a/wit_fce/examples/export_test/src/mem.rs +++ b/wit_fce/examples/export_test/src/mem.rs @@ -14,7 +14,6 @@ * limitations under the License. */ - use std::alloc::{alloc as global_alloc, dealloc as global_dealloc, Layout}; use std::ptr::NonNull; diff --git a/wit_fce/examples/export_test/src/result.rs b/wit_fce/examples/export_test/src/result.rs index 872ef773..57ba678c 100644 --- a/wit_fce/examples/export_test/src/result.rs +++ b/wit_fce/examples/export_test/src/result.rs @@ -14,7 +14,6 @@ * limitations under the License. */ - use std::sync::atomic::AtomicUsize; pub static mut RESULT_PTR: AtomicUsize = AtomicUsize::new(0); diff --git a/wit_fce/examples/ipfs_node/src/lib.rs b/wit_fce/examples/ipfs_node/src/lib.rs index 4fe56e50..5d5f6a05 100644 --- a/wit_fce/examples/ipfs_node/src/lib.rs +++ b/wit_fce/examples/ipfs_node/src/lib.rs @@ -14,6 +14,8 @@ * limitations under the License. */ +#![allow(clippy::missing_safety_doc)] + mod mem; mod result; @@ -21,17 +23,14 @@ use crate::result::{RESULT_PTR, RESULT_SIZE}; #[no_mangle] pub unsafe fn put(file_content_ptr: *mut u8, file_content_size: usize) { - let file_content = String::from_raw_parts( - file_content_ptr, - file_content_size, - file_content_size - ); + let file_content = + String::from_raw_parts(file_content_ptr, file_content_size, file_content_size); let msg = format!("from Wasm node: file content is {}\n", file_content); log_utf8_string(msg.as_ptr() as _, msg.len() as _); let cmd = format!("put {}", file_content); - ipfs(file_content.as_ptr() as _, file_content.len() as _); + ipfs(cmd.as_ptr() as _, cmd.len() as _); let result = "Hello from IPFS node, take your hash".to_string(); @@ -42,11 +41,7 @@ pub unsafe fn put(file_content_ptr: *mut u8, file_content_size: usize) { #[no_mangle] pub unsafe fn get(hash_ptr: *mut u8, hash_size: usize) { - let hash = String::from_raw_parts( - hash_ptr, - hash_size, - hash_size - ); + let hash = String::from_raw_parts(hash_ptr, hash_size, hash_size); let msg = format!("from Wasm node: file content is {}\n", hash); log_utf8_string(msg.as_ptr() as _, msg.len() as _); diff --git a/wit_fce/examples/ipfs_node/src/mem.rs b/wit_fce/examples/ipfs_node/src/mem.rs index 23231126..a9fc45d6 100644 --- a/wit_fce/examples/ipfs_node/src/mem.rs +++ b/wit_fce/examples/ipfs_node/src/mem.rs @@ -14,9 +14,8 @@ * limitations under the License. */ - -use std::alloc::{alloc as global_alloc, dealloc as global_dealloc, Layout}; use crate::log_utf8_string; +use std::alloc::{alloc as global_alloc, dealloc as global_dealloc, Layout}; use std::ptr::NonNull; /// Allocates memory area of specified size and returns its address. diff --git a/wit_fce/examples/ipfs_node/src/result.rs b/wit_fce/examples/ipfs_node/src/result.rs index b0144bf4..e294922e 100644 --- a/wit_fce/examples/ipfs_node/src/result.rs +++ b/wit_fce/examples/ipfs_node/src/result.rs @@ -14,16 +14,15 @@ * limitations under the License. */ - -use std::sync::atomic::AtomicUsize; use crate::log_utf8_string; +use std::sync::atomic::AtomicUsize; pub static mut RESULT_PTR: AtomicUsize = AtomicUsize::new(0); pub static mut RESULT_SIZE: AtomicUsize = AtomicUsize::new(0); #[no_mangle] pub unsafe fn get_result_ptr() -> usize { - let msg = format!("wasm_node: calling get_result_ptr\n"); + let msg = "wasm_node: calling get_result_ptr\n"; log_utf8_string(msg.as_ptr() as _, msg.len() as _); *RESULT_PTR.get_mut() @@ -31,7 +30,7 @@ pub unsafe fn get_result_ptr() -> usize { #[no_mangle] pub unsafe fn get_result_size() -> usize { - let msg = format!("wasm_node: calling get_result_size\n"); + let msg = "wasm_node: calling get_result_size\n"; log_utf8_string(msg.as_ptr() as _, msg.len() as _); *RESULT_SIZE.get_mut() diff --git a/wit_fce/examples/ipfs_rpc/src/lib.rs b/wit_fce/examples/ipfs_rpc/src/lib.rs index 33348394..bb1f50ec 100644 --- a/wit_fce/examples/ipfs_rpc/src/lib.rs +++ b/wit_fce/examples/ipfs_rpc/src/lib.rs @@ -13,19 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#![allow(clippy::missing_safety_doc)] mod mem; mod result; -use crate::result::{RESULT_PTR, RESULT_SIZE}; +// use crate::result::{RESULT_PTR, RESULT_SIZE}; #[no_mangle] pub unsafe fn invoke(file_content_ptr: *mut u8, file_content_size: usize) { - let file_content = String::from_raw_parts( - file_content_ptr, - file_content_size, - file_content_size, - ); + let file_content = + String::from_raw_parts(file_content_ptr, file_content_size, file_content_size); let msg = format!("from Wasm rpc: file_content is {}\n", file_content); log_utf8_string(msg.as_ptr() as _, msg.len() as _); @@ -54,6 +52,7 @@ extern "C" { /// Put a file to ipfs, returns ipfs hash of the file. fn put(ptr: i32, size: i32); + #[allow(unused)] /// Get file from ipfs by hash. fn get(ptr: i32, size: i32); } diff --git a/wit_fce/examples/ipfs_rpc/src/mem.rs b/wit_fce/examples/ipfs_rpc/src/mem.rs index 60dfca94..a0f490c2 100644 --- a/wit_fce/examples/ipfs_rpc/src/mem.rs +++ b/wit_fce/examples/ipfs_rpc/src/mem.rs @@ -14,9 +14,8 @@ * limitations under the License. */ - -use std::alloc::{alloc as global_alloc, dealloc as global_dealloc, Layout}; use crate::log_utf8_string; +use std::alloc::{alloc as global_alloc, dealloc as global_dealloc, Layout}; use std::ptr::NonNull; /// Allocates memory area of specified size and returns its address. diff --git a/wit_fce/examples/ipfs_rpc/src/result.rs b/wit_fce/examples/ipfs_rpc/src/result.rs index 3091d355..4daf8179 100644 --- a/wit_fce/examples/ipfs_rpc/src/result.rs +++ b/wit_fce/examples/ipfs_rpc/src/result.rs @@ -14,16 +14,15 @@ * limitations under the License. */ - -use std::sync::atomic::AtomicUsize; use crate::log_utf8_string; +use std::sync::atomic::AtomicUsize; pub static mut RESULT_PTR: AtomicUsize = AtomicUsize::new(0); pub static mut RESULT_SIZE: AtomicUsize = AtomicUsize::new(0); #[no_mangle] pub unsafe fn get_result_ptr() -> usize { - let msg = format!("wasm_rpc: calling get_result_ptr\n"); + let msg = "wasm_rpc: calling get_result_ptr\n"; log_utf8_string(msg.as_ptr() as _, msg.len() as _); *RESULT_PTR.get_mut() @@ -31,7 +30,7 @@ pub unsafe fn get_result_ptr() -> usize { #[no_mangle] pub unsafe fn get_result_size() -> usize { - let msg = format!("wasm_rpc: calling get_result_size\n"); + let msg = "wasm_rpc: calling get_result_size\n"; log_utf8_string(msg.as_ptr() as _, msg.len() as _); *RESULT_SIZE.get_mut() diff --git a/wit_fce/src/instance/wit_function.rs b/wit_fce/src/instance/wit_function.rs index 5f5d6d7b..2116820d 100644 --- a/wit_fce/src/instance/wit_function.rs +++ b/wit_fce/src/instance/wit_function.rs @@ -30,6 +30,7 @@ enum WITFunctionInner { outputs: Vec, }, Import { + // TODO: use WITInstance here instead of WITModule wit_module: Arc, func_name: String, inputs: Vec, diff --git a/wit_fce/src/instance/wit_module.rs b/wit_fce/src/instance/wit_module.rs index 2179163e..3f48cdf6 100644 --- a/wit_fce/src/instance/wit_module.rs +++ b/wit_fce/src/instance/wit_module.rs @@ -39,12 +39,13 @@ use wasmer_runtime_core::import::Namespace; const WIT_SECTION_NAME: &str = "interface-types"; type WITInterpreter = Interpreter>; +type WITModuleFunc = (WITInterpreter, Vec, Vec); pub struct WITModule { #[allow(unused)] instance: WasmerInstance, wit_instance: Arc, - funcs: HashMap, Vec)>, + funcs: HashMap, } impl WITModule { @@ -128,10 +129,7 @@ impl WITModule { fn extract_wit_exports( interfaces: &Interfaces, - ) -> Result< - HashMap, Vec)>, - WITFCEError, - > { + ) -> Result, WITFCEError> { let exports_type_to_names = interfaces .exports .iter() @@ -254,29 +252,31 @@ impl WITModule { let interpreter: WITInterpreter = instructions.try_into().unwrap(); let wit_instance = wit_instance.clone(); - let inner_import = Box::new(move |_: &mut Ctx, inputs: &[Value]| -> Vec { - // copy here to because otherwise wit_instance will be consumed by the closure - let wit_instance_callable = wit_instance.clone(); - let converted_inputs = inputs.iter().map(wval_to_ival).collect::>(); - unsafe { - // error here will be propagated by the special error instruction - let _ = interpreter.run( - &converted_inputs, - Arc::make_mut(&mut wit_instance_callable.assume_init()), - ); - } + let wit_inner_import = + Box::new(move |_: &mut Ctx, inputs: &[Value]| -> Vec { + // copy here to because otherwise wit_instance will be consumed by the closure + let wit_instance_callable = wit_instance.clone(); + let converted_inputs = inputs.iter().map(wval_to_ival).collect::>(); + unsafe { + // error here will be propagated by the special error instruction + let _ = interpreter.run( + &converted_inputs, + Arc::make_mut(&mut wit_instance_callable.assume_init()), + ); + } - // wit import functions should only change the stack state - - // the result will be returned by an export function - vec![] - }); + // wit import functions should only change the stack state - + // the result will be returned by an export function + vec![] + }); - let linking_import = dyn_func_from_imports(inputs.clone(), inner_import); + let wit_import = dyn_func_from_imports(inputs.clone(), wit_inner_import); - let mut n = Namespace::new(); - n.insert(func_name.clone(), linking_import); + // TODO: refactor this + let mut module_namespace = Namespace::new(); + module_namespace.insert(func_name.clone(), wit_import); - import_namespaces.insert(namespace, n); + import_namespaces.insert(namespace, module_namespace); } else { // TODO: change error type return Err(WITFCEError::WasmerResolveError(format!(