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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +28 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2618 -0
  10. package/native/runtime_agent.c +1717 -0
  11. package/native/runtime_arguments.c +379 -0
  12. package/native/runtime_array.c +5766 -0
  13. package/native/runtime_array_buffer.c +1748 -0
  14. package/native/runtime_async_generator.c +972 -0
  15. package/native/runtime_atomics.c +1283 -0
  16. package/native/runtime_bigint.c +1387 -0
  17. package/native/runtime_bigint_object.c +626 -0
  18. package/native/runtime_binding.c +1029 -0
  19. package/native/runtime_boolean.c +320 -0
  20. package/native/runtime_clock.c +181 -0
  21. package/native/runtime_clock_posix.c +599 -0
  22. package/native/runtime_core.c +488 -0
  23. package/native/runtime_data_view.c +1135 -0
  24. package/native/runtime_date.c +1997 -0
  25. package/native/runtime_descriptor.c +1104 -0
  26. package/native/runtime_disposal.c +1667 -0
  27. package/native/runtime_enumeration.c +636 -0
  28. package/native/runtime_error.c +997 -0
  29. package/native/runtime_event_loop.c +653 -0
  30. package/native/runtime_function.c +3667 -0
  31. package/native/runtime_generator.c +1120 -0
  32. package/native/runtime_internal.h +5578 -0
  33. package/native/runtime_iterator.c +5379 -0
  34. package/native/runtime_json.c +1992 -0
  35. package/native/runtime_map.c +1563 -0
  36. package/native/runtime_math.c +918 -0
  37. package/native/runtime_memory.c +1365 -0
  38. package/native/runtime_module.c +884 -0
  39. package/native/runtime_number.c +2077 -0
  40. package/native/runtime_object.c +423 -0
  41. package/native/runtime_object_builtin.c +3901 -0
  42. package/native/runtime_primitive.c +2268 -0
  43. package/native/runtime_process.c +744 -0
  44. package/native/runtime_promise.c +3378 -0
  45. package/native/runtime_property.c +815 -0
  46. package/native/runtime_proxy.c +1557 -0
  47. package/native/runtime_realm.c +435 -0
  48. package/native/runtime_reflect.c +648 -0
  49. package/native/runtime_regexp.c +3519 -0
  50. package/native/runtime_regexp_matcher.c +3559 -0
  51. package/native/runtime_regexp_symbol.c +1495 -0
  52. package/native/runtime_set.c +1775 -0
  53. package/native/runtime_string.c +2886 -0
  54. package/native/runtime_string_match.c +1185 -0
  55. package/native/runtime_symbol.c +1010 -0
  56. package/native/runtime_typed_array.c +5137 -0
  57. package/native/runtime_unicode_tables.h +14670 -0
  58. package/native/runtime_uri.c +700 -0
  59. package/native/runtime_weak_collection.c +1299 -0
  60. package/package.json +38 -3
