Skip to content

Commit

Permalink
fix(napi-derive): async task optional output type
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Nov 8, 2023
1 parent 938f4df commit adcb86a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/backend/src/typegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ fn is_ts_function_type_notation(ty: &Type) -> bool {
}
}

// return (type, is_optional, is_variadic)
pub fn ty_to_ts_type(
ty: &Type,
is_return_ty: bool,
Expand Down
8 changes: 6 additions & 2 deletions crates/backend/src/typegen/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ impl ToTypeDef for NapiImpl {
fn to_type_def(&self) -> Option<TypeDef> {
if let Some(output_type) = &self.task_output_type {
TASK_STRUCTS.with(|t| {
let resolved_type = ty_to_ts_type(output_type, false, true, false).0;
let (resolved_type, is_optional, _) = ty_to_ts_type(output_type, false, true, false);
t.borrow_mut().insert(
self.name.to_string(),
if resolved_type == "undefined" {
"void".to_owned()
} else {
resolved_type
if is_optional {
format!("{} | null", resolved_type)
} else {
resolved_type
}
},
);
});
Expand Down
2 changes: 2 additions & 0 deletions examples/napi/__tests__/__snapshots__/typegen.spec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ Generated by [AVA](https://avajs.dev).
export function asyncReduceBuffer(buf: Buffer): Promise<number>␊
export function asyncTaskOptionalReturn(): Promise<number | null>␊
export function asyncTaskVoidReturn(): Promise<void>␊
export interface B {␊
Expand Down
Binary file modified examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ export function asyncPlus100(p: Promise<number>): Promise<number>

export function asyncReduceBuffer(buf: Buffer): Promise<number>

export function asyncTaskOptionalReturn(): Promise<number | null>

export function asyncTaskVoidReturn(): Promise<void>

export interface B {
Expand Down
21 changes: 21 additions & 0 deletions examples/napi/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,24 @@ impl Task for AsyncTaskVoidReturn {
fn async_task_void_return() -> AsyncTask<AsyncTaskVoidReturn> {
AsyncTask::new(AsyncTaskVoidReturn {})
}

struct AsyncTaskOptionalReturn {}

#[napi]
impl Task for AsyncTaskOptionalReturn {
type JsValue = Option<u32>;
type Output = ();

fn compute(&mut self) -> Result<Self::Output> {
Ok(())
}

fn resolve(&mut self, _env: Env, _: Self::Output) -> Result<Self::JsValue> {
Ok(None)
}
}

#[napi]
fn async_task_optional_return() -> AsyncTask<AsyncTaskOptionalReturn> {
AsyncTask::new(AsyncTaskOptionalReturn {})
}

1 comment on commit adcb86a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: adcb86a Previous: 688ee04 Ratio
noop#napi-rs 58559791 ops/sec (±0.47%) 54757912 ops/sec (±1.01%) 0.94
noop#JavaScript 712470283 ops/sec (±0.19%) 663656359 ops/sec (±0.94%) 0.93
Plus number#napi-rs 18382220 ops/sec (±0.15%) 17309798 ops/sec (±1.21%) 0.94
Plus number#JavaScript 711132910 ops/sec (±0.12%) 672162126 ops/sec (±1.2%) 0.95
Create buffer#napi-rs 378740 ops/sec (±10.29%) 389726 ops/sec (±9.55%) 1.03
Create buffer#JavaScript 1881833 ops/sec (±8.37%) 1648526 ops/sec (±8.64%) 0.88
createArray#createArrayJson 38705 ops/sec (±0.14%) 36896 ops/sec (±1.14%) 0.95
createArray#create array for loop 7137 ops/sec (±0.16%) 6820 ops/sec (±1.01%) 0.96
createArray#create array with serde trait 7161 ops/sec (±0.31%) 6810 ops/sec (±1.31%) 0.95
getArrayFromJs#get array from json string 16710 ops/sec (±0.14%) 16288 ops/sec (±1.01%) 0.97
getArrayFromJs#get array from serde 9751 ops/sec (±0.1%) 9221 ops/sec (±1.13%) 0.95
getArrayFromJs#get array with for loop 12196 ops/sec (±0.05%) 11431 ops/sec (±1.13%) 0.94
Get Set property#Get Set from native#u32 442528 ops/sec (±8.63%) 416068 ops/sec (±8.8%) 0.94
Get Set property#Get Set from JavaScript#u32 402113 ops/sec (±9.56%) 367107 ops/sec (±9.29%) 0.91
Get Set property#Get Set from native#string 418464 ops/sec (±16.53%) 378515 ops/sec (±15.91%) 0.90
Get Set property#Get Set from JavaScript#string 399661 ops/sec (±3.33%) 362662 ops/sec (±3.35%) 0.91
Async task#spawn task 35881 ops/sec (±1.05%) 32985 ops/sec (±1.11%) 0.92
Async task#ThreadSafeFunction 9752 ops/sec (±5.32%) 6843 ops/sec (±18%) 0.70
Async task#Tokio future to Promise 35209 ops/sec (±1.56%) 29159 ops/sec (±1.02%) 0.83
Query#query * 100 2289 ops/sec (±0.75%) 1988 ops/sec (±1.8%) 0.87
Query#query * 1 30913 ops/sec (±1.6%) 27914 ops/sec (±1.79%) 0.90

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.