seroval 0.4.0-alpha.4 → 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.
- package/dist/cjs/development/index.cjs +642 -610
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +2 -2
- package/dist/esm/development/index.mjs +642 -610
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +2 -2
- package/dist/types/context.d.ts +53 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/tree/async.d.ts +4 -0
- package/dist/types/tree/serialize.d.ts +4 -0
- package/dist/types/tree/shared.d.ts +10 -0
- package/dist/types/tree/sync.d.ts +4 -0
- package/dist/types/tree/types.d.ts +79 -0
- package/package.json +2 -2
- package/src/context.ts +137 -0
- package/src/index.ts +9 -10
- package/src/tree/async.ts +260 -0
- package/src/tree/serialize.ts +563 -0
- package/src/tree/shared.ts +177 -0
- package/src/tree/sync.ts +242 -0
- package/src/tree/types.ts +101 -0
- package/dist/types/tree.d.ts +0 -122
- package/src/tree.ts +0 -1246
|
@@ -263,48 +263,6 @@ function parseTargets(targets) {
|
|
|
263
263
|
return flags;
|
|
264
264
|
}
|
|
265
265
|
|
|
266
|
-
// src/quote.ts
|
|
267
|
-
function quote(str) {
|
|
268
|
-
let result = "";
|
|
269
|
-
let lastPos = 0;
|
|
270
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
|
271
|
-
let replacement;
|
|
272
|
-
switch (str[i]) {
|
|
273
|
-
case '"':
|
|
274
|
-
replacement = '\\"';
|
|
275
|
-
break;
|
|
276
|
-
case "\\":
|
|
277
|
-
replacement = "\\\\";
|
|
278
|
-
break;
|
|
279
|
-
case "<":
|
|
280
|
-
replacement = "\\x3C";
|
|
281
|
-
break;
|
|
282
|
-
case "\n":
|
|
283
|
-
replacement = "\\n";
|
|
284
|
-
break;
|
|
285
|
-
case "\r":
|
|
286
|
-
replacement = "\\r";
|
|
287
|
-
break;
|
|
288
|
-
case "\u2028":
|
|
289
|
-
replacement = "\\u2028";
|
|
290
|
-
break;
|
|
291
|
-
case "\u2029":
|
|
292
|
-
replacement = "\\u2029";
|
|
293
|
-
break;
|
|
294
|
-
default:
|
|
295
|
-
continue;
|
|
296
|
-
}
|
|
297
|
-
result += str.slice(lastPos, i) + replacement;
|
|
298
|
-
lastPos = i + 1;
|
|
299
|
-
}
|
|
300
|
-
if (lastPos === 0) {
|
|
301
|
-
result = str;
|
|
302
|
-
} else {
|
|
303
|
-
result += str.slice(lastPos);
|
|
304
|
-
}
|
|
305
|
-
return `"${result}"`;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
266
|
// src/get-identifier.ts
|
|
309
267
|
var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
|
|
310
268
|
var REF_START_CHARS_LEN = REF_START_CHARS.length;
|
|
@@ -322,7 +280,7 @@ function getIdentifier(index) {
|
|
|
322
280
|
return ref;
|
|
323
281
|
}
|
|
324
282
|
|
|
325
|
-
// src/
|
|
283
|
+
// src/context.ts
|
|
326
284
|
var DEFAULT_OPTIONS = {
|
|
327
285
|
target: "es2023"
|
|
328
286
|
};
|
|
@@ -344,71 +302,6 @@ function createSerializationContext(options) {
|
|
|
344
302
|
markedRefs: new Set(options.markedRefs)
|
|
345
303
|
};
|
|
346
304
|
}
|
|
347
|
-
function getAssignmentExpression(assignment) {
|
|
348
|
-
switch (assignment.type) {
|
|
349
|
-
case "index":
|
|
350
|
-
return `${assignment.source}=${assignment.value}`;
|
|
351
|
-
case "map":
|
|
352
|
-
return `${assignment.source}.set(${assignment.key},${assignment.value})`;
|
|
353
|
-
case "set":
|
|
354
|
-
return `${assignment.source}.add(${assignment.value})`;
|
|
355
|
-
default:
|
|
356
|
-
return "";
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
function mergeAssignments(assignments) {
|
|
360
|
-
const newAssignments = [];
|
|
361
|
-
let current = assignments[0];
|
|
362
|
-
let prev = current;
|
|
363
|
-
for (let i = 1, len = assignments.length; i < len; i++) {
|
|
364
|
-
const item = assignments[i];
|
|
365
|
-
if (item.type === prev.type) {
|
|
366
|
-
if (item.type === "index" && item.value === prev.value) {
|
|
367
|
-
current = {
|
|
368
|
-
type: "index",
|
|
369
|
-
source: item.source,
|
|
370
|
-
value: getAssignmentExpression(current)
|
|
371
|
-
};
|
|
372
|
-
} else if (item.type === "map" && item.source === prev.source) {
|
|
373
|
-
current = {
|
|
374
|
-
type: "map",
|
|
375
|
-
source: getAssignmentExpression(current),
|
|
376
|
-
key: item.key,
|
|
377
|
-
value: item.value
|
|
378
|
-
};
|
|
379
|
-
} else if (item.type === "set" && item.source === prev.source) {
|
|
380
|
-
current = {
|
|
381
|
-
type: "set",
|
|
382
|
-
source: getAssignmentExpression(current),
|
|
383
|
-
value: item.value
|
|
384
|
-
};
|
|
385
|
-
} else {
|
|
386
|
-
newAssignments.push(current);
|
|
387
|
-
current = item;
|
|
388
|
-
}
|
|
389
|
-
} else {
|
|
390
|
-
newAssignments.push(current);
|
|
391
|
-
current = item;
|
|
392
|
-
}
|
|
393
|
-
prev = item;
|
|
394
|
-
}
|
|
395
|
-
newAssignments.push(current);
|
|
396
|
-
return newAssignments;
|
|
397
|
-
}
|
|
398
|
-
function resolveAssignments(assignments) {
|
|
399
|
-
if (assignments.length) {
|
|
400
|
-
let result = "";
|
|
401
|
-
const merged = mergeAssignments(assignments);
|
|
402
|
-
for (let i = 0, len = merged.length; i < len; i++) {
|
|
403
|
-
result += `${getAssignmentExpression(merged[i])},`;
|
|
404
|
-
}
|
|
405
|
-
return result;
|
|
406
|
-
}
|
|
407
|
-
return void 0;
|
|
408
|
-
}
|
|
409
|
-
function resolvePatches(ctx) {
|
|
410
|
-
return resolveAssignments(ctx.assignments);
|
|
411
|
-
}
|
|
412
305
|
function markRef(ctx, current) {
|
|
413
306
|
ctx.markedRefs.add(current);
|
|
414
307
|
}
|
|
@@ -430,12 +323,6 @@ function getRefParam(ctx, index) {
|
|
|
430
323
|
ctx.vars[actualIndex] = result;
|
|
431
324
|
return result;
|
|
432
325
|
}
|
|
433
|
-
function assignRef(ctx, index, value) {
|
|
434
|
-
if (ctx.markedRefs.has(index)) {
|
|
435
|
-
return `${getRefParam(ctx, index)}=${value}`;
|
|
436
|
-
}
|
|
437
|
-
return value;
|
|
438
|
-
}
|
|
439
326
|
function createRef(ctx, index) {
|
|
440
327
|
const current = ctx.refs.get(index);
|
|
441
328
|
if (current != null) {
|
|
@@ -445,39 +332,50 @@ function createRef(ctx, index) {
|
|
|
445
332
|
ctx.refs.set(index, id);
|
|
446
333
|
return id;
|
|
447
334
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
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}"`;
|
|
480
376
|
}
|
|
377
|
+
|
|
378
|
+
// src/tree/shared.ts
|
|
481
379
|
function getErrorConstructor(error) {
|
|
482
380
|
if (error instanceof EvalError) {
|
|
483
381
|
return "EvalError";
|
|
@@ -552,46 +450,16 @@ function generateSemiPrimitiveValue(ctx, current, id) {
|
|
|
552
450
|
if (constructorCheck(current, RegExp)) {
|
|
553
451
|
return [4 /* RegExp */, String(current), id];
|
|
554
452
|
}
|
|
555
|
-
if (constructorCheck(current, Int8Array)) {
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
if (constructorCheck(current, Int16Array)) {
|
|
560
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int16Array"');
|
|
561
|
-
return [14 /* TypedArray */, ["Int16Array", current.toString(), current.byteOffset], id];
|
|
562
|
-
}
|
|
563
|
-
if (constructorCheck(current, Int32Array)) {
|
|
564
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int32Array"');
|
|
565
|
-
return [14 /* TypedArray */, ["Int32Array", current.toString(), current.byteOffset], id];
|
|
566
|
-
}
|
|
567
|
-
if (constructorCheck(current, Uint8Array)) {
|
|
568
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint8Array"');
|
|
569
|
-
return [14 /* TypedArray */, ["Uint8Array", current.toString(), current.byteOffset], id];
|
|
570
|
-
}
|
|
571
|
-
if (constructorCheck(current, Uint16Array)) {
|
|
572
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint16Array"');
|
|
573
|
-
return [14 /* TypedArray */, ["Uint16Array", current.toString(), current.byteOffset], id];
|
|
574
|
-
}
|
|
575
|
-
if (constructorCheck(current, Uint32Array)) {
|
|
576
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint32Array"');
|
|
577
|
-
return [14 /* TypedArray */, ["Uint32Array", current.toString(), current.byteOffset], 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];
|
|
578
457
|
}
|
|
579
|
-
if (constructorCheck(current,
|
|
580
|
-
|
|
581
|
-
return [14 /* TypedArray */, ["Uint8ClampedArray", current.toString(), current.byteOffset], id];
|
|
582
|
-
}
|
|
583
|
-
if (constructorCheck(current, Float32Array)) {
|
|
584
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float32Array"');
|
|
585
|
-
return [14 /* TypedArray */, ["Float32Array", current.toString(), current.byteOffset], id];
|
|
586
|
-
}
|
|
587
|
-
if (constructorCheck(current, Float64Array)) {
|
|
588
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float64Array"');
|
|
589
|
-
return [14 /* TypedArray */, ["Float64Array", current.toString(), current.byteOffset], id];
|
|
590
|
-
}
|
|
591
|
-
if (constructorCheck(current, BigInt64Array)) {
|
|
458
|
+
if (constructorCheck(current, BigInt64Array) || constructorCheck(current, BigUint64Array)) {
|
|
459
|
+
const constructor = current.constructor.name;
|
|
592
460
|
assert(
|
|
593
461
|
ctx.features & 2048 /* BigIntTypedArray */,
|
|
594
|
-
|
|
462
|
+
`Unsupported value type "${constructor}"`
|
|
595
463
|
);
|
|
596
464
|
let result = "";
|
|
597
465
|
const cap = current.length - 1;
|
|
@@ -601,30 +469,42 @@ function generateSemiPrimitiveValue(ctx, current, id) {
|
|
|
601
469
|
result += `"${current[cap]}"`;
|
|
602
470
|
return [
|
|
603
471
|
15 /* BigIntTypedArray */,
|
|
604
|
-
[
|
|
605
|
-
id
|
|
606
|
-
];
|
|
607
|
-
}
|
|
608
|
-
if (constructorCheck(current, BigUint64Array)) {
|
|
609
|
-
assert(
|
|
610
|
-
ctx.features & 2048 /* BigIntTypedArray */,
|
|
611
|
-
'Unsupported value type "BigUint64Array"'
|
|
612
|
-
);
|
|
613
|
-
let result = "";
|
|
614
|
-
const cap = current.length - 1;
|
|
615
|
-
for (let i = 0; i < cap; i++) {
|
|
616
|
-
result += `"${current[i]}n",`;
|
|
617
|
-
}
|
|
618
|
-
result += `"${current[cap]}"`;
|
|
619
|
-
return [
|
|
620
|
-
15 /* BigIntTypedArray */,
|
|
621
|
-
["BigInt64Array", result, current.byteOffset],
|
|
472
|
+
[constructor, result, current.byteOffset],
|
|
622
473
|
id
|
|
623
474
|
];
|
|
624
475
|
}
|
|
625
476
|
return void 0;
|
|
626
477
|
}
|
|
627
|
-
function
|
|
478
|
+
function serializePrimitive(value) {
|
|
479
|
+
if (value === true) {
|
|
480
|
+
return "!0";
|
|
481
|
+
}
|
|
482
|
+
if (value === false) {
|
|
483
|
+
return "!1";
|
|
484
|
+
}
|
|
485
|
+
if (value === void 0) {
|
|
486
|
+
return "void 0";
|
|
487
|
+
}
|
|
488
|
+
if (value === null) {
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
if (typeof value === "string") {
|
|
492
|
+
return quote(value);
|
|
493
|
+
}
|
|
494
|
+
if (Object.is(value, -0)) {
|
|
495
|
+
return "-0";
|
|
496
|
+
}
|
|
497
|
+
if (Object.is(value, Infinity)) {
|
|
498
|
+
return "1/0";
|
|
499
|
+
}
|
|
500
|
+
if (Object.is(value, -Infinity)) {
|
|
501
|
+
return "-1/0";
|
|
502
|
+
}
|
|
503
|
+
return value;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// src/tree/async.ts
|
|
507
|
+
async function serializePropertiesAsync(ctx, properties) {
|
|
628
508
|
const keys = Object.keys(properties);
|
|
629
509
|
const size = keys.length;
|
|
630
510
|
const keyNodes = new Array(size);
|
|
@@ -640,156 +520,111 @@ function serializePropertiesSync(ctx, properties) {
|
|
|
640
520
|
deferredSize++;
|
|
641
521
|
} else {
|
|
642
522
|
keyNodes[nodesSize] = key;
|
|
643
|
-
valueNodes[nodesSize] =
|
|
523
|
+
valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
|
|
644
524
|
nodesSize++;
|
|
645
525
|
}
|
|
646
526
|
}
|
|
647
527
|
for (let i = 0; i < deferredSize; i++) {
|
|
648
528
|
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
649
|
-
valueNodes[nodesSize + i] =
|
|
529
|
+
valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
|
|
650
530
|
}
|
|
651
531
|
return [keyNodes, valueNodes, size];
|
|
652
532
|
}
|
|
653
|
-
function
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
const
|
|
658
|
-
|
|
659
|
-
|
|
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);
|
|
545
|
+
}
|
|
660
546
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
return semiPrimitive;
|
|
547
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
548
|
+
nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
|
|
664
549
|
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
550
|
+
return [5 /* Set */, nodes, id];
|
|
551
|
+
}
|
|
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);
|
|
559
|
+
let deferredSize = 0;
|
|
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;
|
|
565
|
+
deferredSize++;
|
|
566
|
+
} else {
|
|
567
|
+
keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
|
|
568
|
+
valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
|
|
569
|
+
nodeSize++;
|
|
681
570
|
}
|
|
682
|
-
return [5 /* Set */, nodes, id];
|
|
683
571
|
}
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
572
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
573
|
+
keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
|
|
574
|
+
valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
|
|
575
|
+
}
|
|
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];
|
|
698
586
|
} else {
|
|
699
|
-
|
|
700
|
-
valueNodes[nodeSize] = generateTreeSync(ctx, value);
|
|
701
|
-
nodeSize++;
|
|
587
|
+
nodes[i] = await generateTreeAsync(ctx, current[i]);
|
|
702
588
|
}
|
|
703
589
|
}
|
|
704
|
-
for (let i = 0; i < deferredSize; i++) {
|
|
705
|
-
keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
|
|
706
|
-
valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
|
|
707
|
-
}
|
|
708
|
-
return [6 /* Map */, [keyNodes, valueNodes, len], id];
|
|
709
590
|
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
const deferred = new Array(size);
|
|
714
|
-
for (let i = 0; i < size; i++) {
|
|
715
|
-
if (i in current) {
|
|
716
|
-
if (isIterable(current[i])) {
|
|
717
|
-
deferred[i] = current[i];
|
|
718
|
-
} else {
|
|
719
|
-
nodes[i] = generateTreeSync(ctx, current[i]);
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
for (let i = 0; i < size; i++) {
|
|
724
|
-
if (i in deferred) {
|
|
725
|
-
nodes[i] = generateTreeSync(ctx, deferred[i]);
|
|
726
|
-
}
|
|
591
|
+
for (let i = 0; i < size; i++) {
|
|
592
|
+
if (i in deferred) {
|
|
593
|
+
nodes[i] = await generateTreeAsync(ctx, deferred[i]);
|
|
727
594
|
}
|
|
728
|
-
return [7 /* Array */, nodes, id];
|
|
729
|
-
}
|
|
730
|
-
if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
|
|
731
|
-
const options = getErrorOptions(current);
|
|
732
|
-
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
733
|
-
const errorsNode = generateTreeSync(ctx, current.errors);
|
|
734
|
-
return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
735
595
|
}
|
|
736
|
-
|
|
737
|
-
const options = getErrorOptions(current);
|
|
738
|
-
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
739
|
-
return [
|
|
740
|
-
11 /* Error */,
|
|
741
|
-
[getErrorConstructor(current), current.message, optionsNode],
|
|
742
|
-
id
|
|
743
|
-
];
|
|
744
|
-
}
|
|
745
|
-
if (isIterable(current)) {
|
|
746
|
-
assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
|
|
747
|
-
const options = getIterableOptions(current);
|
|
748
|
-
return [13 /* Iterable */, [
|
|
749
|
-
// Parse options first before the items
|
|
750
|
-
options ? serializePropertiesSync(ctx, options) : void 0,
|
|
751
|
-
generateTreeSync(ctx, Array.from(current))
|
|
752
|
-
], id];
|
|
753
|
-
}
|
|
754
|
-
const empty = current.constructor == null;
|
|
755
|
-
if (current.constructor === Object || empty) {
|
|
756
|
-
return [
|
|
757
|
-
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
758
|
-
serializePropertiesSync(ctx, current),
|
|
759
|
-
id
|
|
760
|
-
];
|
|
761
|
-
}
|
|
762
|
-
throw new Error("Unsupported value");
|
|
596
|
+
return nodes;
|
|
763
597
|
}
|
|
764
|
-
function
|
|
765
|
-
|
|
766
|
-
return [result, createRef(ctx, current), result[0] === 8 /* Object */];
|
|
598
|
+
async function generateArrayNode(ctx, current, id) {
|
|
599
|
+
return [7 /* Array */, await generateNodeList(ctx, current), id];
|
|
767
600
|
}
|
|
768
|
-
async function
|
|
769
|
-
const
|
|
770
|
-
const
|
|
771
|
-
const
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
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];
|
|
793
628
|
}
|
|
794
629
|
async function generateTreeAsync(ctx, current) {
|
|
795
630
|
if (isPrimitive(current)) {
|
|
@@ -812,93 +647,22 @@ async function generateTreeAsync(ctx, current) {
|
|
|
812
647
|
]);
|
|
813
648
|
}
|
|
814
649
|
if (constructorCheck(current, Set)) {
|
|
815
|
-
|
|
816
|
-
const len = current.size;
|
|
817
|
-
const nodes = new Array(len);
|
|
818
|
-
const deferred = new Array(len);
|
|
819
|
-
let deferredSize = 0;
|
|
820
|
-
let nodeSize = 0;
|
|
821
|
-
for (const item of current.keys()) {
|
|
822
|
-
if (isIterable(item)) {
|
|
823
|
-
deferred[deferredSize++] = item;
|
|
824
|
-
} else {
|
|
825
|
-
nodes[nodeSize++] = await generateTreeAsync(ctx, item);
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
for (let i = 0; i < deferredSize; i++) {
|
|
829
|
-
nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
|
|
830
|
-
}
|
|
831
|
-
return [5 /* Set */, nodes, id];
|
|
650
|
+
return generateSetNode(ctx, current, id);
|
|
832
651
|
}
|
|
833
652
|
if (constructorCheck(current, Map)) {
|
|
834
|
-
|
|
835
|
-
const len = current.size;
|
|
836
|
-
const keyNodes = new Array(len);
|
|
837
|
-
const valueNodes = new Array(len);
|
|
838
|
-
const deferredKey = new Array(len);
|
|
839
|
-
const deferredValue = new Array(len);
|
|
840
|
-
let deferredSize = 0;
|
|
841
|
-
let nodeSize = 0;
|
|
842
|
-
for (const [key, value] of current.entries()) {
|
|
843
|
-
if (isIterable(key) || isIterable(value)) {
|
|
844
|
-
deferredKey[deferredSize] = key;
|
|
845
|
-
deferredValue[deferredSize] = value;
|
|
846
|
-
deferredSize++;
|
|
847
|
-
} else {
|
|
848
|
-
keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
|
|
849
|
-
valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
|
|
850
|
-
nodeSize++;
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
for (let i = 0; i < deferredSize; i++) {
|
|
854
|
-
keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
|
|
855
|
-
valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
|
|
856
|
-
}
|
|
857
|
-
return [6 /* Map */, [keyNodes, valueNodes, len], id];
|
|
653
|
+
return generateMapNode(ctx, current, id);
|
|
858
654
|
}
|
|
859
655
|
if (Array.isArray(current)) {
|
|
860
|
-
|
|
861
|
-
const nodes = new Array(size);
|
|
862
|
-
const deferred = new Array(size);
|
|
863
|
-
for (let i = 0; i < size; i++) {
|
|
864
|
-
const item = current[i];
|
|
865
|
-
if (i in current) {
|
|
866
|
-
if (isIterable(item)) {
|
|
867
|
-
deferred[i] = item;
|
|
868
|
-
} else {
|
|
869
|
-
nodes[i] = await generateTreeAsync(ctx, item);
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
for (let i = 0; i < size; i++) {
|
|
874
|
-
if (i in deferred) {
|
|
875
|
-
nodes[i] = await generateTreeAsync(ctx, deferred[i]);
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
return [7 /* Array */, nodes, id];
|
|
656
|
+
return generateArrayNode(ctx, current, id);
|
|
879
657
|
}
|
|
880
658
|
if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
|
|
881
|
-
|
|
882
|
-
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
883
|
-
const errorsNode = await generateTreeAsync(ctx, current.errors);
|
|
884
|
-
return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
659
|
+
return generateAggregateErrorNode(ctx, current, id);
|
|
885
660
|
}
|
|
886
661
|
if (current instanceof Error) {
|
|
887
|
-
|
|
888
|
-
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
889
|
-
return [
|
|
890
|
-
11 /* Error */,
|
|
891
|
-
[getErrorConstructor(current), current.message, optionsNode],
|
|
892
|
-
id
|
|
893
|
-
];
|
|
662
|
+
return generateErrorNode(ctx, current, id);
|
|
894
663
|
}
|
|
895
664
|
if (isIterable(current)) {
|
|
896
|
-
|
|
897
|
-
const options = getIterableOptions(current);
|
|
898
|
-
return [13 /* Iterable */, [
|
|
899
|
-
options ? await serializePropertiesAsync(ctx, options) : void 0,
|
|
900
|
-
await generateTreeAsync(ctx, Array.from(current))
|
|
901
|
-
], id];
|
|
665
|
+
return generateIterableNode(ctx, current, id);
|
|
902
666
|
}
|
|
903
667
|
const empty = current.constructor == null;
|
|
904
668
|
if (current.constructor === Object || empty) {
|
|
@@ -914,33 +678,112 @@ async function parseAsync(ctx, current) {
|
|
|
914
678
|
const result = await generateTreeAsync(ctx, current);
|
|
915
679
|
return [result, createRef(ctx, current), result[0] === 8 /* Object */];
|
|
916
680
|
}
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
}
|
|
930
|
-
if (typeof value === "string") {
|
|
931
|
-
return quote(value);
|
|
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 "";
|
|
932
693
|
}
|
|
933
|
-
|
|
934
|
-
|
|
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;
|
|
935
730
|
}
|
|
936
|
-
|
|
937
|
-
|
|
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;
|
|
938
742
|
}
|
|
939
|
-
|
|
940
|
-
|
|
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}`;
|
|
941
751
|
}
|
|
942
752
|
return value;
|
|
943
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);
|
|
786
|
+
}
|
|
944
787
|
var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
|
|
945
788
|
function serializeAssignments(ctx, targetRef, [keys, values, size]) {
|
|
946
789
|
ctx.stack.push(targetRef);
|
|
@@ -968,6 +811,9 @@ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
|
|
|
968
811
|
return resolveAssignments(mainAssignments);
|
|
969
812
|
}
|
|
970
813
|
function serializeObject(ctx, sourceID, [keys, values, size]) {
|
|
814
|
+
if (size === 0) {
|
|
815
|
+
return "{}";
|
|
816
|
+
}
|
|
971
817
|
let result = "";
|
|
972
818
|
ctx.stack.push(sourceID);
|
|
973
819
|
for (let i = 0; i < size; i++) {
|
|
@@ -992,205 +838,227 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
|
|
|
992
838
|
ctx.stack.pop();
|
|
993
839
|
return `{${result.substring(0, result.length - 1)}}`;
|
|
994
840
|
}
|
|
995
|
-
function
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
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]));
|
|
1005
870
|
} else {
|
|
1006
|
-
ctx
|
|
1007
|
-
const result = serializeTree(ctx, value);
|
|
1008
|
-
ctx.stack.pop();
|
|
1009
|
-
serialized = `Promise.resolve(${result})`;
|
|
871
|
+
result += `${serializeTree(ctx, item)},`;
|
|
1010
872
|
}
|
|
1011
|
-
return assignRef(ctx, id, serialized);
|
|
1012
873
|
}
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
return assignRef(ctx, id, `new Date("${value}")`);
|
|
1017
|
-
case 4 /* RegExp */:
|
|
1018
|
-
return assignRef(ctx, id, value);
|
|
1019
|
-
case 15 /* BigIntTypedArray */:
|
|
1020
|
-
case 14 /* TypedArray */: {
|
|
1021
|
-
let args = `[${value[1]}]`;
|
|
1022
|
-
if (value[2] !== 0) {
|
|
1023
|
-
args += `,${value[2]}`;
|
|
1024
|
-
}
|
|
1025
|
-
return assignRef(ctx, id, `new ${value[0]}(${args})`);
|
|
874
|
+
ctx.stack.pop();
|
|
875
|
+
if (result) {
|
|
876
|
+
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1026
877
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
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;
|
|
1044
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)}],`;
|
|
1045
908
|
}
|
|
1046
|
-
return assignRef(ctx, id, serialized);
|
|
1047
909
|
}
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
if (size) {
|
|
1052
|
-
let result = "";
|
|
1053
|
-
ctx.stack.push(id);
|
|
1054
|
-
for (let i = 0; i < size; i++) {
|
|
1055
|
-
const key = value[0][i];
|
|
1056
|
-
const val = value[1][i];
|
|
1057
|
-
if (isReferenceInStack(ctx, key)) {
|
|
1058
|
-
const keyRef = getRefParam(ctx, key[1]);
|
|
1059
|
-
if (isReferenceInStack(ctx, val)) {
|
|
1060
|
-
const valueRef = getRefParam(ctx, val[1]);
|
|
1061
|
-
createMapSet(ctx, id, keyRef, valueRef);
|
|
1062
|
-
} else {
|
|
1063
|
-
const parent = ctx.stack;
|
|
1064
|
-
ctx.stack = [];
|
|
1065
|
-
createMapSet(ctx, id, keyRef, serializeTree(ctx, val));
|
|
1066
|
-
ctx.stack = parent;
|
|
1067
|
-
}
|
|
1068
|
-
} else if (isReferenceInStack(ctx, val)) {
|
|
1069
|
-
const valueRef = getRefParam(ctx, val[1]);
|
|
1070
|
-
const parent = ctx.stack;
|
|
1071
|
-
ctx.stack = [];
|
|
1072
|
-
createMapSet(ctx, id, serializeTree(ctx, key), valueRef);
|
|
1073
|
-
ctx.stack = parent;
|
|
1074
|
-
} else {
|
|
1075
|
-
result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
|
-
ctx.stack.pop();
|
|
1079
|
-
if (result) {
|
|
1080
|
-
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1081
|
-
}
|
|
1082
|
-
}
|
|
1083
|
-
return assignRef(ctx, id, serialized);
|
|
910
|
+
ctx.stack.pop();
|
|
911
|
+
if (result) {
|
|
912
|
+
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1084
913
|
}
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
}
|
|
1101
|
-
} 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) {
|
|
1102
929
|
values += ",";
|
|
1103
930
|
}
|
|
1104
931
|
}
|
|
1105
|
-
|
|
1106
|
-
|
|
932
|
+
} else {
|
|
933
|
+
values += ",";
|
|
1107
934
|
}
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
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})`;
|
|
1124
964
|
}
|
|
1125
|
-
return assignRef(ctx, id, serialized);
|
|
1126
965
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
}
|
|
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})`;
|
|
1141
980
|
}
|
|
1142
|
-
return assignRef(ctx, id, serialized);
|
|
1143
981
|
}
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
}
|
|
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})`;
|
|
1174
1012
|
}
|
|
1175
|
-
return assignRef(ctx, id, serialized);
|
|
1176
1013
|
}
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
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})`;
|
|
1193
1027
|
}
|
|
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);
|
|
1194
1062
|
case 8 /* Object */:
|
|
1195
1063
|
return assignRef(ctx, id, serializeObject(ctx, id, value));
|
|
1196
1064
|
default:
|
|
@@ -1198,6 +1066,170 @@ function serializeTree(ctx, [type, value, id]) {
|
|
|
1198
1066
|
}
|
|
1199
1067
|
}
|
|
1200
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
|
+
|
|
1201
1233
|
// src/index.ts
|
|
1202
1234
|
function finalize(ctx, rootID, isObject, result) {
|
|
1203
1235
|
if (ctx.vars.length) {
|