Merge pull request #70 from oyyd/impl-is-date

feat(napi): try to impl is_date
This commit is contained in:
LongYinan 2020-06-15 21:36:26 +08:00 committed by GitHub
commit a67d44c892
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View file

@ -1042,6 +1042,15 @@ impl Value<Object> {
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 {
self.raw_value
}

View file

@ -1,3 +1,4 @@
const assert = require('assert')
const testModule = require('./index.node')
function testSpawnThread(n) {
@ -14,3 +15,6 @@ testSpawnThread(20)
console.error(e)
process.exit(1)
})
assert(testModule.testObjectIsDate({}) === false)
assert(testModule.testObjectIsDate(new Date()) === true)

View file

@ -3,7 +3,7 @@ extern crate napi_rs as napi;
#[macro_use]
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;
register_module!(test_module, init);
@ -14,6 +14,10 @@ fn init(env: &Env, exports: &mut Value<Object>) -> Result<()> {
"testSpawnThread",
env.create_function("testSpawnThread", test_spawn_thread)?,
)?;
exports.set_named_property(
"testObjectIsDate",
env.create_function("testObjectIsDate", test_object_is_date)?
)?;
Ok(())
}
@ -59,3 +63,9 @@ fn test_spawn_thread(ctx: CallContext) -> Result<Value<Object>> {
fn test_throw(_ctx: CallContext) -> Result<Value<Any>> {
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()?)?)
}