Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Replace JS-side use of SQLITE_TRANSIENT with the new SQLITE_WASM_DEALLOC, reducing the amount allocation/copying required by sqlite3_bind_blob/text() and sqlite3_result_blob/text(). Remove the 'experimental' log message from the virtual table tests. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
ffe2999a91a7dec129a38afb675fe9e5 |
User & Date: | stephan 2022-12-24 15:28:45 |
Context
2022-12-25
| ||
10:22 | Simplify the signature for JS functions, as opposed to function pointers, passed to sqlite3_exec(), eliminating the superfluous initial two arguments. Update related tests to demonstrate both function-passing approaches. (check-in: e7cc70cd user: stephan tags: trunk) | |
2022-12-24
| ||
15:31 | Merge trunk into wasi-patches branch. (check-in: 52f40ab1 user: stephan tags: wasi-patches) | |
15:28 | Replace JS-side use of SQLITE_TRANSIENT with the new SQLITE_WASM_DEALLOC, reducing the amount allocation/copying required by sqlite3_bind_blob/text() and sqlite3_result_blob/text(). Remove the 'experimental' log message from the virtual table tests. (check-in: ffe2999a user: stephan tags: trunk) | |
14:16 | Extend oo1.Stmt.bind() to accept ArrayBuffer instances to bind as blobs. (check-in: f76bd301 user: stephan tags: trunk) | |
Changes
Changes to ext/wasm/api/sqlite3-api-glue.js.
︙ | ︙ | |||
19 20 21 22 23 24 25 26 27 28 29 30 31 32 | self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ 'use strict'; const toss = (...args)=>{throw new Error(args.join(' '))}; const toss3 = sqlite3.SQLite3Error.toss; const capi = sqlite3.capi, wasm = sqlite3.wasm, util = sqlite3.util; self.WhWasmUtilInstaller(wasm); delete self.WhWasmUtilInstaller; /** Signatures for the WASM-exported C-side functions. Each entry is an array with 2+ elements: [ "c-side name", "result type" (wasm.xWrap() syntax), | > > > > > > > > > > > > > > > > > > > > > > > > > > > | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ 'use strict'; const toss = (...args)=>{throw new Error(args.join(' '))}; const toss3 = sqlite3.SQLite3Error.toss; const capi = sqlite3.capi, wasm = sqlite3.wasm, util = sqlite3.util; self.WhWasmUtilInstaller(wasm); delete self.WhWasmUtilInstaller; { /** Find a mapping for SQLITE_WASM_DEALLOC, which the API guarantees is a WASM pointer to the same underlying function as wasm.dealloc() (noting that wasm.dealloc() is permitted to be a JS wrapper around the WASM function). There is unfortunately no O(1) algorithm for finding this pointer: we have to walk the WASM indirect function table to find it. However, experience indicates that that particular function is always very close to the front of the table (it's been entry #3 in all relevant tests). */ const dealloc = wasm.exports[sqlite3.config.deallocExportName]; const nFunc = wasm.functionTable().length; let i; for(i = 0; i < nFunc; ++i){ const e = wasm.functionEntry(i); if(dealloc === e){ capi.SQLITE_WASM_DEALLOC = i; break; } } if(dealloc !== wasm.functionEntry(capi.SQLITE_WASM_DEALLOC)){ toss("Internal error: cannot find function pointer for SQLITE_WASM_DEALLOC."); } } /** Signatures for the WASM-exported C-side functions. Each entry is an array with 2+ elements: [ "c-side name", "result type" (wasm.xWrap() syntax), |
︙ | ︙ | |||
870 871 872 873 874 875 876 | return __bindText(pStmt, iCol, text, nText, xDestroy); }else if(text instanceof ArrayBuffer){ text = new Uint8Array(text); }else if(Array.isArray(pMem)){ text = pMem.join(''); } let p, n; | | | | > > > | 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 | return __bindText(pStmt, iCol, text, nText, xDestroy); }else if(text instanceof ArrayBuffer){ text = new Uint8Array(text); }else if(Array.isArray(pMem)){ text = pMem.join(''); } let p, n; try{ if(util.isSQLableTypedArray(text)){ p = wasm.allocFromTypedArray(text); n = text.byteLength; }else if('string'===typeof text){ [p, n] = wasm.allocCString(text); }else{ return util.sqlite3_wasm_db_error( capi.sqlite3_db_handle(pStmt), capi.SQLITE_MISUSE, "Invalid 3rd argument type for sqlite3_bind_text()." ); } return __bindText(pStmt, iCol, p, n, capi.SQLITE_WASM_DEALLOC); }catch(e){ wasm.dealloc(p); return util.sqlite3_wasm_db_error( capi.sqlite3_db_handle(pStmt), e ); } }/*sqlite3_bind_text()*/; /** Documented in the capi object's initializer. */ capi.sqlite3_bind_blob = function f(pStmt, iCol, pMem, nMem, xDestroy){ if(f.length!==arguments.length){ return __dbArgcMismatch(capi.sqlite3_db_handle(pStmt), |
︙ | ︙ | |||
913 914 915 916 917 918 919 | [p, n] = wasm.allocCString(pMem); }else{ return util.sqlite3_wasm_db_error( capi.sqlite3_db_handle(pStmt), capi.SQLITE_MISUSE, "Invalid 3rd argument type for sqlite3_bind_blob()." ); } | | | > > > | 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 | [p, n] = wasm.allocCString(pMem); }else{ return util.sqlite3_wasm_db_error( capi.sqlite3_db_handle(pStmt), capi.SQLITE_MISUSE, "Invalid 3rd argument type for sqlite3_bind_blob()." ); } return __bindBlob(pStmt, iCol, p, n, capi.SQLITE_WASM_DEALLOC); }catch(e){ wasm.dealloc(p); return util.sqlite3_wasm_db_error( capi.sqlite3_db_handle(pStmt), e ); } }/*sqlite3_bind_blob()*/; }/*sqlite3_bind_text/blob()*/ {/* sqlite3_set_authorizer() */ const __ssa = wasm.xWrap("sqlite3_set_authorizer", 'int', [ |
︙ | ︙ |
Changes to ext/wasm/api/sqlite3-api-oo1.js.
︙ | ︙ | |||
1281 1282 1283 1284 1285 1286 1287 | /** Binds a single bound parameter value on the given stmt at the given index (numeric or named) using the given bindType (see the BindTypes enum) and value. Throws on error. Returns stmt on success. */ const bindOne = function f(stmt,ndx,bindType,val){ | | < < | | | < < < | 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 | /** Binds a single bound parameter value on the given stmt at the given index (numeric or named) using the given bindType (see the BindTypes enum) and value. Throws on error. Returns stmt on success. */ const bindOne = function f(stmt,ndx,bindType,val){ affirmUnlocked(affirmStmtOpen(stmt), 'bind()'); if(!f._){ f._tooBigInt = (v)=>toss3( "BigInt value is too big to store without precision loss:", v ); /* Reminder: when not in BigInt mode, it's impossible for JS to represent a number out of the range we can bind, so we have no range checking. */ f._ = { string: function(stmt, ndx, val, asBlob){ const [pStr, n] = wasm.allocCString(val, true); const f = asBlob ? capi.sqlite3_bind_blob : capi.sqlite3_bind_text; return f(stmt.pointer, ndx, pStr, n, capi.SQLITE_WASM_DEALLOC); } }; }/* static init */ affirmSupportedBindType(val); ndx = affirmParamIndex(stmt,ndx); let rc = 0; switch((null===val || undefined===val) ? BindTypes.null : bindType){ |
︙ | ︙ | |||
1350 1351 1352 1353 1354 1355 1356 | break; }else if(val instanceof ArrayBuffer){ val = new Uint8Array(val); }else if(!util.isBindableTypedArray(val)){ toss3("Binding a value as a blob requires", "that it be a string, Uint8Array, Int8Array, or ArrayBuffer."); } | < < | | | | < < < | 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 | break; }else if(val instanceof ArrayBuffer){ val = new Uint8Array(val); }else if(!util.isBindableTypedArray(val)){ toss3("Binding a value as a blob requires", "that it be a string, Uint8Array, Int8Array, or ArrayBuffer."); } const pBlob = wasm.alloc(val.byteLength || 1); wasm.heap8().set(val.byteLength ? val : [0], pBlob) rc = capi.sqlite3_bind_blob(stmt.pointer, ndx, pBlob, val.byteLength, capi.SQLITE_WASM_DEALLOC); break; } default: console.warn("Unsupported bind() argument type:",val); toss3("Unsupported bind() argument type: "+(typeof val)); } if(rc) DB.checkRc(stmt.db.pointer, rc); |
︙ | ︙ |
Changes to ext/wasm/api/sqlite3-api-prologue.js.
︙ | ︙ | |||
317 318 319 320 321 322 323 | const typedArrayPart = (aTypedArray, begin, end)=>{ return isSharedTypedArray(aTypedArray) ? aTypedArray.slice(begin, end) : aTypedArray.subarray(begin, end); }; /** | | | | | > > | < > > | | > | 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | const typedArrayPart = (aTypedArray, begin, end)=>{ return isSharedTypedArray(aTypedArray) ? aTypedArray.slice(begin, end) : aTypedArray.subarray(begin, end); }; /** Returns v if v appears to be one of our bind()-able TypedArray types: Uint8Array or Int8Array or ArrayBuffer. Support for TypedArrays with element sizes >1 is a potential TODO just waiting on a use case to justify them. Until then, their `buffer` property can be used to pass them as an ArrayBuffer. If it's not a bindable array type, a falsy value is returned. */ const isBindableTypedArray = (v)=>{ return v && (v instanceof Uint8Array || v instanceof Int8Array || v instanceof ArrayBuffer); }; /** Returns true if v appears to be one of the TypedArray types which is legal for holding SQL code (as opposed to binary blobs). Currently this is the same as isBindableTypedArray() but it seems likely that we'll eventually want to add Uint32Array and friends to the isBindableTypedArray() list but not to the isSQLableTypedArray() list. */ const isSQLableTypedArray = (v)=>{ return v && (v instanceof Uint8Array || v instanceof Int8Array || v instanceof ArrayBuffer); }; /** Returns true if isBindableTypedArray(v) does, else throws with a message that v is not a supported TypedArray value. */ const affirmBindableTypedArray = (v)=>{ return isBindableTypedArray(v) || toss3("Value is not of a supported TypedArray type."); |
︙ | ︙ | |||
435 436 437 438 439 440 441 | behavior for a negative 4th argument, but results are undefined if the given 4th argument value is greater than the byteLength of the input array. - If it's an ArrayBuffer, it gets wrapped in a Uint8Array and treated as that type. | | | | 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | behavior for a negative 4th argument, but results are undefined if the given 4th argument value is greater than the byteLength of the input array. - If it's an ArrayBuffer, it gets wrapped in a Uint8Array and treated as that type. In all of those cases, the final argument (destructor) is ignored and capi.SQLITE_WASM_DEALLOC is assumed. A 3rd argument of `null` is treated as if it were a WASM pointer of 0. If the 3rd argument is neither a WASM pointer nor one of the above-described types, capi.SQLITE_MISUSE is returned. |
︙ | ︙ | |||
473 474 475 476 477 478 479 | converted to a string. The 4th argument is ignored, replaced by the array's byteLength value. - If it's an ArrayBuffer, it gets wrapped in a Uint8Array and treated as that type. In each of those cases, the final argument (text destructor) is | | | 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | converted to a string. The 4th argument is ignored, replaced by the array's byteLength value. - If it's an ArrayBuffer, it gets wrapped in a Uint8Array and treated as that type. In each of those cases, the final argument (text destructor) is ignored and capi.SQLITE_WASM_DEALLOC is assumed. A 3rd argument of `null` is treated as if it were a WASM pointer of 0. If the 3rd argument is neither a WASM pointer nor one of the above-described types, capi.SQLITE_MISUSE is returned. |
︙ | ︙ | |||
1641 1642 1643 1644 1645 1646 1647 | }else{ /* Maintenance reminder: ''+e, rather than e.message, will prefix e.message with e.name, so it includes the exception's type name in the result. */; capi.sqlite3_result_error(pCtx, ''+e, -1); } }; | | | | 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 | }else{ /* Maintenance reminder: ''+e, rather than e.message, will prefix e.message with e.name, so it includes the exception's type name in the result. */; capi.sqlite3_result_error(pCtx, ''+e, -1); } }; /** This function passes its 2nd argument to one of the sqlite3_result_xyz() routines, depending on the type of that argument: - If (val instanceof Error), this function passes it to sqlite3_result_error_js(). - `null`: `sqlite3_result_null()` - `boolean`: `sqlite3_result_int()` with a value of 0 or 1. - `number`: `sqlite3_result_int()`, `sqlite3_result_int64()`, or `sqlite3_result_double()`, depending on the range of the number and whether or not int64 support is enabled. - `bigint`: similar to `number` but will trigger an error if the value is too big to store in an int64. - `string`: `sqlite3_result_text()` - Uint8Array or Int8Array or ArrayBuffer: `sqlite3_result_blob()` - `undefined`: is a no-op provided to simplify certain use cases. Anything else triggers `sqlite3_result_error()` with a description of the problem. The first argument to this function is a `(sqlite3_context*)`. Returns void. Does not throw. |
︙ | ︙ | |||
1708 1709 1710 1711 1712 1713 1714 | f = capi.sqlite3_result_int64; }else{ f = capi.sqlite3_result_double; } f(pCtx, val); break; } | | > | > | | 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 | f = capi.sqlite3_result_int64; }else{ f = capi.sqlite3_result_double; } f(pCtx, val); break; } case 'string': { const [p, n] = wasm.allocCString(val,true); capi.sqlite3_result_text(pCtx, p, n, capi.SQLITE_WASM_DEALLOC); break; } case 'object': if(null===val/*yes, typeof null === 'object'*/) { capi.sqlite3_result_null(pCtx); break; }else if(util.isBindableTypedArray(val)){ const pBlob = wasm.allocFromTypedArray(val); capi.sqlite3_result_blob( pCtx, pBlob, val.byteLength, capi.SQLITE_WASM_DEALLOC ); break; } // else fall through default: toss3("Don't not how to handle this UDF result value:",(typeof val), val); } |
︙ | ︙ |
Changes to ext/wasm/api/sqlite3-wasm.c.
︙ | ︙ | |||
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 | #if SQLITE_WASM_TESTS SQLITE_WASM_KEEP int sqlite3_wasm_test_intptr(int * p){ return *p = *p * 2; } SQLITE_WASM_KEEP int64_t sqlite3_wasm_test_int64_max(void){ return (int64_t)0x7fffffffffffffff; } SQLITE_WASM_KEEP | > > > > > | 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 | #if SQLITE_WASM_TESTS SQLITE_WASM_KEEP int sqlite3_wasm_test_intptr(int * p){ return *p = *p * 2; } SQLITE_WASM_KEEP void * sqlite3_wasm_test_voidptr(void * p){ return p; } SQLITE_WASM_KEEP int64_t sqlite3_wasm_test_int64_max(void){ return (int64_t)0x7fffffffffffffff; } SQLITE_WASM_KEEP |
︙ | ︙ |
Changes to ext/wasm/tester1.c-pp.js.
︙ | ︙ | |||
1983 1984 1985 1986 1987 1988 1989 | }/* jaccwabyt-specific tests */) //////////////////////////////////////////////////////////////////////// .t({ name: 'virtual table #1: eponymous w/ manual exception handling', predicate: ()=>!!capi.sqlite3_index_info, test: function(sqlite3){ | < | 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 | }/* jaccwabyt-specific tests */) //////////////////////////////////////////////////////////////////////// .t({ name: 'virtual table #1: eponymous w/ manual exception handling', predicate: ()=>!!capi.sqlite3_index_info, test: function(sqlite3){ const VT = sqlite3.vtab; const tmplCols = Object.assign(Object.create(null),{ A: 0, B: 1 }); /** The vtab demonstrated here is a JS-ification of ext/misc/templatevtab.c. |
︙ | ︙ | |||
2181 2182 2183 2184 2185 2186 2187 | })/*custom vtab #1*/ //////////////////////////////////////////////////////////////////////// .t({ name: 'virtual table #2: non-eponymous w/ automated exception wrapping', predicate: ()=>!!capi.sqlite3_index_info, test: function(sqlite3){ | < | 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 | })/*custom vtab #1*/ //////////////////////////////////////////////////////////////////////// .t({ name: 'virtual table #2: non-eponymous w/ automated exception wrapping', predicate: ()=>!!capi.sqlite3_index_info, test: function(sqlite3){ const VT = sqlite3.vtab; const tmplCols = Object.assign(Object.create(null),{ A: 0, B: 1 }); /** The vtab demonstrated here is a JS-ification of ext/misc/templatevtab.c. |
︙ | ︙ |