seroval 0.4.0-alpha.4 → 0.4.0-alpha.6
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 +643 -636
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +2 -2
- package/dist/esm/development/index.mjs +643 -636
- 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 +252 -0
- package/src/tree/serialize.ts +558 -0
- package/src/tree/shared.ts +177 -0
- package/src/tree/sync.ts +234 -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,195 +469,152 @@ 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 generateDictionary(ctx, properties) {
|
|
628
508
|
const keys = Object.keys(properties);
|
|
629
|
-
const
|
|
630
|
-
const
|
|
631
|
-
const valueNodes = new Array(size);
|
|
632
|
-
const deferredKeys = new Array(size);
|
|
633
|
-
const deferredValues = new Array(size);
|
|
634
|
-
let deferredSize = 0;
|
|
635
|
-
let nodesSize = 0;
|
|
509
|
+
const nodes = {};
|
|
510
|
+
const deferred = {};
|
|
636
511
|
for (const key of keys) {
|
|
637
512
|
if (isIterable(properties[key])) {
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
513
|
+
deferred[key] = properties[key];
|
|
514
|
+
} else {
|
|
515
|
+
nodes[key] = await generateTreeAsync(ctx, properties[key]);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
for (const key of Object.keys(deferred)) {
|
|
519
|
+
nodes[key] = await generateTreeAsync(ctx, deferred[key]);
|
|
520
|
+
}
|
|
521
|
+
return nodes;
|
|
522
|
+
}
|
|
523
|
+
async function generateSetNode(ctx, current, id) {
|
|
524
|
+
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
525
|
+
const len = current.size;
|
|
526
|
+
const nodes = new Array(len);
|
|
527
|
+
const deferred = new Array(len);
|
|
528
|
+
let deferredSize = 0;
|
|
529
|
+
let nodeSize = 0;
|
|
530
|
+
for (const item of current.keys()) {
|
|
531
|
+
if (isIterable(item)) {
|
|
532
|
+
deferred[deferredSize++] = item;
|
|
641
533
|
} else {
|
|
642
|
-
|
|
643
|
-
valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
|
|
644
|
-
nodesSize++;
|
|
534
|
+
nodes[nodeSize++] = await generateTreeAsync(ctx, item);
|
|
645
535
|
}
|
|
646
536
|
}
|
|
647
537
|
for (let i = 0; i < deferredSize; i++) {
|
|
648
|
-
|
|
649
|
-
valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
|
|
538
|
+
nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
|
|
650
539
|
}
|
|
651
|
-
return [
|
|
540
|
+
return [5 /* Set */, nodes, id];
|
|
652
541
|
}
|
|
653
|
-
function
|
|
654
|
-
|
|
655
|
-
|
|
542
|
+
async function generateMapNode(ctx, current, id) {
|
|
543
|
+
assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
|
|
544
|
+
const len = current.size;
|
|
545
|
+
const keyNodes = new Array(len);
|
|
546
|
+
const valueNodes = new Array(len);
|
|
547
|
+
const deferredKey = new Array(len);
|
|
548
|
+
const deferredValue = new Array(len);
|
|
549
|
+
let deferredSize = 0;
|
|
550
|
+
let nodeSize = 0;
|
|
551
|
+
for (const [key, value] of current.entries()) {
|
|
552
|
+
if (isIterable(key) || isIterable(value)) {
|
|
553
|
+
deferredKey[deferredSize] = key;
|
|
554
|
+
deferredValue[deferredSize] = value;
|
|
555
|
+
deferredSize++;
|
|
556
|
+
} else {
|
|
557
|
+
keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
|
|
558
|
+
valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
|
|
559
|
+
nodeSize++;
|
|
560
|
+
}
|
|
656
561
|
}
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
562
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
563
|
+
keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
|
|
564
|
+
valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
|
|
660
565
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
let nodeSize = 0;
|
|
672
|
-
for (const item of current.keys()) {
|
|
673
|
-
if (isIterable(item)) {
|
|
674
|
-
deferred[deferredSize++] = item;
|
|
566
|
+
return [6 /* Map */, [keyNodes, valueNodes, len], id];
|
|
567
|
+
}
|
|
568
|
+
async function generateNodeList(ctx, current) {
|
|
569
|
+
const size = current.length;
|
|
570
|
+
const nodes = new Array(size);
|
|
571
|
+
const deferred = new Array(size);
|
|
572
|
+
for (let i = 0; i < size; i++) {
|
|
573
|
+
if (i in current) {
|
|
574
|
+
if (isIterable(current[i])) {
|
|
575
|
+
deferred[i] = current[i];
|
|
675
576
|
} else {
|
|
676
|
-
nodes[
|
|
577
|
+
nodes[i] = await generateTreeAsync(ctx, current[i]);
|
|
677
578
|
}
|
|
678
579
|
}
|
|
679
|
-
for (let i = 0; i < deferredSize; i++) {
|
|
680
|
-
nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
|
|
681
|
-
}
|
|
682
|
-
return [5 /* Set */, nodes, id];
|
|
683
580
|
}
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
const keyNodes = new Array(len);
|
|
688
|
-
const valueNodes = new Array(len);
|
|
689
|
-
const deferredKey = new Array(len);
|
|
690
|
-
const deferredValue = new Array(len);
|
|
691
|
-
let deferredSize = 0;
|
|
692
|
-
let nodeSize = 0;
|
|
693
|
-
for (const [key, value] of current.entries()) {
|
|
694
|
-
if (isIterable(key) || isIterable(value)) {
|
|
695
|
-
deferredKey[deferredSize] = key;
|
|
696
|
-
deferredValue[deferredSize] = value;
|
|
697
|
-
deferredSize++;
|
|
698
|
-
} else {
|
|
699
|
-
keyNodes[nodeSize] = generateTreeSync(ctx, key);
|
|
700
|
-
valueNodes[nodeSize] = generateTreeSync(ctx, value);
|
|
701
|
-
nodeSize++;
|
|
702
|
-
}
|
|
703
|
-
}
|
|
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
|
-
}
|
|
710
|
-
if (Array.isArray(current)) {
|
|
711
|
-
const size = current.length;
|
|
712
|
-
const nodes = new Array(size);
|
|
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
|
-
}
|
|
581
|
+
for (let i = 0; i < size; i++) {
|
|
582
|
+
if (i in deferred) {
|
|
583
|
+
nodes[i] = await generateTreeAsync(ctx, deferred[i]);
|
|
727
584
|
}
|
|
728
|
-
return [7 /* Array */, nodes, id];
|
|
729
585
|
}
|
|
730
|
-
|
|
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
|
-
}
|
|
736
|
-
if (current instanceof Error) {
|
|
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");
|
|
586
|
+
return nodes;
|
|
763
587
|
}
|
|
764
|
-
function
|
|
765
|
-
|
|
766
|
-
return [result, createRef(ctx, current), result[0] === 8 /* Object */];
|
|
588
|
+
async function generateArrayNode(ctx, current, id) {
|
|
589
|
+
return [7 /* Array */, await generateNodeList(ctx, current), id];
|
|
767
590
|
}
|
|
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
|
-
|
|
591
|
+
async function generateAggregateErrorNode(ctx, current, id) {
|
|
592
|
+
const options = getErrorOptions(current);
|
|
593
|
+
const optionsNode = options ? await generateDictionary(ctx, options) : void 0;
|
|
594
|
+
const errorsNode = await generateTreeAsync(ctx, current.errors);
|
|
595
|
+
return [
|
|
596
|
+
12 /* AggregateError */,
|
|
597
|
+
[current.message, optionsNode, errorsNode],
|
|
598
|
+
id
|
|
599
|
+
];
|
|
600
|
+
}
|
|
601
|
+
async function generateErrorNode(ctx, current, id) {
|
|
602
|
+
const options = getErrorOptions(current);
|
|
603
|
+
const optionsNode = options ? await generateDictionary(ctx, options) : void 0;
|
|
604
|
+
return [
|
|
605
|
+
11 /* Error */,
|
|
606
|
+
[getErrorConstructor(current), current.message, optionsNode],
|
|
607
|
+
id
|
|
608
|
+
];
|
|
609
|
+
}
|
|
610
|
+
async function generateIterableNode(ctx, current, id) {
|
|
611
|
+
assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
|
|
612
|
+
const options = getIterableOptions(current);
|
|
613
|
+
return [13 /* Iterable */, [
|
|
614
|
+
// Parse options first before the items
|
|
615
|
+
options ? await generateDictionary(ctx, options) : void 0,
|
|
616
|
+
await generateNodeList(ctx, Array.from(current))
|
|
617
|
+
], id];
|
|
793
618
|
}
|
|
794
619
|
async function generateTreeAsync(ctx, current) {
|
|
795
620
|
if (isPrimitive(current)) {
|
|
@@ -812,99 +637,28 @@ async function generateTreeAsync(ctx, current) {
|
|
|
812
637
|
]);
|
|
813
638
|
}
|
|
814
639
|
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];
|
|
640
|
+
return generateSetNode(ctx, current, id);
|
|
832
641
|
}
|
|
833
642
|
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];
|
|
643
|
+
return generateMapNode(ctx, current, id);
|
|
858
644
|
}
|
|
859
645
|
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];
|
|
646
|
+
return generateArrayNode(ctx, current, id);
|
|
879
647
|
}
|
|
880
648
|
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];
|
|
649
|
+
return generateAggregateErrorNode(ctx, current, id);
|
|
885
650
|
}
|
|
886
651
|
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
|
-
];
|
|
652
|
+
return generateErrorNode(ctx, current, id);
|
|
894
653
|
}
|
|
895
654
|
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];
|
|
655
|
+
return generateIterableNode(ctx, current, id);
|
|
902
656
|
}
|
|
903
657
|
const empty = current.constructor == null;
|
|
904
658
|
if (current.constructor === Object || empty) {
|
|
905
659
|
return [
|
|
906
660
|
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
907
|
-
await
|
|
661
|
+
await generateDictionary(ctx, current),
|
|
908
662
|
id
|
|
909
663
|
];
|
|
910
664
|
}
|
|
@@ -914,43 +668,121 @@ async function parseAsync(ctx, current) {
|
|
|
914
668
|
const result = await generateTreeAsync(ctx, current);
|
|
915
669
|
return [result, createRef(ctx, current), result[0] === 8 /* Object */];
|
|
916
670
|
}
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
}
|
|
930
|
-
if (typeof value === "string") {
|
|
931
|
-
return quote(value);
|
|
671
|
+
|
|
672
|
+
// src/tree/serialize.ts
|
|
673
|
+
function getAssignmentExpression(assignment) {
|
|
674
|
+
switch (assignment.type) {
|
|
675
|
+
case "index":
|
|
676
|
+
return `${assignment.source}=${assignment.value}`;
|
|
677
|
+
case "map":
|
|
678
|
+
return `${assignment.source}.set(${assignment.key},${assignment.value})`;
|
|
679
|
+
case "set":
|
|
680
|
+
return `${assignment.source}.add(${assignment.value})`;
|
|
681
|
+
default:
|
|
682
|
+
return "";
|
|
932
683
|
}
|
|
933
|
-
|
|
934
|
-
|
|
684
|
+
}
|
|
685
|
+
function mergeAssignments(assignments) {
|
|
686
|
+
const newAssignments = [];
|
|
687
|
+
let current = assignments[0];
|
|
688
|
+
let prev = current;
|
|
689
|
+
for (let i = 1, len = assignments.length; i < len; i++) {
|
|
690
|
+
const item = assignments[i];
|
|
691
|
+
if (item.type === prev.type) {
|
|
692
|
+
if (item.type === "index" && item.value === prev.value) {
|
|
693
|
+
current = {
|
|
694
|
+
type: "index",
|
|
695
|
+
source: item.source,
|
|
696
|
+
value: getAssignmentExpression(current)
|
|
697
|
+
};
|
|
698
|
+
} else if (item.type === "map" && item.source === prev.source) {
|
|
699
|
+
current = {
|
|
700
|
+
type: "map",
|
|
701
|
+
source: getAssignmentExpression(current),
|
|
702
|
+
key: item.key,
|
|
703
|
+
value: item.value
|
|
704
|
+
};
|
|
705
|
+
} else if (item.type === "set" && item.source === prev.source) {
|
|
706
|
+
current = {
|
|
707
|
+
type: "set",
|
|
708
|
+
source: getAssignmentExpression(current),
|
|
709
|
+
value: item.value
|
|
710
|
+
};
|
|
711
|
+
} else {
|
|
712
|
+
newAssignments.push(current);
|
|
713
|
+
current = item;
|
|
714
|
+
}
|
|
715
|
+
} else {
|
|
716
|
+
newAssignments.push(current);
|
|
717
|
+
current = item;
|
|
718
|
+
}
|
|
719
|
+
prev = item;
|
|
935
720
|
}
|
|
936
|
-
|
|
937
|
-
|
|
721
|
+
newAssignments.push(current);
|
|
722
|
+
return newAssignments;
|
|
723
|
+
}
|
|
724
|
+
function resolveAssignments(assignments) {
|
|
725
|
+
if (assignments.length) {
|
|
726
|
+
let result = "";
|
|
727
|
+
const merged = mergeAssignments(assignments);
|
|
728
|
+
for (let i = 0, len = merged.length; i < len; i++) {
|
|
729
|
+
result += `${getAssignmentExpression(merged[i])},`;
|
|
730
|
+
}
|
|
731
|
+
return result;
|
|
938
732
|
}
|
|
939
|
-
|
|
940
|
-
|
|
733
|
+
return void 0;
|
|
734
|
+
}
|
|
735
|
+
function resolvePatches(ctx) {
|
|
736
|
+
return resolveAssignments(ctx.assignments);
|
|
737
|
+
}
|
|
738
|
+
function assignRef(ctx, index, value) {
|
|
739
|
+
if (ctx.markedRefs.has(index)) {
|
|
740
|
+
return `${getRefParam(ctx, index)}=${value}`;
|
|
941
741
|
}
|
|
942
742
|
return value;
|
|
943
743
|
}
|
|
744
|
+
function createAssignment(ctx, source, value) {
|
|
745
|
+
ctx.assignments.push({
|
|
746
|
+
type: "index",
|
|
747
|
+
source,
|
|
748
|
+
value
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
function createSetAdd(ctx, ref, value) {
|
|
752
|
+
markRef(ctx, ref);
|
|
753
|
+
ctx.assignments.push({
|
|
754
|
+
type: "set",
|
|
755
|
+
source: getRefParam(ctx, ref),
|
|
756
|
+
value
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
function createMapSet(ctx, ref, key, value) {
|
|
760
|
+
markRef(ctx, ref);
|
|
761
|
+
ctx.assignments.push({
|
|
762
|
+
type: "map",
|
|
763
|
+
source: getRefParam(ctx, ref),
|
|
764
|
+
key,
|
|
765
|
+
value
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
function createArrayAssign(ctx, ref, index, value) {
|
|
769
|
+
markRef(ctx, ref);
|
|
770
|
+
createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
|
|
771
|
+
}
|
|
772
|
+
function createObjectAssign(ctx, ref, key, value, computed) {
|
|
773
|
+
markRef(ctx, ref);
|
|
774
|
+
const member = computed ? `[${key}]` : `.${key}`;
|
|
775
|
+
createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
|
|
776
|
+
}
|
|
944
777
|
var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
|
|
945
|
-
function serializeAssignments(ctx, targetRef,
|
|
778
|
+
function serializeAssignments(ctx, targetRef, record) {
|
|
946
779
|
ctx.stack.push(targetRef);
|
|
947
780
|
const mainAssignments = [];
|
|
948
|
-
for (
|
|
781
|
+
for (const key of Object.keys(record)) {
|
|
949
782
|
const parentStack = ctx.stack;
|
|
950
783
|
ctx.stack = [];
|
|
951
|
-
const refParam = serializeTree(ctx,
|
|
784
|
+
const refParam = serializeTree(ctx, record[key]);
|
|
952
785
|
ctx.stack = parentStack;
|
|
953
|
-
const key = keys[i];
|
|
954
786
|
const check = Number(key);
|
|
955
787
|
const parentAssignment = ctx.assignments;
|
|
956
788
|
ctx.assignments = mainAssignments;
|
|
@@ -967,12 +799,11 @@ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
|
|
|
967
799
|
ctx.stack.pop();
|
|
968
800
|
return resolveAssignments(mainAssignments);
|
|
969
801
|
}
|
|
970
|
-
function
|
|
802
|
+
function serializeDictionary(ctx, sourceID, record) {
|
|
971
803
|
let result = "";
|
|
972
804
|
ctx.stack.push(sourceID);
|
|
973
|
-
for (
|
|
974
|
-
const
|
|
975
|
-
const val = values[i];
|
|
805
|
+
for (const key of Object.keys(record)) {
|
|
806
|
+
const val = record[key];
|
|
976
807
|
const check = Number(key);
|
|
977
808
|
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
978
809
|
const validKey = isIdentifier ? key : quote(key);
|
|
@@ -992,212 +823,388 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
|
|
|
992
823
|
ctx.stack.pop();
|
|
993
824
|
return `{${result.substring(0, result.length - 1)}}`;
|
|
994
825
|
}
|
|
995
|
-
function
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
826
|
+
function serializePromise(ctx, value, id) {
|
|
827
|
+
let serialized;
|
|
828
|
+
if (isReferenceInStack(ctx, value)) {
|
|
829
|
+
serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
|
|
830
|
+
} else {
|
|
831
|
+
ctx.stack.push(id);
|
|
832
|
+
const result = serializeTree(ctx, value);
|
|
833
|
+
ctx.stack.pop();
|
|
834
|
+
serialized = `Promise.resolve(${result})`;
|
|
835
|
+
}
|
|
836
|
+
return assignRef(ctx, id, serialized);
|
|
837
|
+
}
|
|
838
|
+
function serializeTypedArray(ctx, constructor, init, byteOffset, id) {
|
|
839
|
+
let args = `[${init}]`;
|
|
840
|
+
if (byteOffset !== 0) {
|
|
841
|
+
args += `,${byteOffset}`;
|
|
842
|
+
}
|
|
843
|
+
return assignRef(ctx, id, `new ${constructor}(${args})`);
|
|
844
|
+
}
|
|
845
|
+
function serializeSet(ctx, value, id) {
|
|
846
|
+
let serialized = "new Set";
|
|
847
|
+
const size = value.length;
|
|
848
|
+
if (size) {
|
|
849
|
+
let result = "";
|
|
850
|
+
ctx.stack.push(id);
|
|
851
|
+
for (let i = 0; i < size; i++) {
|
|
852
|
+
const item = value[i];
|
|
853
|
+
if (isReferenceInStack(ctx, item)) {
|
|
854
|
+
createSetAdd(ctx, id, getRefParam(ctx, item[1]));
|
|
1005
855
|
} else {
|
|
1006
|
-
ctx
|
|
1007
|
-
const result = serializeTree(ctx, value);
|
|
1008
|
-
ctx.stack.pop();
|
|
1009
|
-
serialized = `Promise.resolve(${result})`;
|
|
856
|
+
result += `${serializeTree(ctx, item)},`;
|
|
1010
857
|
}
|
|
1011
|
-
return assignRef(ctx, id, serialized);
|
|
1012
858
|
}
|
|
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})`);
|
|
859
|
+
ctx.stack.pop();
|
|
860
|
+
if (result) {
|
|
861
|
+
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1026
862
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
863
|
+
}
|
|
864
|
+
return assignRef(ctx, id, serialized);
|
|
865
|
+
}
|
|
866
|
+
function serializeMap(ctx, keys, values, size, id) {
|
|
867
|
+
let serialized = "new Map";
|
|
868
|
+
if (size) {
|
|
869
|
+
let result = "";
|
|
870
|
+
ctx.stack.push(id);
|
|
871
|
+
for (let i = 0; i < size; i++) {
|
|
872
|
+
const key = keys[i];
|
|
873
|
+
const val = values[i];
|
|
874
|
+
if (isReferenceInStack(ctx, key)) {
|
|
875
|
+
const keyRef = getRefParam(ctx, key[1]);
|
|
876
|
+
if (isReferenceInStack(ctx, val)) {
|
|
877
|
+
const valueRef = getRefParam(ctx, val[1]);
|
|
878
|
+
createMapSet(ctx, id, keyRef, valueRef);
|
|
879
|
+
} else {
|
|
880
|
+
const parent = ctx.stack;
|
|
881
|
+
ctx.stack = [];
|
|
882
|
+
createMapSet(ctx, id, keyRef, serializeTree(ctx, val));
|
|
883
|
+
ctx.stack = parent;
|
|
1044
884
|
}
|
|
885
|
+
} else if (isReferenceInStack(ctx, val)) {
|
|
886
|
+
const valueRef = getRefParam(ctx, val[1]);
|
|
887
|
+
const parent = ctx.stack;
|
|
888
|
+
ctx.stack = [];
|
|
889
|
+
createMapSet(ctx, id, serializeTree(ctx, key), valueRef);
|
|
890
|
+
ctx.stack = parent;
|
|
891
|
+
} else {
|
|
892
|
+
result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
|
|
1045
893
|
}
|
|
1046
|
-
return assignRef(ctx, id, serialized);
|
|
1047
894
|
}
|
|
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);
|
|
895
|
+
ctx.stack.pop();
|
|
896
|
+
if (result) {
|
|
897
|
+
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1084
898
|
}
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
}
|
|
1101
|
-
} else {
|
|
899
|
+
}
|
|
900
|
+
return assignRef(ctx, id, serialized);
|
|
901
|
+
}
|
|
902
|
+
function serializeNodeList(ctx, value, id) {
|
|
903
|
+
const size = value.length;
|
|
904
|
+
let values = "";
|
|
905
|
+
for (let i = 0; i < size; i++) {
|
|
906
|
+
const item = value[i];
|
|
907
|
+
if (item) {
|
|
908
|
+
if (isReferenceInStack(ctx, item)) {
|
|
909
|
+
createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
|
|
910
|
+
values += ",";
|
|
911
|
+
} else {
|
|
912
|
+
values += serializeTree(ctx, item);
|
|
913
|
+
if (i < size - 1) {
|
|
1102
914
|
values += ",";
|
|
1103
915
|
}
|
|
1104
916
|
}
|
|
1105
|
-
|
|
1106
|
-
|
|
917
|
+
} else {
|
|
918
|
+
values += ",";
|
|
1107
919
|
}
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
920
|
+
}
|
|
921
|
+
return `[${values}]`;
|
|
922
|
+
}
|
|
923
|
+
function serializeArray(ctx, value, id) {
|
|
924
|
+
ctx.stack.push(id);
|
|
925
|
+
const result = serializeNodeList(ctx, value, id);
|
|
926
|
+
ctx.stack.pop();
|
|
927
|
+
return assignRef(ctx, id, result);
|
|
928
|
+
}
|
|
929
|
+
function serializeWithObjectAssign(ctx, value, id, serialized) {
|
|
930
|
+
const fields = serializeDictionary(ctx, id, value);
|
|
931
|
+
if (fields !== "{}") {
|
|
932
|
+
return `Object.assign(${serialized},${fields})`;
|
|
933
|
+
}
|
|
934
|
+
return serialized;
|
|
935
|
+
}
|
|
936
|
+
function serializeAggregateError(ctx, message, options, errors, id) {
|
|
937
|
+
ctx.stack.push(id);
|
|
938
|
+
let serialized = `new AggregateError(${serializeTree(ctx, errors)},${quote(message)})`;
|
|
939
|
+
ctx.stack.pop();
|
|
940
|
+
if (options) {
|
|
941
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
942
|
+
serialized = serializeWithObjectAssign(ctx, options, id, serialized);
|
|
943
|
+
} else {
|
|
944
|
+
markRef(ctx, id);
|
|
945
|
+
const assignments = serializeAssignments(ctx, id, options);
|
|
946
|
+
if (assignments) {
|
|
947
|
+
const ref = getRefParam(ctx, id);
|
|
948
|
+
return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
|
|
1124
949
|
}
|
|
1125
|
-
return assignRef(ctx, id, serialized);
|
|
1126
950
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
}
|
|
951
|
+
}
|
|
952
|
+
return assignRef(ctx, id, serialized);
|
|
953
|
+
}
|
|
954
|
+
function serializeError(ctx, constructor, message, options, id) {
|
|
955
|
+
let serialized = `new ${constructor}(${quote(message)})`;
|
|
956
|
+
if (options) {
|
|
957
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
958
|
+
serialized = serializeWithObjectAssign(ctx, options, id, serialized);
|
|
959
|
+
} else {
|
|
960
|
+
markRef(ctx, id);
|
|
961
|
+
const assignments = serializeAssignments(ctx, id, options);
|
|
962
|
+
if (assignments) {
|
|
963
|
+
const ref = getRefParam(ctx, id);
|
|
964
|
+
return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
|
|
1141
965
|
}
|
|
1142
|
-
return assignRef(ctx, id, serialized);
|
|
1143
966
|
}
|
|
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
|
-
}
|
|
967
|
+
}
|
|
968
|
+
return assignRef(ctx, id, serialized);
|
|
969
|
+
}
|
|
970
|
+
function serializeIterable(ctx, options, items, id) {
|
|
971
|
+
const parent = ctx.stack;
|
|
972
|
+
ctx.stack = [];
|
|
973
|
+
const values = serializeNodeList(ctx, items, id);
|
|
974
|
+
ctx.stack = parent;
|
|
975
|
+
let serialized;
|
|
976
|
+
if (ctx.features & 2 /* ArrayPrototypeValues */) {
|
|
977
|
+
serialized = `${values}.values()`;
|
|
978
|
+
} else {
|
|
979
|
+
serialized = `${values}[Symbol.iterator]()`;
|
|
980
|
+
}
|
|
981
|
+
if (ctx.features & 4 /* ArrowFunction */) {
|
|
982
|
+
serialized = `{[Symbol.iterator]:()=>${serialized}}`;
|
|
983
|
+
} else if (ctx.features & 32 /* MethodShorthand */) {
|
|
984
|
+
serialized = `{[Symbol.iterator](){return ${serialized}}}`;
|
|
985
|
+
} else {
|
|
986
|
+
serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
|
|
987
|
+
}
|
|
988
|
+
if (options) {
|
|
989
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
990
|
+
serialized = serializeWithObjectAssign(ctx, options, id, serialized);
|
|
991
|
+
} else {
|
|
992
|
+
markRef(ctx, id);
|
|
993
|
+
const assignments = serializeAssignments(ctx, id, options);
|
|
994
|
+
if (assignments) {
|
|
995
|
+
const ref = getRefParam(ctx, id);
|
|
996
|
+
return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
|
|
1174
997
|
}
|
|
1175
|
-
return assignRef(ctx, id, serialized);
|
|
1176
998
|
}
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
return assignRef(ctx, id, serialized);
|
|
999
|
+
}
|
|
1000
|
+
return assignRef(ctx, id, serialized);
|
|
1001
|
+
}
|
|
1002
|
+
function serializeNullConstructor(ctx, value, id) {
|
|
1003
|
+
let serialized = "Object.create(null)";
|
|
1004
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
1005
|
+
serialized = serializeWithObjectAssign(ctx, value, id, serialized);
|
|
1006
|
+
} else {
|
|
1007
|
+
markRef(ctx, id);
|
|
1008
|
+
const assignments = serializeAssignments(ctx, id, value);
|
|
1009
|
+
if (assignments) {
|
|
1010
|
+
const ref = getRefParam(ctx, id);
|
|
1011
|
+
return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
|
|
1193
1012
|
}
|
|
1013
|
+
}
|
|
1014
|
+
return assignRef(ctx, id, serialized);
|
|
1015
|
+
}
|
|
1016
|
+
function serializeTree(ctx, [type, value, id]) {
|
|
1017
|
+
switch (type) {
|
|
1018
|
+
case 0 /* Primitive */:
|
|
1019
|
+
return String(value);
|
|
1020
|
+
case 2 /* Reference */:
|
|
1021
|
+
return getRefParam(ctx, value);
|
|
1022
|
+
case 10 /* Promise */:
|
|
1023
|
+
return serializePromise(ctx, value, id);
|
|
1024
|
+
case 1 /* BigInt */:
|
|
1025
|
+
return value;
|
|
1026
|
+
case 3 /* Date */:
|
|
1027
|
+
return assignRef(ctx, id, `new Date("${value}")`);
|
|
1028
|
+
case 4 /* RegExp */:
|
|
1029
|
+
return assignRef(ctx, id, value);
|
|
1030
|
+
case 15 /* BigIntTypedArray */:
|
|
1031
|
+
case 14 /* TypedArray */:
|
|
1032
|
+
return serializeTypedArray(ctx, value[0], value[1], value[2], id);
|
|
1033
|
+
case 5 /* Set */:
|
|
1034
|
+
return serializeSet(ctx, value, id);
|
|
1035
|
+
case 6 /* Map */:
|
|
1036
|
+
return serializeMap(ctx, value[0], value[1], value[2], id);
|
|
1037
|
+
case 7 /* Array */:
|
|
1038
|
+
return serializeArray(ctx, value, id);
|
|
1039
|
+
case 12 /* AggregateError */:
|
|
1040
|
+
return serializeAggregateError(ctx, value[0], value[1], value[2], id);
|
|
1041
|
+
case 11 /* Error */:
|
|
1042
|
+
return serializeError(ctx, value[0], value[1], value[2], id);
|
|
1043
|
+
case 13 /* Iterable */:
|
|
1044
|
+
return serializeIterable(ctx, value[0], value[1], id);
|
|
1045
|
+
case 9 /* NullConstructor */:
|
|
1046
|
+
return serializeNullConstructor(ctx, value, id);
|
|
1194
1047
|
case 8 /* Object */:
|
|
1195
|
-
return assignRef(ctx, id,
|
|
1048
|
+
return assignRef(ctx, id, serializeDictionary(ctx, id, value));
|
|
1196
1049
|
default:
|
|
1197
1050
|
throw new Error("Unsupported type");
|
|
1198
1051
|
}
|
|
1199
1052
|
}
|
|
1200
1053
|
|
|
1054
|
+
// src/tree/sync.ts
|
|
1055
|
+
function generateDictionary2(ctx, properties) {
|
|
1056
|
+
const keys = Object.keys(properties);
|
|
1057
|
+
const nodes = {};
|
|
1058
|
+
const deferred = {};
|
|
1059
|
+
for (const key of keys) {
|
|
1060
|
+
if (isIterable(properties[key])) {
|
|
1061
|
+
deferred[key] = properties[key];
|
|
1062
|
+
} else {
|
|
1063
|
+
nodes[key] = generateTreeSync(ctx, properties[key]);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
for (const key of Object.keys(deferred)) {
|
|
1067
|
+
nodes[key] = generateTreeSync(ctx, deferred[key]);
|
|
1068
|
+
}
|
|
1069
|
+
return nodes;
|
|
1070
|
+
}
|
|
1071
|
+
function generateSetNode2(ctx, current, id) {
|
|
1072
|
+
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
1073
|
+
const len = current.size;
|
|
1074
|
+
const nodes = new Array(len);
|
|
1075
|
+
const deferred = new Array(len);
|
|
1076
|
+
let deferredSize = 0;
|
|
1077
|
+
let nodeSize = 0;
|
|
1078
|
+
for (const item of current.keys()) {
|
|
1079
|
+
if (isIterable(item)) {
|
|
1080
|
+
deferred[deferredSize++] = item;
|
|
1081
|
+
} else {
|
|
1082
|
+
nodes[nodeSize++] = generateTreeSync(ctx, item);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1086
|
+
nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
|
|
1087
|
+
}
|
|
1088
|
+
return [5 /* Set */, nodes, id];
|
|
1089
|
+
}
|
|
1090
|
+
function generateMapNode2(ctx, current, id) {
|
|
1091
|
+
assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
|
|
1092
|
+
const len = current.size;
|
|
1093
|
+
const keyNodes = new Array(len);
|
|
1094
|
+
const valueNodes = new Array(len);
|
|
1095
|
+
const deferredKey = new Array(len);
|
|
1096
|
+
const deferredValue = new Array(len);
|
|
1097
|
+
let deferredSize = 0;
|
|
1098
|
+
let nodeSize = 0;
|
|
1099
|
+
for (const [key, value] of current.entries()) {
|
|
1100
|
+
if (isIterable(key) || isIterable(value)) {
|
|
1101
|
+
deferredKey[deferredSize] = key;
|
|
1102
|
+
deferredValue[deferredSize] = value;
|
|
1103
|
+
deferredSize++;
|
|
1104
|
+
} else {
|
|
1105
|
+
keyNodes[nodeSize] = generateTreeSync(ctx, key);
|
|
1106
|
+
valueNodes[nodeSize] = generateTreeSync(ctx, value);
|
|
1107
|
+
nodeSize++;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1111
|
+
keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
|
|
1112
|
+
valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
|
|
1113
|
+
}
|
|
1114
|
+
return [6 /* Map */, [keyNodes, valueNodes, len], id];
|
|
1115
|
+
}
|
|
1116
|
+
function generateNodeList2(ctx, current) {
|
|
1117
|
+
const size = current.length;
|
|
1118
|
+
const nodes = new Array(size);
|
|
1119
|
+
const deferred = new Array(size);
|
|
1120
|
+
for (let i = 0; i < size; i++) {
|
|
1121
|
+
if (i in current) {
|
|
1122
|
+
if (isIterable(current[i])) {
|
|
1123
|
+
deferred[i] = current[i];
|
|
1124
|
+
} else {
|
|
1125
|
+
nodes[i] = generateTreeSync(ctx, current[i]);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
for (let i = 0; i < size; i++) {
|
|
1130
|
+
if (i in deferred) {
|
|
1131
|
+
nodes[i] = generateTreeSync(ctx, deferred[i]);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
return nodes;
|
|
1135
|
+
}
|
|
1136
|
+
function generateArrayNode2(ctx, current, id) {
|
|
1137
|
+
return [7 /* Array */, generateNodeList2(ctx, current), id];
|
|
1138
|
+
}
|
|
1139
|
+
function generateAggregateErrorNode2(ctx, current, id) {
|
|
1140
|
+
const options = getErrorOptions(current);
|
|
1141
|
+
const optionsNode = options ? generateDictionary2(ctx, options) : void 0;
|
|
1142
|
+
const errorsNode = generateTreeSync(ctx, current.errors);
|
|
1143
|
+
return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
1144
|
+
}
|
|
1145
|
+
function generateErrorNode2(ctx, current, id) {
|
|
1146
|
+
const options = getErrorOptions(current);
|
|
1147
|
+
const optionsNode = options ? generateDictionary2(ctx, options) : void 0;
|
|
1148
|
+
return [
|
|
1149
|
+
11 /* Error */,
|
|
1150
|
+
[getErrorConstructor(current), current.message, optionsNode],
|
|
1151
|
+
id
|
|
1152
|
+
];
|
|
1153
|
+
}
|
|
1154
|
+
function generateIterableNode2(ctx, current, id) {
|
|
1155
|
+
assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
|
|
1156
|
+
const options = getIterableOptions(current);
|
|
1157
|
+
return [13 /* Iterable */, [
|
|
1158
|
+
// Parse options first before the items
|
|
1159
|
+
options ? generateDictionary2(ctx, options) : void 0,
|
|
1160
|
+
generateNodeList2(ctx, Array.from(current))
|
|
1161
|
+
], id];
|
|
1162
|
+
}
|
|
1163
|
+
function generateTreeSync(ctx, current) {
|
|
1164
|
+
if (isPrimitive(current)) {
|
|
1165
|
+
return [0 /* Primitive */, serializePrimitive(current)];
|
|
1166
|
+
}
|
|
1167
|
+
const id = generateRef(ctx, current);
|
|
1168
|
+
if (Array.isArray(id)) {
|
|
1169
|
+
return id;
|
|
1170
|
+
}
|
|
1171
|
+
const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
|
|
1172
|
+
if (semiPrimitive) {
|
|
1173
|
+
return semiPrimitive;
|
|
1174
|
+
}
|
|
1175
|
+
if (constructorCheck(current, Set)) {
|
|
1176
|
+
return generateSetNode2(ctx, current, id);
|
|
1177
|
+
}
|
|
1178
|
+
if (constructorCheck(current, Map)) {
|
|
1179
|
+
return generateMapNode2(ctx, current, id);
|
|
1180
|
+
}
|
|
1181
|
+
if (Array.isArray(current)) {
|
|
1182
|
+
return generateArrayNode2(ctx, current, id);
|
|
1183
|
+
}
|
|
1184
|
+
if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
|
|
1185
|
+
return generateAggregateErrorNode2(ctx, current, id);
|
|
1186
|
+
}
|
|
1187
|
+
if (current instanceof Error) {
|
|
1188
|
+
return generateErrorNode2(ctx, current, id);
|
|
1189
|
+
}
|
|
1190
|
+
if (isIterable(current)) {
|
|
1191
|
+
return generateIterableNode2(ctx, current, id);
|
|
1192
|
+
}
|
|
1193
|
+
const empty = current.constructor == null;
|
|
1194
|
+
if (current.constructor === Object || empty) {
|
|
1195
|
+
return [
|
|
1196
|
+
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
1197
|
+
generateDictionary2(ctx, current),
|
|
1198
|
+
id
|
|
1199
|
+
];
|
|
1200
|
+
}
|
|
1201
|
+
throw new Error("Unsupported value");
|
|
1202
|
+
}
|
|
1203
|
+
function parseSync(ctx, current) {
|
|
1204
|
+
const result = generateTreeSync(ctx, current);
|
|
1205
|
+
return [result, createRef(ctx, current), result[0] === 8 /* Object */];
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1201
1208
|
// src/index.ts
|
|
1202
1209
|
function finalize(ctx, rootID, isObject, result) {
|
|
1203
1210
|
if (ctx.vars.length) {
|