fix(napi): load Node-API symbols manually on Windows

This commit is contained in:
LongYinan 2022-05-07 18:54:37 +08:00
parent daf5f1f9e9
commit e551bd7c1a
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
4 changed files with 18 additions and 11 deletions

View file

@ -262,6 +262,10 @@ unsafe extern "C" fn napi_register_module_v1(
env: sys::napi_env, env: sys::napi_env,
exports: sys::napi_value, exports: sys::napi_value,
) -> sys::napi_value { ) -> sys::napi_value {
#[cfg(windows)]
unsafe {
sys::setup();
}
let lock = MODULE_REGISTER_LOCK let lock = MODULE_REGISTER_LOCK
.lock() .lock()
.expect("Failed to acquire module register lock"); .expect("Failed to acquire module register lock");

View file

@ -2,7 +2,7 @@
authors = ["LongYinan <lynweklm@gmail.com>"] authors = ["LongYinan <lynweklm@gmail.com>"]
description = "NodeJS N-API raw binding" description = "NodeJS N-API raw binding"
edition = "2021" edition = "2021"
include = ["src/**/*", "Cargo.toml", "build.rs", ".node-headers/**/*"] include = ["src/**/*", "Cargo.toml"]
keywords = ["NodeJS", "FFI", "NAPI", "n-api"] keywords = ["NodeJS", "FFI", "NAPI", "n-api"]
license = "MIT" license = "MIT"
name = "napi-sys" name = "napi-sys"
@ -26,6 +26,3 @@ independent = true
[target.'cfg(windows)'.dependencies.libloading] [target.'cfg(windows)'.dependencies.libloading]
version = "0.7" version = "0.7"
[target.'cfg(windows)'.dependencies.ctor]
version = "0.1"

View file

@ -739,10 +739,13 @@ pub use napi8::*;
#[cfg(windows)] #[cfg(windows)]
pub(super) unsafe fn load() -> Result<(), libloading::Error> { pub(super) unsafe fn load() -> Result<(), libloading::Error> {
#[cfg(not(windows))] let host = match libloading::os::windows::Library::this() {
let host = libloading::os::unix::Library::this().into(); Ok(lib) => lib.into(),
#[cfg(windows)] Err(err) => {
let host = libloading::os::windows::Library::this()?.into(); eprintln!("Initialize libloading failed {}", err);
return Err(err);
}
};
napi1::load(&host)?; napi1::load(&host)?;
#[cfg(feature = "napi2")] #[cfg(feature = "napi2")]

View file

@ -81,7 +81,10 @@ static SETUP: Once = Once::new();
/// 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(windows)]
#[ctor::ctor] pub unsafe fn setup() {
unsafe fn setup() { SETUP.call_once(|| {
SETUP.call_once(|| load().expect("Failed to load N-API symbols")); if let Err(err) = load() {
panic!("{}", err);
}
});
} }