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
@@ -54,21 +54,11 @@ static size_t g_root_provider_capacity = 0;
54
54
 
55
55
  /* ------------------------------------------------------------------------- */
56
56
  /* Active stack regions */
57
+ /* */
58
+ /* The region table lives in `xt_gc_stack.c`; it grows on demand because an */
59
+ /* async call chain can be deeper than any fixed limit. */
57
60
  /* ------------------------------------------------------------------------- */
58
61
 
59
- #define XT_GC_MAX_STACKS 64
60
- typedef struct {
61
- uintptr_t low;
62
- uintptr_t high;
63
- /* Lower bound captured from the stack below this region when a deeper region
64
- * was entered on top of it. `UINTPTR_MAX` means "this is the top region"; the
65
- * collector then scans from the current stack pointer. */
66
- uintptr_t scan_low;
67
- } xt_stack_region;
68
-
69
- static xt_stack_region g_stack_regions[XT_GC_MAX_STACKS];
70
- static int g_stack_depth = 0;
71
-
72
62
  /* ------------------------------------------------------------------------- */
73
63
  /* Mark stack and address set */
74
64
  /* ------------------------------------------------------------------------- */
@@ -144,6 +134,8 @@ static void xt_heap_link(xt_header *header) {
144
134
  }
145
135
 
