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