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
|
@@ -230,48 +230,6 @@ function parseTargets(targets) {
|
|
|
230
230
|
return flags;
|
|
231
231
|
}
|
|
232
232
|
|
|
233
|
-
// src/quote.ts
|
|
234
|
-
function quote(str) {
|
|
235
|
-
let result = "";
|
|
236
|
-
let lastPos = 0;
|
|
237
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
|
238
|
-
let replacement;
|
|
239
|
-
switch (str[i]) {
|
|
240
|
-
case '"':
|
|
241
|
-
replacement = '\\"';
|
|
242
|
-
break;
|
|
243
|
-
case "\\":
|
|
244
|
-
replacement = "\\\\";
|
|
245
|
-
break;
|
|
246
|
-
case "<":
|
|
247
|
-
replacement = "\\x3C";
|
|
248
|
-
break;
|
|
249
|
-
case "\n":
|
|
250
|
-
replacement = "\\n";
|
|
251
|
-
break;
|
|
252
|
-
case "\r":
|
|
253
|
-
replacement = "\\r";
|
|
254
|
-
break;
|
|
255
|
-
case "\u2028":
|
|
256
|
-
replacement = "\\u2028";
|
|
257
|
-
break;
|
|
258
|
-
case "\u2029":
|
|
259
|
-
replacement = "\\u2029";
|
|
260
|
-
break;
|
|
261
|
-
default:
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
result += str.slice(lastPos, i) + replacement;
|
|
265
|
-
lastPos = i + 1;
|
|
266
|
-
}
|
|
267
|
-
if (lastPos === 0) {
|
|
268
|
-
result = str;
|
|
269
|
-
} else {
|
|
270
|
-
result += str.slice(lastPos);
|
|
271
|
-
}
|
|
272
|
-
return `"${result}"`;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
233
|
// src/get-identifier.ts
|
|
276
234
|
var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
|
|
277
235
|
var REF_START_CHARS_LEN = REF_START_CHARS.length;
|
|
@@ -289,7 +247,7 @@ function getIdentifier(index) {
|
|
|
289
247
|
return ref;
|
|
290
248
|
}
|
|
291
249
|
|
|
292
|
-
// src/
|
|
250
|
+
// src/context.ts
|
|
293
251
|
var DEFAULT_OPTIONS = {
|
|
294
252
|
target: "es2023"
|
|
295
253
|
};
|
|
@@ -311,71 +269,6 @@ function createSerializationContext(options) {
|
|
|
311
269
|
markedRefs: new Set(options.markedRefs)
|
|
312
270
|
};
|
|
313
271
|
}
|
|
314
|
-
function getAssignmentExpression(assignment) {
|
|
315
|
-
switch (assignment.type) {
|
|
316
|
-
case "index":
|
|
317
|
-
return `${assignment.source}=${assignment.value}`;
|
|
318
|
-
case "map":
|
|
319
|
-
return `${assignment.source}.set(${assignment.key},${assignment.value})`;
|
|
320
|
-
case "set":
|
|
321
|
-
return `${assignment.source}.add(${assignment.value})`;
|
|
322
|
-
default:
|
|
323
|
-
return "";
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
function mergeAssignments(assignments) {
|
|
327
|
-
const newAssignments = [];
|
|
328
|
-
let current = assignments[0];
|
|
329
|
-
let prev = current;
|
|
330
|
-
for (let i = 1, len = assignments.length; i < len; i++) {
|
|
331
|
-
const item = assignments[i];
|
|
332
|
-
if (item.type === prev.type) {
|
|
333
|
-
if (item.type === "index" && item.value === prev.value) {
|
|
334
|
-
current = {
|
|
335
|
-
type: "index",
|
|
336
|
-
source: item.source,
|
|
337
|
-
value: getAssignmentExpression(current)
|
|
338
|
-
};
|
|
339
|
-
} else if (item.type === "map" && item.source === prev.source) {
|
|
340
|
-
current = {
|
|
341
|
-
type: "map",
|
|
342
|
-
source: getAssignmentExpression(current),
|
|
343
|
-
key: item.key,
|
|
344
|
-
value: item.value
|
|
345
|
-
};
|
|
346
|
-
} else if (item.type === "set" && item.source === prev.source) {
|
|
347
|
-
current = {
|
|
348
|
-
type: "set",
|
|
349
|
-
source: getAssignmentExpression(current),
|
|
350
|
-
value: item.value
|
|
351
|
-
};
|
|
352
|
-
} else {
|
|
353
|
-
newAssignments.push(current);
|
|
354
|
-
current = item;
|
|
355
|
-
}
|
|
356
|
-
} else {
|
|
357
|
-
newAssignments.push(current);
|
|
358
|
-
current = item;
|
|
359
|
-
}
|
|
360
|
-
prev = item;
|
|
361
|
-
}
|
|
362
|
-
newAssignments.push(current);
|
|
363
|
-
return newAssignments;
|
|
364
|
-
}
|
|
365
|
-
function resolveAssignments(assignments) {
|
|
366
|
-
if (assignments.length) {
|
|
367
|
-
let result = "";
|
|
368
|
-
const merged = mergeAssignments(assignments);
|
|
369
|
-
for (let i = 0, len = merged.length; i < len; i++) {
|
|
370
|
-
result += `${getAssignmentExpression(merged[i])},`;
|
|
371
|
-
}
|
|
372
|
-
return result;
|
|
373
|
-
}
|
|
374
|
-
return void 0;
|
|
375
|
-
}
|
|
376
|
-
function resolvePatches(ctx) {
|
|
377
|
-
return resolveAssignments(ctx.assignments);
|
|
378
|
-
}
|
|
379
272
|
function markRef(ctx, current) {
|
|
380
273
|
ctx.markedRefs.add(current);
|
|
381
274
|
}
|
|
@@ -397,12 +290,6 @@ function getRefParam(ctx, index) {
|
|
|
397
290
|
ctx.vars[actualIndex] = result;
|
|
398
291
|
return result;
|
|
399
292
|
}
|
|
400
|
-
function assignRef(ctx, index, value) {
|
|
401
|
-
if (ctx.markedRefs.has(index)) {
|
|
402
|
-
return `${getRefParam(ctx, index)}=${value}`;
|
|
403
|
-
}
|
|
404
|
-
return value;
|
|
405
|
-
}
|
|
406
293
|
function createRef(ctx, index) {
|
|
407
294
|
const current = ctx.refs.get(index);
|
|
408
295
|
if (current != null) {
|
|
@@ -412,39 +299,50 @@ function createRef(ctx, index) {
|
|
|
412
299
|
ctx.refs.set(index, id);
|
|
413
300
|
return id;
|
|
414
301
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
302
|
+
|
|
303
|
+
// src/quote.ts
|
|
304
|
+
function quote(str) {
|
|
305
|
+
let result = "";
|
|
306
|
+
let lastPos = 0;
|
|
307
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
|
308
|
+
let replacement;
|
|
309
|
+
switch (str[i]) {
|
|
310
|
+
case '"':
|
|
311
|
+
replacement = '\\"';
|
|
312
|
+
break;
|
|
313
|
+
case "\\":
|
|
314
|
+
replacement = "\\\\";
|
|
315
|
+
break;
|
|
316
|
+
case "<":
|
|
317
|
+
replacement = "\\x3C";
|
|
318
|
+
break;
|
|
319
|
+
case "\n":
|
|
320
|
+
replacement = "\\n";
|
|
321
|
+
break;
|
|
322
|
+
case "\r":
|
|
323
|
+
replacement = "\\r";
|
|
324
|
+
break;
|
|
325
|
+
case "\u2028":
|
|
326
|
+
replacement = "\\u2028";
|
|
327
|
+
break;
|
|
328
|
+
case "\u2029":
|
|
329
|
+
replacement = "\\u2029";
|
|
330
|
+
break;
|
|
331
|
+
default:
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
result += str.slice(lastPos, i) + replacement;
|
|
335
|
+
lastPos = i + 1;
|
|
336
|
+
}
|
|
337
|
+
if (lastPos === 0) {
|
|
338
|
+
result = str;
|
|
339
|
+
} else {
|
|
340
|
+
result += str.slice(lastPos);
|
|
341
|
+
}
|
|
342
|
+
return `"${result}"`;
|
|
447
343
|
}
|
|
344
|
+
|
|
345
|
+
// src/tree/shared.ts
|
|
448
346
|
function getErrorConstructor(error) {
|
|
449
347
|
if (error instanceof EvalError) {
|
|
450
348
|
return "EvalError";
|
|
@@ -519,46 +417,16 @@ function generateSemiPrimitiveValue(ctx, current, id) {
|
|
|
519
417
|
if (constructorCheck(current, RegExp)) {
|
|
520
418
|
return [4 /* RegExp */, String(current), id];
|
|
521
419
|
}
|
|
522
|
-
if (constructorCheck(current, Int8Array)) {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
if (constructorCheck(current, Int16Array)) {
|
|
527
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int16Array"');
|
|
528
|
-
return [14 /* TypedArray */, ["Int16Array", current.toString(), current.byteOffset], id];
|
|
529
|
-
}
|
|
530
|
-
if (constructorCheck(current, Int32Array)) {
|
|
531
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int32Array"');
|
|
532
|
-
return [14 /* TypedArray */, ["Int32Array", current.toString(), current.byteOffset], id];
|
|
533
|
-
}
|
|
534
|
-
if (constructorCheck(current, Uint8Array)) {
|
|
535
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint8Array"');
|
|
536
|
-
return [14 /* TypedArray */, ["Uint8Array", current.toString(), current.byteOffset], id];
|
|
537
|
-
}
|
|
538
|
-
if (constructorCheck(current, Uint16Array)) {
|
|
539
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint16Array"');
|
|
540
|
-
return [14 /* TypedArray */, ["Uint16Array", current.toString(), current.byteOffset], id];
|
|
541
|
-
}
|
|
542
|
-
if (constructorCheck(current, Uint32Array)) {
|
|
543
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint32Array"');
|
|
544
|
-
return [14 /* TypedArray */, ["Uint32Array", current.toString(), current.byteOffset], id];
|
|
420
|
+
if (constructorCheck(current, Int8Array) || constructorCheck(current, Int16Array) || constructorCheck(current, Int32Array) || constructorCheck(current, Uint8Array) || constructorCheck(current, Uint16Array) || constructorCheck(current, Uint32Array) || constructorCheck(current, Uint8ClampedArray) || constructorCheck(current, Float32Array) || constructorCheck(current, Float64Array)) {
|
|
421
|
+
const constructor = current.constructor.name;
|
|
422
|
+
assert(ctx.features & 1024 /* TypedArray */, `Unsupported value type "${constructor}"`);
|
|
423
|
+
return [14 /* TypedArray */, [constructor, current.toString(), current.byteOffset], id];
|
|
545
424
|
}
|
|
546
|
-
if (constructorCheck(current,
|
|
547
|
-
|
|
548
|
-
return [14 /* TypedArray */, ["Uint8ClampedArray", current.toString(), current.byteOffset], id];
|
|
549
|
-
}
|
|
550
|
-
if (constructorCheck(current, Float32Array)) {
|
|
551
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float32Array"');
|
|
552
|
-
return [14 /* TypedArray */, ["Float32Array", current.toString(), current.byteOffset], id];
|
|
553
|
-
}
|
|
554
|
-
if (constructorCheck(current, Float64Array)) {
|
|
555
|
-
assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float64Array"');
|
|
556
|
-
return [14 /* TypedArray */, ["Float64Array", current.toString(), current.byteOffset], id];
|
|
557
|
-
}
|
|
558
|
-
if (constructorCheck(current, BigInt64Array)) {
|
|
425
|
+
if (constructorCheck(current, BigInt64Array) || constructorCheck(current, BigUint64Array)) {
|
|
426
|
+
const constructor = current.constructor.name;
|
|
559
427
|
assert(
|
|
560
428
|
ctx.features & 2048 /* BigIntTypedArray */,
|
|
561
|
-
|
|
429
|
+
`Unsupported value type "${constructor}"`
|
|
562
430
|
);
|
|
563
431
|
let result = "";
|
|
564
432
|
const cap = current.length - 1;
|
|
@@ -568,195 +436,152 @@ function generateSemiPrimitiveValue(ctx, current, id) {
|
|
|
568
436
|
result += `"${current[cap]}"`;
|
|
569
437
|
return [
|
|
570
438
|
15 /* BigIntTypedArray */,
|
|
571
|
-
[
|
|
572
|
-
id
|
|
573
|
-
];
|
|
574
|
-
}
|
|
575
|
-
if (constructorCheck(current, BigUint64Array)) {
|
|
576
|
-
assert(
|
|
577
|
-
ctx.features & 2048 /* BigIntTypedArray */,
|
|
578
|
-
'Unsupported value type "BigUint64Array"'
|
|
579
|
-
);
|
|
580
|
-
let result = "";
|
|
581
|
-
const cap = current.length - 1;
|
|
582
|
-
for (let i = 0; i < cap; i++) {
|
|
583
|
-
result += `"${current[i]}n",`;
|
|
584
|
-
}
|
|
585
|
-
result += `"${current[cap]}"`;
|
|
586
|
-
return [
|
|
587
|
-
15 /* BigIntTypedArray */,
|
|
588
|
-
["BigInt64Array", result, current.byteOffset],
|
|
439
|
+
[constructor, result, current.byteOffset],
|
|
589
440
|
id
|
|
590
441
|
];
|
|
591
442
|
}
|
|
592
443
|
return void 0;
|
|
593
444
|
}
|
|
594
|
-
function
|
|
445
|
+
function serializePrimitive(value) {
|
|
446
|
+
if (value === true) {
|
|
447
|
+
return "!0";
|
|
448
|
+
}
|
|
449
|
+
if (value === false) {
|
|
450
|
+
return "!1";
|
|
451
|
+
}
|
|
452
|
+
if (value === void 0) {
|
|
453
|
+
return "void 0";
|
|
454
|
+
}
|
|
455
|
+
if (value === null) {
|
|
456
|
+
return null;
|
|
457
|
+
}
|
|
458
|
+
if (typeof value === "string") {
|
|
459
|
+
return quote(value);
|
|
460
|
+
}
|
|
461
|
+
if (Object.is(value, -0)) {
|
|
462
|
+
return "-0";
|
|
463
|
+
}
|
|
464
|
+
if (Object.is(value, Infinity)) {
|
|
465
|
+
return "1/0";
|
|
466
|
+
}
|
|
467
|
+
if (Object.is(value, -Infinity)) {
|
|
468
|
+
return "-1/0";
|
|
469
|
+
}
|
|
470
|
+
return value;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// src/tree/async.ts
|
|
474
|
+
async function generateDictionary(ctx, properties) {
|
|
595
475
|
const keys = Object.keys(properties);
|
|
596
|
-
const
|
|
597
|
-
const
|
|
598
|
-
const valueNodes = new Array(size);
|
|
599
|
-
const deferredKeys = new Array(size);
|
|
600
|
-
const deferredValues = new Array(size);
|
|
601
|
-
let deferredSize = 0;
|
|
602
|
-
let nodesSize = 0;
|
|
476
|
+
const nodes = {};
|
|
477
|
+
const deferred = {};
|
|
603
478
|
for (const key of keys) {
|
|
604
479
|
if (isIterable(properties[key])) {
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
480
|
+
deferred[key] = properties[key];
|
|
481
|
+
} else {
|
|
482
|
+
nodes[key] = await generateTreeAsync(ctx, properties[key]);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
for (const key of Object.keys(deferred)) {
|
|
486
|
+
nodes[key] = await generateTreeAsync(ctx, deferred[key]);
|
|
487
|
+
}
|
|
488
|
+
return nodes;
|
|
489
|
+
}
|
|
490
|
+
async function generateSetNode(ctx, current, id) {
|
|
491
|
+
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
492
|
+
const len = current.size;
|
|
493
|
+
const nodes = new Array(len);
|
|
494
|
+
const deferred = new Array(len);
|
|
495
|
+
let deferredSize = 0;
|
|
496
|
+
let nodeSize = 0;
|
|
497
|
+
for (const item of current.keys()) {
|
|
498
|
+
if (isIterable(item)) {
|
|
499
|
+
deferred[deferredSize++] = item;
|
|
608
500
|
} else {
|
|
609
|
-
|
|
610
|
-
valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
|
|
611
|
-
nodesSize++;
|
|
501
|
+
nodes[nodeSize++] = await generateTreeAsync(ctx, item);
|
|
612
502
|
}
|
|
613
503
|
}
|
|
614
504
|
for (let i = 0; i < deferredSize; i++) {
|
|
615
|
-
|
|
616
|
-
valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
|
|
505
|
+
nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
|
|
617
506
|
}
|
|
618
|
-
return [
|
|
507
|
+
return [5 /* Set */, nodes, id];
|
|
619
508
|
}
|
|
620
|
-
function
|
|
621
|
-
|
|
622
|
-
|
|
509
|
+
async function generateMapNode(ctx, current, id) {
|
|
510
|
+
assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
|
|
511
|
+
const len = current.size;
|
|
512
|
+
const keyNodes = new Array(len);
|
|
513
|
+
const valueNodes = new Array(len);
|
|
514
|
+
const deferredKey = new Array(len);
|
|
515
|
+
const deferredValue = new Array(len);
|
|
516
|
+
let deferredSize = 0;
|
|
517
|
+
let nodeSize = 0;
|
|
518
|
+
for (const [key, value] of current.entries()) {
|
|
519
|
+
if (isIterable(key) || isIterable(value)) {
|
|
520
|
+
deferredKey[deferredSize] = key;
|
|
521
|
+
deferredValue[deferredSize] = value;
|
|
522
|
+
deferredSize++;
|
|
523
|
+
} else {
|
|
524
|
+
keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
|
|
525
|
+
valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
|
|
526
|
+
nodeSize++;
|
|
527
|
+
}
|
|
623
528
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
529
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
530
|
+
keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
|
|
531
|
+
valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
|
|
627
532
|
}
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
let nodeSize = 0;
|
|
639
|
-
for (const item of current.keys()) {
|
|
640
|
-
if (isIterable(item)) {
|
|
641
|
-
deferred[deferredSize++] = item;
|
|
533
|
+
return [6 /* Map */, [keyNodes, valueNodes, len], id];
|
|
534
|
+
}
|
|
535
|
+
async function generateNodeList(ctx, current) {
|
|
536
|
+
const size = current.length;
|
|
537
|
+
const nodes = new Array(size);
|
|
538
|
+
const deferred = new Array(size);
|
|
539
|
+
for (let i = 0; i < size; i++) {
|
|
540
|
+
if (i in current) {
|
|
541
|
+
if (isIterable(current[i])) {
|
|
542
|
+
deferred[i] = current[i];
|
|
642
543
|
} else {
|
|
643
|
-
nodes[
|
|
544
|
+
nodes[i] = await generateTreeAsync(ctx, current[i]);
|
|
644
545
|
}
|
|
645
546
|
}
|
|
646
|
-
for (let i = 0; i < deferredSize; i++) {
|
|
647
|
-
nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
|
|
648
|
-
}
|
|
649
|
-
return [5 /* Set */, nodes, id];
|
|
650
547
|
}
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
const keyNodes = new Array(len);
|
|
655
|
-
const valueNodes = new Array(len);
|
|
656
|
-
const deferredKey = new Array(len);
|
|
657
|
-
const deferredValue = new Array(len);
|
|
658
|
-
let deferredSize = 0;
|
|
659
|
-
let nodeSize = 0;
|
|
660
|
-
for (const [key, value] of current.entries()) {
|
|
661
|
-
if (isIterable(key) || isIterable(value)) {
|
|
662
|
-
deferredKey[deferredSize] = key;
|
|
663
|
-
deferredValue[deferredSize] = value;
|
|
664
|
-
deferredSize++;
|
|
665
|
-
} else {
|
|
666
|
-
keyNodes[nodeSize] = generateTreeSync(ctx, key);
|
|
667
|
-
valueNodes[nodeSize] = generateTreeSync(ctx, value);
|
|
668
|
-
nodeSize++;
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
for (let i = 0; i < deferredSize; i++) {
|
|
672
|
-
keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
|
|
673
|
-
valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
|
|
674
|
-
}
|
|
675
|
-
return [6 /* Map */, [keyNodes, valueNodes, len], id];
|
|
676
|
-
}
|
|
677
|
-
if (Array.isArray(current)) {
|
|
678
|
-
const size = current.length;
|
|
679
|
-
const nodes = new Array(size);
|
|
680
|
-
const deferred = new Array(size);
|
|
681
|
-
for (let i = 0; i < size; i++) {
|
|
682
|
-
if (i in current) {
|
|
683
|
-
if (isIterable(current[i])) {
|
|
684
|
-
deferred[i] = current[i];
|
|
685
|
-
} else {
|
|
686
|
-
nodes[i] = generateTreeSync(ctx, current[i]);
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
for (let i = 0; i < size; i++) {
|
|
691
|
-
if (i in deferred) {
|
|
692
|
-
nodes[i] = generateTreeSync(ctx, deferred[i]);
|
|
693
|
-
}
|
|
548
|
+
for (let i = 0; i < size; i++) {
|
|
549
|
+
if (i in deferred) {
|
|
550
|
+
nodes[i] = await generateTreeAsync(ctx, deferred[i]);
|
|
694
551
|
}
|
|
695
|
-
return [7 /* Array */, nodes, id];
|
|
696
552
|
}
|
|
697
|
-
|
|
698
|
-
const options = getErrorOptions(current);
|
|
699
|
-
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
700
|
-
const errorsNode = generateTreeSync(ctx, current.errors);
|
|
701
|
-
return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
702
|
-
}
|
|
703
|
-
if (current instanceof Error) {
|
|
704
|
-
const options = getErrorOptions(current);
|
|
705
|
-
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
706
|
-
return [
|
|
707
|
-
11 /* Error */,
|
|
708
|
-
[getErrorConstructor(current), current.message, optionsNode],
|
|
709
|
-
id
|
|
710
|
-
];
|
|
711
|
-
}
|
|
712
|
-
if (isIterable(current)) {
|
|
713
|
-
assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
|
|
714
|
-
const options = getIterableOptions(current);
|
|
715
|
-
return [13 /* Iterable */, [
|
|
716
|
-
// Parse options first before the items
|
|
717
|
-
options ? serializePropertiesSync(ctx, options) : void 0,
|
|
718
|
-
generateTreeSync(ctx, Array.from(current))
|
|
719
|
-
], id];
|
|
720
|
-
}
|
|
721
|
-
const empty = current.constructor == null;
|
|
722
|
-
if (current.constructor === Object || empty) {
|
|
723
|
-
return [
|
|
724
|
-
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
725
|
-
serializePropertiesSync(ctx, current),
|
|
726
|
-
id
|
|
727
|
-
];
|
|
728
|
-
}
|
|
729
|
-
throw new Error("Unsupported value");
|
|
553
|
+
return nodes;
|
|
730
554
|
}
|
|
731
|
-
function
|
|
732
|
-
|
|
733
|
-
return [result, createRef(ctx, current), result[0] === 8 /* Object */];
|
|
555
|
+
async function generateArrayNode(ctx, current, id) {
|
|
556
|
+
return [7 /* Array */, await generateNodeList(ctx, current), id];
|
|
734
557
|
}
|
|
735
|
-
async function
|
|
736
|
-
const
|
|
737
|
-
const
|
|
738
|
-
const
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
558
|
+
async function generateAggregateErrorNode(ctx, current, id) {
|
|
559
|
+
const options = getErrorOptions(current);
|
|
560
|
+
const optionsNode = options ? await generateDictionary(ctx, options) : void 0;
|
|
561
|
+
const errorsNode = await generateTreeAsync(ctx, current.errors);
|
|
562
|
+
return [
|
|
563
|
+
12 /* AggregateError */,
|
|
564
|
+
[current.message, optionsNode, errorsNode],
|
|
565
|
+
id
|
|
566
|
+
];
|
|
567
|
+
}
|
|
568
|
+
async function generateErrorNode(ctx, current, id) {
|
|
569
|
+
const options = getErrorOptions(current);
|
|
570
|
+
const optionsNode = options ? await generateDictionary(ctx, options) : void 0;
|
|
571
|
+
return [
|
|
572
|
+
11 /* Error */,
|
|
573
|
+
[getErrorConstructor(current), current.message, optionsNode],
|
|
574
|
+
id
|
|
575
|
+
];
|
|
576
|
+
}
|
|
577
|
+
async function generateIterableNode(ctx, current, id) {
|
|
578
|
+
assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
|
|
579
|
+
const options = getIterableOptions(current);
|
|
580
|
+
return [13 /* Iterable */, [
|
|
581
|
+
// Parse options first before the items
|
|
582
|
+
options ? await generateDictionary(ctx, options) : void 0,
|
|
583
|
+
await generateNodeList(ctx, Array.from(current))
|
|
584
|
+
], id];
|
|
760
585
|
}
|
|
761
586
|
async function generateTreeAsync(ctx, current) {
|
|
762
587
|
if (isPrimitive(current)) {
|
|
@@ -779,99 +604,28 @@ async function generateTreeAsync(ctx, current) {
|
|
|
779
604
|
]);
|
|
780
605
|
}
|
|
781
606
|
if (constructorCheck(current, Set)) {
|
|
782
|
-
|
|
783
|
-
const len = current.size;
|
|
784
|
-
const nodes = new Array(len);
|
|
785
|
-
const deferred = new Array(len);
|
|
786
|
-
let deferredSize = 0;
|
|
787
|
-
let nodeSize = 0;
|
|
788
|
-
for (const item of current.keys()) {
|
|
789
|
-
if (isIterable(item)) {
|
|
790
|
-
deferred[deferredSize++] = item;
|
|
791
|
-
} else {
|
|
792
|
-
nodes[nodeSize++] = await generateTreeAsync(ctx, item);
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
for (let i = 0; i < deferredSize; i++) {
|
|
796
|
-
nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
|
|
797
|
-
}
|
|
798
|
-
return [5 /* Set */, nodes, id];
|
|
607
|
+
return generateSetNode(ctx, current, id);
|
|
799
608
|
}
|
|
800
609
|
if (constructorCheck(current, Map)) {
|
|
801
|
-
|
|
802
|
-
const len = current.size;
|
|
803
|
-
const keyNodes = new Array(len);
|
|
804
|
-
const valueNodes = new Array(len);
|
|
805
|
-
const deferredKey = new Array(len);
|
|
806
|
-
const deferredValue = new Array(len);
|
|
807
|
-
let deferredSize = 0;
|
|
808
|
-
let nodeSize = 0;
|
|
809
|
-
for (const [key, value] of current.entries()) {
|
|
810
|
-
if (isIterable(key) || isIterable(value)) {
|
|
811
|
-
deferredKey[deferredSize] = key;
|
|
812
|
-
deferredValue[deferredSize] = value;
|
|
813
|
-
deferredSize++;
|
|
814
|
-
} else {
|
|
815
|
-
keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
|
|
816
|
-
valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
|
|
817
|
-
nodeSize++;
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
for (let i = 0; i < deferredSize; i++) {
|
|
821
|
-
keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
|
|
822
|
-
valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
|
|
823
|
-
}
|
|
824
|
-
return [6 /* Map */, [keyNodes, valueNodes, len], id];
|
|
610
|
+
return generateMapNode(ctx, current, id);
|
|
825
611
|
}
|
|
826
612
|
if (Array.isArray(current)) {
|
|
827
|
-
|
|
828
|
-
const nodes = new Array(size);
|
|
829
|
-
const deferred = new Array(size);
|
|
830
|
-
for (let i = 0; i < size; i++) {
|
|
831
|
-
const item = current[i];
|
|
832
|
-
if (i in current) {
|
|
833
|
-
if (isIterable(item)) {
|
|
834
|
-
deferred[i] = item;
|
|
835
|
-
} else {
|
|
836
|
-
nodes[i] = await generateTreeAsync(ctx, item);
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
for (let i = 0; i < size; i++) {
|
|
841
|
-
if (i in deferred) {
|
|
842
|
-
nodes[i] = await generateTreeAsync(ctx, deferred[i]);
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
return [7 /* Array */, nodes, id];
|
|
613
|
+
return generateArrayNode(ctx, current, id);
|
|
846
614
|
}
|
|
847
615
|
if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
|
|
848
|
-
|
|
849
|
-
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
850
|
-
const errorsNode = await generateTreeAsync(ctx, current.errors);
|
|
851
|
-
return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
616
|
+
return generateAggregateErrorNode(ctx, current, id);
|
|
852
617
|
}
|
|
853
618
|
if (current instanceof Error) {
|
|
854
|
-
|
|
855
|
-
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
856
|
-
return [
|
|
857
|
-
11 /* Error */,
|
|
858
|
-
[getErrorConstructor(current), current.message, optionsNode],
|
|
859
|
-
id
|
|
860
|
-
];
|
|
619
|
+
return generateErrorNode(ctx, current, id);
|
|
861
620
|
}
|
|
862
621
|
if (isIterable(current)) {
|
|
863
|
-
|
|
864
|
-
const options = getIterableOptions(current);
|
|
865
|
-
return [13 /* Iterable */, [
|
|
866
|
-
options ? await serializePropertiesAsync(ctx, options) : void 0,
|
|
867
|
-
await generateTreeAsync(ctx, Array.from(current))
|
|
868
|
-
], id];
|
|
622
|
+
return generateIterableNode(ctx, current, id);
|
|
869
623
|
}
|
|
870
624
|
const empty = current.constructor == null;
|
|
871
625
|
if (current.constructor === Object || empty) {
|
|
872
626
|
return [
|
|
873
627
|
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
874
|
-
await
|
|
628
|
+
await generateDictionary(ctx, current),
|
|
875
629
|
id
|
|
876
630
|
];
|
|
877
631
|
}
|
|
@@ -881,43 +635,121 @@ async function parseAsync(ctx, current) {
|
|
|
881
635
|
const result = await generateTreeAsync(ctx, current);
|
|
882
636
|
return [result, createRef(ctx, current), result[0] === 8 /* Object */];
|
|
883
637
|
}
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
}
|
|
897
|
-
if (typeof value === "string") {
|
|
898
|
-
return quote(value);
|
|
638
|
+
|
|
639
|
+
// src/tree/serialize.ts
|
|
640
|
+
function getAssignmentExpression(assignment) {
|
|
641
|
+
switch (assignment.type) {
|
|
642
|
+
case "index":
|
|
643
|
+
return `${assignment.source}=${assignment.value}`;
|
|
644
|
+
case "map":
|
|
645
|
+
return `${assignment.source}.set(${assignment.key},${assignment.value})`;
|
|
646
|
+
case "set":
|
|
647
|
+
return `${assignment.source}.add(${assignment.value})`;
|
|
648
|
+
default:
|
|
649
|
+
return "";
|
|
899
650
|
}
|
|
900
|
-
|
|
901
|
-
|
|
651
|
+
}
|
|
652
|
+
function mergeAssignments(assignments) {
|
|
653
|
+
const newAssignments = [];
|
|
654
|
+
let current = assignments[0];
|
|
655
|
+
let prev = current;
|
|
656
|
+
for (let i = 1, len = assignments.length; i < len; i++) {
|
|
657
|
+
const item = assignments[i];
|
|
658
|
+
if (item.type === prev.type) {
|
|
659
|
+
if (item.type === "index" && item.value === prev.value) {
|
|
660
|
+
current = {
|
|
661
|
+
type: "index",
|
|
662
|
+
source: item.source,
|
|
663
|
+
value: getAssignmentExpression(current)
|
|
664
|
+
};
|
|
665
|
+
} else if (item.type === "map" && item.source === prev.source) {
|
|
666
|
+
current = {
|
|
667
|
+
type: "map",
|
|
668
|
+
source: getAssignmentExpression(current),
|
|
669
|
+
key: item.key,
|
|
670
|
+
value: item.value
|
|
671
|
+
};
|
|
672
|
+
} else if (item.type === "set" && item.source === prev.source) {
|
|
673
|
+
current = {
|
|
674
|
+
type: "set",
|
|
675
|
+
source: getAssignmentExpression(current),
|
|
676
|
+
value: item.value
|
|
677
|
+
};
|
|
678
|
+
} else {
|
|
679
|
+
newAssignments.push(current);
|
|
680
|
+
current = item;
|
|
681
|
+
}
|
|
682
|
+
} else {
|
|
683
|
+
newAssignments.push(current);
|
|
684
|
+
current = item;
|
|
685
|
+
}
|
|
686
|
+
prev = item;
|
|
902
687
|
}
|
|
903
|
-
|
|
904
|
-
|
|
688
|
+
newAssignments.push(current);
|
|
689
|
+
return newAssignments;
|
|
690
|
+
}
|
|
691
|
+
function resolveAssignments(assignments) {
|
|
692
|
+
if (assignments.length) {
|
|
693
|
+
let result = "";
|
|
694
|
+
const merged = mergeAssignments(assignments);
|
|
695
|
+
for (let i = 0, len = merged.length; i < len; i++) {
|
|
696
|
+
result += `${getAssignmentExpression(merged[i])},`;
|
|
697
|
+
}
|
|
698
|
+
return result;
|
|
905
699
|
}
|
|
906
|
-
|
|
907
|
-
|
|
700
|
+
return void 0;
|
|
701
|
+
}
|
|
702
|
+
function resolvePatches(ctx) {
|
|
703
|
+
return resolveAssignments(ctx.assignments);
|
|
704
|
+
}
|
|
705
|
+
function assignRef(ctx, index, value) {
|
|
706
|
+
if (ctx.markedRefs.has(index)) {
|
|
707
|
+
return `${getRefParam(ctx, index)}=${value}`;
|
|
908
708
|
}
|
|
909
709
|
return value;
|
|
910
710
|
}
|
|
711
|
+
function createAssignment(ctx, source, value) {
|
|
712
|
+
ctx.assignments.push({
|
|
713
|
+
type: "index",
|
|
714
|
+
source,
|
|
715
|
+
value
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
function createSetAdd(ctx, ref, value) {
|
|
719
|
+
markRef(ctx, ref);
|
|
720
|
+
ctx.assignments.push({
|
|
721
|
+
type: "set",
|
|
722
|
+
source: getRefParam(ctx, ref),
|
|
723
|
+
value
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
function createMapSet(ctx, ref, key, value) {
|
|
727
|
+
markRef(ctx, ref);
|
|
728
|
+
ctx.assignments.push({
|
|
729
|
+
type: "map",
|
|
730
|
+
source: getRefParam(ctx, ref),
|
|
731
|
+
key,
|
|
732
|
+
value
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
function createArrayAssign(ctx, ref, index, value) {
|
|
736
|
+
markRef(ctx, ref);
|
|
737
|
+
createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
|
|
738
|
+
}
|
|
739
|
+
function createObjectAssign(ctx, ref, key, value, computed) {
|
|
740
|
+
markRef(ctx, ref);
|
|
741
|
+
const member = computed ? `[${key}]` : `.${key}`;
|
|
742
|
+
createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
|
|
743
|
+
}
|
|
911
744
|
var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
|
|
912
|
-
function serializeAssignments(ctx, targetRef,
|
|
745
|
+
function serializeAssignments(ctx, targetRef, record) {
|
|
913
746
|
ctx.stack.push(targetRef);
|
|
914
747
|
const mainAssignments = [];
|
|
915
|
-
for (
|
|
748
|
+
for (const key of Object.keys(record)) {
|
|
916
749
|
const parentStack = ctx.stack;
|
|
917
750
|
ctx.stack = [];
|
|
918
|
-
const refParam = serializeTree(ctx,
|
|
751
|
+
const refParam = serializeTree(ctx, record[key]);
|
|
919
752
|
ctx.stack = parentStack;
|
|
920
|
-
const key = keys[i];
|
|
921
753
|
const check = Number(key);
|
|
922
754
|
const parentAssignment = ctx.assignments;
|
|
923
755
|
ctx.assignments = mainAssignments;
|
|
@@ -934,12 +766,11 @@ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
|
|
|
934
766
|
ctx.stack.pop();
|
|
935
767
|
return resolveAssignments(mainAssignments);
|
|
936
768
|
}
|
|
937
|
-
function
|
|
769
|
+
function serializeDictionary(ctx, sourceID, record) {
|
|
938
770
|
let result = "";
|
|
939
771
|
ctx.stack.push(sourceID);
|
|
940
|
-
for (
|
|
941
|
-
const
|
|
942
|
-
const val = values[i];
|
|
772
|
+
for (const key of Object.keys(record)) {
|
|
773
|
+
const val = record[key];
|
|
943
774
|
const check = Number(key);
|
|
944
775
|
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
945
776
|
const validKey = isIdentifier ? key : quote(key);
|
|
@@ -959,212 +790,388 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
|
|
|
959
790
|
ctx.stack.pop();
|
|
960
791
|
return `{${result.substring(0, result.length - 1)}}`;
|
|
961
792
|
}
|
|
962
|
-
function
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
793
|
+
function serializePromise(ctx, value, id) {
|
|
794
|
+
let serialized;
|
|
795
|
+
if (isReferenceInStack(ctx, value)) {
|
|
796
|
+
serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
|
|
797
|
+
} else {
|
|
798
|
+
ctx.stack.push(id);
|
|
799
|
+
const result = serializeTree(ctx, value);
|
|
800
|
+
ctx.stack.pop();
|
|
801
|
+
serialized = `Promise.resolve(${result})`;
|
|
802
|
+
}
|
|
803
|
+
return assignRef(ctx, id, serialized);
|
|
804
|
+
}
|
|
805
|
+
function serializeTypedArray(ctx, constructor, init, byteOffset, id) {
|
|
806
|
+
let args = `[${init}]`;
|
|
807
|
+
if (byteOffset !== 0) {
|
|
808
|
+
args += `,${byteOffset}`;
|
|
809
|
+
}
|
|
810
|
+
return assignRef(ctx, id, `new ${constructor}(${args})`);
|
|
811
|
+
}
|
|
812
|
+
function serializeSet(ctx, value, id) {
|
|
813
|
+
let serialized = "new Set";
|
|
814
|
+
const size = value.length;
|
|
815
|
+
if (size) {
|
|
816
|
+
let result = "";
|
|
817
|
+
ctx.stack.push(id);
|
|
818
|
+
for (let i = 0; i < size; i++) {
|
|
819
|
+
const item = value[i];
|
|
820
|
+
if (isReferenceInStack(ctx, item)) {
|
|
821
|
+
createSetAdd(ctx, id, getRefParam(ctx, item[1]));
|
|
972
822
|
} else {
|
|
973
|
-
ctx
|
|
974
|
-
const result = serializeTree(ctx, value);
|
|
975
|
-
ctx.stack.pop();
|
|
976
|
-
serialized = `Promise.resolve(${result})`;
|
|
823
|
+
result += `${serializeTree(ctx, item)},`;
|
|
977
824
|
}
|
|
978
|
-
return assignRef(ctx, id, serialized);
|
|
979
825
|
}
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
return assignRef(ctx, id, `new Date("${value}")`);
|
|
984
|
-
case 4 /* RegExp */:
|
|
985
|
-
return assignRef(ctx, id, value);
|
|
986
|
-
case 15 /* BigIntTypedArray */:
|
|
987
|
-
case 14 /* TypedArray */: {
|
|
988
|
-
let args = `[${value[1]}]`;
|
|
989
|
-
if (value[2] !== 0) {
|
|
990
|
-
args += `,${value[2]}`;
|
|
991
|
-
}
|
|
992
|
-
return assignRef(ctx, id, `new ${value[0]}(${args})`);
|
|
826
|
+
ctx.stack.pop();
|
|
827
|
+
if (result) {
|
|
828
|
+
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
993
829
|
}
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
830
|
+
}
|
|
831
|
+
return assignRef(ctx, id, serialized);
|
|
832
|
+
}
|
|
833
|
+
function serializeMap(ctx, keys, values, size, id) {
|
|
834
|
+
let serialized = "new Map";
|
|
835
|
+
if (size) {
|
|
836
|
+
let result = "";
|
|
837
|
+
ctx.stack.push(id);
|
|
838
|
+
for (let i = 0; i < size; i++) {
|
|
839
|
+
const key = keys[i];
|
|
840
|
+
const val = values[i];
|
|
841
|
+
if (isReferenceInStack(ctx, key)) {
|
|
842
|
+
const keyRef = getRefParam(ctx, key[1]);
|
|
843
|
+
if (isReferenceInStack(ctx, val)) {
|
|
844
|
+
const valueRef = getRefParam(ctx, val[1]);
|
|
845
|
+
createMapSet(ctx, id, keyRef, valueRef);
|
|
846
|
+
} else {
|
|
847
|
+
const parent = ctx.stack;
|
|
848
|
+
ctx.stack = [];
|
|
849
|
+
createMapSet(ctx, id, keyRef, serializeTree(ctx, val));
|
|
850
|
+
ctx.stack = parent;
|
|
1011
851
|
}
|
|
852
|
+
} else if (isReferenceInStack(ctx, val)) {
|
|
853
|
+
const valueRef = getRefParam(ctx, val[1]);
|
|
854
|
+
const parent = ctx.stack;
|
|
855
|
+
ctx.stack = [];
|
|
856
|
+
createMapSet(ctx, id, serializeTree(ctx, key), valueRef);
|
|
857
|
+
ctx.stack = parent;
|
|
858
|
+
} else {
|
|
859
|
+
result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
|
|
1012
860
|
}
|
|
1013
|
-
return assignRef(ctx, id, serialized);
|
|
1014
861
|
}
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
if (size) {
|
|
1019
|
-
let result = "";
|
|
1020
|
-
ctx.stack.push(id);
|
|
1021
|
-
for (let i = 0; i < size; i++) {
|
|
1022
|
-
const key = value[0][i];
|
|
1023
|
-
const val = value[1][i];
|
|
1024
|
-
if (isReferenceInStack(ctx, key)) {
|
|
1025
|
-
const keyRef = getRefParam(ctx, key[1]);
|
|
1026
|
-
if (isReferenceInStack(ctx, val)) {
|
|
1027
|
-
const valueRef = getRefParam(ctx, val[1]);
|
|
1028
|
-
createMapSet(ctx, id, keyRef, valueRef);
|
|
1029
|
-
} else {
|
|
1030
|
-
const parent = ctx.stack;
|
|
1031
|
-
ctx.stack = [];
|
|
1032
|
-
createMapSet(ctx, id, keyRef, serializeTree(ctx, val));
|
|
1033
|
-
ctx.stack = parent;
|
|
1034
|
-
}
|
|
1035
|
-
} else if (isReferenceInStack(ctx, val)) {
|
|
1036
|
-
const valueRef = getRefParam(ctx, val[1]);
|
|
1037
|
-
const parent = ctx.stack;
|
|
1038
|
-
ctx.stack = [];
|
|
1039
|
-
createMapSet(ctx, id, serializeTree(ctx, key), valueRef);
|
|
1040
|
-
ctx.stack = parent;
|
|
1041
|
-
} else {
|
|
1042
|
-
result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
ctx.stack.pop();
|
|
1046
|
-
if (result) {
|
|
1047
|
-
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1048
|
-
}
|
|
1049
|
-
}
|
|
1050
|
-
return assignRef(ctx, id, serialized);
|
|
862
|
+
ctx.stack.pop();
|
|
863
|
+
if (result) {
|
|
864
|
+
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1051
865
|
}
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
}
|
|
1068
|
-
} else {
|
|
866
|
+
}
|
|
867
|
+
return assignRef(ctx, id, serialized);
|
|
868
|
+
}
|
|
869
|
+
function serializeNodeList(ctx, value, id) {
|
|
870
|
+
const size = value.length;
|
|
871
|
+
let values = "";
|
|
872
|
+
for (let i = 0; i < size; i++) {
|
|
873
|
+
const item = value[i];
|
|
874
|
+
if (item) {
|
|
875
|
+
if (isReferenceInStack(ctx, item)) {
|
|
876
|
+
createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
|
|
877
|
+
values += ",";
|
|
878
|
+
} else {
|
|
879
|
+
values += serializeTree(ctx, item);
|
|
880
|
+
if (i < size - 1) {
|
|
1069
881
|
values += ",";
|
|
1070
882
|
}
|
|
1071
883
|
}
|
|
1072
|
-
|
|
1073
|
-
|
|
884
|
+
} else {
|
|
885
|
+
values += ",";
|
|
1074
886
|
}
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
887
|
+
}
|
|
888
|
+
return `[${values}]`;
|
|
889
|
+
}
|
|
890
|
+
function serializeArray(ctx, value, id) {
|
|
891
|
+
ctx.stack.push(id);
|
|
892
|
+
const result = serializeNodeList(ctx, value, id);
|
|
893
|
+
ctx.stack.pop();
|
|
894
|
+
return assignRef(ctx, id, result);
|
|
895
|
+
}
|
|
896
|
+
function serializeWithObjectAssign(ctx, value, id, serialized) {
|
|
897
|
+
const fields = serializeDictionary(ctx, id, value);
|
|
898
|
+
if (fields !== "{}") {
|
|
899
|
+
return `Object.assign(${serialized},${fields})`;
|
|
900
|
+
}
|
|
901
|
+
return serialized;
|
|
902
|
+
}
|
|
903
|
+
function serializeAggregateError(ctx, message, options, errors, id) {
|
|
904
|
+
ctx.stack.push(id);
|
|
905
|
+
let serialized = `new AggregateError(${serializeTree(ctx, errors)},${quote(message)})`;
|
|
906
|
+
ctx.stack.pop();
|
|
907
|
+
if (options) {
|
|
908
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
909
|
+
serialized = serializeWithObjectAssign(ctx, options, id, serialized);
|
|
910
|
+
} else {
|
|
911
|
+
markRef(ctx, id);
|
|
912
|
+
const assignments = serializeAssignments(ctx, id, options);
|
|
913
|
+
if (assignments) {
|
|
914
|
+
const ref = getRefParam(ctx, id);
|
|
915
|
+
return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
|
|
1091
916
|
}
|
|
1092
|
-
return assignRef(ctx, id, serialized);
|
|
1093
917
|
}
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
}
|
|
918
|
+
}
|
|
919
|
+
return assignRef(ctx, id, serialized);
|
|
920
|
+
}
|
|
921
|
+
function serializeError(ctx, constructor, message, options, id) {
|
|
922
|
+
let serialized = `new ${constructor}(${quote(message)})`;
|
|
923
|
+
if (options) {
|
|
924
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
925
|
+
serialized = serializeWithObjectAssign(ctx, options, id, serialized);
|
|
926
|
+
} else {
|
|
927
|
+
markRef(ctx, id);
|
|
928
|
+
const assignments = serializeAssignments(ctx, id, options);
|
|
929
|
+
if (assignments) {
|
|
930
|
+
const ref = getRefParam(ctx, id);
|
|
931
|
+
return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
|
|
1108
932
|
}
|
|
1109
|
-
return assignRef(ctx, id, serialized);
|
|
1110
933
|
}
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
}
|
|
934
|
+
}
|
|
935
|
+
return assignRef(ctx, id, serialized);
|
|
936
|
+
}
|
|
937
|
+
function serializeIterable(ctx, options, items, id) {
|
|
938
|
+
const parent = ctx.stack;
|
|
939
|
+
ctx.stack = [];
|
|
940
|
+
const values = serializeNodeList(ctx, items, id);
|
|
941
|
+
ctx.stack = parent;
|
|
942
|
+
let serialized;
|
|
943
|
+
if (ctx.features & 2 /* ArrayPrototypeValues */) {
|
|
944
|
+
serialized = `${values}.values()`;
|
|
945
|
+
} else {
|
|
946
|
+
serialized = `${values}[Symbol.iterator]()`;
|
|
947
|
+
}
|
|
948
|
+
if (ctx.features & 4 /* ArrowFunction */) {
|
|
949
|
+
serialized = `{[Symbol.iterator]:()=>${serialized}}`;
|
|
950
|
+
} else if (ctx.features & 32 /* MethodShorthand */) {
|
|
951
|
+
serialized = `{[Symbol.iterator](){return ${serialized}}}`;
|
|
952
|
+
} else {
|
|
953
|
+
serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
|
|
954
|
+
}
|
|
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})`;
|
|
1141
964
|
}
|
|
1142
|
-
return assignRef(ctx, id, serialized);
|
|
1143
965
|
}
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
}
|
|
1158
|
-
}
|
|
1159
|
-
return assignRef(ctx, id, serialized);
|
|
966
|
+
}
|
|
967
|
+
return assignRef(ctx, id, serialized);
|
|
968
|
+
}
|
|
969
|
+
function serializeNullConstructor(ctx, value, id) {
|
|
970
|
+
let serialized = "Object.create(null)";
|
|
971
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
972
|
+
serialized = serializeWithObjectAssign(ctx, value, id, serialized);
|
|
973
|
+
} else {
|
|
974
|
+
markRef(ctx, id);
|
|
975
|
+
const assignments = serializeAssignments(ctx, id, value);
|
|
976
|
+
if (assignments) {
|
|
977
|
+
const ref = getRefParam(ctx, id);
|
|
978
|
+
return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
|
|
1160
979
|
}
|
|
980
|
+
}
|
|
981
|
+
return assignRef(ctx, id, serialized);
|
|
982
|
+
}
|
|
983
|
+
function serializeTree(ctx, [type, value, id]) {
|
|
984
|
+
switch (type) {
|
|
985
|
+
case 0 /* Primitive */:
|
|
986
|
+
return String(value);
|
|
987
|
+
case 2 /* Reference */:
|
|
988
|
+
return getRefParam(ctx, value);
|
|
989
|
+
case 10 /* Promise */:
|
|
990
|
+
return serializePromise(ctx, value, id);
|
|
991
|
+
case 1 /* BigInt */:
|
|
992
|
+
return value;
|
|
993
|
+
case 3 /* Date */:
|
|
994
|
+
return assignRef(ctx, id, `new Date("${value}")`);
|
|
995
|
+
case 4 /* RegExp */:
|
|
996
|
+
return assignRef(ctx, id, value);
|
|
997
|
+
case 15 /* BigIntTypedArray */:
|
|
998
|
+
case 14 /* TypedArray */:
|
|
999
|
+
return serializeTypedArray(ctx, value[0], value[1], value[2], id);
|
|
1000
|
+
case 5 /* Set */:
|
|
1001
|
+
return serializeSet(ctx, value, id);
|
|
1002
|
+
case 6 /* Map */:
|
|
1003
|
+
return serializeMap(ctx, value[0], value[1], value[2], id);
|
|
1004
|
+
case 7 /* Array */:
|
|
1005
|
+
return serializeArray(ctx, value, id);
|
|
1006
|
+
case 12 /* AggregateError */:
|
|
1007
|
+
return serializeAggregateError(ctx, value[0], value[1], value[2], id);
|
|
1008
|
+
case 11 /* Error */:
|
|
1009
|
+
return serializeError(ctx, value[0], value[1], value[2], id);
|
|
1010
|
+
case 13 /* Iterable */:
|
|
1011
|
+
return serializeIterable(ctx, value[0], value[1], id);
|
|
1012
|
+
case 9 /* NullConstructor */:
|
|
1013
|
+
return serializeNullConstructor(ctx, value, id);
|
|
1161
1014
|
case 8 /* Object */:
|
|
1162
|
-
return assignRef(ctx, id,
|
|
1015
|
+
return assignRef(ctx, id, serializeDictionary(ctx, id, value));
|
|
1163
1016
|
default:
|
|
1164
1017
|
throw new Error("Unsupported type");
|
|
1165
1018
|
}
|
|
1166
1019
|
}
|
|
1167
1020
|
|
|
1021
|
+
// src/tree/sync.ts
|
|
1022
|
+
function generateDictionary2(ctx, properties) {
|
|
1023
|
+
const keys = Object.keys(properties);
|
|
1024
|
+
const nodes = {};
|
|
1025
|
+
const deferred = {};
|
|
1026
|
+
for (const key of keys) {
|
|
1027
|
+
if (isIterable(properties[key])) {
|
|
1028
|
+
deferred[key] = properties[key];
|
|
1029
|
+
} else {
|
|
1030
|
+
nodes[key] = generateTreeSync(ctx, properties[key]);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
for (const key of Object.keys(deferred)) {
|
|
1034
|
+
nodes[key] = generateTreeSync(ctx, deferred[key]);
|
|
1035
|
+
}
|
|
1036
|
+
return nodes;
|
|
1037
|
+
}
|
|
1038
|
+
function generateSetNode2(ctx, current, id) {
|
|
1039
|
+
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
1040
|
+
const len = current.size;
|
|
1041
|
+
const nodes = new Array(len);
|
|
1042
|
+
const deferred = new Array(len);
|
|
1043
|
+
let deferredSize = 0;
|
|
1044
|
+
let nodeSize = 0;
|
|
1045
|
+
for (const item of current.keys()) {
|
|
1046
|
+
if (isIterable(item)) {
|
|
1047
|
+
deferred[deferredSize++] = item;
|
|
1048
|
+
} else {
|
|
1049
|
+
nodes[nodeSize++] = generateTreeSync(ctx, item);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1053
|
+
nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
|
|
1054
|
+
}
|
|
1055
|
+
return [5 /* Set */, nodes, id];
|
|
1056
|
+
}
|
|
1057
|
+
function generateMapNode2(ctx, current, id) {
|
|
1058
|
+
assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
|
|
1059
|
+
const len = current.size;
|
|
1060
|
+
const keyNodes = new Array(len);
|
|
1061
|
+
const valueNodes = new Array(len);
|
|
1062
|
+
const deferredKey = new Array(len);
|
|
1063
|
+
const deferredValue = new Array(len);
|
|
1064
|
+
let deferredSize = 0;
|
|
1065
|
+
let nodeSize = 0;
|
|
1066
|
+
for (const [key, value] of current.entries()) {
|
|
1067
|
+
if (isIterable(key) || isIterable(value)) {
|
|
1068
|
+
deferredKey[deferredSize] = key;
|
|
1069
|
+
deferredValue[deferredSize] = value;
|
|
1070
|
+
deferredSize++;
|
|
1071
|
+
} else {
|
|
1072
|
+
keyNodes[nodeSize] = generateTreeSync(ctx, key);
|
|
1073
|
+
valueNodes[nodeSize] = generateTreeSync(ctx, value);
|
|
1074
|
+
nodeSize++;
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1078
|
+
keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
|
|
1079
|
+
valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
|
|
1080
|
+
}
|
|
1081
|
+
return [6 /* Map */, [keyNodes, valueNodes, len], id];
|
|
1082
|
+
}
|
|
1083
|
+
function generateNodeList2(ctx, current) {
|
|
1084
|
+
const size = current.length;
|
|
1085
|
+
const nodes = new Array(size);
|
|
1086
|
+
const deferred = new Array(size);
|
|
1087
|
+
for (let i = 0; i < size; i++) {
|
|
1088
|
+
if (i in current) {
|
|
1089
|
+
if (isIterable(current[i])) {
|
|
1090
|
+
deferred[i] = current[i];
|
|
1091
|
+
} else {
|
|
1092
|
+
nodes[i] = generateTreeSync(ctx, current[i]);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
for (let i = 0; i < size; i++) {
|
|
1097
|
+
if (i in deferred) {
|
|
1098
|
+
nodes[i] = generateTreeSync(ctx, deferred[i]);
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
return nodes;
|
|
1102
|
+
}
|
|
1103
|
+
function generateArrayNode2(ctx, current, id) {
|
|
1104
|
+
return [7 /* Array */, generateNodeList2(ctx, current), id];
|
|
1105
|
+
}
|
|
1106
|
+
function generateAggregateErrorNode2(ctx, current, id) {
|
|
1107
|
+
const options = getErrorOptions(current);
|
|
1108
|
+
const optionsNode = options ? generateDictionary2(ctx, options) : void 0;
|
|
1109
|
+
const errorsNode = generateTreeSync(ctx, current.errors);
|
|
1110
|
+
return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
1111
|
+
}
|
|
1112
|
+
function generateErrorNode2(ctx, current, id) {
|
|
1113
|
+
const options = getErrorOptions(current);
|
|
1114
|
+
const optionsNode = options ? generateDictionary2(ctx, options) : void 0;
|
|
1115
|
+
return [
|
|
1116
|
+
11 /* Error */,
|
|
1117
|
+
[getErrorConstructor(current), current.message, optionsNode],
|
|
1118
|
+
id
|
|
1119
|
+
];
|
|
1120
|
+
}
|
|
1121
|
+
function generateIterableNode2(ctx, current, id) {
|
|
1122
|
+
assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
|
|
1123
|
+
const options = getIterableOptions(current);
|
|
1124
|
+
return [13 /* Iterable */, [
|
|
1125
|
+
// Parse options first before the items
|
|
1126
|
+
options ? generateDictionary2(ctx, options) : void 0,
|
|
1127
|
+
generateNodeList2(ctx, Array.from(current))
|
|
1128
|
+
], id];
|
|
1129
|
+
}
|
|
1130
|
+
function generateTreeSync(ctx, current) {
|
|
1131
|
+
if (isPrimitive(current)) {
|
|
1132
|
+
return [0 /* Primitive */, serializePrimitive(current)];
|
|
1133
|
+
}
|
|
1134
|
+
const id = generateRef(ctx, current);
|
|
1135
|
+
if (Array.isArray(id)) {
|
|
1136
|
+
return id;
|
|
1137
|
+
}
|
|
1138
|
+
const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
|
|
1139
|
+
if (semiPrimitive) {
|
|
1140
|
+
return semiPrimitive;
|
|
1141
|
+
}
|
|
1142
|
+
if (constructorCheck(current, Set)) {
|
|
1143
|
+
return generateSetNode2(ctx, current, id);
|
|
1144
|
+
}
|
|
1145
|
+
if (constructorCheck(current, Map)) {
|
|
1146
|
+
return generateMapNode2(ctx, current, id);
|
|
1147
|
+
}
|
|
1148
|
+
if (Array.isArray(current)) {
|
|
1149
|
+
return generateArrayNode2(ctx, current, id);
|
|
1150
|
+
}
|
|
1151
|
+
if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
|
|
1152
|
+
return generateAggregateErrorNode2(ctx, current, id);
|
|
1153
|
+
}
|
|
1154
|
+
if (current instanceof Error) {
|
|
1155
|
+
return generateErrorNode2(ctx, current, id);
|
|
1156
|
+
}
|
|
1157
|
+
if (isIterable(current)) {
|
|
1158
|
+
return generateIterableNode2(ctx, current, id);
|
|
1159
|
+
}
|
|
1160
|
+
const empty = current.constructor == null;
|
|
1161
|
+
if (current.constructor === Object || empty) {
|
|
1162
|
+
return [
|
|
1163
|
+
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
1164
|
+
generateDictionary2(ctx, current),
|
|
1165
|
+
id
|
|
1166
|
+
];
|
|
1167
|
+
}
|
|
1168
|
+
throw new Error("Unsupported value");
|
|
1169
|
+
}
|
|
1170
|
+
function parseSync(ctx, current) {
|
|
1171
|
+
const result = generateTreeSync(ctx, current);
|
|
1172
|
+
return [result, createRef(ctx, current), result[0] === 8 /* Object */];
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1168
1175
|
// src/index.ts
|
|
1169
1176
|
function finalize(ctx, rootID, isObject, result) {
|
|
1170
1177
|
if (ctx.vars.length) {
|