1 import sqlite3InitModule from './jswasm/speedtest1-wasmfs.mjs';
2 const wMsg = (type,...args)=>{
3 postMessage({type, args});
5 wMsg('log',"speedtest1-wasmfs starting...");
7 If this environment contains OPFS, this function initializes it and
8 returns the name of the dir on which OPFS is mounted, else it returns
11 const wasmfsDir = function f(wasmUtil,dirName="/opfs"){
12 if(undefined !== f._) return f._;
13 if( !self.FileSystemHandle
14 || !self.FileSystemDirectoryHandle
15 || !self.FileSystemFileHandle){
19 if(0===wasmUtil.xCallWrapped(
20 'sqlite3_wasm_init_wasmfs', 'i32', ['string'], dirName
27 // sqlite3_wasm_init_wasmfs() is not available
31 wasmfsDir._ = undefined;
33 const log = (...args)=>wMsg('log',...args);
34 const logErr = (...args)=>wMsg('logErr',...args);
36 const runTests = function(sqlite3){
37 console.log("Module inited.",sqlite3);
38 const wasm = sqlite3.wasm;
39 const __unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["*","string"]);
40 const unlink = (fn)=>__unlink(0,fn);
41 const pDir = wasmfsDir(wasm);
42 if(pDir) log("Persistent storage:",pDir);
44 logErr("Expecting persistent storage in this build.");
47 const scope = wasm.scopedAllocPush();
48 const dbFile = pDir+"/speedtest1.db";
49 const urlParams = new URL(self.location.href).searchParams;
50 const argv = ["speedtest1"];
51 if(urlParams.has('flags')){
52 argv.push(...(urlParams.get('flags').split(',')));
53 let i = argv.indexOf('--vfs');
54 if(i>=0) argv.splice(i,2);
65 if(argv.indexOf('--memdb')>=0){
66 logErr("WARNING: --memdb flag trumps db filename.");
68 argv.push("--big-transactions"/*important for tests 410 and 510!*/,
71 // These log messages are not emitted to the UI until after main() returns. Fixing that
72 // requires moving the main() call and related cleanup into a timeout handler.
73 if(pDir) unlink(dbFile);
74 log("Starting native app:\n ",argv.join(' '));
75 log("This will take a while and the browser might warn about the runaway JS.",
77 setTimeout(function(){
78 if(pDir) unlink(dbFile);
79 wasm.xCall('wasm_main', argv.length,
80 wasm.scopedAllocMainArgv(argv));
81 wasm.scopedAllocPop(scope);
82 if(pDir) unlink(dbFile);
83 log("Done running native main()");