From 80ea0ad78cfab1d0d07857dfddd6101a67e40f8c Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 6 Feb 2022 20:42:48 +0800 Subject: [PATCH] fix(napi): re-export JsDate as Date in prelude --- crates/backend/src/typegen.rs | 1 + crates/napi/src/bindgen_runtime/js_values.rs | 2 ++ examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 2593 -> 2606 bytes examples/napi/__test__/values.spec.ts | 8 ++++++++ examples/napi/index.d.ts | 1 + examples/napi/src/date.rs | 6 ++++++ examples/napi/src/lib.rs | 1 + 8 files changed, 20 insertions(+) create mode 100644 examples/napi/src/date.rs diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index 846ea47c..8a3a1383 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -169,6 +169,7 @@ static KNOWN_TYPES: Lazy> = Lazy::new(|| { ("BigUint64Array", "BigUint64Array"), ("DataView", "DataView"), ("Date", "Date"), + ("JsDate", "Date"), ("JsBuffer", "Buffer"), ("Buffer", "Buffer"), ("Vec", "Array<{}>"), diff --git a/crates/napi/src/bindgen_runtime/js_values.rs b/crates/napi/src/bindgen_runtime/js_values.rs index 8e40e81a..2469f1c8 100644 --- a/crates/napi/src/bindgen_runtime/js_values.rs +++ b/crates/napi/src/bindgen_runtime/js_values.rs @@ -23,6 +23,8 @@ mod string; mod symbol; mod task; +#[cfg(feature = "napi5")] +pub use crate::JsDate as Date; pub use array::*; pub use arraybuffer::*; #[cfg(feature = "napi6")] diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 94ad6647..1d598448 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -40,6 +40,7 @@ Generated by [AVA](https://avajs.dev). /** napi = { version = 2, features = ["serde-json"] } */␊ export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void␊ export function returnJsFunction(): (...args: any[]) => any␊ + export function dateToNumber(input: Date): number␊ export function eitherStringOrNumber(input: string | number): number␊ export function returnEither(input: number): string | number␊ export function either3(input: string | number | boolean): number␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index b275c22ef9faab0b3f2fcd215d692b5bb7431577..25a061f2ae08ef337057e6905d70fef41f7825bd 100644 GIT binary patch literal 2606 zcmV+}3eojJRzVlS-7XQg#gjm^a1?1d=Iv zP}=FoT_1}G00000000B68EsG8$T@9QRpML!!6a36-e@7b30OEe%|MK9sf4)Lg8P_Uw>zc ztB~FOU5-Q=24u)0mQclrFOr1$il|A@Uyo z=MR5=_SZjte*WquQ8MI_asw`@7c%nl)e>wny}4{{;x0{|6t| zyI3TO_#u^&?B6MtL^M3{K7m}X``hGxiI9ZRKt$n$eDjT~H)gidh<{EQk(03}LjC&k zotrJMT+)ZFhouJ)Hi6aJ zPQD~RgMUM8@(5z*XE1aaK9a||qKRtaQbX1-exf3p$sBDK^3kjHub}P;q=Q*0ol*zzYSq8C@>p)R=d;HuhWKS@Ml%kmWt zJ7FMObR-Y54fJ_uWn~2jL&l)dfTs#Bq<7XAJsqnKkK$ChWDoY5Qw!s2#1a!O zCrKHz(4oi%4MdE{`!}fhjzALWGP)74{I-EE!Or5y*|Yj@(qaJv8v! zT*wcefbKk{ea3HC7YkisIYZo?5<8L_;6NnCJPYX^r|OGIqQ`@`QAp*4!5+DxVG3?e z6YLEl#iLX}x!hQXf`)NU29Vv!YNcvw+;=<*v{jMnE={g=;Ui>E3?Xh-+u56UJPaiy zS*V}peU~bDzm~u6Q>i8yX6j#K0A{uO2ay6lY+yWyuxkRkfLJUB&ko1{;tI|nVz-6l zsWkem)XUpVr4m?N_i=@SpF}K|1PYrDeS91x4EHy1aUjwJo;UINoZrFY7C!Ft;Rv<( z78gCP>HI?@GfW|Ct95<_Z2RJHW8GnHS(rJ6eUyk>okX0bbYLoA-bgu;|BG3{HPc`<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r zsvIk@x1d}oPeFqfu*s+gZ70a&^c-2YT}2_F7&I8<1$2#s7WhK<*R~!H{+>cxx%L?N zB-i_q;wXrAbN6&8Fe>alRDK^-Uw>?bT1-6H>^f@}>=xozrn>FJZU^j~$S8s+ttq;T zu49neV@NfL;sev48R&Dfcm#c%fU)JKp`v|OjHu3FTs^7Q_ID*(y-5=t_+NqE0r+-w zEJOB{$$hbi5HL0?qf^+Ya(<05zhq#3+IRZb8Vm>m$Z+~%W8LKayzRY!9GYz{yqcUI zE3j^bmR4hmPEG6Yz-PP-yOg?7!J*Elwi=;JTPI7BphmZyhc)h!fJ1(riN4QRK8ik6))Kx)^<6<8{y>$=jA|-Mlf93 zePoU`oQ>osPX71b|NP7BqjQfx8IX2Q4vabt*QS=+_bg%N%EMg2 z2Sb{GS@0Gf?-hzAX!kjVdd!r?(nG3A_IU(+hN*ypd*&ZVqH)WD{d9SC|M2Rk){7T~ z3U)F&LC-Q5Gbj^?SMdfrFMBi4an2V*lqcb&*jwkqdzOmM#r2HobD6!G<+O%xvBYBNe3pNcVKn-5@@rO@!Ynhxb2_vxU zvTW>KvvDhD4C_W~hHIsS05}m37L_~^=$_2j4eKDgv$Mg7j(yf;EZk{1*Mb!to@>;` zvN~$ialFO(YHoUX5t3T}K6ky4Bx?Fssy)@Ag ztLvUSCijyuUZ4pCC)&V2hxq>Y?^EF_*zFmOhp41O*t**>Ro5LE*)S*=;N(1=jiwLz zNVS#1uWWCKeV8$teR*GTFhJSkq0=8_q~>G^5Oh zeV(8XvLagH_fAxIOLehTB4JBt6{LppHY>pFwSI*+6&OfP#W?5Q-3>PkImpJ8hH$(AxTU6C@MX6_fyIKA{gNr&ZLYVRt^{N|eLLVyAJ4 z$;M!kY*pfC+C_XH@&Obd&lTaJ#&+wj?OcV{@vdUQMP{{4KP { @@ -548,3 +549,10 @@ Napi4Test('Promise should reject raw error in rust', async (t) => { const err = await t.throwsAsync(() => asyncPlus100(Promise.reject(fxError))) t.is(err, fxError) }) + +const Napi5Test = Number(process.versions.napi) >= 5 ? test : test.skip + +Napi5Test('Date test', (t) => { + const fixture = new Date('2016-12-24') + t.is(dateToNumber(fixture), fixture.valueOf()) +}) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index dad68889..84c3e695 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -30,6 +30,7 @@ export function optionOnly(callback: (arg0?: string | undefined | null) => void) /** napi = { version = 2, features = ["serde-json"] } */ export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void export function returnJsFunction(): (...args: any[]) => any +export function dateToNumber(input: Date): number export function eitherStringOrNumber(input: string | number): number export function returnEither(input: number): string | number export function either3(input: string | number | boolean): number diff --git a/examples/napi/src/date.rs b/examples/napi/src/date.rs new file mode 100644 index 00000000..6987cd53 --- /dev/null +++ b/examples/napi/src/date.rs @@ -0,0 +1,6 @@ +use napi::bindgen_prelude::*; + +#[napi] +fn date_to_number(input: Date) -> Result { + input.value_of() +} diff --git a/examples/napi/src/lib.rs b/examples/napi/src/lib.rs index ce416cf6..98005952 100644 --- a/examples/napi/src/lib.rs +++ b/examples/napi/src/lib.rs @@ -18,6 +18,7 @@ mod bigint; mod callback; mod class; mod class_factory; +mod date; mod either; mod r#enum; mod error;