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.
Files changed (93) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/cli/main.js +1 -0
  3. package/dist/src/cli/main.js.map +1 -1
  4. package/dist/src/driver/build-key.d.ts +3 -0
  5. package/dist/src/driver/build-key.js +3 -1
  6. package/dist/src/driver/build-key.js.map +1 -1
  7. package/dist/src/driver/compiler.js +11 -0
  8. package/dist/src/driver/compiler.js.map +1 -1
  9. package/dist/src/driver/config.d.ts +12 -0
  10. package/dist/src/driver/config.js +22 -0
  11. package/dist/src/driver/config.js.map +1 -1
  12. package/dist/src/driver/index.d.ts +2 -1
  13. package/dist/src/driver/index.js +1 -0
  14. package/dist/src/driver/index.js.map +1 -1
  15. package/dist/src/driver/options.d.ts +8 -1
  16. package/dist/src/driver/storage.d.ts +34 -0
  17. package/dist/src/driver/storage.js +67 -0
  18. package/dist/src/driver/storage.js.map +1 -0
  19. package/dist/src/driver/version.d.ts +1 -1
  20. package/dist/src/driver/version.js +1 -1
  21. package/dist/src/extensions/gui/features.d.ts +1 -1
  22. package/dist/src/extensions/gui/features.js +2 -2
  23. package/dist/src/extensions/gui/features.js.map +1 -1
  24. package/dist/src/extensions/gui/index.js +3 -1
  25. package/dist/src/extensions/gui/index.js.map +1 -1
  26. package/dist/src/extensions/registry.d.ts +4 -1
  27. package/dist/src/extensions/registry.js +8 -2
  28. package/dist/src/extensions/registry.js.map +1 -1
  29. package/dist/src/extensions/storage.d.ts +22 -0
  30. package/dist/src/extensions/storage.js +50 -0
  31. package/dist/src/extensions/storage.js.map +1 -0
  32. package/dist/tests/driver/config.test.js +31 -0
  33. package/dist/tests/driver/config.test.js.map +1 -1
  34. package/dist/tests/driver/link-features.test.js +4 -2
  35. package/dist/tests/driver/link-features.test.js.map +1 -1
  36. package/dist/tests/driver/storage.test.d.ts +1 -0
  37. package/dist/tests/driver/storage.test.js +29 -0
  38. package/dist/tests/driver/storage.test.js.map +1 -0
  39. package/dist/tests/e2e/storage.test.d.ts +7 -0
  40. package/dist/tests/e2e/storage.test.js +86 -0
  41. package/dist/tests/e2e/storage.test.js.map +1 -0
  42. package/dist/tests/extensions/gui-features.test.js +1 -1
  43. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  44. package/dist/tests/extensions/storage.test.d.ts +1 -0
  45. package/dist/tests/extensions/storage.test.js +50 -0
  46. package/dist/tests/extensions/storage.test.js.map +1 -0
  47. package/doc/ai/cli.md +2 -0
  48. package/doc/ai/extensions.md +26 -0
  49. package/doc/ai/zh-CN/cli.md +2 -0
  50. package/doc/ai/zh-CN/extensions.md +23 -0
  51. package/doc/gui/build.md +3 -3
  52. package/doc/gui/media.md +4 -4
  53. package/doc/gui.md +2 -2
  54. package/doc/implemented/extensions.md +1 -1
  55. package/doc/implemented/storage.md +64 -0
  56. package/doc/implemented.md +2 -0
  57. package/doc/xbintsc.config.schema.json +16 -1
  58. package/doc/zh-CN/gui/build.md +3 -3
  59. package/doc/zh-CN/gui/media.md +3 -3
  60. package/doc/zh-CN/gui.md +2 -2
  61. package/doc/zh-CN/implemented/extensions.md +1 -1
  62. package/doc/zh-CN/implemented/storage.md +51 -0
  63. package/doc/zh-CN/implemented.md +2 -0
  64. package/package.json +7 -7
  65. package/runtime/ext_gui/dom_api_form.cpp +10 -0
  66. package/runtime/ext_gui/gui_media.cpp +1 -1
  67. package/runtime/ext_gui/media.cpp +1 -1
  68. package/runtime/ext_gui/media_api.h +2 -2
  69. package/runtime/xt_storage/indexed_db.c +408 -0
  70. package/runtime/xt_storage/indexed_db_internal.h +26 -0
  71. package/runtime/xt_storage/indexed_db_store.c +305 -0
  72. package/runtime/xt_storage/sqlite3/sqlite3.c +271131 -0
  73. package/runtime/xt_storage/sqlite3/sqlite3.h +14447 -0
  74. package/runtime/xt_storage/storage.c +209 -0
  75. package/runtime/xt_storage/storage_internal.h +66 -0
  76. package/scripts/build-gui.ts +1 -1
  77. package/scripts/build-media.ts +2 -2
  78. package/scripts/check-file-length.ts +4 -0
  79. package/src/cli/main.ts +1 -0
  80. package/src/driver/build-key.ts +6 -1
  81. package/src/driver/compiler.ts +13 -0
  82. package/src/driver/config.ts +35 -0
  83. package/src/driver/index.ts +8 -0
  84. package/src/driver/options.ts +8 -1
  85. package/src/driver/storage.ts +84 -0
  86. package/src/driver/version.ts +1 -1
  87. package/src/extensions/gui/features.ts +2 -2
  88. package/src/extensions/gui/index.ts +3 -1
  89. package/src/extensions/registry.ts +8 -2
  90. package/src/extensions/storage.ts +57 -0
  91. /package/runtime/{ext_media → xt_media}/decode_audio.cpp +0 -0
  92. /package/runtime/{ext_media → xt_media}/media.cpp +0 -0
  93. /package/runtime/{ext_media → xt_media}/media_bridge.c +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
+ }