diff options
author | stephan <stephan@noemail.net> | 2022-12-09 14:46:24 +0000 |
---|---|---|
committer | stephan <stephan@noemail.net> | 2022-12-09 14:46:24 +0000 |
commit | 99f30f1cd485dd81b04de48ae08eed24f5bd0725 (patch) | |
tree | 40a9e1f8438c31fe4ec35c3b8e78dd54e4e7b4b0 /ext/wasm/api | |
parent | edfbde52fdf3a3d11667f0171556876029100898 (diff) | |
download | sqlite-99f30f1cd485dd81b04de48ae08eed24f5bd0725.tar.gz sqlite-99f30f1cd485dd81b04de48ae08eed24f5bd0725.zip |
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
Diffstat (limited to 'ext/wasm/api')
-rw-r--r-- | ext/wasm/api/sqlite3-api-glue.js | 40 | ||||
-rw-r--r-- | ext/wasm/api/sqlite3-api-prologue.js | 42 |
2 files changed, 47 insertions, 35 deletions
diff --git a/ext/wasm/api/sqlite3-api-glue.js b/ext/wasm/api/sqlite3-api-glue.js index 6d9d45cda..0fbca54b4 100644 --- a/ext/wasm/api/sqlite3-api-glue.js +++ b/ext/wasm/api/sqlite3-api-glue.js @@ -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()*/; diff --git a/ext/wasm/api/sqlite3-api-prologue.js b/ext/wasm/api/sqlite3-api-prologue.js index 136d050c9..170e89511 100644 --- a/ext/wasm/api/sqlite3-api-prologue.js +++ b/ext/wasm/api/sqlite3-api-prologue.js @@ -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, |