Skip to content

Commit

Permalink
fix(cli,napi-derive): backward compatible with older cli with #1531
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Mar 22, 2023
1 parent 2f4d9d4 commit ee3aa96
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions cli/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ export class BuildCommand extends Command {
...additionalEnv,
TYPE_DEF_TMP_PATH: intermediateTypeFile,
WASI_REGISTER_TMP_PATH: intermediateWasiRegisterFile,
CARGO_CFG_NAPI_RS_CLI_VERSION: version,
}

try {
Expand Down
6 changes: 5 additions & 1 deletion crates/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ independent = true
[features]
noop = []
strict = []
type-def = ["regex", "once_cell"]
type-def = ["regex", "once_cell", "semver"]

[dependencies]
convert_case = "0.6"
Expand All @@ -30,3 +30,7 @@ version = "1"
[dependencies.once_cell]
optional = true
version = "1"

[dependencies.semver]
optional = true
version = "1"
2 changes: 2 additions & 0 deletions crates/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub use ast::*;
pub use codegen::*;
pub use error::{BindgenResult, Diagnostic};
#[cfg(feature = "type-def")]
pub use semver;
#[cfg(feature = "type-def")]
pub use typegen::*;

#[derive(Debug)]
Expand Down
22 changes: 19 additions & 3 deletions crates/backend/src/typegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ mod r#enum;
mod r#fn;
pub(crate) mod r#struct;

use std::{cell::RefCell, collections::HashMap};
use std::{cell::RefCell, collections::HashMap, env};

use once_cell::sync::Lazy;
use syn::Type;

pub static NAPI_RS_CLI_VERSION: Lazy<semver::Version> = Lazy::new(|| {
let version = env::var("CARGO_CFG_NAPI_RS_CLI_VERSION").unwrap_or_else(|_| "0.0.0".to_string());
semver::Version::parse(&version).unwrap_or_else(|_| semver::Version::new(0, 0, 0))
});

pub static NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX: Lazy<semver::Version> =
Lazy::new(|| semver::Version::new(2, 15, 1));

#[derive(Default, Debug)]
pub struct TypeDef {
pub kind: String,
Expand Down Expand Up @@ -103,9 +111,17 @@ impl ToString for TypeDef {
} else {
"".to_owned()
};
// TODO: remove this in v3
// This is a workaround for lower version of @napi-rs/cli
// See https://github.com/napi-rs/napi-rs/pull/1531
let prefix = if *NAPI_RS_CLI_VERSION >= *NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX {
format!("{}:", pkg_name)
} else {
"".to_string()
};
format!(
r#"{}:{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#,
pkg_name,
r#"{}{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#,
prefix,
self.kind,
self.name,
escape_json(&self.js_doc),
Expand Down
7 changes: 6 additions & 1 deletion crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ pub fn napi(attr: RawStream, input: RawStream) -> RawStream {
// logic on first macro expansion
#[cfg(feature = "type-def")]
if let Ok(ref type_def_file) = env::var("TYPE_DEF_TMP_PATH") {
if let Err(_e) = remove_existed_type_def(type_def_file) {
use napi_derive_backend::{NAPI_RS_CLI_VERSION, NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX};
if let Err(_e) = if *NAPI_RS_CLI_VERSION >= *NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX {
remove_existed_type_def(type_def_file)
} else {
fs::remove_file(type_def_file)
} {
#[cfg(debug_assertions)]
{
println!("Failed to manipulate type def file: {:?}", _e);
Expand Down

1 comment on commit ee3aa96

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: ee3aa96 Previous: 2f4d9d4 Ratio
noop#napi-rs 65759245 ops/sec (±0.23%) 44114289 ops/sec (±0.89%) 0.67
noop#JavaScript 591733263 ops/sec (±0.94%) 627819334 ops/sec (±0.65%) 1.06
Plus number#napi-rs 19530976 ops/sec (±0.2%) 8319923 ops/sec (±1.37%) 0.43
Plus number#JavaScript 592763484 ops/sec (±0.12%) 625188130 ops/sec (±0.96%) 1.05
Create buffer#napi-rs 423802 ops/sec (±6.55%) 320729 ops/sec (±11.66%) 0.76
Create buffer#JavaScript 2307961 ops/sec (±4.88%) 1487094 ops/sec (±8.92%) 0.64
createArray#createArrayJson 46010 ops/sec (±0.11%) 32663 ops/sec (±0.75%) 0.71
createArray#create array for loop 8587 ops/sec (±0.11%) 5628 ops/sec (±0.95%) 0.66
createArray#create array with serde trait 8574 ops/sec (±0.1%) 5728 ops/sec (±0.81%) 0.67
getArrayFromJs#get array from json string 18469 ops/sec (±0.12%) 14010 ops/sec (±0.83%) 0.76
getArrayFromJs#get array from serde 11476 ops/sec (±0.13%) 7891 ops/sec (±0.81%) 0.69
getArrayFromJs#get array with for loop 13263 ops/sec (±0.54%) 9843 ops/sec (±0.9%) 0.74
Get Set property#Get Set from native#u32 423792 ops/sec (±7.58%) 370559 ops/sec (±8.47%) 0.87
Get Set property#Get Set from JavaScript#u32 362089 ops/sec (±7.64%) 312476 ops/sec (±9.99%) 0.86
Get Set property#Get Set from native#string 389587 ops/sec (±10.59%) 331733 ops/sec (±3.86%) 0.85
Get Set property#Get Set from JavaScript#string 357834 ops/sec (±2.66%) 294474 ops/sec (±15.96%) 0.82
Async task#spawn task 35798 ops/sec (±1.54%) 24655 ops/sec (±2.36%) 0.69
Async task#ThreadSafeFunction 2658 ops/sec (±2.03%) 1902 ops/sec (±3.71%) 0.72
Async task#Tokio future to Promise 32754 ops/sec (±1.28%) 24078 ops/sec (±2.16%) 0.74
Query#query * 100 2109 ops/sec (±1.44%) 1503 ops/sec (±3.27%) 0.71
Query#query * 1 32681 ops/sec (±1.04%) 21749 ops/sec (±2.29%) 0.67

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.