From 785388ae40f054b39f9dd310297a3850e6266f63 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 6 Nov 2021 22:19:42 +0800 Subject: [PATCH] fix(napi-derive): missing move in async fn --- crates/backend/src/codegen/fn.rs | 2 +- examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 909 -> 923 bytes examples/napi/__test__/values.spec.ts | 5 +++++ examples/napi/index.d.ts | 1 + examples/napi/src/async.rs | 7 +++++++ 6 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index 2f828ffc..a5b869a2 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -33,7 +33,7 @@ impl TryToTokens for NapiFn { quote! { Ok(#receiver(#(#arg_names),*).await) } }; quote! { - execute_tokio_future(env, async { #call }, |env, #receiver_ret_name| { + execute_tokio_future(env, async move { #call }, |env, #receiver_ret_name| { #ret }) } diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index a5412e72..4f2bb9cf 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -12,6 +12,7 @@ Generated by [AVA](https://avajs.dev). export function getNums(): Array␊ export function sumNums(nums: Array): number␊ export function readFileAsync(path: string): Promise␊ + export function asyncMultiTwo(arg: number): Promise␊ export function getCwd(callback: (arg0: string) => void): void␊ export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void␊ export function eitherStringOrNumber(input: string | number): number␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 0071ef33c1151ef6b60252889bbb1c1de36d0a18..d92c080ac4ad25d7b8c5e7a0eac488bbcd2d9f33 100644 GIT binary patch literal 923 zcmV;M17!R`RzV+ty1LP z>1CNa`DUycH~mVc=G%5#3hQW`F~RdZk5YEzfaCHyhqA7FTBJ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DvZdvwQ!9Sw|@p zAs`Q^VSPUZ6++Q{f+y?L8o*YY>zvb(=7n6v&Si*)CTS4rEUtRH8rW&h$9Q=9oLd^* zA-=Us;*5=PmMnlNtvku8R|_Q}=X`6&{t5BgRa*7LttF_-9cLMRYUQ@@CSZY7VOcHe jbshhGM>H4Qlg~CynVMLyk=dj{K@#5pWo|m|a|r+d)t$gK diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index d1548995..479e8810 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -34,6 +34,7 @@ import { either4, withoutAbortController, withAbortController, + asyncMultiTwo, } from '../' test('number', (t) => { @@ -163,6 +164,10 @@ test('async', async (t) => { await t.throwsAsync(() => readFileAsync('some_nonexist_path.file')) }) +test('async move', async (t) => { + t.is(await asyncMultiTwo(2), 4) +}) + test('either', (t) => { t.is(eitherStringOrNumber(2), 2) t.is(eitherStringOrNumber('hello'), 'hello'.length) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index caa26c4f..ab2e701d 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -2,6 +2,7 @@ export function getWords(): Array export function getNums(): Array export function sumNums(nums: Array): number export function readFileAsync(path: string): Promise +export function asyncMultiTwo(arg: number): Promise export function getCwd(callback: (arg0: string) => void): void export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void export function eitherStringOrNumber(input: string | number): number diff --git a/examples/napi/src/async.rs b/examples/napi/src/async.rs index 778c53ce..76ea50bd 100644 --- a/examples/napi/src/async.rs +++ b/examples/napi/src/async.rs @@ -14,3 +14,10 @@ async fn read_file_async(path: String) -> Result { }) .await } + +#[napi] +async fn async_multi_two(arg: u32) -> Result { + tokio::task::spawn(async move { Ok(arg * 2) }) + .await + .unwrap() +}