Skip to content

Commit 8479cc3

Browse files
initial rust extension loading
1 parent 60d065c commit 8479cc3

File tree

15 files changed

+2651
-318
lines changed

15 files changed

+2651
-318
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# @journeyapps/wa-sqlite
2+
3+
## 0.0.2
4+
5+
### Patch Changes
6+
7+
- 1cce650: Fixed Typescript declaration files.
8+
9+
## 0.0.1
10+
11+
### Patch Changes
12+
13+
- 9bb4bb4: Initial loading of PowerSync Rust SQLite extension.

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM mcr.microsoft.com/devcontainers/base:bullseye
2+
3+
# Need Chrome:
4+
RUN sudo apt update
5+
RUN sudo apt install chromium clang default-jre -y
6+
7+
8+
COPY ./scripts/docker-setup.sh /tmp/setup.sh
9+
10+
RUN sudo /bin/bash /tmp/setup.sh

Makefile

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ ASYNCIFY_IMPORTS = src/asyncify_imports.json
2929
OBJ_FILES_DEBUG = $(patsubst %.c,tmp/obj/debug/%.o,$(CFILES))
3030
OBJ_FILES_DIST = $(patsubst %.c,tmp/obj/dist/%.o,$(CFILES))
3131

32+
RS_LIB = powersync
33+
RS_LIB_DIR = ./powersync-sqlite-core
34+
RS_WASM_TGT = wasm32-unknown-emscripten
35+
RS_WASM_TGT_DIR = ${RS_LIB_DIR}/target/$(RS_WASM_TGT)
36+
RS_RELEASE_BC = $(RS_WASM_TGT_DIR)/wasm/deps/$(RS_LIB).bc
37+
RS_DEBUG_BC = $(RS_WASM_TGT_DIR)/debug/deps/$(RS_LIB).bc
38+
3239
# build options
3340
EMCC ?= emcc
3441

@@ -66,6 +73,7 @@ EMFLAGS_LIBRARIES = \
6673
--js-library src/libauthorizer.js \
6774
--js-library src/libfunction.js \
6875
--js-library src/libmodule.js \
76+
--js-library src/libtableupdates.js \
6977
--js-library src/libprogress.js \
7078
--js-library src/libvfs.js
7179

@@ -150,6 +158,16 @@ tmp/obj/dist/%.o: %.c
150158
mkdir -p tmp/obj/dist
151159
$(EMCC) $(CFLAGS_DIST) $(WASQLITE_DEFINES) $^ -c -o $@
152160

161+
$(RS_DEBUG_BC): FORCE
162+
mkdir -p tmp/bc/dist
163+
cd $(RS_LIB_DIR); \
164+
RUSTFLAGS="--emit=llvm-bc -C linker=/bin/true" cargo build -p powersync_loadable --profile wasm --no-default-features --features "powersync_core/static powersync_core/omit_load_extension sqlite_nostd/static sqlite_nostd/omit_load_extension" -Z build-std=panic_abort,core,alloc --target $(RS_WASM_TGT)
165+
166+
$(RS_RELEASE_BC): FORCE
167+
mkdir -p tmp/bc/dist
168+
cd $(RS_LIB_DIR); \
169+
RUSTFLAGS="--emit=llvm-bc -C linker=/bin/true" cargo build -p powersync_loadable --profile wasm --no-default-features --features "powersync_core/static powersync_core/omit_load_extension sqlite_nostd/static sqlite_nostd/omit_load_extension" -Z build-std=panic_abort,core,alloc --target $(RS_WASM_TGT)
170+
153171

154172
## debug
155173
.PHONY: clean-debug
@@ -159,20 +177,22 @@ clean-debug:
159177
.PHONY: debug
160178
debug: debug/wa-sqlite.mjs debug/wa-sqlite-async.mjs
161179

