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.
Files changed (224) hide show
  1. package/AGENTS.md +5 -0
  2. package/dist/src/cli/help.d.ts +1 -1
  3. package/dist/src/cli/main.js +1 -0
  4. package/dist/src/cli/main.js.map +1 -1
  5. package/dist/src/driver/build-key.d.ts +3 -0
  6. package/dist/src/driver/build-key.js +5 -2
  7. package/dist/src/driver/build-key.js.map +1 -1
  8. package/dist/src/driver/compiler.js +11 -0
  9. package/dist/src/driver/compiler.js.map +1 -1
  10. package/dist/src/driver/config.d.ts +12 -0
  11. package/dist/src/driver/config.js +22 -0
  12. package/dist/src/driver/config.js.map +1 -1
  13. package/dist/src/driver/fingerprint.d.ts +11 -0
  14. package/dist/src/driver/fingerprint.js +25 -0
  15. package/dist/src/driver/fingerprint.js.map +1 -1
  16. package/dist/src/driver/index.d.ts +2 -1
  17. package/dist/src/driver/index.js +1 -0
  18. package/dist/src/driver/index.js.map +1 -1
  19. package/dist/src/driver/link-features.js +36 -11
  20. package/dist/src/driver/link-features.js.map +1 -1
  21. package/dist/src/driver/options.d.ts +8 -1
  22. package/dist/src/driver/paths.d.ts +10 -0
  23. package/dist/src/driver/paths.js +18 -0
  24. package/dist/src/driver/paths.js.map +1 -1
  25. package/dist/src/driver/runtime-objects.d.ts +1 -1
  26. package/dist/src/driver/runtime-objects.js +22 -6
  27. package/dist/src/driver/runtime-objects.js.map +1 -1
  28. package/dist/src/driver/storage.d.ts +34 -0
  29. package/dist/src/driver/storage.js +67 -0
  30. package/dist/src/driver/storage.js.map +1 -0
  31. package/dist/src/driver/toolchain.d.ts +1 -1
  32. package/dist/src/driver/toolchain.js +10 -1
  33. package/dist/src/driver/toolchain.js.map +1 -1
  34. package/dist/src/driver/version.d.ts +1 -1
  35. package/dist/src/driver/version.js +1 -1
  36. package/dist/src/extensions/gui/features.d.ts +1 -1
  37. package/dist/src/extensions/gui/features.js +2 -2
  38. package/dist/src/extensions/gui/features.js.map +1 -1
  39. package/dist/src/extensions/gui/index.js +3 -1
  40. package/dist/src/extensions/gui/index.js.map +1 -1
  41. package/dist/src/extensions/node/http2/feature.d.ts +14 -0
  42. package/dist/src/extensions/node/http2/feature.js +28 -0
  43. package/dist/src/extensions/node/http2/feature.js.map +1 -0
  44. package/dist/src/extensions/node/http2/index.d.ts +5 -1
  45. package/dist/src/extensions/node/http2/index.js +6 -1
  46. package/dist/src/extensions/node/http2/index.js.map +1 -1
  47. package/dist/src/extensions/node/index.js +3 -1
  48. package/dist/src/extensions/node/index.js.map +1 -1
  49. package/dist/src/extensions/node/tls/feature.d.ts +12 -0
  50. package/dist/src/extensions/node/tls/feature.js +24 -0
  51. package/dist/src/extensions/node/tls/feature.js.map +1 -0
  52. package/dist/src/extensions/node/tls/index.d.ts +5 -1
  53. package/dist/src/extensions/node/tls/index.js +5 -1
  54. package/dist/src/extensions/node/tls/index.js.map +1 -1
  55. package/dist/src/extensions/node/zlib/compression.js +11 -6
  56. package/dist/src/extensions/node/zlib/compression.js.map +1 -1
  57. package/dist/src/extensions/registry.d.ts +4 -1
  58. package/dist/src/extensions/registry.js +9 -2
  59. package/dist/src/extensions/registry.js.map +1 -1
  60. package/dist/src/extensions/storage.d.ts +22 -0
  61. package/dist/src/extensions/storage.js +51 -0
  62. package/dist/src/extensions/storage.js.map +1 -0
  63. package/dist/src/extensions/tls.d.ts +26 -0
  64. package/dist/src/extensions/tls.js +46 -0
  65. package/dist/src/extensions/tls.js.map +1 -0
  66. package/dist/tests/driver/config.test.js +31 -0
  67. package/dist/tests/driver/config.test.js.map +1 -1
  68. package/dist/tests/driver/link-features.test.js +13 -2
  69. package/dist/tests/driver/link-features.test.js.map +1 -1
  70. package/dist/tests/driver/storage.test.d.ts +1 -0
  71. package/dist/tests/driver/storage.test.js +29 -0
  72. package/dist/tests/driver/storage.test.js.map +1 -0
  73. package/dist/tests/e2e/fetch.test.js +3 -1
  74. package/dist/tests/e2e/fetch.test.js.map +1 -1
  75. package/dist/tests/e2e/http2.test.d.ts +7 -0
  76. package/dist/tests/e2e/http2.test.js +201 -0
  77. package/dist/tests/e2e/http2.test.js.map +1 -0
  78. package/dist/tests/e2e/net-helpers.d.ts +28 -0
  79. package/dist/tests/e2e/net-helpers.js +59 -0
  80. package/dist/tests/e2e/net-helpers.js.map +1 -0
  81. package/dist/tests/e2e/node-modules.test.js +3 -5
  82. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  83. package/dist/tests/e2e/storage.test.d.ts +7 -0
  84. package/dist/tests/e2e/storage.test.js +86 -0
  85. package/dist/tests/e2e/storage.test.js.map +1 -0
  86. package/dist/tests/e2e/tls.test.d.ts +10 -0
  87. package/dist/tests/e2e/tls.test.js +197 -0
  88. package/dist/tests/e2e/tls.test.js.map +1 -0
  89. package/dist/tests/extensions/gui-features.test.js +1 -1
  90. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  91. package/dist/tests/extensions/storage.test.d.ts +1 -0
  92. package/dist/tests/extensions/storage.test.js +51 -0
  93. package/dist/tests/extensions/storage.test.js.map +1 -0
  94. package/doc/ai/cli.md +2 -0
  95. package/doc/ai/contributing.md +14 -0
  96. package/doc/ai/extensions.md +31 -3
  97. package/doc/ai/language-support.md +1 -1
  98. package/doc/ai/zh-CN/cli.md +2 -0
  99. package/doc/ai/zh-CN/contributing.md +12 -0
  100. package/doc/ai/zh-CN/extensions.md +27 -2
  101. package/doc/ai/zh-CN/language-support.md +1 -1
  102. package/doc/gui/api.md +1 -1
  103. package/doc/gui/build.md +7 -5
  104. package/doc/gui/media.md +4 -4
  105. package/doc/gui/rendering.md +1 -1
  106. package/doc/gui.md +3 -3
  107. package/doc/implemented/async.md +1 -1
  108. package/doc/implemented/extensions.md +2 -1
  109. package/doc/implemented/fetch.md +19 -4
  110. package/doc/implemented/node/http2.md +51 -0
  111. package/doc/implemented/node/tls.md +56 -0
  112. package/doc/implemented/node.md +8 -0
  113. package/doc/implemented/storage.md +65 -0
  114. package/doc/implemented.md +4 -2
  115. package/doc/unimplemented/node/modules.md +6 -6
  116. package/doc/unimplemented/toolchain.md +1 -1
  117. package/doc/xbintsc.config.schema.json +16 -1
  118. package/doc/zh-CN/gui/api.md +1 -1
  119. package/doc/zh-CN/gui/build.md +6 -4
  120. package/doc/zh-CN/gui/media.md +3 -3
  121. package/doc/zh-CN/gui/rendering.md +1 -1
  122. package/doc/zh-CN/gui.md +3 -3
  123. package/doc/zh-CN/implemented/async.md +1 -1
  124. package/doc/zh-CN/implemented/extensions.md +2 -1
  125. package/doc/zh-CN/implemented/fetch.md +15 -3
  126. package/doc/zh-CN/implemented/node/http2.md +44 -0
  127. package/doc/zh-CN/implemented/node/tls.md +47 -0
  128. package/doc/zh-CN/implemented/node.md +7 -0
  129. package/doc/zh-CN/implemented/storage.md +52 -0
  130. package/doc/zh-CN/implemented.md +4 -2
  131. package/doc/zh-CN/unimplemented/node/modules.md +3 -3
  132. package/doc/zh-CN/unimplemented/toolchain.md +1 -1
  133. package/package.json +16 -8
  134. package/runtime/ext_gui/dom_api_form.cpp +10 -0
  135. package/runtime/ext_gui/gui_media.cpp +1 -1
  136. package/runtime/ext_gui/media.cpp +1 -1
  137. package/runtime/ext_gui/media_api.h +2 -2
  138. package/runtime/ext_node/http2/http2.c +25 -11
  139. package/runtime/ext_node/http2/http2_backend.c +48 -0
  140. package/runtime/ext_node/http2/http2_bridge.c +25 -0
  141. package/runtime/ext_node/http2/parts/api.inc +40 -0
  142. package/runtime/ext_node/http2/parts/client.inc +215 -0
  143. package/runtime/ext_node/http2/parts/common.inc +330 -0
  144. package/runtime/ext_node/http2/parts/events.inc +139 -0
  145. package/runtime/ext_node/http2/parts/server.inc +301 -0
  146. package/runtime/ext_node/http2/parts/streams.inc +254 -0
  147. package/runtime/ext_node/tls/parts/api.inc +38 -0
  148. package/runtime/ext_node/tls/parts/connect.inc +107 -0
  149. package/runtime/ext_node/tls/parts/helpers.inc +121 -0
  150. package/runtime/ext_node/tls/parts/server.inc +101 -0
  151. package/runtime/ext_node/tls/parts/socket.inc +171 -0
  152. package/runtime/ext_node/tls/tls.c +21 -13
  153. package/runtime/ext_node/tls/tls_backend.c +50 -0
  154. package/runtime/ext_node/zlib/{miniz/compression_bridge.c → compression_bridge.c} +1 -1
  155. package/runtime/xt_fetch/async.inc +19 -12
  156. package/runtime/xt_fetch/socket.inc +55 -0
  157. package/runtime/xt_fetch.c +3 -0
  158. package/runtime/xt_http2/nghttp2_compat.h +19 -0
  159. package/runtime/xt_http2/nghttp2_static.h +18 -0
  160. package/runtime/xt_http2_api.h +38 -0
  161. package/runtime/xt_node_tls_api.h +37 -0
  162. package/runtime/xt_protocol_registry.c +22 -0
  163. package/runtime/xt_storage/indexed_db.c +408 -0
  164. package/runtime/xt_storage/indexed_db_internal.h +26 -0
  165. package/runtime/xt_storage/indexed_db_store.c +305 -0
  166. package/runtime/xt_storage/storage.c +209 -0
  167. package/runtime/xt_storage/storage_internal.h +66 -0
  168. package/runtime/xt_tls/parts/api.inc +21 -0
  169. package/runtime/xt_tls/parts/client.inc +112 -0
  170. package/runtime/xt_tls/parts/io.inc +157 -0
  171. package/runtime/xt_tls/parts/server.inc +102 -0
  172. package/runtime/xt_tls/parts/setup.inc +146 -0
  173. package/runtime/xt_tls/tls_bridge.c +27 -0
  174. package/runtime/xt_tls/tls_core.c +39 -0
  175. package/runtime/xt_tls/tls_internal.h +73 -0
  176. package/runtime/xt_tls_api.h +73 -0
  177. package/scripts/build-gui.ts +9 -8
  178. package/scripts/build-media.ts +2 -2
  179. package/scripts/build-tls.ts +248 -0
  180. package/scripts/check-file-length.ts +4 -21
  181. package/scripts/fetch-vendored.ts +244 -0
  182. package/scripts/package-release.ts +13 -3
  183. package/src/cli/main.ts +1 -0
  184. package/src/driver/build-key.ts +13 -2
  185. package/src/driver/compiler.ts +13 -0
  186. package/src/driver/config.ts +35 -0
  187. package/src/driver/fingerprint.ts +24 -0
  188. package/src/driver/index.ts +8 -0
  189. package/src/driver/link-features.ts +33 -11
  190. package/src/driver/options.ts +8 -1
  191. package/src/driver/paths.ts +20 -0
  192. package/src/driver/runtime-objects.ts +25 -6
  193. package/src/driver/storage.ts +84 -0
  194. package/src/driver/toolchain.ts +11 -2
  195. package/src/driver/version.ts +1 -1
  196. package/src/extensions/gui/features.ts +2 -2
  197. package/src/extensions/gui/index.ts +3 -1
  198. package/src/extensions/node/http2/feature.ts +33 -0
  199. package/src/extensions/node/http2/index.ts +6 -1
  200. package/src/extensions/node/index.ts +3 -1
  201. package/src/extensions/node/tls/feature.ts +29 -0
  202. package/src/extensions/node/tls/index.ts +5 -1
  203. package/src/extensions/node/zlib/compression.ts +11 -6
  204. package/src/extensions/registry.ts +9 -2
  205. package/src/extensions/storage.ts +58 -0
  206. package/src/extensions/tls.ts +52 -0
  207. package/vendor/sqlite3/sqlite3.c +271131 -0
  208. package/vendor/sqlite3/sqlite3.h +14447 -0
  209. package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
  210. /package/runtime/{ext_media → xt_media}/decode_audio.cpp +0 -0
  211. /package/runtime/{ext_media → xt_media}/media.cpp +0 -0
  212. /package/runtime/{ext_media → xt_media}/media_bridge.c +0 -0
  213. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.c +0 -0
  214. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.h +0 -0
  215. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_common.h +0 -0
  216. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_export.h +0 -0
  217. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.c +0 -0
  218. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.h +0 -0
  219. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.c +0 -0
  220. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.h +0 -0
  221. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_zip.h +0 -0
  222. /package/{runtime/ext_gui → vendor/nanosvg}/nanosvg.h +0 -0
  223. /package/{runtime/ext_gui → vendor/nanosvg}/nanosvgrast.h +0 -0
  224. /package/{runtime/ext_gui → vendor/stb}/stb_truetype.h +0 -0
