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