Skip to content

fix(codemode): honor Uint8Array.from mappers, compare byte searches without coercion, skip flatMap holes, and truncate fractional radixes - #57285

Merged
rekram1-node merged 1 commit into
devfrom
codemode-issue-fixes
Oct 20, 2026
Merged

rekram1-node merged 1 commit into
devfrom
codemode-issue-fixes

Conversation

@rekram1-node

Copy link
Copy Markdown
Contributor

Problem

Four reproduced Code Mode issues where a built-in silently returned a different value from JavaScript. The worst case: the usual browser base64 decode, Uint8Array.from(atob(b64), (c) => c.charCodeAt(0)), returned all zeros, because Uint8Array.from ignored its mapper.

Closes #57244, #56924, #56925, #51807.

Change

 Uint8Array.from(atob("aGk="), (c) => c.charCodeAt(0))
-[0, 0]                   mapper and thisArg ignored; a non-callable mapper accepted
+[104, 105]               mapper sees the original values after the source is consumed, as TypedArray.from does

 new Uint8Array([0, 1, 2]).includes("1")      // also indexOf(true), lastIndexOf(null)
-true                     search value coerced to a number
+false                    non-numbers never match a byte; start indexes still coerce

 [1].flatMap(() => [, 2])
-[undefined, 2]           spread turned holes into elements
+[2]                      holes skipped; explicit undefined kept

 (35).toString(36.9)
-RangeError               range checked before the radix was truncated
+"z"                      our check removed; the host truncates, then throws RangeError for 0, 37, NaN, ...
src/stdlib/bytes.ts   collectBytes   takes an optional mapper/thisArg; collects raw values, maps, then converts
                      from           passes args[1], args[2] through
                      indexOf, lastIndexOf, includes   search value used as-is when it is a number, else NaN
src/stdlib/array.ts   flatMap        forEach over the mapped items, which skips holes
src/stdlib/number.ts  toString       radix range check removed

Credit: the byte-search, flatMap, and radix fixes are #56926 (@sdivyanshu90), #56927 (@sdivyanshu90), and #51808 (@icearia0219), unchanged except a comment. The Uint8Array.from fix follows #57257 (@r266-tech) and its tests, but reuses collectBytes instead of a second iteration loop (8 source lines instead of 30). All three are co-authors on the commit; those PRs can close once this merges.

Out of scope

Verification

check result
bun test (packages/codemode) 14,716 pass / 0 fail; every new test fails on dev
test262 (fresh sync) 13,305 pass, same as dev; 0 new failures
bun typecheck clean
bun run check 34/34

…ithout coercion, skip flatMap holes, and truncate fractional radixes

Closes #57244, #56924, #56925, #51807.

Co-authored-by: r266-tech <r266-tech@users.noreply.github.com>
Co-authored-by: sdivyanshu90 <divyanshu74.80@gmail.com>
Co-authored-by: icearia0219 <133481415+icearia0219@users.noreply.github.com>

@opencode-agent opencode-agent 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.

Looks good. I ran the #57244 reproduction (extended with the byte-search, flatMap and radix cases) through CodeMode.execute on the base and on this branch. All four problems show up on the base and are gone here:

  • the mapper and thisArg are applied, and a non-callable mapper throws a TypeError
  • the atob decode gives [104, 105]
  • includes('1'), indexOf(true) and lastIndexOf(null) no longer match
  • [1].flatMap(() => [, 2]) gives [2]
  • (35).toString(36.9) gives "z"

Invalid radixes still throw a RangeError that code can catch, and numeric start indexes are still converted to numbers. The mapper is checked before the source is read and runs only after it is fully read, which matches TypedArray.from. In packages/codemode, the codemode tests (1411 pass) and the type check pass.

Public API: unchanged. Only Code Mode's internal built-ins change.

No concerns.

@rekram1-node
rekram1-node merged commit db45c95 into dev Oct 20, 2026
14 checks passed
@rekram1-node
rekram1-node deleted the codemode-issue-fixes branch October 20, 2026 05:16
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.

codemode: Uint8Array.from ignores its mapper and thisArg

1 participant