@geastack/compiler 1.0.33 → 1.0.35

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 (215) hide show
  1. package/README.md +1 -1
  2. package/dist/cli-coverage.js +2 -4
  3. package/dist/cli-emit.js +1 -1
  4. package/dist/cli-module-graph.js +1 -1
  5. package/dist/cli.js +2 -1
  6. package/dist/compiler.js +14 -6
  7. package/dist/conversion/array-view.js +9 -0
  8. package/dist/conversion/build.js +15 -8
  9. package/dist/conversion/dictionary-view.js +2 -2
  10. package/dist/conversion/nodes.d.ts +7 -0
  11. package/dist/conversion/nodes.js +58 -11
  12. package/dist/identity/ids.d.ts +4 -0
  13. package/dist/identity/ids.js +4 -0
  14. package/dist/ir/build.d.ts +4 -3
  15. package/dist/ir/build.js +14 -5
  16. package/dist/ir/call-dispatch.js +67 -7
  17. package/dist/ir/call-entry.js +1 -1
  18. package/dist/ir/callable-class-flow.d.ts +4 -0
  19. package/dist/ir/callable-class-flow.js +123 -1
  20. package/dist/ir/certify/parallel-region.js +698 -20
  21. package/dist/ir/certify.d.ts +3 -0
  22. package/dist/ir/certify.js +44 -9
  23. package/dist/ir/class-allocation-domain.js +2 -2
  24. package/dist/ir/class-construction-status.d.ts +10 -0
  25. package/dist/ir/class-construction-status.js +36 -0
  26. package/dist/ir/convert-carriers.d.ts +10 -1
  27. package/dist/ir/convert-carriers.js +18 -1
  28. package/dist/ir/dynamic-constructor-conversions.js +4 -2
  29. package/dist/ir/finite-iterator-close.d.ts +14 -0
  30. package/dist/ir/finite-iterator-close.js +50 -0
  31. package/dist/ir/host-template-conversions.js +9 -0
  32. package/dist/ir/lower-allocation.js +1 -1
  33. package/dist/ir/lower-flow.js +24 -3
  34. package/dist/ir/lower-operands.js +88 -7
  35. package/dist/ir/lower-protocol.js +1 -1
  36. package/dist/ir/lower.js +9 -4
  37. package/dist/ir/model.d.ts +39 -1
  38. package/dist/ir/native-accessor-definition.d.ts +3 -2
  39. package/dist/ir/native-accessor-definition.js +29 -10
  40. package/dist/ir/native-array-transport.js +2 -2
  41. package/dist/ir/native-field-view-domains.d.ts +23 -0
  42. package/dist/ir/native-field-view-domains.js +115 -9
  43. package/dist/ir/native-field-view-facts.d.ts +46 -2
  44. package/dist/ir/native-field-view-facts.js +159 -4
  45. package/dist/ir/native-iterable.d.ts +17 -0
  46. package/dist/ir/native-iterable.js +72 -0
  47. package/dist/ir/native-key-query.js +1 -1
  48. package/dist/ir/operation-conversions.js +15 -3
  49. package/dist/ir/program-conversions.js +2 -2
  50. package/dist/ir/publish-native-field-view-facts.js +9 -4
  51. package/dist/ir/pure-conversion-reuse.d.ts +58 -0
  52. package/dist/ir/pure-conversion-reuse.js +263 -0
  53. package/dist/ir/queries.js +1 -0
  54. package/dist/ir/reflection-demand.js +70 -18
  55. package/dist/ir/shake.js +2 -2
  56. package/dist/ir/unobserved-callable-inputs.d.ts +37 -0
  57. package/dist/ir/unobserved-callable-inputs.js +111 -0
  58. package/dist/native-signatures.d.ts +1 -1
  59. package/dist/native-signatures.js +27 -2
  60. package/dist/plugins/gea/component-classes.d.ts +2 -1
  61. package/dist/plugins/gea/component-classes.js +3 -3
  62. package/dist/plugins/gea/contract.d.ts +26 -3
  63. package/dist/plugins/gea/contract.js +42 -43
  64. package/dist/plugins/gea/host.d.ts +9 -0
  65. package/dist/plugins/gea/host.js +25 -4
  66. package/dist/plugins/gea/plugin.js +12 -6
  67. package/dist/plugins/gea/producer.d.ts +2 -2
  68. package/dist/plugins/gea/producer.js +2 -2
  69. package/dist/plugins/load.js +18 -1
  70. package/dist/plugins/model.d.ts +5 -0
  71. package/dist/plugins/native-iterable-providers.d.ts +18 -0
  72. package/dist/plugins/native-iterable-providers.js +51 -0
  73. package/dist/project-preparation.d.ts +8 -0
  74. package/dist/project-preparation.js +49 -7
  75. package/dist/projection/bindings.d.ts +13 -0
  76. package/dist/projection/bindings.js +20 -1
  77. package/dist/projection/classes.d.ts +4 -2
  78. package/dist/projection/classes.js +12 -5
  79. package/dist/representation/collections.js +2 -0
  80. package/dist/representation/derive.js +1 -0
  81. package/dist/representation/model.d.ts +2 -0
  82. package/dist/representation/model.js +1 -0
  83. package/dist/representation/policies.d.ts +2 -0
  84. package/dist/representation/primitive-domain.d.ts +1 -1
  85. package/dist/representation/primitive-domain.js +1 -1
  86. package/dist/representation/unassigned-binding-cells.js +6 -1
  87. package/dist/representation/union.js +6 -6
  88. package/dist/semantics/frontend.d.ts +2 -0
  89. package/dist/semantics/frontend.js +34 -5
  90. package/dist/semantics/host-methods.js +4 -1
  91. package/dist/semantics/jsx-runtime-provider.d.ts +5 -0
  92. package/dist/semantics/jsx-runtime-provider.js +16 -0
  93. package/dist/semantics/model/native-iterable.d.ts +14 -0
  94. package/dist/semantics/model/native-iterable.js +7 -0
  95. package/dist/semantics/model/operands.d.ts +30 -0
  96. package/dist/semantics/model/operations.d.ts +15 -0
  97. package/dist/semantics/model/primitive-domain.d.ts +15 -0
  98. package/dist/semantics/model/primitive-domain.js +27 -4
  99. package/dist/semantics/module-resolution.js +13 -2
  100. package/dist/semantics/normalize/any-cast-member-call.d.ts +1 -1
  101. package/dist/semantics/normalize/callback-parameter-contracts.d.ts +32 -4
  102. package/dist/semantics/normalize/callback-parameter-contracts.js +66 -6
  103. package/dist/semantics/normalize/census.d.ts +3 -0
  104. package/dist/semantics/normalize/census.js +17 -12
  105. package/dist/semantics/normalize/deferred-intrinsic-protocols.d.ts +4 -1
  106. package/dist/semantics/normalize/deferred-intrinsic-protocols.js +29 -9
  107. package/dist/semantics/normalize/flow/callable-reach.js +94 -3
  108. package/dist/semantics/normalize/flow/component-solver.js +144 -55
  109. package/dist/semantics/normalize/flow/member-call-forwarding.d.ts +9 -0
  110. package/dist/semantics/normalize/flow/member-call-forwarding.js +30 -4
  111. package/dist/semantics/normalize/flow/proof-hypotheses.d.ts +2 -0
  112. package/dist/semantics/normalize/flow/proof-hypotheses.js +27 -0
  113. package/dist/semantics/normalize/flow/source-callable-own-data.js +87 -14
  114. package/dist/semantics/normalize/flow/source-commonjs-exports.d.ts +2 -0
  115. package/dist/semantics/normalize/flow/source-commonjs-exports.js +2 -0
  116. package/dist/semantics/normalize/flow/source-native-iterable-integrity.d.ts +17 -0
  117. package/dist/semantics/normalize/flow/source-native-iterable-integrity.js +125 -0
  118. package/dist/semantics/normalize/flow/source-native-origin.d.ts +6 -0
  119. package/dist/semantics/normalize/flow/source-native-origin.js +38 -0
  120. package/dist/semantics/normalize/flow/source-proxy-operation.d.ts +37 -0
  121. package/dist/semantics/normalize/flow/source-proxy-operation.js +201 -0
  122. package/dist/semantics/normalize/flow/source-raw-constructor-entry.d.ts +20 -0
  123. package/dist/semantics/normalize/flow/source-raw-constructor-entry.js +73 -0
  124. package/dist/semantics/normalize/flow/source-value-session.d.ts +50 -1
  125. package/dist/semantics/normalize/flow/source-value-session.js +596 -57
  126. package/dist/semantics/normalize/flow/value-flow.d.ts +17 -0
  127. package/dist/semantics/normalize/flow/value-flow.js +84 -0
  128. package/dist/semantics/normalize/fresh-dictionary-storage.js +2 -2
  129. package/dist/semantics/normalize/fresh-table-last-use.d.ts +52 -0
  130. package/dist/semantics/normalize/fresh-table-last-use.js +129 -0
  131. package/dist/semantics/normalize/gating.js +5 -1
  132. package/dist/semantics/normalize/global-host-mutations.js +190 -13
  133. package/dist/semantics/normalize/identities.js +20 -1
  134. package/dist/semantics/normalize/local-bindings.js +2 -2
  135. package/dist/semantics/normalize/parameter-bindings.js +10 -1
  136. package/dist/semantics/normalize/producer-context.d.ts +5 -0
  137. package/dist/semantics/normalize/producers/binding-kind.js +2 -0
  138. package/dist/semantics/normalize/producers/cited-value-type.d.ts +2 -2
  139. package/dist/semantics/normalize/producers/cited-value-type.js +2 -2
  140. package/dist/semantics/normalize/producers/class-lifecycle.js +39 -9
  141. package/dist/semantics/normalize/producers/computations.js +36 -5
  142. package/dist/semantics/normalize/producers/conditional-guard.d.ts +6 -0
  143. package/dist/semantics/normalize/producers/conditional-guard.js +5 -0
  144. package/dist/semantics/normalize/producers/control.js +7 -1
  145. package/dist/semantics/normalize/producers/destructuring.js +8 -9
  146. package/dist/semantics/normalize/producers/invocations.js +55 -2
  147. package/dist/semantics/normalize/producers/properties.d.ts +13 -0
  148. package/dist/semantics/normalize/producers/properties.js +21 -3
  149. package/dist/semantics/normalize/producers/references.js +8 -4
  150. package/dist/semantics/normalize/reachability.d.ts +15 -3
  151. package/dist/semantics/normalize/reachability.js +128 -20
  152. package/dist/semantics/normalize/return-bindings.js +3 -3
  153. package/dist/semantics/normalize/source-native-iterable.d.ts +19 -0
  154. package/dist/semantics/normalize/source-native-iterable.js +74 -0
  155. package/dist/semantics/normalize/structural-any-binding-consumer.d.ts +1 -1
  156. package/dist/semantics/normalize/structural-declarations.js +1 -1
  157. package/dist/semantics/normalize/structural-indexed-access.js +9 -0
  158. package/dist/semantics/normalize/structural.js +11 -0
  159. package/dist/semantics/normalize/unshared-array-result.d.ts +42 -0
  160. package/dist/semantics/normalize/unshared-array-result.js +269 -0
  161. package/dist/semantics/package-sources.js +3 -2
  162. package/dist/semantics/program.d.ts +8 -0
  163. package/dist/semantics/program.js +55 -0
  164. package/dist/semantics/source-transform-exports.d.ts +8 -0
  165. package/dist/semantics/source-transform-exports.js +145 -0
  166. package/dist/semantics/source-transform-protocol.d.ts +22 -0
  167. package/dist/semantics/this-constructor-source-transform.js +19 -1
  168. package/dist/targets/cpp/class-layout.d.ts +2 -0
  169. package/dist/targets/cpp/class-layout.js +35 -1
  170. package/dist/targets/cpp/class-properties/emit-class-properties.d.ts +11 -1
  171. package/dist/targets/cpp/class-properties/emit-class-properties.js +33 -4
  172. package/dist/targets/cpp/conversions.js +6 -3
  173. package/dist/targets/cpp/emit-callable.js +73 -7
  174. package/dist/targets/cpp/emit-context.d.ts +23 -9
  175. package/dist/targets/cpp/emit-context.js +19 -8
  176. package/dist/targets/cpp/emit-equality.js +41 -1
  177. package/dist/targets/cpp/emit-exceptions.js +29 -11
  178. package/dist/targets/cpp/emit-iterator-object-view.js +2 -2
  179. package/dist/targets/cpp/emit-iterator.js +33 -9
  180. package/dist/targets/cpp/emit-live-field-view.js +6 -1
  181. package/dist/targets/cpp/emit-narrowing.d.ts +11 -3
  182. package/dist/targets/cpp/emit-narrowing.js +58 -18
  183. package/dist/targets/cpp/emit-native-field-view.d.ts +3 -1
  184. package/dist/targets/cpp/emit-native-field-view.js +84 -8
  185. package/dist/targets/cpp/emit-properties.d.ts +1 -0
  186. package/dist/targets/cpp/emit-properties.js +26 -7
  187. package/dist/targets/cpp/emit-protocol-iterator.js +5 -2
  188. package/dist/targets/cpp/emit-record-view.js +14 -2
  189. package/dist/targets/cpp/emit-tostring.js +23 -1
  190. package/dist/targets/cpp/emit-union-properties.js +1 -1
  191. package/dist/targets/cpp/emit.d.ts +1 -1
  192. package/dist/targets/cpp/emit.js +99 -13
  193. package/dist/targets/cpp/host/emit-host-invoke.js +2 -2
  194. package/dist/targets/cpp/host/emit-host-object.js +28 -2
  195. package/dist/targets/cpp/host/emit-host-properties.js +9 -1
  196. package/dist/targets/cpp/host/emit-host-value.d.ts +1 -1
  197. package/dist/targets/cpp/host/emit-host-value.js +8 -6
  198. package/dist/targets/cpp/host/host-members.js +2 -1
  199. package/dist/targets/cpp/prototype/emit-prototype-array.js +69 -5
  200. package/dist/targets/cpp/prototype/emit-prototype-invoke.js +9 -9
  201. package/dist/targets/cpp/prototype/emit-prototype-iterator.js +2 -2
  202. package/dist/targets/cpp/prototype/emit-prototype-promise.js +7 -7
  203. package/dist/targets/cpp/records.d.ts +1 -0
  204. package/dist/targets/cpp/records.js +83 -22
  205. package/dist/targets/cpp/translation-unit.js +30 -16
  206. package/package.json +8 -7
  207. package/src/targets/cpp/runtime/gea_dynamic_proxy.h +42 -3
  208. package/src/targets/cpp/runtime/gea_eval.h +3 -0
  209. package/src/targets/cpp/runtime/gea_runtime.h +2399 -676
  210. package/dist/representation/any-null-record-members.d.ts +0 -41
  211. package/dist/representation/any-null-record-members.js +0 -115
  212. package/dist/semantics/any-null-member-declarations.d.ts +0 -32
  213. package/dist/semantics/any-null-member-declarations.js +0 -90
  214. package/dist/semantics/normalize/fresh-array-result.d.ts +0 -32
  215. package/dist/semantics/normalize/fresh-array-result.js +0 -125
