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
@@ -0,0 +1,151 @@
1
+ /*
2
+ * xbintsc runtime — the small-block allocator.
3
+ *
4
+ * The collector in `xt_alloc.c` owns reachability; this unit owns the bytes.
5
+ * Keeping the two apart keeps each file within the repository's size budget and
6
+ * makes the ownership rule explicit: every block either comes from a size-class
7
+ * free list or from `malloc`, and the collector asks this unit to release it.
8
+ */
9
+
10
+ #include "rt_internal.h"
11
+
12
+ #include <stdio.h>
13
+ #include <stdlib.h>
14
+ #include <string.h>
15
+ /* ------------------------------------------------------------------------- */
16
+ /* Small-block pool */
17
+ /* ------------------------------------------------------------------------- */
18
+
19
+ /*
20
+ * Most runtime objects are small — an `xt_object` is ~120 bytes and a closure
21
+ * ~100 — and a scene build creates millions of them. The C allocator charges a
22
+ * lock, a free-list search and zeroing per block, which made object creation
23
+ * the dominant cost of three.js's geometry and level construction. Blocks up
24
+ * to `XT_POOL_SMALL_MAX` are carved out of 1 MiB slabs and recycled through
25
+ * per-class free lists instead; the collector returns a block to its class, so
26
+ * a steady state reuses the same memory.
27
+ *
28
+ * The pool is main-thread only: an I/O worker allocates result values with
29
+ * `calloc` (see `xt_alloc`), and the collector — the only thing that releases
30
+ * blocks — runs on the main thread. Slabs are never returned to the OS; the
31
+ * pool is bounded by the process's peak live set, the same bound the
32
+ * collector's threshold follows.
33
+ */
34
+ #define XT_POOL_SMALL_MAX 2048
35
+ static const uint16_t xt_pool_sizes[] = {
36
+ 16, 32, 48, 64, 80, 96, 112, 128, 160, 192, 224, 256,
37
+ 320, 384, 448, 512, 640, 768, 896, 1024, 1280, 1536, 1792, 2048,
38
+ };
39
+ #define XT_POOL_CLASS_COUNT ((int)(sizeof(xt_pool_sizes) / sizeof(xt_pool_sizes[0])))
40
+ static void *g_pool_free[XT_POOL_CLASS_COUNT];
41
+ static unsigned char *g_pool_cursor = NULL;
42
+ static size_t g_pool_left = 0;
43
+
44
+ static int xt_pool_class_of(size_t size) {
45
+ for (int index = 0; index < XT_POOL_CLASS_COUNT; index++) {
46
+ if (size <= (size_t)xt_pool_sizes[index]) return index;
47
+ }
48
+ return -1;
49
+ }
50
+
51
+ /** A zeroed block of at least `size` bytes, or NULL when the size is large. */
52
+ void *xt_pool_alloc(size_t size) {
53
+ int cls = xt_pool_class_of(size);
54
+ if (cls < 0) return NULL;
55
+ void *block = g_pool_free[cls];
56
+ if (block != NULL) {
57
+ g_pool_free[cls] = *(void **)block;
58
+ } else {
59
+ size_t need = (size_t)xt_pool_sizes[cls];
60
+ if (g_pool_left < need) {
61
+ size_t chunk = (size_t)1 << 20;
62
+ if (chunk < need) chunk = need;
63
+ g_pool_cursor = (unsigned char *)malloc(chunk);
64
+ if (g_pool_cursor == NULL) return NULL;
65
+ g_pool_left = chunk;
66
+ }
67
+ block = g_pool_cursor;
68
+ g_pool_cursor += need;
69
+ g_pool_left -= need;
70
+ }
71
+ memset(block, 0, size);
72
+ return block;
73
+ }
74
+
75
+ /** Return a block from `xt_pool_alloc` to its class. */
76
+ void xt_pool_free_block(void *block, size_t size) {
77
+ /* Only the main thread owns the pool. A worker never allocates from it, so
78
+ * reaching here on a worker would mean a main-thread block was re-grown
79
+ * there; leaking it is safer than corrupting the free list. */
80
+ if (xt_gc_worker_thread) return;
81
+ int cls = xt_pool_class_of(size);
82
+ if (cls < 0 || block == NULL) return;
83
+ *(void **)block = g_pool_free[cls];
84
+ g_pool_free[cls] = block;
85
+ }
86
+
87
+ /* ------------------------------------------------------------------------- */
88
+ /* Small-block buffers (property tables, array items) */
89
+ /* ------------------------------------------------------------------------- */
90
+
91
+ /*
92
+ * The buffers behind an object's property table and an array's items grow by
93
+ * doubling, so they are allocated outside the collector. They get the same
94
+ * pool treatment through a small private header, which also records where the
95
+ * block came from: a buffer allocated on an I/O worker comes from `malloc` and
96
+ * is released with `free`, while a main-thread buffer is pooled. The 16-byte
97
+ * header keeps the payload 16-byte aligned.
98
+ */
99
+ typedef struct {
100
+ uint32_t cls;
101
+ uint32_t size;
102
+ uint64_t padding;
103
+ } xt_raw_header;
104
+
105
+ #define XT_RAW_HEADER_SIZE ((size_t)16)
106
+
107
+ void *xt_raw_alloc(size_t size) {
108
+ if (size == 0) size = 1;
109
+ unsigned char *block = NULL;
110
+ if (!xt_gc_worker_thread && size <= XT_POOL_SMALL_MAX) block = (unsigned char *)xt_pool_alloc(size + XT_RAW_HEADER_SIZE);
111
+ if (block == NULL) {
112
+ block = (unsigned char *)malloc(size + XT_RAW_HEADER_SIZE);
113
+ if (block == NULL) {
114
+ fprintf(stderr, "xbintsc: out of memory allocating %zu bytes\n", size);
115
+ abort();
116
+ }
117
+ xt_raw_header *header = (xt_raw_header *)block;
118
+ header->cls = 0xffffffffu;
119
+ header->size = (uint32_t)size;
120
+ header->padding = 0;
121
+ } else {
122
+ xt_raw_header *header = (xt_raw_header *)block;
123
+ header->cls = (uint32_t)xt_pool_class_of(size + XT_RAW_HEADER_SIZE);
124
+ header->size = (uint32_t)size;
125
+ header->padding = 0;
126
+ }
127
+ return block + XT_RAW_HEADER_SIZE;
128
+ }
129
+
130
+ void xt_raw_free(void *pointer) {
131
+ if (pointer == NULL) return;
132
+ unsigned char *block = (unsigned char *)pointer - XT_RAW_HEADER_SIZE;
133
+ xt_raw_header *header = (xt_raw_header *)block;
134
+ if (header->cls == 0xffffffffu) {
135
+ free(block);
136
+ return;
137
+ }
138
+ xt_pool_free_block(block, (size_t)header->size + XT_RAW_HEADER_SIZE);
139
+ }
140
+
141
+ void *xt_raw_realloc(void *pointer, size_t new_size) {
142
+ if (pointer == NULL) return xt_raw_alloc(new_size);
143
+ unsigned char *block = (unsigned char *)pointer - XT_RAW_HEADER_SIZE;
144
+ xt_raw_header *header = (xt_raw_header *)block;
145
+ size_t old_size = (size_t)header->size;
146
+ void *grown = xt_raw_alloc(new_size);
147
+ memcpy(grown, pointer, old_size < new_size ? old_size : new_size);
148
+ xt_raw_free(pointer);
149
+ return grown;
150
+ }
151
+
@@ -0,0 +1,126 @@
1
+ /*
2
+ * xbintsc runtime — opt-in call counters for performance work.
3
+ *
4
+ * `XT_PROFILE=1` increments one counter per entry of the runtime's hottest
5
+ * helpers and prints the totals at exit. A counter is one branch and one
6
+ * increment, so a profiled run keeps the shape of the workload; pair the counts
7
+ * with a microbenchmark of the same helper to attribute time.
8
+ *
9
+ * This is diagnostics, not a feature: the counters are off unless the variable
10
+ * is set, and the dump goes to stderr.
11
+ */
12
+
13
+ #include "rt_internal.h"
14
+
15
+ #include <stdio.h>
16
+ #include <stdlib.h>
17
+ #include <string.h>
18
+
19
+ #define XT_PROFILE_KEY_SLOTS 512
20
+ #define XT_PROFILE_KEY_PROBES 8
21
+
22
+ typedef struct {
23
+ char *key;
24
+ unsigned long long count;
25
+ } xt_profile_key;
26
+
27
+ static xt_profile_key g_keys[XT_PROFILE_KEY_SLOTS];
28
+ static unsigned long long g_alloc_kinds[32];
29
+
30
+ int xt_profile_on = 0;
31
+ unsigned long long xt_profile_calls[XT_PROFILE_COUNT];
32
+
33
+ void xt_profile_note_alloc(int kind) {
34
+ if (!xt_profile_on) return;
35
+ if (kind >= 0 && kind < 32) g_alloc_kinds[kind]++;
36
+ }
37
+
38
+ void xt_profile_note_cstr(const char *key) {
39
+ if (!xt_profile_on || key == NULL) return;
40
+ uint64_t hash = 1469598103934665603ull;
41
+ for (const char *p = key; *p != '\0'; p++) {
42
+ hash ^= (unsigned char)*p;
43
+ hash *= 1099511628211ull;
44
+ }
45
+ const size_t start = (size_t)(hash & (XT_PROFILE_KEY_SLOTS - 1));
46
+ for (size_t probe = 0; probe < XT_PROFILE_KEY_PROBES; probe++) {
47
+ xt_profile_key *slot = &g_keys[(start + probe) & (XT_PROFILE_KEY_SLOTS - 1)];
48
+ if (slot->key == NULL) {
49
+ const size_t length = strlen(key) + 1;
50
+ char *copy = (char *)malloc(length);
51
+ if (copy == NULL) return;
52
+ memcpy(copy, key, length);
53
+ slot->key = copy;
54
+ slot->count = 1;
55
+ return;
56
+ }
57
+ if (strcmp(slot->key, key) == 0) {
58
+ slot->count++;
59
+ return;
60
+ }
61
+ }
62
+ }
63
+
64
+ static const char *const xt_profile_kind_names[32] = {
65
+ "none", "string", "object", "array", "function",
66
+ "promise", "map", "set", "date", "regexp",
67
+ "symbol", "bigint", "error", "generator", "iterator",
68
+ "async", "async-iter", "weak-map", "weak-set", "weak-ref",
69
+ "array-buffer",
70
+ };
71
+
72
+ static const char *const xt_profile_names[XT_PROFILE_COUNT] = {
73
+ "alloc",
74
+ "gc",
75
+ "math",
76
+ "set",
77
+ "get-ic",
78
+ "method-ic",
79
+ "object-set",
80
+ "object-insert",
81
+ "object-find",
82
+ "object-reserve",
83
+ "closure-call",
84
+ "array-get",
85
+ "string-cstr",
86
+ "typed-read",
87
+ "typed-write",
88
+ };
89
+
90
+ static void xt_profile_dump(void) {
91
+ if (!xt_profile_on) return;
92
+ for (int i = 0; i < XT_PROFILE_COUNT; i++) {
93
+ if (xt_profile_calls[i] == 0) continue;
94
+ fprintf(stderr, "xt_profile: %-14s %llu\n", xt_profile_names[i], xt_profile_calls[i]);
95
+ }
96
+ for (int kind = 0; kind < 32; kind++) {
97
+ if (g_alloc_kinds[kind] == 0) continue;
98
+ const char *name = kind < (int)(sizeof(xt_profile_kind_names) / sizeof(char *))
99
+ ? xt_profile_kind_names[kind]
100
+ : "other";
101
+ fprintf(stderr, "xt_profile: alloc %-12s %llu\n", name, g_alloc_kinds[kind]);
102
+ }
103
+ /* The twenty most frequent C-string property names: each was an allocated
104
+ * xt_string unless the runtime interns it. */
105
+ for (int rank = 0; rank < 20; rank++) {
106
+ long best = -1;
107
+ unsigned long long best_count = 0;
108
+ for (int i = 0; i < XT_PROFILE_KEY_SLOTS; i++) {
109
+ if (g_keys[i].key == NULL || g_keys[i].count == 0) continue;
110
+ if (g_keys[i].count > best_count) {
111
+ best_count = g_keys[i].count;
112
+ best = i;
113
+ }
114
+ }
115
+ if (best < 0) break;
116
+ fprintf(stderr, "xt_profile: key %-24s %llu\n", g_keys[best].key, g_keys[best].count);
117
+ g_keys[best].count = 0;
118
+ }
119
+ }
120
+
121
+ void xt_profile_init(void) {
122
+ const char *env = getenv("XT_PROFILE");
123
+ if (env == NULL || env[0] == '\0' || env[0] == '0') return;
124
+ xt_profile_on = 1;
125
+ atexit(xt_profile_dump);
126
+ }
@@ -335,9 +335,14 @@ static xt_value xt_function_has_instance(xt_value thisValue, xt_value env, int32
335
335
  xt_value xt_function_has_instance_method(void) {
336
336
  static xt_value cached = 0;
337
337
  if (cached == 0) {
338
+ /* Root the slot before the first allocation. A lazy getter that stores into
339
+ * a static and only registers it afterwards loses the value to a collection
340
+ * that runs while building it: the static is not on the stack, so the
341
+ * collector never sees the new method. Building it allocates its name
342
+ * string, which is exactly such a collection point. */
343
+ xt_gc_add_root(&cached);
338
344
  cached = xt_closure_new((void *)xt_function_has_instance, 0, NULL);
339
345
  xt_function_set_metadata(cached, xt_string_from_cstr("[Symbol.hasInstance]"), 1);
340
- xt_gc_add_root(&cached);
341
346
  }
342
347
  return cached;
343
348
  }
@@ -515,3 +520,4 @@ xt_value xt_call_method(xt_value target, xt_value name, int32_t argc, xt_value *
515
520
  }
516
521
  xt_throw_no_method("value", method);
517
522
  }
