xbintsc 0.6.28 → 0.7.0

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 (280) hide show
  1. package/dist/src/binder/declarations.d.ts +1 -0
  2. package/dist/src/binder/declarations.js +37 -0
  3. package/dist/src/binder/declarations.js.map +1 -1
  4. package/dist/src/binder/references.js +12 -0
  5. package/dist/src/binder/references.js.map +1 -1
  6. package/dist/src/checker/globals.js +1 -0
  7. package/dist/src/checker/globals.js.map +1 -1
  8. package/dist/src/checker/type-system.d.ts +5 -0
  9. package/dist/src/checker/type-system.js +22 -3
  10. package/dist/src/checker/type-system.js.map +1 -1
  11. package/dist/src/cli/help.d.ts +1 -1
  12. package/dist/src/codegen/generator/calls/access.js +25 -0
  13. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  14. package/dist/src/codegen/generator/calls/invocation.js +13 -0
  15. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  16. package/dist/src/codegen/generator/calls/super.js +3 -4
  17. package/dist/src/codegen/generator/calls/super.js.map +1 -1
  18. package/dist/src/codegen/generator/calls/unwrap.d.ts +18 -0
  19. package/dist/src/codegen/generator/calls/unwrap.js +45 -0
  20. package/dist/src/codegen/generator/calls/unwrap.js.map +1 -0
  21. package/dist/src/codegen/generator/expressions/assignment.js +14 -0
  22. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  23. package/dist/src/codegen/generator/expressions/module-values.d.ts +6 -0
  24. package/dist/src/codegen/generator/expressions/module-values.js +51 -17
  25. package/dist/src/codegen/generator/expressions/module-values.js.map +1 -1
  26. package/dist/src/codegen/generator/expressions/primary.js +9 -5
  27. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  28. package/dist/src/codegen/generator/generator.js +4 -0
  29. package/dist/src/codegen/generator/generator.js.map +1 -1
  30. package/dist/src/codegen/generator/lookup-tables.d.ts +22 -0
  31. package/dist/src/codegen/generator/lookup-tables.js +48 -0
  32. package/dist/src/codegen/generator/lookup-tables.js.map +1 -0
  33. package/dist/src/codegen/generator/module.js +38 -7
  34. package/dist/src/codegen/generator/module.js.map +1 -1
  35. package/dist/src/codegen/generator/state.js +17 -0
  36. package/dist/src/codegen/generator/state.js.map +1 -1
  37. package/dist/src/codegen/generator/tables.d.ts +2 -6
  38. package/dist/src/codegen/generator/tables.js +18 -12
  39. package/dist/src/codegen/generator/tables.js.map +1 -1
  40. package/dist/src/driver/bundler/commonjs.d.ts +9 -2
  41. package/dist/src/driver/bundler/commonjs.js +11 -4
  42. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  43. package/dist/src/driver/bundler/concatenate.d.ts +20 -0
  44. package/dist/src/driver/bundler/concatenate.js +68 -4
  45. package/dist/src/driver/bundler/concatenate.js.map +1 -1
  46. package/dist/src/driver/bundler/deferred.d.ts +29 -1
  47. package/dist/src/driver/bundler/deferred.js +114 -5
  48. package/dist/src/driver/bundler/deferred.js.map +1 -1
  49. package/dist/src/driver/bundler/dynamic-import.d.ts +6 -0
  50. package/dist/src/driver/bundler/dynamic-import.js +59 -3
  51. package/dist/src/driver/bundler/dynamic-import.js.map +1 -1
  52. package/dist/src/driver/bundler/graph.d.ts +13 -0
  53. package/dist/src/driver/bundler/graph.js +2 -2
  54. package/dist/src/driver/bundler/graph.js.map +1 -1
  55. package/dist/src/driver/bundler/merge.js +32 -8
  56. package/dist/src/driver/bundler/merge.js.map +1 -1
  57. package/dist/src/driver/bundler/namespace-object.d.ts +11 -0
  58. package/dist/src/driver/bundler/namespace-object.js +32 -0
  59. package/dist/src/driver/bundler/namespace-object.js.map +1 -1
  60. package/dist/src/driver/bundler/reexport-resolution.js +8 -2
  61. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
  62. package/dist/src/driver/bundler/symbols.d.ts +4 -0
  63. package/dist/src/driver/bundler/symbols.js +14 -1
  64. package/dist/src/driver/bundler/symbols.js.map +1 -1
  65. package/dist/src/driver/config.d.ts +1 -1
  66. package/dist/src/driver/options.d.ts +1 -4
  67. package/dist/src/driver/runtime-lib.d.ts +27 -0
  68. package/dist/src/driver/runtime-lib.js +78 -2
  69. package/dist/src/driver/runtime-lib.js.map +1 -1
  70. package/dist/src/driver/runtime-objects.d.ts +1 -1
  71. package/dist/src/driver/runtime-objects.js +24 -5
  72. package/dist/src/driver/runtime-objects.js.map +1 -1
  73. package/dist/src/driver/version.d.ts +1 -1
  74. package/dist/src/driver/version.js +1 -1
  75. package/dist/src/driver/version.js.map +1 -1
  76. package/dist/src/extensions/node/buffer/index.d.ts +5 -4
  77. package/dist/src/extensions/node/buffer/index.js +5 -4
  78. package/dist/src/extensions/node/buffer/index.js.map +1 -1
  79. package/dist/src/extensions/node/events/index.js +8 -1
  80. package/dist/src/extensions/node/events/index.js.map +1 -1
  81. package/dist/src/extensions/node/fs/callbacks.d.ts +9 -0
  82. package/dist/src/extensions/node/fs/callbacks.js +42 -0
  83. package/dist/src/extensions/node/fs/callbacks.js.map +1 -0
  84. package/dist/src/extensions/node/fs/index.d.ts +2 -3
  85. package/dist/src/extensions/node/fs/index.js +11 -4
  86. package/dist/src/extensions/node/fs/index.js.map +1 -1
  87. package/dist/src/extensions/node/process/index.d.ts +10 -3
  88. package/dist/src/extensions/node/process/index.js +30 -5
  89. package/dist/src/extensions/node/process/index.js.map +1 -1
  90. package/dist/src/extensions/registry.d.ts +7 -0
  91. package/dist/src/extensions/registry.js +13 -4
  92. package/dist/src/extensions/registry.js.map +1 -1
  93. package/dist/src/lexer/scanner.d.ts +7 -1
  94. package/dist/src/lexer/scanner.js +15 -0
  95. package/dist/src/lexer/scanner.js.map +1 -1
  96. package/dist/src/parser/context.d.ts +10 -0
  97. package/dist/src/parser/context.js +42 -3
  98. package/dist/src/parser/context.js.map +1 -1
  99. package/dist/src/parser/literals.js +6 -2
  100. package/dist/src/parser/literals.js.map +1 -1
  101. package/dist/tests/binder/binder.test.js +43 -0
  102. package/dist/tests/binder/binder.test.js.map +1 -1
  103. package/dist/tests/codegen/llvm.test.js +15 -0
  104. package/dist/tests/codegen/llvm.test.js.map +1 -1
  105. package/dist/tests/driver/bundler-resolve.test.js +12 -0
  106. package/dist/tests/driver/bundler-resolve.test.js.map +1 -1
  107. package/dist/tests/driver/commonjs-prelude.test.d.ts +11 -0
  108. package/dist/tests/driver/commonjs-prelude.test.js +69 -0
  109. package/dist/tests/driver/commonjs-prelude.test.js.map +1 -0
  110. package/dist/tests/driver/compiler.test.js +42 -1
  111. package/dist/tests/driver/compiler.test.js.map +1 -1
  112. package/dist/tests/driver/html-deferred-cycles.test.d.ts +10 -0
  113. package/dist/tests/driver/html-deferred-cycles.test.js +75 -0
  114. package/dist/tests/driver/html-deferred-cycles.test.js.map +1 -0
  115. package/dist/tests/driver/namespace-type-exports.test.d.ts +9 -0
  116. package/dist/tests/driver/namespace-type-exports.test.js +59 -0
  117. package/dist/tests/driver/namespace-type-exports.test.js.map +1 -0
  118. package/dist/tests/driver/runtime-lib.test.js +49 -1
  119. package/dist/tests/driver/runtime-lib.test.js.map +1 -1
  120. package/dist/tests/e2e/compile-basics.test.js +53 -0
  121. package/dist/tests/e2e/compile-basics.test.js.map +1 -1
  122. package/dist/tests/e2e/compile-deferred-marker.test.d.ts +14 -0
  123. package/dist/tests/e2e/compile-deferred-marker.test.js +68 -0
  124. package/dist/tests/e2e/compile-deferred-marker.test.js.map +1 -0
  125. package/dist/tests/e2e/compile-deferred-reexport.test.d.ts +12 -0
  126. package/dist/tests/e2e/compile-deferred-reexport.test.js +89 -0
  127. package/dist/tests/e2e/compile-deferred-reexport.test.js.map +1 -0
  128. package/dist/tests/e2e/compile-error-module.test.d.ts +9 -0
  129. package/dist/tests/e2e/compile-error-module.test.js +29 -0
  130. package/dist/tests/e2e/compile-error-module.test.js.map +1 -0
  131. package/dist/tests/e2e/compile-gc-promises.test.d.ts +11 -0
  132. package/dist/tests/e2e/compile-gc-promises.test.js +52 -0
  133. package/dist/tests/e2e/compile-gc-promises.test.js.map +1 -0
  134. package/dist/tests/e2e/compile-modules.test.js +40 -0
  135. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  136. package/dist/tests/e2e/compile-node-fs-callbacks.test.d.ts +6 -0
  137. package/dist/tests/e2e/compile-node-fs-callbacks.test.js +46 -0
  138. package/dist/tests/e2e/compile-node-fs-callbacks.test.js.map +1 -0
  139. package/dist/tests/e2e/compile-node-inheritance.test.d.ts +6 -0
  140. package/dist/tests/e2e/compile-node-inheritance.test.js +48 -0
  141. package/dist/tests/e2e/compile-node-inheritance.test.js.map +1 -0
  142. package/dist/tests/e2e/compile-node-require.test.d.ts +9 -0
  143. package/dist/tests/e2e/compile-node-require.test.js +29 -0
  144. package/dist/tests/e2e/compile-node-require.test.js.map +1 -0
  145. package/dist/tests/e2e/compile-node-stats.test.d.ts +8 -0
  146. package/dist/tests/e2e/compile-node-stats.test.js +26 -0
  147. package/dist/tests/e2e/compile-node-stats.test.js.map +1 -0
  148. package/dist/tests/e2e/compile-proxy.test.d.ts +6 -0
  149. package/dist/tests/e2e/compile-proxy.test.js +66 -0
  150. package/dist/tests/e2e/compile-proxy.test.js.map +1 -0
  151. package/dist/tests/e2e/compile-web-globals.test.d.ts +6 -0
  152. package/dist/tests/e2e/compile-web-globals.test.js +94 -0
  153. package/dist/tests/e2e/compile-web-globals.test.js.map +1 -0
  154. package/dist/tests/e2e/fetch.test.js +1 -1
  155. package/dist/tests/e2e/gui-script-import-order.test.js +24 -0
  156. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  157. package/dist/tests/e2e/json-stringify-to-json.test.d.ts +10 -0
  158. package/dist/tests/e2e/json-stringify-to-json.test.js +61 -0
  159. package/dist/tests/e2e/json-stringify-to-json.test.js.map +1 -0
  160. package/dist/tests/e2e/node-process.test.d.ts +7 -0
  161. package/dist/tests/e2e/node-process.test.js +50 -0
  162. package/dist/tests/e2e/node-process.test.js.map +1 -0
  163. package/dist/tests/e2e/runtime-coverage-buffer.test.js +31 -0
  164. package/dist/tests/e2e/runtime-coverage-buffer.test.js.map +1 -1
  165. package/dist/tests/e2e/web-streams.test.js +44 -29
  166. package/dist/tests/e2e/web-streams.test.js.map +1 -1
  167. package/dist/tests/parser/literals.test.js +19 -0
  168. package/dist/tests/parser/literals.test.js.map +1 -1
  169. package/dist/tests/parser/statements.test.js +13 -0
  170. package/dist/tests/parser/statements.test.js.map +1 -1
  171. package/doc/ai/cli.md +1 -0
  172. package/doc/ai/zh-CN/cli.md +1 -0
  173. package/doc/implemented/binder.md +2 -1
  174. package/doc/implemented/checker.md +5 -0
  175. package/doc/implemented/lexer.md +1 -1
  176. package/doc/implemented/modules.md +23 -1
  177. package/doc/implemented/node/buffer.md +18 -5
  178. package/doc/implemented/node/fs.md +22 -1
  179. package/doc/implemented/node/process.md +65 -5
  180. package/doc/implemented/node.md +7 -1
  181. package/doc/implemented/operators.md +3 -1
  182. package/doc/implemented/parser.md +1 -1
  183. package/doc/implemented/runtime-core.md +6 -2
  184. package/doc/implemented/stdlib.md +69 -2
  185. package/doc/unimplemented/classes.md +1 -0
  186. package/doc/unimplemented/modules.md +1 -0
  187. package/doc/unimplemented/stdlib.md +2 -1
  188. package/doc/xbintsc.config.schema.json +5 -0
  189. package/doc/zh-CN/implemented/binder.md +2 -1
  190. package/doc/zh-CN/implemented/checker.md +3 -0
  191. package/doc/zh-CN/implemented/lexer.md +1 -1
  192. package/doc/zh-CN/implemented/modules.md +17 -1
  193. package/doc/zh-CN/implemented/node/buffer.md +3 -1
  194. package/doc/zh-CN/implemented/node/fs.md +19 -1
  195. package/doc/zh-CN/implemented/node/process.md +59 -5
  196. package/doc/zh-CN/implemented/node.md +1 -1
  197. package/doc/zh-CN/implemented/operators.md +2 -1
  198. package/doc/zh-CN/implemented/parser.md +1 -1
  199. package/doc/zh-CN/implemented/runtime-core.md +3 -2
  200. package/doc/zh-CN/implemented/stdlib.md +58 -2
  201. package/doc/zh-CN/unimplemented/classes.md +1 -0
  202. package/doc/zh-CN/unimplemented/modules.md +1 -0
  203. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  204. package/package.json +7 -7
  205. package/runtime/ext_node/buffer/buffer.c +5 -4
  206. package/runtime/ext_node/buffer/parts/byte-access.inc +115 -11
  207. package/runtime/ext_node/buffer/parts/prototype.inc +4 -1
  208. package/runtime/ext_node/events/events.c +9 -0
  209. package/runtime/ext_node/fs/callbacks.c +138 -0
  210. package/runtime/ext_node/fs/fs_ops.c +28 -15
  211. package/runtime/ext_node/module/module.c +18 -9
  212. package/runtime/ext_node/process/process.c +117 -32
  213. package/runtime/ext_node/process/streams.c +370 -0
  214. package/runtime/ext_node/process/streams.h +20 -0
  215. package/runtime/rt.h +36 -0
  216. package/runtime/rt_internal.h +10 -10
  217. package/runtime/xt_alloc.c +67 -49
  218. package/runtime/xt_async_io.c +3 -0
  219. package/runtime/xt_builtins.c +26 -23
  220. package/runtime/xt_constructors.c +101 -2
  221. package/runtime/xt_containers/access.inc +29 -1
  222. package/runtime/xt_containers/function_prototypes.inc +58 -0
  223. package/runtime/xt_containers/objects.inc +18 -2
  224. package/runtime/xt_containers.c +1 -0
  225. package/runtime/xt_exit.c +34 -0
  226. package/runtime/xt_fetch/headers.inc +1 -0
  227. package/runtime/xt_fetch/response.inc +28 -8
  228. package/runtime/xt_fetch/webapi.inc +82 -0
  229. package/runtime/xt_fetch.c +5 -0
  230. package/runtime/xt_gc_verify.c +135 -0
  231. package/runtime/xt_intl.c +224 -0
  232. package/runtime/xt_io/exception.inc +40 -1
  233. package/runtime/xt_module_values.c +82 -0
  234. package/runtime/xt_proxy.c +140 -0
  235. package/runtime/xt_stdlib/dispatch.inc +98 -9
  236. package/runtime/xt_stdlib2/date.inc +7 -1
  237. package/runtime/xt_stdlib2/json.inc +28 -0
  238. package/runtime/xt_stdlib2/json_stringify.inc +70 -53
  239. package/runtime/xt_web.h +27 -0
  240. package/runtime/xt_web_abort.c +373 -0
  241. package/scripts/build-runtime.ts +8 -0
  242. package/src/binder/declarations.ts +34 -0
  243. package/src/binder/references.ts +12 -0
  244. package/src/checker/globals.ts +1 -0
  245. package/src/checker/type-system.ts +23 -3
  246. package/src/codegen/generator/calls/access.ts +23 -0
  247. package/src/codegen/generator/calls/invocation.ts +12 -0
  248. package/src/codegen/generator/calls/super.ts +5 -4
  249. package/src/codegen/generator/calls/unwrap.ts +47 -0
  250. package/src/codegen/generator/expressions/assignment.ts +15 -0
  251. package/src/codegen/generator/expressions/module-values.ts +57 -19
  252. package/src/codegen/generator/expressions/primary.ts +9 -9
  253. package/src/codegen/generator/generator.ts +4 -0
  254. package/src/codegen/generator/lookup-tables.ts +63 -0
  255. package/src/codegen/generator/module.ts +38 -7
  256. package/src/codegen/generator/state.ts +17 -0
  257. package/src/codegen/generator/tables.ts +18 -12
  258. package/src/driver/bundler/commonjs.ts +12 -5
  259. package/src/driver/bundler/concatenate.ts +84 -7
  260. package/src/driver/bundler/deferred.ts +122 -6
  261. package/src/driver/bundler/dynamic-import.ts +75 -2
  262. package/src/driver/bundler/graph.ts +2 -2
  263. package/src/driver/bundler/merge.ts +31 -5
  264. package/src/driver/bundler/namespace-object.ts +44 -0
  265. package/src/driver/bundler/reexport-resolution.ts +12 -2
  266. package/src/driver/bundler/symbols.ts +17 -0
  267. package/src/driver/config.ts +1 -1
  268. package/src/driver/options.ts +1 -4
  269. package/src/driver/runtime-lib.ts +87 -2
  270. package/src/driver/runtime-objects.ts +24 -5
  271. package/src/driver/version.ts +1 -1
  272. package/src/extensions/node/buffer/index.ts +5 -4
  273. package/src/extensions/node/events/index.ts +8 -1
  274. package/src/extensions/node/fs/callbacks.ts +45 -0
  275. package/src/extensions/node/fs/index.ts +11 -4
  276. package/src/extensions/node/process/index.ts +30 -5
  277. package/src/extensions/registry.ts +21 -4
  278. package/src/lexer/scanner.ts +16 -1
  279. package/src/parser/context.ts +39 -3
  280. package/src/parser/literals.ts +6 -2
