seroval 0.4.0-alpha.4 → 0.4.0-alpha.5

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.
@@ -263,48 +263,6 @@ function parseTargets(targets) {
263
263
  return flags;
264
264
  }
265
265
 
266
- // src/quote.ts
267
- function quote(str) {
268
- let result = "";
269
- let lastPos = 0;
270
- for (let i = 0, len = str.length; i < len; i++) {
271
- let replacement;
272
- switch (str[i]) {
273
- case '"':
274
- replacement = '\\"';
275
- break;
276
- case "\\":
277
- replacement = "\\\\";
278
- break;
279
- case "<":
280
- replacement = "\\x3C";
281
- break;
282
- case "\n":
283
- replacement = "\\n";
284
- break;
285
- case "\r":
286
- replacement = "\\r";
287
- break;
288
- case "\u2028":
289
- replacement = "\\u2028";
290
- break;
291
- case "\u2029":
292
- replacement = "\\u2029";
293
- break;
294
- default:
295
- continue;
296
- }
297
- result += str.slice(lastPos, i) + replacement;
298
- lastPos = i + 1;
299
- }
300
- if (lastPos === 0) {
301
- result = str;
302
- } else {
303
- result += str.slice(lastPos);
304
- }
305
- return `"${result}"`;
306
- }
307
-
308
266
  // src/get-identifier.ts
309
267
  var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
310
268
  var REF_START_CHARS_LEN = REF_START_CHARS.length;
@@ -322,7 +280,7 @@ function getIdentifier(index) {
322
280
  return ref;
323
281
  }
324
282
 
325
- // src/tree.ts
283
+ // src/context.ts
326
284
  var DEFAULT_OPTIONS = {
327
285
  target: "es2023"
328
286
  };
@@ -344,71 +302,6 @@ function createSerializationContext(options) {
344
302
  markedRefs: new Set(options.markedRefs)
345
303
  };
346
304
  }
347
- function getAssignmentExpression(assignment) {
348
- switch (assignment.type) {
349
- case "index":
350
- return `${assignment.source}=${assignment.value}`;
351
- case "map":
352
- return `${assignment.source}.set(${assignment.key},${assignment.value})`;
353
- case "set":
354
- return `${assignment.source}.add(${assignment.value})`;
355
- default:
356
- return "";
357
- }
358
- }
359
- function mergeAssignments(assignments) {
360
- const newAssignments = [];
361
- let current = assignments[0];
362
- let prev = current;
363
- for (let i = 1, len = assignments.length; i < len; i++) {
364
- const item = assignments[i];
365
- if (item.type === prev.type) {
366
- if (item.type === "index" && item.value === prev.value) {
367
- current = {
368
- type: "index",
369
- source: item.source,
370
- value: getAssignmentExpression(current)
371
- };
372
- } else if (item.type === "map" && item.source === prev.source) {
373
- current = {
374
- type: "map",
375
- source: getAssignmentExpression(current),
376
- key: item.key,
377
- value: item.value
378
- };
379
- } else if (item.type === "set" && item.source === prev.source) {
380
- current = {
381
- type: "set",
382
- source: getAssignmentExpression(current),
383
- value: item.value
384
- };
385
- } else {
386
- newAssignments.push(current);
387
- current = item;
388
- }
389
- } else {
390
- newAssignments.push(current);
391
- current = item;
392
- }
393
- prev = item;
394
- }
395
- newAssignments.push(current);
396
- return newAssignments;
397
- }
398
- function resolveAssignments(assignments) {
399
- if (assignments.length) {
400
- let result = "";
401
- const merged = mergeAssignments(assignments);
402
- for (let i = 0, len = merged.length; i < len; i++) {
403
- result += `${getAssignmentExpression(merged[i])},`;
404
- }
405
- return result;
406
- }
407
- return void 0;
408
- }
409
- function resolvePatches(ctx) {
410
- return resolveAssignments(ctx.assignments);
411
- }
412
305
  function markRef(ctx, current) {
413
306
  ctx.markedRefs.add(current);
414
307
  }
@@ -430,12 +323,6 @@ function getRefParam(ctx, index) {
430
323
  ctx.vars[actualIndex] = result;
431
324
  return result;
432
325
  }
