Merge pull request #967 from napi-rs/fix/missing-wrap

fix(napi-derive): missing wrap in NAPI-RS created class instance
This commit is contained in:
LongYinan 2021-12-24 23:53:52 +08:00 committed by GitHub
commit ab1af002e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 6 deletions

View file

@ -170,25 +170,37 @@ impl NapiStruct {
quote! {
impl napi::bindgen_prelude::ToNapiValue for #name {
unsafe fn to_napi_value(
env: napi::bindgen_prelude::sys::napi_env, val: #name
) -> napi::bindgen_prelude::Result<napi::bindgen_prelude::sys::napi_value> {
env: napi::sys::napi_env, val: #name
) -> napi::Result<napi::bindgen_prelude::sys::napi_value> {
if let Some(ctor_ref) = napi::bindgen_prelude::get_class_constructor(#js_name_str) {
let mut ctor = std::ptr::null_mut();
napi::bindgen_prelude::check_status!(
napi::bindgen_prelude::sys::napi_get_reference_value(env, ctor_ref, &mut ctor),
napi::check_status!(
napi::sys::napi_get_reference_value(env, ctor_ref, &mut ctor),
"Failed to get constructor of class `{}`",
#js_name_str
)?;
let mut result = std::ptr::null_mut();
napi::bindgen_prelude::___CALL_FROM_FACTORY.store(true, std::sync::atomic::Ordering::Relaxed);
napi::bindgen_prelude::check_status!(
napi::bindgen_prelude::sys::napi_new_instance(env, ctor, 0, std::ptr::null_mut(), &mut result),
napi::check_status!(
napi::sys::napi_new_instance(env, ctor, 0, std::ptr::null_mut(), &mut result),
"Failed to construct class `{}`",
#js_name_str
)?;
napi::bindgen_prelude::___CALL_FROM_FACTORY.store(false, std::sync::atomic::Ordering::Relaxed);
napi::check_status!(
napi::sys::napi_wrap(
env,
result,
Box::into_raw(Box::new(val)) as *mut std::ffi::c_void,
Some(napi::bindgen_prelude::raw_finalize_unchecked::<#name>),
std::ptr::null_mut(),
std::ptr::null_mut(),
),
"Failed to wrap native object of class `{}`",
#js_name_str
)?;
Ok(result)
} else {
Err(napi::bindgen_prelude::Error::new(

View file

@ -154,6 +154,7 @@ Generated by [AVA](https://avajs.dev).
export class Bird {␊
name: string␊
constructor(name: string)␊
getCount(): number␊
}␊
/** Smoking test for type generation */␊
export class Blake2BHasher {␊

View file

@ -129,6 +129,7 @@ test('class', (t) => {
t.is(dog.name, '可乐')
t.deepEqual(dog.returnOtherClass(), new Dog('Doge'))
t.deepEqual(dog.returnOtherClassWithCustomConstructor(), new Bird('parrot'))
t.is(dog.returnOtherClassWithCustomConstructor().getCount(), 1234)
})
test('class factory', (t) => {

View file

@ -144,6 +144,7 @@ export class Dog {
export class Bird {
name: string
constructor(name: string)
getCount(): number
}
/** Smoking test for type generation */
export class Blake2BHasher {

View file

@ -90,6 +90,11 @@ impl Bird {
pub fn new(name: String) -> Self {
Bird { name }
}
#[napi]
pub fn get_count(&self) -> u32 {
1234
}
}
/// Smoking test for type generation