@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,3495 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
2
|
+
/* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
|
|
3
|
+
|
|
4
|
+
#include "runtime_internal.h"
|
|
5
|
+
|
|
6
|
+
#include <limits.h>
|
|
7
|
+
#include <math.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <string.h>
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* The RegExp constructor, initialization state, dynamic matcher artifact,
|
|
13
|
+
* built-in execution, and the prototype methods and accessors. The
|
|
14
|
+
* matcher program and its executor live in runtime_regexp_matcher.c; the
|
|
15
|
+
* well-known symbol methods and the String integration they dispatch to
|
|
16
|
+
* replace the explicit boundary functions below in their own graph nodes.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#define OSEO_REGEXP_PATTERN_LENGTH_LIMIT ((size_t)0x100000u)
|
|
20
|
+
#define OSEO_REGEXP_CAPTURE_LIMIT ((size_t)0xffffu)
|
|
21
|
+
#define OSEO_REGEXP_NESTING_LIMIT ((size_t)256u)
|
|
22
|
+
#define OSEO_REGEXP_MATCHER_INSTRUCTION_LIMIT ((size_t)0x100000u)
|
|
23
|
+
|
|
24
|
+
typedef struct {
|
|
25
|
+
size_t declaration;
|
|
26
|
+
size_t name_offset;
|
|
27
|
+
size_t name_length;
|
|
28
|
+
size_t next;
|
|
29
|
+
size_t path;
|
|
30
|
+
} OseoRegExpNamedCapture;
|
|
31
|
+
|
|
32
|
+
typedef struct {
|
|
33
|
+
size_t parent;
|
|
34
|
+
size_t scope;
|
|
35
|
+
size_t alternative;
|
|
36
|
+
size_t depth;
|
|
37
|
+
} OseoRegExpAlternativeNode;
|
|
38
|
+
|
|
39
|
+
typedef struct {
|
|
40
|
+
void *storage;
|
|
41
|
+
size_t *buckets;
|
|
42
|
+
OseoRegExpNamedCapture *records;
|
|
43
|
+
OseoRegExpAlternativeNode *paths;
|
|
44
|
+
/*
|
|
45
|
+
* Decoded names, in declaration order, followed by room for one
|
|
46
|
+
* decoded reference. A decoded name never outgrows its source text,
|
|
47
|
+
* so twice the pattern length bounds both.
|
|
48
|
+
*/
|
|
49
|
+
uint16_t *names;
|
|
50
|
+
uint16_t *reference;
|
|
51
|
+
size_t name_count;
|
|
52
|
+
size_t bucket_count;
|
|
53
|
+
size_t count;
|
|
54
|
+
size_t path_count;
|
|
55
|
+
OseoRegExpValidation duplicate_status;
|
|
56
|
+
} OseoRegExpNamedCaptureRegistry;
|
|
57
|
+
|
|
58
|
+
static bool regexp_ascii_alpha(uint16_t unit) {
|
|
59
|
+
return (unit >= 'A' && unit <= 'Z') ||
|
|
60
|
+
(unit >= 'a' && unit <= 'z');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static bool regexp_ascii_digit(uint16_t unit) {
|
|
64
|
+
return unit >= '0' && unit <= '9';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static bool regexp_hex_digit(uint16_t unit) {
|
|
68
|
+
return regexp_ascii_digit(unit) ||
|
|
69
|
+
(unit >= 'A' && unit <= 'F') ||
|
|
70
|
+
(unit >= 'a' && unit <= 'f');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static uint16_t regexp_flag_bit(uint16_t unit) {
|
|
74
|
+
switch (unit) {
|
|
75
|
+
case 'd': return OSEO_REGEXP_FLAG_D;
|
|
76
|
+
case 'g': return OSEO_REGEXP_FLAG_G;
|
|
77
|
+
case 'i': return OSEO_REGEXP_FLAG_I;
|
|
78
|
+
case 'm': return OSEO_REGEXP_FLAG_M;
|
|
79
|
+
case 's': return OSEO_REGEXP_FLAG_S;
|
|
80
|
+
case 'u': return OSEO_REGEXP_FLAG_U;
|
|
81
|
+
case 'v': return OSEO_REGEXP_FLAG_V;
|
|
82
|
+
case 'y': return OSEO_REGEXP_FLAG_Y;
|
|
83
|
+
default: return 0u;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static OseoRegExpValidation regexp_validate_flags(
|
|
88
|
+
const OseoString *flags,
|
|
89
|
+
uint16_t *mask
|
|
90
|
+
) {
|
|
91
|
+
uint16_t seen = 0u;
|
|
92
|
+
for (size_t index = 0u; index < flags->length; index += 1u) {
|
|
93
|
+
uint16_t bit = regexp_flag_bit(flags->units[index]);
|
|
94
|
+
if (bit == 0u || (seen & bit) != 0u) return OSEO_REGEXP_INVALID;
|
|
95
|
+
seen = (uint16_t)(seen | bit);
|
|
96
|
+
}
|
|
97
|
+
if ((seen & OSEO_REGEXP_FLAG_U) != 0u &&
|
|
98
|
+
(seen & OSEO_REGEXP_FLAG_V) != 0u) {
|
|
99
|
+
return OSEO_REGEXP_INVALID;
|
|
100
|
+
}
|
|
101
|
+
*mask = seen;
|
|
102
|
+
return OSEO_REGEXP_VALID;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/* Whether one RegExpIdentifierName starts at `start`, and where it ends. */
|
|
106
|
+
static bool regexp_group_name(
|
|
107
|
+
const OseoString *source,
|
|
108
|
+
size_t start,
|
|
109
|
+
size_t *end
|
|
110
|
+
) {
|
|
111
|
+
return oseo_internal_regexp_group_name(source, start, end, NULL, NULL);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static bool regexp_control_escape_point(
|
|
115
|
+
uint16_t escape,
|
|
116
|
+
uint32_t *point
|
|
117
|
+
) {
|
|
118
|
+
switch (escape) {
|
|
119
|
+
case '0': *point = 0u; return true;
|
|
120
|
+
case 'b': *point = 0x08u; return true;
|
|
121
|
+
case 't': *point = 0x09u; return true;
|
|
122
|
+
case 'n': *point = 0x0au; return true;
|
|
123
|
+
case 'v': *point = 0x0bu; return true;
|
|
124
|
+
case 'f': *point = 0x0cu; return true;
|
|
125
|
+
case 'r': *point = 0x0du; return true;
|
|
126
|
+
default: return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static size_t regexp_name_hash(const uint16_t *units, size_t length) {
|
|
131
|
+
size_t hash = (size_t)UINT32_C(2166136261);
|
|
132
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
133
|
+
hash ^= (size_t)units[index];
|
|
134
|
+
hash *= (size_t)UINT32_C(16777619);
|
|
135
|
+
}
|
|
136
|
+
return hash;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
static bool regexp_name_equal(
|
|
140
|
+
const uint16_t *first,
|
|
141
|
+
size_t first_length,
|
|
142
|
+
const uint16_t *second,
|
|
143
|
+
size_t second_length
|
|
144
|
+
) {
|
|
145
|
+
return first_length == second_length &&
|
|
146
|
+
memcmp(first, second, first_length * sizeof(uint16_t)) == 0;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
static bool regexp_named_captures_may_coexist(
|
|
150
|
+
const OseoRegExpNamedCaptureRegistry *registry,
|
|
151
|
+
size_t first,
|
|
152
|
+
size_t second
|
|
153
|
+
) {
|
|
154
|
+
while (registry->paths[first].depth > registry->paths[second].depth) {
|
|
155
|
+
first = registry->paths[first].parent;
|
|
156
|
+
}
|
|
157
|
+
while (registry->paths[second].depth > registry->paths[first].depth) {
|
|
158
|
+
second = registry->paths[second].parent;
|
|
159
|
+
}
|
|
160
|
+
if (first == second) return true;
|
|
161
|
+
while (registry->paths[first].parent !=
|
|
162
|
+
registry->paths[second].parent) {
|
|
163
|
+
first = registry->paths[first].parent;
|
|
164
|
+
second = registry->paths[second].parent;
|
|
165
|
+
}
|
|
166
|
+
return registry->paths[first].scope != registry->paths[second].scope;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static OseoRegExpValidation regexp_named_capture_registry_create(
|
|
170
|
+
OseoContext *context,
|
|
171
|
+
const OseoString *source,
|
|
172
|
+
OseoRegExpNamedCaptureRegistry *registry
|
|
173
|
+
) {
|
|
174
|
+
registry->storage = NULL;
|
|
175
|
+
registry->buckets = NULL;
|
|
176
|
+
registry->records = NULL;
|
|
177
|
+
registry->paths = NULL;
|
|
178
|
+
registry->names = NULL;
|
|
179
|
+
registry->reference = NULL;
|
|
180
|
+
registry->name_count = 0u;
|
|
181
|
+
registry->bucket_count = 0u;
|
|
182
|
+
registry->count = 0u;
|
|
183
|
+
registry->path_count = 0u;
|
|
184
|
+
registry->duplicate_status = OSEO_REGEXP_VALID;
|
|
185
|
+
|
|
186
|
+
size_t named_count = 0u;
|
|
187
|
+
size_t path_capacity = 1u;
|
|
188
|
+
bool character_class = false;
|
|
189
|
+
for (size_t index = 0u; index < source->length; index += 1u) {
|
|
190
|
+
uint16_t unit = source->units[index];
|
|
191
|
+
if (unit == '\\') {
|
|
192
|
+
if (index + 1u < source->length) index += 1u;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (character_class) {
|
|
196
|
+
if (unit == ']') character_class = false;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (unit == '[') {
|
|
200
|
+
character_class = true;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (unit == '|') {
|
|
204
|
+
path_capacity += 1u;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (unit != '(') continue;
|
|
208
|
+
path_capacity += 1u;
|
|
209
|
+
if (index + 3u >= source->length ||
|
|
210
|
+
source->units[index + 1u] != '?' ||
|
|
211
|
+
source->units[index + 2u] != '<' ||
|
|
212
|
+
source->units[index + 3u] == '=' ||
|
|
213
|
+
source->units[index + 3u] == '!') {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
size_t end = 0u;
|
|
217
|
+
if (!regexp_group_name(source, index + 3u, &end)) continue;
|
|
218
|
+
named_count += 1u;
|
|
219
|
+
index = end;
|
|
220
|
+
}
|
|
221
|
+
if (named_count == 0u) return OSEO_REGEXP_VALID;
|
|
222
|
+
|
|
223
|
+
size_t bucket_count = 8u;
|
|
224
|
+
while (bucket_count < named_count * 2u) bucket_count *= 2u;
|
|
225
|
+
size_t bucket_bytes = bucket_count * sizeof(size_t);
|
|
226
|
+
if (named_count >
|
|
227
|
+
(SIZE_MAX - bucket_bytes) / sizeof(OseoRegExpNamedCapture)) {
|
|
228
|
+
return OSEO_REGEXP_LIMIT;
|
|
229
|
+
}
|
|
230
|
+
size_t bytes = bucket_bytes +
|
|
231
|
+
named_count * sizeof(OseoRegExpNamedCapture);
|
|
232
|
+
if (path_capacity >
|
|
233
|
+
(SIZE_MAX - bytes) / sizeof(OseoRegExpAlternativeNode)) {
|
|
234
|
+
return OSEO_REGEXP_LIMIT;
|
|
235
|
+
}
|
|
236
|
+
bytes += path_capacity * sizeof(OseoRegExpAlternativeNode);
|
|
237
|
+
if (source->length > (SIZE_MAX - bytes) / (2u * sizeof(uint16_t))) {
|
|
238
|
+
return OSEO_REGEXP_LIMIT;
|
|
239
|
+
}
|
|
240
|
+
bytes += 2u * source->length * sizeof(uint16_t);
|
|
241
|
+
registry->storage = oseo_internal_allocate_heap_bytes(context, bytes);
|
|
242
|
+
if (registry->storage == NULL) {
|
|
243
|
+
return OSEO_REGEXP_ALLOCATION_FAILURE;
|
|
244
|
+
}
|
|
245
|
+
registry->buckets = registry->storage;
|
|
246
|
+
registry->records = (OseoRegExpNamedCapture *)(
|
|
247
|
+
(unsigned char *)registry->storage + bucket_bytes
|
|
248
|
+
);
|
|
249
|
+
registry->paths = (OseoRegExpAlternativeNode *)(
|
|
250
|
+
registry->records + named_count
|
|
251
|
+
);
|
|
252
|
+
registry->names = (uint16_t *)(registry->paths + path_capacity);
|
|
253
|
+
registry->reference = registry->names + source->length;
|
|
254
|
+
registry->bucket_count = bucket_count;
|
|
255
|
+
for (size_t index = 0u; index < bucket_count; index += 1u) {
|
|
256
|
+
registry->buckets[index] = SIZE_MAX;
|
|
257
|
+
}
|
|
258
|
+
registry->paths[0] = (OseoRegExpAlternativeNode){
|
|
259
|
+
SIZE_MAX,
|
|
260
|
+
SIZE_MAX,
|
|
261
|
+
0u,
|
|
262
|
+
0u,
|
|
263
|
+
};
|
|
264
|
+
registry->path_count = 1u;
|
|
265
|
+
|
|
266
|
+
character_class = false;
|
|
267
|
+
size_t current_path = 0u;
|
|
268
|
+
for (size_t index = 0u; index < source->length; index += 1u) {
|
|
269
|
+
uint16_t unit = source->units[index];
|
|
270
|
+
if (unit == '\\') {
|
|
271
|
+
if (index + 1u < source->length) index += 1u;
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (character_class) {
|
|
275
|
+
if (unit == ']') character_class = false;
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (unit == '[') {
|
|
279
|
+
character_class = true;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (unit == ')') {
|
|
283
|
+
size_t parent = registry->paths[current_path].parent;
|
|
284
|
+
if (parent != SIZE_MAX) current_path = parent;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (unit == '|') {
|
|
288
|
+
OseoRegExpAlternativeNode *current =
|
|
289
|
+
®istry->paths[current_path];
|
|
290
|
+
registry->paths[registry->path_count] =
|
|
291
|
+
(OseoRegExpAlternativeNode){
|
|
292
|
+
current->parent,
|
|
293
|
+
current->scope,
|
|
294
|
+
current->alternative + 1u,
|
|
295
|
+
current->depth,
|
|
296
|
+
};
|
|
297
|
+
current_path = registry->path_count;
|
|
298
|
+
registry->path_count += 1u;
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (unit != '(') continue;
|
|
302
|
+
|
|
303
|
+
size_t end = 0u;
|
|
304
|
+
size_t name_offset = registry->name_count;
|
|
305
|
+
size_t name_length = 0u;
|
|
306
|
+
bool named = index + 3u < source->length &&
|
|
307
|
+
source->units[index + 1u] == '?' &&
|
|
308
|
+
source->units[index + 2u] == '<' &&
|
|
309
|
+
source->units[index + 3u] != '=' &&
|
|
310
|
+
source->units[index + 3u] != '!' &&
|
|
311
|
+
oseo_internal_regexp_group_name(
|
|
312
|
+
source,
|
|
313
|
+
index + 3u,
|
|
314
|
+
&end,
|
|
315
|
+
registry->names + name_offset,
|
|
316
|
+
&name_length
|
|
317
|
+
);
|
|
318
|
+
if (named) {
|
|
319
|
+
const uint16_t *name = registry->names + name_offset;
|
|
320
|
+
registry->name_count += name_length;
|
|
321
|
+
size_t bucket = regexp_name_hash(name, name_length) &
|
|
322
|
+
(bucket_count - 1u);
|
|
323
|
+
size_t previous_same = SIZE_MAX;
|
|
324
|
+
for (size_t previous = registry->buckets[bucket];
|
|
325
|
+
previous != SIZE_MAX;
|
|
326
|
+
previous = registry->records[previous].next) {
|
|
327
|
+
OseoRegExpNamedCapture *record =
|
|
328
|
+
®istry->records[previous];
|
|
329
|
+
if (regexp_name_equal(
|
|
330
|
+
registry->names + record->name_offset,
|
|
331
|
+
record->name_length,
|
|
332
|
+
name,
|
|
333
|
+
name_length
|
|
334
|
+
)) {
|
|
335
|
+
previous_same = previous;
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/*
|
|
340
|
+
* Source order is the compiler parser's alternative-path order.
|
|
341
|
+
* Exclusivity is transitive there, so the adjacent declaration
|
|
342
|
+
* decides whether the whole same-name set can participate.
|
|
343
|
+
*/
|
|
344
|
+
if (previous_same != SIZE_MAX &&
|
|
345
|
+
regexp_named_captures_may_coexist(
|
|
346
|
+
registry,
|
|
347
|
+
registry->records[previous_same].path,
|
|
348
|
+
current_path
|
|
349
|
+
)) {
|
|
350
|
+
registry->duplicate_status = OSEO_REGEXP_INVALID;
|
|
351
|
+
}
|
|
352
|
+
OseoRegExpNamedCapture *record =
|
|
353
|
+
®istry->records[registry->count];
|
|
354
|
+
record->declaration = index;
|
|
355
|
+
record->name_offset = name_offset;
|
|
356
|
+
record->name_length = name_length;
|
|
357
|
+
record->next = registry->buckets[bucket];
|
|
358
|
+
record->path = current_path;
|
|
359
|
+
registry->buckets[bucket] = registry->count;
|
|
360
|
+
registry->count += 1u;
|
|
361
|
+
}
|
|
362
|
+
registry->paths[registry->path_count] =
|
|
363
|
+
(OseoRegExpAlternativeNode){
|
|
364
|
+
current_path,
|
|
365
|
+
index,
|
|
366
|
+
0u,
|
|
367
|
+
registry->paths[current_path].depth + 1u,
|
|
368
|
+
};
|
|
369
|
+
current_path = registry->path_count;
|
|
370
|
+
registry->path_count += 1u;
|
|
371
|
+
if (named) index = end;
|
|
372
|
+
}
|
|
373
|
+
return OSEO_REGEXP_VALID;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
static void regexp_named_capture_registry_destroy(
|
|
377
|
+
OseoRegExpNamedCaptureRegistry *registry
|
|
378
|
+
) {
|
|
379
|
+
free(registry->storage);
|
|
380
|
+
registry->storage = NULL;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/* Count captures before validating backreferences, which may point forward. */
|
|
384
|
+
static OseoRegExpValidation regexp_count_captures(
|
|
385
|
+
const OseoString *source,
|
|
386
|
+
size_t *capture_count
|
|
387
|
+
) {
|
|
388
|
+
bool character_class = false;
|
|
389
|
+
size_t count = 0u;
|
|
390
|
+
for (size_t index = 0u; index < source->length; index += 1u) {
|
|
391
|
+
uint16_t unit = source->units[index];
|
|
392
|
+
if (unit == '\\') {
|
|
393
|
+
if (index + 1u < source->length) index += 1u;
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
if (character_class) {
|
|
397
|
+
if (unit == ']') character_class = false;
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
if (unit == '[') {
|
|
401
|
+
character_class = true;
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
if (unit != '(') continue;
|
|
405
|
+
bool capture = index + 1u >= source->length ||
|
|
406
|
+
source->units[index + 1u] != '?';
|
|
407
|
+
if (!capture && index + 3u < source->length &&
|
|
408
|
+
source->units[index + 2u] == '<' &&
|
|
409
|
+
source->units[index + 3u] != '=' &&
|
|
410
|
+
source->units[index + 3u] != '!') {
|
|
411
|
+
capture = true;
|
|
412
|
+
}
|
|
413
|
+
if (!capture) continue;
|
|
414
|
+
if (count == OSEO_REGEXP_CAPTURE_LIMIT) return OSEO_REGEXP_LIMIT;
|
|
415
|
+
count += 1u;
|
|
416
|
+
}
|
|
417
|
+
*capture_count = count;
|
|
418
|
+
return OSEO_REGEXP_VALID;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/* Whether the reference name starting at `name_start` names a group. */
|
|
422
|
+
static bool regexp_named_capture_exists(
|
|
423
|
+
const OseoRegExpNamedCaptureRegistry *registry,
|
|
424
|
+
const OseoString *source,
|
|
425
|
+
size_t name_start
|
|
426
|
+
) {
|
|
427
|
+
if (registry->bucket_count == 0u) return false;
|
|
428
|
+
size_t end = 0u;
|
|
429
|
+
size_t length = 0u;
|
|
430
|
+
if (!oseo_internal_regexp_group_name(
|
|
431
|
+
source,
|
|
432
|
+
name_start,
|
|
433
|
+
&end,
|
|
434
|
+
registry->reference,
|
|
435
|
+
&length
|
|
436
|
+
)) {
|
|
437
|
+
return false;
|
|
438
|
+
}
|
|
439
|
+
size_t bucket = regexp_name_hash(registry->reference, length) &
|
|
440
|
+
(registry->bucket_count - 1u);
|
|
441
|
+
for (size_t index = registry->buckets[bucket];
|
|
442
|
+
index != SIZE_MAX;
|
|
443
|
+
index = registry->records[index].next) {
|
|
444
|
+
const OseoRegExpNamedCapture *record = ®istry->records[index];
|
|
445
|
+
if (regexp_name_equal(
|
|
446
|
+
registry->names + record->name_offset,
|
|
447
|
+
record->name_length,
|
|
448
|
+
registry->reference,
|
|
449
|
+
length
|
|
450
|
+
)) {
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/*
|
|
458
|
+
* The last unit of the LegacyOctalEscapeSequence whose first digit is at
|
|
459
|
+
* `start`. Three digits are taken only when the first is at most 3, so
|
|
460
|
+
* the value stays at most 0o377.
|
|
461
|
+
*/
|
|
462
|
+
static size_t regexp_legacy_octal_end(const OseoString *source, size_t start) {
|
|
463
|
+
size_t end = start;
|
|
464
|
+
if (end + 1u < source->length &&
|
|
465
|
+
source->units[end + 1u] >= '0' && source->units[end + 1u] <= '7') {
|
|
466
|
+
end += 1u;
|
|
467
|
+
if (source->units[start] <= '3' && end + 1u < source->length &&
|
|
468
|
+
source->units[end + 1u] >= '0' &&
|
|
469
|
+
source->units[end + 1u] <= '7') {
|
|
470
|
+
end += 1u;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return end;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/*
|
|
477
|
+
* Validate one escape at the backslash `*index` and leave `*index` on its
|
|
478
|
+
* last unit.
|
|
479
|
+
*
|
|
480
|
+
* Without `u` or `v` the pattern follows Annex B's grammar, so most text
|
|
481
|
+
* that is an error in the core grammar is an identity escape, a legacy
|
|
482
|
+
* octal escape, or a decimal escape read past the capture count. A `\c`
|
|
483
|
+
* without a letter outside a class is the backslash alone: `*index` stays
|
|
484
|
+
* on it and the `c` is the next atom.
|
|
485
|
+
*/
|
|
486
|
+
static OseoRegExpValidation regexp_escape(
|
|
487
|
+
const OseoString *source,
|
|
488
|
+
size_t *index,
|
|
489
|
+
uint16_t flag_mask,
|
|
490
|
+
size_t capture_count,
|
|
491
|
+
const OseoRegExpNamedCaptureRegistry *named_captures,
|
|
492
|
+
bool character_class
|
|
493
|
+
) {
|
|
494
|
+
size_t cursor = *index + 1u;
|
|
495
|
+
if (cursor >= source->length) return OSEO_REGEXP_INVALID;
|
|
496
|
+
uint16_t unit = source->units[cursor];
|
|
497
|
+
bool unicode = (flag_mask &
|
|
498
|
+
(OSEO_REGEXP_FLAG_U | OSEO_REGEXP_FLAG_V)) != 0u;
|
|
499
|
+
bool annex_b = !unicode;
|
|
500
|
+
if (annex_b && (unit == 'p' || unit == 'P' || unit == 'q' ||
|
|
501
|
+
(character_class && unit == 'B'))) {
|
|
502
|
+
*index = cursor;
|
|
503
|
+
return OSEO_REGEXP_VALID;
|
|
504
|
+
}
|
|
505
|
+
if (character_class && unit == 'B') {
|
|
506
|
+
return OSEO_REGEXP_INVALID;
|
|
507
|
+
}
|
|
508
|
+
if (unit == 'p' || unit == 'P') {
|
|
509
|
+
if (!unicode || cursor + 1u >= source->length ||
|
|
510
|
+
source->units[cursor + 1u] != '{') {
|
|
511
|
+
return OSEO_REGEXP_INVALID;
|
|
512
|
+
}
|
|
513
|
+
size_t prop = cursor + 2u;
|
|
514
|
+
bool has_content = false;
|
|
515
|
+
bool has_equals = false;
|
|
516
|
+
while (prop < source->length &&
|
|
517
|
+
source->units[prop] != '}') {
|
|
518
|
+
uint16_t pc = source->units[prop];
|
|
519
|
+
if (pc == '=') {
|
|
520
|
+
if (has_equals || !has_content) {
|
|
521
|
+
return OSEO_REGEXP_INVALID;
|
|
522
|
+
}
|
|
523
|
+
has_equals = true;
|
|
524
|
+
has_content = false;
|
|
525
|
+
} else if (regexp_ascii_alpha(pc) || pc == '_' ||
|
|
526
|
+
(has_equals && regexp_ascii_digit(pc))) {
|
|
527
|
+
has_content = true;
|
|
528
|
+
} else {
|
|
529
|
+
return OSEO_REGEXP_INVALID;
|
|
530
|
+
}
|
|
531
|
+
prop += 1u;
|
|
532
|
+
}
|
|
533
|
+
if (!has_content || prop >= source->length) {
|
|
534
|
+
return OSEO_REGEXP_INVALID;
|
|
535
|
+
}
|
|
536
|
+
return OSEO_REGEXP_UNSUPPORTED;
|
|
537
|
+
}
|
|
538
|
+
if (character_class && unit == 'q') {
|
|
539
|
+
return (flag_mask & OSEO_REGEXP_FLAG_V) != 0u
|
|
540
|
+
? OSEO_REGEXP_UNSUPPORTED
|
|
541
|
+
: OSEO_REGEXP_INVALID;
|
|
542
|
+
}
|
|
543
|
+
if (unit == 'u') {
|
|
544
|
+
if (cursor + 1u < source->length &&
|
|
545
|
+
source->units[cursor + 1u] == '{' && unicode) {
|
|
546
|
+
size_t digit = cursor + 2u;
|
|
547
|
+
size_t count = 0u;
|
|
548
|
+
uint32_t value = 0u;
|
|
549
|
+
while (digit < source->length &&
|
|
550
|
+
source->units[digit] != '}') {
|
|
551
|
+
uint16_t character = source->units[digit];
|
|
552
|
+
if (!regexp_hex_digit(character)) {
|
|
553
|
+
return OSEO_REGEXP_INVALID;
|
|
554
|
+
}
|
|
555
|
+
uint32_t part = character <= '9'
|
|
556
|
+
? (uint32_t)(character - '0')
|
|
557
|
+
: character <= 'F'
|
|
558
|
+
? (uint32_t)(character - 'A' + 10u)
|
|
559
|
+
: (uint32_t)(character - 'a' + 10u);
|
|
560
|
+
if (value >
|
|
561
|
+
(UINT32_C(0x10ffff) - part) / UINT32_C(16)) {
|
|
562
|
+
return OSEO_REGEXP_INVALID;
|
|
563
|
+
}
|
|
564
|
+
value = value * UINT32_C(16) + part;
|
|
565
|
+
count += 1u;
|
|
566
|
+
digit += 1u;
|
|
567
|
+
}
|
|
568
|
+
if (count == 0u || digit >= source->length) {
|
|
569
|
+
return OSEO_REGEXP_INVALID;
|
|
570
|
+
}
|
|
571
|
+
*index = digit;
|
|
572
|
+
return OSEO_REGEXP_VALID;
|
|
573
|
+
}
|
|
574
|
+
bool complete = cursor + 4u < source->length;
|
|
575
|
+
for (size_t offset = 1u; complete && offset <= 4u; offset += 1u) {
|
|
576
|
+
complete = regexp_hex_digit(source->units[cursor + offset]);
|
|
577
|
+
}
|
|
578
|
+
if (!complete) {
|
|
579
|
+
if (!annex_b) return OSEO_REGEXP_INVALID;
|
|
580
|
+
*index = cursor;
|
|
581
|
+
return OSEO_REGEXP_VALID;
|
|
582
|
+
}
|
|
583
|
+
*index = cursor + 4u;
|
|
584
|
+
return OSEO_REGEXP_VALID;
|
|
585
|
+
}
|
|
586
|
+
if (unit == 'x') {
|
|
587
|
+
if (cursor + 2u >= source->length ||
|
|
588
|
+
!regexp_hex_digit(source->units[cursor + 1u]) ||
|
|
589
|
+
!regexp_hex_digit(source->units[cursor + 2u])) {
|
|
590
|
+
if (!annex_b) return OSEO_REGEXP_INVALID;
|
|
591
|
+
*index = cursor;
|
|
592
|
+
return OSEO_REGEXP_VALID;
|
|
593
|
+
}
|
|
594
|
+
*index = cursor + 2u;
|
|
595
|
+
return OSEO_REGEXP_VALID;
|
|
596
|
+
}
|
|
597
|
+
if (unit == 'c') {
|
|
598
|
+
uint16_t letter = cursor + 1u < source->length
|
|
599
|
+
? source->units[cursor + 1u]
|
|
600
|
+
: 0u;
|
|
601
|
+
if (regexp_ascii_alpha(letter) ||
|
|
602
|
+
(annex_b && character_class &&
|
|
603
|
+
(regexp_ascii_digit(letter) || letter == '_'))) {
|
|
604
|
+
*index = cursor + 1u;
|
|
605
|
+
return OSEO_REGEXP_VALID;
|
|
606
|
+
}
|
|
607
|
+
return annex_b ? OSEO_REGEXP_VALID : OSEO_REGEXP_INVALID;
|
|
608
|
+
}
|
|
609
|
+
if (unit == 'k' && annex_b && named_captures->bucket_count == 0u) {
|
|
610
|
+
*index = cursor;
|
|
611
|
+
return OSEO_REGEXP_VALID;
|
|
612
|
+
}
|
|
613
|
+
if (unit == 'k') {
|
|
614
|
+
if (character_class || cursor + 1u >= source->length ||
|
|
615
|
+
source->units[cursor + 1u] != '<') {
|
|
616
|
+
return OSEO_REGEXP_INVALID;
|
|
617
|
+
}
|
|
618
|
+
size_t end = 0u;
|
|
619
|
+
if (!regexp_group_name(source, cursor + 2u, &end)) {
|
|
620
|
+
return OSEO_REGEXP_INVALID;
|
|
621
|
+
}
|
|
622
|
+
if (!regexp_named_capture_exists(
|
|
623
|
+
named_captures,
|
|
624
|
+
source,
|
|
625
|
+
cursor + 2u
|
|
626
|
+
)) {
|
|
627
|
+
return OSEO_REGEXP_INVALID;
|
|
628
|
+
}
|
|
629
|
+
*index = end;
|
|
630
|
+
return OSEO_REGEXP_VALID;
|
|
631
|
+
}
|
|
632
|
+
if (regexp_ascii_digit(unit) && annex_b) {
|
|
633
|
+
if (!character_class && unit != '0') {
|
|
634
|
+
size_t value = 0u;
|
|
635
|
+
size_t end = cursor;
|
|
636
|
+
while (end < source->length &&
|
|
637
|
+
regexp_ascii_digit(source->units[end])) {
|
|
638
|
+
if (value <= capture_count) {
|
|
639
|
+
value = value * 10u +
|
|
640
|
+
(size_t)(source->units[end] - '0');
|
|
641
|
+
}
|
|
642
|
+
end += 1u;
|
|
643
|
+
}
|
|
644
|
+
if (value <= capture_count) {
|
|
645
|
+
*index = end - 1u;
|
|
646
|
+
return OSEO_REGEXP_VALID;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
*index = unit >= '8'
|
|
650
|
+
? cursor
|
|
651
|
+
: regexp_legacy_octal_end(source, cursor);
|
|
652
|
+
return OSEO_REGEXP_VALID;
|
|
653
|
+
}
|
|
654
|
+
if (regexp_ascii_digit(unit)) {
|
|
655
|
+
if (character_class && unit != '0') {
|
|
656
|
+
return OSEO_REGEXP_INVALID;
|
|
657
|
+
}
|
|
658
|
+
if (unit == '0') {
|
|
659
|
+
if (cursor + 1u < source->length &&
|
|
660
|
+
regexp_ascii_digit(source->units[cursor + 1u])) {
|
|
661
|
+
return OSEO_REGEXP_INVALID;
|
|
662
|
+
}
|
|
663
|
+
*index = cursor;
|
|
664
|
+
return OSEO_REGEXP_VALID;
|
|
665
|
+
}
|
|
666
|
+
size_t value = 0u;
|
|
667
|
+
while (cursor < source->length &&
|
|
668
|
+
regexp_ascii_digit(source->units[cursor])) {
|
|
669
|
+
size_t digit = (size_t)(source->units[cursor] - '0');
|
|
670
|
+
if (value > (SIZE_MAX - digit) / 10u) {
|
|
671
|
+
return OSEO_REGEXP_INVALID;
|
|
672
|
+
}
|
|
673
|
+
value = value * 10u + digit;
|
|
674
|
+
cursor += 1u;
|
|
675
|
+
}
|
|
676
|
+
if (value == 0u || value > capture_count) {
|
|
677
|
+
return OSEO_REGEXP_INVALID;
|
|
678
|
+
}
|
|
679
|
+
*index = cursor - 1u;
|
|
680
|
+
return OSEO_REGEXP_VALID;
|
|
681
|
+
}
|
|
682
|
+
if (unit == 'd' || unit == 'D' || unit == 's' || unit == 'S' ||
|
|
683
|
+
unit == 'w' || unit == 'W' || unit == 'b' || unit == 'B' ||
|
|
684
|
+
unit == 'f' || unit == 'n' || unit == 'r' || unit == 't' ||
|
|
685
|
+
unit == 'v' || unit == '/' || unit == '^' || unit == '$' ||
|
|
686
|
+
unit == '\\' || unit == '.' || unit == '*' || unit == '+' ||
|
|
687
|
+
unit == '?' || unit == '(' || unit == ')' || unit == '[' ||
|
|
688
|
+
unit == ']' || unit == '{' || unit == '}' || unit == '|' ||
|
|
689
|
+
(character_class && unicode && unit == '-')) {
|
|
690
|
+
*index = cursor;
|
|
691
|
+
return OSEO_REGEXP_VALID;
|
|
692
|
+
}
|
|
693
|
+
if (unicode) return OSEO_REGEXP_INVALID;
|
|
694
|
+
/*
|
|
695
|
+
* Annex B's identity escape admits every source character but `c`,
|
|
696
|
+
* and `k` once the pattern declares a group name, both decided above.
|
|
697
|
+
*/
|
|
698
|
+
*index = cursor;
|
|
699
|
+
return OSEO_REGEXP_VALID;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
static OseoRegExpValidation regexp_character_class(
|
|
703
|
+
const OseoString *source,
|
|
704
|
+
size_t *index,
|
|
705
|
+
uint16_t flag_mask,
|
|
706
|
+
size_t capture_count,
|
|
707
|
+
const OseoRegExpNamedCaptureRegistry *named_captures
|
|
708
|
+
) {
|
|
709
|
+
bool unicode_sets = (flag_mask & OSEO_REGEXP_FLAG_V) != 0u;
|
|
710
|
+
bool unicode = (flag_mask &
|
|
711
|
+
(OSEO_REGEXP_FLAG_U | OSEO_REGEXP_FLAG_V)) != 0u;
|
|
712
|
+
if (unicode_sets) return OSEO_REGEXP_UNSUPPORTED;
|
|
713
|
+
size_t cursor = *index + 1u;
|
|
714
|
+
if (cursor < source->length && source->units[cursor] == '^') cursor += 1u;
|
|
715
|
+
bool have_previous = false;
|
|
716
|
+
bool previous_single = false;
|
|
717
|
+
uint32_t previous = 0u;
|
|
718
|
+
bool range = false;
|
|
719
|
+
for (; cursor < source->length; cursor += 1u) {
|
|
720
|
+
uint16_t unit = source->units[cursor];
|
|
721
|
+
if (unit == ']' && !range) {
|
|
722
|
+
*index = cursor;
|
|
723
|
+
return OSEO_REGEXP_VALID;
|
|
724
|
+
}
|
|
725
|
+
if (unicode_sets &&
|
|
726
|
+
(unit == '[' ||
|
|
727
|
+
(cursor + 1u < source->length &&
|
|
728
|
+
((unit == '&' && source->units[cursor + 1u] == '&') ||
|
|
729
|
+
(unit == '-' && source->units[cursor + 1u] == '-'))))) {
|
|
730
|
+
return OSEO_REGEXP_UNSUPPORTED;
|
|
731
|
+
}
|
|
732
|
+
bool single = true;
|
|
733
|
+
uint32_t point = unit;
|
|
734
|
+
if (unicode && unit >= UINT16_C(0xd800) &&
|
|
735
|
+
unit <= UINT16_C(0xdbff) && cursor + 1u < source->length) {
|
|
736
|
+
uint16_t trail = source->units[cursor + 1u];
|
|
737
|
+
if (trail >= UINT16_C(0xdc00) && trail <= UINT16_C(0xdfff)) {
|
|
738
|
+
point = UINT32_C(0x10000) +
|
|
739
|
+
((uint32_t)(unit - UINT16_C(0xd800)) << 10u) +
|
|
740
|
+
(uint32_t)(trail - UINT16_C(0xdc00));
|
|
741
|
+
cursor += 1u;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
if (unit == '\\') {
|
|
745
|
+
size_t escape_start = cursor;
|
|
746
|
+
size_t escaped = cursor;
|
|
747
|
+
OseoRegExpValidation status = regexp_escape(
|
|
748
|
+
source,
|
|
749
|
+
&escaped,
|
|
750
|
+
flag_mask,
|
|
751
|
+
capture_count,
|
|
752
|
+
named_captures,
|
|
753
|
+
true
|
|
754
|
+
);
|
|
755
|
+
if (status != OSEO_REGEXP_VALID) return status;
|
|
756
|
+
uint16_t escape = source->units[escape_start + 1u];
|
|
757
|
+
single = escape != 'd' && escape != 'D' && escape != 's' &&
|
|
758
|
+
escape != 'S' && escape != 'w' && escape != 'W';
|
|
759
|
+
if (escaped == escape_start) {
|
|
760
|
+
// Annex B's `\c` without a control letter: the backslash.
|
|
761
|
+
point = '\\';
|
|
762
|
+
} else if (escaped == escape_start + 1u &&
|
|
763
|
+
(escape == 'x' || escape == 'u' ||
|
|
764
|
+
regexp_ascii_digit(escape))) {
|
|
765
|
+
// An incomplete escape read as an identity escape, or a
|
|
766
|
+
// one-digit legacy octal escape.
|
|
767
|
+
point = regexp_ascii_digit(escape) && escape < '8'
|
|
768
|
+
? (uint32_t)(escape - '0')
|
|
769
|
+
: escape;
|
|
770
|
+
} else if (regexp_ascii_digit(escape)) {
|
|
771
|
+
point = 0u;
|
|
772
|
+
for (size_t offset = escape_start + 1u;
|
|
773
|
+
offset <= escaped;
|
|
774
|
+
offset += 1u) {
|
|
775
|
+
point = point * 8u +
|
|
776
|
+
(uint32_t)(source->units[offset] - '0');
|
|
777
|
+
}
|
|
778
|
+
} else if (escape == 'c') {
|
|
779
|
+
point = source->units[escape_start + 2u] & 0x1fu;
|
|
780
|
+
} else if (escape == 'x') {
|
|
781
|
+
point = 0u;
|
|
782
|
+
for (size_t offset = escape_start + 2u;
|
|
783
|
+
offset <= escaped;
|
|
784
|
+
offset += 1u) {
|
|
785
|
+
uint16_t digit = source->units[offset];
|
|
786
|
+
point = point * 16u + (digit <= '9'
|
|
787
|
+
? (uint32_t)(digit - '0')
|
|
788
|
+
: digit <= 'F'
|
|
789
|
+
? (uint32_t)(digit - 'A' + 10u)
|
|
790
|
+
: (uint32_t)(digit - 'a' + 10u));
|
|
791
|
+
}
|
|
792
|
+
} else if (escape == 'u') {
|
|
793
|
+
size_t start = escape_start + 2u;
|
|
794
|
+
size_t end = escaped + 1u;
|
|
795
|
+
bool braced = source->units[start] == '{';
|
|
796
|
+
if (braced) {
|
|
797
|
+
start += 1u;
|
|
798
|
+
end -= 1u;
|
|
799
|
+
}
|
|
800
|
+
point = 0u;
|
|
801
|
+
for (size_t offset = start; offset < end; offset += 1u) {
|
|
802
|
+
uint16_t digit = source->units[offset];
|
|
803
|
+
point = point * 16u + (digit <= '9'
|
|
804
|
+
? (uint32_t)(digit - '0')
|
|
805
|
+
: digit <= 'F'
|
|
806
|
+
? (uint32_t)(digit - 'A' + 10u)
|
|
807
|
+
: (uint32_t)(digit - 'a' + 10u));
|
|
808
|
+
}
|
|
809
|
+
if (unicode && !braced &&
|
|
810
|
+
point >= UINT32_C(0xd800) &&
|
|
811
|
+
point <= UINT32_C(0xdbff) &&
|
|
812
|
+
escaped + 6u < source->length &&
|
|
813
|
+
source->units[escaped + 1u] == '\\' &&
|
|
814
|
+
source->units[escaped + 2u] == 'u') {
|
|
815
|
+
uint32_t trail = 0u;
|
|
816
|
+
bool valid_trail = true;
|
|
817
|
+
for (size_t offset = escaped + 3u;
|
|
818
|
+
offset <= escaped + 6u;
|
|
819
|
+
offset += 1u) {
|
|
820
|
+
uint16_t digit = source->units[offset];
|
|
821
|
+
if (!regexp_hex_digit(digit)) {
|
|
822
|
+
valid_trail = false;
|
|
823
|
+
break;
|
|
824
|
+
}
|
|
825
|
+
trail = trail * 16u + (digit <= '9'
|
|
826
|
+
? (uint32_t)(digit - '0')
|
|
827
|
+
: digit <= 'F'
|
|
828
|
+
? (uint32_t)(digit - 'A' + 10u)
|
|
829
|
+
: (uint32_t)(digit - 'a' + 10u));
|
|
830
|
+
}
|
|
831
|
+
if (valid_trail && trail >= UINT32_C(0xdc00) &&
|
|
832
|
+
trail <= UINT32_C(0xdfff)) {
|
|
833
|
+
point = UINT32_C(0x10000) +
|
|
834
|
+
((point - UINT32_C(0xd800)) << 10u) +
|
|
835
|
+
(trail - UINT32_C(0xdc00));
|
|
836
|
+
escaped += 6u;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
} else if (!regexp_control_escape_point(escape, &point) &&
|
|
840
|
+
single) {
|
|
841
|
+
point = source->units[escape_start + 1u];
|
|
842
|
+
}
|
|
843
|
+
cursor = escaped;
|
|
844
|
+
}
|
|
845
|
+
// A dash that ends a pending range is that range's second atom.
|
|
846
|
+
if (unit == '-' && have_previous && !range &&
|
|
847
|
+
cursor + 1u < source->length &&
|
|
848
|
+
source->units[cursor + 1u] != ']') {
|
|
849
|
+
if (!previous_single && unicode) return OSEO_REGEXP_INVALID;
|
|
850
|
+
range = true;
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
853
|
+
if (range) {
|
|
854
|
+
/*
|
|
855
|
+
* Annex B's CharacterRangeOrUnion makes a range with a class
|
|
856
|
+
* escape at either end the union of both and the dash.
|
|
857
|
+
*/
|
|
858
|
+
if (previous_single && single && previous > point) {
|
|
859
|
+
return OSEO_REGEXP_INVALID;
|
|
860
|
+
}
|
|
861
|
+
if ((!previous_single || !single) && unicode) {
|
|
862
|
+
return OSEO_REGEXP_INVALID;
|
|
863
|
+
}
|
|
864
|
+
range = false;
|
|
865
|
+
// A completed range is no ClassAtom a following dash extends.
|
|
866
|
+
have_previous = false;
|
|
867
|
+
continue;
|
|
868
|
+
}
|
|
869
|
+
have_previous = true;
|
|
870
|
+
previous_single = single;
|
|
871
|
+
previous = point;
|
|
872
|
+
}
|
|
873
|
+
return OSEO_REGEXP_INVALID;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
static OseoRegExpValidation regexp_decimal_bound(
|
|
877
|
+
const OseoString *source,
|
|
878
|
+
size_t start,
|
|
879
|
+
size_t end,
|
|
880
|
+
uint64_t *value
|
|
881
|
+
) {
|
|
882
|
+
if (start == end) return OSEO_REGEXP_INVALID;
|
|
883
|
+
uint64_t result = 0u;
|
|
884
|
+
for (size_t index = start; index < end; index += 1u) {
|
|
885
|
+
if (!regexp_ascii_digit(source->units[index])) {
|
|
886
|
+
return OSEO_REGEXP_INVALID;
|
|
887
|
+
}
|
|
888
|
+
uint64_t digit = (uint64_t)(source->units[index] - '0');
|
|
889
|
+
if (result > (UINT64_C(9007199254740991) - digit) / 10u) {
|
|
890
|
+
return OSEO_REGEXP_LIMIT;
|
|
891
|
+
}
|
|
892
|
+
result = result * 10u + digit;
|
|
893
|
+
}
|
|
894
|
+
*value = result;
|
|
895
|
+
return OSEO_REGEXP_VALID;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
static bool regexp_bound_exceeds(
|
|
899
|
+
const OseoString *source,
|
|
900
|
+
size_t a_start,
|
|
901
|
+
size_t a_end,
|
|
902
|
+
size_t b_start,
|
|
903
|
+
size_t b_end
|
|
904
|
+
) {
|
|
905
|
+
while (a_start < a_end && source->units[a_start] == '0') {
|
|
906
|
+
a_start += 1u;
|
|
907
|
+
}
|
|
908
|
+
while (b_start < b_end && source->units[b_start] == '0') {
|
|
909
|
+
b_start += 1u;
|
|
910
|
+
}
|
|
911
|
+
size_t a_len = a_end - a_start;
|
|
912
|
+
size_t b_len = b_end - b_start;
|
|
913
|
+
if (a_len != b_len) return a_len > b_len;
|
|
914
|
+
for (size_t i = 0u; i < a_len; i += 1u) {
|
|
915
|
+
if (source->units[a_start + i] !=
|
|
916
|
+
source->units[b_start + i]) {
|
|
917
|
+
return source->units[a_start + i] >
|
|
918
|
+
source->units[b_start + i];
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
return false;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
static OseoRegExpValidation regexp_braced_quantifier(
|
|
925
|
+
const OseoString *source,
|
|
926
|
+
size_t *index
|
|
927
|
+
) {
|
|
928
|
+
size_t cursor = *index + 1u;
|
|
929
|
+
size_t minimum_start = cursor;
|
|
930
|
+
while (cursor < source->length &&
|
|
931
|
+
regexp_ascii_digit(source->units[cursor])) {
|
|
932
|
+
cursor += 1u;
|
|
933
|
+
}
|
|
934
|
+
size_t minimum_end = cursor;
|
|
935
|
+
uint64_t minimum = 0u;
|
|
936
|
+
OseoRegExpValidation minimum_status = regexp_decimal_bound(
|
|
937
|
+
source,
|
|
938
|
+
minimum_start,
|
|
939
|
+
minimum_end,
|
|
940
|
+
&minimum
|
|
941
|
+
);
|
|
942
|
+
if (minimum_status == OSEO_REGEXP_INVALID) {
|
|
943
|
+
return OSEO_REGEXP_INVALID;
|
|
944
|
+
}
|
|
945
|
+
if (cursor < source->length && source->units[cursor] == '}') {
|
|
946
|
+
*index = cursor;
|
|
947
|
+
return minimum_status;
|
|
948
|
+
}
|
|
949
|
+
if (cursor >= source->length || source->units[cursor] != ',') {
|
|
950
|
+
return OSEO_REGEXP_INVALID;
|
|
951
|
+
}
|
|
952
|
+
cursor += 1u;
|
|
953
|
+
size_t maximum_start = cursor;
|
|
954
|
+
while (cursor < source->length &&
|
|
955
|
+
regexp_ascii_digit(source->units[cursor])) {
|
|
956
|
+
cursor += 1u;
|
|
957
|
+
}
|
|
958
|
+
if (cursor >= source->length || source->units[cursor] != '}') {
|
|
959
|
+
return OSEO_REGEXP_INVALID;
|
|
960
|
+
}
|
|
961
|
+
if (maximum_start != cursor) {
|
|
962
|
+
uint64_t maximum = 0u;
|
|
963
|
+
OseoRegExpValidation maximum_status = regexp_decimal_bound(
|
|
964
|
+
source,
|
|
965
|
+
maximum_start,
|
|
966
|
+
cursor,
|
|
967
|
+
&maximum
|
|
968
|
+
);
|
|
969
|
+
bool reversed = false;
|
|
970
|
+
if (minimum_status == OSEO_REGEXP_VALID &&
|
|
971
|
+
maximum_status == OSEO_REGEXP_VALID) {
|
|
972
|
+
reversed = minimum > maximum;
|
|
973
|
+
} else if (minimum_status == OSEO_REGEXP_LIMIT &&
|
|
974
|
+
maximum_status == OSEO_REGEXP_VALID) {
|
|
975
|
+
reversed = true;
|
|
976
|
+
} else if (minimum_status == OSEO_REGEXP_VALID &&
|
|
977
|
+
maximum_status == OSEO_REGEXP_LIMIT) {
|
|
978
|
+
reversed = false;
|
|
979
|
+
} else {
|
|
980
|
+
reversed = regexp_bound_exceeds(
|
|
981
|
+
source,
|
|
982
|
+
minimum_start, minimum_end,
|
|
983
|
+
maximum_start, cursor
|
|
984
|
+
);
|
|
985
|
+
}
|
|
986
|
+
if (reversed) return OSEO_REGEXP_INVALID;
|
|
987
|
+
if (minimum_status == OSEO_REGEXP_LIMIT ||
|
|
988
|
+
maximum_status == OSEO_REGEXP_LIMIT) {
|
|
989
|
+
*index = cursor;
|
|
990
|
+
return OSEO_REGEXP_LIMIT;
|
|
991
|
+
}
|
|
992
|
+
} else if (minimum_status == OSEO_REGEXP_LIMIT) {
|
|
993
|
+
*index = cursor;
|
|
994
|
+
return OSEO_REGEXP_LIMIT;
|
|
995
|
+
}
|
|
996
|
+
*index = cursor;
|
|
997
|
+
return OSEO_REGEXP_VALID;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/*
|
|
1001
|
+
* Whether the `{` at `index` starts a braced quantifier's syntax, whatever
|
|
1002
|
+
* its bounds. Annex B reads any other `{` as a pattern character, while a
|
|
1003
|
+
* quantifier with nothing to repeat stays an early error.
|
|
1004
|
+
*/
|
|
1005
|
+
static bool regexp_braced_quantifier_follows(
|
|
1006
|
+
const OseoString *source,
|
|
1007
|
+
size_t index
|
|
1008
|
+
) {
|
|
1009
|
+
size_t cursor = index + 1u;
|
|
1010
|
+
size_t digits = cursor;
|
|
1011
|
+
while (cursor < source->length &&
|
|
1012
|
+
regexp_ascii_digit(source->units[cursor])) {
|
|
1013
|
+
cursor += 1u;
|
|
1014
|
+
}
|
|
1015
|
+
if (cursor == digits || cursor >= source->length) return false;
|
|
1016
|
+
if (source->units[cursor] == ',') {
|
|
1017
|
+
cursor += 1u;
|
|
1018
|
+
while (cursor < source->length &&
|
|
1019
|
+
regexp_ascii_digit(source->units[cursor])) {
|
|
1020
|
+
cursor += 1u;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
return cursor < source->length && source->units[cursor] == '}';
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
static OseoRegExpValidation regexp_validate_pattern(
|
|
1027
|
+
const OseoString *source,
|
|
1028
|
+
uint16_t flag_mask,
|
|
1029
|
+
size_t capture_count,
|
|
1030
|
+
const OseoRegExpNamedCaptureRegistry *named_captures
|
|
1031
|
+
) {
|
|
1032
|
+
if (source->length > OSEO_REGEXP_PATTERN_LENGTH_LIMIT) {
|
|
1033
|
+
return OSEO_REGEXP_LIMIT;
|
|
1034
|
+
}
|
|
1035
|
+
bool unicode = (flag_mask &
|
|
1036
|
+
(OSEO_REGEXP_FLAG_U | OSEO_REGEXP_FLAG_V)) != 0u;
|
|
1037
|
+
bool group_quantifiable[OSEO_REGEXP_NESTING_LIMIT];
|
|
1038
|
+
size_t depth = 0u;
|
|
1039
|
+
bool can_quantify = false;
|
|
1040
|
+
bool quantified = false;
|
|
1041
|
+
for (size_t index = 0u; index < source->length; index += 1u) {
|
|
1042
|
+
uint16_t unit = source->units[index];
|
|
1043
|
+
if (quantified && unit == '?') {
|
|
1044
|
+
quantified = false;
|
|
1045
|
+
can_quantify = false;
|
|
1046
|
+
continue;
|
|
1047
|
+
}
|
|
1048
|
+
quantified = false;
|
|
1049
|
+
if (unit == '\\') {
|
|
1050
|
+
size_t escape_start = index;
|
|
1051
|
+
OseoRegExpValidation status = regexp_escape(
|
|
1052
|
+
source,
|
|
1053
|
+
&index,
|
|
1054
|
+
flag_mask,
|
|
1055
|
+
capture_count,
|
|
1056
|
+
named_captures,
|
|
1057
|
+
false
|
|
1058
|
+
);
|
|
1059
|
+
if (status != OSEO_REGEXP_VALID) return status;
|
|
1060
|
+
uint16_t escape = source->units[escape_start + 1u];
|
|
1061
|
+
can_quantify = escape != 'b' && escape != 'B';
|
|
1062
|
+
continue;
|
|
1063
|
+
}
|
|
1064
|
+
if (unit == '[') {
|
|
1065
|
+
OseoRegExpValidation status = regexp_character_class(
|
|
1066
|
+
source,
|
|
1067
|
+
&index,
|
|
1068
|
+
flag_mask,
|
|
1069
|
+
capture_count,
|
|
1070
|
+
named_captures
|
|
1071
|
+
);
|
|
1072
|
+
if (status != OSEO_REGEXP_VALID) return status;
|
|
1073
|
+
can_quantify = true;
|
|
1074
|
+
continue;
|
|
1075
|
+
}
|
|
1076
|
+
if (unit == '(') {
|
|
1077
|
+
if (depth == OSEO_REGEXP_NESTING_LIMIT) {
|
|
1078
|
+
return OSEO_REGEXP_LIMIT;
|
|
1079
|
+
}
|
|
1080
|
+
bool group_can_quantify = true;
|
|
1081
|
+
if (index + 1u < source->length &&
|
|
1082
|
+
source->units[index + 1u] == '?') {
|
|
1083
|
+
if (index + 2u >= source->length) {
|
|
1084
|
+
return OSEO_REGEXP_INVALID;
|
|
1085
|
+
}
|
|
1086
|
+
uint16_t kind = source->units[index + 2u];
|
|
1087
|
+
if (kind == ':') {
|
|
1088
|
+
index += 2u;
|
|
1089
|
+
} else if (kind == '=' || kind == '!') {
|
|
1090
|
+
// Annex B quantifies a lookahead as an atom.
|
|
1091
|
+
group_can_quantify = !unicode;
|
|
1092
|
+
index += 2u;
|
|
1093
|
+
} else if (kind == '<') {
|
|
1094
|
+
if (index + 3u >= source->length) {
|
|
1095
|
+
return OSEO_REGEXP_INVALID;
|
|
1096
|
+
}
|
|
1097
|
+
uint16_t next = source->units[index + 3u];
|
|
1098
|
+
if (next == '=' || next == '!') {
|
|
1099
|
+
group_can_quantify = false;
|
|
1100
|
+
index += 3u;
|
|
1101
|
+
} else {
|
|
1102
|
+
size_t end = 0u;
|
|
1103
|
+
if (!regexp_group_name(source, index + 3u, &end)) {
|
|
1104
|
+
return OSEO_REGEXP_INVALID;
|
|
1105
|
+
}
|
|
1106
|
+
index = end;
|
|
1107
|
+
}
|
|
1108
|
+
} else if (kind == 'i' || kind == 'm' || kind == 's' ||
|
|
1109
|
+
kind == '-') {
|
|
1110
|
+
size_t mc = index + 2u;
|
|
1111
|
+
uint16_t add = 0u;
|
|
1112
|
+
uint16_t rem = 0u;
|
|
1113
|
+
bool removing = false;
|
|
1114
|
+
bool valid_mod = true;
|
|
1115
|
+
while (mc < source->length) {
|
|
1116
|
+
uint16_t mf = source->units[mc];
|
|
1117
|
+
if (mf == ':') break;
|
|
1118
|
+
if (mf == '-') {
|
|
1119
|
+
if (removing) {
|
|
1120
|
+
valid_mod = false;
|
|
1121
|
+
break;
|
|
1122
|
+
}
|
|
1123
|
+
removing = true;
|
|
1124
|
+
mc += 1u;
|
|
1125
|
+
continue;
|
|
1126
|
+
}
|
|
1127
|
+
uint16_t fb = 0u;
|
|
1128
|
+
if (mf == 'i') fb = OSEO_REGEXP_FLAG_I;
|
|
1129
|
+
else if (mf == 'm') fb = OSEO_REGEXP_FLAG_M;
|
|
1130
|
+
else if (mf == 's') fb = OSEO_REGEXP_FLAG_S;
|
|
1131
|
+
else { valid_mod = false; break; }
|
|
1132
|
+
uint16_t *tgt = removing ? &rem : &add;
|
|
1133
|
+
if ((*tgt & fb) != 0u) {
|
|
1134
|
+
valid_mod = false;
|
|
1135
|
+
break;
|
|
1136
|
+
}
|
|
1137
|
+
*tgt = (uint16_t)(*tgt | fb);
|
|
1138
|
+
mc += 1u;
|
|
1139
|
+
}
|
|
1140
|
+
if (!valid_mod ||
|
|
1141
|
+
mc >= source->length ||
|
|
1142
|
+
source->units[mc] != ':') {
|
|
1143
|
+
return OSEO_REGEXP_INVALID;
|
|
1144
|
+
}
|
|
1145
|
+
if ((add | rem) == 0u ||
|
|
1146
|
+
(add & rem) != 0u) {
|
|
1147
|
+
return OSEO_REGEXP_INVALID;
|
|
1148
|
+
}
|
|
1149
|
+
index = mc;
|
|
1150
|
+
} else {
|
|
1151
|
+
return OSEO_REGEXP_INVALID;
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
group_quantifiable[depth] = group_can_quantify;
|
|
1155
|
+
depth += 1u;
|
|
1156
|
+
can_quantify = false;
|
|
1157
|
+
continue;
|
|
1158
|
+
}
|
|
1159
|
+
if (unit == ')') {
|
|
1160
|
+
if (depth == 0u) return OSEO_REGEXP_INVALID;
|
|
1161
|
+
depth -= 1u;
|
|
1162
|
+
can_quantify = group_quantifiable[depth];
|
|
1163
|
+
continue;
|
|
1164
|
+
}
|
|
1165
|
+
if (unit == '|') {
|
|
1166
|
+
can_quantify = false;
|
|
1167
|
+
continue;
|
|
1168
|
+
}
|
|
1169
|
+
if (unit == '^' || unit == '$') {
|
|
1170
|
+
can_quantify = false;
|
|
1171
|
+
continue;
|
|
1172
|
+
}
|
|
1173
|
+
if (unit == '*' || unit == '+' || unit == '?') {
|
|
1174
|
+
if (!can_quantify) return OSEO_REGEXP_INVALID;
|
|
1175
|
+
can_quantify = false;
|
|
1176
|
+
quantified = true;
|
|
1177
|
+
continue;
|
|
1178
|
+
}
|
|
1179
|
+
if (unit == '{' && !unicode &&
|
|
1180
|
+
!regexp_braced_quantifier_follows(source, index)) {
|
|
1181
|
+
// Annex B's ExtendedPatternCharacter.
|
|
1182
|
+
can_quantify = true;
|
|
1183
|
+
continue;
|
|
1184
|
+
}
|
|
1185
|
+
if (unit == '{') {
|
|
1186
|
+
if (!can_quantify) return OSEO_REGEXP_INVALID;
|
|
1187
|
+
OseoRegExpValidation status = regexp_braced_quantifier(
|
|
1188
|
+
source,
|
|
1189
|
+
&index
|
|
1190
|
+
);
|
|
1191
|
+
if (status != OSEO_REGEXP_VALID) return status;
|
|
1192
|
+
can_quantify = false;
|
|
1193
|
+
quantified = true;
|
|
1194
|
+
continue;
|
|
1195
|
+
}
|
|
1196
|
+
if ((unit == ']' || unit == '}') && unicode) {
|
|
1197
|
+
return OSEO_REGEXP_INVALID;
|
|
1198
|
+
}
|
|
1199
|
+
can_quantify = true;
|
|
1200
|
+
}
|
|
1201
|
+
if (depth != 0u) return OSEO_REGEXP_INVALID;
|
|
1202
|
+
if (source->length > OSEO_REGEXP_MATCHER_INSTRUCTION_LIMIT - 1u ||
|
|
1203
|
+
capture_count >
|
|
1204
|
+
(OSEO_REGEXP_MATCHER_INSTRUCTION_LIMIT - source->length - 1u) /
|
|
1205
|
+
2u) {
|
|
1206
|
+
return OSEO_REGEXP_LIMIT;
|
|
1207
|
+
}
|
|
1208
|
+
return OSEO_REGEXP_VALID;
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
static OseoResult regexp_validation_result(
|
|
1212
|
+
OseoContext *context,
|
|
1213
|
+
OseoRegExpValidation status
|
|
1214
|
+
) {
|
|
1215
|
+
if (status == OSEO_REGEXP_INVALID) {
|
|
1216
|
+
return oseo_internal_throw_error(
|
|
1217
|
+
context,
|
|
1218
|
+
OSEO_ERROR_SYNTAX,
|
|
1219
|
+
"Invalid regular expression pattern or flags."
|
|
1220
|
+
);
|
|
1221
|
+
}
|
|
1222
|
+
if (status == OSEO_REGEXP_UNSUPPORTED) {
|
|
1223
|
+
return failure(
|
|
1224
|
+
context,
|
|
1225
|
+
"OSEO2001",
|
|
1226
|
+
"Regular expression pattern extension is not admitted yet."
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
if (status == OSEO_REGEXP_ALLOCATION_FAILURE) {
|
|
1230
|
+
return failure(
|
|
1231
|
+
context,
|
|
1232
|
+
"OSEO2001",
|
|
1233
|
+
"RegExp pattern validation allocation failed."
|
|
1234
|
+
);
|
|
1235
|
+
}
|
|
1236
|
+
return failure(
|
|
1237
|
+
context,
|
|
1238
|
+
"OSEO2001",
|
|
1239
|
+
"Regular expression pattern exceeds the reviewed matcher limit."
|
|
1240
|
+
);
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
OseoResult oseo_internal_is_regexp(
|
|
1244
|
+
OseoContext *context,
|
|
1245
|
+
OseoValue value,
|
|
1246
|
+
bool *regexp
|
|
1247
|
+
) {
|
|
1248
|
+
if (!is_object(value)) {
|
|
1249
|
+
*regexp = false;
|
|
1250
|
+
return normal(oseo_boolean(false));
|
|
1251
|
+
}
|
|
1252
|
+
OseoValue slots[2] = {value, oseo_undefined()};
|
|
1253
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
1254
|
+
oseo_roots_push(context, &frame);
|
|
1255
|
+
OseoResult result = oseo_internal_well_known_symbol(
|
|
1256
|
+
context,
|
|
1257
|
+
OSEO_WELL_KNOWN_MATCH
|
|
1258
|
+
);
|
|
1259
|
+
frame.slots[1] = result.value;
|
|
1260
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1261
|
+
result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
|
|
1262
|
+
}
|
|
1263
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
1264
|
+
tag_of(result.value) != OSEO_TAG_UNDEFINED) {
|
|
1265
|
+
*regexp = oseo_to_boolean(result.value);
|
|
1266
|
+
} else if (result.status == OSEO_STATUS_NORMAL) {
|
|
1267
|
+
*regexp = is_regexp(frame.slots[0]);
|
|
1268
|
+
}
|
|
1269
|
+
oseo_roots_pop(context, &frame);
|
|
1270
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
1271
|
+
? normal(oseo_boolean(*regexp))
|
|
1272
|
+
: result;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
static OseoResult regexp_prototype_from_target(
|
|
1276
|
+
OseoContext *context,
|
|
1277
|
+
OseoValue new_target,
|
|
1278
|
+
OseoValue *prototype
|
|
1279
|
+
) {
|
|
1280
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1281
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
1282
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1283
|
+
frame.slots[0] = new_target;
|
|
1284
|
+
result = oseo_internal_ascii_string(context, "prototype");
|
|
1285
|
+
frame.slots[1] = result.value;
|
|
1286
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1287
|
+
result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
|
|
1288
|
+
frame.slots[1] = result.value;
|
|
1289
|
+
}
|
|
1290
|
+
if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[1])) {
|
|
1291
|
+
result = oseo_internal_constructor_realm_default(
|
|
1292
|
+
context,
|
|
1293
|
+
frame.slots[0],
|
|
1294
|
+
OSEO_INTRINSIC_REGEXP_PROTOTYPE
|
|
1295
|
+
);
|
|
1296
|
+
frame.slots[1] = result.value;
|
|
1297
|
+
}
|
|
1298
|
+
if (result.status == OSEO_STATUS_NORMAL) *prototype = frame.slots[1];
|
|
1299
|
+
oseo_roots_release(context, &frame);
|
|
1300
|
+
return result;
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
static void regexp_initialize_ordinary(
|
|
1304
|
+
OseoContext *context,
|
|
1305
|
+
OseoOrdinaryObject *object,
|
|
1306
|
+
OseoValue prototype
|
|
1307
|
+
) {
|
|
1308
|
+
object->prototype = prototype;
|
|
1309
|
+
object->properties = NULL;
|
|
1310
|
+
object->property_capacity = 0u;
|
|
1311
|
+
object->property_count = 0u;
|
|
1312
|
+
oseo_internal_init_dense_index(object);
|
|
1313
|
+
object->private_elements = NULL;
|
|
1314
|
+
object->private_element_capacity = 0u;
|
|
1315
|
+
object->private_element_count = 0u;
|
|
1316
|
+
object->shape_id = context->next_shape_id;
|
|
1317
|
+
context->next_shape_id += 1u;
|
|
1318
|
+
object->array_length = 0u;
|
|
1319
|
+
object->dictionary = false;
|
|
1320
|
+
object->length_writable = false;
|
|
1321
|
+
object->extensible = true;
|
|
1322
|
+
object->module_namespace = false;
|
|
1323
|
+
object->immutable_prototype = false;
|
|
1324
|
+
object->global_object = false;
|
|
1325
|
+
object->error_data = false;
|
|
1326
|
+
object->number_data = false;
|
|
1327
|
+
object->number_value = oseo_undefined();
|
|
1328
|
+
object->primitive_data = false;
|
|
1329
|
+
object->primitive_value = oseo_undefined();
|
|
1330
|
+
object->primitive_wrapper_methods_initialized = false;
|
|
1331
|
+
object->virtual_string_iterator = false;
|
|
1332
|
+
object->virtual_string_iterator_configurable = false;
|
|
1333
|
+
object->virtual_string_iterator_enumerable = false;
|
|
1334
|
+
object->virtual_string_iterator_writable = false;
|
|
1335
|
+
object->iterator_kind = OSEO_ITERATOR_NONE;
|
|
1336
|
+
object->iterator_target = oseo_undefined();
|
|
1337
|
+
object->iterator_index = 0u;
|
|
1338
|
+
object->regexp_string_iterator = false;
|
|
1339
|
+
object->regexp_iterator_regexp = oseo_undefined();
|
|
1340
|
+
object->regexp_iterator_subject = oseo_undefined();
|
|
1341
|
+
object->regexp_iterator_global = false;
|
|
1342
|
+
object->regexp_iterator_unicode = false;
|
|
1343
|
+
object->regexp_iterator_complete = false;
|
|
1344
|
+
object->async_from_sync = false;
|
|
1345
|
+
object->async_sync_iterator = oseo_undefined();
|
|
1346
|
+
object->wrap_for_valid_iterator = false;
|
|
1347
|
+
object->wrapped_iterator = oseo_undefined();
|
|
1348
|
+
object->wrapped_next = oseo_undefined();
|
|
1349
|
+
object->arguments_object = false;
|
|
1350
|
+
object->mapped_arguments = false;
|
|
1351
|
+
object->generator = NULL;
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
/*
|
|
1355
|
+
* RegExpAlloc over a resolved prototype, including the non-enumerable
|
|
1356
|
+
* own lastIndex property. Construction resolves the prototype from its
|
|
1357
|
+
* new target; an ahead-of-time literal takes %RegExp.prototype% directly,
|
|
1358
|
+
* which is what GetPrototypeFromConstructor reaches for %RegExp% because
|
|
1359
|
+
* its `prototype` property is neither writable nor configurable.
|
|
1360
|
+
*/
|
|
1361
|
+
static OseoResult regexp_allocate_from(
|
|
1362
|
+
OseoContext *context,
|
|
1363
|
+
OseoValue prototype
|
|
1364
|
+
) {
|
|
1365
|
+
OseoValue slots[2] = {prototype, oseo_undefined()};
|
|
1366
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
1367
|
+
oseo_roots_push(context, &frame);
|
|
1368
|
+
OseoResult result = normal(oseo_undefined());
|
|
1369
|
+
OseoRegExp *regexp =
|
|
1370
|
+
oseo_internal_allocate_heap_bytes(context, sizeof(*regexp));
|
|
1371
|
+
if (regexp == NULL) {
|
|
1372
|
+
result = failure(context, "OSEO2001", "RegExp allocation failed.");
|
|
1373
|
+
} else {
|
|
1374
|
+
regexp_initialize_ordinary(context, ®exp->ordinary, frame.slots[0]);
|
|
1375
|
+
regexp->matcher = oseo_undefined();
|
|
1376
|
+
result = oseo_internal_publish_heap(
|
|
1377
|
+
context,
|
|
1378
|
+
®exp->ordinary.header,
|
|
1379
|
+
OSEO_HEAP_REGEXP
|
|
1380
|
+
);
|
|
1381
|
+
frame.slots[0] = result.value;
|
|
1382
|
+
}
|
|
1383
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1384
|
+
result = oseo_internal_ascii_string(context, "lastIndex");
|
|
1385
|
+
frame.slots[1] = result.value;
|
|
1386
|
+
}
|
|
1387
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1388
|
+
result = oseo_object_define(
|
|
1389
|
+
context,
|
|
1390
|
+
frame.slots[0],
|
|
1391
|
+
frame.slots[1],
|
|
1392
|
+
oseo_number(0.0),
|
|
1393
|
+
(OseoPropertyAttributes){false, false, true, false}
|
|
1394
|
+
);
|
|
1395
|
+
}
|
|
1396
|
+
if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
|
|
1397
|
+
oseo_roots_pop(context, &frame);
|
|
1398
|
+
return result;
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
/* RegExpAlloc for the constructor, which resolves its own prototype. */
|
|
1402
|
+
static OseoResult regexp_allocate(
|
|
1403
|
+
OseoContext *context,
|
|
1404
|
+
OseoValue new_target
|
|
1405
|
+
) {
|
|
1406
|
+
OseoValue slots[2] = {new_target, oseo_undefined()};
|
|
1407
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
1408
|
+
oseo_roots_push(context, &frame);
|
|
1409
|
+
OseoResult result = regexp_prototype_from_target(
|
|
1410
|
+
context,
|
|
1411
|
+
frame.slots[0],
|
|
1412
|
+
&frame.slots[1]
|
|
1413
|
+
);
|
|
1414
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1415
|
+
result = regexp_allocate_from(context, frame.slots[1]);
|
|
1416
|
+
}
|
|
1417
|
+
oseo_roots_pop(context, &frame);
|
|
1418
|
+
return result;
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
/*
|
|
1422
|
+
* The flags that decide how a pattern parses and what its program does.
|
|
1423
|
+
* `d`, `g`, and `y` only steer RegExpBuiltinExec on the object that
|
|
1424
|
+
* carries them, so no compiled program depends on them.
|
|
1425
|
+
*/
|
|
1426
|
+
#define OSEO_REGEXP_PARSE_FLAGS \
|
|
1427
|
+
(OSEO_REGEXP_FLAG_I | OSEO_REGEXP_FLAG_M | OSEO_REGEXP_FLAG_S | \
|
|
1428
|
+
OSEO_REGEXP_FLAG_U | OSEO_REGEXP_FLAG_V)
|
|
1429
|
+
|
|
1430
|
+
/*
|
|
1431
|
+
* Whether a new matcher can borrow the program of `origin`.
|
|
1432
|
+
*
|
|
1433
|
+
* RegExpInitialize parses [[OriginalSource]] again, but parsing the same
|
|
1434
|
+
* text under the same parse flags reaches the same early errors and the
|
|
1435
|
+
* same matcher. Only a program the build compiled ahead of time is
|
|
1436
|
+
* borrowed: it is immutable generated data that outlives every artifact,
|
|
1437
|
+
* while a dynamic program belongs to the one artifact that compiled it.
|
|
1438
|
+
*/
|
|
1439
|
+
static bool regexp_matcher_reusable(
|
|
1440
|
+
OseoValue origin,
|
|
1441
|
+
OseoValue source,
|
|
1442
|
+
uint16_t flag_mask
|
|
1443
|
+
) {
|
|
1444
|
+
if (tag_of(origin) == OSEO_TAG_UNDEFINED) return false;
|
|
1445
|
+
const OseoRegExpMatcher *matcher = regexp_matcher_object(origin);
|
|
1446
|
+
return !matcher->owns_program && matcher->source == source &&
|
|
1447
|
+
((matcher->flag_mask ^ flag_mask) & OSEO_REGEXP_PARSE_FLAGS) == 0u;
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
/* A fresh artifact over the static program `origin` borrows. */
|
|
1451
|
+
static OseoResult regexp_matcher_borrow(
|
|
1452
|
+
OseoContext *context,
|
|
1453
|
+
OseoValue origin,
|
|
1454
|
+
OseoValue source,
|
|
1455
|
+
OseoValue flags,
|
|
1456
|
+
uint16_t flag_mask
|
|
1457
|
+
) {
|
|
1458
|
+
OseoValue slots[3] = {origin, source, flags};
|
|
1459
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
1460
|
+
oseo_roots_push(context, &frame);
|
|
1461
|
+
OseoRegExpMatcher *matcher = oseo_internal_allocate_heap_bytes(
|
|
1462
|
+
context,
|
|
1463
|
+
sizeof(*matcher)
|
|
1464
|
+
);
|
|
1465
|
+
OseoResult result;
|
|
1466
|
+
if (matcher == NULL) {
|
|
1467
|
+
result = failure(
|
|
1468
|
+
context,
|
|
1469
|
+
"OSEO2001",
|
|
1470
|
+
"RegExp matcher allocation failed."
|
|
1471
|
+
);
|
|
1472
|
+
} else {
|
|
1473
|
+
const OseoRegExpMatcher *borrowed =
|
|
1474
|
+
regexp_matcher_object(slots[0]);
|
|
1475
|
+
matcher->source = slots[1];
|
|
1476
|
+
matcher->flags = slots[2];
|
|
1477
|
+
matcher->program = borrowed->program;
|
|
1478
|
+
matcher->capture_count = borrowed->capture_count;
|
|
1479
|
+
matcher->instruction_count = borrowed->instruction_count;
|
|
1480
|
+
matcher->flag_mask = flag_mask;
|
|
1481
|
+
matcher->owns_program = false;
|
|
1482
|
+
result = oseo_internal_publish_heap(
|
|
1483
|
+
context,
|
|
1484
|
+
&matcher->header,
|
|
1485
|
+
OSEO_HEAP_REGEXP_MATCHER
|
|
1486
|
+
);
|
|
1487
|
+
}
|
|
1488
|
+
oseo_roots_pop(context, &frame);
|
|
1489
|
+
return result;
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
static OseoResult regexp_matcher_create(
|
|
1493
|
+
OseoContext *context,
|
|
1494
|
+
OseoValue source,
|
|
1495
|
+
OseoValue flags,
|
|
1496
|
+
OseoValue origin
|
|
1497
|
+
) {
|
|
1498
|
+
uint16_t flag_mask = 0u;
|
|
1499
|
+
OseoRegExpValidation status = regexp_validate_flags(
|
|
1500
|
+
string_object(flags),
|
|
1501
|
+
&flag_mask
|
|
1502
|
+
);
|
|
1503
|
+
if (status == OSEO_REGEXP_VALID &&
|
|
1504
|
+
regexp_matcher_reusable(origin, source, flag_mask)) {
|
|
1505
|
+
return regexp_matcher_borrow(
|
|
1506
|
+
context,
|
|
1507
|
+
origin,
|
|
1508
|
+
source,
|
|
1509
|
+
flags,
|
|
1510
|
+
flag_mask
|
|
1511
|
+
);
|
|
1512
|
+
}
|
|
1513
|
+
if (status == OSEO_REGEXP_VALID &&
|
|
1514
|
+
string_object(source)->length > OSEO_REGEXP_PATTERN_LENGTH_LIMIT) {
|
|
1515
|
+
status = OSEO_REGEXP_LIMIT;
|
|
1516
|
+
}
|
|
1517
|
+
size_t capture_count = 0u;
|
|
1518
|
+
if (status == OSEO_REGEXP_VALID) {
|
|
1519
|
+
status = regexp_count_captures(
|
|
1520
|
+
string_object(source),
|
|
1521
|
+
&capture_count
|
|
1522
|
+
);
|
|
1523
|
+
}
|
|
1524
|
+
OseoRegExpNamedCaptureRegistry named_captures = {
|
|
1525
|
+
.storage = NULL,
|
|
1526
|
+
.buckets = NULL,
|
|
1527
|
+
.records = NULL,
|
|
1528
|
+
.paths = NULL,
|
|
1529
|
+
.names = NULL,
|
|
1530
|
+
.reference = NULL,
|
|
1531
|
+
.name_count = 0u,
|
|
1532
|
+
.bucket_count = 0u,
|
|
1533
|
+
.count = 0u,
|
|
1534
|
+
.path_count = 0u,
|
|
1535
|
+
.duplicate_status = OSEO_REGEXP_VALID,
|
|
1536
|
+
};
|
|
1537
|
+
if (status == OSEO_REGEXP_VALID) {
|
|
1538
|
+
status = regexp_named_capture_registry_create(
|
|
1539
|
+
context,
|
|
1540
|
+
string_object(source),
|
|
1541
|
+
&named_captures
|
|
1542
|
+
);
|
|
1543
|
+
}
|
|
1544
|
+
if (status == OSEO_REGEXP_VALID) {
|
|
1545
|
+
status = regexp_validate_pattern(
|
|
1546
|
+
string_object(source),
|
|
1547
|
+
flag_mask,
|
|
1548
|
+
capture_count,
|
|
1549
|
+
&named_captures
|
|
1550
|
+
);
|
|
1551
|
+
}
|
|
1552
|
+
if (status == OSEO_REGEXP_VALID &&
|
|
1553
|
+
named_captures.duplicate_status != OSEO_REGEXP_VALID) {
|
|
1554
|
+
status = named_captures.duplicate_status;
|
|
1555
|
+
}
|
|
1556
|
+
regexp_named_capture_registry_destroy(&named_captures);
|
|
1557
|
+
if (status != OSEO_REGEXP_VALID) {
|
|
1558
|
+
return regexp_validation_result(context, status);
|
|
1559
|
+
}
|
|
1560
|
+
/*
|
|
1561
|
+
* The program is compiled before the artifact is allocated. It is
|
|
1562
|
+
* unmanaged memory, so building it reaches no safepoint, and a
|
|
1563
|
+
* failure after it exists releases it rather than publishing an
|
|
1564
|
+
* artifact that owns nothing.
|
|
1565
|
+
*/
|
|
1566
|
+
OseoRegExpProgram *program = oseo_internal_regexp_program_build(
|
|
1567
|
+
context,
|
|
1568
|
+
string_object(source),
|
|
1569
|
+
flag_mask,
|
|
1570
|
+
capture_count,
|
|
1571
|
+
&status
|
|
1572
|
+
);
|
|
1573
|
+
if (program == NULL) return regexp_validation_result(context, status);
|
|
1574
|
+
OseoValue slots[2] = {source, flags};
|
|
1575
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
1576
|
+
oseo_roots_push(context, &frame);
|
|
1577
|
+
OseoRegExpMatcher *matcher = oseo_internal_allocate_heap_bytes(
|
|
1578
|
+
context,
|
|
1579
|
+
sizeof(*matcher)
|
|
1580
|
+
);
|
|
1581
|
+
OseoResult result;
|
|
1582
|
+
if (matcher == NULL) {
|
|
1583
|
+
oseo_internal_regexp_program_release(program);
|
|
1584
|
+
result = failure(
|
|
1585
|
+
context,
|
|
1586
|
+
"OSEO2001",
|
|
1587
|
+
"RegExp matcher allocation failed."
|
|
1588
|
+
);
|
|
1589
|
+
} else {
|
|
1590
|
+
matcher->source = slots[0];
|
|
1591
|
+
matcher->flags = slots[1];
|
|
1592
|
+
matcher->program = program;
|
|
1593
|
+
matcher->capture_count = capture_count;
|
|
1594
|
+
matcher->instruction_count = program->instruction_count;
|
|
1595
|
+
matcher->flag_mask = flag_mask;
|
|
1596
|
+
matcher->owns_program = true;
|
|
1597
|
+
result = oseo_internal_publish_heap(
|
|
1598
|
+
context,
|
|
1599
|
+
&matcher->header,
|
|
1600
|
+
OSEO_HEAP_REGEXP_MATCHER
|
|
1601
|
+
);
|
|
1602
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1603
|
+
oseo_internal_regexp_program_release(program);
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
oseo_roots_pop(context, &frame);
|
|
1607
|
+
return result;
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
/*
|
|
1611
|
+
* RegExpInitialize converts only after RegExpAlloc has completed.
|
|
1612
|
+
* `origin` is the matcher of the RegExp a pattern was copied from, or
|
|
1613
|
+
* undefined, and only lets an equal parse borrow its program.
|
|
1614
|
+
*/
|
|
1615
|
+
static OseoResult regexp_initialize(
|
|
1616
|
+
OseoContext *context,
|
|
1617
|
+
OseoValue regexp,
|
|
1618
|
+
OseoValue pattern,
|
|
1619
|
+
OseoValue flags,
|
|
1620
|
+
OseoValue origin
|
|
1621
|
+
) {
|
|
1622
|
+
OseoValue slots[6] = {
|
|
1623
|
+
regexp,
|
|
1624
|
+
pattern,
|
|
1625
|
+
flags,
|
|
1626
|
+
oseo_undefined(),
|
|
1627
|
+
oseo_undefined(),
|
|
1628
|
+
origin,
|
|
1629
|
+
};
|
|
1630
|
+
OseoRootFrame frame = {NULL, slots, 6u};
|
|
1631
|
+
oseo_roots_push(context, &frame);
|
|
1632
|
+
OseoResult result = tag_of(frame.slots[1]) == OSEO_TAG_UNDEFINED
|
|
1633
|
+
? oseo_internal_ascii_string(context, "")
|
|
1634
|
+
: oseo_internal_value_string(context, frame.slots[1]);
|
|
1635
|
+
frame.slots[3] = result.value;
|
|
1636
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1637
|
+
result = tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED
|
|
1638
|
+
? oseo_internal_ascii_string(context, "")
|
|
1639
|
+
: oseo_internal_value_string(context, frame.slots[2]);
|
|
1640
|
+
frame.slots[4] = result.value;
|
|
1641
|
+
}
|
|
1642
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1643
|
+
result = regexp_matcher_create(
|
|
1644
|
+
context,
|
|
1645
|
+
frame.slots[3],
|
|
1646
|
+
frame.slots[4],
|
|
1647
|
+
frame.slots[5]
|
|
1648
|
+
);
|
|
1649
|
+
frame.slots[3] = result.value;
|
|
1650
|
+
}
|
|
1651
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1652
|
+
regexp_object(frame.slots[0])->matcher = frame.slots[3];
|
|
1653
|
+
result.value = frame.slots[0];
|
|
1654
|
+
}
|
|
1655
|
+
oseo_roots_pop(context, &frame);
|
|
1656
|
+
return result;
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
OseoResult oseo_internal_regexp_create(
|
|
1660
|
+
OseoContext *context,
|
|
1661
|
+
OseoValue pattern,
|
|
1662
|
+
OseoValue flags
|
|
1663
|
+
) {
|
|
1664
|
+
OseoValue slots[3] = {pattern, flags, oseo_undefined()};
|
|
1665
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
1666
|
+
oseo_roots_push(context, &frame);
|
|
1667
|
+
OseoResult result = oseo_internal_intrinsic(
|
|
1668
|
+
context,
|
|
1669
|
+
OSEO_INTRINSIC_REGEXP_PROTOTYPE
|
|
1670
|
+
);
|
|
1671
|
+
slots[2] = result.value;
|
|
1672
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1673
|
+
result = regexp_allocate_from(context, slots[2]);
|
|
1674
|
+
slots[2] = result.value;
|
|
1675
|
+
}
|
|
1676
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1677
|
+
result = regexp_initialize(
|
|
1678
|
+
context,
|
|
1679
|
+
slots[2],
|
|
1680
|
+
slots[0],
|
|
1681
|
+
slots[1],
|
|
1682
|
+
oseo_undefined()
|
|
1683
|
+
);
|
|
1684
|
+
}
|
|
1685
|
+
oseo_roots_pop(context, &frame);
|
|
1686
|
+
return result;
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
/*
|
|
1690
|
+
* The table slot one literal descriptor owns.
|
|
1691
|
+
*
|
|
1692
|
+
* Open addressing over a power-of-two table keeps the search bounded by
|
|
1693
|
+
* the cluster around one address rather than by how many literal sites
|
|
1694
|
+
* the program has. The table is never full, so the walk always reaches
|
|
1695
|
+
* either the descriptor or the empty slot it would occupy.
|
|
1696
|
+
*/
|
|
1697
|
+
static size_t regexp_literal_slot(
|
|
1698
|
+
const OseoRegExpLiteralCacheEntry *cache,
|
|
1699
|
+
size_t capacity,
|
|
1700
|
+
const OseoRegExpLiteral *literal
|
|
1701
|
+
) {
|
|
1702
|
+
uintptr_t bits = (uintptr_t)literal;
|
|
1703
|
+
size_t mask = capacity - 1u;
|
|
1704
|
+
size_t index = (size_t)((bits >> 4u) ^ (bits >> 12u)) & mask;
|
|
1705
|
+
while (cache[index].literal != NULL &&
|
|
1706
|
+
cache[index].literal != literal) {
|
|
1707
|
+
index = (index + 1u) & mask;
|
|
1708
|
+
}
|
|
1709
|
+
return index;
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
/*
|
|
1713
|
+
* Reserve room for one more artifact before any of it is published.
|
|
1714
|
+
*
|
|
1715
|
+
* The table grows through the work-area allocator, so the deterministic
|
|
1716
|
+
* allocation-attempt sweep fails this allocation like every other one the
|
|
1717
|
+
* literal path takes. Reserving before the artifact exists is what lets
|
|
1718
|
+
* the insertion afterwards be infallible: an artifact this realm has
|
|
1719
|
+
* published is always the one every later evaluation of that literal
|
|
1720
|
+
* finds.
|
|
1721
|
+
*/
|
|
1722
|
+
static OseoResult regexp_literal_cache_reserve(OseoContext *context) {
|
|
1723
|
+
OseoRegExpLiteralCacheEntry *cache = context->realm->regexp_literal_cache;
|
|
1724
|
+
size_t capacity = context->realm->regexp_literal_cache_capacity;
|
|
1725
|
+
size_t count = context->realm->regexp_literal_cache_count;
|
|
1726
|
+
if (cache != NULL && (count + 1u) * 2u <= capacity) {
|
|
1727
|
+
return normal(oseo_undefined());
|
|
1728
|
+
}
|
|
1729
|
+
size_t grown = capacity == 0u ? 8u : capacity * 2u;
|
|
1730
|
+
if (grown < capacity || grown > SIZE_MAX / sizeof(*cache)) {
|
|
1731
|
+
return failure(
|
|
1732
|
+
context,
|
|
1733
|
+
"OSEO2001",
|
|
1734
|
+
"RegExp literal cache is too large."
|
|
1735
|
+
);
|
|
1736
|
+
}
|
|
1737
|
+
OseoRegExpLiteralCacheEntry *table = oseo_internal_allocate_work_bytes(
|
|
1738
|
+
context,
|
|
1739
|
+
grown * sizeof(*table)
|
|
1740
|
+
);
|
|
1741
|
+
if (table == NULL) {
|
|
1742
|
+
return failure(
|
|
1743
|
+
context,
|
|
1744
|
+
"OSEO2001",
|
|
1745
|
+
"RegExp literal cache allocation failed."
|
|
1746
|
+
);
|
|
1747
|
+
}
|
|
1748
|
+
/*
|
|
1749
|
+
* An all-zero representation is not portably a null pointer, and the
|
|
1750
|
+
* probe reads `literal` to find an empty slot, so every slot is
|
|
1751
|
+
* initialized rather than cleared.
|
|
1752
|
+
*/
|
|
1753
|
+
for (size_t index = 0u; index < grown; index += 1u) {
|
|
1754
|
+
table[index].literal = NULL;
|
|
1755
|
+
table[index].matcher = oseo_undefined();
|
|
1756
|
+
}
|
|
1757
|
+
for (size_t index = 0u; index < capacity; index += 1u) {
|
|
1758
|
+
if (cache[index].literal == NULL) continue;
|
|
1759
|
+
table[regexp_literal_slot(table, grown, cache[index].literal)] =
|
|
1760
|
+
cache[index];
|
|
1761
|
+
}
|
|
1762
|
+
free(cache);
|
|
1763
|
+
context->realm->regexp_literal_cache = table;
|
|
1764
|
+
context->realm->regexp_literal_cache_capacity = grown;
|
|
1765
|
+
return normal(oseo_undefined());
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
/*
|
|
1769
|
+
* The realm's immutable matcher artifact for one literal descriptor.
|
|
1770
|
+
*
|
|
1771
|
+
* The program the descriptor names was compiled during the build, so this
|
|
1772
|
+
* only wraps it with the source and flag strings the `source`, `flags`,
|
|
1773
|
+
* and `toString` behavior reports. Nothing here parses or compiles a
|
|
1774
|
+
* pattern, and the artifact holds no per-evaluation state.
|
|
1775
|
+
*/
|
|
1776
|
+
static OseoResult regexp_literal_matcher(
|
|
1777
|
+
OseoContext *context,
|
|
1778
|
+
const OseoRegExpLiteral *literal
|
|
1779
|
+
) {
|
|
1780
|
+
OseoRegExpLiteralCacheEntry *cache = context->realm->regexp_literal_cache;
|
|
1781
|
+
if (cache != NULL) {
|
|
1782
|
+
size_t found = regexp_literal_slot(
|
|
1783
|
+
cache,
|
|
1784
|
+
context->realm->regexp_literal_cache_capacity,
|
|
1785
|
+
literal
|
|
1786
|
+
);
|
|
1787
|
+
if (cache[found].literal == literal) {
|
|
1788
|
+
return normal(cache[found].matcher);
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
OseoResult result = regexp_literal_cache_reserve(context);
|
|
1792
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1793
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1794
|
+
result = oseo_roots_allocate(context, &frame, 3u);
|
|
1795
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1796
|
+
result = oseo_string_from_units(
|
|
1797
|
+
context,
|
|
1798
|
+
literal->source_units,
|
|
1799
|
+
literal->source_length
|
|
1800
|
+
);
|
|
1801
|
+
frame.slots[0] = result.value;
|
|
1802
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1803
|
+
result = oseo_string_from_units(
|
|
1804
|
+
context,
|
|
1805
|
+
literal->flag_units,
|
|
1806
|
+
literal->flag_length
|
|
1807
|
+
);
|
|
1808
|
+
frame.slots[1] = result.value;
|
|
1809
|
+
}
|
|
1810
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1811
|
+
OseoRegExpMatcher *matcher = oseo_internal_allocate_heap_bytes(
|
|
1812
|
+
context,
|
|
1813
|
+
sizeof(*matcher)
|
|
1814
|
+
);
|
|
1815
|
+
if (matcher == NULL) {
|
|
1816
|
+
result = failure(
|
|
1817
|
+
context,
|
|
1818
|
+
"OSEO2001",
|
|
1819
|
+
"RegExp matcher allocation failed."
|
|
1820
|
+
);
|
|
1821
|
+
} else {
|
|
1822
|
+
matcher->source = frame.slots[0];
|
|
1823
|
+
matcher->flags = frame.slots[1];
|
|
1824
|
+
matcher->program = literal->program;
|
|
1825
|
+
matcher->capture_count = literal->program->capture_count;
|
|
1826
|
+
matcher->instruction_count =
|
|
1827
|
+
literal->program->instruction_count;
|
|
1828
|
+
matcher->flag_mask = literal->flag_mask;
|
|
1829
|
+
matcher->owns_program = false;
|
|
1830
|
+
result = oseo_internal_publish_heap(
|
|
1831
|
+
context,
|
|
1832
|
+
&matcher->header,
|
|
1833
|
+
OSEO_HEAP_REGEXP_MATCHER
|
|
1834
|
+
);
|
|
1835
|
+
}
|
|
1836
|
+
frame.slots[2] = result.value;
|
|
1837
|
+
}
|
|
1838
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1839
|
+
/*
|
|
1840
|
+
* The reservation above guarantees an empty slot, and nothing
|
|
1841
|
+
* between it and here inserts, so this cannot fail or displace
|
|
1842
|
+
* another descriptor.
|
|
1843
|
+
*/
|
|
1844
|
+
cache = context->realm->regexp_literal_cache;
|
|
1845
|
+
size_t slot = regexp_literal_slot(
|
|
1846
|
+
cache,
|
|
1847
|
+
context->realm->regexp_literal_cache_capacity,
|
|
1848
|
+
literal
|
|
1849
|
+
);
|
|
1850
|
+
cache[slot].literal = literal;
|
|
1851
|
+
cache[slot].matcher = frame.slots[2];
|
|
1852
|
+
context->realm->regexp_literal_cache_count += 1u;
|
|
1853
|
+
result = normal(frame.slots[2]);
|
|
1854
|
+
}
|
|
1855
|
+
oseo_roots_release(context, &frame);
|
|
1856
|
+
return result;
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
OseoResult oseo_regexp_literal(
|
|
1860
|
+
OseoContext *context,
|
|
1861
|
+
const OseoRegExpLiteral *literal
|
|
1862
|
+
) {
|
|
1863
|
+
if (literal == NULL || literal->program == NULL) {
|
|
1864
|
+
return failure(
|
|
1865
|
+
context,
|
|
1866
|
+
"OSEO2001",
|
|
1867
|
+
"Invalid regular expression literal."
|
|
1868
|
+
);
|
|
1869
|
+
}
|
|
1870
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1871
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
1872
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1873
|
+
result = regexp_literal_matcher(context, literal);
|
|
1874
|
+
frame.slots[0] = result.value;
|
|
1875
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1876
|
+
result = oseo_internal_intrinsic(
|
|
1877
|
+
context,
|
|
1878
|
+
OSEO_INTRINSIC_REGEXP_PROTOTYPE
|
|
1879
|
+
);
|
|
1880
|
+
frame.slots[1] = result.value;
|
|
1881
|
+
}
|
|
1882
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1883
|
+
result = regexp_allocate_from(context, frame.slots[1]);
|
|
1884
|
+
frame.slots[1] = result.value;
|
|
1885
|
+
}
|
|
1886
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1887
|
+
regexp_object(frame.slots[1])->matcher = frame.slots[0];
|
|
1888
|
+
result = normal(frame.slots[1]);
|
|
1889
|
+
}
|
|
1890
|
+
oseo_roots_release(context, &frame);
|
|
1891
|
+
return result;
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
static OseoResult regexp_get_named(
|
|
1895
|
+
OseoContext *context,
|
|
1896
|
+
OseoValue object,
|
|
1897
|
+
const char *name,
|
|
1898
|
+
OseoValue *value
|
|
1899
|
+
) {
|
|
1900
|
+
OseoValue slots[2] = {object, oseo_undefined()};
|
|
1901
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
1902
|
+
oseo_roots_push(context, &frame);
|
|
1903
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
1904
|
+
frame.slots[1] = result.value;
|
|
1905
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1906
|
+
result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
|
|
1907
|
+
frame.slots[1] = result.value;
|
|
1908
|
+
}
|
|
1909
|
+
if (result.status == OSEO_STATUS_NORMAL) *value = frame.slots[1];
|
|
1910
|
+
oseo_roots_pop(context, &frame);
|
|
1911
|
+
return result;
|
|
1912
|
+
}
|
|
1913
|
+
|
|
1914
|
+
static OseoResult regexp_construct(
|
|
1915
|
+
OseoContext *context,
|
|
1916
|
+
OseoValue callee,
|
|
1917
|
+
size_t argument_count,
|
|
1918
|
+
const OseoValue *arguments,
|
|
1919
|
+
OseoValue new_target
|
|
1920
|
+
) {
|
|
1921
|
+
OseoValue pattern = argument_count == 0u
|
|
1922
|
+
? oseo_undefined()
|
|
1923
|
+
: arguments[0];
|
|
1924
|
+
OseoValue flags = argument_count < 2u
|
|
1925
|
+
? oseo_undefined()
|
|
1926
|
+
: arguments[1];
|
|
1927
|
+
OseoValue slots[7] = {
|
|
1928
|
+
callee,
|
|
1929
|
+
new_target,
|
|
1930
|
+
pattern,
|
|
1931
|
+
flags,
|
|
1932
|
+
oseo_undefined(),
|
|
1933
|
+
oseo_undefined(),
|
|
1934
|
+
oseo_undefined(),
|
|
1935
|
+
};
|
|
1936
|
+
OseoRootFrame frame = {NULL, slots, 7u};
|
|
1937
|
+
oseo_roots_push(context, &frame);
|
|
1938
|
+
bool called = tag_of(frame.slots[1]) == OSEO_TAG_UNDEFINED;
|
|
1939
|
+
if (called) frame.slots[1] = frame.slots[0];
|
|
1940
|
+
bool pattern_is_regexp = false;
|
|
1941
|
+
OseoResult result = oseo_internal_is_regexp(
|
|
1942
|
+
context,
|
|
1943
|
+
frame.slots[2],
|
|
1944
|
+
&pattern_is_regexp
|
|
1945
|
+
);
|
|
1946
|
+
if (result.status == OSEO_STATUS_NORMAL && called &&
|
|
1947
|
+
pattern_is_regexp &&
|
|
1948
|
+
tag_of(frame.slots[3]) == OSEO_TAG_UNDEFINED) {
|
|
1949
|
+
result = regexp_get_named(
|
|
1950
|
+
context,
|
|
1951
|
+
frame.slots[2],
|
|
1952
|
+
"constructor",
|
|
1953
|
+
&frame.slots[4]
|
|
1954
|
+
);
|
|
1955
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
1956
|
+
frame.slots[4] == frame.slots[1]) {
|
|
1957
|
+
result = normal(frame.slots[2]);
|
|
1958
|
+
oseo_roots_pop(context, &frame);
|
|
1959
|
+
return result;
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
frame.slots[4] = frame.slots[2];
|
|
1963
|
+
if (result.status == OSEO_STATUS_NORMAL && is_regexp(frame.slots[2])) {
|
|
1964
|
+
OseoValue matcher = regexp_object(frame.slots[2])->matcher;
|
|
1965
|
+
frame.slots[6] = matcher;
|
|
1966
|
+
frame.slots[4] = regexp_matcher_object(matcher)->source;
|
|
1967
|
+
if (tag_of(frame.slots[3]) == OSEO_TAG_UNDEFINED) {
|
|
1968
|
+
frame.slots[3] = regexp_matcher_object(matcher)->flags;
|
|
1969
|
+
}
|
|
1970
|
+
} else if (result.status == OSEO_STATUS_NORMAL && pattern_is_regexp) {
|
|
1971
|
+
result = regexp_get_named(
|
|
1972
|
+
context,
|
|
1973
|
+
frame.slots[2],
|
|
1974
|
+
"source",
|
|
1975
|
+
&frame.slots[4]
|
|
1976
|
+
);
|
|
1977
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
1978
|
+
tag_of(frame.slots[3]) == OSEO_TAG_UNDEFINED) {
|
|
1979
|
+
result = regexp_get_named(
|
|
1980
|
+
context,
|
|
1981
|
+
frame.slots[2],
|
|
1982
|
+
"flags",
|
|
1983
|
+
&frame.slots[5]
|
|
1984
|
+
);
|
|
1985
|
+
frame.slots[3] = frame.slots[5];
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1989
|
+
result = regexp_allocate(context, frame.slots[1]);
|
|
1990
|
+
frame.slots[5] = result.value;
|
|
1991
|
+
}
|
|
1992
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1993
|
+
result = regexp_initialize(
|
|
1994
|
+
context,
|
|
1995
|
+
frame.slots[5],
|
|
1996
|
+
frame.slots[4],
|
|
1997
|
+
frame.slots[3],
|
|
1998
|
+
frame.slots[6]
|
|
1999
|
+
);
|
|
2000
|
+
}
|
|
2001
|
+
oseo_roots_pop(context, &frame);
|
|
2002
|
+
return result;
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
/* ToLength's clamp: 2^53 - 1. */
|
|
2006
|
+
#define OSEO_REGEXP_MAX_SAFE_LENGTH 9007199254740991.0
|
|
2007
|
+
|
|
2008
|
+
/* ToLength (7.1.20) for the `lastIndex` read built-in execution makes. */
|
|
2009
|
+
static OseoResult regexp_to_length(
|
|
2010
|
+
OseoContext *context,
|
|
2011
|
+
OseoValue value,
|
|
2012
|
+
double *length
|
|
2013
|
+
) {
|
|
2014
|
+
*length = 0.0;
|
|
2015
|
+
OseoResult result = oseo_internal_to_number(context, value);
|
|
2016
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
2017
|
+
if (!is_number(result.value)) return normal(oseo_undefined());
|
|
2018
|
+
double number = number_value(result.value);
|
|
2019
|
+
/* NaN, negative zero, and every negative value clamp to zero. */
|
|
2020
|
+
if (!(number > 0.0)) return normal(oseo_undefined());
|
|
2021
|
+
number = floor(number);
|
|
2022
|
+
*length = number > OSEO_REGEXP_MAX_SAFE_LENGTH
|
|
2023
|
+
? OSEO_REGEXP_MAX_SAFE_LENGTH
|
|
2024
|
+
: number;
|
|
2025
|
+
return normal(oseo_undefined());
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
/* Set(R, "lastIndex", value, true), which a read-only slot rejects. */
|
|
2029
|
+
static OseoResult regexp_set_last_index(
|
|
2030
|
+
OseoContext *context,
|
|
2031
|
+
OseoValue regexp,
|
|
2032
|
+
double value
|
|
2033
|
+
) {
|
|
2034
|
+
OseoValue slots[2] = {regexp, oseo_undefined()};
|
|
2035
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
2036
|
+
oseo_roots_push(context, &frame);
|
|
2037
|
+
OseoResult result = oseo_internal_ascii_string(context, "lastIndex");
|
|
2038
|
+
frame.slots[1] = result.value;
|
|
2039
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2040
|
+
result = oseo_object_set(
|
|
2041
|
+
context,
|
|
2042
|
+
frame.slots[0],
|
|
2043
|
+
frame.slots[1],
|
|
2044
|
+
oseo_number(value),
|
|
2045
|
+
true
|
|
2046
|
+
);
|
|
2047
|
+
}
|
|
2048
|
+
oseo_roots_pop(context, &frame);
|
|
2049
|
+
return result;
|
|
2050
|
+
}
|
|
2051
|
+
|
|
2052
|
+
/*
|
|
2053
|
+
* RegExp.prototype.compile (B.2.4.1) reinitializes the receiver in place.
|
|
2054
|
+
*
|
|
2055
|
+
* A RegExp pattern supplies its [[OriginalSource]] and [[OriginalFlags]]
|
|
2056
|
+
* and its matcher as the origin, so an equal parse borrows that program
|
|
2057
|
+
* rather than compiling it again; any other pattern goes through the same
|
|
2058
|
+
* conversion and validation as the constructor. RegExpInitialize replaces
|
|
2059
|
+
* the matcher only once the new pattern is valid, and the `lastIndex`
|
|
2060
|
+
* write that follows it throws when the property is not writable, after
|
|
2061
|
+
* the receiver already holds the new pattern.
|
|
2062
|
+
*/
|
|
2063
|
+
static OseoResult regexp_prototype_compile(
|
|
2064
|
+
OseoContext *context,
|
|
2065
|
+
OseoValue receiver,
|
|
2066
|
+
size_t argument_count,
|
|
2067
|
+
const OseoValue *arguments
|
|
2068
|
+
) {
|
|
2069
|
+
if (!is_regexp(receiver)) {
|
|
2070
|
+
return oseo_internal_throw_error(
|
|
2071
|
+
context,
|
|
2072
|
+
OSEO_ERROR_TYPE,
|
|
2073
|
+
"RegExp.prototype.compile requires a RegExp receiver."
|
|
2074
|
+
);
|
|
2075
|
+
}
|
|
2076
|
+
OseoValue slots[4] = {
|
|
2077
|
+
receiver,
|
|
2078
|
+
argument_count == 0u ? oseo_undefined() : arguments[0],
|
|
2079
|
+
argument_count < 2u ? oseo_undefined() : arguments[1],
|
|
2080
|
+
oseo_undefined(),
|
|
2081
|
+
};
|
|
2082
|
+
OseoRootFrame frame = {NULL, slots, 4u};
|
|
2083
|
+
oseo_roots_push(context, &frame);
|
|
2084
|
+
OseoResult result = normal(oseo_undefined());
|
|
2085
|
+
if (is_regexp(frame.slots[1])) {
|
|
2086
|
+
if (tag_of(frame.slots[2]) != OSEO_TAG_UNDEFINED) {
|
|
2087
|
+
result = oseo_internal_throw_error(
|
|
2088
|
+
context,
|
|
2089
|
+
OSEO_ERROR_TYPE,
|
|
2090
|
+
"RegExp.prototype.compile takes no flags with a RegExp."
|
|
2091
|
+
);
|
|
2092
|
+
} else {
|
|
2093
|
+
OseoValue matcher = regexp_object(frame.slots[1])->matcher;
|
|
2094
|
+
frame.slots[3] = matcher;
|
|
2095
|
+
frame.slots[2] = regexp_matcher_object(matcher)->flags;
|
|
2096
|
+
frame.slots[1] = regexp_matcher_object(matcher)->source;
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2100
|
+
result = regexp_initialize(
|
|
2101
|
+
context,
|
|
2102
|
+
frame.slots[0],
|
|
2103
|
+
frame.slots[1],
|
|
2104
|
+
frame.slots[2],
|
|
2105
|
+
frame.slots[3]
|
|
2106
|
+
);
|
|
2107
|
+
}
|
|
2108
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2109
|
+
result = regexp_set_last_index(context, frame.slots[0], 0.0);
|
|
2110
|
+
}
|
|
2111
|
+
if (result.status == OSEO_STATUS_NORMAL) result = normal(frame.slots[0]);
|
|
2112
|
+
oseo_roots_pop(context, &frame);
|
|
2113
|
+
return result;
|
|
2114
|
+
}
|
|
2115
|
+
|
|
2116
|
+
/* CreateDataPropertyOrThrow for one owned result-object property. */
|
|
2117
|
+
static OseoResult regexp_create_data_property(
|
|
2118
|
+
OseoContext *context,
|
|
2119
|
+
OseoValue object,
|
|
2120
|
+
OseoValue key,
|
|
2121
|
+
OseoValue value
|
|
2122
|
+
) {
|
|
2123
|
+
return oseo_object_define(
|
|
2124
|
+
context,
|
|
2125
|
+
object,
|
|
2126
|
+
key,
|
|
2127
|
+
value,
|
|
2128
|
+
(OseoPropertyAttributes){true, true, true, false}
|
|
2129
|
+
);
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
static OseoResult regexp_create_named_property(
|
|
2133
|
+
OseoContext *context,
|
|
2134
|
+
OseoValue object,
|
|
2135
|
+
const char *name,
|
|
2136
|
+
OseoValue value
|
|
2137
|
+
) {
|
|
2138
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
2139
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
2140
|
+
oseo_roots_push(context, &frame);
|
|
2141
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
2142
|
+
frame.slots[2] = result.value;
|
|
2143
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2144
|
+
result = regexp_create_data_property(
|
|
2145
|
+
context,
|
|
2146
|
+
frame.slots[0],
|
|
2147
|
+
frame.slots[2],
|
|
2148
|
+
frame.slots[1]
|
|
2149
|
+
);
|
|
2150
|
+
}
|
|
2151
|
+
oseo_roots_pop(context, &frame);
|
|
2152
|
+
return result;
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
/* The half-open substring of one rooted string value. */
|
|
2156
|
+
static OseoResult regexp_substring(
|
|
2157
|
+
OseoContext *context,
|
|
2158
|
+
OseoValue subject,
|
|
2159
|
+
size_t start,
|
|
2160
|
+
size_t end
|
|
2161
|
+
) {
|
|
2162
|
+
const OseoString *string = string_object(subject);
|
|
2163
|
+
return oseo_internal_allocate_string(
|
|
2164
|
+
context,
|
|
2165
|
+
&string->units[start],
|
|
2166
|
+
end - start
|
|
2167
|
+
);
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
/*
|
|
2171
|
+
* The two-element index pair one capture contributes to `indices`, or
|
|
2172
|
+
* undefined for a capture that did not participate.
|
|
2173
|
+
*/
|
|
2174
|
+
static OseoResult regexp_index_pair(
|
|
2175
|
+
OseoContext *context,
|
|
2176
|
+
int64_t start,
|
|
2177
|
+
int64_t end
|
|
2178
|
+
) {
|
|
2179
|
+
if (start < 0 || end < 0) return normal(oseo_undefined());
|
|
2180
|
+
OseoValue array = oseo_undefined();
|
|
2181
|
+
OseoRootFrame frame = {NULL, &array, 1u};
|
|
2182
|
+
oseo_roots_push(context, &frame);
|
|
2183
|
+
OseoResult result = oseo_array_create(context, 0u);
|
|
2184
|
+
array = result.value;
|
|
2185
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2186
|
+
result = oseo_array_append(context, array, oseo_number((double)start));
|
|
2187
|
+
}
|
|
2188
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2189
|
+
result = oseo_array_append(context, array, oseo_number((double)end));
|
|
2190
|
+
}
|
|
2191
|
+
if (result.status == OSEO_STATUS_NORMAL) result = normal(array);
|
|
2192
|
+
oseo_roots_pop(context, &frame);
|
|
2193
|
+
return result;
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
/* The group name of one capture as a string value. */
|
|
2197
|
+
static OseoResult regexp_group_name_string(
|
|
2198
|
+
OseoContext *context,
|
|
2199
|
+
const OseoRegExpProgram *program,
|
|
2200
|
+
size_t capture
|
|
2201
|
+
) {
|
|
2202
|
+
const OseoRegExpCapture *record = &program->captures[capture - 1u];
|
|
2203
|
+
return oseo_internal_allocate_string(
|
|
2204
|
+
context,
|
|
2205
|
+
&program->name_units[record->name_offset],
|
|
2206
|
+
record->name_length
|
|
2207
|
+
);
|
|
2208
|
+
}
|
|
2209
|
+
|
|
2210
|
+
/* Whether an earlier capture with the same name participated in the match. */
|
|
2211
|
+
static bool regexp_earlier_named_capture_participated(
|
|
2212
|
+
const OseoRegExpProgram *program,
|
|
2213
|
+
const int64_t *captures,
|
|
2214
|
+
size_t capture
|
|
2215
|
+
) {
|
|
2216
|
+
const OseoRegExpCapture *record = &program->captures[capture - 1u];
|
|
2217
|
+
for (size_t earlier = 1u; earlier < capture; earlier += 1u) {
|
|
2218
|
+
const OseoRegExpCapture *candidate =
|
|
2219
|
+
&program->captures[earlier - 1u];
|
|
2220
|
+
if (!candidate->named ||
|
|
2221
|
+
candidate->name_length != record->name_length ||
|
|
2222
|
+
captures[2u * earlier] < 0) {
|
|
2223
|
+
continue;
|
|
2224
|
+
}
|
|
2225
|
+
if (memcmp(
|
|
2226
|
+
&program->name_units[candidate->name_offset],
|
|
2227
|
+
&program->name_units[record->name_offset],
|
|
2228
|
+
record->name_length * sizeof(uint16_t)
|
|
2229
|
+
) == 0) {
|
|
2230
|
+
return true;
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
return false;
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
/*
|
|
2237
|
+
* MakeMatchIndicesIndexPairArray (22.2.7.7).
|
|
2238
|
+
*
|
|
2239
|
+
* The pair array mirrors the result array: element zero is the whole
|
|
2240
|
+
* match, element `index` is capturing group `index`, and a named group
|
|
2241
|
+
* repeats its pair on the `groups` object. Nothing here reads the input
|
|
2242
|
+
* string, so a capture that did not participate needs no span.
|
|
2243
|
+
*/
|
|
2244
|
+
static OseoResult regexp_match_indices(
|
|
2245
|
+
OseoContext *context,
|
|
2246
|
+
const OseoRegExpProgram *program,
|
|
2247
|
+
const int64_t *captures
|
|
2248
|
+
) {
|
|
2249
|
+
OseoValue slots[3] = {
|
|
2250
|
+
oseo_undefined(),
|
|
2251
|
+
oseo_undefined(),
|
|
2252
|
+
oseo_undefined(),
|
|
2253
|
+
};
|
|
2254
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
2255
|
+
oseo_roots_push(context, &frame);
|
|
2256
|
+
OseoResult result = oseo_array_create(context, 0u);
|
|
2257
|
+
frame.slots[0] = result.value;
|
|
2258
|
+
if (result.status == OSEO_STATUS_NORMAL && program->has_group_names) {
|
|
2259
|
+
result = oseo_object_create(context, oseo_null());
|
|
2260
|
+
frame.slots[1] = result.value;
|
|
2261
|
+
}
|
|
2262
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2263
|
+
result = regexp_create_named_property(
|
|
2264
|
+
context,
|
|
2265
|
+
frame.slots[0],
|
|
2266
|
+
"groups",
|
|
2267
|
+
frame.slots[1]
|
|
2268
|
+
);
|
|
2269
|
+
}
|
|
2270
|
+
for (size_t index = 0u;
|
|
2271
|
+
result.status == OSEO_STATUS_NORMAL &&
|
|
2272
|
+
index <= program->capture_count;
|
|
2273
|
+
index += 1u) {
|
|
2274
|
+
result = regexp_index_pair(
|
|
2275
|
+
context,
|
|
2276
|
+
captures[2u * index],
|
|
2277
|
+
captures[2u * index + 1u]
|
|
2278
|
+
);
|
|
2279
|
+
frame.slots[2] = result.value;
|
|
2280
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2281
|
+
result = oseo_array_append(
|
|
2282
|
+
context,
|
|
2283
|
+
frame.slots[0],
|
|
2284
|
+
frame.slots[2]
|
|
2285
|
+
);
|
|
2286
|
+
}
|
|
2287
|
+
if (result.status != OSEO_STATUS_NORMAL || index == 0u) continue;
|
|
2288
|
+
if (!program->captures[index - 1u].named) continue;
|
|
2289
|
+
if (captures[2u * index] < 0 &&
|
|
2290
|
+
regexp_earlier_named_capture_participated(
|
|
2291
|
+
program,
|
|
2292
|
+
captures,
|
|
2293
|
+
index
|
|
2294
|
+
)) {
|
|
2295
|
+
continue;
|
|
2296
|
+
}
|
|
2297
|
+
OseoValue pair = frame.slots[2];
|
|
2298
|
+
result = regexp_group_name_string(context, program, index);
|
|
2299
|
+
OseoValue name = result.value;
|
|
2300
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2301
|
+
OseoValue nested[3] = {frame.slots[1], name, pair};
|
|
2302
|
+
OseoRootFrame inner = {NULL, nested, 3u};
|
|
2303
|
+
oseo_roots_push(context, &inner);
|
|
2304
|
+
result = regexp_create_data_property(
|
|
2305
|
+
context,
|
|
2306
|
+
nested[0],
|
|
2307
|
+
nested[1],
|
|
2308
|
+
nested[2]
|
|
2309
|
+
);
|
|
2310
|
+
oseo_roots_pop(context, &inner);
|
|
2311
|
+
}
|
|
2312
|
+
frame.slots[2] = pair;
|
|
2313
|
+
}
|
|
2314
|
+
if (result.status == OSEO_STATUS_NORMAL) result = normal(frame.slots[0]);
|
|
2315
|
+
oseo_roots_pop(context, &frame);
|
|
2316
|
+
return result;
|
|
2317
|
+
}
|
|
2318
|
+
|
|
2319
|
+
/* The owned boundary a match attempt that ran out of resources reports. */
|
|
2320
|
+
static OseoResult regexp_execution_failure(
|
|
2321
|
+
OseoContext *context,
|
|
2322
|
+
OseoRegExpExecution outcome
|
|
2323
|
+
) {
|
|
2324
|
+
if (outcome == OSEO_REGEXP_EXECUTION_ALLOCATION) {
|
|
2325
|
+
return failure(
|
|
2326
|
+
context,
|
|
2327
|
+
"OSEO2001",
|
|
2328
|
+
"RegExp matcher work area allocation failed."
|
|
2329
|
+
);
|
|
2330
|
+
}
|
|
2331
|
+
return failure(
|
|
2332
|
+
context,
|
|
2333
|
+
"OSEO2001",
|
|
2334
|
+
"Regular expression matching exceeds the reviewed matcher limit."
|
|
2335
|
+
);
|
|
2336
|
+
}
|
|
2337
|
+
|
|
2338
|
+
/*
|
|
2339
|
+
* RegExpBuiltinExec (22.2.7.2).
|
|
2340
|
+
*
|
|
2341
|
+
* The position loop, `lastIndex` conversion and update, and the result
|
|
2342
|
+
* object live here; the matcher component owns the match itself. A
|
|
2343
|
+
* global or sticky pattern writes `lastIndex` through the ordinary
|
|
2344
|
+
* property path, so a frozen or redefined slot is observed exactly where
|
|
2345
|
+
* the edition observes it.
|
|
2346
|
+
*/
|
|
2347
|
+
static OseoResult regexp_builtin_exec(
|
|
2348
|
+
OseoContext *context,
|
|
2349
|
+
OseoValue regexp,
|
|
2350
|
+
OseoValue subject
|
|
2351
|
+
) {
|
|
2352
|
+
OseoValue slots[5] = {
|
|
2353
|
+
regexp,
|
|
2354
|
+
subject,
|
|
2355
|
+
oseo_undefined(),
|
|
2356
|
+
oseo_undefined(),
|
|
2357
|
+
oseo_undefined(),
|
|
2358
|
+
};
|
|
2359
|
+
OseoRootFrame frame = {NULL, slots, 5u};
|
|
2360
|
+
oseo_roots_push(context, &frame);
|
|
2361
|
+
double last_index = 0.0;
|
|
2362
|
+
OseoResult result = regexp_get_named(
|
|
2363
|
+
context,
|
|
2364
|
+
frame.slots[0],
|
|
2365
|
+
"lastIndex",
|
|
2366
|
+
&frame.slots[2]
|
|
2367
|
+
);
|
|
2368
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2369
|
+
result = regexp_to_length(context, frame.slots[2], &last_index);
|
|
2370
|
+
}
|
|
2371
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
2372
|
+
oseo_roots_pop(context, &frame);
|
|
2373
|
+
return result;
|
|
2374
|
+
}
|
|
2375
|
+
OseoRegExpMatcher *matcher =
|
|
2376
|
+
regexp_matcher_object(regexp_object(frame.slots[0])->matcher);
|
|
2377
|
+
uint16_t mask = matcher->flag_mask;
|
|
2378
|
+
bool global = (mask & OSEO_REGEXP_FLAG_G) != 0u;
|
|
2379
|
+
bool sticky = (mask & OSEO_REGEXP_FLAG_Y) != 0u;
|
|
2380
|
+
bool has_indices = (mask & OSEO_REGEXP_FLAG_D) != 0u;
|
|
2381
|
+
if (!global && !sticky) last_index = 0.0;
|
|
2382
|
+
const OseoRegExpProgram *program = matcher->program;
|
|
2383
|
+
size_t length = string_object(frame.slots[1])->length;
|
|
2384
|
+
size_t total = 2u * (program->capture_count + 1u);
|
|
2385
|
+
int64_t *captures = oseo_internal_allocate_work_bytes(
|
|
2386
|
+
context,
|
|
2387
|
+
total * sizeof(int64_t)
|
|
2388
|
+
);
|
|
2389
|
+
if (captures == NULL) {
|
|
2390
|
+
oseo_roots_pop(context, &frame);
|
|
2391
|
+
return failure(
|
|
2392
|
+
context,
|
|
2393
|
+
"OSEO2001",
|
|
2394
|
+
"RegExp capture allocation failed."
|
|
2395
|
+
);
|
|
2396
|
+
}
|
|
2397
|
+
OseoRegExpExecution outcome = OSEO_REGEXP_EXECUTION_UNMATCHED;
|
|
2398
|
+
if (last_index <= (double)length) {
|
|
2399
|
+
outcome = oseo_internal_regexp_program_search(
|
|
2400
|
+
context,
|
|
2401
|
+
program,
|
|
2402
|
+
string_object(frame.slots[1]),
|
|
2403
|
+
(size_t)last_index,
|
|
2404
|
+
sticky,
|
|
2405
|
+
mask == 0u || mask == OSEO_REGEXP_FLAG_U ||
|
|
2406
|
+
mask == OSEO_REGEXP_FLAG_V,
|
|
2407
|
+
captures
|
|
2408
|
+
);
|
|
2409
|
+
}
|
|
2410
|
+
if (outcome == OSEO_REGEXP_EXECUTION_LIMIT ||
|
|
2411
|
+
outcome == OSEO_REGEXP_EXECUTION_ALLOCATION) {
|
|
2412
|
+
free(captures);
|
|
2413
|
+
oseo_roots_pop(context, &frame);
|
|
2414
|
+
return regexp_execution_failure(context, outcome);
|
|
2415
|
+
}
|
|
2416
|
+
if (outcome == OSEO_REGEXP_EXECUTION_UNMATCHED) {
|
|
2417
|
+
free(captures);
|
|
2418
|
+
result = normal(oseo_null());
|
|
2419
|
+
if (global || sticky) {
|
|
2420
|
+
OseoResult reset = regexp_set_last_index(
|
|
2421
|
+
context,
|
|
2422
|
+
frame.slots[0],
|
|
2423
|
+
0.0
|
|
2424
|
+
);
|
|
2425
|
+
if (reset.status != OSEO_STATUS_NORMAL) result = reset;
|
|
2426
|
+
}
|
|
2427
|
+
oseo_roots_pop(context, &frame);
|
|
2428
|
+
return result;
|
|
2429
|
+
}
|
|
2430
|
+
size_t match_start = (size_t)captures[0];
|
|
2431
|
+
size_t match_end = (size_t)captures[1];
|
|
2432
|
+
if (global || sticky) {
|
|
2433
|
+
result = regexp_set_last_index(
|
|
2434
|
+
context,
|
|
2435
|
+
frame.slots[0],
|
|
2436
|
+
(double)match_end
|
|
2437
|
+
);
|
|
2438
|
+
}
|
|
2439
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2440
|
+
result = oseo_array_create(context, 0u);
|
|
2441
|
+
frame.slots[2] = result.value;
|
|
2442
|
+
}
|
|
2443
|
+
if (result.status == OSEO_STATUS_NORMAL && program->has_group_names) {
|
|
2444
|
+
result = oseo_object_create(context, oseo_null());
|
|
2445
|
+
frame.slots[3] = result.value;
|
|
2446
|
+
}
|
|
2447
|
+
for (size_t index = 0u;
|
|
2448
|
+
result.status == OSEO_STATUS_NORMAL &&
|
|
2449
|
+
index <= program->capture_count;
|
|
2450
|
+
index += 1u) {
|
|
2451
|
+
frame.slots[4] = oseo_undefined();
|
|
2452
|
+
if (captures[2u * index] >= 0) {
|
|
2453
|
+
result = regexp_substring(
|
|
2454
|
+
context,
|
|
2455
|
+
frame.slots[1],
|
|
2456
|
+
(size_t)captures[2u * index],
|
|
2457
|
+
(size_t)captures[2u * index + 1u]
|
|
2458
|
+
);
|
|
2459
|
+
frame.slots[4] = result.value;
|
|
2460
|
+
}
|
|
2461
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2462
|
+
result = oseo_array_append(
|
|
2463
|
+
context,
|
|
2464
|
+
frame.slots[2],
|
|
2465
|
+
frame.slots[4]
|
|
2466
|
+
);
|
|
2467
|
+
}
|
|
2468
|
+
if (result.status != OSEO_STATUS_NORMAL || index == 0u) continue;
|
|
2469
|
+
if (!program->captures[index - 1u].named) continue;
|
|
2470
|
+
if (captures[2u * index] < 0 &&
|
|
2471
|
+
regexp_earlier_named_capture_participated(
|
|
2472
|
+
program,
|
|
2473
|
+
captures,
|
|
2474
|
+
index
|
|
2475
|
+
)) {
|
|
2476
|
+
continue;
|
|
2477
|
+
}
|
|
2478
|
+
OseoValue captured = frame.slots[4];
|
|
2479
|
+
result = regexp_group_name_string(context, program, index);
|
|
2480
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2481
|
+
OseoValue nested[3] = {frame.slots[3], result.value, captured};
|
|
2482
|
+
OseoRootFrame inner = {NULL, nested, 3u};
|
|
2483
|
+
oseo_roots_push(context, &inner);
|
|
2484
|
+
result = regexp_create_data_property(
|
|
2485
|
+
context,
|
|
2486
|
+
nested[0],
|
|
2487
|
+
nested[1],
|
|
2488
|
+
nested[2]
|
|
2489
|
+
);
|
|
2490
|
+
oseo_roots_pop(context, &inner);
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2494
|
+
result = regexp_create_named_property(
|
|
2495
|
+
context,
|
|
2496
|
+
frame.slots[2],
|
|
2497
|
+
"index",
|
|
2498
|
+
oseo_number((double)match_start)
|
|
2499
|
+
);
|
|
2500
|
+
}
|
|
2501
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2502
|
+
result = regexp_create_named_property(
|
|
2503
|
+
context,
|
|
2504
|
+
frame.slots[2],
|
|
2505
|
+
"input",
|
|
2506
|
+
frame.slots[1]
|
|
2507
|
+
);
|
|
2508
|
+
}
|
|
2509
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2510
|
+
result = regexp_create_named_property(
|
|
2511
|
+
context,
|
|
2512
|
+
frame.slots[2],
|
|
2513
|
+
"groups",
|
|
2514
|
+
frame.slots[3]
|
|
2515
|
+
);
|
|
2516
|
+
}
|
|
2517
|
+
if (result.status == OSEO_STATUS_NORMAL && has_indices) {
|
|
2518
|
+
result = regexp_match_indices(context, program, captures);
|
|
2519
|
+
frame.slots[4] = result.value;
|
|
2520
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2521
|
+
result = regexp_create_named_property(
|
|
2522
|
+
context,
|
|
2523
|
+
frame.slots[2],
|
|
2524
|
+
"indices",
|
|
2525
|
+
frame.slots[4]
|
|
2526
|
+
);
|
|
2527
|
+
}
|
|
2528
|
+
}
|
|
2529
|
+
free(captures);
|
|
2530
|
+
if (result.status == OSEO_STATUS_NORMAL) result = normal(frame.slots[2]);
|
|
2531
|
+
oseo_roots_pop(context, &frame);
|
|
2532
|
+
return result;
|
|
2533
|
+
}
|
|
2534
|
+
|
|
2535
|
+
/*
|
|
2536
|
+
* RegExpExec (22.2.7.1): an overridden `exec` is called where the
|
|
2537
|
+
* edition requires it, and only a branded RegExp reaches the built-in.
|
|
2538
|
+
*/
|
|
2539
|
+
OseoResult oseo_internal_regexp_exec(
|
|
2540
|
+
OseoContext *context,
|
|
2541
|
+
OseoValue regexp,
|
|
2542
|
+
OseoValue subject
|
|
2543
|
+
) {
|
|
2544
|
+
OseoValue slots[3] = {regexp, subject, oseo_undefined()};
|
|
2545
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
2546
|
+
oseo_roots_push(context, &frame);
|
|
2547
|
+
OseoResult result = regexp_get_named(
|
|
2548
|
+
context,
|
|
2549
|
+
frame.slots[0],
|
|
2550
|
+
"exec",
|
|
2551
|
+
&frame.slots[2]
|
|
2552
|
+
);
|
|
2553
|
+
if (result.status == OSEO_STATUS_NORMAL && is_callable(frame.slots[2])) {
|
|
2554
|
+
result = oseo_call_function(
|
|
2555
|
+
context,
|
|
2556
|
+
frame.slots[2],
|
|
2557
|
+
frame.slots[0],
|
|
2558
|
+
1u,
|
|
2559
|
+
&frame.slots[1],
|
|
2560
|
+
oseo_undefined()
|
|
2561
|
+
);
|
|
2562
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
2563
|
+
!is_object(result.value) &&
|
|
2564
|
+
tag_of(result.value) != OSEO_TAG_NULL) {
|
|
2565
|
+
result = oseo_internal_throw_error(
|
|
2566
|
+
context,
|
|
2567
|
+
OSEO_ERROR_TYPE,
|
|
2568
|
+
"A RegExp exec method must return an object or null."
|
|
2569
|
+
);
|
|
2570
|
+
}
|
|
2571
|
+
oseo_roots_pop(context, &frame);
|
|
2572
|
+
return result;
|
|
2573
|
+
}
|
|
2574
|
+
if (result.status == OSEO_STATUS_NORMAL && !is_regexp(frame.slots[0])) {
|
|
2575
|
+
result = oseo_internal_throw_error(
|
|
2576
|
+
context,
|
|
2577
|
+
OSEO_ERROR_TYPE,
|
|
2578
|
+
"RegExp execution requires a RegExp object receiver."
|
|
2579
|
+
);
|
|
2580
|
+
}
|
|
2581
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2582
|
+
result = regexp_builtin_exec(context, frame.slots[0], frame.slots[1]);
|
|
2583
|
+
}
|
|
2584
|
+
oseo_roots_pop(context, &frame);
|
|
2585
|
+
return result;
|
|
2586
|
+
}
|
|
2587
|
+
|
|
2588
|
+
static OseoResult regexp_prototype_exec(
|
|
2589
|
+
OseoContext *context,
|
|
2590
|
+
OseoValue receiver,
|
|
2591
|
+
size_t argument_count,
|
|
2592
|
+
const OseoValue *arguments
|
|
2593
|
+
) {
|
|
2594
|
+
if (!is_regexp(receiver)) {
|
|
2595
|
+
return oseo_internal_throw_error(
|
|
2596
|
+
context,
|
|
2597
|
+
OSEO_ERROR_TYPE,
|
|
2598
|
+
"RegExp.prototype.exec requires a RegExp object receiver."
|
|
2599
|
+
);
|
|
2600
|
+
}
|
|
2601
|
+
OseoValue slots[2] = {receiver, oseo_undefined()};
|
|
2602
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
2603
|
+
oseo_roots_push(context, &frame);
|
|
2604
|
+
OseoResult result = oseo_internal_value_string(
|
|
2605
|
+
context,
|
|
2606
|
+
argument_count == 0u ? oseo_undefined() : arguments[0]
|
|
2607
|
+
);
|
|
2608
|
+
frame.slots[1] = result.value;
|
|
2609
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2610
|
+
result = regexp_builtin_exec(context, frame.slots[0], frame.slots[1]);
|
|
2611
|
+
}
|
|
2612
|
+
oseo_roots_pop(context, &frame);
|
|
2613
|
+
return result;
|
|
2614
|
+
}
|
|
2615
|
+
|
|
2616
|
+
static OseoResult regexp_prototype_test(
|
|
2617
|
+
OseoContext *context,
|
|
2618
|
+
OseoValue receiver,
|
|
2619
|
+
size_t argument_count,
|
|
2620
|
+
const OseoValue *arguments
|
|
2621
|
+
) {
|
|
2622
|
+
if (!is_object(receiver)) {
|
|
2623
|
+
return oseo_internal_throw_error(
|
|
2624
|
+
context,
|
|
2625
|
+
OSEO_ERROR_TYPE,
|
|
2626
|
+
"RegExp.prototype.test requires an object receiver."
|
|
2627
|
+
);
|
|
2628
|
+
}
|
|
2629
|
+
OseoValue slots[2] = {receiver, oseo_undefined()};
|
|
2630
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
2631
|
+
oseo_roots_push(context, &frame);
|
|
2632
|
+
OseoResult result = oseo_internal_value_string(
|
|
2633
|
+
context,
|
|
2634
|
+
argument_count == 0u ? oseo_undefined() : arguments[0]
|
|
2635
|
+
);
|
|
2636
|
+
frame.slots[1] = result.value;
|
|
2637
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2638
|
+
result = oseo_internal_regexp_exec(
|
|
2639
|
+
context,
|
|
2640
|
+
frame.slots[0],
|
|
2641
|
+
frame.slots[1]
|
|
2642
|
+
);
|
|
2643
|
+
}
|
|
2644
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2645
|
+
result = normal(
|
|
2646
|
+
oseo_boolean(tag_of(result.value) != OSEO_TAG_NULL)
|
|
2647
|
+
);
|
|
2648
|
+
}
|
|
2649
|
+
oseo_roots_pop(context, &frame);
|
|
2650
|
+
return result;
|
|
2651
|
+
}
|
|
2652
|
+
|
|
2653
|
+
/*
|
|
2654
|
+
* EscapeRegExpPattern (22.2.6.13.1).
|
|
2655
|
+
*
|
|
2656
|
+
* The escaping is the smallest one that keeps `"/" + S + "/" + F` a
|
|
2657
|
+
* regular expression literal with the same behavior: an unescaped `/`
|
|
2658
|
+
* outside a character class is escaped, and every LineTerminator becomes
|
|
2659
|
+
* its own escape sequence wherever it appears. An empty pattern becomes
|
|
2660
|
+
* `(?:)`, which is the empty pattern's only literal spelling.
|
|
2661
|
+
*/
|
|
2662
|
+
static bool regexp_line_terminator(uint16_t unit) {
|
|
2663
|
+
return unit == 0x0au || unit == 0x0du || unit == 0x2028u ||
|
|
2664
|
+
unit == 0x2029u;
|
|
2665
|
+
}
|
|
2666
|
+
|
|
2667
|
+
/* The code units one LineTerminator contributes after its backslash. */
|
|
2668
|
+
static size_t regexp_line_terminator_escape(uint16_t unit, uint16_t *units) {
|
|
2669
|
+
if (unit == 0x0au || unit == 0x0du) {
|
|
2670
|
+
units[0] = unit == 0x0au ? (uint16_t)'n' : (uint16_t)'r';
|
|
2671
|
+
return 1u;
|
|
2672
|
+
}
|
|
2673
|
+
units[0] = 'u';
|
|
2674
|
+
units[1] = '2';
|
|
2675
|
+
units[2] = '0';
|
|
2676
|
+
units[3] = '2';
|
|
2677
|
+
units[4] = unit == 0x2028u ? (uint16_t)'8' : (uint16_t)'9';
|
|
2678
|
+
return 5u;
|
|
2679
|
+
}
|
|
2680
|
+
|
|
2681
|
+
/*
|
|
2682
|
+
* EscapeRegExpPattern (22.2.6.13.1).
|
|
2683
|
+
*
|
|
2684
|
+
* The escaping is the smallest one that keeps `"/" + S + "/" + F` a
|
|
2685
|
+
* regular expression literal with the same behavior: an unescaped `/`
|
|
2686
|
+
* outside a character class is escaped, and every LineTerminator becomes
|
|
2687
|
+
* its own escape sequence wherever it appears, reusing a backslash that
|
|
2688
|
+
* already precedes it rather than adding a second one. An empty pattern
|
|
2689
|
+
* becomes `(?:)`, which is the empty pattern's only literal spelling.
|
|
2690
|
+
*/
|
|
2691
|
+
static OseoResult regexp_escape_pattern(
|
|
2692
|
+
OseoContext *context,
|
|
2693
|
+
OseoValue source
|
|
2694
|
+
) {
|
|
2695
|
+
const OseoString *string = string_object(source);
|
|
2696
|
+
if (string->length == 0u) {
|
|
2697
|
+
return oseo_internal_ascii_string(context, "(?:)");
|
|
2698
|
+
}
|
|
2699
|
+
uint16_t escape[5];
|
|
2700
|
+
size_t extra = 0u;
|
|
2701
|
+
bool rewritten = false;
|
|
2702
|
+
bool character_class = false;
|
|
2703
|
+
bool escaped = false;
|
|
2704
|
+
for (size_t index = 0u; index < string->length; index += 1u) {
|
|
2705
|
+
uint16_t unit = string->units[index];
|
|
2706
|
+
if (regexp_line_terminator(unit)) {
|
|
2707
|
+
/* An escaped LineTerminator keeps its length but not its
|
|
2708
|
+
* text, so the rewrite is not decided by the extra count. */
|
|
2709
|
+
extra += regexp_line_terminator_escape(unit, escape) - 1u;
|
|
2710
|
+
if (!escaped) extra += 1u;
|
|
2711
|
+
rewritten = true;
|
|
2712
|
+
escaped = false;
|
|
2713
|
+
continue;
|
|
2714
|
+
}
|
|
2715
|
+
if (escaped) {
|
|
2716
|
+
escaped = false;
|
|
2717
|
+
continue;
|
|
2718
|
+
}
|
|
2719
|
+
if (unit == '\\') {
|
|
2720
|
+
escaped = true;
|
|
2721
|
+
} else if (unit == '[') {
|
|
2722
|
+
character_class = true;
|
|
2723
|
+
} else if (unit == ']') {
|
|
2724
|
+
character_class = false;
|
|
2725
|
+
} else if (unit == '/' && !character_class) {
|
|
2726
|
+
extra += 1u;
|
|
2727
|
+
rewritten = true;
|
|
2728
|
+
}
|
|
2729
|
+
}
|
|
2730
|
+
if (!rewritten) return normal(source);
|
|
2731
|
+
if (string->length > SIZE_MAX - extra) {
|
|
2732
|
+
return failure(context, "OSEO2001", "Pattern escaping is too large.");
|
|
2733
|
+
}
|
|
2734
|
+
/* The escaped text is one ordinary string, so an over-long result is
|
|
2735
|
+
* the same catchable `RangeError` any other string length reports,
|
|
2736
|
+
* decided before a staging buffer that large is requested. */
|
|
2737
|
+
OseoResult length = oseo_internal_validate_string_length(
|
|
2738
|
+
context,
|
|
2739
|
+
string->length + extra
|
|
2740
|
+
);
|
|
2741
|
+
if (length.status != OSEO_STATUS_NORMAL) return length;
|
|
2742
|
+
uint16_t *units = oseo_internal_allocate_work_bytes(
|
|
2743
|
+
context,
|
|
2744
|
+
(string->length + extra) * sizeof(uint16_t)
|
|
2745
|
+
);
|
|
2746
|
+
if (units == NULL) {
|
|
2747
|
+
return failure(context, "OSEO2001", "Pattern escaping failed.");
|
|
2748
|
+
}
|
|
2749
|
+
size_t written = 0u;
|
|
2750
|
+
character_class = false;
|
|
2751
|
+
escaped = false;
|
|
2752
|
+
for (size_t index = 0u; index < string->length; index += 1u) {
|
|
2753
|
+
uint16_t unit = string->units[index];
|
|
2754
|
+
if (regexp_line_terminator(unit)) {
|
|
2755
|
+
size_t length = regexp_line_terminator_escape(unit, escape);
|
|
2756
|
+
if (!escaped) {
|
|
2757
|
+
units[written] = '\\';
|
|
2758
|
+
written += 1u;
|
|
2759
|
+
}
|
|
2760
|
+
for (size_t offset = 0u; offset < length; offset += 1u) {
|
|
2761
|
+
units[written] = escape[offset];
|
|
2762
|
+
written += 1u;
|
|
2763
|
+
}
|
|
2764
|
+
escaped = false;
|
|
2765
|
+
continue;
|
|
2766
|
+
}
|
|
2767
|
+
if (escaped) {
|
|
2768
|
+
escaped = false;
|
|
2769
|
+
} else if (unit == '\\') {
|
|
2770
|
+
escaped = true;
|
|
2771
|
+
} else if (unit == '[') {
|
|
2772
|
+
character_class = true;
|
|
2773
|
+
} else if (unit == ']') {
|
|
2774
|
+
character_class = false;
|
|
2775
|
+
} else if (unit == '/' && !character_class) {
|
|
2776
|
+
units[written] = '\\';
|
|
2777
|
+
written += 1u;
|
|
2778
|
+
}
|
|
2779
|
+
units[written] = unit;
|
|
2780
|
+
written += 1u;
|
|
2781
|
+
}
|
|
2782
|
+
OseoResult result = oseo_internal_allocate_string(
|
|
2783
|
+
context,
|
|
2784
|
+
units,
|
|
2785
|
+
written
|
|
2786
|
+
);
|
|
2787
|
+
free(units);
|
|
2788
|
+
return result;
|
|
2789
|
+
}
|
|
2790
|
+
|
|
2791
|
+
/*
|
|
2792
|
+
* The answer an accessor owes a receiver without the internal slot.
|
|
2793
|
+
*
|
|
2794
|
+
* `%RegExp.prototype%` itself is the one such receiver the edition
|
|
2795
|
+
* admits, so it reports the empty pattern or `undefined` instead of
|
|
2796
|
+
* throwing. The receiver stays rooted while the realm materializes that
|
|
2797
|
+
* intrinsic, because materializing it allocates.
|
|
2798
|
+
*/
|
|
2799
|
+
static OseoResult regexp_prototype_receiver(
|
|
2800
|
+
OseoContext *context,
|
|
2801
|
+
OseoValue receiver,
|
|
2802
|
+
const char *message,
|
|
2803
|
+
bool source_accessor
|
|
2804
|
+
) {
|
|
2805
|
+
OseoValue slots[1] = {receiver};
|
|
2806
|
+
OseoRootFrame frame = {NULL, slots, 1u};
|
|
2807
|
+
oseo_roots_push(context, &frame);
|
|
2808
|
+
OseoResult result = oseo_internal_intrinsic(
|
|
2809
|
+
context,
|
|
2810
|
+
OSEO_INTRINSIC_REGEXP_PROTOTYPE
|
|
2811
|
+
);
|
|
2812
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2813
|
+
if (result.value != frame.slots[0]) {
|
|
2814
|
+
result = oseo_internal_throw_error(
|
|
2815
|
+
context,
|
|
2816
|
+
OSEO_ERROR_TYPE,
|
|
2817
|
+
message
|
|
2818
|
+
);
|
|
2819
|
+
} else if (source_accessor) {
|
|
2820
|
+
result = oseo_internal_ascii_string(context, "(?:)");
|
|
2821
|
+
} else {
|
|
2822
|
+
result = normal(oseo_undefined());
|
|
2823
|
+
}
|
|
2824
|
+
}
|
|
2825
|
+
oseo_roots_pop(context, &frame);
|
|
2826
|
+
return result;
|
|
2827
|
+
}
|
|
2828
|
+
|
|
2829
|
+
static OseoResult regexp_prototype_source(
|
|
2830
|
+
OseoContext *context,
|
|
2831
|
+
OseoValue receiver
|
|
2832
|
+
) {
|
|
2833
|
+
if (!is_object(receiver)) {
|
|
2834
|
+
return oseo_internal_throw_error(
|
|
2835
|
+
context,
|
|
2836
|
+
OSEO_ERROR_TYPE,
|
|
2837
|
+
"The RegExp source accessor requires an object receiver."
|
|
2838
|
+
);
|
|
2839
|
+
}
|
|
2840
|
+
if (!is_regexp(receiver)) {
|
|
2841
|
+
return regexp_prototype_receiver(
|
|
2842
|
+
context,
|
|
2843
|
+
receiver,
|
|
2844
|
+
"The RegExp source accessor requires a RegExp object.",
|
|
2845
|
+
true
|
|
2846
|
+
);
|
|
2847
|
+
}
|
|
2848
|
+
OseoValue source =
|
|
2849
|
+
regexp_matcher_object(regexp_object(receiver)->matcher)->source;
|
|
2850
|
+
OseoRootFrame frame = {NULL, &source, 1u};
|
|
2851
|
+
oseo_roots_push(context, &frame);
|
|
2852
|
+
OseoResult result = regexp_escape_pattern(context, source);
|
|
2853
|
+
oseo_roots_pop(context, &frame);
|
|
2854
|
+
return result;
|
|
2855
|
+
}
|
|
2856
|
+
|
|
2857
|
+
/* The eight flag code units, in the order `flags` prints them. */
|
|
2858
|
+
static const char regexp_flag_letters[] = "dgimsuvy";
|
|
2859
|
+
|
|
2860
|
+
static const uint16_t regexp_flag_bits[] = {
|
|
2861
|
+
OSEO_REGEXP_FLAG_D,
|
|
2862
|
+
OSEO_REGEXP_FLAG_G,
|
|
2863
|
+
OSEO_REGEXP_FLAG_I,
|
|
2864
|
+
OSEO_REGEXP_FLAG_M,
|
|
2865
|
+
OSEO_REGEXP_FLAG_S,
|
|
2866
|
+
OSEO_REGEXP_FLAG_U,
|
|
2867
|
+
OSEO_REGEXP_FLAG_V,
|
|
2868
|
+
OSEO_REGEXP_FLAG_Y,
|
|
2869
|
+
};
|
|
2870
|
+
|
|
2871
|
+
/*
|
|
2872
|
+
* One individual flag accessor (22.2.6.4 and its neighbors).
|
|
2873
|
+
*
|
|
2874
|
+
* A receiver without the internal slot is a type error unless it is the
|
|
2875
|
+
* prototype itself, which answers undefined so that
|
|
2876
|
+
* `RegExp.prototype.flags` stays the empty string.
|
|
2877
|
+
*/
|
|
2878
|
+
static OseoResult regexp_flag_accessor(
|
|
2879
|
+
OseoContext *context,
|
|
2880
|
+
OseoValue receiver,
|
|
2881
|
+
size_t flag
|
|
2882
|
+
) {
|
|
2883
|
+
if (!is_object(receiver)) {
|
|
2884
|
+
return oseo_internal_throw_error(
|
|
2885
|
+
context,
|
|
2886
|
+
OSEO_ERROR_TYPE,
|
|
2887
|
+
"A RegExp flag accessor requires an object receiver."
|
|
2888
|
+
);
|
|
2889
|
+
}
|
|
2890
|
+
if (!is_regexp(receiver)) {
|
|
2891
|
+
return regexp_prototype_receiver(
|
|
2892
|
+
context,
|
|
2893
|
+
receiver,
|
|
2894
|
+
"A RegExp flag accessor requires a RegExp object.",
|
|
2895
|
+
false
|
|
2896
|
+
);
|
|
2897
|
+
}
|
|
2898
|
+
uint16_t mask =
|
|
2899
|
+
regexp_matcher_object(regexp_object(receiver)->matcher)->flag_mask;
|
|
2900
|
+
return normal(oseo_boolean((mask & regexp_flag_bits[flag]) != 0u));
|
|
2901
|
+
}
|
|
2902
|
+
|
|
2903
|
+
/*
|
|
2904
|
+
* The `flags` accessor (22.2.6.5), which reads the eight individual
|
|
2905
|
+
* accessors through ordinary property lookup rather than the internal
|
|
2906
|
+
* slot, so an overridden accessor is observed.
|
|
2907
|
+
*/
|
|
2908
|
+
static OseoResult regexp_prototype_flags(
|
|
2909
|
+
OseoContext *context,
|
|
2910
|
+
OseoValue receiver
|
|
2911
|
+
) {
|
|
2912
|
+
if (!is_object(receiver)) {
|
|
2913
|
+
return oseo_internal_throw_error(
|
|
2914
|
+
context,
|
|
2915
|
+
OSEO_ERROR_TYPE,
|
|
2916
|
+
"The RegExp flags accessor requires an object receiver."
|
|
2917
|
+
);
|
|
2918
|
+
}
|
|
2919
|
+
OseoValue slots[2] = {receiver, oseo_undefined()};
|
|
2920
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
2921
|
+
oseo_roots_push(context, &frame);
|
|
2922
|
+
uint16_t units[8];
|
|
2923
|
+
size_t length = 0u;
|
|
2924
|
+
OseoResult result = normal(oseo_undefined());
|
|
2925
|
+
static const char *const names[] = {
|
|
2926
|
+
"hasIndices",
|
|
2927
|
+
"global",
|
|
2928
|
+
"ignoreCase",
|
|
2929
|
+
"multiline",
|
|
2930
|
+
"dotAll",
|
|
2931
|
+
"unicode",
|
|
2932
|
+
"unicodeSets",
|
|
2933
|
+
"sticky",
|
|
2934
|
+
};
|
|
2935
|
+
for (size_t index = 0u; index < 8u; index += 1u) {
|
|
2936
|
+
result = regexp_get_named(
|
|
2937
|
+
context,
|
|
2938
|
+
frame.slots[0],
|
|
2939
|
+
names[index],
|
|
2940
|
+
&frame.slots[1]
|
|
2941
|
+
);
|
|
2942
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
2943
|
+
if (oseo_to_boolean(frame.slots[1])) {
|
|
2944
|
+
units[length] =
|
|
2945
|
+
(uint16_t)(unsigned char)regexp_flag_letters[index];
|
|
2946
|
+
length += 1u;
|
|
2947
|
+
}
|
|
2948
|
+
}
|
|
2949
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2950
|
+
result = oseo_internal_allocate_string(context, units, length);
|
|
2951
|
+
}
|
|
2952
|
+
oseo_roots_pop(context, &frame);
|
|
2953
|
+
return result;
|
|
2954
|
+
}
|
|
2955
|
+
|
|
2956
|
+
/*
|
|
2957
|
+
* `toString` (22.2.6.17), which reads `source` and `flags` generically
|
|
2958
|
+
* so that any object carrying them stringifies.
|
|
2959
|
+
*/
|
|
2960
|
+
static OseoResult regexp_prototype_to_string(
|
|
2961
|
+
OseoContext *context,
|
|
2962
|
+
OseoValue receiver
|
|
2963
|
+
) {
|
|
2964
|
+
if (!is_object(receiver)) {
|
|
2965
|
+
return oseo_internal_throw_error(
|
|
2966
|
+
context,
|
|
2967
|
+
OSEO_ERROR_TYPE,
|
|
2968
|
+
"RegExp.prototype.toString requires an object receiver."
|
|
2969
|
+
);
|
|
2970
|
+
}
|
|
2971
|
+
OseoValue slots[3] = {receiver, oseo_undefined(), oseo_undefined()};
|
|
2972
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
2973
|
+
oseo_roots_push(context, &frame);
|
|
2974
|
+
OseoResult result = regexp_get_named(
|
|
2975
|
+
context,
|
|
2976
|
+
frame.slots[0],
|
|
2977
|
+
"source",
|
|
2978
|
+
&frame.slots[1]
|
|
2979
|
+
);
|
|
2980
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2981
|
+
result = oseo_internal_value_string(context, frame.slots[1]);
|
|
2982
|
+
frame.slots[1] = result.value;
|
|
2983
|
+
}
|
|
2984
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2985
|
+
result = regexp_get_named(
|
|
2986
|
+
context,
|
|
2987
|
+
frame.slots[0],
|
|
2988
|
+
"flags",
|
|
2989
|
+
&frame.slots[2]
|
|
2990
|
+
);
|
|
2991
|
+
}
|
|
2992
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
2993
|
+
result = oseo_internal_value_string(context, frame.slots[2]);
|
|
2994
|
+
frame.slots[2] = result.value;
|
|
2995
|
+
}
|
|
2996
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
2997
|
+
oseo_roots_pop(context, &frame);
|
|
2998
|
+
return result;
|
|
2999
|
+
}
|
|
3000
|
+
const OseoString *pattern = string_object(frame.slots[1]);
|
|
3001
|
+
const OseoString *flags = string_object(frame.slots[2]);
|
|
3002
|
+
/*
|
|
3003
|
+
* `source` and `flags` are arbitrary strings here, because this
|
|
3004
|
+
* method is generic. Two individually valid ones can concatenate
|
|
3005
|
+
* past the maximum string length, which is the same catchable
|
|
3006
|
+
* `RangeError` ordinary concatenation reports, so the length is
|
|
3007
|
+
* checked before a staging buffer that large is requested.
|
|
3008
|
+
*/
|
|
3009
|
+
if (pattern->length > SIZE_MAX - flags->length - 2u) {
|
|
3010
|
+
oseo_roots_pop(context, &frame);
|
|
3011
|
+
return failure(
|
|
3012
|
+
context,
|
|
3013
|
+
"OSEO2001",
|
|
3014
|
+
"RegExp stringification is too large."
|
|
3015
|
+
);
|
|
3016
|
+
}
|
|
3017
|
+
size_t length = pattern->length + flags->length + 2u;
|
|
3018
|
+
result = oseo_internal_validate_string_length(context, length);
|
|
3019
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
3020
|
+
oseo_roots_pop(context, &frame);
|
|
3021
|
+
return result;
|
|
3022
|
+
}
|
|
3023
|
+
uint16_t *units = oseo_internal_allocate_work_bytes(
|
|
3024
|
+
context,
|
|
3025
|
+
length * sizeof(uint16_t)
|
|
3026
|
+
);
|
|
3027
|
+
if (units == NULL) {
|
|
3028
|
+
oseo_roots_pop(context, &frame);
|
|
3029
|
+
return failure(
|
|
3030
|
+
context,
|
|
3031
|
+
"OSEO2001",
|
|
3032
|
+
"RegExp stringification allocation failed."
|
|
3033
|
+
);
|
|
3034
|
+
}
|
|
3035
|
+
units[0] = '/';
|
|
3036
|
+
memcpy(&units[1], pattern->units, pattern->length * sizeof(uint16_t));
|
|
3037
|
+
units[pattern->length + 1u] = '/';
|
|
3038
|
+
memcpy(
|
|
3039
|
+
&units[pattern->length + 2u],
|
|
3040
|
+
flags->units,
|
|
3041
|
+
flags->length * sizeof(uint16_t)
|
|
3042
|
+
);
|
|
3043
|
+
result = oseo_internal_allocate_string(context, units, length);
|
|
3044
|
+
free(units);
|
|
3045
|
+
oseo_roots_pop(context, &frame);
|
|
3046
|
+
return result;
|
|
3047
|
+
}
|
|
3048
|
+
|
|
3049
|
+
OseoResult oseo_internal_regexp_builtin_dispatch(
|
|
3050
|
+
OseoContext *context,
|
|
3051
|
+
size_t code_id,
|
|
3052
|
+
OseoValue callee,
|
|
3053
|
+
OseoValue receiver,
|
|
3054
|
+
size_t argument_count,
|
|
3055
|
+
const OseoValue *arguments,
|
|
3056
|
+
OseoValue new_target
|
|
3057
|
+
) {
|
|
3058
|
+
if (code_id == OSEO_REGEXP_CONSTRUCTOR_CODE_ID) {
|
|
3059
|
+
return regexp_construct(
|
|
3060
|
+
context,
|
|
3061
|
+
callee,
|
|
3062
|
+
argument_count,
|
|
3063
|
+
arguments,
|
|
3064
|
+
new_target
|
|
3065
|
+
);
|
|
3066
|
+
}
|
|
3067
|
+
if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
|
|
3068
|
+
return oseo_internal_throw_error(
|
|
3069
|
+
context,
|
|
3070
|
+
OSEO_ERROR_TYPE,
|
|
3071
|
+
"RegExp method is not a constructor."
|
|
3072
|
+
);
|
|
3073
|
+
}
|
|
3074
|
+
if (code_id == OSEO_REGEXP_SPECIES_CODE_ID) return normal(receiver);
|
|
3075
|
+
if (code_id == OSEO_REGEXP_EXEC_CODE_ID) {
|
|
3076
|
+
return regexp_prototype_exec(
|
|
3077
|
+
context,
|
|
3078
|
+
receiver,
|
|
3079
|
+
argument_count,
|
|
3080
|
+
arguments
|
|
3081
|
+
);
|
|
3082
|
+
}
|
|
3083
|
+
if (code_id == OSEO_REGEXP_TEST_CODE_ID) {
|
|
3084
|
+
return regexp_prototype_test(
|
|
3085
|
+
context,
|
|
3086
|
+
receiver,
|
|
3087
|
+
argument_count,
|
|
3088
|
+
arguments
|
|
3089
|
+
);
|
|
3090
|
+
}
|
|
3091
|
+
if (code_id == OSEO_REGEXP_TO_STRING_CODE_ID) {
|
|
3092
|
+
return regexp_prototype_to_string(context, receiver);
|
|
3093
|
+
}
|
|
3094
|
+
if (code_id == OSEO_REGEXP_COMPILE_CODE_ID) {
|
|
3095
|
+
return regexp_prototype_compile(
|
|
3096
|
+
context,
|
|
3097
|
+
receiver,
|
|
3098
|
+
argument_count,
|
|
3099
|
+
arguments
|
|
3100
|
+
);
|
|
3101
|
+
}
|
|
3102
|
+
if (code_id == OSEO_REGEXP_FLAGS_CODE_ID) {
|
|
3103
|
+
return regexp_prototype_flags(context, receiver);
|
|
3104
|
+
}
|
|
3105
|
+
if (code_id == OSEO_REGEXP_SOURCE_CODE_ID) {
|
|
3106
|
+
return regexp_prototype_source(context, receiver);
|
|
3107
|
+
}
|
|
3108
|
+
if (code_id >= OSEO_REGEXP_FLAG_ACCESSOR_CODE_ID_FIRST &&
|
|
3109
|
+
code_id <= OSEO_REGEXP_FLAG_ACCESSOR_CODE_ID_LAST) {
|
|
3110
|
+
return regexp_flag_accessor(
|
|
3111
|
+
context,
|
|
3112
|
+
receiver,
|
|
3113
|
+
OSEO_REGEXP_FLAG_ACCESSOR_CODE_ID_LAST - code_id
|
|
3114
|
+
);
|
|
3115
|
+
}
|
|
3116
|
+
if (code_id >= OSEO_REGEXP_ESCAPE_CODE_ID &&
|
|
3117
|
+
code_id <= OSEO_REGEXP_MATCH_CODE_ID) {
|
|
3118
|
+
return oseo_internal_regexp_symbol_dispatch(
|
|
3119
|
+
context,
|
|
3120
|
+
code_id,
|
|
3121
|
+
receiver,
|
|
3122
|
+
argument_count,
|
|
3123
|
+
arguments
|
|
3124
|
+
);
|
|
3125
|
+
}
|
|
3126
|
+
return oseo_unknown_function(context, code_id);
|
|
3127
|
+
}
|
|
3128
|
+
|
|
3129
|
+
static OseoResult create_regexp_builtin(
|
|
3130
|
+
OseoContext *context,
|
|
3131
|
+
size_t code_id,
|
|
3132
|
+
const char *name,
|
|
3133
|
+
size_t length,
|
|
3134
|
+
OseoFunctionKind kind,
|
|
3135
|
+
OseoFunctionNamePrefix prefix
|
|
3136
|
+
) {
|
|
3137
|
+
size_t name_length = strlen(name);
|
|
3138
|
+
uint16_t units[32];
|
|
3139
|
+
if (name_length > sizeof(units) / sizeof(*units)) {
|
|
3140
|
+
return failure(context, "OSEO2001", "Built-in name is too long.");
|
|
3141
|
+
}
|
|
3142
|
+
for (size_t index = 0u; index < name_length; index += 1u) {
|
|
3143
|
+
units[index] = (uint16_t)(unsigned char)name[index];
|
|
3144
|
+
}
|
|
3145
|
+
OseoValue environment = oseo_undefined();
|
|
3146
|
+
OseoRootFrame frame = {NULL, &environment, 1u};
|
|
3147
|
+
oseo_roots_push(context, &frame);
|
|
3148
|
+
OseoResult result = oseo_environment_create(context, 0u);
|
|
3149
|
+
environment = result.value;
|
|
3150
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3151
|
+
result = oseo_function_create(
|
|
3152
|
+
context,
|
|
3153
|
+
code_id,
|
|
3154
|
+
environment,
|
|
3155
|
+
units,
|
|
3156
|
+
name_length,
|
|
3157
|
+
length,
|
|
3158
|
+
kind,
|
|
3159
|
+
oseo_undefined(),
|
|
3160
|
+
oseo_undefined(),
|
|
3161
|
+
prefix
|
|
3162
|
+
);
|
|
3163
|
+
}
|
|
3164
|
+
oseo_roots_pop(context, &frame);
|
|
3165
|
+
return result;
|
|
3166
|
+
}
|
|
3167
|
+
|
|
3168
|
+
static OseoResult define_regexp_property(
|
|
3169
|
+
OseoContext *context,
|
|
3170
|
+
OseoValue object,
|
|
3171
|
+
const char *name,
|
|
3172
|
+
OseoValue value,
|
|
3173
|
+
OseoPropertyAttributes attributes
|
|
3174
|
+
) {
|
|
3175
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
3176
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
3177
|
+
oseo_roots_push(context, &frame);
|
|
3178
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
3179
|
+
slots[2] = result.value;
|
|
3180
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3181
|
+
result = oseo_object_define(
|
|
3182
|
+
context,
|
|
3183
|
+
slots[0],
|
|
3184
|
+
slots[2],
|
|
3185
|
+
slots[1],
|
|
3186
|
+
attributes
|
|
3187
|
+
);
|
|
3188
|
+
}
|
|
3189
|
+
oseo_roots_pop(context, &frame);
|
|
3190
|
+
return result;
|
|
3191
|
+
}
|
|
3192
|
+
|
|
3193
|
+
static OseoResult define_regexp_accessor(
|
|
3194
|
+
OseoContext *context,
|
|
3195
|
+
OseoValue object,
|
|
3196
|
+
const char *name,
|
|
3197
|
+
OseoValue getter
|
|
3198
|
+
) {
|
|
3199
|
+
OseoValue slots[3] = {object, getter, oseo_undefined()};
|
|
3200
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
3201
|
+
oseo_roots_push(context, &frame);
|
|
3202
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
3203
|
+
slots[2] = result.value;
|
|
3204
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3205
|
+
result = oseo_object_define_accessor(
|
|
3206
|
+
context,
|
|
3207
|
+
slots[0],
|
|
3208
|
+
slots[2],
|
|
3209
|
+
slots[1],
|
|
3210
|
+
oseo_undefined(),
|
|
3211
|
+
true,
|
|
3212
|
+
false,
|
|
3213
|
+
(OseoPropertyAttributes){true, false, false, true}
|
|
3214
|
+
);
|
|
3215
|
+
}
|
|
3216
|
+
oseo_roots_pop(context, &frame);
|
|
3217
|
+
return result;
|
|
3218
|
+
}
|
|
3219
|
+
|
|
3220
|
+
static OseoResult regexp_intrinsic_build(OseoContext *context) {
|
|
3221
|
+
OseoValue *marker =
|
|
3222
|
+
&context->realm->intrinsics[OSEO_INTRINSIC_REGEXP_SPECIES];
|
|
3223
|
+
if (tag_of(*marker) == OSEO_TAG_UNINITIALIZED) {
|
|
3224
|
+
return failure(
|
|
3225
|
+
context,
|
|
3226
|
+
"OSEO2001",
|
|
3227
|
+
"The RegExp intrinsic cluster is already being built."
|
|
3228
|
+
);
|
|
3229
|
+
}
|
|
3230
|
+
if (tag_of(*marker) != OSEO_TAG_UNDEFINED) return normal(*marker);
|
|
3231
|
+
size_t entry_allocations = context->allocations;
|
|
3232
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
3233
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 5u);
|
|
3234
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
3235
|
+
*marker = oseo_uninitialized();
|
|
3236
|
+
result = oseo_internal_intrinsic(
|
|
3237
|
+
context,
|
|
3238
|
+
OSEO_INTRINSIC_OBJECT_PROTOTYPE
|
|
3239
|
+
);
|
|
3240
|
+
frame.slots[0] = result.value;
|
|
3241
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3242
|
+
result = oseo_object_create(context, frame.slots[0]);
|
|
3243
|
+
frame.slots[0] = result.value;
|
|
3244
|
+
}
|
|
3245
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3246
|
+
context->realm->intrinsics[OSEO_INTRINSIC_REGEXP_PROTOTYPE] =
|
|
3247
|
+
frame.slots[0];
|
|
3248
|
+
result = create_regexp_builtin(
|
|
3249
|
+
context,
|
|
3250
|
+
OSEO_REGEXP_CONSTRUCTOR_CODE_ID,
|
|
3251
|
+
"RegExp",
|
|
3252
|
+
2u,
|
|
3253
|
+
OSEO_FUNCTION_ORDINARY,
|
|
3254
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
3255
|
+
);
|
|
3256
|
+
frame.slots[1] = result.value;
|
|
3257
|
+
}
|
|
3258
|
+
const OseoPropertyAttributes method = {true, false, true, false};
|
|
3259
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3260
|
+
context->realm->intrinsics[OSEO_INTRINSIC_REGEXP] = frame.slots[1];
|
|
3261
|
+
OseoFunction *constructor = function_object(frame.slots[1]);
|
|
3262
|
+
constructor->prototype_object = frame.slots[0];
|
|
3263
|
+
constructor->prototype_writable = false;
|
|
3264
|
+
result = define_regexp_property(
|
|
3265
|
+
context,
|
|
3266
|
+
frame.slots[0],
|
|
3267
|
+
"constructor",
|
|
3268
|
+
frame.slots[1],
|
|
3269
|
+
method
|
|
3270
|
+
);
|
|
3271
|
+
}
|
|
3272
|
+
static const char *const prototype_methods[] = {
|
|
3273
|
+
"exec",
|
|
3274
|
+
"test",
|
|
3275
|
+
"toString",
|
|
3276
|
+
"compile",
|
|
3277
|
+
};
|
|
3278
|
+
static const size_t prototype_method_lengths[] = {1u, 1u, 0u, 2u};
|
|
3279
|
+
static const size_t prototype_method_codes[] = {
|
|
3280
|
+
OSEO_REGEXP_EXEC_CODE_ID,
|
|
3281
|
+
OSEO_REGEXP_TEST_CODE_ID,
|
|
3282
|
+
OSEO_REGEXP_TO_STRING_CODE_ID,
|
|
3283
|
+
OSEO_REGEXP_COMPILE_CODE_ID,
|
|
3284
|
+
};
|
|
3285
|
+
for (size_t index = 0u;
|
|
3286
|
+
result.status == OSEO_STATUS_NORMAL && index < 4u;
|
|
3287
|
+
index += 1u) {
|
|
3288
|
+
result = create_regexp_builtin(
|
|
3289
|
+
context,
|
|
3290
|
+
prototype_method_codes[index],
|
|
3291
|
+
prototype_methods[index],
|
|
3292
|
+
prototype_method_lengths[index],
|
|
3293
|
+
OSEO_FUNCTION_INTERNAL,
|
|
3294
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
3295
|
+
);
|
|
3296
|
+
frame.slots[2] = result.value;
|
|
3297
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3298
|
+
result = define_regexp_property(
|
|
3299
|
+
context,
|
|
3300
|
+
frame.slots[0],
|
|
3301
|
+
prototype_methods[index],
|
|
3302
|
+
frame.slots[2],
|
|
3303
|
+
method
|
|
3304
|
+
);
|
|
3305
|
+
}
|
|
3306
|
+
}
|
|
3307
|
+
/*
|
|
3308
|
+
* The ten accessors are defined in one pass. `flags` and `source`
|
|
3309
|
+
* own their own code IDs, and the eight individual flag accessors
|
|
3310
|
+
* share one contiguous range in the order `flags` prints them, so an
|
|
3311
|
+
* accessor's code ID names the flag it reports.
|
|
3312
|
+
*/
|
|
3313
|
+
static const char *const prototype_accessors[] = {
|
|
3314
|
+
"flags",
|
|
3315
|
+
"source",
|
|
3316
|
+
"hasIndices",
|
|
3317
|
+
"global",
|
|
3318
|
+
"ignoreCase",
|
|
3319
|
+
"multiline",
|
|
3320
|
+
"dotAll",
|
|
3321
|
+
"unicode",
|
|
3322
|
+
"unicodeSets",
|
|
3323
|
+
"sticky",
|
|
3324
|
+
};
|
|
3325
|
+
for (size_t index = 0u;
|
|
3326
|
+
result.status == OSEO_STATUS_NORMAL && index < 10u;
|
|
3327
|
+
index += 1u) {
|
|
3328
|
+
size_t code_id = index == 0u
|
|
3329
|
+
? OSEO_REGEXP_FLAGS_CODE_ID
|
|
3330
|
+
: index == 1u
|
|
3331
|
+
? OSEO_REGEXP_SOURCE_CODE_ID
|
|
3332
|
+
: OSEO_REGEXP_FLAG_ACCESSOR_CODE_ID_LAST - (index - 2u);
|
|
3333
|
+
result = create_regexp_builtin(
|
|
3334
|
+
context,
|
|
3335
|
+
code_id,
|
|
3336
|
+
prototype_accessors[index],
|
|
3337
|
+
0u,
|
|
3338
|
+
OSEO_FUNCTION_INTERNAL,
|
|
3339
|
+
OSEO_FUNCTION_NAME_PREFIX_GET
|
|
3340
|
+
);
|
|
3341
|
+
frame.slots[2] = result.value;
|
|
3342
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3343
|
+
result = define_regexp_accessor(
|
|
3344
|
+
context,
|
|
3345
|
+
frame.slots[0],
|
|
3346
|
+
prototype_accessors[index],
|
|
3347
|
+
frame.slots[2]
|
|
3348
|
+
);
|
|
3349
|
+
}
|
|
3350
|
+
}
|
|
3351
|
+
static const char *const symbol_methods[] = {
|
|
3352
|
+
"[Symbol.match]",
|
|
3353
|
+
"[Symbol.matchAll]",
|
|
3354
|
+
"[Symbol.replace]",
|
|
3355
|
+
"[Symbol.search]",
|
|
3356
|
+
"[Symbol.split]",
|
|
3357
|
+
};
|
|
3358
|
+
static const size_t symbol_method_symbols[] = {
|
|
3359
|
+
OSEO_WELL_KNOWN_MATCH,
|
|
3360
|
+
OSEO_WELL_KNOWN_MATCH_ALL,
|
|
3361
|
+
OSEO_WELL_KNOWN_REPLACE,
|
|
3362
|
+
OSEO_WELL_KNOWN_SEARCH,
|
|
3363
|
+
OSEO_WELL_KNOWN_SPLIT,
|
|
3364
|
+
};
|
|
3365
|
+
static const size_t symbol_method_codes[] = {
|
|
3366
|
+
OSEO_REGEXP_MATCH_CODE_ID,
|
|
3367
|
+
OSEO_REGEXP_MATCH_ALL_CODE_ID,
|
|
3368
|
+
OSEO_REGEXP_REPLACE_CODE_ID,
|
|
3369
|
+
OSEO_REGEXP_SEARCH_CODE_ID,
|
|
3370
|
+
OSEO_REGEXP_SPLIT_CODE_ID,
|
|
3371
|
+
};
|
|
3372
|
+
static const size_t symbol_method_lengths[] = {1u, 1u, 2u, 1u, 2u};
|
|
3373
|
+
for (size_t index = 0u;
|
|
3374
|
+
result.status == OSEO_STATUS_NORMAL && index < 5u;
|
|
3375
|
+
index += 1u) {
|
|
3376
|
+
result = create_regexp_builtin(
|
|
3377
|
+
context,
|
|
3378
|
+
symbol_method_codes[index],
|
|
3379
|
+
symbol_methods[index],
|
|
3380
|
+
symbol_method_lengths[index],
|
|
3381
|
+
OSEO_FUNCTION_INTERNAL,
|
|
3382
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
3383
|
+
);
|
|
3384
|
+
frame.slots[2] = result.value;
|
|
3385
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3386
|
+
result = oseo_internal_well_known_symbol(
|
|
3387
|
+
context,
|
|
3388
|
+
symbol_method_symbols[index]
|
|
3389
|
+
);
|
|
3390
|
+
frame.slots[3] = result.value;
|
|
3391
|
+
}
|
|
3392
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3393
|
+
result = oseo_object_define(
|
|
3394
|
+
context,
|
|
3395
|
+
frame.slots[0],
|
|
3396
|
+
frame.slots[3],
|
|
3397
|
+
frame.slots[2],
|
|
3398
|
+
method
|
|
3399
|
+
);
|
|
3400
|
+
}
|
|
3401
|
+
}
|
|
3402
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3403
|
+
result = create_regexp_builtin(
|
|
3404
|
+
context,
|
|
3405
|
+
OSEO_REGEXP_ESCAPE_CODE_ID,
|
|
3406
|
+
"escape",
|
|
3407
|
+
1u,
|
|
3408
|
+
OSEO_FUNCTION_INTERNAL,
|
|
3409
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
3410
|
+
);
|
|
3411
|
+
frame.slots[2] = result.value;
|
|
3412
|
+
}
|
|
3413
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3414
|
+
result = define_regexp_property(
|
|
3415
|
+
context,
|
|
3416
|
+
frame.slots[1],
|
|
3417
|
+
"escape",
|
|
3418
|
+
frame.slots[2],
|
|
3419
|
+
method
|
|
3420
|
+
);
|
|
3421
|
+
}
|
|
3422
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3423
|
+
result = create_regexp_builtin(
|
|
3424
|
+
context,
|
|
3425
|
+
OSEO_REGEXP_SPECIES_CODE_ID,
|
|
3426
|
+
"[Symbol.species]",
|
|
3427
|
+
0u,
|
|
3428
|
+
OSEO_FUNCTION_INTERNAL,
|
|
3429
|
+
OSEO_FUNCTION_NAME_PREFIX_GET
|
|
3430
|
+
);
|
|
3431
|
+
frame.slots[2] = result.value;
|
|
3432
|
+
}
|
|
3433
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3434
|
+
result = oseo_internal_well_known_symbol(
|
|
3435
|
+
context,
|
|
3436
|
+
OSEO_WELL_KNOWN_SPECIES
|
|
3437
|
+
);
|
|
3438
|
+
frame.slots[3] = result.value;
|
|
3439
|
+
}
|
|
3440
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3441
|
+
result = oseo_object_define_accessor(
|
|
3442
|
+
context,
|
|
3443
|
+
frame.slots[1],
|
|
3444
|
+
frame.slots[3],
|
|
3445
|
+
frame.slots[2],
|
|
3446
|
+
oseo_undefined(),
|
|
3447
|
+
true,
|
|
3448
|
+
false,
|
|
3449
|
+
(OseoPropertyAttributes){true, false, false, true}
|
|
3450
|
+
);
|
|
3451
|
+
}
|
|
3452
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
3453
|
+
for (size_t index = OSEO_INTRINSIC_REGEXP_PROTOTYPE;
|
|
3454
|
+
index <= OSEO_INTRINSIC_REGEXP_SPECIES;
|
|
3455
|
+
index += 1u) {
|
|
3456
|
+
context->realm->intrinsics[index] = oseo_undefined();
|
|
3457
|
+
}
|
|
3458
|
+
oseo_roots_release(context, &frame);
|
|
3459
|
+
return result;
|
|
3460
|
+
}
|
|
3461
|
+
context->realm->intrinsics[OSEO_INTRINSIC_REGEXP_SPECIES] = frame.slots[2];
|
|
3462
|
+
if (context->observe_specialization) {
|
|
3463
|
+
context->allocations = entry_allocations;
|
|
3464
|
+
}
|
|
3465
|
+
oseo_roots_release(context, &frame);
|
|
3466
|
+
return normal(context->realm->intrinsics[OSEO_INTRINSIC_REGEXP]);
|
|
3467
|
+
}
|
|
3468
|
+
|
|
3469
|
+
OseoResult oseo_internal_regexp_intrinsic(OseoContext *context) {
|
|
3470
|
+
OseoResult built = regexp_intrinsic_build(context);
|
|
3471
|
+
if (built.status != OSEO_STATUS_NORMAL) return built;
|
|
3472
|
+
return normal(context->realm->intrinsics[OSEO_INTRINSIC_REGEXP]);
|
|
3473
|
+
}
|
|
3474
|
+
|
|
3475
|
+
OseoResult oseo_internal_install_regexp_global(
|
|
3476
|
+
OseoContext *context,
|
|
3477
|
+
OseoValue global
|
|
3478
|
+
) {
|
|
3479
|
+
OseoValue slots[2] = {global, oseo_undefined()};
|
|
3480
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
3481
|
+
oseo_roots_push(context, &frame);
|
|
3482
|
+
OseoResult result = oseo_internal_regexp_intrinsic(context);
|
|
3483
|
+
slots[1] = result.value;
|
|
3484
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
3485
|
+
result = define_regexp_property(
|
|
3486
|
+
context,
|
|
3487
|
+
slots[0],
|
|
3488
|
+
"RegExp",
|
|
3489
|
+
slots[1],
|
|
3490
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
3491
|
+
);
|
|
3492
|
+
}
|
|
3493
|
+
oseo_roots_pop(context, &frame);
|
|
3494
|
+
return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
|
|
3495
|
+
}
|