Refactor the sqlite3_value-to-JS conversion from an internal detail to sqlite3.capi.sqlite3_value_to_js() for use with routines like sqlite3_module::xFilter().

FossilOrigin-Name: f6dbf280f99809a80c99337e4c22a86dea7a35ae41ae9a69144c4502385a0a1f
This commit is contained in:
stephan 2022-12-09 14:46:24 +00:00
parent edfbde52fd
commit 99f30f1cd4
6 changed files with 64 additions and 54 deletions

View File

@ -372,49 +372,19 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}/*__udfSetResult()*/;
const __udfConvertArgs = function(argc, pArgv){
let i, pVal, valType, arg;
let i;
const tgt = [];
for(i = 0; i < argc; ++i){
pVal = wasm.peekPtr(pArgv + (wasm.ptrSizeof * i));
/**
Curiously: despite ostensibly requiring 8-byte
alignment, the pArgv array is parcelled into chunks of
4 bytes (1 pointer each). The values those point to
have 8-byte alignment but the individual argv entries
do not.
*/
valType = capi.sqlite3_value_type(pVal);
switch(valType){
case capi.SQLITE_INTEGER:
if(wasm.bigIntEnabled){
arg = capi.sqlite3_value_int64(pVal);
if(util.bigIntFitsDouble(arg)) arg = Number(arg);
}
else arg = capi.sqlite3_value_double(pVal)/*yes, double, for larger integers*/;
break;
case capi.SQLITE_FLOAT:
arg = capi.sqlite3_value_double(pVal);
break;
case capi.SQLITE_TEXT:
arg = capi.sqlite3_value_text(pVal);
break;
case capi.SQLITE_BLOB:{
const n = capi.sqlite3_value_bytes(pVal);
const pBlob = capi.sqlite3_value_blob(pVal);
if(n && !pBlob) sqlite3.WasmAllocError.toss(
"Cannot allocate memory for blob argument of",n,"byte(s)"
);
arg = n ? wasm.heap8u().slice(pBlob, pBlob + Number(n)) : null;
break;
}
case capi.SQLITE_NULL:
arg = null; break;
default:
toss3("Unhandled sqlite3_value_type()",valType,
"is possibly indicative of incorrect",
"pointer size assumption.");
}
tgt.push(arg);
*/
tgt.push(capi.sqlite3_value_to_js(
wasm.peekPtr(pArgv + (wasm.ptrSizeof * i))
));
}
return tgt;
}/*__udfConvertArgs()*/;

View File