@@ -1,5 +1,6 @@
1
1
  import ts from 'typescript';
2
2
  import { inheritedImplementationOf } from './merged-declaration.js';
3
+ import { isFreshOrLastUseOfFreshLocal } from './fresh-table-last-use.js';
3
4
  /**
4
5
  * The method body a member call runs when it returns an array NO OTHER
5
6
  * REFERENCE holds: every `return` hands back an array literal, or a local
@@ -110,6 +111,274 @@ const nonEscapingUse = (use) => {
110
111
  return true;
111
112
  return false;
112
113
  };
114
+ /**
115
+ * `Array.prototype` members that read their receiver and hand no reference to
116
+ * it anywhere: the result is a value, an element, or a NEW array of the
117
+ * receiver's elements. A member answering an iterator (`entries`, `values`)
118
+ * would keep the receiver alive past the call and is not listed. The number
119
+ * is how many parameters the member's callback may declare without receiving
120
+ * the array itself (ECMA-262 passes it as the callback's last argument);
121
+ * `null` for a member taking no callback.
122
+ */
123
+ const READING_ARRAY_MEMBERS = new Map([
124
+ ['forEach', 2],
125
+ ['map', 2],
126
+ ['filter', 2],
127
+ ['some', 2],
128
+ ['every', 2],
129
+ ['find', 2],
130
+ ['findIndex', 2],
131
+ ['findLast', 2],
132
+ ['findLastIndex', 2],
133
+ ['flatMap', 2],
134
+ ['reduce', 3],
135
+ ['reduceRight', 3],
136
+ ['indexOf', null],
137
+ ['lastIndexOf', null],
138
+ ['includes', null],
139
+ ['join', null],
140
+ ['at', null],
141
+ ['slice', null]
142
+ ]);
143
+ /** Only the lib's own `Array<T>`/`ReadonlyArray<T>` member: a program body of the same name is not known to only read. */
144
+ const libArrayMember = (checker, name) => {
145
+ const declarations = checker.getSymbolAtLocation(name)?.declarations ?? [];
146
+ return (declarations.length > 0 &&
147
+ declarations.every((declaration) => {
148
+ if (!declaration.getSourceFile().hasNoDefaultLib)
149
+ return false;
150
+ const owner = declaration.parent;
151
+ return ts.isInterfaceDeclaration(owner) && (owner.name.text === 'Array' || owner.name.text === 'ReadonlyArray');
152
+ }));
153
+ };
154
+ /**
155
+ * `use.member(...)` where the member only reads its receiver: its callback, if
156
+ * any, is an arrow function (no `this`, no `arguments`) that declares too few
157
+ * parameters to receive the array. Its arguments are checked as uses of their
158
+ * own: one naming the array again is a separate, refused use.
159
+ */
160
+ const readingMemberCall = (checker, use) => {
161
+ const access = use.parent;
162
+ if (!ts.isPropertyAccessExpression(access) || access.expression !== use || !ts.isIdentifier(access.name))
163
+ return false;
164
+ const call = access.parent;
165
+ if (!ts.isCallExpression(call) || call.expression !== access || call.questionDotToken !== undefined)
166
+ return false;
167
+ const name = access.name.text;
168
+ if (!READING_ARRAY_MEMBERS.has(name) || !libArrayMember(checker, access.name))
169
+ return false;
170
+ const arity = READING_ARRAY_MEMBERS.get(name);
171
+ if (arity === null)
172
+ return true;
173
+ const callback = call.arguments[0];
174
+ return (callback !== undefined &&
175
+ ts.isArrowFunction(callback) &&
176
+ callback.parameters.length <= arity &&
177
+ callback.parameters.every((parameter) => parameter.dotDotDotToken === undefined));
178
+ };
179
+ const isUpdate = (operator) => operator === ts.SyntaxKind.PlusPlusToken || operator === ts.SyntaxKind.MinusMinusToken;
180
+ /** `array[i]` read and nothing else: no store, no update, no delete, and not a call whose receiver would be the array. */
181
+ const elementRead = (use) => {
182
+ const access = use.parent;
183
+ if (!ts.isElementAccessExpression(access) || access.expression !== use)
184
+ return false;
185
+ const outer = access.parent;
186
+ if (isAssignmentTarget(access) || ts.isDeleteExpression(outer))
187
+ return false;
188
+ if ((ts.isPrefixUnaryExpression(outer) || ts.isPostfixUnaryExpression(outer)) && isUpdate(outer.operator))
189
+ return false;
190
+ if (ts.isCallExpression(outer) && outer.expression === access)
191
+ return false;
192
+ // Possibly a destructuring target (`[a[0]] = ...`, `({ x: a[0] } = ...)`), which stores.
193
+ return !ts.isArrayLiteralExpression(outer) && !ts.isPropertyAssignment(outer) && !ts.isShorthandPropertyAssignment(outer);
194
+ };
195
+ /** `array.length` read, never stored. */
196
+ const lengthRead = (use) => {
197
+ const access = use.parent;
198
+ if (!ts.isPropertyAccessExpression(access) || access.expression !== use || access.name.text !== 'length')
199
+ return false;
200
+ const outer = access.parent;
201
+ if (isAssignmentTarget(access) || ts.isDeleteExpression(outer))
202
+ return false;
203
+ return !((ts.isPrefixUnaryExpression(outer) || ts.isPostfixUnaryExpression(outer)) && isUpdate(outer.operator));
204
+ };
205
+ /** `for (... of array)`: the elements are read; the array is held only by the loop. */
206
+ const iterated = (use) => ts.isForOfStatement(use.parent) && use.parent.expression === use;
207
+ /**
208
+ * A formal parameter of `callee` that the body only READS as an array: it
209
+ * measures it, indexes it, iterates it and calls reading members on it. It
210
+ * never stores through it, stores IT anywhere, compares it by identity,
211
+ * captures it in a closure, or hands it to another call. `arguments` would
212
+ * alias it, so a body naming `arguments` refuses. So does a generator or an
213
+ * `async` body, which keeps running after the call returned, when the
214
+ * caller's array may already have changed.
215
+ *
216
+ * This is the callee half of a call argument's unshared copy
217
+ * (`unsharedArrayArgumentAt`): such a formal cannot observe whether it holds
218
+ * the caller's array or an element-wise copy of it.
219
+ */
220
+ export const readOnlyArrayFormalOf = (checker, callee, index) => {
221
+ if (!ts.isFunctionDeclaration(callee) &&
222
+ !ts.isFunctionExpression(callee) &&
223
+ !ts.isArrowFunction(callee) &&
224
+ !ts.isMethodDeclaration(callee))
225
+ return false;
226
+ const body = callee.body;
227
+ if (!body || (!ts.isArrowFunction(callee) && callee.asteriskToken !== undefined) || callee.getSourceFile().isDeclarationFile)
228
+ return false;
229
+ if (ts.getCombinedModifierFlags(callee) & ts.ModifierFlags.Async)
230
+ return false;
231
+ // A `this` parameter is not a positional argument; it would shift every index.
232
+ const parameters = callee.parameters.filter((parameter) => !(ts.isIdentifier(parameter.name) && parameter.name.text === 'this'));
233
+ const parameter = parameters[index];
234
+ if (!parameter || parameter.dotDotDotToken !== undefined || !ts.isIdentifier(parameter.name))
235
+ return false;
236
+ if (!checker.isArrayType(checker.getTypeAtLocation(parameter)))
237
+ return false;
238
+ const symbol = checker.getSymbolAtLocation(parameter.name);
239
+ if (!symbol)
240
+ return false;
241
+ let admitted = true;
242
+ const visit = (node) => {
243
+ if (!admitted)
244
+ return;
245
+ if (ts.isIdentifier(node)) {
246
+ if (node.text === 'arguments')
247
+ admitted = false;
248
+ else if (node !== parameter.name && checker.getSymbolAtLocation(node) === symbol)
249
+ admitted =
250
+ enclosingFunction(node) === callee &&
251
+ (lengthRead(node) || elementRead(node) || iterated(node) || readingMemberCall(checker, node));
252
+ return;
253
+ }
254
+ ts.forEachChild(node, visit);
255
+ };
256
+ // A default initializer of another formal may name this one: the parameter list is part of the body's reach.
257
+ for (const other of callee.parameters)
258
+ if (other !== parameter)
259
+ visit(other);
260
+ visit(body);
261
+ return admitted;
262
+ };
263
+ /**
264
+ * A `const` local initialized with an array literal whose EVERY use, before
265
+ * or after the one asked about, keeps the array in this body: a
266
+ * `arrayKind` use, a `return`, or an argument `readOnlyArgument` admits.
267
+ * No closure names it (a closure could run while a copy is outstanding), and
268
+ * it is not exported, so no other module can.
269
+ *
270
+ * Unlike `isFreshOrLastUseOfFreshLocal`, the array outlives the use: it is
271
+ * the caller's, still filled and read after the call. What makes a copy at a
272
+ * read-only argument unobservable is that nothing else can reach the array
273
+ * while the callee runs, and the callee can neither write it nor keep it.
274
+ */
275
+ const unsharedLocalArrayOf = (checker, expression, readOnlyArgument) => {
276
+ if (!ts.isIdentifier(expression))
277
+ return false;
278
+ const symbol = checker.getSymbolAtLocation(expression);
279
+ const declaration = symbol?.declarations?.length === 1 ? symbol.declarations[0] : undefined;
280
+ if (!symbol || !declaration || !ts.isVariableDeclaration(declaration) || !ts.isIdentifier(declaration.name))
281
+ return false;
282
+ if (!declaration.initializer || !ts.isArrayLiteralExpression(skipParentheses(declaration.initializer)))
283
+ return false;
284
+ const list = declaration.parent;
285
+ if (!ts.isVariableDeclarationList(list) || (list.flags & ts.NodeFlags.Const) === 0)
286
+ return false;
287
+ const owner = enclosingFunction(declaration);
288
+ const file = declaration.getSourceFile();
289
+ if (owner === undefined) {
290
+ // Top level: only a module's own unexported binding is unreachable from elsewhere.
291
+ if (!ts.isExternalModule(file) || !ts.isVariableStatement(list.parent))
292
+ return false;
293
+ if (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export)
294
+ return false;
295
+ if (isExportedByName(file, declaration.name.text))
296
+ return false;
297
+ }
298
+ let admitted = true;
299
+ const visit = (node) => {
300
+ if (!admitted)
301
+ return;
302
+ if (ts.isIdentifier(node) && node !== declaration.name && checker.getSymbolAtLocation(node) === symbol) {
303
+ admitted =
304
+ enclosingFunction(node) === owner &&
305
+ (ts.isReturnStatement(node.parent) || nonEscapingArrayUse(checker, node) || readOnlyArgument(node));
306
+ return;
307
+ }
308
+ ts.forEachChild(node, visit);
309
+ };
310
+ ts.forEachChild(owner ?? file, visit);
311
+ return admitted;
312
+ };
313
+ /** `export { name }` / `export default name` exports a top-level binding without an `export` modifier on it. */
314
+ const isExportedByName = (file, name) => file.statements.some((statement) => (ts.isExportDeclaration(statement) &&
315
+ statement.exportClause !== undefined &&
316
+ ts.isNamedExports(statement.exportClause) &&
317
+ statement.exportClause.elements.some((element) => (element.propertyName ?? element.name).text === name)) ||
318
+ (ts.isExportAssignment(statement) && ts.isIdentifier(statement.expression) && statement.expression.text === name));
319
+ /** `array.push(...)` / `array.unshift(...)`: fills the array, answers a number. */
320
+ const fillingCall = (use) => {
321
+ const access = use.parent;
322
+ if (!ts.isPropertyAccessExpression(access) || access.expression !== use || !ts.isIdentifier(access.name))
323
+ return false;
324
+ const call = access.parent;
325
+ return (access.name.text === 'push' || access.name.text === 'unshift') && ts.isCallExpression(call) && call.expression === access;
326
+ };
327
+ /** `array[i] = value`: a plain store into the local's own array. */
328
+ const indexedStore = (use) => {
329
+ const access = use.parent;
330
+ if (!ts.isElementAccessExpression(access) || access.expression !== use)
331
+ return false;
332
+ const store = access.parent;
333
+ return ts.isBinaryExpression(store) && store.left === access && store.operatorToken.kind === ts.SyntaxKind.EqualsToken;
334
+ };
335
+ /**
336
+ * An array as `fresh-table-last-use.ts` proves objects unshared: an array
337
+ * literal is the allocation, and a use that fills, measures, indexes,
338
+ * iterates or reads it through a reading member hands no reference to it
339
+ * anywhere.
340
+ */
341
+ export const arrayKind = (checker) => ({
342
+ isFresh: (expression) => ts.isArrayLiteralExpression(expression),
343
+ admitsUse: (use) => nonEscapingArrayUse(checker, use)
344
+ });
345
+ const nonEscapingArrayUse = (checker, use) => lengthRead(use) || fillingCall(use) || indexedStore(use) || elementRead(use) || iterated(use) || readingMemberCall(checker, use);
346
+ /**
347
+ * `return local`: the local array's last use (`isFreshOrLastUseOfFreshLocal`
348
+ * over `arrayKind`), so its one holder dies here and the returned array
349
+ * may be rebuilt at the declared carrier (`SemanticOperand.unsharedFreshArray`).
350
+ * A literal returned directly already takes the slot's carrier from its own
351
+ * allocation, so only the local form is asked.
352
+ */
353
+ export const unsharedReturnedArray = (checker, statement) => statement.expression !== undefined &&
354
+ ts.isIdentifier(statement.expression) &&
355
+ isFreshOrLastUseOfFreshLocal(checker, statement.expression, arrayKind(checker));
356
+ /**
357
+ * `callee(local)` where `local` is unshared (`unsharedLocalArrayOf`) and the
358
+ * formal it binds is read only (`readOnlyArrayFormalOf`)
359
+ * (`SemanticOperand.readOnlyArrayArgument`). `formalOf` is asked of every call
360
+ * the local is passed to, so each of them must be read only too.
361
+ */
362
+ export const readOnlyArrayArgumentAt = (checker, argument, formalOf) => {
363
+ // Cheapest first: most identifier arguments are not array-literal locals at all.
364
+ if (!ts.isIdentifier(argument) || !arrayLiteralConstOf(checker, argument))
365
+ return false;
366
+ const readOnlyArgument = (use) => {
367
+ const call = use.parent;
368
+ if (!ts.isCallExpression(call) || !call.arguments.includes(use) || call.questionDotToken !== undefined)
369
+ return false;
370
+ const formal = formalOf(call, use);
371
+ return formal !== null && readOnlyArrayFormalOf(checker, formal.callee, formal.index);
372
+ };
373
+ return readOnlyArgument(argument) && unsharedLocalArrayOf(checker, argument, readOnlyArgument);
374
+ };
375
+ const arrayLiteralConstOf = (checker, name) => {
376
+ const declaration = checker.getSymbolAtLocation(name)?.declarations?.[0];
377
+ return (declaration !== undefined &&
378
+ ts.isVariableDeclaration(declaration) &&
379
+ declaration.initializer !== undefined &&
380
+ ts.isArrayLiteralExpression(skipParentheses(declaration.initializer)));
381
+ };
113
382
  const isAssignmentTarget = (node) => ts.isBinaryExpression(node.parent) &&
