diff options
author | stephan <stephan@noemail.net> | 2022-10-20 21:28:31 +0000 |
---|---|---|
committer | stephan <stephan@noemail.net> | 2022-10-20 21:28:31 +0000 |
commit | 96b6371d709a91015204b14e4b83e61a209bbde2 (patch) | |
tree | 00e7ca3dc7a33227b5540dc266ebbe4c7a2a3931 /ext/wasm/api | |
parent | 875b95d4f5759c78f69e70496f84fb797702304d (diff) | |
download | sqlite-96b6371d709a91015204b14e4b83e61a209bbde2.tar.gz sqlite-96b6371d709a91015204b14e4b83e61a209bbde2.zip |
Add more JS tests. Flesh out the aggregate UDF tests to use sqlite3_aggregate_context() so that they can each be used multiple times in the same statement. Add sqlite3_js_aggregate_context() convenience helper.
FossilOrigin-Name: 9d034ef5e1bab7c9651c2450dc85765fa6365d3f1414c711550de858ff8b3ece
Diffstat (limited to 'ext/wasm/api')
-rw-r--r-- | ext/wasm/api/sqlite3-api-oo1.js | 22 | ||||
-rw-r--r-- | ext/wasm/api/sqlite3-api-prologue.js | 61 | ||||
-rw-r--r-- | ext/wasm/api/sqlite3-api-worker1.js | 2 | ||||
-rw-r--r-- | ext/wasm/api/sqlite3-wasm.c | 1 |
4 files changed, 57 insertions, 29 deletions
diff --git a/ext/wasm/api/sqlite3-api-oo1.js b/ext/wasm/api/sqlite3-api-oo1.js index c2e7eb14f..d101bc17f 100644 --- a/ext/wasm/api/sqlite3-api-oo1.js +++ b/ext/wasm/api/sqlite3-api-oo1.js @@ -149,9 +149,9 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ if( flagsStr.indexOf('w')>=0 ) oflags |= capi.SQLITE_OPEN_READWRITE; if( 0===oflags ) oflags |= capi.SQLITE_OPEN_READONLY; oflags |= capi.SQLITE_OPEN_EXRESCODE; - const scope = wasm.scopedAllocPush(); + const stack = wasm.pstack.pointer; try { - const pPtr = wasm.allocPtr() /* output (sqlite3**) arg */; + const pPtr = wasm.pstack.allocPtr() /* output (sqlite3**) arg */; const pVfsName = vfsName ? ( ('number'===typeof vfsName ? vfsName : wasm.scopedAllocCString(vfsName)) ): 0; @@ -163,21 +163,19 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ __dbTraceToConsole, 0); } // Check for per-VFS post-open SQL... - wasm.setPtrValue(pPtr, 0); - if(0===capi.sqlite3_file_control( - pDb, "main", capi.SQLITE_FCNTL_VFS_POINTER, pPtr - )){ - const postInitSql = __vfsPostOpenSql[wasm.getPtrValue(pPtr)]; - if(postInitSql){ - rc = capi.sqlite3_exec(pDb, postInitSql, 0, 0, 0); - checkSqlite3Rc(pDb, rc); - } + const pVfs = capi.sqlite3_js_db_vfs(pDb); + //console.warn("Opened db",fn,"with vfs",vfsName,pVfs); + if(!pVfs) toss3("Internal error: cannot get VFS for new db handle."); + const postInitSql = __vfsPostOpenSql[pVfs]; + if(postInitSql){ + rc = capi.sqlite3_exec(pDb, postInitSql, 0, 0, 0); + checkSqlite3Rc(pDb, rc); } }catch( e ){ if( pDb ) capi.sqlite3_close_v2(pDb); throw e; }finally{ - wasm.scopedAllocPop(scope); + wasm.pstack.restore(stack); } this.filename = fnJs; __ptrMap.set(this, pDb); diff --git a/ext/wasm/api/sqlite3-api-prologue.js b/ext/wasm/api/sqlite3-api-prologue.js index ca1542e2b..da7d0740f 100644 --- a/ext/wasm/api/sqlite3-api-prologue.js +++ b/ext/wasm/api/sqlite3-api-prologue.js @@ -280,6 +280,14 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( this.name = 'WasmAllocError'; } }; + /** + Functionally equivalent to the WasmAllocError constructor but may + be used as part of an expression, e.g.: + + ``` + return someAllocatingFunction(x) || WasmAllocError.toss(...); + ``` + */ WasmAllocError.toss = (...args)=>{ throw new WasmAllocError(args.join(' ')); }; @@ -508,6 +516,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( "flexible-string" argument converter. */ sqlite3_exec: (pDb, sql, callback, pVoid, pErrMsg)=>{}/*installed later*/, + /** Various internal-use utilities are added here as needed. They are bound to an object only so that we have access to them in @@ -1024,6 +1033,14 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( this.name = 'SQLite3Error'; } }; + /** + Functionally equivalent to the SQLite3Error constructor but may + be used as part of an expression, e.g.: + + ``` + return someFunction(x) || SQLite3Error.toss(...); + ``` + */ SQLite3Error.toss = (...args)=>{ throw new SQLite3Error(args.join(' ')); }; @@ -1096,8 +1113,10 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( pointer. The 3rd argument specifies the database name of the given database connection to check, defaulting to the main db. - The 2nd and 3rd arguments may either be a JS string or a C-string - allocated from the wasm environment. + The 2nd and 3rd arguments may either be a JS string or a WASM + C-string. If the 2nd argument is a NULL WASM pointer, the default + VFS is assumed. If the 3rd is a NULL WASM pointer, "main" is + assumed. The truthy value it returns is a pointer to the `sqlite3_vfs` object. @@ -1112,17 +1131,9 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( const pK = capi.sqlite3_vfs_find(vfsName); if(!pK) return false; else if(!pDb){ - return capi.sqlite3_vfs_find(0)===pK ? pK : false; - } - const ppVfs = wasm.allocPtr(); - try{ - return ( - (0===capi.sqlite3_file_control( - pDb, dbName, capi.SQLITE_FCNTL_VFS_POINTER, ppVfs - )) && (wasm.getPtrValue(ppVfs) === pK) - ) ? pK : false; - }finally{ - wasm.dealloc(ppVfs); + return pK===capi.sqlite3_vfs_find(0) ? pK : false; + }else{ + return pK===capi.sqlite3_js_db_vfs(pDb) ? pK : false; } }catch(e){ /* Ignore - probably bad args to a wasm-bound function. */ @@ -1179,14 +1190,32 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( ? wasm.heap8u().slice(pOut, pOut + Number(nOut)) : new Uint8Array(); return rc; - }catch(e){ - console.error('internal error?',e); - throw w; }finally{ if(pOut) wasm.exports.sqlite3_free(pOut); wasm.pstack.restore(stack); } }; + + /** + Given a `sqlite3*` and a database name (JS string or WASM + C-string pointer, which may be 0), returns a pointer to the + sqlite3_vfs responsible for it. If the given db name is null/0, + or not provided, then "main" is assumed. + */ + capi.sqlite3_js_db_vfs = + (dbPointer, dbName=0)=>wasm.sqlite3_wasm_db_vfs(dbPointer, dbName); + + /** + A thin wrapper around capi.sqlite3_aggregate_context() which + behaves the same except that it throws a WasmAllocError if that + function returns 0. + */ + capi.sqlite3_js_aggregate_context = (pCtx, n)=>{ + return capi.sqlite3_aggregate_context(pCtx, n) + || WasmAllocError.toss( + "Cannot allocate",n,"bytes for sqlite3_aggregate_context()" + ); + }; if( capi.util.isMainWindow() ){ /* Features specific to the main window thread... */ diff --git a/ext/wasm/api/sqlite3-api-worker1.js b/ext/wasm/api/sqlite3-api-worker1.js index fa6a43846..dea06341a 100644 --- a/ext/wasm/api/sqlite3-api-worker1.js +++ b/ext/wasm/api/sqlite3-api-worker1.js @@ -231,7 +231,7 @@ If the `dbId` does not refer to an opened ID, this is a no-op. If the `args` object contains a truthy `unlink` value then the database - will unlinked (deleted) after closing it. The inability to close a + will be unlinked (deleted) after closing it. The inability to close a db (because it's not opened) or delete its file does not trigger an error. diff --git a/ext/wasm/api/sqlite3-wasm.c b/ext/wasm/api/sqlite3-wasm.c index 1decf0593..8b767948e 100644 --- a/ext/wasm/api/sqlite3-wasm.c +++ b/ext/wasm/api/sqlite3-wasm.c @@ -48,6 +48,7 @@ ** want undefined. Please keep these alphabetized. */ #undef SQLITE_OMIT_DESERIALIZE +#undef SQLITE_OMIT_MEMORYDB /* ** Define any SQLITE_... config defaults we want if they aren't |