Coverage Report

Created: 2026-01-09 07:26

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