Fix to the previous checkin: The colUsed parameter should have high-order bit
[sqlite.git] / ext / wasm / speedtest1-wasmfs.mjs
blob2d5ae322aa403af4eb793b4b22c694a36cc6e619
1 import sqlite3InitModule from './jswasm/speedtest1-wasmfs.mjs';
2 const wMsg = (type,...args)=>{
3   postMessage({type, args});
4 };
5 wMsg('log',"speedtest1-wasmfs starting...");
6 /**
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
9    an empty string.
11 const wasmfsDir = function f(wasmUtil,dirName="/opfs"){
12   if(undefined !== f._) return f._;
13   if( !self.FileSystemHandle
14       || !self.FileSystemDirectoryHandle
15       || !self.FileSystemFileHandle){
16     return f._ = "";
17   }
18   try{
19     if(0===wasmUtil.xCallWrapped(
20       'sqlite3_wasm_init_wasmfs', 'i32', ['string'], dirName
21     )){
22       return f._ = dirName;
23     }else{
24       return f._ = "";
25     }
26   }catch(e){
27     // sqlite3_wasm_init_wasmfs() is not available
28     return f._ = "";
29   }
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);
43   else{
44     logErr("Expecting persistent storage in this build.");
45     return;
46   }
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);
55   }else{
56     argv.push(
57       "--singlethread",
58       "--nomutex",
59       //"--nosync",
60       "--nomemstat",
61       "--size", "10"
62     );
63   }
65   if(argv.indexOf('--memdb')>=0){
66     logErr("WARNING: --memdb flag trumps db filename.");
67   }
68   argv.push("--big-transactions"/*important for tests 410 and 510!*/,
69             dbFile);
70   //log("argv =",argv);
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.",
76       "Give it time...");
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()");
84   }, 25);
85 }/*runTests()*/;
87 sqlite3InitModule({
88   print: log,
89   printErr: logErr
90 }).then(runTests);