1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-01 06:27:03 +03:00

Merge trunk into the sahpool-digest branch.

FossilOrigin-Name: fc1eeb7d1f2880907b0fe71a8c572dd7cd74a5d65ec0177332976ad2f8c2b216
This commit is contained in:
stephan
2025-02-26 03:32:52 +00:00
151 changed files with 5772 additions and 3530 deletions

View File

@ -103,7 +103,7 @@ else
ifeq (,$(filter $(OPTIMIZED_TARGETS),$(MAKECMDGOALS)))
$(info ==============================================================)
$(info == Development build. Make one of (dist, snapshot) for a)
$(info == smaller release build.)
$(info == smaller and faster release build.)
$(info ==============================================================)
endif
endif
@ -136,8 +136,9 @@ JS_BUILD_NAMES := sqlite3 sqlite3-wasmfs
# target of browsers.
#
# - node = for use by node.js for node.js, as opposed to by node.js on
# behalf o browser-side code (use bundler-friendly for that). Note
# behalf of browser-side code (use bundler-friendly for that). Note
# that persistent storage (OPFS) is not available in these builds.
# These builds are UNTESTED and UNSUPPORTED!
#
JS_BUILD_MODES := vanilla esm bunder-friendly node
@ -165,7 +166,7 @@ dir.wasmfs := $(dir.dout)
MKDIR.bld := $(dir.tmp)
$(MKDIR.bld):
-mkdir -p $@ $(dir.dout)
@mkdir -p $@ $(dir.dout)
CLEAN_FILES += *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~ $(dir.fiddle)/*~ \
$(dir.fiddle-debug)/* $(dir.dout)/* $(dir.tmp)/*
@ -358,7 +359,7 @@ endif
# Slight caveat: this uses the version info from the in-tree
# sqlite3.c/h, which may diff from a user-provided $(sqlite3.c). The
# end result is that the generated JS files may have static version
# info from $(bin.version-info) which differ from their runtime-emited
# info from $(bin.version-info) which differ from their runtime-emitted
# version info (e.g. from sqlite3_libversion()).
bin.version-info := $(dir.top)/version-info
.NOTPARALLEL: $(bin.version-info)
@ -424,7 +425,8 @@ define SQLITE.CALL.C-PP.FILTER
# $1 = Input file: c-pp -f $(1).js
# $2 = Output file: c-pp -o $(2).js
# $3 = optional c-pp -D... flags
$(2): $(1) $$(MAKEFILE) $$(bin.c-pp)
$(2): $(1) $$(MAKEFILE_LIST) $$(bin.c-pp)
@mkdir -p $$(dir $$@)
$$(bin.c-pp) -f $(1) -o $$@ $(3) $(SQLITE.CALL.C-PP.FILTER.global)
#CLEAN_FILES += $(2)
endef
@ -511,7 +513,7 @@ 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), in the order they need to be assembled.
sqlite3-api.jses := $(sqlite3-license-version.js)
# sqlite3-api-prologue.js: initial boostrapping bits:
# sqlite3-api-prologue.js: initial bootstrapping bits:
sqlite3-api.jses += $(dir.api)/sqlite3-api-prologue.js
# whwhasm.js and jaccwabyt.js: Low-level utils, mostly replacing
# Emscripten glue:
@ -618,6 +620,12 @@ emcc.exportedRuntimeMethods := \
emcc.jsflags += $(emcc.exportedRuntimeMethods)
emcc.jsflags += -sUSE_CLOSURE_COMPILER=0
emcc.jsflags += -sIMPORTED_MEMORY
ifeq (,$(filter -O0,$(emcc_opt)))
emcc.assert ?= 0
else
emcc.assert ?= 2
endif
emcc.jsflags += -sASSERTIONS=$(emcc.assert)
emcc.jsflags += -sSTRICT_JS=0
# STRICT_JS disabled due to:
# https://github.com/emscripten-core/emscripten/issues/18610
@ -884,12 +892,13 @@ EXPORTED_FUNCTIONS.fiddle := $(dir.tmp)/EXPORTED_FUNCTIONS.fiddle
########################################################################
########################################################################
# We have to ensure that we do not build $(sqlite3*.*js) in parallel
# because they all result in the creation of $(sqlite3.wasm). We have
# no way to build just a .[m]js file without also building the .wasm
# file because the generated .[m]js file has to include info about the
# imports needed by the wasm file, so they have to be built
# for any builds which result in the creation of $(sqlite3.wasm). We
# have no way to build just a .[m]js file without also building the
# .wasm file because the generated .[m]js file has to include info
# about the imports needed by the wasm file, so they have to be built
# together. i.e. we're building $(sqlite3.wasm) multiple times, but
# that's unavoidable (and harmless, just a waste of build time).
# that's unavoidable (and harmless, but is a significant waste of
# build time).
$(sqlite3.wasm): $(sqlite3.js)
$(sqlite3.mjs): $(sqlite3.js)
$(sqlite3-bundler-friendly.mjs): $(sqlite3.mjs)
@ -909,6 +918,31 @@ sqlite3-worker1-promiser.js := $(dir.dout)/sqlite3-worker1-promiser.js
sqlite3-worker1-promiser.mjs := $(dir.dout)/sqlite3-worker1-promiser.mjs
sqlite3-worker1-bundler-friendly.mjs := $(dir.dout)/sqlite3-worker1-bundler-friendly.mjs
sqlite3-worker1-promiser-bundler-friendly.js := $(dir.dout)/sqlite3-worker1-promiser-bundler-friendly.js
ifneq (1,$(MAKING_CLEAN))
# This block MUST come between the above definitions of
# sqlite3-...js/mjs and the $(eval) calls below this block which use
# SQLITE.CALL.C-PP.FILTER.
########################################################################
# bin.mkwb is used for generating some of the makefile code for the
# various wasm builds. It used to be generated in this makefile via a
# difficult-to-read/maintain block of $(eval)'d code. Attempts were
# made to generate it from tcl and bash (shell) but having to escape
# the $ references in those languages made it just as illegible as the
# native makefile code. Somewhat surprisingly, moving that code generation
# to C makes it slightly less illegible than the previous 3 options.
bin.mkwb := ./mkwasmbuilds
$(bin.mkwb): $(bin.mkwb).c $(MAKEFILE)
$(CC) -o $@ $<
DISTCLEAN_FILES += $(bin.mkwb)
.wasmbuilds.make: $(bin.mkwb)
@rm -f $@
$(bin.mkwb) > $@
@chmod -w $@
-include .wasmbuilds.make
endif
DISTCLEAN_FILES += .wasmbuilds.make
$(eval $(call SQLITE.CALL.C-PP.FILTER,$(sqlite3-worker1.js.in),$(sqlite3-worker1.js)))
$(eval $(call SQLITE.CALL.C-PP.FILTER,$(sqlite3-worker1.js.in),$(sqlite3-worker1-bundler-friendly.mjs),\
$(c-pp.D.sqlite3-bundler-friendly)))
@ -940,27 +974,6 @@ sqlite3-api.ext.jses += \
all quick: $(sqlite3-api.ext.jses)
q: quick
ifneq (1,$(MAKING_CLEAN))
########################################################################
# bin.mkwb is used for generating some of the makefile code for the
# various wasm builds. It used to be generated in this makefile via a
# difficult-to-read/maintain block of $(eval)'d code. Attempts were
# made to generate it from tcl and bash (shell) but having to escape
# the $ references in those languages made it just as illegible as the
# native makefile code. Somewhat surprisingly, moving that code generation
# to C makes it slightly less illegible than the previous 3 options.
bin.mkwb := ./mkwasmbuilds
$(bin.mkwb): $(bin.mkwb).c $(MAKEFILE)
$(CC) -o $@ $<
DISTCLEAN_FILES += $(bin.mkwb)
.wasmbuilds.make: $(bin.mkwb)
@rm -f $@
$(bin.mkwb) > $@
@chmod -w $@
-include .wasmbuilds.make
endif
DISTCLEAN_FILES += .wasmbuilds.make
########################################################################
# batch-runner.js is part of one of the test apps which reads in SQL
# dumps generated by $(speedtest1) and executes them.

View File

@ -8,16 +8,16 @@
point the sqlite3 JS API bits will get set up.
*/
Module.runSQLite3PostLoadInit = function(EmscriptenModule/*the Emscripten-style module object*/){
/** ^^^ As don't use Module.postRun, as that runs a different time
/** ^^^ Don't use Module.postRun, as that runs a different time
depending on whether this file is built with emcc 3.1.x or
4.0.x. This function name is intentionally obnoxiously verbose to
ensure that we don't collide with current and future Emscripten
symbol names. */
'use strict';
//console.warn("This is the start of the Module.postRun handler.");
//console.warn("This is the start of Module.runSQLite3PostLoadInit()");
/* This function will contain at least the following:
- post-js-header.js (this file)
- post-js-header.js => this file
- sqlite3-api-prologue.js => Bootstrapping bits to attach the rest to
- common/whwasmutil.js => Replacements for much of Emscripten's glue
- jaccwabyt/jaccwabyt.js => Jaccwabyt (C/JS struct binding)
@ -26,8 +26,8 @@ Module.runSQLite3PostLoadInit = function(EmscriptenModule/*the Emscripten-style
- sqlite3-api-worker1.js => Worker-based API
- sqlite3-vfs-helper.c-pp.js => Utilities for VFS impls
- sqlite3-vtab-helper.c-pp.js => Utilities for virtual table impls
- sqlite3-vfs-opfs.c-pp.js => OPFS VFS
- sqlite3-vfs-opfs.c-pp.js => OPFS VFS
- sqlite3-vfs-opfs-sahpool.c-pp.js => OPFS SAHPool VFS
- sqlite3-api-cleanup.js => final API cleanup
- post-js-footer.js => closes this postRun() function
- post-js-footer.js => closes this function
*/

View File

@ -229,14 +229,15 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
'*'
]],
/**
2025-02-03: We do not have a way to automatically clean up
destructors which are automatically converted from JS functions
via the final argument to sqlite3_set_auxdata(). Because of
that, it is strongly recommended that clients use
wasm.installFunction() to create such callbacks, then pass that
pointer to sqlite3_set_auxdata(). Relying on automated
conversions here will lead to leaks of JS/WASM proxy functions
because sqlite3_set_auxdata() is frequently called in UDFs.
We do not have a way to automatically clean up destructors
which are automatically converted from JS functions via the
final argument to sqlite3_set_auxdata(). Because of that,
automatic function conversion is not supported for this
function. Clients should use wasm.installFunction() to create
such callbacks, then pass that pointer to
sqlite3_set_auxdata(). Relying on automated conversions here
would lead to leaks of JS/WASM proxy functions because
sqlite3_set_auxdata() is frequently called in UDFs.
The sqlite3.oo1.DB class's onclose handlers can be used for this
purpose. For example:
@ -252,14 +253,24 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
Then pass pAuxDtor as the final argument to appropriate
sqlite3_set_auxdata() calls.
Note that versions prior to 3.49.0 ostensibly had automatic
function conversion here but a typo prevented it from
working. Rather than fix it, it was removed because testing the
fix brought the huge potential for memory leaks to the
forefront.
*/
["sqlite3_set_auxdata", undefined, [
"sqlite3_context*", "int", "*",
new wasm.xWrap.FuncPtrAdapter({
name: 'xDestroyAuxData',
signature: 'v(p)',
contextKey: (argv, argIndex)=>argv[0/* sqlite3_context* */]
})
true
? "*"
: new wasm.xWrap.FuncPtrAdapter({
/* If we can find a way to automate their cleanup, JS functions can
be auto-converted with this. */
name: 'xDestroyAuxData',
signature: 'v(p)',
contextKey: (argv, argIndex)=>argv[0/* sqlite3_context* */]
})
]],
["sqlite3_shutdown", undefined],
["sqlite3_sourceid", "string"],

