seroval 0.4.0-alpha.13 → 0.4.0-alpha.15

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.
@@ -1,10 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
7
-
8
1
  // src/compat.ts
9
2
  function compareVersion(left, right) {
10
3
  let diff = left[0] - right[0];
@@ -187,8 +180,9 @@ var platforms = [
187
180
  "samsung"
188
181
  ];
189
182
  function getPlatform(target) {
183
+ let platform;
190
184
  for (let i = 0, len = platforms.length; i < len; i++) {
191
- const platform = target.substring(0, platforms[i].length);
185
+ platform = target.substring(0, platforms[i].length);
192
186
  if (platform === platforms[i]) {
193
187
  return platform;
194
188
  }
@@ -214,11 +208,14 @@ function getTargetVersions(targets) {
214
208
  function parseTargets(targets) {
215
209
  const parsed = getTargetVersions(targets);
216
210
  let flags = 0;
211
+ let base;
212
+ let flag;
213
+ let baseVersion;
217
214
  for (const key of Object.keys(VERSION_TABLE)) {
218
- const base = VERSION_TABLE[key];
219
- let flag = true;
215
+ base = VERSION_TABLE[key];
216
+ flag = true;
220
217
  for (const [platform, version] of parsed) {
221
- const baseVersion = base[platform];
218
+ baseVersion = base[platform];
222
219
  flag = flag && !!baseVersion && compareVersion(baseVersion, version) <= 0;
223
220
  }
224
221
  if (flag) {
@@ -249,35 +246,27 @@ function getIdentifier(index) {
249
246
  var DEFAULT_OPTIONS = {
250
247
  target: "es2023"
251
248
  };
252
- var ParserContext = class {
253
- constructor(options = {}) {
254
- __publicField(this, "refs", /* @__PURE__ */ new Map());
255
- __publicField(this, "markedRefs", {});
256
- __publicField(this, "features");
257
- const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
258
- this.features = parseTargets(result.target);
259
- }
260
- };
261
- var SerializationContext = class {
262
- constructor(options) {
263
- __publicField(this, "stack", []);
264
- // Map tree refs to actual refs
265
- __publicField(this, "validRefs", []);
266
- __publicField(this, "refSize", 0);
267
- // Variables
268
- __publicField(this, "vars", []);
269
- // Array of assignments to be done (used for recursion)
270
- __publicField(this, "assignments", []);
271
- // Supported features
272
- __publicField(this, "features");
273
- // Refs that are...referenced
274
- __publicField(this, "markedRefs");
275
- this.features = options.features;
276
- this.markedRefs = options.markedRefs;
277
- }
278
- };
249
+ function createParserContext(options = {}) {
250
+ const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
251
+ return {
252
+ markedRefs: /* @__PURE__ */ new Set(),
253
+ refs: /* @__PURE__ */ new Map(),
254
+ features: parseTargets(result.target)
255
+ };
256
+ }
257
+ function createSerializationContext(options) {
258
+ return {
259
+ stack: [],
260
+ vars: [],
261
+ assignments: [],
262
+ validRefs: [],
263
+ refSize: 0,
264
+ features: options.features,
265
+ markedRefs: new Set(options.markedRefs)
266
+ };
267
+ }
279
268
  function markRef(ctx, current) {
280
- ctx.markedRefs[current] = 1;
269
+ ctx.markedRefs.add(current);
281
270
  }
282
271
  function createValidRef(ctx, index) {
283
272
  const current = ctx.validRefs[index];
@@ -356,201 +345,123 @@ function quote(str) {
356
345
  } else {
357
346
  result += str.slice(lastPos);
358
347
  }
359
- return `"${result}"`;
348
+ return '"' + result + '"';
360
349
  }
361
350
 
362
- // src/tree/types.ts
363
- var SerovalObjectRecordNode = class {
364
- constructor(k, v, s) {
365
- __publicField(this, "k");
366
- __publicField(this, "v");
367
- __publicField(this, "s");
368
- this.k = k;
369
- this.v = v;
370
- this.s = s;
371
- }
372
- };
373
- var SerovalMapRecordNode = class {
374
- constructor(k, v, s) {
375
- __publicField(this, "k");
376
- __publicField(this, "v");
377
- __publicField(this, "s");
378
- this.k = k;
379
- this.v = v;
380
- this.s = s;
381
- }
382
- };
383
- var SerovalPrimitiveNode = class {
384
- constructor(current) {
385
- __publicField(this, "t", 0 /* Primitive */);
386
- __publicField(this, "s");
387
- this.s = current;
388
- }
389
- };
390
- var SerovalReferenceNode = class {
391
- constructor(id) {
392
- __publicField(this, "t", 2 /* Reference */);
393
- __publicField(this, "i");
394
- this.i = id;
395
- }
396
- };
397
- var SerovalBigIntNode = class {
398
- constructor(current) {
399
- __publicField(this, "t", 1 /* BigInt */);
400
- __publicField(this, "s");
401
- this.s = `${current}n`;
402
- }
403
- };
404
- var SerovalDateNode = class {
405
- constructor(id, current) {
406
- __publicField(this, "t", 3 /* Date */);
407
- __publicField(this, "i");
408
- __publicField(this, "s");
409
- this.i = id;
410
- this.s = current.toISOString();
411
- }
412
- };
413
- var SerovalRegExpNode = class {
414
- constructor(id, current) {
415
- __publicField(this, "t", 4 /* RegExp */);
416
- __publicField(this, "i");
417
- __publicField(this, "s");
418
- this.i = id;
419
- this.s = String(current);
420
- }
421
- };
422
- var SerovalTypedArrayNode = class {
423
- constructor(id, current) {
424
- __publicField(this, "t", 14 /* TypedArray */);
425
- __publicField(this, "i");
426
- __publicField(this, "s");
427
- __publicField(this, "l");
428
- __publicField(this, "c");
429
- this.i = id;
430
- this.s = current.toString();
431
- this.l = current.byteOffset;
432
- this.c = current.constructor.name;
433
- }
434
- };
435
- var SerovalBigIntTypedArrayNode = class {
436
- constructor(id, current) {
437
- __publicField(this, "t", 15 /* BigIntTypedArray */);
438
- __publicField(this, "i");
439
- __publicField(this, "s");
440
- __publicField(this, "l");
441
- __publicField(this, "c");
442
- let result = "";
443
- const cap = current.length - 1;
444
- for (let i = 0; i < cap; i++) {
445
- result += `${current[i]}n,`;
446
- }
447
- result += `"${current[cap]}"`;
448
- this.i = id;
449
- this.s = result;
450
- this.l = current.byteOffset;
451
- this.c = current.constructor.name;
452
- }
453
- };
454
- var SerovalSetNode = class {
455
- constructor(id, a) {
456
- __publicField(this, "t", 5 /* Set */);
457
- __publicField(this, "i");
458
- __publicField(this, "a");
459
- this.i = id;
460
- this.a = a;
461
- }
462
- };
463
- var SerovalMapNode = class {
464
- constructor(id, d) {
465
- __publicField(this, "t", 6 /* Map */);
466
- __publicField(this, "i");
467
- __publicField(this, "d");
468
- this.i = id;
469
- this.d = d;
470
- }
471
- };
472
- var SerovalArrayNode = class {
473
- constructor(id, a) {
474
- __publicField(this, "t", 7 /* Array */);
475
- __publicField(this, "i");
476
- __publicField(this, "a");
477
- this.i = id;
478
- this.a = a;
479
- }
480
- };
481
- var SerovalObjectNode = class {
482
- constructor(id, d) {
483
- __publicField(this, "t", 8 /* Object */);
484
- __publicField(this, "i");
485
- __publicField(this, "d");
486
- this.i = id;
487
- this.d = d;
488
- }
489
- };
490
- var SerovalNullConstructorNode = class {
491
- constructor(id, d) {
492
- __publicField(this, "t", 9 /* NullConstructor */);
493
- __publicField(this, "i");
494
- __publicField(this, "d");
495
- this.i = id;
496
- this.d = d;
497
- }
498
- };
499
- var SerovalPromiseNode = class {
500
- constructor(id, n) {
501
- __publicField(this, "t", 10 /* Promise */);
502
- __publicField(this, "i");
503
- __publicField(this, "n");
504
- this.i = id;
505
- this.n = n;
506
- }
507
- };
508
- var SerovalErrorNode = class {
509
- constructor(id, c, m, d) {
510
- __publicField(this, "t", 11 /* Error */);
511
- __publicField(this, "i");
512
- __publicField(this, "c");
513
- __publicField(this, "m");
514
- __publicField(this, "d");
515
- this.i = id;
516
- this.c = c;
517
- this.m = m;
518
- this.d = d;
519
- }
520
- };
521
- var SerovalAggregateErrorNode = class {
522
- constructor(id, m, d, n) {
523
- __publicField(this, "t", 12 /* AggregateError */);
524
- __publicField(this, "i");
525
- __publicField(this, "m");
526
- __publicField(this, "d");
527
- __publicField(this, "n");
528
- this.i = id;
529
- this.m = m;
530
- this.d = d;
531
- this.n = n;
532
- }
533
- };
534
- var SerovalIterableNode = class {
535
- constructor(id, d, a) {
536
- __publicField(this, "t", 13 /* Iterable */);
537
- __publicField(this, "i");
538
- __publicField(this, "d");
539
- __publicField(this, "a");
540
- this.i = id;
541
- this.d = d;
542
- this.a = a;
543
- }
544
- };
545
-
546
351
  // src/tree/primitives.ts
547
- var TRUE_NODE = new SerovalPrimitiveNode("!0");
548
- var FALSE_NODE = new SerovalPrimitiveNode("!1");
549
- var UNDEFINED_NODE = new SerovalPrimitiveNode("void 0");
550
- var NULL_NODE = new SerovalPrimitiveNode(null);
551
- var NEG_ZERO_NODE = new SerovalPrimitiveNode("-0");
552
- var INFINITY_NODE = new SerovalPrimitiveNode("1/0");
553
- var NEG_INFINITY_NODE = new SerovalPrimitiveNode("-1/0");
352
+ function createPrimitiveNode(value) {
353
+ return {
354
+ t: 0 /* Primitive */,
355
+ i: void 0,
356
+ s: value,
357
+ l: void 0,
358
+ c: void 0,
359
+ m: void 0,
360
+ d: void 0,
361
+ a: void 0,
362
+ n: void 0
363
+ };
364
+ }
365
+ var TRUE_NODE = createPrimitiveNode("!0");
366
+ var FALSE_NODE = createPrimitiveNode("!1");
367
+ var UNDEFINED_NODE = createPrimitiveNode("void 0");
368
+ var NULL_NODE = createPrimitiveNode(null);
369
+ var NEG_ZERO_NODE = createPrimitiveNode("-0");
370
+ var INFINITY_NODE = createPrimitiveNode("1/0");
371
+ var NEG_INFINITY_NODE = createPrimitiveNode("-1/0");
372
+ function createBigIntNode(ctx, current) {
373
+ assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
374
+ return {
375
+ t: 1 /* BigInt */,
376
+ i: void 0,
377
+ s: current.toString() + "n",
378
+ l: void 0,
379
+ c: void 0,
380
+ m: void 0,
381
+ d: void 0,
382
+ a: void 0,
383
+ n: void 0
384
+ };
385
+ }
386
+ function createReferenceNode(id) {
387
+ return {
388
+ t: 2 /* Reference */,
389
+ i: id,
390
+ s: void 0,
391
+ l: void 0,
392
+ c: void 0,
393
+ m: void 0,
394
+ d: void 0,
395
+ a: void 0,
396
+ n: void 0
397
+ };
398
+ }
399
+ function createDateNode(id, current) {
400
+ return {
401
+ t: 3 /* Date */,
402
+ i: id,
403
+ s: current.toISOString(),
404
+ l: void 0,
405
+ c: void 0,
406
+ m: void 0,
407
+ d: void 0,
408
+ n: void 0,
409
+ a: void 0
410
+ };
411
+ }
412
+ function createRegExpNode(id, current) {
413
+ return {
414
+ t: 4 /* RegExp */,
415
+ i: id,
416
+ s: String(current),
417
+ l: void 0,
418
+ c: void 0,
419
+ m: void 0,
420
+ d: void 0,
421
+ a: void 0,
422
+ n: void 0
423
+ };
424
+ }
425
+ function createTypedArrayNode(ctx, id, current) {
426
+ const constructor = current.constructor.name;
427
+ assert(ctx.features & 1024 /* TypedArray */, `Unsupported value type "${constructor}"`);
428
+ return {
429
+ t: 14 /* TypedArray */,
430
+ i: id,
431
+ s: current.toString(),
432
+ l: current.byteOffset,
433
+ c: constructor,
434
+ m: void 0,
435
+ d: void 0,
436
+ a: void 0,
437
+ n: void 0
438
+ };
439
+ }
440
+ function createBigIntTypedArrayNode(ctx, id, current) {
441
+ const constructor = current.constructor.name;
442
+ assert(
443
+ ctx.features & 2048 /* BigIntTypedArray */,
444
+ `Unsupported value type "${constructor}"`
445
+ );
446
+ let result = "";
447
+ for (let i = 0, len = current.length; i < len; i++) {
448
+ if (i !== 0) {
449
+ result += ",";
450
+ }
451
+ result += current[i].toString() + "n";
452
+ }
453
+ return {
454
+ t: 15 /* BigIntTypedArray */,
455
+ i: id,
456
+ s: result,
457
+ l: current.byteOffset,
458
+ c: constructor,
459
+ m: void 0,
460
+ d: void 0,
461
+ a: void 0,
462
+ n: void 0
463
+ };
464
+ }
554
465
 
555
466
  // src/tree/shared.ts
556
467
  function getErrorConstructor(error) {
@@ -602,9 +513,6 @@ function getIterableOptions(obj) {
602
513
  }
603
514
  return void 0;
604
515
  }
605
- function isReferenceInStack(ctx, node) {
606
- return node.t === 2 /* Reference */ && ctx.stack.includes(node.i);
607
- }
608
516
  function isIterable(value) {
609
517
  if (!value || typeof value !== "object") {
610
518
  return false;
@@ -635,11 +543,7 @@ function isIterable(value) {
635
543
 
636
544
  // src/tree/async.ts
637
545
  var AsyncParser = class {
638
- constructor(ctx) {
639
- __publicField(this, "ctx");
640
- this.ctx = ctx;
641
- }
642
- async generateNodeList(current) {
546
+ static async generateNodeList(ctx, current) {
643
547
  const size = current.length;
644
548
  const nodes = new Array(size);
645
549
  const deferred = new Array(size);
@@ -650,22 +554,32 @@ var AsyncParser = class {
650
554
  if (isIterable(item)) {
651
555
  deferred[i] = item;
652
556
  } else {
653
- nodes[i] = await this.parse(item);
557
+ nodes[i] = await this.parse(ctx, item);
654
558
  }
655
559
  }
656
560
  }
657
561
  for (let i = 0; i < size; i++) {
658
562
  if (i in deferred) {
659
- nodes[i] = await this.parse(deferred[i]);
563
+ nodes[i] = await this.parse(ctx, deferred[i]);
660
564
  }
661
565
  }
662
566
  return nodes;
663
567
  }
664
- async generateArrayNode(id, current) {
665
- return new SerovalArrayNode(id, await this.generateNodeList(current));
568
+ static async generateArrayNode(ctx, id, current) {
569
+ return {
570
+ t: 7 /* Array */,
571
+ i: id,
572
+ s: void 0,
573
+ l: void 0,
574
+ c: void 0,
575
+ m: void 0,
576
+ d: void 0,
577
+ a: await this.generateNodeList(ctx, current),
578
+ n: void 0
579
+ };
666
580
  }
667
- async generateMapNode(id, current) {
668
- assert(this.ctx.features & 16 /* Map */, 'Unsupported type "Map"');
581
+ static async generateMapNode(ctx, id, current) {
582
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
669
583
  const len = current.size;
670
584
  const keyNodes = new Array(len);
671
585
  const valueNodes = new Array(len);
@@ -679,19 +593,29 @@ var AsyncParser = class {
679
593
  deferredValue[deferredSize] = value;
680
594
  deferredSize++;
681
595
  } else {
682
- keyNodes[nodeSize] = await this.parse(key);
683
- valueNodes[nodeSize] = await this.parse(value);
596
+ keyNodes[nodeSize] = await this.parse(ctx, key);
597
+ valueNodes[nodeSize] = await this.parse(ctx, value);
684
598
  nodeSize++;
685
599
  }
686
600
  }
687
601
  for (let i = 0; i < deferredSize; i++) {
688
- keyNodes[nodeSize + i] = await this.parse(deferredKey[i]);
689
- valueNodes[nodeSize + i] = await this.parse(deferredValue[i]);
602
+ keyNodes[nodeSize + i] = await this.parse(ctx, deferredKey[i]);
603
+ valueNodes[nodeSize + i] = await this.parse(ctx, deferredValue[i]);
690
604
  }
691
- return new SerovalMapNode(id, new SerovalMapRecordNode(keyNodes, valueNodes, len));
605
+ return {
606
+ t: 6 /* Map */,
607
+ i: id,
608
+ s: void 0,
609
+ l: void 0,
610
+ c: void 0,
611
+ m: void 0,
612
+ d: { k: keyNodes, v: valueNodes, s: len },
613
+ a: void 0,
614
+ n: void 0
615
+ };
692
616
  }
693
- async generateSetNode(id, current) {
694
- assert(this.ctx.features & 256 /* Set */, 'Unsupported type "Set"');
617
+ static async generateSetNode(ctx, id, current) {
618
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
695
619
  const len = current.size;
696
620
  const nodes = new Array(len);
697
621
  const deferred = new Array(len);
@@ -701,15 +625,25 @@ var AsyncParser = class {
701
625
  if (isIterable(item)) {
702
626
  deferred[deferredSize++] = item;
703
627
  } else {
704
- nodes[nodeSize++] = await this.parse(item);
628
+ nodes[nodeSize++] = await this.parse(ctx, item);
705
629
  }
706
630
  }
707
631
  for (let i = 0; i < deferredSize; i++) {
708
- nodes[nodeSize + i] = await this.parse(deferred[i]);
632
+ nodes[nodeSize + i] = await this.parse(ctx, deferred[i]);
709
633
  }
710
- return new SerovalSetNode(id, nodes);
634
+ return {
635
+ t: 5 /* Set */,
636
+ i: id,
637
+ s: void 0,
638
+ l: void 0,
639
+ c: void 0,
640
+ m: void 0,
641
+ d: void 0,
642
+ a: nodes,
643
+ n: void 0
644
+ };
711
645
  }
712
- async generateProperties(properties) {
646
+ static async generateProperties(ctx, properties) {
713
647
  const keys = Object.keys(properties);
714
648
  const size = keys.length;
715
649
  const keyNodes = new Array(size);
@@ -727,98 +661,139 @@ var AsyncParser = class {
727
661
  deferredSize++;
728
662
  } else {
729
663
  keyNodes[nodesSize] = key;
730
- valueNodes[nodesSize] = await this.parse(item);
664
+ valueNodes[nodesSize] = await this.parse(ctx, item);
731
665
  nodesSize++;
732
666
  }
733
667
  }
734
668
  for (let i = 0; i < deferredSize; i++) {
735
669
  keyNodes[nodesSize + i] = deferredKeys[i];
736
- valueNodes[nodesSize + i] = await this.parse(deferredValues[i]);
670
+ valueNodes[nodesSize + i] = await this.parse(ctx, deferredValues[i]);
737
671
  }
738
- return new SerovalObjectRecordNode(keyNodes, valueNodes, size);
672
+ return {
673
+ k: keyNodes,
674
+ v: valueNodes,
675
+ s: size
676
+ };
739
677
  }
740
- async generateIterableNode(id, current) {
741
- assert(this.ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
678
+ static async generateIterableNode(ctx, id, current) {
679
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
742
680
  const options = getIterableOptions(current);
743
- return new SerovalIterableNode(
744
- id,
681
+ return {
682
+ t: 13 /* Iterable */,
683
+ i: id,
684
+ s: void 0,
685
+ l: void 0,
686
+ c: void 0,
687
+ m: void 0,
745
688
  // Parse options first before the items
746
- options ? await this.generateProperties(options) : void 0,
747
- await this.generateNodeList(Array.from(current))
748
- );
689
+ d: options ? await this.generateProperties(ctx, options) : void 0,
690
+ a: await this.generateNodeList(ctx, Array.from(current)),
691
+ n: void 0
692
+ };
749
693
  }
750
- async generatePromiseNode(id, current) {
751
- assert(this.ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
752
- return current.then(async (value) => new SerovalPromiseNode(id, await this.parse(value)));
694
+ static async generatePromiseNode(ctx, id, current) {
695
+ assert(ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
696
+ return current.then(async (value) => ({
697
+ t: 10 /* Promise */,
698
+ i: id,
699
+ s: void 0,
700
+ l: void 0,
701
+ c: void 0,
702
+ m: void 0,
703
+ // Parse options first before the items
704
+ d: void 0,
705
+ a: void 0,
706
+ n: await this.parse(ctx, value)
707
+ }));
753
708
  }
754
- async generateObjectNode(id, current, empty) {
709
+ static async generateObjectNode(ctx, id, current, empty) {
755
710
  if (Symbol.iterator in current) {
756
- return this.generateIterableNode(id, current);
711
+ return this.generateIterableNode(ctx, id, current);
757
712
  }
758
713
  if ("then" in current && typeof current.then === "function") {
759
- return this.generatePromiseNode(id, current);
760
- }
761
- const properties = await this.generateProperties(current);
762
- if (empty) {
763
- return new SerovalNullConstructorNode(id, properties);
714
+ return this.generatePromiseNode(ctx, id, current);
764
715
  }
765
- return new SerovalObjectNode(id, properties);
716
+ return {
717
+ t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
718
+ i: id,
719
+ s: void 0,
720
+ l: void 0,
721
+ c: void 0,
722
+ m: void 0,
723
+ d: await this.generateProperties(ctx, current),
724
+ a: void 0,
725
+ n: void 0
726
+ };
766
727
  }
767
- async generateAggregateErrorNode(id, current) {
728
+ static async generateAggregateErrorNode(ctx, id, current) {
768
729
  const options = getErrorOptions(current);
769
- const optionsNode = options ? await this.generateProperties(options) : void 0;
770
- return new SerovalAggregateErrorNode(
771
- id,
772
- current.message,
773
- optionsNode,
774
- await this.parse(current.errors)
775
- );
776
- }
777
- async generateErrorNode(id, current) {
730
+ const optionsNode = options ? await this.generateProperties(ctx, options) : void 0;
731
+ return {
732
+ t: 12 /* AggregateError */,
733
+ i: id,
734
+ s: void 0,
735
+ l: void 0,
736
+ c: void 0,
737
+ m: current.message,
738
+ d: optionsNode,
739
+ a: void 0,
740
+ n: await this.parse(ctx, current.errors)
741
+ };
742
+ }
743
+ static async generateErrorNode(ctx, id, current) {
778
744
  const options = getErrorOptions(current);
779
- const optionsNode = options ? await this.generateProperties(options) : void 0;
780
- return new SerovalErrorNode(id, getErrorConstructor(current), current.message, optionsNode);
745
+ const optionsNode = options ? await this.generateProperties(ctx, options) : void 0;
746
+ return {
747
+ t: 11 /* Error */,
748
+ i: id,
749
+ s: void 0,
750
+ l: void 0,
751
+ c: getErrorConstructor(current),
752
+ m: current.message,
753
+ d: optionsNode,
754
+ a: void 0,
755
+ n: void 0
756
+ };
781
757
  }
782
- async parse(current) {
758
+ static async parse(ctx, current) {
783
759
  switch (typeof current) {
784
760
  case "boolean":
785
761
  return current ? TRUE_NODE : FALSE_NODE;
786
762
  case "undefined":
787
763
  return UNDEFINED_NODE;
788
764
  case "string":
789
- return new SerovalPrimitiveNode(quote(current));
765
+ return createPrimitiveNode(quote(current));
790
766
  case "number":
791
767
  if (Object.is(current, -0)) {
792
768
  return NEG_ZERO_NODE;
793
769
  }
794
770
  if (Object.is(current, Infinity)) {
795
- return INFINITY_NODE;
771
+ return createPrimitiveNode("1/0");
796
772
  }
797
773
  if (Object.is(current, -Infinity)) {
798
- return NEG_INFINITY_NODE;
774
+ return createPrimitiveNode("-1/0");
799
775
  }
800
- return new SerovalPrimitiveNode(current);
776
+ return createPrimitiveNode(current);
801
777
  case "bigint":
802
- assert(this.ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
803
- return new SerovalBigIntNode(current);
778
+ return createBigIntNode(ctx, current);
804
779
  case "object": {
805
780
  if (!current) {
806
781
  return NULL_NODE;
807
782
  }
808
- const id = createRef(this.ctx, current, true);
809
- if (this.ctx.markedRefs[id]) {
810
- return new SerovalReferenceNode(id);
783
+ const id = createRef(ctx, current, true);
784
+ if (ctx.markedRefs.has(id)) {
785
+ return createReferenceNode(id);
811
786
  }
812
787
  if (Array.isArray(current)) {
813
- return this.generateArrayNode(id, current);
788
+ return this.generateArrayNode(ctx, id, current);
814
789
  }
815
790
  switch (current.constructor) {
816
791
  case Date:
817
- return new SerovalDateNode(id, current);
792
+ return createDateNode(id, current);
818
793
  case RegExp:
819
- return new SerovalRegExpNode(id, current);
794
+ return createRegExpNode(id, current);
820
795
  case Promise:
821
- return this.generatePromiseNode(id, current);
796
+ return this.generatePromiseNode(ctx, id, current);
822
797
  case Int8Array:
823
798
  case Int16Array:
824
799
  case Int32Array:
@@ -828,28 +803,41 @@ var AsyncParser = class {
828
803
  case Uint8ClampedArray:
829
804
  case Float32Array:
830
805
  case Float64Array:
831
- assert(this.ctx.features & 1024 /* TypedArray */, `Unsupported value type "${current.constructor.name}"`);
832
- return new SerovalTypedArrayNode(id, current);
806
+ return createTypedArrayNode(ctx, id, current);
833
807
  case BigInt64Array:
834
808
  case BigUint64Array:
835
- assert(
836
- this.ctx.features & 2048 /* BigIntTypedArray */,
837
- `Unsupported value type "${current.constructor.name}"`
838
- );
839
- return new SerovalBigIntTypedArrayNode(id, current);
809
+ return createBigIntTypedArrayNode(ctx, id, current);
840
810
  case Map:
841
- return this.generateMapNode(id, current);
811
+ return this.generateMapNode(
812
+ ctx,
813
+ id,
814
+ current
815
+ );
842
816
  case Set:
843
- return this.generateSetNode(id, current);
817
+ return this.generateSetNode(
818
+ ctx,
819
+ id,
820
+ current
821
+ );
844
822
  case Object:
845
- return this.generateObjectNode(id, current, false);
823
+ return this.generateObjectNode(
824
+ ctx,
825
+ id,
826
+ current,
827
+ false
828
+ );
846
829
  case void 0:
847
- return this.generateObjectNode(id, current, true);
830
+ return this.generateObjectNode(
831
+ ctx,
832
+ id,
833
+ current,
834
+ true
835
+ );
848
836
  case AggregateError:
849
- if (this.ctx.features & 1 /* AggregateError */) {
850
- return this.generateAggregateErrorNode(id, current);
837
+ if (ctx.features & 1 /* AggregateError */) {
838
+ return this.generateAggregateErrorNode(ctx, id, current);
851
839
  }
852
- return this.generateErrorNode(id, current);
840
+ return this.generateErrorNode(ctx, id, current);
853
841
  case Error:
854
842
  case EvalError:
855
843
  case RangeError:
@@ -857,24 +845,24 @@ var AsyncParser = class {
857
845
  case SyntaxError:
858
846
  case TypeError:
859
847
  case URIError:
860
- return this.generateErrorNode(id, current);
848
+ return this.generateErrorNode(ctx, id, current);
861
849
  default:
862
850
  break;
863
851
  }
864
- if (current instanceof AggregateError && this.ctx.features & 1 /* AggregateError */) {
865
- return this.generateAggregateErrorNode(id, current);
852
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
853
+ return this.generateAggregateErrorNode(ctx, id, current);
866
854
  }
867
855
  if (current instanceof Error) {
868
- return this.generateErrorNode(id, current);
856
+ return this.generateErrorNode(ctx, id, current);
869
857
  }
870
858
  if (current instanceof Promise) {
871
- return this.generatePromiseNode(id, current);
859
+ return this.generatePromiseNode(ctx, id, current);
872
860
  }
873
861
  if (Symbol.iterator in current) {
874
- return this.generateIterableNode(id, current);
862
+ return this.generateIterableNode(ctx, id, current);
875
863
  }
876
864
  if ("then" in current && typeof current.then === "function") {
877
- return this.generatePromiseNode(id, current);
865
+ return this.generatePromiseNode(ctx, id, current);
878
866
  }
879
867
  throw new Error("Unsupported type");
880
868
  }
@@ -884,20 +872,20 @@ var AsyncParser = class {
884
872
  }
885
873
  };
886
874
  async function parseAsync(ctx, current) {
887
- const result = await new AsyncParser(ctx).parse(current);
875
+ const result = await AsyncParser.parse(ctx, current);
888
876
  const isObject = result.t === 8 /* Object */ || result.t === 13 /* Iterable */;
889
877
  return [result, createRef(ctx, current, false), isObject];
890
878
  }
891
879
 
892
880
  // src/tree/serialize.ts
893
881
  function getAssignmentExpression(assignment) {
894
- switch (assignment.type) {
882
+ switch (assignment.t) {
895
883
  case "index":
896
- return `${assignment.source}=${assignment.value}`;
884
+ return assignment.s + "=" + assignment.v;
897
885
  case "map":
898
- return `${assignment.source}.set(${assignment.key},${assignment.value})`;
886
+ return assignment.s + ".set(" + assignment.k + "," + assignment.v + ")";
899
887
  case "set":
900
- return `${assignment.source}.add(${assignment.value})`;
888
+ return assignment.s + ".add(" + assignment.v + ")";
901
889
  default:
902
890
  return "";
903
891
  }
@@ -906,27 +894,30 @@ function mergeAssignments(assignments) {
906
894
  const newAssignments = [];
907
895
  let current = assignments[0];
908
896
  let prev = current;
897
+ let item;
909
898
  for (let i = 1, len = assignments.length; i < len; i++) {
910
- const item = assignments[i];
911
- if (item.type === prev.type) {
912
- if (item.type === "index" && item.value === prev.value) {
899
+ item = assignments[i];
900
+ if (item.t === prev.t) {
901
+ if (item.t === "index" && item.v === prev.v) {
913
902
  current = {
914
- type: "index",
915
- source: item.source,
916
- value: getAssignmentExpression(current)
903
+ t: "index",
904
+ s: item.s,
905
+ k: void 0,
906
+ v: getAssignmentExpression(current)
917
907
  };
918
- } else if (item.type === "map" && item.source === prev.source) {
908
+ } else if (item.t === "map" && item.s === prev.s) {
919
909
  current = {
920
- type: "map",
921
- source: getAssignmentExpression(current),
922
- key: item.key,
923
- value: item.value
910
+ t: "map",
911
+ s: getAssignmentExpression(current),
912
+ k: item.k,
913
+ v: item.v
924
914
  };
925
- } else if (item.type === "set" && item.source === prev.source) {
915
+ } else if (item.t === "set" && item.s === prev.s) {
926
916
  current = {
927
- type: "set",
928
- source: getAssignmentExpression(current),
929
- value: item.value
917
+ t: "set",
918
+ s: getAssignmentExpression(current),
919
+ k: void 0,
920
+ v: item.v
930
921
  };
931
922
  } else {
932
923
  newAssignments.push(current);
@@ -946,7 +937,7 @@ function resolveAssignments(assignments) {
946
937
  let result = "";
947
938
  const merged = mergeAssignments(assignments);
948
939
  for (let i = 0, len = merged.length; i < len; i++) {
949
- result += `${getAssignmentExpression(merged[i])},`;
940
+ result += getAssignmentExpression(merged[i]) + ",";
950
941
  }
951
942
  return result;
952
943
  }
@@ -955,328 +946,338 @@ function resolveAssignments(assignments) {
955
946
  function resolvePatches(ctx) {
956
947
  return resolveAssignments(ctx.assignments);
957
948
  }
958
- function assignRef(ctx, index, value) {
959
- if (ctx.markedRefs[index]) {
960
- return `${getRefParam(ctx, index)}=${value}`;
961
- }
962
- return value;
963
- }
964
949
  function createAssignment(ctx, source, value) {
965
950
  ctx.assignments.push({
966
- type: "index",
967
- source,
968
- value
951
+ t: "index",
952
+ s: source,
953
+ k: void 0,
954
+ v: value
969
955
  });
970
956
  }
971
957
  function createSetAdd(ctx, ref, value) {
972
958
  markRef(ctx, ref);
973
959
  ctx.assignments.push({
974
- type: "set",
975
- source: getRefParam(ctx, ref),
976
- value
960
+ t: "set",
961
+ s: getRefParam(ctx, ref),
962
+ k: void 0,
963
+ v: value
977
964
  });
978
965
  }
979
966
  function createMapSet(ctx, ref, key, value) {
980
967
  markRef(ctx, ref);
981
968
  ctx.assignments.push({
982
- type: "map",
983
- source: getRefParam(ctx, ref),
984
- key,
985
- value
969
+ t: "map",
970
+ s: getRefParam(ctx, ref),
971
+ k: key,
972
+ v: value
986
973
  });
987
974
  }
988
975
  function createArrayAssign(ctx, ref, index, value) {
989
976
  markRef(ctx, ref);
990
- createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
977
+ createAssignment(ctx, getRefParam(ctx, ref) + "[" + index.toString() + "]", value);
991
978
  }
992
979
  function createObjectAssign(ctx, ref, key, value) {
993
980
  markRef(ctx, ref);
994
- createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
981
+ createAssignment(ctx, getRefParam(ctx, ref) + "." + key, value);
995
982
  }
996
- var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
997
- function serializeAssignments(ctx, targetRef, node) {
998
- ctx.stack.push(targetRef);
999
- const mainAssignments = [];
1000
- for (let i = 0; i < node.s; i++) {
1001
- const parentStack = ctx.stack;
1002
- ctx.stack = [];
1003
- const refParam = serializeTree(ctx, node.v[i]);
1004
- ctx.stack = parentStack;
1005
- const key = node.k[i];
1006
- const check = Number(key);
1007
- const parentAssignment = ctx.assignments;
1008
- ctx.assignments = mainAssignments;
1009
- const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1010
- if (isIdentifier && Number.isNaN(check)) {
1011
- createObjectAssign(ctx, targetRef, key, refParam);
1012
- } else {
1013
- createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
1014
- }
1015
- ctx.assignments = parentAssignment;
983
+ function assignRef(ctx, index, value) {
984
+ if (ctx.markedRefs.has(index)) {
985
+ return getRefParam(ctx, index) + "=" + value;
1016
986
  }
1017
- ctx.stack.pop();
1018
- return resolveAssignments(mainAssignments);
987
+ return value;
1019
988
  }
1020
- function serializeObject(ctx, sourceID, node) {
1021
- if (node.s === 0) {
1022
- return "{}";
1023
- }
1024
- let result = "";
1025
- ctx.stack.push(sourceID);
1026
- for (let i = 0; i < node.s; i++) {
1027
- const key = node.k[i];
1028
- const val = node.v[i];
1029
- const check = Number(key);
1030
- const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1031
- if (isReferenceInStack(ctx, val)) {
1032
- const refParam = getRefParam(ctx, val.i);
1033
- if (isIdentifier && Number.isNaN(check)) {
1034
- createObjectAssign(ctx, sourceID, key, refParam);
989
+ function isReferenceInStack(ctx, node) {
990
+ return node.t === 2 /* Reference */ && ctx.stack.includes(node.i);
991
+ }
992
+ var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
993
+ var SerovalSerializer = class {
994
+ static serializeNodeList(ctx, node) {
995
+ const size = node.a.length;
996
+ let values = "";
997
+ let item;
998
+ let isHoley = false;
999
+ for (let i = 0; i < size; i++) {
1000
+ if (i !== 0) {
1001
+ values += ",";
1002
+ }
1003
+ item = node.a[i];
1004
+ if (item) {
1005
+ if (isReferenceInStack(ctx, item)) {
1006
+ createArrayAssign(ctx, node.i, i, getRefParam(ctx, item.i));
1007
+ isHoley = true;
1008
+ } else {
1009
+ values += this.serialize(ctx, item);
1010
+ isHoley = false;
1011
+ }
1035
1012
  } else {
1036
- createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1013
+ isHoley = true;
1037
1014
  }
1038
- } else {
1039
- result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
1040
1015
  }
1016
+ if (isHoley) {
1017
+ values += ",";
1018
+ }
1019
+ return "[" + values + "]";
1041
1020
  }
1042
- ctx.stack.pop();
1043
- return `{${result.substring(0, result.length - 1)}}`;
1044
- }
1045
- function serializePromise(ctx, node) {
1046
- let serialized;
1047
- if (isReferenceInStack(ctx, node.n)) {
1048
- serialized = `Promise.resolve().then(()=>${getRefParam(ctx, node.n.i)})`;
1049
- } else {
1021
+ static serializeArray(ctx, node) {
1050
1022
  ctx.stack.push(node.i);
1051
- const result = serializeTree(ctx, node.n);
1023
+ const result = this.serializeNodeList(ctx, node);
1052
1024
  ctx.stack.pop();
1053
- serialized = `Promise.resolve(${result})`;
1025
+ return assignRef(ctx, node.i, result);
1054
1026
  }
1055
- return assignRef(ctx, node.i, serialized);
1056
- }
1057
- function serializeTypedArray(ctx, node) {
1058
- let args = `[${node.s}]`;
1059
- if (node.l !== 0) {
1060
- args += `,${node.l}`;
1061
- }
1062
- return assignRef(ctx, node.i, `new ${node.c}(${args})`);
1063
- }
1064
- function serializeSet(ctx, node) {
1065
- let serialized = "new Set";
1066
- const size = node.a.length;
1067
- if (size) {
1027
+ static serializeObject(ctx, sourceID, node) {
1028
+ if (node.s === 0) {
1029
+ return "{}";
1030
+ }
1068
1031
  let result = "";
1069
- ctx.stack.push(node.i);
1070
- for (let i = 0; i < size; i++) {
1071
- const item = node.a[i];
1072
- if (isReferenceInStack(ctx, item)) {
1073
- createSetAdd(ctx, node.i, getRefParam(ctx, item.i));
1032
+ ctx.stack.push(sourceID);
1033
+ let key;
1034
+ let val;
1035
+ let check;
1036
+ let isIdentifier;
1037
+ let refParam;
1038
+ let hasPrev = false;
1039
+ for (let i = 0; i < node.s; i++) {
1040
+ key = node.k[i];
1041
+ val = node.v[i];
1042
+ check = Number(key);
1043
+ isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1044
+ if (isReferenceInStack(ctx, val)) {
1045
+ refParam = getRefParam(ctx, val.i);
1046
+ if (isIdentifier && Number.isNaN(check)) {
1047
+ createObjectAssign(ctx, sourceID, key, refParam);
1048
+ } else {
1049
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1050
+ }
1074
1051
  } else {
1075
- result += `${serializeTree(ctx, item)},`;
1052
+ if (hasPrev) {
1053
+ result += ",";
1054
+ }
1055
+ result += isIdentifier ? key : quote(key);
1056
+ result += ":" + this.serialize(ctx, val);
1057
+ hasPrev = true;
1076
1058
  }
1077
1059
  }
1078
1060
  ctx.stack.pop();
1079
- if (result) {
1080
- serialized += `([${result.substring(0, result.length - 1)}])`;
1081
- }
1061
+ return "{" + result + "}";
1082
1062
  }
1083
- return assignRef(ctx, node.i, serialized);
1084
- }
1085
- function serializeMap(ctx, node) {
1086
- let serialized = "new Map";
1087
- if (node.d.s) {
1088
- let result = "";
1089
- ctx.stack.push(node.i);
1090
- for (let i = 0; i < node.d.s; i++) {
1091
- const key = node.d.k[i];
1092
- const val = node.d.v[i];
1093
- if (isReferenceInStack(ctx, key)) {
1094
- const keyRef = getRefParam(ctx, key.i);
1095
- if (isReferenceInStack(ctx, val)) {
1096
- const valueRef = getRefParam(ctx, val.i);
1097
- createMapSet(ctx, node.i, keyRef, valueRef);
1098
- } else {
1099
- const parent = ctx.stack;
1100
- ctx.stack = [];
1101
- createMapSet(ctx, node.i, keyRef, serializeTree(ctx, val));
1102
- ctx.stack = parent;
1103
- }
1104
- } else if (isReferenceInStack(ctx, val)) {
1105
- const valueRef = getRefParam(ctx, val.i);
1106
- const parent = ctx.stack;
1107
- ctx.stack = [];
1108
- createMapSet(ctx, node.i, serializeTree(ctx, key), valueRef);
1109
- ctx.stack = parent;
1063
+ static serializeWithObjectAssign(ctx, value, id, serialized) {
1064
+ const fields = this.serializeObject(ctx, id, value);
1065
+ if (fields !== "{}") {
1066
+ return "Object.assign(" + serialized + "," + fields + ")";
1067
+ }
1068
+ return serialized;
1069
+ }
1070
+ static serializeAssignments(ctx, sourceID, node) {
1071
+ ctx.stack.push(sourceID);
1072
+ const mainAssignments = [];
1073
+ let parentStack;
1074
+ let refParam;
1075
+ let key;
1076
+ let check;
1077
+ let parentAssignment;
1078
+ let isIdentifier;
1079
+ for (let i = 0; i < node.s; i++) {
1080
+ parentStack = ctx.stack;
1081
+ ctx.stack = [];
1082
+ refParam = this.serialize(ctx, node.v[i]);
1083
+ ctx.stack = parentStack;
1084
+ key = node.k[i];
1085
+ check = Number(key);
1086
+ parentAssignment = ctx.assignments;
1087
+ ctx.assignments = mainAssignments;
1088
+ isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
1089
+ if (isIdentifier && Number.isNaN(check)) {
1090
+ createObjectAssign(ctx, sourceID, key, refParam);
1110
1091
  } else {
1111
- result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
1092
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
1112
1093
  }
1094
+ ctx.assignments = parentAssignment;
1113
1095
  }
1114
1096
  ctx.stack.pop();
1115
- if (result) {
1116
- serialized += `([${result.substring(0, result.length - 1)}])`;
1117
- }
1097
+ return resolveAssignments(mainAssignments);
1118
1098
  }
1119
- return assignRef(ctx, node.i, serialized);
1120
- }
1121
- function serializeNodeList(ctx, node) {
1122
- const size = node.a.length;
1123
- let values = "";
1124
- for (let i = 0; i < size; i++) {
1125
- const item = node.a[i];
1126
- if (item) {
1127
- if (isReferenceInStack(ctx, item)) {
1128
- createArrayAssign(ctx, node.i, i, getRefParam(ctx, item.i));
1129
- values += ",";
1099
+ static serializeDictionary(ctx, i, d, init) {
1100
+ if (d) {
1101
+ if (ctx.features & 64 /* ObjectAssign */) {
1102
+ init = this.serializeWithObjectAssign(ctx, d, i, init);
1130
1103
  } else {
1131
- values += serializeTree(ctx, item);
1132
- if (i < size - 1) {
1133
- values += ",";
1104
+ markRef(ctx, i);
1105
+ const assignments = this.serializeAssignments(ctx, i, d);
1106
+ if (assignments) {
1107
+ const ref = getRefParam(ctx, i);
1108
+ return "(" + assignRef(ctx, i, init) + "," + assignments + ref + ")";
1134
1109
  }
1135
1110
  }
1136
- } else {
1137
- values += ",";
1138
1111
  }
1139
- }
1140
- return `[${values}]`;
1141
- }
1142
- function serializeArray(ctx, node) {
1143
- ctx.stack.push(node.i);
1144
- const result = serializeNodeList(ctx, node);
1145
- ctx.stack.pop();
1146
- return assignRef(ctx, node.i, result);
1147
- }
1148
- function serializeWithObjectAssign(ctx, value, id, serialized) {
1149
- const fields = serializeObject(ctx, id, value);
1150
- if (fields !== "{}") {
1151
- return `Object.assign(${serialized},${fields})`;
1152
- }
1153
- return serialized;
1154
- }
1155
- function serializeAggregateError(ctx, node) {
1156
- ctx.stack.push(node.i);
1157
- let serialized = `new AggregateError(${serializeTree(ctx, node.n)},${quote(node.m)})`;
1158
- ctx.stack.pop();
1159
- if (node.d) {
1160
- if (ctx.features & 64 /* ObjectAssign */) {
1161
- serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1162
- } else {
1163
- markRef(ctx, node.i);
1164
- const assignments = serializeAssignments(ctx, node.i, node.d);
1165
- if (assignments) {
1166
- const ref = getRefParam(ctx, node.i);
1167
- return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
1112
+ return assignRef(ctx, i, init);
1113
+ }
1114
+ static serializeNullConstructor(ctx, node) {
1115
+ return this.serializeDictionary(ctx, node.i, node.d, "Object.create(null)");
1116
+ }
1117
+ static serializeSet(ctx, node) {
1118
+ let serialized = "new Set";
1119
+ const size = node.a.length;
1120
+ if (size) {
1121
+ let result = "";
1122
+ ctx.stack.push(node.i);
1123
+ let item;
1124
+ let hasPrev = false;
1125
+ for (let i = 0; i < size; i++) {
1126
+ item = node.a[i];
1127
+ if (isReferenceInStack(ctx, item)) {
1128
+ createSetAdd(ctx, node.i, getRefParam(ctx, item.i));
1129
+ } else {
1130
+ if (hasPrev) {
1131
+ result += ",";
1132
+ }
1133
+ result += this.serialize(ctx, item);
1134
+ hasPrev = true;
1135
+ }
1136
+ }
1137
+ ctx.stack.pop();
1138
+ if (result) {
1139
+ serialized += "([" + result + "])";
1168
1140
  }
1169
1141
  }
1170
- }
1171
- return assignRef(ctx, node.i, serialized);
1172
- }
1173
- function serializeError(ctx, node) {
1174
- let serialized = `new ${node.c}(${quote(node.m)})`;
1175
- if (node.d) {
1176
- if (ctx.features & 64 /* ObjectAssign */) {
1177
- serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1178
- } else {
1179
- markRef(ctx, node.i);
1180
- const assignments = serializeAssignments(ctx, node.i, node.d);
1181
- if (assignments) {
1182
- const ref = getRefParam(ctx, node.i);
1183
- return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
1142
+ return assignRef(ctx, node.i, serialized);
1143
+ }
1144
+ static serializeMap(ctx, node) {
1145
+ let serialized = "new Map";
1146
+ if (node.d.s) {
1147
+ let result = "";
1148
+ ctx.stack.push(node.i);
1149
+ let key;
1150
+ let val;
1151
+ let keyRef;
1152
+ let valueRef;
1153
+ let parent;
1154
+ let hasPrev = false;
1155
+ for (let i = 0; i < node.d.s; i++) {
1156
+ key = node.d.k[i];
1157
+ val = node.d.v[i];
1158
+ if (isReferenceInStack(ctx, key)) {
1159
+ keyRef = getRefParam(ctx, key.i);
1160
+ if (isReferenceInStack(ctx, val)) {
1161
+ valueRef = getRefParam(ctx, val.i);
1162
+ createMapSet(ctx, node.i, keyRef, valueRef);
1163
+ } else {
1164
+ parent = ctx.stack;
1165
+ ctx.stack = [];
1166
+ createMapSet(ctx, node.i, keyRef, this.serialize(ctx, val));
1167
+ ctx.stack = parent;
1168
+ }
1169
+ } else if (isReferenceInStack(ctx, val)) {
1170
+ valueRef = getRefParam(ctx, val.i);
1171
+ parent = ctx.stack;
1172
+ ctx.stack = [];
1173
+ createMapSet(ctx, node.i, this.serialize(ctx, key), valueRef);
1174
+ ctx.stack = parent;
1175
+ } else {
1176
+ if (hasPrev) {
1177
+ result += ",";
1178
+ }
1179
+ result += "[" + this.serialize(ctx, key) + "," + this.serialize(ctx, val) + "]";
1180
+ hasPrev = true;
1181
+ }
1182
+ }
1183
+ ctx.stack.pop();
1184
+ if (result) {
1185
+ serialized += "([" + result + "])";
1184
1186
  }
1185
1187
  }
1188
+ return assignRef(ctx, node.i, serialized);
1186
1189
  }
1187
- return assignRef(ctx, node.i, serialized);
1188
- }
1189
- function serializeIterable(ctx, node) {
1190
- const parent = ctx.stack;
1191
- ctx.stack = [];
1192
- const values = serializeNodeList(ctx, node);
1193
- ctx.stack = parent;
1194
- let serialized;
1195
- if (ctx.features & 2 /* ArrayPrototypeValues */) {
1196
- serialized = `${values}.values()`;
1197
- } else {
1198
- serialized = `${values}[Symbol.iterator]()`;
1190
+ static serializeAggregateError(ctx, node) {
1191
+ ctx.stack.push(node.i);
1192
+ const serialized = "new AggregateError(" + this.serialize(ctx, node.n) + "," + quote(node.m) + ")";
1193
+ ctx.stack.pop();
1194
+ return this.serializeDictionary(ctx, node.i, node.d, serialized);
1199
1195
  }
1200
- if (ctx.features & 4 /* ArrowFunction */) {
1201
- serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1202
- } else if (ctx.features & 32 /* MethodShorthand */) {
1203
- serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1204
- } else {
1205
- serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1196
+ static serializeError(ctx, node) {
1197
+ const serialized = "new " + node.c + "(" + quote(node.m) + ")";
1198
+ return this.serializeDictionary(ctx, node.i, node.d, serialized);
1206
1199
  }
1207
- if (node.d) {
1208
- if (ctx.features & 64 /* ObjectAssign */) {
1209
- serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1200
+ static serializePromise(ctx, node) {
1201
+ let serialized;
1202
+ if (isReferenceInStack(ctx, node.n)) {
1203
+ serialized = "Promise.resolve().then(()=>" + getRefParam(ctx, node.n.i) + ")";
1210
1204
  } else {
1211
- markRef(ctx, node.i);
1212
- const assignments = serializeAssignments(ctx, node.i, node.d);
1213
- if (assignments) {
1214
- const ref = getRefParam(ctx, node.i);
1215
- return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
1216
- }
1205
+ ctx.stack.push(node.i);
1206
+ const result = this.serialize(ctx, node.n);
1207
+ ctx.stack.pop();
1208
+ serialized = "Promise.resolve(" + result + ")";
1217
1209
  }
1210
+ return assignRef(ctx, node.i, serialized);
1218
1211
  }
1219
- return assignRef(ctx, node.i, serialized);
1220
- }
1221
- function serializeNullConstructor(ctx, node) {
1222
- let serialized = "Object.create(null)";
1223
- if (ctx.features & 64 /* ObjectAssign */) {
1224
- serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1225
- } else {
1226
- markRef(ctx, node.i);
1227
- const assignments = serializeAssignments(ctx, node.i, node.d);
1228
- if (assignments) {
1229
- const ref = getRefParam(ctx, node.i);
1230
- return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
1212
+ static serializeTypedArray(ctx, node) {
1213
+ let args = "[" + node.s + "]";
1214
+ if (node.l !== 0) {
1215
+ args += "," + node.l.toString();
1231
1216
  }
1217
+ return assignRef(ctx, node.i, "new " + node.c + "(" + args + ")");
1232
1218
  }
1233
- return assignRef(ctx, node.i, serialized);
1234
- }
1235
- function serializeTree(ctx, node) {
1236
- switch (node.t) {
1237
- case 0 /* Primitive */:
1238
- return String(node.s);
1239
- case 2 /* Reference */:
1240
- return getRefParam(ctx, node.i);
1241
- case 10 /* Promise */:
1242
- return serializePromise(ctx, node);
1243
- case 1 /* BigInt */:
1244
- return node.s;
1245
- case 3 /* Date */:
1246
- return assignRef(ctx, node.i, `new Date("${node.s}")`);
1247
- case 4 /* RegExp */:
1248
- return assignRef(ctx, node.i, node.s);
1249
- case 15 /* BigIntTypedArray */:
1250
- case 14 /* TypedArray */:
1251
- return serializeTypedArray(ctx, node);
1252
- case 5 /* Set */:
1253
- return serializeSet(ctx, node);
1254
- case 6 /* Map */:
1255
- return serializeMap(ctx, node);
1256
- case 7 /* Array */:
1257
- return serializeArray(ctx, node);
1258
- case 12 /* AggregateError */:
1259
- return serializeAggregateError(ctx, node);
1260
- case 11 /* Error */:
1261
- return serializeError(ctx, node);
1262
- case 13 /* Iterable */:
1263
- return serializeIterable(ctx, node);
1264
- case 9 /* NullConstructor */:
1265
- return serializeNullConstructor(ctx, node);
1266
- case 8 /* Object */:
1267
- return assignRef(ctx, node.i, serializeObject(ctx, node.i, node.d));
1268
- default:
1269
- throw new Error("Unsupported type");
1219
+ static serializeIterable(ctx, node) {
1220
+ const parent = ctx.stack;
1221
+ ctx.stack = [];
1222
+ const values = this.serializeNodeList(ctx, node);
1223
+ ctx.stack = parent;
1224
+ let serialized = values;
1225
+ if (ctx.features & 2 /* ArrayPrototypeValues */) {
1226
+ serialized += ".values()";
1227
+ } else {
1228
+ serialized += "[Symbol.iterator]()";
1229
+ }
1230
+ if (ctx.features & 4 /* ArrowFunction */) {
1231
+ serialized = "{[Symbol.iterator]:()=>" + serialized + "}";
1232
+ } else if (ctx.features & 32 /* MethodShorthand */) {
1233
+ serialized = "{[Symbol.iterator](){return " + serialized + "}}";
1234
+ } else {
1235
+ serialized = "{[Symbol.iterator]:function(){return " + serialized + "}}";
1236
+ }
1237
+ return this.serializeDictionary(ctx, node.i, node.d, serialized);
1238
+ }
1239
+ static serialize(ctx, node) {
1240
+ switch (node.t) {
1241
+ case 0 /* Primitive */:
1242
+ return String(node.s);
1243
+ case 1 /* BigInt */:
1244
+ return node.s;
1245
+ case 2 /* Reference */:
1246
+ return getRefParam(ctx, node.i);
1247
+ case 7 /* Array */:
1248
+ return this.serializeArray(ctx, node);
1249
+ case 8 /* Object */:
1250
+ return assignRef(ctx, node.i, this.serializeObject(ctx, node.i, node.d));
1251
+ case 9 /* NullConstructor */:
1252
+ return this.serializeNullConstructor(ctx, node);
1253
+ case 3 /* Date */:
1254
+ return assignRef(ctx, node.i, 'new Date("' + node.s + '")');
1255
+ case 4 /* RegExp */:
1256
+ return assignRef(ctx, node.i, node.s);
1257
+ case 5 /* Set */:
1258
+ return this.serializeSet(ctx, node);
1259
+ case 6 /* Map */:
1260
+ return this.serializeMap(ctx, node);
1261
+ case 15 /* BigIntTypedArray */:
1262
+ case 14 /* TypedArray */:
1263
+ return this.serializeTypedArray(ctx, node);
1264
+ case 12 /* AggregateError */:
1265
+ return this.serializeAggregateError(ctx, node);
1266
+ case 11 /* Error */:
1267
+ return this.serializeError(ctx, node);
1268
+ case 13 /* Iterable */:
1269
+ return this.serializeIterable(ctx, node);
1270
+ case 10 /* Promise */:
1271
+ return this.serializePromise(ctx, node);
1272
+ default:
1273
+ throw new Error("Unsupported type");
1274
+ }
1270
1275
  }
1271
- }
1276
+ };
1272
1277
 
1273
1278
  // src/tree/sync.ts
1274
1279
  var SyncParser = class {
1275
- constructor(ctx) {
1276
- __publicField(this, "ctx");
1277
- this.ctx = ctx;
1278
- }
1279
- generateNodeList(current) {
1280
+ static generateNodeList(ctx, current) {
1280
1281
  const size = current.length;
1281
1282
  const nodes = new Array(size);
1282
1283
  const deferred = new Array(size);
@@ -1287,22 +1288,32 @@ var SyncParser = class {
1287
1288
  if (isIterable(item)) {
1288
1289
  deferred[i] = item;
1289
1290
  } else {
1290
- nodes[i] = this.parse(item);
1291
+ nodes[i] = this.parse(ctx, item);
1291
1292
  }
1292
1293
  }
1293
1294
  }
1294
1295
  for (let i = 0; i < size; i++) {
1295
1296
  if (i in deferred) {
1296
- nodes[i] = this.parse(deferred[i]);
1297
+ nodes[i] = this.parse(ctx, deferred[i]);
1297
1298
  }
1298
1299
  }
1299
1300
  return nodes;
1300
1301
  }
1301
- generateArrayNode(id, current) {
1302
- return new SerovalArrayNode(id, this.generateNodeList(current));
1302
+ static generateArrayNode(ctx, id, current) {
1303
+ return {
1304
+ t: 7 /* Array */,
1305
+ i: id,
1306
+ s: void 0,
1307
+ l: void 0,
1308
+ c: void 0,
1309
+ m: void 0,
1310
+ d: void 0,
1311
+ a: this.generateNodeList(ctx, current),
1312
+ n: void 0
1313
+ };
1303
1314
  }
1304
- generateMapNode(id, current) {
1305
- assert(this.ctx.features & 16 /* Map */, 'Unsupported type "Map"');
1315
+ static generateMapNode(ctx, id, current) {
1316
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
1306
1317
  const len = current.size;
1307
1318
  const keyNodes = new Array(len);
1308
1319
  const valueNodes = new Array(len);
@@ -1316,19 +1327,29 @@ var SyncParser = class {
1316
1327
  deferredValue[deferredSize] = value;
1317
1328
  deferredSize++;
1318
1329
  } else {
1319
- keyNodes[nodeSize] = this.parse(key);
1320
- valueNodes[nodeSize] = this.parse(value);
1330
+ keyNodes[nodeSize] = this.parse(ctx, key);
1331
+ valueNodes[nodeSize] = this.parse(ctx, value);
1321
1332
  nodeSize++;
1322
1333
  }
1323
1334
  }
1324
1335
  for (let i = 0; i < deferredSize; i++) {
1325
- keyNodes[nodeSize + i] = this.parse(deferredKey[i]);
1326
- valueNodes[nodeSize + i] = this.parse(deferredValue[i]);
1336
+ keyNodes[nodeSize + i] = this.parse(ctx, deferredKey[i]);
1337
+ valueNodes[nodeSize + i] = this.parse(ctx, deferredValue[i]);
1327
1338
  }
1328
- return new SerovalMapNode(id, new SerovalMapRecordNode(keyNodes, valueNodes, len));
1339
+ return {
1340
+ t: 6 /* Map */,
1341
+ i: id,
1342
+ s: void 0,
1343
+ l: void 0,
1344
+ c: void 0,
1345
+ m: void 0,
1346
+ d: { k: keyNodes, v: valueNodes, s: len },
1347
+ a: void 0,
1348
+ n: void 0
1349
+ };
1329
1350
  }
1330
- generateSetNode(id, current) {
1331
- assert(this.ctx.features & 256 /* Set */, 'Unsupported type "Set"');
1351
+ static generateSetNode(ctx, id, current) {
1352
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
1332
1353
  const len = current.size;
1333
1354
  const nodes = new Array(len);
1334
1355
  const deferred = new Array(len);
@@ -1338,15 +1359,25 @@ var SyncParser = class {
1338
1359
  if (isIterable(item)) {
1339
1360
  deferred[deferredSize++] = item;
1340
1361
  } else {
1341
- nodes[nodeSize++] = this.parse(item);
1362
+ nodes[nodeSize++] = this.parse(ctx, item);
1342
1363
  }
1343
1364
  }
1344
1365
  for (let i = 0; i < deferredSize; i++) {
1345
- nodes[nodeSize + i] = this.parse(deferred[i]);
1366
+ nodes[nodeSize + i] = this.parse(ctx, deferred[i]);
1346
1367
  }
1347
- return new SerovalSetNode(id, nodes);
1368
+ return {
1369
+ t: 5 /* Set */,
1370
+ i: id,
1371
+ s: void 0,
1372
+ l: void 0,
1373
+ c: void 0,
1374
+ m: void 0,
1375
+ d: void 0,
1376
+ a: nodes,
1377
+ n: void 0
1378
+ };
1348
1379
  }
1349
- generateProperties(properties) {
1380
+ static generateProperties(ctx, properties) {
1350
1381
  const keys = Object.keys(properties);
1351
1382
  const size = keys.length;
1352
1383
  const keyNodes = new Array(size);
@@ -1364,13 +1395,13 @@ var SyncParser = class {
1364
1395
  deferredSize++;
1365
1396
  } else {
1366
1397
  keyNodes[nodesSize] = key;
1367
- valueNodes[nodesSize] = this.parse(item);
1398
+ valueNodes[nodesSize] = this.parse(ctx, item);
1368
1399
  nodesSize++;
1369
1400
  }
1370
1401
  }
1371
1402
  for (let i = 0; i < deferredSize; i++) {
1372
1403
  keyNodes[nodesSize + i] = deferredKeys[i];
1373
- valueNodes[nodesSize + i] = this.parse(deferredValues[i]);
1404
+ valueNodes[nodesSize + i] = this.parse(ctx, deferredValues[i]);
1374
1405
  }
1375
1406
  return {
1376
1407
  k: keyNodes,
@@ -1378,78 +1409,105 @@ var SyncParser = class {
1378
1409
  s: size
1379
1410
  };
1380
1411
  }
1381
- generateIterableNode(id, current) {
1382
- assert(this.ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
1412
+ static generateIterableNode(ctx, id, current) {
1413
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
1383
1414
  const options = getIterableOptions(current);
1384
- return new SerovalIterableNode(
1385
- id,
1415
+ return {
1416
+ t: 13 /* Iterable */,
1417
+ i: id,
1418
+ s: void 0,
1419
+ l: void 0,
1420
+ c: void 0,
1421
+ m: void 0,
1386
1422
  // Parse options first before the items
1387
- options ? this.generateProperties(options) : void 0,
1388
- this.generateNodeList(Array.from(current))
1389
- );
1423
+ d: options ? this.generateProperties(ctx, options) : void 0,
1424
+ a: this.generateNodeList(ctx, Array.from(current)),
1425
+ n: void 0
1426
+ };
1390
1427
  }
1391
- generateObjectNode(id, current, empty) {
1428
+ static generateObjectNode(ctx, id, current, empty) {
1392
1429
  if (Symbol.iterator in current) {
1393
- return this.generateIterableNode(id, current);
1430
+ return this.generateIterableNode(ctx, id, current);
1394
1431
  }
1395
- if (empty) {
1396
- return new SerovalNullConstructorNode(id, this.generateProperties(current));
1397
- }
1398
- return new SerovalObjectNode(id, this.generateProperties(current));
1432
+ return {
1433
+ t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
1434
+ i: id,
1435
+ s: void 0,
1436
+ l: void 0,
1437
+ c: void 0,
1438
+ m: void 0,
1439
+ d: this.generateProperties(ctx, current),
1440
+ a: void 0,
1441
+ n: void 0
1442
+ };
1399
1443
  }
1400
- generateAggregateErrorNode(id, current) {
1444
+ static generateAggregateErrorNode(ctx, id, current) {
1401
1445
  const options = getErrorOptions(current);
1402
- const optionsNode = options ? this.generateProperties(options) : void 0;
1403
- return new SerovalAggregateErrorNode(
1404
- id,
1405
- current.message,
1406
- optionsNode,
1407
- this.parse(current.errors)
1408
- );
1409
- }
1410
- generateErrorNode(id, current) {
1446
+ const optionsNode = options ? this.generateProperties(ctx, options) : void 0;
1447
+ return {
1448
+ t: 12 /* AggregateError */,
1449
+ i: id,
1450
+ s: void 0,
1451
+ l: void 0,
1452
+ c: void 0,
1453
+ m: current.message,
1454
+ d: optionsNode,
1455
+ a: void 0,
1456
+ n: this.parse(ctx, current.errors)
1457
+ };
1458
+ }
1459
+ static generateErrorNode(ctx, id, current) {
1411
1460
  const options = getErrorOptions(current);
1412
- const optionsNode = options ? this.generateProperties(options) : void 0;
1413
- return new SerovalErrorNode(id, getErrorConstructor(current), current.message, optionsNode);
1461
+ const optionsNode = options ? this.generateProperties(ctx, options) : void 0;
1462
+ return {
1463
+ t: 11 /* Error */,
1464
+ i: id,
1465
+ s: void 0,
1466
+ l: void 0,
1467
+ c: getErrorConstructor(current),
1468
+ m: current.message,
1469
+ d: optionsNode,
1470
+ a: void 0,
1471
+ n: void 0
1472
+ };
1414
1473
  }
1415
- parse(current) {
1474
+ static parse(ctx, current) {
1416
1475
  switch (typeof current) {
1417
1476
  case "boolean":
1418
1477
  return current ? TRUE_NODE : FALSE_NODE;
1419
1478
  case "undefined":
1420
1479
  return UNDEFINED_NODE;
1421
1480
  case "string":
1422
- return new SerovalPrimitiveNode(quote(current));
1481
+ return createPrimitiveNode(quote(current));
1423
1482
  case "number":
1424
1483
  if (Object.is(current, -0)) {
1425
1484
  return NEG_ZERO_NODE;
1426
1485
  }
1427
1486
  if (Object.is(current, Infinity)) {
1428
- return INFINITY_NODE;
1487
+ return createPrimitiveNode("1/0");
1429
1488
  }
1430
1489
  if (Object.is(current, -Infinity)) {
1431
- return NEG_INFINITY_NODE;
1490
+ return createPrimitiveNode("-1/0");
1432
1491
  }
1433
- return new SerovalPrimitiveNode(current);
1492
+ return createPrimitiveNode(current);
1434
1493
  case "bigint":
1435
- assert(this.ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
1436
- return new SerovalBigIntNode(current);
1494
+ return createBigIntNode(ctx, current);
1437
1495
  case "object": {
1438
1496
  if (!current) {
1439
1497
  return NULL_NODE;
1440
1498
  }
1441
- const id = createRef(this.ctx, current, true);
1442
- if (this.ctx.markedRefs[id]) {
1443
- return new SerovalReferenceNode(id);
1499
+ const id = createRef(ctx, current, true);
1500
+ if (ctx.markedRefs.has(id)) {
1501
+ return createReferenceNode(id);
1444
1502
  }
1445
1503
  if (Array.isArray(current)) {
1446
- return this.generateArrayNode(id, current);
1504
+ return this.generateArrayNode(ctx, id, current);
1447
1505
  }
1448
1506
  switch (current.constructor) {
1449
1507
  case Date:
1450
- return new SerovalDateNode(id, current);
1508
+ return createDateNode(id, current);
1451
1509
  case RegExp:
1452
- return new SerovalRegExpNode(id, current);
1510
+ return createRegExpNode(id, current);
1453
1511
  case Int8Array:
1454
1512
  case Int16Array:
1455
1513
  case Int32Array:
@@ -1459,28 +1517,23 @@ var SyncParser = class {
1459
1517
  case Uint8ClampedArray:
1460
1518
  case Float32Array:
1461
1519
  case Float64Array:
1462
- assert(this.ctx.features & 1024 /* TypedArray */, `Unsupported value type "${current.constructor.name}"`);
1463
- return new SerovalTypedArrayNode(id, current);
1520
+ return createTypedArrayNode(ctx, id, current);
1464
1521
  case BigInt64Array:
1465
1522
  case BigUint64Array:
1466
- assert(
1467
- this.ctx.features & 2048 /* BigIntTypedArray */,
1468
- `Unsupported value type "${current.constructor.name}"`
1469
- );
1470
- return new SerovalBigIntTypedArrayNode(id, current);
1523
+ return createBigIntTypedArrayNode(ctx, id, current);
1471
1524
  case Map:
1472
- return this.generateMapNode(id, current);
1525
+ return this.generateMapNode(ctx, id, current);
1473
1526
  case Set:
1474
- return this.generateSetNode(id, current);
1527
+ return this.generateSetNode(ctx, id, current);
1475
1528
  case Object:
1476
- return this.generateObjectNode(id, current, false);
1529
+ return this.generateObjectNode(ctx, id, current, false);
1477
1530
  case void 0:
1478
- return this.generateObjectNode(id, current, true);
1531
+ return this.generateObjectNode(ctx, id, current, true);
1479
1532
  case AggregateError:
1480
- if (this.ctx.features & 1 /* AggregateError */) {
1481
- return this.generateAggregateErrorNode(id, current);
1533
+ if (ctx.features & 1 /* AggregateError */) {
1534
+ return this.generateAggregateErrorNode(ctx, id, current);
1482
1535
  }
1483
- return this.generateErrorNode(id, current);
1536
+ return this.generateErrorNode(ctx, id, current);
1484
1537
  case Error:
1485
1538
  case EvalError:
1486
1539
  case RangeError:
@@ -1488,18 +1541,18 @@ var SyncParser = class {
1488
1541
  case SyntaxError:
1489
1542
  case TypeError:
1490
1543
  case URIError:
1491
- return this.generateErrorNode(id, current);
1544
+ return this.generateErrorNode(ctx, id, current);
1492
1545
  default:
1493
1546
  break;
1494
1547
  }
1495
- if (current instanceof AggregateError && this.ctx.features & 1 /* AggregateError */) {
1496
- return this.generateAggregateErrorNode(id, current);
1548
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
1549
+ return this.generateAggregateErrorNode(ctx, id, current);
1497
1550
  }
1498
1551
  if (current instanceof Error) {
1499
- return this.generateErrorNode(id, current);
1552
+ return this.generateErrorNode(ctx, id, current);
1500
1553
  }
1501
1554
  if (Symbol.iterator in current) {
1502
- return this.generateIterableNode(id, current);
1555
+ return this.generateIterableNode(ctx, id, current);
1503
1556
  }
1504
1557
  throw new Error("Unsupported type");
1505
1558
  }
@@ -1509,7 +1562,7 @@ var SyncParser = class {
1509
1562
  }
1510
1563
  };
1511
1564
  function parseSync(ctx, current) {
1512
- const result = new SyncParser(ctx).parse(current);
1565
+ const result = SyncParser.parse(ctx, current);
1513
1566
  const isObject = result.t === 8 /* Object */ || result.t === 13 /* Iterable */;
1514
1567
  return [result, createRef(ctx, current, false), isObject];
1515
1568
  }
@@ -1521,71 +1574,70 @@ function finalize(ctx, rootID, isObject, result) {
1521
1574
  let body = result;
1522
1575
  if (patches) {
1523
1576
  const index = getRefParam(ctx, rootID);
1524
- if (result.startsWith(`${index}=`)) {
1525
- body = `${result},${patches}${index}`;
1526
- } else {
1527
- body = `${index}=${result},${patches}${index}`;
1577
+ body = result + "," + patches + index;
1578
+ if (!result.startsWith(index + "=")) {
1579
+ body = index + "=" + body;
1528
1580
  }
1529
1581
  }
1530
1582
  let params = ctx.vars.length > 1 ? ctx.vars.join(",") : ctx.vars[0];
1531
1583
  if (ctx.features & 4 /* ArrowFunction */) {
1532
- params = ctx.vars.length > 1 || ctx.vars.length === 0 ? `(${params})` : params;
1533
- return `(${params}=>(${body}))()`;
1584
+ params = ctx.vars.length > 1 || ctx.vars.length === 0 ? "(" + params + ")" : params;
1585
+ return "(" + params + "=>(" + body + "))()";
1534
1586
  }
1535
- return `(function(${params}){return ${body}})()`;
1587
+ return "(function(" + params + "){return " + body + "})()";
1536
1588
  }
1537
1589
  if (isObject) {
1538
- return `(${result})`;
1590
+ return "(" + result + ")";
1539
1591
  }
1540
1592
  return result;
1541
1593
  }
1542
1594
  function serialize(source, options) {
1543
- const ctx = new ParserContext(options);
1595
+ const ctx = createParserContext(options);
1544
1596
  const [tree, rootID, isObject] = parseSync(ctx, source);
1545
- const serial = new SerializationContext(ctx);
1546
- const result = serializeTree(serial, tree);
1597
+ const serial = createSerializationContext(ctx);
1598
+ const result = SerovalSerializer.serialize(serial, tree);
1547
1599
  return finalize(serial, rootID, isObject, result);
1548
1600
  }
1549
1601
  async function serializeAsync(source, options) {
1550
- const ctx = new ParserContext(options);
1602
+ const ctx = createParserContext(options);
1551
1603
  const [tree, rootID, isObject] = await parseAsync(ctx, source);
1552
- const serial = new SerializationContext(ctx);
1553
- const result = serializeTree(serial, tree);
1604
+ const serial = createSerializationContext(ctx);
1605
+ const result = SerovalSerializer.serialize(serial, tree);
1554
1606
  return finalize(serial, rootID, isObject, result);
1555
1607
  }
1556
1608
  function deserialize(source) {
1557
1609
  return (0, eval)(source);
1558
1610
  }
1559
1611
  function toJSON(source, options) {
1560
- const ctx = new ParserContext(options);
1612
+ const ctx = createParserContext(options);
1561
1613
  const [tree, root, isObject] = parseSync(ctx, source);
1562
- return JSON.stringify([
1563
- tree,
1564
- root,
1565
- isObject,
1566
- ctx.features,
1567
- ctx.markedRefs
1568
- ]);
1614
+ return JSON.stringify({
1615
+ t: tree,
1616
+ r: root,
1617
+ i: isObject,
1618
+ f: ctx.features,
1619
+ m: Array.from(ctx.markedRefs)
1620
+ });
1569
1621
  }
1570
1622
  async function toJSONAsync(source, options) {
1571
- const ctx = new ParserContext(options);
1623
+ const ctx = createParserContext(options);
1572
1624
  const [tree, root, isObject] = await parseAsync(ctx, source);
1573
- return JSON.stringify([
1574
- tree,
1575
- root,
1576
- isObject,
1577
- ctx.features,
1578
- ctx.markedRefs
1579
- ]);
1625
+ return JSON.stringify({
1626
+ t: tree,
1627
+ r: root,
1628
+ i: isObject,
1629
+ f: ctx.features,
1630
+ m: Array.from(ctx.markedRefs)
1631
+ });
1580
1632
  }
1581
1633
  function compileJSON(source) {
1582
1634
  const parsed = JSON.parse(source);
1583
- const serial = new SerializationContext({
1584
- features: parsed[3],
1585
- markedRefs: parsed[4]
1635
+ const serial = createSerializationContext({
1636
+ features: parsed.f,
1637
+ markedRefs: parsed.m
1586
1638
  });
1587
- const result = serializeTree(serial, parsed[0]);
1588
- return finalize(serial, parsed[1], parsed[2], result);
1639
+ const result = SerovalSerializer.serialize(serial, parsed.t);
1640
+ return finalize(serial, parsed.r, parsed.i, result);
1589
1641
  }
1590
1642
  function fromJSON(source) {
1591
1643
  return deserialize(compileJSON(source));