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