diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index 3ed55f91..70c9a316 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -163,7 +163,7 @@ impl NapiStruct { fn gen_to_napi_value_ctor_impl(&self) -> TokenStream { let name = &self.name; - let js_name_str = &self.js_name; + let js_name_str = format!("{}\0", &self.js_name); let mut field_conversions = vec![]; let mut field_destructions = vec![]; diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 54bd26ac..f00894e4 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -144,6 +144,11 @@ Generated by [AVA](https://avajs.dev). whoami(): string␊ /** This is static... */␊ static getDogKind(): Kind␊ + returnOtherClass(): Dog␊ + }␊ + export class Dog {␊ + name: string␊ + constructor(name: string)␊ }␊ /** 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 2249df9d..4abe403e 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 a61b4d42..83eccc11 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -69,6 +69,7 @@ import { appendBuffer, returnNull, returnUndefined, + Dog, } from '../' test('export const', (t) => { @@ -125,6 +126,7 @@ test('class', (t) => { dog.name = '可乐' t.is(dog.name, '可乐') + t.deepEqual(dog.returnOtherClass(), new Dog('Doge')) }) test('class factory', (t) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 6282cf8f..da828cfa 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -134,6 +134,11 @@ export class Animal { whoami(): string /** This is static... */ static getDogKind(): Kind + returnOtherClass(): Dog +} +export class Dog { + name: string + constructor(name: string) } /** Smoking test for type generation */ export class Blake2BHasher { diff --git a/examples/napi/src/class.rs b/examples/napi/src/class.rs index d4b864c2..bb8e5dd0 100644 --- a/examples/napi/src/class.rs +++ b/examples/napi/src/class.rs @@ -60,6 +60,18 @@ impl Animal { pub fn get_dog_kind() -> Kind { Kind::Dog } + + #[napi] + pub fn return_other_class(&self) -> Dog { + Dog { + name: "Doge".to_owned(), + } + } +} + +#[napi(constructor)] +pub struct Dog { + pub name: String, } /// Smoking test for type generation