seroval 0.4.0-alpha.13 → 0.4.0-alpha.15
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 +771 -717
- package/dist/cjs/development/index.cjs.map +3 -3
- package/dist/cjs/production/index.cjs +2 -2
- package/dist/esm/development/index.mjs +771 -719
- package/dist/esm/development/index.mjs.map +3 -3
- package/dist/esm/production/index.mjs +2 -2
- package/dist/types/context.d.ts +25 -23
- package/dist/types/tree/primitives.d.ts +10 -1
- package/dist/types/tree/serialize.d.ts +18 -2
- package/dist/types/tree/shared.d.ts +0 -3
- package/dist/types/tree/types.d.ts +38 -57
- package/package.json +2 -2
- package/src/compat.ts +8 -4
- package/src/context.ts +53 -49
- package/src/index.ts +44 -44
- package/src/quote.ts +1 -1
- package/src/tree/async.ts +191 -103
- package/src/tree/primitives.ts +144 -7
- package/src/tree/serialize.ts +388 -360
- package/src/tree/shared.ts +0 -13
- package/src/tree/sync.ts +153 -94
- package/src/tree/types.ts +53 -202
|
@@ -3,7 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
6
|
var __export = (target, all) => {
|
|
8
7
|
for (var name in all)
|
|
9
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -17,10 +16,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
16
|
return to;
|
|
18
17
|
};
|
|
19
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
var __publicField = (obj, key, value) => {
|
|
21
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
|
-
return value;
|
|
23
|
-
};
|
|
24
19
|
|
|
25
20
|
// src/index.ts
|
|
26
21
|
var src_exports = {};
|
|
@@ -218,8 +213,9 @@ var platforms = [
|
|
|
218
213
|
"samsung"
|
|
219
214
|
];
|
|
220
215
|
function getPlatform(target) {
|
|
216
|
+
let platform;
|
|
221
217
|
for (let i = 0, len = platforms.length; i < len; i++) {
|
|
222
|
-
|
|
218
|
+
platform = target.substring(0, platforms[i].length);
|
|
223
219
|
if (platform === platforms[i]) {
|
|
224
220
|
return platform;
|
|
225
221
|
}
|
|
@@ -245,11 +241,14 @@ function getTargetVersions(targets) {
|
|
|
245
241
|
function parseTargets(targets) {
|
|
246
242
|
const parsed = getTargetVersions(targets);
|
|
247
243
|
let flags = 0;
|
|
244
|
+
let base;
|
|
245
|
+
let flag;
|
|
246
|
+
let baseVersion;
|
|
248
247
|
for (const key of Object.keys(VERSION_TABLE)) {
|
|
249
|
-
|
|
250
|
-
|
|
248
|
+
base = VERSION_TABLE[key];
|
|
249
|
+
flag = true;
|
|
251
250
|
for (const [platform, version] of parsed) {
|
|
252
|
-
|
|
251
|
+
baseVersion = base[platform];
|
|
253
252
|
flag = flag && !!baseVersion && compareVersion(baseVersion, version) <= 0;
|
|
254
253
|
}
|
|
255
254
|
if (flag) {
|
|
@@ -280,35 +279,27 @@ function getIdentifier(index) {
|
|
|
280
279
|
var DEFAULT_OPTIONS = {
|
|
281
280
|
target: "es2023"
|
|
282
281
|
};
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
// Supported features
|
|
303
|
-
__publicField(this, "features");
|
|
304
|
-
// Refs that are...referenced
|
|
305
|
-
__publicField(this, "markedRefs");
|
|
306
|
-
this.features = options.features;
|
|
307
|
-
this.markedRefs = options.markedRefs;
|
|
308
|
-
}
|
|
309
|
-
};
|
|
282
|
+
function createParserContext(options = {}) {
|
|
283
|
+
const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
|
|
284
|
+
return {
|
|
285
|
+
markedRefs: /* @__PURE__ */ new Set(),
|
|
286
|
+
refs: /* @__PURE__ */ new Map(),
|
|
287
|
+
features: parseTargets(result.target)
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
function createSerializationContext(options) {
|
|
291
|
+
return {
|
|
292
|
+
stack: [],
|
|
293
|
+
vars: [],
|
|
294
|
+
assignments: [],
|
|
295
|
+
validRefs: [],
|
|
296
|
+
refSize: 0,
|
|
297
|
+
features: options.features,
|
|
298
|
+
markedRefs: new Set(options.markedRefs)
|
|
299
|
+
};
|
|
300
|
+
}
|
|
310
301
|
function markRef(ctx, current) {
|
|
311
|
-
ctx.markedRefs
|
|
302
|
+
ctx.markedRefs.add(current);
|
|
312
303
|
}
|
|
313
304
|
function createValidRef(ctx, index) {
|
|
314
305
|
const current = ctx.validRefs[index];
|
|
@@ -387,201 +378,123 @@ function quote(str) {
|
|
|
387
378
|
} else {
|
|
388
379
|
result += str.slice(lastPos);
|
|
389
380
|
}
|
|
390
|
-
return
|
|
381
|
+
return '"' + result + '"';
|
|
391
382
|
}
|
|
392
383
|
|
|
393
|
-
// src/tree/types.ts
|
|
394
|
-
var SerovalObjectRecordNode = class {
|
|
395
|
-
constructor(k, v, s) {
|
|
396
|
-
__publicField(this, "k");
|
|
397
|
-
__publicField(this, "v");
|
|
398
|
-
__publicField(this, "s");
|
|
399
|
-
this.k = k;
|
|
400
|
-
this.v = v;
|
|
401
|
-
this.s = s;
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
var SerovalMapRecordNode = class {
|
|
405
|
-
constructor(k, v, s) {
|
|
406
|
-
__publicField(this, "k");
|
|
407
|
-
__publicField(this, "v");
|
|
408
|
-
__publicField(this, "s");
|
|
409
|
-
this.k = k;
|
|
410
|
-
this.v = v;
|
|
411
|
-
this.s = s;
|
|
412
|
-
}
|
|
413
|
-
};
|
|
414
|
-
var SerovalPrimitiveNode = class {
|
|
415
|
-
constructor(current) {
|
|
416
|
-
__publicField(this, "t", 0 /* Primitive */);
|
|
417
|
-
__publicField(this, "s");
|
|
418
|
-
this.s = current;
|
|
419
|
-
}
|
|
420
|
-
};
|
|
421
|
-
var SerovalReferenceNode = class {
|
|
422
|
-
constructor(id) {
|
|
423
|
-
__publicField(this, "t", 2 /* Reference */);
|
|
424
|
-
__publicField(this, "i");
|
|
425
|
-
this.i = id;
|
|
426
|
-
}
|
|
427
|
-
};
|
|
428
|
-
var SerovalBigIntNode = class {
|
|
429
|
-
constructor(current) {
|
|
430
|
-
__publicField(this, "t", 1 /* BigInt */);
|
|
431
|
-
__publicField(this, "s");
|
|
432
|
-
this.s = `${current}n`;
|
|
433
|
-
}
|
|
434
|
-
};
|
|
435
|
-
var SerovalDateNode = class {
|
|
436
|
-
constructor(id, current) {
|
|
437
|
-
__publicField(this, "t", 3 /* Date */);
|
|
438
|
-
__publicField(this, "i");
|
|
439
|
-
__publicField(this, "s");
|
|
440
|
-
this.i = id;
|
|
441
|
-
this.s = current.toISOString();
|
|
442
|
-
}
|
|
443
|
-
};
|
|
444
|
-
var SerovalRegExpNode = class {
|
|
445
|
-
constructor(id, current) {
|
|
446
|
-
__publicField(this, "t", 4 /* RegExp */);
|
|
447
|
-
__publicField(this, "i");
|
|
448
|
-
__publicField(this, "s");
|
|
449
|
-
this.i = id;
|
|
450
|
-
this.s = String(current);
|
|
451
|
-
}
|
|
452
|
-
};
|
|
453
|
-
var SerovalTypedArrayNode = class {
|
|
454
|
-
constructor(id, current) {
|
|
455
|
-
__publicField(this, "t", 14 /* TypedArray */);
|
|
456
|
-
__publicField(this, "i");
|
|
457
|
-
__publicField(this, "s");
|
|
458
|
-
__publicField(this, "l");
|
|
459
|
-
__publicField(this, "c");
|
|
460
|
-
this.i = id;
|
|
461
|
-
this.s = current.toString();
|
|
462
|
-
this.l = current.byteOffset;
|
|
463
|
-
this.c = current.constructor.name;
|
|
464
|
-
}
|
|
465
|
-
};
|
|
466
|
-
var SerovalBigIntTypedArrayNode = class {
|
|
467
|
-
constructor(id, current) {
|
|
468
|
-
__publicField(this, "t", 15 /* BigIntTypedArray */);
|
|
469
|
-
__publicField(this, "i");
|
|
470
|
-
__publicField(this, "s");
|
|
471
|
-
__publicField(this, "l");
|
|
472
|
-
__publicField(this, "c");
|
|
473
|
-
let result = "";
|
|
474
|
-
const cap = current.length - 1;
|
|
475
|
-
for (let i = 0; i < cap; i++) {
|
|
476
|
-
result += `${current[i]}n,`;
|
|
477
|
-
}
|
|
478
|
-
result += `"${current[cap]}"`;
|
|
479
|
-
this.i = id;
|
|
480
|
-
this.s = result;
|
|
481
|
-
this.l = current.byteOffset;
|
|
482
|
-
this.c = current.constructor.name;
|
|
483
|
-
}
|
|
484
|
-
};
|
|
485
|
-
var SerovalSetNode = class {
|
|
486
|
-
constructor(id, a) {
|
|
487
|
-
__publicField(this, "t", 5 /* Set */);
|
|
488
|
-
__publicField(this, "i");
|
|
489
|
-
__publicField(this, "a");
|
|
490
|
-
this.i = id;
|
|
491
|
-
this.a = a;
|
|
492
|
-
}
|
|
493
|
-
};
|
|
494
|
-
var SerovalMapNode = class {
|
|
495
|
-
constructor(id, d) {
|
|
496
|
-
__publicField(this, "t", 6 /* Map */);
|
|
497
|
-
__publicField(this, "i");
|
|
498
|
-
__publicField(this, "d");
|
|
499
|
-
this.i = id;
|
|
500
|
-
this.d = d;
|
|
501
|
-
}
|
|
502
|
-
};
|
|
503
|
-
var SerovalArrayNode = class {
|
|
504
|
-
constructor(id, a) {
|
|
505
|
-
__publicField(this, "t", 7 /* Array */);
|
|
506
|
-
__publicField(this, "i");
|
|
507
|
-
__publicField(this, "a");
|
|
508
|
-
this.i = id;
|
|
509
|
-
this.a = a;
|
|
510
|
-
}
|
|
511
|
-
};
|
|
512
|
-
var SerovalObjectNode = class {
|
|
513
|
-
constructor(id, d) {
|
|
514
|
-
__publicField(this, "t", 8 /* Object */);
|
|
515
|
-
__publicField(this, "i");
|
|
516
|
-
__publicField(this, "d");
|
|
517
|
-
this.i = id;
|
|
518
|
-
this.d = d;
|
|
519
|
-
}
|
|
520
|
-
};
|
|
521
|
-
var SerovalNullConstructorNode = class {
|
|
522
|
-
constructor(id, d) {
|
|
523
|
-
__publicField(this, "t", 9 /* NullConstructor */);
|
|
524
|
-
__publicField(this, "i");
|
|
525
|
-
__publicField(this, "d");
|
|
526
|
-
this.i = id;
|
|
527
|
-
this.d = d;
|
|
528
|
-
}
|
|
529
|
-
};
|
|
530
|
-
var SerovalPromiseNode = class {
|
|
531
|
-
constructor(id, n) {
|
|
532
|
-
__publicField(this, "t", 10 /* Promise */);
|
|
533
|
-
__publicField(this, "i");
|
|
534
|
-
__publicField(this, "n");
|
|
535
|
-
this.i = id;
|
|
536
|
-
this.n = n;
|
|
537
|
-
}
|
|
538
|
-
};
|
|
539
|
-
var SerovalErrorNode = class {
|
|
540
|
-
constructor(id, c, m, d) {
|
|
541
|
-
__publicField(this, "t", 11 /* Error */);
|
|
542
|
-
__publicField(this, "i");
|
|
543
|
-
__publicField(this, "c");
|
|
544
|
-
__publicField(this, "m");
|
|
545
|
-
__publicField(this, "d");
|
|
546
|
-
this.i = id;
|
|
547
|
-
this.c = c;
|
|
548
|
-
this.m = m;
|
|
549
|
-
this.d = d;
|
|
550
|
-
}
|
|
551
|
-
};
|
|
552
|
-
var SerovalAggregateErrorNode = class {
|
|
553
|
-
constructor(id, m, d, n) {
|
|
554
|
-
__publicField(this, "t", 12 /* AggregateError */);
|
|
555
|
-
__publicField(this, "i");
|
|
556
|
-
__publicField(this, "m");
|
|
557
|
-
__publicField(this, "d");
|
|
558
|
-
__publicField(this, "n");
|
|
559
|
-
this.i = id;
|
|
560
|
-
this.m = m;
|
|
561
|
-
this.d = d;
|
|
562
|
-
this.n = n;
|
|
563
|
-
}
|
|
564
|
-
};
|
|
565
|
-
var SerovalIterableNode = class {
|
|
566
|
-
constructor(id, d, a) {
|
|
567
|
-
__publicField(this, "t", 13 /* Iterable */);
|
|
568
|
-
__publicField(this, "i");
|
|
569
|
-
__publicField(this, "d");
|
|
570
|
-
__publicField(this, "a");
|
|
571
|
-
this.i = id;
|
|
572
|
-
this.d = d;
|
|
573
|
-
this.a = a;
|
|
574
|
-
}
|
|
575
|
-
};
|
|
576
|
-
|
|
577
384
|
// src/tree/primitives.ts
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
385
|
+
function createPrimitiveNode(value) {
|
|
386
|
+
return {
|
|
387
|
+
t: 0 /* Primitive */,
|
|
388
|
+
i: void 0,
|
|
389
|
+
s: value,
|
|
390
|
+
l: void 0,
|
|
391
|
+
c: void 0,
|
|
392
|
+
m: void 0,
|
|
393
|
+
d: void 0,
|
|
394
|
+
a: void 0,
|
|
395
|
+
n: void 0
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
var TRUE_NODE = createPrimitiveNode("!0");
|
|
399
|
+
var FALSE_NODE = createPrimitiveNode("!1");
|
|
400
|
+
var UNDEFINED_NODE = createPrimitiveNode("void 0");
|
|
401
|
+
var NULL_NODE = createPrimitiveNode(null);
|
|
402
|
+
var NEG_ZERO_NODE = createPrimitiveNode("-0");
|
|
403
|
+
var INFINITY_NODE = createPrimitiveNode("1/0");
|
|
404
|
+
var NEG_INFINITY_NODE = createPrimitiveNode("-1/0");
|
|
405
|
+
function createBigIntNode(ctx, current) {
|
|
406
|
+
assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
|
|
407
|
+
return {
|
|
408
|
+
t: 1 /* BigInt */,
|
|
409
|
+
i: void 0,
|
|
410
|
+
s: current.toString() + "n",
|
|
411
|
+
l: void 0,
|
|
412
|
+
c: void 0,
|
|
413
|
+
m: void 0,
|
|
414
|
+
d: void 0,
|
|
415
|
+
a: void 0,
|
|
416
|
+
n: void 0
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
function createReferenceNode(id) {
|
|
420
|
+
return {
|
|
421
|
+
t: 2 /* Reference */,
|
|
422
|
+
i: id,
|
|
423
|
+
s: void 0,
|
|
424
|
+
l: void 0,
|
|
425
|
+
c: void 0,
|
|
426
|
+
m: void 0,
|
|
427
|
+
d: void 0,
|
|
428
|
+
a: void 0,
|
|
429
|
+
n: void 0
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
function createDateNode(id, current) {
|
|
433
|
+
return {
|
|
434
|
+
t: 3 /* Date */,
|
|
435
|
+
i: id,
|
|
436
|
+
s: current.toISOString(),
|
|
437
|
+
l: void 0,
|
|
438
|
+
c: void 0,
|
|
439
|
+
m: void 0,
|
|
440
|
+
d: void 0,
|
|
441
|
+
n: void 0,
|
|
442
|
+
a: void 0
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
function createRegExpNode(id, current) {
|
|
446
|
+
return {
|
|
447
|
+
t: 4 /* RegExp */,
|
|
448
|
+
i: id,
|
|
449
|
+
s: String(current),
|
|
450
|
+
l: void 0,
|
|
451
|
+
c: void 0,
|
|
452
|
+
m: void 0,
|
|
453
|
+
d: void 0,
|
|
454
|
+
a: void 0,
|
|
455
|
+
n: void 0
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
function createTypedArrayNode(ctx, id, current) {
|
|
459
|
+
const constructor = current.constructor.name;
|
|
460
|
+
assert(ctx.features & 1024 /* TypedArray */, `Unsupported value type "${constructor}"`);
|
|
461
|
+
return {
|
|
462
|
+
t: 14 /* TypedArray */,
|
|
463
|
+
i: id,
|
|
464
|
+
s: current.toString(),
|
|
465
|
+
l: current.byteOffset,
|
|
466
|
+
c: constructor,
|
|
467
|
+
m: void 0,
|
|
468
|
+
d: void 0,
|
|
469
|
+
a: void 0,
|
|
470
|
+
n: void 0
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
function createBigIntTypedArrayNode(ctx, id, current) {
|
|
474
|
+
const constructor = current.constructor.name;
|
|
475
|
+
assert(
|
|
476
|
+
ctx.features & 2048 /* BigIntTypedArray */,
|
|
477
|
+
`Unsupported value type "${constructor}"`
|
|
478
|
+
);
|
|
479
|
+
let result = "";
|
|
480
|
+
for (let i = 0, len = current.length; i < len; i++) {
|
|
481
|
+
if (i !== 0) {
|
|
482
|
+
result += ",";
|
|
483
|
+
}
|
|
484
|
+
result += current[i].toString() + "n";
|
|
485
|
+
}
|
|
486
|
+
return {
|
|
487
|
+
t: 15 /* BigIntTypedArray */,
|
|
488
|
+
i: id,
|
|
489
|
+
s: result,
|
|
490
|
+
l: current.byteOffset,
|
|
491
|
+
c: constructor,
|
|
492
|
+
m: void 0,
|
|
493
|
+
d: void 0,
|
|
494
|
+
a: void 0,
|
|
495
|
+
n: void 0
|
|
496
|
+
};
|
|
497
|
+
}
|
|
585
498
|
|
|
586
499
|
// src/tree/shared.ts
|
|
587
500
|
function getErrorConstructor(error) {
|
|
@@ -633,9 +546,6 @@ function getIterableOptions(obj) {
|
|
|
633
546
|
}
|
|
634
547
|
return void 0;
|
|
635
548
|
}
|
|
636
|
-
function isReferenceInStack(ctx, node) {
|
|
637
|
-
return node.t === 2 /* Reference */ && ctx.stack.includes(node.i);
|
|
638
|
-
}
|
|
639
549
|
function isIterable(value) {
|
|
640
550
|
if (!value || typeof value !== "object") {
|
|
641
551
|
return false;
|
|
@@ -666,11 +576,7 @@ function isIterable(value) {
|
|
|
666
576
|
|
|
667
577
|
// src/tree/async.ts
|
|
668
578
|
var AsyncParser = class {
|
|
669
|
-
|
|
670
|
-
__publicField(this, "ctx");
|
|
671
|
-
this.ctx = ctx;
|
|
672
|
-
}
|
|
673
|
-
async generateNodeList(current) {
|
|
579
|
+
static async generateNodeList(ctx, current) {
|
|
674
580
|
const size = current.length;
|
|
675
581
|
const nodes = new Array(size);
|
|
676
582
|
const deferred = new Array(size);
|
|
@@ -681,22 +587,32 @@ var AsyncParser = class {
|
|
|
681
587
|
if (isIterable(item)) {
|
|
682
588
|
deferred[i] = item;
|
|
683
589
|
} else {
|
|
684
|
-
nodes[i] = await this.parse(item);
|
|
590
|
+
nodes[i] = await this.parse(ctx, item);
|
|
685
591
|
}
|
|
686
592
|
}
|
|
687
593
|
}
|
|
688
594
|
for (let i = 0; i < size; i++) {
|
|
689
595
|
if (i in deferred) {
|
|
690
|
-
nodes[i] = await this.parse(deferred[i]);
|
|
596
|
+
nodes[i] = await this.parse(ctx, deferred[i]);
|
|
691
597
|
}
|
|
692
598
|
}
|
|
693
599
|
return nodes;
|
|
694
600
|
}
|
|
695
|
-
async generateArrayNode(id, current) {
|
|
696
|
-
return
|
|
601
|
+
static async generateArrayNode(ctx, id, current) {
|
|
602
|
+
return {
|
|
603
|
+
t: 7 /* Array */,
|
|
604
|
+
i: id,
|
|
605
|
+
s: void 0,
|
|
606
|
+
l: void 0,
|
|
607
|
+
c: void 0,
|
|
608
|
+
m: void 0,
|
|
609
|
+
d: void 0,
|
|
610
|
+
a: await this.generateNodeList(ctx, current),
|
|
611
|
+
n: void 0
|
|
612
|
+
};
|
|
697
613
|
}
|
|
698
|
-
async generateMapNode(id, current) {
|
|
699
|
-
assert(
|
|
614
|
+
static async generateMapNode(ctx, id, current) {
|
|
615
|
+
assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
|
|
700
616
|
const len = current.size;
|
|
701
617
|
const keyNodes = new Array(len);
|
|
702
618
|
const valueNodes = new Array(len);
|
|
@@ -710,19 +626,29 @@ var AsyncParser = class {
|
|
|
710
626
|
deferredValue[deferredSize] = value;
|
|
711
627
|
deferredSize++;
|
|
712
628
|
} else {
|
|
713
|
-
keyNodes[nodeSize] = await this.parse(key);
|
|
714
|
-
valueNodes[nodeSize] = await this.parse(value);
|
|
629
|
+
keyNodes[nodeSize] = await this.parse(ctx, key);
|
|
630
|
+
valueNodes[nodeSize] = await this.parse(ctx, value);
|
|
715
631
|
nodeSize++;
|
|
716
632
|
}
|
|
717
633
|
}
|
|
718
634
|
for (let i = 0; i < deferredSize; i++) {
|
|
719
|
-
keyNodes[nodeSize + i] = await this.parse(deferredKey[i]);
|
|
720
|
-
valueNodes[nodeSize + i] = await this.parse(deferredValue[i]);
|
|
635
|
+
keyNodes[nodeSize + i] = await this.parse(ctx, deferredKey[i]);
|
|
636
|
+
valueNodes[nodeSize + i] = await this.parse(ctx, deferredValue[i]);
|
|
721
637
|
}
|
|
722
|
-
return
|
|
638
|
+
return {
|
|
639
|
+
t: 6 /* Map */,
|
|
640
|
+
i: id,
|
|
641
|
+
s: void 0,
|
|
642
|
+
l: void 0,
|
|
643
|
+
c: void 0,
|
|
644
|
+
m: void 0,
|
|
645
|
+
d: { k: keyNodes, v: valueNodes, s: len },
|
|
646
|
+
a: void 0,
|
|
647
|
+
n: void 0
|
|
648
|
+
};
|
|
723
649
|
}
|
|
724
|
-
async generateSetNode(id, current) {
|
|
725
|
-
assert(
|
|
650
|
+
static async generateSetNode(ctx, id, current) {
|
|
651
|
+
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
726
652
|
const len = current.size;
|
|
727
653
|
const nodes = new Array(len);
|
|
728
654
|
const deferred = new Array(len);
|
|
@@ -732,15 +658,25 @@ var AsyncParser = class {
|
|
|
732
658
|
if (isIterable(item)) {
|
|
733
659
|
deferred[deferredSize++] = item;
|
|
734
660
|
} else {
|
|
735
|
-
nodes[nodeSize++] = await this.parse(item);
|
|
661
|
+
nodes[nodeSize++] = await this.parse(ctx, item);
|
|
736
662
|
}
|
|
737
663
|
}
|
|
738
664
|
for (let i = 0; i < deferredSize; i++) {
|
|
739
|
-
nodes[nodeSize + i] = await this.parse(deferred[i]);
|
|
665
|
+
nodes[nodeSize + i] = await this.parse(ctx, deferred[i]);
|
|
740
666
|
}
|
|
741
|
-
return
|
|
667
|
+
return {
|
|
668
|
+
t: 5 /* Set */,
|
|
669
|
+
i: id,
|
|
670
|
+
s: void 0,
|
|
671
|
+
l: void 0,
|
|
672
|
+
c: void 0,
|
|
673
|
+
m: void 0,
|
|
674
|
+
d: void 0,
|
|
675
|
+
a: nodes,
|
|
676
|
+
n: void 0
|
|
677
|
+
};
|
|
742
678
|
}
|
|
743
|
-
async generateProperties(properties) {
|
|
679
|
+
static async generateProperties(ctx, properties) {
|
|
744
680
|
const keys = Object.keys(properties);
|
|
745
681
|
const size = keys.length;
|
|
746
682
|
const keyNodes = new Array(size);
|
|
@@ -758,98 +694,139 @@ var AsyncParser = class {
|
|
|
758
694
|
deferredSize++;
|
|
759
695
|
} else {
|
|
760
696
|
keyNodes[nodesSize] = key;
|
|
761
|
-
valueNodes[nodesSize] = await this.parse(item);
|
|
697
|
+
valueNodes[nodesSize] = await this.parse(ctx, item);
|
|
762
698
|
nodesSize++;
|
|
763
699
|
}
|
|
764
700
|
}
|
|
765
701
|
for (let i = 0; i < deferredSize; i++) {
|
|
766
702
|
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
767
|
-
valueNodes[nodesSize + i] = await this.parse(deferredValues[i]);
|
|
703
|
+
valueNodes[nodesSize + i] = await this.parse(ctx, deferredValues[i]);
|
|
768
704
|
}
|
|
769
|
-
return
|
|
705
|
+
return {
|
|
706
|
+
k: keyNodes,
|
|
707
|
+
v: valueNodes,
|
|
708
|
+
s: size
|
|
709
|
+
};
|
|
770
710
|
}
|
|
771
|
-
async generateIterableNode(id, current) {
|
|
772
|
-
assert(
|
|
711
|
+
static async generateIterableNode(ctx, id, current) {
|
|
712
|
+
assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
|
|
773
713
|
const options = getIterableOptions(current);
|
|
774
|
-
return
|
|
775
|
-
|
|
714
|
+
return {
|
|
715
|
+
t: 13 /* Iterable */,
|
|
716
|
+
i: id,
|
|
717
|
+
s: void 0,
|
|
718
|
+
l: void 0,
|
|
719
|
+
c: void 0,
|
|
720
|
+
m: void 0,
|
|
776
721
|
// Parse options first before the items
|
|
777
|
-
options ? await this.generateProperties(options) : void 0,
|
|
778
|
-
await this.generateNodeList(Array.from(current))
|
|
779
|
-
|
|
722
|
+
d: options ? await this.generateProperties(ctx, options) : void 0,
|
|
723
|
+
a: await this.generateNodeList(ctx, Array.from(current)),
|
|
724
|
+
n: void 0
|
|
725
|
+
};
|
|
780
726
|
}
|
|
781
|
-
async generatePromiseNode(id, current) {
|
|
782
|
-
assert(
|
|
783
|
-
return current.then(async (value) =>
|
|
727
|
+
static async generatePromiseNode(ctx, id, current) {
|
|
728
|
+
assert(ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
|
|
729
|
+
return current.then(async (value) => ({
|
|
730
|
+
t: 10 /* Promise */,
|
|
731
|
+
i: id,
|
|
732
|
+
s: void 0,
|
|
733
|
+
l: void 0,
|
|
734
|
+
c: void 0,
|
|
735
|
+
m: void 0,
|
|
736
|
+
// Parse options first before the items
|
|
737
|
+
d: void 0,
|
|
738
|
+
a: void 0,
|
|
739
|
+
n: await this.parse(ctx, value)
|
|
740
|
+
}));
|
|
784
741
|
}
|
|
785
|
-
async generateObjectNode(id, current, empty) {
|
|
742
|
+
static async generateObjectNode(ctx, id, current, empty) {
|
|
786
743
|
if (Symbol.iterator in current) {
|
|
787
|
-
return this.generateIterableNode(id, current);
|
|
744
|
+
return this.generateIterableNode(ctx, id, current);
|
|
788
745
|
}
|
|
789
746
|
if ("then" in current && typeof current.then === "function") {
|
|
790
|
-
return this.generatePromiseNode(id, current);
|
|
791
|
-
}
|
|
792
|
-
const properties = await this.generateProperties(current);
|
|
793
|
-
if (empty) {
|
|
794
|
-
return new SerovalNullConstructorNode(id, properties);
|
|
747
|
+
return this.generatePromiseNode(ctx, id, current);
|
|
795
748
|
}
|
|
796
|
-
return
|
|
749
|
+
return {
|
|
750
|
+
t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
751
|
+
i: id,
|
|
752
|
+
s: void 0,
|
|
753
|
+
l: void 0,
|
|
754
|
+
c: void 0,
|
|
755
|
+
m: void 0,
|
|
756
|
+
d: await this.generateProperties(ctx, current),
|
|
757
|
+
a: void 0,
|
|
758
|
+
n: void 0
|
|
759
|
+
};
|
|
797
760
|
}
|
|
798
|
-
async generateAggregateErrorNode(id, current) {
|
|
761
|
+
static async generateAggregateErrorNode(ctx, id, current) {
|
|
799
762
|
const options = getErrorOptions(current);
|
|
800
|
-
const optionsNode = options ? await this.generateProperties(options) : void 0;
|
|
801
|
-
return
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
763
|
+
const optionsNode = options ? await this.generateProperties(ctx, options) : void 0;
|
|
764
|
+
return {
|
|
765
|
+
t: 12 /* AggregateError */,
|
|
766
|
+
i: id,
|
|
767
|
+
s: void 0,
|
|
768
|
+
l: void 0,
|
|
769
|
+
c: void 0,
|
|
770
|
+
m: current.message,
|
|
771
|
+
d: optionsNode,
|
|
772
|
+
a: void 0,
|
|
773
|
+
n: await this.parse(ctx, current.errors)
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
static async generateErrorNode(ctx, id, current) {
|
|
809
777
|
const options = getErrorOptions(current);
|
|
810
|
-
const optionsNode = options ? await this.generateProperties(options) : void 0;
|
|
811
|
-
return
|
|
778
|
+
const optionsNode = options ? await this.generateProperties(ctx, options) : void 0;
|
|
779
|
+
return {
|
|
780
|
+
t: 11 /* Error */,
|
|
781
|
+
i: id,
|
|
782
|
+
s: void 0,
|
|
783
|
+
l: void 0,
|
|
784
|
+
c: getErrorConstructor(current),
|
|
785
|
+
m: current.message,
|
|
786
|
+
d: optionsNode,
|
|
787
|
+
a: void 0,
|
|
788
|
+
n: void 0
|
|
789
|
+
};
|
|
812
790
|
}
|
|
813
|
-
async parse(current) {
|
|
791
|
+
static async parse(ctx, current) {
|
|
814
792
|
switch (typeof current) {
|
|
815
793
|
case "boolean":
|
|
816
794
|
return current ? TRUE_NODE : FALSE_NODE;
|
|
817
795
|
case "undefined":
|
|
818
796
|
return UNDEFINED_NODE;
|
|
819
797
|
case "string":
|
|
820
|
-
return
|
|
798
|
+
return createPrimitiveNode(quote(current));
|
|
821
799
|
case "number":
|
|
822
800
|
if (Object.is(current, -0)) {
|
|
823
801
|
return NEG_ZERO_NODE;
|
|
824
802
|
}
|
|
825
803
|
if (Object.is(current, Infinity)) {
|
|
826
|
-
return
|
|
804
|
+
return createPrimitiveNode("1/0");
|
|
827
805
|
}
|
|
828
806
|
if (Object.is(current, -Infinity)) {
|
|
829
|
-
return
|
|
807
|
+
return createPrimitiveNode("-1/0");
|
|
830
808
|
}
|
|
831
|
-
return
|
|
809
|
+
return createPrimitiveNode(current);
|
|
832
810
|
case "bigint":
|
|
833
|
-
|
|
834
|
-
return new SerovalBigIntNode(current);
|
|
811
|
+
return createBigIntNode(ctx, current);
|
|
835
812
|
case "object": {
|
|
836
813
|
if (!current) {
|
|
837
814
|
return NULL_NODE;
|
|
838
815
|
}
|
|
839
|
-
const id = createRef(
|
|
840
|
-
if (
|
|
841
|
-
return
|
|
816
|
+
const id = createRef(ctx, current, true);
|
|
817
|
+
if (ctx.markedRefs.has(id)) {
|
|
818
|
+
return createReferenceNode(id);
|
|
842
819
|
}
|
|
843
820
|
if (Array.isArray(current)) {
|
|
844
|
-
return this.generateArrayNode(id, current);
|
|
821
|
+
return this.generateArrayNode(ctx, id, current);
|
|
845
822
|
}
|
|
846
823
|
switch (current.constructor) {
|
|
847
824
|
case Date:
|
|
848
|
-
return
|
|
825
|
+
return createDateNode(id, current);
|
|
849
826
|
case RegExp:
|
|
850
|
-
return
|
|
827
|
+
return createRegExpNode(id, current);
|
|
851
828
|
case Promise:
|
|
852
|
-
return this.generatePromiseNode(id, current);
|
|
829
|
+
return this.generatePromiseNode(ctx, id, current);
|
|
853
830
|
case Int8Array:
|
|
854
831
|
case Int16Array:
|
|
855
832
|
case Int32Array:
|
|
@@ -859,28 +836,41 @@ var AsyncParser = class {
|
|
|
859
836
|
case Uint8ClampedArray:
|
|
860
837
|
case Float32Array:
|
|
861
838
|
case Float64Array:
|
|
862
|
-
|
|
863
|
-
return new SerovalTypedArrayNode(id, current);
|
|
839
|
+
return createTypedArrayNode(ctx, id, current);
|
|
864
840
|
case BigInt64Array:
|
|
865
841
|
case BigUint64Array:
|
|
866
|
-
|
|
867
|
-
this.ctx.features & 2048 /* BigIntTypedArray */,
|
|
868
|
-
`Unsupported value type "${current.constructor.name}"`
|
|
869
|
-
);
|
|
870
|
-
return new SerovalBigIntTypedArrayNode(id, current);
|
|
842
|
+
return createBigIntTypedArrayNode(ctx, id, current);
|
|
871
843
|
case Map:
|
|
872
|
-
return this.generateMapNode(
|
|
844
|
+
return this.generateMapNode(
|
|
845
|
+
ctx,
|
|
846
|
+
id,
|
|
847
|
+
current
|
|
848
|
+
);
|
|
873
849
|
case Set:
|
|
874
|
-
return this.generateSetNode(
|
|
850
|
+
return this.generateSetNode(
|
|
851
|
+
ctx,
|
|
852
|
+
id,
|
|
853
|
+
current
|
|
854
|
+
);
|
|
875
855
|
case Object:
|
|
876
|
-
return this.generateObjectNode(
|
|
856
|
+
return this.generateObjectNode(
|
|
857
|
+
ctx,
|
|
858
|
+
id,
|
|
859
|
+
current,
|
|
860
|
+
false
|
|
861
|
+
);
|
|
877
862
|
case void 0:
|
|
878
|
-
return this.generateObjectNode(
|
|
863
|
+
return this.generateObjectNode(
|
|
864
|
+
ctx,
|
|
865
|
+
id,
|
|
866
|
+
current,
|
|
867
|
+
true
|
|
868
|
+
);
|
|
879
869
|
case AggregateError:
|
|
880
|
-
if (
|
|
881
|
-
return this.generateAggregateErrorNode(id, current);
|
|
870
|
+
if (ctx.features & 1 /* AggregateError */) {
|
|
871
|
+
return this.generateAggregateErrorNode(ctx, id, current);
|
|
882
872
|
}
|
|
883
|
-
return this.generateErrorNode(id, current);
|
|
873
|
+
return this.generateErrorNode(ctx, id, current);
|
|
884
874
|
case Error:
|
|
885
875
|
case EvalError:
|
|
886
876
|
case RangeError:
|
|
@@ -888,24 +878,24 @@ var AsyncParser = class {
|
|
|
888
878
|
case SyntaxError:
|
|
889
879
|
case TypeError:
|
|
890
880
|
case URIError:
|
|
891
|
-
return this.generateErrorNode(id, current);
|
|
881
|
+
return this.generateErrorNode(ctx, id, current);
|
|
892
882
|
default:
|
|
893
883
|
break;
|
|
894
884
|
}
|
|
895
|
-
if (current instanceof AggregateError &&
|
|
896
|
-
return this.generateAggregateErrorNode(id, current);
|
|
885
|
+
if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
|
|
886
|
+
return this.generateAggregateErrorNode(ctx, id, current);
|
|
897
887
|
}
|
|
898
888
|
if (current instanceof Error) {
|
|
899
|
-
return this.generateErrorNode(id, current);
|
|
889
|
+
return this.generateErrorNode(ctx, id, current);
|
|
900
890
|
}
|
|
901
891
|
if (current instanceof Promise) {
|
|
902
|
-
return this.generatePromiseNode(id, current);
|
|
892
|
+
return this.generatePromiseNode(ctx, id, current);
|
|
903
893
|
}
|
|
904
894
|
if (Symbol.iterator in current) {
|
|
905
|
-
return this.generateIterableNode(id, current);
|
|
895
|
+
return this.generateIterableNode(ctx, id, current);
|
|
906
896
|
}
|
|
907
897
|
if ("then" in current && typeof current.then === "function") {
|
|
908
|
-
return this.generatePromiseNode(id, current);
|
|
898
|
+
return this.generatePromiseNode(ctx, id, current);
|
|
909
899
|
}
|
|
910
900
|
throw new Error("Unsupported type");
|
|
911
901
|
}
|
|
@@ -915,20 +905,20 @@ var AsyncParser = class {
|
|
|
915
905
|
}
|
|
916
906
|
};
|
|
917
907
|
async function parseAsync(ctx, current) {
|
|
918
|
-
const result = await
|
|
908
|
+
const result = await AsyncParser.parse(ctx, current);
|
|
919
909
|
const isObject = result.t === 8 /* Object */ || result.t === 13 /* Iterable */;
|
|
920
910
|
return [result, createRef(ctx, current, false), isObject];
|
|
921
911
|
}
|
|
922
912
|
|
|
923
913
|
// src/tree/serialize.ts
|
|
924
914
|
function getAssignmentExpression(assignment) {
|
|
925
|
-
switch (assignment.
|
|
915
|
+
switch (assignment.t) {
|
|
926
916
|
case "index":
|
|
927
|
-
return
|
|
917
|
+
return assignment.s + "=" + assignment.v;
|
|
928
918
|
case "map":
|
|
929
|
-
return
|
|
919
|
+
return assignment.s + ".set(" + assignment.k + "," + assignment.v + ")";
|
|
930
920
|
case "set":
|
|
931
|
-
return
|
|
921
|
+
return assignment.s + ".add(" + assignment.v + ")";
|
|
932
922
|
default:
|
|
933
923
|
return "";
|
|
934
924
|
}
|
|
@@ -937,27 +927,30 @@ function mergeAssignments(assignments) {
|
|
|
937
927
|
const newAssignments = [];
|
|
938
928
|
let current = assignments[0];
|
|
939
929
|
let prev = current;
|
|
930
|
+
let item;
|
|
940
931
|
for (let i = 1, len = assignments.length; i < len; i++) {
|
|
941
|
-
|
|
942
|
-
if (item.
|
|
943
|
-
if (item.
|
|
932
|
+
item = assignments[i];
|
|
933
|
+
if (item.t === prev.t) {
|
|
934
|
+
if (item.t === "index" && item.v === prev.v) {
|
|
944
935
|
current = {
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
936
|
+
t: "index",
|
|
937
|
+
s: item.s,
|
|
938
|
+
k: void 0,
|
|
939
|
+
v: getAssignmentExpression(current)
|
|
948
940
|
};
|
|
949
|
-
} else if (item.
|
|
941
|
+
} else if (item.t === "map" && item.s === prev.s) {
|
|
950
942
|
current = {
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
943
|
+
t: "map",
|
|
944
|
+
s: getAssignmentExpression(current),
|
|
945
|
+
k: item.k,
|
|
946
|
+
v: item.v
|
|
955
947
|
};
|
|
956
|
-
} else if (item.
|
|
948
|
+
} else if (item.t === "set" && item.s === prev.s) {
|
|
957
949
|
current = {
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
950
|
+
t: "set",
|
|
951
|
+
s: getAssignmentExpression(current),
|
|
952
|
+
k: void 0,
|
|
953
|
+
v: item.v
|
|
961
954
|
};
|
|
962
955
|
} else {
|
|
963
956
|
newAssignments.push(current);
|
|
@@ -977,7 +970,7 @@ function resolveAssignments(assignments) {
|
|
|
977
970
|
let result = "";
|
|
978
971
|
const merged = mergeAssignments(assignments);
|
|
979
972
|
for (let i = 0, len = merged.length; i < len; i++) {
|
|
980
|
-
result +=
|
|
973
|
+
result += getAssignmentExpression(merged[i]) + ",";
|
|
981
974
|
}
|
|
982
975
|
return result;
|
|
983
976
|
}
|
|
@@ -986,328 +979,338 @@ function resolveAssignments(assignments) {
|
|
|
986
979
|
function resolvePatches(ctx) {
|
|
987
980
|
return resolveAssignments(ctx.assignments);
|
|
988
981
|
}
|
|
989
|
-
function assignRef(ctx, index, value) {
|
|
990
|
-
if (ctx.markedRefs[index]) {
|
|
991
|
-
return `${getRefParam(ctx, index)}=${value}`;
|
|
992
|
-
}
|
|
993
|
-
return value;
|
|
994
|
-
}
|
|
995
982
|
function createAssignment(ctx, source, value) {
|
|
996
983
|
ctx.assignments.push({
|
|
997
|
-
|
|
998
|
-
source,
|
|
999
|
-
|
|
984
|
+
t: "index",
|
|
985
|
+
s: source,
|
|
986
|
+
k: void 0,
|
|
987
|
+
v: value
|
|
1000
988
|
});
|
|
1001
989
|
}
|
|
1002
990
|
function createSetAdd(ctx, ref, value) {
|
|
1003
991
|
markRef(ctx, ref);
|
|
1004
992
|
ctx.assignments.push({
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
993
|
+
t: "set",
|
|
994
|
+
s: getRefParam(ctx, ref),
|
|
995
|
+
k: void 0,
|
|
996
|
+
v: value
|
|
1008
997
|
});
|
|
1009
998
|
}
|
|
1010
999
|
function createMapSet(ctx, ref, key, value) {
|
|
1011
1000
|
markRef(ctx, ref);
|
|
1012
1001
|
ctx.assignments.push({
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
key,
|
|
1016
|
-
value
|
|
1002
|
+
t: "map",
|
|
1003
|
+
s: getRefParam(ctx, ref),
|
|
1004
|
+
k: key,
|
|
1005
|
+
v: value
|
|
1017
1006
|
});
|
|
1018
1007
|
}
|
|
1019
1008
|
function createArrayAssign(ctx, ref, index, value) {
|
|
1020
1009
|
markRef(ctx, ref);
|
|
1021
|
-
createAssignment(ctx,
|
|
1010
|
+
createAssignment(ctx, getRefParam(ctx, ref) + "[" + index.toString() + "]", value);
|
|
1022
1011
|
}
|
|
1023
1012
|
function createObjectAssign(ctx, ref, key, value) {
|
|
1024
1013
|
markRef(ctx, ref);
|
|
1025
|
-
createAssignment(ctx,
|
|
1014
|
+
createAssignment(ctx, getRefParam(ctx, ref) + "." + key, value);
|
|
1026
1015
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
const mainAssignments = [];
|
|
1031
|
-
for (let i = 0; i < node.s; i++) {
|
|
1032
|
-
const parentStack = ctx.stack;
|
|
1033
|
-
ctx.stack = [];
|
|
1034
|
-
const refParam = serializeTree(ctx, node.v[i]);
|
|
1035
|
-
ctx.stack = parentStack;
|
|
1036
|
-
const key = node.k[i];
|
|
1037
|
-
const check = Number(key);
|
|
1038
|
-
const parentAssignment = ctx.assignments;
|
|
1039
|
-
ctx.assignments = mainAssignments;
|
|
1040
|
-
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
1041
|
-
if (isIdentifier && Number.isNaN(check)) {
|
|
1042
|
-
createObjectAssign(ctx, targetRef, key, refParam);
|
|
1043
|
-
} else {
|
|
1044
|
-
createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
|
|
1045
|
-
}
|
|
1046
|
-
ctx.assignments = parentAssignment;
|
|
1016
|
+
function assignRef(ctx, index, value) {
|
|
1017
|
+
if (ctx.markedRefs.has(index)) {
|
|
1018
|
+
return getRefParam(ctx, index) + "=" + value;
|
|
1047
1019
|
}
|
|
1048
|
-
|
|
1049
|
-
return resolveAssignments(mainAssignments);
|
|
1020
|
+
return value;
|
|
1050
1021
|
}
|
|
1051
|
-
function
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
ctx
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1022
|
+
function isReferenceInStack(ctx, node) {
|
|
1023
|
+
return node.t === 2 /* Reference */ && ctx.stack.includes(node.i);
|
|
1024
|
+
}
|
|
1025
|
+
var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
|
|
1026
|
+
var SerovalSerializer = class {
|
|
1027
|
+
static serializeNodeList(ctx, node) {
|
|
1028
|
+
const size = node.a.length;
|
|
1029
|
+
let values = "";
|
|
1030
|
+
let item;
|
|
1031
|
+
let isHoley = false;
|
|
1032
|
+
for (let i = 0; i < size; i++) {
|
|
1033
|
+
if (i !== 0) {
|
|
1034
|
+
values += ",";
|
|
1035
|
+
}
|
|
1036
|
+
item = node.a[i];
|
|
1037
|
+
if (item) {
|
|
1038
|
+
if (isReferenceInStack(ctx, item)) {
|
|
1039
|
+
createArrayAssign(ctx, node.i, i, getRefParam(ctx, item.i));
|
|
1040
|
+
isHoley = true;
|
|
1041
|
+
} else {
|
|
1042
|
+
values += this.serialize(ctx, item);
|
|
1043
|
+
isHoley = false;
|
|
1044
|
+
}
|
|
1066
1045
|
} else {
|
|
1067
|
-
|
|
1046
|
+
isHoley = true;
|
|
1068
1047
|
}
|
|
1069
|
-
} else {
|
|
1070
|
-
result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
|
|
1071
1048
|
}
|
|
1049
|
+
if (isHoley) {
|
|
1050
|
+
values += ",";
|
|
1051
|
+
}
|
|
1052
|
+
return "[" + values + "]";
|
|
1072
1053
|
}
|
|
1073
|
-
ctx
|
|
1074
|
-
return `{${result.substring(0, result.length - 1)}}`;
|
|
1075
|
-
}
|
|
1076
|
-
function serializePromise(ctx, node) {
|
|
1077
|
-
let serialized;
|
|
1078
|
-
if (isReferenceInStack(ctx, node.n)) {
|
|
1079
|
-
serialized = `Promise.resolve().then(()=>${getRefParam(ctx, node.n.i)})`;
|
|
1080
|
-
} else {
|
|
1054
|
+
static serializeArray(ctx, node) {
|
|
1081
1055
|
ctx.stack.push(node.i);
|
|
1082
|
-
const result =
|
|
1056
|
+
const result = this.serializeNodeList(ctx, node);
|
|
1083
1057
|
ctx.stack.pop();
|
|
1084
|
-
|
|
1058
|
+
return assignRef(ctx, node.i, result);
|
|
1085
1059
|
}
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
if (node.l !== 0) {
|
|
1091
|
-
args += `,${node.l}`;
|
|
1092
|
-
}
|
|
1093
|
-
return assignRef(ctx, node.i, `new ${node.c}(${args})`);
|
|
1094
|
-
}
|
|
1095
|
-
function serializeSet(ctx, node) {
|
|
1096
|
-
let serialized = "new Set";
|
|
1097
|
-
const size = node.a.length;
|
|
1098
|
-
if (size) {
|
|
1060
|
+
static serializeObject(ctx, sourceID, node) {
|
|
1061
|
+
if (node.s === 0) {
|
|
1062
|
+
return "{}";
|
|
1063
|
+
}
|
|
1099
1064
|
let result = "";
|
|
1100
|
-
ctx.stack.push(
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1065
|
+
ctx.stack.push(sourceID);
|
|
1066
|
+
let key;
|
|
1067
|
+
let val;
|
|
1068
|
+
let check;
|
|
1069
|
+
let isIdentifier;
|
|
1070
|
+
let refParam;
|
|
1071
|
+
let hasPrev = false;
|
|
1072
|
+
for (let i = 0; i < node.s; i++) {
|
|
1073
|
+
key = node.k[i];
|
|
1074
|
+
val = node.v[i];
|
|
1075
|
+
check = Number(key);
|
|
1076
|
+
isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
1077
|
+
if (isReferenceInStack(ctx, val)) {
|
|
1078
|
+
refParam = getRefParam(ctx, val.i);
|
|
1079
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
1080
|
+
createObjectAssign(ctx, sourceID, key, refParam);
|
|
1081
|
+
} else {
|
|
1082
|
+
createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
|
|
1083
|
+
}
|
|
1105
1084
|
} else {
|
|
1106
|
-
|
|
1085
|
+
if (hasPrev) {
|
|
1086
|
+
result += ",";
|
|
1087
|
+
}
|
|
1088
|
+
result += isIdentifier ? key : quote(key);
|
|
1089
|
+
result += ":" + this.serialize(ctx, val);
|
|
1090
|
+
hasPrev = true;
|
|
1107
1091
|
}
|
|
1108
1092
|
}
|
|
1109
1093
|
ctx.stack.pop();
|
|
1110
|
-
|
|
1111
|
-
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1112
|
-
}
|
|
1094
|
+
return "{" + result + "}";
|
|
1113
1095
|
}
|
|
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
|
-
|
|
1096
|
+
static serializeWithObjectAssign(ctx, value, id, serialized) {
|
|
1097
|
+
const fields = this.serializeObject(ctx, id, value);
|
|
1098
|
+
if (fields !== "{}") {
|
|
1099
|
+
return "Object.assign(" + serialized + "," + fields + ")";
|
|
1100
|
+
}
|
|
1101
|
+
return serialized;
|
|
1102
|
+
}
|
|
1103
|
+
static serializeAssignments(ctx, sourceID, node) {
|
|
1104
|
+
ctx.stack.push(sourceID);
|
|
1105
|
+
const mainAssignments = [];
|
|
1106
|
+
let parentStack;
|
|
1107
|
+
let refParam;
|
|
1108
|
+
let key;
|
|
1109
|
+
let check;
|
|
1110
|
+
let parentAssignment;
|
|
1111
|
+
let isIdentifier;
|
|
1112
|
+
for (let i = 0; i < node.s; i++) {
|
|
1113
|
+
parentStack = ctx.stack;
|
|
1114
|
+
ctx.stack = [];
|
|
1115
|
+
refParam = this.serialize(ctx, node.v[i]);
|
|
1116
|
+
ctx.stack = parentStack;
|
|
1117
|
+
key = node.k[i];
|
|
1118
|
+
check = Number(key);
|
|
1119
|
+
parentAssignment = ctx.assignments;
|
|
1120
|
+
ctx.assignments = mainAssignments;
|
|
1121
|
+
isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
1122
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
1123
|
+
createObjectAssign(ctx, sourceID, key, refParam);
|
|
1141
1124
|
} else {
|
|
1142
|
-
|
|
1125
|
+
createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
|
|
1143
1126
|
}
|
|
1127
|
+
ctx.assignments = parentAssignment;
|
|
1144
1128
|
}
|
|
1145
1129
|
ctx.stack.pop();
|
|
1146
|
-
|
|
1147
|
-
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1148
|
-
}
|
|
1130
|
+
return resolveAssignments(mainAssignments);
|
|
1149
1131
|
}
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
let values = "";
|
|
1155
|
-
for (let i = 0; i < size; i++) {
|
|
1156
|
-
const item = node.a[i];
|
|
1157
|
-
if (item) {
|
|
1158
|
-
if (isReferenceInStack(ctx, item)) {
|
|
1159
|
-
createArrayAssign(ctx, node.i, i, getRefParam(ctx, item.i));
|
|
1160
|
-
values += ",";
|
|
1132
|
+
static serializeDictionary(ctx, i, d, init) {
|
|
1133
|
+
if (d) {
|
|
1134
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
1135
|
+
init = this.serializeWithObjectAssign(ctx, d, i, init);
|
|
1161
1136
|
} else {
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1137
|
+
markRef(ctx, i);
|
|
1138
|
+
const assignments = this.serializeAssignments(ctx, i, d);
|
|
1139
|
+
if (assignments) {
|
|
1140
|
+
const ref = getRefParam(ctx, i);
|
|
1141
|
+
return "(" + assignRef(ctx, i, init) + "," + assignments + ref + ")";
|
|
1165
1142
|
}
|
|
1166
1143
|
}
|
|
1167
|
-
} else {
|
|
1168
|
-
values += ",";
|
|
1169
1144
|
}
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
if (
|
|
1197
|
-
|
|
1198
|
-
return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
|
|
1145
|
+
return assignRef(ctx, i, init);
|
|
1146
|
+
}
|
|
1147
|
+
static serializeNullConstructor(ctx, node) {
|
|
1148
|
+
return this.serializeDictionary(ctx, node.i, node.d, "Object.create(null)");
|
|
1149
|
+
}
|
|
1150
|
+
static serializeSet(ctx, node) {
|
|
1151
|
+
let serialized = "new Set";
|
|
1152
|
+
const size = node.a.length;
|
|
1153
|
+
if (size) {
|
|
1154
|
+
let result = "";
|
|
1155
|
+
ctx.stack.push(node.i);
|
|
1156
|
+
let item;
|
|
1157
|
+
let hasPrev = false;
|
|
1158
|
+
for (let i = 0; i < size; i++) {
|
|
1159
|
+
item = node.a[i];
|
|
1160
|
+
if (isReferenceInStack(ctx, item)) {
|
|
1161
|
+
createSetAdd(ctx, node.i, getRefParam(ctx, item.i));
|
|
1162
|
+
} else {
|
|
1163
|
+
if (hasPrev) {
|
|
1164
|
+
result += ",";
|
|
1165
|
+
}
|
|
1166
|
+
result += this.serialize(ctx, item);
|
|
1167
|
+
hasPrev = true;
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
ctx.stack.pop();
|
|
1171
|
+
if (result) {
|
|
1172
|
+
serialized += "([" + result + "])";
|
|
1199
1173
|
}
|
|
1200
1174
|
}
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1175
|
+
return assignRef(ctx, node.i, serialized);
|
|
1176
|
+
}
|
|
1177
|
+
static serializeMap(ctx, node) {
|
|
1178
|
+
let serialized = "new Map";
|
|
1179
|
+
if (node.d.s) {
|
|
1180
|
+
let result = "";
|
|
1181
|
+
ctx.stack.push(node.i);
|
|
1182
|
+
let key;
|
|
1183
|
+
let val;
|
|
1184
|
+
let keyRef;
|
|
1185
|
+
let valueRef;
|
|
1186
|
+
let parent;
|
|
1187
|
+
let hasPrev = false;
|
|
1188
|
+
for (let i = 0; i < node.d.s; i++) {
|
|
1189
|
+
key = node.d.k[i];
|
|
1190
|
+
val = node.d.v[i];
|
|
1191
|
+
if (isReferenceInStack(ctx, key)) {
|
|
1192
|
+
keyRef = getRefParam(ctx, key.i);
|
|
1193
|
+
if (isReferenceInStack(ctx, val)) {
|
|
1194
|
+
valueRef = getRefParam(ctx, val.i);
|
|
1195
|
+
createMapSet(ctx, node.i, keyRef, valueRef);
|
|
1196
|
+
} else {
|
|
1197
|
+
parent = ctx.stack;
|
|
1198
|
+
ctx.stack = [];
|
|
1199
|
+
createMapSet(ctx, node.i, keyRef, this.serialize(ctx, val));
|
|
1200
|
+
ctx.stack = parent;
|
|
1201
|
+
}
|
|
1202
|
+
} else if (isReferenceInStack(ctx, val)) {
|
|
1203
|
+
valueRef = getRefParam(ctx, val.i);
|
|
1204
|
+
parent = ctx.stack;
|
|
1205
|
+
ctx.stack = [];
|
|
1206
|
+
createMapSet(ctx, node.i, this.serialize(ctx, key), valueRef);
|
|
1207
|
+
ctx.stack = parent;
|
|
1208
|
+
} else {
|
|
1209
|
+
if (hasPrev) {
|
|
1210
|
+
result += ",";
|
|
1211
|
+
}
|
|
1212
|
+
result += "[" + this.serialize(ctx, key) + "," + this.serialize(ctx, val) + "]";
|
|
1213
|
+
hasPrev = true;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
ctx.stack.pop();
|
|
1217
|
+
if (result) {
|
|
1218
|
+
serialized += "([" + result + "])";
|
|
1215
1219
|
}
|
|
1216
1220
|
}
|
|
1221
|
+
return assignRef(ctx, node.i, serialized);
|
|
1217
1222
|
}
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
const values = serializeNodeList(ctx, node);
|
|
1224
|
-
ctx.stack = parent;
|
|
1225
|
-
let serialized;
|
|
1226
|
-
if (ctx.features & 2 /* ArrayPrototypeValues */) {
|
|
1227
|
-
serialized = `${values}.values()`;
|
|
1228
|
-
} else {
|
|
1229
|
-
serialized = `${values}[Symbol.iterator]()`;
|
|
1223
|
+
static serializeAggregateError(ctx, node) {
|
|
1224
|
+
ctx.stack.push(node.i);
|
|
1225
|
+
const serialized = "new AggregateError(" + this.serialize(ctx, node.n) + "," + quote(node.m) + ")";
|
|
1226
|
+
ctx.stack.pop();
|
|
1227
|
+
return this.serializeDictionary(ctx, node.i, node.d, serialized);
|
|
1230
1228
|
}
|
|
1231
|
-
|
|
1232
|
-
serialized =
|
|
1233
|
-
|
|
1234
|
-
serialized = `{[Symbol.iterator](){return ${serialized}}}`;
|
|
1235
|
-
} else {
|
|
1236
|
-
serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
|
|
1229
|
+
static serializeError(ctx, node) {
|
|
1230
|
+
const serialized = "new " + node.c + "(" + quote(node.m) + ")";
|
|
1231
|
+
return this.serializeDictionary(ctx, node.i, node.d, serialized);
|
|
1237
1232
|
}
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1233
|
+
static serializePromise(ctx, node) {
|
|
1234
|
+
let serialized;
|
|
1235
|
+
if (isReferenceInStack(ctx, node.n)) {
|
|
1236
|
+
serialized = "Promise.resolve().then(()=>" + getRefParam(ctx, node.n.i) + ")";
|
|
1241
1237
|
} else {
|
|
1242
|
-
|
|
1243
|
-
const
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
|
|
1247
|
-
}
|
|
1238
|
+
ctx.stack.push(node.i);
|
|
1239
|
+
const result = this.serialize(ctx, node.n);
|
|
1240
|
+
ctx.stack.pop();
|
|
1241
|
+
serialized = "Promise.resolve(" + result + ")";
|
|
1248
1242
|
}
|
|
1243
|
+
return assignRef(ctx, node.i, serialized);
|
|
1249
1244
|
}
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
if (ctx.features & 64 /* ObjectAssign */) {
|
|
1255
|
-
serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
|
|
1256
|
-
} else {
|
|
1257
|
-
markRef(ctx, node.i);
|
|
1258
|
-
const assignments = serializeAssignments(ctx, node.i, node.d);
|
|
1259
|
-
if (assignments) {
|
|
1260
|
-
const ref = getRefParam(ctx, node.i);
|
|
1261
|
-
return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
|
|
1245
|
+
static serializeTypedArray(ctx, node) {
|
|
1246
|
+
let args = "[" + node.s + "]";
|
|
1247
|
+
if (node.l !== 0) {
|
|
1248
|
+
args += "," + node.l.toString();
|
|
1262
1249
|
}
|
|
1250
|
+
return assignRef(ctx, node.i, "new " + node.c + "(" + args + ")");
|
|
1263
1251
|
}
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1252
|
+
static serializeIterable(ctx, node) {
|
|
1253
|
+
const parent = ctx.stack;
|
|
1254
|
+
ctx.stack = [];
|
|
1255
|
+
const values = this.serializeNodeList(ctx, node);
|
|
1256
|
+
ctx.stack = parent;
|
|
1257
|
+
let serialized = values;
|
|
1258
|
+
if (ctx.features & 2 /* ArrayPrototypeValues */) {
|
|
1259
|
+
serialized += ".values()";
|
|
1260
|
+
} else {
|
|
1261
|
+
serialized += "[Symbol.iterator]()";
|
|
1262
|
+
}
|
|
1263
|
+
if (ctx.features & 4 /* ArrowFunction */) {
|
|
1264
|
+
serialized = "{[Symbol.iterator]:()=>" + serialized + "}";
|
|
1265
|
+
} else if (ctx.features & 32 /* MethodShorthand */) {
|
|
1266
|
+
serialized = "{[Symbol.iterator](){return " + serialized + "}}";
|
|
1267
|
+
} else {
|
|
1268
|
+
serialized = "{[Symbol.iterator]:function(){return " + serialized + "}}";
|
|
1269
|
+
}
|
|
1270
|
+
return this.serializeDictionary(ctx, node.i, node.d, serialized);
|
|
1271
|
+
}
|
|
1272
|
+
static serialize(ctx, node) {
|
|
1273
|
+
switch (node.t) {
|
|
1274
|
+
case 0 /* Primitive */:
|
|
1275
|
+
return String(node.s);
|
|
1276
|
+
case 1 /* BigInt */:
|
|
1277
|
+
return node.s;
|
|
1278
|
+
case 2 /* Reference */:
|
|
1279
|
+
return getRefParam(ctx, node.i);
|
|
1280
|
+
case 7 /* Array */:
|
|
1281
|
+
return this.serializeArray(ctx, node);
|
|
1282
|
+
case 8 /* Object */:
|
|
1283
|
+
return assignRef(ctx, node.i, this.serializeObject(ctx, node.i, node.d));
|
|
1284
|
+
case 9 /* NullConstructor */:
|
|
1285
|
+
return this.serializeNullConstructor(ctx, node);
|
|
1286
|
+
case 3 /* Date */:
|
|
1287
|
+
return assignRef(ctx, node.i, 'new Date("' + node.s + '")');
|
|
1288
|
+
case 4 /* RegExp */:
|
|
1289
|
+
return assignRef(ctx, node.i, node.s);
|
|
1290
|
+
case 5 /* Set */:
|
|
1291
|
+
return this.serializeSet(ctx, node);
|
|
1292
|
+
case 6 /* Map */:
|
|
1293
|
+
return this.serializeMap(ctx, node);
|
|
1294
|
+
case 15 /* BigIntTypedArray */:
|
|
1295
|
+
case 14 /* TypedArray */:
|
|
1296
|
+
return this.serializeTypedArray(ctx, node);
|
|
1297
|
+
case 12 /* AggregateError */:
|
|
1298
|
+
return this.serializeAggregateError(ctx, node);
|
|
1299
|
+
case 11 /* Error */:
|
|
1300
|
+
return this.serializeError(ctx, node);
|
|
1301
|
+
case 13 /* Iterable */:
|
|
1302
|
+
return this.serializeIterable(ctx, node);
|
|
1303
|
+
case 10 /* Promise */:
|
|
1304
|
+
return this.serializePromise(ctx, node);
|
|
1305
|
+
default:
|
|
1306
|
+
throw new Error("Unsupported type");
|
|
1307
|
+
}
|
|
1301
1308
|
}
|
|
1302
|
-
}
|
|
1309
|
+
};
|
|
1303
1310
|
|
|
1304
1311
|
// src/tree/sync.ts
|
|
1305
1312
|
var SyncParser = class {
|
|
1306
|
-
|
|
1307
|
-
__publicField(this, "ctx");
|
|
1308
|
-
this.ctx = ctx;
|
|
1309
|
-
}
|
|
1310
|
-
generateNodeList(current) {
|
|
1313
|
+
static generateNodeList(ctx, current) {
|
|
1311
1314
|
const size = current.length;
|
|
1312
1315
|
const nodes = new Array(size);
|
|
1313
1316
|
const deferred = new Array(size);
|
|
@@ -1318,22 +1321,32 @@ var SyncParser = class {
|
|
|
1318
1321
|
if (isIterable(item)) {
|
|
1319
1322
|
deferred[i] = item;
|
|
1320
1323
|
} else {
|
|
1321
|
-
nodes[i] = this.parse(item);
|
|
1324
|
+
nodes[i] = this.parse(ctx, item);
|
|
1322
1325
|
}
|
|
1323
1326
|
}
|
|
1324
1327
|
}
|
|
1325
1328
|
for (let i = 0; i < size; i++) {
|
|
1326
1329
|
if (i in deferred) {
|
|
1327
|
-
nodes[i] = this.parse(deferred[i]);
|
|
1330
|
+
nodes[i] = this.parse(ctx, deferred[i]);
|
|
1328
1331
|
}
|
|
1329
1332
|
}
|
|
1330
1333
|
return nodes;
|
|
1331
1334
|
}
|
|
1332
|
-
generateArrayNode(id, current) {
|
|
1333
|
-
return
|
|
1335
|
+
static generateArrayNode(ctx, id, current) {
|
|
1336
|
+
return {
|
|
1337
|
+
t: 7 /* Array */,
|
|
1338
|
+
i: id,
|
|
1339
|
+
s: void 0,
|
|
1340
|
+
l: void 0,
|
|
1341
|
+
c: void 0,
|
|
1342
|
+
m: void 0,
|
|
1343
|
+
d: void 0,
|
|
1344
|
+
a: this.generateNodeList(ctx, current),
|
|
1345
|
+
n: void 0
|
|
1346
|
+
};
|
|
1334
1347
|
}
|
|
1335
|
-
generateMapNode(id, current) {
|
|
1336
|
-
assert(
|
|
1348
|
+
static generateMapNode(ctx, id, current) {
|
|
1349
|
+
assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
|
|
1337
1350
|
const len = current.size;
|
|
1338
1351
|
const keyNodes = new Array(len);
|
|
1339
1352
|
const valueNodes = new Array(len);
|
|
@@ -1347,19 +1360,29 @@ var SyncParser = class {
|
|
|
1347
1360
|
deferredValue[deferredSize] = value;
|
|
1348
1361
|
deferredSize++;
|
|
1349
1362
|
} else {
|
|
1350
|
-
keyNodes[nodeSize] = this.parse(key);
|
|
1351
|
-
valueNodes[nodeSize] = this.parse(value);
|
|
1363
|
+
keyNodes[nodeSize] = this.parse(ctx, key);
|
|
1364
|
+
valueNodes[nodeSize] = this.parse(ctx, value);
|
|
1352
1365
|
nodeSize++;
|
|
1353
1366
|
}
|
|
1354
1367
|
}
|
|
1355
1368
|
for (let i = 0; i < deferredSize; i++) {
|
|
1356
|
-
keyNodes[nodeSize + i] = this.parse(deferredKey[i]);
|
|
1357
|
-
valueNodes[nodeSize + i] = this.parse(deferredValue[i]);
|
|
1369
|
+
keyNodes[nodeSize + i] = this.parse(ctx, deferredKey[i]);
|
|
1370
|
+
valueNodes[nodeSize + i] = this.parse(ctx, deferredValue[i]);
|
|
1358
1371
|
}
|
|
1359
|
-
return
|
|
1372
|
+
return {
|
|
1373
|
+
t: 6 /* Map */,
|
|
1374
|
+
i: id,
|
|
1375
|
+
s: void 0,
|
|
1376
|
+
l: void 0,
|
|
1377
|
+
c: void 0,
|
|
1378
|
+
m: void 0,
|
|
1379
|
+
d: { k: keyNodes, v: valueNodes, s: len },
|
|
1380
|
+
a: void 0,
|
|
1381
|
+
n: void 0
|
|
1382
|
+
};
|
|
1360
1383
|
}
|
|
1361
|
-
generateSetNode(id, current) {
|
|
1362
|
-
assert(
|
|
1384
|
+
static generateSetNode(ctx, id, current) {
|
|
1385
|
+
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
1363
1386
|
const len = current.size;
|
|
1364
1387
|
const nodes = new Array(len);
|
|
1365
1388
|
const deferred = new Array(len);
|
|
@@ -1369,15 +1392,25 @@ var SyncParser = class {
|
|
|
1369
1392
|
if (isIterable(item)) {
|
|
1370
1393
|
deferred[deferredSize++] = item;
|
|
1371
1394
|
} else {
|
|
1372
|
-
nodes[nodeSize++] = this.parse(item);
|
|
1395
|
+
nodes[nodeSize++] = this.parse(ctx, item);
|
|
1373
1396
|
}
|
|
1374
1397
|
}
|
|
1375
1398
|
for (let i = 0; i < deferredSize; i++) {
|
|
1376
|
-
nodes[nodeSize + i] = this.parse(deferred[i]);
|
|
1399
|
+
nodes[nodeSize + i] = this.parse(ctx, deferred[i]);
|
|
1377
1400
|
}
|
|
1378
|
-
return
|
|
1401
|
+
return {
|
|
1402
|
+
t: 5 /* Set */,
|
|
1403
|
+
i: id,
|
|
1404
|
+
s: void 0,
|
|
1405
|
+
l: void 0,
|
|
1406
|
+
c: void 0,
|
|
1407
|
+
m: void 0,
|
|
1408
|
+
d: void 0,
|
|
1409
|
+
a: nodes,
|
|
1410
|
+
n: void 0
|
|
1411
|
+
};
|
|
1379
1412
|
}
|
|
1380
|
-
generateProperties(properties) {
|
|
1413
|
+
static generateProperties(ctx, properties) {
|
|
1381
1414
|
const keys = Object.keys(properties);
|
|
1382
1415
|
const size = keys.length;
|
|
1383
1416
|
const keyNodes = new Array(size);
|
|
@@ -1395,13 +1428,13 @@ var SyncParser = class {
|
|
|
1395
1428
|
deferredSize++;
|
|
1396
1429
|
} else {
|
|
1397
1430
|
keyNodes[nodesSize] = key;
|
|
1398
|
-
valueNodes[nodesSize] = this.parse(item);
|
|
1431
|
+
valueNodes[nodesSize] = this.parse(ctx, item);
|
|
1399
1432
|
nodesSize++;
|
|
1400
1433
|
}
|
|
1401
1434
|
}
|
|
1402
1435
|
for (let i = 0; i < deferredSize; i++) {
|
|
1403
1436
|
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
1404
|
-
valueNodes[nodesSize + i] = this.parse(deferredValues[i]);
|
|
1437
|
+
valueNodes[nodesSize + i] = this.parse(ctx, deferredValues[i]);
|
|
1405
1438
|
}
|
|
1406
1439
|
return {
|
|
1407
1440
|
k: keyNodes,
|
|
@@ -1409,78 +1442,105 @@ var SyncParser = class {
|
|
|
1409
1442
|
s: size
|
|
1410
1443
|
};
|
|
1411
1444
|
}
|
|
1412
|
-
generateIterableNode(id, current) {
|
|
1413
|
-
assert(
|
|
1445
|
+
static generateIterableNode(ctx, id, current) {
|
|
1446
|
+
assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
|
|
1414
1447
|
const options = getIterableOptions(current);
|
|
1415
|
-
return
|
|
1416
|
-
|
|
1448
|
+
return {
|
|
1449
|
+
t: 13 /* Iterable */,
|
|
1450
|
+
i: id,
|
|
1451
|
+
s: void 0,
|
|
1452
|
+
l: void 0,
|
|
1453
|
+
c: void 0,
|
|
1454
|
+
m: void 0,
|
|
1417
1455
|
// Parse options first before the items
|
|
1418
|
-
options ? this.generateProperties(options) : void 0,
|
|
1419
|
-
this.generateNodeList(Array.from(current))
|
|
1420
|
-
|
|
1456
|
+
d: options ? this.generateProperties(ctx, options) : void 0,
|
|
1457
|
+
a: this.generateNodeList(ctx, Array.from(current)),
|
|
1458
|
+
n: void 0
|
|
1459
|
+
};
|
|
1421
1460
|
}
|
|
1422
|
-
generateObjectNode(id, current, empty) {
|
|
1461
|
+
static generateObjectNode(ctx, id, current, empty) {
|
|
1423
1462
|
if (Symbol.iterator in current) {
|
|
1424
|
-
return this.generateIterableNode(id, current);
|
|
1463
|
+
return this.generateIterableNode(ctx, id, current);
|
|
1425
1464
|
}
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1465
|
+
return {
|
|
1466
|
+
t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
1467
|
+
i: id,
|
|
1468
|
+
s: void 0,
|
|
1469
|
+
l: void 0,
|
|
1470
|
+
c: void 0,
|
|
1471
|
+
m: void 0,
|
|
1472
|
+
d: this.generateProperties(ctx, current),
|
|
1473
|
+
a: void 0,
|
|
1474
|
+
n: void 0
|
|
1475
|
+
};
|
|
1430
1476
|
}
|
|
1431
|
-
generateAggregateErrorNode(id, current) {
|
|
1477
|
+
static generateAggregateErrorNode(ctx, id, current) {
|
|
1432
1478
|
const options = getErrorOptions(current);
|
|
1433
|
-
const optionsNode = options ? this.generateProperties(options) : void 0;
|
|
1434
|
-
return
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1479
|
+
const optionsNode = options ? this.generateProperties(ctx, options) : void 0;
|
|
1480
|
+
return {
|
|
1481
|
+
t: 12 /* AggregateError */,
|
|
1482
|
+
i: id,
|
|
1483
|
+
s: void 0,
|
|
1484
|
+
l: void 0,
|
|
1485
|
+
c: void 0,
|
|
1486
|
+
m: current.message,
|
|
1487
|
+
d: optionsNode,
|
|
1488
|
+
a: void 0,
|
|
1489
|
+
n: this.parse(ctx, current.errors)
|
|
1490
|
+
};
|
|
1491
|
+
}
|
|
1492
|
+
static generateErrorNode(ctx, id, current) {
|
|
1442
1493
|
const options = getErrorOptions(current);
|
|
1443
|
-
const optionsNode = options ? this.generateProperties(options) : void 0;
|
|
1444
|
-
return
|
|
1494
|
+
const optionsNode = options ? this.generateProperties(ctx, options) : void 0;
|
|
1495
|
+
return {
|
|
1496
|
+
t: 11 /* Error */,
|
|
1497
|
+
i: id,
|
|
1498
|
+
s: void 0,
|
|
1499
|
+
l: void 0,
|
|
1500
|
+
c: getErrorConstructor(current),
|
|
1501
|
+
m: current.message,
|
|
1502
|
+
d: optionsNode,
|
|
1503
|
+
a: void 0,
|
|
1504
|
+
n: void 0
|
|
1505
|
+
};
|
|
1445
1506
|
}
|
|
1446
|
-
parse(current) {
|
|
1507
|
+
static parse(ctx, current) {
|
|
1447
1508
|
switch (typeof current) {
|
|
1448
1509
|
case "boolean":
|
|
1449
1510
|
return current ? TRUE_NODE : FALSE_NODE;
|
|
1450
1511
|
case "undefined":
|
|
1451
1512
|
return UNDEFINED_NODE;
|
|
1452
1513
|
case "string":
|
|
1453
|
-
return
|
|
1514
|
+
return createPrimitiveNode(quote(current));
|
|
1454
1515
|
case "number":
|
|
1455
1516
|
if (Object.is(current, -0)) {
|
|
1456
1517
|
return NEG_ZERO_NODE;
|
|
1457
1518
|
}
|
|
1458
1519
|
if (Object.is(current, Infinity)) {
|
|
1459
|
-
return
|
|
1520
|
+
return createPrimitiveNode("1/0");
|
|
1460
1521
|
}
|
|
1461
1522
|
if (Object.is(current, -Infinity)) {
|
|
1462
|
-
return
|
|
1523
|
+
return createPrimitiveNode("-1/0");
|
|
1463
1524
|
}
|
|
1464
|
-
return
|
|
1525
|
+
return createPrimitiveNode(current);
|
|
1465
1526
|
case "bigint":
|
|
1466
|
-
|
|
1467
|
-
return new SerovalBigIntNode(current);
|
|
1527
|
+
return createBigIntNode(ctx, current);
|
|
1468
1528
|
case "object": {
|
|
1469
1529
|
if (!current) {
|
|
1470
1530
|
return NULL_NODE;
|
|
1471
1531
|
}
|
|
1472
|
-
const id = createRef(
|
|
1473
|
-
if (
|
|
1474
|
-
return
|
|
1532
|
+
const id = createRef(ctx, current, true);
|
|
1533
|
+
if (ctx.markedRefs.has(id)) {
|
|
1534
|
+
return createReferenceNode(id);
|
|
1475
1535
|
}
|
|
1476
1536
|
if (Array.isArray(current)) {
|
|
1477
|
-
return this.generateArrayNode(id, current);
|
|
1537
|
+
return this.generateArrayNode(ctx, id, current);
|
|
1478
1538
|
}
|
|
1479
1539
|
switch (current.constructor) {
|
|
1480
1540
|
case Date:
|
|
1481
|
-
return
|
|
1541
|
+
return createDateNode(id, current);
|
|
1482
1542
|
case RegExp:
|
|
1483
|
-
return
|
|
1543
|
+
return createRegExpNode(id, current);
|
|
1484
1544
|
case Int8Array:
|
|
1485
1545
|
case Int16Array:
|
|
1486
1546
|
case Int32Array:
|
|
@@ -1490,28 +1550,23 @@ var SyncParser = class {
|
|
|
1490
1550
|
case Uint8ClampedArray:
|
|
1491
1551
|
case Float32Array:
|
|
1492
1552
|
case Float64Array:
|
|
1493
|
-
|
|
1494
|
-
return new SerovalTypedArrayNode(id, current);
|
|
1553
|
+
return createTypedArrayNode(ctx, id, current);
|
|
1495
1554
|
case BigInt64Array:
|
|
1496
1555
|
case BigUint64Array:
|
|
1497
|
-
|
|
1498
|
-
this.ctx.features & 2048 /* BigIntTypedArray */,
|
|
1499
|
-
`Unsupported value type "${current.constructor.name}"`
|
|
1500
|
-
);
|
|
1501
|
-
return new SerovalBigIntTypedArrayNode(id, current);
|
|
1556
|
+
return createBigIntTypedArrayNode(ctx, id, current);
|
|
1502
1557
|
case Map:
|
|
1503
|
-
return this.generateMapNode(id, current);
|
|
1558
|
+
return this.generateMapNode(ctx, id, current);
|
|
1504
1559
|
case Set:
|
|
1505
|
-
return this.generateSetNode(id, current);
|
|
1560
|
+
return this.generateSetNode(ctx, id, current);
|
|
1506
1561
|
case Object:
|
|
1507
|
-
return this.generateObjectNode(id, current, false);
|
|
1562
|
+
return this.generateObjectNode(ctx, id, current, false);
|
|
1508
1563
|
case void 0:
|
|
1509
|
-
return this.generateObjectNode(id, current, true);
|
|
1564
|
+
return this.generateObjectNode(ctx, id, current, true);
|
|
1510
1565
|
case AggregateError:
|
|
1511
|
-
if (
|
|
1512
|
-
return this.generateAggregateErrorNode(id, current);
|
|
1566
|
+
if (ctx.features & 1 /* AggregateError */) {
|
|
1567
|
+
return this.generateAggregateErrorNode(ctx, id, current);
|
|
1513
1568
|
}
|
|
1514
|
-
return this.generateErrorNode(id, current);
|
|
1569
|
+
return this.generateErrorNode(ctx, id, current);
|
|
1515
1570
|
case Error:
|
|
1516
1571
|
case EvalError:
|
|
1517
1572
|
case RangeError:
|
|
@@ -1519,18 +1574,18 @@ var SyncParser = class {
|
|
|
1519
1574
|
case SyntaxError:
|
|
1520
1575
|
case TypeError:
|
|
1521
1576
|
case URIError:
|
|
1522
|
-
return this.generateErrorNode(id, current);
|
|
1577
|
+
return this.generateErrorNode(ctx, id, current);
|
|
1523
1578
|
default:
|
|
1524
1579
|
break;
|
|
1525
1580
|
}
|
|
1526
|
-
if (current instanceof AggregateError &&
|
|
1527
|
-
return this.generateAggregateErrorNode(id, current);
|
|
1581
|
+
if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
|
|
1582
|
+
return this.generateAggregateErrorNode(ctx, id, current);
|
|
1528
1583
|
}
|
|
1529
1584
|
if (current instanceof Error) {
|
|
1530
|
-
return this.generateErrorNode(id, current);
|
|
1585
|
+
return this.generateErrorNode(ctx, id, current);
|
|
1531
1586
|
}
|
|
1532
1587
|
if (Symbol.iterator in current) {
|
|
1533
|
-
return this.generateIterableNode(id, current);
|
|
1588
|
+
return this.generateIterableNode(ctx, id, current);
|
|
1534
1589
|
}
|
|
1535
1590
|
throw new Error("Unsupported type");
|
|
1536
1591
|
}
|
|
@@ -1540,7 +1595,7 @@ var SyncParser = class {
|
|
|
1540
1595
|
}
|
|
1541
1596
|
};
|
|
1542
1597
|
function parseSync(ctx, current) {
|
|
1543
|
-
const result =
|
|
1598
|
+
const result = SyncParser.parse(ctx, current);
|
|
1544
1599
|
const isObject = result.t === 8 /* Object */ || result.t === 13 /* Iterable */;
|
|
1545
1600
|
return [result, createRef(ctx, current, false), isObject];
|
|
1546
1601
|
}
|
|
@@ -1552,71 +1607,70 @@ function finalize(ctx, rootID, isObject, result) {
|
|
|
1552
1607
|
let body = result;
|
|
1553
1608
|
if (patches) {
|
|
1554
1609
|
const index = getRefParam(ctx, rootID);
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
body = `${index}=${result},${patches}${index}`;
|
|
1610
|
+
body = result + "," + patches + index;
|
|
1611
|
+
if (!result.startsWith(index + "=")) {
|
|
1612
|
+
body = index + "=" + body;
|
|
1559
1613
|
}
|
|
1560
1614
|
}
|
|
1561
1615
|
let params = ctx.vars.length > 1 ? ctx.vars.join(",") : ctx.vars[0];
|
|
1562
1616
|
if (ctx.features & 4 /* ArrowFunction */) {
|
|
1563
|
-
params = ctx.vars.length > 1 || ctx.vars.length === 0 ?
|
|
1564
|
-
return
|
|
1617
|
+
params = ctx.vars.length > 1 || ctx.vars.length === 0 ? "(" + params + ")" : params;
|
|
1618
|
+
return "(" + params + "=>(" + body + "))()";
|
|
1565
1619
|
}
|
|
1566
|
-
return
|
|
1620
|
+
return "(function(" + params + "){return " + body + "})()";
|
|
1567
1621
|
}
|
|
1568
1622
|
if (isObject) {
|
|
1569
|
-
return
|
|
1623
|
+
return "(" + result + ")";
|
|
1570
1624
|
}
|
|
1571
1625
|
return result;
|
|
1572
1626
|
}
|
|
1573
1627
|
function serialize(source, options) {
|
|
1574
|
-
const ctx =
|
|
1628
|
+
const ctx = createParserContext(options);
|
|
1575
1629
|
const [tree, rootID, isObject] = parseSync(ctx, source);
|
|
1576
|
-
const serial =
|
|
1577
|
-
const result =
|
|
1630
|
+
const serial = createSerializationContext(ctx);
|
|
1631
|
+
const result = SerovalSerializer.serialize(serial, tree);
|
|
1578
1632
|
return finalize(serial, rootID, isObject, result);
|
|
1579
1633
|
}
|
|
1580
1634
|
async function serializeAsync(source, options) {
|
|
1581
|
-
const ctx =
|
|
1635
|
+
const ctx = createParserContext(options);
|
|
1582
1636
|
const [tree, rootID, isObject] = await parseAsync(ctx, source);
|
|
1583
|
-
const serial =
|
|
1584
|
-
const result =
|
|
1637
|
+
const serial = createSerializationContext(ctx);
|
|
1638
|
+
const result = SerovalSerializer.serialize(serial, tree);
|
|
1585
1639
|
return finalize(serial, rootID, isObject, result);
|
|
1586
1640
|
}
|
|
1587
1641
|
function deserialize(source) {
|
|
1588
1642
|
return (0, eval)(source);
|
|
1589
1643
|
}
|
|
1590
1644
|
function toJSON(source, options) {
|
|
1591
|
-
const ctx =
|
|
1645
|
+
const ctx = createParserContext(options);
|
|
1592
1646
|
const [tree, root, isObject] = parseSync(ctx, source);
|
|
1593
|
-
return JSON.stringify(
|
|
1594
|
-
tree,
|
|
1595
|
-
root,
|
|
1596
|
-
isObject,
|
|
1597
|
-
ctx.features,
|
|
1598
|
-
ctx.markedRefs
|
|
1599
|
-
|
|
1647
|
+
return JSON.stringify({
|
|
1648
|
+
t: tree,
|
|
1649
|
+
r: root,
|
|
1650
|
+
i: isObject,
|
|
1651
|
+
f: ctx.features,
|
|
1652
|
+
m: Array.from(ctx.markedRefs)
|
|
1653
|
+
});
|
|
1600
1654
|
}
|
|
1601
1655
|
async function toJSONAsync(source, options) {
|
|
1602
|
-
const ctx =
|
|
1656
|
+
const ctx = createParserContext(options);
|
|
1603
1657
|
const [tree, root, isObject] = await parseAsync(ctx, source);
|
|
1604
|
-
return JSON.stringify(
|
|
1605
|
-
tree,
|
|
1606
|
-
root,
|
|
1607
|
-
isObject,
|
|
1608
|
-
ctx.features,
|
|
1609
|
-
ctx.markedRefs
|
|
1610
|
-
|
|
1658
|
+
return JSON.stringify({
|
|
1659
|
+
t: tree,
|
|
1660
|
+
r: root,
|
|
1661
|
+
i: isObject,
|
|
1662
|
+
f: ctx.features,
|
|
1663
|
+
m: Array.from(ctx.markedRefs)
|
|
1664
|
+
});
|
|
1611
1665
|
}
|
|
1612
1666
|
function compileJSON(source) {
|
|
1613
1667
|
const parsed = JSON.parse(source);
|
|
1614
|
-
const serial =
|
|
1615
|
-
features: parsed
|
|
1616
|
-
markedRefs: parsed
|
|
1668
|
+
const serial = createSerializationContext({
|
|
1669
|
+
features: parsed.f,
|
|
1670
|
+
markedRefs: parsed.m
|
|
1617
1671
|
});
|
|
1618
|
-
const result =
|
|
1619
|
-
return finalize(serial, parsed
|
|
1672
|
+
const result = SerovalSerializer.serialize(serial, parsed.t);
|
|
1673
|
+
return finalize(serial, parsed.r, parsed.i, result);
|
|
1620
1674
|
}
|
|
1621
1675
|
function fromJSON(source) {
|
|
1622
1676
|
return deserialize(compileJSON(source));
|