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
|
|
|
|
2020-10-15 12:10:17 +09:00
|
|
|
struct NativeClass {
|
|
|
|
value: i32,
|
|
|
|
}
|
|
|
|
|
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-09-03 01:35:47 +09:00
|
|
|
let add_count_method = Property::new(&ctx.env, "addCount")?.with_method(add_count);
|
2020-10-15 12:10:17 +09:00
|
|
|
let add_native_count = Property::new(&ctx.env, "addNativeCount")?.with_method(add_native_count);
|
|
|
|
let properties = vec![add_count_method, add_native_count];
|
2020-09-03 01:35:47 +09:00
|
|
|
ctx
|
|
|
|
.env
|
|
|
|
.define_class("TestClass", test_class_constructor, properties.as_slice())
|
2020-08-03 00:31:22 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function(1)]
|
2020-09-26 18:49:30 +09:00
|
|
|
fn test_class_constructor(ctx: CallContext) -> Result<JsUndefined> {
|
2020-10-15 12:10:17 +09:00
|
|
|
let count: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
|
2020-11-20 10:09:57 +09:00
|
|
|
let mut this: JsObject = ctx.this_unchecked();
|
2020-10-15 12:10:17 +09:00
|
|
|
ctx
|
|
|
|
.env
|
|
|
|
.wrap(&mut this, NativeClass { value: count + 100 })?;
|
|
|
|
this.set_named_property("count", ctx.env.create_int32(count)?)?;
|
2020-08-03 00:31:22 +09:00
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function(1)]
|
2020-09-26 18:49:30 +09:00
|
|
|
fn add_count(ctx: CallContext) -> Result<JsUndefined> {
|
2020-08-03 00:31:22 +09:00
|
|
|
let add: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
|
2020-11-20 10:09:57 +09:00
|
|
|
let mut this: JsObject = ctx.this_unchecked();
|
2020-09-26 18:49:30 +09:00
|
|
|
let count: i32 = this.get_named_property::<JsNumber>("count")?.try_into()?;
|
|
|
|
this.set_named_property("count", ctx.env.create_int32(count + add)?)?;
|
2020-08-03 00:31:22 +09:00
|
|
|
ctx.env.get_undefined()
|
|
|
|
}
|
2020-09-02 18:05:53 +09:00
|
|
|
|
2020-10-15 12:10:17 +09:00
|
|
|
#[js_function(1)]
|
|
|
|
fn add_native_count(ctx: CallContext) -> Result<JsNumber> {
|
|
|
|
let add: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
|
2020-11-20 10:09:57 +09:00
|
|
|
let this: JsObject = ctx.this_unchecked();
|
2020-10-15 12:10:17 +09:00
|
|
|
let native_class: &mut NativeClass = ctx.env.unwrap(&this)?;
|
|
|
|
native_class.value = native_class.value + add;
|
|
|
|
ctx.env.create_int32(native_class.value)
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:19:40 +09:00
|
|
|
#[js_function(1)]
|
|
|
|
fn new_test_class(ctx: CallContext) -> Result<JsObject> {
|
|
|
|
let add_count_method = Property::new(&ctx.env, "addCount")?.with_method(add_count);
|
|
|
|
let add_native_count = Property::new(&ctx.env, "addNativeCount")?.with_method(add_native_count);
|
|
|
|
let properties = vec![add_count_method, add_native_count];
|
|
|
|
let test_class =
|
|
|
|
ctx
|
|
|
|
.env
|
|
|
|
.define_class("TestClass", test_class_constructor, properties.as_slice())?;
|
|
|
|
|
|
|
|
test_class.new(&vec![ctx.env.create_int32(42)?])
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:05:53 +09:00
|
|
|
pub fn register_js(module: &mut Module) -> Result<()> {
|
|
|
|
module.create_named_method("createTestClass", create_test_class)?;
|
2020-10-27 15:19:40 +09:00
|
|
|
module.create_named_method("newTestClass", new_test_class)?;
|
2020-09-02 18:05:53 +09:00
|
|
|
Ok(())
|
|
|
|
}
|