@@ -0,0 +1,34 @@
1
+ /*
2
+ * xbintsc runtime — program termination.
3
+ *
4
+ * The generated `main` runs the module body, drains microtasks, pumps the
5
+ * reactor and then calls `xt_finish()`, which gives the host one last chance to
6
+ * run JavaScript before the process ends: the Node extension registers a hook
7
+ * that fires the `process` "exit" listeners there. `xt_finish` also returns the
8
+ * status the program asked for, because the generated `main` no longer returns a
9
+ * constant — `process.exitCode = 1` (and `process.exit(1)`, which ends up in the
10
+ * same place) reaches the parent process.
11
+ *
12
+ * The hook runs at most once: `process.exit()` calls `xt_finish()` itself before
13
+ * `exit()`, so the terminating call cannot fire the listeners a second time.
14
+ */
15
+
16
+ #include "rt.h"
17
+
18
+ static int xt_exit_status_value = 0;
19
+ static xt_exit_hook xt_exit_hook_value = NULL;
20
+ static int xt_exit_hook_done = 0;
21
+
22
+ void xt_set_exit_status(int status) { xt_exit_status_value = status; }
23
+
24
+ int xt_get_exit_status(void) { return xt_exit_status_value; }
25
+
26
+ void xt_at_exit(xt_exit_hook hook) { xt_exit_hook_value = hook; }
27
+
28
+ int xt_finish(void) {
29
+ if (!xt_exit_hook_done) {
30
+ xt_exit_hook_done = 1;
31
+ if (xt_exit_hook_value) xt_exit_hook_value();
32
+ }
33
+ return xt_exit_status_value;
34
+ }
@@ -206,6 +206,7 @@ static xt_value xt_headers_method_to_string(xt_value thisValue, xt_value env, in
206
206
 
207
207
  static xt_value xt_headers_new(void) {
208
208
  xt_value headers = xt_object_new();
209
+ xt_object_set_prototype(headers, xt_web_headers_proto());
209
210
  xt_object_set(headers, xt_string_from_cstr("__map"), xt_object_new());
210
211
  xt_fetch_set_method(headers, "get", (void *)xt_headers_method_get);
211
212
  xt_fetch_set_method(headers, "has", (void *)xt_headers_method_has);
@@ -8,6 +8,9 @@
8
8
  * though the value is already available.
9
9
  */
10
10
 
11
+ /* Typed-array constructor (xt_typed_array.c); `bytes()` wraps the body buffer. */
12
+ xt_value xt_ta_u8_ctor(int32_t argc, xt_value *argv);
13
+
11
14
  static void *xt_response_method_impl(xt_value name);
12
15
  static xt_value xt_method_value_for_response(xt_value name);
13
16
 
@@ -31,19 +34,34 @@ static xt_value xt_response_method_json(xt_value thisValue, xt_value env, int32_
31
34
  return xt_promise_resolve(xt_json_parse(1, &body));
32
35
  }
33
36
 
34
- static xt_value xt_response_method_array_buffer(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
35
- (void)env;
36
- (void)argc;
37
- (void)argv;
37
+ /* The body as a real `ArrayBuffer` (the platform type), built from the buffered
38
+ * bytes. `bytes()` wraps the same buffer in a `Uint8Array`. */
39
+ static xt_value xt_response_body_buffer(xt_value thisValue) {
38
40
  xt_value body = xt_response_body(thisValue);
39
41
  const char *data = xt_string_data(body);
40
42
  int32_t length = xt_string_length_value(body);
41
43
  if (!data || length < 0) length = 0;
42
- xt_value bytes = xt_array_new(0, NULL);
44
+ xt_value lengthValue = xt_number((double)length);
45
+ xt_value buffer = xt_array_buffer_ctor(1, &lengthValue);
43
46
  for (int32_t i = 0; i < length; i++) {
44
- xt_array_push(bytes, xt_number((double)(unsigned char)data[i]));
47
+ xt_array_buffer_set_byte(buffer, (uint32_t)i, (uint8_t)data[i]);
45
48
  }
46
- return xt_promise_resolve(bytes);
49
+ return buffer;
50
+ }
51
+
52
+ static xt_value xt_response_method_array_buffer(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
53
+ (void)env;
54
+ (void)argc;
55
+ (void)argv;
56
+ return xt_promise_resolve(xt_response_body_buffer(thisValue));
57
+ }
58
+
59
+ static xt_value xt_response_method_bytes(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
60
+ (void)env;
61
+ (void)argc;
62
+ (void)argv;
63
+ xt_value buffer = xt_response_body_buffer(thisValue);
64
+ return xt_promise_resolve(xt_ta_u8_ctor(1, &buffer));
47
65
  }
48
66
 
49
67
  static xt_value xt_response_method_clone(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
@@ -51,6 +69,7 @@ static xt_value xt_response_method_clone(xt_value thisValue, xt_value env, int32
51
69
  (void)argc;
52
70
  (void)argv;
53
71
  xt_value clone = xt_object_new();
72
+ xt_object_set_prototype(clone, xt_web_response_proto());
54
73
  xt_value keys = xt_object_keys(thisValue);
55
74
  int32_t count = (int32_t)xt_to_number(xt_array_length(keys));
56
75
  for (int32_t i = 0; i < count; i++) {
@@ -101,7 +120,8 @@ static xt_value xt_response_new(xt_value headers, int status, const char *status
101
120
  xt_fetch_set_method(response, "text", (void *)xt_response_method_text);
102
121
  xt_fetch_set_method(response, "json", (void *)xt_response_method_json);
103
122
  xt_fetch_set_method(response, "arrayBuffer", (void *)xt_response_method_array_buffer);
104
- xt_fetch_set_method(response, "bytes", (void *)xt_response_method_array_buffer);
123
+ xt_fetch_set_method(response, "bytes", (void *)xt_response_method_bytes);
105
124
  xt_fetch_set_method(response, "clone", (void *)xt_response_method_clone);
125
+ xt_object_set_prototype(response, xt_web_response_proto());
106
126
  return response;
107
127
  }
@@ -0,0 +1,82 @@
1
+ /*
2
+ * xbintsc runtime — the `Headers` and `Response` globals.
3
+ *
4
+ * `fetch` builds both as ordinary objects with own method closures; these
5
+ * constructors reuse the same builders and chain the objects onto a shared
6
+ * prototype, so `new Headers(...)` / `new Response(...)` work, the platform
7
+ * methods are reachable through the prototype, and `instanceof` holds for
8
+ * fetch's own objects too.
9
+ */
10
+
11
+ /* -- Headers -------------------------------------------------------------- */
12
+
13
+ xt_value xt_web_headers_proto(void) {
14
+ static xt_value proto = XT_UNDEFINED;
15
+ if (proto != XT_UNDEFINED) return proto;
16
+ proto = xt_object_new();
17
+ xt_gc_add_root(&proto);
18
+ xt_fetch_set_method(proto, "get", (void *)xt_headers_method_get);
19
+ xt_fetch_set_method(proto, "has", (void *)xt_headers_method_has);
20
+ xt_fetch_set_method(proto, "set", (void *)xt_headers_method_set);
21
+ xt_fetch_set_method(proto, "append", (void *)xt_headers_method_append);
22
+ xt_fetch_set_method(proto, "delete", (void *)xt_headers_method_delete);
23
+ xt_fetch_set_method(proto, "forEach", (void *)xt_headers_method_forEach);
24
+ xt_fetch_set_method(proto, "keys", (void *)xt_headers_method_keys);
25
+ xt_fetch_set_method(proto, "values", (void *)xt_headers_method_values);
26
+ xt_fetch_set_method(proto, "entries", (void *)xt_headers_method_entries);
27
+ xt_fetch_set_method(proto, "getSetCookie", (void *)xt_headers_method_get_set_cookie);
28
+ xt_fetch_set_method(proto, "toString", (void *)xt_headers_method_to_string);
29
+ return proto;
30
+ }
31
+
32
+ xt_value xt_headers_ctor(int32_t argc, xt_value *argv) {
33
+ xt_value headers = xt_headers_from_source(xt_arg(argc, argv, 0));
34
+ xt_object_set_prototype(headers, xt_web_headers_proto());
35
+ return headers;
36
+ }
37
+
38
+ /* -- Response ------------------------------------------------------------- */
39
+
40
+ xt_value xt_web_response_proto(void) {
41
+ static xt_value proto = XT_UNDEFINED;
42
+ if (proto != XT_UNDEFINED) return proto;
43
+ proto = xt_object_new();
44
+ xt_gc_add_root(&proto);
45
+ xt_fetch_set_method(proto, "text", (void *)xt_response_method_text);
46
+ xt_fetch_set_method(proto, "json", (void *)xt_response_method_json);
47
+ xt_fetch_set_method(proto, "arrayBuffer", (void *)xt_response_method_array_buffer);
48
+ xt_fetch_set_method(proto, "bytes", (void *)xt_response_method_bytes);
49
+ xt_fetch_set_method(proto, "clone", (void *)xt_response_method_clone);
50
+ return proto;
51
+ }
52
+
53
+ /* The body bytes of a `BodyInit`: strings keep their bytes, anything else goes
54
+ * through the ordinary string conversion (Blob / FormData bodies are not
55
+ * modelled yet). */
56
+ static void xt_web_body_bytes(xt_value body, const char **data, size_t *length) {
57
+ *data = "";
58
+ *length = 0;
59
+ if (body == XT_UNDEFINED || body == XT_NULL) return;
60
+ xt_value text = XT_IS_STRING(body) ? body : xt_to_string(body);
61
+ *data = xt_string_data(text);
62
+ *length = (size_t)xt_string_length_value(text);
63
+ }
64
+
65
+ xt_value xt_response_ctor(int32_t argc, xt_value *argv) {
66
+ xt_value body = xt_arg(argc, argv, 0);
67
+ xt_value init = xt_arg(argc, argv, 1);
68
+ int status = 200;
69
+ const char *statusText = "";
70
+ xt_value headers = XT_UNDEFINED;
71
+ if (XT_IS_OBJECT(init)) {
72
+ headers = xt_object_get_cstr(init, "headers");
73
+ xt_value statusValue = xt_object_get_cstr(init, "status");
74
+ if (XT_IS_NUMBER(statusValue)) status = (int)xt_to_number(statusValue);
75
+ xt_value textValue = xt_object_get_cstr(init, "statusText");
76
+ if (XT_IS_STRING(textValue)) statusText = xt_string_data(textValue);
77
+ }
78
+ const char *data = "";
79
+ size_t length = 0;
80
+ xt_web_body_bytes(body, &data, &length);
81
+ return xt_response_new(xt_headers_from_source(headers), status, statusText, "", data, length);
82
+ }
@@ -35,6 +35,10 @@ typedef socklen_t xt_fetch_socklen_t;
35
35
  #define xt_fetch_close_socket close
36
36
  #endif
37
37
 
38
+ /* Prototypes of the `Headers` / `Response` globals (defined in webapi.inc). */
39
+ xt_value xt_web_headers_proto(void);
40
+ xt_value xt_web_response_proto(void);
41
+
38
42
  #include "xt_fetch/buffer.inc"
39
43
  #include "xt_fetch/url.inc"
40
44
  #include "xt_fetch/socket.inc"
@@ -42,3 +46,4 @@ typedef socklen_t xt_fetch_socklen_t;
42
46
  #include "xt_fetch/response.inc"
43
47
  #include "xt_fetch/request.inc"
44
48
  #include "xt_fetch/async.inc"
49
+ #include "xt_fetch/webapi.inc"
@@ -0,0 +1,135 @@
1
+ /*
2
+ * `XT_GC_VERIFY=1`: check that the collector keeps every object a live object
3
+ * points at.
4
+ *
5
+ * After the mark phase and *before* the sweep, walk every marked object's
6
+ * payload conservatively (the same way the collector scans the stack) and
7
+ * report each heap block it references that is not marked. A hit means either
8
+ * the tracer for that object kind skips a field, or the referenced block was
9
+ * never reachable from a root — both are the class of bug that ends in a
10
+ * use-after-free far away from the collection.
11
+ *
12
+ * Debug-only: the walk is O(live objects × payload) and allocates nothing.
13
+ */
14
+
15
+ #include "rt_internal.h"
16
+
17
+ #include <stdio.h>
18
+ #include <stdlib.h>
19
+ #include <string.h>
20
+
21
+ /** Lookup a raw address in the collector's table (xt_alloc.c). */
22
+ xt_header *xt_gc_lookup_raw(uintptr_t address);
23
+
24
+ static const char *xt_gc_kind_name(uint8_t kind) {
25
+ switch (kind) {
26
+ case XT_OBJECT_KIND_STRING: return "string";
27
+ case XT_OBJECT_KIND_OBJECT: return "object";
28
+ case XT_OBJECT_KIND_ARRAY: return "array";
29
+ case XT_OBJECT_KIND_FUNCTION: return "function";
30
+ case XT_OBJECT_KIND_PROMISE: return "promise";
31
+ case XT_OBJECT_KIND_MAP: return "map";
32
+ case XT_OBJECT_KIND_SET: return "set";
33
+ case XT_OBJECT_KIND_DATE: return "date";
34
+ case XT_OBJECT_KIND_REGEXP: return "regexp";
35
+ case XT_OBJECT_KIND_SYMBOL: return "symbol";
36
+ case XT_OBJECT_KIND_BIGINT: return "bigint";
37
+ case XT_OBJECT_KIND_ERROR: return "error";
38
+ case XT_OBJECT_KIND_GENERATOR: return "generator";
39
+ case XT_OBJECT_KIND_ITERATOR: return "iterator";
40
+ case XT_OBJECT_KIND_ASYNC: return "async";
41
+ case XT_OBJECT_KIND_ASYNC_ITERATOR: return "async-iterator";
42
+ case XT_OBJECT_KIND_WEAK_MAP: return "weakmap";
43
+ case XT_OBJECT_KIND_WEAK_SET: return "weakset";
44
+ case XT_OBJECT_KIND_WEAK_REF: return "weakref";
45
+ default: return "?";
46
+ }
47
+ }
48
+
49
+ /** Does `word` name a heap block (raw pointer or NaN-boxed value)? */
50
+ static xt_header *xt_gc_verify_word(uint64_t word) {
51
+ if (XT_IS_STRING(word) || XT_IS_OBJECT(word) || XT_IS_ARRAY(word) || XT_IS_FUNCTION(word) ||
52
+ XT_IS_BIGINT(word)) {
53
+ return xt_gc_lookup_raw((uintptr_t)XT_GET_PTR(word));
54
+ }
55
+ return xt_gc_lookup_raw((uintptr_t)word);
56
+ }
57
+
58
+ void xt_gc_verify_payloads(void) {
59
+ size_t misses = 0;
60
+ for (xt_header *header = xt_gc_heap_head(); header != NULL; header = header->gc_next) {
61
+ if ((header->flags & XT_GC_MARK) == 0) continue;
62
+ if (header->size <= sizeof(xt_header)) continue;
63
+ const uint64_t *words = (const uint64_t *)((const char *)header + sizeof(xt_header));
64
+ size_t count = (header->size - sizeof(xt_header)) / sizeof(uint64_t);
65
+ for (size_t i = 0; i < count; i++) {
66
+ xt_header *target = xt_gc_verify_word(words[i]);
67
+ if (target == NULL || (target->flags & XT_GC_MARK) != 0) continue;
68
+ fprintf(stderr,
69
+ "xbintsc: gc verify: %s(%u bytes) at word %zu references unmarked %s(%u bytes)\n",
70
+ xt_gc_kind_name(header->kind), (unsigned)header->size, i,
71
+ xt_gc_kind_name(target->kind), (unsigned)target->size);
72
+ if (++misses >= 8) {
73
+ fprintf(stderr, "xbintsc: gc verify: stopping after 8 reports\n");
74
+ return;
75
+ }
76
+ }
77
+ }
78
+ if (misses == 0) fprintf(stderr, "xbintsc: gc verify: ok\n");
79
+ }
80
+
81
+ /* -- freed-block ring, for crash triage ----------------------------------- */
82
+
83
+ /* Every block the sweep releases is recorded here: a crash handler that knows
84
+ * the faulting address can say whether it was inside a freed block. */
85
+
86
+ typedef struct {
87
+ uintptr_t address;
88
+ size_t size;
89
+ uint8_t kind;
90
+ } xt_gc_freed_entry;
91
+
92
+ #define XT_GC_FREED_RING 512
93
+ static xt_gc_freed_entry g_freed[XT_GC_FREED_RING];
94
+ static size_t g_freed_count = 0;
95
+
96
+ void xt_gc_debug_record_free(uintptr_t address, size_t size, int kind) {
97
+ xt_gc_freed_entry *entry = &g_freed[g_freed_count % XT_GC_FREED_RING];
98
+ entry->address = address;
99
+ entry->size = size;
100
+ entry->kind = (uint8_t)kind;
101
+ g_freed_count++;
102
+ }
103
+
104
+ /** Describe `address` if it falls inside a block the sweep released. */
105
+ int xt_gc_debug_describe(uintptr_t address, char *out, size_t capacity) {
106
+ size_t live = g_freed_count < XT_GC_FREED_RING ? g_freed_count : XT_GC_FREED_RING;
107
+ {
108
+ xt_header *live_block = xt_gc_lookup_raw(address);
109
+ snprintf(out, capacity,
110
+ " fault address 0x%llx (%zu frees recorded)%s\n",
111
+ (unsigned long long)address, g_freed_count,
112
+ live_block ? ", inside a LIVE block" : ", not in the live heap");
113
+ if (live_block) return 1;
114
+ }
115
+ for (size_t i = 0; i < live; i++) {
116
+ const xt_gc_freed_entry *entry = &g_freed[i];
117
+ if (address < entry->address || address >= entry->address + entry->size) continue;
118
+ snprintf(out, capacity,
119
+ " fault address 0x%llx is +0x%llx inside a %s block of %u bytes that the "
120
+ "collector freed (%zu frees ago)\n",
121
+ (unsigned long long)address, (unsigned long long)(address - entry->address),
122
+ xt_gc_kind_name(entry->kind), (unsigned)entry->size, i);
123
+ return 1;
124
+ }
125
+ return 0;
126
+ }
127
+
128
+ /** A typed pointer that is not a live block: name it under `XT_GC_VERIFY=1`. */
129
+ void xt_gc_verify_rejected(uintptr_t address) {
130
+ static int reported = 0;
131
+ if (getenv("XT_GC_VERIFY") == NULL) return;
132
+ if (reported++ >= 8) return;
133
+ fprintf(stderr, "xbintsc: gc verify: skipped stale typed pointer 0x%llx\n",
134
+ (unsigned long long)address);
135
+ }
@@ -0,0 +1,224 @@
1
+ /*
2
+ * xbintsc runtime — the `Intl` global object.
3
+ *
4
+ * Only `Intl.Segmenter` is provided: the TUI subdivides strings into grapheme
5
+ * clusters (terminal width, truncation) and words (cursor movement), and both
6
+ * segmenters are constructed at module load. ICU is not linked, so the
7
+ * segmentation is a documented approximation of UAX #29:
8
+ *
9
+ * - grapheme: a code point plus the marks, variation selectors, emoji
10
+ * modifiers and ZWJ sequences that attach to it, and paired regional
11
+ * indicators (flags);
12
+ * - word: runs of whitespace, word characters and punctuation.
13
+ *
14
+ * `segment(text)` returns an **array** of `{ segment, index, isWordLike }`.
15
+ * Node returns an iterable of the same records; every consumer in the
16
+ * ecosystem spreads or iterates it, so the array is a compatible subset.
17
+ *
18
+ * Locale arguments are ignored, and `resolvedOptions()` reports `en`.
19
+ */
20
+
21
+ #include "rt_internal.h"
22
+
23
+ #include <stdio.h>
24
+ #include <stdlib.h>
25
+ #include <string.h>
26
+
27
+ static xt_value xt_intl_object_value = 0;
28
+
29
+ /* -- code point classification -------------------------------------------- */
30
+
31
+ #define XT_CP_ZWJ 0x200D
32
+
33
+ static int xt_cp_is_mark(int32_t cp) {
34
+ return (cp >= 0x0300 && cp <= 0x036f) || (cp >= 0x1ab0 && cp <= 0x1aff) ||
35
+ (cp >= 0x1dc0 && cp <= 0x1dff) || (cp >= 0x20d0 && cp <= 0x20ff) ||
36
+ (cp >= 0xfe20 && cp <= 0xfe2f) || (cp >= 0x0900 && cp <= 0x0903) ||
37
+ (cp >= 0x093a && cp <= 0x094f) || (cp >= 0x0e31 && cp <= 0x0e3a) ||
38
+ (cp >= 0x0e47 && cp <= 0x0e4e) || (cp >= 0x3099 && cp <= 0x309a);
39
+ }
40
+
41
+ static int xt_cp_is_variation_selector(int32_t cp) {
42
+ return (cp >= 0xfe00 && cp <= 0xfe0f) || (cp >= 0xe0100 && cp <= 0xe01ef);
43
+ }
44
+
45
+ static int xt_cp_is_emoji_modifier(int32_t cp) { return cp >= 0x1f3fb && cp <= 0x1f3ff; }
46
+
47
+ static int xt_cp_is_regional_indicator(int32_t cp) { return cp >= 0x1f1e6 && cp <= 0x1f1ff; }
48
+
49
+ static int xt_cp_is_whitespace(int32_t cp) {
50
+ return cp == 0x20 || (cp >= 0x09 && cp <= 0x0d) || cp == 0xa0 || cp == 0x1680 ||
51
+ (cp >= 0x2000 && cp <= 0x200a) || cp == 0x2028 || cp == 0x2029 || cp == 0x202f ||
52
+ cp == 0x205f || cp == 0x3000;
53
+ }
54
+
55
+ static int xt_cp_is_ascii_word(int32_t cp) {
56
+ return (cp >= '0' && cp <= '9') || (cp >= 'a' && cp <= 'z') || (cp >= 'A' && cp <= 'Z') || cp == '_';
57
+ }
58
+
59
+ /* Non-ASCII letters are word characters for segmentation purposes; CJK,
60
+ * Cyrillic, Greek and accents all land here. Ranges that are clearly symbols
61
+ * (arrows, emoji, box drawing) are not. */
62
+ static int xt_cp_is_word(int32_t cp) {
63
+ if (xt_cp_is_ascii_word(cp)) return 1;
64
+ if (cp < 0x80) return 0;
65
+ if (xt_cp_is_whitespace(cp) || xt_cp_is_mark(cp)) return 0;
66
+ if (cp >= 0x1f000) return 0;
67
+ if (cp >= 0x2190 && cp <= 0x2bff) return 0;
68
+ if (cp >= 0x2500 && cp <= 0x259f) return 0;
69
+ if (cp >= 0x3000 && cp <= 0x303f) return 0;
70
+ return 1;
71
+ }
72
+
73
+ /* -- segment extraction ---------------------------------------------------- */
74
+
75
+ /** The text of `[start, end)` (UTF-16 units) as a JavaScript string. */
76
+ static xt_value xt_segment_text(xt_string *text, int32_t start, int32_t end) {
77
+ size_t capacity = (size_t)(end - start + 1) * 4;
78
+ char *buffer = (char *)malloc(capacity);
79
+ if (!buffer) return xt_string_from_cstr("");
80
+ size_t length = 0;
81
+ for (int32_t index = start; index < end;) {
82
+ int32_t code = xt_string_code_point_at(text, index);
83
+ if (code < 0) break;
84
+ index += code > 0xffff ? 2 : 1;
85
+ length += xt_utf8_encode((uint32_t)code, buffer + length);
86
+ }
87
+ xt_value segment = xt_string_new(buffer, length);
88
+ free(buffer);
89
+ return segment;
90
+ }
91
+
92
+ static void xt_segment_push(xt_value array, xt_string *text, int32_t start, int32_t end, int word_like) {
93
+ if (end <= start) return;
94
+ xt_value record = xt_object_new();
95
+ xt_object_set(record, xt_string_from_cstr("segment"), xt_segment_text(text, start, end));
96
+ xt_object_set(record, xt_string_from_cstr("index"), xt_number((double)start));
97
+ xt_object_set(record, xt_string_from_cstr("isWordLike"), xt_bool(word_like));
98
+ xt_array_push(array, record);
99
+ }
100
+
101
+ /* True when `code` attaches to the preceding cluster instead of starting one. */
102
+ static int xt_cp_extends_cluster(int32_t code) {
103
+ return xt_cp_is_mark(code) || xt_cp_is_variation_selector(code) || xt_cp_is_emoji_modifier(code) ||
104
+ code == XT_CP_ZWJ;
105
+ }
106
+
107
+ static xt_value xt_segment_graphemes(xt_string *text) {
108
+ xt_value array = xt_array_new(0, NULL);
109
+ int32_t units = (int32_t)xt_string_utf16_length(text);
110
+ int32_t start = 0;
111
+ int32_t index = 0;
112
+ int32_t cluster_codepoints = 0;
113
+ int32_t last_code = 0;
114
+ while (index < units) {
115
+ int32_t code = xt_string_code_point_at(text, index);
116
+ if (code < 0) break;
117
+ int32_t width = code > 0xffff ? 2 : 1;
118
+ int extends = index > start && xt_cp_extends_cluster(code);
119
+ /* A pair of regional indicators is one flag. */
120
+ if (!extends && index > start && xt_cp_is_regional_indicator(code) &&
121
+ xt_cp_is_regional_indicator(last_code) && cluster_codepoints == 1) {
122
+ extends = 1;
123
+ }
124
+ /* A ZWJ consumes the code point that follows it (emoji sequences). */
125
+ if (!extends && index > start && last_code == XT_CP_ZWJ) extends = 1;
126
+ if (!extends && index > start) {
127
+ xt_segment_push(array, text, start, index, 0);
128
+ start = index;
129
+ cluster_codepoints = 0;
130
+ }
131
+ cluster_codepoints++;
132
+ last_code = code;
133
+ index += width;
134
+ }
135
+ xt_segment_push(array, text, start, units, 0);
136
+ return array;
137
+ }
138
+
139
+ static xt_value xt_segment_words(xt_string *text) {
140
+ xt_value array = xt_array_new(0, NULL);
141
+ int32_t units = (int32_t)xt_string_utf16_length(text);
142
+ int32_t start = 0;
143
+ int32_t index = 0;
144
+ int class_of_run = -1; /* 0 punctuation, 1 word, 2 whitespace */
145
+ while (index < units) {
146
+ int32_t code = xt_string_code_point_at(text, index);
147
+ if (code < 0) break;
148
+ int32_t width = code > 0xffff ? 2 : 1;
149
+ int kind = xt_cp_is_whitespace(code) ? 2 : (xt_cp_is_word(code) ? 1 : 0);
150
+ /* Marks continue whatever run they land in. */
151
+ if (xt_cp_is_mark(code) && class_of_run >= 0) kind = class_of_run;
152
+ if (class_of_run >= 0 && kind != class_of_run) {
153
+ xt_segment_push(array, text, start, index, class_of_run == 1);
154
+ start = index;
155
+ }
156
+ class_of_run = kind;
157
+ index += width;
158
+ }
159
+ xt_segment_push(array, text, start, units, class_of_run == 1);
160
+ return array;
161
+ }
162
+
163
+ /* -- segmenter objects ---------------------------------------------------- */
164
+
165
+ static const char *xt_segmenter_granularity(xt_value segmenter) {
166
+ xt_value value = xt_object_get_cstr(segmenter, "__granularity");
167
+ const char *granularity = XT_IS_STRING(value) ? xt_string_data(value) : NULL;
168
+ return granularity ? granularity : "grapheme";
169
+ }
170
+
171
+ static xt_value xt_intl_segment(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
172
+ (void)env;
173
+ const char *granularity = xt_segmenter_granularity(thisValue);
174
+ xt_value input = xt_arg(argc, argv, 0);
175
+ xt_string *text = xt_as_string(xt_to_string(input));
176
+ if (!text) return xt_array_new(0, NULL);
177
+ if (strcmp(granularity, "word") == 0) return xt_segment_words(text);
178
+ if (strcmp(granularity, "sentence") == 0) return xt_segment_words(text);
179
+ return xt_segment_graphemes(text);
180
+ }
181
+
182
+ static xt_value xt_intl_resolved_options(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
183
+ (void)env;
184
+ (void)argc;
185
+ (void)argv;
186
+ xt_value options = xt_object_new();
187
+ xt_object_set(options, xt_string_from_cstr("locale"), xt_string_from_cstr("en"));
188
+ xt_object_set(options, xt_string_from_cstr("granularity"),
189
+ xt_string_from_cstr(xt_segmenter_granularity(thisValue)));
190
+ return options;
191
+ }
192
+
193
+ /* `new Intl.Segmenter(locale, { granularity })` returns the segmenter object
194
+ * itself; `xt_new` keeps it because a constructor that returns an object wins. */
195
+ static xt_value xt_intl_segmenter_ctor(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
196
+ (void)thisValue;
197
+ (void)env;
198
+ const char *granularity = "grapheme";
199
+ xt_value options = xt_arg(argc, argv, 1);
200
+ if (XT_IS_OBJECT(options)) {
201
+ xt_value requested = xt_object_get_cstr(options, "granularity");
202
+ if (XT_IS_STRING(requested)) granularity = xt_string_data(requested);
203
+ }
204
+ xt_value segmenter = xt_object_new();
205
+ xt_object_set(segmenter, xt_string_from_cstr("__granularity"), xt_string_from_cstr(granularity));
206
+ xt_value segment = xt_closure_new((void *)xt_intl_segment, 0, NULL);
207
+ xt_function_set_metadata(segment, xt_string_from_cstr("segment"), 1);
208
+ xt_object_set(segmenter, xt_string_from_cstr("segment"), segment);
209
+ xt_value resolved = xt_closure_new((void *)xt_intl_resolved_options, 0, NULL);
210
+ xt_function_set_metadata(resolved, xt_string_from_cstr("resolvedOptions"), 0);
211
+ xt_object_set(segmenter, xt_string_from_cstr("resolvedOptions"), resolved);
212
+ return segmenter;
213
+ }
214
+
215
+ xt_value xt_intl_object(void) {
216
+ xt_gc_add_root(&xt_intl_object_value);
217
+ if (xt_intl_object_value) return xt_intl_object_value;
218
+ xt_value intl = xt_object_new();
219
+ xt_value segmenter = xt_closure_new((void *)xt_intl_segmenter_ctor, 0, NULL);
220
+ xt_function_set_metadata(segmenter, xt_string_from_cstr("Segmenter"), 0);
221
+ xt_object_set(intl, xt_string_from_cstr("Segmenter"), segmenter);
222
+ xt_intl_object_value = intl;
223
+ return xt_intl_object_value;
224
+ }
@@ -64,6 +64,15 @@ void xt_stack_trim(int32_t depth) {
64
64
  if (depth < g_stack_depth) g_stack_depth = depth;
65
65
  }
66
66
 
67
+ /* The module whose top-level code is running. Every module of a bundle shares
68
+ * one generated function, so a crash inside a module body would otherwise be
69
+ * reported at the entry file's offsets. Codegen emits one call per module
70
+ * boundary. */
71
+ static const char *g_current_module = NULL;
72
+
73
+ void xt_stack_set_module(const char *name) { g_current_module = name; }
74
+
75
+
67
76
  static void xt_stack_append(char **buffer, size_t *length, size_t *capacity, const char *text) {
68
77
  size_t count = strlen(text);
69
78
  if (*length + count + 1 > *capacity) {
@@ -90,6 +99,11 @@ xt_value xt_stack_capture(const char *name, const char *message) {
90
99
  xt_stack_append(&buffer, &length, &capacity, message);
91
100
  }
92
101
  xt_stack_append(&buffer, &length, &capacity, "\n");
102
+ if (g_current_module != NULL) {
103
+ xt_stack_append(&buffer, &length, &capacity, " module: ");
104
+ xt_stack_append(&buffer, &length, &capacity, g_current_module);
105
+ xt_stack_append(&buffer, &length, &capacity, "\n");
106
+ }
93
107
  for (int32_t index = g_stack_depth - 1; index >= 0; index--) {
94
108
  const xt_stack_entry *entry = &g_stack[index];
95
109
  char line[24];
@@ -123,6 +137,13 @@ static char g_crash_report[16384];
123
137
 
124
138
  static int xt_format_stack(char *out, size_t size) {
125
139
  int written = 0;
140
+ /* While a bundle's module bodies run, name the module whose top-level code is
141
+ * executing: every module shares one generated function, so its frames all
142
+ * carry the entry file's coordinates. */
143
+ if (g_current_module != NULL) {
144
+ int count = snprintf(out, size, " module: %s\n", g_current_module);
145
+ if (count > 0) written = count;
146
+ }
126
147
  for (int32_t index = g_stack_depth - 1; index >= 0 && written < (int)size; index--) {
127
148
  const xt_stack_entry *entry = &g_stack[index];
128
149
  int count = snprintf(out + written, size - (size_t)written, " at %s (%s:%d)\n",
@@ -139,7 +160,24 @@ static int xt_format_stack(char *out, size_t size) {
139
160
  static LONG WINAPI xt_fatal_exception(EXCEPTION_POINTERS *info) {
140
161
  unsigned long code =
141
162
  info && info->ExceptionRecord ? (unsigned long)info->ExceptionRecord->ExceptionCode : 0UL;
142
- int written = snprintf(g_crash_report, sizeof(g_crash_report), "\nxbintsc: fatal exception 0x%08lx\n", code);
163
+ int written = snprintf(g_crash_report, sizeof(g_crash_report), "\nxbintsc: fatal exception 0x%08lx\n",
164
+ code);
165
+ written += snprintf(g_crash_report + written, sizeof(g_crash_report) - (size_t)written,
166
+ " thread: %s\n", xt_gc_worker_thread ? "I/O worker" : "main");
167
+ /* A fault inside a block the collector already released is a
168
+ * use-after-free; naming the block turns a mystery crash into a lead. */
169
+ if (info && info->ExceptionRecord) {
170
+ unsigned long parameters = (unsigned long)info->ExceptionRecord->NumberParameters;
171
+ uintptr_t access = (uintptr_t)info->ExceptionRecord->ExceptionInformation[0];
172
+ uintptr_t address = (uintptr_t)info->ExceptionRecord->ExceptionInformation[1];
173
+ written += snprintf(g_crash_report + written, sizeof(g_crash_report) - (size_t)written,
174
+ " detail: %lu parameters, access 0x%llx, address 0x%llx\n", parameters,
175
+ (unsigned long long)access, (unsigned long long)address);
176
+ if (address != 0) {
177
+ written += xt_gc_debug_describe(address, g_crash_report + written,
178
+ sizeof(g_crash_report) - (size_t)written);
179
+ }
180
+ }
143
181
  written += xt_format_stack(g_crash_report + written, sizeof(g_crash_report) - (size_t)written);
144
182
  _write(2, g_crash_report, (unsigned int)written);
145
183
  _exit(1);
@@ -152,6 +190,7 @@ void xt_install_crash_handler(void) { SetUnhandledExceptionFilter(xt_fatal_excep
152
190
 
153
191
  static void xt_fatal_signal(int sig) {
154
192
  int written = snprintf(g_crash_report, sizeof(g_crash_report), "\nxbintsc: fatal signal %d\n", sig);
193
+
155
194
  written += xt_format_stack(g_crash_report + written, sizeof(g_crash_report) - (size_t)written);
156
195
  ssize_t ignored = write(STDERR_FILENO, g_crash_report, (size_t)written);
157
196
  (void)ignored;