Coverage Report

Created: 2025-11-24 06:44

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