Skip to content

Commit

Permalink
Merge pull request #135 from rhashimoto/int64
Browse files Browse the repository at this point in the history
Pass 64-bit arguments to javascript with Emscripten legalization.
  • Loading branch information
rhashimoto authored Dec 8, 2023
2 parents 60d065c + 4897e2d commit 83b377e
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 163 deletions.
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

0 comments on commit 83b377e

Please sign in to comment.