xbintsc 0.6.28 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (280) hide show
  1. package/dist/src/binder/declarations.d.ts +1 -0
  2. package/dist/src/binder/declarations.js +37 -0
  3. package/dist/src/binder/declarations.js.map +1 -1
  4. package/dist/src/binder/references.js +12 -0
  5. package/dist/src/binder/references.js.map +1 -1
  6. package/dist/src/checker/globals.js +1 -0
  7. package/dist/src/checker/globals.js.map +1 -1
  8. package/dist/src/checker/type-system.d.ts +5 -0
  9. package/dist/src/checker/type-system.js +22 -3
  10. package/dist/src/checker/type-system.js.map +1 -1
  11. package/dist/src/cli/help.d.ts +1 -1
  12. package/dist/src/codegen/generator/calls/access.js +25 -0
  13. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  14. package/dist/src/codegen/generator/calls/invocation.js +13 -0
  15. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  16. package/dist/src/codegen/generator/calls/super.js +3 -4
  17. package/dist/src/codegen/generator/calls/super.js.map +1 -1
  18. package/dist/src/codegen/generator/calls/unwrap.d.ts +18 -0
  19. package/dist/src/codegen/generator/calls/unwrap.js +45 -0
  20. package/dist/src/codegen/generator/calls/unwrap.js.map +1 -0
  21. package/dist/src/codegen/generator/expressions/assignment.js +14 -0
  22. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  23. package/dist/src/codegen/generator/expressions/module-values.d.ts +6 -0
  24. package/dist/src/codegen/generator/expressions/module-values.js +51 -17
  25. package/dist/src/codegen/generator/expressions/module-values.js.map +1 -1
  26. package/dist/src/codegen/generator/expressions/primary.js +9 -5
  27. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  28. package/dist/src/codegen/generator/generator.js +4 -0
  29. package/dist/src/codegen/generator/generator.js.map +1 -1
  30. package/dist/src/codegen/generator/lookup-tables.d.ts +22 -0
  31. package/dist/src/codegen/generator/lookup-tables.js +48 -0
  32. package/dist/src/codegen/generator/lookup-tables.js.map +1 -0
  33. package/dist/src/codegen/generator/module.js +38 -7
  34. package/dist/src/codegen/generator/module.js.map +1 -1
  35. package/dist/src/codegen/generator/state.js +17 -0
  36. package/dist/src/codegen/generator/state.js.map +1 -1
  37. package/dist/src/codegen/generator/tables.d.ts +2 -6
  38. package/dist/src/codegen/generator/tables.js +18 -12
  39. package/dist/src/codegen/generator/tables.js.map +1 -1
  40. package/dist/src/driver/bundler/commonjs.d.ts +9 -2
  41. package/dist/src/driver/bundler/commonjs.js +11 -4
  42. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  43. package/dist/src/driver/bundler/concatenate.d.ts +20 -0
  44. package/dist/src/driver/bundler/concatenate.js +68 -4
  45. package/dist/src/driver/bundler/concatenate.js.map +1 -1
  46. package/dist/src/driver/bundler/deferred.d.ts +29 -1
  47. package/dist/src/driver/bundler/deferred.js +114 -5
  48. package/dist/src/driver/bundler/deferred.js.map +1 -1
  49. package/dist/src/driver/bundler/dynamic-import.d.ts +6 -0
  50. package/dist/src/driver/bundler/dynamic-import.js +59 -3
  51. package/dist/src/driver/bundler/dynamic-import.js.map +1 -1
  52. package/dist/src/driver/bundler/graph.d.ts +13 -0
  53. package/dist/src/driver/bundler/graph.js +2 -2
  54. package/dist/src/driver/bundler/graph.js.map +1 -1
  55. package/dist/src/driver/bundler/merge.js +32 -8
  56. package/dist/src/driver/bundler/merge.js.map +1 -1
  57. package/dist/src/driver/bundler/namespace-object.d.ts +11 -0
  58. package/dist/src/driver/bundler/namespace-object.js +32 -0
  59. package/dist/src/driver/bundler/namespace-object.js.map +1 -1
  60. package/dist/src/driver/bundler/reexport-resolution.js +8 -2
  61. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
  62. package/dist/src/driver/bundler/symbols.d.ts +4 -0
  63. package/dist/src/driver/bundler/symbols.js +14 -1
  64. package/dist/src/driver/bundler/symbols.js.map +1 -1
  65. package/dist/src/driver/config.d.ts +1 -1
  66. package/dist/src/driver/options.d.ts +1 -4
  67. package/dist/src/driver/runtime-lib.d.ts +27 -0
  68. package/dist/src/driver/runtime-lib.js +78 -2
  69. package/dist/src/driver/runtime-lib.js.map +1 -1
  70. package/dist/src/driver/runtime-objects.d.ts +1 -1
  71. package/dist/src/driver/runtime-objects.js +24 -5
  72. package/dist/src/driver/runtime-objects.js.map +1 -1
  73. package/dist/src/driver/version.d.ts +1 -1
  74. package/dist/src/driver/version.js +1 -1
  75. package/dist/src/driver/version.js.map +1 -1
  76. package/dist/src/extensions/node/buffer/index.d.ts +5 -4
  77. package/dist/src/extensions/node/buffer/index.js +5 -4
  78. package/dist/src/extensions/node/buffer/index.js.map +1 -1
  79. package/dist/src/extensions/node/events/index.js +8 -1
  80. package/dist/src/extensions/node/events/index.js.map +1 -1
  81. package/dist/src/extensions/node/fs/callbacks.d.ts +9 -0
  82. package/dist/src/extensions/node/fs/callbacks.js +42 -0
  83. package/dist/src/extensions/node/fs/callbacks.js.map +1 -0
  84. package/dist/src/extensions/node/fs/index.d.ts +2 -3
  85. package/dist/src/extensions/node/fs/index.js +11 -4
  86. package/dist/src/extensions/node/fs/index.js.map +1 -1
  87. package/dist/src/extensions/node/process/index.d.ts +10 -3
  88. package/dist/src/extensions/node/process/index.js +30 -5
  89. package/dist/src/extensions/node/process/index.js.map +1 -1
  90. package/dist/src/extensions/registry.d.ts +7 -0
  91. package/dist/src/extensions/registry.js +13 -4
  92. package/dist/src/extensions/registry.js.map +1 -1
  93. package/dist/src/lexer/scanner.d.ts +7 -1
  94. package/dist/src/lexer/scanner.js +15 -0
  95. package/dist/src/lexer/scanner.js.map +1 -1
  96. package/dist/src/parser/context.d.ts +10 -0
  97. package/dist/src/parser/context.js +42 -3
  98. package/dist/src/parser/context.js.map +1 -1
  99. package/dist/src/parser/literals.js +6 -2
  100. package/dist/src/parser/literals.js.map +1 -1
  101. package/dist/tests/binder/binder.test.js +43 -0
  102. package/dist/tests/binder/binder.test.js.map +1 -1
  103. package/dist/tests/codegen/llvm.test.js +15 -0
  104. package/dist/tests/codegen/llvm.test.js.map +1 -1
  105. package/dist/tests/driver/bundler-resolve.test.js +12 -0
  106. package/dist/tests/driver/bundler-resolve.test.js.map +1 -1
  107. package/dist/tests/driver/commonjs-prelude.test.d.ts +11 -0
  108. package/dist/tests/driver/commonjs-prelude.test.js +69 -0
  109. package/dist/tests/driver/commonjs-prelude.test.js.map +1 -0
  110. package/dist/tests/driver/compiler.test.js +42 -1
  111. package/dist/tests/driver/compiler.test.js.map +1 -1
  112. package/dist/tests/driver/html-deferred-cycles.test.d.ts +10 -0
  113. package/dist/tests/driver/html-deferred-cycles.test.js +75 -0
  114. package/dist/tests/driver/html-deferred-cycles.test.js.map +1 -0
  115. package/dist/tests/driver/namespace-type-exports.test.d.ts +9 -0
  116. package/dist/tests/driver/namespace-type-exports.test.js +59 -0
  117. package/dist/tests/driver/namespace-type-exports.test.js.map +1 -0
  118. package/dist/tests/driver/runtime-lib.test.js +49 -1
  119. package/dist/tests/driver/runtime-lib.test.js.map +1 -1
  120. package/dist/tests/e2e/compile-basics.test.js +53 -0
  121. package/dist/tests/e2e/compile-basics.test.js.map +1 -1
  122. package/dist/tests/e2e/compile-deferred-marker.test.d.ts +14 -0
  123. package/dist/tests/e2e/compile-deferred-marker.test.js +68 -0
  124. package/dist/tests/e2e/compile-deferred-marker.test.js.map +1 -0
  125. package/dist/tests/e2e/compile-deferred-reexport.test.d.ts +12 -0
  126. package/dist/tests/e2e/compile-deferred-reexport.test.js +89 -0
  127. package/dist/tests/e2e/compile-deferred-reexport.test.js.map +1 -0
  128. package/dist/tests/e2e/compile-error-module.test.d.ts +9 -0
  129. package/dist/tests/e2e/compile-error-module.test.js +29 -0
  130. package/dist/tests/e2e/compile-error-module.test.js.map +1 -0
  131. package/dist/tests/e2e/compile-gc-promises.test.d.ts +11 -0
  132. package/dist/tests/e2e/compile-gc-promises.test.js +52 -0
  133. package/dist/tests/e2e/compile-gc-promises.test.js.map +1 -0
  134. package/dist/tests/e2e/compile-modules.test.js +40 -0
  135. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  136. package/dist/tests/e2e/compile-node-fs-callbacks.test.d.ts +6 -0
  137. package/dist/tests/e2e/compile-node-fs-callbacks.test.js +46 -0
  138. package/dist/tests/e2e/compile-node-fs-callbacks.test.js.map +1 -0
  139. package/dist/tests/e2e/compile-node-inheritance.test.d.ts +6 -0
  140. package/dist/tests/e2e/compile-node-inheritance.test.js +48 -0
  141. package/dist/tests/e2e/compile-node-inheritance.test.js.map +1 -0
  142. package/dist/tests/e2e/compile-node-require.test.d.ts +9 -0
  143. package/dist/tests/e2e/compile-node-require.test.js +29 -0
  144. package/dist/tests/e2e/compile-node-require.test.js.map +1 -0
  145. package/dist/tests/e2e/compile-node-stats.test.d.ts +8 -0
  146. package/dist/tests/e2e/compile-node-stats.test.js +26 -0
  147. package/dist/tests/e2e/compile-node-stats.test.js.map +1 -0
  148. package/dist/tests/e2e/compile-proxy.test.d.ts +6 -0
  149. package/dist/tests/e2e/compile-proxy.test.js +66 -0
  150. package/dist/tests/e2e/compile-proxy.test.js.map +1 -0
  151. package/dist/tests/e2e/compile-web-globals.test.d.ts +6 -0
  152. package/dist/tests/e2e/compile-web-globals.test.js +94 -0
  153. package/dist/tests/e2e/compile-web-globals.test.js.map +1 -0
  154. package/dist/tests/e2e/fetch.test.js +1 -1
  155. package/dist/tests/e2e/gui-script-import-order.test.js +24 -0
  156. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  157. package/dist/tests/e2e/json-stringify-to-json.test.d.ts +10 -0
  158. package/dist/tests/e2e/json-stringify-to-json.test.js +61 -0
  159. package/dist/tests/e2e/json-stringify-to-json.test.js.map +1 -0
  160. package/dist/tests/e2e/node-process.test.d.ts +7 -0
  161. package/dist/tests/e2e/node-process.test.js +50 -0
  162. package/dist/tests/e2e/node-process.test.js.map +1 -0
  163. package/dist/tests/e2e/runtime-coverage-buffer.test.js +31 -0
  164. package/dist/tests/e2e/runtime-coverage-buffer.test.js.map +1 -1
  165. package/dist/tests/e2e/web-streams.test.js +44 -29
  166. package/dist/tests/e2e/web-streams.test.js.map +1 -1
  167. package/dist/tests/parser/literals.test.js +19 -0
  168. package/dist/tests/parser/literals.test.js.map +1 -1
  169. package/dist/tests/parser/statements.test.js +13 -0
  170. package/dist/tests/parser/statements.test.js.map +1 -1
  171. package/doc/ai/cli.md +1 -0
  172. package/doc/ai/zh-CN/cli.md +1 -0
  173. package/doc/implemented/binder.md +2 -1
  174. package/doc/implemented/checker.md +5 -0
  175. package/doc/implemented/lexer.md +1 -1
  176. package/doc/implemented/modules.md +23 -1
  177. package/doc/implemented/node/buffer.md +18 -5
  178. package/doc/implemented/node/fs.md +22 -1
  179. package/doc/implemented/node/process.md +65 -5
  180. package/doc/implemented/node.md +7 -1
  181. package/doc/implemented/operators.md +3 -1
  182. package/doc/implemented/parser.md +1 -1
  183. package/doc/implemented/runtime-core.md +6 -2
  184. package/doc/implemented/stdlib.md +69 -2
  185. package/doc/unimplemented/classes.md +1 -0
  186. package/doc/unimplemented/modules.md +1 -0
  187. package/doc/unimplemented/stdlib.md +2 -1
  188. package/doc/xbintsc.config.schema.json +5 -0
  189. package/doc/zh-CN/implemented/binder.md +2 -1
  190. package/doc/zh-CN/implemented/checker.md +3 -0
  191. package/doc/zh-CN/implemented/lexer.md +1 -1
  192. package/doc/zh-CN/implemented/modules.md +17 -1
  193. package/doc/zh-CN/implemented/node/buffer.md +3 -1
  194. package/doc/zh-CN/implemented/node/fs.md +19 -1
  195. package/doc/zh-CN/implemented/node/process.md +59 -5
  196. package/doc/zh-CN/implemented/node.md +1 -1
  197. package/doc/zh-CN/implemented/operators.md +2 -1
  198. package/doc/zh-CN/implemented/parser.md +1 -1
  199. package/doc/zh-CN/implemented/runtime-core.md +3 -2
  200. package/doc/zh-CN/implemented/stdlib.md +58 -2
  201. package/doc/zh-CN/unimplemented/classes.md +1 -0
  202. package/doc/zh-CN/unimplemented/modules.md +1 -0
  203. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  204. package/package.json +7 -7
  205. package/runtime/ext_node/buffer/buffer.c +5 -4
  206. package/runtime/ext_node/buffer/parts/byte-access.inc +115 -11
  207. package/runtime/ext_node/buffer/parts/prototype.inc +4 -1
  208. package/runtime/ext_node/events/events.c +9 -0
  209. package/runtime/ext_node/fs/callbacks.c +138 -0
  210. package/runtime/ext_node/fs/fs_ops.c +28 -15
  211. package/runtime/ext_node/module/module.c +18 -9
  212. package/runtime/ext_node/process/process.c +117 -32
  213. package/runtime/ext_node/process/streams.c +370 -0
  214. package/runtime/ext_node/process/streams.h +20 -0
  215. package/runtime/rt.h +36 -0
  216. package/runtime/rt_internal.h +10 -10
  217. package/runtime/xt_alloc.c +67 -49
  218. package/runtime/xt_async_io.c +3 -0
  219. package/runtime/xt_builtins.c +26 -23
  220. package/runtime/xt_constructors.c +101 -2
  221. package/runtime/xt_containers/access.inc +29 -1
  222. package/runtime/xt_containers/function_prototypes.inc +58 -0
  223. package/runtime/xt_containers/objects.inc +18 -2
  224. package/runtime/xt_containers.c +1 -0
  225. package/runtime/xt_exit.c +34 -0
  226. package/runtime/xt_fetch/headers.inc +1 -0
  227. package/runtime/xt_fetch/response.inc +28 -8
  228. package/runtime/xt_fetch/webapi.inc +82 -0
  229. package/runtime/xt_fetch.c +5 -0
  230. package/runtime/xt_gc_verify.c +135 -0
  231. package/runtime/xt_intl.c +224 -0
  232. package/runtime/xt_io/exception.inc +40 -1
  233. package/runtime/xt_module_values.c +82 -0
  234. package/runtime/xt_proxy.c +140 -0
  235. package/runtime/xt_stdlib/dispatch.inc +98 -9
  236. package/runtime/xt_stdlib2/date.inc +7 -1
  237. package/runtime/xt_stdlib2/json.inc +28 -0
  238. package/runtime/xt_stdlib2/json_stringify.inc +70 -53
  239. package/runtime/xt_web.h +27 -0
  240. package/runtime/xt_web_abort.c +373 -0
  241. package/scripts/build-runtime.ts +8 -0
  242. package/src/binder/declarations.ts +34 -0
  243. package/src/binder/references.ts +12 -0
  244. package/src/checker/globals.ts +1 -0
  245. package/src/checker/type-system.ts +23 -3
  246. package/src/codegen/generator/calls/access.ts +23 -0
  247. package/src/codegen/generator/calls/invocation.ts +12 -0
  248. package/src/codegen/generator/calls/super.ts +5 -4
  249. package/src/codegen/generator/calls/unwrap.ts +47 -0
  250. package/src/codegen/generator/expressions/assignment.ts +15 -0
  251. package/src/codegen/generator/expressions/module-values.ts +57 -19
  252. package/src/codegen/generator/expressions/primary.ts +9 -9
  253. package/src/codegen/generator/generator.ts +4 -0
  254. package/src/codegen/generator/lookup-tables.ts +63 -0
  255. package/src/codegen/generator/module.ts +38 -7
  256. package/src/codegen/generator/state.ts +17 -0
  257. package/src/codegen/generator/tables.ts +18 -12
  258. package/src/driver/bundler/commonjs.ts +12 -5
  259. package/src/driver/bundler/concatenate.ts +84 -7
  260. package/src/driver/bundler/deferred.ts +122 -6
  261. package/src/driver/bundler/dynamic-import.ts +75 -2
  262. package/src/driver/bundler/graph.ts +2 -2
  263. package/src/driver/bundler/merge.ts +31 -5
  264. package/src/driver/bundler/namespace-object.ts +44 -0
  265. package/src/driver/bundler/reexport-resolution.ts +12 -2
  266. package/src/driver/bundler/symbols.ts +17 -0
  267. package/src/driver/config.ts +1 -1
  268. package/src/driver/options.ts +1 -4
  269. package/src/driver/runtime-lib.ts +87 -2
  270. package/src/driver/runtime-objects.ts +24 -5
  271. package/src/driver/version.ts +1 -1
  272. package/src/extensions/node/buffer/index.ts +5 -4
  273. package/src/extensions/node/events/index.ts +8 -1
  274. package/src/extensions/node/fs/callbacks.ts +45 -0
  275. package/src/extensions/node/fs/index.ts +11 -4
  276. package/src/extensions/node/process/index.ts +30 -5
  277. package/src/extensions/registry.ts +21 -4
  278. package/src/lexer/scanner.ts +16 -1
  279. package/src/parser/context.ts +39 -3
  280. package/src/parser/literals.ts +6 -2
