feat(napi): impl is_date
This commit is contained in:
parent
8783ce7a5f
commit
4baff91a58
3 changed files with 24 additions and 1 deletions
|
@ -1042,6 +1042,15 @@ impl Value<Object> {
|
||||||
Ok(length)
|
Ok(length)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_date(&self) -> Result<bool> {
|
||||||
|
let mut is_date = true;
|
||||||
|
let status = unsafe {
|
||||||
|
sys::napi_is_date(self.env, self.raw_value(), &mut is_date)
|
||||||
|
};
|
||||||
|
check_status(status)?;
|
||||||
|
Ok(is_date)
|
||||||
|
}
|
||||||
|
|
||||||
fn raw_value(&self) -> sys::napi_value {
|
fn raw_value(&self) -> sys::napi_value {
|
||||||
self.raw_value
|
self.raw_value
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
const assert = require('assert')
|
||||||
const testModule = require('./index.node')
|
const testModule = require('./index.node')
|
||||||
|
|
||||||
function testSpawnThread(n) {
|
function testSpawnThread(n) {
|
||||||
|
@ -14,3 +15,6 @@ testSpawnThread(20)
|
||||||
console.error(e)
|
console.error(e)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
assert(testModule.testObjectIsDate({}) === false)
|
||||||
|
assert(testModule.testObjectIsDate(new Date()) === true)
|
||||||
|
|
|
@ -3,7 +3,7 @@ extern crate napi_rs as napi;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate napi_rs_derive;
|
extern crate napi_rs_derive;
|
||||||
|
|
||||||
use napi::{Any, CallContext, Env, Error, Number, Object, Result, Status, Task, Value};
|
use napi::{Any, CallContext, Env, Error, Number, Object, Result, Status, Task, Value, Boolean};
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
register_module!(test_module, init);
|
register_module!(test_module, init);
|
||||||
|
@ -14,6 +14,10 @@ fn init(env: &Env, exports: &mut Value<Object>) -> Result<()> {
|
||||||
"testSpawnThread",
|
"testSpawnThread",
|
||||||
env.create_function("testSpawnThread", test_spawn_thread)?,
|
env.create_function("testSpawnThread", test_spawn_thread)?,
|
||||||
)?;
|
)?;
|
||||||
|
exports.set_named_property(
|
||||||
|
"testObjectIsDate",
|
||||||
|
env.create_function("testObjectIsDate", test_object_is_date)?
|
||||||
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,3 +63,9 @@ fn test_spawn_thread(ctx: CallContext) -> Result<Value<Object>> {
|
||||||
fn test_throw(_ctx: CallContext) -> Result<Value<Any>> {
|
fn test_throw(_ctx: CallContext) -> Result<Value<Any>> {
|
||||||
Err(Error::from_status(Status::GenericFailure))
|
Err(Error::from_status(Status::GenericFailure))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[js_function(1)]
|
||||||
|
fn test_object_is_date(ctx: CallContext) -> Result<Value<Boolean>> {
|
||||||
|
let obj: Value<Object> = ctx.get::<Object>(0)?;
|
||||||
|
Ok(Env::get_boolean(ctx.env, obj.is_date()?)?)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue