From 047aec36fb17f32c04f0cddd9ccb6ffb78a513e7 Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Thu, 9 May 2024 22:19:23 +0800 Subject: [PATCH] fix --- packages/emnapi/src/core/init.ts | 28 ++--------------- packages/wasi-threads/test/index.html | 21 ++++++++++++- packages/wasi-threads/test/index.js | 43 +++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 29 deletions(-) diff --git a/packages/emnapi/src/core/init.ts b/packages/emnapi/src/core/init.ts index b3339ca8..9b918361 100644 --- a/packages/emnapi/src/core/init.ts +++ b/packages/emnapi/src/core/init.ts @@ -244,37 +244,15 @@ function emnapiAddSendListener (worker: any): boolean { napiModule.emnapi.addSendListener = emnapiAddSendListener -function terminateWorker (worker: any): void { - const tid = worker.__emnapi_tid - worker.terminate() - worker.onmessage = (e: any) => { - if (e.data.__emnapi__) { - err('received "' + e.data.__emnapi__.type + '" command from terminated worker: ' + tid) - } - } -} - -function cleanThread (worker: any, tid: number, force?: boolean): void { - if (!force && reuseWorker) { - PThread.returnWorkerToPool(worker) - } else { - delete PThread.pthreads[tid] - const index = PThread.runningWorkers.indexOf(worker) - if (index !== -1) { - PThread.runningWorkers.splice(index, 1) - } - terminateWorker(worker) - delete worker.__emnapi_tid - } -} - export var PThread = new wasiThreads.ThreadManager({ printErr: err, beforeLoad: (worker) => { emnapiAddSendListener(worker) }, reuseWorker, - onCreateWorker: onCreateWorker as ThreadManagerOptions['onCreateWorker'] + onCreateWorker: onCreateWorker as ThreadManagerOptions['onCreateWorker'] ?? (() => { + throw new Error('options.onCreateWorker` is not provided') + }) }) napiModule.PThread = PThread diff --git a/packages/wasi-threads/test/index.html b/packages/wasi-threads/test/index.html index 73e3a545..21edadf3 100644 --- a/packages/wasi-threads/test/index.html +++ b/packages/wasi-threads/test/index.html @@ -10,7 +10,26 @@ diff --git a/packages/wasi-threads/test/index.js b/packages/wasi-threads/test/index.js index c71629cf..ed968581 100644 --- a/packages/wasi-threads/test/index.js +++ b/packages/wasi-threads/test/index.js @@ -9,6 +9,43 @@ WASIThreads = require('..').WASIThreads } else { if (typeof importScripts === 'function') { + globalThis.Worker = class MyWorker { + constructor (url, options) { + this.id = String(Math.random()) + self.addEventListener('message', ({ data }) => { + if (data.type === 'onmessage' && data.payload.id === this.id) { + this.onmessage?.({ data: data.payload.data }) + } + }) + postMessage({ + type: 'new', + payload: { + id: this.id, + url, + options + } + }) + } + + postMessage (data) { + postMessage({ + type: 'postMessage', + payload: { + id: this.id, + data + } + }) + } + + terminate () { + postMessage({ + type: 'terminate', + payload: { + id: this.id + } + }) + } + } // eslint-disable-next-line no-undef importScripts('../../../node_modules/@tybys/wasm-util/dist/wasm-util.min.js') // eslint-disable-next-line no-undef @@ -28,7 +65,7 @@ const file = model === ExecutionModel.Command ? 'main.wasm' : 'lib.wasm' const wasi = new WASI({ version: 'preview1', - args: [file, ENVIRONMENT_IS_NODE ? 'node' : 'web'], + args: [file, 'node'], ...(ENVIRONMENT_IS_NODE ? { env: process.env } : {}) }) const wasiThreads = new WASIThreads({ @@ -46,7 +83,7 @@ }) }, // optional - waitThreadStart: ENVIRONMENT_IS_NODE + waitThreadStart: true }) const memory = new WebAssembly.Memory({ initial: 16777216 / 65536, @@ -73,7 +110,7 @@ return wasi.start(instance) } else { wasi.initialize(instance) - return instance.exports.fn(ENVIRONMENT_IS_NODE ? 1 : 0) + return instance.exports.fn(1) } }