Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ on:
workflow_dispatch:

env:
WASI_VERSION: '25'
WASI_VERSION_FULL: '25.0'
WASI_SDK_PATH: './wasi-sdk-25.0'
EM_VERSION: '3.1.64'
WASI_VERSION: '27'
WASI_VERSION_FULL: '27.0'
WASI_SDK_PATH: './wasi-sdk-27.0'
EM_VERSION: '4.0.1'
EM_CACHE_FOLDER: 'emsdk-cache'
NODE_VERSION: '22.15.0'
NODE_VERSION: '22.16.0'

jobs:
build:
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
node-version: ${{ matrix.target == 'wasm64-unknown-emscripten' && '24.5.0' || env.NODE_VERSION }}
registry-url: 'https://registry.npmjs.org'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
60 changes: 48 additions & 12 deletions packages/core/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
ThreadMessageHandler,
type ThreadMessageHandlerOptions,
type LoadPayload
type LoadPayload,
type WorkerMessageEvent
} from '@emnapi/wasi-threads'
import type { NapiModule } from './emnapi/index'
import type { InstantiatedSource } from './load'
Expand All @@ -21,7 +22,24 @@ export class MessageHandler extends ThreadMessageHandler {
if (typeof options.onLoad !== 'function') {
throw new TypeError('options.onLoad is not a function')
}
super(options)
const userOnError = options.onError
super({
...options,
onError: (err, type) => {
const emnapi_thread_crashed = this.instance?.exports.emnapi_thread_crashed as () => void
if (typeof emnapi_thread_crashed === 'function') {
emnapi_thread_crashed()
} /* else {
tryWakeUpPthreadJoin(this.instance!)
} */

if (typeof userOnError === 'function') {
userOnError(err, type)
} else {
throw err
}
}
})
this.napiModule = undefined
}

Expand All @@ -38,21 +56,39 @@ export class MessageHandler extends ThreadMessageHandler {
return source
}

