diff --git a/ext/wasm/api/sqlite3-wasm.c b/ext/wasm/api/sqlite3-wasm.c index 6cda681a70..8e382e7f53 100644 --- a/ext/wasm/api/sqlite3-wasm.c +++ b/ext/wasm/api/sqlite3-wasm.c @@ -503,7 +503,7 @@ static void kvstorageMakeKey( ** An internal level of indirection for accessing the static ** kvstorageMakeKey() from EM_JS()-generated functions. This must be ** made available for export via Emscripten but is not intended to be -** used from client code. If called with a NULL zClass it is a no-op. +** used from client code. If called with a NULL zKeyOut it is a no-op. ** It returns KVSTORAGE_KEY_SZ, so JS code (which cannot see that ** constant) may call it with NULL arguments to get the size of the ** allocation they'll need for a kvvfs key. @@ -514,7 +514,7 @@ static void kvstorageMakeKey( int sqlite3_wasm__kvvfsMakeKey(const char *zClass, const char *zKeyIn, char *zKeyOut){ - if(zClass) kvstorageMakeKey(zClass, zKeyIn, zKeyOut); + if(zKeyOut) kvstorageMakeKey(zClass, zKeyIn, zKeyOut); return KVSTORAGE_KEY_SZ; } @@ -559,14 +559,33 @@ EM_JS(void, kvstorageMakeKeyJS, }); #endif +/* +** Internal helper for kvstorageWrite/Read/Delete() which creates a +** storage key for the given zClass/zKeyIn combination. Returns a +** pointer to the key: a C string allocated on the WASM stack, or 0 if +** allocation fails. It is up to the caller to save/restore the stack +** before/after this operation. +*/ +EM_JS(const char *, kvstorageMakeKeyOnJSStack, + (const char *zClass, const char *zKeyIn),{ + if( 0==zClass || 0==zKeyIn) return 0; + const zXKey = stackAlloc(_sqlite3_wasm__kvvfsMakeKey(0,0,0)); + if(zXKey) _sqlite3_wasm__kvvfsMakeKey(zClass, zKeyIn, zXKey); + return zXKey; +}); + +/* +** JS impl of kvstorageWrite(). Main docs are in the C impl. This impl +** writes zData to the global sessionStorage (if zClass starts with +** 's') or localStorage, using a storage key derived from zClass and +** zKey. +*/ EM_JS(int, kvstorageWrite, (const char *zClass, const char *zKey, const char *zData),{ const stack = stackSave(); try { - //const zXKey = stackAlloc(kvstorageMakeKeyJS()); - //kvstorageMakeKeyJS(zClass, zKey, zXKey); - const zXKey = stackAlloc(_sqlite3_wasm__kvvfsMakeKey(0,0,0)); - _sqlite3_wasm__kvvfsMakeKey(zClass, zKey, zXKey); + const zXKey = kvstorageMakeKeyOnJSStack(zClass,zKey); + if(!zXKey) return 1/*OOM*/; const jKey = UTF8ToString(zXKey); /** We could simplify this function and eliminate the @@ -586,11 +605,18 @@ EM_JS(int, kvstorageWrite, return 0; }); +/* +** JS impl of kvstorageDelete(). Main docs are in the C impl. This +** impl generates a key derived from zClass and zKey, and removes the +** matching entry (if any) from global sessionStorage (if zClass +** starts with 's') or localStorage. +*/ EM_JS(int, kvstorageDelete, (const char *zClass, const char *zKey),{ const stack = stackSave(); try { - const zXKey = stackAlloc(_sqlite3_wasm__kvvfsMakeKey(0,0,0)); + const zXKey = kvstorageMakeKeyOnJSStack(zClass,zKey); + if(!zXKey) return 1/*OOM*/; _sqlite3_wasm__kvvfsMakeKey(zClass, zKey, zXKey); const jKey = UTF8ToString(zXKey); ((115/*=='s'*/===getValue(zClass)) @@ -604,16 +630,21 @@ EM_JS(int, kvstorageDelete, return 0; }); +/* +** JS impl of kvstorageRead(). Main docs are in the C impl. This impl +** reads its data from the global sessionStorage (if zClass starts +** with 's') or localStorage, using a storage key derived from zClass +** and zKey. +*/ EM_JS(int, kvstorageRead, (const char *zClass, const char *zKey, char *zBuf, int nBuf),{ const stack = stackSave(); try { - const zXKey = stackAlloc(_sqlite3_wasm__kvvfsMakeKey(0,0,0)); - _sqlite3_wasm__kvvfsMakeKey(zClass, zKey, zXKey); + const zXKey = kvstorageMakeKeyOnJSStack(zClass,zKey); + if(!zXKey) return 1/*OOM*/; const jKey = UTF8ToString(zXKey); - const storage = (115/*=='s'*/===getValue(zClass)) - ? sessionStorage : localStorage; - const jV = storage.getItem(jKey); + const jV = ((115/*=='s'*/===getValue(zClass)) + ? sessionStorage : localStorage).getItem(jKey); if(!jV) return -1; const nV = jV.length /* Note that we are relying 100% on v being ASCII so that jV.length is equal to the @@ -661,7 +692,7 @@ int sqlite3_wasm__emjs_test(int whichOp){ kvstorageDelete(zClass, zKey); break; default: - //kvstorageMakeKeyJS(0,0,0) /* force Emscripten to include this */; + kvstorageMakeKeyOnJSStack(0,0) /* force Emscripten to include this */; break; } return rc; diff --git a/manifest b/manifest index 64d9baf481..fc069fb45b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sEM_JS()\simpl\sfor\skvstorageRead(). -D 2022-09-11T04:49:29.771 +C Minor\scleanups\sand\sdocs\sfor\sthe\sEM_JS()\simpls\sof\sthe\skvvfs\sread/write/delete\sops. +D 2022-09-11T05:38:39.331 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -488,7 +488,7 @@ F ext/wasm/api/sqlite3-api-opfs.js 011799db398157cbd254264b6ebae00d7234b93d0e9e8 F ext/wasm/api/sqlite3-api-prologue.js 88e58145b0c71b00cd523b656016193d6ce8cf22c7ece8225838625f9187e6d1 F ext/wasm/api/sqlite3-api-worker1.js 73579555563b789785ae83724014eaf31811073aad9be6596c8336ffb51edd71 F ext/wasm/api/sqlite3-wasi.h 25356084cfe0d40458a902afb465df8c21fc4152c1d0a59b563a3fba59a068f9 -F ext/wasm/api/sqlite3-wasm.c af771ebd94f4c75bb99823d84667a47ecee1425a05b73f13a7c7ff2b368e439d +F ext/wasm/api/sqlite3-wasm.c ca61244ff11992d82f0abe30563f6a46b5bc5ba6ae6e3fe0de813a539d032638 F ext/wasm/batch-runner.html 23209ade7981acce7ecd79d6eff9f4c5a4e8b14ae867ac27cd89b230be640fa6 F ext/wasm/batch-runner.js a727cbbffe63fd17fb5a590dc679f0b13bd51880e8f84b461d7df246417689e8 F ext/wasm/common/SqliteTestUtil.js 529161a624265ba84271a52db58da022649832fa1c71309fb1e02cc037327a2b @@ -2019,8 +2019,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 a4bd96f5348e607d0fcb627b751f9d86a188173a4b3bfb2c95f92913a78bd31e -R 310c3e2e3269e2d647346b038874eb9d +P 06610314fcf644f323c2f7ae11d7f4349b195e66d0ebbee590438dd99d97eb96 +R 57acbb8d05cac7106145ba92b2f60bd7 U stephan -Z 24c87f794d1a08fdf28cff48f949d74b +Z a67cf75775135685688642fe9cc532b8 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index e21a71990d..f196ec7f1b 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -06610314fcf644f323c2f7ae11d7f4349b195e66d0ebbee590438dd99d97eb96 \ No newline at end of file +53b7572e441be4b4b29b4228d0f95b24457d7faaf6c0b568ed5c970a55e21ffb \ No newline at end of file