diff --git a/Makefile.in b/Makefile.in index 895417f143..31579970a5 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1540,18 +1540,23 @@ $(fiddle_module_js): Makefile sqlite3.c shell.c \ $(fiddle_dir)/EXPORTED_RUNTIME_METHODS \ $(fiddle_dir)/EXPORTED_FUNCTIONS.fiddle emcc -o $@ $(emcc_flags) \ + -sENVIRONMENT=web \ -sMODULARIZE \ -sEXPORT_NAME=initFiddleModule \ - -sENVIRONMENT=web \ -sEXPORTED_RUNTIME_METHODS=@$(fiddle_dir_abs)/EXPORTED_RUNTIME_METHODS \ -sEXPORTED_FUNCTIONS=@$(fiddle_dir_abs)/EXPORTED_FUNCTIONS.fiddle \ sqlite3.c shell.c $(sqlite3_wasm_js): Makefile sqlite3.c \ + $(fiddle_dir)/sqlite3-api.js \ $(fiddle_dir)/EXPORTED_RUNTIME_METHODS \ $(fiddle_dir)/EXPORTED_FUNCTIONS.sqlite3-api emcc -o $@ $(emcc_flags) \ + -sENVIRONMENT=web \ + -sMODULARIZE \ + -sEXPORT_NAME=initSqlite3Module \ -sEXPORTED_RUNTIME_METHODS=@$(fiddle_dir_abs)/EXPORTED_RUNTIME_METHODS \ -sEXPORTED_FUNCTIONS=@$(fiddle_dir_abs)/EXPORTED_FUNCTIONS.sqlite3-api \ + --post-js=$(fiddle_dir)/sqlite3-api.js \ --no-entry \ sqlite3.c fiddle: $(fiddle_module_js) diff --git a/ext/fiddle/Makefile b/ext/fiddle/Makefile index 841a3febde..6f3279b61a 100644 --- a/ext/fiddle/Makefile +++ b/ext/fiddle/Makefile @@ -1,10 +1,10 @@ # This GNU makefile exists primarily to simplify/speed up development # from emacs. It is not part of the canonical build process. default: - make -C ../.. wasm -e emcc_opt=-O0 + $(MAKE) -C ../.. wasm -e emcc_opt=-O0 clean: - make -C ../../ clean-wasm + $(MAKE) -C ../../ clean-wasm fiddle_files = emscripten.css fiddle.html \ fiddle.js fiddle-module.js \ diff --git a/ext/fiddle/SqliteTestUtil.js b/ext/fiddle/SqliteTestUtil.js index 964e60b215..cf78946dc7 100644 --- a/ext/fiddle/SqliteTestUtil.js +++ b/ext/fiddle/SqliteTestUtil.js @@ -1,54 +1,144 @@ -/** - Helpers for writing sqlite3-specific tests. +/* + 2022-05-22 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file contains bootstrapping code used by various test scripts + which live in this file's directory. */ -self/*window or worker*/.SqliteTestUtil = { - /** Running total of the number of tests run via - this API. */ - counter: 0, +(function(){ + /* querySelectorAll() proxy */ + const EAll = function(/*[element=document,] cssSelector*/){ + return (arguments.length>1 ? arguments[0] : document) + .querySelectorAll(arguments[arguments.length-1]); + }; + /* querySelector() proxy */ + const E = function(/*[element=document,] cssSelector*/){ + return (arguments.length>1 ? arguments[0] : document) + .querySelector(arguments[arguments.length-1]); + }; + /** - If expr is a function, it is called and its result - is returned, coerced to a bool, else expr, coerced to - a bool, is returned. + Helpers for writing sqlite3-specific tests. */ - toBool: function(expr){ - return (expr instanceof Function) ? !!expr() : !!expr; - }, - /** abort() if expr is false. If expr is a function, it - is called and its result is evaluated. + self/*window or worker*/.SqliteTestUtil = { + /** Running total of the number of tests run via + this API. */ + counter: 0, + /** + If expr is a function, it is called and its result + is returned, coerced to a bool, else expr, coerced to + a bool, is returned. + */ + toBool: function(expr){ + return (expr instanceof Function) ? !!expr() : !!expr; + }, + /** abort() if expr is false. If expr is a function, it + is called and its result is evaluated. + */ + assert: function f(expr, msg){ + if(!f._){ + f._ = ('undefined'===typeof abort + ? (msg)=>{throw new Error(msg)} + : abort); + } + ++this.counter; + if(!this.toBool(expr)){ + f._(msg || "Assertion failed."); + } + return this; + }, + /** Identical to assert() but throws instead of calling + abort(). */ + affirm: function(expr, msg){ + ++this.counter; + if(!this.toBool(expr)) throw new Error(msg || "Affirmation failed."); + return this; + }, + /** Calls f() and squelches any exception it throws. If it + does not throw, this function throws. */ + mustThrow: function(f, msg){ + ++this.counter; + let err; + try{ f(); } catch(e){err=e;} + if(!err) throw new Error(msg || "Expected exception."); + return this; + }, + /** Throws if expr is truthy or expr is a function and expr() + returns truthy. */ + throwIf: function(expr, msg){ + ++this.counter; + if(this.toBool(expr)) throw new Error(msg || "throwIf() failed"); + return this; + }, + /** Throws if expr is falsy or expr is a function and expr() + returns falsy. */ + throwUnless: function(expr, msg){ + ++this.counter; + if(!this.toBool(expr)) throw new Error(msg || "throwUnless() failed"); + return this; + } + }; + + + /** + This is a module object for use with the emscripten-installed + initSqlite3Module() factory function. */ - assert: function(expr, msg){ - ++this.counter; - if(!this.toBool(expr)) abort(msg || "Assertion failed."); - return this; - }, - /** Identical to assert() but throws instead of calling - abort(). */ - affirm: function(expr, msg){ - ++this.counter; - if(!this.toBool(expr)) throw new Error(msg || "Affirmation failed."); - return this; - }, - /** Calls f() and squelches any exception it throws. If it - does not throw, this function throws. */ - mustThrow: function(f, msg){ - ++this.counter; - let err; - try{ f(); } catch(e){err=e;} - if(!err) throw new Error(msg || "Expected exception."); - return this; - }, - /** Throws if expr is truthy or expr is a function and expr() - returns truthy. */ - throwIf: function(expr, msg){ - ++this.counter; - if(this.toBool(expr)) throw new Error(msg || "throwIf() failed"); - return this; - }, - /** Throws if expr is falsy or expr is a function and expr() - returns falsy. */ - throwUnless: function(expr, msg){ - ++this.counter; - if(!this.toBool(expr)) throw new Error(msg || "throwUnless() failed"); - return this; - } -}; + self.sqlite3TestModule = { + postRun: [ + /* function(theModule){...} */ + ], + //onRuntimeInitialized: function(){}, + /* Proxy for C-side stdout output. */ + print: function(){ + console.log.apply(console, Array.prototype.slice.call(arguments)); + }, + /* Proxy for C-side stderr output. */ + printErr: function(){ + console.error.apply(console, Array.prototype.slice.call(arguments)); + }, + /** + Called by the module init bits to report loading + progress. It gets passed an empty argument when loading is + done (after onRuntimeInitialized() and any this.postRun + callbacks have been run). + */ + setStatus: function f(text){ + if(!f.last){ + f.last = { text: '', step: 0 }; + f.ui = { + status: E('#module-status'), + progress: E('#module-progress'), + spinner: E('#module-spinner') + }; + } + if(text === f.last.text) return; + f.last.text = text; + if(f.ui.progress){ + f.ui.progress.value = f.last.step; + f.ui.progress.max = f.last.step + 1; + } + ++f.last.step; + if(text) { + f.ui.status.classList.remove('hidden'); + f.ui.status.innerText = text; + }else{ + if(f.ui.progress){ + f.ui.progress.remove(); + f.ui.spinner.remove(); + delete f.ui.progress; + delete f.ui.spinner; + } + f.ui.status.classList.add('hidden'); + } + } + }; +})(self/*window or worker*/); diff --git a/ext/fiddle/sqlite3-api.js b/ext/fiddle/sqlite3-api.js index 7dff49980b..d88ec9a8b8 100644 --- a/ext/fiddle/sqlite3-api.js +++ b/ext/fiddle/sqlite3-api.js @@ -10,40 +10,45 @@ *********************************************************************** - This file is intended to be loaded after loading sqlite3.wasm. It - sets up one of any number of potential bindings using that API, this - one as closely matching the C-native API as is feasible in JS. + This file is intended to be appended to the emcc-generated + sqlite3.js via emcc: - Note that this file is not named sqlite3.js because that file gets - generated by emscripten as the JS-glue counterpart of sqlite3.wasm. + emcc ... -sMODULARIZE -sEXPORT_NAME=initSqlite3Module --post-js=THIS_FILE - This code installs an object named self.Module.sqlite3, where self - is expected to be either the global window or Worker object and - Module is the object set up by the emscripten infrastructure. The - sqlite3 object looks like: + It is loaded by importing the emcc-generated sqlite3.js, then: - { - api: bindings for much of the core sqlite3 APIs, - SQLite3: high-level OO API wrapper - } + initSqlite3Module({module object}).then( + function(theModule){ + theModule.sqlite3 == an object containing this file's + deliverables: + { + api: bindings for much of the core sqlite3 APIs, + SQLite3: high-level OO API wrapper + } + }); - The way we export this module is not _really_ modern-JS-friendly - because it exports/relies on a global symbol (which is admittedly - not ideal). Exporting it "cleanly," without introducing any - global-scope symbols, requires using a module loader in all client - code. As there are several different approaches, none of which this - developer is currently truly familiar with, the current approach - will have to do for the time being. + It is up to the caller to provide a module object compatible with + emcc, but it can be a plain empty object. The object passed to + initSqlite3Module() will get populated by the emscripten-generated + bits and, in part, by the code from this file. Specifically, this file + installs the `theModule.sqlite3` part shown above. + + The resulting sqlite3.api object wraps the standard sqlite3 C API in + a way as close to its native form as JS allows for. The + sqlite3.SQLite3 object provides a higher-level wrapper more + appropriate for general client-side use in JS. Because using certain parts of the low-level API properly requires some degree of WASM-related magic, it is not recommended that that API be used as-is in client-level code. Rather, client code should use the higher-level OO API or write a custom wrapper on top of the - lower-level API. In short, using any C-style APIs which take + lower-level API. In short, most of the C-style API is used in an + intuitive manner from JS but any C-style APIs which take pointers-to-pointer arguments require WASM-specific interfaces installed by emcscripten-generated code. Those which take or return only integers, doubles, strings, or "plain" pointers to db or - statement objects can be used in a straightforward manner. + statement objects can be used in "as normal," noting that "pointers" + in wasm are simply 32-bit integers. # Goals and Non-goals of this API @@ -77,7 +82,10 @@ modern web and requires modern platforms. */ -(function(namespace){ +if(!Module.postRun) Module.postRun = []; +/* ^^^^ the name Module is, in this setup, scope-local in the generated + file sqlite3.js, with which this file gets combined at build-time. */ +Module.postRun.push(function(namespace){ 'use strict'; /* For reference: sql.js does essentially everything we want and it solves much of the wasm-related voodoo, but we'll need a @@ -111,6 +119,8 @@ clear and much experimentation is pending. */ + const SQM = namespace/*the sqlite module object */; + /** Set up the main sqlite3 binding API here, mimicking the C API as closely as we can. @@ -157,7 +167,7 @@ will only support UTF8. */ SQLITE_UTF8: 1 }; - const cwrap = Module.cwrap; + const cwrap = SQM.cwrap; [/* C-side functions to bind. Each entry is an array with 3 or 4 elements: @@ -237,7 +247,6 @@ const k = (4==a.length) ? a.shift() : a[0]; api[k] = cwrap.apply(this, a); }); - //console.debug("libversion =",api.sqlite3_libversion()); /* What follows is colloquially known as "OO API #1". It is a binding of the sqlite3 API which is designed to be run within @@ -418,7 +427,7 @@ delete that._statements[k]; if(s && s._pStmt) s.finalize(); }); - Object.values(this._udfs).forEach(Module.removeFunction); + Object.values(this._udfs).forEach(SQM.removeFunction); delete this._udfs; delete this._statements; delete this.filename; @@ -562,7 +571,7 @@ (opt.callback && opt.rowMode) ? opt.rowMode : false); try{ - let pSql = allocateUTF8OnStack(arg.sql) + let pSql = SQM.allocateUTF8OnStack(arg.sql) const pzTail = stackAlloc(4); while(getValue(pSql, "i8")){ setValue(pPtrArg, 0, "i32"); @@ -731,9 +740,10 @@ api.sqlite3_result_null(pCx); break; }else if(undefined!==val.length){ - const pBlob = Module.allocate(val, ALLOC_NORMAL); + const pBlob = + SQM.allocate(val, SQM.ALLOC_NORMAL); api.sqlite3_result_blob(pCx, pBlob, val.length, -1/*==SQLITE_TRANSIENT*/); - Module._free(blobptr); + SQM._free(blobptr); break; } // else fall through @@ -749,7 +759,7 @@ api.sqlite3_result_error(pCx, e.message, -1); } }; - const pUdf = Module.addFunction(wrapper, "viii"); + const pUdf = SQM.addFunction(wrapper, "viii"); let fFlags = 0; if(getOwnOption(opt, 'deterministic')) fFlags |= api.SQLITE_DETERMINISTIC; if(getOwnOption(opt, 'directOnly')) fFlags |= api.SQLITE_DIRECTONLY; @@ -762,11 +772,11 @@ api.SQLITE_UTF8 | fFlags, null/*pApp*/, pUdf, null/*xStep*/, null/*xFinal*/, null/*xDestroy*/)); }catch(e){ - Module.removeFunction(pUdf); + SQM.removeFunction(pUdf); throw e; } if(this._udfs.hasOwnProperty(name)){ - Module.removeFunction(this._udfs[name]); + SQM.removeFunction(this._udfs[name]); } this._udfs[name] = pUdf; return this; @@ -911,7 +921,7 @@ f._ = { string: function(stmt, ndx, val, asBlob){ const bytes = intArrayFromString(val,true); - const pStr = Module.allocate(bytes, ALLOC_NORMAL); + const pStr = SQM.allocate(bytes, ALLOC_NORMAL); stmt._allocs.push(pStr); const func = asBlob ? api.sqlite3_bind_blob : api.sqlite3_bind_text; return func(stmt._pStmt, ndx, pStr, bytes.length, 0); @@ -950,7 +960,7 @@ toss("Binding a value as a blob requires", "that it have a length member."); } - const pBlob = Module.allocate(val, ALLOC_NORMAL); + const pBlob = SQM.allocate(val, ALLOC_NORMAL); stmt._allocs.push(pBlob); rc = api.sqlite3_bind_blob(stmt._pStmt, ndx, pBlob, len, 0); } @@ -966,7 +976,7 @@ const freeBindMemory = function(stmt){ let m; while(undefined !== (m = stmt._allocs.pop())){ - Module._free(m); + SQM._free(m); } return stmt; }; @@ -1383,4 +1393,4 @@ api: api, SQLite3 }; -})(self/*worker or window*/.Module); +}); diff --git a/ext/fiddle/testing-common.js b/ext/fiddle/testing-common.js deleted file mode 100644 index 9a51dfad41..0000000000 --- a/ext/fiddle/testing-common.js +++ /dev/null @@ -1,121 +0,0 @@ -/* - 2022-05-22 - - The author disclaims copyright to this source code. In place of a - legal notice, here is a blessing: - - * May you do good and not evil. - * May you find forgiveness for yourself and forgive others. - * May you share freely, never taking more than you give. - - *********************************************************************** - - This file contains bootstrapping code used by various test scripts - which live in this file's directory. -*/ -(function(){ - /* querySelectorAll() proxy */ - const EAll = function(/*[element=document,] cssSelector*/){ - return (arguments.length>1 ? arguments[0] : document) - .querySelectorAll(arguments[arguments.length-1]); - }; - /* querySelector() proxy */ - const E = function(/*[element=document,] cssSelector*/){ - return (arguments.length>1 ? arguments[0] : document) - .querySelector(arguments[arguments.length-1]); - }; - - /* emscripten-related bits... */ - self.Module = { - /* ^^^ cannot declare that const because sqlite3.js - (auto-generated) includes a decl for it and runs in this - scope. */ - preRun: [], - postRun: [], - //onRuntimeInitialized: function(){}, - print: function(){ - console.log.apply(console, Array.prototype.slice.call(arguments)); - }, - printErr: function(){ - console.error.apply(console, Array.prototype.slice.call(arguments)); - }, - setStatus: function f(text){ - if(!f.last){ - f.last = { text: '', step: 0 }; - f.ui = { - status: E('#module-status'), - progress: E('#module-progress'), - spinner: E('#module-spinner') - }; - } - if(text === f.last.text) return; - f.last.text = text; - if(f.ui.progress){ - f.ui.progress.value = f.last.step; - f.ui.progress.max = f.last.step + 1; - } - ++f.last.step; - if(text) { - f.ui.status.classList.remove('hidden'); - f.ui.status.innerText = text; - }else{ - if(f.ui.progress){ - f.ui.progress.remove(); - f.ui.spinner.remove(); - delete f.ui.progress; - delete f.ui.spinner; - } - f.ui.status.classList.add('hidden'); - } - }, - totalDependencies: 0, - monitorRunDependencies: function(left) { - this.totalDependencies = Math.max(this.totalDependencies, left); - this.setStatus(left - ? ('Preparing... (' + (this.totalDependencies-left) - + '/' + this.totalDependencies + ')') - : 'All downloads complete.'); - }, - /** - Loads sqlite3-api.js and calls the given callback (if - provided), passing it an object: - - { - api:sqlite3_c-like API wrapper, - SQLite3: OO wrapper - } - - Whether this is synchronous or async depends on whether - it's run in the main thread (async) or a worker - (synchronous). - - If called after the module has been loaded, it uses a - cached reference, noting that multiple async calls may end - up loading it multiple times. - */ - loadSqliteAPI: function f(callback){ - const namespace = self.Module; - if(namespace.sqlite3){ - if(callback) callback(namespace.sqlite3); - return; - } - const theScript = 'sqlite3-api.js'; - if(self.importScripts){/*worker*/ - importScripts(theScript); - if(callback) callback(namespace.sqlite3); - }else{/*main thread*/ - new Promise((resolve, reject) => { - const script = document.createElement('script'); - document.body.appendChild(script); - script.onload = resolve; - script.onerror = reject; - script.async = true; - script.src = theScript; - }).then(() => { - if(callback) callback(namespace.sqlite3); - }); - } - } - }; - -})(self/*window or worker*/); diff --git a/ext/fiddle/testing1.html b/ext/fiddle/testing1.html index ce0e4c8fd8..bf22f30ff3 100644 --- a/ext/fiddle/testing1.html +++ b/ext/fiddle/testing1.html @@ -26,9 +26,8 @@