From 8dd13e5358e0f017d1020080b0f01289a444da13 Mon Sep 17 00:00:00 2001 From: lovasoa Date: Wed, 19 Mar 2025 23:57:07 +0000 Subject: [PATCH 1/4] fix jsdoc for updatehook --- src/api.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/api.js b/src/api.js index e1d75dfb..f349ab15 100644 --- a/src/api.js +++ b/src/api.js @@ -1457,17 +1457,12 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { * // This won't trigger any callback * db.run("INSERT INTO users VALUES (2, 'Bob', 1)"); * - * @param {function|null} callback - - * Callback to be executed whenever a row changes. - * Set to `null` to unregister the callback. - * @param {string} callback.operation - - * 'insert', 'update', or 'delete' - * @param {string} callback.database - - * database where the change occurred - * @param {string} callback.table - - * table where the change occurred - * @param {number} callback.rowId - - * rowid of the changed row + * @param {Database~UpdateHookCallback|null} callback + * - Callback to be executed when a row changes. Takes the type of change, + * the name of the database, the name of the table, and the row id of the + * changed row. + * - Set to `null` to unregister. + * @returns {void} */ Database.prototype["updateHook"] = function updateHook(callback) { if (this.updateHookFunctionPtr) { @@ -1528,6 +1523,18 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { ); }; + /** + * @callback Database~UpdateHookCallback + * @param {'insert'|'update'|'delete'} operation + * - The type of change that occurred + * @param {string} database + * - The name of the database where the change occurred + * @param {string} table + * - The name of the database's table where the change occurred + * @param {number} rowId + * - The [rowid](https://www.sqlite.org/rowidtable.html) of the changed row + */ + // export Database to Module Module.Database = Database; }; From b645ca0713830d258f79a13d4ab44f7b1e281835 Mon Sep 17 00:00:00 2001 From: lovasoa Date: Wed, 19 Mar 2025 23:58:46 +0000 Subject: [PATCH 2/4] make updateHook return the db object --- src/api.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/api.js b/src/api.js index f349ab15..c6398c28 100644 --- a/src/api.js +++ b/src/api.js @@ -1462,7 +1462,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { * the name of the database, the name of the table, and the row id of the * changed row. * - Set to `null` to unregister. - * @returns {void} + * @returns {Database} The database object. Useful for method chaining */ Database.prototype["updateHook"] = function updateHook(callback) { if (this.updateHookFunctionPtr) { @@ -1474,7 +1474,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { if (!callback) { // no new callback to register - return; + return this; } // void(*)(void *,int ,char const *,char const *,sqlite3_int64) @@ -1521,6 +1521,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { this.updateHookFunctionPtr, 0 // passed as the first arg to wrappedCallback ); + return this; }; /** From ee67aeb93775ffa267282fbb46fb98410fbac167 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 20 Mar 2025 02:26:36 -0700 Subject: [PATCH 3/4] Remove usage of emscripten's allocate function. (#606) * Remove usage of emscripten's allocate function. This function is deprecated upstream. * fix variable name * Avoid lengthBytesUTF8 * lint * remove usage of deprecated allocateUTF8OnStack --------- Co-authored-by: lovasoa --- src/api.js | 37 ++++++++++++++----------------- src/exported_runtime_methods.json | 6 ++--- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/api.js b/src/api.js index c6398c28..c7f102b7 100644 --- a/src/api.js +++ b/src/api.js @@ -5,19 +5,15 @@ _malloc _free getValue - intArrayFromString setValue stackAlloc stackRestore stackSave UTF8ToString - stringToUTF8 - lengthBytesUTF8 - allocate - ALLOC_NORMAL - allocateUTF8OnStack + stringToNewUTF8 removeFunction addFunction + writeArrayToMemory */ "use strict"; @@ -545,14 +541,13 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { pos = this.pos; this.pos += 1; } - var bytes = intArrayFromString(string); - var strptr = allocate(bytes, ALLOC_NORMAL); + var strptr = stringToNewUTF8(string); this.allocatedmem.push(strptr); this.db.handleError(sqlite3_bind_text( this.stmt, pos, strptr, - bytes.length - 1, + -1, 0 )); return true; @@ -563,7 +558,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { pos = this.pos; this.pos += 1; } - var blobptr = allocate(array, ALLOC_NORMAL); + var blobptr = _malloc(array.length); + writeArrayToMemory(array, blobptr); this.allocatedmem.push(blobptr); this.db.handleError(sqlite3_bind_blob( this.stmt, @@ -734,12 +730,10 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { */ function StatementIterator(sql, db) { this.db = db; - var sz = lengthBytesUTF8(sql) + 1; - this.sqlPtr = _malloc(sz); + this.sqlPtr = stringToNewUTF8(sql); if (this.sqlPtr === null) { throw new Error("Unable to allocate memory for the SQL string"); } - stringToUTF8(sql, this.sqlPtr, sz); this.nextSqlPtr = this.sqlPtr; this.nextSqlString = null; this.activeStatement = null; @@ -952,25 +946,27 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { if (!this.db) { throw "Database closed"; } - var stack = stackSave(); var stmt = null; + var originalSqlPtr = null; + var currentSqlPtr = null; try { - var nextSqlPtr = allocateUTF8OnStack(sql); + originalSqlPtr = stringToNewUTF8(sql); + currentSqlPtr = originalSqlPtr; var pzTail = stackAlloc(4); var results = []; - while (getValue(nextSqlPtr, "i8") !== NULL) { + while (getValue(currentSqlPtr, "i8") !== NULL) { setValue(apiTemp, 0, "i32"); setValue(pzTail, 0, "i32"); this.handleError(sqlite3_prepare_v2_sqlptr( this.db, - nextSqlPtr, + currentSqlPtr, -1, apiTemp, pzTail )); // pointer to a statement, or null var pStmt = getValue(apiTemp, "i32"); - nextSqlPtr = getValue(pzTail, "i32"); + currentSqlPtr = getValue(pzTail, "i32"); // Empty statement if (pStmt !== NULL) { var curresult = null; @@ -996,7 +992,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { if (stmt) stmt["free"](); throw errCaught; } finally { - stackRestore(stack); + if (originalSqlPtr) _free(originalSqlPtr); } }; @@ -1204,7 +1200,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { if (result === null) { sqlite3_result_null(cx); } else if (result.length != null) { - var blobptr = allocate(result, ALLOC_NORMAL); + var blobptr = _malloc(result.length); + writeArrayToMemory(result, blobptr); sqlite3_result_blob(cx, blobptr, result.length, -1); _free(blobptr); } else { diff --git a/src/exported_runtime_methods.json b/src/exported_runtime_methods.json index 245d2078..f099056f 100644 --- a/src/exported_runtime_methods.json +++ b/src/exported_runtime_methods.json @@ -4,10 +4,8 @@ "stackSave", "stackRestore", "UTF8ToString", - -"allocate", -"ALLOC_NORMAL", -"allocateUTF8OnStack", +"stringToNewUTF8", +"writeArrayToMemory", "removeFunction", "addFunction" ] From 52e5649f3a3a2a46aa4ad58a79d118c22f56cf30 Mon Sep 17 00:00:00 2001 From: Aidan Temple <15520814+aidant@users.noreply.github.com> Date: Mon, 14 Apr 2025 17:38:31 +1000 Subject: [PATCH 4/4] Update CONTRIBUTING.md (#610) --- CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f6e06d68..be4d6451 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,7 +31,7 @@ You're now ready to test the dev environment: 4. Click on Terminal->New Terminal to be dropped into a terminal inside the dev environment. 5. Run `$ npm install` to install the required modules 6. Run `$ npm test` to ensure all tests pass -7. Run `$ npm rebuild` to re-compile the project from scratch (using the version of EMSDK installed in the container). +7. Run `$ npm run rebuild` to re-compile the project from scratch (using the version of EMSDK installed in the container). 8. Run `$ npm test` to ensure all tests pass after said rebuild You're now ready for development! @@ -47,7 +47,7 @@ Instructions: 2. Clone this repository 3. Run `$ npm install` to install the required modules 4. Run `$ npm test` to ensure all tests pass -5. Run `$ npm rebuild` to re-compile the project from scratch (using the version of EMSDK installed in the container). +5. Run `$ npm run rebuild` to re-compile the project from scratch (using the version of EMSDK installed in the container). 6. Run `$ npm test` to ensure all tests pass after said rebuild ## Compiling SQLite with different options