Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 499
adds experimental napi_date#497
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
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Date | ||
| `Napi::Date` class is a representation of the JavaScript `Date` object. The | ||
| `Napi::Date` class inherits its behavior from `Napi::Value` class | ||
| (for more info see [`Napi::Value`](value.md)) | ||
| ## Methods | ||
| ### Constructor | ||
| Creates a new _empty_ instance of a `Napi::Date` object. | ||
| ```cpp | ||
| Napi::Date::Date(); | ||
| ``` | ||
| Creates a new _non-empty_ instance of a `Napi::Date` object. | ||
| ```cpp | ||
| Napi::Date::Date(napi_env env, napi_value value); | ||
| ``` | ||
| - `[in] env`: The environment in which to construct the `Napi::Date` object. | ||
| - `[in] value`: The `napi_value` which is a handle for a JavaScript `Date`. | ||
| ### New | ||
| Creates a new instance of a `Napi::Date` object. | ||
| ```cpp | ||
| static Napi::Date Napi::Date::New(Napi::Env env, double value); | ||
| ``` | ||
| - `[in] env`: The environment in which to construct the `Napi::Date` object. | ||
| - `[in] value`: The time value the JavaScript `Date` will contain represented | ||
| as the number of milliseconds since 1 January 1970 00:00:00 UTC. | ||
| Returns a new instance of `Napi::Date` object. | ||
| ### ValueOf | ||
| ```cpp | ||
| double Napi::Date::ValueOf() const; | ||
| ``` | ||
| Returns the time value as `double` primitive represented as the number of | ||
| milliseconds since 1 January 1970 00:00:00 UTC. | ||
| ## Operators | ||
| ### operator double | ||
| Converts a `Napi::Date` value to a `double` primitive. | ||
| ```cpp | ||
| Napi::Date::operator double() const; | ||
| ``` | ||
| ### Example | ||
| The following shows an example of casting a `Napi::Date` value to a `double` | ||
| primitive. | ||
| ```cpp | ||
| double operatorVal = Napi::Date::New(Env(), 0); // Napi::Date to double | ||
| // or | ||
| auto instanceVal = info[0].As<Napi::Date>().ValueOf(); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -116,6 +116,9 @@ namespace Napi { | ||
| #if (NAPI_VERSION > 2147483646) | ||
| class BigInt; | ||
| #endif // NAPI_EXPERIMENTAL | ||
| #if (NAPI_VERSION > 4) | ||
| class Date; | ||
| #endif | ||
| class String; | ||
| class Object; | ||
| class Array; | ||
| @@ -246,6 +249,9 @@ namespace Napi { | ||
| #if (NAPI_VERSION > 2147483646) | ||
| bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint. | ||
| #endif // NAPI_EXPERIMENTAL | ||
| #if (NAPI_VERSION > 4) | ||
| bool IsDate() const; ///< Tests if a value is a JavaScript date. | ||
| #endif | ||
| bool IsString() const; ///< Tests if a value is a JavaScript string. | ||
| bool IsSymbol() const; ///< Tests if a value is a JavaScript symbol. | ||
| bool IsArray() const; ///< Tests if a value is a JavaScript array. | ||
| @@ -358,6 +364,24 @@ namespace Napi { | ||
| }; | ||
| #endif // NAPI_EXPERIMENTAL | ||
| #if (NAPI_VERSION > 4) | ||
| /// A JavaScript date value. | ||
| class Date : public Value { | ||
| public: | ||
| /// Creates a new Date value from a double primitive. | ||
| static Date New( | ||
mathiask88 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| napi_env env, ///< N-API environment | ||
| double value ///< Number value | ||
| ); | ||
| Date(); ///< Creates a new _empty_ Date instance. | ||
| Date(napi_env env, napi_value value); ///< Wraps a N-API value primitive. | ||
| operator double() const; ///< Converts a Date value to double primitive | ||
mathiask88 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| double ValueOf() const; ///< Converts a Date value to a double primitive. | ||
| }; | ||
| #endif | ||
| /// A JavaScript string or symbol value (that can be used as a property name). | ||
| class Name : public Value { | ||
| public: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #define NAPI_EXPERIMENTAL | ||
| #include "napi.h" | ||
| using namespace Napi; | ||
| #if (NAPI_VERSION > 4) | ||
| namespace { | ||
| Value CreateDate(const CallbackInfo& info) { | ||
| double input = info[0].As<Number>().DoubleValue(); | ||
| return Date::New(info.Env(), input); | ||
| } | ||
| Value IsDate(const CallbackInfo& info) { | ||
| Date input = info[0].As<Date>(); | ||
| return Boolean::New(info.Env(), input.IsDate()); | ||
| } | ||
| Value ValueOf(const CallbackInfo& info) { | ||
| Date input = info[0].As<Date>(); | ||
| return Number::New(info.Env(), input.ValueOf()); | ||
| } | ||
| Value OperatorValue(const CallbackInfo& info) { | ||
| Date input = info[0].As<Date>(); | ||
| return Boolean::New(info.Env(), input.ValueOf() == static_cast<double>(input)); | ||
| } | ||
| } // anonymous namespace | ||
| Object InitDate(Env env) { | ||
| Object exports = Object::New(env); | ||
| exports["CreateDate"] = Function::New(env, CreateDate); | ||
| exports["IsDate"] = Function::New(env, IsDate); | ||
| exports["ValueOf"] = Function::New(env, ValueOf); | ||
| exports["OperatorValue"] = Function::New(env, OperatorValue); | ||
| return exports; | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 'use strict'; | ||
| const buildType = process.config.target_defaults.default_configuration; | ||
| const assert = require('assert'); | ||
| test(require(`./build/${buildType}/binding.node`)); | ||
| test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
| function test(binding) { | ||
| const { | ||
| CreateDate, | ||
| IsDate, | ||
| ValueOf, | ||
| OperatorValue, | ||
| } = binding.date; | ||
| assert.deepStrictEqual(CreateDate(0), new Date(0)); | ||
| assert.strictEqual(IsDate(new Date(0)), true); | ||
| assert.strictEqual(ValueOf(new Date(42)), 42); | ||
| assert.strictEqual(OperatorValue(new Date(42)), true); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.