@@ -0,0 +1,1495 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Oseo C runtime library.
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2026 Hong Minhee <https://hongminhee.org/>
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in
17
+ * all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+
28
+ #include "runtime_internal.h"
29
+
30
+ #include <math.h>
31
+ #include <string.h>
32
+
33
+ /*
34
+ * The RegExp.prototype well-known symbol methods of 22.2.6 and the
35
+ * `RegExp.escape` static of 22.2.5. Each method reads its operand through
36
+ * ordinary property access and executes through RegExpExec, so a program
37
+ * that overrides `exec`, `flags`, `lastIndex`, or `constructor` observes
38
+ * exactly what the specification says it observes. The pattern grammar,
39
+ * the matcher artifact, and built-in execution stay with
40
+ * runtime_regexp.c, and the String.prototype entry points that reach
41
+ * these methods stay with runtime_string_match.c.
42
+ */
43
+
44
+ #define OSEO_REGEXP_SYMBOL_MAX_SAFE_LENGTH 9007199254740991.0
45
+ #define OSEO_REGEXP_SYMBOL_MAX_UINT32 4294967295.0
46
+
47
+ static OseoValue symbol_argument(
48
+ size_t argument_count,
49
+ const OseoValue *arguments,
50
+ size_t index
51
+ ) {
52
+ return index < argument_count ? arguments[index] : oseo_undefined();
53
+ }
54
+
55
+ static OseoResult symbol_require_object(
56
+ OseoContext *context,
57
+ OseoValue receiver,
58
+ const char *message
59
+ ) {
60
+ if (is_object(receiver)) return normal(receiver);
61
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE, message);
62
+ }
63
+
64
+ /* Get(object, name) for one ASCII property name. */
65
+ static OseoResult symbol_get(
66
+ OseoContext *context,
67
+ OseoValue object,
68
+ const char *name,
69
+ OseoValue *value
70
+ ) {
71
+ OseoValue slots[2] = {object, oseo_undefined()};
72
+ OseoRootFrame frame = {NULL, slots, 2u};
73
+ oseo_roots_push(context, &frame);
74
+ OseoResult result = oseo_internal_ascii_string(context, name);
75
+ slots[1] = result.value;
76
+ if (result.status == OSEO_STATUS_NORMAL) {
77
+ result = oseo_object_get(context, slots[0], slots[1]);
78
+ slots[1] = result.value;
79
+ }
80
+ if (result.status == OSEO_STATUS_NORMAL) *value = slots[1];
81
+ oseo_roots_pop(context, &frame);
82
+ return result;
83
+ }
84
+
85
+ /* Get(object, ToString(index)) for one canonical numeric key. */
86
+ static OseoResult symbol_get_index(
87
+ OseoContext *context,
88
+ OseoValue object,
89
+ double index,
90
+ OseoValue *value
91
+ ) {
92
+ OseoValue slots[2] = {object, oseo_undefined()};
93
+ OseoRootFrame frame = {NULL, slots, 2u};
94
+ oseo_roots_push(context, &frame);
95
+ OseoResult result = oseo_internal_value_string(
96
+ context,
97
+ oseo_number(index)
98
+ );
99
+ slots[1] = result.value;
100
+ if (result.status == OSEO_STATUS_NORMAL) {
101
+ result = oseo_object_get(context, slots[0], slots[1]);
102
+ slots[1] = result.value;
103
+ }
104
+ if (result.status == OSEO_STATUS_NORMAL) *value = slots[1];
105
+ oseo_roots_pop(context, &frame);
106
+ return result;
107
+ }
108
+
109
+ /* Set(regexp, "lastIndex", value, true), which a read-only slot rejects. */
110
+ static OseoResult symbol_set_last_index(
111
+ OseoContext *context,
112
+ OseoValue regexp,
113
+ OseoValue value
114
+ ) {
115
+ OseoValue slots[3] = {regexp, value, oseo_undefined()};
116
+ OseoRootFrame frame = {NULL, slots, 3u};
117
+ oseo_roots_push(context, &frame);
118
+ OseoResult result = oseo_internal_ascii_string(context, "lastIndex");
119
+ slots[2] = result.value;
120
+ if (result.status == OSEO_STATUS_NORMAL) {
121
+ result = oseo_object_set(
122
+ context,
123
+ slots[0],
124
+ slots[2],
125
+ slots[1],
126
+ true
127
+ );
128
+ }
129
+ oseo_roots_pop(context, &frame);
130
+ return result;
131
+ }
132
+
133
+ /* ToLength, which clamps NaN and every negative value to zero. */
134
+ static OseoResult symbol_to_length(
135
+ OseoContext *context,
136
+ OseoValue value,
137
+ double *length
138
+ ) {
139
+ *length = 0.0;
140
+ OseoResult result = oseo_internal_to_number(context, value);
141
+ if (result.status != OSEO_STATUS_NORMAL) return result;
142
+ if (!is_number(result.value)) return normal(oseo_undefined());
143
+ double number = number_value(result.value);
144
+ if (!(number > 0.0)) return normal(oseo_undefined());
145
+ number = floor(number);
146
+ *length = number > OSEO_REGEXP_SYMBOL_MAX_SAFE_LENGTH
147
+ ? OSEO_REGEXP_SYMBOL_MAX_SAFE_LENGTH
148
+ : number;
149
+ return normal(oseo_undefined());
150
+ }
151
+
152
+ /* ToLength(Get(regexp, "lastIndex")). */
153
+ static OseoResult symbol_last_index(
154
+ OseoContext *context,
155
+ OseoValue regexp,
156
+ double *index
157
+ ) {
158
+ OseoValue value = oseo_undefined();
159
+ OseoValue slots[2] = {regexp, oseo_undefined()};
160
+ OseoRootFrame frame = {NULL, slots, 2u};
161
+ oseo_roots_push(context, &frame);
162
+ OseoResult result = symbol_get(context, slots[0], "lastIndex", &value);
163
+ slots[1] = value;
164
+ if (result.status == OSEO_STATUS_NORMAL) {
165
+ result = symbol_to_length(context, slots[1], index);
166
+ }
167
+ oseo_roots_pop(context, &frame);
168
+ return result;
169
+ }
170
+
171
+ /* ToString(Get(regexp, "flags")). */
172
+ static OseoResult symbol_flags(
173
+ OseoContext *context,
174
+ OseoValue regexp,
175
+ OseoValue *flags
176
+ ) {
177
+ OseoValue value = oseo_undefined();
178
+ OseoValue slots[2] = {regexp, oseo_undefined()};
179
+ OseoRootFrame frame = {NULL, slots, 2u};
180
+ oseo_roots_push(context, &frame);
181
+ OseoResult result = symbol_get(context, slots[0], "flags", &value);
182
+ slots[1] = value;
183
+ if (result.status == OSEO_STATUS_NORMAL) {
184
+ result = oseo_internal_value_string(context, slots[1]);
185
+ slots[1] = result.value;
186
+ }
187
+ if (result.status == OSEO_STATUS_NORMAL) *flags = slots[1];
188
+ oseo_roots_pop(context, &frame);
189
+ return result;
190
+ }
191
+
192
+ static bool symbol_flags_contain(OseoValue flags, uint16_t unit) {
193
+ const OseoString *string = string_object(flags);
194
+ for (size_t index = 0u; index < string->length; index += 1u) {
195
+ if (string->units[index] == unit) return true;
196
+ }
197
+ return false;
198
+ }
199
+
200
+ /* Whether `flags` selects code-point traversal, meaning `u` or `v`. */
201
+ static bool symbol_flags_unicode(OseoValue flags) {
202
+ return symbol_flags_contain(flags, UINT16_C(0x75)) ||
203
+ symbol_flags_contain(flags, UINT16_C(0x76));
204
+ }
205
+
206
+ /* AdvanceStringIndex (22.2.7.3). */
207
+ static double symbol_advance_string_index(
208
+ const OseoString *subject,
209
+ double index,
210
+ bool unicode
211
+ ) {
212
+ if (!unicode || index + 1.0 >= (double)subject->length) {
213
+ return index + 1.0;
214
+ }
215
+ size_t position = (size_t)index;
216
+ uint16_t first = subject->units[position];
217
+ if (first < UINT16_C(0xd800) || first > UINT16_C(0xdbff)) {
218
+ return index + 1.0;
219
+ }
220
+ uint16_t second = subject->units[position + 1u];
221
+ if (second < UINT16_C(0xdc00) || second > UINT16_C(0xdfff)) {
222
+ return index + 1.0;
223
+ }
224
+ return index + 2.0;
225
+ }
226
+
227
+ /* The half-open substring of one rooted string value. */
228
+ static OseoResult symbol_substring(
229
+ OseoContext *context,
230
+ OseoValue subject,
231
+ size_t start,
232
+ size_t end
233
+ ) {
234
+ const OseoString *string = string_object(subject);
235
+ return oseo_internal_allocate_string(
236
+ context,
237
+ string->units + start,
238
+ end - start
239
+ );
240
+ }
241
+
242
+ /* LengthOfArrayLike(object). */
243
+ static OseoResult symbol_length_of_array_like(
244
+ OseoContext *context,
245
+ OseoValue object,
246
+ double *length
247
+ ) {
248
+ OseoValue value = oseo_undefined();
249
+ OseoValue slots[2] = {object, oseo_undefined()};
250
+ OseoRootFrame frame = {NULL, slots, 2u};
251
+ oseo_roots_push(context, &frame);
252
+ OseoResult result = symbol_get(context, slots[0], "length", &value);
253
+ slots[1] = value;
254
+ if (result.status == OSEO_STATUS_NORMAL) {
255
+ result = symbol_to_length(context, slots[1], length);
256
+ }
257
+ oseo_roots_pop(context, &frame);
258
+ return result;
259
+ }
260
+
261
+ /* SpeciesConstructor(object, %RegExp%). */
262
+ static OseoResult symbol_species_constructor(
263
+ OseoContext *context,
264
+ OseoValue object
265
+ ) {
266
+ OseoRootFrame frame = {NULL, NULL, 0u};
267
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
268
+ if (result.status != OSEO_STATUS_NORMAL) return result;
269
+ frame.slots[0] = object;
270
+ result = symbol_get(
271
+ context,
272
+ frame.slots[0],
273
+ "constructor",
274
+ &frame.slots[1]
275
+ );
276
+ if (result.status == OSEO_STATUS_NORMAL &&
277
+ tag_of(frame.slots[1]) == OSEO_TAG_UNDEFINED) {
278
+ result = oseo_internal_intrinsic(context, OSEO_INTRINSIC_REGEXP);
279
+ oseo_roots_release(context, &frame);
280
+ return result;
281
+ }
282
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[1])) {
283
+ result = oseo_internal_throw_error(
284
+ context,
285
+ OSEO_ERROR_TYPE,
286
+ "The RegExp constructor property is not an object."
287
+ );
288
+ }
289
+ if (result.status == OSEO_STATUS_NORMAL) {
290
+ result = oseo_internal_well_known_symbol(
291
+ context,
292
+ OSEO_WELL_KNOWN_SPECIES
293
+ );
294
+ frame.slots[0] = result.value;
295
+ }
296
+ if (result.status == OSEO_STATUS_NORMAL) {
297
+ result = oseo_object_get(context, frame.slots[1], frame.slots[0]);
298
+ frame.slots[1] = result.value;
299
+ }
300
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(frame.slots[1])) {
301
+ result = oseo_internal_intrinsic(context, OSEO_INTRINSIC_REGEXP);
302
+ oseo_roots_release(context, &frame);
303
+ return result;
304
+ }
305
+ if (result.status == OSEO_STATUS_NORMAL) {
306
+ result = function_is_constructible(frame.slots[1])
307
+ ? normal(frame.slots[1])
308
+ : oseo_internal_throw_error(
309
+ context,
310
+ OSEO_ERROR_TYPE,
311
+ "The RegExp species is not a constructor."
312
+ );
313
+ }
314
+ oseo_roots_release(context, &frame);
315
+ return result;
316
+ }
317
+
318
+ /* Construct(constructor, «regexp, flags»). */
319
+ static OseoResult symbol_construct(
320
+ OseoContext *context,
321
+ OseoValue constructor,
322
+ OseoValue regexp,
323
+ OseoValue flags
324
+ ) {
325
+ OseoRootFrame frame = {NULL, NULL, 0u};
326
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
327
+ if (result.status != OSEO_STATUS_NORMAL) return result;
328
+ frame.slots[0] = constructor;
329
+ frame.slots[1] = regexp;
330
+ frame.slots[2] = flags;
331
+ result = oseo_function_prototype(context, frame.slots[0]);
332
+ if (result.status == OSEO_STATUS_NORMAL) {
333
+ result = oseo_internal_function_receiver(
334
+ context,
335
+ frame.slots[0],
336
+ result.value
337
+ );
338
+ frame.slots[3] = result.value;
339
+ }
340
+ if (result.status == OSEO_STATUS_NORMAL) {
341
+ OseoValue call_arguments[2] = {frame.slots[1], frame.slots[2]};
342
+ result = oseo_call_function(
343
+ context,
344
+ frame.slots[0],
345
+ frame.slots[3],
346
+ 2u,
347
+ call_arguments,
348
+ frame.slots[0]
349
+ );
350
+ }
351
+ if (result.status == OSEO_STATUS_NORMAL) {
352
+ result = oseo_constructor_result(
353
+ context,
354
+ result.value,
355
+ frame.slots[3]
356
+ );
357
+ }
358
+ oseo_roots_release(context, &frame);
359
+ return result;
360
+ }
361
+
362
+ OseoResult oseo_internal_regexp_iteration_step(
363
+ OseoContext *context,
364
+ OseoValue regexp,
365
+ OseoValue subject,
366
+ OseoValue match,
367
+ bool unicode
368
+ ) {
369
+ OseoValue slots[4] = {regexp, subject, match, oseo_undefined()};
370
+ OseoRootFrame frame = {NULL, slots, 4u};
371
+ oseo_roots_push(context, &frame);
372
+ OseoResult result = symbol_get_index(context, slots[2], 0.0, &slots[3]);
373
+ if (result.status == OSEO_STATUS_NORMAL) {
374
+ result = oseo_internal_value_string(context, slots[3]);
375
+ slots[3] = result.value;
376
+ }
377
+ if (result.status == OSEO_STATUS_NORMAL &&
378
+ string_object(slots[3])->length == 0u) {
379
+ double index = 0.0;
380
+ result = symbol_last_index(context, slots[0], &index);
381
+ if (result.status == OSEO_STATUS_NORMAL) {
382
+ double next = symbol_advance_string_index(
383
+ string_object(slots[1]),
384
+ index,
385
+ unicode
386
+ );
387
+ result = symbol_set_last_index(
388
+ context,
389
+ slots[0],
390
+ oseo_number(next)
391
+ );
392
+ }
393
+ }
394
+ oseo_roots_pop(context, &frame);
395
+ return result;
396
+ }
397
+
398
+ /* RegExp.prototype[Symbol.match] (22.2.6.8). */
399
+ static OseoResult regexp_symbol_match(
400
+ OseoContext *context,
401
+ OseoValue receiver,
402
+ size_t argument_count,
403
+ const OseoValue *arguments
404
+ ) {
405
+ OseoResult result = symbol_require_object(
406
+ context,
407
+ receiver,
408
+ "RegExp.prototype[Symbol.match] requires an object receiver."
409
+ );
410
+ if (result.status != OSEO_STATUS_NORMAL) return result;
411
+ OseoRootFrame frame = {NULL, NULL, 0u};
412
+ result = oseo_roots_allocate(context, &frame, 5u);
413
+ if (result.status != OSEO_STATUS_NORMAL) return result;
414
+ /* 0: rx, 1: S, 2: flags, 3: accumulated array, 4: one execution */
415
+ frame.slots[0] = receiver;
416
+ result = oseo_internal_value_string(
417
+ context,
418
+ symbol_argument(argument_count, arguments, 0u)
419
+ );
420
+ frame.slots[1] = result.value;
421
+ if (result.status == OSEO_STATUS_NORMAL) {
422
+ result = symbol_flags(context, frame.slots[0], &frame.slots[2]);
423
+ }
424
+ if (result.status == OSEO_STATUS_NORMAL &&
425
+ !symbol_flags_contain(frame.slots[2], UINT16_C(0x67))) {
426
+ result = oseo_internal_regexp_exec(
427
+ context,
428
+ frame.slots[0],
429
+ frame.slots[1]
430
+ );
431
+ oseo_roots_release(context, &frame);
432
+ return result;
433
+ }
434
+ bool unicode = result.status == OSEO_STATUS_NORMAL &&
435
+ symbol_flags_unicode(frame.slots[2]);
436
+ if (result.status == OSEO_STATUS_NORMAL) {
437
+ result = symbol_set_last_index(
438
+ context,
439
+ frame.slots[0],
440
+ oseo_number(0.0)
441
+ );
442
+ }
443
+ if (result.status == OSEO_STATUS_NORMAL) {
444
+ result = oseo_array_create(context, 0u);
445
+ frame.slots[3] = result.value;
446
+ }
447
+ size_t count = 0u;
448
+ while (result.status == OSEO_STATUS_NORMAL) {
449
+ result = oseo_internal_regexp_exec(
450
+ context,
451
+ frame.slots[0],
452
+ frame.slots[1]
453
+ );
454
+ frame.slots[4] = result.value;
455
+ if (result.status != OSEO_STATUS_NORMAL) break;
456
+ if (tag_of(frame.slots[4]) == OSEO_TAG_NULL) {
457
+ result = normal(count == 0u ? oseo_null() : frame.slots[3]);
458
+ break;
459
+ }
460
+ OseoValue matched = oseo_undefined();
461
+ result = symbol_get_index(context, frame.slots[4], 0.0, &matched);
462
+ frame.slots[4] = matched;
463
+ if (result.status == OSEO_STATUS_NORMAL) {
464
+ result = oseo_internal_value_string(context, frame.slots[4]);
465
+ frame.slots[4] = result.value;
466
+ }
467
+ if (result.status == OSEO_STATUS_NORMAL) {
468
+ result = oseo_array_append(
469
+ context,
470
+ frame.slots[3],
471
+ frame.slots[4]
472
+ );
473
+ }
474
+ if (result.status == OSEO_STATUS_NORMAL &&
475
+ string_object(frame.slots[4])->length == 0u) {
476
+ double index = 0.0;
477
+ result = symbol_last_index(context, frame.slots[0], &index);
478
+ if (result.status == OSEO_STATUS_NORMAL) {
479
+ double next = symbol_advance_string_index(
480
+ string_object(frame.slots[1]),
481
+ index,
482
+ unicode
483
+ );
484
+ result = symbol_set_last_index(
485
+ context,
486
+ frame.slots[0],
487
+ oseo_number(next)
488
+ );
489
+ }
490
+ }
491
+ count += 1u;
492
+ }
493
+ oseo_roots_release(context, &frame);
494
+ return result;
495
+ }
496
+
497
+ /* RegExp.prototype[Symbol.matchAll] (22.2.6.9). */
498
+ static OseoResult regexp_symbol_match_all(
499
+ OseoContext *context,
500
+ OseoValue receiver,
501
+ size_t argument_count,
502
+ const OseoValue *arguments
503
+ ) {
504
+ OseoResult result = symbol_require_object(
505
+ context,
506
+ receiver,
507
+ "RegExp.prototype[Symbol.matchAll] requires an object receiver."
508
+ );
509
+ if (result.status != OSEO_STATUS_NORMAL) return result;
510
+ OseoRootFrame frame = {NULL, NULL, 0u};
511
+ result = oseo_roots_allocate(context, &frame, 5u);
512
+ if (result.status != OSEO_STATUS_NORMAL) return result;
513
+ /* 0: R, 1: S, 2: species then flags, 3: flags, 4: matcher */
514
+ frame.slots[0] = receiver;
515
+ result = oseo_internal_value_string(
516
+ context,
517
+ symbol_argument(argument_count, arguments, 0u)
518
+ );
519
+ frame.slots[1] = result.value;
520
+ if (result.status == OSEO_STATUS_NORMAL) {
521
+ result = symbol_species_constructor(context, frame.slots[0]);
522
+ frame.slots[2] = result.value;
523
+ }
524
+ if (result.status == OSEO_STATUS_NORMAL) {
525
+ result = symbol_flags(context, frame.slots[0], &frame.slots[3]);
526
+ }
527
+ if (result.status == OSEO_STATUS_NORMAL) {
528
+ result = symbol_construct(
529
+ context,
530
+ frame.slots[2],
531
+ frame.slots[0],
532
+ frame.slots[3]
533
+ );
534
+ frame.slots[4] = result.value;
535
+ }
536
+ double index = 0.0;
537
+ if (result.status == OSEO_STATUS_NORMAL) {
538
+ result = symbol_last_index(context, frame.slots[0], &index);
539
+ }
540
+ if (result.status == OSEO_STATUS_NORMAL) {
541
+ result = symbol_set_last_index(
542
+ context,
543
+ frame.slots[4],
544
+ oseo_number(index)
545
+ );
546
+ }
547
+ if (result.status == OSEO_STATUS_NORMAL) {
548
+ result = oseo_internal_regexp_string_iterator_create(
549
+ context,
550
+ frame.slots[4],
551
+ frame.slots[1],
552
+ symbol_flags_contain(frame.slots[3], UINT16_C(0x67)),
553
+ symbol_flags_unicode(frame.slots[3])
554
+ );
555
+ }
556
+ oseo_roots_release(context, &frame);
557
+ return result;
558
+ }
559
+
560
+ /* RegExp.prototype[Symbol.search] (22.2.6.12). */
561
+ static OseoResult regexp_symbol_search(
562
+ OseoContext *context,
563
+ OseoValue receiver,
564
+ size_t argument_count,
565
+ const OseoValue *arguments
566
+ ) {
567
+ OseoResult result = symbol_require_object(
568
+ context,
569
+ receiver,
570
+ "RegExp.prototype[Symbol.search] requires an object receiver."
571
+ );
572
+ if (result.status != OSEO_STATUS_NORMAL) return result;
573
+ OseoRootFrame frame = {NULL, NULL, 0u};
574
+ result = oseo_roots_allocate(context, &frame, 5u);
575
+ if (result.status != OSEO_STATUS_NORMAL) return result;
576
+ /* 0: rx, 1: S, 2: previous lastIndex, 3: execution, 4: current */
577
+ frame.slots[0] = receiver;
578
+ result = oseo_internal_value_string(
579
+ context,
580
+ symbol_argument(argument_count, arguments, 0u)
581
+ );
582
+ frame.slots[1] = result.value;
583
+ if (result.status == OSEO_STATUS_NORMAL) {
584
+ result = symbol_get(
585
+ context,
586
+ frame.slots[0],
587
+ "lastIndex",
588
+ &frame.slots[2]
589
+ );
590
+ }
591
+ if (result.status == OSEO_STATUS_NORMAL &&
592
+ !oseo_internal_same_value(frame.slots[2], oseo_number(0.0))) {
593
+ result = symbol_set_last_index(
594
+ context,
595
+ frame.slots[0],
596
+ oseo_number(0.0)
597
+ );
598
+ }
599
+ if (result.status == OSEO_STATUS_NORMAL) {
600
+ result = oseo_internal_regexp_exec(
601
+ context,
602
+ frame.slots[0],
603
+ frame.slots[1]
604
+ );
605
+ frame.slots[3] = result.value;
606
+ }
607
+ if (result.status == OSEO_STATUS_NORMAL) {
608
+ result = symbol_get(
609
+ context,
610
+ frame.slots[0],
611
+ "lastIndex",
612
+ &frame.slots[4]
613
+ );
614
+ }
615
+ if (result.status == OSEO_STATUS_NORMAL &&
616
+ !oseo_internal_same_value(frame.slots[4], frame.slots[2])) {
617
+ result = symbol_set_last_index(
618
+ context,
619
+ frame.slots[0],
620
+ frame.slots[2]
621
+ );
622
+ }
623
+ if (result.status == OSEO_STATUS_NORMAL &&
624
+ tag_of(frame.slots[3]) == OSEO_TAG_NULL) {
625
+ result = normal(oseo_number(-1.0));
626
+ } else if (result.status == OSEO_STATUS_NORMAL) {
627
+ OseoValue index = oseo_undefined();
628
+ result = symbol_get(context, frame.slots[3], "index", &index);
629
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(index);
630
+ }
631
+ oseo_roots_release(context, &frame);
632
+ return result;
633
+ }
634
+
635
+ /* RegExp.prototype[Symbol.split] (22.2.6.14). */
636
+ static OseoResult regexp_symbol_split(
637
+ OseoContext *context,
638
+ OseoValue receiver,
639
+ size_t argument_count,
640
+ const OseoValue *arguments
641
+ ) {
642
+ OseoResult result = symbol_require_object(
643
+ context,
644
+ receiver,
645
+ "RegExp.prototype[Symbol.split] requires an object receiver."
646
+ );
647
+ if (result.status != OSEO_STATUS_NORMAL) return result;
648
+ OseoRootFrame frame = {NULL, NULL, 0u};
649
+ result = oseo_roots_allocate(context, &frame, 7u);
650
+ if (result.status != OSEO_STATUS_NORMAL) return result;
651
+ /*
652
+ * 0: rx, 1: S, 2: species then splitter, 3: flags, 4: A, 5: one
653
+ * execution, 6: the substring or capture being appended. Slot 6 is
654
+ * rooted because appending to an Array allocates its index key, which
655
+ * is a safepoint the value has to survive.
656
+ */
657
+ frame.slots[0] = receiver;
658
+ result = oseo_internal_value_string(
659
+ context,
660
+ symbol_argument(argument_count, arguments, 0u)
661
+ );
662
+ frame.slots[1] = result.value;
663
+ if (result.status == OSEO_STATUS_NORMAL) {
664
+ result = symbol_species_constructor(context, frame.slots[0]);
665
+ frame.slots[2] = result.value;
666
+ }
667
+ if (result.status == OSEO_STATUS_NORMAL) {
668
+ result = symbol_flags(context, frame.slots[0], &frame.slots[3]);
669
+ }
670
+ bool unicode = result.status == OSEO_STATUS_NORMAL &&
671
+ symbol_flags_unicode(frame.slots[3]);
672
+ if (result.status == OSEO_STATUS_NORMAL &&
673
+ !symbol_flags_contain(frame.slots[3], UINT16_C(0x79))) {
674
+ const OseoString *flags = string_object(frame.slots[3]);
675
+ OseoStringBuilder builder = {NULL, 0u, 0u};
676
+ result = oseo_internal_string_builder_append(
677
+ context,
678
+ &builder,
679
+ flags->units,
680
+ flags->length
681
+ );
682
+ if (result.status == OSEO_STATUS_NORMAL) {
683
+ uint16_t sticky = UINT16_C(0x79);
684
+ result = oseo_internal_string_builder_append(
685
+ context,
686
+ &builder,
687
+ &sticky,
688
+ 1u
689
+ );
690
+ }
691
+ if (result.status == OSEO_STATUS_NORMAL) {
692
+ result = oseo_internal_allocate_string(
693
+ context,
694
+ builder.units,
695
+ builder.length
696
+ );
697
+ frame.slots[3] = result.value;
698
+ }
699
+ oseo_internal_string_builder_release(&builder);
700
+ }
701
+ if (result.status == OSEO_STATUS_NORMAL) {
702
+ result = symbol_construct(
703
+ context,
704
+ frame.slots[2],
705
+ frame.slots[0],
706
+ frame.slots[3]
707
+ );
708
+ frame.slots[2] = result.value;
709
+ }
710
+ if (result.status == OSEO_STATUS_NORMAL) {
711
+ result = oseo_array_create(context, 0u);
712
+ frame.slots[4] = result.value;
713
+ }
714
+ double limit = OSEO_REGEXP_SYMBOL_MAX_UINT32;
715
+ OseoValue requested = symbol_argument(argument_count, arguments, 1u);
716
+ if (result.status == OSEO_STATUS_NORMAL &&
717
+ tag_of(requested) != OSEO_TAG_UNDEFINED) {
718
+ result = oseo_internal_to_number(context, requested);
719
+ if (result.status == OSEO_STATUS_NORMAL) {
720
+ double number = number_value(result.value);
721
+ if (!isfinite(number) || number == 0.0) {
722
+ limit = 0.0;
723
+ } else {
724
+ double modulo = fmod(trunc(number), 4294967296.0);
725
+ if (modulo < 0.0) modulo += 4294967296.0;
726
+ limit = modulo;
727
+ }
728
+ }
729
+ }
730
+ if (result.status != OSEO_STATUS_NORMAL || limit == 0.0) {
731
+ if (result.status == OSEO_STATUS_NORMAL) {
732
+ result = normal(frame.slots[4]);
733
+ }
734
+ oseo_roots_release(context, &frame);
735
+ return result;
736
+ }
737
+ size_t size = string_object(frame.slots[1])->length;
738
+ if (size == 0u) {
739
+ result = oseo_internal_regexp_exec(
740
+ context,
741
+ frame.slots[2],
742
+ frame.slots[1]
743
+ );
744
+ if (result.status == OSEO_STATUS_NORMAL &&
745
+ tag_of(result.value) == OSEO_TAG_NULL) {
746
+ result = oseo_array_append(
747
+ context,
748
+ frame.slots[4],
749
+ frame.slots[1]
750
+ );
751
+ }
752
+ if (result.status == OSEO_STATUS_NORMAL) {
753
+ result = normal(frame.slots[4]);
754
+ }
755
+ oseo_roots_release(context, &frame);
756
+ return result;
757
+ }
758
+ double start = 0.0;
759
+ double cursor = 0.0;
760
+ while (result.status == OSEO_STATUS_NORMAL && cursor < (double)size) {
761
+ result = symbol_set_last_index(
762
+ context,
763
+ frame.slots[2],
764
+ oseo_number(cursor)
765
+ );
766
+ if (result.status == OSEO_STATUS_NORMAL) {
767
+ result = oseo_internal_regexp_exec(
768
+ context,
769
+ frame.slots[2],
770
+ frame.slots[1]
771
+ );
772
+ frame.slots[5] = result.value;
773
+ }
774
+ if (result.status != OSEO_STATUS_NORMAL) break;
775
+ if (tag_of(frame.slots[5]) == OSEO_TAG_NULL) {
776
+ cursor = symbol_advance_string_index(
777
+ string_object(frame.slots[1]),
778
+ cursor,
779
+ unicode
780
+ );
781
+ continue;
782
+ }
783
+ double end = 0.0;
784
+ result = symbol_last_index(context, frame.slots[2], &end);
785
+ if (result.status != OSEO_STATUS_NORMAL) break;
786
+ if (end > (double)size) end = (double)size;
787
+ if (end == start) {
788
+ cursor = symbol_advance_string_index(
789
+ string_object(frame.slots[1]),
790
+ cursor,
791
+ unicode
792
+ );
793
+ continue;
794
+ }
795
+ result = symbol_substring(
796
+ context,
797
+ frame.slots[1],
798
+ (size_t)start,
799
+ (size_t)cursor
800
+ );
801
+ frame.slots[6] = result.value;
802
+ if (result.status == OSEO_STATUS_NORMAL) {
803
+ result = oseo_array_append(
804
+ context,
805
+ frame.slots[4],
806
+ frame.slots[6]
807
+ );
808
+ }
809
+ if (result.status != OSEO_STATUS_NORMAL) break;
810
+ if ((double)ordinary_object(frame.slots[4])->array_length == limit) {
811
+ result = normal(frame.slots[4]);
812
+ oseo_roots_release(context, &frame);
813
+ return result;
814
+ }
815
+ start = end;
816
+ double captures = 0.0;
817
+ result = symbol_length_of_array_like(
818
+ context,
819
+ frame.slots[5],
820
+ &captures
821
+ );
822
+ if (result.status != OSEO_STATUS_NORMAL) break;
823
+ captures = captures > 1.0 ? captures - 1.0 : 0.0;
824
+ bool truncated = false;
825
+ for (double index = 1.0;
826
+ result.status == OSEO_STATUS_NORMAL && index <= captures;
827
+ index += 1.0) {
828
+ result = symbol_get_index(
829
+ context,
830
+ frame.slots[5],
831
+ index,
832
+ &frame.slots[6]
833
+ );
834
+ if (result.status == OSEO_STATUS_NORMAL) {
835
+ result = oseo_array_append(
836
+ context,
837
+ frame.slots[4],
838
+ frame.slots[6]
839
+ );
840
+ }
841
+ if (result.status == OSEO_STATUS_NORMAL &&
842
+ (double)ordinary_object(frame.slots[4])->array_length ==
843
+ limit) {
844
+ truncated = true;
845
+ break;
846
+ }
847
+ }
848
+ if (result.status != OSEO_STATUS_NORMAL) break;
849
+ if (truncated) {
850
+ result = normal(frame.slots[4]);
851
+ oseo_roots_release(context, &frame);
852
+ return result;
853
+ }
854
+ cursor = start;
855
+ }
856
+ if (result.status == OSEO_STATUS_NORMAL) {
857
+ result = symbol_substring(
858
+ context,
859
+ frame.slots[1],
860
+ (size_t)start,
861
+ size
862
+ );
863
+ frame.slots[5] = result.value;
864
+ }
865
+ if (result.status == OSEO_STATUS_NORMAL) {
866
+ result = oseo_array_append(context, frame.slots[4], frame.slots[5]);
867
+ }
868
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(frame.slots[4]);
869
+ oseo_roots_release(context, &frame);
870
+ return result;
871
+ }
872
+
873
+ /*
874
+ * One iteration of the @@replace result loop (22.2.6.11 step 14),
875
+ * appending the replacement text into `builder`. It performs every read
876
+ * that step specifies in order, meaning `length`, `0`, `index`, each
877
+ * capture, and `groups`, so a match object built by user code observes
878
+ * exactly the specified sequence. A functional replacer receives the
879
+ * matched text, every capture, the position, the subject, and the
880
+ * named-capture object when the match declares one; a String replacer
881
+ * runs through GetSubstitution instead. `position` and `matched_length`
882
+ * report what the caller needs to stage the surrounding subject text.
883
+ */
884
+ static OseoResult regexp_replacement(
885
+ OseoContext *context,
886
+ OseoStringBuilder *builder,
887
+ OseoValue subject,
888
+ OseoValue match,
889
+ OseoValue replacer,
890
+ bool functional,
891
+ size_t *position,
892
+ size_t *matched_length
893
+ ) {
894
+ OseoRootFrame frame = {NULL, NULL, 0u};
895
+ OseoResult result = oseo_roots_allocate(context, &frame, 7u);
896
+ if (result.status != OSEO_STATUS_NORMAL) return result;
897
+ /*
898
+ * 0: S, 1: match, 2: replacer, 3: matched, 4: captures, 5: groups
899
+ * then the replacer's result, 6: the capture being converted and
900
+ * appended. Slot 6 is rooted because both the conversion and the
901
+ * append are safepoints.
902
+ */
903
+ frame.slots[0] = subject;
904
+ frame.slots[1] = match;
905
+ frame.slots[2] = replacer;
906
+ double length = 0.0;
907
+ result = symbol_length_of_array_like(context, frame.slots[1], &length);
908
+ double count = length > 1.0 ? length - 1.0 : 0.0;
909
+ if (result.status == OSEO_STATUS_NORMAL) {
910
+ result = symbol_get_index(
911
+ context,
912
+ frame.slots[1],
913
+ 0.0,
914
+ &frame.slots[3]
915
+ );
916
+ }
917
+ if (result.status == OSEO_STATUS_NORMAL) {
918
+ result = oseo_internal_value_string(context, frame.slots[3]);
919
+ frame.slots[3] = result.value;
920
+ }
921
+ if (result.status == OSEO_STATUS_NORMAL) {
922
+ *matched_length = string_object(frame.slots[3])->length;
923
+ result = symbol_get(
924
+ context,
925
+ frame.slots[1],
926
+ "index",
927
+ &frame.slots[6]
928
+ );
929
+ }
930
+ if (result.status == OSEO_STATUS_NORMAL) {
931
+ result = oseo_internal_to_number(context, frame.slots[6]);
932
+ }
933
+ if (result.status == OSEO_STATUS_NORMAL) {
934
+ double number = number_value(result.value);
935
+ number = isnan(number) ? 0.0
936
+ : isfinite(number) ? trunc(number)
937
+ : number;
938
+ double limit = (double)string_object(frame.slots[0])->length;
939
+ *position = (size_t)(
940
+ number < 0.0 ? 0.0 : number > limit ? limit : number
941
+ );
942
+ frame.slots[6] = oseo_undefined();
943
+ }
944
+ if (result.status == OSEO_STATUS_NORMAL) {
945
+ result = oseo_array_create(context, 0u);
946
+ frame.slots[4] = result.value;
947
+ }
948
+ for (double index = 1.0;
949
+ result.status == OSEO_STATUS_NORMAL && index <= count;
950
+ index += 1.0) {
951
+ result = symbol_get_index(
952
+ context,
953
+ frame.slots[1],
954
+ index,
955
+ &frame.slots[6]
956
+ );
957
+ if (result.status == OSEO_STATUS_NORMAL &&
958
+ tag_of(frame.slots[6]) != OSEO_TAG_UNDEFINED) {
959
+ result = oseo_internal_value_string(context, frame.slots[6]);
960
+ frame.slots[6] = result.value;
961
+ }
962
+ if (result.status == OSEO_STATUS_NORMAL) {
963
+ result = oseo_array_append(
964
+ context,
965
+ frame.slots[4],
966
+ frame.slots[6]
967
+ );
968
+ }
969
+ }
970
+ frame.slots[6] = oseo_undefined();
971
+ if (result.status == OSEO_STATUS_NORMAL) {
972
+ result = symbol_get(
973
+ context,
974
+ frame.slots[1],
975
+ "groups",
976
+ &frame.slots[5]
977
+ );
978
+ }
979
+ if (result.status == OSEO_STATUS_NORMAL && functional) {
980
+ size_t named = tag_of(frame.slots[5]) == OSEO_TAG_UNDEFINED ? 0u : 1u;
981
+ size_t total = (size_t)count + 3u + named;
982
+ OseoRootFrame call = {NULL, NULL, 0u};
983
+ result = oseo_roots_allocate(context, &call, total);
984
+ if (result.status == OSEO_STATUS_NORMAL) {
985
+ call.slots[0] = frame.slots[3];
986
+ for (size_t index = 0u;
987
+ result.status == OSEO_STATUS_NORMAL &&
988
+ index < (size_t)count;
989
+ index += 1u) {
990
+ result = symbol_get_index(
991
+ context,
992
+ frame.slots[4],
993
+ (double)index,
994
+ &call.slots[index + 1u]
995
+ );
996
+ }
997
+ call.slots[(size_t)count + 1u] =
998
+ oseo_number((double)*position);
999
+ call.slots[(size_t)count + 2u] = frame.slots[0];
1000
+ if (named == 1u) {
1001
+ call.slots[(size_t)count + 3u] = frame.slots[5];
1002
+ }
1003
+ if (result.status == OSEO_STATUS_NORMAL) {
1004
+ result = oseo_call_function(
1005
+ context,
1006
+ frame.slots[2],
1007
+ oseo_undefined(),
1008
+ total,
1009
+ call.slots,
1010
+ oseo_undefined()
1011
+ );
1012
+ }
1013
+ oseo_roots_release(context, &call);
1014
+ }
1015
+ frame.slots[5] = result.value;
1016
+ if (result.status == OSEO_STATUS_NORMAL) {
1017
+ result = oseo_internal_value_string(context, frame.slots[5]);
1018
+ frame.slots[5] = result.value;
1019
+ }
1020
+ if (result.status == OSEO_STATUS_NORMAL) {
1021
+ const OseoString *text = string_object(frame.slots[5]);
1022
+ result = oseo_internal_string_builder_append(
1023
+ context,
1024
+ builder,
1025
+ text->units,
1026
+ text->length
1027
+ );
1028
+ }
1029
+ oseo_roots_release(context, &frame);
1030
+ return result;
1031
+ }
1032
+ if (result.status == OSEO_STATUS_NORMAL &&
1033
+ tag_of(frame.slots[5]) != OSEO_TAG_UNDEFINED) {
1034
+ result = oseo_internal_to_object(context, frame.slots[5]);
1035
+ frame.slots[5] = result.value;
1036
+ }
1037
+ if (result.status == OSEO_STATUS_NORMAL) {
1038
+ result = oseo_internal_get_substitution(
1039
+ context,
1040
+ builder,
1041
+ frame.slots[3],
1042
+ frame.slots[0],
1043
+ *position,
1044
+ frame.slots[4],
1045
+ frame.slots[5],
1046
+ frame.slots[2]
1047
+ );
1048
+ }
1049
+ oseo_roots_release(context, &frame);
1050
+ return result;
1051
+ }
1052
+
1053
+ /* RegExp.prototype[Symbol.replace] (22.2.6.11). */
1054
+ static OseoResult regexp_symbol_replace(
1055
+ OseoContext *context,
1056
+ OseoValue receiver,
1057
+ size_t argument_count,
1058
+ const OseoValue *arguments
1059
+ ) {
1060
+ OseoResult result = symbol_require_object(
1061
+ context,
1062
+ receiver,
1063
+ "RegExp.prototype[Symbol.replace] requires an object receiver."
1064
+ );
1065
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1066
+ OseoRootFrame frame = {NULL, NULL, 0u};
1067
+ result = oseo_roots_allocate(context, &frame, 7u);
1068
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1069
+ /*
1070
+ * 0: rx, 1: S, 2: replacer, 3: flags, 4: results, 5: one execution,
1071
+ * 6: the `index` and `0` reads of that execution, which are rooted
1072
+ * because their conversions are safepoints.
1073
+ */
1074
+ frame.slots[0] = receiver;
1075
+ frame.slots[2] = symbol_argument(argument_count, arguments, 1u);
1076
+ result = oseo_internal_value_string(
1077
+ context,
1078
+ symbol_argument(argument_count, arguments, 0u)
1079
+ );
1080
+ frame.slots[1] = result.value;
1081
+ bool functional = is_callable(frame.slots[2]);
1082
+ if (result.status == OSEO_STATUS_NORMAL && !functional) {
1083
+ result = oseo_internal_value_string(context, frame.slots[2]);
1084
+ frame.slots[2] = result.value;
1085
+ }
1086
+ if (result.status == OSEO_STATUS_NORMAL) {
1087
+ result = symbol_flags(context, frame.slots[0], &frame.slots[3]);
1088
+ }
1089
+ bool global = result.status == OSEO_STATUS_NORMAL &&
1090
+ symbol_flags_contain(frame.slots[3], UINT16_C(0x67));
1091
+ bool unicode = global && symbol_flags_unicode(frame.slots[3]);
1092
+ if (result.status == OSEO_STATUS_NORMAL && global) {
1093
+ result = symbol_set_last_index(
1094
+ context,
1095
+ frame.slots[0],
1096
+ oseo_number(0.0)
1097
+ );
1098
+ }
1099
+ if (result.status == OSEO_STATUS_NORMAL) {
1100
+ result = oseo_array_create(context, 0u);
1101
+ frame.slots[4] = result.value;
1102
+ }
1103
+ bool done = false;
1104
+ while (result.status == OSEO_STATUS_NORMAL && !done) {
1105
+ result = oseo_internal_regexp_exec(
1106
+ context,
1107
+ frame.slots[0],
1108
+ frame.slots[1]
1109
+ );
1110
+ frame.slots[5] = result.value;
1111
+ if (result.status != OSEO_STATUS_NORMAL) break;
1112
+ if (tag_of(frame.slots[5]) == OSEO_TAG_NULL) {
1113
+ done = true;
1114
+ break;
1115
+ }
1116
+ result = oseo_array_append(context, frame.slots[4], frame.slots[5]);
1117
+ if (result.status != OSEO_STATUS_NORMAL) break;
1118
+ if (!global) {
1119
+ done = true;
1120
+ break;
1121
+ }
1122
+ result = oseo_internal_regexp_iteration_step(
1123
+ context,
1124
+ frame.slots[0],
1125
+ frame.slots[1],
1126
+ frame.slots[5],
1127
+ unicode
1128
+ );
1129
+ }
1130
+ OseoStringBuilder builder = {NULL, 0u, 0u};
1131
+ size_t consumed = 0u;
1132
+ size_t total = result.status == OSEO_STATUS_NORMAL
1133
+ ? ordinary_object(frame.slots[4])->array_length
1134
+ : 0u;
1135
+ for (size_t index = 0u;
1136
+ result.status == OSEO_STATUS_NORMAL && index < total;
1137
+ index += 1u) {
1138
+ result = symbol_get_index(
1139
+ context,
1140
+ frame.slots[4],
1141
+ (double)index,
1142
+ &frame.slots[5]
1143
+ );
1144
+ /*
1145
+ * The replacement is computed before the preceding subject slice
1146
+ * is staged, because the specification concatenates only after
1147
+ * the replacer has run.
1148
+ */
1149
+ size_t position = 0u;
1150
+ size_t matched_length = 0u;
1151
+ OseoStringBuilder piece = {NULL, 0u, 0u};
1152
+ if (result.status == OSEO_STATUS_NORMAL) {
1153
+ result = regexp_replacement(
1154
+ context,
1155
+ &piece,
1156
+ frame.slots[1],
1157
+ frame.slots[5],
1158
+ frame.slots[2],
1159
+ functional,
1160
+ &position,
1161
+ &matched_length
1162
+ );
1163
+ }
1164
+ if (result.status == OSEO_STATUS_NORMAL && position >= consumed) {
1165
+ const OseoString *subject = string_object(frame.slots[1]);
1166
+ result = oseo_internal_string_builder_append(
1167
+ context,
1168
+ &builder,
1169
+ subject->units + consumed,
1170
+ position - consumed
1171
+ );
1172
+ if (result.status == OSEO_STATUS_NORMAL) {
1173
+ result = oseo_internal_string_builder_append(
1174
+ context,
1175
+ &builder,
1176
+ piece.units,
1177
+ piece.length
1178
+ );
1179
+ }
1180
+ /*
1181
+ * The next source position keeps the unclamped sum. A match
1182
+ * object built by user code may report a `0` longer than what
1183
+ * remains of the subject, and the specification then ignores
1184
+ * every later match that starts before that sum, including one
1185
+ * at the subject's own end.
1186
+ */
1187
+ consumed = position + matched_length;
1188
+ }
1189
+ oseo_internal_string_builder_release(&piece);
1190
+ }
1191
+ if (result.status == OSEO_STATUS_NORMAL) {
1192
+ const OseoString *subject = string_object(frame.slots[1]);
1193
+ if (consumed < subject->length) {
1194
+ result = oseo_internal_string_builder_append(
1195
+ context,
1196
+ &builder,
1197
+ subject->units + consumed,
1198
+ subject->length - consumed
1199
+ );
1200
+ }
1201
+ }
1202
+ if (result.status == OSEO_STATUS_NORMAL) {
1203
+ result = oseo_internal_allocate_string(
1204
+ context,
1205
+ builder.units,
1206
+ builder.length
1207
+ );
1208
+ }
1209
+ oseo_internal_string_builder_release(&builder);
1210
+ oseo_roots_release(context, &frame);
1211
+ return result;
1212
+ }
1213
+
1214
+ /* The SyntaxCharacter production plus U+002F, which always escape. */
1215
+ static bool escape_syntax_character(uint32_t point) {
1216
+ switch (point) {
1217
+ case 0x005eu:
1218
+ case 0x0024u:
1219
+ case 0x005cu:
1220
+ case 0x002eu:
1221
+ case 0x002au:
1222
+ case 0x002bu:
1223
+ case 0x003fu:
1224
+ case 0x0028u:
1225
+ case 0x0029u:
1226
+ case 0x005bu:
1227
+ case 0x005du:
1228
+ case 0x007bu:
1229
+ case 0x007du:
1230
+ case 0x007cu:
1231
+ case 0x002fu:
1232
+ return true;
1233
+ default:
1234
+ return false;
1235
+ }
1236
+ }
1237
+
1238
+ /* The ControlEscape letter for one code point, or zero for anything else. */
1239
+ static uint16_t escape_control_letter(uint32_t point) {
1240
+ switch (point) {
1241
+ case 0x0009u:
1242
+ return UINT16_C(0x74);
1243
+ case 0x000au:
1244
+ return UINT16_C(0x6e);
1245
+ case 0x000bu:
1246
+ return UINT16_C(0x76);
1247
+ case 0x000cu:
1248
+ return UINT16_C(0x66);
1249
+ case 0x000du:
1250
+ return UINT16_C(0x72);
1251
+ default:
1252
+ return 0u;
1253
+ }
1254
+ }
1255
+
1256
+ /*
1257
+ * The punctuators EncodeForRegExpEscape hex-escapes so that the result
1258
+ * stays inert wherever it is spliced, plus WhiteSpace, LineTerminator,
1259
+ * and the surrogate range.
1260
+ */
1261
+ static bool escape_hex_required(uint32_t point) {
1262
+ switch (point) {
1263
+ case 0x002cu:
1264
+ case 0x002du:
1265
+ case 0x003du:
1266
+ case 0x003cu:
1267
+ case 0x003eu:
1268
+ case 0x0023u:
1269
+ case 0x0026u:
1270
+ case 0x0021u:
1271
+ case 0x0025u:
1272
+ case 0x003au:
1273
+ case 0x003bu:
1274
+ case 0x0040u:
1275
+ case 0x007eu:
1276
+ case 0x0027u:
1277
+ case 0x0060u:
1278
+ case 0x0022u:
1279
+ return true;
1280
+ default:
1281
+ break;
1282
+ }
1283
+ if (point >= 0xd800u && point <= 0xdfffu) return true;
1284
+ return point == 0x0009u || point == 0x000au || point == 0x000bu ||
1285
+ point == 0x000cu || point == 0x000du || point == 0x0020u ||
1286
+ point == 0x00a0u || point == 0x1680u ||
1287
+ (point >= 0x2000u && point <= 0x200au) ||
1288
+ point == 0x2028u || point == 0x2029u || point == 0x202fu ||
1289
+ point == 0x205fu || point == 0x3000u || point == 0xfeffu;
1290
+ }
1291
+
1292
+ static uint16_t escape_hex_digit(uint32_t value) {
1293
+ return value < 10u
1294
+ ? (uint16_t)(UINT16_C(0x30) + value)
1295
+ : (uint16_t)(UINT16_C(0x61) + value - 10u);
1296
+ }
1297
+
1298
+ /* `\xNN` for a code point at or below U+00FF. */
1299
+ static OseoResult escape_hex_pair(
1300
+ OseoContext *context,
1301
+ OseoStringBuilder *builder,
1302
+ uint32_t point
1303
+ ) {
1304
+ uint16_t units[4] = {
1305
+ UINT16_C(0x5c),
1306
+ UINT16_C(0x78),
1307
+ escape_hex_digit((point >> 4u) & 0xfu),
1308
+ escape_hex_digit(point & 0xfu),
1309
+ };
1310
+ return oseo_internal_string_builder_append(context, builder, units, 4u);
1311
+ }
1312
+
1313
+ /* UnicodeEscape of one code unit, meaning `\uNNNN` in lowercase hex. */
1314
+ static OseoResult escape_unicode(
1315
+ OseoContext *context,
1316
+ OseoStringBuilder *builder,
1317
+ uint16_t unit
1318
+ ) {
1319
+ uint16_t units[6] = {
1320
+ UINT16_C(0x5c),
1321
+ UINT16_C(0x75),
1322
+ escape_hex_digit((uint32_t)(unit >> 12u) & 0xfu),
1323
+ escape_hex_digit((uint32_t)(unit >> 8u) & 0xfu),
1324
+ escape_hex_digit((uint32_t)(unit >> 4u) & 0xfu),
1325
+ escape_hex_digit((uint32_t)unit & 0xfu),
1326
+ };
1327
+ return oseo_internal_string_builder_append(context, builder, units, 6u);
1328
+ }
1329
+
1330
+ /* EncodeForRegExpEscape (22.2.5.2.1). */
1331
+ static OseoResult escape_code_point(
1332
+ OseoContext *context,
1333
+ OseoStringBuilder *builder,
1334
+ uint32_t point,
1335
+ const uint16_t *units,
1336
+ size_t length
1337
+ ) {
1338
+ if (escape_syntax_character(point)) {
1339
+ /* Every SyntaxCharacter and the solidus are one BMP code unit. */
1340
+ uint16_t escaped[2] = {UINT16_C(0x5c), units[0]};
1341
+ (void)length;
1342
+ return oseo_internal_string_builder_append(
1343
+ context,
1344
+ builder,
1345
+ escaped,
1346
+ 2u
1347
+ );
1348
+ }
1349
+ uint16_t letter = escape_control_letter(point);
1350
+ if (letter != 0u) {
1351
+ uint16_t escaped[2] = {UINT16_C(0x5c), letter};
1352
+ return oseo_internal_string_builder_append(
1353
+ context,
1354
+ builder,
1355
+ escaped,
1356
+ 2u
1357
+ );
1358
+ }
1359
+ if (!escape_hex_required(point)) {
1360
+ return oseo_internal_string_builder_append(
1361
+ context,
1362
+ builder,
1363
+ units,
1364
+ length
1365
+ );
1366
+ }
1367
+ if (point <= 0xffu) return escape_hex_pair(context, builder, point);
1368
+ OseoResult result = normal(oseo_undefined());
1369
+ for (size_t index = 0u;
1370
+ result.status == OSEO_STATUS_NORMAL && index < length;
1371
+ index += 1u) {
1372
+ result = escape_unicode(context, builder, units[index]);
1373
+ }
1374
+ return result;
1375
+ }
1376
+
1377
+ /*
1378
+ * RegExp.escape (22.2.5.2). The first code point is hex-escaped whenever
1379
+ * it is a decimal digit or an ASCII letter, so the result can never begin
1380
+ * a `\cX` or decimal escape when it is spliced after a backslash.
1381
+ */
1382
+ static OseoResult regexp_escape(
1383
+ OseoContext *context,
1384
+ size_t argument_count,
1385
+ const OseoValue *arguments
1386
+ ) {
1387
+ OseoValue value = symbol_argument(argument_count, arguments, 0u);
1388
+ if (!is_string(value)) {
1389
+ return oseo_internal_throw_error(
1390
+ context,
1391
+ OSEO_ERROR_TYPE,
1392
+ "RegExp.escape requires a String argument."
1393
+ );
1394
+ }
1395
+ OseoValue slots[1] = {value};
1396
+ OseoRootFrame frame = {NULL, slots, 1u};
1397
+ oseo_roots_push(context, &frame);
1398
+ OseoStringBuilder builder = {NULL, 0u, 0u};
1399
+ OseoResult result = normal(oseo_undefined());
1400
+ size_t index = 0u;
1401
+ while (result.status == OSEO_STATUS_NORMAL) {
1402
+ const OseoString *source = string_object(slots[0]);
1403
+ if (index >= source->length) break;
1404
+ uint16_t first = source->units[index];
1405
+ size_t length = 1u;
1406
+ uint32_t point = first;
1407
+ if (first >= UINT16_C(0xd800) && first <= UINT16_C(0xdbff) &&
1408
+ index + 1u < source->length &&
1409
+ source->units[index + 1u] >= UINT16_C(0xdc00) &&
1410
+ source->units[index + 1u] <= UINT16_C(0xdfff)) {
1411
+ length = 2u;
1412
+ point = 0x10000u +
1413
+ (((uint32_t)first - 0xd800u) << 10u) +
1414
+ ((uint32_t)source->units[index + 1u] - 0xdc00u);
1415
+ }
1416
+ bool alphanumeric = (point >= 0x30u && point <= 0x39u) ||
1417
+ (point >= 0x41u && point <= 0x5au) ||
1418
+ (point >= 0x61u && point <= 0x7au);
1419
+ if (index == 0u && alphanumeric) {
1420
+ result = escape_hex_pair(context, &builder, point);
1421
+ } else {
1422
+ result = escape_code_point(
1423
+ context,
1424
+ &builder,
1425
+ point,
1426
+ source->units + index,
1427
+ length
1428
+ );
1429
+ }
1430
+ index += length;
1431
+ }
1432
+ if (result.status == OSEO_STATUS_NORMAL) {
1433
+ result = oseo_internal_allocate_string(
1434
+ context,
1435
+ builder.units,
1436
+ builder.length
1437
+ );
1438
+ }
1439
+ oseo_internal_string_builder_release(&builder);
1440
+ oseo_roots_pop(context, &frame);
1441
+ return result;
1442
+ }
1443
+
1444
+ OseoResult oseo_internal_regexp_symbol_dispatch(
1445
+ OseoContext *context,
1446
+ size_t code_id,
1447
+ OseoValue receiver,
1448
+ size_t argument_count,
1449
+ const OseoValue *arguments
1450
+ ) {
1451
+ if (code_id == OSEO_REGEXP_MATCH_CODE_ID) {
1452
+ return regexp_symbol_match(
1453
+ context,
1454
+ receiver,
1455
+ argument_count,
1456
+ arguments
1457
+ );
1458
+ }
1459
+ if (code_id == OSEO_REGEXP_MATCH_ALL_CODE_ID) {
1460
+ return regexp_symbol_match_all(
1461
+ context,
1462
+ receiver,
1463
+ argument_count,
1464
+ arguments
1465
+ );
1466
+ }
1467
+ if (code_id == OSEO_REGEXP_SEARCH_CODE_ID) {
1468
+ return regexp_symbol_search(
1469
+ context,
1470
+ receiver,
1471
+ argument_count,
1472
+ arguments
1473
+ );
1474
+ }
1475
+ if (code_id == OSEO_REGEXP_SPLIT_CODE_ID) {
1476
+ return regexp_symbol_split(
1477
+ context,
1478
+ receiver,
1479
+ argument_count,
1480
+ arguments
1481
+ );
1482
+ }
1483
+ if (code_id == OSEO_REGEXP_REPLACE_CODE_ID) {
1484
+ return regexp_symbol_replace(
1485
+ context,
1486
+ receiver,
1487
+ argument_count,
1488
+ arguments
1489
+ );
1490
+ }
1491
+ if (code_id == OSEO_REGEXP_ESCAPE_CODE_ID) {
1492
+ return regexp_escape(context, argument_count, arguments);
1493
+ }
1494
+ return oseo_unknown_function(context, code_id);
1495
+ }