mirror of
https://github.com/fluencelabs/marine.git
synced 2025-03-15 22:10:49 +00:00
make api more robust
This commit is contained in:
parent
44ac703e65
commit
ca704ede7a
@ -74,7 +74,7 @@ fn main() -> Result<(), ExitFailure> {
|
|||||||
println!("{}", result_msg);
|
println!("{}", result_msg);
|
||||||
}
|
}
|
||||||
"execute" => {
|
"execute" => {
|
||||||
let module_name = cmd[1].to_string();
|
let module_name = cmd[1];
|
||||||
let arg = cmd[2..].join(" ");
|
let arg = cmd[2..].join(" ");
|
||||||
let result = match frank.invoke(module_name, arg.as_bytes()) {
|
let result = match frank.invoke(module_name, arg.as_bytes()) {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
|
@ -40,7 +40,7 @@ impl Frank {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds ABI of a module with provided module name to the abi_import_object.
|
/// Extracts ABI of a module into Namespace.
|
||||||
fn create_import_object(module: &FrankModule, config: &Config) -> Namespace {
|
fn create_import_object(module: &FrankModule, config: &Config) -> Namespace {
|
||||||
let mut namespace = Namespace::new();
|
let mut namespace = Namespace::new();
|
||||||
|
|
||||||
@ -105,20 +105,29 @@ impl Frank {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Frank {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FrankService for Frank {
|
impl FrankService for Frank {
|
||||||
fn invoke(&mut self, module_name: String, argument: &[u8]) -> Result<FrankResult, FrankError> {
|
fn invoke(&mut self, module_name: &str, argument: &[u8]) -> Result<FrankResult, FrankError> {
|
||||||
match self.modules.entry(module_name) {
|
match self.modules.get_mut(module_name) {
|
||||||
Entry::Vacant(_) => Err(FrankError::NoSuchModule),
|
Some(module) => module.invoke(argument),
|
||||||
Entry::Occupied(mut module) => module.get_mut().invoke(argument),
|
None => Err(FrankError::NoSuchModule)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn register_module(
|
fn register_module<S>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module_name: String,
|
module_name: S,
|
||||||
wasm_bytes: &[u8],
|
wasm_bytes: &[u8],
|
||||||
config: Config,
|
config: Config,
|
||||||
) -> Result<(), FrankError> {
|
) -> Result<(), FrankError>
|
||||||
|
where
|
||||||
|
S: Into<String>,
|
||||||
|
{
|
||||||
let prepared_wasm_bytes =
|
let prepared_wasm_bytes =
|
||||||
crate::vm::prepare::prepare_module(wasm_bytes, config.mem_pages_count)?;
|
crate::vm::prepare::prepare_module(wasm_bytes, config.mem_pages_count)?;
|
||||||
|
|
||||||
@ -130,14 +139,16 @@ impl FrankService for Frank {
|
|||||||
|
|
||||||
// registers ABI of newly registered module in abi_import_object
|
// registers ABI of newly registered module in abi_import_object
|
||||||
let namespace = Frank::create_import_object(&module, &config);
|
let namespace = Frank::create_import_object(&module, &config);
|
||||||
self.abi_import_object.register(module_name.clone(), namespace);
|
let module_name: String = module_name.into();
|
||||||
|
self.abi_import_object
|
||||||
|
.register(module_name.clone(), namespace);
|
||||||
|
|
||||||
match self.modules.entry(module_name) {
|
match self.modules.entry(module_name) {
|
||||||
Entry::Vacant(entry) => {
|
Entry::Vacant(entry) => {
|
||||||
entry.insert(module);
|
entry.insert(module);
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
}
|
||||||
Entry::Occupied(_) => Err(FrankError::NonUniqueModuleName)
|
Entry::Occupied(_) => Err(FrankError::NonUniqueModuleName),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,15 +23,17 @@ use sha2::digest::generic_array::GenericArray;
|
|||||||
/// Describes a service behaviour in the Fluence network.
|
/// Describes a service behaviour in the Fluence network.
|
||||||
pub trait FrankService {
|
pub trait FrankService {
|
||||||
/// Invokes a module supplying byte array and expecting byte array with some outcome back.
|
/// Invokes a module supplying byte array and expecting byte array with some outcome back.
|
||||||
fn invoke(&mut self, module_name: String, argument: &[u8]) -> Result<FrankResult, FrankError>;
|
fn invoke(&mut self, module_name: &str, argument: &[u8]) -> Result<FrankResult, FrankError>;
|
||||||
|
|
||||||
/// Registers new module in the Frank Service.
|
/// Registers new module in the Frank Service.
|
||||||
fn register_module(
|
fn register_module<S>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module_name: String,
|
module_name: S,
|
||||||
wasm_bytes: &[u8],
|
wasm_bytes: &[u8],
|
||||||
config: Config,
|
config: Config,
|
||||||
) -> Result<(), FrankError>;
|
) -> Result<(), FrankError>
|
||||||
|
where
|
||||||
|
S: Into<String>;
|
||||||
|
|
||||||
/// Unregisters previously registered module.
|
/// Unregisters previously registered module.
|
||||||
fn unregister_module(&mut self, module_name: &str) -> Result<(), FrankError>;
|
fn unregister_module(&mut self, module_name: &str) -> Result<(), FrankError>;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user