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.
@@ -20,10 +20,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
+ compileJSON: () => compileJSON,
23
24
  default: () => src_default,
24
25
  deserialize: () => deserialize,
26
+ fromJSON: () => fromJSON,
25
27
  serialize: () => serialize,
26
- serializeAsync: () => serializeAsync
28
+ serializeAsync: () => serializeAsync,
29
+ toJSON: () => toJSON,
30
+ toJSONAsync: () => toJSONAsync
27
31
  });
28
32
  module.exports = __toCommonJS(src_exports);
29
33
 
@@ -33,7 +37,7 @@ function isPrimitive(current) {
33
37
  return true;
34
38
  }
35
39
  const type = typeof current;
36
- return type === "number" || type === "string" || type === "bigint";
40
+ return type === "number" || type === "string";
37
41
  }
38
42
  function isPromise(obj) {
39
43
  return !!obj && (typeof obj === "object" || typeof obj === "function") && "then" in obj && typeof obj.then === "function";
@@ -68,7 +72,7 @@ function compareVersion(left, right) {
68
72
  }
69
73
  var VERSION_TABLE = {
70
74
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError#browser_compatibility
71
- [0 /* AggregateError */]: {
75
+ [1 /* AggregateError */]: {
72
76
  es: [2021, 0, 0],
73
77
  chrome: [85, 0, 0],
74
78
  edge: [85, 0, 0],
@@ -81,7 +85,7 @@ var VERSION_TABLE = {
81
85
  node: [15, 0, 0]
82
86
  },
83
87
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values#browser_compatibility
84
- [1 /* ArrayPrototypeValues */]: {
88
+ [2 /* ArrayPrototypeValues */]: {
85
89
  es: [6, 0, 0],
86
90
  chrome: [66, 0, 0],
87
91
  edge: [14, 0, 0],
@@ -94,7 +98,7 @@ var VERSION_TABLE = {
94
98
  node: [10, 9, 0]
95
99
  },
96
100
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#browser_compatibility
97
- [2 /* ArrowFunction */]: {
101
+ [4 /* ArrowFunction */]: {
98
102
  es: [6, 0, 0],
99
103
  chrome: [45, 0, 0],
100
104
  edge: [12, 0, 0],
@@ -107,7 +111,7 @@ var VERSION_TABLE = {
107
111
  node: [4, 0, 0]
108
112
  },
109
113
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility
110
- [3 /* BigInt */]: {
114
+ [8 /* BigInt */]: {
111
115
  es: [2020, 0, 0],
112
116
  chrome: [67, 0, 0],
113
117
  edge: [79, 0, 0],
@@ -120,7 +124,7 @@ var VERSION_TABLE = {
120
124
  node: [10, 4, 0]
121
125
  },
122
126
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#browser_compatibility
123
- [4 /* Map */]: {
127
+ [16 /* Map */]: {
124
128
  es: [6, 0, 0],
125
129
  chrome: [38, 0, 0],
126
130
  edge: [12, 0, 0],
@@ -133,7 +137,7 @@ var VERSION_TABLE = {
133
137
  node: [0, 12, 0]
134
138
  },
135
139
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#browser_compatibility
136
- [5 /* MethodShorthand */]: {
140
+ [32 /* MethodShorthand */]: {
137
141
  es: [6, 0, 0],
138
142
  chrome: [39, 0, 0],
139
143
  edge: [12, 0, 0],
@@ -146,7 +150,7 @@ var VERSION_TABLE = {
146
150
  node: [4, 0, 0]
147
151
  },
148
152
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#browser_compatibility
149
- [6 /* ObjectAssign */]: {
153
+ [64 /* ObjectAssign */]: {
150
154
  es: [6, 0, 0],
151
155
  chrome: [45, 0, 0],
152
156
  edge: [12, 0, 0],
@@ -159,7 +163,7 @@ var VERSION_TABLE = {
159
163
  node: [4, 0, 0]
160
164
  },
161
165
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#browser_compatibility
162
- [7 /* Promise */]: {
166
+ [128 /* Promise */]: {
163
167
  es: [6, 0, 0],
164
168
  chrome: [32, 0, 0],
165
169
  edge: [12, 0, 0],
@@ -172,7 +176,7 @@ var VERSION_TABLE = {
172
176
  node: [0, 12, 0]
173
177
  },
174
178
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#browser_compatibility
175
- [8 /* Set */]: {
179
+ [256 /* Set */]: {
176
180
  es: [6, 0, 0],
177
181
  chrome: [38, 0, 0],
178
182
  edge: [12, 0, 0],
@@ -185,7 +189,7 @@ var VERSION_TABLE = {
185
189
  node: [0, 12, 0]
186
190
  },
187
191
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator#browser_compatibility
188
- [9 /* SymbolIterator */]: {
192
+ [512 /* SymbolIterator */]: {
189
193
  es: [6, 0, 0],
190
194
  chrome: [43, 0, 0],
191
195
  edge: [12, 0, 0],
@@ -198,7 +202,7 @@ var VERSION_TABLE = {
198
202
  node: [0, 12, 0]
199
203
  },
200
204
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#browser_compatibility
201
- [10 /* TypedArray */]: {
205
+ [1024 /* TypedArray */]: {
202
206
  es: [6, 0, 0],
203
207
  chrome: [7, 0, 0],
204
208
  edge: [12, 0, 0],
@@ -209,6 +213,19 @@ var VERSION_TABLE = {
209
213
  samsung: [1, 0, 0],
210
214
  deno: [1, 0, 0],
211
215
  node: [0, 10, 0]
216
+ },
217
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array#browser_compatibility
218
+ [2048 /* BigIntTypedArray */]: {
219
+ es: [2020, 0, 0],
220
+ chrome: [67, 0, 0],
221
+ edge: [79, 0, 0],
222
+ firefox: [68, 0, 0],
223
+ opera: [54, 6, 0],
224
+ safari: [15, 1, 0],
225
+ ios: [15, 2, 0],
226
+ samsung: [9, 0, 0],
227
+ deno: [1, 0, 0],
228
+ node: [10, 4, 0]
212
229
  }
213
230
  };
214
231
  var TARGET_REGEX = /^(es|chrome|edge|safari|firefox|opera|ios|samsung|deno|node)([0-9]+(\.[0-9]+(\.[0-9]+)?)?)$/i;
@@ -231,7 +248,7 @@ function getTargetVersions(targets) {
231
248
  }
232
249
  function parseTargets(targets) {
233
250
  const parsed = getTargetVersions(targets);
234
- const flags = /* @__PURE__ */ new Set();
251
+ let flags = 0;
235
252
  for (const key in VERSION_TABLE) {
236
253
  const base = VERSION_TABLE[key];
237
254
  let flag = true;
@@ -240,7 +257,7 @@ function parseTargets(targets) {
240
257
  flag = flag && !!baseVersion && compareVersion(baseVersion, version) <= 0;
241
258
  }
242
259
  if (flag) {
243
- flags.add(Number(key));
260
+ flags |= Number(key);
244
261
  }
245
262
  }
246
263
  return flags;
@@ -309,16 +326,22 @@ function getIdentifier(index) {
309
326
  var DEFAULT_OPTIONS = {
310
327
  target: "es2023"
311
328
  };
312
- function createSerializationContext(options = {}) {
329
+ function createParserContext(options = {}) {
313
330
  const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
314
331
  return {
315
- markedRefs: [],
316
- stack: [],
332
+ markedRefs: /* @__PURE__ */ new Set(),
317
333
  refs: /* @__PURE__ */ new Map(),
334
+ features: parseTargets(result.target)
335
+ };
336
+ }
337
+ function createSerializationContext(options) {
338
+ return {
339
+ stack: [],
318
340
  vars: [],
319
341
  assignments: [],
320
342
  validRefs: /* @__PURE__ */ new Map(),
321
- features: parseTargets(result.target)
343
+ features: options.features,
344
+ markedRefs: new Set(options.markedRefs)
322
345
  };
323
346
  }
324
347
  function getAssignmentExpression(assignment) {
@@ -387,16 +410,7 @@ function resolvePatches(ctx) {
387
410
  return resolveAssignments(ctx.assignments);
388
411
  }
389
412
  function markRef(ctx, current) {
390
- ctx.markedRefs[current] = true;
391
- }
392
- function createRef(ctx, index) {
393
- const current = ctx.refs.get(index);
394
- if (current != null) {
395
- return current;
396
- }
397
- const id = ctx.refs.size;
398
- ctx.refs.set(index, id);
399
- return id;
413
+ ctx.markedRefs.add(current);
400
414
  }
401
415
  function createValidRef(ctx, index) {
402
416
  const current = ctx.validRefs.get(index);
@@ -417,11 +431,20 @@ function getRefParam(ctx, index) {
417
431
  return result;
418
432
  }
419
433
  function assignRef(ctx, index, value) {
420
- if (ctx.markedRefs[index]) {
434
+ if (ctx.markedRefs.has(index)) {
421
435
  return `${getRefParam(ctx, index)}=${value}`;
422
436
  }
423
437
  return value;
424
438
  }
439
+ function createRef(ctx, index) {
440
+ const current = ctx.refs.get(index);
441
+ if (current != null) {
442
+ return current;
443
+ }
444
+ const id = ctx.refs.size;
445
+ ctx.refs.set(index, id);
446
+ return id;
447
+ }
425
448
  function createAssignment(ctx, source, value) {
426
449
  ctx.assignments.push({
427
450
  type: "index",
@@ -450,13 +473,10 @@ function createArrayAssign(ctx, ref, index, value) {
450
473
  markRef(ctx, ref);
451
474
  createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
452
475
  }
453
- function createObjectIdentifierAssign(ctx, ref, key, value) {
454
- markRef(ctx, ref);
455
- createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
456
- }
457
- function createObjectComputedAssign(ctx, ref, key, value) {
476
+ function createObjectAssign(ctx, ref, key, value, computed) {
458
477
  markRef(ctx, ref);
459
- createAssignment(ctx, `${getRefParam(ctx, ref)}[${key}]`, value);
478
+ const member = computed ? `[${key}]` : `.${key}`;
479
+ createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
460
480
  }
461
481
  function getErrorConstructor(error) {
462
482
  if (error instanceof EvalError) {
@@ -508,104 +528,131 @@ function getIterableOptions(obj) {
508
528
  return void 0;
509
529
  }
510
530
  function isReferenceInStack(ctx, node) {
511
- return node[0] === 1 /* Reference */ && ctx.stack.includes(node[1]);
531
+ return node[0] === 2 /* Reference */ && ctx.stack.includes(node[1]);
512
532
  }
513
533
  function generateRef(ctx, current) {
514
534
  const ref = ctx.refs.has(current);
515
535
  if (ref) {
516
536
  const refID = ctx.refs.get(current) || 0;
517
537
  markRef(ctx, refID);
518
- return [1 /* Reference */, refID];
538
+ return [2 /* Reference */, refID];
519
539
  }
520
540
  const id = ctx.refs.size;
521
541
  ctx.refs.set(current, id);
522
542
  return id;
523
543
  }
524
544
  function generateSemiPrimitiveValue(ctx, current, id) {
545
+ if (typeof current === "bigint") {
546
+ assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
547
+ return [1 /* BigInt */, `${current}n`];
548
+ }
525
549
  if (constructorCheck(current, Date)) {
526
- return [2 /* Date */, current, id];
550
+ return [3 /* Date */, current.toISOString(), id];
527
551
  }
528
552
  if (constructorCheck(current, RegExp)) {
529
- return [3 /* RegExp */, current, id];
553
+ return [4 /* RegExp */, String(current), id];
530
554
  }
531
555
  if (constructorCheck(current, Int8Array)) {
532
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int8Array"');
533
- return [13 /* TypedArray */, ["Int8Array", current], id];
556
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int8Array"');
557
+ return [14 /* TypedArray */, ["Int8Array", current.toString(), current.byteOffset], id];
534
558
  }
535
559
  if (constructorCheck(current, Int16Array)) {
536
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int16Array"');
537
- return [13 /* TypedArray */, ["Int16Array", current], id];
560
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int16Array"');
561
+ return [14 /* TypedArray */, ["Int16Array", current.toString(), current.byteOffset], id];
538
562
  }
539
563
  if (constructorCheck(current, Int32Array)) {
540
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int32Array"');
541
- return [13 /* TypedArray */, ["Int32Array", current], id];
564
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int32Array"');
565
+ return [14 /* TypedArray */, ["Int32Array", current.toString(), current.byteOffset], id];
542
566
  }
543
567
  if (constructorCheck(current, Uint8Array)) {
544
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint8Array"');
545
- return [13 /* TypedArray */, ["Uint8Array", current], id];
568
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint8Array"');
569
+ return [14 /* TypedArray */, ["Uint8Array", current.toString(), current.byteOffset], id];
546
570
  }
547
571
  if (constructorCheck(current, Uint16Array)) {
548
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint16Array"');
549
- return [13 /* TypedArray */, ["Uint16Array", current], id];
572
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint16Array"');
573
+ return [14 /* TypedArray */, ["Uint16Array", current.toString(), current.byteOffset], id];
550
574
  }
551
575
  if (constructorCheck(current, Uint32Array)) {
552
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint32Array"');
553
- return [13 /* TypedArray */, ["Uint32Array", current], id];
576
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint32Array"');
577
+ return [14 /* TypedArray */, ["Uint32Array", current.toString(), current.byteOffset], id];
554
578
  }
555
579
  if (constructorCheck(current, Uint8ClampedArray)) {
556
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint8ClampedArray"');
557
- return [13 /* TypedArray */, ["Uint8ClampedArray", current], id];
580
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint8ClampedArray"');
581
+ return [14 /* TypedArray */, ["Uint8ClampedArray", current.toString(), current.byteOffset], id];
558
582
  }
559
583
  if (constructorCheck(current, Float32Array)) {
560
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Float32Array"');
561
- return [13 /* TypedArray */, ["Float32Array", current], id];
584
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float32Array"');
585
+ return [14 /* TypedArray */, ["Float32Array", current.toString(), current.byteOffset], id];
562
586
  }
563
587
  if (constructorCheck(current, Float64Array)) {
564
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Float64Array"');
565
- return [13 /* TypedArray */, ["Float64Array", current], id];
588
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float64Array"');
589
+ return [14 /* TypedArray */, ["Float64Array", current.toString(), current.byteOffset], id];
566
590
  }
567
591
  if (constructorCheck(current, BigInt64Array)) {
568
592
  assert(
569
- ctx.features.has(10 /* TypedArray */) && ctx.features.has(3 /* BigInt */),
593
+ ctx.features & 2048 /* BigIntTypedArray */,
570
594
  'Unsupported value type "BigInt64Array"'
571
595
  );
572
- return [13 /* TypedArray */, ["BigInt64Array", current], id];
596
+ let result = "";
597
+ const cap = current.length - 1;
598
+ for (let i = 0; i < cap; i++) {
599
+ result += `${current[i]}n,`;
600
+ }
601
+ result += `"${current[cap]}"`;
602
+ return [
603
+ 15 /* BigIntTypedArray */,
604
+ ["BigInt64Array", result, current.byteOffset],
605
+ id
606
+ ];
573
607
  }
574
608
  if (constructorCheck(current, BigUint64Array)) {
575
609
  assert(
576
- ctx.features.has(10 /* TypedArray */) && ctx.features.has(3 /* BigInt */),
610
+ ctx.features & 2048 /* BigIntTypedArray */,
577
611
  'Unsupported value type "BigUint64Array"'
578
612
  );
579
- return [13 /* TypedArray */, ["BigUint64Array", current], id];
613
+ let result = "";
614
+ const cap = current.length - 1;
615
+ for (let i = 0; i < cap; i++) {
616
+ result += `"${current[i]}n",`;
617
+ }
618
+ result += `"${current[cap]}"`;
619
+ return [
620
+ 15 /* BigIntTypedArray */,
621
+ ["BigInt64Array", result, current.byteOffset],
622
+ id
623
+ ];
580
624
  }
581
625
  return void 0;
582
626
  }
583
627
  function serializePropertiesSync(ctx, properties) {
584
- const keyNodes = [];
585
- const valueNodes = [];
586
- const deferredKeys = [];
587
- const deferredValues = [];
588
628
  const keys = Object.keys(properties);
629
+ const size = keys.length;
630
+ const keyNodes = new Array(size);
631
+ const valueNodes = new Array(size);
632
+ const deferredKeys = new Array(size);
633
+ const deferredValues = new Array(size);
589
634
  let deferredSize = 0;
635
+ let nodesSize = 0;
590
636
  for (const key of keys) {
591
637
  if (isIterable(properties[key])) {
592
- deferredKeys.push(key);
593
- deferredValues.push(properties[key]);
638
+ deferredKeys[deferredSize] = key;
639
+ deferredValues[deferredSize] = properties[key];
594
640
  deferredSize++;
595
641
  } else {
596
- keyNodes.push(key);
597
- valueNodes.push(generateTreeSync(ctx, properties[key]));
642
+ keyNodes[nodesSize] = key;
643
+ valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
644
+ nodesSize++;
598
645
  }
599
646
  }
600
647
  for (let i = 0; i < deferredSize; i++) {
601
- keyNodes.push(deferredKeys[i]);
602
- valueNodes.push(generateTreeSync(ctx, deferredValues[i]));
648
+ keyNodes[nodesSize + i] = deferredKeys[i];
649
+ valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
603
650
  }
604
- return [keyNodes, valueNodes, keys.length];
651
+ return [keyNodes, valueNodes, size];
605
652
  }
606
653
  function generateTreeSync(ctx, current) {
607
654
  if (isPrimitive(current)) {
608
- return [0 /* Primitive */, current];
655
+ return [0 /* Primitive */, serializePrimitive(current)];
609
656
  }
610
657
  const id = generateRef(ctx, current);
611
658
  if (Array.isArray(id)) {
@@ -616,43 +663,49 @@ function generateTreeSync(ctx, current) {
616
663
  return semiPrimitive;
617
664
  }
618
665
  if (constructorCheck(current, Set)) {
619
- assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
620
- const nodes = [];
621
- const deferred = [];
666
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
667
+ const len = current.size;
668
+ const nodes = new Array(len);
669
+ const deferred = new Array(len);
670
+ let deferredSize = 0;
671
+ let nodeSize = 0;
622
672
  for (const item of current.keys()) {
623
673
  if (isIterable(item)) {
624
- deferred.push(item);
674
+ deferred[deferredSize++] = item;
625
675
  } else {
626
- nodes.push(generateTreeSync(ctx, item));
676
+ nodes[nodeSize++] = generateTreeSync(ctx, item);
627
677
  }
628
678
  }
629
- for (let i = 0, len = deferred.length; i < len; i++) {
630
- nodes.push(generateTreeSync(ctx, deferred[i]));
679
+ for (let i = 0; i < deferredSize; i++) {
680
+ nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
631
681
  }
632
- return [4 /* Set */, nodes, id];
682
+ return [5 /* Set */, nodes, id];
633
683
  }
634
684
  if (constructorCheck(current, Map)) {
635
- assert(ctx.features.has(4 /* Map */), 'Unsupported type "Map"');
636
- const keyNodes = [];
637
- const valueNodes = [];
638
- const deferredKey = [];
639
- const deferredValue = [];
685
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
686
+ const len = current.size;
687
+ const keyNodes = new Array(len);
688
+ const valueNodes = new Array(len);
689
+ const deferredKey = new Array(len);
690
+ const deferredValue = new Array(len);
640
691
  let deferredSize = 0;
692
+ let nodeSize = 0;
641
693
  for (const [key, value] of current.entries()) {
642
694
  if (isIterable(key) || isIterable(value)) {
643
- deferredKey.push(key);
644
- deferredValue.push(value);
695
+ deferredKey[deferredSize] = key;
696
+ deferredValue[deferredSize] = value;
645
697
  deferredSize++;
646
698
  } else {
647
- keyNodes.push(generateTreeSync(ctx, key));
648
- valueNodes.push(generateTreeSync(ctx, value));
699
+ keyNodes[nodeSize] = generateTreeSync(ctx, key);
700
+ valueNodes[nodeSize] = generateTreeSync(ctx, value);
701
+ nodeSize++;
649
702
  }
650
703
  }
651
704
  for (let i = 0; i < deferredSize; i++) {
652
- keyNodes.push(generateTreeSync(ctx, deferredKey[i]));
653
- valueNodes.push(generateTreeSync(ctx, deferredValue[i]));
705
+ keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
706
+ valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
654
707
  }
655
- return [5 /* Map */, [keyNodes, valueNodes, current.size], id];
708
+ return [6 /* Map */, [keyNodes, valueNodes, len], id];
656
709
  }
657
710
  if (Array.isArray(current)) {
658
711
  const size = current.length;
@@ -672,27 +725,27 @@ function generateTreeSync(ctx, current) {
672
725
  nodes[i] = generateTreeSync(ctx, deferred[i]);
673
726
  }
674
727
  }
675
- return [6 /* Array */, nodes, id];
728
+ return [7 /* Array */, nodes, id];
676
729
  }
677
- if (current instanceof AggregateError && ctx.features.has(0 /* AggregateError */)) {
730
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
678
731
  const options = getErrorOptions(current);
679
732
  const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
680
733
  const errorsNode = generateTreeSync(ctx, current.errors);
681
- return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
734
+ return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
682
735
  }
683
736
  if (current instanceof Error) {
684
737
  const options = getErrorOptions(current);
685
738
  const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
686
739
  return [
687
- 10 /* Error */,
740
+ 11 /* Error */,
688
741
  [getErrorConstructor(current), current.message, optionsNode],
689
742
  id
690
743
  ];
691
744
  }
692
745
  if (isIterable(current)) {
693
- assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
746
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
694
747
  const options = getIterableOptions(current);
695
- return [12 /* Iterable */, [
748
+ return [13 /* Iterable */, [
696
749
  // Parse options first before the items
697
750
  options ? serializePropertiesSync(ctx, options) : void 0,
698
751
  generateTreeSync(ctx, Array.from(current))
@@ -701,39 +754,46 @@ function generateTreeSync(ctx, current) {
701
754
  const empty = current.constructor == null;
702
755
  if (current.constructor === Object || empty) {
703
756
  return [
704
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
757
+ empty ? 9 /* NullConstructor */ : 8 /* Object */,
705
758
  serializePropertiesSync(ctx, current),
706
759
  id
707
760
  ];
708
761
  }
709
762
  throw new Error("Unsupported value");
710
763
  }
764
+ function parseSync(ctx, current) {
765
+ const result = generateTreeSync(ctx, current);
766
+ return [result, createRef(ctx, current), result[0] === 8 /* Object */];
767
+ }
711
768
  async function serializePropertiesAsync(ctx, properties) {
712
- const keyNodes = [];
713
- const valueNodes = [];
714
- const deferredKeys = [];
715
- const deferredValues = [];
716
769
  const keys = Object.keys(properties);
770
+ const size = keys.length;
771
+ const keyNodes = new Array(size);
772
+ const valueNodes = new Array(size);
773
+ const deferredKeys = new Array(size);
774
+ const deferredValues = new Array(size);
717
775
  let deferredSize = 0;
776
+ let nodesSize = 0;
718
777
  for (const key of keys) {
719
778
  if (isIterable(properties[key])) {
720
- deferredKeys.push(key);
721
- deferredValues.push(properties[key]);
779
+ deferredKeys[deferredSize] = key;
780
+ deferredValues[deferredSize] = properties[key];
722
781
  deferredSize++;
723
782
  } else {
724
- keyNodes.push(key);
725
- valueNodes.push(await generateTreeAsync(ctx, properties[key]));
783
+ keyNodes[nodesSize] = key;
784
+ valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
785
+ nodesSize++;
726
786
  }
727
787
  }
728
788
  for (let i = 0; i < deferredSize; i++) {
729
- keyNodes.push(deferredKeys[i]);
730
- valueNodes.push(await generateTreeAsync(ctx, deferredValues[i]));
789
+ keyNodes[nodesSize + i] = deferredKeys[i];
790
+ valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
731
791
  }
732
- return [keyNodes, valueNodes, keys.length];
792
+ return [keyNodes, valueNodes, size];
733
793
  }
734
794
  async function generateTreeAsync(ctx, current) {
735
795
  if (isPrimitive(current)) {
736
- return [0 /* Primitive */, current];
796
+ return [0 /* Primitive */, serializePrimitive(current)];
737
797
  }
738
798
  const id = generateRef(ctx, current);
739
799
  if (Array.isArray(id)) {
@@ -744,51 +804,57 @@ async function generateTreeAsync(ctx, current) {
744
804
  return semiPrimitive;
745
805
  }
746
806
  if (isPromise(current)) {
747
- assert(ctx.features.has(7 /* Promise */), 'Unsupported type "Promise"');
807
+ assert(ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
748
808
  return current.then(async (value) => [
749
- 9 /* Promise */,
809
+ 10 /* Promise */,
750
810
  await generateTreeAsync(ctx, value),
751
811
  id
752
812
  ]);
753
813
  }
754
814
  if (constructorCheck(current, Set)) {
755
- assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
756
- const nodes = [];
757
- const deferred = [];
815
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
816
+ const len = current.size;
817
+ const nodes = new Array(len);
818
+ const deferred = new Array(len);
819
+ let deferredSize = 0;
820
+ let nodeSize = 0;
758
821
  for (const item of current.keys()) {
759
822
  if (isIterable(item)) {
760
- deferred.push(item);
823
+ deferred[deferredSize++] = item;
761
824
  } else {
762
- nodes.push(await generateTreeAsync(ctx, item));
825
+ nodes[nodeSize++] = await generateTreeAsync(ctx, item);
763
826
  }
764
827
  }
765
- for (let i = 0, len = deferred.length; i < len; i++) {
766
- nodes.push(await generateTreeAsync(ctx, deferred[i]));
828
+ for (let i = 0; i < deferredSize; i++) {
829
+ nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
767
830
  }
768
- return [4 /* Set */, nodes, id];
831
+ return [5 /* Set */, nodes, id];
769
832
  }
770
833
  if (constructorCheck(current, Map)) {
771
- assert(ctx.features.has(4 /* Map */), 'Unsupported type "Map"');
772
- const keyNodes = [];
773
- const valueNodes = [];
774
- const deferredKey = [];
775
- const deferredValue = [];
834
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
835
+ const len = current.size;
836
+ const keyNodes = new Array(len);
837
+ const valueNodes = new Array(len);
838
+ const deferredKey = new Array(len);
839
+ const deferredValue = new Array(len);
776
840
  let deferredSize = 0;
841
+ let nodeSize = 0;
777
842
  for (const [key, value] of current.entries()) {
778
843
  if (isIterable(key) || isIterable(value)) {
779
- deferredKey.push(key);
780
- deferredValue.push(value);
844
+ deferredKey[deferredSize] = key;
845
+ deferredValue[deferredSize] = value;
781
846
  deferredSize++;
782
847
  } else {
783
- keyNodes.push(await generateTreeAsync(ctx, key));
784
- valueNodes.push(await generateTreeAsync(ctx, value));
848
+ keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
849
+ valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
850
+ nodeSize++;
785
851
  }
786
852
  }
787
853
  for (let i = 0; i < deferredSize; i++) {
788
- keyNodes.push(await generateTreeAsync(ctx, deferredKey[i]));
789
- valueNodes.push(await generateTreeAsync(ctx, deferredValue[i]));
854
+ keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
855
+ valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
790
856
  }
791
- return [5 /* Map */, [keyNodes, valueNodes, current.size], id];
857
+ return [6 /* Map */, [keyNodes, valueNodes, len], id];
792
858
  }
793
859
  if (Array.isArray(current)) {
794
860
  const size = current.length;
@@ -809,27 +875,27 @@ async function generateTreeAsync(ctx, current) {
809
875
  nodes[i] = await generateTreeAsync(ctx, deferred[i]);
810
876
  }
811
877
  }
812
- return [6 /* Array */, nodes, id];
878
+ return [7 /* Array */, nodes, id];
813
879
  }
814
- if (current instanceof AggregateError) {
880
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
815
881
  const options = getErrorOptions(current);
816
882
  const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
817
883
  const errorsNode = await generateTreeAsync(ctx, current.errors);
818
- return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
884
+ return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
819
885
  }
820
886
  if (current instanceof Error) {
821
887
  const options = getErrorOptions(current);
822
888
  const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
823
889
  return [
824
- 10 /* Error */,
890
+ 11 /* Error */,
825
891
  [getErrorConstructor(current), current.message, optionsNode],
826
892
  id
827
893
  ];
828
894
  }
829
895
  if (isIterable(current)) {
830
- assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
896
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
831
897
  const options = getIterableOptions(current);
832
- return [12 /* Iterable */, [
898
+ return [13 /* Iterable */, [
833
899
  options ? await serializePropertiesAsync(ctx, options) : void 0,
834
900
  await generateTreeAsync(ctx, Array.from(current))
835
901
  ], id];
@@ -837,14 +903,18 @@ async function generateTreeAsync(ctx, current) {
837
903
  const empty = current.constructor == null;
838
904
  if (current.constructor === Object || empty) {
839
905
  return [
840
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
906
+ empty ? 9 /* NullConstructor */ : 8 /* Object */,
841
907
  await serializePropertiesAsync(ctx, current),
842
908
  id
843
909
  ];
844
910
  }
845
911
  throw new Error("Unsupported value");
846
912
  }
847
- function serializePrimitive(ctx, value) {
913
+ async function parseAsync(ctx, current) {
914
+ const result = await generateTreeAsync(ctx, current);
915
+ return [result, createRef(ctx, current), result[0] === 8 /* Object */];
916
+ }
917
+ function serializePrimitive(value) {
848
918
  if (value === true) {
849
919
  return "!0";
850
920
  }
@@ -855,11 +925,7 @@ function serializePrimitive(ctx, value) {
855
925
  return "void 0";
856
926
  }
857
927
  if (value === null) {
858
- return "null";
859
- }
860
- if (typeof value === "bigint") {
861
- assert(ctx.features.has(3 /* BigInt */), 'Unsupported type "BigInt"');
862
- return `${value}n`;
928
+ return null;
863
929
  }
864
930
  if (typeof value === "string") {
865
931
  return quote(value);
@@ -873,7 +939,7 @@ function serializePrimitive(ctx, value) {
873
939
  if (Object.is(value, -Infinity)) {
874
940
  return "-1/0";
875
941
  }
876
- return String(value);
942
+ return value;
877
943
  }
878
944
  var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
879
945
  function serializeAssignments(ctx, targetRef, [keys, values, size]) {
@@ -888,15 +954,14 @@ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
888
954
  const check = Number(key);
889
955
  const parentAssignment = ctx.assignments;
890
956
  ctx.assignments = mainAssignments;
891
- if (check >= 0 || Number.isNaN(check) && IDENTIFIER_CHECK.test(key)) {
892
- if (!Number.isNaN(check)) {
893
- createObjectComputedAssign(ctx, targetRef, key, refParam);
894
- } else {
895
- createObjectIdentifierAssign(ctx, targetRef, key, refParam);
896
- }
897
- } else {
898
- createObjectComputedAssign(ctx, targetRef, quote(key), refParam);
899
- }
957
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
958
+ createObjectAssign(
959
+ ctx,
960
+ targetRef,
961
+ isIdentifier ? key : quote(key),
962
+ refParam,
963
+ !(isIdentifier && Number.isNaN(check))
964
+ );
900
965
  ctx.assignments = parentAssignment;
901
966
  }
902
967
  ctx.stack.pop();
@@ -909,22 +974,19 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
909
974
  const key = keys[i];
910
975
  const val = values[i];
911
976
  const check = Number(key);
912
- if (check >= 0 || Number.isNaN(check) && IDENTIFIER_CHECK.test(key)) {
913
- if (isReferenceInStack(ctx, val)) {
914
- const refParam = getRefParam(ctx, val[1]);
915
- if (!Number.isNaN(check)) {
916
- createObjectComputedAssign(ctx, sourceID, key, refParam);
917
- } else {
918
- createObjectIdentifierAssign(ctx, sourceID, key, refParam);
919
- }
920
- } else {
921
- result += `${key}:${serializeTree(ctx, val)},`;
922
- }
923
- } else if (isReferenceInStack(ctx, val)) {
977
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
978
+ const validKey = isIdentifier ? key : quote(key);
979
+ if (isReferenceInStack(ctx, val)) {
924
980
  const refParam = getRefParam(ctx, val[1]);
925
- createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
981
+ createObjectAssign(
982
+ ctx,
983
+ sourceID,
984
+ validKey,
985
+ refParam,
986
+ !(isIdentifier && Number.isNaN(check))
987
+ );
926
988
  } else {
927
- result += `${quote(key)}:${serializeTree(ctx, val)},`;
989
+ result += `${validKey}:${serializeTree(ctx, val)},`;
928
990
  }
929
991
  }
930
992
  ctx.stack.pop();
@@ -933,10 +995,10 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
933
995
  function serializeTree(ctx, [type, value, id]) {
934
996
  switch (type) {
935
997
  case 0 /* Primitive */:
936
- return serializePrimitive(ctx, value);
937
- case 1 /* Reference */:
998
+ return String(value);
999
+ case 2 /* Reference */:
938
1000
  return getRefParam(ctx, value);
939
- case 9 /* Promise */: {
1001
+ case 10 /* Promise */: {
940
1002
  let serialized;
941
1003
  if (isReferenceInStack(ctx, value)) {
942
1004
  serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
@@ -948,22 +1010,21 @@ function serializeTree(ctx, [type, value, id]) {
948
1010
  }
949
1011
  return assignRef(ctx, id, serialized);
950
1012
  }
951
- case 2 /* Date */:
952
- return assignRef(ctx, id, `new Date("${value.toISOString()}")`);
953
- case 3 /* RegExp */:
954
- return assignRef(ctx, id, String(value));
955
- case 13 /* TypedArray */: {
956
- let values = "";
957
- for (let i = 0, len = value[1].length; i < len; i++) {
958
- values += `${serializePrimitive(ctx, value[1][i])},`;
959
- }
960
- let args = values ? `[${values.substring(0, values.length - 1)}]` : "[]";
961
- if (value[1].byteOffset !== 0) {
962
- args += `,${value[1].byteOffset}`;
1013
+ case 1 /* BigInt */:
1014
+ return value;
1015
+ case 3 /* Date */:
1016
+ return assignRef(ctx, id, `new Date("${value}")`);
1017
+ case 4 /* RegExp */:
1018
+ return assignRef(ctx, id, value);
1019
+ case 15 /* BigIntTypedArray */:
1020
+ case 14 /* TypedArray */: {
1021
+ let args = `[${value[1]}]`;
1022
+ if (value[2] !== 0) {
1023
+ args += `,${value[2]}`;
963
1024
  }
964
1025
  return assignRef(ctx, id, `new ${value[0]}(${args})`);
965
1026
  }
966
- case 4 /* Set */: {
1027
+ case 5 /* Set */: {
967
1028
  let serialized = "new Set";
968
1029
  const size = value.length;
969
1030
  if (size) {
@@ -984,7 +1045,7 @@ function serializeTree(ctx, [type, value, id]) {
984
1045
  }
985
1046
  return assignRef(ctx, id, serialized);
986
1047
  }
987
- case 5 /* Map */: {
1048
+ case 6 /* Map */: {
988
1049
  let serialized = "new Map";
989
1050
  const size = value[2];
990
1051
  if (size) {
@@ -1021,13 +1082,13 @@ function serializeTree(ctx, [type, value, id]) {
1021
1082
  }
1022
1083
  return assignRef(ctx, id, serialized);
1023
1084
  }
1024
- case 6 /* Array */: {
1085
+ case 7 /* Array */: {
1025
1086
  const size = value.length;
1026
1087
  let values = "";
1027
1088
  ctx.stack.push(id);
1028
1089
  for (let i = 0; i < size; i++) {
1029
1090
  const item = value[i];
1030
- if (i in value) {
1091
+ if (item) {
1031
1092
  if (isReferenceInStack(ctx, item)) {
1032
1093
  createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
1033
1094
  values += ",";
@@ -1044,12 +1105,12 @@ function serializeTree(ctx, [type, value, id]) {
1044
1105
  ctx.stack.pop();
1045
1106
  return assignRef(ctx, id, `[${values}]`);
1046
1107
  }
1047
- case 11 /* AggregateError */: {
1108
+ case 12 /* AggregateError */: {
1048
1109
  ctx.stack.push(id);
1049
1110
  let serialized = `new AggregateError(${serializeTree(ctx, value[2])},${quote(value[0])})`;
1050
1111
  ctx.stack.pop();
1051
1112
  if (value[1]) {
1052
- if (ctx.features.has(6 /* ObjectAssign */)) {
1113
+ if (ctx.features & 64 /* ObjectAssign */) {
1053
1114
  const options = serializeObject(ctx, id, value[1]);
1054
1115
  serialized = `Object.assign(${serialized},${options})`;
1055
1116
  } else {
@@ -1063,10 +1124,10 @@ function serializeTree(ctx, [type, value, id]) {
1063
1124
  }
1064
1125
  return assignRef(ctx, id, serialized);
1065
1126
  }
1066
- case 10 /* Error */: {
1127
+ case 11 /* Error */: {
1067
1128
  let serialized = `new ${value[0]}(${quote(value[1])})`;
1068
1129
  if (value[2]) {
1069
- if (ctx.features.has(6 /* ObjectAssign */)) {
1130
+ if (ctx.features & 64 /* ObjectAssign */) {
1070
1131
  const options = serializeObject(ctx, id, value[2]);
1071
1132
  serialized = `Object.assign(${serialized},${options})`;
1072
1133
  } else {
@@ -1080,26 +1141,26 @@ function serializeTree(ctx, [type, value, id]) {
1080
1141
  }
1081
1142
  return assignRef(ctx, id, serialized);
1082
1143
  }
1083
- case 12 /* Iterable */: {
1144
+ case 13 /* Iterable */: {
1084
1145
  const parent = ctx.stack;
1085
1146
  ctx.stack = [];
1086
1147
  const values = serializeTree(ctx, value[1]);
1087
1148
  ctx.stack = parent;
1088
1149
  let serialized;
1089
- if (ctx.features.has(1 /* ArrayPrototypeValues */)) {
1150
+ if (ctx.features & 2 /* ArrayPrototypeValues */) {
1090
1151
  serialized = `${values}.values()`;
1091
1152
  } else {
1092
1153
  serialized = `${values}[Symbol.iterator]()`;
1093
1154
  }
1094
- if (ctx.features.has(2 /* ArrowFunction */)) {
1155
+ if (ctx.features & 4 /* ArrowFunction */) {
1095
1156
  serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1096
- } else if (ctx.features.has(5 /* MethodShorthand */)) {
1157
+ } else if (ctx.features & 32 /* MethodShorthand */) {
1097
1158
  serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1098
1159
  } else {
1099
1160
  serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1100
1161
  }
1101
1162
  if (value[0]) {
1102
- if (ctx.features.has(6 /* ObjectAssign */)) {
1163
+ if (ctx.features & 64 /* ObjectAssign */) {
1103
1164
  const options = serializeObject(ctx, id, value[0]);
1104
1165
  serialized = `Object.assign(${serialized},${options})`;
1105
1166
  } else {
@@ -1113,9 +1174,9 @@ function serializeTree(ctx, [type, value, id]) {
1113
1174
  }
1114
1175
  return assignRef(ctx, id, serialized);
1115
1176
  }
1116
- case 8 /* NullConstructor */: {
1177
+ case 9 /* NullConstructor */: {
1117
1178
  let serialized = "Object.create(null)";
1118
- if (ctx.features.has(6 /* ObjectAssign */)) {
1179
+ if (ctx.features & 64 /* ObjectAssign */) {
1119
1180
  const fields = serializeObject(ctx, id, value);
1120
1181
  if (fields !== "{}") {
1121
1182
  serialized = `Object.assign(${serialized},${fields})`;
@@ -1130,7 +1191,7 @@ function serializeTree(ctx, [type, value, id]) {
1130
1191
  }
1131
1192
  return assignRef(ctx, id, serialized);
1132
1193
  }
1133
- case 7 /* Object */:
1194
+ case 8 /* Object */:
1134
1195
  return assignRef(ctx, id, serializeObject(ctx, id, value));
1135
1196
  default:
1136
1197
  throw new Error("Unsupported type");
@@ -1138,12 +1199,12 @@ function serializeTree(ctx, [type, value, id]) {
1138
1199
  }
1139
1200
 
1140
1201
  // src/index.ts
1141
- function finalize(ctx, source, result) {
1202
+ function finalize(ctx, rootID, isObject, result) {
1142
1203
  if (ctx.vars.length) {
1143
1204
  const patches = resolvePatches(ctx);
1144
1205
  let body = result;
1145
1206
  if (patches) {
1146
- const index = getRefParam(ctx, createRef(ctx, source));
1207
+ const index = getRefParam(ctx, rootID);
1147
1208
  if (result.startsWith(`${index}=`)) {
1148
1209
  body = `${result},${patches}${index}`;
1149
1210
  } else {
@@ -1151,37 +1212,73 @@ function finalize(ctx, source, result) {
1151
1212
  }
1152
1213
  }
1153
1214
  let params = ctx.vars.length > 1 ? ctx.vars.join(",") : ctx.vars[0];
1154
- if (ctx.features.has(2 /* ArrowFunction */)) {
1215
+ if (ctx.features & 4 /* ArrowFunction */) {
1155
1216
  params = ctx.vars.length > 1 || ctx.vars.length === 0 ? `(${params})` : params;
1156
1217
  return `(${params}=>(${body}))()`;
1157
1218
  }
1158
1219
  return `(function(${params}){return ${body}})()`;
1159
1220
  }
1160
- if (source.constructor === Object) {
1221
+ if (isObject) {
1161
1222
  return `(${result})`;
1162
1223
  }
1163
1224
  return result;
1164
1225
  }
1165
1226
  function serialize(source, options) {
1166
- const ctx = createSerializationContext(options);
1227
+ const ctx = createParserContext(options);
1167
1228
  if (isPrimitive(source)) {
1168
- return serializePrimitive(ctx, source);
1229
+ return String(serializePrimitive(source));
1169
1230
  }
1170
- const tree = generateTreeSync(ctx, source);
1171
- const result = serializeTree(ctx, tree);
1172
- return finalize(ctx, source, result);
1231
+ const [tree, rootID, isObject] = parseSync(ctx, source);
1232
+ const serial = createSerializationContext(ctx);
1233
+ const result = serializeTree(serial, tree);
1234
+ return finalize(serial, rootID, isObject, result);
1173
1235
  }
1174
1236
  async function serializeAsync(source, options) {
1175
- const ctx = createSerializationContext(options);
1237
+ const ctx = createParserContext(options);
1176
1238
  if (isPrimitive(source)) {
1177
- return serializePrimitive(ctx, source);
1239
+ return String(serializePrimitive(source));
1178
1240
  }
1179
- const tree = await generateTreeAsync(ctx, source);
1180
- const result = serializeTree(ctx, tree);
1181
- return finalize(ctx, source, result);
1241
+ const [tree, rootID, isObject] = await parseAsync(ctx, source);
1242
+ const serial = createSerializationContext(ctx);
1243
+ const result = serializeTree(serial, tree);
1244
+ return finalize(serial, rootID, isObject, result);
1182
1245
  }
1183
1246
  function deserialize(source) {
1184
1247
  return (0, eval)(source);
1185
1248
  }
1249
+ function toJSON(source, options) {
1250
+ const ctx = createParserContext(options);
1251
+ const [tree, root, isObject] = parseSync(ctx, source);
1252
+ return JSON.stringify([
1253
+ tree,
1254
+ root,
1255
+ isObject,
1256
+ ctx.features,
1257
+ [...ctx.markedRefs]
1258
+ ]);
1259
+ }
1260
+ async function toJSONAsync(source, options) {
1261
+ const ctx = createParserContext(options);
1262
+ const [tree, root, isObject] = await parseAsync(ctx, source);
1263
+ return JSON.stringify([
1264
+ tree,
1265
+ root,
1266
+ isObject,
1267
+ ctx.features,
1268
+ [...ctx.markedRefs]
1269
+ ]);
1270
+ }
1271
+ function compileJSON(source) {
1272
+ const parsed = JSON.parse(source);
1273
+ const serial = createSerializationContext({
1274
+ features: parsed[3],
1275
+ markedRefs: parsed[4]
1276
+ });
1277
+ const result = serializeTree(serial, parsed[0]);
1278
+ return finalize(serial, parsed[1], parsed[2], result);
1279
+ }
1280
+ function fromJSON(source) {
1281
+ return deserialize(compileJSON(source));
1282
+ }
1186
1283
  var src_default = serialize;
1187
1284
  //# sourceMappingURL=index.cjs.map