xbintsc 0.6.12 → 0.6.17

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 (189) hide show
  1. package/dist/src/ast/expressions.d.ts +6 -0
  2. package/dist/src/checker/type-infer.js +1 -1
  3. package/dist/src/checker/type-infer.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/codegen/generator/calls/literals.js +4 -1
  6. package/dist/src/codegen/generator/calls/literals.js.map +1 -1
  7. package/dist/src/codegen/generator/expressions/assignment.js +3 -1
  8. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  9. package/dist/src/codegen/generator/expressions/operators.js +2 -6
  10. package/dist/src/codegen/generator/expressions/operators.js.map +1 -1
  11. package/dist/src/codegen/generator/expressions/primary.d.ts +9 -0
  12. package/dist/src/codegen/generator/expressions/primary.js +44 -7
  13. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  14. package/dist/src/driver/bundler/commonjs.js +1 -1
  15. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  16. package/dist/src/driver/bundler/symbols.js +6 -0
  17. package/dist/src/driver/bundler/symbols.js.map +1 -1
  18. package/dist/src/driver/version.d.ts +1 -1
  19. package/dist/src/driver/version.js +1 -1
  20. package/dist/src/extensions/gui/features.d.ts +2 -0
  21. package/dist/src/extensions/gui/features.js +24 -0
  22. package/dist/src/extensions/gui/features.js.map +1 -1
  23. package/dist/src/extensions/gui/index.js +39 -0
  24. package/dist/src/extensions/gui/index.js.map +1 -1
  25. package/dist/src/extensions/node/index.js +2 -0
  26. package/dist/src/extensions/node/index.js.map +1 -1
  27. package/dist/src/extensions/node/readline/index.d.ts +12 -0
  28. package/dist/src/extensions/node/readline/index.js +27 -0
  29. package/dist/src/extensions/node/readline/index.js.map +1 -0
  30. package/dist/src/parser/declarations/functions.js +6 -0
  31. package/dist/src/parser/declarations/functions.js.map +1 -1
  32. package/dist/src/parser/literals.js +1 -1
  33. package/dist/src/parser/literals.js.map +1 -1
  34. package/dist/tests/e2e/compile-modules.test.js +22 -0
  35. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  36. package/dist/tests/e2e/date-parse.test.js +17 -0
  37. package/dist/tests/e2e/date-parse.test.js.map +1 -1
  38. package/dist/tests/e2e/gui-browser.test.js +61 -0
  39. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  40. package/dist/tests/e2e/gui-css-coverage.test.js +39 -0
  41. package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
  42. package/dist/tests/e2e/gui-css-features.test.js +100 -0
  43. package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
  44. package/dist/tests/e2e/gui-dom.test.js +34 -0
  45. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  46. package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
  47. package/dist/tests/e2e/gui-errors.test.js +68 -0
  48. package/dist/tests/e2e/gui-errors.test.js.map +1 -0
  49. package/dist/tests/e2e/gui-example-pelican3d.test.d.ts +6 -4
  50. package/dist/tests/e2e/gui-example-pelican3d.test.js +103 -44
  51. package/dist/tests/e2e/gui-example-pelican3d.test.js.map +1 -1
  52. package/dist/tests/e2e/gui-example-webgpu.test.d.ts +9 -0
  53. package/dist/tests/e2e/gui-example-webgpu.test.js +59 -0
  54. package/dist/tests/e2e/gui-example-webgpu.test.js.map +1 -0
  55. package/dist/tests/e2e/gui-helpers.d.ts +1 -1
  56. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +7 -1
  57. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -1
  58. package/dist/tests/e2e/gui-webgpu.test.d.ts +11 -0
  59. package/dist/tests/e2e/gui-webgpu.test.js +402 -0
  60. package/dist/tests/e2e/gui-webgpu.test.js.map +1 -0
  61. package/dist/tests/e2e/harness.d.ts +4 -0
  62. package/dist/tests/e2e/harness.js +2 -0
  63. package/dist/tests/e2e/harness.js.map +1 -1
  64. package/dist/tests/e2e/readline.test.d.ts +7 -0
  65. package/dist/tests/e2e/readline.test.js +73 -0
  66. package/dist/tests/e2e/readline.test.js.map +1 -0
  67. package/dist/tests/extensions/gui-features.test.js +18 -1
  68. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  69. package/doc/ai/extensions.md +7 -1
  70. package/doc/ai/zh-CN/extensions.md +6 -1
  71. package/doc/gui/api.md +15 -0
  72. package/doc/gui/dom.md +7 -2
  73. package/doc/gui/known-issues.md +2 -1
  74. package/doc/gui/layout.md +5 -1
  75. package/doc/gui/webgpu.md +121 -0
  76. package/doc/gui.md +12 -1
  77. package/doc/implemented/node/readline.md +34 -0
  78. package/doc/implemented/node.md +1 -0
  79. package/doc/unimplemented/node/modules.md +2 -3
  80. package/doc/unimplemented/node.md +2 -2
  81. package/doc/zh-CN/gui/api.md +11 -0
  82. package/doc/zh-CN/gui/dom.md +6 -2
  83. package/doc/zh-CN/gui/known-issues.md +2 -1
  84. package/doc/zh-CN/gui/layout.md +4 -1
  85. package/doc/zh-CN/gui/webgpu.md +111 -0
  86. package/doc/zh-CN/gui.md +10 -1
  87. package/doc/zh-CN/implemented/node/readline.md +33 -0
  88. package/doc/zh-CN/implemented/node.md +1 -0
  89. package/doc/zh-CN/unimplemented/node/modules.md +1 -2
  90. package/doc/zh-CN/unimplemented/node.md +2 -2
  91. package/llms.txt +1 -0
  92. package/package.json +7 -7
  93. package/runtime/ext_gui/browser.cpp +10 -0
  94. package/runtime/ext_gui/browser_abort.cpp +2 -2
  95. package/runtime/ext_gui/browser_loop.cpp +1 -0
  96. package/runtime/ext_gui/browser_media.cpp +1 -1
  97. package/runtime/ext_gui/dom_api_canvas.cpp +21 -0
  98. package/runtime/ext_gui/dom_api_canvas_extra.cpp +61 -0
  99. package/runtime/ext_gui/dom_api_canvas_image.cpp +2 -1
  100. package/runtime/ext_gui/dom_api_events.cpp +1 -1
  101. package/runtime/ext_gui/dom_api_internal.h +2 -0
  102. package/runtime/ext_gui/dom_api_node.cpp +18 -12
  103. package/runtime/ext_gui/dom_api_node_read.cpp +27 -0
  104. package/runtime/ext_gui/dom_api_proto.cpp +2 -0
  105. package/runtime/ext_gui/error_report.cpp +166 -0
  106. package/runtime/ext_gui/gpu_feature.cpp +2 -0
  107. package/runtime/ext_gui/gpu_shader_api.h +6 -0
  108. package/runtime/ext_gui/gui.cpp +16 -0
  109. package/runtime/ext_gui/gui_canvas.cpp +18 -0
  110. package/runtime/ext_gui/gui_engine.h +13 -0
  111. package/runtime/ext_gui/gui_events.cpp +4 -1
  112. package/runtime/ext_gui/layout_inline.cpp +46 -14
  113. package/runtime/ext_gui/paint.cpp +22 -0
  114. package/runtime/ext_gui/renderer_image.cpp +6 -0
  115. package/runtime/ext_gui/script.cpp +1 -1
  116. package/runtime/ext_gui/style.cpp +1 -0
  117. package/runtime/ext_gui/style.h +13 -0
  118. package/runtime/ext_gui/style_print.cpp +99 -0
  119. package/runtime/ext_gui/style_text.cpp +79 -0
  120. package/runtime/ext_gui/webgl_glsl.h +8 -0
  121. package/runtime/ext_gui/webgl_glsl_compile.cpp +11 -5
  122. package/runtime/ext_gui/webgl_msl.cpp +20 -9
  123. package/runtime/ext_gui/webgl_msl.h +4 -0
  124. package/runtime/ext_gui/webgl_texture.cpp +0 -8
  125. package/runtime/ext_gui/webgpu.h +371 -0
  126. package/runtime/ext_gui/webgpu_api.h +42 -0
  127. package/runtime/ext_gui/webgpu_bindings.cpp +364 -0
  128. package/runtime/ext_gui/webgpu_bindings.h +59 -0
  129. package/runtime/ext_gui/webgpu_bridge.c +21 -0
  130. package/runtime/ext_gui/webgpu_buffer.cpp +293 -0
  131. package/runtime/ext_gui/webgpu_canvas.cpp +305 -0
  132. package/runtime/ext_gui/webgpu_command.cpp +510 -0
  133. package/runtime/ext_gui/webgpu_compute_pass.cpp +138 -0
  134. package/runtime/ext_gui/webgpu_convert.cpp +309 -0
  135. package/runtime/ext_gui/webgpu_device.cpp +370 -0
  136. package/runtime/ext_gui/webgpu_feature.cpp +74 -0
  137. package/runtime/ext_gui/webgpu_objects.cpp +125 -0
  138. package/runtime/ext_gui/webgpu_pass.cpp +295 -0
  139. package/runtime/ext_gui/webgpu_pass.h +33 -0
  140. package/runtime/ext_gui/webgpu_pipeline.cpp +463 -0
  141. package/runtime/ext_gui/webgpu_queue.cpp +222 -0
  142. package/runtime/ext_gui/webgpu_render_pass.cpp +417 -0
  143. package/runtime/ext_gui/webgpu_resources.cpp +315 -0
  144. package/runtime/ext_gui/webgpu_shader.cpp +242 -0
  145. package/runtime/ext_gui/wgsl.h +99 -0
  146. package/runtime/ext_gui/wgsl_api.cpp +137 -0
  147. package/runtime/ext_gui/wgsl_ast.h +233 -0
  148. package/runtime/ext_gui/wgsl_emit.cpp +452 -0
  149. package/runtime/ext_gui/wgsl_emit.h +128 -0
  150. package/runtime/ext_gui/wgsl_emit_call.cpp +281 -0
  151. package/runtime/ext_gui/wgsl_emit_expr.cpp +192 -0
  152. package/runtime/ext_gui/wgsl_emit_stmt.cpp +209 -0
  153. package/runtime/ext_gui/wgsl_entry.cpp +189 -0
  154. package/runtime/ext_gui/wgsl_infer.cpp +171 -0
  155. package/runtime/ext_gui/wgsl_lexer.cpp +179 -0
  156. package/runtime/ext_gui/wgsl_lexer.h +32 -0
  157. package/runtime/ext_gui/wgsl_parse_expr.cpp +215 -0
  158. package/runtime/ext_gui/wgsl_parse_stmt.cpp +259 -0
  159. package/runtime/ext_gui/wgsl_parser.cpp +457 -0
  160. package/runtime/ext_gui/wgsl_parser.h +77 -0
  161. package/runtime/ext_gui/wgsl_type.cpp +232 -0
  162. package/runtime/ext_gui/window.cpp +22 -8
  163. package/runtime/ext_gui/window_emit.cpp +1 -1
  164. package/runtime/ext_node/fs/parts/platform.inc +2 -0
  165. package/runtime/ext_node/readline/readline.c +572 -0
  166. package/runtime/rt.h +15 -0
  167. package/runtime/rt_internal.h +3 -2
  168. package/runtime/xt_io/exception.inc +29 -6
  169. package/runtime/xt_loop.c +36 -1
  170. package/runtime/xt_promise.c +26 -2
  171. package/runtime/xt_stdlib2/date.inc +2 -2
  172. package/runtime/xt_typed_array/elements.inc +0 -9
  173. package/runtime/xt_url.c +0 -6
  174. package/runtime/xt_url_object.c +1 -1
  175. package/src/ast/expressions.ts +6 -0
  176. package/src/checker/type-infer.ts +1 -1
  177. package/src/codegen/generator/calls/literals.ts +4 -1
  178. package/src/codegen/generator/expressions/assignment.ts +3 -1
  179. package/src/codegen/generator/expressions/operators.ts +2 -6
  180. package/src/codegen/generator/expressions/primary.ts +58 -6
  181. package/src/driver/bundler/commonjs.ts +1 -1
  182. package/src/driver/bundler/symbols.ts +6 -0
  183. package/src/driver/version.ts +1 -1
  184. package/src/extensions/gui/features.ts +24 -0
  185. package/src/extensions/gui/index.ts +39 -0
  186. package/src/extensions/node/index.ts +2 -0
  187. package/src/extensions/node/readline/index.ts +31 -0
  188. package/src/parser/declarations/functions.ts +6 -0
  189. package/src/parser/literals.ts +1 -1
