mirror of
https://github.com/fluencelabs/marine.git
synced 2024-12-12 06:45:32 +00:00
improve interfaces
This commit is contained in:
parent
7e9c247f82
commit
43b765e61a
@ -47,29 +47,44 @@ impl FCE {
|
||||
module_name: MN,
|
||||
func_name: FN,
|
||||
argument: &[IValue],
|
||||
) -> Result<Vec<IValue>, FCEError> {
|
||||
match self.modules.get_mut(module_name.as_ref()) {
|
||||
) -> Result<Vec<IValue>> {
|
||||
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
|
||||
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.
|
||||
pub fn load_module<S>(
|
||||
pub fn load_module<S: Into<String>>(
|
||||
&mut self,
|
||||
module_name: S,
|
||||
wasm_bytes: &[u8],
|
||||
config: FCEModuleConfig,
|
||||
) -> Result<(), FCEError>
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
) -> Result<()> {
|
||||
self.load_module_(module_name.into(), wasm_bytes, config)
|
||||
}
|
||||
|
||||
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 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.insert(module);
|
||||
Ok(())
|
||||
@ -79,10 +94,14 @@ impl FCE {
|
||||
}
|
||||
|
||||
/// Unload previously loaded module.
|
||||
pub fn unload_module<S: AsRef<str>>(&mut self, module_name: S) -> Result<(), FCEError> {
|
||||
match self.modules.remove(module_name.as_ref()) {
|
||||
pub fn unload_module<S: AsRef<str>>(&mut self, module_name: S) -> Result<()> {
|
||||
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(()),
|
||||
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>>(
|
||||
&self,
|
||||
module_name: S,
|
||||
) -> Result<Vec<FCEFunctionSignature<'_>>, FCEError> {
|
||||
) -> Result<Vec<FCEFunctionSignature<'_>>> {
|
||||
match self.modules.get(module_name.as_ref()) {
|
||||
Some(module) => Ok(Self::get_module_function_signatures(module)),
|
||||
None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())),
|
||||
|
@ -39,4 +39,7 @@ pub use engine::FCEFunctionSignature;
|
||||
pub use errors::FCEError;
|
||||
pub use module::IValue;
|
||||
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>;
|
||||
|
@ -18,7 +18,7 @@
|
||||
// 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
|
||||
|
||||
use crate::FCEError;
|
||||
use crate::Result;
|
||||
|
||||
use parity_wasm::{
|
||||
builder, elements,
|
||||
@ -30,7 +30,7 @@ struct 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)?;
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
/// Prepares a Wasm module:
|
||||
/// - 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)?
|
||||
.set_mem_pages_count(mem_pages_count)
|
||||
.into_wasm()
|
||||
|
@ -14,7 +14,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use super::{IValue, IType};
|
||||
use super::IValue;
|
||||
use super::IType;
|
||||
use wasmer_wit::interpreter::wasm;
|
||||
|
||||
// In current implementation export simply does nothing, because there is no more
|
||||
|
@ -16,13 +16,15 @@
|
||||
|
||||
use super::wit_prelude::*;
|
||||
use super::{IType, IValue, WValue};
|
||||
use crate::Result;
|
||||
use crate::FCEModuleConfig;
|
||||
|
||||
use fce_wit_interfaces::FCEWITInterfaces;
|
||||
use wasmer_wit::interpreter::Interpreter;
|
||||
use wasmer_runtime::{compile, ImportObject};
|
||||
use wasmer_core::Instance as WasmerInstance;
|
||||
use wasmer_core::import::Namespace;
|
||||
use wasmer_runtime::compile;
|
||||
use wasmer_runtime::ImportObject;
|
||||
use wasmer_wit::interpreter::Interpreter;
|
||||
use wit_parser::extract_wit;
|
||||
|
||||
use std::collections::HashMap;
|
||||
@ -47,7 +49,7 @@ pub(super) struct 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;
|
||||
|
||||
let result = self
|
||||
@ -86,7 +88,7 @@ impl FCEModule {
|
||||
wasm_bytes: &[u8],
|
||||
fce_module_config: FCEModuleConfig,
|
||||
modules: &HashMap<String, FCEModule>,
|
||||
) -> Result<Self, FCEError> {
|
||||
) -> Result<Self> {
|
||||
let wasmer_module = compile(&wasm_bytes)?;
|
||||
let wit = extract_wit(&wasmer_module)?;
|
||||
let fce_wit = FCEWITInterfaces::new(wit);
|
||||
@ -130,11 +132,7 @@ impl FCEModule {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn call(
|
||||
&mut self,
|
||||
function_name: &str,
|
||||
args: &[IValue],
|
||||
) -> Result<Vec<IValue>, FCEError> {
|
||||
pub(crate) fn call(&mut self, function_name: &str, args: &[IValue]) -> Result<Vec<IValue>> {
|
||||
match self.exports_funcs.get_mut(function_name) {
|
||||
Some(func) => Arc::make_mut(func).call(args),
|
||||
None => Err(FCEError::NoSuchFunction(format!(
|
||||
@ -157,7 +155,7 @@ impl FCEModule {
|
||||
}
|
||||
|
||||
// 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) {
|
||||
Some(func) => Ok(func.clone()),
|
||||
None => Err(FCEError::NoSuchFunction(format!(
|
||||
@ -170,7 +168,7 @@ impl FCEModule {
|
||||
fn instantiate_wit_exports(
|
||||
wit_instance: Arc<WITInstance>,
|
||||
wit: &FCEWITInterfaces<'_>,
|
||||
) -> Result<HashMap<String, Arc<Callable>>, FCEError> {
|
||||
) -> Result<HashMap<String, Arc<Callable>>> {
|
||||
use fce_wit_interfaces::WITAstType;
|
||||
|
||||
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
|
||||
fn adjust_wit_imports(
|
||||
wit: &FCEWITInterfaces<'_>,
|
||||
wit_instance: Arc<MaybeUninit<WITInstance>>,
|
||||
) -> Result<ImportObject, FCEError> {
|
||||
) -> Result<ImportObject> {
|
||||
use fce_wit_interfaces::WITAstType;
|
||||
use wasmer_core::typed_func::DynamicFunc;
|
||||
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();
|
||||
|
||||
|
@ -21,11 +21,12 @@ mod wit_instance;
|
||||
mod type_converters;
|
||||
mod fce_module;
|
||||
|
||||
pub(crate) use fce_module::FCEModule;
|
||||
|
||||
pub use wasmer_wit::types::InterfaceType as IType;
|
||||
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::Value as WValue;
|
||||
|
||||
|
@ -14,10 +14,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use super::wit_prelude::FCEError;
|
||||
use super::fce_module::FCEModule;
|
||||
use super::{IType, IValue, WValue};
|
||||
use super::fce_module::Callable;
|
||||
use crate::Result;
|
||||
|
||||
use wasmer_wit::interpreter::wasm;
|
||||
use wasmer_core::instance::DynFunc;
|
||||
@ -45,7 +45,7 @@ pub(super) struct WITFunction {
|
||||
|
||||
impl WITFunction {
|
||||
/// 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;
|
||||
|
||||
let signature = dyn_func.signature();
|
||||
@ -70,10 +70,7 @@ impl WITFunction {
|
||||
}
|
||||
|
||||
/// Creates function from a module import.
|
||||
pub(super) fn from_import(
|
||||
wit_module: &FCEModule,
|
||||
function_name: &str,
|
||||
) -> Result<Self, FCEError> {
|
||||
pub(super) fn from_import(wit_module: &FCEModule, function_name: &str) -> Result<Self> {
|
||||
let callable = wit_module.get_callable(function_name)?;
|
||||
|
||||
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};
|
||||
|
||||
match &self.inner {
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
use super::wit_prelude::*;
|
||||
use super::fce_module::FCEModule;
|
||||
use crate::Result;
|
||||
|
||||
use fce_wit_interfaces::FCEWITInterfaces;
|
||||
use fce_wit_interfaces::WITAstType;
|
||||
@ -37,7 +38,7 @@ impl WITInstance {
|
||||
wasmer_instance: &WasmerInstance,
|
||||
wit: &FCEWITInterfaces<'_>,
|
||||
modules: &HashMap<String, FCEModule>,
|
||||
) -> Result<Self, FCEError> {
|
||||
) -> Result<Self> {
|
||||
let mut exports = Self::extract_raw_exports(&wasmer_instance, wit)?;
|
||||
let imports = Self::extract_imports(modules, wit, exports.len())?;
|
||||
let memories = Self::extract_memories(&wasmer_instance);
|
||||
@ -51,7 +52,7 @@ impl WITInstance {
|
||||
fn extract_raw_exports(
|
||||
wasmer_instance: &WasmerInstance,
|
||||
wit: &FCEWITInterfaces<'_>,
|
||||
) -> Result<HashMap<usize, WITFunction>, FCEError> {
|
||||
) -> Result<HashMap<usize, WITFunction>> {
|
||||
use wasmer_core::DynFunc;
|
||||
|
||||
let module_exports = &wasmer_instance.exports;
|
||||
@ -76,7 +77,7 @@ impl WITInstance {
|
||||
modules: &HashMap<String, FCEModule>,
|
||||
wit: &FCEWITInterfaces<'_>,
|
||||
start_index: usize,
|
||||
) -> Result<HashMap<usize, WITFunction>, FCEError> {
|
||||
) -> Result<HashMap<usize, WITFunction>> {
|
||||
wit.imports()
|
||||
.filter(|import|
|
||||
// filter out imports that have implementations
|
||||
@ -89,7 +90,7 @@ impl WITInstance {
|
||||
}
|
||||
None => Err(FCEError::NoSuchModule(import.namespace.to_string())),
|
||||
})
|
||||
.collect::<Result<HashMap<_, _>, _>>()
|
||||
.collect::<Result<HashMap<_, _>>>()
|
||||
}
|
||||
|
||||
fn extract_memories(wasmer_instance: &WasmerInstance) -> Vec<WITMemory> {
|
||||
|
Binary file not shown.
Binary file not shown.
@ -106,7 +106,16 @@ impl FluenceFaaS {
|
||||
}
|
||||
|
||||
/// 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,
|
||||
wasm: &[u8],
|
||||
func_name: &str,
|
||||
@ -125,10 +134,10 @@ impl FluenceFaaS {
|
||||
}
|
||||
|
||||
/// 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,
|
||||
module_name: &str,
|
||||
func_name: &str,
|
||||
module_name: MN,
|
||||
func_name: FN,
|
||||
args: &[IValue],
|
||||
) -> Result<Vec<IValue>> {
|
||||
self.fce
|
||||
|
@ -14,20 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use wasmer_core::vm::Ctx;
|
||||
use crate::FaaSError;
|
||||
use super::config::ModuleConfig;
|
||||
|
||||
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_runtime::func;
|
||||
use wasmer_core::typed_func::WasmTypeList;
|
||||
use wasmer_runtime::func;
|
||||
use wasmer_runtime::Func;
|
||||
use wasmer_runtime::error::ResolveError;
|
||||
use wasmer_core::backend::SigRegistry;
|
||||
use wasmer_runtime::types::LocalOrImport;
|
||||
use wasmer_core::module::ExportIndex;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
@ -103,7 +102,7 @@ where
|
||||
}
|
||||
|
||||
/// 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::log_utf8_string;
|
||||
use wasmer_core::import::Namespace;
|
||||
|
Loading…
Reference in New Issue
Block a user