diff --git a/examples/ipfs_node/src/main.rs b/examples/ipfs_node/src/main.rs index 55e799c5..8a95b846 100644 --- a/examples/ipfs_node/src/main.rs +++ b/examples/ipfs_node/src/main.rs @@ -45,7 +45,13 @@ fn main() { println!("ipfs node interface is\n{}", ipfs_node.get_interface()); let result = ipfs_node - .rpc_call(&ipfs_rpc, "put", &[IValue::String("QmdHsYnAvbrvXg3iwr6bLaqooVT31E8CMpZRWc9wX2Fbt8".to_string())]) + .rpc_call( + &ipfs_rpc, + "put", + &[IValue::String( + "QmdHsYnAvbrvXg3iwr6bLaqooVT31E8CMpZRWc9wX2Fbt8".to_string(), + )], + ) .unwrap(); println!("execution result {:?}", result); diff --git a/examples/ipfs_node/src/node.rs b/examples/ipfs_node/src/node.rs index 01d01775..62103820 100644 --- a/examples/ipfs_node/src/node.rs +++ b/examples/ipfs_node/src/node.rs @@ -19,14 +19,12 @@ use crate::config::ModuleConfig; use crate::node_public_interface::NodePublicInterface; use crate::node_public_interface::NodeModulePublicInterface; -use wasmer_core::import::ImportObject; -use wasmer_runtime::func; use fce::FCE; use fce::WasmProcess; use fce::IValue; use fce::FCEModuleConfig; - -use wasmer_wasi::generate_import_object_for_version; +use wasmer_core::import::ImportObject; +use wasmer_runtime::func; use std::collections::HashMap; use std::fs; @@ -118,7 +116,6 @@ impl IpfsNode { use crate::imports::create_host_import_func; use crate::imports::log_utf8_string; use wasmer_core::import::Namespace; - use wasmer_wasi::WasiVersion; let mut wasm_module_config = FCEModuleConfig::default(); @@ -139,7 +136,7 @@ impl IpfsNode { } } - let mut import_object = if let Some(wasi) = module_config.wasi { + if let Some(wasi) = module_config.wasi { if let Some(envs) = wasi.envs { wasm_module_config.wasi_envs = envs; } @@ -157,16 +154,6 @@ impl IpfsNode { .map(|(from, to)| (from, PathBuf::from(to))) .collect::>(); } - - generate_import_object_for_version( - WasiVersion::Latest, - vec![], - wasm_module_config.wasi_envs.clone(), - wasm_module_config.wasi_preopened_files.clone(), - wasm_module_config.wasi_mapped_dirs.clone(), - ) - } else { - ImportObject::new() }; let _mapped_dirs = wasm_module_config @@ -182,10 +169,11 @@ impl IpfsNode { } } + let mut import_object = ImportObject::new(); import_object.register("host", namespace); wasm_module_config.imports = import_object; - wasm_module_config.wasi_version = WasiVersion::Latest; + wasm_module_config.wasi_version = wasmer_wasi::WasiVersion::Latest; Ok(wasm_module_config) } diff --git a/fce/src/vm/fce.rs b/fce/src/vm/fce.rs index d7c0d31d..0f028fed 100644 --- a/fce/src/vm/fce.rs +++ b/fce/src/vm/fce.rs @@ -68,7 +68,7 @@ impl WasmProcess for FCE { let _prepared_wasm_bytes = super::prepare::prepare_module(wasm_bytes, config.mem_pages_count)?; - let module = FCEModule::new(&wasm_bytes, config.imports, &self.modules)?; + let module = FCEModule::new(&wasm_bytes, config, &self.modules)?; match self.modules.entry(module_name.into()) { Entry::Vacant(entry) => { diff --git a/fce/src/vm/module/fce_module.rs b/fce/src/vm/module/fce_module.rs index ba6ddc46..64e325ef 100644 --- a/fce/src/vm/module/fce_module.rs +++ b/fce/src/vm/module/fce_module.rs @@ -28,6 +28,7 @@ use std::collections::HashMap; use std::convert::TryInto; use std::mem::MaybeUninit; use std::sync::Arc; +use crate::FCEModuleConfig; type WITInterpreter = Interpreter>; @@ -64,13 +65,18 @@ pub struct FCEModule { // wasmer_instance is needed because WITInstance contains dynamic functions // that internally keep pointer to Wasmer instance. #[allow(unused)] - wamser_instance: Box, + wasmer_instance: Box, // import_object is needed because ImportObject::extend doesn't really deep copy // imports, so we need to store imports of this module to prevent their removing. #[allow(unused)] import_object: ImportObject, + // host_import_object is needed because ImportObject::extend doesn't really deep copy + // imports, so we need to store imports of this module to prevent their removing. + #[allow(unused)] + host_import_object: ImportObject, + // TODO: replace with dyn Trait exports_funcs: HashMap>, } @@ -78,20 +84,28 @@ pub struct FCEModule { impl FCEModule { pub fn new( wasm_bytes: &[u8], - mut fce_imports: ImportObject, + fce_module_config: FCEModuleConfig, modules: &HashMap, ) -> Result { - use wasmer_runtime::Func; - let wasmer_module = compile(&wasm_bytes)?; let wit = extract_wit(&wasmer_module)?; let fce_wit = FCEWITInterfaces::new(wit); let mut wit_instance = Arc::new_uninit(); let import_object = Self::adjust_wit_imports(&fce_wit, wit_instance.clone())?; - fce_imports.extend(import_object.clone()); - let wasmer_instance = wasmer_module.instantiate(&fce_imports)?; + let mut wasi_import_object = wasmer_wasi::generate_import_object_for_version( + fce_module_config.wasi_version, + vec![], + fce_module_config.wasi_envs.clone(), + fce_module_config.wasi_preopened_files.clone(), + fce_module_config.wasi_mapped_dirs.clone(), + ); + + wasi_import_object.extend(import_object.clone()); + wasi_import_object.extend(fce_module_config.imports.clone()); + + let wasmer_instance = wasmer_module.instantiate(&wasi_import_object)?; let wit_instance = unsafe { // get_mut_unchecked here is safe because currently only this modules have reference to // it and the environment is single-threaded @@ -102,13 +116,16 @@ impl FCEModule { let exports_funcs = Self::instantiate_wit_exports(wit_instance, &fce_wit)?; - if let Ok(start_func) = wasmer_instance.exports.get::>("_start") { + // call _start to populate the WASI state of the module + #[rustfmt::skip] + if let Ok(start_func) = wasmer_instance.exports.get::>("_start") { start_func.call()?; } Ok(Self { - wamser_instance: Box::new(wasmer_instance), + wasmer_instance: Box::new(wasmer_instance), import_object, + host_import_object: fce_module_config.imports, exports_funcs, }) }