From aeb0b4766d9554519eb4002e02910ebb2b4a0b36 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 28 Feb 2024 14:29:02 +0800 Subject: [PATCH] fix(napi): add back the typecheck logic that was accidentally removed in Object::get_named_property (#1982) - Close https://github.com/napi-rs/napi-rs/issues/1641 --- crates/napi/src/js_values/mod.rs | 8 ++++- .../__snapshots__/typegen.spec.ts.md | 2 ++ .../__snapshots__/typegen.spec.ts.snap | Bin 4638 -> 4678 bytes .../__snapshots__/values.spec.ts.snap | Bin 380 -> 378 bytes examples/napi/__tests__/values.spec.ts | 31 ++++++++++++++++++ examples/napi/index.cjs | 1 + examples/napi/index.d.cts | 2 ++ examples/napi/src/object.rs | 7 ++++ 8 files changed, 50 insertions(+), 1 deletion(-) diff --git a/crates/napi/src/js_values/mod.rs b/crates/napi/src/js_values/mod.rs index f4e0d8e4..2deeffa8 100644 --- a/crates/napi/src/js_values/mod.rs +++ b/crates/napi/src/js_values/mod.rs @@ -347,7 +347,7 @@ macro_rules! impl_object_methods { pub fn get_named_property(&self, name: &str) -> Result where - T: FromNapiValue, + T: FromNapiValue + ValidateNapiValue, { let key = CString::new(name)?; let mut raw_value = ptr::null_mut(); @@ -357,6 +357,12 @@ macro_rules! impl_object_methods { }, "get_named_property error" )?; + unsafe { ::validate(self.0.env, raw_value) }.map_err( + |mut err| { + err.reason = format!("Object property '{name}' type mismatch. {}", err.reason); + err + }, + )?; unsafe { ::from_napi_value(self.0.env, raw_value) } } diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index 3d5302db..f529533c 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -515,6 +515,8 @@ Generated by [AVA](https://avajs.dev). bird: Bird␊ }␊ ␊ + export function objectGetNamedPropertyShouldPerformTypecheck(obj: { foo: number; bar: string; }): void␊ + ␊ export interface ObjectOnlyFromJs {␊ count: number␊ callback: (err: Error | null, arg: number) => any␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index ac1ef45aef9010e639222aeebfd120a23b38bdaf..1d5b5c85015b76e54e7a09dbd681dc8a11111ec2 100644 GIT binary patch literal 4678 zcmV-M61nX`RzV9}*3FANra&#kUngsFr>=Ur?-E@z9c|=G`gNP^bhCF^eQVo@+ zEMb4lC=r|0jK}Knhwn$`_Tz|5q3QO~EimxSBk~RTDddTeX%Uv8?vHu$s>&@$n9ARecS^W;}m1-blUL^jQ0s@57FU&8e}CL|yj0HrxC{bb_M zzw*LXM;5{iRpw9+joSDkkV_ohEKYggw?YHF(m+0N(S$x=#Y&EHq(4DZ9Q%h6b{tU| z2xY+cTASXZ;oy*GiFD?s8qc8LSZtCIA6%q-1#0&@O|}O;f-t0OIRVW~jkpTD9+CH8 zZC;MkG}yR{g@%AkRvhGJK_nGYX@4~-W4q8IR5kQL99+@egWm+g%8#oL>1J?6VISyT zGq~Rxl}UggTZ84K12muTFq_ZSDu3oIs;sR+F=M@|0D1X>5rHW`4nBM3bgUp81~Ocp zC)MrPzCG$hyM3svCOS6u#UmijvYypU#;68Srg-inu@X}y-Sg01QUR*zP6%2)t0WSv zNqzo2k@QAy?p3gv(KAY;{SldQ9@8L!)|nCuS9Co))nWQI2$PYhVu|!9<&Ik327q7L z7e#Mv>>ayUW53XLRux`aO{n0UO^Lc{4Hhwxg`!g$(^ZIGf5ESBfG!{VRY~vGc&sSw zX3L?}c!l+?HWd%L#-qR0^nH50HiMEWXtM#@QXez$h1t)vHjQf*$)#0kL$ai7v6Rg= z&=}nJf}X*`qVKH)**MqA*^SN=?J;`0j`FonCu(ub(|oIH=Q)TS+I@%EK6Z%r8!SN! z7sECthOImdTUqd|6JOlTWK{u<*-Qa^n5f`mn@Kb$JpG6wa$;`2v&^C99~xHoI&~*t z@BRXCqY6R@Bftg;&^wN$M{M7K&$Q%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2AXo`ZnNA9?v=JpxT6hC&mV}9I;`v2hxZ@MTE|3Kx7O2&`p z3)K-^x1A)FFDkjOo5TEa1d~*<#Bmr-#V(-)ANxdCvXA>sy}S49vGv6y3_858b$jR&av! zd#TFD3X@_7bINDVykPyoMYm9hP4)I^`Qe3o9?|)EJjsj|(v7psc|6N}t?rY&HO-7L z^*pm}IEleT^URgr-Z&%WUf+HW@wUFxh`c`IS7E=Ft#qTLZffT|>Au;^!o6G97h8~7 z7p=!OatRA6*JB*?uCC=TXx;Zxu_^5f^LGf=_L7CeZ3o)MBxMr(1{FmQa+5|(Pm6yn z;g~M{)?C|3@;D}ZU;}Td*1kz!rr#hi^CVs&SzbGHFnwai>^=`08zKgEEt}pf-K;y_ zqg*2ZE=X)=lnTOzwahjvAF;peu>pDW3z|(auJs+xTCgJAWP|eUb?pTH1&858Nc^KY zT8bPZXP(l9L%dG8db?QJg^JdXqUefxEhzKY46AD;m0~ra;l|t0fwktE;*F zz&!%Ci(hlB`Nk+p_~?hVoP3S*@~Eg?{(u+ulRJ~2_7SVO^MfDR{4*Vv+WyrSOIiF4 zm^v%Qi^S(Cn}G`ZOo{nxPn95f`1a`4$??mRajFR2}v-`AMe#mUNC-~h)%U;Cy U@Q+U(cWmDO0C2LT%~MVQ03AIY;s5{u diff --git a/examples/napi/__tests__/__snapshots__/values.spec.ts.snap b/examples/napi/__tests__/__snapshots__/values.spec.ts.snap index 22206d792e490489581247c0692c5680fee4a4e0..d4241d4d7d0d6ebb4343f3ab1adc96ea29a9ee41 100644 GIT binary patch literal 378 zcmV-=0fqiSRzV9W}Xk*k*&m6J73C%E>1RWi`! zF71*rT_1}G00000000A3kxfp+Fc3yPP1FC9R=@^v1fpJ{Jq8jRCvmBRYeyb8QI}n@ z;|@h)!5O#&w_wQ;U@Dj(BE7`=z9)HNAJ>_2`2!xFRnWnx5|uAhTB5TWt*mh40v?V< zA%nc_Tt}iWP7e%VR?xR39*I}to%keXAjA!^BJPNqcp_eiH{yf%BF>>Vhj`_^+yn3` zNfH4g0rZ}b(GeFA35e$~S<*B*^|;85{w)ExowaT7x-tsc=&*6w8jZwl>UTEitzs== zZMqEmO5wt|LgI$&F4xJj)jDhZ!oe%xJv>60D$|n$p8QV literal 380 zcmV-?0fYWQRzV8k@#ITdcE-py zOSrHMh98Rv00000000A3kWWqmF%*Y?4;cRIK+uix2u`>H9K*y-nJH2%?WC^^Vadv! zcfiDjXYdl3cmc7uM$we0bK;M61`VL!q-ZduQ0Gl2<mekGCEQEg)|rPVA&LyF6~*eTv*Ve6vV zXvvzAoz3FD)I7DmP`nb|5}nQ}6E(-_BKRHaAVDHQUxIBtWLsQQRZ3t-U@s4zIn%BZ z)fLs%R_8HzC#EN+{eQf^@Pv)~%Ljs6r { test('object', (t) => { t.deepEqual(listObjKeys({ name: 'John Doe', age: 20 }), ['name', 'age']) t.deepEqual(createObj(), { test: 1 }) + t.throws( + () => + objectGetNamedPropertyShouldPerformTypecheck({ + // @ts-expect-error + foo: '2', + bar: '3', + }), + { + message: `Object property 'foo' type mismatch. Expect value to be Number, but received String`, + code: 'InvalidArg', + }, + ) + t.throws( + () => + objectGetNamedPropertyShouldPerformTypecheck({ + foo: 2, + // @ts-expect-error + bar: 3, + }), + { + message: `Object property 'bar' type mismatch. Expect value to be String, but received Number`, + code: 'InvalidArg', + }, + ) + t.notThrows(() => + objectGetNamedPropertyShouldPerformTypecheck({ + foo: 2, + bar: '3', + }), + ) }) test('get str from object', (t) => { diff --git a/examples/napi/index.cjs b/examples/napi/index.cjs index a90e361e..15c078f9 100644 --- a/examples/napi/index.cjs +++ b/examples/napi/index.cjs @@ -476,6 +476,7 @@ module.exports.listObjKeys = nativeBinding.listObjKeys module.exports.mapOption = nativeBinding.mapOption module.exports.mutateExternal = nativeBinding.mutateExternal module.exports.mutateTypedArray = nativeBinding.mutateTypedArray +module.exports.objectGetNamedPropertyShouldPerformTypecheck = nativeBinding.objectGetNamedPropertyShouldPerformTypecheck module.exports.optionEnd = nativeBinding.optionEnd module.exports.optionOnly = nativeBinding.optionOnly module.exports.optionStart = nativeBinding.optionStart diff --git a/examples/napi/index.d.cts b/examples/napi/index.d.cts index 215487f3..40f22290 100644 --- a/examples/napi/index.d.cts +++ b/examples/napi/index.d.cts @@ -505,6 +505,8 @@ export interface ObjectFieldClassInstance { bird: Bird } +export function objectGetNamedPropertyShouldPerformTypecheck(obj: { foo: number; bar: string; }): void + export interface ObjectOnlyFromJs { count: number callback: (err: Error | null, arg: number) => any diff --git a/examples/napi/src/object.rs b/examples/napi/src/object.rs index 63ee457f..480b0f60 100644 --- a/examples/napi/src/object.rs +++ b/examples/napi/src/object.rs @@ -124,3 +124,10 @@ fn receive_object_only_from_js( ); }); } + +#[napi(ts_args_type = "obj: { foo: number; bar: string; }")] +fn object_get_named_property_should_perform_typecheck(obj: Object) -> Result<()> { + obj.get_named_property::("foo")?; + obj.get_named_property::("bar")?; + Ok(()) +}