Skip to content

Commit 3da2f08

Browse files
feat: integrate stowDB with lightshell.db.* JS API
1 parent 3465719 commit 3da2f08

4 files changed

Lines changed: 126 additions & 8 deletions

File tree

Makefile

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,29 @@ ENGINE_REPO = https://github.com/lightshell-dev/r8e.git
88
ENGINE_VERSION = v0.1.0
99
ENGINE_DIR = engine/r8e
1010

11+
# === stowDB (auto-fetched) ===
12+
STOW_REPO = https://github.com/lightshell-dev/stowDB.git
13+
STOW_VERSION = main
14+
STOW_DIR = engine/stowDB
15+
1116
# === Compiler ===
1217
UNAME_S := $(shell uname -s)
1318
CC ?= clang
1419
CFLAGS = -std=c11 -Wall -Wextra -Wpedantic \
15-
-I$(ENGINE_DIR)/src/gpu -I$(ENGINE_DIR)/src -I$(ENGINE_DIR)/include
20+
-I$(ENGINE_DIR)/src/gpu -I$(ENGINE_DIR)/src -I$(ENGINE_DIR)/include \
21+
-I$(STOW_DIR)/include
1622

1723
R8E_LIB = $(ENGINE_DIR)/build/libr8e.a
24+
STOW_LIB = $(STOW_DIR)/build/libstow.a
1825
BUILD_DIR = build
1926

2027
# === Platform-specific ===
2128
ifeq ($(UNAME_S),Linux)
2229
OBJCFLAGS =
2330
LDFLAGS = -lvulkan -lX11 -lm
2431
SRCS_PLATFORM = src/platform_linux.c src/gpu_vulkan.c
25-
SRCS_C = src/image.c src/text.c src/glyph_atlas.c src/api_fs.c src/api_sysinfo.c
32+
SRCS_C = src/image.c src/text.c src/glyph_atlas.c src/api_fs.c src/api_sysinfo.c \
33+
src/api_db.c
2634
SRCS_MAIN = src/main_linux.c
2735
else ifeq ($(UNAME_S),Darwin)
2836
OBJCFLAGS = -fobjc-arc
@@ -31,7 +39,8 @@ else ifeq ($(UNAME_S),Darwin)
3139
src/api_clipboard_darwin.m src/api_shell_darwin.m \
3240
src/api_dialog_darwin.m src/api_menu_darwin.m
3341
SRCS_C = src/image.c src/text.c src/glyph_atlas.c src/api_fs.c src/api_sysinfo.c \
34-
src/api_window.c src/api_app.c src/api_console.c src/api_timers.c
42+
src/api_window.c src/api_app.c src/api_console.c src/api_timers.c \
43+
src/api_db.c
3544
SRCS_MAIN =
3645
endif
3746

@@ -52,7 +61,7 @@ else ifeq ($(UNAME_S),Darwin)
5261
endif
5362

5463
# === Targets ===
55-
.PHONY: all clean run-demo engine shaders
64+
.PHONY: all clean run-demo engine stowdb shaders
5665

5766
all: $(BUILD_DIR)/lightshell-demo
5867

@@ -69,21 +78,36 @@ $(R8E_LIB): | $(ENGINE_DIR)
6978
@cd $(ENGINE_DIR) && make release
7079
@echo "[lightshell] r8e engine built."
7180

72-
engine: $(R8E_LIB)
81+
engine: $(R8E_LIB) $(STOW_LIB)
82+
83+
# --- Auto-fetch stowDB ---
84+
$(STOW_DIR):
85+
@echo "[lightshell] Fetching stowDB..."
86+
@mkdir -p engine
87+
@git clone --depth 1 --branch $(STOW_VERSION) $(STOW_REPO) $(STOW_DIR)
88+
@echo "[lightshell] stowDB fetched."
89+
90+
# --- Build stowDB library ---
91+
$(STOW_LIB): | $(STOW_DIR)
92+
@echo "[lightshell] Building stowDB..."
93+
@cd $(STOW_DIR) && make release
94+
@echo "[lightshell] stowDB built."
95+
96+
stowdb: $(STOW_LIB)
7397

7498
# --- Compile GLSL shaders (Linux only, run manually) ---
7599
shaders:
76100
cd src/shaders && sh compile_shaders.sh
77101

78102
# --- Link demo binary ---
79-
$(BUILD_DIR)/lightshell-demo: $(ALL_OBJS) $(R8E_LIB)
103+
$(BUILD_DIR)/lightshell-demo: $(ALL_OBJS) $(R8E_LIB) $(STOW_LIB)
80104
$(CC) $(LDFLAGS) -o $@ $^
81105

82106
# --- Compile rules ---
83-
$(BUILD_DIR)/%.o: src/%.m | $(BUILD_DIR) $(ENGINE_DIR)
107+
$(BUILD_DIR)/%.o: src/%.m | $(BUILD_DIR) $(ENGINE_DIR) $(STOW_DIR)
84108
$(CC) $(CFLAGS) $(OBJCFLAGS) -c -o $@ $<
85109

86-
$(BUILD_DIR)/%.o: src/%.c | $(BUILD_DIR) $(ENGINE_DIR)
110+
$(BUILD_DIR)/%.o: src/%.c | $(BUILD_DIR) $(ENGINE_DIR) $(STOW_DIR)
87111
$(CC) $(CFLAGS) -c -o $@ $<
88112

