xbintsc 0.6.12 → 0.6.17

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 (189) hide show
  1. package/dist/src/ast/expressions.d.ts +6 -0
  2. package/dist/src/checker/type-infer.js +1 -1
  3. package/dist/src/checker/type-infer.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/codegen/generator/calls/literals.js +4 -1
  6. package/dist/src/codegen/generator/calls/literals.js.map +1 -1
  7. package/dist/src/codegen/generator/expressions/assignment.js +3 -1
  8. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  9. package/dist/src/codegen/generator/expressions/operators.js +2 -6
  10. package/dist/src/codegen/generator/expressions/operators.js.map +1 -1
  11. package/dist/src/codegen/generator/expressions/primary.d.ts +9 -0
  12. package/dist/src/codegen/generator/expressions/primary.js +44 -7
  13. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  14. package/dist/src/driver/bundler/commonjs.js +1 -1
  15. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  16. package/dist/src/driver/bundler/symbols.js +6 -0
  17. package/dist/src/driver/bundler/symbols.js.map +1 -1
  18. package/dist/src/driver/version.d.ts +1 -1
  19. package/dist/src/driver/version.js +1 -1
  20. package/dist/src/extensions/gui/features.d.ts +2 -0
  21. package/dist/src/extensions/gui/features.js +24 -0
  22. package/dist/src/extensions/gui/features.js.map +1 -1
  23. package/dist/src/extensions/gui/index.js +39 -0
  24. package/dist/src/extensions/gui/index.js.map +1 -1
  25. package/dist/src/extensions/node/index.js +2 -0
  26. package/dist/src/extensions/node/index.js.map +1 -1
  27. package/dist/src/extensions/node/readline/index.d.ts +12 -0
  28. package/dist/src/extensions/node/readline/index.js +27 -0
  29. package/dist/src/extensions/node/readline/index.js.map +1 -0
  30. package/dist/src/parser/declarations/functions.js +6 -0
  31. package/dist/src/parser/declarations/functions.js.map +1 -1
  32. package/dist/src/parser/literals.js +1 -1
  33. package/dist/src/parser/literals.js.map +1 -1
  34. package/dist/tests/e2e/compile-modules.test.js +22 -0
  35. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  36. package/dist/tests/e2e/date-parse.test.js +17 -0
  37. package/dist/tests/e2e/date-parse.test.js.map +1 -1
  38. package/dist/tests/e2e/gui-browser.test.js +61 -0
  39. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  40. package/dist/tests/e2e/gui-css-coverage.test.js +39 -0
  41. package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
  42. package/dist/tests/e2e/gui-css-features.test.js +100 -0
  43. package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
  44. package/dist/tests/e2e/gui-dom.test.js +34 -0
  45. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  46. package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
  47. package/dist/tests/e2e/gui-errors.test.js +68 -0
  48. package/dist/tests/e2e/gui-errors.test.js.map +1 -0
  49. package/dist/tests/e2e/gui-example-pelican3d.test.d.ts +6 -4
  50. package/dist/tests/e2e/gui-example-pelican3d.test.js +103 -44
  51. package/dist/tests/e2e/gui-example-pelican3d.test.js.map +1 -1
  52. package/dist/tests/e2e/gui-example-webgpu.test.d.ts +9 -0
  53. package/dist/tests/e2e/gui-example-webgpu.test.js +59 -0
  54. package/dist/tests/e2e/gui-example-webgpu.test.js.map +1 -0
  55. package/dist/tests/e2e/gui-helpers.d.ts +1 -1
  56. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +7 -1
  57. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -1
  58. package/dist/tests/e2e/gui-webgpu.test.d.ts +11 -0
  59. package/dist/tests/e2e/gui-webgpu.test.js +402 -0
  60. package/dist/tests/e2e/gui-webgpu.test.js.map +1 -0
  61. package/dist/tests/e2e/harness.d.ts +4 -0
  62. package/dist/tests/e2e/harness.js +2 -0
  63. package/dist/tests/e2e/harness.js.map +1 -1
  64. package/dist/tests/e2e/readline.test.d.ts +7 -0
  65. package/dist/tests/e2e/readline.test.js +73 -0
  66. package/dist/tests/e2e/readline.test.js.map +1 -0
  67. package/dist/tests/extensions/gui-features.test.js +18 -1
  68. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  69. package/doc/ai/extensions.md +7 -1
  70. package/doc/ai/zh-CN/extensions.md +6 -1
  71. package/doc/gui/api.md +15 -0
  72. package/doc/gui/dom.md +7 -2
  73. package/doc/gui/known-issues.md +2 -1
  74. package/doc/gui/layout.md +5 -1
  75. package/doc/gui/webgpu.md +121 -0
  76. package/doc/gui.md +12 -1
  77. package/doc/implemented/node/readline.md +34 -0
  78. package/doc/implemented/node.md +1 -0
  79. package/doc/unimplemented/node/modules.md +2 -3
  80. package/doc/unimplemented/node.md +2 -2
  81. package/doc/zh-CN/gui/api.md +11 -0
  82. package/doc/zh-CN/gui/dom.md +6 -2
  83. package/doc/zh-CN/gui/known-issues.md +2 -1
  84. package/doc/zh-CN/gui/layout.md +4 -1
  85. package/doc/zh-CN/gui/webgpu.md +111 -0
  86. package/doc/zh-CN/gui.md +10 -1
  87. package/doc/zh-CN/implemented/node/readline.md +33 -0
  88. package/doc/zh-CN/implemented/node.md +1 -0
  89. package/doc/zh-CN/unimplemented/node/modules.md +1 -2
  90. package/doc/zh-CN/unimplemented/node.md +2 -2
  91. package/llms.txt +1 -0
  92. package/package.json +7 -7
  93. package/runtime/ext_gui/browser.cpp +10 -0
  94. package/runtime/ext_gui/browser_abort.cpp +2 -2
  95. package/runtime/ext_gui/browser_loop.cpp +1 -0
  96. package/runtime/ext_gui/browser_media.cpp +1 -1
  97. package/runtime/ext_gui/dom_api_canvas.cpp +21 -0
  98. package/runtime/ext_gui/dom_api_canvas_extra.cpp +61 -0
  99. package/runtime/ext_gui/dom_api_canvas_image.cpp +2 -1
  100. package/runtime/ext_gui/dom_api_events.cpp +1 -1
  101. package/runtime/ext_gui/dom_api_internal.h +2 -0
  102. package/runtime/ext_gui/dom_api_node.cpp +18 -12
  103. package/runtime/ext_gui/dom_api_node_read.cpp +27 -0
  104. package/runtime/ext_gui/dom_api_proto.cpp +2 -0
  105. package/runtime/ext_gui/error_report.cpp +166 -0
  106. package/runtime/ext_gui/gpu_feature.cpp +2 -0
  107. package/runtime/ext_gui/gpu_shader_api.h +6 -0
  108. package/runtime/ext_gui/gui.cpp +16 -0
  109. package/runtime/ext_gui/gui_canvas.cpp +18 -0
  110. package/runtime/ext_gui/gui_engine.h +13 -0
  111. package/runtime/ext_gui/gui_events.cpp +4 -1
  112. package/runtime/ext_gui/layout_inline.cpp +46 -14
  113. package/runtime/ext_gui/paint.cpp +22 -0
  114. package/runtime/ext_gui/renderer_image.cpp +6 -0
  115. package/runtime/ext_gui/script.cpp +1 -1
  116. package/runtime/ext_gui/style.cpp +1 -0
  117. package/runtime/ext_gui/style.h +13 -0
  118. package/runtime/ext_gui/style_print.cpp +99 -0
  119. package/runtime/ext_gui/style_text.cpp +79 -0
  120. package/runtime/ext_gui/webgl_glsl.h +8 -0
  121. package/runtime/ext_gui/webgl_glsl_compile.cpp +11 -5
  122. package/runtime/ext_gui/webgl_msl.cpp +20 -9
  123. package/runtime/ext_gui/webgl_msl.h +4 -0
  124. package/runtime/ext_gui/webgl_texture.cpp +0 -8
  125. package/runtime/ext_gui/webgpu.h +371 -0
  126. package/runtime/ext_gui/webgpu_api.h +42 -0
  127. package/runtime/ext_gui/webgpu_bindings.cpp +364 -0
  128. package/runtime/ext_gui/webgpu_bindings.h +59 -0
  129. package/runtime/ext_gui/webgpu_bridge.c +21 -0
  130. package/runtime/ext_gui/webgpu_buffer.cpp +293 -0
  131. package/runtime/ext_gui/webgpu_canvas.cpp +305 -0
  132. package/runtime/ext_gui/webgpu_command.cpp +510 -0
  133. package/runtime/ext_gui/webgpu_compute_pass.cpp +138 -0
  134. package/runtime/ext_gui/webgpu_convert.cpp +309 -0
  135. package/runtime/ext_gui/webgpu_device.cpp +370 -0
  136. package/runtime/ext_gui/webgpu_feature.cpp +74 -0
  137. package/runtime/ext_gui/webgpu_objects.cpp +125 -0
  138. package/runtime/ext_gui/webgpu_pass.cpp +295 -0
  139. package/runtime/ext_gui/webgpu_pass.h +33 -0
  140. package/runtime/ext_gui/webgpu_pipeline.cpp +463 -0
  141. package/runtime/ext_gui/webgpu_queue.cpp +222 -0
  142. package/runtime/ext_gui/webgpu_render_pass.cpp +417 -0
  143. package/runtime/ext_gui/webgpu_resources.cpp +315 -0
  144. package/runtime/ext_gui/webgpu_shader.cpp +242 -0
  145. package/runtime/ext_gui/wgsl.h +99 -0
  146. package/runtime/ext_gui/wgsl_api.cpp +137 -0
  147. package/runtime/ext_gui/wgsl_ast.h +233 -0
  148. package/runtime/ext_gui/wgsl_emit.cpp +452 -0
  149. package/runtime/ext_gui/wgsl_emit.h +128 -0
  150. package/runtime/ext_gui/wgsl_emit_call.cpp +281 -0
  151. package/runtime/ext_gui/wgsl_emit_expr.cpp +192 -0
  152. package/runtime/ext_gui/wgsl_emit_stmt.cpp +209 -0
  153. package/runtime/ext_gui/wgsl_entry.cpp +189 -0
  154. package/runtime/ext_gui/wgsl_infer.cpp +171 -0
  155. package/runtime/ext_gui/wgsl_lexer.cpp +179 -0
  156. package/runtime/ext_gui/wgsl_lexer.h +32 -0
  157. package/runtime/ext_gui/wgsl_parse_expr.cpp +215 -0
  158. package/runtime/ext_gui/wgsl_parse_stmt.cpp +259 -0
  159. package/runtime/ext_gui/wgsl_parser.cpp +457 -0
  160. package/runtime/ext_gui/wgsl_parser.h +77 -0
  161. package/runtime/ext_gui/wgsl_type.cpp +232 -0
  162. package/runtime/ext_gui/window.cpp +22 -8
  163. package/runtime/ext_gui/window_emit.cpp +1 -1
  164. package/runtime/ext_node/fs/parts/platform.inc +2 -0
  165. package/runtime/ext_node/readline/readline.c +572 -0
  166. package/runtime/rt.h +15 -0
  167. package/runtime/rt_internal.h +3 -2
  168. package/runtime/xt_io/exception.inc +29 -6
  169. package/runtime/xt_loop.c +36 -1
  170. package/runtime/xt_promise.c +26 -2
  171. package/runtime/xt_stdlib2/date.inc +2 -2
  172. package/runtime/xt_typed_array/elements.inc +0 -9
  173. package/runtime/xt_url.c +0 -6
  174. package/runtime/xt_url_object.c +1 -1
  175. package/src/ast/expressions.ts +6 -0
  176. package/src/checker/type-infer.ts +1 -1
  177. package/src/codegen/generator/calls/literals.ts +4 -1
  178. package/src/codegen/generator/expressions/assignment.ts +3 -1
  179. package/src/codegen/generator/expressions/operators.ts +2 -6
  180. package/src/codegen/generator/expressions/primary.ts +58 -6
  181. package/src/driver/bundler/commonjs.ts +1 -1
  182. package/src/driver/bundler/symbols.ts +6 -0
  183. package/src/driver/version.ts +1 -1
  184. package/src/extensions/gui/features.ts +24 -0
  185. package/src/extensions/gui/index.ts +39 -0
  186. package/src/extensions/node/index.ts +2 -0
  187. package/src/extensions/node/readline/index.ts +31 -0
  188. package/src/parser/declarations/functions.ts +6 -0
  189. 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
