Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ impl From<Arg<'_>> for Parameter {
name: val.name.into(),
ty: Some(val.r#type).into(),
nullable: val.allow_null,
variadic: val.variadic,
default: val.default_value.map(abi::RString::from).into(),
}
}
Expand Down
69 changes: 62 additions & 7 deletions src/describe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,25 @@ pub struct Module {
impl From<ModuleBuilder<'_>> for Module {
fn from(builder: ModuleBuilder) -> Self {
let functions = builder.functions;

#[allow(unused_mut)]
let mut classes = builder
.classes
.into_iter()
.map(|c| c().into())
.collect::<StdVec<_>>();

#[cfg(feature = "closure")]
classes.push(Class::closure());

Self {
name: builder.name.into(),
functions: functions
.into_iter()
.map(Function::from)
.collect::<StdVec<_>>()
.into(),
classes: builder
.classes
.into_iter()
.map(|c| c().into())
.collect::<StdVec<_>>()
.into(),
classes: classes.into(),
constants: builder
.constants
.into_iter()
Expand Down Expand Up @@ -151,6 +157,8 @@ pub struct Parameter {
pub ty: Option<DataType>,
/// Whether the parameter is nullable.
pub nullable: bool,
/// Whether the parameter is variadic.
pub variadic: bool,
/// Default value of the parameter.
pub default: Option<RString>,
}
Expand All @@ -175,6 +183,43 @@ pub struct Class {
pub constants: Vec<Constant>,
}

#[cfg(feature = "closure")]
impl Class {
/// Creates a new class representing a Rust closure used for generating
/// the stubs if the `closure` feature is enabled.
#[must_use]
pub fn closure() -> Self {
Self {
name: "RustClosure".into(),
docs: DocBlock(StdVec::new().into()),
extends: Option::None,
implements: StdVec::new().into(),
properties: StdVec::new().into(),
methods: vec![Method {
name: "__invoke".into(),
docs: DocBlock(StdVec::new().into()),
ty: MethodType::Member,
params: vec![Parameter {
name: "args".into(),
ty: Option::Some(DataType::Mixed),
nullable: false,
variadic: true,
default: Option::None,
}]
.into(),
retval: Option::Some(Retval {
ty: DataType::Mixed,
nullable: false,
}),
r#static: false,
visibility: Visibility::Public,
}]
.into(),
constants: StdVec::new().into(),
}
}
}

impl From<ClassBuilder> for Class {
fn from(val: ClassBuilder) -> Self {
Self {
Expand Down Expand Up @@ -426,6 +471,8 @@ impl From<(String, Box<dyn IntoConst + Send>, DocComments)> for Constant {
#[cfg(test)]
mod tests {
#![cfg_attr(windows, feature(abi_vectorcall))]
use cfg_if::cfg_if;

use super::*;

use crate::{args::Arg, test::test_function};
Expand Down Expand Up @@ -460,7 +507,13 @@ mod tests {
let module: Module = builder.into();
assert_eq!(module.name, "test".into());
assert_eq!(module.functions.len(), 1);
assert_eq!(module.classes.len(), 0);
cfg_if! {
if #[cfg(feature = "closure")] {
assert_eq!(module.classes.len(), 1);
} else {
assert_eq!(module.classes.len(), 0);
}
}
assert_eq!(module.constants.len(), 0);
}

Expand All @@ -479,6 +532,7 @@ mod tests {
name: "foo".into(),
ty: Option::Some(DataType::Long),
nullable: false,
variadic: false,
default: Option::None,
}]
.into()
Expand Down Expand Up @@ -568,6 +622,7 @@ mod tests {
name: "foo".into(),
ty: Option::Some(DataType::Long),
nullable: false,
variadic: false,
default: Option::None,
}]
.into()
Expand Down
4 changes: 4 additions & 0 deletions src/describe/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ impl ToStub for Parameter {
write!(buf, " ")?;
}

if self.variadic {
write!(buf, "...")?;
}

write!(buf, "${}", self.name)
}
}
Expand Down
Loading