chore(napi-derive): make_ref tweaks (#1371)

This commit is contained in:
LongYinan 2022-11-22 23:17:44 +08:00 committed by GitHub
parent 618d0f8046
commit 573f67b90f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 8 additions and 34 deletions

View file

@ -61,13 +61,12 @@ impl TryToTokens for NapiFn {
unsafe impl Sync for NapiRefContainer {} unsafe impl Sync for NapiRefContainer {}
let _make_ref = |a: ::std::ptr::NonNull<napi::bindgen_prelude::sys::napi_value__>| { let _make_ref = |a: ::std::ptr::NonNull<napi::bindgen_prelude::sys::napi_value__>| {
let mut node_ref = ::std::mem::MaybeUninit::uninit(); let mut node_ref = ::std::mem::MaybeUninit::uninit();
assert_eq!(unsafe { napi::bindgen_prelude::check_status!(unsafe {
napi::bindgen_prelude::sys::napi_create_reference(env, a.as_ptr(), 1, node_ref.as_mut_ptr()) napi::bindgen_prelude::sys::napi_create_reference(env, a.as_ptr(), 0, node_ref.as_mut_ptr())
}, },
napi::bindgen_prelude::sys::Status::napi_ok,
"failed to create napi ref" "failed to create napi ref"
); )?;
unsafe { node_ref.assume_init() } Ok::<napi::sys::napi_ref, napi::Error>(unsafe { node_ref.assume_init() })
}; };
let mut _args_array = [::std::ptr::null_mut::<napi::bindgen_prelude::sys::napi_ref__>(); #arg_ref_count]; let mut _args_array = [::std::ptr::null_mut::<napi::bindgen_prelude::sys::napi_ref__>(); #arg_ref_count];
let mut _arg_write_index = 0; let mut _arg_write_index = 0;
@ -184,7 +183,10 @@ impl NapiFn {
let mut mut_ref_spans = vec![]; let mut mut_ref_spans = vec![];
let make_ref = |input| { let make_ref = |input| {
quote! { quote! {
_args_array[_arg_write_index] = _make_ref(::std::ptr::NonNull::new(#input).expect("ref ptr was null")); _args_array[_arg_write_index] = _make_ref(
::std::ptr::NonNull::new(#input)
.ok_or_else(|| napi::Error::new(napi::Status::InvalidArg, "referenced ptr is null".to_owned()))?
)?;
_arg_write_index += 1; _arg_write_index += 1;
} }
}; };

View file

@ -195,7 +195,6 @@ Generated by [AVA](https://avajs.dev).
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊ export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊
export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊ export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊
export function threadsafeFunctionClosureCapture(func: (...args: any[]) => any): void␊ export function threadsafeFunctionClosureCapture(func: (...args: any[]) => any): void␊
export function useTokioWithoutAsync(): void␊
export function getBuffer(): Buffer␊ export function getBuffer(): Buffer␊
export function appendBuffer(buf: Buffer): Buffer␊ export function appendBuffer(buf: Buffer): Buffer␊
export function getEmptyBuffer(): Buffer␊ export function getEmptyBuffer(): Buffer␊

View file

@ -106,7 +106,6 @@ import {
receiveObjectWithClassField, receiveObjectWithClassField,
AnotherClassForEither, AnotherClassForEither,
receiveDifferentClass, receiveDifferentClass,
useTokioWithoutAsync,
getNumArr, getNumArr,
getNestedNumArr, getNestedNumArr,
CustomFinalize, CustomFinalize,
@ -754,12 +753,6 @@ Napi4Test('await Promise in rust', async (t) => {
t.is(result, fx + 100) t.is(result, fx + 100)
}) })
Napi4Test('Run function which uses tokio internally but is not async', (t) => {
useTokioWithoutAsync()
// The prior didn't throw an exception, so it worked.
t.assert(true)
})
Napi4Test('Promise should reject raw error in rust', async (t) => { Napi4Test('Promise should reject raw error in rust', async (t) => {
const fxError = new Error('What is Happy Planet') const fxError = new Error('What is Happy Planet')
const err = await t.throwsAsync(() => asyncPlus100(Promise.reject(fxError))) const err = await t.throwsAsync(() => asyncPlus100(Promise.reject(fxError)))

View file

@ -185,7 +185,6 @@ export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void
export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void
export function threadsafeFunctionClosureCapture(func: (...args: any[]) => any): void export function threadsafeFunctionClosureCapture(func: (...args: any[]) => any): void
export function useTokioWithoutAsync(): void
export function getBuffer(): Buffer export function getBuffer(): Buffer
export function appendBuffer(buf: Buffer): Buffer export function appendBuffer(buf: Buffer): Buffer
export function getEmptyBuffer(): Buffer export function getEmptyBuffer(): Buffer

View file

@ -40,5 +40,4 @@ mod string;
mod symbol; mod symbol;
mod task; mod task;
mod threadsafe_function; mod threadsafe_function;
mod tokio_outside_async;
mod typed_array; mod typed_array;

View file

@ -1,18 +0,0 @@
use std::time::Duration;
use tokio::{sync::oneshot, time::Instant};
#[napi]
pub fn use_tokio_without_async() {
let (sender, receiver) = oneshot::channel();
let handle = tokio::task::spawn(async {
// If this panics, the test failed.
sender.send(true).unwrap();
});
let start = Instant::now();
while !handle.is_finished() {
if start.elapsed() > Duration::from_secs(5) {
panic!("The future never resolved.");
}
}
assert_eq!(receiver.blocking_recv(), Ok(true));
}