Skip to content

Commit

Permalink
test(napi): spwan thread in thread (#2139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jun 24, 2024
1 parent f972158 commit d7a5a7e
Show file tree
Hide file tree
Showing 10 changed files with 477 additions and 435 deletions.
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 @@ -649,6 +649,8 @@ Generated by [AVA](https://avajs.dev).
value: number␊
}␊
export declare function spawnThreadInThread(tsfn: (err: Error | null, arg: number) => number): void␊
export declare const enum Status {␊
Pristine = 'Pristine',␊
Loading = 'Loading',␊
Expand Down
Binary file modified examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap
Binary file not shown.
Binary file modified examples/napi/__tests__/__snapshots__/values.spec.ts.snap
Binary file not shown.
8 changes: 5 additions & 3 deletions examples/napi/__tests__/values.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,11 @@ test('should be able to into_reference', (t) => {
})

test('callback', (t) => {
getCwd((cwd) => {
t.is(cwd, process.env.WASI_TEST ? '/' : process.cwd())
})
if (!process.env.WASI_TEST) {
getCwd((cwd) => {
t.is(cwd, process.cwd())
})
}

t.throws(
// @ts-expect-error
Expand Down
18 changes: 18 additions & 0 deletions examples/napi/__tests__/wasi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import test from 'ava'

import { spawnThreadInThread } from '../index.cjs'

test('spawnThreadInThread should be fine', async (t) => {
await new Promise((resolve, reject) => {
spawnThreadInThread((err, num) => {
if (err) {
reject(err)
} else {
t.is(num, 42)
resolve(void 0)
}
return 0
})
})
t.pass()
})
436 changes: 220 additions & 216 deletions examples/napi/example.wasi-browser.js

Large diffs are not rendered by default.

436 changes: 220 additions & 216 deletions examples/napi/example.wasi.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/napi/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ module.exports.returnUndefinedIfInvalidPromise = nativeBinding.returnUndefinedIf
module.exports.roundtripStr = nativeBinding.roundtripStr
module.exports.runScript = nativeBinding.runScript
module.exports.setSymbolInObj = nativeBinding.setSymbolInObj
module.exports.spawnThreadInThread = nativeBinding.spawnThreadInThread
module.exports.Status = nativeBinding.Status
module.exports.StringEnum = nativeBinding.StringEnum
module.exports.sumBtreeMapping = nativeBinding.sumBtreeMapping
Expand Down
2 changes: 2 additions & 0 deletions examples/napi/index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ export interface Shared {
value: number
}

export declare function spawnThreadInThread(tsfn: (err: Error | null, arg: number) => number): void

export declare const enum Status {
Pristine = 'Pristine',
Loading = 'Loading',
Expand Down
9 changes: 9 additions & 0 deletions examples/napi/src/threadsafe_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,12 @@ pub async fn tsfn_return_promise_timeout(
pub async fn tsfn_throw_from_js(tsfn: ThreadsafeFunction<u32, Promise<u32>>) -> napi::Result<u32> {
tsfn.call_async(Ok(42)).await?.await
}

#[napi]
pub fn spawn_thread_in_thread(tsfn: ThreadsafeFunction<u32, u32>) {
std::thread::spawn(move || {
std::thread::spawn(move || {
tsfn.call(Ok(42), ThreadsafeFunctionCallMode::NonBlocking);
});
});
}

0 comments on commit d7a5a7e

Please sign in to comment.