diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index 79df9c9e..539d46d5 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -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 { + env: napi::sys::napi_env, val: #name + ) -> napi::Result { 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( diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 68cb4c58..b8d3c8dd 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -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 {␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 870adc52..ff404ed7 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index d1c9ae5c..99ce7fa2 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -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) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 5224aef8..a64af8ad 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -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 { diff --git a/examples/napi/src/class.rs b/examples/napi/src/class.rs index 4c1a7562..f1d5e720 100644 --- a/examples/napi/src/class.rs +++ b/examples/napi/src/class.rs @@ -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