Merge pull request #223 from napi-rs/date

feat(napi): implement date related API
This commit is contained in:
LongYinan 2020-10-10 15:21:54 +08:00 committed by GitHub
commit 27e11d6723
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 70 additions and 8 deletions

View file

@ -87,8 +87,7 @@ jobs:
- name: Unit tests - name: Unit tests
run: | run: |
yarn yarn build:test
yarn --cwd ./test_module build
yarn test yarn test
env: env:
RUST_BACKTRACE: 1 RUST_BACKTRACE: 1

View file

@ -158,7 +158,7 @@ yarn test
| [napi_create_arraybuffer](https://nodejs.org/api/n-api.html#n_api_napi_create_arraybuffer) | 1 | v8.0.0 | ✅ | | [napi_create_arraybuffer](https://nodejs.org/api/n-api.html#n_api_napi_create_arraybuffer) | 1 | v8.0.0 | ✅ |
| [napi_create_buffer](https://nodejs.org/api/n-api.html#n_api_napi_create_buffer) | 1 | v8.0.0 | ✅ | | [napi_create_buffer](https://nodejs.org/api/n-api.html#n_api_napi_create_buffer) | 1 | v8.0.0 | ✅ |
| [napi_create_buffer_copy](https://nodejs.org/api/n-api.html#n_api_napi_create_buffer_copy) | 1 | v8.0.0 | ⛔️ | | [napi_create_buffer_copy](https://nodejs.org/api/n-api.html#n_api_napi_create_buffer_copy) | 1 | v8.0.0 | ⛔️ |
| [napi_create_date](https://nodejs.org/api/n-api.html#n_api_napi_create_date) | 5 | v11.11.0 | ⛔️ | | [napi_create_date](https://nodejs.org/api/n-api.html#n_api_napi_create_date) | 5 | v11.11.0 | |
| [napi_create_external](https://nodejs.org/api/n-api.html#n_api_napi_create_external) | 1 | v8.0.0 | ✅ | | [napi_create_external](https://nodejs.org/api/n-api.html#n_api_napi_create_external) | 1 | v8.0.0 | ✅ |
| [napi_create_external_arraybuffer](https://nodejs.org/api/n-api.html#n_api_napi_create_external_arraybuffer) | 1 | v8.0.0 | ✅ | | [napi_create_external_arraybuffer](https://nodejs.org/api/n-api.html#n_api_napi_create_external_arraybuffer) | 1 | v8.0.0 | ✅ |
| [napi_create_external_buffer](https://nodejs.org/api/n-api.html#n_api_napi_create_external_buffer) | 1 | v8.0.0 | ✅ | | [napi_create_external_buffer](https://nodejs.org/api/n-api.html#n_api_napi_create_external_buffer) | 1 | v8.0.0 | ✅ |
@ -187,7 +187,7 @@ yarn test
| [napi_get_prototype](https://nodejs.org/api/n-api.html#n_api_napi_get_prototype) | 1 | v8.0.0 | ✅ | | [napi_get_prototype](https://nodejs.org/api/n-api.html#n_api_napi_get_prototype) | 1 | v8.0.0 | ✅ |
| [napi_get_typedarray_info](https://nodejs.org/api/n-api.html#n_api_napi_get_typedarray_info) | 1 | v8.0.0 | ⛔️ | | [napi_get_typedarray_info](https://nodejs.org/api/n-api.html#n_api_napi_get_typedarray_info) | 1 | v8.0.0 | ⛔️ |
| [napi_get_dataview_info](https://nodejs.org/api/n-api.html#n_api_napi_get_dataview_info) | 1 | v8.3.0 | ⛔️ | | [napi_get_dataview_info](https://nodejs.org/api/n-api.html#n_api_napi_get_dataview_info) | 1 | v8.3.0 | ⛔️ |
| [napi_get_date_value](https://nodejs.org/api/n-api.html#n_api_napi_get_date_value) | 5 | v11.11.0 | ⛔️ | | [napi_get_date_value](https://nodejs.org/api/n-api.html#n_api_napi_get_date_value) | 5 | v11.11.0 | |
| [napi_get_value_bool](https://nodejs.org/api/n-api.html#n_api_napi_get_value_bool) | 1 | v8.0.0 | ✅ | | [napi_get_value_bool](https://nodejs.org/api/n-api.html#n_api_napi_get_value_bool) | 1 | v8.0.0 | ✅ |
| [napi_get_value_double](https://nodejs.org/api/n-api.html#n_api_napi_get_value_double) | 1 | v8.0.0 | ✅ | | [napi_get_value_double](https://nodejs.org/api/n-api.html#n_api_napi_get_value_double) | 1 | v8.0.0 | ✅ |
| [napi_get_value_bigint_int64](https://nodejs.org/api/n-api.html#n_api_napi_get_value_bigint_int64) | 6 | v10.7.0 | ✅ | | [napi_get_value_bigint_int64](https://nodejs.org/api/n-api.html#n_api_napi_get_value_bigint_int64) | 6 | v10.7.0 | ✅ |

View file

@ -629,13 +629,20 @@ impl Env {
Ok(JsObject::from_raw_unchecked(self.0, raw_promise)) Ok(JsObject::from_raw_unchecked(self.0, raw_promise))
} }
#[cfg(napi5)]
pub fn create_date(&self, time: f64) -> Result<JsDate> {
let mut js_value = ptr::null_mut();
check_status(unsafe { sys::napi_create_date(self.0, time, &mut js_value) })?;
Ok(JsDate::from_raw_unchecked(self.0, js_value))
}
/// # Serialize `Rust Struct` into `JavaScript Value` /// # Serialize `Rust Struct` into `JavaScript Value`
/// ``` /// ```
/// #[derive(Serialize, Debug, Deserialize)] /// #[derive(Serialize, Debug, Deserialize)]
/// struct AnObject { /// struct AnObject {
/// a: u32, /// a: u32,
/// b: Vec<f64>, /// b: Vec<f64>,
/// c: String, /// c: String,
/// } /// }
/// ///
/// #[js_function] /// #[js_function]

View file

@ -0,0 +1,14 @@
use super::check_status;
use crate::{sys, Result, Value};
#[repr(transparent)]
#[derive(Debug)]
pub struct JsDate(pub(crate) Value);
impl JsDate {
pub fn value_of(&self) -> Result<f64> {
let mut timestamp: f64 = 0.0;
check_status(unsafe { sys::napi_get_date_value(self.0.env, self.0.value, &mut timestamp) })?;
Ok(timestamp)
}
}

View file

@ -15,6 +15,8 @@ mod arraybuffer;
mod bigint; mod bigint;
mod boolean; mod boolean;
mod buffer; mod buffer;
#[cfg(napi5)]
mod date;
mod either; mod either;
mod escapable_handle_scope; mod escapable_handle_scope;
mod function; mod function;
@ -34,6 +36,8 @@ pub use arraybuffer::*;
pub use bigint::JsBigint; pub use bigint::JsBigint;
pub use boolean::JsBoolean; pub use boolean::JsBoolean;
pub use buffer::*; pub use buffer::*;
#[cfg(napi5)]
pub use date::*;
#[cfg(feature = "serde-json")] #[cfg(feature = "serde-json")]
pub(crate) use de::De; pub(crate) use de::De;
pub use either::Either; pub use either::Either;
@ -440,6 +444,8 @@ impl_js_value_methods!(JsNumber);
impl_js_value_methods!(JsString); impl_js_value_methods!(JsString);
impl_js_value_methods!(JsObject); impl_js_value_methods!(JsObject);
impl_js_value_methods!(JsGlobal); impl_js_value_methods!(JsGlobal);
#[cfg(napi5)]
impl_js_value_methods!(JsDate);
impl_js_value_methods!(JsFunction); impl_js_value_methods!(JsFunction);
impl_js_value_methods!(JsExternal); impl_js_value_methods!(JsExternal);
impl_js_value_methods!(JsSymbol); impl_js_value_methods!(JsSymbol);
@ -461,6 +467,8 @@ impl_napi_value_trait!(JsNumber, Number);
impl_napi_value_trait!(JsString, String); impl_napi_value_trait!(JsString, String);
impl_napi_value_trait!(JsObject, Object); impl_napi_value_trait!(JsObject, Object);
impl_napi_value_trait!(JsGlobal, Object); impl_napi_value_trait!(JsGlobal, Object);
#[cfg(napi5)]
impl_napi_value_trait!(JsDate, Object);
impl_napi_value_trait!(JsTimeout, Object); impl_napi_value_trait!(JsTimeout, Object);
impl_napi_value_trait!(JsFunction, Function); impl_napi_value_trait!(JsFunction, Function);
impl_napi_value_trait!(JsExternal, External); impl_napi_value_trait!(JsExternal, External);

View file

@ -22,3 +22,21 @@ test('should return true if value is date', (t) => {
t.is(bindings.testObjectIsDate, undefined) t.is(bindings.testObjectIsDate, undefined)
} }
}) })
test('should create date', (t) => {
if (napiVersion >= 5) {
const timestamp = new Date().valueOf()
t.deepEqual(bindings.testCreateDate(timestamp), new Date(timestamp))
} else {
t.is(bindings.testObjectIsDate, undefined)
}
})
test('should get date value', (t) => {
if (napiVersion >= 5) {
const date = new Date()
t.is(bindings.testGetDateValue(date), date.valueOf())
} else {
t.is(bindings.testObjectIsDate, undefined)
}
})

View file

@ -1,7 +1,21 @@
use napi::{CallContext, JsBoolean, JsUnknown, Result}; use std::convert::TryInto;
use napi::{CallContext, JsBoolean, JsDate, JsNumber, JsUnknown, Result};
#[js_function(1)] #[js_function(1)]
pub fn test_object_is_date(ctx: CallContext) -> Result<JsBoolean> { pub fn test_object_is_date(ctx: CallContext) -> Result<JsBoolean> {
let obj = ctx.get::<JsUnknown>(0)?; let obj = ctx.get::<JsUnknown>(0)?;
ctx.env.get_boolean(obj.is_date()?) ctx.env.get_boolean(obj.is_date()?)
} }
#[js_function(1)]
pub fn test_create_date(ctx: CallContext) -> Result<JsDate> {
let timestamp: f64 = ctx.get::<JsNumber>(0)?.try_into()?;
ctx.env.create_date(timestamp)
}
#[js_function(1)]
pub fn test_get_date_value(ctx: CallContext) -> Result<JsNumber> {
let date = ctx.get::<JsDate>(0)?;
ctx.env.create_double(date.value_of()?)
}

View file

@ -4,5 +4,7 @@ mod date;
pub fn register_js(module: &mut Module) -> Result<()> { pub fn register_js(module: &mut Module) -> Result<()> {
module.create_named_method("testObjectIsDate", date::test_object_is_date)?; module.create_named_method("testObjectIsDate", date::test_object_is_date)?;
module.create_named_method("testCreateDate", date::test_create_date)?;
module.create_named_method("testGetDateValue", date::test_get_date_value)?;
Ok(()) Ok(())
} }