PageRenderTime 88ms CodeModel.GetById 14ms app.highlight 68ms RepoModel.GetById 1ms app.codeStats 1ms

/js/src/builtin/RegExp.cpp

http://github.com/zpao/v8monkey
C++ | 655 lines | 434 code | 104 blank | 117 comment | 93 complexity | 6de26900acefed57bfcb952248245d5a MD5 | raw file
  1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2 * vim: set ts=8 sw=4 et tw=99 ft=cpp:
  3 *
  4 * ***** BEGIN LICENSE BLOCK *****
  5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6 *
  7 * The contents of this file are subject to the Mozilla Public License Version
  8 * 1.1 (the "License"); you may not use this file except in compliance with
  9 * the License. You may obtain a copy of the License at
 10 * http://www.mozilla.org/MPL/
 11 *
 12 * Software distributed under the License is distributed on an "AS IS" basis,
 13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 14 * for the specific language governing rights and limitations under the
 15 * License.
 16 *
 17 * The Original Code is Mozilla SpiderMonkey JavaScript code.
 18 *
 19 * The Initial Developer of the Original Code is
 20 * the Mozilla Foundation.
 21 * Portions created by the Initial Developer are Copyright (C) 2011
 22 * the Initial Developer. All Rights Reserved.
 23 *
 24 * Contributor(s):
 25 *  Chris Leary <cdleary@mozilla.com>
 26 *
 27 * Alternatively, the contents of this file may be used under the terms of
 28 * either the GNU General Public License Version 2 or later (the "GPL"), or
 29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 30 * in which case the provisions of the GPL or the LGPL are applicable instead
 31 * of those above. If you wish to allow use of your version of this file only
 32 * under the terms of either the GPL or the LGPL, and not to allow others to
 33 * use your version of this file under the terms of the MPL, indicate your
 34 * decision by deleting the provisions above and replace them with the notice
 35 * and other provisions required by the GPL or the LGPL. If you do not delete
 36 * the provisions above, a recipient may use your version of this file under
 37 * the terms of any one of the MPL, the GPL or the LGPL.
 38 *
 39 * ***** END LICENSE BLOCK ***** */
 40
 41#include "jscntxt.h"
 42
 43#include "builtin/RegExp.h"
 44
 45#include "vm/RegExpObject-inl.h"
 46#include "vm/RegExpStatics-inl.h"
 47
 48using namespace js;
 49using namespace js::types;
 50
 51class RegExpMatchBuilder
 52{
 53    JSContext   * const cx;
 54    JSObject    * const array;
 55
 56    bool setProperty(JSAtom *name, Value v) {
 57        return !!js_DefineProperty(cx, array, ATOM_TO_JSID(name), &v,
 58                                   JS_PropertyStub, JS_StrictPropertyStub, JSPROP_ENUMERATE);
 59    }
 60
 61  public:
 62    RegExpMatchBuilder(JSContext *cx, JSObject *array) : cx(cx), array(array) {}
 63
 64    bool append(uint32_t index, Value v) {
 65        JS_ASSERT(!array->getOps()->getElement);
 66        return !!js_DefineElement(cx, array, index, &v, JS_PropertyStub, JS_StrictPropertyStub,
 67                                  JSPROP_ENUMERATE);
 68    }
 69
 70    bool setIndex(int index) {
 71        return setProperty(cx->runtime->atomState.indexAtom, Int32Value(index));
 72    }
 73
 74    bool setInput(JSString *str) {
 75        JS_ASSERT(str);
 76        return setProperty(cx->runtime->atomState.inputAtom, StringValue(str));
 77    }
 78};
 79
 80static bool
 81CreateRegExpMatchResult(JSContext *cx, JSString *input, const jschar *chars, size_t length,
 82                        MatchPairs *matchPairs, Value *rval)
 83{
 84    /*
 85     * Create the (slow) result array for a match.
 86     *
 87     * Array contents:
 88     *  0:              matched string
 89     *  1..pairCount-1: paren matches
 90     *  input:          input string
 91     *  index:          start index for the match
 92     */
 93    JSObject *array = NewSlowEmptyArray(cx);
 94    if (!array)
 95        return false;
 96
 97    if (!input) {
 98        input = js_NewStringCopyN(cx, chars, length);
 99        if (!input)
100            return false;
101    }
102
103    RegExpMatchBuilder builder(cx, array);
104
105    for (size_t i = 0; i < matchPairs->pairCount(); ++i) {
106        MatchPair pair = matchPairs->pair(i);
107
108        JSString *captured;
109        if (pair.isUndefined()) {
110            JS_ASSERT(i != 0); /* Since we had a match, first pair must be present. */
111            if (!builder.append(i, UndefinedValue()))
112                return false;
113        } else {
114            captured = js_NewDependentString(cx, input, pair.start, pair.length());
115            if (!captured || !builder.append(i, StringValue(captured)))
116                return false;
117        }
118    }
119
120    if (!builder.setIndex(matchPairs->pair(0).start) || !builder.setInput(input))
121        return false;
122
123    *rval = ObjectValue(*array);
124    return true;
125}
126
127template <class T>
128bool
129ExecuteRegExpImpl(JSContext *cx, RegExpStatics *res, T &re, JSLinearString *input,
130                  const jschar *chars, size_t length,
131                  size_t *lastIndex, RegExpExecType type, Value *rval)
132{
133    LifoAllocScope allocScope(&cx->tempLifoAlloc());
134    MatchPairs *matchPairs = NULL;
135    RegExpRunStatus status = re.execute(cx, chars, length, lastIndex, &matchPairs);
136
137    switch (status) {
138      case RegExpRunStatus_Error:
139        return false;
140      case RegExpRunStatus_Success_NotFound:
141        *rval = NullValue();
142        return true;
143      default:
144        JS_ASSERT(status == RegExpRunStatus_Success);
145        JS_ASSERT(matchPairs);
146    }
147
148    if (res)
149        res->updateFromMatchPairs(cx, input, matchPairs);
150
151    *lastIndex = matchPairs->pair(0).limit;
152
153    if (type == RegExpTest) {
154        *rval = BooleanValue(true);
155        return true;
156    }
157
158    return CreateRegExpMatchResult(cx, input, chars, length, matchPairs, rval);
159}
160
161bool
162js::ExecuteRegExp(JSContext *cx, RegExpStatics *res, RegExpShared &shared, JSLinearString *input,
163                  const jschar *chars, size_t length,
164                  size_t *lastIndex, RegExpExecType type, Value *rval)
165{
166    return ExecuteRegExpImpl(cx, res, shared, input, chars, length, lastIndex, type, rval);
167}
168
169bool
170js::ExecuteRegExp(JSContext *cx, RegExpStatics *res, RegExpObject &reobj, JSLinearString *input,
171                  const jschar *chars, size_t length,
172                  size_t *lastIndex, RegExpExecType type, Value *rval)
173{
174    return ExecuteRegExpImpl(cx, res, reobj, input, chars, length, lastIndex, type, rval);
175}
176
177/* Note: returns the original if no escaping need be performed. */
178static JSAtom *
179EscapeNakedForwardSlashes(JSContext *cx, JSAtom *unescaped)
180{
181    size_t oldLen = unescaped->length();
182    const jschar *oldChars = unescaped->chars();
183
184    JS::Anchor<JSString *> anchor(unescaped);
185
186    /* We may never need to use |sb|. Start using it lazily. */
187    StringBuffer sb(cx);
188
189    for (const jschar *it = oldChars; it < oldChars + oldLen; ++it) {
190        if (*it == '/' && (it == oldChars || it[-1] != '\\')) {
191            /* There's a forward slash that needs escaping. */
192            if (sb.empty()) {
193                /* This is the first one we've seen, copy everything up to this point. */
194                if (!sb.reserve(oldLen + 1))
195                    return NULL;
196                sb.infallibleAppend(oldChars, size_t(it - oldChars));
197            }
198            if (!sb.append('\\'))
199                return NULL;
200        }
201
202        if (!sb.empty() && !sb.append(*it))
203            return NULL;
204    }
205
206    return sb.empty() ? unescaped : sb.finishAtom();
207}
208
209/*
210 * Compile a new |RegExpShared| for the |RegExpObject|.
211 *
212 * Per ECMAv5 15.10.4.1, we act on combinations of (pattern, flags) as
213 * arguments:
214 *
215 *  RegExp, undefined => flags := pattern.flags
216 *  RegExp, _ => throw TypeError
217 *  _ => pattern := ToString(pattern) if defined(pattern) else ''
218 *       flags := ToString(flags) if defined(flags) else ''
219 */
220static bool
221CompileRegExpObject(JSContext *cx, RegExpObjectBuilder &builder, CallArgs args)
222{
223    if (args.length() == 0) {
224        RegExpStatics *res = cx->regExpStatics();
225        RegExpObject *reobj = builder.build(cx->runtime->emptyString, res->getFlags());
226        if (!reobj)
227            return false;
228        args.rval() = ObjectValue(*reobj);
229        return true;
230    }
231
232    Value sourceValue = args[0];
233
234    /*
235     * If we get passed in an object whose internal [[Class]] property is
236     * "RegExp", return a new object with the same source/flags.
237     */
238    if (IsObjectWithClass(sourceValue, ESClass_RegExp, cx)) {
239        /*
240         * Beware, sourceObj may be a (transparent) proxy to a RegExp, so only
241         * use generic (proxyable) operations on sourceObj that do not assume
242         * sourceObj.isRegExp().
243         */
244        JSObject &sourceObj = sourceValue.toObject();
245
246        if (args.length() >= 2 && !args[1].isUndefined()) {
247            JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NEWREGEXP_FLAGGED);
248            return false;
249        }
250
251        /*
252         * Only extract the 'flags' out of sourceObj; do not reuse the
253         * RegExpShared since it may be from a different compartment.
254         */
255        RegExpFlag flags;
256        {
257            RegExpShared *shared = RegExpToShared(cx, sourceObj);
258            if (!shared)
259                return false;
260
261            flags = shared->getFlags();
262        }
263
264        /*
265         * 'toSource' is a permanent read-only property, so this is equivalent
266         * to executing RegExpObject::getSource on the unwrapped object.
267         */
268        Value v;
269        if (!sourceObj.getProperty(cx, cx->runtime->atomState.sourceAtom, &v))
270            return false;
271
272        RegExpObject *reobj = builder.build(&v.toString()->asAtom(), flags);
273        if (!reobj)
274            return false;
275
276        args.rval() = ObjectValue(*reobj);
277        return true;
278    }
279
280    JSAtom *source;
281    if (sourceValue.isUndefined()) {
282        source = cx->runtime->emptyString;
283    } else {
284        /* Coerce to string and compile. */
285        JSString *str = ToString(cx, sourceValue);
286        if (!str)
287            return false;
288
289        source = js_AtomizeString(cx, str);
290        if (!source)
291            return false;
292    }
293
294    RegExpFlag flags = RegExpFlag(0);
295    if (args.length() > 1 && !args[1].isUndefined()) {
296        JSString *flagStr = ToString(cx, args[1]);
297        if (!flagStr)
298            return false;
299        args[1].setString(flagStr);
300        if (!ParseRegExpFlags(cx, flagStr, &flags))
301            return false;
302    }
303
304    JSAtom *escapedSourceStr = EscapeNakedForwardSlashes(cx, source);
305    if (!escapedSourceStr)
306        return false;
307
308    if (!js::detail::RegExpCode::checkSyntax(cx, NULL, escapedSourceStr))
309        return false;
310
311    RegExpStatics *res = cx->regExpStatics();
312    RegExpObject *reobj = builder.build(escapedSourceStr, RegExpFlag(flags | res->getFlags()));
313    if (!reobj)
314        return NULL;
315
316    args.rval() = ObjectValue(*reobj);
317    return true;
318}
319
320static JSBool
321regexp_compile(JSContext *cx, uintN argc, Value *vp)
322{
323    CallArgs args = CallArgsFromVp(argc, vp);
324
325    bool ok;
326    JSObject *obj = NonGenericMethodGuard(cx, args, regexp_compile, &RegExpClass, &ok);
327    if (!obj)
328        return ok;
329
330    RegExpObjectBuilder builder(cx, &obj->asRegExp());
331    return CompileRegExpObject(cx, builder, args);
332}
333
334static JSBool
335regexp_construct(JSContext *cx, uintN argc, Value *vp)
336{
337    CallArgs args = CallArgsFromVp(argc, vp);
338
339    if (!IsConstructing(args)) {
340        /*
341         * If first arg is regexp and no flags are given, just return the arg.
342         * Otherwise, delegate to the standard constructor.
343         * See ECMAv5 15.10.3.1.
344         */
345        if (args.length() >= 1 && IsObjectWithClass(args[0], ESClass_RegExp, cx) &&
346            (args.length() == 1 || args[1].isUndefined()))
347        {
348            args.rval() = args[0];
349            return true;
350        }
351    }
352
353    RegExpObjectBuilder builder(cx);
354    return CompileRegExpObject(cx, builder, args);
355}
356
357static JSBool
358regexp_toString(JSContext *cx, uintN argc, Value *vp)
359{
360    CallArgs args = CallArgsFromVp(argc, vp);
361
362    bool ok;
363    JSObject *obj = NonGenericMethodGuard(cx, args, regexp_toString, &RegExpClass, &ok);
364    if (!obj)
365        return ok;
366
367    JSString *str = obj->asRegExp().toString(cx);
368    if (!str)
369        return false;
370
371    *vp = StringValue(str);
372    return true;
373}
374
375static JSFunctionSpec regexp_methods[] = {
376#if JS_HAS_TOSOURCE
377    JS_FN(js_toSource_str,  regexp_toString,    0,0),
378#endif
379    JS_FN(js_toString_str,  regexp_toString,    0,0),
380    JS_FN("compile",        regexp_compile,     2,0),
381    JS_FN("exec",           regexp_exec,        1,0),
382    JS_FN("test",           regexp_test,        1,0),
383    JS_FS_END
384};
385
386/*
387 * RegExp static properties.
388 *
389 * RegExp class static properties and their Perl counterparts:
390 *
391 *  RegExp.input                $_
392 *  RegExp.multiline            $*
393 *  RegExp.lastMatch            $&
394 *  RegExp.lastParen            $+
395 *  RegExp.leftContext          $`
396 *  RegExp.rightContext         $'
397 */
398
399#define DEFINE_STATIC_GETTER(name, code)                                        \
400    static JSBool                                                               \
401    name(JSContext *cx, JSObject *obj, jsid id, jsval *vp)                      \
402    {                                                                           \
403        RegExpStatics *res = cx->regExpStatics();                               \
404        code;                                                                   \
405    }
406
407DEFINE_STATIC_GETTER(static_input_getter,        return res->createPendingInput(cx, vp))
408DEFINE_STATIC_GETTER(static_multiline_getter,    *vp = BOOLEAN_TO_JSVAL(res->multiline());
409                                                 return true)
410DEFINE_STATIC_GETTER(static_lastMatch_getter,    return res->createLastMatch(cx, vp))
411DEFINE_STATIC_GETTER(static_lastParen_getter,    return res->createLastParen(cx, vp))
412DEFINE_STATIC_GETTER(static_leftContext_getter,  return res->createLeftContext(cx, vp))
413DEFINE_STATIC_GETTER(static_rightContext_getter, return res->createRightContext(cx, vp))
414
415DEFINE_STATIC_GETTER(static_paren1_getter,       return res->createParen(cx, 1, vp))
416DEFINE_STATIC_GETTER(static_paren2_getter,       return res->createParen(cx, 2, vp))
417DEFINE_STATIC_GETTER(static_paren3_getter,       return res->createParen(cx, 3, vp))
418DEFINE_STATIC_GETTER(static_paren4_getter,       return res->createParen(cx, 4, vp))
419DEFINE_STATIC_GETTER(static_paren5_getter,       return res->createParen(cx, 5, vp))
420DEFINE_STATIC_GETTER(static_paren6_getter,       return res->createParen(cx, 6, vp))
421DEFINE_STATIC_GETTER(static_paren7_getter,       return res->createParen(cx, 7, vp))
422DEFINE_STATIC_GETTER(static_paren8_getter,       return res->createParen(cx, 8, vp))
423DEFINE_STATIC_GETTER(static_paren9_getter,       return res->createParen(cx, 9, vp))
424
425#define DEFINE_STATIC_SETTER(name, code)                                        \
426    static JSBool                                                               \
427    name(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp)       \
428    {                                                                           \
429        RegExpStatics *res = cx->regExpStatics();                               \
430        code;                                                                   \
431        return true;                                                            \
432    }
433
434DEFINE_STATIC_SETTER(static_input_setter,
435                     if (!JSVAL_IS_STRING(*vp) && !JS_ConvertValue(cx, *vp, JSTYPE_STRING, vp))
436                         return false;
437                     res->setPendingInput(JSVAL_TO_STRING(*vp)))
438DEFINE_STATIC_SETTER(static_multiline_setter,
439                     if (!JSVAL_IS_BOOLEAN(*vp) && !JS_ConvertValue(cx, *vp, JSTYPE_BOOLEAN, vp))
440                         return false;
441                     res->setMultiline(cx, !!JSVAL_TO_BOOLEAN(*vp)))
442
443const uint8_t REGEXP_STATIC_PROP_ATTRS    = JSPROP_PERMANENT | JSPROP_SHARED | JSPROP_ENUMERATE;
444const uint8_t RO_REGEXP_STATIC_PROP_ATTRS = REGEXP_STATIC_PROP_ATTRS | JSPROP_READONLY;
445
446const uint8_t HIDDEN_PROP_ATTRS = JSPROP_PERMANENT | JSPROP_SHARED;
447const uint8_t RO_HIDDEN_PROP_ATTRS = HIDDEN_PROP_ATTRS | JSPROP_READONLY;
448
449static JSPropertySpec regexp_static_props[] = {
450    {"input",        0, REGEXP_STATIC_PROP_ATTRS,    static_input_getter, static_input_setter},
451    {"multiline",    0, REGEXP_STATIC_PROP_ATTRS,    static_multiline_getter,
452                                                     static_multiline_setter},
453    {"lastMatch",    0, RO_REGEXP_STATIC_PROP_ATTRS, static_lastMatch_getter,    NULL},
454    {"lastParen",    0, RO_REGEXP_STATIC_PROP_ATTRS, static_lastParen_getter,    NULL},
455    {"leftContext",  0, RO_REGEXP_STATIC_PROP_ATTRS, static_leftContext_getter,  NULL},
456    {"rightContext", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_rightContext_getter, NULL},
457    {"$1",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren1_getter,       NULL},
458    {"$2",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren2_getter,       NULL},
459    {"$3",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren3_getter,       NULL},
460    {"$4",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren4_getter,       NULL},
461    {"$5",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren5_getter,       NULL},
462    {"$6",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren6_getter,       NULL},
463    {"$7",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren7_getter,       NULL},
464    {"$8",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren8_getter,       NULL},
465    {"$9",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren9_getter,       NULL},
466
467    {"$_",           0, HIDDEN_PROP_ATTRS,    static_input_getter, static_input_setter},
468    {"$*",           0, HIDDEN_PROP_ATTRS,    static_multiline_getter, static_multiline_setter},
469    {"$&",           0, RO_HIDDEN_PROP_ATTRS, static_lastMatch_getter, NULL},
470    {"$+",           0, RO_HIDDEN_PROP_ATTRS, static_lastParen_getter, NULL},
471    {"$`",           0, RO_HIDDEN_PROP_ATTRS, static_leftContext_getter, NULL},
472    {"$'",           0, RO_HIDDEN_PROP_ATTRS, static_rightContext_getter, NULL},
473    {0,0,0,0,0}
474};
475
476JSObject *
477js_InitRegExpClass(JSContext *cx, JSObject *obj)
478{
479    JS_ASSERT(obj->isNative());
480
481    GlobalObject *global = &obj->asGlobal();
482
483    JSObject *proto = global->createBlankPrototype(cx, &RegExpClass);
484    if (!proto)
485        return NULL;
486    proto->setPrivate(NULL);
487
488    RegExpObject *reproto = &proto->asRegExp();
489    RegExpObjectBuilder builder(cx, reproto);
490    if (!builder.build(cx->runtime->emptyString, RegExpFlag(0)))
491        return NULL;
492
493    if (!DefinePropertiesAndBrand(cx, proto, NULL, regexp_methods))
494        return NULL;
495
496    JSFunction *ctor = global->createConstructor(cx, regexp_construct, &RegExpClass,
497                                                 CLASS_ATOM(cx, RegExp), 2);
498    if (!ctor)
499        return NULL;
500
501    if (!LinkConstructorAndPrototype(cx, ctor, proto))
502        return NULL;
503
504    /* Add static properties to the RegExp constructor. */
505    if (!JS_DefineProperties(cx, ctor, regexp_static_props))
506        return NULL;
507
508    /* Capture normal data properties pregenerated for RegExp objects. */
509    TypeObject *type = proto->getNewType(cx);
510    if (!type)
511        return NULL;
512    AddTypeProperty(cx, type, "source", Type::StringType());
513    AddTypeProperty(cx, type, "global", Type::BooleanType());
514    AddTypeProperty(cx, type, "ignoreCase", Type::BooleanType());
515    AddTypeProperty(cx, type, "multiline", Type::BooleanType());
516    AddTypeProperty(cx, type, "sticky", Type::BooleanType());
517    AddTypeProperty(cx, type, "lastIndex", Type::Int32Type());
518
519    if (!DefineConstructorAndPrototype(cx, global, JSProto_RegExp, ctor, proto))
520        return NULL;
521
522    return proto;
523}
524
525
526static const jschar GreedyStarChars[] = {'.', '*'};
527
528static inline bool
529StartsWithGreedyStar(JSAtom *source)
530{
531    return false;
532
533#if 0
534    if (source->length() < 3)
535        return false;
536
537    const jschar *chars = source->chars();
538    return chars[0] == GreedyStarChars[0] &&
539           chars[1] == GreedyStarChars[1] &&
540           chars[2] != '?';
541#endif
542}
543
544static inline RegExpShared *
545GetSharedForGreedyStar(JSContext *cx, JSAtom *source, RegExpFlag flags)
546{
547    if (RegExpShared *hit = cx->compartment->regExps.lookupHack(cx, source, flags))
548        return hit;
549
550    JSAtom *hackedSource = js_AtomizeChars(cx, source->chars() + ArrayLength(GreedyStarChars),
551                                           source->length() - ArrayLength(GreedyStarChars));
552    if (!hackedSource)
553        return NULL;
554
555    return cx->compartment->regExps.getHack(cx, source, hackedSource, flags);
556}
557
558/*
559 * ES5 15.10.6.2 (and 15.10.6.3, which calls 15.10.6.2).
560 *
561 * RegExp.prototype.test doesn't need to create a results array, and we use
562 * |execType| to perform this optimization.
563 */
564static bool
565ExecuteRegExp(JSContext *cx, Native native, uintN argc, Value *vp)
566{
567    CallArgs args = CallArgsFromVp(argc, vp);
568
569    /* Step 1. */
570    bool ok;
571    JSObject *obj = NonGenericMethodGuard(cx, args, native, &RegExpClass, &ok);
572    if (!obj)
573        return ok;
574
575    RegExpObject &reobj = obj->asRegExp();
576
577    RegExpShared *shared;
578    if (StartsWithGreedyStar(reobj.getSource()))
579        shared = GetSharedForGreedyStar(cx, reobj.getSource(), reobj.getFlags());
580    else
581        shared = reobj.getShared(cx);
582
583    if (!shared)
584        return false;
585
586    RegExpShared::Guard re(*shared);
587    RegExpStatics *res = cx->regExpStatics();
588
589    /* Step 2. */
590    JSString *input = ToString(cx, (args.length() > 0) ? args[0] : UndefinedValue());
591    if (!input)
592        return false;
593
594    /* Step 3. */
595    JSLinearString *linearInput = input->ensureLinear(cx);
596    if (!linearInput)
597        return false;
598    const jschar *chars = linearInput->chars();
599    size_t length = input->length();
600
601    /* Step 4. */
602    const Value &lastIndex = reobj.getLastIndex();
603
604    /* Step 5. */
605    jsdouble i;
606    if (!ToInteger(cx, lastIndex, &i))
607        return false;
608
609    /* Steps 6-7 (with sticky extension). */
610    if (!re->global() && !re->sticky())
611        i = 0;
612
613    /* Step 9a. */
614    if (i < 0 || i > length) {
615        reobj.zeroLastIndex();
616        args.rval() = NullValue();
617        return true;
618    }
619
620    /* Steps 8-21. */
621    RegExpExecType execType = (native == regexp_test) ? RegExpTest : RegExpExec;
622    size_t lastIndexInt(i);
623    if (!ExecuteRegExp(cx, res, *re, linearInput, chars, length, &lastIndexInt, execType,
624                       &args.rval())) {
625        return false;
626    }
627
628    /* Step 11 (with sticky extension). */
629    if (re->global() || (!args.rval().isNull() && re->sticky())) {
630        if (args.rval().isNull())
631            reobj.zeroLastIndex();
632        else
633            reobj.setLastIndex(lastIndexInt);
634    }
635
636    return true;
637}
638
639/* ES5 15.10.6.2. */
640JSBool
641js::regexp_exec(JSContext *cx, uintN argc, Value *vp)
642{
643    return ExecuteRegExp(cx, regexp_exec, argc, vp);
644}
645
646/* ES5 15.10.6.3. */
647JSBool
648js::regexp_test(JSContext *cx, uintN argc, Value *vp)
649{
650    if (!ExecuteRegExp(cx, regexp_test, argc, vp))
651        return false;
652    if (!vp->isTrue())
653        vp->setBoolean(false);
654    return true;
655}