xbintsc 0.6.10 → 0.6.13

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 (204) hide show
  1. package/dist/src/ast/expressions.d.ts +6 -0
  2. package/dist/src/checker/globals.js +2 -1
  3. package/dist/src/checker/globals.js.map +1 -1
  4. package/dist/src/checker/type-infer.js +1 -1
  5. package/dist/src/checker/type-infer.js.map +1 -1
  6. package/dist/src/cli/help.d.ts +1 -1
  7. package/dist/src/codegen/generator/calls/literals.js +4 -1
  8. package/dist/src/codegen/generator/calls/literals.js.map +1 -1
  9. package/dist/src/codegen/generator/expressions/assignment.js +3 -1
  10. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  11. package/dist/src/codegen/generator/expressions/operators.js +2 -6
  12. package/dist/src/codegen/generator/expressions/operators.js.map +1 -1
  13. package/dist/src/codegen/generator/expressions/primary.d.ts +9 -0
  14. package/dist/src/codegen/generator/expressions/primary.js +44 -7
  15. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  16. package/dist/src/codegen/generator/state.js +3 -0
  17. package/dist/src/codegen/generator/state.js.map +1 -1
  18. package/dist/src/codegen/generator/tables.js +3 -0
  19. package/dist/src/codegen/generator/tables.js.map +1 -1
  20. package/dist/src/driver/bundler/commonjs.js +1 -1
  21. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  22. package/dist/src/driver/bundler/symbols.js +6 -0
  23. package/dist/src/driver/bundler/symbols.js.map +1 -1
  24. package/dist/src/driver/compiler.js +1 -1
  25. package/dist/src/driver/compiler.js.map +1 -1
  26. package/dist/src/driver/runtime-objects.d.ts +1 -1
  27. package/dist/src/driver/runtime-objects.js +2 -0
  28. package/dist/src/driver/runtime-objects.js.map +1 -1
  29. package/dist/src/driver/version.d.ts +1 -1
  30. package/dist/src/driver/version.js +1 -1
  31. package/dist/src/extensions/node/crypto/index.js +12 -1
  32. package/dist/src/extensions/node/crypto/index.js.map +1 -1
  33. package/dist/src/extensions/node/index.js +2 -0
  34. package/dist/src/extensions/node/index.js.map +1 -1
  35. package/dist/src/extensions/node/readline/index.d.ts +12 -0
  36. package/dist/src/extensions/node/readline/index.js +27 -0
  37. package/dist/src/extensions/node/readline/index.js.map +1 -0
  38. package/dist/src/parser/declarations/functions.js +6 -0
  39. package/dist/src/parser/declarations/functions.js.map +1 -1
  40. package/dist/src/parser/literals.js +1 -1
  41. package/dist/src/parser/literals.js.map +1 -1
  42. package/dist/tests/e2e/compile-builtins.test.js +26 -0
  43. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  44. package/dist/tests/e2e/compile-modules.test.js +22 -0
  45. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  46. package/dist/tests/e2e/compile-node.test.js +37 -1
  47. package/dist/tests/e2e/compile-node.test.js.map +1 -1
  48. package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
  49. package/dist/tests/e2e/crypto-random.test.js +63 -0
  50. package/dist/tests/e2e/crypto-random.test.js.map +1 -0
  51. package/dist/tests/e2e/date-parse.test.js +44 -1
  52. package/dist/tests/e2e/date-parse.test.js.map +1 -1
  53. package/dist/tests/e2e/gui-browser.test.js +61 -0
  54. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  55. package/dist/tests/e2e/gui-css-coverage.test.js +40 -1
  56. package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
  57. package/dist/tests/e2e/gui-css-features.test.js +100 -0
  58. package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
  59. package/dist/tests/e2e/gui-dom.test.js +63 -0
  60. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  61. package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
  62. package/dist/tests/e2e/gui-errors.test.js +68 -0
  63. package/dist/tests/e2e/gui-errors.test.js.map +1 -0
  64. package/dist/tests/e2e/gui-form.test.d.ts +9 -0
  65. package/dist/tests/e2e/gui-form.test.js +359 -0
  66. package/dist/tests/e2e/gui-form.test.js.map +1 -0
  67. package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
  68. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  69. package/dist/tests/e2e/gui-style.test.js +82 -0
  70. package/dist/tests/e2e/gui-style.test.js.map +1 -1
  71. package/dist/tests/e2e/gui-unsupported.test.js +4 -3
  72. package/dist/tests/e2e/gui-unsupported.test.js.map +1 -1
  73. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +7 -1
  74. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -1
  75. package/dist/tests/e2e/harness.d.ts +4 -0
  76. package/dist/tests/e2e/harness.js +2 -0
  77. package/dist/tests/e2e/harness.js.map +1 -1
  78. package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
  79. package/dist/tests/e2e/json-parse-errors.test.js +48 -0
  80. package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
  81. package/dist/tests/e2e/readline.test.d.ts +7 -0
  82. package/dist/tests/e2e/readline.test.js +73 -0
  83. package/dist/tests/e2e/readline.test.js.map +1 -0
  84. package/doc/ai/language-support.md +2 -1
  85. package/doc/ai/zh-CN/language-support.md +2 -1
  86. package/doc/gui/api.md +24 -3
  87. package/doc/gui/dom.md +28 -14
  88. package/doc/gui/input.md +74 -3
  89. package/doc/gui/known-issues.md +2 -1
  90. package/doc/gui/layout.md +5 -1
  91. package/doc/implemented/node/crypto.md +8 -0
  92. package/doc/implemented/node/fs.md +1 -1
  93. package/doc/implemented/node/readline.md +34 -0
  94. package/doc/implemented/node.md +1 -0
  95. package/doc/implemented/stdlib.md +10 -2
  96. package/doc/unimplemented/node/modules.md +2 -3
  97. package/doc/unimplemented/node.md +2 -2
  98. package/doc/zh-CN/gui/api.md +20 -3
  99. package/doc/zh-CN/gui/dom.md +23 -13
  100. package/doc/zh-CN/gui/input.md +65 -4
  101. package/doc/zh-CN/gui/known-issues.md +2 -1
  102. package/doc/zh-CN/gui/layout.md +4 -1
  103. package/doc/zh-CN/implemented/node/crypto.md +6 -0
  104. package/doc/zh-CN/implemented/node/fs.md +1 -1
  105. package/doc/zh-CN/implemented/node/readline.md +33 -0
  106. package/doc/zh-CN/implemented/node.md +1 -0
  107. package/doc/zh-CN/implemented/stdlib.md +10 -2
  108. package/doc/zh-CN/unimplemented/node/modules.md +1 -2
  109. package/doc/zh-CN/unimplemented/node.md +2 -2
  110. package/package.json +7 -7
  111. package/runtime/ext_gui/browser_abort.cpp +2 -2
  112. package/runtime/ext_gui/browser_loop.cpp +1 -0
  113. package/runtime/ext_gui/browser_media.cpp +1 -1
  114. package/runtime/ext_gui/css_match.cpp +21 -1
  115. package/runtime/ext_gui/css_selector.cpp +2 -2
  116. package/runtime/ext_gui/document.cpp +12 -0
  117. package/runtime/ext_gui/dom.h +41 -2
  118. package/runtime/ext_gui/dom_api_canvas_image.cpp +2 -1
  119. package/runtime/ext_gui/dom_api_document.cpp +28 -0
  120. package/runtime/ext_gui/dom_api_events.cpp +18 -15
  121. package/runtime/ext_gui/dom_api_form.cpp +567 -0
  122. package/runtime/ext_gui/dom_api_internal.h +48 -214
  123. package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
  124. package/runtime/ext_gui/dom_api_node.cpp +28 -15
  125. package/runtime/ext_gui/dom_api_node_read.cpp +28 -1
  126. package/runtime/ext_gui/dom_api_proto.cpp +47 -0
  127. package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
  128. package/runtime/ext_gui/dom_parse.cpp +114 -1
  129. package/runtime/ext_gui/error_report.cpp +166 -0
  130. package/runtime/ext_gui/form_focus.cpp +293 -0
  131. package/runtime/ext_gui/form_input.cpp +448 -0
  132. package/runtime/ext_gui/form_internal.h +30 -0
  133. package/runtime/ext_gui/form_shared.cpp +126 -0
  134. package/runtime/ext_gui/form_state.cpp +536 -0
  135. package/runtime/ext_gui/form_state.h +97 -0
  136. package/runtime/ext_gui/gui.cpp +16 -0
  137. package/runtime/ext_gui/gui_engine.h +53 -4
  138. package/runtime/ext_gui/gui_events.cpp +67 -49
  139. package/runtime/ext_gui/layout.cpp +6 -0
  140. package/runtime/ext_gui/layout.h +4 -0
  141. package/runtime/ext_gui/layout_flow.cpp +23 -1
  142. package/runtime/ext_gui/layout_inline.cpp +46 -14
  143. package/runtime/ext_gui/paint.cpp +26 -1
  144. package/runtime/ext_gui/paint_form.cpp +389 -0
  145. package/runtime/ext_gui/paint_internal.h +6 -0
  146. package/runtime/ext_gui/script.cpp +1 -1
  147. package/runtime/ext_gui/style.cpp +1 -0
  148. package/runtime/ext_gui/style.h +13 -0
  149. package/runtime/ext_gui/style_print.cpp +129 -0
  150. package/runtime/ext_gui/style_text.cpp +79 -0
  151. package/runtime/ext_gui/style_ua.cpp +42 -0
  152. package/runtime/ext_gui/warnings.cpp +5 -7
  153. package/runtime/ext_gui/webgl_texture.cpp +0 -8
  154. package/runtime/ext_gui/window.cpp +52 -62
  155. package/runtime/ext_gui/window_diagnostics.cpp +58 -0
  156. package/runtime/ext_gui/window_emit.cpp +51 -0
  157. package/runtime/ext_node/crypto/crypto.c +3 -2
  158. package/runtime/ext_node/crypto/crypto_hash.h +13 -0
  159. package/runtime/ext_node/crypto/crypto_random.c +339 -0
  160. package/runtime/ext_node/fs/async_worker.c +1 -1
  161. package/runtime/ext_node/fs/fs_ops.c +1 -1
  162. package/runtime/ext_node/fs/parts/platform.inc +67 -0
  163. package/runtime/ext_node/readline/readline.c +572 -0
  164. package/runtime/rt.h +42 -0
  165. package/runtime/rt_internal.h +3 -2
  166. package/runtime/xt_crypto.c +132 -0
  167. package/runtime/xt_io/exception.inc +29 -6
  168. package/runtime/xt_loop.c +36 -1
  169. package/runtime/xt_promise.c +26 -2
  170. package/runtime/xt_random.c +143 -0
  171. package/runtime/xt_regex/matcher.inc +9 -28
  172. package/runtime/xt_regex/parser.inc +65 -2
  173. package/runtime/xt_regex/types.inc +11 -1
  174. package/runtime/xt_regex.h +6 -2
  175. package/runtime/xt_stdlib/dispatch.inc +2 -0
  176. package/runtime/xt_stdlib/math.inc +1 -1
  177. package/runtime/xt_stdlib2/date.inc +206 -6
  178. package/runtime/xt_stdlib2/json.inc +4 -546
  179. package/runtime/xt_stdlib2/json_parse.inc +347 -0
  180. package/runtime/xt_stdlib2/json_stringify.inc +368 -0
  181. package/runtime/xt_stdlib2/regexp-match.inc +6 -11
  182. package/runtime/xt_stdlib2.c +0 -4
  183. package/runtime/xt_typed_array/elements.inc +2 -6
  184. package/runtime/xt_url.c +0 -6
  185. package/runtime/xt_url_object.c +1 -1
  186. package/src/ast/expressions.ts +6 -0
  187. package/src/checker/globals.ts +2 -0
  188. package/src/checker/type-infer.ts +1 -1
  189. package/src/codegen/generator/calls/literals.ts +4 -1
  190. package/src/codegen/generator/expressions/assignment.ts +3 -1
  191. package/src/codegen/generator/expressions/operators.ts +2 -6
  192. package/src/codegen/generator/expressions/primary.ts +58 -6
  193. package/src/codegen/generator/state.ts +3 -0
  194. package/src/codegen/generator/tables.ts +3 -0
  195. package/src/driver/bundler/commonjs.ts +1 -1
  196. package/src/driver/bundler/symbols.ts +6 -0
  197. package/src/driver/compiler.ts +1 -1
  198. package/src/driver/runtime-objects.ts +2 -0
  199. package/src/driver/version.ts +1 -1
  200. package/src/extensions/node/crypto/index.ts +12 -1
  201. package/src/extensions/node/index.ts +2 -0
  202. package/src/extensions/node/readline/index.ts +31 -0
  203. package/src/parser/declarations/functions.ts +6 -0
  204. package/src/parser/literals.ts +1 -1
