@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,1161 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <math.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+
10
+ /*
11
+ * String.prototype methods whose first operand may dispatch through a
12
+ * well-known symbol. String-only fallbacks stay separate from the core
13
+ * String value and wrapper implementation so neighboring M5b nodes do not
14
+ * need to edit the same algorithm body.
15
+ */
16
+
17
+ static OseoValue match_argument(
18
+ size_t argument_count,
19
+ const OseoValue *arguments,
20
+ size_t index
21
+ ) {
22
+ return index < argument_count ? arguments[index] : oseo_undefined();
23
+ }
24
+
25
+ static OseoResult match_subject(
26
+ OseoContext *context,
27
+ OseoValue receiver
28
+ ) {
29
+ if (is_nullish(receiver)) {
30
+ return oseo_internal_throw_error(
31
+ context,
32
+ OSEO_ERROR_TYPE,
33
+ "String method receiver is nullish."
34
+ );
35
+ }
36
+ return oseo_internal_value_string(context, receiver);
37
+ }
38
+
39
+ static bool match_matches_at(
40
+ const OseoString *subject,
41
+ const OseoString *search,
42
+ size_t position
43
+ ) {
44
+ if (position > subject->length ||
45
+ search->length > subject->length - position) return false;
46
+ return search->length == 0u ||
47
+ memcmp(
48
+ subject->units + position,
49
+ search->units,
50
+ search->length * sizeof(uint16_t)
51
+ ) == 0;
52
+ }
53
+
54
+ static OseoResult match_is_regexp(
55
+ OseoContext *context,
56
+ OseoValue value,
57
+ bool *regexp
58
+ ) {
59
+ return oseo_internal_is_regexp(context, value, regexp);
60
+ }
61
+
62
+ static bool string_find(
63
+ OseoValue subject_value,
64
+ OseoValue search_value,
65
+ size_t start,
66
+ size_t *position
67
+ ) {
68
+ const OseoString *subject = string_object(subject_value);
69
+ const OseoString *search = string_object(search_value);
70
+ if (start > subject->length || search->length > subject->length) {
71
+ return false;
72
+ }
73
+ size_t last = subject->length - search->length;
74
+ for (size_t index = start; index <= last; index += 1u) {
75
+ if (match_matches_at(subject, search, index)) {
76
+ *position = index;
77
+ return true;
78
+ }
79
+ }
80
+ return false;
81
+ }
82
+
83
+ static size_t string_symbol_index(size_t code_id) {
84
+ if (code_id == OSEO_STRING_MATCH_CODE_ID) return OSEO_WELL_KNOWN_MATCH;
85
+ if (code_id == OSEO_STRING_MATCH_ALL_CODE_ID) {
86
+ return OSEO_WELL_KNOWN_MATCH_ALL;
87
+ }
88
+ if (code_id == OSEO_STRING_SEARCH_CODE_ID) return OSEO_WELL_KNOWN_SEARCH;
89
+ if (code_id == OSEO_STRING_REPLACE_CODE_ID ||
90
+ code_id == OSEO_STRING_REPLACE_ALL_CODE_ID) {
91
+ return OSEO_WELL_KNOWN_REPLACE;
92
+ }
93
+ return OSEO_WELL_KNOWN_SPLIT;
94
+ }
95
+
96
+ /* The two methods whose operand must name a global RegExp when it is one. */
97
+ static bool string_requires_global_flags(size_t code_id) {
98
+ return code_id == OSEO_STRING_MATCH_ALL_CODE_ID ||
99
+ code_id == OSEO_STRING_REPLACE_ALL_CODE_ID;
100
+ }
101
+
102
+ /* The methods that forward a second argument to their symbol method. */
103
+ static bool string_forwards_second_argument(size_t code_id) {
104
+ return code_id == OSEO_STRING_SPLIT_CODE_ID ||
105
+ code_id == OSEO_STRING_REPLACE_CODE_ID ||
106
+ code_id == OSEO_STRING_REPLACE_ALL_CODE_ID;
107
+ }
108
+
109
+ /*
110
+ * The `flags` observation String.prototype.matchAll and replaceAll share
111
+ * once IsRegExp reports true for their operand: the value must be
112
+ * object-coercible and its String form must contain `g`.
113
+ */
114
+ static OseoResult string_require_global_flags(
115
+ OseoContext *context,
116
+ size_t code_id,
117
+ OseoValue regexp
118
+ ) {
119
+ bool match_all = code_id == OSEO_STRING_MATCH_ALL_CODE_ID;
120
+ OseoValue slots[3] = {regexp, oseo_undefined(), oseo_undefined()};
121
+ OseoRootFrame frame = {NULL, slots, 3u};
122
+ oseo_roots_push(context, &frame);
123
+ OseoResult result = oseo_internal_ascii_string(context, "flags");
124
+ slots[1] = result.value;
125
+ if (result.status == OSEO_STATUS_NORMAL) {
126
+ result = oseo_object_get(context, slots[0], slots[1]);
127
+ slots[2] = result.value;
128
+ }
129
+ if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[2])) {
130
+ result = oseo_internal_throw_error(
131
+ context,
132
+ OSEO_ERROR_TYPE,
133
+ match_all
134
+ ? "String.prototype.matchAll flags are nullish."
135
+ : "String.prototype.replaceAll flags are nullish."
136
+ );
137
+ }
138
+ if (result.status == OSEO_STATUS_NORMAL) {
139
+ result = oseo_internal_value_string(context, slots[2]);
140
+ slots[2] = result.value;
141
+ }
142
+ bool global = false;
143
+ if (result.status == OSEO_STATUS_NORMAL) {
144
+ const OseoString *flags = string_object(slots[2]);
145
+ for (size_t index = 0u; index < flags->length; index += 1u) {
146
+ if (flags->units[index] == UINT16_C(0x67)) {
147
+ global = true;
148
+ break;
149
+ }
150
+ }
151
+ if (!global) {
152
+ result = oseo_internal_throw_error(
153
+ context,
154
+ OSEO_ERROR_TYPE,
155
+ match_all
156
+ ? "String.prototype.matchAll requires a global RegExp."
157
+ : "String.prototype.replaceAll requires a global RegExp."
158
+ );
159
+ }
160
+ }
161
+ oseo_roots_pop(context, &frame);
162
+ return result;
163
+ }
164
+
165
+ static OseoResult string_protocol_method(
166
+ OseoContext *context,
167
+ size_t code_id,
168
+ OseoValue receiver,
169
+ size_t argument_count,
170
+ const OseoValue *arguments,
171
+ bool *dispatched
172
+ ) {
173
+ *dispatched = false;
174
+ if (is_nullish(receiver)) {
175
+ return oseo_internal_throw_error(
176
+ context,
177
+ OSEO_ERROR_TYPE,
178
+ "String method receiver is nullish."
179
+ );
180
+ }
181
+ OseoRootFrame frame = {NULL, NULL, 0u};
182
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
183
+ if (result.status != OSEO_STATUS_NORMAL) return result;
184
+ frame.slots[0] = receiver;
185
+ frame.slots[1] = match_argument(
186
+ argument_count,
187
+ arguments,
188
+ 0u
189
+ );
190
+ frame.slots[2] = match_argument(
191
+ argument_count,
192
+ arguments,
193
+ 1u
194
+ );
195
+ frame.slots[3] = oseo_undefined();
196
+ bool regexp = false;
197
+ /*
198
+ * Every one of these methods observes its operand only when the operand
199
+ * is an Object, so a primitive never reaches IsRegExp, the `flags` read,
200
+ * or the symbol lookup.
201
+ */
202
+ if (string_requires_global_flags(code_id) && is_object(frame.slots[1])) {
203
+ result = match_is_regexp(context, frame.slots[1], &regexp);
204
+ if (result.status == OSEO_STATUS_NORMAL && regexp) {
205
+ result = string_require_global_flags(
206
+ context,
207
+ code_id,
208
+ frame.slots[1]
209
+ );
210
+ }
211
+ }
212
+ if (result.status == OSEO_STATUS_NORMAL &&
213
+ is_object(frame.slots[1])) {
214
+ result = oseo_internal_well_known_symbol(
215
+ context,
216
+ string_symbol_index(code_id)
217
+ );
218
+ frame.slots[3] = result.value;
219
+ }
220
+ if (result.status == OSEO_STATUS_NORMAL &&
221
+ is_object(frame.slots[1])) {
222
+ result = oseo_object_get(
223
+ context,
224
+ frame.slots[1],
225
+ frame.slots[3]
226
+ );
227
+ frame.slots[3] = result.value;
228
+ }
229
+ if (result.status == OSEO_STATUS_NORMAL &&
230
+ !is_nullish(frame.slots[3])) {
231
+ if (!is_callable(frame.slots[3])) {
232
+ result = oseo_internal_throw_error(
233
+ context,
234
+ OSEO_ERROR_TYPE,
235
+ "String symbol protocol method is not callable."
236
+ );
237
+ } else {
238
+ size_t call_count =
239
+ string_forwards_second_argument(code_id) ? 2u : 1u;
240
+ OseoValue call_arguments[2] = {
241
+ frame.slots[0],
242
+ frame.slots[2],
243
+ };
244
+ result = oseo_call_function(
245
+ context,
246
+ frame.slots[3],
247
+ frame.slots[1],
248
+ call_count,
249
+ call_arguments,
250
+ oseo_undefined()
251
+ );
252
+ *dispatched = true;
253
+ }
254
+ }
255
+ oseo_roots_release(context, &frame);
256
+ return result;
257
+ }
258
+
259
+ /*
260
+ * The shared tail of String.prototype match, matchAll, and search once
261
+ * GetMethod found no symbol method on the operand: RegExpCreate over the
262
+ * raw operand, then Invoke of the same well-known symbol on the created
263
+ * RegExp. The created object is an ordinary %RegExp.prototype% instance,
264
+ * so the invoked method is the built-in one unless the program replaced
265
+ * it, which is exactly what the specification observes here.
266
+ */
267
+ static OseoResult string_regexp_dispatch(
268
+ OseoContext *context,
269
+ size_t code_id,
270
+ OseoValue receiver,
271
+ OseoValue pattern,
272
+ const char *flags
273
+ ) {
274
+ OseoRootFrame frame = {NULL, NULL, 0u};
275
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
276
+ if (result.status != OSEO_STATUS_NORMAL) return result;
277
+ /* 0: subject, 1: pattern, 2: created RegExp, 3: symbol then method */
278
+ frame.slots[1] = pattern;
279
+ result = match_subject(context, receiver);
280
+ frame.slots[0] = result.value;
281
+ if (result.status == OSEO_STATUS_NORMAL && flags != NULL) {
282
+ result = oseo_internal_ascii_string(context, flags);
283
+ frame.slots[2] = result.value;
284
+ }
285
+ if (result.status == OSEO_STATUS_NORMAL) {
286
+ result = oseo_internal_regexp_create(
287
+ context,
288
+ frame.slots[1],
289
+ flags == NULL ? oseo_undefined() : frame.slots[2]
290
+ );
291
+ frame.slots[2] = result.value;
292
+ }
293
+ if (result.status == OSEO_STATUS_NORMAL) {
294
+ result = oseo_internal_well_known_symbol(
295
+ context,
296
+ string_symbol_index(code_id)
297
+ );
298
+ frame.slots[3] = result.value;
299
+ }
300
+ if (result.status == OSEO_STATUS_NORMAL) {
301
+ result = oseo_object_get(context, frame.slots[2], frame.slots[3]);
302
+ frame.slots[3] = result.value;
303
+ }
304
+ if (result.status == OSEO_STATUS_NORMAL && !is_callable(frame.slots[3])) {
305
+ result = oseo_internal_throw_error(
306
+ context,
307
+ OSEO_ERROR_TYPE,
308
+ "String symbol protocol method is not callable."
309
+ );
310
+ }
311
+ if (result.status == OSEO_STATUS_NORMAL) {
312
+ result = oseo_call_function(
313
+ context,
314
+ frame.slots[3],
315
+ frame.slots[2],
316
+ 1u,
317
+ &frame.slots[0],
318
+ oseo_undefined()
319
+ );
320
+ }
321
+ oseo_roots_release(context, &frame);
322
+ return result;
323
+ }
324
+
325
+ /*
326
+ * String.prototype.match (22.1.3.13), matchAll (22.1.3.14), and search
327
+ * (22.1.3.16). Each observes an Object operand through IsRegExp where the
328
+ * specification says so and through GetMethod of its own well-known
329
+ * symbol; a nullish method reaches RegExpCreate, where matchAll alone
330
+ * supplies the `g` flag.
331
+ */
332
+ static OseoResult string_match_or_search(
333
+ OseoContext *context,
334
+ size_t code_id,
335
+ OseoValue receiver,
336
+ size_t argument_count,
337
+ const OseoValue *arguments
338
+ ) {
339
+ bool dispatched = false;
340
+ OseoResult result = string_protocol_method(
341
+ context,
342
+ code_id,
343
+ receiver,
344
+ argument_count,
345
+ arguments,
346
+ &dispatched
347
+ );
348
+ if (result.status != OSEO_STATUS_NORMAL || dispatched) return result;
349
+ return string_regexp_dispatch(
350
+ context,
351
+ code_id,
352
+ receiver,
353
+ match_argument(argument_count, arguments, 0u),
354
+ code_id == OSEO_STRING_MATCH_ALL_CODE_ID ? "g" : NULL
355
+ );
356
+ }
357
+
358
+ static OseoResult regexp_iterator_function(
359
+ OseoContext *context,
360
+ size_t code_id,
361
+ const char *name,
362
+ size_t length
363
+ ) {
364
+ size_t name_length = strlen(name);
365
+ uint16_t units[8];
366
+ if (name_length > sizeof(units) / sizeof(*units)) {
367
+ return failure(context, "OSEO2001", "Built-in name is too long.");
368
+ }
369
+ for (size_t index = 0u; index < name_length; index += 1u) {
370
+ units[index] = (uint16_t)(unsigned char)name[index];
371
+ }
372
+ OseoValue environment = oseo_undefined();
373
+ OseoRootFrame frame = {NULL, &environment, 1u};
374
+ oseo_roots_push(context, &frame);
375
+ OseoResult result = oseo_environment_create(context, 0u);
376
+ environment = result.value;
377
+ if (result.status == OSEO_STATUS_NORMAL) {
378
+ result = oseo_function_create(
379
+ context,
380
+ code_id,
381
+ environment,
382
+ units,
383
+ name_length,
384
+ length,
385
+ OSEO_FUNCTION_INTERNAL,
386
+ oseo_undefined(),
387
+ oseo_undefined(),
388
+ OSEO_FUNCTION_NAME_PREFIX_NONE
389
+ );
390
+ }
391
+ oseo_roots_pop(context, &frame);
392
+ return result;
393
+ }
394
+
395
+ static OseoResult regexp_iterator_property(
396
+ OseoContext *context,
397
+ OseoValue object,
398
+ OseoValue key,
399
+ OseoValue value,
400
+ OseoPropertyAttributes attributes
401
+ ) {
402
+ OseoValue slots[3] = {object, key, value};
403
+ OseoRootFrame frame = {NULL, slots, 3u};
404
+ oseo_roots_push(context, &frame);
405
+ OseoResult result = oseo_object_define(
406
+ context,
407
+ slots[0],
408
+ slots[1],
409
+ slots[2],
410
+ attributes
411
+ );
412
+ oseo_roots_pop(context, &frame);
413
+ return result;
414
+ }
415
+
416
+ OseoResult oseo_internal_regexp_string_iterator_prototype(
417
+ OseoContext *context
418
+ ) {
419
+ OseoValue *prototype_cache = &context->realm->intrinsics[
420
+ OSEO_INTRINSIC_REGEXP_STRING_ITERATOR_PROTOTYPE
421
+ ];
422
+ OseoValue *next_cache = &context->realm->intrinsics[
423
+ OSEO_INTRINSIC_REGEXP_STRING_ITERATOR_NEXT
424
+ ];
425
+ if (is_object(*prototype_cache) && is_function(*next_cache)) {
426
+ return normal(*prototype_cache);
427
+ }
428
+ size_t entry_allocations = context->allocations;
429
+ OseoRootFrame frame = {NULL, NULL, 0u};
430
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
431
+ if (result.status != OSEO_STATUS_NORMAL) return result;
432
+ result = oseo_internal_intrinsic(
433
+ context,
434
+ OSEO_INTRINSIC_ITERATOR_PROTOTYPE
435
+ );
436
+ frame.slots[0] = result.value;
437
+ if (result.status == OSEO_STATUS_NORMAL) {
438
+ result = oseo_object_create(context, frame.slots[0]);
439
+ frame.slots[1] = result.value;
440
+ }
441
+ if (result.status == OSEO_STATUS_NORMAL) {
442
+ result = regexp_iterator_function(
443
+ context,
444
+ OSEO_REGEXP_STRING_ITERATOR_NEXT_CODE_ID,
445
+ "next",
446
+ 0u
447
+ );
448
+ frame.slots[2] = result.value;
449
+ }
450
+ if (result.status == OSEO_STATUS_NORMAL) {
451
+ result = oseo_internal_ascii_string(context, "next");
452
+ frame.slots[3] = result.value;
453
+ }
454
+ if (result.status == OSEO_STATUS_NORMAL) {
455
+ result = regexp_iterator_property(
456
+ context,
457
+ frame.slots[1],
458
+ frame.slots[3],
459
+ frame.slots[2],
460
+ (OseoPropertyAttributes){true, false, true, false}
461
+ );
462
+ }
463
+ if (result.status == OSEO_STATUS_NORMAL) {
464
+ result = oseo_internal_well_known_symbol(
465
+ context,
466
+ OSEO_WELL_KNOWN_TO_STRING_TAG
467
+ );
468
+ frame.slots[4] = result.value;
469
+ }
470
+ if (result.status == OSEO_STATUS_NORMAL) {
471
+ result = oseo_internal_ascii_string(
472
+ context,
473
+ "RegExp String Iterator"
474
+ );
475
+ frame.slots[5] = result.value;
476
+ }
477
+ if (result.status == OSEO_STATUS_NORMAL) {
478
+ result = regexp_iterator_property(
479
+ context,
480
+ frame.slots[1],
481
+ frame.slots[4],
482
+ frame.slots[5],
483
+ (OseoPropertyAttributes){true, false, false, false}
484
+ );
485
+ }
486
+ if (result.status == OSEO_STATUS_NORMAL) {
487
+ *prototype_cache = frame.slots[1];
488
+ *next_cache = frame.slots[2];
489
+ result = normal(frame.slots[1]);
490
+ if (context->observe_specialization) {
491
+ context->allocations = entry_allocations;
492
+ }
493
+ } else {
494
+ *prototype_cache = oseo_undefined();
495
+ *next_cache = oseo_undefined();
496
+ }
497
+ oseo_roots_release(context, &frame);
498
+ return result;
499
+ }
500
+
501
+ OseoResult oseo_internal_regexp_string_iterator_create(
502
+ OseoContext *context,
503
+ OseoValue regexp,
504
+ OseoValue subject,
505
+ bool global,
506
+ bool unicode
507
+ ) {
508
+ OseoValue slots[3] = {regexp, subject, oseo_undefined()};
509
+ OseoRootFrame frame = {NULL, slots, 3u};
510
+ oseo_roots_push(context, &frame);
511
+ OseoResult result = oseo_internal_regexp_string_iterator_prototype(
512
+ context
513
+ );
514
+ slots[2] = result.value;
515
+ if (result.status == OSEO_STATUS_NORMAL) {
516
+ result = oseo_object_create(context, slots[2]);
517
+ }
518
+ if (result.status == OSEO_STATUS_NORMAL) {
519
+ OseoOrdinaryObject *iterator = ordinary_object(result.value);
520
+ iterator->regexp_string_iterator = true;
521
+ iterator->regexp_iterator_regexp = slots[0];
522
+ iterator->regexp_iterator_subject = slots[1];
523
+ iterator->regexp_iterator_global = global;
524
+ iterator->regexp_iterator_unicode = unicode;
525
+ iterator->regexp_iterator_complete = false;
526
+ }
527
+ oseo_roots_pop(context, &frame);
528
+ return result;
529
+ }
530
+
531
+ /*
532
+ * %RegExpStringIteratorPrototype%.next (22.2.9.2.1). A non-global
533
+ * iterator finishes on its first match, and a global one that matched the
534
+ * empty string advances `lastIndex` on the iterating RegExp itself, which
535
+ * is where the cursor lives.
536
+ */
537
+ static OseoResult regexp_string_iterator_next(
538
+ OseoContext *context,
539
+ OseoValue receiver
540
+ ) {
541
+ if (!is_regexp_string_iterator(receiver)) {
542
+ return oseo_internal_throw_error(
543
+ context,
544
+ OSEO_ERROR_TYPE,
545
+ "RegExp String iterator next requires its iterator receiver."
546
+ );
547
+ }
548
+ OseoOrdinaryObject *state = ordinary_object(receiver);
549
+ if (state->regexp_iterator_complete) {
550
+ return oseo_internal_iterator_result(
551
+ context,
552
+ oseo_undefined(),
553
+ true
554
+ );
555
+ }
556
+ bool global = state->regexp_iterator_global;
557
+ bool unicode = state->regexp_iterator_unicode;
558
+ OseoValue slots[4] = {
559
+ receiver,
560
+ state->regexp_iterator_regexp,
561
+ state->regexp_iterator_subject,
562
+ oseo_undefined(),
563
+ };
564
+ OseoRootFrame frame = {NULL, slots, 4u};
565
+ oseo_roots_push(context, &frame);
566
+ OseoResult result = oseo_internal_regexp_exec(
567
+ context,
568
+ slots[1],
569
+ slots[2]
570
+ );
571
+ slots[3] = result.value;
572
+ bool finished = result.status == OSEO_STATUS_NORMAL &&
573
+ tag_of(slots[3]) == OSEO_TAG_NULL;
574
+ if (finished || (result.status == OSEO_STATUS_NORMAL && !global)) {
575
+ state = ordinary_object(slots[0]);
576
+ state->regexp_iterator_complete = true;
577
+ state->regexp_iterator_regexp = oseo_undefined();
578
+ state->regexp_iterator_subject = oseo_undefined();
579
+ }
580
+ if (result.status == OSEO_STATUS_NORMAL && !finished && global) {
581
+ result = oseo_internal_regexp_iteration_step(
582
+ context,
583
+ slots[1],
584
+ slots[2],
585
+ slots[3],
586
+ unicode
587
+ );
588
+ }
589
+ if (result.status == OSEO_STATUS_NORMAL) {
590
+ result = oseo_internal_iterator_result(
591
+ context,
592
+ finished ? oseo_undefined() : slots[3],
593
+ finished
594
+ );
595
+ }
596
+ oseo_roots_pop(context, &frame);
597
+ return result;
598
+ }
599
+
600
+ static uint32_t string_to_uint32(double number) {
601
+ if (!isfinite(number) || number == 0.0) return 0u;
602
+ double modulo = fmod(trunc(number), 4294967296.0);
603
+ if (modulo < 0.0) modulo += 4294967296.0;
604
+ return (uint32_t)modulo;
605
+ }
606
+
607
+ static OseoResult string_append_slice(
608
+ OseoContext *context,
609
+ OseoValue array,
610
+ OseoValue subject,
611
+ size_t start,
612
+ size_t end
613
+ ) {
614
+ OseoValue slots[3] = {array, subject, oseo_undefined()};
615
+ OseoRootFrame frame = {NULL, slots, 3u};
616
+ oseo_roots_push(context, &frame);
617
+ const OseoString *string = string_object(slots[1]);
618
+ OseoResult result = oseo_internal_allocate_string(
619
+ context,
620
+ string->units + start,
621
+ end - start
622
+ );
623
+ slots[2] = result.value;
624
+ if (result.status == OSEO_STATUS_NORMAL) {
625
+ result = oseo_array_append(context, slots[0], slots[2]);
626
+ }
627
+ oseo_roots_pop(context, &frame);
628
+ return result;
629
+ }
630
+
631
+ static OseoResult string_split(
632
+ OseoContext *context,
633
+ OseoValue receiver,
634
+ size_t argument_count,
635
+ const OseoValue *arguments
636
+ ) {
637
+ bool dispatched = false;
638
+ OseoResult result = string_protocol_method(
639
+ context,
640
+ OSEO_STRING_SPLIT_CODE_ID,
641
+ receiver,
642
+ argument_count,
643
+ arguments,
644
+ &dispatched
645
+ );
646
+ if (result.status != OSEO_STATUS_NORMAL || dispatched) return result;
647
+ OseoRootFrame frame = {NULL, NULL, 0u};
648
+ result = oseo_roots_allocate(context, &frame, 4u);
649
+ if (result.status != OSEO_STATUS_NORMAL) return result;
650
+ frame.slots[0] = match_argument(argument_count, arguments, 0u);
651
+ frame.slots[1] = match_argument(argument_count, arguments, 1u);
652
+ result = match_subject(context, receiver);
653
+ frame.slots[2] = result.value;
654
+ if (result.status == OSEO_STATUS_NORMAL) {
655
+ result = oseo_array_create(context, 0u);
656
+ frame.slots[3] = result.value;
657
+ }
658
+ uint32_t limit = UINT32_MAX;
659
+ bool separator_undefined =
660
+ tag_of(frame.slots[0]) == OSEO_TAG_UNDEFINED;
661
+ if (result.status == OSEO_STATUS_NORMAL &&
662
+ tag_of(frame.slots[1]) != OSEO_TAG_UNDEFINED) {
663
+ result = oseo_internal_to_number(context, frame.slots[1]);
664
+ if (result.status == OSEO_STATUS_NORMAL) {
665
+ limit = string_to_uint32(number_value(result.value));
666
+ }
667
+ }
668
+ if (result.status == OSEO_STATUS_NORMAL && !separator_undefined) {
669
+ result = oseo_internal_value_string(context, frame.slots[0]);
670
+ frame.slots[0] = result.value;
671
+ }
672
+ if (result.status == OSEO_STATUS_NORMAL && limit == 0u) {
673
+ result = normal(frame.slots[3]);
674
+ } else if (result.status == OSEO_STATUS_NORMAL &&
675
+ separator_undefined) {
676
+ result = oseo_array_append(
677
+ context,
678
+ frame.slots[3],
679
+ frame.slots[2]
680
+ );
681
+ }
682
+ if (result.status == OSEO_STATUS_NORMAL &&
683
+ !separator_undefined && limit != 0u) {
684
+ size_t subject_length = string_object(frame.slots[2])->length;
685
+ size_t separator_length = string_object(frame.slots[0])->length;
686
+ if (subject_length == 0u) {
687
+ if (separator_length != 0u) {
688
+ result = string_append_slice(
689
+ context,
690
+ frame.slots[3],
691
+ frame.slots[2],
692
+ 0u,
693
+ 0u
694
+ );
695
+ }
696
+ } else if (separator_length == 0u) {
697
+ for (size_t index = 0u;
698
+ result.status == OSEO_STATUS_NORMAL &&
699
+ index < subject_length && index < (size_t)limit;
700
+ index += 1u) {
701
+ result = string_append_slice(
702
+ context,
703
+ frame.slots[3],
704
+ frame.slots[2],
705
+ index,
706
+ index + 1u
707
+ );
708
+ }
709
+ } else {
710
+ size_t start = 0u;
711
+ while (result.status == OSEO_STATUS_NORMAL &&
712
+ ordinary_object(frame.slots[3])->array_length < limit) {
713
+ size_t position = 0u;
714
+ if (!string_find(
715
+ frame.slots[2],
716
+ frame.slots[0],
717
+ start,
718
+ &position
719
+ )) break;
720
+ result = string_append_slice(
721
+ context,
722
+ frame.slots[3],
723
+ frame.slots[2],
724
+ start,
725
+ position
726
+ );
727
+ start = position + separator_length;
728
+ }
729
+ if (result.status == OSEO_STATUS_NORMAL &&
730
+ ordinary_object(frame.slots[3])->array_length < limit) {
731
+ result = string_append_slice(
732
+ context,
733
+ frame.slots[3],
734
+ frame.slots[2],
735
+ start,
736
+ subject_length
737
+ );
738
+ }
739
+ }
740
+ }
741
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[3];
742
+ oseo_roots_release(context, &frame);
743
+ return result;
744
+ }
745
+
746
+
747
+ /* The decimal value of one ASCII digit, or `UINT16_MAX` for anything else. */
748
+ static uint16_t substitution_digit(uint16_t unit) {
749
+ return unit >= UINT16_C(0x30) && unit <= UINT16_C(0x39)
750
+ ? (uint16_t)(unit - UINT16_C(0x30))
751
+ : UINT16_MAX;
752
+ }
753
+
754
+ /*
755
+ * The `$n`, `$nn`, and `$<name>` reference forms of GetSubstitution.
756
+ * `reference` receives how many code units the reference spans, and
757
+ * `capture` receives the substituted text or `oseo_uninitialized()` when
758
+ * the reference has no referent and copies its own text instead.
759
+ */
760
+ static OseoResult substitution_reference(
761
+ OseoContext *context,
762
+ OseoValue replacement,
763
+ size_t index,
764
+ OseoValue captures,
765
+ OseoValue named_captures,
766
+ size_t *reference,
767
+ OseoValue *capture
768
+ ) {
769
+ const OseoString *pattern = string_object(replacement);
770
+ uint16_t next = pattern->units[index + 1u];
771
+ *capture = oseo_uninitialized();
772
+ if (next == UINT16_C(0x3c)) {
773
+ *reference = 2u;
774
+ if (tag_of(named_captures) == OSEO_TAG_UNDEFINED) {
775
+ return normal(oseo_undefined());
776
+ }
777
+ size_t close = index + 2u;
778
+ while (close < pattern->length &&
779
+ pattern->units[close] != UINT16_C(0x3e)) {
780
+ close += 1u;
781
+ }
782
+ if (close == pattern->length) return normal(oseo_undefined());
783
+ *reference = close + 1u - index;
784
+ OseoValue slots[3] = {replacement, named_captures, oseo_undefined()};
785
+ OseoRootFrame frame = {NULL, slots, 3u};
786
+ oseo_roots_push(context, &frame);
787
+ OseoResult result = oseo_internal_allocate_string(
788
+ context,
789
+ string_object(slots[0])->units + index + 2u,
790
+ close - index - 2u
791
+ );
792
+ slots[2] = result.value;
793
+ if (result.status == OSEO_STATUS_NORMAL) {
794
+ result = oseo_object_get(context, slots[1], slots[2]);
795
+ slots[2] = result.value;
796
+ }
797
+ if (result.status == OSEO_STATUS_NORMAL &&
798
+ tag_of(slots[2]) == OSEO_TAG_UNDEFINED) {
799
+ result = oseo_internal_allocate_string(context, NULL, 0u);
800
+ slots[2] = result.value;
801
+ } else if (result.status == OSEO_STATUS_NORMAL) {
802
+ result = oseo_internal_value_string(context, slots[2]);
803
+ slots[2] = result.value;
804
+ }
805
+ if (result.status == OSEO_STATUS_NORMAL) *capture = slots[2];
806
+ oseo_roots_pop(context, &frame);
807
+ return result;
808
+ }
809
+ uint16_t first = substitution_digit(next);
810
+ if (first == UINT16_MAX) {
811
+ *reference = 1u;
812
+ return normal(oseo_undefined());
813
+ }
814
+ uint16_t second = index + 2u < pattern->length
815
+ ? substitution_digit(pattern->units[index + 2u])
816
+ : UINT16_MAX;
817
+ size_t capture_count = tag_of(captures) == OSEO_TAG_UNDEFINED
818
+ ? 0u
819
+ : ordinary_object(captures)->array_length;
820
+ size_t digits = second == UINT16_MAX ? 1u : 2u;
821
+ size_t number = digits == 1u
822
+ ? (size_t)first
823
+ : (size_t)first * 10u + (size_t)second;
824
+ /*
825
+ * A two-digit reference past the capture count is one digit followed
826
+ * by a literal digit, so the second digit is left for the scan that
827
+ * resumes after this reference.
828
+ */
829
+ if (number > capture_count && digits == 2u) {
830
+ digits = 1u;
831
+ number = (size_t)first;
832
+ }
833
+ *reference = digits + 1u;
834
+ if (number < 1u || number > capture_count) {
835
+ return normal(oseo_undefined());
836
+ }
837
+ OseoValue slots[2] = {captures, oseo_undefined()};
838
+ OseoRootFrame frame = {NULL, slots, 2u};
839
+ oseo_roots_push(context, &frame);
840
+ OseoResult result = oseo_internal_value_string(
841
+ context,
842
+ oseo_number((double)(number - 1u))
843
+ );
844
+ slots[1] = result.value;
845
+ if (result.status == OSEO_STATUS_NORMAL) {
846
+ result = oseo_object_get(context, slots[0], slots[1]);
847
+ slots[1] = result.value;
848
+ }
849
+ if (result.status == OSEO_STATUS_NORMAL &&
850
+ tag_of(slots[1]) == OSEO_TAG_UNDEFINED) {
851
+ result = oseo_internal_allocate_string(context, NULL, 0u);
852
+ slots[1] = result.value;
853
+ }
854
+ if (result.status == OSEO_STATUS_NORMAL) *capture = slots[1];
855
+ oseo_roots_pop(context, &frame);
856
+ return result;
857
+ }
858
+
859
+ OseoResult oseo_internal_get_substitution(
860
+ OseoContext *context,
861
+ OseoStringBuilder *builder,
862
+ OseoValue matched,
863
+ OseoValue subject,
864
+ size_t position,
865
+ OseoValue captures,
866
+ OseoValue named_captures,
867
+ OseoValue replacement
868
+ ) {
869
+ OseoValue slots[6] = {
870
+ matched,
871
+ subject,
872
+ captures,
873
+ named_captures,
874
+ replacement,
875
+ oseo_undefined(),
876
+ };
877
+ OseoRootFrame frame = {NULL, slots, 6u};
878
+ oseo_roots_push(context, &frame);
879
+ OseoResult result = normal(oseo_undefined());
880
+ size_t index = 0u;
881
+ while (result.status == OSEO_STATUS_NORMAL) {
882
+ const OseoString *pattern = string_object(slots[4]);
883
+ if (index >= pattern->length) break;
884
+ uint16_t unit = pattern->units[index];
885
+ if (unit != UINT16_C(0x24) || index + 1u >= pattern->length) {
886
+ result = oseo_internal_string_builder_append(
887
+ context,
888
+ builder,
889
+ &unit,
890
+ 1u
891
+ );
892
+ index += 1u;
893
+ continue;
894
+ }
895
+ uint16_t next = pattern->units[index + 1u];
896
+ if (next == UINT16_C(0x24)) {
897
+ uint16_t dollar = UINT16_C(0x24);
898
+ result = oseo_internal_string_builder_append(
899
+ context,
900
+ builder,
901
+ &dollar,
902
+ 1u
903
+ );
904
+ index += 2u;
905
+ continue;
906
+ }
907
+ if (next == UINT16_C(0x26)) {
908
+ const OseoString *match = string_object(slots[0]);
909
+ result = oseo_internal_string_builder_append(
910
+ context,
911
+ builder,
912
+ match->units,
913
+ match->length
914
+ );
915
+ index += 2u;
916
+ continue;
917
+ }
918
+ if (next == UINT16_C(0x60)) {
919
+ const OseoString *string = string_object(slots[1]);
920
+ result = oseo_internal_string_builder_append(
921
+ context,
922
+ builder,
923
+ string->units,
924
+ position
925
+ );
926
+ index += 2u;
927
+ continue;
928
+ }
929
+ if (next == UINT16_C(0x27)) {
930
+ const OseoString *string = string_object(slots[1]);
931
+ size_t tail = position + string_object(slots[0])->length;
932
+ if (tail > string->length) tail = string->length;
933
+ result = oseo_internal_string_builder_append(
934
+ context,
935
+ builder,
936
+ string->units + tail,
937
+ string->length - tail
938
+ );
939
+ index += 2u;
940
+ continue;
941
+ }
942
+ size_t reference = 1u;
943
+ result = substitution_reference(
944
+ context,
945
+ slots[4],
946
+ index,
947
+ slots[2],
948
+ slots[3],
949
+ &reference,
950
+ &slots[5]
951
+ );
952
+ if (result.status != OSEO_STATUS_NORMAL) break;
953
+ if (tag_of(slots[5]) == OSEO_TAG_UNINITIALIZED) {
954
+ /* The reference has no referent, so it copies its own text. */
955
+ result = oseo_internal_string_builder_append(
956
+ context,
957
+ builder,
958
+ string_object(slots[4])->units + index,
959
+ reference
960
+ );
961
+ } else {
962
+ const OseoString *text = string_object(slots[5]);
963
+ result = oseo_internal_string_builder_append(
964
+ context,
965
+ builder,
966
+ text->units,
967
+ text->length
968
+ );
969
+ slots[5] = oseo_undefined();
970
+ }
971
+ index += reference;
972
+ }
973
+ oseo_roots_pop(context, &frame);
974
+ return result;
975
+ }
976
+
977
+ /*
978
+ * String.prototype.replace (22.1.3.19) and replaceAll (22.1.3.20). The two
979
+ * share one body: `replace` stops after the first match, while `replaceAll`
980
+ * continues from each match end and advances by one code unit for an empty
981
+ * search string. Neither converts a callable replacer, and a String
982
+ * replacer is converted before the subject is searched.
983
+ */
984
+ static OseoResult string_replace(
985
+ OseoContext *context,
986
+ size_t code_id,
987
+ OseoValue receiver,
988
+ size_t argument_count,
989
+ const OseoValue *arguments
990
+ ) {
991
+ bool all = code_id == OSEO_STRING_REPLACE_ALL_CODE_ID;
992
+ bool dispatched = false;
993
+ OseoResult result = string_protocol_method(
994
+ context,
995
+ code_id,
996
+ receiver,
997
+ argument_count,
998
+ arguments,
999
+ &dispatched
1000
+ );
1001
+ if (result.status != OSEO_STATUS_NORMAL || dispatched) return result;
1002
+ OseoRootFrame frame = {NULL, NULL, 0u};
1003
+ result = oseo_roots_allocate(context, &frame, 4u);
1004
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1005
+ /* 0: subject, 1: search string, 2: replacer, 3: replacement text */
1006
+ frame.slots[2] = match_argument(argument_count, arguments, 1u);
1007
+ result = match_subject(context, receiver);
1008
+ frame.slots[0] = result.value;
1009
+ if (result.status == OSEO_STATUS_NORMAL) {
1010
+ result = oseo_internal_value_string(
1011
+ context,
1012
+ match_argument(argument_count, arguments, 0u)
1013
+ );
1014
+ frame.slots[1] = result.value;
1015
+ }
1016
+ bool functional = is_callable(frame.slots[2]);
1017
+ if (result.status == OSEO_STATUS_NORMAL && !functional) {
1018
+ result = oseo_internal_value_string(context, frame.slots[2]);
1019
+ frame.slots[2] = result.value;
1020
+ }
1021
+ size_t position = 0u;
1022
+ bool found = result.status == OSEO_STATUS_NORMAL &&
1023
+ string_find(frame.slots[0], frame.slots[1], 0u, &position);
1024
+ if (result.status != OSEO_STATUS_NORMAL || !found) {
1025
+ if (result.status == OSEO_STATUS_NORMAL) {
1026
+ result = normal(frame.slots[0]);
1027
+ }
1028
+ oseo_roots_release(context, &frame);
1029
+ return result;
1030
+ }
1031
+ size_t search_length = string_object(frame.slots[1])->length;
1032
+ size_t advance = search_length == 0u ? 1u : search_length;
1033
+ size_t consumed = 0u;
1034
+ OseoStringBuilder builder = {NULL, 0u, 0u};
1035
+ while (result.status == OSEO_STATUS_NORMAL && found) {
1036
+ /*
1037
+ * The replacer runs before the preceding subject slice is staged.
1038
+ * The specification computes the replacement and only then builds
1039
+ * the concatenation, so a staged slice that would exceed the
1040
+ * runtime's string ceiling must not suppress this match's call.
1041
+ */
1042
+ if (functional) {
1043
+ OseoValue call_arguments[3] = {
1044
+ frame.slots[1],
1045
+ oseo_number((double)position),
1046
+ frame.slots[0],
1047
+ };
1048
+ result = oseo_call_function(
1049
+ context,
1050
+ frame.slots[2],
1051
+ oseo_undefined(),
1052
+ 3u,
1053
+ call_arguments,
1054
+ oseo_undefined()
1055
+ );
1056
+ frame.slots[3] = result.value;
1057
+ if (result.status == OSEO_STATUS_NORMAL) {
1058
+ result = oseo_internal_value_string(context, frame.slots[3]);
1059
+ frame.slots[3] = result.value;
1060
+ }
1061
+ }
1062
+ if (result.status == OSEO_STATUS_NORMAL) {
1063
+ const OseoString *subject = string_object(frame.slots[0]);
1064
+ result = oseo_internal_string_builder_append(
1065
+ context,
1066
+ &builder,
1067
+ subject->units + consumed,
1068
+ position - consumed
1069
+ );
1070
+ }
1071
+ if (result.status == OSEO_STATUS_NORMAL && functional) {
1072
+ const OseoString *text = string_object(frame.slots[3]);
1073
+ result = oseo_internal_string_builder_append(
1074
+ context,
1075
+ &builder,
1076
+ text->units,
1077
+ text->length
1078
+ );
1079
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1080
+ result = oseo_internal_get_substitution(
1081
+ context,
1082
+ &builder,
1083
+ frame.slots[1],
1084
+ frame.slots[0],
1085
+ position,
1086
+ oseo_undefined(),
1087
+ oseo_undefined(),
1088
+ frame.slots[2]
1089
+ );
1090
+ }
1091
+ if (result.status != OSEO_STATUS_NORMAL) break;
1092
+ consumed = position + search_length;
1093
+ found = all && string_find(
1094
+ frame.slots[0],
1095
+ frame.slots[1],
1096
+ position + advance,
1097
+ &position
1098
+ );
1099
+ }
1100
+ if (result.status == OSEO_STATUS_NORMAL) {
1101
+ const OseoString *subject = string_object(frame.slots[0]);
1102
+ result = oseo_internal_string_builder_append(
1103
+ context,
1104
+ &builder,
1105
+ subject->units + consumed,
1106
+ subject->length - consumed
1107
+ );
1108
+ }
1109
+ if (result.status == OSEO_STATUS_NORMAL) {
1110
+ result = oseo_internal_allocate_string(
1111
+ context,
1112
+ builder.units,
1113
+ builder.length
1114
+ );
1115
+ }
1116
+ oseo_internal_string_builder_release(&builder);
1117
+ oseo_roots_release(context, &frame);
1118
+ return result;
1119
+ }
1120
+
1121
+ OseoResult oseo_internal_string_protocol_dispatch(
1122
+ OseoContext *context,
1123
+ size_t code_id,
1124
+ OseoValue receiver,
1125
+ size_t argument_count,
1126
+ const OseoValue *arguments
1127
+ ) {
1128
+ if (code_id == OSEO_REGEXP_STRING_ITERATOR_NEXT_CODE_ID) {
1129
+ return regexp_string_iterator_next(context, receiver);
1130
+ }
1131
+ if (code_id == OSEO_STRING_MATCH_CODE_ID ||
1132
+ code_id == OSEO_STRING_MATCH_ALL_CODE_ID ||
1133
+ code_id == OSEO_STRING_SEARCH_CODE_ID) {
1134
+ return string_match_or_search(
1135
+ context,
1136
+ code_id,
1137
+ receiver,
1138
+ argument_count,
1139
+ arguments
1140
+ );
1141
+ }
1142
+ if (code_id == OSEO_STRING_SPLIT_CODE_ID) {
1143
+ return string_split(
1144
+ context,
1145
+ receiver,
1146
+ argument_count,
1147
+ arguments
1148
+ );
1149
+ }
1150
+ if (code_id == OSEO_STRING_REPLACE_CODE_ID ||
1151
+ code_id == OSEO_STRING_REPLACE_ALL_CODE_ID) {
1152
+ return string_replace(
1153
+ context,
1154
+ code_id,
1155
+ receiver,
1156
+ argument_count,
1157
+ arguments
1158
+ );
1159
+ }
1160
+ return oseo_unknown_function(context, code_id);
1161
+ }