seroval 0.3.0 → 0.4.0-alpha.1

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.
@@ -19,6 +19,204 @@ function isIterable(obj) {
19
19
  return !!obj && typeof obj === "object" && !isIterableBuiltin(obj) && Symbol.iterator in obj && typeof obj[Symbol.iterator] === "function";
20
20
  }
21
21
 
22
+ // src/assert.ts
23
+ function assert(cond, error) {
24
+ if (!cond) {
25
+ throw new Error(error);
26
+ }
27
+ }
28
+
29
+ // 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
+ [0 /* 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
+ [1 /* 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
+ [2 /* 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
+ [3 /* 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
+ [4 /* 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
+ [5 /* 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
+ [6 /* 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
+ [7 /* 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
+ [8 /* 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
+ [9 /* 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
+ [10 /* 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
+ };
185
+ var TARGET_REGEX = /^(es|chrome|edge|safari|firefox|opera|ios|samsung|deno|node)([0-9]+(\.[0-9]+(\.[0-9]+)?)?)$/i;
186
+ function parseTarget(target) {
187
+ const result = TARGET_REGEX.exec(target);
188
+ assert(result, `Invalid target "${target}"`);
189
+ const [, platform, version] = result;
190
+ const [major, minor = "0", patch = "0"] = version.split(".");
191
+ return [platform, [Number(major), Number(minor), Number(patch)]];
192
+ }
193
+ function getTargetVersions(targets) {
194
+ if (Array.isArray(targets)) {
195
+ const versions = [];
196
+ for (const target of targets) {
197
+ versions.push(parseTarget(target));
198
+ }
199
+ return versions;
200
+ }
201
+ return [parseTarget(targets)];
202
+ }
203
+ function parseTargets(targets) {
204
+ const parsed = getTargetVersions(targets);
205
+ const flags = /* @__PURE__ */ new Set();
206
+ for (const key in VERSION_TABLE) {
207
+ const base = VERSION_TABLE[key];
208
+ let flag = true;
209
+ for (const [platform, version] of parsed) {
210
+ const baseVersion = base[platform];
211
+ flag = flag && !!baseVersion && compareVersion(baseVersion, version) <= 0;
212
+ }
213
+ if (flag) {
214
+ flags.add(Number(key));
215
+ }
216
+ }
217
+ return flags;
218
+ }
219
+
22
220
  // src/quote.ts
