aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm
diff options
context:
space:
mode:
Diffstat (limited to 'ext/wasm')
-rw-r--r--ext/wasm/GNUmakefile27
-rw-r--r--ext/wasm/api/sqlite3-api-prologue.js49
-rw-r--r--ext/wasm/api/sqlite3-wasm.c27
-rw-r--r--ext/wasm/testing1.js5
-rw-r--r--ext/wasm/testing2.js2
5 files changed, 92 insertions, 18 deletions
diff --git a/ext/wasm/GNUmakefile b/ext/wasm/GNUmakefile
index e32febbfc..dad77dba0 100644
--- a/ext/wasm/GNUmakefile
+++ b/ext/wasm/GNUmakefile
@@ -127,9 +127,9 @@ sqlite3-api.jses := \
$(dir.jacc)/jaccwabyt.js \
$(dir.api)/sqlite3-api-glue.js \
$(dir.api)/sqlite3-api-oo1.js \
- $(dir.api)/sqlite3-api-worker.js \
- $(dir.api)/sqlite3-api-opfs.js \
- $(dir.api)/sqlite3-api-cleanup.js
+ $(dir.api)/sqlite3-api-worker.js
+#sqlite3-api.jses += $(dir.api)/sqlite3-api-opfs.js
+sqlite3-api.jses += $(dir.api)/sqlite3-api-cleanup.js
sqlite3-api.js := $(dir.api)/sqlite3-api.js
CLEAN_FILES += $(sqlite3-api.js)
@@ -174,7 +174,6 @@ emcc.cflags += -pthread
# emcc flags specific to building the final .js/.wasm file...
emcc.jsflags := -fPIC
emcc.jsflags += --no-entry
-emcc.jsflags += -sENVIRONMENT=web,worker
emcc.jsflags += -sMODULARIZE
emcc.jsflags += -sSTRICT_JS
emcc.jsflags += -sDYNAMIC_EXECUTION=0
@@ -183,7 +182,12 @@ emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(dir.wasm)/EXPORTED_FUNCTIONS.api
emcc.jsflags += -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory # wasmMemory==>for -sIMPORTED_MEMORY
emcc.jsflags += -sUSE_CLOSURE_COMPILER=0
emcc.jsflags += -sIMPORTED_MEMORY
-emcc.jsflags += -pthread -sWASMFS
+emcc.environment := -sENVIRONMENT=web
+ifeq (0,1)
+ emcc.jsflags += -pthread -sWASMFS -sPTHREAD_POOL_SIZE=2
+ emcc.environment := $(emcc.environment),worker
+endif
+emcc.jsflags += $(emcc.environment)
#emcc.jsflags += -sINITIAL_MEMORY=13107200
#emcc.jsflags += -sTOTAL_STACK=4194304
emcc.jsflags += -sEXPORT_NAME=sqlite3InitModule
@@ -235,11 +239,14 @@ $(dir.api)/sqlite3-wasm.o: $(dir.top)/sqlite3.c
$(dir.api)/wasm_util.o: emcc.cflags += $(SQLITE_OPT)
sqlite3.wasm.c := $(dir.api)/sqlite3-wasm.c \
$(dir.jacc)/jaccwabyt_test.c
-# ^^^ FIXME (how?): jaccwabyt_test.c is only needed for the test
-# apps. However, we want to test the release builds with those apps,
-# so we cannot simply elide that file in release builds. That
-# component is critical to the VFS bindings so needs to be tested
-# along with the core APIs.
+# ^^^ FIXME (how?): jaccwabyt_test.c is only needed for the test apps,
+# so we don't really want to include it in release builds. However, we
+# want to test the release builds with those apps, so we cannot simply
+# elide that file in release builds. That component is critical to the
+# VFS bindings so needs to be tested along with the core APIs.
+ifneq (,$(filter -sWASMFS,$(emcc.jsflags)))
+ $(dir.api)/sqlite3-wasm.o: emcc.cflags+=-DSQLITE_WASM_OPFS
+endif
define WASM_C_COMPILE
$(1).o := $$(subst .c,.o,$(1))
sqlite3.wasm.obj += $$($(1).o)
diff --git a/ext/wasm/api/sqlite3-api-prologue.js b/ext/wasm/api/sqlite3-api-prologue.js
index 60ed61477..08e3ebf6e 100644
--- a/ext/wasm/api/sqlite3-api-prologue.js
+++ b/ext/wasm/api/sqlite3-api-prologue.js
@@ -576,6 +576,55 @@ self.sqlite3ApiBootstrap = function(config){
["sqlite3_total_changes64", "i64", ["sqlite3*"]]
];
+ /** State for sqlite3_web_persistent_dir(). */
+ let __persistentDir;
+ /**
+ An experiment. Do not use.
+
+ If the wasm environment has a persistent storage directory,
+ its path is returned by this function. If it does not then
+ it returns one of:
+
+ - `undefined` if initIfNeeded is false and this function has
+ never been called before.
+
+ - `""` if no persistent storage is available.
+
+ Note that in both cases the return value is falsy.
+ */
+ capi.sqlite3_web_persistent_dir = function(initIfNeeded=true){
+ if(undefined !== __persistentDir) return __persistentDir;
+ else if(!initIfNeeded) return;
+ // If we have no OPFS, there is no persistent dir
+ if(!self.FileSystemHandle || !self.FileSystemDirectoryHandle
+ || !self.FileSystemFileHandle){
+ return __persistentDir = "";
+ }
+ try{
+ if(0===this.wasm.xCall('sqlite3_wasm_init_opfs')){
+ /** OPFS does not support locking and will trigger errors if
+ we try to lock. We don't _really_ want to
+ _unconditionally_ install a non-locking sqlite3 VFS as the
+ default, but we do so here for simplicy's sake for the
+ time being. That said: locking is a no-op on all of the
+ current WASM storage, so this isn't (currently) as bad as
+ it may initially seem. */
+ const pVfs = this.sqlite3_vfs_find("unix-none");
+ if(pVfs){
+ this.sqlite3_vfs_register(pVfs,1);
+ //warn("Installed 'unix-none' as the default sqlite3 VFS.");
+ }
+ return __persistentDir =
+ "/persistent" /* name is hard-coded in sqlite3_wasm_init_opfs()!*/;
+ }else{
+ return __persistentDir = "";
+ }
+ }catch(e){
+ // sqlite3_wasm_init_opfs() is not available
+ return __persistentDir = "";
+ }
+ }.bind(capi);
+
/* The remainder of the API will be set up in later steps. */
return {
capi,
diff --git a/ext/wasm/api/sqlite3-wasm.c b/ext/wasm/api/sqlite3-wasm.c
index 30a9dafeb..487baecf1 100644
--- a/ext/wasm/api/sqlite3-wasm.c
+++ b/ext/wasm/api/sqlite3-wasm.c
@@ -430,7 +430,7 @@ int sqlite3_wasm_vfs_unlink(const char * zName){
return rc;
}
-#ifdef __EMSCRIPTEN__
+#if defined(__EMSCRIPTEN__) && defined(SQLITE_WASM_OPFS)
#include <emscripten/wasmfs.h>
#include <emscripten/console.h>
/*
@@ -443,17 +443,34 @@ int sqlite3_wasm_vfs_unlink(const char * zName){
** WASMFS backend impl for OPFS. On success, subsequent calls are
** no-ops.
**
-** Returns 0 on success, SQLITE_NOMEM if intantiation of the backend
-** object fails.
+** Returns 0 on success, SQLITE_NOMEM if instantiation of the backend
+** object fails, SQLITE_IOERR if mkdir() of the "/persistent" dir in
+** the virtual FS fails. In builds compiled without SQLITE_WASM_OPFS
+** defined, SQLITE_NOTFOUND is returned without side effects.
*/
int sqlite3_wasm_init_opfs(void){
static backend_t pOpfs = 0;
+ static const char * zDir = "/persistent";
if( !pOpfs ){
pOpfs = wasmfs_create_opfs_backend();
if( pOpfs ){
- emscripten_console_log("Created OPFS WASMFS backend.");
+ emscripten_console_log("Created WASMFS OPFS backend.");
}
}
+ /** It's not enough to instantiate the backend. We have to create a
+ mountpoint in the VFS and attach the backend to it. */
+ if( pOpfs && 0!=access(zDir, F_OK) ){
+ /* mkdir() simply hangs when called from fiddle app. Cause is
+ not yet determined but the hypothesis is an init-order
+ issue. */
+ const int rc = wasmfs_create_directory(zDir, 0777, pOpfs);
+ emscripten_console_log(rc ? "OPFS mkdir failed." : "OPFS mkdir ok.");
+ if(rc) return SQLITE_IOERR;
+ }
return pOpfs ? 0 : SQLITE_NOMEM;
}
-#endif /* __EMSCRIPTEN__ */
+#else
+int sqlite3_wasm_init_opfs(void){
+ return SQLITE_NOTFOUND;
+}
+#endif /* __EMSCRIPTEN__ && SQLITE_WASM_OPFS */
diff --git a/ext/wasm/testing1.js b/ext/wasm/testing1.js
index a733156e7..779e0bd72 100644
--- a/ext/wasm/testing1.js
+++ b/ext/wasm/testing1.js
@@ -19,7 +19,8 @@
const toss = function(...args){throw new Error(args.join(' '))};
const debug = console.debug.bind(console);
const eOutput = document.querySelector('#test-output');
- const log = console.log.bind(console)
+ const log = console.log.bind(console),
+ warn = console.warn.bind(console);
const logHtml = function(...args){
log.apply(this, args);
const ln = document.createElement('div');
@@ -1012,7 +1013,7 @@
wasm = capi.wasm;
log("Loaded module:",capi.sqlite3_libversion(), capi.sqlite3_sourceid());
log("Build options:",wasm.compileOptionUsed());
-
+ capi.sqlite3_web_persistent_dir()/*will install OPFS if available, plus a and non-locking VFS*/;
if(1){
/* Let's grab those last few lines of test coverage for
sqlite3-api.js... */
diff --git a/ext/wasm/testing2.js b/ext/wasm/testing2.js
index 3a279513f..f6792955f 100644
--- a/ext/wasm/testing2.js
+++ b/ext/wasm/testing2.js
@@ -20,7 +20,7 @@
id: undefined
};
const eOutput = document.querySelector('#test-output');
- const log = console.log.bind(console)
+ const log = console.log.bind(console);
const logHtml = function(cssClass,...args){
log.apply(this, args);
const ln = document.createElement('div');