napi-rs/examples/napi-compat-mode/src/global.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

25 lines
720 B
Rust

use std::convert::TryInto;
use napi::{CallContext, JsFunction, JsNumber, JsObject, JsTimeout, JsUndefined, Result};
#[js_function(2)]
pub fn set_timeout(ctx: CallContext) -> Result<JsTimeout> {
let handler: JsFunction = ctx.get(0)?;
let timeout: JsNumber = ctx.get(1)?;
ctx
.env
.get_global()?
.set_timeout(handler, timeout.try_into()?)
}
#[js_function(1)]
pub fn clear_timeout(ctx: CallContext) -> Result<JsUndefined> {
let timer: JsTimeout = ctx.get(0)?;
ctx.env.get_global()?.clear_timeout(timer)
}
pub fn register_js(exports: &mut JsObject) -> Result<()> {
exports.create_named_method("setTimeout", set_timeout)?;
exports.create_named_method("clearTimeout", clear_timeout)?;
Ok(())
}