@@ -0,0 +1,572 @@
1
+ /*
2
+ * Node.js `readline` for xbintsc.
3
+ *
4
+ * A line-oriented reader over the process's standard input. Readline is
5
+ * inherently asynchronous (it waits for the user while other timers keep
6
+ * running), so the blocking byte read runs on the runtime's async I/O pool and
7
+ * its completion delivers `line`/`close` events on the main thread. One read is
8
+ * in flight per interface; `pause()` buffers a line that arrives while paused
9
+ * and `resume()` drains the buffer before reading again.
10
+ *
11
+ * Supported: `createInterface`, `Interface`, `question` (queued, one answer per
12
+ * call), `on`/`once`/`off`/`removeListener`/`removeAllListeners` for
13
+ * `line`/`close`/`pause`/`resume`, `prompt`/`setPrompt`/`getPrompt`, `close`,
14
+ * `pause`, `resume`, `write`, `setRawMode`, and the cursor helpers `cursorTo`,
15
+ * `moveCursor`, `clearLine`, `clearScreenDown`, `emitKeypressEvents`.
16
+ */
17
+
18
+ #include "rt_internal.h"
19
+
20
+ #include <stdio.h>
21
+ #include <stdlib.h>
22
+ #include <string.h>
23
+
24
+ #if defined(_WIN32)
25
+ #include <io.h>
26
+ #define xt_readline_read _read
27
+ #else
28
+ #include <unistd.h>
29
+ #define xt_readline_read read
30
+ #endif
31
+
32
+ #define XT_READLINE_MAX_INTERFACES 16
33
+
34
+ /* One rooted slot per live interface. The object owns its listener arrays and
35
+ * pending questions, so marking the object is enough. A listener callback can
36
+ * allocate (and therefore collect), so listener snapshots are rooted too; four
37
+ * nested dispatches are enough for any realistic callback chain. */
38
+ #define XT_READLINE_EMIT_DEPTH 4
39
+
40
+ static xt_value g_readline_interfaces[XT_READLINE_MAX_INTERFACES];
41
+ static xt_value g_readline_emit_roots[XT_READLINE_MAX_INTERFACES][XT_READLINE_EMIT_DEPTH];
42
+ static int g_readline_roots_registered = 0;
43
+
44
+ typedef struct {
45
+ xt_async_job base; /* must stay first: the pool casts to this */
46
+ int slot;
47
+ char *data;
48
+ size_t length;
49
+ size_t capacity;
50
+ int eof;
51
+ } xt_readline_job;
52
+
53
+ static void xt_readline_scan_roots(void) {
54
+ for (int i = 0; i < XT_READLINE_MAX_INTERFACES; i++) {
55
+ xt_gc_mark_value(g_readline_interfaces[i]);
56
+ for (int depth = 0; depth < XT_READLINE_EMIT_DEPTH; depth++) {
57
+ xt_gc_mark_value(g_readline_emit_roots[i][depth]);
58
+ }
59
+ }
60
+ }
61
+
62
+ static void xt_readline_ensure_roots(void) {
63
+ if (g_readline_roots_registered) return;
64
+ g_readline_roots_registered = 1;
65
+ for (int i = 0; i < XT_READLINE_MAX_INTERFACES; i++) {
66
+ g_readline_interfaces[i] = XT_UNDEFINED;
67
+ for (int depth = 0; depth < XT_READLINE_EMIT_DEPTH; depth++) {
68
+ g_readline_emit_roots[i][depth] = XT_UNDEFINED;
69
+ }
70
+ }
71
+ xt_gc_register_root_provider(xt_readline_scan_roots);
72
+ }
73
+
74
+ /* -- small value helpers -------------------------------------------------- */
75
+
76
+ static int xt_readline_truthy(xt_value object, const char *key) {
77
+ return xt_truthy(xt_object_get_cstr(object, key));
78
+ }
79
+
80
+ static void xt_readline_set_bool(xt_value object, const char *key, int value) {
81
+ xt_object_set(object, xt_string_from_cstr(key), value ? XT_TRUE : XT_FALSE);
82
+ }
83
+
84
+ static int xt_readline_slot(xt_value self) {
85
+ xt_value slot = xt_object_get_cstr(self, "__xt_rl_slot");
86
+ if (!XT_IS_NUMBER(slot)) return -1;
87
+ int index = (int)xt_to_number(slot);
88
+ if (index < 0 || index >= XT_READLINE_MAX_INTERFACES) return -1;
89
+ return index;
90
+ }
91
+
92
+ static void xt_readline_write(const char *data, size_t length) {
93
+ if (data == NULL || length == 0) return;
94
+ fwrite(data, 1, length, stdout);
95
+ fflush(stdout);
96
+ }
97
+
98
+ /* -- listeners ------------------------------------------------------------ */
99
+
100
+ static const char *xt_readline_listener_key(const char *type) {
101
+ if (strcmp(type, "line") == 0) return "__xt_rl_line";
102
+ if (strcmp(type, "close") == 0) return "__xt_rl_close";
103
+ if (strcmp(type, "pause") == 0) return "__xt_rl_pause";
104
+ if (strcmp(type, "resume") == 0) return "__xt_rl_resume";
105
+ return NULL;
106
+ }
107
+
108
+ static void xt_readline_add_listener(xt_value self, const char *type, xt_value fn, int once) {
109
+ if (!XT_IS_FUNCTION(fn)) return;
110
+ const char *key = xt_readline_listener_key(type);
111
+ if (key == NULL) return;
112
+ xt_value list = xt_object_get_cstr(self, key);
113
+ if (!XT_IS_ARRAY(list)) {
114
+ list = xt_array_new(0, NULL);
115
+ xt_object_set(self, xt_string_from_cstr(key), list);
116
+ }
117
+ xt_value entry = xt_object_new();
118
+ xt_object_set(entry, xt_string_from_cstr("fn"), fn);
119
+ xt_object_set(entry, xt_string_from_cstr("once"), once ? XT_TRUE : XT_FALSE);
120
+ xt_array_push(list, entry);
121
+ }
122
+
123
+ static void xt_readline_remove_listener(xt_value self, const char *type, xt_value fn) {
124
+ const char *key = xt_readline_listener_key(type);
125
+ if (key == NULL) return;
126
+ xt_value list = xt_object_get_cstr(self, key);
127
+ if (!XT_IS_ARRAY(list)) return;
128
+ xt_value kept = xt_array_new(0, NULL);
129
+ int32_t count = xt_array_size(list);
130
+ for (int32_t i = 0; i < count; i++) {
131
+ xt_value entry = xt_array_get(list, xt_number((double)i));
132
+ xt_value handler = xt_object_get_cstr(entry, "fn");
133
+ if (!xt_truthy(xt_seq(handler, fn))) xt_array_push(kept, entry);
134
+ }
135
+ xt_object_set(self, xt_string_from_cstr(key), kept);
136
+ }
137
+
138
+ static void xt_readline_emit(xt_value self, const char *type, int32_t argc, xt_value *argv) {
139
+ const char *key = xt_readline_listener_key(type);
140
+ if (key == NULL) return;
141
+ xt_value list = xt_object_get_cstr(self, key);
142
+ if (!XT_IS_ARRAY(list)) return;
143
+ /* Snapshot the entries: a listener may remove itself or another listener.
144
+ * The callback can allocate, so the snapshot must be a GC root for the
145
+ * duration of the dispatch. */
146
+ int slot = xt_readline_slot(self);
147
+ xt_value *root = NULL;
148
+ if (slot >= 0) {
149
+ for (int depth = 0; depth < XT_READLINE_EMIT_DEPTH; depth++) {
150
+ if (!XT_IS_OBJECT(g_readline_emit_roots[slot][depth])) {
151
+ root = &g_readline_emit_roots[slot][depth];
152
+ break;
153
+ }
154
+ }
155
+ }
156
+ int32_t count = xt_array_size(list);
157
+ xt_value snapshot = xt_array_new(0, NULL);
158
+ for (int32_t i = 0; i < count; i++) xt_array_push(snapshot, xt_array_get(list, xt_number((double)i)));
159
+ if (root != NULL) *root = snapshot;
160
+ for (int32_t i = 0; i < count; i++) {
161
+ xt_value entry = xt_array_get(snapshot, xt_number((double)i));
162
+ xt_value fn = xt_object_get_cstr(entry, "fn");
163
+ if (!XT_IS_FUNCTION(fn)) continue;
164
+ if (xt_truthy(xt_object_get_cstr(entry, "once"))) {
165
+ xt_readline_remove_listener(self, type, fn);
166
+ }
167
+ xt_call_with_this(fn, self, argc, argv);
168
+ }
169
+ if (root != NULL) *root = XT_UNDEFINED;
170
+ }
171
+
172
+ /* -- reading -------------------------------------------------------------- */
173
+
174
+ static void xt_readline_start_read(int slot);
175
+ static void xt_readline_close(xt_value self);
176
+ static void xt_readline_deliver(xt_value self, xt_value line);
177
+ static void xt_readline_drain_pending(xt_value self);
178
+
179
+ static void xt_readline_job_run(xt_async_job *base) {
180
+ xt_readline_job *job = (xt_readline_job *)base;
181
+ for (;;) {
182
+ char byte = 0;
183
+ /* Not `read`: the local would shadow the POSIX function of the same name
184
+ * inside its own initializer on non-Windows hosts. */
185
+ int got = (int)xt_readline_read(0, &byte, 1);
186
+ if (got <= 0) {
187
+ /* A bare EOF reports the last line (without a newline) and then closes;
188
+ * a read error is treated the same way so the program cannot hang. */
189
+ job->eof = 1;
190
+ break;
191
+ }
192
+ if (byte == '\n') break;
193
+ if (byte == '\r') continue;
194
+ if (job->length + 1 >= job->capacity) {
195
+ size_t next = job->capacity == 0 ? 64 : job->capacity * 2;
196
+ char *grown = (char *)realloc(job->data, next);
197
+ if (grown == NULL) {
198
+ job->eof = 1;
199
+ break;
200
+ }
201
+ job->data = grown;
202
+ job->capacity = next;
203
+ }
204
+ job->data[job->length++] = byte;
205
+ }
206
+ if (job->data == NULL) {
207
+ job->data = (char *)malloc(1);
208
+ if (job->data != NULL) job->data[0] = '\0';
209
+ } else {
210
+ job->data[job->length] = '\0';
211
+ }
212
+ }
213
+
214
+ static void xt_readline_job_release(xt_async_job *base) {
215
+ xt_readline_job *job = (xt_readline_job *)base;
216
+ free(job->data);
217
+ free(job);
218
+ }
219
+
220
+ static void xt_readline_job_complete(xt_async_job *base) {
221
+ xt_readline_job *job = (xt_readline_job *)base;
222
+ int slot = job->slot;
223
+ xt_value self = (slot >= 0 && slot < XT_READLINE_MAX_INTERFACES)
224
+ ? g_readline_interfaces[slot]
225
+ : XT_UNDEFINED;
226
+ /* A closed interface frees its slot; a new interface may reuse it before
227
+ * this job completes. Deliver only to the interface that started the read. */
228
+ if (!XT_IS_OBJECT(self) || !xt_truthy(xt_seq(self, job->base.roots[0]))) return;
229
+ xt_readline_set_bool(self, "__xt_rl_reading", 0);
230
+ if (xt_readline_truthy(self, "__xt_rl_closed")) return;
231
+
232
+ xt_value line = xt_string_new(job->data != NULL ? job->data : "", job->length);
233
+ /* EOF without any pending bytes is not a line; a final unterminated line is. */
234
+ if (!job->eof || job->length > 0) {
235
+ if (xt_readline_truthy(self, "__xt_rl_paused")) {
236
+ xt_value pending = xt_object_get_cstr(self, "__xt_rl_pending");
237
+ if (!XT_IS_ARRAY(pending)) {
238
+ pending = xt_array_new(0, NULL);
239
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_pending"), pending);
240
+ }
241
+ xt_array_push(pending, line);
242
+ } else {
243
+ xt_readline_deliver(self, line);
244
+ }
245
+ }
246
+
247
+ if (job->eof) {
248
+ xt_readline_close(self);
249
+ return;
250
+ }
251
+ if (!xt_readline_truthy(self, "__xt_rl_closed") &&
252
+ !xt_readline_truthy(self, "__xt_rl_paused")) {
253
+ xt_readline_start_read(slot);
254
+ }
255
+ }
256
+
257
+ static void xt_readline_start_read(int slot) {
258
+ if (slot < 0 || slot >= XT_READLINE_MAX_INTERFACES) return;
259
+ xt_value self = g_readline_interfaces[slot];
260
+ if (!XT_IS_OBJECT(self)) return;
261
+ if (xt_readline_truthy(self, "__xt_rl_closed") || xt_readline_truthy(self, "__xt_rl_paused")) return;
262
+ if (xt_readline_truthy(self, "__xt_rl_reading")) return;
263
+
264
+ xt_readline_job *job = (xt_readline_job *)calloc(1, sizeof(xt_readline_job));
265
+ if (job == NULL) return;
266
+ job->slot = slot;
267
+ job->base.run = xt_readline_job_run;
268
+ job->base.complete = xt_readline_job_complete;
269
+ job->base.release = xt_readline_job_release;
270
+ job->base.roots[0] = self;
271
+ xt_readline_set_bool(self, "__xt_rl_reading", 1);
272
+ xt_async_submit(&job->base);
273
+ }
274
+
275
+ /* -- delivery ------------------------------------------------------------- */
276
+
277
+ static void xt_readline_deliver(xt_value self, xt_value line) {
278
+ xt_value questions = xt_object_get_cstr(self, "__xt_rl_questions");
279
+ if (XT_IS_ARRAY(questions) && xt_array_size(questions) > 0) {
280
+ xt_value entry = xt_array_get(questions, xt_number(0));
281
+ xt_value rest = xt_array_new(0, NULL);
282
+ int32_t count = xt_array_size(questions);
283
+ for (int32_t i = 1; i < count; i++) xt_array_push(rest, xt_array_get(questions, xt_number((double)i)));
284
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_questions"), rest);
285
+ xt_value callback = xt_object_get_cstr(entry, "callback");
286
+ if (XT_IS_FUNCTION(callback)) {
287
+ xt_value args[1] = {line};
288
+ xt_call_with_this(callback, XT_UNDEFINED, 1, args);
289
+ }
290
+ return;
291
+ }
292
+ xt_value args[1] = {line};
293
+ xt_readline_emit(self, "line", 1, args);
294
+ }
295
+
296
+ static void xt_readline_drain_pending(xt_value self) {
297
+ xt_value pending = xt_object_get_cstr(self, "__xt_rl_pending");
298
+ if (!XT_IS_ARRAY(pending) || xt_array_size(pending) == 0) return;
299
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_pending"), xt_array_new(0, NULL));
300
+ int32_t count = xt_array_size(pending);
301
+ for (int32_t i = 0; i < count; i++) {
302
+ if (xt_readline_truthy(self, "__xt_rl_closed") || xt_readline_truthy(self, "__xt_rl_paused")) break;
303
+ xt_readline_deliver(self, xt_array_get(pending, xt_number((double)i)));
304
+ }
305
+ }
306
+
307
+ static void xt_readline_close(xt_value self) {
308
+ if (xt_readline_truthy(self, "__xt_rl_closed")) return;
309
+ xt_readline_set_bool(self, "__xt_rl_closed", 1);
310
+ xt_readline_emit(self, "close", 0, NULL);
311
+ int slot = xt_readline_slot(self);
312
+ if (slot >= 0 && slot < XT_READLINE_MAX_INTERFACES) g_readline_interfaces[slot] = XT_UNDEFINED;
313
+ }
314
+
315
+ /* -- Interface methods ---------------------------------------------------- */
316
+
317
+ #define METHOD(name) xt_value name(xt_value self, xt_value env, int32_t argc, xt_value *argv)
318
+
319
+ METHOD(rl_on) {
320
+ (void)env;
321
+ if (argc >= 2) {
322
+ const char *type = xt_string_data(xt_to_string(xt_arg(argc, argv, 0)));
323
+ if (type != NULL) {
324
+ xt_readline_add_listener(self, type, xt_arg(argc, argv, 1), 0);
325
+ if (strcmp(type, "line") == 0) xt_readline_start_read(xt_readline_slot(self));
326
+ }
327
+ }
328
+ return self;
329
+ }
330
+
331
+ METHOD(rl_once) {
332
+ (void)env;
333
+ if (argc >= 2) {
334
+ const char *type = xt_string_data(xt_to_string(xt_arg(argc, argv, 0)));
335
+ if (type != NULL) {
336
+ xt_readline_add_listener(self, type, xt_arg(argc, argv, 1), 1);
337
+ if (strcmp(type, "line") == 0) xt_readline_start_read(xt_readline_slot(self));
338
+ }
339
+ }
340
+ return self;
341
+ }
342
+
343
+ METHOD(rl_off) {
344
+ (void)env;
345
+ if (argc >= 2) {
346
+ const char *type = xt_string_data(xt_to_string(xt_arg(argc, argv, 0)));
347
+ if (type != NULL) xt_readline_remove_listener(self, type, xt_arg(argc, argv, 1));
348
+ }
349
+ return self;
350
+ }
351
+
352
+ METHOD(rl_remove_all_listeners) {
353
+ (void)env;
354
+ if (argc == 0) {
355
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_line"), xt_array_new(0, NULL));
356
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_close"), xt_array_new(0, NULL));
357
+ return self;
358
+ }
359
+ const char *key = xt_readline_listener_key(xt_string_data(xt_to_string(xt_arg(argc, argv, 0))));
360
+ if (key != NULL) xt_object_set(self, xt_string_from_cstr(key), xt_array_new(0, NULL));
361
+ return self;
362
+ }
363
+
364
+ METHOD(rl_question) {
365
+ (void)env;
366
+ const char *query = argc >= 1 ? xt_string_data(xt_to_string(xt_arg(argc, argv, 0))) : "";
367
+ if (query != NULL) xt_readline_write(query, strlen(query));
368
+ if (argc >= 2 && XT_IS_FUNCTION(xt_arg(argc, argv, 1))) {
369
+ xt_value questions = xt_object_get_cstr(self, "__xt_rl_questions");
370
+ if (!XT_IS_ARRAY(questions)) {
371
+ questions = xt_array_new(0, NULL);
372
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_questions"), questions);
373
+ }
374
+ xt_value entry = xt_object_new();
375
+ xt_object_set(entry, xt_string_from_cstr("callback"), xt_arg(argc, argv, 1));
376
+ xt_array_push(questions, entry);
377
+ }
378
+ xt_readline_start_read(xt_readline_slot(self));
379
+ return XT_UNDEFINED;
380
+ }
381
+
382
+ METHOD(rl_prompt) {
383
+ (void)env;
384
+ (void)argc;
385
+ (void)argv;
386
+ xt_value prompt = xt_object_get_cstr(self, "__xt_rl_prompt");
387
+ const char *text = XT_IS_STRING(prompt) ? xt_string_data(prompt) : "> ";
388
+ if (text != NULL) xt_readline_write(text, strlen(text));
389
+ return XT_UNDEFINED;
390
+ }
391
+
392
+ METHOD(rl_set_prompt) {
393
+ (void)env;
394
+ if (argc >= 1) {
395
+ xt_value text = xt_to_string(xt_arg(argc, argv, 0));
396
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_prompt"), text);
397
+ }
398
+ return XT_UNDEFINED;
399
+ }
400
+
401
+ METHOD(rl_get_prompt) {
402
+ (void)env;
403
+ (void)argc;
404
+ (void)argv;
405
+ xt_value prompt = xt_object_get_cstr(self, "__xt_rl_prompt");
406
+ return XT_IS_STRING(prompt) ? prompt : xt_string_from_cstr("> ");
407
+ }
408
+
409
+ METHOD(rl_close) {
410
+ (void)env;
411
+ (void)argc;
412
+ (void)argv;
413
+ xt_readline_close(self);
414
+ return XT_UNDEFINED;
415
+ }
416
+
417
+ METHOD(rl_pause) {
418
+ (void)env;
419
+ (void)argc;
420
+ (void)argv;
421
+ if (xt_readline_truthy(self, "__xt_rl_closed")) return XT_UNDEFINED;
422
+ xt_readline_set_bool(self, "__xt_rl_paused", 1);
423
+ xt_readline_emit(self, "pause", 0, NULL);
424
+ return XT_UNDEFINED;
425
+ }
426
+
427
+ METHOD(rl_resume) {
428
+ (void)env;
429
+ (void)argc;
430
+ (void)argv;
431
+ if (xt_readline_truthy(self, "__xt_rl_closed")) return XT_UNDEFINED;
432
+ xt_readline_set_bool(self, "__xt_rl_paused", 0);
433
+ xt_readline_emit(self, "resume", 0, NULL);
434
+ xt_readline_drain_pending(self);
435
+ xt_readline_start_read(xt_readline_slot(self));
436
+ return XT_UNDEFINED;
437
+ }
438
+
439
+ METHOD(rl_write) {
440
+ (void)env;
441
+ (void)self;
442
+ if (argc >= 1) {
443
+ xt_value text = xt_to_string(xt_arg(argc, argv, 0));
444
+ const char *data = xt_string_data(text);
445
+ if (data != NULL) xt_readline_write(data, strlen(data));
446
+ }
447
+ return XT_TRUE;
448
+ }
449
+
450
+ METHOD(rl_set_raw_mode) {
451
+ (void)self;
452
+ (void)env;
453
+ (void)argc;
454
+ (void)argv;
455
+ /* The host has no terminal raw mode; accepted so a REPL-style program runs. */
456
+ return XT_UNDEFINED;
457
+ }
458
+
459
+ #undef METHOD
460
+
461
+ static xt_value xt_readline_interface_new(int32_t argc, xt_value *argv) {
462
+ xt_readline_ensure_roots();
463
+ int slot = -1;
464
+ for (int i = 0; i < XT_READLINE_MAX_INTERFACES; i++) {
465
+ if (!XT_IS_OBJECT(g_readline_interfaces[i])) {
466
+ slot = i;
467
+ break;
468
+ }
469
+ }
470
+ if (slot < 0) return XT_UNDEFINED;
471
+
472
+ xt_value self = xt_object_new();
473
+ g_readline_interfaces[slot] = self;
474
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_slot"), xt_number((double)slot));
475
+ xt_readline_set_bool(self, "__xt_rl_closed", 0);
476
+ xt_readline_set_bool(self, "__xt_rl_paused", 0);
477
+ xt_readline_set_bool(self, "__xt_rl_reading", 0);
478
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_line"), xt_array_new(0, NULL));
479
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_close"), xt_array_new(0, NULL));
480
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_questions"), xt_array_new(0, NULL));
481
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_pending"), xt_array_new(0, NULL));
482
+
483
+ xt_value options = xt_arg(argc, argv, 0);
484
+ if (XT_IS_OBJECT(options)) {
485
+ xt_value prompt = xt_object_get_cstr(options, "prompt");
486
+ if (XT_IS_STRING(prompt)) xt_object_set(self, xt_string_from_cstr("__xt_rl_prompt"), prompt);
487
+ } else if (XT_IS_STRING(options)) {
488
+ xt_object_set(self, xt_string_from_cstr("__xt_rl_prompt"), options);
489
+ }
490
+
491
+ xt_object_set(self, xt_string_from_cstr("on"), xt_closure_new((void *)rl_on, 0, NULL));
492
+ xt_object_set(self, xt_string_from_cstr("once"), xt_closure_new((void *)rl_once, 0, NULL));
493
+ xt_object_set(self, xt_string_from_cstr("off"), xt_closure_new((void *)rl_off, 0, NULL));
494
+ xt_object_set(self, xt_string_from_cstr("removeListener"),
495
+ xt_closure_new((void *)rl_off, 0, NULL));
496
+ xt_object_set(self, xt_string_from_cstr("removeAllListeners"),
497
+ xt_closure_new((void *)rl_remove_all_listeners, 0, NULL));
498
+ xt_object_set(self, xt_string_from_cstr("question"), xt_closure_new((void *)rl_question, 0, NULL));
499
+ xt_object_set(self, xt_string_from_cstr("prompt"), xt_closure_new((void *)rl_prompt, 0, NULL));
500
+ xt_object_set(self, xt_string_from_cstr("setPrompt"), xt_closure_new((void *)rl_set_prompt, 0, NULL));
501
+ xt_object_set(self, xt_string_from_cstr("getPrompt"), xt_closure_new((void *)rl_get_prompt, 0, NULL));
502
+ xt_object_set(self, xt_string_from_cstr("close"), xt_closure_new((void *)rl_close, 0, NULL));
503
+ xt_object_set(self, xt_string_from_cstr("pause"), xt_closure_new((void *)rl_pause, 0, NULL));
504
+ xt_object_set(self, xt_string_from_cstr("resume"), xt_closure_new((void *)rl_resume, 0, NULL));
505
+ xt_object_set(self, xt_string_from_cstr("write"), xt_closure_new((void *)rl_write, 0, NULL));
506
+ xt_object_set(self, xt_string_from_cstr("setRawMode"), xt_closure_new((void *)rl_set_raw_mode, 0, NULL));
507
+ return self;
508
+ }
509
+
510
+ /* -- module entry points -------------------------------------------------- */
511
+
512
+ xt_value xt_readline_create_interface(int32_t argc, xt_value *argv) {
513
+ return xt_readline_interface_new(argc, argv);
514
+ }
515
+
516
+ xt_value xt_readline_interface_ctor(int32_t argc, xt_value *argv) {
517
+ return xt_readline_interface_new(argc, argv);
518
+ }
519
+
520
+ static void xt_readline_write_escape(const char *format, int a, int b) {
521
+ char buffer[64];
522
+ snprintf(buffer, sizeof(buffer), format, a, b);
523
+ xt_readline_write(buffer, strlen(buffer));
524
+ }
525
+
526
+ xt_value xt_readline_cursor_to(int32_t argc, xt_value *argv) {
527
+ if (argc < 2) return XT_UNDEFINED;
528
+ int x = (int)xt_to_number(xt_arg(argc, argv, 1));
529
+ if (x < 0) x = 0;
530
+ if (argc >= 3 && !XT_IS_UNDEFINED(xt_arg(argc, argv, 2))) {
531
+ int y = (int)xt_to_number(xt_arg(argc, argv, 2));
532
+ if (y < 0) y = 0;
533
+ xt_readline_write_escape("\x1b[%d;%dH", y + 1, x + 1);
534
+ } else {
535
+ xt_readline_write_escape("\x1b[%dG", x + 1, 0);
536
+ }
537
+ return XT_UNDEFINED;
538
+ }
539
+
540
+ xt_value xt_readline_move_cursor(int32_t argc, xt_value *argv) {
541
+ if (argc < 3) return XT_UNDEFINED;
542
+ int dx = (int)xt_to_number(xt_arg(argc, argv, 1));
543
+ int dy = (int)xt_to_number(xt_arg(argc, argv, 2));
544
+ if (dy < 0) xt_readline_write_escape("\x1b[%dA", -dy, 0);
545
+ else if (dy > 0) xt_readline_write_escape("\x1b[%dB", dy, 0);
546
+ if (dx < 0) xt_readline_write_escape("\x1b[%dD", -dx, 0);
547
+ else if (dx > 0) xt_readline_write_escape("\x1b[%dC", dx, 0);
548
+ return XT_UNDEFINED;
549
+ }
550
+
551
+ xt_value xt_readline_clear_line(int32_t argc, xt_value *argv) {
552
+ int direction = argc >= 2 ? (int)xt_to_number(xt_arg(argc, argv, 1)) : 0;
553
+ /* Node: -1 clears to the left, 1 to the right, 0 the whole line. */
554
+ if (direction < 0) xt_readline_write("\x1b[1K", 4);
555
+ else if (direction > 0) xt_readline_write("\x1b[0K", 4);
556
+ else xt_readline_write("\x1b[2K", 4);
557
+ return XT_UNDEFINED;
558
+ }
559
+
560
+ xt_value xt_readline_clear_screen_down(int32_t argc, xt_value *argv) {
561
+ (void)argv;
562
+ (void)argc;
563
+ xt_readline_write("\x1b[0J", 4);
564
+ return XT_UNDEFINED;
565
+ }
566
+
567
+ xt_value xt_readline_emit_keypress_events(int32_t argc, xt_value *argv) {
568
+ (void)argc;
569
+ (void)argv;
570
+ /* Keypress events are not synthesised; accepted for API compatibility. */
571
+ return XT_UNDEFINED;
572
+ }
package/runtime/rt.h CHANGED
@@ -291,8 +291,14 @@ xt_value xt_array_buffer_alloc(uint32_t byte_length);
291
291
  /** Typed-array view internals for extensions: whether `value` is a view, the
292
292
  * `ArrayBuffer` it views, and the byte offset of the view inside it. */
