diff --git a/bench/src/async_compute.rs b/bench/src/async_compute.rs index 2404ce87..87300303 100644 --- a/bench/src/async_compute.rs +++ b/bench/src/async_compute.rs @@ -1,8 +1,6 @@ -use napi::{ - CallContext, Env, JsBuffer, JsBufferValue, JsNumber, JsObject, Module, Ref, Result, Task, -}; +use napi::{CallContext, Env, JsBuffer, JsBufferValue, JsNumber, JsObject, Ref, Result, Task}; + -#[repr(transparent)] struct BufferLength(Ref); impl Task for BufferLength { @@ -27,7 +25,7 @@ fn bench_async_task(ctx: CallContext) -> Result { Ok(async_promise.promise_object()) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("benchAsyncTask", bench_async_task)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("benchAsyncTask", bench_async_task)?; Ok(()) } diff --git a/bench/src/lib.rs b/bench/src/lib.rs index b1d9c318..653c7f5c 100644 --- a/bench/src/lib.rs +++ b/bench/src/lib.rs @@ -1,21 +1,18 @@ #[macro_use] -extern crate napi; -#[macro_use] extern crate napi_derive; -use napi::{Module, Result}; +use napi::{JsObject, Result}; mod async_compute; mod noop; mod plus; -register_module!(bench, init); +#[module_exports] +fn init(mut exports: JsObject) -> Result<()> { + exports.create_named_method("noop", noop::noop)?; -fn init(module: &mut Module) -> Result<()> { - module.create_named_method("noop", noop::noop)?; - - async_compute::register_js(module)?; - plus::register_js(module)?; + async_compute::register_js(&mut exports)?; + plus::register_js(&mut exports)?; Ok(()) } diff --git a/bench/src/plus.rs b/bench/src/plus.rs index 5954630f..1ca91978 100644 --- a/bench/src/plus.rs +++ b/bench/src/plus.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; -use napi::{CallContext, JsNumber, Module, Result}; +use napi::{CallContext, JsNumber, JsObject, Result}; #[js_function(2)] fn bench_plus(ctx: CallContext) -> Result { @@ -9,7 +9,7 @@ fn bench_plus(ctx: CallContext) -> Result { ctx.env.create_uint32(a + b) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("plus", bench_plus)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("plus", bench_plus)?; Ok(()) } diff --git a/build/src/windows.rs b/build/src/windows.rs index 830def4b..202a8793 100644 --- a/build/src/windows.rs +++ b/build/src/windows.rs @@ -1,4 +1,5 @@ use std::collections::hash_map::DefaultHasher; +use std::env; use std::fs::{metadata, write}; use std::hash::{Hash, Hasher}; use std::io::Read; @@ -35,14 +36,14 @@ fn download_node_lib(dist_url: &str, version: &str, arch: &str) -> Vec { } pub fn setup() { - let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR is not set"); + let out_dir = env::var("OUT_DIR").expect("OUT_DIR is not set"); // Assume nodejs if not specified. let dist_url = - std::env::var("NPM_CONFIG_DISTURL").unwrap_or_else(|_| "https://nodejs.org/dist".to_string()); + env::var("NPM_CONFIG_DISTURL").unwrap_or_else(|_| "https://nodejs.org/dist".to_string()); // Try to get local nodejs version if not specified. - let node_version = std::env::var("NPM_CONFIG_TARGET") + let node_version = env::var("NPM_CONFIG_TARGET") .or_else(|_| get_node_version()) .expect("Failed to determine nodejs version"); @@ -53,7 +54,7 @@ pub fn setup() { // See https://nodejs.org/docs/latest/api/process.html#process_process_arch for npm env values. // For windows, we only support `['ia32', 'x64', 'arm64']` // https://github.com/nodejs/node-gyp/blob/master/lib/install.js#L301 - let arch = std::env::var("CARGO_CFG_TARGET_ARCH") + let arch = env::var("CARGO_CFG_TARGET_ARCH") .map(|arch| match arch.as_str() { "x86" => "x86", "x86_64" => "x64", diff --git a/napi-derive-example/src/lib.rs b/napi-derive-example/src/lib.rs index 739eb0d3..35034288 100644 --- a/napi-derive-example/src/lib.rs +++ b/napi-derive-example/src/lib.rs @@ -1,17 +1,14 @@ #[macro_use] -extern crate napi; -#[macro_use] extern crate napi_derive; -use napi::{CallContext, Error, JsNumber, JsUnknown, Module, Result, Status}; +use napi::{CallContext, Error, JsNumber, JsObject, JsUnknown, Result, Status}; use std::convert::TryInto; -register_module!(napi_derive_example, init); +#[module_exports] +fn init(mut exports: JsObject) -> Result<()> { + exports.create_named_method("testThrow", test_throw)?; -fn init(module: &mut Module) -> Result<()> { - module.create_named_method("testThrow", test_throw)?; - - module.create_named_method("fibonacci", fibonacci)?; + exports.create_named_method("fibonacci", fibonacci)?; Ok(()) } diff --git a/napi-derive/src/lib.rs b/napi-derive/src/lib.rs index 1928a188..f7b8760c 100644 --- a/napi-derive/src/lib.rs +++ b/napi-derive/src/lib.rs @@ -1,7 +1,5 @@ extern crate proc_macro; -use std::mem; - use proc_macro::TokenStream; use proc_macro2::{Ident, Literal}; use quote::{format_ident, quote}; @@ -28,8 +26,9 @@ impl Parse for ArgLength { struct JsFunction { args: Vec, - name: mem::MaybeUninit, - signature: mem::MaybeUninit, + name: Option, + signature: Option, + signature_raw: Option, block: Vec, visibility: Visibility, } @@ -38,8 +37,9 @@ impl JsFunction { pub fn new() -> Self { JsFunction { args: vec![], - name: mem::MaybeUninit::uninit(), - signature: mem::MaybeUninit::uninit(), + name: None, + signature: None, + signature_raw: None, visibility: Visibility::Inherited, block: vec![], } @@ -53,10 +53,11 @@ impl Fold for JsFunction { } fn fold_signature(&mut self, signature: Signature) -> Signature { - self.name = mem::MaybeUninit::new(format_ident!("{}", signature.ident)); + self.name = Some(format_ident!("{}", signature.ident)); let mut new_signature = signature.clone(); - new_signature.ident = format_ident!("_{}", signature.ident); - self.signature = mem::MaybeUninit::new(new_signature); + new_signature.ident = format_ident!("_generated_{}_generated_", signature.ident); + self.signature = Some(new_signature); + self.signature_raw = Some(signature.clone()); fold_signature(self, signature) } @@ -78,39 +79,32 @@ pub fn js_function(attr: TokenStream, input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as ItemFn); let mut js_fn = JsFunction::new(); js_fn.fold_item_fn(input); - let fn_name = unsafe { js_fn.name.assume_init() }; + let fn_name = js_fn.name.unwrap(); let fn_block = js_fn.block; - let signature = unsafe { js_fn.signature.assume_init() }; + let signature = js_fn.signature.unwrap(); let visibility = js_fn.visibility; let new_fn_name = signature.ident.clone(); let execute_js_function = get_execute_js_code(new_fn_name, FunctionKind::JsFunction); let expanded = quote! { - #[inline(always)] #signature #(#fn_block)* - #[inline(always)] #visibility extern "C" fn #fn_name( raw_env: napi::sys::napi_env, cb_info: napi::sys::napi_callback_info, ) -> napi::sys::napi_value { - use std::io::Write; - use std::mem; - use std::os::raw::c_char; use std::ptr; use std::panic::{self, AssertUnwindSafe}; use std::ffi::CString; - use napi::{JsUnknown, Env, Error, Status, NapiValue, CallContext}; + use napi::{Env, Error, Status, NapiValue, CallContext}; let mut argc = #arg_len_span as usize; let mut raw_args: [napi::sys::napi_value; #arg_len_span] = [ptr::null_mut(); #arg_len_span]; let mut raw_this = ptr::null_mut(); - let mut has_error = false; - unsafe { let status = napi::sys::napi_get_cb_info( raw_env, cb_info, - &mut argc as *mut usize as *mut _, + &mut argc, raw_args.as_mut_ptr(), &mut raw_this, ptr::null_mut(), @@ -119,7 +113,7 @@ pub fn js_function(attr: TokenStream, input: TokenStream) -> TokenStream { } let mut env = unsafe { Env::from_raw(raw_env) }; - let ctx = CallContext::new(&mut env, cb_info, raw_this, &raw_args, #arg_len_span, argc as usize); + let ctx = CallContext::new(&mut env, cb_info, raw_this, &raw_args, #arg_len_span, argc); #execute_js_function } }; @@ -132,29 +126,24 @@ pub fn contextless_function(_attr: TokenStream, input: TokenStream) -> TokenStre let input = parse_macro_input!(input as ItemFn); let mut js_fn = JsFunction::new(); js_fn.fold_item_fn(input); - let fn_name = unsafe { js_fn.name.assume_init() }; + let fn_name = js_fn.name.unwrap(); let fn_block = js_fn.block; - let signature = unsafe { js_fn.signature.assume_init() }; + let signature = js_fn.signature.unwrap(); let visibility = js_fn.visibility; let new_fn_name = signature.ident.clone(); let execute_js_function = get_execute_js_code(new_fn_name, FunctionKind::Contextless); let expanded = quote! { - #[inline] #signature #(#fn_block)* #visibility extern "C" fn #fn_name( raw_env: napi::sys::napi_env, cb_info: napi::sys::napi_callback_info, ) -> napi::sys::napi_value { - use std::io::Write; - use std::mem; - use std::os::raw::c_char; use std::ptr; use std::panic::{self, AssertUnwindSafe}; use std::ffi::CString; use napi::{Env, NapiValue, Error, Status}; - let mut has_error = false; let ctx = unsafe { Env::from_raw(raw_env) }; #execute_js_function @@ -211,3 +200,63 @@ fn get_execute_js_code( } } } + +#[proc_macro_attribute] +pub fn module_exports(_attr: TokenStream, input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as ItemFn); + let mut js_fn = JsFunction::new(); + js_fn.fold_item_fn(input); + let fn_block = js_fn.block; + let fn_name = js_fn.name.unwrap(); + let signature = js_fn.signature_raw.unwrap(); + let args_len = js_fn.args.len(); + let call_expr = if args_len == 1 { + quote! { #fn_name(exports) } + } else if args_len == 2 { + quote! { #fn_name(exports, env) } + } else { + panic!("Arguments length of #[module_exports] function must be 1 or 2"); + }; + let expanded = quote! { + #[inline] + #signature #(#fn_block)* + + #[no_mangle] + unsafe extern "C" fn napi_register_module_v1( + raw_env: napi::sys::napi_env, + raw_exports: napi::sys::napi_value, + ) -> napi::sys::napi_value { + use std::ffi::CString; + use std::ptr; + use napi::{Env, JsObject, NapiValue}; + + #[cfg(all(feature = "tokio_rt", feature = "napi4"))] + use napi::shutdown_tokio_rt; + let env = Env::from_raw(raw_env); + let exports = JsObject::from_raw_unchecked(raw_env, raw_exports); + let result: napi::Result<()> = #call_expr; + #[cfg(all(feature = "tokio_rt", feature = "napi4"))] + let hook_result = napi::check_status!(unsafe { + napi::sys::napi_add_env_cleanup_hook(raw_env, Some(shutdown_tokio_rt), ptr::null_mut()) + }); + #[cfg(not(all(feature = "tokio_rt", feature = "napi4")))] + let hook_result = Ok(()); + match hook_result.and_then(move |_| result) { + Ok(_) => raw_exports, + Err(e) => { + unsafe { + napi::sys::napi_throw_error( + raw_env, + ptr::null(), + CString::from_vec_unchecked(format!("Error initializing module: {}", e).into()) + .as_ptr(), + ) + }; + ptr::null_mut() + } + } + } + }; + // Hand the output tokens back to the compiler + TokenStream::from(expanded) +} diff --git a/napi/src/async_work.rs b/napi/src/async_work.rs index 915bd434..a8aa8537 100644 --- a/napi/src/async_work.rs +++ b/napi/src/async_work.rs @@ -2,7 +2,7 @@ use std::mem; use std::os::raw::{c_char, c_void}; use std::ptr; -use crate::error::check_status; +use crate::check_status; use crate::js_values::NapiValue; use crate::{sys, Env, JsObject, Result, Task}; @@ -13,7 +13,6 @@ struct AsyncWork { napi_async_work: sys::napi_async_work, } -#[derive(Debug)] pub struct AsyncWorkPromise<'env> { napi_async_work: sys::napi_async_work, raw_promise: sys::napi_value, @@ -21,26 +20,27 @@ pub struct AsyncWorkPromise<'env> { } impl<'env> AsyncWorkPromise<'env> { - #[inline(always)] + #[inline] pub fn promise_object(&self) -> JsObject { unsafe { JsObject::from_raw_unchecked(self.env.0, self.raw_promise) } } + #[inline] pub fn cancel(self) -> Result<()> { - check_status(unsafe { sys::napi_cancel_async_work(self.env.0, self.napi_async_work) }) + check_status!(unsafe { sys::napi_cancel_async_work(self.env.0, self.napi_async_work) }) } } -#[inline(always)] +#[inline] pub fn run<'env, T: Task>(env: &'env Env, task: T) -> Result> { let mut raw_resource = ptr::null_mut(); - check_status(unsafe { sys::napi_create_object(env.0, &mut raw_resource) })?; + check_status!(unsafe { sys::napi_create_object(env.0, &mut raw_resource) })?; let mut raw_promise = ptr::null_mut(); let mut deferred = ptr::null_mut(); - check_status(unsafe { sys::napi_create_promise(env.0, &mut deferred, &mut raw_promise) })?; + check_status!(unsafe { sys::napi_create_promise(env.0, &mut deferred, &mut raw_promise) })?; let mut raw_name = ptr::null_mut(); let s = "napi_rs_async_work"; - check_status(unsafe { + check_status!(unsafe { sys::napi_create_string_utf8( env.0, s.as_ptr() as *const c_char, @@ -54,7 +54,7 @@ pub fn run<'env, T: Task>(env: &'env Env, task: T) -> Result(env: &'env Env, task: T) -> Result( let output = v.assume_init(); work.inner_task.resolve(Env::from_raw(env), output) }); - match check_status(status).and_then(move |_| value) { + match check_status!(status).and_then(move |_| value) { Ok(v) => { let status = sys::napi_resolve_deferred(env, deferred, v.raw()); debug_assert!(status == sys::Status::napi_ok, "Reject promise failed"); diff --git a/napi/src/call_context.rs b/napi/src/call_context.rs index 21c97c75..9c37ce32 100644 --- a/napi/src/call_context.rs +++ b/napi/src/call_context.rs @@ -1,6 +1,6 @@ use std::ptr; -use crate::error::check_status; +use crate::check_status; use crate::{sys, Either, Env, Error, JsUndefined, NapiValue, Result, Status}; /// Function call context @@ -15,6 +15,7 @@ pub struct CallContext<'env> { } impl<'env> CallContext<'env> { + #[inline] pub fn new( env: &'env mut Env, callback_info: sys::napi_callback_info, @@ -33,6 +34,7 @@ impl<'env> CallContext<'env> { } } + #[inline] pub fn get(&self, index: usize) -> Result { if index + 1 > self.arg_len { Err(Error { @@ -44,6 +46,7 @@ impl<'env> CallContext<'env> { } } + #[inline] pub fn try_get(&self, index: usize) -> Result> { if index + 1 > self.arg_len { Err(Error { @@ -59,21 +62,22 @@ impl<'env> CallContext<'env> { } } + #[inline] pub fn get_new_target(&self) -> Result where V: NapiValue, { let mut value = ptr::null_mut(); - check_status(unsafe { sys::napi_get_new_target(self.env.0, self.callback_info, &mut value) })?; + check_status!(unsafe { sys::napi_get_new_target(self.env.0, self.callback_info, &mut value) })?; unsafe { V::from_raw(self.env.0, value) } } - #[inline(always)] + #[inline] pub fn this(&self) -> Result { unsafe { T::from_raw(self.env.0, self.raw_this) } } - #[inline(always)] + #[inline] pub fn this_unchecked(&self) -> T { unsafe { T::from_raw_unchecked(self.env.0, self.raw_this) } } diff --git a/napi/src/cleanup_env.rs b/napi/src/cleanup_env.rs index 4670208f..37aac59e 100644 --- a/napi/src/cleanup_env.rs +++ b/napi/src/cleanup_env.rs @@ -3,6 +3,5 @@ pub(crate) struct CleanupEnvHookData { pub(crate) hook: Box ()>, } -#[repr(transparent)] -#[derive(Debug, Clone, Copy)] +#[derive(Clone, Copy)] pub struct CleanupEnvHook(pub(crate) *mut CleanupEnvHookData); diff --git a/napi/src/env.rs b/napi/src/env.rs index a3b625f8..5bfe8da1 100644 --- a/napi/src/env.rs +++ b/napi/src/env.rs @@ -6,7 +6,7 @@ use std::os::raw::{c_char, c_void}; use std::ptr; use crate::async_work::{self, AsyncWorkPromise}; -use crate::error::check_status; +use crate::check_status; use crate::js_values::*; use crate::task::Task; use crate::{sys, Error, NodeVersion, Result, Status}; @@ -32,104 +32,116 @@ use tokio::sync::mpsc::error::TrySendError; pub type Callback = extern "C" fn(sys::napi_env, sys::napi_callback_info) -> sys::napi_value; -#[repr(transparent)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy)] pub struct Env(pub(crate) sys::napi_env); impl Env { + #[inline] pub unsafe fn from_raw(env: sys::napi_env) -> Self { Env(env) } + #[inline] pub fn get_undefined(&self) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { sys::napi_get_undefined(self.0, &mut raw_value) })?; + check_status!(unsafe { sys::napi_get_undefined(self.0, &mut raw_value) })?; Ok(unsafe { JsUndefined::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn get_null(&self) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { sys::napi_get_null(self.0, &mut raw_value) })?; + check_status!(unsafe { sys::napi_get_null(self.0, &mut raw_value) })?; Ok(unsafe { JsNull::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn get_boolean(&self, value: bool) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { sys::napi_get_boolean(self.0, value, &mut raw_value) })?; + check_status!(unsafe { sys::napi_get_boolean(self.0, value, &mut raw_value) })?; Ok(unsafe { JsBoolean::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn create_int32(&self, int: i32) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_int32(self.0, int, (&mut raw_value) as *mut sys::napi_value) })?; Ok(unsafe { JsNumber::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn create_int64(&self, int: i64) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_int64(self.0, int, (&mut raw_value) as *mut sys::napi_value) })?; Ok(unsafe { JsNumber::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn create_uint32(&self, number: u32) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { sys::napi_create_uint32(self.0, number, &mut raw_value) })?; + check_status!(unsafe { sys::napi_create_uint32(self.0, number, &mut raw_value) })?; Ok(unsafe { JsNumber::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn create_double(&self, double: f64) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_double(self.0, double, (&mut raw_value) as *mut sys::napi_value) })?; Ok(unsafe { JsNumber::from_raw_unchecked(self.0, raw_value) }) } - #[cfg(feature = "napi6")] /// [n_api_napi_create_bigint_int64](https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_int64) + #[cfg(feature = "napi6")] + #[inline] pub fn create_bigint_from_i64(&self, value: i64) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { sys::napi_create_bigint_int64(self.0, value, &mut raw_value) })?; + check_status!(unsafe { sys::napi_create_bigint_int64(self.0, value, &mut raw_value) })?; Ok(JsBigint::from_raw_unchecked(self.0, raw_value, 1)) } #[cfg(feature = "napi6")] + #[inline] pub fn create_bigint_from_u64(&self, value: u64) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { sys::napi_create_bigint_uint64(self.0, value, &mut raw_value) })?; + check_status!(unsafe { sys::napi_create_bigint_uint64(self.0, value, &mut raw_value) })?; Ok(JsBigint::from_raw_unchecked(self.0, raw_value, 1)) } #[cfg(feature = "napi6")] + #[inline] pub fn create_bigint_from_i128(&self, value: i128) -> Result { let mut raw_value = ptr::null_mut(); let sign_bit = if value > 0 { 0 } else { 1 }; let words = &value as *const i128 as *const u64; - check_status(unsafe { + check_status!(unsafe { sys::napi_create_bigint_words(self.0, sign_bit, 2, words, &mut raw_value) })?; Ok(JsBigint::from_raw_unchecked(self.0, raw_value, 1)) } #[cfg(feature = "napi6")] + #[inline] pub fn create_bigint_from_u128(&self, value: u128) -> Result { let mut raw_value = ptr::null_mut(); let words = &value as *const u128 as *const u64; - check_status(unsafe { sys::napi_create_bigint_words(self.0, 0, 2, words, &mut raw_value) })?; + check_status!(unsafe { sys::napi_create_bigint_words(self.0, 0, 2, words, &mut raw_value) })?; Ok(JsBigint::from_raw_unchecked(self.0, raw_value, 1)) } - #[cfg(feature = "napi6")] /// [n_api_napi_create_bigint_words](https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_words) /// The resulting BigInt will be negative when sign_bit is true. + #[cfg(feature = "napi6")] + #[inline] pub fn create_bigint_from_words(&self, sign_bit: bool, words: Vec) -> Result { let mut raw_value = ptr::null_mut(); let len = words.len(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_bigint_words( self.0, match sign_bit { @@ -144,41 +156,48 @@ impl Env { Ok(JsBigint::from_raw_unchecked(self.0, raw_value, len as _)) } + #[inline] pub fn create_string(&self, s: &str) -> Result { self.create_string_from_chars(s.as_ptr() as *const c_char, s.len()) } + #[inline] pub fn create_string_from_std(&self, s: String) -> Result { self.create_string_from_chars(s.as_ptr() as *const c_char, s.len()) } + #[inline] pub fn create_string_from_vec_u8(&self, bytes: Vec) -> Result { self.create_string_from_chars(bytes.as_ptr() as *const c_char, bytes.len()) } + #[inline] pub fn create_string_from_vec_i8(&self, bytes: Vec) -> Result { self.create_string_from_chars(bytes.as_ptr() as *const c_char, bytes.len()) } + #[inline] fn create_string_from_chars(&self, data_ptr: *const c_char, len: usize) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_string_utf8(self.0, data_ptr, len as _, &mut raw_value) })?; Ok(unsafe { JsString::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn create_string_utf16(&self, chars: &[u16]) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_string_utf16(self.0, chars.as_ptr(), chars.len() as _, &mut raw_value) })?; Ok(unsafe { JsString::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn create_string_latin1(&self, chars: &[u8]) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_string_latin1( self.0, chars.as_ptr() as *const _, @@ -189,15 +208,17 @@ impl Env { Ok(unsafe { JsString::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn create_symbol_from_js_string(&self, description: JsString) -> Result { let mut result = ptr::null_mut(); - check_status(unsafe { sys::napi_create_symbol(self.0, description.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_create_symbol(self.0, description.0.value, &mut result) })?; Ok(unsafe { JsSymbol::from_raw_unchecked(self.0, result) }) } + #[inline] pub fn create_symbol(&self, description: Option<&str>) -> Result { let mut result = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_symbol( self.0, description @@ -210,31 +231,35 @@ impl Env { Ok(unsafe { JsSymbol::from_raw_unchecked(self.0, result) }) } + #[inline] pub fn create_object(&self) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { sys::napi_create_object(self.0, &mut raw_value) })?; + check_status!(unsafe { sys::napi_create_object(self.0, &mut raw_value) })?; Ok(unsafe { JsObject::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn create_array(&self) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { sys::napi_create_array(self.0, &mut raw_value) })?; + check_status!(unsafe { sys::napi_create_array(self.0, &mut raw_value) })?; Ok(unsafe { JsObject::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn create_array_with_length(&self, length: usize) -> Result { let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_array_with_length(self.0, length as _, &mut raw_value) })?; Ok(unsafe { JsObject::from_raw_unchecked(self.0, raw_value) }) } + #[inline] pub fn create_buffer(&self, length: usize) -> Result { let mut raw_value = ptr::null_mut(); let mut data: Vec = Vec::with_capacity(length); let mut data_ptr = data.as_mut_ptr() as *mut c_void; - check_status(unsafe { + check_status!(unsafe { sys::napi_create_buffer(self.0, length as _, &mut data_ptr, &mut raw_value) })?; @@ -248,11 +273,12 @@ impl Env { )) } + #[inline] pub fn create_buffer_with_data(&self, mut data: Vec) -> Result { let mut length = data.len(); let mut raw_value = ptr::null_mut(); let data_ptr = data.as_mut_ptr(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_external_buffer( self.0, length as _, @@ -263,7 +289,9 @@ impl Env { ) })?; let mut changed = 0; - check_status(unsafe { sys::napi_adjust_external_memory(self.0, length as i64, &mut changed) })?; + check_status!(unsafe { + sys::napi_adjust_external_memory(self.0, length as i64, &mut changed) + })?; Ok(JsBufferValue::new( JsBuffer(Value { env: self.0, @@ -274,6 +302,7 @@ impl Env { )) } + #[inline] pub fn create_buffer_copy(&self, data_to_copy: D) -> Result where D: AsRef<[u8]>, @@ -282,7 +311,7 @@ impl Env { let data_ptr = data_to_copy.as_ref().as_ptr(); let mut copy_data = ptr::null_mut(); let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_buffer_copy( self.0, length as _, @@ -301,11 +330,12 @@ impl Env { )) } + #[inline] pub fn create_arraybuffer(&self, length: usize) -> Result { let mut raw_value = ptr::null_mut(); let mut data: Vec = Vec::with_capacity(length as usize); let mut data_ptr = data.as_mut_ptr() as *mut c_void; - check_status(unsafe { + check_status!(unsafe { sys::napi_create_arraybuffer(self.0, length as _, &mut data_ptr, &mut raw_value) })?; @@ -315,11 +345,12 @@ impl Env { )) } + #[inline] pub fn create_arraybuffer_with_data(&self, data: Vec) -> Result { let mut length = data.len(); let mut raw_value = ptr::null_mut(); let data_ptr = data.as_ptr(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_external_arraybuffer( self.0, data_ptr as *mut c_void, @@ -330,7 +361,9 @@ impl Env { ) })?; let mut changed = 0; - check_status(unsafe { sys::napi_adjust_external_memory(self.0, length as i64, &mut changed) })?; + check_status!(unsafe { + sys::napi_adjust_external_memory(self.0, length as i64, &mut changed) + })?; Ok(JsArrayBufferValue::new( JsArrayBuffer(Value { @@ -342,11 +375,12 @@ impl Env { )) } + #[inline] pub fn create_function(&self, name: &str, callback: Callback) -> Result { let mut raw_result = ptr::null_mut(); + let len = name.len(); let name = CString::new(name)?; - let len = name.as_bytes().len(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_function( self.0, name.as_ptr(), @@ -360,16 +394,21 @@ impl Env { Ok(unsafe { JsFunction::from_raw_unchecked(self.0, raw_result) }) } + #[inline] pub fn throw(&self, error: Error) -> Result<()> { let err_value = self.create_error(error)?.0.value; - check_status(unsafe { sys::napi_throw(self.0, err_value) })?; + check_status!(unsafe { sys::napi_throw(self.0, err_value) })?; Ok(()) } + #[inline] pub fn throw_error(&self, msg: &str) -> Result<()> { - check_status(unsafe { sys::napi_throw_error(self.0, ptr::null(), CString::new(msg)?.as_ptr()) }) + check_status!(unsafe { + sys::napi_throw_error(self.0, ptr::null(), CString::new(msg)?.as_ptr()) + }) } + #[inline] pub fn define_class( &self, name: &str, @@ -382,7 +421,7 @@ impl Env { .map(|prop| prop.raw()) .collect::>(); - check_status(unsafe { + check_status!(unsafe { sys::napi_define_class( self.0, name.as_ptr() as *const c_char, @@ -398,8 +437,9 @@ impl Env { Ok(unsafe { JsFunction::from_raw_unchecked(self.0, raw_result) }) } + #[inline] pub fn wrap(&self, js_object: &mut JsObject, native_object: T) -> Result<()> { - check_status(unsafe { + check_status!(unsafe { sys::napi_wrap( self.0, js_object.0.value, @@ -411,10 +451,11 @@ impl Env { }) } + #[inline] pub fn unwrap(&self, js_object: &JsObject) -> Result<&mut T> { unsafe { let mut unknown_tagged_object: *mut c_void = ptr::null_mut(); - check_status(sys::napi_unwrap( + check_status!(sys::napi_unwrap( self.0, js_object.0.value, &mut unknown_tagged_object, @@ -436,10 +477,11 @@ impl Env { } } + #[inline] pub fn drop_wrapped(&self, js_object: JsObject) -> Result<()> { unsafe { let mut unknown_tagged_object: *mut c_void = ptr::null_mut(); - check_status(sys::napi_unwrap( + check_status!(sys::napi_unwrap( self.0, js_object.0.value, &mut unknown_tagged_object, @@ -460,9 +502,10 @@ impl Env { } } + #[inline] pub fn create_external(&self, native_object: T) -> Result { let mut object_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_external( self.0, Box::into_raw(Box::new(TaggedObject::new(native_object))) as *mut c_void, @@ -474,10 +517,11 @@ impl Env { Ok(unsafe { JsExternal::from_raw_unchecked(self.0, object_value) }) } + #[inline] pub fn get_value_external(&self, js_external: &JsExternal) -> Result<&mut T> { unsafe { let mut unknown_tagged_object = ptr::null_mut(); - check_status(sys::napi_get_value_external( + check_status!(sys::napi_get_value_external( self.0, js_external.0.value, &mut unknown_tagged_object, @@ -499,39 +543,44 @@ impl Env { } } + #[inline] pub fn create_error(&self, e: Error) -> Result { let reason = e.reason; let reason_string = self.create_string(reason.as_str())?; let mut result = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_error(self.0, ptr::null_mut(), reason_string.0.value, &mut result) })?; Ok(unsafe { JsObject::from_raw_unchecked(self.0, result) }) } + #[inline] pub fn spawn(&self, task: T) -> Result { async_work::run(self, task) } + #[inline] pub fn run_in_scope(&self, executor: F) -> Result where F: FnOnce() -> Result, { let mut handle_scope = ptr::null_mut(); - check_status(unsafe { sys::napi_open_handle_scope(self.0, &mut handle_scope) })?; + check_status!(unsafe { sys::napi_open_handle_scope(self.0, &mut handle_scope) })?; let result = executor(); - check_status(unsafe { sys::napi_close_handle_scope(self.0, handle_scope) })?; + check_status!(unsafe { sys::napi_close_handle_scope(self.0, handle_scope) })?; result } + #[inline] pub fn get_global(&self) -> Result { let mut raw_global = ptr::null_mut(); - check_status(unsafe { sys::napi_get_global(self.0, &mut raw_global) })?; + check_status!(unsafe { sys::napi_get_global(self.0, &mut raw_global) })?; Ok(unsafe { JsGlobal::from_raw_unchecked(self.0, raw_global) }) } + #[inline] pub fn get_napi_version(&self) -> Result { let global = self.get_global()?; let process: JsObject = global.get_named_property("process")?; @@ -545,13 +594,15 @@ impl Env { } #[cfg(feature = "napi2")] + #[inline] pub fn get_uv_event_loop(&self) -> Result<*mut sys::uv_loop_s> { let mut uv_loop: *mut sys::uv_loop_s = ptr::null_mut(); - check_status(unsafe { sys::napi_get_uv_event_loop(self.0, &mut uv_loop) })?; + check_status!(unsafe { sys::napi_get_uv_event_loop(self.0, &mut uv_loop) })?; Ok(uv_loop) } #[cfg(feature = "napi3")] + #[inline] pub fn add_env_cleanup_hook( &mut self, cleanup_data: T, @@ -566,7 +617,7 @@ impl Env { hook: Box::new(cleanup_fn), }; let hook_ref = Box::leak(Box::new(hook)); - check_status(unsafe { + check_status!(unsafe { sys::napi_add_env_cleanup_hook( self.0, Some(cleanup_env::), @@ -577,16 +628,18 @@ impl Env { } #[cfg(feature = "napi3")] + #[inline] pub fn remove_env_cleanup_hook(&mut self, hook: CleanupEnvHook) -> Result<()> where T: 'static, { - check_status(unsafe { + check_status!(unsafe { sys::napi_remove_env_cleanup_hook(self.0, Some(cleanup_env::), hook.0 as *mut _) }) } #[cfg(feature = "napi4")] + #[inline] pub fn create_threadsafe_function< T: Send, V: NapiValue, @@ -601,6 +654,7 @@ impl Env { } #[cfg(all(feature = "tokio_rt", feature = "napi4"))] + #[inline] pub fn execute_tokio_future< T: 'static + Send, V: 'static + NapiValue, @@ -613,7 +667,9 @@ impl Env { ) -> Result { let mut raw_promise = ptr::null_mut(); let mut raw_deferred = ptr::null_mut(); - check_status(unsafe { sys::napi_create_promise(self.0, &mut raw_deferred, &mut raw_promise) })?; + check_status!(unsafe { + sys::napi_create_promise(self.0, &mut raw_deferred, &mut raw_promise) + })?; let raw_env = self.0; let future_promise = @@ -636,9 +692,10 @@ impl Env { } #[cfg(feature = "napi5")] + #[inline] pub fn create_date(&self, time: f64) -> Result { let mut js_value = ptr::null_mut(); - check_status(unsafe { sys::napi_create_date(self.0, time, &mut js_value) })?; + check_status!(unsafe { sys::napi_create_date(self.0, time, &mut js_value) })?; Ok(unsafe { JsDate::from_raw_unchecked(self.0, js_value) }) } @@ -658,6 +715,7 @@ impl Env { /// } /// ``` #[cfg(feature = "serde-json")] + #[inline] pub fn to_js_value(&self, node: &T) -> Result where T: Serialize, @@ -683,6 +741,7 @@ impl Env { /// } /// #[cfg(feature = "serde-json")] + #[inline] pub fn from_js_value(&self, value: V) -> Result where T: DeserializeOwned + ?Sized, @@ -697,20 +756,23 @@ impl Env { T::deserialize(&mut de) } + #[inline] pub fn strict_equals(&self, a: A, b: B) -> Result { let mut result = false; - check_status(unsafe { sys::napi_strict_equals(self.0, a.raw(), b.raw(), &mut result) })?; + check_status!(unsafe { sys::napi_strict_equals(self.0, a.raw(), b.raw(), &mut result) })?; Ok(result) } + #[inline] pub fn get_node_version(&self) -> Result { let mut result = ptr::null(); - check_status(unsafe { sys::napi_get_node_version(self.0, &mut result) })?; + check_status!(unsafe { sys::napi_get_node_version(self.0, &mut result) })?; let version = unsafe { *result }; version.try_into() } /// get raw env ptr + #[inline] pub fn raw(&self) -> sys::napi_env { self.0 } diff --git a/napi/src/error.rs b/napi/src/error.rs index facb38cc..ad715294 100644 --- a/napi/src/error.rs +++ b/napi/src/error.rs @@ -1,5 +1,5 @@ use std::convert::From; -use std::error::Error as StdError; +use std::error; use std::fmt; #[cfg(feature = "serde-json")] use std::fmt::Display; @@ -19,7 +19,7 @@ pub struct Error { pub reason: String, } -impl StdError for Error {} +impl error::Error for Error {} #[cfg(feature = "serde-json")] impl ser::Error for Error { @@ -42,10 +42,12 @@ impl fmt::Display for Error { } impl Error { + #[inline] pub fn new(status: Status, reason: String) -> Self { Error { status, reason } } + #[inline] pub fn from_status(status: Status) -> Self { Error { status, @@ -53,6 +55,7 @@ impl Error { } } + #[inline] pub fn from_reason(reason: String) -> Self { Error { status: Status::GenericFailure, @@ -60,6 +63,7 @@ impl Error { } } + #[inline] pub(crate) fn into_raw(self, env: sys::napi_env) -> sys::napi_value { let mut err = ptr::null_mut(); let s = self.reason; @@ -97,11 +101,14 @@ impl From for Error { } } -#[inline] -pub fn check_status(code: sys::napi_status) -> Result<()> { - let status = Status::from(code); - match status { - Status::Ok => Ok(()), - _ => Err(Error::new(status, "".to_owned())), - } +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! check_status { + ($code:expr) => {{ + let c = $code; + match c { + $crate::sys::Status::napi_ok => Ok(()), + _ => Err($crate::Error::new($crate::Status::from(c), "".to_owned())), + } + }}; } diff --git a/napi/src/js_values/arraybuffer.rs b/napi/src/js_values/arraybuffer.rs index 560438da..6de957f5 100644 --- a/napi/src/js_values/arraybuffer.rs +++ b/napi/src/js_values/arraybuffer.rs @@ -4,40 +4,31 @@ use std::os::raw::c_void; use std::ptr; use super::{Value, ValueType}; -use crate::error::check_status; +use crate::check_status; use crate::{sys, JsUnknown, NapiValue, Ref, Result}; -#[repr(transparent)] -#[derive(Debug)] pub struct JsArrayBuffer(pub(crate) Value); -#[derive(Debug)] pub struct JsArrayBufferValue { pub(crate) value: JsArrayBuffer, data: mem::ManuallyDrop>, } -#[repr(transparent)] -#[derive(Debug)] pub struct JsTypedArray(pub(crate) Value); -#[derive(Debug)] pub struct JsTypedArrayValue { pub arraybuffer: JsArrayBuffer, - data: *mut c_void, + _data: *mut c_void, pub byte_offset: u64, pub length: u64, pub typedarray_type: TypedArrayType, } -#[repr(transparent)] -#[derive(Debug)] pub struct JsDataView(pub(crate) Value); -#[derive(Debug)] pub struct JsDataViewValue { pub arraybuffer: JsArrayBuffer, - data: *mut c_void, + _data: *mut c_void, pub byte_offset: u64, pub length: u64, } @@ -88,23 +79,26 @@ impl From for sys::napi_typedarray_type { impl JsArrayBuffer { #[cfg(feature = "napi7")] + #[inline] pub fn detach(self) -> Result<()> { - check_status(unsafe { sys::napi_detach_arraybuffer(self.0.env, self.0.value) }) + check_status!(unsafe { sys::napi_detach_arraybuffer(self.0.env, self.0.value) }) } #[cfg(feature = "napi7")] + #[inline] pub fn is_detached(&self) -> Result { let mut is_detached = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_is_detached_arraybuffer(self.0.env, self.0.value, &mut is_detached) })?; Ok(is_detached) } + #[inline] pub fn into_value(self) -> Result { let mut data = ptr::null_mut(); let mut len: usize = 0; - check_status(unsafe { + check_status!(unsafe { sys::napi_get_arraybuffer_info( self.0.env, self.0.value, @@ -118,6 +112,7 @@ impl JsArrayBuffer { }) } + #[inline] pub fn into_typedarray( self, typedarray_type: TypedArrayType, @@ -125,7 +120,7 @@ impl JsArrayBuffer { byte_offset: u64, ) -> Result { let mut typedarray_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_typedarray( self.0.env, typedarray_type.into(), @@ -142,9 +137,10 @@ impl JsArrayBuffer { })) } + #[inline] pub fn into_dataview(self, length: u64, byte_offset: u64) -> Result { let mut dataview_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_dataview( self.0.env, length as _, @@ -167,6 +163,7 @@ impl JsArrayBuffer { } impl JsArrayBufferValue { + #[inline] pub fn new(value: JsArrayBuffer, data: Vec) -> Self { JsArrayBufferValue { value, @@ -174,10 +171,12 @@ impl JsArrayBufferValue { } } + #[inline] pub fn into_raw(self) -> JsArrayBuffer { self.value } + #[inline] pub fn into_unknown(self) -> JsUnknown { unsafe { JsUnknown::from_raw_unchecked(self.value.0.env, self.value.0.value) } } @@ -202,13 +201,14 @@ impl JsTypedArray { /// https://nodejs.org/api/n-api.html#n_api_napi_get_typedarray_info /// /// ***Warning***: Use caution while using this API since the underlying data buffer is managed by the VM. + #[inline] pub fn into_value(self) -> Result { let mut typedarray_type = 0; let mut len = 0u64; let mut data = ptr::null_mut(); let mut arraybuffer_value = ptr::null_mut(); let mut byte_offset = 0u64; - check_status(unsafe { + check_status!(unsafe { sys::napi_get_typedarray_info( self.0.env, self.0.value, @@ -221,7 +221,7 @@ impl JsTypedArray { })?; Ok(JsTypedArrayValue { - data, + _data: data, length: len, byte_offset, typedarray_type: typedarray_type.into(), @@ -231,13 +231,14 @@ impl JsTypedArray { } impl JsDataView { + #[inline] pub fn into_value(self) -> Result { let mut length = 0u64; let mut byte_offset = 0u64; let mut arraybuffer_value = ptr::null_mut(); let mut data = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_get_dataview_info( self.0.env, self.0.value, @@ -251,7 +252,7 @@ impl JsDataView { arraybuffer: unsafe { JsArrayBuffer::from_raw_unchecked(self.0.env, arraybuffer_value) }, byte_offset, length, - data, + _data: data, }) } } diff --git a/napi/src/js_values/bigint.rs b/napi/src/js_values/bigint.rs index 58cedd22..77f20895 100644 --- a/napi/src/js_values/bigint.rs +++ b/napi/src/js_values/bigint.rs @@ -2,16 +2,15 @@ use std::convert::TryFrom; use std::ptr; use super::*; -use crate::error::check_status; -use crate::{sys, Result}; +use crate::{check_status, sys, Result}; -#[derive(Debug)] pub struct JsBigint { pub(crate) raw: Value, pub word_count: u64, } impl JsBigint { + #[inline] pub(crate) fn from_raw_unchecked( env: sys::napi_env, value: sys::napi_value, @@ -35,7 +34,7 @@ impl JsBigint { #[inline] pub fn coerce_to_number(self) -> Result { let mut new_raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_coerce_to_number(self.raw.env, self.raw.value, &mut new_raw_value) })?; Ok(JsNumber(Value { @@ -48,7 +47,7 @@ impl JsBigint { #[inline] pub fn coerce_to_string(self) -> Result { let mut new_raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_coerce_to_string(self.raw.env, self.raw.value, &mut new_raw_value) })?; Ok(JsString(Value { @@ -60,7 +59,7 @@ impl JsBigint { #[inline] pub fn coerce_to_object(self) -> Result { let mut new_raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_coerce_to_object(self.raw.env, self.raw.value, &mut new_raw_value) })?; Ok(JsObject(Value { @@ -74,49 +73,49 @@ impl JsBigint { #[cfg(feature = "napi5")] pub fn is_date(&self) -> Result { let mut is_date = true; - check_status(unsafe { sys::napi_is_date(self.raw.env, self.raw.value, &mut is_date) })?; + check_status!(unsafe { sys::napi_is_date(self.raw.env, self.raw.value, &mut is_date) })?; Ok(is_date) } #[inline] pub fn is_error(&self) -> Result { let mut result = false; - check_status(unsafe { sys::napi_is_error(self.raw.env, self.raw.value, &mut result) })?; + check_status!(unsafe { sys::napi_is_error(self.raw.env, self.raw.value, &mut result) })?; Ok(result) } #[inline] pub fn is_typedarray(&self) -> Result { let mut result = false; - check_status(unsafe { sys::napi_is_typedarray(self.raw.env, self.raw.value, &mut result) })?; + check_status!(unsafe { sys::napi_is_typedarray(self.raw.env, self.raw.value, &mut result) })?; Ok(result) } #[inline] pub fn is_dataview(&self) -> Result { let mut result = false; - check_status(unsafe { sys::napi_is_dataview(self.raw.env, self.raw.value, &mut result) })?; + check_status!(unsafe { sys::napi_is_dataview(self.raw.env, self.raw.value, &mut result) })?; Ok(result) } #[inline] pub fn is_array(&self) -> Result { let mut is_array = false; - check_status(unsafe { sys::napi_is_array(self.raw.env, self.raw.value, &mut is_array) })?; + check_status!(unsafe { sys::napi_is_array(self.raw.env, self.raw.value, &mut is_array) })?; Ok(is_array) } #[inline] pub fn is_buffer(&self) -> Result { let mut is_buffer = false; - check_status(unsafe { sys::napi_is_buffer(self.raw.env, self.raw.value, &mut is_buffer) })?; + check_status!(unsafe { sys::napi_is_buffer(self.raw.env, self.raw.value, &mut is_buffer) })?; Ok(is_buffer) } #[inline] pub fn instanceof(&self, constructor: Constructor) -> Result { let mut result = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_instanceof(self.raw.env, self.raw.value, constructor.raw(), &mut result) })?; Ok(result) @@ -130,7 +129,7 @@ impl NapiValue for JsBigint { unsafe fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result { let mut word_count: u64 = 0; - check_status(sys::napi_get_value_bigint_words( + check_status!(sys::napi_get_value_bigint_words( env, value, ptr::null_mut(), @@ -196,7 +195,7 @@ impl JsBigint { let mut words: Vec = Vec::with_capacity(self.word_count as usize); let word_count = &mut self.word_count; let mut sign_bit = 0; - check_status(unsafe { + check_status!(unsafe { sys::napi_get_value_bigint_words( self.raw.env, self.raw.value, @@ -217,7 +216,7 @@ impl JsBigint { pub fn get_u64(&self) -> Result<(u64, bool)> { let mut val: u64 = 0; let mut loss = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_get_value_bigint_uint64(self.raw.env, self.raw.value, &mut val, &mut loss) })?; @@ -228,7 +227,7 @@ impl JsBigint { pub fn get_i64(&self) -> Result<(i64, bool)> { let mut val: i64 = 0; let mut loss: bool = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_get_value_bigint_int64(self.raw.env, self.raw.value, &mut val, &mut loss) })?; Ok((val, loss)) diff --git a/napi/src/js_values/boolean.rs b/napi/src/js_values/boolean.rs index e0d25bd5..11ac20a3 100644 --- a/napi/src/js_values/boolean.rs +++ b/napi/src/js_values/boolean.rs @@ -1,17 +1,16 @@ use std::convert::TryFrom; use super::Value; -use crate::error::check_status; +use crate::check_status; use crate::{sys, Error, Result}; -#[repr(transparent)] -#[derive(Debug)] pub struct JsBoolean(pub(crate) Value); impl JsBoolean { + #[inline] pub fn get_value(&self) -> Result { let mut result = false; - check_status(unsafe { sys::napi_get_value_bool(self.0.env, self.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_get_value_bool(self.0.env, self.0.value, &mut result) })?; Ok(result) } } diff --git a/napi/src/js_values/buffer.rs b/napi/src/js_values/buffer.rs index 83931c2e..fd3476e0 100644 --- a/napi/src/js_values/buffer.rs +++ b/napi/src/js_values/buffer.rs @@ -5,24 +5,22 @@ use std::ptr; use super::Value; #[cfg(feature = "serde-json")] use super::ValueType; -use crate::error::check_status; +use crate::check_status; use crate::{sys, JsUnknown, NapiValue, Ref, Result}; -#[repr(transparent)] -#[derive(Debug)] pub struct JsBuffer(pub(crate) Value); -#[derive(Debug)] pub struct JsBufferValue { pub(crate) value: JsBuffer, data: mem::ManuallyDrop>, } impl JsBuffer { + #[inline] pub fn into_value(self) -> Result { let mut data = ptr::null_mut(); let mut len: usize = 0; - check_status(unsafe { + check_status!(unsafe { sys::napi_get_buffer_info( self.0.env, self.0.value, @@ -44,10 +42,11 @@ impl JsBuffer { impl JsBufferValue { #[cfg(feature = "serde-json")] + #[inline] pub(crate) fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result { let mut data = ptr::null_mut(); let mut len = 0usize; - check_status(unsafe { + check_status!(unsafe { sys::napi_get_buffer_info(env, value, &mut data, &mut len as *mut usize as *mut _) })?; Ok(Self { @@ -60,6 +59,7 @@ impl JsBufferValue { }) } + #[inline] pub fn new(value: JsBuffer, data: Vec) -> Self { JsBufferValue { value, @@ -67,10 +67,12 @@ impl JsBufferValue { } } + #[inline] pub fn into_raw(self) -> JsBuffer { self.value } + #[inline] pub fn into_unknown(self) -> JsUnknown { unsafe { JsUnknown::from_raw_unchecked(self.value.0.env, self.value.0.value) } } diff --git a/napi/src/js_values/date.rs b/napi/src/js_values/date.rs index 8c17c48c..74489ad8 100644 --- a/napi/src/js_values/date.rs +++ b/napi/src/js_values/date.rs @@ -1,14 +1,13 @@ use super::check_status; use crate::{sys, Result, Value}; -#[repr(transparent)] -#[derive(Debug)] pub struct JsDate(pub(crate) Value); impl JsDate { + #[inline] pub fn value_of(&self) -> Result { let mut timestamp: f64 = 0.0; - check_status(unsafe { sys::napi_get_date_value(self.0.env, self.0.value, &mut timestamp) })?; + check_status!(unsafe { sys::napi_get_date_value(self.0.env, self.0.value, &mut timestamp) })?; Ok(timestamp) } } diff --git a/napi/src/js_values/de.rs b/napi/src/js_values/de.rs index 6086985e..9b55d341 100644 --- a/napi/src/js_values/de.rs +++ b/napi/src/js_values/de.rs @@ -3,9 +3,9 @@ use std::convert::TryInto; use serde::de::Visitor; use serde::de::{DeserializeSeed, EnumAccess, MapAccess, SeqAccess, Unexpected, VariantAccess}; -use super::{type_of, NapiValue, Value, ValueType}; #[cfg(feature = "napi6")] use crate::JsBigint; +use crate::{type_of, NapiValue, Value, ValueType}; use crate::{ Error, JsBoolean, JsBufferValue, JsNumber, JsObject, JsString, JsUnknown, Result, Status, }; @@ -20,7 +20,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> { where V: Visitor<'x>, { - let js_value_type = type_of(self.0.env, self.0.value)?; + let js_value_type = unsafe { type_of!(self.0.env, self.0.value) }?; match js_value_type { ValueType::Null | ValueType::Undefined => visitor.visit_unit(), ValueType::Boolean => { @@ -89,7 +89,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> { where V: Visitor<'x>, { - match type_of(self.0.env, self.0.value)? { + match unsafe { type_of!(self.0.env, self.0.value) }? { ValueType::Undefined | ValueType::Null => visitor.visit_none(), _ => visitor.visit_some(self), } @@ -104,7 +104,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> { where V: Visitor<'x>, { - let js_value_type = type_of(self.0.env, self.0.value)?; + let js_value_type = unsafe { type_of!(self.0.env, self.0.value)? }; match js_value_type { ValueType::String => visitor.visit_enum(JsEnumAccess::new( unsafe { JsString::from_raw_unchecked(self.0.env, self.0.value) } diff --git a/napi/src/js_values/escapable_handle_scope.rs b/napi/src/js_values/escapable_handle_scope.rs index d09a4480..734fbe46 100644 --- a/napi/src/js_values/escapable_handle_scope.rs +++ b/napi/src/js_values/escapable_handle_scope.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use std::ptr; -use super::check_status; +use crate::check_status; use crate::{sys, Env, NapiValue, Result}; pub struct EscapableHandleScope { @@ -13,9 +13,9 @@ impl EscapableHandleScope { #[inline] pub fn open(env: sys::napi_env, value: T) -> Result { let mut handle_scope = ptr::null_mut(); - check_status(unsafe { sys::napi_open_escapable_handle_scope(env, &mut handle_scope) })?; + check_status!(unsafe { sys::napi_open_escapable_handle_scope(env, &mut handle_scope) })?; let mut result = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_escape_handle(env, handle_scope, NapiValue::raw(&value), &mut result) })?; Ok(Self { @@ -26,7 +26,7 @@ impl EscapableHandleScope { #[must_use] pub fn close(self, env: Env) -> Result<()> { - check_status(unsafe { sys::napi_close_escapable_handle_scope(env.0, self.handle_scope) }) + check_status!(unsafe { sys::napi_close_escapable_handle_scope(env.0, self.handle_scope) }) } } diff --git a/napi/src/js_values/function.rs b/napi/src/js_values/function.rs index 9c3736b2..3e2b1c53 100644 --- a/napi/src/js_values/function.rs +++ b/napi/src/js_values/function.rs @@ -1,11 +1,9 @@ use std::ptr; use super::Value; -use crate::error::check_status; +use crate::check_status; use crate::{sys, Env, Error, JsObject, JsUnknown, NapiValue, Result, Status}; -#[repr(transparent)] -#[derive(Debug)] pub struct JsFunction(pub(crate) Value); /// See [Working with JavaScript Functions](https://nodejs.org/api/n-api.html#n_api_working_with_javascript_functions). @@ -24,6 +22,7 @@ pub struct JsFunction(pub(crate) Value); /// ``` impl JsFunction { /// [napi_call_function](https://nodejs.org/api/n-api.html#n_api_napi_call_function) + #[inline] pub fn call(&self, this: Option<&JsObject>, args: &[JsUnknown]) -> Result { let raw_this = this .map(|v| unsafe { v.raw() }) @@ -42,7 +41,7 @@ impl JsFunction { .map(|arg| arg.0.value) .collect::>(); let mut return_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_call_function( self.0.env, raw_this, @@ -58,6 +57,7 @@ impl JsFunction { /// https://nodejs.org/api/n-api.html#n_api_napi_new_instance /// This method is used to instantiate a new `JavaScript` value using a given `JsFunction` that represents the constructor for the object. + #[inline] pub fn new(&self, args: &[V]) -> Result where V: NapiValue, @@ -68,7 +68,7 @@ impl JsFunction { .iter() .map(|arg| unsafe { arg.raw() }) .collect::>(); - check_status(unsafe { + check_status!(unsafe { sys::napi_new_instance( self.0.env, self.0.value, diff --git a/napi/src/js_values/global.rs b/napi/src/js_values/global.rs index 70b56021..9f86134e 100644 --- a/napi/src/js_values/global.rs +++ b/napi/src/js_values/global.rs @@ -3,15 +3,12 @@ use std::convert::TryInto; use super::*; use crate::Env; -#[repr(transparent)] -#[derive(Debug)] pub struct JsGlobal(pub(crate) Value); -#[repr(transparent)] -#[derive(Debug)] pub struct JsTimeout(pub(crate) Value); impl JsGlobal { + #[inline] pub fn set_interval(&self, handler: JsFunction, interval: f64) -> Result { let func: JsFunction = self.get_named_property("setInterval")?; func @@ -34,6 +31,7 @@ impl JsGlobal { .and_then(|ret| ret.try_into()) } + #[inline] pub fn set_timeout(&self, handler: JsFunction, interval: f64) -> Result { let func: JsFunction = self.get_named_property("setTimeout")?; func @@ -49,6 +47,7 @@ impl JsGlobal { .and_then(|ret| ret.try_into()) } + #[inline] pub fn clear_timeout(&self, timer: JsTimeout) -> Result { let func: JsFunction = self.get_named_property("clearTimeout")?; func diff --git a/napi/src/js_values/mod.rs b/napi/src/js_values/mod.rs index 3ae48777..82e5c937 100644 --- a/napi/src/js_values/mod.rs +++ b/napi/src/js_values/mod.rs @@ -2,8 +2,8 @@ use std::convert::TryFrom; use std::ffi::CString; use std::ptr; -use crate::error::check_status; -use crate::{sys, Error, Result, Status}; +use crate::check_status; +use crate::{sys, Callback, Error, Result, Status}; #[cfg(feature = "serde-json")] mod de; @@ -57,36 +57,33 @@ pub use value_ref::Ref; pub use value_type::ValueType; // Value types -#[repr(transparent)] -#[derive(Debug)] + pub struct JsUnknown(pub(crate) Value); -#[repr(transparent)] -#[derive(Debug)] pub struct JsNull(pub(crate) Value); -#[repr(transparent)] -#[derive(Debug)] pub struct JsSymbol(pub(crate) Value); -#[repr(transparent)] -#[derive(Debug)] pub struct JsExternal(pub(crate) Value); -#[inline] -pub(crate) fn type_of(env: sys::napi_env, raw_value: sys::napi_value) -> Result { - unsafe { +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! type_of { + ($env:expr, $value:expr) => {{ + use std::convert::TryFrom; + use $crate::sys; let mut value_type = 0; - check_status(sys::napi_typeof(env, raw_value, &mut value_type))?; - ValueType::try_from(value_type).or_else(|_| Ok(ValueType::Unknown)) - } + let status = sys::napi_typeof($env, $value, &mut value_type); + check_status!(status) + .and_then(|_| ValueType::try_from(value_type).or_else(|_| Ok(ValueType::Unknown))) + }}; } macro_rules! impl_napi_value_trait { ($js_value:ident, $value_type:ident) => { impl NapiValue for $js_value { unsafe fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result<$js_value> { - let value_type = type_of(env, value)?; + let value_type = type_of!(env, value)?; if value_type != $value_type { Err(Error::new( Status::InvalidArg, @@ -134,9 +131,9 @@ macro_rules! impl_js_value_methods { #[inline] pub fn coerce_to_number(self) -> Result { let mut new_raw_value = ptr::null_mut(); - let status = - unsafe { sys::napi_coerce_to_number(self.0.env, self.0.value, &mut new_raw_value) }; - check_status(status)?; + check_status!(unsafe { + sys::napi_coerce_to_number(self.0.env, self.0.value, &mut new_raw_value) + })?; Ok(JsNumber(Value { env: self.0.env, value: new_raw_value, @@ -147,9 +144,9 @@ macro_rules! impl_js_value_methods { #[inline] pub fn coerce_to_string(self) -> Result { let mut new_raw_value = ptr::null_mut(); - let status = - unsafe { sys::napi_coerce_to_string(self.0.env, self.0.value, &mut new_raw_value) }; - check_status(status)?; + check_status!(unsafe { + sys::napi_coerce_to_string(self.0.env, self.0.value, &mut new_raw_value) + })?; Ok(JsString(Value { env: self.0.env, value: new_raw_value, @@ -160,9 +157,9 @@ macro_rules! impl_js_value_methods { #[inline] pub fn coerce_to_object(self) -> Result { let mut new_raw_value = ptr::null_mut(); - let status = - unsafe { sys::napi_coerce_to_object(self.0.env, self.0.value, &mut new_raw_value) }; - check_status(status)?; + check_status!(unsafe { + sys::napi_coerce_to_object(self.0.env, self.0.value, &mut new_raw_value) + })?; Ok(JsObject(Value { env: self.0.env, value: new_raw_value, @@ -170,59 +167,60 @@ macro_rules! impl_js_value_methods { })) } - #[inline] #[cfg(feature = "napi5")] + #[inline] pub fn is_date(&self) -> Result { let mut is_date = true; - check_status(unsafe { sys::napi_is_date(self.0.env, self.0.value, &mut is_date) })?; + check_status!(unsafe { sys::napi_is_date(self.0.env, self.0.value, &mut is_date) })?; Ok(is_date) } + #[inline] pub fn is_promise(&self) -> Result { let mut is_promise = true; - check_status(unsafe { sys::napi_is_promise(self.0.env, self.0.value, &mut is_promise) })?; + check_status!(unsafe { sys::napi_is_promise(self.0.env, self.0.value, &mut is_promise) })?; Ok(is_promise) } #[inline] pub fn is_error(&self) -> Result { let mut result = false; - check_status(unsafe { sys::napi_is_error(self.0.env, self.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_is_error(self.0.env, self.0.value, &mut result) })?; Ok(result) } #[inline] pub fn is_typedarray(&self) -> Result { let mut result = false; - check_status(unsafe { sys::napi_is_typedarray(self.0.env, self.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_is_typedarray(self.0.env, self.0.value, &mut result) })?; Ok(result) } #[inline] pub fn is_dataview(&self) -> Result { let mut result = false; - check_status(unsafe { sys::napi_is_dataview(self.0.env, self.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_is_dataview(self.0.env, self.0.value, &mut result) })?; Ok(result) } #[inline] pub fn is_array(&self) -> Result { let mut is_array = false; - check_status(unsafe { sys::napi_is_array(self.0.env, self.0.value, &mut is_array) })?; + check_status!(unsafe { sys::napi_is_array(self.0.env, self.0.value, &mut is_array) })?; Ok(is_array) } #[inline] pub fn is_buffer(&self) -> Result { let mut is_buffer = false; - check_status(unsafe { sys::napi_is_buffer(self.0.env, self.0.value, &mut is_buffer) })?; + check_status!(unsafe { sys::napi_is_buffer(self.0.env, self.0.value, &mut is_buffer) })?; Ok(is_buffer) } #[inline] pub fn instanceof(&self, constructor: Constructor) -> Result { let mut result = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_instanceof(self.0.env, self.0.value, constructor.raw(), &mut result) })?; Ok(result) @@ -234,125 +232,168 @@ macro_rules! impl_js_value_methods { macro_rules! impl_object_methods { ($js_value:ident) => { impl $js_value { + #[inline] pub fn set_property(&mut self, key: JsString, value: V) -> Result<()> where V: NapiValue, { - check_status(unsafe { + check_status!(unsafe { sys::napi_set_property(self.0.env, self.0.value, key.0.value, value.raw()) }) } + + #[inline] pub fn get_property(&self, key: &K) -> Result where K: NapiValue, T: NapiValue, { let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_get_property(self.0.env, self.0.value, key.raw(), &mut raw_value) })?; unsafe { T::from_raw(self.0.env, raw_value) } } + + #[inline] pub fn set_named_property(&mut self, name: &str, value: T) -> Result<()> where T: NapiValue, { let key = CString::new(name)?; - check_status(unsafe { + check_status!(unsafe { sys::napi_set_named_property(self.0.env, self.0.value, key.as_ptr(), value.raw()) }) } + + #[inline] + pub fn create_named_method(&mut self, name: &str, function: Callback) -> Result<()> { + let mut js_function = ptr::null_mut(); + let len = name.len(); + let name = CString::new(name.as_bytes())?; + check_status!(unsafe { + sys::napi_create_function( + self.0.env, + name.as_ptr(), + len, + Some(function), + ptr::null_mut(), + &mut js_function, + ) + })?; + check_status!(unsafe { + sys::napi_set_named_property(self.0.env, self.0.value, name.as_ptr(), js_function) + }) + } + + #[inline] pub fn get_named_property(&self, name: &str) -> Result where T: NapiValue, { let key = CString::new(name)?; let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_get_named_property(self.0.env, self.0.value, key.as_ptr(), &mut raw_value) })?; unsafe { T::from_raw(self.0.env, raw_value) } } + + #[inline] pub fn has_named_property(&self, name: &str) -> Result { let mut result = false; let key = CString::new(name)?; - check_status(unsafe { + check_status!(unsafe { sys::napi_has_named_property(self.0.env, self.0.value, key.as_ptr(), &mut result) })?; Ok(result) } + + #[inline] pub fn delete_property(&mut self, name: S) -> Result where S: NapiValue, { let mut result = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_delete_property(self.0.env, self.0.value, name.raw(), &mut result) })?; Ok(result) } + + #[inline] pub fn delete_named_property(&mut self, name: &str) -> Result { let mut result = false; let key_str = CString::new(name)?; let mut js_key = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_string_utf8(self.0.env, key_str.as_ptr(), name.len(), &mut js_key) })?; - check_status(unsafe { + check_status!(unsafe { sys::napi_delete_property(self.0.env, self.0.value, js_key, &mut result) })?; Ok(result) } + + #[inline] pub fn has_own_property(&self, key: &str) -> Result { let mut result = false; let string = CString::new(key)?; let mut js_key = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_string_utf8(self.0.env, string.as_ptr(), key.len(), &mut js_key) })?; - check_status(unsafe { + check_status!(unsafe { sys::napi_has_own_property(self.0.env, self.0.value, js_key, &mut result) })?; Ok(result) } + + #[inline] pub fn has_own_property_js(&self, key: K) -> Result where K: NapiValue, { let mut result = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_has_own_property(self.0.env, self.0.value, key.raw(), &mut result) })?; Ok(result) } + + #[inline] pub fn has_property(&self, name: &str) -> Result { let string = CString::new(name)?; let mut js_key = ptr::null_mut(); let mut result = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_create_string_utf8(self.0.env, string.as_ptr(), name.len() as _, &mut js_key) })?; - check_status(unsafe { + check_status!(unsafe { sys::napi_has_property(self.0.env, self.0.value, js_key, &mut result) })?; Ok(result) } + + #[inline] pub fn has_property_js(&self, name: K) -> Result where K: NapiValue, { let mut result = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_has_property(self.0.env, self.0.value, name.raw(), &mut result) })?; Ok(result) } + + #[inline] pub fn get_property_names(&self) -> Result where T: NapiValue, { let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_get_property_names(self.0.env, self.0.value, &mut raw_value) })?; unsafe { T::from_raw(self.0.env, raw_value) } @@ -361,6 +402,7 @@ macro_rules! impl_object_methods { /// https://nodejs.org/api/n-api.html#n_api_napi_get_all_property_names /// return `Array` of property names #[cfg(feature = "napi6")] + #[inline] pub fn get_all_property_names( &self, mode: KeyCollectionMode, @@ -368,7 +410,7 @@ macro_rules! impl_object_methods { conversion: KeyConversion, ) -> Result { let mut properties_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_get_all_property_names( self.0.env, self.0.value, @@ -382,49 +424,60 @@ macro_rules! impl_object_methods { } /// This returns the equivalent of `Object.getPrototypeOf` (which is not the same as the function's prototype property). + #[inline] pub fn get_prototype(&self) -> Result where T: NapiValue, { let mut result = ptr::null_mut(); - check_status(unsafe { sys::napi_get_prototype(self.0.env, self.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_get_prototype(self.0.env, self.0.value, &mut result) })?; unsafe { T::from_raw(self.0.env, result) } } + #[inline] pub fn set_element(&mut self, index: u32, value: T) -> Result<()> where T: NapiValue, { - check_status(unsafe { sys::napi_set_element(self.0.env, self.0.value, index, value.raw()) }) + check_status!(unsafe { + sys::napi_set_element(self.0.env, self.0.value, index, value.raw()) + }) } + + #[inline] pub fn has_element(&self, index: u32) -> Result { let mut result = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_has_element(self.0.env, self.0.value, index, &mut result) })?; Ok(result) } + + #[inline] pub fn delete_element(&mut self, index: u32) -> Result { let mut result = false; - check_status(unsafe { + check_status!(unsafe { sys::napi_delete_element(self.0.env, self.0.value, index, &mut result) })?; Ok(result) } + + #[inline] pub fn get_element(&self, index: u32) -> Result where T: NapiValue, { let mut raw_value = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_get_element(self.0.env, self.0.value, index, &mut raw_value) })?; unsafe { T::from_raw(self.0.env, raw_value) } } /// This method allows the efficient definition of multiple properties on a given object. + #[inline] pub fn define_properties(&mut self, properties: &[Property]) -> Result<()> { - check_status(unsafe { + check_status!(unsafe { sys::napi_define_properties( self.0.env, self.0.value, @@ -440,6 +493,7 @@ macro_rules! impl_object_methods { /// Perform `is_array` check before get the length /// if `Object` is not array, `ArrayExpected` error returned + #[inline] pub fn get_array_length(&self) -> Result { if self.is_array()? != true { return Err(Error::new( @@ -454,7 +508,7 @@ macro_rules! impl_object_methods { #[inline] pub fn get_array_length_unchecked(&self) -> Result { let mut length: u32 = 0; - check_status(unsafe { sys::napi_get_array_length(self.0.env, self.raw(), &mut length) })?; + check_status!(unsafe { sys::napi_get_array_length(self.0.env, self.raw(), &mut length) })?; Ok(length) } } @@ -516,6 +570,7 @@ impl_napi_value_trait!(JsExternal, External); impl_napi_value_trait!(JsSymbol, Symbol); impl NapiValue for JsUnknown { + #[inline] unsafe fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result { Ok(JsUnknown(Value { env, @@ -524,6 +579,7 @@ impl NapiValue for JsUnknown { })) } + #[inline] unsafe fn from_raw_unchecked(env: sys::napi_env, value: sys::napi_value) -> Self { JsUnknown(Value { env, @@ -533,6 +589,7 @@ impl NapiValue for JsUnknown { } /// get raw js value ptr + #[inline] unsafe fn raw(&self) -> sys::napi_value { self.0.value } @@ -541,6 +598,6 @@ impl NapiValue for JsUnknown { impl JsUnknown { #[inline] pub fn get_type(&self) -> Result { - type_of(self.0.env, self.0.value) + unsafe { type_of!(self.0.env, self.0.value) } } } diff --git a/napi/src/js_values/number.rs b/napi/src/js_values/number.rs index 4b573025..f3578de1 100644 --- a/napi/src/js_values/number.rs +++ b/napi/src/js_values/number.rs @@ -1,11 +1,9 @@ use std::convert::TryFrom; use super::Value; -use crate::error::check_status; +use crate::check_status; use crate::{sys, Error, Result}; -#[repr(transparent)] -#[derive(Debug)] pub struct JsNumber(pub(crate) Value); impl TryFrom for usize { @@ -13,7 +11,7 @@ impl TryFrom for usize { fn try_from(value: JsNumber) -> Result { let mut result = 0; - check_status(unsafe { sys::napi_get_value_int64(value.0.env, value.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_get_value_int64(value.0.env, value.0.value, &mut result) })?; Ok(result as usize) } } @@ -23,7 +21,7 @@ impl TryFrom for u32 { fn try_from(value: JsNumber) -> Result { let mut result = 0; - check_status(unsafe { sys::napi_get_value_uint32(value.0.env, value.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_get_value_uint32(value.0.env, value.0.value, &mut result) })?; Ok(result) } } @@ -33,7 +31,7 @@ impl TryFrom for i32 { fn try_from(value: JsNumber) -> Result { let mut result = 0; - check_status(unsafe { sys::napi_get_value_int32(value.0.env, value.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_get_value_int32(value.0.env, value.0.value, &mut result) })?; Ok(result) } } @@ -43,7 +41,7 @@ impl TryFrom for i64 { fn try_from(value: JsNumber) -> Result { let mut result = 0; - check_status(unsafe { sys::napi_get_value_int64(value.0.env, value.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_get_value_int64(value.0.env, value.0.value, &mut result) })?; Ok(result) } } @@ -53,7 +51,7 @@ impl TryFrom for f64 { fn try_from(value: JsNumber) -> Result { let mut result = 0_f64; - check_status(unsafe { sys::napi_get_value_double(value.0.env, value.0.value, &mut result) })?; + check_status!(unsafe { sys::napi_get_value_double(value.0.env, value.0.value, &mut result) })?; Ok(result) } } diff --git a/napi/src/js_values/object.rs b/napi/src/js_values/object.rs index 9f18f1d4..e3827a33 100644 --- a/napi/src/js_values/object.rs +++ b/napi/src/js_values/object.rs @@ -6,8 +6,6 @@ use crate::{Error, Result}; #[cfg(feature = "napi6")] use std::convert::TryFrom; -#[repr(transparent)] -#[derive(Debug)] pub struct JsObject(pub(crate) Value); #[cfg(feature = "napi6")] diff --git a/napi/src/js_values/object_property.rs b/napi/src/js_values/object_property.rs index 247cb0dc..44745205 100644 --- a/napi/src/js_values/object_property.rs +++ b/napi/src/js_values/object_property.rs @@ -2,11 +2,11 @@ use std::convert::From; use std::ffi::CString; use std::ptr; -use crate::{error::check_status, sys, Callback, Env, NapiValue, Result}; +use crate::{check_status, sys, Callback, Env, NapiValue, Result}; -#[derive(Debug, Clone, Copy)] +#[derive(Clone, Copy)] pub struct Property<'env> { - name: &'env str, + pub name: &'env str, pub(crate) raw_descriptor: sys::napi_property_descriptor, } @@ -33,10 +33,11 @@ impl From for sys::napi_property_attributes { } impl<'env> Property<'env> { + #[inline] pub fn new(env: &'env Env, name: &'env str) -> Result { let string_value = CString::new(name)?; let mut result = ptr::null_mut(); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_string_utf8(env.0, string_value.as_ptr(), name.len(), &mut result) })?; Ok(Property { @@ -54,31 +55,37 @@ impl<'env> Property<'env> { }) } + #[inline] pub fn with_value(mut self, value: T) -> Self { self.raw_descriptor.value = unsafe { T::raw(&value) }; self } + #[inline] pub fn with_method(mut self, callback: Callback) -> Self { self.raw_descriptor.method = Some(callback); self } + #[inline] pub fn with_getter(mut self, callback: Callback) -> Self { self.raw_descriptor.getter = Some(callback); self } + #[inline] pub fn with_setter(mut self, callback: Callback) -> Self { self.raw_descriptor.setter = Some(callback); self } + #[inline] pub fn with_property_attributes(mut self, attributes: PropertyAttributes) -> Self { self.raw_descriptor.attributes = attributes.into(); self } + #[inline] pub(crate) fn raw(&self) -> sys::napi_property_descriptor { self.raw_descriptor } diff --git a/napi/src/js_values/string/latin1.rs b/napi/src/js_values/string/latin1.rs index a5cc564b..29496923 100644 --- a/napi/src/js_values/string/latin1.rs +++ b/napi/src/js_values/string/latin1.rs @@ -14,23 +14,28 @@ pub struct JsStringLatin1 { } impl JsStringLatin1 { + #[inline] pub fn as_slice(&self) -> &[u8] { &self.buf } + #[inline] pub fn len(&self) -> usize { self.buf.len() } + #[inline] pub fn take(self) -> Vec { self.as_slice().to_vec() } + #[inline] pub fn into_value(self) -> JsString { self.inner } #[cfg(feature = "latin1")] + #[inline] pub fn into_latin1_string(self) -> Result { let mut dst_str = unsafe { String::from_utf8_unchecked(vec![0; self.len() * 2 + 1]) }; encoding_rs::mem::convert_latin1_to_str(self.buf.as_slice(), dst_str.as_mut_str()); diff --git a/napi/src/js_values/string/mod.rs b/napi/src/js_values/string/mod.rs index 0c8182af..cb2010a8 100644 --- a/napi/src/js_values/string/mod.rs +++ b/napi/src/js_values/string/mod.rs @@ -2,7 +2,7 @@ use std::mem; use std::ptr; use super::Value; -use crate::error::check_status; +use crate::check_status; use crate::{sys, Ref, Result}; pub use latin1::JsStringLatin1; @@ -13,41 +13,44 @@ mod latin1; mod utf16; mod utf8; -#[repr(transparent)] -#[derive(Debug, Clone, Copy)] +#[derive(Clone, Copy)] pub struct JsString(pub(crate) Value); impl JsString { + #[inline] pub fn utf8_len(&self) -> Result { let mut length = 0; - check_status(unsafe { + check_status!(unsafe { sys::napi_get_value_string_utf8(self.0.env, self.0.value, ptr::null_mut(), 0, &mut length) })?; Ok(length as usize) } + #[inline] pub fn utf16_len(&self) -> Result { let mut length = 0; - check_status(unsafe { + check_status!(unsafe { sys::napi_get_value_string_utf16(self.0.env, self.0.value, ptr::null_mut(), 0, &mut length) })?; Ok(length as usize) } + #[inline] pub fn latin1_len(&self) -> Result { let mut length = 0; - check_status(unsafe { + check_status!(unsafe { sys::napi_get_value_string_latin1(self.0.env, self.0.value, ptr::null_mut(), 0, &mut length) })?; Ok(length as usize) } + #[inline] pub fn into_utf8(self) -> Result { let mut written_char_count = 0; let len = self.utf8_len()? + 1; let mut result = Vec::with_capacity(len); let buf_ptr = result.as_mut_ptr(); - check_status(unsafe { + check_status!(unsafe { sys::napi_get_value_string_utf8( self.0.env, self.0.value, @@ -71,16 +74,18 @@ impl JsString { }) } + #[inline] pub fn into_utf8_ref(self) -> Result> { Ref::new(self.0, 1, self.into_utf8()?) } + #[inline] pub fn into_utf16(self) -> Result { let mut written_char_count = 0; let len = self.utf16_len()? + 1; let mut result = Vec::with_capacity(len); let buf_ptr = result.as_mut_ptr(); - check_status(unsafe { + check_status!(unsafe { sys::napi_get_value_string_utf16( self.0.env, self.0.value, @@ -99,16 +104,18 @@ impl JsString { }) } + #[inline] pub fn into_utf16_ref(self) -> Result> { Ref::new(self.0, 1, self.into_utf16()?) } + #[inline] pub fn into_latin1(self) -> Result { let mut written_char_count: u64 = 0; let len = self.latin1_len()? + 1; let mut result = Vec::with_capacity(len); let buf_ptr = result.as_mut_ptr(); - check_status(unsafe { + check_status!(unsafe { sys::napi_get_value_string_latin1( self.0.env, self.0.value, @@ -132,6 +139,7 @@ impl JsString { }) } + #[inline] pub fn into_latin1_ref(self) -> Result> { Ref::new(self.0, 1, self.into_latin1()?) } diff --git a/napi/src/js_values/string/utf16.rs b/napi/src/js_values/string/utf16.rs index 250593ff..485d1241 100644 --- a/napi/src/js_values/string/utf16.rs +++ b/napi/src/js_values/string/utf16.rs @@ -10,19 +10,23 @@ pub struct JsStringUtf16 { } impl JsStringUtf16 { + #[inline] pub fn as_str(&self) -> Result { String::from_utf16(self.as_slice()) .map_err(|e| Error::new(Status::InvalidArg, format!("{}", e))) } + #[inline] pub fn as_slice(&self) -> &[u16] { self.buf.as_slice() } + #[inline] pub fn len(&self) -> usize { self.buf.len() } + #[inline] pub fn into_value(self) -> JsString { self.inner } diff --git a/napi/src/js_values/string/utf8.rs b/napi/src/js_values/string/utf8.rs index 6484d646..fbe777c1 100644 --- a/napi/src/js_values/string/utf8.rs +++ b/napi/src/js_values/string/utf8.rs @@ -10,27 +10,33 @@ pub struct JsStringUtf8 { } impl JsStringUtf8 { + #[inline] pub fn as_str(&self) -> Result<&str> { str::from_utf8(self.buf.as_slice()) .map_err(|e| Error::new(Status::InvalidArg, format!("{}", e))) } + #[inline] pub fn as_slice(&self) -> &[u8] { &self.buf } + #[inline] pub fn len(&self) -> usize { self.buf.len() } + #[inline] pub fn to_owned(self) -> Result { Ok(self.as_str()?.to_owned()) } + #[inline] pub fn take(self) -> Vec { self.as_slice().to_vec() } + #[inline] pub fn into_value(self) -> JsString { self.inner } diff --git a/napi/src/js_values/undefined.rs b/napi/src/js_values/undefined.rs index 6fb90619..2d76bf79 100644 --- a/napi/src/js_values/undefined.rs +++ b/napi/src/js_values/undefined.rs @@ -1,5 +1,3 @@ use super::Value; -#[repr(transparent)] -#[derive(Debug)] pub struct JsUndefined(pub(crate) Value); diff --git a/napi/src/js_values/value.rs b/napi/src/js_values/value.rs index 0d575fa8..3559487f 100644 --- a/napi/src/js_values/value.rs +++ b/napi/src/js_values/value.rs @@ -2,7 +2,7 @@ use crate::sys; use super::ValueType; -#[derive(Debug, Clone, Copy)] +#[derive(Clone, Copy)] pub struct Value { pub env: sys::napi_env, pub value: sys::napi_value, diff --git a/napi/src/js_values/value_ref.rs b/napi/src/js_values/value_ref.rs index 4f49957a..bfb37977 100644 --- a/napi/src/js_values/value_ref.rs +++ b/napi/src/js_values/value_ref.rs @@ -14,10 +14,11 @@ unsafe impl Send for Ref {} unsafe impl Sync for Ref {} impl Ref { + #[inline] pub(crate) fn new(js_value: Value, ref_count: u32, inner: T) -> Result> { let mut raw_ref = ptr::null_mut(); let initial_ref_count = 1; - check_status(unsafe { + check_status!(unsafe { sys::napi_create_reference( js_value.env, js_value.value, @@ -32,17 +33,19 @@ impl Ref { }) } + #[inline] pub fn reference(&mut self, env: &Env) -> Result { - check_status(unsafe { sys::napi_reference_ref(env.0, self.raw_ref, &mut self.count) })?; + check_status!(unsafe { sys::napi_reference_ref(env.0, self.raw_ref, &mut self.count) })?; Ok(self.count) } #[must_use] + #[inline] pub fn unref(mut self, env: Env) -> Result { - check_status(unsafe { sys::napi_reference_unref(env.0, self.raw_ref, &mut self.count) })?; + check_status!(unsafe { sys::napi_reference_unref(env.0, self.raw_ref, &mut self.count) })?; if self.count == 0 { - check_status(unsafe { sys::napi_delete_reference(env.0, self.raw_ref) })?; + check_status!(unsafe { sys::napi_delete_reference(env.0, self.raw_ref) })?; } Ok(self.count) } diff --git a/napi/src/lib.rs b/napi/src/lib.rs index 8ce517f4..2dc5ec8d 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -132,6 +132,7 @@ extern crate serde; pub type ContextlessResult = Result>; +/// Deprecated /// register nodejs module /// /// ## Example @@ -143,6 +144,7 @@ pub type ContextlessResult = Result>; /// } /// ``` #[macro_export] +#[deprecated(since = "1.0.0", note = "[module_exports] macro instead")] macro_rules! register_module { ($module_name:ident, $init:ident) => { #[inline] @@ -170,6 +172,10 @@ macro_rules! register_module { #[cfg(all(feature = "tokio_rt", feature = "napi4"))] use $crate::shutdown_tokio_rt; + if cfg!(debug_assertions) { + println!("`register_module` macro will deprecate soon, please migrate to [module_exports]"); + } + 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 }; diff --git a/napi/src/module.rs b/napi/src/module.rs index 96aad4e0..55041b07 100644 --- a/napi/src/module.rs +++ b/napi/src/module.rs @@ -5,6 +5,7 @@ pub struct Module { pub exports: JsObject, } +#[deprecated(since = "1.0.0", note = "[module_exports] macro instead")] impl Module { pub fn create_named_method(&mut self, name: &str, function: Callback) -> Result<()> { self diff --git a/napi/src/promise.rs b/napi/src/promise.rs index 9483a05a..10bbbd51 100644 --- a/napi/src/promise.rs +++ b/napi/src/promise.rs @@ -2,8 +2,7 @@ use futures::prelude::*; use std::os::raw::{c_char, c_void}; use std::ptr; -use crate::error::check_status; -use crate::{sys, Env, NapiValue, Result}; +use crate::{check_status, sys, Env, NapiValue, Result}; pub struct FuturePromise { deferred: sys::napi_deferred, @@ -16,6 +15,7 @@ pub struct FuturePromise { unsafe impl Send for FuturePromise {} impl FuturePromise { + #[inline] pub fn create( env: sys::napi_env, raw_deferred: sys::napi_deferred, @@ -23,7 +23,7 @@ impl FuturePromise { ) -> Result { let mut async_resource_name = ptr::null_mut(); let s = "napi_resolve_promise_from_future"; - check_status(unsafe { + check_status!(unsafe { sys::napi_create_string_utf8( env, s.as_ptr() as *const c_char, @@ -41,13 +41,14 @@ impl FuturePromise { }) } + #[inline] pub(crate) fn start(self) -> Result { let mut tsfn_value = ptr::null_mut(); let async_resource_name = self.async_resource_name; let initial_thread_count = 1; let env = self.env; let self_ref = Box::leak(Box::from(self)); - check_status(unsafe { + check_status!(unsafe { sys::napi_create_threadsafe_function( env, ptr::null_mut(), @@ -77,9 +78,9 @@ pub(crate) async fn resolve_from_future>>( fut: F, ) { let val = fut.await; - check_status(unsafe { sys::napi_acquire_threadsafe_function(tsfn_value.0) }) + check_status!(unsafe { sys::napi_acquire_threadsafe_function(tsfn_value.0) }) .expect("Failed to acquire thread safe function"); - check_status(unsafe { + check_status!(unsafe { sys::napi_call_threadsafe_function( tsfn_value.0, Box::into_raw(Box::from(val)) as *mut _ as *mut c_void, @@ -112,7 +113,7 @@ unsafe extern "C" fn call_js_cb( debug_assert!(status == sys::Status::napi_ok, "Reject promise failed"); } }; - check_status(sys::napi_release_threadsafe_function( + check_status!(sys::napi_release_threadsafe_function( tsfn, sys::napi_threadsafe_function_release_mode::napi_tsfn_release, )) diff --git a/napi/src/threadsafe_function.rs b/napi/src/threadsafe_function.rs index 2fc88cd2..0d2a4184 100644 --- a/napi/src/threadsafe_function.rs +++ b/napi/src/threadsafe_function.rs @@ -5,8 +5,7 @@ use std::ptr; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; -use crate::error::check_status; -use crate::{sys, Env, Error, JsFunction, NapiValue, Result, Status}; +use crate::{check_status, sys, Env, Error, JsFunction, NapiValue, Result, Status}; use sys::napi_threadsafe_function_call_mode; @@ -86,7 +85,6 @@ pub struct ThreadsafeFunction { unsafe impl Send for ThreadsafeFunction {} unsafe impl Sync for ThreadsafeFunction {} -#[repr(transparent)] struct ThreadSafeContext( Box) -> Result>>, ); @@ -94,7 +92,7 @@ struct ThreadSafeContext( impl ThreadsafeFunction { /// See [napi_create_threadsafe_function](https://nodejs.org/api/n-api.html#n_api_napi_create_threadsafe_function) /// for more information. - #[inline(always)] + #[inline] pub fn create< V: NapiValue, R: 'static + Send + FnMut(ThreadSafeCallContext) -> Result>, @@ -106,7 +104,7 @@ impl ThreadsafeFunction { ) -> Result { let mut async_resource_name = ptr::null_mut(); let s = "napi_rs_threadsafe_function"; - check_status(unsafe { + check_status!(unsafe { sys::napi_create_string_utf8( env, s.as_ptr() as *const c_char, @@ -119,7 +117,7 @@ impl ThreadsafeFunction { let mut raw_tsfn = ptr::null_mut(); let context = ThreadSafeContext(Box::from(callback)); let ptr = Box::into_raw(Box::new(context)) as *mut _; - check_status(unsafe { + check_status!(unsafe { sys::napi_create_threadsafe_function( env, func.0.value, @@ -169,7 +167,7 @@ impl ThreadsafeFunction { format!("Can not ref, Thread safe function already aborted"), )); } - check_status(unsafe { sys::napi_ref_threadsafe_function(env.0, self.raw_tsfn) }) + check_status!(unsafe { sys::napi_ref_threadsafe_function(env.0, self.raw_tsfn) }) } /// See [napi_unref_threadsafe_function](https://nodejs.org/api/n-api.html#n_api_napi_unref_threadsafe_function) @@ -181,7 +179,7 @@ impl ThreadsafeFunction { format!("Can not unref, Thread safe function already aborted"), )); } - check_status(unsafe { sys::napi_unref_threadsafe_function(env.0, self.raw_tsfn) }) + check_status!(unsafe { sys::napi_unref_threadsafe_function(env.0, self.raw_tsfn) }) } pub fn aborted(&self) -> bool { @@ -189,7 +187,7 @@ impl ThreadsafeFunction { } pub fn abort(self) -> Result<()> { - check_status(unsafe { + check_status!(unsafe { sys::napi_release_threadsafe_function( self.raw_tsfn, sys::napi_threadsafe_function_release_mode::napi_tsfn_abort, @@ -206,7 +204,7 @@ impl ThreadsafeFunction { format!("Can not clone, Thread safe function already aborted"), )); } - check_status(unsafe { sys::napi_acquire_threadsafe_function(self.raw_tsfn) })?; + check_status!(unsafe { sys::napi_acquire_threadsafe_function(self.raw_tsfn) })?; Ok(Self { raw_tsfn: self.raw_tsfn, aborted: Arc::clone(&self.aborted), diff --git a/sys/src/lib.rs b/sys/src/lib.rs index 321d51f0..c04452a9 100644 --- a/sys/src/lib.rs +++ b/sys/src/lib.rs @@ -3,8 +3,10 @@ #![allow(non_snake_case)] #![allow(dead_code)] +use std::os::raw::{c_char, c_int, c_uint, c_void}; + #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct napi_env__ { _unused: [u8; 0], } @@ -12,7 +14,7 @@ pub struct napi_env__ { /// Env ptr pub type napi_env = *mut napi_env__; #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct napi_value__ { _unused: [u8; 0], } @@ -20,43 +22,43 @@ pub struct napi_value__ { /// JsValue ptr pub type napi_value = *mut napi_value__; #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct napi_ref__ { _unused: [u8; 0], } pub type napi_ref = *mut napi_ref__; #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct napi_handle_scope__ { _unused: [u8; 0], } pub type napi_handle_scope = *mut napi_handle_scope__; #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct napi_escapable_handle_scope__ { _unused: [u8; 0], } pub type napi_escapable_handle_scope = *mut napi_escapable_handle_scope__; #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct napi_callback_info__ { _unused: [u8; 0], } pub type napi_callback_info = *mut napi_callback_info__; #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct napi_deferred__ { _unused: [u8; 0], } #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct uv_loop_s { _unused: [u8; 0], } pub type napi_deferred = *mut napi_deferred__; #[repr(C)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub enum napi_property_attributes { napi_default = 0, napi_writable = 1 << 0, @@ -127,47 +129,42 @@ pub mod Status { pub const napi_would_deadlock: i32 = 21; // unused } -pub type napi_callback = ::std::option::Option< - unsafe extern "C" fn(env: napi_env, info: napi_callback_info) -> napi_value, ->; -pub type napi_finalize = ::std::option::Option< - unsafe extern "C" fn( - env: napi_env, - finalize_data: *mut ::std::os::raw::c_void, - finalize_hint: *mut ::std::os::raw::c_void, - ), +pub type napi_callback = + Option napi_value>; +pub type napi_finalize = Option< + unsafe extern "C" fn(env: napi_env, finalize_data: *mut c_void, finalize_hint: *mut c_void), >; #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct napi_property_descriptor { - pub utf8name: *const ::std::os::raw::c_char, + pub utf8name: *const c_char, pub name: napi_value, pub method: napi_callback, pub getter: napi_callback, pub setter: napi_callback, pub value: napi_value, pub attributes: napi_property_attributes, - pub data: *mut ::std::os::raw::c_void, + pub data: *mut c_void, } #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Copy, Clone)] pub struct napi_extended_error_info { - pub error_message: *const ::std::os::raw::c_char, - pub engine_reserved: *mut ::std::os::raw::c_void, + pub error_message: *const c_char, + pub engine_reserved: *mut c_void, pub engine_error_code: u32, pub error_code: napi_status, } #[repr(C)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub enum napi_key_collection_mode { napi_key_include_prototypes, napi_key_own_only, } #[repr(C)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub enum napi_key_filter { napi_key_all_properties = 0, napi_key_writable = 1, @@ -178,7 +175,7 @@ pub enum napi_key_filter { } #[repr(C)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub enum napi_key_conversion { napi_key_keep_numbers, napi_key_numbers_to_strings, @@ -189,146 +186,91 @@ extern "C" { env: napi_env, result: *mut *const napi_extended_error_info, ) -> napi_status; -} -extern "C" { + pub fn napi_get_undefined(env: napi_env, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_get_null(env: napi_env, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_get_global(env: napi_env, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_get_boolean(env: napi_env, value: bool, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_create_object(env: napi_env, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_create_array(env: napi_env, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_create_array_with_length( env: napi_env, length: usize, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_double(env: napi_env, value: f64, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_create_int32(env: napi_env, value: i32, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_create_uint32(env: napi_env, value: u32, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_create_int64(env: napi_env, value: i64, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_create_string_latin1( env: napi_env, - str_: *const ::std::os::raw::c_char, + str_: *const c_char, length: usize, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_string_utf8( env: napi_env, - str_: *const ::std::os::raw::c_char, + str_: *const c_char, length: usize, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_string_utf16( env: napi_env, str_: *const u16, length: usize, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_symbol( env: napi_env, description: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_function( env: napi_env, - utf8name: *const ::std::os::raw::c_char, + utf8name: *const c_char, length: usize, cb: napi_callback, - data: *mut ::std::os::raw::c_void, + data: *mut c_void, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_error( env: napi_env, code: napi_value, msg: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_type_error( env: napi_env, code: napi_value, msg: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_range_error( env: napi_env, code: napi_value, msg: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_typeof(env: napi_env, value: napi_value, result: *mut napi_valuetype) -> napi_status; -} -extern "C" { pub fn napi_get_value_double(env: napi_env, value: napi_value, result: *mut f64) -> napi_status; -} -extern "C" { pub fn napi_get_value_int32(env: napi_env, value: napi_value, result: *mut i32) -> napi_status; -} -extern "C" { pub fn napi_get_value_uint32(env: napi_env, value: napi_value, result: *mut u32) -> napi_status; -} -extern "C" { pub fn napi_get_value_int64(env: napi_env, value: napi_value, result: *mut i64) -> napi_status; -} -extern "C" { pub fn napi_get_value_bool(env: napi_env, value: napi_value, result: *mut bool) -> napi_status; -} -extern "C" { pub fn napi_get_value_string_latin1( env: napi_env, value: napi_value, - buf: *mut ::std::os::raw::c_char, + buf: *mut c_char, bufsize: usize, result: *mut usize, ) -> napi_status; -} -extern "C" { pub fn napi_get_value_string_utf8( env: napi_env, value: napi_value, - buf: *mut ::std::os::raw::c_char, + buf: *mut c_char, bufsize: usize, result: *mut usize, ) -> napi_status; -} -extern "C" { pub fn napi_get_value_string_utf16( env: napi_env, value: napi_value, @@ -336,168 +278,122 @@ extern "C" { bufsize: usize, result: *mut usize, ) -> napi_status; -} -extern "C" { pub fn napi_coerce_to_bool( env: napi_env, value: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_coerce_to_number( env: napi_env, value: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_coerce_to_object( env: napi_env, value: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_coerce_to_string( env: napi_env, value: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_get_prototype( env: napi_env, object: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_get_property_names( env: napi_env, object: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_set_property( env: napi_env, object: napi_value, key: napi_value, value: napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_has_property( env: napi_env, object: napi_value, key: napi_value, result: *mut bool, ) -> napi_status; -} -extern "C" { pub fn napi_get_property( env: napi_env, object: napi_value, key: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_delete_property( env: napi_env, object: napi_value, key: napi_value, result: *mut bool, ) -> napi_status; -} -extern "C" { pub fn napi_has_own_property( env: napi_env, object: napi_value, key: napi_value, result: *mut bool, ) -> napi_status; -} -extern "C" { pub fn napi_set_named_property( env: napi_env, object: napi_value, - utf8name: *const ::std::os::raw::c_char, + utf8name: *const c_char, value: napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_has_named_property( env: napi_env, object: napi_value, - utf8name: *const ::std::os::raw::c_char, + utf8name: *const c_char, result: *mut bool, ) -> napi_status; -} -extern "C" { pub fn napi_get_named_property( env: napi_env, object: napi_value, - utf8name: *const ::std::os::raw::c_char, + utf8name: *const c_char, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_set_element( env: napi_env, object: napi_value, index: u32, value: napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_has_element( env: napi_env, object: napi_value, index: u32, result: *mut bool, ) -> napi_status; -} -extern "C" { pub fn napi_get_element( env: napi_env, object: napi_value, index: u32, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_delete_element( env: napi_env, object: napi_value, index: u32, result: *mut bool, ) -> napi_status; -} -extern "C" { pub fn napi_define_properties( env: napi_env, object: napi_value, property_count: usize, properties: *const napi_property_descriptor, ) -> napi_status; -} -extern "C" { pub fn napi_is_array(env: napi_env, value: napi_value, result: *mut bool) -> napi_status; -} -extern "C" { pub fn napi_get_array_length(env: napi_env, value: napi_value, result: *mut u32) -> napi_status; -} -extern "C" { pub fn napi_strict_equals( env: napi_env, lhs: napi_value, rhs: napi_value, result: *mut bool, ) -> napi_status; -} -extern "C" { pub fn napi_call_function( env: napi_env, recv: napi_value, @@ -506,8 +402,6 @@ extern "C" { argv: *const napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_new_instance( env: napi_env, constructor: napi_value, @@ -515,200 +409,129 @@ extern "C" { argv: *const napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_instanceof( env: napi_env, object: napi_value, constructor: napi_value, result: *mut bool, ) -> napi_status; -} -extern "C" { pub fn napi_get_cb_info( env: napi_env, cbinfo: napi_callback_info, argc: *mut usize, argv: *mut napi_value, this_arg: *mut napi_value, - data: *mut *mut ::std::os::raw::c_void, + data: *mut *mut c_void, ) -> napi_status; -} -extern "C" { pub fn napi_get_new_target( env: napi_env, cbinfo: napi_callback_info, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_define_class( env: napi_env, - utf8name: *const ::std::os::raw::c_char, + utf8name: *const c_char, length: usize, constructor: napi_callback, - data: *mut ::std::os::raw::c_void, + data: *mut c_void, property_count: usize, properties: *const napi_property_descriptor, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_wrap( env: napi_env, js_object: napi_value, - native_object: *mut ::std::os::raw::c_void, + native_object: *mut c_void, finalize_cb: napi_finalize, - finalize_hint: *mut ::std::os::raw::c_void, + finalize_hint: *mut c_void, result: *mut napi_ref, ) -> napi_status; -} -extern "C" { - pub fn napi_unwrap( - env: napi_env, - js_object: napi_value, - result: *mut *mut ::std::os::raw::c_void, - ) -> napi_status; -} -extern "C" { + pub fn napi_unwrap(env: napi_env, js_object: napi_value, result: *mut *mut c_void) + -> napi_status; pub fn napi_remove_wrap( env: napi_env, js_object: napi_value, - result: *mut *mut ::std::os::raw::c_void, + result: *mut *mut c_void, ) -> napi_status; -} -extern "C" { pub fn napi_create_external( env: napi_env, - data: *mut ::std::os::raw::c_void, + data: *mut c_void, finalize_cb: napi_finalize, - finalize_hint: *mut ::std::os::raw::c_void, + finalize_hint: *mut c_void, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_get_value_external( env: napi_env, value: napi_value, - result: *mut *mut ::std::os::raw::c_void, + result: *mut *mut c_void, ) -> napi_status; -} -extern "C" { pub fn napi_create_reference( env: napi_env, value: napi_value, initial_refcount: u32, result: *mut napi_ref, ) -> napi_status; -} -extern "C" { pub fn napi_delete_reference(env: napi_env, ref_: napi_ref) -> napi_status; -} -extern "C" { pub fn napi_reference_ref(env: napi_env, ref_: napi_ref, result: *mut u32) -> napi_status; -} -extern "C" { pub fn napi_reference_unref(env: napi_env, ref_: napi_ref, result: *mut u32) -> napi_status; -} -extern "C" { pub fn napi_get_reference_value( env: napi_env, ref_: napi_ref, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_open_handle_scope(env: napi_env, result: *mut napi_handle_scope) -> napi_status; -} -extern "C" { pub fn napi_close_handle_scope(env: napi_env, scope: napi_handle_scope) -> napi_status; -} -extern "C" { pub fn napi_open_escapable_handle_scope( env: napi_env, result: *mut napi_escapable_handle_scope, ) -> napi_status; -} -extern "C" { pub fn napi_close_escapable_handle_scope( env: napi_env, scope: napi_escapable_handle_scope, ) -> napi_status; -} -extern "C" { pub fn napi_escape_handle( env: napi_env, scope: napi_escapable_handle_scope, escapee: napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_throw(env: napi_env, error: napi_value) -> napi_status; -} -extern "C" { - pub fn napi_throw_error( - env: napi_env, - code: *const ::std::os::raw::c_char, - msg: *const ::std::os::raw::c_char, - ) -> napi_status; -} -extern "C" { + pub fn napi_throw_error(env: napi_env, code: *const c_char, msg: *const c_char) -> napi_status; pub fn napi_throw_type_error( env: napi_env, - code: *const ::std::os::raw::c_char, - msg: *const ::std::os::raw::c_char, + code: *const c_char, + msg: *const c_char, ) -> napi_status; -} -extern "C" { pub fn napi_throw_range_error( env: napi_env, - code: *const ::std::os::raw::c_char, - msg: *const ::std::os::raw::c_char, + code: *const c_char, + msg: *const c_char, ) -> napi_status; -} -extern "C" { pub fn napi_is_error(env: napi_env, value: napi_value, result: *mut bool) -> napi_status; -} -extern "C" { pub fn napi_is_exception_pending(env: napi_env, result: *mut bool) -> napi_status; -} -extern "C" { pub fn napi_get_and_clear_last_exception(env: napi_env, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_is_arraybuffer(env: napi_env, value: napi_value, result: *mut bool) -> napi_status; -} -extern "C" { pub fn napi_create_arraybuffer( env: napi_env, byte_length: usize, - data: *mut *mut ::std::os::raw::c_void, + data: *mut *mut c_void, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_external_arraybuffer( env: napi_env, - external_data: *mut ::std::os::raw::c_void, + external_data: *mut c_void, byte_length: usize, finalize_cb: napi_finalize, - finalize_hint: *mut ::std::os::raw::c_void, + finalize_hint: *mut c_void, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_get_arraybuffer_info( env: napi_env, arraybuffer: napi_value, - data: *mut *mut ::std::os::raw::c_void, + data: *mut *mut c_void, byte_length: *mut usize, ) -> napi_status; -} -extern "C" { pub fn napi_is_typedarray(env: napi_env, value: napi_value, result: *mut bool) -> napi_status; -} -extern "C" { pub fn napi_create_typedarray( env: napi_env, type_: napi_typedarray_type, @@ -717,19 +540,15 @@ extern "C" { byte_offset: usize, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_get_typedarray_info( env: napi_env, typedarray: napi_value, type_: *mut napi_typedarray_type, length: *mut usize, - data: *mut *mut ::std::os::raw::c_void, + data: *mut *mut c_void, arraybuffer: *mut napi_value, byte_offset: *mut usize, ) -> napi_status; -} -extern "C" { pub fn napi_create_dataview( env: napi_env, length: usize, @@ -737,295 +556,53 @@ extern "C" { byte_offset: usize, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_is_dataview(env: napi_env, value: napi_value, result: *mut bool) -> napi_status; -} -extern "C" { pub fn napi_get_dataview_info( env: napi_env, dataview: napi_value, bytelength: *mut usize, - data: *mut *mut ::std::os::raw::c_void, + data: *mut *mut c_void, arraybuffer: *mut napi_value, byte_offset: *mut usize, ) -> napi_status; -} -extern "C" { pub fn napi_get_version(env: napi_env, result: *mut u32) -> napi_status; -} -extern "C" { pub fn napi_create_promise( env: napi_env, deferred: *mut napi_deferred, promise: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_resolve_deferred( env: napi_env, deferred: napi_deferred, resolution: napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_reject_deferred( env: napi_env, deferred: napi_deferred, rejection: napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_is_promise(env: napi_env, value: napi_value, is_promise: *mut bool) -> napi_status; -} -extern "C" { pub fn napi_run_script(env: napi_env, script: napi_value, result: *mut napi_value) -> napi_status; -} -extern "C" { pub fn napi_adjust_external_memory( env: napi_env, change_in_bytes: i64, adjusted_value: *mut i64, ) -> napi_status; -} - -#[cfg(feature = "napi5")] -extern "C" { - pub fn napi_create_date(env: napi_env, time: f64, result: *mut napi_value) -> napi_status; -} - -#[cfg(feature = "napi5")] -extern "C" { - pub fn napi_is_date(env: napi_env, value: napi_value, is_date: *mut bool) -> napi_status; -} - -#[cfg(feature = "napi5")] -extern "C" { - pub fn napi_get_date_value(env: napi_env, value: napi_value, result: *mut f64) -> napi_status; -} - -#[cfg(feature = "napi5")] -extern "C" { - pub fn napi_add_finalizer( - env: napi_env, - js_object: napi_value, - native_object: *mut ::std::os::raw::c_void, - finalize_cb: napi_finalize, - finalize_hint: *mut ::std::os::raw::c_void, - result: *mut napi_ref, - ) -> napi_status; -} - -#[cfg(feature = "napi6")] -extern "C" { - pub fn napi_create_bigint_int64( - env: napi_env, - value: i64, - result: *mut napi_value, - ) -> napi_status; -} - -#[cfg(feature = "napi6")] -extern "C" { - pub fn napi_create_bigint_uint64( - env: napi_env, - value: u64, - result: *mut napi_value, - ) -> napi_status; -} - -#[cfg(feature = "napi6")] -extern "C" { - pub fn napi_create_bigint_words( - env: napi_env, - sign_bit: ::std::os::raw::c_int, - word_count: usize, - words: *const u64, - result: *mut napi_value, - ) -> napi_status; -} - -#[cfg(feature = "napi6")] -extern "C" { - pub fn napi_get_value_bigint_int64( - env: napi_env, - value: napi_value, - result: *mut i64, - lossless: *mut bool, - ) -> napi_status; -} - -#[cfg(feature = "napi6")] -extern "C" { - pub fn napi_get_value_bigint_uint64( - env: napi_env, - value: napi_value, - result: *mut u64, - lossless: *mut bool, - ) -> napi_status; -} - -#[cfg(feature = "napi6")] -extern "C" { - pub fn napi_get_value_bigint_words( - env: napi_env, - value: napi_value, - sign_bit: *mut ::std::os::raw::c_int, - word_count: *mut usize, - words: *mut u64, - ) -> napi_status; -} - -#[cfg(feature = "napi6")] -extern "C" { - pub fn napi_get_all_property_names( - env: napi_env, - object: napi_value, - key_mode: napi_key_collection_mode, - key_filter: napi_key_filter, - key_conversion: napi_key_conversion, - result: *mut napi_value, - ) -> napi_status; -} - -#[cfg(feature = "napi6")] -extern "C" { - pub fn napi_set_instance_data( - env: napi_env, - data: *mut ::std::os::raw::c_void, - finalize_cb: napi_finalize, - finalize_hint: *mut ::std::os::raw::c_void, - ) -> napi_status; -} - -#[cfg(feature = "napi6")] -extern "C" { - pub fn napi_get_instance_data( - env: napi_env, - data: *mut *mut ::std::os::raw::c_void, - ) -> napi_status; -} - -#[cfg(feature = "napi7")] -extern "C" { - pub fn napi_detach_arraybuffer(env: napi_env, arraybuffer: napi_value) -> napi_status; -} - -#[cfg(feature = "napi7")] -extern "C" { - pub fn napi_is_detached_arraybuffer( - env: napi_env, - value: napi_value, - result: *mut bool, - ) -> napi_status; -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct napi_callback_scope__ { - _unused: [u8; 0], -} -pub type napi_callback_scope = *mut napi_callback_scope__; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct napi_async_context__ { - _unused: [u8; 0], -} -pub type napi_async_context = *mut napi_async_context__; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct napi_async_work__ { - _unused: [u8; 0], -} -pub type napi_async_work = *mut napi_async_work__; - -#[cfg(feature = "napi4")] -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct napi_threadsafe_function__ { - _unused: [u8; 0], -} - -#[cfg(feature = "napi4")] -pub type napi_threadsafe_function = *mut napi_threadsafe_function__; - -#[cfg(feature = "napi4")] -#[repr(C)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub enum napi_threadsafe_function_release_mode { - napi_tsfn_release, - napi_tsfn_abort, -} - -#[cfg(feature = "napi4")] -#[repr(C)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub enum napi_threadsafe_function_call_mode { - napi_tsfn_nonblocking, - napi_tsfn_blocking, -} - -pub type napi_async_execute_callback = - ::std::option::Option; -pub type napi_async_complete_callback = ::std::option::Option< - unsafe extern "C" fn(env: napi_env, status: napi_status, data: *mut ::std::os::raw::c_void), ->; - -#[cfg(feature = "napi4")] -pub type napi_threadsafe_function_call_js = ::std::option::Option< - unsafe extern "C" fn( - env: napi_env, - js_callback: napi_value, - context: *mut ::std::os::raw::c_void, - data: *mut ::std::os::raw::c_void, - ), ->; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct napi_node_version { - pub major: u32, - pub minor: u32, - pub patch: u32, - pub release: *const ::std::os::raw::c_char, -} - -pub type napi_addon_register_func = - ::std::option::Option napi_value>; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct napi_module { - pub nm_version: ::std::os::raw::c_int, - pub nm_flags: ::std::os::raw::c_uint, - pub nm_filename: *const ::std::os::raw::c_char, - pub nm_register_func: napi_addon_register_func, - pub nm_modname: *const ::std::os::raw::c_char, - pub nm_priv: *mut ::std::os::raw::c_void, - pub reserved: [*mut ::std::os::raw::c_void; 4usize], -} - -extern "C" { pub fn napi_module_register(mod_: *mut napi_module); -} -extern "C" { pub fn napi_fatal_error( - location: *const ::std::os::raw::c_char, + location: *const c_char, location_len: usize, - message: *const ::std::os::raw::c_char, + message: *const c_char, message_len: usize, ); -} -extern "C" { pub fn napi_async_init( env: napi_env, async_resource: napi_value, async_resource_name: napi_value, result: *mut napi_async_context, ) -> napi_status; -} -extern "C" { pub fn napi_async_destroy(env: napi_env, async_context: napi_async_context) -> napi_status; -} -extern "C" { pub fn napi_make_callback( env: napi_env, async_context: napi_async_context, @@ -1035,66 +612,46 @@ extern "C" { argv: *const napi_value, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_buffer( env: napi_env, length: usize, - data: *mut *mut ::std::os::raw::c_void, + data: *mut *mut c_void, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_external_buffer( env: napi_env, length: usize, - data: *mut ::std::os::raw::c_void, + data: *mut c_void, finalize_cb: napi_finalize, - finalize_hint: *mut ::std::os::raw::c_void, + finalize_hint: *mut c_void, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_create_buffer_copy( env: napi_env, length: usize, - data: *const ::std::os::raw::c_void, - result_data: *mut *mut ::std::os::raw::c_void, + data: *const c_void, + result_data: *mut *mut c_void, result: *mut napi_value, ) -> napi_status; -} -extern "C" { pub fn napi_is_buffer(env: napi_env, value: napi_value, result: *mut bool) -> napi_status; -} -extern "C" { pub fn napi_get_buffer_info( env: napi_env, value: napi_value, - data: *mut *mut ::std::os::raw::c_void, + data: *mut *mut c_void, length: *mut usize, ) -> napi_status; -} -extern "C" { pub fn napi_create_async_work( env: napi_env, async_resource: napi_value, async_resource_name: napi_value, execute: napi_async_execute_callback, complete: napi_async_complete_callback, - data: *mut ::std::os::raw::c_void, + data: *mut c_void, result: *mut napi_async_work, ) -> napi_status; -} -extern "C" { pub fn napi_delete_async_work(env: napi_env, work: napi_async_work) -> napi_status; -} -extern "C" { pub fn napi_queue_async_work(env: napi_env, work: napi_async_work) -> napi_status; -} -extern "C" { pub fn napi_cancel_async_work(env: napi_env, work: napi_async_work) -> napi_status; -} -extern "C" { pub fn napi_get_node_version( env: napi_env, version: *mut *const napi_node_version, @@ -1109,34 +666,22 @@ extern "C" { #[cfg(feature = "napi3")] extern "C" { pub fn napi_fatal_exception(env: napi_env, err: napi_value) -> napi_status; -} -#[cfg(feature = "napi3")] -extern "C" { pub fn napi_add_env_cleanup_hook( env: napi_env, - fun: ::std::option::Option, - arg: *mut ::std::os::raw::c_void, + fun: Option, + arg: *mut c_void, ) -> napi_status; -} -#[cfg(feature = "napi3")] -extern "C" { pub fn napi_remove_env_cleanup_hook( env: napi_env, - fun: ::std::option::Option, - arg: *mut ::std::os::raw::c_void, + fun: Option, + arg: *mut c_void, ) -> napi_status; -} -#[cfg(feature = "napi3")] -extern "C" { pub fn napi_open_callback_scope( env: napi_env, resource_object: napi_value, context: napi_async_context, result: *mut napi_callback_scope, ) -> napi_status; -} -#[cfg(feature = "napi3")] -extern "C" { pub fn napi_close_callback_scope(env: napi_env, scope: napi_callback_scope) -> napi_status; } @@ -1149,48 +694,191 @@ extern "C" { async_resource_name: napi_value, max_queue_size: usize, initial_thread_count: usize, - thread_finalize_data: *mut ::std::os::raw::c_void, + thread_finalize_data: *mut c_void, thread_finalize_cb: napi_finalize, - context: *mut ::std::os::raw::c_void, + context: *mut c_void, call_js_cb: napi_threadsafe_function_call_js, result: *mut napi_threadsafe_function, ) -> napi_status; -} -#[cfg(feature = "napi4")] -extern "C" { pub fn napi_get_threadsafe_function_context( func: napi_threadsafe_function, - result: *mut *mut ::std::os::raw::c_void, + result: *mut *mut c_void, ) -> napi_status; -} -#[cfg(feature = "napi4")] -extern "C" { pub fn napi_call_threadsafe_function( func: napi_threadsafe_function, - data: *mut ::std::os::raw::c_void, + data: *mut c_void, is_blocking: napi_threadsafe_function_call_mode, ) -> napi_status; -} -#[cfg(feature = "napi4")] -extern "C" { pub fn napi_acquire_threadsafe_function(func: napi_threadsafe_function) -> napi_status; -} -#[cfg(feature = "napi4")] -extern "C" { pub fn napi_release_threadsafe_function( func: napi_threadsafe_function, mode: napi_threadsafe_function_release_mode, ) -> napi_status; -} -#[cfg(feature = "napi4")] -extern "C" { pub fn napi_unref_threadsafe_function( env: napi_env, func: napi_threadsafe_function, ) -> napi_status; -} -#[cfg(feature = "napi4")] -extern "C" { pub fn napi_ref_threadsafe_function(env: napi_env, func: napi_threadsafe_function) -> napi_status; } + +#[cfg(feature = "napi5")] +extern "C" { + pub fn napi_create_date(env: napi_env, time: f64, result: *mut napi_value) -> napi_status; + pub fn napi_is_date(env: napi_env, value: napi_value, is_date: *mut bool) -> napi_status; + pub fn napi_get_date_value(env: napi_env, value: napi_value, result: *mut f64) -> napi_status; + pub fn napi_add_finalizer( + env: napi_env, + js_object: napi_value, + native_object: *mut c_void, + finalize_cb: napi_finalize, + finalize_hint: *mut c_void, + result: *mut napi_ref, + ) -> napi_status; +} + +#[cfg(feature = "napi6")] +extern "C" { + pub fn napi_create_bigint_int64( + env: napi_env, + value: i64, + result: *mut napi_value, + ) -> napi_status; + pub fn napi_create_bigint_uint64( + env: napi_env, + value: u64, + result: *mut napi_value, + ) -> napi_status; + pub fn napi_create_bigint_words( + env: napi_env, + sign_bit: c_int, + word_count: usize, + words: *const u64, + result: *mut napi_value, + ) -> napi_status; + pub fn napi_get_value_bigint_int64( + env: napi_env, + value: napi_value, + result: *mut i64, + lossless: *mut bool, + ) -> napi_status; + pub fn napi_get_value_bigint_uint64( + env: napi_env, + value: napi_value, + result: *mut u64, + lossless: *mut bool, + ) -> napi_status; + pub fn napi_get_value_bigint_words( + env: napi_env, + value: napi_value, + sign_bit: *mut c_int, + word_count: *mut usize, + words: *mut u64, + ) -> napi_status; + pub fn napi_get_all_property_names( + env: napi_env, + object: napi_value, + key_mode: napi_key_collection_mode, + key_filter: napi_key_filter, + key_conversion: napi_key_conversion, + result: *mut napi_value, + ) -> napi_status; + pub fn napi_set_instance_data( + env: napi_env, + data: *mut c_void, + finalize_cb: napi_finalize, + finalize_hint: *mut c_void, + ) -> napi_status; + pub fn napi_get_instance_data(env: napi_env, data: *mut *mut c_void) -> napi_status; +} + +#[cfg(feature = "napi7")] +extern "C" { + pub fn napi_detach_arraybuffer(env: napi_env, arraybuffer: napi_value) -> napi_status; + pub fn napi_is_detached_arraybuffer( + env: napi_env, + value: napi_value, + result: *mut bool, + ) -> napi_status; +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct napi_callback_scope__ { + _unused: [u8; 0], +} +pub type napi_callback_scope = *mut napi_callback_scope__; +#[repr(C)] +#[derive(Copy, Clone)] +pub struct napi_async_context__ { + _unused: [u8; 0], +} +pub type napi_async_context = *mut napi_async_context__; +#[repr(C)] +#[derive(Copy, Clone)] +pub struct napi_async_work__ { + _unused: [u8; 0], +} +pub type napi_async_work = *mut napi_async_work__; + +#[cfg(feature = "napi4")] +#[repr(C)] +#[derive(Copy, Clone)] +pub struct napi_threadsafe_function__ { + _unused: [u8; 0], +} + +#[cfg(feature = "napi4")] +pub type napi_threadsafe_function = *mut napi_threadsafe_function__; + +#[cfg(feature = "napi4")] +#[repr(C)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] +pub enum napi_threadsafe_function_release_mode { + napi_tsfn_release, + napi_tsfn_abort, +} + +#[cfg(feature = "napi4")] +#[repr(C)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] +pub enum napi_threadsafe_function_call_mode { + napi_tsfn_nonblocking, + napi_tsfn_blocking, +} + +pub type napi_async_execute_callback = + Option; +pub type napi_async_complete_callback = + Option; + +#[cfg(feature = "napi4")] +pub type napi_threadsafe_function_call_js = Option< + unsafe extern "C" fn( + env: napi_env, + js_callback: napi_value, + context: *mut c_void, + data: *mut c_void, + ), +>; +#[repr(C)] +#[derive(Copy, Clone)] +pub struct napi_node_version { + pub major: u32, + pub minor: u32, + pub patch: u32, + pub release: *const c_char, +} + +pub type napi_addon_register_func = + Option napi_value>; +#[repr(C)] +#[derive(Copy, Clone)] +pub struct napi_module { + pub nm_version: c_int, + pub nm_flags: c_uint, + pub nm_filename: *const c_char, + pub nm_register_func: napi_addon_register_func, + pub nm_modname: *const c_char, + pub nm_priv: *mut c_void, + pub reserved: [*mut c_void; 4usize], +} diff --git a/test_module/Cargo.toml b/test_module/Cargo.toml index e44734a9..df29d9cb 100644 --- a/test_module/Cargo.toml +++ b/test_module/Cargo.toml @@ -23,3 +23,6 @@ tokio = {version = "0.3", features = ["default", "fs"]} [build-dependencies] napi-build = {path = "../build"} + +[profile.release] +lto = true diff --git a/test_module/package.json b/test_module/package.json index 9e3c1605..b7bb9b0e 100644 --- a/test_module/package.json +++ b/test_module/package.json @@ -6,6 +6,7 @@ "build-napi3": "cargo build --features \"napi3\" && node ../scripts/index.js build", "build-aarch64": "cargo build --features \"latest\" --target aarch64-unknown-linux-gnu && node ../scripts/index.js build --target-triple aarch64-unknown-linux-gnu", "build-i686": "cargo build --features \"latest\" --target i686-pc-windows-msvc && node ../scripts/index.js build --target-triple i686-pc-windows-msvc", + "build-i686-release": "cargo build --release --features \"latest\" --target i686-pc-windows-msvc && node ../scripts/index.js build --release --target-triple i686-pc-windows-msvc", "build-release": "cargo build --features \"latest\" --release && node ../scripts/index.js build --release", "test": "node ./index.js" } diff --git a/test_module/src/array.rs b/test_module/src/array.rs index 08208394..2348320b 100644 --- a/test_module/src/array.rs +++ b/test_module/src/array.rs @@ -2,7 +2,7 @@ use std::convert::TryInto; use napi::{ CallContext, ContextlessResult, Env, JsBoolean, JsNumber, JsObject, JsUndefined, JsUnknown, - Module, Result, + Result, }; #[contextless_function] @@ -43,12 +43,12 @@ fn test_delete_element(ctx: CallContext) -> Result { ctx.env.get_boolean(arr.delete_element(index.try_into()?)?) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("testCreateArray", test_create_array)?; - module.create_named_method("testCreateArrayWithLength", test_create_array_with_length)?; - module.create_named_method("testSetElement", test_set_element)?; - module.create_named_method("testHasElement", test_has_element)?; - module.create_named_method("testDeleteElement", test_delete_element)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("testCreateArray", test_create_array)?; + exports.create_named_method("testCreateArrayWithLength", test_create_array_with_length)?; + exports.create_named_method("testSetElement", test_set_element)?; + exports.create_named_method("testHasElement", test_has_element)?; + exports.create_named_method("testDeleteElement", test_delete_element)?; Ok(()) } diff --git a/test_module/src/arraybuffer.rs b/test_module/src/arraybuffer.rs index 8acb8b74..ad38efd2 100644 --- a/test_module/src/arraybuffer.rs +++ b/test_module/src/arraybuffer.rs @@ -1,6 +1,6 @@ use std::str; -use napi::{CallContext, JsArrayBuffer, JsNumber, Module, Result}; +use napi::{CallContext, JsArrayBuffer, JsNumber, JsObject, Result}; #[js_function(1)] pub fn get_arraybuffer_length(ctx: CallContext) -> Result { @@ -8,7 +8,7 @@ pub fn get_arraybuffer_length(ctx: CallContext) -> Result { ctx.env.create_uint32((&buffer).len() as u32) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("getArraybufferLength", get_arraybuffer_length)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("getArraybufferLength", get_arraybuffer_length)?; Ok(()) } diff --git a/test_module/src/buffer.rs b/test_module/src/buffer.rs index 470c013a..d55214e6 100644 --- a/test_module/src/buffer.rs +++ b/test_module/src/buffer.rs @@ -1,6 +1,6 @@ use std::str; -use napi::{CallContext, Error, JsBuffer, JsNumber, JsString, Module, Result, Status}; +use napi::{CallContext, Error, JsBuffer, JsNumber, JsObject, JsString, Result, Status}; #[js_function(1)] pub fn get_buffer_length(ctx: CallContext) -> Result { @@ -22,9 +22,9 @@ pub fn copy_buffer(ctx: CallContext) -> Result { ctx.env.create_buffer_copy(buffer).map(|b| b.into_raw()) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("getBufferLength", get_buffer_length)?; - module.create_named_method("bufferToString", buffer_to_string)?; - module.create_named_method("copyBuffer", copy_buffer)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("getBufferLength", get_buffer_length)?; + exports.create_named_method("bufferToString", buffer_to_string)?; + exports.create_named_method("copyBuffer", copy_buffer)?; Ok(()) } diff --git a/test_module/src/class.rs b/test_module/src/class.rs index 58de0572..4e8438ea 100644 --- a/test_module/src/class.rs +++ b/test_module/src/class.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; -use napi::{CallContext, JsFunction, JsNumber, JsObject, JsUndefined, Module, Property, Result}; +use napi::{CallContext, JsFunction, JsNumber, JsObject, JsUndefined, Property, Result}; struct NativeClass { value: i32, @@ -58,8 +58,8 @@ fn new_test_class(ctx: CallContext) -> Result { test_class.new(&vec![ctx.env.create_int32(42)?]) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("createTestClass", create_test_class)?; - module.create_named_method("newTestClass", new_test_class)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("createTestClass", create_test_class)?; + exports.create_named_method("newTestClass", new_test_class)?; Ok(()) } diff --git a/test_module/src/cleanup_env.rs b/test_module/src/cleanup_env.rs index ff55b9a5..13c17858 100644 --- a/test_module/src/cleanup_env.rs +++ b/test_module/src/cleanup_env.rs @@ -1,5 +1,5 @@ use napi::{ - CallContext, CleanupEnvHook, ContextlessResult, Env, JsExternal, JsUndefined, Module, Result, + CallContext, CleanupEnvHook, ContextlessResult, Env, JsExternal, JsObject, JsUndefined, Result, }; #[contextless_function] @@ -20,8 +20,8 @@ fn remove_cleanup_hook(ctx: CallContext) -> Result { ctx.env.get_undefined() } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("addCleanupHook", add_cleanup_hook)?; - module.create_named_method("removeCleanupHook", remove_cleanup_hook)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("addCleanupHook", add_cleanup_hook)?; + exports.create_named_method("removeCleanupHook", remove_cleanup_hook)?; Ok(()) } diff --git a/test_module/src/either.rs b/test_module/src/either.rs index c9eb7240..d7045f52 100644 --- a/test_module/src/either.rs +++ b/test_module/src/either.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; -use napi::{CallContext, Either, JsNumber, JsString, Module, Result}; +use napi::{CallContext, Either, JsNumber, JsObject, JsString, Result}; #[js_function(1)] pub fn either_number_string(ctx: CallContext) -> Result> { @@ -28,8 +28,8 @@ pub fn dynamic_argument_length(ctx: CallContext) -> Result { } } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("eitherNumberString", either_number_string)?; - module.create_named_method("dynamicArgumentLength", dynamic_argument_length)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("eitherNumberString", either_number_string)?; + exports.create_named_method("dynamicArgumentLength", dynamic_argument_length)?; Ok(()) } diff --git a/test_module/src/env.rs b/test_module/src/env.rs index 33aaf692..0648a4f8 100644 --- a/test_module/src/env.rs +++ b/test_module/src/env.rs @@ -1,4 +1,4 @@ -use napi::{CallContext, JsBoolean, JsUnknown, Module, Result}; +use napi::{CallContext, JsBoolean, JsObject, JsUnknown, Result}; #[js_function(2)] pub fn instanceof(ctx: CallContext) -> Result { @@ -26,10 +26,10 @@ pub fn strict_equals(ctx: CallContext) -> Result { ctx.env.get_boolean(ctx.env.strict_equals(a, b)?) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("instanceof", instanceof)?; - module.create_named_method("isTypedarray", is_typedarray)?; - module.create_named_method("isDataview", is_dataview)?; - module.create_named_method("strictEquals", strict_equals)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("instanceof", instanceof)?; + exports.create_named_method("isTypedarray", is_typedarray)?; + exports.create_named_method("isDataview", is_dataview)?; + exports.create_named_method("strictEquals", strict_equals)?; Ok(()) } diff --git a/test_module/src/error.rs b/test_module/src/error.rs index 27b6cd3e..7bc86183 100644 --- a/test_module/src/error.rs +++ b/test_module/src/error.rs @@ -1,4 +1,4 @@ -use napi::{CallContext, Error, JsBoolean, JsString, JsUnknown, Module, Result, Status}; +use napi::{CallContext, Error, JsBoolean, JsObject, JsString, JsUnknown, Result, Status}; #[js_function] fn test_throw(_ctx: CallContext) -> Result { @@ -25,9 +25,9 @@ pub fn is_error(ctx: CallContext) -> Result { ctx.env.get_boolean(js_value.is_error()?) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("testThrow", test_throw)?; - module.create_named_method("testThrowWithReason", test_throw_with_reason)?; - module.create_named_method("isError", is_error)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("testThrow", test_throw)?; + exports.create_named_method("testThrowWithReason", test_throw_with_reason)?; + exports.create_named_method("isError", is_error)?; Ok(()) } diff --git a/test_module/src/external.rs b/test_module/src/external.rs index c46a6be4..b49df5f6 100644 --- a/test_module/src/external.rs +++ b/test_module/src/external.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; -use napi::{CallContext, JsExternal, JsNumber, Module, Result}; +use napi::{CallContext, JsExternal, JsNumber, JsObject, Result}; struct NativeObject { count: i32, @@ -20,8 +20,8 @@ pub fn get_external_count(ctx: CallContext) -> Result { ctx.env.create_int32(native_object.count) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("createExternal", create_external)?; - module.create_named_method("getExternalCount", get_external_count)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("createExternal", create_external)?; + exports.create_named_method("getExternalCount", get_external_count)?; Ok(()) } diff --git a/test_module/src/function.rs b/test_module/src/function.rs index 94cd8c6b..271d94cc 100644 --- a/test_module/src/function.rs +++ b/test_module/src/function.rs @@ -1,4 +1,4 @@ -use napi::{CallContext, JsFunction, JsNull, JsObject, Module, Result}; +use napi::{CallContext, JsFunction, JsNull, JsObject, Result}; #[js_function(1)] pub fn call_function(ctx: CallContext) -> Result { @@ -21,8 +21,8 @@ pub fn call_function_with_this(ctx: CallContext) -> Result { ctx.env.get_null() } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("testCallFunction", call_function)?; - module.create_named_method("testCallFunctionWithThis", call_function_with_this)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("testCallFunction", call_function)?; + exports.create_named_method("testCallFunctionWithThis", call_function_with_this)?; Ok(()) } diff --git a/test_module/src/global.rs b/test_module/src/global.rs index d21e0b0a..6dcff50d 100644 --- a/test_module/src/global.rs +++ b/test_module/src/global.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; -use napi::{CallContext, JsFunction, JsNumber, JsTimeout, JsUndefined, Module, Result}; +use napi::{CallContext, JsFunction, JsNumber, JsObject, JsTimeout, JsUndefined, Result}; #[js_function(2)] pub fn set_timeout(ctx: CallContext) -> Result { @@ -18,8 +18,8 @@ pub fn clear_timeout(ctx: CallContext) -> Result { ctx.env.get_global()?.clear_timeout(timer) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("setTimeout", set_timeout)?; - module.create_named_method("clearTimeout", clear_timeout)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("setTimeout", set_timeout)?; + exports.create_named_method("clearTimeout", clear_timeout)?; Ok(()) } diff --git a/test_module/src/lib.rs b/test_module/src/lib.rs index 889cb952..7feab711 100644 --- a/test_module/src/lib.rs +++ b/test_module/src/lib.rs @@ -1,11 +1,9 @@ #[macro_use] -extern crate napi; -#[macro_use] extern crate napi_derive; #[macro_use] extern crate serde_derive; -use napi::{Module, Result}; +use napi::{JsObject, Result}; mod cleanup_env; #[cfg(feature = "latest")] @@ -36,33 +34,32 @@ mod task; use napi_version::get_napi_version; -register_module!(test_module, init); - -fn init(module: &mut Module) -> Result<()> { - module.create_named_method("getNapiVersion", get_napi_version)?; - array::register_js(module)?; - error::register_js(module)?; - string::register_js(module)?; - serde::register_js(module)?; - task::register_js(module)?; - external::register_js(module)?; - arraybuffer::register_js(module)?; - buffer::register_js(module)?; - either::register_js(module)?; - symbol::register_js(module)?; - function::register_js(module)?; - class::register_js(module)?; - env::register_js(module)?; - object::register_js(module)?; - global::register_js(module)?; - cleanup_env::register_js(module)?; +#[module_exports] +fn init(mut exports: JsObject) -> Result<()> { + exports.create_named_method("getNapiVersion", get_napi_version)?; + array::register_js(&mut exports)?; + error::register_js(&mut exports)?; + string::register_js(&mut exports)?; + serde::register_js(&mut exports)?; + task::register_js(&mut exports)?; + external::register_js(&mut exports)?; + arraybuffer::register_js(&mut exports)?; + buffer::register_js(&mut exports)?; + either::register_js(&mut exports)?; + symbol::register_js(&mut exports)?; + function::register_js(&mut exports)?; + class::register_js(&mut exports)?; + env::register_js(&mut exports)?; + object::register_js(&mut exports)?; + global::register_js(&mut exports)?; + cleanup_env::register_js(&mut exports)?; #[cfg(feature = "latest")] - napi4::register_js(module)?; + napi4::register_js(&mut exports)?; #[cfg(feature = "latest")] - tokio_rt::register_js(module)?; + tokio_rt::register_js(&mut exports)?; #[cfg(feature = "latest")] - napi5::register_js(module)?; + napi5::register_js(&mut exports)?; #[cfg(feature = "latest")] - napi6::register_js(module)?; + napi6::register_js(&mut exports)?; Ok(()) } diff --git a/test_module/src/napi4/mod.rs b/test_module/src/napi4/mod.rs index 5a451c97..ea79413e 100644 --- a/test_module/src/napi4/mod.rs +++ b/test_module/src/napi4/mod.rs @@ -1,22 +1,22 @@ -use napi::{Module, Result}; +use napi::{JsObject, Result}; mod tsfn; use tsfn::*; -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("testThreadsafeFunction", test_threadsafe_function)?; - module.create_named_method("testTsfnError", test_tsfn_error)?; - module.create_named_method("testTokioReadfile", test_tokio_readfile)?; - module.create_named_method( +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("testThreadsafeFunction", test_threadsafe_function)?; + exports.create_named_method("testTsfnError", test_tsfn_error)?; + exports.create_named_method("testTokioReadfile", test_tokio_readfile)?; + exports.create_named_method( "testAbortThreadsafeFunction", test_abort_threadsafe_function, )?; - module.create_named_method( + exports.create_named_method( "testAbortIndependentThreadsafeFunction", test_abort_independent_threadsafe_function, )?; - module.create_named_method( + exports.create_named_method( "testCallAbortedThreadsafeFunction", test_call_aborted_threadsafe_function, )?; diff --git a/test_module/src/napi5/mod.rs b/test_module/src/napi5/mod.rs index 7abded31..90789efb 100644 --- a/test_module/src/napi5/mod.rs +++ b/test_module/src/napi5/mod.rs @@ -1,10 +1,10 @@ -use napi::{Module, Result}; +use napi::{JsObject, Result}; mod date; -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("testObjectIsDate", date::test_object_is_date)?; - module.create_named_method("testCreateDate", date::test_create_date)?; - module.create_named_method("testGetDateValue", date::test_get_date_value)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("testObjectIsDate", date::test_object_is_date)?; + exports.create_named_method("testCreateDate", date::test_create_date)?; + exports.create_named_method("testGetDateValue", date::test_get_date_value)?; Ok(()) } diff --git a/test_module/src/napi6/mod.rs b/test_module/src/napi6/mod.rs index 7e4121ff..61a8e246 100644 --- a/test_module/src/napi6/mod.rs +++ b/test_module/src/napi6/mod.rs @@ -1,17 +1,17 @@ -use napi::{Module, Result}; +use napi::{JsObject, Result}; mod bigint; use bigint::*; -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("testCreateBigintFromI64", test_create_bigint_from_i64)?; - module.create_named_method("testCreateBigintFromU64", test_create_bigint_from_u64)?; - module.create_named_method("testCreateBigintFromI128", test_create_bigint_from_i128)?; - module.create_named_method("testCreateBigintFromU128", test_create_bigint_from_u128)?; - module.create_named_method("testCreateBigintFromWords", test_create_bigint_from_words)?; - module.create_named_method("testGetBigintI64", test_get_bigint_i64)?; - module.create_named_method("testGetBigintU64", test_get_bigint_u64)?; - module.create_named_method("testGetBigintWords", test_get_bigint_words)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("testCreateBigintFromI64", test_create_bigint_from_i64)?; + exports.create_named_method("testCreateBigintFromU64", test_create_bigint_from_u64)?; + exports.create_named_method("testCreateBigintFromI128", test_create_bigint_from_i128)?; + exports.create_named_method("testCreateBigintFromU128", test_create_bigint_from_u128)?; + exports.create_named_method("testCreateBigintFromWords", test_create_bigint_from_words)?; + exports.create_named_method("testGetBigintI64", test_get_bigint_i64)?; + exports.create_named_method("testGetBigintU64", test_get_bigint_u64)?; + exports.create_named_method("testGetBigintWords", test_get_bigint_words)?; Ok(()) } diff --git a/test_module/src/object.rs b/test_module/src/object.rs index fef07abc..647821e7 100644 --- a/test_module/src/object.rs +++ b/test_module/src/object.rs @@ -1,8 +1,7 @@ use std::convert::TryInto; use napi::{ - CallContext, JsBoolean, JsNumber, JsObject, JsString, JsUndefined, JsUnknown, Module, Property, - Result, + CallContext, JsBoolean, JsNumber, JsObject, JsString, JsUndefined, JsUnknown, Property, Result, }; #[js_function(2)] @@ -164,28 +163,28 @@ fn test_is_promise(ctx: CallContext) -> Result { ctx.env.get_boolean(obj.is_promise()?) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("testSetProperty", test_set_property)?; - module.create_named_method("testGetProperty", test_get_property)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("testSetProperty", test_set_property)?; + exports.create_named_method("testGetProperty", test_get_property)?; - module.create_named_method("testSetNamedProperty", test_set_named_property)?; - module.create_named_method("testGetNamedProperty", test_get_named_property)?; - module.create_named_method("testHasNamedProperty", test_has_named_property)?; + exports.create_named_method("testSetNamedProperty", test_set_named_property)?; + exports.create_named_method("testGetNamedProperty", test_get_named_property)?; + exports.create_named_method("testHasNamedProperty", test_has_named_property)?; - module.create_named_method("testHasOwnProperty", test_has_own_property)?; - module.create_named_method("testHasOwnPropertyJs", test_has_own_property_js)?; - module.create_named_method("testHasProperty", test_has_property)?; - module.create_named_method("testHasPropertyJs", test_has_property_js)?; - module.create_named_method("testDeleteProperty", test_delete_property)?; - module.create_named_method("testDeleteNamedProperty", test_delete_named_property)?; - module.create_named_method("testGetPropertyNames", test_get_property_names)?; - module.create_named_method("testGetPrototype", test_get_prototype)?; - module.create_named_method("testSetElement", test_set_element)?; - module.create_named_method("testHasElement", test_has_element)?; - module.create_named_method("testGetElement", test_get_element)?; - module.create_named_method("testDeleteElement", test_delete_element)?; - module.create_named_method("testDefineProperties", test_define_properties)?; + exports.create_named_method("testHasOwnProperty", test_has_own_property)?; + exports.create_named_method("testHasOwnPropertyJs", test_has_own_property_js)?; + exports.create_named_method("testHasProperty", test_has_property)?; + exports.create_named_method("testHasPropertyJs", test_has_property_js)?; + exports.create_named_method("testDeleteProperty", test_delete_property)?; + exports.create_named_method("testDeleteNamedProperty", test_delete_named_property)?; + exports.create_named_method("testGetPropertyNames", test_get_property_names)?; + exports.create_named_method("testGetPrototype", test_get_prototype)?; + exports.create_named_method("testSetElement", test_set_element)?; + exports.create_named_method("testHasElement", test_has_element)?; + exports.create_named_method("testGetElement", test_get_element)?; + exports.create_named_method("testDeleteElement", test_delete_element)?; + exports.create_named_method("testDefineProperties", test_define_properties)?; - module.create_named_method("testIsPromise", test_is_promise)?; + exports.create_named_method("testIsPromise", test_is_promise)?; Ok(()) } diff --git a/test_module/src/serde.rs b/test_module/src/serde.rs index 6c99c87f..4871d1c4 100644 --- a/test_module/src/serde.rs +++ b/test_module/src/serde.rs @@ -1,4 +1,4 @@ -use napi::{CallContext, JsObject, JsUndefined, JsUnknown, Module, Result}; +use napi::{CallContext, JsObject, JsUndefined, JsUnknown, Result}; #[derive(Serialize, Debug, Deserialize)] struct AnObject { @@ -162,21 +162,21 @@ fn roundtrip_object(ctx: CallContext) -> Result { ctx.env.to_js_value(&de_serialized) } -pub fn register_js(m: &mut Module) -> Result<()> { - m.create_named_method("make_num_77", make_num_77)?; - m.create_named_method("make_num_32", make_num_32)?; - m.create_named_method("make_str_hello", make_str_hello)?; - m.create_named_method("make_num_array", make_num_array)?; - m.create_named_method("make_buff", make_buff)?; - m.create_named_method("make_obj", make_obj)?; - m.create_named_method("make_object", make_object)?; - m.create_named_method("make_map", make_map)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("make_num_77", make_num_77)?; + exports.create_named_method("make_num_32", make_num_32)?; + exports.create_named_method("make_str_hello", make_str_hello)?; + exports.create_named_method("make_num_array", make_num_array)?; + exports.create_named_method("make_buff", make_buff)?; + exports.create_named_method("make_obj", make_obj)?; + exports.create_named_method("make_object", make_object)?; + exports.create_named_method("make_map", make_map)?; - m.create_named_method("expect_hello_world", expect_hello_world)?; - m.create_named_method("expect_obj", expect_obj)?; - m.create_named_method("expect_num_array", expect_num_array)?; - m.create_named_method("expect_buffer", expect_buffer)?; + exports.create_named_method("expect_hello_world", expect_hello_world)?; + exports.create_named_method("expect_obj", expect_obj)?; + exports.create_named_method("expect_num_array", expect_num_array)?; + exports.create_named_method("expect_buffer", expect_buffer)?; - m.create_named_method("roundtrip_object", roundtrip_object)?; + exports.create_named_method("roundtrip_object", roundtrip_object)?; Ok(()) } diff --git a/test_module/src/string.rs b/test_module/src/string.rs index 02d14190..8e3d0ddc 100644 --- a/test_module/src/string.rs +++ b/test_module/src/string.rs @@ -1,4 +1,4 @@ -use napi::{CallContext, JsString, Module, Result}; +use napi::{CallContext, JsObject, JsString, Result}; #[js_function(1)] fn concat_string(ctx: CallContext) -> Result { @@ -23,9 +23,9 @@ fn create_latin1(ctx: CallContext) -> Result { ctx.env.create_string_latin1(bytes.as_slice()) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("concatString", concat_string)?; - module.create_named_method("concatLatin1String", concat_latin1_string)?; - module.create_named_method("createLatin1", create_latin1)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("concatString", concat_string)?; + exports.create_named_method("concatLatin1String", concat_latin1_string)?; + exports.create_named_method("createLatin1", create_latin1)?; Ok(()) } diff --git a/test_module/src/symbol.rs b/test_module/src/symbol.rs index e4770b29..150d5179 100644 --- a/test_module/src/symbol.rs +++ b/test_module/src/symbol.rs @@ -1,4 +1,4 @@ -use napi::{CallContext, JsString, JsSymbol, Module, Result}; +use napi::{CallContext, JsObject, JsString, JsSymbol, Result}; #[js_function] pub fn create_named_symbol(ctx: CallContext) -> Result { @@ -16,9 +16,9 @@ pub fn create_symbol_from_js_string(ctx: CallContext) -> Result { ctx.env.create_symbol_from_js_string(name) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("createNamedSymbol", create_named_symbol)?; - module.create_named_method("createUnnamedSymbol", create_unnamed_symbol)?; - module.create_named_method("createSymbolFromJsString", create_symbol_from_js_string)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("createNamedSymbol", create_named_symbol)?; + exports.create_named_method("createUnnamedSymbol", create_unnamed_symbol)?; + exports.create_named_method("createSymbolFromJsString", create_symbol_from_js_string)?; Ok(()) } diff --git a/test_module/src/task.rs b/test_module/src/task.rs index 8566372a..54380167 100644 --- a/test_module/src/task.rs +++ b/test_module/src/task.rs @@ -1,8 +1,6 @@ use std::convert::TryInto; -use napi::{ - CallContext, Env, JsBuffer, JsBufferValue, JsNumber, JsObject, Module, Ref, Result, Task, -}; +use napi::{CallContext, Env, JsBuffer, JsBufferValue, JsNumber, JsObject, Ref, Result, Task}; struct ComputeFib { n: u32, @@ -75,8 +73,8 @@ fn test_spawn_thread_with_ref(ctx: CallContext) -> Result { Ok(async_work_promise.promise_object()) } -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("testSpawnThread", test_spawn_thread)?; - module.create_named_method("testSpawnThreadWithRef", test_spawn_thread_with_ref)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("testSpawnThread", test_spawn_thread)?; + exports.create_named_method("testSpawnThreadWithRef", test_spawn_thread_with_ref)?; Ok(()) } diff --git a/test_module/src/tokio_rt/mod.rs b/test_module/src/tokio_rt/mod.rs index 7d34e233..dbc3b87b 100644 --- a/test_module/src/tokio_rt/mod.rs +++ b/test_module/src/tokio_rt/mod.rs @@ -1,11 +1,11 @@ -use napi::{Module, Result}; +use napi::{JsObject, Result}; mod read_file; use read_file::*; -pub fn register_js(module: &mut Module) -> Result<()> { - module.create_named_method("testExecuteTokioReadfile", test_execute_tokio_readfile)?; - module.create_named_method("testTokioError", error_from_tokio_future)?; +pub fn register_js(exports: &mut JsObject) -> Result<()> { + exports.create_named_method("testExecuteTokioReadfile", test_execute_tokio_readfile)?; + exports.create_named_method("testTokioError", error_from_tokio_future)?; Ok(()) }