diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index 00f268f0e5..7ccff0ddcf 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -61,13 +61,12 @@ impl TryToTokens for NapiFn { unsafe impl Sync for NapiRefContainer {} let _make_ref = |a: ::std::ptr::NonNull| { let mut node_ref = ::std::mem::MaybeUninit::uninit(); - assert_eq!(unsafe { - napi::bindgen_prelude::sys::napi_create_reference(env, a.as_ptr(), 1, node_ref.as_mut_ptr()) + napi::bindgen_prelude::check_status!(unsafe { + 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" - ); - unsafe { node_ref.assume_init() } + )?; + Ok::(unsafe { node_ref.assume_init() }) }; let mut _args_array = [::std::ptr::null_mut::(); #arg_ref_count]; let mut _arg_write_index = 0; @@ -184,7 +183,10 @@ impl NapiFn { let mut mut_ref_spans = vec![]; let make_ref = |input| { 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; } }; diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 71a27146a4..1e758c2ba7 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -195,7 +195,6 @@ Generated by [AVA](https://avajs.dev). export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊ export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊ export function threadsafeFunctionClosureCapture(func: (...args: any[]) => any): void␊ - export function useTokioWithoutAsync(): void␊ export function getBuffer(): Buffer␊ export function appendBuffer(buf: Buffer): Buffer␊ export function getEmptyBuffer(): Buffer␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 2bcb9a7bd4..d6b3df0b07 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index 4465ad986f..af98615a87 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -106,7 +106,6 @@ import { receiveObjectWithClassField, AnotherClassForEither, receiveDifferentClass, - useTokioWithoutAsync, getNumArr, getNestedNumArr, CustomFinalize, @@ -754,12 +753,6 @@ Napi4Test('await Promise in rust', async (t) => { 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) => { const fxError = new Error('What is Happy Planet') const err = await t.throwsAsync(() => asyncPlus100(Promise.reject(fxError))) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index dacaa6b9b2..9cadb8a71f 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -185,7 +185,6 @@ export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void export function threadsafeFunctionClosureCapture(func: (...args: any[]) => any): void -export function useTokioWithoutAsync(): void export function getBuffer(): Buffer export function appendBuffer(buf: Buffer): Buffer export function getEmptyBuffer(): Buffer diff --git a/examples/napi/src/lib.rs b/examples/napi/src/lib.rs index 48f23ca502..d50f516e26 100644 --- a/examples/napi/src/lib.rs +++ b/examples/napi/src/lib.rs @@ -40,5 +40,4 @@ mod string; mod symbol; mod task; mod threadsafe_function; -mod tokio_outside_async; mod typed_array; diff --git a/examples/napi/src/tokio_outside_async.rs b/examples/napi/src/tokio_outside_async.rs deleted file mode 100644 index b82a308b56..0000000000 --- a/examples/napi/src/tokio_outside_async.rs +++ /dev/null @@ -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)); -}