Skip to content

fix(codemode): make Buffer map, filter, slice and subarray return a Buffer - #64933

Merged
rekram1-node merged 1 commit into
devfrom
buffer-map
Nov 19, 2026
Merged

rekram1-node merged 1 commit into
devfrom
buffer-map

Conversation

@rekram1-node

Copy link
Copy Markdown
Member

Summary

In Code Mode, the Uint8Array methods a Buffer inherits made a plain Uint8Array, so the result lost Buffer's UTF-8 toString. Real Bun uses species semantics and returns a Buffer.

Buffer.from("abc").map((x) => x + 1).toString()
// Bun 1.4.4:          "bcd"
// Code Mode before:   "98,99,100"
// Code Mode after:    "bcd"

Buffer.from("abc").filter((x) => x > 97).toString()        // before "98,99"  -> after "bc"  (Bun: "bc")
Uint8Array.prototype.slice.call(Buffer.from("abc"), 1)      // before Uint8Array -> after Buffer (Bun: Buffer)
Uint8Array.prototype.subarray.call(Buffer.from("abc"), 1)   // before Uint8Array -> after Buffer (Bun: Buffer)

Bytes gets a derive method, which BufferObj overrides to make a Buffer. Uint8Array.prototype.slice/subarray and the prelude's map/filter (through a new prelude-only $DerivedBytes helper) now build their result with it. Plain Uint8Array results don't change. I checked every case against Bun: reverse, sort and fill already returned the receiver, and Bun's toSorted, toReversed and with return Uint8Array (Code Mode doesn't implement them on Uint8Array). Updated interpreter-support.md and regenerated the prelude bytecode.

Testing

  • New test in packages/codemode/test/buffer.test.ts. It fails without the fix.
  • bun test in packages/codemode: 1785/1786 pass. The one failure, "reporting many unhandled rejections still times out", hit a timeout while the shared machine was loaded and passes when run alone.
  • bun typecheck (codemode) and bun run check (root) both pass.

Visual

<svg xmlns="http://www.w3.org/2000/svg" width="240" height="72" viewBox="0 0 240 72" fill="none" font-family="ui-monospace,monospace" font-size="12"><style>.a{animation:a 4s ease-in-out infinite}.b{opacity:0;animation:b 4s ease-in-out infinite}@keyframes a{0%,40%{opacity:.85}50%,90%{opacity:0}100%{opacity:.85}}@keyframes b{0%,40%{opacity:0}50%,90%{opacity:1}100%{opacity:0}}@media (prefers-reduced-motion:reduce){.a,.b{animation:none}.a{opacity:0}.b{opacity:1}}</style><rect x="1" y="1" width="238" height="70" rx="8" stroke="currentColor" opacity=".3"/><text x="14" y="28" fill="currentColor" opacity=".7">buf.map(x =&gt; x + 1)</text><path d="M14 40h212" stroke="currentColor" opacity=".2"/><g class="a"><text x="14" y="58" fill="currentColor">Uint8Array → "98,99,100"</text></g><g class="b"><text x="14" y="58" fill="currentColor">Buffer → "bcd"</text></g></svg>

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The change looks sound. I copied the new test into the base checkout and it fails there; with this PR it passes. The full packages/codemode suite (1786/1786) and tsc --noEmit also pass, and the prelude-bytecode test confirms the regenerated bytecode.json matches the source.

Putting derive on Bytes, with an override in BufferObj, gives one hook for both the native slice/subarray and the prelude's map/filter. Subclasses of Uint8Array still get a plain Uint8Array back, as before, so nothing regresses.

Public API: unchanged (this is internal Code Mode interpreter behaviour only).

No concerns.

@rekram1-node
rekram1-node merged commit 6fbc0af into dev Nov 19, 2026
11 checks passed
@rekram1-node
rekram1-node deleted the buffer-map branch November 19, 2026 21:26
jlongster added a commit to anomalyco/psychoscript that referenced this pull request Nov 19, 2026
…enCode

Ports anomalyco/opencode#64933, #64931, #64935 and #64938, which landed in OpenCode's language after the move.

User decisions:
- psychoscript holds only the language, nothing tool-specific
@amrkmn amrkmn mentioned this pull request Nov 23, 2026
1 task
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.

1 participant