23
221
  function quote(str) {
24
222
  let result = "";
@@ -61,38 +259,6 @@ function quote(str) {
61
259
  return `"${result}"`;
62
260
  }
63
261
 
64
- // src/serialize-primitive.ts
65
- function serializePrimitive(value) {
66
- if (value === true) {
67
- return "!0";
68
- }
69
- if (value === false) {
70
- return "!1";
71
- }
72
- if (value === void 0) {
73
- return "void 0";
74
- }
75
- if (value === null) {
76
- return "null";
77
- }
78
- if (typeof value === "bigint") {
79
- return `${value}n`;
80
- }
81
- if (typeof value === "string") {
82
- return quote(value);
83
- }
84
- if (Object.is(value, -0)) {
85
- return "-0";
86
- }
87
- if (Object.is(value, Infinity)) {
88
- return "1/0";
89
- }
90
- if (Object.is(value, -Infinity)) {
91
- return "-1/0";
92
- }
93
- return String(value);
94
- }
95
-
96
262
  // src/get-identifier.ts
97
263
  var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
98
264
  var REF_START_CHARS_LEN = REF_START_CHARS.length;
@@ -111,14 +277,19 @@ function getIdentifier(index) {
111
277
  }
112
278
 
113
279
  // src/tree.ts
114
- function createSerializationContext() {
280
+ var DEFAULT_OPTIONS = {
281
+ target: "es2023"
282
+ };
283
+ function createSerializationContext(options = {}) {
284
+ const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
115
285
  return {
116
286
  markedRefs: [],
117
287
  stack: [],
118
288
  refs: /* @__PURE__ */ new Map(),
119
289
  vars: [],
120
290
  assignments: [],
121
- validRefs: /* @__PURE__ */ new Map()
291
+ validRefs: /* @__PURE__ */ new Map(),
292
+ features: parseTargets(result.target)
122
293
  };
123
294
  }
124
295
  function getAssignmentExpression(assignment) {
@@ -133,12 +304,12 @@ function getAssignmentExpression(assignment) {
133
304
  return "";
134
305
  }
135
306
  }
136
- function mergeAssignments(ctx) {
307
+ function mergeAssignments(assignments) {
137
308
  const newAssignments = [];
138
- let current = ctx.assignments[0];
309
+ let current = assignments[0];
139
310
  let prev = current;
140
- for (let i = 1, len = ctx.assignments.length; i < len; i += 1) {
141
- const item = ctx.assignments[i];
311
+ for (let i = 1, len = assignments.length; i < len; i++) {
312
+ const item = assignments[i];
142
313
  if (item.type === prev.type) {
143
314
  if (item.type === "index" && item.value === prev.value) {
144
315
  current = {
@@ -172,15 +343,19 @@ function mergeAssignments(ctx) {
172
343
  newAssignments.push(current);
173
344
  return newAssignments;
174
345
  }
175
- function resolvePatches(ctx) {
176
- if (ctx.assignments.length) {
346
+ function resolveAssignments(assignments) {
347
+ if (assignments.length) {
177
348
  let result = "";
178
- for (const assignment of mergeAssignments(ctx)) {
179
- result += `${getAssignmentExpression(assignment)},`;
349
+ const merged = mergeAssignments(assignments);
350
+ for (let i = 0, len = merged.length; i < len; i++) {
351
+ result += `${getAssignmentExpression(merged[i])},`;
180
352
  }
181
353
  return result;
182
354
  }
183
- return "";
355
+ return void 0;
356
+ }
357
+ function resolvePatches(ctx) {
358
+ return resolveAssignments(ctx.assignments);
184
359
  }
185
360
  function markRef(ctx, current) {
186
361
  ctx.markedRefs[current] = true;
@@ -275,12 +450,6 @@ function getErrorConstructor(error) {
275
450
  }
276
451
  return "Error";
277
452
  }
278
- var EXCLUDED_ERROR_KEYS = {
279
- name: true,
280
- cause: true,
281
- stack: true,
282
- message: true
283
- };
284
453
  function getErrorOptions(error) {
285
454
  let options;
286
455
  const constructor = getErrorConstructor(error);
@@ -291,7 +460,7 @@ function getErrorOptions(error) {
291
460
  }
292
461
  const names = Object.getOwnPropertyNames(error);
293
462
  for (const name of names) {
294
- if (!(name in EXCLUDED_ERROR_KEYS)) {
463
+ if (name !== "name" && name !== "message") {
295
464
  options = options || {};
296
465
  options[name] = error[name];
297
466
  }
@@ -323,7 +492,7 @@ function generateRef(ctx, current) {
323
492
  ctx.refs.set(current, id);
324
493
  return id;
325
494
  }
326
- function generateSemiPrimitiveValue(current, id) {
495
+ function generateSemiPrimitiveValue(ctx, current, id) {
327
496
  if (constructorCheck(current, Date)) {
328
497
  return [2 /* Date */, current, id];
329
498
  }
@@ -331,41 +500,81 @@ function generateSemiPrimitiveValue(current, id) {
331
500
  return [3 /* RegExp */, current, id];
332
501
  }
333
502
  if (constructorCheck(current, Int8Array)) {
334
- return [13 /* TypedArray */, { constructor: "Int8Array", array: current }, id];
503
+ assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int8Array"');
504
+ return [13 /* TypedArray */, ["Int8Array", current], id];
335
505
  }
336
506
  if (constructorCheck(current, Int16Array)) {
337
- return [13 /* TypedArray */, { constructor: "Int16Array", array: current }, id];
507
+ assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int16Array"');
508
+ return [13 /* TypedArray */, ["Int16Array", current], id];
338
509
  }
339
510
  if (constructorCheck(current, Int32Array)) {
340
- return [13 /* TypedArray */, { constructor: "Int32Array", array: current }, id];
511
+ assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int32Array"');
512
+ return [13 /* TypedArray */, ["Int32Array", current], id];
341
513
  }
342
514
  if (constructorCheck(current, Uint8Array)) {
343
- return [13 /* TypedArray */, { constructor: "Uint8Array", array: current }, id];
515
+ assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint8Array"');
516
+ return [13 /* TypedArray */, ["Uint8Array", current], id];
344
517
  }
345
518
  if (constructorCheck(current, Uint16Array)) {
346
- return [13 /* TypedArray */, { constructor: "Uint16Array", array: current }, id];
519
+ assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint16Array"');
520
+ return [13 /* TypedArray */, ["Uint16Array", current], id];
347
521
  }
348
522
  if (constructorCheck(current, Uint32Array)) {
349
- return [13 /* TypedArray */, { constructor: "Uint32Array", array: current }, id];
523
+ assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint32Array"');
524
+ return [13 /* TypedArray */, ["Uint32Array", current], id];
350
525
  }
351
526
  if (constructorCheck(current, Uint8ClampedArray)) {
352
- return [13 /* TypedArray */, { constructor: "Uint8ClampedArray", array: current }, id];
527
+ assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint8ClampedArray"');
528
+ return [13 /* TypedArray */, ["Uint8ClampedArray", current], id];
353
529
  }
354
530
  if (constructorCheck(current, Float32Array)) {
355
- return [13 /* TypedArray */, { constructor: "Float32Array", array: current }, id];
531
+ assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Float32Array"');
532
+ return [13 /* TypedArray */, ["Float32Array", current], id];
356
533
  }
357
534
  if (constructorCheck(current, Float64Array)) {
358
- return [13 /* TypedArray */, { constructor: "Float64Array", array: current }, id];
535
+ assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Float64Array"');
536
+ return [13 /* TypedArray */, ["Float64Array", current], id];
359
537
  }
360
538
  if (constructorCheck(current, BigInt64Array)) {
361
- return [13 /* TypedArray */, { constructor: "BigInt64Array", array: current }, id];
539
+ assert(
540
+ ctx.features.has(10 /* TypedArray */) && ctx.features.has(3 /* BigInt */),
541
+ 'Unsupported value type "BigInt64Array"'
542
+ );
543
+ return [13 /* TypedArray */, ["BigInt64Array", current], id];
362
544
  }
363
545
  if (constructorCheck(current, BigUint64Array)) {
364
- return [13 /* TypedArray */, { constructor: "BigUint64Array", array: current }, id];
546
+ assert(
547
+ ctx.features.has(10 /* TypedArray */) && ctx.features.has(3 /* BigInt */),
548
+ 'Unsupported value type "BigUint64Array"'
549
+ );
550
+ return [13 /* TypedArray */, ["BigUint64Array", current], id];
365
551
  }
366
552
  return void 0;
367
553
  }
368
554
  function generateTreeSync(ctx, current) {
555
+ function serializeProperties(properties) {
556
+ const keyNodes = [];
557
+ const valueNodes = [];
558
+ const deferredKeys = [];
559
+ const deferredValues = [];
560
+ const keys = Object.keys(properties);
561
+ let deferredSize = 0;
562
+ for (const key of keys) {
563
+ if (isIterable(properties[key])) {
564
+ deferredKeys.push(key);
565
+ deferredValues.push(properties[key]);
566
+ deferredSize++;
567
+ } else {
568
+ keyNodes.push(key);
569
+ valueNodes.push(generateTreeSync(ctx, properties[key]));
570
+ }
571
+ }
572
+ for (let i = 0; i < deferredSize; i++) {
573
+ keyNodes.push(deferredKeys[i]);
574
+ valueNodes.push(generateTreeSync(ctx, deferredValues[i]));
575
+ }
576
+ return [keyNodes, valueNodes, keys.length];
577
+ }
369
578
  if (isPrimitive(current)) {
370
579
  return [0 /* Primitive */, current];
371
580
  }
@@ -373,11 +582,12 @@ function generateTreeSync(ctx, current) {
373
582
  if (Array.isArray(id)) {
374
583
  return id;
375
584
  }
376
- const semiPrimitive = generateSemiPrimitiveValue(current, id);
585
+ const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
377
586
  if (semiPrimitive) {
378
587
  return semiPrimitive;
379
588
  }
380
589
  if (constructorCheck(current, Set)) {
590
+ assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
381
591
  const nodes = [];
382
592
  const deferred = [];
383
593
  for (const item of current.keys()) {
@@ -387,95 +597,112 @@ function generateTreeSync(ctx, current) {
387
597
  nodes.push(generateTreeSync(ctx, item));
388
598
  }
389
599
  }
390
- for (const item of deferred) {
391
- nodes.push(generateTreeSync(ctx, item));
600
+ for (let i = 0, len = deferred.length; i < len; i++) {
601
+ nodes.push(generateTreeSync(ctx, deferred[i]));
392
602
  }
393
603
  return [4 /* Set */, nodes, id];
394
604
  }
395
605
  if (constructorCheck(current, Map)) {
396
- const nodes = [];
397
- const deferred = [];
606
+ assert(ctx.features.has(4 /* Map */), 'Unsupported type "Map"');
607
+ const keyNodes = [];
608
+ const valueNodes = [];
609
+ const deferredKey = [];
610
+ const deferredValue = [];
611
+ let deferredSize = 0;
398
612
  for (const [key, value] of current.entries()) {
399
613
  if (isIterable(key) || isIterable(value)) {
400
- deferred.push([key, value]);
614
+ deferredKey.push(key);
615
+ deferredValue.push(value);
616
+ deferredSize++;
401
617
  } else {
402
- const keyNode = generateTreeSync(ctx, key);
403
- const valueNode = generateTreeSync(ctx, value);
404
- nodes.push([keyNode, valueNode]);
618
+ keyNodes.push(generateTreeSync(ctx, key));
619
+ valueNodes.push(generateTreeSync(ctx, value));
405
620
  }
406
621
  }
407
- for (const [key, value] of deferred) {
408
- const keyNode = generateTreeSync(ctx, key);
409
- const valueNode = generateTreeSync(ctx, value);
410
- nodes.push([keyNode, valueNode]);
622
+ for (let i = 0; i < deferredSize; i++) {
623
+ keyNodes.push(generateTreeSync(ctx, deferredKey[i]));
624
+ valueNodes.push(generateTreeSync(ctx, deferredValue[i]));
411
625
  }
412
- return [5 /* Map */, nodes, id];
626
+ return [5 /* Map */, [keyNodes, valueNodes, current.size], id];
413
627
  }
414
628
  if (Array.isArray(current)) {
415
- const nodes = new Array(current.length);
416
- const deferred = new Array(current.length);
417
- for (const key in current) {
418
- const item = current[key];
419
- if (isIterable(item)) {
420
- deferred[key] = item;
421
- } else {
422
- nodes[key] = generateTreeSync(ctx, item);
629
+ const size = current.length;
630
+ const nodes = new Array(size);
631
+ const deferred = new Array(size);
632
+ for (let i = 0; i < size; i++) {
633
+ if (i in current) {
634
+ if (isIterable(current[i])) {
635
+ deferred[i] = current[i];
636
+ } else {
637
+ nodes[i] = generateTreeSync(ctx, current[i]);
638
+ }
423
639
  }
424
640
  }
425
- for (const key in deferred) {
426
- nodes[key] = generateTreeSync(ctx, deferred[key]);
641
+ for (let i = 0; i < size; i++) {
642
+ if (i in deferred) {
643
+ nodes[i] = generateTreeSync(ctx, deferred[i]);
644
+ }
427
645
  }
428
646
  return [6 /* Array */, nodes, id];
429
647
  }
430
- if (current instanceof AggregateError) {
648
+ if (current instanceof AggregateError && ctx.features.has(0 /* AggregateError */)) {
431
649
  const options = getErrorOptions(current);
432
- return [11 /* AggregateError */, {
433
- message: current.message,
434
- options: options ? generateTreeSync(ctx, options) : void 0,
435
- cause: "cause" in current ? generateTreeSync(ctx, { cause: current.cause }) : void 0,
436
- errors: generateTreeSync(ctx, current.errors)
437
- }, id];
650
+ const optionsNode = options ? serializeProperties(options) : void 0;
651
+ const errorsNode = generateTreeSync(ctx, current.errors);
652
+ return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
438
653
  }
439
654
  if (current instanceof Error) {
440
655
  const options = getErrorOptions(current);
441
- return [10 /* Error */, {
442
- constructor: getErrorConstructor(current),
443
- message: current.message,
444
- options: options ? generateTreeSync(ctx, options) : void 0,
445
- cause: "cause" in current ? generateTreeSync(ctx, { cause: current.cause }) : void 0
446
- }, id];
656
+ const optionsNode = options ? serializeProperties(options) : void 0;
657
+ return [
658
+ 10 /* Error */,
659
+ [getErrorConstructor(current), current.message, optionsNode],
660
+ id
661
+ ];
447
662
  }
448
663
  if (isIterable(current)) {
664
+ assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
449
665
  const options = getIterableOptions(current);
450
- return [12 /* Iterable */, {
666
+ return [12 /* Iterable */, [
451
667
  // Parse options first before the items
452
- options: options ? generateTreeSync(ctx, options) : void 0,
453
- items: generateTreeSync(ctx, Array.from(current))
454
- }, id];
668
+ options ? serializeProperties(options) : void 0,
669
+ generateTreeSync(ctx, Array.from(current))
670
+ ], id];
455
671
  }
456
672
  const empty = current.constructor == null;
457
673
  if (current.constructor === Object || empty) {
458
- const nodes = {};
459
- const deferred = {};
460
- for (const [key, item] of Object.entries(current)) {
461
- if (isIterable(item)) {
462
- deferred[key] = item;
463
- } else {
464
- nodes[key] = generateTreeSync(ctx, item);
465
- }
466
- }
467
- for (const [key, item] of Object.entries(deferred)) {
468
- nodes[key] = generateTreeSync(ctx, item);
469
- }
470
674
  return [
471
675
  empty ? 8 /* NullConstructor */ : 7 /* Object */,
472
- nodes,
676
+ serializeProperties(current),
473
677
  id
474
678
  ];
475
679
  }
476
680
  throw new Error("Unsupported value");
477
681
  }
478
682
  async function generateTreeAsync(ctx, current) {
683
+ async function serializeProperties(properties) {
684
+ const keyNodes = [];
685
+ const valueNodes = [];
686
+ const deferredKeys = [];
687
+ const deferredValues = [];
688
+ const keys = Object.keys(properties);
689
+ let deferredSize = 0;
690
+ for (const key of keys) {
691
+ if (isIterable(properties[key])) {
692
+ deferredKeys.push(key);
693
+ deferredValues.push(properties[key]);
694
+ deferredSize++;
695
+ } else {
696
+ keyNodes.push(key);
697
+ valueNodes.push(await generateTreeAsync(ctx, properties[key]));
698
+ }
699
+ }
700
+ for (let i = 0; i < deferredSize; i++) {
701
+ keyNodes.push(deferredKeys[i]);
702
+ valueNodes.push(await generateTreeAsync(ctx, deferredValues[i]));
703
+ }
704
+ return [keyNodes, valueNodes, keys.length];
705
+ }
479
706
  if (isPrimitive(current)) {
480
707
  return [0 /* Primitive */, current];
481
708
  }
@@ -483,11 +710,12 @@ async function generateTreeAsync(ctx, current) {
483
710
  if (Array.isArray(id)) {
484
711
  return id;
485
712
  }
486
- const semiPrimitive = generateSemiPrimitiveValue(current, id);
713
+ const semiPrimitive = generateSemiPrimitiveValue(ctx, current, id);
487
714
  if (semiPrimitive) {
488
715
  return semiPrimitive;
489
716
  }
490
717
  if (isPromise(current)) {
718
+ assert(ctx.features.has(7 /* Promise */), 'Unsupported type "Promise"');
491
719
  return current.then(async (value) => [
492
720
  9 /* Promise */,
493
721
  await generateTreeAsync(ctx, value),
@@ -495,6 +723,7 @@ async function generateTreeAsync(ctx, current) {
495
723
  ]);
496
724
  }
497
725
  if (constructorCheck(current, Set)) {
726
+ assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
498
727
  const nodes = [];
499
728
  const deferred = [];
500
729
  for (const item of current.keys()) {
@@ -504,97 +733,177 @@ async function generateTreeAsync(ctx, current) {
504
733
  nodes.push(await generateTreeAsync(ctx, item));
505
734
  }
506
735
  }
507
- for (const item of deferred) {
508
- nodes.push(await generateTreeAsync(ctx, item));
736
+ for (let i = 0, len = deferred.length; i < len; i++) {
737
+ nodes.push(await generateTreeAsync(ctx, deferred[i]));
509
738
  }
510
739
  return [4 /* Set */, nodes, id];
511
740
  }
512
741
  if (constructorCheck(current, Map)) {
513
- const nodes = [];
514
- const deferred = [];
742
+ assert(ctx.features.has(4 /* Map */), 'Unsupported type "Map"');
743
+ const keyNodes = [];
744
+ const valueNodes = [];
745
+ const deferredKey = [];
746
+ const deferredValue = [];
747
+ let deferredSize = 0;
515
748
  for (const [key, value] of current.entries()) {
516
749
  if (isIterable(key) || isIterable(value)) {
517
- deferred.push([key, value]);
750
+ deferredKey.push(key);
751
+ deferredValue.push(value);
752
+ deferredSize++;
518
753
  } else {
519
- const keyNode = await generateTreeAsync(ctx, key);
520
- const valueNode = await generateTreeAsync(ctx, value);
521
- nodes.push([keyNode, valueNode]);
754
+ keyNodes.push(await generateTreeAsync(ctx, key));
755
+ valueNodes.push(await generateTreeAsync(ctx, value));
522
756
  }
523
757
  }
524
- for (const [key, value] of deferred) {
525
- const keyNode = await generateTreeAsync(ctx, key);
526
- const valueNode = await generateTreeAsync(ctx, value);
527
- nodes.push([keyNode, valueNode]);
758
+ for (let i = 0; i < deferredSize; i++) {
759
+ keyNodes.push(await generateTreeAsync(ctx, deferredKey[i]));
760
+ valueNodes.push(await generateTreeAsync(ctx, deferredValue[i]));
528
761
  }
529
- return [5 /* Map */, nodes, id];
762
+ return [5 /* Map */, [keyNodes, valueNodes, current.size], id];
530
763
  }
531
764
  if (Array.isArray(current)) {
532
- const nodes = new Array(current.length);
533
- const deferred = new Array(current.length);
534
- for (const key in current) {
535
- const item = current[key];
536
- if (isIterable(item)) {
537
- deferred[key] = item;
538
- } else {
539
- nodes[key] = await generateTreeAsync(ctx, item);
765
+ const size = current.length;
766
+ const nodes = new Array(size);
767
+ const deferred = new Array(size);
768
+ for (let i = 0; i < size; i++) {
769
+ const item = current[i];
770
+ if (i in current) {
771
+ if (isIterable(item)) {
772
+ deferred[i] = item;
773
+ } else {
774
+ nodes[i] = await generateTreeAsync(ctx, item);
775
+ }
540
776
  }
541
777
  }
542
- for (const key in deferred) {
543
- nodes[key] = await generateTreeAsync(ctx, deferred[key]);
778
+ for (let i = 0; i < size; i++) {
779
+ if (i in deferred) {
780
+ nodes[i] = await generateTreeAsync(ctx, deferred[i]);
781
+ }
544
782
  }
545
783
  return [6 /* Array */, nodes, id];
546
784
  }
547
785
  if (current instanceof AggregateError) {
548
786
  const options = getErrorOptions(current);
549
- return [11 /* AggregateError */, {
550
- message: current.message,
551
- options: options ? generateTreeSync(ctx, options) : void 0,
552
- cause: "cause" in current ? await generateTreeAsync(ctx, { cause: current.cause }) : void 0,
553
- errors: await generateTreeAsync(ctx, current.errors)
554
- }, id];
787
+ const optionsNode = options ? await serializeProperties(options) : void 0;
788
+ const errorsNode = await generateTreeAsync(ctx, current.errors);
789
+ return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
555
790
  }
556
791
  if (current instanceof Error) {
557
792
  const options = getErrorOptions(current);
558
- return [10 /* Error */, {
559
- constructor: getErrorConstructor(current),
560
- message: current.message,
561
- options: options ? await generateTreeAsync(ctx, options) : void 0,
562
- cause: "cause" in current ? await generateTreeAsync(ctx, { cause: current.cause }) : void 0
563
- }, id];
793
+ const optionsNode = options ? await serializeProperties(options) : void 0;
794
+ return [
795
+ 10 /* Error */,
796
+ [getErrorConstructor(current), current.message, optionsNode],
797
+ id
798
+ ];
564
799
  }
565
800
  if (isIterable(current)) {
801
+ assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
566
802
  const options = getIterableOptions(current);
567
- return [12 /* Iterable */, {
568
- options: options ? await generateTreeAsync(ctx, options) : void 0,
569
- items: await generateTreeAsync(ctx, Array.from(current))
570
- }, id];
803
+ return [12 /* Iterable */, [
804
+ options ? await serializeProperties(options) : void 0,
805
+ await generateTreeAsync(ctx, Array.from(current))
806
+ ], id];
571
807
  }
572
808
  const empty = current.constructor == null;
573
809
  if (current.constructor === Object || empty) {
574
- const nodes = {};
575
- const deferred = {};
576
- for (const [key, item] of Object.entries(current)) {
577
- if (isIterable(item)) {
578
- deferred[key] = item;
579
- } else {
580
- nodes[key] = await generateTreeAsync(ctx, item);
581
- }
582
- }
583
- for (const [key, item] of Object.entries(deferred)) {
584
- nodes[key] = await generateTreeAsync(ctx, item);
585
- }
586
810
  return [
587
811
  empty ? 8 /* NullConstructor */ : 7 /* Object */,
588
- nodes,
812
+ await serializeProperties(current),
589
813
  id
590
814
  ];
591
815
  }
592
816
  throw new Error("Unsupported value");
593
817
  }
818
+ function serializePrimitive(ctx, value) {
819
+ if (value === true) {
820
+ return "!0";
821
+ }
822
+ if (value === false) {
823
+ return "!1";
824
+ }
825
+ if (value === void 0) {
826
+ return "void 0";
827
+ }
828
+ if (value === null) {
829
+ return "null";
830
+ }
831
+ if (typeof value === "bigint") {
832
+ assert(ctx.features.has(3 /* BigInt */), 'Unsupported type "BigInt"');
833
+ return `${value}n`;
834
+ }
835
+ if (typeof value === "string") {
836
+ return quote(value);
837
+ }
838
+ if (Object.is(value, -0)) {
839
+ return "-0";
840
+ }
841
+ if (Object.is(value, Infinity)) {
842
+ return "1/0";
843
+ }
844
+ if (Object.is(value, -Infinity)) {
845
+ return "-1/0";
846
+ }
847
+ return String(value);
848
+ }
594
849
  function serializeTree(ctx, [type, value, id]) {
850
+ function serializeAssignments(targetRef, [keys, values, size]) {
851
+ ctx.stack.push(targetRef);
852
+ const mainAssignments = [];
853
+ for (let i = 0; i < size; i++) {
854
+ const parentStack = ctx.stack;
855
+ ctx.stack = [];
856
+ const refParam = serializeTree(ctx, values[i]);
857
+ ctx.stack = parentStack;
858
+ const key = keys[i];
859
+ const check = Number(key);
860
+ const parentAssignment = ctx.assignments;
861
+ ctx.assignments = mainAssignments;
862
+ if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.test(key)) {
863
+ if (!Number.isNaN(check)) {
864
+ createObjectComputedAssign(ctx, targetRef, key, refParam);
865
+ } else {
866
+ createObjectIdentifierAssign(ctx, targetRef, key, refParam);
867
+ }
868
+ } else {
869
+ createObjectComputedAssign(ctx, targetRef, quote(key), refParam);
870
+ }
871
+ ctx.assignments = parentAssignment;
872
+ }
873
+ ctx.stack.pop();
874
+ return resolveAssignments(mainAssignments);
875
+ }
876
+ function serializeObject(sourceID, [keys, values, size]) {
877
+ let result = "";
878
+ ctx.stack.push(sourceID);
879
+ for (let i = 0; i < size; i++) {
880
+ const key = keys[i];
881
+ const val = values[i];
882
+ const check = Number(key);
883
+ if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.test(key)) {
884
+ if (isReferenceInStack(ctx, val)) {
885
+ const refParam = getRefParam(ctx, val[1]);
886
+ if (!Number.isNaN(check)) {
887
+ createObjectComputedAssign(ctx, sourceID, key, refParam);
888
+ } else {
889
+ createObjectIdentifierAssign(ctx, sourceID, key, refParam);
890
+ }
891
+ } else {
892
+ result += `${key}:${serializeTree(ctx, val)},`;
893
+ }
894
+ } else if (isReferenceInStack(ctx, val)) {
895
+ const refParam = getRefParam(ctx, val[1]);
896
+ createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
897
+ } else {
898
+ result += `${quote(key)}:${serializeTree(ctx, val)},`;
899
+ }
900
+ }
901
+ ctx.stack.pop();
902
+ return `{${result.substring(0, result.length - 1)}}`;
903
+ }
595
904
  switch (type) {
596
905
  case 0 /* Primitive */:
597
- return serializePrimitive(value);
906
+ return serializePrimitive(ctx, value);
598
907
  case 1 /* Reference */:
599
908
  return getRefParam(ctx, value);
600
909
  case 9 /* Promise */: {
@@ -614,38 +923,46 @@ function serializeTree(ctx, [type, value, id]) {
614
923
  case 3 /* RegExp */:
615
924
  return assignRef(ctx, id, String(value));
616
925
  case 13 /* TypedArray */: {
617
- const values = [...value.array].map(serializePrimitive).join(",");
618
- let args = `[${values}]`;
619
- if (value.array.byteOffset !== 0) {
620
- args += `,${value.array.byteOffset}`;
926
+ let values = "";
927
+ for (let i = 0, len = value[1].length; i < len; i++) {
928
+ values += `${serializePrimitive(ctx, value[1][i])},`;
621
929
  }
622
- return assignRef(ctx, id, `new ${value.constructor}(${args})`);
930
+ let args = values ? `[${values.substring(0, values.length - 1)}]` : "[]";
931
+ if (value[1].byteOffset !== 0) {
932
+ args += `,${value[1].byteOffset}`;
933
+ }
934
+ return assignRef(ctx, id, `new ${value[0]}(${args})`);
623
935
  }
624
936
  case 4 /* Set */: {
625
937
  let serialized = "new Set";
626
- if (value.length) {
627
- const values = [];
938
+ const size = value.length;
939
+ if (size) {
940
+ let result = "";
628
941
  ctx.stack.push(id);
629
- for (const item of value) {
942
+ for (let i = 0; i < size; i++) {
943
+ const item = value[i];
630
944
  if (isReferenceInStack(ctx, item)) {
631
945
  createSetAdd(ctx, id, getRefParam(ctx, item[1]));
632
946
  } else {
633
- values.push(serializeTree(ctx, item));
947
+ result += `${serializeTree(ctx, item)},`;
634
948
  }
635
949
  }
636
950
  ctx.stack.pop();
637
- if (values.length) {
638
- serialized += `([${values.join(",")}])`;
951
+ if (result) {
952
+ serialized += `([${result.substring(0, result.length - 1)}])`;
639
953
  }
640
954
  }
641
955
  return assignRef(ctx, id, serialized);
642
956
  }
643
957
  case 5 /* Map */: {
644
958
  let serialized = "new Map";
645
- if (value.length) {
646
- const values = [];
959
+ const size = value[2];
960
+ if (size) {
961
+ let result = "";
647
962
  ctx.stack.push(id);
648
- for (const [key, val] of value) {
963
+ for (let i = 0; i < size; i++) {
964
+ const key = value[0][i];
965
+ const val = value[1][i];
649
966
  if (isReferenceInStack(ctx, key)) {
650
967
  const keyRef = getRefParam(ctx, key[1]);
651
968
  if (isReferenceInStack(ctx, val)) {
@@ -664,20 +981,21 @@ function serializeTree(ctx, [type, value, id]) {
664
981
  createMapSet(ctx, id, serializeTree(ctx, key), valueRef);
665
982
  ctx.stack = parent;
666
983
  } else {
667
- values.push(`[${serializeTree(ctx, key)},${serializeTree(ctx, val)}]`);
984
+ result += `[${serializeTree(ctx, key)},${serializeTree(ctx, val)}],`;
668
985
  }
669
986
  }
670
987
  ctx.stack.pop();
671
- if (values.length) {
672
- serialized += `([${values.join(",")}])`;
988
+ if (result) {
989
+ serialized += `([${result.substring(0, result.length - 1)}])`;
673
990
  }
674
991
  }
675
992
  return assignRef(ctx, id, serialized);
676
993
  }
677
994
  case 6 /* Array */: {
995
+ const size = value.length;
678
996
  let values = "";
679
997
  ctx.stack.push(id);
680
- for (let i = 0, len = value.length; i < len; i++) {
998
+ for (let i = 0; i < size; i++) {
681
999
  const item = value[i];
682
1000
  if (i in value) {
683
1001
  if (isReferenceInStack(ctx, item)) {
@@ -685,7 +1003,7 @@ function serializeTree(ctx, [type, value, id]) {
685
1003
  values += ",";
686
1004
  } else {
687
1005
  values += serializeTree(ctx, item);
688
- if (i < value.length - 1) {
1006
+ if (i < size - 1) {
689
1007
  values += ",";
690
1008
  }
691
1009
  }
@@ -697,74 +1015,93 @@ function serializeTree(ctx, [type, value, id]) {
697
1015
  return assignRef(ctx, id, `[${values}]`);
698
1016
  }
699
1017
  case 11 /* AggregateError */: {
700
- const args = [serializeTree(ctx, value.errors), quote(value.message)];
701
- if (value.cause) {
702
- args.push(serializeTree(ctx, value.cause));
703
- }
704
- let serialized = `new AggregateError(${args.join(",")})`;
705
- if (value.options) {
706
- const options = serializeTree(ctx, value.options);
707
- serialized = `Object.assign(${serialized},${options})`;
1018
+ ctx.stack.push(id);
1019
+ let serialized = `new AggregateError(${serializeTree(ctx, value[2])},${quote(value[0])})`;
1020
+ ctx.stack.pop();
1021
+ if (value[1]) {
1022
+ if (ctx.features.has(6 /* ObjectAssign */)) {
1023
+ const options = serializeObject(id, value[1]);
1024
+ serialized = `Object.assign(${serialized},${options})`;
1025
+ } else {
1026
+ markRef(ctx, id);
1027
+ const assignments = serializeAssignments(id, value[1]);
1028
+ if (assignments) {
1029
+ const ref = getRefParam(ctx, id);
1030
+ return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1031
+ }
1032
+ }
708
1033
  }
709
1034
  return assignRef(ctx, id, serialized);
710
1035
  }
711
1036
  case 10 /* Error */: {
712
- const args = [quote(value.message)];
713
- if (value.cause) {
714
- args.push(serializeTree(ctx, value.cause));
715
- }
716
- let serialized = `new ${value.constructor}(${args.join(",")})`;
717
- if (value.options) {
718
- const options = serializeTree(ctx, value.options);
719
- serialized = `Object.assign(${serialized},${options})`;
1037
+ let serialized = `new ${value[0]}(${quote(value[1])})`;
1038
+ if (value[2]) {
1039
+ if (ctx.features.has(6 /* ObjectAssign */)) {
1040
+ const options = serializeObject(id, value[2]);
1041
+ serialized = `Object.assign(${serialized},${options})`;
1042
+ } else {
1043
+ markRef(ctx, id);
1044
+ const assignments = serializeAssignments(id, value[2]);
1045
+ if (assignments) {
1046
+ const ref = getRefParam(ctx, id);
1047
+ return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1048
+ }
1049
+ }
720
1050
  }
721
1051
  return assignRef(ctx, id, serialized);
722
1052
  }
723
1053
  case 12 /* Iterable */: {
724
1054
  const parent = ctx.stack;
725
1055
  ctx.stack = [];
726
- const values = serializeTree(ctx, value.items);
1056
+ const values = serializeTree(ctx, value[1]);
727
1057
  ctx.stack = parent;
728
- let serialized = `{[Symbol.iterator]:()=>${values}.values()}`;
729
- if (value.options) {
730
- ctx.stack.push(id);
731
- const options = serializeTree(ctx, value.options);
732
- ctx.stack.pop();
733
- serialized = `Object.assign(${serialized},${options})`;
1058
+ let serialized;
1059
+ if (ctx.features.has(1 /* ArrayPrototypeValues */)) {
1060
+ serialized = `${values}.values()`;
1061
+ } else {
1062
+ serialized = `${values}[Symbol.iterator]()`;
734
1063
  }
735
- return assignRef(ctx, id, serialized);
736
- }
737
- case 8 /* NullConstructor */:
738
- case 7 /* Object */: {
739
- const values = [];
740
- ctx.stack.push(id);
741
- for (const [key, val] of Object.entries(value)) {
742
- const check = Number(key);
743
- if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.test(key)) {
744
- if (isReferenceInStack(ctx, val)) {
745
- const refParam = getRefParam(ctx, val[1]);
746
- if (!Number.isNaN(check)) {
747
- createObjectComputedAssign(ctx, id, key, refParam);
748
- } else {
749
- createObjectIdentifierAssign(ctx, id, key, refParam);
750
- }
751
- } else {
752
- values.push(`${key}:${serializeTree(ctx, val)}`);
753
- }
754
- } else if (isReferenceInStack(ctx, val)) {
755
- const refParam = getRefParam(ctx, val[1]);
756
- createObjectComputedAssign(ctx, id, quote(key), refParam);
1064
+ if (ctx.features.has(2 /* ArrowFunction */)) {
1065
+ serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1066
+ } else if (ctx.features.has(5 /* MethodShorthand */)) {
1067
+ serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1068
+ } else {
1069
+ serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1070
+ }
1071
+ if (value[0]) {
1072
+ if (ctx.features.has(6 /* ObjectAssign */)) {
1073
+ const options = serializeObject(id, value[0]);
1074
+ serialized = `Object.assign(${serialized},${options})`;
757
1075
  } else {
758
- values.push(`${quote(key)}:${serializeTree(ctx, val)}`);
1076
+ markRef(ctx, id);
1077
+ const assignments = serializeAssignments(id, value[0]);
1078
+ if (assignments) {
1079
+ const ref = getRefParam(ctx, id);
1080
+ return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1081
+ }
759
1082
  }
760
1083
  }
761
- ctx.stack.pop();
762
- let serialized = `{${values.join(",")}}`;
763
- if (type === 8 /* NullConstructor */) {
764
- serialized = `Object.assign(Object.create(null),${serialized})`;
1084
+ return assignRef(ctx, id, serialized);
1085
+ }
1086
+ case 8 /* NullConstructor */: {
1087
+ let serialized = "Object.create(null)";
1088
+ if (ctx.features.has(6 /* ObjectAssign */)) {
1089
+ const fields = serializeObject(id, value);
1090
+ if (fields !== "{}") {
1091
+ serialized = `Object.assign(${serialized},${fields})`;
1092
+ }
1093
+ } else {
1094
+ markRef(ctx, id);
1095
+ const assignments = serializeAssignments(id, value);
1096
+ if (assignments) {
1097
+ const ref = getRefParam(ctx, id);
1098
+ return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
1099
+ }
765
1100
  }
766
1101
  return assignRef(ctx, id, serialized);
767
1102
  }
1103
+ case 7 /* Object */:
1104
+ return assignRef(ctx, id, serializeObject(id, value));
768
1105
  default:
769
1106
  throw new Error("Unsupported type");
770
1107
  }
@@ -773,33 +1110,42 @@ function serializeTree(ctx, [type, value, id]) {
773
1110
  // src/index.ts
774
1111
  function finalize(ctx, source, result) {
775
1112
  if (ctx.vars.length) {
776
- const index = getRefParam(ctx, createRef(ctx, source));
777
1113
  const patches = resolvePatches(ctx);
778
- const params = ctx.vars.length > 1 ? `(${ctx.vars.join(",")})` : ctx.vars[0];
779
- if (result.startsWith(`${index}=`)) {
780
- return `(${params}=>(${result},${patches}${index}))()`;
1114
+ let body = result;
1115
+ if (patches) {
1116
+ const index = getRefParam(ctx, createRef(ctx, source));
1117
+ if (result.startsWith(`${index}=`)) {
1118
+ body = `${result},${patches}${index}`;
1119
+ } else {
1120
+ body = `${index}=${result},${patches}${index}`;
1121
+ }
1122
+ }
1123
+ let params = ctx.vars.length > 1 ? ctx.vars.join(",") : ctx.vars[0];
1124
+ if (ctx.features.has(2 /* ArrowFunction */)) {
1125
+ params = ctx.vars.length > 1 || ctx.vars.length === 0 ? `(${params})` : params;
1126
+ return `(${params}=>(${body}))()`;
781
1127
  }
782
- return `(${params}=>(${index}=${result},${patches}${index}))()`;
1128
+ return `(function(${params}){return ${body}})()`;
783
1129
  }
784
1130
  if (source.constructor === Object) {
785
1131
  return `(${result})`;
786
1132
  }
787
1133
  return result;
788
1134
  }
789
- function serialize(source) {
1135
+ function serialize(source, options) {
1136
+ const ctx = createSerializationContext(options);
790
1137
  if (isPrimitive(source)) {
791
- return serializePrimitive(source);
1138
+ return serializePrimitive(ctx, source);
792
1139
  }
793
- const ctx = createSerializationContext();
794
1140
  const tree = generateTreeSync(ctx, source);
795
1141
  const result = serializeTree(ctx, tree);
796
1142
  return finalize(ctx, source, result);
797
1143
  }
798
- async function serializeAsync(source) {
1144
+ async function serializeAsync(source, options) {
1145
+ const ctx = createSerializationContext(options);
799
1146
  if (isPrimitive(source)) {
800
- return serializePrimitive(source);
1147
+ return serializePrimitive(ctx, source);
801
1148
  }
802
- const ctx = createSerializationContext();
803
1149
  const tree = await generateTreeAsync(ctx, source);
804
1150
  const result = serializeTree(ctx, tree);
805
1151
  return finalize(ctx, source, result);