diff --git a/ext/rtree/rtree.c b/ext/rtree/rtree.c index 49053a2bcc..7daae9b1cb 100644 --- a/ext/rtree/rtree.c +++ b/ext/rtree/rtree.c @@ -3235,7 +3235,7 @@ static int rtreeUpdate( rtreeReference(pRtree); assert(nData>=1); - cell.iRowid = 0; /* Used only to suppress a compiler warning */ + memset(&cell, 0, sizeof(cell)); /* Constraint handling. A write operation on an r-tree table may return ** SQLITE_CONSTRAINT for two reasons: diff --git a/ext/wasm/EXPORTED_RUNTIME_METHODS.fiddle b/ext/wasm/EXPORTED_RUNTIME_METHODS.fiddle deleted file mode 100644 index 5e80f703ae..0000000000 --- a/ext/wasm/EXPORTED_RUNTIME_METHODS.fiddle +++ /dev/null @@ -1,15 +0,0 @@ -FS -addFunction -allocateUTF8OnStack -ccall -cwrap -getValue -intArrayFromString -lengthBytesUTF8 -removeFunction -setValue -stackAlloc -stackRestore -stackSave -stringToUTF8Array -wasmMemory diff --git a/ext/wasm/GNUmakefile b/ext/wasm/GNUmakefile index cf0e8b1081..815c84c873 100644 --- a/ext/wasm/GNUmakefile +++ b/ext/wasm/GNUmakefile @@ -1,11 +1,32 @@ -######################################################################## +####################################################################### # This GNU makefile drives the build of the sqlite3 WASM # components. It is not part of the canonical build process. +# +# This build assumes a Linux platform and is not intended for +# general-purpose client-level use, except for creating builds with +# custom configurations. It is primarily intended for the sqlite +# project's own development of the JS/WASM components. +# +# Primary targets: +# +# default, all = build in dev mode +# +# o0, o1, o2, o3, os, oz = full clean/rebuild with the -Ox level indicated +# by the target name. Rebuild is necessary for all components to get +# the desired optimization level. +# +# dist = create end user deliverables. Add dist.build=oX to build +# with a specific optimization level, where oX is one of the +# above-listed o? target names. +# +# clean = clean up ######################################################################## SHELL := $(shell which bash 2>/dev/null) MAKEFILE := $(lastword $(MAKEFILE_LIST)) -all: -release: all +CLEAN_FILES := +DISTCLEAN_FILES := ./--dummy-- +default: all +release: oz # Emscripten SDK home dir and related binaries... EMSDK_HOME ?= $(word 1,$(wildcard $(HOME)/src/emsdk $(HOME)/emsdk)) @@ -40,15 +61,53 @@ else endif dir.top := ../.. -# Reminder: some Emscripten flags require absolute paths -dir.wasm := $(patsubst %/,%,$(dir $(abspath $(MAKEFILE)))) +# Reminder: some Emscripten flags require absolute paths but we want +# relative paths for most stuff simply to reduce noise. The +# $(abspath...) GNU make function can transform relative paths to +# absolute. +dir.wasm := $(patsubst %/,%,$(dir $(MAKEFILE))) dir.api := api dir.jacc := jaccwabyt dir.common := common dir.fiddle := fiddle -CLEAN_FILES := *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~ +dir.tool := $(dir.top)/tool +######################################################################## +# MAINTENANCE REMINDER: the output .js and .wasm files of emcc must be +# in _this_ dir, rather than a subdir, or else parts of the generated +# code get confused and cannot load property. Specifically, when X.js +# loads X.wasm, whether or not X.js uses the correct path for X.wasm +# depends on how it's loaded: an HTML script tag will resolve it +# intuitively, whereas a Worker's call to importScripts() will not. +# That's a fundamental incompatibility with how URL resolution in +# JS happens between those two contexts. See: +# +# https://zzz.buzz/2017/03/14/relative-uris-in-web-development/ +# +# We unfortunately have no way, from Worker-initiated code, to +# automatically resolve the path from X.js to X.wasm. +# +# In case we ever find a solution to that which does not require +# duplicating the X.js files only to swap out the path to X.wasm for +# the loading-from-worker case... +# +# dir.dout = output dir for deliverables. +dir.dout := $(dir.wasm)/jswasm +# dir.tmp = output dir for intermediary build files, as opposed to +# end-user deliverables. +dir.tmp := $(dir.wasm)/bld +#CLEAN_FILES += $(wildcard $(dir.dout)/*) $(wildcard $(dir.tmp)/*) +ifeq (,$(wildcard $(dir.dout))) + dir._tmp := $(shell mkdir -p $(dir.dout)) +endif +ifeq (,$(wildcard $(dir.tmp))) + dir._tmp := $(shell mkdir -p $(dir.tmp)) +endif + +cflags.common := -I. -I.. -I$(dir.top) +CLEAN_FILES += *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~ emcc_enable_bigint ?= 1 sqlite3.c := $(dir.top)/sqlite3.c +sqlite3.h := $(dir.top)/sqlite3.h SQLITE_OPT = \ -DSQLITE_ENABLE_FTS4 \ -DSQLITE_ENABLE_RTREE \ @@ -109,11 +168,14 @@ emcc_opt_full := $(emcc_opt) -g3 ######################################################################## -$(sqlite3.c): +$(sqlite3.c) $(sqlite3.h): $(MAKE) -C $(dir.top) sqlite3.c +.PHONY: clean distclean clean: -rm -f $(CLEAN_FILES) +distclean: clean + -rm -f $(DISTCLEAN_FILES) ifeq (release,$(filter release,$(MAKECMDGOALS))) ifeq (,$(wasm-strip)) @@ -124,23 +186,54 @@ else $(info Development build. Use '$(MAKE) release' for a smaller release build.) endif -EXPORTED_FUNCTIONS.api.in := $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-api +bin.version-info := $(dir.wasm)/bin.version-info +# ^^^^ NOT in $(dir.tmp) because we need it to survive the cleanup +# process for the dist build to work properly. +$(bin.version-info): $(dir.wasm)/version-info.c $(sqlite3.h) $(MAKEFILE) + $(CC) -O0 -I$(dir.top) -o $@ $< +DISTCLEAN_FILES += $(bin.version-info) -EXPORTED_FUNCTIONS.api: $(EXPORTED_FUNCTIONS.api.in) $(MAKEFILE) +bin.stripccomments := $(dir.tool)/stripccomments +$(bin.stripccomments): $(bin.stripccomments).c $(MAKEFILE) + $(CC) -o $@ $< +DISTCLEAN_FILES += $(bin.stripccomments) + +EXPORTED_FUNCTIONS.api.in := $(abspath $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-api) +EXPORTED_FUNCTIONS.api := $(dir.tmp)/EXPORTED_FUNCTIONS.api +$(EXPORTED_FUNCTIONS.api): $(EXPORTED_FUNCTIONS.api.in) $(MAKEFILE) cat $(EXPORTED_FUNCTIONS.api.in) > $@ -CLEAN_FILES += EXPORTED_FUNCTIONS.api -sqlite3-api.jses := $(dir.api)/sqlite3-api-prologue.js +sqlite3-license-version.js := $(dir.tmp)/sqlite3-license-version.js +sqlite3-license-version-header.js := $(dir.api)/sqlite3-license-version-header.js +sqlite3-api-build-version.js := $(dir.tmp)/sqlite3-api-build-version.js +# sqlite3-api.jses = the list of JS files which make up $(sqlite3-api.js), in +# the order they need to be assembled. +sqlite3-api.jses := $(sqlite3-license-version.js) +sqlite3-api.jses += $(dir.api)/sqlite3-api-prologue.js sqlite3-api.jses += $(dir.common)/whwasmutil.js sqlite3-api.jses += $(dir.jacc)/jaccwabyt.js sqlite3-api.jses += $(dir.api)/sqlite3-api-glue.js +sqlite3-api.jses += $(sqlite3-api-build-version.js) sqlite3-api.jses += $(dir.api)/sqlite3-api-oo1.js sqlite3-api.jses += $(dir.api)/sqlite3-api-worker1.js sqlite3-api.jses += $(dir.api)/sqlite3-api-opfs.js sqlite3-api.jses += $(dir.api)/sqlite3-api-cleanup.js -sqlite3-api.js := sqlite3-api.js -CLEAN_FILES += $(sqlite3-api.js) +# "External" API files which are part of our distribution +# but not part of the sqlite3-api.js amalgamation. +SOAP.js := $(dir.api)/sqlite3-opfs-async-proxy.js +sqlite3-worker1.js := $(dir.api)/sqlite3-worker1.js +sqlite3-worker1-promiser.js := $(dir.api)/sqlite3-worker1-promiser.js +define CP_XAPI +sqlite3-api.ext.jses += $$(dir.dout)/$$(notdir $(1)) +$$(dir.dout)/$$(notdir $(1)): $(1) $$(MAKEFILE) + cp $$< $$@ +endef +$(foreach X,$(SOAP.js) $(sqlite3-worker1.js) $(sqlite3-worker1-promiser.js),\ + $(eval $(call CP_XAPI,$(X)))) +all: $(sqlite3-api.ext.jses) + +sqlite3-api.js := $(dir.tmp)/sqlite3-api.js $(sqlite3-api.js): $(sqlite3-api.jses) $(MAKEFILE) @echo "Making $@..." @for i in $(sqlite3-api.jses); do \ @@ -149,12 +242,21 @@ $(sqlite3-api.js): $(sqlite3-api.jses) $(MAKEFILE) echo "/* END FILE: $$i */"; \ done > $@ +$(sqlite3-api-build-version.js): $(bin.version-info) $(MAKEFILE) + @echo "Making $@..." + @{ \ + echo 'self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){'; \ + echo -n ' sqlite3.version = '; \ + $(bin.version-info) --json; \ + echo ';'; \ + echo '});'; \ + } > $@ + ######################################################################## # --post-js and --pre-js are emcc flags we use to append/prepend JS to # the generated emscripten module file. pre-js.js := $(dir.api)/pre-js.js -post-js.js := post-js.js -CLEAN_FILES += $(post-js.js) +post-js.js := $(dir.tmp)/post-js.js post-jses := \ $(dir.api)/post-js-header.js \ $(sqlite3-api.js) \ @@ -171,20 +273,30 @@ extern-pre-js.js := $(dir.api)/extern-pre-js.js pre-post-common.flags := \ --post-js=$(post-js.js) \ --extern-post-js=$(extern-post-js.js) \ - --extern-pre-js=$(extern-pre-js.js) + --extern-pre-js=$(sqlite3-license-version.js) pre-post-jses.deps := $(post-js.js) \ - $(extern-post-js.js) $(extern-pre-js.js) + $(extern-post-js.js) $(extern-pre-js.js) $(sqlite3-license-version.js) +$(sqlite3-license-version.js): $(sqlite3.h) $(sqlite3-license-version-header.js) $(MAKEFILE) + @echo "Making $@..."; { \ + cat $(sqlite3-license-version-header.js); \ + echo '/*'; \ + echo '** This code was built from sqlite3 version...'; \ + echo "** "; \ + awk -e '/define SQLITE_VERSION/{$$1=""; print "**" $$0}' \ + -e '/define SQLITE_SOURCE_ID/{$$1=""; print "**" $$0}' $(sqlite3.h); \ + echo '*/'; \ + } > $@ + ######################################################################## # call-make-pre-js creates rules for pre-js-$(1).js. $1 = the base # name of the JS file on whose behalf this pre-js is for. define call-make-pre-js pre-post-$(1).flags ?= -pre-js-$(1).js: $$(pre-js.js) $$(MAKEFILE) +$$(dir.tmp)/pre-js-$(1).js: $$(pre-js.js) $$(MAKEFILE) cp $$(pre-js.js) $$@ echo "Module[xInstantiateWasm].uri = '$(1).wasm';" >> $$@ -CLEAN_FILES += pre-js-$(1).js -pre-post-$(1).deps := $$(pre-post-jses.deps) pre-js-$(1).js -pre-post-$(1).flags += --pre-js=pre-js-$(1).js +pre-post-$(1).deps := $$(pre-post-jses.deps) $$(dir.tmp)/pre-js-$(1).js +pre-post-$(1).flags += --pre-js=$$(dir.tmp)/pre-js-$(1).js endef #$(error $(call call-make-pre-js,sqlite3-wasmfs)) # /post-js and pre-js @@ -192,9 +304,9 @@ endef ######################################################################## # emcc flags for .c/.o/.wasm/.js. -emcc.flags = +emcc.flags := #emcc.flags += -v # _very_ loud but also informative about what it's doing -# -g is needed to keep -O2 and higher from creating broken JS via +# -g3 is needed to keep -O2 and higher from creating broken JS via # minification. ######################################################################## @@ -213,12 +325,11 @@ emcc.jsflags += -sMODULARIZE emcc.jsflags += -sSTRICT_JS emcc.jsflags += -sDYNAMIC_EXECUTION=0 emcc.jsflags += -sNO_POLYFILL -emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(dir.wasm)/EXPORTED_FUNCTIONS.api +emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.api) emcc.exportedRuntimeMethods := \ - -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory,allocateUTF8OnStack + -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory # FS ==> stdio/POSIX I/O proxies # wasmMemory ==> required by our code for use with -sIMPORTED_MEMORY - # allocateUTF8OnStack => for kvvfs internals emcc.jsflags += $(emcc.exportedRuntimeMethods) emcc.jsflags += -sUSE_CLOSURE_COMPILER=0 emcc.jsflags += -sIMPORTED_MEMORY @@ -234,7 +345,9 @@ emcc.jsflags += $(emcc.environment) #emcc.jsflags += -sTOTAL_STACK=4194304 sqlite3.js.init-func := sqlite3InitModule -# ^^^^ $(sqlite3.js.init-func) symbol name is hard-coded in $(extern-post-js.js) +# ^^^^ $(sqlite3.js.init-func) symbol name is hard-coded in +# $(extern-post-js.js) as well as in numerous docs. If changed, it +# needs to be globally modified in *.js and all related documentation. emcc.jsflags += -sEXPORT_NAME=$(sqlite3.js.init-func) emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr. @@ -243,6 +356,7 @@ emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from #emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. sqlite3 needs the FS API #emcc.jsflags += -sABORTING_MALLOC emcc.jsflags += -sALLOW_TABLE_GROWTH +# -sALLOW_TABLE_GROWTH is required for installing new SQL UDFs emcc.jsflags += -Wno-limited-postlink-optimizations # ^^^^^ it likes to warn when we have "limited optimizations" via the -g3 flag. #emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why @@ -250,7 +364,7 @@ emcc.jsflags += -Wno-limited-postlink-optimizations emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=0 emcc.jsflags += -sLLD_REPORT_UNDEFINED #emcc.jsflags += --allow-undefined -emcc.jsflags += --import-undefined +#emcc.jsflags += --import-undefined #emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic #emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined #emcc.jsflags += --unresolved-symbols=ignore-all @@ -278,40 +392,27 @@ emcc.jsflags += -sWASM_BIGINT=$(emcc_enable_bigint) # debugging info, _huge_. ######################################################################## -######################################################################## -# Maintenance reminder: the output .js and .wasm files of emcc must be -# in _this_ dir, rather than a subdir, or else parts of the generated -# code get confused and cannot load property (namely, the -# sqlite3.worker.js generated in conjunction with -sWASMFS). -sqlite3.js := sqlite3.js -sqlite3.wasm := sqlite3.wasm -sqlite3-wasm.o := $(dir.api)/sqlite3-wasm.o -$(sqlite3-wasm.o): emcc.cflags += $(SQLITE_OPT) -$(sqlite3-wasm.o): $(dir.top)/sqlite3.c +sqlite3.js := $(dir.dout)/sqlite3.js +sqlite3.wasm := $(dir.dout)/sqlite3.wasm sqlite3-wasm.c := $(dir.api)/sqlite3-wasm.c -######################################################################## -# call-wasm-c-compile sets up build rules -# for $1.o. $1 must be the name of a C file (with extension). -define call-wasm-c-compile -$(1).o := $$(subst .c,.o,$(1)) -sqlite3.wasm.obj += $$($(1).o) -$$($(1).o): $$(MAKEFILE) $(1) - $$(emcc.bin) $$(emcc_opt_full) $$(emcc.flags) $$(emcc.cflags) -c $(1) -o $$@ -CLEAN_FILES += $$($(1).o) -endef -$(foreach c,$(sqlite3-wasm.c),$(eval $(call call-wasm-c-compile,$(c)))) +# sqlite3-wasm.o vs sqlite3-wasm.c: building against the latter +# (predictably) results in a slightly faster binary, but we're close +# enough to the target speed requirements that the 500ms makes a +# difference. Thus we build all binaries against sqlite3-wasm.c +# instead of building a shared copy of sqlite3-wasm.o. $(eval $(call call-make-pre-js,sqlite3)) +$(sqlite3.js): $(sqlite3.js): $(MAKEFILE) $(sqlite3.wasm.obj) \ - EXPORTED_FUNCTIONS.api \ + $(EXPORTED_FUNCTIONS.api) \ $(pre-post-sqlite3.deps) @echo "Building $@ ..." $(emcc.bin) -o $@ $(emcc_opt_full) $(emcc.flags) \ $(emcc.jsflags) $(pre-post-common.flags) $(pre-post-sqlite3.flags) \ - $(sqlite3.wasm.obj) + $(cflags.common) $(SQLITE_OPT) $(sqlite3-wasm.c) chmod -x $(sqlite3.wasm) $(maybe-wasm-strip) $(sqlite3.wasm) @ls -la $@ $(sqlite3.wasm) - +$(sqlite3.wasm): $(sqlite3.js) CLEAN_FILES += $(sqlite3.js) $(sqlite3.wasm) all: $(sqlite3.js) wasm: $(sqlite3.js) @@ -355,7 +456,8 @@ speedtest1-common.eflags += -sINITIAL_MEMORY=128450560 speedtest1-common.eflags += -sSTRICT_JS speedtest1-common.eflags += -sMODULARIZE speedtest1-common.eflags += -Wno-limited-postlink-optimizations -speedtest1-common.eflags += -sEXPORTED_FUNCTIONS=@$(dir.wasm)/EXPORTED_FUNCTIONS.speedtest1 +EXPORTED_FUNCTIONS.speedtest1 := $(abspath $(dir.tmp)/EXPORTED_FUNCTIONS.speedtest1) +speedtest1-common.eflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.speedtest1) speedtest1-common.eflags += $(emcc.exportedRuntimeMethods) speedtest1-common.eflags += -sALLOW_TABLE_GROWTH speedtest1-common.eflags += -sDYNAMIC_EXECUTION=0 @@ -382,31 +484,24 @@ speedtest1.exit-runtime1 := -sEXIT_RUNTIME=1 # -sEXIT_RUNTIME=1 but we need EXIT_RUNTIME=0 for the worker-based app # which runs speedtest1 multiple times. -EXPORTED_FUNCTIONS.speedtest1: EXPORTED_FUNCTIONS.api - { echo _wasm_main; cat EXPORTED_FUNCTIONS.api; } > $@ -CLEAN_FILES += EXPORTED_FUNCTIONS.speedtest1 -speedtest1.js := speedtest1.js +$(EXPORTED_FUNCTIONS.speedtest1): $(EXPORTED_FUNCTIONS.api) + @echo "Making $@ ..." + @{ echo _wasm_main; cat $(EXPORTED_FUNCTIONS.api); } > $@ +speedtest1.js := $(dir.dout)/speedtest1.js speedtest1.wasm := $(subst .js,.wasm,$(speedtest1.js)) -speedtest1.cflags := \ - -I. -I.. -I$(dir.top) \ - -DSQLITE_SPEEDTEST1_WASM -speedtest1.cs := $(speedtest1.c) $(sqlite3-wasm.c) -$(speedtest1.js): emcc.cflags+= -# speedtest1 notes re. sqlite3-wasm.o vs sqlite3-wasm.c: building against -# the latter (predictably) results in a slightly faster binary, but we're -# close enough to the target speed requirements that the 500ms makes a -# difference. +speedtest1.cflags := $(cflags.common) -DSQLITE_SPEEDTEST1_WASM +speedtest1.cses := $(speedtest1.c) $(sqlite3-wasm.c) $(eval $(call call-make-pre-js,speedtest1)) -$(speedtest1.js): $(MAKEFILE) $(speedtest1.cs) \ +$(speedtest1.js): $(MAKEFILE) $(speedtest1.cses) \ $(pre-post-speedtest1.deps) \ - EXPORTED_FUNCTIONS.speedtest1 + $(EXPORTED_FUNCTIONS.speedtest1) @echo "Building $@ ..." $(emcc.bin) \ $(speedtest1.eflags) $(speedtest1-common.eflags) $(speedtest1.cflags) \ $(pre-post-speedtest1.flags) \ $(SQLITE_OPT) \ $(speedtest1.exit-runtime0) \ - -o $@ $(speedtest1.cs) -lm + -o $@ $(speedtest1.cses) -lm $(maybe-wasm-strip) $(speedtest1.wasm) ls -la $@ $(speedtest1.wasm) @@ -446,16 +541,12 @@ push-fiddle: $(fiddle_files) # painful. .PHONY: o0 o1 o2 o3 os oz -o-xtra := -g3 -# ^^^ -g3 is important to keep higher -O levels from mangling (via -# minification), or outright removing, otherwise working code. - -o-xtra += -flto +o-xtra := -flto # ^^^^ -flto can have a considerably performance boost at -O0 but # doubles the build time and seems to have negligible effect on # higher optimization levels. o0: clean - $(MAKE) -e "emcc_opt=-O0 $(o-xtra)" + $(MAKE) -e "emcc_opt=-O0" o1: clean $(MAKE) -e "emcc_opt=-O1 $(o-xtra)" o2: clean @@ -473,6 +564,13 @@ oz: clean include fiddle.make +ifneq (,$(filter wasmfs,$(MAKECMDGOALS))) +# wasmfs build disabled 2022-10-19 per /chat +# discussion. OPFS-over-wasmfs was initially a stopgap measure and a +# convenient point of comparison for the OPFS sqlite3_vfs's +# performance, but it currently doubles our deliverables for very +# little, if any, benefit. +# ######################################################################## # Some platforms do not support the WASMFS build. Raspberry Pi OS is one # of them. As such platforms are discovered, add their (uname -m) name @@ -481,14 +579,25 @@ PLATFORMS_WITH_NO_WASMFS := aarch64 # add any others here THIS_ARCH := $(shell /usr/bin/uname -m) ifneq (,$(filter $(THIS_ARCH),$(PLATFORMS_WITH_NO_WASMFS))) $(info This platform does not support the WASMFS build.) +HAVE_WASMFS := 0 else +HAVE_WASMFS := 1 include wasmfs.make endif +endif +# /wasmfs +######################################################################## + +######################################################################## +# Create deliverables: +ifneq (,$(filter dist,$(MAKECMDGOALS))) +include dist.make +endif ######################################################################## # Push files to public wasm-testing.sqlite.org server -wasm-testing.include = *.wasm *.js *.html \ - batch-runner.list sql common fiddle jaccwabyt +wasm-testing.include = $(dir.dout) *.js *.html \ + batch-runner.list $(dir.sql) $(dir.common) $(dir.fiddle) $(dir.jacc) wasm-testing.exclude = sql/speedtest1.sql wasm-testing.dir = /jail/sites/wasm-testing wasm-testing.dest ?= wasm-testing:$(wasm-testing.dir) @@ -499,4 +608,3 @@ push: $(wasm-testing.include) $(wasm-testing.dest) ssh wasm-testing 'cd $(wasm-testing.dir) && bash .gzip' || \ echo "SSH failed: it's likely that stale content will be served via old gzip files." - diff --git a/ext/wasm/README-dist.txt b/ext/wasm/README-dist.txt new file mode 100644 index 0000000000..c6fb2c5d1a --- /dev/null +++ b/ext/wasm/README-dist.txt @@ -0,0 +1,23 @@ +This is the README for the sqlite3 WASM/JS distribution. + +Main project page: https://sqlite.org + +TODO: link to main WASM/JS docs, once they are online + +This archive contains the sqlite3.js and sqlite3.wasm file which make +up the sqlite3 WASM/JS build. + +The jswasm directory contains the core sqlite3 deliverables and the +top-level directory contains demonstration and test apps. Browsers +will not serve WASM files from file:// URLs, so the demo/test apps +require a web server and that server must include the following +headers in its response when serving the files: + + Cross-Origin-Opener-Policy: same-origin + Cross-Origin-Embedder-Policy: require-corp + +One simple way to get the demo apps up and running on Unix-style +systems is to install althttpd (https://sqlite.org/althttpd) and run: + + althttpd --enable-sab --page index.html + diff --git a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api index 506775d2fe..b31f613576 100644 --- a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api +++ b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api @@ -89,5 +89,6 @@ _sqlite3_value_text _sqlite3_value_type _sqlite3_vfs_find _sqlite3_vfs_register +_sqlite3_vfs_unregister _malloc _free diff --git a/ext/wasm/api/README.md b/ext/wasm/api/README.md index edac17fa1e..6440eba996 100644 --- a/ext/wasm/api/README.md +++ b/ext/wasm/api/README.md @@ -48,6 +48,10 @@ browser client: Invokes functionality exposed by the previous two files to flesh out low-level parts of `sqlite3-api-prologue.js`. Most of these pieces related to the `sqlite3.capi.wasm` object. +- `sqlite3-api-build-version.js`\ + Gets created by the build process and populates the + `sqlite3.version` object. This part is not critical, but records the + version of the library against which this module was built. - `sqlite3-api-oo1.js`\ Provides a high-level object-oriented wrapper to the lower-level C API, colloquially known as OO API #1. Its API is similar to other @@ -60,12 +64,12 @@ browser client: thread via the Worker message-passing interface. Like OO API #1, this is an optional component, offering one of any number of potential implementations for such an API. - - `../sqlite3-worker1.js`\ + - `sqlite3-worker1.js`\ Is not part of the amalgamated sources and is intended to be loaded by a client Worker thread. It loads the sqlite3 module and runs the Worker #1 API which is implemented in `sqlite3-api-worker1.js`. - - `../sqlite3-worker1-promiser.js`\ + - `sqlite3-worker1-promiser.js`\ Is likewise not part of the amalgamated sources and provides a Promise-based interface into the Worker #1 API. This is a far user-friendlier way to interface with databases running @@ -74,7 +78,7 @@ browser client: is an sqlite3 VFS implementation which supports Google Chrome's Origin-Private FileSystem (OPFS) as a storage layer to provide persistent storage for database files in a browser. It requires... - - `../sqlite3-opfs-async-proxy.js`\ + - `sqlite3-opfs-async-proxy.js`\ is the asynchronous backend part of the OPFS proxy. It speaks directly to the (async) OPFS API and channels those results back to its synchronous counterpart. This file, because it must be diff --git a/ext/wasm/api/extern-post-js.js b/ext/wasm/api/extern-post-js.js index 5ebb5a31e7..67b32e8912 100644 --- a/ext/wasm/api/extern-post-js.js +++ b/ext/wasm/api/extern-post-js.js @@ -1,5 +1,9 @@ /* extern-post-js.js must be appended to the resulting sqlite3.js - file. */ + file. It gets its name from being used as the value for the + --extern-post-js=... Emscripten flag. Note that this code, unlike + most of the associated JS code, runs outside of the + Emscripten-generated module init scope, in the current + global scope. */ (function(){ /** In order to hide the sqlite3InitModule()'s resulting Emscripten @@ -15,6 +19,32 @@ if(!originalInit){ throw new Error("Expecting self.sqlite3InitModule to be defined by the Emscripten build."); } + /** + We need to add some state which our custom Module.locateFile() + can see, but an Emscripten limitation currently prevents us from + attaching it to the sqlite3InitModule function object: + + https://github.com/emscripten-core/emscripten/issues/18071 + + The only(?) current workaround is to temporarily stash this state + into the global scope and delete it when sqlite3InitModule() + is called. + */ + const initModuleState = self.sqlite3InitModuleState = Object.assign(Object.create(null),{ + moduleScript: self?.document?.currentScript, + isWorker: ('undefined' !== typeof WorkerGlobalScope), + location: self.location, + urlParams: new URL(self.location.href).searchParams + }); + if(initModuleState.urlParams.has('sqlite3.dir')){ + initModuleState.sqlite3Dir = initModuleState.urlParams.get('sqlite3.dir') +'/'; + }else if(initModuleState.moduleScript){ + const li = initModuleState.moduleScript.src.split('/'); + li.pop(); + initModuleState.sqlite3Dir = li.join('/') + '/'; + } + //console.warn("initModuleState =",initModuleState); + self.sqlite3InitModule = (...args)=>{ //console.warn("Using replaced sqlite3InitModule()",self.location); return originalInit(...args).then((EmscriptenModule)=>{ @@ -31,6 +61,8 @@ Emscripten details. */ return EmscriptenModule; } + EmscriptenModule.sqlite3.scriptInfo = initModuleState; + //console.warn("sqlite3.scriptInfo =",EmscriptenModule.sqlite3.scriptInfo); const f = EmscriptenModule.sqlite3.asyncPostInit; delete EmscriptenModule.sqlite3.asyncPostInit; return f(); @@ -40,5 +72,27 @@ }); }; self.sqlite3InitModule.ready = originalInit.ready; - //console.warn("Replaced sqlite3InitModule()"); + + if(self.sqlite3InitModuleState.moduleScript){ + const sim = self.sqlite3InitModuleState; + let src = sim.moduleScript.src.split('/'); + src.pop(); + sim.scriptDir = src.join('/') + '/'; + } + if(0){ + console.warn("Replaced sqlite3InitModule()"); + console.warn("self.location.href =",self.location.href); + if('undefined' !== typeof document){ + console.warn("document.currentScript.src =", + document?.currentScript?.src); + } + } + /* Replace the various module exports performed by the Emscripten + glue... */ + if (typeof exports === 'object' && typeof module === 'object') + module.exports = sqlite3InitModule; + else if (typeof exports === 'object') + exports["sqlite3InitModule"] = sqlite3InitModule; + /* AMD modules get injected in a way we cannot override, + so we can't handle those here. */ })(); diff --git a/ext/wasm/api/extern-pre-js.js b/ext/wasm/api/extern-pre-js.js index 26d066d2ee..7d47d33e33 100644 --- a/ext/wasm/api/extern-pre-js.js +++ b/ext/wasm/api/extern-pre-js.js @@ -1,4 +1,7 @@ /* extern-pre-js.js must be prepended to the resulting sqlite3.js file. This file is currently only used for holding snippets during test and development. + + It gets its name from being used as the value for the + --extern-pre-js=... Emscripten flag. */ diff --git a/ext/wasm/api/post-js-footer.js b/ext/wasm/api/post-js-footer.js index ee470928ba..58882cbd9c 100644 --- a/ext/wasm/api/post-js-footer.js +++ b/ext/wasm/api/post-js-footer.js @@ -1,3 +1,4 @@ /* The current function scope was opened via post-js-header.js, which - gets prepended to this at build-time. */ + gets prepended to this at build-time. This file closes that + scope. */ })/*postRun.push(...)*/; diff --git a/ext/wasm/api/pre-js.js b/ext/wasm/api/pre-js.js index 502ecf348f..c07d0373cd 100644 --- a/ext/wasm/api/pre-js.js +++ b/ext/wasm/api/pre-js.js @@ -1,6 +1,52 @@ +/** + BEGIN FILE: api/pre-js.js + + This file is intended to be prepended to the sqlite3.js build using + Emscripten's --pre-js=THIS_FILE flag (or equivalent). +*/ + +// See notes in extern-post-js.js +const sqlite3InitModuleState = self.sqlite3InitModuleState || Object.create(null); +delete self.sqlite3InitModuleState; + +/** + This custom locateFile() tries to figure out where to load `path` + from. The intent is to provide a way for foo/bar/X.js loaded from a + Worker constructor or importScripts() to be able to resolve + foo/bar/X.wasm (in the latter case, with some help): + + 1) If URL param named the same as `path` is set, it is returned. + + 2) If sqlite3InitModuleState.sqlite3Dir is set, then (thatName + path) + is returned (note that it's assumed to end with '/'). + + 3) If this code is running in the main UI thread AND it was loaded + from a SCRIPT tag, the directory part of that URL is used + as the prefix. (This form of resolution unfortunately does not + function for scripts loaded via importScripts().) + + 4) If none of the above apply, (prefix+path) is returned. +*/ Module['locateFile'] = function(path, prefix) { - return prefix + path; -}; + let theFile; + const up = this.urlParams; + if(0){ + console.warn("locateFile(",arguments[0], ',', arguments[1],")", + 'self.location =',self.location, + 'sqlite3InitModuleState.scriptDir =',this.scriptDir, + 'up.entries() =',Array.from(up.entries())); + } + if(up.has(path)){ + theFile = up.get(path); + }else if(this.sqlite3Dir){ + theFile = this.sqlite3Dir + path; + }else if(this.scriptDir){ + theFile = this.scriptDir + path; + }else{ + theFile = prefix + path; + } + return theFile; +}.bind(sqlite3InitModuleState); /** Bug warning: this xInstantiateWasm bit must remain disabled @@ -39,3 +85,4 @@ Module[xInstantiateWasm] = function callee(imports,onSuccess){ scripts. */ Module[xInstantiateWasm].uri = 'sqlite3.wasm'; +/* END FILE: api/pre-js.js */ diff --git a/ext/wasm/api/sqlite3-api-cleanup.js b/ext/wasm/api/sqlite3-api-cleanup.js index c2a3425891..555e5d31c5 100644 --- a/ext/wasm/api/sqlite3-api-cleanup.js +++ b/ext/wasm/api/sqlite3-api-cleanup.js @@ -20,17 +20,15 @@ if('undefined' !== typeof Module){ // presumably an Emscripten build /** Install a suitable default configuration for sqlite3ApiBootstrap(). */ - const SABC = self.sqlite3ApiConfig || Object.create(null); - if(undefined===SABC.Module){ - SABC.Module = Module /* ==> Currently needs to be exposed here for - test code. NOT part of the public API. */; - } - if(undefined===SABC.exports){ - SABC.exports = Module['asm']; - } - if(undefined===SABC.memory){ - SABC.memory = Module.wasmMemory /* gets set if built with -sIMPORT_MEMORY */; - } + const SABC = Object.assign( + Object.create(null), { + Module: Module /* ==> Currently needs to be exposed here for + test code. NOT part of the public API. */, + exports: Module['asm'], + memory: Module.wasmMemory /* gets set if built with -sIMPORT_MEMORY */ + }, + self.sqlite3ApiConfig || Object.create(null) + ); /** For current (2022-08-22) purposes, automatically call @@ -38,10 +36,11 @@ if('undefined' !== typeof Module){ // presumably an Emscripten build point, as we really want client code to be able to call this to configure certain parts. Clients may modify self.sqlite3ApiBootstrap.defaultConfig to tweak the default - configuration used by a no-args call to sqlite3ApiBootstrap(). + configuration used by a no-args call to sqlite3ApiBootstrap(), + but must have first loaded their WASM module in order to be + able to provide the necessary configuration state. */ //console.warn("self.sqlite3ApiConfig = ",self.sqlite3ApiConfig); - const rmApiConfig = (SABC !== self.sqlite3ApiConfig); self.sqlite3ApiConfig = SABC; let sqlite3; try{ @@ -51,7 +50,7 @@ if('undefined' !== typeof Module){ // presumably an Emscripten build throw e; }finally{ delete self.sqlite3ApiBootstrap; - if(rmApiConfig) delete self.sqlite3ApiConfig; + delete self.sqlite3ApiConfig; } if(self.location && +self.location.port > 1024){ diff --git a/ext/wasm/api/sqlite3-api-glue.js b/ext/wasm/api/sqlite3-api-glue.js index fd1f0c3e02..639e670792 100644 --- a/ext/wasm/api/sqlite3-api-glue.js +++ b/ext/wasm/api/sqlite3-api-glue.js @@ -72,10 +72,12 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ ('sqlite3_stmt*', aPtr) ('sqlite3_context*', aPtr) ('sqlite3_value*', aPtr) + ('sqlite3_vfs*', aPtr) ('void*', aPtr); wasm.xWrap.resultAdapter('sqlite3*', aPtr) - ('sqlite3_stmt*', aPtr) ('sqlite3_context*', aPtr) + ('sqlite3_stmt*', aPtr) + ('sqlite3_vfs*', aPtr) ('void*', aPtr); /** @@ -224,6 +226,9 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ const __udfSetResult = function(pCtx, val){ //console.warn("udfSetResult",typeof val, val); switch(typeof val) { + case 'undefined': + /* Assume that the client already called sqlite3_result_xxx(). */ + break; case 'boolean': capi.sqlite3_result_int(pCtx, val ? 1 : 0); break; @@ -318,7 +323,8 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ if(e instanceof sqlite3.WasmAllocError){ capi.sqlite3_result_error_nomem(pCtx); }else{ - capi.sqlite3_result_error(pCtx, e.message, -1); + const msg = ('string'===typeof e) ? e : e.message; + capi.sqlite3_result_error(pCtx, msg, -1); } }; @@ -437,10 +443,10 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ try{ const funcArgs = __xWrapFuncs({xStep, xFinal, xValue, xInverse, xDestroy}, uninstall); - rc = sqlite3CreateFunction(pDb, funcName, nArg, eTextRep, - pApp, ...funcArgs); + rc = sqlite3CreateWindowFunction(pDb, funcName, nArg, eTextRep, + pApp, ...funcArgs); }catch(e){ - console.error("sqlite3_create_function_v2() setup threw:",e); + console.error("sqlite3_create_window_function() setup threw:",e); for(let v of uninstall){ wasm.uninstallFunction(v); } @@ -460,6 +466,11 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ - `number`: sqlite3_result_int() or sqlite3_result_double() - `string`: sqlite3_result_text() - Uint8Array or Int8Array: sqlite3_result_blob() + - `undefined`: indicates that the UDF called one of the + `sqlite3_result_xyz()` routines on its own, making this + function a no-op. Results are _undefined_ if this function is + passed the `undefined` value but did _not_ call one of the + `sqlite3_result_xyz()` routines. Anything else triggers sqlite3_result_error(). */ @@ -492,10 +503,10 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ /** A helper for UDFs implemented in JS and bound to WASM by the client. It expects to be a passed `(sqlite3_context*, Error)` - (i.e. an exception object). And it sets the current UDF's - result to sqlite3_result_error_nomem() or sqlite3_result_error(), - depending on whether the 2nd argument is a - sqlite3.WasmAllocError object or not. + (an exception object or message string). And it sets the + current UDF's result to sqlite3_result_error_nomem() or + sqlite3_result_error(), depending on whether the 2nd argument + is a sqlite3.WasmAllocError object or not. */ capi.sqlite3_create_function_v2.udfSetError = capi.sqlite3_create_function.udfSetError = @@ -590,12 +601,16 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ For the given integer, returns the SQLITE_xxx result code as a string, or undefined if no such mapping is found. */ - capi.sqlite3_web_rc_str = (rc)=>__rcMap[rc]; + capi.sqlite3_js_rc_str = (rc)=>__rcMap[rc]; /* Bind all registered C-side structs... */ const notThese = Object.assign(Object.create(null),{ // Structs NOT to register WasmTestStruct: true }); + if(!util.isUIThread()){ + /* We remove the kvvfs VFS from Worker threads below. */ + notThese.sqlite3_kvvfs_methods = true; + } for(const s of wasm.ctype.structs){ if(!notThese[s.name]){ capi[s.name] = sqlite3.StructBinder(s); @@ -603,94 +618,96 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ } }/*end C constant imports*/ - if( util.isMainWindow() - && 0!==capi.sqlite3_vfs_find("kvvfs") ){/* kvvfs-specific glue */ - const kvvfsMethods = new capi.sqlite3_kvvfs_methods( - wasm.exports.sqlite3_wasm_kvvfs_methods() - ); - delete capi.sqlite3_kvvfs_methods; + const pKvvfs = capi.sqlite3_vfs_find("kvvfs"); + if( pKvvfs ){/* kvvfs-specific glue */ + if(util.isUIThread()){ + const kvvfsMethods = new capi.sqlite3_kvvfs_methods( + wasm.exports.sqlite3_wasm_kvvfs_methods() + ); + delete capi.sqlite3_kvvfs_methods; - const kvvfsMakeKey = wasm.exports.sqlite3_wasm_kvvfsMakeKeyOnPstack, - pstack = wasm.pstack, - pAllocRaw = wasm.exports.sqlite3_wasm_pstack_alloc; + const kvvfsMakeKey = wasm.exports.sqlite3_wasm_kvvfsMakeKeyOnPstack, + pstack = wasm.pstack, + pAllocRaw = wasm.exports.sqlite3_wasm_pstack_alloc; - const kvvfsStorage = (zClass)=> - ((115/*=='s'*/===wasm.getMemValue(zClass)) - ? sessionStorage : localStorage); - - const kvvfsImpls = { - xRead: (zClass, zKey, zBuf, nBuf)=>{ - const stack = pstack.pointer, - astack = wasm.scopedAllocPush(); - try { - const zXKey = kvvfsMakeKey(zClass,zKey); - if(!zXKey) return -3/*OOM*/; - const jKey = wasm.cstringToJs(zXKey); - const jV = kvvfsStorage(zClass).getItem(jKey); - if(!jV) return -1; - const nV = jV.length /* Note that we are relying 100% on v being - ASCII so that jV.length is equal to the - C-string's byte length. */; - if(nBuf<=0) return nV; - else if(1===nBuf){ - wasm.setMemValue(zBuf, 0); - return nV; + const kvvfsStorage = (zClass)=> + ((115/*=='s'*/===wasm.getMemValue(zClass)) + ? sessionStorage : localStorage); + + const kvvfsImpls = { + xRead: (zClass, zKey, zBuf, nBuf)=>{ + const stack = pstack.pointer, + astack = wasm.scopedAllocPush(); + try { + const zXKey = kvvfsMakeKey(zClass,zKey); + if(!zXKey) return -3/*OOM*/; + const jKey = wasm.cstringToJs(zXKey); + const jV = kvvfsStorage(zClass).getItem(jKey); + if(!jV) return -1; + const nV = jV.length /* Note that we are relying 100% on v being + ASCII so that jV.length is equal to the + C-string's byte length. */; + if(nBuf<=0) return nV; + else if(1===nBuf){ + wasm.setMemValue(zBuf, 0); + return nV; + } + const zV = wasm.scopedAllocCString(jV); + if(nBuf > nV + 1) nBuf = nV + 1; + wasm.heap8u().copyWithin(zBuf, zV, zV + nBuf - 1); + wasm.setMemValue(zBuf + nBuf - 1, 0); + return nBuf - 1; + }catch(e){ + console.error("kvstorageRead()",e); + return -2; + }finally{ + pstack.restore(stack); + wasm.scopedAllocPop(astack); + } + }, + xWrite: (zClass, zKey, zData)=>{ + const stack = pstack.pointer; + try { + const zXKey = kvvfsMakeKey(zClass,zKey); + if(!zXKey) return 1/*OOM*/; + const jKey = wasm.cstringToJs(zXKey); + kvvfsStorage(zClass).setItem(jKey, wasm.cstringToJs(zData)); + return 0; + }catch(e){ + console.error("kvstorageWrite()",e); + return capi.SQLITE_IOERR; + }finally{ + pstack.restore(stack); + } + }, + xDelete: (zClass, zKey)=>{ + const stack = pstack.pointer; + try { + const zXKey = kvvfsMakeKey(zClass,zKey); + if(!zXKey) return 1/*OOM*/; + kvvfsStorage(zClass).removeItem(wasm.cstringToJs(zXKey)); + return 0; + }catch(e){ + console.error("kvstorageDelete()",e); + return capi.SQLITE_IOERR; + }finally{ + pstack.restore(stack); } - const zV = wasm.scopedAllocCString(jV); - if(nBuf > nV + 1) nBuf = nV + 1; - wasm.heap8u().copyWithin(zBuf, zV, zV + nBuf - 1); - wasm.setMemValue(zBuf + nBuf - 1, 0); - return nBuf - 1; - }catch(e){ - console.error("kvstorageRead()",e); - return -2; - }finally{ - pstack.restore(stack); - wasm.scopedAllocPop(astack); - } - }, - xWrite: (zClass, zKey, zData)=>{ - const stack = pstack.pointer; - try { - const zXKey = kvvfsMakeKey(zClass,zKey); - if(!zXKey) return 1/*OOM*/; - const jKey = wasm.cstringToJs(zXKey); - kvvfsStorage(zClass).setItem(jKey, wasm.cstringToJs(zData)); - return 0; - }catch(e){ - console.error("kvstorageWrite()",e); - return capi.SQLITE_IOERR; - }finally{ - pstack.restore(stack); - } - }, - xDelete: (zClass, zKey)=>{ - const stack = pstack.pointer; - try { - const zXKey = kvvfsMakeKey(zClass,zKey); - if(!zXKey) return 1/*OOM*/; - kvvfsStorage(zClass).removeItem(wasm.cstringToJs(zXKey)); - return 0; - }catch(e){ - console.error("kvstorageDelete()",e); - return capi.SQLITE_IOERR; - }finally{ - pstack.restore(stack); } + }/*kvvfsImpls*/; + for(let k of Object.keys(kvvfsImpls)){ + kvvfsMethods[kvvfsMethods.memberKey(k)] = + wasm.installFunction( + kvvfsMethods.memberSignature(k), + kvvfsImpls[k] + ); } - }/*kvvfsImpls*/; - for(let k of Object.keys(kvvfsImpls)){ - kvvfsMethods[kvvfsMethods.memberKey(k)] = - wasm.installFunction( - kvvfsMethods.memberSignature(k), - kvvfsImpls[k] - ); + }else{ + /* Worker thread: unregister kvvfs to avoid it being used + for anything other than local/sessionStorage. It "can" + be used that way but it's not really intended to be. */ + capi.sqlite3_vfs_unregister(pKvvfs); } - }/*kvvfs*/ + }/*pKvvfs*/ - sqlite3.version = Object.assign(Object.create(null),{ - library: sqlite3.capi.sqlite3_libversion(), - sourceId: sqlite3.capi.sqlite3_sourceid() - }); }); - diff --git a/ext/wasm/api/sqlite3-api-oo1.js b/ext/wasm/api/sqlite3-api-oo1.js index fa6c6d8e5f..20210a03fb 100644 --- a/ext/wasm/api/sqlite3-api-oo1.js +++ b/ext/wasm/api/sqlite3-api-oo1.js @@ -45,8 +45,10 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ /** If object opts has _its own_ property named p then that property's value is returned, else dflt is returned. */ - const getOwnOption = (opts, p, dflt)=> - opts.hasOwnProperty(p) ? opts[p] : dflt; + const getOwnOption = (opts, p, dflt)=>{ + const d = Object.getOwnPropertyDescriptor(opts,p); + return d ? d.value : dflt; + }; // Documented in DB.checkRc() const checkSqlite3Rc = function(dbPtr, sqliteResultCode){ @@ -85,7 +87,7 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ being-construct DB object as its "this". See the DB constructor for the argument docs. This is split into a separate function in order to enable simple creation of special-case DB constructors, - e.g. JsStorageDB and OpfsDB. + e.g. JsStorageDb and OpfsDb. Expects to be passed a configuration object with the following properties: @@ -147,13 +149,10 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ if( flagsStr.indexOf('w')>=0 ) oflags |= capi.SQLITE_OPEN_READWRITE; if( 0===oflags ) oflags |= capi.SQLITE_OPEN_READONLY; oflags |= capi.SQLITE_OPEN_EXRESCODE; - const scope = wasm.scopedAllocPush(); + const stack = wasm.pstack.pointer; try { - const pPtr = wasm.allocPtr() /* output (sqlite3**) arg */; - const pVfsName = vfsName ? ( - ('number'===typeof vfsName ? vfsName : wasm.scopedAllocCString(vfsName)) - ): 0; - let rc = capi.sqlite3_open_v2(fn, pPtr, oflags, pVfsName); + const pPtr = wasm.pstack.allocPtr() /* output (sqlite3**) arg */; + let rc = capi.sqlite3_open_v2(fn, pPtr, oflags, vfsName || 0); pDb = wasm.getPtrValue(pPtr); checkSqlite3Rc(pDb, rc); if(flagsStr.indexOf('t')>=0){ @@ -161,21 +160,19 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ __dbTraceToConsole, 0); } // Check for per-VFS post-open SQL... - wasm.setPtrValue(pPtr, 0); - if(0===capi.sqlite3_file_control( - pDb, "main", capi.SQLITE_FCNTL_VFS_POINTER, pPtr - )){ - const postInitSql = __vfsPostOpenSql[wasm.getPtrValue(pPtr)]; - if(postInitSql){ - rc = capi.sqlite3_exec(pDb, postInitSql, 0, 0, 0); - checkSqlite3Rc(pDb, rc); - } + const pVfs = capi.sqlite3_js_db_vfs(pDb); + //console.warn("Opened db",fn,"with vfs",vfsName,pVfs); + if(!pVfs) toss3("Internal error: cannot get VFS for new db handle."); + const postInitSql = __vfsPostOpenSql[pVfs]; + if(postInitSql){ + rc = capi.sqlite3_exec(pDb, postInitSql, 0, 0, 0); + checkSqlite3Rc(pDb, rc); } }catch( e ){ if( pDb ) capi.sqlite3_close_v2(pDb); throw e; }finally{ - wasm.scopedAllocPop(scope); + wasm.pstack.restore(stack); } this.filename = fnJs; __ptrMap.set(this, pDb); @@ -374,11 +371,11 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ case 1: if('string'===typeof args[0] || util.isSQLableTypedArray(args[0])){ out.sql = args[0]; + }else if(Array.isArray(args[0])){ + out.sql = args[0]; }else if(args[0] && 'object'===typeof args[0]){ out.opt = args[0]; out.sql = out.opt.sql; - }else if(Array.isArray(args[0])){ - out.sql = args[0]; } break; case 2: @@ -392,7 +389,7 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ }else if(Array.isArray(out.sql)){ out.sql = out.sql.join(''); }else if('string'!==typeof out.sql){ - toss3("Missing SQL argument."); + toss3("Missing SQL argument or unsupported SQL value type."); } if(out.opt.callback || out.opt.resultRows){ switch((undefined===out.opt.rowMode) @@ -810,12 +807,14 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ `xFunc` to align with the C API documentation). In the final case, the function's name must be the 'name' property. - This can currently only be used to create scalar functions, not - aggregate or window functions (requires only a bit of - refactoring to support aggregates and window functions). + The first two call forms can only be used for creating scalar + functions. Creating an aggregate or window function requires + the options-object form (see below for details). UDFs cannot currently be removed from a DB handle after they're - added. + added. More correctly, they can be removed as documented for + sqlite3_create_function_v2(), but doing so will "leak" the + JS-created WASM binding of those functions. On success, returns this object. Throws on error. @@ -825,71 +824,148 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ cannot be determined. The docs for sqlite3_create_function_v2() describe the conversions in more detail. - The optional options object may contain flags to modify how + The values set in the options object differ for scalar and + aggregate functions: + + - Scalar: set the `xFunc` function-type property to the UDF + function. + + - Aggregate: set the `xStep` and `xFinal` function-type + properties to the "step" and "final" callbacks for the + aggregate. Do not set the `xFunc` property. + + - Window: set the `xStep`, `xFinal`, `xValue`, and `xInverse` + function-type properties. Do not set the `xFunc` property. + + The options object may optionally have an `xDestroy` + function-type property, as per sqlite3_create_function_v2(). + Its argument will be the WASM-pointer-type value of the `pApp` + property, and this function will throw if `pApp` is defined but + is not null, undefined, or a numeric (WASM pointer) + value. i.e. `pApp`, if set, must be value suitable for use as a + WASM pointer argument, noting that `null` or `undefined` will + translate to 0 for that purpose. + + The options object may contain flags to modify how the function is defined: - - .arity: the number of arguments which SQL calls to this - function expect or require. The default value is - `callback.length` (i.e. the number of declared parameters it - has) **MINUS 1** (see below for why). As a special case, if - callback.length is 0, its arity is also 0 instead of -1. A - negative arity value means that the function is variadic and - may accept any number of arguments, up to sqlite3's - compile-time limits. sqlite3 will enforce the argument count if - is zero or greater. - - The callback always receives a pointer to an `sqlite3_context` - object as its first argument. Any arguments after that are from - SQL code. The leading context argument does _not_ count towards - the function's arity. See the docs for + - `arity`: the number of arguments which SQL calls to this + function expect or require. The default value is `xFunc.length` + or `xStep.length` (i.e. the number of declared parameters it + has) **MINUS 1** (see below for why). As a special case, if the + `length` is 0, its arity is also 0 instead of -1. A negative + arity value means that the function is variadic and may accept + any number of arguments, up to sqlite3's compile-time + limits. sqlite3 will enforce the argument count if is zero or + greater. The callback always receives a pointer to an + `sqlite3_context` object as its first argument. Any arguments + after that are from SQL code. The leading context argument does + _not_ count towards the function's arity. See the docs for sqlite3.capi.sqlite3_create_function_v2() for why that argument is needed in the interface. - The following properties correspond to flags documented at: + The following options-object properties correspond to flags + documented at: https://sqlite.org/c3ref/create_function.html - - .deterministic = SQLITE_DETERMINISTIC - - .directOnly = SQLITE_DIRECTONLY - - .innocuous = SQLITE_INNOCUOUS - - TODO: for the (optionsObject) form, accept callbacks for - aggregate and window functions. + - `deterministic` = sqlite3.capi.SQLITE_DETERMINISTIC + - `directOnly` = sqlite3.capi.SQLITE_DIRECTONLY + - `innocuous` = sqlite3.capi.SQLITE_INNOCUOUS + Sidebar: the ability to add new WASM-accessible functions to + the runtime requires that the WASM build is compiled with the + equivalent functionality as that provided by Emscripten's + `-sALLOW_TABLE_GROWTH` flag. */ - createFunction: function f(name, callback, opt){ + createFunction: function f(name, xFunc, opt){ + const isFunc = (f)=>(f instanceof Function); switch(arguments.length){ case 1: /* (optionsObject) */ opt = name; name = opt.name; - callback = opt.xFunc || opt.callback; + xFunc = opt.xFunc || 0; break; case 2: /* (name, callback|optionsObject) */ - if(!(callback instanceof Function)){ - opt = callback; - callback = opt.xFunc || opt.callback; + if(!isFunc(xFunc)){ + opt = xFunc; + xFunc = opt.xFunc || 0; } break; + case 3: /* name, xFunc, opt */ + break; default: break; } if(!opt) opt = {}; - if(!(callback instanceof Function)){ - toss3("Invalid arguments: expecting a callback function."); - }else if('string' !== typeof name){ + if('string' !== typeof name){ toss3("Invalid arguments: missing function name."); } + let xStep = opt.xStep || 0; + let xFinal = opt.xFinal || 0; + const xValue = opt.xValue || 0; + const xInverse = opt.xInverse || 0; + let isWindow = undefined; + if(isFunc(xFunc)){ + isWindow = false; + if(isFunc(xStep) || isFunc(xFinal)){ + toss3("Ambiguous arguments: scalar or aggregate?"); + } + xStep = xFinal = null; + }else if(isFunc(xStep)){ + if(!isFunc(xFinal)){ + toss3("Missing xFinal() callback for aggregate or window UDF."); + } + xFunc = null; + }else if(isFunc(xFinal)){ + toss3("Missing xStep() callback for aggregate or window UDF."); + }else{ + toss3("Missing function-type properties."); + } + if(false === isWindow){ + if(isFunc(xValue) || isFunc(xInverse)){ + toss3("xValue and xInverse are not permitted for non-window UDFs."); + } + }else if(isFunc(xValue)){ + if(!isFunc(xInverse)){ + toss3("xInverse must be provided if xValue is."); + } + isWindow = true; + }else if(isFunc(xInverse)){ + toss3("xValue must be provided if xInverse is."); + } + const pApp = opt.pApp; + if(undefined!==pApp && + null!==pApp && + (('number'!==typeof pApp) || !capi.util.isInt32(pApp))){ + toss3("Invalid value for pApp property. Must be a legal WASM pointer value."); + } + const xDestroy = opt.xDestroy || 0; + if(xDestroy && !isFunc(xDestroy)){ + toss3("xDestroy property must be a function."); + } let fFlags = 0 /*flags for sqlite3_create_function_v2()*/; if(getOwnOption(opt, 'deterministic')) fFlags |= capi.SQLITE_DETERMINISTIC; if(getOwnOption(opt, 'directOnly')) fFlags |= capi.SQLITE_DIRECTONLY; if(getOwnOption(opt, 'innocuous')) fFlags |= capi.SQLITE_INNOCUOUS; name = name.toLowerCase(); - DB.checkRc(this, capi.sqlite3_create_function_v2( - this.pointer, name, - (opt.hasOwnProperty('arity') - ? +opt.arity - : (callback.length ? callback.length-1/*for pCtx arg*/ : 0)), - capi.SQLITE_UTF8 | fFlags, null/*pApp*/, callback, - null/*xStep*/, null/*xFinal*/, null/*xDestroy*/)); + const xArity = xFunc || xStep; + const arity = getOwnOption(opt, 'arity'); + const arityArg = ('number'===typeof arity + ? arity + : (xArity.length ? xArity.length-1/*for pCtx arg*/ : 0)); + let rc; + if( isWindow ){ + rc = capi.sqlite3_create_window_function( + this.pointer, name, arityArg, + capi.SQLITE_UTF8 | fFlags, pApp || 0, + xStep, xFinal, xValue, xInverse, xDestroy); + }else{ + rc = capi.sqlite3_create_function_v2( + this.pointer, name, arityArg, + capi.SQLITE_UTF8 | fFlags, pApp || 0, + xFunc, xStep, xFinal, xDestroy); + } + DB.checkRc(this, rc); return this; }/*createFunction()*/, /** @@ -1358,7 +1434,7 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ default: this._mayGet = false; console.warn("sqlite3_step() rc=",rc, - capi.sqlite3_web_rc_str(rc), + capi.sqlite3_js_rc_str(rc), "SQL =", capi.sqlite3_sql(this.pointer)); DB.checkRc(this.db.pointer, rc); } @@ -1596,7 +1672,7 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ dbCtorHelper }/*oo1 object*/; - if(util.isMainWindow()){ + if(util.isUIThread()){ /** Functionally equivalent to DB(storageName,'c','kvvfs') except that it throws if the given storage name is not one of 'local' @@ -1614,8 +1690,8 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ }; const jdb = sqlite3.oo1.JsStorageDb; jdb.prototype = Object.create(DB.prototype); - /** Equivalent to sqlite3_web_kvvfs_clear(). */ - jdb.clearStorage = capi.sqlite3_web_kvvfs_clear; + /** Equivalent to sqlite3_js_kvvfs_clear(). */ + jdb.clearStorage = capi.sqlite3_js_kvvfs_clear; /** Clears this database instance's storage or throws if this instance has been closed. Returns the number of @@ -1624,8 +1700,8 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ jdb.prototype.clearStorage = function(){ return jdb.clearStorage(affirmDbOpen(this).filename); }; - /** Equivalent to sqlite3_web_kvvfs_size(). */ - jdb.storageSize = capi.sqlite3_web_kvvfs_size; + /** Equivalent to sqlite3_js_kvvfs_size(). */ + jdb.storageSize = capi.sqlite3_js_kvvfs_size; /** Returns the _approximate_ number of bytes this database takes up in its storage or throws if this instance has been closed. diff --git a/ext/wasm/api/sqlite3-api-opfs.js b/ext/wasm/api/sqlite3-api-opfs.js index 98defe4fbc..3f27941e8b 100644 --- a/ext/wasm/api/sqlite3-api-opfs.js +++ b/ext/wasm/api/sqlite3-api-opfs.js @@ -15,14 +15,17 @@ asynchronous Origin-Private FileSystem (OPFS) APIs using a second Worker, implemented in sqlite3-opfs-async-proxy.js. This file is intended to be appended to the main sqlite3 JS deliverable somewhere - after sqlite3-api-glue.js and before sqlite3-api-cleanup.js. + after sqlite3-api-oo1.js and before sqlite3-api-cleanup.js. */ 'use strict'; self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ /** - installOpfsVfs() returns a Promise which, on success, installs - an sqlite3_vfs named "opfs", suitable for use with all sqlite3 APIs - which accept a VFS. It uses the Origin-Private FileSystem API for + installOpfsVfs() returns a Promise which, on success, installs an + sqlite3_vfs named "opfs", suitable for use with all sqlite3 APIs + which accept a VFS. It is intended to be called via + sqlite3ApiBootstrap.initializersAsync or an equivalent mechanism. + + The installed VFS uses the Origin-Private FileSystem API for all file storage. On error it is rejected with an exception explaining the problem. Reasons for rejection include, but are not limited to: @@ -48,31 +51,31 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ proxying OPFS's synchronous API via the synchronous interface required by the sqlite3_vfs API. - - This function may only be called a single time and it must be - called from the client, as opposed to the library initialization, - in case the client requires a custom path for this API's - "counterpart": this function's argument is the relative URI to - this module's "asynchronous half". When called, this function removes - itself from the sqlite3 object. + - This function may only be called a single time. When called, this + function removes itself from the sqlite3 object. - The argument may optionally be a plain object with the following - configuration options: + All arguments to this function are for internal/development purposes + only. They do not constitute a public API and may change at any + time. - - proxyUri: as described above + The argument may optionally be a plain object with the following + configuration options: - - verbose (=2): an integer 0-3. 0 disables all logging, 1 enables - logging of errors. 2 enables logging of warnings and errors. 3 - additionally enables debugging info. + - proxyUri: as described above - - sanityChecks (=false): if true, some basic sanity tests are - run on the OPFS VFS API after it's initialized, before the - returned Promise resolves. + - verbose (=2): an integer 0-3. 0 disables all logging, 1 enables + logging of errors. 2 enables logging of warnings and errors. 3 + additionally enables debugging info. - On success, the Promise resolves to the top-most sqlite3 namespace - object and that object gets a new object installed in its - `opfs` property, containing several OPFS-specific utilities. + - sanityChecks (=false): if true, some basic sanity tests are + run on the OPFS VFS API after it's initialized, before the + returned Promise resolves. + + On success, the Promise resolves to the top-most sqlite3 namespace + object and that object gets a new object installed in its + `opfs` property, containing several OPFS-specific utilities. */ -const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ +const installOpfsVfs = function callee(options){ if(!self.SharedArrayBuffer || !self.Atomics || !self.FileSystemHandle || @@ -84,9 +87,9 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ new Error("This environment does not have OPFS support.") ); } - const options = (asyncProxyUri && 'object'===asyncProxyUri) ? asyncProxyUri : { - proxyUri: asyncProxyUri - }; + if(!options || 'object'!==typeof options){ + options = Object.create(null); + } const urlParams = new URL(self.location.href).searchParams; if(undefined===options.verbose){ options.verbose = urlParams.has('opfs-verbose') ? 3 : 2; @@ -98,6 +101,9 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ options.proxyUri = callee.defaultProxyUri; } + if('function' === typeof options.proxyUri){ + options.proxyUri = options.proxyUri(); + } const thePromise = new Promise(function(promiseResolve, promiseReject_){ const loggers = { 0:console.error.bind(console), @@ -110,7 +116,6 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ const log = (...args)=>logImpl(2, ...args); const warn = (...args)=>logImpl(1, ...args); const error = (...args)=>logImpl(0, ...args); - //warn("The OPFS VFS feature is very much experimental and under construction."); const toss = function(...args){throw new Error(args.join(' '))}; const capi = sqlite3.capi; const wasm = capi.wasm; @@ -155,9 +160,6 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ s.count = s.time = 0; s = metrics.s11n.deserialize = Object.create(null); s.count = s.time = 0; - //[ // timed routines which are not in state.opIds - // 'xFileControl' - //].forEach((k)=>r(metrics[k] = Object.create(null))); } }/*metrics*/; const promiseReject = function(err){ @@ -220,32 +222,36 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ cases. We should probably have one SAB here with a single slot for locking a per-file initialization step and then allocate a separate SAB like the above one for each file. That will - require a bit of acrobatics but should be feasible. + require a bit of acrobatics but should be feasible. The most + problematic part is that xOpen() would have to use + postMessage() to communicate its SharedArrayBuffer, and mixing + that approach with Atomics.wait/notify() gets a bit messy. */ const state = Object.create(null); state.verbose = options.verbose; state.littleEndian = (()=>{ const buffer = new ArrayBuffer(2); - new DataView(buffer).setInt16(0, 256, true /* littleEndian */); + new DataView(buffer).setInt16(0, 256, true /* ==>littleEndian */); // Int16Array uses the platform's endianness. return new Int16Array(buffer)[0] === 256; })(); - /** Whether the async counterpart should log exceptions to - the serialization channel. That produces a great deal of - noise for seemingly innocuous things like xAccess() checks - for missing files, so this option may have one of 3 values: + /** + Whether the async counterpart should log exceptions to + the serialization channel. That produces a great deal of + noise for seemingly innocuous things like xAccess() checks + for missing files, so this option may have one of 3 values: - 0 = no exception logging + 0 = no exception logging - 1 = only log exceptions for "significant" ops like xOpen(), - xRead(), and xWrite(). + 1 = only log exceptions for "significant" ops like xOpen(), + xRead(), and xWrite(). - 2 = log all exceptions. + 2 = log all exceptions. */ state.asyncS11nExceptions = 1; - /* Size of file I/O buffer block. 64k = max sqlite3 page size. */ - state.fileBufferSize = - 1024 * 64; + /* Size of file I/O buffer block. 64k = max sqlite3 page size, and + xRead/xWrite() will never deal in blocks larger than that. */ + state.fileBufferSize = 1024 * 64; state.sabS11nOffset = state.fileBufferSize; /** The size of the block in our SAB for serializing arguments and @@ -255,7 +261,8 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ */ state.sabS11nSize = opfsVfs.$mxPathname * 2; /** - The SAB used for all data I/O (files and arg/result s11n). + The SAB used for all data I/O between the synchronous and + async halves (file i/o and arg/result s11n). */ state.sabIO = new SharedArrayBuffer( state.fileBufferSize/* file i/o block */ @@ -294,7 +301,13 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ state.opIds.mkdir = i++; state.opIds['opfs-async-metrics'] = i++; state.opIds['opfs-async-shutdown'] = i++; - state.sabOP = new SharedArrayBuffer(i * 4/*sizeof int32*/); + /* The retry slot is used by the async part for wait-and-retry + semantics. Though we could hypothetically use the xSleep slot + for that, doing so might lead to undesired side effects. */ + state.opIds.retry = i++; + state.sabOP = new SharedArrayBuffer( + i * 4/* ==sizeof int32, noting that Atomics.wait() and friends + can only function on Int32Array views of an SAB. */); opfsUtil.metrics.reset(); } /** @@ -335,9 +348,12 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ state.s11n.serialize(...args); Atomics.store(state.sabOPView, state.opIds.rc, -1); Atomics.store(state.sabOPView, state.opIds.whichOp, opNdx); - Atomics.notify(state.sabOPView, state.opIds.whichOp) /* async thread will take over here */; + Atomics.notify(state.sabOPView, state.opIds.whichOp) + /* async thread will take over here */; const t = performance.now(); - Atomics.wait(state.sabOPView, state.opIds.rc, -1); + Atomics.wait(state.sabOPView, state.opIds.rc, -1) + /* When this wait() call returns, the async half will have + completed the operation and reported its results. */; const rc = Atomics.load(state.sabOPView, state.opIds.rc); metrics[op].wait += performance.now() - t; if(rc && state.asyncS11nExceptions){ @@ -349,17 +365,22 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ const initS11n = ()=>{ /** - ACHTUNG: this code is 100% duplicated in the other half of this - proxy! The documentation is maintained in the "synchronous half". + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + ACHTUNG: this code is 100% duplicated in the other half of + this proxy! The documentation is maintained in the + "synchronous half". + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! This proxy de/serializes cross-thread function arguments and output-pointer values via the state.sabIO SharedArrayBuffer, using the region defined by (state.sabS11nOffset, state.sabS11nOffset]. Only one dataset is recorded at a time. - This is not a general-purpose format. It only supports the range - of operations, and data sizes, needed by the sqlite3_vfs and - sqlite3_io_methods operations. + This is not a general-purpose format. It only supports the + range of operations, and data sizes, needed by the + sqlite3_vfs and sqlite3_io_methods operations. Serialized + data are transient and this serialization algorithm may + change at any time. The data format can be succinctly summarized as: @@ -383,7 +404,8 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ using their TextEncoder/TextDecoder representations. It would arguably make more sense to store them as Int16Arrays of their JS character values, but how best/fastest to get that - in and out of string form us an open point. + in and out of string form is an open point. Initial + experimentation with that approach did not gain us any speed. Historical note: this impl was initially about 1% this size by using using JSON.stringify/parse(), but using fit-to-purpose @@ -580,9 +602,11 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ toss("Member",name," is not a function pointer. Signature =",sigN); } const memKey = tgt.memberKey(name); - //log("installMethod",tgt, name, sigN); const fProxy = 0 - // We can remove this proxy middle-man once the VFS is working + /** This middle-man proxy is only for use during development, to + confirm that we always pass the proper number of + arguments. We know that the C-level code will always use the + correct argument count. */ ? callee.argcProxy(func, sigN) : func; const pFunc = wasm.installFunction(fProxy, tgt.memberSignature(name, true)); @@ -603,7 +627,6 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ const mTimeStart = (op)=>{ opTimer.start = performance.now(); opTimer.op = op; - //metrics[op] || toss("Maintenance required: missing metrics for",op); ++metrics[op].count; }; const mTimeEnd = ()=>( @@ -616,8 +639,15 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ */ const ioSyncWrappers = { xCheckReservedLock: function(pFile,pOut){ - // Exclusive lock is automatically acquired when opened - //warn("xCheckReservedLock(",arguments,") is a no-op"); + /** + As of late 2022, only a single lock can be held on an OPFS + file. We have no way of checking whether any _other_ db + connection has a lock except by trying to obtain and (on + success) release a sync-handle for it, but doing so would + involve an inherent race condition. For the time being, + pending a better solution, we simply report whether the + given pFile instance has a lock. + */ const f = __openFiles[pFile]; wasm.setMemValue(pOut, f.lockMode ? 1 : 0, 'i32'); return 0; @@ -670,14 +700,17 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ return rc; }, xRead: function(pFile,pDest,n,offset64){ - /* int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst) */ mTimeStart('xRead'); const f = __openFiles[pFile]; let rc; try { rc = opRun('xRead',pFile, n, Number(offset64)); - if(0===rc || capi.SQLITE_IOERR_SHORT_READ===rc){ - // set() seems to be the fastest way to copy this... + if(0===rc || capi.SQLITE_IOERR_SHORT_READ===rc){ + /** + Results get written to the SharedArrayBuffer f.sabView. + Because the heap is _not_ a SharedArrayBuffer, we have + to copy the results. TypedArray.set() seems to be the + fastest way to copy this. */ wasm.heap8u().set(f.sabView.subarray(0, n), pDest); } }catch(e){ @@ -710,7 +743,6 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ return rc; }, xWrite: function(pFile,pSrc,n,offset64){ - /* int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst) */ mTimeStart('xWrite'); const f = __openFiles[pFile]; let rc; @@ -776,28 +808,6 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ //xSleep is optionally defined below xOpen: function f(pVfs, zName, pFile, flags, pOutFlags){ mTimeStart('xOpen'); - if(!f._){ - f._ = { - fileTypes: { - SQLITE_OPEN_MAIN_DB: 'mainDb', - SQLITE_OPEN_MAIN_JOURNAL: 'mainJournal', - SQLITE_OPEN_TEMP_DB: 'tempDb', - SQLITE_OPEN_TEMP_JOURNAL: 'tempJournal', - SQLITE_OPEN_TRANSIENT_DB: 'transientDb', - SQLITE_OPEN_SUBJOURNAL: 'subjournal', - SQLITE_OPEN_SUPER_JOURNAL: 'superJournal', - SQLITE_OPEN_WAL: 'wal' - }, - getFileType: function(filename,oflags){ - const ft = f._.fileTypes; - for(let k of Object.keys(ft)){ - if(oflags & capi[k]) return ft[k]; - } - warn("Cannot determine fileType based on xOpen() flags for file",filename); - return '???'; - } - }; - } if(0===zName){ zName = randomFilename(); }else if('number'===typeof zName){ @@ -1081,20 +1091,26 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){ promiseReject(e); error("Unexpected message from the async worker:",data); break; - } - }; - + }/*switch(data.type)*/ + }/*W.onmessage()*/; })/*thePromise*/; return thePromise; }/*installOpfsVfs()*/; installOpfsVfs.defaultProxyUri = - //self.location.pathname.replace(/[^/]*$/, "sqlite3-opfs-async-proxy.js"); "sqlite3-opfs-async-proxy.js"; -//console.warn("sqlite3.installOpfsVfs.defaultProxyUri =",sqlite3.installOpfsVfs.defaultProxyUri); self.sqlite3ApiBootstrap.initializersAsync.push(async (sqlite3)=>{ + if(sqlite3.scriptInfo && !sqlite3.scriptInfo.isWorker){ + return; + } try{ + let proxyJs = installOpfsVfs.defaultProxyUri; + if(sqlite3.scriptInfo.sqlite3Dir){ + installOpfsVfs.defaultProxyUri = + sqlite3.scriptInfo.sqlite3Dir + proxyJs; + //console.warn("installOpfsVfs.defaultProxyUri =",installOpfsVfs.defaultProxyUri); + } return installOpfsVfs().catch((e)=>{ - console.warn("Ignoring inability to install OPFS sqlite3_vfs:",e); + console.warn("Ignoring inability to install OPFS sqlite3_vfs:",e.message); }); }catch(e){ console.error("installOpfsVfs() exception:",e); diff --git a/ext/wasm/api/sqlite3-api-prologue.js b/ext/wasm/api/sqlite3-api-prologue.js index a267e93454..1fcc9a64ab 100644 --- a/ext/wasm/api/sqlite3-api-prologue.js +++ b/ext/wasm/api/sqlite3-api-prologue.js @@ -280,8 +280,16 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( this.name = 'WasmAllocError'; } }; + /** + Functionally equivalent to the WasmAllocError constructor but may + be used as part of an expression, e.g.: + + ``` + return someAllocatingFunction(x) || WasmAllocError.toss(...); + ``` + */ WasmAllocError.toss = (...args)=>{ - throw new WasmAllocError(args.join(' ')); + throw new WasmAllocError(...args); }; /** @@ -508,6 +516,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( "flexible-string" argument converter. */ sqlite3_exec: (pDb, sql, callback, pVoid, pErrMsg)=>{}/*installed later*/, + /** Various internal-use utilities are added here as needed. They are bound to an object only so that we have access to them in @@ -521,9 +530,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( isBindableTypedArray, isInt32, isSQLableTypedArray, isTypedArray, typedArrayToString, - isMainWindow: ()=>{ - return self.window===self && self.document; - } + isUIThread: ()=>'undefined'===typeof WorkerGlobalScope }, /** @@ -830,7 +837,8 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( ["sqlite3_value_text", "string", "sqlite3_value*"], ["sqlite3_value_type", "int", "sqlite3_value*"], ["sqlite3_vfs_find", "*", "string"], - ["sqlite3_vfs_register", "int", "*", "int"] + ["sqlite3_vfs_register", "int", "sqlite3_vfs*", "int"], + ["sqlite3_vfs_unregister", "int", "sqlite3_vfs*"] ]/*wasm.bindingSignatures*/; if(false && wasm.compileOptionUsed('SQLITE_ENABLE_NORMALIZE')){ @@ -861,13 +869,12 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( /** Functions which are intended solely for API-internal use by the WASM components, not client code. These get installed into - wasm. - - TODO: get rid of sqlite3_wasm_vfs_unlink(). It is ill-conceived - and only rarely actually useful. + capi.wasm. */ wasm.bindingSignatures.wasm = [ - ["sqlite3_wasm_vfs_unlink", "int", "string"] + ["sqlite3_wasm_db_reset", "int", "sqlite3*"], + ["sqlite3_wasm_db_vfs", "sqlite3_vfs*", "sqlite3*","string"], + ["sqlite3_wasm_vfs_unlink", "int", "sqlite3_vfs*","string"] ]; @@ -904,8 +911,8 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( */ wasm.pstack = Object.assign(Object.create(null),{ /** - Sets the current ppstack position to the given pointer. - Results are undefined if the passed-in value did not come from + Sets the current pstack position to the given pointer. Results + are undefined if the passed-in value did not come from this.pointer. */ restore: wasm.exports.sqlite3_wasm_pstack_restore, @@ -919,7 +926,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( This method always adjusts the given value to be a multiple of 8 bytes because failing to do so can lead to incorrect results when reading and writing 64-bit values from/to the WASM - heap. + heap. Similarly, the returned address is always 8-byte aligned. */ alloc: (n)=>{ return wasm.exports.sqlite3_wasm_pstack_alloc(n) @@ -927,7 +934,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( "bytes from the pstack."); }, /** - Allocates n chunks, each sz bytes, as a single memory block and + alloc()'s n chunks, each sz bytes, as a single memory block and returns the addresses as an array of n element, each holding the address of one chunk. @@ -965,7 +972,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( will corrupt or read neighboring memory. However, when all pointers involved point to "small" data, it - is safe to pass a falsy value to save to memory. + is safe to pass a falsy value to save a tiny bit of memory. */ allocPtr: (n=1,safePtrSize=true)=>{ return 1===n @@ -1018,15 +1025,31 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( class SQLite3Error extends Error { /** Constructs this object with a message equal to all arguments - concatenated with a space between each one. + concatenated with a space between each one. As a special case, + if it's passed only a single integer argument, the string form + of that argument is the result of + sqlite3.capi.sqlite3_js_rc_str() or (if that returns falsy), a + synthesized string which contains that integer. */ constructor(...args){ - super(args.join(' ')); + if(1===args.length && 'number'===typeof args[0] && args[0]===(args[0] | 0)){ + super(capi.sqlite3_js_rc_str(args[0]) || ("Unknown result code #"+args[0])); + }else{ + super(args.join(' ')); + } this.name = 'SQLite3Error'; } }; + /** + Functionally equivalent to the SQLite3Error constructor but may + be used as part of an expression, e.g.: + + ``` + return someFunction(x) || SQLite3Error.toss(...); + ``` + */ SQLite3Error.toss = (...args)=>{ - throw new SQLite3Error(args.join(' ')); + throw new SQLite3Error(...args); }; /** State for sqlite3_wasmfs_opfs_dir(). */ @@ -1074,12 +1097,8 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( Returns true if sqlite3.capi.sqlite3_wasmfs_opfs_dir() is a non-empty string and the given name starts with (that string + '/'), else returns false. - - Potential (but arguable) TODO: return true if the name is one of - (":localStorage:", "local", ":sessionStorage:", "session") and - kvvfs is available. */ - capi.sqlite3_web_filename_is_persistent = function(name){ + capi.sqlite3_wasmfs_filename_is_persistent = function(name){ const p = capi.sqlite3_wasmfs_opfs_dir(); return (p && name) ? name.startsWith(p+'/') : false; }; @@ -1101,8 +1120,10 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( pointer. The 3rd argument specifies the database name of the given database connection to check, defaulting to the main db. - The 2nd and 3rd arguments may either be a JS string or a C-string - allocated from the wasm environment. + The 2nd and 3rd arguments may either be a JS string or a WASM + C-string. If the 2nd argument is a NULL WASM pointer, the default + VFS is assumed. If the 3rd is a NULL WASM pointer, "main" is + assumed. The truthy value it returns is a pointer to the `sqlite3_vfs` object. @@ -1112,22 +1133,14 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( bad arguments cause a conversion error when passing into wasm-space, false is returned. */ - capi.sqlite3_web_db_uses_vfs = function(pDb,vfsName,dbName="main"){ + capi.sqlite3_js_db_uses_vfs = function(pDb,vfsName,dbName="main"){ try{ const pK = capi.sqlite3_vfs_find(vfsName); if(!pK) return false; else if(!pDb){ - return capi.sqlite3_vfs_find(0)===pK ? pK : false; - } - const ppVfs = wasm.allocPtr(); - try{ - return ( - (0===capi.sqlite3_file_control( - pDb, dbName, capi.SQLITE_FCNTL_VFS_POINTER, ppVfs - )) && (wasm.getPtrValue(ppVfs) === pK) - ) ? pK : false; - }finally{ - wasm.dealloc(ppVfs); + return pK===capi.sqlite3_vfs_find(0) ? pK : false; + }else{ + return pK===capi.sqlite3_js_db_vfs(pDb) ? pK : false; } }catch(e){ /* Ignore - probably bad args to a wasm-bound function. */ @@ -1139,7 +1152,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( Returns an array of the names of all currently-registered sqlite3 VFSes. */ - capi.sqlite3_web_vfs_list = function(){ + capi.sqlite3_js_vfs_list = function(){ const rc = []; let pVfs = capi.sqlite3_vfs_find(0); while(pVfs){ @@ -1156,9 +1169,8 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( sqlite3_serialize(). On success it returns a Uint8Array. On error it throws with a description of the problem. */ - capi.sqlite3_web_db_export = function(pDb){ + capi.sqlite3_js_db_export = function(pDb){ if(!pDb) toss('Invalid sqlite3* argument.'); - const wasm = wasm; if(!wasm.bigIntEnabled) toss('BigInt64 support is not enabled.'); const stack = wasm.pstack.pointer; let pOut; @@ -1177,7 +1189,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( ); if(rc){ toss("Database serialization failed with code", - sqlite3.capi.sqlite3_web_rc_str(rc)); + sqlite3.capi.sqlite3_js_rc_str(rc)); } pOut = wasm.getPtrValue(ppOut); const nOut = wasm.getMemValue(pSize, 'i64'); @@ -1185,20 +1197,40 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( ? wasm.heap8u().slice(pOut, pOut + Number(nOut)) : new Uint8Array(); return rc; - }catch(e){ - console.error('internal error?',e); - throw w; }finally{ if(pOut) wasm.exports.sqlite3_free(pOut); wasm.pstack.restore(stack); } }; + + /** + Given a `sqlite3*` and a database name (JS string or WASM + C-string pointer, which may be 0), returns a pointer to the + sqlite3_vfs responsible for it. If the given db name is null/0, + or not provided, then "main" is assumed. + */ + capi.sqlite3_js_db_vfs = + (dbPointer, dbName=0)=>wasm.sqlite3_wasm_db_vfs(dbPointer, dbName); + + /** + A thin wrapper around capi.sqlite3_aggregate_context() which + behaves the same except that it throws a WasmAllocError if that + function returns 0. As a special case, if n is falsy it does + _not_ throw if that function returns 0. That special case is + intended for use with xFinal() implementations. + */ + capi.sqlite3_js_aggregate_context = (pCtx, n)=>{ + return capi.sqlite3_aggregate_context(pCtx, n) + || (n ? WasmAllocError.toss("Cannot allocate",n, + "bytes for sqlite3_aggregate_context()") + : 0); + }; - if( capi.util.isMainWindow() ){ + if( capi.util.isUIThread() ){ /* Features specific to the main window thread... */ /** - Internal helper for sqlite3_web_kvvfs_clear() and friends. + Internal helper for sqlite3_js_kvvfs_clear() and friends. Its argument should be one of ('local','session',''). */ const __kvvfsInfo = function(which){ @@ -1223,7 +1255,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( Returns the number of entries cleared. */ - capi.sqlite3_web_kvvfs_clear = function(which=''){ + capi.sqlite3_js_kvvfs_clear = function(which=''){ let rc = 0; const kvinfo = __kvvfsInfo(which); kvinfo.stores.forEach((s)=>{ @@ -1256,7 +1288,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( those limits are unspecified and may include per-entry overhead invisible to clients. */ - capi.sqlite3_web_kvvfs_size = function(which=''){ + capi.sqlite3_js_kvvfs_size = function(which=''){ let sz = 0; const kvinfo = __kvvfsInfo(which); kvinfo.stores.forEach((s)=>{ @@ -1281,6 +1313,17 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( SQLite3Error: SQLite3Error, capi, config, + /** + Holds the version info of the sqlite3 source tree from which + the generated sqlite3-api.js gets built. Note that its version + may well differ from that reported by sqlite3_libversion(), but + that should be considered a source file mismatch, as the JS and + WASM files are intended to be built and distributed together. + + This object is initially a placeholder which gets replaced by a + build-generated object. + */ + version: Object.create(null), /** Performs any optional asynchronous library-level initialization which might be required. This function returns a Promise which @@ -1324,7 +1367,20 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap( //while(lip.length) p = p.then(lip.shift()); //return p.then(()=>sqlite3); return Promise.all(lip).then(()=>sqlite3); - } + }, + /** + scriptInfo ideally gets injected into this object by the + infrastructure which assembles the JS/WASM module. It contains + state which must be collected before sqlite3ApiBootstrap() can + be declared. It is not necessarily available to any + sqlite3ApiBootstrap.initializers but "should" be in place (if + it's added at all) by the time that + sqlite3ApiBootstrap.initializersAsync is processed. + + This state is not part of the public API, only intended for use + with the sqlite3 API bootstrapping and wasm-loading process. + */ + scriptInfo: undefined }; try{ sqlite3ApiBootstrap.initializers.forEach((f)=>{ @@ -1400,3 +1456,4 @@ self.sqlite3ApiBootstrap.defaultConfig = Object.create(null); value which will be stored here. */ self.sqlite3ApiBootstrap.sqlite3 = undefined; + diff --git a/ext/wasm/api/sqlite3-api-worker1.js b/ext/wasm/api/sqlite3-api-worker1.js index 69bb8ced61..dea06341a0 100644 --- a/ext/wasm/api/sqlite3-api-worker1.js +++ b/ext/wasm/api/sqlite3-api-worker1.js @@ -106,7 +106,7 @@ ``` { - type: 'error', + type: "error", messageId: ...as above..., @@ -150,7 +150,7 @@ ``` { - type: 'config', + type: "config-get", messageId: ...as above..., result: { @@ -167,7 +167,7 @@ the non-WASMFS OPFS VFS to open a database via a URI-style db filename. - vfses: result of sqlite3.capi.sqlite3_web_vfs_list() + vfsList: result of sqlite3.capi.sqlite3_js_vfs_list() } } ``` @@ -196,7 +196,7 @@ ``` { - type: 'open', + type: "open", messageId: ...as above..., result: { filename: db filename, possibly differing from the input. @@ -205,7 +205,7 @@ envelope to other calls in this API to tell them which db to use. If it is not provided to future calls, they will default to operating on the first-opened db. This property is, for API - consistency's sake, also part of the contaning message envelope. + consistency's sake, also part of the containing message envelope. Only the `open` operation includes it in the `result` property. persistent: true if the given filename resides in the @@ -225,19 +225,21 @@ type: "close", messageId: ...as above... dbId: ...as above... - args: none + args: OPTIONAL {unlink: boolean} } ``` - If the dbId does not refer to an opened ID, this is a no-op. The - inability to close a db (because it's not opened) or delete its - file does not trigger an error. + If the `dbId` does not refer to an opened ID, this is a no-op. If + the `args` object contains a truthy `unlink` value then the database + will be unlinked (deleted) after closing it. The inability to close a + db (because it's not opened) or delete its file does not trigger an + error. Response: ``` { - type: 'close', + type: "close", messageId: ...as above..., result: { @@ -269,7 +271,7 @@ ``` { - type: 'exec', + type: "exec", messageId: ...as above..., dbId: ...as above... result: { @@ -365,14 +367,11 @@ sqlite3.initWorker1API = function(){ if(db){ delete this.dbs[getDbId(db)]; const filename = db.getFilename(); + const pVfs = sqlite3.capi.wasm.sqlite3_wasm_db_vfs(db.pointer, 0); db.close(); if(db===this.defaultDb) this.defaultDb = undefined; - if(alsoUnlink && filename){ - /* This isn't necessarily correct: the db might be using a - VFS other than the default. How do we best resolve this - without having to special-case the kvvfs and opfs - VFSes? */ - sqlite3.capi.wasm.sqlite3_wasm_vfs_unlink(filename); + if(alsoUnlink && filename && pVfs){ + sqlite3.capi.wasm.sqlite3_wasm_vfs_unlink(pVfs, filename); } } }, @@ -435,15 +434,13 @@ sqlite3.initWorker1API = function(){ const pDir = sqlite3.capi.sqlite3_wasmfs_opfs_dir(); if(!args.filename || ':memory:'===args.filename){ oargs.filename = args.filename || ''; - }else if(pDir){ - oargs.filename = pDir + ('/'===args.filename[0] ? args.filename : ('/'+args.filename)); }else{ oargs.filename = args.filename; } const db = wState.open(oargs); rc.filename = db.filename; - rc.persistent = (!!pDir && db.filename.startsWith(pDir)) - || sqlite3.capi.sqlite3_web_db_uses_vfs(db.pointer, "opfs"); + rc.persistent = (!!pDir && db.filename.startsWith(pDir+'/')) + || sqlite3.capi.sqlite3_js_db_uses_vfs(db.pointer, "opfs"); rc.dbId = getDbId(db); return rc; }, @@ -524,7 +521,7 @@ sqlite3.initWorker1API = function(){ }); rc.wasmfsOpfsEnabled = !!sqlite3.capi.sqlite3_wasmfs_opfs_dir(); rc.version = sqlite3.version; - rc.vfses = sqlite3.capi.sqlite3_web_vfs_list(); + rc.vfsList = sqlite3.capi.sqlite3_js_vfs_list(); return rc; }, diff --git a/ext/wasm/api/sqlite3-license-version-header.js b/ext/wasm/api/sqlite3-license-version-header.js new file mode 100644 index 0000000000..f8b3eddc5f --- /dev/null +++ b/ext/wasm/api/sqlite3-license-version-header.js @@ -0,0 +1,25 @@ +/* +** LICENSE for the sqlite3 WebAssembly/JavaScript APIs. +** +** This bundle (typically released as sqlite3.js or sqlite3-wasmfs.js) +** is an amalgamation of JavaScript source code from two projects: +** +** 1) https://emscripten.org: the Emscripten "glue code" is covered by +** the terms of the MIT license and University of Illinois/NCSA +** Open Source License, as described at: +** +** https://emscripten.org/docs/introducing_emscripten/emscripten_license.html +** +** 2) https://sqlite.org: all code and documentation labeled as being +** from this source are released under the same terms as the sqlite3 +** C library: +** +** 2022-10-16 +** +** 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. +*/ diff --git a/ext/wasm/sqlite3-opfs-async-proxy.js b/ext/wasm/api/sqlite3-opfs-async-proxy.js similarity index 94% rename from ext/wasm/sqlite3-opfs-async-proxy.js rename to ext/wasm/api/sqlite3-opfs-async-proxy.js index 5b29aa3ea5..254da7ee5d 100644 --- a/ext/wasm/sqlite3-opfs-async-proxy.js +++ b/ext/wasm/api/sqlite3-opfs-async-proxy.js @@ -10,7 +10,7 @@ *********************************************************************** - An Worker which manages asynchronous OPFS handles on behalf of a + A Worker which manages asynchronous OPFS handles on behalf of a synchronous API which controls it via a combination of Worker messages, SharedArrayBuffer, and Atomics. It is the asynchronous counterpart of the API defined in sqlite3-api-opfs.js. @@ -25,8 +25,12 @@ access to the sqlite3 JS/WASM bits, so any bits which it needs (most notably SQLITE_xxx integer codes) have to be imported into it via an initialization process. + + This file represents an implementation detail of a larger piece of + code, and not a public interface. Its details may change at any time + and are not intended to be used by any client-level code. */ -'use strict'; +"use strict"; const toss = function(...args){throw new Error(args.join(' '))}; if(self.window === self){ toss("This code cannot run from the main thread.", @@ -133,8 +137,8 @@ const getDirForFilename = async function f(absFilename, createDirs = false){ /** Returns the sync access handle associated with the given file - handle object (which must be a valid handle object), lazily opening - it if needed. + handle object (which must be a valid handle object, as created by + xOpen()), lazily opening it if needed. In order to help alleviate cross-tab contention for a dabase, if an exception is thrown while acquiring the handle, this routine @@ -150,7 +154,7 @@ const getSyncHandle = async (fh)=>{ let i = 1, ms = 300; for(; true; ms *= ++i){ try { - //if(1===i) toss("Just testing."); + //if(i<3) toss("Just testing."); //TODO? A config option which tells it to throw here //randomly every now and then, for testing purposes. fh.syncHandle = await fh.fileHandle.createSyncAccessHandle(); @@ -163,7 +167,7 @@ const getSyncHandle = async (fh)=>{ } warn("Error getting sync handle. Waiting",ms, "ms and trying again.",fh.filenameAbs,e); - Atomics.wait(state.sabOPView, state.opIds.xSleep, 0, ms); + Atomics.wait(state.sabOPView, state.opIds.retry, 0, ms); } } log("Got sync handle for",fh.filenameAbs,'in',performance.now() - t,'ms'); @@ -305,7 +309,7 @@ const vfsAsyncImpls = { storeAndNotify('xAccess', rc); mTimeEnd(); }, - xClose: async function(fid){ + xClose: async function(fid/*sqlite3_file pointer*/){ const opName = 'xClose'; mTimeStart(opName); const fh = __openFiles[fid]; @@ -364,7 +368,7 @@ const vfsAsyncImpls = { wTimeEnd(); return rc; }, - xFileSize: async function(fid){ + xFileSize: async function(fid/*sqlite3_file pointer*/){ mTimeStart('xFileSize'); const fh = __openFiles[fid]; let sz; @@ -381,7 +385,8 @@ const vfsAsyncImpls = { storeAndNotify('xFileSize', sz); mTimeEnd(); }, - xLock: async function(fid,lockType){ + xLock: async function(fid/*sqlite3_file pointer*/, + lockType/*SQLITE_LOCK_...*/){ mTimeStart('xLock'); const fh = __openFiles[fid]; let rc = 0; @@ -397,7 +402,8 @@ const vfsAsyncImpls = { storeAndNotify('xLock',rc); mTimeEnd(); }, - xOpen: async function(fid/*sqlite3_file pointer*/, filename, flags){ + xOpen: async function(fid/*sqlite3_file pointer*/, filename, + flags/*SQLITE_OPEN_...*/){ const opName = 'xOpen'; mTimeStart(opName); const deleteOnClose = (state.sq3Codes.SQLITE_OPEN_DELETEONCLOSE & flags); @@ -440,7 +446,7 @@ const vfsAsyncImpls = { } mTimeEnd(); }, - xRead: async function(fid,n,offset){ + xRead: async function(fid/*sqlite3_file pointer*/,n,offset64){ mTimeStart('xRead'); let rc = 0, nRead; const fh = __openFiles[fid]; @@ -448,7 +454,7 @@ const vfsAsyncImpls = { wTimeStart('xRead'); nRead = (await getSyncHandle(fh)).read( fh.sabView.subarray(0, n), - {at: Number(offset)} + {at: Number(offset64)} ); wTimeEnd(); if(nRead < n){/* Zero-fill remaining bytes */ @@ -464,7 +470,7 @@ const vfsAsyncImpls = { storeAndNotify('xRead',rc); mTimeEnd(); }, - xSync: async function(fid,flags/*ignored*/){ + xSync: async function(fid/*sqlite3_file pointer*/,flags/*ignored*/){ mTimeStart('xSync'); const fh = __openFiles[fid]; let rc = 0; @@ -480,7 +486,7 @@ const vfsAsyncImpls = { storeAndNotify('xSync',rc); mTimeEnd(); }, - xTruncate: async function(fid,size){ + xTruncate: async function(fid/*sqlite3_file pointer*/,size){ mTimeStart('xTruncate'); let rc = 0; const fh = __openFiles[fid]; @@ -497,7 +503,8 @@ const vfsAsyncImpls = { storeAndNotify('xTruncate',rc); mTimeEnd(); }, - xUnlock: async function(fid,lockType){ + xUnlock: async function(fid/*sqlite3_file pointer*/, + lockType/*SQLITE_LOCK_...*/){ mTimeStart('xUnlock'); let rc = 0; const fh = __openFiles[fid]; @@ -514,7 +521,7 @@ const vfsAsyncImpls = { storeAndNotify('xUnlock',rc); mTimeEnd(); }, - xWrite: async function(fid,n,offset){ + xWrite: async function(fid/*sqlite3_file pointer*/,n,offset64){ mTimeStart('xWrite'); let rc; wTimeStart('xWrite'); @@ -524,7 +531,7 @@ const vfsAsyncImpls = { rc = ( n === (await getSyncHandle(fh)) .write(fh.sabView.subarray(0, n), - {at: Number(offset)}) + {at: Number(offset64)}) ) ? 0 : state.sq3Codes.SQLITE_IOERR_WRITE; }catch(e){ error("xWrite():",e,fh); diff --git a/ext/wasm/api/sqlite3-wasm.c b/ext/wasm/api/sqlite3-wasm.c index 26771d5c9a..754f9cf0d4 100644 --- a/ext/wasm/api/sqlite3-wasm.c +++ b/ext/wasm/api/sqlite3-wasm.c @@ -48,6 +48,7 @@ ** want undefined. Please keep these alphabetized. */ #undef SQLITE_OMIT_DESERIALIZE +#undef SQLITE_OMIT_MEMORYDB /* ** Define any SQLITE_... config defaults we want if they aren't @@ -55,7 +56,7 @@ */ /**********************************************************************/ -/* SQLITE_DEFAULT_... */ +/* SQLITE_D... */ #ifndef SQLITE_DEFAULT_CACHE_SIZE /* ** The OPFS impls benefit tremendously from an increased cache size @@ -74,6 +75,8 @@ #ifndef SQLITE_DEFAULT_UNIX_VFS # define SQLITE_DEFAULT_UNIX_VFS "unix-none" #endif +#undef SQLITE_DQS +#define SQLITE_DQS 0 /**********************************************************************/ /* SQLITE_ENABLE_... */ @@ -809,28 +812,38 @@ const char * sqlite3_wasm_enum_json(void){ ** This function is NOT part of the sqlite3 public API. It is strictly ** for use by the sqlite project's own JS/WASM bindings. ** -** Do not use this function, even for internal use: it was -** ill-conceived and will be removed once the JS code which still -** calls it has been weeded out. -** -** This function invokes the xDelete method of the default VFS, -** passing on the given filename. If zName is NULL, no default VFS is -** found, or it has no xDelete method, SQLITE_MISUSE is returned, else -** the result of the xDelete() call is returned. +** This function invokes the xDelete method of the given VFS (or the +** default VFS if pVfs is NULL), passing on the given filename. If +** zName is NULL, no default VFS is found, or it has no xDelete +** method, SQLITE_MISUSE is returned, else the result of the xDelete() +** call is returned. */ SQLITE_WASM_KEEP -int sqlite3_wasm_vfs_unlink(const char * zName){ +int sqlite3_wasm_vfs_unlink(sqlite3_vfs *pVfs, const char * zName){ int rc = SQLITE_MISUSE /* ??? */; - sqlite3_vfs * const pVfs = sqlite3_vfs_find(0); -#if defined(__EMSCRIPTEN__) - emscripten_console_warn("sqlite3_wasm_vfs_unlink() will be removed."); -#endif + if( 0==pVfs && 0!=zName ) pVfs = sqlite3_vfs_find(0); if( zName && pVfs && pVfs->xDelete ){ rc = pVfs->xDelete(pVfs, zName, 1); } return rc; } +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Returns a pointer to the given DB's VFS for the given DB name, +** defaulting to "main" if zDbName is 0. Returns 0 if no db with the +** given name is open. +*/ +SQLITE_WASM_KEEP +sqlite3_vfs * sqlite3_wasm_db_vfs(sqlite3 *pDb, const char *zDbName){ + sqlite3_vfs * pVfs = 0; + sqlite3_file_control(pDb, zDbName ? zDbName : "main", + SQLITE_FCNTL_VFS_POINTER, &pVfs); + return pVfs; +} + /* ** This function is NOT part of the sqlite3 public API. It is strictly ** for use by the sqlite project's own JS/WASM bindings. @@ -1007,7 +1020,7 @@ int sqlite3_wasm_init_wasmfs(const char *zMountPoint){ hypothetically suffice for the transient wasm-based virtual filesystem we're currently running in. */ const int rc = wasmfs_create_directory(zMountPoint, 0777, pOpfs); - emscripten_console_logf("OPFS mkdir(%s) rc=%d", zMountPoint, rc); + /*emscripten_console_logf("OPFS mkdir(%s) rc=%d", zMountPoint, rc);*/ if(rc) return SQLITE_IOERR; } return pOpfs ? 0 : SQLITE_NOMEM; diff --git a/ext/wasm/sqlite3-worker1-promiser.js b/ext/wasm/api/sqlite3-worker1-promiser.js similarity index 93% rename from ext/wasm/sqlite3-worker1-promiser.js rename to ext/wasm/api/sqlite3-worker1-promiser.js index 71e2e33135..7360512d49 100644 --- a/ext/wasm/sqlite3-worker1-promiser.js +++ b/ext/wasm/api/sqlite3-worker1-promiser.js @@ -70,8 +70,8 @@ message. The second expects an object in the form {type:..., args:...} plus any other properties the client cares to set. This function will always set the `messageId` property on the object, - even if it's already set, and will set the `dbId` property to - `config.dbId` if it is _not_ set in the message object. + even if it's already set, and will set the `dbId` property to the + current database ID if it is _not_ set in the message object. The function throws on error. @@ -237,6 +237,23 @@ self.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){ }; }/*sqlite3Worker1Promiser()*/; self.sqlite3Worker1Promiser.defaultConfig = { - worker: ()=>new Worker("sqlite3-worker1.js"+self.location.search), + worker: function(){ + let theJs = "sqlite3-worker1.js"; + if(this.currentScript){ + const src = this.currentScript.src.split('/'); + src.pop(); + theJs = src.join('/')+'/' + theJs; + //console.warn("promiser currentScript, theJs =",this.currentScript,theJs); + }else{ + //console.warn("promiser self.location =",self.location); + const urlParams = new URL(self.location.href).searchParams; + if(urlParams.has('sqlite3.dir')){ + theJs = urlParams.get('sqlite3.dir') + '/' + theJs; + } + } + return new Worker(theJs + self.location.search); + }.bind({ + currentScript: self?.document?.currentScript + }), onerror: (...args)=>console.error('worker1 promiser error',...args) }; diff --git a/ext/wasm/sqlite3-worker1.js b/ext/wasm/api/sqlite3-worker1.js similarity index 76% rename from ext/wasm/sqlite3-worker1.js rename to ext/wasm/api/sqlite3-worker1.js index 167c6a5c79..4cff68b08b 100644 --- a/ext/wasm/sqlite3-worker1.js +++ b/ext/wasm/api/sqlite3-worker1.js @@ -25,13 +25,23 @@ Worker-specific API needs to pass _this_ file (or equivalent) to the Worker constructor and then listen for an event in the form shown above in order to know when the module has completed initialization. + + This file accepts a URL arguments to adjust how it loads sqlite3.js: + + - `sqlite3.dir`, if set, treats the given directory name as the + directory from which `sqlite3.js` will be loaded. + + By default is loads 'sqlite3.js'. */ "use strict"; (()=>{ const urlParams = new URL(self.location.href).searchParams; - importScripts(urlParams.has('wasmfs') - ? 'sqlite3-wasmfs.js' - : 'sqlite3.js'); + let theJs = 'sqlite3.js'; + if(urlParams.has('sqlite3.dir')){ + theJs = urlParams.get('sqlite3.dir') + '/' + theJs; + } + //console.warn("worker1 theJs =",theJs); + importScripts(theJs); sqlite3InitModule().then((sqlite3)=>{ sqlite3.capi.sqlite3_wasmfs_opfs_dir(); sqlite3.initWorker1API(); diff --git a/ext/wasm/batch-runner.html b/ext/wasm/batch-runner.html index e7a2b1e082..5258f9597e 100644 --- a/ext/wasm/batch-runner.html +++ b/ext/wasm/batch-runner.html @@ -69,7 +69,7 @@
- + - + - + diff --git a/ext/wasm/demo-kvvfs1.js b/ext/wasm/demo-jsstorage.js similarity index 100% rename from ext/wasm/demo-kvvfs1.js rename to ext/wasm/demo-jsstorage.js diff --git a/ext/wasm/testing-worker1-promiser.html b/ext/wasm/demo-worker1-promiser.html similarity index 91% rename from ext/wasm/testing-worker1-promiser.html rename to ext/wasm/demo-worker1-promiser.html index 9af809d9ed..e99131e6c9 100644 --- a/ext/wasm/testing-worker1-promiser.html +++ b/ext/wasm/demo-worker1-promiser.html @@ -28,7 +28,7 @@
- - + + diff --git a/ext/wasm/testing-worker1-promiser.js b/ext/wasm/demo-worker1-promiser.js similarity index 99% rename from ext/wasm/testing-worker1-promiser.js rename to ext/wasm/demo-worker1-promiser.js index 259d20fddc..855643b2d2 100644 --- a/ext/wasm/testing-worker1-promiser.js +++ b/ext/wasm/demo-worker1-promiser.js @@ -40,7 +40,7 @@ const promiserConfig = { worker: ()=>{ - const w = new Worker("sqlite3-worker1.js"); + const w = new Worker("jswasm/sqlite3-worker1.js"); w.onerror = (event)=>error("worker.onerror",event); return w; }, diff --git a/ext/wasm/testing2.html b/ext/wasm/demo-worker1.html similarity index 96% rename from ext/wasm/testing2.html rename to ext/wasm/demo-worker1.html index 25d5a9f5d8..c766ffd445 100644 --- a/ext/wasm/testing2.html +++ b/ext/wasm/demo-worker1.html @@ -1,3 +1,4 @@ + @@ -28,6 +29,6 @@
- + diff --git a/ext/wasm/testing2.js b/ext/wasm/demo-worker1.js similarity index 98% rename from ext/wasm/testing2.js rename to ext/wasm/demo-worker1.js index 7163fe1204..2da80bc239 100644 --- a/ext/wasm/testing2.js +++ b/ext/wasm/demo-worker1.js @@ -13,13 +13,13 @@ A basic test script for sqlite3-worker1.js. Note that the wrapper interface demonstrated in - testing-worker1-promiser.js is much easier to use from client code, as it + demo-worker1-promiser.js is much easier to use from client code, as it lacks the message-passing acrobatics demonstrated in this file. */ 'use strict'; (function(){ const T = self.SqliteTestUtil; - const SW = new Worker("sqlite3-worker1.js"); + const SW = new Worker("jswasm/sqlite3-worker1.js"); const DbState = { id: undefined }; diff --git a/ext/wasm/dist.make b/ext/wasm/dist.make new file mode 100644 index 0000000000..df3e236d7b --- /dev/null +++ b/ext/wasm/dist.make @@ -0,0 +1,99 @@ +#!/do/not/make +#^^^ help emacs select edit mode +# +# Intended to include'd by ./GNUmakefile. +# +# 'make dist' rules for creating a distribution archive of the WASM/JS +# pieces, noting that we only build a dist of the built files, not the +# numerous pieces required to build them. +####################################################################### +MAKEFILE.dist := $(lastword $(MAKEFILE_LIST)) + +######################################################################## +# Chicken/egg situation: we need $(bin.version-info) to get the version +# info for the archive name, but that binary may not yet be built, and +# won't be built until we expand the dependencies. We have to use a +# temporary name for the archive. +dist-name = sqlite-wasm-TEMP +#ifeq (0,1) +# $(info WARNING *******************************************************************) +# $(info ** Be sure to create the desired build configuration before creating the) +# $(info ** distribution archive. Use one of the following targets to do so:) +# $(info **) +# $(info ** o2: builds with -O2, resulting in the fastest builds) +# $(info ** oz: builds with -Oz, resulting in the smallest builds) +# $(info /WARNING *******************************************************************) +#endif + +######################################################################## +# dist.build must be the name of a target which triggers the +# build of the files to be packed into the dist archive. The +# intention is that it be one of (o0, o1, o2, o3, os, oz), each of +# which uses like-named -Ox optimization level flags. The o2 target +# provides the best overall runtime speeds. The oz target provides +# slightly slower speeds (roughly 10%) with significantly smaller WASM +# file sizes. Note that -O2 (the o2 target) results in faster binaries +# than both -O3 and -Os (the o3 and os targets) in all tests run to +# date. +dist.build ?= oz + +dist-dir.top := $(dist-name) +dist-dir.jswasm := $(dist-dir.top)/$(notdir $(dir.dout)) +dist-dir.common := $(dist-dir.top)/common +dist.top.extras := \ + demo-123.html demo-123-worker.html demo-123.js \ + tester1.html tester1-worker.html tester1.js \ + demo-jsstorage.html demo-jsstorage.js \ + demo-worker1.html demo-worker1.js \ + demo-worker1-promiser.html demo-worker1-promiser.js +dist.jswasm.extras := $(sqlite3-api.ext.jses) $(sqlite3.wasm) +dist.common.extras := \ + $(wildcard $(dir.common)/*.css) \ + $(dir.common)/SqliteTestUtil.js + +.PHONY: dist +######################################################################## +# dist: create the end-user deliverable archive. +# +# Maintenance reminder: because dist depends on $(dist.build), and +# $(dist.build) will depend on clean, having any deps on +# $(dist-archive) which themselves may be cleaned up by the clean +# target will lead to grief in parallel builds (-j #). Thus +# $(dist-target)'s deps must be trimmed to non-generated files or +# files which are _not_ cleaned up by the clean target. +# +# Note that we require $(bin.version-info) in order to figure out the +# dist file's name, so cannot (without a recursive make) have the +# target name equal to the archive name. +dist: \ + $(bin.stripccomments) $(bin.version-info) \ + $(dist.build) \ + $(MAKEFILE) $(MAKEFILE.dist) + @echo "Making end-user deliverables..." + @rm -fr $(dist-dir.top) + @mkdir -p $(dist-dir.jswasm) $(dist-dir.common) + @cp -p $(dist.top.extras) $(dist-dir.top) + @cp -p README-dist.txt $(dist-dir.top)/README.txt + @cp -p index-dist.html $(dist-dir.top)/index.html + @cp -p $(dist.jswasm.extras) $(dist-dir.jswasm) + @$(bin.stripccomments) -k -k < $(sqlite3.js) \ + > $(dist-dir.jswasm)/$(notdir $(sqlite3.js)) + @cp -p $(dist.common.extras) $(dist-dir.common) + @vnum=$$($(bin.version-info) --version-number); \ + vdir=sqlite-wasm-$$vnum; \ + arczip=$$vdir.zip; \ + echo "Making $$arczip ..."; \ + rm -fr $$arczip $$vdir; \ + mv $(dist-dir.top) $$vdir; \ + zip -qr $$arczip $$vdir; \ + rm -fr $$vdir; \ + ls -la $$arczip; \ + unzip -lv $$arczip || echo "Missing unzip app? Not fatal." + +# We need a separate `clean` rule to account for weirdness in +# a sub-make, where we get a copy of the $(dist-name) dir +# copied into the new $(dist-name) dir. +.PHONY: dist-clean +clean: dist-clean +dist-clean: + rm -fr $(dist-name) $(wildcard sqlite-wasm-*.zip) diff --git a/ext/wasm/fiddle.make b/ext/wasm/fiddle.make index 585add4270..3de2414636 100644 --- a/ext/wasm/fiddle.make +++ b/ext/wasm/fiddle.make @@ -23,6 +23,7 @@ $(dir.top)/shell.c: $(SHELL_SRC) $(dir.top)/tool/mkshellc.tcl # /shell.c ######################################################################## +EXPORTED_FUNCTIONS.fiddle := $(dir.tmp)/EXPORTED_FUNCTIONS.fiddle fiddle.emcc-flags = \ $(emcc.cflags) $(emcc_opt_full) \ --minify 0 \ @@ -34,35 +35,36 @@ fiddle.emcc-flags = \ -sDYNAMIC_EXECUTION=0 \ -sWASM_BIGINT=$(emcc_enable_bigint) \ -sEXPORT_NAME=$(sqlite3.js.init-func) \ + -Wno-limited-postlink-optimizations \ $(sqlite3.js.flags.--post-js) \ - -sEXPORTED_RUNTIME_METHODS=@$(dir.wasm)/EXPORTED_RUNTIME_METHODS.fiddle \ - -sEXPORTED_FUNCTIONS=@$(dir.wasm)/EXPORTED_FUNCTIONS.fiddle \ + $(emcc.exportedRuntimeMethods) \ + -sEXPORTED_FUNCTIONS=@$(abspath $(EXPORTED_FUNCTIONS.fiddle)) \ $(SQLITE_OPT) $(SHELL_OPT) \ -DSQLITE_SHELL_FIDDLE # -D_POSIX_C_SOURCE is needed for strdup() with emcc fiddle.EXPORTED_FUNCTIONS.in := \ EXPORTED_FUNCTIONS.fiddle.in \ - EXPORTED_FUNCTIONS.api + $(EXPORTED_FUNCTIONS.api) -EXPORTED_FUNCTIONS.fiddle: $(fiddle.EXPORTED_FUNCTIONS.in) $(MAKEFILE.fiddle) - grep -h -v jaccwabyt $(fiddle.EXPORTED_FUNCTIONS.in) | sort -u > $@ +$(EXPORTED_FUNCTIONS.fiddle): $(fiddle.EXPORTED_FUNCTIONS.in) $(MAKEFILE.fiddle) + sort -u $(fiddle.EXPORTED_FUNCTIONS.in) > $@ fiddle-module.js := $(dir.fiddle)/fiddle-module.js fiddle-module.wasm := $(subst .js,.wasm,$(fiddle-module.js)) -fiddle.cs := $(dir.top)/shell.c $(sqlite3-wasm.c) +fiddle.cses := $(dir.top)/shell.c $(sqlite3-wasm.c) -SOAP.js := sqlite3-opfs-async-proxy.js -$(dir.fiddle)/$(SOAP.js): $(SOAP.js) +fiddle.SOAP.js := $(dir.fiddle)/$(notdir $(SOAP.js)) +$(fiddle.SOAP.js): $(SOAP.js) cp $< $@ $(eval $(call call-make-pre-js,fiddle-module)) $(fiddle-module.js): $(MAKEFILE) $(MAKEFILE.fiddle) \ - EXPORTED_FUNCTIONS.fiddle EXPORTED_RUNTIME_METHODS.fiddle \ - $(fiddle.cs) $(pre-post-fiddle-module.deps) $(dir.fiddle)/$(SOAP.js) + $(EXPORTED_FUNCTIONS.fiddle) \ + $(fiddle.cses) $(pre-post-fiddle-module.deps) $(fiddle.SOAP.js) $(emcc.bin) -o $@ $(fiddle.emcc-flags) \ $(pre-post-common.flags) $(pre-post-fiddle-module.flags) \ - $(fiddle.cs) + $(fiddle.cses) $(maybe-wasm-strip) $(fiddle-module.wasm) gzip < $@ > $@.gz gzip < $(fiddle-module.wasm) > $(fiddle-module.wasm).gz diff --git a/ext/wasm/fiddle/fiddle-worker.js b/ext/wasm/fiddle/fiddle-worker.js index f78a4d3be1..e3a2cbbe81 100644 --- a/ext/wasm/fiddle/fiddle-worker.js +++ b/ext/wasm/fiddle/fiddle-worker.js @@ -102,7 +102,7 @@ const toss = (...args)=>{ throw new Error(args.join(' ')); }; - const fixmeOPFS = "(FIXME: won't work with vanilla OPFS.)"; + const fixmeOPFS = "(FIXME: won't work with OPFS-over-sqlite3_vfs.)"; let sqlite3 /* gets assigned when the wasm module is loaded */; self.onerror = function(/*message, source, lineno, colno, error*/) { @@ -130,7 +130,7 @@ return f._(); }, dbIsOpfs: function f(){ - return sqlite3.opfs && sqlite3.capi.sqlite3_web_db_uses_vfs( + return sqlite3.opfs && sqlite3.capi.sqlite3_js_db_uses_vfs( this.dbHandle(), "opfs" ); }, @@ -251,7 +251,7 @@ const fn2 = fn ? fn.split(/[/\\]/).pop() : null; try{ if(!fn2) toss("DB appears to be closed."); - const buffer = sqlite3.capi.sqlite3_web_db_export( + const buffer = sqlite3.capi.sqlite3_js_db_export( Sqlite3Shell.dbHandle() ); wMsg('db-export',{filename: fn2, buffer: buffer.buffer}, [buffer.buffer]); @@ -370,9 +370,9 @@ */ sqlite3InitModule(fiddleModule).then((_sqlite3)=>{ sqlite3 = _sqlite3; + const dbVfs = sqlite3.capi.wasm.xWrap('fiddle_db_vfs', "*", ['string']); fiddleModule.fsUnlink = (fn)=>{ - stderr("unlink:",fixmeOPFS); - return sqlite3.capi.wasm.sqlite3_wasm_vfs_unlink(fn); + return sqlite3.capi.wasm.sqlite3_wasm_vfs_unlink(dbVfs(0), fn); }; wMsg('fiddle-ready'); })/*then()*/; diff --git a/ext/wasm/index-dist.html b/ext/wasm/index-dist.html new file mode 100644 index 0000000000..6b038c82a9 --- /dev/null +++ b/ext/wasm/index-dist.html @@ -0,0 +1,90 @@ + + + + + + + sqlite3 WASM Demo Page Index + + + +
sqlite3 WASM demo pages
+
+
Below is the list of demo pages for the sqlite3 WASM + builds. The intent is that this page be run + using the functional equivalent of:
+
althttpd -enable-sab -page index.html
+
and the individual pages be started in their own tab. + Warnings and Caveats: + +
+
The tests and demos... + +
+ + + + diff --git a/ext/wasm/index.html b/ext/wasm/index.html index 01d178bb7a..222105599c 100644 --- a/ext/wasm/index.html +++ b/ext/wasm/index.html @@ -34,8 +34,7 @@
  • Whether or not WASMFS/OPFS support is enabled on any given page may depend on build-time options which are off by - default because they currently (as of 2022-09-08) break - with Worker-based pages. + default.
  • @@ -46,7 +45,7 @@
  • tester1: Core unit and regression tests for the various APIs and surrounding utility code.
  • -
  • tester1-worker: same thing +
  • tester1-worker: same thing but running in a Worker.
  • @@ -60,15 +59,21 @@
  • demo-123-worker is the same as demo-123 but loads and runs sqlite3 from a Worker thread.
  • -
  • demo-kvvfs1: very basic +
  • demo-jsstorage: very basic demo of using the key-value VFS for storing a persistent db in JS localStorage or sessionStorage.
  • +
  • demo-worker1: + Worker-based wrapper of the OO API #1. Its Promise-based + wrapper is significantly easier to use, however.
  • +
  • demo-worker1-promiser: + a demo of the Promise-based wrapper of the Worker1 API.
  • speedtest1 ports (sqlite3's primary benchmarking tool)...