]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Wasm speedtest1: replace some 'self' with 'globalThis' and expose the sqlite3 object...
authorstephan <stephan@noemail.net>
Wed, 26 Nov 2025 16:26:27 +0000 (16:26 +0000)
committerstephan <stephan@noemail.net>
Wed, 26 Nov 2025 16:26:27 +0000 (16:26 +0000)
FossilOrigin-Name: c18154bc9a7cf07d3902d74d27b4ca696929aae9e1abe031679bd5a42d759046

ext/wasm/speedtest1-worker.js
ext/wasm/speedtest1.html
manifest
manifest.uuid

index ba11fd163d03fe8bc474a600725c8ffce4005d63..9355ba93fac3b4080a6c81603116abd3a1a4c84f 100644 (file)
@@ -1,7 +1,7 @@
 'use strict';
 (function(){
   let speedtestJs = 'speedtest1.js';
-  const urlParams = new URL(self.location.href).searchParams;
+  const urlParams = new URL(globalThis.location.href).searchParams;
   if(urlParams.has('sqlite3.dir')){
     speedtestJs = urlParams.get('sqlite3.dir') + '/' + speedtestJs;
   }
@@ -14,9 +14,9 @@
   const wasmfsDir = function f(wasmUtil){
     if(undefined !== f._) return f._;
     const pdir = '/opfs';
-    if( !self.FileSystemHandle
-        || !self.FileSystemDirectoryHandle
-        || !self.FileSystemFileHandle){
+    if( !globalThis.FileSystemHandle
+        || !globalThis.FileSystemDirectoryHandle
+        || !globalThis.FileSystemFileHandle){
       return f._ = "";
     }
     try{
@@ -92,7 +92,7 @@
     }
   };
 
-  self.onmessage = function(msg){
+  globalThis.onmessage = function(msg){
     msg = msg.data;
     switch(msg.type){
         case 'run':
     setStatus: (text)=>mPost('load-status',text)
   };
   log("Initializing speedtest1 module...");
-  self.sqlite3InitModule(EmscriptenModule).then(async (sqlite3)=>{
+  globalThis.sqlite3InitModule.__isUnderTest = true;
+  globalThis.sqlite3InitModule(EmscriptenModule).then(async (sqlite3)=>{
     const S = globalThis.S = App.sqlite3 = sqlite3;
     log("Loaded speedtest1 module. Setting up...");
     App.pDir = wasmfsDir(S.wasm);
index 73ea724e1106f59c465d12319a8e933611293761..22077ba4aa674fa9c3aeb97dc92de3c820262540 100644 (file)
        mounted, else it returns an empty string.
     */
     const wasmfsDir = function f(wasmUtil){
-        if(undefined !== f._) return f._;
-        const pdir = '/persistent';
-        if( !self.FileSystemHandle
-            || !self.FileSystemDirectoryHandle
-            || !self.FileSystemFileHandle){
-            return f._ = "";
-        }
-        try{
-            if(0===wasmUtil.xCallWrapped(
-                'sqlite3_wasm_init_wasmfs', 'i32', ['string'], pdir
-            )){
-                return f._ = pdir;
-            }else{
-                return f._ = "";
-            }
-        }catch(e){
-            // sqlite3_wasm_init_wasmfs() is not available
-            return f._ = "";
+      if(undefined !== f._) return f._;
+      const pdir = '/persistent';
+      if( !self.FileSystemHandle
+          || !self.FileSystemDirectoryHandle
+          || !self.FileSystemFileHandle){
+        return f._ = "";
+      }
+      try{
+        if(0===wasmUtil.xCallWrapped(
+          'sqlite3_wasm_init_wasmfs', 'i32', ['string'], pdir
+        )){
+          return f._ = pdir;
+        }else{
+          return f._ = "";
         }
+      }catch(e){
+        // sqlite3_wasm_init_wasmfs() is not available
+        return f._ = "";
+      }
     };
     wasmfsDir._ = undefined;
 
-      const eOut = document.querySelector('#test-output');
-      const log2 = function(cssClass,...args){
-        const ln = document.createElement('div');
-        if(cssClass) ln.classList.add(cssClass);
-        ln.append(document.createTextNode(args.join(' ')));
-        eOut.append(ln);
-        //this.e.output.lastElementChild.scrollIntoViewIfNeeded();
-      };
-      const logList = [];
-      const dumpLogList = function(){
-        logList.forEach((v)=>log2('',v));
-        logList.length = 0;
-      };
-      /* we cannot update DOM while speedtest is running unless we run
-         speedtest in a worker thread. */;
-      const log = (...args)=>{
-        console.log(...args);
-        logList.push(args.join(' '));
-      };
-      const logErr = function(...args){
-        console.error(...args);
-        logList.push('ERROR: '+args.join(' '));
-      };
+    const eOut = document.querySelector('#test-output');
+    const log2 = function(cssClass,...args){
+      const ln = document.createElement('div');
+      if(cssClass) ln.classList.add(cssClass);
+      ln.append(document.createTextNode(args.join(' ')));
+      eOut.append(ln);
+      //this.e.output.lastElementChild.scrollIntoViewIfNeeded();
+    };
+    const logList = [];
+    const dumpLogList = function(){
+      logList.forEach((v)=>log2('',v));
+      logList.length = 0;
+    };
+    /* we cannot update DOM while speedtest is running unless we run
+       speedtest in a worker thread. */;
+    const log = (...args)=>{
+      console.log(...args);
+      logList.push(args.join(' '));
+    };
+    const logErr = function(...args){
+      console.error(...args);
+      logList.push('ERROR: '+args.join(' '));
+    };
+
+    const runTests = function(sqlite3){
+      globalThis.S = sqlite3;
+      const capi = sqlite3.capi, wasm = sqlite3.wasm;
+      //console.debug('sqlite3 (global S) =',sqlite3);
+      const pDir = wasmfsDir(wasm);
+      if(pDir){
+        console.warn("wasmfs storage:",pDir);
+      }
+      const scope = wasm.scopedAllocPush();
+      let dbFile = pDir+"/speedtest1.db";
+      const urlParams = new URL(self.location.href).searchParams;
+      const argv = ["speedtest1"];
+      if(urlParams.has('flags')){
+        argv.push(...(urlParams.get('flags').split(',')));
+      }
 
-      const runTests = function(sqlite3){
-        const capi = sqlite3.capi, wasm = sqlite3.wasm;
-        //console.debug('sqlite3 =',sqlite3);
-        const pDir = wasmfsDir(wasm);
-        if(pDir){
-          console.warn("Persistent storage:",pDir);
+      let forceSize = 0;
+      let vfs, pVfs = 0;
+      if(urlParams.has('vfs')){
+        vfs = urlParams.get('vfs');
+        pVfs = capi.sqlite3_vfs_find(vfs);
+        if(!pVfs){
+          log2('error',"Unknown VFS:",vfs);
+          return;
         }
-        const scope = wasm.scopedAllocPush();
-        let dbFile = pDir+"/speedtest1.db";
-        const urlParams = new URL(self.location.href).searchParams;
-        const argv = ["speedtest1"];
-        if(urlParams.has('flags')){
-          argv.push(...(urlParams.get('flags').split(',')));
+        argv.push("--vfs", vfs);
+        log2('',"Using VFS:",vfs);
+        if('kvvfs' === vfs){
+          //forceSize = 10
+          /* --size > 2 is too big for local/session storage
+             as of mid-2025, but kvvfs v2 (late 2025)
+             lets us use transient storage. */;
+          dbFile = 'transient';
+          log2('warning',"kvvfs VFS: forcing --size",forceSize,
+               "and filename '"+dbFile+"'.");
+          capi.sqlite3_js_kvvfs_clear(dbFile);
         }
-
-        let forceSize = 0;
-        let vfs, pVfs = 0;
-        if(urlParams.has('vfs')){
-          vfs = urlParams.get('vfs');
-          pVfs = capi.sqlite3_vfs_find(vfs);
-          if(!pVfs){
-            log2('error',"Unknown VFS:",vfs);
-            return;
-          }
-          argv.push("--vfs", vfs);
-          log2('',"Using VFS:",vfs);
-          if('kvvfs' === vfs){
-            //forceSize = 10
-            /* --size > 2 is too big for local/session storage
-               as of mid-2025, but kvvfs v2 (late 2025)
-               lets us use transient storage. */;
-            dbFile = 'transient';
-            log2('warning',"kvvfs VFS: forcing --size",forceSize,
-                 "and filename '"+dbFile+"'.");
-            capi.sqlite3_js_kvvfs_clear(dbFile);
-          }
-        }
-        if(forceSize){
-          argv.push('--size',forceSize);
-        }else{
-          [
-            'size'
-          ].forEach(function(k){
-            const v = urlParams.get(k);
-            if(v) argv.push('--'+k, urlParams[k]);
-          });
+      }
+      if(forceSize){
+        argv.push('--size',forceSize);
+      }else{
+        [
+          'size'
+        ].forEach(function(k){
+          const v = urlParams.get(k);
+          if(v) argv.push('--'+k, urlParams[k]);
+        });
+      }
+      argv.push(
+        "--singlethread",
+        //"--nomutex",
+        //"--nosync",
+        //"--memdb", // note that memdb trumps the filename arg
+        "--nomemstat"
+      );
+      argv.push("--big-transactions"/*important for tests 410 and 510!*/,
+                dbFile);
+      console.log("argv =",argv);
+      // These log messages are not emitted to the UI until after main() returns. Fixing that
+      // requires moving the main() call and related cleanup into a timeout handler.
+      if(pDir) wasm.sqlite3_wasm_vfs_unlink(pVfs,dbFile);
+      log2('',"WASM heap size:",sqlite3.wasm.heap8().byteLength,"bytes");
+      log2('',"WASM pointer size:",sqlite3.wasm.ptr.size);
+      log2('',"Starting native app:\n ",argv.join(' '));
+      log2('',"This will take a while and the browser might warn about the runaway JS.",
+           "Give it time...");
+      logList.length = 0;
+      setTimeout(function(){
+        wasm.xCall('wasm_main', argv.length,
+                   wasm.scopedAllocMainArgv(argv));
+        wasm.scopedAllocPop(scope);
+        if('kvvfs'===vfs){
+          logList.unshift("KVVFS "+dbFile+" size = "+
+                          capi.sqlite3_js_kvvfs_size(dbFile));
         }
-        argv.push(
-          "--singlethread",
-          //"--nomutex",
-          //"--nosync",
-          //"--memdb", // note that memdb trumps the filename arg
-          "--nomemstat"
-        );
-        argv.push("--big-transactions"/*important for tests 410 and 510!*/,
-                  dbFile);
-        console.log("argv =",argv);
-        // These log messages are not emitted to the UI until after main() returns. Fixing that
-        // requires moving the main() call and related cleanup into a timeout handler.
         if(pDir) wasm.sqlite3_wasm_vfs_unlink(pVfs,dbFile);
+        logList.unshift("Done running native main(). Output:");
+        dumpLogList();
         log2('',"WASM heap size:",sqlite3.wasm.heap8().byteLength,"bytes");
-        log2('',"WASM pointer size:",sqlite3.wasm.ptr.size);
-        log2('',"Starting native app:\n ",argv.join(' '));
-        log2('',"This will take a while and the browser might warn about the runaway JS.",
-             "Give it time...");
-        logList.length = 0;
-        setTimeout(function(){
-          wasm.xCall('wasm_main', argv.length,
-                     wasm.scopedAllocMainArgv(argv));
-          wasm.scopedAllocPop(scope);
-          if('kvvfs'===vfs){
-            logList.unshift("KVVFS "+dbFile+" size = "+
-                            capi.sqlite3_js_kvvfs_size(dbFile));
-          }
-          if(pDir) wasm.sqlite3_wasm_vfs_unlink(pVfs,dbFile);
-          logList.unshift("Done running native main(). Output:");
-          dumpLogList();
-          log2('',"WASM heap size:",sqlite3.wasm.heap8().byteLength,"bytes");
-        }, 50);
-      }/*runTests()*/;
+      }, 50);
+    }/*runTests()*/;
 
     self.sqlite3TestModule.print = log;
     self.sqlite3TestModule.printErr = logErr;
+    sqlite3InitModule.__isUnderTest = true;
     sqlite3InitModule(self.sqlite3TestModule).then(runTests);
 })();</script>
 </body>
index 5fb4dd91a394959c3ceb3e710a468762517e7383..75aae11297bc1c66edfdf6d35337941e2312181d 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Minor\soptimization\sin\skvvfs\sevent\snotification.
-D 2025-11-26T14:22:51.188
+C Wasm\sspeedtest1:\sreplace\ssome\s'self'\swith\s'globalThis'\sand\sexpose\sthe\ssqlite3\sobject\sto\sthe\sglobal\sscope\sfor\sinspection\svia\sthe\sdev\sconsole\s(for\sinspecting\sresulting\skvvfs\sstorage\sobjects).
+D 2025-11-26T16:26:27.148
 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
@@ -638,8 +638,8 @@ F ext/wasm/scratchpad-wasmfs.mjs 66034b9256b218de59248aad796760a1584c1dd84223150
 F ext/wasm/speedtest1-wasmfs.html 0e9d335a9b5b5fafe6e1bc8dc0f0ca7e22e6eb916682a2d7c36218bb7d67379d
 F ext/wasm/speedtest1-wasmfs.mjs 60dd5842f6d2a70a6d0bef12633a11491bde6984aff75a37c2040980d8cbf36a
 F ext/wasm/speedtest1-worker.html 068d4190f304fa1c34e6501a1b3a4c32fe8d8dac93c2d0f53d667a1cb386eedc
-F ext/wasm/speedtest1-worker.js 958a2d3c710bf8e82567277f656193a0248216db99a3c2c86966124b84309efb
-F ext/wasm/speedtest1.html 5a913c7355dbaa72178484258d0b62b66d0d8134bcb496da90423c2402f462d4
+F ext/wasm/speedtest1-worker.js 8acad67bfd6aeeb799bd5ae007ea32af85a082a287d8877c5a10adf4bd7efd89
+F ext/wasm/speedtest1.html 856ddb26af6f21c228614614ccff73abc1f8bd877447c1c8da15cfabba5615c2
 F ext/wasm/split-speedtest1-script.sh a3e271938d4d14ee49105eb05567c6a69ba4c1f1293583ad5af0cd3a3779e205 x
 F ext/wasm/sql/000-mandelbrot.sql 775337a4b80938ac8146aedf88808282f04d02d983d82675bd63d9c2d97a15f0
 F ext/wasm/sql/001-sudoku.sql 35b7cb7239ba5d5f193bc05ec379bcf66891bce6f2a5b3879f2f78d0917299b5
@@ -2178,8 +2178,8 @@ F tool/version-info.c 33d0390ef484b3b1cb685d59362be891ea162123cea181cb8e6d2cf6dd
 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7
 F tool/warnings.sh d924598cf2f55a4ecbc2aeb055c10bd5f48114793e7ba25f9585435da29e7e98
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P 43173267849f7f4b77a9ee15ab586e9deb0e290071149857fe18421fb948a715
-R 4399e25b2c0c760a62be55eff75ae397
+P fd85b96b12d164f7700624dba478d35e886b23f5c31c853715f783639ea95f23
+R b940057b3e5baaba9e06e05c5ba016ac
 U stephan
-Z b2881af474b600509e635a0dd3ee7414
+Z aef30e10e6a9b3f34615b924cf02cd1b
 # Remove this line to create a well-formed Fossil manifest.
index 6b1be828a53998e84f87a8ff92cceee1a2b30cef..bb710af7c531c4bb8730cedcda5088303a167142 100644 (file)
@@ -1 +1 @@
-fd85b96b12d164f7700624dba478d35e886b23f5c31c853715f783639ea95f23
+c18154bc9a7cf07d3902d74d27b4ca696929aae9e1abe031679bd5a42d759046