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