293
293
  int xt_is_typed_array(xt_value value);
294
+ /** Element kind (0..8: U8, I8, U8C, U16, I16, U32, I32, F32, F64), or -1. */
295
+ int xt_typed_array_kind_of(xt_value value);
296
+ /** Read one element (coerced to a number) from a typed-array view. */
297
+ xt_value xt_typed_array_read(xt_value array, uint32_t index);
294
298
  xt_value xt_typed_array_backing_buffer(xt_value value);
295
299
  uint32_t xt_typed_array_view_offset(xt_value value);
300
+ /** Number of elements in a typed-array view. */
301
+ uint32_t xt_typed_array_length(xt_value value);
296
302
  /** Error-family construction and prototype lookup by constructor name. */
297
303
  xt_value xt_error_ctor_by_name(xt_value name, int32_t argc, xt_value *argv);
298
304
  xt_value xt_error_prototype(xt_value name);
@@ -314,6 +320,27 @@ xt_value xt_url_file_url_to_path(int32_t argc, xt_value *argv);
314
320
  /* -- node:crypto ---------------------------------------------------------- */
315
321
  xt_value xt_crypto_create_hash(int32_t argc, xt_value *argv);
316
322
 
323
+ /* -- entropy (xt_random.c) ------------------------------------------------ */
324
+ /** Fill `out` with `length` bytes from the OS CSPRNG. Returns 0 on success. */
325
+ int xt_secure_random(uint8_t *out, size_t length);
326
+ /** A 64-bit value from the process-wide xoshiro256** generator (seeded from the
327
+ * OS CSPRNG). Never returns a constant stream. */
328
+ uint64_t xt_random_u64(void);
329
+ /** Uniform double in `[0, 1)` with 53 random bits. */
330
+ double xt_random_double(void);
331
+ /** Uniform integer in `[0, bound)` (rejection sampling; `bound` 0 returns 0). */
332
+ uint64_t xt_random_below(uint64_t bound);
333
+
334
+ /* -- Web Crypto surface (xt_crypto.c) ------------------------------------ */
335
+ /** `crypto.<method>(...)` (namespace call lowering). */
336
+ xt_value xt_crypto_static(xt_value name, int32_t argc, xt_value *argv);
337
+ /** `crypto.<property>` read as a value (closures for the methods). */
338
+ xt_value xt_crypto_get(xt_value name);
339
+ /** The first-class `crypto` object (`const c = crypto`). */
340
+ xt_value xt_crypto_object(int32_t argc, xt_value *argv);
341
+ /** The `webcrypto` object re-exported by `node:crypto`. */
342
+ xt_value xt_crypto_webcrypto(int32_t argc, xt_value *argv);
343
+
317
344
  /* -- node:child_process --------------------------------------------------- */
