xbintsc 0.6.10 → 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 (204) hide show
  1. package/dist/src/ast/expressions.d.ts +6 -0
  2. package/dist/src/checker/globals.js +2 -1
  3. package/dist/src/checker/globals.js.map +1 -1
  4. package/dist/src/checker/type-infer.js +1 -1
  5. package/dist/src/checker/type-infer.js.map +1 -1
  6. package/dist/src/cli/help.d.ts +1 -1
  7. package/dist/src/codegen/generator/calls/literals.js +4 -1
  8. package/dist/src/codegen/generator/calls/literals.js.map +1 -1
  9. package/dist/src/codegen/generator/expressions/assignment.js +3 -1
  10. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  11. package/dist/src/codegen/generator/expressions/operators.js +2 -6
  12. package/dist/src/codegen/generator/expressions/operators.js.map +1 -1
  13. package/dist/src/codegen/generator/expressions/primary.d.ts +9 -0
  14. package/dist/src/codegen/generator/expressions/primary.js +44 -7
  15. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  16. package/dist/src/codegen/generator/state.js +3 -0
  17. package/dist/src/codegen/generator/state.js.map +1 -1
  18. package/dist/src/codegen/generator/tables.js +3 -0
  19. package/dist/src/codegen/generator/tables.js.map +1 -1
  20. package/dist/src/driver/bundler/commonjs.js +1 -1
  21. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  22. package/dist/src/driver/bundler/symbols.js +6 -0
  23. package/dist/src/driver/bundler/symbols.js.map +1 -1
  24. package/dist/src/driver/compiler.js +1 -1
  25. package/dist/src/driver/compiler.js.map +1 -1
  26. package/dist/src/driver/runtime-objects.d.ts +1 -1
  27. package/dist/src/driver/runtime-objects.js +2 -0
  28. package/dist/src/driver/runtime-objects.js.map +1 -1
  29. package/dist/src/driver/version.d.ts +1 -1
  30. package/dist/src/driver/version.js +1 -1
  31. package/dist/src/extensions/node/crypto/index.js +12 -1
  32. package/dist/src/extensions/node/crypto/index.js.map +1 -1
  33. package/dist/src/extensions/node/index.js +2 -0
  34. package/dist/src/extensions/node/index.js.map +1 -1
  35. package/dist/src/extensions/node/readline/index.d.ts +12 -0
  36. package/dist/src/extensions/node/readline/index.js +27 -0
  37. package/dist/src/extensions/node/readline/index.js.map +1 -0
  38. package/dist/src/parser/declarations/functions.js +6 -0
  39. package/dist/src/parser/declarations/functions.js.map +1 -1
  40. package/dist/src/parser/literals.js +1 -1
  41. package/dist/src/parser/literals.js.map +1 -1
  42. package/dist/tests/e2e/compile-builtins.test.js +26 -0
  43. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  44. package/dist/tests/e2e/compile-modules.test.js +22 -0
  45. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  46. package/dist/tests/e2e/compile-node.test.js +37 -1
  47. package/dist/tests/e2e/compile-node.test.js.map +1 -1
  48. package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
  49. package/dist/tests/e2e/crypto-random.test.js +63 -0
  50. package/dist/tests/e2e/crypto-random.test.js.map +1 -0
  51. package/dist/tests/e2e/date-parse.test.js +44 -1
  52. package/dist/tests/e2e/date-parse.test.js.map +1 -1
  53. package/dist/tests/e2e/gui-browser.test.js +61 -0
  54. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  55. package/dist/tests/e2e/gui-css-coverage.test.js +40 -1
  56. package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
  57. package/dist/tests/e2e/gui-css-features.test.js +100 -0
  58. package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
  59. package/dist/tests/e2e/gui-dom.test.js +63 -0
  60. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  61. package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
  62. package/dist/tests/e2e/gui-errors.test.js +68 -0
  63. package/dist/tests/e2e/gui-errors.test.js.map +1 -0
  64. package/dist/tests/e2e/gui-form.test.d.ts +9 -0
  65. package/dist/tests/e2e/gui-form.test.js +359 -0
  66. package/dist/tests/e2e/gui-form.test.js.map +1 -0
  67. package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
  68. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  69. package/dist/tests/e2e/gui-style.test.js +82 -0
  70. package/dist/tests/e2e/gui-style.test.js.map +1 -1
  71. package/dist/tests/e2e/gui-unsupported.test.js +4 -3
  72. package/dist/tests/e2e/gui-unsupported.test.js.map +1 -1
  73. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +7 -1
  74. package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -1
  75. package/dist/tests/e2e/harness.d.ts +4 -0
  76. package/dist/tests/e2e/harness.js +2 -0
  77. package/dist/tests/e2e/harness.js.map +1 -1
  78. package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
  79. package/dist/tests/e2e/json-parse-errors.test.js +48 -0
  80. package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
  81. package/dist/tests/e2e/readline.test.d.ts +7 -0
  82. package/dist/tests/e2e/readline.test.js +73 -0
  83. package/dist/tests/e2e/readline.test.js.map +1 -0
  84. package/doc/ai/language-support.md +2 -1
  85. package/doc/ai/zh-CN/language-support.md +2 -1
  86. package/doc/gui/api.md +24 -3
  87. package/doc/gui/dom.md +28 -14
  88. package/doc/gui/input.md +74 -3
  89. package/doc/gui/known-issues.md +2 -1
  90. package/doc/gui/layout.md +5 -1
  91. package/doc/implemented/node/crypto.md +8 -0
  92. package/doc/implemented/node/fs.md +1 -1
  93. package/doc/implemented/node/readline.md +34 -0
  94. package/doc/implemented/node.md +1 -0
  95. package/doc/implemented/stdlib.md +10 -2
  96. package/doc/unimplemented/node/modules.md +2 -3
  97. package/doc/unimplemented/node.md +2 -2
  98. package/doc/zh-CN/gui/api.md +20 -3
  99. package/doc/zh-CN/gui/dom.md +23 -13
  100. package/doc/zh-CN/gui/input.md +65 -4
  101. package/doc/zh-CN/gui/known-issues.md +2 -1
  102. package/doc/zh-CN/gui/layout.md +4 -1
  103. package/doc/zh-CN/implemented/node/crypto.md +6 -0
  104. package/doc/zh-CN/implemented/node/fs.md +1 -1
  105. package/doc/zh-CN/implemented/node/readline.md +33 -0
  106. package/doc/zh-CN/implemented/node.md +1 -0
  107. package/doc/zh-CN/implemented/stdlib.md +10 -2
  108. package/doc/zh-CN/unimplemented/node/modules.md +1 -2
  109. package/doc/zh-CN/unimplemented/node.md +2 -2
  110. package/package.json +7 -7
  111. package/runtime/ext_gui/browser_abort.cpp +2 -2
  112. package/runtime/ext_gui/browser_loop.cpp +1 -0
  113. package/runtime/ext_gui/browser_media.cpp +1 -1
  114. package/runtime/ext_gui/css_match.cpp +21 -1
  115. package/runtime/ext_gui/css_selector.cpp +2 -2
  116. package/runtime/ext_gui/document.cpp +12 -0
  117. package/runtime/ext_gui/dom.h +41 -2
  118. package/runtime/ext_gui/dom_api_canvas_image.cpp +2 -1
  119. package/runtime/ext_gui/dom_api_document.cpp +28 -0
  120. package/runtime/ext_gui/dom_api_events.cpp +18 -15
  121. package/runtime/ext_gui/dom_api_form.cpp +567 -0
  122. package/runtime/ext_gui/dom_api_internal.h +48 -214
  123. package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
  124. package/runtime/ext_gui/dom_api_node.cpp +28 -15
  125. package/runtime/ext_gui/dom_api_node_read.cpp +28 -1
  126. package/runtime/ext_gui/dom_api_proto.cpp +47 -0
  127. package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
  128. package/runtime/ext_gui/dom_parse.cpp +114 -1
  129. package/runtime/ext_gui/error_report.cpp +166 -0
  130. package/runtime/ext_gui/form_focus.cpp +293 -0
  131. package/runtime/ext_gui/form_input.cpp +448 -0
  132. package/runtime/ext_gui/form_internal.h +30 -0
  133. package/runtime/ext_gui/form_shared.cpp +126 -0
  134. package/runtime/ext_gui/form_state.cpp +536 -0
  135. package/runtime/ext_gui/form_state.h +97 -0
  136. package/runtime/ext_gui/gui.cpp +16 -0
  137. package/runtime/ext_gui/gui_engine.h +53 -4
  138. package/runtime/ext_gui/gui_events.cpp +67 -49
  139. package/runtime/ext_gui/layout.cpp +6 -0
  140. package/runtime/ext_gui/layout.h +4 -0
  141. package/runtime/ext_gui/layout_flow.cpp +23 -1
  142. package/runtime/ext_gui/layout_inline.cpp +46 -14
  143. package/runtime/ext_gui/paint.cpp +26 -1
  144. package/runtime/ext_gui/paint_form.cpp +389 -0
  145. package/runtime/ext_gui/paint_internal.h +6 -0
  146. package/runtime/ext_gui/script.cpp +1 -1
  147. package/runtime/ext_gui/style.cpp +1 -0
  148. package/runtime/ext_gui/style.h +13 -0
  149. package/runtime/ext_gui/style_print.cpp +129 -0
  150. package/runtime/ext_gui/style_text.cpp +79 -0
  151. package/runtime/ext_gui/style_ua.cpp +42 -0
  152. package/runtime/ext_gui/warnings.cpp +5 -7
  153. package/runtime/ext_gui/webgl_texture.cpp +0 -8
  154. package/runtime/ext_gui/window.cpp +52 -62
  155. package/runtime/ext_gui/window_diagnostics.cpp +58 -0
  156. package/runtime/ext_gui/window_emit.cpp +51 -0
  157. package/runtime/ext_node/crypto/crypto.c +3 -2
  158. package/runtime/ext_node/crypto/crypto_hash.h +13 -0
  159. package/runtime/ext_node/crypto/crypto_random.c +339 -0
  160. package/runtime/ext_node/fs/async_worker.c +1 -1
  161. package/runtime/ext_node/fs/fs_ops.c +1 -1
  162. package/runtime/ext_node/fs/parts/platform.inc +67 -0
  163. package/runtime/ext_node/readline/readline.c +572 -0
  164. package/runtime/rt.h +42 -0
  165. package/runtime/rt_internal.h +3 -2
  166. package/runtime/xt_crypto.c +132 -0
  167. package/runtime/xt_io/exception.inc +29 -6
  168. package/runtime/xt_loop.c +36 -1
  169. package/runtime/xt_promise.c +26 -2
  170. package/runtime/xt_random.c +143 -0
  171. package/runtime/xt_regex/matcher.inc +9 -28
  172. package/runtime/xt_regex/parser.inc +65 -2
  173. package/runtime/xt_regex/types.inc +11 -1
  174. package/runtime/xt_regex.h +6 -2
  175. package/runtime/xt_stdlib/dispatch.inc +2 -0
  176. package/runtime/xt_stdlib/math.inc +1 -1
  177. package/runtime/xt_stdlib2/date.inc +206 -6
  178. package/runtime/xt_stdlib2/json.inc +4 -546
  179. package/runtime/xt_stdlib2/json_parse.inc +347 -0
  180. package/runtime/xt_stdlib2/json_stringify.inc +368 -0
  181. package/runtime/xt_stdlib2/regexp-match.inc +6 -11
  182. package/runtime/xt_stdlib2.c +0 -4
  183. package/runtime/xt_typed_array/elements.inc +2 -6
  184. package/runtime/xt_url.c +0 -6
  185. package/runtime/xt_url_object.c +1 -1
  186. package/src/ast/expressions.ts +6 -0
  187. package/src/checker/globals.ts +2 -0
  188. package/src/checker/type-infer.ts +1 -1
  189. package/src/codegen/generator/calls/literals.ts +4 -1
  190. package/src/codegen/generator/expressions/assignment.ts +3 -1
  191. package/src/codegen/generator/expressions/operators.ts +2 -6
  192. package/src/codegen/generator/expressions/primary.ts +58 -6
  193. package/src/codegen/generator/state.ts +3 -0
  194. package/src/codegen/generator/tables.ts +3 -0
  195. package/src/driver/bundler/commonjs.ts +1 -1
  196. package/src/driver/bundler/symbols.ts +6 -0
  197. package/src/driver/compiler.ts +1 -1
  198. package/src/driver/runtime-objects.ts +2 -0
  199. package/src/driver/version.ts +1 -1
  200. package/src/extensions/node/crypto/index.ts +12 -1
  201. package/src/extensions/node/index.ts +2 -0
  202. package/src/extensions/node/readline/index.ts +31 -0
  203. package/src/parser/declarations/functions.ts +6 -0
  204. package/src/parser/literals.ts +1 -1
