seroval 0.4.0-alpha.8 → 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,113 +313,108 @@ function getIterableOptions(obj) {
393
313
  }
394
314
  return void 0;
395
315
  }
396
- function isReferenceInStack(ctx, node) {
397
- return node[0] === 2 /* Reference */ && ctx.stack.includes(node[1]);
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 [2 /* Reference */, refID];
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;
405
340
  }
406
- const id = ctx.refs.size;
407
- ctx.refs.set(current, id);
408
- return id;
341
+ return Symbol.iterator in value;
409
342
  }
410
- function generateSemiPrimitiveValue(ctx, current, id) {
411
- if (typeof current === "bigint") {
412
- assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
413
- return [1 /* BigInt */, `${current}n`];
414
- }
415
- if (constructorCheck(current, Date)) {
416
- return [3 /* Date */, current.toISOString(), id];
417
- }
418
- if (constructorCheck(current, RegExp)) {
419
- return [4 /* RegExp */, String(current), id];
420
- }
421
- 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)) {
422
- const constructor = current.constructor.name;
423
- assert(ctx.features & 1024 /* TypedArray */, `Unsupported value type "${constructor}"`);
424
- return [14 /* TypedArray */, [constructor, current.toString(), current.byteOffset], id];
343
+
344
+ // src/tree/async.ts
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
+ }
358
+ }
425
359
  }
