Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsregexp.cpp
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set sw=4 ts=8 et tw=99:
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 Communicator client code, released
18  * March 31, 1998.
19  *
20  * The Initial Developer of the Original Code is
21  * Netscape Communications Corporation.
22  * Portions created by the Initial Developer are Copyright (C) 1998
23  * the Initial Developer. All Rights Reserved.
24  *
25  * Contributor(s):
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either of the GNU General Public License Version 2 or later (the "GPL"),
29  * or 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 /*
42  * JS regular expressions, after Perl.
43  */
44 #include <stdlib.h>
45 #include <string.h>
46 #include "jstypes.h"
47 #include "jsstdint.h"
48 #include "jsutil.h"
49 #include "jsapi.h"
50 #include "jscntxt.h"
51 #include "jsgc.h"
52 #include "jsnum.h"
53 #include "jsobj.h"
54 #include "jsregexp.h"
55 #include "jsstr.h"
56 #include "jsvector.h"
57
58 #include "jsobjinlines.h"
59 #include "jsregexpinlines.h"
60
61 #include "yarr/RegexParser.h"
62
63 #ifdef JS_TRACER
64 #include "jstracer.h"
65 using namespace avmplus;
66 using namespace nanojit;
67 #endif
68
69 using namespace js;
70 using namespace js::gc;
71
72 /*
73  * RegExpStatics allocates memory -- in order to keep the statics stored
74  * per-global and not leak, we create a js::Class to wrap the C++ instance and
75  * provide an appropriate finalizer. We store an instance of that js::Class in
76  * a global reserved slot.
77  */
78
79 static void
80 resc_finalize(JSContext *cx, JSObject *obj)
81 {
82     RegExpStatics *res = static_cast<RegExpStatics *>(obj->getPrivate());
83     cx->destroy<RegExpStatics>(res);
84 }
85
86 static void
87 resc_trace(JSTracer *trc, JSObject *obj)
88 {
89     void *pdata = obj->getPrivate();
90     JS_ASSERT(pdata);
91     RegExpStatics *res = static_cast<RegExpStatics *>(pdata);
92     res->mark(trc);
93 }
94
95 Class js::regexp_statics_class = {
96     "RegExpStatics", 
97     JSCLASS_HAS_PRIVATE | JSCLASS_MARK_IS_TRACE,
98     PropertyStub,         /* addProperty */
99     PropertyStub,         /* delProperty */
100     PropertyStub,         /* getProperty */
101     StrictPropertyStub,   /* setProperty */
102     EnumerateStub,
103     ResolveStub,
104     ConvertStub,
105     resc_finalize,
106     NULL,                 /* reserved0   */
107     NULL,                 /* checkAccess */
108     NULL,                 /* call        */
109     NULL,                 /* construct   */
110     NULL,                 /* xdrObject   */
111     NULL,                 /* hasInstance */
112     JS_CLASS_TRACE(resc_trace)
113 };
114
115 /*
116  * Replace the regexp internals of |obj| with |newRegExp|.
117  * Decref the replaced regexp internals.
118  * Note that the refcount of |newRegExp| is unchanged.
119  */
120 static void
121 SwapObjectRegExp(JSContext *cx, JSObject *obj, AlreadyIncRefed<RegExp> newRegExp)
122 {
123     RegExp *oldRegExp = RegExp::extractFrom(obj);
124 #ifdef DEBUG
125     if (oldRegExp)
126         assertSameCompartment(cx, obj, oldRegExp->compartment);
127     assertSameCompartment(cx, obj, newRegExp->compartment);
128 #endif
129
130     obj->setPrivate(newRegExp.get());
131     obj->zeroRegExpLastIndex();
132     if (oldRegExp)
133         oldRegExp->decref(cx);
134 }
135
136 JSObject * JS_FASTCALL
137 js_CloneRegExpObject(JSContext *cx, JSObject *obj, JSObject *proto)
138 {
139     JS_ASSERT(obj->getClass() == &js_RegExpClass);
140     JS_ASSERT(proto);
141     JS_ASSERT(proto->getClass() == &js_RegExpClass);
142
143     JSObject *clone = NewNativeClassInstance(cx, &js_RegExpClass, proto, proto->getParent());
144     if (!clone)
145         return NULL;
146
147     /* 
148      * This clone functionality does not duplicate the JITted code blob, which is necessary for
149      * cross-compartment cloning functionality.
150      */
151     assertSameCompartment(cx, obj, clone);
152
153     RegExpStatics *res = cx->regExpStatics();
154     RegExp *re = RegExp::extractFrom(obj);
155     {
156         uint32 origFlags = re->getFlags();
157         uint32 staticsFlags = res->getFlags();
158         if ((origFlags & staticsFlags) != staticsFlags) {
159             /*
160              * This regex is lacking flags from the statics, so we must recompile with the new
161              * flags instead of increffing.
162              */
163             AlreadyIncRefed<RegExp> clone = RegExp::create(cx, re->getSource(), origFlags | staticsFlags);
164             if (!clone)
165                 return NULL;
166             re = clone.get();
167         } else {
168             re->incref(cx);
169         }
170     }
171     JS_ASSERT(re);
172     clone->setPrivate(re);
173     clone->zeroRegExpLastIndex();
174     return clone;
175 }
176
177 #ifdef JS_TRACER
178 JS_DEFINE_CALLINFO_3(extern, OBJECT, js_CloneRegExpObject, CONTEXT, OBJECT, OBJECT, 0,
179                      ACCSET_STORE_ANY)
180 #endif
181
182 JSBool
183 js_ObjectIsRegExp(JSObject *obj)
184 {
185     return obj->isRegExp();
186 }
187
188 /*
189  * js::RegExp
190  */
191
192 void
193 RegExp::handleYarrError(JSContext *cx, int error)
194 {
195     switch (error) {
196       case JSC::Yarr::NoError:
197         JS_NOT_REACHED("Precondition violation: an error must have occurred.");
198         return;
199 #define COMPILE_EMSG(__code, __msg) \
200       case JSC::Yarr::__code: \
201         JS_ReportErrorFlagsAndNumberUC(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL, __msg); \
202         return
203       COMPILE_EMSG(PatternTooLarge, JSMSG_REGEXP_TOO_COMPLEX);
204       COMPILE_EMSG(QuantifierOutOfOrder, JSMSG_BAD_QUANTIFIER);
205       COMPILE_EMSG(QuantifierWithoutAtom, JSMSG_BAD_QUANTIFIER);
206       COMPILE_EMSG(MissingParentheses, JSMSG_MISSING_PAREN);
207       COMPILE_EMSG(ParenthesesUnmatched, JSMSG_UNMATCHED_RIGHT_PAREN);
208       COMPILE_EMSG(ParenthesesTypeInvalid, JSMSG_BAD_QUANTIFIER); /* "(?" with bad next char */
209       COMPILE_EMSG(CharacterClassUnmatched, JSMSG_BAD_CLASS_RANGE);
210       COMPILE_EMSG(CharacterClassOutOfOrder, JSMSG_BAD_CLASS_RANGE);
211       COMPILE_EMSG(CharacterClassRangeSingleChar, JSMSG_BAD_CLASS_RANGE);
212       COMPILE_EMSG(EscapeUnterminated, JSMSG_TRAILING_SLASH);
213       COMPILE_EMSG(QuantifierTooLarge, JSMSG_BAD_QUANTIFIER);
214       COMPILE_EMSG(HitRecursionLimit, JSMSG_REGEXP_TOO_COMPLEX);
215 #undef COMPILE_EMSG
216       default:
217         JS_NOT_REACHED("Precondition violation: unknown Yarr error code.");
218     }
219 }
220
221 void
222 RegExp::handlePCREError(JSContext *cx, int error)
223 {
224 #define REPORT(msg_) \
225     JS_ReportErrorFlagsAndNumberUC(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL, msg_); \
226     return
227     switch (error) {
228       case -2: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
229       case 0: JS_NOT_REACHED("Precondition violation: an error must have occurred.");
230       case 1: REPORT(JSMSG_TRAILING_SLASH);
231       case 2: REPORT(JSMSG_TRAILING_SLASH);
232       case 3: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
233       case 4: REPORT(JSMSG_BAD_QUANTIFIER);
234       case 5: REPORT(JSMSG_BAD_QUANTIFIER);
235       case 6: REPORT(JSMSG_BAD_CLASS_RANGE);
236       case 7: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
237       case 8: REPORT(JSMSG_BAD_CLASS_RANGE);
238       case 9: REPORT(JSMSG_BAD_QUANTIFIER);
239       case 10: REPORT(JSMSG_UNMATCHED_RIGHT_PAREN);
240       case 11: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
241       case 12: REPORT(JSMSG_UNMATCHED_RIGHT_PAREN);
242       case 13: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
243       case 14: REPORT(JSMSG_MISSING_PAREN);
244       case 15: REPORT(JSMSG_BAD_BACKREF);
245       case 16: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
246       case 17: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
247       default:
248         JS_NOT_REACHED("Precondition violation: unknown PCRE error code.");
249     }
250 #undef REPORT
251 }
252
253 bool
254 RegExp::parseFlags(JSContext *cx, JSString *flagStr, uintN *flagsOut)
255 {
256     size_t n = flagStr->length();
257     const jschar *s = flagStr->getChars(cx);
258     if (!s)
259         return false;
260
261     *flagsOut = 0;
262     for (size_t i = 0; i < n; i++) {
263 #define HANDLE_FLAG(name_)                                                    \
264         JS_BEGIN_MACRO                                                        \
265             if (*flagsOut & (name_))                                          \
266                 goto bad_flag;                                                \
267             *flagsOut |= (name_);                                             \
268         JS_END_MACRO
269         switch (s[i]) {
270           case 'i': HANDLE_FLAG(JSREG_FOLD); break;
271           case 'g': HANDLE_FLAG(JSREG_GLOB); break;
272           case 'm': HANDLE_FLAG(JSREG_MULTILINE); break;
273           case 'y': HANDLE_FLAG(JSREG_STICKY); break;
274           default:
275           bad_flag:
276           {
277             char charBuf[2];
278             charBuf[0] = char(s[i]);
279             charBuf[1] = '\0';
280             JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL,
281                                          JSMSG_BAD_REGEXP_FLAG, charBuf);
282             return false;
283           }
284         }
285 #undef HANDLE_FLAG
286     }
287     return true;
288 }
289
290 AlreadyIncRefed<RegExp>
291 RegExp::createFlagged(JSContext *cx, JSString *str, JSString *opt)
292 {
293     if (!opt)
294         return create(cx, str, 0);
295     uintN flags = 0;
296     if (!parseFlags(cx, opt, &flags))
297         return AlreadyIncRefed<RegExp>(NULL);
298     return create(cx, str, flags);
299 }
300
301 /*
302  * RegExp instance properties.
303  */
304 #define DEFINE_GETTER(name, code)                                              \
305     static JSBool                                                              \
306     name(JSContext *cx, JSObject *obj, jsid id, Value *vp)                     \
307     {                                                                          \
308         while (obj->getClass() != &js_RegExpClass) {                           \
309             obj = obj->getProto();                                             \
310             if (!obj)                                                          \
311                 return true;                                                   \
312         }                                                                      \
313         RegExp *re = RegExp::extractFrom(obj);                                 \
314         code;                                                                  \
315         return true;                                                           \
316     }
317
318 /* lastIndex is stored in the object, re = re silences the compiler warning. */
319 DEFINE_GETTER(lastIndex_getter,  re = re; *vp = obj->getRegExpLastIndex())
320 DEFINE_GETTER(source_getter,     *vp = StringValue(re->getSource()))
321 DEFINE_GETTER(global_getter,     *vp = BooleanValue(re->global()))
322 DEFINE_GETTER(ignoreCase_getter, *vp = BooleanValue(re->ignoreCase()))
323 DEFINE_GETTER(multiline_getter,  *vp = BooleanValue(re->multiline()))
324 DEFINE_GETTER(sticky_getter,     *vp = BooleanValue(re->sticky()))
325
326 static JSBool
327 lastIndex_setter(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp)
328 {
329     while (obj->getClass() != &js_RegExpClass) {
330         obj = obj->getProto();
331         if (!obj)
332             return true;
333     }
334     obj->setRegExpLastIndex(*vp);
335     return true;
336 }
337
338 static const struct LazyProp {
339     const char *name;
340     uint16 atomOffset;
341     PropertyOp getter;
342 } lazyRegExpProps[] = {
343     { js_source_str,     ATOM_OFFSET(source),     source_getter },
344     { js_global_str,     ATOM_OFFSET(global),     global_getter },
345     { js_ignoreCase_str, ATOM_OFFSET(ignoreCase), ignoreCase_getter },
346     { js_multiline_str,  ATOM_OFFSET(multiline),  multiline_getter },
347     { js_sticky_str,     ATOM_OFFSET(sticky),     sticky_getter }
348 };
349
350 static JSBool
351 regexp_resolve(JSContext *cx, JSObject *obj, jsid id, uint32 flags, JSObject **objp)
352 {
353     JS_ASSERT(obj->isRegExp());
354
355     if (!JSID_IS_ATOM(id))
356         return JS_TRUE;
357
358     if (id == ATOM_TO_JSID(cx->runtime->atomState.lastIndexAtom)) {
359         if (!js_DefineNativeProperty(cx, obj, id, UndefinedValue(),
360                                      lastIndex_getter, lastIndex_setter,
361                                      JSPROP_PERMANENT | JSPROP_SHARED,
362                                      0, 0, NULL)) {
363             return false;
364         }
365         *objp = obj;
366         return true;
367     }
368
369     for (size_t i = 0; i < JS_ARRAY_LENGTH(lazyRegExpProps); i++) {
370         const LazyProp &lazy = lazyRegExpProps[i];
371         JSAtom *atom = OFFSET_TO_ATOM(cx->runtime, lazy.atomOffset);
372         if (id == ATOM_TO_JSID(atom)) {
373             if (!js_DefineNativeProperty(cx, obj, id, UndefinedValue(),
374                                          lazy.getter, NULL,
375                                          JSPROP_PERMANENT | JSPROP_SHARED | JSPROP_READONLY,
376                                          0, 0, NULL)) {
377                 return false;
378             }
379             *objp = obj;
380             return true;
381         }
382     }
383
384     return true;
385 }
386
387 /*
388  * RegExp static properties.
389  *
390  * RegExp class static properties and their Perl counterparts:
391  *
392  *  RegExp.input                $_
393  *  RegExp.multiline            $*
394  *  RegExp.lastMatch            $&
395  *  RegExp.lastParen            $+
396  *  RegExp.leftContext          $`
397  *  RegExp.rightContext         $'
398  */
399
400 #define DEFINE_STATIC_GETTER(name, code)                                        \
401     static JSBool                                                               \
402     name(JSContext *cx, JSObject *obj, jsid id, jsval *vp)                      \
403     {                                                                           \
404         RegExpStatics *res = cx->regExpStatics();                               \
405         code;                                                                   \
406     }
407
408 DEFINE_STATIC_GETTER(static_input_getter,        return res->createPendingInput(cx, Valueify(vp)))
409 DEFINE_STATIC_GETTER(static_multiline_getter,    *vp = BOOLEAN_TO_JSVAL(res->multiline());
410                                                  return true)
411 DEFINE_STATIC_GETTER(static_lastMatch_getter,    return res->createLastMatch(cx, Valueify(vp)))
412 DEFINE_STATIC_GETTER(static_lastParen_getter,    return res->createLastParen(cx, Valueify(vp)))
413 DEFINE_STATIC_GETTER(static_leftContext_getter,  return res->createLeftContext(cx, Valueify(vp)))
414 DEFINE_STATIC_GETTER(static_rightContext_getter, return res->createRightContext(cx, Valueify(vp)))
415
416 DEFINE_STATIC_GETTER(static_paren1_getter,       return res->createParen(cx, 1, Valueify(vp)))
417 DEFINE_STATIC_GETTER(static_paren2_getter,       return res->createParen(cx, 2, Valueify(vp)))
418 DEFINE_STATIC_GETTER(static_paren3_getter,       return res->createParen(cx, 3, Valueify(vp)))
419 DEFINE_STATIC_GETTER(static_paren4_getter,       return res->createParen(cx, 4, Valueify(vp)))
420 DEFINE_STATIC_GETTER(static_paren5_getter,       return res->createParen(cx, 5, Valueify(vp)))
421 DEFINE_STATIC_GETTER(static_paren6_getter,       return res->createParen(cx, 6, Valueify(vp)))
422 DEFINE_STATIC_GETTER(static_paren7_getter,       return res->createParen(cx, 7, Valueify(vp)))
423 DEFINE_STATIC_GETTER(static_paren8_getter,       return res->createParen(cx, 8, Valueify(vp)))
424 DEFINE_STATIC_GETTER(static_paren9_getter,       return res->createParen(cx, 9, Valueify(vp)))
425
426 #define DEFINE_STATIC_SETTER(name, code)                                        \
427     static JSBool                                                               \
428     name(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp)       \
429     {                                                                           \
430         RegExpStatics *res = cx->regExpStatics();                               \
431         code;                                                                   \
432         return true;                                                            \
433     }
434
435 DEFINE_STATIC_SETTER(static_input_setter,
436                      if (!JSVAL_IS_STRING(*vp) && !JS_ConvertValue(cx, *vp, JSTYPE_STRING, vp))
437                          return false;
438                      res->setPendingInput(JSVAL_TO_STRING(*vp)))
439 DEFINE_STATIC_SETTER(static_multiline_setter,
440                      if (!JSVAL_IS_BOOLEAN(*vp) && !JS_ConvertValue(cx, *vp, JSTYPE_BOOLEAN, vp))
441                          return false;
442                      res->setMultiline(!!JSVAL_TO_BOOLEAN(*vp)))
443
444 const uint8 REGEXP_STATIC_PROP_ATTRS    = JSPROP_PERMANENT | JSPROP_SHARED | JSPROP_ENUMERATE;
445 const uint8 RO_REGEXP_STATIC_PROP_ATTRS = REGEXP_STATIC_PROP_ATTRS | JSPROP_READONLY;
446
447 static JSPropertySpec regexp_static_props[] = {
448     {"input",        0, REGEXP_STATIC_PROP_ATTRS,    static_input_getter, static_input_setter},
449     {"multiline",    0, REGEXP_STATIC_PROP_ATTRS,    static_multiline_getter,
450                                                      static_multiline_setter},
451     {"lastMatch",    0, RO_REGEXP_STATIC_PROP_ATTRS, static_lastMatch_getter,    NULL},
452     {"lastParen",    0, RO_REGEXP_STATIC_PROP_ATTRS, static_lastParen_getter,    NULL},
453     {"leftContext",  0, RO_REGEXP_STATIC_PROP_ATTRS, static_leftContext_getter,  NULL},
454     {"rightContext", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_rightContext_getter, NULL},
455     {"$1",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren1_getter,       NULL},
456     {"$2",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren2_getter,       NULL},
457     {"$3",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren3_getter,       NULL},
458     {"$4",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren4_getter,       NULL},
459     {"$5",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren5_getter,       NULL},
460     {"$6",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren6_getter,       NULL},
461     {"$7",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren7_getter,       NULL},
462     {"$8",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren8_getter,       NULL},
463     {"$9",           0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren9_getter,       NULL},
464     {0,0,0,0,0}
465 };
466
467 static void
468 regexp_finalize(JSContext *cx, JSObject *obj)
469 {
470     RegExp *re = RegExp::extractFrom(obj);
471     if (!re)
472         return;
473     re->decref(cx);
474 }
475
476 /* Forward static prototype. */
477 static JSBool
478 regexp_exec_sub(JSContext *cx, JSObject *obj, uintN argc, Value *argv, JSBool test, Value *rval);
479
480 static JSBool
481 regexp_call(JSContext *cx, uintN argc, Value *vp)
482 {
483     return regexp_exec_sub(cx, &JS_CALLEE(cx, vp).toObject(), argc, JS_ARGV(cx, vp), false, vp);
484 }
485
486 #if JS_HAS_XDR
487
488 #include "jsxdrapi.h"
489
490 JSBool
491 js_XDRRegExpObject(JSXDRState *xdr, JSObject **objp)
492 {
493     JSString *source = 0;
494     uint32 flagsword = 0;
495
496     if (xdr->mode == JSXDR_ENCODE) {
497         JS_ASSERT(objp);
498         RegExp *re = RegExp::extractFrom(*objp);
499         if (!re)
500             return false;
501         source = re->getSource();
502         flagsword = re->getFlags();
503     }
504     if (!JS_XDRString(xdr, &source) || !JS_XDRUint32(xdr, &flagsword))
505         return false;
506     if (xdr->mode == JSXDR_DECODE) {
507         JSObject *obj = NewBuiltinClassInstance(xdr->cx, &js_RegExpClass);
508         if (!obj)
509             return false;
510         obj->clearParent();
511         obj->clearProto();
512         AlreadyIncRefed<RegExp> re = RegExp::create(xdr->cx, source, flagsword);
513         if (!re)
514             return false;
515         obj->setPrivate(re.get());
516         obj->zeroRegExpLastIndex();
517         *objp = obj;
518     }
519     return true;
520 }
521
522 #else  /* !JS_HAS_XDR */
523
524 #define js_XDRRegExpObject NULL
525
526 #endif /* !JS_HAS_XDR */
527
528 static void
529 regexp_trace(JSTracer *trc, JSObject *obj)
530 {
531     RegExp *re = RegExp::extractFrom(obj);
532     if (re && re->getSource())
533         MarkString(trc, re->getSource(), "source");
534 }
535
536 static JSBool
537 regexp_enumerate(JSContext *cx, JSObject *obj)
538 {
539     JS_ASSERT(obj->isRegExp());
540
541     jsval v;
542     if (!JS_LookupPropertyById(cx, obj, ATOM_TO_JSID(cx->runtime->atomState.lastIndexAtom), &v))
543         return false;
544
545     for (size_t i = 0; i < JS_ARRAY_LENGTH(lazyRegExpProps); i++) {
546         const LazyProp &lazy = lazyRegExpProps[i];
547         jsid id = ATOM_TO_JSID(OFFSET_TO_ATOM(cx->runtime, lazy.atomOffset));
548         if (!JS_LookupPropertyById(cx, obj, id, &v))
549             return false;
550     }
551
552     return true;
553 }
554
555 js::Class js_RegExpClass = {
556     js_RegExp_str,
557     JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
558     JSCLASS_HAS_RESERVED_SLOTS(JSObject::REGEXP_CLASS_RESERVED_SLOTS) |
559     JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_RegExp),
560     PropertyStub,         /* addProperty */
561     PropertyStub,         /* delProperty */
562     PropertyStub,         /* getProperty */
563     StrictPropertyStub,   /* setProperty */
564     regexp_enumerate,
565     reinterpret_cast<JSResolveOp>(regexp_resolve),
566     ConvertStub,
567     regexp_finalize,
568     NULL,                 /* reserved0 */
569     NULL,                 /* checkAccess */
570     regexp_call,
571     NULL,                 /* construct */
572     js_XDRRegExpObject,
573     NULL,                 /* hasInstance */
574     JS_CLASS_TRACE(regexp_trace)
575 };
576
577 /*
578  * RegExp instance methods.
579  */
580
581 JSBool
582 js_regexp_toString(JSContext *cx, JSObject *obj, Value *vp)
583 {
584     if (!InstanceOf(cx, obj, &js_RegExpClass, vp + 2))
585         return false;
586
587     RegExp *re = RegExp::extractFrom(obj);
588     if (!re) {
589         *vp = StringValue(cx->runtime->emptyString);
590         return true;
591     }
592
593     JSLinearString *src = re->getSource();
594     StringBuffer sb(cx);
595     if (size_t len = src->length()) {
596         if (!sb.reserve(len + 2))
597             return false;
598         JS_ALWAYS_TRUE(sb.append('/'));
599         JS_ALWAYS_TRUE(sb.append(src->chars(), len));
600         JS_ALWAYS_TRUE(sb.append('/'));
601     } else {
602         if (!sb.append("/(?:)/"))
603             return false;
604     }
605     if (re->global() && !sb.append('g'))
606         return false;
607     if (re->ignoreCase() && !sb.append('i'))
608         return false;
609     if (re->multiline() && !sb.append('m'))
610         return false;
611     if (re->sticky() && !sb.append('y'))
612         return false;
613
614     JSFlatString *str = sb.finishString();
615     if (!str)
616         return false;
617     *vp = StringValue(str);
618     return true;
619 }
620
621 static JSBool
622 regexp_toString(JSContext *cx, uintN argc, Value *vp)
623 {
624     JSObject *obj = ToObject(cx, &vp[1]);
625     if (!obj)
626         return false;
627     return js_regexp_toString(cx, obj, vp);
628 }
629
630 /*
631  * Return:
632  * - The original if no escaping need be performed.
633  * - A new string if escaping need be performed.
634  * - NULL on error.
635  */
636 static JSString *
637 EscapeNakedForwardSlashes(JSContext *cx, JSString *unescaped)
638 {
639     size_t oldLen = unescaped->length();
640     const jschar *oldChars = unescaped->getChars(cx);
641     if (!oldChars)
642         return NULL;
643     JS::Anchor<JSString *> anchor(unescaped);
644
645     js::Vector<jschar, 128> newChars(cx);
646     for (const jschar *it = oldChars; it < oldChars + oldLen; ++it) {
647         if (*it == '/' && (it == oldChars || it[-1] != '\\')) {
648             if (!newChars.length()) {
649                 if (!newChars.reserve(oldLen + 1))
650                     return NULL;
651                 JS_ALWAYS_TRUE(newChars.append(oldChars, size_t(it - oldChars)));
652             }
653             if (!newChars.append('\\'))
654                 return NULL;
655         }
656
657         if (!newChars.empty() && !newChars.append(*it))
658             return NULL;
659     }
660
661     if (newChars.length()) {
662         size_t len = newChars.length();
663         if (!newChars.append('\0'))
664             return NULL;
665         jschar *chars = newChars.extractRawBuffer();
666         JSString *escaped = js_NewString(cx, chars, len);
667         if (!escaped)
668             cx->free(chars);
669         return escaped;
670     }
671     return unescaped;
672 }
673
674 static bool
675 SwapRegExpInternals(JSContext *cx, JSObject *obj, Value *rval, JSString *str, uint32 flags = 0)
676 {
677     flags |= cx->regExpStatics()->getFlags();
678     AlreadyIncRefed<RegExp> re = RegExp::create(cx, str, flags);
679     if (!re)
680         return false;
681     SwapObjectRegExp(cx, obj, re);
682     *rval = ObjectValue(*obj);
683     return true;
684 }
685
686 static JSBool
687 regexp_exec_sub(JSContext *cx, JSObject *obj, uintN argc, Value *argv, JSBool test, Value *rval)
688 {
689     if (!InstanceOf(cx, obj, &js_RegExpClass, argv))
690         return false;
691
692     RegExp *re = RegExp::extractFrom(obj);
693     if (!re)
694         return true;
695
696     /* 
697      * Code execution under this call could swap out the guts of |obj|, so we
698      * have to take a defensive refcount here.
699      */
700     AutoRefCount<RegExp> arc(cx, NeedsIncRef<RegExp>(re));
701
702     jsdouble lastIndex;
703     if (re->global() || re->sticky()) {
704         const Value v = obj->getRegExpLastIndex();
705         if (v.isInt32()) {
706             lastIndex = v.toInt32();
707         } else {
708             if (v.isDouble())
709                 lastIndex = v.toDouble();
710             else if (!ValueToNumber(cx, v, &lastIndex))
711                 return JS_FALSE;
712             lastIndex = js_DoubleToInteger(lastIndex);
713         }
714     } else {
715         lastIndex = 0;
716     }
717
718     RegExpStatics *res = cx->regExpStatics();
719
720     JSString *input;
721     if (argc) {
722         input = js_ValueToString(cx, argv[0]);
723         if (!input)
724             return false;
725         argv[0] = StringValue(input);
726     } else {
727         /* Need to grab input from statics. */
728         input = res->getPendingInput();
729         if (!input) {
730             JSAutoByteString sourceBytes(cx, re->getSource());
731             if (!!sourceBytes) {
732                 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NO_INPUT,
733                                      sourceBytes.ptr(),
734                                      re->global() ? "g" : "",
735                                      re->ignoreCase() ? "i" : "",
736                                      re->multiline() ? "m" : "",
737                                      re->sticky() ? "y" : "");
738             }
739             return false;
740         }
741     }
742
743     if (lastIndex < 0 || input->length() < lastIndex) {
744         obj->zeroRegExpLastIndex();
745         *rval = NullValue();
746         return true;
747     }
748
749     size_t lastIndexInt(lastIndex);
750     if (!re->execute(cx, res, input, &lastIndexInt, !!test, rval))
751         return false;
752
753     /* Update lastIndex. */
754     if (re->global() || (!rval->isNull() && re->sticky())) {
755         if (rval->isNull())
756             obj->zeroRegExpLastIndex();
757         else
758             obj->setRegExpLastIndex(lastIndexInt);
759     }
760
761     return true;
762 }
763
764 JSBool
765 js_regexp_exec(JSContext *cx, uintN argc, Value *vp)
766 {
767     JSObject *obj = ToObject(cx, &vp[1]);
768     if (!obj)
769         return false;
770     return regexp_exec_sub(cx, obj, argc, vp + 2, JS_FALSE, vp);
771 }
772
773 JSBool
774 js_regexp_test(JSContext *cx, uintN argc, Value *vp)
775 {
776     JSObject *obj = ToObject(cx, &vp[1]);
777     if (!obj)
778         return false;
779     if (!regexp_exec_sub(cx, obj, argc, vp + 2, JS_TRUE, vp))
780         return false;
781     if (!vp->isTrue())
782         vp->setBoolean(false);
783     return true;
784 }
785
786 /*
787  * Compile new js::RegExp guts for obj.
788  *
789  * Per ECMAv5 15.10.4.1, we act on combinations of (pattern, flags) as
790  * arguments:
791  *
792  *  RegExp, undefined => flags := pattern.flags
793  *  RegExp, _ => throw TypeError
794  *  _ => pattern := ToString(pattern) if defined(pattern) else ''
795  *       flags := ToString(flags) if defined(flags) else ''
796  */
797 static bool
798 CompileRegExpAndSwap(JSContext *cx, JSObject *obj, uintN argc, Value *argv, Value *rval)
799 {
800     if (argc == 0)
801         return SwapRegExpInternals(cx, obj, rval, cx->runtime->emptyString);
802
803     Value sourceValue = argv[0];
804     if (sourceValue.isObject() && sourceValue.toObject().getClass() == &js_RegExpClass) {
805         /*
806          * If we get passed in a RegExp object we return a new object with the
807          * same RegExp (internal matcher program) guts.
808          * Note: the regexp static flags are not taken into consideration here.
809          */
810         JSObject &sourceObj = sourceValue.toObject();
811         if (argc >= 2 && !argv[1].isUndefined()) {
812             JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NEWREGEXP_FLAGGED);
813             return false;
814         }
815         RegExp *re = RegExp::extractFrom(&sourceObj);
816         if (!re)
817             return false;
818
819         re->incref(cx);
820         SwapObjectRegExp(cx, obj, AlreadyIncRefed<RegExp>(re));
821
822         *rval = ObjectValue(*obj);
823         return true;
824     }
825
826     JSString *sourceStr;
827     if (sourceValue.isUndefined()) {
828         sourceStr = cx->runtime->emptyString;
829     } else {
830         /* Coerce to string and compile. */
831         sourceStr = js_ValueToString(cx, sourceValue);
832         if (!sourceStr)
833             return false;
834     }  
835
836     uintN flags = 0;
837     if (argc > 1 && !argv[1].isUndefined()) {
838         JSString *flagStr = js_ValueToString(cx, argv[1]);
839         if (!flagStr)
840             return false;
841         argv[1].setString(flagStr);
842         if (!RegExp::parseFlags(cx, flagStr, &flags))
843             return false;
844     }
845
846     JSString *escapedSourceStr = EscapeNakedForwardSlashes(cx, sourceStr);
847     if (!escapedSourceStr)
848         return false;
849
850     return SwapRegExpInternals(cx, obj, rval, escapedSourceStr, flags);
851 }
852
853 static JSBool
854 regexp_compile(JSContext *cx, uintN argc, Value *vp)
855 {
856     JSObject *obj = ToObject(cx, &vp[1]);
857     if (!obj || !InstanceOf(cx, obj, &js_RegExpClass, JS_ARGV(cx, vp)))
858         return false;
859
860     return CompileRegExpAndSwap(cx, obj, argc, JS_ARGV(cx, vp), &JS_RVAL(cx, vp));
861 }
862
863 static JSBool
864 regexp_construct(JSContext *cx, uintN argc, Value *vp)
865 {
866     Value *argv = JS_ARGV(cx, vp);
867
868     if (!IsConstructing(vp)) {
869         /*
870          * If first arg is regexp and no flags are given, just return the arg.
871          * Otherwise, delegate to the standard constructor.
872          * See ECMAv5 15.10.3.1.
873          */
874         if (argc >= 1 && argv[0].isObject() && argv[0].toObject().isRegExp() &&
875             (argc == 1 || argv[1].isUndefined())) {
876             *vp = argv[0];
877             return true;
878         }
879     }
880
881     JSObject *obj = NewBuiltinClassInstance(cx, &js_RegExpClass);
882     if (!obj)
883         return false;
884
885     return CompileRegExpAndSwap(cx, obj, argc, argv, &JS_RVAL(cx, vp));
886 }
887
888 static JSFunctionSpec regexp_methods[] = {
889 #if JS_HAS_TOSOURCE
890     JS_FN(js_toSource_str,  regexp_toString,    0,0),
891 #endif
892     JS_FN(js_toString_str,  regexp_toString,    0,0),
893     JS_FN("compile",        regexp_compile,     2,0),
894     JS_FN("exec",           js_regexp_exec,     1,0),
895     JS_FN("test",           js_regexp_test,     1,0),
896     JS_FS_END
897 };
898
899 /* Similar to SwapRegExpInternals. */
900 static bool
901 InitRegExpClassCompile(JSContext *cx, JSObject *obj)
902 {
903     AlreadyIncRefed<RegExp> re = RegExp::create(cx, cx->runtime->emptyString, 0);
904     if (!re)
905         return false;
906     SwapObjectRegExp(cx, obj, re);
907     return true;
908 }
909
910 JSObject *
911 js_InitRegExpClass(JSContext *cx, JSObject *obj)
912 {
913     JSObject *proto = js_InitClass(cx, obj, NULL, &js_RegExpClass, regexp_construct, 2,
914                                    NULL, regexp_methods, regexp_static_props, NULL);
915     if (!proto)
916         return NULL;
917
918     JSObject *ctor = JS_GetConstructor(cx, proto);
919     if (!ctor)
920         return NULL;
921
922     /* Give RegExp.prototype private data so it matches the empty string. */
923     if (!JS_AliasProperty(cx, ctor, "input",        "$_") ||
924         !JS_AliasProperty(cx, ctor, "multiline",    "$*") ||
925         !JS_AliasProperty(cx, ctor, "lastMatch",    "$&") ||
926         !JS_AliasProperty(cx, ctor, "lastParen",    "$+") ||
927         !JS_AliasProperty(cx, ctor, "leftContext",  "$`") ||
928         !JS_AliasProperty(cx, ctor, "rightContext", "$'") ||
929         !InitRegExpClassCompile(cx, proto)) {
930         return NULL;
931     }
932
933     return proto;
934 }