Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update wa-sqlite #6

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 61 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ CFILES = \
libvfs.c \
$(CFILES_EXTRA)

JSFILES = \
src/libauthorizer.js \
src/libfunction.js \
src/libmodule.js \
src/libprogress.js \
src/libvfs.js

vpath %.c src
vpath %.c deps
vpath %.c deps/$(SQLITE_VERSION)
Expand Down Expand Up @@ -56,7 +63,7 @@ EMFLAGS_COMMON = \

EMFLAGS_DEBUG = \
-s ASSERTIONS=1 \
-g -Oz \
-g -Oz -Oz \
$(EMFLAGS_COMMON)

EMFLAGS_DIST = \
Expand Down Expand Up @@ -104,6 +111,7 @@ WASQLITE_DEFINES = \
-DSQLITE_THREADSAFE=0 \
-DSQLITE_USE_ALLOCA \
-DSQLITE_ENABLE_BATCH_ATOMIC_WRITE \
-DSQLITE_ENABLE_FTS5 \
$(WASQLITE_EXTRA_DEFINES)

# directories
Expand All @@ -112,11 +120,11 @@ all: dist

.PHONY: clean
clean:
rm -rf dist dist-xl debug tmp
rm -rf dist debug tmp

.PHONY: spotless
spotless:
rm -rf dist dist-xl debug tmp deps cache
rm -rf dist debug tmp deps cache

