|
1 | | -// globalThis.ENVIRONMENT_IS_PTHREAD = true |
| 1 | +/* eslint-disable no-eval */ |
| 2 | +/* eslint-disable no-undef */ |
2 | 3 |
|
3 | | -// const log = (...args) => { |
4 | | -// const str = require('util').format(...args) |
5 | | -// require('fs').writeSync(1, str + '\n') |
6 | | -// } |
7 | | -// const error = (...args) => { |
8 | | -// const str = require('util').format(...args) |
9 | | -// require('fs').writeSync(2, str + '\n') |
10 | | -// } |
| 4 | +(function () { |
| 5 | + // const log = (...args) => { |
| 6 | + // const str = require('util').format(...args) |
| 7 | + // require('fs').writeSync(1, str + '\n') |
| 8 | + // } |
| 9 | + // const error = (...args) => { |
| 10 | + // const str = require('util').format(...args) |
| 11 | + // require('fs').writeSync(2, str + '\n') |
| 12 | + // } |
| 13 | + let fs, WASI |
11 | 14 |
|
12 | | -const nodeWorkerThreads = require('worker_threads') |
| 15 | + const ENVIRONMENT_IS_NODE = |
| 16 | + typeof process === 'object' && process !== null && |
| 17 | + typeof process.versions === 'object' && process.versions !== null && |
| 18 | + typeof process.versions.node === 'string' |
13 | 19 |
|
14 | | -const parentPort = nodeWorkerThreads.parentPort |
| 20 | + if (ENVIRONMENT_IS_NODE) { |
| 21 | + const nodeWorkerThreads = require('worker_threads') |
15 | 22 |
|
16 | | -parentPort.on('message', (data) => { |
17 | | - onmessage({ data }) |
18 | | -}) |
| 23 | + const parentPort = nodeWorkerThreads.parentPort |
19 | 24 |
|
20 | | -const fs = require('fs') |
| 25 | + parentPort.on('message', (data) => { |
| 26 | + globalThis.onmessage({ data }) |
| 27 | + }) |
21 | 28 |
|
22 | | -Object.assign(global, { |
23 | | - self: global, |
24 | | - require, |
25 | | - // Module, |
26 | | - location: { |
27 | | - href: __filename |
28 | | - }, |
29 | | - Worker: nodeWorkerThreads.Worker, |
30 | | - importScripts: function (f) { |
31 | | - // eslint-disable-next-line no-eval |
32 | | - (0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f) |
33 | | - }, |
34 | | - postMessage: function (msg) { |
35 | | - parentPort.postMessage(msg) |
36 | | - }, |
37 | | - performance: global.performance || { |
38 | | - now: function () { |
39 | | - return Date.now() |
40 | | - } |
| 29 | + fs = require('fs') |
| 30 | + |
| 31 | + Object.assign(globalThis, { |
| 32 | + self: globalThis, |
| 33 | + require, |
| 34 | + Worker: nodeWorkerThreads.Worker, |
| 35 | + importScripts: function (f) { |
| 36 | + (0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f) |
| 37 | + }, |
| 38 | + postMessage: function (msg) { |
| 39 | + parentPort.postMessage(msg) |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + WASI = require('./wasi').WASI |
| 44 | + } else { |
| 45 | + importScripts('../../node_modules/memfs-browser/dist/memfs.js') |
| 46 | + importScripts('../../node_modules/@tybys/wasm-util/dist/wasm-util.js') |
| 47 | + |
| 48 | + const { Volume, createFsFromVolume } = memfs |
| 49 | + fs = createFsFromVolume(Volume.from({ |
| 50 | + '/': null |
| 51 | + })) |
| 52 | + |
| 53 | + WASI = globalThis.wasmUtil.WASI |
41 | 54 | } |
42 | | -}) |
43 | 55 |
|
44 | | -const { WASI } = require('./wasi') |
45 | | -const { loadNapiModuleSync, handleMessage } = require('@emnapi/core') |
| 56 | + importScripts('../../node_modules/@emnapi/core/dist/emnapi-core.js') |
46 | 57 |
|
47 | | -function instantiate (wasmMemory, wasmModule, tid, arg) { |
48 | | - const wasi = new WASI({ |
49 | | - fs, |
50 | | - print (...args) { |
51 | | - const str = require('util').format(...args) |
52 | | - fs.writeSync(1, str + '\n') |
53 | | - } |
54 | | - }) |
| 58 | + const { loadNapiModuleSync, handleMessage } = globalThis.emnapiCore |
55 | 59 |
|
56 | | - loadNapiModuleSync(wasmModule, { |
57 | | - childThread: true, |
58 | | - wasi, |
59 | | - overwriteImports (importObject) { |
60 | | - importObject.env.memory = wasmMemory |
61 | | - }, |
62 | | - tid, |
63 | | - arg |
64 | | - }) |
65 | | -} |
| 60 | + function onLoad (payload) { |
| 61 | + const wasi = new WASI({ |
| 62 | + fs, |
| 63 | + print: ENVIRONMENT_IS_NODE |
| 64 | + ? (...args) => { |
| 65 | + const str = require('util').format(...args) |
| 66 | + fs.writeSync(1, str + '\n') |
| 67 | + } |
| 68 | + : function () { console.log.apply(console, arguments) } |
| 69 | + }) |
66 | 70 |
|
67 | | -self.onmessage = function (e) { |
68 | | - handleMessage(e, (type, payload) => { |
69 | | - if (type === 'load') { |
70 | | - instantiate(payload.wasmMemory, payload.wasmModule, payload.tid, payload.arg) |
71 | | - } |
72 | | - }) |
73 | | -} |
| 71 | + loadNapiModuleSync(payload.wasmModule, { |
| 72 | + childThread: true, |
| 73 | + wasi, |
| 74 | + overwriteImports (importObject) { |
| 75 | + importObject.env.memory = payload.wasmMemory |
| 76 | + }, |
| 77 | + tid: payload.tid, |
| 78 | + arg: payload.arg |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + globalThis.onmessage = function (e) { |
| 83 | + handleMessage(e, (type, payload) => { |
| 84 | + if (type === 'load') { |
| 85 | + onLoad(payload) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +})() |
0 commit comments