From 4f41e4f471180e9b86c92e51814b9aa7c5fe00dc Mon Sep 17 00:00:00 2001 From: Jarrod Connolly Date: Sun, 3 Feb 2019 23:26:33 -0800 Subject: [PATCH] n-api: implement date object Implements `napi_create_date()` as well as `napi_is_date()` to allow working with JavaScript Date objects. Backport-PR-URL: https://github.com/nodejs/node/pull/28298 PR-URL: https://github.com/nodejs/node/pull/25917 Reviewed-By: Anna Henningsen Reviewed-By: Michael Dawson Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- doc/api/n-api.md | 73 ++++++++++++++++++++++++++ src/node_api.cc | 45 +++++++++++++++- src/node_api.h | 14 +++++ src/node_api_types.h | 1 + test/addons-napi/test_date/binding.gyp | 10 ++++ test/addons-napi/test_date/test.js | 21 ++++++++ test/addons-napi/test_date/test_date.c | 67 +++++++++++++++++++++++ 7 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 test/addons-napi/test_date/binding.gyp create mode 100644 test/addons-napi/test_date/test.js create mode 100644 test/addons-napi/test_date/test_date.c diff --git a/doc/api/n-api.md b/doc/api/n-api.md index db29ef20e36b78..c9bed390b11bac 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -184,6 +184,7 @@ typedef enum { napi_queue_full, napi_closing, napi_bigint_expected, + napi_date_expected, } napi_status; ``` If additional information is required upon an API returning a failed status, @@ -1468,6 +1469,31 @@ This API allocates a `node::Buffer` object and initializes it with data copied from the passed-in buffer. While this is still a fully-supported data structure, in most cases using a `TypedArray` will suffice. +#### napi_create_date + + +> Stability: 1 - Experimental + +```C +napi_status napi_create_date(napi_env env, + double time, + napi_value* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] time`: ECMAScript time value in milliseconds since 01 January, 1970 UTC. +- `[out] result`: A `napi_value` representing a JavaScript `Date`. + +Returns `napi_ok` if the API succeeded. + +This API allocates a JavaScript `Date` object. + +JavaScript `Date` objects are described in +[Section 20.3][] of the ECMAScript Language Specification. + #### napi_create_external + +> Stability: 1 - Experimental + +```C +napi_status napi_get_date_value(napi_env env, + napi_value value, + double* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing a JavaScript `Date`. +- `[out] result`: Time value as a `double` represented as milliseconds +since midnight at the beginning of 01 January, 1970 UTC. + +Returns `napi_ok` if the API succeeded. If a non-date `napi_value` is passed +in it returns `napi_date_expected`. + +This API returns the C double primitive of time value for the given JavaScript +`Date`. + #### napi_get_value_bool + +> Stability: 1 - Experimental + +```C +napi_status napi_is_date(napi_env env, napi_value value, bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to check. +- `[out] result`: Whether the given `napi_value` represents a JavaScript `Date` +object. + +Returns `napi_ok` if the API succeeded. + +This API checks if the `Object` passed in is a date. + ### napi_is_error