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:
parent
c25cfc5773
commit
e6a30ffcca
7 changed files with 76 additions and 2 deletions
examples/napi
|
@ -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␊
|
||||
|
|
Binary file not shown.
|
@ -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) => {
|
||||
|
|
5
examples/napi/index.d.ts
vendored
5
examples/napi/index.d.ts
vendored
|
@ -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
|
||||
|
|
|
@ -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)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue