@emnapi/wasi-threads 2.0.0 → 2.0.1

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.
@@ -476,6 +476,36 @@ function createInstanceProxy(instance, memory) {
476
476
 
477
477
  //#region src/wasi-threads.ts
478
478
  const patchedWasiInstances = new WeakMap();
479
+ const THREAD_SPAWN_RESULT_SIZE = Int32Array.BYTES_PER_ELEMENT * 2;
480
+ const sharedArrayBufferByteLength = typeof SharedArrayBuffer === 'function'
481
+ ? Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, 'byteLength').get
482
+ : undefined;
483
+ function isConclusivelySharedArrayBuffer(buffer) {
484
+ if (sharedArrayBufferByteLength === undefined)
485
+ return false;
486
+ try {
487
+ sharedArrayBufferByteLength.call(buffer);
488
+ return true;
489
+ }
490
+ catch (_) {
491
+ return false;
492
+ }
493
+ }
494
+ function getThreadSpawnResultView(memory, address, wasm64) {
495
+ const offset = typeof address === 'bigint' ? Number(address) : address >>> 0;
496
+ let buffer = memory.buffer;
497
+ if (offset + THREAD_SPAWN_RESULT_SIZE > buffer.byteLength &&
498
+ isConclusivelySharedArrayBuffer(buffer)) {
499
+ if (wasm64) {
500
+ memory.grow(BigInt(0));
501
+ }
502
+ else {
503
+ memory.grow(0);
504
+ }
505
+ buffer = memory.buffer;
506
+ }
507
+ return new Int32Array(buffer, offset, 2);
508
+ }
479
509
  class WASIThreads {
480
510
  constructor(options) {
481
511
  if (!options) {
@@ -528,7 +558,7 @@ class WASIThreads {
528
558
  catch (err) {
529
559
  this.PThread?.printErr(err.stack);
530
560
  if (isNewABI) {
531
- const struct = new Int32Array(this.wasmMemory.buffer, errorOrTid, 2);
561
+ const struct = getThreadSpawnResultView(this.wasmMemory, errorOrTid, wasm64);
532
562
  Atomics.store(struct, 0, 1);
533
563
  Atomics.store(struct, 1, EAGAIN);
534
564
  Atomics.notify(struct, 1);
@@ -547,7 +577,7 @@ class WASIThreads {
547
577
  }
548
578
  const _free = this.wasmInstance.exports.free;
549
579
  const free = wasm64 ? (ptr) => { _free(BigInt(ptr)); } : _free;
550
- const struct = new Int32Array(this.wasmMemory.buffer, errorOrTid, 2);
580
+ const struct = getThreadSpawnResultView(this.wasmMemory, errorOrTid, wasm64);
551
581
  Atomics.store(struct, 0, 0);
552
582
  Atomics.store(struct, 1, 0);
553
583
  if (this.childThread) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emnapi/wasi-threads",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "WASI threads proposal implementation in JavaScript",
5
5
  "type": "module",
6
6
  "main": "./dist/wasi-threads.js",