]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Refactor kvvfs JS bits to make use of [ea370b9b05f7ed7eaa]. At main-thread startup...
authorstephan <stephan@noemail.net>
Sun, 9 Oct 2022 13:26:15 +0000 (13:26 +0000)
committerstephan <stephan@noemail.net>
Sun, 9 Oct 2022 13:26:15 +0000 (13:26 +0000)
FossilOrigin-Name: a9047e020a097b2259bc9935b63ca1c538a3a7f1d050e15f0d0a08cfb84acc7c

ext/wasm/api/sqlite3-api-glue.js
ext/wasm/api/sqlite3-api-prologue.js
ext/wasm/api/sqlite3-wasm.c
manifest
manifest.uuid

index 2347a40c01c5cbca91d9c734b45bbeff5368e859..519d678c6224c675b2a61d83d4a27f0a2383c9f7 100644 (file)
@@ -597,6 +597,92 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
     }
   }/*end C constant imports*/
 
+  if( util.isMainWindow()
+      && 0!==capi.sqlite3_vfs_find("kvvfs") ){/* kvvfs-specific glue */
+    const kvvfsMethods = new capi.sqlite3_kvvfs_methods(
+      wasm.exports.sqlite3_wasm_kvvfs_methods()
+    );
+    delete capi.sqlite3_kvvfs_methods;
+
+    const kvvfsMakeKey = wasm.exports.sqlite3_wasm_kvvfsMakeKeyOnPstack,
+          pstack = wasm.pstack,
+          pAllocRaw = wasm.exports.sqlite3_wasm_pstack_alloc;
+
+    const kvvfsStorage = (zClass)=>
+          ((115/*=='s'*/===wasm.getMemValue(zClass))
+           ? sessionStorage : localStorage);
+    
+    const kvvfsImpls = {
+      xRead: (zClass, zKey, zBuf, nBuf)=>{
+        const stack = pstack.pointer,
+              astack = wasm.scopedAllocPush();
+        try {
+          const zXKey = kvvfsMakeKey(zClass,zKey);
+          if(!zXKey) return -3/*OOM*/;
+          const jKey = wasm.cstringToJs(zXKey);
+          const jV = kvvfsStorage(zClass).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
+                                  C-string's byte length. */;
+          if(nBuf<=0) return nV;
+          else if(1===nBuf){
+            wasm.setMemValue(zBuf, 0);
+            return nV;
+          }
+          const zV = wasm.scopedAllocCString(jV);
+          if(nBuf > nV + 1) nBuf = nV + 1;
+          wasm.heap8u().copyWithin(zBuf, zV, zV + nBuf - 1);
+          wasm.setMemValue(zBuf + nBuf - 1, 0);
+          return nBuf - 1;
+        }catch(e){
+          console.error("kvstorageRead()",e);
+          return -2;
+        }finally{
+          pstack.restore(stack);
+          wasm.scopedAllocPop(astack);
+        }
+      },
+      xWrite: (zClass, zKey, zData)=>{
+        const stack = pstack.pointer;
+        try {
+          const zXKey = kvvfsMakeKey(zClass,zKey);
+          if(!zXKey) return 1/*OOM*/;
+          const jKey = wasm.cstringToJs(zXKey);
+          kvvfsStorage(zClass).setItem(jKey, wasm.cstringToJs(zData));
+          return 0;
+        }catch(e){
+          console.error("kvstorageWrite()",e);
+          return capi.SQLITE_IOERR;
+        }finally{
+          pstack.restore(stack);
+        }
+      },
+      xDelete: (zClass, zKey)=>{
+        const stack = pstack.pointer;
+        try {
+          const zXKey = kvvfsMakeKey(zClass,zKey);
+          if(!zXKey) return 1/*OOM*/;
+          kvvfsStorage(zClass).removeItem(wasm.cstringToJs(zXKey));
+          return 0;
+        }catch(e){
+          console.error("kvstorageDelete()",e);
+          return capi.SQLITE_IOERR;
+        }finally{
+          pstack.restore(stack);
+        }
+      }
+    }/*kvvfsImpls*/;
+    for(let k of Object.keys(kvvfsImpls)){
+      kvvfsMethods[kvvfsMethods.memberKey(k)] =
+        wasm.installFunction(
+          kvvfsMethods.memberSignature(k),
+          kvvfsImpls[k]
+        );
+    }
+    console.warn('kvvfs stuff',kvvfsMethods);
+  }/*kvvfs*/
+
   sqlite3.version = Object.assign(Object.create(null),{
     library: sqlite3.capi.sqlite3_libversion(),
     sourceId: sqlite3.capi.sqlite3_sourceid()
index 5b80c439bbdfe7f72b781b4f3ba7cb86ab793a41..8e2dda1d74dce420b14ca83cb77c3865e3882db5 100644 (file)
@@ -1313,7 +1313,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap(
       // to resolve in order? We currently only have 1, so it
       // makes no difference.
       lip = lip.map((f)=>f(sqlite3).catch((e)=>{
-        console.error("An async sqlite3 initializer failed:",e);
+        console.error("Ignoring error: an async sqlite3 initializer failed:",e);
       }));
       //let p = lip.shift();
       //while(lip.length) p = p.then(lip.shift());
index 7ed58dbe08dd68da5fcc531f7778fc67802b6f89..cfeb99e922f30eabbb9c80c1df80eb14f75769b9 100644 (file)
@@ -13,6 +13,8 @@
 ** emcc -o sqlite3.wasm ... -I/path/to/sqlite3-c-and-h sqlite3-wasm.c
 */
 
+#define SQLITE_WASM
+
 /*
 ** Threading and file locking: JS is single-threaded. Each Worker
 ** thread is a separate instance of the JS engine so can never access
@@ -87,7 +89,7 @@
 #endif
 
 /*
-** WASM_KEEP is identical to EMSCRIPTEN_KEEPALIVE but is not
+** SQLITE_WASM_KEEP is identical to EMSCRIPTEN_KEEPALIVE but is not
 ** Emscripten-specific. It explicitly marks functions for export into
 ** the target wasm file without requiring explicit listing of those
 ** functions in Emscripten's -sEXPORTED_FUNCTIONS=... list (or
@@ -683,6 +685,14 @@ const char * sqlite3_wasm_enum_json(void){
     } _StructBinder;
 #undef CurrentStruct
 
+#define CurrentStruct sqlite3_kvvfs_methods
+    StructBinder {
+      M(xRead,"i(sspi)");
+      M(xWrite,"i(sss)");
+      M(xDelete,"i(ss)");
+      M(nKeySize,"i");
+    } _StructBinder;
+
   } out( "]"/*structs*/);
 
   out("}"/*top-level object*/);
@@ -820,6 +830,39 @@ int sqlite3_wasm_db_serialize( sqlite3* pDb, unsigned char **pOut, sqlite3_int64
   }
 }
 
+/*
+** This function is NOT part of the sqlite3 public API. It is strictly
+** for use by the sqlite project's own JS/WASM bindings.
+**
+** Allocates sqlite3KvvfsMethods.nKeySize bytes from
+** sqlite3_wasm_pstack_alloc() and returns 0 if that allocation fails,
+** else it passes that string to kvstorageMakeKey() and returns a
+** NUL-terminated pointer to that string. It is up to the caller to
+** use sqlite3_wasm_pstack_restore() to free the returned pointer.
+*/
+WASM_KEEP
+char * sqlite3_wasm_kvvfsMakeKeyOnPstack(const char *zClass,
+                                         const char *zKeyIn){
+  assert(sqlite3KvvfsMethods.nKeySize>24);
+  char *zKeyOut =
+    (char *)sqlite3_wasm_pstack_alloc(sqlite3KvvfsMethods.nKeySize);
+  if(zKeyOut){
+    kvstorageMakeKey(zClass, zKeyIn, zKeyOut);
+  }
+  return zKeyOut;
+}
+
+/*
+** This function is NOT part of the sqlite3 public API. It is strictly
+** for use by the sqlite project's own JS/WASM bindings.
+**
+** Returns the pointer to the singleton object which holds the kvvfs
+** I/O methods and associated state.
+*/
+WASM_KEEP
+sqlite3_kvvfs_methods * sqlite3_wasm_kvvfs_methods(void){
+  return &sqlite3KvvfsMethods;
+}
 
 #if defined(__EMSCRIPTEN__) && defined(SQLITE_WASM_WASMFS)
 #include <emscripten/wasmfs.h>
index fa53ea2b90a67b1aadc421282527d00c294b7202..b9dae4ebf9cda255f30f4fef59df7d1e440686bb 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Refactor\sos_kv.c\sso\sthat\sthe\skvvfs\sread/write/delete\smethods\scan\sbe\sswapped\sout\sat\sruntime\sby\sJS\simplementations.\sThis\seliminates\sthe\skvvfs\sdependency\son\sEmscripten.\sCheckin\spart\s1\sof\s2,\sto\saccount\sfor\scherrypicking.
-D 2022-10-09T13:19:27.791
+C Refactor\skvvfs\sJS\sbits\sto\smake\suse\sof\s[ea370b9b05f7ed7eaa].\sAt\smain-thread\sstartup,\sif\skvvfs\sis\savailable,\sreplace\sthe\skvvfs\sI/O\smethods\swith\sJS\simpls.\sCheckin\spart\s2\sof\s2,\sto\saccount\sfor\scherrypicking\s[ea370b9b05f7ed7eaa]\sinto\sthe\skv-vfs\sbranch.
+D 2022-10-09T13:26:15.854
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -485,13 +485,13 @@ F ext/wasm/api/post-js-footer.js b64319261d920211b8700004d08b956a6c285f3b0bba814
 F ext/wasm/api/post-js-header.js 2e5c886398013ba2af88028ecbced1e4b22dc96a86467f1ecc5ba9e64ef90a8b
 F ext/wasm/api/pre-js.js 2db711eb637991b383fc6b5c0f3df65ec48a7201e5730e304beba8de2d3f9b0b
 F ext/wasm/api/sqlite3-api-cleanup.js 5d22d1d3818ecacb23bfa223d5970cd0617d8cdbb48c8bc4bbd463f05b021a99
-F ext/wasm/api/sqlite3-api-glue.js a5a1bd620e2e2c26b6e843cf439548b35c5bb5ed21b24b89a412e0b0a8592c42
+F ext/wasm/api/sqlite3-api-glue.js 4e1403add52666f4cf36fc580ddb15b65bb8f9ccb446095f953aad6c9da2f324
 F ext/wasm/api/sqlite3-api-oo1.js ac1e08d36bdfb5aa0a2d75b7d4c892fd51819d34c932370c3282810672bcc086
 F ext/wasm/api/sqlite3-api-opfs.js 78fd272d6ec4fe6da93910b978f693fff63beed8e0e5e7dab42a25dbdfefca4b
-F ext/wasm/api/sqlite3-api-prologue.js 587e6aa373bd85e83bcc3fb82b3636b01aaf2caefa39679e02574e766560d711
+F ext/wasm/api/sqlite3-api-prologue.js 2c860a257fc554bc6958c08c8e268e7f3d69813fd9e359032636a55c8e6f4405
 F ext/wasm/api/sqlite3-api-worker1.js 7f4f46cb6b512a48572d7567233896e6a9c46570c44bdc3d13419730c7c221c8
 F ext/wasm/api/sqlite3-wasi.h 25356084cfe0d40458a902afb465df8c21fc4152c1d0a59b563a3fba59a068f9
-F ext/wasm/api/sqlite3-wasm.c 0c8cb242b80b8de27b74f05ed781f427958e04c9339c4720f30c3a7e78ee6242
+F ext/wasm/api/sqlite3-wasm.c ea456d398bda1c5f4b2c4a54512de7b9ca4513c2b0ab326b99a46fb51cef0b42
 F ext/wasm/batch-runner.html cf1a410c92bad50fcec2ddc71390b4e9df63a6ea1bef12a5163a66a0af4d78d9
 F ext/wasm/batch-runner.js 5bae81684728b6be157d1f92b39824153f0fd019345b39f2ab8930f7ee2a57d8
 F ext/wasm/common/SqliteTestUtil.js 647bf014bd30bdd870a7e9001e251d12fc1c9ec9ce176a1004b838a4b33c5c05
@@ -2030,8 +2030,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 92b500da70a3dc64e910c232a2cac7620b6609162c2a5058b269d3b83d763c02
-R 4791c9c6d2e81be0b277d92f5c368eb4
+P ea370b9b05f7ed7eaa154ba58019f6642217eabc18517e721567adf948d93980
+R 029b3fdb077b2976ef8970d4e1e5751b
 U stephan
-Z 01e42f1c0aeb881a7fdcbb6522393054
+Z 32c9ba8da7697e38b9e7f056e3e7c363
 # Remove this line to create a well-formed Fossil manifest.
index b931ddc8fbead6653e7025f7ff3dec337ed1e3dc..f8b61d4efe2caac17972d70d3c8c36ace4283ff0 100644 (file)
@@ -1 +1 @@
-ea370b9b05f7ed7eaa154ba58019f6642217eabc18517e721567adf948d93980
\ No newline at end of file
+a9047e020a097b2259bc9935b63ca1c538a3a7f1d050e15f0d0a08cfb84acc7c
\ No newline at end of file