Merge pull request #918 from napi-rs/noop-napi-derive

Noop napi derive
This commit is contained in:
LongYinan 2021-12-08 13:31:52 +08:00 committed by GitHub
commit 8e0717dcda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 69 additions and 41 deletions

View file

@ -62,7 +62,7 @@ jobs:
run: cargo fmt -- --check
- name: Clippy
run: cargo clippy --all-features
run: cargo clippy
- name: Clear the cargo caches
run: |

View file

@ -55,6 +55,10 @@ _Main branch is now under napi@next developing. Checkout [v1 docs](https://napi.
[![Windows arm64](https://github.com/napi-rs/napi-rs/actions/workflows/windows-arm.yml/badge.svg)](https://github.com/napi-rs/napi-rs/actions/workflows/windows-arm.yml)
[![FreeBSD](https://api.cirrus-ci.com/github/napi-rs/napi-rs.svg)](https://cirrus-ci.com/github/napi-rs/napi-rs?branch=main)
## MSRV
**Rust** `1.57.0`
| | node12 | node14 | node16 | node17 |
| --------------------- | ------ | ------ | ------ | ------ |
| Windows x64 | ✓ | ✓ | ✓ | ✓ |

View file

@ -1,6 +1,6 @@
[package]
authors = ["LongYinan <lynweklm@gmail.com>"]
edition = "2018"
edition = "2021"
name = "napi-bench"
publish = false
version = "0.1.0"

View file

@ -1,14 +1,15 @@
[package]
description = "Codegen backend for napi procedural macro"
edition = "2018"
edition = "2021"
homepage = "https://napi.rs"
license = "MIT"
name = "napi-derive-backend"
readme = "README.md"
repository = "https://github.com/napi-rs/napi-rs"
version = "1.0.15"
version = "1.0.16"
[features]
noop = []
strict = []
type-def = ["regex", "once_cell"]

View file

@ -38,7 +38,7 @@ impl NapiConst {
}
#[allow(non_snake_case)]
#[allow(clippy::all)]
#[cfg(not(test))]
#[cfg(all(not(test), not(feature = "noop")))]
#[napi::bindgen_prelude::ctor]
fn #register_name() {
napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name_lit, #cb_name);

View file

@ -107,7 +107,7 @@ impl NapiEnum {
let mut define_properties = vec![];
for variant in self.variants.iter() {
let name_lit = Literal::string(&format!("{}\0", variant.name.to_string()));
let name_lit = Literal::string(&format!("{}\0", variant.name));
let val_lit = Literal::i32_unsuffixed(variant.val);
define_properties.push(quote! {
@ -147,7 +147,7 @@ impl NapiEnum {
}
#[allow(non_snake_case)]
#[allow(clippy::all)]
#[cfg(not(test))]
#[cfg(all(not(test), not(feature = "noop")))]
#[napi::bindgen_prelude::ctor]
fn #register_name() {
napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name_lit, #callback_name);

View file

@ -327,7 +327,7 @@ impl NapiFn {
#[allow(clippy::all)]
#[allow(non_snake_case)]
#[cfg(not(test))]
#[cfg(all(not(test), not(feature = "noop")))]
#[napi::bindgen_prelude::ctor]
fn #module_register_name() {
napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name, #cb_name);

View file

@ -82,10 +82,7 @@ impl TryToTokens for NapiStruct {
impl NapiStruct {
fn gen_helper_mod(&self) -> TokenStream {
let mod_name = Ident::new(
&format!("__napi_helper__{}", self.name.to_string()),
Span::call_site(),
);
let mod_name = Ident::new(&format!("__napi_helper__{}", self.name), Span::call_site());
let ctor = if self.kind == NapiStructKind::Constructor {
self.gen_default_ctor()
@ -177,8 +174,8 @@ impl NapiStruct {
field_conversions.push(quote! { <#ty as ToNapiValue>::to_napi_value(env, #ident)? });
}
syn::Member::Unnamed(i) => {
field_destructions.push(quote! { arg#i });
field_conversions.push(quote! { <#ty as ToNapiValue>::to_napi_value(env, arg#i)? });
field_destructions.push(quote! { arg #i });
field_conversions.push(quote! { <#ty as ToNapiValue>::to_napi_value(env, arg #i)? });
}
}
}
@ -271,20 +268,20 @@ impl NapiStruct {
}
}
syn::Member::Unnamed(i) => {
field_destructions.push(quote! { arg#i });
field_destructions.push(quote! { arg #i });
if is_optional_field {
obj_field_setters.push(quote! {
if arg#1.is_some() {
obj.set(#field_js_name, arg#i)?;
if arg #1.is_some() {
obj.set(#field_js_name, arg #i)?;
}
});
} else {
obj_field_setters.push(quote! { obj.set(#field_js_name, arg#1)?; });
obj_field_setters.push(quote! { obj.set(#field_js_name, arg #1)?; });
}
if is_optional_field {
obj_field_getters.push(quote! { let arg#i: #ty = obj.get(#field_js_name)?; });
obj_field_getters.push(quote! { let arg #i: #ty = obj.get(#field_js_name)?; });
} else {
obj_field_getters.push(quote! { let arg#i: #ty = obj.get(#field_js_name)?.expect(&format!("Field {} should exist", #field_js_name)); });
obj_field_getters.push(quote! { let arg #i: #ty = obj.get(#field_js_name)?.expect(&format!("Field {} should exist", #field_js_name)); });
}
}
}
@ -453,7 +450,7 @@ impl NapiStruct {
quote! {
#[allow(non_snake_case)]
#[allow(clippy::all)]
#[cfg(not(test))]
#[cfg(all(not(test), not(feature = "noop")))]
#[napi::bindgen_prelude::ctor]
fn #struct_register_name() {
napi::bindgen_prelude::register_class(#name_str, #js_mod_ident, #js_name, vec![#(#props),*]);
@ -523,7 +520,7 @@ impl NapiImpl {
use super::*;
#(#methods)*
#[cfg(not(test))]
#[cfg(all(not(test), not(feature = "noop")))]
#[napi::bindgen_prelude::ctor]
fn #register_name() {
napi::bindgen_prelude::register_class(#name_str, #js_mod_ident, #js_name, vec![#(#props),*]);

View file

@ -1,7 +1,7 @@
[package]
authors = ["LongYinan <lynweklm@gmail.com>"]
description = "N-API build support"
edition = "2018"
edition = "2021"
include = ["src/**/*", "Cargo.toml", "README.md", "LICENSE"]
keywords = ["NodeJS", "FFI", "NAPI", "n-api"]
license = "MIT"

View file

@ -1,18 +1,19 @@
[package]
authors = ["LongYinan <lynweklm@gmail.com>", "Forehalo <forehalo@gmail.com>"]
description = "N-API procedural macros"
edition = "2018"
edition = "2021"
keywords = ["NodeJS", "FFI", "NAPI", "n-api"]
license = "MIT"
name = "napi-derive"
readme = "README.md"
repository = "https://github.com/napi-rs/napi-rs"
version = "2.0.0-beta.3"
version = "2.0.0-beta.4"
[features]
compat-mode = []
default = ["compat-mode", "full"]
full = ["type-def", "strict"]
noop = ["napi-derive-backend/noop"]
strict = ["napi-derive-backend/strict"]
type-def = ["napi-derive-backend/type-def"]

View file

@ -8,22 +8,29 @@ extern crate syn;
extern crate napi_derive_backend;
#[macro_use]
extern crate quote;
use napi_derive_backend::{BindgenResult, TryToTokens};
#[cfg(feature = "type-def")]
use napi_derive_backend::{ToTypeDef, TypeDef};
use parser::{attrs::BindgenAttrs, ParseNapi};
use proc_macro::TokenStream as RawStream;
use proc_macro2::{TokenStream, TokenTree};
use quote::ToTokens;
#[cfg(not(feature = "noop"))]
use std::env;
#[cfg(feature = "type-def")]
#[cfg(all(feature = "type-def", not(feature = "noop")))]
use std::{
fs,
io::{BufWriter, Result as IOResult, Write},
};
#[cfg(not(feature = "noop"))]
use napi_derive_backend::{BindgenResult, TryToTokens};
#[cfg(all(feature = "type-def", not(feature = "noop")))]
use napi_derive_backend::{ToTypeDef, TypeDef};
#[cfg(not(feature = "noop"))]
use parser::{attrs::BindgenAttrs, ParseNapi};
use proc_macro::TokenStream as RawStream;
#[cfg(not(feature = "noop"))]
use proc_macro2::{TokenStream, TokenTree};
#[cfg(not(feature = "noop"))]
use quote::ToTokens;
#[cfg(feature = "compat-mode")]
use syn::{fold::Fold, parse_macro_input, ItemFn};
#[cfg(not(feature = "noop"))]
use syn::{Attribute, Item};
/// ```ignore
@ -32,12 +39,13 @@ use syn::{Attribute, Item};
/// "hello" + name
/// }
/// ```
#[cfg(not(feature = "noop"))]
#[proc_macro_attribute]
pub fn napi(attr: RawStream, input: RawStream) -> RawStream {
match expand(attr.into(), input.into()) {
Ok(tokens) => {
if env::var("DEBUG_GENERATED_CODE").is_ok() {
println!("{}", tokens.to_string());
println!("{}", tokens);
}
tokens.into()
}
@ -49,6 +57,13 @@ pub fn napi(attr: RawStream, input: RawStream) -> RawStream {
}
}
#[cfg(feature = "noop")]
#[proc_macro_attribute]
pub fn napi(_attr: RawStream, input: RawStream) -> RawStream {
input
}
#[cfg(not(feature = "noop"))]
fn expand(attr: TokenStream, input: TokenStream) -> BindgenResult<TokenStream> {
let mut item = syn::parse2::<syn::Item>(input)?;
let opts: BindgenAttrs = syn::parse2(attr)?;
@ -125,7 +140,7 @@ fn expand(attr: TokenStream, input: TokenStream) -> BindgenResult<TokenStream> {
}
}
#[cfg(feature = "type-def")]
#[cfg(all(feature = "type-def", not(feature = "noop")))]
fn output_type_def(type_def_file: String, type_def: TypeDef) -> IOResult<()> {
let file = fs::OpenOptions::new()
.append(true)
@ -273,6 +288,7 @@ pub fn module_exports(_attr: RawStream, input: RawStream) -> RawStream {
.into()
}
#[cfg(not(feature = "noop"))]
fn replace_napi_attr_in_mod(
js_namespace: String,
attrs: &mut Vec<syn::Attribute>,

View file

@ -1,7 +1,7 @@
[package]
authors = ["Nathan Sobo <nathan@github.com>", "Yinan Long <lynweklm@gmail.com>"]
description = "N-API bindings"
edition = "2018"
edition = "2021"
keywords = ["NodeJS", "Node", "FFI", "NAPI", "n-api"]
license = "MIT"
name = "napi"

View file

@ -11,7 +11,7 @@ use crate::{
js_values::*,
sys,
task::Task,
Error, ExtendedErrorInfo, JsError, NodeVersion, Result, Status, ValueType,
Error, ExtendedErrorInfo, NodeVersion, Result, Status, ValueType,
};
#[cfg(feature = "napi8")]
@ -22,6 +22,8 @@ use crate::cleanup_env::{CleanupEnvHook, CleanupEnvHookData};
use crate::js_values::{De, Ser};
#[cfg(feature = "napi4")]
use crate::threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunction};
#[cfg(feature = "napi3")]
use crate::JsError;
#[cfg(all(feature = "serde-json"))]
use serde::de::DeserializeOwned;
#[cfg(all(feature = "serde-json"))]

View file

@ -25,6 +25,7 @@ pub struct Error {
pub reason: String,
// Convert raw `JsError` into Error
// Only be used in `async fn(p: Promise<T>)` scenario
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
pub(crate) maybe_raw: sys::napi_ref,
}
@ -54,6 +55,7 @@ impl From<SerdeJSONError> for Error {
}
}
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
impl From<sys::napi_ref> for Error {
fn from(value: sys::napi_ref) -> Self {
Self {
@ -79,6 +81,7 @@ impl Error {
Error {
status,
reason,
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
maybe_raw: ptr::null_mut(),
}
}
@ -87,6 +90,7 @@ impl Error {
Error {
status,
reason: "".to_owned(),
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
maybe_raw: ptr::null_mut(),
}
}
@ -95,6 +99,7 @@ impl Error {
Error {
status: Status::GenericFailure,
reason,
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
maybe_raw: ptr::null_mut(),
}
}
@ -105,6 +110,7 @@ impl From<std::ffi::NulError> for Error {
Error {
status: Status::GenericFailure,
reason: format!("{}", error),
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
maybe_raw: ptr::null_mut(),
}
}
@ -115,6 +121,7 @@ impl From<std::io::Error> for Error {
Error {
status: Status::GenericFailure,
reason: format!("{}", error),
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
maybe_raw: ptr::null_mut(),
}
}

View file

@ -1,7 +1,7 @@
[package]
authors = ["LongYinan <lynweklm@gmail.com>"]
description = "NodeJS N-API raw binding"
edition = "2018"
edition = "2021"
include = ["src/**/*", "Cargo.toml", "build.rs", ".node-headers/**/*"]
keywords = ["NodeJS", "FFI", "NAPI", "n-api"]
license = "MIT"

View file

@ -1,6 +1,6 @@
[package]
authors = ["LongYinan <lynweklm@gmail.com>"]
edition = "2018"
edition = "2021"
name = "napi-compat-mode-examples"
publish = false
version = "0.1.0"

View file

@ -1,6 +1,6 @@
[package]
authors = ["LongYinan <lynweklm@gmail.com>"]
edition = "2018"
edition = "2021"
name = "napi-examples"
publish = false
version = "0.1.0"

View file

@ -1,6 +1,6 @@
[package]
authors = ["LongYinan <lynweklm@gmail.com>"]
edition = "2018"
edition = "2021"
name = "memory-testing"
publish = false
version = "0.1.0"