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