diff --git a/Cargo.lock b/Cargo.lock index 7f3a4cfd..8667e2a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -734,7 +734,7 @@ dependencies = [ [[package]] name = "fce-wit-interfaces" -version = "0.1.29" +version = "0.2.0" dependencies = [ "multimap", "wasmer-interface-types-fl", @@ -742,7 +742,7 @@ dependencies = [ [[package]] name = "fce-wit-parser" -version = "0.2.0" +version = "0.4.0" dependencies = [ "anyhow", "fce-wit-interfaces", diff --git a/crates/wit-generator/Cargo.toml b/crates/wit-generator/Cargo.toml index cb58d586..3561e9be 100644 --- a/crates/wit-generator/Cargo.toml +++ b/crates/wit-generator/Cargo.toml @@ -11,7 +11,7 @@ name = "fce_wit_generator" path = "src/lib.rs" [dependencies] -fce-wit-parser = { path = "../wit-parser", version = "0.2.0"} +fce-wit-parser = { path = "../wit-parser", version = "0.4.0"} fluence-sdk-wit = "=0.5.0" walrus = "0.18.0" diff --git a/crates/wit-interfaces/Cargo.toml b/crates/wit-interfaces/Cargo.toml index 7b1301ec..0294d101 100644 --- a/crates/wit-interfaces/Cargo.toml +++ b/crates/wit-interfaces/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "fce-wit-interfaces" description = "Fluence FCE interface type helper crate" -version = "0.1.29" +version = "0.2.0" authors = ["Fluence Labs"] license = "Apache-2.0" edition = "2018" diff --git a/crates/wit-parser/Cargo.toml b/crates/wit-parser/Cargo.toml index 2173236f..51204dc4 100644 --- a/crates/wit-parser/Cargo.toml +++ b/crates/wit-parser/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "fce-wit-parser" description = "Fluence FCE interface type helper crate" -version = "0.2.0" +version = "0.4.0" authors = ["Fluence Labs"] license = "Apache-2.0" edition = "2018" @@ -11,7 +11,7 @@ name = "fce_wit_parser" path = "src/lib.rs" [dependencies] -fce-wit-interfaces = { path = "../wit-interfaces", version = "0.1.29" } +fce-wit-interfaces = { path = "../wit-interfaces", version = "0.2.0" } anyhow = "1.0.31" walrus = "0.18.0" diff --git a/crates/wit-parser/src/extractor.rs b/crates/wit-parser/src/extractor.rs index 2dbc35e1..e76a7114 100644 --- a/crates/wit-parser/src/extractor.rs +++ b/crates/wit-parser/src/extractor.rs @@ -22,22 +22,39 @@ pub use wit::*; use crate::Result; use crate::WITParserError; + +use fce_wit_interfaces::FCEWITInterfaces; use std::path::Path; pub fn module_interface

(module_path: P) -> Result where P: AsRef, { - use fce_wit_interfaces::FCEWITInterfaces; + create_fce_it_with(module_path, |it| get_interface(&it)) +} +pub fn module_raw_interface

(module_path: P) -> Result +where + P: AsRef, +{ + create_fce_it_with(module_path, |it| get_raw_interface(&it)) +} + +fn create_fce_it_with( + module_path: P, + transformer: impl FnOnce(FCEWITInterfaces<'_>) -> Result, +) -> Result +where + P: AsRef, +{ let module = walrus::ModuleConfig::new() .parse_file(module_path) .map_err(WITParserError::CorruptedWasmFile)?; - let raw_custom_section = extract_custom_section(&module)?; let custom_section_bytes = raw_custom_section.as_ref(); let wit = extract_wit_from_bytes(custom_section_bytes)?; + let fce_interface = FCEWITInterfaces::new(wit); - get_interface(&fce_interface) + transformer(fce_interface) } diff --git a/crates/wit-parser/src/extractor/functions.rs b/crates/wit-parser/src/extractor/functions.rs index 76644d8c..ab4bd7af 100644 --- a/crates/wit-parser/src/extractor/functions.rs +++ b/crates/wit-parser/src/extractor/functions.rs @@ -27,7 +27,7 @@ use serde::Deserialize; use std::collections::HashMap; use std::rc::Rc; -pub type RecordTypes = HashMap>; +pub type FCERecordTypes = HashMap>; #[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)] pub struct FCEFunctionSignature { @@ -38,22 +38,27 @@ pub struct FCEFunctionSignature { #[derive(PartialEq, Eq, Debug, Clone, Serialize)] pub struct FCEModuleInterface { - pub record_types: RecordTypes, + pub record_types: FCERecordTypes, pub function_signatures: Vec, } -pub fn get_interface(wit: &FCEWITInterfaces<'_>) -> Result { - let function_signatures = get_exports(wit)?; - let record_types = extract_record_types(wit); +pub fn get_interface(fce_it_interface: &FCEWITInterfaces<'_>) -> Result { + let fce_interface = get_raw_interface(fce_it_interface)?; + let service_interface = into_service_interface(fce_interface); + + Ok(service_interface) +} + +pub fn get_raw_interface(fce_it_interface: &FCEWITInterfaces<'_>) -> Result { + let function_signatures = get_exports(fce_it_interface)?; + let record_types = extract_record_types(fce_it_interface); let fce_interface = FCEModuleInterface { record_types, function_signatures, }; - let service_interface = into_service_interface(fce_interface); - - Ok(service_interface) + Ok(fce_interface) } fn get_exports(wit: &FCEWITInterfaces<'_>) -> Result> { @@ -97,7 +102,7 @@ fn get_exports(wit: &FCEWITInterfaces<'_>) -> Result> .collect::>>() } -fn extract_record_types(wit: &FCEWITInterfaces<'_>) -> RecordTypes { +fn extract_record_types(wit: &FCEWITInterfaces<'_>) -> FCERecordTypes { use fce_wit_interfaces::WITAstType; let (record_types_by_id, _) = wit.types().fold( @@ -158,7 +163,7 @@ pub(crate) fn into_service_interface(fce_interface: FCEModuleInterface) -> Servi fn serialize_function_signature( signature: FCEFunctionSignature, - record_types: &RecordTypes, + record_types: &FCERecordTypes, ) -> FunctionSignature { let arguments = signature .arguments @@ -182,7 +187,7 @@ fn serialize_function_signature( fn serialize_record_type<'a, 'b>( id: u64, record: Rc, - record_types: &RecordTypes, + record_types: &FCERecordTypes, ) -> RecordType { let fields = record .fields @@ -197,7 +202,7 @@ fn serialize_record_type<'a, 'b>( } } -fn itype_text_view(arg_ty: &IType, record_types: &RecordTypes) -> String { +fn itype_text_view(arg_ty: &IType, record_types: &FCERecordTypes) -> String { match arg_ty { IType::Record(record_type_id) => { // unwrap is safe because FaaSInterface here is well-formed diff --git a/crates/wit-parser/src/lib.rs b/crates/wit-parser/src/lib.rs index 083636ff..c9eccb0e 100644 --- a/crates/wit-parser/src/lib.rs +++ b/crates/wit-parser/src/lib.rs @@ -34,11 +34,30 @@ pub use errors::WITParserError; pub use deleter::delete_wit_section; pub use deleter::delete_wit_section_from_file; + pub use embedder::embed_wit; pub use embedder::embed_text_wit; + pub use extractor::extract_wit_from_module; pub use extractor::extract_version_from_module; pub use extractor::extract_text_wit; pub use extractor::module_interface; +pub use extractor::module_raw_interface; + +pub mod interface { + pub use crate::extractor::ServiceInterface; + pub use crate::extractor::RecordType; + pub use crate::extractor::FunctionSignature; + + pub use crate::extractor::FCEModuleInterface; + pub use crate::extractor::FCERecordTypes; + pub use crate::extractor::FCEFunctionSignature; + pub mod it { + pub use wasmer_wit::IType; + pub use wasmer_wit::ast::FunctionArg as IFunctionArg; + pub use wasmer_wit::IRecordType; + pub use wasmer_wit::IRecordFieldType; + } +} pub(crate) type Result = std::result::Result; diff --git a/engine/Cargo.toml b/engine/Cargo.toml index 85c0923f..c25b2428 100644 --- a/engine/Cargo.toml +++ b/engine/Cargo.toml @@ -12,8 +12,8 @@ path = "src/lib.rs" [dependencies] fce-module-info-parser = { path = "../crates/module-info-parser", version = "0.1.0" } -fce-wit-interfaces = { path = "../crates/wit-interfaces", version = "0.1.29" } -fce-wit-parser = { path = "../crates/wit-parser", version = "0.2.0" } +fce-wit-interfaces = { path = "../crates/wit-interfaces", version = "0.2.0" } +fce-wit-parser = { path = "../crates/wit-parser", version = "0.4.0" } fce-wit-generator = { path = "../crates/wit-generator", version = "0.2.0" } fce-utils = { path = "../crates/utils", version = "0.1.29" } diff --git a/tools/cli/Cargo.toml b/tools/cli/Cargo.toml index 27f2cf48..1ad8b825 100644 --- a/tools/cli/Cargo.toml +++ b/tools/cli/Cargo.toml @@ -13,7 +13,7 @@ path = "src/main.rs" [dependencies] fce-wit-generator = { path = "../../crates/wit-generator", version = "0.2.0" } -fce-wit-parser = { path = "../../crates/wit-parser", version = "0.2.0" } +fce-wit-parser = { path = "../../crates/wit-parser", version = "0.4.0" } fce-module-info-parser = { path = "../../crates/module-info-parser", version = "0.1.0" } semver = "0.11.0" diff --git a/tools/repl/src/repl.rs b/tools/repl/src/repl.rs index 4740b46f..6638c120 100644 --- a/tools/repl/src/repl.rs +++ b/tools/repl/src/repl.rs @@ -145,18 +145,17 @@ impl REPL { let start = Instant::now(); // TODO: add support of call parameters - let result = match self.app_service.call_module( - module_name, - func_name, - module_arg, - <_>::default(), - ) { - Ok(result) => { - let elapsed_time = start.elapsed(); - format!("result: {:?}\n elapsed time: {:?}", result, elapsed_time) - } - Err(e) => format!("call failed with: {}", e), - }; + let result = + match self + .app_service + .call_module(module_name, func_name, module_arg, <_>::default()) + { + Ok(result) => { + let elapsed_time = start.elapsed(); + format!("result: {:?}\n elapsed time: {:?}", result, elapsed_time) + } + Err(e) => format!("call failed with: {}", e), + }; println!("{}", result); }