Snapshot of upstream SQLite 3.40.1
[sqlcipher.git] / ext / wasm / speedtest1-worker.js
blobc61cab9190b803268fd2abbc3766a3771232b6e6
1 'use strict';
2 (function(){
3 let speedtestJs = 'speedtest1.js';
4 const urlParams = new URL(self.location.href).searchParams;
5 if(urlParams.has('sqlite3.dir')){
6 speedtestJs = urlParams.get('sqlite3.dir') + '/' + speedtestJs;
8 importScripts('common/whwasmutil.js', speedtestJs);
9 /**
10 If this environment contains OPFS, this function initializes it and
11 returns the name of the dir on which OPFS is mounted, else it returns
12 an empty string.
14 const wasmfsDir = function f(wasmUtil){
15 if(undefined !== f._) return f._;
16 const pdir = '/opfs';
17 if( !self.FileSystemHandle
18 || !self.FileSystemDirectoryHandle
19 || !self.FileSystemFileHandle){
20 return f._ = "";
22 try{
23 if(0===wasmUtil.xCallWrapped(
24 'sqlite3_wasm_init_wasmfs', 'i32', ['string'], pdir
25 )){
26 return f._ = pdir;
27 }else{
28 return f._ = "";
30 }catch(e){
31 // sqlite3_wasm_init_wasmfs() is not available
32 return f._ = "";
35 wasmfsDir._ = undefined;
37 const mPost = function(msgType,payload){
38 postMessage({type: msgType, data: payload});
41 const App = Object.create(null);
42 App.logBuffer = [];
43 const logMsg = (type,msgArgs)=>{
44 const msg = msgArgs.join(' ');
45 App.logBuffer.push(msg);
46 mPost(type,msg);
48 const log = (...args)=>logMsg('stdout',args);
49 const logErr = (...args)=>logMsg('stderr',args);
51 const runSpeedtest = function(cliFlagsArray){
52 const scope = App.wasm.scopedAllocPush();
53 const dbFile = App.pDir+"/speedtest1.sqlite3";
54 try{
55 const argv = [
56 "speedtest1.wasm", ...cliFlagsArray, dbFile
58 App.logBuffer.length = 0;
59 mPost('run-start', [...argv]);
60 App.wasm.xCall('wasm_main', argv.length,
61 App.wasm.scopedAllocMainArgv(argv));
62 }catch(e){
63 mPost('error',e.message);
64 }finally{
65 App.wasm.scopedAllocPop(scope);
66 mPost('run-end', App.logBuffer.join('\n'));
67 App.logBuffer.length = 0;
71 self.onmessage = function(msg){
72 msg = msg.data;
73 switch(msg.type){
74 case 'run': runSpeedtest(msg.data || []); break;
75 default:
76 logErr("Unhandled worker message type:",msg.type);
77 break;
81 const EmscriptenModule = {
82 print: log,
83 printErr: logErr,
84 setStatus: (text)=>mPost('load-status',text)
86 self.sqlite3InitModule(EmscriptenModule).then((sqlite3)=>{
87 const S = sqlite3;
88 App.vfsUnlink = function(pDb, fname){
89 const pVfs = S.wasm.sqlite3_wasm_db_vfs(pDb, 0);
90 if(pVfs) S.wasm.sqlite3_wasm_vfs_unlink(pVfs, fname||0);
92 App.pDir = wasmfsDir(S.wasm);
93 App.wasm = S.wasm;
94 //if(App.pDir) log("Persistent storage:",pDir);
95 //else log("Using transient storage.");
96 mPost('ready',true);
97 log("Registered VFSes:", ...S.capi.sqlite3_js_vfs_list());
98 });
99 })();