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 b275c22e..25a061f2 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index 0af1fbf3..bdc5d8b4 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -82,6 +82,7 @@ import { returnJsFunction, testSerdeRoundtrip, createObjWithProperty, + dateToNumber, } from '../' test('export const', (t) => { @@ -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;