feat: move napi version features setup to build package
This commit is contained in:
parent
4f81f4d44e
commit
0d5f03f845
16 changed files with 1421 additions and 467 deletions
69
.github/workflows/napi4.yaml
vendored
Normal file
69
.github/workflows/napi4.yaml
vendored
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
name: Linux-NAPI4
|
||||||
|
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build_and_test:
|
||||||
|
name: stable - x86_64-unknown-linux-gnu - node@12.10
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Setup node
|
||||||
|
uses: actions/setup-node@v1
|
||||||
|
with:
|
||||||
|
node-version: 12.10
|
||||||
|
|
||||||
|
- name: Install stable
|
||||||
|
uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable-x86_64-unknown-linux-gnu
|
||||||
|
profile: minimal
|
||||||
|
override: true
|
||||||
|
|
||||||
|
- name: Generate Cargo.lock
|
||||||
|
uses: actions-rs/cargo@v1
|
||||||
|
with:
|
||||||
|
command: generate-lockfile
|
||||||
|
- name: Cache cargo registry
|
||||||
|
uses: actions/cache@v1
|
||||||
|
with:
|
||||||
|
path: ~/.cargo/registry
|
||||||
|
key: stable-x86_64-unknown-linux-gnu-cargo-registry-trimmed-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
- name: Cache cargo index
|
||||||
|
uses: actions/cache@v1
|
||||||
|
with:
|
||||||
|
path: ~/.cargo/git
|
||||||
|
key: stable-x86_64-unknown-linux-gnu-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
- name: Cache cargo build
|
||||||
|
uses: actions/cache@v1
|
||||||
|
with:
|
||||||
|
path: target
|
||||||
|
key: stable-x86_64-unknown-linux-gnu-cargo-build-trimmed-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
|
||||||
|
- name: Check build
|
||||||
|
uses: actions-rs/cargo@v1
|
||||||
|
with:
|
||||||
|
command: check
|
||||||
|
args: --all --bins --examples --tests -vvv
|
||||||
|
|
||||||
|
- name: Tests
|
||||||
|
uses: actions-rs/cargo@v1
|
||||||
|
timeout-minutes: 5
|
||||||
|
with:
|
||||||
|
command: test
|
||||||
|
args: -p napi-rs --lib -- --nocapture
|
||||||
|
|
||||||
|
- name: Unit tests
|
||||||
|
run: |
|
||||||
|
yarn
|
||||||
|
yarn --cwd ./test_module build
|
||||||
|
yarn test
|
||||||
|
env:
|
||||||
|
RUST_BACKTRACE: 1
|
||||||
|
|
||||||
|
- name: Clear the cargo caches
|
||||||
|
run: |
|
||||||
|
cargo install cargo-cache --no-default-features --features ci-autoclean
|
||||||
|
cargo-cache
|
|
@ -2,6 +2,8 @@ extern crate cfg_if;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
extern crate reqwest;
|
extern crate reqwest;
|
||||||
|
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
|
|
||||||
cfg_if! {
|
cfg_if! {
|
||||||
|
@ -10,7 +12,6 @@ cfg_if! {
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::copy;
|
use std::io::copy;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
pub fn setup() {
|
pub fn setup() {
|
||||||
let node_full_version =
|
let node_full_version =
|
||||||
|
@ -54,14 +55,43 @@ cfg_if! {
|
||||||
println!("cargo:rustc-cdylib-link-arg=delayimp.lib");
|
println!("cargo:rustc-cdylib-link-arg=delayimp.lib");
|
||||||
println!("cargo:rustc-cdylib-link-arg=/DELAYLOAD:node.exe");
|
println!("cargo:rustc-cdylib-link-arg=/DELAYLOAD:node.exe");
|
||||||
}
|
}
|
||||||
|
setup_napi_feature();
|
||||||
}
|
}
|
||||||
} else if #[cfg(target_os = "macos")] {
|
} else if #[cfg(target_os = "macos")] {
|
||||||
/// Set up the build environment by setting Cargo configuration variables.
|
/// Set up the build environment by setting Cargo configuration variables.
|
||||||
pub fn setup() {
|
pub fn setup() {
|
||||||
println!("cargo:rustc-cdylib-link-arg=-undefined");
|
println!("cargo:rustc-cdylib-link-arg=-undefined");
|
||||||
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
|
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
|
||||||
|
setup_napi_feature();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pub fn setup() { }
|
pub fn setup() {
|
||||||
|
setup_napi_feature();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup_napi_feature() {
|
||||||
|
let napi_version = String::from_utf8(
|
||||||
|
Command::new("node")
|
||||||
|
.args(&["-e", "console.log(process.versions.napi)"])
|
||||||
|
.output()
|
||||||
|
.unwrap()
|
||||||
|
.stdout,
|
||||||
|
)
|
||||||
|
.expect("Get NAPI version failed");
|
||||||
|
|
||||||
|
let napi_version_number = napi_version.trim().parse::<u32>().unwrap();
|
||||||
|
|
||||||
|
if napi_version_number < 4 {
|
||||||
|
panic!("current napi version is too low");
|
||||||
|
}
|
||||||
|
|
||||||
|
if napi_version_number == 4 {
|
||||||
|
println!("cargo:rustc-cfg=napi{}", napi_version_number);
|
||||||
|
} else {
|
||||||
|
for version in 4..napi_version_number {
|
||||||
|
println!("cargo:rustc-cfg=napi{}", version);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,12 +20,3 @@ glob = "0.3"
|
||||||
napi-build = { version = "0.1", path = "../build" }
|
napi-build = { version = "0.1", path = "../build" }
|
||||||
regex = "1.3"
|
regex = "1.3"
|
||||||
semver = "0.10"
|
semver = "0.10"
|
||||||
|
|
||||||
[features]
|
|
||||||
default = []
|
|
||||||
# See the N-API Version Matrix https://nodejs.org/api/n-api.html#n_api_n_api_version_matrix
|
|
||||||
napi2 = []
|
|
||||||
napi3 = []
|
|
||||||
napi4 = []
|
|
||||||
napi5 = []
|
|
||||||
napi6 = []
|
|
||||||
|
|
|
@ -57,19 +57,6 @@ fn main() {
|
||||||
.expect("Unable to generate napi bindings")
|
.expect("Unable to generate napi bindings")
|
||||||
.write_to_file(out_path.join("bindings.rs"))
|
.write_to_file(out_path.join("bindings.rs"))
|
||||||
.expect("Unable to write napi bindings");
|
.expect("Unable to write napi bindings");
|
||||||
|
|
||||||
let napi_version = String::from_utf8(
|
|
||||||
Command::new("node")
|
|
||||||
.args(&["-e", "console.log(process.versions.napi)"])
|
|
||||||
.output()
|
|
||||||
.unwrap()
|
|
||||||
.stdout,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
for version in 2..napi_version.trim().parse::<u32>().unwrap() {
|
|
||||||
println!("cargo:rustc-cfg=napi{}", version);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
|
|
|
@ -132,7 +132,9 @@ macro_rules! impl_js_value_methods {
|
||||||
value_type: ValueType::Object,
|
value_type: ValueType::Object,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[cfg(napi5)]
|
||||||
pub fn is_date(&self) -> Result<bool> {
|
pub fn is_date(&self) -> Result<bool> {
|
||||||
let mut is_date = true;
|
let mut is_date = true;
|
||||||
let status = unsafe { sys::napi_is_date(self.0.env, self.0.value, &mut is_date) };
|
let status = unsafe { sys::napi_is_date(self.0.env, self.0.value, &mut is_date) };
|
||||||
|
|
|
@ -4,9 +4,9 @@ mod env;
|
||||||
mod error;
|
mod error;
|
||||||
mod js_values;
|
mod js_values;
|
||||||
mod module;
|
mod module;
|
||||||
|
mod status;
|
||||||
pub mod sys;
|
pub mod sys;
|
||||||
mod task;
|
mod task;
|
||||||
#[cfg(napi4)]
|
|
||||||
pub mod threadsafe_function;
|
pub mod threadsafe_function;
|
||||||
mod version;
|
mod version;
|
||||||
|
|
||||||
|
@ -16,7 +16,8 @@ pub use env::*;
|
||||||
pub use error::{Error, Result};
|
pub use error::{Error, Result};
|
||||||
pub use js_values::*;
|
pub use js_values::*;
|
||||||
pub use module::Module;
|
pub use module::Module;
|
||||||
pub use sys::{napi_valuetype, Status};
|
pub use status::Status;
|
||||||
|
pub use sys::napi_valuetype;
|
||||||
pub use task::Task;
|
pub use task::Task;
|
||||||
pub use version::NodeVersion;
|
pub use version::NodeVersion;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use super::napi_status;
|
use crate::sys::napi_status;
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
|
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
|
||||||
pub enum Status {
|
pub enum Status {
|
||||||
|
@ -19,6 +19,7 @@ pub enum Status {
|
||||||
CallbackScopeMismatch,
|
CallbackScopeMismatch,
|
||||||
QueueFull,
|
QueueFull,
|
||||||
Closing,
|
Closing,
|
||||||
|
#[cfg(node6)]
|
||||||
BigintExpected,
|
BigintExpected,
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
@ -46,6 +47,7 @@ impl From<napi_status> for Status {
|
||||||
napi_callback_scope_mismatch => CallbackScopeMismatch,
|
napi_callback_scope_mismatch => CallbackScopeMismatch,
|
||||||
napi_queue_full => QueueFull,
|
napi_queue_full => QueueFull,
|
||||||
napi_closing => Closing,
|
napi_closing => Closing,
|
||||||
|
#[cfg(node6)]
|
||||||
napi_bigint_expected => BigintExpected,
|
napi_bigint_expected => BigintExpected,
|
||||||
_ => Unknown,
|
_ => Unknown,
|
||||||
}
|
}
|
||||||
|
@ -72,6 +74,7 @@ impl Into<self::napi_status> for Status {
|
||||||
Self::CallbackScopeMismatch => napi_status::napi_callback_scope_mismatch,
|
Self::CallbackScopeMismatch => napi_status::napi_callback_scope_mismatch,
|
||||||
Self::QueueFull => napi_status::napi_queue_full,
|
Self::QueueFull => napi_status::napi_queue_full,
|
||||||
Self::Closing => napi_status::napi_closing,
|
Self::Closing => napi_status::napi_closing,
|
||||||
|
#[cfg(node6)]
|
||||||
Self::BigintExpected => napi_status::napi_bigint_expected,
|
Self::BigintExpected => napi_status::napi_bigint_expected,
|
||||||
Self::Unknown => napi_status::napi_generic_failure,
|
Self::Unknown => napi_status::napi_generic_failure,
|
||||||
}
|
}
|
|
@ -4,6 +4,3 @@
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||||
|
|
||||||
mod stable;
|
|
||||||
pub use self::stable::Status;
|
|
||||||
|
|
|
@ -58,7 +58,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^14.0.14",
|
"@types/node": "^14.0.14",
|
||||||
"ava": "^3.9.0",
|
"ava": "2",
|
||||||
"husky": "^4.2.5",
|
"husky": "^4.2.5",
|
||||||
"lint-staged": "^10.2.11",
|
"lint-staged": "^10.2.11",
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
const test = require('ava')
|
|
||||||
|
|
||||||
const bindings = require('../index.node')
|
|
||||||
|
|
||||||
test('should return false if value is not date', (t) => {
|
|
||||||
t.false(bindings.testObjectIsDate({}))
|
|
||||||
t.false(bindings.testObjectIsDate(null))
|
|
||||||
t.false(bindings.testObjectIsDate())
|
|
||||||
t.false(bindings.testObjectIsDate(10249892))
|
|
||||||
})
|
|
||||||
|
|
||||||
test('should return true if value is date', (t) => {
|
|
||||||
t.true(bindings.testObjectIsDate(new Date()))
|
|
||||||
})
|
|
3
test_module/__test__/napi-version.js
Normal file
3
test_module/__test__/napi-version.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
const napi = parseInt(process.versions.napi || '1', 10)
|
||||||
|
|
||||||
|
module.exports = napi
|
23
test_module/__test__/napi5/is-date.spec.js
Normal file
23
test_module/__test__/napi5/is-date.spec.js
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
const test = require('ava')
|
||||||
|
|
||||||
|
const napiVersion = require('../napi-version')
|
||||||
|
const bindings = require('../../index.node')
|
||||||
|
|
||||||
|
test('should return false if value is not date', (t) => {
|
||||||
|
if (napiVersion >= 5) {
|
||||||
|
t.false(bindings.testObjectIsDate({}))
|
||||||
|
t.false(bindings.testObjectIsDate(null))
|
||||||
|
t.false(bindings.testObjectIsDate())
|
||||||
|
t.false(bindings.testObjectIsDate(10249892))
|
||||||
|
} else {
|
||||||
|
t.is(bindings.testObjectIsDate, undefined)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test('should return true if value is date', (t) => {
|
||||||
|
if (napiVersion >= 5) {
|
||||||
|
t.true(bindings.testObjectIsDate(new Date()))
|
||||||
|
} else {
|
||||||
|
t.is(bindings.testObjectIsDate, undefined)
|
||||||
|
}
|
||||||
|
})
|
|
@ -3,7 +3,10 @@ extern crate napi_rs as napi;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate napi_rs_derive;
|
extern crate napi_rs_derive;
|
||||||
|
|
||||||
use napi::{CallContext, Env, Error, JsBoolean, JsString, JsUnknown, Module, Result, Status};
|
use napi::{CallContext, Error, JsString, JsUnknown, Module, Result, Status};
|
||||||
|
|
||||||
|
#[cfg(napi5)]
|
||||||
|
mod napi5;
|
||||||
|
|
||||||
mod buffer;
|
mod buffer;
|
||||||
mod external;
|
mod external;
|
||||||
|
@ -13,6 +16,8 @@ mod tsfn;
|
||||||
|
|
||||||
use buffer::{buffer_to_string, get_buffer_length};
|
use buffer::{buffer_to_string, get_buffer_length};
|
||||||
use external::{create_external, get_external_count};
|
use external::{create_external, get_external_count};
|
||||||
|
#[cfg(napi5)]
|
||||||
|
use napi5::is_date::test_object_is_date;
|
||||||
use symbol::{create_named_symbol, create_symbol_from_js_string, create_unnamed_symbol};
|
use symbol::{create_named_symbol, create_symbol_from_js_string, create_unnamed_symbol};
|
||||||
use task::test_spawn_thread;
|
use task::test_spawn_thread;
|
||||||
use tsfn::{test_threadsafe_function, test_tokio_readfile, test_tsfn_error};
|
use tsfn::{test_threadsafe_function, test_tokio_readfile, test_tsfn_error};
|
||||||
|
@ -23,7 +28,6 @@ fn init(module: &mut Module) -> Result<()> {
|
||||||
module.create_named_method("testThrow", test_throw)?;
|
module.create_named_method("testThrow", test_throw)?;
|
||||||
module.create_named_method("testThrowWithReason", test_throw_with_reason)?;
|
module.create_named_method("testThrowWithReason", test_throw_with_reason)?;
|
||||||
module.create_named_method("testSpawnThread", test_spawn_thread)?;
|
module.create_named_method("testSpawnThread", test_spawn_thread)?;
|
||||||
module.create_named_method("testObjectIsDate", test_object_is_date)?;
|
|
||||||
module.create_named_method("createExternal", create_external)?;
|
module.create_named_method("createExternal", create_external)?;
|
||||||
module.create_named_method("getExternalCount", get_external_count)?;
|
module.create_named_method("getExternalCount", get_external_count)?;
|
||||||
module.create_named_method("getBufferLength", get_buffer_length)?;
|
module.create_named_method("getBufferLength", get_buffer_length)?;
|
||||||
|
@ -34,6 +38,8 @@ fn init(module: &mut Module) -> Result<()> {
|
||||||
module.create_named_method("testTsfnError", test_tsfn_error)?;
|
module.create_named_method("testTsfnError", test_tsfn_error)?;
|
||||||
module.create_named_method("testThreadsafeFunction", test_threadsafe_function)?;
|
module.create_named_method("testThreadsafeFunction", test_threadsafe_function)?;
|
||||||
module.create_named_method("testTokioReadfile", test_tokio_readfile)?;
|
module.create_named_method("testTokioReadfile", test_tokio_readfile)?;
|
||||||
|
#[cfg(napi5)]
|
||||||
|
module.create_named_method("testObjectIsDate", test_object_is_date)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,9 +56,3 @@ fn test_throw_with_reason(ctx: CallContext) -> Result<JsUnknown> {
|
||||||
reason.as_str()?.to_owned(),
|
reason.as_str()?.to_owned(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[js_function(1)]
|
|
||||||
fn test_object_is_date(ctx: CallContext) -> Result<JsBoolean> {
|
|
||||||
let obj = ctx.get::<JsUnknown>(0)?;
|
|
||||||
Env::get_boolean(ctx.env, obj.is_date()?)
|
|
||||||
}
|
|
||||||
|
|
7
test_module/src/napi5/is_date.rs
Normal file
7
test_module/src/napi5/is_date.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
use napi::{CallContext, JsBoolean, JsUnknown, Result};
|
||||||
|
|
||||||
|
#[js_function(1)]
|
||||||
|
pub fn test_object_is_date(ctx: CallContext) -> Result<JsBoolean> {
|
||||||
|
let obj = ctx.get::<JsUnknown>(0)?;
|
||||||
|
ctx.env.get_boolean(obj.is_date()?)
|
||||||
|
}
|
1
test_module/src/napi5/mod.rs
Normal file
1
test_module/src/napi5/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod is_date;
|
Loading…
Reference in a new issue