View File

@ -12,12 +12,12 @@
This file is intended to be combined at build-time with other
related code, most notably a header and footer which wraps this
whole file into an Emscripten Module.postRun()-style handler. The
sqlite3 JS API has no hard requirements on Emscripten and does not
expose any Emscripten APIs to clients. It is structured such that
its build can be tweaked to include it in arbitrary WASM
environments which can supply the necessary underlying features
(e.g. a POSIX file I/O layer).
whole file into a single callback which can be run after Emscripten
loads the corresponding WASM module. The sqlite3 JS API has no hard
requirements on Emscripten and does not expose any Emscripten APIs
to clients. It is structured such that its build can be tweaked to
include it in arbitrary WASM environments which can supply the
necessary underlying features (e.g. a POSIX file I/O layer).
Main project home page: https://sqlite.org
@ -1453,7 +1453,7 @@ globalThis.sqlite3ApiBootstrap = function sqlite3ApiBootstrap(
creates (or overwrites) the given file using those APIs. This is
primarily intended for use in Emscripten-based builds where the POSIX
APIs are transparently proxied by an in-memory virtual filesystem.
It may behave diffrently in other environments.
It may behave differently in other environments.
The first argument must be either a JS string or WASM C-string
holding the filename. Note that this routine does _not_ create

View File

@ -279,11 +279,11 @@
The arguments are in the same form accepted by oo1.DB.exec(), with
the exceptions noted below.
If the `countChanges` arguments property (added in version 3.43) is
truthy then the `result` property contained by the returned object
will have a `changeCount` property which holds the number of changes
made by the provided SQL. Because the SQL may contain an arbitrary
number of statements, the `changeCount` is calculated by calling
If `args.countChanges` (added in version 3.43) is truthy then the
`result` property contained by the returned object will have a
`changeCount` property which holds the number of changes made by the
provided SQL. Because the SQL may contain an arbitrary number of
statements, the `changeCount` is calculated by calling
`sqlite3_total_changes()` before and after the SQL is evaluated. If
the value of `countChanges` is 64 then the `changeCount` property
will be returned as a 64-bit integer in the form of a BigInt (noting
@ -292,6 +292,15 @@
calling `sqlite3_total_changes64()` before and after the SQL is
evaluated.
If the `args.lastInsertRowId` (added in version 3.50.0) is truthy
then the `result` property contained by the returned object will
have a `lastInsertRowId` will hold a BigInt-type value corresponding
to the result of sqlite3_last_insert_rowid(). This value is only
fetched once, after the SQL is run, regardless of how many
statements the SQL contains. This API has no idea whether the SQL
contains any INSERTs, so it is up to the client to apply/rely on
this property only when it makes sense to do so.
A function-type args.callback property cannot cross
the window/Worker boundary, so is not useful here. If
args.callback is a string then it is assumed to be a
@ -542,6 +551,12 @@ sqlite3.initWorker1API = function(){
if(undefined !== changeCount){
rc.changeCount = db.changes(true,64===rc.countChanges) - changeCount;
}
const lastInsertRowId = !!rc.lastInsertRowId
? sqlite3.capi.sqlite3_last_insert_rowid(db)
: undefined;
if( undefined!==lastInsertRowId ){
rc.lastInsertRowId = lastInsertRowId;
}
if(rc.callback instanceof Function){
rc.callback = theCallback;
/* Post a sentinel message to tell the client that the end

View File

@ -544,22 +544,10 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
currently-opened client-specified filenames. */
getFileNames(){
const rc = [];
const iter = this.#mapFilenameToSAH.keys();
for(const n of iter) rc.push(n);
for(const n of this.#mapFilenameToSAH.keys()) rc.push(n);
return rc;
}
// #createFileObject(sah,clientName,opaqueName){
// const f = Object.assign(Object.create(null),{
// clientName, opaqueName
// });
// this.#mapSAHToMeta.set(sah, f);
// return f;
// }
// #unmapFileObject(sah){
// this.#mapSAHToMeta.delete(sah);
// }
/**
Adds n files to the pool's capacity. This change is
persistent across settings. Returns a Promise which resolves
@ -600,8 +588,9 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}
/**
Releases all currently-opened SAHs. The only legal
operation after this is acquireAccessHandles().
Releases all currently-opened SAHs. The only legal operation
after this is acquireAccessHandles() or (if this is called from
pauseVfs()) either of isPaused() or unpauseVfs().
*/
releaseAccessHandles(){
for(const ah of this.#mapSAHToName.keys()) ah.close();
@ -611,17 +600,21 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}
/**
Opens all files under this.vfsDir/this.#dhOpaque and acquires
a SAH for each. returns a Promise which resolves to no value
but completes once all SAHs are acquired. If acquiring an SAH
throws, SAHPool.$error will contain the corresponding
exception.
Opens all files under this.vfsDir/this.#dhOpaque and acquires a
SAH for each. Returns a Promise which resolves to no value but
completes once all SAHs are acquired. If acquiring an SAH
throws, this.$error will contain the corresponding Error
object.
If it throws, it releases any SAHs which it may have
acquired before the exception was thrown, leaving the VFS in a
well-defined but unusable state.
If clearFiles is true, the client-stored state of each file is
cleared when its handle is acquired, including its name, flags,
and any data stored after the metadata block.
*/
async acquireAccessHandles(clearFiles){
async acquireAccessHandles(clearFiles=false){
const files = [];
for await (const [name,h] of this.#dhOpaque){
if('file'===h.kind){
@ -890,12 +883,18 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
Removes this object's sqlite3_vfs registration and shuts down
this object, releasing all handles, mappings, and whatnot,
including deleting its data directory. There is currently no
way to "revive" the object and reaquire its resources.
way to "revive" the object and reaquire its
resources. Similarly, there is no recovery strategy if removal
of any given SAH fails, so such errors are ignored by this
function.
This function is intended primarily for testing.
Resolves to true if it did its job, false if the
VFS has already been shut down.
@see pauseVfs()
@see unpauseVfs()
*/
async removeVfs(){
if(!this.#cVfs.pointer || !this.#dhOpaque) return false;
@ -911,13 +910,77 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
);
this.#dhVfsRoot = this.#dhVfsParent = undefined;
}catch(e){
sqlite3.config.error(this.vfsName,"removeVfs() failed:",e);
sqlite3.config.error(this.vfsName,"removeVfs() failed with no recovery strategy:",e);
/*otherwise ignored - there is no recovery strategy*/
}
return true;
}
/**
"Pauses" this VFS by unregistering it from SQLite and
relinquishing all open SAHs, leaving the associated files
intact. If this object is already paused, this is a
no-op. Returns this object.
This function throws if SQLite has any opened file handles
hosted by this VFS, as the alternative would be to invoke
Undefined Behavior by closing file handles out from under the
library. Similarly, automatically closing any database handles
opened by this VFS would invoke Undefined Behavior in
downstream code which is holding those pointers.
If this function throws due to open file handles then it has
no side effects. If the OPFS API throws while closing handles
then the VFS is left in an undefined state.
@see isPaused()
@see unpauseVfs()
*/
pauseVfs(){
if(this.#mapS3FileToOFile_.size>0){
sqlite3.SQLite3Error.toss(
capi.SQLITE_MISUSE, "Cannot pause VFS",
this.vfsName,"because it has opened files."
);
}
if(this.#mapSAHToName.size>0){
capi.sqlite3_vfs_unregister(this.vfsName);
this.releaseAccessHandles();
}
return this;
}
/**
Returns true if this pool is currently paused else false.
@see pauseVfs()
@see unpauseVfs()
*/
isPaused(){
return 0===this.#mapSAHToName.size;
}
/**
"Unpauses" this VFS, reacquiring all SAH's and (if successful)
re-registering it with SQLite. This is a no-op if the VFS is
not currently paused.
The returned Promise resolves to this object. See
acquireAccessHandles() for how it behaves if it throws due to
SAH acquisition failure.
@see isPaused()
@see pauseVfs()
*/
async unpauseVfs(){
if(0===this.#mapSAHToName.size){
return this.acquireAccessHandles(false).
then(()=>capi.sqlite3_vfs_register(this.#cVfs, 0),this);
}
return this;
}
//! Documented elsewhere in this file.
exportFile(name){
const sah = this.#mapFilenameToSAH.get(name) || toss("File not found:",name);
@ -1042,6 +1105,10 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
async removeVfs(){ return this.#p.removeVfs() }
pauseVfs(){ this.#p.pauseVfs(); return this; }
async unpauseVfs(){ return this.#p.unpauseVfs().then(()=>this); }
isPaused(){ return this.#p.isPaused() }
}/* class OpfsSAHPoolUtil */;
/**
@ -1275,6 +1342,41 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
Clears all client-defined state of all SAHs and makes all of them
available for re-use by the pool. Results are undefined if any such
handles are currently in use, e.g. by an sqlite3 db.
APIs specific to the "pause" capability (added in version 3.49):
Summary: "pausing" the VFS disassociates it from SQLite and
relinquishes its SAHs so that they may be opened by another
instance of this VFS (running in a separate tab/page or Worker).
"Unpausing" it takes back control, if able.
- pauseVfs()
"Pauses" this VFS by unregistering it from SQLite and
relinquishing all open SAHs, leaving the associated files intact.
This enables pages/tabs to coordinate semi-concurrent usage of
this VFS. If this object is already paused, this is a
no-op. Returns this object. Throws if SQLite has any opened file
handles hosted by this VFS. If this function throws due to open
file handles then it has no side effects. If the OPFS API throws
while closing handles then the VFS is left in an undefined state.
- isPaused()
Returns true if this VFS is paused, else false.
- [async] unpauseVfs()
Restores the VFS to an active state after having called
pauseVfs() on it. This is a no-op if the VFS is not paused. The
returned Promise resolves to this object on success. A rejected
Promise means there was a problem reacquiring the SAH handles
(possibly because they're in use by another instance or have
since been removed). Generically speaking, there is no recovery
strategy for that type of error, but if the problem is simply
that the OPFS files are locked, then a later attempt to unpause
it, made after the concurrent instance releases the SAHs, may
recover from the situation.
*/
sqlite3.installOpfsSAHPoolVfs = async function(options=Object.create(null)){
options = Object.assign(Object.create(null), optionDefaults, (options||{}));

View File

@ -459,7 +459,7 @@ const installOpfsVfs = function callee(options){
Runs the given operation (by name) in the async worker
counterpart, waits for its response, and returns the result
which the async worker writes to SAB[state.opIds.rc]. The
2nd and subsequent arguments must be the aruguments for the
2nd and subsequent arguments must be the arguments for the
async op.
*/
const opRun = (op,...args)=>{

View File

@ -980,7 +980,7 @@ const char * sqlite3__wasm_enum_json(void){
#undef _DefGroup
/*
** Emit an array of "StructBinder" struct descripions, which look
** Emit an array of "StructBinder" struct descriptions, which look
** like:
**
** {

View File

@ -335,8 +335,8 @@ sqlite3Worker1Promiser.v2 = function(config){
/**
When built as a module, we export sqlite3Worker1Promiser.v2()
instead of sqlite3Worker1Promise() because (A) its interface is more
conventional for ESM usage and (B) the ESM option export option for
this API did not exist until v2 was created, so there's no backwards
conventional for ESM usage and (B) the ESM export option for this
API did not exist until v2 was created, so there's no backwards
incompatibility.
*/
export default sqlite3Worker1Promiser.v2;

View File

@ -798,7 +798,7 @@ globalThis.WhWasmUtilInstaller = function(target){
*/
target.peek8 = (...ptr)=>target.peek( (1===ptr.length ? ptr[0] : ptr), 'i8' );
/**
Convience form of poke() intended for setting individual bytes.
Convenience form of poke() intended for setting individual bytes.
Its difference from poke() is that it always writes to the
i8-sized heap view.
*/

View File

@ -103,7 +103,7 @@
if(0===db.selectValue('select count(*) from sqlite_master')){
log("DB is empty. Use the init button to populate it.");
}else{
log("DB contains data from a previous session. Use the Clear Ctorage button to delete it.");
log("DB contains data from a previous session. Use the Clear Storage button to delete it.");
btnSelect.click();
}
};

View File

@ -115,6 +115,7 @@ delete globalThis.sqlite3Worker1Promiser;
"insert into t(a,b) values(1,2),(3,4),(5,6)"
].join(';'),
resultRows: [], columnNames: [],
lastInsertRowId: true,
countChanges: sqConfig.bigIntEnabled ? 64 : true
}, function(ev){
ev = ev.result;
@ -122,7 +123,9 @@ delete globalThis.sqlite3Worker1Promiser;
.assert(0===ev.columnNames.length)
.assert(sqConfig.bigIntEnabled
? (3n===ev.changeCount)
: (3===ev.changeCount));
: (3===ev.changeCount))
.assert('bigint'===typeof ev.lastInsertRowId)
.assert(ev.lastInsertRowId>=3);
});
await wtest('exec',{

View File

@ -156,11 +156,14 @@
sql: ["create table t(a,b);",
"insert into t(a,b) values(1,2),(3,4),(5,6)"
],
lastInsertRowId: true,
resultRows: [], columnNames: []
}, function(ev){
ev = ev.result;
T.assert(0===ev.resultRows.length)
.assert(0===ev.columnNames.length);
.assert(0===ev.columnNames.length)
.assert('bigint'===typeof ev.lastInsertRowId)
.assert(ev.lastInsertRowId>=3);
});
runOneTest('exec',{
sql: 'select a a, b b from t order by a',

View File

@ -91,7 +91,7 @@ all: fiddle
fiddle_remote ?=
ifeq (,$(fiddle_remote))
ifneq (,$(wildcard /home/stephan))
fiddle_remote = wh:www/wh/sqlite3/.
fiddle_remote = wh:www/wasm-testing/fiddle/.
else ifneq (,$(wildcard /home/drh))
#fiddle_remote = if appropriate, add that user@host:/path here
endif

View File

@ -112,7 +112,7 @@
/**
A proxy for localStorage or sessionStorage or a
page-instance-local proxy, if neither one is availble.
page-instance-local proxy, if neither one is available.
Which exact storage implementation is uses is unspecified, and
apps must not rely on it.

View File

@ -84,8 +84,8 @@
wrapper is significantly easier to use, however.</li>
<li><a href='demo-worker1-promiser.html'>demo-worker1-promiser</a>:
a demo of the Promise-based wrapper of the Worker1 API.</li>
<!--li><a href='demo-worker1-promiser-esm.html'>demo-worker1-promiser-esm</a>:
same as the previous demo except loads the promiser from an ESM module.</li-->
<li><a href='demo-worker1-promiser-esm.html'>demo-worker1-promiser-esm</a>:
same as the previous demo except loads the promiser from an ESM module.</li>
</ul>
</li>
<li>speedtest1 ports (sqlite3's primary benchmarking tool)...
@ -119,6 +119,10 @@
<li><a href='tests/opfs/concurrency/index.html'>OPFS concurrency</a>
tests using multiple workers.
</li>
<li><a href='tests/opfs/sahpool/index.html'>OPFS SAHPool cooperative semi-concurrency</a>
demonstrates usage of the OPFS SAHPool VFS's "pause" feature to coordinate
access to a database.
</li>
</ul>
</li>
<li><strong>WASMFS</strong>-specific tests which require that

View File

@ -950,7 +950,7 @@ const char * wasm__ctype_json(void){
assert(LEVEL<5); memset(pos, '}', LEVEL); pos+=LEVEL; lenCheck
////////////////////////////////////////////////////////////////////
// Macros for emiting StructBinders...
// Macros for emitting StructBinders...
#define StructBinder__(TYPE) \
n = 0; \
outf("%s{", (structCount++ ? ", " : "")); \

View File

@ -135,6 +135,28 @@ static void mk_prologue(void){
}
}
/*
** Flags for use with the 3rd argument to mk_pre_post() and
** mk_lib_mode().
**
** Maintenance reminder: do not combine flags within this enum,
** e.g. LIBMODE_BUNDLER_FRIEND=0x02|LIBMODE_ESM, as that will lead to
** breakage in some of the flag checks.
*/
enum LibModeFlags {
/* Indicates an ESM module build. */
LIBMODE_ESM = 0x01,
/* Indicates a "bundler-friendly" build mode. */
LIBMODE_BUNDLER_FRIENDLY = 0x02,
/* Indicates to _not_ add this build to the 'all' target. */
LIBMODE_DONT_ADD_TO_ALL = 0x04,
/* Indicates a node.js-for-node.js build (untested and
** unsupported). */
LIBMODE_NODEJS = 0x08,
/* Indicates a wasmfs build (untested and unsupported). */
LIBMODE_WASMFS = 0x10
};
/*
** Emits makefile code for setting up values for the --pre-js=FILE,
** --post-js=FILE, and --extern-post-js=FILE emcc flags, as well as
@ -142,6 +164,7 @@ static void mk_prologue(void){
*/
static void mk_pre_post(const char *zName /* build name */,
const char *zMode /* build mode */,
int flags /* LIBMODE_... mask */,
const char *zCmppD /* optional -D flags for c-pp for the
** --pre/--post-js files. */){
pf("%s# Begin --pre/--post flags for %s-%s\n", zBanner, zNM);
@ -151,7 +174,7 @@ static void mk_pre_post(const char *zName /* build name */,
/* --pre-js=... */
pf("pre-js.js.%s-%s := $(dir.tmp)/pre-js.%s-%s.js\n",
zNM, zNM);
pf("$(pre-js.js.%s-%s): $(MAKEFILE)\n", zNM);
pf("$(pre-js.js.%s-%s): $(MAKEFILE_LIST)\n", zNM);
#if 1
pf("$(eval $(call SQLITE.CALL.C-PP.FILTER,$(pre-js.js.in),$(pre-js.js.%s-%s),"
"$(c-pp.D.%s-%s)))\n", zNM, zNM);
@ -166,9 +189,10 @@ static void mk_pre_post(const char *zName /* build name */,
pf("\tcp $(pre-js.js.%s-%s.intermediary) $@\n", zNM);
/* Amend $(pre-js.js.zName-zMode) for all targets except the plain
** "sqlite3" build... */
** "sqlite3" and the "sqlite3-wasmfs" builds... */
if( 0!=strcmp("sqlite3-wasmfs", zName)
&& 0!=strcmp("sqlite3", zName) ){
#error "This part ^^^ is needs adapting for use with the LIBMODE_... flags"
pf("\t@echo 'Module[xNameOfInstantiateWasm].uri = "
"\"%s.wasm\";' >> $@\n", zName);
}
@ -209,7 +233,7 @@ static void mk_pre_post(const char *zName /* build name */,
static void mk_fiddle(){
int i = 0;
mk_pre_post("fiddle-module","vanilla", 0);
mk_pre_post("fiddle-module","vanilla", 0, 0);
for( ; i < 2; ++i ){
const char *zTail = i ? ".debug" : "";
const char *zDir = i ? "$(dir.fiddle-debug)" : "$(dir.fiddle)";
@ -218,7 +242,7 @@ static void mk_fiddle(){
pf("fiddle-module.js%s := %s/fiddle-module.js\n", zTail, zDir);
pf("fiddle-module.wasm%s := "
"$(subst .js,.wasm,$(fiddle-module.js%s))\n", zTail, zTail);
pf("$(fiddle-module.js%s):%s $(MAKEFILE) $(MAKEFILE.fiddle) "
pf("$(fiddle-module.js%s):%s $(MAKEFILE_LIST) $(MAKEFILE.fiddle) "
"$(EXPORTED_FUNCTIONS.fiddle) "
"$(fiddle.cses) $(pre-post-fiddle-module-vanilla.deps) "
"$(SOAP.js)\n",
@ -257,7 +281,7 @@ static void mk_fiddle(){
*/
static void mk_lib_mode(const char *zName /* build name */,
const char *zMode /* build mode */,
int bIsEsm /* true only for ESM build */,
int flags /* LIBMODE_... mask */,
const char *zApiJsOut /* name of generated sqlite3-api.js/.mjs */,
const char *zJsOut /* name of generated sqlite3.js/.mjs */,
const char *zCmppD /* extra -D flags for c-pp */,
@ -274,8 +298,9 @@ static void mk_lib_mode(const char *zName /* build name */,
if( !zEmcc ) zEmcc = "";
pf("%s# Begin build [%s-%s]\n", zBanner, zNM);
pf("# zApiJsOut=%s\n# zJsOut=%s\n# zCmppD=%s\n", zApiJsOut, zJsOut, zCmppD);
pf("$(info Setting up build [%s-%s]: %s)\n", zNM, zJsOut);
mk_pre_post(zNM, zCmppD);
mk_pre_post(zNM, flags, zCmppD);
pf("\nemcc.flags.%s.%s ?=\n", zNM);
if( zEmcc[0] ){
pf("emcc.flags.%s.%s += %s\n", zNM, zEmcc);
@ -284,7 +309,7 @@ static void mk_lib_mode(const char *zName /* build name */,
zApiJsOut, zCmppD);
/* target zJsOut */
pf("%s: %s $(MAKEFILE) $(sqlite3-wasm.cfiles) $(EXPORTED_FUNCTIONS.api) "
pf("%s: %s $(MAKEFILE_LIST) $(sqlite3-wasm.cfiles) $(EXPORTED_FUNCTIONS.api) "
"$(pre-post-%s-%s.deps) "
"$(sqlite3-api.ext.jses)"
/* ^^^ maintenance reminder: we set these as deps so that they
@ -302,12 +327,13 @@ static void mk_lib_mode(const char *zName /* build name */,
pf("\t\t$(cflags.common) $(SQLITE_OPT) \\\n"
"\t\t$(cflags.%s) $(cflags.%s.%s) \\\n"
"\t\t$(cflags.wasm_extra_init) $(sqlite3-wasm.cfiles)\n", zName, zNM);
if( bIsEsm ){
/* TODO? Replace this CALL with the corresponding makefile code.
** OTOH, we also use this $(call) in the speedtest1-wasmfs build,
** which is not part of the rules emitted by this program. */
if( LIBMODE_ESM & flags ){
/* TODO? Replace this $(call) with the corresponding makefile
** code. OTOH, we also use this $(call) in the speedtest1-wasmfs
** build, which is not part of the rules emitted by this
** program. */
pf("\t@$(call SQLITE.CALL.xJS.ESM-EXPORT-DEFAULT,1,%d)\n",
0==strcmp("sqlite3-wasmfs", zName) ? 1 : 0);
(LIBMODE_WASMFS & flags) ? 1 : 0);
}
pf("\t@chmod -x %s; \\\n"
"\t\t$(maybe-wasm-strip) %s;\n",
@ -329,20 +355,28 @@ static void mk_lib_mode(const char *zName /* build name */,
** resulting .wasm file is identical for all builds for which zEmcc
** is empty.
*/
if( 0==strcmp("bundler-friendly", zMode)
|| 0==strcmp("node", zMode) ){
if( (LIBMODE_BUNDLER_FRIENDLY & flags)
|| (LIBMODE_NODEJS & flags) ){
pf("\t@echo 'Patching $@ for %s.wasm...'; \\\n", zName);
pf("\t\trm -f %s; \\\n", zWasmOut);
pf("\t\tsed -i -e 's/%s-%s.wasm/%s.wasm/g' $@ || exit;\n",
/* ^^^^^^ reminder: Mac/BSD sed has no -i flag */
zNM, zName);
pf("\t@ls -la $@\n");
if( LIBMODE_BUNDLER_FRIENDLY & flags ){
/* Avoid a 3rd occurance of the bug fixed by 65798c09a00662a3,
** which was (in two cases) caused by makefile refactoring and
** not recognized until after a release was made with the broken
** sqlite3-bundler-friendly.mjs: */
pf("\t@if grep -e '^ *importScripts(' $@; "
"then echo 'ERROR: bug fixed in 65798c09a00662a3 has re-appeared'; "
"exit 1; fi;\n");
}
}else{
pf("\t@ls -la %s $@\n", zWasmOut);
}
if( 0!=strcmp("sqlite3-wasmfs", zName) ){
/* The sqlite3-wasmfs build is optional and needs to be invoked
** conditionally using info we don't have here. */
if( 0==(LIBMODE_DONT_ADD_TO_ALL & flags) ){
pf("all: %s\n", zJsOut);
}
pf("# End build [%s-%s]%s", zNM, zBanner);
@ -354,22 +388,29 @@ int main(void){
mk_prologue();
mk_lib_mode("sqlite3", "vanilla", 0,
"$(sqlite3-api.js)", "$(sqlite3.js)", 0, 0);
mk_lib_mode("sqlite3", "esm", 1,
mk_lib_mode("sqlite3", "esm", LIBMODE_ESM,
"$(sqlite3-api.mjs)", "$(sqlite3.mjs)",
"-Dtarget=es6-module", 0);
mk_lib_mode("sqlite3", "bundler-friendly", 1,
"$(sqlite3-api-bundler-friendly.mjs)", "$(sqlite3-bundler-friendly.mjs)",
mk_lib_mode("sqlite3", "bundler-friendly",
LIBMODE_BUNDLER_FRIENDLY | LIBMODE_ESM,
"$(sqlite3-api-bundler-friendly.mjs)",
"$(sqlite3-bundler-friendly.mjs)",
"$(c-pp.D.sqlite3-esm) -Dtarget=es6-bundler-friendly", 0);
mk_lib_mode("sqlite3" , "node", 1,
mk_lib_mode("sqlite3" , "node",
LIBMODE_NODEJS | LIBMODE_DONT_ADD_TO_ALL,
"$(sqlite3-api-node.mjs)", "$(sqlite3-node.mjs)",
"$(c-pp.D.sqlite3-bundler-friendly) -Dtarget=node", 0);
mk_lib_mode("sqlite3-wasmfs", "esm" ,1,
mk_lib_mode("sqlite3-wasmfs", "esm" ,
LIBMODE_WASMFS | LIBMODE_ESM | LIBMODE_DONT_ADD_TO_ALL,
/* The sqlite3-wasmfs build is optional and needs to be invoked
** conditionally using info we don't have here. */
"$(sqlite3-api-wasmfs.mjs)", "$(sqlite3-wasmfs.mjs)",
"$(c-pp.D.sqlite3-bundler-friendly) -Dwasmfs",
"-sEXPORT_ES6 -sUSE_ES6_IMPORT_META");
mk_fiddle();
mk_pre_post("speedtest1","vanilla", 0);
mk_pre_post("speedtest1-wasmfs","esm", "$(c-pp.D.sqlite3-bundler-friendly) -Dwasmfs");
mk_pre_post("speedtest1","vanilla", 0, 0);
mk_pre_post("speedtest1-wasmfs","esm", 0,
"$(c-pp.D.sqlite3-bundler-friendly) -Dwasmfs");
return rc;
}

View File

@ -3189,8 +3189,25 @@ globalThis.sqlite3InitModule = sqlite3InitModule;
db.close();
T.assert(1 === u1.getFileCount());
db = new u2.OpfsSAHPoolDb(dbName);
T.assert(1 === u1.getFileCount());
T.assert(1 === u1.getFileCount())
.mustThrowMatching(
()=>u1.pauseVfs(),
(err)=>{
return capi.SQLITE_MISUSE===err.resultCode
&& /^SQLITE_MISUSE: Cannot pause VFS /.test(err.message);
},
"Cannot pause VFS with opened db."
);
db.close();
T.assert( u2===u2.pauseVfs() )
.assert( u2.isPaused() )
.assert( 0===capi.sqlite3_vfs_find(u2.vfsName) )
.mustThrowMatching(()=>new u2.OpfsSAHPoolDb(dbName),
/.+no such vfs: .+/,
"VFS is not available")
.assert( u2===await u2.unpauseVfs() )
.assert( u2===await u1.unpauseVfs(), "unpause is a no-op if the VFS is not paused" )
.assert( 0!==capi.sqlite3_vfs_find(u2.vfsName) );
const fileNames = u1.getFileNames();
T.assert(1 === fileNames.length)
.assert(dbName === fileNames[0])
@ -3445,7 +3462,6 @@ globalThis.sqlite3InitModule = sqlite3InitModule;
const stack = wasm.pstack.pointer;
const pAux = wasm.pstack.alloc(4);
let pAuxDestructed = 0;
const args = [];
const pAuxDtor = wasm.installFunction('v(p)', function(ptr){
//log("freeing auxdata");
++pAuxDestructed;
@ -3457,10 +3473,11 @@ globalThis.sqlite3InitModule = sqlite3InitModule;
wasm.uninstallFunction(pAuxDtor);
}
};
let nAuxSet = 0 /* how many times we set aux data */;
let nAuxReused = 0 /* how many times we reused aux data */;
try{
db.createFunction("auxtest",{
xFunc: function(pCx, x, y){
args.push(x);
T.assert(wasm.isPtr(pCx));
const localAux = capi.sqlite3_get_auxdata(pCx, 0);
if( !localAux ){
@ -3469,23 +3486,20 @@ globalThis.sqlite3InitModule = sqlite3InitModule;
We do not currently an automated way to clean up
auxdata finalizer functions (the 4th argument to
sqlite3_set_auxdata()) which get automatically
converted from JS to WASM. Because of that, relying
on automated conversions for those is not
recommended. Instead, follow the pattern show in
converted from JS to WASM. Because of that, enabling
automated conversions here would lead to leaks more
often than not. Instead, follow the pattern show in
this function: use wasm.installFunction() to create
the function, then pass the resulting function
pointer this function, and cleanup (at some point)
using wasm.uninstallFunction().
*/
++nAuxSet;
capi.sqlite3_set_auxdata(pCx, 0, pAux, pAuxDtor);
}else{
/* This is never actually hit in this example and it's
not entirely clear how to cause it to. The point of
this test, however, is to demonstrate that the
finalizer impl gets triggered, so we're not going to
fret over this at the moment. */
//log("seen auxdata",localAux);
//log("reusing auxdata",localAux);
T.assert(pAux===localAux);
++nAuxReused;
}
return x;
}
@ -3493,13 +3507,14 @@ globalThis.sqlite3InitModule = sqlite3InitModule;
db.exec([
"create table t(a);",
"insert into t(a) values(1),(2),(1);",
"select auxtest(a,a), auxtest(a,a) from t order by a"
"select auxtest(1,a), auxtest(1,a) from t order by a"
]);
}finally{
db.close();
wasm.pstack.restore(stack);
}
T.assert(6===args.length);
T.assert(nAuxSet>0).assert(nAuxReused>0)
.assert(6===nAuxReused+nAuxSet);
T.assert(pAuxDestructed>0);
T.assert(pAuxDtorDestructed);
}

View File

@ -0,0 +1,31 @@
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<link rel="stylesheet" href="../../../common/emscripten.css"/>
<link rel="stylesheet" href="../../../common/testing.css"/>
<title>sqlite3 tester: OpfsSAHPool Pausing</title>
<style></style>
</head>
<body><h1 id='color-target'></h1>
<p>
This page provides a <em>very basic</em> demonstration of
"pausing" and "unpausing" the OPFS SAHPool VFS such that
multiple pages or workers can use it by coordinating which
handler may have it open at any given time.
</p>
<div class='input-wrapper'>
<input type='checkbox' id='cb-log-reverse'>
<label for='cb-log-reverse'>Reverse log order?</label>
</div>
<div id='test-output'></div>
<script>(function(){
document.querySelector('h1').innerHTML =
document.querySelector('title').innerHTML;
})();</script>
<script src="sahpool-pausing.js"></script>
</body>
</html>

View File

@ -0,0 +1,183 @@
/*
2025-01-31
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.
***********************************************************************
These tests are specific to the opfs-sahpool VFS and are limited to
demonstrating its pause/unpause capabilities.
Most of this file is infrastructure for displaying results to the
user. Search for runTests() to find where the work actually starts.
*/
'use strict';
(function(){
let logClass;
const mapToString = (v)=>{
switch(typeof v){
case 'number': case 'string': case 'boolean':
case 'undefined': case 'bigint':
return ''+v;
default: break;
}
if(null===v) return 'null';
if(v instanceof Error){
v = {
message: v.message,
stack: v.stack,
errorClass: v.name
};
}
return JSON.stringify(v,undefined,2);
};
const normalizeArgs = (args)=>args.map(mapToString);
const logTarget = document.querySelector('#test-output');
logClass = function(cssClass,...args){
const ln = document.createElement('div');
if(cssClass){
for(const c of (Array.isArray(cssClass) ? cssClass : [cssClass])){
ln.classList.add(c);
}
}
ln.append(document.createTextNode(normalizeArgs(args).join(' ')));
logTarget.append(ln);
};
const cbReverse = document.querySelector('#cb-log-reverse');
//cbReverse.setAttribute('checked','checked');
const cbReverseKey = 'tester1:cb-log-reverse';
const cbReverseIt = ()=>{
logTarget.classList[cbReverse.checked ? 'add' : 'remove']('reverse');
//localStorage.setItem(cbReverseKey, cbReverse.checked ? 1 : 0);
};
cbReverse.addEventListener('change', cbReverseIt, true);
/*if(localStorage.getItem(cbReverseKey)){
cbReverse.checked = !!(+localStorage.getItem(cbReverseKey));
}*/
cbReverseIt();
const log = (...args)=>{
//console.log(...args);
logClass('',...args);
}
const warn = (...args)=>{
console.warn(...args);
logClass('warning',...args);
}
const error = (...args)=>{
console.error(...args);
logClass('error',...args);
};
const toss = (...args)=>{
error(...args);
throw new Error(args.join(' '));
};
const endOfWork = (passed=true)=>{
const eH = document.querySelector('#color-target');
const eT = document.querySelector('title');
if(passed){
log("End of work chain. If you made it this far, you win.");
eH.innerText = 'PASS: '+eH.innerText;
eH.classList.add('tests-pass');
eT.innerText = 'PASS: '+eT.innerText;
}else{
eH.innerText = 'FAIL: '+eH.innerText;
eH.classList.add('tests-fail');
eT.innerText = 'FAIL: '+eT.innerText;
}
};
const nextHandlerQueue = [];
const nextHandler = function(workerId,...msg){
log(workerId,...msg);
(nextHandlerQueue.shift())();
};
const postThen = function(W, msgType, callback){
nextHandlerQueue.push(callback);
W.postMessage({type:msgType});
};
/**
Run a series of operations on an sahpool db spanning two workers.
This would arguably be more legible with Promises, but creating a
Promise-based communication channel for this purpose is left as
an exercise for the reader. An example of such a proxy can be
found in the SQLite source tree:
https://sqlite.org/src/file/ext/wasm/api/sqlite3-worker1-promiser.c-pp.js
*/
const runPyramidOfDoom = function(W1, W2){
postThen(W1, 'vfs-acquire', function(){
postThen(W1, 'db-init', function(){
postThen(W1, 'db-query', function(){
postThen(W1, 'vfs-pause', function(){
postThen(W2, 'vfs-acquire', function(){
postThen(W2, 'db-query', function(){
postThen(W2, 'vfs-remove', endOfWork);
});
});
});
});
});
});
};
const runTests = function(){
log("Running opfs-sahpool pausing tests...");
const wjs = 'sahpool-worker.js?sqlite3.dir=../../../jswasm';
const W1 = new Worker(wjs+'&workerId=w1'),
W2 = new Worker(wjs+'&workerId=w2');
W1.workerId = 'w1';
W2.workerId = 'w2';
let initCount = 0;
const onmessage = function({data}){
//log("onmessage:",data);
switch(data.type){
case 'vfs-acquired':
nextHandler(data.workerId, "VFS acquired");
break;
case 'vfs-paused':
nextHandler(data.workerId, "VFS paused");
break;
case 'vfs-unpaused':
nextHandler(data.workerId, 'VFS unpaused');
break;
case 'vfs-removed':
nextHandler(data.workerId, 'VFS removed');
break;
case 'db-inited':
nextHandler(data.workerId, 'db initialized');
break;
case 'query-result':
nextHandler(data.workerId, 'query result', data.payload);
break;
case 'log':
log(data.workerId, ':', ...data.payload);
break;
case 'error':
error(data.workerId, ':', ...data.payload);
endOfWork(false);
break;
case 'initialized':
log(data.workerId, ': Worker initialized',...data.payload);
if( 2===++initCount ){
runPyramidOfDoom(W1, W2);
}
break;
}
};
W1.onmessage = W2.onmessage = onmessage;
};
runTests();
})();

View File

@ -0,0 +1,104 @@
/*
2025-01-31
The author disclaims copyright to this source code. In place of a
legal notice, here is a blessing:
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
***********************************************************************
This file is part of sahpool-pausing.js's demonstration of the
pause/unpause feature of the opfs-sahpool VFS.
*/
const searchParams = new URL(self.location.href).searchParams;
const workerId = searchParams.get('workerId');
const wPost = (type,...args)=>postMessage({type, workerId, payload:args});
const log = (...args)=>wPost('log',...args);
let capi, wasm, S, poolUtil;
const sahPoolConfig = {
name: 'opfs-sahpool-pausable',
clearOnInit: false,
initialCapacity: 3
};
importScripts(searchParams.get('sqlite3.dir') + '/sqlite3.js');
const sqlExec = function(sql){
const db = new poolUtil.OpfsSAHPoolDb('/my.db');
try{
return db.exec(sql);
}finally{
db.close();
}
};
const clog = console.log.bind(console);
globalThis.onmessage = function({data}){
clog(workerId+": onmessage:",data);
switch(data.type){
case 'vfs-acquire':
if( poolUtil ){
poolUtil.unpauseVfs().then(()=>wPost('vfs-unpaused'));
}else{
S.installOpfsSAHPoolVfs(sahPoolConfig).then(pu=>{
poolUtil = pu;
wPost('vfs-acquired');
});
}
break;
case 'db-init':
try{
sqlExec([
"DROP TABLE IF EXISTS mytable;",
"CREATE TABLE mytable(a);",
"INSERT INTO mytable(a) VALUES(11),(22),(33)"
]);
wPost('db-inited');
}catch(e){
wPost('error',e.message);
}
break;
case 'db-query': {
const rc = sqlExec({
sql: 'select * from mytable order by a',
rowMode: 'array',
returnValue: 'resultRows'
});
wPost('query-result',rc);
break;
}
case 'vfs-remove':
poolUtil.removeVfs().then(()=>wPost('vfs-removed'));
break;
case 'vfs-pause':
poolUtil.pauseVfs();
wPost('vfs-paused');
break;
}
};
const hasOpfs = ()=>{
return globalThis.FileSystemHandle
&& globalThis.FileSystemDirectoryHandle
&& globalThis.FileSystemFileHandle
&& globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle
&& navigator?.storage?.getDirectory;
};
if( !hasOpfs() ){
wPost('error',"OPFS not detected");
}else{
globalThis.sqlite3InitModule().then(async function(sqlite3){
S = sqlite3;
capi = S.capi;
wasm = S.wasm;
log("sqlite3 version:",capi.sqlite3_libversion(),
capi.sqlite3_sourceid());
//return sqlite3.installOpfsSAHPoolVfs(sahPoolConfig).then(pu=>poolUtil=pu);
}).then(()=>{
wPost('initialized');
});
}