Repository navigation
Return ArrayBuffers and typed arrays as BLOBs - #14
Conversation
A BLOB argument reaches JavaScript as an ArrayBuffer, but a function
that returned an ArrayBuffer, or a typed array such as Uint8Array, gave
NULL: js_value_to_sqlite() turned every object into NULL. So no function
could return binary data, and `identity(x'00ff')` with
`(function(args){ return args[0]; })` returned NULL. db.exec() bound an
ArrayBuffer but not a typed array, and bound an empty ArrayBuffer as
NULL.
Add js_get_bytes(), which gets the bytes of an ArrayBuffer or of the part
of a buffer that a typed array views, and use it for function results and
for db.exec() parameters. An empty buffer is an empty BLOB. Returning a
detached buffer is an error; binding one still binds NULL, as before.
A typed array's offset and length come from its byteOffset and
byteLength getters, read before its bytes: one that tracks the length
of a resizable buffer keeps the length QuickJS recorded when it was
made, which goes stale when the buffer is resized, and reading it
after a shrink would read past the buffer. A typed array of fixed
length that its shrunken buffer no longer holds is an error.
Document how values convert in both directions in the README.
Add a Blobs test: BLOBs round-trip through a function, typed arrays,
subarrays and ArrayBuffers made in JavaScript become BLOBs, a function
can build a BLOB from a BLOB, a detached buffer is an error, and
db.exec() binds typed arrays and empty buffers as BLOBs. It also checks
typed arrays over resizable buffers that shrink and grow.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
From reviews of this change: db.exec() bound NULL for a buffer it could not read: a detached buffer, or a typed array out of its buffer's bounds. A failed write then looked like a success, while returning the same value from a function is an error. db.exec() now throws that error. A typed array's offset and length came from its byteOffset and byteLength properties, which a script can shadow or redefine, so a BLOB could hold other bytes of the buffer than the array views. They now come from the getters of %TypedArray%.prototype, taken when the runtime is made. The Blobs test checks both; each check fails without its change. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
|
Merged into Verified locally on macOS/arm64: universal build with no new warnings, full suite green, clean under ASan + UBSan — which I particularly wanted for this one, given it does pointer arithmetic into buffer memory. The new Blobs test is load-bearing: with the source change reverted, the very first assertion ( I verified the API assumptions against Using Taking the getters from the prototype once at runtime creation, instead of reading the properties, is a nice touch: it closes the shadowing hole and guarantees no JavaScript runs while you hold a raw pointer into the buffer. One gap, not a regression and not worth holding the merge for: a Nothing needed changing. Thanks — this closes a real hole, since no function could return binary data at all before. |
|
Thank you for the close check of the pointer handling. Agreed on This reply originated with AI: Claude, Anthropic's AI model, wrote it for App Dev For All. Contact: [email protected] |
A BLOB argument reaches JavaScript as an
ArrayBuffer, but a function that returns anArrayBuffer, or a typed array such asUint8Array, gives NULL:js_value_to_sqlite()turns every object into NULL. So no function can return binary data; with(function(args){ return args[0]; })asidentity,identity(x'00ff')is NULL.db.exec()binds anArrayBufferbut not a typed array, and binds an emptyArrayBufferas NULL.This adds
js_get_bytes(), which gets the bytes of anArrayBuffer, or of the part of a buffer that a typed array views, and uses it for function results anddb.exec()parameters. An empty buffer is an empty BLOB. Returning a detached buffer is an error; binding one still binds NULL, as before. A typed array's offset and length come from itsbyteOffsetandbyteLengthgetters, read before its bytes: one that tracks the length of a resizable buffer keeps the length QuickJS recorded when it was made, which goes stale when the buffer is resized, and after a shrink that length would read past the buffer. A typed array of fixed length that its shrunken buffer no longer holds is an error. A new README section, Values, says how values convert in both directions.Test: a new Blobs test checks that BLOBs round-trip through a function, that typed arrays, subarrays and
ArrayBuffers made in JavaScript become BLOBs, that a function can build a BLOB from a BLOB, that a detached buffer is an error, thatdb.exec()binds typed arrays and empty buffers as BLOBs, and that typed arrays over resizable buffers that shrink or grow pass their current bytes.make testpasses on x86_64 Linux, also under ASan and UBSan.These are six separate pull requests, #11 to #16, so each can be taken or declined on its own; only #15 depends on another (#14). They add tests at the same places in
test/main.c, and #11 and #12 both add code just aboveglobaljs_init(), so those merged later will need a small rebase, which we will do.This pull request originated with AI. Claude, Anthropic's AI model, wrote the code, the tests and this description while packaging sqlite-js for Code on the Go, App Dev For All's Android IDE, which ships sqlite-js with this change. Contact: [email protected]
🤖 Generated with Claude Code
https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
Update, 2026-11-12. Reviews of this change found two more problems, both fixed in 23102b0:
db.exec()bound NULL for a buffer it could not read (detached, or a typed array out of its buffer's bounds). It now throws, as returning that value from a function already did.%TypedArray%.prototype's getters, taken when the runtime is made.test/main.cpasses, also under ASan and UBSan, and each new check fails without its change.