/src/moddable/xs/sources/xsRegExp.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016-2017 Moddable Tech, Inc. |
3 | | * |
4 | | * This file is part of the Moddable SDK Runtime. |
5 | | * |
6 | | * The Moddable SDK Runtime is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * The Moddable SDK Runtime is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | * This file incorporates work covered by the following copyright and |
20 | | * permission notice: |
21 | | * |
22 | | * Copyright (C) 2010-2016 Marvell International Ltd. |
23 | | * Copyright (C) 2002-2010 Kinoma, Inc. |
24 | | * |
25 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
26 | | * you may not use this file except in compliance with the License. |
27 | | * You may obtain a copy of the License at |
28 | | * |
29 | | * http://www.apache.org/licenses/LICENSE-2.0 |
30 | | * |
31 | | * Unless required by applicable law or agreed to in writing, software |
32 | | * distributed under the License is distributed on an "AS IS" BASIS, |
33 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
34 | | * See the License for the specific language governing permissions and |
35 | | * limitations under the License. |
36 | | */ |
37 | | |
38 | | #include "xsAll.h" |
39 | | #ifndef mxRegExp |
40 | | #define mxRegExp 1 |
41 | | #endif |
42 | | #if mxRegExp |
43 | | static txNumber fxAdvanceStringIndex(txMachine* the, txString string, txNumber index, txBoolean unicodeFlag); |
44 | | static txSlot* fxCheckRegExpInstance(txMachine* the, txSlot* slot); |
45 | | static void fxExecuteRegExp(txMachine* the, txSlot* regexp, txSlot* argument); |
46 | | #endif |
47 | | |
48 | | static void fx_RegExp_prototype_get_flag(txMachine* the, txU4 flag); |
49 | | static void fx_RegExp_prototype_split_aux(txMachine* the, txSlot* string, txIndex start, txIndex stop, txSlot* item); |
50 | | |
51 | | void fxBuildRegExp(txMachine* the) |
52 | 23.2k | { |
53 | 23.2k | txSlot* slot; |
54 | 23.2k | mxPush(mxObjectPrototype); |
55 | 23.2k | slot = fxLastProperty(the, fxNewObjectInstance(the)); |
56 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_compile), 0, mxID(_compile), XS_DONT_ENUM_FLAG); |
57 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_exec), 1, mxID(_exec), XS_DONT_ENUM_FLAG); |
58 | 23.2k | mxExecuteRegExpFunction.kind = slot->kind; |
59 | 23.2k | mxExecuteRegExpFunction.value = slot->value; |
60 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_match), 1, mxID(_Symbol_match), XS_DONT_ENUM_FLAG); |
61 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_matchAll), 1, mxID(_Symbol_matchAll), XS_DONT_ENUM_FLAG); |
62 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_replace), 2, mxID(_Symbol_replace), XS_DONT_ENUM_FLAG); |
63 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_search), 1, mxID(_Symbol_search), XS_DONT_ENUM_FLAG); |
64 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_split), 2, mxID(_Symbol_split), XS_DONT_ENUM_FLAG); |
65 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_test), 1, mxID(_test), XS_DONT_ENUM_FLAG); |
66 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG); |
67 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_dotAll), C_NULL, mxID(_dotAll), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
68 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_flags), C_NULL, mxID(_flags), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
69 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_global), C_NULL, mxID(_global), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
70 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_hasIndices), C_NULL, mxID(_hasIndices), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
71 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_ignoreCase), C_NULL, mxID(_ignoreCase), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
72 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_multiline), C_NULL, mxID(_multiline), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
73 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_source), C_NULL, mxID(_source), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
74 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_sticky), C_NULL, mxID(_sticky), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
75 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_unicode), C_NULL, mxID(_unicode), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
76 | 23.2k | #if mxECMAScript2024 |
77 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_RegExp_prototype_get_unicodeSets), C_NULL, mxID(_unicodeSets), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
78 | 23.2k | #endif |
79 | 23.2k | mxRegExpPrototype = *the->stack; |
80 | 23.2k | slot = fxBuildHostConstructor(the, mxCallback(fx_RegExp), 2, mxID(_RegExp)); |
81 | 23.2k | mxRegExpConstructor = *the->stack; |
82 | 23.2k | slot = fxLastProperty(the, slot); |
83 | 23.2k | #if mxECMAScript2025 |
84 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_escape), 1, mxID(_escape), XS_DONT_ENUM_FLAG); |
85 | 23.2k | #endif |
86 | 23.2k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_species_get), C_NULL, mxID(_Symbol_species), XS_DONT_ENUM_FLAG); |
87 | 23.2k | mxPop(); |
88 | 23.2k | fxNewHostFunction(the, mxCallback(fxInitializeRegExp), 2, XS_NO_ID, XS_NO_ID); |
89 | 23.2k | mxInitializeRegExpFunction = *the->stack; |
90 | | |
91 | 23.2k | mxPush(mxIteratorPrototype); |
92 | 23.2k | slot = fxLastProperty(the, fxNewObjectInstance(the)); |
93 | 23.2k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_RegExp_prototype_matchAll_next), 0, mxID(_next), XS_DONT_ENUM_FLAG); |
94 | 23.2k | slot = fxNextStringXProperty(the, slot, "RegExp String Iterator", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
95 | 23.2k | mxPull(mxRegExpStringIteratorPrototype); |
96 | | |
97 | 23.2k | mxPop(); |
98 | 23.2k | } |
99 | | |
100 | | #if mxRegExp |
101 | | txNumber fxAdvanceStringIndex(txMachine* the, txString string, txNumber index, txBoolean unicodeFlag) |
102 | 11.9M | { |
103 | 11.9M | #if mxCESU8 |
104 | 11.9M | if (unicodeFlag) { |
105 | 10.5M | txInteger character; |
106 | 10.5M | txSize offset = fxCacheUnicodeLength(the, string); |
107 | 10.5M | if (index >= offset) |
108 | 76.0k | return index + 1; |
109 | 10.4M | offset = fxCacheUnicodeToUTF8Offset(the, string, (txInteger)index); |
110 | 10.4M | offset = mxPtrDiff(mxStringByteDecode(string + offset, &character) - string); |
111 | 10.4M | if (character == C_EOF) |
112 | 0 | return index + 1; |
113 | 10.4M | return fxCacheUTF8ToUnicodeOffset(the, string, offset); |
114 | 10.4M | } |
115 | 1.38M | #endif |
116 | 1.38M | return index + 1; |
117 | 11.9M | } |
118 | | |
119 | | txSlot* fxCheckRegExpInstance(txMachine* the, txSlot* slot) |
120 | 13.7M | { |
121 | 13.7M | if (slot->kind == XS_REFERENCE_KIND) { |
122 | 13.7M | slot = slot->value.reference; |
123 | 13.7M | if ((slot->next) && (slot->next->flag & XS_INTERNAL_FLAG) && (slot->next->kind == XS_REGEXP_KIND)) |
124 | 13.7M | return slot; |
125 | 13.7M | } |
126 | 13.7M | mxTypeError("this: not a RegExp instance"); |
127 | 0 | return C_NULL; |
128 | 13.7M | } |
129 | | |
130 | | void fxExecuteRegExp(txMachine* the, txSlot* regexp, txSlot* argument) |
131 | 14.1M | { |
132 | 14.1M | mxPushSlot(regexp); |
133 | 14.1M | mxDub(); |
134 | 14.1M | mxGetID(mxID(_exec)); |
135 | 14.1M | if ((the->stack->kind != XS_REFERENCE_KIND) || (!mxIsFunction(the->stack->value.reference))) { |
136 | 33 | the->stack->kind = mxExecuteRegExpFunction.kind; |
137 | 33 | the->stack->value = mxExecuteRegExpFunction.value; |
138 | 33 | } |
139 | 14.1M | mxCall(); |
140 | 14.1M | mxPushSlot(argument); |
141 | 14.1M | mxRunCount(1); |
142 | 14.1M | if ((the->stack->kind != XS_NULL_KIND) && (the->stack->kind != XS_REFERENCE_KIND)) |
143 | 11 | mxTypeError("invalid exec result"); |
144 | 14.1M | } |
145 | | #endif |
146 | | |
147 | | void fxInitializeRegExp(txMachine* the) |
148 | 1.04M | { |
149 | 1.04M | #if mxRegExp |
150 | 1.04M | txSlot* instance = fxToInstance(the, mxThis); |
151 | 1.04M | txSlot* regexp = instance->next; |
152 | 1.04M | txSlot* key = regexp->next; |
153 | 1.04M | txString pattern; |
154 | 1.04M | txString modifier; |
155 | | |
156 | 1.04M | if (mxArgv(0)->kind == XS_UNDEFINED_KIND) |
157 | 6.62k | *mxArgv(0) = mxEmptyString; |
158 | 1.03M | else |
159 | 1.03M | fxToString(the, mxArgv(0)); |
160 | 1.04M | if (mxArgv(1)->kind == XS_UNDEFINED_KIND) |
161 | 201k | *mxArgv(1) = mxEmptyString; |
162 | 842k | else |
163 | 842k | fxToString(the, mxArgv(1)); |
164 | 1.04M | key->kind = (mxArgv(0)->kind == XS_STRING_X_KIND) ? XS_KEY_X_KIND : XS_KEY_KIND; |
165 | 1.04M | pattern = key->value.key.string = mxArgv(0)->value.string; |
166 | 1.04M | modifier = mxArgv(1)->value.string; |
167 | 1.04M | if (!fxCompileRegExp(the, pattern, modifier, ®exp->value.regexp.code, ®exp->value.regexp.data, the->nameBuffer, sizeof(the->nameBuffer))) |
168 | 60.9k | mxSyntaxError("invalid regular expression: %s", the->nameBuffer); |
169 | 982k | *mxResult = *mxThis; |
170 | 982k | #endif |
171 | 982k | } |
172 | | |
173 | | txBoolean fxIsRegExp(txMachine* the, txSlot* slot) |
174 | 1.13M | { |
175 | 1.13M | #if mxRegExp |
176 | 1.13M | if (mxIsReference(slot)) { |
177 | 159k | txSlot* instance = slot->value.reference; |
178 | 159k | mxPushSlot(slot); |
179 | 159k | mxGetID(mxID(_Symbol_match)); |
180 | 159k | if (the->stack->kind != XS_UNDEFINED_KIND) |
181 | 121k | return fxToBoolean(the, the->stack++); |
182 | 38.4k | mxPop(); |
183 | 38.4k | if ((instance->next) && (instance->next->flag & XS_INTERNAL_FLAG) && (instance->next->kind == XS_REGEXP_KIND)) |
184 | 0 | return 1; |
185 | 38.4k | } |
186 | 1.01M | #endif |
187 | 1.01M | return 0; |
188 | 1.13M | } |
189 | | |
190 | | txSlot* fxNewRegExpInstance(txMachine* the) |
191 | 1.04M | { |
192 | 1.04M | #if mxRegExp |
193 | 1.04M | txSlot* instance; |
194 | 1.04M | txSlot* property; |
195 | 1.04M | instance = fxNewObjectInstance(the); |
196 | | |
197 | 1.04M | property = instance->next = fxNewSlot(the); |
198 | 1.04M | property->flag = XS_INTERNAL_FLAG; |
199 | 1.04M | property->kind = XS_REGEXP_KIND; |
200 | 1.04M | property->value.regexp.code = C_NULL; |
201 | 1.04M | property->value.regexp.data = C_NULL; |
202 | | |
203 | 1.04M | property = property->next = fxNewSlot(the); |
204 | 1.04M | property->flag = XS_INTERNAL_FLAG; |
205 | 1.04M | property->kind = (mxEmptyString.kind == XS_STRING_X_KIND) ? XS_KEY_X_KIND : XS_KEY_KIND; |
206 | 1.04M | property->value.key.string = mxEmptyString.value.string; |
207 | 1.04M | property->value.key.sum = 0; |
208 | | |
209 | 1.04M | property = fxNextIntegerProperty(the, property, 0, mxID(_lastIndex), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG); |
210 | | |
211 | 1.04M | return instance; |
212 | | #else |
213 | | return NULL; |
214 | | #endif |
215 | 1.04M | } |
216 | | |
217 | | void fx_RegExp(txMachine* the) |
218 | 885k | { |
219 | 885k | #if mxRegExp |
220 | 885k | txSlot* pattern = ((mxArgc > 0) && (mxArgv(0)->kind != XS_UNDEFINED_KIND)) ? mxArgv(0) : C_NULL; |
221 | 885k | txSlot* flags = ((mxArgc > 1) && (mxArgv(1)->kind != XS_UNDEFINED_KIND)) ? mxArgv(1) : C_NULL; |
222 | 885k | txBoolean patternIsRegExp = (pattern && fxIsRegExp(the, pattern)) ? 1 : 0; |
223 | | |
224 | 885k | if (mxTarget->kind == XS_UNDEFINED_KIND) { |
225 | 43.4k | if (patternIsRegExp && !flags) { |
226 | 21 | mxPushSlot(pattern); |
227 | 21 | mxGetID(mxID(_constructor)); |
228 | 21 | if ((the->stack->kind == mxFunction->kind) && (the->stack->value.reference == mxFunction->value.reference)) { |
229 | 9 | mxPop(); |
230 | 9 | *mxResult = *pattern; |
231 | 9 | return; |
232 | 9 | } |
233 | 12 | mxPop(); |
234 | 12 | } |
235 | 43.4k | mxPushSlot(mxFunction); |
236 | 43.4k | } |
237 | 841k | else { |
238 | 841k | mxPushSlot(mxTarget); |
239 | 841k | } |
240 | 885k | fxGetPrototypeFromConstructor(the, &mxRegExpPrototype); |
241 | 885k | fxNewRegExpInstance(the); |
242 | 885k | mxPush(mxInitializeRegExpFunction); |
243 | 885k | mxCall(); |
244 | 885k | if (patternIsRegExp) { |
245 | 121k | mxPushSlot(pattern); |
246 | 121k | mxGetID(mxID(_source)); |
247 | 121k | if (flags) |
248 | 121k | mxPushSlot(flags); |
249 | 12 | else { |
250 | 12 | mxPushSlot(pattern); |
251 | 12 | mxGetID(mxID(_flags)); |
252 | 12 | } |
253 | 121k | } |
254 | 764k | else { |
255 | 764k | if (pattern) |
256 | 760k | mxPushSlot(pattern); |
257 | 4.26k | else |
258 | 4.26k | mxPushUndefined(); |
259 | 764k | if (flags) |
260 | 721k | mxPushSlot(flags); |
261 | 43.0k | else |
262 | 43.0k | mxPushUndefined(); |
263 | 764k | } |
264 | 885k | mxRunCount(2); |
265 | 885k | mxPullSlot(mxResult); |
266 | | #else |
267 | | mxUnknownError("not built-in"); |
268 | | #endif |
269 | 885k | } |
270 | | |
271 | | void fx_RegExp_prototype_get_flag(txMachine* the, txU4 flag) |
272 | 6.36M | { |
273 | 6.36M | #if mxRegExp |
274 | 6.36M | txSlot* slot = mxThis; |
275 | 6.36M | if (slot->kind == XS_REFERENCE_KIND) { |
276 | 6.36M | slot = slot->value.reference; |
277 | 6.36M | if (slot == mxRegExpPrototype.value.reference) |
278 | 3.01k | return; |
279 | 6.35M | slot = slot->next; |
280 | 6.35M | if ((slot) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_REGEXP_KIND)) { |
281 | 6.35M | txInteger flags = slot->value.regexp.code[0]; |
282 | 6.35M | mxResult->value.boolean = (flags & flag) ? 1 : 0; |
283 | 6.35M | mxResult->kind = XS_BOOLEAN_KIND; |
284 | 6.35M | return; |
285 | 6.35M | } |
286 | 6.35M | } |
287 | 6.36M | mxTypeError("this: not an object"); |
288 | 6.36M | #endif |
289 | 6.36M | } |
290 | | |
291 | | void fx_RegExp_prototype_get_flags(txMachine* the) |
292 | 795k | { |
293 | 795k | #if mxRegExp |
294 | 795k | char flags[9]; |
295 | 795k | txIndex index = 0; |
296 | 795k | if (mxThis->kind != XS_REFERENCE_KIND) |
297 | 13 | mxTypeError("this: not an object"); |
298 | 795k | mxPushSlot(mxThis); |
299 | 795k | mxGetID(mxID(_hasIndices)); |
300 | 795k | if (fxToBoolean(the, the->stack++)) |
301 | 530 | flags[index++] = 'd'; |
302 | 795k | mxPushSlot(mxThis); |
303 | 795k | mxGetID(mxID(_global)); |
304 | 795k | if (fxToBoolean(the, the->stack++)) |
305 | 384k | flags[index++] = 'g'; |
306 | 795k | mxPushSlot(mxThis); |
307 | 795k | mxGetID(mxID(_ignoreCase)); |
308 | 795k | if (fxToBoolean(the, the->stack++)) |
309 | 214k | flags[index++] = 'i'; |
310 | 795k | mxPushSlot(mxThis); |
311 | 795k | mxGetID(mxID(_multiline)); |
312 | 795k | if (fxToBoolean(the, the->stack++)) |
313 | 4.52k | flags[index++] = 'm'; |
314 | 795k | mxPushSlot(mxThis); |
315 | 795k | mxGetID(mxID(_dotAll)); |
316 | 795k | if (fxToBoolean(the, the->stack++)) |
317 | 66.3k | flags[index++] = 's'; |
318 | 795k | mxPushSlot(mxThis); |
319 | 795k | mxGetID(mxID(_unicode)); |
320 | 795k | if (fxToBoolean(the, the->stack++)) |
321 | 243k | flags[index++] = 'u'; |
322 | 795k | #if mxECMAScript2024 |
323 | 795k | mxPushSlot(mxThis); |
324 | 795k | mxGetID(mxID(_unicodeSets)); |
325 | 795k | if (fxToBoolean(the, the->stack++)) |
326 | 77.4k | flags[index++] = 'v'; |
327 | 795k | #endif |
328 | 795k | mxPushSlot(mxThis); |
329 | 795k | mxGetID(mxID(_sticky)); |
330 | 795k | if (fxToBoolean(the, the->stack++)) |
331 | 134 | flags[index++] = 'y'; |
332 | 795k | flags[index] = 0; |
333 | 795k | fxCopyStringC(the, mxResult, flags); |
334 | 795k | #endif |
335 | 795k | } |
336 | | |
337 | | void fx_RegExp_prototype_get_dotAll(txMachine* the) |
338 | 795k | { |
339 | 795k | #if mxRegExp |
340 | 795k | fx_RegExp_prototype_get_flag(the, XS_REGEXP_S); |
341 | 795k | #endif |
342 | 795k | } |
343 | | |
344 | | void fx_RegExp_prototype_get_global(txMachine* the) |
345 | 795k | { |
346 | 795k | #if mxRegExp |
347 | 795k | fx_RegExp_prototype_get_flag(the, XS_REGEXP_G); |
348 | 795k | #endif |
349 | 795k | } |
350 | | |
351 | | void fx_RegExp_prototype_get_hasIndices(txMachine* the) |
352 | 795k | { |
353 | 795k | #if mxRegExp |
354 | 795k | fx_RegExp_prototype_get_flag(the, XS_REGEXP_D); |
355 | 795k | #endif |
356 | 795k | } |
357 | | |
358 | | void fx_RegExp_prototype_get_ignoreCase(txMachine* the) |
359 | 795k | { |
360 | 795k | #if mxRegExp |
361 | 795k | fx_RegExp_prototype_get_flag(the, XS_REGEXP_I); |
362 | 795k | #endif |
363 | 795k | } |
364 | | |
365 | | void fx_RegExp_prototype_get_multiline(txMachine* the) |
366 | 795k | { |
367 | 795k | #if mxRegExp |
368 | 795k | fx_RegExp_prototype_get_flag(the, XS_REGEXP_M); |
369 | 795k | #endif |
370 | 795k | } |
371 | | |
372 | | void fx_RegExp_prototype_get_source(txMachine* the) |
373 | 272k | { |
374 | 272k | #if mxRegExp |
375 | 272k | txSlot* slot = mxThis; |
376 | 272k | if (slot->kind == XS_REFERENCE_KIND) { |
377 | 272k | slot = slot->value.reference; |
378 | 272k | if (slot == mxRegExpPrototype.value.reference) { |
379 | 376 | *mxResult = mxEmptyRegExp; |
380 | 376 | return; |
381 | 376 | } |
382 | 271k | slot = slot->next; |
383 | 271k | if ((slot) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_REGEXP_KIND)) { |
384 | 268k | txString pattern; |
385 | 268k | txInteger escape = 0; |
386 | 268k | txInteger count = 0; |
387 | 268k | txU1 c, d, *s, *r; |
388 | 268k | slot = slot->next; |
389 | 268k | pattern = slot->value.key.string; |
390 | 268k | if (*pattern == 0) { |
391 | 336 | *mxResult = mxEmptyRegExp; |
392 | 336 | return; |
393 | 336 | } |
394 | 268k | s = (txU1*)pattern; |
395 | 268k | d = 0; |
396 | 45.8M | while ((c = *s++)) { |
397 | 45.5M | if ((c == '/') && (d != '\\')) |
398 | 33.7k | escape++; |
399 | 45.5M | else if ((c == 10) || (c == 13)/* || (c == '/')*/) |
400 | 399 | escape++; |
401 | 45.5M | else if ((c == 0xE2) && (s[0] == 0x80) && ((s[1] == 0xA8) || (s[1] == 0xA9))) /* LS || PS */ |
402 | 38.1k | escape += 3; |
403 | 45.5M | d = c; |
404 | 45.5M | count++; |
405 | 45.5M | } |
406 | 268k | if (escape) { |
407 | 49.0k | mxResult->value.string = fxNewChunk(the, count + escape + 1); |
408 | 49.0k | mxResult->kind = XS_STRING_KIND; |
409 | 49.0k | s = (txU1*)slot->value.key.string; |
410 | 49.0k | r = (txU1*)mxResult->value.string; |
411 | 49.0k | d = 0; |
412 | 13.2M | while ((c = *s++)) { |
413 | 13.1M | if ((c == '/') && (d != '\\')) { |
414 | 33.7k | *r++ = '\\'; *r++ = '/'; |
415 | 33.7k | } |
416 | 13.1M | else if (c == 10) { |
417 | 347 | *r++ = '\\'; *r++ = 'n'; |
418 | 347 | } |
419 | 13.1M | else if (c == 13) { |
420 | 52 | *r++ = '\\'; *r++ = 'r'; |
421 | 52 | } |
422 | 13.1M | else if ((c == 0xE2) && (s[0] == 0x80) && (s[1] == 0xA8)) { |
423 | 18 | *r++ = '\\'; *r++ = 'u'; *r++ = '2'; *r++ = '0'; *r++ = '2'; *r++ = '8'; |
424 | 18 | s += 2; |
425 | 18 | } |
426 | 13.1M | else if ((c == 0xE2) && (s[0] == 0x80) && (s[1] == 0xA9)) { |
427 | 38.1k | *r++ = '\\'; *r++ = 'u'; *r++ = '2'; *r++ = '0'; *r++ = '2'; *r++ = '9'; |
428 | 38.1k | s += 2; |
429 | 38.1k | } |
430 | 13.1M | else { |
431 | 13.1M | *r++ = c; |
432 | 13.1M | } |
433 | 13.1M | d = c; |
434 | 13.1M | } |
435 | 49.0k | *r = 0; |
436 | 49.0k | } |
437 | 219k | else { |
438 | 219k | mxResult->value.string = slot->value.string; |
439 | 219k | mxResult->kind = (slot->kind == XS_KEY_X_KIND) ? XS_STRING_X_KIND : XS_STRING_KIND; |
440 | 219k | } |
441 | 268k | return; |
442 | 268k | } |
443 | 271k | } |
444 | 272k | mxTypeError("this: not a RegExp instance"); |
445 | 272k | #endif |
446 | 272k | } |
447 | | |
448 | | void fx_RegExp_prototype_get_sticky(txMachine* the) |
449 | 795k | { |
450 | 795k | #if mxRegExp |
451 | 795k | fx_RegExp_prototype_get_flag(the, XS_REGEXP_Y); |
452 | 795k | #endif |
453 | 795k | } |
454 | | |
455 | | void fx_RegExp_prototype_get_unicode(txMachine* the) |
456 | 795k | { |
457 | 795k | #if mxRegExp |
458 | 795k | fx_RegExp_prototype_get_flag(the, XS_REGEXP_U); |
459 | 795k | #endif |
460 | 795k | } |
461 | | |
462 | | void fx_RegExp_prototype_get_unicodeSets(txMachine* the) |
463 | 795k | { |
464 | 795k | #if mxRegExp |
465 | 795k | fx_RegExp_prototype_get_flag(the, XS_REGEXP_V); |
466 | 795k | #endif |
467 | 795k | } |
468 | | |
469 | | void fx_RegExp_prototype_compile(txMachine* the) |
470 | 0 | { |
471 | 0 | #if mxRegExp |
472 | 0 | *mxResult = *mxThis; |
473 | 0 | #endif |
474 | 0 | } |
475 | | |
476 | | void fx_RegExp_prototype_exec(txMachine* the) |
477 | 13.7M | { |
478 | 13.7M | #if mxRegExp |
479 | 13.7M | txSlot* instance = fxCheckRegExpInstance(the, mxThis); |
480 | 13.7M | txSlot* regexp = instance->next; |
481 | 13.7M | txSlot* argument; |
482 | 13.7M | txSlot* temporary; |
483 | 13.7M | txNumber lastIndex; |
484 | 13.7M | txInteger flags; |
485 | 13.7M | txBoolean globalFlag; |
486 | 13.7M | txBoolean namedFlag; |
487 | 13.7M | txBoolean stickyFlag; |
488 | 13.7M | txBoolean hasIndicesFlag; |
489 | 13.7M | txInteger offset; |
490 | | |
491 | 13.7M | if (mxArgc > 0) |
492 | 13.7M | mxPushSlot(mxArgv(0)); |
493 | 681 | else |
494 | 681 | mxPushUndefined(); |
495 | 13.7M | fxToString(the, the->stack); |
496 | 13.7M | argument = the->stack; |
497 | | |
498 | 13.7M | if (regexp->value.regexp.data == C_NULL) { |
499 | 0 | mxTemporary(temporary); |
500 | 0 | temporary->value.regexp.code = C_NULL; |
501 | 0 | temporary->value.regexp.data = fxAllocateRegExpData(the, regexp->value.regexp.code); |
502 | 0 | temporary->kind = XS_REGEXP_KIND; |
503 | 0 | } |
504 | 13.7M | else |
505 | 13.7M | temporary = regexp; |
506 | | |
507 | 13.7M | mxPushSlot(mxThis); |
508 | 13.7M | mxGetID(mxID(_lastIndex)); |
509 | 13.7M | lastIndex = fxToLength(the, the->stack); |
510 | 13.7M | mxPop(); |
511 | 13.7M | if (lastIndex > 0x7FFFFFFF) |
512 | 34 | lastIndex = 0x7FFFFFFF; |
513 | | |
514 | 13.7M | flags = regexp->value.regexp.code[0]; |
515 | 13.7M | globalFlag = (flags & XS_REGEXP_G) ? 1 : 0; |
516 | 13.7M | namedFlag = (flags & XS_REGEXP_N) ? 1 : 0; |
517 | 13.7M | stickyFlag = (flags & XS_REGEXP_Y) ? 1 : 0; |
518 | 13.7M | hasIndicesFlag = (flags & XS_REGEXP_D) ? 1 : 0; |
519 | 13.7M | offset = (globalFlag || stickyFlag) ? fxCacheUnicodeToUTF8Offset(the, argument->value.string, (txInteger)lastIndex) : 0; |
520 | 13.7M | if ((offset >= 0) && fxMatchRegExp(the, regexp->value.regexp.code, temporary->value.regexp.data, argument->value.string, offset)) { |
521 | 9.50M | txSlot* resultArray; |
522 | 9.50M | txSlot* resultItem; |
523 | 9.50M | txSlot* indicesArray; |
524 | 9.50M | txSlot* indicesItem; |
525 | 9.50M | txSlot* resultObject; |
526 | 9.50M | txSlot* resultProperty; |
527 | 9.50M | txSlot* indicesObject; |
528 | 9.50M | txSlot* indicesProperty; |
529 | 9.50M | txInteger captureCount; |
530 | 9.50M | txInteger nameCount; |
531 | 9.50M | txInteger captureIndex; |
532 | 9.50M | txInteger nameIndex; |
533 | 9.50M | txInteger length; |
534 | 9.50M | if (globalFlag || stickyFlag) { |
535 | 9.48M | offset = fxCacheUTF8ToUnicodeOffset(the, argument->value.string, temporary->value.regexp.data[1]); |
536 | 9.48M | mxPushInteger(offset); |
537 | 9.48M | mxPushSlot(mxThis); |
538 | 9.48M | mxSetID(mxID(_lastIndex)); |
539 | 9.48M | mxPop(); |
540 | 9.48M | } |
541 | 9.50M | captureCount = regexp->value.regexp.code[1]; |
542 | 9.50M | nameCount = regexp->value.regexp.code[2]; |
543 | 9.50M | mxPush(mxArrayPrototype); |
544 | 9.50M | resultArray = fxNewArrayInstance(the); |
545 | 9.50M | resultItem = fxLastProperty(the, resultArray); |
546 | 22.0M | for (captureIndex = 0; captureIndex < captureCount; captureIndex++) { |
547 | 12.5M | txInteger start = temporary->value.regexp.data[2 * captureIndex]; |
548 | 12.5M | resultItem = resultItem->next = fxNewSlot(the); |
549 | 12.5M | if (start >= 0) { |
550 | 12.3M | txInteger end = temporary->value.regexp.data[(2 * captureIndex) + 1]; |
551 | 12.3M | length = end - start; |
552 | 12.3M | resultItem->value.string = (txString)fxNewChunk(the, length + 1); |
553 | 12.3M | c_memcpy(resultItem->value.string, argument->value.string + start, length); |
554 | 12.3M | resultItem->value.string[length] = 0; |
555 | 12.3M | resultItem->kind = XS_STRING_KIND; |
556 | 12.3M | } |
557 | 12.5M | resultArray->next->value.array.length++; |
558 | 12.5M | } |
559 | 9.50M | fxCacheArray(the, resultArray); |
560 | 9.50M | resultItem = fxLastProperty(the, resultArray); |
561 | 9.50M | resultItem = resultItem->next = fxNewSlot(the); |
562 | 9.50M | resultItem->ID = mxID(_index); |
563 | 9.50M | resultItem->kind = XS_INTEGER_KIND; |
564 | 9.50M | resultItem->value.integer = fxCacheUTF8ToUnicodeOffset(the, argument->value.string, temporary->value.regexp.data[0]); |
565 | 9.50M | resultItem = resultItem->next = fxNewSlot(the); |
566 | 9.50M | resultItem->ID = mxID(_input); |
567 | 9.50M | resultItem->value.string = argument->value.string; |
568 | 9.50M | resultItem->kind = argument->kind; |
569 | 9.50M | resultItem = resultItem->next = fxNewSlot(the); |
570 | 9.50M | resultItem->ID = mxID(_groups); |
571 | 9.50M | if (namedFlag) { |
572 | 2.94k | resultObject = fxNewInstance(the); |
573 | 2.94k | resultProperty = fxLastProperty(the, resultObject); |
574 | 7.96k | for (nameIndex = 0; nameIndex < nameCount; nameIndex++) { |
575 | 5.01k | txID name = (txID)(regexp->value.regexp.code[5 + nameIndex]); |
576 | 5.01k | resultProperty = resultProperty->next = fxNewSlot(the); |
577 | 5.01k | resultProperty->ID = name; |
578 | 5.01k | captureIndex = regexp->value.regexp.data[(2 * captureCount) + nameIndex]; |
579 | 5.01k | if (captureIndex >= 0) { |
580 | 3.94k | mxPushReference(resultArray); |
581 | 3.94k | mxGetIndex(captureIndex); |
582 | 3.94k | mxPullSlot(resultProperty); |
583 | 3.94k | } |
584 | 5.01k | } |
585 | 2.94k | mxPullSlot(resultItem); |
586 | 2.94k | } |
587 | 9.50M | if (hasIndicesFlag) { |
588 | 74.6k | mxPush(mxArrayPrototype); |
589 | 74.6k | indicesArray = fxNewArrayInstance(the); |
590 | 74.6k | indicesItem = fxLastProperty(the, indicesArray); |
591 | 242k | for (captureIndex = 0; captureIndex < captureCount; captureIndex++) { |
592 | 168k | txInteger start = temporary->value.regexp.data[2 * captureIndex]; |
593 | 168k | indicesItem = indicesItem->next = fxNewSlot(the); |
594 | 168k | if (start >= 0) { |
595 | 75.6k | txInteger end = temporary->value.regexp.data[(2 * captureIndex) + 1]; |
596 | 75.6k | length = end - start; |
597 | 75.6k | start = fxCacheUTF8ToUnicodeOffset(the, argument->value.string, start); |
598 | 75.6k | end = start + fxUTF8ToUnicodeOffset(argument->value.string + start, length); |
599 | 75.6k | mxPushInteger(start); |
600 | 75.6k | mxPushInteger(end); |
601 | 75.6k | fxConstructArrayEntry(the, indicesItem); |
602 | 75.6k | } |
603 | 168k | indicesArray->next->value.array.length++; |
604 | 168k | } |
605 | 74.6k | fxCacheArray(the, indicesArray); |
606 | 74.6k | indicesItem = fxLastProperty(the, indicesArray); |
607 | 74.6k | indicesItem = indicesItem->next = fxNewSlot(the); |
608 | 74.6k | indicesItem->ID = mxID(_groups); |
609 | 74.6k | if (namedFlag) { |
610 | 8 | indicesObject = fxNewInstance(the); |
611 | 8 | indicesProperty = fxLastProperty(the, indicesObject); |
612 | 23 | for (nameIndex = 0; nameIndex < nameCount; nameIndex++) { |
613 | 15 | txID name = (txID)(regexp->value.regexp.code[5 + nameIndex]); |
614 | 15 | indicesProperty = indicesProperty->next = fxNewSlot(the); |
615 | 15 | indicesProperty->ID = name; |
616 | 15 | captureIndex = regexp->value.regexp.data[(2 * captureCount) + nameIndex]; |
617 | 15 | if (captureIndex >= 0) { |
618 | 12 | mxPushReference(indicesArray); |
619 | 12 | mxGetIndex(captureIndex); |
620 | 12 | mxPullSlot(indicesProperty); |
621 | 12 | } |
622 | 15 | } |
623 | 8 | mxPullSlot(indicesItem); |
624 | 8 | } |
625 | 74.6k | resultItem = resultItem->next = fxNewSlot(the); |
626 | 74.6k | resultItem->ID = mxID(_indices); |
627 | 74.6k | mxPullSlot(resultItem); |
628 | 74.6k | } |
629 | 9.50M | } |
630 | 4.24M | else { |
631 | 4.24M | if (globalFlag || stickyFlag) { |
632 | 4.05M | mxPushInteger(0); |
633 | 4.05M | mxPushSlot(mxThis); |
634 | 4.05M | mxSetID(mxID(_lastIndex)); |
635 | 4.05M | mxPop(); |
636 | 4.05M | } |
637 | 4.24M | mxPushNull(); |
638 | 4.24M | } |
639 | 13.7M | mxPullSlot(mxResult); |
640 | 13.7M | #endif |
641 | 13.7M | } |
642 | | |
643 | | void fx_RegExp_prototype_match(txMachine* the) |
644 | 365k | { |
645 | 365k | #if mxRegExp |
646 | 365k | txSlot* argument; |
647 | 365k | txSlot* flags; |
648 | 365k | fxToInstance(the, mxThis); |
649 | 365k | if (mxArgc == 0) |
650 | 10 | mxPushUndefined(); |
651 | 365k | else |
652 | 365k | mxPushSlot(mxArgv(0)); |
653 | 365k | argument = the->stack; |
654 | 365k | fxToString(the, argument); |
655 | 365k | mxPushSlot(mxThis); |
656 | 365k | mxGetID(mxID(_flags)); |
657 | 365k | flags = the->stack; |
658 | 365k | if (c_strchr(fxToString(the, flags), 'g')) { |
659 | 197k | txIndex count = 0; |
660 | 197k | txBoolean unicodeFlag = (c_strchr(fxToString(the, flags), 'u') || c_strchr(fxToString(the, flags), 'v')) ? 1 : 0; |
661 | 197k | mxPushInteger(0); |
662 | 197k | mxPushSlot(mxThis); |
663 | 197k | mxSetID(mxID(_lastIndex)); |
664 | 197k | mxPop(); |
665 | 197k | mxPush(mxArrayPrototype); |
666 | 197k | fxNewArrayInstance(the); |
667 | 197k | mxPullSlot(mxResult); |
668 | 7.07M | for (;;) { |
669 | 7.07M | fxExecuteRegExp(the, mxThis, argument); |
670 | 7.07M | if (the->stack->kind == XS_NULL_KIND) { |
671 | 197k | if (count == 0) |
672 | 62.4k | mxPullSlot(mxResult); |
673 | 135k | else |
674 | 135k | mxPop(); |
675 | 197k | break; |
676 | 197k | } |
677 | 6.87M | mxGetIndex(0); |
678 | 6.87M | fxToString(the, the->stack); |
679 | 6.87M | mxPushSlot(mxResult); |
680 | 6.87M | mxDefineIndex(count, 0, XS_GET_ONLY); |
681 | 6.87M | if (c_isEmpty(the->stack->value.string)) { |
682 | 5.96M | mxPushSlot(mxThis); |
683 | 5.96M | mxGetID(mxID(_lastIndex)); |
684 | 5.96M | fxToLength(the, the->stack); |
685 | 5.96M | the->stack->value.number = fxAdvanceStringIndex(the, argument->value.string, the->stack->value.number, unicodeFlag); |
686 | 5.96M | mxPushSlot(mxThis); |
687 | 5.96M | mxSetID(mxID(_lastIndex)); |
688 | 5.96M | mxPop(); |
689 | 5.96M | } |
690 | 6.87M | mxPop(); |
691 | 6.87M | count++; |
692 | 6.87M | mxCheckMetering(); |
693 | 6.87M | } |
694 | 197k | } |
695 | 167k | else { |
696 | 167k | fxExecuteRegExp(the, mxThis, argument); |
697 | 167k | mxPullSlot(mxResult); |
698 | 167k | } |
699 | 365k | mxPop(); |
700 | 365k | mxPop(); |
701 | 365k | #endif |
702 | 365k | } |
703 | | |
704 | | void fx_RegExp_prototype_matchAll(txMachine* the) |
705 | 373 | { |
706 | 373 | #if mxRegExp |
707 | 373 | txSlot* argument; |
708 | 373 | txBoolean globalFlag = 0; |
709 | 373 | txBoolean unicodeFlag = 0; |
710 | 373 | txSlot* matcher; |
711 | 373 | txSlot* iterator; |
712 | 373 | txSlot* property; |
713 | | |
714 | 373 | if (!mxIsReference(mxThis)) |
715 | 11 | mxTypeError("this: not an object"); |
716 | 362 | if (mxArgc == 0) |
717 | 0 | mxPushUndefined(); |
718 | 362 | else |
719 | 362 | mxPushSlot(mxArgv(0)); |
720 | 362 | argument = the->stack; |
721 | 362 | fxToString(the, argument); |
722 | | |
723 | 362 | mxPushSlot(mxThis); |
724 | 362 | mxGetID(mxID(_constructor)); |
725 | 362 | fxToSpeciesConstructor(the, &mxRegExpConstructor); |
726 | 362 | mxNew(); |
727 | 362 | mxPushSlot(mxThis); |
728 | 362 | mxPushSlot(mxThis); |
729 | 362 | mxGetID(mxID(_flags)); |
730 | 362 | if (c_strchr(fxToString(the, the->stack), 'g')) |
731 | 343 | globalFlag = 1; |
732 | 362 | if (c_strchr(fxToString(the, the->stack), 'u') || c_strchr(fxToString(the, the->stack), 'v')) |
733 | 1 | unicodeFlag = 1; |
734 | 362 | mxRunCount(2); |
735 | 362 | matcher = the->stack; |
736 | | |
737 | 362 | mxPushSlot(mxThis); |
738 | 362 | mxGetID(mxID(_lastIndex)); |
739 | 362 | fxToInteger(the, the->stack); |
740 | 362 | mxPushSlot(matcher); |
741 | 362 | mxSetID(mxID(_lastIndex)); |
742 | | |
743 | 362 | mxPush(mxRegExpStringIteratorPrototype); |
744 | 362 | iterator = fxNewIteratorInstance(the, matcher, mxID(_RegExp)); |
745 | 362 | property = fxLastProperty(the, iterator); |
746 | 362 | property->kind = argument->kind; |
747 | 362 | property->value = argument->value; |
748 | 362 | property = fxNextBooleanProperty(the, property, globalFlag, XS_NO_ID, XS_INTERNAL_FLAG); |
749 | 362 | property = fxNextBooleanProperty(the, property, unicodeFlag, XS_NO_ID, XS_INTERNAL_FLAG); |
750 | 362 | property = fxNextBooleanProperty(the, property, 0, XS_NO_ID, XS_INTERNAL_FLAG); |
751 | 362 | mxPullSlot(mxResult); |
752 | 362 | #endif |
753 | 362 | } |
754 | | |
755 | | void fx_RegExp_prototype_matchAll_next(txMachine* the) |
756 | 55 | { |
757 | 55 | #if mxRegExp |
758 | 55 | txSlot* iterator = fxCheckIteratorInstance(the, mxThis, mxID(_RegExp)); |
759 | 55 | txSlot* result = iterator->next; |
760 | 55 | txSlot* value = fxCheckIteratorResult(the, result); |
761 | 55 | txSlot* done = value->next; |
762 | 55 | txSlot* matcher = result->next; |
763 | 55 | txSlot* argument = matcher->next; |
764 | 55 | txSlot* global = argument->next; |
765 | 55 | txSlot* unicode = global->next; |
766 | 55 | txSlot* complete = unicode->next; |
767 | 55 | txSlot* match; |
768 | 55 | if (complete->value.boolean) { |
769 | 1 | value->kind = XS_UNDEFINED_KIND; |
770 | 1 | done->value.boolean = 1; |
771 | 1 | } |
772 | 54 | else { |
773 | 54 | fxExecuteRegExp(the, matcher, argument); |
774 | 54 | match = the->stack; |
775 | 54 | if (match->kind == XS_NULL_KIND) { |
776 | 7 | value->kind = XS_UNDEFINED_KIND; |
777 | 7 | done->value.boolean = 1; |
778 | 7 | complete->value.boolean = 1; |
779 | 7 | } |
780 | 47 | else if (global->value.boolean) { |
781 | 38 | mxPushSlot(match); |
782 | 38 | mxGetIndex(0); |
783 | 38 | fxToString(the, the->stack); |
784 | 38 | if (c_isEmpty(the->stack->value.string)) { |
785 | 30 | mxPushSlot(matcher); |
786 | 30 | mxGetID(mxID(_lastIndex)); |
787 | 30 | fxToLength(the, the->stack); |
788 | 30 | the->stack->value.number = fxAdvanceStringIndex(the, argument->value.string, the->stack->value.number, unicode->value.boolean); |
789 | 30 | mxPushSlot(matcher); |
790 | 30 | mxSetID(mxID(_lastIndex)); |
791 | 30 | mxPop(); |
792 | 30 | } |
793 | 38 | mxPop(); |
794 | 38 | value->kind = match->kind; |
795 | 38 | value->value = match->value; |
796 | 38 | done->value.boolean = 0; |
797 | 38 | } |
798 | 9 | else { |
799 | 9 | value->kind = match->kind; |
800 | 9 | value->value = match->value; |
801 | 9 | done->value.boolean = 0; |
802 | 9 | complete->value.boolean = 1; |
803 | 9 | } |
804 | 54 | mxPop(); |
805 | 54 | } |
806 | 55 | mxResult->kind = result->kind; |
807 | 55 | mxResult->value = result->value; |
808 | 55 | #endif |
809 | 55 | } |
810 | | |
811 | | void fx_RegExp_prototype_replace(txMachine* the) |
812 | 160k | { |
813 | 160k | #if mxRegExp |
814 | 160k | txSlot* argument; |
815 | 160k | txSlot* function = C_NULL;; |
816 | 160k | txSlot* replacement = C_NULL;; |
817 | 160k | txSlot* flags; |
818 | 160k | txBoolean globalFlag = 0; |
819 | 160k | txBoolean unicodeFlag = 0; |
820 | 160k | txSlot* list; |
821 | 160k | txSlot* item; |
822 | 160k | txSize size; |
823 | 160k | txSize utf8Size; |
824 | 160k | txInteger former; |
825 | 160k | txSlot* result; |
826 | 160k | txSlot* matched; |
827 | 160k | txSize matchLength; |
828 | 160k | txInteger c, i; |
829 | 160k | txInteger position; |
830 | 160k | fxToInstance(the, mxThis); |
831 | 160k | if (mxArgc <= 0) |
832 | 10 | mxPushUndefined(); |
833 | 160k | else |
834 | 160k | mxPushSlot(mxArgv(0)); |
835 | 160k | argument = the->stack; |
836 | 160k | fxToString(the, argument); |
837 | 160k | if (mxArgc <= 1) |
838 | 13 | mxPushUndefined(); |
839 | 160k | else |
840 | 160k | mxPushSlot(mxArgv(1)); |
841 | 160k | if (mxIsReference(the->stack) && mxIsFunction(the->stack->value.reference)) |
842 | 42 | function = the->stack; |
843 | 160k | else { |
844 | 160k | replacement = the->stack; |
845 | 160k | fxToString(the, replacement); |
846 | 160k | } |
847 | 160k | mxPushSlot(mxThis); |
848 | 160k | mxGetID(mxID(_flags)); |
849 | 160k | flags = the->stack; |
850 | 160k | if (c_strchr(fxToString(the, flags), 'g')) |
851 | 159k | globalFlag = 1; |
852 | 160k | if (globalFlag) { |
853 | 159k | if (c_strchr(fxToString(the, flags), 'u') || c_strchr(fxToString(the, flags), 'v')) |
854 | 27.2k | unicodeFlag = 1; |
855 | 159k | mxPushInteger(0); |
856 | 159k | mxPushSlot(mxThis); |
857 | 159k | mxSetID(mxID(_lastIndex)); |
858 | 159k | mxPop(); |
859 | 159k | } |
860 | 160k | list = item = fxNewInstance(the); |
861 | 160k | mxPushSlot(list); |
862 | 160k | size = fxCacheUnicodeLength(the, argument->value.string); |
863 | 160k | utf8Size = fxCacheUTF8Length(the, argument->value.string); |
864 | 160k | former = 0; |
865 | 3.15M | for (;;) { |
866 | 3.15M | fxExecuteRegExp(the, mxThis, argument); |
867 | 3.15M | if (the->stack->kind == XS_NULL_KIND) { |
868 | 159k | mxPop(); |
869 | 159k | break; |
870 | 159k | } |
871 | 2.99M | result = the->stack; |
872 | | |
873 | 2.99M | mxPushSlot(result); |
874 | 2.99M | mxGetID(mxID(_index)); |
875 | 2.99M | position = fxToInteger(the, the->stack); |
876 | 2.99M | mxPop(); |
877 | 2.99M | if (position < 0) position = 0; |
878 | 2.99M | else if (position > size) position = size; |
879 | | |
880 | 2.99M | if (former < position) { |
881 | 2.14M | item = item->next = fxNewSlot(the); |
882 | 2.14M | fx_RegExp_prototype_split_aux(the, argument, former, position, item); |
883 | 2.14M | } |
884 | | |
885 | 2.99M | if (former <= position) { |
886 | 2.87M | mxPushSlot(result); |
887 | 2.87M | mxGetIndex(0); |
888 | 2.87M | fxToString(the, the->stack); |
889 | 2.87M | matched = the->stack; |
890 | 2.87M | matchLength = fxUnicodeLength(matched->value.string, C_NULL); |
891 | | |
892 | 2.87M | mxPushSlot(result); |
893 | 2.87M | mxGetID(mxID(_length)); |
894 | 2.87M | c = fxToInteger(the, the->stack); |
895 | 2.87M | mxPop(); |
896 | | |
897 | 2.87M | if (function) { |
898 | 129 | mxPushUndefined(); |
899 | 129 | mxPushSlot(function); |
900 | 129 | mxCall(); |
901 | 129 | } |
902 | 2.87M | mxPushSlot(matched); |
903 | 5.99M | for (i = 1; i < c; i++) { |
904 | 3.11M | mxPushSlot(result); |
905 | 3.11M | mxGetIndex(i); |
906 | 3.11M | if (the->stack->kind != XS_UNDEFINED_KIND) |
907 | 2.81M | fxToString(the, the->stack); |
908 | 3.11M | } |
909 | 2.87M | if (function) { |
910 | 129 | mxPushInteger(position); |
911 | 129 | mxPushSlot(argument); |
912 | 129 | mxPushSlot(result); |
913 | 129 | mxGetID(mxID(_groups)); |
914 | 129 | if (mxIsUndefined(the->stack)) { |
915 | 103 | mxPop(); |
916 | 103 | mxRunCount(3 + i - 1); |
917 | 103 | } |
918 | 26 | else { |
919 | 26 | mxRunCount(4 + i - 1); |
920 | 26 | } |
921 | 129 | fxToString(the, the->stack); |
922 | 129 | item = item->next = fxNewSlot(the); |
923 | 129 | mxPullSlot(item); |
924 | 129 | } |
925 | 2.87M | else { |
926 | 2.87M | mxPushSlot(result); |
927 | 2.87M | mxGetID(mxID(_groups)); |
928 | 2.87M | if (!mxIsUndefined(the->stack)) |
929 | 1.11k | fxToInstance(the, the->stack); |
930 | 2.87M | fxPushSubstitutionString(the, argument, utf8Size, fxCacheUnicodeToUTF8Offset(the, argument->value.string, position), matched, mxStringLength(matched->value.string), i - 1, the->stack + 1, the->stack, replacement); |
931 | 2.87M | item = item->next = fxNewSlot(the); |
932 | 2.87M | mxPullSlot(item); |
933 | 2.87M | the->stack += 1 + i; |
934 | 2.87M | } |
935 | 2.87M | former = position + matchLength; |
936 | | |
937 | 2.87M | mxPop(); // matched |
938 | 2.87M | } |
939 | 112k | else |
940 | 112k | matchLength = 0; |
941 | | |
942 | 2.99M | if (!globalFlag) |
943 | 533 | break; |
944 | | |
945 | 2.98M | if (0 == matchLength) { |
946 | 2.24M | mxPushSlot(mxThis); |
947 | 2.24M | mxGetID(mxID(_lastIndex)); |
948 | 2.24M | fxToLength(the, the->stack); |
949 | 2.24M | the->stack->value.number = fxAdvanceStringIndex(the, argument->value.string, the->stack->value.number, unicodeFlag); |
950 | 2.24M | mxPushSlot(mxThis); |
951 | 2.24M | mxSetID(mxID(_lastIndex)); |
952 | 2.24M | mxPop(); |
953 | 2.24M | } |
954 | 2.98M | mxPop(); |
955 | 2.98M | mxCheckMetering(); |
956 | 2.98M | } |
957 | 160k | if (former < size) { |
958 | 75.0k | item = item->next = fxNewSlot(the); |
959 | 75.0k | fx_RegExp_prototype_split_aux(the, argument, former, size, item); |
960 | 75.0k | } |
961 | 160k | size = 0; |
962 | 160k | item = list->next; |
963 | 5.18M | while (item) { |
964 | 5.02M | item->value.key.sum = mxStringLength(item->value.string); |
965 | 5.02M | size = fxAddChunkSizes(the, size, item->value.key.sum); |
966 | 5.02M | item = item->next; |
967 | 5.02M | } |
968 | 160k | size = fxAddChunkSizes(the, size, 1); |
969 | 160k | mxResult->value.string = (txString)fxNewChunk(the, size); |
970 | 160k | size = 0; |
971 | 160k | item = list->next; |
972 | 5.18M | while (item) { |
973 | 5.02M | c_memcpy(mxResult->value.string + size, item->value.string, item->value.key.sum); |
974 | 5.02M | size += item->value.key.sum; |
975 | 5.02M | item = item->next; |
976 | 5.02M | } |
977 | 160k | mxResult->value.string[size] = 0; |
978 | 160k | mxResult->kind = XS_STRING_KIND; |
979 | 160k | mxPop(); |
980 | 160k | mxPop(); |
981 | 160k | mxPop(); |
982 | 160k | #endif |
983 | 160k | } |
984 | | |
985 | | void fx_RegExp_prototype_search(txMachine* the) |
986 | 1.07k | { |
987 | 1.07k | #if mxRegExp |
988 | 1.07k | txSlot* argument; |
989 | 1.07k | txSlot* lastIndex; |
990 | 1.07k | fxToInstance(the, mxThis); |
991 | 1.07k | if (mxArgc == 0) |
992 | 19 | mxPushUndefined(); |
993 | 1.06k | else |
994 | 1.06k | mxPushSlot(mxArgv(0)); |
995 | 1.07k | argument = the->stack; |
996 | 1.07k | fxToString(the, argument); |
997 | 1.07k | mxPushSlot(mxThis); |
998 | 1.07k | mxGetID(mxID(_lastIndex)); |
999 | 1.07k | lastIndex = the->stack; |
1000 | | |
1001 | 1.07k | mxPushInteger(0); |
1002 | 1.07k | if (!fxIsSameValue(the, the->stack, lastIndex, 0)) { |
1003 | 20 | mxPushInteger(0); |
1004 | 20 | mxPushSlot(mxThis); |
1005 | 20 | mxSetID(mxID(_lastIndex)); |
1006 | 20 | mxPop(); |
1007 | 20 | } |
1008 | 1.07k | mxPop(); |
1009 | | |
1010 | 1.07k | fxExecuteRegExp(the, mxThis, argument); |
1011 | 1.07k | if (the->stack->kind == XS_NULL_KIND) { |
1012 | 997 | the->stack->kind = XS_INTEGER_KIND; |
1013 | 997 | the->stack->value.integer = -1; |
1014 | 997 | } |
1015 | 82 | else |
1016 | 82 | mxGetID(mxID(_index)); |
1017 | 1.07k | mxPullSlot(mxResult); |
1018 | | |
1019 | 1.07k | mxPushSlot(mxThis); |
1020 | 1.07k | mxGetID(mxID(_lastIndex)); |
1021 | 1.07k | if (!fxIsSameValue(the, the->stack, lastIndex, 0)) { |
1022 | 10 | mxPushSlot(lastIndex); |
1023 | 10 | mxPushSlot(mxThis); |
1024 | 10 | mxSetID(mxID(_lastIndex)); |
1025 | 10 | mxPop(); |
1026 | 10 | } |
1027 | 1.07k | mxPop(); |
1028 | | |
1029 | 1.07k | mxPop(); |
1030 | 1.07k | #endif |
1031 | 1.07k | } |
1032 | | |
1033 | | void fx_RegExp_prototype_split(txMachine* the) |
1034 | 120k | { |
1035 | 120k | #if mxRegExp |
1036 | 120k | txSlot* argument; |
1037 | 120k | txIndex limit; |
1038 | 120k | txString flags; |
1039 | 120k | txBoolean unicodeFlag = 0; |
1040 | 120k | txSlot* splitter; |
1041 | 120k | txSlot* array; |
1042 | 120k | txSlot* item; |
1043 | 120k | txInteger size, p, q, e, c, i; |
1044 | | |
1045 | 120k | if (!mxIsReference(mxThis)) |
1046 | 13 | mxTypeError("this: not an object"); |
1047 | 120k | if (mxArgc == 0) |
1048 | 16 | mxPushUndefined(); |
1049 | 120k | else |
1050 | 120k | mxPushSlot(mxArgv(0)); |
1051 | 120k | argument = the->stack; |
1052 | 120k | fxToString(the, argument); |
1053 | 120k | limit = ((mxArgc > 1) && (!mxIsUndefined(mxArgv(1)))) ? fxToUnsigned(the, mxArgv(1)) : 0xFFFFFFFF; |
1054 | | |
1055 | 120k | mxPushSlot(mxThis); |
1056 | 120k | mxGetID(mxID(_constructor)); |
1057 | 120k | fxToSpeciesConstructor(the, &mxRegExpConstructor); |
1058 | 120k | mxNew(); |
1059 | 120k | mxPushSlot(mxThis); |
1060 | 120k | mxPushSlot(mxThis); |
1061 | 120k | mxGetID(mxID(_flags)); |
1062 | 120k | flags = fxToString(the, the->stack); |
1063 | 120k | if (c_strchr(flags, 'u') || c_strchr(flags, 'v')) |
1064 | 113k | unicodeFlag = 1; |
1065 | 120k | if (!c_strchr(flags, 'y')) { |
1066 | 120k | mxPushStringC("y"); |
1067 | 120k | fxConcatString(the, the->stack + 1, the->stack); |
1068 | 120k | mxPop(); |
1069 | 120k | } |
1070 | 120k | mxRunCount(2); |
1071 | 120k | splitter = the->stack; |
1072 | | |
1073 | 120k | mxPush(mxArrayPrototype); |
1074 | 120k | array = fxNewArrayInstance(the); |
1075 | 120k | mxPullSlot(mxResult); |
1076 | 120k | item = fxLastProperty(the, array); |
1077 | 120k | if (!limit) |
1078 | 126 | goto bail; |
1079 | 120k | size = fxUnicodeLength(argument->value.string, C_NULL); |
1080 | 120k | if (size == 0) { |
1081 | 3.44k | fxExecuteRegExp(the, splitter, argument); |
1082 | 3.44k | if (the->stack->kind == XS_NULL_KIND) { |
1083 | 2.76k | item = item->next = fxNewSlot(the); |
1084 | 2.76k | item->value.string = mxEmptyString.value.string; |
1085 | 2.76k | item->kind = mxEmptyString.kind; |
1086 | 2.76k | array->next->value.array.length++; |
1087 | 2.76k | } |
1088 | 3.44k | mxPop(); |
1089 | 3.44k | goto bail; |
1090 | 3.44k | } |
1091 | 117k | p = q = 0; |
1092 | 3.89M | while (q < size) { |
1093 | 3.78M | mxPushInteger(q); |
1094 | 3.78M | mxPushSlot(splitter); |
1095 | 3.78M | mxSetID(mxID(_lastIndex)); |
1096 | 3.78M | mxPop(); |
1097 | 3.78M | fxExecuteRegExp(the, splitter, argument); |
1098 | 3.78M | if (the->stack->kind == XS_NULL_KIND) { |
1099 | 3.69M | q = (txInteger)fxAdvanceStringIndex(the, argument->value.string, q, unicodeFlag); |
1100 | 3.69M | } |
1101 | 83.3k | else { |
1102 | 83.3k | mxPushSlot(splitter); |
1103 | 83.3k | mxGetID(mxID(_lastIndex)); |
1104 | 83.3k | e = fxToInteger(the, the->stack); |
1105 | 83.3k | mxPop(); |
1106 | 83.3k | if (e == p) { |
1107 | 26.6k | q = (txInteger)fxAdvanceStringIndex(the, argument->value.string, q, unicodeFlag); |
1108 | 26.6k | } |
1109 | 56.7k | else { |
1110 | 56.7k | txSlot* result = the->stack; |
1111 | 56.7k | item = item->next = fxNewSlot(the); |
1112 | 56.7k | fx_RegExp_prototype_split_aux(the, argument, p, q, item); |
1113 | 56.7k | array->next->value.array.length++; |
1114 | 56.7k | if (array->next->value.array.length == limit) |
1115 | 8 | goto bail; |
1116 | 56.7k | p = e; |
1117 | 56.7k | mxPushSlot(result); |
1118 | 56.7k | mxGetID(mxID(_length)); |
1119 | 56.7k | c = fxToInteger(the, the->stack); |
1120 | 56.7k | mxPop(); |
1121 | 56.7k | c--; if (c < 0) c = 0; |
1122 | 56.7k | i = 1; |
1123 | 121k | while (i <= c) { |
1124 | 65.0k | mxPushSlot(result); |
1125 | 65.0k | mxGetIndex(i); |
1126 | 65.0k | item = item->next = fxNewSlot(the); |
1127 | 65.0k | mxPullSlot(item); |
1128 | 65.0k | array->next->value.array.length++; |
1129 | 65.0k | if (array->next->value.array.length == limit) |
1130 | 1 | goto bail; |
1131 | 65.0k | i++; |
1132 | 65.0k | } |
1133 | 56.7k | q = p; |
1134 | 56.7k | } |
1135 | 83.3k | } |
1136 | 3.78M | mxPop(); |
1137 | 3.78M | mxCheckMetering(); |
1138 | 3.78M | } |
1139 | | //if (p < 0) |
1140 | | // p = 0; |
1141 | 117k | item = item->next = fxNewSlot(the); |
1142 | 117k | fx_RegExp_prototype_split_aux(the, argument, p, size, item); |
1143 | 117k | array->next->value.array.length++; |
1144 | 120k | bail: |
1145 | 120k | fxCacheArray(the, array); |
1146 | 120k | mxPop(); |
1147 | 120k | mxPop(); |
1148 | 120k | mxPop(); |
1149 | 120k | #endif |
1150 | 120k | } |
1151 | | |
1152 | | void fx_RegExp_prototype_split_aux(txMachine* the, txSlot* string, txIndex start, txIndex stop, txSlot* item) |
1153 | 2.38M | { |
1154 | 2.38M | #if mxRegExp |
1155 | 2.38M | txInteger offset = fxCacheUnicodeToUTF8Offset(the, string->value.string, start); |
1156 | 2.38M | txInteger length = fxUnicodeToUTF8Offset(string->value.string + offset, stop - start); |
1157 | 2.38M | if ((offset >= 0) && (length > 0)) { |
1158 | 2.37M | item->value.string = (txString)fxNewChunk(the, length + 1); |
1159 | 2.37M | c_memcpy(item->value.string, string->value.string + offset, length); |
1160 | 2.37M | item->value.string[length] = 0; |
1161 | 2.37M | item->kind = XS_STRING_KIND; |
1162 | 2.37M | } |
1163 | 14.7k | else { |
1164 | 14.7k | item->value.string = mxEmptyString.value.string; |
1165 | 14.7k | item->kind = mxEmptyString.kind; |
1166 | 14.7k | } |
1167 | 2.38M | #endif |
1168 | 2.38M | } |
1169 | | |
1170 | | void fx_RegExp_prototype_test(txMachine* the) |
1171 | 573 | { |
1172 | 573 | #if mxRegExp |
1173 | 573 | fxToInstance(the, mxThis); |
1174 | 573 | if (mxArgc > 0) |
1175 | 568 | mxPushSlot(mxArgv(0)); |
1176 | 5 | else |
1177 | 5 | mxPushUndefined(); |
1178 | 573 | fxToString(the, the->stack); |
1179 | 573 | fxExecuteRegExp(the, mxThis, the->stack); |
1180 | 573 | mxResult->value.boolean = (the->stack->kind != XS_NULL_KIND) ? 1 : 0; |
1181 | 573 | mxResult->kind = XS_BOOLEAN_KIND; |
1182 | 573 | mxPop(); |
1183 | 573 | #endif |
1184 | 573 | } |
1185 | | |
1186 | | void fx_RegExp_prototype_toString(txMachine* the) |
1187 | 182k | { |
1188 | 182k | #if mxRegExp |
1189 | 182k | fxToInstance(the, mxThis); |
1190 | 182k | fxStringX(the, mxResult, "/"); |
1191 | 182k | mxPushSlot(mxThis); |
1192 | 182k | mxGetID(mxID(_source)); |
1193 | 182k | fxToString(the, the->stack); |
1194 | 182k | fxConcatString(the, mxResult, the->stack); |
1195 | 182k | mxPop(); |
1196 | 182k | fxConcatStringC(the, mxResult, "/"); |
1197 | 182k | mxPushSlot(mxThis); |
1198 | 182k | mxGetID(mxID(_flags)); |
1199 | 182k | fxToString(the, the->stack); |
1200 | 182k | fxConcatString(the, mxResult, the->stack); |
1201 | 182k | mxPop(); |
1202 | 182k | #endif |
1203 | 182k | } |