From b2e71b5e033a5c92d493f644701267ad39f2a356 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 8 Dec 2021 11:30:36 +0800 Subject: [PATCH 1/2] feat(napi-derive): add noop feature to skip napi function register --- crates/backend/Cargo.toml | 3 ++- crates/backend/src/codegen/const.rs | 2 +- crates/backend/src/codegen/enum.rs | 2 +- crates/backend/src/codegen/fn.rs | 2 +- crates/backend/src/codegen/struct.rs | 4 ++-- crates/macro/Cargo.toml | 3 ++- 6 files changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index 67897549..135f657d 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -6,11 +6,12 @@ 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] strict = [] type-def = ["regex", "once_cell"] +noop = [] [dependencies] convert_case = "0.4" diff --git a/crates/backend/src/codegen/const.rs b/crates/backend/src/codegen/const.rs index 5fd14a00..9828608a 100644 --- a/crates/backend/src/codegen/const.rs +++ b/crates/backend/src/codegen/const.rs @@ -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); diff --git a/crates/backend/src/codegen/enum.rs b/crates/backend/src/codegen/enum.rs index 11060823..7d16d945 100644 --- a/crates/backend/src/codegen/enum.rs +++ b/crates/backend/src/codegen/enum.rs @@ -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); diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index 7ca411fc..480a0b93 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -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); diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index d9aa1c75..01f8ad88 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -453,7 +453,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 +523,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),*]); diff --git a/crates/macro/Cargo.toml b/crates/macro/Cargo.toml index 91ddac08..04ef30de 100644 --- a/crates/macro/Cargo.toml +++ b/crates/macro/Cargo.toml @@ -7,7 +7,7 @@ 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 = [] @@ -15,6 +15,7 @@ default = ["compat-mode", "full"] full = ["type-def", "strict"] strict = ["napi-derive-backend/strict"] type-def = ["napi-derive-backend/type-def"] +noop = ["napi-derive-backend/noop"] [dependencies] convert_case = "0.4" From e452c00fb4b6d8fad71a89acb115a5e3d657d656 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 8 Dec 2021 12:52:33 +0800 Subject: [PATCH 2/2] feat: edition 2021 --- .github/workflows/lint.yaml | 2 +- README.md | 4 ++++ bench/Cargo.toml | 2 +- crates/backend/Cargo.toml | 4 ++-- crates/backend/src/codegen/enum.rs | 2 +- crates/backend/src/codegen/struct.rs | 21 +++++++--------- crates/build/Cargo.toml | 2 +- crates/macro/Cargo.toml | 4 ++-- crates/macro/src/lib.rs | 36 ++++++++++++++++++++-------- crates/napi/Cargo.toml | 2 +- crates/napi/src/env.rs | 4 +++- crates/napi/src/error.rs | 7 ++++++ crates/sys/Cargo.toml | 2 +- examples/napi-compat-mode/Cargo.toml | 2 +- examples/napi/Cargo.toml | 2 +- memory-testing/Cargo.toml | 2 +- 16 files changed, 62 insertions(+), 36 deletions(-) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index e7e46bff..e48214a4 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -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: | diff --git a/README.md b/README.md index 732ab527..e71065af 100644 --- a/README.md +++ b/README.md @@ -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 | ✓ | ✓ | ✓ | ✓ | diff --git a/bench/Cargo.toml b/bench/Cargo.toml index 3774a48a..dd41b348 100644 --- a/bench/Cargo.toml +++ b/bench/Cargo.toml @@ -1,6 +1,6 @@ [package] authors = ["LongYinan "] -edition = "2018" +edition = "2021" name = "napi-bench" publish = false version = "0.1.0" diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index 135f657d..dee06498 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -1,6 +1,6 @@ [package] description = "Codegen backend for napi procedural macro" -edition = "2018" +edition = "2021" homepage = "https://napi.rs" license = "MIT" name = "napi-derive-backend" @@ -9,9 +9,9 @@ repository = "https://github.com/napi-rs/napi-rs" version = "1.0.16" [features] +noop = [] strict = [] type-def = ["regex", "once_cell"] -noop = [] [dependencies] convert_case = "0.4" diff --git a/crates/backend/src/codegen/enum.rs b/crates/backend/src/codegen/enum.rs index 7d16d945..f85210f3 100644 --- a/crates/backend/src/codegen/enum.rs +++ b/crates/backend/src/codegen/enum.rs @@ -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! { diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index 01f8ad88..1af55d32 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -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)); }); } } } diff --git a/crates/build/Cargo.toml b/crates/build/Cargo.toml index 483ead17..0f07d768 100644 --- a/crates/build/Cargo.toml +++ b/crates/build/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["LongYinan "] description = "N-API build support" -edition = "2018" +edition = "2021" include = ["src/**/*", "Cargo.toml", "README.md", "LICENSE"] keywords = ["NodeJS", "FFI", "NAPI", "n-api"] license = "MIT" diff --git a/crates/macro/Cargo.toml b/crates/macro/Cargo.toml index 04ef30de..7bcc1423 100644 --- a/crates/macro/Cargo.toml +++ b/crates/macro/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["LongYinan ", "Forehalo "] description = "N-API procedural macros" -edition = "2018" +edition = "2021" keywords = ["NodeJS", "FFI", "NAPI", "n-api"] license = "MIT" name = "napi-derive" @@ -13,9 +13,9 @@ version = "2.0.0-beta.4" 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"] -noop = ["napi-derive-backend/noop"] [dependencies] convert_case = "0.4" diff --git a/crates/macro/src/lib.rs b/crates/macro/src/lib.rs index a073eb6a..19a99aa8 100644 --- a/crates/macro/src/lib.rs +++ b/crates/macro/src/lib.rs @@ -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 { let mut item = syn::parse2::(input)?; let opts: BindgenAttrs = syn::parse2(attr)?; @@ -125,7 +140,7 @@ fn expand(attr: TokenStream, input: TokenStream) -> BindgenResult { } } -#[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, diff --git a/crates/napi/Cargo.toml b/crates/napi/Cargo.toml index 74347d48..9079b54e 100644 --- a/crates/napi/Cargo.toml +++ b/crates/napi/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["Nathan Sobo ", "Yinan Long "] description = "N-API bindings" -edition = "2018" +edition = "2021" keywords = ["NodeJS", "Node", "FFI", "NAPI", "n-api"] license = "MIT" name = "napi" diff --git a/crates/napi/src/env.rs b/crates/napi/src/env.rs index 13599bc4..9718d206 100644 --- a/crates/napi/src/env.rs +++ b/crates/napi/src/env.rs @@ -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"))] diff --git a/crates/napi/src/error.rs b/crates/napi/src/error.rs index 846dea58..8eae399a 100644 --- a/crates/napi/src/error.rs +++ b/crates/napi/src/error.rs @@ -25,6 +25,7 @@ pub struct Error { pub reason: String, // Convert raw `JsError` into Error // Only be used in `async fn(p: Promise)` scenario + #[cfg(all(feature = "tokio_rt", feature = "napi4"))] pub(crate) maybe_raw: sys::napi_ref, } @@ -54,6 +55,7 @@ impl From for Error { } } +#[cfg(all(feature = "tokio_rt", feature = "napi4"))] impl From 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 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 for Error { Error { status: Status::GenericFailure, reason: format!("{}", error), + #[cfg(all(feature = "tokio_rt", feature = "napi4"))] maybe_raw: ptr::null_mut(), } } diff --git a/crates/sys/Cargo.toml b/crates/sys/Cargo.toml index c248bdb7..9503bdcd 100644 --- a/crates/sys/Cargo.toml +++ b/crates/sys/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["LongYinan "] 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" diff --git a/examples/napi-compat-mode/Cargo.toml b/examples/napi-compat-mode/Cargo.toml index 96ef7ab7..23d790b6 100644 --- a/examples/napi-compat-mode/Cargo.toml +++ b/examples/napi-compat-mode/Cargo.toml @@ -1,6 +1,6 @@ [package] authors = ["LongYinan "] -edition = "2018" +edition = "2021" name = "napi-compat-mode-examples" publish = false version = "0.1.0" diff --git a/examples/napi/Cargo.toml b/examples/napi/Cargo.toml index 9db80ca8..d7b58c95 100644 --- a/examples/napi/Cargo.toml +++ b/examples/napi/Cargo.toml @@ -1,6 +1,6 @@ [package] authors = ["LongYinan "] -edition = "2018" +edition = "2021" name = "napi-examples" publish = false version = "0.1.0" diff --git a/memory-testing/Cargo.toml b/memory-testing/Cargo.toml index 437fdc83..51091dbf 100644 --- a/memory-testing/Cargo.toml +++ b/memory-testing/Cargo.toml @@ -1,6 +1,6 @@ [package] authors = ["LongYinan "] -edition = "2018" +edition = "2021" name = "memory-testing" publish = false version = "0.1.0"