Skip to content

Commit

Permalink
Make Node-API 8 the default
Browse files Browse the repository at this point in the history
Additionally, warn on older versions instead of panicking.
  • Loading branch information
kjvalencik committed Dec 4, 2023
1 parent 1a0e0d6 commit d66a2ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/neon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ features = ["sync"]
optional = true

[features]
default = ["napi-1"]
default = ["napi-8"]

# Enable the creation of external binary buffers. This is disabled by default
# since these APIs fail at runtime in environments that enable the V8 memory
Expand Down
27 changes: 21 additions & 6 deletions crates/neon/src/sys/bindings/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,21 +437,36 @@ pub(crate) unsafe fn load(env: Env) -> Result<(), libloading::Error> {

// This never fail since `get_version` is in N-API Version 1 and the module will fail
// with `Error: Module did not self-register` if N-API does not exist.
let version = get_version(&host, env).expect("Failed to find N-API version");
let actual_version = get_version(&host, env).expect("Failed to find N-API version");

napi1::load(&host, version, 1);
let expected_version = match () {
_ if cfg!(feature = "napi-8") => 8,
_ if cfg!(feature = "napi-7") => 7,
_ if cfg!(feature = "napi-6") => 6,
_ if cfg!(feature = "napi-5") => 5,
_ if cfg!(feature = "napi-4") => 4,
_ if cfg!(feature = "napi-3") => 3,
_ if cfg!(feature = "napi-2") => 2,
_ => 1,
};

if actual_version < expected_version {
eprintln!("Minimum required Node-API version {expected_version}, found {actual_version}.\n\nSee the Node-API support matrix for more details: https://nodejs.org/api/n-api.html#node-api-version-matrix");
}

napi1::load(&host);

#[cfg(feature = "napi-4")]
napi4::load(&host, version, 4);
napi4::load(&host);

#[cfg(feature = "napi-5")]
napi5::load(&host, version, 5);
napi5::load(&host);

#[cfg(feature = "napi-6")]
napi6::load(&host, version, 6);
napi6::load(&host);

#[cfg(feature = "napi-8")]
napi8::load(&host, version, 8);
napi8::load(&host);

Ok(())
}
13 changes: 1 addition & 12 deletions crates/neon/src/sys/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,7 @@ macro_rules! generate {
}
};

pub(super) unsafe fn load(
host: &libloading::Library,
actual_napi_version: u32,
expected_napi_version: u32,
) {
assert!(
actual_napi_version >= expected_napi_version,
"Minimum required Node-API version {}, found {}.",
expected_napi_version,
actual_napi_version,
);

pub(super) unsafe fn load(host: &libloading::Library) {
let print_warn = |err| eprintln!("WARN: {}", err);

NAPI = Napi {
Expand Down

0 comments on commit d66a2ef

Please sign in to comment.