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