114
383
  node.parent.left === node &&
115
384
  node.parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment &&
@@ -1,5 +1,5 @@
1
1
  import ts from 'typescript';
2
- import { dirname, resolve, relative } from 'node:path';
2
+ import { dirname, resolve, relative, sep } from 'node:path';
3
3
  const recordOf = (value) => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : undefined;
4
4
  // TypeScript reports paths with forward slashes on every platform, while
5
5
  // node:path joins with a backslash on Windows. Appending `sep` therefore made
@@ -253,7 +253,8 @@ export const createPackageSourceHost = (host = ts.sys, packages = []) => {
253
253
  const suffix = relative(outDir, absolute).replace(/\.d\.(?:ts|mts|cts)$/, '');
254
254
  const suffixCandidates = inputs.filter((input) => {
255
255
  const source = relative(root, input).replace(/\.(?:ts|tsx|mts|cts)$/, '');
256
- return source === suffix || source.endsWith(`/${suffix}`);
256
+ // relative() answers in the host's separators.
257
+ return source === suffix || source.endsWith(`${sep}${suffix}`);
257
258
  });
258
259
  if (suffixCandidates.length === 1)
259
260
  return suffixCandidates[0];
@@ -132,6 +132,14 @@ export interface CompiledProgram {
132
132
  readonly absentRequirePackageOf: (specifier: string, containingFile: string) => boolean;
133
133
  /** The Program's canonical source-file object for a resolved path. */
134
134
  readonly sourceFileOf: (fileName: string) => ts.SourceFile | null;
135
+ /** Existing source-export resolver over this Program's canonical runtime graph.
136
+ * Only missing proves absence; unsupported exports remain conservative. */
137
+ readonly runtimeExportOf: (fileName: string, name: string) => {
138
+ readonly kind: 'binding';
139
+ readonly symbol: ts.Symbol;
140
+ } | {
141
+ readonly kind: 'missing' | 'refused';
142
+ };
135
143
  /** Source files reached only through authenticated static CommonJS require edges. */
136
144
  readonly commonJsSourceFiles: readonly ts.SourceFile[];
137
145
  /** The files the compiler is responsible for, excluding ambient declarations. */
@@ -13,6 +13,7 @@ import { createFrontendTiming } from './frontend-timing.js';
13
13
  import { isUncheckedGuardCopyArtifact, uncheckedGuardArgumentCopies } from './unchecked-guard-argument-copies.js';
14
14
  import { knownCallerPredicateParameters } from './known-caller-predicate-parameters.js';
15
15
  import { uncheckedWriteMemberDeclarations } from './unchecked-write-member-declarations.js';
16
+ import { resolveSourceTransformExport, sourceTransformExportStatus } from './source-transform-exports.js';
16
17
  export const defaultCompilerOptions = Object.freeze({
17
18
  target: ts.ScriptTarget.ES2022,
18
19
  module: ts.ModuleKind.ESNext,
@@ -849,12 +850,66 @@ export const createProgram = (input) => {
849
850
  .map(({ diagnostic }) => diagnostic)
850
851
  ];
851
852
  timing.report();
853
+ const exportModules = new Map();
854
+ const exportModuleOf = (fileName) => {
855
+ const file = program.getSourceFile(fileName);
856
+ if (!file)
857
+ return null;
858
+ const previous = exportModules.get(file.fileName);
859
+ if (previous)
860
+ return previous;
861
+ const selected = Object.freeze({
862
+ fileName: file.fileName,
863
+ text: file.text,
864
+ declarationFileName: undefined,
865
+ packageManifest: null,
866
+ resolveSource: (specifier) => {
867
+ const target = configured.runtimeModuleTargetOf(specifier, file.fileName, 'import');
868
+ return target ? exportModuleOf(target) : null;
869
+ }
870
+ });
871
+ exportModules.set(file.fileName, selected);
872
+ return selected;
873
+ };
852
874
  return {
853
875
  program,
854
876
  checker,
855
877
  runtimeModuleTargetOf: configured.runtimeModuleTargetOf,
856
878
  absentRequirePackageOf: configured.absentRequirePackageOf,
857
879
  sourceFileOf: (fileName) => program.getSourceFile(fileName) ?? null,
880
+ runtimeExportOf: (fileName, name) => {
881
+ const module = exportModuleOf(fileName);
882
+ if (!module)
883
+ return { kind: 'refused' };
884
+ const status = sourceTransformExportStatus(module, name);
885
+ if (status !== 'binding')
886
+ return { kind: status };
887
+ const binding = resolveSourceTransformExport(module, name);
888
+ const file = binding ? program.getSourceFile(binding.module.fileName) : undefined;
889
+ if (!binding || !file)
890
+ return { kind: 'refused' };
891
+ // The barrel's checker export may denote a type-only shadow of the
892
+ // actual runtime star export. Resolve only the resolver's terminal local
893
+ // declaration, never the barrel alias or a same-spelled global symbol.
894
+ const symbols = new Set();
895
+ for (const statement of file.statements) {
896
+ if ((ts.isClassDeclaration(statement) || ts.isFunctionDeclaration(statement) || ts.isEnumDeclaration(statement)) &&
897
+ statement.name?.text === binding.localName) {
898
+ const symbol = checker.getSymbolAtLocation(statement.name);
899
+ if (symbol)
900
+ symbols.add(symbol);
901
+ }
902
+ if (ts.isVariableStatement(statement))
903
+ for (const declaration of statement.declarationList.declarations)
904
+ if (ts.isIdentifier(declaration.name) && declaration.name.text === binding.localName) {
905
+ const symbol = checker.getSymbolAtLocation(declaration.name);
906
+ if (symbol)
907
+ symbols.add(symbol);
908
+ }
909
+ }
910
+ const [symbol] = symbols;
911
+ return symbols.size === 1 && symbol ? { kind: 'binding', symbol } : { kind: 'refused' };
912
+ },
858
913
  commonJsSourceFiles: configured.commonJsTargetPaths.flatMap((fileName) => {
859
914
  const sourceFile = program.getSourceFile(fileName);
860
915
  return sourceFile ? [sourceFile] : [];
@@ -0,0 +1,8 @@
1
+ import type { SourceTransformExport, SourceTransformModule } from './source-transform-protocol.js';
2
+ export declare const resolveSourceTransformExport: (module: SourceTransformModule, name: string) => SourceTransformExport | null;
3
+ /** Preserve the existing resolver's distinction between an absent runtime
4
+ * export and unsupported/ambiguous transport. Absence alone can prune an
5
+ * export effect; a refusal never proves absence or authenticates a value.
6
+ * @semanticCategory generic-primitive
7
+ */
8
+ export declare const sourceTransformExportStatus: (module: SourceTransformModule, name: string) => "binding" | "missing" | "refused";
@@ -0,0 +1,145 @@
1
+ import ts from 'typescript';
2
+ const missing = { kind: 'missing' }, refused = { kind: 'refused' };
3
+ const same = (a, b) => a.module.fileName === b.module.fileName && a.localName === b.localName;
4
+ /** Follow original source export bindings through the frontend's own module
5
+ * leases. This never establishes a checker symbol, a value, or purity. Cyclic,
6
+ * ambiguous and unsupported value-alias transport refuses rather than choosing
7
+ * one of the possible bindings.
8
+ * @semanticCategory generic-primitive
9
+ */
10
+ const sourceTransformExportAnswer = (module, name) => {
11
+ const visit = (unit, key, active) => {
12
+ const token = JSON.stringify([unit.fileName, key]);
13
+ if (active.has(token))
14
+ return refused;
15
+ const path = new Set(active).add(token);
16
+ const file = ts.createSourceFile(unit.fileName, unit.text, ts.ScriptTarget.Latest, true);
17
+ if (file.statements.some((statement) => ts.isExportAssignment(statement) && (statement.isExportEquals || key === 'default')))
18
+ return refused;
19
+ const binding = (local) => {
20
+ for (const statement of file.statements) {
21
+ if (ts.isImportDeclaration(statement)) {
22
+ const clause = statement.importClause;
23
+ if (!clause || !ts.isStringLiteralLike(statement.moduleSpecifier))
24
+ continue;
25
+ const imports = clause.namedBindings;
26
+ if (clause.isTypeOnly &&
27
+ (clause.name?.text === local ||
28
+ (imports && ts.isNamespaceImport(imports) && imports.name.text === local) ||
29
+ (imports && ts.isNamedImports(imports) && imports.elements.some((item) => item.name.text === local))))
30
+ return missing;
31
+ if (clause.isTypeOnly)
32
+ continue;
33
+ if (imports && ts.isNamedImports(imports) && imports.elements.some((item) => item.isTypeOnly && item.name.text === local))
34
+ return missing;
35
+ const selected = clause.name?.text === local
36
+ ? 'default'
37
+ : imports && ts.isNamedImports(imports)
38
+ ? imports.elements.find((item) => !item.isTypeOnly && item.name.text === local)
39
+ : undefined;
40
+ if (selected !== undefined) {
41
+ const dependency = unit.resolveSource?.(statement.moduleSpecifier.text);
42
+ return dependency
43
+ ? visit(dependency, typeof selected === 'string' ? selected : (selected.propertyName ?? selected.name).text, path)
44
+ : refused;
45
+ }
46
+ if (imports && ts.isNamespaceImport(imports) && imports.name.text === local)
47
+ return refused;
48
+ }
49
+ if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement) || ts.isEnumDeclaration(statement)) &&
50
+ statement.name?.text === local)
51
+ return { kind: 'binding', value: { module: unit, localName: local, exportedName: key } };
52
+ if (ts.isVariableStatement(statement)) {
53
+ for (const declaration of statement.declarationList.declarations) {
54
+ if (!ts.isIdentifier(declaration.name) || declaration.name.text !== local)
55
+ continue;
56
+ // A variable's binding is not its initializer's binding. In
57
+ // particular `const alias = helper` cannot authenticate helper.
58
+ return { kind: 'binding', value: { module: unit, localName: local, exportedName: key } };
59
+ }
60
+ }
61
+ }
62
+ return refused;
63
+ };
64
+ const explicit = [], stars = [];
65
+ for (const statement of file.statements) {
66
+ if (ts.isExportDeclaration(statement)) {
67
+ if (statement.isTypeOnly)
68
+ continue;
69
+ const clause = statement.exportClause;
70
+ if (!clause) {
71
+ stars.push(statement);
72
+ continue;
73
+ }
74
+ if (!ts.isNamedExports(clause)) {
75
+ if (clause.name.text === key)
76
+ explicit.push(refused);
77
+ continue;
78
+ }
79
+ for (const item of clause.elements) {
80
+ if (item.isTypeOnly || item.name.text !== key)
81
+ continue;
82
+ const local = (item.propertyName ?? item.name).text;
83
+ if (!statement.moduleSpecifier)
84
+ explicit.push(binding(local));
85
+ else if (!ts.isStringLiteralLike(statement.moduleSpecifier))
86
+ explicit.push(refused);
87
+ else {
88
+ const dependency = unit.resolveSource?.(statement.moduleSpecifier.text);
89
+ explicit.push(dependency ? visit(dependency, local, path) : refused);
90
+ }
91
+ }
92
+ }
93
+ else if (ts.canHaveModifiers(statement) &&
94
+ ts.getModifiers(statement)?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
95
+ const isDefault = ts.getModifiers(statement)?.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword);
96
+ if (isDefault) {
97
+ if (key === 'default')
98
+ explicit.push(refused);
99
+ continue;
100
+ }
101
+ if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement) || ts.isEnumDeclaration(statement)) &&
102
+ statement.name?.text === key)
103
+ explicit.push(binding(key));
104
+ else if (ts.isVariableStatement(statement) &&
105
+ statement.declarationList.declarations.some((one) => ts.isIdentifier(one.name) && one.name.text === key))
106
+ explicit.push(binding(key));
107
+ }
108
+ }
109
+ const combine = (answers) => {
110
+ let result = null;
111
+ for (const answer of answers) {
112
+ if (answer.kind === 'refused')
113
+ return refused;
114
+ if (answer.kind === 'missing')
115
+ continue;
116
+ if (result && !same(result, answer.value))
117
+ return refused;
118
+ result = answer.value;
119
+ }
120
+ return result ? { kind: 'binding', value: result } : missing;
121
+ };
122
+ // An explicit export shadows every star, including an ambiguous star.
123
+ if (explicit.length)
124
+ return combine(explicit);
125
+ if (key === 'default')
126
+ return missing;
127
+ return combine(stars.map((statement) => {
128
+ if (!statement.moduleSpecifier || !ts.isStringLiteralLike(statement.moduleSpecifier))
129
+ return refused;
130
+ const dependency = unit.resolveSource?.(statement.moduleSpecifier.text);
131
+ return dependency ? visit(dependency, key, path) : refused;
132
+ }));
133
+ };
134
+ return visit(module, name, new Set());
135
+ };
136
+ export const resolveSourceTransformExport = (module, name) => {
137
+ const answer = sourceTransformExportAnswer(module, name);
138
+ return answer.kind === 'binding' ? answer.value : null;
139
+ };
140
+ /** Preserve the existing resolver's distinction between an absent runtime
141
+ * export and unsupported/ambiguous transport. Absence alone can prune an
142
+ * export effect; a refusal never proves absence or authenticates a value.
143
+ * @semanticCategory generic-primitive
144
+ */
145
+ export const sourceTransformExportStatus = (module, name) => sourceTransformExportAnswer(module, name).kind;
@@ -19,6 +19,28 @@ export interface SourceTransformOutput {
19
19
  readonly text: string;
20
20
  readonly protocols: readonly SourceTransformProtocol[];
21
21
  }
