|
1 | 1 | // N.B. The contents of this file are duplicated in src/library_wasm_worker.js
|
2 | 2 | // in variable "_wasmWorkerBlobUrl" (where the contents are pre-minified) If
|
3 | 3 | // doing any changes to this file, be sure to update the contents there too.
|
4 |
| -onmessage = function(d) { |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +#if ENVIRONMENT_MAY_BE_NODE |
| 8 | +// Node.js support |
| 9 | +var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string'; |
| 10 | +if (ENVIRONMENT_IS_NODE) { |
| 11 | + // Create as web-worker-like an environment as we can. |
| 12 | + |
| 13 | + var nodeWorkerThreads = require('worker_threads'); |
| 14 | + |
| 15 | + var parentPort = nodeWorkerThreads.parentPort; |
| 16 | + |
| 17 | + parentPort.on('message', (data) => typeof onmessage === "function" && onmessage({ data: data })); |
| 18 | + |
| 19 | + var fs = require('fs'); |
| 20 | + |
| 21 | + Object.assign(global, { |
| 22 | + self: global, |
| 23 | + require, |
| 24 | + location: { |
| 25 | + href: __filename |
| 26 | + }, |
| 27 | + Worker: nodeWorkerThreads.Worker, |
| 28 | + importScripts: (f) => (0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f), |
| 29 | + postMessage: (msg) => parentPort.postMessage(msg), |
| 30 | + performance: global.performance || { now: Date.now }, |
| 31 | + addEventListener: (name, handler) => parentPort.on(name, handler), |
| 32 | + removeEventListener: (name, handler) => parentPort.off(name, handler), |
| 33 | + }); |
| 34 | +} |
| 35 | +#endif // ENVIRONMENT_MAY_BE_NODE |
| 36 | + |
| 37 | +{{{ implicitSelf() }}}onmessage = function(d) { |
5 | 38 | // The first message sent to the Worker is always the bootstrap message.
|
6 | 39 | // Drop this message listener, it served its purpose of bootstrapping
|
7 | 40 | // the Wasm Module load, and is no longer needed. Let user code register
|
8 | 41 | // any desired message handlers from now on.
|
9 |
| - onmessage = null; |
| 42 | + {{{ implicitSelf() }}}onmessage = null; |
10 | 43 | d = d.data;
|
11 | 44 | #if !MODULARIZE
|
12 | 45 | self.{{{ EXPORT_NAME }}} = d;
|
13 | 46 | #endif
|
14 | 47 | #if !MINIMAL_RUNTIME
|
15 |
| - d['instantiateWasm'] = (info, receiveInstance) => { var instance = new WebAssembly.Instance(d['wasm'], info); receiveInstance(instance, d['wasm']); return instance.exports; } |
| 48 | + d['instantiateWasm'] = (info, receiveInstance) => { var instance = new WebAssembly.Instance(d['wasm'], info); return receiveInstance(instance, d['wasm']); } |
16 | 49 | #endif
|
17 | 50 | importScripts(d.js);
|
18 | 51 | #if MODULARIZE
|
|
0 commit comments