seroval 0.4.0-alpha.0 → 0.4.0-alpha.10
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 +1210 -826
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +2 -2
- package/dist/esm/development/index.mjs +1210 -826
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +2 -2
- package/dist/types/checks.d.ts +1 -2
- package/dist/types/compat.d.ts +14 -15
- package/dist/types/context.d.ts +54 -0
- package/dist/types/index.d.ts +5 -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 +9 -0
- package/dist/types/tree/sync.d.ts +4 -0
- package/dist/types/tree/types.d.ts +128 -0
- package/dist/types/types.d.ts +2 -2
- package/package.json +2 -2
- package/src/checks.ts +12 -16
- package/src/compat.ts +79 -49
- package/src/context.ts +148 -0
- package/src/index.ts +80 -25
- package/src/tree/async.ts +365 -0
- package/src/tree/serialize.ts +551 -0
- package/src/tree/shared.ts +191 -0
- package/src/tree/sync.ts +345 -0
- package/src/tree/types.ts +179 -0
- package/src/types.ts +3 -3
- package/dist/types/tree.d.ts +0 -112
- package/src/tree.ts +0 -1178
|
@@ -20,10 +20,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
+
compileJSON: () => compileJSON,
|
|
23
24
|
default: () => src_default,
|
|
24
25
|
deserialize: () => deserialize,
|
|
26
|
+
fromJSON: () => fromJSON,
|
|
25
27
|
serialize: () => serialize,
|
|
26
|
-
serializeAsync: () => serializeAsync
|
|
28
|
+
serializeAsync: () => serializeAsync,
|
|
29
|
+
toJSON: () => toJSON,
|
|
30
|
+
toJSONAsync: () => toJSONAsync
|
|
27
31
|
});
|
|
28
32
|
module.exports = __toCommonJS(src_exports);
|
|
29
33
|
|
|
@@ -33,87 +37,22 @@ function isPrimitive(current) {
|
|
|
33
37
|
return true;
|
|
34
38
|
}
|
|
35
39
|
const type = typeof current;
|
|
36
|
-
return type === "number" || type === "string"
|
|
40
|
+
return type === "number" || type === "string";
|
|
37
41
|
}
|
|
38
42
|
function isPromise(obj) {
|
|
39
43
|
return !!obj && (typeof obj === "object" || typeof obj === "function") && "then" in obj && typeof obj.then === "function";
|
|
40
44
|
}
|
|
41
|
-
function constructorCheck(value, constructor) {
|
|
42
|
-
return value.constructor === constructor;
|
|
43
|
-
}
|
|
44
45
|
function isIterableBuiltin(obj) {
|
|
45
|
-
|
|
46
|
+
if (!!obj && typeof obj === "object") {
|
|
47
|
+
const cons = obj.constructor;
|
|
48
|
+
return Array.isArray(obj) || cons === Set || cons === Map;
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
46
51
|
}
|
|
47
52
|
function isIterable(obj) {
|
|
48
53
|
return !!obj && typeof obj === "object" && !isIterableBuiltin(obj) && Symbol.iterator in obj && typeof obj[Symbol.iterator] === "function";
|
|
49
54
|
}
|
|
50
55
|
|
|
51
|
-
// src/quote.ts
|
|
52
|
-
function quote(str) {
|
|
53
|
-
let result = "";
|
|
54
|
-
let lastPos = 0;
|
|
55
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
|
56
|
-
let replacement;
|
|
57
|
-
switch (str[i]) {
|
|
58
|
-
case '"':
|
|
59
|
-
replacement = '\\"';
|
|
60
|
-
break;
|
|
61
|
-
case "\\":
|
|
62
|
-
replacement = "\\\\";
|
|
63
|
-
break;
|
|
64
|
-
case "<":
|
|
65
|
-
replacement = "\\x3C";
|
|
66
|
-
break;
|
|
67
|
-
case "\n":
|
|
68
|
-
replacement = "\\n";
|
|
69
|
-
break;
|
|
70
|
-
case "\r":
|
|
71
|
-
replacement = "\\r";
|
|
72
|
-
break;
|
|
73
|
-
case "\u2028":
|
|
74
|
-
replacement = "\\u2028";
|
|
75
|
-
break;
|
|
76
|
-
case "\u2029":
|
|
77
|
-
replacement = "\\u2029";
|
|
78
|
-
break;
|
|
79
|
-
default:
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
result += str.slice(lastPos, i) + replacement;
|
|
83
|
-
lastPos = i + 1;
|
|
84
|
-
}
|
|
85
|
-
if (lastPos === 0) {
|
|
86
|
-
result = str;
|
|
87
|
-
} else {
|
|
88
|
-
result += str.slice(lastPos);
|
|
89
|
-
}
|
|
90
|
-
return `"${result}"`;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// src/get-identifier.ts
|
|
94
|
-
var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
|
|
95
|
-
var REF_START_CHARS_LEN = REF_START_CHARS.length;
|
|
96
|
-
var REF_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_";
|
|
97
|
-
var REF_CHARS_LEN = REF_CHARS.length;
|
|
98
|
-
function getIdentifier(index) {
|
|
99
|
-
let mod = index % REF_START_CHARS_LEN;
|
|
100
|
-
let ref = REF_START_CHARS[mod];
|
|
101
|
-
index = (index - mod) / REF_START_CHARS_LEN;
|
|
102
|
-
while (index > 0) {
|
|
103
|
-
mod = index % REF_CHARS_LEN;
|
|
104
|
-
ref += REF_CHARS[mod];
|
|
105
|
-
index = (index - mod) / REF_CHARS_LEN;
|
|
106
|
-
}
|
|
107
|
-
return ref;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// src/assert.ts
|
|
111
|
-
function assert(cond, error) {
|
|
112
|
-
if (!cond) {
|
|
113
|
-
throw new Error(error);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
56
|
// src/compat.ts
|
|
118
57
|
function compareVersion(left, right) {
|
|
119
58
|
let diff = left[0] - right[0];
|
|
@@ -127,7 +66,7 @@ function compareVersion(left, right) {
|
|
|
127
66
|
}
|
|
128
67
|
var VERSION_TABLE = {
|
|
129
68
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError#browser_compatibility
|
|
130
|
-
|
|
69
|
+
[1 /* AggregateError */]: {
|
|
131
70
|
es: [2021, 0, 0],
|
|
132
71
|
chrome: [85, 0, 0],
|
|
133
72
|
edge: [85, 0, 0],
|
|
@@ -140,7 +79,7 @@ var VERSION_TABLE = {
|
|
|
140
79
|
node: [15, 0, 0]
|
|
141
80
|
},
|
|
142
81
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values#browser_compatibility
|
|
143
|
-
|
|
82
|
+
[2 /* ArrayPrototypeValues */]: {
|
|
144
83
|
es: [6, 0, 0],
|
|
145
84
|
chrome: [66, 0, 0],
|
|
146
85
|
edge: [14, 0, 0],
|
|
@@ -153,7 +92,7 @@ var VERSION_TABLE = {
|
|
|
153
92
|
node: [10, 9, 0]
|
|
154
93
|
},
|
|
155
94
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#browser_compatibility
|
|
156
|
-
|
|
95
|
+
[4 /* ArrowFunction */]: {
|
|
157
96
|
es: [6, 0, 0],
|
|
158
97
|
chrome: [45, 0, 0],
|
|
159
98
|
edge: [12, 0, 0],
|
|
@@ -166,7 +105,7 @@ var VERSION_TABLE = {
|
|
|
166
105
|
node: [4, 0, 0]
|
|
167
106
|
},
|
|
168
107
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility
|
|
169
|
-
|
|
108
|
+
[8 /* BigInt */]: {
|
|
170
109
|
es: [2020, 0, 0],
|
|
171
110
|
chrome: [67, 0, 0],
|
|
172
111
|
edge: [79, 0, 0],
|
|
@@ -179,7 +118,7 @@ var VERSION_TABLE = {
|
|
|
179
118
|
node: [10, 4, 0]
|
|
180
119
|
},
|
|
181
120
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#browser_compatibility
|
|
182
|
-
|
|
121
|
+
[16 /* Map */]: {
|
|
183
122
|
es: [6, 0, 0],
|
|
184
123
|
chrome: [38, 0, 0],
|
|
185
124
|
edge: [12, 0, 0],
|
|
@@ -192,7 +131,7 @@ var VERSION_TABLE = {
|
|
|
192
131
|
node: [0, 12, 0]
|
|
193
132
|
},
|
|
194
133
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#browser_compatibility
|
|
195
|
-
|
|
134
|
+
[32 /* MethodShorthand */]: {
|
|
196
135
|
es: [6, 0, 0],
|
|
197
136
|
chrome: [39, 0, 0],
|
|
198
137
|
edge: [12, 0, 0],
|
|
@@ -205,7 +144,7 @@ var VERSION_TABLE = {
|
|
|
205
144
|
node: [4, 0, 0]
|
|
206
145
|
},
|
|
207
146
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#browser_compatibility
|
|
208
|
-
|
|
147
|
+
[64 /* ObjectAssign */]: {
|
|
209
148
|
es: [6, 0, 0],
|
|
210
149
|
chrome: [45, 0, 0],
|
|
211
150
|
edge: [12, 0, 0],
|
|
@@ -218,7 +157,7 @@ var VERSION_TABLE = {
|
|
|
218
157
|
node: [4, 0, 0]
|
|
219
158
|
},
|
|
220
159
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#browser_compatibility
|
|
221
|
-
|
|
160
|
+
[128 /* Promise */]: {
|
|
222
161
|
es: [6, 0, 0],
|
|
223
162
|
chrome: [32, 0, 0],
|
|
224
163
|
edge: [12, 0, 0],
|
|
@@ -231,7 +170,7 @@ var VERSION_TABLE = {
|
|
|
231
170
|
node: [0, 12, 0]
|
|
232
171
|
},
|
|
233
172
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#browser_compatibility
|
|
234
|
-
|
|
173
|
+
[256 /* Set */]: {
|
|
235
174
|
es: [6, 0, 0],
|
|
236
175
|
chrome: [38, 0, 0],
|
|
237
176
|
edge: [12, 0, 0],
|
|
@@ -244,7 +183,7 @@ var VERSION_TABLE = {
|
|
|
244
183
|
node: [0, 12, 0]
|
|
245
184
|
},
|
|
246
185
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator#browser_compatibility
|
|
247
|
-
|
|
186
|
+
[512 /* SymbolIterator */]: {
|
|
248
187
|
es: [6, 0, 0],
|
|
249
188
|
chrome: [43, 0, 0],
|
|
250
189
|
edge: [12, 0, 0],
|
|
@@ -257,7 +196,7 @@ var VERSION_TABLE = {
|
|
|
257
196
|
node: [0, 12, 0]
|
|
258
197
|
},
|
|
259
198
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#browser_compatibility
|
|
260
|
-
|
|
199
|
+
[1024 /* TypedArray */]: {
|
|
261
200
|
es: [6, 0, 0],
|
|
262
201
|
chrome: [7, 0, 0],
|
|
263
202
|
edge: [12, 0, 0],
|
|
@@ -268,147 +207,126 @@ var VERSION_TABLE = {
|
|
|
268
207
|
samsung: [1, 0, 0],
|
|
269
208
|
deno: [1, 0, 0],
|
|
270
209
|
node: [0, 10, 0]
|
|
210
|
+
},
|
|
211
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array#browser_compatibility
|
|
212
|
+
[2048 /* BigIntTypedArray */]: {
|
|
213
|
+
es: [2020, 0, 0],
|
|
214
|
+
chrome: [67, 0, 0],
|
|
215
|
+
edge: [79, 0, 0],
|
|
216
|
+
firefox: [68, 0, 0],
|
|
217
|
+
opera: [54, 6, 0],
|
|
218
|
+
safari: [15, 1, 0],
|
|
219
|
+
ios: [15, 2, 0],
|
|
220
|
+
samsung: [9, 0, 0],
|
|
221
|
+
deno: [1, 0, 0],
|
|
222
|
+
node: [10, 4, 0]
|
|
271
223
|
}
|
|
272
224
|
};
|
|
273
|
-
var
|
|
225
|
+
var platforms = [
|
|
226
|
+
"chrome",
|
|
227
|
+
"deno",
|
|
228
|
+
"edge",
|
|
229
|
+
"es",
|
|
230
|
+
"firefox",
|
|
231
|
+
"ios",
|
|
232
|
+
"node",
|
|
233
|
+
"opera",
|
|
234
|
+
"safari",
|
|
235
|
+
"samsung"
|
|
236
|
+
];
|
|
237
|
+
function getPlatform(target) {
|
|
238
|
+
for (let i = 0, len = platforms.length; i < len; i++) {
|
|
239
|
+
const platform = target.substring(0, platforms[i].length);
|
|
240
|
+
if (platform === platforms[i]) {
|
|
241
|
+
return platform;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
throw new Error(`Invalid target "${target}"`);
|
|
245
|
+
}
|
|
274
246
|
function parseTarget(target) {
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
const [, platform, version] = result;
|
|
247
|
+
const platform = getPlatform(target);
|
|
248
|
+
const version = target.substring(platform.length);
|
|
278
249
|
const [major, minor = "0", patch = "0"] = version.split(".");
|
|
279
250
|
return [platform, [Number(major), Number(minor), Number(patch)]];
|
|
280
251
|
}
|
|
281
252
|
function getTargetVersions(targets) {
|
|
282
253
|
if (Array.isArray(targets)) {
|
|
283
254
|
const versions = [];
|
|
284
|
-
for (
|
|
285
|
-
versions.push(parseTarget(
|
|
255
|
+
for (let i = 0, len = targets.length; i < len; i++) {
|
|
256
|
+
versions.push(parseTarget(targets[i]));
|
|
286
257
|
}
|
|
287
258
|
return versions;
|
|
288
259
|
}
|
|
289
260
|
return [parseTarget(targets)];
|
|
290
261
|
}
|
|
291
|
-
function isFeatureSupported(key, targets) {
|
|
292
|
-
const base = VERSION_TABLE[key];
|
|
293
|
-
let flag = true;
|
|
294
|
-
for (const [platform, version] of targets) {
|
|
295
|
-
const baseVersion = base[platform];
|
|
296
|
-
flag = flag && !!baseVersion && compareVersion(baseVersion, version) <= 0;
|
|
297
|
-
}
|
|
298
|
-
return flag;
|
|
299
|
-
}
|
|
300
262
|
function parseTargets(targets) {
|
|
301
263
|
const parsed = getTargetVersions(targets);
|
|
302
|
-
|
|
303
|
-
for (const key
|
|
304
|
-
|
|
305
|
-
|
|
264
|
+
let flags = 0;
|
|
265
|
+
for (const key of Object.keys(VERSION_TABLE)) {
|
|
266
|
+
const base = VERSION_TABLE[key];
|
|
267
|
+
let flag = true;
|
|
268
|
+
for (const [platform, version] of parsed) {
|
|
269
|
+
const baseVersion = base[platform];
|
|
270
|
+
flag = flag && !!baseVersion && compareVersion(baseVersion, version) <= 0;
|
|
271
|
+
}
|
|
272
|
+
if (flag) {
|
|
273
|
+
flags |= Number(key);
|
|
306
274
|
}
|
|
307
275
|
}
|
|
308
276
|
return flags;
|
|
309
277
|
}
|
|
310
278
|
|
|
311
|
-
// src/
|
|
279
|
+
// src/get-identifier.ts
|
|
280
|
+
var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
|
|
281
|
+
var REF_START_CHARS_LEN = REF_START_CHARS.length;
|
|
282
|
+
var REF_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_";
|
|
283
|
+
var REF_CHARS_LEN = REF_CHARS.length;
|
|
284
|
+
function getIdentifier(index) {
|
|
285
|
+
let mod = index % REF_START_CHARS_LEN;
|
|
286
|
+
let ref = REF_START_CHARS[mod];
|
|
287
|
+
index = (index - mod) / REF_START_CHARS_LEN;
|
|
288
|
+
while (index > 0) {
|
|
289
|
+
mod = index % REF_CHARS_LEN;
|
|
290
|
+
ref += REF_CHARS[mod];
|
|
291
|
+
index = (index - mod) / REF_CHARS_LEN;
|
|
292
|
+
}
|
|
293
|
+
return ref;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// src/context.ts
|
|
312
297
|
var DEFAULT_OPTIONS = {
|
|
313
298
|
target: "es2023"
|
|
314
299
|
};
|
|
315
|
-
function
|
|
300
|
+
function createParserContext(options = {}) {
|
|
316
301
|
const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
|
|
317
302
|
return {
|
|
318
|
-
markedRefs:
|
|
319
|
-
stack: [],
|
|
303
|
+
markedRefs: {},
|
|
320
304
|
refs: /* @__PURE__ */ new Map(),
|
|
321
|
-
vars: [],
|
|
322
|
-
assignments: [],
|
|
323
|
-
validRefs: /* @__PURE__ */ new Map(),
|
|
324
305
|
features: parseTargets(result.target)
|
|
325
306
|
};
|
|
326
307
|
}
|
|
327
|
-
function
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
function mergeAssignments(assignments) {
|
|
340
|
-
const newAssignments = [];
|
|
341
|
-
let current = assignments[0];
|
|
342
|
-
let prev = current;
|
|
343
|
-
for (let i = 1, len = assignments.length; i < len; i++) {
|
|
344
|
-
const item = assignments[i];
|
|
345
|
-
if (item.type === prev.type) {
|
|
346
|
-
if (item.type === "index" && item.value === prev.value) {
|
|
347
|
-
current = {
|
|
348
|
-
type: "index",
|
|
349
|
-
source: item.source,
|
|
350
|
-
value: getAssignmentExpression(current)
|
|
351
|
-
};
|
|
352
|
-
} else if (item.type === "map" && item.source === prev.source) {
|
|
353
|
-
current = {
|
|
354
|
-
type: "map",
|
|
355
|
-
source: getAssignmentExpression(current),
|
|
356
|
-
key: item.key,
|
|
357
|
-
value: item.value
|
|
358
|
-
};
|
|
359
|
-
} else if (item.type === "set" && item.source === prev.source) {
|
|
360
|
-
current = {
|
|
361
|
-
type: "set",
|
|
362
|
-
source: getAssignmentExpression(current),
|
|
363
|
-
value: item.value
|
|
364
|
-
};
|
|
365
|
-
} else {
|
|
366
|
-
newAssignments.push(current);
|
|
367
|
-
current = item;
|
|
368
|
-
}
|
|
369
|
-
} else {
|
|
370
|
-
newAssignments.push(current);
|
|
371
|
-
current = item;
|
|
372
|
-
}
|
|
373
|
-
prev = item;
|
|
374
|
-
}
|
|
375
|
-
newAssignments.push(current);
|
|
376
|
-
return newAssignments;
|
|
377
|
-
}
|
|
378
|
-
function resolveAssignments(assignments) {
|
|
379
|
-
if (assignments.length) {
|
|
380
|
-
let result = "";
|
|
381
|
-
const merged = mergeAssignments(assignments);
|
|
382
|
-
for (let i = 0, len = merged.length; i < len; i++) {
|
|
383
|
-
result += `${getAssignmentExpression(merged[i])},`;
|
|
384
|
-
}
|
|
385
|
-
return result;
|
|
386
|
-
}
|
|
387
|
-
return void 0;
|
|
388
|
-
}
|
|
389
|
-
function resolvePatches(ctx) {
|
|
390
|
-
return resolveAssignments(ctx.assignments);
|
|
308
|
+
function createSerializationContext(options) {
|
|
309
|
+
return {
|
|
310
|
+
stack: [],
|
|
311
|
+
vars: [],
|
|
312
|
+
assignments: [],
|
|
313
|
+
validRefs: [],
|
|
314
|
+
refSize: 0,
|
|
315
|
+
features: options.features,
|
|
316
|
+
markedRefs: options.markedRefs
|
|
317
|
+
};
|
|
391
318
|
}
|
|
392
319
|
function markRef(ctx, current) {
|
|
393
|
-
ctx.markedRefs[current] =
|
|
394
|
-
}
|
|
395
|
-
function createRef(ctx, index) {
|
|
396
|
-
const current = ctx.refs.get(index);
|
|
397
|
-
if (current != null) {
|
|
398
|
-
return current;
|
|
399
|
-
}
|
|
400
|
-
const id = ctx.refs.size;
|
|
401
|
-
ctx.refs.set(index, id);
|
|
402
|
-
return id;
|
|
320
|
+
ctx.markedRefs[current] = 1;
|
|
403
321
|
}
|
|
404
322
|
function createValidRef(ctx, index) {
|
|
405
|
-
const current = ctx.validRefs
|
|
406
|
-
if (current
|
|
407
|
-
|
|
323
|
+
const current = ctx.validRefs[index];
|
|
324
|
+
if (current == null) {
|
|
325
|
+
const value = ctx.refSize++;
|
|
326
|
+
ctx.validRefs[index] = value;
|
|
327
|
+
return value;
|
|
408
328
|
}
|
|
409
|
-
|
|
410
|
-
ctx.validRefs.set(index, id);
|
|
411
|
-
return id;
|
|
329
|
+
return current;
|
|
412
330
|
}
|
|
413
331
|
function getRefParam(ctx, index) {
|
|
414
332
|
const actualIndex = createValidRef(ctx, index);
|
|
@@ -419,84 +337,105 @@ function getRefParam(ctx, index) {
|
|
|
419
337
|
ctx.vars[actualIndex] = result;
|
|
420
338
|
return result;
|
|
421
339
|
}
|
|
422
|
-
function
|
|
423
|
-
|
|
424
|
-
|
|
340
|
+
function createRef(ctx, current, mark) {
|
|
341
|
+
const ref = ctx.refs.get(current);
|
|
342
|
+
if (ref != null) {
|
|
343
|
+
if (mark) {
|
|
344
|
+
markRef(ctx, ref);
|
|
345
|
+
}
|
|
346
|
+
return ref;
|
|
425
347
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
ctx.assignments.push({
|
|
430
|
-
type: "index",
|
|
431
|
-
source,
|
|
432
|
-
value
|
|
433
|
-
});
|
|
434
|
-
}
|
|
435
|
-
function createSetAdd(ctx, ref, value) {
|
|
436
|
-
markRef(ctx, ref);
|
|
437
|
-
ctx.assignments.push({
|
|
438
|
-
type: "set",
|
|
439
|
-
source: getRefParam(ctx, ref),
|
|
440
|
-
value
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
function createMapSet(ctx, ref, key, value) {
|
|
444
|
-
markRef(ctx, ref);
|
|
445
|
-
ctx.assignments.push({
|
|
446
|
-
type: "map",
|
|
447
|
-
source: getRefParam(ctx, ref),
|
|
448
|
-
key,
|
|
449
|
-
value
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
function createArrayAssign(ctx, ref, index, value) {
|
|
453
|
-
markRef(ctx, ref);
|
|
454
|
-
createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
|
|
455
|
-
}
|
|
456
|
-
function createObjectIdentifierAssign(ctx, ref, key, value) {
|
|
457
|
-
markRef(ctx, ref);
|
|
458
|
-
createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
|
|
459
|
-
}
|
|
460
|
-
function createObjectComputedAssign(ctx, ref, key, value) {
|
|
461
|
-
markRef(ctx, ref);
|
|
462
|
-
createAssignment(ctx, `${getRefParam(ctx, ref)}[${key}]`, value);
|
|
348
|
+
const id = ctx.refs.size;
|
|
349
|
+
ctx.refs.set(current, id);
|
|
350
|
+
return id;
|
|
463
351
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
return "RangeError";
|
|
470
|
-
}
|
|
471
|
-
if (error instanceof ReferenceError) {
|
|
472
|
-
return "ReferenceError";
|
|
473
|
-
}
|
|
474
|
-
if (error instanceof SyntaxError) {
|
|
475
|
-
return "SyntaxError";
|
|
476
|
-
}
|
|
477
|
-
if (error instanceof TypeError) {
|
|
478
|
-
return "TypeError";
|
|
479
|
-
}
|
|
480
|
-
if (error instanceof URIError) {
|
|
481
|
-
return "URIError";
|
|
352
|
+
|
|
353
|
+
// src/assert.ts
|
|
354
|
+
function assert(cond, error) {
|
|
355
|
+
if (!cond) {
|
|
356
|
+
throw new Error(error);
|
|
482
357
|
}
|
|
483
|
-
return "Error";
|
|
484
358
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
359
|
+
|
|
360
|
+
// src/quote.ts
|
|
361
|
+
function quote(str) {
|
|
362
|
+
let result = "";
|
|
363
|
+
let lastPos = 0;
|
|
364
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
|
365
|
+
let replacement;
|
|
366
|
+
switch (str[i]) {
|
|
367
|
+
case '"':
|
|
368
|
+
replacement = '\\"';
|
|
369
|
+
break;
|
|
370
|
+
case "\\":
|
|
371
|
+
replacement = "\\\\";
|
|
372
|
+
break;
|
|
373
|
+
case "<":
|
|
374
|
+
replacement = "\\x3C";
|
|
375
|
+
break;
|
|
376
|
+
case "\n":
|
|
377
|
+
replacement = "\\n";
|
|
378
|
+
break;
|
|
379
|
+
case "\r":
|
|
380
|
+
replacement = "\\r";
|
|
381
|
+
break;
|
|
382
|
+
case "\u2028":
|
|
383
|
+
replacement = "\\u2028";
|
|
384
|
+
break;
|
|
385
|
+
case "\u2029":
|
|
386
|
+
replacement = "\\u2029";
|
|
387
|
+
break;
|
|
388
|
+
default:
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
result += str.slice(lastPos, i) + replacement;
|
|
392
|
+
lastPos = i + 1;
|
|
393
|
+
}
|
|
394
|
+
if (lastPos === 0) {
|
|
395
|
+
result = str;
|
|
396
|
+
} else {
|
|
397
|
+
result += str.slice(lastPos);
|
|
398
|
+
}
|
|
399
|
+
return `"${result}"`;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// src/tree/shared.ts
|
|
403
|
+
function getErrorConstructor(error) {
|
|
404
|
+
if (error instanceof EvalError) {
|
|
405
|
+
return "EvalError";
|
|
406
|
+
}
|
|
407
|
+
if (error instanceof RangeError) {
|
|
408
|
+
return "RangeError";
|
|
409
|
+
}
|
|
410
|
+
if (error instanceof ReferenceError) {
|
|
411
|
+
return "ReferenceError";
|
|
412
|
+
}
|
|
413
|
+
if (error instanceof SyntaxError) {
|
|
414
|
+
return "SyntaxError";
|
|
415
|
+
}
|
|
416
|
+
if (error instanceof TypeError) {
|
|
417
|
+
return "TypeError";
|
|
418
|
+
}
|
|
419
|
+
if (error instanceof URIError) {
|
|
420
|
+
return "URIError";
|
|
421
|
+
}
|
|
422
|
+
return "Error";
|
|
423
|
+
}
|
|
424
|
+
function getErrorOptions(error) {
|
|
425
|
+
let options;
|
|
426
|
+
const constructor = getErrorConstructor(error);
|
|
427
|
+
if (error.name !== constructor) {
|
|
428
|
+
options = { name: error.name };
|
|
429
|
+
} else if (error.constructor.name !== constructor) {
|
|
430
|
+
options = { name: error.constructor.name };
|
|
431
|
+
}
|
|
432
|
+
const names = Object.getOwnPropertyNames(error);
|
|
433
|
+
for (const name of names) {
|
|
434
|
+
if (name !== "name" && name !== "message") {
|
|
435
|
+
options = options || {};
|
|
436
|
+
options[name] = error[name];
|
|
437
|
+
}
|
|
438
|
+
}
|
|
500
439
|
return options;
|
|
501
440
|
}
|
|
502
441
|
function getIterableOptions(obj) {
|
|
@@ -511,641 +450,1050 @@ function getIterableOptions(obj) {
|
|
|
511
450
|
return void 0;
|
|
512
451
|
}
|
|
513
452
|
function isReferenceInStack(ctx, node) {
|
|
514
|
-
return node
|
|
515
|
-
}
|
|
516
|
-
function generateRef(ctx, current) {
|
|
517
|
-
const ref = ctx.refs.has(current);
|
|
518
|
-
if (ref) {
|
|
519
|
-
const refID = ctx.refs.get(current) || 0;
|
|
520
|
-
markRef(ctx, refID);
|
|
521
|
-
return [1 /* Reference */, refID];
|
|
522
|
-
}
|
|
523
|
-
const id = ctx.refs.size;
|
|
524
|
-
ctx.refs.set(current, id);
|
|
525
|
-
return id;
|
|
453
|
+
return node.t === 2 /* Reference */ && ctx.stack.includes(node.i);
|
|
526
454
|
}
|
|
527
455
|
function generateSemiPrimitiveValue(ctx, current, id) {
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
}
|
|
577
|
-
if (constructorCheck(current, BigUint64Array)) {
|
|
578
|
-
assert(
|
|
579
|
-
ctx.features.has("typed-arrays") && ctx.features.has("bigint"),
|
|
580
|
-
'Unsupported value type "BigUint64Array"'
|
|
581
|
-
);
|
|
582
|
-
return [13 /* TypedArray */, ["BigUint64Array", current], id];
|
|
583
|
-
}
|
|
584
|
-
return void 0;
|
|
585
|
-
}
|
|
586
|
-
function generateTreeSync(ctx, current) {
|
|
587
|
-
function serializeProperties(properties) {
|
|
588
|
-
const keyNodes = [];
|
|
589
|
-
const valueNodes = [];
|
|
590
|
-
const deferredKeys = [];
|
|
591
|
-
const deferredValues = [];
|
|
592
|
-
const keys = Object.keys(properties);
|
|
593
|
-
let deferredSize = 0;
|
|
594
|
-
for (const key of keys) {
|
|
595
|
-
if (isIterable(properties[key])) {
|
|
596
|
-
deferredKeys.push(key);
|
|
597
|
-
deferredValues.push(properties[key]);
|
|
598
|
-
deferredSize++;
|
|
599
|
-
} else {
|
|
600
|
-
keyNodes.push(key);
|
|
601
|
-
valueNodes.push(generateTreeSync(ctx, properties[key]));
|
|
602
|
-
}
|
|
456
|
+
const cs = current.constructor;
|
|
457
|
+
switch (cs) {
|
|
458
|
+
case Date:
|
|
459
|
+
return {
|
|
460
|
+
t: 3 /* Date */,
|
|
461
|
+
i: id,
|
|
462
|
+
a: void 0,
|
|
463
|
+
s: current.toISOString(),
|
|
464
|
+
l: void 0,
|
|
465
|
+
m: void 0,
|
|
466
|
+
c: void 0,
|
|
467
|
+
d: void 0,
|
|
468
|
+
n: void 0
|
|
469
|
+
};
|
|
470
|
+
case RegExp:
|
|
471
|
+
return {
|
|
472
|
+
t: 4 /* RegExp */,
|
|
473
|
+
i: id,
|
|
474
|
+
a: void 0,
|
|
475
|
+
s: String(current),
|
|
476
|
+
l: void 0,
|
|
477
|
+
m: void 0,
|
|
478
|
+
c: void 0,
|
|
479
|
+
d: void 0,
|
|
480
|
+
n: void 0
|
|
481
|
+
};
|
|
482
|
+
case Int8Array:
|
|
483
|
+
case Int16Array:
|
|
484
|
+
case Int32Array:
|
|
485
|
+
case Uint8Array:
|
|
486
|
+
case Uint16Array:
|
|
487
|
+
case Uint32Array:
|
|
488
|
+
case Uint8ClampedArray:
|
|
489
|
+
case Float32Array:
|
|
490
|
+
case Float64Array: {
|
|
491
|
+
const constructor = cs.name;
|
|
492
|
+
assert(ctx.features & 1024 /* TypedArray */, `Unsupported value type "${constructor}"`);
|
|
493
|
+
return {
|
|
494
|
+
t: 14 /* TypedArray */,
|
|
495
|
+
i: id,
|
|
496
|
+
a: void 0,
|
|
497
|
+
s: current.toString(),
|
|
498
|
+
l: current.byteOffset,
|
|
499
|
+
m: void 0,
|
|
500
|
+
c: constructor,
|
|
501
|
+
d: void 0,
|
|
502
|
+
n: void 0
|
|
503
|
+
};
|
|
603
504
|
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
505
|
+
case BigInt64Array:
|
|
506
|
+
case BigUint64Array: {
|
|
507
|
+
const constructor = cs.name;
|
|
508
|
+
assert(
|
|
509
|
+
ctx.features & 2048 /* BigIntTypedArray */,
|
|
510
|
+
`Unsupported value type "${constructor}"`
|
|
511
|
+
);
|
|
512
|
+
let result = "";
|
|
513
|
+
const cap = current.length - 1;
|
|
514
|
+
for (let i = 0; i < cap; i++) {
|
|
515
|
+
result += `${current[i]}n,`;
|
|
516
|
+
}
|
|
517
|
+
result += `"${current[cap]}"`;
|
|
518
|
+
return {
|
|
519
|
+
t: 14 /* TypedArray */,
|
|
520
|
+
i: id,
|
|
521
|
+
a: void 0,
|
|
522
|
+
s: result,
|
|
523
|
+
l: current.byteOffset,
|
|
524
|
+
m: void 0,
|
|
525
|
+
c: constructor,
|
|
526
|
+
d: void 0,
|
|
527
|
+
n: void 0
|
|
528
|
+
};
|
|
607
529
|
}
|
|
608
|
-
|
|
530
|
+
default:
|
|
531
|
+
return void 0;
|
|
609
532
|
}
|
|
610
|
-
|
|
611
|
-
|
|
533
|
+
}
|
|
534
|
+
function serializePrimitive(value) {
|
|
535
|
+
if (value === true) {
|
|
536
|
+
return "!0";
|
|
612
537
|
}
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
return id;
|
|
538
|
+
if (value === false) {
|
|
539
|
+
return "!1";
|
|
616
540
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
return semiPrimitive;
|
|
541
|
+
if (value === void 0) {
|
|
542
|
+
return "void 0";
|
|
620
543
|
}
|
|
621
|
-
if (
|
|
622
|
-
|
|
623
|
-
const nodes = [];
|
|
624
|
-
const deferred = [];
|
|
625
|
-
for (const item of current.keys()) {
|
|
626
|
-
if (isIterable(item)) {
|
|
627
|
-
deferred.push(item);
|
|
628
|
-
} else {
|
|
629
|
-
nodes.push(generateTreeSync(ctx, item));
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
for (let i = 0, len = deferred.length; i < len; i++) {
|
|
633
|
-
nodes.push(generateTreeSync(ctx, deferred[i]));
|
|
634
|
-
}
|
|
635
|
-
return [4 /* Set */, nodes, id];
|
|
636
|
-
}
|
|
637
|
-
if (constructorCheck(current, Map)) {
|
|
638
|
-
assert(ctx.features.has("map"), 'Unsupported type "Map"');
|
|
639
|
-
const keyNodes = [];
|
|
640
|
-
const valueNodes = [];
|
|
641
|
-
const deferredKey = [];
|
|
642
|
-
const deferredValue = [];
|
|
643
|
-
let deferredSize = 0;
|
|
644
|
-
for (const [key, value] of current.entries()) {
|
|
645
|
-
if (isIterable(key) || isIterable(value)) {
|
|
646
|
-
deferredKey.push(key);
|
|
647
|
-
deferredValue.push(value);
|
|
648
|
-
deferredSize++;
|
|
649
|
-
} else {
|
|
650
|
-
keyNodes.push(generateTreeSync(ctx, key));
|
|
651
|
-
valueNodes.push(generateTreeSync(ctx, value));
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
for (let i = 0; i < deferredSize; i++) {
|
|
655
|
-
keyNodes.push(generateTreeSync(ctx, deferredKey[i]));
|
|
656
|
-
valueNodes.push(generateTreeSync(ctx, deferredValue[i]));
|
|
657
|
-
}
|
|
658
|
-
return [5 /* Map */, [keyNodes, valueNodes, current.size], id];
|
|
544
|
+
if (value === null) {
|
|
545
|
+
return null;
|
|
659
546
|
}
|
|
660
|
-
if (
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
547
|
+
if (typeof value === "string") {
|
|
548
|
+
return quote(value);
|
|
549
|
+
}
|
|
550
|
+
if (Object.is(value, -0)) {
|
|
551
|
+
return "-0";
|
|
552
|
+
}
|
|
553
|
+
if (Object.is(value, Infinity)) {
|
|
554
|
+
return "1/0";
|
|
555
|
+
}
|
|
556
|
+
if (Object.is(value, -Infinity)) {
|
|
557
|
+
return "-1/0";
|
|
558
|
+
}
|
|
559
|
+
return value;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// src/tree/async.ts
|
|
563
|
+
async function serializePropertiesAsync(ctx, properties) {
|
|
564
|
+
const keys = Object.keys(properties);
|
|
565
|
+
const size = keys.length;
|
|
566
|
+
const keyNodes = new Array(size);
|
|
567
|
+
const valueNodes = new Array(size);
|
|
568
|
+
const deferredKeys = new Array(size);
|
|
569
|
+
const deferredValues = new Array(size);
|
|
570
|
+
let deferredSize = 0;
|
|
571
|
+
let nodesSize = 0;
|
|
572
|
+
for (const key of keys) {
|
|
573
|
+
if (isIterable(properties[key])) {
|
|
574
|
+
deferredKeys[deferredSize] = key;
|
|
575
|
+
deferredValues[deferredSize] = properties[key];
|
|
576
|
+
deferredSize++;
|
|
577
|
+
} else {
|
|
578
|
+
keyNodes[nodesSize] = key;
|
|
579
|
+
valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
|
|
580
|
+
nodesSize++;
|
|
672
581
|
}
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
582
|
+
}
|
|
583
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
584
|
+
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
585
|
+
valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
|
|
586
|
+
}
|
|
587
|
+
return {
|
|
588
|
+
k: keyNodes,
|
|
589
|
+
v: valueNodes,
|
|
590
|
+
s: size
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
async function generateSetNode(ctx, current, id) {
|
|
594
|
+
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
595
|
+
const len = current.size;
|
|
596
|
+
const nodes = new Array(len);
|
|
597
|
+
const deferred = new Array(len);
|
|
598
|
+
let deferredSize = 0;
|
|
599
|
+
let nodeSize = 0;
|
|
600
|
+
for (const item of current.keys()) {
|
|
601
|
+
if (isIterable(item)) {
|
|
602
|
+
deferred[deferredSize++] = item;
|
|
603
|
+
} else {
|
|
604
|
+
nodes[nodeSize++] = await generateTreeAsync(ctx, item);
|
|
677
605
|
}
|
|
678
|
-
return [6 /* Array */, nodes, id];
|
|
679
606
|
}
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
const optionsNode = options ? serializeProperties(options) : void 0;
|
|
683
|
-
const errorsNode = generateTreeSync(ctx, current.errors);
|
|
684
|
-
return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
607
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
608
|
+
nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
|
|
685
609
|
}
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
const
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
610
|
+
return {
|
|
611
|
+
t: 5 /* Set */,
|
|
612
|
+
i: id,
|
|
613
|
+
a: nodes,
|
|
614
|
+
s: void 0,
|
|
615
|
+
l: void 0,
|
|
616
|
+
m: void 0,
|
|
617
|
+
c: void 0,
|
|
618
|
+
d: void 0,
|
|
619
|
+
n: void 0
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
async function generateMapNode(ctx, current, id) {
|
|
623
|
+
assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
|
|
624
|
+
const len = current.size;
|
|
625
|
+
const keyNodes = new Array(len);
|
|
626
|
+
const valueNodes = new Array(len);
|
|
627
|
+
const deferredKey = new Array(len);
|
|
628
|
+
const deferredValue = new Array(len);
|
|
629
|
+
let deferredSize = 0;
|
|
630
|
+
let nodeSize = 0;
|
|
631
|
+
for (const [key, value] of current.entries()) {
|
|
632
|
+
if (isIterable(key) || isIterable(value)) {
|
|
633
|
+
deferredKey[deferredSize] = key;
|
|
634
|
+
deferredValue[deferredSize] = value;
|
|
635
|
+
deferredSize++;
|
|
636
|
+
} else {
|
|
637
|
+
keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
|
|
638
|
+
valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
|
|
639
|
+
nodeSize++;
|
|
640
|
+
}
|
|
711
641
|
}
|
|
712
|
-
|
|
642
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
643
|
+
keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
|
|
644
|
+
valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
|
|
645
|
+
}
|
|
646
|
+
return {
|
|
647
|
+
t: 6 /* Map */,
|
|
648
|
+
i: id,
|
|
649
|
+
a: void 0,
|
|
650
|
+
s: void 0,
|
|
651
|
+
l: void 0,
|
|
652
|
+
m: void 0,
|
|
653
|
+
c: void 0,
|
|
654
|
+
d: { k: keyNodes, v: valueNodes, s: len },
|
|
655
|
+
n: void 0
|
|
656
|
+
};
|
|
713
657
|
}
|
|
714
|
-
async function
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
for (const key of keys) {
|
|
723
|
-
if (isIterable(properties[key])) {
|
|
724
|
-
deferredKeys.push(key);
|
|
725
|
-
deferredValues.push(properties[key]);
|
|
726
|
-
deferredSize++;
|
|
658
|
+
async function generateNodeList(ctx, current) {
|
|
659
|
+
const size = current.length;
|
|
660
|
+
const nodes = new Array(size);
|
|
661
|
+
const deferred = new Array(size);
|
|
662
|
+
for (let i = 0; i < size; i++) {
|
|
663
|
+
if (i in current) {
|
|
664
|
+
if (isIterable(current[i])) {
|
|
665
|
+
deferred[i] = current[i];
|
|
727
666
|
} else {
|
|
728
|
-
|
|
729
|
-
valueNodes.push(await generateTreeAsync(ctx, properties[key]));
|
|
667
|
+
nodes[i] = await generateTreeAsync(ctx, current[i]);
|
|
730
668
|
}
|
|
731
669
|
}
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
670
|
+
}
|
|
671
|
+
for (let i = 0; i < size; i++) {
|
|
672
|
+
if (i in deferred) {
|
|
673
|
+
nodes[i] = await generateTreeAsync(ctx, deferred[i]);
|
|
735
674
|
}
|
|
736
|
-
return [keyNodes, valueNodes, keys.length];
|
|
737
675
|
}
|
|
676
|
+
return nodes;
|
|
677
|
+
}
|
|
678
|
+
async function generateArrayNode(ctx, current, id) {
|
|
679
|
+
return {
|
|
680
|
+
t: 7 /* Array */,
|
|
681
|
+
i: id,
|
|
682
|
+
a: await generateNodeList(ctx, current),
|
|
683
|
+
s: void 0,
|
|
684
|
+
l: void 0,
|
|
685
|
+
m: void 0,
|
|
686
|
+
c: void 0,
|
|
687
|
+
d: void 0,
|
|
688
|
+
n: void 0
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
async function generateAggregateErrorNode(ctx, current, id) {
|
|
692
|
+
const options = getErrorOptions(current);
|
|
693
|
+
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
694
|
+
return {
|
|
695
|
+
t: 12 /* AggregateError */,
|
|
696
|
+
i: id,
|
|
697
|
+
a: void 0,
|
|
698
|
+
s: void 0,
|
|
699
|
+
l: void 0,
|
|
700
|
+
m: current.message,
|
|
701
|
+
c: void 0,
|
|
702
|
+
d: optionsNode,
|
|
703
|
+
n: await generateTreeAsync(ctx, current.errors)
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
async function generateErrorNode(ctx, current, id) {
|
|
707
|
+
const options = getErrorOptions(current);
|
|
708
|
+
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
709
|
+
return {
|
|
710
|
+
t: 11 /* Error */,
|
|
711
|
+
i: id,
|
|
712
|
+
a: void 0,
|
|
713
|
+
s: void 0,
|
|
714
|
+
l: void 0,
|
|
715
|
+
m: current.message,
|
|
716
|
+
c: getErrorConstructor(current),
|
|
717
|
+
d: optionsNode,
|
|
718
|
+
n: void 0
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
async function generateIterableNode(ctx, current, id) {
|
|
722
|
+
assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
|
|
723
|
+
const options = getIterableOptions(current);
|
|
724
|
+
return {
|
|
725
|
+
t: 13 /* Iterable */,
|
|
726
|
+
i: id,
|
|
727
|
+
s: void 0,
|
|
728
|
+
l: void 0,
|
|
729
|
+
m: void 0,
|
|
730
|
+
c: void 0,
|
|
731
|
+
// Parse options first before the items
|
|
732
|
+
d: options ? await serializePropertiesAsync(ctx, options) : void 0,
|
|
733
|
+
a: await generateNodeList(ctx, Array.from(current)),
|
|
734
|
+
n: void 0
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
async function generateTreeAsync(ctx, current) {
|
|
738
738
|
if (isPrimitive(current)) {
|
|
739
|
-
return
|
|
739
|
+
return {
|
|
740
|
+
t: 0 /* Primitive */,
|
|
741
|
+
i: void 0,
|
|
742
|
+
s: serializePrimitive(current),
|
|
743
|
+
l: void 0,
|
|
744
|
+
m: void 0,
|
|
745
|
+
c: void 0,
|
|
746
|
+
// Parse options first before the items
|
|
747
|
+
d: void 0,
|
|
748
|
+
a: void 0,
|
|
749
|
+
n: void 0
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
if (typeof current === "bigint") {
|
|
753
|
+
assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
|
|
754
|
+
return {
|
|
755
|
+
t: 1 /* BigInt */,
|
|
756
|
+
i: void 0,
|
|
757
|
+
a: void 0,
|
|
758
|
+
s: `${current}n`,
|
|
759
|
+
l: void 0,
|
|
760
|
+
m: void 0,
|
|
761
|
+
c: void 0,
|
|
762
|
+
d: void 0,
|
|
763
|
+
n: void 0
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
const id = createRef(ctx, current, true);
|
|
767
|
+
if (ctx.markedRefs[id]) {
|
|
768
|
+
return {
|
|
769
|
+
t: 2 /* Reference */,
|
|
770
|
+
i: id,
|
|
771
|
+
a: void 0,
|
|
772
|
+
s: void 0,
|
|
773
|
+
l: void 0,
|
|
774
|
+
m: void 0,
|
|
775
|
+
c: void 0,
|
|
776
|
+
d: void 0,
|
|
777
|
+
n: void 0
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
if (Array.isArray(current)) {
|
|
781
|
+
return generateArrayNode(ctx, current, id);
|
|
782
|
+
}
|
|
783
|
+
const cs = current.constructor;
|
|
784
|
+
const empty = cs == null;
|
|
785
|
+
if (cs === Object || empty) {
|
|
786
|
+
if (Symbol.iterator in current) {
|
|
787
|
+
return generateIterableNode(ctx, current, id);
|
|
788
|
+
}
|
|
789
|
+
return {
|
|
790
|
+
t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
791
|
+
i: id,
|
|
792
|
+
s: void 0,
|
|
793
|
+
l: void 0,
|
|
794
|
+
m: void 0,
|
|
795
|
+
c: void 0,
|
|
796
|
+
// Parse options first before the items
|
|
797
|
+
d: await serializePropertiesAsync(ctx, current),
|
|
798
|
+
a: void 0,
|
|
799
|
+
n: void 0
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
if (cs === Set) {
|
|
803
|
+
return generateSetNode(ctx, current, id);
|
|
740
804
|
}
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
return id;
|
|
805
|
+
if (cs === Map) {
|
|
806
|
+
return generateMapNode(ctx, current, id);
|
|
744
807
|
}
|
|
745
808
|
const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
|
|
746
809
|
if (semiPrimitive) {
|
|
747
810
|
return semiPrimitive;
|
|
748
811
|
}
|
|
749
|
-
if (
|
|
750
|
-
|
|
751
|
-
return current.then(async (value) => [
|
|
752
|
-
9 /* Promise */,
|
|
753
|
-
await generateTreeAsync(ctx, value),
|
|
754
|
-
id
|
|
755
|
-
]);
|
|
756
|
-
}
|
|
757
|
-
if (constructorCheck(current, Set)) {
|
|
758
|
-
assert(ctx.features.has("set"), 'Unsupported type "Set"');
|
|
759
|
-
const nodes = [];
|
|
760
|
-
const deferred = [];
|
|
761
|
-
for (const item of current.keys()) {
|
|
762
|
-
if (isIterable(item)) {
|
|
763
|
-
deferred.push(item);
|
|
764
|
-
} else {
|
|
765
|
-
nodes.push(await generateTreeAsync(ctx, item));
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
for (let i = 0, len = deferred.length; i < len; i++) {
|
|
769
|
-
nodes.push(await generateTreeAsync(ctx, deferred[i]));
|
|
770
|
-
}
|
|
771
|
-
return [4 /* Set */, nodes, id];
|
|
772
|
-
}
|
|
773
|
-
if (constructorCheck(current, Map)) {
|
|
774
|
-
assert(ctx.features.has("map"), 'Unsupported type "Map"');
|
|
775
|
-
const keyNodes = [];
|
|
776
|
-
const valueNodes = [];
|
|
777
|
-
const deferredKey = [];
|
|
778
|
-
const deferredValue = [];
|
|
779
|
-
let deferredSize = 0;
|
|
780
|
-
for (const [key, value] of current.entries()) {
|
|
781
|
-
if (isIterable(key) || isIterable(value)) {
|
|
782
|
-
deferredKey.push(key);
|
|
783
|
-
deferredValue.push(value);
|
|
784
|
-
deferredSize++;
|
|
785
|
-
} else {
|
|
786
|
-
keyNodes.push(await generateTreeAsync(ctx, key));
|
|
787
|
-
valueNodes.push(await generateTreeAsync(ctx, value));
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
for (let i = 0; i < deferredSize; i++) {
|
|
791
|
-
keyNodes.push(await generateTreeAsync(ctx, deferredKey[i]));
|
|
792
|
-
valueNodes.push(await generateTreeAsync(ctx, deferredValue[i]));
|
|
793
|
-
}
|
|
794
|
-
return [5 /* Map */, [keyNodes, valueNodes, current.size], id];
|
|
812
|
+
if (Symbol.iterator in current) {
|
|
813
|
+
return generateIterableNode(ctx, current, id);
|
|
795
814
|
}
|
|
796
|
-
if (
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
}
|
|
810
|
-
for (let i = 0; i < size; i++) {
|
|
811
|
-
if (i in deferred) {
|
|
812
|
-
nodes[i] = await generateTreeAsync(ctx, deferred[i]);
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
return [6 /* Array */, nodes, id];
|
|
815
|
+
if (isPromise(current)) {
|
|
816
|
+
assert(ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
|
|
817
|
+
return current.then(async (value) => ({
|
|
818
|
+
t: 10 /* Promise */,
|
|
819
|
+
i: id,
|
|
820
|
+
s: void 0,
|
|
821
|
+
l: void 0,
|
|
822
|
+
m: void 0,
|
|
823
|
+
c: void 0,
|
|
824
|
+
// Parse options first before the items
|
|
825
|
+
d: void 0,
|
|
826
|
+
a: void 0,
|
|
827
|
+
n: await generateTreeAsync(ctx, value)
|
|
828
|
+
}));
|
|
816
829
|
}
|
|
817
|
-
if (current instanceof AggregateError) {
|
|
818
|
-
|
|
819
|
-
const optionsNode = options ? await serializeProperties(options) : void 0;
|
|
820
|
-
const errorsNode = await generateTreeAsync(ctx, current.errors);
|
|
821
|
-
return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
830
|
+
if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
|
|
831
|
+
return generateAggregateErrorNode(ctx, current, id);
|
|
822
832
|
}
|
|
823
833
|
if (current instanceof Error) {
|
|
824
|
-
|
|
825
|
-
const optionsNode = options ? await serializeProperties(options) : void 0;
|
|
826
|
-
return [
|
|
827
|
-
10 /* Error */,
|
|
828
|
-
[getErrorConstructor(current), current.message, optionsNode],
|
|
829
|
-
id
|
|
830
|
-
];
|
|
831
|
-
}
|
|
832
|
-
if (isIterable(current)) {
|
|
833
|
-
assert(ctx.features.has("symbol-iterator"), 'Unsupported type "Iterable"');
|
|
834
|
-
const options = getIterableOptions(current);
|
|
835
|
-
return [12 /* Iterable */, [
|
|
836
|
-
options ? await serializeProperties(options) : void 0,
|
|
837
|
-
await generateTreeAsync(ctx, Array.from(current))
|
|
838
|
-
], id];
|
|
839
|
-
}
|
|
840
|
-
const empty = current.constructor == null;
|
|
841
|
-
if (current.constructor === Object || empty) {
|
|
842
|
-
return [
|
|
843
|
-
empty ? 8 /* NullConstructor */ : 7 /* Object */,
|
|
844
|
-
await serializeProperties(current),
|
|
845
|
-
id
|
|
846
|
-
];
|
|
834
|
+
return generateErrorNode(ctx, current, id);
|
|
847
835
|
}
|
|
848
836
|
throw new Error("Unsupported value");
|
|
849
837
|
}
|
|
850
|
-
function
|
|
851
|
-
|
|
852
|
-
|
|
838
|
+
async function parseAsync(ctx, current) {
|
|
839
|
+
const result = await generateTreeAsync(ctx, current);
|
|
840
|
+
return [result, createRef(ctx, current, false), result.t === 8 /* Object */];
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// src/tree/serialize.ts
|
|
844
|
+
function getAssignmentExpression(assignment) {
|
|
845
|
+
switch (assignment.type) {
|
|
846
|
+
case "index":
|
|
847
|
+
return `${assignment.source}=${assignment.value}`;
|
|
848
|
+
case "map":
|
|
849
|
+
return `${assignment.source}.set(${assignment.key},${assignment.value})`;
|
|
850
|
+
case "set":
|
|
851
|
+
return `${assignment.source}.add(${assignment.value})`;
|
|
852
|
+
default:
|
|
853
|
+
return "";
|
|
853
854
|
}
|
|
854
|
-
|
|
855
|
-
|
|
855
|
+
}
|
|
856
|
+
function mergeAssignments(assignments) {
|
|
857
|
+
const newAssignments = [];
|
|
858
|
+
let current = assignments[0];
|
|
859
|
+
let prev = current;
|
|
860
|
+
for (let i = 1, len = assignments.length; i < len; i++) {
|
|
861
|
+
const item = assignments[i];
|
|
862
|
+
if (item.type === prev.type) {
|
|
863
|
+
if (item.type === "index" && item.value === prev.value) {
|
|
864
|
+
current = {
|
|
865
|
+
type: "index",
|
|
866
|
+
source: item.source,
|
|
867
|
+
value: getAssignmentExpression(current)
|
|
868
|
+
};
|
|
869
|
+
} else if (item.type === "map" && item.source === prev.source) {
|
|
870
|
+
current = {
|
|
871
|
+
type: "map",
|
|
872
|
+
source: getAssignmentExpression(current),
|
|
873
|
+
key: item.key,
|
|
874
|
+
value: item.value
|
|
875
|
+
};
|
|
876
|
+
} else if (item.type === "set" && item.source === prev.source) {
|
|
877
|
+
current = {
|
|
878
|
+
type: "set",
|
|
879
|
+
source: getAssignmentExpression(current),
|
|
880
|
+
value: item.value
|
|
881
|
+
};
|
|
882
|
+
} else {
|
|
883
|
+
newAssignments.push(current);
|
|
884
|
+
current = item;
|
|
885
|
+
}
|
|
886
|
+
} else {
|
|
887
|
+
newAssignments.push(current);
|
|
888
|
+
current = item;
|
|
889
|
+
}
|
|
890
|
+
prev = item;
|
|
856
891
|
}
|
|
857
|
-
|
|
858
|
-
|
|
892
|
+
newAssignments.push(current);
|
|
893
|
+
return newAssignments;
|
|
894
|
+
}
|
|
895
|
+
function resolveAssignments(assignments) {
|
|
896
|
+
if (assignments.length) {
|
|
897
|
+
let result = "";
|
|
898
|
+
const merged = mergeAssignments(assignments);
|
|
899
|
+
for (let i = 0, len = merged.length; i < len; i++) {
|
|
900
|
+
result += `${getAssignmentExpression(merged[i])},`;
|
|
901
|
+
}
|
|
902
|
+
return result;
|
|
859
903
|
}
|
|
860
|
-
|
|
861
|
-
|
|
904
|
+
return void 0;
|
|
905
|
+
}
|
|
906
|
+
function resolvePatches(ctx) {
|
|
907
|
+
return resolveAssignments(ctx.assignments);
|
|
908
|
+
}
|
|
909
|
+
function assignRef(ctx, index, value) {
|
|
910
|
+
if (ctx.markedRefs[index]) {
|
|
911
|
+
return `${getRefParam(ctx, index)}=${value}`;
|
|
862
912
|
}
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
913
|
+
return value;
|
|
914
|
+
}
|
|
915
|
+
function createAssignment(ctx, source, value) {
|
|
916
|
+
ctx.assignments.push({
|
|
917
|
+
type: "index",
|
|
918
|
+
source,
|
|
919
|
+
value
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
function createSetAdd(ctx, ref, value) {
|
|
923
|
+
markRef(ctx, ref);
|
|
924
|
+
ctx.assignments.push({
|
|
925
|
+
type: "set",
|
|
926
|
+
source: getRefParam(ctx, ref),
|
|
927
|
+
value
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
function createMapSet(ctx, ref, key, value) {
|
|
931
|
+
markRef(ctx, ref);
|
|
932
|
+
ctx.assignments.push({
|
|
933
|
+
type: "map",
|
|
934
|
+
source: getRefParam(ctx, ref),
|
|
935
|
+
key,
|
|
936
|
+
value
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
function createArrayAssign(ctx, ref, index, value) {
|
|
940
|
+
markRef(ctx, ref);
|
|
941
|
+
createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
|
|
942
|
+
}
|
|
943
|
+
function createObjectAssign(ctx, ref, key, value) {
|
|
944
|
+
markRef(ctx, ref);
|
|
945
|
+
createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
|
|
946
|
+
}
|
|
947
|
+
var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
|
|
948
|
+
function serializeAssignments(ctx, targetRef, node) {
|
|
949
|
+
ctx.stack.push(targetRef);
|
|
950
|
+
const mainAssignments = [];
|
|
951
|
+
for (let i = 0; i < node.s; i++) {
|
|
952
|
+
const parentStack = ctx.stack;
|
|
953
|
+
ctx.stack = [];
|
|
954
|
+
const refParam = serializeTree(ctx, node.v[i]);
|
|
955
|
+
ctx.stack = parentStack;
|
|
956
|
+
const key = node.k[i];
|
|
957
|
+
const check = Number(key);
|
|
958
|
+
const parentAssignment = ctx.assignments;
|
|
959
|
+
ctx.assignments = mainAssignments;
|
|
960
|
+
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
961
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
962
|
+
createObjectAssign(ctx, targetRef, key, refParam);
|
|
963
|
+
} else {
|
|
964
|
+
createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
|
|
965
|
+
}
|
|
966
|
+
ctx.assignments = parentAssignment;
|
|
866
967
|
}
|
|
867
|
-
|
|
868
|
-
|
|
968
|
+
ctx.stack.pop();
|
|
969
|
+
return resolveAssignments(mainAssignments);
|
|
970
|
+
}
|
|
971
|
+
function serializeObject(ctx, sourceID, node) {
|
|
972
|
+
if (node.s === 0) {
|
|
973
|
+
return "{}";
|
|
869
974
|
}
|
|
870
|
-
|
|
871
|
-
|
|
975
|
+
let result = "";
|
|
976
|
+
ctx.stack.push(sourceID);
|
|
977
|
+
for (let i = 0; i < node.s; i++) {
|
|
978
|
+
const key = node.k[i];
|
|
979
|
+
const val = node.v[i];
|
|
980
|
+
const check = Number(key);
|
|
981
|
+
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
982
|
+
if (isReferenceInStack(ctx, val)) {
|
|
983
|
+
const refParam = getRefParam(ctx, val.i);
|
|
984
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
985
|
+
createObjectAssign(ctx, sourceID, key, refParam);
|
|
986
|
+
} else {
|
|
987
|
+
createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
|
|
988
|
+
}
|
|
989
|
+
} else {
|
|
990
|
+
result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
|
|
991
|
+
}
|
|
872
992
|
}
|
|
873
|
-
|
|
874
|
-
|
|
993
|
+
ctx.stack.pop();
|
|
994
|
+
return `{${result.substring(0, result.length - 1)}}`;
|
|
995
|
+
}
|
|
996
|
+
function serializePromise(ctx, node) {
|
|
997
|
+
let serialized;
|
|
998
|
+
if (isReferenceInStack(ctx, node.n)) {
|
|
999
|
+
serialized = `Promise.resolve().then(()=>${getRefParam(ctx, node.n.i)})`;
|
|
1000
|
+
} else {
|
|
1001
|
+
ctx.stack.push(node.i);
|
|
1002
|
+
const result = serializeTree(ctx, node.n);
|
|
1003
|
+
ctx.stack.pop();
|
|
1004
|
+
serialized = `Promise.resolve(${result})`;
|
|
875
1005
|
}
|
|
876
|
-
|
|
877
|
-
|
|
1006
|
+
return assignRef(ctx, node.i, serialized);
|
|
1007
|
+
}
|
|
1008
|
+
function serializeTypedArray(ctx, node) {
|
|
1009
|
+
let args = `[${node.s}]`;
|
|
1010
|
+
if (node.l !== 0) {
|
|
1011
|
+
args += `,${node.l}`;
|
|
878
1012
|
}
|
|
879
|
-
return
|
|
1013
|
+
return assignRef(ctx, node.i, `new ${node.c}(${args})`);
|
|
880
1014
|
}
|
|
881
|
-
function
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
1015
|
+
function serializeSet(ctx, node) {
|
|
1016
|
+
let serialized = "new Set";
|
|
1017
|
+
const size = node.a.length;
|
|
1018
|
+
if (size) {
|
|
1019
|
+
let result = "";
|
|
1020
|
+
ctx.stack.push(node.i);
|
|
885
1021
|
for (let i = 0; i < size; i++) {
|
|
886
|
-
const
|
|
887
|
-
ctx
|
|
888
|
-
|
|
889
|
-
ctx.stack = parentStack;
|
|
890
|
-
const key = keys[i];
|
|
891
|
-
const check = Number(key);
|
|
892
|
-
const parentAssignment = ctx.assignments;
|
|
893
|
-
ctx.assignments = mainAssignments;
|
|
894
|
-
if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.test(key)) {
|
|
895
|
-
if (!Number.isNaN(check)) {
|
|
896
|
-
createObjectComputedAssign(ctx, targetRef, key, refParam);
|
|
897
|
-
} else {
|
|
898
|
-
createObjectIdentifierAssign(ctx, targetRef, key, refParam);
|
|
899
|
-
}
|
|
1022
|
+
const item = node.a[i];
|
|
1023
|
+
if (isReferenceInStack(ctx, item)) {
|
|
1024
|
+
createSetAdd(ctx, node.i, getRefParam(ctx, item.i));
|
|
900
1025
|
} else {
|
|
901
|
-
|
|
1026
|
+
result += `${serializeTree(ctx, item)},`;
|
|
902
1027
|
}
|
|
903
|
-
ctx.assignments = parentAssignment;
|
|
904
1028
|
}
|
|
905
1029
|
ctx.stack.pop();
|
|
906
|
-
|
|
1030
|
+
if (result) {
|
|
1031
|
+
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1032
|
+
}
|
|
907
1033
|
}
|
|
908
|
-
|
|
1034
|
+
return assignRef(ctx, node.i, serialized);
|
|
1035
|
+
}
|
|
1036
|
+
function serializeMap(ctx, node) {
|
|
1037
|
+
let serialized = "new Map";
|
|
1038
|
+
if (node.d.s) {
|
|
909
1039
|
let result = "";
|
|
910
|
-
ctx.stack.push(
|
|
911
|
-
for (let i = 0; i <
|
|
912
|
-
const key =
|
|
913
|
-
const val =
|
|
914
|
-
|
|
915
|
-
|
|
1040
|
+
ctx.stack.push(node.i);
|
|
1041
|
+
for (let i = 0; i < node.d.s; i++) {
|
|
1042
|
+
const key = node.d.k[i];
|
|
1043
|
+
const val = node.d.v[i];
|
|
1044
|
+
if (isReferenceInStack(ctx, key)) {
|
|
1045
|
+
const keyRef = getRefParam(ctx, key.i);
|
|
916
1046
|
if (isReferenceInStack(ctx, val)) {
|
|
917
|
-
const
|
|
918
|
-
|
|
919
|
-
createObjectComputedAssign(ctx, sourceID, key, refParam);
|
|
920
|
-
} else {
|
|
921
|
-
createObjectIdentifierAssign(ctx, sourceID, key, refParam);
|
|
922
|
-
}
|
|
1047
|
+
const valueRef = getRefParam(ctx, val.i);
|
|
1048
|
+
createMapSet(ctx, node.i, keyRef, valueRef);
|
|
923
1049
|
} else {
|
|
924
|
-
|
|
1050
|
+
const parent = ctx.stack;
|
|
1051
|
+
ctx.stack = [];
|
|
1052
|
+
createMapSet(ctx, node.i, keyRef, serializeTree(ctx, val));
|
|
1053
|
+
ctx.stack = parent;
|
|
925
1054
|
}
|
|
926
1055
|
} else if (isReferenceInStack(ctx, val)) {
|
|
927
|
-
const
|
|
928
|
-
|
|
1056
|
+
const valueRef = getRefParam(ctx, val.i);
|
|
1057
|
+
const parent = ctx.stack;
|
|
1058
|
+
ctx.stack = [];
|
|
1059
|
+
createMapSet(ctx, node.i, serializeTree(ctx, key), valueRef);
|
|
1060
|
+
ctx.stack = parent;
|
|
929
1061
|
} else {
|
|
930
|
-
result +=
|
|
1062
|
+
result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
|
|
931
1063
|
}
|
|
932
1064
|
}
|
|
933
1065
|
ctx.stack.pop();
|
|
934
|
-
|
|
1066
|
+
if (result) {
|
|
1067
|
+
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1068
|
+
}
|
|
935
1069
|
}
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
1070
|
+
return assignRef(ctx, node.i, serialized);
|
|
1071
|
+
}
|
|
1072
|
+
function serializeNodeList(ctx, node) {
|
|
1073
|
+
const size = node.a.length;
|
|
1074
|
+
let values = "";
|
|
1075
|
+
for (let i = 0; i < size; i++) {
|
|
1076
|
+
const item = node.a[i];
|
|
1077
|
+
if (item) {
|
|
1078
|
+
if (isReferenceInStack(ctx, item)) {
|
|
1079
|
+
createArrayAssign(ctx, node.i, i, getRefParam(ctx, item.i));
|
|
1080
|
+
values += ",";
|
|
945
1081
|
} else {
|
|
946
|
-
ctx
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
1082
|
+
values += serializeTree(ctx, item);
|
|
1083
|
+
if (i < size - 1) {
|
|
1084
|
+
values += ",";
|
|
1085
|
+
}
|
|
950
1086
|
}
|
|
951
|
-
|
|
1087
|
+
} else {
|
|
1088
|
+
values += ",";
|
|
952
1089
|
}
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
1090
|
+
}
|
|
1091
|
+
return `[${values}]`;
|
|
1092
|
+
}
|
|
1093
|
+
function serializeArray(ctx, node) {
|
|
1094
|
+
ctx.stack.push(node.i);
|
|
1095
|
+
const result = serializeNodeList(ctx, node);
|
|
1096
|
+
ctx.stack.pop();
|
|
1097
|
+
return assignRef(ctx, node.i, result);
|
|
1098
|
+
}
|
|
1099
|
+
function serializeWithObjectAssign(ctx, value, id, serialized) {
|
|
1100
|
+
const fields = serializeObject(ctx, id, value);
|
|
1101
|
+
if (fields !== "{}") {
|
|
1102
|
+
return `Object.assign(${serialized},${fields})`;
|
|
1103
|
+
}
|
|
1104
|
+
return serialized;
|
|
1105
|
+
}
|
|
1106
|
+
function serializeAggregateError(ctx, node) {
|
|
1107
|
+
ctx.stack.push(node.i);
|
|
1108
|
+
let serialized = `new AggregateError(${serializeTree(ctx, node.n)},${quote(node.m)})`;
|
|
1109
|
+
ctx.stack.pop();
|
|
1110
|
+
if (node.d) {
|
|
1111
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
1112
|
+
serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
|
|
1113
|
+
} else {
|
|
1114
|
+
markRef(ctx, node.i);
|
|
1115
|
+
const assignments = serializeAssignments(ctx, node.i, node.d);
|
|
1116
|
+
if (assignments) {
|
|
1117
|
+
const ref = getRefParam(ctx, node.i);
|
|
1118
|
+
return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
|
|
965
1119
|
}
|
|
966
|
-
return assignRef(ctx, id, `new ${value[0]}(${args})`);
|
|
967
1120
|
}
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
}
|
|
982
|
-
ctx.stack.pop();
|
|
983
|
-
if (result) {
|
|
984
|
-
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
985
|
-
}
|
|
1121
|
+
}
|
|
1122
|
+
return assignRef(ctx, node.i, serialized);
|
|
1123
|
+
}
|
|
1124
|
+
function serializeError(ctx, node) {
|
|
1125
|
+
let serialized = `new ${node.c}(${quote(node.m)})`;
|
|
1126
|
+
if (node.d) {
|
|
1127
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
1128
|
+
serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
|
|
1129
|
+
} else {
|
|
1130
|
+
markRef(ctx, node.i);
|
|
1131
|
+
const assignments = serializeAssignments(ctx, node.i, node.d);
|
|
1132
|
+
if (assignments) {
|
|
1133
|
+
const ref = getRefParam(ctx, node.i);
|
|
1134
|
+
return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
|
|
986
1135
|
}
|
|
987
|
-
return assignRef(ctx, id, serialized);
|
|
988
1136
|
}
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
}
|
|
1019
|
-
ctx.stack.pop();
|
|
1020
|
-
if (result) {
|
|
1021
|
-
serialized += `([${result.substring(0, result.length - 1)}])`;
|
|
1022
|
-
}
|
|
1137
|
+
}
|
|
1138
|
+
return assignRef(ctx, node.i, serialized);
|
|
1139
|
+
}
|
|
1140
|
+
function serializeIterable(ctx, node) {
|
|
1141
|
+
const parent = ctx.stack;
|
|
1142
|
+
ctx.stack = [];
|
|
1143
|
+
const values = serializeNodeList(ctx, node);
|
|
1144
|
+
ctx.stack = parent;
|
|
1145
|
+
let serialized;
|
|
1146
|
+
if (ctx.features & 2 /* ArrayPrototypeValues */) {
|
|
1147
|
+
serialized = `${values}.values()`;
|
|
1148
|
+
} else {
|
|
1149
|
+
serialized = `${values}[Symbol.iterator]()`;
|
|
1150
|
+
}
|
|
1151
|
+
if (ctx.features & 4 /* ArrowFunction */) {
|
|
1152
|
+
serialized = `{[Symbol.iterator]:()=>${serialized}}`;
|
|
1153
|
+
} else if (ctx.features & 32 /* MethodShorthand */) {
|
|
1154
|
+
serialized = `{[Symbol.iterator](){return ${serialized}}}`;
|
|
1155
|
+
} else {
|
|
1156
|
+
serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
|
|
1157
|
+
}
|
|
1158
|
+
if (node.d) {
|
|
1159
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
1160
|
+
serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
|
|
1161
|
+
} else {
|
|
1162
|
+
markRef(ctx, node.i);
|
|
1163
|
+
const assignments = serializeAssignments(ctx, node.i, node.d);
|
|
1164
|
+
if (assignments) {
|
|
1165
|
+
const ref = getRefParam(ctx, node.i);
|
|
1166
|
+
return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
|
|
1023
1167
|
}
|
|
1024
|
-
return assignRef(ctx, id, serialized);
|
|
1025
1168
|
}
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
values += ",";
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
} else {
|
|
1043
|
-
values += ",";
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
ctx.stack.pop();
|
|
1047
|
-
return assignRef(ctx, id, `[${values}]`);
|
|
1169
|
+
}
|
|
1170
|
+
return assignRef(ctx, node.i, serialized);
|
|
1171
|
+
}
|
|
1172
|
+
function serializeNullConstructor(ctx, node) {
|
|
1173
|
+
let serialized = "Object.create(null)";
|
|
1174
|
+
if (ctx.features & 64 /* ObjectAssign */) {
|
|
1175
|
+
serialized = serializeWithObjectAssign(ctx, node.d, node.i, serialized);
|
|
1176
|
+
} else {
|
|
1177
|
+
markRef(ctx, node.i);
|
|
1178
|
+
const assignments = serializeAssignments(ctx, node.i, node.d);
|
|
1179
|
+
if (assignments) {
|
|
1180
|
+
const ref = getRefParam(ctx, node.i);
|
|
1181
|
+
return `(${assignRef(ctx, node.i, serialized)},${assignments}${ref})`;
|
|
1048
1182
|
}
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1183
|
+
}
|
|
1184
|
+
return assignRef(ctx, node.i, serialized);
|
|
1185
|
+
}
|
|
1186
|
+
function serializeTree(ctx, node) {
|
|
1187
|
+
switch (node.t) {
|
|
1188
|
+
case 0 /* Primitive */:
|
|
1189
|
+
return String(node.s);
|
|
1190
|
+
case 2 /* Reference */:
|
|
1191
|
+
return getRefParam(ctx, node.i);
|
|
1192
|
+
case 10 /* Promise */:
|
|
1193
|
+
return serializePromise(ctx, node);
|
|
1194
|
+
case 1 /* BigInt */:
|
|
1195
|
+
return node.s;
|
|
1196
|
+
case 3 /* Date */:
|
|
1197
|
+
return assignRef(ctx, node.i, `new Date("${node.s}")`);
|
|
1198
|
+
case 4 /* RegExp */:
|
|
1199
|
+
return assignRef(ctx, node.i, node.s);
|
|
1200
|
+
case 15 /* BigIntTypedArray */:
|
|
1201
|
+
case 14 /* TypedArray */:
|
|
1202
|
+
return serializeTypedArray(ctx, node);
|
|
1203
|
+
case 5 /* Set */:
|
|
1204
|
+
return serializeSet(ctx, node);
|
|
1205
|
+
case 6 /* Map */:
|
|
1206
|
+
return serializeMap(ctx, node);
|
|
1207
|
+
case 7 /* Array */:
|
|
1208
|
+
return serializeArray(ctx, node);
|
|
1209
|
+
case 12 /* AggregateError */:
|
|
1210
|
+
return serializeAggregateError(ctx, node);
|
|
1211
|
+
case 11 /* Error */:
|
|
1212
|
+
return serializeError(ctx, node);
|
|
1213
|
+
case 13 /* Iterable */:
|
|
1214
|
+
return serializeIterable(ctx, node);
|
|
1215
|
+
case 9 /* NullConstructor */:
|
|
1216
|
+
return serializeNullConstructor(ctx, node);
|
|
1217
|
+
case 8 /* Object */:
|
|
1218
|
+
return assignRef(ctx, node.i, serializeObject(ctx, node.i, node.d));
|
|
1219
|
+
default:
|
|
1220
|
+
throw new Error("Unsupported type");
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
// src/tree/sync.ts
|
|
1225
|
+
function serializePropertiesSync(ctx, properties) {
|
|
1226
|
+
const keys = Object.keys(properties);
|
|
1227
|
+
const size = keys.length;
|
|
1228
|
+
const keyNodes = new Array(size);
|
|
1229
|
+
const valueNodes = new Array(size);
|
|
1230
|
+
const deferredKeys = new Array(size);
|
|
1231
|
+
const deferredValues = new Array(size);
|
|
1232
|
+
let deferredSize = 0;
|
|
1233
|
+
let nodesSize = 0;
|
|
1234
|
+
for (const key of keys) {
|
|
1235
|
+
if (isIterable(properties[key])) {
|
|
1236
|
+
deferredKeys[deferredSize] = key;
|
|
1237
|
+
deferredValues[deferredSize] = properties[key];
|
|
1238
|
+
deferredSize++;
|
|
1239
|
+
} else {
|
|
1240
|
+
keyNodes[nodesSize] = key;
|
|
1241
|
+
valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
|
|
1242
|
+
nodesSize++;
|
|
1067
1243
|
}
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1244
|
+
}
|
|
1245
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1246
|
+
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
1247
|
+
valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
|
|
1248
|
+
}
|
|
1249
|
+
return {
|
|
1250
|
+
k: keyNodes,
|
|
1251
|
+
v: valueNodes,
|
|
1252
|
+
s: size
|
|
1253
|
+
};
|
|
1254
|
+
}
|
|
1255
|
+
function generateSetNode2(ctx, current, id) {
|
|
1256
|
+
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
1257
|
+
const len = current.size;
|
|
1258
|
+
const nodes = new Array(len);
|
|
1259
|
+
const deferred = new Array(len);
|
|
1260
|
+
let deferredSize = 0;
|
|
1261
|
+
let nodeSize = 0;
|
|
1262
|
+
for (const item of current.keys()) {
|
|
1263
|
+
if (isIterable(item)) {
|
|
1264
|
+
deferred[deferredSize++] = item;
|
|
1265
|
+
} else {
|
|
1266
|
+
nodes[nodeSize++] = generateTreeSync(ctx, item);
|
|
1084
1267
|
}
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1268
|
+
}
|
|
1269
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1270
|
+
nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
|
|
1271
|
+
}
|
|
1272
|
+
return {
|
|
1273
|
+
t: 5 /* Set */,
|
|
1274
|
+
i: id,
|
|
1275
|
+
a: nodes,
|
|
1276
|
+
s: void 0,
|
|
1277
|
+
l: void 0,
|
|
1278
|
+
m: void 0,
|
|
1279
|
+
c: void 0,
|
|
1280
|
+
d: void 0,
|
|
1281
|
+
n: void 0
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
function generateMapNode2(ctx, current, id) {
|
|
1285
|
+
assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
|
|
1286
|
+
const len = current.size;
|
|
1287
|
+
const keyNodes = new Array(len);
|
|
1288
|
+
const valueNodes = new Array(len);
|
|
1289
|
+
const deferredKey = new Array(len);
|
|
1290
|
+
const deferredValue = new Array(len);
|
|
1291
|
+
let deferredSize = 0;
|
|
1292
|
+
let nodeSize = 0;
|
|
1293
|
+
for (const [key, value] of current.entries()) {
|
|
1294
|
+
if (isIterable(key) || isIterable(value)) {
|
|
1295
|
+
deferredKey[deferredSize] = key;
|
|
1296
|
+
deferredValue[deferredSize] = value;
|
|
1297
|
+
deferredSize++;
|
|
1298
|
+
} else {
|
|
1299
|
+
keyNodes[nodeSize] = generateTreeSync(ctx, key);
|
|
1300
|
+
valueNodes[nodeSize] = generateTreeSync(ctx, value);
|
|
1301
|
+
nodeSize++;
|
|
1117
1302
|
}
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1303
|
+
}
|
|
1304
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1305
|
+
keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
|
|
1306
|
+
valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
|
|
1307
|
+
}
|
|
1308
|
+
return {
|
|
1309
|
+
t: 6 /* Map */,
|
|
1310
|
+
i: id,
|
|
1311
|
+
a: void 0,
|
|
1312
|
+
s: void 0,
|
|
1313
|
+
l: void 0,
|
|
1314
|
+
m: void 0,
|
|
1315
|
+
c: void 0,
|
|
1316
|
+
d: { k: keyNodes, v: valueNodes, s: len },
|
|
1317
|
+
n: void 0
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
function generateNodeList2(ctx, current) {
|
|
1321
|
+
const size = current.length;
|
|
1322
|
+
const nodes = new Array(size);
|
|
1323
|
+
const deferred = new Array(size);
|
|
1324
|
+
for (let i = 0; i < size; i++) {
|
|
1325
|
+
if (i in current) {
|
|
1326
|
+
if (isIterable(current[i])) {
|
|
1327
|
+
deferred[i] = current[i];
|
|
1125
1328
|
} else {
|
|
1126
|
-
|
|
1127
|
-
const assignments = serializeAssignments(id, value);
|
|
1128
|
-
if (assignments) {
|
|
1129
|
-
const ref = getRefParam(ctx, id);
|
|
1130
|
-
return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
|
|
1131
|
-
}
|
|
1329
|
+
nodes[i] = generateTreeSync(ctx, current[i]);
|
|
1132
1330
|
}
|
|
1133
|
-
return assignRef(ctx, id, serialized);
|
|
1134
1331
|
}
|
|
1135
|
-
case 7 /* Object */:
|
|
1136
|
-
return assignRef(ctx, id, serializeObject(id, value));
|
|
1137
|
-
default:
|
|
1138
|
-
throw new Error("Unsupported type");
|
|
1139
1332
|
}
|
|
1333
|
+
for (let i = 0; i < size; i++) {
|
|
1334
|
+
if (i in deferred) {
|
|
1335
|
+
nodes[i] = generateTreeSync(ctx, deferred[i]);
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
return nodes;
|
|
1339
|
+
}
|
|
1340
|
+
function generateArrayNode2(ctx, current, id) {
|
|
1341
|
+
return {
|
|
1342
|
+
t: 7 /* Array */,
|
|
1343
|
+
i: id,
|
|
1344
|
+
a: generateNodeList2(ctx, current),
|
|
1345
|
+
s: void 0,
|
|
1346
|
+
l: void 0,
|
|
1347
|
+
m: void 0,
|
|
1348
|
+
c: void 0,
|
|
1349
|
+
d: void 0,
|
|
1350
|
+
n: void 0
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
1353
|
+
function generateAggregateErrorNode2(ctx, current, id) {
|
|
1354
|
+
const options = getErrorOptions(current);
|
|
1355
|
+
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
1356
|
+
return {
|
|
1357
|
+
t: 12 /* AggregateError */,
|
|
1358
|
+
i: id,
|
|
1359
|
+
a: void 0,
|
|
1360
|
+
s: void 0,
|
|
1361
|
+
l: void 0,
|
|
1362
|
+
m: current.message,
|
|
1363
|
+
c: void 0,
|
|
1364
|
+
d: optionsNode,
|
|
1365
|
+
n: generateTreeSync(ctx, current.errors)
|
|
1366
|
+
};
|
|
1367
|
+
}
|
|
1368
|
+
function generateErrorNode2(ctx, current, id) {
|
|
1369
|
+
const options = getErrorOptions(current);
|
|
1370
|
+
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
1371
|
+
return {
|
|
1372
|
+
t: 11 /* Error */,
|
|
1373
|
+
i: id,
|
|
1374
|
+
a: void 0,
|
|
1375
|
+
s: void 0,
|
|
1376
|
+
l: void 0,
|
|
1377
|
+
m: current.message,
|
|
1378
|
+
c: getErrorConstructor(current),
|
|
1379
|
+
d: optionsNode,
|
|
1380
|
+
n: void 0
|
|
1381
|
+
};
|
|
1382
|
+
}
|
|
1383
|
+
function generateIterableNode2(ctx, current, id) {
|
|
1384
|
+
assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
|
|
1385
|
+
const options = getIterableOptions(current);
|
|
1386
|
+
return {
|
|
1387
|
+
t: 13 /* Iterable */,
|
|
1388
|
+
i: id,
|
|
1389
|
+
s: void 0,
|
|
1390
|
+
l: void 0,
|
|
1391
|
+
m: void 0,
|
|
1392
|
+
c: void 0,
|
|
1393
|
+
// Parse options first before the items
|
|
1394
|
+
d: options ? serializePropertiesSync(ctx, options) : void 0,
|
|
1395
|
+
a: generateNodeList2(ctx, Array.from(current)),
|
|
1396
|
+
n: void 0
|
|
1397
|
+
};
|
|
1398
|
+
}
|
|
1399
|
+
function generateTreeSync(ctx, current) {
|
|
1400
|
+
if (isPrimitive(current)) {
|
|
1401
|
+
return {
|
|
1402
|
+
t: 0 /* Primitive */,
|
|
1403
|
+
i: void 0,
|
|
1404
|
+
s: serializePrimitive(current),
|
|
1405
|
+
l: void 0,
|
|
1406
|
+
m: void 0,
|
|
1407
|
+
c: void 0,
|
|
1408
|
+
// Parse options first before the items
|
|
1409
|
+
d: void 0,
|
|
1410
|
+
a: void 0,
|
|
1411
|
+
n: void 0
|
|
1412
|
+
};
|
|
1413
|
+
}
|
|
1414
|
+
if (typeof current === "bigint") {
|
|
1415
|
+
assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
|
|
1416
|
+
return {
|
|
1417
|
+
t: 1 /* BigInt */,
|
|
1418
|
+
i: void 0,
|
|
1419
|
+
a: void 0,
|
|
1420
|
+
s: `${current}n`,
|
|
1421
|
+
l: void 0,
|
|
1422
|
+
m: void 0,
|
|
1423
|
+
c: void 0,
|
|
1424
|
+
d: void 0,
|
|
1425
|
+
n: void 0
|
|
1426
|
+
};
|
|
1427
|
+
}
|
|
1428
|
+
const id = createRef(ctx, current, true);
|
|
1429
|
+
if (ctx.markedRefs[id]) {
|
|
1430
|
+
return {
|
|
1431
|
+
t: 2 /* Reference */,
|
|
1432
|
+
i: id,
|
|
1433
|
+
a: void 0,
|
|
1434
|
+
s: void 0,
|
|
1435
|
+
l: void 0,
|
|
1436
|
+
m: void 0,
|
|
1437
|
+
c: void 0,
|
|
1438
|
+
d: void 0,
|
|
1439
|
+
n: void 0
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
1442
|
+
if (Array.isArray(current)) {
|
|
1443
|
+
return generateArrayNode2(ctx, current, id);
|
|
1444
|
+
}
|
|
1445
|
+
const cs = current.constructor;
|
|
1446
|
+
const empty = cs == null;
|
|
1447
|
+
if (cs === Object || empty) {
|
|
1448
|
+
if (Symbol.iterator in current) {
|
|
1449
|
+
return generateIterableNode2(ctx, current, id);
|
|
1450
|
+
}
|
|
1451
|
+
return {
|
|
1452
|
+
t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
1453
|
+
i: id,
|
|
1454
|
+
s: void 0,
|
|
1455
|
+
l: void 0,
|
|
1456
|
+
m: void 0,
|
|
1457
|
+
c: void 0,
|
|
1458
|
+
// Parse options first before the items
|
|
1459
|
+
d: serializePropertiesSync(ctx, current),
|
|
1460
|
+
a: void 0,
|
|
1461
|
+
n: void 0
|
|
1462
|
+
};
|
|
1463
|
+
}
|
|
1464
|
+
if (cs === Set) {
|
|
1465
|
+
return generateSetNode2(ctx, current, id);
|
|
1466
|
+
}
|
|
1467
|
+
if (cs === Map) {
|
|
1468
|
+
return generateMapNode2(ctx, current, id);
|
|
1469
|
+
}
|
|
1470
|
+
const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
|
|
1471
|
+
if (semiPrimitive) {
|
|
1472
|
+
return semiPrimitive;
|
|
1473
|
+
}
|
|
1474
|
+
if (Symbol.iterator in current) {
|
|
1475
|
+
return generateIterableNode2(ctx, current, id);
|
|
1476
|
+
}
|
|
1477
|
+
if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
|
|
1478
|
+
return generateAggregateErrorNode2(ctx, current, id);
|
|
1479
|
+
}
|
|
1480
|
+
if (current instanceof Error) {
|
|
1481
|
+
return generateErrorNode2(ctx, current, id);
|
|
1482
|
+
}
|
|
1483
|
+
throw new Error("Unsupported value");
|
|
1484
|
+
}
|
|
1485
|
+
function parseSync(ctx, current) {
|
|
1486
|
+
const result = generateTreeSync(ctx, current);
|
|
1487
|
+
return [result, createRef(ctx, current, false), result.t === 8 /* Object */];
|
|
1140
1488
|
}
|
|
1141
1489
|
|
|
1142
1490
|
// src/index.ts
|
|
1143
|
-
function finalize(ctx,
|
|
1491
|
+
function finalize(ctx, rootID, isObject, result) {
|
|
1144
1492
|
if (ctx.vars.length) {
|
|
1145
1493
|
const patches = resolvePatches(ctx);
|
|
1146
1494
|
let body = result;
|
|
1147
1495
|
if (patches) {
|
|
1148
|
-
const index = getRefParam(ctx,
|
|
1496
|
+
const index = getRefParam(ctx, rootID);
|
|
1149
1497
|
if (result.startsWith(`${index}=`)) {
|
|
1150
1498
|
body = `${result},${patches}${index}`;
|
|
1151
1499
|
} else {
|
|
@@ -1153,37 +1501,73 @@ function finalize(ctx, source, result) {
|
|
|
1153
1501
|
}
|
|
1154
1502
|
}
|
|
1155
1503
|
let params = ctx.vars.length > 1 ? ctx.vars.join(",") : ctx.vars[0];
|
|
1156
|
-
if (ctx.features
|
|
1504
|
+
if (ctx.features & 4 /* ArrowFunction */) {
|
|
1157
1505
|
params = ctx.vars.length > 1 || ctx.vars.length === 0 ? `(${params})` : params;
|
|
1158
1506
|
return `(${params}=>(${body}))()`;
|
|
1159
1507
|
}
|
|
1160
1508
|
return `(function(${params}){return ${body}})()`;
|
|
1161
1509
|
}
|
|
1162
|
-
if (
|
|
1510
|
+
if (isObject) {
|
|
1163
1511
|
return `(${result})`;
|
|
1164
1512
|
}
|
|
1165
1513
|
return result;
|
|
1166
1514
|
}
|
|
1167
1515
|
function serialize(source, options) {
|
|
1168
|
-
const ctx =
|
|
1516
|
+
const ctx = createParserContext(options);
|
|
1169
1517
|
if (isPrimitive(source)) {
|
|
1170
|
-
return serializePrimitive(
|
|
1518
|
+
return String(serializePrimitive(source));
|
|
1171
1519
|
}
|
|
1172
|
-
const tree =
|
|
1173
|
-
const
|
|
1174
|
-
|
|
1520
|
+
const [tree, rootID, isObject] = parseSync(ctx, source);
|
|
1521
|
+
const serial = createSerializationContext(ctx);
|
|
1522
|
+
const result = serializeTree(serial, tree);
|
|
1523
|
+
return finalize(serial, rootID, isObject, result);
|
|
1175
1524
|
}
|
|
1176
1525
|
async function serializeAsync(source, options) {
|
|
1177
|
-
const ctx =
|
|
1526
|
+
const ctx = createParserContext(options);
|
|
1178
1527
|
if (isPrimitive(source)) {
|
|
1179
|
-
return serializePrimitive(
|
|
1528
|
+
return String(serializePrimitive(source));
|
|
1180
1529
|
}
|
|
1181
|
-
const tree = await
|
|
1182
|
-
const
|
|
1183
|
-
|
|
1530
|
+
const [tree, rootID, isObject] = await parseAsync(ctx, source);
|
|
1531
|
+
const serial = createSerializationContext(ctx);
|
|
1532
|
+
const result = serializeTree(serial, tree);
|
|
1533
|
+
return finalize(serial, rootID, isObject, result);
|
|
1184
1534
|
}
|
|
1185
1535
|
function deserialize(source) {
|
|
1186
1536
|
return (0, eval)(source);
|
|
1187
1537
|
}
|
|
1538
|
+
function toJSON(source, options) {
|
|
1539
|
+
const ctx = createParserContext(options);
|
|
1540
|
+
const [tree, root, isObject] = parseSync(ctx, source);
|
|
1541
|
+
return JSON.stringify([
|
|
1542
|
+
tree,
|
|
1543
|
+
root,
|
|
1544
|
+
isObject,
|
|
1545
|
+
ctx.features,
|
|
1546
|
+
ctx.markedRefs
|
|
1547
|
+
]);
|
|
1548
|
+
}
|
|
1549
|
+
async function toJSONAsync(source, options) {
|
|
1550
|
+
const ctx = createParserContext(options);
|
|
1551
|
+
const [tree, root, isObject] = await parseAsync(ctx, source);
|
|
1552
|
+
return JSON.stringify([
|
|
1553
|
+
tree,
|
|
1554
|
+
root,
|
|
1555
|
+
isObject,
|
|
1556
|
+
ctx.features,
|
|
1557
|
+
ctx.markedRefs
|
|
1558
|
+
]);
|
|
1559
|
+
}
|
|
1560
|
+
function compileJSON(source) {
|
|
1561
|
+
const parsed = JSON.parse(source);
|
|
1562
|
+
const serial = createSerializationContext({
|
|
1563
|
+
features: parsed[3],
|
|
1564
|
+
markedRefs: parsed[4]
|
|
1565
|
+
});
|
|
1566
|
+
const result = serializeTree(serial, parsed[0]);
|
|
1567
|
+
return finalize(serial, parsed[1], parsed[2], result);
|
|
1568
|
+
}
|
|
1569
|
+
function fromJSON(source) {
|
|
1570
|
+
return deserialize(compileJSON(source));
|
|
1571
|
+
}
|
|
1188
1572
|
var src_default = serialize;
|
|
1189
1573
|
//# sourceMappingURL=index.cjs.map
|