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,132 @@
1
+ /*
2
+ * xbintsc runtime — Web Crypto surface.
3
+ *
4
+ * `crypto.getRandomValues(typedArray)` and `crypto.randomUUID()` are available
5
+ * as globals in every program; `node:crypto` re-exports the same object as
6
+ * `webcrypto`. The entropy comes from `xt_random.c` (`BCryptGenRandom` /
7
+ * `/dev/urandom`), never from `Math.random`.
8
+ */
9
+
10
+ #include "rt.h"
11
+
12
+ #include <stdint.h>
13
+ #include <stdio.h>
14
+ #include <stdlib.h>
15
+ #include <string.h>
16
+
17
+ static const int xt_crypto_kind_bytes[] = {1, 1, 1, 2, 2, 4, 4, 4, 8};
18
+
19
+ static void xt_crypto_throw(const char *constructor, const char *message) {
20
+ xt_value text = xt_string_from_cstr(message);
21
+ xt_throw(xt_error_ctor_by_name(xt_string_from_cstr(constructor), 1, &text));
22
+ }
23
+
24
+ static xt_value xt_crypto_make_uuid(void) {
25
+ uint8_t bytes[16];
26
+ if (xt_secure_random(bytes, sizeof(bytes)) != 0) {
27
+ /* The OS source is unavailable: fall back to the seeded generator rather
28
+ * than returning a constant. */
29
+ for (int i = 0; i < 16; i += 8) {
30
+ uint64_t value = xt_random_u64();
31
+ memcpy(bytes + i, &value, 8);
32
+ }
33
+ }
34
+ bytes[6] = (uint8_t)((bytes[6] & 0x0F) | 0x40); /* version 4 */
35
+ bytes[8] = (uint8_t)((bytes[8] & 0x3F) | 0x80); /* RFC 4122 variant */
36
+ char text[37];
37
+ snprintf(text, sizeof(text),
38
+ "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", bytes[0],
39
+ bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8],
40
+ bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
41
+ return xt_string_from_cstr(text);
42
+ }
43
+
44
+ /** Fill a typed array with random bytes. The DOM limits a single call to
45
+ * 65 536 bytes; larger requests throw a `RangeError`. */
46
+ static xt_value xt_crypto_get_random_values(xt_value array) {
47
+ int kind = xt_typed_array_kind_of(array);
48
+ if (kind < 0) {
49
+ xt_crypto_throw("TypeError", "crypto.getRandomValues requires a typed array");
50
+ return XT_UNDEFINED;
51
+ }
52
+ uint32_t length = xt_typed_array_length(array);
53
+ uint64_t bytes64 = (uint64_t)length * (uint64_t)xt_crypto_kind_bytes[kind];
54
+ if (bytes64 > 65536ULL) {
55
+ xt_crypto_throw("RangeError", "crypto.getRandomValues is limited to 65536 bytes");
56
+ return XT_UNDEFINED;
57
+ }
58
+ if (bytes64 == 0) return array;
59
+ size_t count = (size_t)bytes64;
60
+ uint8_t *bytes = (uint8_t *)malloc(count);
61
+ if (bytes == NULL) {
62
+ xt_crypto_throw("Error", "out of memory");
63
+ return XT_UNDEFINED;
64
+ }
65
+ int failed = xt_secure_random(bytes, count);
66
+ if (failed == 0) {
67
+ xt_array_buffer_copy_bytes_in(xt_typed_array_backing_buffer(array),
68
+ xt_typed_array_view_offset(array), bytes, (uint32_t)count);
69
+ }
70
+ free(bytes);
71
+ if (failed != 0) {
72
+ xt_crypto_throw("Error", "the operating system entropy source is unavailable");
73
+ return XT_UNDEFINED;
74
+ }
75
+ return array;
76
+ }
77
+
78
+ static xt_value xt_crypto_get_random_values_method(xt_value self, xt_value env, int32_t argc,
79
+ xt_value *argv) {
80
+ (void)self;
81
+ (void)env;
82
+ return xt_crypto_get_random_values(argc > 0 ? argv[0] : XT_UNDEFINED);
83
+ }
84
+
85
+ static xt_value xt_crypto_random_uuid_method(xt_value self, xt_value env, int32_t argc,
86
+ xt_value *argv) {
87
+ (void)self;
88
+ (void)env;
89
+ (void)argc;
90
+ (void)argv;
91
+ return xt_crypto_make_uuid();
92
+ }
93
+
94
+ xt_value xt_crypto_static(xt_value name, int32_t argc, xt_value *argv) {
95
+ const char *method = xt_string_data(name);
96
+ if (method == NULL) return XT_UNDEFINED;
97
+ if (strcmp(method, "getRandomValues") == 0) {
98
+ return xt_crypto_get_random_values(argc > 0 ? argv[0] : XT_UNDEFINED);
99
+ }
100
+ if (strcmp(method, "randomUUID") == 0) return xt_crypto_make_uuid();
101
+ if (strcmp(method, "toJSON") == 0) {
102
+ (void)argc;
103
+ (void)argv;
104
+ return xt_string_from_cstr("[object Crypto]");
105
+ }
106
+ return XT_UNDEFINED;
107
+ }
108
+
109
+ xt_value xt_crypto_get(xt_value name) {
110
+ const char *property = xt_string_data(name);
111
+ if (property == NULL) return XT_UNDEFINED;
112
+ if (strcmp(property, "getRandomValues") == 0) {
113
+ return xt_closure_new((void *)xt_crypto_get_random_values_method, 0, NULL);
114
+ }
115
+ if (strcmp(property, "randomUUID") == 0) {
116
+ return xt_closure_new((void *)xt_crypto_random_uuid_method, 0, NULL);
117
+ }
118
+ return XT_UNDEFINED;
119
+ }
120
+
121
+ xt_value xt_crypto_object(int32_t argc, xt_value *argv) {
122
+ (void)argc;
123
+ (void)argv;
124
+ xt_value object = xt_object_new();
125
+ xt_object_set(object, xt_string_from_cstr("getRandomValues"),
126
+ xt_closure_new((void *)xt_crypto_get_random_values_method, 0, NULL));
127
+ xt_object_set(object, xt_string_from_cstr("randomUUID"),
128
+ xt_closure_new((void *)xt_crypto_random_uuid_method, 0, NULL));
129
+ return object;
130
+ }
131
+
132
+ xt_value xt_crypto_webcrypto(int32_t argc, xt_value *argv) { return xt_crypto_object(argc, argv); }
@@ -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
  }
