From cde1ce17115fd8fa636f3c521daabc0c1bba0c00 Mon Sep 17 00:00:00 2001 From: Jarrod Connolly Date: Sun, 3 Feb 2019 23:26:33 -0800 Subject: [PATCH 1/5] n-api: implement date object Implements `napi_create_date()` as well as `napi_is_date()` to allow working with JavaScript Date objects. --- doc/api/n-api.md | 47 +++++++++++++++++++++ src/js_native_api.h | 10 +++++ src/js_native_api_v8.cc | 27 ++++++++++++ test/js-native-api/test_date/binding.gyp | 11 +++++ test/js-native-api/test_date/test.js | 19 +++++++++ test/js-native-api/test_date/test_date.c | 52 ++++++++++++++++++++++++ 6 files changed, 166 insertions(+) create mode 100644 test/js-native-api/test_date/binding.gyp create mode 100644 test/js-native-api/test_date/test.js create mode 100644 test/js-native-api/test_date/test_date.c diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 2b061167f6cf4c..dfe347d7cef5bd 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1527,6 +1527,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 `node::Date`. + +Returns `napi_ok` if the API succeeded. + +This API allocates a `node::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_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 `node::Date` +object. + +Returns `napi_ok` if the API succeeded. + +This API checks if the `Object` passed in is a date. + ### napi_is_error + +> 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 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 @@ -1544,11 +1544,11 @@ napi_status napi_create_date(napi_env env, - `[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 `node::Date`. +- `[out] result`: A `napi_value` representing a JavaScript `Date`. Returns `napi_ok` if the API succeeded. -This API allocates a `node::Date` object. +This API allocates a JavaScript `Date` object. JavaScript `Date` objects are described in [Section 20.3][] of the ECMAScript Language Specification. @@ -2175,8 +2175,8 @@ This API returns various properties of a `DataView`. #### napi_get_date_value > Stability: 1 - Experimental @@ -2784,8 +2784,8 @@ This API checks if the `Object` passed in is a buffer. ### napi_is_date > Stability: 1 - Experimental @@ -2796,7 +2796,7 @@ 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 `node::Date` +- `[out] result`: Whether the given `napi_value` represents JavaScript `Date` object. Returns `napi_ok` if the API succeeded. From c21826fbc3604d1c8d397e3607bd7f68bbe81546 Mon Sep 17 00:00:00 2001 From: Jarrod Connolly Date: Mon, 11 Feb 2019 21:42:00 -0800 Subject: [PATCH 5/5] grammar changes and setting `napiVersion` to 4 from pr review --- doc/api/n-api.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index b6123e24b86729..503046a529f6fe 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1531,7 +1531,7 @@ structure, in most cases using a `TypedArray` will suffice. #### napi_create_date > Stability: 1 - Experimental @@ -2176,7 +2176,7 @@ This API returns various properties of a `DataView`. #### napi_get_date_value > Stability: 1 - Experimental @@ -2188,7 +2188,7 @@ napi_status napi_get_date_value(napi_env env, ``` - `[in] env`: The environment that the API is invoked under. -- `[in] value`: `napi_value` representing JavaScript `Date`. +- `[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. @@ -2785,7 +2785,7 @@ This API checks if the `Object` passed in is a buffer. ### napi_is_date > Stability: 1 - Experimental @@ -2796,7 +2796,7 @@ 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 JavaScript `Date` +- `[out] result`: Whether the given `napi_value` represents a JavaScript `Date` object. Returns `napi_ok` if the API succeeded.