seroval 0.4.0-alpha.9 → 0.4.0

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