From 43415251b8626ed96a7878c57414d6cc0c08446f Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 22 Feb 2024 15:37:50 +0100 Subject: [PATCH] feat(napi): allow Reference as a class method param (#1966) As of before this commit, there was a lock in the codegen preventing Reference from being used as a function argument outside of a Reference. This changes it, allowing Reference of any class to be added as a class method argument anywhere. It has the same limitations as reference, as in it requires the class to have been created with a factory or constructor. This change implements FromNapiValue on Reference, which will unwrap the class and call the existing from_value_ptr method. It also updated typegen so that we only emit the reference type if we're in an impl block that doesn't match the Reference we're getting. This ensures that typegen works as expected with the previous behaviour. --- crates/backend/src/codegen/fn.rs | 5 ---- crates/backend/src/typegen/fn.rs | 19 ++++++++++++++- .../bindgen_runtime/js_values/value_ref.rs | 23 +++++++++++++++--- .../__snapshots__/typegen.spec.ts.md | 1 + .../__snapshots__/typegen.spec.ts.snap | Bin 4431 -> 4444 bytes .../__snapshots__/values.spec.ts.snap | Bin 368 -> 368 bytes examples/napi/__tests__/values.spec.ts | 2 ++ examples/napi/index.d.ts | 1 + examples/napi/src/reference.rs | 7 ++++++ 9 files changed, 49 insertions(+), 9 deletions(-) diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index 3fd41b48..a2f3d42b 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -272,11 +272,6 @@ impl NapiFn { }); skipped_arg_count += 1; continue; - } else { - bail_span!( - p, - "The `T` of Reference type must be the same as the class type" - ) } } } diff --git a/crates/backend/src/typegen/fn.rs b/crates/backend/src/typegen/fn.rs index ff798139..5fd6be9e 100644 --- a/crates/backend/src/typegen/fn.rs +++ b/crates/backend/src/typegen/fn.rs @@ -141,7 +141,24 @@ impl NapiFn { if let syn::Type::Path(path) = path.ty.as_ref() { if let Some(PathSegment { ident, arguments }) = path.path.segments.last() { if ident == "Reference" || ident == "WeakReference" { - return None; + if let Some(parent) = &self.parent { + if let PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { + args: angle_bracketed_args, + .. + }) = arguments + { + if let Some(syn::GenericArgument::Type(ty)) = angle_bracketed_args.first() { + if let syn::Type::Path(syn::TypePath { path, .. }) = ty { + if let Some(segment) = path.segments.first() { + if segment.ident.to_string() == parent.to_string() { + // If we have a Reference in an impl A block, it shouldn't be an arg + return None; + } + } + } + } + } + } } if ident == "This" || ident == "this" { if self.kind != FnKind::Normal { diff --git a/crates/napi/src/bindgen_runtime/js_values/value_ref.rs b/crates/napi/src/bindgen_runtime/js_values/value_ref.rs index 18479379..d325bddb 100644 --- a/crates/napi/src/bindgen_runtime/js_values/value_ref.rs +++ b/crates/napi/src/bindgen_runtime/js_values/value_ref.rs @@ -2,8 +2,10 @@ use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::ffi::c_void; use std::ops::{Deref, DerefMut}; +use std::ptr; use std::rc::{Rc, Weak}; +use crate::bindgen_prelude::FromNapiValue; use crate::{bindgen_runtime::ToNapiValue, check_status, Env, Error, Result, Status}; type RefInformation = ( @@ -94,7 +96,7 @@ impl Reference { impl ToNapiValue for Reference { unsafe fn to_napi_value(env: crate::sys::napi_env, val: Self) -> Result { - let mut result = std::ptr::null_mut(); + let mut result = ptr::null_mut(); check_status!( unsafe { crate::sys::napi_get_reference_value(env, val.napi_ref, &mut result) }, "Failed to get reference value" @@ -103,6 +105,21 @@ impl ToNapiValue for Reference { } } +impl FromNapiValue for Reference { + unsafe fn from_napi_value( + env: crate::sys::napi_env, + napi_val: crate::sys::napi_value, + ) -> Result { + let mut value = ptr::null_mut(); + check_status!( + unsafe { crate::sys::napi_unwrap(env, napi_val, &mut value) }, + "Unwrap value [{}] from class Reference failed", + std::any::type_name::(), + )?; + unsafe { Reference::from_value_ptr(value.cast(), env) } + } +} + impl Reference { pub fn clone(&self, env: Env) -> Result { let mut ref_count = 0; @@ -188,7 +205,7 @@ impl ToNapiValue for WeakReference { ), )); }; - let mut result = std::ptr::null_mut(); + let mut result = ptr::null_mut(); check_status!( unsafe { crate::sys::napi_get_reference_value(env, val.napi_ref, &mut result) }, "Failed to get reference value" @@ -276,7 +293,7 @@ impl SharedReference { impl ToNapiValue for SharedReference { unsafe fn to_napi_value(env: crate::sys::napi_env, val: Self) -> Result { - let mut result = std::ptr::null_mut(); + let mut result = ptr::null_mut(); check_status!( unsafe { crate::sys::napi_get_reference_value(env, val.owner.napi_ref, &mut result) }, "Failed to get reference value" diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index c9d8768e..e1a4e7ac 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -174,6 +174,7 @@ Generated by [AVA](https://avajs.dev). }␊ ␊ export class JsRemote {␊ + constructor(repo: JsRepo)␊ name(): string␊ }␊ ␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 25cdfede321ea6cedb44a78f1d839baf82bba167..65024907062463c5f71d9e0c4dd5b2405ad5800b 100644 GIT binary patch literal 4444 zcmV-i5u@%wRzVz+wzW@hr_&jK>l>iC)A4R*tyr?hn0u7({M@bE-Ws=5TK=vtC{u<`ng{^?GyG9?m75Ff+R?;WH*VEC3wyc2f)Gk2QdFy z%wry2iogAvN+x4^{X4PZc^r`?O=uQKNpNe)Yv~{^#iU>#s!|N@TDkY@GsWLIOb?c%=K^w=i>T;It0k z-NT_*20lsHI*8TaKuq!-OCrJ-B+$)3?F{)6UXB%sp`NXnAn*@J0 ziiXr?M8Y;X_*y<(T1QCzxXxqA9)Moq*w<^CNZpQN!u%1s=IGm+C@beq_A(y4vaTrP4=g^?>^pq{ipGlUw+gLH^opgD)THkM_oKbMMBv? zOwXI|VLlFXA^G~y(FhOdDf1p{$B6^ZU>;sGPAf9B)^W;=1z#!onEbnY%r)*^D zBbs3A$Hcz5v5+_Sd@7D&yr7E!R?U$^DZZ;4@TiwR$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+VSu7`%fEzkV5_S8>3m)_%N`kTPZ5U-KM@N%YK>2Tt<5y|$Eqf3E_NRAk)&NGq^-qt;X(wVH4IHnXbKu*60E+1c*ZaV|p@@=UhB|{>B}ggZZX_qU#u3{xvS36`UXky;9dB zg*m0cjPj{7A((b|5iFEqQ*Az5UD|fMo7x4>dSHfCx;*Qy8)@x#ylSV;WH zUQ3bZAftExcZgzBQxq`NL-Vl70%8ZE(3|wJ3EcJd?AcROrS=O{s)Cx%LDQlZXfLj> zXZHKS2-qIpD_`@qLzMB!&ud3=6CcZyvbyv$KG;ubOq$aNKBnboPucvs*K*sB#Vu#~ z>3Dr8#!4c7#3khiQw Vd=3Bf>}kj1{XfkX#u9@@0063Dq96bO diff --git a/examples/napi/__tests__/__snapshots__/values.spec.ts.snap b/examples/napi/__tests__/__snapshots__/values.spec.ts.snap index 84b413863d284fa14c228a24b35f2156dcc80667..f693200f2138abfed89c24cef35dfd670dffa354 100644 GIT binary patch literal 368 zcmV-$0gwJcRzVv99O>~Is8y0~`aaZ`1L zD$c+G%92~K?oO#rp3{9@~e-+_>ri_b)2wVAKkguT`-^XEj>c;L-)$ zAL?2Lc?(~W7>MHsDdczbUl32kEAdWz5(x-#Nn8;(#FBUssnHW>5DAEiC5H)z!^%6vLfMwpfK{rDs)BcxP0A+u zKi-h@7`6SYXB_vBGjTfN^x~KnjoI95TUbZ$**LRXXk(DO<8H&BQ*l1we6&^b+Ur(% O?Y;pje7w^o0ssI>CaIkO literal 368 zcmV-$0gwJcRzV$19CGwq&^o{O!r5} zYrmx}G9QZw00000000A3k2_8SF%(7bV=|LJqbSh88b;};!wzWaaAq9h;MgNSXQEUn zVht=Hlrm-p$=(@us^*1{yxUim zit3iW60~tR)S>(iz#PC6fL8$T06qa^1Y8nuMZgUK3j!VqcqZVDfDZ!3l#M984nb{+ zLo^RhuI4q_pN&5{W%O+Ku8>5!No6m6q&8vf(stYt0ku zE5!@ZiRgUVn5a2UX2EY*2MJmd^dwjxC#2#M)hL0Pz-}2lbD>=$sx7LctuABmPE1Ek z_J3Yic*@58g3RWOYX O#(x7Ue7w^o0ssJ;2cfb6 diff --git a/examples/napi/__tests__/values.spec.ts b/examples/napi/__tests__/values.spec.ts index 493f27b2..ecc6458b 100644 --- a/examples/napi/__tests__/values.spec.ts +++ b/examples/napi/__tests__/values.spec.ts @@ -134,6 +134,7 @@ const { bufferPassThrough, arrayBufferPassThrough, JsRepo, + JsRemote, CssStyleSheet, CatchOnConstructor, CatchOnConstructor2, @@ -409,6 +410,7 @@ test('custom finalize class', (t) => { test('should be able to create object reference and shared reference', (t) => { const repo = new JsRepo('.') t.is(repo.remote().name(), 'origin') + t.is(new JsRemote(repo).name(), 'origin') }) test('should be able to into_reference', (t) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 2f305b25..7874fe39 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -164,6 +164,7 @@ export class JsClassForEither { } export class JsRemote { + constructor(repo: JsRepo) name(): string } diff --git a/examples/napi/src/reference.rs b/examples/napi/src/reference.rs index c87af198..d9519c2b 100644 --- a/examples/napi/src/reference.rs +++ b/examples/napi/src/reference.rs @@ -51,6 +51,13 @@ pub struct JsRemote { #[napi] impl JsRemote { + #[napi(constructor)] + pub fn new(repo: Reference, env: Env) -> Result { + Ok(Self { + inner: repo.share_with(env, |repo| Ok(repo.inner.remote()))?, + }) + } + #[napi] pub fn name(&self) -> String { self.inner.name()