From 4baff91a580c84332bd144770eb256986dc5910a Mon Sep 17 00:00:00 2001 From: Ouyang Yadong Date: Mon, 15 Jun 2020 21:15:37 +0800 Subject: [PATCH] feat(napi): impl is_date --- napi/src/lib.rs | 9 +++++++++ test_module/spawn.js | 4 ++++ test_module/src/lib.rs | 12 +++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/napi/src/lib.rs b/napi/src/lib.rs index 05fa35a1..3f906e47 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -1042,6 +1042,15 @@ impl Value { Ok(length) } + pub fn is_date(&self) -> Result { + 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 } diff --git a/test_module/spawn.js b/test_module/spawn.js index 56a6417f..09e01573 100644 --- a/test_module/spawn.js +++ b/test_module/spawn.js @@ -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) diff --git a/test_module/src/lib.rs b/test_module/src/lib.rs index d0d86b1c..1d7debd1 100644 --- a/test_module/src/lib.rs +++ b/test_module/src/lib.rs @@ -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) -> 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> { fn test_throw(_ctx: CallContext) -> Result> { Err(Error::from_status(Status::GenericFailure)) } + +#[js_function(1)] +fn test_object_is_date(ctx: CallContext) -> Result> { + let obj: Value = ctx.get::(0)?; + Ok(Env::get_boolean(ctx.env, obj.is_date()?)?) +}