seroval 0.4.0-alpha.13 → 0.4.0-alpha.14

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.
@@ -280,35 +280,27 @@ function getIdentifier(index) {
280
280
  var DEFAULT_OPTIONS = {
281
281
  target: "es2023"
282
282
  };
283
- var ParserContext = class {
284
- constructor(options = {}) {
285
- __publicField(this, "refs", /* @__PURE__ */ new Map());
286
- __publicField(this, "markedRefs", {});
287
- __publicField(this, "features");
288
- const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
289
- this.features = parseTargets(result.target);
290
- }
291
- };
292
- var SerializationContext = class {
293
- constructor(options) {
294
- __publicField(this, "stack", []);
295
- // Map tree refs to actual refs
296
- __publicField(this, "validRefs", []);
297
- __publicField(this, "refSize", 0);
298
- // Variables
299
- __publicField(this, "vars", []);
300
- // Array of assignments to be done (used for recursion)
301
- __publicField(this, "assignments", []);
302
- // Supported features
303
- __publicField(this, "features");
304
- // Refs that are...referenced
305
- __publicField(this, "markedRefs");
306
- this.features = options.features;
307
- this.markedRefs = options.markedRefs;
308
- }
309
- };
283
+ function createParserContext(options = {}) {
284
+ const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
285
+ return {
286
+ markedRefs: /* @__PURE__ */ new Set(),
287
+ refs: /* @__PURE__ */ new Map(),
288
+ features: parseTargets(result.target)
289
+ };
290
+ }
291
+ function createSerializationContext(options) {
292
+ return {
293
+ stack: [],
294
+ vars: [],
295
+ assignments: [],
296
+ validRefs: [],
297
+ refSize: 0,
298
+ features: options.features,
299
+ markedRefs: new Set(options.markedRefs)
300
+ };
301
+ }
310
302
  function markRef(ctx, current) {
311
- ctx.markedRefs[current] = 1;
303
+ ctx.markedRefs.add(current);
312
304
  }
313
305
  function createValidRef(ctx, index) {
314
306
  const current = ctx.validRefs[index];
@@ -390,198 +382,119 @@ function quote(str) {
390
382
  return `"${result}"`;
391
383
  }
392
384
 
393
- // src/tree/types.ts
394
- var SerovalObjectRecordNode = class {
395
- constructor(k, v, s) {
396
- __publicField(this, "k");
397
- __publicField(this, "v");
398
- __publicField(this, "s");
399
- this.k = k;
400
- this.v = v;
401
- this.s = s;
402
- }
403
- };
404
- var SerovalMapRecordNode = class {
405
- constructor(k, v, s) {
406
- __publicField(this, "k");
407
- __publicField(this, "v");
408
- __publicField(this, "s");
409
- this.k = k;
410
- this.v = v;
411
- this.s = s;
412
- }
413
- };
414
- var SerovalPrimitiveNode = class {
415
- constructor(current) {
416
- __publicField(this, "t", 0 /* Primitive */);
417
- __publicField(this, "s");
418
- this.s = current;
419
- }
420
- };
421
- var SerovalReferenceNode = class {
422
- constructor(id) {
423
- __publicField(this, "t", 2 /* Reference */);
424
- __publicField(this, "i");
425
- this.i = id;
426
- }
427
- };
428
- var SerovalBigIntNode = class {
429
- constructor(current) {
430
- __publicField(this, "t", 1 /* BigInt */);
431
- __publicField(this, "s");
432
- this.s = `${current}n`;
433
- }
434
- };
435
- var SerovalDateNode = class {
436
- constructor(id, current) {
437
- __publicField(this, "t", 3 /* Date */);
438
- __publicField(this, "i");
439
- __publicField(this, "s");
440
- this.i = id;
441
- this.s = current.toISOString();
442
- }
443
- };
444
- var SerovalRegExpNode = class {
445
- constructor(id, current) {
446
- __publicField(this, "t", 4 /* RegExp */);
447
- __publicField(this, "i");
448
- __publicField(this, "s");
449
- this.i = id;
450
- this.s = String(current);
451
- }
452
- };
453
- var SerovalTypedArrayNode = class {
454
- constructor(id, current) {
455
- __publicField(this, "t", 14 /* TypedArray */);
456
- __publicField(this, "i");
457
- __publicField(this, "s");
458
- __publicField(this, "l");
459
- __publicField(this, "c");
460
- this.i = id;
461
- this.s = current.toString();
462
- this.l = current.byteOffset;
463
- this.c = current.constructor.name;
464
- }
465
- };
466
- var SerovalBigIntTypedArrayNode = class {
467
- constructor(id, current) {
468
- __publicField(this, "t", 15 /* BigIntTypedArray */);
469
- __publicField(this, "i");
470
- __publicField(this, "s");
471
- __publicField(this, "l");
472
- __publicField(this, "c");
473
- let result = "";
474
- const cap = current.length - 1;
475
- for (let i = 0; i < cap; i++) {
476
- result += `${current[i]}n,`;
477
- }
478
- result += `"${current[cap]}"`;
479
- this.i = id;
480
- this.s = result;
481
- this.l = current.byteOffset;
482
- this.c = current.constructor.name;
483
- }
484
- };
485
- var SerovalSetNode = class {
486
- constructor(id, a) {
487
- __publicField(this, "t", 5 /* Set */);
488
- __publicField(this, "i");
489
- __publicField(this, "a");
490
- this.i = id;
491
- this.a = a;
492
- }
493
- };
494
- var SerovalMapNode = class {
495
- constructor(id, d) {
496
- __publicField(this, "t", 6 /* Map */);
497
- __publicField(this, "i");
498
- __publicField(this, "d");
499
- this.i = id;
500
- this.d = d;
501
- }
502
- };
503
- var SerovalArrayNode = class {
504
- constructor(id, a) {
505
- __publicField(this, "t", 7 /* Array */);
506
- __publicField(this, "i");
507
- __publicField(this, "a");
508
- this.i = id;
509
- this.a = a;
510
- }
511
- };
512
- var SerovalObjectNode = class {
513
- constructor(id, d) {
514
- __publicField(this, "t", 8 /* Object */);
515
- __publicField(this, "i");
516
- __publicField(this, "d");
517
- this.i = id;
518
- this.d = d;
519
- }
520
- };
521
- var SerovalNullConstructorNode = class {
522
- constructor(id, d) {
523
- __publicField(this, "t", 9 /* NullConstructor */);
524
- __publicField(this, "i");
525
- __publicField(this, "d");
526
- this.i = id;
527
- this.d = d;
528
- }
529
- };
530
- var SerovalPromiseNode = class {
531
- constructor(id, n) {
532
- __publicField(this, "t", 10 /* Promise */);
533
- __publicField(this, "i");
534
- __publicField(this, "n");
535
- this.i = id;
536
- this.n = n;
537
- }
538
- };
539
- var SerovalErrorNode = class {
540
- constructor(id, c, m, d) {
541
- __publicField(this, "t", 11 /* Error */);
542
- __publicField(this, "i");
543
- __publicField(this, "c");
544
- __publicField(this, "m");
545
- __publicField(this, "d");
546
- this.i = id;
547
- this.c = c;
548
- this.m = m;
549
- this.d = d;
550
- }
551
- };
552
- var SerovalAggregateErrorNode = class {
553
- constructor(id, m, d, n) {
554
- __publicField(this, "t", 12 /* AggregateError */);
555
- __publicField(this, "i");
556
- __publicField(this, "m");
557
- __publicField(this, "d");
558
- __publicField(this, "n");
559
- this.i = id;
560
- this.m = m;
561
- this.d = d;
562
- this.n = n;
563
- }
564
- };
565
- var SerovalIterableNode = class {
566
- constructor(id, d, a) {
567
- __publicField(this, "t", 13 /* Iterable */);
568
- __publicField(this, "i");
569
- __publicField(this, "d");
570
- __publicField(this, "a");
571
- this.i = id;
572
- this.d = d;
573
- this.a = a;
574
- }
575
- };
576
-
577
385
  // src/tree/primitives.ts
578
- var TRUE_NODE = new SerovalPrimitiveNode("!0");
579
- var FALSE_NODE = new SerovalPrimitiveNode("!1");
580
- var UNDEFINED_NODE = new SerovalPrimitiveNode("void 0");
581
- var NULL_NODE = new SerovalPrimitiveNode(null);
582
- var NEG_ZERO_NODE = new SerovalPrimitiveNode("-0");
583
- var INFINITY_NODE = new SerovalPrimitiveNode("1/0");
584
- var NEG_INFINITY_NODE = new SerovalPrimitiveNode("-1/0");
386
+ function createPrimitiveNode(value) {
387
+ return {
388
+ t: 0 /* Primitive */,
389
+ i: void 0,
390
+ s: value,
391
+ l: void 0,
392
+ c: void 0,
393
+ m: void 0,
394
+ d: void 0,
395
+ a: void 0,
396
+ n: void 0
397
+ };
398
+ }
399
+ var TRUE_NODE = createPrimitiveNode("!0");
400
+ var FALSE_NODE = createPrimitiveNode("!1");
401
+ var UNDEFINED_NODE = createPrimitiveNode("void 0");
402
+ var NULL_NODE = createPrimitiveNode(null);
403
+ var NEG_ZERO_NODE = createPrimitiveNode("-0");
404
+ var INFINITY_NODE = createPrimitiveNode("1/0");
405
+ var NEG_INFINITY_NODE = createPrimitiveNode("-1/0");
406
+ function createBigIntNode(ctx, current) {
407
+ assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
408
+ return {
409
+ t: 1 /* BigInt */,
410
+ i: void 0,
411
+ s: `${current}n`,
412
+ l: void 0,
413
+ c: void 0,
414
+ m: void 0,
415
+ d: void 0,
416
+ a: void 0,
417
+ n: void 0
418
+ };
419
+ }
420
+ function createReferenceNode(id) {
421
+ return {
422
+ t: 2 /* Reference */,
423
+ i: id,
424
+ s: void 0,
425
+ l: void 0,
426
+ c: void 0,
427
+ m: void 0,
428
+ d: void 0,
429
+ a: void 0,
430
+ n: void 0
431
+ };
432
+ }
433
+ function createDateNode(id, current) {
434
+ return {
435
+ t: 3 /* Date */,
436
+ i: id,
437
+ s: current.toISOString(),
438
+ l: void 0,
439
+ c: void 0,
440
+ m: void 0,
441
+ d: void 0,
442
+ n: void 0,
443
+ a: void 0
444
+ };
445
+ }
446
+ function createRegExpNode(id, current) {
447
+ return {
448
+ t: 4 /* RegExp */,
449
+ i: id,
450
+ s: String(current),
451
+ l: void 0,
452
+ c: void 0,
453
+ m: void 0,
454
+ d: void 0,
455
+ a: void 0,
456
+ n: void 0
457
+ };
458
+ }
459
+ function createTypedArrayNode(ctx, id, current) {
460
+ const constructor = current.constructor.name;
461
+ assert(ctx.features & 1024 /* TypedArray */, `Unsupported value type "${constructor}"`);
462
+ return {
463
+ t: 14 /* TypedArray */,
464
+ i: id,
465
+ s: current.toString(),
466
+ l: current.byteOffset,
467
+ c: constructor,
468
+ m: void 0,
469
+ d: void 0,
470
+ a: void 0,
471
+ n: void 0
472
+ };
473
+ }
474
+ function createBigIntTypedArrayNode(ctx, id, current) {
475
+ const constructor = current.constructor.name;
476
+ assert(
477
+ ctx.features & 2048 /* BigIntTypedArray */,
478
+ `Unsupported value type "${constructor}"`
479
+ );
480
+ let result = "";
481
+ const cap = current.length - 1;
482
+ for (let i = 0; i < cap; i++) {
483
+ result += `${current[i]}n,`;
484
+ }
485
+ result += `"${current[cap]}"`;
486
+ return {
487
+ t: 15 /* BigIntTypedArray */,
488
+ i: id,
489
+ s: result,
490
+ l: current.byteOffset,
491
+ c: constructor,
492
+ m: void 0,
493
+ d: void 0,
494
+ a: void 0,
495
+ n: void 0
496
+ };
497
+ }
585
498
 
586
499
  // src/tree/shared.ts
587
500
  function getErrorConstructor(error) {
@@ -633,9 +546,6 @@ function getIterableOptions(obj) {
633
546
  }
634
547
  return void 0;
635
548
  }
636
- function isReferenceInStack(ctx, node) {
637
- return node.t === 2 /* Reference */ && ctx.stack.includes(node.i);
638
- }
639
549
  function isIterable(value) {
640
550
  if (!value || typeof value !== "object") {
641
551
  return false;
@@ -693,7 +603,17 @@ var AsyncParser = class {
693
603
  return nodes;
694
604
  }
695
605
  async generateArrayNode(id, current) {
696
- return new SerovalArrayNode(id, await this.generateNodeList(current));
606
+ return {
607
+ t: 7 /* Array */,
608
+ i: id,
609
+ s: void 0,
610
+ l: void 0,
611
+ c: void 0,
612
+ m: void 0,
613
+ d: void 0,
614
+ a: await this.generateNodeList(current),
615
+ n: void 0
616
+ };
697
617
  }
698
618
  async generateMapNode(id, current) {
699
619
  assert(this.ctx.features & 16 /* Map */, 'Unsupported type "Map"');
@@ -719,7 +639,17 @@ var AsyncParser = class {
719
639
  keyNodes[nodeSize + i] = await this.parse(deferredKey[i]);
720
640
  valueNodes[nodeSize + i] = await this.parse(deferredValue[i]);
721
641
  }
722
- return new SerovalMapNode(id, new SerovalMapRecordNode(keyNodes, valueNodes, len));
642
+ return {
643
+ t: 6 /* Map */,
644
+ i: id,
645
+ s: void 0,
646
+ l: void 0,
647
+ c: void 0,
648
+ m: void 0,
649
+ d: { k: keyNodes, v: valueNodes, s: len },
650
+ a: void 0,
651
+ n: void 0
652
+ };
723
653
  }
724
654
  async generateSetNode(id, current) {
725
655
  assert(this.ctx.features & 256 /* Set */, 'Unsupported type "Set"');
@@ -738,7 +668,17 @@ var AsyncParser = class {
738
668
  for (let i = 0; i < deferredSize; i++) {
739
669
  nodes[nodeSize + i] = await this.parse(deferred[i]);
740
670
  }
741
- return new SerovalSetNode(id, nodes);
671
+ return {
672
+ t: 5 /* Set */,
673
+ i: id,
674
+ s: void 0,
675
+ l: void 0,
676
+ c: void 0,
677
+ m: void 0,
678
+ d: void 0,
679
+ a: nodes,
680
+ n: void 0
681
+ };
742
682
  }
743
683
  async generateProperties(properties) {
744
684
  const keys = Object.keys(properties);
@@ -766,21 +706,42 @@ var AsyncParser = class {
766
706
  keyNodes[nodesSize + i] = deferredKeys[i];
767
707
  valueNodes[nodesSize + i] = await this.parse(deferredValues[i]);
768
708
  }
769
- return new SerovalObjectRecordNode(keyNodes, valueNodes, size);
709
+ return {
710
+ k: keyNodes,
711
+ v: valueNodes,
712
+ s: size
713
+ };
770
714
  }
771
715
  async generateIterableNode(id, current) {
772
716
  assert(this.ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
773
717
  const options = getIterableOptions(current);
774
- return new SerovalIterableNode(
775
- id,
718
+ return {
719
+ t: 13 /* Iterable */,
720
+ i: id,
721
+ s: void 0,
722
+ l: void 0,
723
+ c: void 0,
724
+ m: void 0,
776
725
  // Parse options first before the items
777
- options ? await this.generateProperties(options) : void 0,
778
- await this.generateNodeList(Array.from(current))
779
- );
726
+ d: options ? await this.generateProperties(options) : void 0,
727
+ a: await this.generateNodeList(Array.from(current)),
728
+ n: void 0
729
+ };
780
730
  }
781
731
  async generatePromiseNode(id, current) {
782
732
  assert(this.ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
783
- return current.then(async (value) => new SerovalPromiseNode(id, await this.parse(value)));
733
+ return current.then(async (value) => ({
734
+ t: 10 /* Promise */,
735
+ i: id,
736
+ s: void 0,
737
+ l: void 0,
738
+ c: void 0,
739
+ m: void 0,
740
+ // Parse options first before the items
741
+ d: void 0,
742
+ a: void 0,
743
+ n: await this.parse(value)
744
+ }));
784
745
  }
785
746
  async generateObjectNode(id, current, empty) {
786
747
  if (Symbol.iterator in current) {
@@ -789,26 +750,47 @@ var AsyncParser = class {
789
750
  if ("then" in current && typeof current.then === "function") {
790
751
  return this.generatePromiseNode(id, current);
791
752
  }
792
- const properties = await this.generateProperties(current);
793
- if (empty) {
794
- return new SerovalNullConstructorNode(id, properties);
795
- }
796
- return new SerovalObjectNode(id, properties);
753
+ return {
754
+ t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
755
+ i: id,
756
+ s: void 0,
757
+ l: void 0,
758
+ c: void 0,
759
+ m: void 0,
760
+ d: await this.generateProperties(current),
761
+ a: void 0,
762
+ n: void 0
763
+ };
797
764
  }
798
765
  async generateAggregateErrorNode(id, current) {
799
766
  const options = getErrorOptions(current);
800
767
  const optionsNode = options ? await this.generateProperties(options) : void 0;
801
- return new SerovalAggregateErrorNode(
802
- id,
803
- current.message,
804
- optionsNode,
805
- await this.parse(current.errors)
806
- );
768
+ return {
769
+ t: 12 /* AggregateError */,
770
+ i: id,
771
+ s: void 0,
772
+ l: void 0,
773
+ c: void 0,
774
+ m: current.message,
775
+ d: optionsNode,
776
+ a: void 0,
777
+ n: await this.parse(current.errors)
778
+ };
807
779
  }
808
780
  async generateErrorNode(id, current) {
809
781
  const options = getErrorOptions(current);
810
782
  const optionsNode = options ? await this.generateProperties(options) : void 0;
811
- return new SerovalErrorNode(id, getErrorConstructor(current), current.message, optionsNode);
783
+ return {
784
+ t: 11 /* Error */,
785
+ i: id,
786
+ s: void 0,
787
+ l: void 0,
788
+ c: getErrorConstructor(current),
789
+ m: current.message,
790
+ d: optionsNode,
791
+ a: void 0,
792
+ n: void 0
793
+ };
812
794
  }
813
795
  async parse(current) {
814
796
  switch (typeof current) {
@@ -817,37 +799,36 @@ var AsyncParser = class {
817
799
  case "undefined":
818
800
  return UNDEFINED_NODE;
819
801
  case "string":
820
- return new SerovalPrimitiveNode(quote(current));
802
+ return createPrimitiveNode(quote(current));
821
803
  case "number":
822
804
  if (Object.is(current, -0)) {
823
805
  return NEG_ZERO_NODE;
824
806
  }
825
807
  if (Object.is(current, Infinity)) {
826
- return INFINITY_NODE;
808
+ return createPrimitiveNode("1/0");
827
809
  }
828
810
  if (Object.is(current, -Infinity)) {
829
- return NEG_INFINITY_NODE;
811
+ return createPrimitiveNode("-1/0");
830
812
  }
831
- return new SerovalPrimitiveNode(current);
813
+ return createPrimitiveNode(current);
832
814
  case "bigint":
833
- assert(this.ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
834
- return new SerovalBigIntNode(current);
815
+ return createBigIntNode(this.ctx, current);
835
816
  case "object": {
836
817
  if (!current) {
837
818
  return NULL_NODE;
838
819
  }
839
820
  const id = createRef(this.ctx, current, true);
840
- if (this.ctx.markedRefs[id]) {
841
- return new SerovalReferenceNode(id);
821
+ if (this.ctx.markedRefs.has(id)) {
822
+ return createReferenceNode(id);
842
823
  }
843
824
  if (Array.isArray(current)) {
844
825
  return this.generateArrayNode(id, current);
845
826
  }
846
827
  switch (current.constructor) {
847
828
  case Date:
848
- return new SerovalDateNode(id, current);
829
+ return createDateNode(id, current);
849
830
  case RegExp:
850
- return new SerovalRegExpNode(id, current);
831
+ return createRegExpNode(id, current);
851
832
  case Promise:
852
833
  return this.generatePromiseNode(id, current);
853
834
  case Int8Array:
@@ -859,15 +840,10 @@ var AsyncParser = class {
859
840
  case Uint8ClampedArray:
860
841
  case Float32Array:
861
842
  case Float64Array:
862
- assert(this.ctx.features & 1024 /* TypedArray */, `Unsupported value type "${current.constructor.name}"`);
863
- return new SerovalTypedArrayNode(id, current);
843
+ return createTypedArrayNode(this.ctx, id, current);
864
844
  case BigInt64Array:
865
845
  case BigUint64Array:
866
- assert(
867
- this.ctx.features & 2048 /* BigIntTypedArray */,
868
- `Unsupported value type "${current.constructor.name}"`
869
- );
870
- return new SerovalBigIntTypedArrayNode(id, current);
846
+ return createBigIntTypedArrayNode(this.ctx, id, current);
871
847
  case Map:
872
848
  return this.generateMapNode(id, current);
873
849
  case Set:
@@ -922,13 +898,13 @@ async function parseAsync(ctx, current) {
922
898
 
923
899
  // src/tree/serialize.ts
924
900
  function getAssignmentExpression(assignment) {
925
- switch (assignment.type) {
901
+ switch (assignment.t) {
926
902
  case "index":
927
- return `${assignment.source}=${assignment.value}`;
903
+ return `${assignment.s}=${assignment.v}`;
928
904
  case "map":
929
- return `${assignment.source}.set(${assignment.key},${assignment.value})`;
905
+ return `${assignment.s}.set(${assignment.k},${assignment.v})`;
930
906
  case "set":
931
- return `${assignment.source}.add(${assignment.value})`;
907
+ return `${assignment.s}.add(${assignment.v})`;
932
908
  default:
933
909
  return "";
934
910
  }
@@ -939,25 +915,27 @@ function mergeAssignments(assignments) {
939
915
  let prev = current;
940
916
  for (let i = 1, len = assignments.length; i < len; i++) {
941
917
  const item = assignments[i];
942
- if (item.type === prev.type) {
943
- if (item.type === "index" && item.value === prev.value) {
918
+ if (item.t === prev.t) {
919
+ if (item.t === "index" && item.v === prev.v) {
944
920
  current = {
945
- type: "index",
946
- source: item.source,
947
- value: getAssignmentExpression(current)
921
+ t: "index",
922
+ s: item.s,
923
+ k: void 0,
924
+ v: getAssignmentExpression(current)
948
925
  };
949
- } else if (item.type === "map" && item.source === prev.source) {
926
+ } else if (item.t === "map" && item.s === prev.s) {
950
927
  current = {
951
- type: "map",
952
- source: getAssignmentExpression(current),
953
- key: item.key,
954
- value: item.value
928
+ t: "map",
929
+ s: getAssignmentExpression(current),
930
+ k: item.k,
931
+ v: item.v
955
932
  };
956
- } else if (item.type === "set" && item.source === prev.source) {
933
+ } else if (item.t === "set" && item.s === prev.s) {
957
934
  current = {
958
- type: "set",
959
- source: getAssignmentExpression(current),
960
- value: item.value
935
+ t: "set",
936
+ s: getAssignmentExpression(current),
937
+ k: void 0,
938
+ v: item.v
961
939
  };
962
940
  } else {
963
941
  newAssignments.push(current);
@@ -986,34 +964,30 @@ function resolveAssignments(assignments) {
986
964
  function resolvePatches(ctx) {
987
965
  return resolveAssignments(ctx.assignments);
988
966
  }
989
- function assignRef(ctx, index, value) {
990
- if (ctx.markedRefs[index]) {
991
- return `${getRefParam(ctx, index)}=${value}`;
992
- }
993
- return value;
994
- }
995
967
  function createAssignment(ctx, source, value) {
996
968
  ctx.assignments.push({
997
- type: "index",
998
- source,
999
- value
969
+ t: "index",
970
+ s: source,
971
+ k: void 0,
972
+ v: value
1000
973
  });
1001
974
  }
1002
975
  function createSetAdd(ctx, ref, value) {
1003
976
  markRef(ctx, ref);
1004
977
  ctx.assignments.push({
1005
- type: "set",
1006
- source: getRefParam(ctx, ref),
1007
- value
978
+ t: "set",
979
+ s: getRefParam(ctx, ref),
980
+ k: void 0,
981
+ v: value
1008
982
  });
1009
983
  }
1010
984
  function createMapSet(ctx, ref, key, value) {
1011
985
  markRef(ctx, ref);
1012
986
  ctx.assignments.push({
1013
- type: "map",
1014
- source: getRefParam(ctx, ref),
1015
- key,
1016
- value
987
+ t: "map",
988
+ s: getRefParam(ctx, ref),
989
+ k: key,
990
+ v: value
1017
991
  });
1018
992
  }
1019
993
  function createArrayAssign(ctx, ref, index, value) {
@@ -1025,281 +999,296 @@ function createObjectAssign(ctx, ref, key, value) {
1025
999
  createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
1026
1000
  }
1027
1001
  var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
1028
- function serializeAssignments(ctx, targetRef, node) {
1029
- ctx.stack.push(targetRef);
1030
- const mainAssignments = [];
1031
- for (let i = 0; i < node.s; i++) {
1032
- const parentStack = ctx.stack;
1033
- ctx.stack = [];
1034
- const refParam = serializeTree(ctx, node.v[i]);
1035
- ctx.stack = parentStack;
1036
- const key = node.k[i];
1037
- const check = Number(key);
1038
- const parentAssignment = ctx.assignments;
1039
- ctx.assignments = mainAssignments;
1040
- const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1041
- if (isIdentifier && Number.isNaN(check)) {
1042
- createObjectAssign(ctx, targetRef, key, refParam);
1043
- } else {
1044
- createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
1045
- }
1046
- ctx.assignments = parentAssignment;
1047
- }
1048
- ctx.stack.pop();
1049
- return resolveAssignments(mainAssignments);
1050
- }
1051
- function serializeObject(ctx, sourceID, node) {
1052
- if (node.s === 0) {
1053
- return "{}";
1002
+ var SerovalSerializer = class {
1003
+ constructor(ctx) {
1004
+ __publicField(this, "ctx");
1005
+ this.ctx = ctx;
1054
1006
  }
1055
- let result = "";
1056
- ctx.stack.push(sourceID);
1057
- for (let i = 0; i < node.s; i++) {
1058
- const key = node.k[i];
1059
- const val = node.v[i];
1060
- const check = Number(key);
1061
- const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1062
- if (isReferenceInStack(ctx, val)) {
1063
- const refParam = getRefParam(ctx, val.i);
1064
- if (isIdentifier && Number.isNaN(check)) {
1065
- createObjectAssign(ctx, sourceID, key, refParam);
1066
- } else {
1067
- createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1068
- }
1069
- } else {
1070
- result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
1007
+ assignRef(index, value) {
1008
+ if (this.ctx.markedRefs.has(index)) {
1009
+ return `${getRefParam(this.ctx, index)}=${value}`;
1071
1010
  }
1011
+ return value;
1072
1012
  }
1073
- ctx.stack.pop();
1074
- return `{${result.substring(0, result.length - 1)}}`;
1075
- }
1076
- function serializePromise(ctx, node) {
1077
- let serialized;
1078
- if (isReferenceInStack(ctx, node.n)) {
1079
- serialized = `Promise.resolve().then(()=>${getRefParam(ctx, node.n.i)})`;
1080
- } else {
1081
- ctx.stack.push(node.i);
1082
- const result = serializeTree(ctx, node.n);
1083
- ctx.stack.pop();
1084
- serialized = `Promise.resolve(${result})`;
1013
+ isReferenceInStack(node) {
1014
+ return node.t === 2 /* Reference */ && this.ctx.stack.includes(node.i);
1085
1015
  }
1086
- return assignRef(ctx, node.i, serialized);
1087
- }
1088
- function serializeTypedArray(ctx, node) {
1089
- let args = `[${node.s}]`;
1090
- if (node.l !== 0) {
1091
- args += `,${node.l}`;
1092
- }
1093
- return assignRef(ctx, node.i, `new ${node.c}(${args})`);
1094
- }
1095
- function serializeSet(ctx, node) {
1096
- let serialized = "new Set";
1097
- const size = node.a.length;
1098
- if (size) {
1099
- let result = "";
1100
- ctx.stack.push(node.i);
1016
+ serializeNodeList(node) {
1017
+ const size = node.a.length;
1018
+ let values = "";
1101
1019
  for (let i = 0; i < size; i++) {
1102
1020
  const item = node.a[i];
1103
- if (isReferenceInStack(ctx, item)) {
1104
- createSetAdd(ctx, node.i, getRefParam(ctx, item.i));
1021
+ if (item) {
1022
+ if (this.isReferenceInStack(item)) {
1023
+ createArrayAssign(this.ctx, node.i, i, getRefParam(this.ctx, item.i));
1024
+ values += ",";
1025
+ } else {
1026
+ values += this.serialize(item);
1027
+ if (i < size - 1) {
1028
+ values += ",";
1029
+ }
1030
+ }
1105
1031
  } else {
1106
- result += `${serializeTree(ctx, item)},`;
1032
+ values += ",";
1107
1033
  }
1108
1034
  }
1109
- ctx.stack.pop();
1110
- if (result) {
1111
- serialized += `([${result.substring(0, result.length - 1)}])`;
1112
- }
1035
+ return `[${values}]`;
1113
1036
  }
1114
- return assignRef(ctx, node.i, serialized);
1115
- }
1116
- function serializeMap(ctx, node) {
1117
- let serialized = "new Map";
1118
- if (node.d.s) {
1037
+ serializeArray(node) {
1038
+ this.ctx.stack.push(node.i);
1039
+ const result = this.serializeNodeList(node);
1040
+ this.ctx.stack.pop();
1041
+ return this.assignRef(node.i, result);
1042
+ }
1043
+ serializeObject(sourceID, node) {
1044
+ if (node.s === 0) {
1045
+ return "{}";
1046
+ }
1119
1047
  let result = "";
1120
- ctx.stack.push(node.i);
1121
- for (let i = 0; i < node.d.s; i++) {
1122
- const key = node.d.k[i];
1123
- const val = node.d.v[i];
1124
- if (isReferenceInStack(ctx, key)) {
1125
- const keyRef = getRefParam(ctx, key.i);
1126
- if (isReferenceInStack(ctx, val)) {
1127
- const valueRef = getRefParam(ctx, val.i);
1128
- createMapSet(ctx, node.i, keyRef, valueRef);
1048
+ this.ctx.stack.push(sourceID);
1049
+ for (let i = 0; i < node.s; i++) {
1050
+ const key = node.k[i];
1051
+ const val = node.v[i];
1052
+ const check = Number(key);
1053
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1054
+ if (this.isReferenceInStack(val)) {
1055
+ const refParam = getRefParam(this.ctx, val.i);
1056
+ if (isIdentifier && Number.isNaN(check)) {
1057
+ createObjectAssign(this.ctx, sourceID, key, refParam);
1129
1058
  } else {
1130
- const parent = ctx.stack;
1131
- ctx.stack = [];
1132
- createMapSet(ctx, node.i, keyRef, serializeTree(ctx, val));
1133
- ctx.stack = parent;
1059
+ createArrayAssign(this.ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1134
1060
  }
1135
- } else if (isReferenceInStack(ctx, val)) {
1136
- const valueRef = getRefParam(ctx, val.i);
1137
- const parent = ctx.stack;
1138
- ctx.stack = [];
1139
- createMapSet(ctx, node.i, serializeTree(ctx, key), valueRef);
1140
- ctx.stack = parent;
1141
1061
  } else {
1142
- result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
1062
+ result += `${isIdentifier ? key : quote(key)}:${this.serialize(val)},`;
1143
1063
  }
1144
1064
  }
1145
- ctx.stack.pop();
1146
- if (result) {
1147
- serialized += `([${result.substring(0, result.length - 1)}])`;
1148
- }
1065
+ this.ctx.stack.pop();
1066
+ return `{${result.substring(0, result.length - 1)}}`;
1149
1067
  }
1150
- return assignRef(ctx, node.i, serialized);
1151
- }
1152
- function serializeNodeList(ctx, node) {
1153
- const size = node.a.length;
1154
- let values = "";
1155
- for (let i = 0; i < size; i++) {
1156
- const item = node.a[i];
1157
- if (item) {
1158
- if (isReferenceInStack(ctx, item)) {
1159
- createArrayAssign(ctx, node.i, i, getRefParam(ctx, item.i));
1160
- values += ",";
1068
+ serializeWithObjectAssign(value, id, serialized) {
1069
+ const fields = this.serializeObject(id, value);
1070
+ if (fields !== "{}") {
1071
+ return `Object.assign(${serialized},${fields})`;
1072
+ }
1073
+ return serialized;
1074
+ }
1075
+ serializeAssignments(sourceID, node) {
1076
+ this.ctx.stack.push(sourceID);
1077
+ const mainAssignments = [];
1078
+ for (let i = 0; i < node.s; i++) {
1079
+ const parentStack = this.ctx.stack;
1080
+ this.ctx.stack = [];
1081
+ const refParam = this.serialize(node.v[i]);
1082
+ this.ctx.stack = parentStack;
1083
+ const key = node.k[i];
1084
+ const check = Number(key);
1085
+ const parentAssignment = this.ctx.assignments;
1086
+ this.ctx.assignments = mainAssignments;
1087
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1088
+ if (isIdentifier && Number.isNaN(check)) {
1089
+ createObjectAssign(this.ctx, sourceID, key, refParam);
1161
1090
  } else {
1162
- values += serializeTree(ctx, item);
1163
- if (i < size - 1) {
1164
- values += ",";
1165
- }
1091
+ createArrayAssign(this.ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1166
1092
  }
1167
- } else {
1168
- values += ",";
1093
+ this.ctx.assignments = parentAssignment;
1169
1094
  }
1095
+ this.ctx.stack.pop();
1096
+ return resolveAssignments(mainAssignments);
1170
1097
  }
1171
- return `[${values}]`;
1172
- }
1173
- function serializeArray(ctx, node) {
1174
- ctx.stack.push(node.i);
1175
- const result = serializeNodeList(ctx, node);
1176
- ctx.stack.pop();
1177
- return assignRef(ctx, node.i, result);
1178
- }
1179
- function serializeWithObjectAssign(ctx, value, id, serialized) {
1180
- const fields = serializeObject(ctx, id, value);
1181
- if (fields !== "{}") {
1182
- return `Object.assign(${serialized},${fields})`;
1183
- }
1184
- return serialized;
1185
- }
1186
- function serializeAggregateError(ctx, node) {
1187
- ctx.stack.push(node.i);
1188
- let serialized = `new AggregateError(${serializeTree(ctx, node.n)},${quote(node.m)})`;
1189
- ctx.stack.pop();
1190
- if (node.d) {
1191
- if (ctx.features & 64 /* ObjectAssign */) {
1192
- serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1098
+ serializeNullConstructor(node) {
1099
+ let serialized = "Object.create(null)";
1100
+ if (this.ctx.features & 64 /* ObjectAssign */) {
1101
+ serialized = this.serializeWithObjectAssign(node.d, node.i, serialized);
1193
1102
  } else {
1194
- markRef(ctx, node.i);
1195
- const assignments = serializeAssignments(ctx, node.i, node.d);
1103
+ markRef(this.ctx, node.i);
1104
+ const assignments = this.serializeAssignments(node.i, node.d);
1196
1105
  if (assignments) {
1197
- const ref = getRefParam(ctx, node.i);
1198
- return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
1106
+ const ref = getRefParam(this.ctx, node.i);
1107
+ return `(${this.assignRef(node.i, serialized)},${assignments}${ref})`;
1199
1108
  }
1200
1109
  }
1201
- }
1202
- return assignRef(ctx, node.i, serialized);
1203
- }
1204
- function serializeError(ctx, node) {
1205
- let serialized = `new ${node.c}(${quote(node.m)})`;
1206
- if (node.d) {
1207
- if (ctx.features & 64 /* ObjectAssign */) {
1208
- serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1209
- } else {
1210
- markRef(ctx, node.i);
1211
- const assignments = serializeAssignments(ctx, node.i, node.d);
1212
- if (assignments) {
1213
- const ref = getRefParam(ctx, node.i);
1214
- return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
1110
+ return this.assignRef(node.i, serialized);
1111
+ }
1112
+ serializeSet(node) {
1113
+ let serialized = "new Set";
1114
+ const size = node.a.length;
1115
+ if (size) {
1116
+ let result = "";
1117
+ this.ctx.stack.push(node.i);
1118
+ for (let i = 0; i < size; i++) {
1119
+ const item = node.a[i];
1120
+ if (this.isReferenceInStack(item)) {
1121
+ createSetAdd(this.ctx, node.i, getRefParam(this.ctx, item.i));
1122
+ } else {
1123
+ result += `${this.serialize(item)},`;
1124
+ }
1125
+ }
1126
+ this.ctx.stack.pop();
1127
+ if (result) {
1128
+ serialized += `([${result.substring(0, result.length - 1)}])`;
1129
+ }
1130
+ }
1131
+ return this.assignRef(node.i, serialized);
1132
+ }
1133
+ serializeMap(node) {
1134
+ let serialized = "new Map";
1135
+ if (node.d.s) {
1136
+ let result = "";
1137
+ this.ctx.stack.push(node.i);
1138
+ for (let i = 0; i < node.d.s; i++) {
1139
+ const key = node.d.k[i];
1140
+ const val = node.d.v[i];
1141
+ if (this.isReferenceInStack(key)) {
1142
+ const keyRef = getRefParam(this.ctx, key.i);
1143
+ if (this.isReferenceInStack(val)) {
1144
+ const valueRef = getRefParam(this.ctx, val.i);
1145
+ createMapSet(this.ctx, node.i, keyRef, valueRef);
1146
+ } else {
1147
+ const parent = this.ctx.stack;
1148
+ this.ctx.stack = [];
1149
+ createMapSet(this.ctx, node.i, keyRef, this.serialize(val));
1150
+ this.ctx.stack = parent;
1151
+ }
1152
+ } else if (this.isReferenceInStack(val)) {
1153
+ const valueRef = getRefParam(this.ctx, val.i);
1154
+ const parent = this.ctx.stack;
1155
+ this.ctx.stack = [];
1156
+ createMapSet(this.ctx, node.i, this.serialize(key), valueRef);
1157
+ this.ctx.stack = parent;
1158
+ } else {
1159
+ result += `[${this.serialize(key)},${this.serialize(val)}],`;
1160
+ }
1161
+ }
1162
+ this.ctx.stack.pop();
1163
+ if (result) {
1164
+ serialized += `([${result.substring(0, result.length - 1)}])`;
1165
+ }
1166
+ }
1167
+ return this.assignRef(node.i, serialized);
1168
+ }
1169
+ serializeAggregateError(node) {
1170
+ this.ctx.stack.push(node.i);
1171
+ let serialized = `new AggregateError(${this.serialize(node.n)},${quote(node.m)})`;
1172
+ this.ctx.stack.pop();
1173
+ if (node.d) {
1174
+ if (this.ctx.features & 64 /* ObjectAssign */) {
1175
+ serialized = this.serializeWithObjectAssign(node.d, node.i, serialized);
1176
+ } else {
1177
+ markRef(this.ctx, node.i);
1178
+ const assignments = this.serializeAssignments(node.i, node.d);
1179
+ if (assignments) {
1180
+ const ref = getRefParam(this.ctx, node.i);
1181
+ return `(${this.assignRef(node.i, serialized)},${assignments}${ref})`;
1182
+ }
1215
1183
  }
1216
1184
  }
1185
+ return this.assignRef(node.i, serialized);
1217
1186
  }
1218
- return assignRef(ctx, node.i, serialized);
1219
- }
1220
- function serializeIterable(ctx, node) {
1221
- const parent = ctx.stack;
1222
- ctx.stack = [];
1223
- const values = serializeNodeList(ctx, node);
1224
- ctx.stack = parent;
1225
- let serialized;
1226
- if (ctx.features & 2 /* ArrayPrototypeValues */) {
1227
- serialized = `${values}.values()`;
1228
- } else {
1229
- serialized = `${values}[Symbol.iterator]()`;
1187
+ serializeError(node) {
1188
+ let serialized = `new ${node.c}(${quote(node.m)})`;
1189
+ if (node.d) {
1190
+ if (this.ctx.features & 64 /* ObjectAssign */) {
1191
+ serialized = this.serializeWithObjectAssign(node.d, node.i, serialized);
1192
+ } else {
1193
+ markRef(this.ctx, node.i);
1194
+ const assignments = this.serializeAssignments(node.i, node.d);
1195
+ if (assignments) {
1196
+ const ref = getRefParam(this.ctx, node.i);
1197
+ return `(${this.assignRef(node.i, serialized)},${assignments}${ref})`;
1198
+ }
1199
+ }
1200
+ }
1201
+ return this.assignRef(node.i, serialized);
1230
1202
  }
1231
- if (ctx.features & 4 /* ArrowFunction */) {
1232
- serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1233
- } else if (ctx.features & 32 /* MethodShorthand */) {
1234
- serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1235
- } else {
1236
- serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1203
+ serializePromise(node) {
1204
+ let serialized;
1205
+ if (this.isReferenceInStack(node.n)) {
1206
+ serialized = `Promise.resolve().then(()=>${getRefParam(this.ctx, node.n.i)})`;
1207
+ } else {
1208
+ this.ctx.stack.push(node.i);
1209
+ const result = this.serialize(node.n);
1210
+ this.ctx.stack.pop();
1211
+ serialized = `Promise.resolve(${result})`;
1212
+ }
1213
+ return this.assignRef(node.i, serialized);
1237
1214
  }
1238
- if (node.d) {
1239
- if (ctx.features & 64 /* ObjectAssign */) {
1240
- serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1215
+ serializeTypedArray(node) {
1216
+ let args = `[${node.s}]`;
1217
+ if (node.l !== 0) {
1218
+ args += `,${node.l}`;
1219
+ }
1220
+ return this.assignRef(node.i, `new ${node.c}(${args})`);
1221
+ }
1222
+ serializeIterable(node) {
1223
+ const parent = this.ctx.stack;
1224
+ this.ctx.stack = [];
1225
+ const values = this.serializeNodeList(node);
1226
+ this.ctx.stack = parent;
1227
+ let serialized;
1228
+ if (this.ctx.features & 2 /* ArrayPrototypeValues */) {
1229
+ serialized = `${values}.values()`;
1241
1230
  } else {
1242
- markRef(ctx, node.i);
1243
- const assignments = serializeAssignments(ctx, node.i, node.d);
1244
- if (assignments) {
1245
- const ref = getRefParam(ctx, node.i);
1246
- return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
1231
+ serialized = `${values}[Symbol.iterator]()`;
1232
+ }
1233
+ if (this.ctx.features & 4 /* ArrowFunction */) {
1234
+ serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1235
+ } else if (this.ctx.features & 32 /* MethodShorthand */) {
1236
+ serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1237
+ } else {
1238
+ serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1239
+ }
1240
+ if (node.d) {
1241
+ if (this.ctx.features & 64 /* ObjectAssign */) {
1242
+ serialized = this.serializeWithObjectAssign(node.d, node.i, serialized);
1243
+ } else {
1244
+ markRef(this.ctx, node.i);
1245
+ const assignments = this.serializeAssignments(node.i, node.d);
1246
+ if (assignments) {
1247
+ const ref = getRefParam(this.ctx, node.i);
1248
+ return `(${this.assignRef(node.i, serialized)},${assignments}${ref})`;
1249
+ }
1247
1250
  }
1248
1251
  }
1249
- }
1250
- return assignRef(ctx, node.i, serialized);
1251
- }
1252
- function serializeNullConstructor(ctx, node) {
1253
- let serialized = "Object.create(null)";
1254
- if (ctx.features & 64 /* ObjectAssign */) {
1255
- serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1256
- } else {
1257
- markRef(ctx, node.i);
1258
- const assignments = serializeAssignments(ctx, node.i, node.d);
1259
- if (assignments) {
1260
- const ref = getRefParam(ctx, node.i);
1261
- return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
1252
+ return this.assignRef(node.i, serialized);
1253
+ }
1254
+ serialize(node) {
1255
+ switch (node.t) {
1256
+ case 0 /* Primitive */:
1257
+ return String(node.s);
1258
+ case 1 /* BigInt */:
1259
+ return node.s;
1260
+ case 2 /* Reference */:
1261
+ return getRefParam(this.ctx, node.i);
1262
+ case 7 /* Array */:
1263
+ return this.serializeArray(node);
1264
+ case 8 /* Object */:
1265
+ return this.assignRef(node.i, this.serializeObject(node.i, node.d));
1266
+ case 9 /* NullConstructor */:
1267
+ return this.serializeNullConstructor(node);
1268
+ case 3 /* Date */:
1269
+ return this.assignRef(node.i, `new Date("${node.s}")`);
1270
+ case 4 /* RegExp */:
1271
+ return this.assignRef(node.i, node.s);
1272
+ case 5 /* Set */:
1273
+ return this.serializeSet(node);
1274
+ case 6 /* Map */:
1275
+ return this.serializeMap(node);
1276
+ case 15 /* BigIntTypedArray */:
1277
+ case 14 /* TypedArray */:
1278
+ return this.serializeTypedArray(node);
1279
+ case 12 /* AggregateError */:
1280
+ return this.serializeAggregateError(node);
1281
+ case 11 /* Error */:
1282
+ return this.serializeError(node);
1283
+ case 13 /* Iterable */:
1284
+ return this.serializeIterable(node);
1285
+ case 10 /* Promise */:
1286
+ return this.serializePromise(node);
1287
+ default:
1288
+ throw new Error("Unsupported type");
1262
1289
  }
1263
1290
  }
1264
- return assignRef(ctx, node.i, serialized);
1265
- }
1266
- function serializeTree(ctx, node) {
1267
- switch (node.t) {
1268
- case 0 /* Primitive */:
1269
- return String(node.s);
1270
- case 2 /* Reference */:
1271
- return getRefParam(ctx, node.i);
1272
- case 10 /* Promise */:
1273
- return serializePromise(ctx, node);
1274
- case 1 /* BigInt */:
1275
- return node.s;
1276
- case 3 /* Date */:
1277
- return assignRef(ctx, node.i, `new Date("${node.s}")`);
1278
- case 4 /* RegExp */:
1279
- return assignRef(ctx, node.i, node.s);
1280
- case 15 /* BigIntTypedArray */:
1281
- case 14 /* TypedArray */:
1282
- return serializeTypedArray(ctx, node);
1283
- case 5 /* Set */:
1284
- return serializeSet(ctx, node);
1285
- case 6 /* Map */:
1286
- return serializeMap(ctx, node);
1287
- case 7 /* Array */:
1288
- return serializeArray(ctx, node);
1289
- case 12 /* AggregateError */:
1290
- return serializeAggregateError(ctx, node);
1291
- case 11 /* Error */:
1292
- return serializeError(ctx, node);
1293
- case 13 /* Iterable */:
1294
- return serializeIterable(ctx, node);
1295
- case 9 /* NullConstructor */:
1296
- return serializeNullConstructor(ctx, node);
1297
- case 8 /* Object */:
1298
- return assignRef(ctx, node.i, serializeObject(ctx, node.i, node.d));
1299
- default:
1300
- throw new Error("Unsupported type");
1301
- }
1302
- }
1291
+ };
1303
1292
 
1304
1293
  // src/tree/sync.ts
1305
1294
  var SyncParser = class {
@@ -1330,7 +1319,17 @@ var SyncParser = class {
1330
1319
  return nodes;
1331
1320
  }
1332
1321
  generateArrayNode(id, current) {
1333
- return new SerovalArrayNode(id, this.generateNodeList(current));
1322
+ return {
1323
+ t: 7 /* Array */,
1324
+ i: id,
1325
+ s: void 0,
1326
+ l: void 0,
1327
+ c: void 0,
1328
+ m: void 0,
1329
+ d: void 0,
1330
+ a: this.generateNodeList(current),
1331
+ n: void 0
1332
+ };
1334
1333
  }
1335
1334
  generateMapNode(id, current) {
1336
1335
  assert(this.ctx.features & 16 /* Map */, 'Unsupported type "Map"');
@@ -1356,7 +1355,17 @@ var SyncParser = class {
1356
1355
  keyNodes[nodeSize + i] = this.parse(deferredKey[i]);
1357
1356
  valueNodes[nodeSize + i] = this.parse(deferredValue[i]);
1358
1357
  }
1359
- return new SerovalMapNode(id, new SerovalMapRecordNode(keyNodes, valueNodes, len));
1358
+ return {
1359
+ t: 6 /* Map */,
1360
+ i: id,
1361
+ s: void 0,
1362
+ l: void 0,
1363
+ c: void 0,
1364
+ m: void 0,
1365
+ d: { k: keyNodes, v: valueNodes, s: len },
1366
+ a: void 0,
1367
+ n: void 0
1368
+ };
1360
1369
  }
1361
1370
  generateSetNode(id, current) {
1362
1371
  assert(this.ctx.features & 256 /* Set */, 'Unsupported type "Set"');
@@ -1375,7 +1384,17 @@ var SyncParser = class {
1375
1384
  for (let i = 0; i < deferredSize; i++) {
1376
1385
  nodes[nodeSize + i] = this.parse(deferred[i]);
1377
1386
  }
1378
- return new SerovalSetNode(id, nodes);
1387
+ return {
1388
+ t: 5 /* Set */,
1389
+ i: id,
1390
+ s: void 0,
1391
+ l: void 0,
1392
+ c: void 0,
1393
+ m: void 0,
1394
+ d: void 0,
1395
+ a: nodes,
1396
+ n: void 0
1397
+ };
1379
1398
  }
1380
1399
  generateProperties(properties) {
1381
1400
  const keys = Object.keys(properties);
@@ -1412,36 +1431,64 @@ var SyncParser = class {
1412
1431
  generateIterableNode(id, current) {
1413
1432
  assert(this.ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
1414
1433
  const options = getIterableOptions(current);
1415
- return new SerovalIterableNode(
1416
- id,
1434
+ return {
1435
+ t: 13 /* Iterable */,
1436
+ i: id,
1437
+ s: void 0,
1438
+ l: void 0,
1439
+ c: void 0,
1440
+ m: void 0,
1417
1441
  // Parse options first before the items
1418
- options ? this.generateProperties(options) : void 0,
1419
- this.generateNodeList(Array.from(current))
1420
- );
1442
+ d: options ? this.generateProperties(options) : void 0,
1443
+ a: this.generateNodeList(Array.from(current)),
1444
+ n: void 0
1445
+ };
1421
1446
  }
1422
1447
  generateObjectNode(id, current, empty) {
1423
1448
  if (Symbol.iterator in current) {
1424
1449
  return this.generateIterableNode(id, current);
1425
1450
  }
1426
- if (empty) {
1427
- return new SerovalNullConstructorNode(id, this.generateProperties(current));
1428
- }
1429
- return new SerovalObjectNode(id, this.generateProperties(current));
1451
+ return {
1452
+ t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
1453
+ i: id,
1454
+ s: void 0,
1455
+ l: void 0,
1456
+ c: void 0,
1457
+ m: void 0,
1458
+ d: this.generateProperties(current),
1459
+ a: void 0,
1460
+ n: void 0
1461
+ };
1430
1462
  }
1431
1463
  generateAggregateErrorNode(id, current) {
1432
1464
  const options = getErrorOptions(current);
1433
1465
  const optionsNode = options ? this.generateProperties(options) : void 0;
1434
- return new SerovalAggregateErrorNode(
1435
- id,
1436
- current.message,
1437
- optionsNode,
1438
- this.parse(current.errors)
1439
- );
1466
+ return {
1467
+ t: 12 /* AggregateError */,
1468
+ i: id,
1469
+ s: void 0,
1470
+ l: void 0,
1471
+ c: void 0,
1472
+ m: current.message,
1473
+ d: optionsNode,
1474
+ a: void 0,
1475
+ n: this.parse(current.errors)
1476
+ };
1440
1477
  }
1441
1478
  generateErrorNode(id, current) {
1442
1479
  const options = getErrorOptions(current);
1443
1480
  const optionsNode = options ? this.generateProperties(options) : void 0;
1444
- return new SerovalErrorNode(id, getErrorConstructor(current), current.message, optionsNode);
1481
+ return {
1482
+ t: 11 /* Error */,
1483
+ i: id,
1484
+ s: void 0,
1485
+ l: void 0,
1486
+ c: getErrorConstructor(current),
1487
+ m: current.message,
1488
+ d: optionsNode,
1489
+ a: void 0,
1490
+ n: void 0
1491
+ };
1445
1492
  }
1446
1493
  parse(current) {
1447
1494
  switch (typeof current) {
@@ -1450,37 +1497,36 @@ var SyncParser = class {
1450
1497
  case "undefined":
1451
1498
  return UNDEFINED_NODE;
1452
1499
  case "string":
1453
- return new SerovalPrimitiveNode(quote(current));
1500
+ return createPrimitiveNode(quote(current));
1454
1501
  case "number":
1455
1502
  if (Object.is(current, -0)) {
1456
1503
  return NEG_ZERO_NODE;
1457
1504
  }
1458
1505
  if (Object.is(current, Infinity)) {
1459
- return INFINITY_NODE;
1506
+ return createPrimitiveNode("1/0");
1460
1507
  }
1461
1508
  if (Object.is(current, -Infinity)) {
1462
- return NEG_INFINITY_NODE;
1509
+ return createPrimitiveNode("-1/0");
1463
1510
  }
1464
- return new SerovalPrimitiveNode(current);
1511
+ return createPrimitiveNode(current);
1465
1512
  case "bigint":
1466
- assert(this.ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
1467
- return new SerovalBigIntNode(current);
1513
+ return createBigIntNode(this.ctx, current);
1468
1514
  case "object": {
1469
1515
  if (!current) {
1470
1516
  return NULL_NODE;
1471
1517
  }
1472
1518
  const id = createRef(this.ctx, current, true);
1473
- if (this.ctx.markedRefs[id]) {
1474
- return new SerovalReferenceNode(id);
1519
+ if (this.ctx.markedRefs.has(id)) {
1520
+ return createReferenceNode(id);
1475
1521
  }
1476
1522
  if (Array.isArray(current)) {
1477
1523
  return this.generateArrayNode(id, current);
1478
1524
  }
1479
1525
  switch (current.constructor) {
1480
1526
  case Date:
1481
- return new SerovalDateNode(id, current);
1527
+ return createDateNode(id, current);
1482
1528
  case RegExp:
1483
- return new SerovalRegExpNode(id, current);
1529
+ return createRegExpNode(id, current);
1484
1530
  case Int8Array:
1485
1531
  case Int16Array:
1486
1532
  case Int32Array:
@@ -1490,15 +1536,10 @@ var SyncParser = class {
1490
1536
  case Uint8ClampedArray:
1491
1537
  case Float32Array:
1492
1538
  case Float64Array:
1493
- assert(this.ctx.features & 1024 /* TypedArray */, `Unsupported value type "${current.constructor.name}"`);
1494
- return new SerovalTypedArrayNode(id, current);
1539
+ return createTypedArrayNode(this.ctx, id, current);
1495
1540
  case BigInt64Array:
1496
1541
  case BigUint64Array:
1497
- assert(
1498
- this.ctx.features & 2048 /* BigIntTypedArray */,
1499
- `Unsupported value type "${current.constructor.name}"`
1500
- );
1501
- return new SerovalBigIntTypedArrayNode(id, current);
1542
+ return createBigIntTypedArrayNode(this.ctx, id, current);
1502
1543
  case Map:
1503
1544
  return this.generateMapNode(id, current);
1504
1545
  case Set:
@@ -1571,52 +1612,52 @@ function finalize(ctx, rootID, isObject, result) {
1571
1612
  return result;
1572
1613
  }
1573
1614
  function serialize(source, options) {
1574
- const ctx = new ParserContext(options);
1615
+ const ctx = createParserContext(options);
1575
1616
  const [tree, rootID, isObject] = parseSync(ctx, source);
1576
- const serial = new SerializationContext(ctx);
1577
- const result = serializeTree(serial, tree);
1617
+ const serial = createSerializationContext(ctx);
1618
+ const result = new SerovalSerializer(serial).serialize(tree);
1578
1619
  return finalize(serial, rootID, isObject, result);
1579
1620
  }
1580
1621
  async function serializeAsync(source, options) {
1581
- const ctx = new ParserContext(options);
1622
+ const ctx = createParserContext(options);
1582
1623
  const [tree, rootID, isObject] = await parseAsync(ctx, source);
1583
- const serial = new SerializationContext(ctx);
1584
- const result = serializeTree(serial, tree);
1624
+ const serial = createSerializationContext(ctx);
1625
+ const result = new SerovalSerializer(serial).serialize(tree);
1585
1626
  return finalize(serial, rootID, isObject, result);
1586
1627
  }
1587
1628
  function deserialize(source) {
1588
1629
  return (0, eval)(source);
1589
1630
  }
1590
1631
  function toJSON(source, options) {
1591
- const ctx = new ParserContext(options);
1632
+ const ctx = createParserContext(options);
1592
1633
  const [tree, root, isObject] = parseSync(ctx, source);
1593
- return JSON.stringify([
1594
- tree,
1595
- root,
1596
- isObject,
1597
- ctx.features,
1598
- ctx.markedRefs
1599
- ]);
1634
+ return JSON.stringify({
1635
+ t: tree,
1636
+ r: root,
1637
+ i: isObject,
1638
+ f: ctx.features,
1639
+ m: Array.from(ctx.markedRefs)
1640
+ });
1600
1641
  }
1601
1642
  async function toJSONAsync(source, options) {
1602
- const ctx = new ParserContext(options);
1643
+ const ctx = createParserContext(options);
1603
1644
  const [tree, root, isObject] = await parseAsync(ctx, source);
1604
- return JSON.stringify([
1605
- tree,
1606
- root,
1607
- isObject,
1608
- ctx.features,
1609
- ctx.markedRefs
1610
- ]);
1645
+ return JSON.stringify({
1646
+ t: tree,
1647
+ r: root,
1648
+ i: isObject,
1649
+ f: ctx.features,
1650
+ m: Array.from(ctx.markedRefs)
1651
+ });
1611
1652
  }
1612
1653
  function compileJSON(source) {
1613
1654
  const parsed = JSON.parse(source);
1614
- const serial = new SerializationContext({
1615
- features: parsed[3],
1616
- markedRefs: parsed[4]
1655
+ const serial = createSerializationContext({
1656
+ features: parsed.f,
1657
+ markedRefs: parsed.m
1617
1658
  });
1618
- const result = serializeTree(serial, parsed[0]);
1619
- return finalize(serial, parsed[1], parsed[2], result);
1659
+ const result = new SerovalSerializer(serial).serialize(parsed.t);
1660
+ return finalize(serial, parsed.r, parsed.i, result);
1620
1661
  }
1621
1662
  function fromJSON(source) {
1622
1663
  return deserialize(compileJSON(source));