package/runtime/xt_loop.c CHANGED
@@ -108,6 +108,25 @@ double xt_monotonic_ms(void) {
108
108
  #endif
109
109
  }
110
110
 
111
+ /* Wall-clock milliseconds since the Unix epoch, for `Date.now()` and
112
+ * `new Date()`. `time(NULL)` only has second resolution, which silently broke
113
+ * elapsed-time math; this keeps the milliseconds the language exposes. */
114
+ double xt_wallclock_ms(void) {
115
+ #if defined(_WIN32)
116
+ FILETIME file_time;
117
+ GetSystemTimeAsFileTime(&file_time);
118
+ ULARGE_INTEGER ticks;
119
+ ticks.LowPart = file_time.dwLowDateTime;
120
+ ticks.HighPart = file_time.dwHighDateTime;
121
+ /* 100-ns ticks between 1601-01-01 and 1970-01-01. */
122
+ return (double)(int64_t)((ticks.QuadPart - 116444736000000000ULL) / 10000);
123
+ #else
124
+ struct timespec ts;
125
+ clock_gettime(CLOCK_REALTIME, &ts);
126
+ return (double)((int64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
127
+ #endif
128
+ }
129
+
111
130
  static void xt_loop_sleep_ms(int ms) { if (ms <= 0) return;
112
131
  #if defined(_WIN32)
113
132
  Sleep((DWORD)ms);
@@ -222,6 +241,22 @@ static void xt_loop_gc_register(void) {
222
241
  xt_gc_register_root_provider(xt_loop_gc_scan_roots);
223
242
  }
224
243
 
244
+ /* Run one timer callback under a host boundary. Without it an exception from a
245
+ * `setTimeout` callback terminates the process, which in a GUI program looks
246
+ * like the window vanishing; `xt_uncaught_exception` hands it to the host when
247
+ * one is installed and keeps the Node-like fatal default otherwise. */
248
+ static void xt_timer_invoke(xt_value callback, int32_t argc, xt_value *args) {
249
+ void *frame = xt_try_enter();
250
+ if (xt_try_setjmp(frame) == 0) {
251
+ xt_closure_call(callback, argc, args);
252
+ xt_try_leave(frame);
253
+ return;
254
+ }
255
+ xt_value error = xt_try_exception(frame);
256
+ xt_try_leave(frame);
257
+ xt_uncaught_exception(error);
258
+ }
259
+
225
260
  /* Invoke every timer whose deadline has passed. Timers scheduled by a callback
226
261
  * (or by a microtask) are left for the next reactor iteration, so a recursive
227
262
  * `setTimeout(fn, 0)` cannot spin forever inside a single call. */
@@ -240,7 +275,7 @@ static void xt_timer_run_due(void) {
240
275
  } else {
241
276
  timer->active = 0;
242
277
  }
243
- if (XT_IS_FUNCTION(callback)) xt_closure_call(callback, argc, args);
278
+ if (XT_IS_FUNCTION(callback)) xt_timer_invoke(callback, argc, args);
244
279
  }
245
280
  }
246
281
 
@@ -532,11 +532,30 @@ xt_value xt_queue_microtask(int32_t argc, xt_value *argv) {
532
532
 
533
533
  /* -- reporting ------------------------------------------------------------ */
534
534
 
535
+ static xt_unhandled_rejection_handler g_unhandled_rejection_handler = NULL;
536
+
537
+ void xt_set_unhandled_rejection_handler(xt_unhandled_rejection_handler handler) {
538
+ g_unhandled_rejection_handler = handler;
539
+ }
540
+
535
541
  int xt_promise_report_unhandled(void) {
542
+ if (xt_unhandled_count == 0) return 0;
543
+ /* The host handler runs JavaScript, which may observe promises and mutate
544
+ * the queue (removing entries and swapping in the tail). Iterate a snapshot
545
+ * and clear the queue afterwards, so every rejection is reported once. */
546
+ uint32_t count = xt_unhandled_count;
547
+ xt_promise **snapshot = (xt_promise **)malloc(sizeof(xt_promise *) * count);
548
+ if (!snapshot) abort();
549
+ memcpy(snapshot, xt_unhandled, sizeof(xt_promise *) * count);
536
550
  int reported = 0;
537
- for (uint32_t i = 0; i < xt_unhandled_count; i++) {
538
- xt_promise *promise = xt_unhandled[i];
551
+ for (uint32_t i = 0; i < count; i++) {
552
+ xt_promise *promise = snapshot[i];
539
553
  if (!promise || promise->observed) continue;
554
+ int consumed = 0;
555
+ if (g_unhandled_rejection_handler != NULL) {
556
+ consumed = g_unhandled_rejection_handler(promise->value, xt_promise_value(promise));
557
+ }
558
+ if (consumed) continue;
540
559
  reported++;
541
560
  /* `Error` (and its subclasses) carry a captured stack; everything else is
542
561
  * stringified the way Node prints a rejection reason. */
@@ -549,5 +568,10 @@ int xt_promise_report_unhandled(void) {
549
568
  fprintf(stderr, "Unhandled promise rejection\n");
550
569
  }
551
570
  }
571
+ free(snapshot);
572
+ for (uint32_t i = 0; i < xt_unhandled_count; i++) {
573
+ if (xt_unhandled[i]) xt_unhandled[i]->report_index = XT_PROMISE_UNLISTED;
574
+ }
575
+ xt_unhandled_count = 0;
552
576
  return reported;
553
577
  }
@@ -0,0 +1,143 @@
1
+ /*
2
+ * xbintsc runtime — OS entropy and the process-wide PRNG.
3
+ *
4
+ * `Math.random` used to be `rand() / (RAND_MAX + 1)`, which on the MSVC CRT has
5
+ * only 32 767 output states: salts and nonces derived from it collided and were
6
+ * predictable. This unit reads OS entropy (`BCryptGenRandom` on Windows,
7
+ * `/dev/urandom` elsewhere) for the CSPRNG surface and seeds a xoshiro256**
8
+ * generator for `Math.random`, so every run produces a different, well-mixed
9
+ * stream.
10
+ */
11
+
12
+ #include "rt.h"
13
+
14
+ #include <stdint.h>
15
+ #include <string.h>
16
+ #include <time.h>
17
+
18
+ #if defined(_WIN32)
19
+ #include <windows.h>
20
+ #include <bcrypt.h>
21
+ #else
22
+ #include <errno.h>
23
+ #include <fcntl.h>
24
+ #include <unistd.h>
25
+ #if defined(__APPLE__)
26
+ #include <stdlib.h>
27
+ #endif
28
+ #endif
29
+
30
+ static uint64_t xt_random_state[4];
31
+ static volatile int xt_random_seeded = 0;
32
+
33
+ /** SplitMix64, used to expand the seed material and to avoid an all-zero
34
+ * xoshiro state. */
35
+ static uint64_t xt_splitmix64(uint64_t *state) {
36
+ uint64_t z = (*state += 0x9E3779B97F4A7C15ULL);
37
+ z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
38
+ z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
39
+ return z ^ (z >> 31);
40
+ }
41
+
42
+ static uint64_t xt_rotate_left(uint64_t value, int bits) {
43
+ return (value << bits) | (value >> (64 - bits));
44
+ }
45
+
46
+ int xt_secure_random(uint8_t *out, size_t length) {
47
+ if (length == 0) return 0;
48
+ if (out == NULL) return -1;
49
+ #if defined(_WIN32)
50
+ NTSTATUS status = BCryptGenRandom(NULL, (PUCHAR)out, (ULONG)length,
51
+ BCRYPT_USE_SYSTEM_PREFERRED_RNG);
52
+ return status == 0 ? 0 : -1;
53
+ #elif defined(__APPLE__)
54
+ arc4random_buf(out, length);
55
+ return 0;
56
+ #else
57
+ int fd = open("/dev/urandom", O_RDONLY);
58
+ if (fd < 0) return -1;
59
+ size_t offset = 0;
60
+ while (offset < length) {
61
+ ssize_t got = read(fd, out + offset, length - offset);
62
+ if (got <= 0) {
63
+ if (got < 0 && errno == EINTR) continue;
64
+ close(fd);
65
+ return -1;
66
+ }
67
+ offset += (size_t)got;
68
+ }
69
+ close(fd);
70
+ return 0;
71
+ #endif
72
+ }
73
+
74
+ /** Fill four 64-bit words with seed material. The clock, the process id, the
75
+ * address of a static and a counter are mixed in even when the OS CSPRNG
76
+ * works, so two processes started in the same nanosecond cannot share a
77
+ * stream. */
78
+ static void xt_random_fill_seed(uint64_t seed[4]) {
79
+ uint8_t bytes[32];
80
+ uint64_t material[4];
81
+ if (xt_secure_random(bytes, sizeof(bytes)) == 0) {
82
+ memcpy(material, bytes, sizeof(material));
83
+ } else {
84
+ material[0] = (uint64_t)time(NULL);
85
+ material[1] = (uint64_t)clock();
86
+ #if defined(_WIN32)
87
+ LARGE_INTEGER counter;
88
+ QueryPerformanceCounter(&counter);
89
+ material[2] = (uint64_t)counter.QuadPart;
90
+ material[3] = (uint64_t)GetCurrentProcessId();
91
+ #else
92
+ struct timespec ts;
93
+ clock_gettime(CLOCK_MONOTONIC, &ts);
94
+ material[2] = (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
95
+ material[3] = (uint64_t)getpid();
96
+ #endif
97
+ }
98
+ material[0] ^= (uint64_t)(uintptr_t)&xt_random_state;
99
+ material[1] ^= (uint64_t)(uintptr_t)&seed;
100
+ uint64_t state = material[0] ^ xt_rotate_left(material[1], 17);
101
+ for (int i = 0; i < 4; i++) {
102
+ seed[i] = xt_splitmix64(&state);
103
+ seed[i] ^= material[i];
104
+ seed[i] = xt_splitmix64(&seed[i]);
105
+ }
106
+ if ((seed[0] | seed[1] | seed[2] | seed[3]) == 0) seed[0] = 0x9E3779B97F4A7C15ULL;
107
+ }
108
+
109
+ static void xt_random_seed(void) {
110
+ uint64_t seed[4];
111
+ xt_random_fill_seed(seed);
112
+ memcpy(xt_random_state, seed, sizeof(seed));
113
+ xt_random_seeded = 1;
114
+ }
115
+
116
+ uint64_t xt_random_u64(void) {
117
+ if (!xt_random_seeded) xt_random_seed();
118
+ uint64_t *s = xt_random_state;
119
+ uint64_t result = xt_rotate_left(s[1] * 5, 7) * 9;
120
+ uint64_t t = s[1] << 17;
121
+ s[2] ^= s[0];
122
+ s[3] ^= s[1];
123
+ s[1] ^= s[2];
124
+ s[0] ^= s[3];
125
+ s[2] ^= t;
126
+ s[3] = xt_rotate_left(s[3], 45);
127
+ return result;
128
+ }
129
+
130
+ double xt_random_double(void) {
131
+ /* 53 random bits: every double in [0, 1) that JavaScript can represent. */
132
+ return (double)(xt_random_u64() >> 11) * (1.0 / 9007199254740992.0);
133
+ }
134
+
135
+ uint64_t xt_random_below(uint64_t bound) {
136
+ if (bound == 0) return 0;
137
+ /* Rejection sampling removes the modulo bias of `% bound`. */
138
+ uint64_t threshold = (uint64_t)(-bound) % bound;
139
+ for (;;) {
140
+ uint64_t value = xt_random_u64();
141
+ if (value >= threshold) return value % bound;
142
+ }
143
+ }
@@ -160,35 +160,16 @@ static int xt_re_repeat_try(xt_re_repeat *rep, size_t pos, int count, const xt_r
160
160
  static int xt_re_match_piece(xt_re_piece *piece, const xt_re_input *in, size_t pos,
161
161
  xt_re_cont cont, void *ctx, const xt_re_cap *caps) {
162
162
  xt_re_repeat rep;
163
+ if (piece->quant == XT_RE_Q_NONE)
164
+ return xt_re_match_atom(piece->node, in, pos, cont, ctx, caps);
165
+ rep.atom = piece->node;
166
+ rep.in = in;
167
+ rep.min = piece->min;
168
+ rep.max = piece->max;
163
169
  rep.lazy = piece->lazy;
164
- switch (piece->quant) {
165
- case XT_RE_Q_STAR:
166
- rep.atom = piece->node;
167
- rep.in = in;
168
- rep.min = 0;
169
- rep.max = -1;
170
- rep.cont = cont;
171
- rep.ctx = ctx;
172
- return xt_re_repeat_try(&rep, pos, 0, caps);
173
- case XT_RE_Q_PLUS:
174
- rep.atom = piece->node;
175
- rep.in = in;
176
- rep.min = 1;
177
- rep.max = -1;
178
- rep.cont = cont;
179
- rep.ctx = ctx;
180
- return xt_re_repeat_try(&rep, pos, 0, caps);
181
- case XT_RE_Q_QUEST:
182
- rep.atom = piece->node;
183
- rep.in = in;
184
- rep.min = 0;
185
- rep.max = 1;
186
- rep.cont = cont;
187
- rep.ctx = ctx;
188
- return xt_re_repeat_try(&rep, pos, 0, caps);
189
- default:
190
- return xt_re_match_atom(piece->node, in, pos, cont, ctx, caps);
191
- }
170
+ rep.cont = cont;
171
+ rep.ctx = ctx;
172
+ return xt_re_repeat_try(&rep, pos, 0, caps);
192
173
  }
193
174
 
194
175
  typedef struct {
@@ -33,7 +33,8 @@ static xt_re_node *xt_re_new_class(const unsigned char *bits) {
33
33
  return n;
34
34
  }
35
35
 
36
- static void xt_re_push_piece(xt_re_concat *c, xt_re_node *node, int quant, int lazy) {
36
+ static void xt_re_push_piece(xt_re_concat *c, xt_re_node *node, int quant, int lazy, int min,
37
+ int max) {
37
38
  if (c->count == c->capacity) {
38
39
  int next = c->capacity ? c->capacity * 2 : 4;
39
40
  xt_re_piece *grown = (xt_re_piece *)realloc(c->pieces, (size_t)next * sizeof(xt_re_piece));
@@ -44,6 +45,8 @@ static void xt_re_push_piece(xt_re_concat *c, xt_re_node *node, int quant, int l
44
45
  c->pieces[c->count].node = node;
45
46
  c->pieces[c->count].quant = quant;
46
47
  c->pieces[c->count].lazy = lazy;
48
+ c->pieces[c->count].min = min;
49
+ c->pieces[c->count].max = max;
47
50
  c->count++;
48
51
  }
49
52
 
@@ -188,30 +191,90 @@ static xt_re_node *xt_re_parse_atom(xt_re_parser *p) {
188
191
  return xt_re_new_char((unsigned char)c);
189
192
  }
190
193
 
194
+ /** Parse `{n}`, `{n,}` or `{n,m}` starting at `p->i`. On success advances past
195
+ * the closing brace and reports the bounds. A malformed group (or one whose
196
+ * bound is unreasonable) is left untouched so its `{` is matched as a literal,
197
+ * which is what JavaScript does for `a{` and `a{x}`; an inverted range such as
198
+ * `a{2,1}` is treated as literal too rather than rejected. */
199
+ static int xt_re_parse_brace(xt_re_parser *p, int *min, int *max) {
200
+ size_t i = p->i;
201
+ long low = 0;
202
+ long high = 0;
203
+ int have_low = 0;
204
+ int have_high = 0;
205
+ if (p->s[i] != '{') return 0;
206
+ i++;
207
+ while (p->s[i] >= '0' && p->s[i] <= '9') {
208
+ low = low * 10 + (p->s[i] - '0');
209
+ if (low > XT_RE_MAX_REPEAT) return 0;
210
+ have_low = 1;
211
+ i++;
212
+ }
213
+ if (!have_low) return 0;
214
+ if (p->s[i] == '}') {
215
+ i++;
216
+ *min = (int)low;
217
+ *max = (int)low;
218
+ p->i = i;
219
+ return 1;
220
+ }
221
+ if (p->s[i] != ',') return 0;
222
+ i++;
223
+ while (p->s[i] >= '0' && p->s[i] <= '9') {
224
+ high = high * 10 + (p->s[i] - '0');
225
+ if (high > XT_RE_MAX_REPEAT) return 0;
226
+ have_high = 1;
227
+ i++;
228
+ }
229
+ if (p->s[i] != '}') return 0;
230
+ i++;
231
+ if (have_high && high < low) return 0;
232
+ *min = (int)low;
233
+ *max = have_high ? (int)high : -1;
234
+ p->i = i;
235
+ return 1;
236
+ }
237
+
191
238
  static void xt_re_parse_concat(xt_re_parser *p, xt_re_concat *out) {
192
239
  while (p->s[p->i] && p->s[p->i] != '|' && p->s[p->i] != ')') {
193
240
  xt_re_node *node = xt_re_parse_atom(p);
194
241
  int quant = XT_RE_Q_NONE;
195
242
  int lazy = 0;
243
+ int min = 0;
244
+ int max = 0;
196
245
  if (p->error || !node) {
197
246
  p->error = 1;
198
247
  return;
199
248
  }
200
249
  if (p->s[p->i] == '*') {
201
250
  quant = XT_RE_Q_STAR;
251
+ min = 0;
252
+ max = -1;
202
253
  p->i++;
203
254
  } else if (p->s[p->i] == '+') {
204
255
  quant = XT_RE_Q_PLUS;
256
+ min = 1;
257
+ max = -1;
205
258
  p->i++;
206
259
  } else if (p->s[p->i] == '?') {
207
260
  quant = XT_RE_Q_QUEST;
261
+ min = 0;
262
+ max = 1;
208
263
  p->i++;
264
+ } else if (p->s[p->i] == '{') {
265
+ int range_min = 0;
266
+ int range_max = 0;
267
+ if (xt_re_parse_brace(p, &range_min, &range_max)) {
268
+ quant = XT_RE_Q_RANGE;
269
+ min = range_min;
270
+ max = range_max;
271
+ }
209
272
  }
210
273
  if (quant != XT_RE_Q_NONE && p->s[p->i] == '?') {
211
274
  lazy = 1;
212
275
  p->i++;
213
276
  }
214
- xt_re_push_piece(out, node, quant, lazy);
277
+ xt_re_push_piece(out, node, quant, lazy, min, max);
215
278
  }
216
279
  }
217
280
 
@@ -23,9 +23,16 @@ enum {
23
23
  XT_RE_Q_NONE = 0,
24
24
  XT_RE_Q_STAR,
25
25
  XT_RE_Q_PLUS,
26
- XT_RE_Q_QUEST
26
+ XT_RE_Q_QUEST,
27
+ XT_RE_Q_RANGE
27
28
  };
28
29
 
30
+ /** Upper bound for a counted quantifier (`a{0,20000}`): the matcher recurses
31
+ * once per repetition, so an unbounded authored bound could overflow the C
32
+ * stack. A brace group whose count exceeds this is treated as literal text,
33
+ * exactly like any other malformed `{...}`. */
34
+ #define XT_RE_MAX_REPEAT 4096
35
+
29
36
  typedef struct xt_re_alt xt_re_alt;
30
37
 
31
38
  typedef struct {
@@ -40,6 +47,9 @@ typedef struct {
40
47
  xt_re_node *node;
41
48
  int quant;
42
49
  int lazy;
50
+ /** Repetition bounds for the quantifier (`max == -1` means unbounded). */
51
+ int min;
52
+ int max;
43
53
  } xt_re_piece;
44
54
 
45
55
  typedef struct {
@@ -1,11 +1,15 @@
1
1
  /*
2
2
  * xbintsc runtime — minimal POSIX ERE-compatible regular expressions.
3
3
  *
4
- * Windows does not ship <regex.h>, so on `_WIN32` targets we fall back to this
5
- * small self-contained matcher. It implements the subset the runtime uses:
4
+ * The runtime uses this self-contained matcher on every host instead of the C
5
+ * library's <regex.h>. Host implementations disagree on JavaScript-visible
6
+ * details (lazy quantifiers, counted-quantifier limits, malformed `{...}`
7
+ * groups), so one engine keeps a program's output identical everywhere. It
8
+ * implements the subset the runtime uses:
6
9
  *
7
10
  * literals, '.', '^', '$', '[abc]', '[^abc]', '[a-z]', '*', '+', '?',
8
11
  * alternation '|', groups '(...)', and escapes ('\d', '\w', '\s', ...).
12
+ * Counted quantifiers `{n}`, `{n,}` and `{n,m}` accept a lazy `?` suffix.
9
13
  *
10
14
  * Capturing groups are reported through `regmatch_t` the same way POSIX does:
11
15
  * `pmatch[0]` is the whole match and `pmatch[k]` the k-th group in pattern
@@ -158,6 +158,8 @@ static const xt_method_spec XT_DATE_METHODS[] = {
158
158
  {"getUTCFullYear", 0}, {"getUTCMonth", 0}, {"getUTCDate", 0}, {"getUTCDay", 0},
159
159
  {"getUTCHours", 0}, {"getUTCMinutes", 0}, {"getUTCSeconds", 0}, {"getUTCMilliseconds", 0},
160
160
  {"toISOString", 0}, {"toJSON", 1}, {"toString", 0}, {"valueOf", 0},
161
+ {"toLocaleDateString", 0}, {"toLocaleTimeString", 0}, {"toLocaleString", 0},
162
+ {"toDateString", 0}, {"toTimeString", 0}, {"toUTCString", 0},
161
163
  };
162
164
 
163
165
  static const xt_method_spec XT_MAP_METHODS[] = {
@@ -48,7 +48,7 @@ xt_value xt_math_call(xt_value name, int32_t argc, xt_value *argv) {
48
48
  if (isnan(a)) return xt_number(NAN);
49
49
  return xt_number(a > 0 ? 1.0 : a < 0 ? -1.0 : a);
50
50
  }
51
- if (strcmp(fn, "random") == 0) return xt_number((double)rand() / ((double)RAND_MAX + 1.0));
51
+ if (strcmp(fn, "random") == 0) return xt_number(xt_random_double());
52
52
  if (strcmp(fn, "min") == 0) {
53
53
  double result = INFINITY;
54
54
  for (int32_t i = 0; i < argc; i++) {