@oseo/compiler 0.0.0-stage → 0.1.0-canary.368

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.
Files changed (105) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +59 -2
  3. package/dist/agent-programs.d.ts +28 -0
  4. package/dist/agent-programs.d.ts.map +1 -0
  5. package/dist/agent-programs.js +134 -0
  6. package/dist/agent-programs.js.map +1 -0
  7. package/dist/agent-templates.d.ts +33 -0
  8. package/dist/agent-templates.d.ts.map +1 -0
  9. package/dist/agent-templates.js +45 -0
  10. package/dist/agent-templates.js.map +1 -0
  11. package/dist/compile.d.ts +20 -0
  12. package/dist/compile.d.ts.map +1 -0
  13. package/dist/compile.js +75 -0
  14. package/dist/compile.js.map +1 -0
  15. package/dist/fragments.d.ts +72 -0
  16. package/dist/fragments.d.ts.map +1 -0
  17. package/dist/fragments.js +158 -0
  18. package/dist/fragments.js.map +1 -0
  19. package/dist/hir-build.d.ts +80 -0
  20. package/dist/hir-build.d.ts.map +1 -0
  21. package/dist/hir-build.js +3077 -0
  22. package/dist/hir-build.js.map +1 -0
  23. package/dist/hir-print.d.ts +10 -0
  24. package/dist/hir-print.d.ts.map +1 -0
  25. package/dist/hir-print.js +271 -0
  26. package/dist/hir-print.js.map +1 -0
  27. package/dist/hir.d.ts +1246 -0
  28. package/dist/hir.d.ts.map +1 -0
  29. package/dist/hir.js +147 -0
  30. package/dist/hir.js.map +1 -0
  31. package/dist/host-scripts.d.ts +14 -0
  32. package/dist/host-scripts.d.ts.map +1 -0
  33. package/dist/host-scripts.js +204 -0
  34. package/dist/host-scripts.js.map +1 -0
  35. package/dist/index.d.ts +26 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +27 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/mir-build.d.ts +8 -0
  40. package/dist/mir-build.d.ts.map +1 -0
  41. package/dist/mir-build.js +5113 -0
  42. package/dist/mir-build.js.map +1 -0
  43. package/dist/mir-print.d.ts +7 -0
  44. package/dist/mir-print.d.ts.map +1 -0
  45. package/dist/mir-print.js +65 -0
  46. package/dist/mir-print.js.map +1 -0
  47. package/dist/mir-specialize.d.ts +7 -0
  48. package/dist/mir-specialize.d.ts.map +1 -0
  49. package/dist/mir-specialize.js +182 -0
  50. package/dist/mir-specialize.js.map +1 -0
  51. package/dist/mir.d.ts +571 -0
  52. package/dist/mir.d.ts.map +1 -0
  53. package/dist/mir.js +1 -0
  54. package/dist/module-builtin.d.ts +20 -0
  55. package/dist/module-builtin.d.ts.map +1 -0
  56. package/dist/module-builtin.js +57 -0
  57. package/dist/module-builtin.js.map +1 -0
  58. package/dist/module-compile.d.ts +64 -0
  59. package/dist/module-compile.d.ts.map +1 -0
  60. package/dist/module-compile.js +690 -0
  61. package/dist/module-compile.js.map +1 -0
  62. package/dist/module-json.d.ts +7 -0
  63. package/dist/module-json.d.ts.map +1 -0
  64. package/dist/module-json.js +88 -0
  65. package/dist/module-json.js.map +1 -0
  66. package/dist/modules.d.ts +87 -0
  67. package/dist/modules.d.ts.map +1 -0
  68. package/dist/modules.js +734 -0
  69. package/dist/modules.js.map +1 -0
  70. package/dist/native-fragments.d.ts +36 -0
  71. package/dist/native-fragments.d.ts.map +1 -0
  72. package/dist/native-fragments.js +110 -0
  73. package/dist/native-fragments.js.map +1 -0
  74. package/dist/native.d.ts +282 -0
  75. package/dist/native.d.ts.map +1 -0
  76. package/dist/native.js +47 -0
  77. package/dist/native.js.map +1 -0
  78. package/dist/regexp-execute.d.ts +74 -0
  79. package/dist/regexp-execute.d.ts.map +1 -0
  80. package/dist/regexp-execute.js +480 -0
  81. package/dist/regexp-execute.js.map +1 -0
  82. package/dist/regexp-matcher.d.ts +279 -0
  83. package/dist/regexp-matcher.d.ts.map +1 -0
  84. package/dist/regexp-matcher.js +1098 -0
  85. package/dist/regexp-matcher.js.map +1 -0
  86. package/dist/regexp-parse.d.ts +22 -0
  87. package/dist/regexp-parse.d.ts.map +1 -0
  88. package/dist/regexp-parse.js +1440 -0
  89. package/dist/regexp-parse.js.map +1 -0
  90. package/dist/regexp-print.d.ts +24 -0
  91. package/dist/regexp-print.d.ts.map +1 -0
  92. package/dist/regexp-print.js +147 -0
  93. package/dist/regexp-print.js.map +1 -0
  94. package/dist/regexp.d.ts +324 -0
  95. package/dist/regexp.d.ts.map +1 -0
  96. package/dist/regexp.js +22 -0
  97. package/dist/regexp.js.map +1 -0
  98. package/dist/source.d.ts +38 -0
  99. package/dist/source.d.ts.map +1 -0
  100. package/dist/source.js +10 -0
  101. package/dist/source.js.map +1 -0
  102. package/dist/syntax.d.ts +1232 -0
  103. package/dist/syntax.d.ts.map +1 -0
  104. package/dist/syntax.js +1 -0
  105. package/package.json +32 -3