318
345
  xt_value xt_child_process_spawn_sync(int32_t argc, xt_value *argv);
319
346
 
@@ -336,6 +363,13 @@ xt_value xt_await(xt_value value);
336
363
  void xt_drain_microtasks(void);
337
364
  /** Mark `value` (a promise) as observed, so its rejection is not reported. */
338
365
  void xt_promise_observe_value(xt_value value);
366
+ /**
367
+ * Host hook for an unhandled rejection. Return non-zero when the host reported
368
+ * it and the default fatal exit should be suppressed (the GUI engine turns it
369
+ * into a window `unhandledrejection` event).
370
+ */
371
+ typedef int (*xt_unhandled_rejection_handler)(xt_value reason, xt_value promise);
372
+ void xt_set_unhandled_rejection_handler(xt_unhandled_rejection_handler handler);
339
373
  /** Print rejected promises that no handler ever observed; returns how many. */
340
374
  int xt_promise_report_unhandled(void);
341
375
 
@@ -454,6 +488,14 @@ xt_value xt_is_nullish(xt_value value);
454
488
 
455
489
  /* -- exceptions / output -------------------------------------------------- */
456
490
  void xt_throw(xt_value v);
491
+ /**
492
+ * Report an exception that a host-owned callback boundary caught. With a host
493
+ * handler installed (the GUI engine) the handler decides how to surface it;
494
+ * otherwise this prints like `xt_throw` and exits.
495
+ */
496
+ typedef void (*xt_uncaught_exception_handler)(xt_value error);
497
+ void xt_set_uncaught_exception_handler(xt_uncaught_exception_handler handler);
498
+ void xt_uncaught_exception(xt_value error);
457
499
  /** Allocate and push a `try` frame; the returned pointer is the `setjmp` buffer. */