22
+ /** Original implementation selected by this frontend's module authority. This
23
+ * is provenance, not permission to claim a library protocol by package name. */
24
+ export interface SourceTransformModule {
25
+ readonly fileName: string;
26
+ readonly text: string;
27
+ readonly declarationFileName: string | undefined;
28
+ /** Resolve an import of this selected module through the same frontend. */
29
+ readonly resolveSource?: SourceTransformResolver;
30
+ /** Canonical source binding only; the final checker must authenticate symbols. */
31
+ readonly resolveExport?: (name: string) => SourceTransformExport | null;
32
+ readonly packageManifest: {
33
+ readonly fileName: string;
34
+ readonly text: string;
35
+ } | null;
36
+ }
37
+ export interface SourceTransformExport {
38
+ readonly module: SourceTransformModule;
39
+ readonly localName: string;
40
+ /** The terminal module may export a private local under a different name. */
41
+ readonly exportedName: string;
42
+ }
43
+ export type SourceTransformResolver = (specifier: string) => SourceTransformModule | null;
22
44
  export type SourceTransform = (input: {
23
45
  readonly fileName: string;
24
46
  readonly text: string;
@@ -77,6 +77,24 @@ const fileReassignsConstructor = (file) => {
77
77
  * the JSDoc cast; a TypeScript class reaches the same rule through `as`.
78
78
  */
79
79
  const castTo = (fileName, expression, type) => fileName.endsWith('.ts') || fileName.endsWith('.tsx') ? `(${expression} as ${type})` : `(/** @type {${type}} */ (${expression}))`;
80
+ /**
81
+ * The construct signature the cast states: exactly the frame this `new`
82
+ * supplies. Its argument count is a syntactic fact of the site -- unless an
83
+ * argument is spread -- and the cast's parameters become the dispatching
84
+ * constructor's frame, which every candidate class's constructor is adapted
85
+ * from. A rest `any[]` frame claims the site can hand any candidate parameter
86
+ * a dynamic value, so `new this.constructor()` -- which supplies none, and
87
+ * whose omitted parameters every candidate receives as `undefined` -- would
88
+ * demand a dynamic reader for each candidate's parameter carrier
89
+ * (`ShaderMaterial`'s `parameters` record union) the call never exercises.
90
+ * Each supplied argument stays `any`: only its count is a fact here.
91
+ */
92
+ const constructFrameOf = (node) => {
93
+ const supplied = node.arguments ?? [];
94
+ if (supplied.some((argument) => ts.isSpreadElement(argument)))
95
+ return 'new (...args: any[]) => this';
96
+ return `new (${supplied.map((_, position) => `arg${position}: any`).join(', ')}) => this`;
97
+ };
80
98
  const candidatesIn = (file) => {
81
99
  const found = [];
82
100
  const visit = (node) => {
@@ -84,7 +102,7 @@ const candidatesIn = (file) => {
84
102
  found.push({
85
103
  calleeStart: node.expression.getStart(file),
86
104
  calleeEnd: node.expression.end,
87
- replacement: castTo(file.fileName, node.expression.getText(file), 'new (...args: any[]) => this')
105
+ replacement: castTo(file.fileName, node.expression.getText(file), constructFrameOf(node))
88
106
  });
89
107
  }
90
108
  ts.forEachChild(node, visit);
@@ -317,6 +317,8 @@ export declare function reachableClassMethodsOf(classes: ReadonlyMap<Declaration
317
317
  * `translation-unit.ts`).
318
318
  */
319
319
  export interface ClassStaticFieldStorage {
320
+ /** Typed own-property view, instantiated per evaluated constructor state. */
321
+ readonly viewStruct: string;
320
322
  readonly name: string;
321
323
  readonly storageName?: string;
322
324
  readonly representation: Representation;