Skip to content

Return whole numbers as INTEGERs - #15

Merged
marcobambini merged 3 commits into
sqliteai:mainfrom
davidschachterADFA:return-whole-numbers-as-integers
Nov 12, 2026
Merged

marcobambini merged 3 commits into
sqliteai:mainfrom
davidschachterADFA:return-whole-numbers-as-integers

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Builds on #14, which changes the same code and adds the README section this updates: its first commit is #14, so please review the second.

QuickJS keeps small whole numbers as 32-bit integers and every other number as a double, which goes back to SQL as a REAL. So an INTEGER outside the 32-bit range changes type on its way through a function: identity(2147483648) returns 2147483648.0. Arithmetic results are inconsistent too: js_eval('2 + 3') returns the INTEGER 5, but js_eval('2.5 * 2') the REAL 5.0, and a JavaScript sum of large integers is a REAL.

This returns a number that is a whole number from -(2^53 - 1) to 2^53 - 1 (Number.isSafeInteger), which a double holds exactly, as an INTEGER, and any other number as a REAL, for function results and db.exec() parameters alike.

One behavior change to weigh: JavaScript cannot tell 3.0 from 3, so a REAL that holds a whole number now comes back as an INTEGER (identity(3.0) is 3). The README says so, and how to pass an INTEGER beyond 2^53 - 1 without losing precision (as TEXT, then BigInt()).

Test: a new Numbers test checks that integers in and beyond the 32-bit range round-trip as INTEGERs, that other REALs stay REAL, that integers beyond 2^53 - 1 are REALs, and covers arithmetic results, an aggregate result past the 32-bit range, and db.exec() parameters. 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. This PR carries #14's commit, so it also carries #14's new fix (9fc41bb).

davidschachterADFA and others added 2 commits November 10, 2026 22:30
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
QuickJS keeps the small whole numbers it can as 32-bit integers, and any
other number as a double, which went back to SQL as a REAL. So an INTEGER
outside the 32-bit range changed type on its way through a function:
`identity(2147483648)` returned 2147483648.0. The result of arithmetic
did too: js_eval('2 + 3') returned the INTEGER 5 but js_eval('2.5 * 2')
the REAL 5.0, and a JavaScript sum of large integers was a REAL.

Return a number that is a whole number from -(2^53 - 1) to 2^53 - 1
(Number.isSafeInteger), which a double holds exactly, as an INTEGER, and
any other number as a REAL, for function results and db.exec()
parameters alike. JavaScript cannot tell 3.0 from 3, so a REAL that holds
a whole number now comes back as an INTEGER; the README says so, and how
to pass an INTEGER beyond 2^53 - 1 without losing precision.

Add a Numbers test: integers in and beyond the 32-bit range round-trip
as INTEGERs, other REALs stay REAL, integers beyond 2^53 - 1 are REALs,
arithmetic results, an aggregate result past the 32-bit range, and
db.exec() parameters.

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. The new Numbers test is load-bearing — with both call sites of js_number_to_int64 reverted, the first assertion fails.

I probed the boundaries myself rather than trusting the test, and every case behaves as documented:

expression result
js_eval('0'), js_eval('-0') integer 0
js_eval('9007199254740991') integer (2^53 − 1, included)
js_eval('9007199254740992') real (2^53, excluded)
js_eval('-9007199254740991') integer
js_eval('2147483648') integer — the bug this fixes
js_eval('1.5') real
js_eval('1/0') real Inf
js_eval('0/0') null (NaN, via sqlite3_result_double)
BigInt("9223372036854775807") integer, exact
db.exec binding 2147483648 integer

The guard form !(num >= -max && num <= max) is the right way round — it rejects NaN, which fails both comparisons.

For the record, since this is the one change in the series that alters existing behaviour: a REAL holding a whole number now comes back as an INTEGER, so identity(3.0) is 3. JavaScript cannot tell 3.0 from 3, so that is the unavoidable price of no longer silently turning an INTEGER beyond the 32-bit range into a REAL — which was the more damaging of the two. It is documented, including how to carry an integer past 2^53 − 1 as TEXT.

Thanks.

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