@@ -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);
@@ -149,9 +149,172 @@ xt_value xt_date_static(xt_value name, int32_t argc, xt_value *argv) {
149
149
  return XT_UNDEFINED;
150
150
  }
151
151
 
152
+ static const char *const xt_date_month_long[12] = {
153
+ "January", "February", "March", "April", "May", "June",
154
+ "July", "August", "September", "October", "November", "December",
155
+ };
156
+ static const char *const xt_date_month_short[12] = {
157
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
158
+ };
159
+ static const char *const xt_date_weekday_long[7] = {
160
+ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
161
+ };
162
+ static const char *const xt_date_weekday_short[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
163
+
164
+ static void xt_date_append(char *out, size_t capacity, size_t *length, const char *text) {
165
+ if (text == NULL) return;
166
+ size_t count = strlen(text);
167
+ if (*length + count + 1 > capacity) count = capacity > *length + 1 ? capacity - *length - 1 : 0;
168
+ memcpy(out + *length, text, count);
169
+ *length += count;
170
+ out[*length] = '\0';
171
+ }
172
+
173
+ static void xt_date_append_int(char *out, size_t capacity, size_t *length, int value, int width) {
174
+ char buffer[24];
175
+ if (width == 2) {
176
+ snprintf(buffer, sizeof(buffer), "%02d", value);
177
+ } else {
178
+ snprintf(buffer, sizeof(buffer), "%d", value);
179
+ }
180
+ xt_date_append(out, capacity, length, buffer);
181
+ }
182
+
183
+ /** Read a string option (`{ month: "long" }`); null when absent. */
184
+ static const char *xt_date_option(xt_value options, const char *name) {
185
+ if (!XT_IS_OBJECT(options)) return NULL;
186
+ xt_value value = xt_object_get_cstr(options, name);
187
+ if (XT_IS_UNDEFINED(value) || XT_IS_NULL(value)) return NULL;
188
+ return xt_string_data(xt_to_string(value));
189
+ }
190
+
191
+ static int xt_date_option_width(const char *value) {
192
+ return value != NULL && strcmp(value, "2-digit") == 0 ? 2 : 1;
193
+ }
194
+
195
+ static int xt_date_has_option(xt_value options, const char *const *names, int count) {
196
+ for (int i = 0; i < count; i++) {
197
+ if (xt_date_option(options, names[i]) != NULL) return 1;
198
+ }
199
+ return 0;
200
+ }
201
+
202
+ /** `toLocaleDateString` with a small, deterministic en-US-style subset of the
203
+ * options object. No locale database is linked, so unknown locales still use
204
+ * the same month/day order. */
205
+ static xt_value xt_date_locale_date(const struct tm *tm, xt_value options) {
206
+ static const char *const names[] = {"weekday", "year", "month", "day"};
207
+ int custom = xt_date_has_option(options, names, 4);
208
+ char buffer[128];
209
+ size_t length = 0;
210
+ buffer[0] = '\0';
211
+ if (!custom) {
212
+ xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_mon + 1, 1);
213
+ xt_date_append(buffer, sizeof(buffer), &length, "/");
214
+ xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_mday, 1);
215
+ xt_date_append(buffer, sizeof(buffer), &length, "/");
216
+ xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_year + 1900, 4);
217
+ return xt_string_from_cstr(buffer);
218
+ }
219
+ const char *weekday = xt_date_option(options, "weekday");
220
+ if (weekday != NULL) {
221
+ if (strcmp(weekday, "long") == 0) xt_date_append(buffer, sizeof(buffer), &length,
222
+ xt_date_weekday_long[tm->tm_wday]);
223
+ else xt_date_append(buffer, sizeof(buffer), &length, xt_date_weekday_short[tm->tm_wday]);
224
+ xt_date_append(buffer, sizeof(buffer), &length, ", ");
225
+ }
226
+ const char *month = xt_date_option(options, "month");
227
+ const char *day = xt_date_option(options, "day");
228
+ const char *year = xt_date_option(options, "year");
229
+ if (month != NULL && (strcmp(month, "long") == 0 || strcmp(month, "short") == 0 ||
230
+ strcmp(month, "narrow") == 0)) {
231
+ if (strcmp(month, "long") == 0) {
232
+ xt_date_append(buffer, sizeof(buffer), &length, xt_date_month_long[tm->tm_mon]);
233
+ } else {
234
+ xt_date_append(buffer, sizeof(buffer), &length, xt_date_month_short[tm->tm_mon]);
235
+ }
236
+ if (day != NULL) {
237
+ xt_date_append(buffer, sizeof(buffer), &length, " ");
238
+ xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_mday,
239
+ xt_date_option_width(day));
240
+ }
241
+ if (year != NULL) {
242
+ xt_date_append(buffer, sizeof(buffer), &length, ", ");
243
+ xt_date_append_int(buffer, sizeof(buffer), &length,
244
+ xt_date_option_width(year) == 2 ? (tm->tm_year + 1900) % 100
245
+ : tm->tm_year + 1900,
246
+ xt_date_option_width(year));
247
+ }
248
+ return xt_string_from_cstr(buffer);
249
+ }
250
+ if (month != NULL) {
251
+ xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_mon + 1,
252
+ xt_date_option_width(month));
253
+ }
254
+ if (day != NULL) {
255
+ if (month != NULL) xt_date_append(buffer, sizeof(buffer), &length, "/");
256
+ xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_mday, xt_date_option_width(day));
257
+ }
258
+ if (year != NULL) {
259
+ if (month != NULL || day != NULL) xt_date_append(buffer, sizeof(buffer), &length, "/");
260
+ xt_date_append_int(buffer, sizeof(buffer), &length,
261
+ xt_date_option_width(year) == 2 ? (tm->tm_year + 1900) % 100
262
+ : tm->tm_year + 1900,
263
+ xt_date_option_width(year));
264
+ }
265
+ return xt_string_from_cstr(buffer);
266
+ }
267
+
268
+ static xt_value xt_date_locale_time(const struct tm *tm, xt_value options) {
269
+ static const char *const names[] = {"hour", "minute", "second"};
270
+ int custom = xt_date_has_option(options, names, 3);
271
+ const char *hour12_option = xt_date_option(options, "hour12");
272
+ int hour12 = 1;
273
+ if (hour12_option != NULL) hour12 = strcmp(hour12_option, "false") != 0;
274
+ int hour = tm->tm_hour;
275
+ const char *suffix = hour >= 12 ? "PM" : "AM";
276
+ if (hour12) hour = hour % 12 == 0 ? 12 : hour % 12;
277
+ char buffer[64];
278
+ size_t length = 0;
279
+ buffer[0] = '\0';
280
+ const char *hour_option = xt_date_option(options, "hour");
281
+ const char *minute_option = xt_date_option(options, "minute");
282
+ const char *second_option = xt_date_option(options, "second");
283
+ if (!custom) {
284
+ xt_date_append_int(buffer, sizeof(buffer), &length, hour, 1);
285
+ xt_date_append(buffer, sizeof(buffer), &length, ":");
286
+ xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_min, 2);
287
+ xt_date_append(buffer, sizeof(buffer), &length, ":");
288
+ xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_sec, 2);
289
+ if (hour12) {
290
+ xt_date_append(buffer, sizeof(buffer), &length, " ");
291
+ xt_date_append(buffer, sizeof(buffer), &length, suffix);
292
+ }
293
+ return xt_string_from_cstr(buffer);
294
+ }
295
+ if (hour_option != NULL) {
296
+ xt_date_append_int(buffer, sizeof(buffer), &length, hour, xt_date_option_width(hour_option));
297
+ } else if (minute_option != NULL || second_option != NULL) {
298
+ xt_date_append_int(buffer, sizeof(buffer), &length, hour, 1);
299
+ }
300
+ if (minute_option != NULL) {
301
+ xt_date_append(buffer, sizeof(buffer), &length, ":");
302
+ xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_min,
303
+ xt_date_option_width(minute_option));
304
+ }
305
+ if (second_option != NULL) {
306
+ xt_date_append(buffer, sizeof(buffer), &length, ":");
307
+ xt_date_append_int(buffer, sizeof(buffer), &length, tm->tm_sec,
308
+ xt_date_option_width(second_option));
309
+ }
310
+ if (hour12) {
311
+ xt_date_append(buffer, sizeof(buffer), &length, " ");
312
+ xt_date_append(buffer, sizeof(buffer), &length, suffix);
313
+ }
314
+ return xt_string_from_cstr(buffer);
315
+ }
316
+
152
317
  static xt_value xt_date_method(xt_value target, const char *method, int32_t argc, xt_value *argv, int *handled) {
153
- (void)argc;
154
- (void)argv;
155
318
  *handled = 1;
156
319
  xt_date *date = (xt_date *)XT_GET_PTR(target);
157
320
  if (strcmp(method, "getTime") == 0 || strcmp(method, "valueOf") == 0) return xt_number(date->time);
@@ -167,8 +330,45 @@ static xt_value xt_date_method(xt_value target, const char *method, int32_t argc
167
330
  }
168
331
  /* Bare getters and `toString` report local time; `getUTC*` and
169
332
  * `toISOString` report UTC. */
170
- if (isUtc || strcmp(method, "toISOString") == 0) xt_gmtime_r(&seconds, &tm);
171
- else xt_localtime_r(&seconds, &tm);
333
+ if (isUtc || strcmp(method, "toISOString") == 0 || strcmp(method, "toUTCString") == 0) {
334
+ xt_gmtime_r(&seconds, &tm);
335
+ } else {
336
+ xt_localtime_r(&seconds, &tm);
337
+ }
338
+ /* Locale/time-string methods answer "Invalid Date" for NaN time values, as
339
+ * browsers do; `toISOString` is the one that throws. */
340
+ if (strcmp(method, "toLocaleDateString") == 0 || strcmp(method, "toLocaleTimeString") == 0 ||
341
+ strcmp(method, "toLocaleString") == 0 || strcmp(method, "toDateString") == 0 ||
342
+ strcmp(method, "toTimeString") == 0 || strcmp(method, "toUTCString") == 0) {
343
+ if (isnan(date->time)) return xt_string_from_cstr("Invalid Date");
344
+ xt_value options = XT_UNDEFINED;
345
+ if (argc >= 2 && XT_IS_OBJECT(argv[1])) options = argv[1];
346
+ else if (argc >= 1 && XT_IS_OBJECT(argv[0])) options = argv[0];
347
+ if (strcmp(method, "toDateString") == 0) {
348
+ char buffer[48];
349
+ snprintf(buffer, sizeof(buffer), "%s %s %02d %04d", xt_date_weekday_short[tm.tm_wday],
350
+ xt_date_month_short[tm.tm_mon], tm.tm_mday, tm.tm_year + 1900);
351
+ return xt_string_from_cstr(buffer);
352
+ }
353
+ if (strcmp(method, "toTimeString") == 0) {
354
+ char buffer[32];
355
+ snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec);
356
+ return xt_string_from_cstr(buffer);
357
+ }
358
+ if (strcmp(method, "toUTCString") == 0) {
359
+ char buffer[64];
360
+ snprintf(buffer, sizeof(buffer), "%s, %02d %s %04d %02d:%02d:%02d GMT",
361
+ xt_date_weekday_short[tm.tm_wday], tm.tm_mday, xt_date_month_short[tm.tm_mon],
362
+ tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
363
+ return xt_string_from_cstr(buffer);
364
+ }
365
+ xt_value date_part = xt_date_locale_date(&tm, options);
366
+ if (strcmp(method, "toLocaleDateString") == 0) return date_part;
367
+ xt_value time_part = xt_date_locale_time(&tm, options);
368
+ if (strcmp(method, "toLocaleTimeString") == 0) return time_part;
369
+ xt_value comma = xt_string_from_cstr(", ");
370
+ return xt_add(xt_add(date_part, comma), time_part);
371
+ }
172
372
  if (strcmp(name, "getFullYear") == 0) return xt_number(tm.tm_year + 1900);
173
373
  if (strcmp(name, "getMonth") == 0) return xt_number(tm.tm_mon);
174
374
  if (strcmp(name, "getDate") == 0) return xt_number(tm.tm_mday);