seroval 0.4.0-alpha.3 → 0.4.0-alpha.5

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