improve interfaces

This commit is contained in:
vms 2020-07-17 23:07:02 +03:00
parent 7e9c247f82
commit 43b765e61a
12 changed files with 85 additions and 57 deletions

View File

@ -47,29 +47,44 @@ impl FCE {
module_name: MN, module_name: MN,
func_name: FN, func_name: FN,
argument: &[IValue], argument: &[IValue],
) -> Result<Vec<IValue>, FCEError> { ) -> Result<Vec<IValue>> {
match self.modules.get_mut(module_name.as_ref()) { self.call_(module_name.as_ref(), func_name.as_ref(), argument)
}
pub fn call_(
&mut self,
module_name: &str,
func_name: &str,
argument: &[IValue],
) -> Result<Vec<IValue>> {
match self.modules.get_mut(module_name) {
// TODO: refactor errors // TODO: refactor errors
Some(module) => module.call(func_name.as_ref(), argument), Some(module) => module.call(func_name.as_ref(), argument),
None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())), None => Err(FCEError::NoSuchModule(module_name.to_string())),
} }
} }
/// Load a new module inside FCE. /// Load a new module inside FCE.
pub fn load_module<S>( pub fn load_module<S: Into<String>>(
&mut self, &mut self,
module_name: S, module_name: S,
wasm_bytes: &[u8], wasm_bytes: &[u8],
config: FCEModuleConfig, config: FCEModuleConfig,
) -> Result<(), FCEError> ) -> Result<()> {
where self.load_module_(module_name.into(), wasm_bytes, config)
S: Into<String>, }
{
pub fn load_module_(
&mut self,
module_name: String,
wasm_bytes: &[u8],
config: FCEModuleConfig,
) -> Result<()> {
let _prepared_wasm_bytes = crate::misc::prepare_module(wasm_bytes, config.mem_pages_count)?; let _prepared_wasm_bytes = crate::misc::prepare_module(wasm_bytes, config.mem_pages_count)?;
let module = FCEModule::new(&wasm_bytes, config, &self.modules)?; let module = FCEModule::new(&wasm_bytes, config, &self.modules)?;
match self.modules.entry(module_name.into()) { match self.modules.entry(module_name) {
Entry::Vacant(entry) => { Entry::Vacant(entry) => {
entry.insert(module); entry.insert(module);
Ok(()) Ok(())
@ -79,10 +94,14 @@ impl FCE {
} }
/// Unload previously loaded module. /// Unload previously loaded module.
pub fn unload_module<S: AsRef<str>>(&mut self, module_name: S) -> Result<(), FCEError> { pub fn unload_module<S: AsRef<str>>(&mut self, module_name: S) -> Result<()> {
match self.modules.remove(module_name.as_ref()) { self.unload_module_(module_name.as_ref())
}
pub fn unload_module_(&mut self, module_name: &str) -> Result<()> {
match self.modules.remove(module_name) {
Some(_) => Ok(()), Some(_) => Ok(()),
None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())), None => Err(FCEError::NoSuchModule(module_name.to_string())),
} }
} }
@ -100,7 +119,7 @@ impl FCE {
pub fn module_interface<S: AsRef<str>>( pub fn module_interface<S: AsRef<str>>(
&self, &self,
module_name: S, module_name: S,
) -> Result<Vec<FCEFunctionSignature<'_>>, FCEError> { ) -> Result<Vec<FCEFunctionSignature<'_>>> {
match self.modules.get(module_name.as_ref()) { match self.modules.get(module_name.as_ref()) {
Some(module) => Ok(Self::get_module_function_signatures(module)), Some(module) => Ok(Self::get_module_function_signatures(module)),
None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())), None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())),

View File

@ -39,4 +39,7 @@ pub use engine::FCEFunctionSignature;
pub use errors::FCEError; pub use errors::FCEError;
pub use module::IValue; pub use module::IValue;
pub use module::IType; pub use module::IType;
pub use module::{to_interface_value, from_interface_values}; pub use module::from_interface_values;
pub use module::to_interface_value;
pub(crate) type Result<T> = std::result::Result<T, FCEError>;

View File

@ -18,7 +18,7 @@
// https://github.com/paritytech/substrate/blob/master/srml/contracts/src/wasm/prepare.rs // https://github.com/paritytech/substrate/blob/master/srml/contracts/src/wasm/prepare.rs
// https://github.com/nearprotocol/nearcore/blob/master/runtime/near-vm-runner/src/prepare.rs // https://github.com/nearprotocol/nearcore/blob/master/runtime/near-vm-runner/src/prepare.rs
use crate::FCEError; use crate::Result;
use parity_wasm::{ use parity_wasm::{
builder, elements, builder, elements,
@ -30,7 +30,7 @@ struct ModuleBootstrapper {
} }
impl<'a> ModuleBootstrapper { impl<'a> ModuleBootstrapper {
fn init(module_code: &[u8]) -> Result<Self, FCEError> { fn init(module_code: &[u8]) -> Result<Self> {
let module = elements::deserialize_buffer(module_code)?; let module = elements::deserialize_buffer(module_code)?;
Ok(Self { module }) Ok(Self { module })
@ -66,14 +66,14 @@ impl<'a> ModuleBootstrapper {
} }
} }
fn into_wasm(self) -> Result<Vec<u8>, FCEError> { fn into_wasm(self) -> Result<Vec<u8>> {
elements::serialize(self.module).map_err(Into::into) elements::serialize(self.module).map_err(Into::into)
} }
} }
/// Prepares a Wasm module: /// Prepares a Wasm module:
/// - set memory page count /// - set memory page count
pub(crate) fn prepare_module(module: &[u8], mem_pages_count: u32) -> Result<Vec<u8>, FCEError> { pub(crate) fn prepare_module(module: &[u8], mem_pages_count: u32) -> Result<Vec<u8>> {
ModuleBootstrapper::init(module)? ModuleBootstrapper::init(module)?
.set_mem_pages_count(mem_pages_count) .set_mem_pages_count(mem_pages_count)
.into_wasm() .into_wasm()

View File

@ -14,7 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
use super::{IValue, IType}; use super::IValue;
use super::IType;
use wasmer_wit::interpreter::wasm; use wasmer_wit::interpreter::wasm;
// In current implementation export simply does nothing, because there is no more // In current implementation export simply does nothing, because there is no more

View File

@ -16,13 +16,15 @@
use super::wit_prelude::*; use super::wit_prelude::*;
use super::{IType, IValue, WValue}; use super::{IType, IValue, WValue};
use crate::Result;
use crate::FCEModuleConfig; use crate::FCEModuleConfig;
use fce_wit_interfaces::FCEWITInterfaces; use fce_wit_interfaces::FCEWITInterfaces;
use wasmer_wit::interpreter::Interpreter;
use wasmer_runtime::{compile, ImportObject};
use wasmer_core::Instance as WasmerInstance; use wasmer_core::Instance as WasmerInstance;
use wasmer_core::import::Namespace; use wasmer_core::import::Namespace;
use wasmer_runtime::compile;
use wasmer_runtime::ImportObject;
use wasmer_wit::interpreter::Interpreter;
use wit_parser::extract_wit; use wit_parser::extract_wit;
use std::collections::HashMap; use std::collections::HashMap;
@ -47,7 +49,7 @@ pub(super) struct Callable {
} }
impl Callable { impl Callable {
pub fn call(&mut self, args: &[IValue]) -> Result<Vec<IValue>, FCEError> { pub fn call(&mut self, args: &[IValue]) -> Result<Vec<IValue>> {
use wasmer_wit::interpreter::stack::Stackable; use wasmer_wit::interpreter::stack::Stackable;
let result = self let result = self
@ -86,7 +88,7 @@ impl FCEModule {
wasm_bytes: &[u8], wasm_bytes: &[u8],
fce_module_config: FCEModuleConfig, fce_module_config: FCEModuleConfig,
modules: &HashMap<String, FCEModule>, modules: &HashMap<String, FCEModule>,
) -> Result<Self, FCEError> { ) -> Result<Self> {
let wasmer_module = compile(&wasm_bytes)?; let wasmer_module = compile(&wasm_bytes)?;
let wit = extract_wit(&wasmer_module)?; let wit = extract_wit(&wasmer_module)?;
let fce_wit = FCEWITInterfaces::new(wit); let fce_wit = FCEWITInterfaces::new(wit);
@ -130,11 +132,7 @@ impl FCEModule {
}) })
} }
pub(crate) fn call( pub(crate) fn call(&mut self, function_name: &str, args: &[IValue]) -> Result<Vec<IValue>> {
&mut self,
function_name: &str,
args: &[IValue],
) -> Result<Vec<IValue>, FCEError> {
match self.exports_funcs.get_mut(function_name) { match self.exports_funcs.get_mut(function_name) {
Some(func) => Arc::make_mut(func).call(args), Some(func) => Arc::make_mut(func).call(args),
None => Err(FCEError::NoSuchFunction(format!( None => Err(FCEError::NoSuchFunction(format!(
@ -157,7 +155,7 @@ impl FCEModule {
} }
// TODO: change the cloning Callable behaviour after changes of Wasmer API // TODO: change the cloning Callable behaviour after changes of Wasmer API
pub(super) fn get_callable(&self, function_name: &str) -> Result<Arc<Callable>, FCEError> { pub(super) fn get_callable(&self, function_name: &str) -> Result<Arc<Callable>> {
match self.exports_funcs.get(function_name) { match self.exports_funcs.get(function_name) {
Some(func) => Ok(func.clone()), Some(func) => Ok(func.clone()),
None => Err(FCEError::NoSuchFunction(format!( None => Err(FCEError::NoSuchFunction(format!(
@ -170,7 +168,7 @@ impl FCEModule {
fn instantiate_wit_exports( fn instantiate_wit_exports(
wit_instance: Arc<WITInstance>, wit_instance: Arc<WITInstance>,
wit: &FCEWITInterfaces<'_>, wit: &FCEWITInterfaces<'_>,
) -> Result<HashMap<String, Arc<Callable>>, FCEError> { ) -> Result<HashMap<String, Arc<Callable>>> {
use fce_wit_interfaces::WITAstType; use fce_wit_interfaces::WITAstType;
wit.implementations() wit.implementations()
@ -218,14 +216,14 @@ impl FCEModule {
))), ))),
} }
}) })
.collect::<Result<HashMap<String, Arc<Callable>>, FCEError>>() .collect::<Result<HashMap<String, Arc<Callable>>>>()
} }
// this function deals only with import functions that have an adaptor implementation // this function deals only with import functions that have an adaptor implementation
fn adjust_wit_imports( fn adjust_wit_imports(
wit: &FCEWITInterfaces<'_>, wit: &FCEWITInterfaces<'_>,
wit_instance: Arc<MaybeUninit<WITInstance>>, wit_instance: Arc<MaybeUninit<WITInstance>>,
) -> Result<ImportObject, FCEError> { ) -> Result<ImportObject> {
use fce_wit_interfaces::WITAstType; use fce_wit_interfaces::WITAstType;
use wasmer_core::typed_func::DynamicFunc; use wasmer_core::typed_func::DynamicFunc;
use wasmer_core::vm::Ctx; use wasmer_core::vm::Ctx;
@ -327,7 +325,7 @@ impl FCEModule {
))), ))),
} }
}) })
.collect::<Result<multimap::MultiMap<_, _>, FCEError>>()?; .collect::<Result<multimap::MultiMap<_, _>>>()?;
let mut import_object = ImportObject::new(); let mut import_object = ImportObject::new();

View File

@ -21,11 +21,12 @@ mod wit_instance;
mod type_converters; mod type_converters;
mod fce_module; mod fce_module;
pub(crate) use fce_module::FCEModule;
pub use wasmer_wit::types::InterfaceType as IType; pub use wasmer_wit::types::InterfaceType as IType;
pub use wasmer_wit::values::InterfaceValue as IValue; pub use wasmer_wit::values::InterfaceValue as IValue;
pub use wasmer_wit::values::{to_interface_value, from_interface_values}; pub use wasmer_wit::values::from_interface_values;
pub use wasmer_wit::values::to_interface_value;
pub(crate) use fce_module::FCEModule;
pub(self) use wasmer_core::types::Type as WType; pub(self) use wasmer_core::types::Type as WType;
pub(self) use wasmer_core::types::Value as WValue; pub(self) use wasmer_core::types::Value as WValue;

View File

@ -14,10 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
use super::wit_prelude::FCEError;
use super::fce_module::FCEModule; use super::fce_module::FCEModule;
use super::{IType, IValue, WValue}; use super::{IType, IValue, WValue};
use super::fce_module::Callable; use super::fce_module::Callable;
use crate::Result;
use wasmer_wit::interpreter::wasm; use wasmer_wit::interpreter::wasm;
use wasmer_core::instance::DynFunc; use wasmer_core::instance::DynFunc;
@ -45,7 +45,7 @@ pub(super) struct WITFunction {
impl WITFunction { impl WITFunction {
/// Creates functions from a "usual" (not WIT) module export. /// Creates functions from a "usual" (not WIT) module export.
pub(super) fn from_export(dyn_func: DynFunc<'static>) -> Result<Self, FCEError> { pub(super) fn from_export(dyn_func: DynFunc<'static>) -> Result<Self> {
use super::type_converters::wtype_to_itype; use super::type_converters::wtype_to_itype;
let signature = dyn_func.signature(); let signature = dyn_func.signature();
@ -70,10 +70,7 @@ impl WITFunction {
} }
/// Creates function from a module import. /// Creates function from a module import.
pub(super) fn from_import( pub(super) fn from_import(wit_module: &FCEModule, function_name: &str) -> Result<Self> {
wit_module: &FCEModule,
function_name: &str,
) -> Result<Self, FCEError> {
let callable = wit_module.get_callable(function_name)?; let callable = wit_module.get_callable(function_name)?;
let inner = WITFunctionInner::Import { callable }; let inner = WITFunctionInner::Import { callable };
@ -111,7 +108,7 @@ impl wasm::structures::LocalImport for WITFunction {
} }
} }
fn call(&self, arguments: &[IValue]) -> Result<Vec<IValue>, ()> { fn call(&self, arguments: &[IValue]) -> std::result::Result<Vec<IValue>, ()> {
use super::type_converters::{ival_to_wval, wval_to_ival}; use super::type_converters::{ival_to_wval, wval_to_ival};
match &self.inner { match &self.inner {

View File

@ -16,6 +16,7 @@
use super::wit_prelude::*; use super::wit_prelude::*;
use super::fce_module::FCEModule; use super::fce_module::FCEModule;
use crate::Result;
use fce_wit_interfaces::FCEWITInterfaces; use fce_wit_interfaces::FCEWITInterfaces;
use fce_wit_interfaces::WITAstType; use fce_wit_interfaces::WITAstType;
@ -37,7 +38,7 @@ impl WITInstance {
wasmer_instance: &WasmerInstance, wasmer_instance: &WasmerInstance,
wit: &FCEWITInterfaces<'_>, wit: &FCEWITInterfaces<'_>,
modules: &HashMap<String, FCEModule>, modules: &HashMap<String, FCEModule>,
) -> Result<Self, FCEError> { ) -> Result<Self> {
let mut exports = Self::extract_raw_exports(&wasmer_instance, wit)?; let mut exports = Self::extract_raw_exports(&wasmer_instance, wit)?;
let imports = Self::extract_imports(modules, wit, exports.len())?; let imports = Self::extract_imports(modules, wit, exports.len())?;
let memories = Self::extract_memories(&wasmer_instance); let memories = Self::extract_memories(&wasmer_instance);
@ -51,7 +52,7 @@ impl WITInstance {
fn extract_raw_exports( fn extract_raw_exports(
wasmer_instance: &WasmerInstance, wasmer_instance: &WasmerInstance,
wit: &FCEWITInterfaces<'_>, wit: &FCEWITInterfaces<'_>,
) -> Result<HashMap<usize, WITFunction>, FCEError> { ) -> Result<HashMap<usize, WITFunction>> {
use wasmer_core::DynFunc; use wasmer_core::DynFunc;
let module_exports = &wasmer_instance.exports; let module_exports = &wasmer_instance.exports;
@ -76,7 +77,7 @@ impl WITInstance {
modules: &HashMap<String, FCEModule>, modules: &HashMap<String, FCEModule>,
wit: &FCEWITInterfaces<'_>, wit: &FCEWITInterfaces<'_>,
start_index: usize, start_index: usize,
) -> Result<HashMap<usize, WITFunction>, FCEError> { ) -> Result<HashMap<usize, WITFunction>> {
wit.imports() wit.imports()
.filter(|import| .filter(|import|
// filter out imports that have implementations // filter out imports that have implementations
@ -89,7 +90,7 @@ impl WITInstance {
} }
None => Err(FCEError::NoSuchModule(import.namespace.to_string())), None => Err(FCEError::NoSuchModule(import.namespace.to_string())),
}) })
.collect::<Result<HashMap<_, _>, _>>() .collect::<Result<HashMap<_, _>>>()
} }
fn extract_memories(wasmer_instance: &WasmerInstance) -> Vec<WITMemory> { fn extract_memories(wasmer_instance: &WasmerInstance) -> Vec<WITMemory> {

View File

@ -106,7 +106,16 @@ impl FluenceFaaS {
} }
/// Executes provided Wasm code in the internal environment (with access to module exports). /// Executes provided Wasm code in the internal environment (with access to module exports).
pub fn call_code( pub fn call_code<S: AsRef<str>>(
&mut self,
wasm: &[u8],
func_name: S,
args: &[IValue],
) -> Result<Vec<IValue>> {
self.call_code_(wasm, func_name.as_ref(), args)
}
pub fn call_code_(
&mut self, &mut self,
wasm: &[u8], wasm: &[u8],
func_name: &str, func_name: &str,
@ -125,10 +134,10 @@ impl FluenceFaaS {
} }
/// Call a specified function of loaded on a startup module by its name. /// Call a specified function of loaded on a startup module by its name.
pub fn call_module( pub fn call_module<MN: AsRef<str>, FN: AsRef<str>>(
&mut self, &mut self,
module_name: &str, module_name: MN,
func_name: &str, func_name: FN,
args: &[IValue], args: &[IValue],
) -> Result<Vec<IValue>> { ) -> Result<Vec<IValue>> {
self.fce self.fce

View File

@ -14,20 +14,19 @@
* limitations under the License. * limitations under the License.
*/ */
use wasmer_core::vm::Ctx;
use crate::FaaSError;
use super::config::ModuleConfig; use super::config::ModuleConfig;
use fce::FCEModuleConfig; use fce::FCEModuleConfig;
use wasmer_core::backend::SigRegistry;
use wasmer_core::module::ExportIndex;
use wasmer_core::vm::Ctx;
use wasmer_core::import::ImportObject; use wasmer_core::import::ImportObject;
use wasmer_runtime::func;
use wasmer_core::typed_func::WasmTypeList; use wasmer_core::typed_func::WasmTypeList;
use wasmer_runtime::func;
use wasmer_runtime::Func; use wasmer_runtime::Func;
use wasmer_runtime::error::ResolveError; use wasmer_runtime::error::ResolveError;
use wasmer_core::backend::SigRegistry;
use wasmer_runtime::types::LocalOrImport; use wasmer_runtime::types::LocalOrImport;
use wasmer_core::module::ExportIndex;
use std::path::PathBuf; use std::path::PathBuf;
@ -103,7 +102,7 @@ where
} }
/// Make FCE config based on parsed raw config. /// Make FCE config based on parsed raw config.
pub(crate) fn make_fce_config(config: Option<ModuleConfig>) -> Result<FCEModuleConfig, FaaSError> { pub(crate) fn make_fce_config(config: Option<ModuleConfig>) -> crate::Result<FCEModuleConfig> {
use super::imports::create_host_import_func; use super::imports::create_host_import_func;
use super::imports::log_utf8_string; use super::imports::log_utf8_string;
use wasmer_core::import::Namespace; use wasmer_core::import::Namespace;