-
-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
#[napi]
procedural macro to automation development boiler…
…plate (#696) * napi procedural macro for basic rust/JavaScript types * introduce the `compat-mode` for `napi` and `napi-derive` crates for backward compatible * remove #[inline] and let compiler to decide the inline behavior * cli now can produce the `.d.ts` file for native binding * many tests and example for the new procedural macro Co-authored-by: LongYinan <[email protected]>
- Loading branch information
1 parent
b64677a
commit 2467b71
Showing
203 changed files
with
5,307 additions
and
1,499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "napi-derive-backend" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
[features] | ||
type-def = ["regex", "once_cell"] | ||
strict = [] | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0" | ||
quote = "1.0" | ||
syn = {version = "1.0", features = ["fold", "full"]} | ||
convert_case = "0.4.0" | ||
|
||
[dependencies.regex] | ||
optional = true | ||
version = "1" | ||
|
||
[dependencies.once_cell] | ||
optional = true | ||
version = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# napi-derive-backend | ||
|
||
Take care the ast parsing from `napi-derive` and generate "bridge" runtime code for both nodejs and rust. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use proc_macro2::Ident; | ||
use syn::Attribute; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NapiFn { | ||
pub name: Ident, | ||
pub js_name: String, | ||
pub attrs: Vec<Attribute>, | ||
pub args: Vec<NapiFnArgKind>, | ||
pub ret: Option<syn::Type>, | ||
pub is_async: bool, | ||
pub fn_self: Option<FnSelf>, | ||
pub kind: FnKind, | ||
pub vis: syn::Visibility, | ||
pub parent: Option<Ident>, | ||
pub strict: bool, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct CallbackArg { | ||
pub pat: Box<syn::Pat>, | ||
pub args: Vec<syn::Type>, | ||
pub ret: Option<syn::Type>, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum NapiFnArgKind { | ||
PatType(Box<syn::PatType>), | ||
Callback(Box<CallbackArg>), | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum FnKind { | ||
Normal, | ||
Constructor, | ||
Getter, | ||
Setter, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum FnSelf { | ||
Value, | ||
Ref, | ||
MutRef, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NapiStruct { | ||
pub name: Ident, | ||
pub js_name: String, | ||
pub vis: syn::Visibility, | ||
pub fields: Vec<NapiStructField>, | ||
pub is_tuple: bool, | ||
pub gen_default_ctor: bool, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NapiStructField { | ||
pub name: syn::Member, | ||
pub js_name: String, | ||
pub ty: syn::Type, | ||
pub getter: bool, | ||
pub setter: bool, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NapiImpl { | ||
pub name: Ident, | ||
pub js_name: String, | ||
pub items: Vec<NapiFn>, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NapiEnum { | ||
pub name: Ident, | ||
pub js_name: String, | ||
pub variants: Vec<NapiEnumVariant>, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NapiEnumVariant { | ||
pub name: Ident, | ||
pub val: i32, | ||
pub comments: Vec<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use proc_macro2::{Ident, Span, TokenStream}; | ||
|
||
use crate::BindgenResult; | ||
|
||
mod r#enum; | ||
mod r#fn; | ||
mod r#struct; | ||
|
||
pub trait TryToTokens { | ||
fn try_to_tokens(&self, tokens: &mut TokenStream) -> BindgenResult<()>; | ||
|
||
fn try_to_token_stream(&self) -> BindgenResult<TokenStream> { | ||
let mut tokens = TokenStream::default(); | ||
self.try_to_tokens(&mut tokens)?; | ||
|
||
Ok(tokens) | ||
} | ||
} | ||
|
||
fn get_intermediate_ident(name: &str) -> Ident { | ||
let new_name = format!("__napi__{}", name); | ||
Ident::new(&new_name, Span::call_site()) | ||
} | ||
|
||
fn get_register_ident(name: &str) -> Ident { | ||
let new_name = format!("__napi_register__{}", name); | ||
Ident::new(&new_name, Span::call_site()) | ||
} |
Oops, something went wrong.