@ -1658,6 +1658,48 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap(
}
}.bind(Object.create(null));
/**
Given a (sqlite3_value*), this function attempts to convert it
to an equivalent JS value with as much fidelity as feasible and
return it. Throws if it cannot determine any sensible
conversion, but that would be indicative of a serious error.
*/
capi.sqlite3_value_to_js = function(pVal){
let arg;
const valType = capi.sqlite3_value_type(pVal);
switch(valType){
case capi.SQLITE_INTEGER:
if(wasm.bigIntEnabled){
arg = capi.sqlite3_value_int64(pVal);
if(util.bigIntFitsDouble(arg)) arg = Number(arg);
}
else arg = capi.sqlite3_value_double(pVal)/*yes, double, for larger integers*/;
break;
case capi.SQLITE_FLOAT:
arg = capi.sqlite3_value_double(pVal);
break;
case capi.SQLITE_TEXT:
arg = capi.sqlite3_value_text(pVal);
break;
case capi.SQLITE_BLOB:{
const n = capi.sqlite3_value_bytes(pVal);
const pBlob = capi.sqlite3_value_blob(pVal);
if(n && !pBlob) sqlite3.WasmAllocError.toss(
"Cannot allocate memory for blob argument of",n,"byte(s)"
);
arg = n ? wasm.heap8u().slice(pBlob, pBlob + Number(n)) : null;
break;
}
case capi.SQLITE_NULL:
arg = null; break;
default:
toss3("Unhandled sqlite3_value_type()",valType,
"is possibly indicative of incorrect",
"pointer size assumption.");
}
return arg;
};
/* The remainder of the API will be set up in later steps. */
const sqlite3 = {
WasmAllocError: WasmAllocError,

View File

@ -316,13 +316,11 @@ self.WhWasmUtilInstaller = function(target){
Throws if passed an invalid n.
Pedantic side note: the name "heap" is a bit of a misnomer. In an
Emscripten environment, the memory managed via the stack
allocation API is in the same Memory object as the heap (which
makes sense because otherwise arbitrary pointer X would be
ambiguous: is it in the heap or the stack?).
Pedantic side note: the name "heap" is a bit of a misnomer. In a
WASM environment, the stack and heap memory are all accessed via
the same view(s) of the memory.
*/
target.heapForSize = function(n,unsigned = false){
target.heapForSize = function(n,unsigned = true){
let ctor;
const c = (cache.memory && cache.heapSize === cache.memory.buffer.byteLength)
? cache : heapWrappers();

View File

@ -212,8 +212,8 @@
const renderFunc = function(name){
let lbl = name+'()';
const e = eLi(lbl, eFuncs);;
if(name.startsWith('sqlite3_js')
|| name.startsWith('sqlite3_wasm')){
if(name.indexOf('_js')>0
|| name.indexOf('_wasm')>0){
e.classList.add('func-wasm');
}
};

View File

@ -1,5 +1,5 @@
C Fix\stypo\sin\sthe\sSQLITE_DBCONFIG_LOOKASIDE\sdocumentation.
D 2022-12-09T13:49:44.278
C Refactor\sthe\ssqlite3_value-to-JS\sconversion\sfrom\san\sinternal\sdetail\sto\ssqlite3.capi.sqlite3_value_to_js()\sfor\suse\swith\sroutines\slike\ssqlite3_module::xFilter().
D 2022-12-09T14:46:24.289
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -503,9 +503,9 @@ 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 a3c92fa35c2de17100504f7377d643b7ecfe5a6fbcdc795a6d8ca4eb0cf873ad
F ext/wasm/api/sqlite3-api-glue.js ca68c522c31ff0e4fd6e97920f5426f6f8d9dce4bbeb847fe307d73a3b3bd0ae
F ext/wasm/api/sqlite3-api-oo1.js 6d10849609231ccd46fa11b1d3fbbe0f45d9fe84c66a0b054601036540844300
F ext/wasm/api/sqlite3-api-prologue.js 80e672ec8c38521515f15196f18f6b28704412cc2c0101ba55e2f8285e04ddb7
F ext/wasm/api/sqlite3-api-prologue.js 652c9282b8a8cb4b7a72fcb1c149d83228eb31385c5e6cc138b5e88bce345490
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
@ -521,7 +521,7 @@ F ext/wasm/c-pp.c 92285f7bce67ed7b7020b40fde8ed0982c442b63dc33df9dfd4b658d4a6c07
F ext/wasm/common/SqliteTestUtil.js d8bf97ecb0705a2299765c8fc9e11b1a5ac7f10988bbf375a6558b7ca287067b
F ext/wasm/common/emscripten.css 11bd104b6c0d597c67d40cc8ecc0a60dae2b965151e3b6a37fa5708bac3acd15
F ext/wasm/common/testing.css 0ff15602a3ab2bad8aef2c3bd120c7ee3fd1c2054ad2ace7e214187ae68d926f
F ext/wasm/common/whwasmutil.js ab8da0ba4133f44694f5c26687d954af09ba9e2cfa536596f641cd5ac147aebc
F ext/wasm/common/whwasmutil.js 11090ae276ed66150095717ab41c5229b679a66d6fd1b95d40653177d3945cf1
F ext/wasm/demo-123-worker.html a0b58d9caef098a626a1a1db567076fca4245e8d60ba94557ede8684350a81ed
F ext/wasm/demo-123.html 8c70a412ce386bd3796534257935eb1e3ea5c581e5d5aea0490b8232e570a508
F ext/wasm/demo-123.js ebae30756585bca655b4ab2553ec9236a87c23ad24fc8652115dcedb06d28df6
@ -541,7 +541,7 @@ F ext/wasm/index-dist.html c806b6005145b71d64240606e9c6e0bf56878ee8829c66fe7486c
F ext/wasm/index.html f151b7c7b5cfdc066567d556acd168e769efd4e982286dc5f849a5ee69ecd0ff
F ext/wasm/jaccwabyt/jaccwabyt.js 06f2ef1ad640c26c593def3d960336e9bb789819b920516480895c38ed5f58fa
F ext/wasm/jaccwabyt/jaccwabyt.md 37911f00db12cbcca73aa1ed72594430365f30aafae2fa9c886961de74e5e0eb
F ext/wasm/module-symbols.html 980680c8acfa3c8ae6a5aa223512d1b8e78040ced20f8ba2c382129bc73ec028
F ext/wasm/module-symbols.html 3517259495334d873b46918d14517f13d1eb190b42c3d11104141c046e28e163
F ext/wasm/scratchpad-wasmfs-main.html 20cf6f1a8f368e70d01e8c17200e3eaa90f1c8e1029186d836d14b83845fbe06
F ext/wasm/scratchpad-wasmfs-main.js 4c140457f4d6da9d646a49addd91edb6e9ad1643c6c48e3258b5bce24725dc18
F ext/wasm/speedtest1-wasmfs.html bc28eb29b69a73864b8d7aae428448f8b7e1de81d8bfb9bba99541322054dbd0
@ -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 073a2f1eb006230ae0995a5ea6c789407bcaa819ec15b5064c66d8973ed4671a
R 5eb2dffb531ff6773df8711da11fac86
U drh
Z af71aa267fea994673a56bef272d1237
P c6e7582aea4ebcc4563afb4367fded1e8a74f6ef522a569460023c340ca24b30
R 16765b1fd408728eaf5cae322d97e3fc
U stephan
Z 6fd28515eec5c302b8cea52b2f42f90b
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
c6e7582aea4ebcc4563afb4367fded1e8a74f6ef522a569460023c340ca24b30
f6dbf280f99809a80c99337e4c22a86dea7a35ae41ae9a69144c4502385a0a1f