458
500
  void *xt_try_enter(void);
459
501
  xt_value xt_try_exception(void *frame);
@@ -411,8 +411,10 @@ xt_value xt_iter_has(xt_value value, xt_value index);
411
411
  * protocols, or a wrapper around `value[Symbol.iterator]()` otherwise. */
412
412
  xt_value xt_iter_open(xt_value value);
413
413
 
414
- /* Monotonic clock in fractional milliseconds (xt_loop.c), for `performance.now`. */
414
+ /* xbintsc clock helpers (xt_loop.c): monotonic milliseconds for
415
+ * `performance.now`, wall-clock milliseconds for `Date`. */
415
416
  double xt_monotonic_ms(void);
417
+ double xt_wallclock_ms(void);
416
418
 
417
419
  /* Stackful generators (xt_generator.c). */
418
420
  int xt_is_generator(xt_value value);
@@ -593,7 +595,6 @@ uint32_t xt_array_buffer_byte_length(xt_value value);
593
595
  uint8_t xt_array_buffer_get_byte(xt_value buffer, uint32_t index);
594
596
  void xt_array_buffer_set_byte(xt_value buffer, uint32_t index, uint8_t value);
595
597
  xt_value xt_array_buffer_ctor(int32_t argc, xt_value *argv);
596
-
597
598
  #include "xt_async_io.h"
598
599
 
599
600
  #endif /* XT_RT_INTERNAL_H */