Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of emscripten's allocate function. #606

Merged
merged 6 commits into from
Mar 20, 2025
Merged
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
37 changes: 17 additions & 20 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -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 {
6 changes: 2 additions & 4 deletions src/exported_runtime_methods.json
Original file line number Diff line number Diff line change
@@ -4,10 +4,8 @@
"stackSave",
"stackRestore",
"UTF8ToString",

"allocate",
"ALLOC_NORMAL",
"allocateUTF8OnStack",
"stringToNewUTF8",
"writeArrayToMemory",
"removeFunction",
"addFunction"
]