Coverage Report

Created: 2026-01-17 06:19

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