146
136
  void *xt_alloc(size_t size, int kind) {
137
+ XT_PROFILE_TICK(XT_PROFILE_ALLOC);
138
+ if (xt_profile_on) xt_profile_note_alloc(kind);
147
139
  if (!g_gc_initialised) xt_gc_init();
148
140
  if (xt_gc_worker_thread) {
149
141
  void *block = calloc(1, size);
@@ -162,7 +154,15 @@ void *xt_alloc(size_t size, int kind) {
162
154
  * the conservative stack scan. */
163
155
  if (g_gc_armed && !g_gc_running && g_heap_bytes >= g_gc_threshold) xt_gc_collect();
164
156
 
165
- xt_header *header = (xt_header *)calloc(1, size);
157
+ xt_header *header = (xt_header *)xt_pool_alloc(size);
158
+ if (header != NULL) {
159
+ header->kind = (uint8_t)kind;
160
+ header->flags = XT_GC_POOLED;
161
+ header->size = (uint32_t)size;
162
+ xt_heap_link(header);
163
+ return header;
164
+ }
165
+ header = (xt_header *)calloc(1, size);
166
166
  if (!header) {
167
167
  fprintf(stderr, "xbintsc: out of memory allocating %zu bytes\n", size);
168
168
  abort();
@@ -170,7 +170,6 @@ void *xt_alloc(size_t size, int kind) {
170
170
  header->kind = (uint8_t)kind;
171
171
  header->flags = 0;
172
172
  header->size = (uint32_t)size;
173
- header->size = (uint32_t)size;
174
173
  xt_heap_link(header);
175
174
  return header;
176
175
  }
@@ -185,30 +184,20 @@ size_t xt_heap_allocations(void) { return g_heap_allocations; }
185
184
  void xt_gc_set_stack_base(void *base) {
186
185
  uintptr_t high = (uintptr_t)base;
187
186
  if (high == 0) return;
188
- if (g_stack_depth == 0) {
189
- g_stack_regions[0].low = 0;
190
- g_stack_regions[0].high = high;
191
- g_stack_regions[0].scan_low = UINTPTR_MAX;
192
- g_stack_depth = 1;
193
- } else if (high > g_stack_regions[0].high) {
194
- g_stack_regions[0].high = high;
195
- }
187
+ xt_gc_init_main_stack(high);
196
188
  }
197
189
 
198
190
  void xt_gc_init(void) {
199
191
  if (g_gc_initialised) return;
200
192
  g_gc_initialised = 1;
193
+ /* The generated `main` calls this first, before any module code, so the
194
+ * `performance.now()` origin is the start of the program. */
195
+ xt_time_init();
196
+ xt_profile_init();
201
197
  uintptr_t high = (uintptr_t)&high;
202
- if (g_stack_depth == 0) {
203
- g_stack_regions[0].low = 0;
204
- g_stack_regions[0].high = high;
205
- g_stack_regions[0].scan_low = UINTPTR_MAX;
206
- g_stack_depth = 1;
207
- } else if (high > g_stack_regions[0].high) {
208
- /* `xt_gc_set_stack_base` normally installed a bound above the caller's
209
- * frame already; keep the higher of the two. */
210
- g_stack_regions[0].high = high;
211
- }
198
+ /* `xt_gc_set_stack_base` normally installed a bound above the caller's frame
199
+ * already; the helper keeps the higher of the two. */
200
+ xt_gc_init_main_stack(high);
212
201
  const char *env = getenv("XT_GC_THRESHOLD");
213
202
  if (env && *env) {
214
203
  long long value = atoll(env);
@@ -269,24 +258,6 @@ void xt_gc_register_root_provider(void (*provider)(void)) {
269
258
  }
270
259
  g_root_providers[g_root_provider_count++] = provider;
271
260
  }
272
- void xt_gc_push_stack(uintptr_t low, uintptr_t high) {
273
- if (g_stack_depth >= XT_GC_MAX_STACKS) return;
274
- g_stack_regions[g_stack_depth].low = low;
275
- g_stack_regions[g_stack_depth].high = high;
276
- g_stack_regions[g_stack_depth].scan_low = low;
277
- g_stack_depth++;
278
- }
279
- void xt_gc_suspend(uintptr_t sp) {
280
- if (g_stack_depth <= 0) return;
281
- uintptr_t *scan = &g_stack_regions[g_stack_depth - 1].scan_low;
282
- if (*scan == UINTPTR_MAX || sp < *scan) *scan = sp;
283
- }
284
- void xt_gc_pop_stack(void) {
285
- if (g_stack_depth <= 1) return;
286
- g_stack_depth--;
287
- /* Back on the underlying stack: resume scanning it from the current frame. */
288
- if (g_stack_depth > 0) g_stack_regions[g_stack_depth - 1].scan_low = UINTPTR_MAX;
289
- }
290
261
  /* ------------------------------------------------------------------------- */
291
262
  /* Marking */
292
263
  /* ------------------------------------------------------------------------- */
@@ -319,10 +290,12 @@ static void xt_gc_gray_push_checked(xt_header *header) {
319
290
  void xt_gc_mark_header(xt_header *header) { xt_gc_gray_push_checked(header); }
320
291
 
321
292
  void xt_gc_mark_value(xt_value value) {
322
- if (XT_IS_STRING(value) || XT_IS_OBJECT(value) || XT_IS_ARRAY(value) || XT_IS_FUNCTION(value) ||
323
- XT_IS_BIGINT(value)) {
324
- xt_gc_gray_push((xt_header *)XT_GET_PTR(value));
325
- }
293
+ /* One tag compare rejects every number (and undefined/null/booleans), which
294
+ * is most of a scene's property slots; the five individual `XT_IS_*` tests
295
+ * made marking a large live set spend most of its time in predictable
296
+ * branches. */
297
+ if ((value & XT_TAG_MASK) < XT_TAG_BIGINT) return;
298
+ xt_gc_gray_push((xt_header *)XT_GET_PTR(value));
326
299
  }
327
300
  int xt_gc_value_is_marked(xt_value value) {
328
301
  if (!(XT_IS_STRING(value) || XT_IS_OBJECT(value) || XT_IS_ARRAY(value) ||
@@ -377,41 +350,59 @@ static void xt_gc_process(xt_header *header) {
377
350
  /* Numbers/bytes only: no references to follow. */
378
351
  return;
379
352
  case XT_OBJECT_KIND_OBJECT:
380
- case XT_OBJECT_KIND_ERROR: {
353
+ case XT_OBJECT_KIND_ERROR:
354
+ case XT_OBJECT_KIND_ARRAY_BUFFER: {
355
+ /* Precise trace: every reference in an ordinary object is an `xt_value`
356
+ * (a property key/value/accessor, the prototype and the captured error
357
+ * stack). The old conservative payload scan also looked up the raw
358
+ * `properties`/`shape`/`hooks` pointers and every number in the property
359
+ * table, which made marking a large scene spend most of its time in the
360
+ * heap address table. */
381
361
  xt_object *object = (xt_object *)header;
382
- xt_gc_scan_region(base, payload);
383
- if (object->properties && object->count) {
384
- xt_gc_scan_region(object->properties, sizeof(xt_property) * (size_t)object->count);
362
+ xt_gc_mark_value(object->prototype);
363
+ xt_gc_mark_value(object->error_stack);
364
+ for (uint32_t i = 0; i < object->count; i++) {
365
+ xt_gc_mark_value(object->properties[i].key);
366
+ xt_gc_mark_value(object->properties[i].value);
367
+ xt_gc_mark_value(object->properties[i].getter);
368
+ xt_gc_mark_value(object->properties[i].setter);
385
369
  }
386
370
  return;
387
371
  }
388
372
  case XT_OBJECT_KIND_ARRAY: {
389
373
  xt_array *array = (xt_array *)header;
390
- xt_gc_scan_region(base, payload);
391
- if (array->items && array->length) {
392
- xt_gc_scan_region(array->items, sizeof(xt_value) * (size_t)array->length);
393
- }
374
+ xt_gc_mark_value(array->prototype);
375
+ xt_gc_mark_value(array->extra);
376
+ for (uint32_t i = 0; i < array->length; i++) xt_gc_mark_value(array->items[i]);
394
377
  return;
395
378
  }
396
379
  case XT_OBJECT_KIND_FUNCTION: {
397
380
  xt_function *function = (xt_function *)header;
398
- xt_gc_scan_region(base, payload);
399
- if (function->environment && function->environment_count > 0) {
400
- xt_gc_scan_region(function->environment, sizeof(xt_value) * (size_t)function->environment_count);
381
+ if (function->name != NULL) xt_gc_gray_push_checked(&function->name->header);
382
+ if (function->properties != NULL) xt_gc_gray_push_checked(&function->properties->header);
383
+ xt_gc_mark_value(function->prototype);
384
+ for (int32_t i = 0; i < function->environment_count; i++) {
385
+ xt_gc_mark_value(function->environment[i]);
401
386
  }
402
387
  return;
403
388
  }
404
389
  case XT_OBJECT_KIND_MAP: {
405
390
  xt_map *map = (xt_map *)header;
406
- xt_gc_scan_region(base, payload);
407
- if (map->keys && map->count) xt_gc_scan_region(map->keys, sizeof(xt_value) * (size_t)map->count);
408
- if (map->values && map->count) xt_gc_scan_region(map->values, sizeof(xt_value) * (size_t)map->count);
391
+ for (uint32_t i = 0; i < map->count; i++) {
392
+ xt_gc_mark_value(map->keys[i]);
393
+ xt_gc_mark_value(map->values[i]);
394
+ }
409
395
  return;
410
396
  }
411
397
  case XT_OBJECT_KIND_SET: {
412
398
  xt_set_object *set = (xt_set_object *)header;
413
- xt_gc_scan_region(base, payload);
414
- if (set->values && set->count) xt_gc_scan_region(set->values, sizeof(xt_value) * (size_t)set->count);
399
+ for (uint32_t i = 0; i < set->count; i++) xt_gc_mark_value(set->values[i]);
400
+ return;
401
+ }
402
+ case XT_OBJECT_KIND_REGEXP: {
403
+ xt_regexp *regexp = (xt_regexp *)header;
404
+ xt_gc_mark_value(regexp->source);
405
+ xt_gc_mark_value(regexp->flags);
415
406
  return;
416
407
  }
417
408
  case XT_OBJECT_KIND_PROMISE:
@@ -442,52 +433,6 @@ static void xt_gc_process(xt_header *header) {
442
433
  /* ------------------------------------------------------------------------- */
443
434
  /* Release */
444
435
  /* ------------------------------------------------------------------------- */
445
- static void xt_gc_release(xt_header *header) {
446
- switch (header->kind) {
447
- case XT_OBJECT_KIND_STRING:
448
- free(((xt_string *)header)->flat);
449
- break;
450
- case XT_OBJECT_KIND_BIGINT:
451
- free(((xt_bigint *)header)->limbs);
452
- break;
453
- case XT_OBJECT_KIND_OBJECT:
454
- case XT_OBJECT_KIND_ERROR:
455
- free(((xt_object *)header)->properties);
456
- break;
457
- case XT_OBJECT_KIND_ARRAY:
458
- free(((xt_array *)header)->items);
459
- break;
460
- case XT_OBJECT_KIND_FUNCTION:
461
- free(((xt_function *)header)->environment);
462
- break;
463
- case XT_OBJECT_KIND_MAP:
464
- free(((xt_map *)header)->keys);
465
- free(((xt_map *)header)->values);
466
- break;
467
- case XT_OBJECT_KIND_SET:
468
- free(((xt_set_object *)header)->values);
469
- break;
470
- case XT_OBJECT_KIND_WEAK_MAP:
471
- free(((xt_weak_map *)header)->keys);
472
- free(((xt_weak_map *)header)->values);
473
- break;
474
- case XT_OBJECT_KIND_WEAK_SET:
475
- free(((xt_weak_set *)header)->values);
476
- break;
477
- case XT_OBJECT_KIND_PROMISE:
478
- xt_promise_gc_release(header);
479
- break;
480
- case XT_OBJECT_KIND_GENERATOR:
481
- xt_generator_gc_release(header);
482
- break;
483
- case XT_OBJECT_KIND_ASYNC:
484
- xt_async_gc_release(header);
485
- break;
486
- default:
487
- break;
488
- }
489
- free(header);
490
- }
491
436
  /* ------------------------------------------------------------------------- */
492
437
  /* Collection */
493
438
  /* ------------------------------------------------------------------------- */
@@ -504,22 +449,29 @@ static void xt_gc_mark_roots(void) {
504
449
  _setjmp(registers);
505
450
  xt_gc_scan_region(&registers, sizeof(registers));
506
451
  uintptr_t stack_pointer = (uintptr_t)&registers;
507
- for (int i = 0; i < g_stack_depth; i++) {
508
- uintptr_t high = g_stack_regions[i].high;
509
- uintptr_t low = (i == g_stack_depth - 1) ? stack_pointer : g_stack_regions[i].scan_low;
452
+ size_t regionCount = xt_gc_stack_region_count();
453
+ for (size_t i = 0; i < regionCount; i++) {
454
+ uintptr_t high = 0;
455
+ uintptr_t low = 0;
456
+ xt_gc_stack_region_bounds(i, stack_pointer, &low, &high);
510
457
  if (high > low) xt_gc_scan_region((const void *)low, high - low);
511
458
  }
512
459
  }
513
460
  void xt_gc_collect(void) {
461
+ XT_PROFILE_TICK(XT_PROFILE_GC);
514
462
  if (g_gc_running) return;
515
463
  if (!g_gc_initialised) xt_gc_init();
516
464
  g_gc_running = 1;
465
+ const double gc_started_ms = xt_monotonic_ms();
517
466
  const int trace = xt_gc_trace_enabled();
518
467
  const int leak = getenv("XT_GC_LEAK") != NULL;
519
468
  const size_t heap_before = g_heap_bytes;
520
469
  const size_t allocations_before = g_heap_allocations;
521
470
 
471
+ const double phase_started = xt_monotonic_ms();
522
472
  xt_gc_table_build();
473
+ const double phase_table = xt_monotonic_ms() - phase_started;
474
+ const double mark_started = xt_monotonic_ms();
523
475
  xt_gc_mark_roots();
524
476
  /* Ephemeron fixed point: a WeakMap value is reachable only when its key is
525
477
  * reachable. Marking a value can reach further keys, so repeat until a pass
@@ -535,6 +487,8 @@ void xt_gc_collect(void) {
535
487
  xt_weak_gc_clear();
536
488
  xt_weak_gc_compact();
537
489
  if (getenv("XT_GC_VERIFY") != NULL) xt_gc_verify_payloads();
490
+ const double phase_mark = xt_monotonic_ms() - mark_started;
491
+ const double sweep_started = xt_monotonic_ms();
538
492
 
539
493
  xt_header **link = &g_heap_head;
540
494
  xt_header *header = g_heap_head;
@@ -565,20 +519,19 @@ void xt_gc_collect(void) {
565
519
  }
566
520
 
567
521
  xt_gc_table_free();
522
+ const double phase_sweep = xt_monotonic_ms() - sweep_started;
568
523
  g_gc_running = 0;
569
524
 
570
525
  if (!g_gc_threshold_pinned) {
571
- /* Follow the live set: twice the surviving heap is the headroom a
572
- * mark-sweep wants between collections, and the floor keeps a tiny heap
573
- * from collecting on nearly every allocation.
574
- *
575
- * The threshold must *shrink* again, not just ratchet upwards. A doubling
576
- * threshold with a monotonic floor turns any transient growth of the live
577
- * set into exponentially rarer collections: each sweep only reclaims the
578
- * newly dead part of a larger heap, so the heap (and with it the resident
579
- * set) grows without bound. A GUI program that keeps a few extra nodes
580
- * alive for one frame is enough to start that spiral. */
581
- size_t next = g_heap_bytes * 2;
526
+ /* Follow the live set: four times the surviving heap is the headroom a
527
+ * mark-sweep wants between collections for an interactive program. Two
528
+ * times collected too often: a three.js scene that keeps ~380k objects live
529
+ * spent a ~300 ms pause every ~1.2 s, and half of the frame budget went to
530
+ * collections. Four times halves that overhead at a bounded memory cost
531
+ * (the threshold still shrinks with the live set, so it cannot ratchet
532
+ * upwards). The floor keeps a tiny heap from collecting on nearly every
533
+ * allocation. */
534
+ size_t next = g_heap_bytes * 4;
582
535
  if (next < XT_GC_MIN_THRESHOLD) next = XT_GC_MIN_THRESHOLD;
583
536
  g_gc_threshold = next;
584
537
  }
@@ -586,9 +539,11 @@ void xt_gc_collect(void) {
586
539
  g_gc_collections++;
587
540
  if (trace) {
588
541
  fprintf(stderr,
589
- "xt_gc: #%zu heap %zu -> %zu bytes, allocations %zu -> %zu, next trigger %zu\n",
542
+ "xt_gc: #%zu heap %zu -> %zu bytes, allocations %zu -> %zu, next trigger %zu, took %.1fms"
543
+ " (table %.1f, mark %.1f, sweep %.1f)\n",
590
544
  g_gc_collections, heap_before, g_heap_bytes, allocations_before, g_heap_allocations,
591
- g_gc_threshold);
545
+ g_gc_threshold, xt_monotonic_ms() - gc_started_ms, phase_table, phase_mark,
546
+ phase_sweep);
592
547
  fprintf(stderr, "xt_gc: live kinds");
593
548
  for (int i = 1; i < 16; i++) {
594
549
  if (live_by_kind[i] != 0) {
@@ -20,44 +20,69 @@
20
20
  #include <stdlib.h>
21
21
  #include <string.h>
22
22
 
23
- static xt_value xt_ab_data_key_value = XT_UNDEFINED;
24
-
25
- static xt_value xt_ab_data_key(void) {
26
- /* A symbol key keeps the backing bytes out of Object.keys / JSON.stringify,
27
- * like the platform's internal [[ArrayBufferData]] slot. */
28
- if (!xt_is_symbol(xt_ab_data_key_value)) {
29
- xt_ab_data_key_value = xt_symbol_new(xt_string_from_cstr("__arrayBufferData"));
30
- xt_gc_add_root(&xt_ab_data_key_value);
31
- }
32
- return xt_ab_data_key_value;
33
- }
34
-
35
- static xt_value xt_ab_data(xt_value buffer) { if (!XT_IS_OBJECT(buffer)) return XT_UNDEFINED;
36
- return xt_object_get(buffer, xt_ab_data_key());
37
- }
23
+ /*
24
+ * Storage: an ArrayBuffer is an ordinary object of kind
25
+ * `XT_OBJECT_KIND_ARRAY_BUFFER` whose bytes live in one `calloc`ed block
26
+ * (`buffer_bytes`). Typed arrays and DataViews index that block directly, so a
27
+ * geometry loop reads memory instead of paying a property lookup per byte.
28
+ *
29
+ * The block belongs to the buffer object and the collector frees it when the
30
+ * buffer is swept (see `xt_gc_release`). `byteLength` and `slice` are served by
31
+ * `xt_get` from this kind instead of being own properties, so an ArrayBuffer has
32
+ * no enumerable keys (`Object.keys(buffer)` is empty) like on the platform.
33
+ */
38
34
 
39
35
  static int xt_ab_is_buffer(xt_value value) {
40
- return XT_IS_ARRAY(xt_ab_data(value));
36
+ return XT_IS_OBJECT(value) &&
37
+ ((xt_object *)XT_GET_PTR(value))->header.kind == XT_OBJECT_KIND_ARRAY_BUFFER;
41
38
  }
42
39
 
43
40
  static xt_value xt_ab_new(uint32_t length) {
44
- xt_value buffer = xt_object_new();
45
- xt_object_set(buffer, xt_string_from_cstr("byteLength"), xt_number((double)length));
46
- xt_value data = xt_array_new(0, NULL);
47
- for (uint32_t i = 0; i < length; i++) xt_array_push(data, xt_number(0));
48
- xt_object_set(buffer, xt_ab_data_key(), data);
41
+ /* Instances sit on `ArrayBuffer.prototype`, so `buffer instanceof ArrayBuffer`
42
+ * and `buffer.constructor` behave like the platform (the properties are still
43
+ * served from the kind, not from the prototype). */
44
+ xt_value proto = xt_builtin_prototype_for_kind(XT_OBJECT_KIND_ARRAY_BUFFER);
45
+ xt_value buffer = XT_IS_OBJECT(proto) ? xt_object_new_with_proto(proto) : xt_object_new();
46
+ xt_object *object = (xt_object *)XT_GET_PTR(buffer);
47
+ object->header.kind = (uint8_t)XT_OBJECT_KIND_ARRAY_BUFFER;
48
+ /* A zero-length buffer keeps a NULL block: nothing ever indexes it. */
49
+ object->buffer_bytes = length > 0 ? (uint8_t *)calloc(1, length) : NULL;
50
+ object->buffer_length = object->buffer_bytes != NULL ? length : 0;
49
51
  return buffer;
50
52
  }
51
53
 
52
54
  static xt_value xt_ab_slice(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
53
- static void xt_ab_install(xt_value object, const char *name, void *fn);
55
+
56
+ /** The single `slice` method value `xt_get` hands out for an ArrayBuffer. */
57
+ xt_value xt_array_buffer_slice_value(void) {
58
+ static xt_value cached = XT_UNDEFINED;
59
+ if (XT_IS_FUNCTION(cached)) return cached;
60
+ cached = xt_closure_new((void *)xt_ab_slice, 0, NULL);
61
+ xt_gc_add_root(&cached);
62
+ return cached;
63
+ }
64
+
65
+ /** The properties an ArrayBuffer serves from its kind rather than from a
66
+ * property bag: `byteLength` and `slice`. `xt_get` and `xt_object_get` both
67
+ * consult this, so a C helper and JavaScript code agree. */
68
+ xt_value xt_array_buffer_served_property(xt_value buffer, xt_value key) {
69
+ if (!XT_IS_STRING(key)) return XT_UNDEFINED;
70
+ xt_object *object = (xt_object *)XT_GET_PTR(buffer);
71
+ xt_string *name = xt_as_string(key);
72
+ if (name == NULL) return XT_UNDEFINED;
73
+ if (name->length == 10 && memcmp(xt_string_bytes(name), "byteLength", 10) == 0) {
74
+ return xt_number((double)object->buffer_length);
75
+ }
76
+ if (name->length == 5 && memcmp(xt_string_bytes(name), "slice", 5) == 0) {
77
+ return xt_array_buffer_slice_value();
78
+ }
79
+ return XT_UNDEFINED;
80
+ }
54
81
 
55
82
  xt_value xt_array_buffer_ctor(int32_t argc, xt_value *argv) {
56
83
  double length = xt_to_number(xt_arg_at(argc, argv, 0));
57
84
  if (!(length >= 0) || length > 4294967295.0) length = 0;
58
- xt_value buffer = xt_ab_new((uint32_t)length);
59
- xt_ab_install(buffer, "slice", (void *)xt_ab_slice);
60
- return buffer;
85
+ return xt_ab_new((uint32_t)length);
61
86
  }
62
87
 
63
88
  xt_value xt_array_buffer_alloc(uint32_t byte_length) { return xt_ab_new(byte_length); }
@@ -65,55 +90,61 @@ xt_value xt_array_buffer_alloc(uint32_t byte_length) { return xt_ab_new(byte_len
65
90
  int xt_is_array_buffer(xt_value value) { return xt_ab_is_buffer(value); }
66
91
 
67
92
  uint32_t xt_array_buffer_byte_length(xt_value value) {
68
- if (!xt_ab_is_buffer(value)) return 0;
69
- return (uint32_t)xt_to_number(xt_get(value, xt_string_from_cstr("byteLength")));
93
+ if (!XT_IS_OBJECT(value)) return 0;
94
+ xt_object *object = (xt_object *)XT_GET_PTR(value);
95
+ if (object->header.kind != XT_OBJECT_KIND_ARRAY_BUFFER) return 0;
96
+ return object->buffer_length;
97
+ }
98
+
99
+ uint8_t *xt_array_buffer_bytes(xt_value buffer, uint32_t *byte_length) {
100
+ if (byte_length != NULL) *byte_length = 0;
101
+ if (!XT_IS_OBJECT(buffer)) return NULL;
102
+ xt_object *object = (xt_object *)XT_GET_PTR(buffer);
103
+ if (object->header.kind != XT_OBJECT_KIND_ARRAY_BUFFER) return NULL;
104
+ if (byte_length != NULL) *byte_length = object->buffer_length;
105
+ return object->buffer_bytes;
70
106
  }
71
107
 
72
108
  uint8_t xt_array_buffer_get_byte(xt_value buffer, uint32_t index) {
73
- xt_value data = xt_ab_data(buffer);
74
- if (!XT_IS_ARRAY(data)) return 0;
75
- return (uint8_t)xt_to_int32(xt_get(data, xt_number((double)index)));
109
+ uint32_t length = 0;
110
+ uint8_t *bytes = xt_array_buffer_bytes(buffer, &length);
111
+ if (bytes == NULL || index >= length) return 0;
112
+ return bytes[index];
76
113
  }
77
114
 
78
115
  void xt_array_buffer_set_byte(xt_value buffer, uint32_t index, uint8_t value) {
79
- xt_value data = xt_ab_data(buffer);
80
- if (!XT_IS_ARRAY(data)) return;
81
- xt_set(data, xt_number((double)index), xt_number((double)value));
116
+ uint32_t length = 0;
117
+ uint8_t *bytes = xt_array_buffer_bytes(buffer, &length);
118
+ if (bytes == NULL || index >= length) return;
119
+ bytes[index] = value;
82
120
  }
83
121
 
84
- /* Bulk byte access. A decoder (WAV, image, …) would otherwise pay one hidden
85
- * property read per byte, which is far too slow for a multi-megabyte file. The
86
- * backing store is an `xt_array` of numbers; reading its `items` directly is
87
- * the same representation `xt_ab_new` writes. */
122
+ /* Bulk byte access for decoders and wasm memories. */
88
123
  uint32_t xt_array_buffer_copy_bytes_out(xt_value buffer, uint32_t offset, uint8_t *dst,
89
124
  uint32_t count) {
90
- xt_array *data = xt_as_array(xt_ab_data(buffer));
91
- if (data == NULL || dst == NULL) return 0;
92
- if (offset >= data->length) return 0;
93
- if (count > data->length - offset) count = data->length - offset;
94
- for (uint32_t i = 0; i < count; i++) {
95
- dst[i] = (uint8_t)(int32_t)xt_to_number(data->items[offset + i]);
96
- }
125
+ uint32_t length = 0;
126
+ uint8_t *bytes = xt_array_buffer_bytes(buffer, &length);
127
+ if (bytes == NULL || dst == NULL) return 0;
128
+ if (offset >= length) return 0;
129
+ if (count > length - offset) count = length - offset;
130
+ memcpy(dst, bytes + offset, count);
97
131
  return count;
98
132
  }
99
133
 
100
134
  uint32_t xt_array_buffer_copy_bytes_in(xt_value buffer, uint32_t offset, const uint8_t *src,
101
135
  uint32_t count) {
102
- xt_array *data = xt_as_array(xt_ab_data(buffer));
103
- if (data == NULL || src == NULL) return 0;
104
- if (offset >= data->length) return 0;
105
- if (count > data->length - offset) count = data->length - offset;
106
- for (uint32_t i = 0; i < count; i++) {
107
- data->items[offset + i] = xt_number((double)src[i]);
108
- }
136
+ uint32_t length = 0;
137
+ uint8_t *bytes = xt_array_buffer_bytes(buffer, &length);
138
+ if (bytes == NULL || src == NULL) return 0;
139
+ if (offset >= length) return 0;
140
+ if (count > length - offset) count = length - offset;
141
+ memcpy(bytes + offset, src, count);
109
142
  return count;
110
143
  }
111
144
 
112
145
  static xt_value xt_ab_slice(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
113
146
  (void)env;
114
- xt_value data = xt_ab_data(thisValue);
115
- if (!XT_IS_ARRAY(data)) return xt_ab_new(0);
116
- int32_t length = xt_to_int32(xt_array_length(data));
147
+ int32_t length = (int32_t)xt_array_buffer_byte_length(thisValue);
117
148
  int32_t start = argc > 0 && argv[0] != XT_UNDEFINED ? xt_to_int32(argv[0]) : 0;
118
149
  int32_t end = argc > 1 && argv[1] != XT_UNDEFINED ? xt_to_int32(argv[1]) : length;
119
150
  if (start < 0) start += length;
@@ -121,18 +152,12 @@ static xt_value xt_ab_slice(xt_value thisValue, xt_value env, int32_t argc, xt_v
121
152
  if (start < 0) start = 0;
122
153
  if (end > length) end = length;
123
154
  if (end < start) end = start;
124
- xt_value result = xt_ab_new(0);
125
- xt_ab_install(result, "slice", (void *)xt_ab_slice);
126
- xt_value source = xt_ab_data(result);
127
- for (int32_t i = start; i < end; i++) xt_array_push(source, xt_get(data, xt_number((double)i)));
128
- xt_object_set(result, xt_string_from_cstr("byteLength"), xt_number((double)(end - start)));
155
+ xt_value result = xt_ab_new((uint32_t)(end - start));
156
+ xt_array_buffer_copy_bytes_out(thisValue, (uint32_t)start,
157
+ xt_array_buffer_bytes(result, NULL), (uint32_t)(end - start));
129
158
  return result;
130
159
  }
131
160
 
132
- static void xt_ab_install(xt_value object, const char *name, void *fn) {
133
- xt_object_set(object, xt_string_from_cstr(name), xt_closure_new(fn, 0, NULL));
134
- }
135
-
136
161
  xt_value xt_array_buffer_static(xt_value name, int32_t argc, xt_value *argv) {
137
162
  const char *method = xt_string_data(name);
138
163
  if (!method) return XT_UNDEFINED;
@@ -182,8 +207,8 @@ static int xt_dv_signed(int kind) {
182
207
  return kind == XT_DV_I8 || kind == XT_DV_I16 || kind == XT_DV_I32;
183
208
  }
184
209
 
185
- static xt_value xt_dv_data(xt_value view) {
186
- return xt_ab_data(xt_get(view, xt_string_from_cstr("buffer")));
210
+ static xt_value xt_dv_buffer(xt_value view) {
211
+ return xt_get(view, xt_string_from_cstr("buffer"));
187
212
  }
188
213
 
189
214
  static uint32_t xt_dv_offset(xt_value view) {
@@ -207,23 +232,25 @@ static int xt_dv_check(xt_value view, xt_value indexValue, uint32_t width, uint3
207
232
  }
208
233
 
209
234
  static uint64_t xt_dv_read_bits(xt_value view, uint32_t offset, uint32_t width, int little) {
210
- xt_value data = xt_dv_data(view);
235
+ uint32_t length = 0;
236
+ uint8_t *bytes = xt_array_buffer_bytes(xt_dv_buffer(view), &length);
211
237
  uint64_t value = 0;
238
+ if (bytes == NULL || offset + width > length) return 0;
212
239
  for (uint32_t i = 0; i < width; i++) {
213
240
  uint32_t index = little ? offset + width - 1 - i : offset + i;
214
- uint64_t byte = (uint64_t)xt_to_number(xt_get(data, xt_number((double)index)));
215
- value = (value << 8) | (byte & 0xff);
241
+ value = (value << 8) | (uint64_t)bytes[index];
216
242
  }
217
243
  return value;
218
244
  }
219
245
 
220
246
  static void xt_dv_write_bits(xt_value view, uint32_t offset, uint32_t width, uint64_t value,
221
247
  int little) {
222
- xt_value data = xt_dv_data(view);
248
+ uint32_t length = 0;
249
+ uint8_t *bytes = xt_array_buffer_bytes(xt_dv_buffer(view), &length);
250
+ if (bytes == NULL || offset + width > length) return;
223
251
  for (uint32_t i = 0; i < width; i++) {
224
252
  uint32_t shift = little ? i * 8 : (width - 1 - i) * 8;
225
- uint32_t index = offset + i;
226
- xt_set(data, xt_number((double)index), xt_number((double)((value >> shift) & 0xff)));
253
+ bytes[offset + i] = (uint8_t)((value >> shift) & 0xff);
227
254
  }
228
255
  }
229
256
 
@@ -312,3 +339,4 @@ xt_value xt_data_view_ctor(int32_t argc, xt_value *argv) {
312
339
  }
313
340
  return view;
314
341
  }
342
+
@@ -117,7 +117,7 @@ xt_value xt_object_rest(xt_value source, int32_t argc, xt_value *argv) {
117
117
  if (!XT_IS_OBJECT(source) && !XT_IS_FUNCTION(source)) return result;
118
118
  xt_object *object = xt_as_object(source);
119
119
  if (!object) return result;
120
- if (object->header.kind != XT_OBJECT_KIND_OBJECT && object->header.kind != XT_OBJECT_KIND_ERROR) {
120
+ if (!xt_kind_is_object(object->header.kind)) {
121
121
  /* Map/Set/Date/RegExp/Promise have no own enumerable string properties. */
122
122
  return result;
123
123
  }
@@ -421,6 +421,7 @@ xt_value xt_closure_new(void *fn, int32_t env_count, xt_value *env) {
421
421
  }
422
422
 
423
423
  xt_value xt_closure_call(xt_value value, int32_t argc, xt_value *argv) {
424
+ XT_PROFILE_TICK(XT_PROFILE_CLOSURE_CALL);
424
425
  return xt_call_with_this(value, XT_UNDEFINED, argc, argv);
425
426
  }
426
427
 
@@ -595,3 +596,4 @@ xt_value xt_function_set_metadata(xt_value value, xt_value name, int32_t arity)
595
596
  return value;
596
597
  }
597
598
 
599
+
@@ -487,6 +487,7 @@ xt_value xt_builtin_prototype_for_kind(int32_t kind) {
487
487
  case XT_OBJECT_KIND_REGEXP: name = "RegExp"; break;
488
488
  case XT_OBJECT_KIND_PROMISE: name = "Promise"; break;
489
489
  case XT_OBJECT_KIND_SYMBOL: name = "Symbol"; break;
490
+ case XT_OBJECT_KIND_ARRAY_BUFFER: name = "ArrayBuffer"; break;
490
491
  default: return XT_UNDEFINED;
491
492
  }
492
493
  xt_value constructor = xt_builtin_constructor(xt_string_from_cstr(name));