Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Packages that are not in `termux-packages` live in `packages/<name>/`, laid out
| Package | What it is |
|---------|------------|
| `sqlite-brotli` | SQLite extension adding `encodeBrotli()` and `decodeBrotli()`. See [its README](packages/sqlite-brotli/README.md). |
| `sqlite-js` | SQLite extension for SQL functions written in JavaScript: [sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.2, which includes our changes. See [its README](packages/sqlite-js/README.md). |
| `sqlite-js` | SQLite extension for SQL functions written in JavaScript: [sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.4, which includes our changes. See [its README](packages/sqlite-js/README.md). |

The key specified with the `-s` option is the **public key** (**NOT** the private key!), which can be generated using :

Expand Down
19 changes: 17 additions & 2 deletions packages/sqlite-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

A SQLite loadable extension for writing SQL functions in JavaScript: scalar
functions, aggregates, window functions and collations (ADFA-7081). It is
[sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.2, which
includes twelve changes of ours, listed [below](#changes-in-this-build). The
[sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.4, which
includes fourteen changes of ours, listed [below](#changes-in-this-build). The
JavaScript engine, [quickjs-ng](https://github.com/quickjs-ng/quickjs) 0.12.1,
is built in.

Expand Down Expand Up @@ -177,6 +177,11 @@ it as TEXT, `CAST(x AS TEXT)`, and convert it with `BigInt(args[0])`.
`js_set_memory_limit` changes the limit, and collects garbage. Memory that
globals still hold counts against the limit until they are cleared, so a
limit under it leaves no room for any JavaScript until it is raised again.
The limit counts the memory the allocator actually hands out. Where that is
jemalloc, which rounds large blocks up to a size class (measured on a 32-bit
Android 9 phone), a big `ArrayBuffer` can count up to a quarter more than its
length: 32 MB and 4 KB counts as 40 MB, and 60 MB as 64 MB, the whole
default limit.
- **Endless code.** Ctrl-C stops running JavaScript: the statement fails with
an "interrupted" error, even if the code catches exceptions. In an
interactive session `sqlite3` keeps running; with SQL from a file or from
Expand Down Expand Up @@ -234,6 +239,16 @@ function could crash `sqlite3`, and a result set kept in a global kept a
statement open: Ctrl-C during plain SQL then left every later statement
failing with "interrupted".

Upstream released one more in 1.4.3: #33 lets an app that embeds sqlite-js
cancel all running JavaScript with `sqlitejs_cancel_all()`. `sqlite3` does not
use it, and this build does not export it.

And one more in 1.4.4 (ADFA-7200):

| Upstream PR | Change |
|-------------|--------|
| #36 | Errors that 1.4.3 drops are reported. An error while building a call's arguments, or a row of `toArray()`, fails the call; in 1.4.3 the function runs with fewer arguments, or gets a short row. Text that SQLite runs out of memory converting gives an "out of memory" error; in 1.4.3, `sqlite3` crashes. `js_create_scalar` and its siblings fail when they cannot store the function in `js_functions`, for example on a read-only database; in 1.4.3 they report success, and the function is gone in the next session. |

This build carries no patches.

The build also exports only the extension's entry points, and adds the one
Expand Down
12 changes: 6 additions & 6 deletions packages/sqlite-js/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ TERMUX_PKG_DESCRIPTION="SQLite extension for functions written in JavaScript"
TERMUX_PKG_LICENSE="Apache-2.0, MIT"
TERMUX_PKG_LICENSE_FILE="LICENSE, LICENSE.quickjs"
TERMUX_PKG_MAINTAINER="@appdevforall"
TERMUX_PKG_VERSION=1.4.2
TERMUX_PKG_REVISION=3
TERMUX_PKG_VERSION=1.4.4
TERMUX_PKG_SRCURL=https://github.com/sqliteai/sqlite-js/archive/refs/tags/${TERMUX_PKG_VERSION}.tar.gz
TERMUX_PKG_SHA256=1aa4c566491f1b56f3fd676b80880fdccbf0806b8f3cd015f60cd0f1c171cf24
# Upstream released our changes (sqliteai/sqlite-js #11 to #19) in 1.4.0, and
# #22 to #24 in 1.4.2, so this build carries no patches. Patches of ours would
# go here as *.patch files, each offered upstream first.
TERMUX_PKG_SHA256=0a6a196a1e487009a086608a9e72c4043ba6d548506ee8170795280614492642
# Upstream released our changes (sqliteai/sqlite-js #11 to #19) in 1.4.0, #22
# to #24 in 1.4.2, #33 in 1.4.3 and #36 in 1.4.4, so this build carries no
# patches. Patches of ours would go here as *.patch files, each offered
# upstream first.
TERMUX_PKG_BUILD_DEPENDS="libsqlite"
TERMUX_PKG_RECOMMENDS="sqlite"
TERMUX_PKG_BUILD_IN_SRC=true
Expand Down
27 changes: 25 additions & 2 deletions packages/sqlite-js/tests/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ expect "loading the extension twice in one session is harmless" "2" \

# --- AC2: the engine is quickjs-ng --------------------------------------------

expect "AC2: js_version() is the sqlite-js version, 1.4.2" "1.4.2" "SELECT js_version();"
expect "AC2: js_version() is the sqlite-js version, 1.4.4" "1.4.4" "SELECT js_version();"
got=$(sql "SELECT js_version(1);" 2>&1)
# Bellard's QuickJS has no version function, so sqlite-js does not build with it.
if [[ $got =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
Expand Down Expand Up @@ -538,9 +538,13 @@ expect_nobail "AC9: code that keeps allocating fails at the limit, and the sessi
"1,2,3" "out of memory" \
"SELECT js_eval('(function () { const a = []; for (let i = 0; i < 160; i++) a.push(new Array(100000).fill(1)); })()');" \
"SELECT js_eval('[1, 2, 3].join()');"
# 48 MB, not 60: the limit counts a block's usable size, and jemalloc (measured
# on a 32-bit Android 9 phone) rounds a 60 MB block up to 64 MB, the whole limit,
# so nothing else could even be parsed until it was collected. 48 MB is an exact
# jemalloc size class.
expect_nobail "AC9: memory held by a global counts until the global is cleared" \
$'0\n16777216' "out of memory" \
"SELECT js_eval('globalThis.keep = new ArrayBuffer(60 * 1024 * 1024); 0');" \
"SELECT js_eval('globalThis.keep = new ArrayBuffer(48 * 1024 * 1024); 0');" \
"SELECT js_eval('new ArrayBuffer(16 * 1024 * 1024).byteLength');" \
"SELECT js_eval('keep = null; new ArrayBuffer(16 * 1024 * 1024).byteLength');"

Expand Down Expand Up @@ -772,6 +776,25 @@ expect_error "AC14: a result set kept in a global throws when a later call reads
"SELECT js_eval('globalThis.held = db.exec(\"SELECT 1 UNION ALL SELECT 2\"); held.next(); 0');" \
"SELECT js_eval('held.next()');"

# --- ADFA-7200: errors that 1.4.3 drops (upstream #36, in 1.4.4) --------------

# A setter on Array.prototype makes putting an element into a new array throw.
# The call must fail with that error, not run with fewer arguments or rows.
SETTER="SELECT js_eval('Object.defineProperty(Array.prototype, \"0\", { set(v) { throw new Error(\"index setter\"); }, configurable: true }); 1');"
expect_error "ADFA-7200: an error building a call's arguments fails the call" "index setter" \
"SELECT js_create_scalar('first_arg', '(function (args) { return (args[0] === undefined) ? \"missing\" : args[0]; })');" \
"$SETTER" "SELECT first_arg(5);"
expect_error "ADFA-7200: an error building a toArray() row fails the call" "index setter" \
"$SETTER" "SELECT js_eval('db.exec(\"SELECT 1, 2\").toArray().length');"

# On a read-only connection a new function is registered but cannot be stored
# in js_functions, so it would be gone in the next session: that is an error.
sql "SELECT js_init_table();" >/dev/null 2>&1
expect_error "ADFA-7200: a function that cannot be stored in js_functions is an error" \
"could not be stored in js_functions" \
".open --readonly $(dot_arg "$DB")" "$LOAD_EXT" \
"SELECT js_create_scalar('not_stored', '(function (args) { return 42; })');"

echo "1..$n"
if [[ $failed -ne 0 ]]; then
echo "# FAILED $failed of $n"
Expand Down
Loading