diff options
author | stephan <stephan@noemail.net> | 2022-12-09 12:12:49 +0000 |
---|---|---|
committer | stephan <stephan@noemail.net> | 2022-12-09 12:12:49 +0000 |
commit | 3ec44736b51d1f8ed7239654e1ee891e93fd7577 (patch) | |
tree | c3a0b2e954bfe3565ec52cfb5474445be95b906e | |
parent | 81a368317456d9c1500e33949b214185e1e0b565 (diff) | |
download | sqlite-3ec44736b51d1f8ed7239654e1ee891e93fd7577.tar.gz sqlite-3ec44736b51d1f8ed7239654e1ee891e93fd7577.zip |
Remove some unused sqlite3_status() codes from the JS API. Add custom JS wrappers for sqlite3_create_collation/_v2() which accept JS functions (plus tests). Expand the argument options for sqlite3_wasm_db_error() to enable it to translate exception objects to C-level errors.
FossilOrigin-Name: 073a2f1eb006230ae0995a5ea6c789407bcaa819ec15b5064c66d8973ed4671a
-rw-r--r-- | ext/wasm/api/sqlite3-api-glue.js | 83 | ||||
-rw-r--r-- | ext/wasm/api/sqlite3-api-prologue.js | 10 | ||||
-rw-r--r-- | ext/wasm/api/sqlite3-wasm.c | 6 | ||||
-rw-r--r-- | ext/wasm/tester1.c-pp.js | 23 | ||||
-rw-r--r-- | manifest | 18 | ||||
-rw-r--r-- | manifest.uuid | 2 |
6 files changed, 125 insertions, 17 deletions
diff --git a/ext/wasm/api/sqlite3-api-glue.js b/ext/wasm/api/sqlite3-api-glue.js index 80438e086..6d9d45cda 100644 --- a/ext/wasm/api/sqlite3-api-glue.js +++ b/ext/wasm/api/sqlite3-api-glue.js @@ -158,9 +158,35 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ delete wasm.bindingSignatures; if(wasm.exports.sqlite3_wasm_db_error){ - util.sqlite3_wasm_db_error = wasm.xWrap( + const __db_err = wasm.xWrap( 'sqlite3_wasm_db_error', 'int', 'sqlite3*', 'int', 'string' ); + /** + Sets the given db's error state. Accepts: + + - (sqlite3*, int code, string msg) + - (sqlite3*, Error [,string msg]) + + If passed a WasmAllocError, the message is ingored and the + result code is SQLITE_NOMEM. If passed any other Error type, + the result code defaults to SQLITE_ERROR unless the Error + object has a resultCode property, in which case that is used + (e.g. SQLite3Error has that). If passed a non-WasmAllocError + exception, the message string defaults to theError.message. + + Returns the resulting code. Pass (pDb,0,0) to clear the error + state. + */ + util.sqlite3_wasm_db_error = function(pDb, resultCode, message){ + if(resultCode instanceof sqlite3.WasmAllocError){ + resultCode = capi.SQLITE_NOMEM; + message = 0 /*avoid allocating message string*/; + }else if(resultCode instanceof Error){ + message = message || resultCode.message; + resultCode = (resultCode.resultCode || capi.SQLITE_ERROR); + } + return __db_err(pDb, resultCode, message); + }; }else{ util.sqlite3_wasm_db_error = function(pDb,errCode,msg){ console.warn("sqlite3_wasm_db_error() is not exported.",arguments); @@ -180,6 +206,61 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ (1===n?"":'s')+"."); }; + if(1){/* Bindings for sqlite3_create_collation() */ + + const __ccv2 = wasm.xWrap( + 'sqlite3_create_collation_v2', 'int', + 'sqlite3*','string','int','*','*','*' + /* int(*xCompare)(void*,int,const void*,int,const void*) */ + /* void(*xDestroy(void*) */); + + /** + Works exactly like C's sqlite3_create_collation_v2() except that: + + 1) It permits its two function arguments to be JS functions, + for which it will install WASM-bound proxies. + + 2) It returns capi.SQLITE_FORMAT if the 3rd argument is not + capi.SQLITE_UTF8. No other encodings are supported. + + Returns 0 on success, non-0 on error, in which case the error + state of pDb (of type `sqlite3*` or argument-convertible to it) + may contain more information. + */ + capi.sqlite3_create_collation_v2 = function(pDb,zName,eTextRep,pArg,xCompare,xDestroy){ + if(6!==arguments.length) return __dbArgcMismatch(pDb, 'sqlite3_create_collation_v2', 6); + else if(capi.SQLITE_UTF8!==eTextRep){ + return util.sqlite3_wasm_db_error( + pDb, capi.SQLITE_FORMAT, "SQLITE_UTF8 is the only supported encoding." + ); + } + let rc, pfCompare, pfDestroy; + try{ + if(xCompare instanceof Function){ + pfCompare = wasm.installFunction(xCompare, 'i(pipip)'); + } + if(xDestroy instanceof Function){ + pfDestroy = wasm.installFunction(xDestroy, 'v(p)'); + } + rc = __ccv2(pDb, zName, eTextRep, pArg, + pfCompare || xCompare, pfDestroy || xDestroy); + }catch(e){ + if(pfCompare) wasm.uninstallFunction(pfCompare); + if(pfDestroy) wasm.uninstallFunction(pfDestroy); + rc = util.sqlite3_wasm_db_error(pDb, e); + } + return rc; + }; + + capi.sqlite3_create_collation = (pDb,zName,eTextRep,pArg,xCompare)=>{ + return (5===arguments.length) + ? capi.sqlite3_create_collation_v2(pDb,zName,eTextRep,pArg,xCompare,0) + : __dbArgcMismatch(pDb, 'sqlite3_create_collation', 5); + } + + + }/*sqlite3_create_collation() and friends*/ + if(1){/* Special-case handling of sqlite3_exec() */ const __exec = wasm.xWrap("sqlite3_exec", "int", ["sqlite3*", "string:flexible", "*", "*", "**"]); diff --git a/ext/wasm/api/sqlite3-api-prologue.js b/ext/wasm/api/sqlite3-api-prologue.js index 22dd55e76..136d050c9 100644 --- a/ext/wasm/api/sqlite3-api-prologue.js +++ b/ext/wasm/api/sqlite3-api-prologue.js @@ -936,12 +936,16 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( /* sqlite3_create_function(), sqlite3_create_function_v2(), and sqlite3_create_window_function() use hand-written bindings to simplify handling of their function-type arguments. */ - ["sqlite3_create_collation", "int", - "sqlite3*", "string", "int"/*SQLITE_UTF8 is the only legal value*/, + /* sqlite3_create_collation() and sqlite3_create_collation_v2() + use hand-written bindings to simplify passing of the callback + function. + ["sqlite3_create_collation", "int", + "sqlite3*", "string", "int",//SQLITE_UTF8 is the only legal value "*", "*"], ["sqlite3_create_collation_v2", "int", - "sqlite3*", "string", "int"/*SQLITE_UTF8 is the only legal value*/, + "sqlite3*", "string", "int",//SQLITE_UTF8 is the only legal value "*", "*", "*"], + */ ["sqlite3_data_count", "int", "sqlite3_stmt*"], ["sqlite3_db_filename", "string", "sqlite3*", "string"], ["sqlite3_db_handle", "sqlite3*", "sqlite3_stmt*"], diff --git a/ext/wasm/api/sqlite3-wasm.c b/ext/wasm/api/sqlite3-wasm.c index ec0f72f73..dbed1b684 100644 --- a/ext/wasm/api/sqlite3-wasm.c +++ b/ext/wasm/api/sqlite3-wasm.c @@ -706,12 +706,12 @@ const char * sqlite3_wasm_enum_json(void){ DefInt(SQLITE_STATUS_MEMORY_USED); DefInt(SQLITE_STATUS_PAGECACHE_USED); DefInt(SQLITE_STATUS_PAGECACHE_OVERFLOW); - DefInt(SQLITE_STATUS_SCRATCH_USED) /* NOT USED */; - DefInt(SQLITE_STATUS_SCRATCH_OVERFLOW) /* NOT USED */; + //DefInt(SQLITE_STATUS_SCRATCH_USED) /* NOT USED */; + //DefInt(SQLITE_STATUS_SCRATCH_OVERFLOW) /* NOT USED */; DefInt(SQLITE_STATUS_MALLOC_SIZE); DefInt(SQLITE_STATUS_PARSER_STACK); DefInt(SQLITE_STATUS_PAGECACHE_SIZE); - DefInt(SQLITE_STATUS_SCRATCH_SIZE) /* NOT USED */; + //DefInt(SQLITE_STATUS_SCRATCH_SIZE) /* NOT USED */; DefInt(SQLITE_STATUS_MALLOC_COUNT); } _DefGroup; diff --git a/ext/wasm/tester1.c-pp.js b/ext/wasm/tester1.c-pp.js index ddf60def9..1dc58983f 100644 --- a/ext/wasm/tester1.c-pp.js +++ b/ext/wasm/tester1.c-pp.js @@ -2106,6 +2106,29 @@ self.sqlite3InitModule = sqlite3InitModule; .assert(2000===list[0][1]); } })/*custom vtab #2*/ + //////////////////////////////////////////////////////////////////////// + .t('Custom collation', function(sqlite3){ + let myCmp = function(pArg,n1,p1,n2,p2){ + //int (*)(void*,int,const void*,int,const void*) + const rc = wasm.exports.sqlite3_strnicmp(p1,p2,(n1<n2?n1:n2)); + return rc ? rc : (n1 - n2); + }; + let rc = capi.sqlite3_create_collation_v2(this.db, "mycollation", capi.SQLITE_UTF8, + 0, myCmp, 0); + this.db.checkRc(rc); + rc = this.db.selectValue("select 'hi' = 'HI' collate mycollation"); + T.assert(1===rc); + rc = this.db.selectValue("select 'hii' = 'HI' collate mycollation"); + T.assert(0===rc); + rc = this.db.selectValue("select 'hi' = 'HIi' collate mycollation"); + T.assert(0===rc); + rc = capi.sqlite3_create_collation(this.db,"hi",capi.SQLITE_UTF8/*not enough args*/); + T.assert(capi.SQLITE_MISUSE === rc); + rc = capi.sqlite3_create_collation_v2(this.db,"hi",0/*wrong encoding*/,0,0,0); + T.assert(capi.SQLITE_FORMAT === rc) + .mustThrowMatching(()=>this.db.checkRc(rc), + /SQLITE_UTF8 is the only supported encoding./); + }) //////////////////////////////////////////////////////////////////////// .t('Close db', function(){ @@ -1,5 +1,5 @@ -C Rename\sthe\soft-used,\sverbose\ssqlite3.wasm.get/setMemValue()\sand\sget/setPtrValue()\sto\speek/poke()\sand\speek/pokePtr().\sThe\sold\snames\sare\sretained\sas\saliases\sjust\sin\scase\sany\sclient\scode\sactually\suses\sthem,\sbut\sthey\sare\snow\sdeprecated. -D 2022-12-09T09:23:27.874 +C Remove\ssome\sunused\ssqlite3_status()\scodes\sfrom\sthe\sJS\sAPI.\sAdd\scustom\sJS\swrappers\sfor\ssqlite3_create_collation/_v2()\swhich\saccept\sJS\sfunctions\s(plus\stests).\sExpand\sthe\sargument\soptions\sfor\ssqlite3_wasm_db_error()\sto\senable\sit\sto\stranslate\sexception\sobjects\sto\sC-level\serrors. +D 2022-12-09T12:12:49.238 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -503,16 +503,16 @@ F ext/wasm/api/post-js-footer.js cd0a8ec768501d9bd45d325ab0442037fb0e33d1f3b4f08 F ext/wasm/api/post-js-header.js 47b6b281f39ad59fa6e8b658308cd98ea292c286a68407b35ff3ed9cfd281a62 F ext/wasm/api/pre-js.c-pp.js b88499dc303c21fc3f55f2c364a0f814f587b60a95784303881169f9e91c1d5f F ext/wasm/api/sqlite3-api-cleanup.js 680d5ccfff54459db136a49b2199d9f879c8405d9c99af1dda0cc5e7c29056f4 -F ext/wasm/api/sqlite3-api-glue.js f687f921f10a52800228445d10c7f7f4f5dab6059097e93d7749629b5fe5fe88 +F ext/wasm/api/sqlite3-api-glue.js a3c92fa35c2de17100504f7377d643b7ecfe5a6fbcdc795a6d8ca4eb0cf873ad F ext/wasm/api/sqlite3-api-oo1.js 6d10849609231ccd46fa11b1d3fbbe0f45d9fe84c66a0b054601036540844300 -F ext/wasm/api/sqlite3-api-prologue.js 496a4158e9904265d75715c57830d34ce528c4578b741d6f47c037d1a86fbe0d +F ext/wasm/api/sqlite3-api-prologue.js 80e672ec8c38521515f15196f18f6b28704412cc2c0101ba55e2f8285e04ddb7 F ext/wasm/api/sqlite3-api-worker1.js e94ba98e44afccfa482874cd9acb325883ade50ed1f9f9526beb9de1711f182f F ext/wasm/api/sqlite3-license-version-header.js a661182fc93fc2cf212dfd0b987f8e138a3ac98f850b1112e29b5fbdaecc87c3 F ext/wasm/api/sqlite3-opfs-async-proxy.js 7795b84b66a7a8dedc791340709b310bb497c3c72a80bef364fa2a58e2ddae3f F ext/wasm/api/sqlite3-v-helper.js 64bb3559446eb441ce87afe942f6f924ee135a9f069a82705ffefdcd55fc6481 F ext/wasm/api/sqlite3-vfs-opfs.c-pp.js 66daf6fb6843bea615fe193109e1542efbeca24f560ee9da63375a910bb48115 F ext/wasm/api/sqlite3-wasi.h 25356084cfe0d40458a902afb465df8c21fc4152c1d0a59b563a3fba59a068f9 -F ext/wasm/api/sqlite3-wasm.c ecf7af7259c7db4b467b7a3fec2faaa766777f370f201f5e6533593911d4acde +F ext/wasm/api/sqlite3-wasm.c daad00c6822facb6ceb506dadc2201c734e9128c457c93f9304df01542084216 F ext/wasm/api/sqlite3-worker1-promiser.js 0c7a9826dbf82a5ed4e4f7bf7816e825a52aff253afbf3350431f5773faf0e4b F ext/wasm/api/sqlite3-worker1.js 1e54ea3d540161bcfb2100368a2fc0cad871a207b8336afee1c445715851ec54 F ext/wasm/batch-runner.html 4deeed44fe41496dc6898d9fb17938ea3291f40f4bfb977e29d0cef96fbbe4c8 @@ -555,7 +555,7 @@ F ext/wasm/test-opfs-vfs.html 1f2d672f3f3fce810dfd48a8d56914aba22e45c6834e262555 F ext/wasm/test-opfs-vfs.js 44363db07b2a20e73b0eb1808de4400ca71b703af718d0fa6d962f15e73bf2ac F ext/wasm/tester1-worker.html d43f3c131d88f10d00aff3e328fed13c858d674ea2ff1ff90225506137f85aa9 F ext/wasm/tester1.c-pp.html d34bef3d48e5cbc1c7c06882ad240fec49bf88f5f65696cc2c72c416933aa406 -F ext/wasm/tester1.c-pp.js c5555f271bf10db2cda8550549178ec7b9a6f77236592f0a9c3132c46c4ca5cf +F ext/wasm/tester1.c-pp.js 88981420cd91fa134293fc15507b4c94923dab6b3d2e9fc9a4510fdb4f4c6368 F ext/wasm/tests/opfs/concurrency/index.html 86d8ac435074d1e7007b91105f4897f368c165e8cecb6a9aa3d81f5cf5dcbe70 F ext/wasm/tests/opfs/concurrency/test.js a98016113eaf71e81ddbf71655aa29b0fed9a8b79a3cdd3620d1658eb1cc9a5d F ext/wasm/tests/opfs/concurrency/worker.js 0a8c1a3e6ebb38aabbee24f122693f1fb29d599948915c76906681bb7da1d3d2 @@ -2067,8 +2067,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P ee47e9b83ca668b37dc1d8e519048a635693cf33d9967a2d81ff0824b7eea4ba -R 51ad3b31f5be17cf2dd5876e95922a5a +P ad0a8139b0b025f8e9d2eca0c303557ef10fdfab8c8b65afb08c510a804073d5 +R 63845e659c20277d26eb8fabe4c19b32 U stephan -Z 8e66df1e6b7cf683557d1817b0c14d69 +Z 931a2830ca8e3c84dca677991c17e36e # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 7067f450c..49b218e10 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -ad0a8139b0b025f8e9d2eca0c303557ef10fdfab8c8b65afb08c510a804073d5
\ No newline at end of file +073a2f1eb006230ae0995a5ea6c789407bcaa819ec15b5064c66d8973ed4671a
\ No newline at end of file |