xbintsc 0.6.20 → 0.6.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +5 -0
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/main.js +1 -0
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/driver/build-key.d.ts +3 -0
- package/dist/src/driver/build-key.js +5 -2
- package/dist/src/driver/build-key.js.map +1 -1
- package/dist/src/driver/compiler.js +11 -0
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/config.d.ts +12 -0
- package/dist/src/driver/config.js +22 -0
- package/dist/src/driver/config.js.map +1 -1
- package/dist/src/driver/fingerprint.d.ts +11 -0
- package/dist/src/driver/fingerprint.js +25 -0
- package/dist/src/driver/fingerprint.js.map +1 -1
- package/dist/src/driver/index.d.ts +2 -1
- package/dist/src/driver/index.js +1 -0
- package/dist/src/driver/index.js.map +1 -1
- package/dist/src/driver/link-features.js +36 -11
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/options.d.ts +8 -1
- package/dist/src/driver/paths.d.ts +10 -0
- package/dist/src/driver/paths.js +18 -0
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +22 -6
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/storage.d.ts +34 -0
- package/dist/src/driver/storage.js +67 -0
- package/dist/src/driver/storage.js.map +1 -0
- package/dist/src/driver/toolchain.d.ts +1 -1
- package/dist/src/driver/toolchain.js +10 -1
- package/dist/src/driver/toolchain.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/gui/features.d.ts +1 -1
- package/dist/src/extensions/gui/features.js +2 -2
- package/dist/src/extensions/gui/features.js.map +1 -1
- package/dist/src/extensions/gui/index.js +3 -1
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/node/http2/feature.d.ts +14 -0
- package/dist/src/extensions/node/http2/feature.js +28 -0
- package/dist/src/extensions/node/http2/feature.js.map +1 -0
- package/dist/src/extensions/node/http2/index.d.ts +5 -1
- package/dist/src/extensions/node/http2/index.js +6 -1
- package/dist/src/extensions/node/http2/index.js.map +1 -1
- package/dist/src/extensions/node/index.js +3 -1
- package/dist/src/extensions/node/index.js.map +1 -1
- package/dist/src/extensions/node/tls/feature.d.ts +12 -0
- package/dist/src/extensions/node/tls/feature.js +24 -0
- package/dist/src/extensions/node/tls/feature.js.map +1 -0
- package/dist/src/extensions/node/tls/index.d.ts +5 -1
- package/dist/src/extensions/node/tls/index.js +5 -1
- package/dist/src/extensions/node/tls/index.js.map +1 -1
- package/dist/src/extensions/node/zlib/compression.js +11 -6
- package/dist/src/extensions/node/zlib/compression.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +4 -1
- package/dist/src/extensions/registry.js +9 -2
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/storage.d.ts +22 -0
- package/dist/src/extensions/storage.js +51 -0
- package/dist/src/extensions/storage.js.map +1 -0
- package/dist/src/extensions/tls.d.ts +26 -0
- package/dist/src/extensions/tls.js +46 -0
- package/dist/src/extensions/tls.js.map +1 -0
- package/dist/tests/driver/config.test.js +31 -0
- package/dist/tests/driver/config.test.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +13 -2
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/driver/storage.test.d.ts +1 -0
- package/dist/tests/driver/storage.test.js +29 -0
- package/dist/tests/driver/storage.test.js.map +1 -0
- package/dist/tests/e2e/fetch.test.js +3 -1
- package/dist/tests/e2e/fetch.test.js.map +1 -1
- package/dist/tests/e2e/http2.test.d.ts +7 -0
- package/dist/tests/e2e/http2.test.js +201 -0
- package/dist/tests/e2e/http2.test.js.map +1 -0
- package/dist/tests/e2e/net-helpers.d.ts +28 -0
- package/dist/tests/e2e/net-helpers.js +59 -0
- package/dist/tests/e2e/net-helpers.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +3 -5
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/e2e/storage.test.d.ts +7 -0
- package/dist/tests/e2e/storage.test.js +86 -0
- package/dist/tests/e2e/storage.test.js.map +1 -0
- package/dist/tests/e2e/tls.test.d.ts +10 -0
- package/dist/tests/e2e/tls.test.js +197 -0
- package/dist/tests/e2e/tls.test.js.map +1 -0
- package/dist/tests/extensions/gui-features.test.js +1 -1
- package/dist/tests/extensions/gui-features.test.js.map +1 -1
- package/dist/tests/extensions/storage.test.d.ts +1 -0
- package/dist/tests/extensions/storage.test.js +51 -0
- package/dist/tests/extensions/storage.test.js.map +1 -0
- package/doc/ai/cli.md +2 -0
- package/doc/ai/contributing.md +14 -0
- package/doc/ai/extensions.md +31 -3
- package/doc/ai/language-support.md +1 -1
- package/doc/ai/zh-CN/cli.md +2 -0
- package/doc/ai/zh-CN/contributing.md +12 -0
- package/doc/ai/zh-CN/extensions.md +27 -2
- package/doc/ai/zh-CN/language-support.md +1 -1
- package/doc/gui/api.md +1 -1
- package/doc/gui/build.md +7 -5
- package/doc/gui/media.md +4 -4
- package/doc/gui/rendering.md +1 -1
- package/doc/gui.md +3 -3
- package/doc/implemented/async.md +1 -1
- package/doc/implemented/extensions.md +2 -1
- package/doc/implemented/fetch.md +19 -4
- package/doc/implemented/node/http2.md +51 -0
- package/doc/implemented/node/tls.md +56 -0
- package/doc/implemented/node.md +8 -0
- package/doc/implemented/storage.md +65 -0
- package/doc/implemented.md +4 -2
- package/doc/unimplemented/node/modules.md +6 -6
- package/doc/unimplemented/toolchain.md +1 -1
- package/doc/xbintsc.config.schema.json +16 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/gui/build.md +6 -4
- package/doc/zh-CN/gui/media.md +3 -3
- package/doc/zh-CN/gui/rendering.md +1 -1
- package/doc/zh-CN/gui.md +3 -3
- package/doc/zh-CN/implemented/async.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +2 -1
- package/doc/zh-CN/implemented/fetch.md +15 -3
- package/doc/zh-CN/implemented/node/http2.md +44 -0
- package/doc/zh-CN/implemented/node/tls.md +47 -0
- package/doc/zh-CN/implemented/node.md +7 -0
- package/doc/zh-CN/implemented/storage.md +52 -0
- package/doc/zh-CN/implemented.md +4 -2
- package/doc/zh-CN/unimplemented/node/modules.md +3 -3
- package/doc/zh-CN/unimplemented/toolchain.md +1 -1
- package/package.json +16 -8
- package/runtime/ext_gui/dom_api_form.cpp +10 -0
- package/runtime/ext_gui/gui_media.cpp +1 -1
- package/runtime/ext_gui/media.cpp +1 -1
- package/runtime/ext_gui/media_api.h +2 -2
- package/runtime/ext_node/http2/http2.c +25 -11
- package/runtime/ext_node/http2/http2_backend.c +48 -0
- package/runtime/ext_node/http2/http2_bridge.c +25 -0
- package/runtime/ext_node/http2/parts/api.inc +40 -0
- package/runtime/ext_node/http2/parts/client.inc +215 -0
- package/runtime/ext_node/http2/parts/common.inc +330 -0
- package/runtime/ext_node/http2/parts/events.inc +139 -0
- package/runtime/ext_node/http2/parts/server.inc +301 -0
- package/runtime/ext_node/http2/parts/streams.inc +254 -0
- package/runtime/ext_node/tls/parts/api.inc +38 -0
- package/runtime/ext_node/tls/parts/connect.inc +107 -0
- package/runtime/ext_node/tls/parts/helpers.inc +121 -0
- package/runtime/ext_node/tls/parts/server.inc +101 -0
- package/runtime/ext_node/tls/parts/socket.inc +171 -0
- package/runtime/ext_node/tls/tls.c +21 -13
- package/runtime/ext_node/tls/tls_backend.c +50 -0
- package/runtime/ext_node/zlib/{miniz/compression_bridge.c → compression_bridge.c} +1 -1
- package/runtime/xt_fetch/async.inc +19 -12
- package/runtime/xt_fetch/socket.inc +55 -0
- package/runtime/xt_fetch.c +3 -0
- package/runtime/xt_http2/nghttp2_compat.h +19 -0
- package/runtime/xt_http2/nghttp2_static.h +18 -0
- package/runtime/xt_http2_api.h +38 -0
- package/runtime/xt_node_tls_api.h +37 -0
- package/runtime/xt_protocol_registry.c +22 -0
- package/runtime/xt_storage/indexed_db.c +408 -0
- package/runtime/xt_storage/indexed_db_internal.h +26 -0
- package/runtime/xt_storage/indexed_db_store.c +305 -0
- package/runtime/xt_storage/storage.c +209 -0
- package/runtime/xt_storage/storage_internal.h +66 -0
- package/runtime/xt_tls/parts/api.inc +21 -0
- package/runtime/xt_tls/parts/client.inc +112 -0
- package/runtime/xt_tls/parts/io.inc +157 -0
- package/runtime/xt_tls/parts/server.inc +102 -0
- package/runtime/xt_tls/parts/setup.inc +146 -0
- package/runtime/xt_tls/tls_bridge.c +27 -0
- package/runtime/xt_tls/tls_core.c +39 -0
- package/runtime/xt_tls/tls_internal.h +73 -0
- package/runtime/xt_tls_api.h +73 -0
- package/scripts/build-gui.ts +9 -8
- package/scripts/build-media.ts +2 -2
- package/scripts/build-tls.ts +248 -0
- package/scripts/check-file-length.ts +4 -21
- package/scripts/fetch-vendored.ts +244 -0
- package/scripts/package-release.ts +13 -3
- package/src/cli/main.ts +1 -0
- package/src/driver/build-key.ts +13 -2
- package/src/driver/compiler.ts +13 -0
- package/src/driver/config.ts +35 -0
- package/src/driver/fingerprint.ts +24 -0
- package/src/driver/index.ts +8 -0
- package/src/driver/link-features.ts +33 -11
- package/src/driver/options.ts +8 -1
- package/src/driver/paths.ts +20 -0
- package/src/driver/runtime-objects.ts +25 -6
- package/src/driver/storage.ts +84 -0
- package/src/driver/toolchain.ts +11 -2
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/features.ts +2 -2
- package/src/extensions/gui/index.ts +3 -1
- package/src/extensions/node/http2/feature.ts +33 -0
- package/src/extensions/node/http2/index.ts +6 -1
- package/src/extensions/node/index.ts +3 -1
- package/src/extensions/node/tls/feature.ts +29 -0
- package/src/extensions/node/tls/index.ts +5 -1
- package/src/extensions/node/zlib/compression.ts +11 -6
- package/src/extensions/registry.ts +9 -2
- package/src/extensions/storage.ts +58 -0
- package/src/extensions/tls.ts +52 -0
- package/vendor/sqlite3/sqlite3.c +271131 -0
- package/vendor/sqlite3/sqlite3.h +14447 -0
- package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
- /package/runtime/{ext_media → xt_media}/decode_audio.cpp +0 -0
- /package/runtime/{ext_media → xt_media}/media.cpp +0 -0
- /package/runtime/{ext_media → xt_media}/media_bridge.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_common.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_export.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_zip.h +0 -0
- /package/{runtime/ext_gui → vendor/nanosvg}/nanosvg.h +0 -0
- /package/{runtime/ext_gui → vendor/nanosvg}/nanosvgrast.h +0 -0
- /package/{runtime/ext_gui → vendor/stb}/stb_truetype.h +0 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc `storage` extension — IndexedDB object stores.
|
|
3
|
+
*
|
|
4
|
+
* `put` / `add` / `get` / `getAll` / `getKey` / `delete` / `clear` / `count`
|
|
5
|
+
* plus the object store handle itself. Every operation is applied synchronously
|
|
6
|
+
* to SQLite and settles its IDBRequest from a microtask (see indexed_db.c), so
|
|
7
|
+
* the store surface keeps the same call shape as the browser while staying
|
|
8
|
+
* single-threaded.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#include "indexed_db_internal.h"
|
|
12
|
+
|
|
13
|
+
/* -- key handling --------------------------------------------------------- */
|
|
14
|
+
|
|
15
|
+
/* Allocate (or increment) the auto-increment sequence for one store. */
|
|
16
|
+
static xt_value idb_next_seq(sqlite3 *db, const char *dbname, const char *store) {
|
|
17
|
+
sqlite3_stmt *stmt = NULL;
|
|
18
|
+
sqlite3_prepare_v2(db,
|
|
19
|
+
"INSERT INTO __idb_seq(db,store,seq) VALUES(?1,?2,1)"
|
|
20
|
+
" ON CONFLICT(db,store) DO UPDATE SET seq=seq+1 RETURNING seq",
|
|
21
|
+
-1, &stmt, NULL);
|
|
22
|
+
if (stmt == NULL) return XT_UNDEFINED;
|
|
23
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
24
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
25
|
+
xt_value key = XT_UNDEFINED;
|
|
26
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) key = xt_number((double)sqlite3_column_int64(stmt, 0));
|
|
27
|
+
sqlite3_finalize(stmt);
|
|
28
|
+
return key;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/*
|
|
32
|
+
* Resolve the key for a `put`/`add` value: the keyPath property when set, the
|
|
33
|
+
* explicit key otherwise, or a fresh auto-increment key. Returns XT_UNDEFINED
|
|
34
|
+
* when no key could be produced.
|
|
35
|
+
*/
|
|
36
|
+
static xt_value idb_resolve_key(sqlite3 *db, const char *dbname, const char *store, xt_value value,
|
|
37
|
+
xt_value explicit_key, xt_value key_path, int auto_increment) {
|
|
38
|
+
xt_value key = XT_UNDEFINED;
|
|
39
|
+
if (!XT_IS_NULL(key_path) && XT_IS_OBJECT(value)) {
|
|
40
|
+
key = xt_get(value, key_path);
|
|
41
|
+
} else if (!XT_IS_UNDEFINED(explicit_key)) {
|
|
42
|
+
key = explicit_key;
|
|
43
|
+
}
|
|
44
|
+
if (XT_IS_UNDEFINED(key) && auto_increment) key = idb_next_seq(db, dbname, store);
|
|
45
|
+
return key;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/* Serialize a value for the `value` column (JSON, or "null" when unencodable). */
|
|
49
|
+
static xt_value idb_serialize(xt_value value) {
|
|
50
|
+
xt_value serialized = idb_encode(value);
|
|
51
|
+
return XT_IS_UNDEFINED(serialized) ? xt_string_from_cstr("null") : serialized;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/* -- operations ----------------------------------------------------------- */
|
|
55
|
+
|
|
56
|
+
static xt_value idb_store_put(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
57
|
+
(void)env;
|
|
58
|
+
sqlite3 *db = xt_storage_db();
|
|
59
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__db"));
|
|
60
|
+
const char *store = xt_storage_cstr(xt_object_get_cstr(self, "__store"));
|
|
61
|
+
xt_value value = xt_arg(argc, argv, 0);
|
|
62
|
+
xt_value explicit_key = argc >= 2 ? xt_arg(argc, argv, 1) : XT_UNDEFINED;
|
|
63
|
+
xt_value req = idb_request_new();
|
|
64
|
+
xt_storage_set(req, "source", self);
|
|
65
|
+
|
|
66
|
+
xt_value key_path = XT_NULL;
|
|
67
|
+
int auto_increment = 0;
|
|
68
|
+
idb_store_meta(db, dbname, store, &key_path, &auto_increment);
|
|
69
|
+
xt_value key = idb_resolve_key(db, dbname, store, value, explicit_key, key_path, auto_increment);
|
|
70
|
+
if (XT_IS_UNDEFINED(key)) {
|
|
71
|
+
idb_complete(req, 0, xt_storage_error("DataError: no key was supplied"));
|
|
72
|
+
return req;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
sqlite3_stmt *stmt = NULL;
|
|
76
|
+
sqlite3_prepare_v2(db,
|
|
77
|
+
"INSERT INTO __idb_records(db,store,key,value) VALUES(?1,?2,?3,?4)"
|
|
78
|
+
" ON CONFLICT(db,store,key) DO UPDATE SET value=excluded.value",
|
|
79
|
+
-1, &stmt, NULL);
|
|
80
|
+
if (stmt != NULL) {
|
|
81
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
82
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
83
|
+
xt_storage_bind(stmt, 3, idb_encode(key));
|
|
84
|
+
xt_storage_bind(stmt, 4, idb_serialize(value));
|
|
85
|
+
sqlite3_step(stmt);
|
|
86
|
+
sqlite3_finalize(stmt);
|
|
87
|
+
}
|
|
88
|
+
idb_complete(req, 1, key);
|
|
89
|
+
return req;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static xt_value idb_store_add(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
93
|
+
(void)env;
|
|
94
|
+
sqlite3 *db = xt_storage_db();
|
|
95
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__db"));
|
|
96
|
+
const char *store = xt_storage_cstr(xt_object_get_cstr(self, "__store"));
|
|
97
|
+
xt_value value = xt_arg(argc, argv, 0);
|
|
98
|
+
xt_value explicit_key = argc >= 2 ? xt_arg(argc, argv, 1) : XT_UNDEFINED;
|
|
99
|
+
xt_value req = idb_request_new();
|
|
100
|
+
xt_storage_set(req, "source", self);
|
|
101
|
+
|
|
102
|
+
xt_value key_path = XT_NULL;
|
|
103
|
+
int auto_increment = 0;
|
|
104
|
+
idb_store_meta(db, dbname, store, &key_path, &auto_increment);
|
|
105
|
+
xt_value key = idb_resolve_key(db, dbname, store, value, explicit_key, key_path, auto_increment);
|
|
106
|
+
if (XT_IS_UNDEFINED(key)) {
|
|
107
|
+
idb_complete(req, 0, xt_storage_error("DataError: no key was supplied"));
|
|
108
|
+
return req;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
sqlite3_stmt *stmt = NULL;
|
|
112
|
+
sqlite3_prepare_v2(db, "INSERT INTO __idb_records(db,store,key,value) VALUES(?1,?2,?3,?4)", -1, &stmt,
|
|
113
|
+
NULL);
|
|
114
|
+
if (stmt != NULL) {
|
|
115
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
116
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
117
|
+
xt_storage_bind(stmt, 3, idb_encode(key));
|
|
118
|
+
xt_storage_bind(stmt, 4, idb_serialize(value));
|
|
119
|
+
int rc = sqlite3_step(stmt);
|
|
120
|
+
sqlite3_finalize(stmt);
|
|
121
|
+
if (rc != SQLITE_DONE) {
|
|
122
|
+
idb_complete(req, 0, xt_storage_error("ConstraintError: key already exists"));
|
|
123
|
+
return req;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
idb_complete(req, 1, key);
|
|
127
|
+
return req;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static xt_value idb_store_get(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
131
|
+
(void)env;
|
|
132
|
+
sqlite3 *db = xt_storage_db();
|
|
133
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__db"));
|
|
134
|
+
const char *store = xt_storage_cstr(xt_object_get_cstr(self, "__store"));
|
|
135
|
+
xt_value req = idb_request_new();
|
|
136
|
+
xt_storage_set(req, "source", self);
|
|
137
|
+
sqlite3_stmt *stmt = NULL;
|
|
138
|
+
sqlite3_prepare_v2(db, "SELECT value FROM __idb_records WHERE db=?1 AND store=?2 AND key=?3", -1,
|
|
139
|
+
&stmt, NULL);
|
|
140
|
+
xt_value result = XT_UNDEFINED;
|
|
141
|
+
if (stmt != NULL) {
|
|
142
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
143
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
144
|
+
xt_storage_bind(stmt, 3, idb_encode(xt_arg(argc, argv, 0)));
|
|
145
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) result = idb_decode(xt_storage_column(stmt, 0));
|
|
146
|
+
sqlite3_finalize(stmt);
|
|
147
|
+
}
|
|
148
|
+
idb_complete(req, 1, result);
|
|
149
|
+
return req;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
static xt_value idb_store_get_all(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
153
|
+
(void)env;
|
|
154
|
+
(void)argc;
|
|
155
|
+
(void)argv;
|
|
156
|
+
sqlite3 *db = xt_storage_db();
|
|
157
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__db"));
|
|
158
|
+
const char *store = xt_storage_cstr(xt_object_get_cstr(self, "__store"));
|
|
159
|
+
xt_value req = idb_request_new();
|
|
160
|
+
xt_storage_set(req, "source", self);
|
|
161
|
+
sqlite3_stmt *stmt = NULL;
|
|
162
|
+
sqlite3_prepare_v2(db, "SELECT value FROM __idb_records WHERE db=?1 AND store=?2 ORDER BY key", -1,
|
|
163
|
+
&stmt, NULL);
|
|
164
|
+
xt_value results = xt_array_new(0, NULL);
|
|
165
|
+
if (stmt != NULL) {
|
|
166
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
167
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
168
|
+
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
169
|
+
xt_array_push(results, idb_decode(xt_storage_column(stmt, 0)));
|
|
170
|
+
}
|
|
171
|
+
sqlite3_finalize(stmt);
|
|
172
|
+
}
|
|
173
|
+
idb_complete(req, 1, results);
|
|
174
|
+
return req;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static xt_value idb_store_get_key(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
178
|
+
(void)env;
|
|
179
|
+
sqlite3 *db = xt_storage_db();
|
|
180
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__db"));
|
|
181
|
+
const char *store = xt_storage_cstr(xt_object_get_cstr(self, "__store"));
|
|
182
|
+
xt_value req = idb_request_new();
|
|
183
|
+
xt_storage_set(req, "source", self);
|
|
184
|
+
sqlite3_stmt *stmt = NULL;
|
|
185
|
+
sqlite3_prepare_v2(db, "SELECT key FROM __idb_records WHERE db=?1 AND store=?2 AND key=?3", -1, &stmt,
|
|
186
|
+
NULL);
|
|
187
|
+
xt_value result = XT_UNDEFINED;
|
|
188
|
+
if (stmt != NULL) {
|
|
189
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
190
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
191
|
+
xt_storage_bind(stmt, 3, idb_encode(xt_arg(argc, argv, 0)));
|
|
192
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) result = idb_decode(xt_storage_column(stmt, 0));
|
|
193
|
+
sqlite3_finalize(stmt);
|
|
194
|
+
}
|
|
195
|
+
idb_complete(req, 1, result);
|
|
196
|
+
return req;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
static xt_value idb_store_delete(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
200
|
+
(void)env;
|
|
201
|
+
sqlite3 *db = xt_storage_db();
|
|
202
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__db"));
|
|
203
|
+
const char *store = xt_storage_cstr(xt_object_get_cstr(self, "__store"));
|
|
204
|
+
xt_value req = idb_request_new();
|
|
205
|
+
xt_storage_set(req, "source", self);
|
|
206
|
+
sqlite3_stmt *stmt = NULL;
|
|
207
|
+
sqlite3_prepare_v2(db, "DELETE FROM __idb_records WHERE db=?1 AND store=?2 AND key=?3", -1, &stmt, NULL);
|
|
208
|
+
if (stmt != NULL) {
|
|
209
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
210
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
211
|
+
xt_storage_bind(stmt, 3, idb_encode(xt_arg(argc, argv, 0)));
|
|
212
|
+
sqlite3_step(stmt);
|
|
213
|
+
sqlite3_finalize(stmt);
|
|
214
|
+
}
|
|
215
|
+
idb_complete(req, 1, XT_UNDEFINED);
|
|
216
|
+
return req;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
static xt_value idb_store_clear(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
220
|
+
(void)env;
|
|
221
|
+
(void)argc;
|
|
222
|
+
(void)argv;
|
|
223
|
+
sqlite3 *db = xt_storage_db();
|
|
224
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__db"));
|
|
225
|
+
const char *store = xt_storage_cstr(xt_object_get_cstr(self, "__store"));
|
|
226
|
+
xt_value req = idb_request_new();
|
|
227
|
+
xt_storage_set(req, "source", self);
|
|
228
|
+
sqlite3_stmt *stmt = NULL;
|
|
229
|
+
sqlite3_prepare_v2(db, "DELETE FROM __idb_records WHERE db=?1 AND store=?2", -1, &stmt, NULL);
|
|
230
|
+
if (stmt != NULL) {
|
|
231
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
232
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
233
|
+
sqlite3_step(stmt);
|
|
234
|
+
sqlite3_finalize(stmt);
|
|
235
|
+
}
|
|
236
|
+
idb_complete(req, 1, XT_UNDEFINED);
|
|
237
|
+
return req;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
static xt_value idb_store_count(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
241
|
+
(void)env;
|
|
242
|
+
(void)argc;
|
|
243
|
+
(void)argv;
|
|
244
|
+
sqlite3 *db = xt_storage_db();
|
|
245
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__db"));
|
|
246
|
+
const char *store = xt_storage_cstr(xt_object_get_cstr(self, "__store"));
|
|
247
|
+
xt_value req = idb_request_new();
|
|
248
|
+
xt_storage_set(req, "source", self);
|
|
249
|
+
sqlite3_stmt *stmt = NULL;
|
|
250
|
+
sqlite3_prepare_v2(db, "SELECT COUNT(*) FROM __idb_records WHERE db=?1 AND store=?2", -1, &stmt, NULL);
|
|
251
|
+
double count = 0;
|
|
252
|
+
if (stmt != NULL) {
|
|
253
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
254
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
255
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) count = (double)sqlite3_column_int64(stmt, 0);
|
|
256
|
+
sqlite3_finalize(stmt);
|
|
257
|
+
}
|
|
258
|
+
idb_complete(req, 1, xt_number(count));
|
|
259
|
+
return req;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/* -- handle --------------------------------------------------------------- */
|
|
263
|
+
|
|
264
|
+
static xt_value idb_store_key_path_get(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
265
|
+
(void)env;
|
|
266
|
+
(void)argc;
|
|
267
|
+
(void)argv;
|
|
268
|
+
xt_value key_path = XT_NULL;
|
|
269
|
+
int auto_increment = 0;
|
|
270
|
+
idb_store_meta(xt_storage_db(), xt_storage_cstr(xt_object_get_cstr(self, "__db")),
|
|
271
|
+
xt_storage_cstr(xt_object_get_cstr(self, "__store")), &key_path, &auto_increment);
|
|
272
|
+
return key_path;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
static xt_value idb_store_auto_increment_get(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
276
|
+
(void)env;
|
|
277
|
+
(void)argc;
|
|
278
|
+
(void)argv;
|
|
279
|
+
xt_value key_path = XT_NULL;
|
|
280
|
+
int auto_increment = 0;
|
|
281
|
+
idb_store_meta(xt_storage_db(), xt_storage_cstr(xt_object_get_cstr(self, "__db")),
|
|
282
|
+
xt_storage_cstr(xt_object_get_cstr(self, "__store")), &key_path, &auto_increment);
|
|
283
|
+
return xt_bool(auto_increment);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
xt_value idb_store_build(const char *dbname, const char *store) {
|
|
287
|
+
xt_value handle = xt_object_new();
|
|
288
|
+
xt_storage_set(handle, "__db", xt_string_from_cstr(dbname));
|
|
289
|
+
xt_storage_set(handle, "__store", xt_string_from_cstr(store));
|
|
290
|
+
xt_storage_set_cstr(handle, "name", store);
|
|
291
|
+
xt_storage_set(handle, "indexNames", xt_array_new(0, NULL));
|
|
292
|
+
xt_object_define_getter(handle, xt_string_from_cstr("keyPath"),
|
|
293
|
+
xt_closure_new((void *)idb_store_key_path_get, 0, NULL));
|
|
294
|
+
xt_object_define_getter(handle, xt_string_from_cstr("autoIncrement"),
|
|
295
|
+
xt_closure_new((void *)idb_store_auto_increment_get, 0, NULL));
|
|
296
|
+
xt_storage_method(handle, "put", (void *)idb_store_put);
|
|
297
|
+
xt_storage_method(handle, "add", (void *)idb_store_add);
|
|
298
|
+
xt_storage_method(handle, "get", (void *)idb_store_get);
|
|
299
|
+
xt_storage_method(handle, "getAll", (void *)idb_store_get_all);
|
|
300
|
+
xt_storage_method(handle, "getKey", (void *)idb_store_get_key);
|
|
301
|
+
xt_storage_method(handle, "delete", (void *)idb_store_delete);
|
|
302
|
+
xt_storage_method(handle, "clear", (void *)idb_store_clear);
|
|
303
|
+
xt_storage_method(handle, "count", (void *)idb_store_count);
|
|
304
|
+
return handle;
|
|
305
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc `storage` extension — Web Storage (localStorage / sessionStorage).
|
|
3
|
+
*
|
|
4
|
+
* Both objects share the Storage surface (`getItem`, `setItem`, `removeItem`,
|
|
5
|
+
* `clear`, `key`, `length`) but address different databases: localStorage uses
|
|
6
|
+
* the configured backend (in-memory by default, or the file embedded by the
|
|
7
|
+
* driver), while sessionStorage always uses a private in-memory database.
|
|
8
|
+
*
|
|
9
|
+
* The backend selection is compile-time: the driver embeds `xt_storage_backend`
|
|
10
|
+
* and `xt_storage_path` (see src/driver/storage.ts), and this unit reads them
|
|
11
|
+
* the first time a database is opened.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
#include "storage_internal.h"
|
|
15
|
+
|
|
16
|
+
/* Embedded configuration symbols. Strong definitions come from the object the
|
|
17
|
+
* driver links for every program that enables the storage extension. */
|
|
18
|
+
extern const unsigned int xt_storage_backend;
|
|
19
|
+
extern const unsigned char xt_storage_path[];
|
|
20
|
+
extern const unsigned long long xt_storage_path_len;
|
|
21
|
+
|
|
22
|
+
static sqlite3 *g_db = NULL;
|
|
23
|
+
static sqlite3 *g_session_db = NULL;
|
|
24
|
+
|
|
25
|
+
static void storage_init_schema(sqlite3 *db) {
|
|
26
|
+
const char *sql =
|
|
27
|
+
"CREATE TABLE IF NOT EXISTS __web_storage("
|
|
28
|
+
" scope TEXT NOT NULL, key TEXT NOT NULL, value TEXT NOT NULL,"
|
|
29
|
+
" PRIMARY KEY(scope, key));"
|
|
30
|
+
"CREATE TABLE IF NOT EXISTS __idb_meta("
|
|
31
|
+
" db TEXT PRIMARY KEY, version INTEGER NOT NULL);"
|
|
32
|
+
"CREATE TABLE IF NOT EXISTS __idb_stores("
|
|
33
|
+
" db TEXT NOT NULL, store TEXT NOT NULL, key_path TEXT,"
|
|
34
|
+
" auto_increment INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(db, store));"
|
|
35
|
+
"CREATE TABLE IF NOT EXISTS __idb_records("
|
|
36
|
+
" db TEXT NOT NULL, store TEXT NOT NULL, key TEXT NOT NULL, value TEXT NOT NULL,"
|
|
37
|
+
" PRIMARY KEY(db, store, key));"
|
|
38
|
+
"CREATE TABLE IF NOT EXISTS __idb_seq("
|
|
39
|
+
" db TEXT NOT NULL, store TEXT NOT NULL, seq INTEGER NOT NULL,"
|
|
40
|
+
" PRIMARY KEY(db, store));";
|
|
41
|
+
sqlite3_exec(db, sql, NULL, NULL, NULL);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static void storage_open(sqlite3 **out, const char *path) {
|
|
45
|
+
if (*out != NULL) return;
|
|
46
|
+
if (sqlite3_open(path, out) != SQLITE_OK) {
|
|
47
|
+
const char *message = sqlite3_errmsg(*out);
|
|
48
|
+
sqlite3_close(*out);
|
|
49
|
+
*out = NULL;
|
|
50
|
+
xt_throw(xt_storage_error(message != NULL ? message : "storage: cannot open database"));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
storage_init_schema(*out);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
sqlite3 *xt_storage_db(void) {
|
|
57
|
+
const char *path = xt_storage_backend != 0 ? (const char *)xt_storage_path : ":memory:";
|
|
58
|
+
storage_open(&g_db, path);
|
|
59
|
+
return g_db;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
sqlite3 *xt_storage_session_db(void) {
|
|
63
|
+
storage_open(&g_session_db, ":memory:");
|
|
64
|
+
return g_session_db;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/* -- Storage surface ------------------------------------------------------ */
|
|
68
|
+
|
|
69
|
+
static sqlite3 *storage_db_for(xt_value self) {
|
|
70
|
+
const char *scope = xt_storage_cstr(xt_object_get_cstr(self, "__scope"));
|
|
71
|
+
return scope != NULL && strcmp(scope, "session") == 0 ? xt_storage_session_db() : xt_storage_db();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static xt_value storage_scope_of(xt_value self) {
|
|
75
|
+
return xt_object_get_cstr(self, "__scope");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static xt_value storage_get_item(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
79
|
+
(void)env;
|
|
80
|
+
sqlite3 *db = storage_db_for(self);
|
|
81
|
+
sqlite3_stmt *stmt = NULL;
|
|
82
|
+
if (sqlite3_prepare_v2(db, "SELECT value FROM __web_storage WHERE scope=?1 AND key=?2", -1, &stmt, NULL) !=
|
|
83
|
+
SQLITE_OK) {
|
|
84
|
+
return XT_NULL;
|
|
85
|
+
}
|
|
86
|
+
xt_storage_bind(stmt, 1, storage_scope_of(self));
|
|
87
|
+
xt_storage_bind(stmt, 2, xt_to_string(xt_arg(argc, argv, 0)));
|
|
88
|
+
xt_value result = XT_NULL;
|
|
89
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) result = xt_storage_column(stmt, 0);
|
|
90
|
+
sqlite3_finalize(stmt);
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static xt_value storage_set_item(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
95
|
+
(void)env;
|
|
96
|
+
sqlite3 *db = storage_db_for(self);
|
|
97
|
+
sqlite3_stmt *stmt = NULL;
|
|
98
|
+
sqlite3_prepare_v2(db,
|
|
99
|
+
"INSERT INTO __web_storage(scope,key,value) VALUES(?1,?2,?3)"
|
|
100
|
+
" ON CONFLICT(scope,key) DO UPDATE SET value=excluded.value",
|
|
101
|
+
-1, &stmt, NULL);
|
|
102
|
+
if (stmt != NULL) {
|
|
103
|
+
xt_storage_bind(stmt, 1, storage_scope_of(self));
|
|
104
|
+
xt_storage_bind(stmt, 2, xt_to_string(xt_arg(argc, argv, 0)));
|
|
105
|
+
xt_storage_bind(stmt, 3, xt_to_string(xt_arg(argc, argv, 1)));
|
|
106
|
+
sqlite3_step(stmt);
|
|
107
|
+
sqlite3_finalize(stmt);
|
|
108
|
+
}
|
|
109
|
+
return XT_UNDEFINED;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static xt_value storage_remove_item(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
113
|
+
(void)env;
|
|
114
|
+
sqlite3 *db = storage_db_for(self);
|
|
115
|
+
sqlite3_stmt *stmt = NULL;
|
|
116
|
+
sqlite3_prepare_v2(db, "DELETE FROM __web_storage WHERE scope=?1 AND key=?2", -1, &stmt, NULL);
|
|
117
|
+
if (stmt != NULL) {
|
|
118
|
+
xt_storage_bind(stmt, 1, storage_scope_of(self));
|
|
119
|
+
xt_storage_bind(stmt, 2, xt_to_string(xt_arg(argc, argv, 0)));
|
|
120
|
+
sqlite3_step(stmt);
|
|
121
|
+
sqlite3_finalize(stmt);
|
|
122
|
+
}
|
|
123
|
+
return XT_UNDEFINED;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
static xt_value storage_clear(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
127
|
+
(void)env;
|
|
128
|
+
(void)argc;
|
|
129
|
+
(void)argv;
|
|
130
|
+
sqlite3 *db = storage_db_for(self);
|
|
131
|
+
sqlite3_stmt *stmt = NULL;
|
|
132
|
+
sqlite3_prepare_v2(db, "DELETE FROM __web_storage WHERE scope=?1", -1, &stmt, NULL);
|
|
133
|
+
if (stmt != NULL) {
|
|
134
|
+
xt_storage_bind(stmt, 1, storage_scope_of(self));
|
|
135
|
+
sqlite3_step(stmt);
|
|
136
|
+
sqlite3_finalize(stmt);
|
|
137
|
+
}
|
|
138
|
+
return XT_UNDEFINED;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
static xt_value storage_length_get(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
142
|
+
(void)env;
|
|
143
|
+
(void)argc;
|
|
144
|
+
(void)argv;
|
|
145
|
+
sqlite3 *db = storage_db_for(self);
|
|
146
|
+
sqlite3_stmt *stmt = NULL;
|
|
147
|
+
sqlite3_prepare_v2(db, "SELECT COUNT(*) FROM __web_storage WHERE scope=?1", -1, &stmt, NULL);
|
|
148
|
+
double count = 0;
|
|
149
|
+
if (stmt != NULL) {
|
|
150
|
+
xt_storage_bind(stmt, 1, storage_scope_of(self));
|
|
151
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) count = (double)sqlite3_column_int64(stmt, 0);
|
|
152
|
+
sqlite3_finalize(stmt);
|
|
153
|
+
}
|
|
154
|
+
return xt_number(count);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
static xt_value storage_key(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
158
|
+
(void)env;
|
|
159
|
+
sqlite3 *db = storage_db_for(self);
|
|
160
|
+
double at = argc > 0 ? xt_to_number(argv[0]) : -1;
|
|
161
|
+
if (at < 0) return XT_NULL;
|
|
162
|
+
sqlite3_stmt *stmt = NULL;
|
|
163
|
+
sqlite3_prepare_v2(db, "SELECT key FROM __web_storage WHERE scope=?1 ORDER BY rowid LIMIT 1 OFFSET ?2",
|
|
164
|
+
-1, &stmt, NULL);
|
|
165
|
+
xt_value result = XT_NULL;
|
|
166
|
+
if (stmt != NULL) {
|
|
167
|
+
xt_storage_bind(stmt, 1, storage_scope_of(self));
|
|
168
|
+
sqlite3_bind_int64(stmt, 2, (sqlite3_int64)at);
|
|
169
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) result = xt_storage_column(stmt, 0);
|
|
170
|
+
sqlite3_finalize(stmt);
|
|
171
|
+
}
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
static xt_value storage_build(const char *scope) {
|
|
176
|
+
xt_value storage = xt_object_new();
|
|
177
|
+
xt_storage_set(storage, "__scope", xt_string_from_cstr(scope));
|
|
178
|
+
xt_storage_method(storage, "getItem", (void *)storage_get_item);
|
|
179
|
+
xt_storage_method(storage, "setItem", (void *)storage_set_item);
|
|
180
|
+
xt_storage_method(storage, "removeItem", (void *)storage_remove_item);
|
|
181
|
+
xt_storage_method(storage, "clear", (void *)storage_clear);
|
|
182
|
+
xt_storage_method(storage, "key", (void *)storage_key);
|
|
183
|
+
xt_object_define_getter(storage, xt_string_from_cstr("length"),
|
|
184
|
+
xt_closure_new((void *)storage_length_get, 0, NULL));
|
|
185
|
+
return storage;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static xt_value g_local_storage = XT_UNDEFINED;
|
|
189
|
+
static xt_value g_session_storage = XT_UNDEFINED;
|
|
190
|
+
|
|
191
|
+
xt_value xt_storage_local(int32_t argc, xt_value *argv) {
|
|
192
|
+
(void)argc;
|
|
193
|
+
(void)argv;
|
|
194
|
+
if (!XT_IS_OBJECT(g_local_storage)) {
|
|
195
|
+
g_local_storage = storage_build("local");
|
|
196
|
+
xt_gc_add_root(&g_local_storage);
|
|
197
|
+
}
|
|
198
|
+
return g_local_storage;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
xt_value xt_storage_session(int32_t argc, xt_value *argv) {
|
|
202
|
+
(void)argc;
|
|
203
|
+
(void)argv;
|
|
204
|
+
if (!XT_IS_OBJECT(g_session_storage)) {
|
|
205
|
+
g_session_storage = storage_build("session");
|
|
206
|
+
xt_gc_add_root(&g_session_storage);
|
|
207
|
+
}
|
|
208
|
+
return g_session_storage;
|
|
209
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Shared helpers for the xbintsc `storage` extension.
|
|
3
|
+
*
|
|
4
|
+
* localStorage / sessionStorage / indexedDB are implemented on top of a vendored
|
|
5
|
+
* SQLite (vendor/sqlite3, fetched by `npm run vendor`). This header owns the
|
|
6
|
+
* small value helpers and the two database handles; the Web Storage surface
|
|
7
|
+
* lives in `storage.c` and the IndexedDB surface in `indexed_db.c`.
|
|
8
|
+
*/
|
|
9
|
+
#ifndef XT_STORAGE_INTERNAL_H
|
|
10
|
+
#define XT_STORAGE_INTERNAL_H
|
|
11
|
+
|
|
12
|
+
#include "rt_internal.h"
|
|
13
|
+
#include "sqlite3/sqlite3.h"
|
|
14
|
+
|
|
15
|
+
#include <stdio.h>
|
|
16
|
+
#include <stdlib.h>
|
|
17
|
+
#include <string.h>
|
|
18
|
+
|
|
19
|
+
/* The persistent database (localStorage + indexedDB): the configured backend —
|
|
20
|
+
* an in-memory database by default, or the file embedded by the driver. */
|
|
21
|
+
sqlite3 *xt_storage_db(void);
|
|
22
|
+
/* The session database (sessionStorage): always in-memory, regardless of the
|
|
23
|
+
* configured backend, because a session never outlives the process. */
|
|
24
|
+
sqlite3 *xt_storage_session_db(void);
|
|
25
|
+
|
|
26
|
+
/* -- small value helpers -------------------------------------------------- */
|
|
27
|
+
|
|
28
|
+
static inline const char *xt_storage_cstr(xt_value value) {
|
|
29
|
+
const char *data = xt_string_data(xt_to_string(value));
|
|
30
|
+
return data != NULL ? data : "";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static inline void xt_storage_set(xt_value object, const char *key, xt_value value) {
|
|
34
|
+
xt_object_set(object, xt_string_from_cstr(key), value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static inline void xt_storage_set_cstr(xt_value object, const char *key, const char *value) {
|
|
38
|
+
xt_storage_set(object, key, xt_string_from_cstr(value));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static inline xt_value xt_storage_get(xt_value object, const char *key) {
|
|
42
|
+
return xt_object_get_cstr(object, key);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static inline void xt_storage_method(xt_value object, const char *name, void *fn) {
|
|
46
|
+
xt_storage_set(object, name, xt_closure_new(fn, 0, NULL));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static inline xt_value xt_storage_error(const char *message) {
|
|
50
|
+
return xt_error_from_message(message);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* -- SQLite binding helpers ----------------------------------------------- */
|
|
54
|
+
|
|
55
|
+
/* Bind a JavaScript string (which may contain embedded NUL bytes) as a length-
|
|
56
|
+
* aware text value. SQLITE_TRANSIENT makes SQLite copy the bytes. */
|
|
57
|
+
static inline void xt_storage_bind(sqlite3_stmt *stmt, int index, xt_value value) {
|
|
58
|
+
sqlite3_bind_text(stmt, index, xt_string_data(value), (int)xt_string_length_value(value), SQLITE_TRANSIENT);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* Read a text column as a JavaScript string (length-aware). */
|
|
62
|
+
static inline xt_value xt_storage_column(sqlite3_stmt *stmt, int index) {
|
|
63
|
+
return xt_string_new((const char *)sqlite3_column_text(stmt, index), (size_t)sqlite3_column_bytes(stmt, index));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#endif /* XT_STORAGE_INTERNAL_H */
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/* -- API table and registration ------------------------------------------- */
|
|
2
|
+
|
|
3
|
+
static const XtTlsApi kTlsApi = {
|
|
4
|
+
xt_tls_client_connect,
|
|
5
|
+
xt_tls_server_config_new,
|
|
6
|
+
xt_tls_server_accept,
|
|
7
|
+
xt_tls_conn_read,
|
|
8
|
+
xt_tls_conn_write,
|
|
9
|
+
xt_tls_conn_shutdown,
|
|
10
|
+
xt_tls_conn_close,
|
|
11
|
+
xt_tls_conn_free,
|
|
12
|
+
xt_tls_server_config_free,
|
|
13
|
+
xt_tls_conn_alpn,
|
|
14
|
+
xt_tls_conn_error,
|
|
15
|
+
xt_tls_conn_fd,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/* Called by `tls_bridge.c`, which is the feature's compiled `runtimeSources()`
|
|
19
|
+
* entry. The call is what makes the linker extract this object (and the mbedTLS
|
|
20
|
+
* members it references) from `tls.lib`. */
|
|
21
|
+
void xt_tls_backend_boot(void) { xt_tls_core_register(&kTlsApi); }
|