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