From 938f4df83d2e9864db70381560a01f92dd05351e Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 8 Nov 2023 20:05:09 +0800 Subject: [PATCH] fix(napi-derive): async task void output type (#1795) --- crates/backend/src/typegen/struct.rs | 7 +++++- crates/macro/src/parser/mod.rs | 4 +--- .../__snapshots__/typegen.spec.ts.md | 2 ++ .../__snapshots__/typegen.spec.ts.snap | Bin 4126 -> 4132 bytes examples/napi/index.d.ts | 2 ++ examples/napi/src/task.rs | 21 ++++++++++++++++++ 6 files changed, 32 insertions(+), 4 deletions(-) diff --git a/crates/backend/src/typegen/struct.rs b/crates/backend/src/typegen/struct.rs index 971edfd2..0ac18d85 100644 --- a/crates/backend/src/typegen/struct.rs +++ b/crates/backend/src/typegen/struct.rs @@ -36,9 +36,14 @@ impl ToTypeDef for NapiImpl { fn to_type_def(&self) -> Option { if let Some(output_type) = &self.task_output_type { TASK_STRUCTS.with(|t| { + let resolved_type = ty_to_ts_type(output_type, false, true, false).0; t.borrow_mut().insert( self.name.to_string(), - ty_to_ts_type(output_type, false, true, false).0, + if resolved_type == "undefined" { + "void".to_owned() + } else { + resolved_type + }, ); }); } diff --git a/crates/macro/src/parser/mod.rs b/crates/macro/src/parser/mod.rs index 86afc1f0..e58f62ba 100644 --- a/crates/macro/src/parser/mod.rs +++ b/crates/macro/src/parser/mod.rs @@ -1033,9 +1033,7 @@ impl ConvertToAST for syn::ItemImpl { if let Some((_, t, _)) = &self.trait_ { if let Some(PathSegment { ident, .. }) = t.segments.last() { if ident == "Task" && m.ident == "JsValue" { - if let Type::Path(_) = &m.ty { - task_output_type = Some(m.ty.clone()); - } + task_output_type = Some(m.ty.clone()); } else if ident == "Generator" { if let Type::Path(_) = &m.ty { if m.ident == "Yield" { diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index 45c24471..43e37706 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -246,6 +246,8 @@ Generated by [AVA](https://avajs.dev). ␊ export function asyncReduceBuffer(buf: Buffer): Promise␊ ␊ + export function asyncTaskVoidReturn(): Promise␊ + ␊ export interface B {␊ bar: number␊ }␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 3801ad873926c9c1b57785d2631a8492475655fd..522fda1a4822e4167976a582842d15b57baa43cd 100644 GIT binary patch literal 4132 zcmV+<5ZmuTRzVR;>2$_#ed{CYPREqztXQ(glipbJNV4R}u_TY~ z94F&=VjhXRgT*5*1ZerA| zghvVltyC%|b~@#IuY>lE~GPl9*1(lqHmjG+Y9u6hHqG$Ds_r z-M#gP&;Ilo{`0#(e)i|z|7G{{+a$~-zhec(WU?Zsd(V&WoR7%u&u_qYDiW5;J24aC zB%#=N;|6`dSk}1!~8{PT_g#VDU zj0zGa35=d5u^^I@2_?&XlCWriVz`{s?2-veWH=*iu>{hTgn~HYk?wD=VCGohv~Ik< zfkUqhd@p5-Fj0d8HOW^jjR~KUP&Wg$Gvo{SI8-EtdOT->z<;2T+q-6S8iJB@K+mpAiy90KGV_z(2Ds?-m3G+wjg1=?tzyJC7KUF&pBPGnhb{ftIk4#Vd>r3^k3Y z&|GpJN@DgOSQiyhxP*OUc3`p^YO?QMz5a0Z`FF#cH$Uixt70e_m3fw)p)DSuA)#)d zrsvB`n2&>8NWM65ECR^)S%yR5?`W2>m>#Dwd&lBDO!l(bS$d)jAV_%z%R64#DH~Y& zn5NkJA+=wwtjMbyJ`+bZ9@A+EtLEsT4Bxd4c-70F=8Flhygpc`js;2E7XK`(^j1<$?PEC+k zFX$1J^2@<*I}Tw@Lb2bE42pY}!5Qmm1DJ;m5mk=+J{G$!b=F-E?>Q9!0N0YBt)R{# z0ka!h7l1|5_j(I0!qtSHQW`%Rk_qPt4O3{HsI?lB7Z8qov6p4x%CjFFr60ozn`JJi zNcU1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nlItV* zty!uX+b5L5t&r+k#LSi%22U6)FaD|nT&7)vJNrv3hhn#6Y(Z;qHSE#M7Kb60#agA- zwKoeLF{0Uj<{oBwuys&Pi%_3-MaxRLLkdu`=tE}d#B77gwhODZTWv%0TZPuixSFiK zj`2q%hcjrxAB^N8O<_fS*A7QaCJQPc6ZM2%wwXZe06=@2KDH6};luLDV>(8&6I4P$ z19K2-R>%FwI&1Hms=%`EUFlCku#Aoy#hWe%4t_2o7JKeRpwwaxry=jy-|Wd;8y1!*`W+ c=L9bacFcu;4uAjTanI!a6O%A}#XLR$03v-LFaQ7m diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index bd950724..782b9ca9 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -236,6 +236,8 @@ export function asyncPlus100(p: Promise): Promise export function asyncReduceBuffer(buf: Buffer): Promise +export function asyncTaskVoidReturn(): Promise + export interface B { bar: number } diff --git a/examples/napi/src/task.rs b/examples/napi/src/task.rs index b2f1c4f1..c4e9b050 100644 --- a/examples/napi/src/task.rs +++ b/examples/napi/src/task.rs @@ -28,3 +28,24 @@ fn without_abort_controller(a: u32, b: u32) -> AsyncTask { fn with_abort_controller(a: u32, b: u32, signal: AbortSignal) -> AsyncTask { AsyncTask::with_signal(DelaySum(a, b), signal) } + +struct AsyncTaskVoidReturn {} + +#[napi] +impl Task for AsyncTaskVoidReturn { + type JsValue = (); + type Output = (); + + fn compute(&mut self) -> Result { + Ok(()) + } + + fn resolve(&mut self, _env: Env, output: Self::Output) -> Result { + Ok(output) + } +} + +#[napi] +fn async_task_void_return() -> AsyncTask { + AsyncTask::new(AsyncTaskVoidReturn {}) +}