Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed May 9, 2024
1 parent 495c987 commit 047aec3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
28 changes: 3 additions & 25 deletions packages/emnapi/src/core/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 20 additions & 1 deletion packages/wasi-threads/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,26 @@
<script src="../dist/wasi-threads.js"></script>
<!-- <script src="./index.js"></script> -->
<script>
new Worker('./index.js')
const worker = new Worker('./index.js')
const map = new Map()
worker.onmessage = (e) => {
const { type, payload } = e.data
if (type === 'new') {
const { id, url, options } = payload
const w = new Worker(url, options)
map.set(id, w)
w.onmessage = (e) => {
worker.postMessage({ type: 'onmessage', payload: { id, data: e.data } })
}
} else if (type === 'postMessage') {
const { id, data } = payload
map.get(id).postMessage(data)
} else if (type === 'terminate') {
const { id } = payload
map.get(id).terminate()
map.delete(id)
}
}
</script>
</body>
</html>
43 changes: 40 additions & 3 deletions packages/wasi-threads/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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({
Expand All @@ -46,7 +83,7 @@
})
},
// optional
waitThreadStart: ENVIRONMENT_IS_NODE
waitThreadStart: true
})
const memory = new WebAssembly.Memory({
initial: 16777216 / 65536,
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 047aec3

Please sign in to comment.