fix(napi-derive): return instance from non-default constructor class

Fix https://github.com/napi-rs/napi-rs/issues/933
This commit is contained in:
LongYinan 2021-12-24 18:46:10 +08:00
parent c25cfc5773
commit e6a30ffcca
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
7 changed files with 76 additions and 2 deletions

View file

@ -145,11 +145,16 @@ Generated by [AVA](https://avajs.dev).
/** This is static... */␊
static getDogKind(): Kind␊
returnOtherClass(): Dog␊
returnOtherClassWithCustomConstructor(): Bird␊
}␊
export class Dog {␊
name: string␊
constructor(name: string)␊
}␊
export class Bird {␊
name: string␊
constructor(name: string)␊
}␊
/** Smoking test for type generation */␊
export class Blake2BHasher {␊
static withKey(key: Blake2bKey): Blake2BHasher␊

View file

@ -70,6 +70,7 @@ import {
returnNull,
returnUndefined,
Dog,
Bird,
} from '../'
test('export const', (t) => {
@ -127,6 +128,7 @@ test('class', (t) => {
dog.name = '可乐'
t.is(dog.name, '可乐')
t.deepEqual(dog.returnOtherClass(), new Dog('Doge'))
t.deepEqual(dog.returnOtherClassWithCustomConstructor(), new Bird('parrot'))
})
test('class factory', (t) => {

View file

@ -135,11 +135,16 @@ export class Animal {
/** This is static... */
static getDogKind(): Kind
returnOtherClass(): Dog
returnOtherClassWithCustomConstructor(): Bird
}
export class Dog {
name: string
constructor(name: string)
}
export class Bird {
name: string
constructor(name: string)
}
/** Smoking test for type generation */
export class Blake2BHasher {
static withKey(key: Blake2bKey): Blake2BHasher

View file

@ -67,6 +67,11 @@ impl Animal {
name: "Doge".to_owned(),
}
}
#[napi]
pub fn return_other_class_with_custom_constructor(&self) -> Bird {
Bird::new("parrot".to_owned())
}
}
#[napi(constructor)]
@ -74,6 +79,19 @@ pub struct Dog {
pub name: String,
}
#[napi]
pub struct Bird {
pub name: String,
}
#[napi]
impl Bird {
#[napi(constructor)]
pub fn new(name: String) -> Self {
Bird { name }
}
}
/// Smoking test for type generation
#[napi]
#[repr(transparent)]