Coverage Report

Created: 2026-08-13 06:36

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