5
5
_malloc
6
6
_free
7
7
getValue
8
- intArrayFromString
9
8
setValue
10
9
stackAlloc
11
10
stackRestore
12
11
stackSave
13
12
UTF8ToString
14
- stringToUTF8
15
- lengthBytesUTF8
16
- allocate
17
- ALLOC_NORMAL
18
- allocateUTF8OnStack
13
+ stringToNewUTF8
19
14
removeFunction
20
15
addFunction
16
+ writeArrayToMemory
21
17
*/
22
18
23
19
"use strict" ;
@@ -545,14 +541,13 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
545
541
pos = this . pos ;
546
542
this . pos += 1 ;
547
543
}
548
- var bytes = intArrayFromString ( string ) ;
549
- var strptr = allocate ( bytes , ALLOC_NORMAL ) ;
544
+ var strptr = stringToNewUTF8 ( string ) ;
550
545
this . allocatedmem . push ( strptr ) ;
551
546
this . db . handleError ( sqlite3_bind_text (
552
547
this . stmt ,
553
548
pos ,
554
549
strptr ,
555
- bytes . length - 1 ,
550
+ - 1 ,
556
551
0
557
552
) ) ;
558
553
return true ;
@@ -563,7 +558,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
563
558
pos = this . pos ;
564
559
this . pos += 1 ;
565
560
}
566
- var blobptr = allocate ( array , ALLOC_NORMAL ) ;
561
+ var blobptr = _malloc ( array . length ) ;
562
+ writeArrayToMemory ( array , blobptr ) ;
567
563
this . allocatedmem . push ( blobptr ) ;
568
564
this . db . handleError ( sqlite3_bind_blob (
569
565
this . stmt ,
@@ -734,12 +730,10 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
734
730
*/
735
731
function StatementIterator ( sql , db ) {
736
732
this . db = db ;
737
- var sz = lengthBytesUTF8 ( sql ) + 1 ;
738
- this . sqlPtr = _malloc ( sz ) ;
733
+ this . sqlPtr = stringToNewUTF8 ( sql ) ;
739
734
if ( this . sqlPtr === null ) {
740
735
throw new Error ( "Unable to allocate memory for the SQL string" ) ;
741
736
}
742
- stringToUTF8 ( sql , this . sqlPtr , sz ) ;
743
737
this . nextSqlPtr = this . sqlPtr ;
744
738
this . nextSqlString = null ;
745
739
this . activeStatement = null ;
@@ -952,25 +946,27 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
952
946
if ( ! this . db ) {
953
947
throw "Database closed" ;
954
948
}
955
- var stack = stackSave ( ) ;
956
949
var stmt = null ;
950
+ var originalSqlPtr = null ;
951
+ var currentSqlPtr = null ;
957
952
try {
958
- var nextSqlPtr = allocateUTF8OnStack ( sql ) ;
953
+ originalSqlPtr = stringToNewUTF8 ( sql ) ;
954
+ currentSqlPtr = originalSqlPtr ;
959
955
var pzTail = stackAlloc ( 4 ) ;
960
956
var results = [ ] ;
961
- while ( getValue ( nextSqlPtr , "i8" ) !== NULL ) {
957
+ while ( getValue ( currentSqlPtr , "i8" ) !== NULL ) {
962
958
setValue ( apiTemp , 0 , "i32" ) ;
963
959
setValue ( pzTail , 0 , "i32" ) ;
964
960
this . handleError ( sqlite3_prepare_v2_sqlptr (
965
961
this . db ,
966
- nextSqlPtr ,
962
+ currentSqlPtr ,
967
963
- 1 ,
968
964
apiTemp ,
969
965
pzTail
970
966
) ) ;
971
967
// pointer to a statement, or null
972
968
var pStmt = getValue ( apiTemp , "i32" ) ;
973
- nextSqlPtr = getValue ( pzTail , "i32" ) ;
969
+ currentSqlPtr = getValue ( pzTail , "i32" ) ;
974
970
// Empty statement
975
971
if ( pStmt !== NULL ) {
976
972
var curresult = null ;
@@ -996,7 +992,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
996
992
if ( stmt ) stmt [ "free" ] ( ) ;
997
993
throw errCaught ;
998
994
} finally {
999
- stackRestore ( stack ) ;
995
+ if ( originalSqlPtr ) _free ( originalSqlPtr ) ;
1000
996
}
1001
997
} ;
1002
998
@@ -1204,7 +1200,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
1204
1200
if ( result === null ) {
1205
1201
sqlite3_result_null ( cx ) ;
1206
1202
} else if ( result . length != null ) {
1207
- var blobptr = allocate ( result , ALLOC_NORMAL ) ;
1203
+ var blobptr = _malloc ( result . length ) ;
1204
+ writeArrayToMemory ( result , blobptr ) ;
1208
1205
sqlite3_result_blob ( cx , blobptr , result . length , - 1 ) ;
1209
1206
_free ( blobptr ) ;
1210
1207
} else {
0 commit comments