feat(napi-sys): support load Node-API symbols dynamically (#2014)
This commit is contained in:
parent
0550c56fcf
commit
f2e5094345
8 changed files with 74 additions and 68 deletions
2
.github/workflows/test-release.yaml
vendored
2
.github/workflows/test-release.yaml
vendored
|
@ -548,7 +548,7 @@ jobs:
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
bun run build
|
bun run build
|
||||||
bun run build:test
|
yarn workspace @examples/napi build --features dyn-symbols
|
||||||
- name: Test
|
- name: Test
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
run: bun run test:bun
|
run: bun run test:bun
|
||||||
|
|
|
@ -55,6 +55,7 @@ tokio_stats = ["tokio/stats"]
|
||||||
tokio_sync = ["tokio/sync"]
|
tokio_sync = ["tokio/sync"]
|
||||||
tokio_test_util = ["tokio/test-util"]
|
tokio_test_util = ["tokio/test-util"]
|
||||||
tokio_time = ["tokio/time"]
|
tokio_time = ["tokio/time"]
|
||||||
|
dyn-symbols = ["napi-sys/dyn-symbols"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bitflags = "2"
|
bitflags = "2"
|
||||||
|
|
|
@ -243,7 +243,7 @@ pub fn get_c_callback(raw_fn: ExportRegisterCallback) -> Result<crate::Callback>
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(windows, not(feature = "noop")))]
|
#[cfg(all(any(windows, feature = "dyn-symbols"), not(feature = "noop")))]
|
||||||
#[ctor::ctor]
|
#[ctor::ctor]
|
||||||
fn load_host() {
|
fn load_host() {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
|
@ -12,6 +12,7 @@ rust-version = "1.65"
|
||||||
version = "2.3.0"
|
version = "2.3.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
dyn-symbols = ["libloading"]
|
||||||
experimental = []
|
experimental = []
|
||||||
napi1 = []
|
napi1 = []
|
||||||
napi2 = ["napi1"]
|
napi2 = ["napi1"]
|
||||||
|
@ -26,5 +27,8 @@ napi9 = ["napi8"]
|
||||||
[package.metadata.workspaces]
|
[package.metadata.workspaces]
|
||||||
independent = true
|
independent = true
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies.libloading]
|
[dependencies]
|
||||||
version = "0.8"
|
libloading = { version = "0.8", optional = true }
|
||||||
|
|
||||||
|
[target.'cfg(windows)'.dependencies]
|
||||||
|
libloading = "0.8"
|
||||||
|
|
|
@ -780,15 +780,13 @@ pub use napi8::*;
|
||||||
#[cfg(feature = "napi9")]
|
#[cfg(feature = "napi9")]
|
||||||
pub use napi9::*;
|
pub use napi9::*;
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(any(windows, feature = "dyn-symbols"))]
|
||||||
pub(super) unsafe fn load_all() -> Result<libloading::Library, libloading::Error> {
|
pub(super) unsafe fn load_all() -> Result<libloading::Library, libloading::Error> {
|
||||||
let host = match libloading::os::windows::Library::this() {
|
#[cfg(windows)]
|
||||||
Ok(lib) => lib.into(),
|
let host = libloading::os::windows::Library::this()?.into();
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Initialize libloading failed {}", err);
|
#[cfg(unix)]
|
||||||
return Err(err);
|
let host = libloading::os::unix::Library::this().into();
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
napi1::load(&host)?;
|
napi1::load(&host)?;
|
||||||
#[cfg(feature = "napi2")]
|
#[cfg(feature = "napi2")]
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
// borrowed from https://github.com/neon-bindings/neon/tree/main/crates/neon/src/sys/bindings
|
||||||
|
|
||||||
#![allow(ambiguous_glob_reexports)]
|
#![allow(ambiguous_glob_reexports)]
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(any(windows, feature = "dyn-symbols"))]
|
||||||
macro_rules! generate {
|
macro_rules! generate {
|
||||||
(extern "C" {
|
(extern "C" {
|
||||||
$(fn $name:ident($($param:ident: $ptype:ty$(,)?)*)$( -> $rtype:ty)?;)+
|
$(fn $name:ident($($param:ident: $ptype:ty$(,)?)*)$( -> $rtype:ty)?;)+
|
||||||
|
@ -15,7 +17,7 @@ macro_rules! generate {
|
||||||
|
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
fn panic_load<T>() -> T {
|
fn panic_load<T>() -> T {
|
||||||
panic!("Must load N-API bindings")
|
panic!("Node-API symbol has not been loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
static mut NAPI: Napi = {
|
static mut NAPI: Napi = {
|
||||||
|
@ -43,11 +45,10 @@ macro_rules! generate {
|
||||||
match symbol {
|
match symbol {
|
||||||
Ok(f) => *f,
|
Ok(f) => *f,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
debug_assert!({
|
#[cfg(debug_assertions)] {
|
||||||
println!("Load Node-API [{}] from host runtime failed: {}", stringify!($name), e);
|
eprintln!("Load Node-API [{}] from host runtime failed: {}", stringify!($name), e);
|
||||||
true
|
}
|
||||||
});
|
NAPI.$name
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -67,7 +68,7 @@ macro_rules! generate {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(any(windows, feature = "dyn-symbols")))]
|
||||||
macro_rules! generate {
|
macro_rules! generate {
|
||||||
(extern "C" {
|
(extern "C" {
|
||||||
$(fn $name:ident($($param:ident: $ptype:ty$(,)?)*)$( -> $rtype:ty)?;)+
|
$(fn $name:ident($($param:ident: $ptype:ty$(,)?)*)$( -> $rtype:ty)?;)+
|
||||||
|
@ -90,7 +91,7 @@ pub use types::*;
|
||||||
/// Must be called at least once before using any functions in bindings or
|
/// Must be called at least once before using any functions in bindings or
|
||||||
/// they will panic.
|
/// they will panic.
|
||||||
/// Safety: `env` must be a valid `napi_env` for the current thread
|
/// Safety: `env` must be a valid `napi_env` for the current thread
|
||||||
#[cfg(windows)]
|
#[cfg(any(windows, feature = "dyn-symbols"))]
|
||||||
#[allow(clippy::missing_safety_doc)]
|
#[allow(clippy::missing_safety_doc)]
|
||||||
pub unsafe fn setup() -> libloading::Library {
|
pub unsafe fn setup() -> libloading::Library {
|
||||||
match load_all() {
|
match load_all() {
|
||||||
|
|
|
@ -11,6 +11,7 @@ crate-type = ["cdylib"]
|
||||||
[features]
|
[features]
|
||||||
latest = ["napi/napi9"]
|
latest = ["napi/napi9"]
|
||||||
napi3 = ["napi/napi3"]
|
napi3 = ["napi/napi3"]
|
||||||
|
dyn-symbols = ["napi/dyn-symbols"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
|
|
|
@ -10,6 +10,7 @@ crate-type = ["cdylib"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
snmalloc = ["snmalloc-rs"]
|
snmalloc = ["snmalloc-rs"]
|
||||||
|
dyn-symbols = ["napi/dyn-symbols"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
|
|
Loading…
Reference in a new issue