@geastack/compiler 1.0.49 → 1.0.63

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 (125) hide show
  1. package/README.md +57 -19
  2. package/dist/cli.js +2 -1
  3. package/dist/compiler.js +8 -2
  4. package/dist/conversion/derive.js +5 -0
  5. package/dist/conversion/native-field-view.d.ts +7 -0
  6. package/dist/conversion/native-field-view.js +50 -0
  7. package/dist/conversion/native-record-products.js +1 -0
  8. package/dist/conversion/native-view-origins.d.ts +1 -1
  9. package/dist/conversion/native-view-origins.js +5 -4
  10. package/dist/conversion/nodes.d.ts +9 -0
  11. package/dist/conversion/nodes.js +18 -0
  12. package/dist/conversion/record-view.d.ts +25 -1
  13. package/dist/conversion/record-view.js +61 -2
  14. package/dist/conversion/structural-plan.js +7 -6
  15. package/dist/ir/borrowed-field-reads.d.ts +30 -0
  16. package/dist/ir/borrowed-field-reads.js +218 -0
  17. package/dist/ir/borrowed-window-cells.d.ts +35 -0
  18. package/dist/ir/borrowed-window-cells.js +117 -0
  19. package/dist/ir/call-dispatch.js +21 -4
  20. package/dist/ir/callable-class-flow.js +269 -43
  21. package/dist/ir/captures.js +18 -1
  22. package/dist/ir/certify.js +14 -1
  23. package/dist/ir/construction-only-fields.d.ts +15 -1
  24. package/dist/ir/construction-only-fields.js +71 -3
  25. package/dist/ir/dead-logical-merge.d.ts +4 -0
  26. package/dist/ir/dead-logical-merge.js +89 -18
  27. package/dist/ir/dense-loops.d.ts +38 -4
  28. package/dist/ir/dense-loops.js +120 -18
  29. package/dist/ir/dense-read-windows.d.ts +42 -0
  30. package/dist/ir/dense-read-windows.js +137 -0
  31. package/dist/ir/facts.d.ts +12 -0
  32. package/dist/ir/facts.js +18 -2
  33. package/dist/ir/field-read-reuse.d.ts +71 -0
  34. package/dist/ir/field-read-reuse.js +430 -0
  35. package/dist/ir/hoist.d.ts +19 -2
  36. package/dist/ir/hoist.js +143 -24
  37. package/dist/ir/lower-narrow.js +19 -1
  38. package/dist/ir/model.d.ts +19 -0
  39. package/dist/ir/native-field-view-domains.d.ts +12 -2
  40. package/dist/ir/native-field-view-domains.js +51 -15
  41. package/dist/ir/native-field-view-facts.js +5 -2
  42. package/dist/ir/native-method-frames.js +15 -2
  43. package/dist/ir/plain-field-read.d.ts +31 -0
  44. package/dist/ir/plain-field-read.js +51 -0
  45. package/dist/ir/program-facts.d.ts +14 -0
  46. package/dist/ir/program-facts.js +85 -2
  47. package/dist/ir/proven-branches.js +8 -1
  48. package/dist/ir/publish-conversion-recipes.js +5 -1
  49. package/dist/ir/short-circuit-threading.d.ts +12 -4
  50. package/dist/ir/short-circuit-threading.js +124 -12
  51. package/dist/ir/stable-cell-reads.d.ts +24 -1
  52. package/dist/ir/stable-cell-reads.js +65 -42
  53. package/dist/ir/write-effects.d.ts +42 -0
  54. package/dist/ir/write-effects.js +208 -0
  55. package/dist/plugins/gea/contract.d.ts +3 -2
  56. package/dist/plugins/gea/contract.js +3 -1
  57. package/dist/plugins/gea/host.d.ts +7 -2
  58. package/dist/plugins/gea/host.js +25 -5
  59. package/dist/project-preparation.d.ts +11 -0
  60. package/dist/project-preparation.js +44 -0
  61. package/dist/projection/classes.d.ts +8 -0
  62. package/dist/projection/classes.js +112 -12
  63. package/dist/projection/dispatch.d.ts +24 -4
  64. package/dist/projection/dispatch.js +71 -2
  65. package/dist/projection/fields.js +2 -2
  66. package/dist/representation/model.d.ts +3 -0
  67. package/dist/representation/model.js +3 -0
  68. package/dist/representation/value-records.js +22 -0
  69. package/dist/semantics/frontend-timing.d.ts +9 -0
  70. package/dist/semantics/frontend-timing.js +43 -2
  71. package/dist/semantics/frontend.js +103 -7
  72. package/dist/semantics/model/operations.d.ts +7 -0
  73. package/dist/semantics/normalize/binding-fixpoint.js +7 -2
  74. package/dist/semantics/normalize/callable-data-write.js +6 -2
  75. package/dist/semantics/normalize/field-bindings.js +151 -111
  76. package/dist/semantics/normalize/flow/callable-reach.js +27 -0
  77. package/dist/semantics/normalize/flow/component-solver.js +23 -5
  78. package/dist/semantics/normalize/flow/source-record-data.js +6 -2
  79. package/dist/semantics/normalize/flow/source-value-session.d.ts +13 -0
  80. package/dist/semantics/normalize/flow/source-value-session.js +207 -65
  81. package/dist/semantics/normalize/global-host-mutations.js +162 -1
  82. package/dist/semantics/normalize/parameter-bindings.js +16 -1
  83. package/dist/semantics/normalize/producers/class-lifecycle.js +2 -1
  84. package/dist/semantics/normalize/producers/erasure.js +13 -8
  85. package/dist/semantics/subclass-member-overlay-transform.js +17 -4
  86. package/dist/semantics/typescript-tsgo.d.ts +20 -0
  87. package/dist/semantics/typescript-tsgo.js +832 -0
  88. package/dist/semantics/typescript.d.ts +0 -9
  89. package/dist/semantics/typescript.js +4 -1
  90. package/dist/targets/cpp/borrowed-call-entry.d.ts +9 -0
  91. package/dist/targets/cpp/borrowed-call-entry.js +12 -2
  92. package/dist/targets/cpp/captures.js +4 -0
  93. package/dist/targets/cpp/class-properties/computed-method-value.d.ts +1 -1
  94. package/dist/targets/cpp/class-properties/computed-method-value.js +17 -7
  95. package/dist/targets/cpp/class-properties/emit-class-properties.d.ts +8 -0
  96. package/dist/targets/cpp/class-properties/emit-class-properties.js +62 -2
  97. package/dist/targets/cpp/class-properties/native-method-read.js +12 -1
  98. package/dist/targets/cpp/conversions.js +1 -0
  99. package/dist/targets/cpp/emit-arrays.d.ts +5 -0
  100. package/dist/targets/cpp/emit-arrays.js +60 -10
  101. package/dist/targets/cpp/emit-binding-reference.d.ts +19 -6
  102. package/dist/targets/cpp/emit-binding-reference.js +26 -8
  103. package/dist/targets/cpp/emit-bindings.js +22 -0
  104. package/dist/targets/cpp/emit-callable.js +133 -16
  105. package/dist/targets/cpp/emit-carrier-members.d.ts +6 -0
  106. package/dist/targets/cpp/emit-carrier-members.js +24 -2
  107. package/dist/targets/cpp/emit-context.d.ts +97 -1
  108. package/dist/targets/cpp/emit-context.js +52 -0
  109. package/dist/targets/cpp/emit-equality.js +3 -0
  110. package/dist/targets/cpp/emit-narrowing.js +9 -0
  111. package/dist/targets/cpp/emit-native-field-view.d.ts +24 -0
  112. package/dist/targets/cpp/emit-native-field-view.js +87 -8
  113. package/dist/targets/cpp/emit-properties.js +139 -16
  114. package/dist/targets/cpp/emit-record-view.js +49 -22
  115. package/dist/targets/cpp/emit-return.js +5 -1
  116. package/dist/targets/cpp/emit-tonumber.js +4 -1
  117. package/dist/targets/cpp/emit-union-properties.js +1 -1
  118. package/dist/targets/cpp/emit.d.ts +4 -1
  119. package/dist/targets/cpp/emit.js +148 -6
  120. package/dist/targets/cpp/reactive-dependencies.js +100 -28
  121. package/dist/targets/cpp/translation-unit.js +71 -17
  122. package/dist/targets/cpp/virtual-methods.d.ts +2 -2
  123. package/dist/targets/cpp/virtual-methods.js +11 -3
  124. package/package.json +1 -1
  125. package/src/targets/cpp/runtime/gea_runtime.h +400 -21
