From 008d5990dec80a4cdfc21cb831964066bc65d36e Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 17 Mar 2025 12:21:47 -0700 Subject: [PATCH 1/5] Remove usage of emscripten's allocate function. This function is deprecated upstream. --- src/api.js | 12 ++++++------ src/exported_runtime_methods.json | 5 ++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/api.js b/src/api.js index e1d75dfb..66ca2e3e 100644 --- a/src/api.js +++ b/src/api.js @@ -13,8 +13,6 @@ UTF8ToString stringToUTF8 lengthBytesUTF8 - allocate - ALLOC_NORMAL allocateUTF8OnStack removeFunction addFunction @@ -545,8 +543,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { pos = this.pos; this.pos += 1; } - var bytes = intArrayFromString(string); - var strptr = allocate(bytes, ALLOC_NORMAL); + var length = lengthBytesUTF8(string); + var strptr = stringToNewUTF8(string); this.allocatedmem.push(strptr); this.db.handleError(sqlite3_bind_text( this.stmt, @@ -563,7 +561,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, @@ -1204,7 +1203,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..d59643e1 100644 --- a/src/exported_runtime_methods.json +++ b/src/exported_runtime_methods.json @@ -4,9 +4,8 @@ "stackSave", "stackRestore", "UTF8ToString", - -"allocate", -"ALLOC_NORMAL", +"stringToNewUTF8", +"writeArrayToMemory", "allocateUTF8OnStack", "removeFunction", "addFunction" From 1b161eeeaf4b64ded75e95847aac2ba4d746f5d7 Mon Sep 17 00:00:00 2001 From: lovasoa Date: Wed, 19 Mar 2025 23:35:35 +0000 Subject: [PATCH 2/5] fix variable name --- src/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api.js b/src/api.js index 66ca2e3e..7285547d 100644 --- a/src/api.js +++ b/src/api.js @@ -550,7 +550,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { this.stmt, pos, strptr, - bytes.length - 1, + length - 1, 0 )); return true; From 3a3e908fb6f0160146e77ae94c6ec2e1e8f09759 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 19 Mar 2025 17:30:56 -0700 Subject: [PATCH 3/5] Avoid lengthBytesUTF8 --- src/api.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/api.js b/src/api.js index d78fa662..ac4f340a 100644 --- a/src/api.js +++ b/src/api.js @@ -5,17 +5,16 @@ _malloc _free getValue - intArrayFromString setValue stackAlloc stackRestore stackSave UTF8ToString - stringToUTF8 - lengthBytesUTF8 + stringToNewUTF8 allocateUTF8OnStack removeFunction addFunction + writeArrayToMemory */ "use strict"; @@ -543,14 +542,13 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { pos = this.pos; this.pos += 1; } - var length = lengthBytesUTF8(string); var strptr = stringToNewUTF8(string); this.allocatedmem.push(strptr); this.db.handleError(sqlite3_bind_text( this.stmt, pos, strptr, - length - 1, + -1, 0 )); return true; @@ -733,12 +731,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; From 2d8a41024b4a6dc122069ed0a1e9058e4f2def79 Mon Sep 17 00:00:00 2001 From: lovasoa Date: Thu, 20 Mar 2025 08:44:00 +0000 Subject: [PATCH 4/5] lint --- src/api.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api.js b/src/api.js index ac4f340a..46f47b00 100644 --- a/src/api.js +++ b/src/api.js @@ -560,7 +560,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { this.pos += 1; } var blobptr = _malloc(array.length); - writeArrayToMemory(array, blobptr) + writeArrayToMemory(array, blobptr); this.allocatedmem.push(blobptr); this.db.handleError(sqlite3_bind_blob( this.stmt, @@ -1200,7 +1200,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { sqlite3_result_null(cx); } else if (result.length != null) { var blobptr = _malloc(result.length); - writeArrayToMemory(result, blobptr) + writeArrayToMemory(result, blobptr); sqlite3_result_blob(cx, blobptr, result.length, -1); _free(blobptr); } else { From 095a99a8653c171988b48c8ff1475cf720ae8e53 Mon Sep 17 00:00:00 2001 From: lovasoa Date: Thu, 20 Mar 2025 09:03:18 +0000 Subject: [PATCH 5/5] remove usage of deprecated allocateUTF8OnStack --- src/api.js | 15 ++++++++------- src/exported_runtime_methods.json | 1 - 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/api.js b/src/api.js index 46f47b00..c7f102b7 100644 --- a/src/api.js +++ b/src/api.js @@ -11,7 +11,6 @@ stackSave UTF8ToString stringToNewUTF8 - allocateUTF8OnStack removeFunction addFunction writeArrayToMemory @@ -947,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; @@ -991,7 +992,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { if (stmt) stmt["free"](); throw errCaught; } finally { - stackRestore(stack); + if (originalSqlPtr) _free(originalSqlPtr); } }; diff --git a/src/exported_runtime_methods.json b/src/exported_runtime_methods.json index d59643e1..f099056f 100644 --- a/src/exported_runtime_methods.json +++ b/src/exported_runtime_methods.json @@ -6,7 +6,6 @@ "UTF8ToString", "stringToNewUTF8", "writeArrayToMemory", -"allocateUTF8OnStack", "removeFunction", "addFunction" ]