@@ -71,3 +71,58 @@ static int xt_fetch_recv(int fd, char *buffer, size_t capacity) {
71
71
  return count > 2147483647 ? 2147483647 : (int)count;
72
72
  #endif
73
73
  }
74
+
75
+ /* -- transport abstraction (plain TCP or TLS) ------------------------------ */
76
+
77
+ /*
78
+ * The worker is the same for both schemes: only connect/write/read/close
79
+ * differ. TLS is reached through the optional `tls` link feature's dispatch
80
+ * table, which is NULL in a program that never enables it; an `https://`
81
+ * request then rejects instead of failing to link.
82
+ */
83
+ typedef struct {
84
+ int fd;
85
+ xt_tls_conn *tls;
86
+ } xt_fetch_stream;
87
+
88
+ static int xt_fetch_stream_open(const xt_fetch_url *url, xt_fetch_stream *stream, char *err,
89
+ size_t errlen) {
90
+ stream->fd = -1;
91
+ stream->tls = NULL;
92
+ if (!url->secure) {
93
+ stream->fd = xt_fetch_connect(url);
94
+ return stream->fd < 0 ? -1 : 0;
95
+ }
96
+ if (xt_tls_core_api == NULL) {
97
+ snprintf(err, errlen, "https is not supported (the tls link feature is not linked)");
98
+ return -1;
99
+ }
100
+ int port = url->port[0] != '\0' ? atoi(url->port) : 443;
101
+ stream->tls = xt_tls_core_api->client_connect(url->host, port, url->host, 1, NULL, NULL, NULL,
102
+ "http/1.1", err, errlen);
103
+ if (stream->tls == NULL) return -1;
104
+ stream->fd = xt_tls_core_api->fd(stream->tls);
105
+ return 0;
106
+ }
107
+
108
+ static int xt_fetch_stream_send(xt_fetch_stream *stream, const char *data, size_t length) {
109
+ if (stream->tls != NULL) return xt_tls_core_api->write(stream->tls, (const unsigned char *)data, length);
110
+ return xt_fetch_send_all(stream->fd, data, length);
111
+ }
112
+
113
+ static int xt_fetch_stream_recv(xt_fetch_stream *stream, char *buffer, size_t capacity) {
114
+ if (stream->tls != NULL) {
115
+ return xt_tls_core_api->read(stream->tls, (unsigned char *)buffer, capacity);
116
+ }
117
+ return xt_fetch_recv(stream->fd, buffer, capacity);
118
+ }
119
+
120
+ static void xt_fetch_stream_close(xt_fetch_stream *stream) {
121
+ if (stream->tls != NULL) {
122
+ xt_tls_core_api->close(stream->tls);
123
+ stream->tls = NULL;
124
+ } else if (stream->fd >= 0) {
125
+ xt_fetch_close_socket(stream->fd);
126
+ }
127
+ stream->fd = -1;
128
+ }
@@ -14,6 +14,9 @@
14
14
 
