seroval 0.4.0-alpha.2 → 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) {
425
- markRef(ctx, ref);
426
- createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
427
- }
428
- function createObjectComputedAssign(ctx, ref, key, value) {
443
+ function createObjectAssign(ctx, ref, key, value, computed) {
429
444
  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,104 +495,131 @@ 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
  }
554
594
  function serializePropertiesSync(ctx, properties) {
555
- const keyNodes = [];
556
- const valueNodes = [];
557
- const deferredKeys = [];
558
- const deferredValues = [];
559
595
  const keys = Object.keys(properties);
596
+ const size = keys.length;
597
+ const keyNodes = new Array(size);
598
+ const valueNodes = new Array(size);
599
+ const deferredKeys = new Array(size);
600
+ const deferredValues = new Array(size);
560
601
  let deferredSize = 0;
602
+ let nodesSize = 0;
561
603
  for (const key of keys) {
562
604
  if (isIterable(properties[key])) {
563
- deferredKeys.push(key);
564
- deferredValues.push(properties[key]);
605
+ deferredKeys[deferredSize] = key;
606
+ deferredValues[deferredSize] = properties[key];
565
607
  deferredSize++;
566
608
  } else {
567
- keyNodes.push(key);
568
- valueNodes.push(generateTreeSync(ctx, properties[key]));
609
+ keyNodes[nodesSize] = key;
610
+ valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
611
+ nodesSize++;
569
612
  }
570
613
  }
571
614
  for (let i = 0; i < deferredSize; i++) {
572
- keyNodes.push(deferredKeys[i]);
573
- valueNodes.push(generateTreeSync(ctx, deferredValues[i]));
615
+ keyNodes[nodesSize + i] = deferredKeys[i];
616
+ valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
574
617
  }
575
- return [keyNodes, valueNodes, keys.length];
618
+ return [keyNodes, valueNodes, size];
576
619
  }
577
620
  function generateTreeSync(ctx, current) {
578
621
  if (isPrimitive(current)) {
579
- return [0 /* Primitive */, current];
622
+ return [0 /* Primitive */, serializePrimitive(current)];
580
623
  }
581
624
  const id = generateRef(ctx, current);
582
625
  if (Array.isArray(id)) {
@@ -587,43 +630,49 @@ function generateTreeSync(ctx, current) {
587
630
  return semiPrimitive;
588
631
  }
589
632
  if (constructorCheck(current, Set)) {
590
- assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
591
- const nodes = [];
592
- const deferred = [];
633
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
634
+ const len = current.size;
635
+ const nodes = new Array(len);
636
+ const deferred = new Array(len);
637
+ let deferredSize = 0;
638
+ let nodeSize = 0;
593
639
  for (const item of current.keys()) {
594
640
  if (isIterable(item)) {
595
- deferred.push(item);
641
+ deferred[deferredSize++] = item;
596
642
  } else {
597
- nodes.push(generateTreeSync(ctx, item));
643
+ nodes[nodeSize++] = generateTreeSync(ctx, item);
598
644
  }
599
645
  }
600
- for (let i = 0, len = deferred.length; i < len; i++) {
601
- nodes.push(generateTreeSync(ctx, deferred[i]));
646
+ for (let i = 0; i < deferredSize; i++) {
647
+ nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
602
648
  }
603
- return [4 /* Set */, nodes, id];
649
+ return [5 /* Set */, nodes, id];
604
650
  }
605
651
  if (constructorCheck(current, Map)) {
606
- assert(ctx.features.has(4 /* Map */), 'Unsupported type "Map"');
607
- const keyNodes = [];
608
- const valueNodes = [];
609
- const deferredKey = [];
610
- const deferredValue = [];
652
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
653
+ const len = current.size;
654
+ const keyNodes = new Array(len);
655
+ const valueNodes = new Array(len);
656
+ const deferredKey = new Array(len);
657
+ const deferredValue = new Array(len);
611
658
  let deferredSize = 0;
659
+ let nodeSize = 0;
612
660
  for (const [key, value] of current.entries()) {
613
661
  if (isIterable(key) || isIterable(value)) {
614
- deferredKey.push(key);
615
- deferredValue.push(value);
662
+ deferredKey[deferredSize] = key;
663
+ deferredValue[deferredSize] = value;
616
664
  deferredSize++;
617
665
  } else {
618
- keyNodes.push(generateTreeSync(ctx, key));
619
- valueNodes.push(generateTreeSync(ctx, value));
666
+ keyNodes[nodeSize] = generateTreeSync(ctx, key);
667
+ valueNodes[nodeSize] = generateTreeSync(ctx, value);
668
+ nodeSize++;
620
669
  }
621
670
  }
622
671
  for (let i = 0; i < deferredSize; i++) {
623
- keyNodes.push(generateTreeSync(ctx, deferredKey[i]));
624
- valueNodes.push(generateTreeSync(ctx, deferredValue[i]));
672
+ keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
673
+ valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
625
674
  }
626
- return [5 /* Map */, [keyNodes, valueNodes, current.size], id];
675
+ return [6 /* Map */, [keyNodes, valueNodes, len], id];
627
676
  }
628
677
  if (Array.isArray(current)) {
629
678
  const size = current.length;
@@ -643,27 +692,27 @@ function generateTreeSync(ctx, current) {
643
692
  nodes[i] = generateTreeSync(ctx, deferred[i]);
644
693
  }
645
694
  }
646
- return [6 /* Array */, nodes, id];
695
+ return [7 /* Array */, nodes, id];
647
696
  }
648
- if (current instanceof AggregateError && ctx.features.has(0 /* AggregateError */)) {
697
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
649
698
  const options = getErrorOptions(current);
650
699
  const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
651
700
  const errorsNode = generateTreeSync(ctx, current.errors);
652
- return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
701
+ return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
653
702
  }
654
703
  if (current instanceof Error) {
655
704
  const options = getErrorOptions(current);
656
705
  const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
657
706
  return [
658
- 10 /* Error */,
707
+ 11 /* Error */,
659
708
  [getErrorConstructor(current), current.message, optionsNode],
660
709
  id
661
710
  ];
662
711
  }
663
712
  if (isIterable(current)) {
664
- assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
713
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
665
714
  const options = getIterableOptions(current);
666
- return [12 /* Iterable */, [
715
+ return [13 /* Iterable */, [
667
716
  // Parse options first before the items
668
717
  options ? serializePropertiesSync(ctx, options) : void 0,
669
718
  generateTreeSync(ctx, Array.from(current))
@@ -672,39 +721,46 @@ function generateTreeSync(ctx, current) {
672
721
  const empty = current.constructor == null;
673
722
  if (current.constructor === Object || empty) {
674
723
  return [
675
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
724
+ empty ? 9 /* NullConstructor */ : 8 /* Object */,
676
725
  serializePropertiesSync(ctx, current),
677
726
  id
678
727
  ];
679
728
  }
680
729
  throw new Error("Unsupported value");
681
730
  }
731
+ function parseSync(ctx, current) {
732
+ const result = generateTreeSync(ctx, current);
733
+ return [result, createRef(ctx, current), result[0] === 8 /* Object */];
734
+ }
682
735
  async function serializePropertiesAsync(ctx, properties) {
683
- const keyNodes = [];
684
- const valueNodes = [];
685
- const deferredKeys = [];
686
- const deferredValues = [];
687
736
  const keys = Object.keys(properties);
737
+ const size = keys.length;
738
+ const keyNodes = new Array(size);
739
+ const valueNodes = new Array(size);
740
+ const deferredKeys = new Array(size);
741
+ const deferredValues = new Array(size);
688
742
  let deferredSize = 0;
743
+ let nodesSize = 0;
689
744
  for (const key of keys) {
690
745
  if (isIterable(properties[key])) {
691
- deferredKeys.push(key);
692
- deferredValues.push(properties[key]);
746
+ deferredKeys[deferredSize] = key;
747
+ deferredValues[deferredSize] = properties[key];
693
748
  deferredSize++;
694
749
  } else {
695
- keyNodes.push(key);
696
- valueNodes.push(await generateTreeAsync(ctx, properties[key]));
750
+ keyNodes[nodesSize] = key;
751
+ valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
752
+ nodesSize++;
697
753
  }
698
754
  }
699
755
  for (let i = 0; i < deferredSize; i++) {
700
- keyNodes.push(deferredKeys[i]);
701
- valueNodes.push(await generateTreeAsync(ctx, deferredValues[i]));
756
+ keyNodes[nodesSize + i] = deferredKeys[i];
757
+ valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
702
758
  }
703
- return [keyNodes, valueNodes, keys.length];
759
+ return [keyNodes, valueNodes, size];
704
760
  }
705
761
  async function generateTreeAsync(ctx, current) {
706
762
  if (isPrimitive(current)) {
707
- return [0 /* Primitive */, current];
763
+ return [0 /* Primitive */, serializePrimitive(current)];
708
764
  }
709
765
  const id = generateRef(ctx, current);
710
766
  if (Array.isArray(id)) {
@@ -715,51 +771,57 @@ async function generateTreeAsync(ctx, current) {
715
771
  return semiPrimitive;
716
772
  }
717
773
  if (isPromise(current)) {
718
- assert(ctx.features.has(7 /* Promise */), 'Unsupported type "Promise"');
774
+ assert(ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
719
775
  return current.then(async (value) => [
720
- 9 /* Promise */,
776
+ 10 /* Promise */,
721
777
  await generateTreeAsync(ctx, value),
722
778
  id
723
779
  ]);
724
780
  }
725
781
  if (constructorCheck(current, Set)) {
726
- assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
727
- const nodes = [];
728
- const deferred = [];
782
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
783
+ const len = current.size;
784
+ const nodes = new Array(len);
785
+ const deferred = new Array(len);
786
+ let deferredSize = 0;
787
+ let nodeSize = 0;
729
788
  for (const item of current.keys()) {
730
789
  if (isIterable(item)) {
731
- deferred.push(item);
790
+ deferred[deferredSize++] = item;
732
791
  } else {
733
- nodes.push(await generateTreeAsync(ctx, item));
792
+ nodes[nodeSize++] = await generateTreeAsync(ctx, item);
734
793
  }
735
794
  }
736
- for (let i = 0, len = deferred.length; i < len; i++) {
737
- nodes.push(await generateTreeAsync(ctx, deferred[i]));
795
+ for (let i = 0; i < deferredSize; i++) {
796
+ nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
738
797
  }
739
- return [4 /* Set */, nodes, id];
798
+ return [5 /* Set */, nodes, id];
740
799
  }
741
800
  if (constructorCheck(current, Map)) {
742
- assert(ctx.features.has(4 /* Map */), 'Unsupported type "Map"');
743
- const keyNodes = [];
744
- const valueNodes = [];
745
- const deferredKey = [];
746
- const deferredValue = [];
801
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
802
+ const len = current.size;
803
+ const keyNodes = new Array(len);
804
+ const valueNodes = new Array(len);
805
+ const deferredKey = new Array(len);
806
+ const deferredValue = new Array(len);
747
807
  let deferredSize = 0;
808
+ let nodeSize = 0;
748
809
  for (const [key, value] of current.entries()) {
749
810
  if (isIterable(key) || isIterable(value)) {
750
- deferredKey.push(key);
751
- deferredValue.push(value);
811
+ deferredKey[deferredSize] = key;
812
+ deferredValue[deferredSize] = value;
752
813
  deferredSize++;
753
814
  } else {
754
- keyNodes.push(await generateTreeAsync(ctx, key));
755
- valueNodes.push(await generateTreeAsync(ctx, value));
815
+ keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
816
+ valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
817
+ nodeSize++;
756
818
  }
757
819
  }
758
820
  for (let i = 0; i < deferredSize; i++) {
759
- keyNodes.push(await generateTreeAsync(ctx, deferredKey[i]));
760
- valueNodes.push(await generateTreeAsync(ctx, deferredValue[i]));
821
+ keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
822
+ valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
761
823
  }
762
- return [5 /* Map */, [keyNodes, valueNodes, current.size], id];
824
+ return [6 /* Map */, [keyNodes, valueNodes, len], id];
763
825
  }
764
826
  if (Array.isArray(current)) {
765
827
  const size = current.length;
@@ -780,27 +842,27 @@ async function generateTreeAsync(ctx, current) {
780
842
  nodes[i] = await generateTreeAsync(ctx, deferred[i]);
781
843
  }
782
844
  }
783
- return [6 /* Array */, nodes, id];
845
+ return [7 /* Array */, nodes, id];
784
846
  }
785
- if (current instanceof AggregateError) {
847
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
786
848
  const options = getErrorOptions(current);
787
849
  const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
788
850
  const errorsNode = await generateTreeAsync(ctx, current.errors);
789
- return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
851
+ return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
790
852
  }
791
853
  if (current instanceof Error) {
792
854
  const options = getErrorOptions(current);
793
855
  const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
794
856
  return [
795
- 10 /* Error */,
857
+ 11 /* Error */,
796
858
  [getErrorConstructor(current), current.message, optionsNode],
797
859
  id
798
860
  ];
799
861
  }
800
862
  if (isIterable(current)) {
801
- assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
863
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
802
864
  const options = getIterableOptions(current);
803
- return [12 /* Iterable */, [
865
+ return [13 /* Iterable */, [
804
866
  options ? await serializePropertiesAsync(ctx, options) : void 0,
805
867
  await generateTreeAsync(ctx, Array.from(current))
806
868
  ], id];
@@ -808,14 +870,18 @@ async function generateTreeAsync(ctx, current) {
808
870
  const empty = current.constructor == null;
809
871
  if (current.constructor === Object || empty) {
810
872
  return [
811
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
873
+ empty ? 9 /* NullConstructor */ : 8 /* Object */,
812
874
  await serializePropertiesAsync(ctx, current),
813
875
  id
814
876
  ];
815
877
  }
816
878
  throw new Error("Unsupported value");
817
879
  }
818
- 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) {
819
885
  if (value === true) {
820
886
  return "!0";
821
887
  }
@@ -826,11 +892,7 @@ function serializePrimitive(ctx, value) {
826
892
  return "void 0";
827
893
  }
828
894
  if (value === null) {
829
- return "null";
830
- }
831
- if (typeof value === "bigint") {
832
- assert(ctx.features.has(3 /* BigInt */), 'Unsupported type "BigInt"');
833
- return `${value}n`;
895
+ return null;
834
896
  }
835
897
  if (typeof value === "string") {
836
898
  return quote(value);
@@ -844,7 +906,7 @@ function serializePrimitive(ctx, value) {
844
906
  if (Object.is(value, -Infinity)) {
845
907
  return "-1/0";
846
908
  }
847
- return String(value);
909
+ return value;
848
910
  }
849
911
  var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
850
912
  function serializeAssignments(ctx, targetRef, [keys, values, size]) {
@@ -859,15 +921,14 @@ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
859
921
  const check = Number(key);
860
922
  const parentAssignment = ctx.assignments;
861
923
  ctx.assignments = mainAssignments;
862
- if (check >= 0 || Number.isNaN(check) && IDENTIFIER_CHECK.test(key)) {
863
- if (!Number.isNaN(check)) {
864
- createObjectComputedAssign(ctx, targetRef, key, refParam);
865
- } else {
866
- createObjectIdentifierAssign(ctx, targetRef, key, refParam);
867
- }
868
- } else {
869
- createObjectComputedAssign(ctx, targetRef, quote(key), refParam);
870
- }
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
+ );
871
932
  ctx.assignments = parentAssignment;
872
933
  }
873
934
  ctx.stack.pop();
@@ -880,22 +941,19 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
880
941
  const key = keys[i];
881
942
  const val = values[i];
882
943
  const check = Number(key);
883
- if (check >= 0 || Number.isNaN(check) && IDENTIFIER_CHECK.test(key)) {
884
- if (isReferenceInStack(ctx, val)) {
885
- const refParam = getRefParam(ctx, val[1]);
886
- if (!Number.isNaN(check)) {
887
- createObjectComputedAssign(ctx, sourceID, key, refParam);
888
- } else {
889
- createObjectIdentifierAssign(ctx, sourceID, key, refParam);
890
- }
891
- } else {
892
- result += `${key}:${serializeTree(ctx, val)},`;
893
- }
894
- } else if (isReferenceInStack(ctx, val)) {
944
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
945
+ const validKey = isIdentifier ? key : quote(key);
946
+ if (isReferenceInStack(ctx, val)) {
895
947
  const refParam = getRefParam(ctx, val[1]);
896
- createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
948
+ createObjectAssign(
949
+ ctx,
950
+ sourceID,
951
+ validKey,
952
+ refParam,
953
+ !(isIdentifier && Number.isNaN(check))
954
+ );
897
955
  } else {
898
- result += `${quote(key)}:${serializeTree(ctx, val)},`;
956
+ result += `${validKey}:${serializeTree(ctx, val)},`;
899
957
  }
900
958
  }
901
959
  ctx.stack.pop();
@@ -904,10 +962,10 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
904
962
  function serializeTree(ctx, [type, value, id]) {
905
963
  switch (type) {
906
964
  case 0 /* Primitive */:
907
- return serializePrimitive(ctx, value);
908
- case 1 /* Reference */:
965
+ return String(value);
966
+ case 2 /* Reference */:
909
967
  return getRefParam(ctx, value);
910
- case 9 /* Promise */: {
968
+ case 10 /* Promise */: {
911
969
  let serialized;
912
970
  if (isReferenceInStack(ctx, value)) {
913
971
  serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
@@ -919,22 +977,21 @@ function serializeTree(ctx, [type, value, id]) {
919
977
  }
920
978
  return assignRef(ctx, id, serialized);
921
979
  }
922
- case 2 /* Date */:
923
- return assignRef(ctx, id, `new Date("${value.toISOString()}")`);
924
- case 3 /* RegExp */:
925
- return assignRef(ctx, id, String(value));
926
- case 13 /* TypedArray */: {
927
- let values = "";
928
- for (let i = 0, len = value[1].length; i < len; i++) {
929
- values += `${serializePrimitive(ctx, value[1][i])},`;
930
- }
931
- let args = values ? `[${values.substring(0, values.length - 1)}]` : "[]";
932
- if (value[1].byteOffset !== 0) {
933
- 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]}`;
934
991
  }
935
992
  return assignRef(ctx, id, `new ${value[0]}(${args})`);
936
993
  }
937
- case 4 /* Set */: {
994
+ case 5 /* Set */: {
938
995
  let serialized = "new Set";
939
996
  const size = value.length;
940
997
  if (size) {
@@ -955,7 +1012,7 @@ function serializeTree(ctx, [type, value, id]) {
955
1012
  }
956
1013
  return assignRef(ctx, id, serialized);
957
1014
  }
958
- case 5 /* Map */: {
1015
+ case 6 /* Map */: {
959
1016
  let serialized = "new Map";
960
1017
  const size = value[2];
961
1018
  if (size) {
@@ -992,13 +1049,13 @@ function serializeTree(ctx, [type, value, id]) {
992
1049
  }
993
1050
  return assignRef(ctx, id, serialized);
994
1051
  }
995
- case 6 /* Array */: {
1052
+ case 7 /* Array */: {
996
1053
  const size = value.length;
997
1054
  let values = "";
998
1055
  ctx.stack.push(id);
999
1056
  for (let i = 0; i < size; i++) {
1000
1057
  const item = value[i];
1001
- if (i in value) {
1058
+ if (item) {
1002
1059
  if (isReferenceInStack(ctx, item)) {
1003
1060
  createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
1004
1061
  values += ",";
@@ -1015,12 +1072,12 @@ function serializeTree(ctx, [type, value, id]) {
1015
1072
  ctx.stack.pop();
1016
1073
  return assignRef(ctx, id, `[${values}]`);
1017
1074
  }
1018
- case 11 /* AggregateError */: {
1075
+ case 12 /* AggregateError */: {
1019
1076
  ctx.stack.push(id);
1020
1077
  let serialized = `new AggregateError(${serializeTree(ctx, value[2])},${quote(value[0])})`;
1021
1078
  ctx.stack.pop();
1022
1079
  if (value[1]) {
1023
- if (ctx.features.has(6 /* ObjectAssign */)) {
1080
+ if (ctx.features & 64 /* ObjectAssign */) {
1024
1081
  const options = serializeObject(ctx, id, value[1]);
1025
1082
  serialized = `Object.assign(${serialized},${options})`;
1026
1083
  } else {
@@ -1034,10 +1091,10 @@ function serializeTree(ctx, [type, value, id]) {
1034
1091
  }
1035
1092
  return assignRef(ctx, id, serialized);
1036
1093
  }
1037
- case 10 /* Error */: {
1094
+ case 11 /* Error */: {
1038
1095
  let serialized = `new ${value[0]}(${quote(value[1])})`;
1039
1096
  if (value[2]) {
1040
- if (ctx.features.has(6 /* ObjectAssign */)) {
1097
+ if (ctx.features & 64 /* ObjectAssign */) {
1041
1098
  const options = serializeObject(ctx, id, value[2]);
1042
1099
  serialized = `Object.assign(${serialized},${options})`;
1043
1100
  } else {
@@ -1051,26 +1108,26 @@ function serializeTree(ctx, [type, value, id]) {
1051
1108
  }
1052
1109
  return assignRef(ctx, id, serialized);
1053
1110
  }
1054
- case 12 /* Iterable */: {
1111
+ case 13 /* Iterable */: {
1055
1112
  const parent = ctx.stack;
1056
1113
  ctx.stack = [];
1057
1114
  const values = serializeTree(ctx, value[1]);
1058
1115
  ctx.stack = parent;
1059
1116
  let serialized;
1060
- if (ctx.features.has(1 /* ArrayPrototypeValues */)) {
1117
+ if (ctx.features & 2 /* ArrayPrototypeValues */) {
1061
1118
  serialized = `${values}.values()`;
1062
1119
  } else {
1063
1120
  serialized = `${values}[Symbol.iterator]()`;
1064
1121
  }
1065
- if (ctx.features.has(2 /* ArrowFunction */)) {
1122
+ if (ctx.features & 4 /* ArrowFunction */) {
1066
1123
  serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1067
- } else if (ctx.features.has(5 /* MethodShorthand */)) {
1124
+ } else if (ctx.features & 32 /* MethodShorthand */) {
1068
1125
  serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1069
1126
  } else {
1070
1127
  serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1071
1128
  }
1072
1129
  if (value[0]) {
1073
- if (ctx.features.has(6 /* ObjectAssign */)) {
1130
+ if (ctx.features & 64 /* ObjectAssign */) {
1074
1131
  const options = serializeObject(ctx, id, value[0]);
1075
1132
  serialized = `Object.assign(${serialized},${options})`;
1076
1133
  } else {
@@ -1084,9 +1141,9 @@ function serializeTree(ctx, [type, value, id]) {
1084
1141
  }
1085
1142
  return assignRef(ctx, id, serialized);
1086
1143
  }
1087
- case 8 /* NullConstructor */: {
1144
+ case 9 /* NullConstructor */: {
1088
1145
  let serialized = "Object.create(null)";
1089
- if (ctx.features.has(6 /* ObjectAssign */)) {
1146
+ if (ctx.features & 64 /* ObjectAssign */) {
1090
1147
  const fields = serializeObject(ctx, id, value);
1091
1148
  if (fields !== "{}") {
1092
1149
  serialized = `Object.assign(${serialized},${fields})`;
@@ -1101,7 +1158,7 @@ function serializeTree(ctx, [type, value, id]) {
1101
1158
  }
1102
1159
  return assignRef(ctx, id, serialized);
1103
1160
  }
1104
- case 7 /* Object */:
1161
+ case 8 /* Object */:
1105
1162
  return assignRef(ctx, id, serializeObject(ctx, id, value));
1106
1163
  default:
1107
1164
  throw new Error("Unsupported type");
@@ -1109,12 +1166,12 @@ function serializeTree(ctx, [type, value, id]) {
1109
1166
  }
1110
1167
 
1111
1168
  // src/index.ts
1112
- function finalize(ctx, source, result) {
1169
+ function finalize(ctx, rootID, isObject, result) {
1113
1170
  if (ctx.vars.length) {
1114
1171
  const patches = resolvePatches(ctx);
1115
1172
  let body = result;
1116
1173
  if (patches) {
1117
- const index = getRefParam(ctx, createRef(ctx, source));
1174
+ const index = getRefParam(ctx, rootID);
1118
1175
  if (result.startsWith(`${index}=`)) {
1119
1176
  body = `${result},${patches}${index}`;
1120
1177
  } else {
@@ -1122,43 +1179,83 @@ function finalize(ctx, source, result) {
1122
1179
  }
1123
1180
  }
1124
1181
  let params = ctx.vars.length > 1 ? ctx.vars.join(",") : ctx.vars[0];
1125
- if (ctx.features.has(2 /* ArrowFunction */)) {
1182
+ if (ctx.features & 4 /* ArrowFunction */) {
1126
1183
  params = ctx.vars.length > 1 || ctx.vars.length === 0 ? `(${params})` : params;
1127
1184
  return `(${params}=>(${body}))()`;
1128
1185
  }
1129
1186
  return `(function(${params}){return ${body}})()`;
1130
1187
  }
1131
- if (source.constructor === Object) {
1188
+ if (isObject) {
1132
1189
  return `(${result})`;
1133
1190
  }
1134
1191
  return result;
1135
1192
  }
1136
1193
  function serialize(source, options) {
1137
- const ctx = createSerializationContext(options);
1194
+ const ctx = createParserContext(options);
1138
1195
  if (isPrimitive(source)) {
1139
- return serializePrimitive(ctx, source);
1196
+ return String(serializePrimitive(source));
1140
1197
  }
1141
- const tree = generateTreeSync(ctx, source);
1142
- const result = serializeTree(ctx, tree);
1143
- 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);
1144
1202
  }
1145
1203
  async function serializeAsync(source, options) {
1146
- const ctx = createSerializationContext(options);
1204
+ const ctx = createParserContext(options);
1147
1205
  if (isPrimitive(source)) {
1148
- return serializePrimitive(ctx, source);
1206
+ return String(serializePrimitive(source));
1149
1207
  }
1150
- const tree = await generateTreeAsync(ctx, source);
1151
- const result = serializeTree(ctx, tree);
1152
- 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);
1153
1212
  }
1154
1213
  function deserialize(source) {
1155
1214
  return (0, eval)(source);
1156
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
+ }
1157
1250
  var src_default = serialize;
1158
1251
  export {
1252
+ compileJSON,
1159
1253
  src_default as default,
1160
1254
  deserialize,
1255
+ fromJSON,
1161
1256
  serialize,
1162
- serializeAsync
1257
+ serializeAsync,
1258
+ toJSON,
1259
+ toJSONAsync
1163
1260
  };
1164
1261
  //# sourceMappingURL=index.mjs.map