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
5 changes: 3 additions & 2 deletions subprojects/sqlite-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ An AAR that lets apps built with Code on the Go define and call JavaScript funct

## Contents

- `upstream/`: the sqlite-js 1.4.3 release (Apache-2.0), which has all our changes (upstream pull requests #11 to #19, #22 to #24, and #33, which added `sqlitejs_cancel_all()`), with QuickJS amalgamated in `libs/quickjs.c` (MIT). Only the files the build uses, plus `LICENSE` and `LICENSE.quickjs`.
- `patches/`, when there is one: our changes not yet upstream, which `vendor.sh` and `test.sh` apply in order. There are none now.
- `upstream/`: the sqlite-js 1.4.4 release (Apache-2.0), which has all our changes (upstream pull requests #11 to #19, #22 to #24, #33, which added `sqlitejs_cancel_all()`, and #36), with QuickJS amalgamated in `libs/quickjs.c` (MIT). Only the files the build uses, plus `LICENSE` and `LICENSE.quickjs`.
- `patches/`, when there is one: our changes not yet upstream, which `vendor.sh` and `test.sh` apply in order. Drop each one once a release has it. There are none now.
- Since 1.4.4 (upstream #36, ADFA-7200) a call fails, instead of running with fewer arguments or a short `toArray()` row, when building one of them fails. Text that SQLite runs out of memory converting gives an "out of memory" error instead of a crash in `strlen`. `js_create_scalar()` and its siblings report an error when the function can't be stored in `js_functions`, for example on a read-only connection, instead of reporting success.
- Since 1.4.2 a `db.exec` result set does not outlive the call into JavaScript that created it: every one still open is ended when the call returns, because its statement keeps a read transaction open, keeps an interrupt in effect and stops the connection from closing. Reading one afterwards throws "The result set was closed when the call into JavaScript that created it returned", or "interrupted" when a cancel (or, on SQLite 3.41 and later, `sqlite3_interrupt()`) stopped the call. Ending them runs the finals of the window and aggregate functions they were computing, and a cancel during the call or while they are ended stops each final at QuickJS's next interrupt check. One read to its end still reports the end of its rows.
- `src/main/cpp/`: the CMake build of `libsqlitejs.so`, and `sqlitejs_android.c`, which is ours: JNI for `SqliteJs`, and the two entry points it names. The version script exports only the entry points (and upstream's `sqlite3_js_init`) and `JNI_OnLoad`.
- `src/test/c/glue_test.c` and `test.sh`: the tests the Android build cannot run. See Testing.
Expand Down
51 changes: 41 additions & 10 deletions subprojects/sqlite-js/upstream/src/sqlitejs.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,23 @@ static JSValue js_rowset_to_array(JSContext *ctx, JSValueConst this_val, int arg
return JS_EXCEPTION;
}

// a value that cannot be converted or stored (out of memory, or a setter a script put on
// Array.prototype) is an exception, not a shorter row or a shorter array: JS_SetPropertyUint32
// frees the value it is given, also when it fails
for (int i=0; i<ncols; ++i) {
sqlite3_value *value = sqlite3_column_value(rs->vm, i);
JSValue jvalue = sqlite_value_to_js(ctx, value);
JS_SetPropertyUint32(ctx, row, i, jvalue);
if (JS_IsException(jvalue) || (JS_SetPropertyUint32(ctx, row, i, jvalue) < 0)) {
JS_FreeValue(ctx, row);
JS_FreeValue(ctx, result);
return JS_EXCEPTION;
}
}

JS_SetPropertyUint32(ctx, result, nrows++, row);
if (JS_SetPropertyUint32(ctx, result, nrows++, row) < 0) {
JS_FreeValue(ctx, result);
return JS_EXCEPTION;
}
}

// as in js_rowset_next: an error is an exception, not a shorter array
Expand Down Expand Up @@ -1334,8 +1344,12 @@ static JSValue sqlite_value_to_js (JSContext *ctx, sqlite3_value *value) {
return JS_NewFloat64(ctx, sqlite3_value_double(value));
break;

case SQLITE_TEXT:
return JS_NewString(ctx, (const char *)sqlite3_value_text(value));
case SQLITE_TEXT: {
// NULL when SQLite runs out of memory converting the text to UTF-8
const char *text = (const char *)sqlite3_value_text(value);
if (!text) return JS_ThrowOutOfMemory(ctx);
return JS_NewString(ctx, text);
}
break;

case SQLITE_BLOB: {
Expand Down Expand Up @@ -1367,16 +1381,21 @@ static char *sqlite_strdup (const char *str) {
// MARK: - Execution -

static void js_execute_common (sqlite3_context *context, JSContext *js_context, int nvalues, sqlite3_value **values, JSValue func, JSValue this_obj, bool return_value) {
// create JS array for arguments
// create JS array for arguments: an argument that cannot be converted or stored (out of memory,
// or a setter a script put on Array.prototype) fails the call instead of leaving it out, and
// JS_SetPropertyUint32 frees the value it is given, also when it fails
JSValue args = (values) ? JS_NewArray(js_context) : JS_NULL;
for (int i=0; i<nvalues; ++i) {
for (int i=0; (i<nvalues) && !JS_IsException(args); ++i) {
JSValue js_val = sqlite_value_to_js(js_context, values[i]);
JS_SetPropertyUint32(js_context, args, i, js_val);
if (JS_IsException(js_val) || (JS_SetPropertyUint32(js_context, args, i, js_val) < 0)) {
JS_FreeValue(js_context, args);
args = JS_EXCEPTION;
}
}

// call the JavaScript function with args_array as the argument
JSValueConst args_val[] = {args};
JSValue result = JS_Call(js_context, func, this_obj, (values) ? 1 : 0, (values) ? args_val : NULL);
JSValue result = (JS_IsException(args)) ? JS_EXCEPTION : JS_Call(js_context, func, this_obj, (values) ? 1 : 0, (values) ? args_val : NULL);
JS_FreeValue(js_context, args);

if (return_value) js_value_to_sqlite(context, js_context, result);
Expand Down Expand Up @@ -1664,8 +1683,20 @@ static bool js_create_function (sqlite3_context *context, const char *type, cons
// so for collations the function context has to be released here
if ((rc != SQLITE_OK) && is_collation) functionjs_free(fctx);

if ((is_load == false) && (rc == SQLITE_OK)) {
js_add_to_table(context, type, name, init_code, step_code, final_code, value_code, inverse_code);
// a function that is registered but not stored (on a read-only connection, for example) would
// be gone when the database is reopened, so it is an error even though this connection has it
if ((is_load == false) && (rc == SQLITE_OK) && !js_add_to_table(context, type, name, init_code, step_code, final_code, value_code, inverse_code)) {
sqlite3 *db = sqlite3_context_db_handle(context);
int code = sqlite3_errcode(db);
char *msg = sqlite3_mprintf("The function was registered on this connection, but it could not be stored in js_functions: %s", sqlite3_errmsg(db));
if (!msg) {
sqlite3_result_error_nomem(context);
return false;
}
sqlite3_result_error(context, msg, -1);
sqlite3_free(msg);
sqlite3_result_error_code(context, (code != SQLITE_OK) ? code : SQLITE_ERROR);
return false;
}

// js_execute_cleanup is automatically called in case of error (collations excluded, see above)
Expand Down
2 changes: 1 addition & 1 deletion subprojects/sqlite-js/upstream/src/sqlitejs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "sqlite3.h"
#endif

#define SQLITE_JS_VERSION "1.4.3"
#define SQLITE_JS_VERSION "1.4.4"

int sqlite3_js_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi);
const char *sqlitejs_version (void);
Expand Down
4 changes: 2 additions & 2 deletions subprojects/sqlite-js/vendor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# Rebuilds upstream/ from the sqlite-js release VERSION and any patches/, or checks that it matches.
# Usage: vendor.sh [--check] [TARBALL] (without TARBALL, downloads it)
set -euo pipefail
VERSION=1.4.3
SHA256=a2ed16f353757767188134f74fc9f0b07feefa96abfa56bcc211758b4105ad0a
VERSION=1.4.4
SHA256=0a6a196a1e487009a086608a9e72c4043ba6d548506ee8170795280614492642
URL=https://github.com/sqliteai/sqlite-js/archive/refs/tags/$VERSION.tar.gz
HERE=$(cd "$(dirname "$0")" && pwd)
CHECK=false
Expand Down
Loading