@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.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 (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +1677 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2594 -0
  10. package/native/runtime_agent.c +1693 -0
  11. package/native/runtime_arguments.c +355 -0
  12. package/native/runtime_array.c +5742 -0
  13. package/native/runtime_array_buffer.c +1724 -0
  14. package/native/runtime_async_generator.c +948 -0
  15. package/native/runtime_atomics.c +1259 -0
  16. package/native/runtime_bigint.c +1363 -0
  17. package/native/runtime_bigint_object.c +602 -0
  18. package/native/runtime_binding.c +1005 -0
  19. package/native/runtime_boolean.c +296 -0
  20. package/native/runtime_clock.c +157 -0
  21. package/native/runtime_clock_posix.c +575 -0
  22. package/native/runtime_core.c +464 -0
  23. package/native/runtime_data_view.c +1111 -0
  24. package/native/runtime_date.c +1973 -0
  25. package/native/runtime_descriptor.c +1080 -0
  26. package/native/runtime_disposal.c +1643 -0
  27. package/native/runtime_enumeration.c +612 -0
  28. package/native/runtime_error.c +973 -0
  29. package/native/runtime_event_loop.c +629 -0
  30. package/native/runtime_function.c +3643 -0
  31. package/native/runtime_generator.c +1096 -0
  32. package/native/runtime_internal.h +5554 -0
  33. package/native/runtime_iterator.c +5355 -0
  34. package/native/runtime_json.c +1968 -0
  35. package/native/runtime_map.c +1539 -0
  36. package/native/runtime_math.c +894 -0
  37. package/native/runtime_memory.c +1341 -0
  38. package/native/runtime_module.c +860 -0
  39. package/native/runtime_number.c +2053 -0
  40. package/native/runtime_object.c +399 -0
  41. package/native/runtime_object_builtin.c +3877 -0
  42. package/native/runtime_primitive.c +2244 -0
  43. package/native/runtime_process.c +720 -0
  44. package/native/runtime_promise.c +3354 -0
  45. package/native/runtime_property.c +791 -0
  46. package/native/runtime_proxy.c +1533 -0
  47. package/native/runtime_realm.c +411 -0
  48. package/native/runtime_reflect.c +624 -0
  49. package/native/runtime_regexp.c +3495 -0
  50. package/native/runtime_regexp_matcher.c +3535 -0
  51. package/native/runtime_regexp_symbol.c +1471 -0
  52. package/native/runtime_set.c +1751 -0
  53. package/native/runtime_string.c +2862 -0
  54. package/native/runtime_string_match.c +1161 -0
  55. package/native/runtime_symbol.c +986 -0
  56. package/native/runtime_typed_array.c +5113 -0
  57. package/native/runtime_unicode_tables.h +14647 -0
  58. package/native/runtime_uri.c +676 -0
  59. package/native/runtime_weak_collection.c +1275 -0
  60. package/package.json +38 -3
@@ -0,0 +1,1973 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <math.h>
7
+ #include <stdio.h>
8
+ #include <string.h>
9
+
10
+ /*
11
+ * The Date family: the %Date% constructor with its call and construct
12
+ * behavior, the `now`, `parse`, and `UTC` statics, %Date.prototype% and
13
+ * its forty-four own methods, and the time-value arithmetic of 21.4.1.
14
+ *
15
+ * The realm's local time zone is UTC, so LocalTZA is +0 for every time
16
+ * value and LocalTime and UTC are identities on a finite operand. That
17
+ * is a host choice ECMA-262 permits; the clock adapter of ADR 0025 has
18
+ * no time-zone capability, so no host zone reaches this component.
19
+ *
20
+ * The current time is the realm's epoch real-time capability, read
21
+ * through `oseo_clock_real_time` in `date_current_time_value` alone. This
22
+ * component never reads a host clock itself and never touches the
23
+ * monotonic domain: reading the current time neither opens the
24
+ * scheduler's monotonic origin nor moves a timer deadline, and a
25
+ * wall-clock adjustment changes later readings and nothing else.
26
+ */
27
+
28
+ /*
29
+ * 21.4.1.11 and 21.4.1.13 specify MakeTime and MakeDate as IEEE 754-2019
30
+ * arithmetic in a fixed order, so every product must round to a double
31
+ * before it reaches the sum that follows it. A contracted multiply-add
32
+ * skips that rounding, which changes the answer: on a target whose
33
+ * baseline has a fused multiply-add, C11 leaves the compiler free to
34
+ * contract `day * msPerDay + time`, and
35
+ * `Date.UTC(1970, 0, 213503982336, 0, 0, 0, -18446744073709552000)` then
36
+ * reports 34448384 rather than the specified 34447360. Pinning
37
+ * contraction off for this component makes every step round where the
38
+ * specification says it rounds, on every target.
39
+ */
40
+ #if defined(__GNUC__) && !defined(__clang__)
41
+ #pragma GCC optimize ("fp-contract=off")
42
+ #else
43
+ #pragma STDC FP_CONTRACT OFF
44
+ #endif
45
+
46
+ #define OSEO_MS_PER_SECOND 1000.0
47
+ #define OSEO_MS_PER_MINUTE 60000.0
48
+ #define OSEO_MS_PER_HOUR 3600000.0
49
+ #define OSEO_MS_PER_DAY 86400000.0
50
+ /* The reviewed time-value range of 21.4.1.1, 100,000,000 days either
51
+ * way from the epoch. */
52
+ #define OSEO_TIME_VALUE_LIMIT 8.64e15
53
+
54
+ /* One `Date.prototype` method's own `name` and `length`. */
55
+ typedef struct {
56
+ const char *name;
57
+ size_t length;
58
+ } OseoDateMethodEntry;
59
+
60
+ /*
61
+ * The prototype methods in creation order. `[Symbol.toPrimitive]` holds
62
+ * the function name the specification gives it; its property key is the
63
+ * well-known symbol rather than that text.
64
+ */
65
+ static const OseoDateMethodEntry date_methods[] = {
66
+ {"getDate", 0u},
67
+ {"getDay", 0u},
68
+ {"getFullYear", 0u},
69
+ {"getHours", 0u},
70
+ {"getMilliseconds", 0u},
71
+ {"getMinutes", 0u},
72
+ {"getMonth", 0u},
73
+ {"getSeconds", 0u},
74
+ {"getTime", 0u},
75
+ {"getTimezoneOffset", 0u},
76
+ {"getUTCDate", 0u},
77
+ {"getUTCDay", 0u},
78
+ {"getUTCFullYear", 0u},
79
+ {"getUTCHours", 0u},
80
+ {"getUTCMilliseconds", 0u},
81
+ {"getUTCMinutes", 0u},
82
+ {"getUTCMonth", 0u},
83
+ {"getUTCSeconds", 0u},
84
+ {"setDate", 1u},
85
+ {"setFullYear", 3u},
86
+ {"setHours", 4u},
87
+ {"setMilliseconds", 1u},
88
+ {"setMinutes", 3u},
89
+ {"setMonth", 2u},
90
+ {"setSeconds", 2u},
91
+ {"setTime", 1u},
92
+ {"setUTCDate", 1u},
93
+ {"setUTCFullYear", 3u},
94
+ {"setUTCHours", 4u},
95
+ {"setUTCMilliseconds", 1u},
96
+ {"setUTCMinutes", 3u},
97
+ {"setUTCMonth", 2u},
98
+ {"setUTCSeconds", 2u},
99
+ {"toDateString", 0u},
100
+ {"toISOString", 0u},
101
+ {"toJSON", 1u},
102
+ {"toLocaleDateString", 0u},
103
+ {"toLocaleString", 0u},
104
+ {"toLocaleTimeString", 0u},
105
+ {"toString", 0u},
106
+ {"toTimeString", 0u},
107
+ {"toUTCString", 0u},
108
+ {"valueOf", 0u},
109
+ {"[Symbol.toPrimitive]", 1u},
110
+ {"getYear", 0u},
111
+ {"setYear", 1u},
112
+ };
113
+
114
+ _Static_assert(
115
+ sizeof(date_methods) / sizeof(date_methods[0]) ==
116
+ (size_t)OSEO_DATE_METHOD_COUNT,
117
+ "The Date method table must match its reviewed code-ID range."
118
+ );
119
+
120
+ static const char *const date_weekday_names[] = {
121
+ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
122
+ };
123
+
124
+ static const char *const date_month_names[] = {
125
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
126
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
127
+ };
128
+
129
+ /*
130
+ * The broken-down civil fields of one time value. `year` is the full
131
+ * proleptic Gregorian year, `month` is 0 through 11 as MonthFromTime
132
+ * reports it, and `weekday` is 0 for Sunday.
133
+ */
134
+ typedef struct {
135
+ int64_t year;
136
+ int64_t month;
137
+ int64_t day;
138
+ int64_t weekday;
139
+ int64_t hour;
140
+ int64_t minute;
141
+ int64_t second;
142
+ int64_t millisecond;
143
+ } OseoDateFields;
144
+
145
+ /* The seven MakeDay and MakeTime operands, in specification order. */
146
+ typedef enum {
147
+ OSEO_DATE_FIELD_YEAR = 0,
148
+ OSEO_DATE_FIELD_MONTH = 1,
149
+ OSEO_DATE_FIELD_DAY = 2,
150
+ OSEO_DATE_FIELD_HOUR = 3,
151
+ OSEO_DATE_FIELD_MINUTE = 4,
152
+ OSEO_DATE_FIELD_SECOND = 5,
153
+ OSEO_DATE_FIELD_MILLISECOND = 6,
154
+ OSEO_DATE_FIELD_COUNT = 7,
155
+ } OseoDateField;
156
+
157
+ /* One component setter's argument window over those seven fields. */
158
+ typedef struct {
159
+ OseoDateField first;
160
+ size_t count;
161
+ bool local;
162
+ } OseoDateSetter;
163
+
164
+ /*
165
+ * ECMA-262's modulo, whose result takes the sign of the divisor. Both
166
+ * operands are finite here, so `fmod` is exact and the correction is
167
+ * one addition.
168
+ */
169
+ static double date_modulo(double value, double divisor) {
170
+ double remainder = fmod(value, divisor);
171
+ if (remainder != 0.0 && ((remainder < 0.0) != (divisor < 0.0))) {
172
+ remainder += divisor;
173
+ }
174
+ return remainder;
175
+ }
176
+
177
+ /* Day(t), 21.4.1.3. */
178
+ static double date_day(double t) {
179
+ return floor(t / OSEO_MS_PER_DAY);
180
+ }
181
+
182
+ /* TimeWithinDay(t), 21.4.1.4. */
183
+ static double date_time_within_day(double t) {
184
+ return date_modulo(t, OSEO_MS_PER_DAY);
185
+ }
186
+
187
+ /*
188
+ * The civil year, month, and day of a day number, using the exact
189
+ * integer era arithmetic of the proleptic Gregorian calendar. `days` is
190
+ * bounded by the reviewed time-value range before any caller reaches
191
+ * this, so every intermediate stays far inside int64_t.
192
+ */
193
+ static void date_civil_from_days(
194
+ int64_t days,
195
+ int64_t *year,
196
+ int64_t *month,
197
+ int64_t *day
198
+ ) {
199
+ int64_t shifted = days + 719468;
200
+ int64_t era = (shifted >= 0 ? shifted : shifted - 146096) / 146097;
201
+ int64_t day_of_era = shifted - era * 146097;
202
+ int64_t year_of_era = (day_of_era - day_of_era / 1460 +
203
+ day_of_era / 36524 - day_of_era / 146096) /
204
+ 365;
205
+ int64_t civil_year = year_of_era + era * 400;
206
+ int64_t day_of_year = day_of_era -
207
+ (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
208
+ int64_t shifted_month = (5 * day_of_year + 2) / 153;
209
+ *day = day_of_year - (153 * shifted_month + 2) / 5 + 1;
210
+ *month = shifted_month < 10 ? shifted_month + 3 : shifted_month - 9;
211
+ *year = civil_year + (*month <= 2 ? 1 : 0);
212
+ }
213
+
214
+ /*
215
+ * The day number of one civil date, the inverse of
216
+ * `date_civil_from_days`. `year` is bounded by `date_make_day` before
217
+ * this runs, so the products stay inside int64_t.
218
+ */
219
+ static int64_t date_days_from_civil(
220
+ int64_t year,
221
+ int64_t month,
222
+ int64_t day
223
+ ) {
224
+ int64_t shifted_year = year - (month <= 2 ? 1 : 0);
225
+ int64_t era = (shifted_year >= 0 ? shifted_year : shifted_year - 399) /
226
+ 400;
227
+ int64_t year_of_era = shifted_year - era * 400;
228
+ int64_t day_of_year =
229
+ (153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1;
230
+ int64_t day_of_era = year_of_era * 365 + year_of_era / 4 -
231
+ year_of_era / 100 + day_of_year;
232
+ return era * 146097 + day_of_era - 719468;
233
+ }
234
+
235
+ /*
236
+ * YearFromTime, MonthFromTime, DateFromTime, WeekDay, HourFromTime,
237
+ * MinFromTime, SecFromTime, and msFromTime of one finite time value,
238
+ * computed once. `t` is always a finite time value within the reviewed
239
+ * range or a MakeDate result a caller has already clipped.
240
+ */
241
+ static void date_fields(double t, OseoDateFields *fields) {
242
+ double day_number = date_day(t);
243
+ double within_day = date_time_within_day(t);
244
+ date_civil_from_days(
245
+ (int64_t)day_number,
246
+ &fields->year,
247
+ &fields->month,
248
+ &fields->day
249
+ );
250
+ fields->month -= 1;
251
+ fields->weekday = (int64_t)date_modulo(day_number + 4.0, 7.0);
252
+ fields->hour =
253
+ (int64_t)floor(within_day / OSEO_MS_PER_HOUR);
254
+ fields->minute = (int64_t)date_modulo(
255
+ floor(within_day / OSEO_MS_PER_MINUTE),
256
+ 60.0
257
+ );
258
+ fields->second = (int64_t)date_modulo(
259
+ floor(within_day / OSEO_MS_PER_SECOND),
260
+ 60.0
261
+ );
262
+ fields->millisecond = (int64_t)date_modulo(within_day, 1000.0);
263
+ }
264
+
265
+ /*
266
+ * LocalTZA(t, isUTC), 21.4.1.7. The realm's local time zone is UTC, so
267
+ * the offset is +0 for every time value and neither argument can change
268
+ * it. Every LocalTime and UTC call routes through this one definition so
269
+ * the later host time-zone adapter has exactly one place to replace.
270
+ */
271
+ static double date_local_time_zone_adjustment(void) {
272
+ return 0.0;
273
+ }
274
+
275
+ /* LocalTime(t), 21.4.1.8. */
276
+ static double date_local_time(double t) {
277
+ if (!isfinite(t)) return (double)NAN;
278
+ return t + date_local_time_zone_adjustment();
279
+ }
280
+
281
+ /* UTC(t), 21.4.1.9. */
282
+ static double date_utc_time(double t) {
283
+ if (!isfinite(t)) return (double)NAN;
284
+ return t - date_local_time_zone_adjustment();
285
+ }
286
+
287
+ /* ToIntegerOrInfinity over an already converted Number, 7.1.5. */
288
+ static double date_to_integer(double value) {
289
+ if (isnan(value)) return 0.0;
290
+ if (!isfinite(value)) return value;
291
+ double truncated = trunc(value);
292
+ return truncated == 0.0 ? 0.0 : truncated;
293
+ }
294
+
295
+ /* MakeTime(hour, min, sec, ms), 21.4.1.11. */
296
+ static double date_make_time(
297
+ double hour,
298
+ double minute,
299
+ double second,
300
+ double millisecond
301
+ ) {
302
+ if (!isfinite(hour) || !isfinite(minute) || !isfinite(second) ||
303
+ !isfinite(millisecond)) {
304
+ return (double)NAN;
305
+ }
306
+ return date_to_integer(hour) * OSEO_MS_PER_HOUR +
307
+ date_to_integer(minute) * OSEO_MS_PER_MINUTE +
308
+ date_to_integer(second) * OSEO_MS_PER_SECOND +
309
+ date_to_integer(millisecond);
310
+ }
311
+
312
+ /*
313
+ * MakeDay(year, month, date), 21.4.1.12. Step 6 asks for a finite time
314
+ * value naming the first day of the resolved year and month; a year
315
+ * whose magnitude passes this bound has none, because MakeDate would
316
+ * leave the reviewed range by more than the day count can recover, so it
317
+ * takes the specified NaN.
318
+ */
319
+ static double date_make_day(double year, double month, double day) {
320
+ if (!isfinite(year) || !isfinite(month) || !isfinite(day)) {
321
+ return (double)NAN;
322
+ }
323
+ double integral_year = date_to_integer(year);
324
+ double integral_month = date_to_integer(month);
325
+ double integral_day = date_to_integer(day);
326
+ double resolved_year = integral_year + floor(integral_month / 12.0);
327
+ if (!isfinite(resolved_year) || fabs(resolved_year) > 1e9) {
328
+ return (double)NAN;
329
+ }
330
+ double resolved_month = date_modulo(integral_month, 12.0);
331
+ int64_t days = date_days_from_civil(
332
+ (int64_t)resolved_year,
333
+ (int64_t)resolved_month + 1,
334
+ 1
335
+ );
336
+ return (double)days + integral_day - 1.0;
337
+ }
338
+
339
+ /* MakeDate(day, time), 21.4.1.13. */
340
+ static double date_make_date(double day, double time) {
341
+ if (!isfinite(day) || !isfinite(time)) return (double)NAN;
342
+ double value = day * OSEO_MS_PER_DAY + time;
343
+ if (!isfinite(value)) return (double)NAN;
344
+ return value;
345
+ }
346
+
347
+ /* TimeClip(time), 21.4.1.14. */
348
+ static double date_time_clip(double time) {
349
+ if (!isfinite(time)) return (double)NAN;
350
+ if (fabs(time) > OSEO_TIME_VALUE_LIMIT) return (double)NAN;
351
+ return date_to_integer(time);
352
+ }
353
+
354
+ /* MakeFullYear(year), 21.4.1.15. */
355
+ static double date_make_full_year(double year) {
356
+ if (isnan(year)) return (double)NAN;
357
+ double truncated = date_to_integer(year);
358
+ if (truncated >= 0.0 && truncated <= 99.0) return 1900.0 + truncated;
359
+ return truncated;
360
+ }
361
+
362
+ /*
363
+ * The current time value, 21.4.2.1 step 1 and 21.4.3.1: the realm's epoch
364
+ * real-time capability rounded down to a whole millisecond, then
365
+ * TimeClip. An adapter may report a fraction, and the millisecond that
366
+ * contains the instant is the one its time value designates, including
367
+ * before the epoch. A reading past the time-value range clips to NaN as
368
+ * any other time value would. An adapter without real time reports the
369
+ * owned non-catchable diagnostic rather than an invented time value.
370
+ */
371
+ static OseoResult date_current_time_value(
372
+ OseoContext *context,
373
+ double *value
374
+ ) {
375
+ double milliseconds = 0.0;
376
+ if (!oseo_clock_real_time(context, &milliseconds)) {
377
+ return failure(
378
+ context,
379
+ "OSEO2001",
380
+ "The host real-time clock is unavailable."
381
+ );
382
+ }
383
+ *value = date_time_clip(floor(milliseconds));
384
+ return normal(oseo_undefined());
385
+ }
386
+
387
+ /* thisTimeValue(value), 21.4.4.1's RequireInternalSlot. */
388
+ static OseoResult date_receiver(
389
+ OseoContext *context,
390
+ OseoValue receiver,
391
+ double *time_value
392
+ ) {
393
+ if (!is_date(receiver)) {
394
+ return oseo_internal_throw_error(
395
+ context,
396
+ OSEO_ERROR_TYPE,
397
+ "Date method requires a Date receiver."
398
+ );
399
+ }
400
+ *time_value = date_object(receiver)->time_value;
401
+ return normal(receiver);
402
+ }
403
+
404
+ /* One argument of a built-in call, or undefined past the end. */
405
+ static OseoValue date_argument(
406
+ size_t argument_count,
407
+ const OseoValue *arguments,
408
+ size_t index
409
+ ) {
410
+ return index < argument_count ? arguments[index] : oseo_undefined();
411
+ }
412
+
413
+ /* The getters of 21.4.4.2 through 21.4.4.21, over one field selection. */
414
+ static OseoResult date_get_field(
415
+ OseoContext *context,
416
+ OseoValue receiver,
417
+ OseoDateMethod method
418
+ ) {
419
+ double time_value = 0.0;
420
+ OseoResult required = date_receiver(context, receiver, &time_value);
421
+ if (required.status != OSEO_STATUS_NORMAL) return required;
422
+ if (isnan(time_value)) return normal(oseo_number((double)NAN));
423
+ if (method == OSEO_DATE_GET_TIME) {
424
+ return normal(oseo_number(time_value));
425
+ }
426
+ if (method == OSEO_DATE_GET_TIMEZONE_OFFSET) {
427
+ double local = date_local_time(time_value);
428
+ return normal(
429
+ oseo_number((time_value - local) / OSEO_MS_PER_MINUTE)
430
+ );
431
+ }
432
+ bool local = method <= OSEO_DATE_GET_SECONDS;
433
+ OseoDateFields fields;
434
+ date_fields(local ? date_local_time(time_value) : time_value, &fields);
435
+ int64_t component = 0;
436
+ switch (method) {
437
+ case OSEO_DATE_GET_DATE:
438
+ case OSEO_DATE_GET_UTC_DATE:
439
+ component = fields.day;
440
+ break;
441
+ case OSEO_DATE_GET_DAY:
442
+ case OSEO_DATE_GET_UTC_DAY:
443
+ component = fields.weekday;
444
+ break;
445
+ case OSEO_DATE_GET_FULL_YEAR:
446
+ case OSEO_DATE_GET_UTC_FULL_YEAR:
447
+ component = fields.year;
448
+ break;
449
+ case OSEO_DATE_GET_HOURS:
450
+ case OSEO_DATE_GET_UTC_HOURS:
451
+ component = fields.hour;
452
+ break;
453
+ case OSEO_DATE_GET_MILLISECONDS:
454
+ case OSEO_DATE_GET_UTC_MILLISECONDS:
455
+ component = fields.millisecond;
456
+ break;
457
+ case OSEO_DATE_GET_MINUTES:
458
+ case OSEO_DATE_GET_UTC_MINUTES:
459
+ component = fields.minute;
460
+ break;
461
+ case OSEO_DATE_GET_MONTH:
462
+ case OSEO_DATE_GET_UTC_MONTH:
463
+ component = fields.month;
464
+ break;
465
+ default:
466
+ component = fields.second;
467
+ break;
468
+ }
469
+ return normal(oseo_number((double)component));
470
+ }
471
+
472
+ /*
473
+ * The argument window and time zone of each component setter. The table
474
+ * is indexed by `method - OSEO_DATE_SET_DATE` and skips `setTime`, whose
475
+ * own clause has no window.
476
+ */
477
+ static bool date_setter_shape(
478
+ OseoDateMethod method,
479
+ OseoDateSetter *setter
480
+ ) {
481
+ switch (method) {
482
+ case OSEO_DATE_SET_DATE:
483
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_DAY, 1u, true};
484
+ return true;
485
+ case OSEO_DATE_SET_FULL_YEAR:
486
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_YEAR, 3u, true};
487
+ return true;
488
+ case OSEO_DATE_SET_HOURS:
489
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_HOUR, 4u, true};
490
+ return true;
491
+ case OSEO_DATE_SET_MILLISECONDS:
492
+ *setter =
493
+ (OseoDateSetter){OSEO_DATE_FIELD_MILLISECOND, 1u, true};
494
+ return true;
495
+ case OSEO_DATE_SET_MINUTES:
496
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_MINUTE, 3u, true};
497
+ return true;
498
+ case OSEO_DATE_SET_MONTH:
499
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_MONTH, 2u, true};
500
+ return true;
501
+ case OSEO_DATE_SET_SECONDS:
502
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_SECOND, 2u, true};
503
+ return true;
504
+ case OSEO_DATE_SET_UTC_DATE:
505
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_DAY, 1u, false};
506
+ return true;
507
+ case OSEO_DATE_SET_UTC_FULL_YEAR:
508
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_YEAR, 3u, false};
509
+ return true;
510
+ case OSEO_DATE_SET_UTC_HOURS:
511
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_HOUR, 4u, false};
512
+ return true;
513
+ case OSEO_DATE_SET_UTC_MILLISECONDS:
514
+ *setter =
515
+ (OseoDateSetter){OSEO_DATE_FIELD_MILLISECOND, 1u, false};
516
+ return true;
517
+ case OSEO_DATE_SET_UTC_MINUTES:
518
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_MINUTE, 3u, false};
519
+ return true;
520
+ case OSEO_DATE_SET_UTC_MONTH:
521
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_MONTH, 2u, false};
522
+ return true;
523
+ case OSEO_DATE_SET_UTC_SECONDS:
524
+ *setter = (OseoDateSetter){OSEO_DATE_FIELD_SECOND, 2u, false};
525
+ return true;
526
+ default:
527
+ return false;
528
+ }
529
+ }
530
+
531
+ /*
532
+ * The component setters of 21.4.4.20 through 21.4.4.31. Each reads the
533
+ * receiver's [[DateValue]] first and then runs ToNumber over every
534
+ * present argument in specification order, so an abrupt conversion is
535
+ * observed whatever that time value is. The NaN test and every
536
+ * component read that one snapshot, so a conversion that stores a new
537
+ * time value into the same Date does not change the result; the pinned
538
+ * Node.js host instead rereads the slot after the conversions. A
539
+ * year-first setter starts from +0 when the snapshot is NaN; every
540
+ * other window leaves the slot untouched and reports NaN.
541
+ *
542
+ * Recomposing all seven fields is the same arithmetic the individual
543
+ * clauses perform: MakeDay over the receiver's own year, month, and day
544
+ * reproduces Day(t), and MakeTime over its hour, minute, second, and
545
+ * millisecond reproduces TimeWithinDay(t).
546
+ */
547
+ static OseoResult date_set_fields(
548
+ OseoContext *context,
549
+ OseoValue receiver,
550
+ OseoDateMethod method,
551
+ size_t argument_count,
552
+ const OseoValue *arguments
553
+ ) {
554
+ OseoDateSetter setter = {OSEO_DATE_FIELD_YEAR, 0u, true};
555
+ if (!date_setter_shape(method, &setter)) {
556
+ return failure(context, "OSEO2001", "Unknown Date setter.");
557
+ }
558
+ double time_value = 0.0;
559
+ OseoResult required = date_receiver(context, receiver, &time_value);
560
+ if (required.status != OSEO_STATUS_NORMAL) return required;
561
+ double supplied[OSEO_DATE_FIELD_COUNT];
562
+ bool present[OSEO_DATE_FIELD_COUNT];
563
+ for (size_t index = 0u; index < OSEO_DATE_FIELD_COUNT; index += 1u) {
564
+ supplied[index] = 0.0;
565
+ present[index] = false;
566
+ }
567
+ OseoValue slots[1] = {receiver};
568
+ OseoRootFrame frame = {NULL, slots, 1u};
569
+ oseo_roots_push(context, &frame);
570
+ OseoResult result = normal(oseo_undefined());
571
+ /*
572
+ * The first parameter of every component setter is converted whether
573
+ * or not the call supplied it, because its clause reads it
574
+ * unconditionally: `setDate()` converts undefined and reports NaN. The
575
+ * trailing parameters are the ones the clauses guard with "if present",
576
+ * so an absent one takes its component from the receiver instead.
577
+ */
578
+ for (size_t index = 0u; index < setter.count; index += 1u) {
579
+ if (index > 0u && index >= argument_count) break;
580
+ result = oseo_internal_to_number(
581
+ context,
582
+ date_argument(argument_count, arguments, index)
583
+ );
584
+ if (result.status != OSEO_STATUS_NORMAL) {
585
+ oseo_roots_pop(context, &frame);
586
+ return result;
587
+ }
588
+ supplied[(size_t)setter.first + index] = number_value(result.value);
589
+ present[(size_t)setter.first + index] = true;
590
+ }
591
+ bool year_first = setter.first == OSEO_DATE_FIELD_YEAR;
592
+ if (isnan(time_value) && !year_first) {
593
+ oseo_roots_pop(context, &frame);
594
+ return normal(oseo_number((double)NAN));
595
+ }
596
+ double base = isnan(time_value)
597
+ ? 0.0
598
+ : (setter.local ? date_local_time(time_value) : time_value);
599
+ OseoDateFields fields;
600
+ date_fields(base, &fields);
601
+ double components[OSEO_DATE_FIELD_COUNT] = {
602
+ (double)fields.year,
603
+ (double)fields.month,
604
+ (double)fields.day,
605
+ (double)fields.hour,
606
+ (double)fields.minute,
607
+ (double)fields.second,
608
+ (double)fields.millisecond,
609
+ };
610
+ for (size_t index = 0u; index < OSEO_DATE_FIELD_COUNT; index += 1u) {
611
+ if (present[index]) components[index] = supplied[index];
612
+ }
613
+ double day = date_make_day(
614
+ components[OSEO_DATE_FIELD_YEAR],
615
+ components[OSEO_DATE_FIELD_MONTH],
616
+ components[OSEO_DATE_FIELD_DAY]
617
+ );
618
+ double time = date_make_time(
619
+ components[OSEO_DATE_FIELD_HOUR],
620
+ components[OSEO_DATE_FIELD_MINUTE],
621
+ components[OSEO_DATE_FIELD_SECOND],
622
+ components[OSEO_DATE_FIELD_MILLISECOND]
623
+ );
624
+ double composed = date_make_date(day, time);
625
+ double updated = date_time_clip(
626
+ setter.local ? date_utc_time(composed) : composed
627
+ );
628
+ date_object(slots[0])->time_value = updated;
629
+ oseo_roots_pop(context, &frame);
630
+ return normal(oseo_number(updated));
631
+ }
632
+
633
+ /*
634
+ * Date.prototype.getYear(), B.2.3.1: YearFromTime(LocalTime(t)) minus
635
+ * 1900, or NaN for an invalid date.
636
+ */
637
+ static OseoResult date_get_year(OseoContext *context, OseoValue receiver) {
638
+ double time_value = 0.0;
639
+ OseoResult required = date_receiver(context, receiver, &time_value);
640
+ if (required.status != OSEO_STATUS_NORMAL) return required;
641
+ if (isnan(time_value)) return normal(oseo_number((double)NAN));
642
+ OseoDateFields fields;
643
+ date_fields(date_local_time(time_value), &fields);
644
+ return normal(oseo_number((double)fields.year - 1900.0));
645
+ }
646
+
647
+ /*
648
+ * Date.prototype.setYear(year), B.2.3.2. The receiver's time value is
649
+ * read before ToNumber(year), like the other setters. An invalid date
650
+ * starts from +0, MakeFullYear maps 0 through 99 to 1900 through 1999,
651
+ * and the month, date, and time within the day come from LocalTime(t).
652
+ */
653
+ static OseoResult date_set_year(
654
+ OseoContext *context,
655
+ OseoValue receiver,
656
+ size_t argument_count,
657
+ const OseoValue *arguments
658
+ ) {
659
+ double time_value = 0.0;
660
+ OseoResult required = date_receiver(context, receiver, &time_value);
661
+ if (required.status != OSEO_STATUS_NORMAL) return required;
662
+ OseoValue slots[1] = {receiver};
663
+ OseoRootFrame frame = {NULL, slots, 1u};
664
+ oseo_roots_push(context, &frame);
665
+ OseoResult result = oseo_internal_to_number(
666
+ context,
667
+ date_argument(argument_count, arguments, 0u)
668
+ );
669
+ if (result.status != OSEO_STATUS_NORMAL) {
670
+ oseo_roots_pop(context, &frame);
671
+ return result;
672
+ }
673
+ double year = date_make_full_year(number_value(result.value));
674
+ double base = isnan(time_value) ? 0.0 : date_local_time(time_value);
675
+ OseoDateFields fields;
676
+ date_fields(base, &fields);
677
+ double day = date_make_day(
678
+ year,
679
+ (double)fields.month,
680
+ (double)fields.day
681
+ );
682
+ double composed = date_make_date(day, date_time_within_day(base));
683
+ double updated = date_time_clip(date_utc_time(composed));
684
+ date_object(slots[0])->time_value = updated;
685
+ oseo_roots_pop(context, &frame);
686
+ return normal(oseo_number(updated));
687
+ }
688
+
689
+ /* Date.prototype.setTime(time), 21.4.4.27. */
690
+ static OseoResult date_set_time(
691
+ OseoContext *context,
692
+ OseoValue receiver,
693
+ size_t argument_count,
694
+ const OseoValue *arguments
695
+ ) {
696
+ double time_value = 0.0;
697
+ OseoResult required = date_receiver(context, receiver, &time_value);
698
+ if (required.status != OSEO_STATUS_NORMAL) return required;
699
+ OseoValue slots[1] = {receiver};
700
+ OseoRootFrame frame = {NULL, slots, 1u};
701
+ oseo_roots_push(context, &frame);
702
+ OseoResult converted = oseo_internal_to_number(
703
+ context,
704
+ date_argument(argument_count, arguments, 0u)
705
+ );
706
+ if (converted.status != OSEO_STATUS_NORMAL) {
707
+ oseo_roots_pop(context, &frame);
708
+ return converted;
709
+ }
710
+ double updated = date_time_clip(number_value(converted.value));
711
+ date_object(slots[0])->time_value = updated;
712
+ oseo_roots_pop(context, &frame);
713
+ return normal(oseo_number(updated));
714
+ }
715
+
716
+ /*
717
+ * One short ASCII text this component assembles. `oseo_internal_ascii_string`
718
+ * is bounded by the property-name ceiling, which every Date text but
719
+ * "Invalid Date" exceeds, so the units are widened here instead.
720
+ */
721
+ static OseoResult date_text_string(
722
+ OseoContext *context,
723
+ const char *text,
724
+ size_t length
725
+ ) {
726
+ uint16_t units[128];
727
+ if (length > sizeof(units) / sizeof(*units)) {
728
+ return failure(context, "OSEO2001", "Date text is too long.");
729
+ }
730
+ for (size_t index = 0u; index < length; index += 1u) {
731
+ units[index] = (uint16_t)(unsigned char)text[index];
732
+ }
733
+ return oseo_string_from_units(context, units, length);
734
+ }
735
+
736
+ /* One nonnegative integer, zero-padded to at least `width` digits. */
737
+ static void date_append_padded(
738
+ char *text,
739
+ size_t *length,
740
+ int64_t value,
741
+ int width
742
+ ) {
743
+ char digits[24];
744
+ int written = snprintf(digits, sizeof(digits), "%lld", (long long)value);
745
+ if (written < 0) written = 0;
746
+ for (int index = written; index < width; index += 1) {
747
+ text[*length] = '0';
748
+ *length += 1u;
749
+ }
750
+ memcpy(text + *length, digits, (size_t)written);
751
+ *length += (size_t)written;
752
+ }
753
+
754
+ static void date_append_text(
755
+ char *text,
756
+ size_t *length,
757
+ const char *appended
758
+ ) {
759
+ size_t appended_length = strlen(appended);
760
+ memcpy(text + *length, appended, appended_length);
761
+ *length += appended_length;
762
+ }
763
+
764
+ /* DateString(tv), 21.4.4.41.2. */
765
+ static void date_append_date_string(
766
+ char *text,
767
+ size_t *length,
768
+ const OseoDateFields *fields
769
+ ) {
770
+ date_append_text(text, length, date_weekday_names[fields->weekday]);
771
+ date_append_text(text, length, " ");
772
+ date_append_text(text, length, date_month_names[fields->month]);
773
+ date_append_text(text, length, " ");
774
+ date_append_padded(text, length, fields->day, 2);
775
+ date_append_text(text, length, " ");
776
+ if (fields->year < 0) date_append_text(text, length, "-");
777
+ date_append_padded(
778
+ text,
779
+ length,
780
+ fields->year < 0 ? -fields->year : fields->year,
781
+ 4
782
+ );
783
+ }
784
+
785
+ /* TimeString(tv), 21.4.4.41.1, including its trailing " GMT". */
786
+ static void date_append_time_string(
787
+ char *text,
788
+ size_t *length,
789
+ const OseoDateFields *fields
790
+ ) {
791
+ date_append_padded(text, length, fields->hour, 2);
792
+ date_append_text(text, length, ":");
793
+ date_append_padded(text, length, fields->minute, 2);
794
+ date_append_text(text, length, ":");
795
+ date_append_padded(text, length, fields->second, 2);
796
+ date_append_text(text, length, " GMT");
797
+ }
798
+
799
+ /*
800
+ * TimeZoneString(tv), 21.4.4.41.3. The realm's local time zone is UTC,
801
+ * so the offset is always +0000 and the implementation-defined name is
802
+ * that zone's own.
803
+ */
804
+ static void date_append_time_zone_string(char *text, size_t *length) {
805
+ date_append_text(text, length, "+0000 (Coordinated Universal Time)");
806
+ }
807
+
808
+ /*
809
+ * ToDateString(tv), 21.4.4.41.4, and the three prototype methods that
810
+ * report one of its parts. The complete text is short ASCII, so it is
811
+ * assembled in a local buffer and published once.
812
+ */
813
+ static OseoResult date_to_date_string(
814
+ OseoContext *context,
815
+ double time_value,
816
+ OseoDateMethod method
817
+ ) {
818
+ if (isnan(time_value)) {
819
+ return date_text_string(context, "Invalid Date", 12u);
820
+ }
821
+ OseoDateFields fields;
822
+ date_fields(date_local_time(time_value), &fields);
823
+ char text[128];
824
+ size_t length = 0u;
825
+ if (method != OSEO_DATE_TO_TIME_STRING) {
826
+ date_append_date_string(text, &length, &fields);
827
+ }
828
+ if (method != OSEO_DATE_TO_DATE_STRING) {
829
+ if (method != OSEO_DATE_TO_TIME_STRING) {
830
+ date_append_text(text, &length, " ");
831
+ }
832
+ date_append_time_string(text, &length, &fields);
833
+ date_append_time_zone_string(text, &length);
834
+ }
835
+ return date_text_string(context, text, length);
836
+ }
837
+
838
+ /* Date.prototype.toUTCString(), 21.4.4.43. */
839
+ static OseoResult date_to_utc_string(
840
+ OseoContext *context,
841
+ double time_value
842
+ ) {
843
+ if (isnan(time_value)) {
844
+ return date_text_string(context, "Invalid Date", 12u);
845
+ }
846
+ OseoDateFields fields;
847
+ date_fields(time_value, &fields);
848
+ char text[128];
849
+ size_t length = 0u;
850
+ date_append_text(text, &length, date_weekday_names[fields.weekday]);
851
+ date_append_text(text, &length, ", ");
852
+ date_append_padded(text, &length, fields.day, 2);
853
+ date_append_text(text, &length, " ");
854
+ date_append_text(text, &length, date_month_names[fields.month]);
855
+ date_append_text(text, &length, " ");
856
+ if (fields.year < 0) date_append_text(text, &length, "-");
857
+ date_append_padded(
858
+ text,
859
+ &length,
860
+ fields.year < 0 ? -fields.year : fields.year,
861
+ 4
862
+ );
863
+ date_append_text(text, &length, " ");
864
+ date_append_time_string(text, &length, &fields);
865
+ return date_text_string(context, text, length);
866
+ }
867
+
868
+ /* Date.prototype.toISOString(), 21.4.4.36. */
869
+ static OseoResult date_to_iso_string(
870
+ OseoContext *context,
871
+ double time_value
872
+ ) {
873
+ if (isnan(time_value)) {
874
+ return oseo_internal_throw_error(
875
+ context,
876
+ OSEO_ERROR_RANGE,
877
+ "Date.prototype.toISOString requires a valid time value."
878
+ );
879
+ }
880
+ OseoDateFields fields;
881
+ date_fields(time_value, &fields);
882
+ char text[64];
883
+ size_t length = 0u;
884
+ if (fields.year < 0) {
885
+ date_append_text(text, &length, "-");
886
+ date_append_padded(text, &length, -fields.year, 6);
887
+ } else if (fields.year > 9999) {
888
+ date_append_text(text, &length, "+");
889
+ date_append_padded(text, &length, fields.year, 6);
890
+ } else {
891
+ date_append_padded(text, &length, fields.year, 4);
892
+ }
893
+ date_append_text(text, &length, "-");
894
+ date_append_padded(text, &length, fields.month + 1, 2);
895
+ date_append_text(text, &length, "-");
896
+ date_append_padded(text, &length, fields.day, 2);
897
+ date_append_text(text, &length, "T");
898
+ date_append_padded(text, &length, fields.hour, 2);
899
+ date_append_text(text, &length, ":");
900
+ date_append_padded(text, &length, fields.minute, 2);
901
+ date_append_text(text, &length, ":");
902
+ date_append_padded(text, &length, fields.second, 2);
903
+ date_append_text(text, &length, ".");
904
+ date_append_padded(text, &length, fields.millisecond, 3);
905
+ date_append_text(text, &length, "Z");
906
+ return date_text_string(context, text, length);
907
+ }
908
+
909
+ /*
910
+ * Date.prototype.toJSON(key), 21.4.4.37. The receiver need not be a
911
+ * Date: the conversion and the `toISOString` invocation are ordinary
912
+ * operations on whatever object ToObject produces.
913
+ */
914
+ static OseoResult date_to_json(OseoContext *context, OseoValue receiver) {
915
+ OseoRootFrame frame = {NULL, NULL, 0u};
916
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
917
+ if (result.status != OSEO_STATUS_NORMAL) return result;
918
+ result = oseo_internal_to_object(context, receiver);
919
+ frame.slots[0] = result.value;
920
+ if (result.status == OSEO_STATUS_NORMAL) {
921
+ result = oseo_internal_to_primitive(
922
+ context,
923
+ frame.slots[0],
924
+ OSEO_TO_PRIMITIVE_NUMBER
925
+ );
926
+ frame.slots[1] = result.value;
927
+ }
928
+ if (result.status == OSEO_STATUS_NORMAL &&
929
+ is_number(frame.slots[1]) &&
930
+ !isfinite(number_value(frame.slots[1]))) {
931
+ oseo_roots_release(context, &frame);
932
+ return normal(oseo_null());
933
+ }
934
+ if (result.status == OSEO_STATUS_NORMAL) {
935
+ result = oseo_internal_ascii_string(context, "toISOString");
936
+ frame.slots[2] = result.value;
937
+ }
938
+ if (result.status == OSEO_STATUS_NORMAL) {
939
+ result = oseo_object_get(context, frame.slots[0], frame.slots[2]);
940
+ frame.slots[2] = result.value;
941
+ }
942
+ if (result.status == OSEO_STATUS_NORMAL && !is_callable(frame.slots[2])) {
943
+ result = oseo_internal_throw_error(
944
+ context,
945
+ OSEO_ERROR_TYPE,
946
+ "Date.prototype.toJSON requires a callable toISOString."
947
+ );
948
+ }
949
+ if (result.status == OSEO_STATUS_NORMAL) {
950
+ result = oseo_call_function(
951
+ context,
952
+ frame.slots[2],
953
+ frame.slots[0],
954
+ 0u,
955
+ NULL,
956
+ oseo_undefined()
957
+ );
958
+ }
959
+ oseo_roots_release(context, &frame);
960
+ return result;
961
+ }
962
+
963
+ /* Date.prototype[Symbol.toPrimitive](hint), 21.4.4.45. */
964
+ static OseoResult date_to_primitive(
965
+ OseoContext *context,
966
+ OseoValue receiver,
967
+ size_t argument_count,
968
+ const OseoValue *arguments
969
+ ) {
970
+ if (!is_object(receiver)) {
971
+ return oseo_internal_throw_error(
972
+ context,
973
+ OSEO_ERROR_TYPE,
974
+ "Date.prototype[Symbol.toPrimitive] requires an object "
975
+ "receiver."
976
+ );
977
+ }
978
+ OseoValue hint = date_argument(argument_count, arguments, 0u);
979
+ OseoToPrimitiveHint resolved;
980
+ if (oseo_internal_string_is_ascii(hint, "string") ||
981
+ oseo_internal_string_is_ascii(hint, "default")) {
982
+ resolved = OSEO_TO_PRIMITIVE_STRING;
983
+ } else if (oseo_internal_string_is_ascii(hint, "number")) {
984
+ resolved = OSEO_TO_PRIMITIVE_NUMBER;
985
+ } else {
986
+ return oseo_internal_throw_error(
987
+ context,
988
+ OSEO_ERROR_TYPE,
989
+ "Date.prototype[Symbol.toPrimitive] requires the hint "
990
+ "'string', 'default', or 'number'."
991
+ );
992
+ }
993
+ return oseo_internal_ordinary_to_primitive(context, receiver, resolved);
994
+ }
995
+
996
+ /* One ASCII digit of a parsed string, or -1 for anything else. */
997
+ static int date_digit(const uint16_t *units, size_t length, size_t index) {
998
+ if (index >= length) return -1;
999
+ uint16_t unit = units[index];
1000
+ if (unit < '0' || unit > '9') return -1;
1001
+ return (int)(unit - '0');
1002
+ }
1003
+
1004
+ /* `count` consecutive digits read as one nonnegative integer. */
1005
+ static bool date_read_digits(
1006
+ const uint16_t *units,
1007
+ size_t length,
1008
+ size_t *cursor,
1009
+ size_t count,
1010
+ int64_t *value
1011
+ ) {
1012
+ int64_t accumulated = 0;
1013
+ for (size_t index = 0u; index < count; index += 1u) {
1014
+ int digit = date_digit(units, length, *cursor + index);
1015
+ if (digit < 0) return false;
1016
+ accumulated = accumulated * 10 + digit;
1017
+ }
1018
+ *cursor += count;
1019
+ *value = accumulated;
1020
+ return true;
1021
+ }
1022
+
1023
+ static bool date_expect(
1024
+ const uint16_t *units,
1025
+ size_t length,
1026
+ size_t *cursor,
1027
+ uint16_t unit
1028
+ ) {
1029
+ if (*cursor >= length || units[*cursor] != unit) return false;
1030
+ *cursor += 1u;
1031
+ return true;
1032
+ }
1033
+
1034
+ /*
1035
+ * The Date Time String Format of 21.4.1.32. An absent UTC offset makes a
1036
+ * date-only form UTC and a date-time form local time, which this realm
1037
+ * resolves the same way because its local time zone is UTC. `-000000` is
1038
+ * rejected as an extended year, an hour of 24 requires a zero minute,
1039
+ * second, and millisecond, and every other out-of-range component is a
1040
+ * NaN rather than a wrapped value.
1041
+ */
1042
+ static bool date_parse_iso(
1043
+ const uint16_t *units,
1044
+ size_t length,
1045
+ double *time_value
1046
+ ) {
1047
+ size_t cursor = 0u;
1048
+ int64_t year = 0;
1049
+ bool negative_year = false;
1050
+ if (cursor < length && (units[cursor] == '+' || units[cursor] == '-')) {
1051
+ negative_year = units[cursor] == '-';
1052
+ cursor += 1u;
1053
+ if (!date_read_digits(units, length, &cursor, 6u, &year)) {
1054
+ return false;
1055
+ }
1056
+ if (negative_year && year == 0) return false;
1057
+ if (negative_year) year = -year;
1058
+ } else if (!date_read_digits(units, length, &cursor, 4u, &year)) {
1059
+ return false;
1060
+ }
1061
+ int64_t month = 1;
1062
+ int64_t day = 1;
1063
+ if (cursor < length && units[cursor] == '-') {
1064
+ cursor += 1u;
1065
+ if (!date_read_digits(units, length, &cursor, 2u, &month)) {
1066
+ return false;
1067
+ }
1068
+ if (month < 1 || month > 12) return false;
1069
+ if (cursor < length && units[cursor] == '-') {
1070
+ cursor += 1u;
1071
+ if (!date_read_digits(units, length, &cursor, 2u, &day)) {
1072
+ return false;
1073
+ }
1074
+ if (day < 1 || day > 31) return false;
1075
+ }
1076
+ }
1077
+ int64_t hour = 0;
1078
+ int64_t minute = 0;
1079
+ int64_t second = 0;
1080
+ int64_t millisecond = 0;
1081
+ bool has_time = false;
1082
+ if (cursor < length && units[cursor] == 'T') {
1083
+ has_time = true;
1084
+ cursor += 1u;
1085
+ if (!date_read_digits(units, length, &cursor, 2u, &hour)) {
1086
+ return false;
1087
+ }
1088
+ if (hour > 24) return false;
1089
+ if (!date_expect(units, length, &cursor, ':')) return false;
1090
+ if (!date_read_digits(units, length, &cursor, 2u, &minute)) {
1091
+ return false;
1092
+ }
1093
+ if (minute > 59) return false;
1094
+ if (cursor < length && units[cursor] == ':') {
1095
+ cursor += 1u;
1096
+ if (!date_read_digits(units, length, &cursor, 2u, &second)) {
1097
+ return false;
1098
+ }
1099
+ if (second > 59) return false;
1100
+ if (cursor < length && units[cursor] == '.') {
1101
+ cursor += 1u;
1102
+ if (!date_read_digits(
1103
+ units,
1104
+ length,
1105
+ &cursor,
1106
+ 3u,
1107
+ &millisecond
1108
+ )) {
1109
+ return false;
1110
+ }
1111
+ }
1112
+ }
1113
+ if (hour == 24 &&
1114
+ (minute != 0 || second != 0 || millisecond != 0)) {
1115
+ return false;
1116
+ }
1117
+ }
1118
+ /*
1119
+ * A UTC offset representation is appended to a date-time form only,
1120
+ * so a date-only form followed by `Z` or by a numeric offset is not
1121
+ * a production of the format even though both reference hosts accept
1122
+ * one.
1123
+ */
1124
+ double offset_minutes = 0.0;
1125
+ if (cursor < length && !has_time) return false;
1126
+ if (cursor < length) {
1127
+ if (units[cursor] == 'Z') {
1128
+ cursor += 1u;
1129
+ } else if (units[cursor] == '+' || units[cursor] == '-') {
1130
+ bool negative_offset = units[cursor] == '-';
1131
+ cursor += 1u;
1132
+ int64_t offset_hour = 0;
1133
+ int64_t offset_minute = 0;
1134
+ if (!date_read_digits(units, length, &cursor, 2u, &offset_hour)) {
1135
+ return false;
1136
+ }
1137
+ if (offset_hour > 23) return false;
1138
+ if (!date_expect(units, length, &cursor, ':')) return false;
1139
+ if (!date_read_digits(
1140
+ units,
1141
+ length,
1142
+ &cursor,
1143
+ 2u,
1144
+ &offset_minute
1145
+ )) {
1146
+ return false;
1147
+ }
1148
+ if (offset_minute > 59) return false;
1149
+ offset_minutes =
1150
+ (double)(offset_hour * 60 + offset_minute);
1151
+ if (negative_offset) offset_minutes = -offset_minutes;
1152
+ } else {
1153
+ return false;
1154
+ }
1155
+ } else if (has_time) {
1156
+ /* An offsetless date-time form is local time, and this realm's
1157
+ * local time zone is UTC, so the two resolutions coincide. */
1158
+ offset_minutes = -date_local_time_zone_adjustment() /
1159
+ OSEO_MS_PER_MINUTE;
1160
+ }
1161
+ if (cursor != length) return false;
1162
+ double composed = date_make_date(
1163
+ date_make_day((double)year, (double)(month - 1), (double)day),
1164
+ date_make_time(
1165
+ (double)hour,
1166
+ (double)minute,
1167
+ (double)second,
1168
+ (double)millisecond
1169
+ )
1170
+ );
1171
+ *time_value = date_time_clip(
1172
+ composed - offset_minutes * OSEO_MS_PER_MINUTE
1173
+ );
1174
+ return true;
1175
+ }
1176
+
1177
+ /* One three-letter name of `names`, or -1 when the text does not match. */
1178
+ static int64_t date_named_index(
1179
+ const uint16_t *units,
1180
+ size_t length,
1181
+ size_t cursor,
1182
+ const char *const *names,
1183
+ size_t count
1184
+ ) {
1185
+ if (cursor + 3u > length) return -1;
1186
+ for (size_t index = 0u; index < count; index += 1u) {
1187
+ bool matched = true;
1188
+ for (size_t unit = 0u; unit < 3u; unit += 1u) {
1189
+ if (units[cursor + unit] !=
1190
+ (uint16_t)(unsigned char)names[index][unit]) {
1191
+ matched = false;
1192
+ break;
1193
+ }
1194
+ }
1195
+ if (matched) return (int64_t)index;
1196
+ }
1197
+ return -1;
1198
+ }
1199
+
1200
+ /*
1201
+ * The two texts 21.4.3.2 asks Date.parse to recover: this realm's own
1202
+ * `Date.prototype.toString` and `Date.prototype.toUTCString` output.
1203
+ * Both are recognized exactly, including the weekday, which must agree
1204
+ * with the date it precedes, so no other text reaches this fallback.
1205
+ */
1206
+ static bool date_parse_own_format(
1207
+ const uint16_t *units,
1208
+ size_t length,
1209
+ double *time_value
1210
+ ) {
1211
+ int64_t weekday = date_named_index(
1212
+ units,
1213
+ length,
1214
+ 0u,
1215
+ date_weekday_names,
1216
+ 7u
1217
+ );
1218
+ if (weekday < 0) return false;
1219
+ size_t cursor = 3u;
1220
+ bool utc_format = cursor < length && units[cursor] == ',';
1221
+ if (utc_format) cursor += 1u;
1222
+ if (!date_expect(units, length, &cursor, ' ')) return false;
1223
+ int64_t month = 0;
1224
+ int64_t day = 0;
1225
+ if (utc_format) {
1226
+ if (!date_read_digits(units, length, &cursor, 2u, &day)) return false;
1227
+ if (!date_expect(units, length, &cursor, ' ')) return false;
1228
+ month = date_named_index(units, length, cursor, date_month_names, 12u);
1229
+ if (month < 0) return false;
1230
+ cursor += 3u;
1231
+ } else {
1232
+ month = date_named_index(units, length, cursor, date_month_names, 12u);
1233
+ if (month < 0) return false;
1234
+ cursor += 3u;
1235
+ if (!date_expect(units, length, &cursor, ' ')) return false;
1236
+ if (!date_read_digits(units, length, &cursor, 2u, &day)) return false;
1237
+ }
1238
+ if (!date_expect(units, length, &cursor, ' ')) return false;
1239
+ bool negative_year = cursor < length && units[cursor] == '-';
1240
+ if (negative_year) cursor += 1u;
1241
+ /*
1242
+ * `DateString` pads the year to four digits and never writes more
1243
+ * than the six an expanded year needs, so the digit count is bounded
1244
+ * before any digit is accumulated and no product can overflow.
1245
+ */
1246
+ size_t year_digits = 0u;
1247
+ while (date_digit(units, length, cursor + year_digits) >= 0) {
1248
+ year_digits += 1u;
1249
+ }
1250
+ if (year_digits < 4u || year_digits > 6u) return false;
1251
+ int64_t year = 0;
1252
+ if (!date_read_digits(units, length, &cursor, year_digits, &year)) {
1253
+ return false;
1254
+ }
1255
+ if (negative_year) year = -year;
1256
+ if (!date_expect(units, length, &cursor, ' ')) return false;
1257
+ int64_t hour = 0;
1258
+ int64_t minute = 0;
1259
+ int64_t second = 0;
1260
+ if (!date_read_digits(units, length, &cursor, 2u, &hour)) return false;
1261
+ if (!date_expect(units, length, &cursor, ':')) return false;
1262
+ if (!date_read_digits(units, length, &cursor, 2u, &minute)) return false;
1263
+ if (!date_expect(units, length, &cursor, ':')) return false;
1264
+ if (!date_read_digits(units, length, &cursor, 2u, &second)) return false;
1265
+ if (hour > 24 || minute > 59 || second > 59) return false;
1266
+ if (!date_expect(units, length, &cursor, ' ')) return false;
1267
+ static const char gmt[] = "GMT";
1268
+ for (size_t index = 0u; index < 3u; index += 1u) {
1269
+ if (!date_expect(
1270
+ units,
1271
+ length,
1272
+ &cursor,
1273
+ (uint16_t)(unsigned char)gmt[index]
1274
+ )) {
1275
+ return false;
1276
+ }
1277
+ }
1278
+ double offset_minutes = 0.0;
1279
+ if (!utc_format) {
1280
+ bool negative_offset = cursor < length && units[cursor] == '-';
1281
+ if (!negative_offset &&
1282
+ !date_expect(units, length, &cursor, '+')) {
1283
+ return false;
1284
+ }
1285
+ if (negative_offset) cursor += 1u;
1286
+ int64_t offset_hour = 0;
1287
+ int64_t offset_minute = 0;
1288
+ if (!date_read_digits(units, length, &cursor, 2u, &offset_hour)) {
1289
+ return false;
1290
+ }
1291
+ if (!date_read_digits(units, length, &cursor, 2u, &offset_minute)) {
1292
+ return false;
1293
+ }
1294
+ if (offset_hour > 23 || offset_minute > 59) return false;
1295
+ offset_minutes = (double)(offset_hour * 60 + offset_minute);
1296
+ if (negative_offset) offset_minutes = -offset_minutes;
1297
+ /* The parenthesized zone name is the implementation-defined part
1298
+ * of TimeZoneString, so it is accepted and ignored. */
1299
+ if (cursor < length) {
1300
+ if (!date_expect(units, length, &cursor, ' ')) return false;
1301
+ if (!date_expect(units, length, &cursor, '(')) return false;
1302
+ if (length == 0u || units[length - 1u] != ')') return false;
1303
+ cursor = length;
1304
+ }
1305
+ }
1306
+ if (cursor != length) return false;
1307
+ double day_number = date_make_day(
1308
+ (double)year,
1309
+ (double)month,
1310
+ (double)day
1311
+ );
1312
+ if (isnan(day_number)) return false;
1313
+ if (date_modulo(day_number + 4.0, 7.0) != (double)weekday) return false;
1314
+ double composed = date_make_date(
1315
+ day_number,
1316
+ date_make_time((double)hour, (double)minute, (double)second, 0.0)
1317
+ );
1318
+ *time_value = date_time_clip(
1319
+ composed - offset_minutes * OSEO_MS_PER_MINUTE
1320
+ );
1321
+ return true;
1322
+ }
1323
+
1324
+ /* The complete parse of 21.4.3.2 over one already converted String. */
1325
+ static double date_parse_string(OseoValue text) {
1326
+ const OseoString *string = string_object(text);
1327
+ double time_value = (double)NAN;
1328
+ if (date_parse_iso(string->units, string->length, &time_value)) {
1329
+ return time_value;
1330
+ }
1331
+ if (date_parse_own_format(string->units, string->length, &time_value)) {
1332
+ return time_value;
1333
+ }
1334
+ return (double)NAN;
1335
+ }
1336
+
1337
+ /*
1338
+ * OrdinaryCreateFromConstructor(newTarget, "%Date.prototype%"). The
1339
+ * `prototype` read is the specified Get, so a new target whose property
1340
+ * is an accessor runs it, and a non-object result falls back to the
1341
+ * realm prototype.
1342
+ */
1343
+ static OseoResult date_prototype_from_target(
1344
+ OseoContext *context,
1345
+ OseoValue new_target,
1346
+ OseoValue *prototype
1347
+ ) {
1348
+ OseoRootFrame frame = {NULL, NULL, 0u};
1349
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
1350
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1351
+ frame.slots[0] = new_target;
1352
+ result = oseo_internal_constructor_prototype(context, frame.slots[0]);
1353
+ frame.slots[1] = result.value;
1354
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[1])) {
1355
+ result = oseo_internal_constructor_realm_default(
1356
+ context,
1357
+ frame.slots[0],
1358
+ OSEO_INTRINSIC_DATE_PROTOTYPE
1359
+ );
1360
+ frame.slots[1] = result.value;
1361
+ }
1362
+ if (result.status == OSEO_STATUS_NORMAL) *prototype = frame.slots[1];
1363
+ oseo_roots_release(context, &frame);
1364
+ return result;
1365
+ }
1366
+
1367
+ /*
1368
+ * One Date record. The time value is published with the record, so a
1369
+ * Date never exists with an unset [[DateValue]].
1370
+ */
1371
+ static OseoResult date_allocate(
1372
+ OseoContext *context,
1373
+ OseoValue prototype,
1374
+ double time_value
1375
+ ) {
1376
+ OseoValue slots[1] = {prototype};
1377
+ OseoRootFrame frame = {NULL, slots, 1u};
1378
+ oseo_roots_push(context, &frame);
1379
+ OseoDate *date = oseo_internal_allocate_heap_bytes(context, sizeof(*date));
1380
+ if (date == NULL) {
1381
+ oseo_roots_pop(context, &frame);
1382
+ return failure(context, "OSEO2001", "Date allocation failed.");
1383
+ }
1384
+ date->ordinary.prototype = slots[0];
1385
+ date->ordinary.properties = NULL;
1386
+ date->ordinary.property_capacity = 0u;
1387
+ date->ordinary.property_count = 0u;
1388
+ oseo_internal_init_dense_index(&date->ordinary);
1389
+ date->ordinary.private_elements = NULL;
1390
+ date->ordinary.private_element_capacity = 0u;
1391
+ date->ordinary.private_element_count = 0u;
1392
+ date->ordinary.shape_id = context->next_shape_id;
1393
+ context->next_shape_id += 1u;
1394
+ date->ordinary.array_length = 0u;
1395
+ date->ordinary.dictionary = false;
1396
+ date->ordinary.length_writable = false;
1397
+ date->ordinary.extensible = true;
1398
+ date->ordinary.module_namespace = false;
1399
+ date->ordinary.immutable_prototype = false;
1400
+ date->ordinary.global_object = false;
1401
+ date->ordinary.error_data = false;
1402
+ date->ordinary.number_data = false;
1403
+ date->ordinary.number_value = oseo_undefined();
1404
+ date->ordinary.primitive_data = false;
1405
+ date->ordinary.primitive_value = oseo_undefined();
1406
+ date->ordinary.primitive_wrapper_methods_initialized = false;
1407
+ date->ordinary.virtual_string_iterator = false;
1408
+ date->ordinary.virtual_string_iterator_configurable = false;
1409
+ date->ordinary.virtual_string_iterator_enumerable = false;
1410
+ date->ordinary.virtual_string_iterator_writable = false;
1411
+ date->ordinary.iterator_kind = OSEO_ITERATOR_NONE;
1412
+ date->ordinary.iterator_target = oseo_undefined();
1413
+ date->ordinary.iterator_index = 0u;
1414
+ date->ordinary.regexp_string_iterator = false;
1415
+ date->ordinary.regexp_iterator_regexp = oseo_undefined();
1416
+ date->ordinary.regexp_iterator_subject = oseo_undefined();
1417
+ date->ordinary.regexp_iterator_global = false;
1418
+ date->ordinary.regexp_iterator_unicode = false;
1419
+ date->ordinary.regexp_iterator_complete = false;
1420
+ date->ordinary.async_from_sync = false;
1421
+ date->ordinary.async_sync_iterator = oseo_undefined();
1422
+ date->ordinary.wrap_for_valid_iterator = false;
1423
+ date->ordinary.wrapped_iterator = oseo_undefined();
1424
+ date->ordinary.wrapped_next = oseo_undefined();
1425
+ date->ordinary.generator = NULL;
1426
+ date->ordinary.arguments_object = false;
1427
+ date->ordinary.mapped_arguments = false;
1428
+ date->time_value = time_value;
1429
+ OseoResult published = oseo_internal_publish_heap(
1430
+ context,
1431
+ &date->ordinary.header,
1432
+ OSEO_HEAP_DATE
1433
+ );
1434
+ oseo_roots_pop(context, &frame);
1435
+ return published;
1436
+ }
1437
+
1438
+ /*
1439
+ * The Date constructor, 21.4.2.1. Every argument conversion runs before
1440
+ * the new target's `prototype` read, which is the position clause 21.4.2.1
1441
+ * gives OrdinaryCreateFromConstructor. A one-argument call copies an
1442
+ * existing Date's [[DateValue]] without converting it, so a Date whose
1443
+ * `valueOf` a program replaced still copies its slot.
1444
+ */
1445
+ static OseoResult date_construct(
1446
+ OseoContext *context,
1447
+ size_t argument_count,
1448
+ const OseoValue *arguments,
1449
+ OseoValue new_target
1450
+ ) {
1451
+ double time_value = (double)NAN;
1452
+ OseoRootFrame frame = {NULL, NULL, 0u};
1453
+ OseoResult result = oseo_roots_allocate(context, &frame, 8u);
1454
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1455
+ frame.slots[0] = new_target;
1456
+ if (argument_count == 0u) {
1457
+ result = date_current_time_value(context, &time_value);
1458
+ if (result.status != OSEO_STATUS_NORMAL) {
1459
+ oseo_roots_release(context, &frame);
1460
+ return result;
1461
+ }
1462
+ } else if (argument_count == 1u) {
1463
+ frame.slots[1] = arguments[0];
1464
+ if (is_date(frame.slots[1])) {
1465
+ time_value = date_time_clip(
1466
+ date_object(frame.slots[1])->time_value
1467
+ );
1468
+ } else {
1469
+ result = oseo_internal_to_primitive(
1470
+ context,
1471
+ frame.slots[1],
1472
+ OSEO_TO_PRIMITIVE_DEFAULT
1473
+ );
1474
+ frame.slots[1] = result.value;
1475
+ if (result.status != OSEO_STATUS_NORMAL) {
1476
+ oseo_roots_release(context, &frame);
1477
+ return result;
1478
+ }
1479
+ if (is_string(frame.slots[1])) {
1480
+ time_value = date_time_clip(
1481
+ date_parse_string(frame.slots[1])
1482
+ );
1483
+ } else {
1484
+ result = oseo_internal_to_number(context, frame.slots[1]);
1485
+ if (result.status != OSEO_STATUS_NORMAL) {
1486
+ oseo_roots_release(context, &frame);
1487
+ return result;
1488
+ }
1489
+ time_value = date_time_clip(number_value(result.value));
1490
+ }
1491
+ }
1492
+ } else {
1493
+ double components[OSEO_DATE_FIELD_COUNT] = {
1494
+ 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
1495
+ };
1496
+ for (size_t index = 0u;
1497
+ index < OSEO_DATE_FIELD_COUNT && index < argument_count;
1498
+ index += 1u) {
1499
+ result = oseo_internal_to_number(context, arguments[index]);
1500
+ if (result.status != OSEO_STATUS_NORMAL) {
1501
+ oseo_roots_release(context, &frame);
1502
+ return result;
1503
+ }
1504
+ components[index] = number_value(result.value);
1505
+ }
1506
+ double resolved_year =
1507
+ date_make_full_year(components[OSEO_DATE_FIELD_YEAR]);
1508
+ double composed = date_make_date(
1509
+ date_make_day(
1510
+ resolved_year,
1511
+ components[OSEO_DATE_FIELD_MONTH],
1512
+ components[OSEO_DATE_FIELD_DAY]
1513
+ ),
1514
+ date_make_time(
1515
+ components[OSEO_DATE_FIELD_HOUR],
1516
+ components[OSEO_DATE_FIELD_MINUTE],
1517
+ components[OSEO_DATE_FIELD_SECOND],
1518
+ components[OSEO_DATE_FIELD_MILLISECOND]
1519
+ )
1520
+ );
1521
+ time_value = date_time_clip(date_utc_time(composed));
1522
+ }
1523
+ result = date_prototype_from_target(
1524
+ context,
1525
+ frame.slots[0],
1526
+ &frame.slots[2]
1527
+ );
1528
+ if (result.status == OSEO_STATUS_NORMAL) {
1529
+ result = date_allocate(context, frame.slots[2], time_value);
1530
+ }
1531
+ oseo_roots_release(context, &frame);
1532
+ return result;
1533
+ }
1534
+
1535
+ /* Date(), 21.4.2.1 step 1: the current time as a String, ignoring every
1536
+ * argument. */
1537
+ static OseoResult date_call(OseoContext *context) {
1538
+ double time_value = 0.0;
1539
+ OseoResult result = date_current_time_value(context, &time_value);
1540
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1541
+ return date_to_date_string(context, time_value, OSEO_DATE_TO_STRING);
1542
+ }
1543
+
1544
+ /* Date.now(), 21.4.3.1. */
1545
+ static OseoResult date_now(OseoContext *context) {
1546
+ double time_value = 0.0;
1547
+ OseoResult result = date_current_time_value(context, &time_value);
1548
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1549
+ return normal(oseo_number(time_value));
1550
+ }
1551
+
1552
+ /* Date.parse(string), 21.4.3.2. */
1553
+ static OseoResult date_parse(
1554
+ OseoContext *context,
1555
+ size_t argument_count,
1556
+ const OseoValue *arguments
1557
+ ) {
1558
+ OseoResult text = oseo_to_string(
1559
+ context,
1560
+ date_argument(argument_count, arguments, 0u)
1561
+ );
1562
+ if (text.status != OSEO_STATUS_NORMAL) return text;
1563
+ return normal(oseo_number(date_parse_string(text.value)));
1564
+ }
1565
+
1566
+ /* Date.UTC(year, month, date, hours, minutes, seconds, ms), 21.4.3.4. */
1567
+ static OseoResult date_utc(
1568
+ OseoContext *context,
1569
+ size_t argument_count,
1570
+ const OseoValue *arguments
1571
+ ) {
1572
+ double components[OSEO_DATE_FIELD_COUNT] = {
1573
+ (double)NAN, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
1574
+ };
1575
+ /* `year` is converted whether or not the call supplied it; every
1576
+ * later parameter keeps its specified default when absent. */
1577
+ for (size_t index = 0u;
1578
+ index < OSEO_DATE_FIELD_COUNT &&
1579
+ (index == 0u || index < argument_count);
1580
+ index += 1u) {
1581
+ OseoResult converted = oseo_internal_to_number(
1582
+ context,
1583
+ date_argument(argument_count, arguments, index)
1584
+ );
1585
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
1586
+ components[index] = number_value(converted.value);
1587
+ }
1588
+ double resolved_year =
1589
+ date_make_full_year(components[OSEO_DATE_FIELD_YEAR]);
1590
+ double composed = date_make_date(
1591
+ date_make_day(
1592
+ resolved_year,
1593
+ components[OSEO_DATE_FIELD_MONTH],
1594
+ components[OSEO_DATE_FIELD_DAY]
1595
+ ),
1596
+ date_make_time(
1597
+ components[OSEO_DATE_FIELD_HOUR],
1598
+ components[OSEO_DATE_FIELD_MINUTE],
1599
+ components[OSEO_DATE_FIELD_SECOND],
1600
+ components[OSEO_DATE_FIELD_MILLISECOND]
1601
+ )
1602
+ );
1603
+ return normal(oseo_number(date_time_clip(composed)));
1604
+ }
1605
+
1606
+ /* One prototype method, dispatched by its table index. */
1607
+ static OseoResult date_prototype_method(
1608
+ OseoContext *context,
1609
+ OseoDateMethod method,
1610
+ OseoValue receiver,
1611
+ size_t argument_count,
1612
+ const OseoValue *arguments
1613
+ ) {
1614
+ if (method == OSEO_DATE_GET_YEAR) {
1615
+ return date_get_year(context, receiver);
1616
+ }
1617
+ if (method == OSEO_DATE_SET_YEAR) {
1618
+ return date_set_year(context, receiver, argument_count, arguments);
1619
+ }
1620
+ if (method <= OSEO_DATE_GET_UTC_SECONDS) {
1621
+ return date_get_field(context, receiver, method);
1622
+ }
1623
+ if (method == OSEO_DATE_SET_TIME) {
1624
+ return date_set_time(context, receiver, argument_count, arguments);
1625
+ }
1626
+ if (method <= OSEO_DATE_SET_UTC_SECONDS) {
1627
+ return date_set_fields(
1628
+ context,
1629
+ receiver,
1630
+ method,
1631
+ argument_count,
1632
+ arguments
1633
+ );
1634
+ }
1635
+ if (method == OSEO_DATE_TO_JSON) {
1636
+ return date_to_json(context, receiver);
1637
+ }
1638
+ if (method == OSEO_DATE_TO_PRIMITIVE) {
1639
+ return date_to_primitive(
1640
+ context,
1641
+ receiver,
1642
+ argument_count,
1643
+ arguments
1644
+ );
1645
+ }
1646
+ double time_value = 0.0;
1647
+ OseoResult required = date_receiver(context, receiver, &time_value);
1648
+ if (required.status != OSEO_STATUS_NORMAL) return required;
1649
+ switch (method) {
1650
+ case OSEO_DATE_VALUE_OF:
1651
+ return normal(oseo_number(time_value));
1652
+ case OSEO_DATE_TO_ISO_STRING:
1653
+ return date_to_iso_string(context, time_value);
1654
+ case OSEO_DATE_TO_UTC_STRING:
1655
+ return date_to_utc_string(context, time_value);
1656
+ case OSEO_DATE_TO_DATE_STRING:
1657
+ case OSEO_DATE_TO_LOCALE_DATE_STRING:
1658
+ return date_to_date_string(
1659
+ context,
1660
+ time_value,
1661
+ OSEO_DATE_TO_DATE_STRING
1662
+ );
1663
+ case OSEO_DATE_TO_TIME_STRING:
1664
+ case OSEO_DATE_TO_LOCALE_TIME_STRING:
1665
+ return date_to_date_string(
1666
+ context,
1667
+ time_value,
1668
+ OSEO_DATE_TO_TIME_STRING
1669
+ );
1670
+ default:
1671
+ /* toString and the two remaining locale methods. ECMA-402 is
1672
+ * outside the claim, so each locale method reports the same
1673
+ * text as the operation it localizes. */
1674
+ return date_to_date_string(
1675
+ context,
1676
+ time_value,
1677
+ OSEO_DATE_TO_STRING
1678
+ );
1679
+ }
1680
+ }
1681
+
1682
+ OseoResult oseo_internal_date_builtin_dispatch(
1683
+ OseoContext *context,
1684
+ size_t code_id,
1685
+ OseoValue callee,
1686
+ OseoValue receiver,
1687
+ size_t argument_count,
1688
+ const OseoValue *arguments,
1689
+ OseoValue new_target
1690
+ ) {
1691
+ (void)callee;
1692
+ if (code_id == OSEO_DATE_CONSTRUCTOR_CODE_ID) {
1693
+ if (tag_of(new_target) == OSEO_TAG_UNDEFINED) {
1694
+ return date_call(context);
1695
+ }
1696
+ return date_construct(
1697
+ context,
1698
+ argument_count,
1699
+ arguments,
1700
+ new_target
1701
+ );
1702
+ }
1703
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
1704
+ return oseo_internal_throw_error(
1705
+ context,
1706
+ OSEO_ERROR_TYPE,
1707
+ "Date method is not a constructor."
1708
+ );
1709
+ }
1710
+ if (code_id == OSEO_DATE_NOW_CODE_ID) return date_now(context);
1711
+ if (code_id == OSEO_DATE_PARSE_CODE_ID) {
1712
+ return date_parse(context, argument_count, arguments);
1713
+ }
1714
+ if (code_id == OSEO_DATE_UTC_CODE_ID) {
1715
+ return date_utc(context, argument_count, arguments);
1716
+ }
1717
+ if (code_id >= OSEO_DATE_METHOD_CODE_ID_FIRST &&
1718
+ code_id <= OSEO_DATE_METHOD_CODE_ID_LAST) {
1719
+ return date_prototype_method(
1720
+ context,
1721
+ (OseoDateMethod)(OSEO_DATE_METHOD_CODE_ID_LAST - code_id),
1722
+ receiver,
1723
+ argument_count,
1724
+ arguments
1725
+ );
1726
+ }
1727
+ return oseo_unknown_function(context, code_id);
1728
+ }
1729
+
1730
+ static OseoResult create_date_builtin(
1731
+ OseoContext *context,
1732
+ size_t code_id,
1733
+ const char *name,
1734
+ size_t length,
1735
+ OseoFunctionKind kind
1736
+ ) {
1737
+ size_t name_length = strlen(name);
1738
+ uint16_t units[32];
1739
+ if (name_length > sizeof(units) / sizeof(*units)) {
1740
+ return failure(context, "OSEO2001", "Built-in name is too long.");
1741
+ }
1742
+ for (size_t index = 0u; index < name_length; index += 1u) {
1743
+ units[index] = (uint16_t)(unsigned char)name[index];
1744
+ }
1745
+ OseoValue environment = oseo_undefined();
1746
+ OseoRootFrame frame = {NULL, &environment, 1u};
1747
+ oseo_roots_push(context, &frame);
1748
+ OseoResult result = oseo_environment_create(context, 0u);
1749
+ environment = result.value;
1750
+ if (result.status == OSEO_STATUS_NORMAL) {
1751
+ result = oseo_function_create(
1752
+ context,
1753
+ code_id,
1754
+ environment,
1755
+ units,
1756
+ name_length,
1757
+ length,
1758
+ kind,
1759
+ oseo_undefined(),
1760
+ oseo_undefined(),
1761
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1762
+ );
1763
+ }
1764
+ oseo_roots_pop(context, &frame);
1765
+ return result;
1766
+ }
1767
+
1768
+ static OseoResult define_date_property(
1769
+ OseoContext *context,
1770
+ OseoValue object,
1771
+ const char *name,
1772
+ OseoValue value,
1773
+ OseoPropertyAttributes attributes
1774
+ ) {
1775
+ OseoValue slots[3] = {object, value, oseo_undefined()};
1776
+ OseoRootFrame frame = {NULL, slots, 3u};
1777
+ oseo_roots_push(context, &frame);
1778
+ OseoResult result = oseo_internal_ascii_string(context, name);
1779
+ slots[2] = result.value;
1780
+ if (result.status == OSEO_STATUS_NORMAL) {
1781
+ result = oseo_object_define(
1782
+ context,
1783
+ slots[0],
1784
+ slots[2],
1785
+ slots[1],
1786
+ attributes
1787
+ );
1788
+ }
1789
+ oseo_roots_pop(context, &frame);
1790
+ return result;
1791
+ }
1792
+
1793
+ /*
1794
+ * Materializes %Date%, %Date.prototype%, and every own property
1795
+ * ECMA-262 gives them. `OSEO_INTRINSIC_DATE` is filled last, so it
1796
+ * doubles as the completion marker: a partially built cluster leaves it
1797
+ * undefined and the failure path clears both slots. The marker holds the
1798
+ * uninitialized sentinel while the attempt runs, so a dependency that
1799
+ * reentered the build reports that instead of splitting the constructor
1800
+ * and prototype identities across two attempts.
1801
+ */
1802
+ static OseoResult date_intrinsic_build(OseoContext *context) {
1803
+ OseoValue *marker = &context->realm->intrinsics[OSEO_INTRINSIC_DATE];
1804
+ if (tag_of(*marker) == OSEO_TAG_UNINITIALIZED) {
1805
+ return failure(
1806
+ context,
1807
+ "OSEO2001",
1808
+ "The Date intrinsic cluster is already being built."
1809
+ );
1810
+ }
1811
+ if (tag_of(*marker) != OSEO_TAG_UNDEFINED) return normal(*marker);
1812
+ size_t entry_allocations = context->allocations;
1813
+ OseoRootFrame frame = {NULL, NULL, 0u};
1814
+ /* Slot 4 keeps toUTCString for the Annex B toGMTString property. */
1815
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
1816
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1817
+ *marker = oseo_uninitialized();
1818
+ result = oseo_internal_intrinsic(
1819
+ context,
1820
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
1821
+ );
1822
+ frame.slots[0] = result.value;
1823
+ if (result.status == OSEO_STATUS_NORMAL) {
1824
+ result = oseo_object_create(context, frame.slots[0]);
1825
+ frame.slots[0] = result.value;
1826
+ }
1827
+ if (result.status == OSEO_STATUS_NORMAL) {
1828
+ context->realm->intrinsics[OSEO_INTRINSIC_DATE_PROTOTYPE] =
1829
+ frame.slots[0];
1830
+ result = create_date_builtin(
1831
+ context,
1832
+ OSEO_DATE_CONSTRUCTOR_CODE_ID,
1833
+ "Date",
1834
+ 7u,
1835
+ OSEO_FUNCTION_ORDINARY
1836
+ );
1837
+ frame.slots[1] = result.value;
1838
+ }
1839
+ const OseoPropertyAttributes method = {true, false, true, false};
1840
+ if (result.status == OSEO_STATUS_NORMAL) {
1841
+ OseoFunction *constructor = function_object(frame.slots[1]);
1842
+ constructor->prototype_object = frame.slots[0];
1843
+ constructor->prototype_writable = false;
1844
+ result = define_date_property(
1845
+ context,
1846
+ frame.slots[0],
1847
+ "constructor",
1848
+ frame.slots[1],
1849
+ method
1850
+ );
1851
+ }
1852
+ static const char *const static_names[] = {"now", "parse", "UTC"};
1853
+ static const size_t static_codes[] = {
1854
+ OSEO_DATE_NOW_CODE_ID,
1855
+ OSEO_DATE_PARSE_CODE_ID,
1856
+ OSEO_DATE_UTC_CODE_ID,
1857
+ };
1858
+ static const size_t static_lengths[] = {0u, 1u, 7u};
1859
+ for (size_t index = 0u;
1860
+ result.status == OSEO_STATUS_NORMAL && index < 3u;
1861
+ index += 1u) {
1862
+ result = create_date_builtin(
1863
+ context,
1864
+ static_codes[index],
1865
+ static_names[index],
1866
+ static_lengths[index],
1867
+ OSEO_FUNCTION_INTERNAL
1868
+ );
1869
+ frame.slots[2] = result.value;
1870
+ if (result.status != OSEO_STATUS_NORMAL) break;
1871
+ result = define_date_property(
1872
+ context,
1873
+ frame.slots[1],
1874
+ static_names[index],
1875
+ frame.slots[2],
1876
+ method
1877
+ );
1878
+ }
1879
+ for (size_t index = 0u;
1880
+ result.status == OSEO_STATUS_NORMAL &&
1881
+ index < (size_t)OSEO_DATE_METHOD_COUNT;
1882
+ index += 1u) {
1883
+ result = create_date_builtin(
1884
+ context,
1885
+ OSEO_DATE_METHOD_CODE_ID_LAST - index,
1886
+ date_methods[index].name,
1887
+ date_methods[index].length,
1888
+ OSEO_FUNCTION_INTERNAL
1889
+ );
1890
+ frame.slots[2] = result.value;
1891
+ if (result.status != OSEO_STATUS_NORMAL) break;
1892
+ if (index == (size_t)OSEO_DATE_TO_UTC_STRING) {
1893
+ frame.slots[4] = frame.slots[2];
1894
+ }
1895
+ if (index != (size_t)OSEO_DATE_TO_PRIMITIVE) {
1896
+ result = define_date_property(
1897
+ context,
1898
+ frame.slots[0],
1899
+ date_methods[index].name,
1900
+ frame.slots[2],
1901
+ method
1902
+ );
1903
+ continue;
1904
+ }
1905
+ result = oseo_internal_well_known_symbol(
1906
+ context,
1907
+ OSEO_WELL_KNOWN_TO_PRIMITIVE
1908
+ );
1909
+ frame.slots[3] = result.value;
1910
+ if (result.status != OSEO_STATUS_NORMAL) break;
1911
+ result = oseo_object_define(
1912
+ context,
1913
+ frame.slots[0],
1914
+ frame.slots[3],
1915
+ frame.slots[2],
1916
+ (OseoPropertyAttributes){true, false, false, false}
1917
+ );
1918
+ }
1919
+ /*
1920
+ * B.2.3.3: the initial value of toGMTString is the toUTCString
1921
+ * function object itself, so the property keeps that name.
1922
+ */
1923
+ if (result.status == OSEO_STATUS_NORMAL) {
1924
+ result = define_date_property(
1925
+ context,
1926
+ frame.slots[0],
1927
+ "toGMTString",
1928
+ frame.slots[4],
1929
+ method
1930
+ );
1931
+ }
1932
+ if (result.status != OSEO_STATUS_NORMAL) {
1933
+ context->realm->intrinsics[OSEO_INTRINSIC_DATE_PROTOTYPE] =
1934
+ oseo_undefined();
1935
+ *marker = oseo_undefined();
1936
+ oseo_roots_release(context, &frame);
1937
+ return result;
1938
+ }
1939
+ *marker = frame.slots[1];
1940
+ if (context->observe_specialization) {
1941
+ context->allocations = entry_allocations;
1942
+ }
1943
+ oseo_roots_release(context, &frame);
1944
+ return normal(*marker);
1945
+ }
1946
+
1947
+ OseoResult oseo_internal_date_intrinsic(OseoContext *context) {
1948
+ OseoResult built = date_intrinsic_build(context);
1949
+ if (built.status != OSEO_STATUS_NORMAL) return built;
1950
+ return normal(context->realm->intrinsics[OSEO_INTRINSIC_DATE]);
1951
+ }
1952
+
1953
+ OseoResult oseo_internal_install_date_global(
1954
+ OseoContext *context,
1955
+ OseoValue global
1956
+ ) {
1957
+ OseoValue slots[2] = {global, oseo_undefined()};
1958
+ OseoRootFrame frame = {NULL, slots, 2u};
1959
+ oseo_roots_push(context, &frame);
1960
+ OseoResult result = oseo_internal_date_intrinsic(context);
1961
+ slots[1] = result.value;
1962
+ if (result.status == OSEO_STATUS_NORMAL) {
1963
+ result = define_date_property(
1964
+ context,
1965
+ slots[0],
1966
+ "Date",
1967
+ slots[1],
1968
+ (OseoPropertyAttributes){true, false, true, false}
1969
+ );
1970
+ }
1971
+ oseo_roots_pop(context, &frame);
1972
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
1973
+ }