@@ -363,6 +363,13 @@ xt_value xt_await(xt_value value);
363
363
  void xt_drain_microtasks(void);
364
364
  /** Mark `value` (a promise) as observed, so its rejection is not reported. */
365
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);
366
373
  /** Print rejected promises that no handler ever observed; returns how many. */
367
374
  int xt_promise_report_unhandled(void);
368
375
 
@@ -481,6 +488,14 @@ xt_value xt_is_nullish(xt_value value);
481
488
 
482
489
  /* -- exceptions / output -------------------------------------------------- */
483
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);
484
499
  /** Allocate and push a `try` frame; the returned pointer is the `setjmp` buffer. */
485
500
  void *xt_try_enter(void);
486
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 */
@@ -293,21 +293,44 @@ void *xt_try_mark(void) { return (void *)g_try_top; }
293
293
 
294
294
  void xt_try_restore(void *mark) { g_try_top = (xt_try_frame *)mark; }
295
295
 
296
- void xt_throw(xt_value v) {
297
- if (g_try_top) {
298
- g_try_top->exception = v;
299
- longjmp(g_try_top->buf, 1);
300
- }
296
+ /* A host (the GUI engine) can install a handler so an exception thrown by a
297
+ * callback it invoked is reported as a page error instead of killing the
298
+ * process. The handler only runs from a host-owned boundary that can resume
299
+ * after the `longjmp`; `xt_throw` itself still terminates when nothing catches. */
300
+ static xt_uncaught_exception_handler g_uncaught_exception_handler = NULL;
301
+
302
+ void xt_set_uncaught_exception_handler(xt_uncaught_exception_handler handler) {
303
+ g_uncaught_exception_handler = handler;
304
+ }
305
+
306
+ static void xt_print_uncaught(xt_value v) {
301
307
  xt_value stack = xt_error_stack(v);
302
308
  if (stack != XT_UNDEFINED) {
303
309
  xt_string *text = xt_as_string(stack);
304
310
  if (text) {
305
311
  fprintf(stderr, "Uncaught %s\n", xt_string_bytes(text));
306
- exit(1);
312
+ return;
307
313
  }
308
314
  }
309
315
  xt_value message = xt_to_string(v);
310
316
  xt_string *s = xt_as_string(message);
311
317
  fprintf(stderr, "Uncaught %s\n", xt_string_bytes(s));
318
+ }
319
+
320
+ void xt_uncaught_exception(xt_value v) {
321
+ if (g_uncaught_exception_handler != NULL) {
322
+ g_uncaught_exception_handler(v);
323
+ return;
324
+ }
325
+ xt_print_uncaught(v);
326
+ exit(1);
327
+ }
328
+
329
+ void xt_throw(xt_value v) {
330
+ if (g_try_top) {
331
+ g_try_top->exception = v;
332
+ longjmp(g_try_top->buf, 1);
333
+ }
334
+ xt_print_uncaught(v);
312
335
  exit(1);
313
336
  }