seroval 0.4.0-alpha.3 → 0.4.0-alpha.4

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.
@@ -4,7 +4,7 @@ function isPrimitive(current) {
4
4
  return true;
5
5
  }
6
6
  const type = typeof current;
7
- return type === "number" || type === "string" || type === "bigint";
7
+ return type === "number" || type === "string";
8
8
  }
9
9
  function isPromise(obj) {
10
10
  return !!obj && (typeof obj === "object" || typeof obj === "function") && "then" in obj && typeof obj.then === "function";
@@ -39,7 +39,7 @@ function compareVersion(left, right) {
39
39
  }
40
40
  var VERSION_TABLE = {
41
41
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError#browser_compatibility
42
- [0 /* AggregateError */]: {
42
+ [1 /* AggregateError */]: {
43
43
  es: [2021, 0, 0],
44
44
  chrome: [85, 0, 0],
45
45
  edge: [85, 0, 0],
@@ -52,7 +52,7 @@ var VERSION_TABLE = {
52
52
  node: [15, 0, 0]
53
53
  },
54
54
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values#browser_compatibility
55
- [1 /* ArrayPrototypeValues */]: {
55
+ [2 /* ArrayPrototypeValues */]: {
56
56
  es: [6, 0, 0],
57
57
  chrome: [66, 0, 0],
58
58
  edge: [14, 0, 0],
@@ -65,7 +65,7 @@ var VERSION_TABLE = {
65
65
  node: [10, 9, 0]
66
66
  },
67
67
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#browser_compatibility
68
- [2 /* ArrowFunction */]: {
68
+ [4 /* ArrowFunction */]: {
69
69
  es: [6, 0, 0],
70
70
  chrome: [45, 0, 0],
71
71
  edge: [12, 0, 0],
@@ -78,7 +78,7 @@ var VERSION_TABLE = {
78
78
  node: [4, 0, 0]
79
79
  },
80
80
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility
81
- [3 /* BigInt */]: {
81
+ [8 /* BigInt */]: {
82
82
  es: [2020, 0, 0],
83
83
  chrome: [67, 0, 0],
84
84
  edge: [79, 0, 0],
@@ -91,7 +91,7 @@ var VERSION_TABLE = {
91
91
  node: [10, 4, 0]
92
92
  },
93
93
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#browser_compatibility
94
- [4 /* Map */]: {
94
+ [16 /* Map */]: {
95
95
  es: [6, 0, 0],
96
96
  chrome: [38, 0, 0],
97
97
  edge: [12, 0, 0],
@@ -104,7 +104,7 @@ var VERSION_TABLE = {
104
104
  node: [0, 12, 0]
105
105
  },
106
106
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#browser_compatibility
107
- [5 /* MethodShorthand */]: {
107
+ [32 /* MethodShorthand */]: {
108
108
  es: [6, 0, 0],
109
109
  chrome: [39, 0, 0],
110
110
  edge: [12, 0, 0],
@@ -117,7 +117,7 @@ var VERSION_TABLE = {
117
117
  node: [4, 0, 0]
118
118
  },
119
119
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#browser_compatibility
120
- [6 /* ObjectAssign */]: {
120
+ [64 /* ObjectAssign */]: {
121
121
  es: [6, 0, 0],
122
122
  chrome: [45, 0, 0],
123
123
  edge: [12, 0, 0],
@@ -130,7 +130,7 @@ var VERSION_TABLE = {
130
130
  node: [4, 0, 0]
131
131
  },
132
132
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#browser_compatibility
133
- [7 /* Promise */]: {
133
+ [128 /* Promise */]: {
134
134
  es: [6, 0, 0],
135
135
  chrome: [32, 0, 0],
136
136
  edge: [12, 0, 0],
@@ -143,7 +143,7 @@ var VERSION_TABLE = {
143
143
  node: [0, 12, 0]
144
144
  },
145
145
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#browser_compatibility
146
- [8 /* Set */]: {
146
+ [256 /* Set */]: {
147
147
  es: [6, 0, 0],
148
148
  chrome: [38, 0, 0],
149
149
  edge: [12, 0, 0],
@@ -156,7 +156,7 @@ var VERSION_TABLE = {
156
156
  node: [0, 12, 0]
157
157
  },
158
158
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator#browser_compatibility
159
- [9 /* SymbolIterator */]: {
159
+ [512 /* SymbolIterator */]: {
160
160
  es: [6, 0, 0],
161
161
  chrome: [43, 0, 0],
162
162
  edge: [12, 0, 0],
@@ -169,7 +169,7 @@ var VERSION_TABLE = {
169
169
  node: [0, 12, 0]
170
170
  },
171
171
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#browser_compatibility
172
- [10 /* TypedArray */]: {
172
+ [1024 /* TypedArray */]: {
173
173
  es: [6, 0, 0],
174
174
  chrome: [7, 0, 0],
175
175
  edge: [12, 0, 0],
@@ -180,6 +180,19 @@ var VERSION_TABLE = {
180
180
  samsung: [1, 0, 0],
181
181
  deno: [1, 0, 0],
182
182
  node: [0, 10, 0]
183
+ },
184
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array#browser_compatibility
185
+ [2048 /* BigIntTypedArray */]: {
186
+ es: [2020, 0, 0],
187
+ chrome: [67, 0, 0],
188
+ edge: [79, 0, 0],
189
+ firefox: [68, 0, 0],
190
+ opera: [54, 6, 0],
191
+ safari: [15, 1, 0],
192
+ ios: [15, 2, 0],
193
+ samsung: [9, 0, 0],
194
+ deno: [1, 0, 0],
195
+ node: [10, 4, 0]
183
196
  }
184
197
  };
185
198
  var TARGET_REGEX = /^(es|chrome|edge|safari|firefox|opera|ios|samsung|deno|node)([0-9]+(\.[0-9]+(\.[0-9]+)?)?)$/i;
@@ -202,7 +215,7 @@ function getTargetVersions(targets) {
202
215
  }
203
216
  function parseTargets(targets) {
204
217
  const parsed = getTargetVersions(targets);
205
- const flags = /* @__PURE__ */ new Set();
218
+ let flags = 0;
206
219
  for (const key in VERSION_TABLE) {
207
220
  const base = VERSION_TABLE[key];
208
221
  let flag = true;
@@ -211,7 +224,7 @@ function parseTargets(targets) {
211
224
  flag = flag && !!baseVersion && compareVersion(baseVersion, version) <= 0;
212
225
  }
213
226
  if (flag) {
214
- flags.add(Number(key));
227
+ flags |= Number(key);
215
228
  }
216
229
  }
217
230
  return flags;
@@ -280,16 +293,22 @@ function getIdentifier(index) {
280
293
  var DEFAULT_OPTIONS = {
281
294
  target: "es2023"
282
295
  };
283
- function createSerializationContext(options = {}) {
296
+ function createParserContext(options = {}) {
284
297
  const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
285
298
  return {
286
- markedRefs: [],
287
- stack: [],
299
+ markedRefs: /* @__PURE__ */ new Set(),
288
300
  refs: /* @__PURE__ */ new Map(),
301
+ features: parseTargets(result.target)
302
+ };
303
+ }
304
+ function createSerializationContext(options) {
305
+ return {
306
+ stack: [],
289
307
  vars: [],
290
308
  assignments: [],
291
309
  validRefs: /* @__PURE__ */ new Map(),
292
- features: parseTargets(result.target)
310
+ features: options.features,
311
+ markedRefs: new Set(options.markedRefs)
293
312
  };
294
313
  }
295
314
  function getAssignmentExpression(assignment) {
@@ -358,16 +377,7 @@ function resolvePatches(ctx) {
358
377
  return resolveAssignments(ctx.assignments);
359
378
  }
360
379
  function markRef(ctx, current) {
361
- ctx.markedRefs[current] = true;
362
- }
363
- function createRef(ctx, index) {
364
- const current = ctx.refs.get(index);
365
- if (current != null) {
366
- return current;
367
- }
368
- const id = ctx.refs.size;
369
- ctx.refs.set(index, id);
370
- return id;
380
+ ctx.markedRefs.add(current);
371
381
  }
372
382
  function createValidRef(ctx, index) {
373
383
  const current = ctx.validRefs.get(index);
@@ -388,11 +398,20 @@ function getRefParam(ctx, index) {
388
398
  return result;
389
399
  }
390
400
  function assignRef(ctx, index, value) {
391
- if (ctx.markedRefs[index]) {
401
+ if (ctx.markedRefs.has(index)) {
392
402
  return `${getRefParam(ctx, index)}=${value}`;
393
403
  }
394
404
  return value;
395
405
  }
406
+ function createRef(ctx, index) {
407
+ const current = ctx.refs.get(index);
408
+ if (current != null) {
409
+ return current;
410
+ }
411
+ const id = ctx.refs.size;
412
+ ctx.refs.set(index, id);
413
+ return id;
414
+ }
396
415
  function createAssignment(ctx, source, value) {
397
416
  ctx.assignments.push({
398
417
  type: "index",
@@ -421,13 +440,10 @@ function createArrayAssign(ctx, ref, index, value) {
421
440
  markRef(ctx, ref);
422
441
  createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
423
442
  }
424
- function createObjectIdentifierAssign(ctx, ref, key, value) {
443
+ function createObjectAssign(ctx, ref, key, value, computed) {
425
444
  markRef(ctx, ref);
426
- createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
427
- }
428
- function createObjectComputedAssign(ctx, ref, key, value) {
429
- markRef(ctx, ref);
430
- createAssignment(ctx, `${getRefParam(ctx, ref)}[${key}]`, value);
445
+ const member = computed ? `[${key}]` : `.${key}`;
446
+ createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
431
447
  }
432
448
  function getErrorConstructor(error) {
433
449
  if (error instanceof EvalError) {
@@ -479,75 +495,99 @@ function getIterableOptions(obj) {
479
495
  return void 0;
480
496
  }
481
497
  function isReferenceInStack(ctx, node) {
482
- return node[0] === 1 /* Reference */ && ctx.stack.includes(node[1]);
498
+ return node[0] === 2 /* Reference */ && ctx.stack.includes(node[1]);
483
499
  }
484
500
  function generateRef(ctx, current) {
485
501
  const ref = ctx.refs.has(current);
486
502
  if (ref) {
487
503
  const refID = ctx.refs.get(current) || 0;
488
504
  markRef(ctx, refID);
489
- return [1 /* Reference */, refID];
505
+ return [2 /* Reference */, refID];
490
506
  }
491
507
  const id = ctx.refs.size;
492
508
  ctx.refs.set(current, id);
493
509
  return id;
494
510
  }
495
511
  function generateSemiPrimitiveValue(ctx, current, id) {
512
+ if (typeof current === "bigint") {
513
+ assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
514
+ return [1 /* BigInt */, `${current}n`];
515
+ }
496
516
  if (constructorCheck(current, Date)) {
497
- return [2 /* Date */, current, id];
517
+ return [3 /* Date */, current.toISOString(), id];
498
518
  }
499
519
  if (constructorCheck(current, RegExp)) {
500
- return [3 /* RegExp */, current, id];
520
+ return [4 /* RegExp */, String(current), id];
501
521
  }
502
522
  if (constructorCheck(current, Int8Array)) {
503
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int8Array"');
504
- return [13 /* TypedArray */, ["Int8Array", current], id];
523
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int8Array"');
524
+ return [14 /* TypedArray */, ["Int8Array", current.toString(), current.byteOffset], id];
505
525
  }
506
526
  if (constructorCheck(current, Int16Array)) {
507
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int16Array"');
508
- return [13 /* TypedArray */, ["Int16Array", current], id];
527
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int16Array"');
528
+ return [14 /* TypedArray */, ["Int16Array", current.toString(), current.byteOffset], id];
509
529
  }
510
530
  if (constructorCheck(current, Int32Array)) {
511
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int32Array"');
512
- return [13 /* TypedArray */, ["Int32Array", current], id];
531
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int32Array"');
532
+ return [14 /* TypedArray */, ["Int32Array", current.toString(), current.byteOffset], id];
513
533
  }
514
534
  if (constructorCheck(current, Uint8Array)) {
515
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint8Array"');
516
- return [13 /* TypedArray */, ["Uint8Array", current], id];
535
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint8Array"');
536
+ return [14 /* TypedArray */, ["Uint8Array", current.toString(), current.byteOffset], id];
517
537
  }
518
538
  if (constructorCheck(current, Uint16Array)) {
519
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint16Array"');
520
- return [13 /* TypedArray */, ["Uint16Array", current], id];
539
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint16Array"');
540
+ return [14 /* TypedArray */, ["Uint16Array", current.toString(), current.byteOffset], id];
521
541
  }
522
542
  if (constructorCheck(current, Uint32Array)) {
523
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint32Array"');
524
- return [13 /* TypedArray */, ["Uint32Array", current], id];
543
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint32Array"');
544
+ return [14 /* TypedArray */, ["Uint32Array", current.toString(), current.byteOffset], id];
525
545
  }
526
546
  if (constructorCheck(current, Uint8ClampedArray)) {
527
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint8ClampedArray"');
528
- return [13 /* TypedArray */, ["Uint8ClampedArray", current], id];
547
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint8ClampedArray"');
548
+ return [14 /* TypedArray */, ["Uint8ClampedArray", current.toString(), current.byteOffset], id];
529
549
  }
530
550
  if (constructorCheck(current, Float32Array)) {
531
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Float32Array"');
532
- return [13 /* TypedArray */, ["Float32Array", current], id];
551
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float32Array"');
552
+ return [14 /* TypedArray */, ["Float32Array", current.toString(), current.byteOffset], id];
533
553
  }
534
554
  if (constructorCheck(current, Float64Array)) {
535
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Float64Array"');
536
- return [13 /* TypedArray */, ["Float64Array", current], id];
555
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float64Array"');
556
+ return [14 /* TypedArray */, ["Float64Array", current.toString(), current.byteOffset], id];
537
557
  }
538
558
  if (constructorCheck(current, BigInt64Array)) {
539
559
  assert(
540
- ctx.features.has(10 /* TypedArray */) && ctx.features.has(3 /* BigInt */),
560
+ ctx.features & 2048 /* BigIntTypedArray */,
541
561
  'Unsupported value type "BigInt64Array"'
542
562
  );
543
- return [13 /* TypedArray */, ["BigInt64Array", current], id];
563
+ let result = "";
564
+ const cap = current.length - 1;
565
+ for (let i = 0; i < cap; i++) {
566
+ result += `${current[i]}n,`;
567
+ }
568
+ result += `"${current[cap]}"`;
569
+ return [
570
+ 15 /* BigIntTypedArray */,
571
+ ["BigInt64Array", result, current.byteOffset],
572
+ id
573
+ ];
544
574
  }
545
575
  if (constructorCheck(current, BigUint64Array)) {
546
576
  assert(
547
- ctx.features.has(10 /* TypedArray */) && ctx.features.has(3 /* BigInt */),
577
+ ctx.features & 2048 /* BigIntTypedArray */,
548
578
  'Unsupported value type "BigUint64Array"'
549
579
  );
550
- return [13 /* TypedArray */, ["BigUint64Array", current], id];
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],
589
+ id
590
+ ];
551
591
  }
552
592
  return void 0;
553
593
  }
@@ -579,7 +619,7 @@ function serializePropertiesSync(ctx, properties) {
579
619
  }
580
620
  function generateTreeSync(ctx, current) {
581
621
  if (isPrimitive(current)) {
582
- return [0 /* Primitive */, current];
622
+ return [0 /* Primitive */, serializePrimitive(current)];
583
623
  }
584
624
  const id = generateRef(ctx, current);
585
625
  if (Array.isArray(id)) {
@@ -590,7 +630,7 @@ function generateTreeSync(ctx, current) {
590
630
  return semiPrimitive;
591
631
  }
592
632
  if (constructorCheck(current, Set)) {
593
- assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
633
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
594
634
  const len = current.size;
595
635
  const nodes = new Array(len);
596
636
  const deferred = new Array(len);
@@ -606,10 +646,10 @@ function generateTreeSync(ctx, current) {
606
646
  for (let i = 0; i < deferredSize; i++) {
607
647
  nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
608
648
  }
609
- return [4 /* Set */, nodes, id];
649
+ return [5 /* Set */, nodes, id];
610
650
  }
611
651
  if (constructorCheck(current, Map)) {
612
- assert(ctx.features.has(4 /* Map */), 'Unsupported type "Map"');
652
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
613
653
  const len = current.size;
614
654
  const keyNodes = new Array(len);
615
655
  const valueNodes = new Array(len);
@@ -632,7 +672,7 @@ function generateTreeSync(ctx, current) {
632
672
  keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
633
673
  valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
634
674
  }
635
- return [5 /* Map */, [keyNodes, valueNodes, len], id];
675
+ return [6 /* Map */, [keyNodes, valueNodes, len], id];
636
676
  }
637
677
  if (Array.isArray(current)) {
638
678
  const size = current.length;
@@ -652,27 +692,27 @@ function generateTreeSync(ctx, current) {
652
692
  nodes[i] = generateTreeSync(ctx, deferred[i]);
653
693
  }
654
694
  }
655
- return [6 /* Array */, nodes, id];
695
+ return [7 /* Array */, nodes, id];
656
696
  }
657
- if (current instanceof AggregateError && ctx.features.has(0 /* AggregateError */)) {
697
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
658
698
  const options = getErrorOptions(current);
659
699
  const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
660
700
  const errorsNode = generateTreeSync(ctx, current.errors);
661
- return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
701
+ return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
662
702
  }
663
703
  if (current instanceof Error) {
664
704
  const options = getErrorOptions(current);
665
705
  const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
666
706
  return [
667
- 10 /* Error */,
707
+ 11 /* Error */,
668
708
  [getErrorConstructor(current), current.message, optionsNode],
669
709
  id
670
710
  ];
671
711
  }
672
712
  if (isIterable(current)) {
673
- assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
713
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
674
714
  const options = getIterableOptions(current);
675
- return [12 /* Iterable */, [
715
+ return [13 /* Iterable */, [
676
716
  // Parse options first before the items
677
717
  options ? serializePropertiesSync(ctx, options) : void 0,
678
718
  generateTreeSync(ctx, Array.from(current))
@@ -681,13 +721,17 @@ function generateTreeSync(ctx, current) {
681
721
  const empty = current.constructor == null;
682
722
  if (current.constructor === Object || empty) {
683
723
  return [
684
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
724
+ empty ? 9 /* NullConstructor */ : 8 /* Object */,
685
725
  serializePropertiesSync(ctx, current),
686
726
  id
687
727
  ];
688
728
  }
689
729
  throw new Error("Unsupported value");
690
730
  }
731
+ function parseSync(ctx, current) {
732
+ const result = generateTreeSync(ctx, current);
733
+ return [result, createRef(ctx, current), result[0] === 8 /* Object */];
734
+ }
691
735
  async function serializePropertiesAsync(ctx, properties) {
692
736
  const keys = Object.keys(properties);
693
737
  const size = keys.length;
@@ -716,7 +760,7 @@ async function serializePropertiesAsync(ctx, properties) {
716
760
  }
717
761
  async function generateTreeAsync(ctx, current) {
718
762
  if (isPrimitive(current)) {
719
- return [0 /* Primitive */, current];
763
+ return [0 /* Primitive */, serializePrimitive(current)];
720
764
  }
721
765
  const id = generateRef(ctx, current);
722
766
  if (Array.isArray(id)) {
@@ -727,15 +771,15 @@ async function generateTreeAsync(ctx, current) {
727
771
  return semiPrimitive;
728
772
  }
729
773
  if (isPromise(current)) {
730
- assert(ctx.features.has(7 /* Promise */), 'Unsupported type "Promise"');
774
+ assert(ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
731
775
  return current.then(async (value) => [
732
- 9 /* Promise */,
776
+ 10 /* Promise */,
733
777
  await generateTreeAsync(ctx, value),
734
778
  id
735
779
  ]);
736
780
  }
737
781
  if (constructorCheck(current, Set)) {
738
- assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
782
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
739
783
  const len = current.size;
740
784
  const nodes = new Array(len);
741
785
  const deferred = new Array(len);
@@ -751,10 +795,10 @@ async function generateTreeAsync(ctx, current) {
751
795
  for (let i = 0; i < deferredSize; i++) {
752
796
  nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
753
797
  }
754
- return [4 /* Set */, nodes, id];
798
+ return [5 /* Set */, nodes, id];
755
799
  }
756
800
  if (constructorCheck(current, Map)) {
757
- assert(ctx.features.has(4 /* Map */), 'Unsupported type "Map"');
801
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
758
802
  const len = current.size;
759
803
  const keyNodes = new Array(len);
760
804
  const valueNodes = new Array(len);
@@ -777,7 +821,7 @@ async function generateTreeAsync(ctx, current) {
777
821
  keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
778
822
  valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
779
823
  }
780
- return [5 /* Map */, [keyNodes, valueNodes, len], id];
824
+ return [6 /* Map */, [keyNodes, valueNodes, len], id];
781
825
  }
782
826
  if (Array.isArray(current)) {
783
827
  const size = current.length;
@@ -798,27 +842,27 @@ async function generateTreeAsync(ctx, current) {
798
842
  nodes[i] = await generateTreeAsync(ctx, deferred[i]);
799
843
  }
800
844
  }
801
- return [6 /* Array */, nodes, id];
845
+ return [7 /* Array */, nodes, id];
802
846
  }
803
- if (current instanceof AggregateError) {
847
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
804
848
  const options = getErrorOptions(current);
805
849
  const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
806
850
  const errorsNode = await generateTreeAsync(ctx, current.errors);
807
- return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
851
+ return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
808
852
  }
809
853
  if (current instanceof Error) {
810
854
  const options = getErrorOptions(current);
811
855
  const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
812
856
  return [
813
- 10 /* Error */,
857
+ 11 /* Error */,
814
858
  [getErrorConstructor(current), current.message, optionsNode],
815
859
  id
816
860
  ];
817
861
  }
818
862
  if (isIterable(current)) {
819
- assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
863
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
820
864
  const options = getIterableOptions(current);
821
- return [12 /* Iterable */, [
865
+ return [13 /* Iterable */, [
822
866
  options ? await serializePropertiesAsync(ctx, options) : void 0,
823
867
  await generateTreeAsync(ctx, Array.from(current))
824
868
  ], id];
@@ -826,14 +870,18 @@ async function generateTreeAsync(ctx, current) {
826
870
  const empty = current.constructor == null;
827
871
  if (current.constructor === Object || empty) {
828
872
  return [
829
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
873
+ empty ? 9 /* NullConstructor */ : 8 /* Object */,
830
874
  await serializePropertiesAsync(ctx, current),
831
875
  id
832
876
  ];
833
877
  }
834
878
  throw new Error("Unsupported value");
835
879
  }
836
- function serializePrimitive(ctx, value) {
880
+ async function parseAsync(ctx, current) {
881
+ const result = await generateTreeAsync(ctx, current);
882
+ return [result, createRef(ctx, current), result[0] === 8 /* Object */];
883
+ }
884
+ function serializePrimitive(value) {
837
885
  if (value === true) {
838
886
  return "!0";
839
887
  }
@@ -844,11 +892,7 @@ function serializePrimitive(ctx, value) {
844
892
  return "void 0";
845
893
  }
846
894
  if (value === null) {
847
- return "null";
848
- }
849
- if (typeof value === "bigint") {
850
- assert(ctx.features.has(3 /* BigInt */), 'Unsupported type "BigInt"');
851
- return `${value}n`;
895
+ return null;
852
896
  }
853
897
  if (typeof value === "string") {
854
898
  return quote(value);
@@ -862,7 +906,7 @@ function serializePrimitive(ctx, value) {
862
906
  if (Object.is(value, -Infinity)) {
863
907
  return "-1/0";
864
908
  }
865
- return String(value);
909
+ return value;
866
910
  }
867
911
  var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
868
912
  function serializeAssignments(ctx, targetRef, [keys, values, size]) {
@@ -877,15 +921,14 @@ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
877
921
  const check = Number(key);
878
922
  const parentAssignment = ctx.assignments;
879
923
  ctx.assignments = mainAssignments;
880
- if (IDENTIFIER_CHECK.test(key) || check >= 0) {
881
- if (!Number.isNaN(check)) {
882
- createObjectComputedAssign(ctx, targetRef, key, refParam);
883
- } else {
884
- createObjectIdentifierAssign(ctx, targetRef, key, refParam);
885
- }
886
- } else {
887
- createObjectComputedAssign(ctx, targetRef, quote(key), refParam);
888
- }
924
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
925
+ createObjectAssign(
926
+ ctx,
927
+ targetRef,
928
+ isIdentifier ? key : quote(key),
929
+ refParam,
930
+ !(isIdentifier && Number.isNaN(check))
931
+ );
889
932
  ctx.assignments = parentAssignment;
890
933
  }
891
934
  ctx.stack.pop();
@@ -898,22 +941,19 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
898
941
  const key = keys[i];
899
942
  const val = values[i];
900
943
  const check = Number(key);
901
- const isIdentifier = IDENTIFIER_CHECK.test(key) || check >= 0;
944
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
945
+ const validKey = isIdentifier ? key : quote(key);
902
946
  if (isReferenceInStack(ctx, val)) {
903
947
  const refParam = getRefParam(ctx, val[1]);
904
- if (isIdentifier) {
905
- if (!Number.isNaN(check)) {
906
- createObjectComputedAssign(ctx, sourceID, key, refParam);
907
- } else {
908
- createObjectIdentifierAssign(ctx, sourceID, key, refParam);
909
- }
910
- } else {
911
- createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
912
- }
913
- } else if (isIdentifier) {
914
- result += `${key}:${serializeTree(ctx, val)},`;
948
+ createObjectAssign(
949
+ ctx,
950
+ sourceID,
951
+ validKey,
952
+ refParam,
953
+ !(isIdentifier && Number.isNaN(check))
954
+ );
915
955
  } else {
916
- result += `${quote(key)}:${serializeTree(ctx, val)},`;
956
+ result += `${validKey}:${serializeTree(ctx, val)},`;
917
957
  }
918
958
  }
919
959
  ctx.stack.pop();
@@ -922,10 +962,10 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
922
962
  function serializeTree(ctx, [type, value, id]) {
923
963
  switch (type) {
924
964
  case 0 /* Primitive */:
925
- return serializePrimitive(ctx, value);
926
- case 1 /* Reference */:
965
+ return String(value);
966
+ case 2 /* Reference */:
927
967
  return getRefParam(ctx, value);
928
- case 9 /* Promise */: {
968
+ case 10 /* Promise */: {
929
969
  let serialized;
930
970
  if (isReferenceInStack(ctx, value)) {
931
971
  serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
@@ -937,22 +977,21 @@ function serializeTree(ctx, [type, value, id]) {
937
977
  }
938
978
  return assignRef(ctx, id, serialized);
939
979
  }
940
- case 2 /* Date */:
941
- return assignRef(ctx, id, `new Date("${value.toISOString()}")`);
942
- case 3 /* RegExp */:
943
- return assignRef(ctx, id, String(value));
944
- case 13 /* TypedArray */: {
945
- let values = "";
946
- for (let i = 0, len = value[1].length; i < len; i++) {
947
- values += `${serializePrimitive(ctx, value[1][i])},`;
948
- }
949
- let args = values ? `[${values.substring(0, values.length - 1)}]` : "[]";
950
- if (value[1].byteOffset !== 0) {
951
- args += `,${value[1].byteOffset}`;
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]}`;
952
991
  }
953
992
  return assignRef(ctx, id, `new ${value[0]}(${args})`);
954
993
  }
955
- case 4 /* Set */: {
994
+ case 5 /* Set */: {
956
995
  let serialized = "new Set";
957
996
  const size = value.length;
958
997
  if (size) {
@@ -973,7 +1012,7 @@ function serializeTree(ctx, [type, value, id]) {
973
1012
  }
974
1013
  return assignRef(ctx, id, serialized);
975
1014
  }
976
- case 5 /* Map */: {
1015
+ case 6 /* Map */: {
977
1016
  let serialized = "new Map";
978
1017
  const size = value[2];
979
1018
  if (size) {
@@ -1010,13 +1049,13 @@ function serializeTree(ctx, [type, value, id]) {
1010
1049
  }
1011
1050
  return assignRef(ctx, id, serialized);
1012
1051
  }
1013
- case 6 /* Array */: {
1052
+ case 7 /* Array */: {
1014
1053
  const size = value.length;
1015
1054
  let values = "";
1016
1055
  ctx.stack.push(id);
1017
1056
  for (let i = 0; i < size; i++) {
1018
1057
  const item = value[i];
1019
- if (i in value) {
1058
+ if (item) {
1020
1059
  if (isReferenceInStack(ctx, item)) {
1021
1060
  createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
1022
1061
  values += ",";
@@ -1033,12 +1072,12 @@ function serializeTree(ctx, [type, value, id]) {
1033
1072
  ctx.stack.pop();
1034
1073
  return assignRef(ctx, id, `[${values}]`);
1035
1074
  }
1036
- case 11 /* AggregateError */: {
1075
+ case 12 /* AggregateError */: {
1037
1076
  ctx.stack.push(id);
1038
1077
  let serialized = `new AggregateError(${serializeTree(ctx, value[2])},${quote(value[0])})`;
1039
1078
  ctx.stack.pop();
1040
1079
  if (value[1]) {
1041
- if (ctx.features.has(6 /* ObjectAssign */)) {
1080
+ if (ctx.features & 64 /* ObjectAssign */) {
1042
1081
  const options = serializeObject(ctx, id, value[1]);
1043
1082
  serialized = `Object.assign(${serialized},${options})`;
1044
1083
  } else {
@@ -1052,10 +1091,10 @@ function serializeTree(ctx, [type, value, id]) {
1052
1091
  }
1053
1092
  return assignRef(ctx, id, serialized);
1054
1093
  }
1055
- case 10 /* Error */: {
1094
+ case 11 /* Error */: {
1056
1095
  let serialized = `new ${value[0]}(${quote(value[1])})`;
1057
1096
  if (value[2]) {
1058
- if (ctx.features.has(6 /* ObjectAssign */)) {
1097
+ if (ctx.features & 64 /* ObjectAssign */) {
1059
1098
  const options = serializeObject(ctx, id, value[2]);
1060
1099
  serialized = `Object.assign(${serialized},${options})`;
1061
1100
  } else {
@@ -1069,26 +1108,26 @@ function serializeTree(ctx, [type, value, id]) {
1069
1108
  }
1070
1109
  return assignRef(ctx, id, serialized);
1071
1110
  }
1072
- case 12 /* Iterable */: {
1111
+ case 13 /* Iterable */: {
1073
1112
  const parent = ctx.stack;
1074
1113
  ctx.stack = [];
1075
1114
  const values = serializeTree(ctx, value[1]);
1076
1115
  ctx.stack = parent;
1077
1116
  let serialized;
1078
- if (ctx.features.has(1 /* ArrayPrototypeValues */)) {
1117
+ if (ctx.features & 2 /* ArrayPrototypeValues */) {
1079
1118
  serialized = `${values}.values()`;
1080
1119
  } else {
1081
1120
  serialized = `${values}[Symbol.iterator]()`;
1082
1121
  }
1083
- if (ctx.features.has(2 /* ArrowFunction */)) {
1122
+ if (ctx.features & 4 /* ArrowFunction */) {
1084
1123
  serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1085
- } else if (ctx.features.has(5 /* MethodShorthand */)) {
1124
+ } else if (ctx.features & 32 /* MethodShorthand */) {
1086
1125
  serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1087
1126
  } else {
1088
1127
  serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1089
1128
  }
1090
1129
  if (value[0]) {
1091
- if (ctx.features.has(6 /* ObjectAssign */)) {
1130
+ if (ctx.features & 64 /* ObjectAssign */) {
1092
1131
  const options = serializeObject(ctx, id, value[0]);
1093
1132
  serialized = `Object.assign(${serialized},${options})`;
1094
1133
  } else {
@@ -1102,9 +1141,9 @@ function serializeTree(ctx, [type, value, id]) {
1102
1141
  }
1103
1142
  return assignRef(ctx, id, serialized);
1104
1143
  }
1105
- case 8 /* NullConstructor */: {
1144
+ case 9 /* NullConstructor */: {
1106
1145
  let serialized = "Object.create(null)";
1107
- if (ctx.features.has(6 /* ObjectAssign */)) {
1146
+ if (ctx.features & 64 /* ObjectAssign */) {
1108
1147
  const fields = serializeObject(ctx, id, value);
1109
1148
  if (fields !== "{}") {
1110
1149
  serialized = `Object.assign(${serialized},${fields})`;
@@ -1119,7 +1158,7 @@ function serializeTree(ctx, [type, value, id]) {
1119
1158
  }
1120
1159
  return assignRef(ctx, id, serialized);
1121
1160
  }
1122
- case 7 /* Object */:
1161
+ case 8 /* Object */:
1123
1162
  return assignRef(ctx, id, serializeObject(ctx, id, value));
1124
1163
  default:
1125
1164
  throw new Error("Unsupported type");
@@ -1127,12 +1166,12 @@ function serializeTree(ctx, [type, value, id]) {
1127
1166
  }
1128
1167
 
1129
1168
  // src/index.ts
1130
- function finalize(ctx, source, result) {
1169
+ function finalize(ctx, rootID, isObject, result) {
1131
1170
  if (ctx.vars.length) {
1132
1171
  const patches = resolvePatches(ctx);
1133
1172
  let body = result;
1134
1173
  if (patches) {
1135
- const index = getRefParam(ctx, createRef(ctx, source));
1174
+ const index = getRefParam(ctx, rootID);
1136
1175
  if (result.startsWith(`${index}=`)) {
1137
1176
  body = `${result},${patches}${index}`;
1138
1177
  } else {
@@ -1140,43 +1179,83 @@ function finalize(ctx, source, result) {
1140
1179
  }
1141
1180
  }
1142
1181
  let params = ctx.vars.length > 1 ? ctx.vars.join(",") : ctx.vars[0];
1143
- if (ctx.features.has(2 /* ArrowFunction */)) {
1182
+ if (ctx.features & 4 /* ArrowFunction */) {
1144
1183
  params = ctx.vars.length > 1 || ctx.vars.length === 0 ? `(${params})` : params;
1145
1184
  return `(${params}=>(${body}))()`;
1146
1185
  }
1147
1186
  return `(function(${params}){return ${body}})()`;
1148
1187
  }
1149
- if (source.constructor === Object) {
1188
+ if (isObject) {
1150
1189
  return `(${result})`;
1151
1190
  }
1152
1191
  return result;
1153
1192
  }
1154
1193
  function serialize(source, options) {
1155
- const ctx = createSerializationContext(options);
1194
+ const ctx = createParserContext(options);
1156
1195
  if (isPrimitive(source)) {
1157
- return serializePrimitive(ctx, source);
1196
+ return String(serializePrimitive(source));
1158
1197
  }
1159
- const tree = generateTreeSync(ctx, source);
1160
- const result = serializeTree(ctx, tree);
1161
- return finalize(ctx, source, result);
1198
+ const [tree, rootID, isObject] = parseSync(ctx, source);
1199
+ const serial = createSerializationContext(ctx);
1200
+ const result = serializeTree(serial, tree);
1201
+ return finalize(serial, rootID, isObject, result);
1162
1202
  }
1163
1203
  async function serializeAsync(source, options) {
1164
- const ctx = createSerializationContext(options);
1204
+ const ctx = createParserContext(options);
1165
1205
  if (isPrimitive(source)) {
1166
- return serializePrimitive(ctx, source);
1206
+ return String(serializePrimitive(source));
1167
1207
  }
1168
- const tree = await generateTreeAsync(ctx, source);
1169
- const result = serializeTree(ctx, tree);
1170
- return finalize(ctx, source, result);
1208
+ const [tree, rootID, isObject] = await parseAsync(ctx, source);
1209
+ const serial = createSerializationContext(ctx);
1210
+ const result = serializeTree(serial, tree);
1211
+ return finalize(serial, rootID, isObject, result);
1171
1212
  }
1172
1213
  function deserialize(source) {
1173
1214
  return (0, eval)(source);
1174
1215
  }
1216
+ function toJSON(source, options) {
1217
+ const ctx = createParserContext(options);
1218
+ const [tree, root, isObject] = parseSync(ctx, source);
1219
+ return JSON.stringify([
1220
+ tree,
1221
+ root,
1222
+ isObject,
1223
+ ctx.features,
1224
+ [...ctx.markedRefs]
1225
+ ]);
1226
+ }
1227
+ async function toJSONAsync(source, options) {
1228
+ const ctx = createParserContext(options);
1229
+ const [tree, root, isObject] = await parseAsync(ctx, source);
1230
+ return JSON.stringify([
1231
+ tree,
1232
+ root,
1233
+ isObject,
1234
+ ctx.features,
1235
+ [...ctx.markedRefs]
1236
+ ]);
1237
+ }
1238
+ function compileJSON(source) {
1239
+ const parsed = JSON.parse(source);
1240
+ const serial = createSerializationContext({
1241
+ features: parsed[3],
1242
+ markedRefs: parsed[4]
1243
+ });
1244
+ const result = serializeTree(serial, parsed[0]);
1245
+ return finalize(serial, parsed[1], parsed[2], result);
1246
+ }
1247
+ function fromJSON(source) {
1248
+ return deserialize(compileJSON(source));
1249
+ }
1175
1250
  var src_default = serialize;
1176
1251
  export {
1252
+ compileJSON,
1177
1253
  src_default as default,
1178
1254
  deserialize,
1255
+ fromJSON,
1179
1256
  serialize,
1180
- serializeAsync
1257
+ serializeAsync,
1258
+ toJSON,
1259
+ toJSONAsync
1181
1260
  };
1182
1261
  //# sourceMappingURL=index.mjs.map