napi-rs/examples/napi-compat-mode/src/napi5/date.rs
forehalo 2467b7139b
Introduce #[napi] procedural macro to automation development boilerplate (#696)
* napi procedural macro for basic rust/JavaScript types
* introduce the `compat-mode` for `napi` and `napi-derive` crates for backward compatible
* remove #[inline] and let compiler to decide the inline behavior
* cli now can produce the `.d.ts` file for native binding
* many tests and example for the new procedural macro

Co-authored-by: LongYinan <lynweklm@gmail.com>
2021-09-23 01:29:09 +08:00

21 lines
608 B
Rust

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()?)
}