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

Pass 64-bit arguments to javascript with Emscripten legalization. #135

Merged
merged 2 commits into from
Dec 8, 2023
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ EMFLAGS_COMMON = \

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

EMFLAGS_DIST = \
Expand Down
114 changes: 57 additions & 57 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.
154 changes: 77 additions & 77 deletions dist/wa-sqlite.mjs

Large diffs are not rendered by default.

Binary file modified dist/wa-sqlite.wasm
Binary file not shown.
23 changes: 6 additions & 17 deletions src/libvfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
// 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, const sqlite3_int64* iOffset);
extern int vfsWrite(sqlite3_file* file, const void* pData, int iAmt, const sqlite3_int64* iOffset);
extern int vfsTruncate(sqlite3_file* file, const sqlite3_int64* size);
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);
Expand All @@ -23,24 +23,13 @@ extern int vfsOpen(sqlite3_vfs* vfs, const char *zName, sqlite3_file* file, int
extern int vfsDelete(sqlite3_vfs* vfs, const char *zName, int syncDir);
extern int vfsAccess(sqlite3_vfs* vfs, const char *zName, int flags, int *pResOut);

// Glue functions to pass 64-bit integers by pointer.
static int xRead(sqlite3_file* file, void* pData, int iAmt, sqlite3_int64 iOffset) {
return vfsRead(file, pData, iAmt, &iOffset);
}
static int xWrite(sqlite3_file* file, const void* pData, int iAmt, sqlite3_int64 iOffset) {
return vfsWrite(file, pData, iAmt, &iOffset);
}
static int xTruncate(sqlite3_file* file, sqlite3_int64 size) {
return vfsTruncate(file, &size);
}

static int xOpen(sqlite3_vfs* vfs, const char* zName, sqlite3_file* file, int flags, int* pOutFlags) {
static sqlite3_io_methods io_methods = {
1,
vfsClose,
xRead,
xWrite,
xTruncate,
vfsRead,
vfsWrite,
vfsTruncate,
vfsSync,
vfsFileSize,
vfsLock,
Expand Down
21 changes: 10 additions & 11 deletions src/libvfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ const vfs_methods = {
});
}

// Convert 64-bit unsigned int in WASM memory to Number. The unsigned
// int is assumed to be <= Number.MAX_SAFE_INTEGER;
function u64(ptr) {
const index = ptr >> 2;
return HEAPU32[index] + (HEAPU32[index + 1] * (2**32));
// Emscripten "legalizes" 64-bit integer arguments by passing them as
// two 32-bit signed integers.
function delegalize(lo32, hi32) {
return (hi32 * 0x100000000) + lo32 + (lo32 < 0 ? 2**32 : 0);
}

const closedFiles = hasAsyncify ? new Set() : null;
Expand All @@ -92,23 +91,23 @@ const vfs_methods = {
}

// int xRead(sqlite3_file* file, void* pData, int iAmt, sqlite3_int64 iOffset);
_vfsRead = function(file, pData, iAmt, iOffset) {
_vfsRead = function(file, pData, iAmt, iOffsetLo, iOffsetHi) {
const vfs = mapFileToVFS.get(file);
const pDataArray = HEAPU8.subarray(pData, pData + iAmt);
return vfs['xRead'](file, pDataArray, u64(iOffset));
return vfs['xRead'](file, pDataArray, delegalize(iOffsetLo, iOffsetHi));
}

// int xWrite(sqlite3_file* file, const void* pData, int iAmt, sqlite3_int64 iOffset);
_vfsWrite = function(file, pData, iAmt, iOffset) {
_vfsWrite = function(file, pData, iAmt, iOffsetLo, iOffsetHi) {
const vfs = mapFileToVFS.get(file);
const pDataArray = HEAPU8.subarray(pData, pData + iAmt);
return vfs['xWrite'](file, pDataArray, u64(iOffset));
return vfs['xWrite'](file, pDataArray, delegalize(iOffsetLo, iOffsetHi));
}

// int xTruncate(sqlite3_file* file, sqlite3_int64 size);
_vfsTruncate = function(file, iSize) {
_vfsTruncate = function(file, iSizeLo, iSizeHi) {
const vfs = mapFileToVFS.get(file);
return vfs['xTruncate'](file, u64(iSize));
return vfs['xTruncate'](file, delegalize(iSizeLo, iSizeHi));
}

// int xSync(sqlite3_file* file, int flags);
Expand Down
Loading