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.
@@ -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" || type === "bigint";
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
- return Array.isArray(obj) || constructorCheck(obj, Set) || constructorCheck(obj, Map);
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
- "aggregate-error": {
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
- "array-values": {
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
- "arrow-function": {
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
- bigint: {
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
- map: {
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
- "method-shorthand": {
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
- "object-assign": {
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
- promise: {
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
- set: {
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
- "symbol-iterator": {
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
- "typed-arrays": {
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 TARGET_REGEX = /^(es|chrome|edge|safari|firefox|opera|ios|samsung|deno|node)([0-9]+(\.[0-9]+(\.[0-9]+)?)?)$/i;
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 result = TARGET_REGEX.exec(target);
276
- assert(result, `Invalid target "${target}"`);
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 (const target of targets) {
285
- versions.push(parseTarget(target));
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
- const flags = /* @__PURE__ */ new Set();
303
- for (const key in VERSION_TABLE) {
304
- if (isFeatureSupported(key, parsed)) {
305
- flags.add(key);
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/tree.ts
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 createSerializationContext(options = {}) {
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 getAssignmentExpression(assignment) {
328
- switch (assignment.type) {
329
- case "index":
330
- return `${assignment.source}=${assignment.value}`;
331
- case "map":
332
- return `${assignment.source}.set(${assignment.key},${assignment.value})`;
333
- case "set":
334
- return `${assignment.source}.add(${assignment.value})`;
335
- default:
336
- return "";
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] = true;
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.get(index);
406
- if (current != null) {
407
- return current;
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
- const id = ctx.validRefs.size;
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 assignRef(ctx, index, value) {
423
- if (ctx.markedRefs[index]) {
424
- return `${getRefParam(ctx, index)}=${value}`;
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
- return value;
427
- }
428
- function createAssignment(ctx, source, value) {
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
- function getErrorConstructor(error) {
465
- if (error instanceof EvalError) {
466
- return "EvalError";
467
- }
468
- if (error instanceof RangeError) {
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
- function getErrorOptions(error) {
486
- let options;
487
- const constructor = getErrorConstructor(error);
488
- if (error.name !== constructor) {
489
- options = { name: error.name };
490
- } else if (error.constructor.name !== constructor) {
491
- options = { name: error.constructor.name };
492
- }
493
- const names = Object.getOwnPropertyNames(error);
494
- for (const name of names) {
495
- if (name !== "name" && name !== "message") {
496
- options = options || {};
497
- options[name] = error[name];
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[0] === 1 /* Reference */ && ctx.stack.includes(node[1]);
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
- if (constructorCheck(current, Date)) {
529
- return [2 /* Date */, current, id];
530
- }
531
- if (constructorCheck(current, RegExp)) {
532
- return [3 /* RegExp */, current, id];
533
- }
534
- if (constructorCheck(current, Int8Array)) {
535
- assert(ctx.features.has("typed-arrays"), 'Unsupported value type "Int8Array"');
536
- return [13 /* TypedArray */, ["Int8Array", current], id];
537
- }
538
- if (constructorCheck(current, Int16Array)) {
539
- assert(ctx.features.has("typed-arrays"), 'Unsupported value type "Int16Array"');
540
- return [13 /* TypedArray */, ["Int16Array", current], id];
541
- }
542
- if (constructorCheck(current, Int32Array)) {
543
- assert(ctx.features.has("typed-arrays"), 'Unsupported value type "Int32Array"');
544
- return [13 /* TypedArray */, ["Int32Array", current], id];
545
- }
546
- if (constructorCheck(current, Uint8Array)) {
547
- assert(ctx.features.has("typed-arrays"), 'Unsupported value type "Uint8Array"');
548
- return [13 /* TypedArray */, ["Uint8Array", current], id];
549
- }
550
- if (constructorCheck(current, Uint16Array)) {
551
- assert(ctx.features.has("typed-arrays"), 'Unsupported value type "Uint16Array"');
552
- return [13 /* TypedArray */, ["Uint16Array", current], id];
553
- }
554
- if (constructorCheck(current, Uint32Array)) {
555
- assert(ctx.features.has("typed-arrays"), 'Unsupported value type "Uint32Array"');
556
- return [13 /* TypedArray */, ["Uint32Array", current], id];
557
- }
558
- if (constructorCheck(current, Uint8ClampedArray)) {
559
- assert(ctx.features.has("typed-arrays"), 'Unsupported value type "Uint8ClampedArray"');
560
- return [13 /* TypedArray */, ["Uint8ClampedArray", current], id];
561
- }
562
- if (constructorCheck(current, Float32Array)) {
563
- assert(ctx.features.has("typed-arrays"), 'Unsupported value type "Float32Array"');
564
- return [13 /* TypedArray */, ["Float32Array", current], id];
565
- }
566
- if (constructorCheck(current, Float64Array)) {
567
- assert(ctx.features.has("typed-arrays"), 'Unsupported value type "Float64Array"');
568
- return [13 /* TypedArray */, ["Float64Array", current], id];
569
- }
570
- if (constructorCheck(current, BigInt64Array)) {
571
- assert(
572
- ctx.features.has("typed-arrays") && ctx.features.has("bigint"),
573
- 'Unsupported value type "BigInt64Array"'
574
- );
575
- return [13 /* TypedArray */, ["BigInt64Array", current], id];
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
- for (let i = 0; i < deferredSize; i++) {
605
- keyNodes.push(deferredKeys[i]);
606
- valueNodes.push(generateTreeSync(ctx, deferredValues[i]));
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
- return [keyNodes, valueNodes, keys.length];
530
+ default:
531
+ return void 0;
609
532
  }
610
- if (isPrimitive(current)) {
611
- return [0 /* Primitive */, current];
533
+ }
534
+ function serializePrimitive(value) {
535
+ if (value === true) {
536
+ return "!0";
612
537
  }
613
- const id = generateRef(ctx, current);
614
- if (Array.isArray(id)) {
615
- return id;
538
+ if (value === false) {
539
+ return "!1";
616
540
  }
617
- const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
618
- if (semiPrimitive) {
619
- return semiPrimitive;
541
+ if (value === void 0) {
542
+ return "void 0";
620
543
  }
621
- if (constructorCheck(current, Set)) {
622
- assert(ctx.features.has("set"), 'Unsupported type "Set"');
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 (Array.isArray(current)) {
661
- const size = current.length;
662
- const nodes = new Array(size);
663
- const deferred = new Array(size);
664
- for (let i = 0; i < size; i++) {
665
- if (i in current) {
666
- if (isIterable(current[i])) {
667
- deferred[i] = current[i];
668
- } else {
669
- nodes[i] = generateTreeSync(ctx, current[i]);
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
- for (let i = 0; i < size; i++) {
674
- if (i in deferred) {
675
- nodes[i] = generateTreeSync(ctx, deferred[i]);
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
- if (current instanceof AggregateError && ctx.features.has("aggregate-error")) {
681
- const options = getErrorOptions(current);
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
- if (current instanceof Error) {
687
- const options = getErrorOptions(current);
688
- const optionsNode = options ? serializeProperties(options) : void 0;
689
- return [
690
- 10 /* Error */,
691
- [getErrorConstructor(current), current.message, optionsNode],
692
- id
693
- ];
694
- }
695
- if (isIterable(current)) {
696
- assert(ctx.features.has("symbol-iterator"), 'Unsupported type "Iterable"');
697
- const options = getIterableOptions(current);
698
- return [12 /* Iterable */, [
699
- // Parse options first before the items
700
- options ? serializeProperties(options) : void 0,
701
- generateTreeSync(ctx, Array.from(current))
702
- ], id];
703
- }
704
- const empty = current.constructor == null;
705
- if (current.constructor === Object || empty) {
706
- return [
707
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
708
- serializeProperties(current),
709
- id
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
- throw new Error("Unsupported value");
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 generateTreeAsync(ctx, current) {
715
- async function serializeProperties(properties) {
716
- const keyNodes = [];
717
- const valueNodes = [];
718
- const deferredKeys = [];
719
- const deferredValues = [];
720
- const keys = Object.keys(properties);
721
- let deferredSize = 0;
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
- keyNodes.push(key);
729
- valueNodes.push(await generateTreeAsync(ctx, properties[key]));
667
+ nodes[i] = await generateTreeAsync(ctx, current[i]);
730
668
  }
731
669
  }
732
- for (let i = 0; i < deferredSize; i++) {
733
- keyNodes.push(deferredKeys[i]);
734
- valueNodes.push(await generateTreeAsync(ctx, deferredValues[i]));
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 [0 /* Primitive */, current];
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
- const id = generateRef(ctx, current);
742
- if (Array.isArray(id)) {
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 (isPromise(current)) {
750
- assert(ctx.features.has("promise"), 'Unsupported type "Promise"');
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 (Array.isArray(current)) {
797
- const size = current.length;
798
- const nodes = new Array(size);
799
- const deferred = new Array(size);
800
- for (let i = 0; i < size; i++) {
801
- const item = current[i];
802
- if (i in current) {
803
- if (isIterable(item)) {
804
- deferred[i] = item;
805
- } else {
806
- nodes[i] = await generateTreeAsync(ctx, item);
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
- const options = getErrorOptions(current);
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
- const options = getErrorOptions(current);
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 serializePrimitive(ctx, value) {
851
- if (value === true) {
852
- return "!0";
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
- if (value === false) {
855
- return "!1";
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
- if (value === void 0) {
858
- return "void 0";
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
- if (value === null) {
861
- return "null";
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
- if (typeof value === "bigint") {
864
- assert(ctx.features.has("bigint"), 'Unsupported type "BigInt"');
865
- return `${value}n`;
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
- if (typeof value === "string") {
868
- return quote(value);
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
- if (Object.is(value, -0)) {
871
- return "-0";
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
- if (Object.is(value, Infinity)) {
874
- return "1/0";
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
- if (Object.is(value, -Infinity)) {
877
- return "-1/0";
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 String(value);
1013
+ return assignRef(ctx, node.i, `new ${node.c}(${args})`);
880
1014
  }
881
- function serializeTree(ctx, [type, value, id]) {
882
- function serializeAssignments(targetRef, [keys, values, size]) {
883
- ctx.stack.push(targetRef);
884
- const mainAssignments = [];
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 parentStack = ctx.stack;
887
- ctx.stack = [];
888
- const refParam = serializeTree(ctx, values[i]);
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
- createObjectComputedAssign(ctx, targetRef, quote(key), refParam);
1026
+ result += `${serializeTree(ctx, item)},`;
902
1027
  }
903
- ctx.assignments = parentAssignment;
904
1028
  }
905
1029
  ctx.stack.pop();
906
- return resolveAssignments(mainAssignments);
1030
+ if (result) {
1031
+ serialized += `([${result.substring(0, result.length - 1)}])`;
1032
+ }
907
1033
  }
908
- function serializeObject(sourceID, [keys, values, size]) {
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(sourceID);
911
- for (let i = 0; i < size; i++) {
912
- const key = keys[i];
913
- const val = values[i];
914
- const check = Number(key);
915
- if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.test(key)) {
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 refParam = getRefParam(ctx, val[1]);
918
- if (!Number.isNaN(check)) {
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
- result += `${key}:${serializeTree(ctx, val)},`;
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 refParam = getRefParam(ctx, val[1]);
928
- createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
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 += `${quote(key)}:${serializeTree(ctx, val)},`;
1062
+ result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
931
1063
  }
932
1064
  }
933
1065
  ctx.stack.pop();
934
- return `{${result.substring(0, result.length - 1)}}`;
1066
+ if (result) {
1067
+ serialized += `([${result.substring(0, result.length - 1)}])`;
1068
+ }
935
1069
  }
936
- switch (type) {
937
- case 0 /* Primitive */:
938
- return serializePrimitive(ctx, value);
939
- case 1 /* Reference */:
940
- return getRefParam(ctx, value);
941
- case 9 /* Promise */: {
942
- let serialized;
943
- if (isReferenceInStack(ctx, value)) {
944
- serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
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.stack.push(id);
947
- const result = serializeTree(ctx, value);
948
- ctx.stack.pop();
949
- serialized = `Promise.resolve(${result})`;
1082
+ values += serializeTree(ctx, item);
1083
+ if (i < size - 1) {
1084
+ values += ",";
1085
+ }
950
1086
  }
951
- return assignRef(ctx, id, serialized);
1087
+ } else {
1088
+ values += ",";
952
1089
  }
953
- case 2 /* Date */:
954
- return assignRef(ctx, id, `new Date("${value.toISOString()}")`);
955
- case 3 /* RegExp */:
956
- return assignRef(ctx, id, String(value));
957
- case 13 /* TypedArray */: {
958
- let values = "";
959
- for (let i = 0, len = value[1].length; i < len; i++) {
960
- values += `${serializePrimitive(ctx, value[1][i])},`;
961
- }
962
- let args = values ? `[${values.substring(0, values.length - 1)}]` : "[]";
963
- if (value[1].byteOffset !== 0) {
964
- args += `,${value[1].byteOffset}`;
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
- case 4 /* Set */: {
969
- let serialized = "new Set";
970
- const size = value.length;
971
- if (size) {
972
- let result = "";
973
- ctx.stack.push(id);
974
- for (let i = 0; i < size; i++) {
975
- const item = value[i];
976
- if (isReferenceInStack(ctx, item)) {
977
- createSetAdd(ctx, id, getRefParam(ctx, item[1]));
978
- } else {
979
- result += `${serializeTree(ctx, item)},`;
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
- case 5 /* Map */: {
990
- let serialized = "new Map";
991
- const size = value[2];
992
- if (size) {
993
- let result = "";
994
- ctx.stack.push(id);
995
- for (let i = 0; i < size; i++) {
996
- const key = value[0][i];
997
- const val = value[1][i];
998
- if (isReferenceInStack(ctx, key)) {
999
- const keyRef = getRefParam(ctx, key[1]);
1000
- if (isReferenceInStack(ctx, val)) {
1001
- const valueRef = getRefParam(ctx, val[1]);
1002
- createMapSet(ctx, id, keyRef, valueRef);
1003
- } else {
1004
- const parent = ctx.stack;
1005
- ctx.stack = [];
1006
- createMapSet(ctx, id, keyRef, serializeTree(ctx, val));
1007
- ctx.stack = parent;
1008
- }
1009
- } else if (isReferenceInStack(ctx, val)) {
1010
- const valueRef = getRefParam(ctx, val[1]);
1011
- const parent = ctx.stack;
1012
- ctx.stack = [];
1013
- createMapSet(ctx, id, serializeTree(ctx, key), valueRef);
1014
- ctx.stack = parent;
1015
- } else {
1016
- result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
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
- case 6 /* Array */: {
1027
- const size = value.length;
1028
- let values = "";
1029
- ctx.stack.push(id);
1030
- for (let i = 0; i < size; i++) {
1031
- const item = value[i];
1032
- if (i in value) {
1033
- if (isReferenceInStack(ctx, item)) {
1034
- createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
1035
- values += ",";
1036
- } else {
1037
- values += serializeTree(ctx, item);
1038
- if (i < size - 1) {
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
- case 11 /* AggregateError */: {
1050
- ctx.stack.push(id);
1051
- let serialized = `new AggregateError(${serializeTree(ctx, value[2])},${quote(value[0])})`;
1052
- ctx.stack.pop();
1053
- if (value[1]) {
1054
- if (ctx.features.has("object-assign")) {
1055
- const options = serializeObject(id, value[1]);
1056
- serialized = `Object.assign(${serialized},${options})`;
1057
- } else {
1058
- markRef(ctx, id);
1059
- const assignments = serializeAssignments(id, value[1]);
1060
- if (assignments) {
1061
- const ref = getRefParam(ctx, id);
1062
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1063
- }
1064
- }
1065
- }
1066
- return assignRef(ctx, id, serialized);
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
- case 10 /* Error */: {
1069
- let serialized = `new ${value[0]}(${quote(value[1])})`;
1070
- if (value[2]) {
1071
- if (ctx.features.has("object-assign")) {
1072
- const options = serializeObject(id, value[2]);
1073
- serialized = `Object.assign(${serialized},${options})`;
1074
- } else {
1075
- markRef(ctx, id);
1076
- const assignments = serializeAssignments(id, value[2]);
1077
- if (assignments) {
1078
- const ref = getRefParam(ctx, id);
1079
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1080
- }
1081
- }
1082
- }
1083
- return assignRef(ctx, id, serialized);
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
- case 12 /* Iterable */: {
1086
- const parent = ctx.stack;
1087
- ctx.stack = [];
1088
- const values = serializeTree(ctx, value[1]);
1089
- ctx.stack = parent;
1090
- let serialized;
1091
- if (ctx.features.has("array-values")) {
1092
- serialized = `${values}.values()`;
1093
- } else {
1094
- serialized = `${values}[Symbol.iterator]()`;
1095
- }
1096
- if (ctx.features.has("arrow-function")) {
1097
- serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1098
- } else if (ctx.features.has("method-shorthand")) {
1099
- serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1100
- } else {
1101
- serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1102
- }
1103
- if (value[0]) {
1104
- if (ctx.features.has("object-assign")) {
1105
- const options = serializeObject(id, value[0]);
1106
- serialized = `Object.assign(${serialized},${options})`;
1107
- } else {
1108
- markRef(ctx, id);
1109
- const assignments = serializeAssignments(id, value[0]);
1110
- if (assignments) {
1111
- const ref = getRefParam(ctx, id);
1112
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1113
- }
1114
- }
1115
- }
1116
- return assignRef(ctx, id, serialized);
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
- case 8 /* NullConstructor */: {
1119
- let serialized = "Object.create(null)";
1120
- if (ctx.features.has("object-assign")) {
1121
- const fields = serializeObject(id, value);
1122
- if (fields !== "{}") {
1123
- serialized = `Object.assign(${serialized},${fields})`;
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
- markRef(ctx, id);
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, source, result) {
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, createRef(ctx, source));
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.has("arrow-function")) {
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 (source.constructor === Object) {
1510
+ if (isObject) {
1163
1511
  return `(${result})`;
1164
1512
  }
1165
1513
  return result;
1166
1514
  }
1167
1515
  function serialize(source, options) {
1168
- const ctx = createSerializationContext(options);
1516
+ const ctx = createParserContext(options);
1169
1517
  if (isPrimitive(source)) {
1170
- return serializePrimitive(ctx, source);
1518
+ return String(serializePrimitive(source));
1171
1519
  }
1172
- const tree = generateTreeSync(ctx, source);
1173
- const result = serializeTree(ctx, tree);
1174
- return finalize(ctx, source, result);
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 = createSerializationContext(options);
1526
+ const ctx = createParserContext(options);
1178
1527
  if (isPrimitive(source)) {
1179
- return serializePrimitive(ctx, source);
1528
+ return String(serializePrimitive(source));
1180
1529
  }
1181
- const tree = await generateTreeAsync(ctx, source);
1182
- const result = serializeTree(ctx, tree);
1183
- return finalize(ctx, source, result);
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