xbintsc 0.6.12 → 0.6.13

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 (115) 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/node/index.js +2 -0
  21. package/dist/src/extensions/node/index.js.map +1 -1
  22. package/dist/src/extensions/node/readline/index.d.ts +12 -0
  23. package/dist/src/extensions/node/readline/index.js +27 -0
  24. package/dist/src/extensions/node/readline/index.js.map +1 -0
  25. package/dist/src/parser/declarations/functions.js +6 -0
  26. package/dist/src/parser/declarations/functions.js.map +1 -1
  27. package/dist/src/parser/literals.js +1 -1
  28. package/dist/src/parser/literals.js.map +1 -1
  29. package/dist/tests/e2e/compile-modules.test.js +22 -0
  30. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  31. package/dist/tests/e2e/date-parse.test.js +17 -0
  32. package/dist/tests/e2e/date-parse.test.js.map +1 -1
  33. package/dist/tests/e2e/gui-browser.test.js +61 -0
  34. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  35. package/dist/tests/e2e/gui-css-coverage.test.js +39 -0
  36. package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
  37. package/dist/tests/e2e/gui-css-features.test.js +100 -0
  38. package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
  39. package/dist/tests/e2e/gui-dom.test.js +34 -0
  40. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  41. package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
  42. package/dist/tests/e2e/gui-errors.test.js +68 -0
  43. package/dist/tests/e2e/gui-errors.test.js.map +1 -0
  44. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +7 -1
  45. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -1
  46. package/dist/tests/e2e/harness.d.ts +4 -0
  47. package/dist/tests/e2e/harness.js +2 -0
  48. package/dist/tests/e2e/harness.js.map +1 -1
  49. package/dist/tests/e2e/readline.test.d.ts +7 -0
  50. package/dist/tests/e2e/readline.test.js +73 -0
  51. package/dist/tests/e2e/readline.test.js.map +1 -0
  52. package/doc/gui/api.md +15 -0
  53. package/doc/gui/dom.md +7 -2
  54. package/doc/gui/known-issues.md +2 -1
  55. package/doc/gui/layout.md +5 -1
  56. package/doc/implemented/node/readline.md +34 -0
  57. package/doc/implemented/node.md +1 -0
  58. package/doc/unimplemented/node/modules.md +2 -3
  59. package/doc/unimplemented/node.md +2 -2
  60. package/doc/zh-CN/gui/api.md +11 -0
  61. package/doc/zh-CN/gui/dom.md +6 -2
  62. package/doc/zh-CN/gui/known-issues.md +2 -1
  63. package/doc/zh-CN/gui/layout.md +4 -1
  64. package/doc/zh-CN/implemented/node/readline.md +33 -0
  65. package/doc/zh-CN/implemented/node.md +1 -0
  66. package/doc/zh-CN/unimplemented/node/modules.md +1 -2
  67. package/doc/zh-CN/unimplemented/node.md +2 -2
  68. package/package.json +7 -7
  69. package/runtime/ext_gui/browser_abort.cpp +2 -2
  70. package/runtime/ext_gui/browser_loop.cpp +1 -0
  71. package/runtime/ext_gui/browser_media.cpp +1 -1
  72. package/runtime/ext_gui/dom_api_canvas_image.cpp +2 -1
  73. package/runtime/ext_gui/dom_api_events.cpp +1 -1
  74. package/runtime/ext_gui/dom_api_internal.h +2 -0
  75. package/runtime/ext_gui/dom_api_node.cpp +18 -12
  76. package/runtime/ext_gui/dom_api_node_read.cpp +27 -0
  77. package/runtime/ext_gui/dom_api_proto.cpp +2 -0
  78. package/runtime/ext_gui/error_report.cpp +166 -0
  79. package/runtime/ext_gui/gui.cpp +16 -0
  80. package/runtime/ext_gui/gui_engine.h +13 -0
  81. package/runtime/ext_gui/gui_events.cpp +4 -1
  82. package/runtime/ext_gui/layout_inline.cpp +46 -14
  83. package/runtime/ext_gui/paint.cpp +22 -0
  84. package/runtime/ext_gui/script.cpp +1 -1
  85. package/runtime/ext_gui/style.cpp +1 -0
  86. package/runtime/ext_gui/style.h +13 -0
  87. package/runtime/ext_gui/style_print.cpp +99 -0
  88. package/runtime/ext_gui/style_text.cpp +79 -0
  89. package/runtime/ext_gui/webgl_texture.cpp +0 -8
  90. package/runtime/ext_gui/window.cpp +22 -8
  91. package/runtime/ext_gui/window_emit.cpp +1 -1
  92. package/runtime/ext_node/fs/parts/platform.inc +2 -0
  93. package/runtime/ext_node/readline/readline.c +572 -0
  94. package/runtime/rt.h +15 -0
  95. package/runtime/rt_internal.h +3 -2
  96. package/runtime/xt_io/exception.inc +29 -6
  97. package/runtime/xt_loop.c +36 -1
  98. package/runtime/xt_promise.c +26 -2
  99. package/runtime/xt_stdlib2/date.inc +2 -2
  100. package/runtime/xt_typed_array/elements.inc +0 -9
  101. package/runtime/xt_url.c +0 -6
  102. package/runtime/xt_url_object.c +1 -1
  103. package/src/ast/expressions.ts +6 -0
  104. package/src/checker/type-infer.ts +1 -1
  105. package/src/codegen/generator/calls/literals.ts +4 -1
  106. package/src/codegen/generator/expressions/assignment.ts +3 -1
  107. package/src/codegen/generator/expressions/operators.ts +2 -6
  108. package/src/codegen/generator/expressions/primary.ts +58 -6
  109. package/src/driver/bundler/commonjs.ts +1 -1
  110. package/src/driver/bundler/symbols.ts +6 -0
  111. package/src/driver/version.ts +1 -1
  112. package/src/extensions/node/index.ts +2 -0
  113. package/src/extensions/node/readline/index.ts +31 -0
  114. package/src/parser/declarations/functions.ts +6 -0
  115. package/src/parser/literals.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xbintsc",
