introduce node public interface

This commit is contained in:
vms 2020-06-10 16:55:18 +03:00
parent 49b4504ed7
commit bbe4612a40
5 changed files with 65 additions and 14 deletions

View File

@ -18,6 +18,8 @@ mod node;
mod errors; mod errors;
mod config; mod config;
mod imports; mod imports;
mod node_public_interface;
pub use node::IpfsNode; pub use node::IpfsNode;
pub use node::NodeModule; pub use node_public_interface::NodePublicInterface;
pub use node_public_interface::NodeModulePublicInterface;

View File

@ -18,6 +18,7 @@ mod node;
mod errors; mod errors;
mod config; mod config;
mod imports; mod imports;
mod node_public_interface;
use node::IpfsNode; use node::IpfsNode;
@ -33,13 +34,14 @@ const IPFS_RPC: &str = "/Users/mike/dev/work/fluence/wasm/fce/bin/wasm_ipfs_rpc_
fn main() { fn main() {
let ipfs_rpc = std::fs::read(IPFS_RPC).unwrap(); 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_DIR),
PathBuf::from(IPFS_MODULES_CONFIG_PATH), PathBuf::from(IPFS_MODULES_CONFIG_PATH),
) )
.unwrap(); .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(); let result = ipfs_node.rpc_call(&ipfs_rpc, &[]).unwrap();

View File

@ -15,18 +15,19 @@
*/ */
use super::errors::NodeError; 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_core::import::ImportObject;
use wasmer_runtime::func; use wasmer_runtime::func;
use fce::FCE; use fce::FCE;
use fce::WasmProcess; use fce::WasmProcess;
use fce::NodeFunction;
use fce::IValue; use fce::IValue;
use fce::FCEModuleConfig; use fce::FCEModuleConfig;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use crate::config::ModuleConfig;
pub struct IpfsNode { pub struct IpfsNode {
process: FCE, process: FCE,
@ -35,12 +36,6 @@ pub struct IpfsNode {
rpc_module_config: FCEModuleConfig, rpc_module_config: FCEModuleConfig,
} }
#[derive(Debug)]
pub struct NodeModule<'a> {
pub name: &'a str,
pub functions: Vec<NodeFunction<'a>>,
}
impl IpfsNode { impl IpfsNode {
pub fn new<P: Into<PathBuf>>(core_modules_dir: P, config_file_path: P) -> Result<Self, NodeError> { pub fn new<P: Into<PathBuf>>(core_modules_dir: P, config_file_path: P) -> Result<Self, NodeError> {
let mut wasm_process = FCE::new(); let mut wasm_process = FCE::new();
@ -92,18 +87,18 @@ impl IpfsNode {
Ok(call_result) Ok(call_result)
} }
pub fn get_interface(&self) -> Vec<NodeModule> { pub fn get_interface(&self) -> NodePublicInterface {
let mut modules = Vec::with_capacity(self.module_names.len()); let mut modules = Vec::with_capacity(self.module_names.len());
for module_name in self.module_names.iter() { for module_name in self.module_names.iter() {
let functions = self.process.get_interface(module_name).unwrap(); let functions = self.process.get_interface(module_name).unwrap();
modules.push(NodeModule { modules.push(NodeModulePublicInterface {
name: module_name, name: module_name,
functions, functions,
}) })
} }
modules NodePublicInterface { modules }
} }
fn make_wasm_process_config(config: Option<ModuleConfig>) -> Result<FCEModuleConfig, NodeError> { fn make_wasm_process_config(config: Option<ModuleConfig>) -> Result<FCEModuleConfig, NodeError> {

View File

@ -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<NodeModulePublicInterface<'a>>,
}
#[derive(Debug)]
pub struct NodeModulePublicInterface<'a> {
pub name: &'a str,
pub functions: Vec<fce::NodeFunction<'a>>,
}
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(())
}
}

View File

@ -43,6 +43,7 @@ pub(super) struct WITFunction {
inner: WITFunctionInner, inner: WITFunctionInner,
} }
/*
impl Drop for WITFunction { impl Drop for WITFunction {
fn drop(&mut self) { fn drop(&mut self) {
match &self.inner { match &self.inner {
@ -58,6 +59,7 @@ impl Drop for WITFunction {
} }
} }
} }
*/
impl WITFunction { impl WITFunction {
/// Creates functions from a "usual" (not WIT) module export. /// Creates functions from a "usual" (not WIT) module export.