mirror of
https://github.com/sqlite/sqlite.git
synced 2025-07-27 20:41:58 +03:00
Add EM_JS() impl for kvstorageRead().
FossilOrigin-Name: 06610314fcf644f323c2f7ae11d7f4349b195e66d0ebbee590438dd99d97eb96
This commit is contained in:
@ -193,8 +193,9 @@ 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_RUNTIME_METHODS=FS,wasmMemory
|
||||
emcc.jsflags += -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory,allocateUTF8OnStack
|
||||
# wasmMemory ==> required by our code for use with -sIMPORTED_MEMORY
|
||||
# allocateUTF8OnStack => for kvvp internals
|
||||
emcc.jsflags += -sUSE_CLOSURE_COMPILER=0
|
||||
emcc.jsflags += -sIMPORTED_MEMORY
|
||||
emcc.environment := -sENVIRONMENT=web
|
||||
|
@ -651,7 +651,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap(
|
||||
["sqlite3_vfs_find", "*", "string"],
|
||||
["sqlite3_vfs_register", "int", "*", "int"],
|
||||
["sqlite3_wasm_vfs_unlink", "int", "string"],
|
||||
["sqlite3_wasm__emjs_test", undefined, "int"]
|
||||
["sqlite3_wasm__emjs_test", "int", "int"]
|
||||
]/*capi.wasm.bindingSignatures*/;
|
||||
|
||||
if(false && capi.wasm.compileOptionUsed('SQLITE_ENABLE_NORMALIZE')){
|
||||
|
@ -604,26 +604,67 @@ EM_JS(int, kvstorageDelete,
|
||||
return 0;
|
||||
});
|
||||
|
||||
EM_JS(int, kvstorageRead,
|
||||
(const char *zClass, const char *zKey, char *zBuf, int nBuf),{
|
||||
const stack = stackSave();
|
||||
try {
|
||||
const zXKey = stackAlloc(_sqlite3_wasm__kvvfsMakeKey(0,0,0));
|
||||
_sqlite3_wasm__kvvfsMakeKey(zClass, zKey, zXKey);
|
||||
const jKey = UTF8ToString(zXKey);
|
||||
const storage = (115/*=='s'*/===getValue(zClass))
|
||||
? sessionStorage : localStorage;
|
||||
const jV = storage.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){
|
||||
setValue(zBuf, 0);
|
||||
return nV;
|
||||
}
|
||||
const zV = allocateUTF8OnStack(jV);
|
||||
if(nBuf > nV + 1) nBuf = nV + 1;
|
||||
HEAPU8.copyWithin(zBuf, zV, zV + nBuf - 1);
|
||||
setValue( zBuf + nBuf - 1, 0 );
|
||||
return nBuf - 1;
|
||||
}catch(e){
|
||||
console.error("kvstorageRead()",e);
|
||||
return -1;
|
||||
}finally{
|
||||
stackRestore(stack);
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
** This function exists for (1) WASM testing purposes and (2) as a
|
||||
** hook to get Emscripten to export several EM_JS()-generated
|
||||
** functions. It is not part of the public API and its signature
|
||||
** and semantics may change at any time.
|
||||
*/
|
||||
void sqlite3_wasm__emjs_test(int whichOp){
|
||||
int sqlite3_wasm__emjs_test(int whichOp){
|
||||
const char * zClass = "session";
|
||||
const char * zKey = "hello";
|
||||
int rc = 0;
|
||||
switch( whichOp ){
|
||||
case 1:
|
||||
kvstorageWrite(zClass, zKey, "world");
|
||||
break;
|
||||
case 2:
|
||||
case 2: {
|
||||
char buffer[128] = {0};
|
||||
char * zBuf = &buffer[0];
|
||||
rc = kvstorageRead(zClass, zKey, zBuf, (int)sizeof(buffer));
|
||||
printf("kvstorageRead()=%d %s\n", rc, zBuf);
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
kvstorageDelete(zClass, zKey);
|
||||
break;
|
||||
default:
|
||||
//kvstorageMakeKeyJS(0,0,0) /* force Emscripten to include this */;
|
||||
break;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
#endif /* ifdef __EMSCRIPTEN__ */
|
||||
|
Reference in New Issue
Block a user