Merge pull request #283 from napi-rs/simplify-module-register

chore(napi): simplify module register, support node@10+
This commit is contained in:
LongYinan 2020-11-04 17:41:19 +08:00 committed by GitHub
commit f9ec712d57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 56 deletions

View file

@ -1,4 +1,4 @@
use std::convert::{From, TryFrom}; use std::convert::TryFrom;
use std::ffi::CString; use std::ffi::CString;
use std::ptr; use std::ptr;

View file

@ -1,4 +1,5 @@
use super::Value; use super::Value;
#[cfg(napi6)]
use crate::sys; use crate::sys;
use crate::{Error, Result, Status}; use crate::{Error, Result, Status};
use std::convert::TryFrom; use std::convert::TryFrom;

View file

@ -157,61 +157,42 @@ macro_rules! register_module {
} }
} }
extern "C" fn register_module() { use std::ffi::CString;
use std::ffi::CString; use std::io::Write;
use std::io::Write; use std::os::raw::c_char;
use std::os::raw::c_char; use std::ptr;
use std::ptr; use $crate::{sys, Env, JsObject, NapiValue};
use $crate::{sys, Env, JsObject, Module, NapiValue};
#[cfg(all(feature = "tokio_rt", napi4))]
use $crate::shutdown_tokio_rt;
#[no_mangle]
unsafe extern "C" fn napi_register_module_v1(
raw_env: sys::napi_env,
raw_exports: sys::napi_value,
) -> sys::napi_value {
let env = Env::from_raw(raw_env);
let mut exports: JsObject = JsObject::from_raw_unchecked(raw_env, raw_exports);
let mut cjs_module = Module { env, exports };
let result = $init(&mut cjs_module);
#[cfg(all(feature = "tokio_rt", napi4))] #[cfg(all(feature = "tokio_rt", napi4))]
use $crate::shutdown_tokio_rt; let hook_result = check_status(unsafe {
static mut MODULE_DESCRIPTOR: Option<sys::napi_module> = None; sys::napi_add_env_cleanup_hook(raw_env, Some(shutdown_tokio_rt), ptr::null_mut())
unsafe { });
MODULE_DESCRIPTOR = Some(sys::napi_module { #[cfg(not(all(feature = "tokio_rt", napi4)))]
nm_version: 1, let hook_result = Ok(());
nm_flags: 0, match hook_result.and_then(move |_| result) {
nm_filename: concat!(file!(), "\0").as_ptr() as *const c_char, Ok(_) => cjs_module.exports.raw(),
nm_register_func: Some(napi_register_module_v1), Err(e) => {
nm_modname: concat!(stringify!($module_name), "\0").as_ptr() as *const c_char, unsafe {
nm_priv: 0 as *mut _, sys::napi_throw_error(
reserved: [0 as *mut _; 4], raw_env,
}); ptr::null(),
CString::from_vec_unchecked(format!("Error initializing module: {}", e).into())
sys::napi_module_register(MODULE_DESCRIPTOR.as_mut().unwrap() as *mut sys::napi_module); .as_ptr() as *const _,
} )
};
#[no_mangle] ptr::null_mut()
pub unsafe extern "C" fn napi_register_module_v1(
raw_env: sys::napi_env,
raw_exports: sys::napi_value,
) -> sys::napi_value {
let env = Env::from_raw(raw_env);
let mut exports: JsObject = JsObject::from_raw_unchecked(raw_env, raw_exports);
let mut cjs_module = Module { env, exports };
let result = $init(&mut cjs_module);
#[cfg(all(feature = "tokio_rt", napi4))]
let hook_result = check_status(unsafe {
sys::napi_add_env_cleanup_hook(raw_env, Some(shutdown_tokio_rt), ptr::null_mut())
});
#[cfg(not(all(feature = "tokio_rt", napi4)))]
let hook_result = Ok(());
match hook_result.and_then(move |_| result) {
Ok(_) => cjs_module.exports.raw(),
Err(e) => {
unsafe {
sys::napi_throw_error(
raw_env,
ptr::null(),
CString::from_vec_unchecked(format!("Error initializing module: {}", e).into())
.as_ptr() as *const _,
)
};
ptr::null_mut()
}
} }
} }
} }

View file

@ -6,7 +6,7 @@
"napi": "scripts/index.js" "napi": "scripts/index.js"
}, },
"engines": { "engines": {
"node": ">= 10.15" "node": ">= 10"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View file

@ -20,7 +20,7 @@
"suppressExcessPropertyErrors": true, "suppressExcessPropertyErrors": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"preserveSymlinks": true, "preserveSymlinks": true,
"target": "ES2018", "target": "ES2015",
"sourceMap": true, "sourceMap": true,
"esModuleInterop": true, "esModuleInterop": true,
"stripInternal": true, "stripInternal": true,