@tailwindcss/oxide-wasm32-wasi 0.0.0-insiders.bd9cf92 → 0.0.0-insiders.bdcd708
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node_modules/@emnapi/core/dist/emnapi-core.cjs.js +1469 -511
- package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.d.ts +61 -5
- package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.js +1 -1
- package/node_modules/@emnapi/core/dist/emnapi-core.d.mts +61 -5
- package/node_modules/@emnapi/core/dist/emnapi-core.d.ts +61 -5
- package/node_modules/@emnapi/core/dist/emnapi-core.esm-bundler.js +1628 -523
- package/node_modules/@emnapi/core/dist/emnapi-core.js +1705 -585
- package/node_modules/@emnapi/core/dist/emnapi-core.min.d.mts +61 -5
- package/node_modules/@emnapi/core/dist/emnapi-core.min.js +1 -1
- package/node_modules/@emnapi/core/dist/emnapi-core.min.mjs +1 -1
- package/node_modules/@emnapi/core/dist/emnapi-core.mjs +1469 -511
- package/node_modules/@emnapi/core/package.json +2 -2
- package/node_modules/@emnapi/runtime/dist/emnapi.cjs.js +44 -14
- package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.d.ts +15 -7
- package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.js +1 -1
- package/node_modules/@emnapi/runtime/dist/emnapi.d.mts +15 -7
- package/node_modules/@emnapi/runtime/dist/emnapi.d.ts +15 -7
- package/node_modules/@emnapi/runtime/dist/emnapi.esm-bundler.js +45 -14
- package/node_modules/@emnapi/runtime/dist/emnapi.iife.d.ts +12 -6
- package/node_modules/@emnapi/runtime/dist/emnapi.iife.js +45 -14
- package/node_modules/@emnapi/runtime/dist/emnapi.js +45 -14
- package/node_modules/@emnapi/runtime/dist/emnapi.min.d.mts +15 -7
- package/node_modules/@emnapi/runtime/dist/emnapi.min.js +1 -1
- package/node_modules/@emnapi/runtime/dist/emnapi.min.mjs +1 -1
- package/node_modules/@emnapi/runtime/dist/emnapi.mjs +44 -14
- package/node_modules/@emnapi/runtime/package.json +1 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.js +67 -63
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.d.ts +59 -2
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.js +1 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.mts +59 -2
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts +59 -2
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.js +67 -63
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js +67 -63
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.d.mts +59 -2
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.js +1 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.mjs +1 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.mjs +67 -63
- package/node_modules/@emnapi/wasi-threads/package.json +1 -1
- package/node_modules/@napi-rs/wasm-runtime/dist/fs-proxy.cjs +299 -0
- package/node_modules/@napi-rs/wasm-runtime/dist/fs.js +22125 -20726
- package/node_modules/@napi-rs/wasm-runtime/fs-proxy.js +294 -0
- package/node_modules/@napi-rs/wasm-runtime/package.json +25 -16
- package/node_modules/@napi-rs/wasm-runtime/runtime.cjs +1 -1
- package/node_modules/@napi-rs/wasm-runtime/runtime.js +8 -0
- package/node_modules/@tybys/wasm-util/dist/wasm-util.d.ts +6 -0
- package/node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.js +215 -33
- package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.js +215 -33
- package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.min.js +1 -1
- package/node_modules/@tybys/wasm-util/dist/wasm-util.js +215 -33
- package/node_modules/@tybys/wasm-util/dist/wasm-util.min.js +1 -1
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/index.js +12 -14
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/path.js +142 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/preview1.js +61 -19
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/index.mjs +12 -14
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/path.mjs +142 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/preview1.mjs +61 -19
- package/node_modules/@tybys/wasm-util/package.json +1 -1
- package/node_modules/tslib/package.json +1 -1
- package/node_modules/tslib/tslib.es6.js +10 -1
- package/node_modules/tslib/tslib.es6.mjs +10 -1
- package/node_modules/tslib/tslib.js +42 -33
- package/package.json +7 -7
- package/tailwindcss-oxide.wasi-browser.js +7 -3
- package/tailwindcss-oxide.wasi.cjs +27 -4
- package/tailwindcss-oxide.wasm32-wasi.wasm +0 -0
- package/wasi-worker-browser.mjs +2 -0
- package/node_modules/@napi-rs/wasm-runtime/LICENSE +0 -43
- package/node_modules/@napi-rs/wasm-runtime/dist/runtime.js +0 -12285
- package/node_modules/@napi-rs/wasm-runtime/fs-proxy.cjs +0 -209
|
@@ -154,7 +154,7 @@ const _Buffer = typeof Buffer === 'function'
|
|
|
154
154
|
catch (_) { }
|
|
155
155
|
return undefined;
|
|
156
156
|
})();
|
|
157
|
-
const version = "1.
|
|
157
|
+
const version = "1.11.1";
|
|
158
158
|
const NODE_API_SUPPORTED_VERSION_MIN = 1;
|
|
159
159
|
const NODE_API_SUPPORTED_VERSION_MAX = 10;
|
|
160
160
|
const NAPI_VERSION_EXPERIMENTAL = 2147483647;
|
|
@@ -349,7 +349,7 @@ class ScopeStore {
|
|
|
349
349
|
get(id) {
|
|
350
350
|
return this._values[id];
|
|
351
351
|
}
|
|
352
|
-
openScope(
|
|
352
|
+
openScope(handleStore) {
|
|
353
353
|
const currentScope = this.currentScope;
|
|
354
354
|
let scope = currentScope.child;
|
|
355
355
|
if (scope !== null) {
|
|
@@ -357,20 +357,16 @@ class ScopeStore {
|
|
|
357
357
|
}
|
|
358
358
|
else {
|
|
359
359
|
const id = currentScope.id + 1;
|
|
360
|
-
scope = new HandleScope(
|
|
360
|
+
scope = new HandleScope(handleStore, id, currentScope, currentScope.end);
|
|
361
361
|
this._values[id] = scope;
|
|
362
362
|
}
|
|
363
363
|
this.currentScope = scope;
|
|
364
|
-
envObject.openHandleScopes++;
|
|
365
364
|
return scope;
|
|
366
365
|
}
|
|
367
|
-
closeScope(
|
|
368
|
-
if (envObject.openHandleScopes === 0)
|
|
369
|
-
return;
|
|
366
|
+
closeScope() {
|
|
370
367
|
const scope = this.currentScope;
|
|
371
368
|
this.currentScope = scope.parent;
|
|
372
369
|
scope.dispose();
|
|
373
|
-
envObject.openHandleScopes--;
|
|
374
370
|
}
|
|
375
371
|
dispose() {
|
|
376
372
|
this.currentScope = this._rootScope;
|
|
@@ -706,7 +702,7 @@ class NodeEnv extends Env {
|
|
|
706
702
|
console.warn(`${prefix}${type || 'Warning'}: ${warning}`);
|
|
707
703
|
}
|
|
708
704
|
};
|
|
709
|
-
warn('Uncaught
|
|
705
|
+
warn('Uncaught Node-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.', 'DeprecationWarning', 'DEP0168');
|
|
710
706
|
return;
|
|
711
707
|
}
|
|
712
708
|
envObject.triggerFatalException(err);
|
|
@@ -1110,6 +1106,29 @@ class Store {
|
|
|
1110
1106
|
}
|
|
1111
1107
|
}
|
|
1112
1108
|
|
|
1109
|
+
const kMaxReasonableBytes = BigInt(1) << BigInt(60);
|
|
1110
|
+
const kMinReasonableBytes = -kMaxReasonableBytes;
|
|
1111
|
+
class ExternalMemory {
|
|
1112
|
+
constructor(onChange) {
|
|
1113
|
+
this.total = BigInt(0);
|
|
1114
|
+
this.onChange = onChange !== null && onChange !== void 0 ? onChange : null;
|
|
1115
|
+
}
|
|
1116
|
+
adjust(changeInBytes) {
|
|
1117
|
+
changeInBytes = BigInt(changeInBytes);
|
|
1118
|
+
if (!(kMinReasonableBytes <= changeInBytes && changeInBytes < kMaxReasonableBytes)) {
|
|
1119
|
+
throw new RangeError(`changeInBytes ${changeInBytes} is out of reasonable range`);
|
|
1120
|
+
}
|
|
1121
|
+
const old = this.total;
|
|
1122
|
+
this.total += changeInBytes;
|
|
1123
|
+
const amount = this.total;
|
|
1124
|
+
const onChange = this.onChange;
|
|
1125
|
+
if (changeInBytes) {
|
|
1126
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(amount, old, changeInBytes);
|
|
1127
|
+
}
|
|
1128
|
+
return amount;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1113
1132
|
class CleanupHookCallback {
|
|
1114
1133
|
constructor(envObject, fn, arg, order) {
|
|
1115
1134
|
this.envObject = envObject;
|
|
@@ -1185,7 +1204,7 @@ class NodejsWaitingRequestCounter {
|
|
|
1185
1204
|
}
|
|
1186
1205
|
}
|
|
1187
1206
|
class Context {
|
|
1188
|
-
constructor() {
|
|
1207
|
+
constructor(options) {
|
|
1189
1208
|
this._isStopping = false;
|
|
1190
1209
|
this._canCallIntoJs = true;
|
|
1191
1210
|
this._suppressDestroy = false;
|
|
@@ -1206,6 +1225,7 @@ class Context {
|
|
|
1206
1225
|
MessageChannel: _MessageChannel
|
|
1207
1226
|
};
|
|
1208
1227
|
this.cleanupQueue = new CleanupQueue();
|
|
1228
|
+
this._externalMemory = new ExternalMemory(options === null || options === void 0 ? void 0 : options.onExternalMemoryChange);
|
|
1209
1229
|
if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
|
|
1210
1230
|
this.refCounter = new NodejsWaitingRequestCounter();
|
|
1211
1231
|
process.once('beforeExit', () => {
|
|
@@ -1244,6 +1264,9 @@ class Context {
|
|
|
1244
1264
|
createDeferred(value) {
|
|
1245
1265
|
return Deferred.create(this, value);
|
|
1246
1266
|
}
|
|
1267
|
+
adjustAmountOfExternalAllocatedMemory(changeInBytes) {
|
|
1268
|
+
return this._externalMemory.adjust(changeInBytes);
|
|
1269
|
+
}
|
|
1247
1270
|
createEnv(filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
|
|
1248
1271
|
return newEnv(this, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
|
|
1249
1272
|
}
|
|
@@ -1257,10 +1280,17 @@ class Context {
|
|
|
1257
1280
|
return this.scopeStore.currentScope.add(value);
|
|
1258
1281
|
}
|
|
1259
1282
|
openScope(envObject) {
|
|
1260
|
-
|
|
1283
|
+
const scope = this.scopeStore.openScope(this.handleStore);
|
|
1284
|
+
if (envObject)
|
|
1285
|
+
envObject.openHandleScopes++;
|
|
1286
|
+
return scope;
|
|
1261
1287
|
}
|
|
1262
1288
|
closeScope(envObject, _scope) {
|
|
1263
|
-
|
|
1289
|
+
if (envObject && envObject.openHandleScopes === 0)
|
|
1290
|
+
return;
|
|
1291
|
+
this.scopeStore.closeScope();
|
|
1292
|
+
if (envObject)
|
|
1293
|
+
envObject.openHandleScopes--;
|
|
1264
1294
|
}
|
|
1265
1295
|
ensureHandle(value) {
|
|
1266
1296
|
switch (value) {
|
|
@@ -1307,8 +1337,8 @@ class Context {
|
|
|
1307
1337
|
}
|
|
1308
1338
|
}
|
|
1309
1339
|
let defaultContext;
|
|
1310
|
-
function createContext() {
|
|
1311
|
-
return new Context();
|
|
1340
|
+
function createContext(options) {
|
|
1341
|
+
return new Context(options);
|
|
1312
1342
|
}
|
|
1313
1343
|
function getDefaultContext() {
|
|
1314
1344
|
if (!defaultContext) {
|
|
@@ -123,7 +123,6 @@ class ThreadManager {
|
|
|
123
123
|
constructor(options) {
|
|
124
124
|
var _a;
|
|
125
125
|
this.unusedWorkers = [];
|
|
126
|
-
this.runningWorkers = [];
|
|
127
126
|
this.pthreads = Object.create(null);
|
|
128
127
|
this.wasmModule = null;
|
|
129
128
|
this.wasmMemory = null;
|
|
@@ -148,6 +147,7 @@ class ThreadManager {
|
|
|
148
147
|
this._beforeLoad = options.beforeLoad;
|
|
149
148
|
}
|
|
150
149
|
this.printErr = (_a = options.printErr) !== null && _a !== void 0 ? _a : console.error.bind(console);
|
|
150
|
+
this.threadSpawn = options.threadSpawn;
|
|
151
151
|
}
|
|
152
152
|
init() {
|
|
153
153
|
if (!this._childThread) {
|
|
@@ -220,7 +220,6 @@ class ThreadManager {
|
|
|
220
220
|
delete this.pthreads[tid];
|
|
221
221
|
}
|
|
222
222
|
this.unusedWorkers.push(worker);
|
|
223
|
-
this.runningWorkers.splice(this.runningWorkers.indexOf(worker), 1);
|
|
224
223
|
delete worker.__emnapi_tid;
|
|
225
224
|
if (ENVIRONMENT_IS_NODE) {
|
|
226
225
|
worker.unref();
|
|
@@ -238,12 +237,17 @@ class ThreadManager {
|
|
|
238
237
|
if (worker.__emnapi_tid !== undefined) {
|
|
239
238
|
message = 'worker (tid = ' + worker.__emnapi_tid + ') sent an error!';
|
|
240
239
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
240
|
+
if ('message' in e) {
|
|
241
|
+
err(message + ' ' + e.message);
|
|
242
|
+
if (e.message.indexOf('RuntimeError') !== -1 || e.message.indexOf('unreachable') !== -1) {
|
|
243
|
+
try {
|
|
244
|
+
_this.terminateAllThreads();
|
|
245
|
+
}
|
|
246
|
+
catch (_) { }
|
|
245
247
|
}
|
|
246
|
-
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
err(message);
|
|
247
251
|
}
|
|
248
252
|
reject(e);
|
|
249
253
|
throw e;
|
|
@@ -264,6 +268,12 @@ class ThreadManager {
|
|
|
264
268
|
this.cleanThread(worker, payload.tid);
|
|
265
269
|
}
|
|
266
270
|
}
|
|
271
|
+
else if (type === 'spawn-thread') {
|
|
272
|
+
this.threadSpawn(payload.startArg, payload.errorOrTid);
|
|
273
|
+
}
|
|
274
|
+
else if (type === 'terminate-all-threads') {
|
|
275
|
+
this.terminateAllThreads();
|
|
276
|
+
}
|
|
267
277
|
}
|
|
268
278
|
};
|
|
269
279
|
worker.onmessage = (e) => {
|
|
@@ -336,10 +346,6 @@ class ThreadManager {
|
|
|
336
346
|
}
|
|
337
347
|
else {
|
|
338
348
|
delete this.pthreads[tid];
|
|
339
|
-
const index = this.runningWorkers.indexOf(worker);
|
|
340
|
-
if (index !== -1) {
|
|
341
|
-
this.runningWorkers.splice(index, 1);
|
|
342
|
-
}
|
|
343
349
|
this.terminateWorker(worker);
|
|
344
350
|
delete worker.__emnapi_tid;
|
|
345
351
|
}
|
|
@@ -358,14 +364,14 @@ class ThreadManager {
|
|
|
358
364
|
};
|
|
359
365
|
}
|
|
360
366
|
terminateAllThreads() {
|
|
361
|
-
|
|
362
|
-
|
|
367
|
+
const runningWorkers = Object.values(this.pthreads);
|
|
368
|
+
for (let i = 0; i < runningWorkers.length; ++i) {
|
|
369
|
+
this.terminateWorker(runningWorkers[i]);
|
|
363
370
|
}
|
|
364
371
|
for (let i = 0; i < this.unusedWorkers.length; ++i) {
|
|
365
372
|
this.terminateWorker(this.unusedWorkers[i]);
|
|
366
373
|
}
|
|
367
374
|
this.unusedWorkers = [];
|
|
368
|
-
this.runningWorkers = [];
|
|
369
375
|
this.pthreads = Object.create(null);
|
|
370
376
|
this.preparePool();
|
|
371
377
|
}
|
|
@@ -507,18 +513,6 @@ class WASIThreads {
|
|
|
507
513
|
}
|
|
508
514
|
this.postMessage = postMessage;
|
|
509
515
|
const wasm64 = Boolean(options.wasm64);
|
|
510
|
-
const onMessage = (e) => {
|
|
511
|
-
if (e.data.__emnapi__) {
|
|
512
|
-
const type = e.data.__emnapi__.type;
|
|
513
|
-
const payload = e.data.__emnapi__.payload;
|
|
514
|
-
if (type === 'spawn-thread') {
|
|
515
|
-
threadSpawn(payload.startArg, payload.errorOrTid);
|
|
516
|
-
}
|
|
517
|
-
else if (type === 'terminate-all-threads') {
|
|
518
|
-
this.terminateAllThreads();
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
};
|
|
522
516
|
const threadSpawn = (startArg, errorOrTid) => {
|
|
523
517
|
var _a;
|
|
524
518
|
const EAGAIN = 6;
|
|
@@ -536,12 +530,12 @@ class WASIThreads {
|
|
|
536
530
|
return 1;
|
|
537
531
|
}
|
|
538
532
|
else {
|
|
539
|
-
return -
|
|
533
|
+
return -EAGAIN;
|
|
540
534
|
}
|
|
541
535
|
}
|
|
542
536
|
if (!isNewABI) {
|
|
543
537
|
const malloc = this.wasmInstance.exports.malloc;
|
|
544
|
-
errorOrTid = wasm64 ? Number(malloc(BigInt(8))) : malloc(8);
|
|
538
|
+
errorOrTid = wasm64 ? Number(malloc(BigInt(8))) : (malloc(8) >>> 0);
|
|
545
539
|
if (!errorOrTid) {
|
|
546
540
|
return -48;
|
|
547
541
|
}
|
|
@@ -579,10 +573,9 @@ class WASIThreads {
|
|
|
579
573
|
if (!worker) {
|
|
580
574
|
throw new Error('failed to get new worker');
|
|
581
575
|
}
|
|
582
|
-
PThread.addMessageEventListener(worker, onMessage);
|
|
583
576
|
tid = PThread.markId(worker);
|
|
584
577
|
if (ENVIRONMENT_IS_NODE) {
|
|
585
|
-
worker.
|
|
578
|
+
worker.unref();
|
|
586
579
|
}
|
|
587
580
|
worker.postMessage(createMessage('start', {
|
|
588
581
|
tid,
|
|
@@ -593,10 +586,6 @@ class WASIThreads {
|
|
|
593
586
|
if (typeof waitThreadStart === 'number') {
|
|
594
587
|
const waitResult = Atomics.wait(sab, 0, 0, waitThreadStart);
|
|
595
588
|
if (waitResult === 'timed-out') {
|
|
596
|
-
try {
|
|
597
|
-
PThread.cleanThread(worker, tid, true);
|
|
598
|
-
}
|
|
599
|
-
catch (_) { }
|
|
600
589
|
throw new Error('Spawning thread timed out. Please check if the worker is created successfully and if message is handled properly in the worker.');
|
|
601
590
|
}
|
|
602
591
|
}
|
|
@@ -605,15 +594,17 @@ class WASIThreads {
|
|
|
605
594
|
}
|
|
606
595
|
const r = Atomics.load(sab, 0);
|
|
607
596
|
if (r > 1) {
|
|
608
|
-
try {
|
|
609
|
-
PThread.cleanThread(worker, tid, true);
|
|
610
|
-
}
|
|
611
|
-
catch (_) { }
|
|
612
597
|
throw deserizeErrorFromBuffer(sab.buffer);
|
|
613
598
|
}
|
|
614
599
|
}
|
|
615
600
|
}
|
|
616
601
|
catch (e) {
|
|
602
|
+
if (worker !== undefined && tid !== undefined) {
|
|
603
|
+
try {
|
|
604
|
+
PThread.cleanThread(worker, tid, true);
|
|
605
|
+
}
|
|
606
|
+
catch (_) { }
|
|
607
|
+
}
|
|
617
608
|
Atomics.store(struct, 0, 1);
|
|
618
609
|
Atomics.store(struct, 1, EAGAIN);
|
|
619
610
|
Atomics.notify(struct, 1);
|
|
@@ -622,12 +613,11 @@ class WASIThreads {
|
|
|
622
613
|
return 1;
|
|
623
614
|
}
|
|
624
615
|
free(errorOrTid);
|
|
625
|
-
return -
|
|
616
|
+
return -EAGAIN;
|
|
626
617
|
}
|
|
627
618
|
Atomics.store(struct, 0, 0);
|
|
628
619
|
Atomics.store(struct, 1, tid);
|
|
629
620
|
Atomics.notify(struct, 1);
|
|
630
|
-
PThread.runningWorkers.push(worker);
|
|
631
621
|
if (!shouldWait) {
|
|
632
622
|
worker.whenLoaded.catch((err) => {
|
|
633
623
|
delete worker.whenLoaded;
|
|
@@ -642,6 +632,9 @@ class WASIThreads {
|
|
|
642
632
|
return tid;
|
|
643
633
|
};
|
|
644
634
|
this.threadSpawn = threadSpawn;
|
|
635
|
+
if (this.PThread) {
|
|
636
|
+
this.PThread.threadSpawn = threadSpawn;
|
|
637
|
+
}
|
|
645
638
|
}
|
|
646
639
|
getImportObject() {
|
|
647
640
|
return {
|
|
@@ -724,19 +717,21 @@ function patchWasiInstance(wasiThreads, wasi) {
|
|
|
724
717
|
return proc_exit.call(this, code);
|
|
725
718
|
};
|
|
726
719
|
}
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
catch (err) {
|
|
734
|
-
if (isTrapError(err)) {
|
|
735
|
-
_this.terminateAllThreads();
|
|
720
|
+
if (!_this.childThread) {
|
|
721
|
+
const start = wasi.start;
|
|
722
|
+
if (typeof start === 'function') {
|
|
723
|
+
wasi.start = function (instance) {
|
|
724
|
+
try {
|
|
725
|
+
return start.call(this, instance);
|
|
736
726
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
727
|
+
catch (err) {
|
|
728
|
+
if (isTrapError(err)) {
|
|
729
|
+
_this.terminateAllThreads();
|
|
730
|
+
}
|
|
731
|
+
throw err;
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
}
|
|
740
735
|
}
|
|
741
736
|
patched.add(wasi);
|
|
742
737
|
}
|
|
@@ -767,6 +762,7 @@ class ThreadMessageHandler {
|
|
|
767
762
|
}
|
|
768
763
|
this.postMessage = postMsg;
|
|
769
764
|
this.onLoad = options === null || options === void 0 ? void 0 : options.onLoad;
|
|
765
|
+
this.onError = typeof (options === null || options === void 0 ? void 0 : options.onError) === 'function' ? options.onError : (_type, err) => { throw err; };
|
|
770
766
|
this.instance = undefined;
|
|
771
767
|
this.messagesBeforeLoad = [];
|
|
772
768
|
}
|
|
@@ -781,13 +777,18 @@ class ThreadMessageHandler {
|
|
|
781
777
|
if ((_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.__emnapi__) {
|
|
782
778
|
const type = e.data.__emnapi__.type;
|
|
783
779
|
const payload = e.data.__emnapi__.payload;
|
|
784
|
-
|
|
785
|
-
|
|
780
|
+
try {
|
|
781
|
+
if (type === 'load') {
|
|
782
|
+
this._load(payload);
|
|
783
|
+
}
|
|
784
|
+
else if (type === 'start') {
|
|
785
|
+
this.handleAfterLoad(e, () => {
|
|
786
|
+
this._start(payload);
|
|
787
|
+
});
|
|
788
|
+
}
|
|
786
789
|
}
|
|
787
|
-
|
|
788
|
-
this.
|
|
789
|
-
this._start(payload);
|
|
790
|
-
});
|
|
790
|
+
catch (err) {
|
|
791
|
+
this.onError(err, type);
|
|
791
792
|
}
|
|
792
793
|
}
|
|
793
794
|
}
|
|
@@ -811,7 +812,8 @@ class ThreadMessageHandler {
|
|
|
811
812
|
}
|
|
812
813
|
}
|
|
813
814
|
_start(payload) {
|
|
814
|
-
|
|
815
|
+
const wasi_thread_start = this.instance.exports.wasi_thread_start;
|
|
816
|
+
if (typeof wasi_thread_start !== 'function') {
|
|
815
817
|
const err = new TypeError('wasi_thread_start is not exported');
|
|
816
818
|
notifyPthreadCreateResult(payload.sab, 2, err);
|
|
817
819
|
throw err;
|
|
@@ -821,13 +823,15 @@ class ThreadMessageHandler {
|
|
|
821
823
|
const startArg = payload.arg;
|
|
822
824
|
notifyPthreadCreateResult(payload.sab, 1);
|
|
823
825
|
try {
|
|
824
|
-
|
|
826
|
+
wasi_thread_start(tid, startArg);
|
|
825
827
|
}
|
|
826
828
|
catch (err) {
|
|
827
|
-
if (
|
|
828
|
-
|
|
829
|
+
if (err !== 'unwind') {
|
|
830
|
+
throw err;
|
|
831
|
+
}
|
|
832
|
+
else {
|
|
833
|
+
return;
|
|
829
834
|
}
|
|
830
|
-
throw err;
|
|
831
835
|
}
|
|
832
836
|
postMessage(createMessage('cleanup-thread', { tid }));
|
|
833
837
|
}
|
|
@@ -15,6 +15,30 @@ export declare interface ChildThreadOptions extends BaseOptions {
|
|
|
15
15
|
postMessage?: (data: any) => void;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
/** @public */
|
|
19
|
+
export declare interface CleanupThreadPayload {
|
|
20
|
+
tid: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @public */
|
|
24
|
+
export declare interface CommandInfo<T extends CommandType> {
|
|
25
|
+
type: T;
|
|
26
|
+
payload: CommandPayloadMap[T];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** @public */
|
|
30
|
+
export declare interface CommandPayloadMap {
|
|
31
|
+
load: LoadPayload;
|
|
32
|
+
loaded: LoadedPayload;
|
|
33
|
+
start: StartPayload;
|
|
34
|
+
'cleanup-thread': CleanupThreadPayload;
|
|
35
|
+
'terminate-all-threads': TerminateAllThreadsPayload;
|
|
36
|
+
'spawn-thread': SpawnThreadPayload;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** @public */
|
|
40
|
+
export declare type CommandType = keyof CommandPayloadMap;
|
|
41
|
+
|
|
18
42
|
/** @public */
|
|
19
43
|
export declare function createInstanceProxy(instance: WebAssembly.Instance, memory?: WebAssembly.Memory | (() => WebAssembly.Memory)): WebAssembly.Instance;
|
|
20
44
|
|
|
@@ -24,6 +48,11 @@ export declare function isSharedArrayBuffer(value: any): value is SharedArrayBuf
|
|
|
24
48
|
/** @public */
|
|
25
49
|
export declare function isTrapError(e: Error): e is WebAssembly.RuntimeError;
|
|
26
50
|
|
|
51
|
+
/** @public */
|
|
52
|
+
export declare interface LoadedPayload {
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** @public */
|
|
27
56
|
export declare interface LoadPayload {
|
|
28
57
|
wasmModule: WebAssembly.Module;
|
|
29
58
|
wasmMemory: WebAssembly.Memory;
|
|
@@ -47,6 +76,11 @@ export declare interface MainThreadOptionsWithThreadManager extends MainThreadBa
|
|
|
47
76
|
threadManager?: ThreadManager | (() => ThreadManager);
|
|
48
77
|
}
|
|
49
78
|
|
|
79
|
+
/** @public */
|
|
80
|
+
export declare interface MessageEventData<T extends CommandType> {
|
|
81
|
+
__emnapi__: CommandInfo<T>;
|
|
82
|
+
}
|
|
83
|
+
|
|
50
84
|
/** @public */
|
|
51
85
|
export declare interface ReuseWorkerOptions {
|
|
52
86
|
/**
|
|
@@ -59,16 +93,32 @@ export declare interface ReuseWorkerOptions {
|
|
|
59
93
|
strict?: boolean;
|
|
60
94
|
}
|
|
61
95
|
|
|
96
|
+
/** @public */
|
|
97
|
+
export declare interface SpawnThreadPayload {
|
|
98
|
+
startArg: number;
|
|
99
|
+
errorOrTid: number;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @public */
|
|
103
|
+
export declare interface StartPayload {
|
|
104
|
+
tid: number;
|
|
105
|
+
arg: number;
|
|
106
|
+
sab?: Int32Array;
|
|
107
|
+
}
|
|
108
|
+
|
|
62
109
|
/** @public */
|
|
63
110
|
export declare interface StartResult {
|
|
64
111
|
exitCode: number;
|
|
65
112
|
instance: WebAssembly.Instance;
|
|
66
113
|
}
|
|
67
114
|
|
|
115
|
+
/** @public */
|
|
116
|
+
export declare interface TerminateAllThreadsPayload {
|
|
117
|
+
}
|
|
118
|
+
|
|
68
119
|
/** @public */
|
|
69
120
|
export declare class ThreadManager {
|
|
70
121
|
unusedWorkers: WorkerLike[];
|
|
71
|
-
runningWorkers: WorkerLike[];
|
|
72
122
|
pthreads: Record<number, WorkerLike>;
|
|
73
123
|
get nextWorkerID(): number;
|
|
74
124
|
wasmModule: WebAssembly.Module | null;
|
|
@@ -79,6 +129,7 @@ export declare class ThreadManager {
|
|
|
79
129
|
private readonly _reuseWorker;
|
|
80
130
|
private readonly _beforeLoad?;
|
|
81
131
|
/* Excluded from this release type: printErr */
|
|
132
|
+
threadSpawn?: ((startArg: number, errorOrTid?: number) => number);
|
|
82
133
|
constructor(options: ThreadManagerOptions);
|
|
83
134
|
init(): void;
|
|
84
135
|
initMainThread(): void;
|
|
@@ -105,6 +156,7 @@ export declare type ThreadManagerOptions = ThreadManagerOptionsMain | ThreadMana
|
|
|
105
156
|
/** @public */
|
|
106
157
|
export declare interface ThreadManagerOptionsBase {
|
|
107
158
|
printErr?: (message: string) => void;
|
|
159
|
+
threadSpawn?: (startArg: number, errorOrTid?: number) => number;
|
|
108
160
|
}
|
|
109
161
|
|
|
110
162
|
/** @public */
|
|
@@ -126,11 +178,12 @@ export declare class ThreadMessageHandler {
|
|
|
126
178
|
private messagesBeforeLoad;
|
|
127
179
|
protected postMessage: (message: any) => void;
|
|
128
180
|
protected onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
|
181
|
+
protected onError: (error: Error, type: WorkerMessageType) => void;
|
|
129
182
|
constructor(options?: ThreadMessageHandlerOptions);
|
|
130
183
|
/** @virtual */
|
|
131
184
|
instantiate(data: LoadPayload): WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
|
132
185
|
/** @virtual */
|
|
133
|
-
handle(e: WorkerMessageEvent): void;
|
|
186
|
+
handle(e: WorkerMessageEvent<MessageEventData<WorkerMessageType>>): void;
|
|
134
187
|
private _load;
|
|
135
188
|
private _start;
|
|
136
189
|
protected _loaded(err: Error | null, source: WebAssembly.WebAssemblyInstantiatedSource | null, payload: LoadPayload): void;
|
|
@@ -140,6 +193,7 @@ export declare class ThreadMessageHandler {
|
|
|
140
193
|
/** @public */
|
|
141
194
|
export declare interface ThreadMessageHandlerOptions {
|
|
142
195
|
onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>;
|
|
196
|
+
onError?: (error: Error, type: WorkerMessageType) => void;
|
|
143
197
|
postMessage?: (message: any) => void;
|
|
144
198
|
}
|
|
145
199
|
|
|
@@ -211,4 +265,7 @@ export declare interface WorkerMessageEvent<T = any> {
|
|
|
211
265
|
data: T;
|
|
212
266
|
}
|
|
213
267
|
|
|
268
|
+
/** @public */
|
|
269
|
+
export declare type WorkerMessageType = 'load' | 'start';
|
|
270
|
+
|
|
214
271
|
export { }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e="undefined"!=typeof WebAssembly?WebAssembly:"undefined"!=typeof WXWebAssembly?WXWebAssembly:void 0,t="object"==typeof process&&null!==process&&"object"==typeof process.versions&&null!==process.versions&&"string"==typeof process.versions.node;function r(e){return"function"==typeof(null==e?void 0:e.postMessage)?e.postMessage:"function"==typeof postMessage?postMessage:void 0}function s(e){return"function"==typeof SharedArrayBuffer&&e instanceof SharedArrayBuffer||"[object SharedArrayBuffer]"===Object.prototype.toString.call(e)}function o(t){try{return t instanceof e.RuntimeError}catch(e){return!1}}function n(e,t){return{__emnapi__:{type:e,payload:t}}}function i(e){if(e){if(!s(e.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("undefined"==typeof SharedArrayBuffer)throw new Error("Current environment does not support SharedArrayBuffer, threads are not available!")}let a=0;class d{get nextWorkerID(){return a}constructor(e){var t;if(this.unusedWorkers=[],this.runningWorkers=[],this.pthreads=Object.create(null),this.wasmModule=null,this.wasmMemory=null,this.messageEvents=new WeakMap,!e)throw new TypeError("ThreadManager(): options is not provided");this._childThread="childThread"in e&&Boolean(e.childThread),this._childThread?(this._onCreateWorker=void 0,this._reuseWorker=!1,this._beforeLoad=void 0):(this._onCreateWorker=e.onCreateWorker,this._reuseWorker=function(e){var t;if("boolean"==typeof e)return!!e&&{size:0,strict:!1};if("number"==typeof e){if(!(e>=0))throw new RangeError("reuseWorker: size must be a non-negative integer");return{size:e,strict:!1}}if(!e)return!1;const r=null!==(t=Number(e.size))&&void 0!==t?t:0,s=Boolean(e.strict);if(!(r>0)&&s)throw new RangeError("reuseWorker: size must be set to positive integer if strict is set to true");return{size:r,strict:s}}(e.reuseWorker),this._beforeLoad=e.beforeLoad),this.printErr=null!==(t=e.printErr)&&void 0!==t?t:console.error.bind(console)}init(){this._childThread||this.initMainThread()}initMainThread(){this.preparePool()}preparePool(){if(this._reuseWorker&&this._reuseWorker.size){let e=this._reuseWorker.size;for(;e--;){const e=this.allocateUnusedWorker();t&&(e.once("message",(()=>{})),e.unref())}}}shouldPreloadWorkers(){return!this._childThread&&this._reuseWorker&&this._reuseWorker.size>0}loadWasmModuleToAllWorkers(){const e=Array(this.unusedWorkers.length);for(let r=0;r<this.unusedWorkers.length;++r){const s=this.unusedWorkers[r];t&&s.ref(),e[r]=this.loadWasmModuleToWorker(s).then((e=>(t&&s.unref(),e)),(e=>{throw t&&s.unref(),e}))}return Promise.all(e).catch((e=>{throw this.terminateAllThreads(),e}))}preloadWorkers(){return this.shouldPreloadWorkers()?this.loadWasmModuleToAllWorkers():Promise.resolve([])}setup(e,t){this.wasmModule=e,this.wasmMemory=t}markId(e){if(e.__emnapi_tid)return e.__emnapi_tid;const t=a+43;return a=(a+1)%536870869,this.pthreads[t]=e,e.__emnapi_tid=t,t}returnWorkerToPool(e){var r=e.__emnapi_tid;void 0!==r&&delete this.pthreads[r],this.unusedWorkers.push(e),this.runningWorkers.splice(this.runningWorkers.indexOf(e),1),delete e.__emnapi_tid,t&&e.unref()}loadWasmModuleToWorker(e,r){if(e.whenLoaded)return e.whenLoaded;const s=this.printErr,o=this._beforeLoad,a=this;return e.whenLoaded=new Promise(((d,h)=>{const l=r=>{if(r.__emnapi__){const s=r.__emnapi__.type,o=r.__emnapi__.payload;"loaded"===s?(e.loaded=!0,t&&!e.__emnapi_tid&&e.unref(),d(e)):"cleanup-thread"===s&&o.tid in this.pthreads&&this.cleanThread(e,o.tid)}};e.onmessage=t=>{l(t.data),this.fireMessageEvent(e,t)},e.onerror=function(t){let r="worker sent an error!";if(void 0!==e.__emnapi_tid&&(r="worker (tid = "+e.__emnapi_tid+") sent an error!"),s(r+" "+t.message),-1!==t.message.indexOf("RuntimeError")||-1!==t.message.indexOf("unreachable"))try{a.terminateAllThreads()}catch(e){}throw h(t),t},t&&(e.on("message",(function(t){var r,s;null===(s=(r=e).onmessage)||void 0===s||s.call(r,{data:t})})),e.on("error",(function(t){var r,s;null===(s=(r=e).onerror)||void 0===s||s.call(r,t)})),e.on("detachedExit",(function(){}))),"function"==typeof o&&o(e);try{e.postMessage(n("load",{wasmModule:this.wasmModule,wasmMemory:this.wasmMemory,sab:r}))}catch(e){throw i(this.wasmMemory),e}})),e.whenLoaded}allocateUnusedWorker(){const e=this._onCreateWorker;if("function"!=typeof e)throw new TypeError("`options.onCreateWorker` is not provided");const t=e({type:"thread",name:"emnapi-pthread"});return this.unusedWorkers.push(t),t}getNewWorker(e){if(this._reuseWorker){if(0===this.unusedWorkers.length){if(this._reuseWorker.strict&&!t){return void(0,this.printErr)("Tried to spawn a new thread, but the thread pool is exhausted.\nThis might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.")}const r=this.allocateUnusedWorker();this.loadWasmModuleToWorker(r,e)}return this.unusedWorkers.pop()}const r=this.allocateUnusedWorker();return this.loadWasmModuleToWorker(r,e),this.unusedWorkers.pop()}cleanThread(e,t,r){if(!r&&this._reuseWorker)this.returnWorkerToPool(e);else{delete this.pthreads[t];const r=this.runningWorkers.indexOf(e);-1!==r&&this.runningWorkers.splice(r,1),this.terminateWorker(e),delete e.__emnapi_tid}}terminateWorker(e){var t;const r=e.__emnapi_tid;e.terminate(),null===(t=this.messageEvents.get(e))||void 0===t||t.clear(),this.messageEvents.delete(e),e.onmessage=e=>{if(e.data.__emnapi__){(0,this.printErr)('received "'+e.data.__emnapi__.type+'" command from terminated worker: '+r)}}}terminateAllThreads(){for(let e=0;e<this.runningWorkers.length;++e)this.terminateWorker(this.runningWorkers[e]);for(let e=0;e<this.unusedWorkers.length;++e)this.terminateWorker(this.unusedWorkers[e]);this.unusedWorkers=[],this.runningWorkers=[],this.pthreads=Object.create(null),this.preparePool()}addMessageEventListener(e,t){let r=this.messageEvents.get(e);return r||(r=new Set,this.messageEvents.set(e,r)),r.add(t),()=>{null==r||r.delete(t)}}fireMessageEvent(e,t){const r=this.messageEvents.get(e);if(!r)return;const s=this.printErr;r.forEach((e=>{try{e(t)}catch(e){s(e.stack)}}))}}const h=Symbol("kIsProxy");function l(e,t){if(e[h])return e;const r=e.exports,s=function(e){const t=["apply","construct","defineProperty","deleteProperty","get","getOwnPropertyDescriptor","getPrototypeOf","has","isExtensible","ownKeys","preventExtensions","set","setPrototypeOf"],r={};for(let s=0;s<t.length;s++){const o=t[s];r[o]=function(){const t=Array.prototype.slice.call(arguments,1);return t.unshift(e),Reflect[o].apply(Reflect,t)}}return r}(r),o=()=>{},n=()=>0;s.get=function(e,s,i){var a;return"memory"===s?null!==(a="function"==typeof t?t():t)&&void 0!==a?a:Reflect.get(r,s,i):"_initialize"===s?s in r?o:void 0:"_start"===s?s in r?n:void 0:Reflect.get(r,s,i)},s.has=function(e,t){return"memory"===t||Reflect.has(r,t)};const i=new Proxy(Object.create(null),s);return new Proxy(e,{get:(e,t,r)=>"exports"===t?i:t===h||Reflect.get(e,t,r)})}const c=new WeakMap;function u(e,t){const r=Object.getOwnPropertySymbols(e),s=e=>t=>t.description?t.description===e:t.toString()===`Symbol(${e})`;return Array.isArray(t)?t.map((e=>r.filter(s(e))[0])):r.filter(s(t))[0]}function p(e,t,r){e&&(!function(e,t,r){const s=new Int32Array(e);if(Atomics.store(s,0,t),t>1&&r){const t=r.name,o=r.message,n=r.stack,i=(new TextEncoder).encode(t),a=(new TextEncoder).encode(o),d=(new TextEncoder).encode(n);Atomics.store(s,1,i.length),Atomics.store(s,2,a.length),Atomics.store(s,3,d.length);const h=new Uint8Array(e);h.set(i,16),h.set(a,16+i.length),h.set(d,16+i.length+a.length)}}(e.buffer,t,r),Atomics.notify(e,0))}exports.ThreadManager=d,exports.ThreadMessageHandler=class{constructor(e){const t=r(e);if("function"!=typeof t)throw new TypeError("options.postMessage is not a function");this.postMessage=t,this.onLoad=null==e?void 0:e.onLoad,this.instance=void 0,this.messagesBeforeLoad=[]}instantiate(e){if("function"==typeof this.onLoad)return this.onLoad(e);throw new Error("ThreadMessageHandler.prototype.instantiate is not implemented")}handle(e){var t;if(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.__emnapi__){const t=e.data.__emnapi__.type,r=e.data.__emnapi__.payload;"load"===t?this._load(r):"start"===t&&this.handleAfterLoad(e,(()=>{this._start(r)}))}}_load(e){if(void 0!==this.instance)return;let t;try{t=this.instantiate(e)}catch(t){return void this._loaded(t,null,e)}const r=t&&"then"in t?t.then:void 0;"function"==typeof r?r.call(t,(t=>{this._loaded(null,t,e)}),(t=>{this._loaded(t,null,e)})):this._loaded(null,t,e)}_start(e){if("function"!=typeof this.instance.exports.wasi_thread_start){const t=new TypeError("wasi_thread_start is not exported");throw p(e.sab,2,t),t}const t=this.postMessage,r=e.tid,s=e.arg;p(e.sab,1);try{this.instance.exports.wasi_thread_start(r,s)}catch(e){throw o(e)&&t(n("terminate-all-threads",{})),e}t(n("cleanup-thread",{tid:r}))}_loaded(e,t,r){if(e)throw p(r.sab,2,e),e;if(null==t){const e=new TypeError("onLoad should return an object");throw p(r.sab,2,e),e}const s=t.instance;if(!s){const e=new TypeError('onLoad should return an object which includes "instance"');throw p(r.sab,2,e),e}this.instance=s;(0,this.postMessage)(n("loaded",{}));const o=this.messagesBeforeLoad;this.messagesBeforeLoad=[];for(let e=0;e<o.length;e++){const t=o[e];this.handle({data:t})}}handleAfterLoad(e,t){void 0!==this.instance?t.call(this,e):this.messagesBeforeLoad.push(e.data)}},exports.WASIThreads=class{constructor(s){if(!s)throw new TypeError("WASIThreads(): options is not provided");if(!s.wasi)throw new TypeError("WASIThreads(): options.wasi is not provided");c.set(this,new WeakSet);const a=s.wasi;!function(e,t){const r=c.get(e);if(r.has(t))return;const s=e,n=t.wasiImport;if(n){const e=n.proc_exit;n.proc_exit=function(t){return s.terminateAllThreads(),e.call(this,t)}}const i=t.start;"function"==typeof i&&(t.start=function(e){try{return i.call(this,e)}catch(e){throw o(e)&&s.terminateAllThreads(),e}});r.add(t)}(this,a),this.wasi=a,this.childThread="childThread"in s&&Boolean(s.childThread),this.PThread=void 0,"threadManager"in s?"function"==typeof s.threadManager?this.PThread=s.threadManager():this.PThread=s.threadManager:this.childThread||(this.PThread=new d(s),this.PThread.init());let h=!1;"waitThreadStart"in s&&(h="number"==typeof s.waitThreadStart?s.waitThreadStart:Boolean(s.waitThreadStart));const l=r(s);if(this.childThread&&"function"!=typeof l)throw new TypeError("options.postMessage is not a function");this.postMessage=l;const u=Boolean(s.wasm64),p=e=>{if(e.data.__emnapi__){const t=e.data.__emnapi__.type,r=e.data.__emnapi__.payload;"spawn-thread"===t?f(r.startArg,r.errorOrTid):"terminate-all-threads"===t&&this.terminateAllThreads()}},f=(r,s)=>{var o;const a=void 0!==s;try{i(this.wasmMemory)}catch(e){if(null===(o=this.PThread)||void 0===o||o.printErr(e.stack),a){const e=new Int32Array(this.wasmMemory.buffer,s,2);return Atomics.store(e,0,1),Atomics.store(e,1,6),Atomics.notify(e,1),1}return-6}if(!a){const e=this.wasmInstance.exports.malloc;if(!(s=u?Number(e(BigInt(8))):e(8)))return-48}const d=this.wasmInstance.exports.free,c=u?e=>{d(BigInt(e))}:d,f=new Int32Array(this.wasmMemory.buffer,s,2);if(Atomics.store(f,0,0),Atomics.store(f,1,0),this.childThread){l(n("spawn-thread",{startArg:r,errorOrTid:s})),Atomics.wait(f,1,0);const e=Atomics.load(f,0),t=Atomics.load(f,1);return a?e:(c(s),e?-t:t)}const m=h||0===h;let w,y,_;m&&(w=new Int32Array(new SharedArrayBuffer(8208)),Atomics.store(w,0,0));const g=this.PThread;try{if(y=g.getNewWorker(w),!y)throw new Error("failed to get new worker");if(g.addMessageEventListener(y,p),_=g.markId(y),t&&y.ref(),y.postMessage(n("start",{tid:_,arg:r,sab:w})),m){if("number"==typeof h){if("timed-out"===Atomics.wait(w,0,0,h)){try{g.cleanThread(y,_,!0)}catch(e){}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(w,0,0);if(Atomics.load(w,0)>1){try{g.cleanThread(y,_,!0)}catch(e){}throw function(t){var r,s;const o=new Int32Array(t);if(Atomics.load(o,0)<=1)return null;const n=Atomics.load(o,1),i=Atomics.load(o,2),a=Atomics.load(o,3),d=new Uint8Array(t),h=d.slice(16,16+n),l=d.slice(16+n,16+n+i),c=d.slice(16+n+i,16+n+i+a),u=(new TextDecoder).decode(h),p=(new TextDecoder).decode(l),f=(new TextDecoder).decode(c),m=new(null!==(r=globalThis[u])&&void 0!==r?r:"RuntimeError"===u&&null!==(s=e.RuntimeError)&&void 0!==s?s:Error)(p);return Object.defineProperty(m,"stack",{value:f,writable:!0,enumerable:!1,configurable:!0}),m}(w.buffer)}}}catch(e){return Atomics.store(f,0,1),Atomics.store(f,1,6),Atomics.notify(f,1),null==g||g.printErr(e.stack),a?1:(c(s),-6)}return Atomics.store(f,0,0),Atomics.store(f,1,_),Atomics.notify(f,1),g.runningWorkers.push(y),m||y.whenLoaded.catch((e=>{throw delete y.whenLoaded,g.cleanThread(y,_,!0),e})),a?0:(c(s),_)};this.threadSpawn=f}getImportObject(){return{wasi:{"thread-spawn":this.threadSpawn}}}setup(e,t,r){null!=r||(r=e.exports.memory),this.wasmInstance=e,this.wasmMemory=r,this.PThread&&this.PThread.setup(t,r)}preloadWorkers(){return this.PThread?this.PThread.preloadWorkers():Promise.resolve([])}initialize(e,t,r){const s=e.exports;null!=r||(r=s.memory),this.childThread&&(e=l(e,r)),this.setup(e,t,r);const o=this.wasi;if("_start"in s&&"function"==typeof s._start)if(this.childThread){o.start(e);try{o[u(o,"kStarted")]=!1}catch(e){}}else!function(e,t){const[r,s]=u(e,["kInstance","kSetMemory"]);e[r]=t,e[s](t.exports.memory)}(o,e);else o.initialize(e);return e}start(e,t,r){const s=e.exports;null!=r||(r=s.memory),this.childThread&&(e=l(e,r)),this.setup(e,t,r);return{exitCode:this.wasi.start(e),instance:e}}terminateAllThreads(){var e;this.childThread?this.postMessage(n("terminate-all-threads",{})):null===(e=this.PThread)||void 0===e||e.terminateAllThreads()}},exports.createInstanceProxy=l,exports.isSharedArrayBuffer=s,exports.isTrapError=o;
|
|
1
|
+
const e="undefined"!=typeof WebAssembly?WebAssembly:"undefined"!=typeof WXWebAssembly?WXWebAssembly:void 0,t="object"==typeof process&&null!==process&&"object"==typeof process.versions&&null!==process.versions&&"string"==typeof process.versions.node;function r(e){return"function"==typeof(null==e?void 0:e.postMessage)?e.postMessage:"function"==typeof postMessage?postMessage:void 0}function s(e){return"function"==typeof SharedArrayBuffer&&e instanceof SharedArrayBuffer||"[object SharedArrayBuffer]"===Object.prototype.toString.call(e)}function o(t){try{return t instanceof e.RuntimeError}catch(e){return!1}}function n(e,t){return{__emnapi__:{type:e,payload:t}}}function i(e){if(e){if(!s(e.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("undefined"==typeof SharedArrayBuffer)throw new Error("Current environment does not support SharedArrayBuffer, threads are not available!")}let a=0;class d{get nextWorkerID(){return a}constructor(e){var t;if(this.unusedWorkers=[],this.pthreads=Object.create(null),this.wasmModule=null,this.wasmMemory=null,this.messageEvents=new WeakMap,!e)throw new TypeError("ThreadManager(): options is not provided");this._childThread="childThread"in e&&Boolean(e.childThread),this._childThread?(this._onCreateWorker=void 0,this._reuseWorker=!1,this._beforeLoad=void 0):(this._onCreateWorker=e.onCreateWorker,this._reuseWorker=function(e){var t;if("boolean"==typeof e)return!!e&&{size:0,strict:!1};if("number"==typeof e){if(!(e>=0))throw new RangeError("reuseWorker: size must be a non-negative integer");return{size:e,strict:!1}}if(!e)return!1;const r=null!==(t=Number(e.size))&&void 0!==t?t:0,s=Boolean(e.strict);if(!(r>0)&&s)throw new RangeError("reuseWorker: size must be set to positive integer if strict is set to true");return{size:r,strict:s}}(e.reuseWorker),this._beforeLoad=e.beforeLoad),this.printErr=null!==(t=e.printErr)&&void 0!==t?t:console.error.bind(console),this.threadSpawn=e.threadSpawn}init(){this._childThread||this.initMainThread()}initMainThread(){this.preparePool()}preparePool(){if(this._reuseWorker&&this._reuseWorker.size){let e=this._reuseWorker.size;for(;e--;){const e=this.allocateUnusedWorker();t&&(e.once("message",()=>{}),e.unref())}}}shouldPreloadWorkers(){return!this._childThread&&this._reuseWorker&&this._reuseWorker.size>0}loadWasmModuleToAllWorkers(){const e=Array(this.unusedWorkers.length);for(let r=0;r<this.unusedWorkers.length;++r){const s=this.unusedWorkers[r];t&&s.ref(),e[r]=this.loadWasmModuleToWorker(s).then(e=>(t&&s.unref(),e),e=>{throw t&&s.unref(),e})}return Promise.all(e).catch(e=>{throw this.terminateAllThreads(),e})}preloadWorkers(){return this.shouldPreloadWorkers()?this.loadWasmModuleToAllWorkers():Promise.resolve([])}setup(e,t){this.wasmModule=e,this.wasmMemory=t}markId(e){if(e.__emnapi_tid)return e.__emnapi_tid;const t=a+43;return a=(a+1)%536870869,this.pthreads[t]=e,e.__emnapi_tid=t,t}returnWorkerToPool(e){var r=e.__emnapi_tid;void 0!==r&&delete this.pthreads[r],this.unusedWorkers.push(e),delete e.__emnapi_tid,t&&e.unref()}loadWasmModuleToWorker(e,r){if(e.whenLoaded)return e.whenLoaded;const s=this.printErr,o=this._beforeLoad,a=this;return e.whenLoaded=new Promise((d,h)=>{const l=r=>{if(r.__emnapi__){const s=r.__emnapi__.type,o=r.__emnapi__.payload;"loaded"===s?(e.loaded=!0,t&&!e.__emnapi_tid&&e.unref(),d(e)):"cleanup-thread"===s?o.tid in this.pthreads&&this.cleanThread(e,o.tid):"spawn-thread"===s?this.threadSpawn(o.startArg,o.errorOrTid):"terminate-all-threads"===s&&this.terminateAllThreads()}};e.onmessage=t=>{l(t.data),this.fireMessageEvent(e,t)},e.onerror=function(t){let r="worker sent an error!";if(void 0!==e.__emnapi_tid&&(r="worker (tid = "+e.__emnapi_tid+") sent an error!"),"message"in t){if(s(r+" "+t.message),-1!==t.message.indexOf("RuntimeError")||-1!==t.message.indexOf("unreachable"))try{a.terminateAllThreads()}catch(e){}}else s(r);throw h(t),t},t&&(e.on("message",function(t){var r,s;null===(s=(r=e).onmessage)||void 0===s||s.call(r,{data:t})}),e.on("error",function(t){var r,s;null===(s=(r=e).onerror)||void 0===s||s.call(r,t)}),e.on("detachedExit",function(){})),"function"==typeof o&&o(e);try{e.postMessage(n("load",{wasmModule:this.wasmModule,wasmMemory:this.wasmMemory,sab:r}))}catch(e){throw i(this.wasmMemory),e}}),e.whenLoaded}allocateUnusedWorker(){const e=this._onCreateWorker;if("function"!=typeof e)throw new TypeError("`options.onCreateWorker` is not provided");const t=e({type:"thread",name:"emnapi-pthread"});return this.unusedWorkers.push(t),t}getNewWorker(e){if(this._reuseWorker){if(0===this.unusedWorkers.length){if(this._reuseWorker.strict&&!t){return void(0,this.printErr)("Tried to spawn a new thread, but the thread pool is exhausted.\nThis might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.")}const r=this.allocateUnusedWorker();this.loadWasmModuleToWorker(r,e)}return this.unusedWorkers.pop()}const r=this.allocateUnusedWorker();return this.loadWasmModuleToWorker(r,e),this.unusedWorkers.pop()}cleanThread(e,t,r){!r&&this._reuseWorker?this.returnWorkerToPool(e):(delete this.pthreads[t],this.terminateWorker(e),delete e.__emnapi_tid)}terminateWorker(e){var t;const r=e.__emnapi_tid;e.terminate(),null===(t=this.messageEvents.get(e))||void 0===t||t.clear(),this.messageEvents.delete(e),e.onmessage=e=>{if(e.data.__emnapi__){(0,this.printErr)('received "'+e.data.__emnapi__.type+'" command from terminated worker: '+r)}}}terminateAllThreads(){const e=Object.values(this.pthreads);for(let t=0;t<e.length;++t)this.terminateWorker(e[t]);for(let e=0;e<this.unusedWorkers.length;++e)this.terminateWorker(this.unusedWorkers[e]);this.unusedWorkers=[],this.pthreads=Object.create(null),this.preparePool()}addMessageEventListener(e,t){let r=this.messageEvents.get(e);return r||(r=new Set,this.messageEvents.set(e,r)),r.add(t),()=>{null==r||r.delete(t)}}fireMessageEvent(e,t){const r=this.messageEvents.get(e);if(!r)return;const s=this.printErr;r.forEach(e=>{try{e(t)}catch(e){s(e.stack)}})}}const h=Symbol("kIsProxy");function l(e,t){if(e[h])return e;const r=e.exports,s=function(e){const t=["apply","construct","defineProperty","deleteProperty","get","getOwnPropertyDescriptor","getPrototypeOf","has","isExtensible","ownKeys","preventExtensions","set","setPrototypeOf"],r={};for(let s=0;s<t.length;s++){const o=t[s];r[o]=function(){const t=Array.prototype.slice.call(arguments,1);return t.unshift(e),Reflect[o].apply(Reflect,t)}}return r}(r),o=()=>{},n=()=>0;s.get=function(e,s,i){var a;return"memory"===s?null!==(a="function"==typeof t?t():t)&&void 0!==a?a:Reflect.get(r,s,i):"_initialize"===s?s in r?o:void 0:"_start"===s?s in r?n:void 0:Reflect.get(r,s,i)},s.has=function(e,t){return"memory"===t||Reflect.has(r,t)};const i=new Proxy(Object.create(null),s);return new Proxy(e,{get:(e,t,r)=>"exports"===t?i:t===h||Reflect.get(e,t,r)})}const c=new WeakMap;function u(e,t){const r=Object.getOwnPropertySymbols(e),s=e=>t=>t.description?t.description===e:t.toString()===`Symbol(${e})`;return Array.isArray(t)?t.map(e=>r.filter(s(e))[0]):r.filter(s(t))[0]}function f(e,t,r){e&&(!function(e,t,r){const s=new Int32Array(e);if(Atomics.store(s,0,t),t>1&&r){const t=r.name,o=r.message,n=r.stack,i=(new TextEncoder).encode(t),a=(new TextEncoder).encode(o),d=(new TextEncoder).encode(n);Atomics.store(s,1,i.length),Atomics.store(s,2,a.length),Atomics.store(s,3,d.length);const h=new Uint8Array(e);h.set(i,16),h.set(a,16+i.length),h.set(d,16+i.length+a.length)}}(e.buffer,t,r),Atomics.notify(e,0))}exports.ThreadManager=d,exports.ThreadMessageHandler=class{constructor(e){const t=r(e);if("function"!=typeof t)throw new TypeError("options.postMessage is not a function");this.postMessage=t,this.onLoad=null==e?void 0:e.onLoad,this.onError="function"==typeof(null==e?void 0:e.onError)?e.onError:(e,t)=>{throw t},this.instance=void 0,this.messagesBeforeLoad=[]}instantiate(e){if("function"==typeof this.onLoad)return this.onLoad(e);throw new Error("ThreadMessageHandler.prototype.instantiate is not implemented")}handle(e){var t;if(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.__emnapi__){const t=e.data.__emnapi__.type,r=e.data.__emnapi__.payload;try{"load"===t?this._load(r):"start"===t&&this.handleAfterLoad(e,()=>{this._start(r)})}catch(e){this.onError(e,t)}}}_load(e){if(void 0!==this.instance)return;let t;try{t=this.instantiate(e)}catch(t){return void this._loaded(t,null,e)}const r=t&&"then"in t?t.then:void 0;"function"==typeof r?r.call(t,t=>{this._loaded(null,t,e)},t=>{this._loaded(t,null,e)}):this._loaded(null,t,e)}_start(e){const t=this.instance.exports.wasi_thread_start;if("function"!=typeof t){const t=new TypeError("wasi_thread_start is not exported");throw f(e.sab,2,t),t}const r=this.postMessage,s=e.tid,o=e.arg;f(e.sab,1);try{t(s,o)}catch(e){if("unwind"!==e)throw e;return}r(n("cleanup-thread",{tid:s}))}_loaded(e,t,r){if(e)throw f(r.sab,2,e),e;if(null==t){const e=new TypeError("onLoad should return an object");throw f(r.sab,2,e),e}const s=t.instance;if(!s){const e=new TypeError('onLoad should return an object which includes "instance"');throw f(r.sab,2,e),e}this.instance=s;(0,this.postMessage)(n("loaded",{}));const o=this.messagesBeforeLoad;this.messagesBeforeLoad=[];for(let e=0;e<o.length;e++){const t=o[e];this.handle({data:t})}}handleAfterLoad(e,t){void 0!==this.instance?t.call(this,e):this.messagesBeforeLoad.push(e.data)}},exports.WASIThreads=class{constructor(s){if(!s)throw new TypeError("WASIThreads(): options is not provided");if(!s.wasi)throw new TypeError("WASIThreads(): options.wasi is not provided");c.set(this,new WeakSet);const a=s.wasi;!function(e,t){const r=c.get(e);if(r.has(t))return;const s=e,n=t.wasiImport;if(n){const e=n.proc_exit;n.proc_exit=function(t){return s.terminateAllThreads(),e.call(this,t)}}if(!s.childThread){const e=t.start;"function"==typeof e&&(t.start=function(t){try{return e.call(this,t)}catch(e){throw o(e)&&s.terminateAllThreads(),e}})}r.add(t)}(this,a),this.wasi=a,this.childThread="childThread"in s&&Boolean(s.childThread),this.PThread=void 0,"threadManager"in s?"function"==typeof s.threadManager?this.PThread=s.threadManager():this.PThread=s.threadManager:this.childThread||(this.PThread=new d(s),this.PThread.init());let h=!1;"waitThreadStart"in s&&(h="number"==typeof s.waitThreadStart?s.waitThreadStart:Boolean(s.waitThreadStart));const l=r(s);if(this.childThread&&"function"!=typeof l)throw new TypeError("options.postMessage is not a function");this.postMessage=l;const u=Boolean(s.wasm64),f=(r,s)=>{var o;const a=void 0!==s;try{i(this.wasmMemory)}catch(e){if(null===(o=this.PThread)||void 0===o||o.printErr(e.stack),a){const e=new Int32Array(this.wasmMemory.buffer,s,2);return Atomics.store(e,0,1),Atomics.store(e,1,6),Atomics.notify(e,1),1}return-6}if(!a){const e=this.wasmInstance.exports.malloc;if(!(s=u?Number(e(BigInt(8))):e(8)>>>0))return-48}const d=this.wasmInstance.exports.free,c=u?e=>{d(BigInt(e))}:d,f=new Int32Array(this.wasmMemory.buffer,s,2);if(Atomics.store(f,0,0),Atomics.store(f,1,0),this.childThread){l(n("spawn-thread",{startArg:r,errorOrTid:s})),Atomics.wait(f,1,0);const e=Atomics.load(f,0),t=Atomics.load(f,1);return a?e:(c(s),e?-t:t)}const p=h||0===h;let m,w,y;p&&(m=new Int32Array(new SharedArrayBuffer(8208)),Atomics.store(m,0,0));const _=this.PThread;try{if(w=_.getNewWorker(m),!w)throw new Error("failed to get new worker");if(y=_.markId(w),t&&w.unref(),w.postMessage(n("start",{tid:y,arg:r,sab:m})),p){if("number"==typeof h){if("timed-out"===Atomics.wait(m,0,0,h))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(m,0,0);if(Atomics.load(m,0)>1)throw function(t){var r,s;const o=new Int32Array(t);if(Atomics.load(o,0)<=1)return null;const n=Atomics.load(o,1),i=Atomics.load(o,2),a=Atomics.load(o,3),d=new Uint8Array(t),h=d.slice(16,16+n),l=d.slice(16+n,16+n+i),c=d.slice(16+n+i,16+n+i+a),u=(new TextDecoder).decode(h),f=(new TextDecoder).decode(l),p=(new TextDecoder).decode(c),m=new(null!==(r=globalThis[u])&&void 0!==r?r:"RuntimeError"===u&&null!==(s=e.RuntimeError)&&void 0!==s?s:Error)(f);return Object.defineProperty(m,"stack",{value:p,writable:!0,enumerable:!1,configurable:!0}),m}(m.buffer)}}catch(e){if(void 0!==w&&void 0!==y)try{_.cleanThread(w,y,!0)}catch(e){}return Atomics.store(f,0,1),Atomics.store(f,1,6),Atomics.notify(f,1),null==_||_.printErr(e.stack),a?1:(c(s),-6)}return Atomics.store(f,0,0),Atomics.store(f,1,y),Atomics.notify(f,1),p||w.whenLoaded.catch(e=>{throw delete w.whenLoaded,_.cleanThread(w,y,!0),e}),a?0:(c(s),y)};this.threadSpawn=f,this.PThread&&(this.PThread.threadSpawn=f)}getImportObject(){return{wasi:{"thread-spawn":this.threadSpawn}}}setup(e,t,r){null!=r||(r=e.exports.memory),this.wasmInstance=e,this.wasmMemory=r,this.PThread&&this.PThread.setup(t,r)}preloadWorkers(){return this.PThread?this.PThread.preloadWorkers():Promise.resolve([])}initialize(e,t,r){const s=e.exports;null!=r||(r=s.memory),this.childThread&&(e=l(e,r)),this.setup(e,t,r);const o=this.wasi;if("_start"in s&&"function"==typeof s._start)if(this.childThread){o.start(e);try{o[u(o,"kStarted")]=!1}catch(e){}}else!function(e,t){const[r,s]=u(e,["kInstance","kSetMemory"]);e[r]=t,e[s](t.exports.memory)}(o,e);else o.initialize(e);return e}start(e,t,r){const s=e.exports;null!=r||(r=s.memory),this.childThread&&(e=l(e,r)),this.setup(e,t,r);return{exitCode:this.wasi.start(e),instance:e}}terminateAllThreads(){var e;this.childThread?this.postMessage(n("terminate-all-threads",{})):null===(e=this.PThread)||void 0===e||e.terminateAllThreads()}},exports.createInstanceProxy=l,exports.isSharedArrayBuffer=s,exports.isTrapError=o;
|