fix(typedarray): Uint8Array.of/from build the buffer-backed representation#4280
Merged
Conversation
…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.
cf0cc43 to
6728876
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Uint8Array.of(...)/Uint8Array.from(...)produced garbage bytes —Uint8Array.of(1,2,3)[0]read back as a denormal like1.27e-321(issue #871part-2 regression).
Perry represents
Uint8Arrayas a buffer (BufferHeader, viajs_uint8array_new/js_uint8array_from_array), andnew Uint8Array([...])already builds that form. But the
Uint8ArrayFromcodegen lowered.of/.fromto
js_typed_array_new_from_array(kind=1, …), which builds the genericTypedArrayHeaderrepresentation. Element reads (u[i]) then mis-read thatheader as a plain array → uninitialized memory.
Fix
perry-codegen/src/expr/instance_misc1.rs:Uint8ArrayFromnow callsjs_uint8array_from_array(arr)(buffer-backed, matchingnew Uint8Array)instead of
js_typed_array_new_from_array(1, arr).perry-runtime/src/typedarray.rs:js_typed_array_new_from_array(stillused by non-
Uint8Array.of/.fromlikeInt8Array.of) now reads sourceelements through the canonical
js_array_get_f64accessor instead of a rawinline-
f64read, so it correctly materializes cloned source arrays.Verification
Uint8Array.of(77,88,99)→77 88 99;Uint8Array.from([44,55,66])→44 55 66test_issue_871_propertyget_callee_20_args→ matches NodeInt8Array.of(5,6,7)→5 6 7(matches Node)