package/dist/ir/hoist.js CHANGED
@@ -1,5 +1,8 @@
1
1
  import { controlFlowGraphOf, cyclicBlocksOf, dominatorTreeOf, naturalLoopsOf } from './dominance.js';
2
+ import { borrowSafeOperationsOf } from './borrow-effects.js';
3
+ import { codeFreeGetOf } from './plain-field-read.js';
2
4
  import { operandsOfIrOperation, resultOfIrOperation } from './queries.js';
5
+ import { representationKey } from '../representation/model.js';
3
6
  export const emptyHoistPlan = { into: new Map(), relocated: new Set() };
4
7
  /** The operators whose C++ spelling reads nothing and traps on nothing. */
5
8
  const pureOperators = new Set([
@@ -37,13 +40,23 @@ const isNumberWidthConversion = (operation) => operation.kind === 'convert' &&
37
40
  operation.source.representation.domain === 'number' &&
38
41
  operation.result.representation.kind === 'scalar' &&
39
42
  operation.result.representation.domain === 'number';
43
+ /**
44
+ * A value put into an optional of its own carrier: `hitBox = box` stores a
45
+ * box into a `CollisionBox | null`. The payload is already the right carrier,
46
+ * so the conversion only marks it present and reaches no user code.
47
+ */
48
+ const isPresenceConversion = (operation) => operation.kind === 'convert' &&
49
+ operation.result.representation.kind === 'optional' &&
50
+ representationKey(operation.result.representation.payload) === representationKey(operation.source.representation);
40
51
  /**
41
52
  * The operation kinds that cannot change what an array answers.
42
53
  *
43
54
  * Deliberately a whitelist: a kind absent here blocks the move, so a kind
44
55
  * added to the IR later is fail-closed rather than silently admitted. `set`,
45
56
  * `convert` and every allocation are out on purpose -- a store writes array
46
- * storage, and a conversion of an unknown value can reach user code.
57
+ * storage, and a conversion of an unknown value can reach user code. A `phi`
58
+ * only names which incoming value a merge holds. A `get` is out too: a getter
59
+ * runs program code, so each read asks `codeFreeGet`.
47
60
  */
48
61
  const storageNeutralKinds = new Set([
49
62
  'constant',
@@ -52,8 +65,50 @@ const storageNeutralKinds = new Set([
52
65
  'binding-write',
53
66
  'parameter',
54
67
  'receiver',
55
- 'get'
68
+ 'phi'
56
69
  ]);
70
+ /**
71
+ * Whether anything in a set of blocks can change what an array answers: a
72
+ * store, a call, an allocation -- any operation outside the whitelist below.
73
+ *
74
+ * A store into a record's data field and a pure `Math` call over Numbers run
75
+ * none of the program's code and touch no array: a capsule mover clamps with
76
+ * `Math.max(box.min.x, Math.min(box.max.x, p.x))` and pushes `position.x` out
77
+ * of each box, and either one kept its box loop from checking `boxes` once.
78
+ * `codeFreeStore` is the target's answer to which stores those are.
79
+ *
80
+ * `codeFreeGet` says which property reads run none of the program's code
81
+ * (`codeFreeGetOf`). A getter is a read too, and it can push onto the array
82
+ * the loop walks: trusting every read hoisted a loop's `boxes.length` past a
83
+ * getter that grew `boxes`, so the loop stopped at the length it began with,
84
+ * and a push that moved the storage left its window reading freed cells.
85
+ */
86
+ export const arrayStorageMutationOf = (body, codeFreeStore, codeFreeGet) => {
87
+ let neutralCalls = null;
88
+ const touchesNoArray = (operation) => {
89
+ if (operation.kind === 'get')
90
+ return codeFreeGet(operation);
91
+ if (storageNeutralKinds.has(operation.kind) || isNumberWidthConversion(operation) || isPresenceConversion(operation))
92
+ return true;
93
+ if (operation.kind === 'set')
94
+ return codeFreeStore(operation);
95
+ if (operation.kind !== 'call')
96
+ return false;
97
+ neutralCalls ??= borrowSafeOperationsOf(body);
98
+ return neutralCalls.has(operation);
99
+ };
100
+ return (blocks) => {
101
+ for (const blockId of blocks) {
102
+ const block = body.blocks.get(blockId);
103
+ if (!block)
104
+ continue;
105
+ for (const operation of block.operations)
106
+ if (!touchesNoArray(operation))
107
+ return true;
108
+ }
109
+ return false;
110
+ };
111
+ };
57
112
  /**
58
113
  * @param programConstants cells written exactly once program-wide, with that
59
114
  * integer literal (`ir/integers.ts`'s `programCellConstantsOf`). A module
@@ -63,7 +118,7 @@ const storageNeutralKinds = new Set([
63
118
  * in software doubles on a core with no double FPU, and `dense-loops.ts`
64
119
  * refused the window because its bound was defined inside the loop.
65
120
  */
66
- export const loopInvariantHoistsOf = (body, programConstants = new Map(), copiedCapture = () => false) => {
121
+ export const loopInvariantHoistsOf = (body, programConstants = new Map(), copiedCapture = () => false, settledCapture = () => false, codeFreeStore = () => false, codeFreeGet = codeFreeGetOf(body, () => false, () => false)) => {
67
122
  // A try region renders as one `try { } catch { }` chunk assembled from
68
123
  // several blocks at once, so a relocation into or out of one has no single
69
124
  // place to land. Bodies with a region are left alone entirely.
@@ -222,18 +277,7 @@ export const loopInvariantHoistsOf = (body, programConstants = new Map(), copied
222
277
  * of another; regaining that needs an alias proof (distinct allocation sites),
223
278
  * not a weaker invariance test.
224
279
  */
225
- const mutatesArrayStorage = (blocks) => {
226
- for (const blockId of blocks) {
227
- const block = body.blocks.get(blockId);
228
- if (!block)
229
- continue;
230
- for (const operation of block.operations) {
231
- if (!storageNeutralKinds.has(operation.kind) && !isNumberWidthConversion(operation))
232
- return true;
233
- }
234
- }
235
- return false;
236
- };
280
+ const mutatesArrayStorage = arrayStorageMutationOf(body, codeFreeStore, codeFreeGet);
237
281
  /**
238
282
  * Whether every operation in these blocks is one that cannot hand control to
239
283
  * other code: no call or construction, no property access that could reach
@@ -268,12 +312,83 @@ export const loopInvariantHoistsOf = (body, programConstants = new Map(), copied
268
312
  };
269
313
  /** Answered once per loop: the walk is over every operation the loop owns. */
270
314
  const arrayStorageMutated = new Map();
315
+ const storageMutatedBy = (loop) => {
316
+ const mutated = arrayStorageMutated.get(loop.header) ?? mutatesArrayStorage(loop.blocks);
317
+ arrayStorageMutated.set(loop.header, mutated);
318
+ return mutated;
319
+ };
320
+ /**
321
+ * The field keys a loop stores through, or `null` when it may store any:
322
+ * a computed key, a call that is not a pure native one, a read that can run
323
+ * a getter. An element store into an Array or a typed view writes no field.
324
+ */
325
+ const storedKeys = new WeakMap();
326
+ let neutralCalls = null;
327
+ const fieldKeysStoredBy = (blocks) => {
328
+ if (storedKeys.has(blocks))
329
+ return storedKeys.get(blocks) ?? null;
330
+ const keys = new Set();
331
+ const scan = () => {
332
+ for (const blockId of blocks)
333
+ for (const operation of body.blocks.get(blockId)?.operations ?? []) {
334
+ if (storageNeutralKinds.has(operation.kind) || isNumberWidthConversion(operation) || isPresenceConversion(operation))
335
+ continue;
336
+ if (operation.kind === 'get') {
337
+ if (codeFreeGet(operation))
338
+ continue;
339
+ return null;
340
+ }
341
+ if (operation.kind === 'set') {
342
+ const receiver = operation.receiver.representation.kind;
343
+ if (receiver === 'typed-array' || receiver === 'array-object')
344
+ continue;
345
+ const text = constantTexts.get(operation.key.value);
346
+ if (text === undefined)
347
+ return null;
348
+ keys.add(text);
349
+ continue;
350
+ }
351
+ if (operation.kind === 'call') {
352
+ neutralCalls ??= borrowSafeOperationsOf(body);
353
+ if (neutralCalls.has(operation))
354
+ continue;
355
+ }
356
+ return null;
357
+ }
358
+ return keys;
359
+ };
360
+ const found = scan();
361
+ storedKeys.set(blocks, found);
362
+ return found;
363
+ };
364
+ /**
365
+ * A data field read the loop cannot change: no getter answers it
366
+ * (`codeFreeGet`) and nothing in the loop stores that key. A quad writer
367
+ * reads its corner arrays off `this` on every turn, and each read stood
368
+ * between the typed view and a window over it. A field is not array storage,
369
+ * so a loop that writes arrays may still move it.
370
+ */
371
+ const settledFieldRead = (operation, blocks) => {
372
+ if (operation.kind !== 'get')
373
+ return false;
374
+ const receiver = operation.receiver.representation.kind;
375
+ if (receiver !== 'class-ref' && receiver !== 'record' && receiver !== 'native-record-ref')
376
+ return false;
377
+ const key = constantTexts.get(operation.key.value);
378
+ if (key === undefined || !codeFreeGet(operation))
379
+ return false;
380
+ const stored = fieldKeysStoredBy(blocks);
381
+ return stored !== null && !stored.has(key);
382
+ };
271
383
  const relocated = new Set();
272
384
  const into = new Map();
273
385
  /** Whether this operation reads only, and reads only things that cannot move. */
274
386
  const isPureRead = (operation, blocks) => {
275
387
  if (operation.kind === 'constant')
276
388
  return true;
389
+ // The body's receiver names one object for the whole call.
390
+ if (operation.kind === 'receiver')
391
+ return true;
277
392
  if (operation.kind === 'compute') {
278
393
  if (!pureOperators.has(operation.operator))
279
394
  return false;
@@ -301,7 +416,7 @@ export const loopInvariantHoistsOf = (body, programConstants = new Map(), copied
301
416
  // Array elements and immutable string builtins: a record or class receiver can
302
417
  // reach an accessor, which is a call, and a call is not something this
303
418
  // may run an extra time or in a different order.
304
- return operation.receiver.representation.kind === 'array-object' || immutableStringRead(operation);
419
+ return (operation.receiver.representation.kind === 'array-object' || immutableStringRead(operation) || settledFieldRead(operation, blocks));
305
420
  }
306
421
  return false;
307
422
  };
@@ -347,12 +462,11 @@ export const loopInvariantHoistsOf = (body, programConstants = new Map(), copied
347
462
  // An element load and a `length` read both answer from storage this
348
463
  // loop may rewrite. `isPureRead` proved the handle invariant; this
349
464
  // proves the bytes behind it are too.
350
- if (operation.kind === 'get' && !immutableStringRead(operation)) {
351
- const mutated = arrayStorageMutated.get(loop.header) ?? mutatesArrayStorage(loop.blocks);
352
- arrayStorageMutated.set(loop.header, mutated);
353
- if (mutated)
354
- continue;
355
- }
465
+ if (operation.kind === 'get' &&
466
+ !immutableStringRead(operation) &&
467
+ !settledFieldRead(operation, loop.blocks) &&
468
+ storageMutatedBy(loop))
469
+ continue;
356
470
  // A cell read moved ahead of the loop has to name a cell something
357
471
  // already assigned; a write that only happens AFTER the loop leaves
358
472
  // the variable uninitialized at the new position.
@@ -362,11 +476,16 @@ export const loopInvariantHoistsOf = (body, programConstants = new Map(), copied
362
476
  // none reads the same value on every turn as at its preheader's end,
363
477
  // and a capture this body holds its own copy of reads the same value
364
478
  // whatever the loop calls: a row loop calling a helper otherwise re-read
365
- // its captured bound on every turn.
479
+ // its captured bound on every turn. A settled box holds one value
480
+ // from its one write on, but that write can run inside the loop, in
481
+ // code the loop calls, after the preheader read: only a loop that
482
+ // changes no storage reads it early.
366
483
  if (operation.kind === 'binding-read' && !isProgramConstant(operation.declaration)) {
367
484
  const written = cellWrites.get(operation.declaration) ?? [];
368
485
  const assigned = written.length === 0
369
- ? copiedCapture(operation.declaration) || runsNoOtherCode(loop.blocks)
486
+ ? copiedCapture(operation.declaration) ||
487
+ runsNoOtherCode(loop.blocks) ||
488
+ (settledCapture(operation.declaration) && !storageMutatedBy(loop))
370
489
  : written.some((write) => dominance.dominates(write.block, preheader));
371
490
  if (!assigned)
372
491
  continue;
@@ -1,4 +1,4 @@
1
- import { carriesMergeAbsence, carriesNoAbsence, carriesUndefined, contributesOnlyAbsence, contributesNoTruthiness, isBooleanShapedMergeTarget, isDeadMergeContribution, mergesAsTruthiness, partialDeadMergeArms, representationKey } from '../representation/model.js';
1
+ import { carriesMergeAbsence, carriesNoAbsence, carriesUndefined, contributesOnlyAbsence, contributesNoTruthiness, isBooleanShapedMergeTarget, isDeadMergeContribution, mergesAsTruthiness, objectCarrierWithOnlyNativeUndefined, partialDeadMergeArms, representationKey } from '../representation/model.js';
2
2
  import { mergeFalsyAbsence, mergeMaterialization, mergeTaggedAbsence } from '../representation/merge.js';
3
3
  import { operandOf } from '../semantics/model/operands.js';
4
4
  import { IrLoweringBlockedError } from './lower-graph.js';
@@ -400,5 +400,23 @@ export const mergeIncoming = (ctx, block, lineage, operation, role, operand, rep
400
400
  return convertOrDrift(ctx, block, lineage, operation.id, role, operand.ordinal, dead, representation);
401
401
  }
402
402
  }
403
+ // The `&&` left is an Object carrier whose only falsy state is the native
404
+ // undefined lane (a shared record reference), and the merge has nowhere to
405
+ // put an `undefined`: `api && api.version` over `const api = typeof v ===
406
+ // 'string' ? { version: v } as ServerApi : v as ServerApi`. The lane is
407
+ // filled only by a strict unbound receiver, so whether this arm can run is a
408
+ // question about where the left value came from -- a fresh allocation or a
409
+ // checked assertion out of a box never fills it. That proof is taken over
410
+ // the finished body by certification (`dead-logical-merge.ts`'s
411
+ // `objectValued`), and a left it cannot prove refuses there by name.
412
+ // Asked last, so it only ever replaces this refusal.
413
+ if (role === 'kept' &&
414
+ operation.family === 'computation' &&
415
+ operation.form === 'logical' &&
416
+ operation.operator === '&&' &&
417
+ objectCarrierWithOnlyNativeUndefined(incoming.representation) &&
418
+ !carriesMergeAbsence(representation)) {
419
+ return { value: ctx.builder.deadLogicalMergeValue(block, lineage, incoming, representation), representation };
420
+ }
403
421
  throw blockedMergeArm(operation, role, incoming.representation, representation);
404
422
  };
@@ -451,6 +451,17 @@ export interface CallOperation extends IrOperationBase {
451
451
  readonly functionId: FunctionId;
452
452
  readonly key: string;
453
453
  };
454
+ /**
455
+ * The method key a write through a dynamic receiver may have shadowed on
456
+ * this call's receiver (`classDynamicMethodShadowOf`). Such a shadow has no
457
+ * slot: it lives in the instance's native expando table, so the emitter
458
+ * tests the instance's expando bit -- clear on every instance nothing wrote
459
+ * through a dynamic receiver -- and calls the shadow only when the table
460
+ * holds the key. The call is otherwise resolved as if no shadow could exist.
461
+ */
462
+ readonly expandoShadowGuard?: {
463
+ readonly key: string;
464
+ };
454
465
  /**
455
466
  * The compiler-owned body identity of this call's callee, when the same
456
467
  * producer/member resolution used by `target` proved it without requiring
@@ -1701,6 +1712,14 @@ export interface IrBodyFacts {
1701
1712
  * shared cell before its first rendered block rather than at the write.
1702
1713
  */
1703
1714
  readonly requiresEarlyBox: ReadonlySet<DeclarationId>;
1715
+ /**
1716
+ * The `boxed` declarations the program writes exactly once, outside every
1717
+ * loop. They are boxed
1718
+ * only because a closure can run before that write -- a hoisted function
1719
+ * declaration reading a `const` declared below it -- so once a read has
1720
+ * seen the value, no later read sees another.
1721
+ */
1722
+ readonly settledBoxes?: ReadonlySet<DeclarationId>;
1704
1723
  /**
1705
1724
  * The recursion group this body is a member of (`IrCaptureGroup`), absent
1706
1725
  * for every body that is not one. A member's `capturedDeclarations` and
@@ -57,7 +57,7 @@ export declare const nativeFieldViewPlansByTargetOf: (nodes: Iterable<Conversion
57
57
  * slot, accessor or installed descriptor along the way declares: a store of
58
58
  * one of those takes its route, never a home.
59
59
  */
60
- export declare const nativeFieldViewOpenWriteHomesOf: (receiver: Representation, plans: ReadonlyMap<string, readonly NativeFieldViewPlan[]>, layoutOf: Pick<RepresentationDeriver, "layoutOf"> | null | undefined) => {
60
+ export declare const nativeFieldViewOpenWriteHomesOf: (receiver: Representation, plans: ReadonlyMap<string, readonly NativeFieldViewPlan[]>, layoutOf: Pick<RepresentationDeriver, "layoutOf"> | null | undefined, sourcePlansOf?: (plan: NativeFieldViewPlan) => ReadonlySet<NativeFieldViewPlan> | null) => {
61
61
  readonly homes: readonly Representation[];
62
62
  readonly named: ReadonlySet<string>;
63
63
  } | null;
@@ -70,4 +70,14 @@ export declare const nativeFieldViewOpenWriteHomesOf: (receiver: Representation,
70
70
  export declare const nativeFieldViewDomainsOf: (nodes: Iterable<ConversionNode>, layoutOf: Pick<RepresentationDeriver, "layoutOf"> | null | undefined, abiOf: (id: FunctionId) => CallableAbi | null, classes?: ReadonlyMap<DeclarationId, ClassLayout>,
71
71
  /** The native carriers copies store under `key` in the expando of an
72
72
  * allocation carried as `shapeId` (`spread-conversions.ts`'s `expandoData`). */
73
- expandoStoredOf?: (shapeId: string, key: string | null) => readonly Representation[]) => NativeFieldViewDomains;
73
+ expandoStoredOf?: (shapeId: string, key: string | null) => readonly Representation[],
74
+ /**
75
+ * The installed plans whose views can be the very value `plan` is
76
+ * installed over, or `null` when the program does not bound them. A plan's
77
+ * target names a SHAPE, and every value carried at that shape -- the empty
78
+ * `{}` above all -- is a source of each plan from it; following a plan's
79
+ * source only through the views that actually reach its conversions keeps
80
+ * an unrelated view of the same shape (an all-string deny list declared at
81
+ * a keyless mapped type) out of every options bag a `{}` default fills.
82
+ */
83
+ sourcePlansOf?: (plan: NativeFieldViewPlan) => ReadonlySet<NativeFieldViewPlan> | null) => NativeFieldViewDomains;
@@ -33,9 +33,9 @@ export const nativeFieldViewPlansByTargetOf = (nodes) => {
33
33
  * slot, accessor or installed descriptor along the way declares: a store of
34
34
  * one of those takes its route, never a home.
35
35
  */
36
- export const nativeFieldViewOpenWriteHomesOf = (receiver, plans, layoutOf) => {
36
+ export const nativeFieldViewOpenWriteHomesOf = (receiver, plans, layoutOf, sourcePlansOf = () => null) => {
37
37
  if (receiver.kind === 'optional' || receiver.kind === 'borrowed-ref')
38
- return nativeFieldViewOpenWriteHomesOf(receiver.kind === 'optional' ? receiver.payload : receiver.referent, plans, layoutOf);
38
+ return nativeFieldViewOpenWriteHomesOf(receiver.kind === 'optional' ? receiver.payload : receiver.referent, plans, layoutOf, sourcePlansOf);
39
39
  if (receiver.kind !== 'record' && receiver.kind !== 'record-with-index' && receiver.kind !== 'native-record-ref')
40
40
  return null;
41
41
  if (receiver.ownership !== 'shared-refcount')
@@ -57,14 +57,22 @@ export const nativeFieldViewOpenWriteHomesOf = (receiver, plans, layoutOf) => {
57
57
  return { homes: [], named };
58
58
  }
59
59
  const homes = [];
60
- const pending = [receiver];
60
+ // Each carrier is walked under the plan whose source it is: only the views
61
+ // that can be the very value that plan is installed over lead further
62
+ // (`nativeFieldViewDomainsOf`'s `sourcePlansOf`).
63
+ const pending = [{ carrier: receiver, via: null }];
61
64
  const seen = new Set();
65
+ const numbers = new Map();
62
66
  while (pending.length > 0) {
63
- const carrier = pending.pop();
67
+ const { carrier, via } = pending.pop();
68
+ const reaching = via === null ? null : sourcePlansOf(via);
69
+ let number = via === null || reaching === null ? -1 : numbers.get(via);
70
+ if (number === undefined)
71
+ numbers.set(via, (number = numbers.size));
64
72
  const identity = representationKey(carrier);
65
- if (seen.has(identity))
73
+ if (seen.has(`${number} ${identity}`))
66
74
  continue;
67
- seen.add(identity);
75
+ seen.add(`${number} ${identity}`);
68
76
  if ((carrier.kind !== 'record' && carrier.kind !== 'native-record-ref') || carrier.ownership !== 'shared-refcount')
69
77
  return null;
70
78
  const ordinary = carrier.kind === 'native-record-ref'
@@ -74,13 +82,16 @@ export const nativeFieldViewOpenWriteHomesOf = (receiver, plans, layoutOf) => {
74
82
  : carrier;
75
83
  if (ordinary?.kind !== 'record')
76
84
  return null;
77
- homes.push(carrier);
85
+ if (!homes.some((home) => representationKey(home) === identity))
86
+ homes.push(carrier);
78
87
  name(ordinary.fields);
79
88
  name(ordinary.accessors);
80
89
  for (const plan of plans.get(carrier.shapeId) ?? []) {
90
+ if (reaching !== null && !reaching.has(plan))
91
+ continue;
81
92
  name(plan.fields);
82
93
  if (!isDocumentPlan(plan))
83
- pending.push(plan.source);
94
+ pending.push({ carrier: plan.source, via: plan });
84
95
  }
85
96
  }
86
97
  return { homes, named };
@@ -94,7 +105,17 @@ export const nativeFieldViewOpenWriteHomesOf = (receiver, plans, layoutOf) => {
94
105
  export const nativeFieldViewDomainsOf = (nodes, layoutOf, abiOf, classes = new Map(),
95
106
  /** The native carriers copies store under `key` in the expando of an
96
107
  * allocation carried as `shapeId` (`spread-conversions.ts`'s `expandoData`). */
97
- expandoStoredOf = () => []) => {
108
+ expandoStoredOf = () => [],
109
+ /**
110
+ * The installed plans whose views can be the very value `plan` is
111
+ * installed over, or `null` when the program does not bound them. A plan's
112
+ * target names a SHAPE, and every value carried at that shape -- the empty
113
+ * `{}` above all -- is a source of each plan from it; following a plan's
114
+ * source only through the views that actually reach its conversions keeps
115
+ * an unrelated view of the same shape (an all-string deny list declared at
116
+ * a keyless mapped type) out of every options bag a `{}` default fills.
117
+ */
118
+ sourcePlansOf = () => null) => {
98
119
  const plans = new Map();
99
120
  for (const plan of nativeFieldViewPlansOf(nodes)) {
100
121
  const entries = plans.get(plan.target.shapeId) ?? [];
@@ -103,6 +124,21 @@ expandoStoredOf = () => []) => {
103
124
  plans.set(plan.target.shapeId, entries);
104
125
  }
105
126
  const cache = new Map();
127
+ const planNumbers = new Map();
128
+ const planNumberOf = (plan) => {
129
+ let number = planNumbers.get(plan);
130
+ if (number === undefined)
131
+ planNumbers.set(plan, (number = planNumbers.size));
132
+ return number;
133
+ };
134
+ /** The plans installed at `shapeId` that a value reached through `via`'s source can carry. */
135
+ const installedFor = (shapeId, via) => {
136
+ const all = plans.get(shapeId);
137
+ if (via === null || all === undefined)
138
+ return all;
139
+ const reaching = sourcePlansOf(via);
140
+ return reaching === null ? all : all.filter((plan) => reaching.has(plan));
141
+ };
106
142
  /**
107
143
  * Whether no allocation this carrier can hold has `key` as an own slot,
108
144
  * accessor, class member, or descriptor route -- so its ordinary [[Get]]
@@ -171,12 +207,12 @@ expandoStoredOf = () => []) => {
171
207
  return (ordinary.fields.some((field) => field.key === key) ||
172
208
  (ordinary.kind === 'record' && ordinary.accessors.some((accessor) => accessor.key === key)));
173
209
  };
174
- const domainsOf = (receiver, ordinaryArm = false) => {
175
- const identity = `${representationKey(receiver)}:${ordinaryArm ? 'ordinary' : 'live'}`;
210
+ const domainsOf = (receiver, ordinaryArm = false, via = null) => {
211
+ const identity = `${representationKey(receiver)}:${ordinaryArm ? 'ordinary' : 'live'}:${via === null ? '*' : planNumberOf(via)}`;
176
212
  if (cache.has(identity))
177
213
  return cache.get(identity) ?? null;
178
214
  if (receiver.kind === 'optional' || receiver.kind === 'borrowed-ref')
179
- return domainsOf(receiver.kind === 'optional' ? receiver.payload : receiver.referent, ordinaryArm);
215
+ return domainsOf(receiver.kind === 'optional' ? receiver.payload : receiver.referent, ordinaryArm, via);
180
216
  if (receiver.kind === 'tagged-union') {
181
217
  // A `dynamic` arm is already a `gea::Value` (`decorate(decrypted[k], ...)`
182
218
  // recursing with an `any` entry): its read is that Value's own [[Get]],
@@ -185,7 +221,7 @@ expandoStoredOf = () => []) => {
185
221
  const objectArms = receiver.arms.filter((arm) => arm.value.kind !== 'undefined' && arm.value.kind !== 'null' && arm.value.kind !== 'dynamic');
186
222
  if (!ordinaryArm && !objectArms.some((arm) => domainsOf(arm.value) !== null))
187
223
  return null;
188
- const arms = objectArms.map((arm) => domainsOf(arm.value, true));
224
+ const arms = objectArms.map((arm) => domainsOf(arm.value, true, via));
189
225
  if (!arms.length || arms.some((arm) => arm === null))
190
226
  return null;
191
227
  // A per-arm union, not an intersection: a key only one arm stores (a
@@ -287,7 +323,7 @@ expandoStoredOf = () => []) => {
287
323
  if ((receiver.kind !== 'record' && receiver.kind !== 'record-with-index' && receiver.kind !== 'native-record-ref') ||
288
324
  receiver.ownership !== 'shared-refcount')
289
325
  return null;
290
- const installed = plans.get(receiver.shapeId);
326
+ const installed = installedFor(receiver.shapeId, via);
291
327
  if (!ordinaryArm && !installed?.length)
292
328
  return null;
293
329
  // Install the accumulator before following a recursive view's source.
@@ -350,7 +386,7 @@ expandoStoredOf = () => []) => {
350
386
  // The public intermediate view may omit a key. Its native descriptor
351
387
  // still delegates that key to the immediate source, including a getter
352
388
  // whose Function identity has escaped.
353
- const inherited = domainsOf(plan.source, true);
389
+ const inherited = domainsOf(plan.source, true, plan);
354
390
  for (const [key, domains] of inherited ?? []) {
355
391
  const checked = plan.fields.find((field) => field.key === key)?.checkedRead;
356
392
  for (const domain of domains)
@@ -200,9 +200,12 @@ export const nativeFieldViewReadFor = (receiver, key, target, flow, conversions,
200
200
  // conversion, so the arm and the entry route cannot disagree.
201
201
  if (dynamicGet === true && sources.has(representationKey(source)))
202
202
  continue;
203
+ // A route whose carrier can never be the narrowed target (a `number` slot
204
+ // read where a guard on an earlier [[Get]] proved a class instance) is the
205
+ // property-read context `fieldReadFor` already answers: that route throws.
203
206
  const node = abiOfCallee(source) !== null && abiOfCallee(target) !== null
204
207
  ? (conversions.nativeMethodFor(source, target) ?? conversions.nodeFor(source, target))
205
- : conversions.nodeFor(source, target);
208
+ : conversions.fieldReadFor(source, target);
206
209
  const unguarded = (checkedRead === undefined
207
210
  ? null
208
211
  : representationKey(checkedRead.source) === representationKey(source) &&
@@ -217,7 +220,7 @@ export const nativeFieldViewReadFor = (receiver, key, target, flow, conversions,
217
220
  // guard proved, a TypeError for any other -- never an unchecked `get<k>`.
218
221
  const selected = recipeIsMaterializableWithoutPriorSourceGuard(unguarded, conversions.nodeById)
219
222
  ? unguarded
220
- : conversions.assertedUnionFor(source, target, false);
223
+ : (conversions.assertedUnionFor(source, target, false) ?? conversions.checkedClassFieldReadFor(source, target));
221
224
  if (selected === null || !recipeIsMaterializableWithoutPriorSourceGuard(selected, conversions.nodeById))
222
225
  return null;
223
226
  const previous = sources.get(representationKey(source));
@@ -1,5 +1,5 @@
1
1
  import { abiOfCallee } from '../projection/callee.js';
2
- import { classFamilyOverridesOf, classMethodValueArmsOf, classPrototypeMethodKeysOf, classPrototypeMethodValueArmsOf } from '../projection/dispatch.js';
2
+ import { classDynamicMethodShadowOf, classFamilyOverridesOf, classPresentMethodArmsOf, classPrototypeMethodKeysOf, classPrototypeMethodValueArmsOf } from '../projection/dispatch.js';
3
3
  import { classMemberOf, classMethodOverrideOf } from '../projection/fields.js';
4
4
  import { heldMethodCopyOf, publishedMethodCopyOf } from '../projection/method-values.js';
5
5
  import { nativeUnboundMethodContractOf } from '../conversion/native-method.js';
@@ -67,6 +67,11 @@ export const nativeMethodSourceInputsOf = (operation, keyText, classes, abis, bo
67
67
  return null;
68
68
  });
69
69
  const abiOf = (callable) => abis.get(callable) ?? null;
70
+ // A shadow a dynamic write installed is read out of the expando table as the
71
+ // slot would have held it: the method's own convention, without a receiver
72
+ // (`ClassLayout.dynamicMethodShadows`).
73
+ const shadowOf = (abi) => ({ kind: 'function-value-dispatch', abi: { ...abi, receiver: null } });
74
+ const shadows = [];
70
75
  const union = operation.receiver.representation.kind === 'tagged-union';
71
76
  for (const receiver of receiverArmsOf(operation.receiver.representation)) {
72
77
  if (receiver.kind !== 'class-ref')
@@ -89,6 +94,8 @@ export const nativeMethodSourceInputsOf = (operation, keyText, classes, abis, bo
89
94
  const own = classMethodOverrideOf(classes, arm.allocation, key);
90
95
  if (own !== null)
91
96
  add(key, null, 'own', own.value);
97
+ else if (abi !== null && classDynamicMethodShadowOf(classes, arm.allocation, key))
98
+ add(key, null, 'own', shadowOf(abi));
92
99
  }
93
100
  continue;
94
101
  }
@@ -96,7 +103,7 @@ export const nativeMethodSourceInputsOf = (operation, keyText, classes, abis, bo
96
103
  continue;
97
104
  const overridden = !staticDispatch && classFamilyOverridesOf(classes, receiver.declaration, key).length > 0;
98
105
  const methods = overridden
99
- ? (classMethodValueArmsOf(classes, receiver.declaration, key)?.map((arm) => arm.method) ?? [])
106
+ ? (classPresentMethodArmsOf(classes, receiver.declaration, key)?.map((arm) => arm.method) ?? [])
100
107
  : [member.method];
101
108
  for (const method of methods) {
102
109
  const selected = union
@@ -107,10 +114,16 @@ export const nativeMethodSourceInputsOf = (operation, keyText, classes, abis, bo
107
114
  const abi = selected.callable === null ? null : abiOf(selected.callable);
108
115
  if (abi !== null)
109
116
  add(key, selected.callable, 'prototype', { kind: 'function-value-dispatch', abi });
117
+ if (abi !== null && !staticDispatch && classDynamicMethodShadowOf(classes, receiver.declaration, key))
118
+ shadows.push(shadowOf(abi));
110
119
  }
111
120
  const own = staticDispatch ? null : classMethodOverrideOf(classes, receiver.declaration, key);
112
121
  if (own !== null)
113
122
  add(key, null, 'own', own.value);
123
+ else
124
+ for (const shadow of shadows.splice(0))
125
+ add(key, null, 'own', shadow);
126
+ shadows.length = 0;
114
127
  }
115
128
  }
116
129
  return [...inputs.values()];
@@ -0,0 +1,31 @@
1
+ import type { DeclarationId } from '../identity/ids.js';
2
+ import type { ClassLayout } from '../projection/classes.js';
3
+ import type { RepresentationDeriver } from '../representation/derive.js';
4
+ import { type PlainFieldRead } from './borrow-effects.js';
5
+ import type { CodeFreeFieldRead } from './field-read-reuse.js';
6
+ import type { GetOperation, IrBody } from './model.js';
7
+ /**
8
+ * Whether reading `key` off a value carried as `receiver` loads a data field:
9
+ * no getter of the program's can answer it.
10
+ *
11
+ * One answer for the whole compilation, asked by the IR's own dense-loop plan
12
+ * as well as by the target, because both have to agree on which loops change
13
+ * no array: the IR plan places a window's setup where its hoists put the bound,
14
+ * and the target renders the setup there.
15
+ */
16
+ export declare const plainFieldReadOf: (classes: ReadonlyMap<DeclarationId, ClassLayout>, deriver: RepresentationDeriver) => PlainFieldRead;
17
+ /**
18
+ * The property reads of a body that run none of the program's code: a data
19
+ * field (`plainFieldRead`, through borrow-effects' safe reads), a read of
20
+ * native data -- an array element or length, a string, a host handle -- or a
21
+ * host method. A getter is a property read too, and it can push onto the array
22
+ * a loop walks; `ir/hoist.ts` asks this before treating a read as touching no
23
+ * array.
24
+ *
25
+ * `plainFieldRead` judges the receiver's own type, and a record viewed as that
26
+ * type reads through its origin: a `Box` whose `min` is a data field still
27
+ * runs a getter when the box is a live view of a shape that answers `min` with
28
+ * one. `codeFreeFieldRead` (`field-read-reuse.ts`) knows every key some getter
29
+ * of the program answers, so a field read must satisfy both.
30
+ */
31
+ export declare const codeFreeGetOf: (body: IrBody, plainFieldRead: PlainFieldRead, codeFreeFieldRead: CodeFreeFieldRead) => ((operation: GetOperation) => boolean);
@@ -0,0 +1,51 @@
1
+ import { classMemberOf, recordAccessorsOfShape, recordFieldsOfShape } from '../projection/fields.js';
2
+ import { borrowSafeOperationsOf } from './borrow-effects.js';
3
+ import { nativeData } from './write-effects.js';
4
+ /**
5
+ * Whether reading `key` off a value carried as `receiver` loads a data field:
6
+ * no getter of the program's can answer it.
7
+ *
8
+ * One answer for the whole compilation, asked by the IR's own dense-loop plan
9
+ * as well as by the target, because both have to agree on which loops change
10
+ * no array: the IR plan places a window's setup where its hoists put the bound,
11
+ * and the target renders the setup there.
12
+ */
13
+ export const plainFieldReadOf = (classes, deriver) => (receiver, key) => {
14
+ if (receiver.kind === 'class-ref') {
15
+ // A class over a host base reads its members through the host's
16
+ // protocol; only a wholly projected class has plain member loads.
17
+ if (classes.get(receiver.declaration)?.nativeBase !== null)
18
+ return false;
19
+ return classMemberOf(classes, receiver.declaration, key)?.kind === 'field';
20
+ }
21
+ if (receiver.kind === 'record')
22
+ return receiver.fields.some((field) => field.key === key) && !receiver.accessors.some((accessor) => accessor.key === key);
23
+ // An interface-typed record is the same struct, its members named by its shape.
24
+ if (receiver.kind === 'native-record-ref' && receiver.native === null)
25
+ return ((recordFieldsOfShape(deriver, receiver.shapeId)?.some((field) => field.key === key) ?? false) &&
26
+ !(recordAccessorsOfShape(deriver, receiver.shapeId)?.some((accessor) => accessor.key === key) ?? true));
27
+ return false;
28
+ };
29
+ /**
30
+ * The property reads of a body that run none of the program's code: a data
31
+ * field (`plainFieldRead`, through borrow-effects' safe reads), a read of
32
+ * native data -- an array element or length, a string, a host handle -- or a
33
+ * host method. A getter is a property read too, and it can push onto the array
34
+ * a loop walks; `ir/hoist.ts` asks this before treating a read as touching no
35
+ * array.
36
+ *
37
+ * `plainFieldRead` judges the receiver's own type, and a record viewed as that
38
+ * type reads through its origin: a `Box` whose `min` is a data field still
39
+ * runs a getter when the box is a live view of a shape that answers `min` with
40
+ * one. `codeFreeFieldRead` (`field-read-reuse.ts`) knows every key some getter
41
+ * of the program answers, so a field read must satisfy both.
42
+ */
43
+ export const codeFreeGetOf = (body, plainFieldRead, codeFreeFieldRead) => {
44
+ let safe = null;
45
+ return (operation) => {
46
+ if (operation.hostMethod !== undefined || nativeData(operation.receiver.representation))
47
+ return true;
48
+ safe ??= borrowSafeOperationsOf(body, new Set(), (receiver, key) => plainFieldRead(receiver, key) && codeFreeFieldRead(receiver, key));
49
+ return safe.has(operation);
50
+ };
51
+ };