Skip to content

Commit bde407d

Browse files
authored
fix(napi-derive): generate types for threadsafe_function with WEAK=true correctly (#2813)
1 parent 069661e commit bde407d

8 files changed

Lines changed: 20 additions & 6 deletions

File tree

crates/backend/src/typegen.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,9 @@ pub fn ty_to_ts_type(
511511
{
512512
Some((t, false))
513513
} else if rust_ty == TSFN_RUST_TY {
514-
let fatal_tsfn = match args.last() {
515-
Some((arg, _)) => arg == "false",
516-
_ => false,
514+
let handled_tsfn = match args.get(4) {
515+
Some((arg, _)) => arg == "true",
516+
_ => true,
517517
};
518518
let fn_args = args
519519
.get(2)
@@ -534,13 +534,13 @@ pub fn ty_to_ts_type(
534534
.get(1)
535535
.map(|(ty, _)| ty.clone())
536536
.unwrap_or("any".to_owned());
537-
if fatal_tsfn {
538-
Some((format!("(({fn_args}) => {return_ty})"), false))
539-
} else {
537+
if handled_tsfn {
540538
Some((
541539
format!("((err: Error | null, {fn_args}) => {return_ty})"),
542540
false,
543541
))
542+
} else {
543+
Some((format!("(({fn_args}) => {return_ty})"), false))
544544
}
545545
} else {
546546
// there should be runtime registered type in else

examples/napi/__tests__/__snapshots__/values.spec.ts.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,8 @@ Generated by [AVA](https://avajs.dev).
999999
10001000
export declare function tsfnThrowFromJsCallbackContainsTsfn(tsfn: ((err: Error | null, arg: number) => Promise<number>)): Promise<void>␊
10011001
1002+
export declare function tsfnWeak(tsfn: (() => void)): Promise<void>␊
1003+
10021004
export declare function tsRename(a: { foo: number }): string[]␊
10031005
10041006
export interface TsTypeChanged {␊
10 Bytes
Binary file not shown.

examples/napi/example.wasi-browser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ export const tsfnReturnPromise = __napiModule.exports.tsfnReturnPromise
350350
export const tsfnReturnPromiseTimeout = __napiModule.exports.tsfnReturnPromiseTimeout
351351
export const tsfnThrowFromJs = __napiModule.exports.tsfnThrowFromJs
352352
export const tsfnThrowFromJsCallbackContainsTsfn = __napiModule.exports.tsfnThrowFromJsCallbackContainsTsfn
353+
export const tsfnWeak = __napiModule.exports.tsfnWeak
353354
export const tsRename = __napiModule.exports.tsRename
354355
export const u16ArrayToArray = __napiModule.exports.u16ArrayToArray
355356
export const u32ArrayToArray = __napiModule.exports.u32ArrayToArray

examples/napi/example.wasi.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ module.exports.tsfnReturnPromise = __napiModule.exports.tsfnReturnPromise
395395
module.exports.tsfnReturnPromiseTimeout = __napiModule.exports.tsfnReturnPromiseTimeout
396396
module.exports.tsfnThrowFromJs = __napiModule.exports.tsfnThrowFromJs
397397
module.exports.tsfnThrowFromJsCallbackContainsTsfn = __napiModule.exports.tsfnThrowFromJsCallbackContainsTsfn
398+
module.exports.tsfnWeak = __napiModule.exports.tsfnWeak
398399
module.exports.tsRename = __napiModule.exports.tsRename
399400
module.exports.u16ArrayToArray = __napiModule.exports.u16ArrayToArray
400401
module.exports.u32ArrayToArray = __napiModule.exports.u32ArrayToArray

examples/napi/index.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ module.exports.tsfnReturnPromise = nativeBinding.tsfnReturnPromise
680680
module.exports.tsfnReturnPromiseTimeout = nativeBinding.tsfnReturnPromiseTimeout
681681
module.exports.tsfnThrowFromJs = nativeBinding.tsfnThrowFromJs
682682
module.exports.tsfnThrowFromJsCallbackContainsTsfn = nativeBinding.tsfnThrowFromJsCallbackContainsTsfn
683+
module.exports.tsfnWeak = nativeBinding.tsfnWeak
683684
module.exports.tsRename = nativeBinding.tsRename
684685
module.exports.u16ArrayToArray = nativeBinding.u16ArrayToArray
685686
module.exports.u32ArrayToArray = nativeBinding.u32ArrayToArray

examples/napi/index.d.cts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ export declare function tsfnThrowFromJs(tsfn: ((err: Error | null, arg: number)
961961

962962
export declare function tsfnThrowFromJsCallbackContainsTsfn(tsfn: ((err: Error | null, arg: number) => Promise<number>)): Promise<void>
963963

964+
export declare function tsfnWeak(tsfn: (() => void)): Promise<void>
965+
964966
export declare function tsRename(a: { foo: number }): string[]
965967

966968
export interface TsTypeChanged {

examples/napi/src/threadsafe_function.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,10 @@ pub fn tsfn_in_either(pet: Pet) {
284284
});
285285
}
286286
}
287+
288+
#[napi]
289+
pub async fn tsfn_weak(
290+
tsfn: ThreadsafeFunction<(), (), (), Status, false, true>,
291+
) -> napi::Result<()> {
292+
tsfn.call_async(()).await
293+
}

0 commit comments

Comments
 (0)