xbintsc 0.7.0 → 0.7.6

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 (323) hide show
  1. package/dist/src/ast/common.d.ts +10 -0
  2. package/dist/src/checker/source.js +4 -0
  3. package/dist/src/checker/source.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/cli/main.js +15 -3
  6. package/dist/src/cli/main.js.map +1 -1
  7. package/dist/src/codegen/generator/calls/access.js +4 -11
  8. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  9. package/dist/src/codegen/generator/calls/element-inline.d.ts +42 -0
  10. package/dist/src/codegen/generator/calls/element-inline.js +388 -0
  11. package/dist/src/codegen/generator/calls/element-inline.js.map +1 -0
  12. package/dist/src/codegen/generator/calls/invocation.js +82 -1
  13. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  14. package/dist/src/codegen/generator/calls.d.ts +3 -1
  15. package/dist/src/codegen/generator/calls.js +3 -1
  16. package/dist/src/codegen/generator/calls.js.map +1 -1
  17. package/dist/src/codegen/generator/debug.js +20 -3
  18. package/dist/src/codegen/generator/debug.js.map +1 -1
  19. package/dist/src/codegen/generator/expressions/assignment.js +10 -2
  20. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  21. package/dist/src/codegen/generator/expressions/global-values.d.ts +19 -0
  22. package/dist/src/codegen/generator/expressions/global-values.js +49 -0
  23. package/dist/src/codegen/generator/expressions/global-values.js.map +1 -0
  24. package/dist/src/codegen/generator/expressions/primary.d.ts +0 -2
  25. package/dist/src/codegen/generator/expressions/primary.js +4 -24
  26. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  27. package/dist/src/codegen/generator/expressions.d.ts +2 -1
  28. package/dist/src/codegen/generator/expressions.js +2 -1
  29. package/dist/src/codegen/generator/expressions.js.map +1 -1
  30. package/dist/src/codegen/generator/layout.d.ts +65 -0
  31. package/dist/src/codegen/generator/layout.js +50 -0
  32. package/dist/src/codegen/generator/layout.js.map +1 -0
  33. package/dist/src/codegen/generator/state.js +4 -0
  34. package/dist/src/codegen/generator/state.js.map +1 -1
  35. package/dist/src/diagnostics/diagnostic.js +1 -1
  36. package/dist/src/diagnostics/diagnostic.js.map +1 -1
  37. package/dist/src/diagnostics/source-text.d.ts +37 -9
  38. package/dist/src/diagnostics/source-text.js +124 -23
  39. package/dist/src/diagnostics/source-text.js.map +1 -1
  40. package/dist/src/diagnostics/source.d.ts +16 -6
  41. package/dist/src/diagnostics/source.js +25 -4
  42. package/dist/src/diagnostics/source.js.map +1 -1
  43. package/dist/src/driver/build-key.d.ts +14 -0
  44. package/dist/src/driver/build-key.js +6 -1
  45. package/dist/src/driver/build-key.js.map +1 -1
  46. package/dist/src/driver/bundler/concatenate.js +11 -4
  47. package/dist/src/driver/bundler/concatenate.js.map +1 -1
  48. package/dist/src/driver/bundler/graph.js +2 -2
  49. package/dist/src/driver/bundler/graph.js.map +1 -1
  50. package/dist/src/driver/bundler/merge.js +11 -5
  51. package/dist/src/driver/bundler/merge.js.map +1 -1
  52. package/dist/src/driver/bundler/reexport-resolution.js +3 -0
  53. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
  54. package/dist/src/driver/compiler.js +13 -3
  55. package/dist/src/driver/compiler.js.map +1 -1
  56. package/dist/src/driver/frontend.js +3 -4
  57. package/dist/src/driver/frontend.js.map +1 -1
  58. package/dist/src/driver/runtime-objects.d.ts +1 -1
  59. package/dist/src/driver/runtime-objects.js +4 -0
  60. package/dist/src/driver/runtime-objects.js.map +1 -1
  61. package/dist/src/driver/toolchain.js +28 -5
  62. package/dist/src/driver/toolchain.js.map +1 -1
  63. package/dist/src/driver/version.d.ts +1 -1
  64. package/dist/src/driver/version.js +1 -1
  65. package/dist/src/extensions/gui/html.js +42 -10
  66. package/dist/src/extensions/gui/html.js.map +1 -1
  67. package/dist/src/extensions/gui/index.js +48 -0
  68. package/dist/src/extensions/gui/index.js.map +1 -1
  69. package/dist/src/extensions/registry.d.ts +20 -0
  70. package/dist/src/extensions/registry.js.map +1 -1
  71. package/dist/src/parser/expressions.js +2 -2
  72. package/dist/src/parser/expressions.js.map +1 -1
  73. package/dist/src/parser/helpers.d.ts +8 -0
  74. package/dist/src/parser/helpers.js +21 -0
  75. package/dist/src/parser/helpers.js.map +1 -1
  76. package/dist/tests/cli/main.test.js +9 -0
  77. package/dist/tests/cli/main.test.js.map +1 -1
  78. package/dist/tests/codegen/element-layout.test.d.ts +19 -0
  79. package/dist/tests/codegen/element-layout.test.js +80 -0
  80. package/dist/tests/codegen/element-layout.test.js.map +1 -0
  81. package/dist/tests/codegen/expressions.test.js +25 -0
  82. package/dist/tests/codegen/expressions.test.js.map +1 -1
  83. package/dist/tests/codegen/llvm.test.js +13 -0
  84. package/dist/tests/codegen/llvm.test.js.map +1 -1
  85. package/dist/tests/diagnostics/diagnostic.test.js +9 -0
  86. package/dist/tests/diagnostics/diagnostic.test.js.map +1 -1
  87. package/dist/tests/diagnostics/source-text.test.d.ts +1 -0
  88. package/dist/tests/diagnostics/source-text.test.js +50 -0
  89. package/dist/tests/diagnostics/source-text.test.js.map +1 -0
  90. package/dist/tests/diagnostics/source.test.js +17 -0
  91. package/dist/tests/diagnostics/source.test.js.map +1 -1
  92. package/dist/tests/driver/build-key.test.d.ts +1 -0
  93. package/dist/tests/driver/build-key.test.js +49 -0
  94. package/dist/tests/driver/build-key.test.js.map +1 -0
  95. package/dist/tests/driver/toolchain.test.js +8 -1
  96. package/dist/tests/driver/toolchain.test.js.map +1 -1
  97. package/dist/tests/driver/usage.test.js +2 -1
  98. package/dist/tests/driver/usage.test.js.map +1 -1
  99. package/dist/tests/e2e/async-await.test.js +16 -0
  100. package/dist/tests/e2e/async-await.test.js.map +1 -1
  101. package/dist/tests/e2e/compile-builtins.test.js +29 -0
  102. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  103. package/dist/tests/e2e/compile-modules.test.js +13 -0
  104. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  105. package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
  106. package/dist/tests/e2e/compile-prototype-chain.test.d.ts +8 -0
  107. package/dist/tests/e2e/compile-prototype-chain.test.js +24 -0
  108. package/dist/tests/e2e/compile-prototype-chain.test.js.map +1 -0
  109. package/dist/tests/e2e/differential-language.test.js +25 -0
  110. package/dist/tests/e2e/differential-language.test.js.map +1 -1
  111. package/dist/tests/e2e/differential-values.test.js +59 -0
  112. package/dist/tests/e2e/differential-values.test.js.map +1 -1
  113. package/dist/tests/e2e/gc.test.js +22 -0
  114. package/dist/tests/e2e/gc.test.js.map +1 -1
  115. package/dist/tests/e2e/gui-audio-extra.test.d.ts +12 -0
  116. package/dist/tests/e2e/gui-audio-extra.test.js +185 -0
  117. package/dist/tests/e2e/gui-audio-extra.test.js.map +1 -0
  118. package/dist/tests/e2e/gui-browser.test.js +35 -2
  119. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  120. package/dist/tests/e2e/gui-dom.test.js +88 -0
  121. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  122. package/dist/tests/e2e/gui-webgl-declarations.test.d.ts +13 -0
  123. package/dist/tests/e2e/gui-webgl-declarations.test.js +136 -0
  124. package/dist/tests/e2e/gui-webgl-declarations.test.js.map +1 -0
  125. package/dist/tests/e2e/gui-webgl-texture-units.test.d.ts +12 -0
  126. package/dist/tests/e2e/gui-webgl-texture-units.test.js +86 -0
  127. package/dist/tests/e2e/gui-webgl-texture-units.test.js.map +1 -0
  128. package/dist/tests/e2e/gui-webgl2-blit.test.js +6 -2
  129. package/dist/tests/e2e/gui-webgl2-blit.test.js.map +1 -1
  130. package/dist/tests/e2e/gui-webgl2-msaa-float.test.d.ts +12 -0
  131. package/dist/tests/e2e/gui-webgl2-msaa-float.test.js +157 -0
  132. package/dist/tests/e2e/gui-webgl2-msaa-float.test.js.map +1 -0
  133. package/dist/tests/e2e/gui-webgl2-msaa.test.d.ts +10 -0
  134. package/dist/tests/e2e/gui-webgl2-msaa.test.js +344 -0
  135. package/dist/tests/e2e/gui-webgl2-msaa.test.js.map +1 -0
  136. package/dist/tests/e2e/gui-webgl2-target-orientation.test.d.ts +15 -0
  137. package/dist/tests/e2e/gui-webgl2-target-orientation.test.js +287 -0
  138. package/dist/tests/e2e/gui-webgl2-target-orientation.test.js.map +1 -0
  139. package/dist/tests/e2e/gui-webgl2.test.js +59 -1
  140. package/dist/tests/e2e/gui-webgl2.test.js.map +1 -1
  141. package/dist/tests/e2e/hang-watchdog.test.d.ts +10 -0
  142. package/dist/tests/e2e/hang-watchdog.test.js +50 -0
  143. package/dist/tests/e2e/hang-watchdog.test.js.map +1 -0
  144. package/dist/tests/e2e/method-call-cache.test.d.ts +5 -0
  145. package/dist/tests/e2e/method-call-cache.test.js +65 -0
  146. package/dist/tests/e2e/method-call-cache.test.js.map +1 -0
  147. package/dist/tests/e2e/performance-now.test.d.ts +9 -0
  148. package/dist/tests/e2e/performance-now.test.js +36 -0
  149. package/dist/tests/e2e/performance-now.test.js.map +1 -0
  150. package/dist/tests/e2e/property-ic.test.js +33 -0
  151. package/dist/tests/e2e/property-ic.test.js.map +1 -1
  152. package/dist/tests/e2e/shape-interning.test.d.ts +16 -0
  153. package/dist/tests/e2e/shape-interning.test.js +55 -0
  154. package/dist/tests/e2e/shape-interning.test.js.map +1 -0
  155. package/dist/tests/e2e/string-utf16.test.js +9 -0
  156. package/dist/tests/e2e/string-utf16.test.js.map +1 -1
  157. package/dist/tests/e2e/typed-array-views.test.js +26 -0
  158. package/dist/tests/e2e/typed-array-views.test.js.map +1 -1
  159. package/dist/tests/e2e/url-search-params.test.js +19 -0
  160. package/dist/tests/e2e/url-search-params.test.js.map +1 -1
  161. package/dist/tests/extensions/gui.test.js +16 -1
  162. package/dist/tests/extensions/gui.test.js.map +1 -1
  163. package/dist/tests/helpers.js +1 -0
  164. package/dist/tests/helpers.js.map +1 -1
  165. package/dist/tests/parser/expressions.test.js +36 -0
  166. package/dist/tests/parser/expressions.test.js.map +1 -1
  167. package/doc/ai/contributing.md +8 -5
  168. package/doc/ai/extensions.md +12 -3
  169. package/doc/ai/language-support.md +2 -1
  170. package/doc/ai/troubleshooting.md +14 -0
  171. package/doc/ai/zh-CN/contributing.md +6 -3
  172. package/doc/ai/zh-CN/extensions.md +10 -2
  173. package/doc/ai/zh-CN/language-support.md +2 -1
  174. package/doc/ai/zh-CN/troubleshooting.md +12 -0
  175. package/doc/gui/api.md +26 -4
  176. package/doc/gui/dom.md +8 -0
  177. package/doc/gui/known-issues.md +35 -6
  178. package/doc/gui/runtime.md +16 -1
  179. package/doc/gui/webgl.md +97 -26
  180. package/doc/gui-scripts.md +2 -1
  181. package/doc/gui.md +8 -5
  182. package/doc/implemented/checker.md +9 -0
  183. package/doc/implemented/codegen.md +7 -0
  184. package/doc/implemented/parser.md +2 -1
  185. package/doc/implemented/runtime-core.md +9 -2
  186. package/doc/implemented/stdlib.md +10 -2
  187. package/doc/implemented/typed-arrays.md +18 -2
  188. package/doc/zh-CN/gui/api.md +19 -5
  189. package/doc/zh-CN/gui/dom.md +6 -0
  190. package/doc/zh-CN/gui/known-issues.md +24 -5
  191. package/doc/zh-CN/gui/runtime.md +13 -1
  192. package/doc/zh-CN/gui/webgl.md +20 -7
  193. package/doc/zh-CN/gui-scripts.md +2 -1
  194. package/doc/zh-CN/gui.md +4 -1
  195. package/doc/zh-CN/implemented/checker.md +7 -0
  196. package/doc/zh-CN/implemented/codegen.md +7 -0
  197. package/doc/zh-CN/implemented/parser.md +2 -1
  198. package/doc/zh-CN/implemented/runtime-core.md +9 -2
  199. package/doc/zh-CN/implemented/stdlib.md +6 -2
  200. package/doc/zh-CN/implemented/typed-arrays.md +14 -2
  201. package/package.json +7 -7
  202. package/runtime/ext_gui/audio.cpp +39 -3
  203. package/runtime/ext_gui/audio_extra.cpp +353 -0
  204. package/runtime/ext_gui/audio_extra.h +47 -0
  205. package/runtime/ext_gui/audio_fft.h +76 -0
  206. package/runtime/ext_gui/audio_internal.h +21 -0
  207. package/runtime/ext_gui/audio_mixer.cpp +46 -1
  208. package/runtime/ext_gui/audio_mixer.h +89 -1
  209. package/runtime/ext_gui/audio_mixer_extra.cpp +126 -0
  210. package/runtime/ext_gui/audio_nodes.cpp +242 -0
  211. package/runtime/ext_gui/audio_nodes_internal.h +81 -0
  212. package/runtime/ext_gui/audio_nodes_spatial.cpp +375 -0
  213. package/runtime/ext_gui/browser.cpp +9 -0
  214. package/runtime/ext_gui/browser_screen.cpp +73 -0
  215. package/runtime/ext_gui/dom_api_canvas.cpp +9 -1
  216. package/runtime/ext_gui/dom_api_internal.h +3 -0
  217. package/runtime/ext_gui/dom_api_node.cpp +21 -0
  218. package/runtime/ext_gui/dom_api_node_read.cpp +29 -0
  219. package/runtime/ext_gui/dom_api_proto.cpp +3 -0
  220. package/runtime/ext_gui/dom_api_webgl2.cpp +16 -11
  221. package/runtime/ext_gui/dom_api_webgl2_msaa.cpp +64 -0
  222. package/runtime/ext_gui/dom_api_webgl_compressed.cpp +2 -2
  223. package/runtime/ext_gui/dom_api_webgl_framebuffer.cpp +68 -11
  224. package/runtime/ext_gui/dom_api_webgl_methods.h +3 -0
  225. package/runtime/ext_gui/dom_api_webgl_state.cpp +4 -2
  226. package/runtime/ext_gui/dom_api_webgl_texture.cpp +6 -6
  227. package/runtime/ext_gui/gui.cpp +6 -1
  228. package/runtime/ext_gui/gui_engine.h +2 -0
  229. package/runtime/ext_gui/gui_events.cpp +13 -8
  230. package/runtime/ext_gui/gui_webgl.cpp +3 -2
  231. package/runtime/ext_gui/style_ua.cpp +4 -0
  232. package/runtime/ext_gui/warnings.cpp +1 -1
  233. package/runtime/ext_gui/webgl.h +102 -7
  234. package/runtime/ext_gui/webgl_api.h +3 -2
  235. package/runtime/ext_gui/webgl_blit.cpp +109 -15
  236. package/runtime/ext_gui/webgl_blit_color.cpp +264 -0
  237. package/runtime/ext_gui/webgl_blit_depth.cpp +26 -8
  238. package/runtime/ext_gui/webgl_buffer.cpp +20 -170
  239. package/runtime/ext_gui/webgl_context.cpp +117 -169
  240. package/runtime/ext_gui/webgl_draw.cpp +116 -5
  241. package/runtime/ext_gui/webgl_draw_buffers.cpp +21 -6
  242. package/runtime/ext_gui/webgl_draw_setup.cpp +1 -0
  243. package/runtime/ext_gui/webgl_draw_state.h +2 -0
  244. package/runtime/ext_gui/webgl_feature.cpp +2 -2
  245. package/runtime/ext_gui/webgl_feature.h +4 -3
  246. package/runtime/ext_gui/webgl_framebuffer.cpp +346 -0
  247. package/runtime/ext_gui/webgl_glsl_rewrite.cpp +78 -47
  248. package/runtime/ext_gui/webgl_objects.h +16 -2
  249. package/runtime/ext_gui/webgl_pipeline.cpp +1 -1
  250. package/runtime/ext_gui/webgl_state.cpp +45 -36
  251. package/runtime/ext_gui/webgl_targets.cpp +440 -0
  252. package/runtime/ext_gui/webgl_texture.cpp +81 -25
  253. package/runtime/ext_gui/window.cpp +8 -0
  254. package/runtime/rt.h +27 -27
  255. package/runtime/rt_binary.h +44 -0
  256. package/runtime/rt_internal.h +52 -60
  257. package/runtime/rt_object.h +83 -0
  258. package/runtime/rt_profile.h +45 -0
  259. package/runtime/xt_alloc.c +88 -133
  260. package/runtime/xt_array_buffer.c +99 -71
  261. package/runtime/xt_builtins.c +3 -1
  262. package/runtime/xt_constructors.c +1 -0
  263. package/runtime/xt_containers/access.inc +96 -11
  264. package/runtime/xt_containers/arrays.inc +2 -1
  265. package/runtime/xt_containers/cstr_cache.inc +56 -0
  266. package/runtime/xt_containers/method_cache.inc +121 -0
  267. package/runtime/xt_containers/object_keys.inc +58 -0
  268. package/runtime/xt_containers/objects.inc +47 -79
  269. package/runtime/xt_containers/shapes.inc +153 -8
  270. package/runtime/xt_containers.c +4 -0
  271. package/runtime/xt_gc_release.c +70 -0
  272. package/runtime/xt_gc_stack.c +83 -0
  273. package/runtime/xt_gc_verify.c +1 -0
  274. package/runtime/xt_io/console.inc +66 -0
  275. package/runtime/xt_io/exception.inc +32 -6
  276. package/runtime/xt_io/watchdog.inc +101 -0
  277. package/runtime/xt_io.c +1 -0
  278. package/runtime/xt_loop.c +23 -3
  279. package/runtime/xt_pool.c +151 -0
  280. package/runtime/xt_profile.c +126 -0
  281. package/runtime/xt_stdlib/dispatch.inc +7 -1
  282. package/runtime/xt_stdlib/math.inc +272 -57
  283. package/runtime/xt_stdlib2/json_stringify.inc +1 -2
  284. package/runtime/xt_stdlib2/regexp-match.inc +17 -0
  285. package/runtime/xt_storage/indexed_db.c +4 -1
  286. package/runtime/xt_structured_clone.c +2 -1
  287. package/runtime/xt_typed_array/construction.inc +10 -0
  288. package/runtime/xt_typed_array/elements.inc +43 -23
  289. package/runtime/xt_url.c +50 -0
  290. package/runtime/xt_values/constructors.inc +4 -1
  291. package/runtime/xt_values/strings.inc +3 -1
  292. package/src/ast/common.ts +10 -0
  293. package/src/checker/source.ts +3 -0
  294. package/src/cli/main.ts +14 -3
  295. package/src/codegen/generator/calls/access.ts +5 -10
  296. package/src/codegen/generator/calls/element-inline.ts +433 -0
  297. package/src/codegen/generator/calls/invocation.ts +87 -1
  298. package/src/codegen/generator/calls.ts +5 -0
  299. package/src/codegen/generator/debug.ts +21 -3
  300. package/src/codegen/generator/expressions/assignment.ts +11 -2
  301. package/src/codegen/generator/expressions/global-values.ts +57 -0
  302. package/src/codegen/generator/expressions/primary.ts +4 -27
  303. package/src/codegen/generator/expressions.ts +3 -0
  304. package/src/codegen/generator/layout.ts +54 -0
  305. package/src/codegen/generator/state.ts +5 -0
  306. package/src/diagnostics/diagnostic.ts +1 -1
  307. package/src/diagnostics/source-text.ts +118 -22
  308. package/src/diagnostics/source.ts +29 -7
  309. package/src/driver/build-key.ts +21 -1
  310. package/src/driver/bundler/concatenate.ts +12 -5
  311. package/src/driver/bundler/graph.ts +2 -2
  312. package/src/driver/bundler/merge.ts +11 -4
  313. package/src/driver/bundler/reexport-resolution.ts +2 -0
  314. package/src/driver/compiler.ts +14 -3
  315. package/src/driver/frontend.ts +3 -4
  316. package/src/driver/runtime-objects.ts +4 -0
  317. package/src/driver/toolchain.ts +28 -5
  318. package/src/driver/version.ts +1 -1
  319. package/src/extensions/gui/html.ts +41 -11
  320. package/src/extensions/gui/index.ts +50 -1
  321. package/src/extensions/registry.ts +20 -0
  322. package/src/parser/expressions.ts +2 -2
  323. package/src/parser/helpers.ts +22 -0
