diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/fiddle/EXPORTED_RUNTIME_METHODS | 1 | ||||
-rw-r--r-- | ext/fiddle/sqlite3-api.js | 75 | ||||
-rw-r--r-- | ext/fiddle/testing1.js | 15 |
3 files changed, 55 insertions, 36 deletions
diff --git a/ext/fiddle/EXPORTED_RUNTIME_METHODS b/ext/fiddle/EXPORTED_RUNTIME_METHODS index 3d06c3219..af2c48a3f 100644 --- a/ext/fiddle/EXPORTED_RUNTIME_METHODS +++ b/ext/fiddle/EXPORTED_RUNTIME_METHODS @@ -11,3 +11,4 @@ setValue stackAlloc stackRestore stackSave +stringToUTF8Array diff --git a/ext/fiddle/sqlite3-api.js b/ext/fiddle/sqlite3-api.js index eabac6cc5..450bbb62c 100644 --- a/ext/fiddle/sqlite3-api.js +++ b/ext/fiddle/sqlite3-api.js @@ -405,7 +405,7 @@ Module.postRun.push(function(namespace/*the module object, the target for ['getValue','setValue', 'stackSave', 'stackRestore', 'stackAlloc', 'allocateUTF8OnStack', '_malloc', '_free', 'addFunction', 'removeFunction', - 'intArrayFromString' + 'intArrayFromString', 'lengthBytesUTF8', 'stringToUTF8Array' ].forEach(function(m){ if(undefined === (api.wasm[m] = SQM[m])){ toss("Internal init error: Module."+m+" not found."); @@ -524,7 +524,6 @@ Module.postRun.push(function(namespace/*the module object, the target for this._pStmt = arguments[1]; this.columnCount = api.sqlite3_column_count(this._pStmt); this.parameterCount = api.sqlite3_bind_parameter_count(this._pStmt); - this._allocs = [/*list of alloc'd memory blocks for bind() values*/] }; /** Throws if the given DB has been closed, else it is returned. */ @@ -1186,12 +1185,29 @@ Module.postRun.push(function(namespace/*the module object, the target for if(!f._){ f._ = { string: function(stmt, ndx, val, asBlob){ - const bytes = api.wasm.intArrayFromString(val,true); - const pStr = api.wasm._malloc(bytes.length || 1); - stmt._allocs.push(pStr); - api.wasm._malloc.HEAP.set(bytes, pStr); - const func = asBlob ? api.sqlite3_bind_blob : api.sqlite3_bind_text; - return func(stmt._pStmt, ndx, pStr, bytes.length, api.SQLITE_STATIC); + if(1){ + /* _Hypothetically_ more efficient than the impl in the 'else' block. */ + const stack = api.wasm.stackSave(); + try{ + const n = api.wasm.lengthBytesUTF8(val)+1/*required for NUL terminator*/; + const pStr = api.wasm.stackAlloc(n); + api.wasm.stringToUTF8Array(val, api.wasm.HEAP8, pStr, n); + const f = asBlob ? api.sqlite3_bind_blob : api.sqlite3_bind_text; + return f(stmt._pStmt, ndx, pStr, n-1, api.SQLITE_TRANSIENT); + }finally{ + api.wasm.stackRestore(stack); + } + }else{ + const bytes = api.wasm.intArrayFromString(val,true); + const pStr = api.wasm._malloc(bytes.length || 1); + api.wasm._malloc.HEAP.set(bytes.length ? bytes : [0], pStr); + try{ + const f = asBlob ? api.sqlite3_bind_blob : api.sqlite3_bind_text; + return f(stmt._pStmt, ndx, pStr, bytes.length, api.SQLITE_TRANSIENT); + }finally{ + api.wasm._free(pStr); + } + } } }; } @@ -1221,16 +1237,28 @@ Module.postRun.push(function(namespace/*the module object, the target for case BindTypes.blob: { if('string'===typeof val){ rc = f._.string(stmt, ndx, val, true); - }else{ - if(!isSupportedTypedArray(val)){ - toss("Binding a value as a blob requires", - "that it be a string, Uint8Array, or Int8Array."); + }else if(!isSupportedTypedArray(val)){ + toss("Binding a value as a blob requires", + "that it be a string, Uint8Array, or Int8Array."); + }else if(1){ + /* _Hypothetically_ more efficient than the impl in the 'else' block. */ + const stack = api.wasm.stackSave(); + try{ + const pBlob = api.wasm.stackAlloc(val.byteLength || 1); + api.wasm.HEAP8.set(val.byteLength ? val : [0], pBlob) + rc = api.sqlite3_bind_blob(stmt._pStmt, ndx, pBlob, val.byteLength, + api.SQLITE_TRANSIENT); + }finally{ + api.wasm.stackRestore(stack); } - //console.debug("Binding blob",len,val); + }else{ const pBlob = api.wasm.mallocFromTypedArray(val); - stmt._allocs.push(pBlob); - rc = api.sqlite3_bind_blob(stmt._pStmt, ndx, pBlob, val.byteLength, - api.SQLITE_STATIC); + try{ + rc = api.sqlite3_bind_blob(stmt._pStmt, ndx, pBlob, val.byteLength, + api.SQLITE_TRANSIENT); + }finally{ + api.wasm._free(pBlob); + } } break; } @@ -1241,16 +1269,6 @@ Module.postRun.push(function(namespace/*the module object, the target for if(rc) stmt.db.checkRc(rc); return stmt; }; - - /** Frees any memory explicitly allocated for the given - Stmt object. Returns stmt. */ - const freeBindMemory = function(stmt){ - let m; - while(undefined !== (m = stmt._allocs.pop())){ - api.wasm._free(m); - } - return stmt; - }; Stmt.prototype = { /** @@ -1262,7 +1280,6 @@ Module.postRun.push(function(namespace/*the module object, the target for finalize: function(){ if(this._pStmt){ affirmUnlocked(this,'finalize()'); - freeBindMemory(this); delete this.db._statements[this._pStmt]; api.sqlite3_finalize(this._pStmt); delete this.columnCount; @@ -1275,9 +1292,7 @@ Module.postRun.push(function(namespace/*the module object, the target for /** Clears all bound values. Returns this object. Throws if this statement has been finalized. */ clearBindings: function(){ - freeBindMemory( - affirmUnlocked(affirmStmtOpen(this), 'clearBindings()') - ); + affirmUnlocked(affirmStmtOpen(this), 'clearBindings()') api.sqlite3_clear_bindings(this._pStmt); this._mayGet = false; return this; diff --git a/ext/fiddle/testing1.js b/ext/fiddle/testing1.js index cc84ef6ef..9c17115f3 100644 --- a/ext/fiddle/testing1.js +++ b/ext/fiddle/testing1.js @@ -16,6 +16,7 @@ (function(){ const T = self.SqliteTestUtil; const log = console.log.bind(console); + const debug = console.debug.bind(console); const assert = function(condition, text) { if (!condition) { @@ -31,7 +32,7 @@ new TextEncoder('utf-8').encode("select 3 as a") /* Testing handling of Uint8Array input */ ); - //log("statement =",st); + //debug("statement =",st); T.assert(st._pStmt) .assert(!st._mayGet) .assert('a' === st.getColumnName(0)) @@ -76,7 +77,7 @@ /* Achtung: ^^^ bind support might be removed from multi-mode exec. */ }); T.assert(2 === list.length); - //log("Exec'd SQL:", list); + //debug("Exec'd SQL:", list); let blob = db.selectValue("select b from t where a='blob'"); T.assert(blob instanceof Uint8Array). @@ -138,7 +139,7 @@ assert('hi'===db.selectValue("select asis('hi')")); const eqApprox = function(v1,v2,factor=0.05){ - //log('eqApprox',v1, v2); + //debug('eqApprox',v1, v2); return v1>=(v2-factor) && v1<=(v2+factor); }; @@ -165,11 +166,11 @@ blobArg = new Int8Array(2); blobArg.set([0x68, 0x69]); - console.debug("blobArg=",blobArg); + //debug("blobArg=",blobArg); blobRc = db.selectValue("select asis(?1)", blobArg); T.assert(blobRc instanceof Uint8Array). assert(2 === blobRc.length); - console.debug("blobRc=",blobRc); + //debug("blobRc=",blobRc); T.assert(0x68==blobRc[0] && 0x69==blobRc[1]); }; @@ -196,9 +197,10 @@ const sqlite3 = Module.sqlite3; const api = sqlite3.api; const oo = sqlite3.SQLite3; - console.log("Loaded module:",api.sqlite3_libversion(), + log("Loaded module:",api.sqlite3_libversion(), api.sqlite3_sourceid()); log("Build options:",oo.compileOptionUsed()); + log("api.wasm.HEAP8 size =",api.wasm.HEAP8.length); const db = new oo.DB(); try { log("DB:",db.filename); @@ -213,6 +215,7 @@ db.close(); } log("Total Test count:",T.counter); + log("api.wasm.HEAP8 size =",api.wasm.HEAP8.length); }; initSqlite3Module(self.sqlite3TestModule).then(function(theModule){ |