426
- if (constructorCheck(current, BigInt64Array) || constructorCheck(current, BigUint64Array)) {
427
- const constructor = current.constructor.name;
428
- assert(
429
- ctx.features & 2048 /* BigIntTypedArray */,
430
- `Unsupported value type "${constructor}"`
431
- );
432
- let result = "";
433
- const cap = current.length - 1;
434
- for (let i = 0; i < cap; i++) {
435
- result += `${current[i]}n,`;
360
+ for (let i = 0; i < size; i++) {
361
+ if (i in deferred) {
362
+ nodes[i] = await parse(ctx, deferred[i]);
436
363
  }
437
- result += `"${current[cap]}"`;
438
- return [
439
- 15 /* BigIntTypedArray */,
440
- [constructor, result, current.byteOffset],
441
- id
442
- ];
443
364
  }
444
- return void 0;
365
+ return nodes;
445
366
  }
446
- function serializePrimitive(value) {
447
- if (value === true) {
448
- return "!0";
449
- }
450
- if (value === false) {
451
- return "!1";
452
- }
453
- if (value === void 0) {
454
- return "void 0";
455
- }
456
- if (value === null) {
457
- return null;
458
- }
459
- if (typeof value === "string") {
460
- return quote(value);
461
- }
462
- if (Object.is(value, -0)) {
463
- return "-0";
464
- }
465
- if (Object.is(value, Infinity)) {
466
- return "1/0";
467
- }
468
- if (Object.is(value, -Infinity)) {
469
- return "-1/0";
470
- }
471
- return value;
367
+ async function generateArrayNode(ctx, id, current) {
368
+ return {
369
+ t: 7 /* Array */,
370
+ i: id,
371
+ s: void 0,
372
+ l: void 0,
373
+ c: void 0,
374
+ m: void 0,
375
+ d: void 0,
376
+ a: await generateNodeList(ctx, current),
377
+ n: void 0
378
+ };
472
379
  }
473
-
474
- // src/tree/async.ts
475
- async function serializePropertiesAsync(ctx, properties) {
476
- const keys = Object.keys(properties);
477
- const size = keys.length;
478
- const keyNodes = new Array(size);
479
- const valueNodes = new Array(size);
480
- const deferredKeys = new Array(size);
481
- const deferredValues = new Array(size);
380
+ async function generateMapNode(ctx, id, current) {
381
+ assert(ctx.features & 32 /* Map */, 'Unsupported type "Map"');
382
+ const len = current.size;
383
+ const keyNodes = new Array(len);
384
+ const valueNodes = new Array(len);
385
+ const deferredKey = new Array(len);
386
+ const deferredValue = new Array(len);
482
387
  let deferredSize = 0;
483
- let nodesSize = 0;
484
- for (const key of keys) {
485
- if (isIterable(properties[key])) {
486
- deferredKeys[deferredSize] = key;
487
- deferredValues[deferredSize] = properties[key];
388
+ let nodeSize = 0;
389
+ for (const [key, value] of current.entries()) {
390
+ if (isIterable(key) || isIterable(value)) {
391
+ deferredKey[deferredSize] = key;
392
+ deferredValue[deferredSize] = value;
488
393
  deferredSize++;
489
394
  } else {
490
- keyNodes[nodesSize] = key;
491
- valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
492
- nodesSize++;
395
+ keyNodes[nodeSize] = await parse(ctx, key);
396
+ valueNodes[nodeSize] = await parse(ctx, value);
397
+ nodeSize++;
493
398
  }
494
399
  }
495
400
  for (let i = 0; i < deferredSize; i++) {
496
- keyNodes[nodesSize + i] = deferredKeys[i];
497
- valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
401
+ keyNodes[nodeSize + i] = await parse(ctx, deferredKey[i]);
402
+ valueNodes[nodeSize + i] = await parse(ctx, deferredValue[i]);
498
403
  }
499
- return [keyNodes, valueNodes, size];
404
+ return {
405
+ t: 6 /* Map */,
406
+ i: id,
407
+ s: void 0,
408
+ l: void 0,
409
+ c: void 0,
410
+ m: void 0,
411
+ d: { k: keyNodes, v: valueNodes, s: len },
412
+ a: void 0,
413
+ n: void 0
414
+ };
500
415
  }
501
- async function generateSetNode(ctx, current, id) {
502
- assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
416
+ async function generateSetNode(ctx, id, current) {
417
+ assert(ctx.features & 512 /* Set */, 'Unsupported type "Set"');
503
418
  const len = current.size;
504
419
  const nodes = new Array(len);
505
420
  const deferred = new Array(len);
@@ -509,153 +424,266 @@ async function generateSetNode(ctx, current, id) {
509
424
  if (isIterable(item)) {
510
425
  deferred[deferredSize++] = item;
511
426
  } else {
512
- nodes[nodeSize++] = await generateTreeAsync(ctx, item);
427
+ nodes[nodeSize++] = await parse(ctx, item);
513
428
  }
514
429
  }
515
430
  for (let i = 0; i < deferredSize; i++) {
516
- nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
431
+ nodes[nodeSize + i] = await parse(ctx, deferred[i]);
517
432
  }
518
- return [5 /* Set */, nodes, id];
433
+ return {
434
+ t: 5 /* Set */,
435
+ i: id,
436
+ s: void 0,
437
+ l: void 0,
438
+ c: void 0,
439
+ m: void 0,
440
+ d: void 0,
441
+ a: nodes,
442
+ n: void 0
443
+ };
519
444
  }
520
- async function generateMapNode(ctx, current, id) {
521
- assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
522
- const len = current.size;
523
- const keyNodes = new Array(len);
524
- const valueNodes = new Array(len);
525
- const deferredKey = new Array(len);
526
- const deferredValue = new Array(len);
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);
527
452
  let deferredSize = 0;
528
- let nodeSize = 0;
529
- for (const [key, value] of current.entries()) {
530
- if (isIterable(key) || isIterable(value)) {
531
- deferredKey[deferredSize] = key;
532
- deferredValue[deferredSize] = value;
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;
533
460
  deferredSize++;
534
461
  } else {
535
- keyNodes[nodeSize] = await generateTreeAsync(ctx, key);
536
- valueNodes[nodeSize] = await generateTreeAsync(ctx, value);
537
- nodeSize++;
462
+ keyNodes[nodesSize] = key;
463
+ valueNodes[nodesSize] = await parse(ctx, item);
464
+ nodesSize++;
538
465
  }
539
466
  }
540
467
  for (let i = 0; i < deferredSize; i++) {
541
- keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
542
- valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
543
- }
544
- return [6 /* Map */, [keyNodes, valueNodes, len], id];
545
- }
546
- async function generateNodeList(ctx, current) {
547
- const size = current.length;
548
- const nodes = new Array(size);
549
- const deferred = new Array(size);
550
- for (let i = 0; i < size; i++) {
551
- if (i in current) {
552
- if (isIterable(current[i])) {
553
- deferred[i] = current[i];
554
- } else {
555
- nodes[i] = await generateTreeAsync(ctx, current[i]);
556
- }
557
- }
558
- }
559
- for (let i = 0; i < size; i++) {
560
- if (i in deferred) {
561
- nodes[i] = await generateTreeAsync(ctx, deferred[i]);
562
- }
468
+ keyNodes[nodesSize + i] = deferredKeys[i];
469
+ valueNodes[nodesSize + i] = await parse(ctx, deferredValues[i]);
563
470
  }
564
- return nodes;
471
+ return {
472
+ k: keyNodes,
473
+ v: valueNodes,
474
+ s: size
475
+ };
565
476
  }
566
- async function generateArrayNode(ctx, current, id) {
567
- return [7 /* Array */, await generateNodeList(ctx, current), id];
568
- }
569
- async function generateAggregateErrorNode(ctx, current, id) {
570
- const options = getErrorOptions(current);
571
- const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
572
- const errorsNode = await generateTreeAsync(ctx, current.errors);
573
- return [
574
- 12 /* AggregateError */,
575
- [current.message, optionsNode, errorsNode],
576
- id
577
- ];
578
- }
579
- async function generateErrorNode(ctx, current, id) {
580
- const options = getErrorOptions(current);
581
- const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
582
- return [
583
- 11 /* Error */,
584
- [getErrorConstructor(current), current.message, optionsNode],
585
- id
586
- ];
587
- }
588
- async function generateIterableNode(ctx, current, id) {
589
- assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
477
+ async function generateIterableNode(ctx, id, current) {
478
+ assert(ctx.features & 1024 /* SymbolIterator */, 'Unsupported type "Iterable"');
590
479
  const options = getIterableOptions(current);
591
- return [13 /* Iterable */, [
480
+ return {
481
+ t: 13 /* Iterable */,
482
+ i: id,
483
+ s: void 0,
484
+ l: void 0,
485
+ c: void 0,
486
+ m: void 0,
592
487
  // Parse options first before the items
593
- options ? await serializePropertiesAsync(ctx, options) : void 0,
594
- await generateNodeList(ctx, Array.from(current))
595
- ], id];
488
+ d: options ? await generateProperties(ctx, options) : void 0,
489
+ a: await generateNodeList(ctx, Array.from(current)),
490
+ n: void 0
491
+ };
596
492
  }
597
- async function generateTreeAsync(ctx, current) {
598
- if (isPrimitive(current)) {
599
- return [0 /* Primitive */, serializePrimitive(current)];
600
- }
601
- const id = generateRef(ctx, current);
602
- if (Array.isArray(id)) {
603
- return id;
604
- }
605
- const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
606
- if (semiPrimitive) {
607
- return semiPrimitive;
608
- }
609
- if (isPromise(current)) {
610
- assert(ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
611
- return current.then(async (value) => [
612
- 10 /* Promise */,
613
- await generateTreeAsync(ctx, value),
614
- id
615
- ]);
616
- }
617
- if (constructorCheck(current, Set)) {
618
- return generateSetNode(ctx, current, id);
619
- }
620
- if (constructorCheck(current, Map)) {
621
- return generateMapNode(ctx, current, id);
622
- }
623
- if (Array.isArray(current)) {
624
- return generateArrayNode(ctx, current, id);
625
- }
626
- if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
627
- return generateAggregateErrorNode(ctx, current, id);
628
- }
629
- if (current instanceof Error) {
630
- return generateErrorNode(ctx, current, id);
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);
631
511
  }
632
- if (isIterable(current)) {
633
- return generateIterableNode(ctx, current, id);
512
+ if ("then" in current && typeof current.then === "function") {
513
+ return generatePromiseNode(ctx, id, current);
634
514
  }
635
- const empty = current.constructor == null;
636
- if (current.constructor === Object || empty) {
637
- return [
638
- empty ? 9 /* NullConstructor */ : 8 /* Object */,
639
- await serializePropertiesAsync(ctx, current),
640
- id
641
- ];
515
+ return {
516
+ t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
517
+ i: id,
518
+ s: void 0,
519
+ l: void 0,
520
+ c: void 0,
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,
535
+ c: void 0,
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,
554
+ n: void 0
555
+ };
556
+ }
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");
642
670
  }
643
- throw new Error("Unsupported value");
644
671
  }
645
672
  async function parseAsync(ctx, current) {
646
- const result = await generateTreeAsync(ctx, current);
647
- return [result, createRef(ctx, current), result[0] === 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];
648
676
  }
649
677
 
650
678
  // src/tree/serialize.ts
651
679
  function getAssignmentExpression(assignment) {
652
- switch (assignment.type) {
680
+ switch (assignment.t) {
653
681
  case "index":
654
- return `${assignment.source}=${assignment.value}`;
682
+ return assignment.s + "=" + assignment.v;
655
683
  case "map":
656
- return `${assignment.source}.set(${assignment.key},${assignment.value})`;
684
+ return assignment.s + ".set(" + assignment.k + "," + assignment.v + ")";
657
685
  case "set":
658
- return `${assignment.source}.add(${assignment.value})`;
686
+ return assignment.s + ".add(" + assignment.v + ")";
659
687
  default:
660
688
  return "";
661
689
  }
@@ -664,27 +692,30 @@ function mergeAssignments(assignments) {
664
692
  const newAssignments = [];
665
693
  let current = assignments[0];
666
694
  let prev = current;
695
+ let item;
667
696
  for (let i = 1, len = assignments.length; i < len; i++) {
668
- const item = assignments[i];
669
- if (item.type === prev.type) {
670
- 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) {
671
700
  current = {
672
- type: "index",
673
- source: item.source,
674
- value: getAssignmentExpression(current)
701
+ t: "index",
702
+ s: item.s,
703
+ k: void 0,
704
+ v: getAssignmentExpression(current)
675
705
  };
676
- } else if (item.type === "map" && item.source === prev.source) {
706
+ } else if (item.t === "map" && item.s === prev.s) {
677
707
  current = {
678
- type: "map",
679
- source: getAssignmentExpression(current),
680
- key: item.key,
681
- value: item.value
708
+ t: "map",
709
+ s: getAssignmentExpression(current),
710
+ k: item.k,
711
+ v: item.v
682
712
  };
683
- } else if (item.type === "set" && item.source === prev.source) {
713
+ } else if (item.t === "set" && item.s === prev.s) {
684
714
  current = {
685
- type: "set",
686
- source: getAssignmentExpression(current),
687
- value: item.value
715
+ t: "set",
716
+ s: getAssignmentExpression(current),
717
+ k: void 0,
718
+ v: item.v
688
719
  };
689
720
  } else {
690
721
  newAssignments.push(current);
@@ -704,7 +735,7 @@ function resolveAssignments(assignments) {
704
735
  let result = "";
705
736
  const merged = mergeAssignments(assignments);
706
737
  for (let i = 0, len = merged.length; i < len; i++) {
707
- result += `${getAssignmentExpression(merged[i])},`;
738
+ result += getAssignmentExpression(merged[i]) + ",";
708
739
  }
709
740
  return result;
710
741
  }
@@ -713,350 +744,412 @@ function resolveAssignments(assignments) {
713
744
  function resolvePatches(ctx) {
714
745
  return resolveAssignments(ctx.assignments);
715
746
  }
716
- function assignRef(ctx, index, value) {
717
- if (ctx.markedRefs[index]) {
718
- return `${getRefParam(ctx, index)}=${value}`;
719
- }
720
- return value;
721
- }
722
747
  function createAssignment(ctx, source, value) {
723
748
  ctx.assignments.push({
724
- type: "index",
725
- source,
726
- value
749
+ t: "index",
750
+ s: source,
751
+ k: void 0,
752
+ v: value
727
753
  });
728
754
  }
729
755
  function createSetAdd(ctx, ref, value) {
730
756
  markRef(ctx, ref);
731
757
  ctx.assignments.push({
732
- type: "set",
733
- source: getRefParam(ctx, ref),
734
- value
758
+ t: "set",
759
+ s: getRefParam(ctx, ref),
760
+ k: void 0,
761
+ v: value
735
762
  });
736
763
  }
737
764
  function createMapSet(ctx, ref, key, value) {
738
765
  markRef(ctx, ref);
739
766
  ctx.assignments.push({
740
- type: "map",
741
- source: getRefParam(ctx, ref),
742
- key,
743
- value
767
+ t: "map",
768
+ s: getRefParam(ctx, ref),
769
+ k: key,
770
+ v: value
744
771
  });
745
772
  }
746
773
  function createArrayAssign(ctx, ref, index, value) {
747
774
  markRef(ctx, ref);
748
- createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
775
+ createAssignment(ctx, getRefParam(ctx, ref) + "[" + index.toString() + "]", value);
749
776
  }
750
777
  function createObjectAssign(ctx, ref, key, value) {
751
778
  markRef(ctx, ref);
752
- 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);
753
789
  }
754
790
  var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
755
- function serializeAssignments(ctx, targetRef, [keys, values, size]) {
756
- ctx.stack.push(targetRef);
757
- const mainAssignments = [];
791
+ function serializeNodeList(ctx, node) {
792
+ const size = node.a.length;
793
+ let values = "";
794
+ let item;
795
+ let isHoley = false;
758
796
  for (let i = 0; i < size; i++) {
759
- const parentStack = ctx.stack;
760
- ctx.stack = [];
761
- const refParam = serializeTree(ctx, values[i]);
762
- ctx.stack = parentStack;
763
- const key = keys[i];
764
- const check = Number(key);
765
- const parentAssignment = ctx.assignments;
766
- ctx.assignments = mainAssignments;
767
- const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
768
- if (isIdentifier && Number.isNaN(check)) {
769
- createObjectAssign(ctx, targetRef, key, refParam);
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
+ }
770
809
  } else {
771
- createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
810
+ isHoley = true;
772
811
  }
773
- ctx.assignments = parentAssignment;
774
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);
775
821
  ctx.stack.pop();
776
- return resolveAssignments(mainAssignments);
822
+ return assignRef(ctx, node.i, result);
777
823
  }
778
- function serializeObject(ctx, sourceID, [keys, values, size]) {
779
- if (size === 0) {
824
+ function serializeObject(ctx, sourceID, node) {
825
+ if (node.s === 0) {
780
826
  return "{}";
781
827
  }
782
828
  let result = "";
783
829
  ctx.stack.push(sourceID);
784
- for (let i = 0; i < size; i++) {
785
- const key = keys[i];
786
- const val = values[i];
787
- const check = Number(key);
788
- const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
830
+ let key;
831
+ let val;
832
+ let check;
833
+ let isIdentifier;
834
+ let refParam;
835
+ let hasPrev = false;
836
+ for (let i = 0; i < node.s; i++) {
837
+ key = node.k[i];
838
+ val = node.v[i];
839
+ check = Number(key);
840
+ isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
789
841
  if (isReferenceInStack(ctx, val)) {
790
- const refParam = getRefParam(ctx, val[1]);
842
+ refParam = getRefParam(ctx, val.i);
791
843
  if (isIdentifier && Number.isNaN(check)) {
792
844
  createObjectAssign(ctx, sourceID, key, refParam);
793
845
  } else {
794
846
  createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
795
847
  }
796
848
  } else {
797
- 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;
798
855
  }
799
856
  }
800
857
  ctx.stack.pop();
801
- return `{${result.substring(0, result.length - 1)}}`;
858
+ return "{" + result + "}";
802
859
  }
803
- function serializePromise(ctx, value, id) {
804
- let serialized;
805
- if (isReferenceInStack(ctx, value)) {
806
- serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
807
- } else {
808
- ctx.stack.push(id);
809
- const result = serializeTree(ctx, value);
810
- ctx.stack.pop();
811
- 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 + ")";
812
864
  }
813
- return assignRef(ctx, id, serialized);
865
+ return serialized;
814
866
  }
815
- function serializeTypedArray(ctx, constructor, init, byteOffset, id) {
816
- let args = `[${init}]`;
817
- if (byteOffset !== 0) {
818
- args += `,${byteOffset}`;
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;
819
892
  }
820
- return assignRef(ctx, id, `new ${constructor}(${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)");
821
913
  }
822
- function serializeSet(ctx, value, id) {
914
+ function serializeSet(ctx, node) {
823
915
  let serialized = "new Set";
824
- const size = value.length;
916
+ const size = node.a.length;
825
917
  if (size) {
826
918
  let result = "";
827
- ctx.stack.push(id);
919
+ ctx.stack.push(node.i);
920
+ let item;
921
+ let hasPrev = false;
828
922
  for (let i = 0; i < size; i++) {
829
- const item = value[i];
923
+ item = node.a[i];
830
924
  if (isReferenceInStack(ctx, item)) {
831
- createSetAdd(ctx, id, getRefParam(ctx, item[1]));
925
+ createSetAdd(ctx, node.i, getRefParam(ctx, item.i));
832
926
  } else {
833
- result += `${serializeTree(ctx, item)},`;
927
+ if (hasPrev) {
928
+ result += ",";
929
+ }
930
+ result += serializeTree(ctx, item);
931
+ hasPrev = true;
834
932
  }
835
933
  }
836
934
  ctx.stack.pop();
837
935
  if (result) {
838
- serialized += `([${result.substring(0, result.length - 1)}])`;
936
+ serialized += "([" + result + "])";
839
937
  }
840
938
  }
841
- return assignRef(ctx, id, serialized);
939
+ return assignRef(ctx, node.i, serialized);
842
940
  }
843
- function serializeMap(ctx, keys, values, size, id) {
941
+ function serializeMap(ctx, node) {
844
942
  let serialized = "new Map";
845
- if (size) {
943
+ if (node.d.s) {
846
944
  let result = "";
847
- ctx.stack.push(id);
848
- for (let i = 0; i < size; i++) {
849
- const key = keys[i];
850
- const val = values[i];
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;
952
+ for (let i = 0; i < node.d.s; i++) {
953
+ key = node.d.k[i];
954
+ val = node.d.v[i];
851
955
  if (isReferenceInStack(ctx, key)) {
852
- const keyRef = getRefParam(ctx, key[1]);
956
+ keyRef = getRefParam(ctx, key.i);
853
957
  if (isReferenceInStack(ctx, val)) {
854
- const valueRef = getRefParam(ctx, val[1]);
855
- createMapSet(ctx, id, keyRef, valueRef);
958
+ valueRef = getRefParam(ctx, val.i);
959
+ createMapSet(ctx, node.i, keyRef, valueRef);
856
960
  } else {
857
- const parent = ctx.stack;
961
+ parent = ctx.stack;
858
962
  ctx.stack = [];
859
- createMapSet(ctx, id, keyRef, serializeTree(ctx, val));
963
+ createMapSet(ctx, node.i, keyRef, serializeTree(ctx, val));
860
964
  ctx.stack = parent;
861
965
  }
862
966
  } else if (isReferenceInStack(ctx, val)) {
863
- const valueRef = getRefParam(ctx, val[1]);
864
- const parent = ctx.stack;
967
+ valueRef = getRefParam(ctx, val.i);
968
+ parent = ctx.stack;
865
969
  ctx.stack = [];
866
- createMapSet(ctx, id, serializeTree(ctx, key), valueRef);
970
+ createMapSet(ctx, node.i, serializeTree(ctx, key), valueRef);
867
971
  ctx.stack = parent;
868
972
  } else {
869
- result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
973
+ if (hasPrev) {
974
+ result += ",";
975
+ }
976
+ result += "[" + serializeTree(ctx, key) + "," + serializeTree(ctx, val) + "]";
977
+ hasPrev = true;
870
978
  }
871
979
  }
872
980
  ctx.stack.pop();
873
981
  if (result) {
874
- serialized += `([${result.substring(0, result.length - 1)}])`;
982
+ serialized += "([" + result + "])";
875
983
  }
876
984
  }
877
- return assignRef(ctx, id, serialized);
985
+ return assignRef(ctx, node.i, serialized);
878
986
  }
879
- function serializeNodeList(ctx, value, id) {
880
- const size = value.length;
881
- let values = "";
882
- for (let i = 0; i < size; i++) {
883
- const item = value[i];
884
- if (item) {
885
- if (isReferenceInStack(ctx, item)) {
886
- createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
887
- values += ",";
888
- } else {
889
- values += serializeTree(ctx, item);
890
- if (i < size - 1) {
891
- values += ",";
892
- }
893
- }
894
- } else {
895
- values += ",";
896
- }
897
- }
898
- return `[${values}]`;
899
- }
900
- function serializeArray(ctx, value, id) {
901
- ctx.stack.push(id);
902
- const result = serializeNodeList(ctx, value, id);
987
+ function serializeAggregateError(ctx, node) {
988
+ ctx.stack.push(node.i);
989
+ const serialized = "new AggregateError(" + serializeTree(ctx, node.n) + "," + quote(node.m) + ")";
903
990
  ctx.stack.pop();
904
- return assignRef(ctx, id, result);
991
+ return serializeDictionary(ctx, node.i, node.d, serialized);
905
992
  }
906
- function serializeWithObjectAssign(ctx, value, id, serialized) {
907
- const fields = serializeObject(ctx, id, value);
908
- if (fields !== "{}") {
909
- return `Object.assign(${serialized},${fields})`;
910
- }
911
- 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);
912
996
  }
913
- function serializeAggregateError(ctx, message, options, errors, id) {
914
- ctx.stack.push(id);
915
- let serialized = `new AggregateError(${serializeTree(ctx, errors)},${quote(message)})`;
916
- ctx.stack.pop();
917
- if (options) {
918
- if (ctx.features & 64 /* ObjectAssign */) {
919
- serialized = serializeWithObjectAssign(ctx, options, id, 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 + ")";
920
1003
  } else {
921
- markRef(ctx, id);
922
- const assignments = serializeAssignments(ctx, id, options);
923
- if (assignments) {
924
- const ref = getRefParam(ctx, id);
925
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
926
- }
1004
+ serialized = "Promise.resolve().then(function(){return " + ref + "})";
927
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 + ")";
928
1011
  }
929
- return assignRef(ctx, id, serialized);
1012
+ return assignRef(ctx, node.i, serialized);
930
1013
  }
931
- function serializeError(ctx, constructor, message, options, id) {
932
- let serialized = `new ${constructor}(${quote(message)})`;
933
- if (options) {
934
- if (ctx.features & 64 /* ObjectAssign */) {
935
- serialized = serializeWithObjectAssign(ctx, options, id, serialized);
936
- } else {
937
- markRef(ctx, id);
938
- const assignments = serializeAssignments(ctx, id, options);
939
- if (assignments) {
940
- const ref = getRefParam(ctx, id);
941
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
942
- }
943
- }
1014
+ function serializeTypedArray(ctx, node) {
1015
+ let args = "[" + node.s + "]";
1016
+ if (node.l !== 0) {
1017
+ args += "," + node.l.toString();
944
1018
  }
945
- return assignRef(ctx, id, serialized);
1019
+ return assignRef(ctx, node.i, "new " + node.c + "(" + args + ")");
946
1020
  }
947
- function serializeIterable(ctx, options, items, id) {
1021
+ function serializeIterable(ctx, node) {
948
1022
  const parent = ctx.stack;
949
1023
  ctx.stack = [];
950
- const values = serializeNodeList(ctx, items, id);
1024
+ const values = serializeNodeList(ctx, node);
951
1025
  ctx.stack = parent;
952
- let serialized;
1026
+ let serialized = values;
953
1027
  if (ctx.features & 2 /* ArrayPrototypeValues */) {
954
- serialized = `${values}.values()`;
1028
+ serialized += ".values()";
955
1029
  } else {
956
- serialized = `${values}[Symbol.iterator]()`;
1030
+ serialized += "[Symbol.iterator]()";
957
1031
  }
958
1032
  if (ctx.features & 4 /* ArrowFunction */) {
959
- serialized = `{[Symbol.iterator]:()=>${serialized}}`;
960
- } else if (ctx.features & 32 /* MethodShorthand */) {
961
- serialized = `{[Symbol.iterator](){return ${serialized}}}`;
962
- } else {
963
- serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
964
- }
965
- if (options) {
966
- if (ctx.features & 64 /* ObjectAssign */) {
967
- serialized = serializeWithObjectAssign(ctx, options, id, serialized);
968
- } else {
969
- markRef(ctx, id);
970
- const assignments = serializeAssignments(ctx, id, options);
971
- if (assignments) {
972
- const ref = getRefParam(ctx, id);
973
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
974
- }
975
- }
976
- }
977
- return assignRef(ctx, id, serialized);
978
- }
979
- function serializeNullConstructor(ctx, value, id) {
980
- let serialized = "Object.create(null)";
981
- if (ctx.features & 64 /* ObjectAssign */) {
982
- serialized = serializeWithObjectAssign(ctx, value, id, serialized);
1033
+ serialized = "{[Symbol.iterator]:()=>" + serialized + "}";
1034
+ } else if (ctx.features & 64 /* MethodShorthand */) {
1035
+ serialized = "{[Symbol.iterator](){return " + serialized + "}}";
983
1036
  } else {
984
- markRef(ctx, id);
985
- const assignments = serializeAssignments(ctx, id, value);
986
- if (assignments) {
987
- const ref = getRefParam(ctx, id);
988
- return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
989
- }
1037
+ serialized = "{[Symbol.iterator]:function(){return " + serialized + "}}";
990
1038
  }
991
- return assignRef(ctx, id, serialized);
1039
+ return serializeDictionary(ctx, node.i, node.d, serialized);
992
1040
  }
993
- function serializeTree(ctx, [type, value, id]) {
994
- switch (type) {
1041
+ function serializeTree(ctx, node) {
1042
+ switch (node.t) {
995
1043
  case 0 /* Primitive */:
996
- return String(value);
997
- case 2 /* Reference */:
998
- return getRefParam(ctx, value);
999
- case 10 /* Promise */:
1000
- return serializePromise(ctx, value, id);
1044
+ return String(node.s);
1001
1045
  case 1 /* BigInt */:
1002
- return value;
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);
1003
1055
  case 3 /* Date */:
1004
- return assignRef(ctx, id, `new Date("${value}")`);
1056
+ return assignRef(ctx, node.i, 'new Date("' + node.s + '")');
1005
1057
  case 4 /* RegExp */:
1006
- return assignRef(ctx, id, value);
1007
- case 15 /* BigIntTypedArray */:
1008
- case 14 /* TypedArray */:
1009
- return serializeTypedArray(ctx, value[0], value[1], value[2], id);
1058
+ return assignRef(ctx, node.i, node.s);
1010
1059
  case 5 /* Set */:
1011
- return serializeSet(ctx, value, id);
1060
+ return serializeSet(ctx, node);
1012
1061
  case 6 /* Map */:
1013
- return serializeMap(ctx, value[0], value[1], value[2], id);
1014
- case 7 /* Array */:
1015
- return serializeArray(ctx, value, id);
1062
+ return serializeMap(ctx, node);
1063
+ case 15 /* BigIntTypedArray */:
1064
+ case 14 /* TypedArray */:
1065
+ return serializeTypedArray(ctx, node);
1016
1066
  case 12 /* AggregateError */:
1017
- return serializeAggregateError(ctx, value[0], value[1], value[2], id);
1067
+ return serializeAggregateError(ctx, node);
1018
1068
  case 11 /* Error */:
1019
- return serializeError(ctx, value[0], value[1], value[2], id);
1069
+ return serializeError(ctx, node);
1020
1070
  case 13 /* Iterable */:
1021
- return serializeIterable(ctx, value[0], value[1], id);
1022
- case 9 /* NullConstructor */:
1023
- return serializeNullConstructor(ctx, value, id);
1024
- case 8 /* Object */:
1025
- return assignRef(ctx, id, serializeObject(ctx, id, value));
1071
+ return serializeIterable(ctx, node);
1072
+ case 10 /* Promise */:
1073
+ return serializePromise(ctx, node);
1026
1074
  default:
1027
1075
  throw new Error("Unsupported type");
1028
1076
  }
1029
1077
  }
1030
1078
 
1031
1079
  // src/tree/sync.ts
1032
- function serializePropertiesSync(ctx, properties) {
1033
- const keys = Object.keys(properties);
1034
- const size = keys.length;
1035
- const keyNodes = new Array(size);
1036
- const valueNodes = new Array(size);
1037
- const deferredKeys = new Array(size);
1038
- const deferredValues = new Array(size);
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
+ }
1093
+ }
1094
+ }
1095
+ for (let i = 0; i < size; i++) {
1096
+ if (i in deferred) {
1097
+ nodes[i] = parse2(ctx, deferred[i]);
1098
+ }
1099
+ }
1100
+ return nodes;
1101
+ }
1102
+ function generateArrayNode2(ctx, id, current) {
1103
+ return {
1104
+ t: 7 /* Array */,
1105
+ i: id,
1106
+ s: void 0,
1107
+ l: void 0,
1108
+ c: void 0,
1109
+ m: void 0,
1110
+ d: void 0,
1111
+ a: generateNodeList2(ctx, current),
1112
+ n: void 0
1113
+ };
1114
+ }
1115
+ function generateMapNode2(ctx, id, current) {
1116
+ assert(ctx.features & 32 /* Map */, 'Unsupported type "Map"');
1117
+ const len = current.size;
1118
+ const keyNodes = new Array(len);
1119
+ const valueNodes = new Array(len);
1120
+ const deferredKey = new Array(len);
1121
+ const deferredValue = new Array(len);
1039
1122
  let deferredSize = 0;
1040
- let nodesSize = 0;
1041
- for (const key of keys) {
1042
- if (isIterable(properties[key])) {
1043
- deferredKeys[deferredSize] = key;
1044
- deferredValues[deferredSize] = properties[key];
1123
+ let nodeSize = 0;
1124
+ for (const [key, value] of current.entries()) {
1125
+ if (isIterable(key) || isIterable(value)) {
1126
+ deferredKey[deferredSize] = key;
1127
+ deferredValue[deferredSize] = value;
1045
1128
  deferredSize++;
1046
1129
  } else {
1047
- keyNodes[nodesSize] = key;
1048
- valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
1049
- nodesSize++;
1130
+ keyNodes[nodeSize] = parse2(ctx, key);
1131
+ valueNodes[nodeSize] = parse2(ctx, value);
1132
+ nodeSize++;
1050
1133
  }
1051
1134
  }
1052
1135
  for (let i = 0; i < deferredSize; i++) {
1053
- keyNodes[nodesSize + i] = deferredKeys[i];
1054
- valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
1136
+ keyNodes[nodeSize + i] = parse2(ctx, deferredKey[i]);
1137
+ valueNodes[nodeSize + i] = parse2(ctx, deferredValue[i]);
1055
1138
  }
1056
- return [keyNodes, valueNodes, size];
1139
+ return {
1140
+ t: 6 /* Map */,
1141
+ i: id,
1142
+ s: void 0,
1143
+ l: void 0,
1144
+ c: void 0,
1145
+ m: void 0,
1146
+ d: { k: keyNodes, v: valueNodes, s: len },
1147
+ a: void 0,
1148
+ n: void 0
1149
+ };
1057
1150
  }
1058
- function generateSetNode2(ctx, current, id) {
1059
- assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
1151
+ function generateSetNode2(ctx, id, current) {
1152
+ assert(ctx.features & 512 /* Set */, 'Unsupported type "Set"');
1060
1153
  const len = current.size;
1061
1154
  const nodes = new Array(len);
1062
1155
  const deferred = new Array(len);
@@ -1066,130 +1159,211 @@ function generateSetNode2(ctx, current, id) {
1066
1159
  if (isIterable(item)) {
1067
1160
  deferred[deferredSize++] = item;
1068
1161
  } else {
1069
- nodes[nodeSize++] = generateTreeSync(ctx, item);
1162
+ nodes[nodeSize++] = parse2(ctx, item);
1070
1163
  }
1071
1164
  }
1072
1165
  for (let i = 0; i < deferredSize; i++) {
1073
- nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
1166
+ nodes[nodeSize + i] = parse2(ctx, deferred[i]);
1074
1167
  }
1075
- return [5 /* Set */, nodes, id];
1168
+ return {
1169
+ t: 5 /* Set */,
1170
+ i: id,
1171
+ s: void 0,
1172
+ l: void 0,
1173
+ c: void 0,
1174
+ m: void 0,
1175
+ d: void 0,
1176
+ a: nodes,
1177
+ n: void 0
1178
+ };
1076
1179
  }
1077
- function generateMapNode2(ctx, current, id) {
1078
- assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
1079
- const len = current.size;
1080
- const keyNodes = new Array(len);
1081
- const valueNodes = new Array(len);
1082
- const deferredKey = new Array(len);
1083
- const deferredValue = new Array(len);
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);
1084
1187
  let deferredSize = 0;
1085
- let nodeSize = 0;
1086
- for (const [key, value] of current.entries()) {
1087
- if (isIterable(key) || isIterable(value)) {
1088
- deferredKey[deferredSize] = key;
1089
- deferredValue[deferredSize] = value;
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;
1090
1195
  deferredSize++;
1091
1196
  } else {
1092
- keyNodes[nodeSize] = generateTreeSync(ctx, key);
1093
- valueNodes[nodeSize] = generateTreeSync(ctx, value);
1094
- nodeSize++;
1197
+ keyNodes[nodesSize] = key;
1198
+ valueNodes[nodesSize] = parse2(ctx, item);
1199
+ nodesSize++;
1095
1200
  }
1096
1201
  }
1097
1202
  for (let i = 0; i < deferredSize; i++) {
1098
- keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
1099
- valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
1100
- }
1101
- return [6 /* Map */, [keyNodes, valueNodes, len], id];
1102
- }
1103
- function generateNodeList2(ctx, current) {
1104
- const size = current.length;
1105
- const nodes = new Array(size);
1106
- const deferred = new Array(size);
1107
- for (let i = 0; i < size; i++) {
1108
- if (i in current) {
1109
- if (isIterable(current[i])) {
1110
- deferred[i] = current[i];
1111
- } else {
1112
- nodes[i] = generateTreeSync(ctx, current[i]);
1113
- }
1114
- }
1115
- }
1116
- for (let i = 0; i < size; i++) {
1117
- if (i in deferred) {
1118
- nodes[i] = generateTreeSync(ctx, deferred[i]);
1119
- }
1203
+ keyNodes[nodesSize + i] = deferredKeys[i];
1204
+ valueNodes[nodesSize + i] = parse2(ctx, deferredValues[i]);
1120
1205
  }
1121
- return nodes;
1206
+ return {
1207
+ k: keyNodes,
1208
+ v: valueNodes,
1209
+ s: size
1210
+ };
1122
1211
  }
1123
- function generateArrayNode2(ctx, current, id) {
1124
- return [7 /* Array */, generateNodeList2(ctx, current), id];
1125
- }
1126
- function generateAggregateErrorNode2(ctx, current, id) {
1127
- const options = getErrorOptions(current);
1128
- const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
1129
- const errorsNode = generateTreeSync(ctx, current.errors);
1130
- return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
1131
- }
1132
- function generateErrorNode2(ctx, current, id) {
1133
- const options = getErrorOptions(current);
1134
- const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
1135
- return [
1136
- 11 /* Error */,
1137
- [getErrorConstructor(current), current.message, optionsNode],
1138
- id
1139
- ];
1140
- }
1141
- function generateIterableNode2(ctx, current, id) {
1142
- assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
1212
+ function generateIterableNode2(ctx, id, current) {
1213
+ assert(ctx.features & 1024 /* SymbolIterator */, 'Unsupported type "Iterable"');
1143
1214
  const options = getIterableOptions(current);
1144
- return [13 /* Iterable */, [
1215
+ return {
1216
+ t: 13 /* Iterable */,
1217
+ i: id,
1218
+ s: void 0,
1219
+ l: void 0,
1220
+ c: void 0,
1221
+ m: void 0,
1145
1222
  // Parse options first before the items
1146
- options ? serializePropertiesSync(ctx, options) : void 0,
1147
- generateNodeList2(ctx, Array.from(current))
1148
- ], id];
1223
+ d: options ? generateProperties2(ctx, options) : void 0,
1224
+ a: generateNodeList2(ctx, Array.from(current)),
1225
+ n: void 0
1226
+ };
1149
1227
  }
1150
- function generateTreeSync(ctx, current) {
1151
- if (isPrimitive(current)) {
1152
- return [0 /* Primitive */, serializePrimitive(current)];
1153
- }
1154
- const id = generateRef(ctx, current);
1155
- if (Array.isArray(id)) {
1156
- return id;
1157
- }
1158
- const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
1159
- if (semiPrimitive) {
1160
- return semiPrimitive;
1228
+ function generateObjectNode2(ctx, id, current, empty) {
1229
+ if (Symbol.iterator in current) {
1230
+ return generateIterableNode2(ctx, id, current);
1161
1231
  }
1162
- if (constructorCheck(current, Set)) {
1163
- return generateSetNode2(ctx, current, id);
1164
- }
1165
- if (constructorCheck(current, Map)) {
1166
- return generateMapNode2(ctx, current, id);
1167
- }
1168
- if (Array.isArray(current)) {
1169
- return generateArrayNode2(ctx, current, id);
1170
- }
1171
- if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
1172
- return generateAggregateErrorNode2(ctx, current, id);
1173
- }
1174
- if (current instanceof Error) {
1175
- return generateErrorNode2(ctx, current, id);
1176
- }
1177
- if (isIterable(current)) {
1178
- return generateIterableNode2(ctx, current, id);
1179
- }
1180
- const empty = current.constructor == null;
1181
- if (current.constructor === Object || empty) {
1182
- return [
1183
- empty ? 9 /* NullConstructor */ : 8 /* Object */,
1184
- serializePropertiesSync(ctx, current),
1185
- id
1186
- ];
1232
+ return {
1233
+ t: empty ? 9 /* NullConstructor */ : 8 /* Object */,
1234
+ i: id,
1235
+ s: void 0,
1236
+ l: void 0,
1237
+ c: void 0,
1238
+ m: void 0,
1239
+ d: generateProperties2(ctx, current),
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,
1250
+ s: void 0,
1251
+ l: void 0,
1252
+ c: void 0,
1253
+ m: current.message,
1254
+ d: optionsNode,
1255
+ a: void 0,
1256
+ n: parse2(ctx, current.errors)
1257
+ };
1258
+ }
1259
+ function generateErrorNode2(ctx, id, current) {
1260
+ const options = getErrorOptions(ctx, current);
1261
+ const optionsNode = options ? generateProperties2(ctx, options) : void 0;
1262
+ return {
1263
+ t: 11 /* Error */,
1264
+ i: id,
1265
+ s: void 0,
1266
+ l: void 0,
1267
+ c: getErrorConstructor(current),
1268
+ m: current.message,
1269
+ d: optionsNode,
1270
+ a: void 0,
1271
+ n: void 0
1272
+ };
1273
+ }
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");
1187
1361
  }
1188
- throw new Error("Unsupported value");
1189
1362
  }
1190
1363
  function parseSync(ctx, current) {
1191
- const result = generateTreeSync(ctx, current);
1192
- return [result, createRef(ctx, current), result[0] === 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];
1193
1367
  }
1194
1368
 
1195
1369
  // src/index.ts
@@ -1199,29 +1373,25 @@ function finalize(ctx, rootID, isObject, result) {
1199
1373
  let body = result;
1200
1374
  if (patches) {
1201
1375
  const index = getRefParam(ctx, rootID);
1202
- if (result.startsWith(`${index}=`)) {
1203
- body = `${result},${patches}${index}`;
1204
- } else {
1205
- body = `${index}=${result},${patches}${index}`;
1376
+ body = result + "," + patches + index;
1377
+ if (!result.startsWith(index + "=")) {
1378
+ body = index + "=" + body;
1206
1379
  }
1207
1380
  }
1208
1381
  let params = ctx.vars.length > 1 ? ctx.vars.join(",") : ctx.vars[0];
1209
1382
  if (ctx.features & 4 /* ArrowFunction */) {
1210
- params = ctx.vars.length > 1 || ctx.vars.length === 0 ? `(${params})` : params;
1211
- return `(${params}=>(${body}))()`;
1383
+ params = ctx.vars.length > 1 || ctx.vars.length === 0 ? "(" + params + ")" : params;
1384
+ return "(" + params + "=>(" + body + "))()";
1212
1385
  }
1213
- return `(function(${params}){return ${body}})()`;
1386
+ return "(function(" + params + "){return " + body + "})()";
1214
1387
  }
1215
1388
  if (isObject) {
1216
- return `(${result})`;
1389
+ return "(" + result + ")";
1217
1390
  }
1218
1391
  return result;
1219
1392
  }
1220
1393
  function serialize(source, options) {
1221
1394
  const ctx = createParserContext(options);
1222
- if (isPrimitive(source)) {
1223
- return String(serializePrimitive(source));
1224
- }
1225
1395
  const [tree, rootID, isObject] = parseSync(ctx, source);
1226
1396
  const serial = createSerializationContext(ctx);
1227
1397
  const result = serializeTree(serial, tree);
@@ -1229,9 +1399,6 @@ function serialize(source, options) {
1229
1399
  }
1230
1400
  async function serializeAsync(source, options) {
1231
1401
  const ctx = createParserContext(options);
1232
- if (isPrimitive(source)) {
1233
- return String(serializePrimitive(source));
1234
- }
1235
1402
  const [tree, rootID, isObject] = await parseAsync(ctx, source);
1236
1403
  const serial = createSerializationContext(ctx);
1237
1404
  const result = serializeTree(serial, tree);
@@ -1243,39 +1410,40 @@ function deserialize(source) {
1243
1410
  function toJSON(source, options) {
1244
1411
  const ctx = createParserContext(options);
1245
1412
  const [tree, root, isObject] = parseSync(ctx, source);
1246
- return JSON.stringify([
1247
- tree,
1248
- root,
1249
- isObject,
1250
- ctx.features,
1251
- ctx.markedRefs
1252
- ]);
1413
+ return JSON.stringify({
1414
+ t: tree,
1415
+ r: root,
1416
+ i: isObject,
1417
+ f: ctx.features,
1418
+ m: Array.from(ctx.markedRefs)
1419
+ });
1253
1420
  }
1254
1421
  async function toJSONAsync(source, options) {
1255
1422
  const ctx = createParserContext(options);
1256
1423
  const [tree, root, isObject] = await parseAsync(ctx, source);
1257
- return JSON.stringify([
1258
- tree,
1259
- root,
1260
- isObject,
1261
- ctx.features,
1262
- ctx.markedRefs
1263
- ]);
1424
+ return JSON.stringify({
1425
+ t: tree,
1426
+ r: root,
1427
+ i: isObject,
1428
+ f: ctx.features,
1429
+ m: Array.from(ctx.markedRefs)
1430
+ });
1264
1431
  }
1265
1432
  function compileJSON(source) {
1266
1433
  const parsed = JSON.parse(source);
1267
1434
  const serial = createSerializationContext({
1268
- features: parsed[3],
1269
- markedRefs: parsed[4]
1435
+ features: parsed.f,
1436
+ markedRefs: parsed.m
1270
1437
  });
1271
- const result = serializeTree(serial, parsed[0]);
1272
- return finalize(serial, parsed[1], parsed[2], result);
1438
+ const result = serializeTree(serial, parsed.t);
1439
+ return finalize(serial, parsed.r, parsed.i, result);
1273
1440
  }
1274
1441
  function fromJSON(source) {
1275
1442
  return deserialize(compileJSON(source));
1276
1443
  }
1277
1444
  var src_default = serialize;
1278
1445
  export {
1446
+ Feature,
1279
1447
  compileJSON,
1280
1448
  src_default as default,
1281
1449
  deserialize,