@@ -4,23 +4,57 @@
4
4
 
5
5
  /* Shapes are interned by their (parent, property key) transition. They are
6
6
  * immortal metadata, but their keys are ordinary strings/symbols, so a root
7
- * provider keeps those keys alive. Pointer identity is enough to share a
8
- * transition between the same interned literal; dynamically built keys simply
9
- * get their own shape, which is still a correct layout identity for the IC. */
10
- #define XT_SHAPE_BUCKETS 256
7
+ * provider keeps those keys alive. A key is matched by *text* (see
8
+ * `xt_shape_bucket`), so an interned literal and a string built at run time with
9
+ * the same bytes share one transition.
10
+ *
11
+ * The bucket count and the intern limit are performance parameters, not
12
+ * semantics: a saturated table (or a NULL shape) disables the shape/method
13
+ * inline caches for every object created afterwards, so the next lookup is a
14
+ * linear scan of the receiver and its prototype chain. A GUI page built on a
15
+ * large JS bundle interns tens of thousands of transitions, so the old 256
16
+ * buckets / 16K shapes pair walked long chains and then silently degraded the
17
+ * hot paths; both are sized for a large application now.
18
+ */
19
+ #define XT_SHAPE_BUCKETS 4096
11
20
  /* Interned shapes are immortal metadata. A program that creates property names
12
21
  * dynamically would otherwise leak one shape per name forever, so stop
13
22
  * interning after a bounded number; those objects simply fall back to the
14
23
  * uncached lookup (a NULL shape). */
