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