Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v8.x] n-api: implement date object #28301

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```
Expand Down Expand Up @@ -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
<!-- YAML
added: REPLACEME
napiVersion: 4
Copy link
Member

@richardlau richardlau Jun 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

napiVersion shouldn't be set for experimental API's? #28330

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to land this as is and then backport #28330.

-->

> 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
<!-- YAML
added: v8.0.0
Expand Down Expand Up @@ -1956,6 +1985,31 @@ Returns `napi_ok` if the API succeeded.

This API returns various properties of a DataView.

#### napi_get_date_value
<!-- YAML
added: REPLACEME
napiVersion: 4
-->

> 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
<!-- YAML
added: v8.0.0
Expand Down Expand Up @@ -2452,6 +2506,27 @@ Returns `napi_ok` if the API succeeded.

This API checks if the Object passed in is a buffer.

### napi_is_date
<!-- YAML
added: REPLACEME
napiVersion: 4
-->

> 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
<!-- YAML
added: v8.0.0
Expand Down Expand Up @@ -4374,6 +4449,7 @@ This API may only be called from the main thread.
[Promises]: #n_api_promises
[Script Execution]: #n_api_script_execution
[Section 12.5.5]: https://tc39.github.io/ecma262/#sec-typeof-operator
[Section 20.3]: https://tc39.github.io/ecma262/#sec-date-objects
[Section 24.3]: https://tc39.github.io/ecma262/#sec-dataview-objects
[Section 25.4]: https://tc39.github.io/ecma262/#sec-promise-objects
[Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
Expand Down
49 changes: 47 additions & 2 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,9 @@ const char* error_messages[] = {nullptr,
"Invalid handle scope usage",
"Invalid callback scope usage",
"Thread-safe function queue is full",
"Thread-safe function handle is closing"
"Thread-safe function handle is closing",
"A bigint was expected",
"A date was expected",
};

static inline napi_status napi_clear_last_error(napi_env env) {
Expand Down Expand Up @@ -1332,7 +1334,7 @@ napi_status napi_get_last_error_info(napi_env env,
// We don't have a napi_status_last as this would result in an ABI
// change each time a message was added.
static_assert(
node::arraysize(error_messages) == napi_closing + 1,
node::arraysize(error_messages) == napi_date_expected + 1,
"Count of error messages must match count of error values");
CHECK_LE(env->last_error.error_code, napi_callback_scope_mismatch);

Expand Down Expand Up @@ -3928,6 +3930,49 @@ napi_status napi_is_promise(napi_env env,
return napi_clear_last_error(env);
}

napi_status napi_create_date(napi_env env,
double time,
napi_value* result) {
NAPI_PREAMBLE(env);
CHECK_ARG(env, result);

v8::MaybeLocal<v8::Value> maybe_date =
v8::Date::New(env->isolate->GetCurrentContext(), time);
CHECK_MAYBE_EMPTY(env, maybe_date, napi_generic_failure);

*result = v8impl::JsValueFromV8LocalValue(maybe_date.ToLocalChecked());

return GET_RETURN_STATUS(env);
}

napi_status napi_is_date(napi_env env,
napi_value value,
bool* is_date) {
CHECK_ENV(env);
CHECK_ARG(env, value);
CHECK_ARG(env, is_date);

*is_date = v8impl::V8LocalValueFromJsValue(value)->IsDate();

return napi_clear_last_error(env);
}

napi_status napi_get_date_value(napi_env env,
napi_value value,
double* result) {
NAPI_PREAMBLE(env);
CHECK_ARG(env, value);
CHECK_ARG(env, result);

v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);
RETURN_STATUS_IF_FALSE(env, val->IsDate(), napi_date_expected);

v8::Local<v8::Date> date = val.As<v8::Date>();
*result = date->ValueOf();

return GET_RETURN_STATUS(env);
}

napi_status napi_run_script(napi_env env,
napi_value script,
napi_value* result) {
Expand Down
16 changes: 16 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,22 @@ napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func);

#endif // NAPI_VERSION >= 4

#ifdef NAPI_EXPERIMENTAL

// Dates
NAPI_EXTERN napi_status napi_create_date(napi_env env,
double time,
napi_value* result);

NAPI_EXTERN napi_status napi_is_date(napi_env env,
napi_value value,
bool* is_date);

NAPI_EXTERN napi_status napi_get_date_value(napi_env env,
napi_value value,
double* result);
#endif // NAPI_EXPERIMENTAL

EXTERN_C_END

#endif // SRC_NODE_API_H_
4 changes: 4 additions & 0 deletions src/node_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ typedef enum {
napi_queue_full,
napi_closing,
#endif // NAPI_VERSION >= 4
#ifdef NAPI_EXPERIMENTAL
napi_bigint_expected,
napi_date_expected,
#endif // NAPI_EXPERIMENTAL
} napi_status;

#if NAPI_VERSION >= 4
Expand Down
10 changes: 10 additions & 0 deletions test/addons-napi/test_date/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"targets": [
{
"target_name": "test_date",
"sources": [
"test_date.c"
]
}
]
}
21 changes: 21 additions & 0 deletions test/addons-napi/test_date/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const common = require('../../common');

// This tests the date-related n-api calls

const assert = require('assert');
const test_date = require(`./build/${common.buildType}/test_date`);

const dateTypeTestDate = test_date.createDate(1549183351);
assert.strictEqual(test_date.isDate(dateTypeTestDate), true);

assert.strictEqual(test_date.isDate(new Date(1549183351)), true);

assert.strictEqual(test_date.isDate(2.4), false);
assert.strictEqual(test_date.isDate('not a date'), false);
assert.strictEqual(test_date.isDate(undefined), false);
assert.strictEqual(test_date.isDate(null), false);
assert.strictEqual(test_date.isDate({}), false);

assert.strictEqual(test_date.getDateValue(new Date(1549183351)), 1549183351);
67 changes: 67 additions & 0 deletions test/addons-napi/test_date/test_date.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#define NAPI_EXPERIMENTAL

#include <node_api.h>
#include "../common.h"

static napi_value createDate(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));

NAPI_ASSERT(env, valuetype0 == napi_number,
"Wrong type of arguments. Expects a number as first argument.");

double time;
NAPI_CALL(env, napi_get_value_double(env, args[0], &time));

napi_value date;
NAPI_CALL(env, napi_create_date(env, time, &date));

return date;
}

static napi_value isDate(napi_env env, napi_callback_info info) {
napi_value date, result;
size_t argc = 1;
bool is_date;

NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &date, NULL, NULL));
NAPI_CALL(env, napi_is_date(env, date, &is_date));
NAPI_CALL(env, napi_get_boolean(env, is_date, &result));

return result;
}

static napi_value getDateValue(napi_env env, napi_callback_info info) {
napi_value date, result;
size_t argc = 1;
double value;

NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &date, NULL, NULL));
NAPI_CALL(env, napi_get_date_value(env, date, &value));
NAPI_CALL(env, napi_create_double(env, value, &result));

return result;
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_PROPERTY("createDate", createDate),
DECLARE_NAPI_PROPERTY("isDate", isDate),
DECLARE_NAPI_PROPERTY("getDateValue", getDateValue),
};

NAPI_CALL(env, napi_define_properties(
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));

return exports;
}
EXTERN_C_END

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)