3
- "version": "0.6.12",
3
+ "version": "0.6.13",
4
4
  "description": "xbintsc - a TypeScript binary compiler (TypeScript -> LLVM IR -> native binary)",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -72,12 +72,12 @@
72
72
  "vitest": "^2.1.8"
73
73
  },
74
74
  "optionalDependencies": {
75
- "@xbintsc/linux-x64": "0.6.12",
76
- "@xbintsc/linux-arm64": "0.6.12",
77
- "@xbintsc/darwin-x64": "0.6.12",
78
- "@xbintsc/darwin-arm64": "0.6.12",
79
- "@xbintsc/win32-x64": "0.6.12",
80
- "@xbintsc/win32-arm64": "0.6.12"
75
+ "@xbintsc/linux-x64": "0.6.13",
76
+ "@xbintsc/linux-arm64": "0.6.13",
77
+ "@xbintsc/darwin-x64": "0.6.13",
78
+ "@xbintsc/darwin-arm64": "0.6.13",
79
+ "@xbintsc/win32-x64": "0.6.13",
80
+ "@xbintsc/win32-arm64": "0.6.13"
81
81
  },
82
82
  "engines": {
83
83
  "node": ">=22"
@@ -58,14 +58,14 @@ void call_abort_listener(xt_value fn, xt_value signal) {
58
58
  xt_object_set(event, xt_string_from_cstr("defaultPrevented"), XT_FALSE);
59
59
  if (XT_IS_FUNCTION(fn)) {
60
60
  xt_value args[1] = {event};
61
- xt_call_with_this(fn, signal, 1, args);
61
+ xt_gui_invoke(fn, signal, 1, args);
62
62
  return;
63
63
  }
64
64
  if (XT_IS_OBJECT(fn)) {
65
65
  xt_value handler = xt_object_get_cstr(fn, "handleEvent");
66
66
  if (XT_IS_FUNCTION(handler)) {
67
67
  xt_value args[1] = {event};
68
- xt_call_with_this(handler, fn, 1, args);
68
+ xt_gui_invoke(handler, fn, 1, args);
69
69
  }
70
70
  }
71
71
  }
@@ -75,6 +75,7 @@ xt_value browser_tick(xt_value self, xt_value env, int32_t argc, xt_value *argv)
75
75
  if (record == NULL || !record->open) return XT_UNDEFINED;
76
76
  xt_gui_pump_events();
77
77
  xt_gui_render_window(record);
78
+ xt_promise_report_unhandled();
78
79
  if (record->open && !record->animation_frames.empty()) arm_tick();
79
80
  return XT_UNDEFINED;
80
81
  }
@@ -54,7 +54,7 @@ void remove_listener(xt_value self, xt_value fn) {
54
54
  void call_change_listener(xt_value fn, xt_value list, xt_value event) {
55
55
  if (!XT_IS_FUNCTION(fn)) return;
56
56
  xt_value args[1] = {event};
57
- xt_call_with_this(fn, list, 1, args);
57
+ xt_gui_invoke(fn, list, 1, args);
58
58
  }
59
59
 
60
60
  #define METHOD(name) xt_value name(xt_value self, xt_value env, int32_t argc, xt_value *argv)
@@ -114,7 +114,8 @@ xt_value blob_callback(xt_value self, xt_value env, int32_t argc, xt_value *argv
114
114
  xt_value fn = xt_closure_env(env, 0);
115
115
  xt_value blob = xt_closure_env(env, 1);
116
116
  xt_value args[1] = {blob};
117
- return xt_call_with_this(fn, XT_UNDEFINED, 1, args);
117
+ xt_gui_invoke(fn, XT_UNDEFINED, 1, args);
118
+ return XT_UNDEFINED;
118
119
  }
119
120
 
120
121
  } // namespace