15
15
  #include "rt_internal.h"
16
16
 
17
+ /* The optional mbedTLS backend's dispatch table (`tls` link feature). */
18
+ #include "xt_tls_api.h"
19
+
17
20
  #include <stdio.h>
18
21
  #include <stdlib.h>
19
22
  #include <string.h>
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Forced-include shim for the vendored nghttp2 build.
3
+ *
4
+ * nghttp2's public header uses POSIX `ssize_t`, which the MSVC/UCRT headers
5
+ * clang targets on Windows do not define. The xbintsc runtime header does
6
+ * define it (`rt.h`), but nghttp2's own translation units do not include that
7
+ * header, so the archive build passes this file with `-include`. The guards
8
+ * make it a no-op on POSIX and when `rt.h` was already seen.
9
+ */
10
+ #ifndef XT_NGHTTP2_COMPAT_H
11
+ #define XT_NGHTTP2_COMPAT_H
12
+
13
+ #if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
14
+ #include <stdint.h>
15
+ typedef intptr_t ssize_t;
16
+ #define _SSIZE_T_DEFINED
17
+ #endif
18
+
19
+ #endif /* XT_NGHTTP2_COMPAT_H */
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Include nghttp2 as a static library.
3
+ *
4
+ * The archive is linked directly into the executable, so the public header must
5
+ * not decorate its declarations with `__declspec(dllimport)` on Windows. The
6
+ * vendored library is built with `NGHTTP2_STATICLIB`; consumers must define it
7
+ * before including `<nghttp2/nghttp2.h>` too.
8
+ */
9
+ #ifndef XT_NGHTTP2_STATIC_H
10
+ #define XT_NGHTTP2_STATIC_H
11
+
12
+ #ifndef NGHTTP2_STATICLIB
13
+ #define NGHTTP2_STATICLIB 1
14
+ #endif
15
+
16
+ #include <nghttp2/nghttp2.h>
17
+
18
+ #endif /* XT_NGHTTP2_STATIC_H */
@@ -0,0 +1,38 @@
1
+ /*
2
+ * High-level `node:http2` surface provided by the `http2` link feature.
3
+ *
4
+ * Same shape as `xt_node_tls_api.h`: the always-linked `http2` module keeps the
5
+ * importable bindings and forwards to this table, which the nghttp2-backed
6
+ * feature installs in a constructor.
7
+ */
8
+
9
+ #ifndef XT_HTTP2_API_H
10
+ #define XT_HTTP2_API_H
11
+
12
+ #include "rt.h"
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ typedef struct xt_http2_api {
19
+ /** `http2.connect(authority[, options][, listener])`. */
20
+ xt_value (*connect)(int32_t argc, xt_value *argv);
21
+ /** `http2.createServer([options][, onRequest])` (cleartext h2c). */
22
+ xt_value (*create_server)(int32_t argc, xt_value *argv);
23
+ /** `http2.createSecureServer(options[, onRequest])` (TLS + ALPN h2). */
24
+ xt_value (*create_secure_server)(int32_t argc, xt_value *argv);
25
+ /** `http2.constants` (frame/error code numbers). */
26
+ xt_value (*constants)(void);
27
+ } XtHttp2Api;
28
+
29
+ /** The installed backend, or NULL when the `http2` feature is not linked. */
30
+ extern const XtHttp2Api *xt_http2_api;
31
+
32
+ void xt_http2_register(const XtHttp2Api *api);
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
37
+
38
+ #endif /* XT_HTTP2_API_H */
@@ -0,0 +1,37 @@
1
+ /*
2
+ * High-level `node:tls` surface provided by the `node-tls` link feature.
3
+ *
4
+ * The always-linked `tls` module keeps the importable bindings and falls back to
5
+ * a clear runtime error when the feature is absent. The feature's backend owns
6
+ * the JavaScript objects (`TLSSocket`, the server wrapper) and registers them
7
+ * here in a constructor, mirroring the `zlib` compression bridge.
8
+ */
9
+
10
+ #ifndef XT_NODE_TLS_API_H
11
+ #define XT_NODE_TLS_API_H
12
+
13
+ #include "rt.h"
14
+
15
+ #ifdef __cplusplus
16
+ extern "C" {
17
+ #endif
18
+
19
+ typedef struct xt_node_tls_api {
20
+ /** `tls.connect(options[, host][, callback])`. */
21
+ xt_value (*connect)(int32_t argc, xt_value *argv);
22
+ /** `tls.createServer(options[, secureConnectionListener])`. */
23
+ xt_value (*create_server)(int32_t argc, xt_value *argv);
24
+ /** `new tls.TLSSocket(...)`. */
25
+ xt_value (*socket_ctor)(int32_t argc, xt_value *argv);
26
+ } XtNodeTlsApi;
27
+
28
+ /** The installed backend, or NULL when the `node-tls` feature is not linked. */
29
+ extern const XtNodeTlsApi *xt_tls_node_api;
30
+
31
+ void xt_node_tls_register(const XtNodeTlsApi *api);
32
+
33
+ #ifdef __cplusplus
34
+ }
35
+ #endif
36
+
37
+ #endif /* XT_NODE_TLS_API_H */
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Registration slots for the optional TLS and HTTP/2 backends.
3
+ *
4
+ * These globals live in the core runtime so a program that never enables the
5
+ * `tls`/`http2` link features still links: the fallbacks (rejecting `https://`,
6
+ * throwing from `node:tls`) only need the NULL check. The feature
7
+ * implementations install their tables from a constructor before `main`.
8
+ */
9
+
10
+ #include "xt_http2_api.h"
11
+ #include "xt_node_tls_api.h"
12
+ #include "xt_tls_api.h"
13
+
14
+ const XtTlsApi *xt_tls_core_api = NULL;
15
+ const XtNodeTlsApi *xt_tls_node_api = NULL;
16
+ const XtHttp2Api *xt_http2_api = NULL;
17
+
18
+ void xt_tls_core_register(const XtTlsApi *api) { xt_tls_core_api = api; }
19
+
20
+ void xt_node_tls_register(const XtNodeTlsApi *api) { xt_tls_node_api = api; }
21
+
22
+ void xt_http2_register(const XtHttp2Api *api) { xt_http2_api = api; }
@@ -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 */