@geastack/compiler 1.0.42 → 1.0.43

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 (44) hide show
  1. package/dist/conversion/nodes.js +5 -2
  2. package/dist/ir/certify.js +1 -1
  3. package/dist/ir/host-template-conversions.d.ts +2 -2
  4. package/dist/ir/host-template-conversions.js +73 -2
  5. package/dist/ir/lower-destructuring.js +22 -3
  6. package/dist/ir/lower-operands.js +24 -0
  7. package/dist/ir/native-field-view-facts.d.ts +8 -1
  8. package/dist/ir/native-field-view-facts.js +13 -3
  9. package/dist/ir/operation-conversions.js +1 -1
  10. package/dist/ir/program-conversions.d.ts +1 -1
  11. package/dist/ir/program-conversions.js +68 -1
  12. package/dist/ir/property-read-conversions.js +7 -1
  13. package/dist/ir/publish-native-field-view-facts.js +4 -0
  14. package/dist/ir/reflection-demand.js +29 -3
  15. package/dist/projection/dispatch.d.ts +19 -1
  16. package/dist/projection/dispatch.js +34 -0
  17. package/dist/representation/value-records.js +182 -13
  18. package/dist/semantics/normalize/collection-bindings.d.ts +7 -0
  19. package/dist/semantics/normalize/collection-bindings.js +35 -1
  20. package/dist/semantics/normalize/derived-expression-type.js +49 -0
  21. package/dist/semantics/normalize/field-bindings.js +6 -1
  22. package/dist/semantics/normalize/flow/source-value-session.d.ts +9 -2
  23. package/dist/semantics/normalize/flow/source-value-session.js +538 -94
  24. package/dist/semantics/normalize/local-bindings.js +23 -1
  25. package/dist/semantics/normalize/producers/references.js +26 -18
  26. package/dist/semantics/normalize/structural-array-element.d.ts +7 -0
  27. package/dist/semantics/normalize/structural-array-element.js +24 -1
  28. package/dist/semantics/normalize/structural-layout-type.js +5 -1
  29. package/dist/semantics/normalize/structural-parts.js +13 -8
  30. package/dist/semantics/normalize/structural.js +25 -1
  31. package/dist/targets/cpp/class-properties/emit-class-properties.js +5 -1
  32. package/dist/targets/cpp/emit-callable.js +19 -10
  33. package/dist/targets/cpp/emit-native-field-view.d.ts +2 -0
  34. package/dist/targets/cpp/emit-native-field-view.js +1 -1
  35. package/dist/targets/cpp/emit-properties.js +114 -12
  36. package/dist/targets/cpp/host/emit-host-invoke.js +16 -2
  37. package/dist/targets/cpp/host/host-members.js +4 -0
  38. package/dist/targets/cpp/prototype/emit-prototype-array.d.ts +42 -0
  39. package/dist/targets/cpp/prototype/emit-prototype-array.js +31 -9
  40. package/dist/targets/cpp/records.js +39 -16
  41. package/dist/targets/cpp/translation-unit.js +23 -2
  42. package/package.json +1 -1
  43. package/src/targets/cpp/runtime/gea_dynamic_proxy.h +17 -1
  44. package/src/targets/cpp/runtime/gea_runtime.h +298 -51
@@ -166,6 +166,17 @@ namespace gea {
166
166
  return value;
167
167
  }
168
168
 
