From 76798a4343972349daf0d5e297a7edf7cd45de72 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 24 Dec 2021 22:56:16 +0800 Subject: [PATCH] fix(napi-derive): missing wrap in NAPI-RS created class instance --- crates/backend/src/codegen/struct.rs | 24 +++++++++++++++----- examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 2131 -> 2141 bytes examples/napi/__test__/values.spec.ts | 1 + examples/napi/index.d.ts | 1 + examples/napi/src/class.rs | 5 ++++ 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index 79df9c9e..539d46d5 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -170,25 +170,37 @@ impl NapiStruct { quote! { impl napi::bindgen_prelude::ToNapiValue for #name { unsafe fn to_napi_value( - env: napi::bindgen_prelude::sys::napi_env, val: #name - ) -> napi::bindgen_prelude::Result { + env: napi::sys::napi_env, val: #name + ) -> napi::Result { if let Some(ctor_ref) = napi::bindgen_prelude::get_class_constructor(#js_name_str) { let mut ctor = std::ptr::null_mut(); - napi::bindgen_prelude::check_status!( - napi::bindgen_prelude::sys::napi_get_reference_value(env, ctor_ref, &mut ctor), + napi::check_status!( + napi::sys::napi_get_reference_value(env, ctor_ref, &mut ctor), "Failed to get constructor of class `{}`", #js_name_str )?; let mut result = std::ptr::null_mut(); napi::bindgen_prelude::___CALL_FROM_FACTORY.store(true, std::sync::atomic::Ordering::Relaxed); - napi::bindgen_prelude::check_status!( - napi::bindgen_prelude::sys::napi_new_instance(env, ctor, 0, std::ptr::null_mut(), &mut result), + napi::check_status!( + napi::sys::napi_new_instance(env, ctor, 0, std::ptr::null_mut(), &mut result), "Failed to construct class `{}`", #js_name_str )?; napi::bindgen_prelude::___CALL_FROM_FACTORY.store(false, std::sync::atomic::Ordering::Relaxed); + napi::check_status!( + napi::sys::napi_wrap( + env, + result, + Box::into_raw(Box::new(val)) as *mut std::ffi::c_void, + Some(napi::bindgen_prelude::raw_finalize_unchecked::<#name>), + std::ptr::null_mut(), + std::ptr::null_mut(), + ), + "Failed to wrap native object of class `{}`", + #js_name_str + )?; Ok(result) } else { Err(napi::bindgen_prelude::Error::new( diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 68cb4c58..b8d3c8dd 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -154,6 +154,7 @@ Generated by [AVA](https://avajs.dev). export class Bird {␊ name: string␊ constructor(name: string)␊ + getCount(): number␊ }␊ /** Smoking test for type generation */␊ export class Blake2BHasher {␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 870adc528bd485e780403fd6c804d4afffcaca71..ff404ed7920740490a7cc2834a52a5cffe2d49c6 100644 GIT binary patch literal 2141 zcmV-j2%`5vRzV)giFKoHlz%a5yawLsK&M=&zZFOTH zKOv_cdI^wA@*{F93iRG{|3Z;d&V6r<v_ef3yOJ z%p9&dhj>aeEmwz3u#{>R5`RK^omWSzr)LI$-6k^CB#5X|J zjS>_BnEn32Vdwm1|BLR)S-(w07W*tMAO@KTG?#)5nf^?sq4Jt-(n(V~d7^a6#ci5$mXl2)u;q5vaq62sp1& z8FR&+>}7*N6-SCl$62I#|3-Q=9TsdH+&XD}J_HBogrP^7A2`>PN zHM5uA!@!Pqw(o%i{dR9cFQ8H6(_q{t_>lFf7Rb|GaxHlX1n{4+o1h6NPsv+y4K_m> zPswJB41i;nG6lPf4-`v7wtA(c_~4Sn#z=X10T(Q65xCfRR>M!(ldj9lJ<;IJT8(g6%7o^rQp_t!5O)xQ3h6}DZ|LuTx1dw z!`3E*uBONlJe{nU@`J&B!9{39Myh)>9h(p*WM2-!P1ala%?lny3PJ)hg}v`m4fh+x zeV-~l$uYBFjQ*Bqsn2Bw{IUp=B{pP?QW$@xL<1a~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fdUnWXy7O=VWWAN&yyQ`&AS57D*!wQkaKBO9 z_o>p895V~X=x=$J`buWNFN+{qf0h;pG+t8bQK};VFi~)eQ)ST?k@Trif}SKy4#?0PRDmv6g=v4RO21G&N2#;2(1{{z z&cfYI;zIwuuVexU9kTnZUy+|u7BGIzN*qy*K&mWBrBAsJb&_xA)luin$lfW6!yYEe znH=t$A<9??St!fj3;tpVz;fy@6)zZwQ~QQ6C*fhU=u&GDmXoS=Pwe|%N157KU-yzy zuUFCi%{A1*c?XuL1ezZ(#Ts@?PFb1(y|w^f;C*4*!m>q$gt}LBW8iUE26M8uZ02_k zR>%?trV#;W>Uk-LP%+u9cAAh~@Bb{+DsQ5p^EUKxXIm8t&UFv-b28h{;ZC!3IfkKN zQ7)9HplSzGHMR)!GQhwoWO;v7MIoRXd<8vc3SG|*fY0?{W9Q-EuPF2?8xMh3OrM$I z?MPr+vV&YP(A6g?o5(CCYCWF^7r}0zg_N1@_;A_u9;Y#WeiZLoz0i-N<)qB#fN46`*?W10(uD1ovB z2on-B$Pl5W1k<4AfeCXY;ZQ^K8c6{PT+Gn+noccJ#bE_k{7U7pw%Zs#x~xVxR0T4Q zVFIz|$Q%nSi{%w3Km7GCe>CSP-pxqTn9s3w-(;nSaG84yNDHH?c}{bxQ`^O!pn{5c;g3^Wa~{aSxU=IUcU57CR^7K>NF1|PsosV_ z-iy{-qBRFtGHj2FE85Re9kIIhuWRxnnc$ZQf#5_N_}@Sb{ONCVE)5g#DNRQBNQbbE z%wwv?T^U&)oie}?WIkdSja2yhVV)K$q;zc1D@A&{M8YP8Do70hI}-peRf0`24BuOZ z@!Vhmef*2w@j001UH47mUR diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index d1c9ae5c..99ce7fa2 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -129,6 +129,7 @@ test('class', (t) => { t.is(dog.name, '可乐') t.deepEqual(dog.returnOtherClass(), new Dog('Doge')) t.deepEqual(dog.returnOtherClassWithCustomConstructor(), new Bird('parrot')) + t.is(dog.returnOtherClassWithCustomConstructor().getCount(), 1234) }) test('class factory', (t) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 5224aef8..a64af8ad 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -144,6 +144,7 @@ export class Dog { export class Bird { name: string constructor(name: string) + getCount(): number } /** Smoking test for type generation */ export class Blake2BHasher { diff --git a/examples/napi/src/class.rs b/examples/napi/src/class.rs index 4c1a7562..f1d5e720 100644 --- a/examples/napi/src/class.rs +++ b/examples/napi/src/class.rs @@ -90,6 +90,11 @@ impl Bird { pub fn new(name: String) -> Self { Bird { name } } + + #[napi] + pub fn get_count(&self) -> u32 { + 1234 + } } /// Smoking test for type generation