From 573f67b90fbeeeef3be5b5743d757fe674e64d55 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 22 Nov 2022 23:17:44 +0800 Subject: [PATCH] chore(napi-derive): make_ref tweaks (#1371) --- crates/backend/src/codegen/fn.rs | 14 ++++++++------ examples/napi/__test__/typegen.spec.ts.md | 1 - examples/napi/__test__/typegen.spec.ts.snap | Bin 3614 -> 3602 bytes examples/napi/__test__/values.spec.ts | 7 ------- examples/napi/index.d.ts | 1 - examples/napi/src/lib.rs | 1 - examples/napi/src/tokio_outside_async.rs | 18 ------------------ 7 files changed, 8 insertions(+), 34 deletions(-) delete mode 100644 examples/napi/src/tokio_outside_async.rs diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index 00f268f0..7ccff0dd 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 71a27146..1e758c2b 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 2bcb9a7bd40ef7b07e088045ff27030340b63285..d6b3df0b0717a738faafc5b064e52ad4a9c88e12 100644 GIT binary patch delta 3592 zcmV+j4)^h%9FiP=K~_N^Q*L2!b7*gLAa*kf0{|#Ma-z=xMn&xa*yj`|pY*a0j!ddM z1loTjkS(md)rKF72mk;800003ts3oa9JeuTQ55jMz5(l^$etlPmSh;|!5=uCq(rtQ z8Pd5jo=bQV> zWE_j|LjL?KQ(VRD`fqY3@;D+>mavQ}MnaKgEL21-Qbr;+Arl@mE)$vpq>?}X5=B(e zZ?~WQ;j=$|hX4KUkDvYd_Yb$fctDhld7}CemvkI6^5BbmFv;}fLuV3aX|BXWyJtkk z3vx7gd+_jor`ZlgfW$p>|HCCB{V)Be+0Uojb4zOdxT_+Mk0w99aAc_Lo zF9sWbWNaUM_Mx>d1d0{g;nRae1?zzwJbkhR(oBO2ynFg&AWt>#!I(Rg7`;!bGMC4fWank>%FH4b=cn@d69iN;}Vcl08#5C=(`E5N5QjDVyHW`j8Fu`H{&GlvZF0v7>) zPTl38j-rE}6a}pj9VEj7e-Ms0Ee(hVb{=olW$Kd>STf5dG-SjCs(sGlXowN)0D=fj z;01=MG0&nt*@1r{`gHpg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)5|Pc(Qq*Q#ix}hFgR8HLef1}7J2AarTVrgDGQ0*c;~g{B8UN`VB3ClinW^?4dY%0^jaQO= z%ZZP|-60ni#TLN@1Bu#=VchH2b|oZFBi)GBnQ!6fwBK(fAF!0P!=RNQ1iB$Lv12C2 zu1(!Jr;9N=Vt*_u8;w~1>g|2MT|1RtmBU>i+EQNRu6CCypzq8pL7{{;ZppLY+ctNK zs=sTl1U=nvQzqkT3RUUSt&Iv`LsgG>@{x}6OvS8i6U!nmKAJe51h$^dlZ!-L`JGnd zl7>2AAPc;TOAtFz8vUQZq5bQZihoMj)d@{!C=-tkqks2@R9*~4Z0x?E#~MUs&Ug>> zg!YpS{I_R*YAW_h4y42n6rU6GP&)$i@}@;8VB>&CUVUIuxn5rDK5mps!?G#v)_IgrYKU=6Lg!mpFcQgg=e zrn}SF^|wssix^|Wt=^;Zu7RTn(EB#s>h>Dw7~A-)qv)1M)r1WJjgLQ?mela9Enly! zUbfH28tBY)79MehQBq{*XqpFh@XU$cd_(8*_J7r=w|)g~D_>KZ1(Gq}F(K}j{k9Fm zd6wzV0E&}OAUbs@LJfGM0^9pPsc)R-2}?!eFe0A$29rTXteE-q=R`lZ;lcSr9oqt} z_RYi7(~~@AZ@5JNJe1OnYIL zqkpo_5b5Emlj(uh#aK-QGNivoy$d^;RfNU6+m~$pN6vtuM<(I;&~H z1VGCXeS>rwzI_~K<{QUEGgUCFO>S*g7BQK2eLo#m@O{LmkoN~Z3AY`x+wx`SGqi=b a3@fe{+hzLA%KO~gd;bGWY>joaGXMbR8T_pP diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index 4465ad98..af98615a 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 dacaa6b9..9cadb8a7 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 48f23ca5..d50f516e 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 b82a308b..00000000 --- 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)); -}