@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,2244 @@
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 <stdlib.h>
9
+ #include <string.h>
10
+
11
+ /*
12
+ * Coercions, arithmetic, comparison, string conversion, and
13
+ * console output.
14
+ */
15
+
16
+ static bool numeric_whitespace(uint16_t unit) {
17
+ return unit == UINT16_C(0x0009) || unit == UINT16_C(0x000a) ||
18
+ unit == UINT16_C(0x000b) || unit == UINT16_C(0x000c) ||
19
+ unit == UINT16_C(0x000d) || unit == UINT16_C(0x0020) ||
20
+ unit == UINT16_C(0x00a0) || unit == UINT16_C(0x1680) ||
21
+ (unit >= UINT16_C(0x2000) && unit <= UINT16_C(0x200a)) ||
22
+ unit == UINT16_C(0x2028) || unit == UINT16_C(0x2029) ||
23
+ unit == UINT16_C(0x202f) || unit == UINT16_C(0x205f) ||
24
+ unit == UINT16_C(0x3000) || unit == UINT16_C(0xfeff);
25
+ }
26
+
27
+ static int radix_digit(char character) {
28
+ if (character >= '0' && character <= '9') return character - '0';
29
+ if (character >= 'a' && character <= 'f') {
30
+ return character - 'a' + 10;
31
+ }
32
+ if (character >= 'A' && character <= 'F') {
33
+ return character - 'A' + 10;
34
+ }
35
+ return -1;
36
+ }
37
+
38
+ static double prefixed_integer(
39
+ const char *text,
40
+ size_t length,
41
+ int radix
42
+ ) {
43
+ if (length <= 2u) return NAN;
44
+ int bits_per_digit = radix == 2 ? 1 : radix == 8 ? 3 : 4;
45
+ size_t first_nonzero = length;
46
+ for (size_t index = 2u; index < length; index += 1u) {
47
+ int digit = radix_digit(text[index]);
48
+ if (digit < 0 || digit >= radix) return NAN;
49
+ if (first_nonzero == length && digit != 0) first_nonzero = index;
50
+ }
51
+ if (first_nonzero == length) return 0.0;
52
+
53
+ int first_digit = radix_digit(text[first_nonzero]);
54
+ size_t first_bits = 0u;
55
+ for (int digit = first_digit; digit != 0; digit >>= 1) {
56
+ first_bits += 1u;
57
+ }
58
+ size_t remaining_digits = length - first_nonzero - 1u;
59
+ if (remaining_digits >
60
+ (SIZE_MAX - first_bits) / (size_t)bits_per_digit) {
61
+ return INFINITY;
62
+ }
63
+ size_t bit_length = first_bits +
64
+ remaining_digits * (size_t)bits_per_digit;
65
+ uint64_t significant = 0u;
66
+ size_t seen_bits = 0u;
67
+ bool round_bit = false;
68
+ bool sticky_bit = false;
69
+ for (size_t index = first_nonzero; index < length; index += 1u) {
70
+ int digit = radix_digit(text[index]);
71
+ int highest_bit = index == first_nonzero
72
+ ? (int)first_bits - 1
73
+ : bits_per_digit - 1;
74
+ for (int bit = highest_bit; bit >= 0; bit -= 1) {
75
+ bool set = (digit & (1 << bit)) != 0;
76
+ if (seen_bits < 53u) {
77
+ significant = (significant << 1u) | (set ? 1u : 0u);
78
+ } else if (seen_bits == 53u) {
79
+ round_bit = set;
80
+ } else if (set) {
81
+ sticky_bit = true;
82
+ }
83
+ seen_bits += 1u;
84
+ }
85
+ }
86
+ if (bit_length <= 53u) return (double)significant;
87
+
88
+ size_t shift = bit_length - 53u;
89
+ if (round_bit && (sticky_bit || (significant & 1u) != 0u)) {
90
+ significant += 1u;
91
+ if (significant == (UINT64_C(1) << 53u)) {
92
+ significant >>= 1u;
93
+ shift += 1u;
94
+ }
95
+ }
96
+ if (shift > 971u) return INFINITY;
97
+ return ldexp((double)significant, (int)shift);
98
+ }
99
+
100
+ static OseoResult string_number(
101
+ OseoContext *context,
102
+ const OseoString *string
103
+ ) {
104
+ size_t start = 0u;
105
+ size_t end_index = string->length;
106
+ while (start < end_index && numeric_whitespace(string->units[start])) {
107
+ start += 1u;
108
+ }
109
+ while (end_index > start &&
110
+ numeric_whitespace(string->units[end_index - 1u])) {
111
+ end_index -= 1u;
112
+ }
113
+ if (start == end_index) return normal(oseo_number(0.0));
114
+ size_t length = end_index - start;
115
+ char *text = malloc(length + 1u);
116
+ if (text == NULL) {
117
+ return failure(
118
+ context,
119
+ "OSEO2001",
120
+ "Numeric conversion allocation failed."
121
+ );
122
+ }
123
+ for (size_t index = 0u; index < length; index += 1u) {
124
+ uint16_t unit = string->units[start + index];
125
+ if (unit == 0u || unit > UINT16_C(0x7f)) {
126
+ free(text);
127
+ return normal(oseo_number(NAN));
128
+ }
129
+ text[index] = (char)unit;
130
+ }
131
+ text[length] = '\0';
132
+ if (strcmp(text, "Infinity") == 0 || strcmp(text, "+Infinity") == 0) {
133
+ free(text);
134
+ return normal(oseo_number(INFINITY));
135
+ }
136
+ if (strcmp(text, "-Infinity") == 0) {
137
+ free(text);
138
+ return normal(oseo_number(-INFINITY));
139
+ }
140
+ double result;
141
+ if (length >= 2u && text[0] == '0' &&
142
+ (text[1] == 'b' || text[1] == 'B')) {
143
+ result = prefixed_integer(text, length, 2);
144
+ free(text);
145
+ return normal(oseo_number(result));
146
+ }
147
+ if (length >= 2u && text[0] == '0' &&
148
+ (text[1] == 'o' || text[1] == 'O')) {
149
+ result = prefixed_integer(text, length, 8);
150
+ free(text);
151
+ return normal(oseo_number(result));
152
+ }
153
+ if (length >= 2u && text[0] == '0' &&
154
+ (text[1] == 'x' || text[1] == 'X')) {
155
+ result = prefixed_integer(text, length, 16);
156
+ free(text);
157
+ return normal(oseo_number(result));
158
+ }
159
+ if (length >= 3u && (text[0] == '+' || text[0] == '-') &&
160
+ text[1] == '0' &&
161
+ (text[2] == 'b' || text[2] == 'B' || text[2] == 'o' ||
162
+ text[2] == 'O' || text[2] == 'x' || text[2] == 'X')) {
163
+ free(text);
164
+ return normal(oseo_number(NAN));
165
+ }
166
+ if ((text[0] < '0' || text[0] > '9') && text[0] != '+' &&
167
+ text[0] != '-' && text[0] != '.') {
168
+ free(text);
169
+ return normal(oseo_number(NAN));
170
+ }
171
+ for (size_t index = 0u; index < length; index += 1u) {
172
+ char character = text[index];
173
+ bool lower_non_exponent = character >= 'a' && character <= 'z' &&
174
+ character != 'e';
175
+ bool upper_non_exponent = character >= 'A' && character <= 'Z' &&
176
+ character != 'E';
177
+ if (lower_non_exponent || upper_non_exponent) {
178
+ free(text);
179
+ return normal(oseo_number(NAN));
180
+ }
181
+ }
182
+ char *parse_end;
183
+ result = strtod(text, &parse_end);
184
+ if (parse_end == text || *parse_end != '\0') result = NAN;
185
+ free(text);
186
+ return normal(oseo_number(result));
187
+ }
188
+
189
+ /*
190
+ * Nested default array conversion tracks the arrays already being
191
+ * joined so a cyclic membership renders as an empty element instead of
192
+ * recursing forever, matching Array.prototype.join cycle handling.
193
+ */
194
+ typedef struct ConversionAncestor {
195
+ OseoValue value;
196
+ const struct ConversionAncestor *previous;
197
+ } ConversionAncestor;
198
+
199
+ static OseoResult to_primitive_value(
200
+ OseoContext *context,
201
+ OseoValue value,
202
+ OseoToPrimitiveHint hint,
203
+ const ConversionAncestor *previous
204
+ );
205
+
206
+ static OseoResult value_text(
207
+ OseoContext *context,
208
+ OseoValue value,
209
+ const ConversionAncestor *previous
210
+ );
211
+
212
+ /*
213
+ * ToIndex(value), 7.1.22. The admitted result is one integer in the
214
+ * inclusive interval from 0 to 2**53 - 1, reported as the Number it is.
215
+ * NaN converts to +0 and every other value truncates toward zero, so
216
+ * -0.5 is admitted as 0 while -1 is not. `description` is the complete
217
+ * RangeError message, because each caller names its own argument.
218
+ */
219
+ OseoResult oseo_internal_to_index(
220
+ OseoContext *context,
221
+ OseoValue value,
222
+ const char *description,
223
+ double *index
224
+ ) {
225
+ OseoResult number = oseo_internal_to_number(context, value);
226
+ if (number.status != OSEO_STATUS_NORMAL) return number;
227
+ double integer = number_value(number.value);
228
+ if (isnan(integer)) {
229
+ integer = 0.0;
230
+ } else if (isfinite(integer)) {
231
+ integer = trunc(integer);
232
+ }
233
+ /* An infinity fails the comparison below rather than truncating, so
234
+ * both signs reach the same RangeError. */
235
+ if (!(integer >= 0.0) || integer > OSEO_INDEX_LIMIT) {
236
+ return oseo_internal_throw_error(
237
+ context,
238
+ OSEO_ERROR_RANGE,
239
+ description
240
+ );
241
+ }
242
+ *index = integer;
243
+ return normal(oseo_number(integer));
244
+ }
245
+
246
+ OseoResult oseo_internal_to_number(OseoContext *context, OseoValue value) {
247
+ uint64_t tag = tag_of(value);
248
+ if (is_number(value)) return normal(value);
249
+ if (is_bigint(value)) {
250
+ return oseo_internal_throw_error(
251
+ context,
252
+ OSEO_ERROR_TYPE,
253
+ "Cannot convert a BigInt to a number."
254
+ );
255
+ }
256
+ if (tag == OSEO_TAG_UNDEFINED) return normal(oseo_number(NAN));
257
+ if (tag == OSEO_TAG_NULL) return normal(oseo_number(0.0));
258
+ if (tag == OSEO_TAG_BOOLEAN) {
259
+ double number = (value & 1u) != 0u ? 1.0 : 0.0;
260
+ return normal(oseo_number(number));
261
+ }
262
+ if (is_string(value)) {
263
+ return string_number(context, string_object(value));
264
+ }
265
+ if (is_symbol(value)) {
266
+ return oseo_internal_throw_error(
267
+ context,
268
+ OSEO_ERROR_TYPE,
269
+ "Cannot convert a symbol to a number."
270
+ );
271
+ }
272
+ if (is_object(value)) {
273
+ OseoResult primitive = to_primitive_value(
274
+ context,
275
+ value,
276
+ OSEO_TO_PRIMITIVE_NUMBER,
277
+ NULL
278
+ );
279
+ if (primitive.status != OSEO_STATUS_NORMAL) return primitive;
280
+ return oseo_internal_to_number(context, primitive.value);
281
+ }
282
+ return failure(
283
+ context,
284
+ "OSEO2001",
285
+ "Object-to-primitive conversion is unsupported."
286
+ );
287
+ }
288
+
289
+ static void append_character(
290
+ char *text,
291
+ size_t capacity,
292
+ size_t *length,
293
+ char character
294
+ ) {
295
+ if (*length + 1u < capacity) text[*length] = character;
296
+ *length += 1u;
297
+ }
298
+
299
+ /*
300
+ * Parses a printf "%g"-family candidate's significant digits and decimal
301
+ * point position, discarding any sign. decimal_position is the count of
302
+ * digits of *digits that fall before the decimal point: value ==
303
+ * 0.digits * 10^decimal_position.
304
+ */
305
+ static void decimal_candidate_digits(
306
+ const char *candidate,
307
+ char digits[32],
308
+ size_t *digit_count,
309
+ int *decimal_position
310
+ ) {
311
+ size_t digits_before_point = 0u;
312
+ bool before_point = true;
313
+ bool negative = candidate[0] == '-';
314
+ const char *cursor = candidate + (negative ? 1u : 0u);
315
+ const char *exponent_marker = strchr(cursor, 'e');
316
+ const char *mantissa_end = exponent_marker == NULL
317
+ ? cursor + strlen(cursor)
318
+ : exponent_marker;
319
+ size_t count = 0u;
320
+ for (const char *part = cursor; part < mantissa_end; part += 1) {
321
+ if (*part == '.') {
322
+ before_point = false;
323
+ } else {
324
+ digits[count] = *part;
325
+ count += 1u;
326
+ if (before_point) digits_before_point += 1u;
327
+ }
328
+ }
329
+ int exponent = 0;
330
+ if (exponent_marker != NULL) {
331
+ const char *part = exponent_marker + 1;
332
+ int sign = 1;
333
+ if (*part == '+' || *part == '-') {
334
+ if (*part == '-') sign = -1;
335
+ part += 1;
336
+ }
337
+ while (*part >= '0' && *part <= '9') {
338
+ exponent = exponent * 10 + (*part - '0');
339
+ part += 1;
340
+ }
341
+ exponent *= sign;
342
+ }
343
+ *digit_count = count;
344
+ *decimal_position = (int)digits_before_point + exponent;
345
+ }
346
+
347
+ static size_t format_shortest_decimal(
348
+ const char *candidate,
349
+ char *output,
350
+ size_t capacity
351
+ ) {
352
+ char digits[32];
353
+ size_t digit_count = 0u;
354
+ int decimal_position = 0;
355
+ bool negative = candidate[0] == '-';
356
+ decimal_candidate_digits(
357
+ candidate,
358
+ digits,
359
+ &digit_count,
360
+ &decimal_position
361
+ );
362
+ char formatted[96];
363
+ size_t length = 0u;
364
+ if (negative) {
365
+ append_character(formatted, sizeof(formatted), &length, '-');
366
+ }
367
+ if (decimal_position <= 0 && decimal_position > -6) {
368
+ append_character(formatted, sizeof(formatted), &length, '0');
369
+ append_character(formatted, sizeof(formatted), &length, '.');
370
+ for (int index = 0; index < -decimal_position; index += 1) {
371
+ append_character(formatted, sizeof(formatted), &length, '0');
372
+ }
373
+ for (size_t index = 0u; index < digit_count; index += 1u) {
374
+ append_character(
375
+ formatted,
376
+ sizeof(formatted),
377
+ &length,
378
+ digits[index]
379
+ );
380
+ }
381
+ } else if (decimal_position > 0 && decimal_position <= 21) {
382
+ for (int index = 0; index < decimal_position; index += 1) {
383
+ char digit = index < (int)digit_count
384
+ ? digits[(size_t)index]
385
+ : '0';
386
+ append_character(formatted, sizeof(formatted), &length, digit);
387
+ }
388
+ if (decimal_position < (int)digit_count) {
389
+ append_character(formatted, sizeof(formatted), &length, '.');
390
+ for (size_t index = (size_t)decimal_position;
391
+ index < digit_count;
392
+ index += 1u) {
393
+ append_character(
394
+ formatted,
395
+ sizeof(formatted),
396
+ &length,
397
+ digits[index]
398
+ );
399
+ }
400
+ }
401
+ } else {
402
+ append_character(formatted, sizeof(formatted), &length, digits[0]);
403
+ if (digit_count > 1u) {
404
+ append_character(formatted, sizeof(formatted), &length, '.');
405
+ for (size_t index = 1u; index < digit_count; index += 1u) {
406
+ append_character(
407
+ formatted,
408
+ sizeof(formatted),
409
+ &length,
410
+ digits[index]
411
+ );
412
+ }
413
+ }
414
+ int scientific_exponent = decimal_position - 1;
415
+ char exponent_text[16];
416
+ (void)snprintf(
417
+ exponent_text,
418
+ sizeof(exponent_text),
419
+ "e%+d",
420
+ scientific_exponent
421
+ );
422
+ for (const char *part = exponent_text; *part != '\0'; part += 1) {
423
+ append_character(
424
+ formatted,
425
+ sizeof(formatted),
426
+ &length,
427
+ *part
428
+ );
429
+ }
430
+ }
431
+ size_t stored = length < sizeof(formatted) - 1u
432
+ ? length
433
+ : sizeof(formatted) - 1u;
434
+ formatted[stored] = '\0';
435
+ return (size_t)snprintf(output, capacity, "%s", formatted);
436
+ }
437
+
438
+ /*
439
+ * True when value is a normal power of two above the smallest normal: its
440
+ * next-lower double lies half as far away as its next-higher one, so the
441
+ * decimal strings that read back as value reach twice as far above it as
442
+ * below. Every other finite double, including the smallest normal (whose
443
+ * lower neighbor is a subnormal at the same spacing) and every subnormal,
444
+ * sits in a symmetric interval. Zero, infinities, and NaN are excluded by
445
+ * the biased exponent bounds.
446
+ */
447
+ static bool rounding_interval_is_asymmetric(double value) {
448
+ uint64_t bits = double_bits(value);
449
+ uint64_t biased_exponent = (bits >> 52) & 0x7ffu;
450
+ return (bits & 0xfffffffffffffu) == 0u &&
451
+ biased_exponent > 1u &&
452
+ biased_exponent < 0x7ffu;
453
+ }
454
+
455
+ static bool candidate_round_trips(const char *candidate, double value) {
456
+ char *end;
457
+ double parsed = strtod(candidate, &end);
458
+ return *end == '\0' && double_bits(parsed) == double_bits(value);
459
+ }
460
+
461
+ /*
462
+ * Writes the precision-digit decimal one unit in the last place above
463
+ * value's correctly rounded precision-digit decimal, in magnitude, with
464
+ * value's sign, in "%e" form without trailing zeros. A carry out of the
465
+ * leading digit becomes a new leading 1 and raises the exponent. Returns
466
+ * false only when the "%e" text does not have the expected shape.
467
+ */
468
+ static bool decimal_successor_candidate(
469
+ double value,
470
+ int precision,
471
+ char candidate[64]
472
+ ) {
473
+ char nearest[64];
474
+ int written = snprintf(
475
+ nearest,
476
+ sizeof(nearest),
477
+ "%.*e",
478
+ precision - 1,
479
+ fabs(value)
480
+ );
481
+ if (written <= 0 || (size_t)written >= sizeof(nearest)) return false;
482
+ char digits[17];
483
+ size_t count = 0u;
484
+ const char *cursor = nearest;
485
+ for (; *cursor != '\0' && *cursor != 'e'; cursor += 1) {
486
+ if (*cursor == '.') continue;
487
+ if (*cursor < '0' || *cursor > '9' || count == sizeof(digits)) {
488
+ return false;
489
+ }
490
+ digits[count] = *cursor;
491
+ count += 1u;
492
+ }
493
+ if (*cursor != 'e' || count != (size_t)precision) return false;
494
+ long exponent = strtol(cursor + 1, NULL, 10);
495
+ bool carry = true;
496
+ for (size_t index = count; carry && index > 0u; index -= 1u) {
497
+ if (digits[index - 1u] == '9') {
498
+ digits[index - 1u] = '0';
499
+ } else {
500
+ digits[index - 1u] = (char)(digits[index - 1u] + 1);
501
+ carry = false;
502
+ }
503
+ }
504
+ if (carry) {
505
+ digits[0] = '1';
506
+ exponent += 1;
507
+ }
508
+ while (count > 1u && digits[count - 1u] == '0') count -= 1u;
509
+ size_t length = 0u;
510
+ if (signbit(value)) {
511
+ append_character(candidate, 64u, &length, '-');
512
+ }
513
+ append_character(candidate, 64u, &length, digits[0]);
514
+ if (count > 1u) append_character(candidate, 64u, &length, '.');
515
+ for (size_t index = 1u; index < count; index += 1u) {
516
+ append_character(candidate, 64u, &length, digits[index]);
517
+ }
518
+ written = snprintf(candidate + length, 64u - length, "e%+ld", exponent);
519
+ return written > 0 && (size_t)written < 64u - length;
520
+ }
521
+
522
+ /*
523
+ * Number::toString's digit selection (6.1.6.1.20 step 5): the fewest
524
+ * significant digits that read back as value, and among those the
525
+ * decimal closest to value, the even one on a tie. value must be finite
526
+ * and nonzero; candidate receives "%g"-family text with value's sign.
527
+ *
528
+ * At each digit count the correctly rounded "%.*g" decimal is the closest
529
+ * one, and printf rounds an exact tie to even. In a symmetric interval no
530
+ * farther decimal can read back when the closest does not. Above a power
531
+ * of two the interval reaches twice as far upward, so a closest decimal
532
+ * below value can miss while the next one up, the only candidate that
533
+ * can still fit, reads back: 2^-24 needs 5.960464477539063e-8, not the
534
+ * tie-to-even 5.960464477539062e-8. Assumes the C locale's '.' radix and
535
+ * the default round-to-nearest mode, which the runtime never changes, so
536
+ * snprintf and strtod are correctly rounded.
537
+ */
538
+ static void shortest_round_trip_candidate(double value, char candidate[64]) {
539
+ bool asymmetric = rounding_interval_is_asymmetric(value);
540
+ for (int precision = 1; precision <= 17; precision += 1) {
541
+ (void)snprintf(candidate, 64u, "%.*g", precision, value);
542
+ if (candidate_round_trips(candidate, value)) return;
543
+ if (asymmetric &&
544
+ decimal_successor_candidate(value, precision, candidate) &&
545
+ candidate_round_trips(candidate, value)) {
546
+ return;
547
+ }
548
+ }
549
+ (void)snprintf(candidate, 64u, "%.17g", value);
550
+ }
551
+
552
+ size_t oseo_internal_number_text(double value, char *output, size_t capacity) {
553
+ if (isnan(value)) return (size_t)snprintf(output, capacity, "NaN");
554
+ if (isinf(value)) {
555
+ return (size_t)snprintf(
556
+ output,
557
+ capacity,
558
+ signbit(value) ? "-Infinity" : "Infinity"
559
+ );
560
+ }
561
+ if (value == 0.0) return (size_t)snprintf(output, capacity, "0");
562
+ /*
563
+ * Every integer below 2^53 in magnitude is a double whose neighbors lie
564
+ * at most one apart, so no decimal with fewer significant digits reads
565
+ * back as it: Number::toString's digits are the integer's own, in plain
566
+ * notation because it is below 10^21. Array indices and loop counters
567
+ * take this path instead of the round-trip search below, whose
568
+ * snprintf and strtod calls dominate index-heavy code.
569
+ */
570
+ double magnitude = fabs(value);
571
+ if (magnitude < 9007199254740992.0 && magnitude == floor(magnitude)) {
572
+ uint64_t integer = (uint64_t)magnitude;
573
+ char reversed[20];
574
+ size_t count = 0u;
575
+ do {
576
+ reversed[count] = (char)('0' + (int)(integer % 10u));
577
+ count += 1u;
578
+ integer /= 10u;
579
+ } while (integer != 0u);
580
+ size_t length = count + (value < 0.0 ? 1u : 0u);
581
+ if (capacity == 0u) return length;
582
+ size_t stored = 0u;
583
+ if (value < 0.0 && stored + 1u < capacity) {
584
+ output[stored] = '-';
585
+ stored += 1u;
586
+ }
587
+ while (count > 0u && stored + 1u < capacity) {
588
+ count -= 1u;
589
+ output[stored] = reversed[count];
590
+ stored += 1u;
591
+ }
592
+ output[stored] = '\0';
593
+ return length;
594
+ }
595
+ char candidate[64];
596
+ shortest_round_trip_candidate(value, candidate);
597
+ return format_shortest_decimal(candidate, output, capacity);
598
+ }
599
+
600
+ /*
601
+ * Drops the leading and trailing zeros a fixed-notation candidate carries,
602
+ * leaving only significant digits. A leading zero contributes no
603
+ * significant digit and shifts the decimal exponent down; a trailing zero
604
+ * leaves the exponent alone because the value is read as
605
+ * 0.digits * 10^exponent. Both must go: a retained leading zero would make
606
+ * the first digit zero, and a retained trailing zero would make the digit
607
+ * count larger than the smallest one that still round-trips.
608
+ */
609
+ static void trim_insignificant_zeros(
610
+ char digits[32],
611
+ size_t *digit_count,
612
+ int *decimal_exponent
613
+ ) {
614
+ size_t leading = 0u;
615
+ while (leading < *digit_count && digits[leading] == '0') leading += 1u;
616
+ if (leading == *digit_count) {
617
+ digits[0] = '0';
618
+ *digit_count = 1u;
619
+ *decimal_exponent = 0;
620
+ return;
621
+ }
622
+ size_t length = *digit_count - leading;
623
+ memmove(digits, digits + leading, length);
624
+ while (length > 1u && digits[length - 1u] == '0') length -= 1u;
625
+ *digit_count = length;
626
+ *decimal_exponent -= (int)leading;
627
+ }
628
+
629
+ /*
630
+ * Same round-trip digit selection as oseo_internal_number_text, but
631
+ * returns the parsed significant digits and decimal exponent instead of a
632
+ * formatted string. value must be finite, nonzero, and non-negative.
633
+ * At most 17 significant digits round-trip a double, so the trimmed result
634
+ * always fits the caller's buffer.
635
+ */
636
+ void oseo_internal_number_shortest_digits(
637
+ double value,
638
+ char digits[18],
639
+ size_t *digit_count,
640
+ int *decimal_exponent
641
+ ) {
642
+ char parsed_digits[32];
643
+ size_t count = 0u;
644
+ char candidate[64];
645
+ shortest_round_trip_candidate(value, candidate);
646
+ decimal_candidate_digits(
647
+ candidate,
648
+ parsed_digits,
649
+ &count,
650
+ decimal_exponent
651
+ );
652
+ trim_insignificant_zeros(parsed_digits, &count, decimal_exponent);
653
+ *digit_count = count;
654
+ memcpy(digits, parsed_digits, count);
655
+ }
656
+
657
+ OseoResult oseo_internal_canonical_numeric_index(
658
+ OseoContext *context,
659
+ OseoValue key,
660
+ bool *result
661
+ ) {
662
+ *result = false;
663
+ if (!is_string(key)) return normal(oseo_undefined());
664
+ if (oseo_internal_string_is_ascii(key, "-0")) {
665
+ *result = true;
666
+ return normal(oseo_undefined());
667
+ }
668
+ OseoString *string = string_object(key);
669
+ OseoResult converted = string_number(context, string);
670
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
671
+ char text[64];
672
+ size_t length = oseo_internal_number_text(
673
+ number_value(converted.value),
674
+ text,
675
+ sizeof(text)
676
+ );
677
+ if (length != string->length || length >= sizeof(text)) {
678
+ return normal(oseo_undefined());
679
+ }
680
+ for (size_t index = 0u; index < length; index += 1u) {
681
+ if (string->units[index] !=
682
+ (uint16_t)(unsigned char)text[index]) {
683
+ return normal(oseo_undefined());
684
+ }
685
+ }
686
+ *result = true;
687
+ return normal(oseo_undefined());
688
+ }
689
+
690
+ static OseoResult value_text(
691
+ OseoContext *context,
692
+ OseoValue value,
693
+ const ConversionAncestor *previous
694
+ ) {
695
+ if (is_string(value)) return normal(value);
696
+ if (is_bigint(value)) {
697
+ return oseo_internal_bigint_string(context, value);
698
+ }
699
+ if (is_symbol(value)) {
700
+ return oseo_internal_throw_error(
701
+ context,
702
+ OSEO_ERROR_TYPE,
703
+ "Cannot convert a symbol to a string."
704
+ );
705
+ }
706
+ if (is_object(value)) {
707
+ OseoResult primitive = to_primitive_value(
708
+ context,
709
+ value,
710
+ OSEO_TO_PRIMITIVE_STRING,
711
+ previous
712
+ );
713
+ if (primitive.status != OSEO_STATUS_NORMAL) return primitive;
714
+ return value_text(context, primitive.value, previous);
715
+ }
716
+ const char *constant = NULL;
717
+ uint64_t tag = tag_of(value);
718
+ char number[64];
719
+ if (tag == OSEO_TAG_UNDEFINED) constant = "undefined";
720
+ else if (tag == OSEO_TAG_NULL) constant = "null";
721
+ else if (tag == OSEO_TAG_BOOLEAN) {
722
+ constant = (value & 1u) != 0u ? "true" : "false";
723
+ } else if (is_number(value)) {
724
+ (void)oseo_internal_number_text(
725
+ number_value(value),
726
+ number,
727
+ sizeof(number)
728
+ );
729
+ constant = number;
730
+ }
731
+ if (constant == NULL) {
732
+ return failure(context, "OSEO2001", "Value is outside M1 semantics.");
733
+ }
734
+ size_t length = strlen(constant);
735
+ uint16_t units[64];
736
+ if (length > 64u) {
737
+ return failure(context, "OSEO2001", "Primitive text is too long.");
738
+ }
739
+ for (size_t index = 0u; index < length; index += 1u) {
740
+ units[index] = (uint16_t)(unsigned char)constant[index];
741
+ }
742
+ return oseo_internal_allocate_string(context, units, length);
743
+ }
744
+
745
+ OseoResult oseo_internal_value_string(OseoContext *context, OseoValue value) {
746
+ return value_text(context, value, NULL);
747
+ }
748
+
749
+ OseoResult oseo_internal_array_join_element_string(
750
+ OseoContext *context,
751
+ OseoValue value,
752
+ OseoValue array
753
+ ) {
754
+ ConversionAncestor current = {array, NULL};
755
+ return value_text(context, value, &current);
756
+ }
757
+
758
+ /* The keys this walk asks about are the ASCII method names of the default
759
+ * conversion protocol, so the plain descriptor primitive is enough: no
760
+ * synthetic symbol-keyed own property can answer here. */
761
+ static bool conversion_property_exists(
762
+ OseoValue object_value,
763
+ OseoValue key
764
+ ) {
765
+ OseoValue current = object_value;
766
+ while (is_object(current)) {
767
+ OseoValue property_value = oseo_undefined();
768
+ OseoPropertyAttributes attributes = {false, false, false, false};
769
+ OseoValue ignored_getter = oseo_undefined();
770
+ OseoValue ignored_setter = oseo_undefined();
771
+ if (oseo_internal_own_descriptor(
772
+ current,
773
+ key,
774
+ &property_value,
775
+ &attributes,
776
+ &ignored_getter,
777
+ &ignored_setter
778
+ )) {
779
+ return true;
780
+ }
781
+ current = ordinary_object(current)->prototype;
782
+ }
783
+ return false;
784
+ }
785
+
786
+ /*
787
+ * The one remaining deferred default toString is Array text, and the
788
+ * Array prototype nodes still own it. It applies when %Array.prototype%
789
+ * is the nearest materialized intrinsic prototype on the chain: a nearer
790
+ * realm-owned prototype means some other owner's method answers, and no
791
+ * realm-owned prototype at all means the ordinary lookup found a user
792
+ * method that happens to be Array's.
793
+ */
794
+ static bool conversion_reaches_array_prototype(
795
+ OseoContext *context,
796
+ OseoValue value
797
+ ) {
798
+ OseoValue current = value;
799
+ while (is_object(current)) {
800
+ if (current ==
801
+ context->realm->intrinsics[OSEO_INTRINSIC_ARRAY_PROTOTYPE]) {
802
+ return true;
803
+ }
804
+ if (current ==
805
+ context->realm->intrinsics[OSEO_INTRINSIC_FUNCTION_PROTOTYPE] ||
806
+ current ==
807
+ context->realm->intrinsics[OSEO_INTRINSIC_PROMISE_PROTOTYPE] ||
808
+ current ==
809
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_PROTOTYPE]) {
810
+ return false;
811
+ }
812
+ current = ordinary_object(current)->prototype;
813
+ }
814
+ return false;
815
+ }
816
+
817
+ static bool conversion_is_ancestor(
818
+ OseoValue value,
819
+ const ConversionAncestor *ancestor
820
+ ) {
821
+ for (const ConversionAncestor *current = ancestor;
822
+ current != NULL;
823
+ current = current->previous) {
824
+ if (current->value == value) return true;
825
+ }
826
+ return false;
827
+ }
828
+
829
+ static bool array_string_is_ancestor(
830
+ OseoValue value,
831
+ const OseoArrayStringAncestor *ancestor
832
+ ) {
833
+ for (const OseoArrayStringAncestor *current = ancestor;
834
+ current != NULL;
835
+ current = current->previous) {
836
+ if (current->value == value) return true;
837
+ }
838
+ return false;
839
+ }
840
+
841
+ static OseoResult array_join_text(
842
+ OseoContext *context,
843
+ OseoValue array_value,
844
+ const ConversionAncestor *previous
845
+ ) {
846
+ const OseoArrayStringAncestor *previous_string =
847
+ context->array_string_stack;
848
+ if (conversion_is_ancestor(array_value, previous) ||
849
+ array_string_is_ancestor(array_value, previous_string)) {
850
+ return oseo_string_from_units(context, NULL, 0u);
851
+ }
852
+ /* Nested array conversion recurses in C, so it consumes the same
853
+ * deterministic call-depth budget as a JavaScript call. */
854
+ OseoResult result = oseo_call_enter(context);
855
+ if (result.status != OSEO_STATUS_NORMAL) return result;
856
+ OseoRootFrame frame = {NULL, NULL, 0u};
857
+ result = oseo_roots_allocate(context, &frame, 4u);
858
+ if (result.status != OSEO_STATUS_NORMAL) {
859
+ oseo_call_leave(context);
860
+ return result;
861
+ }
862
+ frame.slots[0] = array_value;
863
+ OseoArrayStringAncestor current_string = {
864
+ frame.slots[0],
865
+ previous_string,
866
+ };
867
+ context->array_string_stack = &current_string;
868
+ ConversionAncestor current = {frame.slots[0], previous};
869
+ uint16_t *units = NULL;
870
+ size_t length = 0u;
871
+ static const uint16_t length_units[] = {
872
+ 'l', 'e', 'n', 'g', 't', 'h'
873
+ };
874
+ result = oseo_string_from_units(context, length_units, 6u);
875
+ frame.slots[1] = result.value;
876
+ if (result.status == OSEO_STATUS_NORMAL) {
877
+ result = oseo_object_get(
878
+ context,
879
+ frame.slots[0],
880
+ frame.slots[1]
881
+ );
882
+ frame.slots[2] = result.value;
883
+ }
884
+ if (result.status == OSEO_STATUS_NORMAL) {
885
+ result = oseo_internal_to_number(context, frame.slots[2]);
886
+ frame.slots[2] = result.value;
887
+ }
888
+ uint32_t array_length = 0u;
889
+ if (result.status == OSEO_STATUS_NORMAL) {
890
+ double numeric_length = number_value(frame.slots[2]);
891
+ double truncated_length = numeric_length > 0.0
892
+ ? floor(numeric_length)
893
+ : 0.0;
894
+ if (truncated_length > (double)UINT32_MAX) {
895
+ result = oseo_internal_throw_error(
896
+ context,
897
+ OSEO_ERROR_TYPE,
898
+ "Invalid array length."
899
+ );
900
+ } else {
901
+ array_length = (uint32_t)truncated_length;
902
+ }
903
+ }
904
+ for (uint32_t index = 0u;
905
+ result.status == OSEO_STATUS_NORMAL && index < array_length;
906
+ index += 1u) {
907
+ if (index > 0u) {
908
+ if (length >= OSEO_MAX_STRING_LENGTH) {
909
+ result = oseo_internal_throw_error(
910
+ context,
911
+ OSEO_ERROR_RANGE,
912
+ "Invalid string length."
913
+ );
914
+ break;
915
+ }
916
+ uint16_t *grown = realloc(
917
+ units,
918
+ (length + 1u) * sizeof(uint16_t)
919
+ );
920
+ if (grown == NULL) {
921
+ result = failure(
922
+ context,
923
+ "OSEO2001",
924
+ "String allocation failed."
925
+ );
926
+ break;
927
+ }
928
+ units = grown;
929
+ units[length] = ',';
930
+ length += 1u;
931
+ }
932
+ result = oseo_property_key(context, oseo_number((double)index));
933
+ frame.slots[1] = result.value;
934
+ if (result.status == OSEO_STATUS_NORMAL) {
935
+ result = oseo_object_get(
936
+ context,
937
+ frame.slots[0],
938
+ frame.slots[1]
939
+ );
940
+ frame.slots[2] = result.value;
941
+ }
942
+ if (result.status != OSEO_STATUS_NORMAL) break;
943
+ if (is_nullish(frame.slots[2])) {
944
+ continue;
945
+ }
946
+ result = value_text(context, frame.slots[2], &current);
947
+ frame.slots[3] = result.value;
948
+ if (result.status != OSEO_STATUS_NORMAL) break;
949
+ OseoString *element = string_object(frame.slots[3]);
950
+ if (element->length > OSEO_MAX_STRING_LENGTH - length) {
951
+ result = oseo_internal_throw_error(
952
+ context,
953
+ OSEO_ERROR_RANGE,
954
+ "Invalid string length."
955
+ );
956
+ break;
957
+ }
958
+ size_t next_length = length + element->length;
959
+ uint16_t *grown = realloc(
960
+ units,
961
+ next_length * sizeof(uint16_t)
962
+ );
963
+ if (grown == NULL && next_length > 0u) {
964
+ result = failure(
965
+ context,
966
+ "OSEO2001",
967
+ "String allocation failed."
968
+ );
969
+ break;
970
+ }
971
+ units = grown;
972
+ if (element->length > 0u) {
973
+ memcpy(
974
+ units + length,
975
+ element->units,
976
+ element->length * sizeof(uint16_t)
977
+ );
978
+ }
979
+ length = next_length;
980
+ }
981
+ if (result.status == OSEO_STATUS_NORMAL) {
982
+ result = oseo_string_from_units(context, units, length);
983
+ }
984
+ free(units);
985
+ context->array_string_stack = (void *)previous_string;
986
+ oseo_roots_release(context, &frame);
987
+ oseo_call_leave(context);
988
+ return result;
989
+ }
990
+
991
+ /* The deferred Array.prototype.toString: a user join is honored. */
992
+ static OseoResult default_array_text(
993
+ OseoContext *context,
994
+ OseoValue array_value,
995
+ const ConversionAncestor *previous
996
+ ) {
997
+ static const uint16_t join_units[] = {'j', 'o', 'i', 'n'};
998
+ OseoRootFrame frame = {NULL, NULL, 0u};
999
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1000
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1001
+ frame.slots[0] = array_value;
1002
+ result = oseo_string_from_units(context, join_units, 4u);
1003
+ frame.slots[1] = result.value;
1004
+ bool property_exists = result.status == OSEO_STATUS_NORMAL &&
1005
+ conversion_property_exists(frame.slots[0], frame.slots[1]);
1006
+ if (result.status == OSEO_STATUS_NORMAL && property_exists) {
1007
+ result = oseo_object_get(
1008
+ context,
1009
+ frame.slots[0],
1010
+ frame.slots[1]
1011
+ );
1012
+ frame.slots[2] = result.value;
1013
+ if (result.status == OSEO_STATUS_NORMAL &&
1014
+ is_callable(frame.slots[2])) {
1015
+ /* The shortcut runs in the running realm, so another realm's
1016
+ * `join` takes the ordinary call that enters its own. */
1017
+ if (is_function(frame.slots[2]) &&
1018
+ function_object(frame.slots[2])->realm ==
1019
+ context->realm_record &&
1020
+ function_object(frame.slots[2])->code_id ==
1021
+ OSEO_ARRAY_JOIN_CODE_ID &&
1022
+ oseo_internal_string_is_ascii(
1023
+ function_object(frame.slots[2])->initial_name,
1024
+ "join"
1025
+ )) {
1026
+ result = array_join_text(
1027
+ context,
1028
+ frame.slots[0],
1029
+ previous
1030
+ );
1031
+ } else {
1032
+ result = oseo_call_function(
1033
+ context,
1034
+ frame.slots[2],
1035
+ frame.slots[0],
1036
+ 0u,
1037
+ NULL,
1038
+ oseo_undefined()
1039
+ );
1040
+ }
1041
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1042
+ result = oseo_call_function(
1043
+ context,
1044
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_TO_STRING],
1045
+ frame.slots[0],
1046
+ 0u,
1047
+ NULL,
1048
+ oseo_undefined()
1049
+ );
1050
+ }
1051
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1052
+ result = oseo_call_function(
1053
+ context,
1054
+ context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_TO_STRING],
1055
+ frame.slots[0],
1056
+ 0u,
1057
+ NULL,
1058
+ oseo_undefined()
1059
+ );
1060
+ }
1061
+ oseo_roots_release(context, &frame);
1062
+ return result;
1063
+ }
1064
+
1065
+ /*
1066
+ * The generic ToPrimitive over OrdinaryToPrimitive: @@toPrimitive is
1067
+ * dispatched first, then valueOf and toString are read with the ordinary
1068
+ * property lookup and called in hint order. Whichever method the lookup
1069
+ * finds is the one called, so an arbitrary object whose nearest toString
1070
+ * is %Object.prototype.toString% reaches that materialized function and
1071
+ * renders through its builtinTag and @@toStringTag composition rather than
1072
+ * through a private text substitute. Array text keeps its deferred
1073
+ * conversion, which honors a user `join` and shares this call's cycle
1074
+ * stack.
1075
+ */
1076
+ static OseoResult ordinary_to_primitive_value(
1077
+ OseoContext *context,
1078
+ OseoValue value,
1079
+ OseoToPrimitiveHint hint,
1080
+ const ConversionAncestor *previous
1081
+ ) {
1082
+ static const uint16_t to_string_units[] = {
1083
+ 't', 'o', 'S', 't', 'r', 'i', 'n', 'g'
1084
+ };
1085
+ static const uint16_t value_of_units[] = {
1086
+ 'v', 'a', 'l', 'u', 'e', 'O', 'f'
1087
+ };
1088
+ bool string_first = hint == OSEO_TO_PRIMITIVE_STRING;
1089
+ const uint16_t *names[2];
1090
+ size_t lengths[2];
1091
+ names[0] = string_first ? to_string_units : value_of_units;
1092
+ lengths[0] = string_first ? 8u : 7u;
1093
+ names[1] = string_first ? value_of_units : to_string_units;
1094
+ lengths[1] = string_first ? 7u : 8u;
1095
+ OseoRootFrame frame = {NULL, NULL, 0u};
1096
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1097
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1098
+ frame.slots[0] = value;
1099
+ bool converted = false;
1100
+ for (size_t index = 0u; index < 2u; index += 1u) {
1101
+ bool trying_to_string = names[index] == to_string_units;
1102
+ result = oseo_string_from_units(
1103
+ context,
1104
+ names[index],
1105
+ lengths[index]
1106
+ );
1107
+ frame.slots[1] = result.value;
1108
+ if (result.status != OSEO_STATUS_NORMAL) break;
1109
+ result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
1110
+ frame.slots[2] = result.value;
1111
+ if (result.status != OSEO_STATUS_NORMAL) break;
1112
+ if (!is_callable(frame.slots[2])) continue;
1113
+ if (trying_to_string &&
1114
+ is_function(frame.slots[2]) &&
1115
+ function_object(frame.slots[2])->realm == context->realm_record &&
1116
+ conversion_reaches_array_prototype(context, frame.slots[0]) &&
1117
+ function_object(frame.slots[2])->code_id ==
1118
+ OSEO_ARRAY_TO_STRING_CODE_ID &&
1119
+ oseo_internal_string_is_ascii(
1120
+ function_object(frame.slots[2])->initial_name,
1121
+ "toString"
1122
+ )) {
1123
+ result = default_array_text(
1124
+ context,
1125
+ frame.slots[0],
1126
+ previous
1127
+ );
1128
+ if (result.status != OSEO_STATUS_NORMAL) break;
1129
+ converted = !is_object(result.value);
1130
+ if (converted) break;
1131
+ continue;
1132
+ }
1133
+ result = oseo_call_function(
1134
+ context,
1135
+ frame.slots[2],
1136
+ frame.slots[0],
1137
+ 0u,
1138
+ NULL,
1139
+ oseo_undefined()
1140
+ );
1141
+ if (result.status != OSEO_STATUS_NORMAL) break;
1142
+ if (!is_object(result.value)) {
1143
+ converted = true;
1144
+ break;
1145
+ }
1146
+ }
1147
+ if (result.status == OSEO_STATUS_NORMAL && !converted) {
1148
+ result = oseo_internal_throw_error(
1149
+ context,
1150
+ OSEO_ERROR_TYPE,
1151
+ "Cannot convert an object to a primitive value."
1152
+ );
1153
+ }
1154
+ oseo_roots_release(context, &frame);
1155
+ return result;
1156
+ }
1157
+
1158
+ /*
1159
+ * ToPrimitive (7.1.1) over OrdinaryToPrimitive: @@toPrimitive is
1160
+ * dispatched first, and everything else is the ordinary conversion.
1161
+ */
1162
+ static OseoResult to_primitive_value(
1163
+ OseoContext *context,
1164
+ OseoValue value,
1165
+ OseoToPrimitiveHint hint,
1166
+ const ConversionAncestor *previous
1167
+ ) {
1168
+ if (!is_object(value)) return normal(value);
1169
+ /*
1170
+ * A Symbol.toPrimitive method can exist only after the program has
1171
+ * touched the Symbol intrinsic, so an untouched intrinsic skips
1172
+ * the dispatch without creating it.
1173
+ */
1174
+ OseoValue exotic_key =
1175
+ context->well_known_symbols[OSEO_WELL_KNOWN_TO_PRIMITIVE];
1176
+ if (tag_of(exotic_key) == OSEO_TAG_UNDEFINED) {
1177
+ return ordinary_to_primitive_value(context, value, hint, previous);
1178
+ }
1179
+ OseoRootFrame frame = {NULL, NULL, 0u};
1180
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1181
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1182
+ frame.slots[0] = value;
1183
+ result = oseo_object_get(context, frame.slots[0], exotic_key);
1184
+ frame.slots[2] = result.value;
1185
+ if (result.status != OSEO_STATUS_NORMAL) {
1186
+ oseo_roots_release(context, &frame);
1187
+ return result;
1188
+ }
1189
+ if (is_nullish(frame.slots[2])) {
1190
+ oseo_roots_release(context, &frame);
1191
+ return ordinary_to_primitive_value(context, value, hint, previous);
1192
+ }
1193
+ if (!is_callable(frame.slots[2])) {
1194
+ result = oseo_internal_throw_error(
1195
+ context,
1196
+ OSEO_ERROR_TYPE,
1197
+ "The Symbol.toPrimitive method is not callable."
1198
+ );
1199
+ oseo_roots_release(context, &frame);
1200
+ return result;
1201
+ }
1202
+ const char *hint_name = hint == OSEO_TO_PRIMITIVE_STRING
1203
+ ? "string"
1204
+ : hint == OSEO_TO_PRIMITIVE_DEFAULT ? "default" : "number";
1205
+ size_t hint_length = strlen(hint_name);
1206
+ uint16_t hint_units[8];
1207
+ for (size_t unit = 0u; unit < hint_length; unit += 1u) {
1208
+ hint_units[unit] = (uint16_t)(unsigned char)hint_name[unit];
1209
+ }
1210
+ result = oseo_string_from_units(context, hint_units, hint_length);
1211
+ frame.slots[1] = result.value;
1212
+ if (result.status == OSEO_STATUS_NORMAL) {
1213
+ result = oseo_call_function(
1214
+ context,
1215
+ frame.slots[2],
1216
+ frame.slots[0],
1217
+ 1u,
1218
+ &frame.slots[1],
1219
+ oseo_undefined()
1220
+ );
1221
+ }
1222
+ if (result.status == OSEO_STATUS_NORMAL && is_object(result.value)) {
1223
+ result = oseo_internal_throw_error(
1224
+ context,
1225
+ OSEO_ERROR_TYPE,
1226
+ "Cannot convert an object to a primitive value."
1227
+ );
1228
+ }
1229
+ oseo_roots_release(context, &frame);
1230
+ return result;
1231
+ }
1232
+
1233
+ OseoResult oseo_internal_ordinary_to_primitive(
1234
+ OseoContext *context,
1235
+ OseoValue value,
1236
+ OseoToPrimitiveHint hint
1237
+ ) {
1238
+ if (!is_object(value)) return normal(value);
1239
+ return ordinary_to_primitive_value(context, value, hint, NULL);
1240
+ }
1241
+
1242
+ OseoResult oseo_internal_to_primitive(
1243
+ OseoContext *context,
1244
+ OseoValue value,
1245
+ OseoToPrimitiveHint hint
1246
+ ) {
1247
+ return to_primitive_value(context, value, hint, NULL);
1248
+ }
1249
+
1250
+ OseoResult oseo_property_key(OseoContext *context, OseoValue value) {
1251
+ if (is_symbol(value)) return normal(value);
1252
+ if (is_object(value)) {
1253
+ OseoResult primitive = to_primitive_value(
1254
+ context,
1255
+ value,
1256
+ OSEO_TO_PRIMITIVE_STRING,
1257
+ NULL
1258
+ );
1259
+ if (primitive.status != OSEO_STATUS_NORMAL) return primitive;
1260
+ if (is_symbol(primitive.value)) return primitive;
1261
+ return oseo_internal_value_string(context, primitive.value);
1262
+ }
1263
+ return oseo_internal_value_string(context, value);
1264
+ }
1265
+
1266
+ OseoResult oseo_negate(OseoContext *context, OseoValue value) {
1267
+ OseoResult numeric = oseo_to_numeric(context, value);
1268
+ if (numeric.status != OSEO_STATUS_NORMAL) return numeric;
1269
+ if (is_bigint(numeric.value)) {
1270
+ OseoValue slot = numeric.value;
1271
+ OseoRootFrame frame = {NULL, &slot, 1u};
1272
+ oseo_roots_push(context, &frame);
1273
+ OseoResult result = oseo_internal_bigint_negate(context, slot);
1274
+ oseo_roots_pop(context, &frame);
1275
+ return result;
1276
+ }
1277
+ return normal(oseo_number(-number_value(numeric.value)));
1278
+ }
1279
+
1280
+ OseoResult oseo_typeof(OseoContext *context, OseoValue value) {
1281
+ const char *constant = NULL;
1282
+ uint64_t tag = tag_of(value);
1283
+ if (tag == OSEO_TAG_UNDEFINED) constant = "undefined";
1284
+ else if (tag == OSEO_TAG_NULL) constant = "object";
1285
+ else if (tag == OSEO_TAG_BOOLEAN) constant = "boolean";
1286
+ else if (is_number(value)) constant = "number";
1287
+ else if (is_bigint(value)) constant = "bigint";
1288
+ else if (is_string(value)) constant = "string";
1289
+ else if (is_symbol(value)) constant = "symbol";
1290
+ else if (is_callable(value)) constant = "function";
1291
+ else if (is_object(value)) constant = "object";
1292
+ if (constant == NULL) {
1293
+ return failure(context, "OSEO2001", "Value has no typeof text.");
1294
+ }
1295
+ size_t length = strlen(constant);
1296
+ uint16_t units[16];
1297
+ for (size_t index = 0u; index < length; index += 1u) {
1298
+ units[index] = (uint16_t)(unsigned char)constant[index];
1299
+ }
1300
+ return oseo_internal_allocate_string(context, units, length);
1301
+ }
1302
+
1303
+ static OseoResult numeric_binary(
1304
+ OseoContext *context,
1305
+ OseoValue left,
1306
+ OseoValue right,
1307
+ char operator
1308
+ ) {
1309
+ OseoValue slots[2] = {left, right};
1310
+ OseoRootFrame frame = {NULL, slots, 2u};
1311
+ oseo_roots_push(context, &frame);
1312
+ OseoResult converted = oseo_to_numeric(context, slots[0]);
1313
+ if (converted.status != OSEO_STATUS_NORMAL) {
1314
+ oseo_roots_pop(context, &frame);
1315
+ return converted;
1316
+ }
1317
+ slots[0] = converted.value;
1318
+ converted = oseo_to_numeric(context, slots[1]);
1319
+ if (converted.status != OSEO_STATUS_NORMAL) {
1320
+ oseo_roots_pop(context, &frame);
1321
+ return converted;
1322
+ }
1323
+ slots[1] = converted.value;
1324
+ if (is_bigint(slots[0]) != is_bigint(slots[1])) {
1325
+ oseo_roots_pop(context, &frame);
1326
+ return oseo_internal_throw_error(
1327
+ context,
1328
+ OSEO_ERROR_TYPE,
1329
+ "Cannot mix BigInt and Number operands."
1330
+ );
1331
+ }
1332
+ if (is_bigint(slots[0])) {
1333
+ OseoBigIntOperator operation = OSEO_BIGINT_DIVIDE;
1334
+ if (operator == '+') operation = OSEO_BIGINT_ADD;
1335
+ else if (operator == '-') operation = OSEO_BIGINT_SUBTRACT;
1336
+ else if (operator == '*') operation = OSEO_BIGINT_MULTIPLY;
1337
+ else if (operator == '%') operation = OSEO_BIGINT_REMAINDER;
1338
+ else if (operator == 'e') operation = OSEO_BIGINT_EXPONENTIATE;
1339
+ OseoResult result = oseo_internal_bigint_binary(
1340
+ context,
1341
+ slots[0],
1342
+ slots[1],
1343
+ operation
1344
+ );
1345
+ oseo_roots_pop(context, &frame);
1346
+ return result;
1347
+ }
1348
+ double left_value = number_value(slots[0]);
1349
+ double right_value = number_value(slots[1]);
1350
+ double value;
1351
+ if (operator == '+') value = left_value + right_value;
1352
+ else if (operator == '-') value = left_value - right_value;
1353
+ else if (operator == '*') value = left_value * right_value;
1354
+ else if (operator == '%') value = fmod(left_value, right_value);
1355
+ else if (operator == 'e') {
1356
+ value = oseo_internal_number_exponentiate(left_value, right_value);
1357
+ }
1358
+ else value = left_value / right_value;
1359
+ oseo_roots_pop(context, &frame);
1360
+ return normal(oseo_number(value));
1361
+ }
1362
+
1363
+ OseoResult oseo_add(
1364
+ OseoContext *context,
1365
+ OseoValue left,
1366
+ OseoValue right
1367
+ ) {
1368
+ if (context->observe_specialization) {
1369
+ context->generic_addition_calls += 1u;
1370
+ }
1371
+ OseoValue slots[2] = {left, right};
1372
+ OseoRootFrame frame = {NULL, slots, 2u};
1373
+ oseo_roots_push(context, &frame);
1374
+ if (is_object(slots[0])) {
1375
+ OseoResult converted = to_primitive_value(
1376
+ context,
1377
+ slots[0],
1378
+ OSEO_TO_PRIMITIVE_DEFAULT,
1379
+ NULL
1380
+ );
1381
+ if (converted.status != OSEO_STATUS_NORMAL) {
1382
+ oseo_roots_pop(context, &frame);
1383
+ return converted;
1384
+ }
1385
+ slots[0] = converted.value;
1386
+ }
1387
+ if (is_object(slots[1])) {
1388
+ OseoResult converted = to_primitive_value(
1389
+ context,
1390
+ slots[1],
1391
+ OSEO_TO_PRIMITIVE_DEFAULT,
1392
+ NULL
1393
+ );
1394
+ if (converted.status != OSEO_STATUS_NORMAL) {
1395
+ oseo_roots_pop(context, &frame);
1396
+ return converted;
1397
+ }
1398
+ slots[1] = converted.value;
1399
+ }
1400
+ if (!is_string(slots[0]) && !is_string(slots[1])) {
1401
+ OseoResult numeric =
1402
+ numeric_binary(context, slots[0], slots[1], '+');
1403
+ oseo_roots_pop(context, &frame);
1404
+ return numeric;
1405
+ }
1406
+ OseoResult left_string = oseo_internal_value_string(context, slots[0]);
1407
+ if (left_string.status != OSEO_STATUS_NORMAL) {
1408
+ oseo_roots_pop(context, &frame);
1409
+ return left_string;
1410
+ }
1411
+ slots[0] = left_string.value;
1412
+ OseoResult right_string = oseo_internal_value_string(context, slots[1]);
1413
+ if (right_string.status != OSEO_STATUS_NORMAL) {
1414
+ oseo_roots_pop(context, &frame);
1415
+ return right_string;
1416
+ }
1417
+ slots[1] = right_string.value;
1418
+ OseoString *left_object = string_object(slots[0]);
1419
+ OseoString *right_object = string_object(slots[1]);
1420
+ size_t maximum_length =
1421
+ (SIZE_MAX - sizeof(OseoString)) / sizeof(uint16_t);
1422
+ if (left_object->length > maximum_length ||
1423
+ right_object->length > maximum_length - left_object->length) {
1424
+ oseo_roots_pop(context, &frame);
1425
+ return failure(
1426
+ context,
1427
+ "OSEO2001",
1428
+ "String allocation is too large."
1429
+ );
1430
+ }
1431
+ size_t length = left_object->length + right_object->length;
1432
+ OseoResult valid = oseo_internal_validate_string_length(context, length);
1433
+ if (valid.status != OSEO_STATUS_NORMAL) {
1434
+ oseo_roots_pop(context, &frame);
1435
+ return valid;
1436
+ }
1437
+ uint16_t *units = length == 0u
1438
+ ? NULL
1439
+ : malloc(length * sizeof(uint16_t));
1440
+ if (units == NULL && length > 0u) {
1441
+ oseo_roots_pop(context, &frame);
1442
+ return failure(context, "OSEO2001", "String allocation failed.");
1443
+ }
1444
+ if (left_object->length > 0u) {
1445
+ memcpy(
1446
+ units,
1447
+ left_object->units,
1448
+ left_object->length * sizeof(uint16_t)
1449
+ );
1450
+ }
1451
+ if (right_object->length > 0u) {
1452
+ memcpy(
1453
+ units + left_object->length,
1454
+ right_object->units,
1455
+ right_object->length * sizeof(uint16_t)
1456
+ );
1457
+ }
1458
+ OseoResult result = oseo_internal_allocate_string(context, units, length);
1459
+ free(units);
1460
+ oseo_roots_pop(context, &frame);
1461
+ return result;
1462
+ }
1463
+
1464
+ OseoResult oseo_subtract(
1465
+ OseoContext *context,
1466
+ OseoValue left,
1467
+ OseoValue right
1468
+ ) {
1469
+ return numeric_binary(context, left, right, '-');
1470
+ }
1471
+
1472
+ OseoResult oseo_multiply(
1473
+ OseoContext *context,
1474
+ OseoValue left,
1475
+ OseoValue right
1476
+ ) {
1477
+ return numeric_binary(context, left, right, '*');
1478
+ }
1479
+
1480
+ OseoResult oseo_divide(
1481
+ OseoContext *context,
1482
+ OseoValue left,
1483
+ OseoValue right
1484
+ ) {
1485
+ return numeric_binary(context, left, right, '/');
1486
+ }
1487
+
1488
+ OseoResult oseo_remainder(
1489
+ OseoContext *context,
1490
+ OseoValue left,
1491
+ OseoValue right
1492
+ ) {
1493
+ return numeric_binary(context, left, right, '%');
1494
+ }
1495
+
1496
+ OseoResult oseo_to_number(OseoContext *context, OseoValue value) {
1497
+ return oseo_internal_to_number(context, value);
1498
+ }
1499
+
1500
+ OseoResult oseo_to_numeric(OseoContext *context, OseoValue value) {
1501
+ OseoResult primitive = to_primitive_value(
1502
+ context,
1503
+ value,
1504
+ OSEO_TO_PRIMITIVE_NUMBER,
1505
+ NULL
1506
+ );
1507
+ if (primitive.status != OSEO_STATUS_NORMAL) return primitive;
1508
+ if (is_bigint(primitive.value)) return primitive;
1509
+ return oseo_internal_to_number(context, primitive.value);
1510
+ }
1511
+
1512
+ OseoResult oseo_numeric_one(OseoContext *context, OseoValue value) {
1513
+ if (is_bigint(value)) return oseo_bigint_literal(context, "1", 10u);
1514
+ if (is_number(value)) return normal(oseo_number(1.0));
1515
+ return failure(context, "OSEO2001", "Value is not numeric.");
1516
+ }
1517
+
1518
+ OseoResult oseo_to_string(OseoContext *context, OseoValue value) {
1519
+ return value_text(context, value, NULL);
1520
+ }
1521
+
1522
+ OseoResult oseo_exponentiate(
1523
+ OseoContext *context,
1524
+ OseoValue left,
1525
+ OseoValue right
1526
+ ) {
1527
+ return numeric_binary(context, left, right, 'e');
1528
+ }
1529
+
1530
+ double oseo_internal_number_exponentiate(double base, double exponent) {
1531
+ /*
1532
+ * Number::exponentiate differs from C `pow` in exactly two cases:
1533
+ * a NaN exponent is NaN even for a base of one, and a base of
1534
+ * exactly one or minus one with an infinite exponent is NaN rather
1535
+ * than one. Every other case agrees, so the library result stands.
1536
+ */
1537
+ if (isnan(exponent)) return NAN;
1538
+ if (fabs(base) == 1.0 && isinf(exponent)) return NAN;
1539
+ return pow(base, exponent);
1540
+ }
1541
+
1542
+ uint32_t oseo_internal_number_to_uint32(double number) {
1543
+ if (!isfinite(number) || number == 0.0) return 0u;
1544
+ double wrapped = fmod(trunc(number), 4294967296.0);
1545
+ if (wrapped < 0.0) wrapped += 4294967296.0;
1546
+ return (uint32_t)wrapped;
1547
+ }
1548
+
1549
+ static double int32_number(uint32_t bits) {
1550
+ return bits >= 2147483648u
1551
+ ? (double)((int64_t)bits - INT64_C(4294967296))
1552
+ : (double)bits;
1553
+ }
1554
+
1555
+ static OseoResult int32_binary(
1556
+ OseoContext *context,
1557
+ OseoValue left,
1558
+ OseoValue right,
1559
+ char operator
1560
+ ) {
1561
+ OseoValue slots[2] = {left, right};
1562
+ OseoRootFrame frame = {NULL, slots, 2u};
1563
+ oseo_roots_push(context, &frame);
1564
+ OseoResult converted = oseo_to_numeric(context, slots[0]);
1565
+ if (converted.status != OSEO_STATUS_NORMAL) {
1566
+ oseo_roots_pop(context, &frame);
1567
+ return converted;
1568
+ }
1569
+ slots[0] = converted.value;
1570
+ converted = oseo_to_numeric(context, slots[1]);
1571
+ if (converted.status != OSEO_STATUS_NORMAL) {
1572
+ oseo_roots_pop(context, &frame);
1573
+ return converted;
1574
+ }
1575
+ slots[1] = converted.value;
1576
+ if (is_bigint(slots[0]) != is_bigint(slots[1])) {
1577
+ oseo_roots_pop(context, &frame);
1578
+ return oseo_internal_throw_error(
1579
+ context,
1580
+ OSEO_ERROR_TYPE,
1581
+ "Cannot mix BigInt and Number operands."
1582
+ );
1583
+ }
1584
+ if (is_bigint(slots[0])) {
1585
+ if (operator == 'u') {
1586
+ oseo_roots_pop(context, &frame);
1587
+ return oseo_internal_throw_error(
1588
+ context,
1589
+ OSEO_ERROR_TYPE,
1590
+ "BigInt has no unsigned right shift."
1591
+ );
1592
+ }
1593
+ OseoBigIntOperator operation = OSEO_BIGINT_SHIFT_RIGHT;
1594
+ if (operator == '&') operation = OSEO_BIGINT_AND;
1595
+ else if (operator == '|') operation = OSEO_BIGINT_OR;
1596
+ else if (operator == '^') operation = OSEO_BIGINT_XOR;
1597
+ else if (operator == '<') operation = OSEO_BIGINT_SHIFT_LEFT;
1598
+ OseoResult result = oseo_internal_bigint_binary(
1599
+ context,
1600
+ slots[0],
1601
+ slots[1],
1602
+ operation
1603
+ );
1604
+ oseo_roots_pop(context, &frame);
1605
+ return result;
1606
+ }
1607
+ uint32_t left_bits =
1608
+ oseo_internal_number_to_uint32(number_value(slots[0]));
1609
+ uint32_t right_bits =
1610
+ oseo_internal_number_to_uint32(number_value(slots[1]));
1611
+ if (operator == '&') {
1612
+ converted = normal(oseo_number(int32_number(left_bits & right_bits)));
1613
+ oseo_roots_pop(context, &frame);
1614
+ return converted;
1615
+ }
1616
+ if (operator == '|') {
1617
+ converted = normal(oseo_number(int32_number(left_bits | right_bits)));
1618
+ oseo_roots_pop(context, &frame);
1619
+ return converted;
1620
+ }
1621
+ if (operator == '^') {
1622
+ converted = normal(oseo_number(int32_number(left_bits ^ right_bits)));
1623
+ oseo_roots_pop(context, &frame);
1624
+ return converted;
1625
+ }
1626
+ uint32_t shift = right_bits & 31u;
1627
+ if (operator == '<') {
1628
+ converted = normal(oseo_number(int32_number(left_bits << shift)));
1629
+ oseo_roots_pop(context, &frame);
1630
+ return converted;
1631
+ }
1632
+ if (operator == '>') {
1633
+ uint32_t shifted = (left_bits & 2147483648u) != 0u
1634
+ ? ~(~left_bits >> shift)
1635
+ : left_bits >> shift;
1636
+ converted = normal(oseo_number(int32_number(shifted)));
1637
+ oseo_roots_pop(context, &frame);
1638
+ return converted;
1639
+ }
1640
+ converted = normal(oseo_number((double)(left_bits >> shift)));
1641
+ oseo_roots_pop(context, &frame);
1642
+ return converted;
1643
+ }
1644
+
1645
+ OseoResult oseo_bitwise_and(
1646
+ OseoContext *context,
1647
+ OseoValue left,
1648
+ OseoValue right
1649
+ ) {
1650
+ return int32_binary(context, left, right, '&');
1651
+ }
1652
+
1653
+ OseoResult oseo_bitwise_or(
1654
+ OseoContext *context,
1655
+ OseoValue left,
1656
+ OseoValue right
1657
+ ) {
1658
+ return int32_binary(context, left, right, '|');
1659
+ }
1660
+
1661
+ OseoResult oseo_bitwise_xor(
1662
+ OseoContext *context,
1663
+ OseoValue left,
1664
+ OseoValue right
1665
+ ) {
1666
+ return int32_binary(context, left, right, '^');
1667
+ }
1668
+
1669
+ OseoResult oseo_shift_left(
1670
+ OseoContext *context,
1671
+ OseoValue left,
1672
+ OseoValue right
1673
+ ) {
1674
+ return int32_binary(context, left, right, '<');
1675
+ }
1676
+
1677
+ OseoResult oseo_shift_right(
1678
+ OseoContext *context,
1679
+ OseoValue left,
1680
+ OseoValue right
1681
+ ) {
1682
+ return int32_binary(context, left, right, '>');
1683
+ }
1684
+
1685
+ OseoResult oseo_shift_right_unsigned(
1686
+ OseoContext *context,
1687
+ OseoValue left,
1688
+ OseoValue right
1689
+ ) {
1690
+ return int32_binary(context, left, right, 'u');
1691
+ }
1692
+
1693
+ OseoResult oseo_bitwise_not(OseoContext *context, OseoValue value) {
1694
+ OseoResult number = oseo_to_numeric(context, value);
1695
+ if (number.status != OSEO_STATUS_NORMAL) return number;
1696
+ if (is_bigint(number.value)) {
1697
+ OseoValue slot = number.value;
1698
+ OseoRootFrame frame = {NULL, &slot, 1u};
1699
+ oseo_roots_push(context, &frame);
1700
+ OseoResult result = oseo_internal_bigint_not(context, slot);
1701
+ oseo_roots_pop(context, &frame);
1702
+ return result;
1703
+ }
1704
+ uint32_t bits = ~oseo_internal_number_to_uint32(number_value(number.value));
1705
+ return normal(oseo_number(int32_number(bits)));
1706
+ }
1707
+
1708
+ static bool strict_equal_value(OseoValue left, OseoValue right) {
1709
+ if (is_number(left) && is_number(right)) {
1710
+ double left_number = number_value(left);
1711
+ double right_number = number_value(right);
1712
+ return !isnan(left_number) && !isnan(right_number) &&
1713
+ left_number == right_number;
1714
+ }
1715
+ if (is_bigint(left) && is_bigint(right)) {
1716
+ return oseo_internal_bigint_equal(left, right);
1717
+ }
1718
+ uint64_t left_tag = tag_of(left);
1719
+ if (left_tag != tag_of(right)) return false;
1720
+ if (left_tag == OSEO_TAG_UNDEFINED || left_tag == OSEO_TAG_NULL) {
1721
+ return true;
1722
+ }
1723
+ if (left_tag == OSEO_TAG_BOOLEAN) return (left & 1u) == (right & 1u);
1724
+ if (is_string(left) && is_string(right)) {
1725
+ OseoString *left_string = string_object(left);
1726
+ OseoString *right_string = string_object(right);
1727
+ return left_string->length == right_string->length &&
1728
+ memcmp(
1729
+ left_string->units,
1730
+ right_string->units,
1731
+ left_string->length * sizeof(uint16_t)
1732
+ ) == 0;
1733
+ }
1734
+ if (left_tag == OSEO_TAG_HEAP) {
1735
+ return left == right || same_registered_symbol(left, right);
1736
+ }
1737
+ return false;
1738
+ }
1739
+
1740
+ OseoResult oseo_strict_equal(
1741
+ OseoContext *context,
1742
+ OseoValue left,
1743
+ OseoValue right
1744
+ ) {
1745
+ (void)context;
1746
+ return normal(oseo_boolean(strict_equal_value(left, right)));
1747
+ }
1748
+
1749
+ /* IsLooselyEqual for the admitted values; objects compare by identity and
1750
+ * object-to-primitive coercion keeps the shared unsupported boundary. */
1751
+ static OseoResult loose_equal_value(
1752
+ OseoContext *context,
1753
+ OseoValue left,
1754
+ OseoValue right,
1755
+ bool *equal
1756
+ ) {
1757
+ if (is_number(left) && is_number(right)) {
1758
+ *equal = strict_equal_value(left, right);
1759
+ return normal(oseo_undefined());
1760
+ }
1761
+ if (is_bigint(left) && is_bigint(right)) {
1762
+ *equal = oseo_internal_bigint_equal(left, right);
1763
+ return normal(oseo_undefined());
1764
+ }
1765
+ if ((is_bigint(left) && is_number(right)) ||
1766
+ (is_number(left) && is_bigint(right))) {
1767
+ OseoValue integer = is_bigint(left) ? left : right;
1768
+ double number = number_value(is_number(left) ? left : right);
1769
+ *equal = isfinite(number) && trunc(number) == number &&
1770
+ oseo_internal_bigint_compare_number(integer, number) == 0;
1771
+ return normal(oseo_undefined());
1772
+ }
1773
+ uint64_t left_tag = tag_of(left);
1774
+ uint64_t right_tag = tag_of(right);
1775
+ bool left_nullish =
1776
+ left_tag == OSEO_TAG_UNDEFINED || left_tag == OSEO_TAG_NULL;
1777
+ bool right_nullish =
1778
+ right_tag == OSEO_TAG_UNDEFINED || right_tag == OSEO_TAG_NULL;
1779
+ if (left_nullish || right_nullish) {
1780
+ *equal = left_nullish && right_nullish;
1781
+ return normal(oseo_undefined());
1782
+ }
1783
+ if ((is_symbol(left) || is_symbol(right)) &&
1784
+ !is_object(left) && !is_object(right)) {
1785
+ *equal = left == right || same_registered_symbol(left, right);
1786
+ return normal(oseo_undefined());
1787
+ }
1788
+ if (left_tag == OSEO_TAG_BOOLEAN) {
1789
+ OseoResult number = oseo_internal_to_number(context, left);
1790
+ if (number.status != OSEO_STATUS_NORMAL) return number;
1791
+ return loose_equal_value(context, number.value, right, equal);
1792
+ }
1793
+ if (right_tag == OSEO_TAG_BOOLEAN) {
1794
+ OseoResult number = oseo_internal_to_number(context, right);
1795
+ if (number.status != OSEO_STATUS_NORMAL) return number;
1796
+ return loose_equal_value(context, left, number.value, equal);
1797
+ }
1798
+ if (is_string(left) && is_string(right)) {
1799
+ *equal = strict_equal_value(left, right);
1800
+ return normal(oseo_undefined());
1801
+ }
1802
+ if ((is_bigint(left) && is_string(right)) ||
1803
+ (is_string(left) && is_bigint(right))) {
1804
+ OseoValue slots[2] = {left, right};
1805
+ OseoRootFrame frame = {NULL, slots, 2u};
1806
+ oseo_roots_push(context, &frame);
1807
+ OseoValue integer = is_bigint(slots[0]) ? slots[0] : slots[1];
1808
+ OseoString *string = string_object(
1809
+ is_string(slots[0]) ? slots[0] : slots[1]
1810
+ );
1811
+ bool valid = false;
1812
+ OseoResult converted = oseo_internal_string_to_bigint(
1813
+ context,
1814
+ string,
1815
+ &valid
1816
+ );
1817
+ if (converted.status == OSEO_STATUS_NORMAL) {
1818
+ *equal = valid &&
1819
+ oseo_internal_bigint_equal(integer, converted.value);
1820
+ converted = normal(oseo_undefined());
1821
+ }
1822
+ oseo_roots_pop(context, &frame);
1823
+ return converted;
1824
+ }
1825
+ if (is_number(left) && is_string(right)) {
1826
+ OseoResult converted = string_number(context, string_object(right));
1827
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
1828
+ return loose_equal_value(context, left, converted.value, equal);
1829
+ }
1830
+ if (is_string(left) && is_number(right)) {
1831
+ OseoResult converted = string_number(context, string_object(left));
1832
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
1833
+ return loose_equal_value(context, converted.value, right, equal);
1834
+ }
1835
+ if (is_object(left) && is_object(right)) {
1836
+ *equal = left == right;
1837
+ return normal(oseo_undefined());
1838
+ }
1839
+ if (is_object(left)) {
1840
+ OseoValue slots[2] = {left, right};
1841
+ OseoRootFrame frame = {NULL, slots, 2u};
1842
+ oseo_roots_push(context, &frame);
1843
+ OseoResult converted = to_primitive_value(
1844
+ context,
1845
+ slots[0],
1846
+ OSEO_TO_PRIMITIVE_DEFAULT,
1847
+ NULL
1848
+ );
1849
+ if (converted.status == OSEO_STATUS_NORMAL) {
1850
+ slots[0] = converted.value;
1851
+ converted = loose_equal_value(
1852
+ context,
1853
+ slots[0],
1854
+ slots[1],
1855
+ equal
1856
+ );
1857
+ }
1858
+ oseo_roots_pop(context, &frame);
1859
+ return converted;
1860
+ }
1861
+ if (is_object(right)) {
1862
+ OseoValue slots[2] = {left, right};
1863
+ OseoRootFrame frame = {NULL, slots, 2u};
1864
+ oseo_roots_push(context, &frame);
1865
+ OseoResult converted = to_primitive_value(
1866
+ context,
1867
+ slots[1],
1868
+ OSEO_TO_PRIMITIVE_DEFAULT,
1869
+ NULL
1870
+ );
1871
+ if (converted.status == OSEO_STATUS_NORMAL) {
1872
+ slots[1] = converted.value;
1873
+ converted = loose_equal_value(
1874
+ context,
1875
+ slots[0],
1876
+ slots[1],
1877
+ equal
1878
+ );
1879
+ }
1880
+ oseo_roots_pop(context, &frame);
1881
+ return converted;
1882
+ }
1883
+ return failure(
1884
+ context,
1885
+ "OSEO2001",
1886
+ "Object-to-primitive conversion is unsupported."
1887
+ );
1888
+ }
1889
+
1890
+ OseoResult oseo_loose_equal(
1891
+ OseoContext *context,
1892
+ OseoValue left,
1893
+ OseoValue right
1894
+ ) {
1895
+ bool equal = false;
1896
+ OseoResult status = loose_equal_value(context, left, right, &equal);
1897
+ if (status.status != OSEO_STATUS_NORMAL) return status;
1898
+ return normal(oseo_boolean(equal));
1899
+ }
1900
+
1901
+ OseoResult oseo_not_loose_equal(
1902
+ OseoContext *context,
1903
+ OseoValue left,
1904
+ OseoValue right
1905
+ ) {
1906
+ bool equal = false;
1907
+ OseoResult status = loose_equal_value(context, left, right, &equal);
1908
+ if (status.status != OSEO_STATUS_NORMAL) return status;
1909
+ return normal(oseo_boolean(!equal));
1910
+ }
1911
+
1912
+ OseoResult oseo_has_property(
1913
+ OseoContext *context,
1914
+ OseoValue key,
1915
+ OseoValue object_value
1916
+ ) {
1917
+ if (!is_object(object_value)) {
1918
+ return oseo_internal_throw_error(
1919
+ context,
1920
+ OSEO_ERROR_TYPE,
1921
+ "The in operator requires an object."
1922
+ );
1923
+ }
1924
+ OseoValue slots[2] = {key, object_value};
1925
+ OseoRootFrame frame = {NULL, slots, 2u};
1926
+ oseo_roots_push(context, &frame);
1927
+ OseoResult converted = oseo_property_key(context, slots[0]);
1928
+ oseo_roots_pop(context, &frame);
1929
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
1930
+ OseoValue property = converted.value;
1931
+ OseoValue current = slots[1];
1932
+ while (is_object(current)) {
1933
+ if (is_proxy(current)) {
1934
+ return oseo_internal_proxy_has(context, current, property);
1935
+ }
1936
+ if (is_typed_array(current)) {
1937
+ /* A view's [[HasProperty]] answers every canonical
1938
+ * numeric key without consulting its prototype. */
1939
+ bool numeric = false;
1940
+ size_t typed_index = SIZE_MAX;
1941
+ OseoResult classified = oseo_internal_typed_array_numeric_key(
1942
+ context,
1943
+ property,
1944
+ &numeric,
1945
+ &typed_index
1946
+ );
1947
+ if (classified.status != OSEO_STATUS_NORMAL) return classified;
1948
+ if (numeric) {
1949
+ return normal(oseo_boolean(
1950
+ oseo_internal_typed_array_has_index(current, typed_index)
1951
+ ));
1952
+ }
1953
+ }
1954
+ OseoValue value = oseo_undefined();
1955
+ OseoPropertyAttributes attributes = {false, false, false, false};
1956
+ OseoValue ignored_getter = oseo_undefined();
1957
+ OseoValue ignored_setter = oseo_undefined();
1958
+ if (oseo_internal_own_property_descriptor(
1959
+ context, current, property, &value, &attributes,
1960
+ &ignored_getter, &ignored_setter)) {
1961
+ return normal(oseo_boolean(true));
1962
+ }
1963
+ OseoResult prototype = oseo_internal_get_prototype(context, current);
1964
+ if (prototype.status != OSEO_STATUS_NORMAL) return prototype;
1965
+ current = prototype.value;
1966
+ }
1967
+ return normal(oseo_boolean(false));
1968
+ }
1969
+
1970
+ OseoResult oseo_instanceof(
1971
+ OseoContext *context,
1972
+ OseoValue left,
1973
+ OseoValue right
1974
+ ) {
1975
+ OseoValue slots[4] = {
1976
+ left,
1977
+ right,
1978
+ oseo_undefined(),
1979
+ oseo_undefined(),
1980
+ };
1981
+ OseoRootFrame frame = {NULL, slots, 4u};
1982
+ oseo_roots_push(context, &frame);
1983
+ OseoResult result = oseo_internal_well_known_symbol(
1984
+ context,
1985
+ OSEO_WELL_KNOWN_HAS_INSTANCE
1986
+ );
1987
+ slots[2] = result.value;
1988
+ if (result.status == OSEO_STATUS_NORMAL) {
1989
+ result = oseo_object_get(context, slots[1], slots[2]);
1990
+ slots[3] = result.value;
1991
+ }
1992
+ if (result.status == OSEO_STATUS_NORMAL && !is_nullish(slots[3])) {
1993
+ if (!is_callable(slots[3])) {
1994
+ result = oseo_internal_throw_error(
1995
+ context,
1996
+ OSEO_ERROR_TYPE,
1997
+ "Symbol.hasInstance is not callable."
1998
+ );
1999
+ } else {
2000
+ result = oseo_call_function(
2001
+ context,
2002
+ slots[3],
2003
+ slots[1],
2004
+ 1u,
2005
+ &slots[0],
2006
+ oseo_undefined()
2007
+ );
2008
+ if (result.status == OSEO_STATUS_NORMAL) {
2009
+ result = normal(oseo_boolean(oseo_to_boolean(result.value)));
2010
+ }
2011
+ }
2012
+ } else if (result.status == OSEO_STATUS_NORMAL) {
2013
+ if (!is_callable(slots[1])) {
2014
+ result = oseo_internal_throw_error(
2015
+ context,
2016
+ OSEO_ERROR_TYPE,
2017
+ "The instanceof right operand must be callable."
2018
+ );
2019
+ } else {
2020
+ result = oseo_internal_ordinary_has_instance(
2021
+ context,
2022
+ slots[1],
2023
+ slots[0]
2024
+ );
2025
+ }
2026
+ }
2027
+ oseo_roots_pop(context, &frame);
2028
+ return result;
2029
+ }
2030
+
2031
+ OseoResult oseo_not_strict_equal(
2032
+ OseoContext *context,
2033
+ OseoValue left,
2034
+ OseoValue right
2035
+ ) {
2036
+ (void)context;
2037
+ return normal(oseo_boolean(!strict_equal_value(left, right)));
2038
+ }
2039
+
2040
+ static int compare_strings(
2041
+ const OseoString *left,
2042
+ const OseoString *right
2043
+ ) {
2044
+ size_t length = left->length < right->length
2045
+ ? left->length
2046
+ : right->length;
2047
+ for (size_t index = 0u; index < length; index += 1u) {
2048
+ if (left->units[index] < right->units[index]) return -1;
2049
+ if (left->units[index] > right->units[index]) return 1;
2050
+ }
2051
+ if (left->length < right->length) return -1;
2052
+ if (left->length > right->length) return 1;
2053
+ return 0;
2054
+ }
2055
+
2056
+ static OseoResult relational(
2057
+ OseoContext *context,
2058
+ OseoValue left,
2059
+ OseoValue right,
2060
+ char operator
2061
+ ) {
2062
+ if (is_object(left) || is_object(right)) {
2063
+ OseoValue slots[2] = {left, right};
2064
+ OseoRootFrame frame = {NULL, slots, 2u};
2065
+ oseo_roots_push(context, &frame);
2066
+ OseoResult converted = to_primitive_value(
2067
+ context,
2068
+ slots[0],
2069
+ OSEO_TO_PRIMITIVE_NUMBER,
2070
+ NULL
2071
+ );
2072
+ if (converted.status == OSEO_STATUS_NORMAL) {
2073
+ slots[0] = converted.value;
2074
+ converted = to_primitive_value(
2075
+ context,
2076
+ slots[1],
2077
+ OSEO_TO_PRIMITIVE_NUMBER,
2078
+ NULL
2079
+ );
2080
+ slots[1] = converted.value;
2081
+ }
2082
+ if (converted.status == OSEO_STATUS_NORMAL) {
2083
+ converted = relational(context, slots[0], slots[1], operator);
2084
+ }
2085
+ oseo_roots_pop(context, &frame);
2086
+ return converted;
2087
+ }
2088
+ if (is_string(left) && is_string(right)) {
2089
+ int order = compare_strings(string_object(left), string_object(right));
2090
+ bool result;
2091
+ if (operator == '<') result = order < 0;
2092
+ else if (operator == 'l') result = order <= 0;
2093
+ else if (operator == '>') result = order > 0;
2094
+ else result = order >= 0;
2095
+ return normal(oseo_boolean(result));
2096
+ }
2097
+ if ((is_bigint(left) && is_string(right)) ||
2098
+ (is_string(left) && is_bigint(right))) {
2099
+ OseoValue slots[2] = {left, right};
2100
+ OseoRootFrame frame = {NULL, slots, 2u};
2101
+ oseo_roots_push(context, &frame);
2102
+ bool left_bigint = is_bigint(slots[0]);
2103
+ OseoValue integer = left_bigint ? slots[0] : slots[1];
2104
+ OseoString *string = string_object(
2105
+ left_bigint ? slots[1] : slots[0]
2106
+ );
2107
+ bool valid = false;
2108
+ OseoResult converted = oseo_internal_string_to_bigint(
2109
+ context,
2110
+ string,
2111
+ &valid
2112
+ );
2113
+ if (converted.status == OSEO_STATUS_NORMAL) {
2114
+ if (!valid) {
2115
+ converted = normal(oseo_boolean(false));
2116
+ } else {
2117
+ int order = left_bigint
2118
+ ? oseo_internal_bigint_compare(integer, converted.value)
2119
+ : oseo_internal_bigint_compare(
2120
+ converted.value,
2121
+ integer
2122
+ );
2123
+ bool result = operator == '<'
2124
+ ? order < 0
2125
+ : operator == 'l'
2126
+ ? order <= 0
2127
+ : operator == '>'
2128
+ ? order > 0
2129
+ : order >= 0;
2130
+ converted = normal(oseo_boolean(result));
2131
+ }
2132
+ }
2133
+ oseo_roots_pop(context, &frame);
2134
+ return converted;
2135
+ }
2136
+ OseoValue slots[2] = {left, right};
2137
+ OseoRootFrame frame = {NULL, slots, 2u};
2138
+ oseo_roots_push(context, &frame);
2139
+ OseoResult converted = oseo_to_numeric(context, slots[0]);
2140
+ if (converted.status != OSEO_STATUS_NORMAL) {
2141
+ oseo_roots_pop(context, &frame);
2142
+ return converted;
2143
+ }
2144
+ slots[0] = converted.value;
2145
+ converted = oseo_to_numeric(context, slots[1]);
2146
+ if (converted.status != OSEO_STATUS_NORMAL) {
2147
+ oseo_roots_pop(context, &frame);
2148
+ return converted;
2149
+ }
2150
+ slots[1] = converted.value;
2151
+ if (is_bigint(slots[0]) || is_bigint(slots[1])) {
2152
+ int order;
2153
+ bool unordered = false;
2154
+ if (is_bigint(slots[0]) && is_bigint(slots[1])) {
2155
+ order = oseo_internal_bigint_compare(slots[0], slots[1]);
2156
+ } else {
2157
+ OseoValue integer = is_bigint(slots[0]) ? slots[0] : slots[1];
2158
+ double number = number_value(
2159
+ is_number(slots[0]) ? slots[0] : slots[1]
2160
+ );
2161
+ unordered = isnan(number);
2162
+ order = unordered
2163
+ ? 0
2164
+ : oseo_internal_bigint_compare_number(integer, number);
2165
+ if (is_bigint(slots[1])) order = -order;
2166
+ }
2167
+ bool result = !unordered && (operator == '<'
2168
+ ? order < 0
2169
+ : operator == 'l'
2170
+ ? order <= 0
2171
+ : operator == '>'
2172
+ ? order > 0
2173
+ : order >= 0);
2174
+ oseo_roots_pop(context, &frame);
2175
+ return normal(oseo_boolean(result));
2176
+ }
2177
+ double left_value = number_value(slots[0]);
2178
+ double right_value = number_value(slots[1]);
2179
+ bool result = false;
2180
+ if (!isnan(left_value) && !isnan(right_value)) {
2181
+ if (operator == '<') result = left_value < right_value;
2182
+ else if (operator == 'l') result = left_value <= right_value;
2183
+ else if (operator == '>') result = left_value > right_value;
2184
+ else result = left_value >= right_value;
2185
+ }
2186
+ oseo_roots_pop(context, &frame);
2187
+ return normal(oseo_boolean(result));
2188
+ }
2189
+
2190
+ OseoResult oseo_less_than(
2191
+ OseoContext *context,
2192
+ OseoValue left,
2193
+ OseoValue right
2194
+ ) {
2195
+ return relational(context, left, right, '<');
2196
+ }
2197
+
2198
+ OseoResult oseo_less_equal(
2199
+ OseoContext *context,
2200
+ OseoValue left,
2201
+ OseoValue right
2202
+ ) {
2203
+ return relational(context, left, right, 'l');
2204
+ }
2205
+
2206
+ OseoResult oseo_greater_than(
2207
+ OseoContext *context,
2208
+ OseoValue left,
2209
+ OseoValue right
2210
+ ) {
2211
+ return relational(context, left, right, '>');
2212
+ }
2213
+
2214
+ OseoResult oseo_greater_equal(
2215
+ OseoContext *context,
2216
+ OseoValue left,
2217
+ OseoValue right
2218
+ ) {
2219
+ return relational(context, left, right, 'g');
2220
+ }
2221
+
2222
+ OseoResult oseo_console_log(
2223
+ OseoContext *context,
2224
+ size_t argument_count,
2225
+ const OseoValue *arguments
2226
+ ) {
2227
+ for (size_t index = 0u; index < argument_count; index += 1u) {
2228
+ if (index > 0u) {
2229
+ const unsigned char space = ' ';
2230
+ OseoResult written = oseo_internal_process_write(
2231
+ context, false, &space, 1u, false);
2232
+ if (written.status != OSEO_STATUS_NORMAL) return written;
2233
+ }
2234
+ OseoResult string = is_symbol(arguments[index])
2235
+ ? oseo_internal_symbol_text(context, arguments[index])
2236
+ : oseo_internal_value_string(context, arguments[index]);
2237
+ if (string.status != OSEO_STATUS_NORMAL) return string;
2238
+ OseoResult written = oseo_internal_process_write_string(
2239
+ context, false, string.value, false);
2240
+ if (written.status != OSEO_STATUS_NORMAL) return written;
2241
+ }
2242
+ const unsigned char newline = '\n';
2243
+ return oseo_internal_process_write(context, false, &newline, 1u, false);
2244
+ }