Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Commit 51cfc36

Browse files
authored
WebAssembly API implementation (#4815)
1 parent a575aec commit 51cfc36

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+7095
-30
lines changed

.github/workflows/ci-workflow.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,31 @@ jobs:
287287
with:
288288
name: binary-demo
289289
path: dataset/binary-demo
290+
291+
- name: Install dependencies
292+
working-directory: tools/wasm
293+
run: npm i
290294

291-
- name: Build & test
295+
- name: Build WebAssembly package
296+
working-directory: tools/wasm
297+
run: |
298+
source /home/runner/emsdk/emsdk_env.sh
299+
npm run build
300+
301+
- name: API test
302+
working-directory: tools/wasm
303+
run: npm run test
304+
305+
- name: Clean up
306+
run: make clean
307+
308+
- name: Kernel build & test
292309
run: |
293310
ln -s /home/runner/ldbc-1-csv dataset/ldbc-1/csv
294311
source /home/runner/emsdk/emsdk_env.sh
295312
make wasmtest
296313
297-
- name: Build & test (single-thread mode)
314+
- name: Kernel build & test (single-threaded)
298315
env:
299316
SINGLE_THREADED: true
300317
run: |

.github/workflows/gen-docs.yml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,33 @@ jobs:
7878
with:
7979
name: kuzu-nodejs-docs
8080
path: ./docs
81+
82+
generate-wasm-docs:
83+
runs-on: macos-latest
84+
steps:
85+
- uses: actions/checkout@v4
86+
87+
- name: Setup Node.js
88+
uses: actions/setup-node@v4
89+
with:
90+
node-version: "20"
91+
92+
- name: Install Node.js packages
93+
run: |
94+
npm install -g jsdoc
95+
96+
- name: Generate Node.js docs
97+
shell: bash
98+
run: |
99+
mkdir docs
100+
jsdoc ./tools/wasm/src_js/sync/*.js ./tools/wasm/src_js/sync/README.md --destination ./docs/sync
101+
jsdoc ./tools/wasm/src_js/*.js ./tools/wasm/src_js/README.md --destination ./docs/async
102+
103+
- name: Upload Node.js docs
104+
uses: actions/upload-artifact@v4
105+
with:
106+
name: kuzu-wasm-docs
107+
path: ./docs
81108

82109
generate-java-docs:
83110
runs-on: ubuntu-latest
@@ -127,7 +154,13 @@ jobs:
127154

128155
update-docs:
129156
runs-on: ubuntu-22.04
130-
needs: [generate-python-docs, generate-nodejs-docs, generate-java-docs, generate-cpp-docs]
157+
needs: [
158+
generate-python-docs,
159+
generate-nodejs-docs,
160+
generate-wasm-docs,
161+
generate-java-docs,
162+
generate-cpp-docs
163+
]
131164
steps:
132165
- uses: actions/checkout@v4
133166
with:
@@ -149,6 +182,12 @@ jobs:
149182
with:
150183
name: kuzu-nodejs-docs
151184
path: kuzudb.github.io/api-docs/nodejs
185+
186+
- name: Download WASM docs
187+
uses: actions/download-artifact@v4
188+
with:
189+
name: kuzu-wasm-docs
190+
path: kuzudb.github.io/api-docs/wasm
152191

153192
- name: Download Java docs
154193
uses: actions/download-artifact@v4
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Build WebAssembly module
2+
3+
on:
4+
workflow_dispatch:
5+
workflow_call:
6+
inputs:
7+
isNightly:
8+
type: boolean
9+
required: true
10+
default: false
11+
12+
jobs:
13+
build-wasm:
14+
runs-on: kuzu-self-hosted-testing
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Update nightly version
19+
if: ${{ inputs.isNightly == true }}
20+
env:
21+
PIP_BREAK_SYSTEM_PACKAGES: 1
22+
run: |
23+
python3 -m pip install packaging
24+
python3 update-nightly-build-version.py
25+
working-directory: scripts
26+
27+
- name: Build WebAssembly package
28+
working-directory: tools/wasm
29+
run: |
30+
source /home/runner/emsdk/emsdk_env.sh
31+
npm run build
32+
33+
- uses: actions/upload-artifact@v4
34+
with:
35+
name: kuzu-deploy-wasm
36+
path: tools/wasm/kuzu-wasm.tar.gz

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ tools/nodejs_api/cmake_install.cmake
4646
tools/nodejs_api/package-lock.json
4747
tools/nodejs_api/prebuilt/
4848

49+
### WASM
50+
tools/wasm/node_modules/
51+
tools/wasm/examples/browser/node_modules/
52+
tools/wasm/examples/browser/public/index.js
53+
tools/wasm/examples/browser/public/kuzu_wasm_worker.js
54+
tools/wasm/examples/nodejs/node_modules/
55+
tools/wasm/package/
56+
4957
# Archive files
5058
*.zip
5159
*.tar.gz

CMakeLists.txt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,35 @@ else()
158158
add_compile_options(-Wno-unknown-pragmas)
159159
endif()
160160

161-
if(BUILD_WASM)
161+
if(${BUILD_WASM})
162162
if(NOT __SINGLE_THREADED__)
163163
add_compile_options(-pthread)
164164
add_link_options(-pthread)
165165
add_link_options(-sPTHREAD_POOL_SIZE=8)
166166
endif()
167167
add_compile_options(-s DISABLE_EXCEPTION_CATCHING=0)
168168
add_link_options(-sSTACK_SIZE=4MB)
169+
add_link_options(-sASSERTIONS=1)
170+
add_link_options(-lembind)
171+
add_link_options(-sWASM_BIGINT)
172+
169173
if(BUILD_TESTS OR BUILD_EXTENSION_TESTS)
170-
add_link_options(-sINITIAL_MEMORY=3892MB) # 3.8GB
174+
add_link_options(-sINITIAL_MEMORY=3892MB)
175+
add_link_options(-sNODERAWFS=1)
176+
elseif(WASM_NODEFS)
171177
add_link_options(-sNODERAWFS=1)
172-
add_link_options(-sASSERTIONS=1)
178+
add_link_options(-sALLOW_MEMORY_GROWTH=1)
179+
add_link_options(-sMODULARIZE=1)
180+
add_link_options(-sEXPORTED_RUNTIME_METHODS=FS,wasmMemory)
181+
add_link_options(-sEXPORT_NAME=kuzu)
182+
add_link_options(-sMAXIMUM_MEMORY=4GB)
173183
else()
174-
add_link_options(-sINITIAL_MEMORY=2GB)
184+
add_link_options(-sSINGLE_FILE=1)
175185
add_link_options(-sALLOW_MEMORY_GROWTH=1)
186+
add_link_options(-sMODULARIZE=1)
187+
add_link_options(-sEXPORTED_RUNTIME_METHODS=FS,wasmMemory)
188+
add_link_options(-sEXPORT_NAME=kuzu)
189+
add_link_options(-sMAXIMUM_MEMORY=4GB)
176190
endif()
177191
set(__WASM__ TRUE)
178192
add_compile_options(-fexceptions)

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ ifdef SINGLE_THREADED
7373
CMAKE_FLAGS += -DSINGLE_THREADED=$(SINGLE_THREADED)
7474
endif
7575

76+
ifdef WASM_NODEFS
77+
CMAKE_FLAGS += -DWASM_NODEFS=$(WASM_NODEFS)
78+
endif
79+
7680
# Must be first in the Makefile so that it is the default target.
7781
release:
7882
$(call run-cmake-release,)
@@ -139,6 +143,11 @@ python:
139143
python-debug:
140144
$(call run-cmake-debug, -DBUILD_PYTHON=TRUE)
141145

146+
wasm:
147+
mkdir -p build/wasm && cd build/wasm &&\
148+
emcmake cmake $(CMAKE_FLAGS) -DCMAKE_BUILD_TYPE=Release -DBUILD_WASM=TRUE -DBUILD_BENCHMARK=FALSE -DBUILD_TESTS=FALSE -DBUILD_SHELL=FALSE ../.. && \
149+
cmake --build . --config Release -j $(NUM_THREADS)
150+
142151
# Language API tests
143152
javatest:
144153
ifeq ($(OS),Windows_NT)

src/main/database.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ SystemConfig::SystemConfig(uint64_t bufferPoolSize_, uint64_t maxNumThreads, boo
5959
#else
6060
// In single-threaded mode, even if the user specifies a number of threads,
6161
// it will be ignored and set to 0.
62-
this->maxNumThreads = 0;
62+
this->maxNumThreads = 1;
6363
#endif
6464
if (maxDBSize == -1u) {
6565
maxDBSize = BufferPoolConstants::DEFAULT_VM_REGION_MAX_SIZE;

tools/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ endif()
1313
if(${BUILD_BENCHMARK})
1414
add_subdirectory(benchmark)
1515
endif()
16+
if(${BUILD_WASM})
17+
add_subdirectory(wasm)
18+
endif()

tools/nodejs_api/src_js/database.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

33
const KuzuNative = require("./kuzu_native.js");
4-
const LoggingLevel = require("./logging_level.js");
54

65
class Database {
76
/**
@@ -38,7 +37,7 @@ class Database {
3837
throw new Error("Buffer manager size must be a positive integer.");
3938
}
4039
if (typeof maxDBSize !== "number" || maxDBSize < 0) {
41-
throw new Error("Max DB size must be a positive integer.");
40+
throw new Error("Max DB size must be a positive integer.");
4241
}
4342
if (typeof checkpointThreshold !== "number" || maxDBSize < -1) {
4443
throw new Error("Checkpoint threshold must be a positive integer.");
@@ -90,10 +89,6 @@ class Database {
9089
} else {
9190
try {
9291
this._isInitialized = true;
93-
if (this._loggingLevel) {
94-
this.setLoggingLevel(this._loggingLevel);
95-
delete this._loggingLevel;
96-
}
9792
} catch (e) {
9893
return reject(e);
9994
}

tools/nodejs_api/src_js/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
const Connection = require("./connection.js");
44
const Database = require("./database.js");
5-
const LoggingLevel = require("./logging_level.js");
65
const PreparedStatement = require("./prepared_statement.js");
76
const QueryResult = require("./query_result.js");
87

98
module.exports = {
109
Connection,
1110
Database,
12-
LoggingLevel,
1311
PreparedStatement,
1412
QueryResult,
1513
get VERSION() {

0 commit comments

Comments
 (0)