15
- #define XT_SHAPE_LIMIT 16384
24
+ #define XT_SHAPE_LIMIT 262144
16
25
  static xt_shape *g_shapes[XT_SHAPE_BUCKETS];
17
26
  static xt_shape *g_shape_all = NULL;
18
27
  static xt_shape *g_shape_root = NULL;
19
28
  static uint32_t g_shape_count = 0;
20
29
 
30
+ /* A key's *content* decides its bucket, not the address of the string that
31
+ * holds it. Property keys built at run time (a typed array's `byteLength`, a
32
+ * DOM handle's `length`, any `obj["k" + i]`) are fresh string objects with the
33
+ * same bytes; hashing the pointer would put each of them in its own bucket, so
34
+ * the value comparison in the chain below could never find the existing
35
+ * transition and *every insert* would intern a new shape. Hashing the bytes is
36
+ * what makes value interning work, and therefore what keeps the shape table
37
+ * (and the inline caches that depend on it) usable in a large program. */
38
+ static uint64_t xt_shape_key_hash(xt_value key) {
39
+ if (XT_IS_STRING(key)) {
40
+ xt_string *string = xt_as_string(key);
41
+ const char *bytes = xt_string_bytes(string);
42
+ uint64_t hash = 1469598103934665603ULL; /* FNV-1a */
43
+ for (uint32_t i = 0; i < string->length; i++) {
44
+ hash ^= (unsigned char)bytes[i];
45
+ hash *= 1099511628211ULL;
46
+ }
47
+ return hash;
48
+ }
49
+ return (uint64_t)(uintptr_t)XT_GET_PTR(key);
50
+ }
51
+
21
52
  static size_t xt_shape_bucket(xt_shape *parent, xt_value key) {
22
- uintptr_t mixed = ((uintptr_t)parent >> 4) ^ ((uintptr_t)XT_GET_PTR(key) >> 4);
23
- return (size_t)mixed & (XT_SHAPE_BUCKETS - 1);
53
+ uintptr_t mixed = ((uintptr_t)parent >> 4) ^ (uintptr_t)xt_shape_key_hash(key);
54
+ /* The low bits are alignment, not entropy: mix them before masking. */
55
+ mixed ^= mixed >> 17;
56
+ mixed *= 0x9e3779b97f4a7c15ULL;
57
+ return (size_t)(mixed >> 32) & (XT_SHAPE_BUCKETS - 1);
24
58
  }
