From d962e34d3ac51efac3eee53ff197d655d25fb4b2 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 10 Mar 2024 21:22:46 +0800 Subject: [PATCH] fix(napi): remove useless FromNapiValue bound check for ValidateNapiValue (#1999) --- crates/napi/src/bindgen_runtime/js_values.rs | 2 +- .../__snapshots__/typegen.spec.ts.md | 7 +++++++ .../__snapshots__/typegen.spec.ts.snap | Bin 4678 -> 4708 bytes examples/napi/__tests__/values.spec.ts | 8 ++++++++ examples/napi/index.cjs | 1 + examples/napi/index.d.cts | 7 +++++++ examples/napi/src/object.rs | 14 ++++++++++++++ 7 files changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/napi/src/bindgen_runtime/js_values.rs b/crates/napi/src/bindgen_runtime/js_values.rs index 301a7208..27bf6c6f 100644 --- a/crates/napi/src/bindgen_runtime/js_values.rs +++ b/crates/napi/src/bindgen_runtime/js_values.rs @@ -126,7 +126,7 @@ pub trait FromNapiMutRef { ) -> Result<&'static mut Self>; } -pub trait ValidateNapiValue: FromNapiValue + TypeName { +pub trait ValidateNapiValue: TypeName { /// # Safety /// /// this function called to validate whether napi value passed to rust is valid type diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index f529533c..ed0416e5 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -522,6 +522,11 @@ Generated by [AVA](https://avajs.dev). callback: (err: Error | null, arg: number) => any␊ }␊ ␊ + export interface ObjectOnlyToJs {␊ + name: number␊ + dependencies: any␊ + }␊ + ␊ export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void␊ ␊ export function optionOnly(callback: (arg0?: string | undefined | null) => void): void␊ @@ -584,6 +589,8 @@ Generated by [AVA](https://avajs.dev). ␊ export function returnNull(): null␊ ␊ + export function returnObjectOnlyToJs(): ObjectOnlyToJs␊ + ␊ export function returnUndefined(): void␊ ␊ export function returnUndefinedIfInvalid(input: boolean): boolean␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 1d5b5c85015b76e54e7a09dbd681dc8a11111ec2..4855e659d0bf7d8c0d6853cd1af161607ccb46cc 100644 GIT binary patch delta 4707 zcmV-p5}fVEB;+K2K~_N^Q*L2!b7*gLAa*kf0|3B}gO`%l6Q!fuR7>fh+iA`F)I^%% z0B{b#c|Ll4Fz6qP2mk;800003?Of|_+(>qwBnSfdU*4cHLEzS)t+8aoaC*lQTC(Jo z$GSk>-kF8h*kO~^-DR;^y{w|89!Eg-4e~pGg55XE8|))0Z_@BS| z`>+1-Z~y!3+iysaN&dtFMP#-iXXA^LCzliQ&9{%>ITbNWmqDNf4jUJ^>5gP4~!`M}(v_h+pi;krBW_4B^jf#e&&5DG* z<=|`n@YOm@>dRFYOZEix3fI0`(M0O|$R@~-&=vp0$Y1{JKmT3b$Aw%kd9Y$727lev z>ve8#Z;*zZ1k{Bry4??zvL{YBrn&6ko=nWf1&>f#|P67mLYdY=3M z@;JS#B23+dbpJb~UO)GlrjvST-Zh5BVxeShZdc^Qb z&F3OE(+M$=o0v|P6!h|oQQ0($sggB3zJ%ZJn2>;A0F>pl^plB0|H=znoqt&fGgX;G zJvCb6i$E@Mb|akf#BYTLc%^}S;GzkAzzRu@a-@HQTXF24M%Z~oWg^r8-)n7pkA{Oo zo+Z**o4R=hg!tehUwsn!}M!VCZkb>i1aMwfm+`NfM3}c#c1v99luy(zp!@J6kb|OsNkGUiMDDD z9x;)Hp;H#qHHhAL!EbMXE}#2V$?n#8tQhQO&!NnCh4rmA6A!k=W53n%eRjRJf|4s} z^8wmZA2W!B+0V4LjB6Ijr8Q|ovZQ}(v6Rg|&^X-pf}Vk3G4@u9Y=UbQ>_%sb_86mG z$N1W(6D1t;G~cQ^cn)HRcHbekj~(Lu22aq!#juTuVJi>ARu(*R;)}bvtSZ1Un<;<~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$!*1aGJ>nXS!-I zt@SP0%&r5*Qs-Qh>UP>ccqKKhRaJ9-dds*OO`z!hgwlwMaBfD#ebmL9$?f+!G)Zj= zdn}40uu5>(^pCk~Mu&9#HY&-R8f-suVB312y*F@B?VS7dJF4uie{n9L% zgwP-4U;WP^F~#V08bPH#7&%EWxuDNN?3qaYkg0GA^y~GML^`FDMS!YLAXM**6v9V+ zeDT;`KgJ039S&+b<_MQoSj`u5x_rP- zQ!7O<-;^BcvI1N3dB>uJI;^TnJ;hTmb+Y(yc*+bu5GYntUH>9LeyyA{jnRE-DvJTz zQ74;31~!kTNZ(Vw1fFR) zRZ(}YsqQRftPOz`B|MXVn~{{{8TSwGA9#REs=m<*DoGvX9%{jH()@)gp9)L~9WH}p zVayAjbC~Fs=3-NBJ}*A-FwZ?YKl3NE@g%l)HaqiYv+q>pk~OBY5vH2Y)`cxT7;iqi zR731%q*&{R&s9Fu_aL6vr~E3c3)3g+D5-tzoFzR_Yni)uUG=v)nRd|ztd~oeQ<++3 zr$)AuKj#wxD;0fdU#s6c=&V_DyX#7~-Xuk`{T?Tm7Q|ER(;4kpTKrK4@RRJ)+}TO8 zI4XQ#BWtNT-=r-E=ntk3sEZ)0(CGRpDEdI|RTviLSsB zPT0`NZ2g4A_X{FD$m?I~^u@T-_dcCqdAP|&#oKFJ3H+sq@eG>qS0Pd!#~@<9FMEbW zu!TC>QoSeYrq3J20l<0aGe#e{o0}!rCamD7>86STMY}K7XkdUwe@O2E+r_U&I=*G{ z57vNEkD|x^`8UL)cV(DG-du { bar: '3', }), ) + t.deepEqual(returnObjectOnlyToJs(), { + name: 42, + dependencies: { + '@napi-rs/cli': '^3.0.0', + rollup: '^4.0.0', + }, + }) }) test('get str from object', (t) => { diff --git a/examples/napi/index.cjs b/examples/napi/index.cjs index 403224b3..cc92f5d1 100644 --- a/examples/napi/index.cjs +++ b/examples/napi/index.cjs @@ -501,6 +501,7 @@ module.exports.returnEitherClass = nativeBinding.returnEitherClass module.exports.returnFromSharedCrate = nativeBinding.returnFromSharedCrate module.exports.returnJsFunction = nativeBinding.returnJsFunction module.exports.returnNull = nativeBinding.returnNull +module.exports.returnObjectOnlyToJs = nativeBinding.returnObjectOnlyToJs module.exports.returnUndefined = nativeBinding.returnUndefined module.exports.returnUndefinedIfInvalid = nativeBinding.returnUndefinedIfInvalid module.exports.returnUndefinedIfInvalidPromise = nativeBinding.returnUndefinedIfInvalidPromise diff --git a/examples/napi/index.d.cts b/examples/napi/index.d.cts index 40f22290..24feb183 100644 --- a/examples/napi/index.d.cts +++ b/examples/napi/index.d.cts @@ -512,6 +512,11 @@ export interface ObjectOnlyFromJs { callback: (err: Error | null, arg: number) => any } +export interface ObjectOnlyToJs { + name: number + dependencies: any +} + export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void export function optionOnly(callback: (arg0?: string | undefined | null) => void): void @@ -574,6 +579,8 @@ export function returnJsFunction(): (...args: any[]) => any export function returnNull(): null +export function returnObjectOnlyToJs(): ObjectOnlyToJs + export function returnUndefined(): void export function returnUndefinedIfInvalid(input: boolean): boolean diff --git a/examples/napi/src/object.rs b/examples/napi/src/object.rs index 480b0f60..4f85284f 100644 --- a/examples/napi/src/object.rs +++ b/examples/napi/src/object.rs @@ -131,3 +131,17 @@ fn object_get_named_property_should_perform_typecheck(obj: Object) -> Result<()> obj.get_named_property::("bar")?; Ok(()) } + +#[napi(object, object_from_js = false)] +struct ObjectOnlyToJs { + pub name: u32, + pub dependencies: serde_json::Value, +} + +#[napi] +fn return_object_only_to_js() -> ObjectOnlyToJs { + ObjectOnlyToJs { + name: 42, + dependencies: serde_json::json!({ "@napi-rs/cli": "^3.0.0", "rollup": "^4.0.0" }), + } +}