Skip to content

fix(typedarray): Uint8Array.of/from build the buffer-backed representation#4280

Merged
proggeramlug merged 1 commit into
mainfrom
fix-871-uint8array-of-from
Jun 3, 2026
Merged

fix(typedarray): Uint8Array.of/from build the buffer-backed representation#4280
proggeramlug merged 1 commit into
mainfrom
fix-871-uint8array-of-from

Conversation

@proggeramlug
Copy link
Copy Markdown
Contributor

Problem

Uint8Array.of(...) / Uint8Array.from(...) produced garbage bytes
Uint8Array.of(1,2,3)[0] read back as a denormal like 1.27e-321 (issue #871
part-2 regression).

Perry represents Uint8Array as a buffer (BufferHeader, via
js_uint8array_new / js_uint8array_from_array), and new Uint8Array([...])
already builds that form. But the Uint8ArrayFrom codegen lowered .of/.from
to js_typed_array_new_from_array(kind=1, …), which builds the generic
TypedArrayHeader representation. Element reads (u[i]) then mis-read that
header as a plain array → uninitialized memory.

Fix

  • perry-codegen/src/expr/instance_misc1.rs: Uint8ArrayFrom now calls
    js_uint8array_from_array(arr) (buffer-backed, matching new Uint8Array)
    instead of js_typed_array_new_from_array(1, arr).
  • perry-runtime/src/typedarray.rs: js_typed_array_new_from_array (still
    used by non-Uint8Array .of/.from like Int8Array.of) now reads source
    elements through the canonical js_array_get_f64 accessor instead of a raw
    inline-f64 read, so it correctly materializes cloned source arrays.

Verification

  • Uint8Array.of(77,88,99)77 88 99; Uint8Array.from([44,55,66])44 55 66
  • test_issue_871_propertyget_callee_20_argsmatches Node
  • Int8Array.of(5,6,7)5 6 7 (matches Node)

…ation

Uint8Array.of/from produced garbage bytes (#871 part-2 regression). Perry backs
Uint8Array with a BufferHeader (js_uint8array_*), and new Uint8Array([...]) uses
it, but Uint8ArrayFrom codegen built a generic TypedArrayHeader (kind=1) via
js_typed_array_new_from_array — the wrong representation, so u[i] read garbage.

- instance_misc1.rs: Uint8ArrayFrom -> js_uint8array_from_array(arr)
- typedarray.rs: js_typed_array_new_from_array reads source via js_array_get_f64
  (canonical accessor) for the remaining non-Uint8 .of/.from kinds (Int8Array etc).

Verified Uint8Array.of/from + test_issue_871 + Int8Array.of match node. Bumps 0.5.1115.
@proggeramlug proggeramlug force-pushed the fix-871-uint8array-of-from branch from cf0cc43 to 6728876 Compare June 3, 2026 17:26
@proggeramlug proggeramlug merged commit ebe604a into main Jun 3, 2026
11 checks passed
@proggeramlug proggeramlug deleted the fix-871-uint8array-of-from branch June 3, 2026 17:54
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