From fb4786e2bf411a8cab47254e75f22796100d8fde 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. 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 | 78 +++++++++++++++++++++++++- src/node_api.cc | 49 +++++++++++++++- src/node_api.h | 16 ++++++ src/node_api_types.h | 4 ++ 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, 242 insertions(+), 3 deletions(-) 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 cb322345d534f0..b739c718294f37 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -128,9 +128,13 @@ typedef enum { napi_escape_called_twice, napi_handle_scope_mismatch, napi_callback_scope_mismatch, -#ifdef NAPI_EXPERIMENTAL +#if NAPI_VERSION >= 4 napi_queue_full, napi_closing, +#endif // NAPI_VERSION >= 4 +#define NAPI_EXPERIMENTAL + napi_bigint_expected, + napi_date_expected, #endif // NAPI_EXPERIMENTAL } napi_status; ``` @@ -1375,6 +1379,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