433
- function assignRef(ctx, index, value) {
434
- if (ctx.markedRefs.has(index)) {
435
- return `${getRefParam(ctx, index)}=${value}`;
436
- }
437
- return value;
438
- }
439
326
  function createRef(ctx, index) {
440
327
  const current = ctx.refs.get(index);
441
328
  if (current != null) {
@@ -445,39 +332,50 @@ function createRef(ctx, index) {
445
332
  ctx.refs.set(index, id);
446
333
  return id;
447
334
  }
448
- function createAssignment(ctx, source, value) {
449
- ctx.assignments.push({
450
- type: "index",
451
- source,
452
- value
453
- });
454
- }
455
- function createSetAdd(ctx, ref, value) {
456
- markRef(ctx, ref);
457
- ctx.assignments.push({
458
- type: "set",
459
- source: getRefParam(ctx, ref),
460
- value
461
- });
462
- }
463
- function createMapSet(ctx, ref, key, value) {
464
- markRef(ctx, ref);
465
- ctx.assignments.push({
466
- type: "map",
467
- source: getRefParam(ctx, ref),
468
- key,
469
- value
470
- });
471
- }
472
- function createArrayAssign(ctx, ref, index, value) {
473
- markRef(ctx, ref);
474
- createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
475
- }
476
- function createObjectAssign(ctx, ref, key, value, computed) {
477
- markRef(ctx, ref);
478
- const member = computed ? `[${key}]` : `.${key}`;
479
- createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
335
+
336
+ // src/quote.ts
337
+ function quote(str) {
338
+ let result = "";
339
+ let lastPos = 0;
340
+ for (let i = 0, len = str.length; i < len; i++) {
341
+ let replacement;
342
+ switch (str[i]) {
343
+ case '"':
344
+ replacement = '\\"';
345
+ break;
346
+ case "\\":
347
+ replacement = "\\\\";
348
+ break;
349
+ case "<":
350
+ replacement = "\\x3C";
351
+ break;
352
+ case "\n":
353
+ replacement = "\\n";
354
+ break;
355
+ case "\r":
356
+ replacement = "\\r";
357
+ break;
358
+ case "\u2028":
359
+ replacement = "\\u2028";
360
+ break;
361
+ case "\u2029":
362
+ replacement = "\\u2029";
363
+ break;
364
+ default:
365
+ continue;
366
+ }
367
+ result += str.slice(lastPos, i) + replacement;
368
+ lastPos = i + 1;
369
+ }
370
+ if (lastPos === 0) {
371
+ result = str;
372
+ } else {
373
+ result += str.slice(lastPos);
374
+ }
375
+ return `"${result}"`;
480
376
  }
377
+
378
+ // src/tree/shared.ts
481
379
  function getErrorConstructor(error) {
482
380
  if (error instanceof EvalError) {
483
381
  return "EvalError";
@@ -552,46 +450,16 @@ function generateSemiPrimitiveValue(ctx, current, id) {
552
450
  if (constructorCheck(current, RegExp)) {
553
451
  return [4 /* RegExp */, String(current), id];
554
452
  }
555
- if (constructorCheck(current, Int8Array)) {
556
- assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int8Array"');
557
- return [14 /* TypedArray */, ["Int8Array", current.toString(), current.byteOffset], id];
558
- }
559
- if (constructorCheck(current, Int16Array)) {
560
- assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int16Array"');
561
- return [14 /* TypedArray */, ["Int16Array", current.toString(), current.byteOffset], id];
562
- }
563
- if (constructorCheck(current, Int32Array)) {
564
- assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int32Array"');
565
- return [14 /* TypedArray */, ["Int32Array", current.toString(), current.byteOffset], id];
566
- }
567
- if (constructorCheck(current, Uint8Array)) {
568
- assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint8Array"');
569
- return [14 /* TypedArray */, ["Uint8Array", current.toString(), current.byteOffset], id];
570
- }
571
- if (constructorCheck(current, Uint16Array)) {
572
- assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint16Array"');
573
- return [14 /* TypedArray */, ["Uint16Array", current.toString(), current.byteOffset], id];
574
- }
575
- if (constructorCheck(current, Uint32Array)) {
576
- assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint32Array"');
577
- return [14 /* TypedArray */, ["Uint32Array", current.toString(), current.byteOffset], id];
453
+ if (constructorCheck(current, Int8Array) || constructorCheck(current, Int16Array) || constructorCheck(current, Int32Array) || constructorCheck(current, Uint8Array) || constructorCheck(current, Uint16Array) || constructorCheck(current, Uint32Array) || constructorCheck(current, Uint8ClampedArray) || constructorCheck(current, Float32Array) || constructorCheck(current, Float64Array)) {
454
+ const constructor = current.constructor.name;
455
+ assert(ctx.features & 1024 /* TypedArray */, `Unsupported value type "${constructor}"`);
456
+ return [14 /* TypedArray */, [constructor, current.toString(), current.byteOffset], id];
578
457
  }
579
- if (constructorCheck(current, Uint8ClampedArray)) {
580
- assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint8ClampedArray"');
581
- return [14 /* TypedArray */, ["Uint8ClampedArray", current.toString(), current.byteOffset], id];
582
- }
583
- if (constructorCheck(current, Float32Array)) {
584
- assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float32Array"');
585
- return [14 /* TypedArray */, ["Float32Array", current.toString(), current.byteOffset], id];
586
- }
587
- if (constructorCheck(current, Float64Array)) {
588
- assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float64Array"');
589
- return [14 /* TypedArray */, ["Float64Array", current.toString(), current.byteOffset], id];
590
- }
591
- if (constructorCheck(current, BigInt64Array)) {
458
+ if (constructorCheck(current, BigInt64Array) || constructorCheck(current, BigUint64Array)) {
459
+ const constructor = current.constructor.name;
592
460
  assert(
593
461
  ctx.features & 2048 /* BigIntTypedArray */,
594
- 'Unsupported value type "BigInt64Array"'
462
+ `Unsupported value type "${constructor}"`
595
463
  );
596
464
  let result = "";
597
465
  const cap = current.length - 1;
@@ -601,30 +469,42 @@ function generateSemiPrimitiveValue(ctx, current, id) {
601
469
  result += `"${current[cap]}"`;
602
470
  return [
603
471
  15 /* BigIntTypedArray */,
604
- ["BigInt64Array", result, current.byteOffset],
605
- id
606
- ];
607
- }
608
- if (constructorCheck(current, BigUint64Array)) {
609
- assert(
610
- ctx.features & 2048 /* BigIntTypedArray */,
611
- 'Unsupported value type "BigUint64Array"'
612
- );
613
- let result = "";
614
- const cap = current.length - 1;
615
- for (let i = 0; i < cap; i++) {
616
- result += `"${current[i]}n",`;
617
- }
618
- result += `"${current[cap]}"`;
619
- return [
620
- 15 /* BigIntTypedArray */,
621
- ["BigInt64Array", result, current.byteOffset],
472
+ [constructor, result, current.byteOffset],
622
473
  id
623
474
  ];
624
475
  }
625
476
  return void 0;
626
477
  }
627
- function serializePropertiesSync(ctx, properties) {
478
+ function serializePrimitive(value) {
479
+ if (value === true) {
480
+ return "!0";
481
+ }
482
+ if (value === false) {
483
+ return "!1";
484
+ }
485
+ if (value === void 0) {
486
+ return "void 0";
487
+ }
488
+ if (value === null) {
489
+ return null;
490
+ }
491
+ if (typeof value === "string") {
492
+ return quote(value);
493
+ }
494
+ if (Object.is(value, -0)) {
495
+ return "-0";
496
+ }
497
+ if (Object.is(value, Infinity)) {
498
+ return "1/0";
499
+ }
500
+ if (Object.is(value, -Infinity)) {
501
+ return "-1/0";
502
+ }
503
+ return value;
504
+ }
505
+
506
+ // src/tree/async.ts
507
+ async function serializePropertiesAsync(ctx, properties) {
628
508
  const keys = Object.keys(properties);
629
509
  const size = keys.length;
630
510
  const keyNodes = new Array(size);
@@ -640,156 +520,111 @@ function serializePropertiesSync(ctx, properties) {
640
520
  deferredSize++;
641
521
  } else {
642
522
  keyNodes[nodesSize] = key;
643
- valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
523
+ valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
644
524
  nodesSize++;
645
525
  }
646
526
  }
647
527
  for (let i = 0; i < deferredSize; i++) {
648
528
  keyNodes[nodesSize + i] = deferredKeys[i];
649
- valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
529
+ valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
650
530
  }
651
531
  return [keyNodes, valueNodes, size];
652
532
  }
653
- function generateTreeSync(ctx, current) {
654
- if (isPrimitive(current)) {
655
- return [0 /* Primitive */, serializePrimitive(current)];
656
- }
657
- const id = generateRef(ctx, current);
658
- if (Array.isArray(id)) {
659
- return id;
533
+ async function generateSetNode(ctx, current, id) {
534
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
535
+ const len = current.size;
536
+ const nodes = new Array(len);
537
+ const deferred = new Array(len);
538
+ let deferredSize = 0;
539
+ let nodeSize = 0;
540
+ for (const item of current.keys()) {
541
+ if (isIterable(item)) {
542
+ deferred[deferredSize++] = item;
543
+ } else {
544
+ nodes[nodeSize++] = await generateTreeAsync(ctx, item);
545
+ }
660
546
  }
661
- const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
662
- if (semiPrimitive) {
663
- return semiPrimitive;
547
+ for (let i = 0; i < deferredSize; i++) {
548
+ nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
664
549
  }
665
- if (constructorCheck(current, Set)) {
666
- assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
667
- const len = current.size;
668
- const nodes = new Array(len);
669
- const deferred = new Array(len);
670
- let deferredSize = 0;
671
- let nodeSize = 0;
672
- for (const item of current.keys()) {
673
- if (isIterable(item)) {
674
- deferred[deferredSize++] = item;
675
- } else {
676
- nodes[nodeSize++] = generateTreeSync(ctx, item);
677
- }
678
- }
679
- for (let i = 0; i < deferredSize; i++) {
680
- nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
550
+ return [5 /* Set */, nodes, id];
551
+ }
552
+ async function generateMapNode(ctx, current, id) {
553
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
554
+ const len = current.size;
555
+ const keyNodes = new Array(len);
556
+ const valueNodes = new Array(len);
557
+ const deferredKey = new Array(len);
558
+ const deferredValue = new Array(len);
559
+ let deferredSize = 0;
560
+ let nodeSize = 0;
561
+ for (const [key, value] of current.entries()) {
562
+ if (isIterable(key) || isIterable(value)) {
563
+ deferredKey[deferredSize] = key;
564
+ deferredValue[deferredSize] = value;
565
+ deferredSize++;
566
+ } else {
567
+ keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
568
+ valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
569
+ nodeSize++;
681
570
  }
682
- return [5 /* Set */, nodes, id];
683
571
  }
684
- if (constructorCheck(current, Map)) {
685
- assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
686
- const len = current.size;
687
- const keyNodes = new Array(len);
688
- const valueNodes = new Array(len);
689
- const deferredKey = new Array(len);
690
- const deferredValue = new Array(len);
691
- let deferredSize = 0;
692
- let nodeSize = 0;
693
- for (const [key, value] of current.entries()) {
694
- if (isIterable(key) || isIterable(value)) {
695
- deferredKey[deferredSize] = key;
696
- deferredValue[deferredSize] = value;
697
- deferredSize++;
572
+ for (let i = 0; i < deferredSize; i++) {
573
+ keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
574
+ valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
575
+ }
576
+ return [6 /* Map */, [keyNodes, valueNodes, len], id];
577
+ }
578
+ async function generateNodeList(ctx, current) {
579
+ const size = current.length;
580
+ const nodes = new Array(size);
581
+ const deferred = new Array(size);
582
+ for (let i = 0; i < size; i++) {
583
+ if (i in current) {
584
+ if (isIterable(current[i])) {
585
+ deferred[i] = current[i];
698
586
  } else {
699
- keyNodes[nodeSize] = generateTreeSync(ctx, key);
700
- valueNodes[nodeSize] = generateTreeSync(ctx, value);
701
- nodeSize++;
587
+ nodes[i] = await generateTreeAsync(ctx, current[i]);
702
588
  }
703
589
  }
704
- for (let i = 0; i < deferredSize; i++) {
705
- keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
706
- valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
707
- }
708
- return [6 /* Map */, [keyNodes, valueNodes, len], id];
709
590
  }
710
- if (Array.isArray(current)) {
711
- const size = current.length;
712
- const nodes = new Array(size);
713
- const deferred = new Array(size);
714
- for (let i = 0; i < size; i++) {
715
- if (i in current) {
716
- if (isIterable(current[i])) {
717
- deferred[i] = current[i];
718
- } else {
719
- nodes[i] = generateTreeSync(ctx, current[i]);
720
- }
721
- }
722
- }
723
- for (let i = 0; i < size; i++) {
724
- if (i in deferred) {
725
- nodes[i] = generateTreeSync(ctx, deferred[i]);
726
- }
591
+ for (let i = 0; i < size; i++) {
592
+ if (i in deferred) {
593
+ nodes[i] = await generateTreeAsync(ctx, deferred[i]);
727
594
  }
728
- return [7 /* Array */, nodes, id];
729
- }
730
- if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
731
- const options = getErrorOptions(current);
732
- const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
733
- const errorsNode = generateTreeSync(ctx, current.errors);
734
- return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
735
595
  }
736
- if (current instanceof Error) {
737
- const options = getErrorOptions(current);
738
- const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
739
- return [
740
- 11 /* Error */,
741
- [getErrorConstructor(current), current.message, optionsNode],
742
- id
743
- ];
744
- }
745
- if (isIterable(current)) {
746
- assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
747
- const options = getIterableOptions(current);
748
- return [13 /* Iterable */, [
749
- // Parse options first before the items
750
- options ? serializePropertiesSync(ctx, options) : void 0,
751
- generateTreeSync(ctx, Array.from(current))
752
- ], id];
753
- }
754
- const empty = current.constructor == null;
755
- if (current.constructor === Object || empty) {
756
- return [
757
- empty ? 9 /* NullConstructor */ : 8 /* Object */,
758
- serializePropertiesSync(ctx, current),
759
- id
760
- ];
761
- }
762
- throw new Error("Unsupported value");
596
+ return nodes;
763
597
  }
764
- function parseSync(ctx, current) {
765
- const result = generateTreeSync(ctx, current);
766
- return [result, createRef(ctx, current), result[0] === 8 /* Object */];
598
+ async function generateArrayNode(ctx, current, id) {
599
+ return [7 /* Array */, await generateNodeList(ctx, current), id];
767
600
  }
768
- async function serializePropertiesAsync(ctx, properties) {
769
- const keys = Object.keys(properties);
770
- const size = keys.length;
771
- const keyNodes = new Array(size);
772
- const valueNodes = new Array(size);
773
- const deferredKeys = new Array(size);
774
- const deferredValues = new Array(size);
775
- let deferredSize = 0;
776
- let nodesSize = 0;
777
- for (const key of keys) {
778
- if (isIterable(properties[key])) {
779
- deferredKeys[deferredSize] = key;
780
- deferredValues[deferredSize] = properties[key];
781
- deferredSize++;
782
- } else {
783
- keyNodes[nodesSize] = key;
784
- valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
785
- nodesSize++;
786
- }
787
- }
788
- for (let i = 0; i < deferredSize; i++) {
789
- keyNodes[nodesSize + i] = deferredKeys[i];
790
- valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
791
- }
792
- return [keyNodes, valueNodes, size];
601
+ async function generateAggregateErrorNode(ctx, current, id) {
602
+ const options = getErrorOptions(current);
603
+ const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
604
+ const errorsNode = await generateTreeAsync(ctx, current.errors);
605
+ return [
606
+ 12 /* AggregateError */,
607
+ [current.message, optionsNode, errorsNode],
608
+ id
609
+ ];
610
+ }
611
+ async function generateErrorNode(ctx, current, id) {
612
+ const options = getErrorOptions(current);
613
+ const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
614
+ return [
615
+ 11 /* Error */,
616
+ [getErrorConstructor(current), current.message, optionsNode],
617
+ id
618
+ ];
619
+ }
620
+ async function generateIterableNode(ctx, current, id) {
621
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
622
+ const options = getIterableOptions(current);
623
+ return [13 /* Iterable */, [
624
+ // Parse options first before the items
625
+ options ? await serializePropertiesAsync(ctx, options) : void 0,
626
+ await generateNodeList(ctx, Array.from(current))
627
+ ], id];
793
628
  }
794
629
  async function generateTreeAsync(ctx, current) {
795
630
  if (isPrimitive(current)) {
@@ -812,93 +647,22 @@ async function generateTreeAsync(ctx, current) {
812
647
  ]);
813
648
  }
814
649
  if (constructorCheck(current, Set)) {
815
- assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
816
- const len = current.size;
817
- const nodes = new Array(len);
818
- const deferred = new Array(len);
819
- let deferredSize = 0;
820
- let nodeSize = 0;
821
- for (const item of current.keys()) {
822
- if (isIterable(item)) {
823
- deferred[deferredSize++] = item;
824
- } else {
825
- nodes[nodeSize++] = await generateTreeAsync(ctx, item);
826
- }
827
- }
828
- for (let i = 0; i < deferredSize; i++) {
829
- nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
830
- }
831
- return [5 /* Set */, nodes, id];
650
+ return generateSetNode(ctx, current, id);
832
651
  }
833
652
  if (constructorCheck(current, Map)) {
834
- assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
835
- const len = current.size;
836
- const keyNodes = new Array(len);
837
- const valueNodes = new Array(len);
838
- const deferredKey = new Array(len);
839
- const deferredValue = new Array(len);
840
- let deferredSize = 0;
841
- let nodeSize = 0;
842
- for (const [key, value] of current.entries()) {
843
- if (isIterable(key) || isIterable(value)) {
844
- deferredKey[deferredSize] = key;
845
- deferredValue[deferredSize] = value;
846
- deferredSize++;
847
- } else {
848
- keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
849
- valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
850
- nodeSize++;
851
- }
852
- }
853
- for (let i = 0; i < deferredSize; i++) {
854
- keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
855
- valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
856
- }
857
- return [6 /* Map */, [keyNodes, valueNodes, len], id];
653
+ return generateMapNode(ctx, current, id);
858
654
  }
859
655
  if (Array.isArray(current)) {
860
- const size = current.length;
861
- const nodes = new Array(size);
862
- const deferred = new Array(size);
863
- for (let i = 0; i < size; i++) {
864
- const item = current[i];
865
- if (i in current) {
866
- if (isIterable(item)) {
867
- deferred[i] = item;
868
- } else {
869
- nodes[i] = await generateTreeAsync(ctx, item);
870
- }
871
- }
872
- }
873
- for (let i = 0; i < size; i++) {
874
- if (i in deferred) {
875
- nodes[i] = await generateTreeAsync(ctx, deferred[i]);
876
- }
877
- }
878
- return [7 /* Array */, nodes, id];
656
+ return generateArrayNode(ctx, current, id);
879
657
  }
880
658
  if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
881
- const options = getErrorOptions(current);
882
- const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
883
- const errorsNode = await generateTreeAsync(ctx, current.errors);
884
- return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
659
+ return generateAggregateErrorNode(ctx, current, id);
885
660
  }
886
661
  if (current instanceof Error) {
887
- const options = getErrorOptions(current);
888
- const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
889
- return [
890
- 11 /* Error */,
891
- [getErrorConstructor(current), current.message, optionsNode],
892
- id
893
- ];
662
+ return generateErrorNode(ctx, current, id);
894
663
  }
895
664
  if (isIterable(current)) {
896
- assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
897
- const options = getIterableOptions(current);
898
- return [13 /* Iterable */, [
899
- options ? await serializePropertiesAsync(ctx, options) : void 0,
900
- await generateTreeAsync(ctx, Array.from(current))
901
- ], id];
665
+ return generateIterableNode(ctx, current, id);
902
666
  }
903
667
  const empty = current.constructor == null;
904
668
  if (current.constructor === Object || empty) {
@@ -914,33 +678,112 @@ async function parseAsync(ctx, current) {
914
678
  const result = await generateTreeAsync(ctx, current);
915
679
  return [result, createRef(ctx, current), result[0] === 8 /* Object */];
916
680
  }
917
- function serializePrimitive(value) {
918
- if (value === true) {
919
- return "!0";
920
- }
921
- if (value === false) {
922
- return "!1";
923
- }
924
- if (value === void 0) {
925
- return "void 0";
926
- }
927
- if (value === null) {
928
- return null;
929
- }
930
- if (typeof value === "string") {
931
- return quote(value);
681
+
682
+ // src/tree/serialize.ts
683
+ function getAssignmentExpression(assignment) {
684
+ switch (assignment.type) {
685
+ case "index":
686
+ return `${assignment.source}=${assignment.value}`;
687
+ case "map":
688
+ return `${assignment.source}.set(${assignment.key},${assignment.value})`;
689
+ case "set":
690
+ return `${assignment.source}.add(${assignment.value})`;
691
+ default:
692
+ return "";
932
693
  }
933
- if (Object.is(value, -0)) {
934
- return "-0";
694
+ }
695
+ function mergeAssignments(assignments) {
696
+ const newAssignments = [];
697
+ let current = assignments[0];
698
+ let prev = current;
699
+ for (let i = 1, len = assignments.length; i < len; i++) {
700
+ const item = assignments[i];
701
+ if (item.type === prev.type) {
702
+ if (item.type === "index" && item.value === prev.value) {
703
+ current = {
704
+ type: "index",
705
+ source: item.source,
706
+ value: getAssignmentExpression(current)
707
+ };
708
+ } else if (item.type === "map" && item.source === prev.source) {
709
+ current = {
710
+ type: "map",
711
+ source: getAssignmentExpression(current),
712
+ key: item.key,
713
+ value: item.value
714
+ };
715
+ } else if (item.type === "set" && item.source === prev.source) {
716
+ current = {
717
+ type: "set",
718
+ source: getAssignmentExpression(current),
719
+ value: item.value
720
+ };
721
+ } else {
722
+ newAssignments.push(current);
723
+ current = item;
724
+ }
725
+ } else {
726
+ newAssignments.push(current);
727
+ current = item;
728
+ }
729
+ prev = item;
935
730
  }
936
- if (Object.is(value, Infinity)) {
937
- return "1/0";
731
+ newAssignments.push(current);
732
+ return newAssignments;
733
+ }
734
+ function resolveAssignments(assignments) {
735
+ if (assignments.length) {
736
+ let result = "";
737
+ const merged = mergeAssignments(assignments);
738
+ for (let i = 0, len = merged.length; i < len; i++) {
739
+ result += `${getAssignmentExpression(merged[i])},`;
740
+ }
741
+ return result;
938
742
  }
939
- if (Object.is(value, -Infinity)) {
940
- return "-1/0";
743
+ return void 0;
744
+ }
745
+ function resolvePatches(ctx) {
746
+ return resolveAssignments(ctx.assignments);
747
+ }
748
+ function assignRef(ctx, index, value) {
749
+ if (ctx.markedRefs.has(index)) {
750
+ return `${getRefParam(ctx, index)}=${value}`;
941
751
  }
942
752
  return value;
943
753
  }
754
+ function createAssignment(ctx, source, value) {
755
+ ctx.assignments.push({
756
+ type: "index",
757
+ source,
758
+ value
759
+ });
760
+ }
761
+ function createSetAdd(ctx, ref, value) {
762
+ markRef(ctx, ref);
763
+ ctx.assignments.push({
764
+ type: "set",
765
+ source: getRefParam(ctx, ref),
766
+ value
767
+ });
768
+ }
769
+ function createMapSet(ctx, ref, key, value) {
770
+ markRef(ctx, ref);
771
+ ctx.assignments.push({
772
+ type: "map",
773
+ source: getRefParam(ctx, ref),
774
+ key,
775
+ value
776
+ });
777
+ }
778
+ function createArrayAssign(ctx, ref, index, value) {
779
+ markRef(ctx, ref);
780
+ createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
781
+ }
782
+ function createObjectAssign(ctx, ref, key, value, computed) {
783
+ markRef(ctx, ref);
784
+ const member = computed ? `[${key}]` : `.${key}`;
785
+ createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
786
+ }
944
787
  var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
945
788
  function serializeAssignments(ctx, targetRef, [keys, values, size]) {
946
789
  ctx.stack.push(targetRef);
@@ -968,6 +811,9 @@ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
968
811
  return resolveAssignments(mainAssignments);
969
812
  }
970
813
  function serializeObject(ctx, sourceID, [keys, values, size]) {
814
+ if (size === 0) {
815
+ return "{}";
816
+ }
971
817
  let result = "";
972
818
  ctx.stack.push(sourceID);
973
819
  for (let i = 0; i < size; i++) {
@@ -992,205 +838,227 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
992
838
  ctx.stack.pop();
993
839
  return `{${result.substring(0, result.length - 1)}}`;
994
840
  }
995
- function serializeTree(ctx, [type, value, id]) {
996
- switch (type) {
997
- case 0 /* Primitive */:
998
- return String(value);
999
- case 2 /* Reference */:
1000
- return getRefParam(ctx, value);
1001
- case 10 /* Promise */: {
1002
- let serialized;
1003
- if (isReferenceInStack(ctx, value)) {
1004
- serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
841
+ function serializePromise(ctx, value, id) {
842
+ let serialized;
843
+ if (isReferenceInStack(ctx, value)) {
844
+ serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
845
+ } else {
846
+ ctx.stack.push(id);
847
+ const result = serializeTree(ctx, value);
848
+ ctx.stack.pop();
849
+ serialized = `Promise.resolve(${result})`;
850
+ }
851
+ return assignRef(ctx, id, serialized);
852
+ }
853
+ function serializeTypedArray(ctx, constructor, init, byteOffset, id) {
854
+ let args = `[${init}]`;
855
+ if (byteOffset !== 0) {
856
+ args += `,${byteOffset}`;
857
+ }
858
+ return assignRef(ctx, id, `new ${constructor}(${args})`);
859
+ }
860
+ function serializeSet(ctx, value, id) {
861
+ let serialized = "new Set";
862
+ const size = value.length;
863
+ if (size) {
864
+ let result = "";
865
+ ctx.stack.push(id);
866
+ for (let i = 0; i < size; i++) {
867
+ const item = value[i];
868
+ if (isReferenceInStack(ctx, item)) {
869
+ createSetAdd(ctx, id, getRefParam(ctx, item[1]));
1005
870
  } else {
1006
- ctx.stack.push(id);
1007
- const result = serializeTree(ctx, value);
1008
- ctx.stack.pop();
1009
- serialized = `Promise.resolve(${result})`;
871
+ result += `${serializeTree(ctx, item)},`;
1010
872
  }
1011
- return assignRef(ctx, id, serialized);
1012
873
  }
1013
- case 1 /* BigInt */:
1014
- return value;
1015
- case 3 /* Date */:
1016
- return assignRef(ctx, id, `new Date("${value}")`);
1017
- case 4 /* RegExp */:
1018
- return assignRef(ctx, id, value);
1019
- case 15 /* BigIntTypedArray */:
1020
- case 14 /* TypedArray */: {
1021
- let args = `[${value[1]}]`;
1022
- if (value[2] !== 0) {
1023
- args += `,${value[2]}`;
1024
- }
1025
- return assignRef(ctx, id, `new ${value[0]}(${args})`);
874
+ ctx.stack.pop();
875
+ if (result) {
876
+ serialized += `([${result.substring(0, result.length - 1)}])`;
1026
877
  }
1027
- case 5 /* Set */: {
1028
- let serialized = "new Set";
1029
- const size = value.length;
1030
- if (size) {
1031
- let result = "";
1032
- ctx.stack.push(id);
1033
- for (let i = 0; i < size; i++) {
1034
- const item = value[i];
1035
- if (isReferenceInStack(ctx, item)) {
1036
- createSetAdd(ctx, id, getRefParam(ctx, item[1]));
1037
- } else {
1038
- result += `${serializeTree(ctx, item)},`;
1039
- }
1040
- }
1041
- ctx.stack.pop();
1042
- if (result) {
1043
- serialized += `([${result.substring(0, result.length - 1)}])`;
878
+ }
879
+ return assignRef(ctx, id, serialized);
880
+ }
881
+ function serializeMap(ctx, keys, values, size, id) {
882
+ let serialized = "new Map";
883
+ if (size) {
884
+ let result = "";
885
+ ctx.stack.push(id);
886
+ for (let i = 0; i < size; i++) {
887
+ const key = keys[i];
888
+ const val = values[i];
889
+ if (isReferenceInStack(ctx, key)) {
890
+ const keyRef = getRefParam(ctx, key[1]);
891
+ if (isReferenceInStack(ctx, val)) {
892
+ const valueRef = getRefParam(ctx, val[1]);
893
+ createMapSet(ctx, id, keyRef, valueRef);
894
+ } else {
895
+ const parent = ctx.stack;
896
+ ctx.stack = [];
897
+ createMapSet(ctx, id, keyRef, serializeTree(ctx, val));
898
+ ctx.stack = parent;
1044
899
  }
900
+ } else if (isReferenceInStack(ctx, val)) {
901
+ const valueRef = getRefParam(ctx, val[1]);
902
+ const parent = ctx.stack;
903
+ ctx.stack = [];
904
+ createMapSet(ctx, id, serializeTree(ctx, key), valueRef);
905
+ ctx.stack = parent;
906
+ } else {
907
+ result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
1045
908
  }
1046
- return assignRef(ctx, id, serialized);
1047
909
  }
1048
- case 6 /* Map */: {
1049
- let serialized = "new Map";
1050
- const size = value[2];
1051
- if (size) {
1052
- let result = "";
1053
- ctx.stack.push(id);
1054
- for (let i = 0; i < size; i++) {
1055
- const key = value[0][i];
1056
- const val = value[1][i];
1057
- if (isReferenceInStack(ctx, key)) {
1058
- const keyRef = getRefParam(ctx, key[1]);
1059
- if (isReferenceInStack(ctx, val)) {
1060
- const valueRef = getRefParam(ctx, val[1]);
1061
- createMapSet(ctx, id, keyRef, valueRef);
1062
- } else {
1063
- const parent = ctx.stack;
1064
- ctx.stack = [];
1065
- createMapSet(ctx, id, keyRef, serializeTree(ctx, val));
1066
- ctx.stack = parent;
1067
- }
1068
- } else if (isReferenceInStack(ctx, val)) {
1069
- const valueRef = getRefParam(ctx, val[1]);
1070
- const parent = ctx.stack;
1071
- ctx.stack = [];
1072
- createMapSet(ctx, id, serializeTree(ctx, key), valueRef);
1073
- ctx.stack = parent;
1074
- } else {
1075
- result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
1076
- }
1077
- }
1078
- ctx.stack.pop();
1079
- if (result) {
1080
- serialized += `([${result.substring(0, result.length - 1)}])`;
1081
- }
1082
- }
1083
- return assignRef(ctx, id, serialized);
910
+ ctx.stack.pop();
911
+ if (result) {
912
+ serialized += `([${result.substring(0, result.length - 1)}])`;
1084
913
  }
1085
- case 7 /* Array */: {
1086
- const size = value.length;
1087
- let values = "";
1088
- ctx.stack.push(id);
1089
- for (let i = 0; i < size; i++) {
1090
- const item = value[i];
1091
- if (item) {
1092
- if (isReferenceInStack(ctx, item)) {
1093
- createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
1094
- values += ",";
1095
- } else {
1096
- values += serializeTree(ctx, item);
1097
- if (i < size - 1) {
1098
- values += ",";
1099
- }
1100
- }
1101
- } else {
914
+ }
915
+ return assignRef(ctx, id, serialized);
916
+ }
917
+ function serializeNodeList(ctx, value, id) {
918
+ const size = value.length;
919
+ let values = "";
920
+ for (let i = 0; i < size; i++) {
921
+ const item = value[i];
922
+ if (item) {
923
+ if (isReferenceInStack(ctx, item)) {
924
+ createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
925
+ values += ",";
926
+ } else {
927
+ values += serializeTree(ctx, item);
928
+ if (i < size - 1) {
1102
929
  values += ",";
1103
930
  }
1104
931
  }
1105
- ctx.stack.pop();
1106
- return assignRef(ctx, id, `[${values}]`);
932
+ } else {
933
+ values += ",";
1107
934
  }
1108
- case 12 /* AggregateError */: {
1109
- ctx.stack.push(id);
1110
- let serialized = `new AggregateError(${serializeTree(ctx, value[2])},${quote(value[0])})`;
1111
- ctx.stack.pop();
1112
- if (value[1]) {
1113
- if (ctx.features & 64 /* ObjectAssign */) {
1114
- const options = serializeObject(ctx, id, value[1]);
1115
- serialized = `Object.assign(${serialized},${options})`;
1116
- } else {
1117
- markRef(ctx, id);
1118
- const assignments = serializeAssignments(ctx, id, value[1]);
1119
- if (assignments) {
1120
- const ref = getRefParam(ctx, id);
1121
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1122
- }
1123
- }
935
+ }
936
+ return `[${values}]`;
937
+ }
938
+ function serializeArray(ctx, value, id) {
939
+ ctx.stack.push(id);
940
+ const result = serializeNodeList(ctx, value, id);
941
+ ctx.stack.pop();
942
+ return assignRef(ctx, id, result);
943
+ }
944
+ function serializeWithObjectAssign(ctx, value, id, serialized) {
945
+ const fields = serializeObject(ctx, id, value);
946
+ if (fields !== "{}") {
947
+ return `Object.assign(${serialized},${fields})`;
948
+ }
949
+ return serialized;
950
+ }
951
+ function serializeAggregateError(ctx, message, options, errors, id) {
952
+ ctx.stack.push(id);
953
+ let serialized = `new AggregateError(${serializeTree(ctx, errors)},${quote(message)})`;
954
+ ctx.stack.pop();
955
+ if (options) {
956
+ if (ctx.features & 64 /* ObjectAssign */) {
957
+ serialized = serializeWithObjectAssign(ctx, options, id, serialized);
958
+ } else {
959
+ markRef(ctx, id);
960
+ const assignments = serializeAssignments(ctx, id, options);
961
+ if (assignments) {
962
+ const ref = getRefParam(ctx, id);
963
+ return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1124
964
  }
1125
- return assignRef(ctx, id, serialized);
1126
965
  }
1127
- case 11 /* Error */: {
1128
- let serialized = `new ${value[0]}(${quote(value[1])})`;
1129
- if (value[2]) {
1130
- if (ctx.features & 64 /* ObjectAssign */) {
1131
- const options = serializeObject(ctx, id, value[2]);
1132
- serialized = `Object.assign(${serialized},${options})`;
1133
- } else {
1134
- markRef(ctx, id);
1135
- const assignments = serializeAssignments(ctx, id, value[2]);
1136
- if (assignments) {
1137
- const ref = getRefParam(ctx, id);
1138
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1139
- }
1140
- }
966
+ }
967
+ return assignRef(ctx, id, serialized);
968
+ }
969
+ function serializeError(ctx, constructor, message, options, id) {
970
+ let serialized = `new ${constructor}(${quote(message)})`;
971
+ if (options) {
972
+ if (ctx.features & 64 /* ObjectAssign */) {
973
+ serialized = serializeWithObjectAssign(ctx, options, id, serialized);
974
+ } else {
975
+ markRef(ctx, id);
976
+ const assignments = serializeAssignments(ctx, id, options);
977
+ if (assignments) {
978
+ const ref = getRefParam(ctx, id);
979
+ return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1141
980
  }
1142
- return assignRef(ctx, id, serialized);
1143
981
  }
1144
- case 13 /* Iterable */: {
1145
- const parent = ctx.stack;
1146
- ctx.stack = [];
1147
- const values = serializeTree(ctx, value[1]);
1148
- ctx.stack = parent;
1149
- let serialized;
1150
- if (ctx.features & 2 /* ArrayPrototypeValues */) {
1151
- serialized = `${values}.values()`;
1152
- } else {
1153
- serialized = `${values}[Symbol.iterator]()`;
1154
- }
1155
- if (ctx.features & 4 /* ArrowFunction */) {
1156
- serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1157
- } else if (ctx.features & 32 /* MethodShorthand */) {
1158
- serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1159
- } else {
1160
- serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1161
- }
1162
- if (value[0]) {
1163
- if (ctx.features & 64 /* ObjectAssign */) {
1164
- const options = serializeObject(ctx, id, value[0]);
1165
- serialized = `Object.assign(${serialized},${options})`;
1166
- } else {
1167
- markRef(ctx, id);
1168
- const assignments = serializeAssignments(ctx, id, value[0]);
1169
- if (assignments) {
1170
- const ref = getRefParam(ctx, id);
1171
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1172
- }
1173
- }
982
+ }
983
+ return assignRef(ctx, id, serialized);
984
+ }
985
+ function serializeIterable(ctx, options, items, id) {
986
+ const parent = ctx.stack;
987
+ ctx.stack = [];
988
+ const values = serializeNodeList(ctx, items, id);
989
+ ctx.stack = parent;
990
+ let serialized;
991
+ if (ctx.features & 2 /* ArrayPrototypeValues */) {
992
+ serialized = `${values}.values()`;
993
+ } else {
994
+ serialized = `${values}[Symbol.iterator]()`;
995
+ }
996
+ if (ctx.features & 4 /* ArrowFunction */) {
997
+ serialized = `{[Symbol.iterator]:()=>${serialized}}`;
998
+ } else if (ctx.features & 32 /* MethodShorthand */) {
999
+ serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1000
+ } else {
1001
+ serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1002
+ }
1003
+ if (options) {
1004
+ if (ctx.features & 64 /* ObjectAssign */) {
1005
+ serialized = serializeWithObjectAssign(ctx, options, id, serialized);
1006
+ } else {
1007
+ markRef(ctx, id);
1008
+ const assignments = serializeAssignments(ctx, id, options);
1009
+ if (assignments) {
1010
+ const ref = getRefParam(ctx, id);
1011
+ return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1174
1012
  }
1175
- return assignRef(ctx, id, serialized);
1176
1013
  }
1177
- case 9 /* NullConstructor */: {
1178
- let serialized = "Object.create(null)";
1179
- if (ctx.features & 64 /* ObjectAssign */) {
1180
- const fields = serializeObject(ctx, id, value);
1181
- if (fields !== "{}") {
1182
- serialized = `Object.assign(${serialized},${fields})`;
1183
- }
1184
- } else {
1185
- markRef(ctx, id);
1186
- const assignments = serializeAssignments(ctx, id, value);
1187
- if (assignments) {
1188
- const ref = getRefParam(ctx, id);
1189
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1190
- }
1191
- }
1192
- return assignRef(ctx, id, serialized);
1014
+ }
1015
+ return assignRef(ctx, id, serialized);
1016
+ }
1017
+ function serializeNullConstructor(ctx, value, id) {
1018
+ let serialized = "Object.create(null)";
1019
+ if (ctx.features & 64 /* ObjectAssign */) {
1020
+ serialized = serializeWithObjectAssign(ctx, value, id, serialized);
1021
+ } else {
1022
+ markRef(ctx, id);
1023
+ const assignments = serializeAssignments(ctx, id, value);
1024
+ if (assignments) {
1025
+ const ref = getRefParam(ctx, id);
1026
+ return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1193
1027
  }
1028
+ }
1029
+ return assignRef(ctx, id, serialized);
1030
+ }
1031
+ function serializeTree(ctx, [type, value, id]) {
1032
+ switch (type) {
1033
+ case 0 /* Primitive */:
1034
+ return String(value);
1035
+ case 2 /* Reference */:
1036
+ return getRefParam(ctx, value);
1037
+ case 10 /* Promise */:
1038
+ return serializePromise(ctx, value, id);
1039
+ case 1 /* BigInt */:
1040
+ return value;
1041
+ case 3 /* Date */:
1042
+ return assignRef(ctx, id, `new Date("${value}")`);
1043
+ case 4 /* RegExp */:
1044
+ return assignRef(ctx, id, value);
1045
+ case 15 /* BigIntTypedArray */:
1046
+ case 14 /* TypedArray */:
1047
+ return serializeTypedArray(ctx, value[0], value[1], value[2], id);
1048
+ case 5 /* Set */:
1049
+ return serializeSet(ctx, value, id);
1050
+ case 6 /* Map */:
1051
+ return serializeMap(ctx, value[0], value[1], value[2], id);
1052
+ case 7 /* Array */:
1053
+ return serializeArray(ctx, value, id);
1054
+ case 12 /* AggregateError */:
1055
+ return serializeAggregateError(ctx, value[0], value[1], value[2], id);
1056
+ case 11 /* Error */:
1057
+ return serializeError(ctx, value[0], value[1], value[2], id);
1058
+ case 13 /* Iterable */:
1059
+ return serializeIterable(ctx, value[0], value[1], id);
1060
+ case 9 /* NullConstructor */:
1061
+ return serializeNullConstructor(ctx, value, id);
1194
1062
  case 8 /* Object */:
1195
1063
  return assignRef(ctx, id, serializeObject(ctx, id, value));
1196
1064
  default:
@@ -1198,6 +1066,170 @@ function serializeTree(ctx, [type, value, id]) {
1198
1066
  }
1199
1067
  }
1200
1068
 
1069
+ // src/tree/sync.ts
1070
+ function serializePropertiesSync(ctx, properties) {
1071
+ const keys = Object.keys(properties);
1072
+ const size = keys.length;
1073
+ const keyNodes = new Array(size);
1074
+ const valueNodes = new Array(size);
1075
+ const deferredKeys = new Array(size);
1076
+ const deferredValues = new Array(size);
1077
+ let deferredSize = 0;
1078
+ let nodesSize = 0;
1079
+ for (const key of keys) {
1080
+ if (isIterable(properties[key])) {
1081
+ deferredKeys[deferredSize] = key;
1082
+ deferredValues[deferredSize] = properties[key];
1083
+ deferredSize++;
1084
+ } else {
1085
+ keyNodes[nodesSize] = key;
1086
+ valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
1087
+ nodesSize++;
1088
+ }
1089
+ }
1090
+ for (let i = 0; i < deferredSize; i++) {
1091
+ keyNodes[nodesSize + i] = deferredKeys[i];
1092
+ valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
1093
+ }
1094
+ return [keyNodes, valueNodes, size];
1095
+ }
1096
+ function generateSetNode2(ctx, current, id) {
1097
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
1098
+ const len = current.size;
1099
+ const nodes = new Array(len);
1100
+ const deferred = new Array(len);
1101
+ let deferredSize = 0;
1102
+ let nodeSize = 0;
1103
+ for (const item of current.keys()) {
1104
+ if (isIterable(item)) {
1105
+ deferred[deferredSize++] = item;
1106
+ } else {
1107
+ nodes[nodeSize++] = generateTreeSync(ctx, item);
1108
+ }
1109
+ }
1110
+ for (let i = 0; i < deferredSize; i++) {
1111
+ nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
1112
+ }
1113
+ return [5 /* Set */, nodes, id];
1114
+ }
1115
+ function generateMapNode2(ctx, current, id) {
1116
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
1117
+ const len = current.size;
1118
+ const keyNodes = new Array(len);
1119
+ const valueNodes = new Array(len);
1120
+ const deferredKey = new Array(len);
1121
+ const deferredValue = new Array(len);
1122
+ let deferredSize = 0;
1123
+ let nodeSize = 0;
1124
+ for (const [key, value] of current.entries()) {
1125
+ if (isIterable(key) || isIterable(value)) {
1126
+ deferredKey[deferredSize] = key;
1127
+ deferredValue[deferredSize] = value;
1128
+ deferredSize++;
1129
+ } else {
1130
+ keyNodes[nodeSize] = generateTreeSync(ctx, key);
1131
+ valueNodes[nodeSize] = generateTreeSync(ctx, value);
1132
+ nodeSize++;
1133
+ }
1134
+ }
1135
+ for (let i = 0; i < deferredSize; i++) {
1136
+ keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
1137
+ valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
1138
+ }
1139
+ return [6 /* Map */, [keyNodes, valueNodes, len], id];
1140
+ }
1141
+ function generateNodeList2(ctx, current) {
1142
+ const size = current.length;
1143
+ const nodes = new Array(size);
1144
+ const deferred = new Array(size);
1145
+ for (let i = 0; i < size; i++) {
1146
+ if (i in current) {
1147
+ if (isIterable(current[i])) {
1148
+ deferred[i] = current[i];
1149
+ } else {
1150
+ nodes[i] = generateTreeSync(ctx, current[i]);
1151
+ }
1152
+ }
1153
+ }
1154
+ for (let i = 0; i < size; i++) {
1155
+ if (i in deferred) {
1156
+ nodes[i] = generateTreeSync(ctx, deferred[i]);
1157
+ }
1158
+ }
1159
+ return nodes;
1160
+ }
1161
+ function generateArrayNode2(ctx, current, id) {
1162
+ return [7 /* Array */, generateNodeList2(ctx, current), id];
1163
+ }
1164
+ function generateAggregateErrorNode2(ctx, current, id) {
1165
+ const options = getErrorOptions(current);
1166
+ const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
1167
+ const errorsNode = generateTreeSync(ctx, current.errors);
1168
+ return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
1169
+ }
1170
+ function generateErrorNode2(ctx, current, id) {
1171
+ const options = getErrorOptions(current);
1172
+ const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
1173
+ return [
1174
+ 11 /* Error */,
1175
+ [getErrorConstructor(current), current.message, optionsNode],
1176
+ id
1177
+ ];
1178
+ }
1179
+ function generateIterableNode2(ctx, current, id) {
1180
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
1181
+ const options = getIterableOptions(current);
1182
+ return [13 /* Iterable */, [
1183
+ // Parse options first before the items
1184
+ options ? serializePropertiesSync(ctx, options) : void 0,
1185
+ generateNodeList2(ctx, Array.from(current))
1186
+ ], id];
1187
+ }
1188
+ function generateTreeSync(ctx, current) {
1189
+ if (isPrimitive(current)) {
1190
+ return [0 /* Primitive */, serializePrimitive(current)];
1191
+ }
1192
+ const id = generateRef(ctx, current);
1193
+ if (Array.isArray(id)) {
1194
+ return id;
1195
+ }
1196
+ const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
1197
+ if (semiPrimitive) {
1198
+ return semiPrimitive;
1199
+ }
1200
+ if (constructorCheck(current, Set)) {
1201
+ return generateSetNode2(ctx, current, id);
1202
+ }
1203
+ if (constructorCheck(current, Map)) {
1204
+ return generateMapNode2(ctx, current, id);
1205
+ }
1206
+ if (Array.isArray(current)) {
1207
+ return generateArrayNode2(ctx, current, id);
1208
+ }
1209
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
1210
+ return generateAggregateErrorNode2(ctx, current, id);
1211
+ }
1212
+ if (current instanceof Error) {
1213
+ return generateErrorNode2(ctx, current, id);
1214
+ }
1215
+ if (isIterable(current)) {
1216
+ return generateIterableNode2(ctx, current, id);
1217
+ }
1218
+ const empty = current.constructor == null;
1219
+ if (current.constructor === Object || empty) {
1220
+ return [
1221
+ empty ? 9 /* NullConstructor */ : 8 /* Object */,
1222
+ serializePropertiesSync(ctx, current),
1223
+ id
1224
+ ];
1225
+ }
1226
+ throw new Error("Unsupported value");
1227
+ }
1228
+ function parseSync(ctx, current) {
1229
+ const result = generateTreeSync(ctx, current);
1230
+ return [result, createRef(ctx, current), result[0] === 8 /* Object */];
1231
+ }
1232
+
1201
1233
  // src/index.ts
1202
1234
  function finalize(ctx, rootID, isObject, result) {
1203
1235
  if (ctx.vars.length) {