@@ -0,0 +1,3077 @@
1
+ import { agentTemplateSegments, isAgentStartTarget, test262HostGlobalName } from "./agent-templates.js";
2
+ import { anonymousDefinition, errorIntrinsicName, importMetaName, intrinsicGlobalObjectName, isStandardGlobalName, isTypedArrayIntrinsicName } from "./hir.js";
3
+
4
+ //#region src/hir-build.ts
5
+ function includePropertiesWhen(properties) {
6
+ return properties() ?? {};
7
+ }
8
+ /**
9
+ * The OSEO1001 message of every ImportCall the profile rejects. The
10
+ * exclusion audit quotes this text, so it changes only with an audit
11
+ * update.
12
+ */
13
+ const importCallProfileMessage = "ImportExpression is outside the M1 profile.";
14
+ function sourceDiagnostic(sourceId, node, message) {
15
+ return {
16
+ byteRange: node.byteRange ?? {
17
+ end: 0,
18
+ start: 0
19
+ },
20
+ code: "OSEO1001",
21
+ message,
22
+ range: node.range,
23
+ sourceId
24
+ };
25
+ }
26
+ function findBinding(scopes, state, name) {
27
+ for (let index = scopes.length - 1; index >= 0; index -= 1) {
28
+ const binding = scopes[index]?.get(name);
29
+ if (binding != null) {
30
+ state.referencedBindingIds.add(binding.id);
31
+ return binding;
32
+ }
33
+ }
34
+ }
35
+ /**
36
+ * Resolve one name while retaining each intervening `with` object.
37
+ * A lexical binding stops the search, so an outer `with` environment
38
+ * cannot bypass a nearer declarative environment.
39
+ */
40
+ function resolveName(scopes, state, name, preserveGlobalBinding = false) {
41
+ const objectBindingIds = [];
42
+ for (let index = scopes.length - 1; index >= 0; index -= 1) {
43
+ const scope = scopes[index];
44
+ if (scope == null) continue;
45
+ const binding = scope.get(name);
46
+ if (binding != null) {
47
+ if (scope === state.scriptScope) state.globalReferences?.add(name);
48
+ state.referencedBindingIds.add(binding.id);
49
+ if (!preserveGlobalBinding && state.hostScriptBindings === true && state.globalObjectBindingIds.has(binding.id)) return { objectBindingIds };
50
+ return {
51
+ binding,
52
+ objectBindingIds
53
+ };
54
+ }
55
+ const objectBindingId = state.withScopes.get(scope);
56
+ if (objectBindingId != null) objectBindingIds.push(objectBindingId);
57
+ }
58
+ state.globalReferences?.add(name);
59
+ return { objectBindingIds };
60
+ }
61
+ /**
62
+ * Allocate the uninitialized fallback behind an unresolved `with` name.
63
+ * Only a name the realm global object never resolves reaches it, which
64
+ * this profile limits to a runtime-owned call-target intrinsic and a
65
+ * standard global it has not admitted as a value. Every other all-miss
66
+ * path reaches the global object's property instead, so no hidden cell
67
+ * can shadow a real global property. An `initializing` use is one whose
68
+ * all-miss path reaches PutValue without a prior GetValue, so it can
69
+ * actually write the hidden cell: a simple assignment or a destructuring
70
+ * or loop assignment target. Recording those names lets a separately
71
+ * compiled Script fragment fall back instead of hiding the write.
72
+ */
73
+ function withFallbackBinding(name, state, initializing) {
74
+ const owner = state.withFallbacks.at(-1);
75
+ if (owner == null) throw new Error("A with fallback was requested outside a with statement.");
76
+ if (initializing) state.withInitializingFallbackNames.add(name);
77
+ const existing = owner.get(name);
78
+ if (existing != null) return existing;
79
+ const binding = {
80
+ id: state.nextBindingId,
81
+ mutable: true,
82
+ name
83
+ };
84
+ state.nextBindingId += 1;
85
+ owner.set(name, binding);
86
+ return binding;
87
+ }
88
+ /**
89
+ * Reject a strict-code write to a `with` fallback that has no realm
90
+ * global-object property behind it. Global-object names reach the
91
+ * global object's strict PutValue instead, so only runtime-owned call
92
+ * targets such as `console`, `setTimeout`, and `clearTimeout` remain
93
+ * here: their fallback is a hidden cell, which cannot model a strict
94
+ * PutValue's resolution and failure checks. The rejection covers simple,
95
+ * pattern, and loop-head writes; it consequently never records an
96
+ * initializing name that could poison the typeof fold.
97
+ */
98
+ function rejectStrictWithFallbackWrite(name, located, state) {
99
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, located, `Assigning with fallback binding '${name}' in strict code is outside the admitted global-object profile; a strict PutValue on an unresolvable reference throws instead of creating a global.`));
100
+ }
101
+ /**
102
+ * Reject a `with` fallback write that would bypass the realm global
103
+ * object's property for the same name. Object-environment hits remain
104
+ * admitted, but an all-miss PutValue reaches the global object, where a
105
+ * non-strict write creates or updates a real property; the compiler
106
+ * cannot let that path create a separate hidden cell instead, because a
107
+ * later reference to the name reads the global object. A host Script's
108
+ * declared name never reaches this rejection: its write leaf carries
109
+ * the global-object fallback from `hostScriptWithGlobalFallback`.
110
+ */
111
+ function rejectGlobalObjectWithFallbackWrite(name, located, state) {
112
+ if (!isGlobalObjectResolvedName(name, state)) return false;
113
+ const subject = isPropertyOwnedIntrinsicName(name) ? "property-owned intrinsic" : "global-object name";
114
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, located, `Assigning ${subject} '${name}' through a with fallback is outside the admitted global-object profile.`));
115
+ return true;
116
+ }
117
+ /**
118
+ * The global-object fallback of a host Script's `var` or function name
119
+ * behind an all-miss `with` chain. Such a binding is the realm global
120
+ * object's property, so `resolveName` reports it unbound. A pattern or
121
+ * loop-head write leaf keeps the declared binding's identity, but its
122
+ * PutValue reaches that property through the persistent Global
123
+ * Environment Record, as a simple or compound `with` assignment does.
124
+ */
125
+ function hostScriptWithGlobalFallback(name, range, state) {
126
+ if (state.hostScriptBindings !== true) return void 0;
127
+ const binding = state.scriptScope?.get(name);
128
+ if (binding == null || !state.globalObjectBindingIds.has(binding.id)) return;
129
+ return {
130
+ binding,
131
+ fallback: {
132
+ name,
133
+ object: intrinsicGlobalObjectRead(range, state, name),
134
+ objectBindingId: intrinsicGlobalObjectBinding(state).id
135
+ }
136
+ };
137
+ }
138
+ /**
139
+ * Whether a host Script's simple `var` loop head writes through the
140
+ * persistent Global Environment Record. Its hoisted binding is the realm
141
+ * global object's property, but a later Script can declare a lexical
142
+ * binding over that property when it is configurable, so each PutValue
143
+ * resolves the name again, as an assignment head or a `var` pattern head
144
+ * already does. Intervening `with` records use the pattern/reference
145
+ * path, retaining their object-environment lookup before this fallback.
146
+ */
147
+ function hostScriptGlobalVarHead(name, scopes, state) {
148
+ if (state.hostScriptBindings !== true) return false;
149
+ const resolution = resolveName(scopes, state, name);
150
+ return resolution.binding == null && resolution.objectBindingIds.length === 0 && isGlobalObjectResolvedName(name, state);
151
+ }
152
+ /** Whether the mutable global object property owns this intrinsic value. */
153
+ function isPropertyOwnedIntrinsicName(name) {
154
+ return name === "AggregateError" || name === "Array" || name === "ArrayBuffer" || name === "AsyncDisposableStack" || name === "Atomics" || name === "BigInt" || name === "Boolean" || name === "DataView" || name === "Date" || name === "DisposableStack" || name === "Error" || name === "EvalError" || name === "Function" || name === "Infinity" || name === "Iterator" || name === "JSON" || name === "Map" || name === "Math" || name === "NaN" || name === "Number" || name === "Object" || name === "process" || name === "Promise" || name === "Proxy" || name === "Reflect" || name === "RegExp" || name === "RangeError" || name === "ReferenceError" || name === "String" || name === "SuppressedError" || name === "Symbol" || name === "SyntaxError" || name === "TypeError" || name === "URIError" || name === "decodeURI" || name === "decodeURIComponent" || name === "encodeURI" || name === "encodeURIComponent" || name === "escape" || name === "eval" || name === "globalThis" || name === "isFinite" || name === "isNaN" || name === "parseFloat" || name === "parseInt" || name === "Set" || name === "SharedArrayBuffer" || name === "FinalizationRegistry" || name === "WeakMap" || name === "WeakRef" || name === "WeakSet" || name === "undefined" || name === "unescape" || isTypedArrayIntrinsicName(name);
155
+ }
156
+ function agentHoleAt(identifier, state) {
157
+ const hole = state.agentHoles?.get(identifier.name);
158
+ if (hole == null || identifier.byteRange == null || identifier.byteRange.start !== hole.span.start || identifier.byteRange.end !== hole.span.end) return;
159
+ return hole;
160
+ }
161
+ /**
162
+ * Whether an unshadowed reference to `name` resolves through the realm's
163
+ * global object rather than through a compiled binding.
164
+ *
165
+ * A property-owned intrinsic always does: its value lives in the global
166
+ * object property the realm installed. Every other name that ECMA-262
167
+ * does not require a conforming realm to bind does too, because a
168
+ * program can create such a property at run time through `globalThis`,
169
+ * so no closed-world analysis can decide the reference statically. The
170
+ * two exclusions keep names this profile has not admitted as values
171
+ * reportable: a runtime-owned intrinsic is a call target with no global
172
+ * property behind it, and a standard global this realm does not install
173
+ * yet would answer an absent property where ECMA-262 requires a value.
174
+ * Under host Script bindings a Script's own `var` and function names do
175
+ * too, since their binding is the global object's property.
176
+ */
177
+ function isGlobalObjectResolvedName(name, state) {
178
+ const declared = state?.scriptScope?.get(name);
179
+ if (state?.hostScriptBindings === true && declared != null && state.globalObjectBindingIds.has(declared.id)) return true;
180
+ if (isPropertyOwnedIntrinsicName(name)) return true;
181
+ if (isRuntimeOwnedIntrinsicName(name)) return false;
182
+ return !isStandardGlobalName(name);
183
+ }
184
+ function bindingExpression(binding, range) {
185
+ return {
186
+ bindingId: binding.id,
187
+ kind: "binding",
188
+ name: binding.name,
189
+ range
190
+ };
191
+ }
192
+ /** The realm global object captured before the source body executes. */
193
+ function intrinsicGlobalObjectBinding(state) {
194
+ let binding = state.intrinsicGlobalObjectBinding;
195
+ if (binding == null) {
196
+ binding = {
197
+ id: state.nextBindingId,
198
+ mutable: false,
199
+ name: intrinsicGlobalObjectName
200
+ };
201
+ state.nextBindingId += 1;
202
+ state.intrinsicGlobalObjectBinding = binding;
203
+ }
204
+ return binding;
205
+ }
206
+ /**
207
+ * The hidden cell of the module's `import.meta` object. ECMA-262 2025
208
+ * 13.3.12.1 creates the object on the first evaluation and returns it
209
+ * from every later one. Oseo's closed module host contributes no
210
+ * HostGetImportMetaProperties entries and no HostFinalizeImportMeta
211
+ * step, so that creation runs no code and allocating the object before
212
+ * the module's first evaluation is indistinguishable from allocating it
213
+ * on demand.
214
+ */
215
+ function importMetaBinding(state) {
216
+ let binding = state.importMetaBinding;
217
+ if (binding == null) {
218
+ binding = {
219
+ id: state.nextBindingId,
220
+ mutable: false,
221
+ name: importMetaName
222
+ };
223
+ state.nextBindingId += 1;
224
+ state.importMetaBinding = binding;
225
+ }
226
+ return binding;
227
+ }
228
+ /**
229
+ * Create the `import.meta` object: an ordinary, extensible object whose
230
+ * [[Prototype]] is null and which has no own properties, because the
231
+ * closed module host defines no import.meta properties.
232
+ */
233
+ function importMetaInitializer(binding, range) {
234
+ return {
235
+ bindingId: binding.id,
236
+ hint: void 0,
237
+ initializer: {
238
+ kind: "object",
239
+ properties: [{
240
+ key: {
241
+ kind: "string",
242
+ range,
243
+ value: "__proto__"
244
+ },
245
+ kind: "definition",
246
+ prototypeSetter: true,
247
+ value: {
248
+ kind: "null",
249
+ range
250
+ }
251
+ }],
252
+ range
253
+ },
254
+ kind: "const",
255
+ name: binding.name,
256
+ range
257
+ };
258
+ }
259
+ /** Read the realm global object captured before the source body executes. */
260
+ function intrinsicGlobalObjectRead(range, state, name) {
261
+ if (state.hostScriptBindings === true && name != null) return {
262
+ kind: "global-environment",
263
+ name,
264
+ range
265
+ };
266
+ return bindingExpression(intrinsicGlobalObjectBinding(state), range);
267
+ }
268
+ /** Read one mutable intrinsic through the realm's global object property. */
269
+ function intrinsicGlobalPropertyRead(name, range, state) {
270
+ return {
271
+ key: {
272
+ kind: "string",
273
+ range,
274
+ value: name
275
+ },
276
+ kind: "property-get",
277
+ object: intrinsicGlobalObjectRead(range, state, name),
278
+ range
279
+ };
280
+ }
281
+ /** Test whether one mutable intrinsic still exists on the global object. */
282
+ function intrinsicGlobalPropertyExists(name, range, state) {
283
+ return {
284
+ kind: "binary",
285
+ left: {
286
+ kind: "string",
287
+ range,
288
+ value: name
289
+ },
290
+ operator: "in",
291
+ range,
292
+ right: intrinsicGlobalObjectRead(range, state, name)
293
+ };
294
+ }
295
+ /** Return the uninitialized fallback for one absent mutable intrinsic. */
296
+ function missingIntrinsicBinding(name, state) {
297
+ let fallback = state.intrinsicReadFallbacks.get(name);
298
+ if (fallback == null) {
299
+ fallback = {
300
+ id: state.nextBindingId,
301
+ mutable: false,
302
+ name: `*missing intrinsic:${name}*`
303
+ };
304
+ state.nextBindingId += 1;
305
+ state.intrinsicReadFallbacks.set(name, fallback);
306
+ }
307
+ return fallback;
308
+ }
309
+ /** Read the uninitialized fallback for one absent mutable intrinsic. */
310
+ function missingIntrinsicRead(name, range, state) {
311
+ return bindingExpression(missingIntrinsicBinding(name, state), range);
312
+ }
313
+ /**
314
+ * Mark one global-object property reference as an identifier reference,
315
+ * so MIR lowering performs the global Environment Record's own
316
+ * HasProperty tests around it. Only strict code needs the hidden cell
317
+ * whose read throws the missing binding's ReferenceError.
318
+ */
319
+ function intrinsicGlobalReference(name, state) {
320
+ const hostGlobal = state.hostScriptBindings === true ? { hostGlobal: true } : {};
321
+ if (!state.strict) return hostGlobal;
322
+ const fallback = missingIntrinsicBinding(name, state);
323
+ return {
324
+ ...hostGlobal,
325
+ strictFallback: {
326
+ bindingId: fallback.id,
327
+ name: fallback.name
328
+ }
329
+ };
330
+ }
331
+ /** Resolve one assignment-pattern leaf to a mutable intrinsic property. */
332
+ function intrinsicGlobalPatternTarget(name, located, state) {
333
+ const reference = intrinsicGlobalPropertyRead(name, located.range, state);
334
+ return {
335
+ ...includePropertiesWhen(() => {
336
+ if (located.byteRange == null) return void 0;
337
+ return { byteRange: located.byteRange };
338
+ }),
339
+ globalReference: intrinsicGlobalReference(name, state),
340
+ inferredName: name,
341
+ key: reference.key,
342
+ kind: "assignment-member",
343
+ object: reference.object,
344
+ range: located.range
345
+ };
346
+ }
347
+ /** Resolve one direct loop target to a mutable intrinsic property. */
348
+ function intrinsicGlobalLoopTarget(name, range, state) {
349
+ const reference = intrinsicGlobalPropertyRead(name, range, state);
350
+ return {
351
+ globalReference: intrinsicGlobalReference(name, state),
352
+ key: reference.key,
353
+ kind: "property",
354
+ object: reference.object,
355
+ range
356
+ };
357
+ }
358
+ /**
359
+ * Read a global-object name with ordinary identifier semantics:
360
+ * ResolveBinding's HasProperty, then GetBindingValue's own HasProperty
361
+ * and Get, which MIR lowering performs over one shared evaluation of the
362
+ * global object and the key. `unresolvable` answers an absent property at
363
+ * the first test; an identifier read reaches the hidden uninitialized
364
+ * cell, whose read throws ReferenceError instead of returning the
365
+ * `undefined` an ordinary property read would produce.
366
+ */
367
+ function intrinsicGlobalIdentifierRead(name, range, state, unresolvable = missingIntrinsicRead(name, range, state)) {
368
+ return {
369
+ globalReference: intrinsicGlobalReference(name, state),
370
+ kind: "global-read",
371
+ name,
372
+ object: intrinsicGlobalObjectRead(range, state, name),
373
+ range,
374
+ unresolvable
375
+ };
376
+ }
377
+ /**
378
+ * Build the statically owned fallback for a name read through `with`.
379
+ * An otherwise unresolved name receives an uninitialized hidden cell,
380
+ * preserving the ReferenceError when every object environment misses.
381
+ */
382
+ function identifierFallback(name, range, binding, state) {
383
+ if (binding != null) return bindingExpression(binding, range);
384
+ if (name === "undefined") return {
385
+ kind: "undefined",
386
+ range
387
+ };
388
+ if (name === "NaN" || name === "Infinity") return {
389
+ kind: "number",
390
+ range,
391
+ value: name === "NaN" ? NaN : Infinity
392
+ };
393
+ if (isGlobalObjectResolvedName(name, state)) return intrinsicGlobalIdentifierRead(name, range, state);
394
+ return bindingExpression(withFallbackBinding(name, state, false), range);
395
+ }
396
+ /** The source location of one syntax node, without its other fields. */
397
+ function locatedOf(value) {
398
+ return {
399
+ ...includePropertiesWhen(() => {
400
+ if (value.byteRange == null) return void 0;
401
+ return { byteRange: value.byteRange };
402
+ }),
403
+ range: value.range
404
+ };
405
+ }
406
+ function shadowedMethodTarget(binding, key, range) {
407
+ return {
408
+ key: {
409
+ kind: "string",
410
+ range,
411
+ value: key
412
+ },
413
+ kind: "method",
414
+ object: {
415
+ bindingId: binding.id,
416
+ kind: "binding",
417
+ name: binding.name,
418
+ range
419
+ }
420
+ };
421
+ }
422
+ /**
423
+ * Applies NamedEvaluation to an anonymous function or class expression.
424
+ * A class carries its name on the constructor closure, so an anonymous
425
+ * class takes the storage name the same way an anonymous function does.
426
+ */
427
+ function inferFunctionName(expression, name) {
428
+ if (expression.kind === "function" && expression.name === "") return {
429
+ ...expression,
430
+ name
431
+ };
432
+ if (expression.kind === "class" && expression.constructorFunction.kind === "function" && expression.constructorFunction.name === "") return {
433
+ ...expression,
434
+ constructorFunction: {
435
+ ...expression.constructorFunction,
436
+ name
437
+ }
438
+ };
439
+ return expression;
440
+ }
441
+ /**
442
+ * Resolves one private name against the enclosing class bodies. A class
443
+ * body binds every private name it declares in its own scope under the
444
+ * spelled `#name`, which no identifier can collide with, so an inner
445
+ * class shadows an outer declaration of the same name and a reference
446
+ * outside every declaring body reports an unresolved name.
447
+ */
448
+ function resolvePrivateName(name, located, scopes, state) {
449
+ const binding = findBinding(scopes, state, name);
450
+ if (binding == null) {
451
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, located, `Private name ${name} is not declared in an enclosing class body.`));
452
+ return;
453
+ }
454
+ return {
455
+ bindingId: binding.id,
456
+ name
457
+ };
458
+ }
459
+ function resolveCallArgument(argument, scopes, state) {
460
+ if (argument.kind === "spread") {
461
+ const resolved = resolveExpression(argument.argument, scopes, state);
462
+ return resolved == null ? void 0 : {
463
+ ...argument,
464
+ argument: resolved
465
+ };
466
+ }
467
+ return resolveExpression(argument, scopes, state);
468
+ }
469
+ /**
470
+ * Whether this call target reaches the unshadowed dynamic Function boundary.
471
+ */
472
+ function callsDynamicFunctionConstructor(target, scopes, state) {
473
+ const name = target.kind === "name" ? target.name : target.kind === "dynamic" && target.callee.kind === "identifier" ? target.callee.name : void 0;
474
+ if (name !== "Function") return false;
475
+ const resolution = resolveName(scopes, state, name, true);
476
+ return resolution.binding == null && resolution.objectBindingIds.length === 0;
477
+ }
478
+ /**
479
+ * Whether `$262` here reads the global object's property, which the host
480
+ * object occupies unless the program replaces it. A Script-level `var` or
481
+ * function binding is that property, but a lexical binding or an
482
+ * intervening `with` object may hold any value, so a `start` call through
483
+ * one is an ordinary call whose argument is not agent source and must not
484
+ * be compiled as an agent program.
485
+ */
486
+ function resolvesToHostGlobal(scopes, state) {
487
+ const resolution = resolveName(scopes, state, test262HostGlobalName);
488
+ return resolution.objectBindingIds.length === 0 && (resolution.binding == null || state.globalObjectBindingIds.has(resolution.binding.id));
489
+ }
490
+ /** Resolves one optional chain without inflating the recursive dispatcher. */
491
+ function resolveOptionalChain(expression, scopes, state) {
492
+ const base = resolveExpression(expression.base, scopes, state);
493
+ if (base == null) return void 0;
494
+ const links = [];
495
+ for (const link of expression.links) {
496
+ if (link.kind === "member") {
497
+ const key = resolveExpression(link.key, scopes, state);
498
+ if (key == null) return void 0;
499
+ links.push({
500
+ ...link,
501
+ key
502
+ });
503
+ continue;
504
+ }
505
+ if (link.kind === "private-member") {
506
+ const privateName = resolvePrivateName(link.name, link, scopes, state);
507
+ if (privateName == null) return void 0;
508
+ links.push({
509
+ ...locatedOf(link),
510
+ kind: "private-member",
511
+ optional: link.optional,
512
+ privateName
513
+ });
514
+ continue;
515
+ }
516
+ const argumentsValue = [];
517
+ for (const argument of link.arguments) {
518
+ const resolved = resolveCallArgument(argument, scopes, state);
519
+ if (resolved == null) return void 0;
520
+ argumentsValue.push(resolved);
521
+ }
522
+ links.push({
523
+ ...link,
524
+ arguments: argumentsValue
525
+ });
526
+ }
527
+ return {
528
+ ...expression,
529
+ base,
530
+ links
531
+ };
532
+ }
533
+ /** Whether identifier deletion needs an owned intrinsic diagnostic. */
534
+ function isRuntimeOwnedIntrinsicName(name) {
535
+ return name === "console" || name === "Array" || name === "ArrayBuffer" || name === "BigInt" || name === "Boolean" || name === "DataView" || name === "Date" || name === "Function" || name === "Iterator" || name === "Map" || name === "Number" || name === "Object" || name === "Promise" || name === "Proxy" || name === "RegExp" || name === "String" || name === "Set" || name === "SharedArrayBuffer" || name === "Symbol" || name === "FinalizationRegistry" || name === "WeakMap" || name === "WeakRef" || name === "WeakSet" || isTypedArrayIntrinsicName(name) || name === "setTimeout" || name === "clearTimeout" || errorIntrinsicName(name) != null;
536
+ }
537
+ /**
538
+ * Resolve an identifier delete without reading the selected binding. A
539
+ * declarative binding is decided statically, and every global-object
540
+ * name deletes the same property that its later identifier reads
541
+ * observe. The remaining constant `true` answers the standard globals
542
+ * this profile has not admitted as values: this realm binds no property
543
+ * for them, and deleting an unresolvable reference is true.
544
+ */
545
+ function resolveIdentifierDelete(expression, argument, scopes, state) {
546
+ const resolution = resolveName(scopes, state, argument.name);
547
+ const globalObjectBinding = resolution.binding != null && state.globalObjectBindingIds.has(resolution.binding.id);
548
+ let fallback;
549
+ if (globalObjectBinding) fallback = {
550
+ key: {
551
+ kind: "string",
552
+ range: argument.range,
553
+ value: argument.name
554
+ },
555
+ kind: "property-delete",
556
+ object: intrinsicGlobalObjectRead(argument.range, state, argument.name),
557
+ range: expression.range
558
+ };
559
+ else if (resolution.binding != null) fallback = {
560
+ kind: "boolean",
561
+ range: expression.range,
562
+ value: false
563
+ };
564
+ else if (argument.name === "undefined" || argument.name === "NaN" || argument.name === "Infinity") fallback = {
565
+ kind: "boolean",
566
+ range: expression.range,
567
+ value: false
568
+ };
569
+ else if (isGlobalObjectResolvedName(argument.name, state)) fallback = {
570
+ alternate: {
571
+ kind: "boolean",
572
+ range: expression.range,
573
+ value: true
574
+ },
575
+ consequent: {
576
+ key: {
577
+ kind: "string",
578
+ range: argument.range,
579
+ value: argument.name
580
+ },
581
+ kind: "property-delete",
582
+ object: intrinsicGlobalObjectRead(argument.range, state, argument.name),
583
+ range: expression.range
584
+ },
585
+ kind: "conditional",
586
+ range: expression.range,
587
+ test: intrinsicGlobalPropertyExists(argument.name, argument.range, state)
588
+ };
589
+ else if (isRuntimeOwnedIntrinsicName(argument.name)) {
590
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, argument, `Deleting runtime intrinsic binding '${argument.name}' is outside the admitted global-object profile.`));
591
+ return;
592
+ } else fallback = {
593
+ kind: "boolean",
594
+ range: expression.range,
595
+ value: true
596
+ };
597
+ if (resolution.objectBindingIds.length > 0) {
598
+ if (state.withFallbacks.some((owner) => owner.has(argument.name))) {
599
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, argument, `Deleting with fallback binding '${argument.name}' is outside the admitted global-object profile.`));
600
+ return;
601
+ }
602
+ return {
603
+ ...locatedOf(expression),
604
+ fallback,
605
+ kind: "with-delete",
606
+ name: argument.name,
607
+ objectBindingIds: resolution.objectBindingIds
608
+ };
609
+ }
610
+ return fallback;
611
+ }
612
+ /**
613
+ * Resolve a direct `typeof` applied to an identifier. ECMA-262 answers
614
+ * `"undefined"` for an unresolvable reference instead of throwing, which
615
+ * is what an ordinary property read of the realm global object already
616
+ * produces for an absent name, so every global-object-resolved name uses
617
+ * that read instead of the uninitialized cell an ordinary reference needs
618
+ * for its ReferenceError. Runtime-owned call-target intrinsics and the
619
+ * standard globals this profile has not admitted as values stay rejected:
620
+ * ECMA-262 resolves them to real global values, so answering `"undefined"`
621
+ * for them would misreport the realm.
622
+ */
623
+ function resolveTypeofIdentifier(expression, argument, scopes, state) {
624
+ const resolution = resolveName(scopes, state, argument.name);
625
+ if (resolution.binding != null && state.globalObjectBindingIds.has(resolution.binding.id)) {
626
+ const fallback = intrinsicGlobalPropertyRead(argument.name, argument.range, state);
627
+ const resolved = resolution.objectBindingIds.length === 0 ? fallback : {
628
+ fallback,
629
+ globalReference: intrinsicGlobalReference(argument.name, state),
630
+ kind: "with-get",
631
+ name: argument.name,
632
+ objectBindingIds: resolution.objectBindingIds,
633
+ range: argument.range
634
+ };
635
+ return {
636
+ ...expression,
637
+ argument: resolved
638
+ };
639
+ }
640
+ if (resolution.binding == null && resolution.objectBindingIds.length === 0 && agentHoleAt(argument, state) != null) {
641
+ const resolved = resolveExpression(argument, scopes, state);
642
+ return resolved == null ? void 0 : {
643
+ ...expression,
644
+ argument: resolved
645
+ };
646
+ }
647
+ if (isGlobalObjectResolvedName(argument.name, state) && resolution.binding == null) {
648
+ const fallback = intrinsicGlobalIdentifierRead(argument.name, argument.range, state, {
649
+ kind: "undefined",
650
+ range: argument.range
651
+ });
652
+ const resolved = resolution.objectBindingIds.length === 0 ? fallback : {
653
+ fallback,
654
+ globalReference: intrinsicGlobalReference(argument.name, state),
655
+ kind: "with-get",
656
+ name: argument.name,
657
+ objectBindingIds: resolution.objectBindingIds,
658
+ range: argument.range
659
+ };
660
+ return {
661
+ ...expression,
662
+ argument: resolved
663
+ };
664
+ }
665
+ if (resolution.binding != null || argument.name === "undefined" || argument.name === "NaN" || argument.name === "Infinity") {
666
+ const resolved = resolveExpression(argument, scopes, state);
667
+ return resolved == null ? void 0 : {
668
+ ...expression,
669
+ argument: resolved
670
+ };
671
+ }
672
+ if (isRuntimeOwnedIntrinsicName(argument.name)) {
673
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, argument, `typeof runtime intrinsic binding '${argument.name}' is outside the admitted global-object profile.`));
674
+ return;
675
+ }
676
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, argument, `typeof standard global binding '${argument.name}' is outside the admitted global-object profile.`));
677
+ }
678
+ function resolveExpression(expression, scopes, state) {
679
+ if (expression.kind === "invalid-assignment-target") {
680
+ const call = resolveExpression(expression.call, scopes, state);
681
+ if (call == null) return void 0;
682
+ const range = expression.range;
683
+ return {
684
+ ...includePropertiesWhen(() => {
685
+ if (expression.byteRange == null) return void 0;
686
+ return { byteRange: expression.byteRange };
687
+ }),
688
+ call,
689
+ error: {
690
+ arguments: [{
691
+ kind: "string",
692
+ range,
693
+ value: "Invalid assignment target."
694
+ }],
695
+ callee: {
696
+ errorName: "ReferenceError",
697
+ kind: "error-intrinsic",
698
+ range
699
+ },
700
+ kind: "new",
701
+ range
702
+ },
703
+ kind: "invalid-assignment-target",
704
+ range
705
+ };
706
+ }
707
+ if (expression.kind === "delete") {
708
+ if (expression.argument.kind === "identifier") return resolveIdentifierDelete(expression, expression.argument, scopes, state);
709
+ if (expression.argument.kind === "optional-chain") {
710
+ const chain = resolveOptionalChain(expression.argument, scopes, state);
711
+ return chain == null ? void 0 : {
712
+ ...chain,
713
+ delete: true
714
+ };
715
+ }
716
+ const argument = resolveExpression(expression.argument, scopes, state);
717
+ return argument == null ? void 0 : {
718
+ ...expression,
719
+ argument,
720
+ kind: "delete-value"
721
+ };
722
+ }
723
+ if (expression.kind === "binding-set" || expression.kind === "binding-update") {
724
+ const assignment = {
725
+ ...locatedOf(expression),
726
+ name: expression.name
727
+ };
728
+ const named = (assigned, name) => expression.parenthesizedTarget === true ? assigned : inferFunctionName(assigned, name);
729
+ const resolution = resolveName(scopes, state, expression.name);
730
+ const value = resolveExpression(expression.value, scopes, state);
731
+ if (isGlobalObjectResolvedName(expression.name, state) && resolution.binding == null && resolution.objectBindingIds.length === 0) {
732
+ if (value == null) return void 0;
733
+ const inferred = expression.kind === "binding-set" || expression.operator === "&&" || expression.operator === "??" || expression.operator === "||" ? named(value, expression.name) : value;
734
+ const reference = intrinsicGlobalPropertyRead(expression.name, expression.range, state);
735
+ const globalReference = intrinsicGlobalReference(expression.name, state);
736
+ const write = expression.kind === "binding-set" ? {
737
+ ...locatedOf(expression),
738
+ globalReference,
739
+ key: reference.key,
740
+ kind: "property-set",
741
+ object: reference.object,
742
+ value: inferred
743
+ } : {
744
+ ...locatedOf(expression),
745
+ globalReference,
746
+ key: reference.key,
747
+ kind: "property-update",
748
+ object: reference.object,
749
+ operator: expression.operator,
750
+ value: inferred
751
+ };
752
+ if (expression.kind === "binding-set") return write;
753
+ return {
754
+ alternate: missingIntrinsicRead(expression.name, expression.range, state),
755
+ consequent: write,
756
+ kind: "conditional",
757
+ range: expression.range,
758
+ test: intrinsicGlobalPropertyExists(expression.name, expression.range, state)
759
+ };
760
+ }
761
+ if (resolution.binding == null && resolution.objectBindingIds.length === 0) {
762
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, expression, `Unknown binding '${expression.name}'.`));
763
+ return;
764
+ }
765
+ if (value == null) return void 0;
766
+ if (resolution.binding == null && resolution.objectBindingIds.length > 0 && isGlobalObjectResolvedName(expression.name, state)) {
767
+ const fallback = {
768
+ name: expression.name,
769
+ objectBindingId: intrinsicGlobalObjectBinding(state).id,
770
+ ...includePropertiesWhen(() => state.hostScriptBindings !== true ? void 0 : { object: intrinsicGlobalObjectRead(expression.range, state, expression.name) })
771
+ };
772
+ if (expression.kind === "binding-set") return {
773
+ ...assignment,
774
+ fallback,
775
+ globalReference: intrinsicGlobalReference(expression.name, state),
776
+ kind: "with-set",
777
+ objectBindingIds: resolution.objectBindingIds,
778
+ value: named(value, expression.name)
779
+ };
780
+ return {
781
+ ...assignment,
782
+ fallback: {
783
+ ...fallback,
784
+ read: intrinsicGlobalIdentifierRead(expression.name, expression.range, state)
785
+ },
786
+ globalReference: intrinsicGlobalReference(expression.name, state),
787
+ kind: "with-update",
788
+ objectBindingIds: resolution.objectBindingIds,
789
+ operator: expression.operator,
790
+ value: expression.operator === "&&" || expression.operator === "??" || expression.operator === "||" ? named(value, expression.name) : value
791
+ };
792
+ }
793
+ if (resolution.binding == null && resolution.objectBindingIds.length > 0 && rejectGlobalObjectWithFallbackWrite(expression.name, expression, state)) return;
794
+ if (resolution.binding == null && state.strict && expression.kind === "binding-set") {
795
+ rejectStrictWithFallbackWrite(expression.name, expression, state);
796
+ return;
797
+ }
798
+ const binding = resolution.binding ?? withFallbackBinding(expression.name, state, expression.kind === "binding-set");
799
+ const inferred = expression.kind === "binding-set" || expression.operator === "&&" || expression.operator === "??" || expression.operator === "||" ? named(value, binding.name) : value;
800
+ if (resolution.objectBindingIds.length > 0) {
801
+ const fallback = {
802
+ bindingId: binding.id,
803
+ ...includePropertiesWhen(() => {
804
+ if (!(binding.functionNameBinding === true)) return void 0;
805
+ return { functionNameBinding: true };
806
+ }),
807
+ ...includePropertiesWhen(() => {
808
+ if (!(binding.importedBinding === true)) return void 0;
809
+ return { importedBinding: true };
810
+ }),
811
+ mutable: binding.mutable,
812
+ name: binding.name
813
+ };
814
+ if (expression.kind === "binding-set") return {
815
+ ...assignment,
816
+ fallback,
817
+ globalReference: intrinsicGlobalReference(expression.name, state),
818
+ kind: "with-set",
819
+ objectBindingIds: resolution.objectBindingIds,
820
+ value: inferred
821
+ };
822
+ return {
823
+ ...assignment,
824
+ fallback,
825
+ globalReference: intrinsicGlobalReference(expression.name, state),
826
+ kind: "with-update",
827
+ objectBindingIds: resolution.objectBindingIds,
828
+ operator: expression.operator,
829
+ value: inferred
830
+ };
831
+ }
832
+ return {
833
+ ...assignment,
834
+ ...expression.kind === "binding-set" ? { kind: expression.kind } : {
835
+ kind: expression.kind,
836
+ operator: expression.operator
837
+ },
838
+ bindingId: binding.id,
839
+ ...includePropertiesWhen(() => {
840
+ if (!(binding.functionNameBinding === true)) return void 0;
841
+ return { functionNameBinding: true };
842
+ }),
843
+ ...includePropertiesWhen(() => {
844
+ if (!(binding.importedBinding === true)) return void 0;
845
+ return { importedBinding: true };
846
+ }),
847
+ mutable: binding.mutable,
848
+ value: inferred
849
+ };
850
+ }
851
+ if (expression.kind === "binding-step") {
852
+ const resolution = resolveName(scopes, state, expression.name);
853
+ if (isGlobalObjectResolvedName(expression.name, state) && resolution.binding == null && resolution.objectBindingIds.length === 0) {
854
+ const reference = intrinsicGlobalPropertyRead(expression.name, expression.range, state);
855
+ const step = {
856
+ ...locatedOf(expression),
857
+ globalReference: intrinsicGlobalReference(expression.name, state),
858
+ key: reference.key,
859
+ kind: "property-step",
860
+ object: reference.object,
861
+ operator: expression.operator,
862
+ prefix: expression.prefix
863
+ };
864
+ return {
865
+ alternate: missingIntrinsicRead(expression.name, expression.range, state),
866
+ consequent: step,
867
+ kind: "conditional",
868
+ range: expression.range,
869
+ test: intrinsicGlobalPropertyExists(expression.name, expression.range, state)
870
+ };
871
+ }
872
+ if (resolution.binding == null && resolution.objectBindingIds.length === 0) {
873
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, expression, `Unknown binding '${expression.name}'.`));
874
+ return;
875
+ }
876
+ if (resolution.binding == null && resolution.objectBindingIds.length > 0 && isGlobalObjectResolvedName(expression.name, state)) return {
877
+ ...expression,
878
+ fallback: {
879
+ name: expression.name,
880
+ objectBindingId: intrinsicGlobalObjectBinding(state).id,
881
+ ...includePropertiesWhen(() => state.hostScriptBindings !== true ? void 0 : { object: intrinsicGlobalObjectRead(expression.range, state, expression.name) }),
882
+ read: intrinsicGlobalIdentifierRead(expression.name, expression.range, state)
883
+ },
884
+ globalReference: intrinsicGlobalReference(expression.name, state),
885
+ kind: "with-step",
886
+ objectBindingIds: resolution.objectBindingIds
887
+ };
888
+ if (resolution.binding == null && resolution.objectBindingIds.length > 0 && rejectGlobalObjectWithFallbackWrite(expression.name, expression, state)) return;
889
+ const binding = resolution.binding ?? withFallbackBinding(expression.name, state, false);
890
+ if (resolution.objectBindingIds.length > 0) return {
891
+ ...expression,
892
+ fallback: {
893
+ bindingId: binding.id,
894
+ ...includePropertiesWhen(() => {
895
+ if (!(binding.functionNameBinding === true)) return void 0;
896
+ return { functionNameBinding: true };
897
+ }),
898
+ ...includePropertiesWhen(() => {
899
+ if (!(binding.importedBinding === true)) return void 0;
900
+ return { importedBinding: true };
901
+ }),
902
+ mutable: binding.mutable,
903
+ name: binding.name
904
+ },
905
+ globalReference: intrinsicGlobalReference(expression.name, state),
906
+ kind: "with-step",
907
+ objectBindingIds: resolution.objectBindingIds
908
+ };
909
+ return {
910
+ ...expression,
911
+ bindingId: binding.id,
912
+ ...includePropertiesWhen(() => {
913
+ if (!(binding.functionNameBinding === true)) return void 0;
914
+ return { functionNameBinding: true };
915
+ }),
916
+ ...includePropertiesWhen(() => {
917
+ if (!(binding.importedBinding === true)) return void 0;
918
+ return { importedBinding: true };
919
+ }),
920
+ mutable: binding.mutable
921
+ };
922
+ }
923
+ if (expression.kind === "destructuring-set") {
924
+ const value = resolveExpression(expression.value, scopes, state);
925
+ const pattern = resolveBindingPattern(expression.pattern, scopes, state, "write", true);
926
+ return value == null || pattern == null ? void 0 : {
927
+ ...expression,
928
+ pattern,
929
+ value
930
+ };
931
+ }
932
+ if (expression.kind === "array") {
933
+ const elements = [];
934
+ for (const element of expression.elements) {
935
+ if (element == null) {
936
+ elements.push(void 0);
937
+ continue;
938
+ }
939
+ if (element.kind === "spread") {
940
+ const argument = resolveExpression(element.argument, scopes, state);
941
+ if (argument == null) return void 0;
942
+ elements.push({
943
+ ...element,
944
+ argument
945
+ });
946
+ continue;
947
+ }
948
+ const resolved = resolveExpression(element, scopes, state);
949
+ if (resolved == null) return void 0;
950
+ elements.push(resolved);
951
+ }
952
+ return {
953
+ ...expression,
954
+ elements
955
+ };
956
+ }
957
+ if (expression.kind === "template-object") return expression;
958
+ if (expression.kind === "await") {
959
+ const argument = resolveExpression(expression.argument, scopes, state);
960
+ return argument == null ? void 0 : {
961
+ ...expression,
962
+ argument
963
+ };
964
+ }
965
+ if (expression.kind === "yield") {
966
+ if (expression.argument == null) return {
967
+ ...includePropertiesWhen(() => {
968
+ if (expression.byteRange == null) return void 0;
969
+ return { byteRange: expression.byteRange };
970
+ }),
971
+ kind: "yield",
972
+ range: expression.range
973
+ };
974
+ const argument = resolveExpression(expression.argument, scopes, state);
975
+ return argument == null ? void 0 : {
976
+ ...expression,
977
+ argument
978
+ };
979
+ }
980
+ if (expression.kind === "super-base") {
981
+ const receiver = resolveExpression({
982
+ kind: "this",
983
+ range: expression.range,
984
+ thisMode: expression.thisMode
985
+ }, scopes, state);
986
+ return receiver == null ? void 0 : {
987
+ ...expression,
988
+ receiver
989
+ };
990
+ }
991
+ if (expression.kind === "this" && state.thisBinding != null) return {
992
+ bindingId: state.thisBinding.bindingId,
993
+ kind: "binding",
994
+ name: "this",
995
+ range: expression.range
996
+ };
997
+ if (expression.kind === "import-meta") {
998
+ if (!state.moduleCode) {
999
+ state.diagnostics.push({
1000
+ ...sourceDiagnostic(state.sourceId, expression, "import.meta is only valid in Module code."),
1001
+ code: "OSEO0001"
1002
+ });
1003
+ return;
1004
+ }
1005
+ return bindingExpression(importMetaBinding(state), expression.range);
1006
+ }
1007
+ if (expression.kind === "boolean" || expression.kind === "bigint" || expression.kind === "new-target" || expression.kind === "null" || expression.kind === "number" || expression.kind === "regexp" || expression.kind === "string" || expression.kind === "this" || expression.kind === "undefined") return expression;
1008
+ if (expression.kind === "function") return resolveFunctionExpression(expression.functionValue, scopes, state, expression);
1009
+ if (expression.kind === "identifier") {
1010
+ const resolution = resolveName(scopes, state, expression.name);
1011
+ if (resolution.objectBindingIds.length > 0) return {
1012
+ fallback: identifierFallback(expression.name, expression.range, resolution.binding, state),
1013
+ globalReference: intrinsicGlobalReference(expression.name, state),
1014
+ kind: "with-get",
1015
+ name: expression.name,
1016
+ objectBindingIds: resolution.objectBindingIds,
1017
+ range: expression.range
1018
+ };
1019
+ const binding = resolution.binding;
1020
+ if (binding == null) {
1021
+ if (expression.name === "undefined") return {
1022
+ kind: "undefined",
1023
+ range: expression.range
1024
+ };
1025
+ if (expression.name === "NaN" || expression.name === "Infinity") return {
1026
+ kind: "number",
1027
+ range: expression.range,
1028
+ value: expression.name === "NaN" ? NaN : Infinity
1029
+ };
1030
+ const hole = agentHoleAt(expression, state);
1031
+ if (hole != null) {
1032
+ hole.reads += 1;
1033
+ return {
1034
+ index: hole.index,
1035
+ kind: "agent-hole",
1036
+ range: expression.range
1037
+ };
1038
+ }
1039
+ if (isGlobalObjectResolvedName(expression.name, state)) return intrinsicGlobalIdentifierRead(expression.name, expression.range, state);
1040
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, expression, `Unknown binding '${expression.name}'.`));
1041
+ return;
1042
+ }
1043
+ return {
1044
+ bindingId: binding.id,
1045
+ kind: "binding",
1046
+ name: expression.name,
1047
+ range: expression.range
1048
+ };
1049
+ }
1050
+ if (expression.kind === "unary") {
1051
+ if (expression.operator === "typeof" && expression.argument.kind === "identifier") return resolveTypeofIdentifier(expression, expression.argument, scopes, state);
1052
+ const argument = resolveExpression(expression.argument, scopes, state);
1053
+ if (argument == null) return void 0;
1054
+ return {
1055
+ ...expression,
1056
+ argument
1057
+ };
1058
+ }
1059
+ if (expression.kind === "binary" || expression.kind === "logical") {
1060
+ const left = resolveExpression(expression.left, scopes, state);
1061
+ const right = resolveExpression(expression.right, scopes, state);
1062
+ if (left == null || right == null) return void 0;
1063
+ return {
1064
+ ...expression,
1065
+ left,
1066
+ right
1067
+ };
1068
+ }
1069
+ if (expression.kind === "conditional") {
1070
+ const test = resolveExpression(expression.test, scopes, state);
1071
+ const consequent = resolveExpression(expression.consequent, scopes, state);
1072
+ const alternate = resolveExpression(expression.alternate, scopes, state);
1073
+ if (test == null || consequent == null || alternate == null) return;
1074
+ return {
1075
+ ...expression,
1076
+ alternate,
1077
+ consequent,
1078
+ test
1079
+ };
1080
+ }
1081
+ if (expression.kind === "sequence") {
1082
+ const expressions = [];
1083
+ for (const element of expression.expressions) {
1084
+ const resolved = resolveExpression(element, scopes, state);
1085
+ if (resolved == null) return void 0;
1086
+ expressions.push(resolved);
1087
+ }
1088
+ return {
1089
+ ...expression,
1090
+ expressions
1091
+ };
1092
+ }
1093
+ if (expression.kind === "object") {
1094
+ let foundPrototypeSetter = false;
1095
+ for (const property of expression.properties) {
1096
+ if (property.kind !== "definition" || property.prototypeSetter !== true) continue;
1097
+ if (foundPrototypeSetter) {
1098
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, property.key, "An object literal cannot contain multiple prototype setters."));
1099
+ return;
1100
+ }
1101
+ foundPrototypeSetter = true;
1102
+ }
1103
+ const properties = [];
1104
+ for (const property of expression.properties) {
1105
+ if (property.kind === "spread") {
1106
+ const argument = resolveExpression(property.argument, scopes, state);
1107
+ if (argument == null) return void 0;
1108
+ properties.push({
1109
+ ...property,
1110
+ argument
1111
+ });
1112
+ continue;
1113
+ }
1114
+ const key = resolveExpression(property.key, scopes, state);
1115
+ const value = resolveExpression(property.value, scopes, state);
1116
+ if (key == null || value == null) return void 0;
1117
+ properties.push({
1118
+ ...includePropertiesWhen(() => {
1119
+ if (property.accessorKind == null) return void 0;
1120
+ return { accessorKind: property.accessorKind };
1121
+ }),
1122
+ key,
1123
+ kind: "definition",
1124
+ ...includePropertiesWhen(() => {
1125
+ if (!(property.prototypeSetter === true)) return void 0;
1126
+ return { prototypeSetter: true };
1127
+ }),
1128
+ value: key.kind === "string" && property.prototypeSetter !== true ? inferFunctionName(value, key.value) : value
1129
+ });
1130
+ }
1131
+ return {
1132
+ ...expression,
1133
+ properties
1134
+ };
1135
+ }
1136
+ if (expression.kind === "optional-chain") return resolveOptionalChain(expression, scopes, state);
1137
+ if (expression.kind === "class") {
1138
+ const outerStrict = state.strict;
1139
+ state.strict = true;
1140
+ const resolved = resolveClassExpression(expression, scopes, state);
1141
+ state.strict = outerStrict;
1142
+ return resolved;
1143
+ }
1144
+ if (expression.kind === "property-get" || expression.kind === "property-delete") {
1145
+ const object = resolveExpression(expression.object, scopes, state);
1146
+ const key = resolveExpression(expression.key, scopes, state);
1147
+ return object == null || key == null ? void 0 : {
1148
+ ...expression,
1149
+ key,
1150
+ object
1151
+ };
1152
+ }
1153
+ if (expression.kind === "property-set") {
1154
+ const object = resolveExpression(expression.object, scopes, state);
1155
+ const key = resolveExpression(expression.key, scopes, state);
1156
+ const value = resolveExpression(expression.value, scopes, state);
1157
+ if (object == null || key == null || value == null) return void 0;
1158
+ return {
1159
+ ...expression,
1160
+ key,
1161
+ object,
1162
+ value
1163
+ };
1164
+ }
1165
+ if (expression.kind === "property-update") {
1166
+ const object = resolveExpression(expression.object, scopes, state);
1167
+ const key = resolveExpression(expression.key, scopes, state);
1168
+ const value = resolveExpression(expression.value, scopes, state);
1169
+ if (object == null || key == null || value == null) return void 0;
1170
+ return {
1171
+ ...expression,
1172
+ key,
1173
+ object,
1174
+ value
1175
+ };
1176
+ }
1177
+ if (expression.kind === "property-step") {
1178
+ const object = resolveExpression(expression.object, scopes, state);
1179
+ const key = resolveExpression(expression.key, scopes, state);
1180
+ return object == null || key == null ? void 0 : {
1181
+ ...expression,
1182
+ key,
1183
+ object
1184
+ };
1185
+ }
1186
+ if (expression.kind === "private-get" || expression.kind === "private-in" || expression.kind === "private-set" || expression.kind === "private-step" || expression.kind === "private-update") {
1187
+ const located = locatedOf(expression);
1188
+ const object = resolveExpression(expression.object, scopes, state);
1189
+ const privateName = resolvePrivateName(expression.name, expression, scopes, state);
1190
+ if (object == null || privateName == null) return void 0;
1191
+ if (expression.kind === "private-get" || expression.kind === "private-in") return {
1192
+ ...located,
1193
+ kind: expression.kind,
1194
+ object,
1195
+ privateName
1196
+ };
1197
+ if (expression.kind === "private-step") return {
1198
+ ...located,
1199
+ kind: "private-step",
1200
+ object,
1201
+ operator: expression.operator,
1202
+ prefix: expression.prefix,
1203
+ privateName
1204
+ };
1205
+ const value = resolveExpression(expression.value, scopes, state);
1206
+ if (value == null) return void 0;
1207
+ return expression.kind === "private-set" ? {
1208
+ ...located,
1209
+ kind: "private-set",
1210
+ object,
1211
+ privateName,
1212
+ value
1213
+ } : {
1214
+ ...located,
1215
+ kind: "private-update",
1216
+ object,
1217
+ operator: expression.operator,
1218
+ privateName,
1219
+ value
1220
+ };
1221
+ }
1222
+ if (expression.kind === "new") {
1223
+ const functionResolution = expression.callee.kind === "identifier" && expression.callee.name === "Function" ? resolveName(scopes, state, "Function", true) : void 0;
1224
+ if (functionResolution != null && functionResolution.binding == null && functionResolution.objectBindingIds.length === 0) {
1225
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, expression, "The Function constructor requires dynamic source and is unsupported in M5."));
1226
+ return;
1227
+ }
1228
+ const argumentsValue = [];
1229
+ for (const argument of expression.arguments) {
1230
+ const resolved = resolveCallArgument(argument, scopes, state);
1231
+ if (resolved == null) return void 0;
1232
+ argumentsValue.push(resolved);
1233
+ }
1234
+ const callee = resolveExpression(expression.callee, scopes, state);
1235
+ return callee == null ? void 0 : {
1236
+ ...expression,
1237
+ arguments: argumentsValue,
1238
+ callee
1239
+ };
1240
+ }
1241
+ if (expression.kind === "import-call") {
1242
+ const start = expression.byteRange?.start;
1243
+ const target = start == null ? void 0 : state.importCallTargets?.get(start);
1244
+ if (target == null) {
1245
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, expression, importCallProfileMessage));
1246
+ return;
1247
+ }
1248
+ return {
1249
+ arguments: [{
1250
+ kind: "number",
1251
+ range: expression.range,
1252
+ value: target
1253
+ }],
1254
+ kind: "call",
1255
+ range: expression.range,
1256
+ target: { kind: "module-import" }
1257
+ };
1258
+ }
1259
+ if (callsDynamicFunctionConstructor(expression.target, scopes, state)) {
1260
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, expression, "The Function constructor requires dynamic source and is unsupported in M5."));
1261
+ return;
1262
+ }
1263
+ if (state.agentTemplates != null && isAgentStartTarget(expression.target) && resolvesToHostGlobal(scopes, state)) {
1264
+ const segments = agentTemplateSegments(expression.arguments[0]);
1265
+ const key = JSON.stringify(segments);
1266
+ if (segments != null && !state.agentTemplates.has(key)) state.agentTemplates.set(key, {
1267
+ range: expression.range,
1268
+ segments
1269
+ });
1270
+ }
1271
+ if (state.hostScriptSources != null && hostScriptCallTarget(expression.target, scopes, state)) {
1272
+ const segments = agentTemplateSegments(expression.arguments[0]);
1273
+ if (segments?.length === 1) state.hostScriptSources.add(segments[0]);
1274
+ }
1275
+ const argumentValues = [];
1276
+ for (const argument of expression.arguments) {
1277
+ const resolved = resolveCallArgument(argument, scopes, state);
1278
+ if (resolved == null) return void 0;
1279
+ argumentValues.push(resolved);
1280
+ }
1281
+ let target;
1282
+ if (expression.target.kind === "console-log") {
1283
+ const resolution = resolveName(scopes, state, "console", true);
1284
+ const binding = resolution.binding;
1285
+ target = binding == null ? state.hostScriptBindings === true ? {
1286
+ kind: "host-runtime-intrinsic",
1287
+ name: "console",
1288
+ objectBindingIds: resolution.objectBindingIds,
1289
+ reference: intrinsicGlobalIdentifierRead("console", expression.target.range, state)
1290
+ } : { kind: "console-log" } : shadowedMethodTarget(binding, "log", expression.target.range);
1291
+ if (binding != null && state.hostScriptBindings === true) {
1292
+ const object = resolveExpression({
1293
+ kind: "identifier",
1294
+ name: "console",
1295
+ range: expression.target.range
1296
+ }, scopes, state);
1297
+ if (object == null) return void 0;
1298
+ target = {
1299
+ kind: "method",
1300
+ key: {
1301
+ kind: "string",
1302
+ value: "log",
1303
+ range: expression.target.range
1304
+ },
1305
+ object
1306
+ };
1307
+ }
1308
+ } else if (expression.target.kind === "object-intrinsic") {
1309
+ const object = resolveExpression({
1310
+ kind: "identifier",
1311
+ name: "Object",
1312
+ range: expression.target.range
1313
+ }, scopes, state);
1314
+ if (object == null) return void 0;
1315
+ target = {
1316
+ key: {
1317
+ kind: "string",
1318
+ range: expression.target.range,
1319
+ value: expression.target.method
1320
+ },
1321
+ kind: "method",
1322
+ object
1323
+ };
1324
+ } else if (expression.target.kind === "promise-intrinsic-direct") target = {
1325
+ kind: "promise-intrinsic",
1326
+ method: expression.target.method
1327
+ };
1328
+ else if (expression.target.kind === "promise-intrinsic") {
1329
+ const object = resolveExpression({
1330
+ kind: "identifier",
1331
+ name: "Promise",
1332
+ range: expression.target.range
1333
+ }, scopes, state);
1334
+ if (object == null) return void 0;
1335
+ target = {
1336
+ key: {
1337
+ kind: "string",
1338
+ range: expression.target.range,
1339
+ value: expression.target.method
1340
+ },
1341
+ kind: "method",
1342
+ object
1343
+ };
1344
+ } else if (expression.target.kind === "timer-intrinsic") {
1345
+ const resolution = resolveName(scopes, state, expression.target.method, true);
1346
+ const binding = resolution.binding;
1347
+ target = binding == null ? state.hostScriptBindings === true ? {
1348
+ kind: "host-runtime-intrinsic",
1349
+ name: expression.target.method,
1350
+ objectBindingIds: resolution.objectBindingIds,
1351
+ reference: intrinsicGlobalIdentifierRead(expression.target.method, expression.target.range, state)
1352
+ } : {
1353
+ kind: "timer-intrinsic",
1354
+ method: expression.target.method
1355
+ } : {
1356
+ callee: bindingExpression(binding, expression.target.range),
1357
+ kind: "dynamic"
1358
+ };
1359
+ if (binding != null && state.hostScriptBindings === true) {
1360
+ const callee = resolveExpression({
1361
+ kind: "identifier",
1362
+ name: binding.name,
1363
+ range: expression.target.range
1364
+ }, scopes, state);
1365
+ if (callee == null) return void 0;
1366
+ target = {
1367
+ callee,
1368
+ kind: "dynamic"
1369
+ };
1370
+ }
1371
+ } else if (expression.target.kind === "name") {
1372
+ const callee = resolveExpression({
1373
+ kind: "identifier",
1374
+ name: expression.target.name,
1375
+ range: expression.target.range
1376
+ }, scopes, state);
1377
+ if (callee == null) return void 0;
1378
+ target = {
1379
+ callee,
1380
+ kind: "dynamic"
1381
+ };
1382
+ } else if (expression.target.kind === "dynamic") {
1383
+ const callee = resolveExpression(expression.target.callee, scopes, state);
1384
+ if (callee == null) return void 0;
1385
+ target = {
1386
+ callee,
1387
+ kind: "dynamic"
1388
+ };
1389
+ } else if (expression.target.kind === "super") {
1390
+ const thisBinding = state.thisBinding;
1391
+ if (thisBinding == null) {
1392
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, expression, "super() is only valid in a derived class constructor."));
1393
+ return;
1394
+ }
1395
+ target = {
1396
+ ...expression.target,
1397
+ kind: "super",
1398
+ thisBinding
1399
+ };
1400
+ } else if (expression.target.kind === "private-method") {
1401
+ const object = resolveExpression(expression.target.object, scopes, state);
1402
+ const privateName = resolvePrivateName(expression.target.name, expression.target, scopes, state);
1403
+ if (object == null || privateName == null) return void 0;
1404
+ target = {
1405
+ kind: "private-method",
1406
+ object,
1407
+ privateName
1408
+ };
1409
+ } else {
1410
+ const object = resolveExpression(expression.target.object, scopes, state);
1411
+ const key = resolveExpression(expression.target.key, scopes, state);
1412
+ if (object == null || key == null) return void 0;
1413
+ target = {
1414
+ key,
1415
+ kind: "method",
1416
+ object
1417
+ };
1418
+ }
1419
+ return {
1420
+ ...expression,
1421
+ arguments: argumentValues,
1422
+ target
1423
+ };
1424
+ }
1425
+ /** A lexical const alias is usable only without an intervening with record. */
1426
+ function hostScriptAlias(name, scopes, state) {
1427
+ const resolved = resolveName(scopes, state, name, true);
1428
+ return resolved.binding?.mutable === false && resolved.objectBindingIds.length === 0 && state.hostScriptAliases?.has(resolved.binding.id) === true;
1429
+ }
1430
+ /** Keep source discovery finite without treating unrelated calls as Scripts. */
1431
+ function hostScriptCallTarget(target, scopes, state) {
1432
+ if (target.kind === "property") return target.key.kind === "string" && target.key.value === "evalScript";
1433
+ if (target.kind === "name") return hostScriptAlias(target.name, scopes, state);
1434
+ return target.kind === "dynamic" && target.callee.kind === "identifier" && hostScriptAlias(target.callee.name, scopes, state);
1435
+ }
1436
+ /**
1437
+ * Record this scope's simple const aliases before resolving hoisted functions
1438
+ * that may capture them. The fixed point admits const-to-const chains; cells
1439
+ * still enforce initialization order, TDZ and assignment errors at runtime.
1440
+ * A property read is conservative just like a direct evalScript call: a
1441
+ * non-host receiver contributes only an unused, deferred source candidate.
1442
+ */
1443
+ function recordHostScriptAliases(statements, scope, scopes, state) {
1444
+ recordHostScriptAliasDeclarations(statements.flatMap((statement) => {
1445
+ if (statement.kind === "declaration-list") return statement.declarations.flatMap((declaration) => declaration.kind === "const" ? [declaration] : []);
1446
+ return statement.kind === "const" ? [statement] : [];
1447
+ }), scope, scopes, state);
1448
+ }
1449
+ /**
1450
+ * Record the simple const aliases of one lexical declaration position: a
1451
+ * statement list or a classic for statement head. Every position whose
1452
+ * const bindings the resolver creates must record them here, or a call
1453
+ * through the alias loses its literal Script source.
1454
+ */
1455
+ function recordHostScriptAliasDeclarations(declarations, scope, scopes, state) {
1456
+ const aliases = state.hostScriptAliases;
1457
+ if (aliases == null) return;
1458
+ let changed;
1459
+ do {
1460
+ changed = false;
1461
+ for (const declaration of declarations) {
1462
+ const binding = scope.get(declaration.name);
1463
+ if (binding == null || binding.mutable || aliases.has(binding.id)) continue;
1464
+ const value = declaration.initializer;
1465
+ if (!(value.kind === "property-get" && value.key.kind === "string" && value.key.value === "evalScript" || value.kind === "identifier" && hostScriptAlias(value.name, scopes, state))) continue;
1466
+ aliases.add(binding.id);
1467
+ changed = true;
1468
+ }
1469
+ } while (changed);
1470
+ }
1471
+ function syntaxBindingNames(pattern) {
1472
+ if (pattern.kind === "assignment-member" || pattern.kind === "assignment-private") return [];
1473
+ if (pattern.kind === "binding-identifier") return [pattern.name];
1474
+ if (pattern.kind === "object-binding-pattern") return [...pattern.properties.flatMap((property) => syntaxBindingNames(property.pattern)), ...pattern.rest == null ? [] : syntaxBindingNames(pattern.rest)];
1475
+ return [...pattern.elements.flatMap((element) => element == null ? [] : syntaxBindingNames(element.pattern)), ...pattern.rest == null ? [] : syntaxBindingNames(pattern.rest)];
1476
+ }
1477
+ /**
1478
+ * The declaration kind one declarator of a lexical declaration list
1479
+ * binds. A binding pattern names its kind directly, while an identifier
1480
+ * declarator is discriminated by its own statement kind.
1481
+ */
1482
+ function declaratorKind(declarator) {
1483
+ return declarator.kind === "binding-pattern" ? declarator.declarationKind : declarator.kind;
1484
+ }
1485
+ /**
1486
+ * True when a declaration list mixes `const` and `let` declarators. One
1487
+ * LetOrConst covers a whole BindingList, so no source can produce such a
1488
+ * list, and resolving one would have to invent a mutability for it.
1489
+ */
1490
+ function mixedDeclarationList(declarations) {
1491
+ const first = declarations[0];
1492
+ return first != null && declarations.some((declarator) => declaratorKind(declarator) !== declaratorKind(first));
1493
+ }
1494
+ /**
1495
+ * Predeclares every lexical, function, and pattern-declared name of one
1496
+ * statement list into `scope`.
1497
+ *
1498
+ * `lexicalScope` distinguishes a Block, CaseBlock, or module top level's
1499
+ * own LexicallyDeclaredNames from a Script or FunctionBody's top-level
1500
+ * declarations. ECMA-262 treats a Script or FunctionBody's top-level
1501
+ * function declaration as a hoistable, var-like declaration that any
1502
+ * later declaration of the same name freely replaces regardless of its
1503
+ * kind or the code's strictness, because its LexicallyDeclaredNames
1504
+ * excludes function and var bindings entirely. A Block, CaseBlock, or
1505
+ * module top level's FunctionDeclarations are LexicallyDeclaredNames
1506
+ * instead. Annex B.3.2.4 and B.3.2.5 exempt a duplicate entry there only
1507
+ * in non-strict code and only when every declaration of the name is an
1508
+ * ordinary function declaration; the last one initializes the shared
1509
+ * binding. Every other duplicate stays an early error, which keeps the
1510
+ * rule for strict code and modules.
1511
+ */
1512
+ function predeclareBindings(statements, scope, state, lexicalScope, reuseScope) {
1513
+ for (const statement of statements) {
1514
+ if (statement.kind === "declaration-list") {
1515
+ predeclareBindings(statement.declarations, scope, state, lexicalScope);
1516
+ continue;
1517
+ }
1518
+ if (statement.kind === "binding-pattern" && statement.mode === "declare" && statement.declarationKind !== "var") {
1519
+ for (const name of syntaxBindingNames(statement.pattern)) {
1520
+ const previous = scope.get(name);
1521
+ if (previous != null && previous.pendingDeclaration !== true) {
1522
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, `Duplicate declaration '${name}'.`));
1523
+ continue;
1524
+ }
1525
+ scope.set(name, {
1526
+ id: previous?.id ?? state.nextBindingId,
1527
+ mutable: statement.declarationKind === "let",
1528
+ name
1529
+ });
1530
+ if (previous == null) state.nextBindingId += 1;
1531
+ }
1532
+ continue;
1533
+ }
1534
+ if (statement.kind !== "const" && statement.kind !== "let" && statement.kind !== "function") continue;
1535
+ const name = statement.kind === "function" ? statement.bindingName ?? statement.name : statement.name;
1536
+ if (name == null) {
1537
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, "A function declaration requires a name."));
1538
+ continue;
1539
+ }
1540
+ const previous = scope.get(name);
1541
+ const duplicateFunctions = statement.kind === "function" && previous?.functionId != null;
1542
+ const webLegacyFunction = statement.kind === "function" && !state.strict && (statement.functionKind ?? "ordinary") === "ordinary";
1543
+ if (previous != null && previous.pendingDeclaration !== true && !(duplicateFunctions && !lexicalScope) && !(duplicateFunctions && webLegacyFunction && previous.webLegacyFunction === true)) {
1544
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, `Duplicate declaration '${name}'.`));
1545
+ continue;
1546
+ }
1547
+ if (statement.kind === "function") {
1548
+ const functionId = state.nextFunctionId;
1549
+ state.nextFunctionId += 1;
1550
+ const reusedFromOuterScope = previous == null ? reuseScope?.get(name) : void 0;
1551
+ const reused = previous ?? reusedFromOuterScope;
1552
+ const bindingId = reused?.id ?? state.nextBindingId;
1553
+ if (reused == null) state.nextBindingId += 1;
1554
+ const alreadyInitialized = reusedFromOuterScope != null || previous?.alreadyInitialized === true;
1555
+ scope.set(name, {
1556
+ ...includePropertiesWhen(() => {
1557
+ if (!alreadyInitialized) return void 0;
1558
+ return { alreadyInitialized: true };
1559
+ }),
1560
+ functionId,
1561
+ id: bindingId,
1562
+ mutable: true,
1563
+ name,
1564
+ ...includePropertiesWhen(() => {
1565
+ if (!webLegacyFunction) return void 0;
1566
+ return { webLegacyFunction: true };
1567
+ })
1568
+ });
1569
+ state.functionInfo.set(statement, {
1570
+ bindingId,
1571
+ id: functionId,
1572
+ ...includePropertiesWhen(() => {
1573
+ if (!alreadyInitialized) return void 0;
1574
+ return { alreadyInitialized: true };
1575
+ })
1576
+ });
1577
+ } else {
1578
+ scope.set(name, {
1579
+ id: previous?.id ?? state.nextBindingId,
1580
+ mutable: statement.kind === "let",
1581
+ name
1582
+ });
1583
+ if (previous == null) state.nextBindingId += 1;
1584
+ }
1585
+ }
1586
+ }
1587
+ /**
1588
+ * Builds the `function-init` HIR statement for every function declaration
1589
+ * directly in `statements` whose binding still owns `scope`, in source
1590
+ * order. A block runs these before any other statement in its body,
1591
+ * which is InstantiateFunctionObject inside its own
1592
+ * BlockDeclarationInstantiation; a switch CaseBlock shares one such list
1593
+ * across every clause instead of giving each clause its own.
1594
+ */
1595
+ function buildFunctionInits(statements, scope, scopes, state) {
1596
+ recordHostScriptAliases(statements, scope, scopes, state);
1597
+ const result = [];
1598
+ for (const statement of statements) {
1599
+ if (statement.kind !== "function" || statement.name == null) continue;
1600
+ const bindingName = statement.bindingName ?? statement.name;
1601
+ const info = state.functionInfo.get(statement);
1602
+ if (info == null) continue;
1603
+ if (scope.get(bindingName)?.functionId !== info.id) continue;
1604
+ const functionValue = resolveFunction(statement, scopes, state, info.id);
1605
+ result.push({
1606
+ ...includePropertiesWhen(() => {
1607
+ if (!(info.alreadyInitialized === true)) return void 0;
1608
+ return { alreadyInitialized: true };
1609
+ }),
1610
+ bindingId: info.bindingId ?? -1,
1611
+ functionId: info.id,
1612
+ functionKind: functionValue.functionKind,
1613
+ functionName: functionValue.name,
1614
+ functionLength: functionValue.functionLength,
1615
+ ...includePropertiesWhen(() => {
1616
+ if (functionValue.sourceText == null) return void 0;
1617
+ return { sourceText: functionValue.sourceText };
1618
+ }),
1619
+ kind: "function-init",
1620
+ name: bindingName,
1621
+ range: statement.range
1622
+ });
1623
+ }
1624
+ return result;
1625
+ }
1626
+ function resolveStatementList(statements, parentScopes, state, functionBody, existingLocal, lexicalScope = true, loopDepth = 0, breakDepth = 0) {
1627
+ const local = existingLocal ?? /* @__PURE__ */ new Map();
1628
+ if (existingLocal == null) predeclareBindings(statements, local, state, lexicalScope);
1629
+ const scopes = [...parentScopes, local];
1630
+ const result = [...buildFunctionInits(statements, local, scopes, state)];
1631
+ for (const statement of statements) {
1632
+ if (statement.kind === "function") continue;
1633
+ const resolved = resolveStatementItems(statement, scopes, state, functionBody, loopDepth, breakDepth);
1634
+ if (resolved != null) result.push(...resolved);
1635
+ }
1636
+ return result;
1637
+ }
1638
+ /**
1639
+ * Resolves one statement of a statement list, expanding a lexical
1640
+ * declaration list into its declarators.
1641
+ *
1642
+ * The declarators join the enclosing list rather than a nested block.
1643
+ * ECMAScript gives them the scope that contains the declaration, and a
1644
+ * block would reset their cells a second time and break the identity a
1645
+ * closure captured between the two resets.
1646
+ */
1647
+ function resolveStatementItems(statement, scopes, state, functionBody, loopDepth, breakDepth) {
1648
+ if (statement.kind !== "declaration-list") {
1649
+ const resolved = resolveStatement(statement, scopes, state, functionBody, loopDepth, breakDepth);
1650
+ return resolved == null ? void 0 : [resolved];
1651
+ }
1652
+ if (mixedDeclarationList(statement.declarations)) {
1653
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, "A lexical declaration list declares one kind of binding."));
1654
+ return;
1655
+ }
1656
+ const resolved = [];
1657
+ for (const declarator of statement.declarations) {
1658
+ const item = resolveStatement(declarator, scopes, state, functionBody, loopDepth, breakDepth);
1659
+ if (item == null) return void 0;
1660
+ resolved.push(item);
1661
+ }
1662
+ return resolved;
1663
+ }
1664
+ /**
1665
+ * True for a function that takes `this` and `new.target` from the
1666
+ * function that encloses it instead of from its own invocation.
1667
+ */
1668
+ function lexicalReceiver(functionValue) {
1669
+ return functionValue.functionKind === "arrow" || functionValue.functionKind === "async-arrow";
1670
+ }
1671
+ /**
1672
+ * True when ECMA-262 gives the function form its own `arguments` binding.
1673
+ * FunctionDeclarationInstantiation creates one for every function except
1674
+ * an arrow, whose `arguments` reference resolves lexically in the
1675
+ * enclosing function instead. Strictness selects the object's shape, not
1676
+ * whether the binding exists.
1677
+ */
1678
+ function admitsArgumentsObject(functionValue) {
1679
+ const kind = functionValue.functionKind ?? "ordinary";
1680
+ return kind !== "arrow" && kind !== "async-arrow";
1681
+ }
1682
+ function resolveFunction(functionValue, outerScopes, state, id, selfBinding, derivedThisBinding, sourceBody = true) {
1683
+ const outerThisBinding = state.thisBinding;
1684
+ if (derivedThisBinding != null) state.thisBinding = derivedThisBinding;
1685
+ else if (!lexicalReceiver(functionValue)) state.thisBinding = void 0;
1686
+ const outerStrict = state.strict;
1687
+ state.strict = functionValue.strict === true;
1688
+ const parameterScope = /* @__PURE__ */ new Map();
1689
+ const parameters = [];
1690
+ for (const parameter of functionValue.parameters) {
1691
+ let binding = parameterScope.get(parameter.name);
1692
+ if (binding == null) {
1693
+ binding = {
1694
+ id: state.nextBindingId,
1695
+ mutable: true,
1696
+ name: parameter.name
1697
+ };
1698
+ state.nextBindingId += 1;
1699
+ parameterScope.set(parameter.name, binding);
1700
+ }
1701
+ parameters.push({
1702
+ ...parameter,
1703
+ bindingId: binding.id
1704
+ });
1705
+ }
1706
+ let argumentsBinding;
1707
+ if (admitsArgumentsObject(functionValue) && functionValue.argumentsFormal !== true && !parameterScope.has("arguments")) {
1708
+ argumentsBinding = {
1709
+ id: state.nextBindingId,
1710
+ mutable: true,
1711
+ name: "arguments"
1712
+ };
1713
+ state.nextBindingId += 1;
1714
+ parameterScope.set("arguments", argumentsBinding);
1715
+ }
1716
+ const bodyScope = /* @__PURE__ */ new Map();
1717
+ predeclareBindings(functionValue.body, bodyScope, state, false, parameterScope);
1718
+ const outerLabels = state.labels.splice(0);
1719
+ const outerVarScope = state.varScope;
1720
+ state.varScope = bodyScope;
1721
+ const body = resolveStatementList(functionValue.body, [
1722
+ ...outerScopes,
1723
+ ...selfBinding == null ? [] : [/* @__PURE__ */ new Map([[selfBinding.name, selfBinding]])],
1724
+ parameterScope
1725
+ ], state, true, bodyScope);
1726
+ state.labels.push(...outerLabels);
1727
+ state.varScope = outerVarScope;
1728
+ state.thisBinding = outerThisBinding;
1729
+ state.strict = outerStrict;
1730
+ const bodyBindingIds = declaredHirBindingIds(body);
1731
+ if (argumentsBinding != null && !state.referencedBindingIds.has(argumentsBinding.id) && !bodyBindingIds.includes(argumentsBinding.id)) {
1732
+ parameterScope.delete("arguments");
1733
+ argumentsBinding = void 0;
1734
+ }
1735
+ const argumentsMapped = argumentsBinding != null && functionValue.strict !== true && functionValue.simpleParameterList === true;
1736
+ const functionKind = functionValue.functionKind ?? "ordinary";
1737
+ const tailCalls = sourceBody && functionValue.strict === true && (functionKind === "ordinary" || functionKind === "arrow" || functionKind === "method" || functionKind === "class");
1738
+ const resolved = {
1739
+ ...functionValue,
1740
+ ...includePropertiesWhen(() => {
1741
+ if (argumentsBinding == null) return void 0;
1742
+ return { argumentsBindingId: argumentsBinding.id };
1743
+ }),
1744
+ ...includePropertiesWhen(() => {
1745
+ if (!argumentsMapped) return void 0;
1746
+ return { argumentsMapped: true };
1747
+ }),
1748
+ body,
1749
+ ...includePropertiesWhen(() => {
1750
+ if (derivedThisBinding == null) return void 0;
1751
+ return { derivedThisBindingId: derivedThisBinding.bindingId };
1752
+ }),
1753
+ functionLength: functionValue.functionLength ?? parameters.length,
1754
+ functionKind,
1755
+ id,
1756
+ kind: "hir-function",
1757
+ localBindingIds: [.../* @__PURE__ */ new Set([
1758
+ ...Array.from(parameterScope.values(), (binding) => binding.id),
1759
+ ...selfBinding == null ? [] : [selfBinding.id],
1760
+ ...derivedThisBinding == null ? [] : [derivedThisBinding.bindingId],
1761
+ ...bodyBindingIds
1762
+ ])],
1763
+ name: functionValue.name ?? `<anonymous-${id}>`,
1764
+ parameters,
1765
+ ...includePropertiesWhen(() => {
1766
+ if (selfBinding == null) return void 0;
1767
+ return { selfBindingId: selfBinding.id };
1768
+ }),
1769
+ ...includePropertiesWhen(() => {
1770
+ if (!tailCalls) return void 0;
1771
+ return { tailCalls: true };
1772
+ })
1773
+ };
1774
+ state.hirFunctions.push(resolved);
1775
+ return resolved;
1776
+ }
1777
+ function hirBindingIdentifiers(pattern) {
1778
+ if (pattern.kind === "assignment-member" || pattern.kind === "assignment-private") return [];
1779
+ if (pattern.kind === "binding-identifier") return [pattern];
1780
+ if (pattern.kind === "object-binding-pattern") return [...pattern.properties.flatMap((property) => hirBindingIdentifiers(property.pattern)), ...pattern.rest == null ? [] : hirBindingIdentifiers(pattern.rest)];
1781
+ return [...pattern.elements.flatMap((element) => element == null ? [] : hirBindingIdentifiers(element.pattern)), ...pattern.rest == null ? [] : hirBindingIdentifiers(pattern.rest)];
1782
+ }
1783
+ function hirCallArgumentHasAwait(argument) {
1784
+ return hirExpressionHasAwait(argument.kind === "spread" ? argument.argument : argument);
1785
+ }
1786
+ /** Finds an await in an optional chain outside the recursive dispatcher. */
1787
+ function hirOptionalChainHasAwait(expression) {
1788
+ return hirExpressionHasAwait(expression.base) || expression.links.some((link) => link.kind === "member" ? hirExpressionHasAwait(link.key) : link.kind === "private-member" ? false : link.arguments.some(hirCallArgumentHasAwait));
1789
+ }
1790
+ function hirExpressionHasAwait(expression) {
1791
+ if (expression.kind === "await") return true;
1792
+ if (expression.kind === "delete-value") return hirExpressionHasAwait(expression.argument);
1793
+ if (expression.kind === "invalid-assignment-target") return hirExpressionHasAwait(expression.call);
1794
+ if (expression.kind === "binding-set" || expression.kind === "binding-update" || expression.kind === "with-set" || expression.kind === "with-update") return hirExpressionHasAwait(expression.value);
1795
+ if (expression.kind === "destructuring-set") return hirExpressionHasAwait(expression.value) || hirBindingPatternHasAwait(expression.pattern);
1796
+ if (expression.kind === "array") return expression.elements.some((element) => element != null && hirExpressionHasAwait(element.kind === "spread" ? element.argument : element));
1797
+ if (expression.kind === "binary") return hirExpressionHasAwait(expression.left) || hirExpressionHasAwait(expression.right);
1798
+ if (expression.kind === "call") return (expression.target.kind === "dynamic" ? hirExpressionHasAwait(expression.target.callee) : expression.target.kind === "method" ? hirExpressionHasAwait(expression.target.object) || hirExpressionHasAwait(expression.target.key) : expression.target.kind === "private-method" ? hirExpressionHasAwait(expression.target.object) : false) || expression.arguments.some(hirCallArgumentHasAwait);
1799
+ if (expression.kind === "new") return hirExpressionHasAwait(expression.callee) || expression.arguments.some(hirCallArgumentHasAwait);
1800
+ if (expression.kind === "object") return expression.properties.some((property) => property.kind === "spread" ? hirExpressionHasAwait(property.argument) : hirExpressionHasAwait(property.key) || hirExpressionHasAwait(property.value));
1801
+ if (expression.kind === "optional-chain") return hirOptionalChainHasAwait(expression);
1802
+ if (expression.kind === "class") return expression.heritage != null && hirExpressionHasAwait(expression.heritage) || expression.elements.some((element) => element.kind !== "static-block" && element.key.kind !== "private-name" && hirExpressionHasAwait(element.key));
1803
+ if (expression.kind === "property-delete" || expression.kind === "property-get" || expression.kind === "property-step") return hirExpressionHasAwait(expression.object) || hirExpressionHasAwait(expression.key);
1804
+ if (expression.kind === "property-set" || expression.kind === "property-update") return hirExpressionHasAwait(expression.object) || hirExpressionHasAwait(expression.key) || hirExpressionHasAwait(expression.value);
1805
+ if (expression.kind === "private-get" || expression.kind === "private-in" || expression.kind === "private-step") return hirExpressionHasAwait(expression.object);
1806
+ if (expression.kind === "private-set" || expression.kind === "private-update") return hirExpressionHasAwait(expression.object) || hirExpressionHasAwait(expression.value);
1807
+ if (expression.kind === "logical") return hirExpressionHasAwait(expression.left) || hirExpressionHasAwait(expression.right);
1808
+ if (expression.kind === "conditional") return hirExpressionHasAwait(expression.test) || hirExpressionHasAwait(expression.consequent) || hirExpressionHasAwait(expression.alternate);
1809
+ if (expression.kind === "sequence") return expression.expressions.some(hirExpressionHasAwait);
1810
+ return expression.kind === "unary" && hirExpressionHasAwait(expression.argument);
1811
+ }
1812
+ function hirBindingPatternHasAwait(pattern) {
1813
+ if (pattern.kind === "assignment-member") return hirExpressionHasAwait(pattern.object) || hirExpressionHasAwait(pattern.key);
1814
+ if (pattern.kind === "assignment-private") return hirExpressionHasAwait(pattern.object);
1815
+ if (pattern.kind === "binding-identifier") return false;
1816
+ if (pattern.kind === "object-binding-pattern") return pattern.properties.some((property) => hirExpressionHasAwait(property.key) || property.initializer != null && hirExpressionHasAwait(property.initializer) || hirBindingPatternHasAwait(property.pattern)) || pattern.rest != null && hirBindingPatternHasAwait(pattern.rest);
1817
+ return pattern.elements.some((element) => element != null && (element.initializer != null && hirExpressionHasAwait(element.initializer) || hirBindingPatternHasAwait(element.pattern))) || pattern.rest != null && hirBindingPatternHasAwait(pattern.rest);
1818
+ }
1819
+ function declaredHirBindingIds(statements) {
1820
+ const result = [];
1821
+ for (const statement of statements) if (statement.kind === "const" || statement.kind === "let" || statement.kind === "function-init") result.push(statement.bindingId);
1822
+ else if (statement.kind === "binding-pattern" && statement.mode === "declare" && statement.declarationKind !== "var") result.push(...hirBindingIdentifiers(statement.pattern).map((item) => item.bindingId));
1823
+ else if (statement.kind === "block") result.push(...declaredHirBindingIds(statement.body));
1824
+ else if (statement.kind === "if") {
1825
+ result.push(...declaredHirBindingIds([statement.consequent]));
1826
+ if (statement.alternate != null) result.push(...declaredHirBindingIds([statement.alternate]));
1827
+ } else if (statement.kind === "while" || statement.kind === "do-while" || statement.kind === "labeled" || statement.kind === "with") {
1828
+ if (statement.kind === "with") {
1829
+ result.push(statement.objectBindingId);
1830
+ result.push(...statement.fallbackBindings.map((binding) => binding.bindingId));
1831
+ }
1832
+ result.push(...declaredHirBindingIds([statement.body]));
1833
+ } else if (statement.kind === "for") {
1834
+ for (const declaration of statement.declarations ?? []) {
1835
+ if (declaration.declarationKind === "var") continue;
1836
+ if (declaration.kind === "binding") result.push(declaration.bindingId);
1837
+ else result.push(...hirBindingIdentifiers(declaration.pattern).map((item) => item.bindingId));
1838
+ }
1839
+ result.push(...declaredHirBindingIds([statement.body]));
1840
+ } else if (statement.kind === "for-of") {
1841
+ if (statement.target.kind === "declaration" && statement.target.declarationKind !== "var") result.push(statement.target.bindingId);
1842
+ else if (statement.target.kind === "pattern-declaration" && statement.target.declarationKind !== "var") result.push(...hirBindingIdentifiers(statement.target.pattern).map((item) => item.bindingId));
1843
+ result.push(...declaredHirBindingIds([statement.body]));
1844
+ } else if (statement.kind === "for-in") {
1845
+ if (statement.target.kind === "declaration" && statement.target.declarationKind !== "var") result.push(statement.target.bindingId);
1846
+ else if (statement.target.kind === "pattern-declaration" && statement.target.declarationKind !== "var") result.push(...hirBindingIdentifiers(statement.target.pattern).map((item) => item.bindingId));
1847
+ result.push(...declaredHirBindingIds([statement.body]));
1848
+ } else if (statement.kind === "switch") {
1849
+ result.push(...declaredHirBindingIds(statement.functionInits ?? []));
1850
+ for (const switchCase of statement.cases) result.push(...declaredHirBindingIds(switchCase.body));
1851
+ } else if (statement.kind === "try") {
1852
+ result.push(...declaredHirBindingIds([statement.block]));
1853
+ if (statement.handler != null) {
1854
+ if (statement.handler.pattern != null) result.push(...hirBindingIdentifiers(statement.handler.pattern).map((item) => item.bindingId));
1855
+ result.push(...declaredHirBindingIds([statement.handler.body]));
1856
+ }
1857
+ if (statement.finalizer != null) result.push(...declaredHirBindingIds([statement.finalizer]));
1858
+ }
1859
+ return result;
1860
+ }
1861
+ function resolveFunctionExpression(functionValue, scopes, state, expression) {
1862
+ const id = state.nextFunctionId;
1863
+ state.nextFunctionId += 1;
1864
+ state.functionInfo.set(functionValue, { id });
1865
+ const selfBinding = functionValue.name == null ? void 0 : {
1866
+ functionNameBinding: true,
1867
+ id: state.nextBindingId,
1868
+ mutable: false,
1869
+ name: functionValue.name
1870
+ };
1871
+ if (selfBinding != null) state.nextBindingId += 1;
1872
+ const resolved = resolveFunction(functionValue, scopes, state, id, selfBinding);
1873
+ return {
1874
+ functionId: id,
1875
+ functionKind: resolved.functionKind,
1876
+ functionLength: resolved.functionLength,
1877
+ kind: "function",
1878
+ name: expression.inferredName ?? functionValue.name ?? "",
1879
+ range: expression.range,
1880
+ ...includePropertiesWhen(() => {
1881
+ if (resolved.sourceText == null) return void 0;
1882
+ return { sourceText: resolved.sourceText };
1883
+ })
1884
+ };
1885
+ }
1886
+ /**
1887
+ * Resolves one class element key. A private name resolves to the class
1888
+ * binding that holds the private name value rather than to a property
1889
+ * key expression, so the element it names is recorded under an identity
1890
+ * no property observation can produce.
1891
+ */
1892
+ function resolveClassElementKey(key, classScopes, state) {
1893
+ if (key.kind !== "private-name") return resolveExpression(key, classScopes, state);
1894
+ const privateName = resolvePrivateName(key.name, key, classScopes, state);
1895
+ return privateName == null ? void 0 : {
1896
+ ...locatedOf(key),
1897
+ kind: "private-name",
1898
+ privateName
1899
+ };
1900
+ }
1901
+ /**
1902
+ * The name a class element key gives an anonymous definition it holds.
1903
+ * ECMA-262 names a private element after its declared `#name`, exactly
1904
+ * as a static string key names an ordinary one; every other key names
1905
+ * its definition only where the evaluated key value exists.
1906
+ */
1907
+ function classElementStaticName(key) {
1908
+ if (key.kind === "private-name") return key.privateName.name;
1909
+ return key.kind === "string" ? key.value : void 0;
1910
+ }
1911
+ /**
1912
+ * Resolves one field definition. The key belongs to the class body and
1913
+ * is evaluated where the element appears, while the initializer becomes
1914
+ * a separate function that runs once per instance, or once for the
1915
+ * whole class when the field is `static`. That function is built here
1916
+ * rather than resolved from a synthesized syntax function, because its
1917
+ * body is exactly one `return` of the initializer expression and it
1918
+ * declares nothing of its own.
1919
+ *
1920
+ * The initializer's scope is the class scope, so it never reaches the
1921
+ * constructor's parameters, and it provides its own receiver, so a
1922
+ * derived constructor's `this` binding stops at it and an arrow
1923
+ * function inside it captures the object being initialized.
1924
+ */
1925
+ function resolveClassField(element, classScopes, state) {
1926
+ const key = resolveClassElementKey(element.key, classScopes, state);
1927
+ if (key == null) return void 0;
1928
+ const located = locatedOf(element);
1929
+ const placement = element.staticPlacement === true ? { staticPlacement: true } : {};
1930
+ if (element.initializer == null) return {
1931
+ ...located,
1932
+ key,
1933
+ kind: "field",
1934
+ ...placement
1935
+ };
1936
+ const initializerId = state.nextFunctionId;
1937
+ state.nextFunctionId += 1;
1938
+ const outerThisBinding = state.thisBinding;
1939
+ state.thisBinding = void 0;
1940
+ const outerLabels = state.labels.splice(0);
1941
+ const resolved = resolveExpression(element.initializer, classScopes, state);
1942
+ state.labels.push(...outerLabels);
1943
+ state.thisBinding = outerThisBinding;
1944
+ if (resolved == null) return void 0;
1945
+ const range = element.initializer.range;
1946
+ const staticName = classElementStaticName(key);
1947
+ const named = staticName == null ? resolved : inferFunctionName(resolved, staticName);
1948
+ const keyNameBindingId = staticName != null || !anonymousDefinition(named) ? void 0 : state.nextBindingId;
1949
+ if (keyNameBindingId != null) state.nextBindingId += 1;
1950
+ state.hirFunctions.push({
1951
+ body: [{
1952
+ expression: named,
1953
+ kind: "return",
1954
+ range
1955
+ }],
1956
+ ...includePropertiesWhen(() => {
1957
+ if (keyNameBindingId == null) return void 0;
1958
+ return { fieldKeyBindingId: keyNameBindingId };
1959
+ }),
1960
+ functionKind: "method",
1961
+ functionLength: 0,
1962
+ id: initializerId,
1963
+ kind: "hir-function",
1964
+ localBindingIds: [],
1965
+ name: "",
1966
+ parameters: [],
1967
+ range,
1968
+ returnHints: [],
1969
+ strict: true
1970
+ });
1971
+ return {
1972
+ ...located,
1973
+ initializer: {
1974
+ functionId: initializerId,
1975
+ functionKind: "method",
1976
+ functionLength: 0,
1977
+ kind: "function",
1978
+ name: "",
1979
+ range
1980
+ },
1981
+ key,
1982
+ ...includePropertiesWhen(() => {
1983
+ if (keyNameBindingId == null) return void 0;
1984
+ return { keyNameBindingId };
1985
+ }),
1986
+ kind: "field",
1987
+ ...placement
1988
+ };
1989
+ }
1990
+ /**
1991
+ * Resolves one class expression inside its own lexical environment. A
1992
+ * named class binds its name immutably in that environment, so the
1993
+ * constructor and every method reach the class through a binding that
1994
+ * an outer assignment cannot replace. The constructor is resolved
1995
+ * without a function self-binding, because the class-scope binding
1996
+ * already covers the name it would provide.
1997
+ *
1998
+ * A derived class resolves its heritage operand in that same
1999
+ * environment, before anything else, and gives its constructor a `this`
2000
+ * binding that only `super()` initializes.
2001
+ */
2002
+ function resolveClassExpression(expression, scopes, state) {
2003
+ const classScope = /* @__PURE__ */ new Map();
2004
+ let nameBinding;
2005
+ if (expression.nameBinding != null) {
2006
+ nameBinding = {
2007
+ bindingId: state.nextBindingId,
2008
+ name: expression.nameBinding
2009
+ };
2010
+ state.nextBindingId += 1;
2011
+ classScope.set(nameBinding.name, {
2012
+ id: nameBinding.bindingId,
2013
+ mutable: false,
2014
+ name: nameBinding.name
2015
+ });
2016
+ }
2017
+ const classScopes = [...scopes, classScope];
2018
+ let heritage;
2019
+ if (expression.heritage != null) {
2020
+ heritage = resolveExpression(expression.heritage, classScopes, state);
2021
+ if (heritage == null) return void 0;
2022
+ }
2023
+ const privateNames = [];
2024
+ for (const element of expression.elements) {
2025
+ if (element.kind === "static-block") continue;
2026
+ if (element.key.kind !== "private-name") continue;
2027
+ if (classScope.has(element.key.name)) continue;
2028
+ const name = element.key.name;
2029
+ const bindingId = state.nextBindingId;
2030
+ state.nextBindingId += 1;
2031
+ classScope.set(name, {
2032
+ id: bindingId,
2033
+ mutable: false,
2034
+ name
2035
+ });
2036
+ privateNames.push({
2037
+ bindingId,
2038
+ name
2039
+ });
2040
+ }
2041
+ let thisBinding;
2042
+ if (expression.heritage != null) {
2043
+ thisBinding = { bindingId: state.nextBindingId };
2044
+ state.nextBindingId += 1;
2045
+ }
2046
+ const constructorId = state.nextFunctionId;
2047
+ state.nextFunctionId += 1;
2048
+ state.functionInfo.set(expression.constructorFunction, { id: constructorId });
2049
+ const resolvedConstructor = resolveFunction(expression.constructorFunction, classScopes, state, constructorId, void 0, thisBinding);
2050
+ const constructorFunction = {
2051
+ functionId: constructorId,
2052
+ functionKind: resolvedConstructor.functionKind,
2053
+ functionLength: resolvedConstructor.functionLength,
2054
+ kind: "function",
2055
+ name: expression.inferredName ?? expression.constructorFunction.name ?? "",
2056
+ range: expression.range,
2057
+ ...includePropertiesWhen(() => {
2058
+ if (resolvedConstructor.sourceText == null) return void 0;
2059
+ return { sourceText: resolvedConstructor.sourceText };
2060
+ })
2061
+ };
2062
+ const elements = [];
2063
+ for (const element of expression.elements) {
2064
+ if (element.kind === "field") {
2065
+ const field = resolveClassField(element, classScopes, state);
2066
+ if (field == null) return void 0;
2067
+ elements.push(field);
2068
+ continue;
2069
+ }
2070
+ if (element.kind === "static-block") {
2071
+ const blockId = state.nextFunctionId;
2072
+ state.nextFunctionId += 1;
2073
+ state.functionInfo.set(element.body, { id: blockId });
2074
+ const resolvedBlock = resolveFunction(element.body, classScopes, state, blockId, void 0, void 0, false);
2075
+ elements.push({
2076
+ ...element,
2077
+ body: {
2078
+ functionId: blockId,
2079
+ functionKind: resolvedBlock.functionKind,
2080
+ functionLength: resolvedBlock.functionLength,
2081
+ kind: "function",
2082
+ name: "",
2083
+ range: element.range,
2084
+ ...includePropertiesWhen(() => {
2085
+ if (resolvedBlock.sourceText == null) return void 0;
2086
+ return { sourceText: resolvedBlock.sourceText };
2087
+ })
2088
+ }
2089
+ });
2090
+ continue;
2091
+ }
2092
+ const key = resolveClassElementKey(element.key, classScopes, state);
2093
+ const methodId = state.nextFunctionId;
2094
+ state.nextFunctionId += 1;
2095
+ state.functionInfo.set(element.value, { id: methodId });
2096
+ const resolvedMethod = resolveFunction(element.value, classScopes, state, methodId);
2097
+ if (key == null) return void 0;
2098
+ const value = {
2099
+ functionId: methodId,
2100
+ functionKind: resolvedMethod.functionKind,
2101
+ functionLength: resolvedMethod.functionLength,
2102
+ kind: "function",
2103
+ name: element.value.name ?? "",
2104
+ range: element.range,
2105
+ ...includePropertiesWhen(() => {
2106
+ if (resolvedMethod.sourceText == null) return void 0;
2107
+ return { sourceText: resolvedMethod.sourceText };
2108
+ })
2109
+ };
2110
+ const staticName = classElementStaticName(key);
2111
+ elements.push({
2112
+ ...element,
2113
+ key,
2114
+ value: staticName == null ? value : inferFunctionName(value, staticName)
2115
+ });
2116
+ }
2117
+ const byteRange = expression.byteRange;
2118
+ return {
2119
+ ...includePropertiesWhen(() => {
2120
+ if (byteRange == null) return void 0;
2121
+ return { byteRange };
2122
+ }),
2123
+ constructorFunction,
2124
+ elements,
2125
+ ...includePropertiesWhen(() => {
2126
+ if (heritage == null) return void 0;
2127
+ return { heritage };
2128
+ }),
2129
+ kind: "class",
2130
+ ...includePropertiesWhen(() => {
2131
+ if (nameBinding == null) return void 0;
2132
+ return { nameBinding };
2133
+ }),
2134
+ ...includePropertiesWhen(() => {
2135
+ if (privateNames.length === 0) return void 0;
2136
+ return { privateNames };
2137
+ }),
2138
+ range: expression.range
2139
+ };
2140
+ }
2141
+ function resolveBindingPattern(pattern, scopes, state, mode, allowAssignmentTargets) {
2142
+ if (pattern.kind === "assignment-member") {
2143
+ if (!allowAssignmentTargets) {
2144
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, pattern, "Member targets are valid only in assignment patterns."));
2145
+ return;
2146
+ }
2147
+ const object = resolveExpression(pattern.object, scopes, state);
2148
+ const key = resolveExpression(pattern.key, scopes, state);
2149
+ return object == null || key == null ? void 0 : {
2150
+ ...pattern,
2151
+ key,
2152
+ object
2153
+ };
2154
+ }
2155
+ if (pattern.kind === "assignment-private") {
2156
+ if (!allowAssignmentTargets) {
2157
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, pattern, "Private targets are valid only in assignment patterns."));
2158
+ return;
2159
+ }
2160
+ const object = resolveExpression(pattern.object, scopes, state);
2161
+ const privateName = resolvePrivateName(pattern.name, pattern, scopes, state);
2162
+ return object == null || privateName == null ? void 0 : {
2163
+ ...locatedOf(pattern),
2164
+ kind: "assignment-private",
2165
+ object,
2166
+ privateName
2167
+ };
2168
+ }
2169
+ if (pattern.kind === "binding-identifier") {
2170
+ const resolution = mode === "declare" ? {
2171
+ binding: scopes.at(-1)?.get(pattern.name),
2172
+ objectBindingIds: []
2173
+ } : resolveName(scopes, state, pattern.name);
2174
+ if (mode === "write" && isGlobalObjectResolvedName(pattern.name, state) && resolution.binding == null && resolution.objectBindingIds.length === 0) return intrinsicGlobalPatternTarget(pattern.name, pattern, state);
2175
+ const hostGlobal = mode === "write" && resolution.binding == null && resolution.objectBindingIds.length > 0 ? hostScriptWithGlobalFallback(pattern.name, pattern.range, state) : void 0;
2176
+ if (hostGlobal == null && resolution.binding == null && resolution.objectBindingIds.length > 0 && rejectGlobalObjectWithFallbackWrite(pattern.name, pattern, state)) return;
2177
+ if (hostGlobal == null && resolution.binding == null && resolution.objectBindingIds.length > 0 && state.strict) {
2178
+ rejectStrictWithFallbackWrite(pattern.name, pattern, state);
2179
+ return;
2180
+ }
2181
+ const binding = resolution.binding ?? hostGlobal?.binding ?? (resolution.objectBindingIds.length === 0 ? void 0 : withFallbackBinding(pattern.name, state, true));
2182
+ if (binding == null) {
2183
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, pattern, `Unknown binding '${pattern.name}'.`));
2184
+ return;
2185
+ }
2186
+ return {
2187
+ ...pattern,
2188
+ bindingId: binding.id,
2189
+ ...includePropertiesWhen(() => {
2190
+ if (!(binding.functionNameBinding === true)) return void 0;
2191
+ return { functionNameBinding: true };
2192
+ }),
2193
+ ...includePropertiesWhen(() => {
2194
+ if (!(binding.importedBinding === true)) return void 0;
2195
+ return { importedBinding: true };
2196
+ }),
2197
+ mutable: binding.mutable,
2198
+ ...includePropertiesWhen(() => {
2199
+ if (resolution.objectBindingIds.length === 0) return void 0;
2200
+ return {
2201
+ withObjectBindingIds: resolution.objectBindingIds,
2202
+ ...includePropertiesWhen(() => hostGlobal == null ? void 0 : { withGlobalFallback: hostGlobal.fallback }),
2203
+ ...includePropertiesWhen(() => {
2204
+ if (!state.strict && hostGlobal == null) return void 0;
2205
+ return { globalReference: intrinsicGlobalReference(pattern.name, state) };
2206
+ })
2207
+ };
2208
+ })
2209
+ };
2210
+ }
2211
+ if (pattern.kind === "object-binding-pattern") {
2212
+ const properties = [];
2213
+ for (const property of pattern.properties) {
2214
+ const key = resolveExpression(property.key, scopes, state);
2215
+ const resolvedPattern = resolveBindingPattern(property.pattern, scopes, state, mode, allowAssignmentTargets);
2216
+ let initializer = property.initializer == null ? void 0 : resolveExpression(property.initializer, scopes, state);
2217
+ const inferredName = property.parenthesizedTarget === true ? void 0 : resolvedPattern?.kind === "binding-identifier" ? resolvedPattern.name : resolvedPattern?.kind === "assignment-member" ? resolvedPattern.inferredName : void 0;
2218
+ if (initializer != null && inferredName != null) initializer = inferFunctionName(initializer, inferredName);
2219
+ if (key == null || resolvedPattern == null || property.initializer != null && initializer == null) return;
2220
+ properties.push({
2221
+ ...includePropertiesWhen(() => {
2222
+ if (property.byteRange == null) return void 0;
2223
+ return { byteRange: property.byteRange };
2224
+ }),
2225
+ ...includePropertiesWhen(() => {
2226
+ if (initializer == null) return void 0;
2227
+ return { initializer };
2228
+ }),
2229
+ key,
2230
+ pattern: resolvedPattern,
2231
+ range: property.range
2232
+ });
2233
+ }
2234
+ let rest;
2235
+ if (pattern.rest != null) {
2236
+ const resolvedRest = resolveBindingPattern(pattern.rest, scopes, state, mode, allowAssignmentTargets);
2237
+ if (resolvedRest?.kind !== "binding-identifier" && resolvedRest?.kind !== "assignment-member" && resolvedRest?.kind !== "assignment-private") return;
2238
+ rest = resolvedRest;
2239
+ }
2240
+ return {
2241
+ ...includePropertiesWhen(() => {
2242
+ if (pattern.byteRange == null) return void 0;
2243
+ return { byteRange: pattern.byteRange };
2244
+ }),
2245
+ kind: "object-binding-pattern",
2246
+ properties,
2247
+ ...includePropertiesWhen(() => {
2248
+ if (rest == null) return void 0;
2249
+ return { rest };
2250
+ }),
2251
+ range: pattern.range
2252
+ };
2253
+ }
2254
+ const elements = [];
2255
+ for (const element of pattern.elements) {
2256
+ if (element == null) {
2257
+ elements.push(void 0);
2258
+ continue;
2259
+ }
2260
+ const resolvedPattern = resolveBindingPattern(element.pattern, scopes, state, mode, allowAssignmentTargets);
2261
+ let initializer = element.initializer == null ? void 0 : resolveExpression(element.initializer, scopes, state);
2262
+ const inferredName = element.parenthesizedTarget === true ? void 0 : resolvedPattern?.kind === "binding-identifier" ? resolvedPattern.name : resolvedPattern?.kind === "assignment-member" ? resolvedPattern.inferredName : void 0;
2263
+ if (initializer != null && inferredName != null) initializer = inferFunctionName(initializer, inferredName);
2264
+ if (resolvedPattern == null || element.initializer != null && initializer == null) return;
2265
+ elements.push({
2266
+ ...includePropertiesWhen(() => {
2267
+ if (element.byteRange == null) return void 0;
2268
+ return { byteRange: element.byteRange };
2269
+ }),
2270
+ ...includePropertiesWhen(() => {
2271
+ if (initializer == null) return void 0;
2272
+ return { initializer };
2273
+ }),
2274
+ pattern: resolvedPattern,
2275
+ range: element.range
2276
+ });
2277
+ }
2278
+ const rest = pattern.rest == null ? void 0 : resolveBindingPattern(pattern.rest, scopes, state, mode, allowAssignmentTargets);
2279
+ if (pattern.rest != null && rest == null) return void 0;
2280
+ return {
2281
+ ...includePropertiesWhen(() => {
2282
+ if (pattern.byteRange == null) return void 0;
2283
+ return { byteRange: pattern.byteRange };
2284
+ }),
2285
+ elements,
2286
+ kind: "array-binding-pattern",
2287
+ ...includePropertiesWhen(() => {
2288
+ if (rest == null) return void 0;
2289
+ return { rest };
2290
+ }),
2291
+ range: pattern.range
2292
+ };
2293
+ }
2294
+ /**
2295
+ * Resolve a for-in head target against the scopes outside the head's own
2296
+ * lexical environment.
2297
+ *
2298
+ * ForIn/OfBodyEvaluation evaluates an assignment or `var` head reference
2299
+ * once per iteration in the surrounding environment, so only a `let` or
2300
+ * `const` head sees the fresh binding `declaredBinding` names or the
2301
+ * `headScopes` a lexical pattern head declares. The subject expression is
2302
+ * resolved by the caller, because the head environment exists while it
2303
+ * runs and these references do not.
2304
+ */
2305
+ function resolveForInTarget(target, scopes, headScopes, declaredBinding, state) {
2306
+ if (target.kind === "pattern-declaration") {
2307
+ const lexical = target.declarationKind !== "var";
2308
+ const pattern = resolveBindingPattern(target.pattern, lexical ? headScopes : scopes, state, lexical ? "declare" : "write", false);
2309
+ return pattern == null ? void 0 : {
2310
+ ...target,
2311
+ pattern
2312
+ };
2313
+ }
2314
+ if (target.kind === "assignment-pattern") {
2315
+ const pattern = resolveBindingPattern(target.pattern, scopes, state, "write", true);
2316
+ return pattern == null ? void 0 : {
2317
+ ...target,
2318
+ pattern
2319
+ };
2320
+ }
2321
+ if (target.kind === "declaration") {
2322
+ if (target.declarationKind === "var" && resolveName(scopes, state, target.name).objectBindingIds.length > 0) return resolveForInTarget({
2323
+ kind: "binding",
2324
+ name: target.name,
2325
+ range: target.range
2326
+ }, scopes, headScopes, void 0, state);
2327
+ if (declaredBinding == null && hostScriptGlobalVarHead(target.name, scopes, state)) return intrinsicGlobalLoopTarget(target.name, target.range, state);
2328
+ const binding = declaredBinding ?? findBinding(scopes, state, target.name);
2329
+ if (binding == null) {
2330
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, target, `Unknown binding '${target.name}'.`));
2331
+ return;
2332
+ }
2333
+ return {
2334
+ ...target,
2335
+ bindingId: binding.id,
2336
+ mutable: binding.mutable
2337
+ };
2338
+ }
2339
+ if (target.kind === "binding") {
2340
+ const resolution = resolveName(scopes, state, target.name);
2341
+ if (isGlobalObjectResolvedName(target.name, state) && resolution.binding == null && resolution.objectBindingIds.length === 0) return intrinsicGlobalLoopTarget(target.name, target.range, state);
2342
+ const hostGlobal = resolution.binding == null && resolution.objectBindingIds.length > 0 ? hostScriptWithGlobalFallback(target.name, target.range, state) : void 0;
2343
+ if (hostGlobal == null && resolution.binding == null && resolution.objectBindingIds.length > 0 && rejectGlobalObjectWithFallbackWrite(target.name, target, state)) return;
2344
+ if (hostGlobal == null && resolution.binding == null && resolution.objectBindingIds.length > 0 && state.strict) {
2345
+ rejectStrictWithFallbackWrite(target.name, target, state);
2346
+ return;
2347
+ }
2348
+ const binding = resolution.binding ?? hostGlobal?.binding ?? (resolution.objectBindingIds.length === 0 ? void 0 : withFallbackBinding(target.name, state, true));
2349
+ if (binding == null) {
2350
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, target, `Unknown binding '${target.name}'.`));
2351
+ return;
2352
+ }
2353
+ return {
2354
+ ...target,
2355
+ bindingId: binding.id,
2356
+ ...includePropertiesWhen(() => {
2357
+ if (!(binding.functionNameBinding === true)) return void 0;
2358
+ return { functionNameBinding: true };
2359
+ }),
2360
+ ...includePropertiesWhen(() => {
2361
+ if (!(binding.importedBinding === true)) return void 0;
2362
+ return { importedBinding: true };
2363
+ }),
2364
+ mutable: binding.mutable,
2365
+ ...includePropertiesWhen(() => {
2366
+ if (resolution.objectBindingIds.length === 0) return void 0;
2367
+ return {
2368
+ withObjectBindingIds: resolution.objectBindingIds,
2369
+ ...includePropertiesWhen(() => hostGlobal == null ? void 0 : { withGlobalFallback: hostGlobal.fallback }),
2370
+ ...includePropertiesWhen(() => {
2371
+ if (!state.strict && hostGlobal == null) return void 0;
2372
+ return { globalReference: intrinsicGlobalReference(target.name, state) };
2373
+ })
2374
+ };
2375
+ })
2376
+ };
2377
+ }
2378
+ if (target.kind === "property") {
2379
+ const object = resolveExpression(target.object, scopes, state);
2380
+ const key = resolveExpression(target.key, scopes, state);
2381
+ return object == null || key == null ? void 0 : {
2382
+ ...target,
2383
+ key,
2384
+ object
2385
+ };
2386
+ }
2387
+ const object = resolveExpression(target.object, scopes, state);
2388
+ const privateName = resolvePrivateName(target.name, target, scopes, state);
2389
+ return object == null || privateName == null ? void 0 : {
2390
+ ...locatedOf(target),
2391
+ kind: "private",
2392
+ object,
2393
+ privateName
2394
+ };
2395
+ }
2396
+ /**
2397
+ * Resolve the Annex B.3.2 evaluation of a qualifying block-level
2398
+ * function declaration. The value comes from the binding of the Block or
2399
+ * CaseBlock that directly contains the declaration, which is the
2400
+ * innermost scope here. The write goes to the var-scoped binding the
2401
+ * frontend hoisted, found in the var scope itself rather than through
2402
+ * name resolution: B.3.2 writes the VariableEnvironment directly, past
2403
+ * any catch parameter or `with` object between the two. A global
2404
+ * binding's write takes the global object's SetMutableBinding path.
2405
+ */
2406
+ function resolveAnnexBFunctionCopy(statement, scopes, state) {
2407
+ const source = scopes.at(-1)?.get(statement.name);
2408
+ const target = state.varScope?.get(statement.name);
2409
+ if (source?.functionId == null || target == null) throw new Error(`Annex B function copy '${statement.name}' has no block or var binding.`);
2410
+ state.referencedBindingIds.add(source.id);
2411
+ state.referencedBindingIds.add(target.id);
2412
+ return {
2413
+ ...includePropertiesWhen(() => {
2414
+ if (statement.byteRange == null) return void 0;
2415
+ return { byteRange: statement.byteRange };
2416
+ }),
2417
+ emptyCompletion: true,
2418
+ expression: {
2419
+ bindingId: target.id,
2420
+ kind: "binding-set",
2421
+ mutable: true,
2422
+ name: statement.name,
2423
+ range: statement.range,
2424
+ value: bindingExpression(source, statement.range)
2425
+ },
2426
+ kind: "expression",
2427
+ range: statement.range
2428
+ };
2429
+ }
2430
+ function resolveStatement(statement, scopes, state, functionBody, loopDepth = 0, breakDepth = 0) {
2431
+ if (statement.kind === "declaration-list") {
2432
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, "A lexical declaration list requires a statement list."));
2433
+ return;
2434
+ }
2435
+ if (statement.kind === "binding-pattern") {
2436
+ const initializer = resolveExpression(statement.initializer, scopes, state);
2437
+ const pattern = resolveBindingPattern(statement.pattern, scopes, state, statement.mode, false);
2438
+ return initializer == null || pattern == null ? void 0 : {
2439
+ ...statement,
2440
+ initializer,
2441
+ pattern
2442
+ };
2443
+ }
2444
+ if (statement.kind === "binding-init" || statement.kind === "const" || statement.kind === "let") {
2445
+ const initializer = resolveExpression(statement.initializer, scopes, state);
2446
+ const binding = statement.kind === "binding-init" ? findBinding(scopes, state, statement.name) : scopes.at(-1)?.get(statement.name);
2447
+ if (binding == null || initializer == null) return void 0;
2448
+ return {
2449
+ ...statement,
2450
+ bindingId: binding.id,
2451
+ initializer: inferFunctionName(initializer, binding.name)
2452
+ };
2453
+ }
2454
+ if (statement.kind === "expression") {
2455
+ const expression = resolveExpression(statement.expression, scopes, state);
2456
+ return expression == null ? void 0 : {
2457
+ ...statement,
2458
+ expression
2459
+ };
2460
+ }
2461
+ if (statement.kind === "return") {
2462
+ if (!functionBody) {
2463
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, "A return statement is only valid inside a function."));
2464
+ return;
2465
+ }
2466
+ const expression = statement.expression == null ? void 0 : resolveExpression(statement.expression, scopes, state);
2467
+ if (statement.expression != null && expression == null) return void 0;
2468
+ return {
2469
+ ...statement,
2470
+ expression
2471
+ };
2472
+ }
2473
+ if (statement.kind === "throw") {
2474
+ const expression = resolveExpression(statement.expression, scopes, state);
2475
+ return expression == null ? void 0 : {
2476
+ ...statement,
2477
+ expression
2478
+ };
2479
+ }
2480
+ if (statement.kind === "try") {
2481
+ const block = resolveStatement(statement.block, scopes, state, functionBody, loopDepth, breakDepth);
2482
+ let handler;
2483
+ if (statement.handler != null) {
2484
+ if (statement.handler.body == null) {
2485
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, "A catch clause requires a block body."));
2486
+ return;
2487
+ }
2488
+ const syntaxPattern = statement.handler.pattern;
2489
+ const catchScope = syntaxPattern == null ? void 0 : /* @__PURE__ */ new Map();
2490
+ if (syntaxPattern != null && catchScope != null) for (const name of syntaxBindingNames(syntaxPattern)) {
2491
+ if (catchScope.has(name)) {
2492
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, syntaxPattern, `Duplicate catch binding '${name}'.`));
2493
+ return;
2494
+ }
2495
+ catchScope.set(name, {
2496
+ id: state.nextBindingId,
2497
+ mutable: true,
2498
+ name
2499
+ });
2500
+ state.nextBindingId += 1;
2501
+ }
2502
+ const handlerScopes = catchScope == null ? scopes : [...scopes, catchScope];
2503
+ const pattern = syntaxPattern == null ? void 0 : resolveBindingPattern(syntaxPattern, handlerScopes, state, "declare", false);
2504
+ if (syntaxPattern != null && pattern == null) return void 0;
2505
+ const body = resolveStatement(statement.handler.body, handlerScopes, state, functionBody, loopDepth, breakDepth);
2506
+ if (body == null) return void 0;
2507
+ handler = {
2508
+ body,
2509
+ pattern,
2510
+ range: statement.handler.range
2511
+ };
2512
+ }
2513
+ const finalizer = statement.finalizer == null ? void 0 : resolveStatement(statement.finalizer, scopes, state, functionBody, loopDepth, breakDepth);
2514
+ if (block == null || statement.finalizer != null && finalizer == null) return;
2515
+ return {
2516
+ ...statement,
2517
+ block,
2518
+ finalizer,
2519
+ handler
2520
+ };
2521
+ }
2522
+ if (statement.kind === "break" || statement.kind === "continue") {
2523
+ if (statement.label != null) {
2524
+ const label = state.labels.findLast((entry) => entry.name === statement.label);
2525
+ if (!(label != null && (statement.kind === "break" || label.loop))) {
2526
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, label == null ? `Undefined label '${statement.label}'.` : `A continue label must reference an enclosing loop.`));
2527
+ return;
2528
+ }
2529
+ return statement;
2530
+ }
2531
+ if (!(statement.kind === "break" ? breakDepth > 0 : loopDepth > 0)) {
2532
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, statement.kind === "break" ? "A break statement requires an enclosing loop or switch." : "A continue statement requires an enclosing loop."));
2533
+ return;
2534
+ }
2535
+ return statement;
2536
+ }
2537
+ if (statement.kind === "block") {
2538
+ if (statement.parameterEnvironmentBody === true) {
2539
+ const local = /* @__PURE__ */ new Map();
2540
+ predeclareBindings(statement.body, local, state, false);
2541
+ const outerVarScope = state.varScope;
2542
+ state.varScope = local;
2543
+ const body = resolveStatementList(statement.body, scopes, state, functionBody, local, false, loopDepth, breakDepth);
2544
+ state.varScope = outerVarScope;
2545
+ return {
2546
+ ...statement,
2547
+ body
2548
+ };
2549
+ }
2550
+ return {
2551
+ ...statement,
2552
+ body: resolveStatementList(statement.body, scopes, state, functionBody, void 0, true, loopDepth, breakDepth)
2553
+ };
2554
+ }
2555
+ if (statement.kind === "annexb-function-copy") return resolveAnnexBFunctionCopy(statement, scopes, state);
2556
+ if (statement.kind === "while") {
2557
+ const test = resolveExpression(statement.test, scopes, state);
2558
+ const body = resolveStatement(statement.body, scopes, state, functionBody, loopDepth + 1, breakDepth + 1);
2559
+ return test == null || body == null ? void 0 : {
2560
+ ...statement,
2561
+ body,
2562
+ test
2563
+ };
2564
+ }
2565
+ if (statement.kind === "do-while") {
2566
+ const body = resolveStatement(statement.body, scopes, state, functionBody, loopDepth + 1, breakDepth + 1);
2567
+ const test = resolveExpression(statement.test, scopes, state);
2568
+ return body == null || test == null ? void 0 : {
2569
+ ...statement,
2570
+ body,
2571
+ test
2572
+ };
2573
+ }
2574
+ if (statement.kind === "labeled") {
2575
+ if (state.labels.some((entry) => entry.name === statement.label)) {
2576
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, `Duplicate label '${statement.label}'.`));
2577
+ return;
2578
+ }
2579
+ let terminal = statement.body;
2580
+ while (terminal.kind === "labeled") terminal = terminal.body;
2581
+ const loop = terminal.kind === "while" || terminal.kind === "do-while" || terminal.kind === "for" || terminal.kind === "for-in" || terminal.kind === "for-of";
2582
+ state.labels.push({
2583
+ loop,
2584
+ name: statement.label
2585
+ });
2586
+ const body = resolveStatement(statement.body, scopes, state, functionBody, loopDepth, breakDepth);
2587
+ state.labels.pop();
2588
+ return body == null ? void 0 : {
2589
+ ...statement,
2590
+ body
2591
+ };
2592
+ }
2593
+ if (statement.kind === "for") {
2594
+ const forScope = /* @__PURE__ */ new Map();
2595
+ const forScopes = [...scopes, forScope];
2596
+ let declarations;
2597
+ let init;
2598
+ if (statement.declarations != null) {
2599
+ for (const declaration of statement.declarations) {
2600
+ if (declaration.declarationKind === "var") continue;
2601
+ const names = declaration.kind === "binding" ? [declaration.name] : syntaxBindingNames(declaration.pattern);
2602
+ for (const name of names) {
2603
+ if (forScope.has(name)) {
2604
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, `Duplicate declaration '${name}'.`));
2605
+ return;
2606
+ }
2607
+ forScope.set(name, {
2608
+ id: state.nextBindingId,
2609
+ mutable: declaration.declarationKind === "let",
2610
+ name
2611
+ });
2612
+ state.nextBindingId += 1;
2613
+ }
2614
+ }
2615
+ recordHostScriptAliasDeclarations(statement.declarations.flatMap((declaration) => declaration.declarationKind === "const" && declaration.kind === "binding" ? [declaration] : []), forScope, forScopes, state);
2616
+ declarations = [];
2617
+ for (const declaration of statement.declarations) {
2618
+ const declarationScopes = declaration.declarationKind === "var" ? scopes : forScopes;
2619
+ const initializer = resolveExpression(declaration.initializer, declarationScopes, state);
2620
+ if (initializer == null) return void 0;
2621
+ if (declaration.kind === "binding" && declaration.declarationKind === "var" && (hostScriptGlobalVarHead(declaration.name, scopes, state) || resolveName(scopes, state, declaration.name).objectBindingIds.length > 0)) {
2622
+ const pattern = resolveBindingPattern({
2623
+ hints: declaration.hint == null ? [] : [declaration.hint],
2624
+ kind: "binding-identifier",
2625
+ name: declaration.name,
2626
+ range: declaration.range
2627
+ }, scopes, state, "write", false);
2628
+ if (pattern == null) return void 0;
2629
+ declarations.push({
2630
+ declarationKind: "var",
2631
+ initializer: inferFunctionName(initializer, declaration.name),
2632
+ kind: "pattern",
2633
+ pattern,
2634
+ range: declaration.range
2635
+ });
2636
+ continue;
2637
+ }
2638
+ if (declaration.kind === "binding") {
2639
+ const binding = declaration.declarationKind === "var" ? findBinding(scopes, state, declaration.name) : forScope.get(declaration.name);
2640
+ if (binding == null) return void 0;
2641
+ declarations.push({
2642
+ bindingId: binding.id,
2643
+ declarationKind: declaration.declarationKind,
2644
+ ...includePropertiesWhen(() => {
2645
+ if (declaration.disposal == null) return void 0;
2646
+ return { disposal: declaration.disposal };
2647
+ }),
2648
+ hint: declaration.hint,
2649
+ initializer: inferFunctionName(initializer, declaration.name),
2650
+ kind: "binding",
2651
+ name: declaration.name,
2652
+ range: declaration.range
2653
+ });
2654
+ continue;
2655
+ }
2656
+ const pattern = resolveBindingPattern(declaration.pattern, declarationScopes, state, declaration.declarationKind === "var" ? "write" : "declare", false);
2657
+ if (pattern == null) return void 0;
2658
+ declarations.push({
2659
+ declarationKind: declaration.declarationKind,
2660
+ initializer,
2661
+ kind: "pattern",
2662
+ pattern,
2663
+ range: declaration.range
2664
+ });
2665
+ }
2666
+ } else if (statement.init != null) {
2667
+ init = resolveExpression(statement.init, scopes, state);
2668
+ if (init == null) return void 0;
2669
+ }
2670
+ const test = statement.test == null ? void 0 : resolveExpression(statement.test, forScopes, state);
2671
+ if (statement.test != null && test == null) return void 0;
2672
+ const update = statement.update == null ? void 0 : resolveExpression(statement.update, forScopes, state);
2673
+ if (statement.update != null && update == null) return void 0;
2674
+ const body = resolveStatement(statement.body, forScopes, state, functionBody, loopDepth + 1, breakDepth + 1);
2675
+ if (body == null) return void 0;
2676
+ return {
2677
+ ...includePropertiesWhen(() => {
2678
+ if (statement.byteRange == null) return void 0;
2679
+ return { byteRange: statement.byteRange };
2680
+ }),
2681
+ body,
2682
+ ...includePropertiesWhen(() => {
2683
+ if (declarations == null) return void 0;
2684
+ return { declarations };
2685
+ }),
2686
+ ...includePropertiesWhen(() => {
2687
+ if (init == null) return void 0;
2688
+ return { init };
2689
+ }),
2690
+ kind: "for",
2691
+ range: statement.range,
2692
+ ...includePropertiesWhen(() => {
2693
+ if (test == null) return void 0;
2694
+ return { test };
2695
+ }),
2696
+ ...includePropertiesWhen(() => {
2697
+ if (update == null) return void 0;
2698
+ return { update };
2699
+ })
2700
+ };
2701
+ }
2702
+ if (statement.kind === "for-in") {
2703
+ let forScopes = scopes;
2704
+ let declaredBinding;
2705
+ if (statement.target.kind === "declaration" && statement.target.declarationKind !== "var") {
2706
+ declaredBinding = {
2707
+ id: state.nextBindingId,
2708
+ mutable: statement.target.declarationKind === "let",
2709
+ name: statement.target.name
2710
+ };
2711
+ state.nextBindingId += 1;
2712
+ forScopes = [...scopes, /* @__PURE__ */ new Map([[statement.target.name, declaredBinding]])];
2713
+ } else if (statement.target.kind === "pattern-declaration" && statement.target.declarationKind !== "var") {
2714
+ const patternScope = /* @__PURE__ */ new Map();
2715
+ for (const name of syntaxBindingNames(statement.target.pattern)) {
2716
+ if (patternScope.has(name)) {
2717
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement.target.pattern, `Duplicate for-in binding '${name}'.`));
2718
+ return;
2719
+ }
2720
+ patternScope.set(name, {
2721
+ id: state.nextBindingId,
2722
+ mutable: statement.target.declarationKind === "let",
2723
+ name
2724
+ });
2725
+ state.nextBindingId += 1;
2726
+ }
2727
+ forScopes = [...scopes, patternScope];
2728
+ }
2729
+ const subject = resolveExpression(statement.subject, forScopes, state);
2730
+ const target = resolveForInTarget(statement.target, scopes, forScopes, declaredBinding, state);
2731
+ const body = resolveStatement(statement.body, forScopes, state, functionBody, loopDepth + 1, breakDepth + 1);
2732
+ if (subject == null || target == null || body == null) return void 0;
2733
+ return {
2734
+ ...statement,
2735
+ body,
2736
+ subject,
2737
+ target
2738
+ };
2739
+ }
2740
+ if (statement.kind === "for-of") {
2741
+ let forScopes = scopes;
2742
+ let declaredBinding;
2743
+ let patternScope;
2744
+ if (statement.target.kind === "declaration" && statement.target.declarationKind !== "var") {
2745
+ declaredBinding = {
2746
+ id: state.nextBindingId,
2747
+ mutable: statement.target.declarationKind === "let",
2748
+ name: statement.target.name
2749
+ };
2750
+ state.nextBindingId += 1;
2751
+ forScopes = [...scopes, /* @__PURE__ */ new Map([[statement.target.name, declaredBinding]])];
2752
+ } else if (statement.target.kind === "pattern-declaration" && statement.target.declarationKind !== "var") {
2753
+ patternScope = /* @__PURE__ */ new Map();
2754
+ for (const name of syntaxBindingNames(statement.target.pattern)) {
2755
+ if (patternScope.has(name)) {
2756
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement.target.pattern, `Duplicate for-of binding '${name}'.`));
2757
+ return;
2758
+ }
2759
+ patternScope.set(name, {
2760
+ id: state.nextBindingId,
2761
+ mutable: statement.target.declarationKind === "let",
2762
+ name
2763
+ });
2764
+ state.nextBindingId += 1;
2765
+ }
2766
+ forScopes = [...scopes, patternScope];
2767
+ }
2768
+ const iterable = resolveExpression(statement.iterable, forScopes, state);
2769
+ let target;
2770
+ if (statement.target.kind === "declaration" && declaredBinding == null && (hostScriptGlobalVarHead(statement.target.name, scopes, state) || resolveName(scopes, state, statement.target.name).objectBindingIds.length > 0)) target = resolveForInTarget(statement.target, scopes, forScopes, void 0, state);
2771
+ else if (statement.target.kind === "declaration") {
2772
+ const binding = declaredBinding ?? findBinding(scopes, state, statement.target.name);
2773
+ if (binding == null) state.diagnostics.push(sourceDiagnostic(state.sourceId, statement.target, `Unknown binding '${statement.target.name}'.`));
2774
+ else target = {
2775
+ ...statement.target,
2776
+ bindingId: binding.id,
2777
+ mutable: binding.mutable
2778
+ };
2779
+ } else if (statement.target.kind === "pattern-declaration") {
2780
+ const lexical = statement.target.declarationKind !== "var";
2781
+ const pattern = resolveBindingPattern(statement.target.pattern, lexical ? forScopes : scopes, state, lexical ? "declare" : "write", false);
2782
+ if (pattern != null) target = {
2783
+ ...statement.target,
2784
+ pattern
2785
+ };
2786
+ } else if (statement.target.kind === "assignment-pattern") {
2787
+ const pattern = resolveBindingPattern(statement.target.pattern, scopes, state, "write", true);
2788
+ if (pattern != null) target = {
2789
+ ...statement.target,
2790
+ pattern
2791
+ };
2792
+ } else if (statement.target.kind === "binding") {
2793
+ const resolution = resolveName(scopes, state, statement.target.name);
2794
+ const hostGlobal = resolution.binding == null && resolution.objectBindingIds.length > 0 ? hostScriptWithGlobalFallback(statement.target.name, statement.target.range, state) : void 0;
2795
+ if (isGlobalObjectResolvedName(statement.target.name, state) && resolution.binding == null && resolution.objectBindingIds.length === 0) target = intrinsicGlobalLoopTarget(statement.target.name, statement.target.range, state);
2796
+ else if (hostGlobal == null && resolution.binding == null && resolution.objectBindingIds.length > 0 && rejectGlobalObjectWithFallbackWrite(statement.target.name, statement.target, state)) return;
2797
+ else if (hostGlobal == null && resolution.binding == null && resolution.objectBindingIds.length > 0 && state.strict) {
2798
+ rejectStrictWithFallbackWrite(statement.target.name, statement.target, state);
2799
+ return;
2800
+ } else {
2801
+ const binding = resolution.binding ?? hostGlobal?.binding ?? (resolution.objectBindingIds.length === 0 ? void 0 : withFallbackBinding(statement.target.name, state, true));
2802
+ if (binding == null) state.diagnostics.push(sourceDiagnostic(state.sourceId, statement.target, `Unknown binding '${statement.target.name}'.`));
2803
+ else target = {
2804
+ ...statement.target,
2805
+ bindingId: binding.id,
2806
+ ...includePropertiesWhen(() => {
2807
+ if (!(binding.functionNameBinding === true)) return void 0;
2808
+ return { functionNameBinding: true };
2809
+ }),
2810
+ ...includePropertiesWhen(() => {
2811
+ if (!(binding.importedBinding === true)) return void 0;
2812
+ return { importedBinding: true };
2813
+ }),
2814
+ mutable: binding.mutable,
2815
+ ...includePropertiesWhen(() => {
2816
+ if (resolution.objectBindingIds.length === 0) return void 0;
2817
+ return {
2818
+ withObjectBindingIds: resolution.objectBindingIds,
2819
+ ...includePropertiesWhen(() => hostGlobal == null ? void 0 : { withGlobalFallback: hostGlobal.fallback }),
2820
+ ...includePropertiesWhen(() => {
2821
+ if (!state.strict && hostGlobal == null) return void 0;
2822
+ return { globalReference: intrinsicGlobalReference(binding.name, state) };
2823
+ })
2824
+ };
2825
+ })
2826
+ };
2827
+ }
2828
+ } else if (statement.target.kind === "property") {
2829
+ const object = resolveExpression(statement.target.object, scopes, state);
2830
+ const key = resolveExpression(statement.target.key, scopes, state);
2831
+ if (object != null && key != null) target = {
2832
+ ...statement.target,
2833
+ key,
2834
+ object
2835
+ };
2836
+ } else {
2837
+ const object = resolveExpression(statement.target.object, scopes, state);
2838
+ const privateName = resolvePrivateName(statement.target.name, statement.target, scopes, state);
2839
+ if (object != null && privateName != null) target = {
2840
+ ...locatedOf(statement.target),
2841
+ kind: "private",
2842
+ object,
2843
+ privateName
2844
+ };
2845
+ }
2846
+ const body = resolveStatement(statement.body, forScopes, state, functionBody, loopDepth + 1, breakDepth + 1);
2847
+ if (iterable == null || target == null || body == null) return void 0;
2848
+ return {
2849
+ ...statement,
2850
+ body,
2851
+ iterable,
2852
+ target
2853
+ };
2854
+ }
2855
+ if (statement.kind === "switch") {
2856
+ const discriminant = resolveExpression(statement.discriminant, scopes, state);
2857
+ if (discriminant == null) return void 0;
2858
+ const caseScope = /* @__PURE__ */ new Map();
2859
+ const caseStatements = statement.cases.flatMap((switchCase) => switchCase.body);
2860
+ predeclareBindings(caseStatements, caseScope, state, true);
2861
+ const caseScopes = [...scopes, caseScope];
2862
+ const functionInits = buildFunctionInits(caseStatements, caseScope, caseScopes, state);
2863
+ let sawDefault = false;
2864
+ const cases = [];
2865
+ for (const switchCase of statement.cases) {
2866
+ if (switchCase.test == null) {
2867
+ if (sawDefault) {
2868
+ state.diagnostics.push(sourceDiagnostic(state.sourceId, statement, "A switch statement allows one default clause."));
2869
+ return;
2870
+ }
2871
+ sawDefault = true;
2872
+ }
2873
+ const test = switchCase.test == null ? void 0 : resolveExpression(switchCase.test, caseScopes, state);
2874
+ if (switchCase.test != null && test == null) return void 0;
2875
+ const body = [];
2876
+ for (const child of switchCase.body) {
2877
+ if (child.kind === "function") continue;
2878
+ const resolved = resolveStatementItems(child, caseScopes, state, functionBody, loopDepth, breakDepth + 1);
2879
+ if (resolved == null) return void 0;
2880
+ body.push(...resolved);
2881
+ }
2882
+ cases.push({
2883
+ body,
2884
+ range: switchCase.range,
2885
+ ...includePropertiesWhen(() => {
2886
+ if (test == null) return void 0;
2887
+ return { test };
2888
+ })
2889
+ });
2890
+ }
2891
+ return {
2892
+ ...statement,
2893
+ cases,
2894
+ discriminant,
2895
+ ...includePropertiesWhen(() => {
2896
+ if (functionInits.length === 0) return void 0;
2897
+ return { functionInits };
2898
+ })
2899
+ };
2900
+ }
2901
+ if (statement.kind === "with") {
2902
+ const object = resolveExpression(statement.object, scopes, state);
2903
+ if (object == null) return void 0;
2904
+ const objectBindingId = state.nextBindingId;
2905
+ state.nextBindingId += 1;
2906
+ const withScope = /* @__PURE__ */ new Map();
2907
+ const fallbackBindings = /* @__PURE__ */ new Map();
2908
+ state.withScopes.set(withScope, objectBindingId);
2909
+ state.withFallbacks.push(fallbackBindings);
2910
+ const body = resolveStatement(statement.body, [...scopes, withScope], state, functionBody, loopDepth, breakDepth);
2911
+ state.withFallbacks.pop();
2912
+ state.withScopes.delete(withScope);
2913
+ if (body == null) return void 0;
2914
+ return {
2915
+ ...statement,
2916
+ body,
2917
+ fallbackBindings: [...fallbackBindings.values()].map((binding) => ({
2918
+ bindingId: binding.id,
2919
+ name: binding.name
2920
+ })),
2921
+ object,
2922
+ objectBindingId
2923
+ };
2924
+ }
2925
+ const test = resolveExpression(statement.test, scopes, state);
2926
+ const consequent = resolveStatement(statement.consequent, scopes, state, functionBody, loopDepth, breakDepth);
2927
+ const alternate = statement.alternate == null ? void 0 : resolveStatement(statement.alternate, scopes, state, functionBody, loopDepth, breakDepth);
2928
+ if (test == null || consequent == null) return void 0;
2929
+ if (statement.alternate != null && alternate == null) return void 0;
2930
+ return {
2931
+ ...statement,
2932
+ alternate,
2933
+ consequent,
2934
+ test
2935
+ };
2936
+ }
2937
+ function buildSeededHir(program, seed = {}) {
2938
+ const diagnostics = [];
2939
+ const scriptScope = new Map(seed.bindings);
2940
+ const state = {
2941
+ diagnostics,
2942
+ hostScriptBindings: seed.hostScriptBindings,
2943
+ hostScriptSources: seed.hostScriptBindings === true ? /* @__PURE__ */ new Set() : void 0,
2944
+ hostScriptAliases: seed.hostScriptBindings === true ? /* @__PURE__ */ new Set() : void 0,
2945
+ globalObjectBindingIds: new Set(seed.globalObjectBindingIds),
2946
+ globalReferences: seed.globalReferences,
2947
+ scriptScope,
2948
+ functionInfo: /* @__PURE__ */ new Map(),
2949
+ hirFunctions: [],
2950
+ agentHoles: seed.agentHoles == null ? void 0 : new Map(seed.agentHoles.map((hole, index) => [hole.name, {
2951
+ index,
2952
+ reads: 0,
2953
+ span: hole.span
2954
+ }])),
2955
+ agentTemplates: seed.collectAgentTemplates === true ? /* @__PURE__ */ new Map() : void 0,
2956
+ importCallTargets: seed.importCallTargets,
2957
+ intrinsicGlobalObjectBinding: void 0,
2958
+ intrinsicReadFallbacks: /* @__PURE__ */ new Map(),
2959
+ labels: [],
2960
+ moduleCode: seed.moduleBody === true,
2961
+ nextBindingId: seed.nextBindingId ?? 0,
2962
+ nextFunctionId: seed.nextFunctionId ?? 0,
2963
+ sourceId: program.sourceId,
2964
+ strict: program.strict === true,
2965
+ varScope: scriptScope,
2966
+ withFallbacks: [],
2967
+ withInitializingFallbackNames: /* @__PURE__ */ new Set(),
2968
+ referencedBindingIds: /* @__PURE__ */ new Set(),
2969
+ withScopes: /* @__PURE__ */ new Map()
2970
+ };
2971
+ predeclareBindings(program.body, scriptScope, state, seed.moduleBody === true);
2972
+ for (const entry of program.globalObjectNames ?? []) {
2973
+ const binding = scriptScope.get(entry.name);
2974
+ if (binding != null) state.globalObjectBindingIds.add(binding.id);
2975
+ }
2976
+ const body = resolveStatementList(program.body, seed.outerBindings == null ? [] : [new Map(seed.outerBindings)], state, false, scriptScope);
2977
+ for (const [name, hole] of state.agentHoles ?? []) {
2978
+ if (hole.reads === 1) continue;
2979
+ diagnostics.push(sourceDiagnostic(state.sourceId, program, `Agent source template hole ${hole.index} (${name}) is not one numeric literal position.`));
2980
+ }
2981
+ if (diagnostics.length > 0) return {
2982
+ diagnostics,
2983
+ nextBindingId: state.nextBindingId,
2984
+ nextFunctionId: state.nextFunctionId
2985
+ };
2986
+ const globalObjectBindings = [];
2987
+ for (const entry of program.globalObjectNames ?? []) {
2988
+ const binding = scriptScope.get(entry.name);
2989
+ if (binding == null) throw new Error(`Global object name '${entry.name}' has no script binding.`);
2990
+ globalObjectBindings.push({
2991
+ declaration: entry.declaration,
2992
+ id: binding.id,
2993
+ name: entry.name,
2994
+ range: entry.range
2995
+ });
2996
+ }
2997
+ if (diagnostics.length > 0) return {
2998
+ diagnostics,
2999
+ nextBindingId: state.nextBindingId,
3000
+ nextFunctionId: state.nextFunctionId
3001
+ };
3002
+ return {
3003
+ diagnostics,
3004
+ nextBindingId: state.nextBindingId,
3005
+ nextFunctionId: state.nextFunctionId,
3006
+ ...includePropertiesWhen(() => {
3007
+ if (seed.fragmentMetadata !== true) return void 0;
3008
+ return {
3009
+ scriptBindings: scriptScope,
3010
+ initializingWithNames: [...state.withInitializingFallbackNames]
3011
+ };
3012
+ }),
3013
+ program: {
3014
+ ...includePropertiesWhen(() => state.hostScriptSources == null ? void 0 : { hostScriptSources: [...state.hostScriptSources] }),
3015
+ ...includePropertiesWhen(() => {
3016
+ if (state.agentTemplates == null) return void 0;
3017
+ return { agentTemplates: [...state.agentTemplates.values()] };
3018
+ }),
3019
+ body: [
3020
+ ...state.intrinsicGlobalObjectBinding == null ? [] : [{
3021
+ bindingId: state.intrinsicGlobalObjectBinding.id,
3022
+ hint: void 0,
3023
+ initializer: {
3024
+ kind: "this",
3025
+ range: program.range,
3026
+ thisMode: "global"
3027
+ },
3028
+ kind: "const",
3029
+ name: state.intrinsicGlobalObjectBinding.name,
3030
+ range: program.range
3031
+ }],
3032
+ ...state.importMetaBinding == null ? [] : [importMetaInitializer(state.importMetaBinding, program.range)],
3033
+ ...body
3034
+ ],
3035
+ functions: state.hirFunctions,
3036
+ globalLexicalNames: (program.globalLexicalNames ?? []).map((entry) => {
3037
+ if (seed.hostScriptBindings !== true) return {
3038
+ name: entry.name,
3039
+ range: entry.range
3040
+ };
3041
+ const binding = scriptScope.get(entry.name);
3042
+ return {
3043
+ id: binding.id,
3044
+ mutable: binding.mutable,
3045
+ name: entry.name,
3046
+ range: entry.range
3047
+ };
3048
+ }),
3049
+ globalBindings: [...state.intrinsicGlobalObjectBinding == null ? [] : [state.intrinsicGlobalObjectBinding], ...[...state.intrinsicReadFallbacks].map(([unresolvableName, binding]) => ({
3050
+ id: binding.id,
3051
+ name: binding.name,
3052
+ unresolvableName
3053
+ }))],
3054
+ globalObjectBindings,
3055
+ ...includePropertiesWhen(() => {
3056
+ if (state.intrinsicGlobalObjectBinding == null) return void 0;
3057
+ return { intrinsicGlobalObjectBindingId: state.intrinsicGlobalObjectBinding.id };
3058
+ }),
3059
+ ...includePropertiesWhen(() => {
3060
+ if (state.importMetaBinding == null) return void 0;
3061
+ return { importMetaBindingId: state.importMetaBinding.id };
3062
+ }),
3063
+ kind: "hir-program",
3064
+ range: program.range,
3065
+ sourceId: program.sourceId,
3066
+ strict: program.strict === true
3067
+ }
3068
+ };
3069
+ }
3070
+ /** Validate owned syntax and resolve all lexical and function identities. */
3071
+ function buildHir(program, options = {}) {
3072
+ return buildSeededHir(program, options);
3073
+ }
3074
+
3075
+ //#endregion
3076
+ export { buildHir, buildSeededHir, declaredHirBindingIds, hirBindingIdentifiers, hirBindingPatternHasAwait, hirExpressionHasAwait, importCallProfileMessage, sourceDiagnostic };
3077
+ //# sourceMappingURL=hir-build.js.map