seroval 0.7.0 → 0.8.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.
Files changed (41) hide show
  1. package/dist/cjs/development/index.cjs +1109 -1039
  2. package/dist/cjs/development/index.cjs.map +4 -4
  3. package/dist/cjs/production/index.cjs +3 -3
  4. package/dist/esm/development/index.mjs +1109 -1039
  5. package/dist/esm/development/index.mjs.map +4 -4
  6. package/dist/esm/production/index.mjs +3 -3
  7. package/dist/types/assert.d.ts +1 -1
  8. package/dist/types/context.d.ts +2 -2
  9. package/dist/types/index.d.ts +3 -4
  10. package/dist/types/string.d.ts +1 -1
  11. package/dist/types/tree/UnsupportedTypeError.d.ts +3 -0
  12. package/dist/types/tree/async.d.ts +3 -3
  13. package/dist/types/tree/constants.d.ts +11 -0
  14. package/dist/types/tree/deserialize.d.ts +2 -2
  15. package/dist/types/tree/primitives.d.ts +9 -15
  16. package/dist/types/tree/serialize.d.ts +2 -2
  17. package/dist/types/tree/shared.d.ts +10 -8
  18. package/dist/types/tree/sync.d.ts +3 -3
  19. package/dist/types/tree/types.d.ts +64 -71
  20. package/dist/types/tree/web-api.d.ts +2 -2
  21. package/dist/types/types.d.ts +4 -4
  22. package/package.json +10 -8
  23. package/src/assert.ts +2 -2
  24. package/src/compat.ts +0 -1
  25. package/src/context.ts +10 -7
  26. package/src/get-identifier.ts +1 -1
  27. package/src/index.ts +35 -26
  28. package/src/string.ts +2 -2
  29. package/src/tree/UnsupportedTypeError.ts +8 -0
  30. package/src/tree/async.ts +245 -233
  31. package/src/tree/constants.ts +72 -0
  32. package/src/tree/deserialize.ts +96 -93
  33. package/src/tree/primitives.ts +81 -134
  34. package/src/tree/reference.ts +4 -3
  35. package/src/tree/serialize.ts +251 -197
  36. package/src/tree/shared.ts +31 -22
  37. package/src/tree/symbols.ts +13 -13
  38. package/src/tree/sync.ts +226 -198
  39. package/src/tree/types.ts +74 -93
  40. package/src/tree/web-api.ts +10 -7
  41. package/src/types.ts +7 -3
@@ -40,7 +40,7 @@ var DEFAULT_OPTIONS = {
40
40
  disabledFeatures: 0
41
41
  };
