@gjsify/v8 0.1.13 → 0.2.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.
@@ -0,0 +1,37 @@
1
+ import GLib from "@girs/glib-2.0";
2
+ function readProcStatus() {
3
+ const map = /* @__PURE__ */ new Map();
4
+ try {
5
+ const [ok, contents] = GLib.file_get_contents("/proc/self/status");
6
+ if (!ok || !contents) return map;
7
+ const text = new TextDecoder().decode(contents);
8
+ for (const line of text.split("\n")) {
9
+ const m = /^(\w+):\s+(\d+)(\s+kB)?/.exec(line);
10
+ if (m) map.set(m[1], parseInt(m[2]) * (m[3] ? 1024 : 1));
11
+ }
12
+ } catch {
13
+ }
14
+ return map;
15
+ }
16
+ function getHeapStatistics() {
17
+ const proc = readProcStatus();
18
+ return {
19
+ total_heap_size: proc.get("VmSize") ?? 0,
20
+ total_heap_size_executable: 0,
21
+ total_physical_size: proc.get("VmRSS") ?? 0,
22
+ total_available_size: 0,
23
+ used_heap_size: proc.get("VmRSS") ?? 0,
24
+ heap_size_limit: 0,
25
+ malloced_memory: proc.get("VmData") ?? 0,
26
+ peak_malloced_memory: proc.get("VmPeak") ?? 0,
27
+ does_zap_garbage: 0,
28
+ number_of_native_contexts: 0,
29
+ number_of_detached_contexts: 0,
30
+ total_global_handles_size: 0,
31
+ used_global_handles_size: 0,
32
+ external_memory: 0
33
+ };
34
+ }
35
+ export {
36
+ getHeapStatistics
37
+ };
package/lib/esm/index.js CHANGED
@@ -1,30 +1,24 @@
1
- function getHeapStatistics() {
2
- return {
3
- total_heap_size: 0,
4
- total_heap_size_executable: 0,
5
- total_physical_size: 0,
6
- total_available_size: 0,
7
- used_heap_size: 0,
8
- heap_size_limit: 0,
9
- malloced_memory: 0,
10
- peak_malloced_memory: 0,
11
- does_zap_garbage: 0,
12
- number_of_native_contexts: 0,
13
- number_of_detached_contexts: 0,
14
- external_memory: 0
15
- };
1
+ import { getHeapStatistics } from "./heap.js";
2
+ import {
3
+ Serializer,
4
+ Deserializer,
5
+ DefaultSerializer,
6
+ DefaultDeserializer
7
+ } from "./serdes.js";
8
+ function serialize(value) {
9
+ const ser = new DefaultSerializer();
10
+ ser.writeHeader();
11
+ ser.writeValue(value);
12
+ return ser.releaseBuffer();
13
+ }
14
+ function deserialize(buffer) {
15
+ const des = new DefaultDeserializer(buffer);
16
+ des.readHeader();
17
+ return des.readValue();
16
18
  }
17
19
  function getHeapSpaceStatistics() {
18
20
  return [];
19
21
  }
20
- function setFlagsFromString(_flags) {
21
- }
22
- function getHeapSnapshot() {
23
- return null;
24
- }
25
- function writeHeapSnapshot(_filename) {
26
- return "";
27
- }
28
22
  function getHeapCodeStatistics() {
29
23
  return {
30
24
  code_and_metadata_size: 0,
@@ -33,30 +27,86 @@ function getHeapCodeStatistics() {
33
27
  cpu_profiler_metadata_size: 0
34
28
  };
35
29
  }
36
- function serialize(value) {
37
- return Buffer.from(JSON.stringify(value));
30
+ function setFlagsFromString(_flags) {
38
31
  }
39
- function deserialize(buffer) {
40
- return JSON.parse(buffer.toString());
32
+ function getHeapSnapshot(_options) {
33
+ return null;
34
+ }
35
+ function writeHeapSnapshot(_filename) {
36
+ return "";
37
+ }
38
+ function isStringOneByteRepresentation(content) {
39
+ for (let i = 0; i < content.length; i++) {
40
+ if (content.charCodeAt(i) > 255) return false;
41
+ }
42
+ return true;
43
+ }
44
+ class GCProfiler {
45
+ #running = false;
46
+ #startTime = 0;
47
+ start() {
48
+ if (this.#running) return;
49
+ this.#running = true;
50
+ this.#startTime = Date.now();
51
+ }
52
+ stop() {
53
+ if (!this.#running) return void 0;
54
+ this.#running = false;
55
+ try {
56
+ const system = globalThis.imports?.system;
57
+ if (typeof system?.gc === "function") system.gc();
58
+ } catch {
59
+ }
60
+ return { version: 1, startTime: this.#startTime, endTime: Date.now(), stats: [] };
61
+ }
62
+ [Symbol.dispose]() {
63
+ this.stop();
64
+ }
65
+ }
66
+ class SyncCPUProfileHandle {
67
+ stop() {
68
+ return void 0;
69
+ }
70
+ [Symbol.dispose]() {
71
+ this.stop();
72
+ }
73
+ }
74
+ function startCpuProfile() {
75
+ return new SyncCPUProfileHandle();
41
76
  }
42
77
  var index_default = {
43
78
  getHeapStatistics,
44
79
  getHeapSpaceStatistics,
80
+ getHeapCodeStatistics,
45
81
  setFlagsFromString,
46
82
  getHeapSnapshot,
47
83
  writeHeapSnapshot,
48
- getHeapCodeStatistics,
49
84
  serialize,
50
- deserialize
85
+ deserialize,
86
+ isStringOneByteRepresentation,
87
+ Serializer,
88
+ Deserializer,
89
+ DefaultSerializer,
90
+ DefaultDeserializer,
91
+ GCProfiler,
92
+ startCpuProfile
51
93
  };
52
94
  export {
95
+ DefaultDeserializer,
96
+ DefaultSerializer,
97
+ Deserializer,
98
+ GCProfiler,
99
+ Serializer,
100
+ SyncCPUProfileHandle,
53
101
  index_default as default,
54
102
  deserialize,
55
103
  getHeapCodeStatistics,
56
104
  getHeapSnapshot,
57
105
  getHeapSpaceStatistics,
58
106
  getHeapStatistics,
107
+ isStringOneByteRepresentation,
59
108
  serialize,
60
109
  setFlagsFromString,
110
+ startCpuProfile,
61
111
  writeHeapSnapshot
62
112
  };
@@ -0,0 +1,536 @@
1
+ import { Buffer } from "node:buffer";
2
+ const WIRE_VERSION = 15;
3
+ const kVersion = 255;
4
+ const kUndefined = 95;
5
+ const kNull = 48;
6
+ const kTrue = 84;
7
+ const kFalse = 70;
8
+ const kInt32 = 73;
9
+ const kUint32 = 85;
10
+ const kDouble = 78;
11
+ const kBigInt = 90;
12
+ const kOneByteString = 34;
13
+ const kUtf8String = 99;
14
+ const kDate = 68;
15
+ const kRegExp = 82;
16
+ const kBeginJSObject = 111;
17
+ const kEndJSObject = 123;
18
+ const kBeginDenseArray = 65;
19
+ const kEndDenseArray = 36;
20
+ const kArrayBuffer = 66;
21
+ const kObjectReference = 94;
22
+ const kHostObject = 92;
23
+ function writeVarint(buf, n) {
24
+ n = n >>> 0;
25
+ while (n >= 128) {
26
+ buf.push(n & 127 | 128);
27
+ n >>>= 7;
28
+ }
29
+ buf.push(n);
30
+ }
31
+ function readVarint(buf, pos) {
32
+ let result = 0, shift = 0, byte;
33
+ do {
34
+ if (pos >= buf.length) throw new Error("Unexpected end of buffer reading varint");
35
+ byte = buf[pos++];
36
+ result |= (byte & 127) << shift;
37
+ shift += 7;
38
+ } while (byte & 128);
39
+ return [result >>> 0, pos];
40
+ }
41
+ function zigZagEncode(n) {
42
+ return n >= 0 ? n * 2 : -n * 2 - 1;
43
+ }
44
+ function zigZagDecode(n) {
45
+ return n & 1 ? -(n >>> 1) - 1 : n >>> 1;
46
+ }
47
+ function writeFloat64(buf, d) {
48
+ const tmp = new DataView(new ArrayBuffer(8));
49
+ tmp.setFloat64(
50
+ 0,
51
+ d,
52
+ true
53
+ /* LE */
54
+ );
55
+ for (let i = 0; i < 8; i++) buf.push(tmp.getUint8(i));
56
+ }
57
+ function readFloat64(buf, pos) {
58
+ if (pos + 8 > buf.length) throw new Error("ReadDouble() failed");
59
+ const view = new DataView(buf.buffer, buf.byteOffset + pos, 8);
60
+ return [view.getFloat64(0, true), pos + 8];
61
+ }
62
+ function isOneByte(s) {
63
+ for (let i = 0; i < s.length; i++) {
64
+ if (s.charCodeAt(i) > 255) return false;
65
+ }
66
+ return true;
67
+ }
68
+ function typedArrayToIndex(abView) {
69
+ const tag = Object.prototype.toString.call(abView);
70
+ if (tag === "[object Int8Array]") return 0;
71
+ if (tag === "[object Uint8Array]") return 1;
72
+ if (tag === "[object Uint8ClampedArray]") return 2;
73
+ if (tag === "[object Int16Array]") return 3;
74
+ if (tag === "[object Uint16Array]") return 4;
75
+ if (tag === "[object Int32Array]") return 5;
76
+ if (tag === "[object Uint32Array]") return 6;
77
+ if (tag === "[object Float32Array]") return 7;
78
+ if (tag === "[object Float64Array]") return 8;
79
+ if (tag === "[object DataView]") return 9;
80
+ if (tag === "[object BigInt64Array]") return 11;
81
+ if (tag === "[object BigUint64Array]") return 12;
82
+ return -1;
83
+ }
84
+ function indexToTypedArray(index) {
85
+ switch (index) {
86
+ case 0:
87
+ return Int8Array;
88
+ case 1:
89
+ return Uint8Array;
90
+ case 2:
91
+ return Uint8ClampedArray;
92
+ case 3:
93
+ return Int16Array;
94
+ case 4:
95
+ return Uint16Array;
96
+ case 5:
97
+ return Int32Array;
98
+ case 6:
99
+ return Uint32Array;
100
+ case 7:
101
+ return Float32Array;
102
+ case 8:
103
+ return Float64Array;
104
+ case 9:
105
+ return DataView;
106
+ case 10:
107
+ return Buffer;
108
+ case 11:
109
+ return BigInt64Array;
110
+ case 12:
111
+ return BigUint64Array;
112
+ }
113
+ return void 0;
114
+ }
115
+ class Serializer {
116
+ _bytes = [];
117
+ _seen = [];
118
+ _treatAbvAsHostObjects = false;
119
+ _getDataCloneError = Error;
120
+ writeHeader() {
121
+ this._bytes.push(kVersion);
122
+ writeVarint(this._bytes, WIRE_VERSION);
123
+ }
124
+ writeValue(value) {
125
+ if (value !== null && value !== void 0 && typeof value === "object") {
126
+ const idx = this._seen.indexOf(value);
127
+ if (idx !== -1) {
128
+ this._bytes.push(kObjectReference);
129
+ writeVarint(this._bytes, idx);
130
+ return;
131
+ }
132
+ }
133
+ if (value === void 0) {
134
+ this._bytes.push(kUndefined);
135
+ } else if (value === null) {
136
+ this._bytes.push(kNull);
137
+ } else if (value === true) {
138
+ this._bytes.push(kTrue);
139
+ } else if (value === false) {
140
+ this._bytes.push(kFalse);
141
+ } else if (typeof value === "number") {
142
+ if (Number.isInteger(value) && value >= -(2 ** 31) && value <= 2 ** 32 - 1) {
143
+ if (value >= 0) {
144
+ this._bytes.push(kUint32);
145
+ writeVarint(this._bytes, value);
146
+ } else {
147
+ this._bytes.push(kInt32);
148
+ writeVarint(this._bytes, zigZagEncode(value));
149
+ }
150
+ } else {
151
+ this._bytes.push(kDouble);
152
+ writeFloat64(this._bytes, value);
153
+ }
154
+ } else if (typeof value === "bigint") {
155
+ this._writeBigInt(value);
156
+ } else if (typeof value === "string") {
157
+ this._writeString(value);
158
+ } else if (typeof value === "object") {
159
+ const obj = value;
160
+ if (value instanceof Date) {
161
+ this._seen.push(obj);
162
+ this._bytes.push(kDate);
163
+ writeFloat64(this._bytes, value.getTime());
164
+ } else if (value instanceof RegExp) {
165
+ this._seen.push(obj);
166
+ this._bytes.push(kRegExp);
167
+ this._writeString(value.source);
168
+ writeVarint(this._bytes, regExpFlagsToInt(value));
169
+ } else if (value instanceof ArrayBuffer) {
170
+ this._seen.push(obj);
171
+ const bytes = new Uint8Array(value);
172
+ this._bytes.push(kArrayBuffer);
173
+ writeVarint(this._bytes, bytes.length);
174
+ for (let i = 0; i < bytes.length; i++) this._bytes.push(bytes[i]);
175
+ } else if (this._treatAbvAsHostObjects && (ArrayBuffer.isView(value) || Buffer.isBuffer(value))) {
176
+ this._seen.push(obj);
177
+ this._bytes.push(kHostObject);
178
+ this._writeHostObject(obj);
179
+ } else if (Array.isArray(value)) {
180
+ this._seen.push(obj);
181
+ const arr = value;
182
+ this._bytes.push(kBeginDenseArray);
183
+ writeVarint(this._bytes, arr.length);
184
+ for (let i = 0; i < arr.length; i++) this.writeValue(arr[i]);
185
+ this._bytes.push(kEndDenseArray);
186
+ writeVarint(this._bytes, 0);
187
+ writeVarint(this._bytes, arr.length);
188
+ } else {
189
+ this._seen.push(obj);
190
+ this._bytes.push(kBeginJSObject);
191
+ const keys = Object.keys(obj);
192
+ for (const key of keys) {
193
+ this._writeString(key);
194
+ this.writeValue(obj[key]);
195
+ }
196
+ this._bytes.push(kEndJSObject);
197
+ writeVarint(this._bytes, keys.length);
198
+ }
199
+ } else {
200
+ throw new this._getDataCloneError(
201
+ `${String(value)} could not be cloned.`
202
+ );
203
+ }
204
+ }
205
+ releaseBuffer() {
206
+ const result = Buffer.from(this._bytes);
207
+ this._bytes = [];
208
+ this._seen = [];
209
+ return result;
210
+ }
211
+ writeUint32(n) {
212
+ writeVarint(this._bytes, n >>> 0);
213
+ }
214
+ writeUint64(hi, lo) {
215
+ writeVarint(this._bytes, hi >>> 0);
216
+ writeVarint(this._bytes, lo >>> 0);
217
+ }
218
+ writeDouble(d) {
219
+ writeFloat64(this._bytes, d);
220
+ }
221
+ writeRawBytes(source) {
222
+ if (!ArrayBuffer.isView(source)) {
223
+ throw new TypeError("source must be a TypedArray or a DataView");
224
+ }
225
+ const bytes = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
226
+ for (let i = 0; i < bytes.length; i++) this._bytes.push(bytes[i]);
227
+ }
228
+ _writeHostObject(_obj) {
229
+ throw new this._getDataCloneError(
230
+ `Unserializable host object: ${Object.prototype.toString.call(_obj)}`
231
+ );
232
+ }
233
+ _setTreatArrayBufferViewsAsHostObjects(flag) {
234
+ this._treatAbvAsHostObjects = flag;
235
+ }
236
+ _writeString(s) {
237
+ if (isOneByte(s)) {
238
+ this._bytes.push(kOneByteString);
239
+ writeVarint(this._bytes, s.length);
240
+ for (let i = 0; i < s.length; i++) this._bytes.push(s.charCodeAt(i) & 255);
241
+ } else {
242
+ const encoded = new TextEncoder().encode(s);
243
+ this._bytes.push(kUtf8String);
244
+ writeVarint(this._bytes, encoded.length);
245
+ for (let i = 0; i < encoded.length; i++) this._bytes.push(encoded[i]);
246
+ }
247
+ }
248
+ _writeBigInt(n) {
249
+ this._bytes.push(kBigInt);
250
+ const negative = n < 0n;
251
+ const abs = negative ? -n : n;
252
+ const words = [];
253
+ let remaining = abs;
254
+ while (remaining > 0n) {
255
+ words.push(Number(remaining & 0xFFFFFFFFn));
256
+ words.push(Number(remaining >> 32n & 0xFFFFFFFFn));
257
+ remaining >>= 64n;
258
+ }
259
+ const wordCount = words.length;
260
+ const bitfield = wordCount << 1 | (negative ? 1 : 0);
261
+ writeVarint(this._bytes, bitfield);
262
+ for (const w of words) {
263
+ this._bytes.push(w & 255);
264
+ this._bytes.push(w >> 8 & 255);
265
+ this._bytes.push(w >> 16 & 255);
266
+ this._bytes.push(w >> 24 & 255);
267
+ }
268
+ if (words.length === 0) {
269
+ }
270
+ }
271
+ }
272
+ class Deserializer {
273
+ _pos = 0;
274
+ _wireVersion = 0;
275
+ _seen = [];
276
+ buffer;
277
+ constructor(buffer) {
278
+ if (!ArrayBuffer.isView(buffer) && !(buffer instanceof ArrayBuffer)) {
279
+ throw new TypeError("buffer must be a TypedArray or a DataView");
280
+ }
281
+ if (buffer instanceof ArrayBuffer) {
282
+ this.buffer = Buffer.from(buffer);
283
+ } else {
284
+ this.buffer = Buffer.from(
285
+ buffer.buffer,
286
+ buffer.byteOffset,
287
+ buffer.byteLength
288
+ );
289
+ }
290
+ }
291
+ readHeader() {
292
+ if (this.buffer[this._pos] !== kVersion) {
293
+ throw new Error("ReadHeader() failed");
294
+ }
295
+ this._pos++;
296
+ const [ver, pos] = readVarint(this.buffer, this._pos);
297
+ this._wireVersion = ver;
298
+ this._pos = pos;
299
+ return true;
300
+ }
301
+ getWireFormatVersion() {
302
+ return this._wireVersion;
303
+ }
304
+ readValue() {
305
+ if (this._pos >= this.buffer.length) {
306
+ throw new Error("Unexpected end of buffer");
307
+ }
308
+ const tag = this.buffer[this._pos++];
309
+ switch (tag) {
310
+ case kUndefined:
311
+ return void 0;
312
+ case kNull:
313
+ return null;
314
+ case kTrue:
315
+ return true;
316
+ case kFalse:
317
+ return false;
318
+ case kInt32: {
319
+ const [n, p] = readVarint(this.buffer, this._pos);
320
+ this._pos = p;
321
+ return zigZagDecode(n);
322
+ }
323
+ case kUint32: {
324
+ const [n, p] = readVarint(this.buffer, this._pos);
325
+ this._pos = p;
326
+ return n >>> 0;
327
+ }
328
+ case kDouble: {
329
+ const [d, p] = readFloat64(this.buffer, this._pos);
330
+ this._pos = p;
331
+ return d;
332
+ }
333
+ case kBigInt: {
334
+ return this._readBigInt();
335
+ }
336
+ case kOneByteString: {
337
+ const [len, p] = readVarint(this.buffer, this._pos);
338
+ this._pos = p;
339
+ let s = "";
340
+ for (let i = 0; i < len; i++) s += String.fromCharCode(this.buffer[this._pos++]);
341
+ return s;
342
+ }
343
+ case kUtf8String: {
344
+ const [len, p] = readVarint(this.buffer, this._pos);
345
+ this._pos = p;
346
+ const bytes = this.buffer.slice(this._pos, this._pos + len);
347
+ this._pos += len;
348
+ return new TextDecoder().decode(bytes);
349
+ }
350
+ case kDate: {
351
+ const [ms, p] = readFloat64(this.buffer, this._pos);
352
+ this._pos = p;
353
+ const d = new Date(ms);
354
+ this._seen.push(d);
355
+ return d;
356
+ }
357
+ case kRegExp: {
358
+ const source = this.readValue();
359
+ const [flagBits, p] = readVarint(this.buffer, this._pos);
360
+ this._pos = p;
361
+ const flags = intToRegExpFlags(flagBits);
362
+ const re = new RegExp(source, flags);
363
+ this._seen.push(re);
364
+ return re;
365
+ }
366
+ case kArrayBuffer: {
367
+ const [byteLen, p] = readVarint(this.buffer, this._pos);
368
+ this._pos = p;
369
+ const ab = new ArrayBuffer(byteLen);
370
+ const view = new Uint8Array(ab);
371
+ for (let i = 0; i < byteLen; i++) view[i] = this.buffer[this._pos++];
372
+ this._seen.push(ab);
373
+ return ab;
374
+ }
375
+ case kObjectReference: {
376
+ const [idx, p] = readVarint(this.buffer, this._pos);
377
+ this._pos = p;
378
+ if (idx >= this._seen.length) throw new Error("Invalid object reference");
379
+ return this._seen[idx];
380
+ }
381
+ case kBeginDenseArray: {
382
+ const [len, p] = readVarint(this.buffer, this._pos);
383
+ this._pos = p;
384
+ const arr = new Array(len);
385
+ this._seen.push(arr);
386
+ for (let i = 0; i < len; i++) arr[i] = this.readValue();
387
+ if (this.buffer[this._pos] !== kEndDenseArray) throw new Error("Expected kEndDenseArray");
388
+ this._pos++;
389
+ const [, p2] = readVarint(this.buffer, this._pos);
390
+ this._pos = p2;
391
+ const [, p3] = readVarint(this.buffer, this._pos);
392
+ this._pos = p3;
393
+ return arr;
394
+ }
395
+ case kBeginJSObject: {
396
+ const obj = {};
397
+ this._seen.push(obj);
398
+ while (this.buffer[this._pos] !== kEndJSObject) {
399
+ const key = this.readValue();
400
+ obj[key] = this.readValue();
401
+ }
402
+ this._pos++;
403
+ const [, p] = readVarint(this.buffer, this._pos);
404
+ this._pos = p;
405
+ return obj;
406
+ }
407
+ case kHostObject: {
408
+ const obj = this._readHostObject();
409
+ if (obj !== null && obj !== void 0) this._seen.push(obj);
410
+ return obj;
411
+ }
412
+ default:
413
+ throw new Error(`Unknown tag 0x${tag.toString(16).padStart(2, "0")} at position ${this._pos - 1}`);
414
+ }
415
+ }
416
+ readUint32() {
417
+ const [n, p] = readVarint(this.buffer, this._pos);
418
+ this._pos = p;
419
+ return n >>> 0;
420
+ }
421
+ readUint64() {
422
+ const [hi, p1] = readVarint(this.buffer, this._pos);
423
+ const [lo, p2] = readVarint(this.buffer, p1);
424
+ this._pos = p2;
425
+ return [hi >>> 0, lo >>> 0];
426
+ }
427
+ readDouble() {
428
+ const [d, p] = readFloat64(this.buffer, this._pos);
429
+ this._pos = p;
430
+ return d;
431
+ }
432
+ // Returns the byte offset within this.buffer where the raw bytes start.
433
+ // Callers use: new FastBuffer(this.buffer.buffer, this.buffer.byteOffset + offset, length)
434
+ _readRawBytes(length) {
435
+ const offset = this._pos;
436
+ this._pos += length;
437
+ if (this._pos > this.buffer.length) throw new Error("Unexpected end of buffer reading raw bytes");
438
+ return offset;
439
+ }
440
+ // readRawBytes is patched on the prototype below (as in Node.js) to return a Buffer slice
441
+ readRawBytes(length) {
442
+ const offset = this._readRawBytes(length);
443
+ return Buffer.from(this.buffer.buffer, this.buffer.byteOffset + offset, length);
444
+ }
445
+ _readHostObject() {
446
+ throw new Error("No host object deserializer installed");
447
+ }
448
+ _readBigInt() {
449
+ const [bitfield, p] = readVarint(this.buffer, this._pos);
450
+ this._pos = p;
451
+ const negative = (bitfield & 1) === 1;
452
+ const u32Count = bitfield >> 1;
453
+ let result = 0n;
454
+ for (let i = 0; i < u32Count; i++) {
455
+ const lo = this.buffer[this._pos] | this.buffer[this._pos + 1] << 8 | this.buffer[this._pos + 2] << 16 | this.buffer[this._pos + 3] << 24;
456
+ this._pos += 4;
457
+ result |= BigInt(lo >>> 0) << BigInt(i * 32);
458
+ }
459
+ return negative ? -result : result;
460
+ }
461
+ }
462
+ function regExpFlagsToInt(re) {
463
+ let flags = 0;
464
+ if (re.global) flags |= 1 << 0;
465
+ if (re.ignoreCase) flags |= 1 << 1;
466
+ if (re.multiline) flags |= 1 << 2;
467
+ if (re.sticky) flags |= 1 << 3;
468
+ if (re.unicode) flags |= 1 << 4;
469
+ if (re.dotAll) flags |= 1 << 5;
470
+ if (re.hasIndices) flags |= 1 << 6;
471
+ if (re.unicodeSets) flags |= 1 << 7;
472
+ return flags;
473
+ }
474
+ function intToRegExpFlags(bits) {
475
+ let flags = "";
476
+ if (bits & 1 << 0) flags += "g";
477
+ if (bits & 1 << 1) flags += "i";
478
+ if (bits & 1 << 2) flags += "m";
479
+ if (bits & 1 << 3) flags += "y";
480
+ if (bits & 1 << 4) flags += "u";
481
+ if (bits & 1 << 5) flags += "s";
482
+ if (bits & 1 << 6) flags += "d";
483
+ if (bits & 1 << 7) flags += "v";
484
+ return flags;
485
+ }
486
+ class DefaultSerializer extends Serializer {
487
+ constructor() {
488
+ super();
489
+ this._setTreatArrayBufferViewsAsHostObjects(true);
490
+ }
491
+ _writeHostObject(abView) {
492
+ let typeIndex;
493
+ if (Buffer.isBuffer(abView)) {
494
+ typeIndex = 10;
495
+ } else {
496
+ typeIndex = typedArrayToIndex(abView);
497
+ if (typeIndex === -1) {
498
+ throw new this._getDataCloneError(
499
+ `Unserializable host object: ${Object.prototype.toString.call(abView)}`
500
+ );
501
+ }
502
+ }
503
+ this.writeUint32(typeIndex);
504
+ this.writeUint32(abView.byteLength);
505
+ this.writeRawBytes(new Uint8Array(abView.buffer, abView.byteOffset, abView.byteLength));
506
+ }
507
+ }
508
+ class DefaultDeserializer extends Deserializer {
509
+ _readHostObject() {
510
+ const typeIndex = this.readUint32();
511
+ const byteLength = this.readUint32();
512
+ const byteOffset = this._readRawBytes(byteLength);
513
+ if (typeIndex === 10) {
514
+ const b = Buffer.allocUnsafe(byteLength);
515
+ this.buffer.copy(b, 0, this.buffer.byteOffset + byteOffset, this.buffer.byteOffset + byteOffset + byteLength);
516
+ return b;
517
+ }
518
+ const Ctor = indexToTypedArray(typeIndex);
519
+ if (!Ctor) throw new Error(`Unknown TypedArray type index ${typeIndex}`);
520
+ const bytesPerElement = Ctor.BYTES_PER_ELEMENT ?? 1;
521
+ const elementCount = byteLength / bytesPerElement;
522
+ const absoluteOffset = this.buffer.byteOffset + byteOffset;
523
+ if (absoluteOffset % bytesPerElement === 0) {
524
+ return new Ctor(this.buffer.buffer, absoluteOffset, elementCount);
525
+ }
526
+ const copy = Buffer.allocUnsafe(byteLength);
527
+ this.buffer.copy(copy, 0, this.buffer.byteOffset + byteOffset, this.buffer.byteOffset + byteOffset + byteLength);
528
+ return new Ctor(copy.buffer, copy.byteOffset, elementCount);
529
+ }
530
+ }
531
+ export {
532
+ DefaultDeserializer,
533
+ DefaultSerializer,
534
+ Deserializer,
535
+ Serializer
536
+ };