sharedthread 1.0.0
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.cjs +1026 -0
- package/dist/index.d.cts +272 -0
- package/dist/index.d.ts +272 -0
- package/dist/index.js +976 -0
- package/package.json +41 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1026 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Int32: () => Int32,
|
|
34
|
+
MainThread: () => MainThread,
|
|
35
|
+
SharedArray: () => SharedArray,
|
|
36
|
+
SharedHeap: () => SharedHeap,
|
|
37
|
+
SharedInt32: () => SharedInt32,
|
|
38
|
+
SharedPointer: () => SharedPointer,
|
|
39
|
+
SharedPrimitive: () => SharedPrimitive,
|
|
40
|
+
SharedReference: () => SharedReference,
|
|
41
|
+
SharedStruct: () => SharedStruct,
|
|
42
|
+
SharedType: () => SharedType,
|
|
43
|
+
SharedUint32: () => SharedUint32,
|
|
44
|
+
Thread: () => Thread,
|
|
45
|
+
TypeRegistry: () => TypeRegistry,
|
|
46
|
+
Uint32: () => Uint32,
|
|
47
|
+
WorkerThread: () => WorkerThreadExport,
|
|
48
|
+
isMainThread: () => import_node_worker_threads5.isMainThread
|
|
49
|
+
});
|
|
50
|
+
module.exports = __toCommonJS(index_exports);
|
|
51
|
+
var import_node_worker_threads5 = require("worker_threads");
|
|
52
|
+
|
|
53
|
+
// src/Memory/TypeRegistry.ts
|
|
54
|
+
var TypeRegistry = class _TypeRegistry {
|
|
55
|
+
static indexToType = /* @__PURE__ */ new Map();
|
|
56
|
+
static typeToIndex = /* @__PURE__ */ new Map();
|
|
57
|
+
static hashToType = /* @__PURE__ */ new Map();
|
|
58
|
+
static typeToHash = /* @__PURE__ */ new Map();
|
|
59
|
+
static indexToHash = new Uint32Array(0);
|
|
60
|
+
// private static cyrb53(str: string, seed: number = 0): number {
|
|
61
|
+
// let h1 = 0xdeadbeef ^ seed;
|
|
62
|
+
// let h2 = 0x41c6ce57 ^ seed;
|
|
63
|
+
// for (let i = 0, ch: number; i < str.length; i++) {
|
|
64
|
+
// ch = str.charCodeAt(i);
|
|
65
|
+
// h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
66
|
+
// h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
67
|
+
// }
|
|
68
|
+
// h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
|
|
69
|
+
// h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
70
|
+
// h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
|
|
71
|
+
// h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
72
|
+
// // Combine the two 32-bit hashes into a single 53-bit unsigned integer
|
|
73
|
+
// return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
74
|
+
// }
|
|
75
|
+
static fnv1(str) {
|
|
76
|
+
let hash = 2166136261;
|
|
77
|
+
for (let i = 0; i < str.length; i++) {
|
|
78
|
+
hash ^= str.charCodeAt(i);
|
|
79
|
+
hash = Math.imul(hash, 16777619);
|
|
80
|
+
}
|
|
81
|
+
return hash >>> 0;
|
|
82
|
+
}
|
|
83
|
+
static registerType(dataType) {
|
|
84
|
+
if (this.typeToIndex.has(dataType)) throw new Error("class is alreadly registered");
|
|
85
|
+
let idx = _TypeRegistry.indexToType.size;
|
|
86
|
+
let hash;
|
|
87
|
+
do {
|
|
88
|
+
hash = _TypeRegistry.fnv1(dataType.toString());
|
|
89
|
+
} while (_TypeRegistry.hashToType.has(hash));
|
|
90
|
+
_TypeRegistry.indexToType.set(idx, dataType);
|
|
91
|
+
_TypeRegistry.typeToIndex.set(dataType, idx);
|
|
92
|
+
_TypeRegistry.hashToType.set(hash, dataType);
|
|
93
|
+
_TypeRegistry.typeToHash.set(dataType, hash);
|
|
94
|
+
let nextHashes = new Uint32Array(idx + 1);
|
|
95
|
+
nextHashes.set(_TypeRegistry.indexToHash);
|
|
96
|
+
nextHashes[idx] = hash;
|
|
97
|
+
_TypeRegistry.indexToHash = nextHashes;
|
|
98
|
+
dataType.typeID = idx;
|
|
99
|
+
return idx;
|
|
100
|
+
}
|
|
101
|
+
static getTypeBuffer() {
|
|
102
|
+
return new Uint32Array(_TypeRegistry.indexToHash).buffer;
|
|
103
|
+
}
|
|
104
|
+
static verifyTypeBuffer(typeBuffer) {
|
|
105
|
+
const arrayEqual = (a, b) => a.length == b.length && a.every((v, i) => v == b[i]);
|
|
106
|
+
const setEqual = (a, b) => a.length === b.length && ((m) => a.every((x) => m.set(x, (m.get(x) || 0) + 1)) && b.every((x) => m.get(x) && m.set(x, m.get(x) - 1)))(/* @__PURE__ */ new Map());
|
|
107
|
+
let hashArray = new Uint32Array(typeBuffer);
|
|
108
|
+
let isEqual = arrayEqual(this.indexToHash, hashArray);
|
|
109
|
+
let isSetEqual = setEqual(this.indexToHash, hashArray);
|
|
110
|
+
if (!isEqual) {
|
|
111
|
+
if (isSetEqual) {
|
|
112
|
+
throw new Error("type registrations are out of order");
|
|
113
|
+
} else {
|
|
114
|
+
throw new Error("type registrations mismatched");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
static getTypeByIndex(index) {
|
|
119
|
+
if (!this.indexToType.has(index)) throw new Error("cannot find type by index");
|
|
120
|
+
return this.indexToType.get(index);
|
|
121
|
+
}
|
|
122
|
+
static getTypeByHash(hash) {
|
|
123
|
+
if (!this.hashToType.has(hash)) throw new Error("cannot find type by hash");
|
|
124
|
+
return this.hashToType.get(hash);
|
|
125
|
+
}
|
|
126
|
+
static getIndexByType(type) {
|
|
127
|
+
if (!this.typeToIndex.has(type)) throw new Error("cannot find id by type");
|
|
128
|
+
return this.typeToIndex.get(type);
|
|
129
|
+
}
|
|
130
|
+
static getHashByType(type) {
|
|
131
|
+
if (!this.typeToHash.has(type)) throw new Error("cannot find hash by index");
|
|
132
|
+
return this.typeToHash.get(type);
|
|
133
|
+
}
|
|
134
|
+
constructor() {
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// src/Memory/SharedHeap.ts
|
|
139
|
+
var SharedHeap = class _SharedHeap {
|
|
140
|
+
static heaps = [];
|
|
141
|
+
static getHeapByID(i) {
|
|
142
|
+
if (_SharedHeap.heaps[i] === void 0) throw new Error(`cannot find heap with heapID`);
|
|
143
|
+
return _SharedHeap.heaps[i];
|
|
144
|
+
}
|
|
145
|
+
_heapID;
|
|
146
|
+
_heapSize;
|
|
147
|
+
_buffer;
|
|
148
|
+
_view;
|
|
149
|
+
// Bit-Packing Constants for the Lower 32-bit Header
|
|
150
|
+
static SHIFT_TYPEID = 0;
|
|
151
|
+
static SHIFT_ARRAY = 23;
|
|
152
|
+
static SHIFT_PTR = 26;
|
|
153
|
+
static SHIFT_ALLOC = 29;
|
|
154
|
+
static MASK_TYPEID = 8388607;
|
|
155
|
+
// 23 bits
|
|
156
|
+
static MASK_ARRAY = 7;
|
|
157
|
+
// 3 bits
|
|
158
|
+
static MASK_PTR = 7;
|
|
159
|
+
// 3 bits
|
|
160
|
+
static MASK_ALLOC = 7;
|
|
161
|
+
// 3 bits
|
|
162
|
+
constructor(b, heapID = _SharedHeap.heaps.length) {
|
|
163
|
+
if (_SharedHeap.heaps[heapID] !== void 0) throw new Error("heap assignment overlap");
|
|
164
|
+
this._heapID = heapID;
|
|
165
|
+
_SharedHeap.heaps[this._heapID] = this;
|
|
166
|
+
if (b instanceof SharedArrayBuffer) {
|
|
167
|
+
this._heapSize = b.byteLength;
|
|
168
|
+
this._buffer = b;
|
|
169
|
+
this._view = new DataView(this._buffer);
|
|
170
|
+
} else {
|
|
171
|
+
this._heapSize = b + 4 + 3 & ~3;
|
|
172
|
+
this._buffer = new SharedArrayBuffer(this._heapSize);
|
|
173
|
+
this._view = new DataView(this._buffer);
|
|
174
|
+
this.listHead = -1;
|
|
175
|
+
const initialPayloadSize = this._heapSize - 4 - 8;
|
|
176
|
+
this.writeHeader(4, initialPayloadSize, 0, 0, 0, 0);
|
|
177
|
+
this._view.setInt32(12, -1, true);
|
|
178
|
+
this._view.setInt32(16, -1, true);
|
|
179
|
+
this.syncFreeList();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
get heapID() {
|
|
183
|
+
return this._heapID;
|
|
184
|
+
}
|
|
185
|
+
get view() {
|
|
186
|
+
return this._view;
|
|
187
|
+
}
|
|
188
|
+
get heapSize() {
|
|
189
|
+
return this._heapSize;
|
|
190
|
+
}
|
|
191
|
+
get buffer() {
|
|
192
|
+
return this._buffer;
|
|
193
|
+
}
|
|
194
|
+
// Dynamically reads and writes to the beginning of the SharedArrayBuffer
|
|
195
|
+
get listHead() {
|
|
196
|
+
return this._view.getInt32(0, true);
|
|
197
|
+
}
|
|
198
|
+
set listHead(offset) {
|
|
199
|
+
this._view.setInt32(0, offset, true);
|
|
200
|
+
}
|
|
201
|
+
// --- Fixed Dual 32-Bit Bit-Packing Utilities ---
|
|
202
|
+
getHeaderSize(offset) {
|
|
203
|
+
return this._view.getUint32(offset, true);
|
|
204
|
+
}
|
|
205
|
+
getHeaderAllocated(offset) {
|
|
206
|
+
const lowerHeader = this._view.getUint32(offset + 4, true);
|
|
207
|
+
return lowerHeader >>> _SharedHeap.SHIFT_ALLOC & _SharedHeap.MASK_ALLOC;
|
|
208
|
+
}
|
|
209
|
+
writeHeader(offset, size, alloc, ptr, array, typeID) {
|
|
210
|
+
if (typeID < 0 || typeID > _SharedHeap.MASK_TYPEID) throw new Error("typeID out of bounds");
|
|
211
|
+
if (size < 0 || size > 4294967295) throw new Error("size exceeds 32-bit limit");
|
|
212
|
+
if (alloc < 0 || alloc > 7 || ptr < 0 || ptr > 7 || array < 0 || array > 7) throw new Error("alloc, ptr, and array must fit in 3 bits (0-7)");
|
|
213
|
+
this._view.setUint32(offset, size, true);
|
|
214
|
+
const lowerHeader = (alloc & _SharedHeap.MASK_ALLOC) << _SharedHeap.SHIFT_ALLOC | (ptr & _SharedHeap.MASK_PTR) << _SharedHeap.SHIFT_PTR | (array & _SharedHeap.MASK_ARRAY) << _SharedHeap.SHIFT_ARRAY | (typeID & _SharedHeap.MASK_TYPEID) << _SharedHeap.SHIFT_TYPEID;
|
|
215
|
+
this._view.setUint32(offset + 4, lowerHeader >>> 0, true);
|
|
216
|
+
}
|
|
217
|
+
syncFreeList() {
|
|
218
|
+
let current = 4;
|
|
219
|
+
this.listHead = -1;
|
|
220
|
+
let lastFree = -1;
|
|
221
|
+
while (current < this._heapSize) {
|
|
222
|
+
const size = this.getHeaderSize(current);
|
|
223
|
+
const allocVal = this.getHeaderAllocated(current);
|
|
224
|
+
if (allocVal === 0) {
|
|
225
|
+
if (this.listHead === -1) {
|
|
226
|
+
this.listHead = current;
|
|
227
|
+
}
|
|
228
|
+
if (lastFree !== -1) {
|
|
229
|
+
this._view.setInt32(lastFree + 8, current, true);
|
|
230
|
+
this._view.setInt32(current + 12, lastFree, true);
|
|
231
|
+
}
|
|
232
|
+
lastFree = current;
|
|
233
|
+
}
|
|
234
|
+
current += 8 + size;
|
|
235
|
+
}
|
|
236
|
+
if (lastFree !== -1) {
|
|
237
|
+
this._view.setInt32(lastFree + 8, -1, true);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
allocate(requestedSize, typeID, isPtr = false, isArray = false) {
|
|
241
|
+
if (isNaN(requestedSize)) throw new Error("cannot allocate NaN size");
|
|
242
|
+
if (requestedSize <= 0) throw new Error("cannot allocate negative size");
|
|
243
|
+
const allocValue = 1;
|
|
244
|
+
const ptrValue = isPtr ? 1 : 0;
|
|
245
|
+
const arrayValue = isArray ? 1 : 0;
|
|
246
|
+
const alignedSize = Math.max(requestedSize + 3 & ~3, 4);
|
|
247
|
+
let currentOffset = this.listHead;
|
|
248
|
+
while (currentOffset !== -1) {
|
|
249
|
+
const size = this.getHeaderSize(currentOffset);
|
|
250
|
+
if (size >= alignedSize) {
|
|
251
|
+
const remainingSpace = size - alignedSize - 8;
|
|
252
|
+
if (remainingSpace >= 12) {
|
|
253
|
+
const nextFreeOffset = currentOffset + 8 + alignedSize;
|
|
254
|
+
this.replaceFreeNode(currentOffset, nextFreeOffset);
|
|
255
|
+
this.writeHeader(currentOffset, alignedSize, allocValue, ptrValue, arrayValue, typeID);
|
|
256
|
+
this.writeHeader(nextFreeOffset, remainingSpace, 0, 0, 0, 0);
|
|
257
|
+
} else {
|
|
258
|
+
this.removeFreeNode(currentOffset);
|
|
259
|
+
this.writeHeader(currentOffset, size, allocValue, ptrValue, arrayValue, typeID);
|
|
260
|
+
}
|
|
261
|
+
return currentOffset + 8;
|
|
262
|
+
}
|
|
263
|
+
currentOffset = this._view.getInt32(currentOffset + 8, true);
|
|
264
|
+
}
|
|
265
|
+
throw new Error("heap out of memory");
|
|
266
|
+
}
|
|
267
|
+
free(ptr) {
|
|
268
|
+
if (ptr < 12 || ptr >= this._heapSize) throw new Error("cannot free out of bounds");
|
|
269
|
+
const headerOffset = ptr - 8;
|
|
270
|
+
const size = this.getHeaderSize(headerOffset);
|
|
271
|
+
this.writeHeader(headerOffset, size, 0, 0, 0, 0);
|
|
272
|
+
this.addFreeNodeToFront(headerOffset);
|
|
273
|
+
}
|
|
274
|
+
addFreeNodeToFront(offset) {
|
|
275
|
+
const oldHead = this.listHead;
|
|
276
|
+
this.listHead = offset;
|
|
277
|
+
this._view.setInt32(offset + 8, oldHead, true);
|
|
278
|
+
this._view.setInt32(offset + 12, -1, true);
|
|
279
|
+
if (oldHead !== -1) {
|
|
280
|
+
this._view.setInt32(oldHead + 12, offset, true);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
// --- Public External Metadata Readers (Accepts Pointer Address, Reads Header) ---
|
|
284
|
+
getTypeIDAt(ptr) {
|
|
285
|
+
if (ptr < 12 || ptr >= this._heapSize) return 0;
|
|
286
|
+
const lowerHeader = this._view.getUint32(ptr - 4, true);
|
|
287
|
+
return lowerHeader >>> _SharedHeap.SHIFT_TYPEID & _SharedHeap.MASK_TYPEID;
|
|
288
|
+
}
|
|
289
|
+
getAllocAt(ptr) {
|
|
290
|
+
if (ptr < 12 || ptr >= this._heapSize) return 0;
|
|
291
|
+
const lowerHeader = this._view.getUint32(ptr - 4, true);
|
|
292
|
+
return lowerHeader >>> _SharedHeap.SHIFT_ALLOC & _SharedHeap.MASK_ALLOC;
|
|
293
|
+
}
|
|
294
|
+
getPtrAt(ptr) {
|
|
295
|
+
if (ptr < 12 || ptr >= this._heapSize) return 0;
|
|
296
|
+
const lowerHeader = this._view.getUint32(ptr - 4, true);
|
|
297
|
+
return lowerHeader >>> _SharedHeap.SHIFT_PTR & _SharedHeap.MASK_PTR;
|
|
298
|
+
}
|
|
299
|
+
getArrayAt(ptr) {
|
|
300
|
+
if (ptr < 12 || ptr >= this._heapSize) return 0;
|
|
301
|
+
const lowerHeader = this._view.getUint32(ptr - 4, true);
|
|
302
|
+
return lowerHeader >>> _SharedHeap.SHIFT_ARRAY & _SharedHeap.MASK_ARRAY;
|
|
303
|
+
}
|
|
304
|
+
// --- Private Free-List Node Handlers ---
|
|
305
|
+
removeFreeNode(offset) {
|
|
306
|
+
const next = this._view.getInt32(offset + 8, true);
|
|
307
|
+
const prev = this._view.getInt32(offset + 12, true);
|
|
308
|
+
if (prev !== -1) this._view.setInt32(prev + 8, next, true);
|
|
309
|
+
else this.listHead = next;
|
|
310
|
+
if (next !== -1) this._view.setInt32(next + 12, prev, true);
|
|
311
|
+
}
|
|
312
|
+
replaceFreeNode(oldOffset, newOffset) {
|
|
313
|
+
const next = this._view.getInt32(oldOffset + 8, true);
|
|
314
|
+
const prev = this._view.getInt32(oldOffset + 12, true);
|
|
315
|
+
if (next !== -1) this._view.setInt32(next + 12, newOffset, true);
|
|
316
|
+
if (prev !== -1) this._view.setInt32(prev + 8, newOffset, true);
|
|
317
|
+
else this.listHead = newOffset;
|
|
318
|
+
this._view.setInt32(newOffset + 8, next, true);
|
|
319
|
+
this._view.setInt32(newOffset + 12, prev, true);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
// src/Memory/SharedType.ts
|
|
324
|
+
var SharedType = class {
|
|
325
|
+
_addr;
|
|
326
|
+
_heap;
|
|
327
|
+
constructor(heap, addr) {
|
|
328
|
+
if (!isFinite(addr) || addr < 0) throw new Error("invalid address");
|
|
329
|
+
this._heap = heap;
|
|
330
|
+
this._addr = addr;
|
|
331
|
+
}
|
|
332
|
+
get addr() {
|
|
333
|
+
return this._addr;
|
|
334
|
+
}
|
|
335
|
+
get heap() {
|
|
336
|
+
return this._heap;
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
// src/Memory/SharedPrimitive.ts
|
|
341
|
+
var SharedPrimitive = class extends SharedType {
|
|
342
|
+
static typeID;
|
|
343
|
+
static byteSize;
|
|
344
|
+
static isPtr = false;
|
|
345
|
+
static isArr = false;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
// src/Memory/SharedReference.ts
|
|
349
|
+
var SharedReference = class extends SharedType {
|
|
350
|
+
static typeID;
|
|
351
|
+
static isPtr = false;
|
|
352
|
+
static isArr = false;
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
// src/Memory/SharedPointer.ts
|
|
356
|
+
var SharedPointer = class _SharedPointer extends SharedPrimitive {
|
|
357
|
+
static byteSize = 4;
|
|
358
|
+
static isPtr = true;
|
|
359
|
+
static fromData(heap, v) {
|
|
360
|
+
let addr = heap.allocate(_SharedPointer.byteSize, v.type.typeID, _SharedPointer.isPtr);
|
|
361
|
+
let obj = new _SharedPointer(heap, addr);
|
|
362
|
+
if (v.addr) obj.value = addr;
|
|
363
|
+
return obj;
|
|
364
|
+
}
|
|
365
|
+
_heldType;
|
|
366
|
+
_deref = null;
|
|
367
|
+
constructor(heap, addr, dataType) {
|
|
368
|
+
super(heap, addr);
|
|
369
|
+
this._heldType = dataType ?? TypeRegistry.getTypeByIndex(heap.getTypeIDAt(addr));
|
|
370
|
+
}
|
|
371
|
+
get heldType() {
|
|
372
|
+
return this._heldType;
|
|
373
|
+
}
|
|
374
|
+
get addr() {
|
|
375
|
+
return this._addr;
|
|
376
|
+
}
|
|
377
|
+
get heap() {
|
|
378
|
+
return this._heap;
|
|
379
|
+
}
|
|
380
|
+
set value(p) {
|
|
381
|
+
this._heap.view.setUint32(this._addr, p);
|
|
382
|
+
if (this.heap.getArrayAt(p)) {
|
|
383
|
+
if (this._heldType != SharedArray) throw new Error("pointer points to wrong type");
|
|
384
|
+
} else {
|
|
385
|
+
if (this._heldType != TypeRegistry.getTypeByIndex(this._heap.getTypeIDAt(this._addr))) throw new Error("pointer points to wrong type");
|
|
386
|
+
}
|
|
387
|
+
this._deref = new this._heldType(this._heap, p);
|
|
388
|
+
}
|
|
389
|
+
get value() {
|
|
390
|
+
return this._heap.view.getUint32(this._addr);
|
|
391
|
+
}
|
|
392
|
+
set deref(v) {
|
|
393
|
+
if (!(v instanceof this._heldType)) throw new Error("assigned wrong type to pointer");
|
|
394
|
+
this.value = v.addr;
|
|
395
|
+
this._deref = v;
|
|
396
|
+
}
|
|
397
|
+
get deref() {
|
|
398
|
+
return this._deref;
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
// src/Memory/SharedUint32.ts
|
|
403
|
+
function Uint32(value) {
|
|
404
|
+
if (!Number.isInteger(value) || value < -2147483648 || value > 2147483647) {
|
|
405
|
+
throw new RangeError("cannot cast value to uint32");
|
|
406
|
+
}
|
|
407
|
+
return value;
|
|
408
|
+
}
|
|
409
|
+
var SharedUint32 = class _SharedUint32 extends SharedPrimitive {
|
|
410
|
+
static byteSize = 4;
|
|
411
|
+
static fromData(heap, v) {
|
|
412
|
+
let addr = heap.allocate(_SharedUint32.byteSize, _SharedUint32.typeID);
|
|
413
|
+
let obj = new _SharedUint32(heap, addr);
|
|
414
|
+
obj.value = v;
|
|
415
|
+
return obj;
|
|
416
|
+
}
|
|
417
|
+
set value(v) {
|
|
418
|
+
this._heap.view.setUint32(this._addr, Uint32(v));
|
|
419
|
+
}
|
|
420
|
+
get value() {
|
|
421
|
+
return this._heap.view.getUint32(this._addr);
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
TypeRegistry.registerType(SharedUint32);
|
|
425
|
+
|
|
426
|
+
// src/Memory/SharedArray.ts
|
|
427
|
+
var SharedArray = class extends SharedReference {
|
|
428
|
+
static isArr = true;
|
|
429
|
+
static properties = {
|
|
430
|
+
length: { type: SharedUint32 }
|
|
431
|
+
};
|
|
432
|
+
static fromData(heap, v) {
|
|
433
|
+
let length = v.length;
|
|
434
|
+
let type = v.type;
|
|
435
|
+
if (type.prototype instanceof SharedPrimitive) {
|
|
436
|
+
type = type;
|
|
437
|
+
} else {
|
|
438
|
+
type = SharedPointer;
|
|
439
|
+
}
|
|
440
|
+
let elementSize = type.byteSize;
|
|
441
|
+
let totalSize = 4 + elementSize * length;
|
|
442
|
+
let addr = heap.allocate(totalSize, type.typeID, false, true);
|
|
443
|
+
heap.view.setUint32(addr, length);
|
|
444
|
+
let obj = new this(heap, addr);
|
|
445
|
+
if (v.array) {
|
|
446
|
+
for (let i = 0; i < length; i++) {
|
|
447
|
+
obj.elements[i] = v.array[i];
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return obj;
|
|
451
|
+
}
|
|
452
|
+
_heldType;
|
|
453
|
+
_elementType;
|
|
454
|
+
_elementSize;
|
|
455
|
+
_byteSize;
|
|
456
|
+
_length;
|
|
457
|
+
elements = [];
|
|
458
|
+
constructor(heap, addr) {
|
|
459
|
+
super(heap, addr);
|
|
460
|
+
this._elementType = TypeRegistry.getTypeByIndex(heap.getTypeIDAt(addr));
|
|
461
|
+
this._heldType = heap.getPtrAt(addr) ? SharedPointer : this._elementType;
|
|
462
|
+
this._elementSize = this._heldType.byteSize;
|
|
463
|
+
this._length = heap.view.getUint32(addr);
|
|
464
|
+
this._byteSize = 4 + this._elementSize * this._length;
|
|
465
|
+
let offset = 4;
|
|
466
|
+
for (let i = 0; i < this._length; i++, offset += this._elementSize) {
|
|
467
|
+
this.elements.push(new this._heldType(heap, addr + offset));
|
|
468
|
+
}
|
|
469
|
+
return new Proxy(this, {
|
|
470
|
+
get(target, prop, receiver) {
|
|
471
|
+
if (Number.isInteger(Number(prop)) && prop >= 0 && prop < target._length) {
|
|
472
|
+
return target.elements[prop];
|
|
473
|
+
}
|
|
474
|
+
return Reflect.get(target, prop, receiver);
|
|
475
|
+
},
|
|
476
|
+
set(target, prop, value, receiver) {
|
|
477
|
+
if (typeof prop == "number" && prop >= 0 && prop < target._length) {
|
|
478
|
+
if (target.elements[prop] && "value" in target.elements[prop]) {
|
|
479
|
+
target.elements[prop].value = value;
|
|
480
|
+
return true;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return Reflect.set(target, prop, value, receiver);
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
get addr() {
|
|
488
|
+
return this._addr;
|
|
489
|
+
}
|
|
490
|
+
get heap() {
|
|
491
|
+
return this._heap;
|
|
492
|
+
}
|
|
493
|
+
get heldType() {
|
|
494
|
+
return this._heldType;
|
|
495
|
+
}
|
|
496
|
+
get elementType() {
|
|
497
|
+
return this._elementType;
|
|
498
|
+
}
|
|
499
|
+
get elementSize() {
|
|
500
|
+
return this._elementSize;
|
|
501
|
+
}
|
|
502
|
+
static get byteSize() {
|
|
503
|
+
throw new Error("cannot precalculate array size");
|
|
504
|
+
}
|
|
505
|
+
get byteSize() {
|
|
506
|
+
return this._byteSize;
|
|
507
|
+
}
|
|
508
|
+
get length() {
|
|
509
|
+
return this._length;
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
// src/Memory/SharedInt32.ts
|
|
514
|
+
function Int32(value) {
|
|
515
|
+
if (!Number.isInteger(value) || value < -2147483648 || value > 2147483647) {
|
|
516
|
+
throw new RangeError("cannot cast value to int32");
|
|
517
|
+
}
|
|
518
|
+
return value;
|
|
519
|
+
}
|
|
520
|
+
var SharedInt32 = class _SharedInt32 extends SharedPrimitive {
|
|
521
|
+
static byteSize = 4;
|
|
522
|
+
static fromData(heap, v) {
|
|
523
|
+
let addr = heap.allocate(_SharedInt32.byteSize, _SharedInt32.typeID);
|
|
524
|
+
let obj = new _SharedInt32(heap, addr);
|
|
525
|
+
obj.value = v;
|
|
526
|
+
return obj;
|
|
527
|
+
}
|
|
528
|
+
set value(v) {
|
|
529
|
+
this._heap.view.setInt32(this._addr, Int32(v));
|
|
530
|
+
}
|
|
531
|
+
get value() {
|
|
532
|
+
return this._heap.view.getInt32(this._addr);
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
TypeRegistry.registerType(SharedInt32);
|
|
536
|
+
|
|
537
|
+
// src/Memory/SharedStruct.ts
|
|
538
|
+
var SharedStruct = class _SharedStruct extends SharedReference {
|
|
539
|
+
static fromData(heap, param) {
|
|
540
|
+
let addr = heap.allocate(this.byteSize, this.typeID);
|
|
541
|
+
let obj = new this(heap, addr);
|
|
542
|
+
obj.imprint(param);
|
|
543
|
+
return obj;
|
|
544
|
+
}
|
|
545
|
+
static properties;
|
|
546
|
+
_byteSize = 0;
|
|
547
|
+
properties = {};
|
|
548
|
+
constructor(heap, addr) {
|
|
549
|
+
super(heap, addr);
|
|
550
|
+
const prop = this.constructor.properties;
|
|
551
|
+
for (let key in prop) {
|
|
552
|
+
let dataDeclaration = prop[key];
|
|
553
|
+
let dataType = dataDeclaration.type;
|
|
554
|
+
let params = dataDeclaration.param;
|
|
555
|
+
let data;
|
|
556
|
+
let propSize;
|
|
557
|
+
if (SharedPrimitive.isPrototypeOf(dataType)) {
|
|
558
|
+
dataType = dataType;
|
|
559
|
+
if (dataType == SharedPointer) {
|
|
560
|
+
data = new SharedPointer(heap, addr + this._byteSize, params.type);
|
|
561
|
+
propSize = SharedPointer.byteSize;
|
|
562
|
+
} else {
|
|
563
|
+
data = new dataType(heap, addr + this._byteSize);
|
|
564
|
+
if (params !== void 0) data.value = params;
|
|
565
|
+
propSize = dataType.byteSize;
|
|
566
|
+
}
|
|
567
|
+
} else if ("type" in dataDeclaration && "param" in dataDeclaration) {
|
|
568
|
+
data = dataDeclaration.type.fromData(heap, dataDeclaration.param);
|
|
569
|
+
let p = new SharedPointer(heap, addr + this._byteSize, dataDeclaration.type);
|
|
570
|
+
propSize = SharedPointer.byteSize;
|
|
571
|
+
p.value = data.addr;
|
|
572
|
+
data = p;
|
|
573
|
+
} else {
|
|
574
|
+
throw new Error("invalid declaration");
|
|
575
|
+
}
|
|
576
|
+
this._byteSize += propSize;
|
|
577
|
+
this.properties[key] = data;
|
|
578
|
+
}
|
|
579
|
+
queueMicrotask(() => {
|
|
580
|
+
Object.preventExtensions(this);
|
|
581
|
+
});
|
|
582
|
+
return new Proxy(this, {
|
|
583
|
+
get(target, prop2, receiver) {
|
|
584
|
+
if (prop2 in target.properties) {
|
|
585
|
+
let v = target.properties[prop2];
|
|
586
|
+
return v;
|
|
587
|
+
}
|
|
588
|
+
return Reflect.get(target, prop2, receiver);
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
imprint(obj) {
|
|
593
|
+
if (obj == void 0) return;
|
|
594
|
+
for (let key in obj) {
|
|
595
|
+
if (key in this.properties) {
|
|
596
|
+
let ref = this.properties[key];
|
|
597
|
+
if (ref instanceof _SharedStruct) {
|
|
598
|
+
ref.imprint(obj[key]);
|
|
599
|
+
} else if (ref instanceof SharedPrimitive) {
|
|
600
|
+
ref.value = obj[key];
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
static get byteSize() {
|
|
606
|
+
let size = 0;
|
|
607
|
+
let properties = this.properties;
|
|
608
|
+
for (let key in properties) {
|
|
609
|
+
let declaration = properties[key];
|
|
610
|
+
let dataType = declaration.type;
|
|
611
|
+
if (SharedPrimitive.isPrototypeOf(dataType)) {
|
|
612
|
+
size += dataType.byteSize;
|
|
613
|
+
} else {
|
|
614
|
+
size += SharedPointer.byteSize;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return size;
|
|
618
|
+
}
|
|
619
|
+
get byteSize() {
|
|
620
|
+
let size = 0;
|
|
621
|
+
let properties = this.constructor.properties;
|
|
622
|
+
for (let key in properties) {
|
|
623
|
+
let declaration = properties[key];
|
|
624
|
+
let dataType = declaration.type;
|
|
625
|
+
if (SharedPrimitive.isPrototypeOf(dataType)) {
|
|
626
|
+
size += dataType.byteSize;
|
|
627
|
+
} else {
|
|
628
|
+
size += SharedPointer.byteSize;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return size;
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
// src/Threads/MainThread.ts
|
|
636
|
+
var import_node_worker_threads2 = require("worker_threads");
|
|
637
|
+
|
|
638
|
+
// src/Threads/Thread.ts
|
|
639
|
+
var import_node_events = require("events");
|
|
640
|
+
var import_node_worker_threads = require("worker_threads");
|
|
641
|
+
var Thread = class extends import_node_events.EventEmitter {
|
|
642
|
+
static async sendToThread(threadId3, data, label = null) {
|
|
643
|
+
if (!data) return;
|
|
644
|
+
return (0, import_node_worker_threads.postMessageToThread)(threadId3, {
|
|
645
|
+
tag: "message",
|
|
646
|
+
label,
|
|
647
|
+
data
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
static async transferToThread(threadId3, data, transferList, label = null) {
|
|
651
|
+
if (!data) return;
|
|
652
|
+
return (0, import_node_worker_threads.postMessageToThread)(threadId3, {
|
|
653
|
+
tag: "message",
|
|
654
|
+
label,
|
|
655
|
+
data
|
|
656
|
+
}, transferList);
|
|
657
|
+
}
|
|
658
|
+
port;
|
|
659
|
+
config;
|
|
660
|
+
constructor(port, config = {}) {
|
|
661
|
+
super();
|
|
662
|
+
this.port = port;
|
|
663
|
+
this.config = config;
|
|
664
|
+
}
|
|
665
|
+
send(data, label = null) {
|
|
666
|
+
if (!data) return;
|
|
667
|
+
this.port.postMessage({
|
|
668
|
+
tag: "message",
|
|
669
|
+
label,
|
|
670
|
+
data
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
sendWithTransfer(data, transferList, label = null) {
|
|
674
|
+
if (!data) return;
|
|
675
|
+
this.port.postMessage({
|
|
676
|
+
tag: "message",
|
|
677
|
+
label,
|
|
678
|
+
data
|
|
679
|
+
}, transferList);
|
|
680
|
+
}
|
|
681
|
+
async request(data, label = null) {
|
|
682
|
+
if (!data) return;
|
|
683
|
+
this.port.postMessage({
|
|
684
|
+
tag: "message",
|
|
685
|
+
label,
|
|
686
|
+
data
|
|
687
|
+
});
|
|
688
|
+
return new Promise((res, rej) => {
|
|
689
|
+
const handler = (data2, label2) => {
|
|
690
|
+
if (label == label2) {
|
|
691
|
+
res(data2);
|
|
692
|
+
this.off("message", handler);
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
this.on("message", handler);
|
|
696
|
+
if (this.config.timeout) setTimeout(() => {
|
|
697
|
+
rej(new Error("failed to receive response"));
|
|
698
|
+
this.off("message", handler);
|
|
699
|
+
}, this.config.timeout);
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
listenOnce(cb, label = null) {
|
|
703
|
+
const handler = (data, label2) => {
|
|
704
|
+
if (label == label2) {
|
|
705
|
+
cb(data);
|
|
706
|
+
this.off("message", handler);
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
this.on("message", handler);
|
|
710
|
+
}
|
|
711
|
+
listenAll(cb, label = null) {
|
|
712
|
+
this.on("message", (data, label2) => {
|
|
713
|
+
if (label == label2) {
|
|
714
|
+
cb(data);
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
respondOnce(cb, label = null) {
|
|
719
|
+
const handler = (data, label2) => {
|
|
720
|
+
if (label == label2) {
|
|
721
|
+
this.send(cb(data), label);
|
|
722
|
+
this.off("message", handler);
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
this.on("message", handler);
|
|
726
|
+
}
|
|
727
|
+
respondAll(cb, label = null) {
|
|
728
|
+
this.on("message", (data, label2) => {
|
|
729
|
+
if (label == label2) {
|
|
730
|
+
this.send(cb(data), label);
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
async addHeap(heap, name) {
|
|
735
|
+
if (heap instanceof SharedArrayBuffer) {
|
|
736
|
+
heap = new SharedHeap(heap);
|
|
737
|
+
}
|
|
738
|
+
const buffer = heap.buffer;
|
|
739
|
+
this.port.postMessage({
|
|
740
|
+
tag: "sync",
|
|
741
|
+
name,
|
|
742
|
+
buffer,
|
|
743
|
+
heapID: heap.heapID,
|
|
744
|
+
rebound: false
|
|
745
|
+
});
|
|
746
|
+
return new Promise((res, rej) => {
|
|
747
|
+
const handler = (name2, buffer2, heapID, rebound) => {
|
|
748
|
+
if (rebound && name == name2 && buffer2 == null && heapID == heap.heapID) {
|
|
749
|
+
res();
|
|
750
|
+
this.off("sync", handler);
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
this.on("sync", handler);
|
|
754
|
+
if (this.config.timeout) setTimeout(() => {
|
|
755
|
+
rej(new Error("failed to receive response"));
|
|
756
|
+
this.off("sync", handler);
|
|
757
|
+
}, this.config.timeout);
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
async syncHeap(name) {
|
|
761
|
+
return new Promise((res, rej) => {
|
|
762
|
+
const handler = (name2, buffer, heapID, rebound) => {
|
|
763
|
+
if (!rebound && name == name2) {
|
|
764
|
+
const heap = new SharedHeap(buffer, heapID);
|
|
765
|
+
this.port.postMessage({
|
|
766
|
+
tag: "sync",
|
|
767
|
+
name,
|
|
768
|
+
buffer: null,
|
|
769
|
+
heapID: heap.heapID,
|
|
770
|
+
rebound: true
|
|
771
|
+
});
|
|
772
|
+
this.off("sync", handler);
|
|
773
|
+
res(heap);
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
this.on("sync", handler);
|
|
777
|
+
if (this.config.timeout) setTimeout(() => {
|
|
778
|
+
rej(new Error("failed to receive response"));
|
|
779
|
+
this.off("sync", handler);
|
|
780
|
+
}, this.config.timeout);
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
async addVar(data, name) {
|
|
784
|
+
let heapID = data.heap.heapID;
|
|
785
|
+
let addr = data.addr;
|
|
786
|
+
this.port.postMessage({
|
|
787
|
+
tag: "assign",
|
|
788
|
+
name,
|
|
789
|
+
heapID,
|
|
790
|
+
addr,
|
|
791
|
+
rebound: false
|
|
792
|
+
});
|
|
793
|
+
return new Promise((res, rej) => {
|
|
794
|
+
const handler = (name2, heapID2, addr2, rebound) => {
|
|
795
|
+
if (rebound && name2 == name && heapID2 == heapID && addr2 == addr) {
|
|
796
|
+
this.off("assign", handler);
|
|
797
|
+
res();
|
|
798
|
+
}
|
|
799
|
+
};
|
|
800
|
+
this.on("assign", handler);
|
|
801
|
+
if (this.config.timeout) setTimeout(() => {
|
|
802
|
+
rej(new Error("failed to receive response"));
|
|
803
|
+
this.off("assign", handler);
|
|
804
|
+
}, this.config.timeout);
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
async syncVar(name) {
|
|
808
|
+
return new Promise((res, rej) => {
|
|
809
|
+
const handler = (name2, heapID, addr, rebound) => {
|
|
810
|
+
if (!rebound && name2 == name) {
|
|
811
|
+
let heap = SharedHeap.getHeapByID(heapID);
|
|
812
|
+
let isArr = heap.getArrayAt(addr);
|
|
813
|
+
let isPtr = heap.getPtrAt(addr);
|
|
814
|
+
let dataType = TypeRegistry.getTypeByIndex(heap.getTypeIDAt(addr));
|
|
815
|
+
let data;
|
|
816
|
+
if (isArr) {
|
|
817
|
+
data = new SharedArray(heap, addr);
|
|
818
|
+
} else if (isPtr) {
|
|
819
|
+
data = new SharedPointer(heap, addr);
|
|
820
|
+
} else {
|
|
821
|
+
data = new dataType(heap, addr);
|
|
822
|
+
}
|
|
823
|
+
this.port.postMessage({
|
|
824
|
+
tag: "assign",
|
|
825
|
+
name,
|
|
826
|
+
heapID,
|
|
827
|
+
addr,
|
|
828
|
+
rebound: true
|
|
829
|
+
});
|
|
830
|
+
res(data);
|
|
831
|
+
this.off("assign", handler);
|
|
832
|
+
}
|
|
833
|
+
};
|
|
834
|
+
this.on("assign", handler);
|
|
835
|
+
if (this.config.timeout) setTimeout(() => {
|
|
836
|
+
rej(new Error("failed to receive response"));
|
|
837
|
+
this.off("assign", handler);
|
|
838
|
+
}, this.config.timeout);
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
};
|
|
842
|
+
|
|
843
|
+
// src/Threads/MainThread.ts
|
|
844
|
+
var import_node_os = __toESM(require("os"), 1);
|
|
845
|
+
var MainThread = class extends Thread {
|
|
846
|
+
static get optimalTypeRegistrys() {
|
|
847
|
+
return import_node_os.default.availableParallelism();
|
|
848
|
+
}
|
|
849
|
+
worker;
|
|
850
|
+
active = false;
|
|
851
|
+
constructor(path, config = {}) {
|
|
852
|
+
config = { ...config };
|
|
853
|
+
config.workerOptions ??= {};
|
|
854
|
+
config.workerOptions.transferList ??= [];
|
|
855
|
+
config.workerOptions.workerData = {
|
|
856
|
+
config: {
|
|
857
|
+
timeout: config.timeout,
|
|
858
|
+
logLevel: config.logLevel
|
|
859
|
+
},
|
|
860
|
+
default: config.workerOptions.workerData
|
|
861
|
+
};
|
|
862
|
+
if (config.useTypescript) {
|
|
863
|
+
if (config.workerOptions.execArgv) {
|
|
864
|
+
config.workerOptions.execArgv.push("--import");
|
|
865
|
+
config.workerOptions.execArgv.push("tsx");
|
|
866
|
+
} else {
|
|
867
|
+
config.workerOptions.execArgv = [...process.execArgv, "--import", "tsx"];
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
let typeBuffer = TypeRegistry.getTypeBuffer();
|
|
871
|
+
config.workerOptions.transferList.push(typeBuffer);
|
|
872
|
+
config.workerOptions.workerData = {
|
|
873
|
+
orig: config.workerOptions.workerData,
|
|
874
|
+
//extra data here for syncing
|
|
875
|
+
types: typeBuffer
|
|
876
|
+
};
|
|
877
|
+
if (path instanceof Function) {
|
|
878
|
+
path = path.toString();
|
|
879
|
+
config.workerOptions.eval = true;
|
|
880
|
+
}
|
|
881
|
+
let worker = new import_node_worker_threads2.Worker(path, config.workerOptions);
|
|
882
|
+
let port = worker;
|
|
883
|
+
super(port, config);
|
|
884
|
+
this.worker = worker;
|
|
885
|
+
this.port.on("message", (msg) => {
|
|
886
|
+
this.emit("internalmessage", msg);
|
|
887
|
+
switch (msg.tag) {
|
|
888
|
+
case "message":
|
|
889
|
+
this.emit("message", msg.data, msg.label);
|
|
890
|
+
break;
|
|
891
|
+
case "sync":
|
|
892
|
+
this.emit("sync", msg.name, msg.buffer, msg.heapID, msg.rebound);
|
|
893
|
+
break;
|
|
894
|
+
case "assign":
|
|
895
|
+
this.emit("assign", msg.name, msg.heapID, msg.addr, msg.rebound);
|
|
896
|
+
break;
|
|
897
|
+
}
|
|
898
|
+
if (this.config.transient) {
|
|
899
|
+
this.worker.terminate();
|
|
900
|
+
}
|
|
901
|
+
});
|
|
902
|
+
this.worker.on("error", (err) => {
|
|
903
|
+
let caught = this.emit("error", err);
|
|
904
|
+
if (!caught) {
|
|
905
|
+
this.err(err);
|
|
906
|
+
}
|
|
907
|
+
});
|
|
908
|
+
this.port.on("messageerror", (err) => {
|
|
909
|
+
let caught = this.emit("messageerror", err);
|
|
910
|
+
if (!caught) {
|
|
911
|
+
this.err(err);
|
|
912
|
+
}
|
|
913
|
+
});
|
|
914
|
+
this.worker.on("online", () => {
|
|
915
|
+
this.emit("online");
|
|
916
|
+
this.active = true;
|
|
917
|
+
});
|
|
918
|
+
this.worker.on("exit", (exitCode) => {
|
|
919
|
+
this.active = false;
|
|
920
|
+
this.emit("exit", exitCode);
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
async ready() {
|
|
924
|
+
if (this.active) return;
|
|
925
|
+
return new Promise((res) => {
|
|
926
|
+
this.port.once("online", () => {
|
|
927
|
+
res();
|
|
928
|
+
});
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
terminate() {
|
|
932
|
+
if (!this.active) this.err("cannot terminate worker(worker is not active)");
|
|
933
|
+
this.active = false;
|
|
934
|
+
return this.worker.terminate();
|
|
935
|
+
}
|
|
936
|
+
// private log(level: number, msg: string): void{
|
|
937
|
+
// ///////TODO
|
|
938
|
+
// if(level >= (this.config.logLevel ?? 0)){
|
|
939
|
+
// console.log(`${new Date().toTimeString()} - ${threadName}(${threadId}) - ${msg}`);
|
|
940
|
+
// }
|
|
941
|
+
// }
|
|
942
|
+
err(msg) {
|
|
943
|
+
throw new Error(`${(/* @__PURE__ */ new Date()).toTimeString()} - ${import_node_worker_threads2.threadName}(${import_node_worker_threads2.threadId}) - ${msg}`);
|
|
944
|
+
}
|
|
945
|
+
get isActive() {
|
|
946
|
+
return this.active;
|
|
947
|
+
}
|
|
948
|
+
//functions that pass through the call
|
|
949
|
+
ref() {
|
|
950
|
+
this.port.ref();
|
|
951
|
+
}
|
|
952
|
+
unref() {
|
|
953
|
+
this.port.unref();
|
|
954
|
+
}
|
|
955
|
+
//todo
|
|
956
|
+
// worker.cpuUsage([prev])
|
|
957
|
+
// worker.getHeapSnapshot([options])
|
|
958
|
+
// worker.getHeapStatistics()
|
|
959
|
+
// worker.performance
|
|
960
|
+
// worker.stderr
|
|
961
|
+
// worker.stdin
|
|
962
|
+
// worker.stdout
|
|
963
|
+
};
|
|
964
|
+
|
|
965
|
+
// src/Threads/WorkerThread.ts
|
|
966
|
+
var import_node_worker_threads3 = require("worker_threads");
|
|
967
|
+
var WorkerThread = class extends Thread {
|
|
968
|
+
_workerData = import_node_worker_threads3.workerData.workerData;
|
|
969
|
+
constructor() {
|
|
970
|
+
if (import_node_worker_threads3.parentPort == null) throw new Error("cannot find parent port");
|
|
971
|
+
super(import_node_worker_threads3.parentPort, import_node_worker_threads3.workerData.config);
|
|
972
|
+
this._workerData = import_node_worker_threads3.workerData.default;
|
|
973
|
+
queueMicrotask(() => {
|
|
974
|
+
TypeRegistry.verifyTypeBuffer(import_node_worker_threads3.workerData.types);
|
|
975
|
+
});
|
|
976
|
+
this.port.on("message", (msg) => {
|
|
977
|
+
this.emit("internalmessage", msg);
|
|
978
|
+
switch (msg.tag) {
|
|
979
|
+
case "message":
|
|
980
|
+
this.emit("message", msg.data, msg.label);
|
|
981
|
+
break;
|
|
982
|
+
case "sync":
|
|
983
|
+
this.emit("sync", msg.name, msg.buffer, msg.heapID, msg.rebound);
|
|
984
|
+
break;
|
|
985
|
+
case "assign":
|
|
986
|
+
this.emit("assign", msg.name, msg.heapID, msg.addr, msg.rebound);
|
|
987
|
+
break;
|
|
988
|
+
}
|
|
989
|
+
});
|
|
990
|
+
this.port.on("messageerror", (err) => {
|
|
991
|
+
let caught = this.emit("messageerror", err);
|
|
992
|
+
if (!caught) {
|
|
993
|
+
this.err(err);
|
|
994
|
+
}
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
get workerData() {
|
|
998
|
+
return this._workerData;
|
|
999
|
+
}
|
|
1000
|
+
err(msg) {
|
|
1001
|
+
throw new Error(`${(/* @__PURE__ */ new Date()).toTimeString()} - ${import_node_worker_threads3.threadName}(${import_node_worker_threads3.threadId}) - ${msg}`);
|
|
1002
|
+
}
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
// src/Threads/index.ts
|
|
1006
|
+
var import_node_worker_threads4 = require("worker_threads");
|
|
1007
|
+
var WorkerThreadExport = import_node_worker_threads4.isMainThread ? WorkerThread : new WorkerThread();
|
|
1008
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1009
|
+
0 && (module.exports = {
|
|
1010
|
+
Int32,
|
|
1011
|
+
MainThread,
|
|
1012
|
+
SharedArray,
|
|
1013
|
+
SharedHeap,
|
|
1014
|
+
SharedInt32,
|
|
1015
|
+
SharedPointer,
|
|
1016
|
+
SharedPrimitive,
|
|
1017
|
+
SharedReference,
|
|
1018
|
+
SharedStruct,
|
|
1019
|
+
SharedType,
|
|
1020
|
+
SharedUint32,
|
|
1021
|
+
Thread,
|
|
1022
|
+
TypeRegistry,
|
|
1023
|
+
Uint32,
|
|
1024
|
+
WorkerThread,
|
|
1025
|
+
isMainThread
|
|
1026
|
+
});
|