@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,2862 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+ #include "runtime_unicode_tables.h"
6
+
7
+ #include <math.h>
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+
12
+ /*
13
+ * String values and the string half of property keys: allocation,
14
+ * content equality, ASCII name matching, canonical array-index
15
+ * recognition, and the own properties a String exotic object exposes.
16
+ * Every component that compares, builds, or classifies a property key
17
+ * name reaches this unit through the internal header. The same unit
18
+ * owns the realm's `String` constructor, %String.prototype%, the String
19
+ * exotic objects `new String` creates, and the `fromCharCode`,
20
+ * `fromCodePoint`, and `raw` statics. Case conversion, trimming,
21
+ * normalization, and deterministic locale comparison read only the pinned
22
+ * tables in the generated runtime Unicode header.
23
+ */
24
+
25
+ OseoResult oseo_internal_validate_string_length(
26
+ OseoContext *context,
27
+ size_t length
28
+ ) {
29
+ if (length > OSEO_MAX_STRING_LENGTH) {
30
+ return oseo_internal_throw_error(
31
+ context,
32
+ OSEO_ERROR_RANGE,
33
+ "Invalid string length."
34
+ );
35
+ }
36
+ return normal(oseo_undefined());
37
+ }
38
+
39
+ /* FNV-1a over the code units, for the string table's index. */
40
+ static uint32_t string_table_hash(const uint16_t *units, size_t length) {
41
+ uint32_t hash = UINT32_C(2166136261);
42
+ for (size_t index = 0u; index < length; index += 1u) {
43
+ hash = (hash ^ (uint32_t)units[index]) * UINT32_C(16777619);
44
+ }
45
+ return hash;
46
+ }
47
+
48
+ /* Returns the shared string with exactly these units, or zero. */
49
+ static OseoValue string_table_find(
50
+ const OseoStringTable *table,
51
+ uint32_t hash,
52
+ const uint16_t *units,
53
+ size_t length
54
+ ) {
55
+ if (table == NULL || table->slot_capacity == 0u) return 0u;
56
+ size_t mask = table->slot_capacity - 1u;
57
+ for (size_t slot = hash & mask;; slot = (slot + 1u) & mask) {
58
+ uint32_t position = table->slots[slot];
59
+ if (position == 0u) return 0u;
60
+ OseoValue entry = table->entries[position - 1u];
61
+ const OseoString *string = string_object(entry);
62
+ if (string->length == length &&
63
+ (length == 0u ||
64
+ memcmp(string->units, units, length * sizeof(uint16_t)) == 0)) {
65
+ return entry;
66
+ }
67
+ }
68
+ }
69
+
70
+ /* Places entry `position` in an index with at least one empty slot. */
71
+ static void string_table_index(OseoStringTable *table, size_t position) {
72
+ size_t mask = table->slot_capacity - 1u;
73
+ size_t slot = table->hashes[position] & mask;
74
+ while (table->slots[slot] != 0u) slot = (slot + 1u) & mask;
75
+ table->slots[slot] = (uint32_t)(position + 1u);
76
+ }
77
+
78
+ /* Rebuilds the index of a table whose entries moved. */
79
+ static void string_table_reindex(OseoStringTable *table) {
80
+ memset(table->slots, 0, table->slot_capacity * sizeof(*table->slots));
81
+ for (size_t position = 0u; position < table->count; position += 1u) {
82
+ string_table_index(table, position);
83
+ }
84
+ }
85
+
86
+ /*
87
+ * Shares a freshly published string. Growth uses the ordinary allocator
88
+ * without a safepoint, and a failed growth only leaves the string
89
+ * unshared, so neither path changes what the caller observes.
90
+ */
91
+ static void string_table_insert(
92
+ OseoContext *context,
93
+ OseoValue value,
94
+ uint32_t hash
95
+ ) {
96
+ OseoStringTable *table = context->string_table;
97
+ if (table == NULL) {
98
+ table = calloc(1u, sizeof(*table));
99
+ if (table == NULL) return;
100
+ context->string_table = table;
101
+ }
102
+ if (table->count == UINT32_MAX - 1u) return;
103
+ if (table->count == table->capacity) {
104
+ size_t capacity = table->capacity == 0u ? 64u : table->capacity * 2u;
105
+ if (capacity > SIZE_MAX / sizeof(OseoValue)) return;
106
+ OseoValue *entries =
107
+ realloc(table->entries, capacity * sizeof(*entries));
108
+ if (entries == NULL) return;
109
+ table->entries = entries;
110
+ uint32_t *hashes = realloc(table->hashes, capacity * sizeof(*hashes));
111
+ if (hashes == NULL) return;
112
+ table->hashes = hashes;
113
+ table->capacity = capacity;
114
+ }
115
+ if ((table->count + 1u) * 2u > table->slot_capacity) {
116
+ size_t slot_capacity =
117
+ table->slot_capacity == 0u ? 128u : table->slot_capacity * 2u;
118
+ if (slot_capacity > SIZE_MAX / sizeof(uint32_t)) return;
119
+ uint32_t *slots = calloc(slot_capacity, sizeof(*slots));
120
+ if (slots == NULL) return;
121
+ free(table->slots);
122
+ table->slots = slots;
123
+ table->slot_capacity = slot_capacity;
124
+ string_table_reindex(table);
125
+ }
126
+ table->entries[table->count] = value;
127
+ table->hashes[table->count] = hash;
128
+ string_table_index(table, table->count);
129
+ table->count += 1u;
130
+ }
131
+
132
+ void oseo_internal_string_table_sweep(OseoContext *context) {
133
+ OseoStringTable *table = context->string_table;
134
+ if (table == NULL) return;
135
+ size_t kept = 0u;
136
+ for (size_t position = 0u; position < table->count; position += 1u) {
137
+ if (!heap_object(table->entries[position])->marked) continue;
138
+ table->entries[kept] = table->entries[position];
139
+ table->hashes[kept] = table->hashes[position];
140
+ kept += 1u;
141
+ }
142
+ if (kept == table->count) return;
143
+ table->count = kept;
144
+ string_table_reindex(table);
145
+ }
146
+
147
+ void oseo_internal_string_table_release(OseoContext *context) {
148
+ OseoStringTable *table = context->string_table;
149
+ if (table == NULL) return;
150
+ free(table->entries);
151
+ free(table->hashes);
152
+ free(table->slots);
153
+ free(table);
154
+ context->string_table = NULL;
155
+ }
156
+
157
+ static OseoResult allocate_string(
158
+ OseoContext *context,
159
+ const uint16_t *units,
160
+ size_t length,
161
+ bool intern
162
+ ) {
163
+ OseoResult valid = oseo_internal_validate_string_length(context, length);
164
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
165
+ bool shareable = length <= OSEO_STRING_TABLE_MAX_UNITS;
166
+ uint32_t hash = shareable ? string_table_hash(units, length) : 0u;
167
+ if (shareable) {
168
+ OseoValue shared =
169
+ string_table_find(context->string_table, hash, units, length);
170
+ if (shared != 0u) return normal(shared);
171
+ }
172
+ if (length > (SIZE_MAX - sizeof(OseoString)) / sizeof(uint16_t)) {
173
+ return failure(context, "OSEO2001", "String allocation is too large.");
174
+ }
175
+ size_t size = sizeof(OseoString) + length * sizeof(uint16_t);
176
+ OseoString *object = oseo_internal_allocate_heap_bytes(context, size);
177
+ if (object == NULL) {
178
+ return failure(context, "OSEO2001", "String allocation failed.");
179
+ }
180
+ object->length = length;
181
+ if (length > 0u) memcpy(object->units, units, length * sizeof(uint16_t));
182
+ OseoResult published = oseo_internal_publish_heap(
183
+ context, &object->header, OSEO_HEAP_STRING);
184
+ if (published.status == OSEO_STATUS_NORMAL && shareable && intern) {
185
+ string_table_insert(context, published.value, hash);
186
+ }
187
+ return published;
188
+ }
189
+
190
+ OseoResult oseo_internal_allocate_string(
191
+ OseoContext *context,
192
+ const uint16_t *units,
193
+ size_t length
194
+ ) {
195
+ return allocate_string(context, units, length, true);
196
+ }
197
+
198
+ OseoResult oseo_internal_allocate_uninterned_string(
199
+ OseoContext *context,
200
+ const uint16_t *units,
201
+ size_t length
202
+ ) {
203
+ return allocate_string(context, units, length, false);
204
+ }
205
+
206
+ OseoResult oseo_string_from_units(
207
+ OseoContext *context,
208
+ const uint16_t *units,
209
+ size_t length
210
+ ) {
211
+ return oseo_internal_allocate_string(context, units, length);
212
+ }
213
+
214
+ OseoResult oseo_internal_ascii_string(OseoContext *context, const char *text) {
215
+ uint16_t units[32];
216
+ size_t length = strlen(text);
217
+ if (length > sizeof(units) / sizeof(*units)) {
218
+ return failure(context, "OSEO2001", "Internal property name is long.");
219
+ }
220
+ for (size_t index = 0u; index < length; index += 1u) {
221
+ units[index] = (uint16_t)(unsigned char)text[index];
222
+ }
223
+ return oseo_internal_allocate_string(context, units, length);
224
+ }
225
+
226
+ bool oseo_internal_string_is_ascii(OseoValue value, const char *text) {
227
+ if (!is_string(value)) return false;
228
+ OseoString *string = string_object(value);
229
+ size_t length = strlen(text);
230
+ if (string->length != length) return false;
231
+ for (size_t index = 0u; index < length; index += 1u) {
232
+ if (string->units[index] !=
233
+ (uint16_t)(unsigned char)text[index]) return false;
234
+ }
235
+ return true;
236
+ }
237
+
238
+ bool oseo_internal_string_equal(OseoValue left, OseoValue right) {
239
+ if (!is_string(left) || !is_string(right)) return false;
240
+ OseoString *left_string = string_object(left);
241
+ OseoString *right_string = string_object(right);
242
+ return left_string->length == right_string->length &&
243
+ memcmp(
244
+ left_string->units,
245
+ right_string->units,
246
+ left_string->length * sizeof(uint16_t)
247
+ ) == 0;
248
+ }
249
+
250
+ /*
251
+ * Property keys are strings compared by content or symbols by identity,
252
+ * where two representatives of one registry entry name one symbol.
253
+ */
254
+ bool oseo_internal_property_key_equal(OseoValue left, OseoValue right) {
255
+ if (is_string(left) && is_string(right)) {
256
+ return oseo_internal_string_equal(left, right);
257
+ }
258
+ return left == right || same_registered_symbol(left, right);
259
+ }
260
+
261
+ bool oseo_internal_array_index(OseoValue key, uint32_t *result) {
262
+ if (!is_string(key)) return false;
263
+ OseoString *string = string_object(key);
264
+ if (string->length == 0u || string->length > 10u) return false;
265
+ if (string->length > 1u && string->units[0] == UINT16_C(0x30)) {
266
+ return false;
267
+ }
268
+ uint64_t value = 0u;
269
+ for (size_t index = 0u; index < string->length; index += 1u) {
270
+ uint16_t unit = string->units[index];
271
+ if (unit < UINT16_C(0x30) || unit > UINT16_C(0x39)) return false;
272
+ value = value * UINT64_C(10) + (uint64_t)(unit - UINT16_C(0x30));
273
+ if (value > UINT64_C(4294967294)) return false;
274
+ }
275
+ *result = (uint32_t)value;
276
+ return true;
277
+ }
278
+
279
+ bool oseo_internal_string_own_property(
280
+ OseoValue string_value,
281
+ OseoValue key,
282
+ uint32_t *index
283
+ ) {
284
+ if (!is_string(string_value)) return false;
285
+ if (oseo_internal_string_is_ascii(key, "length")) return true;
286
+ uint32_t candidate = 0u;
287
+ if (!oseo_internal_array_index(key, &candidate) ||
288
+ candidate >= string_object(string_value)->length) return false;
289
+ if (index != NULL) *index = candidate;
290
+ return true;
291
+ }
292
+
293
+ bool oseo_internal_string_data(OseoValue value) {
294
+ return is_object(value) && ordinary_object(value)->primitive_data &&
295
+ is_string(ordinary_object(value)->primitive_value);
296
+ }
297
+
298
+ /*
299
+ * True when the whole run of wrapper definitions may append without a
300
+ * duplicate-key scan. Every key the run produces is a distinct
301
+ * canonical index or `length`, and no user code runs between two of
302
+ * them, so an empty extensible ordinary object stays free of the
303
+ * collisions [[DefineOwnProperty]] exists to detect. A wrapper that
304
+ * arrives with any own property, or that is not an ordinary object,
305
+ * takes the ordinary definition path instead.
306
+ */
307
+ static bool string_wrapper_may_append(OseoValue wrapper) {
308
+ if (!is_object(wrapper) || is_array(wrapper) || is_function(wrapper)) {
309
+ return false;
310
+ }
311
+ const OseoOrdinaryObject *object = ordinary_object(wrapper);
312
+ return object->extensible && !object->module_namespace &&
313
+ object->property_count == 0u;
314
+ }
315
+
316
+ static OseoResult define_wrapper_property(
317
+ OseoContext *context,
318
+ bool append,
319
+ OseoValue wrapper,
320
+ OseoValue key,
321
+ OseoValue value,
322
+ OseoPropertyAttributes attributes
323
+ ) {
324
+ return append
325
+ ? oseo_internal_append_own_property(
326
+ context, wrapper, key, value, attributes)
327
+ : oseo_object_define(context, wrapper, key, value, attributes);
328
+ }
329
+
330
+ OseoResult oseo_internal_string_wrapper_properties(
331
+ OseoContext *context,
332
+ OseoValue string_value,
333
+ OseoValue wrapper
334
+ ) {
335
+ OseoRootFrame frame = {NULL, NULL, 0u};
336
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
337
+ if (result.status != OSEO_STATUS_NORMAL) return result;
338
+ frame.slots[0] = string_value;
339
+ frame.slots[1] = wrapper;
340
+ bool append = string_wrapper_may_append(frame.slots[1]);
341
+ size_t length = string_object(frame.slots[0])->length;
342
+ for (size_t index = 0u; index < length; index += 1u) {
343
+ char key_text[24];
344
+ (void)snprintf(key_text, sizeof(key_text), "%zu", index);
345
+ result = oseo_internal_ascii_string(context, key_text);
346
+ frame.slots[2] = result.value;
347
+ if (result.status != OSEO_STATUS_NORMAL) break;
348
+ uint16_t unit = string_object(frame.slots[0])->units[index];
349
+ result = oseo_internal_allocate_string(context, &unit, 1u);
350
+ frame.slots[3] = result.value;
351
+ if (result.status != OSEO_STATUS_NORMAL) break;
352
+ result = define_wrapper_property(
353
+ context,
354
+ append,
355
+ frame.slots[1],
356
+ frame.slots[2],
357
+ frame.slots[3],
358
+ (OseoPropertyAttributes){false, true, false, false}
359
+ );
360
+ if (result.status != OSEO_STATUS_NORMAL) break;
361
+ }
362
+ if (result.status == OSEO_STATUS_NORMAL) {
363
+ result = oseo_internal_ascii_string(context, "length");
364
+ frame.slots[2] = result.value;
365
+ }
366
+ if (result.status == OSEO_STATUS_NORMAL) {
367
+ result = define_wrapper_property(
368
+ context,
369
+ append,
370
+ frame.slots[1],
371
+ frame.slots[2],
372
+ oseo_number((double)string_object(frame.slots[0])->length),
373
+ (OseoPropertyAttributes){false, false, false, false}
374
+ );
375
+ }
376
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(frame.slots[1]);
377
+ oseo_roots_release(context, &frame);
378
+ return result;
379
+ }
380
+
381
+ static OseoResult string_builder_reserve(
382
+ OseoContext *context,
383
+ OseoStringBuilder *builder,
384
+ size_t additional
385
+ ) {
386
+ if (additional > SIZE_MAX - builder->length) {
387
+ return failure(context, "OSEO2001", "String allocation is too large.");
388
+ }
389
+ size_t required = builder->length + additional;
390
+ OseoResult valid = oseo_internal_validate_string_length(
391
+ context,
392
+ required
393
+ );
394
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
395
+ if (required <= builder->capacity) return normal(oseo_undefined());
396
+ size_t capacity = builder->capacity == 0u ? 16u : builder->capacity;
397
+ while (capacity < required) {
398
+ if (capacity > OSEO_MAX_STRING_LENGTH / 2u) {
399
+ capacity = required;
400
+ break;
401
+ }
402
+ capacity *= 2u;
403
+ }
404
+ uint16_t *units = realloc(builder->units, capacity * sizeof(uint16_t));
405
+ if (units == NULL) {
406
+ return failure(context, "OSEO2001", "String allocation failed.");
407
+ }
408
+ builder->units = units;
409
+ builder->capacity = capacity;
410
+ return normal(oseo_undefined());
411
+ }
412
+
413
+ static OseoResult string_builder_append_unit(
414
+ OseoContext *context,
415
+ OseoStringBuilder *builder,
416
+ uint16_t unit
417
+ ) {
418
+ OseoResult result = string_builder_reserve(context, builder, 1u);
419
+ if (result.status != OSEO_STATUS_NORMAL) return result;
420
+ builder->units[builder->length] = unit;
421
+ builder->length += 1u;
422
+ return result;
423
+ }
424
+
425
+ OseoResult oseo_internal_string_builder_append(
426
+ OseoContext *context,
427
+ OseoStringBuilder *builder,
428
+ const uint16_t *units,
429
+ size_t length
430
+ ) {
431
+ if (length == 0u) return normal(oseo_undefined());
432
+ OseoResult result = string_builder_reserve(context, builder, length);
433
+ if (result.status != OSEO_STATUS_NORMAL) return result;
434
+ memcpy(
435
+ builder->units + builder->length,
436
+ units,
437
+ length * sizeof(uint16_t)
438
+ );
439
+ builder->length += length;
440
+ return result;
441
+ }
442
+
443
+ void oseo_internal_string_builder_release(OseoStringBuilder *builder) {
444
+ free(builder->units);
445
+ builder->units = NULL;
446
+ builder->length = 0u;
447
+ builder->capacity = 0u;
448
+ }
449
+
450
+ typedef struct {
451
+ uint32_t *values;
452
+ size_t length;
453
+ size_t capacity;
454
+ } OseoCodePointBuilder;
455
+
456
+ static OseoValue string_builtin_argument(
457
+ size_t argument_count,
458
+ const OseoValue *arguments,
459
+ size_t index
460
+ );
461
+
462
+ static OseoResult string_method_subject(
463
+ OseoContext *context,
464
+ OseoValue receiver
465
+ );
466
+
467
+ static OseoResult code_point_builder_reserve(
468
+ OseoContext *context,
469
+ OseoCodePointBuilder *builder,
470
+ size_t additional
471
+ ) {
472
+ if (additional > SIZE_MAX - builder->length) {
473
+ return failure(context, "OSEO2001", "String allocation is too large.");
474
+ }
475
+ size_t required = builder->length + additional;
476
+ if (required <= builder->capacity) return normal(oseo_undefined());
477
+ size_t capacity = builder->capacity == 0u ? 16u : builder->capacity;
478
+ while (capacity < required) {
479
+ if (capacity > SIZE_MAX / 2u) {
480
+ capacity = required;
481
+ break;
482
+ }
483
+ capacity *= 2u;
484
+ }
485
+ if (capacity > SIZE_MAX / sizeof(uint32_t)) {
486
+ return failure(context, "OSEO2001", "String allocation is too large.");
487
+ }
488
+ uint32_t *values = realloc(
489
+ builder->values,
490
+ capacity * sizeof(uint32_t)
491
+ );
492
+ if (values == NULL) {
493
+ return failure(context, "OSEO2001", "String allocation failed.");
494
+ }
495
+ builder->values = values;
496
+ builder->capacity = capacity;
497
+ return normal(oseo_undefined());
498
+ }
499
+
500
+ static OseoResult code_point_builder_append(
501
+ OseoContext *context,
502
+ OseoCodePointBuilder *builder,
503
+ uint32_t value
504
+ ) {
505
+ OseoResult result = code_point_builder_reserve(context, builder, 1u);
506
+ if (result.status != OSEO_STATUS_NORMAL) return result;
507
+ builder->values[builder->length] = value;
508
+ builder->length += 1u;
509
+ return result;
510
+ }
511
+
512
+ static void code_point_builder_release(OseoCodePointBuilder *builder) {
513
+ free(builder->values);
514
+ builder->values = NULL;
515
+ builder->length = 0u;
516
+ builder->capacity = 0u;
517
+ }
518
+
519
+ static uint32_t string_next_code_point(
520
+ const OseoString *string,
521
+ size_t *cursor
522
+ ) {
523
+ uint16_t first = string->units[*cursor];
524
+ *cursor += 1u;
525
+ if (first < UINT16_C(0xd800) || first > UINT16_C(0xdbff) ||
526
+ *cursor >= string->length) return first;
527
+ uint16_t second = string->units[*cursor];
528
+ if (second < UINT16_C(0xdc00) || second > UINT16_C(0xdfff)) return first;
529
+ *cursor += 1u;
530
+ return UINT32_C(0x10000) +
531
+ ((uint32_t)(first - UINT16_C(0xd800)) << 10u) +
532
+ (uint32_t)(second - UINT16_C(0xdc00));
533
+ }
534
+
535
+ static OseoResult code_points_from_string(
536
+ OseoContext *context,
537
+ const OseoString *string,
538
+ OseoCodePointBuilder *builder
539
+ ) {
540
+ OseoResult result = code_point_builder_reserve(
541
+ context,
542
+ builder,
543
+ string->length
544
+ );
545
+ if (result.status != OSEO_STATUS_NORMAL) return result;
546
+ size_t cursor = 0u;
547
+ while (cursor < string->length) {
548
+ builder->values[builder->length] = string_next_code_point(
549
+ string,
550
+ &cursor
551
+ );
552
+ builder->length += 1u;
553
+ }
554
+ return result;
555
+ }
556
+
557
+ static OseoResult string_from_code_points(
558
+ OseoContext *context,
559
+ const OseoCodePointBuilder *builder
560
+ ) {
561
+ OseoStringBuilder units = {NULL, 0u, 0u};
562
+ OseoResult result = normal(oseo_undefined());
563
+ for (size_t index = 0u;
564
+ result.status == OSEO_STATUS_NORMAL && index < builder->length;
565
+ index += 1u) {
566
+ uint32_t code_point = builder->values[index];
567
+ if (code_point <= UINT32_C(0xffff)) {
568
+ result = string_builder_append_unit(
569
+ context,
570
+ &units,
571
+ (uint16_t)code_point
572
+ );
573
+ } else {
574
+ uint32_t value = code_point - UINT32_C(0x10000);
575
+ result = string_builder_append_unit(
576
+ context,
577
+ &units,
578
+ (uint16_t)(UINT32_C(0xd800) + (value >> 10u))
579
+ );
580
+ if (result.status == OSEO_STATUS_NORMAL) {
581
+ result = string_builder_append_unit(
582
+ context,
583
+ &units,
584
+ (uint16_t)(UINT32_C(0xdc00) +
585
+ (value & UINT32_C(0x3ff)))
586
+ );
587
+ }
588
+ }
589
+ }
590
+ if (result.status == OSEO_STATUS_NORMAL) {
591
+ result = oseo_internal_allocate_string(
592
+ context,
593
+ units.units,
594
+ units.length
595
+ );
596
+ }
597
+ oseo_internal_string_builder_release(&units);
598
+ return result;
599
+ }
600
+
601
+ static const OseoUnicodeMapping *unicode_mapping_find(
602
+ const OseoUnicodeMapping *mappings,
603
+ size_t count,
604
+ uint32_t code_point
605
+ ) {
606
+ size_t low = 0u;
607
+ size_t high = count;
608
+ while (low < high) {
609
+ size_t middle = low + (high - low) / 2u;
610
+ uint32_t candidate = mappings[middle].code_point;
611
+ if (candidate < code_point) low = middle + 1u;
612
+ else high = middle;
613
+ }
614
+ return low < count && mappings[low].code_point == code_point
615
+ ? &mappings[low]
616
+ : NULL;
617
+ }
618
+
619
+ static uint8_t unicode_combining_class(uint32_t code_point) {
620
+ size_t count = sizeof(oseo_unicode_combining_classes) /
621
+ sizeof(oseo_unicode_combining_classes[0]);
622
+ size_t low = 0u;
623
+ size_t high = count;
624
+ while (low < high) {
625
+ size_t middle = low + (high - low) / 2u;
626
+ if (oseo_unicode_combining_classes[middle].start <= code_point) {
627
+ low = middle + 1u;
628
+ } else {
629
+ high = middle;
630
+ }
631
+ }
632
+ return low == 0u ? 0u : oseo_unicode_combining_classes[low - 1u].value;
633
+ }
634
+
635
+ static bool unicode_set_has(
636
+ const uint32_t *boundaries,
637
+ size_t count,
638
+ uint32_t code_point
639
+ ) {
640
+ size_t low = 0u;
641
+ size_t high = count;
642
+ while (low < high) {
643
+ size_t middle = low + (high - low) / 2u;
644
+ if (boundaries[middle] <= code_point) low = middle + 1u;
645
+ else high = middle;
646
+ }
647
+ return (low & 1u) != 0u;
648
+ }
649
+
650
+ static bool unicode_cased(uint32_t code_point) {
651
+ return unicode_set_has(
652
+ oseo_unicode_cased,
653
+ sizeof(oseo_unicode_cased) / sizeof(oseo_unicode_cased[0]),
654
+ code_point
655
+ );
656
+ }
657
+
658
+ static bool unicode_case_ignorable(uint32_t code_point) {
659
+ return unicode_set_has(
660
+ oseo_unicode_case_ignorable,
661
+ sizeof(oseo_unicode_case_ignorable) /
662
+ sizeof(oseo_unicode_case_ignorable[0]),
663
+ code_point
664
+ );
665
+ }
666
+
667
+ bool oseo_internal_unicode_id_start(uint32_t code_point) {
668
+ return unicode_set_has(
669
+ oseo_unicode_id_start,
670
+ sizeof(oseo_unicode_id_start) / sizeof(oseo_unicode_id_start[0]),
671
+ code_point
672
+ );
673
+ }
674
+
675
+ bool oseo_internal_unicode_id_continue(uint32_t code_point) {
676
+ return unicode_set_has(
677
+ oseo_unicode_id_continue,
678
+ sizeof(oseo_unicode_id_continue) /
679
+ sizeof(oseo_unicode_id_continue[0]),
680
+ code_point
681
+ );
682
+ }
683
+
684
+ static bool unicode_final_sigma(
685
+ const OseoCodePointBuilder *subject,
686
+ size_t index
687
+ ) {
688
+ bool preceded = false;
689
+ size_t cursor = index;
690
+ while (cursor > 0u) {
691
+ cursor -= 1u;
692
+ uint32_t code_point = subject->values[cursor];
693
+ if (unicode_case_ignorable(code_point)) continue;
694
+ preceded = unicode_cased(code_point);
695
+ break;
696
+ }
697
+ if (!preceded) return false;
698
+ for (cursor = index + 1u; cursor < subject->length; cursor += 1u) {
699
+ uint32_t code_point = subject->values[cursor];
700
+ if (unicode_case_ignorable(code_point)) continue;
701
+ return !unicode_cased(code_point);
702
+ }
703
+ return true;
704
+ }
705
+
706
+ static OseoResult string_convert_case(
707
+ OseoContext *context,
708
+ OseoValue receiver,
709
+ bool uppercase
710
+ ) {
711
+ OseoRootFrame frame = {NULL, NULL, 0u};
712
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
713
+ if (result.status != OSEO_STATUS_NORMAL) return result;
714
+ frame.slots[0] = receiver;
715
+ result = string_method_subject(context, frame.slots[0]);
716
+ frame.slots[1] = result.value;
717
+ OseoCodePointBuilder input = {NULL, 0u, 0u};
718
+ OseoCodePointBuilder output = {NULL, 0u, 0u};
719
+ if (result.status == OSEO_STATUS_NORMAL) {
720
+ result = code_points_from_string(
721
+ context,
722
+ string_object(frame.slots[1]),
723
+ &input
724
+ );
725
+ }
726
+ const OseoUnicodeMapping *mappings = uppercase
727
+ ? oseo_unicode_uppercase
728
+ : oseo_unicode_lowercase;
729
+ size_t mapping_count = uppercase
730
+ ? sizeof(oseo_unicode_uppercase) /
731
+ sizeof(oseo_unicode_uppercase[0])
732
+ : sizeof(oseo_unicode_lowercase) /
733
+ sizeof(oseo_unicode_lowercase[0]);
734
+ for (size_t index = 0u;
735
+ result.status == OSEO_STATUS_NORMAL && index < input.length;
736
+ index += 1u) {
737
+ uint32_t code_point = input.values[index];
738
+ if (!uppercase && code_point == UINT32_C(0x3a3) &&
739
+ unicode_final_sigma(&input, index)) {
740
+ result = code_point_builder_append(
741
+ context,
742
+ &output,
743
+ UINT32_C(0x3c2)
744
+ );
745
+ continue;
746
+ }
747
+ const OseoUnicodeMapping *mapping = unicode_mapping_find(
748
+ mappings,
749
+ mapping_count,
750
+ code_point
751
+ );
752
+ if (mapping == NULL) {
753
+ result = code_point_builder_append(context, &output, code_point);
754
+ continue;
755
+ }
756
+ for (size_t item = 0u;
757
+ result.status == OSEO_STATUS_NORMAL && item < mapping->length;
758
+ item += 1u) {
759
+ result = code_point_builder_append(
760
+ context,
761
+ &output,
762
+ oseo_unicode_mapping_values[mapping->offset + item]
763
+ );
764
+ }
765
+ }
766
+ if (result.status == OSEO_STATUS_NORMAL) {
767
+ result = string_from_code_points(context, &output);
768
+ }
769
+ code_point_builder_release(&input);
770
+ code_point_builder_release(&output);
771
+ oseo_roots_release(context, &frame);
772
+ return result;
773
+ }
774
+
775
+ static bool string_trim_code_unit(uint16_t unit) {
776
+ return unit == UINT16_C(0x0009) || unit == UINT16_C(0x000a) ||
777
+ unit == UINT16_C(0x000b) || unit == UINT16_C(0x000c) ||
778
+ unit == UINT16_C(0x000d) || unit == UINT16_C(0x0020) ||
779
+ unit == UINT16_C(0x00a0) || unit == UINT16_C(0x1680) ||
780
+ (unit >= UINT16_C(0x2000) && unit <= UINT16_C(0x200a)) ||
781
+ unit == UINT16_C(0x2028) || unit == UINT16_C(0x2029) ||
782
+ unit == UINT16_C(0x202f) || unit == UINT16_C(0x205f) ||
783
+ unit == UINT16_C(0x3000) || unit == UINT16_C(0xfeff);
784
+ }
785
+
786
+ static OseoResult string_trim(
787
+ OseoContext *context,
788
+ OseoValue receiver,
789
+ bool trim_start,
790
+ bool trim_end
791
+ ) {
792
+ OseoRootFrame frame = {NULL, NULL, 0u};
793
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
794
+ if (result.status != OSEO_STATUS_NORMAL) return result;
795
+ frame.slots[0] = receiver;
796
+ result = string_method_subject(context, frame.slots[0]);
797
+ frame.slots[1] = result.value;
798
+ if (result.status == OSEO_STATUS_NORMAL) {
799
+ OseoString *subject = string_object(frame.slots[1]);
800
+ size_t start = 0u;
801
+ size_t end = subject->length;
802
+ if (trim_start) {
803
+ while (start < end && string_trim_code_unit(
804
+ subject->units[start])) start += 1u;
805
+ }
806
+ if (trim_end) {
807
+ while (end > start && string_trim_code_unit(
808
+ subject->units[end - 1u])) end -= 1u;
809
+ }
810
+ result = oseo_internal_allocate_string(
811
+ context,
812
+ start == 0u ? subject->units : subject->units + start,
813
+ end - start
814
+ );
815
+ }
816
+ oseo_roots_release(context, &frame);
817
+ return result;
818
+ }
819
+
820
+ static OseoResult normalize_append_decomposed(
821
+ OseoContext *context,
822
+ OseoCodePointBuilder *output,
823
+ uint32_t code_point
824
+ ) {
825
+ return code_point_builder_append(context, output, code_point);
826
+ }
827
+
828
+ /*
829
+ * Stably order each non-starter run after decomposition. Counting the fixed
830
+ * canonical-combining-class range keeps one descending run linear instead of
831
+ * shifting its already appended suffix once for every code point.
832
+ */
833
+ static OseoResult normalize_order(
834
+ OseoContext *context,
835
+ OseoCodePointBuilder *output
836
+ ) {
837
+ if (output->length < 2u) return normal(oseo_undefined());
838
+ uint8_t prior = unicode_combining_class(output->values[0]);
839
+ bool requires_ordering = false;
840
+ for (size_t index = 1u; index < output->length; index += 1u) {
841
+ uint8_t combining = unicode_combining_class(output->values[index]);
842
+ if (combining != 0u && prior > combining) {
843
+ requires_ordering = true;
844
+ break;
845
+ }
846
+ prior = combining;
847
+ }
848
+ if (!requires_ordering) return normal(oseo_undefined());
849
+ OseoCodePointBuilder ordered = {NULL, 0u, 0u};
850
+ OseoResult result = code_point_builder_reserve(
851
+ context,
852
+ &ordered,
853
+ output->length
854
+ );
855
+ if (result.status != OSEO_STATUS_NORMAL) return result;
856
+ ordered.length = output->length;
857
+ size_t run_start = 0u;
858
+ while (run_start < output->length) {
859
+ if (unicode_combining_class(output->values[run_start]) == 0u) {
860
+ ordered.values[run_start] = output->values[run_start];
861
+ run_start += 1u;
862
+ continue;
863
+ }
864
+ size_t counts[256] = {0u};
865
+ size_t run_end = run_start;
866
+ while (run_end < output->length) {
867
+ uint8_t combining = unicode_combining_class(
868
+ output->values[run_end]
869
+ );
870
+ if (combining == 0u) break;
871
+ counts[combining] += 1u;
872
+ run_end += 1u;
873
+ }
874
+ size_t offset = run_start;
875
+ for (size_t combining = 1u; combining < 256u; combining += 1u) {
876
+ size_t count = counts[combining];
877
+ counts[combining] = offset;
878
+ offset += count;
879
+ }
880
+ for (size_t index = run_start; index < run_end; index += 1u) {
881
+ uint8_t combining = unicode_combining_class(
882
+ output->values[index]
883
+ );
884
+ ordered.values[counts[combining]] = output->values[index];
885
+ counts[combining] += 1u;
886
+ }
887
+ run_start = run_end;
888
+ }
889
+ memcpy(
890
+ output->values,
891
+ ordered.values,
892
+ output->length * sizeof(*output->values)
893
+ );
894
+ code_point_builder_release(&ordered);
895
+ return normal(oseo_undefined());
896
+ }
897
+
898
+ static OseoResult normalize_decompose(
899
+ OseoContext *context,
900
+ OseoCodePointBuilder *output,
901
+ uint32_t code_point,
902
+ bool compatibility
903
+ ) {
904
+ const uint32_t s_base = UINT32_C(0xac00);
905
+ const uint32_t l_base = UINT32_C(0x1100);
906
+ const uint32_t v_base = UINT32_C(0x1161);
907
+ const uint32_t t_base = UINT32_C(0x11a7);
908
+ const uint32_t l_count = UINT32_C(19);
909
+ const uint32_t v_count = UINT32_C(21);
910
+ const uint32_t t_count = UINT32_C(28);
911
+ const uint32_t n_count = v_count * t_count;
912
+ const uint32_t s_count = l_count * n_count;
913
+ if (code_point >= s_base && code_point < s_base + s_count) {
914
+ uint32_t index = code_point - s_base;
915
+ OseoResult result = normalize_append_decomposed(
916
+ context,
917
+ output,
918
+ l_base + index / n_count
919
+ );
920
+ if (result.status == OSEO_STATUS_NORMAL) {
921
+ result = normalize_append_decomposed(
922
+ context,
923
+ output,
924
+ v_base + (index % n_count) / t_count
925
+ );
926
+ }
927
+ if (result.status == OSEO_STATUS_NORMAL && index % t_count != 0u) {
928
+ result = normalize_append_decomposed(
929
+ context,
930
+ output,
931
+ t_base + index % t_count
932
+ );
933
+ }
934
+ return result;
935
+ }
936
+ const OseoUnicodeMapping *mapping = NULL;
937
+ if (compatibility) {
938
+ mapping = unicode_mapping_find(
939
+ oseo_unicode_compat_decomposition,
940
+ sizeof(oseo_unicode_compat_decomposition) /
941
+ sizeof(oseo_unicode_compat_decomposition[0]),
942
+ code_point
943
+ );
944
+ }
945
+ if (mapping == NULL) {
946
+ mapping = unicode_mapping_find(
947
+ oseo_unicode_canonical_decomposition,
948
+ sizeof(oseo_unicode_canonical_decomposition) /
949
+ sizeof(oseo_unicode_canonical_decomposition[0]),
950
+ code_point
951
+ );
952
+ }
953
+ if (mapping == NULL) {
954
+ return normalize_append_decomposed(context, output, code_point);
955
+ }
956
+ OseoResult result = normal(oseo_undefined());
957
+ for (size_t index = 0u;
958
+ result.status == OSEO_STATUS_NORMAL && index < mapping->length;
959
+ index += 1u) {
960
+ result = normalize_decompose(
961
+ context,
962
+ output,
963
+ oseo_unicode_mapping_values[mapping->offset + index],
964
+ compatibility
965
+ );
966
+ }
967
+ return result;
968
+ }
969
+
970
+ static uint32_t normalize_compose_pair(uint32_t first, uint32_t second) {
971
+ const uint32_t s_base = UINT32_C(0xac00);
972
+ const uint32_t l_base = UINT32_C(0x1100);
973
+ const uint32_t v_base = UINT32_C(0x1161);
974
+ const uint32_t t_base = UINT32_C(0x11a7);
975
+ const uint32_t l_count = UINT32_C(19);
976
+ const uint32_t v_count = UINT32_C(21);
977
+ const uint32_t t_count = UINT32_C(28);
978
+ const uint32_t n_count = v_count * t_count;
979
+ const uint32_t s_count = l_count * n_count;
980
+ if (first >= l_base && first < l_base + l_count &&
981
+ second >= v_base && second < v_base + v_count) {
982
+ return s_base + ((first - l_base) * v_count +
983
+ (second - v_base)) * t_count;
984
+ }
985
+ if (first >= s_base && first < s_base + s_count &&
986
+ (first - s_base) % t_count == 0u && second > t_base &&
987
+ second < t_base + t_count) {
988
+ return first + second - t_base;
989
+ }
990
+ size_t count = sizeof(oseo_unicode_compositions) /
991
+ sizeof(oseo_unicode_compositions[0]);
992
+ size_t low = 0u;
993
+ size_t high = count;
994
+ while (low < high) {
995
+ size_t middle = low + (high - low) / 2u;
996
+ OseoUnicodeComposition candidate = oseo_unicode_compositions[middle];
997
+ if (candidate.first < first ||
998
+ (candidate.first == first && candidate.second < second)) {
999
+ low = middle + 1u;
1000
+ } else {
1001
+ high = middle;
1002
+ }
1003
+ }
1004
+ if (low < count && oseo_unicode_compositions[low].first == first &&
1005
+ oseo_unicode_compositions[low].second == second) {
1006
+ return oseo_unicode_compositions[low].composite;
1007
+ }
1008
+ return 0u;
1009
+ }
1010
+
1011
+ static void normalize_compose(OseoCodePointBuilder *output) {
1012
+ if (output->length < 2u) return;
1013
+ size_t result_length = 1u;
1014
+ size_t starter_index = 0u;
1015
+ uint32_t starter = output->values[0];
1016
+ uint8_t prior_combining = unicode_combining_class(starter);
1017
+ for (size_t index = 1u; index < output->length; index += 1u) {
1018
+ uint32_t code_point = output->values[index];
1019
+ uint8_t combining = unicode_combining_class(code_point);
1020
+ uint32_t composite = 0u;
1021
+ if (prior_combining < combining || prior_combining == 0u) {
1022
+ composite = normalize_compose_pair(starter, code_point);
1023
+ }
1024
+ if (composite != 0u) {
1025
+ output->values[starter_index] = composite;
1026
+ starter = composite;
1027
+ continue;
1028
+ }
1029
+ output->values[result_length] = code_point;
1030
+ if (combining == 0u) {
1031
+ starter_index = result_length;
1032
+ starter = code_point;
1033
+ }
1034
+ result_length += 1u;
1035
+ prior_combining = combining;
1036
+ }
1037
+ output->length = result_length;
1038
+ }
1039
+
1040
+ static OseoResult normalize_subject(
1041
+ OseoContext *context,
1042
+ OseoValue subject,
1043
+ bool compatibility,
1044
+ bool compose
1045
+ ) {
1046
+ OseoCodePointBuilder input = {NULL, 0u, 0u};
1047
+ OseoCodePointBuilder output = {NULL, 0u, 0u};
1048
+ OseoResult result = code_points_from_string(
1049
+ context,
1050
+ string_object(subject),
1051
+ &input
1052
+ );
1053
+ for (size_t index = 0u;
1054
+ result.status == OSEO_STATUS_NORMAL && index < input.length;
1055
+ index += 1u) {
1056
+ result = normalize_decompose(
1057
+ context,
1058
+ &output,
1059
+ input.values[index],
1060
+ compatibility
1061
+ );
1062
+ }
1063
+ if (result.status == OSEO_STATUS_NORMAL) {
1064
+ result = normalize_order(context, &output);
1065
+ }
1066
+ if (result.status == OSEO_STATUS_NORMAL && compose) {
1067
+ normalize_compose(&output);
1068
+ }
1069
+ if (result.status == OSEO_STATUS_NORMAL) {
1070
+ result = string_from_code_points(context, &output);
1071
+ }
1072
+ code_point_builder_release(&input);
1073
+ code_point_builder_release(&output);
1074
+ return result;
1075
+ }
1076
+
1077
+ static OseoResult string_normalize(
1078
+ OseoContext *context,
1079
+ OseoValue receiver,
1080
+ size_t argument_count,
1081
+ const OseoValue *arguments
1082
+ ) {
1083
+ OseoRootFrame frame = {NULL, NULL, 0u};
1084
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1085
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1086
+ frame.slots[0] = receiver;
1087
+ frame.slots[1] = string_builtin_argument(argument_count, arguments, 0u);
1088
+ result = string_method_subject(context, frame.slots[0]);
1089
+ frame.slots[2] = result.value;
1090
+ bool compatibility = false;
1091
+ bool compose = true;
1092
+ if (result.status == OSEO_STATUS_NORMAL &&
1093
+ tag_of(frame.slots[1]) != OSEO_TAG_UNDEFINED) {
1094
+ result = oseo_internal_value_string(context, frame.slots[1]);
1095
+ frame.slots[1] = result.value;
1096
+ if (result.status == OSEO_STATUS_NORMAL) {
1097
+ if (oseo_internal_string_is_ascii(frame.slots[1], "NFC")) {
1098
+ compatibility = false;
1099
+ compose = true;
1100
+ } else if (oseo_internal_string_is_ascii(
1101
+ frame.slots[1], "NFD")) {
1102
+ compatibility = false;
1103
+ compose = false;
1104
+ } else if (oseo_internal_string_is_ascii(
1105
+ frame.slots[1], "NFKC")) {
1106
+ compatibility = true;
1107
+ compose = true;
1108
+ } else if (oseo_internal_string_is_ascii(
1109
+ frame.slots[1], "NFKD")) {
1110
+ compatibility = true;
1111
+ compose = false;
1112
+ } else {
1113
+ result = oseo_internal_throw_error(
1114
+ context,
1115
+ OSEO_ERROR_RANGE,
1116
+ "Invalid Unicode normalization form."
1117
+ );
1118
+ }
1119
+ }
1120
+ }
1121
+ if (result.status == OSEO_STATUS_NORMAL) {
1122
+ result = normalize_subject(
1123
+ context,
1124
+ frame.slots[2],
1125
+ compatibility,
1126
+ compose
1127
+ );
1128
+ }
1129
+ oseo_roots_release(context, &frame);
1130
+ return result;
1131
+ }
1132
+
1133
+ static OseoResult string_locale_compare(
1134
+ OseoContext *context,
1135
+ OseoValue receiver,
1136
+ size_t argument_count,
1137
+ const OseoValue *arguments
1138
+ ) {
1139
+ OseoRootFrame frame = {NULL, NULL, 0u};
1140
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
1141
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1142
+ frame.slots[0] = receiver;
1143
+ frame.slots[1] = string_builtin_argument(argument_count, arguments, 0u);
1144
+ result = string_method_subject(context, frame.slots[0]);
1145
+ frame.slots[2] = result.value;
1146
+ if (result.status == OSEO_STATUS_NORMAL) {
1147
+ result = oseo_internal_value_string(context, frame.slots[1]);
1148
+ frame.slots[3] = result.value;
1149
+ }
1150
+ if (result.status == OSEO_STATUS_NORMAL) {
1151
+ result = normalize_subject(context, frame.slots[2], false, false);
1152
+ frame.slots[2] = result.value;
1153
+ }
1154
+ if (result.status == OSEO_STATUS_NORMAL) {
1155
+ result = normalize_subject(context, frame.slots[3], false, false);
1156
+ frame.slots[3] = result.value;
1157
+ }
1158
+ if (result.status == OSEO_STATUS_NORMAL) {
1159
+ const OseoString *left = string_object(frame.slots[2]);
1160
+ const OseoString *right = string_object(frame.slots[3]);
1161
+ size_t common = left->length < right->length
1162
+ ? left->length
1163
+ : right->length;
1164
+ int comparison = 0;
1165
+ for (size_t index = 0u; index < common; index += 1u) {
1166
+ if (left->units[index] < right->units[index]) {
1167
+ comparison = -1;
1168
+ break;
1169
+ }
1170
+ if (left->units[index] > right->units[index]) {
1171
+ comparison = 1;
1172
+ break;
1173
+ }
1174
+ }
1175
+ double order = comparison < 0 ||
1176
+ (comparison == 0 && left->length < right->length)
1177
+ ? -1.0
1178
+ : comparison > 0 ||
1179
+ (comparison == 0 && left->length > right->length)
1180
+ ? 1.0
1181
+ : 0.0;
1182
+ result = normal(oseo_number(order));
1183
+ }
1184
+ oseo_roots_release(context, &frame);
1185
+ return result;
1186
+ }
1187
+
1188
+ /* ToUint16 (7.1.7) over an already converted Number. */
1189
+ static uint16_t string_to_uint16(double value) {
1190
+ if (!isfinite(value) || value == 0.0) return 0u;
1191
+ double modulo = fmod(trunc(value), 65536.0);
1192
+ if (modulo < 0.0) modulo += 65536.0;
1193
+ return (uint16_t)modulo;
1194
+ }
1195
+
1196
+ static OseoValue string_builtin_argument(
1197
+ size_t argument_count,
1198
+ const OseoValue *arguments,
1199
+ size_t index
1200
+ ) {
1201
+ return index < argument_count ? arguments[index] : oseo_undefined();
1202
+ }
1203
+
1204
+ /* ToIntegerOrInfinity over one already rooted input value. */
1205
+ static OseoResult string_integer_or_infinity(
1206
+ OseoContext *context,
1207
+ OseoValue value
1208
+ ) {
1209
+ OseoResult result = oseo_internal_to_number(context, value);
1210
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1211
+ double number = number_value(result.value);
1212
+ if (isnan(number) || number == 0.0) number = 0.0;
1213
+ else if (isfinite(number)) number = trunc(number);
1214
+ return normal(oseo_number(number));
1215
+ }
1216
+
1217
+ static OseoResult string_method_subject(
1218
+ OseoContext *context,
1219
+ OseoValue receiver
1220
+ ) {
1221
+ if (is_nullish(receiver)) {
1222
+ return oseo_internal_throw_error(
1223
+ context,
1224
+ OSEO_ERROR_TYPE,
1225
+ "String method receiver is nullish."
1226
+ );
1227
+ }
1228
+ return oseo_internal_value_string(context, receiver);
1229
+ }
1230
+
1231
+ static OseoResult string_repeat(
1232
+ OseoContext *context,
1233
+ OseoValue receiver,
1234
+ size_t argument_count,
1235
+ const OseoValue *arguments
1236
+ ) {
1237
+ OseoValue slots[3] = {
1238
+ receiver,
1239
+ string_builtin_argument(argument_count, arguments, 0u),
1240
+ oseo_undefined(),
1241
+ };
1242
+ OseoRootFrame frame = {NULL, slots, 3u};
1243
+ oseo_roots_push(context, &frame);
1244
+ OseoResult result = string_method_subject(context, slots[0]);
1245
+ slots[2] = result.value;
1246
+ if (result.status == OSEO_STATUS_NORMAL) {
1247
+ result = string_integer_or_infinity(context, slots[1]);
1248
+ }
1249
+ if (result.status != OSEO_STATUS_NORMAL) {
1250
+ oseo_roots_pop(context, &frame);
1251
+ return result;
1252
+ }
1253
+ double count_number = number_value(result.value);
1254
+ if (count_number < 0.0 || count_number == INFINITY) {
1255
+ result = oseo_internal_throw_error(
1256
+ context,
1257
+ OSEO_ERROR_RANGE,
1258
+ "Invalid repeat count."
1259
+ );
1260
+ oseo_roots_pop(context, &frame);
1261
+ return result;
1262
+ }
1263
+ const OseoString *subject = string_object(slots[2]);
1264
+ if (subject->length == 0u || count_number == 0.0) {
1265
+ result = oseo_internal_allocate_string(context, NULL, 0u);
1266
+ oseo_roots_pop(context, &frame);
1267
+ return result;
1268
+ }
1269
+ if (count_number > (double)(OSEO_MAX_STRING_LENGTH / subject->length)) {
1270
+ result = oseo_internal_throw_error(
1271
+ context,
1272
+ OSEO_ERROR_RANGE,
1273
+ "Invalid string length."
1274
+ );
1275
+ oseo_roots_pop(context, &frame);
1276
+ return result;
1277
+ }
1278
+ size_t count = (size_t)count_number;
1279
+ size_t length = subject->length * count;
1280
+ uint16_t *units = malloc(length * sizeof(uint16_t));
1281
+ if (units == NULL) {
1282
+ result = failure(context, "OSEO2001", "String allocation failed.");
1283
+ oseo_roots_pop(context, &frame);
1284
+ return result;
1285
+ }
1286
+ memcpy(units, subject->units, subject->length * sizeof(uint16_t));
1287
+ size_t copied = subject->length;
1288
+ while (copied < length) {
1289
+ size_t chunk = copied < length - copied ? copied : length - copied;
1290
+ memcpy(units + copied, units, chunk * sizeof(uint16_t));
1291
+ copied += chunk;
1292
+ }
1293
+ result = oseo_internal_allocate_string(context, units, length);
1294
+ free(units);
1295
+ oseo_roots_pop(context, &frame);
1296
+ return result;
1297
+ }
1298
+
1299
+ static OseoResult string_pad(
1300
+ OseoContext *context,
1301
+ OseoValue receiver,
1302
+ size_t argument_count,
1303
+ const OseoValue *arguments,
1304
+ bool at_start
1305
+ ) {
1306
+ OseoValue slots[5] = {
1307
+ receiver,
1308
+ string_builtin_argument(argument_count, arguments, 0u),
1309
+ string_builtin_argument(argument_count, arguments, 1u),
1310
+ oseo_undefined(),
1311
+ oseo_undefined(),
1312
+ };
1313
+ OseoRootFrame frame = {NULL, slots, 5u};
1314
+ oseo_roots_push(context, &frame);
1315
+ OseoResult result = string_method_subject(context, slots[0]);
1316
+ slots[3] = result.value;
1317
+ if (result.status == OSEO_STATUS_NORMAL) {
1318
+ result = string_integer_or_infinity(context, slots[1]);
1319
+ }
1320
+ double target_number = 0.0;
1321
+ if (result.status == OSEO_STATUS_NORMAL) {
1322
+ target_number = number_value(result.value);
1323
+ if (!(target_number > 0.0)) target_number = 0.0;
1324
+ else if (target_number == INFINITY ||
1325
+ target_number > 9007199254740991.0) {
1326
+ target_number = 9007199254740991.0;
1327
+ }
1328
+ }
1329
+ if (result.status != OSEO_STATUS_NORMAL) {
1330
+ oseo_roots_pop(context, &frame);
1331
+ return result;
1332
+ }
1333
+ const OseoString *subject = string_object(slots[3]);
1334
+ if (target_number <= (double)subject->length) {
1335
+ result = normal(slots[3]);
1336
+ oseo_roots_pop(context, &frame);
1337
+ return result;
1338
+ }
1339
+ if (tag_of(slots[2]) == OSEO_TAG_UNDEFINED) {
1340
+ result = oseo_internal_ascii_string(context, " ");
1341
+ slots[4] = result.value;
1342
+ } else {
1343
+ result = oseo_internal_value_string(context, slots[2]);
1344
+ slots[4] = result.value;
1345
+ }
1346
+ if (result.status != OSEO_STATUS_NORMAL) {
1347
+ oseo_roots_pop(context, &frame);
1348
+ return result;
1349
+ }
1350
+ const OseoString *filler = string_object(slots[4]);
1351
+ if (filler->length == 0u) {
1352
+ result = normal(slots[3]);
1353
+ oseo_roots_pop(context, &frame);
1354
+ return result;
1355
+ }
1356
+ if (target_number > (double)OSEO_MAX_STRING_LENGTH) {
1357
+ result = oseo_internal_throw_error(
1358
+ context,
1359
+ OSEO_ERROR_RANGE,
1360
+ "Invalid string length."
1361
+ );
1362
+ oseo_roots_pop(context, &frame);
1363
+ return result;
1364
+ }
1365
+ size_t target = (size_t)target_number;
1366
+ size_t fill_length = target - subject->length;
1367
+ uint16_t *units = malloc(target * sizeof(uint16_t));
1368
+ if (units == NULL) {
1369
+ result = failure(context, "OSEO2001", "String allocation failed.");
1370
+ oseo_roots_pop(context, &frame);
1371
+ return result;
1372
+ }
1373
+ size_t fill_offset = at_start ? 0u : subject->length;
1374
+ for (size_t index = 0u; index < fill_length; index += 1u) {
1375
+ units[fill_offset + index] = filler->units[index % filler->length];
1376
+ }
1377
+ size_t subject_offset = at_start ? fill_length : 0u;
1378
+ memcpy(
1379
+ units + subject_offset,
1380
+ subject->units,
1381
+ subject->length * sizeof(uint16_t)
1382
+ );
1383
+ result = oseo_internal_allocate_string(context, units, target);
1384
+ free(units);
1385
+ oseo_roots_pop(context, &frame);
1386
+ return result;
1387
+ }
1388
+
1389
+ static bool string_is_leading_surrogate(uint16_t unit) {
1390
+ return unit >= UINT16_C(0xd800) && unit <= UINT16_C(0xdbff);
1391
+ }
1392
+
1393
+ static bool string_is_trailing_surrogate(uint16_t unit) {
1394
+ return unit >= UINT16_C(0xdc00) && unit <= UINT16_C(0xdfff);
1395
+ }
1396
+
1397
+ static bool string_is_well_formed_value(const OseoString *subject) {
1398
+ for (size_t index = 0u; index < subject->length; index += 1u) {
1399
+ uint16_t unit = subject->units[index];
1400
+ if (string_is_leading_surrogate(unit)) {
1401
+ if (index + 1u >= subject->length ||
1402
+ !string_is_trailing_surrogate(subject->units[index + 1u])) {
1403
+ return false;
1404
+ }
1405
+ index += 1u;
1406
+ } else if (string_is_trailing_surrogate(unit)) {
1407
+ return false;
1408
+ }
1409
+ }
1410
+ return true;
1411
+ }
1412
+
1413
+ static OseoResult string_well_formed(
1414
+ OseoContext *context,
1415
+ OseoValue receiver,
1416
+ bool repair
1417
+ ) {
1418
+ OseoValue slots[2] = {receiver, oseo_undefined()};
1419
+ OseoRootFrame frame = {NULL, slots, 2u};
1420
+ oseo_roots_push(context, &frame);
1421
+ OseoResult result = string_method_subject(context, slots[0]);
1422
+ slots[1] = result.value;
1423
+ if (result.status != OSEO_STATUS_NORMAL) {
1424
+ oseo_roots_pop(context, &frame);
1425
+ return result;
1426
+ }
1427
+ const OseoString *subject = string_object(slots[1]);
1428
+ bool well_formed = string_is_well_formed_value(subject);
1429
+ if (!repair) {
1430
+ result = normal(oseo_boolean(well_formed));
1431
+ oseo_roots_pop(context, &frame);
1432
+ return result;
1433
+ }
1434
+ if (well_formed) {
1435
+ result = normal(slots[1]);
1436
+ oseo_roots_pop(context, &frame);
1437
+ return result;
1438
+ }
1439
+ uint16_t *units = malloc(subject->length * sizeof(uint16_t));
1440
+ if (units == NULL) {
1441
+ result = failure(context, "OSEO2001", "String allocation failed.");
1442
+ oseo_roots_pop(context, &frame);
1443
+ return result;
1444
+ }
1445
+ for (size_t index = 0u; index < subject->length; index += 1u) {
1446
+ uint16_t unit = subject->units[index];
1447
+ bool paired_lead = string_is_leading_surrogate(unit) &&
1448
+ index + 1u < subject->length &&
1449
+ string_is_trailing_surrogate(subject->units[index + 1u]);
1450
+ units[index] = string_is_trailing_surrogate(unit) ||
1451
+ (string_is_leading_surrogate(unit) && !paired_lead)
1452
+ ? UINT16_C(0xfffd)
1453
+ : unit;
1454
+ if (paired_lead) {
1455
+ index += 1u;
1456
+ units[index] = subject->units[index];
1457
+ }
1458
+ }
1459
+ result = oseo_internal_allocate_string(
1460
+ context,
1461
+ units,
1462
+ subject->length
1463
+ );
1464
+ free(units);
1465
+ oseo_roots_pop(context, &frame);
1466
+ return result;
1467
+ }
1468
+
1469
+ static OseoResult string_at_index(
1470
+ OseoContext *context,
1471
+ size_t code_id,
1472
+ OseoValue receiver,
1473
+ size_t argument_count,
1474
+ const OseoValue *arguments
1475
+ ) {
1476
+ OseoRootFrame frame = {NULL, NULL, 0u};
1477
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
1478
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1479
+ frame.slots[0] = receiver;
1480
+ frame.slots[1] = string_builtin_argument(
1481
+ argument_count,
1482
+ arguments,
1483
+ 0u
1484
+ );
1485
+ result = string_method_subject(context, frame.slots[0]);
1486
+ frame.slots[2] = result.value;
1487
+ if (result.status == OSEO_STATUS_NORMAL) {
1488
+ result = string_integer_or_infinity(context, frame.slots[1]);
1489
+ }
1490
+ if (result.status != OSEO_STATUS_NORMAL) {
1491
+ oseo_roots_release(context, &frame);
1492
+ return result;
1493
+ }
1494
+
1495
+ double position = number_value(result.value);
1496
+ size_t length = string_object(frame.slots[2])->length;
1497
+ if (code_id == OSEO_STRING_AT_CODE_ID && position < 0.0) {
1498
+ position += (double)length;
1499
+ }
1500
+ if (position < 0.0 || !(position < (double)length)) {
1501
+ if (code_id == OSEO_STRING_CHAR_AT_CODE_ID) {
1502
+ result = oseo_internal_allocate_string(context, NULL, 0u);
1503
+ } else if (code_id == OSEO_STRING_CHAR_CODE_AT_CODE_ID) {
1504
+ result = normal(oseo_number(NAN));
1505
+ } else {
1506
+ result = normal(oseo_undefined());
1507
+ }
1508
+ oseo_roots_release(context, &frame);
1509
+ return result;
1510
+ }
1511
+
1512
+ size_t index = (size_t)position;
1513
+ uint16_t first = string_object(frame.slots[2])->units[index];
1514
+ if (code_id == OSEO_STRING_CHAR_CODE_AT_CODE_ID) {
1515
+ result = normal(oseo_number((double)first));
1516
+ } else if (code_id == OSEO_STRING_CODE_POINT_AT_CODE_ID &&
1517
+ first >= UINT16_C(0xd800) &&
1518
+ first <= UINT16_C(0xdbff) && index + 1u < length) {
1519
+ uint16_t second = string_object(frame.slots[2])->units[index + 1u];
1520
+ if (second >= UINT16_C(0xdc00) && second <= UINT16_C(0xdfff)) {
1521
+ uint32_t code_point = UINT32_C(0x10000) +
1522
+ ((uint32_t)(first - UINT16_C(0xd800)) << 10u) +
1523
+ (uint32_t)(second - UINT16_C(0xdc00));
1524
+ result = normal(oseo_number((double)code_point));
1525
+ } else {
1526
+ result = normal(oseo_number((double)first));
1527
+ }
1528
+ } else if (code_id == OSEO_STRING_CODE_POINT_AT_CODE_ID) {
1529
+ result = normal(oseo_number((double)first));
1530
+ } else {
1531
+ result = oseo_internal_allocate_string(context, &first, 1u);
1532
+ }
1533
+ oseo_roots_release(context, &frame);
1534
+ return result;
1535
+ }
1536
+
1537
+ static OseoResult string_this_value(
1538
+ OseoContext *context,
1539
+ OseoValue receiver
1540
+ ) {
1541
+ if (is_string(receiver)) return normal(receiver);
1542
+ if (oseo_internal_string_data(receiver)) {
1543
+ return normal(ordinary_object(receiver)->primitive_value);
1544
+ }
1545
+ return oseo_internal_throw_error(
1546
+ context,
1547
+ OSEO_ERROR_TYPE,
1548
+ "String.prototype method requires a String receiver."
1549
+ );
1550
+ }
1551
+
1552
+ static bool string_matches_at(
1553
+ const OseoString *subject,
1554
+ const OseoString *search,
1555
+ size_t position
1556
+ ) {
1557
+ if (position > subject->length ||
1558
+ search->length > subject->length - position) return false;
1559
+ return search->length == 0u ||
1560
+ memcmp(
1561
+ subject->units + position,
1562
+ search->units,
1563
+ search->length * sizeof(uint16_t)
1564
+ ) == 0;
1565
+ }
1566
+
1567
+ static size_t string_clamped_position(double position, size_t length) {
1568
+ if (!(position > 0.0)) return 0u;
1569
+ if (!(position < (double)length)) return length;
1570
+ return (size_t)position;
1571
+ }
1572
+
1573
+ static size_t string_relative_position(double position, size_t length) {
1574
+ if (position == -INFINITY) return 0u;
1575
+ if (position < 0.0) {
1576
+ double relative = (double)length + position;
1577
+ return relative > 0.0 ? (size_t)relative : 0u;
1578
+ }
1579
+ return string_clamped_position(position, length);
1580
+ }
1581
+
1582
+ static OseoResult string_is_regexp(
1583
+ OseoContext *context,
1584
+ OseoValue value,
1585
+ bool *regexp
1586
+ ) {
1587
+ return oseo_internal_is_regexp(context, value, regexp);
1588
+ }
1589
+
1590
+ static OseoResult string_concat(
1591
+ OseoContext *context,
1592
+ OseoValue receiver,
1593
+ size_t argument_count,
1594
+ const OseoValue *arguments
1595
+ ) {
1596
+ OseoStringBuilder builder = {NULL, 0u, 0u};
1597
+ OseoRootFrame frame = {NULL, NULL, 0u};
1598
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
1599
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1600
+ frame.slots[0] = receiver;
1601
+ result = string_method_subject(context, frame.slots[0]);
1602
+ frame.slots[1] = result.value;
1603
+ if (result.status == OSEO_STATUS_NORMAL) {
1604
+ result = oseo_internal_string_builder_append(
1605
+ context,
1606
+ &builder,
1607
+ string_object(frame.slots[1])->units,
1608
+ string_object(frame.slots[1])->length
1609
+ );
1610
+ }
1611
+ for (size_t index = 0u;
1612
+ result.status == OSEO_STATUS_NORMAL && index < argument_count;
1613
+ index += 1u) {
1614
+ result = oseo_internal_value_string(context, arguments[index]);
1615
+ frame.slots[1] = result.value;
1616
+ if (result.status == OSEO_STATUS_NORMAL) {
1617
+ result = oseo_internal_string_builder_append(
1618
+ context,
1619
+ &builder,
1620
+ string_object(frame.slots[1])->units,
1621
+ string_object(frame.slots[1])->length
1622
+ );
1623
+ }
1624
+ }
1625
+ if (result.status == OSEO_STATUS_NORMAL) {
1626
+ result = oseo_internal_allocate_string(
1627
+ context,
1628
+ builder.units,
1629
+ builder.length
1630
+ );
1631
+ }
1632
+ oseo_internal_string_builder_release(&builder);
1633
+ oseo_roots_release(context, &frame);
1634
+ return result;
1635
+ }
1636
+
1637
+ static OseoResult string_search(
1638
+ OseoContext *context,
1639
+ size_t code_id,
1640
+ OseoValue receiver,
1641
+ size_t argument_count,
1642
+ const OseoValue *arguments
1643
+ ) {
1644
+ OseoRootFrame frame = {NULL, NULL, 0u};
1645
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
1646
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1647
+ frame.slots[0] = receiver;
1648
+ frame.slots[1] = string_builtin_argument(argument_count, arguments, 0u);
1649
+ frame.slots[2] = string_builtin_argument(argument_count, arguments, 1u);
1650
+ result = string_method_subject(context, frame.slots[0]);
1651
+ frame.slots[3] = result.value;
1652
+
1653
+ bool rejects_regexp = code_id == OSEO_STRING_INCLUDES_CODE_ID ||
1654
+ code_id == OSEO_STRING_STARTS_WITH_CODE_ID ||
1655
+ code_id == OSEO_STRING_ENDS_WITH_CODE_ID;
1656
+ bool regexp = false;
1657
+ if (result.status == OSEO_STATUS_NORMAL && rejects_regexp) {
1658
+ result = string_is_regexp(context, frame.slots[1], &regexp);
1659
+ }
1660
+ if (result.status == OSEO_STATUS_NORMAL && regexp) {
1661
+ result = oseo_internal_throw_error(
1662
+ context,
1663
+ OSEO_ERROR_TYPE,
1664
+ "String search argument must not be a RegExp."
1665
+ );
1666
+ }
1667
+ if (result.status == OSEO_STATUS_NORMAL) {
1668
+ result = oseo_internal_value_string(context, frame.slots[1]);
1669
+ frame.slots[4] = result.value;
1670
+ }
1671
+
1672
+ double position = 0.0;
1673
+ if (result.status == OSEO_STATUS_NORMAL &&
1674
+ code_id == OSEO_STRING_LAST_INDEX_OF_CODE_ID) {
1675
+ result = oseo_internal_to_number(context, frame.slots[2]);
1676
+ if (result.status == OSEO_STATUS_NORMAL) {
1677
+ position = number_value(result.value);
1678
+ if (isnan(position)) position = INFINITY;
1679
+ else if (isfinite(position)) position = trunc(position);
1680
+ }
1681
+ } else if (result.status == OSEO_STATUS_NORMAL &&
1682
+ code_id == OSEO_STRING_ENDS_WITH_CODE_ID &&
1683
+ tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED) {
1684
+ position = (double)string_object(frame.slots[3])->length;
1685
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1686
+ result = string_integer_or_infinity(context, frame.slots[2]);
1687
+ if (result.status == OSEO_STATUS_NORMAL) {
1688
+ position = number_value(result.value);
1689
+ }
1690
+ }
1691
+ if (result.status != OSEO_STATUS_NORMAL) {
1692
+ oseo_roots_release(context, &frame);
1693
+ return result;
1694
+ }
1695
+
1696
+ const OseoString *subject = string_object(frame.slots[3]);
1697
+ const OseoString *search = string_object(frame.slots[4]);
1698
+ size_t start = string_clamped_position(position, subject->length);
1699
+ if (code_id == OSEO_STRING_STARTS_WITH_CODE_ID) {
1700
+ result = normal(oseo_boolean(string_matches_at(
1701
+ subject,
1702
+ search,
1703
+ start
1704
+ )));
1705
+ } else if (code_id == OSEO_STRING_ENDS_WITH_CODE_ID) {
1706
+ bool matches = search->length <= start &&
1707
+ string_matches_at(subject, search, start - search->length);
1708
+ result = normal(oseo_boolean(matches));
1709
+ } else if (code_id == OSEO_STRING_INDEX_OF_CODE_ID ||
1710
+ code_id == OSEO_STRING_INCLUDES_CODE_ID) {
1711
+ size_t found = SIZE_MAX;
1712
+ if (search->length <= subject->length) {
1713
+ size_t limit = subject->length - search->length;
1714
+ for (size_t index = start; index <= limit; index += 1u) {
1715
+ if (string_matches_at(subject, search, index)) {
1716
+ found = index;
1717
+ break;
1718
+ }
1719
+ }
1720
+ }
1721
+ result = code_id == OSEO_STRING_INCLUDES_CODE_ID
1722
+ ? normal(oseo_boolean(found != SIZE_MAX))
1723
+ : normal(oseo_number(
1724
+ found == SIZE_MAX ? -1.0 : (double)found));
1725
+ } else {
1726
+ size_t found = SIZE_MAX;
1727
+ if (search->length <= subject->length) {
1728
+ size_t latest = subject->length - search->length;
1729
+ if (start < latest) latest = start;
1730
+ for (size_t index = latest;; index -= 1u) {
1731
+ if (string_matches_at(subject, search, index)) {
1732
+ found = index;
1733
+ break;
1734
+ }
1735
+ if (index == 0u) break;
1736
+ }
1737
+ }
1738
+ result = normal(oseo_number(
1739
+ found == SIZE_MAX ? -1.0 : (double)found));
1740
+ }
1741
+ oseo_roots_release(context, &frame);
1742
+ return result;
1743
+ }
1744
+
1745
+ static OseoResult string_slice(
1746
+ OseoContext *context,
1747
+ size_t code_id,
1748
+ OseoValue receiver,
1749
+ size_t argument_count,
1750
+ const OseoValue *arguments
1751
+ ) {
1752
+ OseoRootFrame frame = {NULL, NULL, 0u};
1753
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
1754
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1755
+ frame.slots[0] = receiver;
1756
+ frame.slots[1] = string_builtin_argument(argument_count, arguments, 0u);
1757
+ frame.slots[2] = string_builtin_argument(argument_count, arguments, 1u);
1758
+ result = string_method_subject(context, frame.slots[0]);
1759
+ frame.slots[3] = result.value;
1760
+ if (result.status == OSEO_STATUS_NORMAL) {
1761
+ result = string_integer_or_infinity(context, frame.slots[1]);
1762
+ }
1763
+ double start = result.status == OSEO_STATUS_NORMAL
1764
+ ? number_value(result.value)
1765
+ : 0.0;
1766
+ double end = 0.0;
1767
+ if (result.status == OSEO_STATUS_NORMAL &&
1768
+ tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED) {
1769
+ end = (double)string_object(frame.slots[3])->length;
1770
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1771
+ result = string_integer_or_infinity(context, frame.slots[2]);
1772
+ if (result.status == OSEO_STATUS_NORMAL) {
1773
+ end = number_value(result.value);
1774
+ }
1775
+ }
1776
+ if (result.status != OSEO_STATUS_NORMAL) {
1777
+ oseo_roots_release(context, &frame);
1778
+ return result;
1779
+ }
1780
+
1781
+ const OseoString *subject = string_object(frame.slots[3]);
1782
+ size_t from;
1783
+ size_t to;
1784
+ if (code_id == OSEO_STRING_SLICE_CODE_ID) {
1785
+ from = string_relative_position(start, subject->length);
1786
+ to = string_relative_position(end, subject->length);
1787
+ if (to < from) to = from;
1788
+ } else {
1789
+ from = string_clamped_position(start, subject->length);
1790
+ to = string_clamped_position(end, subject->length);
1791
+ if (from > to) {
1792
+ size_t swap = from;
1793
+ from = to;
1794
+ to = swap;
1795
+ }
1796
+ }
1797
+ result = oseo_internal_allocate_string(
1798
+ context,
1799
+ subject->units + from,
1800
+ to - from
1801
+ );
1802
+ oseo_roots_release(context, &frame);
1803
+ return result;
1804
+ }
1805
+
1806
+ /*
1807
+ * String.prototype.substr(start, length), B.2.2.1. The start is relative
1808
+ * to the end when negative, the length is clamped to what remains, and
1809
+ * an absent length takes the rest of the string. Both conversions run
1810
+ * after the receiver's ToString, start before length.
1811
+ */
1812
+ static OseoResult string_substr(
1813
+ OseoContext *context,
1814
+ OseoValue receiver,
1815
+ size_t argument_count,
1816
+ const OseoValue *arguments
1817
+ ) {
1818
+ OseoRootFrame frame = {NULL, NULL, 0u};
1819
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
1820
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1821
+ frame.slots[0] = receiver;
1822
+ frame.slots[1] = string_builtin_argument(argument_count, arguments, 0u);
1823
+ frame.slots[2] = string_builtin_argument(argument_count, arguments, 1u);
1824
+ result = string_method_subject(context, frame.slots[0]);
1825
+ frame.slots[3] = result.value;
1826
+ if (result.status == OSEO_STATUS_NORMAL) {
1827
+ result = string_integer_or_infinity(context, frame.slots[1]);
1828
+ }
1829
+ double start = result.status == OSEO_STATUS_NORMAL
1830
+ ? number_value(result.value)
1831
+ : 0.0;
1832
+ double length = 0.0;
1833
+ if (result.status == OSEO_STATUS_NORMAL &&
1834
+ tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED) {
1835
+ length = (double)string_object(frame.slots[3])->length;
1836
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1837
+ result = string_integer_or_infinity(context, frame.slots[2]);
1838
+ if (result.status == OSEO_STATUS_NORMAL) {
1839
+ length = number_value(result.value);
1840
+ }
1841
+ }
1842
+ if (result.status != OSEO_STATUS_NORMAL) {
1843
+ oseo_roots_release(context, &frame);
1844
+ return result;
1845
+ }
1846
+ const OseoString *subject = string_object(frame.slots[3]);
1847
+ size_t from = string_relative_position(start, subject->length);
1848
+ size_t count = string_clamped_position(length, subject->length - from);
1849
+ result = oseo_internal_allocate_string(
1850
+ context,
1851
+ subject->units + from,
1852
+ count
1853
+ );
1854
+ oseo_roots_release(context, &frame);
1855
+ return result;
1856
+ }
1857
+
1858
+ /*
1859
+ * One Annex B HTML method of B.2.2.2 through B.2.2.14: the tag it wraps
1860
+ * the string in and the attribute it writes, or an empty attribute for
1861
+ * the eight methods that take no argument.
1862
+ */
1863
+ typedef struct {
1864
+ const char *name;
1865
+ const char *tag;
1866
+ const char *attribute;
1867
+ } OseoStringHtmlMethod;
1868
+
1869
+ static const OseoStringHtmlMethod string_html_methods[] = {
1870
+ {"anchor", "a", "name"},
1871
+ {"big", "big", ""},
1872
+ {"blink", "blink", ""},
1873
+ {"bold", "b", ""},
1874
+ {"fixed", "tt", ""},
1875
+ {"fontcolor", "font", "color"},
1876
+ {"fontsize", "font", "size"},
1877
+ {"italics", "i", ""},
1878
+ {"link", "a", "href"},
1879
+ {"small", "small", ""},
1880
+ {"strike", "strike", ""},
1881
+ {"sub", "sub", ""},
1882
+ {"sup", "sup", ""},
1883
+ };
1884
+
1885
+ _Static_assert(
1886
+ sizeof(string_html_methods) / sizeof(string_html_methods[0]) ==
1887
+ OSEO_STRING_HTML_METHOD_COUNT,
1888
+ "The HTML method table must match its reviewed code-ID range."
1889
+ );
1890
+
1891
+ /* Append the ASCII text `text` to `builder`. */
1892
+ static OseoResult string_builder_append_ascii(
1893
+ OseoContext *context,
1894
+ OseoStringBuilder *builder,
1895
+ const char *text
1896
+ ) {
1897
+ OseoResult result = normal(oseo_undefined());
1898
+ for (const char *unit = text;
1899
+ result.status == OSEO_STATUS_NORMAL && *unit != '\0';
1900
+ unit += 1) {
1901
+ result = string_builder_append_unit(
1902
+ context,
1903
+ builder,
1904
+ (uint16_t)(unsigned char)*unit
1905
+ );
1906
+ }
1907
+ return result;
1908
+ }
1909
+
1910
+ /*
1911
+ * CreateHTML(string, tag, attribute, value), B.2.2.2.1. The receiver's
1912
+ * ToString runs before the attribute value's, and every quotation mark
1913
+ * in the attribute value becomes `&quot;`; no other unit is escaped.
1914
+ */
1915
+ static OseoResult string_create_html(
1916
+ OseoContext *context,
1917
+ const OseoStringHtmlMethod *method,
1918
+ OseoValue receiver,
1919
+ size_t argument_count,
1920
+ const OseoValue *arguments
1921
+ ) {
1922
+ OseoValue slots[3] = {
1923
+ receiver,
1924
+ string_builtin_argument(argument_count, arguments, 0u),
1925
+ oseo_undefined(),
1926
+ };
1927
+ OseoRootFrame frame = {NULL, slots, 3u};
1928
+ oseo_roots_push(context, &frame);
1929
+ OseoStringBuilder builder = {NULL, 0u, 0u};
1930
+ bool attributed = method->attribute[0] != '\0';
1931
+ OseoResult result = string_method_subject(context, slots[0]);
1932
+ slots[0] = result.value;
1933
+ if (result.status == OSEO_STATUS_NORMAL && attributed) {
1934
+ result = oseo_internal_value_string(context, slots[1]);
1935
+ slots[2] = result.value;
1936
+ }
1937
+ if (result.status == OSEO_STATUS_NORMAL) {
1938
+ result = string_builder_append_ascii(context, &builder, "<");
1939
+ }
1940
+ if (result.status == OSEO_STATUS_NORMAL) {
1941
+ result = string_builder_append_ascii(context, &builder, method->tag);
1942
+ }
1943
+ if (result.status == OSEO_STATUS_NORMAL && attributed) {
1944
+ result = string_builder_append_ascii(context, &builder, " ");
1945
+ if (result.status == OSEO_STATUS_NORMAL) {
1946
+ result = string_builder_append_ascii(
1947
+ context,
1948
+ &builder,
1949
+ method->attribute
1950
+ );
1951
+ }
1952
+ if (result.status == OSEO_STATUS_NORMAL) {
1953
+ result = string_builder_append_ascii(context, &builder, "=\"");
1954
+ }
1955
+ const OseoString *value = result.status == OSEO_STATUS_NORMAL
1956
+ ? string_object(slots[2])
1957
+ : NULL;
1958
+ for (size_t index = 0u;
1959
+ result.status == OSEO_STATUS_NORMAL && index < value->length;
1960
+ index += 1u) {
1961
+ uint16_t unit = value->units[index];
1962
+ result = unit == (uint16_t)'"'
1963
+ ? string_builder_append_ascii(context, &builder, "&quot;")
1964
+ : string_builder_append_unit(context, &builder, unit);
1965
+ }
1966
+ if (result.status == OSEO_STATUS_NORMAL) {
1967
+ result = string_builder_append_ascii(context, &builder, "\"");
1968
+ }
1969
+ }
1970
+ if (result.status == OSEO_STATUS_NORMAL) {
1971
+ result = string_builder_append_ascii(context, &builder, ">");
1972
+ }
1973
+ if (result.status == OSEO_STATUS_NORMAL) {
1974
+ const OseoString *subject = string_object(slots[0]);
1975
+ result = oseo_internal_string_builder_append(
1976
+ context,
1977
+ &builder,
1978
+ subject->units,
1979
+ subject->length
1980
+ );
1981
+ }
1982
+ if (result.status == OSEO_STATUS_NORMAL) {
1983
+ result = string_builder_append_ascii(context, &builder, "</");
1984
+ }
1985
+ if (result.status == OSEO_STATUS_NORMAL) {
1986
+ result = string_builder_append_ascii(context, &builder, method->tag);
1987
+ }
1988
+ if (result.status == OSEO_STATUS_NORMAL) {
1989
+ result = string_builder_append_ascii(context, &builder, ">");
1990
+ }
1991
+ if (result.status == OSEO_STATUS_NORMAL) {
1992
+ result = oseo_internal_allocate_string(
1993
+ context,
1994
+ builder.units,
1995
+ builder.length
1996
+ );
1997
+ }
1998
+ oseo_internal_string_builder_release(&builder);
1999
+ oseo_roots_pop(context, &frame);
2000
+ return result;
2001
+ }
2002
+
2003
+ static OseoResult string_from_char_code(
2004
+ OseoContext *context,
2005
+ size_t argument_count,
2006
+ const OseoValue *arguments
2007
+ ) {
2008
+ OseoStringBuilder builder = {NULL, 0u, 0u};
2009
+ OseoResult result = normal(oseo_undefined());
2010
+ for (size_t index = 0u; index < argument_count; index += 1u) {
2011
+ result = oseo_internal_to_number(context, arguments[index]);
2012
+ if (result.status != OSEO_STATUS_NORMAL) break;
2013
+ result = string_builder_append_unit(
2014
+ context,
2015
+ &builder,
2016
+ string_to_uint16(number_value(result.value))
2017
+ );
2018
+ if (result.status != OSEO_STATUS_NORMAL) {
2019
+ break;
2020
+ }
2021
+ }
2022
+ if (result.status == OSEO_STATUS_NORMAL) {
2023
+ result = oseo_internal_allocate_string(
2024
+ context,
2025
+ builder.units,
2026
+ builder.length
2027
+ );
2028
+ }
2029
+ oseo_internal_string_builder_release(&builder);
2030
+ return result;
2031
+ }
2032
+
2033
+ static OseoResult string_from_code_point(
2034
+ OseoContext *context,
2035
+ size_t argument_count,
2036
+ const OseoValue *arguments
2037
+ ) {
2038
+ OseoStringBuilder builder = {NULL, 0u, 0u};
2039
+ OseoResult result = normal(oseo_undefined());
2040
+ for (size_t index = 0u; index < argument_count; index += 1u) {
2041
+ result = oseo_internal_to_number(context, arguments[index]);
2042
+ if (result.status != OSEO_STATUS_NORMAL) break;
2043
+ double next = number_value(result.value);
2044
+ if (!isfinite(next) || trunc(next) != next || next < 0.0 ||
2045
+ next > 1114111.0) {
2046
+ result = oseo_internal_throw_error(
2047
+ context,
2048
+ OSEO_ERROR_RANGE,
2049
+ "String.fromCodePoint received an invalid code point."
2050
+ );
2051
+ break;
2052
+ }
2053
+ uint32_t code_point = (uint32_t)next;
2054
+ if (code_point <= UINT32_C(0xffff)) {
2055
+ result = string_builder_append_unit(
2056
+ context,
2057
+ &builder,
2058
+ (uint16_t)code_point
2059
+ );
2060
+ } else {
2061
+ uint32_t rest = code_point - UINT32_C(0x10000);
2062
+ uint16_t pair[] = {
2063
+ (uint16_t)(UINT32_C(0xd800) + (rest >> 10u)),
2064
+ (uint16_t)(UINT32_C(0xdc00) + (rest & UINT32_C(0x3ff))),
2065
+ };
2066
+ result = oseo_internal_string_builder_append(
2067
+ context,
2068
+ &builder,
2069
+ pair,
2070
+ 2u
2071
+ );
2072
+ }
2073
+ if (result.status != OSEO_STATUS_NORMAL) break;
2074
+ }
2075
+ if (result.status == OSEO_STATUS_NORMAL) {
2076
+ result = oseo_internal_allocate_string(
2077
+ context,
2078
+ builder.units,
2079
+ builder.length
2080
+ );
2081
+ }
2082
+ oseo_internal_string_builder_release(&builder);
2083
+ return result;
2084
+ }
2085
+
2086
+ /* LengthOfArrayLike (7.3.18) reported as an exact non-negative double. */
2087
+ static OseoResult string_length_of_array_like(
2088
+ OseoContext *context,
2089
+ OseoValue object_value,
2090
+ double *length
2091
+ ) {
2092
+ OseoValue slots[2] = {object_value, oseo_undefined()};
2093
+ OseoRootFrame frame = {NULL, slots, 2u};
2094
+ oseo_roots_push(context, &frame);
2095
+ OseoResult result = oseo_internal_ascii_string(context, "length");
2096
+ slots[1] = result.value;
2097
+ if (result.status == OSEO_STATUS_NORMAL) {
2098
+ result = oseo_object_get(context, slots[0], slots[1]);
2099
+ }
2100
+ if (result.status == OSEO_STATUS_NORMAL) {
2101
+ result = oseo_internal_to_number(context, result.value);
2102
+ }
2103
+ oseo_roots_pop(context, &frame);
2104
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2105
+ double value = number_value(result.value);
2106
+ if (isnan(value)) value = 0.0;
2107
+ else value = trunc(value);
2108
+ if (!(value > 0.0)) value = 0.0;
2109
+ else if (value > 9007199254740991.0) value = 9007199254740991.0;
2110
+ *length = value;
2111
+ return normal(oseo_number(value));
2112
+ }
2113
+
2114
+ static OseoResult string_raw(
2115
+ OseoContext *context,
2116
+ size_t argument_count,
2117
+ const OseoValue *arguments
2118
+ ) {
2119
+ size_t substitution_count = argument_count == 0u
2120
+ ? 0u
2121
+ : argument_count - 1u;
2122
+ OseoStringBuilder builder = {NULL, 0u, 0u};
2123
+ OseoRootFrame frame = {NULL, NULL, 0u};
2124
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
2125
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2126
+ result = oseo_internal_to_object(
2127
+ context,
2128
+ argument_count == 0u ? oseo_undefined() : arguments[0]
2129
+ );
2130
+ frame.slots[0] = result.value;
2131
+ if (result.status == OSEO_STATUS_NORMAL) {
2132
+ result = oseo_internal_ascii_string(context, "raw");
2133
+ frame.slots[1] = result.value;
2134
+ }
2135
+ if (result.status == OSEO_STATUS_NORMAL) {
2136
+ result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
2137
+ frame.slots[1] = result.value;
2138
+ }
2139
+ if (result.status == OSEO_STATUS_NORMAL) {
2140
+ result = oseo_internal_to_object(context, frame.slots[1]);
2141
+ frame.slots[1] = result.value;
2142
+ }
2143
+ double literal_count = 0.0;
2144
+ if (result.status == OSEO_STATUS_NORMAL) {
2145
+ result = string_length_of_array_like(
2146
+ context,
2147
+ frame.slots[1],
2148
+ &literal_count
2149
+ );
2150
+ }
2151
+ for (double next = 0.0;
2152
+ result.status == OSEO_STATUS_NORMAL && next < literal_count;
2153
+ next += 1.0) {
2154
+ char key_text[32];
2155
+ (void)snprintf(key_text, sizeof(key_text), "%.0f", next);
2156
+ result = oseo_internal_ascii_string(context, key_text);
2157
+ frame.slots[2] = result.value;
2158
+ if (result.status != OSEO_STATUS_NORMAL) break;
2159
+ result = oseo_object_get(context, frame.slots[1], frame.slots[2]);
2160
+ frame.slots[2] = result.value;
2161
+ if (result.status != OSEO_STATUS_NORMAL) break;
2162
+ result = oseo_internal_value_string(context, frame.slots[2]);
2163
+ frame.slots[2] = result.value;
2164
+ if (result.status != OSEO_STATUS_NORMAL) break;
2165
+ result = oseo_internal_string_builder_append(
2166
+ context,
2167
+ &builder,
2168
+ string_object(frame.slots[2])->units,
2169
+ string_object(frame.slots[2])->length
2170
+ );
2171
+ if (result.status != OSEO_STATUS_NORMAL) {
2172
+ break;
2173
+ }
2174
+ if (next + 1.0 >= literal_count) break;
2175
+ if (next >= (double)substitution_count) continue;
2176
+ result = oseo_internal_value_string(
2177
+ context,
2178
+ arguments[(size_t)next + 1u]
2179
+ );
2180
+ frame.slots[2] = result.value;
2181
+ if (result.status != OSEO_STATUS_NORMAL) break;
2182
+ result = oseo_internal_string_builder_append(
2183
+ context,
2184
+ &builder,
2185
+ string_object(frame.slots[2])->units,
2186
+ string_object(frame.slots[2])->length
2187
+ );
2188
+ if (result.status != OSEO_STATUS_NORMAL) {
2189
+ break;
2190
+ }
2191
+ }
2192
+ if (result.status == OSEO_STATUS_NORMAL) {
2193
+ result = oseo_internal_allocate_string(
2194
+ context,
2195
+ builder.units,
2196
+ builder.length
2197
+ );
2198
+ }
2199
+ oseo_internal_string_builder_release(&builder);
2200
+ oseo_roots_release(context, &frame);
2201
+ return result;
2202
+ }
2203
+
2204
+ /*
2205
+ * The String constructor (22.1.1.1). A call with a Symbol argument
2206
+ * renders SymbolDescriptiveString instead of throwing, which is the one
2207
+ * conversion difference between calling and constructing. Construction
2208
+ * runs StringCreate, which takes its prototype from
2209
+ * GetPrototypeFromConstructor(NewTarget, "%String.prototype%") after
2210
+ * the argument's ToString, then brands the object it creates with
2211
+ * [[StringData]] and gives it the exotic own properties. A new target
2212
+ * whose `prototype` is an accessor therefore observes that order, and
2213
+ * an abrupt getter reports after the conversion. The read is a real Get
2214
+ * because `Reflect.construct` can name a bound function, which owns no
2215
+ * synthetic `prototype` slot.
2216
+ */
2217
+ static OseoResult string_construct(
2218
+ OseoContext *context,
2219
+ OseoValue new_target,
2220
+ size_t argument_count,
2221
+ const OseoValue *arguments,
2222
+ bool constructing
2223
+ ) {
2224
+ /* Slots: 0 the new target, 1 the converted string, 2 the wrapper.
2225
+ * The conversion is a safepoint and can run arbitrary code, so the
2226
+ * new target is rooted across it. */
2227
+ OseoValue slots[3] = {new_target, oseo_undefined(), oseo_undefined()};
2228
+ OseoRootFrame frame = {NULL, slots, 3u};
2229
+ oseo_roots_push(context, &frame);
2230
+ bool descriptive =
2231
+ argument_count > 0u && !constructing && is_symbol(arguments[0]);
2232
+ OseoResult result;
2233
+ if (descriptive) {
2234
+ result = oseo_internal_symbol_text(context, arguments[0]);
2235
+ } else if (argument_count == 0u) {
2236
+ result = oseo_internal_allocate_string(context, NULL, 0u);
2237
+ } else {
2238
+ result = oseo_internal_value_string(context, arguments[0]);
2239
+ }
2240
+ slots[1] = result.value;
2241
+ if (!descriptive && result.status == OSEO_STATUS_NORMAL && constructing) {
2242
+ result = oseo_internal_constructor_prototype(context, slots[0]);
2243
+ slots[2] = result.value;
2244
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(slots[2])) {
2245
+ result = oseo_internal_constructor_realm_default(
2246
+ context,
2247
+ slots[0],
2248
+ OSEO_INTRINSIC_STRING_PROTOTYPE
2249
+ );
2250
+ slots[2] = result.value;
2251
+ }
2252
+ if (result.status == OSEO_STATUS_NORMAL) {
2253
+ result = oseo_object_create(context, slots[2]);
2254
+ slots[2] = result.value;
2255
+ }
2256
+ if (result.status == OSEO_STATUS_NORMAL) {
2257
+ OseoOrdinaryObject *wrapper = ordinary_object(slots[2]);
2258
+ wrapper->primitive_data = true;
2259
+ wrapper->primitive_value = slots[1];
2260
+ result = oseo_internal_string_wrapper_properties(
2261
+ context,
2262
+ slots[1],
2263
+ slots[2]
2264
+ );
2265
+ }
2266
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(slots[2]);
2267
+ }
2268
+ oseo_roots_pop(context, &frame);
2269
+ return result;
2270
+ }
2271
+
2272
+ OseoResult oseo_internal_string_builtin_dispatch(
2273
+ OseoContext *context,
2274
+ size_t code_id,
2275
+ OseoValue callee,
2276
+ OseoValue receiver,
2277
+ size_t argument_count,
2278
+ const OseoValue *arguments,
2279
+ OseoValue new_target
2280
+ ) {
2281
+ (void)callee;
2282
+ if (code_id == OSEO_STRING_CONSTRUCTOR_CODE_ID) {
2283
+ return string_construct(
2284
+ context,
2285
+ new_target,
2286
+ argument_count,
2287
+ arguments,
2288
+ tag_of(new_target) != OSEO_TAG_UNDEFINED
2289
+ );
2290
+ }
2291
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
2292
+ return oseo_internal_throw_error(
2293
+ context,
2294
+ OSEO_ERROR_TYPE,
2295
+ "String method is not a constructor."
2296
+ );
2297
+ }
2298
+ if (code_id == OSEO_STRING_AT_CODE_ID ||
2299
+ code_id == OSEO_STRING_CHAR_AT_CODE_ID ||
2300
+ code_id == OSEO_STRING_CHAR_CODE_AT_CODE_ID ||
2301
+ code_id == OSEO_STRING_CODE_POINT_AT_CODE_ID) {
2302
+ return string_at_index(
2303
+ context,
2304
+ code_id,
2305
+ receiver,
2306
+ argument_count,
2307
+ arguments
2308
+ );
2309
+ }
2310
+ if (code_id == OSEO_STRING_TO_STRING_CODE_ID ||
2311
+ code_id == OSEO_STRING_VALUE_OF_CODE_ID) {
2312
+ return string_this_value(context, receiver);
2313
+ }
2314
+ if (code_id == OSEO_STRING_CONCAT_CODE_ID) {
2315
+ return string_concat(context, receiver, argument_count, arguments);
2316
+ }
2317
+ if (code_id == OSEO_STRING_INDEX_OF_CODE_ID ||
2318
+ code_id == OSEO_STRING_LAST_INDEX_OF_CODE_ID ||
2319
+ code_id == OSEO_STRING_INCLUDES_CODE_ID ||
2320
+ code_id == OSEO_STRING_STARTS_WITH_CODE_ID ||
2321
+ code_id == OSEO_STRING_ENDS_WITH_CODE_ID) {
2322
+ return string_search(
2323
+ context,
2324
+ code_id,
2325
+ receiver,
2326
+ argument_count,
2327
+ arguments
2328
+ );
2329
+ }
2330
+ if (code_id == OSEO_STRING_SLICE_CODE_ID ||
2331
+ code_id == OSEO_STRING_SUBSTRING_CODE_ID) {
2332
+ return string_slice(
2333
+ context,
2334
+ code_id,
2335
+ receiver,
2336
+ argument_count,
2337
+ arguments
2338
+ );
2339
+ }
2340
+ if (code_id == OSEO_STRING_SUBSTR_CODE_ID) {
2341
+ return string_substr(context, receiver, argument_count, arguments);
2342
+ }
2343
+ if (code_id >= OSEO_STRING_HTML_CODE_ID_FIRST &&
2344
+ code_id <= OSEO_STRING_HTML_CODE_ID_LAST) {
2345
+ return string_create_html(
2346
+ context,
2347
+ &string_html_methods[code_id - OSEO_STRING_HTML_CODE_ID_FIRST],
2348
+ receiver,
2349
+ argument_count,
2350
+ arguments
2351
+ );
2352
+ }
2353
+ if (code_id == OSEO_STRING_LOWERCASE_CODE_ID ||
2354
+ code_id == OSEO_STRING_UPPERCASE_CODE_ID) {
2355
+ return string_convert_case(
2356
+ context,
2357
+ receiver,
2358
+ code_id == OSEO_STRING_UPPERCASE_CODE_ID
2359
+ );
2360
+ }
2361
+ if (code_id == OSEO_STRING_TRIM_CODE_ID ||
2362
+ code_id == OSEO_STRING_TRIM_START_CODE_ID ||
2363
+ code_id == OSEO_STRING_TRIM_END_CODE_ID) {
2364
+ return string_trim(
2365
+ context,
2366
+ receiver,
2367
+ code_id != OSEO_STRING_TRIM_END_CODE_ID,
2368
+ code_id != OSEO_STRING_TRIM_START_CODE_ID
2369
+ );
2370
+ }
2371
+ if (code_id == OSEO_STRING_NORMALIZE_CODE_ID) {
2372
+ return string_normalize(
2373
+ context,
2374
+ receiver,
2375
+ argument_count,
2376
+ arguments
2377
+ );
2378
+ }
2379
+ if (code_id == OSEO_STRING_LOCALE_COMPARE_CODE_ID) {
2380
+ return string_locale_compare(
2381
+ context,
2382
+ receiver,
2383
+ argument_count,
2384
+ arguments
2385
+ );
2386
+ }
2387
+ if (code_id == OSEO_STRING_REPEAT_CODE_ID) {
2388
+ return string_repeat(context, receiver, argument_count, arguments);
2389
+ }
2390
+ if (code_id == OSEO_STRING_PAD_START_CODE_ID ||
2391
+ code_id == OSEO_STRING_PAD_END_CODE_ID) {
2392
+ return string_pad(
2393
+ context,
2394
+ receiver,
2395
+ argument_count,
2396
+ arguments,
2397
+ code_id == OSEO_STRING_PAD_START_CODE_ID
2398
+ );
2399
+ }
2400
+ if (code_id == OSEO_STRING_IS_WELL_FORMED_CODE_ID ||
2401
+ code_id == OSEO_STRING_TO_WELL_FORMED_CODE_ID) {
2402
+ return string_well_formed(
2403
+ context,
2404
+ receiver,
2405
+ code_id == OSEO_STRING_TO_WELL_FORMED_CODE_ID
2406
+ );
2407
+ }
2408
+ if (code_id == OSEO_STRING_MATCH_CODE_ID ||
2409
+ code_id == OSEO_STRING_MATCH_ALL_CODE_ID ||
2410
+ code_id == OSEO_STRING_SEARCH_CODE_ID ||
2411
+ code_id == OSEO_STRING_SPLIT_CODE_ID ||
2412
+ code_id == OSEO_STRING_REPLACE_CODE_ID ||
2413
+ code_id == OSEO_STRING_REPLACE_ALL_CODE_ID) {
2414
+ return oseo_internal_string_protocol_dispatch(
2415
+ context,
2416
+ code_id,
2417
+ receiver,
2418
+ argument_count,
2419
+ arguments
2420
+ );
2421
+ }
2422
+ if (code_id == OSEO_REGEXP_STRING_ITERATOR_NEXT_CODE_ID) {
2423
+ return oseo_internal_string_protocol_dispatch(
2424
+ context,
2425
+ code_id,
2426
+ receiver,
2427
+ argument_count,
2428
+ arguments
2429
+ );
2430
+ }
2431
+ if (code_id == OSEO_STRING_FROM_CHAR_CODE_CODE_ID) {
2432
+ return string_from_char_code(context, argument_count, arguments);
2433
+ }
2434
+ if (code_id == OSEO_STRING_FROM_CODE_POINT_CODE_ID) {
2435
+ return string_from_code_point(context, argument_count, arguments);
2436
+ }
2437
+ if (code_id == OSEO_STRING_RAW_CODE_ID) {
2438
+ return string_raw(context, argument_count, arguments);
2439
+ }
2440
+ if (code_id == OSEO_STRING_UNADMITTED_METHOD_CODE_ID) {
2441
+ return failure(
2442
+ context,
2443
+ "OSEO2001",
2444
+ "String prototype method is not admitted in this M5b node."
2445
+ );
2446
+ }
2447
+ return oseo_unknown_function(context, code_id);
2448
+ }
2449
+
2450
+ static OseoResult create_string_function(
2451
+ OseoContext *context,
2452
+ size_t code_id,
2453
+ const char *name,
2454
+ size_t length,
2455
+ OseoFunctionKind kind
2456
+ ) {
2457
+ size_t name_length = strlen(name);
2458
+ uint16_t units[31];
2459
+ if (name_length > sizeof(units) / sizeof(*units)) {
2460
+ return failure(context, "OSEO2001", "Built-in name is too long.");
2461
+ }
2462
+ for (size_t index = 0u; index < name_length; index += 1u) {
2463
+ units[index] = (uint16_t)(unsigned char)name[index];
2464
+ }
2465
+ OseoValue environment = oseo_undefined();
2466
+ OseoRootFrame frame = {NULL, &environment, 1u};
2467
+ oseo_roots_push(context, &frame);
2468
+ OseoResult result = oseo_environment_create(context, 0u);
2469
+ environment = result.value;
2470
+ if (result.status == OSEO_STATUS_NORMAL) {
2471
+ result = oseo_function_create(
2472
+ context,
2473
+ code_id,
2474
+ environment,
2475
+ units,
2476
+ name_length,
2477
+ length,
2478
+ kind,
2479
+ oseo_undefined(),
2480
+ oseo_undefined(),
2481
+ OSEO_FUNCTION_NAME_PREFIX_NONE
2482
+ );
2483
+ }
2484
+ oseo_roots_pop(context, &frame);
2485
+ return result;
2486
+ }
2487
+
2488
+ static OseoResult define_string_property(
2489
+ OseoContext *context,
2490
+ OseoValue object,
2491
+ const char *name,
2492
+ OseoValue value,
2493
+ OseoPropertyAttributes attributes
2494
+ ) {
2495
+ OseoValue slots[3] = {object, value, oseo_undefined()};
2496
+ OseoRootFrame frame = {NULL, slots, 3u};
2497
+ oseo_roots_push(context, &frame);
2498
+ OseoResult result = oseo_internal_ascii_string(context, name);
2499
+ slots[2] = result.value;
2500
+ if (result.status == OSEO_STATUS_NORMAL) {
2501
+ result = oseo_object_define(
2502
+ context,
2503
+ slots[0],
2504
+ slots[2],
2505
+ slots[1],
2506
+ attributes
2507
+ );
2508
+ }
2509
+ oseo_roots_pop(context, &frame);
2510
+ return result;
2511
+ }
2512
+
2513
+ OseoResult oseo_internal_string_intrinsic(OseoContext *context) {
2514
+ OseoValue *marker = &context->realm->intrinsics[OSEO_INTRINSIC_STRING_RAW];
2515
+ if (tag_of(*marker) != OSEO_TAG_UNDEFINED) {
2516
+ return normal(context->realm->intrinsics[OSEO_INTRINSIC_STRING]);
2517
+ }
2518
+ size_t entry_allocations = context->allocations;
2519
+ OseoRootFrame frame = {NULL, NULL, 0u};
2520
+ /*
2521
+ * Slots 3 and 4 keep trimStart and trimEnd so that the Annex B
2522
+ * trimLeft and trimRight properties can name the same functions.
2523
+ */
2524
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
2525
+ if (result.status != OSEO_STATUS_NORMAL) return result;
2526
+ result = oseo_internal_intrinsic(
2527
+ context,
2528
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
2529
+ );
2530
+ frame.slots[0] = result.value;
2531
+ if (result.status == OSEO_STATUS_NORMAL) {
2532
+ result = oseo_object_create(context, frame.slots[0]);
2533
+ frame.slots[0] = result.value;
2534
+ }
2535
+ if (result.status == OSEO_STATUS_NORMAL) {
2536
+ result = oseo_internal_allocate_string(context, NULL, 0u);
2537
+ frame.slots[1] = result.value;
2538
+ }
2539
+ if (result.status == OSEO_STATUS_NORMAL) {
2540
+ /*
2541
+ * %String.prototype% is itself a String exotic object whose
2542
+ * [[StringData]] is the empty String, so it carries the same
2543
+ * brand and the same non-writable `length` an instance does.
2544
+ */
2545
+ OseoOrdinaryObject *prototype = ordinary_object(frame.slots[0]);
2546
+ prototype->primitive_data = true;
2547
+ prototype->primitive_value = frame.slots[1];
2548
+ prototype->virtual_string_iterator = true;
2549
+ prototype->virtual_string_iterator_configurable = true;
2550
+ prototype->virtual_string_iterator_enumerable = false;
2551
+ prototype->virtual_string_iterator_writable = true;
2552
+ context->realm->intrinsics[OSEO_INTRINSIC_STRING_PROTOTYPE] =
2553
+ frame.slots[0];
2554
+ /*
2555
+ * The marker slot is claimed before the remaining allocations so
2556
+ * a nested materialization cannot start a second construction;
2557
+ * the failure path below clears every slot again.
2558
+ */
2559
+ *marker = frame.slots[0];
2560
+ result = oseo_internal_string_wrapper_properties(
2561
+ context,
2562
+ frame.slots[1],
2563
+ frame.slots[0]
2564
+ );
2565
+ }
2566
+ if (result.status == OSEO_STATUS_NORMAL) {
2567
+ result = oseo_internal_install_primitive_wrapper_methods(
2568
+ context,
2569
+ frame.slots[0],
2570
+ true
2571
+ );
2572
+ }
2573
+ static const size_t access_codes[] = {
2574
+ OSEO_STRING_AT_CODE_ID,
2575
+ OSEO_STRING_CHAR_AT_CODE_ID,
2576
+ OSEO_STRING_CHAR_CODE_AT_CODE_ID,
2577
+ OSEO_STRING_CODE_POINT_AT_CODE_ID,
2578
+ OSEO_STRING_TO_STRING_CODE_ID,
2579
+ OSEO_STRING_VALUE_OF_CODE_ID,
2580
+ OSEO_STRING_CONCAT_CODE_ID,
2581
+ OSEO_STRING_INDEX_OF_CODE_ID,
2582
+ OSEO_STRING_LAST_INDEX_OF_CODE_ID,
2583
+ OSEO_STRING_INCLUDES_CODE_ID,
2584
+ OSEO_STRING_STARTS_WITH_CODE_ID,
2585
+ OSEO_STRING_ENDS_WITH_CODE_ID,
2586
+ OSEO_STRING_SLICE_CODE_ID,
2587
+ OSEO_STRING_SUBSTRING_CODE_ID,
2588
+ OSEO_STRING_MATCH_CODE_ID,
2589
+ OSEO_STRING_MATCH_ALL_CODE_ID,
2590
+ OSEO_STRING_SEARCH_CODE_ID,
2591
+ OSEO_STRING_SPLIT_CODE_ID,
2592
+ OSEO_STRING_REPLACE_CODE_ID,
2593
+ OSEO_STRING_REPLACE_ALL_CODE_ID,
2594
+ OSEO_STRING_LOCALE_COMPARE_CODE_ID,
2595
+ OSEO_STRING_LOWERCASE_CODE_ID,
2596
+ OSEO_STRING_UPPERCASE_CODE_ID,
2597
+ OSEO_STRING_LOWERCASE_CODE_ID,
2598
+ OSEO_STRING_UPPERCASE_CODE_ID,
2599
+ OSEO_STRING_TRIM_CODE_ID,
2600
+ OSEO_STRING_TRIM_START_CODE_ID,
2601
+ OSEO_STRING_TRIM_END_CODE_ID,
2602
+ OSEO_STRING_NORMALIZE_CODE_ID,
2603
+ OSEO_STRING_REPEAT_CODE_ID,
2604
+ OSEO_STRING_PAD_START_CODE_ID,
2605
+ OSEO_STRING_PAD_END_CODE_ID,
2606
+ OSEO_STRING_IS_WELL_FORMED_CODE_ID,
2607
+ OSEO_STRING_TO_WELL_FORMED_CODE_ID,
2608
+ OSEO_STRING_SUBSTR_CODE_ID,
2609
+ OSEO_STRING_HTML_CODE_ID_FIRST + 0u,
2610
+ OSEO_STRING_HTML_CODE_ID_FIRST + 1u,
2611
+ OSEO_STRING_HTML_CODE_ID_FIRST + 2u,
2612
+ OSEO_STRING_HTML_CODE_ID_FIRST + 3u,
2613
+ OSEO_STRING_HTML_CODE_ID_FIRST + 4u,
2614
+ OSEO_STRING_HTML_CODE_ID_FIRST + 5u,
2615
+ OSEO_STRING_HTML_CODE_ID_FIRST + 6u,
2616
+ OSEO_STRING_HTML_CODE_ID_FIRST + 7u,
2617
+ OSEO_STRING_HTML_CODE_ID_FIRST + 8u,
2618
+ OSEO_STRING_HTML_CODE_ID_FIRST + 9u,
2619
+ OSEO_STRING_HTML_CODE_ID_FIRST + 10u,
2620
+ OSEO_STRING_HTML_CODE_ID_FIRST + 11u,
2621
+ OSEO_STRING_HTML_CODE_ID_FIRST + 12u,
2622
+ };
2623
+ static const char *const access_names[] = {
2624
+ "at",
2625
+ "charAt",
2626
+ "charCodeAt",
2627
+ "codePointAt",
2628
+ "toString",
2629
+ "valueOf",
2630
+ "concat",
2631
+ "indexOf",
2632
+ "lastIndexOf",
2633
+ "includes",
2634
+ "startsWith",
2635
+ "endsWith",
2636
+ "slice",
2637
+ "substring",
2638
+ "match",
2639
+ "matchAll",
2640
+ "search",
2641
+ "split",
2642
+ "replace",
2643
+ "replaceAll",
2644
+ "localeCompare",
2645
+ "toLocaleLowerCase",
2646
+ "toLocaleUpperCase",
2647
+ "toLowerCase",
2648
+ "toUpperCase",
2649
+ "trim",
2650
+ "trimStart",
2651
+ "trimEnd",
2652
+ "normalize",
2653
+ "repeat",
2654
+ "padStart",
2655
+ "padEnd",
2656
+ "isWellFormed",
2657
+ "toWellFormed",
2658
+ "substr",
2659
+ "anchor",
2660
+ "big",
2661
+ "blink",
2662
+ "bold",
2663
+ "fixed",
2664
+ "fontcolor",
2665
+ "fontsize",
2666
+ "italics",
2667
+ "link",
2668
+ "small",
2669
+ "strike",
2670
+ "sub",
2671
+ "sup",
2672
+ };
2673
+ static const size_t access_lengths[] = {
2674
+ 1u, 1u, 1u, 1u, 0u, 0u, 1u,
2675
+ 1u, 1u, 1u, 1u, 1u, 2u, 2u,
2676
+ 1u, 1u, 1u, 2u, 2u, 2u,
2677
+ 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
2678
+ 1u, 1u, 1u, 0u, 0u,
2679
+ 2u, 1u, 0u, 0u, 0u, 0u, 1u, 1u, 0u, 1u, 0u, 0u, 0u, 0u,
2680
+ };
2681
+ _Static_assert(
2682
+ sizeof(access_codes) / sizeof(access_codes[0]) ==
2683
+ sizeof(access_names) / sizeof(access_names[0]) &&
2684
+ sizeof(access_codes) / sizeof(access_codes[0]) ==
2685
+ sizeof(access_lengths) / sizeof(access_lengths[0]),
2686
+ "Every String.prototype method needs a name and a length."
2687
+ );
2688
+ for (size_t index = 0u;
2689
+ result.status == OSEO_STATUS_NORMAL &&
2690
+ index < sizeof(access_codes) / sizeof(access_codes[0]);
2691
+ index += 1u) {
2692
+ result = create_string_function(
2693
+ context,
2694
+ access_codes[index],
2695
+ access_names[index],
2696
+ access_lengths[index],
2697
+ OSEO_FUNCTION_INTERNAL
2698
+ );
2699
+ frame.slots[1] = result.value;
2700
+ if (result.status == OSEO_STATUS_NORMAL) {
2701
+ result = define_string_property(
2702
+ context,
2703
+ frame.slots[0],
2704
+ access_names[index],
2705
+ frame.slots[1],
2706
+ (OseoPropertyAttributes){true, false, true, false}
2707
+ );
2708
+ }
2709
+ if (access_codes[index] == OSEO_STRING_TRIM_START_CODE_ID) {
2710
+ frame.slots[3] = frame.slots[1];
2711
+ } else if (access_codes[index] == OSEO_STRING_TRIM_END_CODE_ID) {
2712
+ frame.slots[4] = frame.slots[1];
2713
+ }
2714
+ }
2715
+ /*
2716
+ * B.2.2.15 and B.2.2.16: the initial values of trimLeft and trimRight
2717
+ * are the trimStart and trimEnd function objects themselves, so the
2718
+ * aliases keep those names.
2719
+ */
2720
+ if (result.status == OSEO_STATUS_NORMAL) {
2721
+ result = define_string_property(
2722
+ context,
2723
+ frame.slots[0],
2724
+ "trimLeft",
2725
+ frame.slots[3],
2726
+ (OseoPropertyAttributes){true, false, true, false}
2727
+ );
2728
+ }
2729
+ if (result.status == OSEO_STATUS_NORMAL) {
2730
+ result = define_string_property(
2731
+ context,
2732
+ frame.slots[0],
2733
+ "trimRight",
2734
+ frame.slots[4],
2735
+ (OseoPropertyAttributes){true, false, true, false}
2736
+ );
2737
+ }
2738
+ if (result.status == OSEO_STATUS_NORMAL) {
2739
+ result = oseo_internal_iterator_method(
2740
+ context,
2741
+ OSEO_STRING_PROTOTYPE_ITERATOR_CODE_ID
2742
+ );
2743
+ frame.slots[1] = result.value;
2744
+ }
2745
+ if (result.status == OSEO_STATUS_NORMAL) {
2746
+ result = oseo_internal_well_known_symbol(
2747
+ context,
2748
+ OSEO_WELL_KNOWN_ITERATOR
2749
+ );
2750
+ frame.slots[2] = result.value;
2751
+ }
2752
+ if (result.status == OSEO_STATUS_NORMAL) {
2753
+ result = oseo_object_define(
2754
+ context,
2755
+ frame.slots[0],
2756
+ frame.slots[2],
2757
+ frame.slots[1],
2758
+ (OseoPropertyAttributes){true, false, true, false}
2759
+ );
2760
+ }
2761
+ if (result.status == OSEO_STATUS_NORMAL) {
2762
+ result = create_string_function(
2763
+ context,
2764
+ OSEO_STRING_CONSTRUCTOR_CODE_ID,
2765
+ "String",
2766
+ 1u,
2767
+ OSEO_FUNCTION_ORDINARY
2768
+ );
2769
+ frame.slots[1] = result.value;
2770
+ }
2771
+ if (result.status == OSEO_STATUS_NORMAL) {
2772
+ context->realm->intrinsics[OSEO_INTRINSIC_STRING] = frame.slots[1];
2773
+ OseoFunction *constructor = function_object(frame.slots[1]);
2774
+ constructor->prototype_object = frame.slots[0];
2775
+ constructor->prototype_writable = false;
2776
+ result = define_string_property(
2777
+ context,
2778
+ frame.slots[0],
2779
+ "constructor",
2780
+ frame.slots[1],
2781
+ (OseoPropertyAttributes){true, false, true, false}
2782
+ );
2783
+ }
2784
+ static const OseoIntrinsic static_intrinsics[] = {
2785
+ OSEO_INTRINSIC_STRING_FROM_CHAR_CODE,
2786
+ OSEO_INTRINSIC_STRING_FROM_CODE_POINT,
2787
+ OSEO_INTRINSIC_STRING_RAW,
2788
+ };
2789
+ static const size_t static_codes[] = {
2790
+ OSEO_STRING_FROM_CHAR_CODE_CODE_ID,
2791
+ OSEO_STRING_FROM_CODE_POINT_CODE_ID,
2792
+ OSEO_STRING_RAW_CODE_ID,
2793
+ };
2794
+ static const char *const static_names[] = {
2795
+ "fromCharCode",
2796
+ "fromCodePoint",
2797
+ "raw",
2798
+ };
2799
+ for (size_t index = 0u;
2800
+ result.status == OSEO_STATUS_NORMAL && index < 3u;
2801
+ index += 1u) {
2802
+ result = create_string_function(
2803
+ context,
2804
+ static_codes[index],
2805
+ static_names[index],
2806
+ 1u,
2807
+ OSEO_FUNCTION_INTERNAL
2808
+ );
2809
+ frame.slots[2] = result.value;
2810
+ if (result.status != OSEO_STATUS_NORMAL) break;
2811
+ context->realm->intrinsics[static_intrinsics[index]] = frame.slots[2];
2812
+ result = define_string_property(
2813
+ context,
2814
+ frame.slots[1],
2815
+ static_names[index],
2816
+ frame.slots[2],
2817
+ (OseoPropertyAttributes){true, false, true, false}
2818
+ );
2819
+ }
2820
+ if (result.status != OSEO_STATUS_NORMAL) {
2821
+ context->realm->intrinsics[OSEO_INTRINSIC_STRING_PROTOTYPE] =
2822
+ oseo_undefined();
2823
+ for (size_t index = OSEO_INTRINSIC_STRING;
2824
+ index <= OSEO_INTRINSIC_STRING_RAW;
2825
+ index += 1u) {
2826
+ context->realm->intrinsics[index] = oseo_undefined();
2827
+ }
2828
+ context->realm->intrinsics[OSEO_INTRINSIC_STRING_PROTOTYPE_ITERATOR] =
2829
+ oseo_undefined();
2830
+ } else if (context->observe_specialization) {
2831
+ context->allocations = entry_allocations;
2832
+ }
2833
+ oseo_roots_release(context, &frame);
2834
+ return result.status == OSEO_STATUS_NORMAL
2835
+ ? normal(context->realm->intrinsics[OSEO_INTRINSIC_STRING])
2836
+ : result;
2837
+ }
2838
+
2839
+ OseoResult oseo_internal_install_string_global(
2840
+ OseoContext *context,
2841
+ OseoValue global
2842
+ ) {
2843
+ OseoValue slots[2] = {global, oseo_undefined()};
2844
+ OseoRootFrame frame = {NULL, slots, 2u};
2845
+ oseo_roots_push(context, &frame);
2846
+ OseoResult result = oseo_internal_string_intrinsic(context);
2847
+ slots[1] = result.value;
2848
+ if (result.status == OSEO_STATUS_NORMAL) {
2849
+ result = define_string_property(
2850
+ context,
2851
+ slots[0],
2852
+ "String",
2853
+ slots[1],
2854
+ (OseoPropertyAttributes){true, false, true, false}
2855
+ );
2856
+ }
2857
+ OseoValue global_value = slots[0];
2858
+ oseo_roots_pop(context, &frame);
2859
+ return result.status == OSEO_STATUS_NORMAL
2860
+ ? normal(global_value)
2861
+ : result;
2862
+ }