Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: typed arrays ref shouldn't use offset. #1376

Merged
merged 1 commit into from Nov 30, 2022

Conversation

nihohit
Copy link
Contributor

@nihohit nihohit commented Nov 29, 2022

Notice from the n-api docs that the data returned from napi_get_typedarray_info is already adjusted by the byte offset. https://nodejs.org/api/n-api.html#napi_get_typedarray_info

This means that when as_ref/as_mut apply the byte offset, the offset is in practice applied twice.
This wasn't caught in tests because no test tried to modify a typed array with a byte offset, and the test didn't us the typed array structs, only JsTypedArray. If you want, I can modify the rest of the functions in examples/napi-compt-mode/src/arraybuffers.rs and the matching tests, to test all typed arrays.

IMO the byte_offset field can be removed entirely from the struct, but I wanted to submit a minimal PR.

Notice from the n-api docs that the data returned from
`napi_get_typedarray_info` is already adjusted by the byte offset.
https://nodejs.org/api/n-api.html#napi_get_typedarray_info

This means that when `as_ref`/`as_mut` apply the byte offset, the
offset is in practice applied twice.
This wasn't caught in tests because no test tried to modify a typed
array with a byte offset, and the test didn't us the typed array
structs, only `JsTypedArray`. If you want, I can modify the rest of the
functions in examples/napi-compt-mode/src/arraybuffers.rs
and the matching tests, to test all typed arrays.

IMO the `byte_offset` field can be removed entirely from the struct,
but I wanted to submit a minimal PR.
@Brooooooklyn Brooooooklyn merged commit 1cf3263 into napi-rs:main Nov 30, 2022
@nihohit
Copy link
Contributor Author

nihohit commented Dec 7, 2022

@Brooooooklyn, can you please release a version with this fix?

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.

None yet

2 participants