1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

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);