Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function Attributes #4394

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
26c1dab
Update README.md
rouzwelt Dec 27, 2024
55a74d6
init
rouzwelt Jan 6, 2025
fac43b5
rm
rouzwelt Jan 6, 2025
de4e449
fix test
rouzwelt Jan 6, 2025
11c2571
fix schema hash
rouzwelt Jan 6, 2025
c43561e
update shared schema version
rouzwelt Jan 6, 2025
003312f
Update schema_hash_approval.rs
rouzwelt Jan 6, 2025
e111551
update
rouzwelt Jan 6, 2025
59faf69
docs
rouzwelt Jan 6, 2025
c115222
docs
rouzwelt Jan 6, 2025
13acbe4
docs
rouzwelt Jan 6, 2025
73d39b6
fix debug impl
rouzwelt Jan 6, 2025
6734413
Update run.sh
rouzwelt Jan 6, 2025
e0b04f9
update
rouzwelt Jan 7, 2025
6e7e1d2
fix ts doc generator fn
rouzwelt Jan 7, 2025
f24b605
update
rouzwelt Jan 7, 2025
f3a6ba0
fold fn attrs inside original props
rouzwelt Jan 8, 2025
c25c5dd
Update parser.rs
rouzwelt Jan 8, 2025
e46deda
update test
rouzwelt Jan 8, 2025
9507ed8
update test
rouzwelt Jan 8, 2025
d861076
Update parser.rs
rouzwelt Jan 8, 2025
935286e
Update function-attributes.md
rouzwelt Jan 8, 2025
19bfb47
Update ast.rs
rouzwelt Jan 8, 2025
5707447
fix docs
rouzwelt Jan 8, 2025
375570d
impl requested changes
rouzwelt Jan 9, 2025
bd11aab
fix
rouzwelt Jan 9, 2025
9d167f0
fmt
rouzwelt Jan 9, 2025
0a598c4
typo
rouzwelt Jan 9, 2025
2ea857e
fmt
rouzwelt Jan 9, 2025
227a60b
fix test
rouzwelt Jan 9, 2025
185f21b
typo
rouzwelt Jan 9, 2025
e7f2618
Add ability to specific attributes that error when unused
daxpedda Jan 9, 2025
0992426
Respect MSRV
daxpedda Jan 9, 2025
2e8e6c1
apply minor requested changes
rouzwelt Jan 9, 2025
76baf36
update
rouzwelt Jan 9, 2025
14dc57a
typo
rouzwelt Jan 9, 2025
225acf5
minor fix
rouzwelt Jan 9, 2025
44f8a55
add test for no fn attr on 'self' argument
rouzwelt Jan 9, 2025
1aec830
use fold()
rouzwelt Jan 9, 2025
0cbe5a8
better fold()
rouzwelt Jan 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ pub struct Function {
/// Whether the function has a js_name attribute
pub renamed_via_js_name: bool,
/// The arguments to the function
pub arguments: Vec<syn::PatType>,
/// The return type of the function, if provided
pub ret: Option<syn::Type>,
pub arguments: Vec<FunctionArgumentData>,
/// The data of return type of the function
pub ret: FunctionReturnData,
rouzwelt marked this conversation as resolved.
Show resolved Hide resolved
/// Any custom attributes being applied to the function
pub rust_attrs: Vec<syn::Attribute>,
/// The visibility of this function in Rust
Expand All @@ -394,6 +394,32 @@ pub struct Function {
pub variadic: bool,
}

/// Information about a function's return
#[cfg_attr(feature = "extra-traits", derive(Debug))]
#[derive(Clone)]
pub struct FunctionReturnData {
/// Specifies the type of the function's return
pub r#type: Option<syn::Type>,
/// Specifies the return type override provided by attributes
pub ty_override: Option<String>,
rouzwelt marked this conversation as resolved.
Show resolved Hide resolved
/// Specifies the return description
pub desc: Option<String>,
}

/// Information about a function's argument
#[cfg_attr(feature = "extra-traits", derive(Debug))]
#[derive(Clone)]
pub struct FunctionArgumentData {
/// Specifies the argument name
pub js_name: Option<String>,
/// Specifies the type of the function's argument
pub pat_type: syn::PatType,
rouzwelt marked this conversation as resolved.
Show resolved Hide resolved
/// Specifies the argument type override provided by attributes
pub ty_override: Option<String>,
rouzwelt marked this conversation as resolved.
Show resolved Hide resolved
/// Specifies the argument description
pub desc: Option<String>,
}

/// Information about a Struct being exported
#[cfg_attr(feature = "extra-traits", derive(Debug))]
#[derive(Clone)]
Expand Down
16 changes: 8 additions & 8 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ impl TryToTokens for ast::Export {

let mut argtys = Vec::new();
for (i, arg) in self.function.arguments.iter().enumerate() {
argtys.push(&*arg.ty);
argtys.push(&*arg.pat_type.ty);
let i = i + offset;
let ident = Ident::new(&format!("arg{}", i), Span::call_site());
fn unwrap_nested_types(ty: &syn::Type) -> &syn::Type {
Expand All @@ -647,7 +647,7 @@ impl TryToTokens for ast::Export {
_ => ty,
}
}
let ty = unwrap_nested_types(&arg.ty);
let ty = unwrap_nested_types(&arg.pat_type.ty);

match &ty {
syn::Type::Reference(syn::TypeReference {
Expand Down Expand Up @@ -720,7 +720,7 @@ impl TryToTokens for ast::Export {
elems: Default::default(),
paren_token: Default::default(),
});
let syn_ret = self.function.ret.as_ref().unwrap_or(&syn_unit);
let syn_ret = self.function.ret.r#type.as_ref().unwrap_or(&syn_unit);
if let syn::Type::Reference(_) = syn_ret {
bail_span!(syn_ret, "cannot return a borrowed ref with #[wasm_bindgen]",)
}
Expand Down Expand Up @@ -1323,7 +1323,7 @@ impl TryToTokens for ast::ImportFunction {
ast::ImportFunctionKind::Normal => {}
}
let vis = &self.function.rust_vis;
let ret = match &self.function.ret {
let ret = match &self.function.ret.r#type {
Some(ty) => quote! { -> #ty },
None => quote!(),
};
Expand All @@ -1337,8 +1337,8 @@ impl TryToTokens for ast::ImportFunction {
let wasm_bindgen_futures = &self.wasm_bindgen_futures;

for (i, arg) in self.function.arguments.iter().enumerate() {
let ty = &arg.ty;
let name = match &*arg.pat {
let ty = &arg.pat_type.ty;
let name = match &*arg.pat_type.pat {
syn::Pat::Ident(syn::PatIdent {
by_ref: None,
ident,
Expand All @@ -1347,7 +1347,7 @@ impl TryToTokens for ast::ImportFunction {
}) => ident.clone(),
syn::Pat::Wild(_) => syn::Ident::new(&format!("__genarg_{}", i), Span::call_site()),
_ => bail_span!(
arg.pat,
arg.pat_type.pat,
"unsupported pattern in #[wasm_bindgen] imported function",
),
};
Expand Down Expand Up @@ -1542,7 +1542,7 @@ impl ToTokens for DescribeImport<'_> {
ast::ImportKind::Type(_) => return,
ast::ImportKind::Enum(_) => return,
};
let argtys = f.function.arguments.iter().map(|arg| &arg.ty);
let argtys = f.function.arguments.iter().map(|arg| &arg.pat_type.ty);
let nargs = f.function.arguments.len() as u32;
let inform_ret = match &f.js_ret {
Some(ref t) => quote! { <#t as WasmDescribe>::describe(); },
Expand Down
21 changes: 20 additions & 1 deletion crates/backend/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,38 @@ fn shared_function<'a>(func: &'a ast::Function, _intern: &'a Interner) -> Functi
.iter()
.enumerate()
.map(|(idx, arg)| {
if let syn::Pat::Ident(x) = &*arg.pat {
// use argument's "js_name" if it was provided in attributes
if let Some(name) = &arg.js_name {
return name.clone();
}
if let syn::Pat::Ident(x) = &*arg.pat_type.pat {
return x.ident.unraw().to_string();
}
format!("arg{}", idx)
})
.collect::<Vec<_>>();
let fn_attrs = Some(FunctionAttributes {
ret: FunctionComponentAttributes {
ty: func.ret.ty_override.clone(),
desc: func.ret.desc.clone(),
},
args: func
.arguments
.iter()
.map(|arg_attr| FunctionComponentAttributes {
ty: arg_attr.ty_override.clone(),
desc: arg_attr.desc.clone(),
})
.collect::<Vec<_>>(),
});
Function {
arg_names,
asyncness: func.r#async,
name: &func.name,
generate_typescript: func.generate_typescript,
generate_jsdoc: func.generate_jsdoc,
variadic: func.variadic,
fn_attrs,
}
}

Expand Down
20 changes: 19 additions & 1 deletion crates/cli-support/src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ops::Deref, str};
use std::{fmt::Debug, ops::Deref, str};

pub trait Decode<'src>: Sized {
fn decode(data: &mut &'src [u8]) -> Self;
Expand Down Expand Up @@ -94,6 +94,24 @@ impl<'src, T: Decode<'src>> Decode<'src> for Option<T> {
}
}

impl Debug for FunctionAttributes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"FunctionAttributes {{ ret: {:?}, args: {:?} }}",
self.ret, self.args
))
}
}

impl Debug for FunctionComponentAttributes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"FunctionComponentAttributes {{ ty: {:?}, desc: {:?} }}",
self.ty, self.desc
))
}
}

macro_rules! decode_struct {
($name:ident ($($lt:tt)*) $($field:ident: $ty:ty,)*) => {
pub struct $name <$($lt)*> {
Expand Down
Loading
Loading