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 run: cargo fmt -- --check
- name: Clippy - name: Clippy
run: cargo clippy --all-features run: cargo clippy
- name: Clear the cargo caches - name: Clear the cargo caches
run: | 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) [![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) [![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 | | | node12 | node14 | node16 | node17 |
| --------------------- | ------ | ------ | ------ | ------ | | --------------------- | ------ | ------ | ------ | ------ |
| Windows x64 | ✓ | ✓ | ✓ | ✓ | | Windows x64 | ✓ | ✓ | ✓ | ✓ |

View file

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

View file

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

View file

@ -38,7 +38,7 @@ impl NapiConst {
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
#[allow(clippy::all)] #[allow(clippy::all)]
#[cfg(not(test))] #[cfg(all(not(test), not(feature = "noop")))]
#[napi::bindgen_prelude::ctor] #[napi::bindgen_prelude::ctor]
fn #register_name() { fn #register_name() {
napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name_lit, #cb_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![]; let mut define_properties = vec![];
for variant in self.variants.iter() { 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); let val_lit = Literal::i32_unsuffixed(variant.val);
define_properties.push(quote! { define_properties.push(quote! {
@ -147,7 +147,7 @@ impl NapiEnum {
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
#[allow(clippy::all)] #[allow(clippy::all)]
#[cfg(not(test))] #[cfg(all(not(test), not(feature = "noop")))]
#[napi::bindgen_prelude::ctor] #[napi::bindgen_prelude::ctor]
fn #register_name() { fn #register_name() {
napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name_lit, #callback_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(clippy::all)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
#[cfg(not(test))] #[cfg(all(not(test), not(feature = "noop")))]
#[napi::bindgen_prelude::ctor] #[napi::bindgen_prelude::ctor]
fn #module_register_name() { fn #module_register_name() {
napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name, #cb_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 { impl NapiStruct {
fn gen_helper_mod(&self) -> TokenStream { fn gen_helper_mod(&self) -> TokenStream {
let mod_name = Ident::new( let mod_name = Ident::new(&format!("__napi_helper__{}", self.name), Span::call_site());
&format!("__napi_helper__{}", self.name.to_string()),
Span::call_site(),
);
let ctor = if self.kind == NapiStructKind::Constructor { let ctor = if self.kind == NapiStructKind::Constructor {
self.gen_default_ctor() self.gen_default_ctor()
@ -453,7 +450,7 @@ impl NapiStruct {
quote! { quote! {
#[allow(non_snake_case)] #[allow(non_snake_case)]
#[allow(clippy::all)] #[allow(clippy::all)]
#[cfg(not(test))] #[cfg(all(not(test), not(feature = "noop")))]
#[napi::bindgen_prelude::ctor] #[napi::bindgen_prelude::ctor]
fn #struct_register_name() { fn #struct_register_name() {
napi::bindgen_prelude::register_class(#name_str, #js_mod_ident, #js_name, vec![#(#props),*]); napi::bindgen_prelude::register_class(#name_str, #js_mod_ident, #js_name, vec![#(#props),*]);
@ -523,7 +520,7 @@ impl NapiImpl {
use super::*; use super::*;
#(#methods)* #(#methods)*
#[cfg(not(test))] #[cfg(all(not(test), not(feature = "noop")))]
#[napi::bindgen_prelude::ctor] #[napi::bindgen_prelude::ctor]
fn #register_name() { fn #register_name() {
napi::bindgen_prelude::register_class(#name_str, #js_mod_ident, #js_name, vec![#(#props),*]); napi::bindgen_prelude::register_class(#name_str, #js_mod_ident, #js_name, vec![#(#props),*]);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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