From bbe4612a4028c6004f85df8dfde0f852c0247685 Mon Sep 17 00:00:00 2001 From: vms Date: Wed, 10 Jun 2020 16:55:18 +0300 Subject: [PATCH] introduce node public interface --- examples/ipfs_node/src/lib.rs | 4 +- examples/ipfs_node/src/main.rs | 6 ++- examples/ipfs_node/src/node.rs | 17 +++---- .../ipfs_node/src/node_public_interface.rs | 50 +++++++++++++++++++ fce/src/vm/module/wit_function.rs | 2 + 5 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 examples/ipfs_node/src/node_public_interface.rs diff --git a/examples/ipfs_node/src/lib.rs b/examples/ipfs_node/src/lib.rs index f83a8c0c..353ea2c7 100644 --- a/examples/ipfs_node/src/lib.rs +++ b/examples/ipfs_node/src/lib.rs @@ -18,6 +18,8 @@ mod node; mod errors; mod config; mod imports; +mod node_public_interface; pub use node::IpfsNode; -pub use node::NodeModule; +pub use node_public_interface::NodePublicInterface; +pub use node_public_interface::NodeModulePublicInterface; diff --git a/examples/ipfs_node/src/main.rs b/examples/ipfs_node/src/main.rs index 4c2f856b..1ef0854b 100644 --- a/examples/ipfs_node/src/main.rs +++ b/examples/ipfs_node/src/main.rs @@ -18,6 +18,7 @@ mod node; mod errors; mod config; mod imports; +mod node_public_interface; use node::IpfsNode; @@ -33,13 +34,14 @@ const IPFS_RPC: &str = "/Users/mike/dev/work/fluence/wasm/fce/bin/wasm_ipfs_rpc_ fn main() { let ipfs_rpc = std::fs::read(IPFS_RPC).unwrap(); - let mut ipfs_node = IpfsNode::new( + let mut ipfs_node = crate::IpfsNode::new( PathBuf::from(IPFS_MODULES_DIR), PathBuf::from(IPFS_MODULES_CONFIG_PATH), ) .unwrap(); - println!("ipfs node interface is {:?}", ipfs_node.get_interface()); + + println!("ipfs node interface is {}", ipfs_node.get_interface()); let result = ipfs_node.rpc_call(&ipfs_rpc, &[]).unwrap(); diff --git a/examples/ipfs_node/src/node.rs b/examples/ipfs_node/src/node.rs index ad0b117d..e880bcd6 100644 --- a/examples/ipfs_node/src/node.rs +++ b/examples/ipfs_node/src/node.rs @@ -15,18 +15,19 @@ */ use super::errors::NodeError; +use crate::config::ModuleConfig; +use crate::node_public_interface::NodePublicInterface; +use crate::node_public_interface::NodeModulePublicInterface; use wasmer_core::import::ImportObject; use wasmer_runtime::func; use fce::FCE; use fce::WasmProcess; -use fce::NodeFunction; use fce::IValue; use fce::FCEModuleConfig; use std::fs; use std::path::PathBuf; -use crate::config::ModuleConfig; pub struct IpfsNode { process: FCE, @@ -35,12 +36,6 @@ pub struct IpfsNode { rpc_module_config: FCEModuleConfig, } -#[derive(Debug)] -pub struct NodeModule<'a> { - pub name: &'a str, - pub functions: Vec>, -} - impl IpfsNode { pub fn new>(core_modules_dir: P, config_file_path: P) -> Result { let mut wasm_process = FCE::new(); @@ -92,18 +87,18 @@ impl IpfsNode { Ok(call_result) } - pub fn get_interface(&self) -> Vec { + pub fn get_interface(&self) -> NodePublicInterface { let mut modules = Vec::with_capacity(self.module_names.len()); for module_name in self.module_names.iter() { let functions = self.process.get_interface(module_name).unwrap(); - modules.push(NodeModule { + modules.push(NodeModulePublicInterface { name: module_name, functions, }) } - modules + NodePublicInterface { modules } } fn make_wasm_process_config(config: Option) -> Result { diff --git a/examples/ipfs_node/src/node_public_interface.rs b/examples/ipfs_node/src/node_public_interface.rs new file mode 100644 index 00000000..283c5c7b --- /dev/null +++ b/examples/ipfs_node/src/node_public_interface.rs @@ -0,0 +1,50 @@ +/* + * 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 std::fmt; + +#[derive(Debug)] +pub struct NodePublicInterface<'a> { + pub modules: Vec>, +} + +#[derive(Debug)] +pub struct NodeModulePublicInterface<'a> { + pub name: &'a str, + pub functions: Vec>, +} + +impl<'a> fmt::Display for NodePublicInterface<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for module in self.modules.iter() { + write!(f, "{}", module)?; + } + + Ok(()) + } +} + +impl<'a> fmt::Display for NodeModulePublicInterface<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}\n", self.name)?; + + for function in self.functions.iter() { + write!(f, " pub fn {}({:?}) -> {:?}\n", function.name, function.inputs, function.outputs)?; + } + + Ok(()) + } +} diff --git a/fce/src/vm/module/wit_function.rs b/fce/src/vm/module/wit_function.rs index d803ef05..4c229f9b 100644 --- a/fce/src/vm/module/wit_function.rs +++ b/fce/src/vm/module/wit_function.rs @@ -43,6 +43,7 @@ pub(super) struct WITFunction { inner: WITFunctionInner, } +/* impl Drop for WITFunction { fn drop(&mut self) { match &self.inner { @@ -58,6 +59,7 @@ impl Drop for WITFunction { } } } + */ impl WITFunction { /// Creates functions from a "usual" (not WIT) module export.