Website Structure
This commit is contained in:
parent
62812f2090
commit
71f0676a62
22365 changed files with 4265753 additions and 791 deletions
21
Frontend-Learner/node_modules/@emnapi/wasi-threads/LICENSE
generated
vendored
Normal file
21
Frontend-Learner/node_modules/@emnapi/wasi-threads/LICENSE
generated
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2021-present Toyobayashi
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
203
Frontend-Learner/node_modules/@emnapi/wasi-threads/README.md
generated
vendored
Normal file
203
Frontend-Learner/node_modules/@emnapi/wasi-threads/README.md
generated
vendored
Normal file
|
|
@ -0,0 +1,203 @@
|
|||
# @emnapi/wasi-threads
|
||||
|
||||
This package makes [wasi-threads proposal](https://github.com/WebAssembly/wasi-threads) based WASI modules work in Node.js and browser.
|
||||
|
||||
## Quick Start
|
||||
|
||||
`index.html`
|
||||
|
||||
```html
|
||||
<script src="./node_modules/@tybys/wasm-util/dist/wasm-util.js"></script>
|
||||
<script src="./node_modules/@emnapi/wasi-threads/dist/wasi-threads.js"></script>
|
||||
<script src="./index.js"></script>
|
||||
```
|
||||
|
||||
If your application will block browser main thread (for example `pthread_join`), please run it in worker instead.
|
||||
|
||||
```html
|
||||
<script>
|
||||
// pthread_join (Atomics.wait) cannot be called in browser main thread
|
||||
new Worker('./index.js')
|
||||
</script>
|
||||
```
|
||||
|
||||
`index.js`
|
||||
|
||||
```js
|
||||
const ENVIRONMENT_IS_NODE =
|
||||
typeof process === 'object' && process !== null &&
|
||||
typeof process.versions === 'object' && process.versions !== null &&
|
||||
typeof process.versions.node === 'string';
|
||||
|
||||
(function (main) {
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
main(require)
|
||||
} else {
|
||||
if (typeof importScripts === 'function') {
|
||||
importScripts('./node_modules/@tybys/wasm-util/dist/wasm-util.js')
|
||||
importScripts('./node_modules/@emnapi/wasi-threads/dist/wasi-threads.js')
|
||||
}
|
||||
const nodeWasi = { WASI: globalThis.wasmUtil.WASI }
|
||||
const nodeWorkerThreads = {
|
||||
Worker: globalThis.Worker
|
||||
}
|
||||
const _require = function (request) {
|
||||
if (request === 'node:wasi' || request === 'wasi') return nodeWasi
|
||||
if (request === 'node:worker_threads' || request === 'worker_threads') return nodeWorkerThreads
|
||||
if (request === '@emnapi/wasi-threads') return globalThis.wasiThreads
|
||||
throw new Error('Can not find module: ' + request)
|
||||
}
|
||||
main(_require)
|
||||
}
|
||||
})(async function (require) {
|
||||
const { WASI } = require('wasi')
|
||||
const { Worker } = require('worker_threads')
|
||||
const { WASIThreads } = require('@emnapi/wasi-threads')
|
||||
|
||||
const wasi = new WASI({
|
||||
version: 'preview1'
|
||||
})
|
||||
const wasiThreads = new WASIThreads({
|
||||
wasi,
|
||||
|
||||
/**
|
||||
* avoid Atomics.wait() deadlock during thread creation in browser
|
||||
* see https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size
|
||||
*/
|
||||
reuseWorker: ENVIRONMENT_IS_NODE
|
||||
? false
|
||||
: {
|
||||
size: 4 /** greater than actual needs (2) */,
|
||||
strict: true
|
||||
},
|
||||
|
||||
/**
|
||||
* Synchronous thread creation
|
||||
* pthread_create will not return until thread worker actually starts
|
||||
*/
|
||||
waitThreadStart: typeof window === 'undefined' ? 1000 : false,
|
||||
|
||||
onCreateWorker: () => {
|
||||
return new Worker('./worker.js', {
|
||||
execArgv: ['--experimental-wasi-unstable-preview1']
|
||||
})
|
||||
}
|
||||
})
|
||||
const memory = new WebAssembly.Memory({
|
||||
initial: 16777216 / 65536,
|
||||
maximum: 2147483648 / 65536,
|
||||
shared: true
|
||||
})
|
||||
let input
|
||||
const file = 'path/to/your/wasi-module.wasm'
|
||||
try {
|
||||
input = require('fs').readFileSync(require('path').join(__dirname, file))
|
||||
} catch (err) {
|
||||
const response = await fetch(file)
|
||||
input = await response.arrayBuffer()
|
||||
}
|
||||
let { module, instance } = await WebAssembly.instantiate(input, {
|
||||
env: { memory },
|
||||
wasi_snapshot_preview1: wasi.wasiImport,
|
||||
...wasiThreads.getImportObject()
|
||||
})
|
||||
|
||||
wasiThreads.setup(instance, module, memory)
|
||||
await wasiThreads.preloadWorkers()
|
||||
|
||||
if (typeof instance.exports._start === 'function') {
|
||||
return wasi.start(instance)
|
||||
} else {
|
||||
wasi.initialize(instance)
|
||||
// instance.exports.exported_wasm_function()
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
`worker.js`
|
||||
|
||||
```js
|
||||
(function (main) {
|
||||
const ENVIRONMENT_IS_NODE =
|
||||
typeof process === 'object' && process !== null &&
|
||||
typeof process.versions === 'object' && process.versions !== null &&
|
||||
typeof process.versions.node === 'string'
|
||||
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
const _require = function (request) {
|
||||
return require(request)
|
||||
}
|
||||
|
||||
const _init = function () {
|
||||
const nodeWorkerThreads = require('worker_threads')
|
||||
const parentPort = nodeWorkerThreads.parentPort
|
||||
|
||||
parentPort.on('message', (data) => {
|
||||
globalThis.onmessage({ data })
|
||||
})
|
||||
|
||||
Object.assign(globalThis, {
|
||||
self: globalThis,
|
||||
require,
|
||||
Worker: nodeWorkerThreads.Worker,
|
||||
importScripts: function (f) {
|
||||
(0, eval)(require('fs').readFileSync(f, 'utf8') + '//# sourceURL=' + f)
|
||||
},
|
||||
postMessage: function (msg) {
|
||||
parentPort.postMessage(msg)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
main(_require, _init)
|
||||
} else {
|
||||
importScripts('./node_modules/@tybys/wasm-util/dist/wasm-util.js')
|
||||
importScripts('./node_modules/@emnapi/wasi-threads/dist/wasi-threads.js')
|
||||
|
||||
const nodeWasi = { WASI: globalThis.wasmUtil.WASI }
|
||||
const _require = function (request) {
|
||||
if (request === '@emnapi/wasi-threads') return globalThis.wasiThreads
|
||||
if (request === 'node:wasi' || request === 'wasi') return nodeWasi
|
||||
throw new Error('Can not find module: ' + request)
|
||||
}
|
||||
const _init = function () {}
|
||||
main(_require, _init)
|
||||
}
|
||||
})(function main (require, init) {
|
||||
init()
|
||||
|
||||
const { WASI } = require('wasi')
|
||||
const { ThreadMessageHandler, WASIThreads } = require('@emnapi/wasi-threads')
|
||||
|
||||
const handler = new ThreadMessageHandler({
|
||||
async onLoad ({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({
|
||||
version: 'preview1'
|
||||
})
|
||||
|
||||
const wasiThreads = new WASIThreads({
|
||||
wasi,
|
||||
childThread: true
|
||||
})
|
||||
|
||||
const originalInstance = await WebAssembly.instantiate(wasmModule, {
|
||||
env: {
|
||||
memory: wasmMemory,
|
||||
},
|
||||
wasi_snapshot_preview1: wasi.wasiImport,
|
||||
...wasiThreads.getImportObject()
|
||||
})
|
||||
|
||||
// must call `initialize` instead of `start` in child thread
|
||||
const instance = wasiThreads.initialize(originalInstance, wasmModule, wasmMemory)
|
||||
|
||||
return { module: wasmModule, instance }
|
||||
}
|
||||
})
|
||||
|
||||
globalThis.onmessage = function (e) {
|
||||
handler.handle(e)
|
||||
// handle other messages
|
||||
}
|
||||
})
|
||||
```
|
||||
897
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.js
generated
vendored
Normal file
897
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.js
generated
vendored
Normal file
|
|
@ -0,0 +1,897 @@
|
|||
const _WebAssembly = typeof WebAssembly !== 'undefined'
|
||||
? WebAssembly
|
||||
: typeof WXWebAssembly !== 'undefined'
|
||||
? WXWebAssembly
|
||||
: undefined;
|
||||
const ENVIRONMENT_IS_NODE = typeof process === 'object' && process !== null &&
|
||||
typeof process.versions === 'object' && process.versions !== null &&
|
||||
typeof process.versions.node === 'string';
|
||||
function getPostMessage(options) {
|
||||
return typeof (options === null || options === void 0 ? void 0 : options.postMessage) === 'function'
|
||||
? options.postMessage
|
||||
: typeof postMessage === 'function'
|
||||
? postMessage
|
||||
: undefined;
|
||||
}
|
||||
function serizeErrorToBuffer(sab, code, error) {
|
||||
const i32array = new Int32Array(sab);
|
||||
Atomics.store(i32array, 0, code);
|
||||
if (code > 1 && error) {
|
||||
const name = error.name;
|
||||
const message = error.message;
|
||||
const stack = error.stack;
|
||||
const nameBuffer = new TextEncoder().encode(name);
|
||||
const messageBuffer = new TextEncoder().encode(message);
|
||||
const stackBuffer = new TextEncoder().encode(stack);
|
||||
Atomics.store(i32array, 1, nameBuffer.length);
|
||||
Atomics.store(i32array, 2, messageBuffer.length);
|
||||
Atomics.store(i32array, 3, stackBuffer.length);
|
||||
const buffer = new Uint8Array(sab);
|
||||
buffer.set(nameBuffer, 16);
|
||||
buffer.set(messageBuffer, 16 + nameBuffer.length);
|
||||
buffer.set(stackBuffer, 16 + nameBuffer.length + messageBuffer.length);
|
||||
}
|
||||
}
|
||||
function deserizeErrorFromBuffer(sab) {
|
||||
var _a, _b;
|
||||
const i32array = new Int32Array(sab);
|
||||
const status = Atomics.load(i32array, 0);
|
||||
if (status <= 1) {
|
||||
return null;
|
||||
}
|
||||
const nameLength = Atomics.load(i32array, 1);
|
||||
const messageLength = Atomics.load(i32array, 2);
|
||||
const stackLength = Atomics.load(i32array, 3);
|
||||
const buffer = new Uint8Array(sab);
|
||||
const nameBuffer = buffer.slice(16, 16 + nameLength);
|
||||
const messageBuffer = buffer.slice(16 + nameLength, 16 + nameLength + messageLength);
|
||||
const stackBuffer = buffer.slice(16 + nameLength + messageLength, 16 + nameLength + messageLength + stackLength);
|
||||
const name = new TextDecoder().decode(nameBuffer);
|
||||
const message = new TextDecoder().decode(messageBuffer);
|
||||
const stack = new TextDecoder().decode(stackBuffer);
|
||||
const ErrorConstructor = (_a = globalThis[name]) !== null && _a !== void 0 ? _a : (name === 'RuntimeError' ? ((_b = _WebAssembly.RuntimeError) !== null && _b !== void 0 ? _b : Error) : Error);
|
||||
const error = new ErrorConstructor(message);
|
||||
Object.defineProperty(error, 'stack', {
|
||||
value: stack,
|
||||
writable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
return error;
|
||||
}
|
||||
function isSharedArrayBuffer(value) {
|
||||
return ((typeof SharedArrayBuffer === 'function' && value instanceof SharedArrayBuffer) ||
|
||||
(Object.prototype.toString.call(value) === '[object SharedArrayBuffer]'));
|
||||
}
|
||||
function isTrapError(e) {
|
||||
try {
|
||||
return e instanceof _WebAssembly.RuntimeError;
|
||||
}
|
||||
catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function createMessage(type, payload) {
|
||||
return {
|
||||
__emnapi__: {
|
||||
type,
|
||||
payload
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const WASI_THREADS_MAX_TID = 0x1FFFFFFF;
|
||||
function checkSharedWasmMemory(wasmMemory) {
|
||||
if (wasmMemory) {
|
||||
if (!isSharedArrayBuffer(wasmMemory.buffer)) {
|
||||
throw new Error('Multithread features require shared wasm memory. ' +
|
||||
'Try to compile with `-matomics -mbulk-memory` and use `--import-memory --shared-memory` during linking, ' +
|
||||
'then create WebAssembly.Memory with `shared: true` option');
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (typeof SharedArrayBuffer === 'undefined') {
|
||||
throw new Error('Current environment does not support SharedArrayBuffer, threads are not available!');
|
||||
}
|
||||
}
|
||||
}
|
||||
function getReuseWorker(value) {
|
||||
var _a;
|
||||
if (typeof value === 'boolean') {
|
||||
return value ? { size: 0, strict: false } : false;
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
if (!(value >= 0)) {
|
||||
throw new RangeError('reuseWorker: size must be a non-negative integer');
|
||||
}
|
||||
return { size: value, strict: false };
|
||||
}
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
const size = (_a = Number(value.size)) !== null && _a !== void 0 ? _a : 0;
|
||||
const strict = Boolean(value.strict);
|
||||
if (!(size > 0) && strict) {
|
||||
throw new RangeError('reuseWorker: size must be set to positive integer if strict is set to true');
|
||||
}
|
||||
return { size, strict };
|
||||
}
|
||||
let nextWorkerID = 0;
|
||||
class ThreadManager {
|
||||
get nextWorkerID() { return nextWorkerID; }
|
||||
constructor(options) {
|
||||
var _a;
|
||||
this.unusedWorkers = [];
|
||||
this.runningWorkers = [];
|
||||
this.pthreads = Object.create(null);
|
||||
this.wasmModule = null;
|
||||
this.wasmMemory = null;
|
||||
this.messageEvents = new WeakMap();
|
||||
if (!options) {
|
||||
throw new TypeError('ThreadManager(): options is not provided');
|
||||
}
|
||||
if ('childThread' in options) {
|
||||
this._childThread = Boolean(options.childThread);
|
||||
}
|
||||
else {
|
||||
this._childThread = false;
|
||||
}
|
||||
if (this._childThread) {
|
||||
this._onCreateWorker = undefined;
|
||||
this._reuseWorker = false;
|
||||
this._beforeLoad = undefined;
|
||||
}
|
||||
else {
|
||||
this._onCreateWorker = options.onCreateWorker;
|
||||
this._reuseWorker = getReuseWorker(options.reuseWorker);
|
||||
this._beforeLoad = options.beforeLoad;
|
||||
}
|
||||
this.printErr = (_a = options.printErr) !== null && _a !== void 0 ? _a : console.error.bind(console);
|
||||
}
|
||||
init() {
|
||||
if (!this._childThread) {
|
||||
this.initMainThread();
|
||||
}
|
||||
}
|
||||
initMainThread() {
|
||||
this.preparePool();
|
||||
}
|
||||
preparePool() {
|
||||
if (this._reuseWorker) {
|
||||
if (this._reuseWorker.size) {
|
||||
let pthreadPoolSize = this._reuseWorker.size;
|
||||
while (pthreadPoolSize--) {
|
||||
const worker = this.allocateUnusedWorker();
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.once('message', () => { });
|
||||
worker.unref();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
shouldPreloadWorkers() {
|
||||
return !this._childThread && this._reuseWorker && this._reuseWorker.size > 0;
|
||||
}
|
||||
loadWasmModuleToAllWorkers() {
|
||||
const promises = Array(this.unusedWorkers.length);
|
||||
for (let i = 0; i < this.unusedWorkers.length; ++i) {
|
||||
const worker = this.unusedWorkers[i];
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.ref();
|
||||
promises[i] = this.loadWasmModuleToWorker(worker).then((w) => {
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.unref();
|
||||
return w;
|
||||
}, (e) => {
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.unref();
|
||||
throw e;
|
||||
});
|
||||
}
|
||||
return Promise.all(promises).catch((err) => {
|
||||
this.terminateAllThreads();
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
preloadWorkers() {
|
||||
if (this.shouldPreloadWorkers()) {
|
||||
return this.loadWasmModuleToAllWorkers();
|
||||
}
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
setup(wasmModule, wasmMemory) {
|
||||
this.wasmModule = wasmModule;
|
||||
this.wasmMemory = wasmMemory;
|
||||
}
|
||||
markId(worker) {
|
||||
if (worker.__emnapi_tid)
|
||||
return worker.__emnapi_tid;
|
||||
const tid = nextWorkerID + 43;
|
||||
nextWorkerID = (nextWorkerID + 1) % (WASI_THREADS_MAX_TID - 42);
|
||||
this.pthreads[tid] = worker;
|
||||
worker.__emnapi_tid = tid;
|
||||
return tid;
|
||||
}
|
||||
returnWorkerToPool(worker) {
|
||||
var tid = worker.__emnapi_tid;
|
||||
if (tid !== undefined) {
|
||||
delete this.pthreads[tid];
|
||||
}
|
||||
this.unusedWorkers.push(worker);
|
||||
this.runningWorkers.splice(this.runningWorkers.indexOf(worker), 1);
|
||||
delete worker.__emnapi_tid;
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.unref();
|
||||
}
|
||||
}
|
||||
loadWasmModuleToWorker(worker, sab) {
|
||||
if (worker.whenLoaded)
|
||||
return worker.whenLoaded;
|
||||
const err = this.printErr;
|
||||
const beforeLoad = this._beforeLoad;
|
||||
const _this = this;
|
||||
worker.whenLoaded = new Promise((resolve, reject) => {
|
||||
const handleError = function (e) {
|
||||
let message = 'worker sent an error!';
|
||||
if (worker.__emnapi_tid !== undefined) {
|
||||
message = 'worker (tid = ' + worker.__emnapi_tid + ') sent an error!';
|
||||
}
|
||||
if ('message' in e) {
|
||||
err(message + ' ' + e.message);
|
||||
if (e.message.indexOf('RuntimeError') !== -1 || e.message.indexOf('unreachable') !== -1) {
|
||||
try {
|
||||
_this.terminateAllThreads();
|
||||
}
|
||||
catch (_) { }
|
||||
}
|
||||
}
|
||||
else {
|
||||
err(message);
|
||||
}
|
||||
reject(e);
|
||||
throw e;
|
||||
};
|
||||
const handleMessage = (data) => {
|
||||
if (data.__emnapi__) {
|
||||
const type = data.__emnapi__.type;
|
||||
const payload = data.__emnapi__.payload;
|
||||
if (type === 'loaded') {
|
||||
worker.loaded = true;
|
||||
if (ENVIRONMENT_IS_NODE && !worker.__emnapi_tid) {
|
||||
worker.unref();
|
||||
}
|
||||
resolve(worker);
|
||||
}
|
||||
else if (type === 'cleanup-thread') {
|
||||
if (payload.tid in this.pthreads) {
|
||||
this.cleanThread(worker, payload.tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
worker.onmessage = (e) => {
|
||||
handleMessage(e.data);
|
||||
this.fireMessageEvent(worker, e);
|
||||
};
|
||||
worker.onerror = handleError;
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.on('message', function (data) {
|
||||
var _a, _b;
|
||||
(_b = (_a = worker).onmessage) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
||||
data
|
||||
});
|
||||
});
|
||||
worker.on('error', function (e) {
|
||||
var _a, _b;
|
||||
(_b = (_a = worker).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, e);
|
||||
});
|
||||
worker.on('detachedExit', function () { });
|
||||
}
|
||||
if (typeof beforeLoad === 'function') {
|
||||
beforeLoad(worker);
|
||||
}
|
||||
try {
|
||||
worker.postMessage(createMessage('load', {
|
||||
wasmModule: this.wasmModule,
|
||||
wasmMemory: this.wasmMemory,
|
||||
sab
|
||||
}));
|
||||
}
|
||||
catch (err) {
|
||||
checkSharedWasmMemory(this.wasmMemory);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
return worker.whenLoaded;
|
||||
}
|
||||
allocateUnusedWorker() {
|
||||
const _onCreateWorker = this._onCreateWorker;
|
||||
if (typeof _onCreateWorker !== 'function') {
|
||||
throw new TypeError('`options.onCreateWorker` is not provided');
|
||||
}
|
||||
const worker = _onCreateWorker({ type: 'thread', name: 'emnapi-pthread' });
|
||||
this.unusedWorkers.push(worker);
|
||||
return worker;
|
||||
}
|
||||
getNewWorker(sab) {
|
||||
if (this._reuseWorker) {
|
||||
if (this.unusedWorkers.length === 0) {
|
||||
if (this._reuseWorker.strict) {
|
||||
if (!ENVIRONMENT_IS_NODE) {
|
||||
const err = this.printErr;
|
||||
err('Tried to spawn a new thread, but the thread pool is exhausted.\n' +
|
||||
'This might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
const worker = this.allocateUnusedWorker();
|
||||
this.loadWasmModuleToWorker(worker, sab);
|
||||
}
|
||||
return this.unusedWorkers.pop();
|
||||
}
|
||||
const worker = this.allocateUnusedWorker();
|
||||
this.loadWasmModuleToWorker(worker, sab);
|
||||
return this.unusedWorkers.pop();
|
||||
}
|
||||
cleanThread(worker, tid, force) {
|
||||
if (!force && this._reuseWorker) {
|
||||
this.returnWorkerToPool(worker);
|
||||
}
|
||||
else {
|
||||
delete this.pthreads[tid];
|
||||
const index = this.runningWorkers.indexOf(worker);
|
||||
if (index !== -1) {
|
||||
this.runningWorkers.splice(index, 1);
|
||||
}
|
||||
this.terminateWorker(worker);
|
||||
delete worker.__emnapi_tid;
|
||||
}
|
||||
}
|
||||
terminateWorker(worker) {
|
||||
var _a;
|
||||
const tid = worker.__emnapi_tid;
|
||||
worker.terminate();
|
||||
(_a = this.messageEvents.get(worker)) === null || _a === void 0 ? void 0 : _a.clear();
|
||||
this.messageEvents.delete(worker);
|
||||
worker.onmessage = (e) => {
|
||||
if (e.data.__emnapi__) {
|
||||
const err = this.printErr;
|
||||
err('received "' + e.data.__emnapi__.type + '" command from terminated worker: ' + tid);
|
||||
}
|
||||
};
|
||||
}
|
||||
terminateAllThreads() {
|
||||
for (let i = 0; i < this.runningWorkers.length; ++i) {
|
||||
this.terminateWorker(this.runningWorkers[i]);
|
||||
}
|
||||
for (let i = 0; i < this.unusedWorkers.length; ++i) {
|
||||
this.terminateWorker(this.unusedWorkers[i]);
|
||||
}
|
||||
this.unusedWorkers = [];
|
||||
this.runningWorkers = [];
|
||||
this.pthreads = Object.create(null);
|
||||
this.preparePool();
|
||||
}
|
||||
addMessageEventListener(worker, onMessage) {
|
||||
let listeners = this.messageEvents.get(worker);
|
||||
if (!listeners) {
|
||||
listeners = new Set();
|
||||
this.messageEvents.set(worker, listeners);
|
||||
}
|
||||
listeners.add(onMessage);
|
||||
return () => {
|
||||
listeners === null || listeners === void 0 ? void 0 : listeners.delete(onMessage);
|
||||
};
|
||||
}
|
||||
fireMessageEvent(worker, e) {
|
||||
const listeners = this.messageEvents.get(worker);
|
||||
if (!listeners)
|
||||
return;
|
||||
const err = this.printErr;
|
||||
listeners.forEach((listener) => {
|
||||
try {
|
||||
listener(e);
|
||||
}
|
||||
catch (e) {
|
||||
err(e.stack);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const kIsProxy = Symbol('kIsProxy');
|
||||
function createInstanceProxy(instance, memory) {
|
||||
if (instance[kIsProxy])
|
||||
return instance;
|
||||
const originalExports = instance.exports;
|
||||
const createHandler = function (target) {
|
||||
const handlers = [
|
||||
'apply',
|
||||
'construct',
|
||||
'defineProperty',
|
||||
'deleteProperty',
|
||||
'get',
|
||||
'getOwnPropertyDescriptor',
|
||||
'getPrototypeOf',
|
||||
'has',
|
||||
'isExtensible',
|
||||
'ownKeys',
|
||||
'preventExtensions',
|
||||
'set',
|
||||
'setPrototypeOf'
|
||||
];
|
||||
const handler = {};
|
||||
for (let i = 0; i < handlers.length; i++) {
|
||||
const name = handlers[i];
|
||||
handler[name] = function () {
|
||||
const args = Array.prototype.slice.call(arguments, 1);
|
||||
args.unshift(target);
|
||||
return Reflect[name].apply(Reflect, args);
|
||||
};
|
||||
}
|
||||
return handler;
|
||||
};
|
||||
const handler = createHandler(originalExports);
|
||||
const _initialize = () => { };
|
||||
const _start = () => 0;
|
||||
handler.get = function (_target, p, receiver) {
|
||||
var _a;
|
||||
if (p === 'memory') {
|
||||
return (_a = (typeof memory === 'function' ? memory() : memory)) !== null && _a !== void 0 ? _a : Reflect.get(originalExports, p, receiver);
|
||||
}
|
||||
if (p === '_initialize') {
|
||||
return p in originalExports ? _initialize : undefined;
|
||||
}
|
||||
if (p === '_start') {
|
||||
return p in originalExports ? _start : undefined;
|
||||
}
|
||||
return Reflect.get(originalExports, p, receiver);
|
||||
};
|
||||
handler.has = function (_target, p) {
|
||||
if (p === 'memory')
|
||||
return true;
|
||||
return Reflect.has(originalExports, p);
|
||||
};
|
||||
const exportsProxy = new Proxy(Object.create(null), handler);
|
||||
return new Proxy(instance, {
|
||||
get(target, p, receiver) {
|
||||
if (p === 'exports') {
|
||||
return exportsProxy;
|
||||
}
|
||||
if (p === kIsProxy) {
|
||||
return true;
|
||||
}
|
||||
return Reflect.get(target, p, receiver);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const patchedWasiInstances = new WeakMap();
|
||||
class WASIThreads {
|
||||
constructor(options) {
|
||||
if (!options) {
|
||||
throw new TypeError('WASIThreads(): options is not provided');
|
||||
}
|
||||
if (!options.wasi) {
|
||||
throw new TypeError('WASIThreads(): options.wasi is not provided');
|
||||
}
|
||||
patchedWasiInstances.set(this, new WeakSet());
|
||||
const wasi = options.wasi;
|
||||
patchWasiInstance(this, wasi);
|
||||
this.wasi = wasi;
|
||||
if ('childThread' in options) {
|
||||
this.childThread = Boolean(options.childThread);
|
||||
}
|
||||
else {
|
||||
this.childThread = false;
|
||||
}
|
||||
this.PThread = undefined;
|
||||
if ('threadManager' in options) {
|
||||
if (typeof options.threadManager === 'function') {
|
||||
this.PThread = options.threadManager();
|
||||
}
|
||||
else {
|
||||
this.PThread = options.threadManager;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!this.childThread) {
|
||||
this.PThread = new ThreadManager(options);
|
||||
this.PThread.init();
|
||||
}
|
||||
}
|
||||
let waitThreadStart = false;
|
||||
if ('waitThreadStart' in options) {
|
||||
waitThreadStart = typeof options.waitThreadStart === 'number' ? options.waitThreadStart : Boolean(options.waitThreadStart);
|
||||
}
|
||||
const postMessage = getPostMessage(options);
|
||||
if (this.childThread && typeof postMessage !== 'function') {
|
||||
throw new TypeError('options.postMessage is not a function');
|
||||
}
|
||||
this.postMessage = postMessage;
|
||||
const wasm64 = Boolean(options.wasm64);
|
||||
const onMessage = (e) => {
|
||||
if (e.data.__emnapi__) {
|
||||
const type = e.data.__emnapi__.type;
|
||||
const payload = e.data.__emnapi__.payload;
|
||||
if (type === 'spawn-thread') {
|
||||
threadSpawn(payload.startArg, payload.errorOrTid);
|
||||
}
|
||||
else if (type === 'terminate-all-threads') {
|
||||
this.terminateAllThreads();
|
||||
}
|
||||
}
|
||||
};
|
||||
const threadSpawn = (startArg, errorOrTid) => {
|
||||
var _a;
|
||||
const EAGAIN = 6;
|
||||
const isNewABI = errorOrTid !== undefined;
|
||||
try {
|
||||
checkSharedWasmMemory(this.wasmMemory);
|
||||
}
|
||||
catch (err) {
|
||||
(_a = this.PThread) === null || _a === void 0 ? void 0 : _a.printErr(err.stack);
|
||||
if (isNewABI) {
|
||||
const struct = new Int32Array(this.wasmMemory.buffer, errorOrTid, 2);
|
||||
Atomics.store(struct, 0, 1);
|
||||
Atomics.store(struct, 1, EAGAIN);
|
||||
Atomics.notify(struct, 1);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return -EAGAIN;
|
||||
}
|
||||
}
|
||||
if (!isNewABI) {
|
||||
const malloc = this.wasmInstance.exports.malloc;
|
||||
errorOrTid = wasm64 ? Number(malloc(BigInt(8))) : malloc(8);
|
||||
if (!errorOrTid) {
|
||||
return -48;
|
||||
}
|
||||
}
|
||||
const _free = this.wasmInstance.exports.free;
|
||||
const free = wasm64 ? (ptr) => { _free(BigInt(ptr)); } : _free;
|
||||
const struct = new Int32Array(this.wasmMemory.buffer, errorOrTid, 2);
|
||||
Atomics.store(struct, 0, 0);
|
||||
Atomics.store(struct, 1, 0);
|
||||
if (this.childThread) {
|
||||
postMessage(createMessage('spawn-thread', {
|
||||
startArg,
|
||||
errorOrTid: errorOrTid
|
||||
}));
|
||||
Atomics.wait(struct, 1, 0);
|
||||
const isError = Atomics.load(struct, 0);
|
||||
const result = Atomics.load(struct, 1);
|
||||
if (isNewABI) {
|
||||
return isError;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return isError ? -result : result;
|
||||
}
|
||||
const shouldWait = waitThreadStart || (waitThreadStart === 0);
|
||||
let sab;
|
||||
if (shouldWait) {
|
||||
sab = new Int32Array(new SharedArrayBuffer(16 + 8192));
|
||||
Atomics.store(sab, 0, 0);
|
||||
}
|
||||
let worker;
|
||||
let tid;
|
||||
const PThread = this.PThread;
|
||||
try {
|
||||
worker = PThread.getNewWorker(sab);
|
||||
if (!worker) {
|
||||
throw new Error('failed to get new worker');
|
||||
}
|
||||
PThread.addMessageEventListener(worker, onMessage);
|
||||
tid = PThread.markId(worker);
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.ref();
|
||||
}
|
||||
worker.postMessage(createMessage('start', {
|
||||
tid,
|
||||
arg: startArg,
|
||||
sab
|
||||
}));
|
||||
if (shouldWait) {
|
||||
if (typeof waitThreadStart === 'number') {
|
||||
const waitResult = Atomics.wait(sab, 0, 0, waitThreadStart);
|
||||
if (waitResult === 'timed-out') {
|
||||
try {
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
}
|
||||
catch (_) { }
|
||||
throw new Error('Spawning thread timed out. Please check if the worker is created successfully and if message is handled properly in the worker.');
|
||||
}
|
||||
}
|
||||
else {
|
||||
Atomics.wait(sab, 0, 0);
|
||||
}
|
||||
const r = Atomics.load(sab, 0);
|
||||
if (r > 1) {
|
||||
try {
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
}
|
||||
catch (_) { }
|
||||
throw deserizeErrorFromBuffer(sab.buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
Atomics.store(struct, 0, 1);
|
||||
Atomics.store(struct, 1, EAGAIN);
|
||||
Atomics.notify(struct, 1);
|
||||
PThread === null || PThread === void 0 ? void 0 : PThread.printErr(e.stack);
|
||||
if (isNewABI) {
|
||||
return 1;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return -EAGAIN;
|
||||
}
|
||||
Atomics.store(struct, 0, 0);
|
||||
Atomics.store(struct, 1, tid);
|
||||
Atomics.notify(struct, 1);
|
||||
PThread.runningWorkers.push(worker);
|
||||
if (!shouldWait) {
|
||||
worker.whenLoaded.catch((err) => {
|
||||
delete worker.whenLoaded;
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
if (isNewABI) {
|
||||
return 0;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return tid;
|
||||
};
|
||||
this.threadSpawn = threadSpawn;
|
||||
}
|
||||
getImportObject() {
|
||||
return {
|
||||
wasi: {
|
||||
'thread-spawn': this.threadSpawn
|
||||
}
|
||||
};
|
||||
}
|
||||
setup(wasmInstance, wasmModule, wasmMemory) {
|
||||
wasmMemory !== null && wasmMemory !== void 0 ? wasmMemory : (wasmMemory = wasmInstance.exports.memory);
|
||||
this.wasmInstance = wasmInstance;
|
||||
this.wasmMemory = wasmMemory;
|
||||
if (this.PThread) {
|
||||
this.PThread.setup(wasmModule, wasmMemory);
|
||||
}
|
||||
}
|
||||
preloadWorkers() {
|
||||
if (this.PThread) {
|
||||
return this.PThread.preloadWorkers();
|
||||
}
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
initialize(instance, module, memory) {
|
||||
const exports = instance.exports;
|
||||
memory !== null && memory !== void 0 ? memory : (memory = exports.memory);
|
||||
if (this.childThread) {
|
||||
instance = createInstanceProxy(instance, memory);
|
||||
}
|
||||
this.setup(instance, module, memory);
|
||||
const wasi = this.wasi;
|
||||
if (('_start' in exports) && (typeof exports._start === 'function')) {
|
||||
if (this.childThread) {
|
||||
wasi.start(instance);
|
||||
try {
|
||||
const kStarted = getWasiSymbol(wasi, 'kStarted');
|
||||
wasi[kStarted] = false;
|
||||
}
|
||||
catch (_) { }
|
||||
}
|
||||
else {
|
||||
setupInstance(wasi, instance);
|
||||
}
|
||||
}
|
||||
else {
|
||||
wasi.initialize(instance);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
start(instance, module, memory) {
|
||||
const exports = instance.exports;
|
||||
memory !== null && memory !== void 0 ? memory : (memory = exports.memory);
|
||||
if (this.childThread) {
|
||||
instance = createInstanceProxy(instance, memory);
|
||||
}
|
||||
this.setup(instance, module, memory);
|
||||
const exitCode = this.wasi.start(instance);
|
||||
return { exitCode, instance };
|
||||
}
|
||||
terminateAllThreads() {
|
||||
var _a;
|
||||
if (!this.childThread) {
|
||||
(_a = this.PThread) === null || _a === void 0 ? void 0 : _a.terminateAllThreads();
|
||||
}
|
||||
else {
|
||||
this.postMessage(createMessage('terminate-all-threads', {}));
|
||||
}
|
||||
}
|
||||
}
|
||||
function patchWasiInstance(wasiThreads, wasi) {
|
||||
const patched = patchedWasiInstances.get(wasiThreads);
|
||||
if (patched.has(wasi)) {
|
||||
return;
|
||||
}
|
||||
const _this = wasiThreads;
|
||||
const wasiImport = wasi.wasiImport;
|
||||
if (wasiImport) {
|
||||
const proc_exit = wasiImport.proc_exit;
|
||||
wasiImport.proc_exit = function (code) {
|
||||
_this.terminateAllThreads();
|
||||
return proc_exit.call(this, code);
|
||||
};
|
||||
}
|
||||
if (!_this.childThread) {
|
||||
const start = wasi.start;
|
||||
if (typeof start === 'function') {
|
||||
wasi.start = function (instance) {
|
||||
try {
|
||||
return start.call(this, instance);
|
||||
}
|
||||
catch (err) {
|
||||
if (isTrapError(err)) {
|
||||
_this.terminateAllThreads();
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
patched.add(wasi);
|
||||
}
|
||||
function getWasiSymbol(wasi, description) {
|
||||
const symbols = Object.getOwnPropertySymbols(wasi);
|
||||
const selectDescription = (description) => (s) => {
|
||||
if (s.description) {
|
||||
return s.description === description;
|
||||
}
|
||||
return s.toString() === `Symbol(${description})`;
|
||||
};
|
||||
if (Array.isArray(description)) {
|
||||
return description.map(d => symbols.filter(selectDescription(d))[0]);
|
||||
}
|
||||
return symbols.filter(selectDescription(description))[0];
|
||||
}
|
||||
function setupInstance(wasi, instance) {
|
||||
const [kInstance, kSetMemory] = getWasiSymbol(wasi, ['kInstance', 'kSetMemory']);
|
||||
wasi[kInstance] = instance;
|
||||
wasi[kSetMemory](instance.exports.memory);
|
||||
}
|
||||
|
||||
class ThreadMessageHandler {
|
||||
constructor(options) {
|
||||
const postMsg = getPostMessage(options);
|
||||
if (typeof postMsg !== 'function') {
|
||||
throw new TypeError('options.postMessage is not a function');
|
||||
}
|
||||
this.postMessage = postMsg;
|
||||
this.onLoad = options === null || options === void 0 ? void 0 : options.onLoad;
|
||||
this.onError = typeof (options === null || options === void 0 ? void 0 : options.onError) === 'function' ? options.onError : (_type, err) => { throw err; };
|
||||
this.instance = undefined;
|
||||
this.messagesBeforeLoad = [];
|
||||
}
|
||||
instantiate(data) {
|
||||
if (typeof this.onLoad === 'function') {
|
||||
return this.onLoad(data);
|
||||
}
|
||||
throw new Error('ThreadMessageHandler.prototype.instantiate is not implemented');
|
||||
}
|
||||
handle(e) {
|
||||
var _a;
|
||||
if ((_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.__emnapi__) {
|
||||
const type = e.data.__emnapi__.type;
|
||||
const payload = e.data.__emnapi__.payload;
|
||||
try {
|
||||
if (type === 'load') {
|
||||
this._load(payload);
|
||||
}
|
||||
else if (type === 'start') {
|
||||
this.handleAfterLoad(e, () => {
|
||||
this._start(payload);
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
this.onError(err, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
_load(payload) {
|
||||
if (this.instance !== undefined)
|
||||
return;
|
||||
let source;
|
||||
try {
|
||||
source = this.instantiate(payload);
|
||||
}
|
||||
catch (err) {
|
||||
this._loaded(err, null, payload);
|
||||
return;
|
||||
}
|
||||
const then = source && 'then' in source ? source.then : undefined;
|
||||
if (typeof then === 'function') {
|
||||
then.call(source, (source) => { this._loaded(null, source, payload); }, (err) => { this._loaded(err, null, payload); });
|
||||
}
|
||||
else {
|
||||
this._loaded(null, source, payload);
|
||||
}
|
||||
}
|
||||
_start(payload) {
|
||||
const wasi_thread_start = this.instance.exports.wasi_thread_start;
|
||||
if (typeof wasi_thread_start !== 'function') {
|
||||
const err = new TypeError('wasi_thread_start is not exported');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
const postMessage = this.postMessage;
|
||||
const tid = payload.tid;
|
||||
const startArg = payload.arg;
|
||||
notifyPthreadCreateResult(payload.sab, 1);
|
||||
try {
|
||||
wasi_thread_start(tid, startArg);
|
||||
}
|
||||
catch (err) {
|
||||
if (err !== 'unwind') {
|
||||
throw err;
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
postMessage(createMessage('cleanup-thread', { tid }));
|
||||
}
|
||||
_loaded(err, source, payload) {
|
||||
if (err) {
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
if (source == null) {
|
||||
const err = new TypeError('onLoad should return an object');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
const instance = source.instance;
|
||||
if (!instance) {
|
||||
const err = new TypeError('onLoad should return an object which includes "instance"');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
this.instance = instance;
|
||||
const postMessage = this.postMessage;
|
||||
postMessage(createMessage('loaded', {}));
|
||||
const messages = this.messagesBeforeLoad;
|
||||
this.messagesBeforeLoad = [];
|
||||
for (let i = 0; i < messages.length; i++) {
|
||||
const data = messages[i];
|
||||
this.handle({ data });
|
||||
}
|
||||
}
|
||||
handleAfterLoad(e, f) {
|
||||
if (this.instance !== undefined) {
|
||||
f.call(this, e);
|
||||
}
|
||||
else {
|
||||
this.messagesBeforeLoad.push(e.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
function notifyPthreadCreateResult(sab, result, error) {
|
||||
if (sab) {
|
||||
serizeErrorToBuffer(sab.buffer, result, error);
|
||||
Atomics.notify(sab, 0);
|
||||
}
|
||||
}
|
||||
|
||||
exports.ThreadManager = ThreadManager;
|
||||
exports.ThreadMessageHandler = ThreadMessageHandler;
|
||||
exports.WASIThreads = WASIThreads;
|
||||
exports.createInstanceProxy = createInstanceProxy;
|
||||
exports.isSharedArrayBuffer = isSharedArrayBuffer;
|
||||
exports.isTrapError = isTrapError;
|
||||
270
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.d.ts
generated
vendored
Normal file
270
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.d.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,270 @@
|
|||
/// <reference types="node" />
|
||||
|
||||
import type { Worker as Worker_2 } from 'worker_threads';
|
||||
|
||||
/** @public */
|
||||
export declare interface BaseOptions {
|
||||
wasi: WASIInstance;
|
||||
version?: 'preview1';
|
||||
wasm64?: boolean;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ChildThreadOptions extends BaseOptions {
|
||||
childThread: true;
|
||||
postMessage?: (data: any) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CleanupThreadPayload {
|
||||
tid: number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CommandInfo<T extends CommandType> {
|
||||
type: T;
|
||||
payload: CommandPayloadMap[T];
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CommandPayloadMap {
|
||||
load: LoadPayload;
|
||||
loaded: LoadedPayload;
|
||||
start: StartPayload;
|
||||
'cleanup-thread': CleanupThreadPayload;
|
||||
'terminate-all-threads': TerminateAllThreadsPayload;
|
||||
'spawn-thread': SpawnThreadPayload;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type CommandType = keyof CommandPayloadMap;
|
||||
|
||||
/** @public */
|
||||
export declare function createInstanceProxy(instance: WebAssembly.Instance, memory?: WebAssembly.Memory | (() => WebAssembly.Memory)): WebAssembly.Instance;
|
||||
|
||||
/** @public */
|
||||
export declare function isSharedArrayBuffer(value: any): value is SharedArrayBuffer;
|
||||
|
||||
/** @public */
|
||||
export declare function isTrapError(e: Error): e is WebAssembly.RuntimeError;
|
||||
|
||||
/** @public */
|
||||
export declare interface LoadedPayload {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface LoadPayload {
|
||||
wasmModule: WebAssembly.Module;
|
||||
wasmMemory: WebAssembly.Memory;
|
||||
sab?: Int32Array;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadBaseOptions extends BaseOptions {
|
||||
waitThreadStart?: boolean | number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type MainThreadOptions = MainThreadOptionsWithThreadManager | MainThreadOptionsCreateThreadManager;
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadOptionsCreateThreadManager extends MainThreadBaseOptions, ThreadManagerOptionsMain {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadOptionsWithThreadManager extends MainThreadBaseOptions {
|
||||
threadManager?: ThreadManager | (() => ThreadManager);
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MessageEventData<T extends CommandType> {
|
||||
__emnapi__: CommandInfo<T>;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ReuseWorkerOptions {
|
||||
/**
|
||||
* @see {@link https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size | PTHREAD_POOL_SIZE}
|
||||
*/
|
||||
size: number;
|
||||
/**
|
||||
* @see {@link https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size-strict | PTHREAD_POOL_SIZE_STRICT}
|
||||
*/
|
||||
strict?: boolean;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface SpawnThreadPayload {
|
||||
startArg: number;
|
||||
errorOrTid: number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface StartPayload {
|
||||
tid: number;
|
||||
arg: number;
|
||||
sab?: Int32Array;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface StartResult {
|
||||
exitCode: number;
|
||||
instance: WebAssembly.Instance;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface TerminateAllThreadsPayload {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class ThreadManager {
|
||||
unusedWorkers: WorkerLike[];
|
||||
runningWorkers: WorkerLike[];
|
||||
pthreads: Record<number, WorkerLike>;
|
||||
get nextWorkerID(): number;
|
||||
wasmModule: WebAssembly.Module | null;
|
||||
wasmMemory: WebAssembly.Memory | null;
|
||||
private readonly messageEvents;
|
||||
private readonly _childThread;
|
||||
private readonly _onCreateWorker;
|
||||
private readonly _reuseWorker;
|
||||
private readonly _beforeLoad?;
|
||||
/* Excluded from this release type: printErr */
|
||||
constructor(options: ThreadManagerOptions);
|
||||
init(): void;
|
||||
initMainThread(): void;
|
||||
private preparePool;
|
||||
shouldPreloadWorkers(): boolean;
|
||||
loadWasmModuleToAllWorkers(): Promise<WorkerLike[]>;
|
||||
preloadWorkers(): Promise<WorkerLike[]>;
|
||||
setup(wasmModule: WebAssembly.Module, wasmMemory: WebAssembly.Memory): void;
|
||||
markId(worker: WorkerLike): number;
|
||||
returnWorkerToPool(worker: WorkerLike): void;
|
||||
loadWasmModuleToWorker(worker: WorkerLike, sab?: Int32Array): Promise<WorkerLike>;
|
||||
allocateUnusedWorker(): WorkerLike;
|
||||
getNewWorker(sab?: Int32Array): WorkerLike | undefined;
|
||||
cleanThread(worker: WorkerLike, tid: number, force?: boolean): void;
|
||||
terminateWorker(worker: WorkerLike): void;
|
||||
terminateAllThreads(): void;
|
||||
addMessageEventListener(worker: WorkerLike, onMessage: (e: WorkerMessageEvent) => void): () => void;
|
||||
fireMessageEvent(worker: WorkerLike, e: WorkerMessageEvent): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type ThreadManagerOptions = ThreadManagerOptionsMain | ThreadManagerOptionsChild;
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsBase {
|
||||
printErr?: (message: string) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsChild extends ThreadManagerOptionsBase {
|
||||
childThread: true;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsMain extends ThreadManagerOptionsBase {
|
||||
beforeLoad?: (worker: WorkerLike) => any;
|
||||
reuseWorker?: boolean | number | ReuseWorkerOptions;
|
||||
onCreateWorker: WorkerFactory;
|
||||
childThread?: false;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class ThreadMessageHandler {
|
||||
protected instance: WebAssembly.Instance | undefined;
|
||||
private messagesBeforeLoad;
|
||||
protected postMessage: (message: any) => void;
|
||||
protected onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
protected onError: (error: Error, type: WorkerMessageType) => void;
|
||||
constructor(options?: ThreadMessageHandlerOptions);
|
||||
/** @virtual */
|
||||
instantiate(data: LoadPayload): WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
/** @virtual */
|
||||
handle(e: WorkerMessageEvent<MessageEventData<WorkerMessageType>>): void;
|
||||
private _load;
|
||||
private _start;
|
||||
protected _loaded(err: Error | null, source: WebAssembly.WebAssemblyInstantiatedSource | null, payload: LoadPayload): void;
|
||||
protected handleAfterLoad<E extends WorkerMessageEvent>(e: E, f: (e: E) => void): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadMessageHandlerOptions {
|
||||
onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
onError?: (error: Error, type: WorkerMessageType) => void;
|
||||
postMessage?: (message: any) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface WASIInstance {
|
||||
readonly wasiImport?: Record<string, any>;
|
||||
initialize(instance: object): void;
|
||||
start(instance: object): number;
|
||||
getImportObject?(): any;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class WASIThreads {
|
||||
PThread: ThreadManager | undefined;
|
||||
private wasmMemory;
|
||||
private wasmInstance;
|
||||
private readonly threadSpawn;
|
||||
readonly childThread: boolean;
|
||||
private readonly postMessage;
|
||||
readonly wasi: WASIInstance;
|
||||
constructor(options: WASIThreadsOptions);
|
||||
getImportObject(): {
|
||||
wasi: WASIThreadsImports;
|
||||
};
|
||||
setup(wasmInstance: WebAssembly.Instance, wasmModule: WebAssembly.Module, wasmMemory?: WebAssembly.Memory): void;
|
||||
preloadWorkers(): Promise<WorkerLike[]>;
|
||||
/**
|
||||
* It's ok to call this method to a WASI command module.
|
||||
*
|
||||
* in child thread, must call this method instead of {@link WASIThreads.start} even if it's a WASI command module
|
||||
*
|
||||
* @returns A proxied WebAssembly instance if in child thread, other wise the original instance
|
||||
*/
|
||||
initialize(instance: WebAssembly.Instance, module: WebAssembly.Module, memory?: WebAssembly.Memory): WebAssembly.Instance;
|
||||
/**
|
||||
* Equivalent to calling {@link WASIThreads.initialize} and then calling {@link WASIInstance.start}
|
||||
* ```js
|
||||
* this.initialize(instance, module, memory)
|
||||
* this.wasi.start(instance)
|
||||
* ```
|
||||
*/
|
||||
start(instance: WebAssembly.Instance, module: WebAssembly.Module, memory?: WebAssembly.Memory): StartResult;
|
||||
terminateAllThreads(): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface WASIThreadsImports {
|
||||
'thread-spawn': (startArg: number, errorOrTid?: number) => number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type WASIThreadsOptions = MainThreadOptions | ChildThreadOptions;
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerFactory = (ctx: {
|
||||
type: string;
|
||||
name: string;
|
||||
}) => WorkerLike;
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerLike = (Worker | Worker_2) & {
|
||||
whenLoaded?: Promise<WorkerLike>;
|
||||
loaded?: boolean;
|
||||
__emnapi_tid?: number;
|
||||
};
|
||||
|
||||
/** @public */
|
||||
export declare interface WorkerMessageEvent<T = any> {
|
||||
data: T;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerMessageType = 'load' | 'start';
|
||||
|
||||
export { }
|
||||
1
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.js
generated
vendored
Normal file
1
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
270
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.mts
generated
vendored
Normal file
270
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.mts
generated
vendored
Normal file
|
|
@ -0,0 +1,270 @@
|
|||
/// <reference types="node" />
|
||||
|
||||
import type { Worker as Worker_2 } from 'worker_threads';
|
||||
|
||||
/** @public */
|
||||
export declare interface BaseOptions {
|
||||
wasi: WASIInstance;
|
||||
version?: 'preview1';
|
||||
wasm64?: boolean;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ChildThreadOptions extends BaseOptions {
|
||||
childThread: true;
|
||||
postMessage?: (data: any) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CleanupThreadPayload {
|
||||
tid: number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CommandInfo<T extends CommandType> {
|
||||
type: T;
|
||||
payload: CommandPayloadMap[T];
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CommandPayloadMap {
|
||||
load: LoadPayload;
|
||||
loaded: LoadedPayload;
|
||||
start: StartPayload;
|
||||
'cleanup-thread': CleanupThreadPayload;
|
||||
'terminate-all-threads': TerminateAllThreadsPayload;
|
||||
'spawn-thread': SpawnThreadPayload;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type CommandType = keyof CommandPayloadMap;
|
||||
|
||||
/** @public */
|
||||
export declare function createInstanceProxy(instance: WebAssembly.Instance, memory?: WebAssembly.Memory | (() => WebAssembly.Memory)): WebAssembly.Instance;
|
||||
|
||||
/** @public */
|
||||
export declare function isSharedArrayBuffer(value: any): value is SharedArrayBuffer;
|
||||
|
||||
/** @public */
|
||||
export declare function isTrapError(e: Error): e is WebAssembly.RuntimeError;
|
||||
|
||||
/** @public */
|
||||
export declare interface LoadedPayload {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface LoadPayload {
|
||||
wasmModule: WebAssembly.Module;
|
||||
wasmMemory: WebAssembly.Memory;
|
||||
sab?: Int32Array;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadBaseOptions extends BaseOptions {
|
||||
waitThreadStart?: boolean | number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type MainThreadOptions = MainThreadOptionsWithThreadManager | MainThreadOptionsCreateThreadManager;
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadOptionsCreateThreadManager extends MainThreadBaseOptions, ThreadManagerOptionsMain {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadOptionsWithThreadManager extends MainThreadBaseOptions {
|
||||
threadManager?: ThreadManager | (() => ThreadManager);
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MessageEventData<T extends CommandType> {
|
||||
__emnapi__: CommandInfo<T>;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ReuseWorkerOptions {
|
||||
/**
|
||||
* @see {@link https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size | PTHREAD_POOL_SIZE}
|
||||
*/
|
||||
size: number;
|
||||
/**
|
||||
* @see {@link https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size-strict | PTHREAD_POOL_SIZE_STRICT}
|
||||
*/
|
||||
strict?: boolean;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface SpawnThreadPayload {
|
||||
startArg: number;
|
||||
errorOrTid: number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface StartPayload {
|
||||
tid: number;
|
||||
arg: number;
|
||||
sab?: Int32Array;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface StartResult {
|
||||
exitCode: number;
|
||||
instance: WebAssembly.Instance;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface TerminateAllThreadsPayload {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class ThreadManager {
|
||||
unusedWorkers: WorkerLike[];
|
||||
runningWorkers: WorkerLike[];
|
||||
pthreads: Record<number, WorkerLike>;
|
||||
get nextWorkerID(): number;
|
||||
wasmModule: WebAssembly.Module | null;
|
||||
wasmMemory: WebAssembly.Memory | null;
|
||||
private readonly messageEvents;
|
||||
private readonly _childThread;
|
||||
private readonly _onCreateWorker;
|
||||
private readonly _reuseWorker;
|
||||
private readonly _beforeLoad?;
|
||||
/* Excluded from this release type: printErr */
|
||||
constructor(options: ThreadManagerOptions);
|
||||
init(): void;
|
||||
initMainThread(): void;
|
||||
private preparePool;
|
||||
shouldPreloadWorkers(): boolean;
|
||||
loadWasmModuleToAllWorkers(): Promise<WorkerLike[]>;
|
||||
preloadWorkers(): Promise<WorkerLike[]>;
|
||||
setup(wasmModule: WebAssembly.Module, wasmMemory: WebAssembly.Memory): void;
|
||||
markId(worker: WorkerLike): number;
|
||||
returnWorkerToPool(worker: WorkerLike): void;
|
||||
loadWasmModuleToWorker(worker: WorkerLike, sab?: Int32Array): Promise<WorkerLike>;
|
||||
allocateUnusedWorker(): WorkerLike;
|
||||
getNewWorker(sab?: Int32Array): WorkerLike | undefined;
|
||||
cleanThread(worker: WorkerLike, tid: number, force?: boolean): void;
|
||||
terminateWorker(worker: WorkerLike): void;
|
||||
terminateAllThreads(): void;
|
||||
addMessageEventListener(worker: WorkerLike, onMessage: (e: WorkerMessageEvent) => void): () => void;
|
||||
fireMessageEvent(worker: WorkerLike, e: WorkerMessageEvent): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type ThreadManagerOptions = ThreadManagerOptionsMain | ThreadManagerOptionsChild;
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsBase {
|
||||
printErr?: (message: string) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsChild extends ThreadManagerOptionsBase {
|
||||
childThread: true;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsMain extends ThreadManagerOptionsBase {
|
||||
beforeLoad?: (worker: WorkerLike) => any;
|
||||
reuseWorker?: boolean | number | ReuseWorkerOptions;
|
||||
onCreateWorker: WorkerFactory;
|
||||
childThread?: false;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class ThreadMessageHandler {
|
||||
protected instance: WebAssembly.Instance | undefined;
|
||||
private messagesBeforeLoad;
|
||||
protected postMessage: (message: any) => void;
|
||||
protected onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
protected onError: (error: Error, type: WorkerMessageType) => void;
|
||||
constructor(options?: ThreadMessageHandlerOptions);
|
||||
/** @virtual */
|
||||
instantiate(data: LoadPayload): WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
/** @virtual */
|
||||
handle(e: WorkerMessageEvent<MessageEventData<WorkerMessageType>>): void;
|
||||
private _load;
|
||||
private _start;
|
||||
protected _loaded(err: Error | null, source: WebAssembly.WebAssemblyInstantiatedSource | null, payload: LoadPayload): void;
|
||||
protected handleAfterLoad<E extends WorkerMessageEvent>(e: E, f: (e: E) => void): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadMessageHandlerOptions {
|
||||
onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
onError?: (error: Error, type: WorkerMessageType) => void;
|
||||
postMessage?: (message: any) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface WASIInstance {
|
||||
readonly wasiImport?: Record<string, any>;
|
||||
initialize(instance: object): void;
|
||||
start(instance: object): number;
|
||||
getImportObject?(): any;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class WASIThreads {
|
||||
PThread: ThreadManager | undefined;
|
||||
private wasmMemory;
|
||||
private wasmInstance;
|
||||
private readonly threadSpawn;
|
||||
readonly childThread: boolean;
|
||||
private readonly postMessage;
|
||||
readonly wasi: WASIInstance;
|
||||
constructor(options: WASIThreadsOptions);
|
||||
getImportObject(): {
|
||||
wasi: WASIThreadsImports;
|
||||
};
|
||||
setup(wasmInstance: WebAssembly.Instance, wasmModule: WebAssembly.Module, wasmMemory?: WebAssembly.Memory): void;
|
||||
preloadWorkers(): Promise<WorkerLike[]>;
|
||||
/**
|
||||
* It's ok to call this method to a WASI command module.
|
||||
*
|
||||
* in child thread, must call this method instead of {@link WASIThreads.start} even if it's a WASI command module
|
||||
*
|
||||
* @returns A proxied WebAssembly instance if in child thread, other wise the original instance
|
||||
*/
|
||||
initialize(instance: WebAssembly.Instance, module: WebAssembly.Module, memory?: WebAssembly.Memory): WebAssembly.Instance;
|
||||
/**
|
||||
* Equivalent to calling {@link WASIThreads.initialize} and then calling {@link WASIInstance.start}
|
||||
* ```js
|
||||
* this.initialize(instance, module, memory)
|
||||
* this.wasi.start(instance)
|
||||
* ```
|
||||
*/
|
||||
start(instance: WebAssembly.Instance, module: WebAssembly.Module, memory?: WebAssembly.Memory): StartResult;
|
||||
terminateAllThreads(): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface WASIThreadsImports {
|
||||
'thread-spawn': (startArg: number, errorOrTid?: number) => number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type WASIThreadsOptions = MainThreadOptions | ChildThreadOptions;
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerFactory = (ctx: {
|
||||
type: string;
|
||||
name: string;
|
||||
}) => WorkerLike;
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerLike = (Worker | Worker_2) & {
|
||||
whenLoaded?: Promise<WorkerLike>;
|
||||
loaded?: boolean;
|
||||
__emnapi_tid?: number;
|
||||
};
|
||||
|
||||
/** @public */
|
||||
export declare interface WorkerMessageEvent<T = any> {
|
||||
data: T;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerMessageType = 'load' | 'start';
|
||||
|
||||
export { }
|
||||
272
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts
generated
vendored
Normal file
272
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,272 @@
|
|||
/// <reference types="node" />
|
||||
|
||||
import type { Worker as Worker_2 } from 'worker_threads';
|
||||
|
||||
/** @public */
|
||||
export declare interface BaseOptions {
|
||||
wasi: WASIInstance;
|
||||
version?: 'preview1';
|
||||
wasm64?: boolean;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ChildThreadOptions extends BaseOptions {
|
||||
childThread: true;
|
||||
postMessage?: (data: any) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CleanupThreadPayload {
|
||||
tid: number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CommandInfo<T extends CommandType> {
|
||||
type: T;
|
||||
payload: CommandPayloadMap[T];
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CommandPayloadMap {
|
||||
load: LoadPayload;
|
||||
loaded: LoadedPayload;
|
||||
start: StartPayload;
|
||||
'cleanup-thread': CleanupThreadPayload;
|
||||
'terminate-all-threads': TerminateAllThreadsPayload;
|
||||
'spawn-thread': SpawnThreadPayload;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type CommandType = keyof CommandPayloadMap;
|
||||
|
||||
/** @public */
|
||||
export declare function createInstanceProxy(instance: WebAssembly.Instance, memory?: WebAssembly.Memory | (() => WebAssembly.Memory)): WebAssembly.Instance;
|
||||
|
||||
/** @public */
|
||||
export declare function isSharedArrayBuffer(value: any): value is SharedArrayBuffer;
|
||||
|
||||
/** @public */
|
||||
export declare function isTrapError(e: Error): e is WebAssembly.RuntimeError;
|
||||
|
||||
/** @public */
|
||||
export declare interface LoadedPayload {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface LoadPayload {
|
||||
wasmModule: WebAssembly.Module;
|
||||
wasmMemory: WebAssembly.Memory;
|
||||
sab?: Int32Array;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadBaseOptions extends BaseOptions {
|
||||
waitThreadStart?: boolean | number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type MainThreadOptions = MainThreadOptionsWithThreadManager | MainThreadOptionsCreateThreadManager;
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadOptionsCreateThreadManager extends MainThreadBaseOptions, ThreadManagerOptionsMain {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadOptionsWithThreadManager extends MainThreadBaseOptions {
|
||||
threadManager?: ThreadManager | (() => ThreadManager);
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MessageEventData<T extends CommandType> {
|
||||
__emnapi__: CommandInfo<T>;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ReuseWorkerOptions {
|
||||
/**
|
||||
* @see {@link https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size | PTHREAD_POOL_SIZE}
|
||||
*/
|
||||
size: number;
|
||||
/**
|
||||
* @see {@link https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size-strict | PTHREAD_POOL_SIZE_STRICT}
|
||||
*/
|
||||
strict?: boolean;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface SpawnThreadPayload {
|
||||
startArg: number;
|
||||
errorOrTid: number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface StartPayload {
|
||||
tid: number;
|
||||
arg: number;
|
||||
sab?: Int32Array;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface StartResult {
|
||||
exitCode: number;
|
||||
instance: WebAssembly.Instance;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface TerminateAllThreadsPayload {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class ThreadManager {
|
||||
unusedWorkers: WorkerLike[];
|
||||
runningWorkers: WorkerLike[];
|
||||
pthreads: Record<number, WorkerLike>;
|
||||
get nextWorkerID(): number;
|
||||
wasmModule: WebAssembly.Module | null;
|
||||
wasmMemory: WebAssembly.Memory | null;
|
||||
private readonly messageEvents;
|
||||
private readonly _childThread;
|
||||
private readonly _onCreateWorker;
|
||||
private readonly _reuseWorker;
|
||||
private readonly _beforeLoad?;
|
||||
/* Excluded from this release type: printErr */
|
||||
constructor(options: ThreadManagerOptions);
|
||||
init(): void;
|
||||
initMainThread(): void;
|
||||
private preparePool;
|
||||
shouldPreloadWorkers(): boolean;
|
||||
loadWasmModuleToAllWorkers(): Promise<WorkerLike[]>;
|
||||
preloadWorkers(): Promise<WorkerLike[]>;
|
||||
setup(wasmModule: WebAssembly.Module, wasmMemory: WebAssembly.Memory): void;
|
||||
markId(worker: WorkerLike): number;
|
||||
returnWorkerToPool(worker: WorkerLike): void;
|
||||
loadWasmModuleToWorker(worker: WorkerLike, sab?: Int32Array): Promise<WorkerLike>;
|
||||
allocateUnusedWorker(): WorkerLike;
|
||||
getNewWorker(sab?: Int32Array): WorkerLike | undefined;
|
||||
cleanThread(worker: WorkerLike, tid: number, force?: boolean): void;
|
||||
terminateWorker(worker: WorkerLike): void;
|
||||
terminateAllThreads(): void;
|
||||
addMessageEventListener(worker: WorkerLike, onMessage: (e: WorkerMessageEvent) => void): () => void;
|
||||
fireMessageEvent(worker: WorkerLike, e: WorkerMessageEvent): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type ThreadManagerOptions = ThreadManagerOptionsMain | ThreadManagerOptionsChild;
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsBase {
|
||||
printErr?: (message: string) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsChild extends ThreadManagerOptionsBase {
|
||||
childThread: true;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsMain extends ThreadManagerOptionsBase {
|
||||
beforeLoad?: (worker: WorkerLike) => any;
|
||||
reuseWorker?: boolean | number | ReuseWorkerOptions;
|
||||
onCreateWorker: WorkerFactory;
|
||||
childThread?: false;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class ThreadMessageHandler {
|
||||
protected instance: WebAssembly.Instance | undefined;
|
||||
private messagesBeforeLoad;
|
||||
protected postMessage: (message: any) => void;
|
||||
protected onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
protected onError: (error: Error, type: WorkerMessageType) => void;
|
||||
constructor(options?: ThreadMessageHandlerOptions);
|
||||
/** @virtual */
|
||||
instantiate(data: LoadPayload): WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
/** @virtual */
|
||||
handle(e: WorkerMessageEvent<MessageEventData<WorkerMessageType>>): void;
|
||||
private _load;
|
||||
private _start;
|
||||
protected _loaded(err: Error | null, source: WebAssembly.WebAssemblyInstantiatedSource | null, payload: LoadPayload): void;
|
||||
protected handleAfterLoad<E extends WorkerMessageEvent>(e: E, f: (e: E) => void): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadMessageHandlerOptions {
|
||||
onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
onError?: (error: Error, type: WorkerMessageType) => void;
|
||||
postMessage?: (message: any) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface WASIInstance {
|
||||
readonly wasiImport?: Record<string, any>;
|
||||
initialize(instance: object): void;
|
||||
start(instance: object): number;
|
||||
getImportObject?(): any;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class WASIThreads {
|
||||
PThread: ThreadManager | undefined;
|
||||
private wasmMemory;
|
||||
private wasmInstance;
|
||||
private readonly threadSpawn;
|
||||
readonly childThread: boolean;
|
||||
private readonly postMessage;
|
||||
readonly wasi: WASIInstance;
|
||||
constructor(options: WASIThreadsOptions);
|
||||
getImportObject(): {
|
||||
wasi: WASIThreadsImports;
|
||||
};
|
||||
setup(wasmInstance: WebAssembly.Instance, wasmModule: WebAssembly.Module, wasmMemory?: WebAssembly.Memory): void;
|
||||
preloadWorkers(): Promise<WorkerLike[]>;
|
||||
/**
|
||||
* It's ok to call this method to a WASI command module.
|
||||
*
|
||||
* in child thread, must call this method instead of {@link WASIThreads.start} even if it's a WASI command module
|
||||
*
|
||||
* @returns A proxied WebAssembly instance if in child thread, other wise the original instance
|
||||
*/
|
||||
initialize(instance: WebAssembly.Instance, module: WebAssembly.Module, memory?: WebAssembly.Memory): WebAssembly.Instance;
|
||||
/**
|
||||
* Equivalent to calling {@link WASIThreads.initialize} and then calling {@link WASIInstance.start}
|
||||
* ```js
|
||||
* this.initialize(instance, module, memory)
|
||||
* this.wasi.start(instance)
|
||||
* ```
|
||||
*/
|
||||
start(instance: WebAssembly.Instance, module: WebAssembly.Module, memory?: WebAssembly.Memory): StartResult;
|
||||
terminateAllThreads(): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface WASIThreadsImports {
|
||||
'thread-spawn': (startArg: number, errorOrTid?: number) => number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type WASIThreadsOptions = MainThreadOptions | ChildThreadOptions;
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerFactory = (ctx: {
|
||||
type: string;
|
||||
name: string;
|
||||
}) => WorkerLike;
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerLike = (Worker | Worker_2) & {
|
||||
whenLoaded?: Promise<WorkerLike>;
|
||||
loaded?: boolean;
|
||||
__emnapi_tid?: number;
|
||||
};
|
||||
|
||||
/** @public */
|
||||
export declare interface WorkerMessageEvent<T = any> {
|
||||
data: T;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerMessageType = 'load' | 'start';
|
||||
|
||||
export { }
|
||||
|
||||
export as namespace wasiThreads;
|
||||
945
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.js
generated
vendored
Normal file
945
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.js
generated
vendored
Normal file
|
|
@ -0,0 +1,945 @@
|
|||
var _WebAssembly = typeof WebAssembly !== 'undefined'
|
||||
? WebAssembly
|
||||
: typeof WXWebAssembly !== 'undefined'
|
||||
? WXWebAssembly
|
||||
: undefined;
|
||||
var ENVIRONMENT_IS_NODE = typeof process === 'object' && process !== null &&
|
||||
typeof process.versions === 'object' && process.versions !== null &&
|
||||
typeof process.versions.node === 'string';
|
||||
function getPostMessage(options) {
|
||||
return typeof (options === null || options === void 0 ? void 0 : options.postMessage) === 'function'
|
||||
? options.postMessage
|
||||
: typeof postMessage === 'function'
|
||||
? postMessage
|
||||
: undefined;
|
||||
}
|
||||
function serizeErrorToBuffer(sab, code, error) {
|
||||
var i32array = new Int32Array(sab);
|
||||
Atomics.store(i32array, 0, code);
|
||||
if (code > 1 && error) {
|
||||
var name_1 = error.name;
|
||||
var message = error.message;
|
||||
var stack = error.stack;
|
||||
var nameBuffer = new TextEncoder().encode(name_1);
|
||||
var messageBuffer = new TextEncoder().encode(message);
|
||||
var stackBuffer = new TextEncoder().encode(stack);
|
||||
Atomics.store(i32array, 1, nameBuffer.length);
|
||||
Atomics.store(i32array, 2, messageBuffer.length);
|
||||
Atomics.store(i32array, 3, stackBuffer.length);
|
||||
var buffer = new Uint8Array(sab);
|
||||
buffer.set(nameBuffer, 16);
|
||||
buffer.set(messageBuffer, 16 + nameBuffer.length);
|
||||
buffer.set(stackBuffer, 16 + nameBuffer.length + messageBuffer.length);
|
||||
}
|
||||
}
|
||||
function deserizeErrorFromBuffer(sab) {
|
||||
var _a, _b;
|
||||
var i32array = new Int32Array(sab);
|
||||
var status = Atomics.load(i32array, 0);
|
||||
if (status <= 1) {
|
||||
return null;
|
||||
}
|
||||
var nameLength = Atomics.load(i32array, 1);
|
||||
var messageLength = Atomics.load(i32array, 2);
|
||||
var stackLength = Atomics.load(i32array, 3);
|
||||
var buffer = new Uint8Array(sab);
|
||||
var nameBuffer = buffer.slice(16, 16 + nameLength);
|
||||
var messageBuffer = buffer.slice(16 + nameLength, 16 + nameLength + messageLength);
|
||||
var stackBuffer = buffer.slice(16 + nameLength + messageLength, 16 + nameLength + messageLength + stackLength);
|
||||
var name = new TextDecoder().decode(nameBuffer);
|
||||
var message = new TextDecoder().decode(messageBuffer);
|
||||
var stack = new TextDecoder().decode(stackBuffer);
|
||||
var ErrorConstructor = (_a = globalThis[name]) !== null && _a !== void 0 ? _a : (name === 'RuntimeError' ? ((_b = _WebAssembly.RuntimeError) !== null && _b !== void 0 ? _b : Error) : Error);
|
||||
var error = new ErrorConstructor(message);
|
||||
Object.defineProperty(error, 'stack', {
|
||||
value: stack,
|
||||
writable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
return error;
|
||||
}
|
||||
/** @public */
|
||||
function isSharedArrayBuffer(value) {
|
||||
return ((typeof SharedArrayBuffer === 'function' && value instanceof SharedArrayBuffer) ||
|
||||
(Object.prototype.toString.call(value) === '[object SharedArrayBuffer]'));
|
||||
}
|
||||
/** @public */
|
||||
function isTrapError(e) {
|
||||
try {
|
||||
return e instanceof _WebAssembly.RuntimeError;
|
||||
}
|
||||
catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function createMessage(type, payload) {
|
||||
return {
|
||||
__emnapi__: {
|
||||
type: type,
|
||||
payload: payload
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var WASI_THREADS_MAX_TID = 0x1FFFFFFF;
|
||||
function checkSharedWasmMemory(wasmMemory) {
|
||||
if (wasmMemory) {
|
||||
if (!isSharedArrayBuffer(wasmMemory.buffer)) {
|
||||
throw new Error('Multithread features require shared wasm memory. ' +
|
||||
'Try to compile with `-matomics -mbulk-memory` and use `--import-memory --shared-memory` during linking, ' +
|
||||
'then create WebAssembly.Memory with `shared: true` option');
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (typeof SharedArrayBuffer === 'undefined') {
|
||||
throw new Error('Current environment does not support SharedArrayBuffer, threads are not available!');
|
||||
}
|
||||
}
|
||||
}
|
||||
function getReuseWorker(value) {
|
||||
var _a;
|
||||
if (typeof value === 'boolean') {
|
||||
return value ? { size: 0, strict: false } : false;
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
if (!(value >= 0)) {
|
||||
throw new RangeError('reuseWorker: size must be a non-negative integer');
|
||||
}
|
||||
return { size: value, strict: false };
|
||||
}
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
var size = (_a = Number(value.size)) !== null && _a !== void 0 ? _a : 0;
|
||||
var strict = Boolean(value.strict);
|
||||
if (!(size > 0) && strict) {
|
||||
throw new RangeError('reuseWorker: size must be set to positive integer if strict is set to true');
|
||||
}
|
||||
return { size: size, strict: strict };
|
||||
}
|
||||
var nextWorkerID = 0;
|
||||
/** @public */
|
||||
var ThreadManager = /*#__PURE__*/ (function () {
|
||||
function ThreadManager(options) {
|
||||
var _a;
|
||||
this.unusedWorkers = [];
|
||||
this.runningWorkers = [];
|
||||
this.pthreads = Object.create(null);
|
||||
this.wasmModule = null;
|
||||
this.wasmMemory = null;
|
||||
this.messageEvents = new WeakMap();
|
||||
if (!options) {
|
||||
throw new TypeError('ThreadManager(): options is not provided');
|
||||
}
|
||||
if ('childThread' in options) {
|
||||
this._childThread = Boolean(options.childThread);
|
||||
}
|
||||
else {
|
||||
this._childThread = false;
|
||||
}
|
||||
if (this._childThread) {
|
||||
this._onCreateWorker = undefined;
|
||||
this._reuseWorker = false;
|
||||
this._beforeLoad = undefined;
|
||||
}
|
||||
else {
|
||||
this._onCreateWorker = options.onCreateWorker;
|
||||
this._reuseWorker = getReuseWorker(options.reuseWorker);
|
||||
this._beforeLoad = options.beforeLoad;
|
||||
}
|
||||
this.printErr = (_a = options.printErr) !== null && _a !== void 0 ? _a : console.error.bind(console);
|
||||
}
|
||||
Object.defineProperty(ThreadManager.prototype, "nextWorkerID", {
|
||||
get: function () { return nextWorkerID; },
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
ThreadManager.prototype.init = function () {
|
||||
if (!this._childThread) {
|
||||
this.initMainThread();
|
||||
}
|
||||
};
|
||||
ThreadManager.prototype.initMainThread = function () {
|
||||
this.preparePool();
|
||||
};
|
||||
ThreadManager.prototype.preparePool = function () {
|
||||
if (this._reuseWorker) {
|
||||
if (this._reuseWorker.size) {
|
||||
var pthreadPoolSize = this._reuseWorker.size;
|
||||
while (pthreadPoolSize--) {
|
||||
var worker = this.allocateUnusedWorker();
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
// https://github.com/nodejs/node/issues/53036
|
||||
worker.once('message', function () { });
|
||||
worker.unref();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
ThreadManager.prototype.shouldPreloadWorkers = function () {
|
||||
return !this._childThread && this._reuseWorker && this._reuseWorker.size > 0;
|
||||
};
|
||||
ThreadManager.prototype.loadWasmModuleToAllWorkers = function () {
|
||||
var _this_1 = this;
|
||||
var promises = Array(this.unusedWorkers.length);
|
||||
var _loop_1 = function (i) {
|
||||
var worker = this_1.unusedWorkers[i];
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.ref();
|
||||
promises[i] = this_1.loadWasmModuleToWorker(worker).then(function (w) {
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.unref();
|
||||
return w;
|
||||
}, function (e) {
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.unref();
|
||||
throw e;
|
||||
});
|
||||
};
|
||||
var this_1 = this;
|
||||
for (var i = 0; i < this.unusedWorkers.length; ++i) {
|
||||
_loop_1(i);
|
||||
}
|
||||
return Promise.all(promises).catch(function (err) {
|
||||
_this_1.terminateAllThreads();
|
||||
throw err;
|
||||
});
|
||||
};
|
||||
ThreadManager.prototype.preloadWorkers = function () {
|
||||
if (this.shouldPreloadWorkers()) {
|
||||
return this.loadWasmModuleToAllWorkers();
|
||||
}
|
||||
return Promise.resolve([]);
|
||||
};
|
||||
ThreadManager.prototype.setup = function (wasmModule, wasmMemory) {
|
||||
this.wasmModule = wasmModule;
|
||||
this.wasmMemory = wasmMemory;
|
||||
};
|
||||
ThreadManager.prototype.markId = function (worker) {
|
||||
if (worker.__emnapi_tid)
|
||||
return worker.__emnapi_tid;
|
||||
var tid = nextWorkerID + 43;
|
||||
nextWorkerID = (nextWorkerID + 1) % (WASI_THREADS_MAX_TID - 42);
|
||||
this.pthreads[tid] = worker;
|
||||
worker.__emnapi_tid = tid;
|
||||
return tid;
|
||||
};
|
||||
ThreadManager.prototype.returnWorkerToPool = function (worker) {
|
||||
var tid = worker.__emnapi_tid;
|
||||
if (tid !== undefined) {
|
||||
delete this.pthreads[tid];
|
||||
}
|
||||
this.unusedWorkers.push(worker);
|
||||
this.runningWorkers.splice(this.runningWorkers.indexOf(worker), 1);
|
||||
delete worker.__emnapi_tid;
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.unref();
|
||||
}
|
||||
};
|
||||
ThreadManager.prototype.loadWasmModuleToWorker = function (worker, sab) {
|
||||
var _this_1 = this;
|
||||
if (worker.whenLoaded)
|
||||
return worker.whenLoaded;
|
||||
var err = this.printErr;
|
||||
var beforeLoad = this._beforeLoad;
|
||||
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
||||
var _this = this;
|
||||
worker.whenLoaded = new Promise(function (resolve, reject) {
|
||||
var handleError = function (e) {
|
||||
var message = 'worker sent an error!';
|
||||
if (worker.__emnapi_tid !== undefined) {
|
||||
message = 'worker (tid = ' + worker.__emnapi_tid + ') sent an error!';
|
||||
}
|
||||
if ('message' in e) {
|
||||
err(message + ' ' + e.message);
|
||||
if (e.message.indexOf('RuntimeError') !== -1 || e.message.indexOf('unreachable') !== -1) {
|
||||
try {
|
||||
_this.terminateAllThreads();
|
||||
}
|
||||
catch (_) { }
|
||||
}
|
||||
}
|
||||
else {
|
||||
err(message);
|
||||
}
|
||||
reject(e);
|
||||
throw e;
|
||||
};
|
||||
var handleMessage = function (data) {
|
||||
if (data.__emnapi__) {
|
||||
var type = data.__emnapi__.type;
|
||||
var payload = data.__emnapi__.payload;
|
||||
if (type === 'loaded') {
|
||||
worker.loaded = true;
|
||||
if (ENVIRONMENT_IS_NODE && !worker.__emnapi_tid) {
|
||||
worker.unref();
|
||||
}
|
||||
resolve(worker);
|
||||
// if (payload.err) {
|
||||
// err('failed to load in child thread: ' + (payload.err.message || payload.err))
|
||||
// }
|
||||
}
|
||||
else if (type === 'cleanup-thread') {
|
||||
if (payload.tid in _this_1.pthreads) {
|
||||
_this_1.cleanThread(worker, payload.tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
worker.onmessage = function (e) {
|
||||
handleMessage(e.data);
|
||||
_this_1.fireMessageEvent(worker, e);
|
||||
};
|
||||
worker.onerror = handleError;
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.on('message', function (data) {
|
||||
var _a, _b;
|
||||
(_b = (_a = worker).onmessage) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
||||
data: data
|
||||
});
|
||||
});
|
||||
worker.on('error', function (e) {
|
||||
var _a, _b;
|
||||
(_b = (_a = worker).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, e);
|
||||
});
|
||||
worker.on('detachedExit', function () { });
|
||||
}
|
||||
if (typeof beforeLoad === 'function') {
|
||||
beforeLoad(worker);
|
||||
}
|
||||
try {
|
||||
worker.postMessage(createMessage('load', {
|
||||
wasmModule: _this_1.wasmModule,
|
||||
wasmMemory: _this_1.wasmMemory,
|
||||
sab: sab
|
||||
}));
|
||||
}
|
||||
catch (err) {
|
||||
checkSharedWasmMemory(_this_1.wasmMemory);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
return worker.whenLoaded;
|
||||
};
|
||||
ThreadManager.prototype.allocateUnusedWorker = function () {
|
||||
var _onCreateWorker = this._onCreateWorker;
|
||||
if (typeof _onCreateWorker !== 'function') {
|
||||
throw new TypeError('`options.onCreateWorker` is not provided');
|
||||
}
|
||||
var worker = _onCreateWorker({ type: 'thread', name: 'emnapi-pthread' });
|
||||
this.unusedWorkers.push(worker);
|
||||
return worker;
|
||||
};
|
||||
ThreadManager.prototype.getNewWorker = function (sab) {
|
||||
if (this._reuseWorker) {
|
||||
if (this.unusedWorkers.length === 0) {
|
||||
if (this._reuseWorker.strict) {
|
||||
if (!ENVIRONMENT_IS_NODE) {
|
||||
var err = this.printErr;
|
||||
err('Tried to spawn a new thread, but the thread pool is exhausted.\n' +
|
||||
'This might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
var worker_1 = this.allocateUnusedWorker();
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
this.loadWasmModuleToWorker(worker_1, sab);
|
||||
}
|
||||
return this.unusedWorkers.pop();
|
||||
}
|
||||
var worker = this.allocateUnusedWorker();
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
this.loadWasmModuleToWorker(worker, sab);
|
||||
return this.unusedWorkers.pop();
|
||||
};
|
||||
ThreadManager.prototype.cleanThread = function (worker, tid, force) {
|
||||
if (!force && this._reuseWorker) {
|
||||
this.returnWorkerToPool(worker);
|
||||
}
|
||||
else {
|
||||
delete this.pthreads[tid];
|
||||
var index = this.runningWorkers.indexOf(worker);
|
||||
if (index !== -1) {
|
||||
this.runningWorkers.splice(index, 1);
|
||||
}
|
||||
this.terminateWorker(worker);
|
||||
delete worker.__emnapi_tid;
|
||||
}
|
||||
};
|
||||
ThreadManager.prototype.terminateWorker = function (worker) {
|
||||
var _this_1 = this;
|
||||
var _a;
|
||||
var tid = worker.__emnapi_tid;
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
worker.terminate();
|
||||
(_a = this.messageEvents.get(worker)) === null || _a === void 0 ? void 0 : _a.clear();
|
||||
this.messageEvents.delete(worker);
|
||||
worker.onmessage = function (e) {
|
||||
if (e.data.__emnapi__) {
|
||||
var err = _this_1.printErr;
|
||||
err('received "' + e.data.__emnapi__.type + '" command from terminated worker: ' + tid);
|
||||
}
|
||||
};
|
||||
};
|
||||
ThreadManager.prototype.terminateAllThreads = function () {
|
||||
for (var i = 0; i < this.runningWorkers.length; ++i) {
|
||||
this.terminateWorker(this.runningWorkers[i]);
|
||||
}
|
||||
for (var i = 0; i < this.unusedWorkers.length; ++i) {
|
||||
this.terminateWorker(this.unusedWorkers[i]);
|
||||
}
|
||||
this.unusedWorkers = [];
|
||||
this.runningWorkers = [];
|
||||
this.pthreads = Object.create(null);
|
||||
this.preparePool();
|
||||
};
|
||||
ThreadManager.prototype.addMessageEventListener = function (worker, onMessage) {
|
||||
var listeners = this.messageEvents.get(worker);
|
||||
if (!listeners) {
|
||||
listeners = new Set();
|
||||
this.messageEvents.set(worker, listeners);
|
||||
}
|
||||
listeners.add(onMessage);
|
||||
return function () {
|
||||
listeners === null || listeners === void 0 ? void 0 : listeners.delete(onMessage);
|
||||
};
|
||||
};
|
||||
ThreadManager.prototype.fireMessageEvent = function (worker, e) {
|
||||
var listeners = this.messageEvents.get(worker);
|
||||
if (!listeners)
|
||||
return;
|
||||
var err = this.printErr;
|
||||
listeners.forEach(function (listener) {
|
||||
try {
|
||||
listener(e);
|
||||
}
|
||||
catch (e) {
|
||||
err(e.stack);
|
||||
}
|
||||
});
|
||||
};
|
||||
return ThreadManager;
|
||||
}());
|
||||
|
||||
var kIsProxy = Symbol('kIsProxy');
|
||||
/** @public */
|
||||
function createInstanceProxy(instance, memory) {
|
||||
if (instance[kIsProxy])
|
||||
return instance;
|
||||
// https://github.com/nodejs/help/issues/4102
|
||||
var originalExports = instance.exports;
|
||||
var createHandler = function (target) {
|
||||
var handlers = [
|
||||
'apply',
|
||||
'construct',
|
||||
'defineProperty',
|
||||
'deleteProperty',
|
||||
'get',
|
||||
'getOwnPropertyDescriptor',
|
||||
'getPrototypeOf',
|
||||
'has',
|
||||
'isExtensible',
|
||||
'ownKeys',
|
||||
'preventExtensions',
|
||||
'set',
|
||||
'setPrototypeOf'
|
||||
];
|
||||
var handler = {};
|
||||
var _loop_1 = function (i) {
|
||||
var name_1 = handlers[i];
|
||||
handler[name_1] = function () {
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
args.unshift(target);
|
||||
return Reflect[name_1].apply(Reflect, args);
|
||||
};
|
||||
};
|
||||
for (var i = 0; i < handlers.length; i++) {
|
||||
_loop_1(i);
|
||||
}
|
||||
return handler;
|
||||
};
|
||||
var handler = createHandler(originalExports);
|
||||
var _initialize = function () { };
|
||||
var _start = function () { return 0; };
|
||||
handler.get = function (_target, p, receiver) {
|
||||
var _a;
|
||||
if (p === 'memory') {
|
||||
return (_a = (typeof memory === 'function' ? memory() : memory)) !== null && _a !== void 0 ? _a : Reflect.get(originalExports, p, receiver);
|
||||
}
|
||||
if (p === '_initialize') {
|
||||
return p in originalExports ? _initialize : undefined;
|
||||
}
|
||||
if (p === '_start') {
|
||||
return p in originalExports ? _start : undefined;
|
||||
}
|
||||
return Reflect.get(originalExports, p, receiver);
|
||||
};
|
||||
handler.has = function (_target, p) {
|
||||
if (p === 'memory')
|
||||
return true;
|
||||
return Reflect.has(originalExports, p);
|
||||
};
|
||||
var exportsProxy = new Proxy(Object.create(null), handler);
|
||||
return new Proxy(instance, {
|
||||
get: function (target, p, receiver) {
|
||||
if (p === 'exports') {
|
||||
return exportsProxy;
|
||||
}
|
||||
if (p === kIsProxy) {
|
||||
return true;
|
||||
}
|
||||
return Reflect.get(target, p, receiver);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var patchedWasiInstances = new WeakMap();
|
||||
/** @public */
|
||||
var WASIThreads = /*#__PURE__*/ (function () {
|
||||
function WASIThreads(options) {
|
||||
var _this_1 = this;
|
||||
if (!options) {
|
||||
throw new TypeError('WASIThreads(): options is not provided');
|
||||
}
|
||||
if (!options.wasi) {
|
||||
throw new TypeError('WASIThreads(): options.wasi is not provided');
|
||||
}
|
||||
patchedWasiInstances.set(this, new WeakSet());
|
||||
var wasi = options.wasi;
|
||||
patchWasiInstance(this, wasi);
|
||||
this.wasi = wasi;
|
||||
if ('childThread' in options) {
|
||||
this.childThread = Boolean(options.childThread);
|
||||
}
|
||||
else {
|
||||
this.childThread = false;
|
||||
}
|
||||
this.PThread = undefined;
|
||||
if ('threadManager' in options) {
|
||||
if (typeof options.threadManager === 'function') {
|
||||
this.PThread = options.threadManager();
|
||||
}
|
||||
else {
|
||||
this.PThread = options.threadManager;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!this.childThread) {
|
||||
this.PThread = new ThreadManager(options);
|
||||
this.PThread.init();
|
||||
}
|
||||
}
|
||||
var waitThreadStart = false;
|
||||
if ('waitThreadStart' in options) {
|
||||
waitThreadStart = typeof options.waitThreadStart === 'number' ? options.waitThreadStart : Boolean(options.waitThreadStart);
|
||||
}
|
||||
var postMessage = getPostMessage(options);
|
||||
if (this.childThread && typeof postMessage !== 'function') {
|
||||
throw new TypeError('options.postMessage is not a function');
|
||||
}
|
||||
this.postMessage = postMessage;
|
||||
var wasm64 = Boolean(options.wasm64);
|
||||
var onMessage = function (e) {
|
||||
if (e.data.__emnapi__) {
|
||||
var type = e.data.__emnapi__.type;
|
||||
var payload = e.data.__emnapi__.payload;
|
||||
if (type === 'spawn-thread') {
|
||||
threadSpawn(payload.startArg, payload.errorOrTid);
|
||||
}
|
||||
else if (type === 'terminate-all-threads') {
|
||||
_this_1.terminateAllThreads();
|
||||
}
|
||||
}
|
||||
};
|
||||
var threadSpawn = function (startArg, errorOrTid) {
|
||||
var _a;
|
||||
var EAGAIN = 6;
|
||||
var isNewABI = errorOrTid !== undefined;
|
||||
try {
|
||||
checkSharedWasmMemory(_this_1.wasmMemory);
|
||||
}
|
||||
catch (err) {
|
||||
(_a = _this_1.PThread) === null || _a === void 0 ? void 0 : _a.printErr(err.stack);
|
||||
if (isNewABI) {
|
||||
var struct_1 = new Int32Array(_this_1.wasmMemory.buffer, errorOrTid, 2);
|
||||
Atomics.store(struct_1, 0, 1);
|
||||
Atomics.store(struct_1, 1, EAGAIN);
|
||||
Atomics.notify(struct_1, 1);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return -EAGAIN;
|
||||
}
|
||||
}
|
||||
if (!isNewABI) {
|
||||
var malloc = _this_1.wasmInstance.exports.malloc;
|
||||
errorOrTid = wasm64 ? Number(malloc(BigInt(8))) : malloc(8);
|
||||
if (!errorOrTid) {
|
||||
return -48; /* ENOMEM */
|
||||
}
|
||||
}
|
||||
var _free = _this_1.wasmInstance.exports.free;
|
||||
var free = wasm64 ? function (ptr) { _free(BigInt(ptr)); } : _free;
|
||||
var struct = new Int32Array(_this_1.wasmMemory.buffer, errorOrTid, 2);
|
||||
Atomics.store(struct, 0, 0);
|
||||
Atomics.store(struct, 1, 0);
|
||||
if (_this_1.childThread) {
|
||||
postMessage(createMessage('spawn-thread', {
|
||||
startArg: startArg,
|
||||
errorOrTid: errorOrTid
|
||||
}));
|
||||
Atomics.wait(struct, 1, 0);
|
||||
var isError = Atomics.load(struct, 0);
|
||||
var result = Atomics.load(struct, 1);
|
||||
if (isNewABI) {
|
||||
return isError;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return isError ? -result : result;
|
||||
}
|
||||
var shouldWait = waitThreadStart || (waitThreadStart === 0);
|
||||
var sab;
|
||||
if (shouldWait) {
|
||||
sab = new Int32Array(new SharedArrayBuffer(16 + 8192));
|
||||
Atomics.store(sab, 0, 0);
|
||||
}
|
||||
var worker;
|
||||
var tid;
|
||||
var PThread = _this_1.PThread;
|
||||
try {
|
||||
worker = PThread.getNewWorker(sab);
|
||||
if (!worker) {
|
||||
throw new Error('failed to get new worker');
|
||||
}
|
||||
PThread.addMessageEventListener(worker, onMessage);
|
||||
tid = PThread.markId(worker);
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.ref();
|
||||
}
|
||||
worker.postMessage(createMessage('start', {
|
||||
tid: tid,
|
||||
arg: startArg,
|
||||
sab: sab
|
||||
}));
|
||||
if (shouldWait) {
|
||||
if (typeof waitThreadStart === 'number') {
|
||||
var waitResult = Atomics.wait(sab, 0, 0, waitThreadStart);
|
||||
if (waitResult === 'timed-out') {
|
||||
try {
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
}
|
||||
catch (_) { }
|
||||
throw new Error('Spawning thread timed out. Please check if the worker is created successfully and if message is handled properly in the worker.');
|
||||
}
|
||||
}
|
||||
else {
|
||||
Atomics.wait(sab, 0, 0);
|
||||
}
|
||||
var r = Atomics.load(sab, 0);
|
||||
if (r > 1) {
|
||||
try {
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
}
|
||||
catch (_) { }
|
||||
throw deserizeErrorFromBuffer(sab.buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
Atomics.store(struct, 0, 1);
|
||||
Atomics.store(struct, 1, EAGAIN);
|
||||
Atomics.notify(struct, 1);
|
||||
PThread === null || PThread === void 0 ? void 0 : PThread.printErr(e.stack);
|
||||
if (isNewABI) {
|
||||
return 1;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return -EAGAIN;
|
||||
}
|
||||
Atomics.store(struct, 0, 0);
|
||||
Atomics.store(struct, 1, tid);
|
||||
Atomics.notify(struct, 1);
|
||||
PThread.runningWorkers.push(worker);
|
||||
if (!shouldWait) {
|
||||
worker.whenLoaded.catch(function (err) {
|
||||
delete worker.whenLoaded;
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
if (isNewABI) {
|
||||
return 0;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return tid;
|
||||
};
|
||||
this.threadSpawn = threadSpawn;
|
||||
}
|
||||
WASIThreads.prototype.getImportObject = function () {
|
||||
return {
|
||||
wasi: {
|
||||
'thread-spawn': this.threadSpawn
|
||||
}
|
||||
};
|
||||
};
|
||||
WASIThreads.prototype.setup = function (wasmInstance, wasmModule, wasmMemory) {
|
||||
wasmMemory !== null && wasmMemory !== void 0 ? wasmMemory : (wasmMemory = wasmInstance.exports.memory);
|
||||
this.wasmInstance = wasmInstance;
|
||||
this.wasmMemory = wasmMemory;
|
||||
if (this.PThread) {
|
||||
this.PThread.setup(wasmModule, wasmMemory);
|
||||
}
|
||||
};
|
||||
WASIThreads.prototype.preloadWorkers = function () {
|
||||
if (this.PThread) {
|
||||
return this.PThread.preloadWorkers();
|
||||
}
|
||||
return Promise.resolve([]);
|
||||
};
|
||||
/**
|
||||
* It's ok to call this method to a WASI command module.
|
||||
*
|
||||
* in child thread, must call this method instead of {@link WASIThreads.start} even if it's a WASI command module
|
||||
*
|
||||
* @returns A proxied WebAssembly instance if in child thread, other wise the original instance
|
||||
*/
|
||||
WASIThreads.prototype.initialize = function (instance, module, memory) {
|
||||
var exports = instance.exports;
|
||||
memory !== null && memory !== void 0 ? memory : (memory = exports.memory);
|
||||
if (this.childThread) {
|
||||
instance = createInstanceProxy(instance, memory);
|
||||
}
|
||||
this.setup(instance, module, memory);
|
||||
var wasi = this.wasi;
|
||||
if (('_start' in exports) && (typeof exports._start === 'function')) {
|
||||
if (this.childThread) {
|
||||
wasi.start(instance);
|
||||
try {
|
||||
var kStarted = getWasiSymbol(wasi, 'kStarted');
|
||||
wasi[kStarted] = false;
|
||||
}
|
||||
catch (_) { }
|
||||
}
|
||||
else {
|
||||
setupInstance(wasi, instance);
|
||||
}
|
||||
}
|
||||
else {
|
||||
wasi.initialize(instance);
|
||||
}
|
||||
return instance;
|
||||
};
|
||||
/**
|
||||
* Equivalent to calling {@link WASIThreads.initialize} and then calling {@link WASIInstance.start}
|
||||
* ```js
|
||||
* this.initialize(instance, module, memory)
|
||||
* this.wasi.start(instance)
|
||||
* ```
|
||||
*/
|
||||
WASIThreads.prototype.start = function (instance, module, memory) {
|
||||
var exports = instance.exports;
|
||||
memory !== null && memory !== void 0 ? memory : (memory = exports.memory);
|
||||
if (this.childThread) {
|
||||
instance = createInstanceProxy(instance, memory);
|
||||
}
|
||||
this.setup(instance, module, memory);
|
||||
var exitCode = this.wasi.start(instance);
|
||||
return { exitCode: exitCode, instance: instance };
|
||||
};
|
||||
WASIThreads.prototype.terminateAllThreads = function () {
|
||||
var _a;
|
||||
if (!this.childThread) {
|
||||
(_a = this.PThread) === null || _a === void 0 ? void 0 : _a.terminateAllThreads();
|
||||
}
|
||||
else {
|
||||
this.postMessage(createMessage('terminate-all-threads', {}));
|
||||
}
|
||||
};
|
||||
return WASIThreads;
|
||||
}());
|
||||
function patchWasiInstance(wasiThreads, wasi) {
|
||||
var patched = patchedWasiInstances.get(wasiThreads);
|
||||
if (patched.has(wasi)) {
|
||||
return;
|
||||
}
|
||||
var _this = wasiThreads;
|
||||
var wasiImport = wasi.wasiImport;
|
||||
if (wasiImport) {
|
||||
var proc_exit_1 = wasiImport.proc_exit;
|
||||
wasiImport.proc_exit = function (code) {
|
||||
_this.terminateAllThreads();
|
||||
return proc_exit_1.call(this, code);
|
||||
};
|
||||
}
|
||||
if (!_this.childThread) {
|
||||
var start_1 = wasi.start;
|
||||
if (typeof start_1 === 'function') {
|
||||
wasi.start = function (instance) {
|
||||
try {
|
||||
return start_1.call(this, instance);
|
||||
}
|
||||
catch (err) {
|
||||
if (isTrapError(err)) {
|
||||
_this.terminateAllThreads();
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
patched.add(wasi);
|
||||
}
|
||||
function getWasiSymbol(wasi, description) {
|
||||
var symbols = Object.getOwnPropertySymbols(wasi);
|
||||
var selectDescription = function (description) { return function (s) {
|
||||
if (s.description) {
|
||||
return s.description === description;
|
||||
}
|
||||
return s.toString() === "Symbol(".concat(description, ")");
|
||||
}; };
|
||||
if (Array.isArray(description)) {
|
||||
return description.map(function (d) { return symbols.filter(selectDescription(d))[0]; });
|
||||
}
|
||||
return symbols.filter(selectDescription(description))[0];
|
||||
}
|
||||
function setupInstance(wasi, instance) {
|
||||
var _a = getWasiSymbol(wasi, ['kInstance', 'kSetMemory']), kInstance = _a[0], kSetMemory = _a[1];
|
||||
wasi[kInstance] = instance;
|
||||
wasi[kSetMemory](instance.exports.memory);
|
||||
}
|
||||
|
||||
/** @public */
|
||||
var ThreadMessageHandler = /*#__PURE__*/ (function () {
|
||||
function ThreadMessageHandler(options) {
|
||||
var postMsg = getPostMessage(options);
|
||||
if (typeof postMsg !== 'function') {
|
||||
throw new TypeError('options.postMessage is not a function');
|
||||
}
|
||||
this.postMessage = postMsg;
|
||||
this.onLoad = options === null || options === void 0 ? void 0 : options.onLoad;
|
||||
this.onError = typeof (options === null || options === void 0 ? void 0 : options.onError) === 'function' ? options.onError : function (_type, err) { throw err; };
|
||||
this.instance = undefined;
|
||||
// this.module = undefined
|
||||
this.messagesBeforeLoad = [];
|
||||
}
|
||||
/** @virtual */
|
||||
ThreadMessageHandler.prototype.instantiate = function (data) {
|
||||
if (typeof this.onLoad === 'function') {
|
||||
return this.onLoad(data);
|
||||
}
|
||||
throw new Error('ThreadMessageHandler.prototype.instantiate is not implemented');
|
||||
};
|
||||
/** @virtual */
|
||||
ThreadMessageHandler.prototype.handle = function (e) {
|
||||
var _this = this;
|
||||
var _a;
|
||||
if ((_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.__emnapi__) {
|
||||
var type = e.data.__emnapi__.type;
|
||||
var payload_1 = e.data.__emnapi__.payload;
|
||||
try {
|
||||
if (type === 'load') {
|
||||
this._load(payload_1);
|
||||
}
|
||||
else if (type === 'start') {
|
||||
this.handleAfterLoad(e, function () {
|
||||
_this._start(payload_1);
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
this.onError(err, type);
|
||||
}
|
||||
}
|
||||
};
|
||||
ThreadMessageHandler.prototype._load = function (payload) {
|
||||
var _this = this;
|
||||
if (this.instance !== undefined)
|
||||
return;
|
||||
var source;
|
||||
try {
|
||||
source = this.instantiate(payload);
|
||||
}
|
||||
catch (err) {
|
||||
this._loaded(err, null, payload);
|
||||
return;
|
||||
}
|
||||
var then = source && 'then' in source ? source.then : undefined;
|
||||
if (typeof then === 'function') {
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
then.call(source, function (source) { _this._loaded(null, source, payload); }, function (err) { _this._loaded(err, null, payload); });
|
||||
}
|
||||
else {
|
||||
this._loaded(null, source, payload);
|
||||
}
|
||||
};
|
||||
ThreadMessageHandler.prototype._start = function (payload) {
|
||||
var wasi_thread_start = this.instance.exports.wasi_thread_start;
|
||||
if (typeof wasi_thread_start !== 'function') {
|
||||
var err = new TypeError('wasi_thread_start is not exported');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
var postMessage = this.postMessage;
|
||||
var tid = payload.tid;
|
||||
var startArg = payload.arg;
|
||||
notifyPthreadCreateResult(payload.sab, 1);
|
||||
try {
|
||||
wasi_thread_start(tid, startArg);
|
||||
}
|
||||
catch (err) {
|
||||
if (err !== 'unwind') {
|
||||
throw err;
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
postMessage(createMessage('cleanup-thread', { tid: tid }));
|
||||
};
|
||||
ThreadMessageHandler.prototype._loaded = function (err, source, payload) {
|
||||
if (err) {
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
if (source == null) {
|
||||
var err_1 = new TypeError('onLoad should return an object');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err_1);
|
||||
throw err_1;
|
||||
}
|
||||
var instance = source.instance;
|
||||
if (!instance) {
|
||||
var err_2 = new TypeError('onLoad should return an object which includes "instance"');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err_2);
|
||||
throw err_2;
|
||||
}
|
||||
this.instance = instance;
|
||||
var postMessage = this.postMessage;
|
||||
postMessage(createMessage('loaded', {}));
|
||||
var messages = this.messagesBeforeLoad;
|
||||
this.messagesBeforeLoad = [];
|
||||
for (var i = 0; i < messages.length; i++) {
|
||||
var data = messages[i];
|
||||
this.handle({ data: data });
|
||||
}
|
||||
};
|
||||
ThreadMessageHandler.prototype.handleAfterLoad = function (e, f) {
|
||||
if (this.instance !== undefined) {
|
||||
f.call(this, e);
|
||||
}
|
||||
else {
|
||||
this.messagesBeforeLoad.push(e.data);
|
||||
}
|
||||
};
|
||||
return ThreadMessageHandler;
|
||||
}());
|
||||
function notifyPthreadCreateResult(sab, result, error) {
|
||||
if (sab) {
|
||||
serizeErrorToBuffer(sab.buffer, result, error);
|
||||
Atomics.notify(sab, 0);
|
||||
}
|
||||
}
|
||||
|
||||
export { ThreadManager, ThreadMessageHandler, WASIThreads, createInstanceProxy, isSharedArrayBuffer, isTrapError };
|
||||
957
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js
generated
vendored
Normal file
957
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js
generated
vendored
Normal file
|
|
@ -0,0 +1,957 @@
|
|||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
||||
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.wasiThreads = {}));
|
||||
})(this, (function (exports) {
|
||||
var _WebAssembly = typeof WebAssembly !== 'undefined'
|
||||
? WebAssembly
|
||||
: typeof WXWebAssembly !== 'undefined'
|
||||
? WXWebAssembly
|
||||
: undefined;
|
||||
var ENVIRONMENT_IS_NODE = typeof process === 'object' && process !== null &&
|
||||
typeof process.versions === 'object' && process.versions !== null &&
|
||||
typeof process.versions.node === 'string';
|
||||
function getPostMessage(options) {
|
||||
return typeof (options === null || options === void 0 ? void 0 : options.postMessage) === 'function'
|
||||
? options.postMessage
|
||||
: typeof postMessage === 'function'
|
||||
? postMessage
|
||||
: undefined;
|
||||
}
|
||||
function serizeErrorToBuffer(sab, code, error) {
|
||||
var i32array = new Int32Array(sab);
|
||||
Atomics.store(i32array, 0, code);
|
||||
if (code > 1 && error) {
|
||||
var name_1 = error.name;
|
||||
var message = error.message;
|
||||
var stack = error.stack;
|
||||
var nameBuffer = new TextEncoder().encode(name_1);
|
||||
var messageBuffer = new TextEncoder().encode(message);
|
||||
var stackBuffer = new TextEncoder().encode(stack);
|
||||
Atomics.store(i32array, 1, nameBuffer.length);
|
||||
Atomics.store(i32array, 2, messageBuffer.length);
|
||||
Atomics.store(i32array, 3, stackBuffer.length);
|
||||
var buffer = new Uint8Array(sab);
|
||||
buffer.set(nameBuffer, 16);
|
||||
buffer.set(messageBuffer, 16 + nameBuffer.length);
|
||||
buffer.set(stackBuffer, 16 + nameBuffer.length + messageBuffer.length);
|
||||
}
|
||||
}
|
||||
function deserizeErrorFromBuffer(sab) {
|
||||
var _a, _b;
|
||||
var i32array = new Int32Array(sab);
|
||||
var status = Atomics.load(i32array, 0);
|
||||
if (status <= 1) {
|
||||
return null;
|
||||
}
|
||||
var nameLength = Atomics.load(i32array, 1);
|
||||
var messageLength = Atomics.load(i32array, 2);
|
||||
var stackLength = Atomics.load(i32array, 3);
|
||||
var buffer = new Uint8Array(sab);
|
||||
var nameBuffer = buffer.slice(16, 16 + nameLength);
|
||||
var messageBuffer = buffer.slice(16 + nameLength, 16 + nameLength + messageLength);
|
||||
var stackBuffer = buffer.slice(16 + nameLength + messageLength, 16 + nameLength + messageLength + stackLength);
|
||||
var name = new TextDecoder().decode(nameBuffer);
|
||||
var message = new TextDecoder().decode(messageBuffer);
|
||||
var stack = new TextDecoder().decode(stackBuffer);
|
||||
var ErrorConstructor = (_a = globalThis[name]) !== null && _a !== void 0 ? _a : (name === 'RuntimeError' ? ((_b = _WebAssembly.RuntimeError) !== null && _b !== void 0 ? _b : Error) : Error);
|
||||
var error = new ErrorConstructor(message);
|
||||
Object.defineProperty(error, 'stack', {
|
||||
value: stack,
|
||||
writable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
return error;
|
||||
}
|
||||
/** @public */
|
||||
function isSharedArrayBuffer(value) {
|
||||
return ((typeof SharedArrayBuffer === 'function' && value instanceof SharedArrayBuffer) ||
|
||||
(Object.prototype.toString.call(value) === '[object SharedArrayBuffer]'));
|
||||
}
|
||||
/** @public */
|
||||
function isTrapError(e) {
|
||||
try {
|
||||
return e instanceof _WebAssembly.RuntimeError;
|
||||
}
|
||||
catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function createMessage(type, payload) {
|
||||
return {
|
||||
__emnapi__: {
|
||||
type: type,
|
||||
payload: payload
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var WASI_THREADS_MAX_TID = 0x1FFFFFFF;
|
||||
function checkSharedWasmMemory(wasmMemory) {
|
||||
if (wasmMemory) {
|
||||
if (!isSharedArrayBuffer(wasmMemory.buffer)) {
|
||||
throw new Error('Multithread features require shared wasm memory. ' +
|
||||
'Try to compile with `-matomics -mbulk-memory` and use `--import-memory --shared-memory` during linking, ' +
|
||||
'then create WebAssembly.Memory with `shared: true` option');
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (typeof SharedArrayBuffer === 'undefined') {
|
||||
throw new Error('Current environment does not support SharedArrayBuffer, threads are not available!');
|
||||
}
|
||||
}
|
||||
}
|
||||
function getReuseWorker(value) {
|
||||
var _a;
|
||||
if (typeof value === 'boolean') {
|
||||
return value ? { size: 0, strict: false } : false;
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
if (!(value >= 0)) {
|
||||
throw new RangeError('reuseWorker: size must be a non-negative integer');
|
||||
}
|
||||
return { size: value, strict: false };
|
||||
}
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
var size = (_a = Number(value.size)) !== null && _a !== void 0 ? _a : 0;
|
||||
var strict = Boolean(value.strict);
|
||||
if (!(size > 0) && strict) {
|
||||
throw new RangeError('reuseWorker: size must be set to positive integer if strict is set to true');
|
||||
}
|
||||
return { size: size, strict: strict };
|
||||
}
|
||||
var nextWorkerID = 0;
|
||||
/** @public */
|
||||
var ThreadManager = /*#__PURE__*/ (function () {
|
||||
function ThreadManager(options) {
|
||||
var _a;
|
||||
this.unusedWorkers = [];
|
||||
this.runningWorkers = [];
|
||||
this.pthreads = Object.create(null);
|
||||
this.wasmModule = null;
|
||||
this.wasmMemory = null;
|
||||
this.messageEvents = new WeakMap();
|
||||
if (!options) {
|
||||
throw new TypeError('ThreadManager(): options is not provided');
|
||||
}
|
||||
if ('childThread' in options) {
|
||||
this._childThread = Boolean(options.childThread);
|
||||
}
|
||||
else {
|
||||
this._childThread = false;
|
||||
}
|
||||
if (this._childThread) {
|
||||
this._onCreateWorker = undefined;
|
||||
this._reuseWorker = false;
|
||||
this._beforeLoad = undefined;
|
||||
}
|
||||
else {
|
||||
this._onCreateWorker = options.onCreateWorker;
|
||||
this._reuseWorker = getReuseWorker(options.reuseWorker);
|
||||
this._beforeLoad = options.beforeLoad;
|
||||
}
|
||||
this.printErr = (_a = options.printErr) !== null && _a !== void 0 ? _a : console.error.bind(console);
|
||||
}
|
||||
Object.defineProperty(ThreadManager.prototype, "nextWorkerID", {
|
||||
get: function () { return nextWorkerID; },
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
ThreadManager.prototype.init = function () {
|
||||
if (!this._childThread) {
|
||||
this.initMainThread();
|
||||
}
|
||||
};
|
||||
ThreadManager.prototype.initMainThread = function () {
|
||||
this.preparePool();
|
||||
};
|
||||
ThreadManager.prototype.preparePool = function () {
|
||||
if (this._reuseWorker) {
|
||||
if (this._reuseWorker.size) {
|
||||
var pthreadPoolSize = this._reuseWorker.size;
|
||||
while (pthreadPoolSize--) {
|
||||
var worker = this.allocateUnusedWorker();
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
// https://github.com/nodejs/node/issues/53036
|
||||
worker.once('message', function () { });
|
||||
worker.unref();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
ThreadManager.prototype.shouldPreloadWorkers = function () {
|
||||
return !this._childThread && this._reuseWorker && this._reuseWorker.size > 0;
|
||||
};
|
||||
ThreadManager.prototype.loadWasmModuleToAllWorkers = function () {
|
||||
var _this_1 = this;
|
||||
var promises = Array(this.unusedWorkers.length);
|
||||
var _loop_1 = function (i) {
|
||||
var worker = this_1.unusedWorkers[i];
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.ref();
|
||||
promises[i] = this_1.loadWasmModuleToWorker(worker).then(function (w) {
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.unref();
|
||||
return w;
|
||||
}, function (e) {
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.unref();
|
||||
throw e;
|
||||
});
|
||||
};
|
||||
var this_1 = this;
|
||||
for (var i = 0; i < this.unusedWorkers.length; ++i) {
|
||||
_loop_1(i);
|
||||
}
|
||||
return Promise.all(promises).catch(function (err) {
|
||||
_this_1.terminateAllThreads();
|
||||
throw err;
|
||||
});
|
||||
};
|
||||
ThreadManager.prototype.preloadWorkers = function () {
|
||||
if (this.shouldPreloadWorkers()) {
|
||||
return this.loadWasmModuleToAllWorkers();
|
||||
}
|
||||
return Promise.resolve([]);
|
||||
};
|
||||
ThreadManager.prototype.setup = function (wasmModule, wasmMemory) {
|
||||
this.wasmModule = wasmModule;
|
||||
this.wasmMemory = wasmMemory;
|
||||
};
|
||||
ThreadManager.prototype.markId = function (worker) {
|
||||
if (worker.__emnapi_tid)
|
||||
return worker.__emnapi_tid;
|
||||
var tid = nextWorkerID + 43;
|
||||
nextWorkerID = (nextWorkerID + 1) % (WASI_THREADS_MAX_TID - 42);
|
||||
this.pthreads[tid] = worker;
|
||||
worker.__emnapi_tid = tid;
|
||||
return tid;
|
||||
};
|
||||
ThreadManager.prototype.returnWorkerToPool = function (worker) {
|
||||
var tid = worker.__emnapi_tid;
|
||||
if (tid !== undefined) {
|
||||
delete this.pthreads[tid];
|
||||
}
|
||||
this.unusedWorkers.push(worker);
|
||||
this.runningWorkers.splice(this.runningWorkers.indexOf(worker), 1);
|
||||
delete worker.__emnapi_tid;
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.unref();
|
||||
}
|
||||
};
|
||||
ThreadManager.prototype.loadWasmModuleToWorker = function (worker, sab) {
|
||||
var _this_1 = this;
|
||||
if (worker.whenLoaded)
|
||||
return worker.whenLoaded;
|
||||
var err = this.printErr;
|
||||
var beforeLoad = this._beforeLoad;
|
||||
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
||||
var _this = this;
|
||||
worker.whenLoaded = new Promise(function (resolve, reject) {
|
||||
var handleError = function (e) {
|
||||
var message = 'worker sent an error!';
|
||||
if (worker.__emnapi_tid !== undefined) {
|
||||
message = 'worker (tid = ' + worker.__emnapi_tid + ') sent an error!';
|
||||
}
|
||||
if ('message' in e) {
|
||||
err(message + ' ' + e.message);
|
||||
if (e.message.indexOf('RuntimeError') !== -1 || e.message.indexOf('unreachable') !== -1) {
|
||||
try {
|
||||
_this.terminateAllThreads();
|
||||
}
|
||||
catch (_) { }
|
||||
}
|
||||
}
|
||||
else {
|
||||
err(message);
|
||||
}
|
||||
reject(e);
|
||||
throw e;
|
||||
};
|
||||
var handleMessage = function (data) {
|
||||
if (data.__emnapi__) {
|
||||
var type = data.__emnapi__.type;
|
||||
var payload = data.__emnapi__.payload;
|
||||
if (type === 'loaded') {
|
||||
worker.loaded = true;
|
||||
if (ENVIRONMENT_IS_NODE && !worker.__emnapi_tid) {
|
||||
worker.unref();
|
||||
}
|
||||
resolve(worker);
|
||||
// if (payload.err) {
|
||||
// err('failed to load in child thread: ' + (payload.err.message || payload.err))
|
||||
// }
|
||||
}
|
||||
else if (type === 'cleanup-thread') {
|
||||
if (payload.tid in _this_1.pthreads) {
|
||||
_this_1.cleanThread(worker, payload.tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
worker.onmessage = function (e) {
|
||||
handleMessage(e.data);
|
||||
_this_1.fireMessageEvent(worker, e);
|
||||
};
|
||||
worker.onerror = handleError;
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.on('message', function (data) {
|
||||
var _a, _b;
|
||||
(_b = (_a = worker).onmessage) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
||||
data: data
|
||||
});
|
||||
});
|
||||
worker.on('error', function (e) {
|
||||
var _a, _b;
|
||||
(_b = (_a = worker).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, e);
|
||||
});
|
||||
worker.on('detachedExit', function () { });
|
||||
}
|
||||
if (typeof beforeLoad === 'function') {
|
||||
beforeLoad(worker);
|
||||
}
|
||||
try {
|
||||
worker.postMessage(createMessage('load', {
|
||||
wasmModule: _this_1.wasmModule,
|
||||
wasmMemory: _this_1.wasmMemory,
|
||||
sab: sab
|
||||
}));
|
||||
}
|
||||
catch (err) {
|
||||
checkSharedWasmMemory(_this_1.wasmMemory);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
return worker.whenLoaded;
|
||||
};
|
||||
ThreadManager.prototype.allocateUnusedWorker = function () {
|
||||
var _onCreateWorker = this._onCreateWorker;
|
||||
if (typeof _onCreateWorker !== 'function') {
|
||||
throw new TypeError('`options.onCreateWorker` is not provided');
|
||||
}
|
||||
var worker = _onCreateWorker({ type: 'thread', name: 'emnapi-pthread' });
|
||||
this.unusedWorkers.push(worker);
|
||||
return worker;
|
||||
};
|
||||
ThreadManager.prototype.getNewWorker = function (sab) {
|
||||
if (this._reuseWorker) {
|
||||
if (this.unusedWorkers.length === 0) {
|
||||
if (this._reuseWorker.strict) {
|
||||
if (!ENVIRONMENT_IS_NODE) {
|
||||
var err = this.printErr;
|
||||
err('Tried to spawn a new thread, but the thread pool is exhausted.\n' +
|
||||
'This might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
var worker_1 = this.allocateUnusedWorker();
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
this.loadWasmModuleToWorker(worker_1, sab);
|
||||
}
|
||||
return this.unusedWorkers.pop();
|
||||
}
|
||||
var worker = this.allocateUnusedWorker();
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
this.loadWasmModuleToWorker(worker, sab);
|
||||
return this.unusedWorkers.pop();
|
||||
};
|
||||
ThreadManager.prototype.cleanThread = function (worker, tid, force) {
|
||||
if (!force && this._reuseWorker) {
|
||||
this.returnWorkerToPool(worker);
|
||||
}
|
||||
else {
|
||||
delete this.pthreads[tid];
|
||||
var index = this.runningWorkers.indexOf(worker);
|
||||
if (index !== -1) {
|
||||
this.runningWorkers.splice(index, 1);
|
||||
}
|
||||
this.terminateWorker(worker);
|
||||
delete worker.__emnapi_tid;
|
||||
}
|
||||
};
|
||||
ThreadManager.prototype.terminateWorker = function (worker) {
|
||||
var _this_1 = this;
|
||||
var _a;
|
||||
var tid = worker.__emnapi_tid;
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
worker.terminate();
|
||||
(_a = this.messageEvents.get(worker)) === null || _a === void 0 ? void 0 : _a.clear();
|
||||
this.messageEvents.delete(worker);
|
||||
worker.onmessage = function (e) {
|
||||
if (e.data.__emnapi__) {
|
||||
var err = _this_1.printErr;
|
||||
err('received "' + e.data.__emnapi__.type + '" command from terminated worker: ' + tid);
|
||||
}
|
||||
};
|
||||
};
|
||||
ThreadManager.prototype.terminateAllThreads = function () {
|
||||
for (var i = 0; i < this.runningWorkers.length; ++i) {
|
||||
this.terminateWorker(this.runningWorkers[i]);
|
||||
}
|
||||
for (var i = 0; i < this.unusedWorkers.length; ++i) {
|
||||
this.terminateWorker(this.unusedWorkers[i]);
|
||||
}
|
||||
this.unusedWorkers = [];
|
||||
this.runningWorkers = [];
|
||||
this.pthreads = Object.create(null);
|
||||
this.preparePool();
|
||||
};
|
||||
ThreadManager.prototype.addMessageEventListener = function (worker, onMessage) {
|
||||
var listeners = this.messageEvents.get(worker);
|
||||
if (!listeners) {
|
||||
listeners = new Set();
|
||||
this.messageEvents.set(worker, listeners);
|
||||
}
|
||||
listeners.add(onMessage);
|
||||
return function () {
|
||||
listeners === null || listeners === void 0 ? void 0 : listeners.delete(onMessage);
|
||||
};
|
||||
};
|
||||
ThreadManager.prototype.fireMessageEvent = function (worker, e) {
|
||||
var listeners = this.messageEvents.get(worker);
|
||||
if (!listeners)
|
||||
return;
|
||||
var err = this.printErr;
|
||||
listeners.forEach(function (listener) {
|
||||
try {
|
||||
listener(e);
|
||||
}
|
||||
catch (e) {
|
||||
err(e.stack);
|
||||
}
|
||||
});
|
||||
};
|
||||
return ThreadManager;
|
||||
}());
|
||||
|
||||
var kIsProxy = Symbol('kIsProxy');
|
||||
/** @public */
|
||||
function createInstanceProxy(instance, memory) {
|
||||
if (instance[kIsProxy])
|
||||
return instance;
|
||||
// https://github.com/nodejs/help/issues/4102
|
||||
var originalExports = instance.exports;
|
||||
var createHandler = function (target) {
|
||||
var handlers = [
|
||||
'apply',
|
||||
'construct',
|
||||
'defineProperty',
|
||||
'deleteProperty',
|
||||
'get',
|
||||
'getOwnPropertyDescriptor',
|
||||
'getPrototypeOf',
|
||||
'has',
|
||||
'isExtensible',
|
||||
'ownKeys',
|
||||
'preventExtensions',
|
||||
'set',
|
||||
'setPrototypeOf'
|
||||
];
|
||||
var handler = {};
|
||||
var _loop_1 = function (i) {
|
||||
var name_1 = handlers[i];
|
||||
handler[name_1] = function () {
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
args.unshift(target);
|
||||
return Reflect[name_1].apply(Reflect, args);
|
||||
};
|
||||
};
|
||||
for (var i = 0; i < handlers.length; i++) {
|
||||
_loop_1(i);
|
||||
}
|
||||
return handler;
|
||||
};
|
||||
var handler = createHandler(originalExports);
|
||||
var _initialize = function () { };
|
||||
var _start = function () { return 0; };
|
||||
handler.get = function (_target, p, receiver) {
|
||||
var _a;
|
||||
if (p === 'memory') {
|
||||
return (_a = (typeof memory === 'function' ? memory() : memory)) !== null && _a !== void 0 ? _a : Reflect.get(originalExports, p, receiver);
|
||||
}
|
||||
if (p === '_initialize') {
|
||||
return p in originalExports ? _initialize : undefined;
|
||||
}
|
||||
if (p === '_start') {
|
||||
return p in originalExports ? _start : undefined;
|
||||
}
|
||||
return Reflect.get(originalExports, p, receiver);
|
||||
};
|
||||
handler.has = function (_target, p) {
|
||||
if (p === 'memory')
|
||||
return true;
|
||||
return Reflect.has(originalExports, p);
|
||||
};
|
||||
var exportsProxy = new Proxy(Object.create(null), handler);
|
||||
return new Proxy(instance, {
|
||||
get: function (target, p, receiver) {
|
||||
if (p === 'exports') {
|
||||
return exportsProxy;
|
||||
}
|
||||
if (p === kIsProxy) {
|
||||
return true;
|
||||
}
|
||||
return Reflect.get(target, p, receiver);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var patchedWasiInstances = new WeakMap();
|
||||
/** @public */
|
||||
var WASIThreads = /*#__PURE__*/ (function () {
|
||||
function WASIThreads(options) {
|
||||
var _this_1 = this;
|
||||
if (!options) {
|
||||
throw new TypeError('WASIThreads(): options is not provided');
|
||||
}
|
||||
if (!options.wasi) {
|
||||
throw new TypeError('WASIThreads(): options.wasi is not provided');
|
||||
}
|
||||
patchedWasiInstances.set(this, new WeakSet());
|
||||
var wasi = options.wasi;
|
||||
patchWasiInstance(this, wasi);
|
||||
this.wasi = wasi;
|
||||
if ('childThread' in options) {
|
||||
this.childThread = Boolean(options.childThread);
|
||||
}
|
||||
else {
|
||||
this.childThread = false;
|
||||
}
|
||||
this.PThread = undefined;
|
||||
if ('threadManager' in options) {
|
||||
if (typeof options.threadManager === 'function') {
|
||||
this.PThread = options.threadManager();
|
||||
}
|
||||
else {
|
||||
this.PThread = options.threadManager;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!this.childThread) {
|
||||
this.PThread = new ThreadManager(options);
|
||||
this.PThread.init();
|
||||
}
|
||||
}
|
||||
var waitThreadStart = false;
|
||||
if ('waitThreadStart' in options) {
|
||||
waitThreadStart = typeof options.waitThreadStart === 'number' ? options.waitThreadStart : Boolean(options.waitThreadStart);
|
||||
}
|
||||
var postMessage = getPostMessage(options);
|
||||
if (this.childThread && typeof postMessage !== 'function') {
|
||||
throw new TypeError('options.postMessage is not a function');
|
||||
}
|
||||
this.postMessage = postMessage;
|
||||
var wasm64 = Boolean(options.wasm64);
|
||||
var onMessage = function (e) {
|
||||
if (e.data.__emnapi__) {
|
||||
var type = e.data.__emnapi__.type;
|
||||
var payload = e.data.__emnapi__.payload;
|
||||
if (type === 'spawn-thread') {
|
||||
threadSpawn(payload.startArg, payload.errorOrTid);
|
||||
}
|
||||
else if (type === 'terminate-all-threads') {
|
||||
_this_1.terminateAllThreads();
|
||||
}
|
||||
}
|
||||
};
|
||||
var threadSpawn = function (startArg, errorOrTid) {
|
||||
var _a;
|
||||
var EAGAIN = 6;
|
||||
var isNewABI = errorOrTid !== undefined;
|
||||
try {
|
||||
checkSharedWasmMemory(_this_1.wasmMemory);
|
||||
}
|
||||
catch (err) {
|
||||
(_a = _this_1.PThread) === null || _a === void 0 ? void 0 : _a.printErr(err.stack);
|
||||
if (isNewABI) {
|
||||
var struct_1 = new Int32Array(_this_1.wasmMemory.buffer, errorOrTid, 2);
|
||||
Atomics.store(struct_1, 0, 1);
|
||||
Atomics.store(struct_1, 1, EAGAIN);
|
||||
Atomics.notify(struct_1, 1);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return -EAGAIN;
|
||||
}
|
||||
}
|
||||
if (!isNewABI) {
|
||||
var malloc = _this_1.wasmInstance.exports.malloc;
|
||||
errorOrTid = wasm64 ? Number(malloc(BigInt(8))) : malloc(8);
|
||||
if (!errorOrTid) {
|
||||
return -48; /* ENOMEM */
|
||||
}
|
||||
}
|
||||
var _free = _this_1.wasmInstance.exports.free;
|
||||
var free = wasm64 ? function (ptr) { _free(BigInt(ptr)); } : _free;
|
||||
var struct = new Int32Array(_this_1.wasmMemory.buffer, errorOrTid, 2);
|
||||
Atomics.store(struct, 0, 0);
|
||||
Atomics.store(struct, 1, 0);
|
||||
if (_this_1.childThread) {
|
||||
postMessage(createMessage('spawn-thread', {
|
||||
startArg: startArg,
|
||||
errorOrTid: errorOrTid
|
||||
}));
|
||||
Atomics.wait(struct, 1, 0);
|
||||
var isError = Atomics.load(struct, 0);
|
||||
var result = Atomics.load(struct, 1);
|
||||
if (isNewABI) {
|
||||
return isError;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return isError ? -result : result;
|
||||
}
|
||||
var shouldWait = waitThreadStart || (waitThreadStart === 0);
|
||||
var sab;
|
||||
if (shouldWait) {
|
||||
sab = new Int32Array(new SharedArrayBuffer(16 + 8192));
|
||||
Atomics.store(sab, 0, 0);
|
||||
}
|
||||
var worker;
|
||||
var tid;
|
||||
var PThread = _this_1.PThread;
|
||||
try {
|
||||
worker = PThread.getNewWorker(sab);
|
||||
if (!worker) {
|
||||
throw new Error('failed to get new worker');
|
||||
}
|
||||
PThread.addMessageEventListener(worker, onMessage);
|
||||
tid = PThread.markId(worker);
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.ref();
|
||||
}
|
||||
worker.postMessage(createMessage('start', {
|
||||
tid: tid,
|
||||
arg: startArg,
|
||||
sab: sab
|
||||
}));
|
||||
if (shouldWait) {
|
||||
if (typeof waitThreadStart === 'number') {
|
||||
var waitResult = Atomics.wait(sab, 0, 0, waitThreadStart);
|
||||
if (waitResult === 'timed-out') {
|
||||
try {
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
}
|
||||
catch (_) { }
|
||||
throw new Error('Spawning thread timed out. Please check if the worker is created successfully and if message is handled properly in the worker.');
|
||||
}
|
||||
}
|
||||
else {
|
||||
Atomics.wait(sab, 0, 0);
|
||||
}
|
||||
var r = Atomics.load(sab, 0);
|
||||
if (r > 1) {
|
||||
try {
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
}
|
||||
catch (_) { }
|
||||
throw deserizeErrorFromBuffer(sab.buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
Atomics.store(struct, 0, 1);
|
||||
Atomics.store(struct, 1, EAGAIN);
|
||||
Atomics.notify(struct, 1);
|
||||
PThread === null || PThread === void 0 ? void 0 : PThread.printErr(e.stack);
|
||||
if (isNewABI) {
|
||||
return 1;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return -EAGAIN;
|
||||
}
|
||||
Atomics.store(struct, 0, 0);
|
||||
Atomics.store(struct, 1, tid);
|
||||
Atomics.notify(struct, 1);
|
||||
PThread.runningWorkers.push(worker);
|
||||
if (!shouldWait) {
|
||||
worker.whenLoaded.catch(function (err) {
|
||||
delete worker.whenLoaded;
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
if (isNewABI) {
|
||||
return 0;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return tid;
|
||||
};
|
||||
this.threadSpawn = threadSpawn;
|
||||
}
|
||||
WASIThreads.prototype.getImportObject = function () {
|
||||
return {
|
||||
wasi: {
|
||||
'thread-spawn': this.threadSpawn
|
||||
}
|
||||
};
|
||||
};
|
||||
WASIThreads.prototype.setup = function (wasmInstance, wasmModule, wasmMemory) {
|
||||
wasmMemory !== null && wasmMemory !== void 0 ? wasmMemory : (wasmMemory = wasmInstance.exports.memory);
|
||||
this.wasmInstance = wasmInstance;
|
||||
this.wasmMemory = wasmMemory;
|
||||
if (this.PThread) {
|
||||
this.PThread.setup(wasmModule, wasmMemory);
|
||||
}
|
||||
};
|
||||
WASIThreads.prototype.preloadWorkers = function () {
|
||||
if (this.PThread) {
|
||||
return this.PThread.preloadWorkers();
|
||||
}
|
||||
return Promise.resolve([]);
|
||||
};
|
||||
/**
|
||||
* It's ok to call this method to a WASI command module.
|
||||
*
|
||||
* in child thread, must call this method instead of {@link WASIThreads.start} even if it's a WASI command module
|
||||
*
|
||||
* @returns A proxied WebAssembly instance if in child thread, other wise the original instance
|
||||
*/
|
||||
WASIThreads.prototype.initialize = function (instance, module, memory) {
|
||||
var exports = instance.exports;
|
||||
memory !== null && memory !== void 0 ? memory : (memory = exports.memory);
|
||||
if (this.childThread) {
|
||||
instance = createInstanceProxy(instance, memory);
|
||||
}
|
||||
this.setup(instance, module, memory);
|
||||
var wasi = this.wasi;
|
||||
if (('_start' in exports) && (typeof exports._start === 'function')) {
|
||||
if (this.childThread) {
|
||||
wasi.start(instance);
|
||||
try {
|
||||
var kStarted = getWasiSymbol(wasi, 'kStarted');
|
||||
wasi[kStarted] = false;
|
||||
}
|
||||
catch (_) { }
|
||||
}
|
||||
else {
|
||||
setupInstance(wasi, instance);
|
||||
}
|
||||
}
|
||||
else {
|
||||
wasi.initialize(instance);
|
||||
}
|
||||
return instance;
|
||||
};
|
||||
/**
|
||||
* Equivalent to calling {@link WASIThreads.initialize} and then calling {@link WASIInstance.start}
|
||||
* ```js
|
||||
* this.initialize(instance, module, memory)
|
||||
* this.wasi.start(instance)
|
||||
* ```
|
||||
*/
|
||||
WASIThreads.prototype.start = function (instance, module, memory) {
|
||||
var exports = instance.exports;
|
||||
memory !== null && memory !== void 0 ? memory : (memory = exports.memory);
|
||||
if (this.childThread) {
|
||||
instance = createInstanceProxy(instance, memory);
|
||||
}
|
||||
this.setup(instance, module, memory);
|
||||
var exitCode = this.wasi.start(instance);
|
||||
return { exitCode: exitCode, instance: instance };
|
||||
};
|
||||
WASIThreads.prototype.terminateAllThreads = function () {
|
||||
var _a;
|
||||
if (!this.childThread) {
|
||||
(_a = this.PThread) === null || _a === void 0 ? void 0 : _a.terminateAllThreads();
|
||||
}
|
||||
else {
|
||||
this.postMessage(createMessage('terminate-all-threads', {}));
|
||||
}
|
||||
};
|
||||
return WASIThreads;
|
||||
}());
|
||||
function patchWasiInstance(wasiThreads, wasi) {
|
||||
var patched = patchedWasiInstances.get(wasiThreads);
|
||||
if (patched.has(wasi)) {
|
||||
return;
|
||||
}
|
||||
var _this = wasiThreads;
|
||||
var wasiImport = wasi.wasiImport;
|
||||
if (wasiImport) {
|
||||
var proc_exit_1 = wasiImport.proc_exit;
|
||||
wasiImport.proc_exit = function (code) {
|
||||
_this.terminateAllThreads();
|
||||
return proc_exit_1.call(this, code);
|
||||
};
|
||||
}
|
||||
if (!_this.childThread) {
|
||||
var start_1 = wasi.start;
|
||||
if (typeof start_1 === 'function') {
|
||||
wasi.start = function (instance) {
|
||||
try {
|
||||
return start_1.call(this, instance);
|
||||
}
|
||||
catch (err) {
|
||||
if (isTrapError(err)) {
|
||||
_this.terminateAllThreads();
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
patched.add(wasi);
|
||||
}
|
||||
function getWasiSymbol(wasi, description) {
|
||||
var symbols = Object.getOwnPropertySymbols(wasi);
|
||||
var selectDescription = function (description) { return function (s) {
|
||||
if (s.description) {
|
||||
return s.description === description;
|
||||
}
|
||||
return s.toString() === "Symbol(".concat(description, ")");
|
||||
}; };
|
||||
if (Array.isArray(description)) {
|
||||
return description.map(function (d) { return symbols.filter(selectDescription(d))[0]; });
|
||||
}
|
||||
return symbols.filter(selectDescription(description))[0];
|
||||
}
|
||||
function setupInstance(wasi, instance) {
|
||||
var _a = getWasiSymbol(wasi, ['kInstance', 'kSetMemory']), kInstance = _a[0], kSetMemory = _a[1];
|
||||
wasi[kInstance] = instance;
|
||||
wasi[kSetMemory](instance.exports.memory);
|
||||
}
|
||||
|
||||
/** @public */
|
||||
var ThreadMessageHandler = /*#__PURE__*/ (function () {
|
||||
function ThreadMessageHandler(options) {
|
||||
var postMsg = getPostMessage(options);
|
||||
if (typeof postMsg !== 'function') {
|
||||
throw new TypeError('options.postMessage is not a function');
|
||||
}
|
||||
this.postMessage = postMsg;
|
||||
this.onLoad = options === null || options === void 0 ? void 0 : options.onLoad;
|
||||
this.onError = typeof (options === null || options === void 0 ? void 0 : options.onError) === 'function' ? options.onError : function (_type, err) { throw err; };
|
||||
this.instance = undefined;
|
||||
// this.module = undefined
|
||||
this.messagesBeforeLoad = [];
|
||||
}
|
||||
/** @virtual */
|
||||
ThreadMessageHandler.prototype.instantiate = function (data) {
|
||||
if (typeof this.onLoad === 'function') {
|
||||
return this.onLoad(data);
|
||||
}
|
||||
throw new Error('ThreadMessageHandler.prototype.instantiate is not implemented');
|
||||
};
|
||||
/** @virtual */
|
||||
ThreadMessageHandler.prototype.handle = function (e) {
|
||||
var _this = this;
|
||||
var _a;
|
||||
if ((_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.__emnapi__) {
|
||||
var type = e.data.__emnapi__.type;
|
||||
var payload_1 = e.data.__emnapi__.payload;
|
||||
try {
|
||||
if (type === 'load') {
|
||||
this._load(payload_1);
|
||||
}
|
||||
else if (type === 'start') {
|
||||
this.handleAfterLoad(e, function () {
|
||||
_this._start(payload_1);
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
this.onError(err, type);
|
||||
}
|
||||
}
|
||||
};
|
||||
ThreadMessageHandler.prototype._load = function (payload) {
|
||||
var _this = this;
|
||||
if (this.instance !== undefined)
|
||||
return;
|
||||
var source;
|
||||
try {
|
||||
source = this.instantiate(payload);
|
||||
}
|
||||
catch (err) {
|
||||
this._loaded(err, null, payload);
|
||||
return;
|
||||
}
|
||||
var then = source && 'then' in source ? source.then : undefined;
|
||||
if (typeof then === 'function') {
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
then.call(source, function (source) { _this._loaded(null, source, payload); }, function (err) { _this._loaded(err, null, payload); });
|
||||
}
|
||||
else {
|
||||
this._loaded(null, source, payload);
|
||||
}
|
||||
};
|
||||
ThreadMessageHandler.prototype._start = function (payload) {
|
||||
var wasi_thread_start = this.instance.exports.wasi_thread_start;
|
||||
if (typeof wasi_thread_start !== 'function') {
|
||||
var err = new TypeError('wasi_thread_start is not exported');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
var postMessage = this.postMessage;
|
||||
var tid = payload.tid;
|
||||
var startArg = payload.arg;
|
||||
notifyPthreadCreateResult(payload.sab, 1);
|
||||
try {
|
||||
wasi_thread_start(tid, startArg);
|
||||
}
|
||||
catch (err) {
|
||||
if (err !== 'unwind') {
|
||||
throw err;
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
postMessage(createMessage('cleanup-thread', { tid: tid }));
|
||||
};
|
||||
ThreadMessageHandler.prototype._loaded = function (err, source, payload) {
|
||||
if (err) {
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
if (source == null) {
|
||||
var err_1 = new TypeError('onLoad should return an object');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err_1);
|
||||
throw err_1;
|
||||
}
|
||||
var instance = source.instance;
|
||||
if (!instance) {
|
||||
var err_2 = new TypeError('onLoad should return an object which includes "instance"');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err_2);
|
||||
throw err_2;
|
||||
}
|
||||
this.instance = instance;
|
||||
var postMessage = this.postMessage;
|
||||
postMessage(createMessage('loaded', {}));
|
||||
var messages = this.messagesBeforeLoad;
|
||||
this.messagesBeforeLoad = [];
|
||||
for (var i = 0; i < messages.length; i++) {
|
||||
var data = messages[i];
|
||||
this.handle({ data: data });
|
||||
}
|
||||
};
|
||||
ThreadMessageHandler.prototype.handleAfterLoad = function (e, f) {
|
||||
if (this.instance !== undefined) {
|
||||
f.call(this, e);
|
||||
}
|
||||
else {
|
||||
this.messagesBeforeLoad.push(e.data);
|
||||
}
|
||||
};
|
||||
return ThreadMessageHandler;
|
||||
}());
|
||||
function notifyPthreadCreateResult(sab, result, error) {
|
||||
if (sab) {
|
||||
serizeErrorToBuffer(sab.buffer, result, error);
|
||||
Atomics.notify(sab, 0);
|
||||
}
|
||||
}
|
||||
|
||||
exports.ThreadManager = ThreadManager;
|
||||
exports.ThreadMessageHandler = ThreadMessageHandler;
|
||||
exports.WASIThreads = WASIThreads;
|
||||
exports.createInstanceProxy = createInstanceProxy;
|
||||
exports.isSharedArrayBuffer = isSharedArrayBuffer;
|
||||
exports.isTrapError = isTrapError;
|
||||
|
||||
}));
|
||||
270
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.d.mts
generated
vendored
Normal file
270
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.d.mts
generated
vendored
Normal file
|
|
@ -0,0 +1,270 @@
|
|||
/// <reference types="node" />
|
||||
|
||||
import type { Worker as Worker_2 } from 'worker_threads';
|
||||
|
||||
/** @public */
|
||||
export declare interface BaseOptions {
|
||||
wasi: WASIInstance;
|
||||
version?: 'preview1';
|
||||
wasm64?: boolean;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ChildThreadOptions extends BaseOptions {
|
||||
childThread: true;
|
||||
postMessage?: (data: any) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CleanupThreadPayload {
|
||||
tid: number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CommandInfo<T extends CommandType> {
|
||||
type: T;
|
||||
payload: CommandPayloadMap[T];
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface CommandPayloadMap {
|
||||
load: LoadPayload;
|
||||
loaded: LoadedPayload;
|
||||
start: StartPayload;
|
||||
'cleanup-thread': CleanupThreadPayload;
|
||||
'terminate-all-threads': TerminateAllThreadsPayload;
|
||||
'spawn-thread': SpawnThreadPayload;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type CommandType = keyof CommandPayloadMap;
|
||||
|
||||
/** @public */
|
||||
export declare function createInstanceProxy(instance: WebAssembly.Instance, memory?: WebAssembly.Memory | (() => WebAssembly.Memory)): WebAssembly.Instance;
|
||||
|
||||
/** @public */
|
||||
export declare function isSharedArrayBuffer(value: any): value is SharedArrayBuffer;
|
||||
|
||||
/** @public */
|
||||
export declare function isTrapError(e: Error): e is WebAssembly.RuntimeError;
|
||||
|
||||
/** @public */
|
||||
export declare interface LoadedPayload {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface LoadPayload {
|
||||
wasmModule: WebAssembly.Module;
|
||||
wasmMemory: WebAssembly.Memory;
|
||||
sab?: Int32Array;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadBaseOptions extends BaseOptions {
|
||||
waitThreadStart?: boolean | number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type MainThreadOptions = MainThreadOptionsWithThreadManager | MainThreadOptionsCreateThreadManager;
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadOptionsCreateThreadManager extends MainThreadBaseOptions, ThreadManagerOptionsMain {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MainThreadOptionsWithThreadManager extends MainThreadBaseOptions {
|
||||
threadManager?: ThreadManager | (() => ThreadManager);
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface MessageEventData<T extends CommandType> {
|
||||
__emnapi__: CommandInfo<T>;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ReuseWorkerOptions {
|
||||
/**
|
||||
* @see {@link https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size | PTHREAD_POOL_SIZE}
|
||||
*/
|
||||
size: number;
|
||||
/**
|
||||
* @see {@link https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size-strict | PTHREAD_POOL_SIZE_STRICT}
|
||||
*/
|
||||
strict?: boolean;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface SpawnThreadPayload {
|
||||
startArg: number;
|
||||
errorOrTid: number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface StartPayload {
|
||||
tid: number;
|
||||
arg: number;
|
||||
sab?: Int32Array;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface StartResult {
|
||||
exitCode: number;
|
||||
instance: WebAssembly.Instance;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface TerminateAllThreadsPayload {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class ThreadManager {
|
||||
unusedWorkers: WorkerLike[];
|
||||
runningWorkers: WorkerLike[];
|
||||
pthreads: Record<number, WorkerLike>;
|
||||
get nextWorkerID(): number;
|
||||
wasmModule: WebAssembly.Module | null;
|
||||
wasmMemory: WebAssembly.Memory | null;
|
||||
private readonly messageEvents;
|
||||
private readonly _childThread;
|
||||
private readonly _onCreateWorker;
|
||||
private readonly _reuseWorker;
|
||||
private readonly _beforeLoad?;
|
||||
/* Excluded from this release type: printErr */
|
||||
constructor(options: ThreadManagerOptions);
|
||||
init(): void;
|
||||
initMainThread(): void;
|
||||
private preparePool;
|
||||
shouldPreloadWorkers(): boolean;
|
||||
loadWasmModuleToAllWorkers(): Promise<WorkerLike[]>;
|
||||
preloadWorkers(): Promise<WorkerLike[]>;
|
||||
setup(wasmModule: WebAssembly.Module, wasmMemory: WebAssembly.Memory): void;
|
||||
markId(worker: WorkerLike): number;
|
||||
returnWorkerToPool(worker: WorkerLike): void;
|
||||
loadWasmModuleToWorker(worker: WorkerLike, sab?: Int32Array): Promise<WorkerLike>;
|
||||
allocateUnusedWorker(): WorkerLike;
|
||||
getNewWorker(sab?: Int32Array): WorkerLike | undefined;
|
||||
cleanThread(worker: WorkerLike, tid: number, force?: boolean): void;
|
||||
terminateWorker(worker: WorkerLike): void;
|
||||
terminateAllThreads(): void;
|
||||
addMessageEventListener(worker: WorkerLike, onMessage: (e: WorkerMessageEvent) => void): () => void;
|
||||
fireMessageEvent(worker: WorkerLike, e: WorkerMessageEvent): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type ThreadManagerOptions = ThreadManagerOptionsMain | ThreadManagerOptionsChild;
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsBase {
|
||||
printErr?: (message: string) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsChild extends ThreadManagerOptionsBase {
|
||||
childThread: true;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadManagerOptionsMain extends ThreadManagerOptionsBase {
|
||||
beforeLoad?: (worker: WorkerLike) => any;
|
||||
reuseWorker?: boolean | number | ReuseWorkerOptions;
|
||||
onCreateWorker: WorkerFactory;
|
||||
childThread?: false;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class ThreadMessageHandler {
|
||||
protected instance: WebAssembly.Instance | undefined;
|
||||
private messagesBeforeLoad;
|
||||
protected postMessage: (message: any) => void;
|
||||
protected onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
protected onError: (error: Error, type: WorkerMessageType) => void;
|
||||
constructor(options?: ThreadMessageHandlerOptions);
|
||||
/** @virtual */
|
||||
instantiate(data: LoadPayload): WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
/** @virtual */
|
||||
handle(e: WorkerMessageEvent<MessageEventData<WorkerMessageType>>): void;
|
||||
private _load;
|
||||
private _start;
|
||||
protected _loaded(err: Error | null, source: WebAssembly.WebAssemblyInstantiatedSource | null, payload: LoadPayload): void;
|
||||
protected handleAfterLoad<E extends WorkerMessageEvent>(e: E, f: (e: E) => void): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface ThreadMessageHandlerOptions {
|
||||
onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
||||
onError?: (error: Error, type: WorkerMessageType) => void;
|
||||
postMessage?: (message: any) => void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface WASIInstance {
|
||||
readonly wasiImport?: Record<string, any>;
|
||||
initialize(instance: object): void;
|
||||
start(instance: object): number;
|
||||
getImportObject?(): any;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare class WASIThreads {
|
||||
PThread: ThreadManager | undefined;
|
||||
private wasmMemory;
|
||||
private wasmInstance;
|
||||
private readonly threadSpawn;
|
||||
readonly childThread: boolean;
|
||||
private readonly postMessage;
|
||||
readonly wasi: WASIInstance;
|
||||
constructor(options: WASIThreadsOptions);
|
||||
getImportObject(): {
|
||||
wasi: WASIThreadsImports;
|
||||
};
|
||||
setup(wasmInstance: WebAssembly.Instance, wasmModule: WebAssembly.Module, wasmMemory?: WebAssembly.Memory): void;
|
||||
preloadWorkers(): Promise<WorkerLike[]>;
|
||||
/**
|
||||
* It's ok to call this method to a WASI command module.
|
||||
*
|
||||
* in child thread, must call this method instead of {@link WASIThreads.start} even if it's a WASI command module
|
||||
*
|
||||
* @returns A proxied WebAssembly instance if in child thread, other wise the original instance
|
||||
*/
|
||||
initialize(instance: WebAssembly.Instance, module: WebAssembly.Module, memory?: WebAssembly.Memory): WebAssembly.Instance;
|
||||
/**
|
||||
* Equivalent to calling {@link WASIThreads.initialize} and then calling {@link WASIInstance.start}
|
||||
* ```js
|
||||
* this.initialize(instance, module, memory)
|
||||
* this.wasi.start(instance)
|
||||
* ```
|
||||
*/
|
||||
start(instance: WebAssembly.Instance, module: WebAssembly.Module, memory?: WebAssembly.Memory): StartResult;
|
||||
terminateAllThreads(): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare interface WASIThreadsImports {
|
||||
'thread-spawn': (startArg: number, errorOrTid?: number) => number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type WASIThreadsOptions = MainThreadOptions | ChildThreadOptions;
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerFactory = (ctx: {
|
||||
type: string;
|
||||
name: string;
|
||||
}) => WorkerLike;
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerLike = (Worker | Worker_2) & {
|
||||
whenLoaded?: Promise<WorkerLike>;
|
||||
loaded?: boolean;
|
||||
__emnapi_tid?: number;
|
||||
};
|
||||
|
||||
/** @public */
|
||||
export declare interface WorkerMessageEvent<T = any> {
|
||||
data: T;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export declare type WorkerMessageType = 'load' | 'start';
|
||||
|
||||
export { }
|
||||
1
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.js
generated
vendored
Normal file
1
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
1
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.mjs
generated
vendored
Normal file
1
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.mjs
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
892
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.mjs
generated
vendored
Normal file
892
Frontend-Learner/node_modules/@emnapi/wasi-threads/dist/wasi-threads.mjs
generated
vendored
Normal file
|
|
@ -0,0 +1,892 @@
|
|||
const _WebAssembly = typeof WebAssembly !== 'undefined'
|
||||
? WebAssembly
|
||||
: typeof WXWebAssembly !== 'undefined'
|
||||
? WXWebAssembly
|
||||
: undefined;
|
||||
const ENVIRONMENT_IS_NODE = typeof process === 'object' && process !== null &&
|
||||
typeof process.versions === 'object' && process.versions !== null &&
|
||||
typeof process.versions.node === 'string';
|
||||
function getPostMessage(options) {
|
||||
return typeof (options === null || options === void 0 ? void 0 : options.postMessage) === 'function'
|
||||
? options.postMessage
|
||||
: typeof postMessage === 'function'
|
||||
? postMessage
|
||||
: undefined;
|
||||
}
|
||||
function serizeErrorToBuffer(sab, code, error) {
|
||||
const i32array = new Int32Array(sab);
|
||||
Atomics.store(i32array, 0, code);
|
||||
if (code > 1 && error) {
|
||||
const name = error.name;
|
||||
const message = error.message;
|
||||
const stack = error.stack;
|
||||
const nameBuffer = new TextEncoder().encode(name);
|
||||
const messageBuffer = new TextEncoder().encode(message);
|
||||
const stackBuffer = new TextEncoder().encode(stack);
|
||||
Atomics.store(i32array, 1, nameBuffer.length);
|
||||
Atomics.store(i32array, 2, messageBuffer.length);
|
||||
Atomics.store(i32array, 3, stackBuffer.length);
|
||||
const buffer = new Uint8Array(sab);
|
||||
buffer.set(nameBuffer, 16);
|
||||
buffer.set(messageBuffer, 16 + nameBuffer.length);
|
||||
buffer.set(stackBuffer, 16 + nameBuffer.length + messageBuffer.length);
|
||||
}
|
||||
}
|
||||
function deserizeErrorFromBuffer(sab) {
|
||||
var _a, _b;
|
||||
const i32array = new Int32Array(sab);
|
||||
const status = Atomics.load(i32array, 0);
|
||||
if (status <= 1) {
|
||||
return null;
|
||||
}
|
||||
const nameLength = Atomics.load(i32array, 1);
|
||||
const messageLength = Atomics.load(i32array, 2);
|
||||
const stackLength = Atomics.load(i32array, 3);
|
||||
const buffer = new Uint8Array(sab);
|
||||
const nameBuffer = buffer.slice(16, 16 + nameLength);
|
||||
const messageBuffer = buffer.slice(16 + nameLength, 16 + nameLength + messageLength);
|
||||
const stackBuffer = buffer.slice(16 + nameLength + messageLength, 16 + nameLength + messageLength + stackLength);
|
||||
const name = new TextDecoder().decode(nameBuffer);
|
||||
const message = new TextDecoder().decode(messageBuffer);
|
||||
const stack = new TextDecoder().decode(stackBuffer);
|
||||
const ErrorConstructor = (_a = globalThis[name]) !== null && _a !== void 0 ? _a : (name === 'RuntimeError' ? ((_b = _WebAssembly.RuntimeError) !== null && _b !== void 0 ? _b : Error) : Error);
|
||||
const error = new ErrorConstructor(message);
|
||||
Object.defineProperty(error, 'stack', {
|
||||
value: stack,
|
||||
writable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
return error;
|
||||
}
|
||||
function isSharedArrayBuffer(value) {
|
||||
return ((typeof SharedArrayBuffer === 'function' && value instanceof SharedArrayBuffer) ||
|
||||
(Object.prototype.toString.call(value) === '[object SharedArrayBuffer]'));
|
||||
}
|
||||
function isTrapError(e) {
|
||||
try {
|
||||
return e instanceof _WebAssembly.RuntimeError;
|
||||
}
|
||||
catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function createMessage(type, payload) {
|
||||
return {
|
||||
__emnapi__: {
|
||||
type,
|
||||
payload
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const WASI_THREADS_MAX_TID = 0x1FFFFFFF;
|
||||
function checkSharedWasmMemory(wasmMemory) {
|
||||
if (wasmMemory) {
|
||||
if (!isSharedArrayBuffer(wasmMemory.buffer)) {
|
||||
throw new Error('Multithread features require shared wasm memory. ' +
|
||||
'Try to compile with `-matomics -mbulk-memory` and use `--import-memory --shared-memory` during linking, ' +
|
||||
'then create WebAssembly.Memory with `shared: true` option');
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (typeof SharedArrayBuffer === 'undefined') {
|
||||
throw new Error('Current environment does not support SharedArrayBuffer, threads are not available!');
|
||||
}
|
||||
}
|
||||
}
|
||||
function getReuseWorker(value) {
|
||||
var _a;
|
||||
if (typeof value === 'boolean') {
|
||||
return value ? { size: 0, strict: false } : false;
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
if (!(value >= 0)) {
|
||||
throw new RangeError('reuseWorker: size must be a non-negative integer');
|
||||
}
|
||||
return { size: value, strict: false };
|
||||
}
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
const size = (_a = Number(value.size)) !== null && _a !== void 0 ? _a : 0;
|
||||
const strict = Boolean(value.strict);
|
||||
if (!(size > 0) && strict) {
|
||||
throw new RangeError('reuseWorker: size must be set to positive integer if strict is set to true');
|
||||
}
|
||||
return { size, strict };
|
||||
}
|
||||
let nextWorkerID = 0;
|
||||
class ThreadManager {
|
||||
get nextWorkerID() { return nextWorkerID; }
|
||||
constructor(options) {
|
||||
var _a;
|
||||
this.unusedWorkers = [];
|
||||
this.runningWorkers = [];
|
||||
this.pthreads = Object.create(null);
|
||||
this.wasmModule = null;
|
||||
this.wasmMemory = null;
|
||||
this.messageEvents = new WeakMap();
|
||||
if (!options) {
|
||||
throw new TypeError('ThreadManager(): options is not provided');
|
||||
}
|
||||
if ('childThread' in options) {
|
||||
this._childThread = Boolean(options.childThread);
|
||||
}
|
||||
else {
|
||||
this._childThread = false;
|
||||
}
|
||||
if (this._childThread) {
|
||||
this._onCreateWorker = undefined;
|
||||
this._reuseWorker = false;
|
||||
this._beforeLoad = undefined;
|
||||
}
|
||||
else {
|
||||
this._onCreateWorker = options.onCreateWorker;
|
||||
this._reuseWorker = getReuseWorker(options.reuseWorker);
|
||||
this._beforeLoad = options.beforeLoad;
|
||||
}
|
||||
this.printErr = (_a = options.printErr) !== null && _a !== void 0 ? _a : console.error.bind(console);
|
||||
}
|
||||
init() {
|
||||
if (!this._childThread) {
|
||||
this.initMainThread();
|
||||
}
|
||||
}
|
||||
initMainThread() {
|
||||
this.preparePool();
|
||||
}
|
||||
preparePool() {
|
||||
if (this._reuseWorker) {
|
||||
if (this._reuseWorker.size) {
|
||||
let pthreadPoolSize = this._reuseWorker.size;
|
||||
while (pthreadPoolSize--) {
|
||||
const worker = this.allocateUnusedWorker();
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.once('message', () => { });
|
||||
worker.unref();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
shouldPreloadWorkers() {
|
||||
return !this._childThread && this._reuseWorker && this._reuseWorker.size > 0;
|
||||
}
|
||||
loadWasmModuleToAllWorkers() {
|
||||
const promises = Array(this.unusedWorkers.length);
|
||||
for (let i = 0; i < this.unusedWorkers.length; ++i) {
|
||||
const worker = this.unusedWorkers[i];
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.ref();
|
||||
promises[i] = this.loadWasmModuleToWorker(worker).then((w) => {
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.unref();
|
||||
return w;
|
||||
}, (e) => {
|
||||
if (ENVIRONMENT_IS_NODE)
|
||||
worker.unref();
|
||||
throw e;
|
||||
});
|
||||
}
|
||||
return Promise.all(promises).catch((err) => {
|
||||
this.terminateAllThreads();
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
preloadWorkers() {
|
||||
if (this.shouldPreloadWorkers()) {
|
||||
return this.loadWasmModuleToAllWorkers();
|
||||
}
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
setup(wasmModule, wasmMemory) {
|
||||
this.wasmModule = wasmModule;
|
||||
this.wasmMemory = wasmMemory;
|
||||
}
|
||||
markId(worker) {
|
||||
if (worker.__emnapi_tid)
|
||||
return worker.__emnapi_tid;
|
||||
const tid = nextWorkerID + 43;
|
||||
nextWorkerID = (nextWorkerID + 1) % (WASI_THREADS_MAX_TID - 42);
|
||||
this.pthreads[tid] = worker;
|
||||
worker.__emnapi_tid = tid;
|
||||
return tid;
|
||||
}
|
||||
returnWorkerToPool(worker) {
|
||||
var tid = worker.__emnapi_tid;
|
||||
if (tid !== undefined) {
|
||||
delete this.pthreads[tid];
|
||||
}
|
||||
this.unusedWorkers.push(worker);
|
||||
this.runningWorkers.splice(this.runningWorkers.indexOf(worker), 1);
|
||||
delete worker.__emnapi_tid;
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.unref();
|
||||
}
|
||||
}
|
||||
loadWasmModuleToWorker(worker, sab) {
|
||||
if (worker.whenLoaded)
|
||||
return worker.whenLoaded;
|
||||
const err = this.printErr;
|
||||
const beforeLoad = this._beforeLoad;
|
||||
const _this = this;
|
||||
worker.whenLoaded = new Promise((resolve, reject) => {
|
||||
const handleError = function (e) {
|
||||
let message = 'worker sent an error!';
|
||||
if (worker.__emnapi_tid !== undefined) {
|
||||
message = 'worker (tid = ' + worker.__emnapi_tid + ') sent an error!';
|
||||
}
|
||||
if ('message' in e) {
|
||||
err(message + ' ' + e.message);
|
||||
if (e.message.indexOf('RuntimeError') !== -1 || e.message.indexOf('unreachable') !== -1) {
|
||||
try {
|
||||
_this.terminateAllThreads();
|
||||
}
|
||||
catch (_) { }
|
||||
}
|
||||
}
|
||||
else {
|
||||
err(message);
|
||||
}
|
||||
reject(e);
|
||||
throw e;
|
||||
};
|
||||
const handleMessage = (data) => {
|
||||
if (data.__emnapi__) {
|
||||
const type = data.__emnapi__.type;
|
||||
const payload = data.__emnapi__.payload;
|
||||
if (type === 'loaded') {
|
||||
worker.loaded = true;
|
||||
if (ENVIRONMENT_IS_NODE && !worker.__emnapi_tid) {
|
||||
worker.unref();
|
||||
}
|
||||
resolve(worker);
|
||||
}
|
||||
else if (type === 'cleanup-thread') {
|
||||
if (payload.tid in this.pthreads) {
|
||||
this.cleanThread(worker, payload.tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
worker.onmessage = (e) => {
|
||||
handleMessage(e.data);
|
||||
this.fireMessageEvent(worker, e);
|
||||
};
|
||||
worker.onerror = handleError;
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.on('message', function (data) {
|
||||
var _a, _b;
|
||||
(_b = (_a = worker).onmessage) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
||||
data
|
||||
});
|
||||
});
|
||||
worker.on('error', function (e) {
|
||||
var _a, _b;
|
||||
(_b = (_a = worker).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, e);
|
||||
});
|
||||
worker.on('detachedExit', function () { });
|
||||
}
|
||||
if (typeof beforeLoad === 'function') {
|
||||
beforeLoad(worker);
|
||||
}
|
||||
try {
|
||||
worker.postMessage(createMessage('load', {
|
||||
wasmModule: this.wasmModule,
|
||||
wasmMemory: this.wasmMemory,
|
||||
sab
|
||||
}));
|
||||
}
|
||||
catch (err) {
|
||||
checkSharedWasmMemory(this.wasmMemory);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
return worker.whenLoaded;
|
||||
}
|
||||
allocateUnusedWorker() {
|
||||
const _onCreateWorker = this._onCreateWorker;
|
||||
if (typeof _onCreateWorker !== 'function') {
|
||||
throw new TypeError('`options.onCreateWorker` is not provided');
|
||||
}
|
||||
const worker = _onCreateWorker({ type: 'thread', name: 'emnapi-pthread' });
|
||||
this.unusedWorkers.push(worker);
|
||||
return worker;
|
||||
}
|
||||
getNewWorker(sab) {
|
||||
if (this._reuseWorker) {
|
||||
if (this.unusedWorkers.length === 0) {
|
||||
if (this._reuseWorker.strict) {
|
||||
if (!ENVIRONMENT_IS_NODE) {
|
||||
const err = this.printErr;
|
||||
err('Tried to spawn a new thread, but the thread pool is exhausted.\n' +
|
||||
'This might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
const worker = this.allocateUnusedWorker();
|
||||
this.loadWasmModuleToWorker(worker, sab);
|
||||
}
|
||||
return this.unusedWorkers.pop();
|
||||
}
|
||||
const worker = this.allocateUnusedWorker();
|
||||
this.loadWasmModuleToWorker(worker, sab);
|
||||
return this.unusedWorkers.pop();
|
||||
}
|
||||
cleanThread(worker, tid, force) {
|
||||
if (!force && this._reuseWorker) {
|
||||
this.returnWorkerToPool(worker);
|
||||
}
|
||||
else {
|
||||
delete this.pthreads[tid];
|
||||
const index = this.runningWorkers.indexOf(worker);
|
||||
if (index !== -1) {
|
||||
this.runningWorkers.splice(index, 1);
|
||||
}
|
||||
this.terminateWorker(worker);
|
||||
delete worker.__emnapi_tid;
|
||||
}
|
||||
}
|
||||
terminateWorker(worker) {
|
||||
var _a;
|
||||
const tid = worker.__emnapi_tid;
|
||||
worker.terminate();
|
||||
(_a = this.messageEvents.get(worker)) === null || _a === void 0 ? void 0 : _a.clear();
|
||||
this.messageEvents.delete(worker);
|
||||
worker.onmessage = (e) => {
|
||||
if (e.data.__emnapi__) {
|
||||
const err = this.printErr;
|
||||
err('received "' + e.data.__emnapi__.type + '" command from terminated worker: ' + tid);
|
||||
}
|
||||
};
|
||||
}
|
||||
terminateAllThreads() {
|
||||
for (let i = 0; i < this.runningWorkers.length; ++i) {
|
||||
this.terminateWorker(this.runningWorkers[i]);
|
||||
}
|
||||
for (let i = 0; i < this.unusedWorkers.length; ++i) {
|
||||
this.terminateWorker(this.unusedWorkers[i]);
|
||||
}
|
||||
this.unusedWorkers = [];
|
||||
this.runningWorkers = [];
|
||||
this.pthreads = Object.create(null);
|
||||
this.preparePool();
|
||||
}
|
||||
addMessageEventListener(worker, onMessage) {
|
||||
let listeners = this.messageEvents.get(worker);
|
||||
if (!listeners) {
|
||||
listeners = new Set();
|
||||
this.messageEvents.set(worker, listeners);
|
||||
}
|
||||
listeners.add(onMessage);
|
||||
return () => {
|
||||
listeners === null || listeners === void 0 ? void 0 : listeners.delete(onMessage);
|
||||
};
|
||||
}
|
||||
fireMessageEvent(worker, e) {
|
||||
const listeners = this.messageEvents.get(worker);
|
||||
if (!listeners)
|
||||
return;
|
||||
const err = this.printErr;
|
||||
listeners.forEach((listener) => {
|
||||
try {
|
||||
listener(e);
|
||||
}
|
||||
catch (e) {
|
||||
err(e.stack);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const kIsProxy = Symbol('kIsProxy');
|
||||
function createInstanceProxy(instance, memory) {
|
||||
if (instance[kIsProxy])
|
||||
return instance;
|
||||
const originalExports = instance.exports;
|
||||
const createHandler = function (target) {
|
||||
const handlers = [
|
||||
'apply',
|
||||
'construct',
|
||||
'defineProperty',
|
||||
'deleteProperty',
|
||||
'get',
|
||||
'getOwnPropertyDescriptor',
|
||||
'getPrototypeOf',
|
||||
'has',
|
||||
'isExtensible',
|
||||
'ownKeys',
|
||||
'preventExtensions',
|
||||
'set',
|
||||
'setPrototypeOf'
|
||||
];
|
||||
const handler = {};
|
||||
for (let i = 0; i < handlers.length; i++) {
|
||||
const name = handlers[i];
|
||||
handler[name] = function () {
|
||||
const args = Array.prototype.slice.call(arguments, 1);
|
||||
args.unshift(target);
|
||||
return Reflect[name].apply(Reflect, args);
|
||||
};
|
||||
}
|
||||
return handler;
|
||||
};
|
||||
const handler = createHandler(originalExports);
|
||||
const _initialize = () => { };
|
||||
const _start = () => 0;
|
||||
handler.get = function (_target, p, receiver) {
|
||||
var _a;
|
||||
if (p === 'memory') {
|
||||
return (_a = (typeof memory === 'function' ? memory() : memory)) !== null && _a !== void 0 ? _a : Reflect.get(originalExports, p, receiver);
|
||||
}
|
||||
if (p === '_initialize') {
|
||||
return p in originalExports ? _initialize : undefined;
|
||||
}
|
||||
if (p === '_start') {
|
||||
return p in originalExports ? _start : undefined;
|
||||
}
|
||||
return Reflect.get(originalExports, p, receiver);
|
||||
};
|
||||
handler.has = function (_target, p) {
|
||||
if (p === 'memory')
|
||||
return true;
|
||||
return Reflect.has(originalExports, p);
|
||||
};
|
||||
const exportsProxy = new Proxy(Object.create(null), handler);
|
||||
return new Proxy(instance, {
|
||||
get(target, p, receiver) {
|
||||
if (p === 'exports') {
|
||||
return exportsProxy;
|
||||
}
|
||||
if (p === kIsProxy) {
|
||||
return true;
|
||||
}
|
||||
return Reflect.get(target, p, receiver);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const patchedWasiInstances = new WeakMap();
|
||||
class WASIThreads {
|
||||
constructor(options) {
|
||||
if (!options) {
|
||||
throw new TypeError('WASIThreads(): options is not provided');
|
||||
}
|
||||
if (!options.wasi) {
|
||||
throw new TypeError('WASIThreads(): options.wasi is not provided');
|
||||
}
|
||||
patchedWasiInstances.set(this, new WeakSet());
|
||||
const wasi = options.wasi;
|
||||
patchWasiInstance(this, wasi);
|
||||
this.wasi = wasi;
|
||||
if ('childThread' in options) {
|
||||
this.childThread = Boolean(options.childThread);
|
||||
}
|
||||
else {
|
||||
this.childThread = false;
|
||||
}
|
||||
this.PThread = undefined;
|
||||
if ('threadManager' in options) {
|
||||
if (typeof options.threadManager === 'function') {
|
||||
this.PThread = options.threadManager();
|
||||
}
|
||||
else {
|
||||
this.PThread = options.threadManager;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!this.childThread) {
|
||||
this.PThread = new ThreadManager(options);
|
||||
this.PThread.init();
|
||||
}
|
||||
}
|
||||
let waitThreadStart = false;
|
||||
if ('waitThreadStart' in options) {
|
||||
waitThreadStart = typeof options.waitThreadStart === 'number' ? options.waitThreadStart : Boolean(options.waitThreadStart);
|
||||
}
|
||||
const postMessage = getPostMessage(options);
|
||||
if (this.childThread && typeof postMessage !== 'function') {
|
||||
throw new TypeError('options.postMessage is not a function');
|
||||
}
|
||||
this.postMessage = postMessage;
|
||||
const wasm64 = Boolean(options.wasm64);
|
||||
const onMessage = (e) => {
|
||||
if (e.data.__emnapi__) {
|
||||
const type = e.data.__emnapi__.type;
|
||||
const payload = e.data.__emnapi__.payload;
|
||||
if (type === 'spawn-thread') {
|
||||
threadSpawn(payload.startArg, payload.errorOrTid);
|
||||
}
|
||||
else if (type === 'terminate-all-threads') {
|
||||
this.terminateAllThreads();
|
||||
}
|
||||
}
|
||||
};
|
||||
const threadSpawn = (startArg, errorOrTid) => {
|
||||
var _a;
|
||||
const EAGAIN = 6;
|
||||
const isNewABI = errorOrTid !== undefined;
|
||||
try {
|
||||
checkSharedWasmMemory(this.wasmMemory);
|
||||
}
|
||||
catch (err) {
|
||||
(_a = this.PThread) === null || _a === void 0 ? void 0 : _a.printErr(err.stack);
|
||||
if (isNewABI) {
|
||||
const struct = new Int32Array(this.wasmMemory.buffer, errorOrTid, 2);
|
||||
Atomics.store(struct, 0, 1);
|
||||
Atomics.store(struct, 1, EAGAIN);
|
||||
Atomics.notify(struct, 1);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return -EAGAIN;
|
||||
}
|
||||
}
|
||||
if (!isNewABI) {
|
||||
const malloc = this.wasmInstance.exports.malloc;
|
||||
errorOrTid = wasm64 ? Number(malloc(BigInt(8))) : malloc(8);
|
||||
if (!errorOrTid) {
|
||||
return -48;
|
||||
}
|
||||
}
|
||||
const _free = this.wasmInstance.exports.free;
|
||||
const free = wasm64 ? (ptr) => { _free(BigInt(ptr)); } : _free;
|
||||
const struct = new Int32Array(this.wasmMemory.buffer, errorOrTid, 2);
|
||||
Atomics.store(struct, 0, 0);
|
||||
Atomics.store(struct, 1, 0);
|
||||
if (this.childThread) {
|
||||
postMessage(createMessage('spawn-thread', {
|
||||
startArg,
|
||||
errorOrTid: errorOrTid
|
||||
}));
|
||||
Atomics.wait(struct, 1, 0);
|
||||
const isError = Atomics.load(struct, 0);
|
||||
const result = Atomics.load(struct, 1);
|
||||
if (isNewABI) {
|
||||
return isError;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return isError ? -result : result;
|
||||
}
|
||||
const shouldWait = waitThreadStart || (waitThreadStart === 0);
|
||||
let sab;
|
||||
if (shouldWait) {
|
||||
sab = new Int32Array(new SharedArrayBuffer(16 + 8192));
|
||||
Atomics.store(sab, 0, 0);
|
||||
}
|
||||
let worker;
|
||||
let tid;
|
||||
const PThread = this.PThread;
|
||||
try {
|
||||
worker = PThread.getNewWorker(sab);
|
||||
if (!worker) {
|
||||
throw new Error('failed to get new worker');
|
||||
}
|
||||
PThread.addMessageEventListener(worker, onMessage);
|
||||
tid = PThread.markId(worker);
|
||||
if (ENVIRONMENT_IS_NODE) {
|
||||
worker.ref();
|
||||
}
|
||||
worker.postMessage(createMessage('start', {
|
||||
tid,
|
||||
arg: startArg,
|
||||
sab
|
||||
}));
|
||||
if (shouldWait) {
|
||||
if (typeof waitThreadStart === 'number') {
|
||||
const waitResult = Atomics.wait(sab, 0, 0, waitThreadStart);
|
||||
if (waitResult === 'timed-out') {
|
||||
try {
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
}
|
||||
catch (_) { }
|
||||
throw new Error('Spawning thread timed out. Please check if the worker is created successfully and if message is handled properly in the worker.');
|
||||
}
|
||||
}
|
||||
else {
|
||||
Atomics.wait(sab, 0, 0);
|
||||
}
|
||||
const r = Atomics.load(sab, 0);
|
||||
if (r > 1) {
|
||||
try {
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
}
|
||||
catch (_) { }
|
||||
throw deserizeErrorFromBuffer(sab.buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
Atomics.store(struct, 0, 1);
|
||||
Atomics.store(struct, 1, EAGAIN);
|
||||
Atomics.notify(struct, 1);
|
||||
PThread === null || PThread === void 0 ? void 0 : PThread.printErr(e.stack);
|
||||
if (isNewABI) {
|
||||
return 1;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return -EAGAIN;
|
||||
}
|
||||
Atomics.store(struct, 0, 0);
|
||||
Atomics.store(struct, 1, tid);
|
||||
Atomics.notify(struct, 1);
|
||||
PThread.runningWorkers.push(worker);
|
||||
if (!shouldWait) {
|
||||
worker.whenLoaded.catch((err) => {
|
||||
delete worker.whenLoaded;
|
||||
PThread.cleanThread(worker, tid, true);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
if (isNewABI) {
|
||||
return 0;
|
||||
}
|
||||
free(errorOrTid);
|
||||
return tid;
|
||||
};
|
||||
this.threadSpawn = threadSpawn;
|
||||
}
|
||||
getImportObject() {
|
||||
return {
|
||||
wasi: {
|
||||
'thread-spawn': this.threadSpawn
|
||||
}
|
||||
};
|
||||
}
|
||||
setup(wasmInstance, wasmModule, wasmMemory) {
|
||||
wasmMemory !== null && wasmMemory !== void 0 ? wasmMemory : (wasmMemory = wasmInstance.exports.memory);
|
||||
this.wasmInstance = wasmInstance;
|
||||
this.wasmMemory = wasmMemory;
|
||||
if (this.PThread) {
|
||||
this.PThread.setup(wasmModule, wasmMemory);
|
||||
}
|
||||
}
|
||||
preloadWorkers() {
|
||||
if (this.PThread) {
|
||||
return this.PThread.preloadWorkers();
|
||||
}
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
initialize(instance, module, memory) {
|
||||
const exports = instance.exports;
|
||||
memory !== null && memory !== void 0 ? memory : (memory = exports.memory);
|
||||
if (this.childThread) {
|
||||
instance = createInstanceProxy(instance, memory);
|
||||
}
|
||||
this.setup(instance, module, memory);
|
||||
const wasi = this.wasi;
|
||||
if (('_start' in exports) && (typeof exports._start === 'function')) {
|
||||
if (this.childThread) {
|
||||
wasi.start(instance);
|
||||
try {
|
||||
const kStarted = getWasiSymbol(wasi, 'kStarted');
|
||||
wasi[kStarted] = false;
|
||||
}
|
||||
catch (_) { }
|
||||
}
|
||||
else {
|
||||
setupInstance(wasi, instance);
|
||||
}
|
||||
}
|
||||
else {
|
||||
wasi.initialize(instance);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
start(instance, module, memory) {
|
||||
const exports = instance.exports;
|
||||
memory !== null && memory !== void 0 ? memory : (memory = exports.memory);
|
||||
if (this.childThread) {
|
||||
instance = createInstanceProxy(instance, memory);
|
||||
}
|
||||
this.setup(instance, module, memory);
|
||||
const exitCode = this.wasi.start(instance);
|
||||
return { exitCode, instance };
|
||||
}
|
||||
terminateAllThreads() {
|
||||
var _a;
|
||||
if (!this.childThread) {
|
||||
(_a = this.PThread) === null || _a === void 0 ? void 0 : _a.terminateAllThreads();
|
||||
}
|
||||
else {
|
||||
this.postMessage(createMessage('terminate-all-threads', {}));
|
||||
}
|
||||
}
|
||||
}
|
||||
function patchWasiInstance(wasiThreads, wasi) {
|
||||
const patched = patchedWasiInstances.get(wasiThreads);
|
||||
if (patched.has(wasi)) {
|
||||
return;
|
||||
}
|
||||
const _this = wasiThreads;
|
||||
const wasiImport = wasi.wasiImport;
|
||||
if (wasiImport) {
|
||||
const proc_exit = wasiImport.proc_exit;
|
||||
wasiImport.proc_exit = function (code) {
|
||||
_this.terminateAllThreads();
|
||||
return proc_exit.call(this, code);
|
||||
};
|
||||
}
|
||||
if (!_this.childThread) {
|
||||
const start = wasi.start;
|
||||
if (typeof start === 'function') {
|
||||
wasi.start = function (instance) {
|
||||
try {
|
||||
return start.call(this, instance);
|
||||
}
|
||||
catch (err) {
|
||||
if (isTrapError(err)) {
|
||||
_this.terminateAllThreads();
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
patched.add(wasi);
|
||||
}
|
||||
function getWasiSymbol(wasi, description) {
|
||||
const symbols = Object.getOwnPropertySymbols(wasi);
|
||||
const selectDescription = (description) => (s) => {
|
||||
if (s.description) {
|
||||
return s.description === description;
|
||||
}
|
||||
return s.toString() === `Symbol(${description})`;
|
||||
};
|
||||
if (Array.isArray(description)) {
|
||||
return description.map(d => symbols.filter(selectDescription(d))[0]);
|
||||
}
|
||||
return symbols.filter(selectDescription(description))[0];
|
||||
}
|
||||
function setupInstance(wasi, instance) {
|
||||
const [kInstance, kSetMemory] = getWasiSymbol(wasi, ['kInstance', 'kSetMemory']);
|
||||
wasi[kInstance] = instance;
|
||||
wasi[kSetMemory](instance.exports.memory);
|
||||
}
|
||||
|
||||
class ThreadMessageHandler {
|
||||
constructor(options) {
|
||||
const postMsg = getPostMessage(options);
|
||||
if (typeof postMsg !== 'function') {
|
||||
throw new TypeError('options.postMessage is not a function');
|
||||
}
|
||||
this.postMessage = postMsg;
|
||||
this.onLoad = options === null || options === void 0 ? void 0 : options.onLoad;
|
||||
this.onError = typeof (options === null || options === void 0 ? void 0 : options.onError) === 'function' ? options.onError : (_type, err) => { throw err; };
|
||||
this.instance = undefined;
|
||||
this.messagesBeforeLoad = [];
|
||||
}
|
||||
instantiate(data) {
|
||||
if (typeof this.onLoad === 'function') {
|
||||
return this.onLoad(data);
|
||||
}
|
||||
throw new Error('ThreadMessageHandler.prototype.instantiate is not implemented');
|
||||
}
|
||||
handle(e) {
|
||||
var _a;
|
||||
if ((_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.__emnapi__) {
|
||||
const type = e.data.__emnapi__.type;
|
||||
const payload = e.data.__emnapi__.payload;
|
||||
try {
|
||||
if (type === 'load') {
|
||||
this._load(payload);
|
||||
}
|
||||
else if (type === 'start') {
|
||||
this.handleAfterLoad(e, () => {
|
||||
this._start(payload);
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
this.onError(err, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
_load(payload) {
|
||||
if (this.instance !== undefined)
|
||||
return;
|
||||
let source;
|
||||
try {
|
||||
source = this.instantiate(payload);
|
||||
}
|
||||
catch (err) {
|
||||
this._loaded(err, null, payload);
|
||||
return;
|
||||
}
|
||||
const then = source && 'then' in source ? source.then : undefined;
|
||||
if (typeof then === 'function') {
|
||||
then.call(source, (source) => { this._loaded(null, source, payload); }, (err) => { this._loaded(err, null, payload); });
|
||||
}
|
||||
else {
|
||||
this._loaded(null, source, payload);
|
||||
}
|
||||
}
|
||||
_start(payload) {
|
||||
const wasi_thread_start = this.instance.exports.wasi_thread_start;
|
||||
if (typeof wasi_thread_start !== 'function') {
|
||||
const err = new TypeError('wasi_thread_start is not exported');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
const postMessage = this.postMessage;
|
||||
const tid = payload.tid;
|
||||
const startArg = payload.arg;
|
||||
notifyPthreadCreateResult(payload.sab, 1);
|
||||
try {
|
||||
wasi_thread_start(tid, startArg);
|
||||
}
|
||||
catch (err) {
|
||||
if (err !== 'unwind') {
|
||||
throw err;
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
postMessage(createMessage('cleanup-thread', { tid }));
|
||||
}
|
||||
_loaded(err, source, payload) {
|
||||
if (err) {
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
if (source == null) {
|
||||
const err = new TypeError('onLoad should return an object');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
const instance = source.instance;
|
||||
if (!instance) {
|
||||
const err = new TypeError('onLoad should return an object which includes "instance"');
|
||||
notifyPthreadCreateResult(payload.sab, 2, err);
|
||||
throw err;
|
||||
}
|
||||
this.instance = instance;
|
||||
const postMessage = this.postMessage;
|
||||
postMessage(createMessage('loaded', {}));
|
||||
const messages = this.messagesBeforeLoad;
|
||||
this.messagesBeforeLoad = [];
|
||||
for (let i = 0; i < messages.length; i++) {
|
||||
const data = messages[i];
|
||||
this.handle({ data });
|
||||
}
|
||||
}
|
||||
handleAfterLoad(e, f) {
|
||||
if (this.instance !== undefined) {
|
||||
f.call(this, e);
|
||||
}
|
||||
else {
|
||||
this.messagesBeforeLoad.push(e.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
function notifyPthreadCreateResult(sab, result, error) {
|
||||
if (sab) {
|
||||
serizeErrorToBuffer(sab.buffer, result, error);
|
||||
Atomics.notify(sab, 0);
|
||||
}
|
||||
}
|
||||
|
||||
export { ThreadManager, ThreadMessageHandler, WASIThreads, createInstanceProxy, isSharedArrayBuffer, isTrapError };
|
||||
5
Frontend-Learner/node_modules/@emnapi/wasi-threads/index.js
generated
vendored
Normal file
5
Frontend-Learner/node_modules/@emnapi/wasi-threads/index.js
generated
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') {
|
||||
module.exports = require('./dist/wasi-threads.cjs.min.js')
|
||||
} else {
|
||||
module.exports = require('./dist/wasi-threads.cjs.js')
|
||||
}
|
||||
50
Frontend-Learner/node_modules/@emnapi/wasi-threads/package.json
generated
vendored
Normal file
50
Frontend-Learner/node_modules/@emnapi/wasi-threads/package.json
generated
vendored
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
{
|
||||
"name": "@emnapi/wasi-threads",
|
||||
"version": "1.1.0",
|
||||
"description": "WASI threads proposal implementation in JavaScript",
|
||||
"main": "index.js",
|
||||
"module": "./dist/wasi-threads.esm-bundler.js",
|
||||
"types": "./dist/wasi-threads.d.ts",
|
||||
"sideEffects": false,
|
||||
"exports": {
|
||||
".": {
|
||||
"types": {
|
||||
"module": "./dist/wasi-threads.d.ts",
|
||||
"import": "./dist/wasi-threads.d.mts",
|
||||
"default": "./dist/wasi-threads.d.ts"
|
||||
},
|
||||
"module": "./dist/wasi-threads.esm-bundler.js",
|
||||
"import": "./dist/wasi-threads.mjs",
|
||||
"default": "./index.js"
|
||||
},
|
||||
"./dist/wasi-threads.cjs.min": {
|
||||
"types": "./dist/wasi-threads.d.ts",
|
||||
"default": "./dist/wasi-threads.cjs.min.js"
|
||||
},
|
||||
"./dist/wasi-threads.min.mjs": {
|
||||
"types": "./dist/wasi-threads.d.mts",
|
||||
"default": "./dist/wasi-threads.min.mjs"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"tslib": "^2.4.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "node ./script/build.js",
|
||||
"build:test": "node ./test/build.js",
|
||||
"test": "node ./test/index.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/toyobayashi/emnapi.git"
|
||||
},
|
||||
"author": "toyobayashi",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/toyobayashi/emnapi/issues"
|
||||
},
|
||||
"homepage": "https://github.com/toyobayashi/emnapi#readme",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue