Skip to content

Return ArrayBuffers and typed arrays as BLOBs - #14

Merged
marcobambini merged 2 commits into
sqliteai:mainfrom
davidschachterADFA:return-blobs
Nov 12, 2026
Merged

marcobambini merged 2 commits into
sqliteai:mainfrom
davidschachterADFA:return-blobs

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

A BLOB argument reaches JavaScript as an ArrayBuffer, but a function that returns an ArrayBuffer, or a typed array such as Uint8Array, gives NULL: js_value_to_sqlite() turns every object into NULL. So no function can return binary data; with (function(args){ return args[0]; }) as identity, identity(x'00ff') is NULL. db.exec() binds an ArrayBuffer but not a typed array, and binds an empty ArrayBuffer as NULL.

This adds js_get_bytes(), which gets the bytes of an ArrayBuffer, or of the part of a buffer that a typed array views, and uses it for function results and 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 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, that db.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 test passes 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 above globaljs_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.
  • A typed array's offset and length came from properties a script can shadow. They now come from %TypedArray%.prototype's getters, taken when the runtime is made.

test/main.c passes, also under ASan and UBSan, and each new check fails without its change.

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
@marcobambini

Copy link
Copy Markdown
Member

Merged into main as part of a review of #11–#19 together.

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 (quote(identity(x'00ff10'))) fails. The test is genuinely thorough — resizable buffers that shrink and grow, shadowed byteOffset/byteLength, and tampering with %TypedArray%.prototype itself all covered.

I verified the API assumptions against libs/quickjs.h: JS_GetTypedArrayType returns -1 for a non-typed-array, JS_GetTypedArrayBuffer accepts NULL for all three out-parameters, and JS_HasException exists. The bounds check is ordered correctly too — offset > size is tested before length > size - offset, so the subtraction cannot underflow.

Using sqlite3_result_zeroblob(context, 0) / sqlite3_bind_zeroblob(vm, index, 0) for the empty case rather than passing a possibly-NULL pointer to *_blob is the right workaround, since SQLite reads a NULL pointer as a NULL value.

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 DataView is neither an ArrayBuffer nor a typed array, so JS_GetTypedArrayType returns -1 and it still comes back as NULL. Mentioning it in case you want it covered later.

Nothing needed changing. Thanks — this closes a real hole, since no function could return binary data at all before.

@davidschachterADFA

Copy link
Copy Markdown
Contributor Author

Thank you for the close check of the pointer handling. Agreed on DataView: it still comes back as NULL. We don't need it today, so we've noted it rather than widened this change.

This reply originated with AI: Claude, Anthropic's AI model, wrote it for App Dev For All. Contact: [email protected]

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.

2 participants