Minor cleanups and documentation in the wasm pieces.

FossilOrigin-Name: 4e6ce329872eb733ba2f7f7879747c52761ae97790fd8ed169a25a79854cc3d9
This commit is contained in:
stephan 2022-09-11 16:59:40 +00:00
parent 2a91126d76
commit 73079dba00
9 changed files with 90 additions and 39 deletions

View File

@ -66,11 +66,5 @@ _sqlite3_value_text
_sqlite3_value_type
_sqlite3_vfs_find
_sqlite3_vfs_register
_sqlite3_wasm_db_error
_sqlite3_wasm_enum_json
_sqlite3_wasm_init_opfs
_sqlite3_wasm_vfs_unlink
_sqlite3_wasm__emjs_test
_sqlite3_wasm__kvvfsMakeKey
_malloc
_free

View File

@ -63,6 +63,9 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
for(const e of wasm.bindingSignatures){
capi[e[0]] = wasm.xWrap.apply(null, e);
}
for(const e of wasm.bindingSignatures.wasm){
capi.wasm[e[0]] = wasm.xWrap.apply(null, e);
}
/* For C API functions which cannot work properly unless
wasm.bigIntEnabled is true, install a bogus impl which

View File

@ -649,9 +649,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap(
["sqlite3_value_text", "string", "*"],
["sqlite3_value_type", "int", "*"],
["sqlite3_vfs_find", "*", "string"],
["sqlite3_vfs_register", "int", "*", "int"],
["sqlite3_wasm_vfs_unlink", "int", "string"],
["sqlite3_wasm__emjs_test", "int", "int"]
["sqlite3_vfs_register", "int", "*", "int"]
]/*capi.wasm.bindingSignatures*/;
if(false && capi.wasm.compileOptionUsed('SQLITE_ENABLE_NORMALIZE')){
@ -673,6 +671,15 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap(
["sqlite3_total_changes64", "i64", ["sqlite3*"]]
];
/**
Functions which are intended solely for API-internal use by the
WASM components, not client code. These get installed into
capi.wasm.
*/
capi.wasm.bindingSignatures.wasm = [
["sqlite3_wasm_vfs_unlink", "int", "string"]
];
/** State for sqlite3_web_persistent_dir(). */
let __persistentDir;
/**

View File

@ -375,7 +375,7 @@ sqlite3.initWorker1API = function(){
db.close();
if(db===this.defaultDb) this.defaultDb = undefined;
if(alsoUnlink && filename){
sqlite3.capi.sqlite3_wasm_vfs_unlink(filename);
sqlite3.capi.wasm.sqlite3_wasm_vfs_unlink(filename);
}
}
},

View File

@ -1,5 +1,39 @@
/*
** This file requires access to sqlite3.c static state in order to
** implement certain WASM-specific features. Unlike the rest of
** sqlite3.c, this file requires compiling with -std=c99 (or
** equivalent, or a later C version) because it makes use of features
** not available in C89.
*/
#include "sqlite3.c"
/*
** WASM_KEEP is identical to EMSCRIPTEN_KEEPALIVE but is not
** Emscripten-specific. It explicitly includes marked functions for
** export into the target wasm file without requiring explicit listing
** of those functions in Emscripten's -sEXPORTED_FUNCTIONS=... list
** (or equivalent in other build platforms). Any function with neither
** this attribute nor which is listed as an explicit export will not
** be exported from the wasm file (but may still be used internally
** within the wasm file).
**
** The functions in this file (sqlite3-wasm.c) which require exporting
** are marked with this flag. They may also be added to any explicit
** build-time export list but need not be. All of these APIs are
** intended for use only within the project's own JS/WASM code, and
** not by client code, so an argument can be made for reducing their
** visibility by not including them in any build-time export lists.
**
** 2022-09-11: it's not yet _proven_ that this approach works in
** non-Emscripten builds. If not, such builds will need to export
** those using the --export=... wasm-ld flag (or equivalent). As of
** this writing we are tied to Emscripten for various reasons
** and cannot test the library with other build environments.
*/
#define WASM_KEEP __attribute__((used,visibility("default")))
// See also:
//__attribute__((export_name("theExportedName"), used, visibility("default")))
/*
** This function is NOT part of the sqlite3 public API. It is strictly
** for use by the sqlite project's own JS/WASM bindings.
@ -14,8 +48,8 @@
**
** Returns err_code.
*/
int sqlite3_wasm_db_error(sqlite3*db, int err_code,
const char *zMsg){
WASM_KEEP
int sqlite3_wasm_db_error(sqlite3*db, int err_code, const char *zMsg){
if(0!=zMsg){
const int nMsg = sqlite3Strlen30(zMsg);
sqlite3ErrorWithMsg(db, err_code, "%.*s", nMsg, zMsg);
@ -40,6 +74,7 @@ int sqlite3_wasm_db_error(sqlite3*db, int err_code,
** buffer is not large enough for the generated JSON. In debug builds
** that will trigger an assert().
*/
WASM_KEEP
const char * sqlite3_wasm_enum_json(void){
static char strBuf[1024 * 8] = {0} /* where the JSON goes */;
int n = 0, childCount = 0, structCount = 0
@ -421,6 +456,7 @@ const char * sqlite3_wasm_enum_json(void){
** found, or it has no xDelete method, SQLITE_MISUSE is returned, else
** the result of the xDelete() call is returned.
*/
WASM_KEEP
int sqlite3_wasm_vfs_unlink(const char * zName){
int rc = SQLITE_MISUSE /* ??? */;
sqlite3_vfs * const pVfs = sqlite3_vfs_find(0);
@ -433,6 +469,7 @@ int sqlite3_wasm_vfs_unlink(const char * zName){
#if defined(__EMSCRIPTEN__) && defined(SQLITE_WASM_OPFS)
#include <emscripten/wasmfs.h>
#include <emscripten/console.h>
/*
** This function is NOT part of the sqlite3 public API. It is strictly
** for use by the sqlite project's own JS/WASM bindings, specifically
@ -454,6 +491,7 @@ int sqlite3_wasm_vfs_unlink(const char * zName){
** the virtual FS fails. In builds compiled without SQLITE_WASM_OPFS
** defined, SQLITE_NOTFOUND is returned without side effects.
*/
WASM_KEEP
int sqlite3_wasm_init_opfs(const char *zMountPoint){
static backend_t pOpfs = 0;
if( !zMountPoint || !*zMountPoint ) zMountPoint = "/persistent";
@ -479,13 +517,15 @@ int sqlite3_wasm_init_opfs(const char *zMountPoint){
return pOpfs ? 0 : SQLITE_NOMEM;
}
#else
WASM_KEEP
int sqlite3_wasm_init_opfs(void){
return SQLITE_NOTFOUND;
}
#endif /* __EMSCRIPTEN__ && SQLITE_WASM_OPFS */
#if defined(__EMSCRIPTEN__) // && defined(SQLITE_OS_KV)
#include "emscripten.h"
#include <emscripten.h>
#include <emscripten/console.h>
#ifndef KVSTORAGE_KEY_SZ
/* We can remove this once kvvfs and this bit is merged. */
@ -511,6 +551,7 @@ static void kvstorageMakeKey(
** Maintenance reminder: Emscripten will install this in the Module
** init scope and will prefix its name with "_".
*/
WASM_KEEP
int sqlite3_wasm__kvvfsMakeKey(const char *zClass,
const char *zKeyIn,
char *zKeyOut){
@ -522,9 +563,11 @@ int sqlite3_wasm__kvvfsMakeKey(const char *zClass,
/*
** Alternately, we can implement kvstorageMakeKey() in JS in such a
** way that it's visible to kvstorageWrite/Delete/Read() but not the
** rest of the world. This impl is considerably more verbose than
** the C impl because writing directly to memory requires more code in
** JS.
** rest of the world. This impl is considerably more verbose than the
** C impl because writing directly to memory requires more code in
** JS. Though more verbose, this approach enables removal of
** sqlite3_wasm__kvvfsMakeKey(). The only catch is that the
** KVSTORAGE_KEY_SZ constant has to be hard-coded into this function.
*/
EM_JS(void, kvstorageMakeKeyJS,
(const char *zClass, const char *zKeyIn, char *zKeyOut),{
@ -673,11 +716,13 @@ EM_JS(int, kvstorageRead,
** functions. It is not part of the public API and its signature
** and semantics may change at any time.
*/
int sqlite3_wasm__emjs_test(int whichOp){
WASM_KEEP
int sqlite3_wasm__emjs_keep(int whichOp){
int rc = 0;
const char * zClass = "session";
const char * zKey = "hello";
int rc = 0;
switch( whichOp ){
case 0: break;
case 1:
kvstorageWrite(zClass, zKey, "world");
break;
@ -685,17 +730,19 @@ int sqlite3_wasm__emjs_test(int whichOp){
char buffer[128] = {0};
char * zBuf = &buffer[0];
rc = kvstorageRead(zClass, zKey, zBuf, (int)sizeof(buffer));
printf("kvstorageRead()=%d %s\n", rc, zBuf);
emscripten_console_logf("kvstorageRead()=%d %s\n", rc, zBuf);
break;
}
case 3:
kvstorageDelete(zClass, zKey);
break;
default:
kvstorageMakeKeyOnJSStack(0,0) /* force Emscripten to include this */;
break;
case 4:
kvstorageMakeKeyOnJSStack(0,0) /* force Emscripten to include this */;
break;
default: break;
}
return rc;
}
#endif /* ifdef __EMSCRIPTEN__ (kvvfs method impls) */
#endif /* ifdef __EMSCRIPTEN__ */
#undef WASM_KEEP

View File

@ -69,7 +69,7 @@
let pDb = 0;
try{
if(unlinkFirst && fn && ':memory:'!==fn){
capi.sqlite3_wasm_vfs_unlink(fn);
capi.wasm.sqlite3_wasm_vfs_unlink(fn);
}
const oFlags = capi.SQLITE_OPEN_CREATE | capi.SQLITE_OPEN_READWRITE;
const ppDb = wasm.scopedAllocPtr();
@ -93,7 +93,7 @@
if(this.db && this.db.ptr){
this.sqlite3.capi.sqlite3_close_v2(this.db.ptr);
this.logHtml("Closed db",this.db.filename);
if(unlink) capi.sqlite3_wasm_vfs_unlink(this.db.filename);
if(unlink) capi.wasm.sqlite3_wasm_vfs_unlink(this.db.filename);
this.db.ptr = this.db.filename = undefined;
}
},

View File

@ -30,7 +30,7 @@
0 ? "" : capi.sqlite3_web_persistent_dir()
)+"/mydb.sqlite3"
if(0 && capi.sqlite3_web_persistent_dir()){
capi.sqlite3_wasm_vfs_unlink(dbName);
capi.wasm.sqlite3_wasm_vfs_unlink(dbName);
}
const db = new oo.DB(dbName);
log("db =",db.filename);

View File

@ -1,5 +1,5 @@
C Correct\sthe\sresult\scode\sfrom\skvstorageRead()\sfor\sthe\sOOM\scase\sto\sbe\sa\snegative\svalue.
D 2022-09-11T05:44:15.092
C Minor\scleanups\sand\sdocumentation\sin\sthe\swasm\spieces.
D 2022-09-11T16:59:40.674
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -476,27 +476,27 @@ F ext/wasm/EXPORTED_FUNCTIONS.fiddle db7a4602f043cf4a5e4135be3609a487f9f1c83f057
F ext/wasm/EXPORTED_RUNTIME_METHODS.fiddle a004bd5eeeda6d3b28d16779b7f1a80305bfe009dfc7f0721b042967f0d39d02
F ext/wasm/GNUmakefile 18b80a063c684b0ca44b96221fc2c23647d0196f757c83c7a572b853562d8ac9
F ext/wasm/README.md e1ee1e7c321c6a250bf78a84ca6f5882890a237a450ba5a0649c7a8399194c52
F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api d6396db6405ecfc4d25ebee8c1fab29d4522c852fcf9011df8bbcd54da00c14a
F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api 1dfd067b3cbd9a49cb204097367cf2f8fe71b5a3b245d9d82a24779fd4ac2394
F ext/wasm/api/EXPORTED_RUNTIME_METHODS.sqlite3-api 1ec3c73e7d66e95529c3c64ac3de2470b0e9e7fbf7a5b41261c367cf4f1b7287
F ext/wasm/api/README.md d876597edd2b9542b6ea031adaaff1c042076fde7b670b1dc6d8a87b28a6631b
F ext/wasm/api/post-js-footer.js b64319261d920211b8700004d08b956a6c285f3b0bba81456260a713ed04900c
F ext/wasm/api/post-js-header.js 0e853b78db83cb1c06b01663549e0e8b4f377f12f5a2d9a4a06cb776c003880b
F ext/wasm/api/sqlite3-api-cleanup.js 1a12e64060c2cb0defd34656a76a9b1d7ed58459c290249bb31567c806fd44de
F ext/wasm/api/sqlite3-api-glue.js 67ca83974410961953eeaa1dfed3518530d68381729ed1d27f95122f5baeabd3
F ext/wasm/api/sqlite3-api-glue.js 2bf536a38cde324cf352bc2c575f8e22c6d204d667c0eda5a254ba45318914bc
F ext/wasm/api/sqlite3-api-oo1.js b06a1ac982c7d433396b8304550ce1493a63671a3bf653c3b5646a9075e0ca41
F ext/wasm/api/sqlite3-api-opfs.js 011799db398157cbd254264b6ebae00d7234b93d0e9e810345f213a5774993c0
F ext/wasm/api/sqlite3-api-prologue.js 88e58145b0c71b00cd523b656016193d6ce8cf22c7ece8225838625f9187e6d1
F ext/wasm/api/sqlite3-api-worker1.js 73579555563b789785ae83724014eaf31811073aad9be6596c8336ffb51edd71
F ext/wasm/api/sqlite3-api-prologue.js afd753be49ecba4b5bfc3e54929826a01eaf96ac2d8cec575d683e3c5bcc8464
F ext/wasm/api/sqlite3-api-worker1.js d33062afa045fd4be01ba4abc266801807472558b862b30056211b00c9c347b4
F ext/wasm/api/sqlite3-wasi.h 25356084cfe0d40458a902afb465df8c21fc4152c1d0a59b563a3fba59a068f9
F ext/wasm/api/sqlite3-wasm.c 7fc3a86843e99e0dc09df8ef11fa8e5d17351ddb77ff2603e6282dd376f63e72
F ext/wasm/api/sqlite3-wasm.c ab8abf26708238a17840cad224bec1511efde6e5b182068f3abf079ba1d83ef6
F ext/wasm/batch-runner.html 23209ade7981acce7ecd79d6eff9f4c5a4e8b14ae867ac27cd89b230be640fa6
F ext/wasm/batch-runner.js a727cbbffe63fd17fb5a590dc679f0b13bd51880e8f84b461d7df246417689e8
F ext/wasm/batch-runner.js 2abd146d3e3a66128ac0a2cc39bfd01e9811c9511fa10ec927d6649795f1ee50
F ext/wasm/common/SqliteTestUtil.js 529161a624265ba84271a52db58da022649832fa1c71309fb1e02cc037327a2b
F ext/wasm/common/emscripten.css 3d253a6fdb8983a2ac983855bfbdd4b6fa1ff267c28d69513dd6ef1f289ada3f
F ext/wasm/common/testing.css 3a5143699c2b73a85b962271e1a9b3241b30d90e30d895e4f55665e648572962
F ext/wasm/common/whwasmutil.js f7282ef36c9625330d4e6e82d1beec6678cd101e95e7108cd85db587a788c145
F ext/wasm/demo-oo1.html 75646855b38405d82781246fd08c852a2b3bee05dd9f0fe10ab655a8cffb79aa
F ext/wasm/demo-oo1.js aad38cb90b6fa7fd4d1184e759b25056fb4ed45c4957c458896354281259515f
F ext/wasm/demo-oo1.js 477f230cce3455e701431436d892d8c6bfea2bdf1ddcdd32a273e2f4bb339801
F ext/wasm/fiddle/emscripten.css 3d253a6fdb8983a2ac983855bfbdd4b6fa1ff267c28d69513dd6ef1f289ada3f
F ext/wasm/fiddle/fiddle-worker.js bccf46045be8824752876f3eec01c223be0616ccac184bffd0024cfe7a3262b8
F ext/wasm/fiddle/fiddle.html 550c5aafce40bd218de9bf26192749f69f9b10bc379423ecd2e162bcef885c08
@ -2019,8 +2019,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 53b7572e441be4b4b29b4228d0f95b24457d7faaf6c0b568ed5c970a55e21ffb
R 8c8179df681ac6c18cab6f56d59f4476
P cdbf09fa1b0c93aeb3222a157de33a4688ae629c2b829ffff0f1f62364c5ae1c
R 9d278bdeb3b04c59e81ef94ad61886ea
U stephan
Z a75e401f8f11ec78560c63fda40a275f
Z 1e71f5bdda12caaeb1881e6b3d2fda00
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
cdbf09fa1b0c93aeb3222a157de33a4688ae629c2b829ffff0f1f62364c5ae1c
4e6ce329872eb733ba2f7f7879747c52761ae97790fd8ed169a25a79854cc3d9