@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
@@ -490,6 +490,15 @@ inline const bool terminateFlushInstalled = [] {
490
490
 
491
491
  } // namespace detail
492
492
 
493
+ // A checked typed window's Number index (`DenseArray.checked`): a whole
494
+ // number inside the element count the preheader took, so it names an element.
495
+ // Anything else -- a fraction, a negative, NaN, past the end -- takes the
496
+ // view's own path, which drops the write or answers the read.
497
+ [[gnu::always_inline]] inline bool denseCheckedIndex(double key, std::size_t extent) {
498
+ std::uint32_t index;
499
+ return detail::doubleArrayIndex(key, index) && index < extent;
500
+ }
501
+
493
502
  // A loop guard must validate Number operands before converting them to native
494
503
  // indices. Fractional/non-finite inputs keep the ordinary access path. Once
495
504
  // these inputs are safe integers, the endpoint sums fit in int64_t (at most
@@ -1607,6 +1616,26 @@ template <typename... Arms>
1607
1616
  class TaggedUnion;
1608
1617
  template <typename T>
1609
1618
  struct Ref;
1619
+ /**
1620
+ * A run of constant-index reads of one array, taken once at its first read
1621
+ * (`ir/dense-read-windows.ts`): the cells when every index the run reads is
1622
+ * plain dense storage, else null, and every read of a null window takes
1623
+ * `readElementAtIndex`. Null until taken, so a member rendered without the
1624
+ * first read still reads correctly.
1625
+ */
1626
+ template <typename ArrayRef>
1627
+ struct DenseReadWindow {
1628
+ typename ArrayRef::element_type::Cell* cells = nullptr;
1629
+ };
1630
+ template <typename Array>
1631
+ [[gnu::always_inline]] inline typename Array::Cell* denseWindowCells(const gea::Ref<Array>& array, long long last) {
1632
+ return array->plainDenseThrough(last) ? array->cells.data() : nullptr;
1633
+ }
1634
+ /** `denseWindowCells` for a run that also stores: the array must also accept writes (`requireMutable`). */
1635
+ template <typename Array>
1636
+ [[gnu::always_inline]] inline typename Array::Cell* denseWindowCellsMutable(const gea::Ref<Array>& array, long long last) {
1637
+ return array->plainDenseThrough(last) && !array->hasFrozenIntegrity() ? array->cells.data() : nullptr;
1638
+ }
1610
1639
  class DynamicObject;
1611
1640
  class FunctionObjectIdentity;
1612
1641
  class Value;
@@ -3742,6 +3771,18 @@ inline void filterCycleCandidates(std::vector<CycleReference>& list, std::size_t
3742
3771
  ++allocationProfile().edgelessCandidates;
3743
3772
  if (reference.operations->profile != nullptr) ++reference.operations->profile().edgelessCandidates;
3744
3773
  #endif
3774
+ // Keep the verdict. A long-lived holder of untraced objects -- three's
3775
+ // material uniforms, a dictionary of `{ value }` view-target records --
3776
+ // dips on every frame's borrow, and dropping it with its bit clear had
3777
+ // the next dip buffer it and this probe walk every entry again: a
3778
+ // sixth of Saltline's frame. Found edgeless twice, it is permanent,
3779
+ // so its dips wait in `deferred` for the full collection (the one
3780
+ // pass that would see an edge it gains later, as for any permanent
3781
+ // object; see `deferredOverdue` for the bound). The first verdict
3782
+ // only makes it mature, which a young pass may treat as a leaf: it has
3783
+ // no traced edge to follow. An object that dies young never gets here
3784
+ // twice, so its block is not held in `deferred`.
3785
+ reference.counts->weak |= (reference.counts->weak & cycleMature) != 0 ? cyclePermanent : cycleMature;
3745
3786
  continue;
3746
3787
  }
3747
3788
  if (probe.selfEdges != 0 && probe.selfEdges == reference.counts->strong) {
@@ -6932,6 +6973,17 @@ inline Environment* unpackEnvironment(void* packed, void* slot) {
6932
6973
  }
6933
6974
  }
6934
6975
 
