Merge pull request #964 from napi-rs/fix-struct-gen

fix(napi-derive): return Class instance in function
This commit is contained in:
LongYinan 2021-12-23 14:27:07 +08:00 committed by GitHub
commit b05e0e713f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 { fn gen_to_napi_value_ctor_impl(&self) -> TokenStream {
let name = &self.name; 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_conversions = vec![];
let mut field_destructions = vec![]; let mut field_destructions = vec![];

View file

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

View file

@ -69,6 +69,7 @@ import {
appendBuffer, appendBuffer,
returnNull, returnNull,
returnUndefined, returnUndefined,
Dog,
} from '../' } from '../'
test('export const', (t) => { test('export const', (t) => {
@ -125,6 +126,7 @@ test('class', (t) => {
dog.name = '可乐' dog.name = '可乐'
t.is(dog.name, '可乐') t.is(dog.name, '可乐')
t.deepEqual(dog.returnOtherClass(), new Dog('Doge'))
}) })
test('class factory', (t) => { test('class factory', (t) => {

View file

@ -134,6 +134,11 @@ export class Animal {
whoami(): string whoami(): string
/** This is static... */ /** This is static... */
static getDogKind(): Kind static getDogKind(): Kind
returnOtherClass(): Dog
}
export class Dog {
name: string
constructor(name: string)
} }
/** Smoking test for type generation */ /** Smoking test for type generation */
export class Blake2BHasher { export class Blake2BHasher {

View file

@ -60,6 +60,18 @@ impl Animal {
pub fn get_dog_kind() -> Kind { pub fn get_dog_kind() -> Kind {
Kind::Dog 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 /// Smoking test for type generation