523
+
@@ -2,74 +2,289 @@
2
2
  /* Math */
3
3
  /* ------------------------------------------------------------------------- */
4
4
 
5
+ /*
6
+ * `Math.cbrt` uses fdlibm's cbrt instead of the host libm. The host result is
7
+ * not portable: glibc's algorithm has ~1 ulp of error and computes
8
+ * `cbrt(27) = 3.0000000000000004`, while macOS/Windows libm and Node return
9
+ * `3`. The differential suite caught that on Linux. V8 (and therefore Node)
10
+ * ships this exact fdlibm version in `src/base/ieee754.cc`, so borrowing it
11
+ * makes `Math.cbrt` bit-identical with Node on every host.
12
+ *
13
+ * The following is adapted from fdlibm (http://www.netlib.org/fdlibm).
14
+ *
15
+ * ====================================================
16
+ * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
17
+ *
18
+ * Developed at SunSoft, a Sun Microsystems, Inc. business.
19
+ * Permission to use, copy, modify, and distribute this
20
+ * software is freely granted, provided that this notice
21
+ * is preserved.
22
+ * ====================================================
23
+ *
24
+ * V8 compiles its ieee754 code with `-ffp-contract=off`; the pragma keeps a
25
+ * compiler from fusing the polynomial's multiply-adds on FMA targets, which
26
+ * would change the low bits.
27
+ */
28
+ #pragma STDC FP_CONTRACT OFF
29
+ static double xt_math_cbrt(double x) {
30
+ /* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */
31
+ static const uint32_t B1 = 715094163u; /* (1023-1023/3-0.03306235651)*2**20 */
32
+ static const uint32_t B2 = 696219795u; /* (1023-1023/3-54/3-0.03306235651)*2**20 */
33
+ static const double P0 = 1.87595182427177009643;
34
+ static const double P1 = -1.88497979543377169875;
35
+ static const double P2 = 1.621429720105354466140;
36
+ static const double P3 = -0.758397934778766047437;
37
+ static const double P4 = 0.145996192886612446982;
38
+
39
+ uint64_t bits;
40
+ uint32_t hx, low, sign, high;
41
+ double r, s, t, w;
42
+
43
+ memcpy(&bits, &x, sizeof(bits));
44
+ hx = (uint32_t)(bits >> 32);
45
+ low = (uint32_t)bits;
46
+ sign = hx & 0x80000000u; /* sign= sign(x) */
47
+ hx ^= sign;
48
+ if (hx >= 0x7FF00000u) return x + x; /* cbrt(NaN,INF) is itself */
49
+
50
+ if (hx < 0x00100000u) { /* zero or subnormal? */
51
+ if ((hx | low) == 0) return x; /* cbrt(0) is itself */
52
+ t = 0x1p54 * x; /* scale the subnormal into the normal range */
53
+ memcpy(&bits, &t, sizeof(bits));
54
+ high = (uint32_t)(bits >> 32);
55
+ bits = (uint64_t)(sign | ((high & 0x7FFFFFFFu) / 3u + B2)) << 32;
56
+ } else {
57
+ bits = (uint64_t)(sign | (hx / 3u + B1)) << 32;
58
+ }
59
+ memcpy(&t, &bits, sizeof(t));
60
+
61
+ /* New cbrt to 23 bits: cbrt(x) ~= t*P(t**3/x), then one Newton step. */
62
+ r = (t * t) * (t / x);
63
+ t = t * ((P0 + r * (P1 + r * P2)) + ((r * r) * r) * (P3 + r * P4));
64
+
65
+ /* Round t away from zero to 23 bits so it is slightly above |cbrt(x)|. */
66
+ memcpy(&bits, &t, sizeof(bits));
67
+ bits = (bits + 0x80000000ull) & 0xFFFFFFFFC0000000ull;
68
+ memcpy(&t, &bits, sizeof(t));
69
+
70
+ /* One Newton iteration to 53 bits with error < 0.667 ulps. */
71
+ s = t * t; /* t*t is exact */
72
+ r = x / s; /* error <= 0.5 ulps; |r| < |t| */
73
+ w = t + t; /* t+t is exact */
74
+ r = (r - t) / (w + r); /* r-t is exact; w+r ~= 3*t */
75
+ t = t + t * r; /* error <= 0.5 + 0.5/3 + epsilon */
76
+ return t;
77
+ }
78
+ #pragma STDC FP_CONTRACT ON
79
+
5
80
  xt_value xt_math_call(xt_value name, int32_t argc, xt_value *argv) {
81
+ XT_PROFILE_TICK(XT_PROFILE_MATH);
6
82
  const char *fn = xt_string_data(name);
83
+ if (!fn) return xt_number(NAN);
7
84
  double a = xt_to_number(xt_arg_at(argc, argv, 0));
8
85
  double b = xt_to_number(xt_arg_at(argc, argv, 1));
9
- if (!fn) return xt_number(NAN);
10
- if (strcmp(fn, "abs") == 0) return xt_number(fabs(a));
11
- if (strcmp(fn, "floor") == 0) return xt_number(floor(a));
12
- if (strcmp(fn, "ceil") == 0) return xt_number(ceil(a));
13
- if (strcmp(fn, "round") == 0) return xt_number(floor(a + 0.5));
14
- if (strcmp(fn, "trunc") == 0) return xt_number(trunc(a));
15
- if (strcmp(fn, "sqrt") == 0) return xt_number(sqrt(a));
16
- if (strcmp(fn, "cbrt") == 0) return xt_number(cbrt(a));
17
- if (strcmp(fn, "pow") == 0) return xt_number(pow(a, b));
18
- if (strcmp(fn, "exp") == 0) return xt_number(exp(a));
19
- if (strcmp(fn, "log") == 0) return xt_number(log(a));
20
- if (strcmp(fn, "log2") == 0) return xt_number(log2(a));
21
- if (strcmp(fn, "log10") == 0) return xt_number(log10(a));
22
- if (strcmp(fn, "log1p") == 0) return xt_number(log1p(a));
23
- if (strcmp(fn, "expm1") == 0) return xt_number(expm1(a));
24
- if (strcmp(fn, "sinh") == 0) return xt_number(sinh(a));
25
- if (strcmp(fn, "cosh") == 0) return xt_number(cosh(a));
26
- if (strcmp(fn, "tanh") == 0) return xt_number(tanh(a));
27
- if (strcmp(fn, "asinh") == 0) return xt_number(asinh(a));
28
- if (strcmp(fn, "acosh") == 0) return xt_number(acosh(a));
29
- if (strcmp(fn, "atanh") == 0) return xt_number(atanh(a));
30
- if (strcmp(fn, "fround") == 0) return xt_number((double)(float)a);
31
- if (strcmp(fn, "imul") == 0) return xt_number((double)((int32_t)xt_to_int32(xt_arg_at(argc, argv, 0)) * (int32_t)xt_to_int32(xt_arg_at(argc, argv, 1))));
32
- if (strcmp(fn, "clz32") == 0) {
33
- uint32_t v = (uint32_t)xt_to_int32(xt_arg_at(argc, argv, 0));
34
- int count = 0;
35
- if (v == 0) count = 32;
36
- else { while (!(v & 0x80000000u)) { v <<= 1; count++; } }
37
- return xt_number((double)count);
86
+ /* Dispatch on the first byte before comparing names. A flat chain over ~35
87
+ * names paid up to 35 `strcmp`s per call, and numeric code calls
88
+ * `Math.imul`/`floor`/`exp` hundreds of millions of times (procedural noise,
89
+ * geometry), so the difference is seconds of startup on a large page. */
90
+ switch (fn[0]) {
91
+ case 'a':
92
+ if (strcmp(fn, "abs") == 0) return xt_number(fabs(a));
93
+ if (strcmp(fn, "acos") == 0) return xt_number(acos(a));
94
+ if (strcmp(fn, "acosh") == 0) return xt_number(acosh(a));
95
+ if (strcmp(fn, "asin") == 0) return xt_number(asin(a));
96
+ if (strcmp(fn, "asinh") == 0) return xt_number(asinh(a));
97
+ if (strcmp(fn, "atan") == 0) return xt_number(atan(a));
98
+ if (strcmp(fn, "atan2") == 0) return xt_number(atan2(a, b));
99
+ if (strcmp(fn, "atanh") == 0) return xt_number(atanh(a));
100
+ break;
101
+ case 'c':
102
+ if (strcmp(fn, "cbrt") == 0) return xt_number(xt_math_cbrt(a));
103
+ if (strcmp(fn, "ceil") == 0) return xt_number(ceil(a));
104
+ if (strcmp(fn, "clz32") == 0) {
105
+ uint32_t v = (uint32_t)xt_to_int32(xt_arg_at(argc, argv, 0));
106
+ int count = 0;
107
+ if (v == 0) count = 32;
108
+ else { while (!(v & 0x80000000u)) { v <<= 1; count++; } }
109
+ return xt_number((double)count);
110
+ }
111
+ if (strcmp(fn, "cos") == 0) return xt_number(cos(a));
112
+ if (strcmp(fn, "cosh") == 0) return xt_number(cosh(a));
113
+ break;
114
+ case 'e':
115
+ if (strcmp(fn, "exp") == 0) return xt_number(exp(a));
116
+ if (strcmp(fn, "expm1") == 0) return xt_number(expm1(a));
117
+ break;
118
+ case 'f':
119
+ if (strcmp(fn, "floor") == 0) return xt_number(floor(a));
120
+ if (strcmp(fn, "fround") == 0) return xt_number((double)(float)a);
121
+ break;
122
+ case 'h':
123
+ if (strcmp(fn, "hypot") == 0) {
124
+ /* `Math.hypot(...values)` takes any number of arguments (ECMA-262
125
+ * 21.3.2.18): every argument participates, NaN wins, and the sum is
126
+ * scaled by the largest magnitude so `hypot(1e308, 1e308)` does not
127
+ * overflow. */
128
+ double scale = 0.0;
129
+ for (int32_t i = 0; i < argc; i++) {
130
+ double v = fabs(xt_to_number(argv[i]));
131
+ if (isnan(v)) return xt_number(NAN);
132
+ if (v > scale) scale = v;
133
+ }
134
+ if (scale == INFINITY) return xt_number(INFINITY);
135
+ if (scale == 0.0) return xt_number(0.0);
136
+ double sum = 0.0;
137
+ for (int32_t i = 0; i < argc; i++) {
138
+ double v = xt_to_number(argv[i]) / scale;
139
+ sum += v * v;
140
+ }
141
+ return xt_number(scale * sqrt(sum));
142
+ }
143
+ break;
144
+ case 'i':
145
+ if (strcmp(fn, "imul") == 0)
146
+ return xt_number((double)((int32_t)xt_to_int32(xt_arg_at(argc, argv, 0)) *
147
+ (int32_t)xt_to_int32(xt_arg_at(argc, argv, 1))));
148
+ break;
149
+ case 'l':
150
+ if (strcmp(fn, "log") == 0) return xt_number(log(a));
151
+ if (strcmp(fn, "log10") == 0) return xt_number(log10(a));
152
+ if (strcmp(fn, "log1p") == 0) return xt_number(log1p(a));
153
+ if (strcmp(fn, "log2") == 0) return xt_number(log2(a));
154
+ break;
155
+ case 'm':
156
+ if (strcmp(fn, "max") == 0) {
157
+ double result = -INFINITY;
158
+ for (int32_t i = 0; i < argc; i++) {
159
+ double v = xt_to_number(argv[i]);
160
+ if (isnan(v)) return xt_number(NAN);
161
+ if (v > result) result = v;
162
+ }
163
+ return xt_number(result);
164
+ }
165
+ if (strcmp(fn, "min") == 0) {
166
+ double result = INFINITY;
167
+ for (int32_t i = 0; i < argc; i++) {
168
+ double v = xt_to_number(argv[i]);
169
+ if (isnan(v)) return xt_number(NAN);
170
+ if (v < result) result = v;
171
+ }
172
+ return xt_number(result);
173
+ }
174
+ break;
175
+ case 'p':
176
+ if (strcmp(fn, "pow") == 0) return xt_number(pow(a, b));
177
+ break;
178
+ case 'r':
179
+ if (strcmp(fn, "random") == 0) return xt_number(xt_random_double());
180
+ if (strcmp(fn, "round") == 0) return xt_number(floor(a + 0.5));
181
+ break;
182
+ case 's':
183
+ if (strcmp(fn, "sign") == 0) {
184
+ if (isnan(a)) return xt_number(NAN);
185
+ return xt_number(a > 0 ? 1.0 : a < 0 ? -1.0 : a);
186
+ }
187
+ if (strcmp(fn, "sin") == 0) return xt_number(sin(a));
188
+ if (strcmp(fn, "sinh") == 0) return xt_number(sinh(a));
189
+ if (strcmp(fn, "sqrt") == 0) return xt_number(sqrt(a));
190
+ break;
191
+ case 't':
192
+ if (strcmp(fn, "tan") == 0) return xt_number(tan(a));
193
+ if (strcmp(fn, "tanh") == 0) return xt_number(tanh(a));
194
+ if (strcmp(fn, "trunc") == 0) return xt_number(trunc(a));
195
+ break;
196
+ default:
197
+ break;
38
198
  }
39
- if (strcmp(fn, "sin") == 0) return xt_number(sin(a));
40
- if (strcmp(fn, "cos") == 0) return xt_number(cos(a));
41
- if (strcmp(fn, "tan") == 0) return xt_number(tan(a));
42
- if (strcmp(fn, "asin") == 0) return xt_number(asin(a));
43
- if (strcmp(fn, "acos") == 0) return xt_number(acos(a));
44
- if (strcmp(fn, "atan") == 0) return xt_number(atan(a));
45
- if (strcmp(fn, "atan2") == 0) return xt_number(atan2(a, b));
46
- if (strcmp(fn, "hypot") == 0) return xt_number(hypot(a, b));
47
- if (strcmp(fn, "sign") == 0) {
48
- if (isnan(a)) return xt_number(NAN);
49
- return xt_number(a > 0 ? 1.0 : a < 0 ? -1.0 : a);
199
+ return xt_number(NAN);
200
+ }
201
+
202
+
203
+ /* -- direct call forms ----------------------------------------------------- */
204
+
205
+ /*
206
+ * `Math.floor(x)` used to lower to `xt_math_call("floor", 1, &x)`: a name
207
+ * dispatch plus an argument array for a call whose arity and name the compiler
208
+ * already knows. A numeric kernel calls these hundreds of times per pixel, so
209
+ * codegen emits the fixed-arity form below instead — the arguments arrive in
210
+ * registers and there is no name to resolve. `min`/`max` stay variadic and keep
211
+ * `xt_math_call`. Semantics are identical: ToNumber first, then the platform
212
+ * operation.
213
+ */
214
+ #define XT_MATH_UNARY(name, expression) \
215
+ xt_value xt_math1_##name(xt_value arg) { \
216
+ double x = xt_to_number(arg); \
217
+ return xt_number(expression); \
50
218
  }
51
- if (strcmp(fn, "random") == 0) return xt_number(xt_random_double());
52
- if (strcmp(fn, "min") == 0) {
53
- double result = INFINITY;
54
- for (int32_t i = 0; i < argc; i++) {
55
- double v = xt_to_number(argv[i]);
56
- if (isnan(v)) return xt_number(NAN);
57
- if (v < result) result = v;
58
- }
59
- return xt_number(result);
219
+
220
+ #define XT_MATH_BINARY(name, expression) \
221
+ xt_value xt_math2_##name(xt_value left, xt_value right) { \
222
+ double x = xt_to_number(left); \
223
+ double y = xt_to_number(right); \
224
+ return xt_number(expression); \
60
225
  }
61
- if (strcmp(fn, "max") == 0) {
62
- double result = -INFINITY;
63
- for (int32_t i = 0; i < argc; i++) {
64
- double v = xt_to_number(argv[i]);
65
- if (isnan(v)) return xt_number(NAN);
66
- if (v > result) result = v;
226
+
227
+ XT_MATH_UNARY(abs, fabs(x))
228
+ XT_MATH_UNARY(floor, floor(x))
229
+ XT_MATH_UNARY(ceil, ceil(x))
230
+ XT_MATH_UNARY(round, floor(x + 0.5))
231
+ XT_MATH_UNARY(trunc, trunc(x))
232
+ XT_MATH_UNARY(sqrt, sqrt(x))
233
+ XT_MATH_UNARY(cbrt, xt_math_cbrt(x))
234
+ XT_MATH_UNARY(exp, exp(x))
235
+ XT_MATH_UNARY(log, log(x))
236
+ XT_MATH_UNARY(log2, log2(x))
237
+ XT_MATH_UNARY(log10, log10(x))
238
+ XT_MATH_UNARY(log1p, log1p(x))
239
+ XT_MATH_UNARY(expm1, expm1(x))
240
+ XT_MATH_UNARY(sinh, sinh(x))
241
+ XT_MATH_UNARY(cosh, cosh(x))
242
+ XT_MATH_UNARY(tanh, tanh(x))
243
+ XT_MATH_UNARY(asinh, asinh(x))
244
+ XT_MATH_UNARY(acosh, acosh(x))
245
+ XT_MATH_UNARY(atanh, atanh(x))
246
+ XT_MATH_UNARY(fround, (double)(float)x)
247
+ XT_MATH_UNARY(sin, sin(x))
248
+ XT_MATH_UNARY(cos, cos(x))
249
+ XT_MATH_UNARY(tan, tan(x))
250
+ XT_MATH_UNARY(asin, asin(x))
251
+ XT_MATH_UNARY(acos, acos(x))
252
+ XT_MATH_UNARY(atan, atan(x))
253
+
254
+ XT_MATH_BINARY(pow, pow(x, y))
255
+ XT_MATH_BINARY(atan2, atan2(x, y))
256
+ XT_MATH_BINARY(hypot, hypot(x, y))
257
+
258
+ #undef XT_MATH_UNARY
259
+ #undef XT_MATH_BINARY
260
+
261
+ xt_value xt_math1_clz32(xt_value arg) {
262
+ uint32_t v = (uint32_t)xt_to_int32(arg);
263
+ int count = 0;
264
+ if (v == 0) {
265
+ count = 32;
266
+ } else {
267
+ while (!(v & 0x80000000u)) {
268
+ v <<= 1;
269
+ count++;
67
270
  }
68
- return xt_number(result);
69
271
  }
70
- return xt_number(NAN);
272
+ return xt_number((double)count);
273
+ }
274
+
275
+ xt_value xt_math1_sign(xt_value arg) {
276
+ double x = xt_to_number(arg);
277
+ if (isnan(x)) return xt_number(NAN);
278
+ return xt_number(x > 0 ? 1.0 : x < 0 ? -1.0 : x);
71
279
  }
72
280
 
281
+ xt_value xt_math0_random(void) { return xt_number(xt_random_double()); }
282
+
283
+ xt_value xt_math2_imul(xt_value left, xt_value right) {
284
+ int32_t a = (int32_t)xt_to_int32(left);
285
+ int32_t b = (int32_t)xt_to_int32(right);
286
+ return xt_number((double)(a * b));
287
+ }
73
288
 
74
289
  /* -- first-class Math ------------------------------------------------------ */
75
290
 
@@ -330,8 +330,7 @@ static int xt_json_replacer_write(xt_json_replacer_context *ctx, xt_value holder
330
330
  value = xt_call_method(value, xt_string_from_cstr("toJSON"), 1, &key);
331
331
  } else if (XT_IS_ARRAY(value) ||
332
332
  (XT_IS_OBJECT(value) &&
333
- (((xt_object *)XT_GET_PTR(value))->header.kind == XT_OBJECT_KIND_OBJECT ||
334
- ((xt_object *)XT_GET_PTR(value))->header.kind == XT_OBJECT_KIND_ERROR))) {
333
+ xt_kind_is_object(((xt_object *)XT_GET_PTR(value))->header.kind))) {
335
334
  xt_value toJSON = XT_IS_ARRAY(value) ? xt_get(value, xt_string_from_cstr("toJSON"))
336
335
  : xt_object_get(value, xt_string_from_cstr("toJSON"));
337
336
  if (XT_IS_FUNCTION(toJSON)) {