Files
marine/engine/src/engine.rs

127 lines
3.7 KiB
Rust
Raw Normal View History

2020-04-18 18:27:01 +03:00
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-06-06 02:31:59 +03:00
use super::module::FCEModule;
use super::*;
2020-04-28 05:00:20 +03:00
use std::collections::hash_map::Entry;
use std::collections::HashMap;
/// Represent a function type inside FCE.
#[derive(Debug, serde::Serialize)]
pub struct FCEFunction<'a> {
pub name: &'a str,
pub inputs: &'a Vec<IType>,
pub outputs: &'a Vec<IType>,
}
/// The base struct of the Fluence Compute Engine.
pub struct FCE {
// set of modules registered inside FCE
modules: HashMap<String, FCEModule>,
}
2020-05-08 20:38:29 +03:00
impl FCE {
2020-04-28 05:00:20 +03:00
pub fn new() -> Self {
Self {
modules: HashMap::new(),
2020-04-28 14:27:08 +03:00
}
}
2020-04-18 18:27:01 +03:00
/// Invoke a function of a module inside FCE by given function name with given arguments.
pub fn call<MN: AsRef<str>, FN: AsRef<str>>(
&mut self,
module_name: MN,
func_name: FN,
argument: &[IValue],
) -> Result<Vec<IValue>, FCEError> {
match self.modules.get_mut(module_name.as_ref()) {
// TODO: refactor errors
Some(module) => module.call(func_name.as_ref(), argument),
2020-06-10 14:56:15 +03:00
None => Err(FCEError::NoSuchModule),
2020-04-18 18:27:01 +03:00
}
}
/// Load a new module inside FCE.
pub fn load_module<S>(
2020-04-28 05:00:20 +03:00
&mut self,
2020-04-29 22:15:17 +03:00
module_name: S,
2020-04-28 05:00:20 +03:00
wasm_bytes: &[u8],
config: FCEModuleConfig,
2020-05-08 20:38:29 +03:00
) -> Result<(), FCEError>
2020-04-29 22:15:17 +03:00
where
S: Into<String>,
{
let _prepared_wasm_bytes = crate::misc::prepare_module(wasm_bytes, config.mem_pages_count)?;
2020-04-18 18:27:01 +03:00
2020-06-12 01:24:07 +03:00
let module = FCEModule::new(&wasm_bytes, config, &self.modules)?;
2020-04-18 18:27:01 +03:00
match self.modules.entry(module_name.into()) {
2020-04-29 20:42:28 +03:00
Entry::Vacant(entry) => {
2020-06-07 22:57:30 +03:00
entry.insert(module);
2020-04-29 20:42:28 +03:00
Ok(())
2020-04-29 22:15:17 +03:00
}
2020-05-08 20:38:29 +03:00
Entry::Occupied(_) => Err(FCEError::NonUniqueModuleName),
2020-04-29 20:42:28 +03:00
}
2020-04-28 05:00:20 +03:00
}
2020-04-18 18:27:01 +03:00
/// 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()) {
Some(_) => Ok(()),
None => Err(FCEError::NoSuchModule),
2020-05-09 00:39:42 +03:00
}
2020-04-18 18:27:01 +03:00
}
2020-06-05 23:12:02 +03:00
/// Return function signatures of all loaded info FCE modules with their names.
pub fn interface(&self) -> impl Iterator<Item = (&str, Vec<FCEFunction<'_>>)> {
self.modules.iter().map(|(module_name, module)| {
(
module_name.as_str(),
Self::get_module_function_signatures(module),
)
})
}
/// Return function signatures exported by module with given name.
pub fn module_interface<S: AsRef<str>>(
&self,
module_name: S,
) -> Result<Vec<FCEFunction<'_>>, FCEError> {
match self.modules.get(module_name.as_ref()) {
Some(module) => Ok(Self::get_module_function_signatures(module)),
2020-06-05 23:12:02 +03:00
None => Err(FCEError::NoSuchModule),
}
}
fn get_module_function_signatures(module: &FCEModule) -> Vec<FCEFunction<'_>> {
module
.get_exports_signatures()
.map(|(name, inputs, outputs)| FCEFunction {
name,
inputs,
outputs,
})
.collect::<Vec<_>>()
}
2020-04-18 18:27:01 +03:00
}
impl Default for FCE {
fn default() -> Self {
Self::new()
}
}