move wasi initialization inside fce

This commit is contained in:
vms 2020-06-12 01:24:07 +03:00
parent 8921535f0b
commit 1254b64b9d
4 changed files with 38 additions and 27 deletions

View File

@ -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);

View File

@ -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::<Vec<_>>();
}
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)
}

View File

@ -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) => {

View File

@ -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<WITInstance, WITExport, WITFunction, WITMemory, WITMemoryView<'static>>;
@ -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<WasmerInstance>,
wasmer_instance: Box<WasmerInstance>,
// 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<String, Arc<Callable>>,
}
@ -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<String, FCEModule>,
) -> Result<Self, FCEError> {
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::<Func<'_, (), ()>>("_start") {
// call _start to populate the WASI state of the module
#[rustfmt::skip]
if let Ok(start_func) = wasmer_instance.exports.get::<wasmer_runtime::Func<'_, (), ()>>("_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,
})
}