test: add env#wrap tests

This commit is contained in:
LongYinan 2020-10-15 11:10:17 +08:00
parent fbd82d4e27
commit 1bd29e2761
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881
2 changed files with 28 additions and 3 deletions

View file

@ -11,3 +11,11 @@ test('should create class', (t) => {
testClass.addCount(add) testClass.addCount(add)
t.is(testClass.count, fixture + add) t.is(testClass.count, fixture + add)
}) })
test('should be able to manipulate wrapped native value', (t) => {
const TestClass = bindings.createTestClass()
const fixture = 20
const testClass = new TestClass(fixture)
const add = 101
t.is(testClass.addNativeCount(add), fixture + add + 100)
})

View file

@ -2,10 +2,15 @@ use std::convert::TryInto;
use napi::{CallContext, JsFunction, JsNumber, JsObject, JsUndefined, Module, Property, Result}; use napi::{CallContext, JsFunction, JsNumber, JsObject, JsUndefined, Module, Property, Result};
struct NativeClass {
value: i32,
}
#[js_function(1)] #[js_function(1)]
fn create_test_class(ctx: CallContext) -> Result<JsFunction> { fn create_test_class(ctx: CallContext) -> Result<JsFunction> {
let add_count_method = Property::new(&ctx.env, "addCount")?.with_method(add_count); let add_count_method = Property::new(&ctx.env, "addCount")?.with_method(add_count);
let properties = vec![add_count_method]; let add_native_count = Property::new(&ctx.env, "addNativeCount")?.with_method(add_native_count);
let properties = vec![add_count_method, add_native_count];
ctx ctx
.env .env
.define_class("TestClass", test_class_constructor, properties.as_slice()) .define_class("TestClass", test_class_constructor, properties.as_slice())
@ -13,9 +18,12 @@ fn create_test_class(ctx: CallContext) -> Result<JsFunction> {
#[js_function(1)] #[js_function(1)]
fn test_class_constructor(ctx: CallContext) -> Result<JsUndefined> { fn test_class_constructor(ctx: CallContext) -> Result<JsUndefined> {
let count = ctx.get::<JsNumber>(0)?; let count: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
let mut this: JsObject = ctx.this_unchecked(); let mut this: JsObject = ctx.this_unchecked();
this.set_named_property("count", ctx.env.create_int32(count.try_into()?)?)?; ctx
.env
.wrap(&mut this, NativeClass { value: count + 100 })?;
this.set_named_property("count", ctx.env.create_int32(count)?)?;
ctx.env.get_undefined() ctx.env.get_undefined()
} }
@ -28,6 +36,15 @@ fn add_count(ctx: CallContext) -> Result<JsUndefined> {
ctx.env.get_undefined() ctx.env.get_undefined()
} }
#[js_function(1)]
fn add_native_count(ctx: CallContext) -> Result<JsNumber> {
let add: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
let this: JsObject = ctx.this_unchecked();
let native_class: &mut NativeClass = ctx.env.unwrap(&this)?;
native_class.value = native_class.value + add;
ctx.env.create_int32(native_class.value)
}
pub fn register_js(module: &mut Module) -> Result<()> { pub fn register_js(module: &mut Module) -> Result<()> {
module.create_named_method("createTestClass", create_test_class)?; module.create_named_method("createTestClass", create_test_class)?;
Ok(()) Ok(())