From 60c9f03902ccb08ae47579b86a9dec6a06719d6c Mon Sep 17 00:00:00 2001 From: vms Date: Sat, 6 Jun 2020 01:03:49 +0300 Subject: [PATCH] simplify the wit extrator --- crates/wit_parser/src/extractor.rs | 42 +++++++----------------------- crates/wit_parser/src/lib.rs | 2 +- examples/ipfs_node/Config.toml | 2 +- fce/src/vm/instance/fce_module.rs | 11 ++++---- fce/src/wasm_process.rs | 5 ++-- 5 files changed, 20 insertions(+), 42 deletions(-) diff --git a/crates/wit_parser/src/extractor.rs b/crates/wit_parser/src/extractor.rs index d3aa4828..f7728644 100644 --- a/crates/wit_parser/src/extractor.rs +++ b/crates/wit_parser/src/extractor.rs @@ -17,7 +17,6 @@ use super::custom::WIT_SECTION_NAME; use super::errors::WITParserError; -use fce_wit_interfaces::FCEWITInterfaces; use walrus::{IdsToIndices, ModuleConfig}; use wasmer_wit::ast::Interfaces; use wasmer_core::Module as WasmerModule; @@ -27,16 +26,12 @@ use std::path::PathBuf; /// Extracts WIT section of provided Wasm binary and converts it to a string. pub fn extract_text_wit(wasm_file_path: PathBuf) -> Result { let wit_section_bytes = extract_wit_section_bytes(wasm_file_path)?; - extract_wit_with_fn( - &wit_section_bytes, - |wit: Interfaces<'_>| -> Result { Ok((&wit).to_string()) }, - ) + let wit = extract_wit_with_fn(&wit_section_bytes)?; + Ok((&wit).to_string()) } /// Extracts WIT section of provided Wasm binary and converts it to a FCEWITInterfaces. -pub fn extract_fce_wit( - wasmer_module: &WasmerModule, -) -> Result, WITParserError> { +pub fn extract_wit(wasmer_module: &WasmerModule) -> Result, WITParserError> { let wit_sections = wasmer_module .custom_sections(WIT_SECTION_NAME) .ok_or_else(|| WITParserError::NoWITSection)?; @@ -45,32 +40,15 @@ pub fn extract_fce_wit( return Err(WITParserError::MultipleWITSections); } - extract_wit_with_fn( - &wit_sections[0], - |wit: Interfaces<'_>| -> Result, WITParserError> { - Ok(FCEWITInterfaces::new(wit)) - }, - ) + extract_wit_with_fn(&wit_sections[0]) } -fn extract_wit_with_fn<'a, F, FResultType: 'a>( - wit_section_bytes: &'a [u8], - func: F, -) -> Result -where - F: FnOnce(Interfaces<'a>) -> Result, -{ - let raw_wit = match wasmer_wit::decoders::binary::parse::<()>(&wit_section_bytes) { - Ok((remainder, wit)) if remainder.is_empty() => wit, - Ok(_) => { - return Err(WITParserError::WITRemainderNotEmpty); - } - Err(_) => { - return Err(WITParserError::CorruptedWITSection); - } - }; - - func(raw_wit) +fn extract_wit_with_fn(wit_section_bytes: &[u8]) -> Result, WITParserError> { + match wasmer_wit::decoders::binary::parse::<()>(&wit_section_bytes) { + Ok((remainder, wit)) if remainder.is_empty() => Ok(wit), + Ok(_) => Err(WITParserError::WITRemainderNotEmpty), + Err(_) => Err(WITParserError::CorruptedWITSection), + } } fn extract_wit_section_bytes(wasm_file_path: PathBuf) -> Result, WITParserError> { diff --git a/crates/wit_parser/src/lib.rs b/crates/wit_parser/src/lib.rs index 99874dc4..5aa4d944 100644 --- a/crates/wit_parser/src/lib.rs +++ b/crates/wit_parser/src/lib.rs @@ -34,5 +34,5 @@ pub use errors::WITParserError; pub use embedder::embed_text_wit; pub use deleter::delete_wit_section; -pub use extractor::extract_fce_wit; +pub use extractor::extract_wit; pub use extractor::extract_text_wit; diff --git a/examples/ipfs_node/Config.toml b/examples/ipfs_node/Config.toml index 977287f2..9a44af68 100644 --- a/examples/ipfs_node/Config.toml +++ b/examples/ipfs_node/Config.toml @@ -3,4 +3,4 @@ ipfs = "/usr/bin/ipfs" [wasi] - preopened_dir = ["/tmp"] + preopened_dirs = ["/tmp"] diff --git a/fce/src/vm/instance/fce_module.rs b/fce/src/vm/instance/fce_module.rs index 87874d66..eca2f389 100644 --- a/fce/src/vm/instance/fce_module.rs +++ b/fce/src/vm/instance/fce_module.rs @@ -22,7 +22,7 @@ use wasmer_wit::interpreter::Interpreter; use wasmer_runtime::{compile, ImportObject}; use wasmer_core::Instance as WasmerInstance; use wasmer_core::import::Namespace; -use wit_parser::extract_fce_wit; +use wit_parser::extract_wit; use std::collections::HashMap; use std::convert::TryInto; @@ -54,11 +54,12 @@ impl FCEModule { modules: &HashMap>, ) -> Result { let wasmer_module = compile(&wasm_bytes)?; - let wit = extract_fce_wit(&wasmer_module)?; - let wit_exports = Self::instantiate_wit_exports(&wit)?; + let wit = extract_wit(&wasmer_module)?; + let fce_wit = FCEWITInterfaces::new(wit); + let wit_exports = Self::instantiate_wit_exports(&fce_wit)?; let mut wit_instance = Arc::new_uninit(); - let mut import_object = Self::adjust_wit_imports(&wit, wit_instance.clone())?; + let mut import_object = Self::adjust_wit_imports(&fce_wit, wit_instance.clone())?; import_object.extend(imports); let wasmer_instance = wasmer_module.instantiate(&import_object)?; @@ -67,7 +68,7 @@ impl FCEModule { // get_mut_unchecked here is safe because currently only this modules have reference to // it and the environment is single-threaded *Arc::get_mut_unchecked(&mut wit_instance) = - MaybeUninit::new(WITInstance::new(&wasmer_instance, &wit, modules)?); + MaybeUninit::new(WITInstance::new(&wasmer_instance, &fce_wit, modules)?); std::mem::transmute::<_, Arc>(wit_instance) }; diff --git a/fce/src/wasm_process.rs b/fce/src/wasm_process.rs index 583553d4..c64f4576 100644 --- a/fce/src/wasm_process.rs +++ b/fce/src/wasm_process.rs @@ -35,8 +35,7 @@ pub trait WasmProcess { arguments: &[IValue], ) -> Result, FCEError>; - /// Registers new module in the FCE Service. - /// TODO: + /// Loads new module in the FCE Service. fn load_module( &mut self, module_name: S, @@ -46,7 +45,7 @@ pub trait WasmProcess { where S: Into; - /// Unregisters previously registered module. + /// Unloads previously registered module. fn unload_module(&mut self, module_name: &str) -> Result<(), FCEError>; /// Returns signatures of all exported functions by this module.