Files
marine/fce/src/vm/fce.rs

93 lines
2.5 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.
*/
use super::instance::FCEModule;
use super::*;
2020-04-28 05:00:20 +03:00
use std::sync::Arc;
2020-04-28 05:00:20 +03:00
use std::collections::hash_map::Entry;
use std::collections::HashMap;
2020-05-08 20:38:29 +03:00
pub struct FCE {
// set of modules registered inside FCE
modules: HashMap<String, Arc<FCEModule>>,
2020-04-18 18:27:01 +03:00
}
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
}
2020-05-08 20:38:29 +03:00
impl Default for FCE {
2020-04-29 22:15:17 +03:00
fn default() -> Self {
Self::new()
}
}
2020-05-08 20:38:29 +03:00
impl FCEService for FCE {
fn call(
&mut self,
module_name: &str,
func_name: &str,
argument: &[IValue],
) -> Result<Vec<IValue>, FCEError> {
2020-04-29 22:15:17 +03:00
match self.modules.get_mut(module_name) {
// TODO: refactor errors
Some(mut module) => unsafe {
Ok(Arc::get_mut_unchecked(&mut module).call(func_name, argument)?)
},
2020-05-08 20:38:29 +03:00
None => Err(FCEError::NoSuchModule),
2020-04-18 18:27:01 +03:00
}
}
2020-04-29 22:15:17 +03:00
fn register_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 =
super::prepare::prepare_module(wasm_bytes, config.mem_pages_count)?;
2020-04-18 18:27:01 +03:00
let module = FCEModule::new(&wasm_bytes, config.import_object, &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) => {
entry.insert(Arc::new(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
2020-05-08 20:38:29 +03:00
fn unregister_module(&mut self, module_name: &str) -> Result<(), FCEError> {
2020-05-09 00:39:42 +03:00
match self.modules.entry(module_name.to_string()) {
Entry::Vacant(_) => Err(FCEError::NoSuchModule),
Entry::Occupied(module) => {
module.remove_entry();
Ok(())
}
}
2020-04-18 18:27:01 +03:00
}
}