89113
$(BUILD_DIR)/r8e_display_list.o: $(SRCS_R8E_DL) | $(BUILD_DIR) $(ENGINE_DIR)

src/api.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ void ls_api_console_init(R8EContext *ctx);
4343
/* Timer API (setTimeout/setInterval/clear*) */
4444
void ls_api_timers_init(R8EContext *ctx);
4545

46+
/* Database API (stowDB) */
47+
void ls_api_db_init(R8EContext *ctx);
48+
4649
/* Tick timers — call each frame from event loop */
4750
void ls_timers_tick(R8EContext *ctx);
4851

src/api_db.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "api.h"
2+
#include "stow.h"
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
static StowDB *g_db = NULL;
7+
8+
/* lightshell.db.open(path) — open/create database */
9+
static R8EValue api_db_open(R8EContext *ctx, R8EValue this_val,
10+
int argc, const R8EValue *argv) {
11+
(void)this_val;
12+
if (argc < 1) return R8E_UNDEFINED;
13+
char buf[256]; size_t len;
14+
const char *path = r8e_get_cstring(argv[0], buf, &len);
15+
char pathbuf[512];
16+
snprintf(pathbuf, sizeof(pathbuf), "%.*s", (int)len, path);
17+
18+
if (g_db) stow_close(g_db);
19+
g_db = stow_open(pathbuf);
20+
return g_db ? R8E_TRUE : R8E_FALSE;
21+
}
22+
23+
/* lightshell.db.set(key, value) */
24+
static R8EValue api_db_set(R8EContext *ctx, R8EValue this_val,
25+
int argc, const R8EValue *argv) {
26+
(void)this_val;
27+
if (!g_db || argc < 2) return R8E_FALSE;
28+
char kbuf[256]; size_t klen;
29+
const char *key = r8e_get_cstring(argv[0], kbuf, &klen);
30+
char vbuf[1024]; size_t vlen;
31+
const char *val = r8e_get_cstring(argv[1], vbuf, &vlen);
32+
33+
char keybuf[512];
34+
snprintf(keybuf, sizeof(keybuf), "%.*s", (int)klen, key);
35+
36+
int r = stow_set(g_db, keybuf, val, (uint32_t)vlen);
37+
return r == STOW_OK ? R8E_TRUE : R8E_FALSE;
38+
}
39+
40+
/* lightshell.db.get(key) */
41+
static R8EValue api_db_get(R8EContext *ctx, R8EValue this_val,
42+
int argc, const R8EValue *argv) {
43+
(void)this_val;
44+
if (!g_db || argc < 1) return R8E_UNDEFINED;
45+
char kbuf[256]; size_t klen;
46+
const char *key = r8e_get_cstring(argv[0], kbuf, &klen);
47+
char keybuf[512];
48+
snprintf(keybuf, sizeof(keybuf), "%.*s", (int)klen, key);
49+
50+
void *val; uint32_t vlen;
51+
int r = stow_get(g_db, keybuf, &val, &vlen);
52+
if (r != STOW_OK) return R8E_UNDEFINED;
53+
54+
return r8e_make_string(ctx, (const char *)val, vlen);
55+
}
56+
57+
/* lightshell.db.delete(key) */
58+
static R8EValue api_db_delete(R8EContext *ctx, R8EValue this_val,
59+
int argc, const R8EValue *argv) {
60+
(void)this_val;
61+
if (!g_db || argc < 1) return R8E_FALSE;
62+
char kbuf[256]; size_t klen;
63+
const char *key = r8e_get_cstring(argv[0], kbuf, &klen);
64+
char keybuf[512];
65+
snprintf(keybuf, sizeof(keybuf), "%.*s", (int)klen, key);
66+
67+
int r = stow_delete(g_db, keybuf);
68+
return r == STOW_OK ? R8E_TRUE : R8E_FALSE;
69+
}
70+
71+
/* lightshell.db.close() */
72+
static R8EValue api_db_close(R8EContext *ctx, R8EValue this_val,
73+
int argc, const R8EValue *argv) {
74+
(void)ctx; (void)this_val; (void)argc; (void)argv;
75+
if (g_db) { stow_close(g_db); g_db = NULL; }
76+
return R8E_UNDEFINED;
77+
}
78+
79+
/* Registration */
80+
void ls_api_db_init(R8EContext *ctx) {
81+
R8EValue db = r8e_make_object(ctx);
82+
r8e_set_prop(ctx, db, "open", r8e_make_native_func(ctx, api_db_open, "open", 1));
83+
r8e_set_prop(ctx, db, "set", r8e_make_native_func(ctx, api_db_set, "set", 2));
84+
r8e_set_prop(ctx, db, "get", r8e_make_native_func(ctx, api_db_get, "get", 1));
85+
r8e_set_prop(ctx, db, "delete", r8e_make_native_func(ctx, api_db_delete, "delete", 1));
86+
r8e_set_prop(ctx, db, "close", r8e_make_native_func(ctx, api_db_close, "close", 0));
87+
88+
R8EValue ls = ls_get_namespace(ctx);
89+
r8e_set_prop(ctx, ls, "db", db);
90+
}

src/main.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ int main(int argc, char **argv) {
6767
ls_api_app_init(ctx);
6868
ls_api_console_init(ctx);
6969
ls_api_timers_init(ctx);
70+
ls_api_db_init(ctx);
7071

7172
/* Verify native APIs */
7273
{

0 commit comments

Comments
 (0)