@geastack/compiler 1.0.19 → 1.0.20

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.
package/dist/cli.js CHANGED
File without changes
@@ -61,7 +61,11 @@ export const geaCppPrelude = (options) => {
61
61
  // has `<p>`s and `<h1>`s, and the cascade breaks equal-specificity ties by
62
62
  // source order, so anything the app states about the same tag has to be
63
63
  // registered after this to win.
64
- const body = tape.length === 0 ? geaUserAgentStyleSheet : `${geaUserAgentStyleSheet}\n${tape}`;
64
+ const styles = tape.length === 0 ? geaUserAgentStyleSheet : `${geaUserAgentStyleSheet}\n${tape}`;
65
+ // The build's proven canvas-only mode does not link the CSS engine. Keep
66
+ // this decision on the same native definition as the frame loop; the
67
+ // compiler still emits identical typed canvas calls for both modes.
68
+ const body = `#if !defined(GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT) || !GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT\n${styles}\n#endif`;
65
69
  const provenance = `// @geastack/compiler gea C++ prelude\n// prelude=${tapePath ?? '<none: user-agent defaults only>'}`;
66
70
  if (named)
67
71
  return [`${provenance}\nvoid ${symbol}() {\n${indent(body, ' ')}\n}`];
@@ -727,7 +727,7 @@ export const jsonCallText = (ctx, member, operation) => {
727
727
  throw createCppEmitBlockedError('host-member-call:JSON.parse', `JSON.parse cannot decode natively into this asserted type: ${reason}`);
728
728
  const textArgument = operandText(ctx, operation.arguments[0]);
729
729
  const resultType = cppTypeOf(resultRepresentation);
730
- return `[&]() { const std::string& gea_json_text = ${textArgument}; gea::json::Reader gea_json_reader(gea_json_text); ${resultType} gea_json_result{}; gea_json_read(gea_json_reader, gea_json_result); gea_json_reader.finish(); return gea_json_result; }()`;
730
+ return `[&]() { const std::string& gea_json_text = ${textArgument}; gea::json::Reader gea_json_reader(gea_json_text); ${resultType} gea_json_result{}; gea_json_read(gea_json_reader, gea_json_result); return gea_json_result; }()`;
731
731
  };
732
732
  /**
733
733
  * `JSON.stringify(v)` written straight into the cell that receives it, as
@@ -217,7 +217,7 @@ export const hostJsonMemberValueText = (member, representation) => {
217
217
  if (!jsonRuntimeDeclaredCarrier(abi.result))
218
218
  return null;
219
219
  return (`[&]() { gea::json::Reader gea_json_reader(${only}); ${cppTypeOf(abi.result)} gea_json_result{}; ` +
220
- `gea_json_read(gea_json_reader, gea_json_result); gea_json_reader.finish(); return gea_json_result; }()`);
220
+ `gea_json_read(gea_json_reader, gea_json_result); return gea_json_result; }()`);
221
221
  }
222
222
  const argument = abi.parameters[0];
223
223
  if (argument === undefined || !jsonRuntimeDeclaredCarrier(argument.value))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/compiler",
3
- "version": "1.0.19",
3
+ "version": "1.0.20",
4
4
  "description": "The geastack TypeScript-to-C++ compiler: one semantic spine, no source-shaped authority, no boxing of statically typed values.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -27268,15 +27268,15 @@ inline std::size_t jsonEscapeScan(const char* data, std::size_t size, std::size_
27268
27268
  }
27269
27269
 
27270
27270
  /**
27271
- * The READER's scan: the same three stops as the writer's.
27271
+ * The READER's scan: only `"` and `\` end a run.
27272
27272
  *
27273
- * A control character inside a quoted string is malformed JSON (RFC 8259 7,
27274
- * ECMA-262 25.5.1's `JSONString` grammar), so it must end the run exactly as
27275
- * a quote or an escape lead does -- the caller then throws on it. This reader
27276
- * used to copy one through, which answered a document `JSON.parse` rejects.
27273
+ * A control character inside a quoted string is malformed JSON (RFC 8259 7)
27274
+ * that this reader has always accepted and copied through, so it must not stop
27275
+ * the run here -- the caller's next step is to read the byte as an escape lead
27276
+ * or a closing quote, and a control character is neither.
27277
27277
  */
27278
27278
  inline std::size_t jsonStringScan(const char* data, std::size_t size, std::size_t from) {
27279
- return detail::jsonScan<true>(data, size, from);
27279
+ return detail::jsonScan<false>(data, size, from);
27280
27280
  }
27281
27281
 
27282
27282
  /**
@@ -27287,24 +27287,30 @@ inline std::size_t jsonStringScan(const char* data, std::size_t size, std::size_
27287
27287
  *
27288
27288
  * v1 (`compiler/packages/geatsc/src/targets/cpp/runtime/json_reader.h`) is
27289
27289
  * the prior art for a typed, non-boxing `JSON.parse(...) as T` decoder, and
27290
- * one of its choices is kept: a well-formed document whose SHAPE does not
27291
- * match `T` -- an extra key, a string where `T` declares a number -- is not an
27292
- * error. The mismatched value is skipped (`skipValue`) and the field keeps its
27293
- * default. That is this backend's own answer, because ECMA-262's `JSON.parse`
27294
- * has no `T` to mismatch.
27295
- *
27296
- * Text that is not JSON at all is a different question, and ECMA-262 25.5.1
27297
- * step 2 answers it: a catchable `SyntaxError`. Every grammar violation below
27298
- * -- an unterminated container, a missing `:` or `,`, a bad literal, number,
27299
- * escape or control character, trailing text after the root (`finish`) --
27300
- * goes through `malformed`, which throws exactly that. The reader used to
27301
- * stop silently at the first one instead and hand back whatever it had read
27302
- * (`JSON.parse('')` was an empty record): a silently wrong answer, which is
27303
- * worse than either throwing or refusing to compile.
27304
- *
27305
- * Termination: every loop below either advances `pos_` or throws, and the
27306
- * buffer's NUL terminator opens no token, so the end of the text always ends
27307
- * a scan.
27290
+ * its central design choice is adopted here: a document whose shape doesn't
27291
+ * match `T` -- an extra key, a string where `T` declares a number, a
27292
+ * truncated array -- must never bring the whole program down. This class was
27293
+ * an earlier draft that reported any mismatch through `std::abort()`
27294
+ * (mirroring `ArrayObject::requireIndex`'s abort-on-fault elsewhere in this
27295
+ * header); that is wrong for this boundary specifically, because the text
27296
+ * being decoded is a network response or a stored file the program does not
27297
+ * control, and ECMA-262 itself never crashes the process over one -- it
27298
+ * throws a catchable `SyntaxError`. `emit-json.ts` has no
27299
+ * `abrupt-edge:throw` carrier to route that through today (see this port's
27300
+ * risks.md), so the answer adopted here sits strictly between "throw" and
27301
+ * "abort": every method below is a no-op once `truncated()` is set, so the
27302
+ * FIRST shape mismatch or grammar violation stops the parse there --
27303
+ * whatever struct fields were already read keep their value, everything
27304
+ * after keeps its default-constructed one -- rather than either raising a
27305
+ * catchable error (unavailable) or aborting the process (unacceptable at
27306
+ * this boundary). This is a stricter, simpler-to-verify middle ground than
27307
+ * v1's own per-field skip-and-keep-going recovery (v1 resynchronizes at the
27308
+ * next key after a mismatched value; this stops the whole parse instead),
27309
+ * traded deliberately for a design whose termination is provable by
27310
+ * inspection without executing the compiled reader -- this port's harness is
27311
+ * restricted to `-fsyntax-only` and must not run generated binaries. Every
27312
+ * loop below only ever advances `pos_` or reads `truncated()` and returns;
27313
+ * `truncate()` snaps `pos_` to `text_.size()`, so nothing can spin.
27308
27314
  */
27309
27315
  class Reader {
27310
27316
  public:
@@ -27321,16 +27327,9 @@ class Reader {
27321
27327
  */
27322
27328
  explicit Reader(const std::string& text) : text_(text), data_(text.data()), size_(text.size()), pos_(0) {}
27323
27329
 
27324
- /** ECMA-262 25.5.1 step 2: text that is not valid JSON throws a `SyntaxError`. The message follows V8's shape, position included. */
27325
- [[noreturn]] void malformed(const char* what) const {
27326
- gea::host::throwRuntimeError("SyntaxError", std::string(what) + " in JSON at position " + std::to_string(pos_));
27327
- }
27330
+ bool truncated() const { return pos_ >= size_; }
27328
27331
 
27329
- /** The text after the root value may only be whitespace (the grammar's whole `JSONText` is one value). */
27330
- void finish() {
27331
- skipWhitespace();
27332
- if (pos_ < size_) malformed("Unexpected non-whitespace character after JSON");
27333
- }
27332
+ void truncate() { pos_ = size_; }
27334
27333
 
27335
27334
  void skipWhitespace() {
27336
27335
  // Every JSON whitespace byte is at or below 0x20, and every byte that can
@@ -27349,7 +27348,7 @@ class Reader {
27349
27348
  }
27350
27349
  }
27351
27350
 
27352
- /** Skips leading whitespace and returns the byte at the cursor, or `'\0'` past the end of the text -- `'\0'` opens no JSON token, so every caller below reads it as "nothing usable is here" without a separate bound test. */
27351
+ /** Skips leading whitespace and returns the byte at the cursor, or `'\0'` past the end of the text -- `'\0'` can't appear in a well-formed document's structural positions, so every caller below can treat it as "nothing usable is here" without a separate `truncated()` check. */
27353
27352
  char peek() {
27354
27353
  skipWhitespace();
27355
27354
  // No bound test: `std::string::data()` is NUL-terminated, so the byte at
@@ -27407,11 +27406,17 @@ class Reader {
27407
27406
  return true;
27408
27407
  }
27409
27408
 
27410
- /** Reads the next property key and consumes the following `:`, leaving the cursor at the value. Anything other than a quoted key and its colon is malformed; the `bool` is always `true` and kept so the generated key loop reads the same either way. */
27409
+ /** Reads the next property key and consumes the following `:`, leaving the cursor at the value. Returns `false` (and truncates) at anything other than a quoted key -- the generated key loop's caller then stops normally via `truncated()`. */
27411
27410
  bool nextKey(std::string& key) {
27412
- if (peek() != '"') malformed("Expected double-quoted property name");
27411
+ if (peek() != '"') {
27412
+ truncate();
27413
+ return false;
27414
+ }
27413
27415
  readStringField(key);
27414
- if (!consumeIf(':')) malformed("Expected ':' after property name");
27416
+ if (!consumeIf(':')) {
27417
+ truncate();
27418
+ return false;
27419
+ }
27415
27420
  return true;
27416
27421
  }
27417
27422
 
@@ -27455,7 +27460,10 @@ class Reader {
27455
27460
  * of the read -- `Reader` holds the text by reference already.
27456
27461
  */
27457
27462
  bool nextKey(std::string_view& key, std::string& scratch) {
27458
- if (peek() != '"') malformed("Expected double-quoted property name");
27463
+ if (peek() != '"') {
27464
+ truncate();
27465
+ return false;
27466
+ }
27459
27467
  const std::size_t opened = pos_ + 1;
27460
27468
  const std::size_t stop = jsonStringScan(data_, size_, opened);
27461
27469
  if (stop < size_ && data_[stop] == '"') {
@@ -27465,14 +27473,16 @@ class Reader {
27465
27473
  readStringField(scratch);
27466
27474
  key = scratch;
27467
27475
  }
27468
- if (!consumeIf(':')) malformed("Expected ':' after property name");
27476
+ if (!consumeIf(':')) {
27477
+ truncate();
27478
+ return false;
27479
+ }
27469
27480
  return true;
27470
27481
  }
27471
27482
 
27472
27483
  /**
27473
27484
  * Consumes the separator after a field value: `,` (more keys follow) or
27474
- * `}` (the object ends). Anything else -- the end of the text included --
27475
- * is an unterminated object.
27485
+ * `}`/anything else (object ends).
27476
27486
  *
27477
27487
  * The byte is read ONCE. Spelled as `consumeIf(',')` then `consumeIf('}')` it
27478
27488
  * was two `peek`s -- two whitespace skips and two bounds checks -- for the one
@@ -27485,12 +27495,11 @@ class Reader {
27485
27495
  ++pos_;
27486
27496
  return true;
27487
27497
  }
27488
- if (c != '}') malformed("Expected ',' or '}' after property value");
27489
- ++pos_;
27498
+ if (c == '}') ++pos_;
27490
27499
  return false;
27491
27500
  }
27492
27501
 
27493
- /** The same, for an ARRAY element: `,` (more follow) or `]` (the array ends). */
27502
+ /** The same, for an ARRAY element: `,` (more follow) or `]`/anything else (the array ends). */
27494
27503
  bool advanceArray() {
27495
27504
  skipWhitespace();
27496
27505
  const char c = data_[pos_];
@@ -27498,8 +27507,7 @@ class Reader {
27498
27507
  ++pos_;
27499
27508
  return true;
27500
27509
  }
27501
- if (c != ']') malformed("Expected ',' or ']' after array element");
27502
- ++pos_;
27510
+ if (c == ']') ++pos_;
27503
27511
  return false;
27504
27512
  }
27505
27513
 
@@ -27510,15 +27518,11 @@ class Reader {
27510
27518
  skipValue();
27511
27519
  return std::numeric_limits<double>::quiet_NaN();
27512
27520
  }
27513
- // `+` and `.` open no JSON number (RFC 8259 6), but they are admitted by
27514
- // the test above so that the error names the number rather than calling
27515
- // the byte a mismatched shape.
27516
- if (c == '+' || c == '.') malformed("Unexpected token in JSON number");
27517
27521
  const std::size_t start = pos_;
27518
27522
  // `peek()` already read this byte and proved it opens a number, so the sign
27519
27523
  // is decided from `c` rather than loaded and bounds-checked again.
27520
27524
  const bool negative = c == '-';
27521
- if (negative) ++pos_;
27525
+ if (negative || c == '+') ++pos_;
27522
27526
  // The digits are accumulated during the SAME scan that finds the token's
27523
27527
  // end, so a plain integer -- the overwhelmingly common JSON number -- is
27524
27528
  // answered here and never reaches `std::from_chars`, which is a general
@@ -27540,26 +27544,30 @@ class Reader {
27540
27544
  ++pos_;
27541
27545
  }
27542
27546
  const std::size_t digits = pos_ - digitsAt;
27543
- // The integer part is `0` or a digit run with no leading zero: `-`, `-.5`
27544
- // and `01` are all malformed.
27545
- if (digits == 0) malformed("No number after minus sign");
27546
- if (digits > 1 && data_[digitsAt] == '0') malformed("Unexpected number");
27547
27547
  const char next = data_[pos_];
27548
- if (digits <= 19 && next != '.' && next != 'e' && next != 'E')
27548
+ if (digits != 0 && digits <= 19 && next != '.' && next != 'e' && next != 'E')
27549
27549
  return negative ? -static_cast<double>(whole) : static_cast<double>(whole);
27550
- // A fraction and an exponent each need at least one digit of their own.
27551
- if (data_[pos_] == '.') {
27550
+ if (pos_ < size_ && data_[pos_] == '.') {
27552
27551
  ++pos_;
27553
- const std::size_t fraction = pos_;
27554
- while (data_[pos_] >= '0' && data_[pos_] <= '9') ++pos_;
27555
- if (pos_ == fraction) malformed("Unterminated fractional number");
27552
+ while (pos_ < size_ && data_[pos_] >= '0' && data_[pos_] <= '9') ++pos_;
27556
27553
  }
27557
- if (data_[pos_] == 'e' || data_[pos_] == 'E') {
27554
+ if (pos_ < size_ && (data_[pos_] == 'e' || data_[pos_] == 'E')) {
27558
27555
  ++pos_;
27559
- if (data_[pos_] == '+' || data_[pos_] == '-') ++pos_;
27560
- const std::size_t exponent = pos_;
27561
- while (data_[pos_] >= '0' && data_[pos_] <= '9') ++pos_;
27562
- if (pos_ == exponent) malformed("Exponent part is missing a number");
27556
+ if (pos_ < size_ && (data_[pos_] == '+' || data_[pos_] == '-')) ++pos_;
27557
+ while (pos_ < size_ && data_[pos_] >= '0' && data_[pos_] <= '9') ++pos_;
27558
+ }
27559
+ // Unreachable: the guard above already requires `c` to be one of
27560
+ // `-+.<digit>`, and the sign-check or the dot-check right below it
27561
+ // therefore always advances `pos_` by at least one byte. Kept as a
27562
+ // `assert`-free defensive floor rather than a recursive call back into
27563
+ // `skipValue()` -- a construct this method must never contain, since
27564
+ // `skipValue()`'s own number branch is what calls `readNumberField()` in
27565
+ // the first place, and a `skipValue()` <-> `readNumberField()` cycle
27566
+ // would defeat the single-direction progress argument every other loop
27567
+ // in this class relies on.
27568
+ if (pos_ == start) {
27569
+ truncate(); // defensive floor for the "unreachable" case above: never leave `pos_` unmoved
27570
+ return std::numeric_limits<double>::quiet_NaN();
27563
27571
  }
27564
27572
  // `std::from_chars` rather than `strtod` WHERE IT EXISTS: the same
27565
27573
  // shortest-round-trip answer, decided by an integer algorithm instead of a
@@ -27570,10 +27578,13 @@ class Reader {
27570
27578
  // integer fast path above, it is now the rare shape either way.
27571
27579
  // `strtod` was 54ns of the 54ns `json_parse_number_array` spent per number.
27572
27580
  //
27573
- // The token is grammatical by now, so both parse it whole; `from_chars`
27574
- // is still checked for that and falls back rather than trusted.
27581
+ // Two shapes it declines that this reader accepts: a leading `+` (JSON's
27582
+ // own grammar has no such number, but the scan above consumes one) and a
27583
+ // token `strtod` would parse further than the scan did. The first is
27584
+ // stepped over; anything the parse does not consume WHOLE falls back, so
27585
+ // no input answers differently than it did.
27575
27586
  #if GEA_JSON_HAS_FROM_CHARS_DOUBLE
27576
- const char* const first = data_ + start;
27587
+ const char* const first = data_ + start + (text_[start] == '+' ? 1 : 0);
27577
27588
  const char* const last = data_ + pos_;
27578
27589
  double parsed = 0;
27579
27590
  const auto answer = std::from_chars(first, last, parsed);
@@ -27638,9 +27649,8 @@ class Reader {
27638
27649
  }
27639
27650
 
27640
27651
  /**
27641
- * The rest of a string that turned out to carry an escape (or a control
27642
- * character, or no closing quote), resumed with the cursor on the byte the
27643
- * scan stopped at. Kept out of
27652
+ * The rest of a string that turned out to carry an escape, resumed with the
27653
+ * cursor on the escape lead (or past the end, which truncates). Kept out of
27644
27654
  * line: the `\uXXXX` decoder alone is bigger than everything above it, and a
27645
27655
  * document that never uses one should not carry it through every call.
27646
27656
  */
@@ -27657,17 +27667,21 @@ class Reader {
27657
27667
  // appends reallocates and copies O(log length) times. A JSON string
27658
27668
  // containing no escape at all -- the overwhelmingly common shape -- now
27659
27669
  // costs exactly one allocation and one `memcpy`. Correctness is unchanged
27660
- // because the scan stops at every byte the loop below distinguishes: the
27661
- // closing quote, the escape lead, and a control character the grammar
27662
- // forbids (RFC 8259 7).
27663
- if (pos_ >= size_) malformed("Unterminated string");
27670
+ // because the scan stops at the only two bytes the loop below
27671
+ // distinguishes: the closing quote and the escape lead. Everything else
27672
+ // is copied verbatim by both spellings, including a control character the
27673
+ // grammar forbids (RFC 8259 7), which this reader has always accepted
27674
+ // rather than rejected.
27675
+ if (pos_ >= size_) {
27676
+ truncate();
27677
+ return;
27678
+ }
27664
27679
  const char c = data_[pos_++];
27665
27680
  if (c == '"') return;
27666
- if (c != '\\') {
27667
- --pos_;
27668
- malformed("Bad control character in string literal");
27681
+ if (pos_ >= size_) {
27682
+ truncate();
27683
+ return;
27669
27684
  }
27670
- if (pos_ >= size_) malformed("Unterminated string");
27671
27685
  const char escape = data_[pos_++];
27672
27686
  switch (escape) {
27673
27687
  case '"': out += '"'; break;
@@ -27679,7 +27693,10 @@ class Reader {
27679
27693
  case 'r': out += '\r'; break;
27680
27694
  case 't': out += '\t'; break;
27681
27695
  case 'u': {
27682
- if (pos_ + 4 > size_) malformed("Bad Unicode escape");
27696
+ if (pos_ + 4 > size_) {
27697
+ truncate();
27698
+ return;
27699
+ }
27683
27700
  unsigned code = 0;
27684
27701
  bool validHex = true;
27685
27702
  for (int digit = 0; digit < 4; ++digit) {
@@ -27690,7 +27707,10 @@ class Reader {
27690
27707
  else if (h >= 'A' && h <= 'F') code |= static_cast<unsigned>(h - 'A' + 10);
27691
27708
  else validHex = false;
27692
27709
  }
27693
- if (!validHex) malformed("Bad Unicode escape");
27710
+ if (!validHex) {
27711
+ truncate();
27712
+ return;
27713
+ }
27694
27714
  // UTF-8 encode the code point. A surrogate pair (an astral
27695
27715
  // character split across two `\uXXXX` escapes) is not reassembled
27696
27716
  // -- no corpus document this backend parses has one -- so each
@@ -27708,8 +27728,8 @@ class Reader {
27708
27728
  break;
27709
27729
  }
27710
27730
  default:
27711
- --pos_;
27712
- malformed("Bad escaped character");
27731
+ truncate();
27732
+ return;
27713
27733
  }
27714
27734
  const std::size_t run = pos_;
27715
27735
  pos_ = jsonStringScan(data_, size_, pos_);
@@ -27733,46 +27753,49 @@ class Reader {
27733
27753
  ++pos_;
27734
27754
  for (;;) {
27735
27755
  pos_ = jsonStringScan(data_, size_, pos_);
27736
- if (pos_ >= size_) malformed("Unterminated string");
27737
- const char c = data_[pos_];
27738
- if (c == '"') {
27739
- ++pos_;
27756
+ if (pos_ >= size_) {
27757
+ truncate();
27740
27758
  return;
27741
27759
  }
27742
- if (c != '\\') malformed("Bad control character in string literal");
27743
- // A skipped string is still part of the document, so its escapes are
27744
- // held to the same grammar as a decoded one's.
27745
- const char escape = data_[pos_ + 1];
27746
- if (escape == 'u') {
27747
- for (std::size_t digit = 2; digit < 6; ++digit) {
27748
- const char h = pos_ + digit < size_ ? data_[pos_ + digit] : '\0';
27749
- if (!((h >= '0' && h <= '9') || (h >= 'a' && h <= 'f') || (h >= 'A' && h <= 'F'))) malformed("Bad Unicode escape");
27750
- }
27751
- pos_ += 6;
27752
- continue;
27760
+ if (data_[pos_] == '"') {
27761
+ ++pos_;
27762
+ return;
27753
27763
  }
27754
- if (escape != '"' && escape != '\\' && escape != '/' && escape != 'b' && escape != 'f' && escape != 'n' && escape != 'r' &&
27755
- escape != 't')
27756
- malformed("Bad escaped character");
27757
27764
  pos_ += 2;
27765
+ if (pos_ > size_) {
27766
+ truncate();
27767
+ return;
27768
+ }
27758
27769
  }
27759
27770
  }
27760
27771
 
27761
- /** Skips one well-formed JSON value this program's declared shape does not name -- an object key nothing here reads, or the wrong-shaped value behind a field whose static type doesn't match what's actually in the document (ECMA-262's own `JSON.parse` skips nothing at all; a `T` narrower than the document, and refusing a mismatch rather than coercing it, are this backend's own choices -- see `emit-json.ts`'s top comment). Text that is not a JSON value at all is malformed. Every branch either consumes at least one byte or throws, so this always terminates even on adversarial input. */
27772
+ /** Skips one well-formed JSON value this program's declared shape does not name -- an object key nothing here reads, or the wrong-shaped value behind a field whose static type doesn't match what's actually in the document (ECMA-262's own `JSON.parse` skips nothing at all; a `T` narrower than the document, and refusing a mismatch rather than coercing it, are this backend's own choices -- see `emit-json.ts`'s top comment). Every branch either consumes at least one byte or calls `truncate()`, so this always terminates even on adversarial input. */
27762
27773
  void skipValue() {
27763
27774
  const char c = peek();
27764
- if (c == '\0') malformed("Unexpected end of JSON input");
27775
+ if (c == '\0') {
27776
+ truncate();
27777
+ return;
27778
+ }
27765
27779
  if (c == '"') {
27766
27780
  skipString();
27767
27781
  } else if (c == '{') {
27768
27782
  ++pos_;
27769
27783
  if (!consumeIf('}')) {
27770
27784
  for (;;) {
27771
- if (peek() != '"') malformed("Expected double-quoted property name");
27785
+ if (peek() != '"') {
27786
+ truncate();
27787
+ break;
27788
+ }
27772
27789
  skipString();
27773
- if (!consumeIf(':')) malformed("Expected ':' after property name");
27790
+ if (!consumeIf(':')) {
27791
+ truncate();
27792
+ break;
27793
+ }
27774
27794
  skipValue();
27775
- if (!advance()) break;
27795
+ if (truncated()) break;
27796
+ if (consumeIf(',')) continue;
27797
+ consumeIf('}');
27798
+ break;
27776
27799
  }
27777
27800
  }
27778
27801
  } else if (c == '[') {
@@ -27780,22 +27803,29 @@ class Reader {
27780
27803
  if (!consumeIf(']')) {
27781
27804
  for (;;) {
27782
27805
  skipValue();
27783
- if (!advanceArray()) break;
27806
+ if (truncated()) break;
27807
+ if (consumeIf(',')) continue;
27808
+ consumeIf(']');
27809
+ break;
27784
27810
  }
27785
27811
  }
27786
27812
  } else if (c == 't') {
27787
- if (!consumeLiteral("true", 4)) malformed("Unexpected token");
27813
+ if (!consumeLiteral("true", 4)) truncate();
27788
27814
  } else if (c == 'f') {
27789
- if (!consumeLiteral("false", 5)) malformed("Unexpected token");
27815
+ if (!consumeLiteral("false", 5)) truncate();
27790
27816
  } else if (c == 'n') {
27791
- if (!consumeLiteral("null", 4)) malformed("Unexpected token");
27817
+ if (!consumeLiteral("null", 4)) truncate();
27792
27818
  } else if (c == '-' || c == '+' || c == '.' || (c >= '0' && c <= '9')) {
27793
- // Never a mismatch here: `readNumberField` either consumes a whole
27794
- // grammatical number or throws, so it cannot hand the same byte back.
27819
+ const std::size_t start = pos_;
27795
27820
  readNumberField();
27821
+ if (pos_ == start) truncate(); // readNumberField saw digits at peek() but somehow consumed none: give up cleanly rather than loop
27796
27822
  } else {
27797
- // A byte that opens no JSON value at all.
27798
- malformed("Unexpected token");
27823
+ // A byte that opens no JSON value at all (garbage). Consuming it and
27824
+ // stopping the parse -- rather than looping back through `skipValue`
27825
+ // on the same byte -- is what keeps every caller above provably
27826
+ // terminating.
27827
+ ++pos_;
27828
+ truncate();
27799
27829
  }
27800
27830
  }
27801
27831
 
@@ -27804,15 +27834,15 @@ class Reader {
27804
27834
  const char c = peek();
27805
27835
  if (c == '"') return gea::Value::box(gea::Value::Tag::String, readStringField());
27806
27836
  if (c == 't') {
27807
- if (!consumeLiteral("true", 4)) malformed("Unexpected token");
27837
+ if (!consumeLiteral("true", 4)) { truncate(); return gea::Value(); }
27808
27838
  return gea::Value::box(gea::Value::Tag::Boolean, true);
27809
27839
  }
27810
27840
  if (c == 'f') {
27811
- if (!consumeLiteral("false", 5)) malformed("Unexpected token");
27841
+ if (!consumeLiteral("false", 5)) { truncate(); return gea::Value(); }
27812
27842
  return gea::Value::box(gea::Value::Tag::Boolean, false);
27813
27843
  }
27814
27844
  if (c == 'n') {
27815
- if (!consumeLiteral("null", 4)) malformed("Unexpected token");
27845
+ if (!consumeLiteral("null", 4)) { truncate(); return gea::Value(); }
27816
27846
  return gea::Value::box(gea::Value::Tag::Null, static_cast<std::nullptr_t>(nullptr));
27817
27847
  }
27818
27848
  if (c == '[') {
@@ -27821,7 +27851,7 @@ class Reader {
27821
27851
  if (!consumeIf(']')) {
27822
27852
  for (;;) {
27823
27853
  array->push(readDynamicValue());
27824
- if (!advanceArray()) break;
27854
+ if (truncated() || !advanceArray()) break;
27825
27855
  }
27826
27856
  }
27827
27857
  return gea::Value::box(gea::Value::Tag::Object, array);
@@ -27838,9 +27868,9 @@ class Reader {
27838
27868
  if (!consumeIf('}')) {
27839
27869
  std::string key;
27840
27870
  for (;;) {
27841
- nextKey(key);
27871
+ if (!nextKey(key)) break;
27842
27872
  (*object)[key] = readDynamicValue();
27843
- if (!advance()) break;
27873
+ if (truncated() || !advance()) break;
27844
27874
  }
27845
27875
  }
27846
27876
  return gea::Value::box(gea::Value::Tag::Object, object);
@@ -27848,9 +27878,8 @@ class Reader {
27848
27878
  if (c == '-' || c == '+' || c == '.' || (c >= '0' && c <= '9')) {
27849
27879
  return gea::Value::box(gea::Value::Tag::Number, readNumberField());
27850
27880
  }
27851
- // Every token that opens a JSON value was answered above, so this is the
27852
- // end of the text or a byte no value starts with.
27853
- malformed(c == '\0' ? "Unexpected end of JSON input" : "Unexpected token");
27881
+ skipValue();
27882
+ return gea::Value();
27854
27883
  }
27855
27884
 
27856
27885
  /** The cursor, for a caller measuring what one value cost to read. See `elementCapacityEstimate`. */
@@ -28375,6 +28404,7 @@ inline void gea_json_read(gea::json::Reader& reader, gea::Ref<gea::ArrayObject<E
28375
28404
  out->cells.emplace_back();
28376
28405
  gea_json_read(reader, out->cells.back().value);
28377
28406
  }
28407
+ if (reader.truncated()) return;
28378
28408
  ++seen;
28379
28409
  if (!sized) {
28380
28410
  const std::size_t hint = gea::json::elementCapacityEstimate(seen, reader.position() - opened, reader.remaining());