seroval 0.4.0-alpha.12 → 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.
@@ -283,7 +283,7 @@ var DEFAULT_OPTIONS = {
283
283
  function createParserContext(options = {}) {
284
284
  const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
285
285
  return {
286
- markedRefs: {},
286
+ markedRefs: /* @__PURE__ */ new Set(),
287
287
  refs: /* @__PURE__ */ new Map(),
288
288
  features: parseTargets(result.target)
289
289
  };
@@ -296,11 +296,11 @@ function createSerializationContext(options) {
296
296
  validRefs: [],
297
297
  refSize: 0,
298
298
  features: options.features,
299
- markedRefs: options.markedRefs
299
+ markedRefs: new Set(options.markedRefs)
300
300
  };
301
301
  }
302
302
  function markRef(ctx, current) {
303
- ctx.markedRefs[current] = 1;
303
+ ctx.markedRefs.add(current);
304
304
  }
305
305
  function createValidRef(ctx, index) {
306
306
  const current = ctx.validRefs[index];
@@ -389,8 +389,8 @@ function createPrimitiveNode(value) {
389
389
  i: void 0,
390
390
  s: value,
391
391
  l: void 0,
392
- m: void 0,
393
392
  c: void 0,
393
+ m: void 0,
394
394
  d: void 0,
395
395
  a: void 0,
396
396
  n: void 0
@@ -408,12 +408,12 @@ function createBigIntNode(ctx, current) {
408
408
  return {
409
409
  t: 1 /* BigInt */,
410
410
  i: void 0,
411
- a: void 0,
412
411
  s: `${current}n`,
413
412
  l: void 0,
414
- m: void 0,
415
413
  c: void 0,
414
+ m: void 0,
416
415
  d: void 0,
416
+ a: void 0,
417
417
  n: void 0
418
418
  };
419
419
  }
@@ -421,12 +421,12 @@ function createReferenceNode(id) {
421
421
  return {
422
422
  t: 2 /* Reference */,
423
423
  i: id,
424
- a: void 0,
425
424
  s: void 0,
426
425
  l: void 0,
427
- m: void 0,
428
426
  c: void 0,
427
+ m: void 0,
429
428
  d: void 0,
429
+ a: void 0,
430
430
  n: void 0
431
431
  };
432
432
  }
@@ -434,25 +434,25 @@ function createDateNode(id, current) {
434
434
  return {
435
435
  t: 3 /* Date */,
436
436
  i: id,
437
- a: void 0,
438
437
  s: current.toISOString(),
439
438
  l: void 0,
440
- m: void 0,
441
439
  c: void 0,
440
+ m: void 0,
442
441
  d: void 0,
443
- n: void 0
442
+ n: void 0,
443
+ a: void 0
444
444
  };
445
445
  }
446
446
  function createRegExpNode(id, current) {
447
447
  return {
448
448
  t: 4 /* RegExp */,
449
449
  i: id,
450
- a: void 0,
451
450
  s: String(current),
452
451
  l: void 0,
453
- m: void 0,
454
452
  c: void 0,
453
+ m: void 0,
455
454
  d: void 0,
455
+ a: void 0,
456
456
  n: void 0
457
457
  };
458
458
  }
@@ -462,12 +462,12 @@ function createTypedArrayNode(ctx, id, current) {
462
462
  return {
463
463
  t: 14 /* TypedArray */,
464
464
  i: id,
465
- a: void 0,
466
465
  s: current.toString(),
467
466
  l: current.byteOffset,
468
- m: void 0,
469
467
  c: constructor,
468
+ m: void 0,
470
469
  d: void 0,
470
+ a: void 0,
471
471
  n: void 0
472
472
  };
473
473
  }
@@ -486,12 +486,12 @@ function createBigIntTypedArrayNode(ctx, id, current) {
486
486
  return {
487
487
  t: 15 /* BigIntTypedArray */,
488
488
  i: id,
489
- a: void 0,
490
489
  s: result,
491
490
  l: current.byteOffset,
492
- m: void 0,
493
491
  c: constructor,
492
+ m: void 0,
494
493
  d: void 0,
494
+ a: void 0,
495
495
  n: void 0
496
496
  };
497
497
  }
@@ -546,9 +546,6 @@ function getIterableOptions(obj) {
546
546
  }
547
547
  return void 0;
548
548
  }
549
- function isReferenceInStack(ctx, node) {
550
- return node.t === 2 /* Reference */ && ctx.stack.includes(node.i);
551
- }
552
549
  function isIterable(value) {
553
550
  if (!value || typeof value !== "object") {
554
551
  return false;
@@ -609,12 +606,12 @@ var AsyncParser = class {
609
606
  return {
610
607
  t: 7 /* Array */,
611
608
  i: id,
612
- a: await this.generateNodeList(current),
613
609
  s: void 0,
614
610
  l: void 0,
615
- m: void 0,
616
611
  c: void 0,
612
+ m: void 0,
617
613
  d: void 0,
614
+ a: await this.generateNodeList(current),
618
615
  n: void 0
619
616
  };
620
617
  }
@@ -645,12 +642,12 @@ var AsyncParser = class {
645
642
  return {
646
643
  t: 6 /* Map */,
647
644
  i: id,
648
- a: void 0,
649
645
  s: void 0,
650
646
  l: void 0,
651
- m: void 0,
652
647
  c: void 0,
648
+ m: void 0,
653
649
  d: { k: keyNodes, v: valueNodes, s: len },
650
+ a: void 0,
654
651
  n: void 0
655
652
  };
656
653
  }
@@ -674,12 +671,12 @@ var AsyncParser = class {
674
671
  return {
675
672
  t: 5 /* Set */,
676
673
  i: id,
677
- a: nodes,
678
674
  s: void 0,
679
675
  l: void 0,
680
- m: void 0,
681
676
  c: void 0,
677
+ m: void 0,
682
678
  d: void 0,
679
+ a: nodes,
683
680
  n: void 0
684
681
  };
685
682
  }
@@ -723,8 +720,8 @@ var AsyncParser = class {
723
720
  i: id,
724
721
  s: void 0,
725
722
  l: void 0,
726
- m: void 0,
727
723
  c: void 0,
724
+ m: void 0,
728
725
  // Parse options first before the items
729
726
  d: options ? await this.generateProperties(options) : void 0,
730
727
  a: await this.generateNodeList(Array.from(current)),
@@ -738,8 +735,8 @@ var AsyncParser = class {
738
735
  i: id,
739
736
  s: void 0,
740
737
  l: void 0,
741
- m: void 0,
742
738
  c: void 0,
739
+ m: void 0,
743
740
  // Parse options first before the items
744
741
  d: void 0,
745
742
  a: void 0,
@@ -758,8 +755,8 @@ var AsyncParser = class {
758
755
  i: id,
759
756
  s: void 0,
760
757
  l: void 0,
761
- m: void 0,
762
758
  c: void 0,
759
+ m: void 0,
763
760
  d: await this.generateProperties(current),
764
761
  a: void 0,
765
762
  n: void 0
@@ -771,12 +768,12 @@ var AsyncParser = class {
771
768
  return {
772
769
  t: 12 /* AggregateError */,
773
770
  i: id,
774
- a: void 0,
775
771
  s: void 0,
776
772
  l: void 0,
777
- m: current.message,
778
773
  c: void 0,
774
+ m: current.message,
779
775
  d: optionsNode,
776
+ a: void 0,
780
777
  n: await this.parse(current.errors)
781
778
  };
782
779
  }
@@ -786,12 +783,12 @@ var AsyncParser = class {
786
783
  return {
787
784
  t: 11 /* Error */,
788
785
  i: id,
789
- a: void 0,
790
786
  s: void 0,
791
787
  l: void 0,
792
- m: current.message,
793
788
  c: getErrorConstructor(current),
789
+ m: current.message,
794
790
  d: optionsNode,
791
+ a: void 0,
795
792
  n: void 0
796
793
  };
797
794
  }
@@ -821,7 +818,7 @@ var AsyncParser = class {
821
818
  return NULL_NODE;
822
819
  }
823
820
  const id = createRef(this.ctx, current, true);
824
- if (this.ctx.markedRefs[id]) {
821
+ if (this.ctx.markedRefs.has(id)) {
825
822
  return createReferenceNode(id);
826
823
  }
827
824
  if (Array.isArray(current)) {
@@ -901,13 +898,13 @@ async function parseAsync(ctx, current) {
901
898
 
902
899
  // src/tree/serialize.ts
903
900
  function getAssignmentExpression(assignment) {
904
- switch (assignment.type) {
901
+ switch (assignment.t) {
905
902
  case "index":
906
- return `${assignment.source}=${assignment.value}`;
903
+ return `${assignment.s}=${assignment.v}`;
907
904
  case "map":
908
- return `${assignment.source}.set(${assignment.key},${assignment.value})`;
905
+ return `${assignment.s}.set(${assignment.k},${assignment.v})`;
909
906
  case "set":
910
- return `${assignment.source}.add(${assignment.value})`;
907
+ return `${assignment.s}.add(${assignment.v})`;
911
908
  default:
912
909
  return "";
913
910
  }
@@ -918,25 +915,27 @@ function mergeAssignments(assignments) {
918
915
  let prev = current;
919
916
  for (let i = 1, len = assignments.length; i < len; i++) {
920
917
  const item = assignments[i];
921
- if (item.type === prev.type) {
922
- if (item.type === "index" && item.value === prev.value) {
918
+ if (item.t === prev.t) {
919
+ if (item.t === "index" && item.v === prev.v) {
923
920
  current = {
924
- type: "index",
925
- source: item.source,
926
- value: getAssignmentExpression(current)
921
+ t: "index",
922
+ s: item.s,
923
+ k: void 0,
924
+ v: getAssignmentExpression(current)
927
925
  };
928
- } else if (item.type === "map" && item.source === prev.source) {
926
+ } else if (item.t === "map" && item.s === prev.s) {
929
927
  current = {
930
- type: "map",
931
- source: getAssignmentExpression(current),
932
- key: item.key,
933
- value: item.value
928
+ t: "map",
929
+ s: getAssignmentExpression(current),
930
+ k: item.k,
931
+ v: item.v
934
932
  };
935
- } else if (item.type === "set" && item.source === prev.source) {
933
+ } else if (item.t === "set" && item.s === prev.s) {
936
934
  current = {
937
- type: "set",
938
- source: getAssignmentExpression(current),
939
- value: item.value
935
+ t: "set",
936
+ s: getAssignmentExpression(current),
937
+ k: void 0,
938
+ v: item.v
940
939
  };
941
940
  } else {
942
941
  newAssignments.push(current);
@@ -965,34 +964,30 @@ function resolveAssignments(assignments) {
965
964
  function resolvePatches(ctx) {
966
965
  return resolveAssignments(ctx.assignments);
967
966
  }
968
- function assignRef(ctx, index, value) {
969
- if (ctx.markedRefs[index]) {
970
- return `${getRefParam(ctx, index)}=${value}`;
971
- }
972
- return value;
973
- }
974
967
  function createAssignment(ctx, source, value) {
975
968
  ctx.assignments.push({
976
- type: "index",
977
- source,
978
- value
969
+ t: "index",
970
+ s: source,
971
+ k: void 0,
972
+ v: value
979
973
  });
980
974
  }
981
975
  function createSetAdd(ctx, ref, value) {
982
976
  markRef(ctx, ref);
983
977
  ctx.assignments.push({
984
- type: "set",
985
- source: getRefParam(ctx, ref),
986
- value
978
+ t: "set",
979
+ s: getRefParam(ctx, ref),
980
+ k: void 0,
981
+ v: value
987
982
  });
988
983
  }
989
984
  function createMapSet(ctx, ref, key, value) {
990
985
  markRef(ctx, ref);
991
986
  ctx.assignments.push({
992
- type: "map",
993
- source: getRefParam(ctx, ref),
994
- key,
995
- value
987
+ t: "map",
988
+ s: getRefParam(ctx, ref),
989
+ k: key,
990
+ v: value
996
991
  });
997
992
  }
998
993
  function createArrayAssign(ctx, ref, index, value) {
@@ -1004,281 +999,296 @@ function createObjectAssign(ctx, ref, key, value) {
1004
999
  createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
1005
1000
  }
1006
1001
  var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
1007
- function serializeAssignments(ctx, targetRef, node) {
1008
- ctx.stack.push(targetRef);
1009
- const mainAssignments = [];
1010
- for (let i = 0; i < node.s; i++) {
1011
- const parentStack = ctx.stack;
1012
- ctx.stack = [];
1013
- const refParam = serializeTree(ctx, node.v[i]);
1014
- ctx.stack = parentStack;
1015
- const key = node.k[i];
1016
- const check = Number(key);
1017
- const parentAssignment = ctx.assignments;
1018
- ctx.assignments = mainAssignments;
1019
- const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1020
- if (isIdentifier && Number.isNaN(check)) {
1021
- createObjectAssign(ctx, targetRef, key, refParam);
1022
- } else {
1023
- createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
1024
- }
1025
- ctx.assignments = parentAssignment;
1026
- }
1027
- ctx.stack.pop();
1028
- return resolveAssignments(mainAssignments);
1029
- }
1030
- function serializeObject(ctx, sourceID, node) {
1031
- if (node.s === 0) {
1032
- return "{}";
1002
+ var SerovalSerializer = class {
1003
+ constructor(ctx) {
1004
+ __publicField(this, "ctx");
1005
+ this.ctx = ctx;
1033
1006
  }
1034
- let result = "";
1035
- ctx.stack.push(sourceID);
1036
- for (let i = 0; i < node.s; i++) {
1037
- const key = node.k[i];
1038
- const val = node.v[i];
1039
- const check = Number(key);
1040
- const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1041
- if (isReferenceInStack(ctx, val)) {
1042
- const refParam = getRefParam(ctx, val.i);
1043
- if (isIdentifier && Number.isNaN(check)) {
1044
- createObjectAssign(ctx, sourceID, key, refParam);
1045
- } else {
1046
- createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1047
- }
1048
- } else {
1049
- 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}`;
1050
1010
  }
1011
+ return value;
1051
1012
  }
1052
- ctx.stack.pop();
1053
- return `{${result.substring(0, result.length - 1)}}`;
1054
- }
1055
- function serializePromise(ctx, node) {
1056
- let serialized;
1057
- if (isReferenceInStack(ctx, node.n)) {
1058
- serialized = `Promise.resolve().then(()=>${getRefParam(ctx, node.n.i)})`;
1059
- } else {
1060
- ctx.stack.push(node.i);
1061
- const result = serializeTree(ctx, node.n);
1062
- ctx.stack.pop();
1063
- serialized = `Promise.resolve(${result})`;
1064
- }
1065
- return assignRef(ctx, node.i, serialized);
1066
- }
1067
- function serializeTypedArray(ctx, node) {
1068
- let args = `[${node.s}]`;
1069
- if (node.l !== 0) {
1070
- args += `,${node.l}`;
1013
+ isReferenceInStack(node) {
1014
+ return node.t === 2 /* Reference */ && this.ctx.stack.includes(node.i);
1071
1015
  }
1072
- return assignRef(ctx, node.i, `new ${node.c}(${args})`);
1073
- }
1074
- function serializeSet(ctx, node) {
1075
- let serialized = "new Set";
1076
- const size = node.a.length;
1077
- if (size) {
1078
- let result = "";
1079
- ctx.stack.push(node.i);
1016
+ serializeNodeList(node) {
1017
+ const size = node.a.length;
1018
+ let values = "";
1080
1019
  for (let i = 0; i < size; i++) {
1081
1020
  const item = node.a[i];
1082
- if (isReferenceInStack(ctx, item)) {
1083
- 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
+ }
1084
1031
  } else {
1085
- result += `${serializeTree(ctx, item)},`;
1032
+ values += ",";
1086
1033
  }
1087
1034
  }
1088
- ctx.stack.pop();
1089
- if (result) {
1090
- serialized += `([${result.substring(0, result.length - 1)}])`;
1091
- }
1035
+ return `[${values}]`;
1092
1036
  }
1093
- return assignRef(ctx, node.i, serialized);
1094
- }
1095
- function serializeMap(ctx, node) {
1096
- let serialized = "new Map";
1097
- 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
+ }
1098
1047
  let result = "";
1099
- ctx.stack.push(node.i);
1100
- for (let i = 0; i < node.d.s; i++) {
1101
- const key = node.d.k[i];
1102
- const val = node.d.v[i];
1103
- if (isReferenceInStack(ctx, key)) {
1104
- const keyRef = getRefParam(ctx, key.i);
1105
- if (isReferenceInStack(ctx, val)) {
1106
- const valueRef = getRefParam(ctx, val.i);
1107
- 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);
1108
1058
  } else {
1109
- const parent = ctx.stack;
1110
- ctx.stack = [];
1111
- createMapSet(ctx, node.i, keyRef, serializeTree(ctx, val));
1112
- ctx.stack = parent;
1059
+ createArrayAssign(this.ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1113
1060
  }
1114
- } else if (isReferenceInStack(ctx, val)) {
1115
- const valueRef = getRefParam(ctx, val.i);
1116
- const parent = ctx.stack;
1117
- ctx.stack = [];
1118
- createMapSet(ctx, node.i, serializeTree(ctx, key), valueRef);
1119
- ctx.stack = parent;
1120
1061
  } else {
1121
- result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
1062
+ result += `${isIdentifier ? key : quote(key)}:${this.serialize(val)},`;
1122
1063
  }
1123
1064
  }
1124
- ctx.stack.pop();
1125
- if (result) {
1126
- serialized += `([${result.substring(0, result.length - 1)}])`;
1127
- }
1065
+ this.ctx.stack.pop();
1066
+ return `{${result.substring(0, result.length - 1)}}`;
1128
1067
  }
1129
- return assignRef(ctx, node.i, serialized);
1130
- }
1131
- function serializeNodeList(ctx, node) {
1132
- const size = node.a.length;
1133
- let values = "";
1134
- for (let i = 0; i < size; i++) {
1135
- const item = node.a[i];
1136
- if (item) {
1137
- if (isReferenceInStack(ctx, item)) {
1138
- createArrayAssign(ctx, node.i, i, getRefParam(ctx, item.i));
1139
- 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);
1140
1090
  } else {
1141
- values += serializeTree(ctx, item);
1142
- if (i < size - 1) {
1143
- values += ",";
1144
- }
1091
+ createArrayAssign(this.ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1145
1092
  }
1146
- } else {
1147
- values += ",";
1093
+ this.ctx.assignments = parentAssignment;
1148
1094
  }
1095
+ this.ctx.stack.pop();
1096
+ return resolveAssignments(mainAssignments);
1149
1097
  }
1150
- return `[${values}]`;
1151
- }
1152
- function serializeArray(ctx, node) {
1153
- ctx.stack.push(node.i);
1154
- const result = serializeNodeList(ctx, node);
1155
- ctx.stack.pop();
1156
- return assignRef(ctx, node.i, result);
1157
- }
1158
- function serializeWithObjectAssign(ctx, value, id, serialized) {
1159
- const fields = serializeObject(ctx, id, value);
1160
- if (fields !== "{}") {
1161
- return `Object.assign(${serialized},${fields})`;
1162
- }
1163
- return serialized;
1164
- }
1165
- function serializeAggregateError(ctx, node) {
1166
- ctx.stack.push(node.i);
1167
- let serialized = `new AggregateError(${serializeTree(ctx, node.n)},${quote(node.m)})`;
1168
- ctx.stack.pop();
1169
- if (node.d) {
1170
- if (ctx.features & 64 /* ObjectAssign */) {
1171
- 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);
1172
1102
  } else {
1173
- markRef(ctx, node.i);
1174
- const assignments = serializeAssignments(ctx, node.i, node.d);
1103
+ markRef(this.ctx, node.i);
1104
+ const assignments = this.serializeAssignments(node.i, node.d);
1175
1105
  if (assignments) {
1176
- const ref = getRefParam(ctx, node.i);
1177
- 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})`;
1178
1108
  }
1179
1109
  }
1180
- }
1181
- return assignRef(ctx, node.i, serialized);
1182
- }
1183
- function serializeError(ctx, node) {
1184
- let serialized = `new ${node.c}(${quote(node.m)})`;
1185
- if (node.d) {
1186
- if (ctx.features & 64 /* ObjectAssign */) {
1187
- serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1188
- } else {
1189
- markRef(ctx, node.i);
1190
- const assignments = serializeAssignments(ctx, node.i, node.d);
1191
- if (assignments) {
1192
- const ref = getRefParam(ctx, node.i);
1193
- 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)}])`;
1194
1129
  }
1195
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
+ }
1183
+ }
1184
+ }
1185
+ return this.assignRef(node.i, serialized);
1196
1186
  }
1197
- return assignRef(ctx, node.i, serialized);
1198
- }
1199
- function serializeIterable(ctx, node) {
1200
- const parent = ctx.stack;
1201
- ctx.stack = [];
1202
- const values = serializeNodeList(ctx, node);
1203
- ctx.stack = parent;
1204
- let serialized;
1205
- if (ctx.features & 2 /* ArrayPrototypeValues */) {
1206
- serialized = `${values}.values()`;
1207
- } else {
1208
- 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);
1209
1202
  }
1210
- if (ctx.features & 4 /* ArrowFunction */) {
1211
- serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1212
- } else if (ctx.features & 32 /* MethodShorthand */) {
1213
- serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1214
- } else {
1215
- 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);
1216
1214
  }
1217
- if (node.d) {
1218
- if (ctx.features & 64 /* ObjectAssign */) {
1219
- 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()`;
1220
1230
  } else {
1221
- markRef(ctx, node.i);
1222
- const assignments = serializeAssignments(ctx, node.i, node.d);
1223
- if (assignments) {
1224
- const ref = getRefParam(ctx, node.i);
1225
- 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
+ }
1226
1250
  }
1227
1251
  }
1228
- }
1229
- return assignRef(ctx, node.i, serialized);
1230
- }
1231
- function serializeNullConstructor(ctx, node) {
1232
- let serialized = "Object.create(null)";
1233
- if (ctx.features & 64 /* ObjectAssign */) {
1234
- serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1235
- } else {
1236
- markRef(ctx, node.i);
1237
- const assignments = serializeAssignments(ctx, node.i, node.d);
1238
- if (assignments) {
1239
- const ref = getRefParam(ctx, node.i);
1240
- 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");
1241
1289
  }
1242
1290
  }
1243
- return assignRef(ctx, node.i, serialized);
1244
- }
1245
- function serializeTree(ctx, node) {
1246
- switch (node.t) {
1247
- case 0 /* Primitive */:
1248
- return String(node.s);
1249
- case 2 /* Reference */:
1250
- return getRefParam(ctx, node.i);
1251
- case 10 /* Promise */:
1252
- return serializePromise(ctx, node);
1253
- case 1 /* BigInt */:
1254
- return node.s;
1255
- case 3 /* Date */:
1256
- return assignRef(ctx, node.i, `new Date("${node.s}")`);
1257
- case 4 /* RegExp */:
1258
- return assignRef(ctx, node.i, node.s);
1259
- case 15 /* BigIntTypedArray */:
1260
- case 14 /* TypedArray */:
1261
- return serializeTypedArray(ctx, node);
1262
- case 5 /* Set */:
1263
- return serializeSet(ctx, node);
1264
- case 6 /* Map */:
1265
- return serializeMap(ctx, node);
1266
- case 7 /* Array */:
1267
- return serializeArray(ctx, node);
1268
- case 12 /* AggregateError */:
1269
- return serializeAggregateError(ctx, node);
1270
- case 11 /* Error */:
1271
- return serializeError(ctx, node);
1272
- case 13 /* Iterable */:
1273
- return serializeIterable(ctx, node);
1274
- case 9 /* NullConstructor */:
1275
- return serializeNullConstructor(ctx, node);
1276
- case 8 /* Object */:
1277
- return assignRef(ctx, node.i, serializeObject(ctx, node.i, node.d));
1278
- default:
1279
- throw new Error("Unsupported type");
1280
- }
1281
- }
1291
+ };
1282
1292
 
1283
1293
  // src/tree/sync.ts
1284
1294
  var SyncParser = class {
@@ -1312,12 +1322,12 @@ var SyncParser = class {
1312
1322
  return {
1313
1323
  t: 7 /* Array */,
1314
1324
  i: id,
1315
- a: this.generateNodeList(current),
1316
1325
  s: void 0,
1317
1326
  l: void 0,
1318
- m: void 0,
1319
1327
  c: void 0,
1328
+ m: void 0,
1320
1329
  d: void 0,
1330
+ a: this.generateNodeList(current),
1321
1331
  n: void 0
1322
1332
  };
1323
1333
  }
@@ -1348,12 +1358,12 @@ var SyncParser = class {
1348
1358
  return {
1349
1359
  t: 6 /* Map */,
1350
1360
  i: id,
1351
- a: void 0,
1352
1361
  s: void 0,
1353
1362
  l: void 0,
1354
- m: void 0,
1355
1363
  c: void 0,
1364
+ m: void 0,
1356
1365
  d: { k: keyNodes, v: valueNodes, s: len },
1366
+ a: void 0,
1357
1367
  n: void 0
1358
1368
  };
1359
1369
  }
@@ -1377,12 +1387,12 @@ var SyncParser = class {
1377
1387
  return {
1378
1388
  t: 5 /* Set */,
1379
1389
  i: id,
1380
- a: nodes,
1381
1390
  s: void 0,
1382
1391
  l: void 0,
1383
- m: void 0,
1384
1392
  c: void 0,
1393
+ m: void 0,
1385
1394
  d: void 0,
1395
+ a: nodes,
1386
1396
  n: void 0
1387
1397
  };
1388
1398
  }
@@ -1426,8 +1436,8 @@ var SyncParser = class {
1426
1436
  i: id,
1427
1437
  s: void 0,
1428
1438
  l: void 0,
1429
- m: void 0,
1430
1439
  c: void 0,
1440
+ m: void 0,
1431
1441
  // Parse options first before the items
1432
1442
  d: options ? this.generateProperties(options) : void 0,
1433
1443
  a: this.generateNodeList(Array.from(current)),
@@ -1443,8 +1453,8 @@ var SyncParser = class {
1443
1453
  i: id,
1444
1454
  s: void 0,
1445
1455
  l: void 0,
1446
- m: void 0,
1447
1456
  c: void 0,
1457
+ m: void 0,
1448
1458
  d: this.generateProperties(current),
1449
1459
  a: void 0,
1450
1460
  n: void 0
@@ -1456,12 +1466,12 @@ var SyncParser = class {
1456
1466
  return {
1457
1467
  t: 12 /* AggregateError */,
1458
1468
  i: id,
1459
- a: void 0,
1460
1469
  s: void 0,
1461
1470
  l: void 0,
1462
- m: current.message,
1463
1471
  c: void 0,
1472
+ m: current.message,
1464
1473
  d: optionsNode,
1474
+ a: void 0,
1465
1475
  n: this.parse(current.errors)
1466
1476
  };
1467
1477
  }
@@ -1471,12 +1481,12 @@ var SyncParser = class {
1471
1481
  return {
1472
1482
  t: 11 /* Error */,
1473
1483
  i: id,
1474
- a: void 0,
1475
1484
  s: void 0,
1476
1485
  l: void 0,
1477
- m: current.message,
1478
1486
  c: getErrorConstructor(current),
1487
+ m: current.message,
1479
1488
  d: optionsNode,
1489
+ a: void 0,
1480
1490
  n: void 0
1481
1491
  };
1482
1492
  }
@@ -1506,7 +1516,7 @@ var SyncParser = class {
1506
1516
  return NULL_NODE;
1507
1517
  }
1508
1518
  const id = createRef(this.ctx, current, true);
1509
- if (this.ctx.markedRefs[id]) {
1519
+ if (this.ctx.markedRefs.has(id)) {
1510
1520
  return createReferenceNode(id);
1511
1521
  }
1512
1522
  if (Array.isArray(current)) {
@@ -1605,14 +1615,14 @@ function serialize(source, options) {
1605
1615
  const ctx = createParserContext(options);
1606
1616
  const [tree, rootID, isObject] = parseSync(ctx, source);
1607
1617
  const serial = createSerializationContext(ctx);
1608
- const result = serializeTree(serial, tree);
1618
+ const result = new SerovalSerializer(serial).serialize(tree);
1609
1619
  return finalize(serial, rootID, isObject, result);
1610
1620
  }
1611
1621
  async function serializeAsync(source, options) {
1612
1622
  const ctx = createParserContext(options);
1613
1623
  const [tree, rootID, isObject] = await parseAsync(ctx, source);
1614
1624
  const serial = createSerializationContext(ctx);
1615
- const result = serializeTree(serial, tree);
1625
+ const result = new SerovalSerializer(serial).serialize(tree);
1616
1626
  return finalize(serial, rootID, isObject, result);
1617
1627
  }
1618
1628
  function deserialize(source) {
@@ -1621,33 +1631,33 @@ function deserialize(source) {
1621
1631
  function toJSON(source, options) {
1622
1632
  const ctx = createParserContext(options);
1623
1633
  const [tree, root, isObject] = parseSync(ctx, source);
1624
- return JSON.stringify([
1625
- tree,
1626
- root,
1627
- isObject,
1628
- ctx.features,
1629
- ctx.markedRefs
1630
- ]);
1634
+ return JSON.stringify({
1635
+ t: tree,
1636
+ r: root,
1637
+ i: isObject,
1638
+ f: ctx.features,
1639
+ m: Array.from(ctx.markedRefs)
1640
+ });
1631
1641
  }
1632
1642
  async function toJSONAsync(source, options) {
1633
1643
  const ctx = createParserContext(options);
1634
1644
  const [tree, root, isObject] = await parseAsync(ctx, source);
1635
- return JSON.stringify([
1636
- tree,
1637
- root,
1638
- isObject,
1639
- ctx.features,
1640
- ctx.markedRefs
1641
- ]);
1645
+ return JSON.stringify({
1646
+ t: tree,
1647
+ r: root,
1648
+ i: isObject,
1649
+ f: ctx.features,
1650
+ m: Array.from(ctx.markedRefs)
1651
+ });
1642
1652
  }
1643
1653
  function compileJSON(source) {
1644
1654
  const parsed = JSON.parse(source);
1645
1655
  const serial = createSerializationContext({
1646
- features: parsed[3],
1647
- markedRefs: parsed[4]
1656
+ features: parsed.f,
1657
+ markedRefs: parsed.m
1648
1658
  });
1649
- const result = serializeTree(serial, parsed[0]);
1650
- 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);
1651
1661
  }
1652
1662
  function fromJSON(source) {
1653
1663
  return deserialize(compileJSON(source));