package/runtime/xt_loop.c CHANGED
@@ -108,6 +108,25 @@ double xt_monotonic_ms(void) {
108
108
  #endif
109
109
  }
110
110
 
111
+ /* Wall-clock milliseconds since the Unix epoch, for `Date.now()` and
112
+ * `new Date()`. `time(NULL)` only has second resolution, which silently broke
113
+ * elapsed-time math; this keeps the milliseconds the language exposes. */
114
+ double xt_wallclock_ms(void) {
115
+ #if defined(_WIN32)
116
+ FILETIME file_time;
117
+ GetSystemTimeAsFileTime(&file_time);
118
+ ULARGE_INTEGER ticks;
119
+ ticks.LowPart = file_time.dwLowDateTime;
120
+ ticks.HighPart = file_time.dwHighDateTime;
121
+ /* 100-ns ticks between 1601-01-01 and 1970-01-01. */
122
+ return (double)(int64_t)((ticks.QuadPart - 116444736000000000ULL) / 10000);
123
+ #else
124
+ struct timespec ts;
125
+ clock_gettime(CLOCK_REALTIME, &ts);
126
+ return (double)((int64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
127
+ #endif
128
+ }
129
+
111
130
  static void xt_loop_sleep_ms(int ms) { if (ms <= 0) return;
112
131
  #if defined(_WIN32)
113
132
  Sleep((DWORD)ms);
@@ -222,6 +241,22 @@ static void xt_loop_gc_register(void) {
222
241
  xt_gc_register_root_provider(xt_loop_gc_scan_roots);
223
242
  }
224
243
 
244
+ /* Run one timer callback under a host boundary. Without it an exception from a
245
+ * `setTimeout` callback terminates the process, which in a GUI program looks
246
+ * like the window vanishing; `xt_uncaught_exception` hands it to the host when
247
+ * one is installed and keeps the Node-like fatal default otherwise. */
248
+ static void xt_timer_invoke(xt_value callback, int32_t argc, xt_value *args) {
249
+ void *frame = xt_try_enter();
250
+ if (xt_try_setjmp(frame) == 0) {
251
+ xt_closure_call(callback, argc, args);
252
+ xt_try_leave(frame);
253
+ return;
254
+ }
255
+ xt_value error = xt_try_exception(frame);
256
+ xt_try_leave(frame);
257
+ xt_uncaught_exception(error);
258
+ }
259
+
225
260
  /* Invoke every timer whose deadline has passed. Timers scheduled by a callback
226
261
  * (or by a microtask) are left for the next reactor iteration, so a recursive
227
262
  * `setTimeout(fn, 0)` cannot spin forever inside a single call. */
@@ -240,7 +275,7 @@ static void xt_timer_run_due(void) {
240
275
  } else {
241
276
  timer->active = 0;
242
277
  }
243
- if (XT_IS_FUNCTION(callback)) xt_closure_call(callback, argc, args);
278
+ if (XT_IS_FUNCTION(callback)) xt_timer_invoke(callback, argc, args);
244
279
  }
245
280
  }
246
281
 
@@ -532,11 +532,30 @@ xt_value xt_queue_microtask(int32_t argc, xt_value *argv) {
532
532
 
533
533
  /* -- reporting ------------------------------------------------------------ */
534
534
 
535
+ static xt_unhandled_rejection_handler g_unhandled_rejection_handler = NULL;
536
+
537
+ void xt_set_unhandled_rejection_handler(xt_unhandled_rejection_handler handler) {
538
+ g_unhandled_rejection_handler = handler;
539
+ }
540
+
535
541
  int xt_promise_report_unhandled(void) {
542
+ if (xt_unhandled_count == 0) return 0;
543
+ /* The host handler runs JavaScript, which may observe promises and mutate
544
+ * the queue (removing entries and swapping in the tail). Iterate a snapshot
545
+ * and clear the queue afterwards, so every rejection is reported once. */
546
+ uint32_t count = xt_unhandled_count;
547
+ xt_promise **snapshot = (xt_promise **)malloc(sizeof(xt_promise *) * count);
548
+ if (!snapshot) abort();
549
+ memcpy(snapshot, xt_unhandled, sizeof(xt_promise *) * count);
536
550
  int reported = 0;
537
- for (uint32_t i = 0; i < xt_unhandled_count; i++) {
538
- xt_promise *promise = xt_unhandled[i];
551
+ for (uint32_t i = 0; i < count; i++) {
552
+ xt_promise *promise = snapshot[i];
539
553
  if (!promise || promise->observed) continue;
554
+ int consumed = 0;
555
+ if (g_unhandled_rejection_handler != NULL) {
556
+ consumed = g_unhandled_rejection_handler(promise->value, xt_promise_value(promise));
557
+ }
558
+ if (consumed) continue;
540
559
  reported++;
541
560
  /* `Error` (and its subclasses) carry a captured stack; everything else is
542
561
  * stringified the way Node prints a rejection reason. */
@@ -549,5 +568,10 @@ int xt_promise_report_unhandled(void) {
549
568
  fprintf(stderr, "Unhandled promise rejection\n");
550
569
  }
551
570
  }
571
+ free(snapshot);
572
+ for (uint32_t i = 0; i < xt_unhandled_count; i++) {
573
+ if (xt_unhandled[i]) xt_unhandled[i]->report_index = XT_PROMISE_UNLISTED;
574
+ }
575
+ xt_unhandled_count = 0;
552
576
  return reported;
553
577
  }
@@ -101,7 +101,7 @@ static double xt_date_parse_text(const char *text) {
101
101
 
102
102
  xt_value xt_date_ctor(int32_t argc, xt_value *argv) {
103
103
  xt_date *date = (xt_date *)xt_alloc(sizeof(xt_date), XT_OBJECT_KIND_DATE);
104
- if (argc == 0) date->time = (double)time(NULL) * 1000.0;
104
+ if (argc == 0) date->time = xt_wallclock_ms();
105
105
  else if (argc == 1) {
106
106
  if (XT_IS_STRING(argv[0])) {
107
107
  date->time = xt_date_parse_text(xt_string_data(argv[0]));
@@ -126,7 +126,7 @@ xt_value xt_date_ctor(int32_t argc, xt_value *argv) {
126
126
  xt_value xt_date_static(xt_value name, int32_t argc, xt_value *argv) {
127
127
  const char *fn = xt_string_data(name);
128
128
  if (!fn) return XT_UNDEFINED;
129
- if (strcmp(fn, "now") == 0) return xt_number((double)time(NULL) * 1000.0);
129
+ if (strcmp(fn, "now") == 0) return xt_number(xt_wallclock_ms());
130
130
  if (strcmp(fn, "parse") == 0) {
131
131
  xt_value input = xt_arg_at(argc, argv, 0);
132
132
  if (!XT_IS_STRING(input)) return xt_number(NAN);
@@ -254,15 +254,6 @@ static int32_t xt_ta_relative(int32_t index, int32_t length) {
254
254
  return index;
255
255
  }
256
256
 
257
- static xt_value xt_ta_append(xt_value array, int kind, xt_value value) {
258
- uint32_t length = xt_ta_length(array);
259
- xt_object_set(array, xt_string_from_cstr("length"), xt_number((double)(length + 1)));
260
- xt_object_set(array, xt_string_from_cstr("byteLength"),
261
- xt_number((double)(length + 1) * xt_ta_bytes[kind]));
262
- xt_ta_store(array, kind, length, xt_to_number(value));
263
- return array;
264
- }
265
-
266
257
  uint32_t xt_typed_array_length(xt_value value) {
267
258
  return xt_typed_array_kind_of(value) >= 0 ? xt_ta_length(value) : 0;
268
259
  }
package/runtime/xt_url.c CHANGED
@@ -99,12 +99,6 @@ static void xt_url_append_pair(xt_value entries, xt_value key, xt_value value) {
99
99
  xt_array_push(entries, pair);
100
100
  }
101
101
 
102
- static xt_value xt_url_string_at(xt_value text, size_t start, size_t length) {
103
- xt_string *string = xt_as_string(text);
104
- const char *bytes = xt_string_bytes(string);
105
- return xt_string_new(bytes + start, length);
106
- }
107
-
108
102
  /* Split a query string on `&`, then each item on the first `=`; decode both
109
103
  * halves and append them. A leading `?` is ignored. */
110
104
  static void xt_url_parse_into(xt_value params, xt_value input) {
@@ -339,11 +339,11 @@ xt_value xt_url_ctor(int32_t argc, xt_value *argv) {
339
339
  /* A reference without a scheme is a filesystem path. With no explicit base
340
340
  * it resolves against the working directory, so `new URL("./a.txt")` is a
341
341
  * `file://` URL rather than an unparseable relative one. */
342
- char adjusted[XT_URL_MAX];
343
342
  if (base[0] == 0 && xt_url_scheme_length(inputText) == 0) {
344
343
  xt_url_cwd_base(base, sizeof(base));
345
344
  }
346
345
  #if defined(_WIN32)
346
+ char adjusted[XT_URL_MAX];
347
347
  if (xt_url_scheme_length(inputText) == 0 && isalpha((unsigned char)inputText[0]) &&
348
348
  inputText[1] == ':' && (inputText[2] == '\\' || inputText[2] == '/')) {
349
349
  /* `C:\dir\file` -> `/C:/dir/file`, the path part of a file URL. */
@@ -122,6 +122,12 @@ export interface PropertyAssignment extends Node {
122
122
  export interface ShorthandPropertyAssignment extends Node {
123
123
  readonly kind: SyntaxKind.ShorthandPropertyAssignment;
124
124
  readonly name: Identifier;
125
+ /**
126
+ * The literal property key as written (`"a"` in `{ a }`). Kept separate from
127
+ * `name.text`, which the bundler rewrites when it renames the binding: the
128
+ * key is part of the object shape, not a reference to the binding.
129
+ */
130
+ readonly propertyName: string;
125
131
  /** Present for `{ a = default }` when used as a destructuring target. */
126
132
  readonly initializer?: Expression;
127
133
  }
@@ -156,7 +156,7 @@ function objectLiteralType(context: InferenceContext, literal: ObjectLiteralExpr
156
156
  if (!(property.accessor === "set" && properties.has(name))) properties.set(name, { type, optional: false });
157
157
  } else if (element.kind === SyntaxKind.ShorthandPropertyAssignment) {
158
158
  const shorthand = element as ShorthandPropertyAssignment;
159
- properties.set(shorthand.name.text, { type: inferTypeOfExpression(context, shorthand.name), optional: false });
159
+ properties.set(shorthand.propertyName, { type: inferTypeOfExpression(context, shorthand.name), optional: false });
160
160
  } else {
161
161
  return ANY; // spread: the shape is not static
162
162
  }
@@ -68,7 +68,10 @@ export const literalCallMethods: LiteralCallMethods = {
68
68
  continue;
69
69
  }
70
70
  const identifier = property.name;
71
- const key = this.stringValue(identifier.text);
71
+ /* The key is the spelling in the source; `identifier.text` may have
72
+ * been rewritten to a bundler-mangled name when the variable itself is
73
+ * renamed. */
74
+ const key = this.stringValue(property.propertyName);
72
75
  const value = this.emitIdentifier(identifier);
73
76
  this.emit(` call i64 @xt_set(i64 ${object}, i64 ${key}, i64 ${value})`);
74
77
  } else if (property.kind === SyntaxKind.SpreadElement) {
@@ -217,7 +217,9 @@ export const assignmentExpressionMethods: AssignmentExpressionMethods = {
217
217
  continue;
218
218
  }
219
219
  const shorthand = property as ShorthandPropertyAssignment;
220
- const key = this.stringValue(shorthand.name.text);
220
+ /* A destructuring target reads the source key, not the (possibly
221
+ * bundler-renamed) binding name. */
222
+ const key = this.stringValue(shorthand.propertyName);
221
223
  excludedKeys.push(key);
222
224
  let elementValue = this.reg();
223
225
  this.emit(` ${elementValue} = call i64 @xt_get(i64 ${value}, i64 ${key})`);
@@ -184,12 +184,8 @@ export const operatorExpressionMethods: OperatorExpressionMethods = {
184
184
  this.emit(` ${result} = call i64 @xt_bit_not(i64 ${operand})`);
185
185
  return result;
186
186
  }
187
- case PrefixUnaryOperator.TypeOf: {
188
- const operand = this.emitExpression(node.operand);
189
- const result = this.reg();
190
- this.emit(` ${result} = call i64 @xt_typeof(i64 ${operand})`);
191
- return result;
192
- }
187
+ case PrefixUnaryOperator.TypeOf:
188
+ return this.emitTypeOf(node.operand);
193
189
  case PrefixUnaryOperator.Void:
194
190
  if (node.operand) this.emitExpression(node.operand);
195
191
  return i64(XT_UNDEFINED);
@@ -34,6 +34,9 @@ import {
34
34
  CTOR_FUNCTIONS,
35
35
  ERROR_CONSTRUCTORS,
36
36
  FIRST_CLASS_CONSTRUCTORS,
37
+ GLOBAL_FUNCTIONS,
38
+ NAMESPACE_PROPERTIES,
39
+ NAMESPACE_STATICS,
37
40
  NAMESPACE_VALUES,
38
41
  } from "../tables.js";
39
42
  import type { GlobalValue } from "../../../extensions/registry.js";
@@ -42,6 +45,15 @@ import type { Generator } from "../generator.js";
42
45
  export interface PrimaryExpressionMethods {
43
46
  emitExpression(this: Generator, node: Expression): string;
44
47
  emitIdentifier(this: Generator, identifier: Identifier): string;
48
+ /**
49
+ * `typeof` lowering. Host globals the compiler knows about but that have no
50
+ * first-class value (the console object, `JSON`, `performance`, extension
51
+ * builtins such as `requestAnimationFrame`) used to lower their operand as
52
+ * an undefined constant, so `typeof` reported "undefined" for a global the
53
+ * program could call. Answer these from the known global kinds instead.
54
+ */
55
+ emitTypeOf(this: Generator, operand: Expression): string;
56
+ globalTypeOf(this: Generator, name: string): string | undefined;
45
57
  emitGlobalValue(this: Generator, value: GlobalValue): string;
46
58
  emitFunctionValue(this: Generator, symbol: SymbolInfo): string;
47
59
  emitTemplate(this: Generator, node: TemplateLiteral): string;
@@ -111,12 +123,8 @@ export const primaryExpressionMethods: PrimaryExpressionMethods = {
111
123
  return this.emitExpression((node as { expression: Expression }).expression);
112
124
  case SyntaxKind.DeleteExpression:
113
125
  return this.emitDelete(node as DeleteExpression);
114
- case SyntaxKind.TypeOfExpression: {
115
- const operand = this.emitExpression((node as { expression: Expression }).expression);
116
- const result = this.reg();
117
- this.emit(` ${result} = call i64 @xt_typeof(i64 ${operand})`);
118
- return result;
119
- }
126
+ case SyntaxKind.TypeOfExpression:
127
+ return this.emitTypeOf((node as { expression: Expression }).expression);
120
128
  case SyntaxKind.VoidExpression:
121
129
  this.emitExpression((node as { expression: Expression }).expression);
122
130
  return i64(XT_UNDEFINED);
@@ -166,6 +174,50 @@ export const primaryExpressionMethods: PrimaryExpressionMethods = {
166
174
  }
167
175
  },
168
176
 
177
+ emitTypeOf(operand: Expression): string {
178
+ if (operand.kind === SyntaxKind.Identifier) {
179
+ const identifier = operand as Identifier;
180
+ /* A real binding (including an import) shadows the global, so lower it
181
+ * normally. Only an unresolved name can be a host global. */
182
+ if (!this.binding.symbolOfIdentifier.get(identifier)) {
183
+ const kind = this.globalTypeOf(identifier.text);
184
+ if (kind !== undefined) return this.stringValue(kind);
185
+ }
186
+ }
187
+ const value = this.emitExpression(operand);
188
+ const result = this.reg();
189
+ this.emit(` ${result} = call i64 @xt_typeof(i64 ${value})`);
190
+ return result;
191
+ },
192
+
193
+ /** Known kind of an unresolved global name, or undefined when the runtime
194
+ * getter should answer (`window`, `document`, `localStorage`, …). */
195
+ globalTypeOf(this: Generator, name: string): string | undefined {
196
+ /* A host extension function has no object value yet, but it is callable. */
197
+ if (this.builtins[name]) return "function";
198
+ /* Extension values are real host objects (or deliberately undefined for an
199
+ * unsupported type), so let `xt_typeof` inspect the getter's result. */
200
+ if (this.globalValues[name]) return undefined;
201
+ if (
202
+ ERROR_CONSTRUCTORS[name] ||
203
+ FIRST_CLASS_CONSTRUCTORS.has(name) ||
204
+ CTOR_FUNCTIONS[name] ||
205
+ BUILTIN_FUNCTION_VALUES[name] ||
206
+ GLOBAL_FUNCTIONS[name]
207
+ ) {
208
+ return "function";
209
+ }
210
+ if (
211
+ name === "console" ||
212
+ NAMESPACE_STATICS[name] ||
213
+ NAMESPACE_VALUES[name] ||
214
+ NAMESPACE_PROPERTIES[name]
215
+ ) {
216
+ return "object";
217
+ }
218
+ return undefined;
219
+ },
220
+
169
221
  emitIdentifier(identifier: Identifier): string {
170
222
  const symbol = this.binding.symbolOfIdentifier.get(identifier);
171
223
  if (symbol) {
@@ -292,7 +292,7 @@ function isModuleExports(expression: Expression, bind: BindResult): boolean {
292
292
 
293
293
  function literalPropertyName(property: ObjectLiteralElementLike): string | undefined {
294
294
  if (property.kind === SyntaxKind.ShorthandPropertyAssignment) {
295
- return (property as unknown as { name: Identifier }).name.text;
295
+ return (property as unknown as { propertyName: string }).propertyName;
296
296
  }
297
297
  if (property.kind === SyntaxKind.PropertyAssignment) {
298
298
  const name = (property as unknown as { name: Node }).name;
@@ -14,6 +14,12 @@ export function hasModifier(node: Node, kind: ModifierKind): boolean {
14
14
 
15
15
  export function declarationName(node: Node): Identifier | undefined {
16
16
  switch (node.kind) {
17
+ /* Destructured bindings are declared with their identifier node (see
18
+ * `Binder.declareBinding`), so renaming must accept a bare Identifier too;
19
+ * otherwise the declaration keeps its old name while references are
20
+ * rewritten and the merged program fails to resolve it. */
21
+ case SyntaxKind.Identifier:
22
+ return node as Identifier;
17
23
  case SyntaxKind.FunctionDeclaration:
18
24
  case SyntaxKind.ClassDeclaration:
19
25
  case SyntaxKind.EnumDeclaration:
@@ -9,4 +9,4 @@
9
9
  * `main`/`master` only (see `.githooks/pre-commit`), so feature branches never
10
10
  * change this line and merging two of them cannot conflict here.
11
11
  */
12
- export const COMPILER_VERSION = "0.6.12";
12
+ export const COMPILER_VERSION = "0.6.17";
@@ -40,9 +40,21 @@ export function hasMediaUsage(usage: ProgramUsage): boolean {
40
40
  return usage.strings.some((value) => MEDIA_TAG.test(value) || MEDIA_TAG_NAME.test(value));
41
41
  }
42
42
 
43
+ /** True when the program may use `navigator.gpu` / WGSL shaders. */
44
+ export function hasWebgpuUsage(usage: ProgramUsage): boolean {
45
+ if (usage.dynamicMarkup) return true;
46
+ for (const name of usage.identifiers) {
47
+ if (name.startsWith("GPU")) return true;
48
+ }
49
+ return usage.strings.some((value) =>
50
+ /webgpu|navigator\.gpu|requestAdapter|createShaderModule|@vertex|@fragment|@compute|wgsl/i.test(value),
51
+ );
52
+ }
53
+
43
54
  /** True when the program may compile GLSL at run time (WebGL or blending). */
44
55
  export function hasGpuUsage(usage: ProgramUsage): boolean {
45
56
  if (usage.dynamicMarkup) return true;
57
+ if (hasWebgpuUsage(usage)) return true;
46
58
  if (usage.identifiers.has("mixBlendMode") || usage.identifiers.has("backdropFilter")) return true;
47
59
  if (usage.getContext.some((kind) => kind === undefined || /webgl/i.test(kind))) return true;
48
60
  return usage.strings.some((value) => GPU_TOKEN.test(value));
@@ -151,6 +163,18 @@ export const guiLinkFeatures: readonly LinkFeature[] = [
151
163
  runtimeSources: () => [join(findRuntimeDir(), "ext_gui", "text_bridge.c")],
152
164
  used: hasShapingUsage,
153
165
  },
166
+ {
167
+ name: "webgpu",
168
+ description: "WebGPU (navigator.gpu, WGSL) rendering over SDL_GPU (requires gpu)",
169
+ requires: ["gpu"],
170
+ /**
171
+ * `webgpu_bridge.c` installs `XtWebgpuApi` and references the WebGPU
172
+ * runtime, pulling the object model, WGSL front end and canvas bindings
173
+ * out of `gui.a`. A page without WebGPU links none of them.
174
+ */
175
+ runtimeSources: () => [join(findRuntimeDir(), "ext_gui", "webgpu_bridge.c")],
176
+ used: hasWebgpuUsage,
177
+ },
154
178
  {
155
179
  name: "webgl",
156
180
  description: "WebGL context and DOM API (requires gpu)",
@@ -105,6 +105,45 @@ const hostTypes = [
105
105
  "WebGLTransformFeedback",
106
106
  "XRWebGLLayer",
107
107
  "XRWebGLBinding",
108
+ /* WebGPU interfaces (see runtime/ext_gui/webgpu_bindings.cpp). The `webgpu`
109
+ * link feature installs them; without it `xt_browser_type` falls back to the
110
+ * DOM answer, so a `typeof GPUDevice` probe is honest. */
111
+ "GPU",
112
+ "GPUAdapter",
113
+ "GPUAdapterInfo",
114
+ "GPUDevice",
115
+ "GPUQueue",
116
+ "GPUBuffer",
117
+ "GPUTexture",
118
+ "GPUTextureView",
119
+ "GPUSampler",
120
+ "GPUShaderModule",
121
+ "GPUBindGroupLayout",
122
+ "GPUPipelineLayout",
123
+ "GPUBindGroup",
124
+ "GPURenderPipeline",
125
+ "GPUComputePipeline",
126
+ "GPUCommandEncoder",
127
+ "GPUCommandBuffer",
128
+ "GPURenderPassEncoder",
129
+ "GPUComputePassEncoder",
130
+ "GPURenderBundleEncoder",
131
+ "GPURenderBundle",
132
+ "GPUQuerySet",
133
+ "GPUCanvasContext",
134
+ "GPUCompilationInfo",
135
+ "GPUCompilationMessage",
136
+ "GPUSupportedFeatures",
137
+ "GPUSupportedLimits",
138
+ "GPUBufferUsage",
139
+ "GPUTextureUsage",
140
+ "GPUShaderStage",
141
+ "GPUMapMode",
142
+ "GPUColorWrite",
143
+ "GPUValidationError",
144
+ "GPUOutOfMemoryError",
145
+ "GPUInternalError",
146
+ "GPUDeviceLostInfo",
108
147
  ] as const;
109
148
 
110
149
  const browserGlobals: Record<string, { symbol: string; argument?: string }> = {
@@ -33,6 +33,7 @@ import { utilModule } from "./util/index.js";
33
33
  import { querystringModule } from "./querystring/index.js";
34
34
  import { assertModule } from "./assert/index.js";
35
35
  import { testModule } from "./test/index.js";
36
+ import { readlineModule } from "./readline/index.js";
36
37
 
37
38
  /** Every Node module the extension currently provides. */
38
39
  const modules: readonly NodeModule[] = [
@@ -57,6 +58,7 @@ const modules: readonly NodeModule[] = [
57
58
  querystringModule,
58
59
  assertModule,
59
60
  testModule,
61
+ readlineModule,
60
62
  ];
61
63
 
62
64
  export const nodeExtension: Extension = {
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Node's `readline` module.
3
+ *
4
+ * `createInterface` (and the `Interface` constructor) return a line reader over
5
+ * standard input. The implementation offloads the blocking stdin read to the
6
+ * runtime's async I/O pool, so `question` and `on("line")` do not block timers
7
+ * or the event loop; it lives in
8
+ * `runtime/ext_node/readline/readline.c`. The cursor helpers write the same
9
+ * ANSI sequences Node emits.
10
+ */
11
+
12
+ import type { NodeModule } from "../module.js";
13
+ import { resolveFrom } from "../module.js";
14
+ import type { ModuleExports } from "../../registry.js";
15
+
16
+ const exports: ModuleExports = {
17
+ createInterface: { symbol: "xt_readline_create_interface" },
18
+ Interface: { symbol: "xt_readline_interface_ctor", isConstructor: true },
19
+ cursorTo: { symbol: "xt_readline_cursor_to" },
20
+ moveCursor: { symbol: "xt_readline_move_cursor" },
21
+ clearLine: { symbol: "xt_readline_clear_line" },
22
+ clearScreenDown: { symbol: "xt_readline_clear_screen_down" },
23
+ emitKeypressEvents: { symbol: "xt_readline_emit_keypress_events" },
24
+ };
25
+
26
+ export const readlineModule: NodeModule = {
27
+ name: "readline",
28
+ runtimeSources: () => [resolveFrom(import.meta.url, "../../../../runtime/ext_node/readline/readline.c")],
29
+ builtins: () => ({}),
30
+ exports: () => exports,
31
+ };
@@ -227,6 +227,12 @@ export const declarationFunctionMethods: DeclarationFunctionMethods = {
227
227
  name = this.parseBindingName();
228
228
  } else {
229
229
  name = first;
230
+ /* `{ a }` reads the key `a`; record it in a separate node so a later
231
+ * rename of the binding does not change which property is read. */
232
+ propertyName =
233
+ first.kind === SyntaxKind.Identifier
234
+ ? { kind: SyntaxKind.Identifier, text: first.text, escaped: first.escaped, start: first.start, end: first.end }
235
+ : first;
230
236
  }
231
237
  } else {
232
238
  // Array element, or a nested pattern used as an object value.
@@ -144,7 +144,7 @@ export const literalMethods: LiteralMethods = {
144
144
  initializer = this.parseAssignmentExpression();
145
145
  end = initializer.end;
146
146
  }
147
- const shorthand: ShorthandPropertyAssignment = { kind: SyntaxKind.ShorthandPropertyAssignment, name, initializer, start, end };
147
+ const shorthand: ShorthandPropertyAssignment = { kind: SyntaxKind.ShorthandPropertyAssignment, name, propertyName: name.text, initializer, start, end };
148
148
  properties.push(shorthand);
149
149
  } else {
150
150
  this.error(DiagnosticCode.UnexpectedToken, "Invalid object literal member");