xbintsc 0.6.20 → 0.6.22
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/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 +3 -1
- 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/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/options.d.ts +8 -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/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/registry.d.ts +4 -1
- package/dist/src/extensions/registry.js +8 -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 +50 -0
- package/dist/src/extensions/storage.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 +4 -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/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/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 +50 -0
- package/dist/tests/extensions/storage.test.js.map +1 -0
- package/doc/ai/cli.md +2 -0
- package/doc/ai/extensions.md +26 -0
- package/doc/ai/zh-CN/cli.md +2 -0
- package/doc/ai/zh-CN/extensions.md +23 -0
- package/doc/gui/build.md +3 -3
- package/doc/gui/media.md +4 -4
- package/doc/gui.md +2 -2
- package/doc/implemented/extensions.md +1 -1
- package/doc/implemented/storage.md +64 -0
- package/doc/implemented.md +2 -0
- package/doc/xbintsc.config.schema.json +16 -1
- package/doc/zh-CN/gui/build.md +3 -3
- package/doc/zh-CN/gui/media.md +3 -3
- package/doc/zh-CN/gui.md +2 -2
- package/doc/zh-CN/implemented/extensions.md +1 -1
- package/doc/zh-CN/implemented/storage.md +51 -0
- package/doc/zh-CN/implemented.md +2 -0
- package/package.json +7 -7
- 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/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/sqlite3/sqlite3.c +271131 -0
- package/runtime/xt_storage/sqlite3/sqlite3.h +14447 -0
- package/runtime/xt_storage/storage.c +209 -0
- package/runtime/xt_storage/storage_internal.h +66 -0
- package/scripts/build-gui.ts +1 -1
- package/scripts/build-media.ts +2 -2
- package/scripts/check-file-length.ts +4 -0
- package/src/cli/main.ts +1 -0
- package/src/driver/build-key.ts +6 -1
- package/src/driver/compiler.ts +13 -0
- package/src/driver/config.ts +35 -0
- package/src/driver/index.ts +8 -0
- package/src/driver/options.ts +8 -1
- package/src/driver/storage.ts +84 -0
- 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/registry.ts +8 -2
- package/src/extensions/storage.ts +57 -0
- /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
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc `storage` extension — IndexedDB core.
|
|
3
|
+
*
|
|
4
|
+
* A focused, synchronous-core subset of the browser IndexedDB API backed by the
|
|
5
|
+
* same SQLite database as `localStorage`: `open`, `deleteDatabase`, databases,
|
|
6
|
+
* transactions, and the object store operations in `indexed_db_store.c`.
|
|
7
|
+
* Requests deliver their result from a microtask, so `onsuccess` / `onerror` /
|
|
8
|
+
* `onupgradeneeded` fire asynchronously exactly like the browser.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#include "indexed_db_internal.h"
|
|
12
|
+
|
|
13
|
+
/* Forward declaration: `idb_database_transaction` and `idb_open_cb` both build a
|
|
14
|
+
* transaction, whose definition follows the database handle below. */
|
|
15
|
+
static xt_value idb_transaction_build(const char *dbname, xt_value names, const char *mode);
|
|
16
|
+
|
|
17
|
+
/* -- request plumbing ----------------------------------------------------- */
|
|
18
|
+
|
|
19
|
+
xt_value idb_request_new(void) {
|
|
20
|
+
xt_value req = xt_object_new();
|
|
21
|
+
xt_storage_set(req, "result", XT_UNDEFINED);
|
|
22
|
+
xt_storage_set(req, "error", XT_NULL);
|
|
23
|
+
xt_storage_set_cstr(req, "readyState", "pending");
|
|
24
|
+
xt_storage_set(req, "source", XT_NULL);
|
|
25
|
+
xt_storage_set(req, "transaction", XT_NULL);
|
|
26
|
+
return req;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* Settle a request from a microtask: set `result`/`error`, mark it done, and
|
|
30
|
+
* invoke `onsuccess`/`onerror` (read at fire time, so assigning the handler
|
|
31
|
+
* after the call still works). */
|
|
32
|
+
static xt_value idb_complete_cb(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
33
|
+
(void)self;
|
|
34
|
+
(void)argc;
|
|
35
|
+
(void)argv;
|
|
36
|
+
xt_value req = xt_closure_env(env, 0);
|
|
37
|
+
xt_value value = xt_closure_env(env, 1);
|
|
38
|
+
int success = xt_truthy(xt_closure_env(env, 2));
|
|
39
|
+
xt_storage_set_cstr(req, "readyState", "done");
|
|
40
|
+
if (success) {
|
|
41
|
+
xt_storage_set(req, "result", value);
|
|
42
|
+
} else {
|
|
43
|
+
xt_storage_set(req, "error", value);
|
|
44
|
+
}
|
|
45
|
+
const char *prop = success ? "onsuccess" : "onerror";
|
|
46
|
+
const char *type = success ? "success" : "error";
|
|
47
|
+
xt_value cb = xt_object_get_cstr(req, prop);
|
|
48
|
+
if (XT_IS_FUNCTION(cb)) {
|
|
49
|
+
xt_value event = xt_object_new();
|
|
50
|
+
xt_storage_set(event, "target", req);
|
|
51
|
+
xt_storage_set_cstr(event, "type", type);
|
|
52
|
+
xt_value args[1] = {event};
|
|
53
|
+
xt_call_with_this(cb, req, 1, args);
|
|
54
|
+
}
|
|
55
|
+
return XT_UNDEFINED;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
void idb_complete(xt_value req, int success, xt_value value) {
|
|
59
|
+
xt_value captured[3] = {req, value, xt_bool(success)};
|
|
60
|
+
xt_value task = xt_closure_new((void *)idb_complete_cb, 3, captured);
|
|
61
|
+
xt_value args[1] = {task};
|
|
62
|
+
xt_queue_microtask(1, args);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* Fire a single event property (`oncomplete`, ...) from a microtask. */
|
|
66
|
+
static xt_value idb_event_cb(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
67
|
+
(void)self;
|
|
68
|
+
(void)argc;
|
|
69
|
+
(void)argv;
|
|
70
|
+
xt_value target = xt_closure_env(env, 0);
|
|
71
|
+
const char *prop = xt_storage_cstr(xt_closure_env(env, 1));
|
|
72
|
+
const char *type = xt_storage_cstr(xt_closure_env(env, 2));
|
|
73
|
+
xt_value cb = xt_object_get_cstr(target, prop);
|
|
74
|
+
if (XT_IS_FUNCTION(cb)) {
|
|
75
|
+
xt_value event = xt_object_new();
|
|
76
|
+
xt_storage_set(event, "target", target);
|
|
77
|
+
xt_storage_set_cstr(event, "type", type);
|
|
78
|
+
xt_value args[1] = {event};
|
|
79
|
+
xt_call_with_this(cb, target, 1, args);
|
|
80
|
+
}
|
|
81
|
+
return XT_UNDEFINED;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static void idb_schedule_event(xt_value target, const char *prop, const char *type) {
|
|
85
|
+
xt_value captured[3] = {target, xt_string_from_cstr(prop), xt_string_from_cstr(type)};
|
|
86
|
+
xt_value task = xt_closure_new((void *)idb_event_cb, 3, captured);
|
|
87
|
+
xt_value args[1] = {task};
|
|
88
|
+
xt_queue_microtask(1, args);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/* -- JSON helpers --------------------------------------------------------- */
|
|
92
|
+
|
|
93
|
+
xt_value idb_encode(xt_value value) { return xt_json_stringify(1, &value); }
|
|
94
|
+
|
|
95
|
+
xt_value idb_decode(xt_value text) {
|
|
96
|
+
xt_value args[1] = {text};
|
|
97
|
+
return xt_json_parse(1, args);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* -- database metadata ---------------------------------------------------- */
|
|
101
|
+
|
|
102
|
+
static int64_t idb_version(sqlite3 *db, const char *name) {
|
|
103
|
+
sqlite3_stmt *stmt = NULL;
|
|
104
|
+
sqlite3_prepare_v2(db, "SELECT version FROM __idb_meta WHERE db=?1", -1, &stmt, NULL);
|
|
105
|
+
if (stmt == NULL) return 0;
|
|
106
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(name));
|
|
107
|
+
int64_t version = 0;
|
|
108
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) version = sqlite3_column_int64(stmt, 0);
|
|
109
|
+
sqlite3_finalize(stmt);
|
|
110
|
+
return version;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static void idb_set_version(sqlite3 *db, const char *name, int64_t version) {
|
|
114
|
+
sqlite3_stmt *stmt = NULL;
|
|
115
|
+
sqlite3_prepare_v2(db,
|
|
116
|
+
"INSERT INTO __idb_meta(db,version) VALUES(?1,?2)"
|
|
117
|
+
" ON CONFLICT(db) DO UPDATE SET version=excluded.version",
|
|
118
|
+
-1, &stmt, NULL);
|
|
119
|
+
if (stmt == NULL) return;
|
|
120
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(name));
|
|
121
|
+
sqlite3_bind_int64(stmt, 2, version);
|
|
122
|
+
sqlite3_step(stmt);
|
|
123
|
+
sqlite3_finalize(stmt);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
static int idb_store_exists(sqlite3 *db, const char *name, const char *store) {
|
|
127
|
+
sqlite3_stmt *stmt = NULL;
|
|
128
|
+
sqlite3_prepare_v2(db, "SELECT 1 FROM __idb_stores WHERE db=?1 AND store=?2", -1, &stmt, NULL);
|
|
129
|
+
if (stmt == NULL) return 0;
|
|
130
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(name));
|
|
131
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
132
|
+
int exists = sqlite3_step(stmt) == SQLITE_ROW;
|
|
133
|
+
sqlite3_finalize(stmt);
|
|
134
|
+
return exists;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
void idb_store_meta(sqlite3 *db, const char *name, const char *store, xt_value *key_path,
|
|
138
|
+
int *auto_increment) {
|
|
139
|
+
*key_path = XT_NULL;
|
|
140
|
+
*auto_increment = 0;
|
|
141
|
+
sqlite3_stmt *stmt = NULL;
|
|
142
|
+
sqlite3_prepare_v2(db, "SELECT key_path, auto_increment FROM __idb_stores WHERE db=?1 AND store=?2",
|
|
143
|
+
-1, &stmt, NULL);
|
|
144
|
+
if (stmt == NULL) return;
|
|
145
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(name));
|
|
146
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
147
|
+
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
148
|
+
if (sqlite3_column_type(stmt, 0) != SQLITE_NULL) *key_path = xt_storage_column(stmt, 0);
|
|
149
|
+
*auto_increment = sqlite3_column_int(stmt, 1);
|
|
150
|
+
}
|
|
151
|
+
sqlite3_finalize(stmt);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/* -- database handle ------------------------------------------------------ */
|
|
155
|
+
|
|
156
|
+
static xt_value idb_object_store_names_get(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
157
|
+
(void)env;
|
|
158
|
+
(void)argc;
|
|
159
|
+
(void)argv;
|
|
160
|
+
sqlite3 *db = xt_storage_db();
|
|
161
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__name"));
|
|
162
|
+
sqlite3_stmt *stmt = NULL;
|
|
163
|
+
sqlite3_prepare_v2(db, "SELECT store FROM __idb_stores WHERE db=?1 ORDER BY rowid", -1, &stmt, NULL);
|
|
164
|
+
xt_value names = xt_array_new(0, NULL);
|
|
165
|
+
if (stmt != NULL) {
|
|
166
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
167
|
+
while (sqlite3_step(stmt) == SQLITE_ROW) xt_array_push(names, xt_storage_column(stmt, 0));
|
|
168
|
+
sqlite3_finalize(stmt);
|
|
169
|
+
}
|
|
170
|
+
return names;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
static xt_value idb_database_create_object_store(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
174
|
+
(void)env;
|
|
175
|
+
sqlite3 *db = xt_storage_db();
|
|
176
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__name"));
|
|
177
|
+
const char *store = xt_storage_cstr(xt_arg(argc, argv, 0));
|
|
178
|
+
const char *key_path = NULL;
|
|
179
|
+
int auto_increment = 0;
|
|
180
|
+
if (argc >= 2 && XT_IS_OBJECT(argv[1])) {
|
|
181
|
+
xt_value key_path_value = xt_object_get_cstr(argv[1], "keyPath");
|
|
182
|
+
if (XT_IS_STRING(key_path_value)) key_path = xt_storage_cstr(key_path_value);
|
|
183
|
+
auto_increment = xt_truthy(xt_object_get_cstr(argv[1], "autoIncrement"));
|
|
184
|
+
}
|
|
185
|
+
if (idb_store_exists(db, dbname, store)) {
|
|
186
|
+
xt_throw(xt_storage_error("ConstraintError: an object store with that name already exists"));
|
|
187
|
+
return XT_UNDEFINED;
|
|
188
|
+
}
|
|
189
|
+
sqlite3_stmt *stmt = NULL;
|
|
190
|
+
sqlite3_prepare_v2(db, "INSERT INTO __idb_stores(db,store,key_path,auto_increment) VALUES(?1,?2,?3,?4)",
|
|
191
|
+
-1, &stmt, NULL);
|
|
192
|
+
if (stmt != NULL) {
|
|
193
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
194
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
195
|
+
if (key_path != NULL) {
|
|
196
|
+
xt_storage_bind(stmt, 3, xt_string_from_cstr(key_path));
|
|
197
|
+
} else {
|
|
198
|
+
sqlite3_bind_null(stmt, 3);
|
|
199
|
+
}
|
|
200
|
+
sqlite3_bind_int(stmt, 4, auto_increment);
|
|
201
|
+
sqlite3_step(stmt);
|
|
202
|
+
sqlite3_finalize(stmt);
|
|
203
|
+
}
|
|
204
|
+
return idb_store_build(dbname, store);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
static xt_value idb_database_delete_object_store(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
208
|
+
(void)env;
|
|
209
|
+
sqlite3 *db = xt_storage_db();
|
|
210
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__name"));
|
|
211
|
+
const char *store = xt_storage_cstr(xt_arg(argc, argv, 0));
|
|
212
|
+
sqlite3_stmt *stmt = NULL;
|
|
213
|
+
sqlite3_prepare_v2(db, "DELETE FROM __idb_stores WHERE db=?1 AND store=?2", -1, &stmt, NULL);
|
|
214
|
+
if (stmt != NULL) {
|
|
215
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
216
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
217
|
+
sqlite3_step(stmt);
|
|
218
|
+
sqlite3_finalize(stmt);
|
|
219
|
+
}
|
|
220
|
+
sqlite3_prepare_v2(db, "DELETE FROM __idb_records WHERE db=?1 AND store=?2", -1, &stmt, NULL);
|
|
221
|
+
if (stmt != NULL) {
|
|
222
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(dbname));
|
|
223
|
+
xt_storage_bind(stmt, 2, xt_string_from_cstr(store));
|
|
224
|
+
sqlite3_step(stmt);
|
|
225
|
+
sqlite3_finalize(stmt);
|
|
226
|
+
}
|
|
227
|
+
return XT_UNDEFINED;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static xt_value idb_database_transaction(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
231
|
+
(void)env;
|
|
232
|
+
sqlite3 *db = xt_storage_db();
|
|
233
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__name"));
|
|
234
|
+
xt_value raw = xt_arg(argc, argv, 0);
|
|
235
|
+
xt_value names = XT_IS_ARRAY(raw) ? raw : xt_array_new(1, &raw);
|
|
236
|
+
int32_t count = xt_array_size(names);
|
|
237
|
+
for (int32_t i = 0; i < count; i++) {
|
|
238
|
+
xt_value store = xt_array_get(names, xt_number((double)i));
|
|
239
|
+
if (!idb_store_exists(db, dbname, xt_storage_cstr(store))) {
|
|
240
|
+
xt_throw(xt_storage_error("NotFoundError: one of the object stores does not exist"));
|
|
241
|
+
return XT_UNDEFINED;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const char *mode = argc >= 2 ? xt_storage_cstr(argv[1]) : "readonly";
|
|
245
|
+
return idb_transaction_build(dbname, names, mode);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
static xt_value idb_database_close(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
249
|
+
(void)self;
|
|
250
|
+
(void)env;
|
|
251
|
+
(void)argc;
|
|
252
|
+
(void)argv;
|
|
253
|
+
return XT_UNDEFINED;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
static xt_value idb_database_build(const char *name, double version) {
|
|
257
|
+
xt_value dbase = xt_object_new();
|
|
258
|
+
xt_storage_set(dbase, "__name", xt_string_from_cstr(name));
|
|
259
|
+
xt_storage_set_cstr(dbase, "name", name);
|
|
260
|
+
xt_storage_set(dbase, "version", xt_number(version));
|
|
261
|
+
xt_object_define_getter(dbase, xt_string_from_cstr("objectStoreNames"),
|
|
262
|
+
xt_closure_new((void *)idb_object_store_names_get, 0, NULL));
|
|
263
|
+
xt_storage_method(dbase, "createObjectStore", (void *)idb_database_create_object_store);
|
|
264
|
+
xt_storage_method(dbase, "deleteObjectStore", (void *)idb_database_delete_object_store);
|
|
265
|
+
xt_storage_method(dbase, "transaction", (void *)idb_database_transaction);
|
|
266
|
+
xt_storage_method(dbase, "close", (void *)idb_database_close);
|
|
267
|
+
return dbase;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/* -- transaction handle --------------------------------------------------- */
|
|
271
|
+
|
|
272
|
+
static xt_value idb_transaction_object_store(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
273
|
+
(void)env;
|
|
274
|
+
const char *dbname = xt_storage_cstr(xt_object_get_cstr(self, "__db"));
|
|
275
|
+
return idb_store_build(dbname, xt_storage_cstr(xt_arg(argc, argv, 0)));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
static xt_value idb_transaction_build(const char *dbname, xt_value names, const char *mode) {
|
|
279
|
+
xt_value tx = xt_object_new();
|
|
280
|
+
xt_storage_set(tx, "__db", xt_string_from_cstr(dbname));
|
|
281
|
+
xt_storage_set(tx, "__names", names);
|
|
282
|
+
xt_storage_set_cstr(tx, "mode", mode);
|
|
283
|
+
xt_storage_set(tx, "objectStoreNames", names);
|
|
284
|
+
xt_storage_set(tx, "error", XT_NULL);
|
|
285
|
+
xt_storage_method(tx, "objectStore", (void *)idb_transaction_object_store);
|
|
286
|
+
/* Store operations are applied synchronously, so `oncomplete` can be scheduled
|
|
287
|
+
* immediately; it reads the (possibly later-assigned) handler when it runs. */
|
|
288
|
+
idb_schedule_event(tx, "oncomplete", "complete");
|
|
289
|
+
return tx;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/* -- open / delete -------------------------------------------------------- */
|
|
293
|
+
|
|
294
|
+
static xt_value idb_open_cb(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
295
|
+
(void)self;
|
|
296
|
+
(void)argc;
|
|
297
|
+
(void)argv;
|
|
298
|
+
xt_value req = xt_closure_env(env, 0);
|
|
299
|
+
const char *name = xt_storage_cstr(xt_closure_env(env, 1));
|
|
300
|
+
double version = xt_to_number(xt_closure_env(env, 2));
|
|
301
|
+
sqlite3 *db = xt_storage_db();
|
|
302
|
+
|
|
303
|
+
int64_t current = idb_version(db, name);
|
|
304
|
+
double final_version = current > 0 ? (double)current : 0;
|
|
305
|
+
int upgrade = version > (double)current && version > 0;
|
|
306
|
+
|
|
307
|
+
if (version == 0) {
|
|
308
|
+
if (current == 0) {
|
|
309
|
+
idb_set_version(db, name, 1);
|
|
310
|
+
final_version = 1;
|
|
311
|
+
}
|
|
312
|
+
} else if (upgrade) {
|
|
313
|
+
final_version = version;
|
|
314
|
+
xt_value dbase = idb_database_build(name, version);
|
|
315
|
+
xt_storage_set(req, "result", dbase);
|
|
316
|
+
xt_value names = xt_array_new(0, NULL);
|
|
317
|
+
xt_storage_set(req, "transaction", idb_transaction_build(name, names, "versionchange"));
|
|
318
|
+
xt_value cb = xt_object_get_cstr(req, "onupgradeneeded");
|
|
319
|
+
if (XT_IS_FUNCTION(cb)) {
|
|
320
|
+
xt_value event = xt_object_new();
|
|
321
|
+
xt_storage_set(event, "target", req);
|
|
322
|
+
xt_storage_set_cstr(event, "type", "upgradeneeded");
|
|
323
|
+
xt_value args[1] = {event};
|
|
324
|
+
xt_call_with_this(cb, req, 1, args);
|
|
325
|
+
}
|
|
326
|
+
idb_set_version(db, name, (int64_t)version);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
idb_complete(req, 1, idb_database_build(name, final_version));
|
|
330
|
+
return XT_UNDEFINED;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
static xt_value idb_open(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
334
|
+
(void)self;
|
|
335
|
+
(void)env;
|
|
336
|
+
const char *name = xt_storage_cstr(xt_arg(argc, argv, 0));
|
|
337
|
+
double version = argc >= 2 ? xt_to_number(argv[1]) : 0;
|
|
338
|
+
xt_value req = idb_request_new();
|
|
339
|
+
xt_value captured[3] = {req, xt_string_from_cstr(name), xt_number(version)};
|
|
340
|
+
xt_value task = xt_closure_new((void *)idb_open_cb, 3, captured);
|
|
341
|
+
xt_value args[1] = {task};
|
|
342
|
+
xt_queue_microtask(1, args);
|
|
343
|
+
return req;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
static xt_value idb_delete_cb(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
347
|
+
(void)self;
|
|
348
|
+
(void)argc;
|
|
349
|
+
(void)argv;
|
|
350
|
+
xt_value req = xt_closure_env(env, 0);
|
|
351
|
+
const char *name = xt_storage_cstr(xt_closure_env(env, 1));
|
|
352
|
+
sqlite3 *db = xt_storage_db();
|
|
353
|
+
sqlite3_stmt *stmt = NULL;
|
|
354
|
+
sqlite3_prepare_v2(db, "DELETE FROM __idb_meta WHERE db=?1", -1, &stmt, NULL);
|
|
355
|
+
if (stmt != NULL) {
|
|
356
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(name));
|
|
357
|
+
sqlite3_step(stmt);
|
|
358
|
+
sqlite3_finalize(stmt);
|
|
359
|
+
}
|
|
360
|
+
sqlite3_prepare_v2(db, "DELETE FROM __idb_stores WHERE db=?1", -1, &stmt, NULL);
|
|
361
|
+
if (stmt != NULL) {
|
|
362
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(name));
|
|
363
|
+
sqlite3_step(stmt);
|
|
364
|
+
sqlite3_finalize(stmt);
|
|
365
|
+
}
|
|
366
|
+
sqlite3_prepare_v2(db, "DELETE FROM __idb_records WHERE db=?1", -1, &stmt, NULL);
|
|
367
|
+
if (stmt != NULL) {
|
|
368
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(name));
|
|
369
|
+
sqlite3_step(stmt);
|
|
370
|
+
sqlite3_finalize(stmt);
|
|
371
|
+
}
|
|
372
|
+
sqlite3_prepare_v2(db, "DELETE FROM __idb_seq WHERE db=?1", -1, &stmt, NULL);
|
|
373
|
+
if (stmt != NULL) {
|
|
374
|
+
xt_storage_bind(stmt, 1, xt_string_from_cstr(name));
|
|
375
|
+
sqlite3_step(stmt);
|
|
376
|
+
sqlite3_finalize(stmt);
|
|
377
|
+
}
|
|
378
|
+
idb_complete(req, 1, XT_UNDEFINED);
|
|
379
|
+
return XT_UNDEFINED;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
static xt_value idb_delete_database(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
383
|
+
(void)self;
|
|
384
|
+
(void)env;
|
|
385
|
+
const char *name = xt_storage_cstr(xt_arg(argc, argv, 0));
|
|
386
|
+
xt_value req = idb_request_new();
|
|
387
|
+
xt_value captured[2] = {req, xt_string_from_cstr(name)};
|
|
388
|
+
xt_value task = xt_closure_new((void *)idb_delete_cb, 2, captured);
|
|
389
|
+
xt_value args[1] = {task};
|
|
390
|
+
xt_queue_microtask(1, args);
|
|
391
|
+
return req;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/* -- the global ----------------------------------------------------------- */
|
|
395
|
+
|
|
396
|
+
static xt_value g_indexed_db = XT_UNDEFINED;
|
|
397
|
+
|
|
398
|
+
xt_value xt_storage_indexed_db(int32_t argc, xt_value *argv) {
|
|
399
|
+
(void)argc;
|
|
400
|
+
(void)argv;
|
|
401
|
+
if (!XT_IS_OBJECT(g_indexed_db)) {
|
|
402
|
+
g_indexed_db = xt_object_new();
|
|
403
|
+
xt_storage_method(g_indexed_db, "open", (void *)idb_open);
|
|
404
|
+
xt_storage_method(g_indexed_db, "deleteDatabase", (void *)idb_delete_database);
|
|
405
|
+
xt_gc_add_root(&g_indexed_db);
|
|
406
|
+
}
|
|
407
|
+
return g_indexed_db;
|
|
408
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Internal declarations shared between the IndexedDB translation units.
|
|
3
|
+
*
|
|
4
|
+
* `indexed_db.c` owns the request plumbing, JSON helpers, database/transaction
|
|
5
|
+
* handles and the open/delete lifecycle; `indexed_db_store.c` owns the object
|
|
6
|
+
* store operations (`put`, `get`, `getAll`, …) and builds the store handle.
|
|
7
|
+
*/
|
|
8
|
+
#ifndef XT_STORAGE_INDEXED_DB_INTERNAL_H
|
|
9
|
+
#define XT_STORAGE_INDEXED_DB_INTERNAL_H
|
|
10
|
+
|
|
11
|
+
#include "storage_internal.h"
|
|
12
|
+
|
|
13
|
+
/* A fresh IDBRequest with its result/error/readyState initialised. */
|
|
14
|
+
xt_value idb_request_new(void);
|
|
15
|
+
/* Settle `req` from a microtask and invoke its `onsuccess`/`onerror`. */
|
|
16
|
+
void idb_complete(xt_value req, int success, xt_value value);
|
|
17
|
+
/* JSON stringify / parse used to serialise keys and values. */
|
|
18
|
+
xt_value idb_encode(xt_value value);
|
|
19
|
+
xt_value idb_decode(xt_value text);
|
|
20
|
+
/* Read the keyPath / autoIncrement of a store (NULL keyPath + 0 when absent). */
|
|
21
|
+
void idb_store_meta(sqlite3 *db, const char *name, const char *store, xt_value *key_path,
|
|
22
|
+
int *auto_increment);
|
|
23
|
+
/* Build an IDBObjectStore handle (defined in indexed_db_store.c). */
|
|
24
|
+
xt_value idb_store_build(const char *dbname, const char *store);
|
|
25
|
+
|
|
26
|
+
#endif /* XT_STORAGE_INDEXED_DB_INTERNAL_H */
|