From 940be7df9938f77738b269d4d53eb2b9c26594f0 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 23 Dec 2021 14:07:35 +0800 Subject: [PATCH] fix(napi-derive): return Class instance in function --- crates/backend/src/codegen/struct.rs | 2 +- examples/napi/__test__/typegen.spec.ts.md | 5 +++++ examples/napi/__test__/typegen.spec.ts.snap | Bin 2094 -> 2116 bytes examples/napi/__test__/values.spec.ts | 2 ++ examples/napi/index.d.ts | 5 +++++ examples/napi/src/class.rs | 12 ++++++++++++ 6 files changed, 25 insertions(+), 1 deletion(-) 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 2249df9d222003ec2628f9930a4135d2dbdd3b96..4abe403e7c8e0afa31bc1fdd3897cbd9107374e1 100644 GIT binary patch literal 2116 zcmV-K2)p+|RzVJh+ao8NFT!HYWaM~~Av+UilsN3HqG+eOyKGn7UDexFox}{0 z_z9dia6#e%H~s+Pf{?g#<}V;loO!RR+pczZ$I)t|D5;P4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U<>DCswVp~vg7wm0ru@>m@hsO{)=8Y%wws>O6LvwmGJ0RMA%5G zqtt-%xdC{n66`EtV%;)|OWim$zc2d72mOKb3^lXv*4I1RO@prWFc58Qel}+^F0GLS zME47NgLYcKxiaj|o8l5x5L@mn3Gtx~C&|Am`b-VL^N^1qs6E$&y9VEFy1sKSw29Nf Yk`cDvr*D@2hH$s?6TF*EB7_(K0P6Ah0ssI2 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