LCOV - app.info - js/src/builtin/RegExp.cpp

LCOV - code coverage report
| Current view: | directory - js/src/builtin - RegExp.cpp (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 256 | 189 | 73.8 %
| Date: | 2012-06-02 | Functions: | 38 | 24 | 63.2 %

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

Generated by: LCOV version 1.7