## cache
.PHONY: clean-cache
Expand Down Expand Up @@ -194,6 +202,27 @@ debug/wa-sqlite-async.mjs: $(OBJ_FILES_DEBUG) $(RS_DEBUG_BC) $(EXPORTED_FUNCTION
$(RS_WASM_TGT_DIR)/debug/deps/*.bc \
$(OBJ_FILES_DEBUG) *.o -o $@

## Debug FTS builds
# .PHONY: debug
# debug: debug/wa-sqlite.mjs debug/wa-sqlite-async.mjs

# debug/wa-sqlite.mjs: $(OBJ_FILES_DEBUG_FTS) $(RS_DEBUG_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS)
# mkdir -p debug
# $(EMCC) $(EMFLAGS_DEBUG) \
# $(EMFLAGS_INTERFACES) \
# $(EMFLAGS_LIBRARIES) \
# $(RS_WASM_TGT_DIR)/debug/deps/*.bc \
# $(OBJ_FILES_DEBUG_FTS) *.o -o $@

# debug/wa-sqlite-async.mjs: $(OBJ_FILES_DEBUG_FTS) $(RS_DEBUG_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS)
# mkdir -p debug
# $(EMCC) $(EMFLAGS_DEBUG) \
# $(EMFLAGS_INTERFACES) \
# $(EMFLAGS_LIBRARIES) \
# $(EMFLAGS_ASYNCIFY_DEBUG) \
# $(RS_WASM_TGT_DIR)/debug/deps/*.bc \
# $(OBJ_FILES_DEBUG_FTS) *.o -o $@

## dist
.PHONY: clean-dist
clean-dist:
Expand All @@ -220,4 +249,32 @@ dist/wa-sqlite-async.mjs: $(OBJ_FILES_DIST) $(RS_RELEASE_BC) $(EXPORTED_FUNCTION
$(RS_WASM_TGT_DIR)/wasm/deps/*.bc \
$(OBJ_FILES_DIST) -o $@

FORCE: ;
FORCE: ;

# FTS builds
# .PHONY: clean-dist
# clean-dist:
# rm -rf dist

# .PHONY: dist
# dist: dist/wa-sqlite.mjs dist/wa-sqlite-async.mjs

# dist/wa-sqlite.mjs: $(OBJ_FILES_DIST_FTS) $(RS_RELEASE_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS)
# mkdir -p dist
# $(EMCC) $(EMFLAGS_DIST) \
# $(EMFLAGS_INTERFACES) \
# $(EMFLAGS_LIBRARIES) \
# $(RS_WASM_TGT_DIR)/wasm/deps/*.bc \
# $(OBJ_FILES_DIST_FTS) -o $@

# dist/wa-sqlite-async.mjs: $(OBJ_FILES_DIST_FTS) $(RS_RELEASE_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS)
# mkdir -p dist
# $(EMCC) $(EMFLAGS_DIST) \
# $(EMFLAGS_INTERFACES) \
# $(EMFLAGS_LIBRARIES) \
# $(EMFLAGS_ASYNCIFY_DIST) \
# $(CFLAGS_DIST) \
# $(RS_WASM_TGT_DIR)/wasm/deps/*.bc \
# $(OBJ_FILES_DIST_FTS) -o $@

# FORCE: ;
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Here are the build steps:
* `git clone [email protected]:rhashimoto/wa-sqlite.git`
* `cd wa-sqlite`
* `yarn install`
* `make` (or `yarn prepack`)
* `make`

The default build produces ES6 modules + WASM, [synchronous and asynchronous](https://github.com/rhashimoto/wa-sqlite/issues/7) (using Asyncify) in `dist/`.

Expand All @@ -51,7 +51,7 @@ git submodule init
```

```bash
git submodule update --recursive
git submodule update --init --recursive
```

```bash
Expand Down
268 changes: 132 additions & 136 deletions dist/wa-sqlite-async.mjs

Large diffs are not rendered by default.

Binary file modified dist/wa-sqlite-async.wasm
Binary file not shown.
261 changes: 129 additions & 132 deletions dist/wa-sqlite.mjs

Large diffs are not rendered by default.

Binary file modified dist/wa-sqlite.wasm
Binary file not shown.
7 changes: 4 additions & 3 deletions scripts/docker-setup.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source ~/.bashrc
rustup component add rust-src --toolchain nightly-2023-08-28-aarch64-unknown-linux-gnu -y
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
. $HOME/.cargo/env && \
rustup toolchain install nightly-2023-08-28-aarch64-unknown-linux-gnu && \
rustup component add rust-src --toolchain nightly-2023-08-28-aarch64-unknown-linux-gnu

sudo apt-get update -y
sudo apt-get install -y tclsh
Expand Down
104 changes: 56 additions & 48 deletions src/libvfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,75 @@

// sqlite3_io_methods javascript handlers
// 64-bit integer parameters are passed by pointer.
extern int vfsClose(sqlite3_file* file);
extern int vfsRead(sqlite3_file* file, void* pData, int iAmt, sqlite3_int64 iOffset);
extern int vfsWrite(sqlite3_file* file, const void* pData, int iAmt, sqlite3_int64 iOffset);
extern int vfsTruncate(sqlite3_file* file, sqlite3_int64 size);
extern int vfsSync(sqlite3_file* file, int flags);
extern int vfsFileSize(sqlite3_file* file, sqlite3_int64* pSize);
extern int vfsLock(sqlite3_file* file, int flags);
extern int vfsUnlock(sqlite3_file* file, int flags);
extern int vfsCheckReservedLock(sqlite3_file* file, int* pResOut);
extern int vfsFileControl(sqlite3_file* file, int flags, void* pOut);
extern int vfsSectorSize(sqlite3_file* file);
extern int vfsDeviceCharacteristics(sqlite3_file* file);
extern int vfsClose(sqlite3_file *file);
extern int vfsRead(sqlite3_file *file, void *pData, int iAmt, sqlite3_int64 iOffset);
extern int vfsWrite(sqlite3_file *file, const void *pData, int iAmt, sqlite3_int64 iOffset);
extern int vfsTruncate(sqlite3_file *file, sqlite3_int64 size);
extern int vfsSync(sqlite3_file *file, int flags);
extern int vfsFileSize(sqlite3_file *file, sqlite3_int64 *pSize);
extern int vfsLock(sqlite3_file *file, int flags);
extern int vfsUnlock(sqlite3_file *file, int flags);
extern int vfsCheckReservedLock(sqlite3_file *file, int *pResOut);
extern int vfsFileControl(sqlite3_file *file, int flags, void *pOut);
extern int vfsSectorSize(sqlite3_file *file);
extern int vfsDeviceCharacteristics(sqlite3_file *file);

extern int vfsOpen(sqlite3_vfs* vfs, const char *zName, sqlite3_file* file, int flags, int *pOutFlags);
extern int vfsDelete(sqlite3_vfs* vfs, const char *zName, int syncDir);
extern int vfsAccess(sqlite3_vfs* vfs, const char *zName, int flags, int *pResOut);
extern int vfsOpen(sqlite3_vfs *vfs, const char *zName, sqlite3_file *file, int flags, int *pOutFlags);
extern int vfsDelete(sqlite3_vfs *vfs, const char *zName, int syncDir);
extern int vfsAccess(sqlite3_vfs *vfs, const char *zName, int flags, int *pResOut);

// This is undefined in the WASM linker step if not specified
extern int __rust_no_alloc_shim_is_unstable = 0;
extern int sqlite3_powersync_init(sqlite3 *db, char **pzErrMsg,
const sqlite3_api_routines *pApi);
const sqlite3_api_routines *pApi);

static int xOpen(sqlite3_vfs* vfs, const char* zName, sqlite3_file* file, int flags, int* pOutFlags) {
static int xOpen(sqlite3_vfs *vfs, const char *zName, sqlite3_file *file, int flags, int *pOutFlags)
{
static sqlite3_io_methods io_methods = {
1,
vfsClose,
vfsRead,
vfsWrite,
vfsTruncate,
vfsSync,
vfsFileSize,
vfsLock,
vfsUnlock,
vfsCheckReservedLock,
vfsFileControl,
vfsSectorSize,
vfsDeviceCharacteristics
};
1,
vfsClose,
vfsRead,
vfsWrite,
vfsTruncate,
vfsSync,
vfsFileSize,
vfsLock,
vfsUnlock,
vfsCheckReservedLock,
vfsFileControl,
vfsSectorSize,
vfsDeviceCharacteristics};
file->pMethods = &io_methods;

return vfsOpen(vfs, zName, file, flags, pOutFlags);
}

static int xFullPathname(sqlite3_vfs* vfs, const char* zName, int nOut, char* zOut) {
static int xFullPathname(sqlite3_vfs *vfs, const char *zName, int nOut, char *zOut)
{
strncpy(zOut, zName, nOut);
return SQLITE_OK;
}

static int xCurrentTime(sqlite3_vfs* vfs, double* pJulianDay) {
static int xCurrentTime(sqlite3_vfs *vfs, double *pJulianDay)
{
// UNIX epoch 1/1/1970 is Julian day 2440587.5
static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
static const sqlite3_int64 unixEpoch = 24405875 * (sqlite3_int64)8640000;
struct timeval sNow;
gettimeofday(&sNow, 0);
sqlite3_int64 julianMillis = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
sqlite3_int64 julianMillis = unixEpoch + 1000 * (sqlite3_int64)sNow.tv_sec + sNow.tv_usec / 1000;
*pJulianDay = julianMillis / 86400000.0;
return SQLITE_OK;
}

const int EMSCRIPTEN_KEEPALIVE register_vfs(
const char* zName,
int mxPathName,
int makeDefault,
sqlite3_vfs** ppVFS) {
sqlite3_vfs* vfs = *ppVFS = (sqlite3_vfs*)sqlite3_malloc(sizeof(sqlite3_vfs));
if (!vfs) {
const char *zName,
int mxPathName,
int makeDefault,
sqlite3_vfs **ppVFS)
{
sqlite3_vfs *vfs = *ppVFS = (sqlite3_vfs *)sqlite3_malloc(sizeof(sqlite3_vfs));
if (!vfs)
{
return SQLITE_NOMEM;
}

Expand All @@ -85,9 +89,9 @@ const int EMSCRIPTEN_KEEPALIVE register_vfs(
vfs->xAccess = vfsAccess;
vfs->xFullPathname = xFullPathname;
vfs->xCurrentTime = xCurrentTime;

// Get remaining functionality from the default VFS.
sqlite3_vfs* defer = sqlite3_vfs_find(0);
sqlite3_vfs *defer = sqlite3_vfs_find(0);
#define COPY_FIELD(NAME) vfs->NAME = defer->NAME
COPY_FIELD(xDlOpen);
COPY_FIELD(xDlError);
Expand All @@ -99,22 +103,26 @@ const int EMSCRIPTEN_KEEPALIVE register_vfs(
#undef COPY_FIELD

const int result = sqlite3_vfs_register(vfs, makeDefault);
if (result != SQLITE_OK) {
if (result != SQLITE_OK)
{
*ppVFS = 0;
sqlite3_free(vfs);
}
return result;
}

void* EMSCRIPTEN_KEEPALIVE getSqliteFree() {
void *EMSCRIPTEN_KEEPALIVE getSqliteFree()
{
return sqlite3_free;
}

int main() {
int main()
{
sqlite3_initialize();
return 0;
}

int setup_powersync() {
return sqlite3_auto_extension((void (*)(void)) &sqlite3_powersync_init);
int setup_powersync()
{
return sqlite3_auto_extension((void (*)(void)) & sqlite3_powersync_init);
}
2 changes: 1 addition & 1 deletion src/libvfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const vfs_methods = {
vfs['handleAsync'] = Asyncify.handleAsync;
}

const mxPathName = vfs.mxPathName ?? 64;
const mxPathName = vfs['mxPathName'] ?? 64;
const out = Module['_malloc'](4);
const result = ccall('register_vfs', 'number', ['string', 'number', 'number', 'number'],
[vfs.name, mxPathName, makeDefault ? 1 : 0, out]);
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1692,4 +1692,4 @@ declare module 'wa-sqlite/src/examples/tag.js' {
* @returns {function(TemplateStringsArray, ...any): Promise<object[]>}
*/
export function tag(sqlite3: any, db: number): (arg0: TemplateStringsArray, ...args: any[]) => Promise<object[]>;
}
}
Loading