feat(napi): implement date related API
This commit is contained in:
parent
1b69902381
commit
6c95d86d2f
8 changed files with 70 additions and 8 deletions
3
.github/workflows/test.yaml
vendored
3
.github/workflows/test.yaml
vendored
|
@ -87,8 +87,7 @@ jobs:
|
|||
|
||||
- name: Unit tests
|
||||
run: |
|
||||
yarn
|
||||
yarn --cwd ./test_module build
|
||||
yarn build:test
|
||||
yarn test
|
||||
env:
|
||||
RUST_BACKTRACE: 1
|
||||
|
|
|
@ -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_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_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_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 | ✅ |
|
||||
|
@ -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_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_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_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 | ✅ |
|
||||
|
|
|
@ -629,13 +629,20 @@ impl Env {
|
|||
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`
|
||||
/// ```
|
||||
/// #[derive(Serialize, Debug, Deserialize)]
|
||||
/// struct AnObject {
|
||||
/// a: u32,
|
||||
/// b: Vec<f64>,
|
||||
/// c: String,
|
||||
/// a: u32,
|
||||
/// b: Vec<f64>,
|
||||
/// c: String,
|
||||
/// }
|
||||
///
|
||||
/// #[js_function]
|
||||
|
|
14
napi/src/js_values/date.rs
Normal file
14
napi/src/js_values/date.rs
Normal 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)
|
||||
}
|
||||
}
|
|
@ -15,6 +15,8 @@ mod arraybuffer;
|
|||
mod bigint;
|
||||
mod boolean;
|
||||
mod buffer;
|
||||
#[cfg(napi5)]
|
||||
mod date;
|
||||
mod either;
|
||||
mod escapable_handle_scope;
|
||||
mod function;
|
||||
|
@ -34,6 +36,8 @@ pub use arraybuffer::*;
|
|||
pub use bigint::JsBigint;
|
||||
pub use boolean::JsBoolean;
|
||||
pub use buffer::*;
|
||||
#[cfg(napi5)]
|
||||
pub use date::*;
|
||||
#[cfg(feature = "serde-json")]
|
||||
pub(crate) use de::De;
|
||||
pub use either::Either;
|
||||
|
@ -440,6 +444,8 @@ impl_js_value_methods!(JsNumber);
|
|||
impl_js_value_methods!(JsString);
|
||||
impl_js_value_methods!(JsObject);
|
||||
impl_js_value_methods!(JsGlobal);
|
||||
#[cfg(napi5)]
|
||||
impl_js_value_methods!(JsDate);
|
||||
impl_js_value_methods!(JsFunction);
|
||||
impl_js_value_methods!(JsExternal);
|
||||
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!(JsObject, 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!(JsFunction, Function);
|
||||
impl_napi_value_trait!(JsExternal, External);
|
||||
|
|
|
@ -22,3 +22,21 @@ test('should return true if value is date', (t) => {
|
|||
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)
|
||||
}
|
||||
})
|
|
@ -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)]
|
||||
pub fn test_object_is_date(ctx: CallContext) -> Result<JsBoolean> {
|
||||
let obj = ctx.get::<JsUnknown>(0)?;
|
||||
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()?)
|
||||
}
|
||||
|
|
|
@ -4,5 +4,7 @@ mod date;
|
|||
|
||||
pub fn register_js(module: &mut Module) -> Result<()> {
|
||||
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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue