Closed
Description
This should work
import * as std from "qjs:std";
import * as os from "qjs:os";
const buffer = new ArrayBuffer(0, {maxByteLength: 16384});
const view = new DataView(buffer);
const arr = [97, 98, 99];
const file = os.open("test.txt", os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0o764);
buffer.resize(arr.length);
console.log(buffer.byteLength);
for (let i = 0; i < arr.length; i++) {
console.log(`setUint8(${i}, ${arr[i]})`);
view.setUint8(i, arr[i]);
}
console.log(os.write(file, buffer, 0, buffer.byteLength));
console.log(os.close(file));
I get
3
setUint8(0, 97)
Possibly unhandled promise rejection: RangeError: out of bound
at setUint8 (native)
at <anonymous> (test.js:15:17)
Looks like a bug in DataView
reading resizable ArrayBuffer
.
This works as expected
import * as std from "qjs:std";
import * as os from "qjs:os";
const buffer = new ArrayBuffer(3);
const view = new DataView(buffer);
const arr = [97, 98, 99];
const file = os.open("test.txt", os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0o764);
//buffer.resize(arr.length);
console.log(buffer.byteLength);
for (let i = 0; i < arr.length; i++) {
console.log(`setUint8(${i}, ${arr[i]})`);
view.setUint8(i, arr[i]);
}
console.log(os.write(file, buffer, 0, buffer.byteLength));
console.log(os.close(file));
3
setUint8(0, 97)
setUint8(1, 98)
setUint8(2, 99)
3
0
ls -l test.txt
-rwxr--r-- 1 user user 3 Mar 20 05:09 test.txt
Metadata
Metadata
Assignees
Labels
No labels