seroval 0.4.0-alpha.1 → 0.4.0-alpha.11

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