@@ -117,7 +117,7 @@ static bool call_node_listeners(XtGuiWindow *win, xtgui::Node *node, const char
117
117
  xt_object_set(event, xt_string_from_cstr("__xt_gui_passive"),
118
118
  listener.passive ? XT_TRUE : XT_FALSE);
119
119
  xt_value args[1] = {event};
120
- xt_call_with_this(listener.fn, self, 1, args);
120
+ xt_gui_invoke(listener.fn, self, 1, args);
121
121
  xt_object_set(event, xt_string_from_cstr("__xt_gui_passive"), previousPassive);
122
122
  if (value_truthy(xt_object_get_cstr(event, "__stopImmediate"))) return true;
123
123
  if (value_truthy(xt_object_get_cstr(event, "__stop"))) return true;
@@ -176,6 +176,8 @@ METHOD(node_id_get);
176
176
  METHOD(node_id_set);
177
177
  METHOD(node_class_name_get);
178
178
  METHOD(node_class_name_set);
179
+ METHOD(node_html_for_get);
180
+ METHOD(node_html_for_set);
179
181
  METHOD(node_text_content_get);
180
182
  METHOD(node_text_content_set);
181
183
  METHOD(node_inner_html_get);
@@ -214,25 +214,31 @@ METHOD(node_get_bounding_rect) {
214
214
  (void)env;
215
215
  (void)argc;
216
216
  (void)argv;
217
+ /* Like the DOM, an element with no layout box (`display: none`, a detached
218
+ * node, or a stale handle) still returns a rectangle, all zeroes. Returning
219
+ * `undefined` made every hidden-state measurement in UI code fail. */
220
+ auto rectangle = [](float x, float y, float width, float height) {
221
+ xt_value object = xt_object_new();
222
+ xt_object_set(object, xt_string_from_cstr("x"), xt_number(x));
223
+ xt_object_set(object, xt_string_from_cstr("y"), xt_number(y));
224
+ xt_object_set(object, xt_string_from_cstr("width"), xt_number(width));
225
+ xt_object_set(object, xt_string_from_cstr("height"), xt_number(height));
226
+ xt_object_set(object, xt_string_from_cstr("top"), xt_number(y));
227
+ xt_object_set(object, xt_string_from_cstr("right"), xt_number(x + width));
228
+ xt_object_set(object, xt_string_from_cstr("bottom"), xt_number(y + height));
229
+ xt_object_set(object, xt_string_from_cstr("left"), xt_number(x));
230
+ return object;
231
+ };
217
232
  XtGuiWindow *win = nullptr;
218
233
  xtgui::Node *node = resolve_node(self, &win);
219
- if (node == nullptr || win == nullptr || win->document == nullptr) return XT_UNDEFINED;
234
+ if (node == nullptr || win == nullptr || win->document == nullptr) return rectangle(0, 0, 0, 0);
220
235
  const xtgui::LayoutBox *box = win->document->boxOf(node);
221
- if (box == nullptr) return XT_UNDEFINED;
236
+ if (box == nullptr) return rectangle(0, 0, 0, 0);
222
237
  /* Like the DOM: the rectangle of the *transformed* border box. The legacy
223
238
  * `top`/`right`/`bottom`/`left` members are the same rectangle in the other
224
239
  * spelling; libraries read both. */
225
240
  xtgui::LayoutRect rect = xtgui::xt_layout_transformed_bounds(box);
226
- xt_value object = xt_object_new();
227
- xt_object_set(object, xt_string_from_cstr("x"), xt_number(rect.x));
228
- xt_object_set(object, xt_string_from_cstr("y"), xt_number(rect.y));
229
- xt_object_set(object, xt_string_from_cstr("width"), xt_number(rect.width));
230
- xt_object_set(object, xt_string_from_cstr("height"), xt_number(rect.height));
231
- xt_object_set(object, xt_string_from_cstr("top"), xt_number(rect.y));
232
- xt_object_set(object, xt_string_from_cstr("right"), xt_number(rect.x + rect.width));
233
- xt_object_set(object, xt_string_from_cstr("bottom"), xt_number(rect.y + rect.height));
234
- xt_object_set(object, xt_string_from_cstr("left"), xt_number(rect.x));
235
- return object;
241
+ return rectangle(rect.x, rect.y, rect.width, rect.height);
236
242
  }
237
243
 
238
244
  METHOD(node_offset_width_get) {
@@ -102,6 +102,33 @@ METHOD(node_id_set) {
102
102
  return XT_UNDEFINED;
103
103
  }
104
104
 
105
+ /** `label.htmlFor` reflects the `for` attribute (`""` when absent). */
106
+ METHOD(node_html_for_get) {
107
+ (void)env;
108
+ (void)argc;
109
+ (void)argv;
110
+ xtgui::Node *node = resolve_node(self, nullptr);
111
+ if (node == nullptr || !node->isElement()) return xt_string_from_cstr("");
112
+ const std::string *value = node->attr("for");
113
+ return xt_string_from_cstr(value != nullptr ? value->c_str() : "");
114
+ }
115
+
116
+ METHOD(node_html_for_set) {
117
+ (void)env;
118
+ XtGuiWindow *win = nullptr;
119
+ xtgui::Node *node = resolve_node(self, &win);
120
+ if (node == nullptr || !node->isElement()) return XT_UNDEFINED;
121
+ std::string value = argc >= 1 ? to_string_value(xt_arg(argc, argv, 0)) : "";
122
+ /* HTML reflects an empty `htmlFor` as a missing attribute. */
123
+ if (value.empty()) {
124
+ remove_attribute_raw(node, "for");
125
+ mark_dirty(win);
126
+ } else {
127
+ set_attribute(win, node, "for", value);
128
+ }
129
+ return XT_UNDEFINED;
130
+ }
131
+
105
132
  METHOD(node_class_name_get) {
106
133
  (void)env;
107
134
  (void)argc;
@@ -32,6 +32,7 @@ xt_value node_proto() {
32
32
  define_getter(proto, "prefix", (void *)node_prefix_get);
33
33
  define_getter(proto, "id", (void *)node_id_get);
34
34
  define_getter(proto, "className", (void *)node_class_name_get);
35
+ define_getter(proto, "htmlFor", (void *)node_html_for_get);
35
36
  define_getter(proto, "textContent", (void *)node_text_content_get);
36
37
  define_getter(proto, "innerText", (void *)node_text_content_get);
37
38
  define_getter(proto, "innerHTML", (void *)node_inner_html_get);
@@ -92,6 +93,7 @@ xt_value node_proto() {
92
93
  /* Setters. */
93
94
  define_setter(proto, "id", (void *)node_id_set);
94
95
  define_setter(proto, "className", (void *)node_class_name_set);
96
+ define_setter(proto, "htmlFor", (void *)node_html_for_set);
95
97
  define_setter(proto, "textContent", (void *)node_text_content_set);
96
98
  define_setter(proto, "innerText", (void *)node_text_content_set);
97
99
  define_setter(proto, "innerHTML", (void *)node_inner_html_set);
@@ -0,0 +1,166 @@
1
+ /*
2
+ * xbintsc GUI engine — page-level error reporting.
3
+ *
4
+ * A callback the engine invokes (an element listener, a window listener, a
5
+ * timer, an animation frame, a media event) can throw. Without a boundary that
6
+ * exception reaches `xt_throw` with no `try` frame, prints to stderr and calls
7
+ * `exit(1)`; in a GUI program the only visible effect is the window vanishing.
8
+ * `xt_gui_invoke` runs such callbacks under a `try` frame and hands the value
9
+ * to `xt_gui_report_uncaught`, which mirrors the browser surfaces:
10
+ *
11
+ * - `window.onerror(message, source, line, column, error)`
12
+ * - `window.addEventListener("error", handler)` (an ErrorEvent-like object)
13
+ * - `window.onunhandledrejection` / the `unhandledrejection` event
14
+ *
15
+ * Reporting never terminates the program, so one bad handler cannot take the
16
+ * page (or the other listeners) down. A throw inside the reporter itself is
17
+ * only logged, which also breaks the recursion an error listener could cause.
18
+ */
19
+
20
+ #include "dom_api_internal.h"
21
+ #include "gui_engine.h"
22
+
23
+ #include <setjmp.h>
24
+ #include <cstdio>
25
+ #include <string>
26
+
27
+ namespace {
28
+
29
+ /** Set while a report (and the handlers it runs) is in progress. */
30
+ bool g_reporting = false;
31
+
32
+ std::string to_std_string(xt_value value) {
33
+ const char *data = xt_string_data(xt_to_string(value));
34
+ return data != nullptr ? std::string(data) : std::string();
35
+ }
36
+
37
+ std::string error_message(xt_value error) {
38
+ if (XT_IS_OBJECT(error)) {
39
+ xt_value message = xt_object_get_cstr(error, "message");
40
+ if (XT_IS_STRING(message)) {
41
+ const char *data = xt_string_data(message);
42
+ if (data != nullptr) return std::string(data);
43
+ }
44
+ }
45
+ return to_std_string(error);
46
+ }
47
+
48
+ xt_value set_field(xt_value object, const char *key, xt_value value) {
49
+ xt_object_set(object, xt_string_from_cstr(key), value);
50
+ return object;
51
+ }
52
+
53
+ /** Common Event fields; `event_proto` supplies `preventDefault()`. */
54
+ xt_value make_event(const char *type, XtGuiWindow *win) {
55
+ xt_value event = xt_object_new_with_proto(domapi::event_proto());
56
+ set_field(event, "type", xt_string_from_cstr(type));
57
+ set_field(event, "target", win->object);
58
+ set_field(event, "currentTarget", win->object);
59
+ set_field(event, "defaultPrevented", XT_FALSE);
60
+ set_field(event, "bubbles", XT_FALSE);
61
+ set_field(event, "cancelable", XT_TRUE);
62
+ set_field(event, "isTrusted", XT_TRUE);
63
+ set_field(event, "eventPhase", xt_number(2));
64
+ set_field(event, "timeStamp", xt_number(0));
65
+ return event;
66
+ }
67
+
68
+ void log_uncaught(const std::string &message) {
69
+ std::fprintf(stderr, "Uncaught %s\n", message.c_str());
70
+ }
71
+
72
+ } // namespace
73
+
74
+ extern "C" void xt_gui_install_error_handlers(void) {
75
+ xt_set_uncaught_exception_handler(xt_gui_report_uncaught);
76
+ xt_set_unhandled_rejection_handler(xt_gui_unhandled_rejection);
77
+ }
78
+
79
+ extern "C" void xt_gui_report_uncaught(xt_value error) {
80
+ std::string message = error_message(error);
81
+ if (g_reporting) {
82
+ log_uncaught(message);
83
+ return;
84
+ }
85
+ XtGuiWindow *win = xt_gui_primary_window();
86
+ if (win == nullptr) {
87
+ log_uncaught(message);
88
+ return;
89
+ }
90
+ g_reporting = true;
91
+
92
+ xt_value event = make_event("error", win);
93
+ set_field(event, "message", xt_string_from_cstr(message.c_str()));
94
+ set_field(event, "filename", xt_string_from_cstr(""));
95
+ set_field(event, "lineno", xt_number(0));
96
+ set_field(event, "colno", xt_number(0));
97
+ set_field(event, "error", error);
98
+
99
+ /* The classic `onerror` signature, then every `error` listener. Either can
100
+ * call the event's `preventDefault()` (or `onerror` can return true) to
101
+ * suppress the default log line. */
102
+ int prevented = 0;
103
+ xt_value handler = xt_object_get_cstr(win->object, "onerror");
104
+ if (XT_IS_FUNCTION(handler)) {
105
+ xt_value args[5] = {
106
+ xt_string_from_cstr(message.c_str()),
107
+ xt_string_from_cstr(""),
108
+ xt_number(0),
109
+ xt_number(0),
110
+ error,
111
+ };
112
+ prevented = xt_truthy(xt_call_with_this(handler, win->object, 5, args));
113
+ }
114
+ xt_gui_emit_payload(win, "error", event);
115
+ if (xt_truthy(xt_object_get_cstr(event, "defaultPrevented"))) prevented = 1;
116
+ if (!prevented) log_uncaught(message);
117
+
118
+ g_reporting = false;
119
+ }
120
+
121
+ extern "C" int xt_gui_unhandled_rejection(xt_value reason, xt_value promise) {
122
+ XtGuiWindow *win = xt_gui_primary_window();
123
+ if (win == nullptr) return 0;
124
+ std::string message = error_message(reason);
125
+ if (g_reporting) {
126
+ std::fprintf(stderr, "Unhandled promise rejection\n%s\n", message.c_str());
127
+ return 1;
128
+ }
129
+ g_reporting = true;
130
+
131
+ xt_value event = make_event("unhandledrejection", win);
132
+ set_field(event, "reason", reason);
133
+ set_field(event, "promise", promise);
134
+ set_field(event, "message", xt_string_from_cstr(message.c_str()));
135
+
136
+ xt_value handler = xt_object_get_cstr(win->object, "onunhandledrejection");
137
+ if (XT_IS_FUNCTION(handler)) {
138
+ xt_value args[1] = {event};
139
+ xt_call_with_this(handler, win->object, 1, args);
140
+ }
141
+ xt_gui_emit_payload(win, "unhandledrejection", event);
142
+ if (!xt_truthy(xt_object_get_cstr(event, "defaultPrevented"))) {
143
+ std::fprintf(stderr, "Unhandled promise rejection\n%s\n", message.c_str());
144
+ }
145
+
146
+ g_reporting = false;
147
+ /* The page owns the rejection now; never turn it into a fatal process exit. */
148
+ return 1;
149
+ }
150
+
151
+ extern "C" int xt_gui_invoke(xt_value fn, xt_value thisValue, int32_t argc, xt_value *argv) {
152
+ if (!XT_IS_FUNCTION(fn)) return 1;
153
+ void *frame = xt_try_enter();
154
+ /* `xt_try_frame` starts with the setjmp buffer; the longjmp in `xt_throw`
155
+ * returns here and the C++ frames between the callback and this boundary
156
+ * hold no destructible state. */
157
+ if (_setjmp(*(jmp_buf *)frame) == 0) {
158
+ xt_call_with_this(fn, thisValue, argc, argv);
159
+ xt_try_leave(frame);
160
+ return 1;
161
+ }
162
+ xt_value error = xt_try_exception(frame);
163
+ xt_try_leave(frame);
164
+ xt_gui_report_uncaught(error);
165
+ return 0;
166
+ }
@@ -122,6 +122,7 @@ static void ensure_init(void) {
122
122
  /* Registered only once a window can exist: a failed init has nothing to
123
123
  * mark, and a caller that cannot retry never needs a second provider. */
124
124
  xt_gc_register_root_provider(xt_gui_gc_scan_roots);
125
+ xt_gui_install_error_handlers();
125
126
  g_sdl_initialized = 1;
126
127
  }
127
128
 
@@ -149,6 +150,13 @@ XtGuiWindow *xt_gui_window_from_this(xt_value thisValue) {
149
150
  return window_by_object(thisValue);
150
151
  }
151
152
 
153
+ XtGuiWindow *xt_gui_primary_window(void) {
154
+ for (int i = g_window_count - 1; i >= 0; i--) {
155
+ if (g_windows[i].open) return &g_windows[i];
156
+ }
157
+ return NULL;
158
+ }
159
+
152
160
  void xt_gui_quit_window(XtGuiWindow *win) {
153
161
  if (win == NULL || !win->open) return;
154
162
  win->open = 0;
@@ -253,6 +261,11 @@ static XtGuiWindow *create_window_record(const char *title, int width, int heigh
253
261
  xt_object_set(object, xt_string_from_cstr("__xt_gui_index"), xt_number((double)index));
254
262
  xt_object_set(object, xt_string_from_cstr("__xt_gui_html"), xt_string_from_cstr(""));
255
263
  xt_object_set(object, xt_string_from_cstr("__xt_gui_events"), xt_object_new());
264
+ /* Present-but-null handler properties, like a browser window: a feature
265
+ * probe gets "object" instead of "undefined", and assigning a function is a
266
+ * plain property write. */
267
+ xt_object_set(object, xt_string_from_cstr("onerror"), XT_NULL);
268
+ xt_object_set(object, xt_string_from_cstr("onunhandledrejection"), XT_NULL);
256
269
  /* The engine owns a graph of values on the window's behalf — element
257
270
  * handles (`node_handles`), their event listeners and the queued animation
258
271
  * frame closures. Most of them are referenced from TypeScript too, but not
@@ -413,6 +426,9 @@ extern "C" xt_value xt_gui_run(int32_t argc, xt_value *argv) {
413
426
  * frames. `xt_loop_poll(0)` is non-blocking. */
414
427
  xt_loop_poll(0);
415
428
  xt_drain_microtasks();
429
+ /* Rejections nobody observed become window `unhandledrejection` events;
430
+ * the installed handler suppresses the process-killing default. */
431
+ xt_promise_report_unhandled();
416
432
 
417
433
  if (deadline != 0 && SDL_GetTicks() >= deadline) break;
418
434
  if (!had_event) SDL_Delay(1);
@@ -70,6 +70,14 @@ void xt_browser_schedule_frame(void);
70
70
  /** Whether `run()` currently owns the frame loop (the pump then stays idle). */
71
71
  int xt_gui_run_active(void);
72
72
 
73
+ /* Page-level error reporting (error_report.cpp). `xt_gui_invoke` runs one
74
+ * host-invoked callback under a `try` boundary so a throw reaches the window's
75
+ * `error`/`unhandledrejection` surfaces instead of terminating the process. */
76
+ void xt_gui_install_error_handlers(void);
77
+ void xt_gui_report_uncaught(xt_value error);
78
+ int xt_gui_unhandled_rejection(xt_value reason, xt_value promise);
79
+ int xt_gui_invoke(xt_value fn, xt_value thisValue, int32_t argc, xt_value *argv);
80
+
73
81
  /* Web Audio objects (`audio.cpp`).
74
82
  * `AudioContext.prototype` — also the `instanceof AudioContext` target. */
75
83
  xt_value xt_gui_audio_proto(void);
@@ -244,6 +252,11 @@ struct XtGuiWindow {
244
252
  /** Resolve a window handle (`this`) to its record, or NULL for a foreign value. */
245
253
  XtGuiWindow *xt_gui_window_from_this(xt_value thisValue);
246
254
 
255
+ /** The most recently created live window, or NULL when none is open. Error
256
+ * reporting uses it because a throw can escape a callback with no window
257
+ * argument. */
258
+ XtGuiWindow *xt_gui_primary_window(void);
259
+
247
260
 
248
261
  /** The `document` handle for `win` (created on first use). */
249
262
  xt_value xt_gui_document_handle(XtGuiWindow *win);
@@ -304,6 +304,9 @@ void handle_event(const SDL_Event *event) {
304
304
  if (event->type == SDL_EVENT_WINDOW_RESIZED) {
305
305
  XtGuiWindow *win = xt_gui_find_by_window_id(event->window.windowID);
306
306
  if (win == NULL || !win->open) return;
307
+ /* `setSize` already applied and emitted the change synchronously; the
308
+ * queued backend event must not deliver the same resize twice. */
309
+ if (win->width == event->window.data1 && win->height == event->window.data2) return;
307
310
  win->width = event->window.data1;
308
311
  win->height = event->window.data2;
309
312
  if (win->document != NULL) win->document->restyle((float)win->width, (float)win->height);
@@ -401,7 +404,7 @@ void xt_gui_run_animation_frames(XtGuiWindow *win, double timestamp_ms) {
401
404
  for (const XtGuiAnimationFrame &frame : frames) {
402
405
  if (!win->open) break;
403
406
  xt_value arg = xt_number(timestamp_ms);
404
- xt_call_with_this(frame.fn, win->object, 1, &arg);
407
+ xt_gui_invoke(frame.fn, win->object, 1, &arg);
405
408
  }
406
409
  frames.clear();
407
410
  }
@@ -67,7 +67,10 @@ void Layouter::flattenInline(LayoutBox *box, float contentWidth,
67
67
  float available = contentWidth;
68
68
  float intrinsic = preferredContentWidth(box, available);
69
69
  setContainingBlock(box, containingBlockAncestor(box));
70
- layoutBlock(box, 0, 0, available, -1, intrinsic);
70
+ /* An explicit width must go through the normal used-width path so
71
+ * `box-sizing: border-box` can subtract padding and borders. Only an `auto`
72
+ * width needs the shrink-to-fit content width forced here. */
73
+ layoutBlock(box, 0, 0, available, -1, isAuto(style.width) ? intrinsic : -1.0f);
71
74
  InlineItem item;
72
75
  item.box = box;
73
76
  item.style = &style;
@@ -274,24 +277,53 @@ float Layouter::layoutInlineRun(LayoutBox *container, const std::vector<LayoutBo
274
277
  lineDescent = 0;
275
278
  };
276
279
 
277
- for (InlineItem &item : items) {
280
+ for (size_t index = 0; index < items.size(); index++) {
281
+ InlineItem &item = items[index];
278
282
  if (item.force_break) {
279
283
  finalize();
280
284
  continue;
281
285
  }
282
286
  if (item.is_space && line.empty()) continue; // no leading space on a line
283
- /* Break against the band the line would occupy, then place it. */
284
- float pendingHeight = std::max(lineAscent + lineDescent, item.height);
285
- float x0 = 0;
286
- float x1 = 0;
287
- band(cursorY, pendingHeight, &x0, &x1);
288
- float available = x1 - x0;
289
- if (wrap && lineWidth + item.width > available && !line.empty()) finalize();
290
- if (item.is_space && line.empty()) continue;
291
- line.push_back(&item);
292
- lineWidth += item.width;
293
- lineAscent = std::max(lineAscent, item.ascent);
294
- lineDescent = std::max(lineDescent, item.height - item.ascent);
287
+ /* A `break-word`/`anywhere` word that does not fit is split at the
288
+ * largest prefix the remaining line can hold; the remainder is processed
289
+ * again on the next line. Without this a long token overflowed the box. */
290
+ for (;;) {
291
+ /* Break against the band the line would occupy, then place it. */
292
+ float pendingHeight = std::max(lineAscent + lineDescent, item.height);
293
+ float x0 = 0;
294
+ float x1 = 0;
295
+ band(cursorY, pendingHeight, &x0, &x1);
296
+ float available = x1 - x0;
297
+ bool overflows = lineWidth + item.width > available + 0.001f;
298
+ if (wrap && overflows) {
299
+ bool breakWord = item.style != nullptr &&
300
+ item.style->overflow_wrap != OverflowWrap::Normal;
301
+ if (breakWord && !item.atomic && !item.is_space && item.text.size() > 1) {
302
+ float room = available - lineWidth;
303
+ size_t prefix = fittingPrefix(item.text, *item.style, room);
304
+ if (prefix > 0 && prefix < item.text.size()) {
305
+ InlineItem head = item;
306
+ head.text = item.text.substr(0, prefix);
307
+ head.width = textWidth(head.text, *item.style);
308
+ line.push_back(&head);
309
+ lineWidth += head.width;
310
+ lineAscent = std::max(lineAscent, head.ascent);
311
+ lineDescent = std::max(lineDescent, head.height - head.ascent);
312
+ finalize();
313
+ item.text = item.text.substr(prefix);
314
+ item.width = textWidth(item.text, *item.style);
315
+ continue;
316
+ }
317
+ }
318
+ if (!line.empty()) finalize();
319
+ }
320
+ if (item.is_space && line.empty()) break;
321
+ line.push_back(&item);
322
+ lineWidth += item.width;
323
+ lineAscent = std::max(lineAscent, item.ascent);
324
+ lineDescent = std::max(lineDescent, item.height - item.ascent);
325
+ break;
326
+ }
295
327
  }
296
328
  finalize();
297
329
 
@@ -173,6 +173,28 @@ void addTextRuns(const LayoutBox *box, const XtStyle &style, DisplayList &out,
173
173
  paint_detail::addTextRun(out, fragment.x, fragment.y + ascent, fragment.width,
174
174
  fragment.height, fragment.text, spec, base, box->world, clip, style,
175
175
  chain, viewportWidth, viewportHeight);
176
+ if (style.text_decoration_line != 0) {
177
+ /* The decoration uses the element's text colour unless the shorthand
178
+ * named one. Dashed/dotted/wavy are currently painted solid; the style
179
+ * is still retained for `getComputedStyle`. */
180
+ Color rule = paint_detail::filterColor(
181
+ chain, style.has_text_decoration_color ? style.text_decoration_color : style.color);
182
+ float thickness = std::max(1.0f, style.font_size / 14.0f);
183
+ float baseline = fragment.y + ascent;
184
+ if (style.text_decoration_line & (uint8_t)TextDecorationLine::Underline) {
185
+ paint_detail::addRect(out, fragment.x, baseline + thickness, fragment.width, thickness,
186
+ 0.0f, rule, clip, box->world);
187
+ }
188
+ if (style.text_decoration_line & (uint8_t)TextDecorationLine::Overline) {
189
+ paint_detail::addRect(out, fragment.x, fragment.y, fragment.width, thickness, 0.0f, rule,
190
+ clip, box->world);
191
+ }
192
+ if (style.text_decoration_line & (uint8_t)TextDecorationLine::LineThrough) {
193
+ paint_detail::addRect(out, fragment.x,
194
+ fragment.y + fragment.height * 0.55f - thickness / 2.0f,
195
+ fragment.width, thickness, 0.0f, rule, clip, box->world);
196
+ }
197
+ }
176
198
  continue;
177
199
  }
178
200
  if (fragment.upright) {
@@ -99,6 +99,6 @@ void xt_gui_run_scripts(XtGuiWindow *win) {
99
99
  auto it = script_registry().find(id);
100
100
  if (it == script_registry().end()) continue;
101
101
  xt_value args[2] = {window_handle, document_handle};
102
- xt_call_with_this(it->second, XT_UNDEFINED, 2, args);
102
+ xt_gui_invoke(it->second, XT_UNDEFINED, 2, args);
103
103
  }
104
104
  }
@@ -187,6 +187,7 @@ void inheritFrom(XtStyle *style, const XtStyle *parent) {
187
187
  style->white_space = parent->white_space;
188
188
  style->text_shadow = parent->text_shadow;
189
189
  style->text_transform = parent->text_transform;
190
+ style->overflow_wrap = parent->overflow_wrap;
190
191
  style->letter_spacing = parent->letter_spacing;
191
192
  style->font_variant_numeric = parent->font_variant_numeric;
192
193
  style->writing_mode = parent->writing_mode;
@@ -10,6 +10,7 @@
10
10
  #ifndef XT_GUI_STYLE_H
11
11
  #define XT_GUI_STYLE_H
12
12
 
13
+ #include <cstdint>
13
14
  #include <map>
14
15
  #include <memory>
15
16
  #include <string>
@@ -37,6 +38,11 @@ enum class TextAlign { Start, Center, End, Justify };
37
38
  enum class TextOverflow { Clip, Ellipsis };
38
39
  enum class WhiteSpace { Normal, NoWrap, Pre, PreWrap, PreLine };
39
40
  enum class Overflow { Visible, Hidden, Scroll, Auto };
41
+ /** `text-decoration-line` is a bit set; `None` is zero. */
42
+ enum class TextDecorationLine : uint8_t { None = 0, Underline = 1, Overline = 2, LineThrough = 4 };
43
+ enum class TextDecorationStyle { Solid, Double, Dotted, Dashed, Wavy };
44
+ /** `overflow-wrap` (and its legacy `word-wrap` spelling). */
45
+ enum class OverflowWrap { Normal, BreakWord, Anywhere };
40
46
  enum class BorderStyle { None, Solid, Dashed, Dotted };
41
47
  enum class FontStyle { Normal, Italic };
42
48
 
@@ -311,6 +317,13 @@ struct XtStyle {
311
317
  TextOverflow text_overflow = TextOverflow::Clip;
312
318
  WhiteSpace white_space = WhiteSpace::Normal; // inherited
313
319
  TextTransform text_transform = TextTransform::None; // inherited
320
+ /** `text-decoration` shorthand (line/style/color); not inherited, but it
321
+ * propagates visually to in-flow descendants. */
322
+ uint8_t text_decoration_line = 0;
323
+ TextDecorationStyle text_decoration_style = TextDecorationStyle::Solid;
324
+ Color text_decoration_color{0.0f, 0.0f, 0.0f, 1.0f};
325
+ bool has_text_decoration_color = false;
326
+ OverflowWrap overflow_wrap = OverflowWrap::Normal; // inherited
314
327
  /** Resolved to pixels in the cascade (em is the element's own font size). */
315
328
  Length letter_spacing{0.0f, Unit::Px}; // inherited
316
329
  bool font_variant_numeric = false; // inherited; accepted, no OpenType features