napi-rs/test_module/src/function.rs
LongYinan 1a3621b727
feat(napi): major upgrades for napi@1
1. inline everything
2. change `check_status` and `type_of` to macro
3. provide #[module_exports] macro
4. remove debug and repr[transparent] for ffi struct
2020-11-26 11:31:49 +08:00

28 lines
896 B
Rust

use napi::{CallContext, JsFunction, JsNull, JsObject, Result};
#[js_function(1)]
pub fn call_function(ctx: CallContext) -> Result<JsNull> {
let js_func = ctx.get::<JsFunction>(0)?;
let js_string_hello = ctx.env.create_string("hello".as_ref())?.into_unknown();
let js_string_world = ctx.env.create_string("world".as_ref())?.into_unknown();
js_func.call(None, &[js_string_hello, js_string_world])?;
ctx.env.get_null()
}
#[js_function(1)]
pub fn call_function_with_this(ctx: CallContext) -> Result<JsNull> {
let js_this: JsObject = ctx.this_unchecked();
let js_func = ctx.get::<JsFunction>(0)?;
js_func.call(Some(&js_this), &[])?;
ctx.env.get_null()
}
pub fn register_js(exports: &mut JsObject) -> Result<()> {
exports.create_named_method("testCallFunction", call_function)?;
exports.create_named_method("testCallFunctionWithThis", call_function_with_this)?;
Ok(())
}