Skip to content

Commit ee67aeb

Browse files
sbc100lovasoa
andauthoredMar 20, 2025
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 <contact@ophir.dev>
1 parent b645ca0 commit ee67aeb

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed
 

‎src/api.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@
55
_malloc
66
_free
77
getValue
8-
intArrayFromString
98
setValue
109
stackAlloc
1110
stackRestore
1211
stackSave
1312
UTF8ToString
14-
stringToUTF8
15-
lengthBytesUTF8
16-
allocate
17-
ALLOC_NORMAL
18-
allocateUTF8OnStack
13+
stringToNewUTF8
1914
removeFunction
2015
addFunction
16+
writeArrayToMemory
2117
*/
2218

2319
"use strict";
@@ -545,14 +541,13 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
545541
pos = this.pos;
546542
this.pos += 1;
547543
}
548-
var bytes = intArrayFromString(string);
549-
var strptr = allocate(bytes, ALLOC_NORMAL);
544+
var strptr = stringToNewUTF8(string);
550545
this.allocatedmem.push(strptr);
551546
this.db.handleError(sqlite3_bind_text(
552547
this.stmt,
553548
pos,
554549
strptr,
555-
bytes.length - 1,
550+
-1,
556551
0
557552
));
558553
return true;
@@ -563,7 +558,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
563558
pos = this.pos;
564559
this.pos += 1;
565560
}
566-
var blobptr = allocate(array, ALLOC_NORMAL);
561+
var blobptr = _malloc(array.length);
562+
writeArrayToMemory(array, blobptr);
567563
this.allocatedmem.push(blobptr);
568564
this.db.handleError(sqlite3_bind_blob(
569565
this.stmt,
@@ -734,12 +730,10 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
734730
*/
735731
function StatementIterator(sql, db) {
736732
this.db = db;
737-
var sz = lengthBytesUTF8(sql) + 1;
738-
this.sqlPtr = _malloc(sz);
733+
this.sqlPtr = stringToNewUTF8(sql);
739734
if (this.sqlPtr === null) {
740735
throw new Error("Unable to allocate memory for the SQL string");
741736
}
742-
stringToUTF8(sql, this.sqlPtr, sz);
743737
this.nextSqlPtr = this.sqlPtr;
744738
this.nextSqlString = null;
745739
this.activeStatement = null;
@@ -952,25 +946,27 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
952946
if (!this.db) {
953947
throw "Database closed";
954948
}
955-
var stack = stackSave();
956949
var stmt = null;
950+
var originalSqlPtr = null;
951+
var currentSqlPtr = null;
957952
try {
958-
var nextSqlPtr = allocateUTF8OnStack(sql);
953+
originalSqlPtr = stringToNewUTF8(sql);
954+
currentSqlPtr = originalSqlPtr;
959955
var pzTail = stackAlloc(4);
960956
var results = [];
961-
while (getValue(nextSqlPtr, "i8") !== NULL) {
957+
while (getValue(currentSqlPtr, "i8") !== NULL) {
962958
setValue(apiTemp, 0, "i32");
963959
setValue(pzTail, 0, "i32");
964960
this.handleError(sqlite3_prepare_v2_sqlptr(
965961
this.db,
966-
nextSqlPtr,
962+
currentSqlPtr,
967963
-1,
968964
apiTemp,
969965
pzTail
970966
));
971967
// pointer to a statement, or null
972968
var pStmt = getValue(apiTemp, "i32");
973-
nextSqlPtr = getValue(pzTail, "i32");
969+
currentSqlPtr = getValue(pzTail, "i32");
974970
// Empty statement
975971
if (pStmt !== NULL) {
976972
var curresult = null;
@@ -996,7 +992,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
996992
if (stmt) stmt["free"]();
997993
throw errCaught;
998994
} finally {
999-
stackRestore(stack);
995+
if (originalSqlPtr) _free(originalSqlPtr);
1000996
}
1001997
};
1002998

@@ -1204,7 +1200,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
12041200
if (result === null) {
12051201
sqlite3_result_null(cx);
12061202
} else if (result.length != null) {
1207-
var blobptr = allocate(result, ALLOC_NORMAL);
1203+
var blobptr = _malloc(result.length);
1204+
writeArrayToMemory(result, blobptr);
12081205
sqlite3_result_blob(cx, blobptr, result.length, -1);
12091206
_free(blobptr);
12101207
} else {

‎src/exported_runtime_methods.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
"stackSave",
55
"stackRestore",
66
"UTF8ToString",
7-
8-
"allocate",
9-
"ALLOC_NORMAL",
10-
"allocateUTF8OnStack",
7+
"stringToNewUTF8",
8+
"writeArrayToMemory",
119
"removeFunction",
1210
"addFunction"
1311
]

0 commit comments

Comments
 (0)
Failed to load comments.