@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.370

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