From e9ab2192dab8006684f6654a4215c5cdafd9cbf1 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 6 Nov 2021 21:48:18 +0800 Subject: [PATCH] fix(napi-derive): return Result type in Constructor and Factory --- crates/backend/src/codegen/fn.rs | 12 +++++++-- crates/macro/src/parser/mod.rs | 5 ++-- examples/napi/__test__/typegen.spec.ts.md | 6 +++++ examples/napi/__test__/typegen.spec.ts.snap | Bin 878 -> 909 bytes examples/napi/__test__/values.spec.ts | 11 ++++++++ examples/napi/index.d.ts | 6 +++++ examples/napi/src/class.rs | 27 ++++++++++++++++++++ 7 files changed, 62 insertions(+), 5 deletions(-) diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index 930dab6d..2f828ffc 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -244,9 +244,17 @@ impl NapiFn { if let Some(ty) = &self.ret { if self.kind == FnKind::Constructor { - quote! { cb.construct(#js_name, #ret) } + if self.is_ret_result { + quote! { cb.construct(#js_name, #ret?) } + } else { + quote! { cb.construct(#js_name, #ret) } + } } else if self.kind == FnKind::Factory { - quote! { cb.factory(#js_name, #ret) } + if self.is_ret_result { + quote! { cb.factory(#js_name, #ret?) } + } else { + quote! { cb.factory(#js_name, #ret) } + } } else if self.is_ret_result { if self.is_async { quote! { diff --git a/crates/macro/src/parser/mod.rs b/crates/macro/src/parser/mod.rs index de4da2f6..7d6aa765 100644 --- a/crates/macro/src/parser/mod.rs +++ b/crates/macro/src/parser/mod.rs @@ -234,9 +234,8 @@ fn extract_callback_trait_types( fn extract_result_ty(ty: &syn::Type) -> BindgenResult> { match ty { - syn::Type::Path(syn::TypePath { qself: None, path }) if path.segments.len() == 1 => { - let segment = path.segments.first().unwrap(); - + syn::Type::Path(syn::TypePath { qself: None, path }) => { + let segment = path.segments.last().unwrap(); if segment.ident != "Result" { Ok(None) } else { diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 9408013c..a5412e72 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -61,6 +61,12 @@ Generated by [AVA](https://avajs.dev). export class Blake2BKey {␊ ␊ }␊ + export class Context {␊ + ␊ + constructor()␊ + static withData(data: string): Context␊ + method(): string␊ + }␊ export class ClassWithFactory {␊ name: string␊ static withName(name: string): ClassWithFactory␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index d3478adf0f2bdb1423a8b0c35b429b4945548dbd..0071ef33c1151ef6b60252889bbb1c1de36d0a18 100644 GIT binary patch literal 909 zcmV;819JR9RzV@YJNC0=1!avzHb00000000Ax zRn2Z2HxOPoD2n8kQ?GNRfIavpO`Anxz_x58O&|wRil9d>cO)^|B?XdeS#bRtd4=R5 zaw`h--g93e$DA{ywDL-;jpW06NPhEuoZ-v}qA1#n?tS?Azkk2{`T657zkdDypT9rt zMgFgUKZ@=}KLva5$LQe?-TQbw(Z<1`RKiKEV2JLWHkoCe1Wt_Mv!~V>sfNc7HXwUt zQ3I;7NYONbwk!gW&g~kOeCJlf$so@zWR53xrUaXCH%efB6Y_a&bRjK1JuL@=O^S52 zvuVZz&-0XvaRQ8+;r@~Uo*l!LmKibmf34b0f^%cEfxn+Qm3h8Z z0HqtD=?9-n(+e43QcX&?!sNHVmmyagbsnfH=cUgYOUn(8T13f6wa$^NJ9()bngJIG zm+5=Bc>r+Ll(}sYdbCxDEaGiMi?oE7Qe|)hT|INaIUIR~-(u*=;QNGJPen7eX?%9h^(+=)GRz%rc$6Pk=rs+}5EXsJ9%Fr;mE>Q>(1k)N6c*zgt533RAqm zs%Ct_p|M0#+sSZ()QU<-q|x#Q1&ywSHg%?&9arh`(5vpQ=;jmqkx;vxzkO}FX#mxZ zdlUwZu;qHiciF7XTxsRFRF+vPQL#)!431_Fy4pNk^w1^-!5z&RR+O$^b7)s2{2y*` z@R-?M;jg%p>R=cAlxBf0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+test('class constructor return Result', (t) => { + const c = new Context() + t.is(c.method(), 'not empty') +}) + +test('class Factory return Result', (t) => { + const c = Context.withData('not empty') + t.is(c.method(), 'not empty') +}) + test('callback', (t) => { getCwd((cwd) => { t.is(cwd, process.cwd()) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index c532a62e..caa26c4f 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -50,6 +50,12 @@ export class Blake2BHasher { } export class Blake2BKey { +} +export class Context { + + constructor() + static withData(data: string): Context + method(): string } export class ClassWithFactory { name: string diff --git a/examples/napi/src/class.rs b/examples/napi/src/class.rs index 7d6ef668..154d0117 100644 --- a/examples/napi/src/class.rs +++ b/examples/napi/src/class.rs @@ -1,4 +1,5 @@ use napi::bindgen_prelude::*; +use napi::Result; use crate::r#enum::Kind; @@ -75,3 +76,29 @@ impl Blake2bKey { self.0 } } + +#[napi] +pub struct Context { + data: String, +} + +// Test for return `napi::Result` and `Result` +#[napi] +impl Context { + #[napi(constructor)] + pub fn new() -> napi::Result { + Ok(Self { + data: "not empty".into(), + }) + } + + #[napi(factory)] + pub fn with_data(data: String) -> Result { + Ok(Self { data }) + } + + #[napi] + pub fn method(&self) -> String { + self.data.clone() + } +}