6976
+ namespace detail {
6977
+ // The environment struct a closure's borrowed body takes as its leading
6978
+ // `Environment*` formal (`callKnownBorrowedEnvironment`).
6979
+ template <typename Function>
6980
+ struct BorrowedEnvironmentOf;
6981
+ template <typename Result, typename Environment, typename... Rest>
6982
+ struct BorrowedEnvironmentOf<Result (*)(Environment*, Rest...)> {
6983
+ using type = Environment;
6984
+ };
6985
+ } // namespace detail
6986
+
6935
6987
  /**
6936
6988
  * `unpackEnvironment`'s counterpart for a `packTransientEnvironment`-packed
6937
6989
  * environment: the same two cases, plus the one `packTransientEnvironment`
@@ -8521,6 +8573,40 @@ struct CallableObject<Result(Arguments...)> {
8521
8573
  return invoke(environment, std::forward<Actual>(arguments)...);
8522
8574
  }
8523
8575
 
8576
+ // `callKnownBorrowed` for a closure's borrowed body, which takes its
8577
+ // environment first: the guard's hit unpacks the environment exactly as the
8578
+ // closure's thunk does (into a stack slot, unretained) and enters the body
8579
+ // with the arguments bound by reference, where `callKnown` copied each one
8580
+ // into the thunk's by-value formals and released it on return.
8581
+ template <auto Known, auto Borrowed, typename... Actual>
8582
+ [[gnu::always_inline]] Result callKnownBorrowedEnvironment(Actual&&... arguments) const {
8583
+ requireCallable();
8584
+ const auto keepAlive = environmentOwner;
8585
+ if constexpr (std::is_same_v<decltype(Known), Invoke>) {
8586
+ if (invoke == Known) {
8587
+ alignas(void*) unsigned char slot[sizeof(void*)];
8588
+ using Environment = typename detail::BorrowedEnvironmentOf<decltype(Borrowed)>::type;
8589
+ return Borrowed(unpackTransientEnvironment<Environment>(environment, slot), std::forward<Actual>(arguments)...);
8590
+ }
8591
+ }
8592
+ return invoke(environment, std::forward<Actual>(arguments)...);
8593
+ }
8594
+
8595
+ // The same for a callee `callStable` may take: the caller's own slot keeps
8596
+ // the environment alive, so neither branch retains it.
8597
+ template <auto Known, auto Borrowed, typename... Actual>
8598
+ [[gnu::always_inline]] Result callStableKnownBorrowedEnvironment(Actual&&... arguments) const {
8599
+ requireCallable();
8600
+ if constexpr (std::is_same_v<decltype(Known), Invoke>) {
8601
+ if (invoke == Known) {
8602
+ alignas(void*) unsigned char slot[sizeof(void*)];
8603
+ using Environment = typename detail::BorrowedEnvironmentOf<decltype(Borrowed)>::type;
8604
+ return Borrowed(unpackTransientEnvironment<Environment>(environment, slot), std::forward<Actual>(arguments)...);
8605
+ }
8606
+ }
8607
+ return invoke(environment, std::forward<Actual>(arguments)...);
8608
+ }
8609
+
8524
8610
  // Two candidates behind one slot (`callableMemberAlternateSlot`): at most
8525
8611
  // one guard matches, so each branch forwards the arguments once.
8526
8612
  template <auto Known, auto Borrowed, auto Known2, auto Borrowed2, typename... Actual>
@@ -8886,6 +8972,40 @@ struct CallableConstructorObject<Result(Arguments...), Constructed(ConstructArgu
8886
8972
  return invoke(environment, std::forward<Actual>(arguments)...);
8887
8973
  }
8888
8974
 
8975
+ // `callKnownBorrowed` for a closure's borrowed body, which takes its
8976
+ // environment first: the guard's hit unpacks the environment exactly as the
8977
+ // closure's thunk does (into a stack slot, unretained) and enters the body
8978
+ // with the arguments bound by reference, where `callKnown` copied each one
8979
+ // into the thunk's by-value formals and released it on return.
8980
+ template <auto Known, auto Borrowed, typename... Actual>
8981
+ [[gnu::always_inline]] Result callKnownBorrowedEnvironment(Actual&&... arguments) const {
8982
+ requireCallable();
8983
+ const auto keepAlive = environmentOwner;
8984
+ if constexpr (std::is_same_v<decltype(Known), Invoke>) {
8985
+ if (invoke == Known) {
8986
+ alignas(void*) unsigned char slot[sizeof(void*)];
8987
+ using Environment = typename detail::BorrowedEnvironmentOf<decltype(Borrowed)>::type;
8988
+ return Borrowed(unpackTransientEnvironment<Environment>(environment, slot), std::forward<Actual>(arguments)...);
8989
+ }
8990
+ }
8991
+ return invoke(environment, std::forward<Actual>(arguments)...);
8992
+ }
8993
+
8994
+ // The same for a callee `callStable` may take: the caller's own slot keeps
8995
+ // the environment alive, so neither branch retains it.
8996
+ template <auto Known, auto Borrowed, typename... Actual>
8997
+ [[gnu::always_inline]] Result callStableKnownBorrowedEnvironment(Actual&&... arguments) const {
8998
+ requireCallable();
8999
+ if constexpr (std::is_same_v<decltype(Known), Invoke>) {
9000
+ if (invoke == Known) {
9001
+ alignas(void*) unsigned char slot[sizeof(void*)];
9002
+ using Environment = typename detail::BorrowedEnvironmentOf<decltype(Borrowed)>::type;
9003
+ return Borrowed(unpackTransientEnvironment<Environment>(environment, slot), std::forward<Actual>(arguments)...);
9004
+ }
9005
+ }
9006
+ return invoke(environment, std::forward<Actual>(arguments)...);
9007
+ }
9008
+
8889
9009
  // Two candidates behind one slot (`callableMemberAlternateSlot`): at most
8890
9010
  // one guard matches, so each branch forwards the arguments once.
8891
9011
  template <auto Known, auto Borrowed, auto Known2, auto Borrowed2, typename... Actual>
@@ -10106,6 +10226,14 @@ struct ArrayObject {
10106
10226
  // `elementAtIndex`'s dense case first: ordinary storage, in range, no hole or
10107
10227
  // undefined bookkeeping answers the cell after one unsigned compare, so an
10108
10228
  // integer-keyed read keeps the shape the borrowed accessor had.
10229
+ /**
10230
+ * Whether `readElementAtIndex` answers every index up to `last` from `cells`
10231
+ * alone: the checks it makes per read, asked once for a run of reads
10232
+ * (`gea::denseWindowCells`, `ir/dense-read-windows.ts`).
10233
+ */
10234
+ [[gnu::always_inline]] bool plainDenseThrough(long long last) const {
10235
+ return !liveSource && !indexedDescriptors && static_cast<unsigned long long>(last) < cells.size() && holes.empty() && undefineds.empty();
10236
+ }
10109
10237
  [[gnu::always_inline]] Element readElementAtIndex(long long index) const {
10110
10238
  if (!liveSource && !indexedDescriptors && static_cast<unsigned long long>(index) < cells.size() && holes.empty() &&
10111
10239
  undefineds.empty()) [[likely]]
@@ -10951,6 +11079,17 @@ class ArrayBuffer {
10951
11079
  size_ = heap_.size();
10952
11080
  }
10953
11081
 
11082
+ /** Tag for the borrowing constructor below. */
11083
+ struct Borrowed {};
11084
+
11085
+ /**
11086
+ * Views a block the host owns and keeps alive for as long as any view of it
11087
+ * can run -- a display's scanout framebuffer, which an app renders into in
11088
+ * place instead of into a copy. The buffer never frees it; growing (which no
11089
+ * view of a fixed block does) or transferring copies it out first.
11090
+ */
11091
+ ArrayBuffer(Borrowed, std::uint8_t* bytes, std::size_t byteLength) noexcept : data_(bytes), size_(byteLength) {}
11092
+
10954
11093
  ArrayBuffer(const ArrayBuffer&) = delete;
10955
11094
  ArrayBuffer& operator=(const ArrayBuffer&) = delete;
10956
11095
  ArrayBuffer(ArrayBuffer&&) = delete;
@@ -10969,7 +11108,7 @@ class ArrayBuffer {
10969
11108
  requireAttached();
10970
11109
  std::vector<std::uint8_t> result;
10971
11110
  if (usingHeap_) result = std::move(heap_);
10972
- else result.assign(inline_, inline_ + size_);
11111
+ else result.assign(data_, data_ + size_);
10973
11112
  size_ = 0;
10974
11113
  data_ = inline_;
10975
11114
  usingHeap_ = false;
@@ -10989,7 +11128,7 @@ class ArrayBuffer {
10989
11128
  /** `TextEncoder::encode`'s upfront sizing: grows capacity to at least `requested` in place while it still fits inline, else moves (once) to the `vector` fallback -- the one path an ArrayBuffer's byte count ever grows through; a `new ArrayBuffer(n)` itself never resizes (see the class comment above). */
10990
11129
  void reserve(std::size_t requested) {
10991
11130
  requireAttached();
10992
- if (!usingHeap_ && requested <= kInlineCapacity) return;
11131
+ if (!usingHeap_ && data_ == inline_ && requested <= kInlineCapacity) return;
10993
11132
  migrateToHeap();
10994
11133
  heap_.reserve(requested);
10995
11134
  data_ = heap_.data();
@@ -11001,7 +11140,7 @@ class ArrayBuffer {
11001
11140
  requireAttached();
11002
11141
  const std::size_t offset = static_cast<std::size_t>(position - data_);
11003
11142
  const std::size_t count = static_cast<std::size_t>(last - first);
11004
- if (!usingHeap_ && size_ + count <= kInlineCapacity) {
11143
+ if (!usingHeap_ && data_ == inline_ && size_ + count <= kInlineCapacity) {
11005
11144
  if (offset != size_) std::memmove(inline_ + offset + count, inline_ + offset, size_ - offset);
11006
11145
  for (std::size_t written = 0; written < count; ++written, ++first) inline_[offset + written] = static_cast<std::uint8_t>(*first);
11007
11146
  size_ += count;
@@ -11023,7 +11162,7 @@ class ArrayBuffer {
11023
11162
 
11024
11163
  void migrateToHeap() {
11025
11164
  if (usingHeap_) return;
11026
- heap_.assign(inline_, inline_ + size_);
11165
+ heap_.assign(data_, data_ + size_);
11027
11166
  usingHeap_ = true;
11028
11167
  }
11029
11168
 
@@ -13848,6 +13987,28 @@ class StableEntryStore {
13848
13987
  * references to existing elements valid where a vector's reallocation would
13849
13988
  * dangle them.
13850
13989
  */
13990
+ namespace detail {
13991
+ /**
13992
+ * The interned key sequence of a string-keyed table (`Dictionary::layoutId`):
13993
+ * tables holding the same keys at the same positions -- three's uniform tables,
13994
+ * cloned from one template per material -- share one id, so a read site that
13995
+ * cached (id, slot) reads the slot of any of them without hashing or comparing
13996
+ * the key. Ids are never reused; the table stops growing at a fixed bound, and
13997
+ * a table whose sequence it cannot hold has no layout.
13998
+ */
13999
+ inline std::uint32_t internDictionaryLayout(const std::string& encoded) {
14000
+ static std::mutex lock;
14001
+ static auto* const layouts = new std::unordered_map<std::string, std::uint32_t>();
14002
+ const std::lock_guard<std::mutex> held(lock);
14003
+ const auto found = layouts->find(encoded);
14004
+ if (found != layouts->end()) return found->second;
14005
+ if (layouts->size() >= (std::size_t{1} << 16)) return 0;
14006
+ const auto id = static_cast<std::uint32_t>(layouts->size() + 1);
14007
+ layouts->emplace(encoded, id);
14008
+ return id;
14009
+ }
14010
+ } // namespace detail
14011
+
13851
14012
  template <typename V>
13852
14013
  class Dictionary {
13853
14014
  public:
@@ -13907,6 +14068,7 @@ class Dictionary {
13907
14068
  */
13908
14069
  void migrateTo(detail::DictionaryAliasHandle<V> alias) {
13909
14070
  static_assert(aliasable, "only a Dictionary<Value> can view another object");
14071
+ invalidateLayout();
13910
14072
  first_.reset();
13911
14073
  entries_.clear();
13912
14074
  index_.clear();
@@ -13929,6 +14091,7 @@ class Dictionary {
13929
14091
  if (!first_ && entries_.empty()) {
13930
14092
  enumerableKeys_.reset();
13931
14093
  noteKey(key);
14094
+ invalidateLayout();
13932
14095
  first_.emplace(detail::duplicateString(key), V{});
13933
14096
  if constexpr (aliasable) detail::noteDocumentDataKeyCreated(this, key);
13934
14097
  return first_->second;
@@ -13937,6 +14100,7 @@ class Dictionary {
13937
14100
  if (found != npos) return entries_[found].second;
13938
14101
  enumerableKeys_.reset();
13939
14102
  noteKey(key);
14103
+ invalidateLayout();
13940
14104
  Entry& entry = entries_.emplace_back(detail::duplicateString(key), V{});
13941
14105
  GEA_EH_TRY_ANY(gea_eh_failed) {
13942
14106
  indexInsert(entries_.size() - 1);
@@ -14016,6 +14180,53 @@ class Dictionary {
14016
14180
  }
14017
14181
  return readIfPresent<Carrier>(key);
14018
14182
  }
14183
+ /**
14184
+ * The slot of a constant key read at one site, asked of the site's cached
14185
+ * (layout, slot) pair first. A table with the cached layout holds the same
14186
+ * keys at the same slots, so the slot is the answer -- absence included --
14187
+ * with no hash and no key compare; it is what an inline cache keyed on a
14188
+ * hidden class does. A table without a layout keeps the position hint in the
14189
+ * low half. Relaxed: the pair is one word, so it never tears, and a stale one
14190
+ * only sends the read to the lookup.
14191
+ */
14192
+ std::uint32_t layoutSlot(std::string_view key, std::atomic<std::uint64_t>& hint) const {
14193
+ const std::uint64_t cached = hint.load(std::memory_order_relaxed);
14194
+ const std::uint32_t layout = layoutId();
14195
+ if (layout != 0 && static_cast<std::uint32_t>(cached >> 32) == layout) [[likely]] return static_cast<std::uint32_t>(cached);
14196
+ std::uint32_t slot = kAbsentSlot;
14197
+ if (first_ && first_->first == key) slot = 0;
14198
+ else if (mayHoldKey(key)) {
14199
+ const std::uint32_t at = layout == 0 && (cached >> 32) == 0 ? static_cast<std::uint32_t>(cached) : kAbsentSlot;
14200
+ if (at != kAbsentSlot && at != 0 && at - 1 < entries_.size() && entries_[at - 1].first == key) slot = at;
14201
+ else {
14202
+ const std::size_t found = findPosition(key);
14203
+ if (found != npos && found < kAbsentSlot - 1) slot = static_cast<std::uint32_t>(found + 1);
14204
+ }
14205
+ }
14206
+ if (layout != 0 || slot != kAbsentSlot) hint.store((std::uint64_t{layout} << 32) | slot, std::memory_order_relaxed);
14207
+ return slot;
14208
+ }
14209
+ /** `read` with a site's layout-keyed cache (`layoutSlot`). */
14210
+ V readHinted(std::string_view key, std::atomic<std::uint64_t>& hint) const {
14211
+ if constexpr (!aliasable) {
14212
+ if (!view_) [[likely]] {
14213
+ const std::uint32_t slot = layoutSlot(key, hint);
14214
+ return slot == kAbsentSlot ? V{} : slot == 0 ? first_->second : entries_[slot - 1].second;
14215
+ }
14216
+ }
14217
+ return read(key);
14218
+ }
14219
+ /** `readIfPresent` with a site's layout-keyed cache (`layoutSlot`). */
14220
+ template <typename Carrier>
14221
+ Carrier readIfPresentHinted(std::string_view key, std::atomic<std::uint64_t>& hint) const {
14222
+ if constexpr (!aliasable) {
14223
+ if (!view_) [[likely]] {
14224
+ const std::uint32_t slot = layoutSlot(key, hint);
14225
+ return slot == kAbsentSlot ? Carrier() : slot == 0 ? Carrier(first_->second) : Carrier(entries_[slot - 1].second);
14226
+ }
14227
+ }
14228
+ return readIfPresent<Carrier>(key);
14229
+ }
14019
14230
  /**
14020
14231
  * `has(key) ? Carrier(read(key)) : Carrier()` with one lookup for a plain
14021
14232
  * table: no view and no Document alias or overlay, where `has` and `read`
@@ -14075,13 +14286,14 @@ class Dictionary {
14075
14286
  }
14076
14287
  }
14077
14288
  if (first_ && first_->first == key) {
14078
- enumerableKeys_.reset(); first_.reset(); forgetAttributes(key);
14289
+ enumerableKeys_.reset(); invalidateLayout(); first_.reset(); forgetAttributes(key);
14079
14290
  if constexpr (aliasable) detail::forgetDocumentDataKey(this, key);
14080
14291
  return;
14081
14292
  }
14082
14293
  const std::size_t found = findPosition(key);
14083
14294
  if (found == npos) return;
14084
14295
  enumerableKeys_.reset();
14296
+ invalidateLayout();
14085
14297
  entries_.erase(found);
14086
14298
  if (entries_.size() <= kScanLimit) index_.clear();
14087
14299
  else rebuildIndex();
@@ -14523,6 +14735,7 @@ class Dictionary {
14523
14735
  if (view_) [[unlikely]] detail::refuseAliasedDictionaryStore();
14524
14736
  enumerableKeys_.reset();
14525
14737
  noteKey(key);
14738
+ invalidateLayout();
14526
14739
  if (!first_ && entries_.empty()) {
14527
14740
  first_.emplace(detail::duplicateString(key), std::move(value));
14528
14741
  return;
@@ -14653,6 +14866,45 @@ class Dictionary {
14653
14866
  * an alias holds no entries of its own here.
14654
14867
  */
14655
14868
  std::uint64_t keyMask_ = 0;
14869
+ // `layoutId`: 0 until asked after a change of the key sequence, kNoLayout
14870
+ // once the table has changed under its readers too often to be worth naming.
14871
+ // Read through `std::atomic_ref`: a const read computes it, and concurrent
14872
+ // readers may race to store the same id. A copy holds the same keys, so it
14873
+ // keeps the id.
14874
+ static constexpr std::uint32_t kAbsentSlot = static_cast<std::uint32_t>(-1);
14875
+ static constexpr std::uint32_t kNoLayout = static_cast<std::uint32_t>(-1);
14876
+ mutable std::uint32_t layoutId_ = 0;
14877
+ mutable std::uint32_t layoutComputations_ = 0;
14878
+ void invalidateLayout() {
14879
+ if (layoutId_ != kNoLayout) std::atomic_ref<std::uint32_t>(layoutId_).store(0, std::memory_order_relaxed);
14880
+ }
14881
+ /** The interned key sequence this table holds now (`detail::internDictionaryLayout`), or 0 when it has none. */
14882
+ std::uint32_t layoutId() const {
14883
+ const std::uint32_t id = std::atomic_ref<std::uint32_t>(layoutId_).load(std::memory_order_relaxed);
14884
+ if (id != 0) [[likely]] return id == kNoLayout ? 0 : id;
14885
+ return computeLayoutId();
14886
+ }
14887
+ [[gnu::noinline]] std::uint32_t computeLayoutId() const {
14888
+ std::atomic_ref<std::uint32_t> id(layoutId_);
14889
+ // A table rebuilt between reads -- one grown a key at a time while a site
14890
+ // reads it -- would intern a sequence per step; it keeps the plain lookup.
14891
+ if (std::atomic_ref<std::uint32_t>(layoutComputations_).fetch_add(1, std::memory_order_relaxed) >= 8 || entries_.size() > 1024) {
14892
+ id.store(kNoLayout, std::memory_order_relaxed);
14893
+ return 0;
14894
+ }
14895
+ std::string encoded;
14896
+ const auto append = [&](std::string_view key) {
14897
+ const auto size = static_cast<std::uint32_t>(key.size());
14898
+ encoded.append(reinterpret_cast<const char*>(&size), sizeof size);
14899
+ encoded.append(key);
14900
+ };
14901
+ encoded.push_back(first_ ? '\x01' : '\x00');
14902
+ if (first_) append(first_->first);
14903
+ for (std::size_t position = 0; position < entries_.size(); ++position) append(entries_[position].first);
14904
+ const std::uint32_t interned = detail::internDictionaryLayout(encoded);
14905
+ id.store(interned == 0 ? kNoLayout : interned, std::memory_order_relaxed);
14906
+ return interned;
14907
+ }
14656
14908
  static constexpr unsigned keyBit(std::string_view key) {
14657
14909
  const std::size_t size = key.size();
14658
14910
  const unsigned edges = size == 0 ? 0u : static_cast<unsigned>(static_cast<unsigned char>(key[0])) * 3u + static_cast<unsigned char>(key[size - 1]);
@@ -23351,6 +23603,19 @@ inline Value installOrdinaryConstructorPrototype(Value ctor) {
23351
23603
  } // namespace host
23352
23604
 
23353
23605
  namespace dictionary {
23606
+ /**
23607
+ * A key as the view the tables compare. A constant key arrives as the string
23608
+ * literal the emitter spelled, and the view takes its length from the array:
23609
+ * converting it through `const char*` measured the literal on every read --
23610
+ * three's uniform reads (`uniforms.diffuse`, `uniforms.projectionMatrix`) ran
23611
+ * a strlen per lookup, per material, per frame -- and would have cut a key
23612
+ * holding a NUL short.
23613
+ */
23614
+ template <typename Key>
23615
+ [[gnu::always_inline]] inline std::string_view keyView(const Key& key) {
23616
+ if constexpr (std::is_array_v<Key>) return std::string_view(key, std::extent_v<Key> - 1);
23617
+ else return std::string_view(key);
23618
+ }
23354
23619
  /**
23355
23620
  * The present-or-absent read of a keyed table (`has(k) ? Carrier(read(k)) :
23356
23621
  * Carrier()`). A table with a single-lookup `readIfPresent` answers through
@@ -23358,24 +23623,24 @@ namespace dictionary {
23358
23623
  */
23359
23624
  template <typename Carrier, typename Table, typename Key>
23360
23625
  inline Carrier readIfPresent(const Table& table, const Key& key) {
23361
- if constexpr (requires { table.template readIfPresent<Carrier>(std::string_view(key)); })
23362
- return table.template readIfPresent<Carrier>(std::string_view(key));
23626
+ if constexpr (requires { table.template readIfPresent<Carrier>(keyView(key)); })
23627
+ return table.template readIfPresent<Carrier>(keyView(key));
23363
23628
  else
23364
23629
  return table.has(key) ? Carrier(table.read(key)) : Carrier();
23365
23630
  }
23366
23631
  /** `readIfPresent` for a constant key, with the emitting site's position hint (`Dictionary::hintedPosition`). */
23367
23632
  template <typename Carrier, typename Table, typename Key>
23368
- inline Carrier readIfPresentHinted(const Table& table, const Key& key, std::atomic<std::uint32_t>& hint) {
23369
- if constexpr (requires { table.template readIfPresentHinted<Carrier>(std::string_view(key), hint); })
23370
- return table.template readIfPresentHinted<Carrier>(std::string_view(key), hint);
23633
+ inline Carrier readIfPresentHinted(const Table& table, const Key& key, std::atomic<std::uint64_t>& hint) {
23634
+ if constexpr (requires { table.template readIfPresentHinted<Carrier>(keyView(key), hint); })
23635
+ return table.template readIfPresentHinted<Carrier>(keyView(key), hint);
23371
23636
  else
23372
23637
  return readIfPresent<Carrier>(table, key);
23373
23638
  }
23374
23639
  /** `table.read(key)` for a constant key, with the emitting site's position hint. */
23375
23640
  template <typename Table, typename Key>
23376
- inline auto readHinted(const Table& table, const Key& key, std::atomic<std::uint32_t>& hint) {
23377
- if constexpr (requires { table.readHinted(std::string_view(key), hint); })
23378
- return table.readHinted(std::string_view(key), hint);
23641
+ inline auto readHinted(const Table& table, const Key& key, std::atomic<std::uint64_t>& hint) {
23642
+ if constexpr (requires { table.readHinted(keyView(key), hint); })
23643
+ return table.readHinted(keyView(key), hint);
23379
23644
  else
23380
23645
  return table.read(key);
23381
23646
  }
@@ -28687,6 +28952,27 @@ Ref<void> nativePropertyAnchor(const Ref<T>& object) {
28687
28952
  return Ref<void>(object);
28688
28953
  }
28689
28954
 
28955
+ namespace detail {
28956
+ /**
28957
+ * Whether a class instance owns a native expando table: the header's
28958
+ * `expandoEntry` bit, one load. A method only a write through a dynamic
28959
+ * receiver can shadow (`CallOperation.expandoShadowGuard`) is called directly
28960
+ * while this is clear, which is every instance nothing such wrote to.
28961
+ */
28962
+ template <typename T>
28963
+ [[gnu::always_inline]] inline bool nativeExpandoPresent(const Ref<T>& object) {
28964
+ return object && (refCountsOf(const_cast<std::remove_const_t<T>*>(object.get()))->weak & expandoEntry) != 0;
28965
+ }
28966
+ /** The value an instance's expando table holds as its own `key`, reading an accessor as `[[Get]]` would; empty when it holds none. */
28967
+ template <typename T>
28968
+ inline std::optional<Value> nativeExpandoOwnValue(const Ref<T>& object, std::string_view key) {
28969
+ const auto table = expandoFor(nativePropertyAnchor(object), false);
28970
+ const PropertyKey property = PropertyKey::string(std::string(key));
28971
+ if (!table || table->ownProperty(property) == nullptr) return std::nullopt;
28972
+ return table->getWithNativeReceiver(property, NativeCallReceiver::object(object));
28973
+ }
28974
+ } // namespace detail
28975
+
28690
28976
  /**
28691
28977
  * Integrity state for a statically typed native object.
28692
28978
  *
@@ -31341,6 +31627,24 @@ Result readLiveField(
31341
31627
  return std::move(*answer);
31342
31628
  }
31343
31629
 
31630
+ /**
31631
+ * `readLiveField` for a read whose only use is the receiver of the next member
31632
+ * read in the same expression (`gull.pos.x`): the plain allocation answers a
31633
+ * reference to its own member, so the handle is neither retained nor released;
31634
+ * a live view's answer is moved into `slot`, a temporary the caller's full
31635
+ * expression owns, and that is what is referenced. Nothing runs between this
31636
+ * returning and the member load that consumes it.
31637
+ */
31638
+ template <typename Result, typename Receiver, typename Key, typename Adapter, typename Fallback>
31639
+ const Result& readLiveFieldBorrowed(
31640
+ const gea::Ref<Receiver>& receiver, const Key& fieldKey,
31641
+ Adapter&& adapter, NativeFieldRead::AcceptFn accepts, Result& slot, Fallback&& fallback) {
31642
+ if (!receiver) host::throwRuntimeError("TypeError", "Cannot read a property of a nullish native view");
31643
+ if (!isLiveFieldViewReceiver(receiver)) [[likely]] return std::forward<Fallback>(fallback)();
31644
+ slot = readLiveField<Result>(receiver, fieldKey, std::forward<Adapter>(adapter), accepts, [&]() -> Result { return std::forward<Fallback>(fallback)(); });
31645
+ return slot;
31646
+ }
31647
+
31344
31648
  template <typename Receiver, typename Key, typename Source, typename Adapter, typename Fallback>
31345
31649
  bool writeLiveField(
31346
31650
  const gea::Ref<Receiver>& receiver, const Key& fieldKey, const Source& value,
@@ -40685,6 +40989,53 @@ inline std::string toString(double value) {
40685
40989
  auto result = std::to_chars(buf, buf + sizeof(buf), static_cast<long long>(value));
40686
40990
  return std::string(buf, result.ptr);
40687
40991
  }
40992
+ // Short decimals -- chart coordinates, prices, anything rounded to a few
40993
+ // places -- without the general path, which on targets without
40994
+ // `to_chars(double)` raises a `%.*g` precision until `strtod` round-trips:
40995
+ // up to 17 snprintf/strtod pairs, in software double arithmetic on parts
40996
+ // with no double FPU (ESP32), for every number in a template string.
40997
+ //
40998
+ // Exact, not approximate. For the smallest k at which some integer c has
40999
+ // c / 10^k == value, the k-decimal spelling of c is the shortest
41000
+ // round-trip string: c and 10^k are exact doubles and IEEE division
41001
+ // rounds the exact quotient c/10^k just as strtod rounds that decimal, so
41002
+ // it parses back to `value`; no spelling with fewer decimals does, or a
41003
+ // smaller k would have matched. Any integer that round-trips at k lies
41004
+ // within one of value * 10^k, so testing the rounded product and its two
41005
+ // neighbours misses none; when two of them qualify, ECMAScript's "closest
41006
+ // to the value" tie-break applies and the general path answers instead.
41007
+ // |value| in [1e-6, 1e15) is where ToString spells these in fixed notation.
41008
+ {
41009
+ const double magnitude = std::fabs(value);
41010
+ if (magnitude >= 1e-6 && magnitude < 1e15) {
41011
+ double scale = 1;
41012
+ for (int places = 1; places <= 9; ++places) {
41013
+ scale *= 10;
41014
+ const double scaled = value * scale;
41015
+ if (std::fabs(scaled) >= 9007199254740992.0) break;
41016
+ const double nearest = std::nearbyint(scaled);
41017
+ int matches = 0;
41018
+ double digits = 0;
41019
+ for (const double candidate : {nearest - 1, nearest, nearest + 1}) {
41020
+ if (candidate / scale == value) {
41021
+ ++matches;
41022
+ digits = candidate;
41023
+ }
41024
+ }
41025
+ if (matches > 1) break;
41026
+ if (matches == 1) {
41027
+ char buf[32];
41028
+ const auto end = std::to_chars(buf, buf + sizeof(buf), static_cast<long long>(std::fabs(digits))).ptr;
41029
+ std::string integer(buf, end);
41030
+ if (static_cast<int>(integer.size()) <= places)
41031
+ integer.insert(0, static_cast<std::size_t>(places + 1 - static_cast<int>(integer.size())), '0');
41032
+ integer.insert(integer.size() - static_cast<std::size_t>(places), 1, '.');
41033
+ if (value < 0) integer.insert(integer.begin(), '-');
41034
+ return integer;
41035
+ }
41036
+ }
41037
+ }
41038
+ }
40688
41039
  // v1 has a third fast path here -- `snprintf("%.0f")` for integer-valued
40689
41040
  // doubles below 1e21 -- and it is wrong in exactly the band it covers. Above
40690
41041
  // 2^53 (the branch above) a double no longer represents every integer, and
@@ -42111,6 +42462,17 @@ inline double bulkAppend(const gea::Ref<ArrayObject<Element>>& receiver, const V
42111
42462
  return receiver->length();
42112
42463
  }
42113
42464
 
42465
+ /**
42466
+ * One element of an array literal stored through a call rather than an inlined
42467
+ * `push`: a body that builds thousands of literals is a table built once, and
42468
+ * the inlined capacity branches and unwind edges only cost the C++ compiler
42469
+ * time (`emit-arrays.ts`, `outOfLineLiteralPushThreshold`).
42470
+ */
42471
+ template <typename Receiver, typename Value>
42472
+ [[gnu::noinline]] void pushOutOfLine(const Receiver& receiver, Value&& value) {
42473
+ receiver->push(std::forward<Value>(value));
42474
+ }
42475
+
42114
42476
  /**
42115
42477
  * One array literal made entirely of constants, installed from a static table.
42116
42478
  *
@@ -43603,24 +43965,41 @@ inline double round_invoke(void*, double x) {
43603
43965
  // never rounds to a neighboring double.
43604
43966
  return std::floor(x + 0.5);
43605
43967
  }
43606
- #if defined(GEA_RUNTIME_FLOAT_MATH) && GEA_RUNTIME_FLOAT_MATH
43607
- // Opt-in for targets whose FPU is single precision only (ESP32-S31): double
43608
- // transcendentals there are soft-float ROM routines, ~15% of a game frame.
43609
- // The argument is reduced in double first, so time-driven angles keep their
43610
- // phase; the result carries float precision, which a renderer cannot see.
43968
+ // Trigonometry on a single-precision-only FPU (ESP32-S3 Xtensa LX7, ESP32-P4
43969
+ // RISC-V with F but no D): double sin/cos/atan2 there are software routines
43970
+ // whose every multiply is itself emulated, so they run in float on the FPU.
43971
+ // Automatic for those cores; trig results are never exact in JS anyway, and a
43972
+ // renderer cannot see float precision. The argument is reduced in double
43973
+ // first, so time-driven angles keep their phase.
43974
+ #if !defined(GEA_RUNTIME_FLOAT_TRIG)
43975
+ #if (defined(GEA_RUNTIME_FLOAT_MATH) && GEA_RUNTIME_FLOAT_MATH) || \
43976
+ (defined(__XTENSA__) && defined(__XCHAL_HAVE_DFP) && __XCHAL_HAVE_FP && !__XCHAL_HAVE_DFP) || \
43977
+ (defined(__riscv_flen) && __riscv_flen == 32)
43978
+ #define GEA_RUNTIME_FLOAT_TRIG 1
43979
+ #else
43980
+ #define GEA_RUNTIME_FLOAT_TRIG 0
43981
+ #endif
43982
+ #endif
43983
+ #if GEA_RUNTIME_FLOAT_TRIG
43611
43984
  inline float floatMathTurn(double x) {
43612
43985
  constexpr double twoPi = 6.283185307179586;
43613
43986
  return static_cast<float>(x - std::nearbyint(x * (1.0 / twoPi)) * twoPi);
43614
43987
  }
43615
43988
  inline double sin_invoke(void*, double x) { return x != 0 && std::isfinite(x) ? static_cast<double>(sinf(floatMathTurn(x))) : std::sin(x); }
43616
43989
  inline double cos_invoke(void*, double x) { return std::isfinite(x) ? static_cast<double>(cosf(floatMathTurn(x))) : std::cos(x); }
43990
+ #else
43991
+ inline double sin_invoke(void*, double x) { return std::sin(x); }
43992
+ inline double cos_invoke(void*, double x) { return std::cos(x); }
43993
+ #endif
43994
+ // sqrt is correctly rounded and programs rely on it being exact (an integer
43995
+ // square root through Math.floor(Math.sqrt(n))), so its float path stays an
43996
+ // explicit opt-in.
43997
+ #if defined(GEA_RUNTIME_FLOAT_MATH) && GEA_RUNTIME_FLOAT_MATH
43617
43998
  inline double sqrt_invoke(void*, double x) {
43618
43999
  // Doubles beyond float range keep the exact path.
43619
44000
  return (x >= 0 && x < 3.0e38) ? static_cast<double>(sqrtf(static_cast<float>(x))) : std::sqrt(x);
43620
44001
  }
43621
44002
  #else
43622
- inline double sin_invoke(void*, double x) { return std::sin(x); }
43623
- inline double cos_invoke(void*, double x) { return std::cos(x); }
43624
44003
  inline double sqrt_invoke(void*, double x) { return std::sqrt(x); }
43625
44004
  #endif
43626
44005
  inline double abs_invoke(void*, double x) { return std::fabs(x); }
@@ -43630,7 +44009,7 @@ inline double abs_invoke(void*, double x) { return std::fabs(x); }
43630
44009
  // ceil(+/-Infinity) is +/-Infinity).
43631
44010
  inline double ceil_invoke(void*, double x) { return std::ceil(x); }
43632
44011
  inline double pow_invoke(void*, double base, double exponent) { return std::pow(base, exponent); }
43633
- #if defined(GEA_RUNTIME_FLOAT_MATH) && GEA_RUNTIME_FLOAT_MATH
44012
+ #if GEA_RUNTIME_FLOAT_TRIG
43634
44013
  inline double atan2_invoke(void*, double y, double x) {
43635
44014
  const float fy = static_cast<float>(y), fx = static_cast<float>(x);
43636
44015
  return (std::isfinite(fy) && std::isfinite(fx)) ? static_cast<double>(atan2f(fy, fx)) : std::atan2(y, x);