fix(napi-derive): missing move in async fn

This commit is contained in:
LongYinan 2021-11-06 22:19:42 +08:00
parent c2c5887ea4
commit 785388ae40
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
6 changed files with 15 additions and 1 deletions

View file

@ -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
})
}

View file

@ -12,6 +12,7 @@ Generated by [AVA](https://avajs.dev).
export function getNums(): Array<number>
export function sumNums(nums: Array<number>): number␊
export function readFileAsync(path: string): Promise<Buffer>
export function asyncMultiTwo(arg: number): Promise<number>
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␊

View file

@ -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)

View file

@ -2,6 +2,7 @@ export function getWords(): Array<string>
export function getNums(): Array<number>
export function sumNums(nums: Array<number>): number
export function readFileAsync(path: string): Promise<Buffer>
export function asyncMultiTwo(arg: number): Promise<number>
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

View file

@ -14,3 +14,10 @@ async fn read_file_async(path: String) -> Result<Buffer> {
})
.await
}
#[napi]
async fn async_multi_two(arg: u32) -> Result<u32> {
tokio::task::spawn(async move { Ok(arg * 2) })
.await
.unwrap()
}