fix(napi-derive): return Result type in Constructor and Factory
This commit is contained in:
parent
44040e3bfe
commit
e9ab2192da
7 changed files with 62 additions and 5 deletions
|
@ -244,9 +244,17 @@ impl NapiFn {
|
||||||
|
|
||||||
if let Some(ty) = &self.ret {
|
if let Some(ty) = &self.ret {
|
||||||
if self.kind == FnKind::Constructor {
|
if self.kind == FnKind::Constructor {
|
||||||
|
if self.is_ret_result {
|
||||||
|
quote! { cb.construct(#js_name, #ret?) }
|
||||||
|
} else {
|
||||||
quote! { cb.construct(#js_name, #ret) }
|
quote! { cb.construct(#js_name, #ret) }
|
||||||
|
}
|
||||||
} else if self.kind == FnKind::Factory {
|
} else if self.kind == FnKind::Factory {
|
||||||
|
if self.is_ret_result {
|
||||||
|
quote! { cb.factory(#js_name, #ret?) }
|
||||||
|
} else {
|
||||||
quote! { cb.factory(#js_name, #ret) }
|
quote! { cb.factory(#js_name, #ret) }
|
||||||
|
}
|
||||||
} else if self.is_ret_result {
|
} else if self.is_ret_result {
|
||||||
if self.is_async {
|
if self.is_async {
|
||||||
quote! {
|
quote! {
|
||||||
|
|
|
@ -234,9 +234,8 @@ fn extract_callback_trait_types(
|
||||||
|
|
||||||
fn extract_result_ty(ty: &syn::Type) -> BindgenResult<Option<syn::Type>> {
|
fn extract_result_ty(ty: &syn::Type) -> BindgenResult<Option<syn::Type>> {
|
||||||
match ty {
|
match ty {
|
||||||
syn::Type::Path(syn::TypePath { qself: None, path }) if path.segments.len() == 1 => {
|
syn::Type::Path(syn::TypePath { qself: None, path }) => {
|
||||||
let segment = path.segments.first().unwrap();
|
let segment = path.segments.last().unwrap();
|
||||||
|
|
||||||
if segment.ident != "Result" {
|
if segment.ident != "Result" {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -61,6 +61,12 @@ Generated by [AVA](https://avajs.dev).
|
||||||
export class Blake2BKey {␊
|
export class Blake2BKey {␊
|
||||||
␊
|
␊
|
||||||
}␊
|
}␊
|
||||||
|
export class Context {␊
|
||||||
|
␊
|
||||||
|
constructor()␊
|
||||||
|
static withData(data: string): Context␊
|
||||||
|
method(): string␊
|
||||||
|
}␊
|
||||||
export class ClassWithFactory {␊
|
export class ClassWithFactory {␊
|
||||||
name: string␊
|
name: string␊
|
||||||
static withName(name: string): ClassWithFactory␊
|
static withName(name: string): ClassWithFactory␊
|
||||||
|
|
Binary file not shown.
|
@ -17,6 +17,7 @@ import {
|
||||||
Kind,
|
Kind,
|
||||||
ClassWithFactory,
|
ClassWithFactory,
|
||||||
CustomNumEnum,
|
CustomNumEnum,
|
||||||
|
Context,
|
||||||
enumToI32,
|
enumToI32,
|
||||||
listObjKeys,
|
listObjKeys,
|
||||||
createObj,
|
createObj,
|
||||||
|
@ -96,6 +97,16 @@ test('class factory', (t) => {
|
||||||
t.is(doge.name, '旺财')
|
t.is(doge.name, '旺财')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('class constructor return Result', (t) => {
|
||||||
|
const c = new Context()
|
||||||
|
t.is(c.method(), 'not empty')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('class Factory return Result', (t) => {
|
||||||
|
const c = Context.withData('not empty')
|
||||||
|
t.is(c.method(), 'not empty')
|
||||||
|
})
|
||||||
|
|
||||||
test('callback', (t) => {
|
test('callback', (t) => {
|
||||||
getCwd((cwd) => {
|
getCwd((cwd) => {
|
||||||
t.is(cwd, process.cwd())
|
t.is(cwd, process.cwd())
|
||||||
|
|
6
examples/napi/index.d.ts
vendored
6
examples/napi/index.d.ts
vendored
|
@ -50,6 +50,12 @@ export class Blake2BHasher {
|
||||||
}
|
}
|
||||||
export class Blake2BKey {
|
export class Blake2BKey {
|
||||||
|
|
||||||
|
}
|
||||||
|
export class Context {
|
||||||
|
|
||||||
|
constructor()
|
||||||
|
static withData(data: string): Context
|
||||||
|
method(): string
|
||||||
}
|
}
|
||||||
export class ClassWithFactory {
|
export class ClassWithFactory {
|
||||||
name: string
|
name: string
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use napi::bindgen_prelude::*;
|
use napi::bindgen_prelude::*;
|
||||||
|
use napi::Result;
|
||||||
|
|
||||||
use crate::r#enum::Kind;
|
use crate::r#enum::Kind;
|
||||||
|
|
||||||
|
@ -75,3 +76,29 @@ impl Blake2bKey {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub struct Context {
|
||||||
|
data: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test for return `napi::Result` and `Result`
|
||||||
|
#[napi]
|
||||||
|
impl Context {
|
||||||
|
#[napi(constructor)]
|
||||||
|
pub fn new() -> napi::Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
data: "not empty".into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi(factory)]
|
||||||
|
pub fn with_data(data: String) -> Result<Self> {
|
||||||
|
Ok(Self { data })
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub fn method(&self) -> String {
|
||||||
|
self.data.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue