2020-08-03 00:31:22 +09:00
|
|
|
use std::convert::TryInto;
|
|
|
|
|
2020-09-02 18:05:53 +09:00
|
|
|
use napi::{CallContext, JsFunction, JsNumber, JsObject, JsUndefined, Module, Property, Result};
|
2020-08-03 00:31:22 +09:00
|
|
|
|
|
|
|
#[js_function(1)]
|
2020-09-02 18:05:53 +09:00
|
|
|
fn create_test_class(ctx: CallContext) -> Result<JsFunction> {
|
2020-08-03 00:31:22 +09:00
|
|
|
let add_count_method = Property::new("addCount").with_method(add_count);
|
2020-09-02 18:05:53 +09:00
|
|
|
let mut properties = vec![add_count_method];
|
|
|
|
ctx.env.define_class(
|
|
|
|
"TestClass",
|
|
|
|
test_class_constructor,
|
|
|
|
properties.as_mut_slice(),
|
|
|
|
)
|
2020-08-03 00:31:22 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function(1)]
|
2020-09-02 18:05:53 +09:00
|
|
|
fn test_class_constructor(mut ctx: CallContext<JsObject>) -> Result<JsUndefined> {
|
2020-08-03 00:31:22 +09:00
|
|
|
let count = ctx.get::<JsNumber>(0)?;
|
|
|
|
ctx
|
|
|
|
.this
|
|
|
|
.set_named_property("count", ctx.env.create_int32(count.try_into()?)?)?;
|
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function(1)]
|
2020-09-02 18:05:53 +09:00
|
|
|
fn add_count(mut ctx: CallContext<JsObject>) -> Result<JsUndefined> {
|
2020-08-03 00:31:22 +09:00
|
|
|
let add: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
|
|
|
|
let count: i32 = ctx
|
|
|
|
.this
|
|
|
|
.get_named_property::<JsNumber>("count")?
|
|
|
|
.try_into()?;
|
|
|
|
ctx
|
|
|
|
.this
|
|
|
|
.set_named_property("count", ctx.env.create_int32(count + add)?)?;
|
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
2020-09-02 18:05:53 +09:00
|
|
|
|
|
|
|
pub fn register_js(module: &mut Module) -> Result<()> {
|
|
|
|
module.create_named_method("createTestClass", create_test_class)?;
|
|
|
|
Ok(())
|
|
|
|
}
|