|
| 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 | +} |
0 commit comments