162-
debug/wa-sqlite.mjs: $(OBJ_FILES_DEBUG) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS)
180+
debug/wa-sqlite.mjs: $(OBJ_FILES_DEBUG) $(RS_DEBUG_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS)
163181
mkdir -p debug
164182
$(EMCC) $(EMFLAGS_DEBUG) \
165183
$(EMFLAGS_INTERFACES) \
166184
$(EMFLAGS_LIBRARIES) \
167-
$(OBJ_FILES_DEBUG) -o $@
185+
$(RS_WASM_TGT_DIR)/debug/deps/*.bc \
186+
$(OBJ_FILES_DEBUG) *.o -o $@
168187

169-
debug/wa-sqlite-async.mjs: $(OBJ_FILES_DEBUG) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS)
188+
debug/wa-sqlite-async.mjs: $(OBJ_FILES_DEBUG) $(RS_DEBUG_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS)
170189
mkdir -p debug
171190
$(EMCC) $(EMFLAGS_DEBUG) \
172191
$(EMFLAGS_INTERFACES) \
173192
$(EMFLAGS_LIBRARIES) \
174193
$(EMFLAGS_ASYNCIFY_DEBUG) \
175-
$(OBJ_FILES_DEBUG) -o $@
194+
$(RS_WASM_TGT_DIR)/debug/deps/*.bc \
195+
$(OBJ_FILES_DEBUG) *.o -o $@
176196

177197
## dist
178198
.PHONY: clean-dist
@@ -182,17 +202,22 @@ clean-dist:
182202
.PHONY: dist
183203
dist: dist/wa-sqlite.mjs dist/wa-sqlite-async.mjs
184204

185-
dist/wa-sqlite.mjs: $(OBJ_FILES_DIST) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS)
205+
dist/wa-sqlite.mjs: $(OBJ_FILES_DIST) $(RS_RELEASE_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS)
186206
mkdir -p dist
187207
$(EMCC) $(EMFLAGS_DIST) \
188208
$(EMFLAGS_INTERFACES) \
189209
$(EMFLAGS_LIBRARIES) \
190-
$(OBJ_FILES_DIST) -o $@
210+
$(RS_WASM_TGT_DIR)/wasm/deps/*.bc \
211+
$(OBJ_FILES_DIST) -o $@
191212

192-
dist/wa-sqlite-async.mjs: $(OBJ_FILES_DIST) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS)
213+
dist/wa-sqlite-async.mjs: $(OBJ_FILES_DIST) $(RS_RELEASE_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS)
193214
mkdir -p dist
194215
$(EMCC) $(EMFLAGS_DIST) \
195216
$(EMFLAGS_INTERFACES) \
196217
$(EMFLAGS_LIBRARIES) \
197218
$(EMFLAGS_ASYNCIFY_DIST) \
198-
$(OBJ_FILES_DIST) -o $@
219+
$(CFLAGS_DIST) \
220+
$(RS_WASM_TGT_DIR)/wasm/deps/*.bc \
221+
$(OBJ_FILES_DIST) -o $@
222+
223+
FORCE: ;

README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ The primary motivation for this project is to enable additions to SQLite with on
1313
Note that earlier versions of the project only provided pre-built artifacts in the
1414
"buildless" branch; that branch will no longer be maintained.
1515

16-
Minor build customization (e.g. changing build defines or flags) can be done with [make arguments](https://github.com/rhashimoto/wa-sqlite/discussions/128), and the helper project [sqwab](https://github.com/rhashimoto/sqwab) can be used to build without a local build environment.
17-
18-
If you do want to build yourself, here are the prerequisites:
16+
If you do want to build - e.g. you want to change build flags, use a specific EMSDK version, or modify wa-sqlite itself - here are the prerequisites:
1917

2018
* Building on Debian Linux is known to work, compatibility with other platforms is unknown.
2119
* `yarn` - If you use a different package manager (e.g. `npm`) then file paths in the demo will need adjustment.
2220
* [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html) 3.1.25+.
23-
* `curl`, `make`, `openssl`, `sed`, `tclsh`, `unzip`
21+
* `curl`, `make`, `openssl`, `sed`, `unzip`
2422

2523
Here are the build steps:
2624
* Make sure `emcc` works.
@@ -31,6 +29,38 @@ Here are the build steps:
3129

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

32+
## JouneyApps instructions
33+
34+
Note as per above that this is known to compile under Debian.
35+
36+
MacOS initially complained about OpenSSL config. Compiling on MacOS may be possible with additional config, but this is currently unknown.
37+
38+
Development has been done using VSCode's [development container](https://code.visualstudio.com/docs/devcontainers/containers) feature.
39+
40+
```bash
41+
git clone [this repo]
42+
```
43+
44+
45+
```bash
46+
git submodule init
47+
```
48+
49+
```bash
50+
git submodule update --recursive
51+
```
52+
53+
```bash
54+
yarn install
55+
56+
```
57+
58+
59+
```bash
60+
make -B
61+
```
62+
63+
3464
## API
3565
Javascript wrappers for core SQLITE C API functions (and some others) are provided. Some convenience functions are also provided to reduce boilerplate. Here's sample code to load the library and call the API:
3666

@@ -76,4 +106,4 @@ For convenience, if any text region is selected in the editor, only that region
76106
## License
77107
MIT License as of February 10, 2023, changed by generous sponsors
78108
[Fleet Device Management](https://fleetdm.com/) and [Reflect](https://reflect.app/).
79-
Existing licensees may continue under the GPLv3 or switch to the new license.
109+
Existing licensees may continue under the GPLv3 or switch to the new license.

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{
2-
"name": "wa-sqlite",
3-
"version": "0.9.9",
2+
"name": "@journeyapps/wa-sqlite",
3+
"version": "0.0.2",
4+
"publishConfig": {
5+
"access": "public"
6+
},
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/powersync-ja/wa-sqlite.git"
10+
},
411
"type": "module",
512
"main": "src/sqlite-api.js",
613
"types": "src/types/index.d.ts",
@@ -15,12 +22,13 @@
1522
],
1623
"scripts": {
1724
"build-docs": "typedoc",
18-
"prepack": "make",
25+
"release": "yarn changeset publish",
1926
"start": "web-dev-server --node-resolve",
2027
"test": "karma start karma.conf.cjs --browsers ChromeHeadless",
2128
"test-manual": "karma start karma.conf.cjs --auto-watch --no-single-run --reporters progress"
2229
},
2330
"devDependencies": {
31+
"@changesets/cli": "^2.26.2",
2432
"@web/dev-server": "^0.1.13",
2533
"comlink": "^4.4.1",
2634
"jasmine-core": "^4.5.0",

powersync-sqlite-core

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 53223d621dbd77789eb1ac162a4c0abd4fdb4043

scripts/docker-setup.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
2+
source ~/.bashrc
3+
rustup component add rust-src --toolchain nightly-2023-08-28-aarch64-unknown-linux-gnu -y
4+
5+
6+
# Need NVM:
7+
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
8+
source ~/.bashrc
9+
nvm install 18.12.0 && nvm use 18.12.0
10+
npm install -g yarn

src/exported_functions.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,8 @@
5353
"_sqlite3_value_int64",
5454
"_sqlite3_value_text",
5555
"_sqlite3_value_type",
56-
"_sqlite3_vfs_find"
56+
"_sqlite3_vfs_find",
57+
"_sqlite3_last_insert_rowid",
58+
"_setup_powersync",
59+
"_register_table_update_hook"
5760
]

src/libmodule.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ extern int modRollback(sqlite3_vtab *pVTab);
3232
// void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
3333
// void **ppArg);
3434
extern int modRename(sqlite3_vtab *pVtab, const char *zNew);
35+
extern void onTableChangeCallback(sqlite3*, int, const char *, int);
36+
37+
EMSCRIPTEN_KEEPALIVE
38+
void on_tables_changed(void *db, int opType, char const *dbName,
39+
char const *tableName, sqlite3_int64 rowId) {
40+
onTableChangeCallback((sqlite3 *) db, opType, tableName, rowId);
41+
}
42+
43+
void register_table_update_hook(sqlite3 *db) {
44+
sqlite3_update_hook(db, on_tables_changed,
45+
(void *)(db));
46+
}
3547

3648
static int xCreate(
3749
sqlite3* db,
@@ -65,6 +77,7 @@ static int xConnect(
6577
return result;
6678
}
6779

80+
6881
static int xOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor) {
6982
*ppCursor = (sqlite3_vtab_cursor*)sqlite3_malloc(sizeof(sqlite3_vtab_cursor));
7083
return modOpen(pVTab, *ppCursor);

src/libtableupdates.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mergeInto(LibraryManager.library, {
2+
onTableChangeCallback: function(db, optType, tableName, rowId) {
3+
// This is exposed globally since exporting from this module caused WASM compilation errors
4+
const fn = globalThis['__onTablesChanged'];
5+
fn?.(db, optType, tableName, rowId);
6+
}
7+
});

0 commit comments

Comments
 (0)