@@ -1,33 +1,13 @@
1
1
  /*
2
2
  * xbintsc runtime — heap allocation and garbage collection.
3
3
  *
4
- * Every heap object is a `calloc`-backed block whose first bytes are an
5
- * `xt_header`. Live blocks are threaded through `g_heap_head` (`gc_next`), and
6
- * the collector is a non-moving mark-sweep over that list:
7
- *
8
- * 1. build an address -> header hash set of every live allocation;
9
- * 2. mark from the roots — registered slots, root providers and a
10
- * conservative scan of the active C stack (the whole `jmp_buf` plus the
11
- * stack words up to the region's high bound);
12
- * 3. trace each marked object. Value fields are visited precisely where the
13
- * layout is known, but the object payload and its separately allocated
14
- * backing buffers are also scanned conservatively, so a raw pointer held
15
- * by older code is still followed;
16
- * 4. sweep: unmarked blocks are released (including their backing buffers)
17
- * and unlinked, survivors keep their block and have the mark bit cleared.
18
- *
19
- * A non-moving collector keeps the NaN-boxed `xt_value` representation
20
- * untouched: a pointer never changes, so no write barriers or handle
21
- * indirection are needed. `gc_next` doubles as the live list and, once a block
22
- * is released, the memory simply returns to `malloc`.
23
- *
24
- * The collector is single-threaded, like the rest of the runtime (workers are
25
- * child processes, not threads).
4
+ * Non-moving mark-sweep over a `calloc`-ed block list; see `xt_gc_collect`.
26
5
  */