169
+ /** `float32Product`'s pin for a double product: `Math.hypot` squares and sums
170
+ * in one expression, and Clang's default fuses that pair into one rounding. */
171
+ [[gnu::always_inline]] inline double float64Product(double value) {
172
+ #if defined(__aarch64__)
173
+ __asm__("" : "+w"(value));
174
+ #elif (defined(__x86_64__) || defined(__i386__)) && defined(__SSE2__)
175
+ __asm__("" : "+x"(value));
176
+ #endif
177
+ return value;
178
+ }
179
+
169
180
  namespace detail {
170
181
 
171
182
  inline void collectReferenceCycles(bool full);
@@ -15970,6 +15981,15 @@ struct ArrayObjectElementOf<ArrayObject<Element>> {
15970
15981
  using type = Element;
15971
15982
  };
15972
15983
 
15984
+ namespace detail {
15985
+ template <typename Element>
15986
+ ArrayObject<Element> arrayObjectBaseProbe(const ArrayObject<Element>&);
15987
+ template <typename T>
15988
+ using ArrayObjectBaseOf = decltype(arrayObjectBaseProbe(std::declval<const T&>()));
15989
+ template <typename T>
15990
+ concept DerivedArrayObject = !IsArrayObjectStruct<T>::value && requires { typename ArrayObjectBaseOf<T>; };
15991
+ }
15992
+
15973
15993
  namespace detail {
15974
15994
  template <typename T>
15975
15995
  struct DynamicCarrier;
@@ -20236,6 +20256,8 @@ struct Error;
20236
20256
  namespace host {
20237
20257
  inline gea::Ref<gea::runtime::Error> createRuntimeError(const char* kind, const gea::Optional<std::string>& message);
20238
20258
  inline bool isRuntimeError(const gea::Value& value);
20259
+ inline gea::Ref<gea::runtime::Error> runtimeErrorOf(const gea::Value& value);
20260
+ inline bool isErrorValue(const gea::Value& value);
20239
20261
  inline bool instanceOfRuntimeError(const gea::Value& value, const char* kind);
20240
20262
  inline std::string runtimeErrorString(const gea::Value& value);
20241
20263
  [[noreturn]] inline void throwRuntimeError(const char* kind, const std::string& message);
@@ -24448,6 +24470,20 @@ struct DynamicCallableCarrier<Optional<T>> {
24448
24470
  }
24449
24471
  };
24450
24472
 
24473
+ /**
24474
+ * The compiler's certified live view of a dynamic object as record `T`, for a
24475
+ * record a boxed callable's frame names (`program-conversions.ts`'s
24476
+ * `dynamic-call-argument`). The generated program specializes it for exactly
24477
+ * the records it published that recipe for; every other `T` has none.
24478
+ */
24479
+ template <typename T>
24480
+ struct DynamicArgumentView {
24481
+ static constexpr bool installed = false;
24482
+ };
24483
+
24484
+ template <typename Element>
24485
+ gea::Ref<ArrayObject<Element>> dynamicArgumentArrayView(const Value& value);
24486
+
24451
24487
  template <typename T>
24452
24488
  struct DynamicCarrier<gea::Ref<T>> {
24453
24489
  static constexpr bool supported = true;
@@ -24459,6 +24495,19 @@ struct DynamicCarrier<gea::Ref<T>> {
24459
24495
  }
24460
24496
  static gea::Ref<T> in(const Value& value, std::size_t position) {
24461
24497
  if (value.tag() == Value::Tag::Undefined) refuseMissingCallArgument(position);
24498
+ // A caller hands the callee its own object, whatever layout the caller
24499
+ // gave it: `pts.map(([r, y]) => ...)` over `number[]` rows, an options
24500
+ // literal into an `o = {}` parameter. Another layout is read through a
24501
+ // live view of that same object, so the callee's writes reach the caller.
24502
+ if (value.tag() == Value::Tag::Object && !accepts(value)) {
24503
+ if constexpr (IsArrayObjectStruct<T>::value) {
24504
+ using Element = typename ArrayObjectElementOf<T>::type;
24505
+ if constexpr (DynamicCarrier<Element>::supported)
24506
+ if (value.isArrayPayload()) return dynamicArgumentArrayView<Element>(value);
24507
+ } else if constexpr (DynamicArgumentView<T>::installed) {
24508
+ return DynamicArgumentView<T>::in(value);
24509
+ }
24510
+ }
24462
24511
  return unboxClassRef<T>(value, "a dynamic call argument");
24463
24512
  }
24464
24513
  };
@@ -29421,6 +29470,14 @@ using ::gea::checkedNativeArrayView;
29421
29470
  using ::gea::nativeArrayViewAccepts;
29422
29471
  using ::gea::nativeArrayViewAcceptsExact;
29423
29472
  using ::gea::nativeArrayViewElementsAccept;
29473
+
29474
+ /** Each element crosses by the frame's own rule for its carrier, in both directions. */
29475
+ template <typename Element>
29476
+ gea::Ref<ArrayObject<Element>> dynamicArgumentArrayView(const Value& value) {
29477
+ return checkedNativeArrayView<Element, NativeFieldLeafPolicy>(
29478
+ value, "a dynamic call argument", [](const Value& entry) -> Element { return DynamicCarrier<Element>::in(entry, 0); },
29479
+ [](const Element& entry) -> Value { return DynamicCarrier<Element>::out(entry); });
29480
+ }
29424
29481
  }
29425
29482
 
29426
29483
  /** A descriptor query creates a new ordinary object. Its copied native
@@ -29831,20 +29888,30 @@ bool nativeDynamicRead(const gea::Ref<T>& object, const PropertyKey& key, Value&
29831
29888
  }
29832
29889
  if (const auto read = record::readDocumentView(Ref<void>(object), key, answer)) return *read;
29833
29890
  if (const auto read = record::readNativeViewValue(Ref<void>(object), key, answer)) return *read;
29891
+ // OrdinaryGet continues from every own miss to the prototype, exactly as the
29892
+ // boxed twin `Value::getProperty` does: a class's accessors and methods are
29893
+ // answered by its generated prototype hook, never by instance storage.
29894
+ const auto inherited = [&]() -> bool {
29895
+ if constexpr (requires(const T& reader) { reader.gea_readPrototypeProperty(key, answer); })
29896
+ return object->gea_readPrototypeProperty(key, answer);
29897
+ else
29898
+ return false;
29899
+ };
29834
29900
  if constexpr (requires(const T& reader) { reader.gea_readOwnField(key, answer); }) {
29835
29901
  if (object->gea_readOwnField(key, answer)) return true;
29836
29902
  if constexpr (detail::NativeOwnFieldPredicate<T>) {
29837
29903
  // A deleted fixed slot is still the layout's slot. Do not let an index
29838
29904
  // sidecar or expando shadow it merely because its presence bit is false.
29839
- if (object->gea_matchesOwnField(key)) return false;
29905
+ if (object->gea_matchesOwnField(key)) return inherited();
29840
29906
  }
29841
29907
  }
29842
29908
  if constexpr (requires(const T& reader) { reader.gea_readOwnIndex(key, answer); reader.gea_matchesOwnIndex(key); }) {
29843
29909
  if (object->gea_readOwnIndex(key, answer)) return true;
29844
- if (object->gea_matchesOwnIndex(key)) return false;
29910
+ if (object->gea_matchesOwnIndex(key)) return inherited();
29845
29911
  }
29846
29912
  const gea::Ref<DynamicObject> expando = detail::expandoFor(nativePropertyAnchor(object), false);
29847
- return expando && expando->readWithReceiver(key, [&] { return Value::box(Value::Tag::Object, object); }, answer);
29913
+ if (expando && expando->readWithReceiver(key, [&] { return Value::box(Value::Tag::Object, object); }, answer)) return true;
29914
+ return inherited();
29848
29915
  }
29849
29916
 
29850
29917
  // A certified missing property still evaluates its receiver/key and throws on
@@ -32583,6 +32650,26 @@ bool assignOwnPropertiesUnorderedWith(const gea::Ref<T>& source, const gea::Ref<
32583
32650
  }
32584
32651
 
32585
32652
  /** Array exotic own enumerable string keys, followed by enumerable expandos. */
32653
+ template <typename Element>
32654
+ std::vector<std::string> arrayOwnEnumerableKeys(const gea::Ref<ArrayObject<Element>>& array);
32655
+ template <typename Element>
32656
+ bool arrayHasOwnEnumerableKey(const gea::Ref<ArrayObject<Element>>& array, const std::string& key);
32657
+ template <typename Element>
32658
+ std::vector<std::string> arrayOwnPropertyNames(const gea::Ref<ArrayObject<Element>>& array);
32659
+
32660
+ template <detail::DerivedArrayObject Array>
32661
+ std::vector<std::string> arrayOwnEnumerableKeys(const gea::Ref<Array>& array) {
32662
+ return arrayOwnEnumerableKeys(gea::Ref<detail::ArrayObjectBaseOf<Array>>(array));
32663
+ }
32664
+ template <detail::DerivedArrayObject Array>
32665
+ bool arrayHasOwnEnumerableKey(const gea::Ref<Array>& array, const std::string& key) {
32666
+ return arrayHasOwnEnumerableKey(gea::Ref<detail::ArrayObjectBaseOf<Array>>(array), key);
32667
+ }
32668
+ template <detail::DerivedArrayObject Array>
32669
+ std::vector<std::string> arrayOwnPropertyNames(const gea::Ref<Array>& array) {
32670
+ return arrayOwnPropertyNames(gea::Ref<detail::ArrayObjectBaseOf<Array>>(array));
32671
+ }
32672
+
32586
32673
  template <typename Element>
32587
32674
  std::vector<std::string> arrayOwnEnumerableKeys(const gea::Ref<ArrayObject<Element>>& array) {
32588
32675
  std::vector<std::string> keys;
@@ -33965,6 +34052,26 @@ inline bool isShortBasicLatin(const std::string &s) {
33965
34052
  return size == 0 || (static_cast<unsigned char>(s[0]) & 0x80u) == 0;
33966
34053
  }
33967
34054
 
34055
+ // The UTF-8 walks below take eight ASCII bytes (eight code units) per step
34056
+ // with this. A per-byte walk past a string's first non-ASCII byte made every
34057
+ // `length`/`charAt`/`slice` on a long, mostly-ASCII string -- a shader chunk
34058
+ // with one accented comment -- a full byte loop, and three.js's include
34059
+ // resolver calls them once per character.
34060
+ inline bool isAsciiWord(const unsigned char *bytes) {
34061
+ std::uint64_t word;
34062
+ std::memcpy(&word, bytes, 8);
34063
+ return (word & 0x8080808080808080ull) == 0;
34064
+ }
34065
+
34066
+ /** How many of the first `limit` bytes (at most `s.size()`) lead `s` as ASCII. */
34067
+ inline std::size_t asciiPrefixLength(const std::string &s, std::size_t limit) {
34068
+ const unsigned char *d = reinterpret_cast<const unsigned char *>(s.data());
34069
+ std::size_t i = 0;
34070
+ while (i + 8 <= limit && isAsciiWord(d + i)) i += 8;
34071
+ while (i < limit && !(d[i] & 0x80)) ++i;
34072
+ return i;
34073
+ }
34074
+
33968
34075
  inline Utf16Metadata utf16Metadata(const std::string &s) {
33969
34076
  // A short name answers from two loads; `OnDemandDocument.isElementName`
33970
34077
  // takes the metadata of the name it compares once per element.
@@ -33989,12 +34096,17 @@ inline Utf16Metadata utf16Metadata(const std::string &s) {
33989
34096
  std::size_t n = len;
33990
34097
  if (!basicLatin) {
33991
34098
  n = i; // ASCII prefix: one unit per byte so far
33992
- for (; i < len; ++i) {
33993
- unsigned char byte = d[i];
33994
- if ((byte & 0xC0) == 0x80) continue;
34099
+ const auto count = [&](unsigned char byte) {
34100
+ if ((byte & 0xC0) == 0x80) return;
33995
34101
  if ((byte & 0xF8) == 0xF0) n += 2;
33996
34102
  else n += 1;
34103
+ };
34104
+ // Each byte counts on its own, so whole words can be classified at once.
34105
+ for (; i + 8 <= len; i += 8) {
34106
+ if (isAsciiWord(d + i)) n += 8;
34107
+ else for (std::size_t k = 0; k < 8; ++k) count(d[i + k]);
33997
34108
  }
34109
+ for (; i < len; ++i) count(d[i]);
33998
34110
  }
33999
34111
  return {n, basicLatin, {}};
34000
34112
  }
@@ -34038,9 +34150,15 @@ inline std::uint32_t decodeCodePointAt(const std::string &s, std::size_t i, std:
34038
34150
  /** Map a JavaScript UTF-16 code-unit offset onto this runtime's UTF-8 storage. */
34039
34151
  inline std::size_t byteOffsetFromUtf16(const std::string &s, std::size_t target) {
34040
34152
  if (isShortBasicLatin(s)) return std::min(target, s.size());
34153
+ const unsigned char *d = reinterpret_cast<const unsigned char *>(s.data());
34041
34154
  std::size_t byte = 0;
34042
34155
  std::size_t units = 0;
34043
34156
  while (byte < s.size() && units < target) {
34157
+ if (units + 8 <= target && byte + 8 <= s.size() && isAsciiWord(d + byte)) {
34158
+ byte += 8;
34159
+ units += 8;
34160
+ continue;
34161
+ }
34044
34162
  std::size_t width = 1;
34045
34163
  const std::uint32_t codePoint = decodeCodePointAt(s, byte, width);
34046
34164
  byte += width;
@@ -34052,10 +34170,16 @@ inline std::size_t byteOffsetFromUtf16(const std::string &s, std::size_t target)
34052
34170
  /** Map a matcher byte boundary back to the UTF-16 index ECMAScript exposes. */
34053
34171
  inline std::size_t utf16OffsetFromByte(const std::string &s, std::size_t target) {
34054
34172
  if (isShortBasicLatin(s)) return std::min(target, s.size());
34173
+ const unsigned char *d = reinterpret_cast<const unsigned char *>(s.data());
34055
34174
  std::size_t byte = 0;
34056
34175
  std::size_t units = 0;
34057
34176
  const std::size_t stop = std::min(target, s.size());
34058
34177
  while (byte < stop) {
34178
+ if (byte + 8 <= stop && isAsciiWord(d + byte)) {
34179
+ byte += 8;
34180
+ units += 8;
34181
+ continue;
34182
+ }
34059
34183
  std::size_t width = 1;
34060
34184
  const std::uint32_t codePoint = decodeCodePointAt(s, byte, width);
34061
34185
  byte += width;
@@ -34163,24 +34287,28 @@ inline std::string substringUtf16(const std::string &s, std::size_t startUnit, s
34163
34287
  // ASCII fast path: if every byte up to `endUnit` is ASCII, UTF-16 indices
34164
34288
  // equal byte indices, so the slice is a plain byte substr -- no
34165
34289
  // per-code-point surrogate accounting and no quadratic walk.
34166
- {
34167
- const unsigned char *d = reinterpret_cast<const unsigned char *>(s.data());
34168
- const std::size_t size = s.size();
34169
- const std::size_t scanLimit = endUnit < size ? endUnit : size;
34170
- std::size_t i = 0;
34171
- while (i < scanLimit && !(d[i] & 0x80)) ++i;
34172
- if (i == scanLimit) {
34173
- const std::size_t b0 = startUnit < size ? startUnit : size;
34174
- const std::size_t b1 = endUnit < size ? endUnit : size;
34175
- if (b0 >= b1) return std::string();
34176
- return s.substr(b0, b1 - b0);
34177
- }
34290
+ const std::size_t scanLimit = endUnit < s.size() ? endUnit : s.size();
34291
+ const std::size_t asciiPrefix = asciiPrefixLength(s, scanLimit);
34292
+ if (asciiPrefix == scanLimit) {
34293
+ const std::size_t b0 = startUnit < s.size() ? startUnit : s.size();
34294
+ if (b0 >= scanLimit) return std::string();
34295
+ return s.substr(b0, scanLimit - b0);
34178
34296
  }
34297
+ const unsigned char *d = reinterpret_cast<const unsigned char *>(s.data());
34179
34298
  std::string out;
34180
34299
  out.reserve((endUnit - startUnit) * 2);
34181
- std::size_t unitPosition = 0;
34182
- std::size_t i = 0;
34300
+ // The prefix just scanned is one unit per byte: resume the walk there.
34301
+ std::size_t unitPosition = asciiPrefix < startUnit ? asciiPrefix : startUnit;
34302
+ std::size_t i = unitPosition;
34183
34303
  while (i < s.size() && unitPosition < endUnit) {
34304
+ // An ASCII word is eight units: counted wholly before the range, copied wholly inside it.
34305
+ if (i + 8 <= s.size() && (unitPosition + 8 <= startUnit || (unitPosition >= startUnit && unitPosition + 8 <= endUnit)) &&
34306
+ isAsciiWord(d + i)) {
34307
+ if (unitPosition >= startUnit) out.append(s, i, 8);
34308
+ i += 8;
34309
+ unitPosition += 8;
34310
+ continue;
34311
+ }
34184
34312
  unsigned char b = static_cast<unsigned char>(s[i]);
34185
34313
  std::size_t length;
34186
34314
  bool astral = false;
@@ -34388,6 +34516,11 @@ inline std::string charAt(const std::string &s, double position) {
34388
34516
  position = integerPosition(position);
34389
34517
  // The first unit of a string whose first byte is ASCII is that byte, without scanning for its UTF-16 length.
34390
34518
  if (position == 0.0 && !s.empty() && static_cast<unsigned char>(s[0]) < 0x80) return std::string(1, s[0]);
34519
+ // So is any unit inside the ASCII prefix: a scan to it, not the whole string's length.
34520
+ if (position > 0.0 && position < static_cast<double>(s.size())) {
34521
+ const std::size_t index = static_cast<std::size_t>(position);
34522
+ if (asciiPrefixLength(s, index + 1) > index) return std::string(1, s[index]);
34523
+ }
34391
34524
  const std::size_t length = utf16Length(s);
34392
34525
  if (!std::isfinite(position) || position < 0.0 || position >= static_cast<double>(length)) return std::string();
34393
34526
  const std::size_t index = static_cast<std::size_t>(position);
@@ -37337,6 +37470,44 @@ struct Pattern {
37337
37470
  // `test`: 0 unknown, 1 compiled, -1 outside the subset.
37338
37471
  mutable signed char simpleState = 0;
37339
37472
  mutable std::shared_ptr<const simple::Program> simpleProgram;
37473
+ // The unit every match must open with, for `matchAt`'s search: -1 unknown,
37474
+ // 0 none.
37475
+ mutable int leadingUnitState = -1;
37476
+
37477
+ /**
37478
+ * The ASCII character a match of `compiledSource` must begin with: its
37479
+ * first atom, when that is a plain character no quantifier can skip and no
37480
+ * top-level `|` routes around -- `/#pragma unroll_loop_start\s+…/`. 0 when
37481
+ * the pattern does not pin one.
37482
+ */
37483
+ int leadingUnit() const {
37484
+ if (leadingUnitState >= 0) return leadingUnitState;
37485
+ leadingUnitState = 0;
37486
+ const std::string &text = compiledSource;
37487
+ if (ignoreCase || unicode || text.empty()) return 0;
37488
+ const unsigned char first = static_cast<unsigned char>(text[0]);
37489
+ if (first == 0 || first >= 0x80 || std::string_view("\\^$.|?*+()[]{}").find(static_cast<char>(first)) != std::string_view::npos) return 0;
37490
+ if (text.size() > 1 && std::string_view("*?{").find(text[1]) != std::string_view::npos) return 0;
37491
+ int depth = 0;
37492
+ bool inClass = false;
37493
+ for (std::size_t i = 0; i < text.size(); ++i) {
37494
+ const char c = text[i];
37495
+ if (c == '\\') {
37496
+ ++i;
37497
+ } else if (inClass) {
37498
+ if (c == ']') inClass = false;
37499
+ } else if (c == '[') {
37500
+ inClass = true;
37501
+ } else if (c == '(') {
37502
+ ++depth;
37503
+ } else if (c == ')') {
37504
+ --depth;
37505
+ } else if (c == '|' && depth == 0) {
37506
+ return 0;
37507
+ }
37508
+ }
37509
+ return leadingUnitState = first;
37510
+ }
37340
37511
 
37341
37512
  const simple::Program *simplePattern() const {
37342
37513
  if (simpleState == 0) {
@@ -37897,6 +38068,28 @@ struct Pattern {
37897
38068
  if (stateful) resetLastIndex();
37898
38069
  return MatchSlice{};
37899
38070
  }
38071
+ // An ASCII literal (`/NUM_DIR_LIGHTS/g`) is a byte search, as in `test`:
38072
+ // no byte of a multi-byte sequence is ASCII, so the bytes match exactly
38073
+ // where the code units do, and a start inside a surrogate pair cannot
38074
+ // begin a match. Transcoding the whole input for `std::regex` on every
38075
+ // match made each `replace` of three.js's shader constants O(n) per match
38076
+ // on top of libc++'s matcher. Sticky stays below: it must not slide off a
38077
+ // half pair.
38078
+ if (!sticky && !unicode && literalPattern() && !literalUtf8.empty() && gea::runtime::string::asciiOnly(literalUtf8)) {
38079
+ const std::size_t from = gea::runtime::string::byteOffsetFromUtf16(input, startPublicUnits);
38080
+ const std::size_t at = input.find(literalUtf8, from);
38081
+ if (at == std::string::npos) {
38082
+ if (stateful) resetLastIndex();
38083
+ return MatchSlice{};
38084
+ }
38085
+ MatchSlice out;
38086
+ out.matched = true;
38087
+ out.position = gea::runtime::string::utf16OffsetFromByte(input, at);
38088
+ out.length = literalUtf8.size();
38089
+ if (stateful) setLastIndexUnits(out.position + out.length);
38090
+ out.captures.push_back(gea::Optional<std::string>(literalUtf8));
38091
+ return out;
38092
+ }
37900
38093
  // A caller may write lastIndex into the middle of a surrogate pair. In
37901
38094
  // Unicode mode CodePointAt sees the trailing surrogate there as a lone
37902
38095
  // code point; preserving this exact boundary keeps sticky/global matching
@@ -37915,11 +38108,30 @@ struct Pattern {
37915
38108
  const Utf16Regex &compiled = compiledRegex();
37916
38109
  Utf16Match match;
37917
38110
  auto searchBegin = inputUnits.cbegin() + static_cast<std::ptrdiff_t>(startUnits);
37918
- if (!std::regex_search(searchBegin, inputUnits.cend(), match, compiled, searchFlagsFrom(inputUnits, startUnits))) {
38111
+ bool found = false;
38112
+ if (const int lead = sticky ? 0 : leadingUnit()) {
38113
+ // libc++ allocates and frees its matcher state at every start it tries,
38114
+ // which made one walk over a shader source (`#pragma unroll_loop_start`)
38115
+ // cost milliseconds. A match can only start at `lead`: try only there,
38116
+ // in order, so the first success is still the leftmost match.
38117
+ for (auto at = searchBegin; (at = std::find(at, inputUnits.cend(), static_cast<RegexUnit>(lead))) != inputUnits.cend(); ++at) {
38118
+ const auto flags =
38119
+ searchFlagsFrom(inputUnits, static_cast<std::size_t>(at - inputUnits.cbegin())) | std::regex_constants::match_continuous;
38120
+ if (std::regex_search(at, inputUnits.cend(), match, compiled, flags)) {
38121
+ searchBegin = at;
38122
+ found = true;
38123
+ break;
38124
+ }
38125
+ }
38126
+ } else {
38127
+ found = std::regex_search(searchBegin, inputUnits.cend(), match, compiled, searchFlagsFrom(inputUnits, startUnits));
38128
+ }
38129
+ if (!found) {
37919
38130
  if (stateful) resetLastIndex();
37920
38131
  return MatchSlice{};
37921
38132
  }
37922
- const std::size_t matchStartUnits = startUnits + static_cast<std::size_t>(match.position(0));
38133
+ const std::size_t matchStartUnits =
38134
+ static_cast<std::size_t>(searchBegin - inputUnits.cbegin()) + static_cast<std::size_t>(match.position(0));
37923
38135
  #endif
37924
38136
  if (sticky && matchStartUnits != startUnits) {
37925
38137
  resetLastIndex();
@@ -43065,7 +43277,7 @@ inline std::string typeOf(const gea::Value &value) {
43065
43277
  // without that dynamic boundary still refuses by name inside `dynamicToString`:
43066
43278
  // manufacturing "[object Object]" would bypass a possible native `toString`.
43067
43279
  inline std::string toString(const gea::Value &value) {
43068
- if (gea::host::isRuntimeError(value)) return gea::host::runtimeErrorString(value);
43280
+ if (gea::host::isErrorValue(value)) return gea::host::runtimeErrorString(value);
43069
43281
  switch (value.tag()) {
43070
43282
  case gea::Value::Tag::Undefined:
43071
43283
  return toStringUndefined();
@@ -43324,9 +43536,15 @@ inline double min_invoke(void*, gea::Ref<gea::ArrayObject<double>> values) {
43324
43536
  * another argument is NaN*; any NaN with no infinity makes it NaN; no
43325
43537
  * arguments at all is +0. And squaring before summing overflows to +Infinity
43326
43538
  * for magnitudes above ~1.34e154 whose true hypotenuse is finite, and flushes
43327
- * to +0 below ~1.5e-162. Scaling every term by the largest magnitude first --
43328
- * the standard formulation, and the one V8 uses -- removes both, and is exact
43329
- * on the cases that should be exact (hypot(3, 4) is 5).
43539
+ * to +0 below ~1.5e-162. Scaling every term by the largest magnitude first
43540
+ * removes both, and is exact on the cases that should be exact (hypot(3, 4)
43541
+ * is 5).
43542
+ *
43543
+ * The sum is V8's: Kahan-compensated, in argument order, then
43544
+ * `sqrt(sum) * largest`, each square rounded on its own. ECMA-262 lets the
43545
+ * result be approximated, so this is for parity with the browser: a plain sum
43546
+ * differs from V8 on about 6% of three-operand calls, and a fused
43547
+ * square-and-add on about 1% of two-operand calls.
43330
43548
  */
43331
43549
  inline double hypot_invoke(void*, gea::Ref<gea::ArrayObject<double>> values) {
43332
43550
  bool sawNaN = false;
@@ -43343,13 +43561,17 @@ inline double hypot_invoke(void*, gea::Ref<gea::ArrayObject<double>> values) {
43343
43561
  }
43344
43562
  if (sawNaN) return std::numeric_limits<double>::quiet_NaN();
43345
43563
  if (largest == 0.0) return 0.0;
43346
- double sumOfSquares = 0.0;
43564
+ double sum = 0.0;
43565
+ double compensation = 0.0;
43347
43566
  for (const auto& slot : values->slots()) {
43348
43567
  if (!slot.present) continue;
43349
- const double scaled = slot.value / largest;
43350
- sumOfSquares += scaled * scaled;
43568
+ const double scaled = std::fabs(slot.value) / largest;
43569
+ const double summand = gea::float64Product(scaled * scaled) - compensation;
43570
+ const double preliminary = sum + summand;
43571
+ compensation = (preliminary - sum) - summand;
43572
+ sum = preliminary;
43351
43573
  }
43352
- return largest * std::sqrt(sumOfSquares);
43574
+ return std::sqrt(sum) * largest;
43353
43575
  }
43354
43576
 
43355
43577
  // ---------------------------------------------------------------------------
@@ -44009,7 +44231,7 @@ inline void registerErrorRecordType(const char* name) {
44009
44231
  */
44010
44232
  inline bool instanceOfError(const gea::Value& value, const char* name) {
44011
44233
  if (value.tag() != gea::Value::Tag::Object) return false;
44012
- if (gea::host::isRuntimeError(value)) return gea::host::instanceOfRuntimeError(value, name);
44234
+ if (gea::host::isErrorValue(value)) return gea::host::instanceOfRuntimeError(value, name);
44013
44235
  const auto found = detail::errorPayloadTypes().find(name);
44014
44236
  if (found == detail::errorPayloadTypes().end()) return false;
44015
44237
  const void* payload = value.payloadType();
@@ -44829,7 +45051,7 @@ inline void log(const std::string& text) {
44829
45051
  std::fputc('\n', stdout);
44830
45052
  }
44831
45053
 
44832
- /** Node routes both `console.warn` and `console.error` to stderr; only `error` has a real call site in the corpus, so only it is implemented. */
45054
+ /** Node routes both `console.warn` and `console.error` to stderr; `warn` below delegates here. */
44833
45055
  inline void error(const std::string& text) {
44834
45056
  std::fputs(text.c_str(), stderr);
44835
45057
  std::fputc('\n', stderr);
@@ -44955,6 +45177,23 @@ inline void info(const std::vector<gea::Value>& values) { log(values); }
44955
45177
 
44956
45178
  inline void info(const gea::Ref<gea::ArrayObject<gea::Value>>& values) { log(values); }
44957
45179
 
45180
+ /**
45181
+ * `console.warn` -- stderr, byte for byte what `console.error` writes.
45182
+ *
45183
+ * Node's `warn` and `error` (`lib/internal/console/constructor.js`) both write
45184
+ * `kFormatForStderr(args)` to `kUseStderr`; they differ only in which
45185
+ * diagnostics channel they publish to, which this runtime does not have. So
45186
+ * `warn` delegates to `error`, as `info` delegates to `log` above, and restates
45187
+ * the same four overloads for the same reason: the arity shape picks the one.
45188
+ */
45189
+ inline void warn(const std::string& text) { error(text); }
45190
+
45191
+ inline void warn(const gea::Value& first, const std::vector<gea::Value>& rest) { error(first, rest); }
45192
+
45193
+ inline void warn(const std::vector<gea::Value>& values) { error(values); }
45194
+
45195
+ inline void warn(const gea::Ref<gea::ArrayObject<gea::Value>>& values) { error(values); }
45196
+
44958
45197
  } // namespace console
44959
45198
 
44960
45199
  } // namespace gea::host
@@ -50529,17 +50768,13 @@ inline void gea_json_read(gea::json::Reader& reader, gea::embedded::ui::Signal<T
50529
50768
  // and `new TextDecoder(label)` for a label this implementation does not
50530
50769
  // support (`RangeError`).
50531
50770
  //
50532
- // v1 raises those through `gea_cpp_reject(gea_cpp_value::error(...))`, which
50533
- // needs its boxed error carrier. This runtime has no such carrier: a thrown
50534
- // value here is a program-generated error struct (`ErrorConstructor::create`
50535
- // is a template over one, declared *after* this header), so a library
50536
- // function in this header cannot construct one. So each of the four aborts BY
50537
- // NAME with the specification's own message and clause, exactly as
50538
- // `detail::typedArrayLengthIndex`, `gea::runtime::array::reduce`'s empty-array
50539
- // TypeError and `detail::refuseOmittedArgument` already do for the same
50540
- // reason. That is a real difference from ECMAScript -- a `try { atob(x) }
50541
- // catch {}` cannot recover -- and it is stated here rather than papered over
50542
- // with a plausible empty string.
50771
+ // `decodeURI`/`decodeURIComponent` throw a catchable `URIError` through
50772
+ // `gea::host::throwRuntimeError`, the runtime-owned Error carrier. `btoa`,
50773
+ // `atob` and the TextDecoder label check still abort BY NAME with the
50774
+ // specification's message: they predate that carrier and have not been moved
50775
+ // onto it, so a `try { atob(x) } catch {}` cannot recover yet. That is a real
50776
+ // difference from ECMAScript, stated here rather than papered over with a
50777
+ // plausible empty string.
50543
50778
  // ===========================================================================
50544
50779
 
50545
50780
  namespace gea::runtime::text {
@@ -50855,11 +51090,7 @@ inline void appendPercentU(std::string& out, std::uint32_t codeUnit) {
50855
51090
  out.push_back(hexDigit(codeUnit & 0x0F));
50856
51091
  }
50857
51092
 
50858
- /** v1: `gea::uri::uri_error` (uri.cpp), re-typed to this runtime's named abort -- see this family's header comment for why. */
50859
- [[noreturn]] inline void refuseMalformedUri() {
50860
- std::fprintf(stderr, "gea: URIError: URI malformed (ECMA-262 19.2.6.5 Decode, step 4.b / 19.2.6.4 Encode, step 4.b)\n");
50861
- gea::detail::abortAfterFlush();
50862
- }
51093
+ [[noreturn]] inline void refuseMalformedUri() { gea::host::throwRuntimeError("URIError", "URI malformed"); }
50863
51094
 
50864
51095
  /** v1: `gea::uri::is_unescaped` (uri.cpp). ECMA-262 19.2.6.1.1's `uriUnescaped` = `uriAlpha` + DecimalDigit + `uriMark`. */
50865
51096
  inline bool isUnescaped(std::uint32_t codePoint) {
@@ -51096,15 +51327,19 @@ inline std::string unescapeImpl(const std::string& input) {
51096
51327
  * than through a second copy.
51097
51328
  */
51098
51329
  inline std::string encodeUri(const std::string& value) { return encodeImpl(value, false); }
51330
+ inline std::string encodeUri(const char* value) { return encodeUri(std::string(value)); }
51099
51331
  inline std::string encodeUri(double value) { return encodeImpl(gea::host::detail::toString(value), false); }
51100
51332
  inline std::string encodeUri(bool value) { return encodeImpl(gea::host::detail::toString(value), false); }
51101
51333
  inline std::string encodeUriComponent(const std::string& value) { return encodeImpl(value, true); }
51334
+ inline std::string encodeUriComponent(const char* value) { return encodeUriComponent(std::string(value)); }
51102
51335
  inline std::string encodeUriComponent(double value) { return encodeImpl(gea::host::detail::toString(value), true); }
51103
51336
  inline std::string encodeUriComponent(bool value) { return encodeImpl(gea::host::detail::toString(value), true); }
51104
51337
  inline std::string decodeUri(const std::string& value) { return decodeImpl(value, false); }
51338
+ inline std::string decodeUri(const char* value) { return decodeUri(std::string(value)); }
51105
51339
  inline std::string decodeUri(double value) { return decodeImpl(gea::host::detail::toString(value), false); }
51106
51340
  inline std::string decodeUri(bool value) { return decodeImpl(gea::host::detail::toString(value), false); }
51107
51341
  inline std::string decodeUriComponent(const std::string& value) { return decodeImpl(value, true); }
51342
+ inline std::string decodeUriComponent(const char* value) { return decodeUriComponent(std::string(value)); }
51108
51343
  inline std::string decodeUriComponent(double value) { return decodeImpl(gea::host::detail::toString(value), true); }
51109
51344
  inline std::string decodeUriComponent(bool value) { return decodeImpl(gea::host::detail::toString(value), true); }
51110
51345
 
@@ -51707,11 +51942,23 @@ inline bool isRuntimeError(const gea::Value& value) {
51707
51942
  return value.tag() == gea::Value::Tag::Object &&
51708
51943
  value.payloadType() == gea::detail::payloadTypeTagFor<gea::Ref<gea::runtime::Error>>();
51709
51944
  }
51945
+ inline gea::Ref<gea::runtime::Error> runtimeErrorOf(const gea::Value& value) {
51946
+ if (value.tag() != gea::Value::Tag::Object) return {};
51947
+ if (value.payloadType() == gea::detail::payloadTypeTagFor<gea::Ref<gea::runtime::Error>>()) return value.as<gea::Ref<gea::runtime::Error>>();
51948
+ if (value.classObject() && gea::detail::classIdentityExtends(value.classIdentity(), &gea::detail::RefOperationsFor<gea::runtime::Error>::table))
51949
+ return value.classObject().template staticCast<gea::runtime::Error>();
51950
+ return {};
51951
+ }
51952
+ inline bool isErrorValue(const gea::Value& value) { return static_cast<bool>(runtimeErrorOf(value)); }
51710
51953
  inline bool instanceOfRuntimeError(const gea::Value& value, const char* kind) {
51711
- return isRuntimeError(value) && value.as<gea::Ref<gea::runtime::Error>>()->instanceOf(kind);
51954
+ const auto error = runtimeErrorOf(value);
51955
+ return error && error->instanceOf(kind);
51712
51956
  }
51713
51957
  inline std::string runtimeErrorString(const gea::Value& value) {
51714
- return gea::detail::unboxAs<gea::Ref<gea::runtime::Error>>(value, gea::Value::Tag::Object, "Error ToString")->toString();
51958
+ const auto error = runtimeErrorOf(value);
51959
+ if (!error) return gea::detail::unboxAs<gea::Ref<gea::runtime::Error>>(value, gea::Value::Tag::Object, "Error ToString")->toString();
51960
+ if (value.classObject() && value.getProperty(gea::PropertyKey::string("toString")).tag() == gea::Value::Tag::Function) return gea::dynamicToString(value);
51961
+ return error->toString();
51715
51962
  }
51716
51963
  [[noreturn]] inline void throwRuntimeError(const char* kind, const std::string& message) {
51717
51964
  #if defined(GEA_RUNTIME_THROW_ENDS_PROGRAM) && GEA_RUNTIME_THROW_ENDS_PROGRAM
@@ -51736,7 +51983,7 @@ inline const bool uncaughtDescriberInstalled = [] {
51736
51983
  std::rethrow_exception(current);
51737
51984
  } catch (const gea::Value& value) {
51738
51985
  try {
51739
- text = gea::host::isRuntimeError(value) ? gea::host::runtimeErrorString(value) : gea::dynamicToString(value);
51986
+ text = gea::host::isErrorValue(value) ? gea::host::runtimeErrorString(value) : gea::dynamicToString(value);
51740
51987
  } catch (...) {
51741
51988
  text = "<a value whose ToString threw>";
51742
51989
  }