seroval 0.4.0-alpha.1 → 0.4.0-alpha.10

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,28 +37,22 @@ 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";
40
44
  }
41
- function constructorCheck(value, constructor) {
42
- return value.constructor === constructor;
43
- }
44
45
  function isIterableBuiltin(obj) {
45
- return Array.isArray(obj) || constructorCheck(obj, Set) || constructorCheck(obj, Map);
46
+ if (!!obj && typeof obj === "object") {
47
+ const cons = obj.constructor;
48
+ return Array.isArray(obj) || cons === Set || cons === Map;
49
+ }
50
+ return false;
46
51
  }
47
52
  function isIterable(obj) {
48
53
  return !!obj && typeof obj === "object" && !isIterableBuiltin(obj) && Symbol.iterator in obj && typeof obj[Symbol.iterator] === "function";
49
54
  }
50
55
 
51
- // src/assert.ts
52
- function assert(cond, error) {
53
- if (!cond) {
54
- throw new Error(error);
55
- }
56
- }
57
-
58
56
  // src/compat.ts
59
57
  function compareVersion(left, right) {
60
58
  let diff = left[0] - right[0];
@@ -68,7 +66,7 @@ function compareVersion(left, right) {
68
66
  }
69
67
  var VERSION_TABLE = {
70
68
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError#browser_compatibility
71
- [0 /* AggregateError */]: {
69
+ [1 /* AggregateError */]: {
72
70
  es: [2021, 0, 0],
73
71
  chrome: [85, 0, 0],
74
72
  edge: [85, 0, 0],
@@ -81,7 +79,7 @@ var VERSION_TABLE = {
81
79
  node: [15, 0, 0]
82
80
  },
83
81
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values#browser_compatibility
84
- [1 /* ArrayPrototypeValues */]: {
82
+ [2 /* ArrayPrototypeValues */]: {
85
83
  es: [6, 0, 0],
86
84
  chrome: [66, 0, 0],
87
85
  edge: [14, 0, 0],
@@ -94,7 +92,7 @@ var VERSION_TABLE = {
94
92
  node: [10, 9, 0]
95
93
  },
96
94
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#browser_compatibility
97
- [2 /* ArrowFunction */]: {
95
+ [4 /* ArrowFunction */]: {
98
96
  es: [6, 0, 0],
99
97
  chrome: [45, 0, 0],
100
98
  edge: [12, 0, 0],
@@ -107,7 +105,7 @@ var VERSION_TABLE = {
107
105
  node: [4, 0, 0]
108
106
  },
109
107
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility
110
- [3 /* BigInt */]: {
108
+ [8 /* BigInt */]: {
111
109
  es: [2020, 0, 0],
112
110
  chrome: [67, 0, 0],
113
111
  edge: [79, 0, 0],
@@ -120,7 +118,7 @@ var VERSION_TABLE = {
120
118
  node: [10, 4, 0]
121
119
  },
122
120
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#browser_compatibility
123
- [4 /* Map */]: {
121
+ [16 /* Map */]: {
124
122
  es: [6, 0, 0],
125
123
  chrome: [38, 0, 0],
126
124
  edge: [12, 0, 0],
@@ -133,7 +131,7 @@ var VERSION_TABLE = {
133
131
  node: [0, 12, 0]
134
132
  },
135
133
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#browser_compatibility
136
- [5 /* MethodShorthand */]: {
134
+ [32 /* MethodShorthand */]: {
137
135
  es: [6, 0, 0],
138
136
  chrome: [39, 0, 0],
139
137
  edge: [12, 0, 0],
@@ -146,7 +144,7 @@ var VERSION_TABLE = {
146
144
  node: [4, 0, 0]
147
145
  },
148
146
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#browser_compatibility
149
- [6 /* ObjectAssign */]: {
147
+ [64 /* ObjectAssign */]: {
150
148
  es: [6, 0, 0],
151
149
  chrome: [45, 0, 0],
152
150
  edge: [12, 0, 0],
@@ -159,7 +157,7 @@ var VERSION_TABLE = {
159
157
  node: [4, 0, 0]
160
158
  },
161
159
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#browser_compatibility
162
- [7 /* Promise */]: {
160
+ [128 /* Promise */]: {
163
161
  es: [6, 0, 0],
164
162
  chrome: [32, 0, 0],
165
163
  edge: [12, 0, 0],
@@ -172,7 +170,7 @@ var VERSION_TABLE = {
172
170
  node: [0, 12, 0]
173
171
  },
174
172
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#browser_compatibility
175
- [8 /* Set */]: {
173
+ [256 /* Set */]: {
176
174
  es: [6, 0, 0],
177
175
  chrome: [38, 0, 0],
178
176
  edge: [12, 0, 0],
@@ -185,7 +183,7 @@ var VERSION_TABLE = {
185
183
  node: [0, 12, 0]
186
184
  },
187
185
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator#browser_compatibility
188
- [9 /* SymbolIterator */]: {
186
+ [512 /* SymbolIterator */]: {
189
187
  es: [6, 0, 0],
190
188
  chrome: [43, 0, 0],
191
189
  edge: [12, 0, 0],
@@ -198,7 +196,7 @@ var VERSION_TABLE = {
198
196
  node: [0, 12, 0]
199
197
  },
200
198
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#browser_compatibility
201
- [10 /* TypedArray */]: {
199
+ [1024 /* TypedArray */]: {
202
200
  es: [6, 0, 0],
203
201
  chrome: [7, 0, 0],
204
202
  edge: [12, 0, 0],
@@ -209,21 +207,53 @@ var VERSION_TABLE = {
209
207
  samsung: [1, 0, 0],
210
208
  deno: [1, 0, 0],
211
209
  node: [0, 10, 0]
210
+ },
211
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array#browser_compatibility
212
+ [2048 /* BigIntTypedArray */]: {
213
+ es: [2020, 0, 0],
214
+ chrome: [67, 0, 0],
215
+ edge: [79, 0, 0],
216
+ firefox: [68, 0, 0],
217
+ opera: [54, 6, 0],
218
+ safari: [15, 1, 0],
219
+ ios: [15, 2, 0],
220
+ samsung: [9, 0, 0],
221
+ deno: [1, 0, 0],
222
+ node: [10, 4, 0]
212
223
  }
213
224
  };
214
- var TARGET_REGEX = /^(es|chrome|edge|safari|firefox|opera|ios|samsung|deno|node)([0-9]+(\.[0-9]+(\.[0-9]+)?)?)$/i;
225
+ var platforms = [
226
+ "chrome",
227
+ "deno",
228
+ "edge",
229
+ "es",
230
+ "firefox",
231
+ "ios",
232
+ "node",
233
+ "opera",
234
+ "safari",
235
+ "samsung"
236
+ ];
237
+ function getPlatform(target) {
238
+ for (let i = 0, len = platforms.length; i < len; i++) {
239
+ const platform = target.substring(0, platforms[i].length);
240
+ if (platform === platforms[i]) {
241
+ return platform;
242
+ }
243
+ }
244
+ throw new Error(`Invalid target "${target}"`);
245
+ }
215
246
  function parseTarget(target) {
216
- const result = TARGET_REGEX.exec(target);
217
- assert(result, `Invalid target "${target}"`);
218
- const [, platform, version] = result;
247
+ const platform = getPlatform(target);
248
+ const version = target.substring(platform.length);
219
249
  const [major, minor = "0", patch = "0"] = version.split(".");
220
250
  return [platform, [Number(major), Number(minor), Number(patch)]];
221
251
  }
222
252
  function getTargetVersions(targets) {
223
253
  if (Array.isArray(targets)) {
224
254
  const versions = [];
225
- for (const target of targets) {
226
- versions.push(parseTarget(target));
255
+ for (let i = 0, len = targets.length; i < len; i++) {
256
+ versions.push(parseTarget(targets[i]));
227
257
  }
228
258
  return versions;
229
259
  }
@@ -231,8 +261,8 @@ function getTargetVersions(targets) {
231
261
  }
232
262
  function parseTargets(targets) {
233
263
  const parsed = getTargetVersions(targets);
234
- const flags = /* @__PURE__ */ new Set();
235
- for (const key in VERSION_TABLE) {
264
+ let flags = 0;
265
+ for (const key of Object.keys(VERSION_TABLE)) {
236
266
  const base = VERSION_TABLE[key];
237
267
  let flag = true;
238
268
  for (const [platform, version] of parsed) {
@@ -240,12 +270,93 @@ function parseTargets(targets) {
240
270
  flag = flag && !!baseVersion && compareVersion(baseVersion, version) <= 0;
241
271
  }
242
272
  if (flag) {
243
- flags.add(Number(key));
273
+ flags |= Number(key);
244
274
  }
245
275
  }
246
276
  return flags;
247
277
  }
248
278
 
279
+ // src/get-identifier.ts
280
+ var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
281
+ var REF_START_CHARS_LEN = REF_START_CHARS.length;
282
+ var REF_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_";
283
+ var REF_CHARS_LEN = REF_CHARS.length;
284
+ function getIdentifier(index) {
285
+ let mod = index % REF_START_CHARS_LEN;
286
+ let ref = REF_START_CHARS[mod];
287
+ index = (index - mod) / REF_START_CHARS_LEN;
288
+ while (index > 0) {
289
+ mod = index % REF_CHARS_LEN;
290
+ ref += REF_CHARS[mod];
291
+ index = (index - mod) / REF_CHARS_LEN;
292
+ }
293
+ return ref;
294
+ }
295
+
296
+ // src/context.ts
297
+ var DEFAULT_OPTIONS = {
298
+ target: "es2023"
299
+ };
300
+ function createParserContext(options = {}) {
301
+ const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
302
+ return {
303
+ markedRefs: {},
304
+ refs: /* @__PURE__ */ new Map(),
305
+ features: parseTargets(result.target)
306
+ };
307
+ }
308
+ function createSerializationContext(options) {
309
+ return {
310
+ stack: [],
311
+ vars: [],
312
+ assignments: [],
313
+ validRefs: [],
314
+ refSize: 0,
315
+ features: options.features,
316
+ markedRefs: options.markedRefs
317
+ };
318
+ }
319
+ function markRef(ctx, current) {
320
+ ctx.markedRefs[current] = 1;
321
+ }
322
+ function createValidRef(ctx, index) {
323
+ const current = ctx.validRefs[index];
324
+ if (current == null) {
325
+ const value = ctx.refSize++;
326
+ ctx.validRefs[index] = value;
327
+ return value;
328
+ }
329
+ return current;
330
+ }
331
+ function getRefParam(ctx, index) {
332
+ const actualIndex = createValidRef(ctx, index);
333
+ if (ctx.vars[actualIndex]) {
334
+ return ctx.vars[actualIndex];
335
+ }
336
+ const result = getIdentifier(actualIndex);
337
+ ctx.vars[actualIndex] = result;
338
+ return result;
339
+ }
340
+ function createRef(ctx, current, mark) {
341
+ const ref = ctx.refs.get(current);
342
+ if (ref != null) {
343
+ if (mark) {
344
+ markRef(ctx, ref);
345
+ }
346
+ return ref;
347
+ }
348
+ const id = ctx.refs.size;
349
+ ctx.refs.set(current, id);
350
+ return id;
351
+ }
352
+
353
+ // src/assert.ts
354
+ function assert(cond, error) {
355
+ if (!cond) {
356
+ throw new Error(error);
357
+ }
358
+ }
359
+
249
360
  // src/quote.ts
250
361
  function quote(str) {
251
362
  let result = "";
@@ -288,39 +399,448 @@ function quote(str) {
288
399
  return `"${result}"`;
289
400
  }
290
401
 
291
- // src/get-identifier.ts
292
- var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
293
- var REF_START_CHARS_LEN = REF_START_CHARS.length;
294
- var REF_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_";
295
- var REF_CHARS_LEN = REF_CHARS.length;
296
- function getIdentifier(index) {
297
- let mod = index % REF_START_CHARS_LEN;
298
- let ref = REF_START_CHARS[mod];
299
- index = (index - mod) / REF_START_CHARS_LEN;
300
- while (index > 0) {
301
- mod = index % REF_CHARS_LEN;
302
- ref += REF_CHARS[mod];
303
- index = (index - mod) / REF_CHARS_LEN;
402
+ // src/tree/shared.ts
403
+ function getErrorConstructor(error) {
404
+ if (error instanceof EvalError) {
405
+ return "EvalError";
304
406
  }
305
- return ref;
407
+ if (error instanceof RangeError) {
408
+ return "RangeError";
409
+ }
410
+ if (error instanceof ReferenceError) {
411
+ return "ReferenceError";
412
+ }
413
+ if (error instanceof SyntaxError) {
414
+ return "SyntaxError";
415
+ }
416
+ if (error instanceof TypeError) {
417
+ return "TypeError";
418
+ }
419
+ if (error instanceof URIError) {
420
+ return "URIError";
421
+ }
422
+ return "Error";
423
+ }
424
+ function getErrorOptions(error) {
425
+ let options;
426
+ const constructor = getErrorConstructor(error);
427
+ if (error.name !== constructor) {
428
+ options = { name: error.name };
429
+ } else if (error.constructor.name !== constructor) {
430
+ options = { name: error.constructor.name };
431
+ }
432
+ const names = Object.getOwnPropertyNames(error);
433
+ for (const name of names) {
434
+ if (name !== "name" && name !== "message") {
435
+ options = options || {};
436
+ options[name] = error[name];
437
+ }
438
+ }
439
+ return options;
440
+ }
441
+ function getIterableOptions(obj) {
442
+ const names = Object.getOwnPropertyNames(obj);
443
+ if (names.length) {
444
+ const options = {};
445
+ for (const name of names) {
446
+ options[name] = obj[name];
447
+ }
448
+ return options;
449
+ }
450
+ return void 0;
451
+ }
452
+ function isReferenceInStack(ctx, node) {
453
+ return node.t === 2 /* Reference */ && ctx.stack.includes(node.i);
454
+ }
455
+ function generateSemiPrimitiveValue(ctx, current, id) {
456
+ const cs = current.constructor;
457
+ switch (cs) {
458
+ case Date:
459
+ return {
460
+ t: 3 /* Date */,
461
+ i: id,
462
+ a: void 0,
463
+ s: current.toISOString(),
464
+ l: void 0,
465
+ m: void 0,
466
+ c: void 0,
467
+ d: void 0,
468
+ n: void 0
469
+ };
470
+ case RegExp:
471
+ return {
472
+ t: 4 /* RegExp */,
473
+ i: id,
474
+ a: void 0,
475
+ s: String(current),
476
+ l: void 0,
477
+ m: void 0,
478
+ c: void 0,
479
+ d: void 0,
480
+ n: void 0
481
+ };
482
+ case Int8Array:
483
+ case Int16Array:
484
+ case Int32Array:
485
+ case Uint8Array:
486
+ case Uint16Array:
487
+ case Uint32Array:
488
+ case Uint8ClampedArray:
489
+ case Float32Array:
490
+ case Float64Array: {
491
+ const constructor = cs.name;
492
+ assert(ctx.features & 1024 /* TypedArray */, `Unsupported value type "${constructor}"`);
493
+ return {
494
+ t: 14 /* TypedArray */,
495
+ i: id,
496
+ a: void 0,
497
+ s: current.toString(),
498
+ l: current.byteOffset,
499
+ m: void 0,
500
+ c: constructor,
501
+ d: void 0,
502
+ n: void 0
503
+ };
504
+ }
505
+ case BigInt64Array:
506
+ case BigUint64Array: {
507
+ const constructor = cs.name;
508
+ assert(
509
+ ctx.features & 2048 /* BigIntTypedArray */,
510
+ `Unsupported value type "${constructor}"`
511
+ );
512
+ let result = "";
513
+ const cap = current.length - 1;
514
+ for (let i = 0; i < cap; i++) {
515
+ result += `${current[i]}n,`;
516
+ }
517
+ result += `"${current[cap]}"`;
518
+ return {
519
+ t: 14 /* TypedArray */,
520
+ i: id,
521
+ a: void 0,
522
+ s: result,
523
+ l: current.byteOffset,
524
+ m: void 0,
525
+ c: constructor,
526
+ d: void 0,
527
+ n: void 0
528
+ };
529
+ }
530
+ default:
531
+ return void 0;
532
+ }
533
+ }
534
+ function serializePrimitive(value) {
535
+ if (value === true) {
536
+ return "!0";
537
+ }
538
+ if (value === false) {
539
+ return "!1";
540
+ }
541
+ if (value === void 0) {
542
+ return "void 0";
543
+ }
544
+ if (value === null) {
545
+ return null;
546
+ }
547
+ if (typeof value === "string") {
548
+ return quote(value);
549
+ }
550
+ if (Object.is(value, -0)) {
551
+ return "-0";
552
+ }
553
+ if (Object.is(value, Infinity)) {
554
+ return "1/0";
555
+ }
556
+ if (Object.is(value, -Infinity)) {
557
+ return "-1/0";
558
+ }
559
+ return value;
306
560
  }
307
561
 
308
- // src/tree.ts
309
- var DEFAULT_OPTIONS = {
310
- target: "es2023"
311
- };
312
- function createSerializationContext(options = {}) {
313
- const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
562
+ // src/tree/async.ts
563
+ async function serializePropertiesAsync(ctx, properties) {
564
+ const keys = Object.keys(properties);
565
+ const size = keys.length;
566
+ const keyNodes = new Array(size);
567
+ const valueNodes = new Array(size);
568
+ const deferredKeys = new Array(size);
569
+ const deferredValues = new Array(size);
570
+ let deferredSize = 0;
571
+ let nodesSize = 0;
572
+ for (const key of keys) {
573
+ if (isIterable(properties[key])) {
574
+ deferredKeys[deferredSize] = key;
575
+ deferredValues[deferredSize] = properties[key];
576
+ deferredSize++;
577
+ } else {
578
+ keyNodes[nodesSize] = key;
579
+ valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
580
+ nodesSize++;
581
+ }
582
+ }
583
+ for (let i = 0; i < deferredSize; i++) {
584
+ keyNodes[nodesSize + i] = deferredKeys[i];
585
+ valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
586
+ }
314
587
  return {
315
- markedRefs: [],
316
- stack: [],
317
- refs: /* @__PURE__ */ new Map(),
318
- vars: [],
319
- assignments: [],
320
- validRefs: /* @__PURE__ */ new Map(),
321
- features: parseTargets(result.target)
588
+ k: keyNodes,
589
+ v: valueNodes,
590
+ s: size
591
+ };
592
+ }
593
+ async function generateSetNode(ctx, current, id) {
594
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
595
+ const len = current.size;
596
+ const nodes = new Array(len);
597
+ const deferred = new Array(len);
598
+ let deferredSize = 0;
599
+ let nodeSize = 0;
600
+ for (const item of current.keys()) {
601
+ if (isIterable(item)) {
602
+ deferred[deferredSize++] = item;
603
+ } else {
604
+ nodes[nodeSize++] = await generateTreeAsync(ctx, item);
605
+ }
606
+ }
607
+ for (let i = 0; i < deferredSize; i++) {
608
+ nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
609
+ }
610
+ return {
611
+ t: 5 /* Set */,
612
+ i: id,
613
+ a: nodes,
614
+ s: void 0,
615
+ l: void 0,
616
+ m: void 0,
617
+ c: void 0,
618
+ d: void 0,
619
+ n: void 0
620
+ };
621
+ }
622
+ async function generateMapNode(ctx, current, id) {
623
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
624
+ const len = current.size;
625
+ const keyNodes = new Array(len);
626
+ const valueNodes = new Array(len);
627
+ const deferredKey = new Array(len);
628
+ const deferredValue = new Array(len);
629
+ let deferredSize = 0;
630
+ let nodeSize = 0;
631
+ for (const [key, value] of current.entries()) {
632
+ if (isIterable(key) || isIterable(value)) {
633
+ deferredKey[deferredSize] = key;
634
+ deferredValue[deferredSize] = value;
635
+ deferredSize++;
636
+ } else {
637
+ keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
638
+ valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
639
+ nodeSize++;
640
+ }
641
+ }
642
+ for (let i = 0; i < deferredSize; i++) {
643
+ keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
644
+ valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
645
+ }
646
+ return {
647
+ t: 6 /* Map */,
648
+ i: id,
649
+ a: void 0,
650
+ s: void 0,
651
+ l: void 0,
652
+ m: void 0,
653
+ c: void 0,
654
+ d: { k: keyNodes, v: valueNodes, s: len },
655
+ n: void 0
656
+ };
657
+ }
658
+ async function generateNodeList(ctx, current) {
659
+ const size = current.length;
660
+ const nodes = new Array(size);
661
+ const deferred = new Array(size);
662
+ for (let i = 0; i < size; i++) {
663
+ if (i in current) {
664
+ if (isIterable(current[i])) {
665
+ deferred[i] = current[i];
666
+ } else {
667
+ nodes[i] = await generateTreeAsync(ctx, current[i]);
668
+ }
669
+ }
670
+ }
671
+ for (let i = 0; i < size; i++) {
672
+ if (i in deferred) {
673
+ nodes[i] = await generateTreeAsync(ctx, deferred[i]);
674
+ }
675
+ }
676
+ return nodes;
677
+ }
678
+ async function generateArrayNode(ctx, current, id) {
679
+ return {
680
+ t: 7 /* Array */,
681
+ i: id,
682
+ a: await generateNodeList(ctx, current),
683
+ s: void 0,
684
+ l: void 0,
685
+ m: void 0,
686
+ c: void 0,
687
+ d: void 0,
688
+ n: void 0
689
+ };
690
+ }
691
+ async function generateAggregateErrorNode(ctx, current, id) {
692
+ const options = getErrorOptions(current);
693
+ const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
694
+ return {
695
+ t: 12 /* AggregateError */,
696
+ i: id,
697
+ a: void 0,
698
+ s: void 0,
699
+ l: void 0,
700
+ m: current.message,
701
+ c: void 0,
702
+ d: optionsNode,
703
+ n: await generateTreeAsync(ctx, current.errors)
704
+ };
705
+ }
706
+ async function generateErrorNode(ctx, current, id) {
707
+ const options = getErrorOptions(current);
708
+ const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
709
+ return {
710
+ t: 11 /* Error */,
711
+ i: id,
712
+ a: void 0,
713
+ s: void 0,
714
+ l: void 0,
715
+ m: current.message,
716
+ c: getErrorConstructor(current),
717
+ d: optionsNode,
718
+ n: void 0
719
+ };
720
+ }
721
+ async function generateIterableNode(ctx, current, id) {
722
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
723
+ const options = getIterableOptions(current);
724
+ return {
725
+ t: 13 /* Iterable */,
726
+ i: id,
727
+ s: void 0,
728
+ l: void 0,
729
+ m: void 0,
730
+ c: void 0,
731
+ // Parse options first before the items
732
+ d: options ? await serializePropertiesAsync(ctx, options) : void 0,
733
+ a: await generateNodeList(ctx, Array.from(current)),
734
+ n: void 0
322
735
  };
323
736
  }
737
+ async function generateTreeAsync(ctx, current) {
738
+ if (isPrimitive(current)) {
739
+ return {
740
+ t: 0 /* Primitive */,
741
+ i: void 0,
742
+ s: serializePrimitive(current),
743
+ l: void 0,
744
+ m: void 0,
745
+ c: void 0,
746
+ // Parse options first before the items
747
+ d: void 0,
748
+ a: void 0,
749
+ n: void 0
750
+ };
751
+ }
752
+ if (typeof current === "bigint") {
753
+ assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
754
+ return {
755
+ t: 1 /* BigInt */,
756
+ i: void 0,
757
+ a: void 0,
758
+ s: `${current}n`,
759
+ l: void 0,
760
+ m: void 0,
761
+ c: void 0,
762
+ d: void 0,
763
+ n: void 0
764
+ };
765
+ }
766
+ const id = createRef(ctx, current, true);
767
+ if (ctx.markedRefs[id]) {
768
+ return {
769
+ t: 2 /* Reference */,
770
+ i: id,
771
+ a: void 0,
772
+ s: void 0,
773
+ l: void 0,
774
+ m: void 0,
775
+ c: void 0,
776
+ d: void 0,
777
+ n: void 0
778
+ };
779
+ }
780
+ if (Array.isArray(current)) {
781
+ return generateArrayNode(ctx, current, id);
782
+ }
783
+ const cs = current.constructor;
784
+ const empty = cs == null;
785
+ if (cs === Object || empty) {
786
+ if (Symbol.iterator in current) {
787
+ return generateIterableNode(ctx, current, id);
788
+ }
789
+ return {
790
+ t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
791
+ i: id,
792
+ s: void 0,
793
+ l: void 0,
794
+ m: void 0,
795
+ c: void 0,
796
+ // Parse options first before the items
797
+ d: await serializePropertiesAsync(ctx, current),
798
+ a: void 0,
799
+ n: void 0
800
+ };
801
+ }
802
+ if (cs === Set) {
803
+ return generateSetNode(ctx, current, id);
804
+ }
805
+ if (cs === Map) {
806
+ return generateMapNode(ctx, current, id);
807
+ }
808
+ const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
809
+ if (semiPrimitive) {
810
+ return semiPrimitive;
811
+ }
812
+ if (Symbol.iterator in current) {
813
+ return generateIterableNode(ctx, current, id);
814
+ }
815
+ if (isPromise(current)) {
816
+ assert(ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
817
+ return current.then(async (value) => ({
818
+ t: 10 /* Promise */,
819
+ i: id,
820
+ s: void 0,
821
+ l: void 0,
822
+ m: void 0,
823
+ c: void 0,
824
+ // Parse options first before the items
825
+ d: void 0,
826
+ a: void 0,
827
+ n: await generateTreeAsync(ctx, value)
828
+ }));
829
+ }
830
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
831
+ return generateAggregateErrorNode(ctx, current, id);
832
+ }
833
+ if (current instanceof Error) {
834
+ return generateErrorNode(ctx, current, id);
835
+ }
836
+ throw new Error("Unsupported value");
837
+ }
838
+ async function parseAsync(ctx, current) {
839
+ const result = await generateTreeAsync(ctx, current);
840
+ return [result, createRef(ctx, current, false), result.t === 8 /* Object */];
841
+ }
842
+
843
+ // src/tree/serialize.ts
324
844
  function getAssignmentExpression(assignment) {
325
845
  switch (assignment.type) {
326
846
  case "index":
@@ -386,41 +906,11 @@ function resolveAssignments(assignments) {
386
906
  function resolvePatches(ctx) {
387
907
  return resolveAssignments(ctx.assignments);
388
908
  }
389
- 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;
909
+ function assignRef(ctx, index, value) {
910
+ if (ctx.markedRefs[index]) {
911
+ return `${getRefParam(ctx, index)}=${value}`;
396
912
  }
397
- const id = ctx.refs.size;
398
- ctx.refs.set(index, id);
399
- return id;
400
- }
401
- function createValidRef(ctx, index) {
402
- const current = ctx.validRefs.get(index);
403
- if (current != null) {
404
- return current;
405
- }
406
- const id = ctx.validRefs.size;
407
- ctx.validRefs.set(index, id);
408
- return id;
409
- }
410
- function getRefParam(ctx, index) {
411
- const actualIndex = createValidRef(ctx, index);
412
- if (ctx.vars[actualIndex]) {
413
- return ctx.vars[actualIndex];
414
- }
415
- const result = getIdentifier(actualIndex);
416
- ctx.vars[actualIndex] = result;
417
- return result;
418
- }
419
- function assignRef(ctx, index, value) {
420
- if (ctx.markedRefs[index]) {
421
- return `${getRefParam(ctx, index)}=${value}`;
422
- }
423
- return value;
913
+ return value;
424
914
  }
425
915
  function createAssignment(ctx, source, value) {
426
916
  ctx.assignments.push({
@@ -450,699 +940,560 @@ function createArrayAssign(ctx, ref, index, value) {
450
940
  markRef(ctx, ref);
451
941
  createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
452
942
  }
453
- function createObjectIdentifierAssign(ctx, ref, key, value) {
943
+ function createObjectAssign(ctx, ref, key, value) {
454
944
  markRef(ctx, ref);
455
945
  createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
456
946
  }
457
- function createObjectComputedAssign(ctx, ref, key, value) {
458
- markRef(ctx, ref);
459
- createAssignment(ctx, `${getRefParam(ctx, ref)}[${key}]`, value);
460
- }
461
- function getErrorConstructor(error) {
462
- if (error instanceof EvalError) {
463
- return "EvalError";
464
- }
465
- if (error instanceof RangeError) {
466
- return "RangeError";
467
- }
468
- if (error instanceof ReferenceError) {
469
- return "ReferenceError";
470
- }
471
- if (error instanceof SyntaxError) {
472
- return "SyntaxError";
473
- }
474
- if (error instanceof TypeError) {
475
- return "TypeError";
476
- }
477
- if (error instanceof URIError) {
478
- return "URIError";
479
- }
480
- return "Error";
481
- }
482
- function getErrorOptions(error) {
483
- let options;
484
- const constructor = getErrorConstructor(error);
485
- if (error.name !== constructor) {
486
- options = { name: error.name };
487
- } else if (error.constructor.name !== constructor) {
488
- options = { name: error.constructor.name };
489
- }
490
- const names = Object.getOwnPropertyNames(error);
491
- for (const name of names) {
492
- if (name !== "name" && name !== "message") {
493
- options = options || {};
494
- options[name] = error[name];
947
+ var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
948
+ function serializeAssignments(ctx, targetRef, node) {
949
+ ctx.stack.push(targetRef);
950
+ const mainAssignments = [];
951
+ for (let i = 0; i < node.s; i++) {
952
+ const parentStack = ctx.stack;
953
+ ctx.stack = [];
954
+ const refParam = serializeTree(ctx, node.v[i]);
955
+ ctx.stack = parentStack;
956
+ const key = node.k[i];
957
+ const check = Number(key);
958
+ const parentAssignment = ctx.assignments;
959
+ ctx.assignments = mainAssignments;
960
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
961
+ if (isIdentifier && Number.isNaN(check)) {
962
+ createObjectAssign(ctx, targetRef, key, refParam);
963
+ } else {
964
+ createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
495
965
  }
966
+ ctx.assignments = parentAssignment;
496
967
  }
497
- return options;
968
+ ctx.stack.pop();
969
+ return resolveAssignments(mainAssignments);
498
970
  }
499
- function getIterableOptions(obj) {
500
- const names = Object.getOwnPropertyNames(obj);
501
- if (names.length) {
502
- const options = {};
503
- for (const name of names) {
504
- options[name] = obj[name];
971
+ function serializeObject(ctx, sourceID, node) {
972
+ if (node.s === 0) {
973
+ return "{}";
974
+ }
975
+ let result = "";
976
+ ctx.stack.push(sourceID);
977
+ for (let i = 0; i < node.s; i++) {
978
+ const key = node.k[i];
979
+ const val = node.v[i];
980
+ const check = Number(key);
981
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
982
+ if (isReferenceInStack(ctx, val)) {
983
+ const refParam = getRefParam(ctx, val.i);
984
+ if (isIdentifier && Number.isNaN(check)) {
985
+ createObjectAssign(ctx, sourceID, key, refParam);
986
+ } else {
987
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
988
+ }
989
+ } else {
990
+ result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
505
991
  }
506
- return options;
507
992
  }
508
- return void 0;
993
+ ctx.stack.pop();
994
+ return `{${result.substring(0, result.length - 1)}}`;
509
995
  }
510
- function isReferenceInStack(ctx, node) {
511
- return node[0] === 1 /* Reference */ && ctx.stack.includes(node[1]);
512
- }
513
- function generateRef(ctx, current) {
514
- const ref = ctx.refs.has(current);
515
- if (ref) {
516
- const refID = ctx.refs.get(current) || 0;
517
- markRef(ctx, refID);
518
- return [1 /* Reference */, refID];
996
+ function serializePromise(ctx, node) {
997
+ let serialized;
998
+ if (isReferenceInStack(ctx, node.n)) {
999
+ serialized = `Promise.resolve().then(()=>${getRefParam(ctx, node.n.i)})`;
1000
+ } else {
1001
+ ctx.stack.push(node.i);
1002
+ const result = serializeTree(ctx, node.n);
1003
+ ctx.stack.pop();
1004
+ serialized = `Promise.resolve(${result})`;
519
1005
  }
520
- const id = ctx.refs.size;
521
- ctx.refs.set(current, id);
522
- return id;
1006
+ return assignRef(ctx, node.i, serialized);
523
1007
  }
524
- function generateSemiPrimitiveValue(ctx, current, id) {
525
- if (constructorCheck(current, Date)) {
526
- return [2 /* Date */, current, id];
527
- }
528
- if (constructorCheck(current, RegExp)) {
529
- return [3 /* RegExp */, current, id];
530
- }
531
- if (constructorCheck(current, Int8Array)) {
532
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int8Array"');
533
- return [13 /* TypedArray */, ["Int8Array", current], id];
534
- }
535
- if (constructorCheck(current, Int16Array)) {
536
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int16Array"');
537
- return [13 /* TypedArray */, ["Int16Array", current], id];
538
- }
539
- if (constructorCheck(current, Int32Array)) {
540
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int32Array"');
541
- return [13 /* TypedArray */, ["Int32Array", current], id];
542
- }
543
- if (constructorCheck(current, Uint8Array)) {
544
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint8Array"');
545
- return [13 /* TypedArray */, ["Uint8Array", current], id];
546
- }
547
- if (constructorCheck(current, Uint16Array)) {
548
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint16Array"');
549
- return [13 /* TypedArray */, ["Uint16Array", current], id];
550
- }
551
- if (constructorCheck(current, Uint32Array)) {
552
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint32Array"');
553
- return [13 /* TypedArray */, ["Uint32Array", current], id];
554
- }
555
- if (constructorCheck(current, Uint8ClampedArray)) {
556
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint8ClampedArray"');
557
- return [13 /* TypedArray */, ["Uint8ClampedArray", current], id];
558
- }
559
- if (constructorCheck(current, Float32Array)) {
560
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Float32Array"');
561
- return [13 /* TypedArray */, ["Float32Array", current], id];
562
- }
563
- if (constructorCheck(current, Float64Array)) {
564
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Float64Array"');
565
- return [13 /* TypedArray */, ["Float64Array", current], id];
566
- }
567
- if (constructorCheck(current, BigInt64Array)) {
568
- assert(
569
- ctx.features.has(10 /* TypedArray */) && ctx.features.has(3 /* BigInt */),
570
- 'Unsupported value type "BigInt64Array"'
571
- );
572
- return [13 /* TypedArray */, ["BigInt64Array", current], id];
573
- }
574
- if (constructorCheck(current, BigUint64Array)) {
575
- assert(
576
- ctx.features.has(10 /* TypedArray */) && ctx.features.has(3 /* BigInt */),
577
- 'Unsupported value type "BigUint64Array"'
578
- );
579
- return [13 /* TypedArray */, ["BigUint64Array", current], id];
1008
+ function serializeTypedArray(ctx, node) {
1009
+ let args = `[${node.s}]`;
1010
+ if (node.l !== 0) {
1011
+ args += `,${node.l}`;
580
1012
  }
581
- return void 0;
1013
+ return assignRef(ctx, node.i, `new ${node.c}(${args})`);
582
1014
  }
583
- function generateTreeSync(ctx, current) {
584
- function serializeProperties(properties) {
585
- const keyNodes = [];
586
- const valueNodes = [];
587
- const deferredKeys = [];
588
- const deferredValues = [];
589
- const keys = Object.keys(properties);
590
- let deferredSize = 0;
591
- for (const key of keys) {
592
- if (isIterable(properties[key])) {
593
- deferredKeys.push(key);
594
- deferredValues.push(properties[key]);
595
- deferredSize++;
1015
+ function serializeSet(ctx, node) {
1016
+ let serialized = "new Set";
1017
+ const size = node.a.length;
1018
+ if (size) {
1019
+ let result = "";
1020
+ ctx.stack.push(node.i);
1021
+ for (let i = 0; i < size; i++) {
1022
+ const item = node.a[i];
1023
+ if (isReferenceInStack(ctx, item)) {
1024
+ createSetAdd(ctx, node.i, getRefParam(ctx, item.i));
596
1025
  } else {
597
- keyNodes.push(key);
598
- valueNodes.push(generateTreeSync(ctx, properties[key]));
1026
+ result += `${serializeTree(ctx, item)},`;
599
1027
  }
600
1028
  }
601
- for (let i = 0; i < deferredSize; i++) {
602
- keyNodes.push(deferredKeys[i]);
603
- valueNodes.push(generateTreeSync(ctx, deferredValues[i]));
1029
+ ctx.stack.pop();
1030
+ if (result) {
1031
+ serialized += `([${result.substring(0, result.length - 1)}])`;
604
1032
  }
605
- return [keyNodes, valueNodes, keys.length];
606
- }
607
- if (isPrimitive(current)) {
608
- return [0 /* Primitive */, current];
609
1033
  }
610
- const id = generateRef(ctx, current);
611
- if (Array.isArray(id)) {
612
- return id;
613
- }
614
- const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
615
- if (semiPrimitive) {
616
- return semiPrimitive;
617
- }
618
- if (constructorCheck(current, Set)) {
619
- assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
620
- const nodes = [];
621
- const deferred = [];
622
- for (const item of current.keys()) {
623
- if (isIterable(item)) {
624
- deferred.push(item);
1034
+ return assignRef(ctx, node.i, serialized);
1035
+ }
1036
+ function serializeMap(ctx, node) {
1037
+ let serialized = "new Map";
1038
+ if (node.d.s) {
1039
+ let result = "";
1040
+ ctx.stack.push(node.i);
1041
+ for (let i = 0; i < node.d.s; i++) {
1042
+ const key = node.d.k[i];
1043
+ const val = node.d.v[i];
1044
+ if (isReferenceInStack(ctx, key)) {
1045
+ const keyRef = getRefParam(ctx, key.i);
1046
+ if (isReferenceInStack(ctx, val)) {
1047
+ const valueRef = getRefParam(ctx, val.i);
1048
+ createMapSet(ctx, node.i, keyRef, valueRef);
1049
+ } else {
1050
+ const parent = ctx.stack;
1051
+ ctx.stack = [];
1052
+ createMapSet(ctx, node.i, keyRef, serializeTree(ctx, val));
1053
+ ctx.stack = parent;
1054
+ }
1055
+ } else if (isReferenceInStack(ctx, val)) {
1056
+ const valueRef = getRefParam(ctx, val.i);
1057
+ const parent = ctx.stack;
1058
+ ctx.stack = [];
1059
+ createMapSet(ctx, node.i, serializeTree(ctx, key), valueRef);
1060
+ ctx.stack = parent;
625
1061
  } else {
626
- nodes.push(generateTreeSync(ctx, item));
1062
+ result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
627
1063
  }
628
1064
  }
629
- for (let i = 0, len = deferred.length; i < len; i++) {
630
- nodes.push(generateTreeSync(ctx, deferred[i]));
1065
+ ctx.stack.pop();
1066
+ if (result) {
1067
+ serialized += `([${result.substring(0, result.length - 1)}])`;
631
1068
  }
632
- return [4 /* Set */, nodes, id];
633
- }
634
- 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 = [];
640
- let deferredSize = 0;
641
- for (const [key, value] of current.entries()) {
642
- if (isIterable(key) || isIterable(value)) {
643
- deferredKey.push(key);
644
- deferredValue.push(value);
645
- deferredSize++;
1069
+ }
1070
+ return assignRef(ctx, node.i, serialized);
1071
+ }
1072
+ function serializeNodeList(ctx, node) {
1073
+ const size = node.a.length;
1074
+ let values = "";
1075
+ for (let i = 0; i < size; i++) {
1076
+ const item = node.a[i];
1077
+ if (item) {
1078
+ if (isReferenceInStack(ctx, item)) {
1079
+ createArrayAssign(ctx, node.i, i, getRefParam(ctx, item.i));
1080
+ values += ",";
646
1081
  } else {
647
- keyNodes.push(generateTreeSync(ctx, key));
648
- valueNodes.push(generateTreeSync(ctx, value));
1082
+ values += serializeTree(ctx, item);
1083
+ if (i < size - 1) {
1084
+ values += ",";
1085
+ }
649
1086
  }
1087
+ } else {
1088
+ values += ",";
650
1089
  }
651
- for (let i = 0; i < deferredSize; i++) {
652
- keyNodes.push(generateTreeSync(ctx, deferredKey[i]));
653
- valueNodes.push(generateTreeSync(ctx, deferredValue[i]));
654
- }
655
- return [5 /* Map */, [keyNodes, valueNodes, current.size], id];
656
1090
  }
657
- if (Array.isArray(current)) {
658
- const size = current.length;
659
- const nodes = new Array(size);
660
- const deferred = new Array(size);
661
- for (let i = 0; i < size; i++) {
662
- if (i in current) {
663
- if (isIterable(current[i])) {
664
- deferred[i] = current[i];
665
- } else {
666
- nodes[i] = generateTreeSync(ctx, current[i]);
667
- }
1091
+ return `[${values}]`;
1092
+ }
1093
+ function serializeArray(ctx, node) {
1094
+ ctx.stack.push(node.i);
1095
+ const result = serializeNodeList(ctx, node);
1096
+ ctx.stack.pop();
1097
+ return assignRef(ctx, node.i, result);
1098
+ }
1099
+ function serializeWithObjectAssign(ctx, value, id, serialized) {
1100
+ const fields = serializeObject(ctx, id, value);
1101
+ if (fields !== "{}") {
1102
+ return `Object.assign(${serialized},${fields})`;
1103
+ }
1104
+ return serialized;
1105
+ }
1106
+ function serializeAggregateError(ctx, node) {
1107
+ ctx.stack.push(node.i);
1108
+ let serialized = `new AggregateError(${serializeTree(ctx, node.n)},${quote(node.m)})`;
1109
+ ctx.stack.pop();
1110
+ if (node.d) {
1111
+ if (ctx.features & 64 /* ObjectAssign */) {
1112
+ serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1113
+ } else {
1114
+ markRef(ctx, node.i);
1115
+ const assignments = serializeAssignments(ctx, node.i, node.d);
1116
+ if (assignments) {
1117
+ const ref = getRefParam(ctx, node.i);
1118
+ return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
668
1119
  }
669
1120
  }
670
- for (let i = 0; i < size; i++) {
671
- if (i in deferred) {
672
- nodes[i] = generateTreeSync(ctx, deferred[i]);
1121
+ }
1122
+ return assignRef(ctx, node.i, serialized);
1123
+ }
1124
+ function serializeError(ctx, node) {
1125
+ let serialized = `new ${node.c}(${quote(node.m)})`;
1126
+ if (node.d) {
1127
+ if (ctx.features & 64 /* ObjectAssign */) {
1128
+ serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1129
+ } else {
1130
+ markRef(ctx, node.i);
1131
+ const assignments = serializeAssignments(ctx, node.i, node.d);
1132
+ if (assignments) {
1133
+ const ref = getRefParam(ctx, node.i);
1134
+ return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
673
1135
  }
674
1136
  }
675
- return [6 /* Array */, nodes, id];
676
1137
  }
677
- if (current instanceof AggregateError && ctx.features.has(0 /* AggregateError */)) {
678
- const options = getErrorOptions(current);
679
- const optionsNode = options ? serializeProperties(options) : void 0;
680
- const errorsNode = generateTreeSync(ctx, current.errors);
681
- return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
1138
+ return assignRef(ctx, node.i, serialized);
1139
+ }
1140
+ function serializeIterable(ctx, node) {
1141
+ const parent = ctx.stack;
1142
+ ctx.stack = [];
1143
+ const values = serializeNodeList(ctx, node);
1144
+ ctx.stack = parent;
1145
+ let serialized;
1146
+ if (ctx.features & 2 /* ArrayPrototypeValues */) {
1147
+ serialized = `${values}.values()`;
1148
+ } else {
1149
+ serialized = `${values}[Symbol.iterator]()`;
682
1150
  }
683
- if (current instanceof Error) {
684
- const options = getErrorOptions(current);
685
- const optionsNode = options ? serializeProperties(options) : void 0;
686
- return [
687
- 10 /* Error */,
688
- [getErrorConstructor(current), current.message, optionsNode],
689
- id
690
- ];
691
- }
692
- if (isIterable(current)) {
693
- assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
694
- const options = getIterableOptions(current);
695
- return [12 /* Iterable */, [
696
- // Parse options first before the items
697
- options ? serializeProperties(options) : void 0,
698
- generateTreeSync(ctx, Array.from(current))
699
- ], id];
700
- }
701
- const empty = current.constructor == null;
702
- if (current.constructor === Object || empty) {
703
- return [
704
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
705
- serializeProperties(current),
706
- id
707
- ];
1151
+ if (ctx.features & 4 /* ArrowFunction */) {
1152
+ serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1153
+ } else if (ctx.features & 32 /* MethodShorthand */) {
1154
+ serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1155
+ } else {
1156
+ serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
708
1157
  }
709
- throw new Error("Unsupported value");
710
- }
711
- async function generateTreeAsync(ctx, current) {
712
- async function serializeProperties(properties) {
713
- const keyNodes = [];
714
- const valueNodes = [];
715
- const deferredKeys = [];
716
- const deferredValues = [];
717
- const keys = Object.keys(properties);
718
- let deferredSize = 0;
719
- for (const key of keys) {
720
- if (isIterable(properties[key])) {
721
- deferredKeys.push(key);
722
- deferredValues.push(properties[key]);
723
- deferredSize++;
724
- } else {
725
- keyNodes.push(key);
726
- valueNodes.push(await generateTreeAsync(ctx, properties[key]));
1158
+ if (node.d) {
1159
+ if (ctx.features & 64 /* ObjectAssign */) {
1160
+ serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1161
+ } else {
1162
+ markRef(ctx, node.i);
1163
+ const assignments = serializeAssignments(ctx, node.i, node.d);
1164
+ if (assignments) {
1165
+ const ref = getRefParam(ctx, node.i);
1166
+ return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
727
1167
  }
728
1168
  }
729
- for (let i = 0; i < deferredSize; i++) {
730
- keyNodes.push(deferredKeys[i]);
731
- valueNodes.push(await generateTreeAsync(ctx, deferredValues[i]));
1169
+ }
1170
+ return assignRef(ctx, node.i, serialized);
1171
+ }
1172
+ function serializeNullConstructor(ctx, node) {
1173
+ let serialized = "Object.create(null)";
1174
+ if (ctx.features & 64 /* ObjectAssign */) {
1175
+ serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
1176
+ } else {
1177
+ markRef(ctx, node.i);
1178
+ const assignments = serializeAssignments(ctx, node.i, node.d);
1179
+ if (assignments) {
1180
+ const ref = getRefParam(ctx, node.i);
1181
+ return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
732
1182
  }
733
- return [keyNodes, valueNodes, keys.length];
734
1183
  }
735
- if (isPrimitive(current)) {
736
- return [0 /* Primitive */, current];
1184
+ return assignRef(ctx, node.i, serialized);
1185
+ }
1186
+ function serializeTree(ctx, node) {
1187
+ switch (node.t) {
1188
+ case 0 /* Primitive */:
1189
+ return String(node.s);
1190
+ case 2 /* Reference */:
1191
+ return getRefParam(ctx, node.i);
1192
+ case 10 /* Promise */:
1193
+ return serializePromise(ctx, node);
1194
+ case 1 /* BigInt */:
1195
+ return node.s;
1196
+ case 3 /* Date */:
1197
+ return assignRef(ctx, node.i, `new Date("${node.s}")`);
1198
+ case 4 /* RegExp */:
1199
+ return assignRef(ctx, node.i, node.s);
1200
+ case 15 /* BigIntTypedArray */:
1201
+ case 14 /* TypedArray */:
1202
+ return serializeTypedArray(ctx, node);
1203
+ case 5 /* Set */:
1204
+ return serializeSet(ctx, node);
1205
+ case 6 /* Map */:
1206
+ return serializeMap(ctx, node);
1207
+ case 7 /* Array */:
1208
+ return serializeArray(ctx, node);
1209
+ case 12 /* AggregateError */:
1210
+ return serializeAggregateError(ctx, node);
1211
+ case 11 /* Error */:
1212
+ return serializeError(ctx, node);
1213
+ case 13 /* Iterable */:
1214
+ return serializeIterable(ctx, node);
1215
+ case 9 /* NullConstructor */:
1216
+ return serializeNullConstructor(ctx, node);
1217
+ case 8 /* Object */:
1218
+ return assignRef(ctx, node.i, serializeObject(ctx, node.i, node.d));
1219
+ default:
1220
+ throw new Error("Unsupported type");
737
1221
  }
738
- const id = generateRef(ctx, current);
739
- if (Array.isArray(id)) {
740
- return id;
1222
+ }
1223
+
1224
+ // src/tree/sync.ts
1225
+ function serializePropertiesSync(ctx, properties) {
1226
+ const keys = Object.keys(properties);
1227
+ const size = keys.length;
1228
+ const keyNodes = new Array(size);
1229
+ const valueNodes = new Array(size);
1230
+ const deferredKeys = new Array(size);
1231
+ const deferredValues = new Array(size);
1232
+ let deferredSize = 0;
1233
+ let nodesSize = 0;
1234
+ for (const key of keys) {
1235
+ if (isIterable(properties[key])) {
1236
+ deferredKeys[deferredSize] = key;
1237
+ deferredValues[deferredSize] = properties[key];
1238
+ deferredSize++;
1239
+ } else {
1240
+ keyNodes[nodesSize] = key;
1241
+ valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
1242
+ nodesSize++;
1243
+ }
741
1244
  }
742
- const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
743
- if (semiPrimitive) {
744
- return semiPrimitive;
1245
+ for (let i = 0; i < deferredSize; i++) {
1246
+ keyNodes[nodesSize + i] = deferredKeys[i];
1247
+ valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
745
1248
  }
746
- if (isPromise(current)) {
747
- assert(ctx.features.has(7 /* Promise */), 'Unsupported type "Promise"');
748
- return current.then(async (value) => [
749
- 9 /* Promise */,
750
- await generateTreeAsync(ctx, value),
751
- id
752
- ]);
753
- }
754
- if (constructorCheck(current, Set)) {
755
- assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
756
- const nodes = [];
757
- const deferred = [];
758
- for (const item of current.keys()) {
759
- if (isIterable(item)) {
760
- deferred.push(item);
761
- } else {
762
- nodes.push(await generateTreeAsync(ctx, item));
763
- }
1249
+ return {
1250
+ k: keyNodes,
1251
+ v: valueNodes,
1252
+ s: size
1253
+ };
1254
+ }
1255
+ function generateSetNode2(ctx, current, id) {
1256
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
1257
+ const len = current.size;
1258
+ const nodes = new Array(len);
1259
+ const deferred = new Array(len);
1260
+ let deferredSize = 0;
1261
+ let nodeSize = 0;
1262
+ for (const item of current.keys()) {
1263
+ if (isIterable(item)) {
1264
+ deferred[deferredSize++] = item;
1265
+ } else {
1266
+ nodes[nodeSize++] = generateTreeSync(ctx, item);
764
1267
  }
765
- for (let i = 0, len = deferred.length; i < len; i++) {
766
- nodes.push(await generateTreeAsync(ctx, deferred[i]));
1268
+ }
1269
+ for (let i = 0; i < deferredSize; i++) {
1270
+ nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
1271
+ }
1272
+ return {
1273
+ t: 5 /* Set */,
1274
+ i: id,
1275
+ a: nodes,
1276
+ s: void 0,
1277
+ l: void 0,
1278
+ m: void 0,
1279
+ c: void 0,
1280
+ d: void 0,
1281
+ n: void 0
1282
+ };
1283
+ }
1284
+ function generateMapNode2(ctx, current, id) {
1285
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
1286
+ const len = current.size;
1287
+ const keyNodes = new Array(len);
1288
+ const valueNodes = new Array(len);
1289
+ const deferredKey = new Array(len);
1290
+ const deferredValue = new Array(len);
1291
+ let deferredSize = 0;
1292
+ let nodeSize = 0;
1293
+ for (const [key, value] of current.entries()) {
1294
+ if (isIterable(key) || isIterable(value)) {
1295
+ deferredKey[deferredSize] = key;
1296
+ deferredValue[deferredSize] = value;
1297
+ deferredSize++;
1298
+ } else {
1299
+ keyNodes[nodeSize] = generateTreeSync(ctx, key);
1300
+ valueNodes[nodeSize] = generateTreeSync(ctx, value);
1301
+ nodeSize++;
767
1302
  }
768
- return [4 /* Set */, nodes, id];
769
- }
770
- 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 = [];
776
- let deferredSize = 0;
777
- for (const [key, value] of current.entries()) {
778
- if (isIterable(key) || isIterable(value)) {
779
- deferredKey.push(key);
780
- deferredValue.push(value);
781
- deferredSize++;
1303
+ }
1304
+ for (let i = 0; i < deferredSize; i++) {
1305
+ keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
1306
+ valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
1307
+ }
1308
+ return {
1309
+ t: 6 /* Map */,
1310
+ i: id,
1311
+ a: void 0,
1312
+ s: void 0,
1313
+ l: void 0,
1314
+ m: void 0,
1315
+ c: void 0,
1316
+ d: { k: keyNodes, v: valueNodes, s: len },
1317
+ n: void 0
1318
+ };
1319
+ }
1320
+ function generateNodeList2(ctx, current) {
1321
+ const size = current.length;
1322
+ const nodes = new Array(size);
1323
+ const deferred = new Array(size);
1324
+ for (let i = 0; i < size; i++) {
1325
+ if (i in current) {
1326
+ if (isIterable(current[i])) {
1327
+ deferred[i] = current[i];
782
1328
  } else {
783
- keyNodes.push(await generateTreeAsync(ctx, key));
784
- valueNodes.push(await generateTreeAsync(ctx, value));
1329
+ nodes[i] = generateTreeSync(ctx, current[i]);
785
1330
  }
786
1331
  }
787
- for (let i = 0; i < deferredSize; i++) {
788
- keyNodes.push(await generateTreeAsync(ctx, deferredKey[i]));
789
- valueNodes.push(await generateTreeAsync(ctx, deferredValue[i]));
790
- }
791
- return [5 /* Map */, [keyNodes, valueNodes, current.size], id];
792
1332
  }
793
- if (Array.isArray(current)) {
794
- const size = current.length;
795
- const nodes = new Array(size);
796
- const deferred = new Array(size);
797
- for (let i = 0; i < size; i++) {
798
- const item = current[i];
799
- if (i in current) {
800
- if (isIterable(item)) {
801
- deferred[i] = item;
802
- } else {
803
- nodes[i] = await generateTreeAsync(ctx, item);
804
- }
805
- }
1333
+ for (let i = 0; i < size; i++) {
1334
+ if (i in deferred) {
1335
+ nodes[i] = generateTreeSync(ctx, deferred[i]);
806
1336
  }
807
- for (let i = 0; i < size; i++) {
808
- if (i in deferred) {
809
- nodes[i] = await generateTreeAsync(ctx, deferred[i]);
810
- }
811
- }
812
- return [6 /* Array */, nodes, id];
813
- }
814
- if (current instanceof AggregateError) {
815
- const options = getErrorOptions(current);
816
- const optionsNode = options ? await serializeProperties(options) : void 0;
817
- const errorsNode = await generateTreeAsync(ctx, current.errors);
818
- return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
819
- }
820
- if (current instanceof Error) {
821
- const options = getErrorOptions(current);
822
- const optionsNode = options ? await serializeProperties(options) : void 0;
823
- return [
824
- 10 /* Error */,
825
- [getErrorConstructor(current), current.message, optionsNode],
826
- id
827
- ];
828
- }
829
- if (isIterable(current)) {
830
- assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
831
- const options = getIterableOptions(current);
832
- return [12 /* Iterable */, [
833
- options ? await serializeProperties(options) : void 0,
834
- await generateTreeAsync(ctx, Array.from(current))
835
- ], id];
836
- }
837
- const empty = current.constructor == null;
838
- if (current.constructor === Object || empty) {
839
- return [
840
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
841
- await serializeProperties(current),
842
- id
843
- ];
844
1337
  }
845
- throw new Error("Unsupported value");
1338
+ return nodes;
846
1339
  }
847
- function serializePrimitive(ctx, value) {
848
- if (value === true) {
849
- return "!0";
1340
+ function generateArrayNode2(ctx, current, id) {
1341
+ return {
1342
+ t: 7 /* Array */,
1343
+ i: id,
1344
+ a: generateNodeList2(ctx, current),
1345
+ s: void 0,
1346
+ l: void 0,
1347
+ m: void 0,
1348
+ c: void 0,
1349
+ d: void 0,
1350
+ n: void 0
1351
+ };
1352
+ }
1353
+ function generateAggregateErrorNode2(ctx, current, id) {
1354
+ const options = getErrorOptions(current);
1355
+ const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
1356
+ return {
1357
+ t: 12 /* AggregateError */,
1358
+ i: id,
1359
+ a: void 0,
1360
+ s: void 0,
1361
+ l: void 0,
1362
+ m: current.message,
1363
+ c: void 0,
1364
+ d: optionsNode,
1365
+ n: generateTreeSync(ctx, current.errors)
1366
+ };
1367
+ }
1368
+ function generateErrorNode2(ctx, current, id) {
1369
+ const options = getErrorOptions(current);
1370
+ const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
1371
+ return {
1372
+ t: 11 /* Error */,
1373
+ i: id,
1374
+ a: void 0,
1375
+ s: void 0,
1376
+ l: void 0,
1377
+ m: current.message,
1378
+ c: getErrorConstructor(current),
1379
+ d: optionsNode,
1380
+ n: void 0
1381
+ };
1382
+ }
1383
+ function generateIterableNode2(ctx, current, id) {
1384
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
1385
+ const options = getIterableOptions(current);
1386
+ return {
1387
+ t: 13 /* Iterable */,
1388
+ i: id,
1389
+ s: void 0,
1390
+ l: void 0,
1391
+ m: void 0,
1392
+ c: void 0,
1393
+ // Parse options first before the items
1394
+ d: options ? serializePropertiesSync(ctx, options) : void 0,
1395
+ a: generateNodeList2(ctx, Array.from(current)),
1396
+ n: void 0
1397
+ };
1398
+ }
1399
+ function generateTreeSync(ctx, current) {
1400
+ if (isPrimitive(current)) {
1401
+ return {
1402
+ t: 0 /* Primitive */,
1403
+ i: void 0,
1404
+ s: serializePrimitive(current),
1405
+ l: void 0,
1406
+ m: void 0,
1407
+ c: void 0,
1408
+ // Parse options first before the items
1409
+ d: void 0,
1410
+ a: void 0,
1411
+ n: void 0
1412
+ };
1413
+ }
1414
+ if (typeof current === "bigint") {
1415
+ assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
1416
+ return {
1417
+ t: 1 /* BigInt */,
1418
+ i: void 0,
1419
+ a: void 0,
1420
+ s: `${current}n`,
1421
+ l: void 0,
1422
+ m: void 0,
1423
+ c: void 0,
1424
+ d: void 0,
1425
+ n: void 0
1426
+ };
1427
+ }
1428
+ const id = createRef(ctx, current, true);
1429
+ if (ctx.markedRefs[id]) {
1430
+ return {
1431
+ t: 2 /* Reference */,
1432
+ i: id,
1433
+ a: void 0,
1434
+ s: void 0,
1435
+ l: void 0,
1436
+ m: void 0,
1437
+ c: void 0,
1438
+ d: void 0,
1439
+ n: void 0
1440
+ };
850
1441
  }
851
- if (value === false) {
852
- return "!1";
1442
+ if (Array.isArray(current)) {
1443
+ return generateArrayNode2(ctx, current, id);
853
1444
  }
854
- if (value === void 0) {
855
- return "void 0";
1445
+ const cs = current.constructor;
1446
+ const empty = cs == null;
1447
+ if (cs === Object || empty) {
1448
+ if (Symbol.iterator in current) {
1449
+ return generateIterableNode2(ctx, current, id);
1450
+ }
1451
+ return {
1452
+ t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
1453
+ i: id,
1454
+ s: void 0,
1455
+ l: void 0,
1456
+ m: void 0,
1457
+ c: void 0,
1458
+ // Parse options first before the items
1459
+ d: serializePropertiesSync(ctx, current),
1460
+ a: void 0,
1461
+ n: void 0
1462
+ };
856
1463
  }
857
- if (value === null) {
858
- return "null";
1464
+ if (cs === Set) {
1465
+ return generateSetNode2(ctx, current, id);
859
1466
  }
860
- if (typeof value === "bigint") {
861
- assert(ctx.features.has(3 /* BigInt */), 'Unsupported type "BigInt"');
862
- return `${value}n`;
1467
+ if (cs === Map) {
1468
+ return generateMapNode2(ctx, current, id);
863
1469
  }
864
- if (typeof value === "string") {
865
- return quote(value);
1470
+ const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
1471
+ if (semiPrimitive) {
1472
+ return semiPrimitive;
866
1473
  }
867
- if (Object.is(value, -0)) {
868
- return "-0";
1474
+ if (Symbol.iterator in current) {
1475
+ return generateIterableNode2(ctx, current, id);
869
1476
  }
870
- if (Object.is(value, Infinity)) {
871
- return "1/0";
1477
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
1478
+ return generateAggregateErrorNode2(ctx, current, id);
872
1479
  }
873
- if (Object.is(value, -Infinity)) {
874
- return "-1/0";
1480
+ if (current instanceof Error) {
1481
+ return generateErrorNode2(ctx, current, id);
875
1482
  }
876
- return String(value);
1483
+ throw new Error("Unsupported value");
877
1484
  }
878
- function serializeTree(ctx, [type, value, id]) {
879
- function serializeAssignments(targetRef, [keys, values, size]) {
880
- ctx.stack.push(targetRef);
881
- const mainAssignments = [];
882
- for (let i = 0; i < size; i++) {
883
- const parentStack = ctx.stack;
884
- ctx.stack = [];
885
- const refParam = serializeTree(ctx, values[i]);
886
- ctx.stack = parentStack;
887
- const key = keys[i];
888
- const check = Number(key);
889
- const parentAssignment = ctx.assignments;
890
- ctx.assignments = mainAssignments;
891
- if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.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
- }
900
- ctx.assignments = parentAssignment;
901
- }
902
- ctx.stack.pop();
903
- return resolveAssignments(mainAssignments);
904
- }
905
- function serializeObject(sourceID, [keys, values, size]) {
906
- let result = "";
907
- ctx.stack.push(sourceID);
908
- for (let i = 0; i < size; i++) {
909
- const key = keys[i];
910
- const val = values[i];
911
- const check = Number(key);
912
- if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.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)) {
924
- const refParam = getRefParam(ctx, val[1]);
925
- createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
926
- } else {
927
- result += `${quote(key)}:${serializeTree(ctx, val)},`;
928
- }
929
- }
930
- ctx.stack.pop();
931
- return `{${result.substring(0, result.length - 1)}}`;
932
- }
933
- switch (type) {
934
- case 0 /* Primitive */:
935
- return serializePrimitive(ctx, value);
936
- case 1 /* Reference */:
937
- return getRefParam(ctx, value);
938
- case 9 /* Promise */: {
939
- let serialized;
940
- if (isReferenceInStack(ctx, value)) {
941
- serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
942
- } else {
943
- ctx.stack.push(id);
944
- const result = serializeTree(ctx, value);
945
- ctx.stack.pop();
946
- serialized = `Promise.resolve(${result})`;
947
- }
948
- return assignRef(ctx, id, serialized);
949
- }
950
- case 2 /* Date */:
951
- return assignRef(ctx, id, `new Date("${value.toISOString()}")`);
952
- case 3 /* RegExp */:
953
- return assignRef(ctx, id, String(value));
954
- case 13 /* TypedArray */: {
955
- let values = "";
956
- for (let i = 0, len = value[1].length; i < len; i++) {
957
- values += `${serializePrimitive(ctx, value[1][i])},`;
958
- }
959
- let args = values ? `[${values.substring(0, values.length - 1)}]` : "[]";
960
- if (value[1].byteOffset !== 0) {
961
- args += `,${value[1].byteOffset}`;
962
- }
963
- return assignRef(ctx, id, `new ${value[0]}(${args})`);
964
- }
965
- case 4 /* Set */: {
966
- let serialized = "new Set";
967
- const size = value.length;
968
- if (size) {
969
- let result = "";
970
- ctx.stack.push(id);
971
- for (let i = 0; i < size; i++) {
972
- const item = value[i];
973
- if (isReferenceInStack(ctx, item)) {
974
- createSetAdd(ctx, id, getRefParam(ctx, item[1]));
975
- } else {
976
- result += `${serializeTree(ctx, item)},`;
977
- }
978
- }
979
- ctx.stack.pop();
980
- if (result) {
981
- serialized += `([${result.substring(0, result.length - 1)}])`;
982
- }
983
- }
984
- return assignRef(ctx, id, serialized);
985
- }
986
- case 5 /* Map */: {
987
- let serialized = "new Map";
988
- const size = value[2];
989
- if (size) {
990
- let result = "";
991
- ctx.stack.push(id);
992
- for (let i = 0; i < size; i++) {
993
- const key = value[0][i];
994
- const val = value[1][i];
995
- if (isReferenceInStack(ctx, key)) {
996
- const keyRef = getRefParam(ctx, key[1]);
997
- if (isReferenceInStack(ctx, val)) {
998
- const valueRef = getRefParam(ctx, val[1]);
999
- createMapSet(ctx, id, keyRef, valueRef);
1000
- } else {
1001
- const parent = ctx.stack;
1002
- ctx.stack = [];
1003
- createMapSet(ctx, id, keyRef, serializeTree(ctx, val));
1004
- ctx.stack = parent;
1005
- }
1006
- } else if (isReferenceInStack(ctx, val)) {
1007
- const valueRef = getRefParam(ctx, val[1]);
1008
- const parent = ctx.stack;
1009
- ctx.stack = [];
1010
- createMapSet(ctx, id, serializeTree(ctx, key), valueRef);
1011
- ctx.stack = parent;
1012
- } else {
1013
- result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
1014
- }
1015
- }
1016
- ctx.stack.pop();
1017
- if (result) {
1018
- serialized += `([${result.substring(0, result.length - 1)}])`;
1019
- }
1020
- }
1021
- return assignRef(ctx, id, serialized);
1022
- }
1023
- case 6 /* Array */: {
1024
- const size = value.length;
1025
- let values = "";
1026
- ctx.stack.push(id);
1027
- for (let i = 0; i < size; i++) {
1028
- const item = value[i];
1029
- if (i in value) {
1030
- if (isReferenceInStack(ctx, item)) {
1031
- createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
1032
- values += ",";
1033
- } else {
1034
- values += serializeTree(ctx, item);
1035
- if (i < size - 1) {
1036
- values += ",";
1037
- }
1038
- }
1039
- } else {
1040
- values += ",";
1041
- }
1042
- }
1043
- ctx.stack.pop();
1044
- return assignRef(ctx, id, `[${values}]`);
1045
- }
1046
- case 11 /* AggregateError */: {
1047
- ctx.stack.push(id);
1048
- let serialized = `new AggregateError(${serializeTree(ctx, value[2])},${quote(value[0])})`;
1049
- ctx.stack.pop();
1050
- if (value[1]) {
1051
- if (ctx.features.has(6 /* ObjectAssign */)) {
1052
- const options = serializeObject(id, value[1]);
1053
- serialized = `Object.assign(${serialized},${options})`;
1054
- } else {
1055
- markRef(ctx, id);
1056
- const assignments = serializeAssignments(id, value[1]);
1057
- if (assignments) {
1058
- const ref = getRefParam(ctx, id);
1059
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1060
- }
1061
- }
1062
- }
1063
- return assignRef(ctx, id, serialized);
1064
- }
1065
- case 10 /* Error */: {
1066
- let serialized = `new ${value[0]}(${quote(value[1])})`;
1067
- if (value[2]) {
1068
- if (ctx.features.has(6 /* ObjectAssign */)) {
1069
- const options = serializeObject(id, value[2]);
1070
- serialized = `Object.assign(${serialized},${options})`;
1071
- } else {
1072
- markRef(ctx, id);
1073
- const assignments = serializeAssignments(id, value[2]);
1074
- if (assignments) {
1075
- const ref = getRefParam(ctx, id);
1076
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1077
- }
1078
- }
1079
- }
1080
- return assignRef(ctx, id, serialized);
1081
- }
1082
- case 12 /* Iterable */: {
1083
- const parent = ctx.stack;
1084
- ctx.stack = [];
1085
- const values = serializeTree(ctx, value[1]);
1086
- ctx.stack = parent;
1087
- let serialized;
1088
- if (ctx.features.has(1 /* ArrayPrototypeValues */)) {
1089
- serialized = `${values}.values()`;
1090
- } else {
1091
- serialized = `${values}[Symbol.iterator]()`;
1092
- }
1093
- if (ctx.features.has(2 /* ArrowFunction */)) {
1094
- serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1095
- } else if (ctx.features.has(5 /* MethodShorthand */)) {
1096
- serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1097
- } else {
1098
- serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1099
- }
1100
- if (value[0]) {
1101
- if (ctx.features.has(6 /* ObjectAssign */)) {
1102
- const options = serializeObject(id, value[0]);
1103
- serialized = `Object.assign(${serialized},${options})`;
1104
- } else {
1105
- markRef(ctx, id);
1106
- const assignments = serializeAssignments(id, value[0]);
1107
- if (assignments) {
1108
- const ref = getRefParam(ctx, id);
1109
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1110
- }
1111
- }
1112
- }
1113
- return assignRef(ctx, id, serialized);
1114
- }
1115
- case 8 /* NullConstructor */: {
1116
- let serialized = "Object.create(null)";
1117
- if (ctx.features.has(6 /* ObjectAssign */)) {
1118
- const fields = serializeObject(id, value);
1119
- if (fields !== "{}") {
1120
- serialized = `Object.assign(${serialized},${fields})`;
1121
- }
1122
- } else {
1123
- markRef(ctx, id);
1124
- const assignments = serializeAssignments(id, value);
1125
- if (assignments) {
1126
- const ref = getRefParam(ctx, id);
1127
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1128
- }
1129
- }
1130
- return assignRef(ctx, id, serialized);
1131
- }
1132
- case 7 /* Object */:
1133
- return assignRef(ctx, id, serializeObject(id, value));
1134
- default:
1135
- throw new Error("Unsupported type");
1136
- }
1485
+ function parseSync(ctx, current) {
1486
+ const result = generateTreeSync(ctx, current);
1487
+ return [result, createRef(ctx, current, false), result.t === 8 /* Object */];
1137
1488
  }
1138
1489
 
1139
1490
  // src/index.ts
1140
- function finalize(ctx, source, result) {
1491
+ function finalize(ctx, rootID, isObject, result) {
1141
1492
  if (ctx.vars.length) {
1142
1493
  const patches = resolvePatches(ctx);
1143
1494
  let body = result;
1144
1495
  if (patches) {
1145
- const index = getRefParam(ctx, createRef(ctx, source));
1496
+ const index = getRefParam(ctx, rootID);
1146
1497
  if (result.startsWith(`${index}=`)) {
1147
1498
  body = `${result},${patches}${index}`;
1148
1499
  } else {
@@ -1150,37 +1501,73 @@ function finalize(ctx, source, result) {
1150
1501
  }
1151
1502
  }
1152
1503
  let params = ctx.vars.length > 1 ? ctx.vars.join(",") : ctx.vars[0];
1153
- if (ctx.features.has(2 /* ArrowFunction */)) {
1504
+ if (ctx.features & 4 /* ArrowFunction */) {
1154
1505
  params = ctx.vars.length > 1 || ctx.vars.length === 0 ? `(${params})` : params;
1155
1506
  return `(${params}=>(${body}))()`;
1156
1507
  }
1157
1508
  return `(function(${params}){return ${body}})()`;
1158
1509
  }
1159
- if (source.constructor === Object) {
1510
+ if (isObject) {
1160
1511
  return `(${result})`;
1161
1512
  }
1162
1513
  return result;
1163
1514
  }
1164
1515
  function serialize(source, options) {
1165
- const ctx = createSerializationContext(options);
1516
+ const ctx = createParserContext(options);
1166
1517
  if (isPrimitive(source)) {
1167
- return serializePrimitive(ctx, source);
1518
+ return String(serializePrimitive(source));
1168
1519
  }
1169
- const tree = generateTreeSync(ctx, source);
1170
- const result = serializeTree(ctx, tree);
1171
- return finalize(ctx, source, result);
1520
+ const [tree, rootID, isObject] = parseSync(ctx, source);
1521
+ const serial = createSerializationContext(ctx);
1522
+ const result = serializeTree(serial, tree);
1523
+ return finalize(serial, rootID, isObject, result);
1172
1524
  }
1173
1525
  async function serializeAsync(source, options) {
1174
- const ctx = createSerializationContext(options);
1526
+ const ctx = createParserContext(options);
1175
1527
  if (isPrimitive(source)) {
1176
- return serializePrimitive(ctx, source);
1528
+ return String(serializePrimitive(source));
1177
1529
  }
1178
- const tree = await generateTreeAsync(ctx, source);
1179
- const result = serializeTree(ctx, tree);
1180
- return finalize(ctx, source, result);
1530
+ const [tree, rootID, isObject] = await parseAsync(ctx, source);
1531
+ const serial = createSerializationContext(ctx);
1532
+ const result = serializeTree(serial, tree);
1533
+ return finalize(serial, rootID, isObject, result);
1181
1534
  }
1182
1535
  function deserialize(source) {
1183
1536
  return (0, eval)(source);
1184
1537
  }
1538
+ function toJSON(source, options) {
1539
+ const ctx = createParserContext(options);
1540
+ const [tree, root, isObject] = parseSync(ctx, source);
1541
+ return JSON.stringify([
1542
+ tree,
1543
+ root,
1544
+ isObject,
1545
+ ctx.features,
1546
+ ctx.markedRefs
1547
+ ]);
1548
+ }
1549
+ async function toJSONAsync(source, options) {
1550
+ const ctx = createParserContext(options);
1551
+ const [tree, root, isObject] = await parseAsync(ctx, source);
1552
+ return JSON.stringify([
1553
+ tree,
1554
+ root,
1555
+ isObject,
1556
+ ctx.features,
1557
+ ctx.markedRefs
1558
+ ]);
1559
+ }
1560
+ function compileJSON(source) {
1561
+ const parsed = JSON.parse(source);
1562
+ const serial = createSerializationContext({
1563
+ features: parsed[3],
1564
+ markedRefs: parsed[4]
1565
+ });
1566
+ const result = serializeTree(serial, parsed[0]);
1567
+ return finalize(serial, parsed[1], parsed[2], result);
1568
+ }
1569
+ function fromJSON(source) {
1570
+ return deserialize(compileJSON(source));
1571
+ }
1185
1572
  var src_default = serialize;
1186
1573
  //# sourceMappingURL=index.cjs.map