diff --git a/Cargo.toml b/Cargo.toml index 588fbc3..8e135e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ fluence-sdk-main = { path = "crates/main", version = "=0.2.0" } # Print some internal logs by log_utf8_string debug = ["fluence-sdk-main/debug"] -# Print some internal logs by log_utf8_string +# Enable logger (this will cause log_utf8_string to appear in imports) logger = ["fluence-sdk-main/logger"] [workspace] diff --git a/crates/main/Cargo.toml b/crates/main/Cargo.toml index 7c814dc..1d20f11 100644 --- a/crates/main/Cargo.toml +++ b/crates/main/Cargo.toml @@ -28,5 +28,5 @@ lazy_static = "1.4.0" # used in doc test # Print some internal logs by log_utf8_string debug = [] -# Enable logger, this will cause log_utf8_string to appear in imports +# Enable logger (this will cause log_utf8_string to appear in imports) logger = [] diff --git a/crates/wit/src/fce_ast_types.rs b/crates/wit/src/fce_ast_types.rs index a4524f7..67acfe9 100644 --- a/crates/wit/src/fce_ast_types.rs +++ b/crates/wit/src/fce_ast_types.rs @@ -19,8 +19,11 @@ use crate::parsed_type::ParsedType; use serde::Serialize; use serde::Deserialize; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct AstFunctionSignature { + // Option is needed only for skipping serialization/deserialization of syn::ItemFn + #[serde(skip)] + pub visibility: Option, pub name: String, pub input_types: Vec, // fce supports only one return value now, @@ -45,7 +48,7 @@ pub struct AstRecordItem { pub original: Option, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct AstExternFnItem { pub link_name: Option, // only imports are possible here diff --git a/crates/wit/src/parse_macro_input/item_fn.rs b/crates/wit/src/parse_macro_input/item_fn.rs index 002e913..3ad4cb3 100644 --- a/crates/wit/src/parse_macro_input/item_fn.rs +++ b/crates/wit/src/parse_macro_input/item_fn.rs @@ -22,24 +22,24 @@ use syn::Result; impl ParseMacroInput for syn::ItemFn { fn parse_macro_input(self) -> Result { - parse_function(self.sig.clone(), self.vis.clone()).map(|f| { + try_to_ast_signature(self.sig.clone(), self.vis.clone()).map(|signature| { FCEAst::Function(AstFunctionItem { - signature: f, + signature, original: Some(self), }) }) } } -pub(super) fn parse_function( - function_sig: syn::Signature, - function_vis: syn::Visibility, +pub(super) fn try_to_ast_signature( + signature: syn::Signature, + visibility: syn::Visibility, ) -> Result { use crate::parsed_type::ParsedType; - check_func(&function_sig, function_vis)?; + check_function(&signature)?; - let syn::Signature { inputs, output, .. } = function_sig; + let syn::Signature { inputs, output, .. } = signature; let input_types = inputs .iter() @@ -49,7 +49,8 @@ pub(super) fn parse_function( let output_type = ParsedType::from_return_type(&output)?; let ast_function_item = fce_ast_types::AstFunctionSignature { - name: function_sig.ident.to_string(), + visibility: Some(visibility), + name: signature.ident.to_string(), input_types, output_type, }; @@ -58,7 +59,7 @@ pub(super) fn parse_function( } /// Check whether the #[fce] macro could be applied to a function. -fn check_func(function_sig: &syn::Signature, function_vis: syn::Visibility) -> Result<()> { +fn check_function(signature: &syn::Signature) -> Result<()> { use syn::Error; use syn::spanned::Spanned; @@ -69,7 +70,7 @@ fn check_func(function_sig: &syn::Signature, function_vis: syn::Visibility) -> R variadic, generics, .. - } = function_sig; + } = signature; if let Some(constness) = constness { return Err(Error::new( @@ -91,7 +92,7 @@ fn check_func(function_sig: &syn::Signature, function_vis: syn::Visibility) -> R } if !generics.params.is_empty() || generics.where_clause.is_some() { return Err(Error::new( - function_sig.span(), + signature.span(), "FCE export function shouldn't use template parameters", )); } @@ -103,12 +104,5 @@ fn check_func(function_sig: &syn::Signature, function_vis: syn::Visibility) -> R } // TODO: check for a lifetime - - match function_vis { - syn::Visibility::Public(_) => Ok(()), - _ => Err(Error::new( - variadic.span(), - "FCE export function should be public", - )), - } + Ok(()) } diff --git a/crates/wit/src/parse_macro_input/item_foreign_mod.rs b/crates/wit/src/parse_macro_input/item_foreign_mod.rs index e45a499..c07b4c5 100644 --- a/crates/wit/src/parse_macro_input/item_foreign_mod.rs +++ b/crates/wit/src/parse_macro_input/item_foreign_mod.rs @@ -117,7 +117,7 @@ fn parse_raw_foreign_item(raw_item: syn::ForeignItem) -> Result None, }; - let function = super::item_fn::parse_function(function_item.sig, function_item.vis)?; + let function = super::item_fn::try_to_ast_signature(function_item.sig, function_item.vis)?; let ast_extern_fn_item = fce_ast_types::AstExternFnItem { link_name, signature: function, diff --git a/crates/wit/src/token_stream_generator/foreign_mod_generator.rs b/crates/wit/src/token_stream_generator/foreign_mod_generator.rs index 143d723..1eb794d 100644 --- a/crates/wit/src/token_stream_generator/foreign_mod_generator.rs +++ b/crates/wit/src/token_stream_generator/foreign_mod_generator.rs @@ -79,7 +79,7 @@ fn generate_extern_section_items(extern_item: &fce_ast_types::AstExternModItem) let func = quote! { #[link_name = #link_name] - pub fn #import_name(#(#raw_arg_names: #raw_arg_types),*) #fn_return_type; + fn #import_name(#(#raw_arg_names: #raw_arg_types),*) #fn_return_type; }; token_stream.extend(func); @@ -98,7 +98,7 @@ fn generate_wrapper_functions(extern_item: &fce_ast_types::AstExternModItem) -> for import in &extern_item.imports { let signature = &import.signature; - let visibility = new_ident!("pub"); + let visibility = &signature.visibility; let func_name = new_ident!(&signature.name); let return_type = signature.output_type.generate_wrapper_return_type();