fix(napi-derive): return Class instance in function

This commit is contained in:
LongYinan 2021-12-23 14:07:35 +08:00
parent 17a3000077
commit 940be7df99
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
6 changed files with 25 additions and 1 deletions

View file

@ -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![];

View file

@ -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 {␊

View file

@ -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) => {

View file

@ -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 {

View file

@ -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