25
59
 
26
60
  static void xt_shape_gc_roots(void) {
@@ -38,11 +72,111 @@ static xt_shape *xt_shape_empty(void) {
38
72
  return shape;
39
73
  }
40
74
 
75
+ static int xt_shape_trace_enabled(void) {
76
+ static int enabled = -1;
77
+ if (enabled < 0) {
78
+ const char *env = getenv("XT_SHAPE_TRACE");
79
+ enabled = (env != NULL && env[0] != '\0' && env[0] != '0') ? 1 : 0;
80
+ }
81
+ return enabled;
82
+ }
83
+
84
+ static uint64_t g_shape_rebuilds = 0;
85
+
86
+ /* Trace-only histogram over the property names that create transitions. */
87
+ typedef struct {
88
+ char text[24];
89
+ uint32_t count;
90
+ } xt_shape_key_stat;
91
+ #define XT_SHAPE_TRACE_KEYS 64
92
+ static xt_shape_key_stat g_shape_key_stats[XT_SHAPE_TRACE_KEYS];
93
+ static uint32_t g_shape_key_stat_count = 0;
94
+
95
+ static void xt_shape_trace_record(xt_value key) {
96
+ char text[24];
97
+ if (XT_IS_STRING(key)) {
98
+ xt_string *string = xt_as_string(key);
99
+ const char *bytes = xt_string_bytes(string);
100
+ uint32_t length = string->length;
101
+ if (length > 20) length = 20;
102
+ if (bytes) {
103
+ memcpy(text, bytes, length);
104
+ text[length] = '\0';
105
+ } else {
106
+ text[0] = '\0';
107
+ }
108
+ } else if (xt_is_symbol(key)) {
109
+ xt_value description = xt_symbol_description(key);
110
+ const char *name = XT_IS_STRING(description) ? xt_string_bytes(xt_as_string(description)) : "";
111
+ snprintf(text, sizeof(text), "@%s", name != NULL ? name : "");
112
+ } else {
113
+ snprintf(text, sizeof(text), "<other>");
114
+ }
115
+ for (uint32_t i = 0; i < g_shape_key_stat_count; i++) {
116
+ if (strcmp(g_shape_key_stats[i].text, text) == 0) {
117
+ g_shape_key_stats[i].count++;
118
+ return;
119
+ }
120
+ }
121
+ if (g_shape_key_stat_count < XT_SHAPE_TRACE_KEYS) {
122
+ snprintf(g_shape_key_stats[g_shape_key_stat_count].text, sizeof(g_shape_key_stats[0].text), "%s", text);
123
+ g_shape_key_stats[g_shape_key_stat_count].count = 1;
124
+ g_shape_key_stat_count++;
125
+ }
126
+ }
127
+
128
+ static void xt_shape_trace_report(void) {
129
+ fprintf(stderr, "xt_shape: %u transitions, %llu rebuilds; top property names:", (unsigned)g_shape_count,
130
+ (unsigned long long)g_shape_rebuilds);
131
+ for (int rank = 0; rank < 8; rank++) {
132
+ int best = -1;
133
+ for (uint32_t i = 0; i < g_shape_key_stat_count; i++) {
134
+ if (g_shape_key_stats[i].count == 0) continue;
135
+ if (best < 0 || g_shape_key_stats[i].count > g_shape_key_stats[best].count) best = (int)i;
136
+ }
137
+ if (best < 0) break;
138
+ fprintf(stderr, " %s=%u", g_shape_key_stats[best].text, g_shape_key_stats[best].count);
139
+ g_shape_key_stats[best].count = 0;
140
+ }
141
+ fprintf(stderr, "\n");
142
+ }
143
+
144
+ /* Print the full property layout of a newly interned shape (root first). */
145
+ static void xt_shape_trace_chain(xt_shape *shape) {
146
+ const xt_shape *chain[64];
147
+ int depth = 0;
148
+ for (const xt_shape *s = shape; s != NULL && s->parent != NULL && depth < 64; s = s->parent) chain[depth++] = s;
149
+ fprintf(stderr, "xt_shape: latest layout:");
150
+ for (int i = depth - 1; i >= 0; i--) {
151
+ xt_value key = chain[i]->key;
152
+ if (XT_IS_STRING(key)) {
153
+ xt_string *string = xt_as_string(key);
154
+ const char *bytes = xt_string_bytes(string);
155
+ uint32_t length = string->length;
156
+ if (length > 16) length = 16;
157
+ fprintf(stderr, " %.*s", (int)length, bytes != NULL ? bytes : "");
158
+ } else if (xt_is_symbol(key)) {
159
+ xt_value description = xt_symbol_description(key);
160
+ const char *name = XT_IS_STRING(description) ? xt_string_bytes(xt_as_string(description)) : "?";
161
+ fprintf(stderr, " @%s", name != NULL ? name : "?");
162
+ } else {
163
+ fprintf(stderr, " <other>");
164
+ }
165
+ }
166
+ fprintf(stderr, "\n");
167
+ }
168
+
41
169
  xt_shape *xt_shape_transition(xt_shape *parent, xt_value key) {
42
170
  if (!parent) parent = xt_shape_empty();
43
171
  size_t bucket = xt_shape_bucket(parent, key);
44
172
  for (xt_shape *shape = g_shapes[bucket]; shape; shape = shape->next) {
45
- if (shape->parent == parent && shape->key == key) return shape;
173
+ if (shape->parent != parent) continue;
174
+ /* Property keys are interned for literals, but a key built at run time
175
+ * (`obj[name]`, a number converted to a string, a symbol-less parser) is a
176
+ * fresh value every time. Interning by value, not by pointer, is what keeps
177
+ * a page from creating one shape per *insert* instead of one per distinct
178
+ * layout. */
179
+ if (shape->key == key || xt_property_key_equals(shape->key, key)) return shape;
46
180
  }
47
181
  if (g_shape_count >= XT_SHAPE_LIMIT) return NULL;
48
182
  xt_shape *shape = (xt_shape *)calloc(1, sizeof(xt_shape));
@@ -55,10 +189,21 @@ xt_shape *xt_shape_transition(xt_shape *parent, xt_value key) {
55
189
  shape->all_next = g_shape_all;
56
190
  g_shape_all = shape;
57
191
  g_shape_count++;
192
+ /* Diagnostics: `XT_SHAPE_TRACE=1` reports the interned-transition count so a
193
+ * page that needs a larger table is visible instead of silently degrading.
194
+ * The per-key histogram shows *which* property names are multiplying. */
195
+ if (xt_shape_trace_enabled()) {
196
+ xt_shape_trace_record(key);
197
+ if ((g_shape_count & 32767u) == 0) {
198
+ xt_shape_trace_report();
199
+ xt_shape_trace_chain(shape);
200
+ }
201
+ }
58
202
  return shape;
59
203
  }
60
204
 
61
205
  void xt_object_shape_changed(xt_object *obj) {
206
+ g_shape_rebuilds++;
62
207
  xt_shape *shape = xt_shape_empty();
63
208
  for (uint32_t i = 0; i < obj->count; i++) {
64
209
  shape = xt_shape_transition(shape, obj->properties[i].key);
@@ -19,6 +19,10 @@
19
19
  #include "xt_containers/shapes.inc"
20
20
  #include "xt_containers/function_prototypes.inc"
21
21
  #include "xt_containers/objects.inc"
22
+ #include "xt_containers/cstr_cache.inc"
23
+ #include "xt_containers/object_keys.inc"
22
24
  #include "xt_containers/arrays.inc"
23
25
  #include "xt_containers/access.inc"
26
+ #include "xt_containers/method_cache.inc"
24
27
  #include "xt_containers/boxes.inc"
28
+
@@ -0,0 +1,70 @@
1
+ /*
2
+ * xbintsc runtime — release the backing storage of one collected block.
3
+ *
4
+ * Split out of xt_alloc.c to keep the collector within the file-size budget;
5
+ * called from the sweep with the block already unlinked from the heap list.
6
+ */
7
+
8
+ #include "rt_internal.h"
9
+
10
+ #include <stdlib.h>
11
+
12
+ void xt_gc_release(xt_header *header) {
13
+ const size_t block_size = (size_t)header->size;
14
+ const int pooled = (header->flags & XT_GC_POOLED) != 0;
15
+ switch (header->kind) {
16
+ case XT_OBJECT_KIND_STRING:
17
+ free(((xt_string *)header)->flat);
18
+ break;
19
+ case XT_OBJECT_KIND_BIGINT:
20
+ free(((xt_bigint *)header)->limbs);
21
+ break;
22
+ case XT_OBJECT_KIND_OBJECT:
23
+ case XT_OBJECT_KIND_ERROR:
24
+ xt_raw_free(((xt_object *)header)->properties);
25
+ break;
26
+ case XT_OBJECT_KIND_ARRAY_BUFFER: {
27
+ xt_object *object = (xt_object *)header;
28
+ free(object->buffer_bytes);
29
+ xt_raw_free(object->properties);
30
+ break;
31
+ }
32
+ case XT_OBJECT_KIND_ARRAY:
33
+ xt_raw_free(((xt_array *)header)->items);
34
+ break;
35
+ case XT_OBJECT_KIND_FUNCTION:
36
+ free(((xt_function *)header)->environment);
37
+ break;
38
+ case XT_OBJECT_KIND_MAP:
39
+ free(((xt_map *)header)->keys);
40
+ free(((xt_map *)header)->values);
41
+ break;
42
+ case XT_OBJECT_KIND_SET:
43
+ free(((xt_set_object *)header)->values);
44
+ break;
45
+ case XT_OBJECT_KIND_WEAK_MAP:
46
+ free(((xt_weak_map *)header)->keys);
47
+ free(((xt_weak_map *)header)->values);
48
+ break;
49
+ case XT_OBJECT_KIND_WEAK_SET:
50
+ free(((xt_weak_set *)header)->values);
51
+ break;
52
+ case XT_OBJECT_KIND_PROMISE:
53
+ xt_promise_gc_release(header);
54
+ break;
55
+ case XT_OBJECT_KIND_GENERATOR:
56
+ xt_generator_gc_release(header);
57
+ break;
58
+ case XT_OBJECT_KIND_ASYNC:
59
+ xt_async_gc_release(header);
60
+ break;
61
+ default:
62
+ break;
63
+ }
64
+ if (pooled) {
65
+ xt_pool_free_block(header, block_size);
66
+ } else {
67
+ free(header);
68
+ }
69
+ }
70
+
@@ -0,0 +1,83 @@
1
+ /*
2
+ * xbintsc runtime — GC stack-region table.
3
+ *
4
+ * A coroutine has a private C stack; while it runs (or while it is suspended
5
+ * under a deeper coroutine) the conservative collector must scan that region.
6
+ * The table grows on demand: an `async` call chain can be deeper than any fixed
7
+ * limit, and silently dropping a push would desynchronise push/pop, so a later
8
+ * pop evicted a still-live parent region and the collector stopped scanning it.
9
+ */
10
+
11
+ #include "rt_internal.h"
12
+
13
+ #include <stddef.h>
14
+ #include <stdio.h>
15
+ #include <stdlib.h>
16
+
17
+ typedef struct {
18
+ uintptr_t low;
19
+ uintptr_t high;
20
+ /* Lower bound captured from the stack below this region when a deeper region
21
+ * was entered on top of it. `UINTPTR_MAX` means "this is the top region"; the
22
+ * collector then scans from the current stack pointer. */
23
+ uintptr_t scan_low;
24
+ } xt_stack_region;
25
+
26
+ static xt_stack_region *g_stack_regions = NULL;
27
+ static size_t g_stack_depth = 0;
28
+ static size_t g_stack_capacity = 0;
29
+
30
+ /** Ensure room for one more region; aborts when the process is out of memory. */
31
+ static void xt_gc_reserve_stack_region(void) {
32
+ if (g_stack_depth < g_stack_capacity) return;
33
+ size_t capacity = g_stack_capacity == 0 ? 16 : g_stack_capacity * 2;
34
+ xt_stack_region *grown = (xt_stack_region *)realloc(g_stack_regions, sizeof(xt_stack_region) * capacity);
35
+ if (!grown) {
36
+ fprintf(stderr, "xbintsc: out of memory growing the GC stack-region table\n");
37
+ abort();
38
+ }
39
+ g_stack_regions = grown;
40
+ g_stack_capacity = capacity;
41
+ }
42
+
43
+ void xt_gc_init_main_stack(uintptr_t high) {
44
+ xt_gc_reserve_stack_region();
45
+ if (g_stack_depth == 0) {
46
+ g_stack_regions[0].low = 0;
47
+ g_stack_regions[0].high = high;
48
+ g_stack_regions[0].scan_low = UINTPTR_MAX;
49
+ g_stack_depth = 1;
50
+ } else if (high > g_stack_regions[0].high) {
51
+ g_stack_regions[0].high = high;
52
+ }
53
+ }
54
+
55
+ void xt_gc_push_stack(uintptr_t low, uintptr_t high) {
56
+ xt_gc_reserve_stack_region();
57
+ g_stack_regions[g_stack_depth].low = low;
58
+ g_stack_regions[g_stack_depth].high = high;
59
+ g_stack_regions[g_stack_depth].scan_low = low;
60
+ g_stack_depth++;
61
+ }
62
+
63
+ void xt_gc_suspend(uintptr_t sp) {
64
+ if (g_stack_depth == 0) return;
65
+ uintptr_t *scan = &g_stack_regions[g_stack_depth - 1].scan_low;
66
+ if (*scan == UINTPTR_MAX || sp < *scan) *scan = sp;
67
+ }
68
+
69
+ void xt_gc_pop_stack(void) {
70
+ if (g_stack_depth <= 1) return;
71
+ g_stack_depth--;
72
+ /* Back on the underlying stack: resume scanning it from the current frame. */
73
+ if (g_stack_depth > 0) g_stack_regions[g_stack_depth - 1].scan_low = UINTPTR_MAX;
74
+ }
75
+
76
+ size_t xt_gc_stack_region_count(void) {
77
+ return g_stack_depth;
78
+ }
79
+
80
+ void xt_gc_stack_region_bounds(size_t index, uintptr_t current, uintptr_t *low, uintptr_t *high) {
81
+ *high = g_stack_regions[index].high;
82
+ *low = index + 1 == g_stack_depth ? current : g_stack_regions[index].scan_low;
83
+ }
@@ -42,6 +42,7 @@ static const char *xt_gc_kind_name(uint8_t kind) {
42
42
  case XT_OBJECT_KIND_WEAK_MAP: return "weakmap";
43
43
  case XT_OBJECT_KIND_WEAK_SET: return "weakset";
44
44
  case XT_OBJECT_KIND_WEAK_REF: return "weakref";
45
+ case XT_OBJECT_KIND_ARRAY_BUFFER: return "array-buffer";
45
46
  default: return "?";
46
47
  }
47
48
  }
@@ -129,3 +129,69 @@ xt_value xt_console_time_end(int32_t argc, xt_value *argv) {
129
129
  }
130
130
  return XT_UNDEFINED;
131
131
  }
132
+
133
+ /* ------------------------------------------------------------------------- */
134
+ /* The global `console` object */
135
+ /* ------------------------------------------------------------------------- */
136
+
137
+ /*
138
+ * `console` is a global *value* as well as a namespace: code writes
139
+ * `const log = console.log`, `console.error.bind(console)` and
140
+ * `const { warn } = console`, so the binding has to be a real object whose
141
+ * methods are ordinary first-class functions. The object is built once and
142
+ * rooted; each method is a closure over the raw `(argc, argv)` runtime symbol,
143
+ * so a method keeps its behaviour when detached from the object.
144
+ */
145
+
146
+ typedef void (*xt_console_void_method)(int32_t, xt_value *);
147
+ typedef xt_value (*xt_console_value_method)(int32_t, xt_value *);
148
+
149
+ static xt_value xt_console_void_bridge(xt_value thisValue, xt_value env, int32_t argc,
150
+ xt_value *argv) {
151
+ (void)thisValue;
152
+ ((xt_console_void_method)(uintptr_t)xt_closure_env(env, 0))(argc, argv);
153
+ return XT_UNDEFINED;
154
+ }
155
+
156
+ static xt_value xt_console_value_bridge(xt_value thisValue, xt_value env, int32_t argc,
157
+ xt_value *argv) {
158
+ (void)thisValue;
159
+ return ((xt_console_value_method)(uintptr_t)xt_closure_env(env, 0))(argc, argv);
160
+ }
161
+
162
+ static void xt_console_add(xt_value object, const char *name, void *method, int returnsValue) {
163
+ xt_value env[1];
164
+ env[0] = (xt_value)(uintptr_t)method;
165
+ xt_value closure = xt_closure_new(
166
+ returnsValue ? (void *)xt_console_value_bridge : (void *)xt_console_void_bridge, 1, env);
167
+ xt_value key = xt_string_from_cstr(name);
168
+ xt_function_set_metadata(closure, key, returnsValue ? 1 : 0);
169
+ xt_object_set(object, key, closure);
170
+ }
171
+
172
+ xt_value xt_console_object(int32_t argc, xt_value *argv) {
173
+ (void)argc;
174
+ (void)argv;
175
+ static xt_value cached = 0;
176
+ if (cached != 0) return cached;
177
+ xt_value object = xt_object_new();
178
+ xt_console_add(object, "log", (void *)xt_console_log, 0);
179
+ xt_console_add(object, "info", (void *)xt_console_info, 0);
180
+ xt_console_add(object, "warn", (void *)xt_console_warn, 0);
181
+ xt_console_add(object, "error", (void *)xt_console_error, 0);
182
+ xt_console_add(object, "debug", (void *)xt_console_log, 0);
183
+ xt_console_add(object, "dir", (void *)xt_console_dir, 1);
184
+ xt_console_add(object, "trace", (void *)xt_console_trace, 1);
185
+ xt_console_add(object, "assert", (void *)xt_console_assert, 1);
186
+ xt_console_add(object, "count", (void *)xt_console_count, 1);
187
+ xt_console_add(object, "countReset", (void *)xt_console_count_reset, 1);
188
+ xt_console_add(object, "group", (void *)xt_console_group, 1);
189
+ xt_console_add(object, "groupEnd", (void *)xt_console_group_end, 1);
190
+ xt_console_add(object, "table", (void *)xt_console_table, 1);
191
+ xt_console_add(object, "time", (void *)xt_console_time, 1);
192
+ xt_console_add(object, "timeEnd", (void *)xt_console_time_end, 1);
193
+ xt_console_add(object, "timeLog", (void *)xt_console_time_log, 1);
194
+ cached = object;
195
+ xt_gc_add_root(&cached);
196
+ return object;
197
+ }
@@ -38,8 +38,39 @@ typedef struct {
38
38
 
39
39
  static xt_stack_entry g_stack[XT_STACK_MAX_DEPTH];
40
40
  static int32_t g_stack_depth = 0;
41
+ /* Bumped on every push/pop so the hang watchdog (`watchdog.inc`) can tell a
42
+ * program that is working from one that is spinning without calling anything. */
43
+ static volatile unsigned long long g_stack_ticks = 0;
44
+
45
+ /* The module whose top-level code is running. Every module of a bundle shares
46
+ * one generated function, so a crash inside a module body would otherwise be
47
+ * reported at the entry file's offsets. Codegen emits one call per module
48
+ * boundary. */
49
+ static const char *g_current_module = NULL;
50
+
51
+ /* Defined in `watchdog.inc`, which is included after this file. */
52
+ static void xt_stack_watchdog_start_once(void);
53
+
54
+ /** Print the shadow stack, innermost frame first (used by the hang watchdog
55
+ * and by hosts that want the JavaScript frames on a fatal signal). */
56
+ static void xt_stack_dump_frames(void) {
57
+ int32_t depth = g_stack_depth;
58
+ if (depth < 0) depth = 0;
59
+ if (depth > XT_STACK_MAX_DEPTH) depth = XT_STACK_MAX_DEPTH;
60
+ for (int32_t index = depth - 1; index >= 0; index--) {
61
+ const xt_stack_entry *entry = &g_stack[index];
62
+ fprintf(stderr, " at %s (%s:%d)\n",
63
+ entry->name && entry->name[0] ? entry->name : "anonymous",
64
+ entry->file && entry->file[0] ? entry->file : "<anonymous>", entry->line);
65
+ }
66
+ if (depth == 0 && g_current_module != NULL) {
67
+ fprintf(stderr, " (no JavaScript frame; module %s)\n", g_current_module);
68
+ }
69
+ }
41
70
 
42
71
  void xt_stack_push(const char *name, const char *file, int32_t line) {
72
+ g_stack_ticks++;
73
+ xt_stack_watchdog_start_once();
43
74
  if (g_stack_depth >= XT_STACK_MAX_DEPTH) return;
44
75
  xt_stack_entry *entry = &g_stack[g_stack_depth++];
45
76
  entry->name = name;
@@ -48,6 +79,7 @@ void xt_stack_push(const char *name, const char *file, int32_t line) {
48
79
  }
49
80
 
50
81
  void xt_stack_pop(void) {
82
+ g_stack_ticks++;
51
83
  if (g_stack_depth > 0) g_stack_depth--;
52
84
  }
53
85
 
@@ -64,12 +96,6 @@ void xt_stack_trim(int32_t depth) {
64
96
  if (depth < g_stack_depth) g_stack_depth = depth;
65
97
  }
66
98
 
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
99
  void xt_stack_set_module(const char *name) { g_current_module = name; }
74
100
 
75
101
 
@@ -0,0 +1,101 @@
1
+ /* ------------------------------------------------------------------------- */
2
+ /* Hang watchdog */
3
+ /* ------------------------------------------------------------------------- */
4
+
5
+ /*
6
+ * A compiled program that spins — an unexpected infinite loop in user code, a
7
+ * blocking host call, a pathological input — prints nothing at all, and a GUI
8
+ * program merely stops painting. There is no debugger attached and no V8 to
9
+ * request a profile from, so "it hangs" is all the user can report.
10
+ *
11
+ * Setting `XT_STACK_WATCHDOG_MS=<n>` starts a sampler thread that watches the
12
+ * shadow call stack: when no function has been entered or left for `n`
13
+ * milliseconds, it prints the innermost frames (name, file, line). That turns a
14
+ * silent hang into the source location that is spinning.
15
+ *
16
+ * Cost when the variable is unset: one relaxed counter increment per compiled
17
+ * call and nothing else — the thread is not started. The frame array is read
18
+ * from another thread without a lock, which can tear a frame in the middle of
19
+ * an update; that is acceptable for a diagnostic that only runs when asked for.
20
+ * The stack data itself lives in `exception.inc`.
21
+ */
22
+
23
+ #if !defined(_WIN32)
24
+ #include <pthread.h>
25
+ #endif
26
+
27
+ static volatile int g_watchdog_started = 0;
28
+ static volatile int g_watchdog_stop = 0;
29
+
30
+ /** Monotonic milliseconds. */
31
+ static unsigned long long xt_watchdog_now_ms(void) {
32
+ #if defined(_WIN32)
33
+ return (unsigned long long)GetTickCount64();
34
+ #else
35
+ struct timespec ts;
36
+ if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return 0;
37
+ return (unsigned long long)ts.tv_sec * 1000ull + (unsigned long long)(ts.tv_nsec / 1000000);
38
+ #endif
39
+ }
40
+
41
+ #if defined(_WIN32)
42
+ static unsigned __stdcall xt_watchdog_entry(void *argument) {
43
+ #else
44
+ static void *xt_watchdog_entry(void *argument) {
45
+ #endif
46
+ unsigned long long threshold = (unsigned long long)(uintptr_t)argument;
47
+ unsigned long long interval = threshold / 4;
48
+ if (interval < 20) interval = 20;
49
+ unsigned long long last_ticks = g_stack_ticks;
50
+ unsigned long long idle_since = xt_watchdog_now_ms();
51
+ int sample_all = getenv("XT_STACK_SAMPLE_ALL") != NULL;
52
+ while (!g_watchdog_stop) {
53
+ unsigned long long now;
54
+ #if defined(_WIN32)
55
+ Sleep((DWORD)interval);
56
+ #else
57
+ struct timespec sleep_for;
58
+ sleep_for.tv_sec = (time_t)(interval / 1000);
59
+ sleep_for.tv_nsec = (long)((interval % 1000) * 1000000);
60
+ nanosleep(&sleep_for, NULL);
61
+ #endif
62
+ if (g_watchdog_stop) break;
63
+ now = xt_watchdog_now_ms();
64
+ if (!sample_all && g_stack_ticks != last_ticks) {
65
+ last_ticks = g_stack_ticks;
66
+ idle_since = now;
67
+ continue;
68
+ }
69
+ if (!sample_all && now - idle_since < threshold) continue;
70
+ if (sample_all) last_ticks = g_stack_ticks;
71
+ fprintf(stderr, "xt: no JavaScript call for %llu ms, shadow stack:\n", now - idle_since);
72
+ xt_stack_dump_frames();
73
+ fflush(stderr);
74
+ /* Report once per idle period, not once per interval. */
75
+ idle_since = now;
76
+ }
77
+ #if defined(_WIN32)
78
+ return 0;
79
+ #else
80
+ return NULL;
81
+ #endif
82
+ }
83
+
84
+ /** Start the sampler once, if `XT_STACK_WATCHDOG_MS` asks for it. */
85
+ static void xt_stack_watchdog_start_once(void) {
86
+ if (g_watchdog_started) return;
87
+ g_watchdog_started = 1;
88
+ const char *configured = getenv("XT_STACK_WATCHDOG_MS");
89
+ if (configured == NULL || configured[0] == 0) return;
90
+ long ms = strtol(configured, NULL, 10);
91
+ if (ms <= 0) return;
92
+ #if defined(_WIN32)
93
+ uintptr_t handle = _beginthreadex(NULL, 0, xt_watchdog_entry, (void *)(uintptr_t)ms, 0, NULL);
94
+ if (handle != 0) CloseHandle((HANDLE)handle);
95
+ #else
96
+ pthread_t thread;
97
+ if (pthread_create(&thread, NULL, xt_watchdog_entry, (void *)(uintptr_t)ms) == 0) {
98
+ pthread_detach(thread);
99
+ }
100
+ #endif
101
+ }
package/runtime/xt_io.c CHANGED
@@ -46,6 +46,7 @@
46
46
  #endif
47
47
 
48
48
  #include "xt_io/exception.inc"
49
+ #include "xt_io/watchdog.inc"
49
50
  #include "xt_io/inspect.inc"
50
51
  #include "xt_io/console.inc"
51
52
  #include "xt_io/base64.inc"
package/runtime/xt_loop.c CHANGED
@@ -92,9 +92,9 @@ static int64_t xt_loop_now_ms(void) {
92
92
  #endif
93
93
  }
94
94
 
95
- /* Fractional monotonic milliseconds for `performance.now()`. Kept separate from
96
- * xt_loop_now_ms(), which only needs whole milliseconds for timer deadlines. */
97
- double xt_monotonic_ms(void) {
95
+ /* Raw fractional monotonic milliseconds: on Windows this is the QPC counter,
96
+ * which counts from machine boot, not from process start. */
97
+ static double xt_raw_monotonic_ms(void) {
98
98
  #if defined(_WIN32)
99
99
  static LARGE_INTEGER frequency;
100
100
  if (frequency.QuadPart == 0) QueryPerformanceFrequency(&frequency);
@@ -108,6 +108,26 @@ double xt_monotonic_ms(void) {
108
108
  #endif
109
109
  }
110
110
 
111
+ /* `performance.now()` is relative to the program's own time origin, like a
112
+ * browser's (navigation start) or Node's (process start) — not to machine boot.
113
+ * Before the origin existed, a process started six days into an uptime reported
114
+ * `performance.now() == 521,000,000` on its first call: deltas were still right,
115
+ * so three.js kept working, but every absolute reading was nonsense and any
116
+ * `now() / 1000` (three.js's `clock.elapsedTime`) started at six days. */
117
+ static double g_time_origin_ms = -1.0;
118
+
119
+ void xt_time_init(void) {
120
+ if (g_time_origin_ms < 0.0) g_time_origin_ms = xt_raw_monotonic_ms();
121
+ }
122
+
123
+ /* Fractional monotonic milliseconds for `performance.now()`. Kept separate from
124
+ * xt_loop_now_ms(), which only needs whole milliseconds for timer deadlines. */
125
+ double xt_monotonic_ms(void) {
126
+ if (g_time_origin_ms < 0.0) g_time_origin_ms = xt_raw_monotonic_ms();
127
+ double elapsed = xt_raw_monotonic_ms() - g_time_origin_ms;
128
+ return elapsed < 0.0 ? 0.0 : elapsed;
129
+ }
130
+
111
131
  /* Wall-clock milliseconds since the Unix epoch, for `Date.now()` and
112
132
  * `new Date()`. `time(NULL)` only has second resolution, which silently broke
113
133
  * elapsed-time math; this keeps the milliseconds the language exposes. */