fluencenicize

This commit is contained in:
vms 2020-04-17 20:44:55 +03:00
parent a913ff21f0
commit eaa210ca74
3 changed files with 31 additions and 77 deletions

View File

@ -237,7 +237,9 @@ int sqlite3_initialize(void){
} }
if( rc==SQLITE_OK ){ if( rc==SQLITE_OK ){
sqlite3GlobalConfig.isPCacheInit = 1; sqlite3GlobalConfig.isPCacheInit = 1;
#if __sqlite_unmodified_upstream
rc = sqlite3OsInit(); rc = sqlite3OsInit();
#endif
} }
#ifdef SQLITE_ENABLE_DESERIALIZE #ifdef SQLITE_ENABLE_DESERIALIZE
if( rc==SQLITE_OK ){ if( rc==SQLITE_OK ){
@ -321,7 +323,9 @@ int sqlite3_shutdown(void){
void SQLITE_EXTRA_SHUTDOWN(void); void SQLITE_EXTRA_SHUTDOWN(void);
SQLITE_EXTRA_SHUTDOWN(); SQLITE_EXTRA_SHUTDOWN();
#endif #endif
#if __sqlite_unmodified_upstream
sqlite3_os_end(); sqlite3_os_end();
#endif
sqlite3_reset_auto_extension(); sqlite3_reset_auto_extension();
sqlite3GlobalConfig.isInit = 0; sqlite3GlobalConfig.isInit = 0;
} }

View File

@ -17,6 +17,10 @@
** sqlite3_deserialize(). ** sqlite3_deserialize().
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#if __sqlite_unmodified_upstream
#else
#include <stdlib.h>
#endif
#ifdef SQLITE_ENABLE_DESERIALIZE #ifdef SQLITE_ENABLE_DESERIALIZE
/* /*
@ -422,7 +426,15 @@ static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
** random data. ** random data.
*/ */
static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
#if __sqlite_unmodified_upstream
return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
#else
zBufOut = malloc(nByte);
for(int i = 0; i < nByte; ++i) {
zBufOut[i] = rand() % 256;
}
return SQLITE_OK;
#endif
} }
/* /*
@ -610,14 +622,23 @@ end_deserialize:
** Register the new VFS. ** Register the new VFS.
*/ */
int sqlite3MemdbInit(void){ int sqlite3MemdbInit(void){
#if __sqlite_unmodified_upstream
sqlite3_vfs *pLower = sqlite3_vfs_find(0); sqlite3_vfs *pLower = sqlite3_vfs_find(0);
int sz = pLower->szOsFile; int sz = pLower->szOsFile;
memdb_vfs.pAppData = pLower; memdb_vfs.pAppData = pLower;
#else
memdb_vfs.pAppData = (void *)0xFFFFFFFF;
int sz = sizeof(MemFile);
#endif
/* In all known configurations of SQLite, the size of a default /* In all known configurations of SQLite, the size of a default
** sqlite3_file is greater than the size of a memdb sqlite3_file. ** sqlite3_file is greater than the size of a memdb sqlite3_file.
** Should that ever change, remove the following NEVER() */ ** Should that ever change, remove the following NEVER() */
if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile); if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
memdb_vfs.szOsFile = sz; memdb_vfs.szOsFile = sz;
#if __sqlite_unmodified_upstream
return sqlite3_vfs_register(&memdb_vfs, 0); return sqlite3_vfs_register(&memdb_vfs, 0);
#else
return sqlite3_vfs_register(&memdb_vfs, 1);
#endif
} }
#endif /* SQLITE_ENABLE_DESERIALIZE */ #endif /* SQLITE_ENABLE_DESERIALIZE */

View File

@ -15,45 +15,11 @@ int init() {
int g_isInited = 0; int g_isInited = 0;
/** void* allocate(size_t size) {
* Stores one byte by a given address in the module memory.
*
* @param ptr a address where byte should be stored
* @param value a byte to be stored
*/
void store(char *ptr, unsigned char value) {
*ptr = value;
}
/**
* Returns one byte by a given address in the module memory.
*
* @param ptr a address at which the needed byte is located
* @return the byte at the given address
*/
unsigned char load(const unsigned char *ptr) {
return *ptr;
}
/**
* Allocates a memory region of a given size.
* Could be used by Wasm execution environments for byte array passing.
*
* @param size a size of allocated memory region
* @return a pointer to the allocated memory region
*/
void* allocate(size_t size) {
return malloc(size + 1); return malloc(size + 1);
} }
/** void deallocate(void *ptr, int size) {
* Frees a memory region.
* Could be used by Wasm execution environments for freeing previous memory allocated by `allocate` function.
*
* @param ptr a pointer to the previously allocated memory region
* @param size a size of the previously allocated memory region
*/
void deallocate(void *ptr, int size) {
free(ptr); free(ptr);
} }
@ -140,34 +106,19 @@ static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
return 0; return 0;
} }
/** const char *invoke(char *request, int request_size) {
* Executes given SQL request and returns result in as a pointer to the following structure:
* | result size (4 bytes, le)| result (size bytes) |.
*
* @param request a pointer to the supplied sql request
* @param request_size a size of the supplied sql request
* @return a pointer to the struct contains result_size and result
*/
const char *invoke(char *request, int request_size) {
if(g_isInited == 0) { if(g_isInited == 0) {
// TODO: check the return code // TODO: check the return code
init(); init();
const char successInitMessage[] = "Sqlite has been initialized";
#ifdef ENABLE_LOG
const char successInitMessage[] = "Sqlite has been initialized\n";
wasm_log(successInitMessage, sizeof(successInitMessage)); wasm_log(successInitMessage, sizeof(successInitMessage));
#endif
g_isInited = 1; g_isInited = 1;
} }
request[request_size] = '\n';
#ifdef ENABLE_LOG
wasm_log(request, request_size);
#endif
request[request_size] = 0; request[request_size] = 0;
wasm_log(request, request_size);
ShellText str; ShellText str;
initText(&str); initText(&str);
char *errorMessage = 0; char *errorMessage = 0;
@ -188,30 +139,8 @@ static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
} }
} }
deallocate(request, request_size + 1);
freeText(&str); freeText(&str);
return response; return response;
} }
// functions with prefix sqlite_ are needed to support Rust backends
// (https://github.com/rust-lang/rust/issues/63562)
const char *sqlite_invoke(char *request, int request_size) {
return invoke(request, request_size);
}
void* sqlite_allocate(size_t size) {
return allocate(size);
}
void sqlite_deallocate(void *ptr, int size) {
deallocate(ptr, size);
}
void sqlite_store(char *ptr, unsigned char byte) {
store(ptr, byte);
}
unsigned char sqlite_load(const unsigned char *ptr) {
return load(ptr);
}