public override handle (e: any): void {
public override handle (e: WorkerMessageEvent): void {
super.handle(e)
if (e?.data?.__emnapi__) {
const type = e.data.__emnapi__.type
const payload = e.data.__emnapi__.payload

if (type === 'async-worker-init') {
this.handleAfterLoad(e, () => {
this.napiModule!.initWorker(payload.arg)
})
} else if (type === 'async-work-execute') {
this.handleAfterLoad(e, () => {
this.napiModule!.executeAsyncWork(payload.work)
})
try {
if (type === 'async-worker-init') {
this.handleAfterLoad(e, () => {
this.napiModule!.initWorker(payload.arg)
})
} else if (type === 'async-work-execute') {
this.handleAfterLoad(e, () => {
this.napiModule!.executeAsyncWork(payload.work)
})
}
} catch (err) {
this.onError(err, type)
}
}
}
}

// function tryWakeUpPthreadJoin (instance: WebAssembly.Instance): void {
// // https://github.com/WebAssembly/wasi-libc/blob/574b88da481569b65a237cb80daf9a2d5aeaf82d/libc-top-half/musl/src/thread/pthread_join.c#L18-L21
// const pthread_self = instance.exports.pthread_self as () => number
// const memory = instance.exports.memory as WebAssembly.Memory
// if (typeof pthread_self === 'function') {
// const selfThread = pthread_self()
// if (selfThread && memory) {
// // https://github.com/WebAssembly/wasi-libc/blob/574b88da481569b65a237cb80daf9a2d5aeaf82d/libc-top-half/musl/src/internal/pthread_impl.h#L45
// const detatchState = new Int32Array(memory.buffer, selfThread + 7 * 4 /** detach_state */, 1)
// Atomics.store(detatchState, 0, 0)
// Atomics.notify(detatchState, 0, Infinity)
// }
// }
// }
1 change: 1 addition & 0 deletions packages/emnapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ set(ENAPI_BASIC_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/src/node_api.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/async_cleanup_hook.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/async_context.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/wasi_wait.c"
)
set(EMNAPI_THREADS_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/src/async_work.c"
Expand Down
6 changes: 4 additions & 2 deletions packages/emnapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,9 @@ Now emnapi has 3 implementations of async work and 2 implementations of TSFN:
There are some limitations on browser about wasi-libc's pthread implementation, for example
`pthread_mutex_lock` may call `__builtin_wasm_memory_atomic_wait32`(`memory.atomic.wait32`)
which is disallowed in browser JS main thread. While Emscripten's pthread implementation
has considered usage in browser. If you need to run your addon with multithreaded features on browser,
we recommend you use Emscripten A & D, or bare wasm32 C & E.
has considered usage in browser. This issue can be solved by upgrading `wasi-sdk` to v26+
and emnapi v1.5.0+ then pass `--export=emnapi_thread_crashed` to the linker. If you need to
run your addon with multithreaded features, we recommend you use A & D or C & E.
Note: For browsers, all the multithreaded features relying on Web Workers (Emscripten pthread also relying on Web Workers)
require cross-origin isolation to enable `SharedArrayBuffer`. You can make a page cross-origin isolated
Expand Down Expand Up @@ -879,6 +880,7 @@ elseif(CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi-threads")
"-Wl,--export-if-defined=node_api_module_get_api_version_v1"
"-Wl,--export=malloc"
"-Wl,--export=free"
"-Wl,--export=emnapi_thread_crashed"
"-Wl,--import-undefined"
"-Wl,--export-table"
)
Expand Down
11 changes: 11 additions & 0 deletions packages/emnapi/common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@
'src/async_cleanup_hook.c',
'src/async_context.c',
'src/async_work.c',
'src/wasi_wait.c',
'src/threadsafe_function.c',
'src/uv/uv-common.c',
'src/uv/threadpool.c',
Expand Down Expand Up @@ -372,6 +373,16 @@
]
},
}],
['OS == "wasi"', {
'ldflags': [
'-Wl,--export=emnapi_thread_crashed',
],
'xcode_settings': {
'OTHER_LDFLAGS': [
'-Wl,--export=emnapi_thread_crashed',
],
},
}],
['OS != "wasi"', {
'defines': [
'PAGESIZE=65536'
Expand Down
18 changes: 18 additions & 0 deletions packages/emnapi/emnapi.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'src/node_api.c',
'src/async_cleanup_hook.c',
'src/async_context.c',
'src/wasi_wait.c',
],
'link_settings': {
'target_conditions': [
Expand Down Expand Up @@ -88,6 +89,22 @@
]
},
}],
['OS == "wasi"', {
'link_settings': {
'target_conditions': [
['_type == "executable"', {
'ldflags': [
'-Wl,--export=emnapi_thread_crashed',
],
'xcode_settings': {
'OTHER_LDFLAGS': [
'-Wl,--export=emnapi_thread_crashed',
],
},
}],
]
},
}],
]
},
{
Expand All @@ -98,6 +115,7 @@
'src/node_api.c',
'src/async_cleanup_hook.c',
'src/async_context.c',
'src/wasi_wait.c',

'src/uv/uv-common.c',
'src/uv/threadpool.c',
Expand Down
5 changes: 0 additions & 5 deletions packages/emnapi/src/core/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ var uvThreadpoolReady: Promise<void> & { ready: boolean } = new Promise<void>((r
}) as any
uvThreadpoolReady.ready = false

/** @__sig i */
export function _emnapi_is_main_browser_thread (): number {
return (typeof window !== 'undefined' && typeof document !== 'undefined' && !ENVIRONMENT_IS_NODE) ? 1 : 0
}

/** @__sig vppi */
export function _emnapi_after_uvthreadpool_ready (callback: number, q: number, type: number): void {
if (uvThreadpoolReady.ready) {
Expand Down
10 changes: 8 additions & 2 deletions packages/emnapi/src/core/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ export var napiModule: INapiModule = {
const moduleHandle = scope.add(napiModule)
instance.exports[nodeRegisterModuleSymbol](to64('exportsHandle'), to64('moduleHandle'), to64('5'))
} catch (err) {
if (err !== 'unwind') {
throw err
}
} finally {
emnapiCtx.isolate.closeScope(scope)
throw err
}
emnapiCtx.isolate.closeScope(scope)
napiModule.loaded = true
delete napiModule.envObject
return napiModule.exports
Expand Down Expand Up @@ -150,6 +152,10 @@ export var napiModule: INapiModule = {
const napiValue = napi_register_wasm_v1(to64('_envObject.id'), to64('exportsHandle'))
napiModule.exports = (!napiValue) ? exports : emnapiCtx.jsValueFromNapiValue(napiValue)!
})
} catch (e) {
if (e !== 'unwind') {
throw e
}
} finally {
emnapiCtx.closeScope(envObject, scope)
}
Expand Down
5 changes: 5 additions & 0 deletions packages/emnapi/src/emnapi_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ EMNAPI_INTERNAL_EXTERN void _emnapi_env_unref(napi_env env);
EMNAPI_INTERNAL_EXTERN void _emnapi_ctx_increase_waiting_request_counter();
EMNAPI_INTERNAL_EXTERN void _emnapi_ctx_decrease_waiting_request_counter();

EMNAPI_INTERNAL_EXTERN int _emnapi_is_main_browser_thread();
EMNAPI_INTERNAL_EXTERN int _emnapi_is_main_runtime_thread();
EMNAPI_INTERNAL_EXTERN double _emnapi_get_now();
EMNAPI_INTERNAL_EXTERN void _emnapi_unwind();

#if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
#define EMNAPI_HAVE_THREADS 1
#else
Expand Down
12 changes: 8 additions & 4 deletions packages/emnapi/src/emscripten/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ export function emnapiInit (options: InitOptions): any {
const moduleHandle = scope.add(emnapiModule)
Module[emscriptenExportedSymbol](to64('exportsHandle'), to64('moduleHandle'), to64('5'))
} catch (err) {
if (err !== 'unwind') {
throw err
}
} finally {
emnapiCtx.isolate.closeScope(scope)
throw err
}
emnapiCtx.isolate.closeScope(scope)
emnapiModule.loaded = true
delete emnapiModule.envObject
return emnapiModule.exports
Expand Down Expand Up @@ -118,10 +120,12 @@ NODE_MODULE_VERSION ${NODE_MODULE_VERSION}.`)
emnapiModule.exports = (!napiValue) ? exports : emnapiCtx.jsValueFromNapiValue(napiValue)!
})
} catch (err) {
if (err !== 'unwind') {
throw err
}
} finally {
emnapiCtx.closeScope(envObject, scope)
throw err
}
emnapiCtx.closeScope(envObject, scope)
emnapiModule.loaded = true
delete emnapiModule.envObject
return emnapiModule.exports
Expand Down
30 changes: 29 additions & 1 deletion packages/emnapi/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runtimeKeepalivePop, runtimeKeepalivePush } from 'emscripten:runtime'
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_PTHREAD, runtimeKeepalivePop, runtimeKeepalivePush } from 'emscripten:runtime'
import { from64, makeSetValue, makeDynCall } from 'emscripten:parse-tools'
import { emnapiCtx } from 'emnapi:shared'

Expand Down Expand Up @@ -119,6 +119,34 @@ export function _emnapi_ctx_decrease_waiting_request_counter (): void {
emnapiCtx.decreaseWaitingRequestCounter()
}

/**
* @__sig i
*/
export function _emnapi_is_main_runtime_thread (): number {
return ENVIRONMENT_IS_PTHREAD ? 0 : 1
}

/**
* @__sig i
*/
export function _emnapi_is_main_browser_thread (): number {
return (typeof window !== 'undefined' && typeof document !== 'undefined' && !ENVIRONMENT_IS_NODE) ? 1 : 0
}

/**
* @__sig v
*/
export function _emnapi_unwind (): never {
throw 'unwind'
}

/**
* @__sig d
*/
export function _emnapi_get_now (): double {
return performance.timeOrigin + performance.now()
}

export function $emnapiSetValueI64 (result: Pointer<int64_t>, numberValue: number): void {
let tempDouble: number

Expand Down
Loading