27
6
 
28
7
  #include "rt_internal.h"
29
8
 
30
9
  #include <setjmp.h>
10
+ #include <stdatomic.h>
31
11
  #include <stdio.h>
32
12
  #include <stdlib.h>
33
13
  #include <string.h>
@@ -146,8 +126,37 @@ static void xt_gc_table_free(void) {
146
126
  /* Allocation */
147
127
  /* ------------------------------------------------------------------------- */
148
128
 
129
+ _Thread_local int xt_gc_worker_thread = 0;
130
+
131
+ /* Workers allocate result values, so linking takes a spinlock; a block born
132
+ * while a collection runs is marked, or the sweep would free it. */
133
+ static atomic_flag g_heap_lock = ATOMIC_FLAG_INIT;
134
+
135
+ static void xt_heap_link(xt_header *header) {
136
+ while (atomic_flag_test_and_set(&g_heap_lock)) {
137
+ }
138
+ header->gc_next = g_heap_head;
139
+ g_heap_head = header;
140
+ if (g_gc_running && header->kind < 32) header->flags |= XT_GC_MARK;
141
+ g_heap_bytes += header->size;
142
+ g_heap_allocations++;
143
+ atomic_flag_clear(&g_heap_lock);
144
+ }
145
+
149
146
  void *xt_alloc(size_t size, int kind) {
150
147
  if (!g_gc_initialised) xt_gc_init();
148
+ if (xt_gc_worker_thread) {
149
+ void *block = calloc(1, size);
150
+ if (!block) {
151
+ fprintf(stderr, "xbintsc: out of memory allocating %zu bytes\n", size);
152
+ abort();
153
+ }
154
+ xt_header *header = (xt_header *)block;
155
+ header->kind = (uint8_t)kind;
156
+ header->size = (uint32_t)size;
157
+ xt_heap_link(header);
158
+ return header;
159
+ }
151
160
  /* Collect before handing out the new block: the block being allocated here
152
161
  * is guaranteed not to be swept, and references held by the caller survive
153
162
  * the conservative stack scan. */
@@ -161,10 +170,8 @@ void *xt_alloc(size_t size, int kind) {
161
170
  header->kind = (uint8_t)kind;
162
171
  header->flags = 0;
163
172
  header->size = (uint32_t)size;
164
- header->gc_next = g_heap_head;
165
- g_heap_head = header;
166
- g_heap_bytes += size;
167
- g_heap_allocations++;
173
+ header->size = (uint32_t)size;
174
+ xt_heap_link(header);
168
175
  return header;
169
176
  }
170
177
 
@@ -214,14 +221,23 @@ void xt_gc_init(void) {
214
221
  * most useful thing to print; install the handler once per process. */
215
222
  xt_install_crash_handler();
216
223
  }
217
-
218
224
  void xt_gc_arm(void) { g_gc_armed = 1; }
219
225
 
226
+ /* Module initialisation runs with a much higher threshold: a bundle allocates
227
+ * its module graph inside one generated function, and collecting in the middle
228
+ * of it has produced access violations. Startup is bounded. */
229
+ #define XT_GC_STARTUP_THRESHOLD ((size_t)512 << 20) /* 512 MiB */
230
+
231
+ void xt_gc_begin_startup(void) {
232
+ if (!g_gc_threshold_pinned) g_gc_threshold = XT_GC_STARTUP_THRESHOLD;
233
+ }
234
+ void xt_gc_end_startup(void) {
235
+ if (!g_gc_threshold_pinned) g_gc_threshold = (size_t)16 << 20;
236
+ }
220
237
  void xt_gc_set_threshold(size_t bytes) {
221
238
  g_gc_threshold = bytes > XT_GC_MIN_THRESHOLD ? bytes : XT_GC_MIN_THRESHOLD;
222
239
  g_gc_threshold_pinned = 1;
223
240
  }
224
-
225
241
  void xt_gc_add_root(xt_value *slot) {
226
242
  if (!slot) return;
227
243
  for (size_t i = 0; i < g_root_count; i++) {
@@ -236,11 +252,9 @@ void xt_gc_add_root(xt_value *slot) {
236
252
  }
237
253
  g_root_slots[g_root_count++] = slot;
238
254
  }
239
-
240
255
  void xt_gc_add_root_range(xt_value *base, size_t count) {
241
256
  for (size_t i = 0; i < count; i++) xt_gc_add_root(base + i);
242
257
  }
243
-
244
258
  void xt_gc_register_root_provider(void (*provider)(void)) {
245
259
  if (!provider) return;
246
260
  for (size_t i = 0; i < g_root_provider_count; i++) {
@@ -255,7 +269,6 @@ void xt_gc_register_root_provider(void (*provider)(void)) {
255
269
  }
256
270
  g_root_providers[g_root_provider_count++] = provider;
257
271
  }
258
-
259
272
  void xt_gc_push_stack(uintptr_t low, uintptr_t high) {
260
273
  if (g_stack_depth >= XT_GC_MAX_STACKS) return;
261
274
  g_stack_regions[g_stack_depth].low = low;
@@ -263,24 +276,20 @@ void xt_gc_push_stack(uintptr_t low, uintptr_t high) {
263
276
  g_stack_regions[g_stack_depth].scan_low = low;
264
277
  g_stack_depth++;
265
278
  }
266
-
267
279
  void xt_gc_suspend(uintptr_t sp) {
268
280
  if (g_stack_depth <= 0) return;
269
281
  uintptr_t *scan = &g_stack_regions[g_stack_depth - 1].scan_low;
270
282
  if (*scan == UINTPTR_MAX || sp < *scan) *scan = sp;
271
283
  }
272
-
273
284
  void xt_gc_pop_stack(void) {
274
285
  if (g_stack_depth <= 1) return;
275
286
  g_stack_depth--;
276
287
  /* Back on the underlying stack: resume scanning it from the current frame. */
277
288
  if (g_stack_depth > 0) g_stack_regions[g_stack_depth - 1].scan_low = UINTPTR_MAX;
278
289
  }
279
-
280
290
  /* ------------------------------------------------------------------------- */
281
291
  /* Marking */
282
292
  /* ------------------------------------------------------------------------- */
283
-
284
293
  static void xt_gc_gray_push(xt_header *header) {
285
294
  if (!header || (header->flags & XT_GC_MARK)) return;
286
295
  header->flags |= XT_GC_MARK;
@@ -293,8 +302,21 @@ static void xt_gc_gray_push(xt_header *header) {
293
302
  }
294
303
  g_gray[g_gray_count++] = header;
295
304
  }
296
-
297
- void xt_gc_mark_header(xt_header *header) { xt_gc_gray_push(header); }
305
+ xt_header *xt_gc_lookup_raw(uintptr_t address);
306
+
307
+ /* Typed pointers (a promise's `reaction->promise`, a coroutine's active async
308
+ * frame, ...) are only *probably* live: a stale one must be skipped, not
309
+ * dereferenced. Dereferencing it here is how a crash turned into "read at
310
+ * 0xffffffffffffffff" instead of a diagnosis. */
311
+ static void xt_gc_gray_push_checked(xt_header *header) {
312
+ if (!header) return;
313
+ if (xt_gc_lookup_raw((uintptr_t)header) == header) {
314
+ xt_gc_gray_push(header);
315
+ return;
316
+ }
317
+ xt_gc_verify_rejected((uintptr_t)header);
318
+ }
319
+ void xt_gc_mark_header(xt_header *header) { xt_gc_gray_push_checked(header); }
298
320
 
299
321
  void xt_gc_mark_value(xt_value value) {
300
322
  if (XT_IS_STRING(value) || XT_IS_OBJECT(value) || XT_IS_ARRAY(value) || XT_IS_FUNCTION(value) ||
@@ -302,7 +324,6 @@ void xt_gc_mark_value(xt_value value) {
302
324
  xt_gc_gray_push((xt_header *)XT_GET_PTR(value));
303
325
  }
304
326
  }
305
-
306
327
  int xt_gc_value_is_marked(xt_value value) {
307
328
  if (!(XT_IS_STRING(value) || XT_IS_OBJECT(value) || XT_IS_ARRAY(value) ||
308
329
  XT_IS_FUNCTION(value) || XT_IS_BIGINT(value))) {
@@ -311,9 +332,10 @@ int xt_gc_value_is_marked(xt_value value) {
311
332
  xt_header *header = xt_gc_table_lookup((uintptr_t)XT_GET_PTR(value));
312
333
  return header != NULL && (header->flags & XT_GC_MARK) != 0;
313
334
  }
314
-
315
335
  xt_header *xt_gc_heap_head(void) { return g_heap_head; }
316
336
 
337
+ xt_header *xt_gc_lookup_raw(uintptr_t address) { return xt_gc_table_lookup(address); }
338
+
317
339
  void xt_gc_scan_region(const void *base, size_t bytes) {
318
340
  if (!base) return;
319
341
  const uint64_t *words = (const uint64_t *)base;
@@ -330,11 +352,9 @@ void xt_gc_scan_region(const void *base, size_t bytes) {
330
352
  if (header) xt_gc_gray_push(header);
331
353
  }
332
354
  }
333
-
334
355
  /* ------------------------------------------------------------------------- */
335
356
  /* Tracing */
336
357
  /* ------------------------------------------------------------------------- */
337
-
338
358
  /* Visit the payload (everything after the header) plus any separately
339
359
  * allocated backing buffer owned by the object. */
340
360
  static void xt_gc_process(xt_header *header) {
@@ -347,8 +367,8 @@ static void xt_gc_process(xt_header *header) {
347
367
  * children must stay alive until `xt_string_bytes` materialises it. */
348
368
  xt_string *string = (xt_string *)header;
349
369
  if (string->flat == NULL) {
350
- if (string->left) xt_gc_gray_push(&string->left->header);
351
- if (string->right) xt_gc_gray_push(&string->right->header);
370
+ if (string->left) xt_gc_gray_push_checked(&string->left->header);
371
+ if (string->right) xt_gc_gray_push_checked(&string->right->header);
352
372
  }
353
373
  return;
354
374
  }
@@ -419,11 +439,9 @@ static void xt_gc_process(xt_header *header) {
419
439
  return;
420
440
  }
421
441
  }
422
-
423
442
  /* ------------------------------------------------------------------------- */
424
443
  /* Release */
425
444
  /* ------------------------------------------------------------------------- */
426
-
427
445
  static void xt_gc_release(xt_header *header) {
428
446
  switch (header->kind) {
429
447
  case XT_OBJECT_KIND_STRING:
@@ -470,11 +488,9 @@ static void xt_gc_release(xt_header *header) {
470
488
  }
471
489
  free(header);
472
490
  }
473
-
474
491
  /* ------------------------------------------------------------------------- */
475
492
  /* Collection */
476
493
  /* ------------------------------------------------------------------------- */
477
-
478
494
  static void xt_gc_mark_roots(void) {
479
495
  for (size_t i = 0; i < g_root_count; i++) {
480
496
  if (g_root_slots[i]) xt_gc_mark_value(*g_root_slots[i]);
@@ -494,12 +510,12 @@ static void xt_gc_mark_roots(void) {
494
510
  if (high > low) xt_gc_scan_region((const void *)low, high - low);
495
511
  }
496
512
  }
497
-
498
513
  void xt_gc_collect(void) {
499
514
  if (g_gc_running) return;
500
515
  if (!g_gc_initialised) xt_gc_init();
501
516
  g_gc_running = 1;
502
517
  const int trace = xt_gc_trace_enabled();
518
+ const int leak = getenv("XT_GC_LEAK") != NULL;
503
519
  const size_t heap_before = g_heap_bytes;
504
520
  const size_t allocations_before = g_heap_allocations;
505
521
 
@@ -518,6 +534,7 @@ void xt_gc_collect(void) {
518
534
  }
519
535
  xt_weak_gc_clear();
520
536
  xt_weak_gc_compact();
537
+ if (getenv("XT_GC_VERIFY") != NULL) xt_gc_verify_payloads();
521
538
 
522
539
  xt_header **link = &g_heap_head;
523
540
  xt_header *header = g_heap_head;
@@ -541,7 +558,8 @@ void xt_gc_collect(void) {
541
558
  *link = next;
542
559
  g_heap_bytes -= header->size;
543
560
  g_heap_allocations--;
544
- xt_gc_release(header);
561
+ xt_gc_debug_record_free((uintptr_t)header, header->size, header->kind);
562
+ if (!leak) xt_gc_release(header);
545
563
  }
546
564
  header = next;
547
565
  }
@@ -230,6 +230,9 @@ static void xt_async_register_roots(void) {
230
230
  /* -- worker threads -------------------------------------------------------- */
231
231
 
232
232
  static void xt_async_worker_run(void) {
233
+ /* Workers allocate result values but must never collect: the collector scans
234
+ * the main thread's stack region and is not thread-safe. */
235
+ xt_gc_worker_thread = 1;
233
236
  for (;;) {
234
237
  XT_ASYNC_LOCK();
235
238
  xt_async_job *job = xt_async_pop_work();
@@ -385,28 +385,6 @@ XT_BUILTIN_TRAMPOLINE(xt_builtin_value_queueMicrotask, xt_queue_microtask)
385
385
  XT_BUILTIN_TRAMPOLINE(xt_builtin_value_structuredClone, xt_structured_clone)
386
386
  XT_BUILTIN_TRAMPOLINE(xt_builtin_value_fetch, xt_fetch)
387
387
 
388
- xt_value xt_in(xt_value key, xt_value value) {
389
- if (XT_IS_OBJECT(value)) return xt_object_has(value, key);
390
- if (XT_IS_ARRAY(value)) {
391
- if (xt_is_symbol(key)) {
392
- xt_array *array = (xt_array *)XT_GET_PTR(value);
393
- if (!XT_IS_OBJECT(array->extra)) return XT_FALSE;
394
- return xt_object_has_own(array->extra, key);
395
- }
396
- int32_t index = xt_to_int32(key);
397
- return xt_bool(index >= 0 && (uint32_t)index < xt_as_array(value)->length);
398
- }
399
- if (XT_IS_STRING(value)) {
400
- if (xt_is_symbol(key)) return XT_FALSE;
401
- xt_string *s = xt_as_string(value);
402
- xt_string *k = xt_as_string(xt_to_string(key));
403
- if (k->length == 6 && memcmp(xt_string_bytes(k), "length", 6) == 0) return XT_TRUE;
404
- int32_t index = xt_to_int32(key);
405
- return xt_bool(index >= 0 && (uint32_t)index < xt_string_utf16_length(s));
406
- }
407
- return XT_FALSE;
408
- }
409
-
410
388
  xt_value xt_rest_args(int32_t argc, xt_value *argv, int32_t start) {
411
389
  if (start < 0) start = 0;
412
390
  int32_t count = argc - start;
@@ -482,13 +460,38 @@ xt_value xt_new(xt_value ctor, int32_t argc, xt_value *argv) {
482
460
  return XT_UNDEFINED;
483
461
  }
484
462
  xt_function *function = (xt_function *)XT_GET_PTR(ctor);
485
- xt_value proto = function->prototype;
463
+ /* Share the lazily created `.prototype` with `ctor.prototype`, so ES5 code
464
+ * that assigns methods after the declaration builds a working chain. */
465
+ xt_value proto = xt_function_prototype_object(ctor);
486
466
  xt_value instance = xt_object_new_with_proto(XT_IS_OBJECT(proto) ? proto : XT_UNDEFINED);
487
467
  xt_value result = function->code(instance, ctor, argc, argv);
488
468
  if (XT_IS_OBJECT(result) || XT_IS_ARRAY(result) || XT_IS_FUNCTION(result)) return result;
489
469
  return instance;
490
470
  }
491
471
 
472
+ /* The global object (`globalThis`, and Node's alias `global`).
473
+ *
474
+ * xbintsc resolves a bare global *name* directly (there is no scope object), so
475
+ * this object is a plain property bag: it exists for code that treats the
476
+ * global object as a value — feature probes, `global[key]` lookups, and
477
+ * libraries that publish state under a symbol key on `global`. Property access
478
+ * spelled `globalThis.foo` still lowers to the bare global `foo`, so the two
479
+ * forms agree for names the compiler knows; a miss simply reads `undefined`. */
480
+ static xt_value g_global_object = XT_UNDEFINED;
481
+ static int g_global_object_rooted = 0;
482
+
483
+ xt_value xt_global_object(void) {
484
+ if (g_global_object == XT_UNDEFINED) {
485
+ g_global_object = xt_object_new();
486
+ if (!g_global_object_rooted) {
487
+ xt_gc_add_root_range(&g_global_object, 1);
488
+ g_global_object_rooted = 1;
489
+ }
490
+ }
491
+ return g_global_object;
492
+ }
493
+
494
+
492
495
  xt_value xt_instance_of(xt_value value, xt_value ctor) {
493
496
  if (!XT_IS_FUNCTION(ctor)) return XT_FALSE;
494
497
  xt_value proto = ((xt_function *)XT_GET_PTR(ctor))->prototype;
@@ -40,6 +40,11 @@ xt_value xt_count_queuing_strategy_ctor(int32_t argc, xt_value *argv);
40
40
  xt_value xt_byte_length_queuing_strategy_ctor(int32_t argc, xt_value *argv);
41
41
  xt_value xt_text_encoder_stream_ctor(int32_t argc, xt_value *argv);
42
42
  xt_value xt_text_decoder_stream_ctor(int32_t argc, xt_value *argv);
43
+ /* DataView's constructor and ArrayBuffer's statics live with the ArrayBuffer
44
+ * implementation; the proxy constructor lives with its hooks. */
45
+ xt_value xt_data_view_ctor(int32_t argc, xt_value *argv);
46
+ xt_value xt_proxy_ctor(int32_t argc, xt_value *argv);
47
+ xt_value xt_array_buffer_static(xt_value method, int32_t argc, xt_value *argv);
43
48
  int xt_streams_has_static(const char *name, const char *method);
44
49
  xt_value xt_streams_static(xt_value name, int32_t argc, xt_value *argv);
45
50
 
@@ -54,7 +59,11 @@ enum {
54
59
  XT_PROTO_WRITABLE_STREAM,
55
60
  XT_PROTO_TRANSFORM_STREAM,
56
61
  XT_PROTO_COUNT_STRATEGY,
57
- XT_PROTO_BYTE_STRATEGY
62
+ XT_PROTO_BYTE_STRATEGY,
63
+ XT_PROTO_ABORT_CONTROLLER,
64
+ XT_PROTO_ABORT_SIGNAL,
65
+ XT_PROTO_HEADERS,
66
+ XT_PROTO_RESPONSE
58
67
  };
59
68
 
60
69
  typedef struct {
@@ -104,7 +113,14 @@ typedef struct {
104
113
  X(CountQueuingStrategy, 1, XT_PROTO_COUNT_STRATEGY) \
105
114
  X(ByteLengthQueuingStrategy, 1, XT_PROTO_BYTE_STRATEGY) \
106
115
  X(TextEncoderStream, 0, XT_PROTO_TRANSFORM_STREAM) \
107
- X(TextDecoderStream, 0, XT_PROTO_TRANSFORM_STREAM)
116
+ X(TextDecoderStream, 0, XT_PROTO_TRANSFORM_STREAM) \
117
+ X(AbortController, 0, XT_PROTO_ABORT_CONTROLLER) \
118
+ X(AbortSignal, 0, XT_PROTO_ABORT_SIGNAL) \
119
+ X(Headers, 1, XT_PROTO_HEADERS) \
120
+ X(Response, 2, XT_PROTO_RESPONSE) \
121
+ X(ArrayBuffer, 1, XT_PROTO_PLAIN) \
122
+ X(DataView, 1, XT_PROTO_PLAIN) \
123
+ X(Proxy, 2, XT_PROTO_PLAIN)
108
124
 
109
125
  #define XT_CTOR_ENUM(name, arity, proto) XT_CTOR_##name,
110
126
  enum { XT_CTOR_SPECS(XT_CTOR_ENUM) XT_CTOR_COUNT };
@@ -220,6 +236,13 @@ static xt_value xt_builtin_ctor_call(const char *name, int32_t argc, xt_value *a
220
236
  if (strcmp(name, "ByteLengthQueuingStrategy") == 0) return xt_byte_length_queuing_strategy_ctor(argc, argv);
221
237
  if (strcmp(name, "TextEncoderStream") == 0) return xt_text_encoder_stream_ctor(argc, argv);
222
238
  if (strcmp(name, "TextDecoderStream") == 0) return xt_text_decoder_stream_ctor(argc, argv);
239
+ if (strcmp(name, "AbortController") == 0) return xt_abort_controller_ctor(argc, argv);
240
+ if (strcmp(name, "AbortSignal") == 0) return xt_abort_signal_ctor(argc, argv);
241
+ if (strcmp(name, "Headers") == 0) return xt_headers_ctor(argc, argv);
242
+ if (strcmp(name, "Response") == 0) return xt_response_ctor(argc, argv);
243
+ if (strcmp(name, "ArrayBuffer") == 0) return xt_array_buffer_ctor(argc, argv);
244
+ if (strcmp(name, "DataView") == 0) return xt_data_view_ctor(argc, argv);
245
+ if (strcmp(name, "Proxy") == 0) return xt_proxy_ctor(argc, argv);
223
246
  if (xt_ctor_is_typed_array(name)) return xt_typed_array_ctor_by_name(nameValue, argc, argv);
224
247
  if (xt_ctor_is_error(name)) return xt_error_ctor_by_name(nameValue, argc, argv);
225
248
  return XT_UNDEFINED;
@@ -261,10 +284,21 @@ static xt_value xt_ctor_prototype(int index, xt_value nameValue) {
261
284
  return xt_stream_count_strategy_proto();
262
285
  case XT_PROTO_BYTE_STRATEGY:
263
286
  return xt_stream_byte_strategy_proto();
287
+ case XT_PROTO_ABORT_CONTROLLER:
288
+ return xt_web_abort_controller_proto();
289
+ case XT_PROTO_ABORT_SIGNAL:
290
+ return xt_web_abort_signal_proto();
291
+ case XT_PROTO_HEADERS:
292
+ return xt_web_headers_proto();
293
+ case XT_PROTO_RESPONSE:
294
+ return xt_web_response_proto();
264
295
  case XT_PROTO_PLAIN:
265
296
  default:
266
297
  if (xt_ctor_plain_protos[index] == XT_UNDEFINED) {
267
298
  xt_ctor_plain_protos[index] = xt_object_new();
299
+ /* `Map.prototype.get`, `String.prototype.slice`, ... are ordinary
300
+ * values; libraries bind them (`Map.prototype.get.bind(map)`). */
301
+ xt_install_proto_methods(xt_ctor_plain_protos[index], spec->name);
268
302
  }
269
303
  return xt_ctor_plain_protos[index];
270
304
  }
@@ -337,6 +371,8 @@ static const char *const XT_DATE_STATICS[] = {"now", "parse", "UTC"};
337
371
  static const char *const XT_PROMISE_STATICS[] = {
338
372
  "resolve", "reject", "all", "allSettled", "race", "any", "withResolvers",
339
373
  };
374
+ static const char *const XT_ABORT_SIGNAL_STATICS[] = {"abort", "timeout", "any"};
375
+ static const char *const XT_ARRAY_BUFFER_STATICS[] = {"isView"};
340
376
  static const char *const XT_TYPED_ARRAY_STATICS[] = {"from", "of"};
341
377
 
342
378
  static int xt_name_in(const char *name, const char *const *names, size_t count) {
@@ -367,6 +403,10 @@ static int xt_ctor_has_static(const char *namespace, const char *method) {
367
403
  return xt_name_in(method, XT_PROMISE_STATICS, XT_NAMES_COUNT(XT_PROMISE_STATICS));
368
404
  if (xt_ctor_is_typed_array(namespace))
369
405
  return xt_name_in(method, XT_TYPED_ARRAY_STATICS, XT_NAMES_COUNT(XT_TYPED_ARRAY_STATICS));
406
+ if (strcmp(namespace, "AbortSignal") == 0)
407
+ return xt_name_in(method, XT_ABORT_SIGNAL_STATICS, XT_NAMES_COUNT(XT_ABORT_SIGNAL_STATICS));
408
+ if (strcmp(namespace, "ArrayBuffer") == 0)
409
+ return xt_name_in(method, XT_ARRAY_BUFFER_STATICS, XT_NAMES_COUNT(XT_ARRAY_BUFFER_STATICS));
370
410
  if (xt_streams_has_static(namespace, method)) return 1;
371
411
  return 0;
372
412
  }
@@ -385,6 +425,8 @@ static xt_value xt_builtin_static_call(const char *namespace, const char *method
385
425
  if (strcmp(namespace, "Promise") == 0) return xt_promise_static(methodValue, argc, argv);
386
426
  if (xt_ctor_is_typed_array(namespace))
387
427
  return xt_typed_array_static_by_name(nameValue, methodValue, argc, argv);
428
+ if (strcmp(namespace, "AbortSignal") == 0) return xt_abort_signal_static(methodValue, argc, argv);
429
+ if (strcmp(namespace, "ArrayBuffer") == 0) return xt_array_buffer_static(methodValue, argc, argv);
388
430
  if (xt_streams_has_static(namespace, method)) return xt_streams_static(methodValue, argc, argv);
389
431
  return XT_UNDEFINED;
390
432
  }
@@ -450,3 +492,60 @@ xt_value xt_builtin_prototype_for_kind(int32_t kind) {
450
492
  xt_value constructor = xt_builtin_constructor(xt_string_from_cstr(name));
451
493
  return xt_function_get_prototype(constructor);
452
494
  }
495
+
496
+ /*
497
+ * `super(...)` and the `extends` prototype link.
498
+ *
499
+ * A class statement chains its prototype onto the parent class's prototype and
500
+ * runs the parent constructor from the derived one. A user class publishes its
501
+ * constructor closure as `__ctor` on its prototype, which is what a subclass's
502
+ * `super(...)` must run. A native/extension class (`class X extends
503
+ * EventEmitter`) has no such hook: its constructor function is the class
504
+ * value's own [[Prototype]] (the class statement links the two for static
505
+ * inheritance), and its prototype object only exists once it has constructed
506
+ * something. Both helpers hide that difference from the code generator.
507
+ */
508
+
509
+ xt_value xt_super_construct(xt_value thisValue, xt_value owner, xt_value parentProto,
510
+ int32_t argc, xt_value *argv) {
511
+ xt_value ctor = xt_get(parentProto, xt_string_from_cstr("__ctor"));
512
+ if (XT_IS_FUNCTION(ctor)) return xt_call_with_this(ctor, thisValue, argc, argv);
513
+ xt_value parentCtor = xt_object_get_prototype(owner);
514
+ if (!XT_IS_FUNCTION(parentCtor)) return XT_UNDEFINED;
515
+ /* Run the parent constructor and carry over the state it put on the instance
516
+ * it allocated. Constructors exposed this way (EventEmitter, the streams) are
517
+ * stateful only in lazily created properties, so this is usually a no-op. */
518
+ xt_value parent = xt_new(parentCtor, argc, argv);
519
+ if (!XT_IS_OBJECT(parent)) return thisValue;
520
+ xt_value keys = xt_object_keys(parent);
521
+ int32_t count = (int32_t)xt_to_number(xt_array_length(keys));
522
+ for (int32_t index = 0; index < count; index++) {
523
+ xt_value key = xt_array_get(keys, xt_number((double)index));
524
+ xt_set(thisValue, key, xt_get(parent, key));
525
+ }
526
+ return thisValue;
527
+ }
528
+
529
+ xt_value xt_parent_prototype(xt_value ctor) {
530
+ if (!XT_IS_FUNCTION(ctor)) return XT_UNDEFINED;
531
+ /* A class value publishes its prototype object. Every other callable gets the
532
+ * lazily created plain object a function's `.prototype` is in JavaScript —
533
+ * never an instance: `class X extends SomeConstructor` must not run the
534
+ * parent's body at class-definition time (it may need arguments, or have
535
+ * side effects the program did not ask for). An extension class that wants a
536
+ * real prototype publishes it when its constructor value is built. */
537
+ return xt_function_prototype_object(ctor);
538
+ }
539
+
540
+ /* `new <name>(...)` where the callee is not a constructor: name the callee so
541
+ * the error points at the source (`new missing.Thing()`), not at nothing. */
542
+ xt_value xt_new_named(xt_value ctor, xt_value name, int32_t argc, xt_value *argv) {
543
+ if (!XT_IS_FUNCTION(ctor)) {
544
+ xt_value label = XT_IS_STRING(name) ? name : xt_string_from_cstr("(expression)");
545
+ xt_value message = xt_add(xt_string_from_cstr("TypeError: new "),
546
+ xt_add(label, xt_string_from_cstr(" is not a constructor")));
547
+ xt_throw(xt_error_from_message(xt_string_data(message)));
548
+ return XT_UNDEFINED;
549
+ }
550
+ return xt_new(ctor, argc, argv);
551
+ }
@@ -478,4 +478,32 @@ xt_value xt_object_has_own(xt_value value, xt_value key) {
478
478
  return XT_TRUE;
479
479
  }
480
480
  return XT_FALSE;
481
- }
481
+ }
482
+
483
+ /* `length` / `name` / `prototype` are synthesised by `xt_get` for functions and
484
+ * have no property-bag entry, so `"length" in fn` must answer for them. */
485
+ static int xt_in_function_key(xt_value value, xt_value key) {
486
+ if (!XT_IS_FUNCTION(value) || !XT_IS_STRING(key)) return 0;
487
+ xt_string *k = xt_as_string(key);
488
+ if (k->length == 6 && memcmp(xt_string_bytes(k), "length", 6) == 0) return 1;
489
+ if (k->length == 4 && memcmp(xt_string_bytes(k), "name", 4) == 0) return 1;
490
+ if (k->length == 9 && memcmp(xt_string_bytes(k), "prototype", 9) == 0) return 1;
491
+ return 0;
492
+ }
493
+
494
+ /* `key in value`, including the prototype chain and the built-in methods that
495
+ * live in the runtime's method tables rather than in a property bag. Without
496
+ * this the operator only answered for own properties, so `"map" in []` and
497
+ * `"call" in someFunction` were false. */
498
+ xt_value xt_in(xt_value key, xt_value value) {
499
+ if (xt_truthy(xt_object_has_own(value, key))) return XT_TRUE;
500
+ if (xt_in_function_key(value, key)) return XT_TRUE;
501
+ if (xt_method_value(value, key) != XT_UNDEFINED) return XT_TRUE;
502
+ xt_value proto = xt_object_get_prototype(value);
503
+ while (proto != XT_UNDEFINED && proto != XT_NULL) {
504
+ if (xt_truthy(xt_object_has_own(proto, key))) return XT_TRUE;
505
+ if (xt_method_value(proto, key) != XT_UNDEFINED) return XT_TRUE;
506
+ proto = xt_object_get_prototype(proto);
507
+ }
508
+ return XT_FALSE;
509
+ }
@@ -0,0 +1,58 @@
1
+ /*
2
+ * Function values and their `.prototype` objects.
3
+ *
4
+ * Split out of `objects.inc` to keep both files inside the repository's
5
+ * length budget; included before it because `xt_object_get` resolves
6
+ * `fn.prototype` through `xt_function_prototype_object`.
7
+ */
8
+
9
+ /* The `.prototype` object of a function value, created on first use with a
10
+ * `constructor` back-reference. `xt_new` and `fn.prototype` must agree on it so
11
+ * the ES5 pattern `function Foo() {}; Foo.prototype.bar = ...` keeps working.
12
+ *
13
+ * Every reference to a declared function builds a fresh closure value (`Foo`
14
+ * inside `Foo.prototype` is not the same value as the `Foo` used by `new`), so
15
+ * the prototype is keyed by the compiled code pointer and shared by all
16
+ * closures of that declaration. Without this, `new Foo()` had a different
17
+ * prototype than `Foo.prototype` and instance methods were invisible. */
18
+ typedef struct xt_function_proto_slot {
19
+ xt_code_fn code;
20
+ xt_value proto;
21
+ struct xt_function_proto_slot *next;
22
+ } xt_function_proto_slot;
23
+
24
+ static xt_function_proto_slot *xt_function_proto_slots = NULL;
25
+ static int xt_function_proto_registered = 0;
26
+
27
+ static void xt_function_proto_roots(void) {
28
+ for (xt_function_proto_slot *slot = xt_function_proto_slots; slot; slot = slot->next) {
29
+ xt_gc_mark_value(slot->proto);
30
+ }
31
+ }
32
+
33
+ xt_value xt_function_prototype_object(xt_value value) {
34
+ if (!XT_IS_FUNCTION(value)) return XT_UNDEFINED;
35
+ xt_function *function = (xt_function *)XT_GET_PTR(value);
36
+ if (function->prototype != XT_UNDEFINED) return function->prototype;
37
+ if (!xt_function_proto_registered) {
38
+ xt_function_proto_registered = 1;
39
+ xt_gc_register_root_provider(xt_function_proto_roots);
40
+ }
41
+ for (xt_function_proto_slot *slot = xt_function_proto_slots; slot; slot = slot->next) {
42
+ if (slot->code == function->code) {
43
+ function->prototype = slot->proto;
44
+ return slot->proto;
45
+ }
46
+ }
47
+ xt_value proto = xt_object_new();
48
+ xt_object_set(proto, xt_string_from_cstr("constructor"), value);
49
+ xt_function_proto_slot *slot = (xt_function_proto_slot *)malloc(sizeof(xt_function_proto_slot));
50
+ if (slot) {
51
+ slot->code = function->code;
52
+ slot->proto = proto;
53
+ slot->next = xt_function_proto_slots;
54
+ xt_function_proto_slots = slot;
55
+ }
56
+ function->prototype = proto;
57
+ return proto;
58
+ }
@@ -125,6 +125,13 @@ xt_value xt_function_prototype(void) {
125
125
  xt_function_proto_ready = 1;
126
126
  xt_function_proto_root = xt_object_new_with_proto(xt_object_prototype());
127
127
  xt_gc_add_root(&xt_function_proto_root);
128
+ /* `Function.prototype.call` / `.apply` / `.bind` / `.toString` are values
129
+ * libraries uncurry (`Function.call.bind(...)`), and
130
+ * `Function.prototype[Symbol.hasInstance]` must be present for the same
131
+ * reason. */
132
+ xt_install_proto_methods(xt_function_proto_root, "Function");
133
+ xt_object_set(xt_function_proto_root, xt_symbol_well_known("hasInstance"),
134
+ xt_function_has_instance_method());
128
135
  /* `Array.constructor === Function` (every function inherits from here). The
129
136
  * factory sees the ready flag, so this does not recurse. */
130
137
  xt_value constructor = xt_builtin_constructor(xt_string_from_cstr("Function"));
@@ -282,9 +289,12 @@ static int xt_key_is_prototype(xt_value key) {
282
289
  xt_value xt_object_get(xt_value value, xt_value key) {
283
290
  /* A function's `.prototype` lives in a dedicated field (set by
284
291
  `xt_function_set_prototype`), not in its property bag. Expose it as a
285
- normal property so `Class.prototype` returns the object instances use. */
292
+ normal property so `Class.prototype` returns the object instances use.
293
+ A plain function declaration starts without one — ES5 code assigns
294
+ `Foo.prototype.bar = ...` right after declaring `Foo`, so materialise it on
295
+ first read, like the specification's [[Construct]]-able function. */
286
296
  if (XT_IS_FUNCTION(value) && xt_key_is_prototype(key)) {
287
- return ((xt_function *)XT_GET_PTR(value))->prototype;
297
+ return xt_function_prototype_object(value);
288
298
  }
289
299
  xt_object *obj = xt_as_object(value);
290
300
  if (!obj) return XT_UNDEFINED;
@@ -523,6 +533,12 @@ xt_value xt_object_keys(xt_value value) {
523
533
  }
524
534
  if (XT_IS_OBJECT(value)) {
525
535
  xt_object *obj = (xt_object *)XT_GET_PTR(value);
536
+ /* Map/Set/Date/RegExp/Promise/Symbol use their own layout and have no
537
+ * property bag; reading `obj->count` there walked off the end of the
538
+ * allocation (`Object.getOwnPropertyNames(new Map())` crashed). */
539
+ if (obj->header.kind != XT_OBJECT_KIND_OBJECT && obj->header.kind != XT_OBJECT_KIND_ERROR) {
540
+ return result;
541
+ }
526
542
  for (uint32_t i = 0; i < obj->count; i++) {
527
543
  if (XT_IS_STRING(obj->properties[i].key)) xt_array_push(result, obj->properties[i].key);
528
544
  }
@@ -17,6 +17,7 @@
17
17
  #include <string.h>
18
18
 
19
19
  #include "xt_containers/shapes.inc"
20
+ #include "xt_containers/function_prototypes.inc"
20
21
  #include "xt_containers/objects.inc"
21
22
  #include "xt_containers/arrays.inc"
22
23
  #include "xt_containers/access.inc"