Skip to content

Add the buffer_utf8 data type and an optional size for buffer_read and buffer_peek (GameMaker-Bugs#16485) - #809

Merged
rwkay merged 4 commits into
developfrom
buffers/gmb-16485
Nov 23, 2026
Merged

rwkay merged 4 commits into
developfrom
buffers/gmb-16485

Conversation

@rwkay

@rwkay rwkay commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

HTML5 part of YoYoGames/GameMaker-Bugs#16485: the buffer_utf8 data type and an optional size for buffer_read / buffer_peek. Needs the asset compiler from YoYoGames/GameMaker#5591 (the buffer_utf8 constant, and buffer_read / buffer_peek taking 2-3 / 3-4 arguments).

Changes (scripts/yyBuffer.js)

  • eBuffer_UTF8 = 14.
  • New yyDecodeUTF8(dataView, offset, end): decodes one character without reading at or past end, returning the code point, or U+FFFD with a length of 1 for a malformed sequence (a continuation byte or a byte that can't start a character, a missing or bad continuation byte, an overlong encoding, a surrogate, a value above U+10FFFF, or a sequence cut off by end). yyEncodeUTF8(value) encodes a code point, giving U+FFFD for a value that isn't one.
  • yyb_read / yyb_peek: eBuffer_UTF8 returns the code point; buffer_read advances by the character's length. It never reads past the end of the buffer, so never across the end of a wrap buffer. At or past the end it returns what the other types return (eBuffer_OutOfBounds for a read, undefined for a peek).
  • yyb_write / yyb_poke: eBuffer_UTF8 writes the code point as 1 to 4 bytes. In a wrap buffer a write or poke carries on at the start (offset 0), as every other write does there and as the C++ runner does: the bytes at the tail count toward the used size, and a character that ends exactly at the end leaves buffer_tell at the buffer size until the next write wraps it, like the other types; on other buffers a poke that doesn't fit isn't written. Reads still never decode across the end.
  • yyb_fill: buffer_fill with buffer_utf8 does nothing, as on the C++ runner and GMRT, and a type with no size (an empty buffer_text) returns instead of looping forever.
  • buffer_text / buffer_string reads and peeks now share yyb_decodestring(start, end), built on yyDecodeUTF8. With a size (0 or more) the span is [position, min(position + size, buffer size)), the string stops at the first zero byte, and buffer_read advances by the whole span. Without one (or with a negative one) the behaviour is as before: up to the zero byte or the used size, and buffer_read moves past the zero byte. A size of 0 returns an empty string without moving at or before the end of the buffer (past it, it's out of bounds like any other read); any other sized read at or past the end returns what an unsized one does there, as the C++ runner does.
  • buffer_read(buffer, type, [size]) and buffer_peek(buffer, offset, type, [size]) pass the size on (ignored for the other types), and raise an error outside 2-3 and 3-4 arguments (buffer_read: expected 2 or 3 arguments, got N, as the C++ runner words it). buffer_sizeof(buffer_utf8) is 0, like buffer_text.

Behaviour change for malformed bytes in an ordinary text read: the old decoder assumed every byte from 0xF0 up started a 4-byte character and read continuation bytes without checking them (or the end of the data). A malformed sequence now gives U+FFFD per bad byte, as browsers' own decoders do. Valid text decodes exactly as before.

Testing

GM-TestFramework BasicBufferUtf8TestSuite (branch buffers/gmb-16485 at f83ef14, which adds the size-0-at-the-end test) on HTML5: xUnit built for HTML5 on macOS with Igor from runtime 9.9.1.1404 and the asset compiler from GameMaker#5591 (ac52c2d62e), served locally, run in headless Chrome:

  • HTML5 develop runner: 4 of 18 pass.
  • This branch (0bfa27c): 18 of 18 pass. The later poke and wrap-write changes weren't re-run on HTML5 (the suite doesn't cover poke wrapping); it's covered by the Node checks below.

BasicBufferTestSuite has suite_filter: platform_not_browser, so it doesn't run on HTML5. In its place I compared develop's and this branch's text reads in Node on 3000 random scenarios (1 to 4 strings of mixed 1 to 4 byte characters written with buffer_text / buffer_string, read back, plus peeks at random offsets). All reads are identical. The only differences are the 40 peeks that start on a continuation byte, where develop decoded garbage and this branch gives U+FFFD. A separate Node check of the new code (116 checks: every case in the suite, plus past-the-end reads, writes and pokes across the end of a wrap buffer, the used size and buffer_tell after wrapping writes, a size of 0 past the end, a poke that doesn't fit a fixed buffer, buffer_fill with buffer_utf8 and with an empty buffer_text, and the argument count errors) passes.

…d buffer_peek (GameMaker-Bugs#16485)

buffer_utf8 reads and peeks one UTF-8 character as its code point (U+FFFD and one byte for a malformed sequence) and writes and pokes a code point as UTF-8. A size for buffer_text / buffer_string reads exactly that many bytes, up to the end of the buffer. The text decode is shared through yyDecodeUTF8, which also stops it reading past the end of the data.

Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5
@rwkay
rwkay requested a review from iampremo November 23, 2026 21:37

@gm-auto-review gm-auto-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of b70b9c6. The summary is in the Automated review comment on this PR.

Reviewed b70b9c6

Comment thread scripts/yyBuffer.js Resolved
@gm-auto-review

gm-auto-review Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Automated review

This PR adds the buffer_utf8 type (eBuffer_UTF8 = 14) to the HTML5 runner. It covers read, peek, write and poke, and buffer_fill with this type does nothing. buffer_read/buffer_peek take a new optional size for buffer_text/buffer_string, and both now raise an error if called with the wrong number of arguments. Text reads and peeks now share one bounds-checked UTF-8 decoder, so a malformed or cut-off sequence can no longer read past the end of the DataView.

Checked against the code:

  • Wrap-buffer writes and pokes now add the bytes at the end of the buffer to the used size before going back to offset 0.
  • yyb_fill returns early for buffer_utf8 and for any type with no size, so the old infinite loop can't happen.
  • The internal callers of buffer_read/buffer_peek (ds_* reading, variable reading, sound) all pass 2 or 3 arguments and still work.
  • Peeks at the buffer size and sized reads at or past the end give the results the description says.

Malformed bytes in an ordinary text read now come out as U+FFFD for each bad byte. The description states this change openly. No new problems were found and the change looks ready to merge.

Nothing open.

Reviewed 895e59b

Advisory: this check doesn't block merging. It runs again on each push. Reply in a comment's thread if it is wrong, and resolve the thread once it is dealt with: the next review reads both.

@iampremo iampremo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at b70b9c6.

I went through the read, peek, write and poke paths for buffer_utf8 and the new yyb_decodestring. The decoder looks right to me. Bounds are checked against _end. A bad lead or continuation byte takes 1 byte, so a truncated sequence before a zero byte still stops at the terminator instead of swallowing it. Sized and unsized reads advance as the description says.

The bot's buffer_fill finding is correct and still open. With buffer_utf8, typesize is 0 from BufferSizeOf, so in yyb_fill neither byteswritten nor virtualoffset ever moves forward and the while (byteswritten < _size) loop never ends. buffer_fill always passes stride -1, so every call with the new type hangs the page. This needs fixing before merge. Set typesize from yyEncodeUTF8(_value).length and bail out when typesize <= 0. That guard would also cover the existing hang when you fill with an empty buffer_text string.

I also have one question about how a wrap buffer behaves once a character no longer fits at the end (inline).

Comment thread scripts/yyBuffer.js Resolved
…ap buffer, argument count checks, size 0 at the end

buffer_fill with buffer_utf8 does nothing (as on the C++ runner and GMRT), and a type with no size (an empty buffer_text) no longer loops forever. buffer_utf8 writes carry on at the start of a wrap buffer like the other writes. buffer_read and buffer_peek raise an error outside 2-3 and 3-4 arguments. A size of 0 reads an empty string without moving, even at the end of the buffer.

Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5
@rwkay
rwkay requested a review from iampremo November 23, 2026 22:02
As on the C++ runner, a poke into a wrap buffer carries on at offset 0; on other buffers a character that doesn't fit isn't written.

Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5

@gm-auto-review gm-auto-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of 0bfa27c. The summary is in the Automated review comment on this PR.

Reviewed 0bfa27c

Comment thread scripts/yyBuffer.js Resolved

@iampremo iampremo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at e0ad0db.

Re-reviewed at e0ad0db.

The earlier points are dealt with:

  • buffer_fill hang (my blocker, and the bot's): fixed. yyb_fill now returns for buffer_utf8 and whenever typesize <= 0, so an empty buffer_text fill no longer hangs either.
  • Wrap buffer question: answered, and the code matches the reply. yyb_write and yyb_poke now continue writing the character's bytes at offset 0. Tracing a 3-byte character written at offset 2 of a 4-byte wrap buffer gives [0xAC, 0, 0xE2, 0x82] with the position at 1, as you said.

The size-0 early return, the argument-count errors and the poke-wrap used-size handling all look right. No internal callers of buffer_read/buffer_peek pass the wrong number of arguments.

One new problem, inline: the wrap path in yyb_write resets the index to 0 before the used size is updated, so the used size misses the bytes written at the tail. yyb_poke already gets this right.

Comment thread scripts/yyBuffer.js Resolved
…nly at or before the end

A buffer_utf8 write that wraps now updates the used size to the end of the buffer before going back to 0, and only wraps when bytes are still to be written, so a character that ends exactly at the end leaves buffer_tell at the buffer size like the other types. A size of 0 reads an empty string only at or before the end of the buffer; past it the read is out of bounds like any other.

Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5
@rwkay
rwkay requested a review from iampremo November 23, 2026 22:19

@iampremo iampremo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 895e59b.

Re-reviewed at 895e59b. The used-size problem from my last review is fixed, and the reply's three points match the code:

  • In yyb_write, the wrap path now calls yyb_UpdateUsedSize(this.m_Size) before setting the index back to 0, as yyb_poke does. I traced both of my cases again:
    • Four buffer_utf8 writes of "A" into a new 4-byte wrap buffer leave the used size at 4, the same as buffer_u8.
    • Seeking to 2 and writing U+20AC puts bytes at 2, 3 and 0, and the used size stays at 4.
    • With "A" at offset 1, an unsized text read from 1 gives "A" followed by two U+FFFD for the cut-off bytes, as the reply says.
  • The mid-character wrap now only happens when another byte is still to be written (u + 1 < UTF8_Bytes.length). A character that ends exactly at the end leaves buffer_tell at the buffer size, and the next write wraps it in the existing loop at the top of yyb_write. That's how the fixed-size types behave.
  • A size-0 read now returns "" only on a wrap buffer or when the position is at or before the end. Past the end (reachable with buffer_seek), it falls through to the normal out-of-bounds path: "" for buffer_string, eBuffer_OutOfBounds for buffer_text, the same as an unsized read there. A size-0 peek past the end of a non-wrap buffer hits the existing _offset > m_Size - 0 check and returns undefined.

The bot's review is accurate on these points, and I have nothing new to raise. Looks good to merge.

@rwkay
rwkay merged commit a644919 into develop Nov 23, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants