Merge pull request #1169 from napi-rs/setup-sys

fix(napi): load Node-API symbols manually on Windows
This commit is contained in:
LongYinan 2022-05-07 20:21:47 +08:00 committed by GitHub
commit 700f78fbf6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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,
exports: sys::napi_value,
) -> sys::napi_value {
#[cfg(windows)]
unsafe {
sys::setup();
}
let lock = MODULE_REGISTER_LOCK
.lock()
.expect("Failed to acquire module register lock");

View file

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

View file

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

View file

@ -81,7 +81,10 @@ static SETUP: Once = Once::new();
/// they will panic.
/// Safety: `env` must be a valid `napi_env` for the current thread
#[cfg(windows)]
#[ctor::ctor]
unsafe fn setup() {
SETUP.call_once(|| load().expect("Failed to load N-API symbols"));
pub unsafe fn setup() {
SETUP.call_once(|| {
if let Err(err) = load() {
panic!("{}", err);
}
});
}