42
42
  function createParserContext(options = {}) {
43
- const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
43
+ const result = Object.assign({}, DEFAULT_OPTIONS, options);
44
44
  return {
45
45
  markedRefs: /* @__PURE__ */ new Set(),
46
46
  refs: /* @__PURE__ */ new Map(),
@@ -96,7 +96,7 @@ function createIndexedValue(ctx, current) {
96
96
  // src/assert.ts
97
97
  function assert(cond, error) {
98
98
  if (!cond) {
99
- throw new Error(error);
99
+ throw error;
100
100
  }
101
101
  }
102
102
 
@@ -149,6 +149,82 @@ function deserializeString(str) {
149
149
  return str.replace(/\\"/g, '"').replace(/\\\\/g, "\\").replace(/\\n/g, "\n").replace(/\\r/g, "\r").replace(/\\b/g, "\b").replace(/\\t/g, " ").replace(/\\f/g, "\f").replace(/\\x3C/g, "<").replace(/\\u2028/g, "\u2028").replace(/\\u2029/g, "\u2029");
150
150
  }
151
151
 
152
+ // src/tree/UnsupportedTypeError.ts
153
+ var { toString } = Object.prototype;
154
+ var UnsupportedTypeError = class extends Error {
155
+ constructor(value) {
156
+ super('Unsupported type "' + toString.call(value) + '"');
157
+ }
158
+ };
159
+
160
+ // src/tree/constants.ts
161
+ function createConstantNode(value) {
162
+ return {
163
+ t: 2 /* Constant */,
164
+ i: void 0,
165
+ s: value,
166
+ l: void 0,
167
+ c: void 0,
168
+ m: void 0,
169
+ d: void 0,
170
+ a: void 0,
171
+ f: void 0,
172
+ b: void 0
173
+ };
174
+ }
175
+ var TRUE_NODE = createConstantNode(2 /* True */);
176
+ var FALSE_NODE = createConstantNode(3 /* False */);
177
+ var UNDEFINED_NODE = createConstantNode(1 /* Undefined */);
178
+ var NULL_NODE = createConstantNode(0 /* Null */);
179
+ var NEG_ZERO_NODE = createConstantNode(4 /* NegativeZero */);
180
+ var INFINITY_NODE = createConstantNode(5 /* Infinity */);
181
+ var NEG_INFINITY_NODE = createConstantNode(6 /* NegativeInfinity */);
182
+ var NAN_NODE = createConstantNode(7 /* NaN */);
183
+ function serializeConstant(node) {
184
+ switch (node.s) {
185
+ case 2 /* True */:
186
+ return "!0";
187
+ case 3 /* False */:
188
+ return "!1";
189
+ case 1 /* Undefined */:
190
+ return "void 0";
191
+ case 0 /* Null */:
192
+ return "null";
193
+ case 4 /* NegativeZero */:
194
+ return "-0";
195
+ case 5 /* Infinity */:
196
+ return "1/0";
197
+ case 6 /* NegativeInfinity */:
198
+ return "-1/0";
199
+ case 7 /* NaN */:
200
+ return "NaN";
201
+ default:
202
+ throw new Error("invariant");
203
+ }
204
+ }
205
+ function deserializeConstant(node) {
206
+ switch (node.s) {
207
+ case 2 /* True */:
208
+ return true;
209
+ case 3 /* False */:
210
+ return false;
211
+ case 1 /* Undefined */:
212
+ return void 0;
213
+ case 0 /* Null */:
214
+ return null;
215
+ case 4 /* NegativeZero */:
216
+ return -0;
217
+ case 5 /* Infinity */:
218
+ return Infinity;
219
+ case 6 /* NegativeInfinity */:
220
+ return -Infinity;
221
+ case 7 /* NaN */:
222
+ return NaN;
223
+ default:
224
+ throw new Error("invariant");
225
+ }
226
+ }
227
+
152
228
  // src/tree/reference.ts
153
229
  var REFERENCE = /* @__PURE__ */ new Map();
154
230
  var INV_REFERENCE = /* @__PURE__ */ new Map();
@@ -164,11 +240,11 @@ function hasReference(id) {
164
240
  return INV_REFERENCE.has(id);
165
241
  }
166
242
  function getReferenceID(value) {
167
- assert(hasReferenceID(value), "Missing reference id");
243
+ assert(hasReferenceID(value), new Error("Missing reference id"));
168
244
  return REFERENCE.get(value);
169
245
  }
170
246
  function getReference(id) {
171
- assert(hasReference(id), "Missing reference for id:" + id);
247
+ assert(hasReference(id), new Error("Missing reference for id:" + id));
172
248
  return INV_REFERENCE.get(id);
173
249
  }
174
250
  var GLOBAL_KEY = "__SEROVAL__";
@@ -250,115 +326,32 @@ var SYMBOL_REF = {
250
326
  };
251
327
 
252
328
  // src/tree/primitives.ts
253
- var TRUE_NODE = {
254
- t: 2 /* Boolean */,
255
- i: void 0,
256
- s: true,
257
- l: void 0,
258
- c: void 0,
259
- m: void 0,
260
- d: void 0,
261
- a: void 0,
262
- f: void 0,
263
- b: void 0
264
- };
265
- var FALSE_NODE = {
266
- t: 2 /* Boolean */,
267
- i: void 0,
268
- s: false,
269
- l: void 0,
270
- c: void 0,
271
- m: void 0,
272
- d: void 0,
273
- a: void 0,
274
- f: void 0,
275
- b: void 0
276
- };
277
- var UNDEFINED_NODE = {
278
- t: 4 /* Undefined */,
279
- i: void 0,
280
- s: void 0,
281
- l: void 0,
282
- c: void 0,
283
- m: void 0,
284
- d: void 0,
285
- a: void 0,
286
- f: void 0,
287
- b: void 0
288
- };
289
- var NULL_NODE = {
290
- t: 3 /* Null */,
291
- i: void 0,
292
- s: void 0,
293
- l: void 0,
294
- c: void 0,
295
- m: void 0,
296
- d: void 0,
297
- a: void 0,
298
- f: void 0,
299
- b: void 0
300
- };
301
- var NEG_ZERO_NODE = {
302
- t: 5 /* NegativeZero */,
303
- i: void 0,
304
- s: void 0,
305
- l: void 0,
306
- c: void 0,
307
- m: void 0,
308
- d: void 0,
309
- a: void 0,
310
- f: void 0,
311
- b: void 0
312
- };
313
- var INFINITY_NODE = {
314
- t: 6 /* Infinity */,
315
- i: void 0,
316
- s: void 0,
317
- l: void 0,
318
- c: void 0,
319
- m: void 0,
320
- d: void 0,
321
- a: void 0,
322
- f: void 0,
323
- b: void 0
324
- };
325
- var NEG_INFINITY_NODE = {
326
- t: 7 /* NegativeInfinity */,
327
- i: void 0,
328
- s: void 0,
329
- l: void 0,
330
- c: void 0,
331
- m: void 0,
332
- d: void 0,
333
- a: void 0,
334
- f: void 0,
335
- b: void 0
336
- };
337
- var NAN_NODE = {
338
- t: 8 /* NaN */,
339
- i: void 0,
340
- s: void 0,
341
- l: void 0,
342
- c: void 0,
343
- m: void 0,
344
- d: void 0,
345
- a: void 0,
346
- f: void 0,
347
- b: void 0
348
- };
349
329
  function createNumberNode(value) {
350
- return {
351
- t: 0 /* Number */,
352
- i: void 0,
353
- s: value,
354
- l: void 0,
355
- c: void 0,
356
- m: void 0,
357
- d: void 0,
358
- a: void 0,
359
- f: void 0,
360
- b: void 0
361
- };
330
+ switch (value) {
331
+ case Infinity:
332
+ return INFINITY_NODE;
333
+ case -Infinity:
334
+ return NEG_INFINITY_NODE;
335
+ default:
336
+ if (value !== value) {
337
+ return NAN_NODE;
338
+ }
339
+ if (Object.is(value, -0)) {
340
+ return NEG_ZERO_NODE;
341
+ }
342
+ return {
343
+ t: 0 /* Number */,
344
+ i: void 0,
345
+ s: value,
346
+ l: void 0,
347
+ c: void 0,
348
+ m: void 0,
349
+ d: void 0,
350
+ a: void 0,
351
+ f: void 0,
352
+ b: void 0
353
+ };
354
+ }
362
355
  }
363
356
  function createStringNode(value) {
364
357
  return {
@@ -375,9 +368,9 @@ function createStringNode(value) {
375
368
  };
376
369
  }
377
370
  function createBigIntNode(ctx, current) {
378
- assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
371
+ assert(ctx.features & 8 /* BigInt */, new UnsupportedTypeError(current));
379
372
  return {
380
- t: 9 /* BigInt */,
373
+ t: 3 /* BigInt */,
381
374
  i: void 0,
382
375
  s: "" + current,
383
376
  l: void 0,
@@ -391,7 +384,7 @@ function createBigIntNode(ctx, current) {
391
384
  }
392
385
  function createIndexedValueNode(id) {
393
386
  return {
394
- t: 10 /* IndexedValue */,
387
+ t: 4 /* IndexedValue */,
395
388
  i: id,
396
389
  s: void 0,
397
390
  l: void 0,
@@ -405,7 +398,7 @@ function createIndexedValueNode(id) {
405
398
  }
406
399
  function createDateNode(id, current) {
407
400
  return {
408
- t: 11 /* Date */,
401
+ t: 5 /* Date */,
409
402
  i: id,
410
403
  s: current.toISOString(),
411
404
  l: void 0,
@@ -419,7 +412,7 @@ function createDateNode(id, current) {
419
412
  }
420
413
  function createRegExpNode(id, current) {
421
414
  return {
422
- t: 12 /* RegExp */,
415
+ t: 6 /* RegExp */,
423
416
  i: id,
424
417
  s: void 0,
425
418
  l: void 0,
@@ -439,7 +432,7 @@ function createArrayBufferNode(id, current) {
439
432
  values[i] = bytes[i];
440
433
  }
441
434
  return {
442
- t: 28 /* ArrayBuffer */,
435
+ t: 21 /* ArrayBuffer */,
443
436
  i: id,
444
437
  s: values,
445
438
  l: void 0,
@@ -459,14 +452,13 @@ function serializeArrayBuffer(ctx, current) {
459
452
  return createArrayBufferNode(id, current);
460
453
  }
461
454
  function createTypedArrayNode(ctx, id, current) {
462
- const constructor = current.constructor.name;
463
- assert(ctx.features & 2048 /* TypedArray */, `Unsupported value type "${constructor}"`);
455
+ assert(ctx.features & 2048 /* TypedArray */, new UnsupportedTypeError(current));
464
456
  return {
465
- t: 22 /* TypedArray */,
457
+ t: 15 /* TypedArray */,
466
458
  i: id,
467
459
  s: void 0,
468
460
  l: current.length,
469
- c: constructor,
461
+ c: current.constructor.name,
470
462
  m: void 0,
471
463
  d: void 0,
472
464
  a: void 0,
@@ -476,17 +468,16 @@ function createTypedArrayNode(ctx, id, current) {
476
468
  }
477
469
  var BIGINT_FLAG = 4096 /* BigIntTypedArray */ | 8 /* BigInt */;
478
470
  function createBigIntTypedArrayNode(ctx, id, current) {
479
- const constructor = current.constructor.name;
480
471
  assert(
481
472
  (ctx.features & BIGINT_FLAG) === BIGINT_FLAG,
482
- `Unsupported value type "${constructor}"`
473
+ new UnsupportedTypeError(current)
483
474
  );
484
475
  return {
485
- t: 23 /* BigIntTypedArray */,
476
+ t: 16 /* BigIntTypedArray */,
486
477
  i: id,
487
478
  s: void 0,
488
479
  l: current.length,
489
- c: constructor,
480
+ c: current.constructor.name,
490
481
  m: void 0,
491
482
  d: void 0,
492
483
  a: void 0,
@@ -494,12 +485,12 @@ function createBigIntTypedArrayNode(ctx, id, current) {
494
485
  b: current.byteOffset
495
486
  };
496
487
  }
497
- function createWKSymbolNode(ctx, current) {
498
- assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "symbol"');
499
- assert(current in INV_SYMBOL_REF, "seroval only supports well-known symbols");
488
+ function createWKSymbolNode(ctx, id, current) {
489
+ assert(ctx.features & 1024 /* Symbol */, new UnsupportedTypeError(current));
490
+ assert(current in INV_SYMBOL_REF, new Error("Only well-known symbols are supported."));
500
491
  return {
501
- t: 24 /* WKSymbol */,
502
- i: void 0,
492
+ t: 17 /* WKSymbol */,
493
+ i: id,
503
494
  s: INV_SYMBOL_REF[current],
504
495
  l: void 0,
505
496
  c: void 0,
@@ -512,7 +503,7 @@ function createWKSymbolNode(ctx, current) {
512
503
  }
513
504
  function createReferenceNode(id, ref) {
514
505
  return {
515
- t: 27 /* Reference */,
506
+ t: 20 /* Reference */,
516
507
  i: id,
517
508
  s: serializeString(getReferenceID(ref)),
518
509
  l: void 0,
@@ -526,7 +517,7 @@ function createReferenceNode(id, ref) {
526
517
  }
527
518
  function createDataViewNode(ctx, id, current) {
528
519
  return {
529
- t: 29 /* DataView */,
520
+ t: 22 /* DataView */,
530
521
  i: id,
531
522
  s: void 0,
532
523
  l: current.byteLength,
@@ -538,6 +529,24 @@ function createDataViewNode(ctx, id, current) {
538
529
  b: current.byteOffset
539
530
  };
540
531
  }
532
+ function createSymbolNode(ctx, current) {
533
+ const id = createIndexedValue(ctx, current);
534
+ if (ctx.markedRefs.has(id)) {
535
+ return createIndexedValueNode(id);
536
+ }
537
+ if (hasReferenceID(current)) {
538
+ return createReferenceNode(id, current);
539
+ }
540
+ return createWKSymbolNode(ctx, id, current);
541
+ }
542
+ function createFunctionNode(ctx, current) {
543
+ assert(hasReferenceID(current), new Error("Cannot serialize function without reference ID."));
544
+ const id = createIndexedValue(ctx, current);
545
+ if (ctx.markedRefs.has(id)) {
546
+ return createIndexedValueNode(id);
547
+ }
548
+ return createReferenceNode(id, current);
549
+ }
541
550
 
542
551
  // src/tree/shared.ts
543
552
  function getErrorConstructorName(error) {
@@ -605,17 +614,6 @@ function getErrorOptions(ctx, error) {
605
614
  }
606
615
  return options;
607
616
  }
608
- function getIterableOptions(obj) {
609
- const names = Object.getOwnPropertyNames(obj);
610
- if (names.length) {
611
- const options = {};
612
- for (const name of names) {
613
- options[name] = obj[name];
614
- }
615
- return options;
616
- }
617
- return void 0;
618
- }
619
617
  function isIterable(value) {
620
618
  if (!value || typeof value !== "object") {
621
619
  return false;
@@ -679,9 +677,9 @@ function isValidIdentifier(name) {
679
677
 
680
678
  // src/tree/web-api.ts
681
679
  function createURLNode(ctx, id, current) {
682
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "URL"');
680
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
683
681
  return {
684
- t: 25 /* URL */,
682
+ t: 18 /* URL */,
685
683
  i: id,
686
684
  s: serializeString(current.href),
687
685
  l: void 0,
@@ -694,9 +692,9 @@ function createURLNode(ctx, id, current) {
694
692
  };
695
693
  }
696
694
  function createURLSearchParamsNode(ctx, id, current) {
697
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "URLSearchParams"');
695
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
698
696
  return {
699
- t: 26 /* URLSearchParams */,
697
+ t: 19 /* URLSearchParams */,
700
698
  i: id,
701
699
  s: serializeString(current.toString()),
702
700
  l: void 0,
@@ -709,9 +707,9 @@ function createURLSearchParamsNode(ctx, id, current) {
709
707
  };
710
708
  }
711
709
  async function createBlobNode(ctx, id, current) {
712
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "Blob"');
710
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
713
711
  return {
714
- t: 30 /* Blob */,
712
+ t: 23 /* Blob */,
715
713
  i: id,
716
714
  s: void 0,
717
715
  l: void 0,
@@ -724,9 +722,9 @@ async function createBlobNode(ctx, id, current) {
724
722
  };
725
723
  }
726
724
  async function createFileNode(ctx, id, current) {
727
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "File"');
725
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
728
726
  return {
729
- t: 31 /* File */,
727
+ t: 24 /* File */,
730
728
  i: id,
731
729
  s: void 0,
732
730
  l: void 0,
@@ -739,8 +737,8 @@ async function createFileNode(ctx, id, current) {
739
737
  };
740
738
  }
741
739
 
742
- // src/tree/async.ts
743
- async function generateNodeList(ctx, current) {
740
+ // src/tree/sync.ts
741
+ function generateNodeList(ctx, current) {
744
742
  const size = current.length;
745
743
  const nodes = new Array(size);
746
744
  const deferred = new Array(size);
@@ -751,33 +749,33 @@ async function generateNodeList(ctx, current) {
751
749
  if (isIterable(item)) {
752
750
  deferred[i] = item;
753
751
  } else {
754
- nodes[i] = await parse(ctx, item);
752
+ nodes[i] = parseSync(ctx, item);
755
753
  }
756
754
  }
757
755
  }
758
756
  for (let i = 0; i < size; i++) {
759
757
  if (i in deferred) {
760
- nodes[i] = await parse(ctx, deferred[i]);
758
+ nodes[i] = parseSync(ctx, deferred[i]);
761
759
  }
762
760
  }
763
761
  return nodes;
764
762
  }
765
- async function generateArrayNode(ctx, id, current) {
763
+ function generateArrayNode(ctx, id, current) {
766
764
  return {
767
- t: 15 /* Array */,
765
+ t: 9 /* Array */,
768
766
  i: id,
769
767
  s: void 0,
770
768
  l: current.length,
771
769
  c: void 0,
772
770
  m: void 0,
773
771
  d: void 0,
774
- a: await generateNodeList(ctx, current),
772
+ a: generateNodeList(ctx, current),
775
773
  f: void 0,
776
774
  b: void 0
777
775
  };
778
776
  }
779
- async function generateMapNode(ctx, id, current) {
780
- assert(ctx.features & 32 /* Map */, 'Unsupported type "Map"');
777
+ function generateMapNode(ctx, id, current) {
778
+ assert(ctx.features & 32 /* Map */, new UnsupportedTypeError(current));
781
779
  const len = current.size;
782
780
  const keyNodes = new Array(len);
783
781
  const valueNodes = new Array(len);
@@ -791,17 +789,17 @@ async function generateMapNode(ctx, id, current) {
791
789
  deferredValue[deferredSize] = value;
792
790
  deferredSize++;
793
791
  } else {
794
- keyNodes[nodeSize] = await parse(ctx, key);
795
- valueNodes[nodeSize] = await parse(ctx, value);
792
+ keyNodes[nodeSize] = parseSync(ctx, key);
793
+ valueNodes[nodeSize] = parseSync(ctx, value);
796
794
  nodeSize++;
797
795
  }
798
796
  }
799
797
  for (let i = 0; i < deferredSize; i++) {
800
- keyNodes[nodeSize + i] = await parse(ctx, deferredKey[i]);
801
- valueNodes[nodeSize + i] = await parse(ctx, deferredValue[i]);
798
+ keyNodes[nodeSize + i] = parseSync(ctx, deferredKey[i]);
799
+ valueNodes[nodeSize + i] = parseSync(ctx, deferredValue[i]);
802
800
  }
803
801
  return {
804
- t: 14 /* Map */,
802
+ t: 8 /* Map */,
805
803
  i: id,
806
804
  s: void 0,
807
805
  l: void 0,
@@ -813,8 +811,8 @@ async function generateMapNode(ctx, id, current) {
813
811
  b: void 0
814
812
  };
815
813
  }
816
- async function generateSetNode(ctx, id, current) {
817
- assert(ctx.features & 512 /* Set */, 'Unsupported type "Set"');
814
+ function generateSetNode(ctx, id, current) {
815
+ assert(ctx.features & 512 /* Set */, new UnsupportedTypeError(current));
818
816
  const len = current.size;
819
817
  const nodes = new Array(len);
820
818
  const deferred = new Array(len);
@@ -824,14 +822,14 @@ async function generateSetNode(ctx, id, current) {
824
822
  if (isIterable(item)) {
825
823
  deferred[deferredSize++] = item;
826
824
  } else {
827
- nodes[nodeSize++] = await parse(ctx, item);
825
+ nodes[nodeSize++] = parseSync(ctx, item);
828
826
  }
829
827
  }
830
828
  for (let i = 0; i < deferredSize; i++) {
831
- nodes[nodeSize + i] = await parse(ctx, deferred[i]);
829
+ nodes[nodeSize + i] = parseSync(ctx, deferred[i]);
832
830
  }
833
831
  return {
834
- t: 13 /* Set */,
832
+ t: 7 /* Set */,
835
833
  i: id,
836
834
  s: void 0,
837
835
  l: len,
@@ -843,9 +841,9 @@ async function generateSetNode(ctx, id, current) {
843
841
  b: void 0
844
842
  };
845
843
  }
846
- async function generateProperties(ctx, properties) {
844
+ function generateProperties(ctx, properties) {
847
845
  const keys = Object.keys(properties);
848
- const size = keys.length;
846
+ let size = keys.length;
849
847
  const keyNodes = new Array(size);
850
848
  const valueNodes = new Array(size);
851
849
  const deferredKeys = new Array(size);
@@ -863,13 +861,22 @@ async function generateProperties(ctx, properties) {
863
861
  deferredSize++;
864
862
  } else {
865
863
  keyNodes[nodesSize] = escaped;
866
- valueNodes[nodesSize] = await parse(ctx, item);
864
+ valueNodes[nodesSize] = parseSync(ctx, item);
867
865
  nodesSize++;
868
866
  }
869
867
  }
870
868
  for (let i = 0; i < deferredSize; i++) {
871
869
  keyNodes[nodesSize + i] = deferredKeys[i];
872
- valueNodes[nodesSize + i] = await parse(ctx, deferredValues[i]);
870
+ valueNodes[nodesSize + i] = parseSync(ctx, deferredValues[i]);
871
+ }
872
+ if (ctx.features & 1024 /* Symbol */) {
873
+ if (Symbol.iterator in properties) {
874
+ keyNodes[size] = 0 /* SymbolIterator */;
875
+ const items = Array.from(properties);
876
+ const id = createIndexedValue(ctx, items);
877
+ valueNodes[size] = generateArrayNode(ctx, id, items);
878
+ size++;
879
+ }
873
880
  }
874
881
  return {
875
882
  k: keyNodes,
@@ -877,65 +884,59 @@ async function generateProperties(ctx, properties) {
877
884
  s: size
878
885
  };
879
886
  }
880
- async function generateIterableNode(ctx, id, current) {
881
- assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "Iterable"');
882
- const options = getIterableOptions(current);
883
- const array = Array.from(current);
887
+ function generatePlainProperties(ctx, properties) {
888
+ const keys = Object.keys(properties);
889
+ const size = keys.length;
890
+ const keyNodes = new Array(size);
891
+ const valueNodes = new Array(size);
892
+ const deferredKeys = new Array(size);
893
+ const deferredValues = new Array(size);
894
+ let deferredSize = 0;
895
+ let nodesSize = 0;
896
+ let item;
897
+ let escaped;
898
+ for (const key of keys) {
899
+ item = properties[key];
900
+ escaped = serializeString(key);
901
+ if (isIterable(item)) {
902
+ deferredKeys[deferredSize] = escaped;
903
+ deferredValues[deferredSize] = item;
904
+ deferredSize++;
905
+ } else {
906
+ keyNodes[nodesSize] = escaped;
907
+ valueNodes[nodesSize] = parseSync(ctx, item);
908
+ nodesSize++;
909
+ }
910
+ }
911
+ for (let i = 0; i < deferredSize; i++) {
912
+ keyNodes[nodesSize + i] = deferredKeys[i];
913
+ valueNodes[nodesSize + i] = parseSync(ctx, deferredValues[i]);
914
+ }
884
915
  return {
885
- t: 21 /* Iterable */,
886
- i: id,
887
- s: void 0,
888
- l: array.length,
889
- c: void 0,
890
- m: void 0,
891
- // Parse options first before the items
892
- d: options ? await generateProperties(ctx, options) : void 0,
893
- a: await generateNodeList(ctx, array),
894
- f: void 0,
895
- b: void 0
916
+ k: keyNodes,
917
+ v: valueNodes,
918
+ s: size
896
919
  };
897
920
  }
898
- async function generatePromiseNode(ctx, id, current) {
899
- assert(ctx.features & 256 /* Promise */, 'Unsupported type "Promise"');
900
- return current.then(async (value) => ({
901
- t: 18 /* Promise */,
902
- i: id,
903
- s: void 0,
904
- l: void 0,
905
- c: void 0,
906
- m: void 0,
907
- // Parse options first before the items
908
- d: void 0,
909
- a: void 0,
910
- f: await parse(ctx, value),
911
- b: void 0
912
- }));
913
- }
914
- async function generateObjectNode(ctx, id, current, empty) {
915
- if (Symbol.iterator in current) {
916
- return generateIterableNode(ctx, id, current);
917
- }
918
- if ("then" in current && typeof current.then === "function") {
919
- return generatePromiseNode(ctx, id, current);
920
- }
921
+ function generateObjectNode(ctx, id, current, empty) {
921
922
  return {
922
- t: empty ? 17 /* NullConstructor */ : 16 /* Object */,
923
+ t: empty ? 11 /* NullConstructor */ : 10 /* Object */,
923
924
  i: id,
924
925
  s: void 0,
925
926
  l: void 0,
926
927
  c: void 0,
927
928
  m: void 0,
928
- d: await generateProperties(ctx, current),
929
+ d: generateProperties(ctx, current),
929
930
  a: void 0,
930
931
  f: void 0,
931
932
  b: void 0
932
933
  };
933
934
  }
934
- async function generateAggregateErrorNode(ctx, id, current) {
935
+ function generateAggregateErrorNode(ctx, id, current) {
935
936
  const options = getErrorOptions(ctx, current);
936
- const optionsNode = options ? await generateProperties(ctx, options) : void 0;
937
+ const optionsNode = options ? generateProperties(ctx, options) : void 0;
937
938
  return {
938
- t: 20 /* AggregateError */,
939
+ t: 14 /* AggregateError */,
939
940
  i: id,
940
941
  s: void 0,
941
942
  l: void 0,
@@ -947,11 +948,11 @@ async function generateAggregateErrorNode(ctx, id, current) {
947
948
  b: void 0
948
949
  };
949
950
  }
950
- async function generateErrorNode(ctx, id, current) {
951
+ function generateErrorNode(ctx, id, current) {
951
952
  const options = getErrorOptions(ctx, current);
952
- const optionsNode = options ? await generateProperties(ctx, options) : void 0;
953
+ const optionsNode = options ? generateProperties(ctx, options) : void 0;
953
954
  return {
954
- t: 19 /* Error */,
955
+ t: 13 /* Error */,
955
956
  i: id,
956
957
  s: void 0,
957
958
  l: void 0,
@@ -963,45 +964,158 @@ async function generateErrorNode(ctx, id, current) {
963
964
  b: void 0
964
965
  };
965
966
  }
966
- async function generateHeadersNode(ctx, id, current) {
967
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "Headers"');
967
+ function generateHeadersNode(ctx, id, current) {
968
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
968
969
  const items = {};
969
970
  current.forEach((value, key) => {
970
971
  items[key] = value;
971
972
  });
972
973
  return {
973
- t: 32 /* Headers */,
974
+ t: 25 /* Headers */,
974
975
  i: id,
975
976
  s: void 0,
976
977
  l: void 0,
977
978
  c: void 0,
978
979
  m: void 0,
979
- d: await generateProperties(ctx, items),
980
+ d: generatePlainProperties(ctx, items),
980
981
  a: void 0,
981
982
  f: void 0,
982
983
  b: void 0
983
984
  };
984
985
  }
985
- async function generateFormDataNode(ctx, id, current) {
986
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "FormData"');
986
+ function generateFormDataNode(ctx, id, current) {
987
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
987
988
  const items = {};
988
989
  current.forEach((value, key) => {
989
990
  items[key] = value;
990
991
  });
991
992
  return {
992
- t: 33 /* FormData */,
993
+ t: 26 /* FormData */,
993
994
  i: id,
994
995
  s: void 0,
995
996
  l: void 0,
996
997
  c: void 0,
997
998
  m: void 0,
998
- d: await generateProperties(ctx, items),
999
+ d: generatePlainProperties(ctx, items),
999
1000
  a: void 0,
1000
1001
  f: void 0,
1001
1002
  b: void 0
1002
1003
  };
1003
1004
  }
1004
- async function parse(ctx, current) {
1005
+ function generateBoxedNode(ctx, id, current) {
1006
+ return {
1007
+ t: 27 /* Boxed */,
1008
+ i: id,
1009
+ s: void 0,
1010
+ l: void 0,
1011
+ c: void 0,
1012
+ m: void 0,
1013
+ d: void 0,
1014
+ a: void 0,
1015
+ f: parseSync(ctx, current.valueOf()),
1016
+ b: void 0
1017
+ };
1018
+ }
1019
+ function parseObject(ctx, current) {
1020
+ if (!current) {
1021
+ return NULL_NODE;
1022
+ }
1023
+ const id = createIndexedValue(ctx, current);
1024
+ if (ctx.markedRefs.has(id)) {
1025
+ return createIndexedValueNode(id);
1026
+ }
1027
+ if (hasReferenceID(current)) {
1028
+ return createReferenceNode(id, current);
1029
+ }
1030
+ if (Array.isArray(current)) {
1031
+ return generateArrayNode(ctx, id, current);
1032
+ }
1033
+ switch (current.constructor) {
1034
+ case Number:
1035
+ case Boolean:
1036
+ case String:
1037
+ case BigInt:
1038
+ case Function:
1039
+ case Symbol:
1040
+ return generateBoxedNode(ctx, id, current);
1041
+ case Date:
1042
+ return createDateNode(id, current);
1043
+ case RegExp:
1044
+ return createRegExpNode(id, current);
1045
+ case ArrayBuffer:
1046
+ return createArrayBufferNode(id, current);
1047
+ case Int8Array:
1048
+ case Int16Array:
1049
+ case Int32Array:
1050
+ case Uint8Array:
1051
+ case Uint16Array:
1052
+ case Uint32Array:
1053
+ case Uint8ClampedArray:
1054
+ case Float32Array:
1055
+ case Float64Array:
1056
+ return createTypedArrayNode(ctx, id, current);
1057
+ case BigInt64Array:
1058
+ case BigUint64Array:
1059
+ return createBigIntTypedArrayNode(ctx, id, current);
1060
+ case DataView:
1061
+ return createDataViewNode(ctx, id, current);
1062
+ case Map:
1063
+ return generateMapNode(ctx, id, current);
1064
+ case Set:
1065
+ return generateSetNode(ctx, id, current);
1066
+ case Object:
1067
+ return generateObjectNode(
1068
+ ctx,
1069
+ id,
1070
+ current,
1071
+ false
1072
+ );
1073
+ case void 0:
1074
+ return generateObjectNode(
1075
+ ctx,
1076
+ id,
1077
+ current,
1078
+ true
1079
+ );
1080
+ case AggregateError:
1081
+ if (ctx.features & 1 /* AggregateError */) {
1082
+ return generateAggregateErrorNode(ctx, id, current);
1083
+ }
1084
+ return generateErrorNode(ctx, id, current);
1085
+ case Error:
1086
+ case EvalError:
1087
+ case RangeError:
1088
+ case ReferenceError:
1089
+ case SyntaxError:
1090
+ case TypeError:
1091
+ case URIError:
1092
+ return generateErrorNode(ctx, id, current);
1093
+ case URL:
1094
+ return createURLNode(ctx, id, current);
1095
+ case URLSearchParams:
1096
+ return createURLSearchParamsNode(ctx, id, current);
1097
+ case Headers:
1098
+ return generateHeadersNode(ctx, id, current);
1099
+ case FormData:
1100
+ return generateFormDataNode(ctx, id, current);
1101
+ default:
1102
+ break;
1103
+ }
1104
+ if (current instanceof AggregateError) {
1105
+ if (ctx.features & 1 /* AggregateError */) {
1106
+ return generateAggregateErrorNode(ctx, id, current);
1107
+ }
1108
+ return generateErrorNode(ctx, id, current);
1109
+ }
1110
+ if (current instanceof Error) {
1111
+ return generateErrorNode(ctx, id, current);
1112
+ }
1113
+ if (Symbol.iterator in current) {
1114
+ return generateObjectNode(ctx, id, current, !!current.constructor);
1115
+ }
1116
+ throw new UnsupportedTypeError(current);
1117
+ }
1118
+ function parseSync(ctx, current) {
1005
1119
  switch (typeof current) {
1006
1120
  case "boolean":
1007
1121
  return current ? TRUE_NODE : FALSE_NODE;
@@ -1010,210 +1124,519 @@ async function parse(ctx, current) {
1010
1124
  case "string":
1011
1125
  return createStringNode(current);
1012
1126
  case "number":
1013
- switch (current) {
1014
- case Infinity:
1015
- return INFINITY_NODE;
1016
- case -Infinity:
1017
- return NEG_INFINITY_NODE;
1018
- default:
1019
- if (current !== current) {
1020
- return NAN_NODE;
1021
- }
1022
- if (Object.is(current, -0)) {
1023
- return NEG_ZERO_NODE;
1024
- }
1025
- return createNumberNode(current);
1026
- }
1127
+ return createNumberNode(current);
1027
1128
  case "bigint":
1028
1129
  return createBigIntNode(ctx, current);
1029
- case "object": {
1030
- if (!current) {
1031
- return NULL_NODE;
1032
- }
1033
- const id = createIndexedValue(ctx, current);
1034
- if (ctx.markedRefs.has(id)) {
1035
- return createIndexedValueNode(id);
1036
- }
1037
- if (hasReferenceID(current)) {
1038
- return createReferenceNode(id, current);
1039
- }
1040
- if (Array.isArray(current)) {
1041
- return generateArrayNode(ctx, id, current);
1042
- }
1043
- switch (current.constructor) {
1044
- case Date:
1045
- return createDateNode(id, current);
1046
- case RegExp:
1047
- return createRegExpNode(id, current);
1048
- case Promise:
1049
- return generatePromiseNode(ctx, id, current);
1050
- case ArrayBuffer:
1051
- return createArrayBufferNode(id, current);
1052
- case Int8Array:
1053
- case Int16Array:
1054
- case Int32Array:
1055
- case Uint8Array:
1056
- case Uint16Array:
1057
- case Uint32Array:
1058
- case Uint8ClampedArray:
1059
- case Float32Array:
1060
- case Float64Array:
1061
- return createTypedArrayNode(ctx, id, current);
1062
- case BigInt64Array:
1063
- case BigUint64Array:
1064
- return createBigIntTypedArrayNode(ctx, id, current);
1065
- case DataView:
1066
- return createDataViewNode(ctx, id, current);
1067
- case Map:
1068
- return generateMapNode(
1069
- ctx,
1070
- id,
1071
- current
1072
- );
1073
- case Set:
1074
- return generateSetNode(
1075
- ctx,
1076
- id,
1077
- current
1078
- );
1079
- case Object:
1080
- return generateObjectNode(
1081
- ctx,
1082
- id,
1083
- current,
1084
- false
1085
- );
1086
- case void 0:
1087
- return generateObjectNode(
1088
- ctx,
1089
- id,
1090
- current,
1091
- true
1092
- );
1093
- case AggregateError:
1094
- if (ctx.features & 1 /* AggregateError */) {
1095
- return generateAggregateErrorNode(ctx, id, current);
1096
- }
1097
- return generateErrorNode(ctx, id, current);
1098
- case Error:
1099
- case EvalError:
1100
- case RangeError:
1101
- case ReferenceError:
1102
- case SyntaxError:
1103
- case TypeError:
1104
- case URIError:
1105
- return generateErrorNode(ctx, id, current);
1106
- case URL:
1107
- return createURLNode(ctx, id, current);
1108
- case URLSearchParams:
1109
- return createURLSearchParamsNode(ctx, id, current);
1110
- case Blob:
1111
- return createBlobNode(ctx, id, current);
1112
- case File:
1113
- return createFileNode(ctx, id, current);
1114
- case Headers:
1115
- return generateHeadersNode(ctx, id, current);
1116
- case FormData:
1117
- return generateFormDataNode(ctx, id, current);
1118
- default:
1119
- break;
1120
- }
1121
- if (current instanceof AggregateError) {
1122
- if (ctx.features & 1 /* AggregateError */) {
1123
- return generateAggregateErrorNode(ctx, id, current);
1124
- }
1125
- return generateErrorNode(ctx, id, current);
1126
- }
1127
- if (current instanceof Error) {
1128
- return generateErrorNode(ctx, id, current);
1129
- }
1130
- if (current instanceof Promise) {
1131
- return generatePromiseNode(ctx, id, current);
1132
- }
1133
- if (Symbol.iterator in current) {
1134
- return generateIterableNode(ctx, id, current);
1135
- }
1136
- if ("then" in current && typeof current.then === "function") {
1137
- return generatePromiseNode(ctx, id, current);
1138
- }
1139
- throw new Error("Unsupported type");
1140
- }
1130
+ case "object":
1131
+ return parseObject(ctx, current);
1141
1132
  case "symbol":
1142
- if (hasReferenceID(current)) {
1143
- const id = createIndexedValue(ctx, current);
1144
- if (ctx.markedRefs.has(id)) {
1145
- return createIndexedValueNode(id);
1146
- }
1147
- return createReferenceNode(id, current);
1148
- }
1149
- return createWKSymbolNode(ctx, current);
1150
- case "function": {
1151
- assert(hasReferenceID(current), "Cannot serialize function without reference ID.");
1152
- const id = createIndexedValue(ctx, current);
1153
- if (ctx.markedRefs.has(id)) {
1154
- return createIndexedValueNode(id);
1155
- }
1156
- return createReferenceNode(id, current);
1157
- }
1133
+ return createSymbolNode(ctx, current);
1134
+ case "function":
1135
+ return createFunctionNode(ctx, current);
1158
1136
  default:
1159
- throw new Error("Unsupported type");
1137
+ throw new UnsupportedTypeError(current);
1160
1138
  }
1161
1139
  }
1162
- async function parseAsync(ctx, current) {
1163
- const result = await parse(ctx, current);
1164
- const isObject = result.t === 16 /* Object */ || result.t === 21 /* Iterable */;
1165
- return [result, getRootID(ctx, current), isObject];
1166
- }
1167
1140
 
1168
- // src/tree/deserialize.ts
1169
- function assignIndexedValue(ctx, index, value) {
1170
- if (ctx.markedRefs.has(index)) {
1171
- ctx.valueMap.set(index, value);
1172
- }
1173
- return value;
1174
- }
1175
- function deserializeArray(ctx, node) {
1176
- const result = assignIndexedValue(
1177
- ctx,
1178
- node.i,
1179
- new Array(node.l)
1180
- );
1141
+ // src/tree/async.ts
1142
+ async function generateNodeList2(ctx, current) {
1143
+ const size = current.length;
1144
+ const nodes = new Array(size);
1145
+ const deferred = new Array(size);
1181
1146
  let item;
1182
- for (let i = 0, len = node.l; i < len; i++) {
1183
- item = node.a[i];
1184
- if (item) {
1185
- result[i] = deserializeTree(ctx, item);
1147
+ for (let i = 0; i < size; i++) {
1148
+ if (i in current) {
1149
+ item = current[i];
1150
+ if (isIterable(item)) {
1151
+ deferred[i] = item;
1152
+ } else {
1153
+ nodes[i] = await parseAsync(ctx, item);
1154
+ }
1186
1155
  }
1187
1156
  }
1188
- return result;
1189
- }
1190
- function deserializeProperties(ctx, node, result) {
1191
- if (node.s === 0) {
1192
- return {};
1193
- }
1194
- for (let i = 0; i < node.s; i++) {
1195
- result[deserializeString(node.k[i])] = deserializeTree(ctx, node.v[i]);
1157
+ for (let i = 0; i < size; i++) {
1158
+ if (i in deferred) {
1159
+ nodes[i] = await parseAsync(ctx, deferred[i]);
1160
+ }
1196
1161
  }
1197
- return result;
1162
+ return nodes;
1198
1163
  }
1199
- function deserializeNullConstructor(ctx, node) {
1200
- const result = assignIndexedValue(
1201
- ctx,
1202
- node.i,
1203
- /* @__PURE__ */ Object.create(null)
1204
- );
1205
- deserializeProperties(ctx, node.d, result);
1206
- return result;
1164
+ async function generateArrayNode2(ctx, id, current) {
1165
+ return {
1166
+ t: 9 /* Array */,
1167
+ i: id,
1168
+ s: void 0,
1169
+ l: current.length,
1170
+ c: void 0,
1171
+ m: void 0,
1172
+ d: void 0,
1173
+ a: await generateNodeList2(ctx, current),
1174
+ f: void 0,
1175
+ b: void 0
1176
+ };
1207
1177
  }
1208
- function deserializeObject(ctx, node) {
1209
- const result = assignIndexedValue(ctx, node.i, {});
1210
- deserializeProperties(ctx, node.d, result);
1178
+ async function generateMapNode2(ctx, id, current) {
1179
+ assert(ctx.features & 32 /* Map */, new UnsupportedTypeError(current));
1180
+ const len = current.size;
1181
+ const keyNodes = new Array(len);
1182
+ const valueNodes = new Array(len);
1183
+ const deferredKey = new Array(len);
1184
+ const deferredValue = new Array(len);
1185
+ let deferredSize = 0;
1186
+ let nodeSize = 0;
1187
+ for (const [key, value] of current.entries()) {
1188
+ if (isIterable(key) || isIterable(value)) {
1189
+ deferredKey[deferredSize] = key;
1190
+ deferredValue[deferredSize] = value;
1191
+ deferredSize++;
1192
+ } else {
1193
+ keyNodes[nodeSize] = await parseAsync(ctx, key);
1194
+ valueNodes[nodeSize] = await parseAsync(ctx, value);
1195
+ nodeSize++;
1196
+ }
1197
+ }
1198
+ for (let i = 0; i < deferredSize; i++) {
1199
+ keyNodes[nodeSize + i] = await parseAsync(ctx, deferredKey[i]);
1200
+ valueNodes[nodeSize + i] = await parseAsync(ctx, deferredValue[i]);
1201
+ }
1202
+ return {
1203
+ t: 8 /* Map */,
1204
+ i: id,
1205
+ s: void 0,
1206
+ l: void 0,
1207
+ c: void 0,
1208
+ m: void 0,
1209
+ d: { k: keyNodes, v: valueNodes, s: len },
1210
+ a: void 0,
1211
+ f: void 0,
1212
+ b: void 0
1213
+ };
1214
+ }
1215
+ async function generateSetNode2(ctx, id, current) {
1216
+ assert(ctx.features & 512 /* Set */, new UnsupportedTypeError(current));
1217
+ const len = current.size;
1218
+ const nodes = new Array(len);
1219
+ const deferred = new Array(len);
1220
+ let deferredSize = 0;
1221
+ let nodeSize = 0;
1222
+ for (const item of current.keys()) {
1223
+ if (isIterable(item)) {
1224
+ deferred[deferredSize++] = item;
1225
+ } else {
1226
+ nodes[nodeSize++] = await parseAsync(ctx, item);
1227
+ }
1228
+ }
1229
+ for (let i = 0; i < deferredSize; i++) {
1230
+ nodes[nodeSize + i] = await parseAsync(ctx, deferred[i]);
1231
+ }
1232
+ return {
1233
+ t: 7 /* Set */,
1234
+ i: id,
1235
+ s: void 0,
1236
+ l: len,
1237
+ c: void 0,
1238
+ m: void 0,
1239
+ d: void 0,
1240
+ a: nodes,
1241
+ f: void 0,
1242
+ b: void 0
1243
+ };
1244
+ }
1245
+ async function generateProperties2(ctx, properties) {
1246
+ const keys = Object.keys(properties);
1247
+ let size = keys.length;
1248
+ const keyNodes = new Array(size);
1249
+ const valueNodes = new Array(size);
1250
+ const deferredKeys = new Array(size);
1251
+ const deferredValues = new Array(size);
1252
+ let deferredSize = 0;
1253
+ let nodesSize = 0;
1254
+ let item;
1255
+ let escaped;
1256
+ for (const key of keys) {
1257
+ item = properties[key];
1258
+ escaped = serializeString(key);
1259
+ if (isIterable(item)) {
1260
+ deferredKeys[deferredSize] = escaped;
1261
+ deferredValues[deferredSize] = item;
1262
+ deferredSize++;
1263
+ } else {
1264
+ keyNodes[nodesSize] = escaped;
1265
+ valueNodes[nodesSize] = await parseAsync(ctx, item);
1266
+ nodesSize++;
1267
+ }
1268
+ }
1269
+ for (let i = 0; i < deferredSize; i++) {
1270
+ keyNodes[nodesSize + i] = deferredKeys[i];
1271
+ valueNodes[nodesSize + i] = await parseAsync(ctx, deferredValues[i]);
1272
+ }
1273
+ if (ctx.features & 1024 /* Symbol */) {
1274
+ if (Symbol.iterator in properties) {
1275
+ keyNodes[size] = 0 /* SymbolIterator */;
1276
+ const items = Array.from(properties);
1277
+ valueNodes[size] = await generateArrayNode2(
1278
+ ctx,
1279
+ createIndexedValue(ctx, items),
1280
+ items
1281
+ );
1282
+ size++;
1283
+ }
1284
+ }
1285
+ return {
1286
+ k: keyNodes,
1287
+ v: valueNodes,
1288
+ s: size
1289
+ };
1290
+ }
1291
+ async function generatePlainProperties2(ctx, properties) {
1292
+ const keys = Object.keys(properties);
1293
+ const size = keys.length;
1294
+ const keyNodes = new Array(size);
1295
+ const valueNodes = new Array(size);
1296
+ const deferredKeys = new Array(size);
1297
+ const deferredValues = new Array(size);
1298
+ let deferredSize = 0;
1299
+ let nodesSize = 0;
1300
+ let item;
1301
+ let escaped;
1302
+ for (const key of keys) {
1303
+ item = properties[key];
1304
+ escaped = serializeString(key);
1305
+ if (isIterable(item)) {
1306
+ deferredKeys[deferredSize] = escaped;
1307
+ deferredValues[deferredSize] = item;
1308
+ deferredSize++;
1309
+ } else {
1310
+ keyNodes[nodesSize] = escaped;
1311
+ valueNodes[nodesSize] = await parseAsync(ctx, item);
1312
+ nodesSize++;
1313
+ }
1314
+ }
1315
+ for (let i = 0; i < deferredSize; i++) {
1316
+ keyNodes[nodesSize + i] = deferredKeys[i];
1317
+ valueNodes[nodesSize + i] = await parseAsync(ctx, deferredValues[i]);
1318
+ }
1319
+ return {
1320
+ k: keyNodes,
1321
+ v: valueNodes,
1322
+ s: size
1323
+ };
1324
+ }
1325
+ async function generatePromiseNode(ctx, id, current) {
1326
+ assert(ctx.features & 256 /* Promise */, new UnsupportedTypeError(current));
1327
+ return {
1328
+ t: 12 /* Promise */,
1329
+ i: id,
1330
+ s: void 0,
1331
+ l: void 0,
1332
+ c: void 0,
1333
+ m: void 0,
1334
+ // Parse options first before the items
1335
+ d: void 0,
1336
+ a: void 0,
1337
+ f: await parseAsync(ctx, await current),
1338
+ b: void 0
1339
+ };
1340
+ }
1341
+ async function generateObjectNode2(ctx, id, current, empty) {
1342
+ return {
1343
+ t: empty ? 11 /* NullConstructor */ : 10 /* Object */,
1344
+ i: id,
1345
+ s: void 0,
1346
+ l: void 0,
1347
+ c: void 0,
1348
+ m: void 0,
1349
+ d: await generateProperties2(ctx, current),
1350
+ a: void 0,
1351
+ f: void 0,
1352
+ b: void 0
1353
+ };
1354
+ }
1355
+ async function generateAggregateErrorNode2(ctx, id, current) {
1356
+ const options = getErrorOptions(ctx, current);
1357
+ const optionsNode = options ? await generateProperties2(ctx, options) : void 0;
1358
+ return {
1359
+ t: 14 /* AggregateError */,
1360
+ i: id,
1361
+ s: void 0,
1362
+ l: void 0,
1363
+ c: void 0,
1364
+ m: serializeString(current.message),
1365
+ d: optionsNode,
1366
+ a: void 0,
1367
+ f: void 0,
1368
+ b: void 0
1369
+ };
1370
+ }
1371
+ async function generateErrorNode2(ctx, id, current) {
1372
+ const options = getErrorOptions(ctx, current);
1373
+ const optionsNode = options ? await generateProperties2(ctx, options) : void 0;
1374
+ return {
1375
+ t: 13 /* Error */,
1376
+ i: id,
1377
+ s: void 0,
1378
+ l: void 0,
1379
+ c: getErrorConstructorName(current),
1380
+ m: serializeString(current.message),
1381
+ d: optionsNode,
1382
+ a: void 0,
1383
+ f: void 0,
1384
+ b: void 0
1385
+ };
1386
+ }
1387
+ async function generateHeadersNode2(ctx, id, current) {
1388
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
1389
+ const items = {};
1390
+ current.forEach((value, key) => {
1391
+ items[key] = value;
1392
+ });
1393
+ return {
1394
+ t: 25 /* Headers */,
1395
+ i: id,
1396
+ s: void 0,
1397
+ l: void 0,
1398
+ c: void 0,
1399
+ m: void 0,
1400
+ d: await generatePlainProperties2(ctx, items),
1401
+ a: void 0,
1402
+ f: void 0,
1403
+ b: void 0
1404
+ };
1405
+ }
1406
+ async function generateFormDataNode2(ctx, id, current) {
1407
+ assert(ctx.features & 8192 /* WebAPI */, new UnsupportedTypeError(current));
1408
+ const items = {};
1409
+ current.forEach((value, key) => {
1410
+ items[key] = value;
1411
+ });
1412
+ return {
1413
+ t: 26 /* FormData */,
1414
+ i: id,
1415
+ s: void 0,
1416
+ l: void 0,
1417
+ c: void 0,
1418
+ m: void 0,
1419
+ d: await generatePlainProperties2(ctx, items),
1420
+ a: void 0,
1421
+ f: void 0,
1422
+ b: void 0
1423
+ };
1424
+ }
1425
+ async function generateBoxedNode2(ctx, id, current) {
1426
+ return {
1427
+ t: 27 /* Boxed */,
1428
+ i: id,
1429
+ s: void 0,
1430
+ l: void 0,
1431
+ c: void 0,
1432
+ m: void 0,
1433
+ d: void 0,
1434
+ a: void 0,
1435
+ f: await parseAsync(ctx, current.valueOf()),
1436
+ b: void 0
1437
+ };
1438
+ }
1439
+ async function parseObject2(ctx, current) {
1440
+ if (!current) {
1441
+ return NULL_NODE;
1442
+ }
1443
+ const id = createIndexedValue(ctx, current);
1444
+ if (ctx.markedRefs.has(id)) {
1445
+ return createIndexedValueNode(id);
1446
+ }
1447
+ if (hasReferenceID(current)) {
1448
+ return createReferenceNode(id, current);
1449
+ }
1450
+ if (Array.isArray(current)) {
1451
+ return generateArrayNode2(ctx, id, current);
1452
+ }
1453
+ switch (current.constructor) {
1454
+ case Number:
1455
+ case Boolean:
1456
+ case String:
1457
+ case BigInt:
1458
+ case Function:
1459
+ case Symbol:
1460
+ return generateBoxedNode2(ctx, id, current);
1461
+ case Date:
1462
+ return createDateNode(id, current);
1463
+ case RegExp:
1464
+ return createRegExpNode(id, current);
1465
+ case Promise:
1466
+ return generatePromiseNode(ctx, id, current);
1467
+ case ArrayBuffer:
1468
+ return createArrayBufferNode(id, current);
1469
+ case Int8Array:
1470
+ case Int16Array:
1471
+ case Int32Array:
1472
+ case Uint8Array:
1473
+ case Uint16Array:
1474
+ case Uint32Array:
1475
+ case Uint8ClampedArray:
1476
+ case Float32Array:
1477
+ case Float64Array:
1478
+ return createTypedArrayNode(ctx, id, current);
1479
+ case BigInt64Array:
1480
+ case BigUint64Array:
1481
+ return createBigIntTypedArrayNode(ctx, id, current);
1482
+ case DataView:
1483
+ return createDataViewNode(ctx, id, current);
1484
+ case Map:
1485
+ return generateMapNode2(
1486
+ ctx,
1487
+ id,
1488
+ current
1489
+ );
1490
+ case Set:
1491
+ return generateSetNode2(
1492
+ ctx,
1493
+ id,
1494
+ current
1495
+ );
1496
+ case Object:
1497
+ return generateObjectNode2(
1498
+ ctx,
1499
+ id,
1500
+ current,
1501
+ false
1502
+ );
1503
+ case void 0:
1504
+ return generateObjectNode2(
1505
+ ctx,
1506
+ id,
1507
+ current,
1508
+ true
1509
+ );
1510
+ case AggregateError:
1511
+ if (ctx.features & 1 /* AggregateError */) {
1512
+ return generateAggregateErrorNode2(ctx, id, current);
1513
+ }
1514
+ return generateErrorNode2(ctx, id, current);
1515
+ case Error:
1516
+ case EvalError:
1517
+ case RangeError:
1518
+ case ReferenceError:
1519
+ case SyntaxError:
1520
+ case TypeError:
1521
+ case URIError:
1522
+ return generateErrorNode2(ctx, id, current);
1523
+ case URL:
1524
+ return createURLNode(ctx, id, current);
1525
+ case URLSearchParams:
1526
+ return createURLSearchParamsNode(ctx, id, current);
1527
+ case Blob:
1528
+ return createBlobNode(ctx, id, current);
1529
+ case File:
1530
+ return createFileNode(ctx, id, current);
1531
+ case Headers:
1532
+ return generateHeadersNode2(ctx, id, current);
1533
+ case FormData:
1534
+ return generateFormDataNode2(ctx, id, current);
1535
+ default:
1536
+ break;
1537
+ }
1538
+ if (current instanceof AggregateError) {
1539
+ if (ctx.features & 1 /* AggregateError */) {
1540
+ return generateAggregateErrorNode2(ctx, id, current);
1541
+ }
1542
+ return generateErrorNode2(ctx, id, current);
1543
+ }
1544
+ if (current instanceof Error) {
1545
+ return generateErrorNode2(ctx, id, current);
1546
+ }
1547
+ if (current instanceof Promise) {
1548
+ return generatePromiseNode(ctx, id, current);
1549
+ }
1550
+ if (Symbol.iterator in current) {
1551
+ return generateObjectNode2(ctx, id, current, !!current.constructor);
1552
+ }
1553
+ throw new UnsupportedTypeError(current);
1554
+ }
1555
+ async function parseAsync(ctx, current) {
1556
+ switch (typeof current) {
1557
+ case "boolean":
1558
+ return current ? TRUE_NODE : FALSE_NODE;
1559
+ case "undefined":
1560
+ return UNDEFINED_NODE;
1561
+ case "string":
1562
+ return createStringNode(current);
1563
+ case "number":
1564
+ return createNumberNode(current);
1565
+ case "bigint":
1566
+ return createBigIntNode(ctx, current);
1567
+ case "object":
1568
+ return parseObject2(ctx, current);
1569
+ case "symbol":
1570
+ return createSymbolNode(ctx, current);
1571
+ case "function":
1572
+ return createFunctionNode(ctx, current);
1573
+ default:
1574
+ throw new UnsupportedTypeError(current);
1575
+ }
1576
+ }
1577
+
1578
+ // src/tree/deserialize.ts
1579
+ function assignIndexedValue(ctx, index, value) {
1580
+ if (ctx.markedRefs.has(index)) {
1581
+ ctx.valueMap.set(index, value);
1582
+ }
1583
+ return value;
1584
+ }
1585
+ function deserializeArray(ctx, node) {
1586
+ const len = node.l;
1587
+ const result = assignIndexedValue(
1588
+ ctx,
1589
+ node.i,
1590
+ new Array(len)
1591
+ );
1592
+ let item;
1593
+ for (let i = 0; i < len; i++) {
1594
+ item = node.a[i];
1595
+ if (item) {
1596
+ result[i] = deserializeTree(ctx, item);
1597
+ }
1598
+ }
1599
+ return result;
1600
+ }
1601
+ function deserializeProperties(ctx, node, result) {
1602
+ const len = node.s;
1603
+ if (len) {
1604
+ let key;
1605
+ let value;
1606
+ const keys = node.k;
1607
+ const vals = node.v;
1608
+ for (let i = 0; i < len; i++) {
1609
+ key = keys[i];
1610
+ value = deserializeTree(ctx, vals[i]);
1611
+ switch (key) {
1612
+ case 0 /* SymbolIterator */:
1613
+ {
1614
+ const current = value;
1615
+ result[Symbol.iterator] = () => current.values();
1616
+ }
1617
+ break;
1618
+ default:
1619
+ result[deserializeString(key)] = value;
1620
+ break;
1621
+ }
1622
+ }
1623
+ }
1624
+ return result;
1625
+ }
1626
+ function deserializeObject(ctx, node) {
1627
+ const result = assignIndexedValue(
1628
+ ctx,
1629
+ node.i,
1630
+ node.t === 10 /* Object */ ? {} : /* @__PURE__ */ Object.create(null)
1631
+ );
1632
+ deserializeProperties(ctx, node.d, result);
1211
1633
  return result;
1212
1634
  }
1213
1635
  function deserializeSet(ctx, node) {
1214
1636
  const result = assignIndexedValue(ctx, node.i, /* @__PURE__ */ new Set());
1637
+ const items = node.a;
1215
1638
  for (let i = 0, len = node.l; i < len; i++) {
1216
- result.add(deserializeTree(ctx, node.a[i]));
1639
+ result.add(deserializeTree(ctx, items[i]));
1217
1640
  }
1218
1641
  return result;
1219
1642
  }
@@ -1223,10 +1646,12 @@ function deserializeMap(ctx, node) {
1223
1646
  node.i,
1224
1647
  /* @__PURE__ */ new Map()
1225
1648
  );
1649
+ const keys = node.d.k;
1650
+ const vals = node.d.v;
1226
1651
  for (let i = 0, len = node.d.s; i < len; i++) {
1227
1652
  result.set(
1228
- deserializeTree(ctx, node.d.k[i]),
1229
- deserializeTree(ctx, node.d.v[i])
1653
+ deserializeTree(ctx, keys[i]),
1654
+ deserializeTree(ctx, vals[i])
1230
1655
  );
1231
1656
  }
1232
1657
  return result;
@@ -1262,7 +1687,7 @@ function createDeferred() {
1262
1687
  })
1263
1688
  };
1264
1689
  }
1265
- function deserializePromise(ctx, node) {
1690
+ async function deserializePromise(ctx, node) {
1266
1691
  const deferred = createDeferred();
1267
1692
  const result = assignIndexedValue(ctx, node.i, deferred.promise);
1268
1693
  deferred.resolve(deserializeTree(ctx, node.f));
@@ -1283,20 +1708,6 @@ function deserializeTypedArray(ctx, node) {
1283
1708
  ));
1284
1709
  return result;
1285
1710
  }
1286
- function deserializeIterable(ctx, node) {
1287
- const values = [];
1288
- let item;
1289
- for (let i = 0, len = node.l; i < len; i++) {
1290
- item = node.a[i];
1291
- if (item) {
1292
- values[i] = deserializeTree(ctx, item);
1293
- }
1294
- }
1295
- const result = assignIndexedValue(ctx, node.i, {
1296
- [Symbol.iterator]: () => values.values()
1297
- });
1298
- return deserializeDictionary(ctx, node, result);
1299
- }
1300
1711
  function deserializeDate(ctx, node) {
1301
1712
  return assignIndexedValue(ctx, node.i, new Date(node.s));
1302
1713
  }
@@ -1340,94 +1751,93 @@ function deserializeFile(ctx, node) {
1340
1751
  }
1341
1752
  function deserializeHeaders(ctx, node) {
1342
1753
  const result = assignIndexedValue(ctx, node.i, new Headers());
1754
+ const keys = node.d.k;
1755
+ const vals = node.d.v;
1343
1756
  for (let i = 0, len = node.d.s; i < len; i++) {
1344
1757
  result.set(
1345
- deserializeString(node.d.k[i]),
1346
- deserializeTree(ctx, node.d.v[i])
1758
+ deserializeString(keys[i]),
1759
+ deserializeTree(ctx, vals[i])
1347
1760
  );
1348
1761
  }
1349
1762
  return result;
1350
1763
  }
1351
1764
  function deserializeFormData(ctx, node) {
1352
1765
  const result = assignIndexedValue(ctx, node.i, new FormData());
1766
+ const keys = node.d.k;
1767
+ const vals = node.d.v;
1353
1768
  for (let i = 0, len = node.d.s; i < len; i++) {
1354
1769
  result.set(
1355
- deserializeString(node.d.k[i]),
1356
- deserializeTree(ctx, node.d.v[i])
1770
+ deserializeString(keys[i]),
1771
+ deserializeTree(ctx, vals[i])
1357
1772
  );
1358
1773
  }
1359
1774
  return result;
1360
1775
  }
1776
+ function deserializeBoxed(ctx, node) {
1777
+ return assignIndexedValue(
1778
+ ctx,
1779
+ node.i,
1780
+ Object(deserializeTree(ctx, node.f))
1781
+ );
1782
+ }
1361
1783
  function deserializeTree(ctx, node) {
1362
1784
  switch (node.t) {
1785
+ case 2 /* Constant */:
1786
+ return deserializeConstant(node);
1363
1787
  case 0 /* Number */:
1364
- case 2 /* Boolean */:
1365
1788
  return node.s;
1366
1789
  case 1 /* String */:
1367
1790
  return deserializeString(node.s);
1368
- case 4 /* Undefined */:
1369
- return void 0;
1370
- case 3 /* Null */:
1371
- return null;
1372
- case 5 /* NegativeZero */:
1373
- return -0;
1374
- case 6 /* Infinity */:
1375
- return Infinity;
1376
- case 7 /* NegativeInfinity */:
1377
- return -Infinity;
1378
- case 8 /* NaN */:
1379
- return NaN;
1380
- case 9 /* BigInt */:
1791
+ case 3 /* BigInt */:
1381
1792
  return BigInt(node.s);
1382
- case 10 /* IndexedValue */:
1793
+ case 4 /* IndexedValue */:
1383
1794
  return ctx.valueMap.get(node.i);
1384
- case 15 /* Array */:
1795
+ case 9 /* Array */:
1385
1796
  return deserializeArray(ctx, node);
1386
- case 16 /* Object */:
1797
+ case 10 /* Object */:
1798
+ case 11 /* NullConstructor */:
1387
1799
  return deserializeObject(ctx, node);
1388
- case 17 /* NullConstructor */:
1389
- return deserializeNullConstructor(ctx, node);
1390
- case 11 /* Date */:
1800
+ case 5 /* Date */:
1391
1801
  return deserializeDate(ctx, node);
1392
- case 12 /* RegExp */:
1802
+ case 6 /* RegExp */:
1393
1803
  return deserializeRegExp(ctx, node);
1394
- case 13 /* Set */:
1804
+ case 7 /* Set */:
1395
1805
  return deserializeSet(ctx, node);
1396
- case 14 /* Map */:
1806
+ case 8 /* Map */:
1397
1807
  return deserializeMap(ctx, node);
1398
- case 28 /* ArrayBuffer */:
1808
+ case 21 /* ArrayBuffer */:
1399
1809
  return deserializeArrayBuffer(ctx, node);
1400
- case 23 /* BigIntTypedArray */:
1401
- case 22 /* TypedArray */:
1810
+ case 16 /* BigIntTypedArray */:
1811
+ case 15 /* TypedArray */:
1402
1812
  return deserializeTypedArray(ctx, node);
1403
- case 29 /* DataView */:
1813
+ case 22 /* DataView */:
1404
1814
  return deserializeDataView(ctx, node);
1405
- case 20 /* AggregateError */:
1815
+ case 14 /* AggregateError */:
1406
1816
  return deserializeAggregateError(ctx, node);
1407
- case 19 /* Error */:
1817
+ case 13 /* Error */:
1408
1818
  return deserializeError(ctx, node);
1409
- case 21 /* Iterable */:
1410
- return deserializeIterable(ctx, node);
1411
- case 18 /* Promise */:
1819
+ case 12 /* Promise */:
1412
1820
  return deserializePromise(ctx, node);
1413
- case 24 /* WKSymbol */:
1821
+ case 17 /* WKSymbol */:
1414
1822
  return SYMBOL_REF[node.s];
1415
- case 25 /* URL */:
1823
+ case 18 /* URL */:
1416
1824
  return deserializeURL(ctx, node);
1417
- case 26 /* URLSearchParams */:
1825
+ case 19 /* URLSearchParams */:
1418
1826
  return deserializeURLSearchParams(ctx, node);
1419
- case 27 /* Reference */:
1827
+ case 20 /* Reference */:
1420
1828
  return deserializeReference(ctx, node);
1421
- case 30 /* Blob */:
1829
+ case 23 /* Blob */:
1422
1830
  return deserializeBlob(ctx, node);
1423
- case 31 /* File */:
1831
+ case 24 /* File */:
1424
1832
  return deserializeFile(ctx, node);
1425
- case 32 /* Headers */:
1833
+ case 25 /* Headers */:
1426
1834
  return deserializeHeaders(ctx, node);
1427
- case 33 /* FormData */:
1835
+ case 26 /* FormData */:
1428
1836
  return deserializeFormData(ctx, node);
1837
+ case 27 /* Boxed */:
1838
+ return deserializeBoxed(ctx, node);
1429
1839
  default:
1430
- throw new Error("Unsupported type");
1840
+ throw new Error("invariant");
1431
1841
  }
1432
1842
  }
1433
1843
 
@@ -1570,22 +1980,24 @@ function assignIndexedValue2(ctx, index, value) {
1570
1980
  return value;
1571
1981
  }
1572
1982
  function isIndexedValueInStack(ctx, node) {
1573
- return node.t === 10 /* IndexedValue */ && ctx.stack.includes(node.i);
1983
+ return node.t === 4 /* IndexedValue */ && ctx.stack.includes(node.i);
1574
1984
  }
1575
- function serializeNodeList(ctx, node) {
1576
- const size = node.l;
1985
+ function serializeArray(ctx, node) {
1986
+ const id = node.i;
1987
+ ctx.stack.push(id);
1577
1988
  let values = "";
1578
1989
  let item;
1579
1990
  let isHoley = false;
1580
- for (let i = 0; i < size; i++) {
1991
+ const list = node.a;
1992
+ for (let i = 0, len = node.l; i < len; i++) {
1581
1993
  if (i !== 0) {
1582
1994
  values += ",";
1583
1995
  }
1584
- item = node.a[i];
1996
+ item = list[i];
1585
1997
  if (item) {
1586
1998
  if (isIndexedValueInStack(ctx, item)) {
1587
- markRef(ctx, node.i);
1588
- createArrayAssign(ctx, node.i, i, getRefParam(ctx, item.i));
1999
+ markRef(ctx, id);
2000
+ createArrayAssign(ctx, id, i, getRefParam(ctx, item.i));
1589
2001
  isHoley = true;
1590
2002
  } else {
1591
2003
  values += serializeTree(ctx, item);
@@ -1595,16 +2007,29 @@ function serializeNodeList(ctx, node) {
1595
2007
  isHoley = true;
1596
2008
  }
1597
2009
  }
1598
- return "[" + values + (isHoley ? ",]" : "]");
1599
- }
1600
- function serializeArray(ctx, node) {
1601
- ctx.stack.push(node.i);
1602
- const result = serializeNodeList(ctx, node);
1603
2010
  ctx.stack.pop();
1604
- return assignIndexedValue2(ctx, node.i, result);
2011
+ return assignIndexedValue2(ctx, id, "[" + values + (isHoley ? ",]" : "]"));
2012
+ }
2013
+ function getIterableAccess(ctx) {
2014
+ return ctx.features & 2 /* ArrayPrototypeValues */ ? ".values()" : "[Symbol.iterator]()";
2015
+ }
2016
+ function serializeIterable(ctx, node) {
2017
+ const parent = ctx.stack;
2018
+ ctx.stack = [];
2019
+ let serialized = serializeTree(ctx, node) + getIterableAccess(ctx);
2020
+ ctx.stack = parent;
2021
+ if (ctx.features & 4 /* ArrowFunction */) {
2022
+ serialized = "[Symbol.iterator]:()=>" + serialized;
2023
+ } else if (ctx.features & 64 /* MethodShorthand */) {
2024
+ serialized = "[Symbol.iterator](){return " + serialized + "}";
2025
+ } else {
2026
+ serialized = "[Symbol.iterator]:function(){return " + serialized + "}";
2027
+ }
2028
+ return serialized;
1605
2029
  }
1606
2030
  function serializeProperties(ctx, sourceID, node) {
1607
- if (node.s === 0) {
2031
+ const len = node.s;
2032
+ if (len === 0) {
1608
2033
  return "{}";
1609
2034
  }
1610
2035
  let result = "";
@@ -1617,23 +2042,30 @@ function serializeProperties(ctx, sourceID, node) {
1617
2042
  let hasPrev = false;
1618
2043
  const keys = node.k;
1619
2044
  const values = node.v;
1620
- for (let i = 0, len = node.s; i < len; i++) {
2045
+ for (let i = 0; i < len; i++) {
1621
2046
  key = keys[i];
1622
2047
  val = values[i];
1623
- check = Number(key);
1624
- isIdentifier = check >= 0 || isValidIdentifier(key);
1625
- if (isIndexedValueInStack(ctx, val)) {
1626
- refParam = getRefParam(ctx, val.i);
1627
- if (isIdentifier && Number.isNaN(check)) {
1628
- markRef(ctx, sourceID);
1629
- createObjectAssign(ctx, sourceID, key, refParam);
1630
- } else {
1631
- markRef(ctx, sourceID);
1632
- createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + key + '"', refParam);
1633
- }
1634
- } else {
1635
- result += (hasPrev ? "," : "") + (isIdentifier ? key : '"' + key + '"') + ":" + serializeTree(ctx, val);
1636
- hasPrev = true;
2048
+ switch (key) {
2049
+ case 0 /* SymbolIterator */:
2050
+ result += (hasPrev ? "," : "") + serializeIterable(ctx, val);
2051
+ hasPrev = true;
2052
+ break;
2053
+ default:
2054
+ check = Number(key);
2055
+ isIdentifier = check >= 0 || isValidIdentifier(key);
2056
+ if (isIndexedValueInStack(ctx, val)) {
2057
+ refParam = getRefParam(ctx, val.i);
2058
+ markRef(ctx, sourceID);
2059
+ if (isIdentifier && Number.isNaN(check)) {
2060
+ createObjectAssign(ctx, sourceID, key, refParam);
2061
+ } else {
2062
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + key + '"', refParam);
2063
+ }
2064
+ } else {
2065
+ result += (hasPrev ? "," : "") + (isIdentifier ? key : '"' + key + '"') + ":" + serializeTree(ctx, val);
2066
+ hasPrev = true;
2067
+ }
2068
+ break;
1637
2069
  }
1638
2070
  }
1639
2071
  ctx.stack.pop();
@@ -1649,28 +2081,41 @@ function serializeWithObjectAssign(ctx, value, id, serialized) {
1649
2081
  function serializeAssignments(ctx, sourceID, node) {
1650
2082
  ctx.stack.push(sourceID);
1651
2083
  const mainAssignments = [];
1652
- let parentStack;
1653
- let refParam;
1654
2084
  let key;
1655
- let check;
2085
+ let value;
1656
2086
  let parentAssignment;
1657
- let isIdentifier;
1658
2087
  const keys = node.k;
1659
2088
  const values = node.v;
1660
2089
  for (let i = 0, len = node.s; i < len; i++) {
1661
- parentStack = ctx.stack;
1662
- ctx.stack = [];
1663
- refParam = serializeTree(ctx, values[i]);
1664
- ctx.stack = parentStack;
1665
2090
  key = keys[i];
1666
- check = Number(key);
2091
+ value = values[i];
1667
2092
  parentAssignment = ctx.assignments;
1668
2093
  ctx.assignments = mainAssignments;
1669
- isIdentifier = check >= 0 || isValidIdentifier(key);
1670
- if (isIdentifier && Number.isNaN(check)) {
1671
- createObjectAssign(ctx, sourceID, key, refParam);
1672
- } else {
1673
- createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + key + '"', refParam);
2094
+ switch (key) {
2095
+ case 0 /* SymbolIterator */:
2096
+ {
2097
+ const parent = ctx.stack;
2098
+ ctx.stack = [];
2099
+ const serialized = serializeTree(ctx, value) + getIterableAccess(ctx);
2100
+ ctx.stack = parent;
2101
+ createArrayAssign(
2102
+ ctx,
2103
+ sourceID,
2104
+ "Symbol.iterator",
2105
+ ctx.features & 4 /* ArrowFunction */ ? "()=>" + serialized : "function(){return " + serialized + "}"
2106
+ );
2107
+ }
2108
+ break;
2109
+ default: {
2110
+ const serialized = serializeTree(ctx, value);
2111
+ const check = Number(key);
2112
+ const isIdentifier = check >= 0 || isValidIdentifier(key);
2113
+ if (isIdentifier && Number.isNaN(check)) {
2114
+ createObjectAssign(ctx, sourceID, key, serialized);
2115
+ } else {
2116
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : '"' + key + '"', serialized);
2117
+ }
2118
+ }
1674
2119
  }
1675
2120
  ctx.assignments = parentAssignment;
1676
2121
  }
@@ -1691,8 +2136,9 @@ function serializeDictionary(ctx, i, d, init) {
1691
2136
  }
1692
2137
  return assignIndexedValue2(ctx, i, init);
1693
2138
  }
2139
+ var NULL_CONSTRUCTOR = "Object.create(null)";
1694
2140
  function serializeNullConstructor(ctx, node) {
1695
- return serializeDictionary(ctx, node.i, node.d, "Object.create(null)");
2141
+ return serializeDictionary(ctx, node.i, node.d, NULL_CONSTRUCTOR);
1696
2142
  }
1697
2143
  function serializeObject(ctx, node) {
1698
2144
  return assignIndexedValue2(ctx, node.i, serializeProperties(ctx, node.i, node.d));
@@ -1700,16 +2146,18 @@ function serializeObject(ctx, node) {
1700
2146
  function serializeSet(ctx, node) {
1701
2147
  let serialized = "new Set";
1702
2148
  const size = node.l;
2149
+ const id = node.i;
1703
2150
  if (size) {
1704
2151
  let result = "";
1705
- ctx.stack.push(node.i);
1706
2152
  let item;
1707
2153
  let hasPrev = false;
2154
+ const items = node.a;
2155
+ ctx.stack.push(id);
1708
2156
  for (let i = 0; i < size; i++) {
1709
- item = node.a[i];
2157
+ item = items[i];
1710
2158
  if (isIndexedValueInStack(ctx, item)) {
1711
- markRef(ctx, node.i);
1712
- createAddAssignment(ctx, node.i, getRefParam(ctx, item.i));
2159
+ markRef(ctx, id);
2160
+ createAddAssignment(ctx, id, getRefParam(ctx, item.i));
1713
2161
  } else {
1714
2162
  result += (hasPrev ? "," : "") + serializeTree(ctx, item);
1715
2163
  hasPrev = true;
@@ -1720,40 +2168,44 @@ function serializeSet(ctx, node) {
1720
2168
  serialized += "([" + result + "])";
1721
2169
  }
1722
2170
  }
1723
- return assignIndexedValue2(ctx, node.i, serialized);
2171
+ return assignIndexedValue2(ctx, id, serialized);
1724
2172
  }
1725
2173
  function serializeMap(ctx, node) {
1726
2174
  let serialized = "new Map";
1727
- if (node.d.s) {
2175
+ const size = node.d.s;
2176
+ const id = node.i;
2177
+ if (size) {
1728
2178
  let result = "";
1729
- ctx.stack.push(node.i);
1730
2179
  let key;
1731
2180
  let val;
1732
2181
  let keyRef;
1733
2182
  let valueRef;
1734
2183
  let parent;
1735
2184
  let hasPrev = false;
1736
- for (let i = 0; i < node.d.s; i++) {
1737
- key = node.d.k[i];
1738
- val = node.d.v[i];
2185
+ const keys = node.d.k;
2186
+ const vals = node.d.v;
2187
+ ctx.stack.push(id);
2188
+ for (let i = 0; i < size; i++) {
2189
+ key = keys[i];
2190
+ val = vals[i];
1739
2191
  if (isIndexedValueInStack(ctx, key)) {
1740
- keyRef = getRefParam(ctx, key.i);
1741
- markRef(ctx, node.i);
2192
+ keyRef = getRefParam(ctx, id);
2193
+ markRef(ctx, id);
1742
2194
  if (isIndexedValueInStack(ctx, val)) {
1743
2195
  valueRef = getRefParam(ctx, val.i);
1744
- createSetAssignment(ctx, node.i, keyRef, valueRef);
2196
+ createSetAssignment(ctx, id, keyRef, valueRef);
1745
2197
  } else {
1746
2198
  parent = ctx.stack;
1747
2199
  ctx.stack = [];
1748
- createSetAssignment(ctx, node.i, keyRef, serializeTree(ctx, val));
2200
+ createSetAssignment(ctx, id, keyRef, serializeTree(ctx, val));
1749
2201
  ctx.stack = parent;
1750
2202
  }
1751
2203
  } else if (isIndexedValueInStack(ctx, val)) {
1752
2204
  valueRef = getRefParam(ctx, val.i);
1753
- markRef(ctx, node.i);
2205
+ markRef(ctx, id);
1754
2206
  parent = ctx.stack;
1755
2207
  ctx.stack = [];
1756
- createSetAssignment(ctx, node.i, serializeTree(ctx, key), valueRef);
2208
+ createSetAssignment(ctx, id, serializeTree(ctx, key), valueRef);
1757
2209
  ctx.stack = parent;
1758
2210
  } else {
1759
2211
  result += (hasPrev ? ",[" : "[") + serializeTree(ctx, key) + "," + serializeTree(ctx, val) + "]";
@@ -1765,69 +2217,56 @@ function serializeMap(ctx, node) {
1765
2217
  serialized += "([" + result + "])";
1766
2218
  }
1767
2219
  }
1768
- return assignIndexedValue2(ctx, node.i, serialized);
2220
+ return assignIndexedValue2(ctx, id, serialized);
1769
2221
  }
1770
2222
  function serializeAggregateError(ctx, node) {
1771
- ctx.stack.push(node.i);
2223
+ const id = node.i;
2224
+ ctx.stack.push(id);
1772
2225
  const serialized = 'new AggregateError([],"' + node.m + '")';
1773
2226
  ctx.stack.pop();
1774
- return serializeDictionary(ctx, node.i, node.d, serialized);
2227
+ return serializeDictionary(ctx, id, node.d, serialized);
1775
2228
  }
1776
2229
  function serializeError(ctx, node) {
1777
- const serialized = "new " + node.c + '("' + node.m + '")';
1778
- return serializeDictionary(ctx, node.i, node.d, serialized);
2230
+ return serializeDictionary(ctx, node.i, node.d, "new " + node.c + '("' + node.m + '")');
1779
2231
  }
1780
2232
  function serializePromise(ctx, node) {
1781
2233
  let serialized;
1782
- if (isIndexedValueInStack(ctx, node.f)) {
1783
- const ref = getRefParam(ctx, node.f.i);
2234
+ const fulfilled = node.f;
2235
+ const id = node.i;
2236
+ if (isIndexedValueInStack(ctx, fulfilled)) {
2237
+ const ref = getRefParam(ctx, fulfilled.i);
1784
2238
  if (ctx.features & 4 /* ArrowFunction */) {
1785
2239
  serialized = "Promise.resolve().then(()=>" + ref + ")";
1786
2240
  } else {
1787
2241
  serialized = "Promise.resolve().then(function(){return " + ref + "})";
1788
2242
  }
1789
2243
  } else {
1790
- ctx.stack.push(node.i);
1791
- const result = serializeTree(ctx, node.f);
2244
+ ctx.stack.push(id);
2245
+ const result = serializeTree(ctx, fulfilled);
1792
2246
  ctx.stack.pop();
1793
2247
  serialized = "Promise.resolve(" + result + ")";
1794
2248
  }
1795
- return assignIndexedValue2(ctx, node.i, serialized);
2249
+ return assignIndexedValue2(ctx, id, serialized);
1796
2250
  }
1797
2251
  function serializeArrayBuffer2(ctx, node) {
1798
2252
  let result = "new Uint8Array(";
1799
- if (node.s.length) {
2253
+ const buffer = node.s;
2254
+ const len = buffer.length;
2255
+ if (len) {
1800
2256
  result += "[";
1801
- for (let i = 0, len = node.s.length; i < len; i++) {
1802
- result += (i > 0 ? "," : "") + node.s[i];
2257
+ for (let i = 0; i < len; i++) {
2258
+ result += (i > 0 ? "," : "") + buffer[i];
1803
2259
  }
1804
2260
  result += "]";
1805
2261
  }
1806
2262
  return assignIndexedValue2(ctx, node.i, result + ").buffer");
1807
2263
  }
1808
2264
  function serializeTypedArray(ctx, node) {
1809
- const args = serializeTree(ctx, node.f) + "," + node.b + "," + node.l;
1810
- return assignIndexedValue2(ctx, node.i, "new " + node.c + "(" + args + ")");
1811
- }
1812
- function serializeIterable(ctx, node) {
1813
- const parent = ctx.stack;
1814
- ctx.stack = [];
1815
- const values = serializeNodeList(ctx, node);
1816
- ctx.stack = parent;
1817
- let serialized = values;
1818
- if (ctx.features & 2 /* ArrayPrototypeValues */) {
1819
- serialized += ".values()";
1820
- } else {
1821
- serialized += "[Symbol.iterator]()";
1822
- }
1823
- if (ctx.features & 4 /* ArrowFunction */) {
1824
- serialized = "{[Symbol.iterator]:()=>" + serialized + "}";
1825
- } else if (ctx.features & 64 /* MethodShorthand */) {
1826
- serialized = "{[Symbol.iterator](){return " + serialized + "}}";
1827
- } else {
1828
- serialized = "{[Symbol.iterator]:function(){return " + serialized + "}}";
1829
- }
1830
- return serializeDictionary(ctx, node.i, node.d, serialized);
2265
+ return assignIndexedValue2(
2266
+ ctx,
2267
+ node.i,
2268
+ "new " + node.c + "(" + serializeTree(ctx, node.f) + "," + node.b + "," + node.l + ")"
2269
+ );
1831
2270
  }
1832
2271
  function serializeDate(ctx, node) {
1833
2272
  return assignIndexedValue2(ctx, node.i, 'new Date("' + node.s + '")');
@@ -1839,518 +2278,143 @@ function serializeURL(ctx, node) {
1839
2278
  return assignIndexedValue2(ctx, node.i, 'new URL("' + node.s + '")');
1840
2279
  }
1841
2280
  function serializeURLSearchParams(ctx, node) {
1842
- return assignIndexedValue2(ctx, node.i, node.s ? 'new URLSearchParams("' + node.s + '")' : "new URLSearchParams");
2281
+ return assignIndexedValue2(
2282
+ ctx,
2283
+ node.i,
2284
+ node.s ? 'new URLSearchParams("' + node.s + '")' : "new URLSearchParams"
2285
+ );
1843
2286
  }
1844
2287
  function serializeReference(ctx, node) {
1845
2288
  return assignIndexedValue2(ctx, node.i, GLOBAL_KEY + '.get("' + node.s + '")');
1846
2289
  }
1847
2290
  function serializeDataView(ctx, node) {
1848
- const args = serializeTree(ctx, node.f) + "," + node.b + "," + node.l;
1849
- return assignIndexedValue2(ctx, node.i, "new DataView(" + args + ")");
2291
+ return assignIndexedValue2(
2292
+ ctx,
2293
+ node.i,
2294
+ "new DataView(" + serializeTree(ctx, node.f) + "," + node.b + "," + node.l + ")"
2295
+ );
1850
2296
  }
1851
2297
  function serializeBlob(ctx, node) {
1852
- const args = "[" + serializeTree(ctx, node.f) + '],{type:"' + node.c + '"}';
1853
- return assignIndexedValue2(ctx, node.i, "new Blob(" + args + ")");
2298
+ return assignIndexedValue2(
2299
+ ctx,
2300
+ node.i,
2301
+ "new Blob([" + serializeTree(ctx, node.f) + '],{type:"' + node.c + '"})'
2302
+ );
1854
2303
  }
1855
2304
  function serializeFile(ctx, node) {
1856
- const options = '{type:"' + node.c + '",lastModified:' + node.b + "}";
1857
- const args = "[" + serializeTree(ctx, node.f) + '],"' + node.m + '",' + options;
1858
- return assignIndexedValue2(ctx, node.i, "new File(" + args + ")");
2305
+ return assignIndexedValue2(
2306
+ ctx,
2307
+ node.i,
2308
+ "new File([" + serializeTree(ctx, node.f) + '],"' + node.m + '",{type:"' + node.c + '",lastModified:' + node.b + "})"
2309
+ );
1859
2310
  }
1860
2311
  function serializeHeaders(ctx, node) {
1861
- return assignIndexedValue2(ctx, node.i, "new Headers(" + serializeProperties(ctx, node.i, node.d) + ")");
2312
+ return assignIndexedValue2(
2313
+ ctx,
2314
+ node.i,
2315
+ "new Headers(" + serializeProperties(ctx, node.i, node.d) + ")"
2316
+ );
1862
2317
  }
1863
2318
  function serializeFormDataEntries(ctx, node) {
1864
- ctx.stack.push(node.i);
1865
- const mainAssignments = [];
1866
- let parentStack;
1867
2319
  let value;
1868
2320
  let key;
2321
+ const keys = node.d.k;
2322
+ const vals = node.d.v;
2323
+ const id = node.i;
2324
+ const mainAssignments = [];
1869
2325
  let parentAssignment;
1870
- for (let i = 0; i < node.d.s; i++) {
1871
- parentStack = ctx.stack;
1872
- ctx.stack = [];
1873
- value = serializeTree(ctx, node.d.v[i]);
1874
- key = node.d.k[i];
1875
- ctx.stack = parentStack;
2326
+ ctx.stack.push(id);
2327
+ for (let i = 0, len = node.d.s; i < len; i++) {
2328
+ key = keys[i];
2329
+ value = serializeTree(ctx, vals[i]);
1876
2330
  parentAssignment = ctx.assignments;
1877
2331
  ctx.assignments = mainAssignments;
1878
- createAppendAssignment(ctx, node.i, '"' + key + '"', value);
2332
+ createAppendAssignment(ctx, id, '"' + key + '"', value);
1879
2333
  ctx.assignments = parentAssignment;
1880
2334
  }
1881
2335
  ctx.stack.pop();
1882
2336
  return resolveAssignments(mainAssignments);
1883
2337
  }
1884
2338
  function serializeFormData(ctx, node) {
1885
- if (node.d.s) {
1886
- markRef(ctx, node.i);
2339
+ const size = node.d.s;
2340
+ const id = node.i;
2341
+ if (size) {
2342
+ markRef(ctx, id);
1887
2343
  }
1888
- const result = assignIndexedValue2(ctx, node.i, "new FormData()");
1889
- if (node.d.s) {
1890
- return "(" + result + "," + serializeFormDataEntries(ctx, node) + getRefParam(ctx, node.i) + ")";
2344
+ const result = assignIndexedValue2(ctx, id, "new FormData()");
2345
+ if (size) {
2346
+ const entries = serializeFormDataEntries(ctx, node);
2347
+ return "(" + result + "," + (entries == null ? "" : entries) + getRefParam(ctx, id) + ")";
1891
2348
  }
1892
2349
  return result;
1893
2350
  }
2351
+ function serializeBoxed(ctx, node) {
2352
+ return assignIndexedValue2(ctx, node.i, "Object(" + serializeTree(ctx, node.f) + ")");
2353
+ }
2354
+ function serializeWKSymbol(ctx, node) {
2355
+ return assignIndexedValue2(ctx, node.i, SYMBOL_STRING[node.s]);
2356
+ }
1894
2357
  function serializeTree(ctx, node) {
1895
2358
  switch (node.t) {
1896
2359
  case 0 /* Number */:
1897
2360
  return "" + node.s;
1898
2361
  case 1 /* String */:
1899
2362
  return '"' + node.s + '"';
1900
- case 2 /* Boolean */:
1901
- return node.s ? "!0" : "!1";
1902
- case 4 /* Undefined */:
1903
- return "void 0";
1904
- case 3 /* Null */:
1905
- return "null";
1906
- case 5 /* NegativeZero */:
1907
- return "-0";
1908
- case 6 /* Infinity */:
1909
- return "1/0";
1910
- case 7 /* NegativeInfinity */:
1911
- return "-1/0";
1912
- case 8 /* NaN */:
1913
- return "NaN";
1914
- case 9 /* BigInt */:
2363
+ case 2 /* Constant */:
2364
+ return serializeConstant(node);
2365
+ case 3 /* BigInt */:
1915
2366
  return node.s + "n";
1916
- case 10 /* IndexedValue */:
2367
+ case 4 /* IndexedValue */:
1917
2368
  return getRefParam(ctx, node.i);
1918
- case 15 /* Array */:
2369
+ case 9 /* Array */:
1919
2370
  return serializeArray(ctx, node);
1920
- case 16 /* Object */:
2371
+ case 10 /* Object */:
1921
2372
  return serializeObject(ctx, node);
1922
- case 17 /* NullConstructor */:
2373
+ case 11 /* NullConstructor */:
1923
2374
  return serializeNullConstructor(ctx, node);
1924
- case 11 /* Date */:
2375
+ case 5 /* Date */:
1925
2376
  return serializeDate(ctx, node);
1926
- case 12 /* RegExp */:
2377
+ case 6 /* RegExp */:
1927
2378
  return serializeRegExp(ctx, node);
1928
- case 13 /* Set */:
2379
+ case 7 /* Set */:
1929
2380
  return serializeSet(ctx, node);
1930
- case 14 /* Map */:
2381
+ case 8 /* Map */:
1931
2382
  return serializeMap(ctx, node);
1932
- case 28 /* ArrayBuffer */:
2383
+ case 21 /* ArrayBuffer */:
1933
2384
  return serializeArrayBuffer2(ctx, node);
1934
- case 23 /* BigIntTypedArray */:
1935
- case 22 /* TypedArray */:
2385
+ case 16 /* BigIntTypedArray */:
2386
+ case 15 /* TypedArray */:
1936
2387
  return serializeTypedArray(ctx, node);
1937
- case 29 /* DataView */:
2388
+ case 22 /* DataView */:
1938
2389
  return serializeDataView(ctx, node);
1939
- case 20 /* AggregateError */:
2390
+ case 14 /* AggregateError */:
1940
2391
  return serializeAggregateError(ctx, node);
1941
- case 19 /* Error */:
2392
+ case 13 /* Error */:
1942
2393
  return serializeError(ctx, node);
1943
- case 21 /* Iterable */:
1944
- return serializeIterable(ctx, node);
1945
- case 18 /* Promise */:
2394
+ case 12 /* Promise */:
1946
2395
  return serializePromise(ctx, node);
1947
- case 24 /* WKSymbol */:
1948
- return SYMBOL_STRING[node.s];
1949
- case 25 /* URL */:
2396
+ case 17 /* WKSymbol */:
2397
+ return serializeWKSymbol(ctx, node);
2398
+ case 18 /* URL */:
1950
2399
  return serializeURL(ctx, node);
1951
- case 26 /* URLSearchParams */:
2400
+ case 19 /* URLSearchParams */:
1952
2401
  return serializeURLSearchParams(ctx, node);
1953
- case 27 /* Reference */:
2402
+ case 20 /* Reference */:
1954
2403
  return serializeReference(ctx, node);
1955
- case 30 /* Blob */:
2404
+ case 23 /* Blob */:
1956
2405
  return serializeBlob(ctx, node);
1957
- case 31 /* File */:
2406
+ case 24 /* File */:
1958
2407
  return serializeFile(ctx, node);
1959
- case 32 /* Headers */:
2408
+ case 25 /* Headers */:
1960
2409
  return serializeHeaders(ctx, node);
1961
- case 33 /* FormData */:
2410
+ case 26 /* FormData */:
1962
2411
  return serializeFormData(ctx, node);
2412
+ case 27 /* Boxed */:
2413
+ return serializeBoxed(ctx, node);
1963
2414
  default:
1964
- throw new Error("Unsupported type");
1965
- }
1966
- }
1967
-
1968
- // src/tree/sync.ts
1969
- function generateNodeList2(ctx, current) {
1970
- const size = current.length;
1971
- const nodes = new Array(size);
1972
- const deferred = new Array(size);
1973
- let item;
1974
- for (let i = 0; i < size; i++) {
1975
- if (i in current) {
1976
- item = current[i];
1977
- if (isIterable(item)) {
1978
- deferred[i] = item;
1979
- } else {
1980
- nodes[i] = parse2(ctx, item);
1981
- }
1982
- }
1983
- }
1984
- for (let i = 0; i < size; i++) {
1985
- if (i in deferred) {
1986
- nodes[i] = parse2(ctx, deferred[i]);
1987
- }
1988
- }
1989
- return nodes;
1990
- }
1991
- function generateArrayNode2(ctx, id, current) {
1992
- return {
1993
- t: 15 /* Array */,
1994
- i: id,
1995
- s: void 0,
1996
- l: current.length,
1997
- c: void 0,
1998
- m: void 0,
1999
- d: void 0,
2000
- a: generateNodeList2(ctx, current),
2001
- f: void 0,
2002
- b: void 0
2003
- };
2004
- }
2005
- function generateMapNode2(ctx, id, current) {
2006
- assert(ctx.features & 32 /* Map */, 'Unsupported type "Map"');
2007
- const len = current.size;
2008
- const keyNodes = new Array(len);
2009
- const valueNodes = new Array(len);
2010
- const deferredKey = new Array(len);
2011
- const deferredValue = new Array(len);
2012
- let deferredSize = 0;
2013
- let nodeSize = 0;
2014
- for (const [key, value] of current.entries()) {
2015
- if (isIterable(key) || isIterable(value)) {
2016
- deferredKey[deferredSize] = key;
2017
- deferredValue[deferredSize] = value;
2018
- deferredSize++;
2019
- } else {
2020
- keyNodes[nodeSize] = parse2(ctx, key);
2021
- valueNodes[nodeSize] = parse2(ctx, value);
2022
- nodeSize++;
2023
- }
2024
- }
2025
- for (let i = 0; i < deferredSize; i++) {
2026
- keyNodes[nodeSize + i] = parse2(ctx, deferredKey[i]);
2027
- valueNodes[nodeSize + i] = parse2(ctx, deferredValue[i]);
2028
- }
2029
- return {
2030
- t: 14 /* Map */,
2031
- i: id,
2032
- s: void 0,
2033
- l: void 0,
2034
- c: void 0,
2035
- m: void 0,
2036
- d: { k: keyNodes, v: valueNodes, s: len },
2037
- a: void 0,
2038
- f: void 0,
2039
- b: void 0
2040
- };
2041
- }
2042
- function generateSetNode2(ctx, id, current) {
2043
- assert(ctx.features & 512 /* Set */, 'Unsupported type "Set"');
2044
- const len = current.size;
2045
- const nodes = new Array(len);
2046
- const deferred = new Array(len);
2047
- let deferredSize = 0;
2048
- let nodeSize = 0;
2049
- for (const item of current.keys()) {
2050
- if (isIterable(item)) {
2051
- deferred[deferredSize++] = item;
2052
- } else {
2053
- nodes[nodeSize++] = parse2(ctx, item);
2054
- }
2055
- }
2056
- for (let i = 0; i < deferredSize; i++) {
2057
- nodes[nodeSize + i] = parse2(ctx, deferred[i]);
2058
- }
2059
- return {
2060
- t: 13 /* Set */,
2061
- i: id,
2062
- s: void 0,
2063
- l: len,
2064
- c: void 0,
2065
- m: void 0,
2066
- d: void 0,
2067
- a: nodes,
2068
- f: void 0,
2069
- b: void 0
2070
- };
2071
- }
2072
- function generateProperties2(ctx, properties) {
2073
- const keys = Object.keys(properties);
2074
- const size = keys.length;
2075
- const keyNodes = new Array(size);
2076
- const valueNodes = new Array(size);
2077
- const deferredKeys = new Array(size);
2078
- const deferredValues = new Array(size);
2079
- let deferredSize = 0;
2080
- let nodesSize = 0;
2081
- let item;
2082
- let escaped;
2083
- for (const key of keys) {
2084
- item = properties[key];
2085
- escaped = serializeString(key);
2086
- if (isIterable(item)) {
2087
- deferredKeys[deferredSize] = escaped;
2088
- deferredValues[deferredSize] = item;
2089
- deferredSize++;
2090
- } else {
2091
- keyNodes[nodesSize] = escaped;
2092
- valueNodes[nodesSize] = parse2(ctx, item);
2093
- nodesSize++;
2094
- }
2095
- }
2096
- for (let i = 0; i < deferredSize; i++) {
2097
- keyNodes[nodesSize + i] = deferredKeys[i];
2098
- valueNodes[nodesSize + i] = parse2(ctx, deferredValues[i]);
2099
- }
2100
- return {
2101
- k: keyNodes,
2102
- v: valueNodes,
2103
- s: size
2104
- };
2105
- }
2106
- function generateIterableNode2(ctx, id, current) {
2107
- assert(ctx.features & 1024 /* Symbol */, 'Unsupported type "Iterable"');
2108
- const options = getIterableOptions(current);
2109
- const array = Array.from(current);
2110
- return {
2111
- t: 21 /* Iterable */,
2112
- i: id,
2113
- s: void 0,
2114
- l: array.length,
2115
- c: void 0,
2116
- m: void 0,
2117
- // Parse options first before the items
2118
- d: options ? generateProperties2(ctx, options) : void 0,
2119
- a: generateNodeList2(ctx, array),
2120
- f: void 0,
2121
- b: void 0
2122
- };
2123
- }
2124
- function generateObjectNode2(ctx, id, current, empty) {
2125
- if (Symbol.iterator in current) {
2126
- return generateIterableNode2(ctx, id, current);
2127
- }
2128
- return {
2129
- t: empty ? 17 /* NullConstructor */ : 16 /* Object */,
2130
- i: id,
2131
- s: void 0,
2132
- l: void 0,
2133
- c: void 0,
2134
- m: void 0,
2135
- d: generateProperties2(ctx, current),
2136
- a: void 0,
2137
- f: void 0,
2138
- b: void 0
2139
- };
2140
- }
2141
- function generateAggregateErrorNode2(ctx, id, current) {
2142
- const options = getErrorOptions(ctx, current);
2143
- const optionsNode = options ? generateProperties2(ctx, options) : void 0;
2144
- return {
2145
- t: 20 /* AggregateError */,
2146
- i: id,
2147
- s: void 0,
2148
- l: void 0,
2149
- c: void 0,
2150
- m: serializeString(current.message),
2151
- d: optionsNode,
2152
- a: void 0,
2153
- f: void 0,
2154
- b: void 0
2155
- };
2156
- }
2157
- function generateErrorNode2(ctx, id, current) {
2158
- const options = getErrorOptions(ctx, current);
2159
- const optionsNode = options ? generateProperties2(ctx, options) : void 0;
2160
- return {
2161
- t: 19 /* Error */,
2162
- i: id,
2163
- s: void 0,
2164
- l: void 0,
2165
- c: getErrorConstructorName(current),
2166
- m: serializeString(current.message),
2167
- d: optionsNode,
2168
- a: void 0,
2169
- f: void 0,
2170
- b: void 0
2171
- };
2172
- }
2173
- function generateHeadersNode2(ctx, id, current) {
2174
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "Headers"');
2175
- const items = {};
2176
- current.forEach((value, key) => {
2177
- items[key] = value;
2178
- });
2179
- return {
2180
- t: 32 /* Headers */,
2181
- i: id,
2182
- s: void 0,
2183
- l: void 0,
2184
- c: void 0,
2185
- m: void 0,
2186
- d: generateProperties2(ctx, items),
2187
- a: void 0,
2188
- f: void 0,
2189
- b: void 0
2190
- };
2191
- }
2192
- function generateFormDataNode2(ctx, id, current) {
2193
- assert(ctx.features & 8192 /* WebAPI */, 'Unsupported type "FormData"');
2194
- const items = {};
2195
- current.forEach((value, key) => {
2196
- items[key] = value;
2197
- });
2198
- return {
2199
- t: 33 /* FormData */,
2200
- i: id,
2201
- s: void 0,
2202
- l: void 0,
2203
- c: void 0,
2204
- m: void 0,
2205
- d: generateProperties2(ctx, items),
2206
- a: void 0,
2207
- f: void 0,
2208
- b: void 0
2209
- };
2210
- }
2211
- function parse2(ctx, current) {
2212
- switch (typeof current) {
2213
- case "boolean":
2214
- return current ? TRUE_NODE : FALSE_NODE;
2215
- case "undefined":
2216
- return UNDEFINED_NODE;
2217
- case "string":
2218
- return createStringNode(current);
2219
- case "number":
2220
- switch (current) {
2221
- case Infinity:
2222
- return INFINITY_NODE;
2223
- case -Infinity:
2224
- return NEG_INFINITY_NODE;
2225
- default:
2226
- if (current !== current) {
2227
- return NAN_NODE;
2228
- }
2229
- if (Object.is(current, -0)) {
2230
- return NEG_ZERO_NODE;
2231
- }
2232
- return createNumberNode(current);
2233
- }
2234
- case "bigint":
2235
- return createBigIntNode(ctx, current);
2236
- case "object": {
2237
- if (!current) {
2238
- return NULL_NODE;
2239
- }
2240
- const id = createIndexedValue(ctx, current);
2241
- if (ctx.markedRefs.has(id)) {
2242
- return createIndexedValueNode(id);
2243
- }
2244
- if (hasReferenceID(current)) {
2245
- return createReferenceNode(id, current);
2246
- }
2247
- if (Array.isArray(current)) {
2248
- return generateArrayNode2(ctx, id, current);
2249
- }
2250
- switch (current.constructor) {
2251
- case Date:
2252
- return createDateNode(id, current);
2253
- case RegExp:
2254
- return createRegExpNode(id, current);
2255
- case ArrayBuffer:
2256
- return createArrayBufferNode(id, current);
2257
- case Int8Array:
2258
- case Int16Array:
2259
- case Int32Array:
2260
- case Uint8Array:
2261
- case Uint16Array:
2262
- case Uint32Array:
2263
- case Uint8ClampedArray:
2264
- case Float32Array:
2265
- case Float64Array:
2266
- return createTypedArrayNode(ctx, id, current);
2267
- case BigInt64Array:
2268
- case BigUint64Array:
2269
- return createBigIntTypedArrayNode(ctx, id, current);
2270
- case DataView:
2271
- return createDataViewNode(ctx, id, current);
2272
- case Map:
2273
- return generateMapNode2(ctx, id, current);
2274
- case Set:
2275
- return generateSetNode2(ctx, id, current);
2276
- case Object:
2277
- return generateObjectNode2(
2278
- ctx,
2279
- id,
2280
- current,
2281
- false
2282
- );
2283
- case void 0:
2284
- return generateObjectNode2(
2285
- ctx,
2286
- id,
2287
- current,
2288
- true
2289
- );
2290
- case AggregateError:
2291
- if (ctx.features & 1 /* AggregateError */) {
2292
- return generateAggregateErrorNode2(ctx, id, current);
2293
- }
2294
- return generateErrorNode2(ctx, id, current);
2295
- case Error:
2296
- case EvalError:
2297
- case RangeError:
2298
- case ReferenceError:
2299
- case SyntaxError:
2300
- case TypeError:
2301
- case URIError:
2302
- return generateErrorNode2(ctx, id, current);
2303
- case URL:
2304
- return createURLNode(ctx, id, current);
2305
- case URLSearchParams:
2306
- return createURLSearchParamsNode(ctx, id, current);
2307
- case Headers:
2308
- return generateHeadersNode2(ctx, id, current);
2309
- case FormData:
2310
- return generateFormDataNode2(ctx, id, current);
2311
- default:
2312
- break;
2313
- }
2314
- if (current instanceof AggregateError) {
2315
- if (ctx.features & 1 /* AggregateError */) {
2316
- return generateAggregateErrorNode2(ctx, id, current);
2317
- }
2318
- return generateErrorNode2(ctx, id, current);
2319
- }
2320
- if (current instanceof Error) {
2321
- return generateErrorNode2(ctx, id, current);
2322
- }
2323
- if (Symbol.iterator in current) {
2324
- return generateIterableNode2(ctx, id, current);
2325
- }
2326
- throw new Error("Unsupported type");
2327
- }
2328
- case "symbol":
2329
- if (hasReferenceID(current)) {
2330
- const id = createIndexedValue(ctx, current);
2331
- if (ctx.markedRefs.has(id)) {
2332
- return createIndexedValueNode(id);
2333
- }
2334
- return createReferenceNode(id, current);
2335
- }
2336
- return createWKSymbolNode(ctx, current);
2337
- case "function": {
2338
- assert(hasReferenceID(current), "Cannot serialize function without reference ID.");
2339
- const id = createIndexedValue(ctx, current);
2340
- if (ctx.markedRefs.has(id)) {
2341
- return createIndexedValueNode(id);
2342
- }
2343
- return createReferenceNode(id, current);
2344
- }
2345
- default:
2346
- throw new Error("Unsupported type");
2415
+ throw new Error("invariant");
2347
2416
  }
2348
2417
  }
2349
- function parseSync(ctx, current) {
2350
- const result = parse2(ctx, current);
2351
- const isObject = result.t === 16 /* Object */ || result.t === 21 /* Iterable */;
2352
- return [result, getRootID(ctx, current), isObject];
2353
- }
2354
2418
 
2355
2419
  // src/index.ts
2356
2420
  function finalize(ctx, rootID, isObject, result) {
@@ -2378,39 +2442,45 @@ function finalize(ctx, rootID, isObject, result) {
2378
2442
  }
2379
2443
  function serialize(source, options) {
2380
2444
  const ctx = createParserContext(options);
2381
- const [tree, rootID, isObject] = parseSync(ctx, source);
2445
+ const tree = parseSync(ctx, source);
2382
2446
  const serial = createSerializationContext(ctx);
2383
2447
  const result = serializeTree(serial, tree);
2384
- return finalize(serial, rootID, isObject, result);
2448
+ return finalize(
2449
+ serial,
2450
+ getRootID(ctx, source),
2451
+ tree.t === 10 /* Object */,
2452
+ result
2453
+ );
2385
2454
  }
2386
2455
  async function serializeAsync(source, options) {
2387
2456
  const ctx = createParserContext(options);
2388
- const [tree, rootID, isObject] = await parseAsync(ctx, source);
2457
+ const tree = await parseAsync(ctx, source);
2389
2458
  const serial = createSerializationContext(ctx);
2390
2459
  const result = serializeTree(serial, tree);
2391
- return finalize(serial, rootID, isObject, result);
2460
+ return finalize(
2461
+ serial,
2462
+ getRootID(ctx, source),
2463
+ tree.t === 10 /* Object */,
2464
+ result
2465
+ );
2392
2466
  }
2393
2467
  function deserialize(source) {
2394
2468
  return (0, eval)(source);
2395
2469
  }
2396
2470
  function toJSON(source, options) {
2397
2471
  const ctx = createParserContext(options);
2398
- const [tree, root, isObject] = parseSync(ctx, source);
2399
2472
  return {
2400
- t: tree,
2401
- r: root,
2402
- i: isObject,
2473
+ t: parseSync(ctx, source),
2474
+ r: getRootID(ctx, source),
2403
2475
  f: ctx.features,
2404
2476
  m: Array.from(ctx.markedRefs)
2405
2477
  };
2406
2478
  }
2407
2479
  async function toJSONAsync(source, options) {
2408
2480
  const ctx = createParserContext(options);
2409
- const [tree, root, isObject] = await parseAsync(ctx, source);
2410
2481
  return {
2411
- t: tree,
2412
- r: root,
2413
- i: isObject,
2482
+ t: await parseAsync(ctx, source),
2483
+ r: getRootID(ctx, source),
2414
2484
  f: ctx.features,
2415
2485
  m: Array.from(ctx.markedRefs)
2416
2486
  };
@@ -2421,7 +2491,7 @@ function compileJSON(source) {
2421
2491
  markedRefs: source.m
2422
2492
  });
2423
2493
  const result = serializeTree(serial, source.t);
2424
- return finalize(serial, source.r, source.i, result);
2494
+ return finalize(serial, source.r, source.t.i === 10 /* Object */, result);
2425
2495
  }
2426
2496
  function fromJSON(source) {
2427
2497
  const serial = createSerializationContext({