Skip to content

Commit f037af2

Browse files
jtdowneylpil
authored andcommitted
Fix BitArray$BitArray$data DataView byte length
The DataView constructor was using bitArray.byteLength which is undefined on BitArray, causing sliced bit arrays to include extra bytes from the underlying buffer.
1 parent 5f4e6b4 commit f037af2

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
### Bug fixes
1414

15+
- Fixed a bug where `BitArray$BitArray$data` constructed a `DataView` with
16+
incorrect byte length instead of the slice's actual size, causing sliced bit
17+
arrays to include extra bytes from the underlying buffer on JavaScript.
18+
([John Downey](https://github.com/jtdowney))
19+
1520
## v1.15.1 - 2026-03-17
1621

1722
### Bug fixes

compiler-core/templates/prelude.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export const BitArray$BitArray$data = (bitArray) => {
301301
if (bitArray.bitSize % 8 !== 0)
302302
throw new Error("BitArray$BitArray$data called on un-aligned bit array");
303303
const array = bitArray.rawBuffer;
304-
return new DataView(array.buffer, array.byteOffset, bitArray.byteLength);
304+
return new DataView(array.buffer, array.byteOffset, bitArray.byteSize);
305305
};
306306

307307
/**

test/language/test/language/bit_array_test.gleam

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,9 @@ pub fn sliced_bit_array_data_view_offset_test() {
183183
let value = ffi.read_uint16_from_bit_array(sliced)
184184
assert value == 48_076
185185
}
186+
187+
pub fn sliced_bit_array_data_view_byte_length_test() {
188+
let data = <<0xAA, 0xBB, 0xCC, 0xDD>>
189+
let assert Ok(sliced) = bit_array.slice(data, 1, 2)
190+
assert sliced == <<0xBB, 0xCC>>
191+
}

0 commit comments

Comments
 (0)