Encapsulate RegExpStatics more. (r=gal, b=610223)
[mozilla-central.git] / js / src / jsregexp.cpp
bloba604d858a3b7b399a7a765dc10dcbeaaa4c1cea9
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set sw=4 ts=8 et tw=99:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
17 * The Original Code is Mozilla Communicator client code, released
18 * March 31, 1998.
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.
25 * Contributor(s):
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.
39 * ***** END LICENSE BLOCK ***** */
42 * JS regular expressions, after Perl.
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"
58 #include "jsobjinlines.h"
59 #include "jsregexpinlines.h"
61 #ifdef JS_TRACER
62 #include "jstracer.h"
63 using namespace avmplus;
64 using namespace nanojit;
65 #endif
67 using namespace js;
68 using namespace js::gc;
71 * RegExpStatics allocates memory -- in order to keep the statics stored
72 * per-global and not leak, we create a js::Class to wrap the C++ instance and
73 * provide an appropriate finalizer. We store an instance of that js::Class in
74 * a global reserved slot.
77 static void
78 resc_finalize(JSContext *cx, JSObject *obj)
80 RegExpStatics *res = static_cast<RegExpStatics *>(obj->getPrivate());
81 cx->destroy<RegExpStatics>(res);
84 static void
85 resc_trace(JSTracer *trc, JSObject *obj)
87 void *pdata = obj->getPrivate();
88 JS_ASSERT(pdata);
89 RegExpStatics *res = static_cast<RegExpStatics *>(pdata);
90 res->mark(trc);
93 Class js::regexp_statics_class = {
94 "RegExpStatics",
95 JSCLASS_HAS_PRIVATE | JSCLASS_MARK_IS_TRACE,
96 PropertyStub, /* addProperty */
97 PropertyStub, /* delProperty */
98 PropertyStub, /* getProperty */
99 PropertyStub, /* setProperty */
100 EnumerateStub,
101 ResolveStub,
102 ConvertStub,
103 resc_finalize,
104 NULL, /* reserved0 */
105 NULL, /* checkAccess */
106 NULL, /* call */
107 NULL, /* construct */
108 NULL, /* xdrObject */
109 NULL, /* hasInstance */
110 JS_CLASS_TRACE(resc_trace)
114 * Lock obj and replace its regexp internals with |newRegExp|.
115 * Decref the replaced regexp internals.
117 static void
118 SwapObjectRegExp(JSContext *cx, JSObject *obj, RegExp &newRegExp)
120 RegExp *oldRegExp = RegExp::extractFrom(obj);
121 obj->setPrivate(&newRegExp);
122 obj->zeroRegExpLastIndex();
123 if (oldRegExp)
124 oldRegExp->decref(cx);
127 JSObject * JS_FASTCALL
128 js_CloneRegExpObject(JSContext *cx, JSObject *obj, JSObject *proto)
130 JS_ASSERT(obj->getClass() == &js_RegExpClass);
131 JS_ASSERT(proto);
132 JS_ASSERT(proto->getClass() == &js_RegExpClass);
133 JSObject *clone = NewNativeClassInstance(cx, &js_RegExpClass, proto, proto->getParent());
134 if (!clone)
135 return NULL;
136 RegExpStatics *res = cx->regExpStatics();
137 RegExp *re = RegExp::extractFrom(obj);
139 uint32 origFlags = re->getFlags();
140 uint32 staticsFlags = res->getFlags();
141 if ((origFlags & staticsFlags) != staticsFlags) {
143 * This regex is lacking flags from the statics, so we must recompile with the new
144 * flags instead of increffing.
146 re = RegExp::create(cx, re->getSource(), origFlags | staticsFlags);
147 } else {
148 re->incref(cx);
151 clone->setPrivate(re);
152 clone->zeroRegExpLastIndex();
153 return clone;
156 #ifdef JS_TRACER
157 JS_DEFINE_CALLINFO_3(extern, OBJECT, js_CloneRegExpObject, CONTEXT, OBJECT, OBJECT, 0,
158 ACCSET_STORE_ANY)
159 #endif
161 JSBool
162 js_ObjectIsRegExp(JSObject *obj)
164 return obj->isRegExp();
168 * js::RegExp
171 void
172 RegExp::handleYarrError(JSContext *cx, int error)
174 /* Hack: duplicated from yarr/yarr/RegexParser.h */
175 enum ErrorCode {
176 NoError,
177 PatternTooLarge,
178 QuantifierOutOfOrder,
179 QuantifierWithoutAtom,
180 MissingParentheses,
181 ParenthesesUnmatched,
182 ParenthesesTypeInvalid, /* "(?" with bad next char or end of pattern. */
183 CharacterClassUnmatched,
184 CharacterClassOutOfOrder,
185 QuantifierTooLarge,
186 EscapeUnterminated
188 switch (error) {
189 case NoError:
190 JS_NOT_REACHED("Precondition violation: an error must have occurred.");
191 return;
192 #define COMPILE_EMSG(__code, __msg) \
193 case __code: \
194 JS_ReportErrorFlagsAndNumberUC(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL, __msg); \
195 return
196 COMPILE_EMSG(PatternTooLarge, JSMSG_REGEXP_TOO_COMPLEX);
197 COMPILE_EMSG(QuantifierOutOfOrder, JSMSG_BAD_QUANTIFIER);
198 COMPILE_EMSG(QuantifierWithoutAtom, JSMSG_BAD_QUANTIFIER);
199 COMPILE_EMSG(MissingParentheses, JSMSG_MISSING_PAREN);
200 COMPILE_EMSG(ParenthesesUnmatched, JSMSG_UNMATCHED_RIGHT_PAREN);
201 COMPILE_EMSG(ParenthesesTypeInvalid, JSMSG_BAD_QUANTIFIER);
202 COMPILE_EMSG(CharacterClassUnmatched, JSMSG_BAD_CLASS_RANGE);
203 COMPILE_EMSG(CharacterClassOutOfOrder, JSMSG_BAD_CLASS_RANGE);
204 COMPILE_EMSG(EscapeUnterminated, JSMSG_TRAILING_SLASH);
205 COMPILE_EMSG(QuantifierTooLarge, JSMSG_BAD_QUANTIFIER);
206 #undef COMPILE_EMSG
207 default:
208 JS_NOT_REACHED("Precondition violation: unknown Yarr error code.");
212 void
213 RegExp::handlePCREError(JSContext *cx, int error)
215 #define REPORT(__msg) \
216 JS_ReportErrorFlagsAndNumberUC(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL, __msg); \
217 return
218 switch (error) {
219 case 0: JS_NOT_REACHED("Precondition violation: an error must have occurred.");
220 case 1: REPORT(JSMSG_TRAILING_SLASH);
221 case 2: REPORT(JSMSG_TRAILING_SLASH);
222 case 3: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
223 case 4: REPORT(JSMSG_BAD_QUANTIFIER);
224 case 5: REPORT(JSMSG_BAD_QUANTIFIER);
225 case 6: REPORT(JSMSG_BAD_CLASS_RANGE);
226 case 7: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
227 case 8: REPORT(JSMSG_BAD_CLASS_RANGE);
228 case 9: REPORT(JSMSG_BAD_QUANTIFIER);
229 case 10: REPORT(JSMSG_UNMATCHED_RIGHT_PAREN);
230 case 11: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
231 case 12: REPORT(JSMSG_UNMATCHED_RIGHT_PAREN);
232 case 13: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
233 case 14: REPORT(JSMSG_MISSING_PAREN);
234 case 15: REPORT(JSMSG_BAD_BACKREF);
235 case 16: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
236 case 17: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
237 default:
238 JS_NOT_REACHED("Precondition violation: unknown PCRE error code.");
240 #undef REPORT
243 bool
244 RegExp::parseFlags(JSContext *cx, JSString *flagStr, uint32 &flagsOut)
246 const jschar *s;
247 size_t n;
248 flagStr->getCharsAndLength(s, n);
249 flagsOut = 0;
250 for (size_t i = 0; i < n; i++) {
251 #define HANDLE_FLAG(__name) \
252 JS_BEGIN_MACRO \
253 if (flagsOut & (__name)) \
254 goto bad_flag; \
255 flagsOut |= (__name); \
256 JS_END_MACRO
257 switch (s[i]) {
258 case 'i': HANDLE_FLAG(JSREG_FOLD); break;
259 case 'g': HANDLE_FLAG(JSREG_GLOB); break;
260 case 'm': HANDLE_FLAG(JSREG_MULTILINE); break;
261 case 'y': HANDLE_FLAG(JSREG_STICKY); break;
262 default:
263 bad_flag:
265 char charBuf[2];
266 charBuf[0] = char(s[i]);
267 charBuf[1] = '\0';
268 JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL,
269 JSMSG_BAD_REGEXP_FLAG, charBuf);
270 return false;
273 #undef HANDLE_FLAG
275 return true;
278 RegExp *
279 RegExp::createFlagged(JSContext *cx, JSString *str, JSString *opt)
281 if (!opt)
282 return create(cx, str, 0);
283 uint32 flags = 0;
284 if (!parseFlags(cx, opt, flags))
285 return false;
286 return create(cx, str, flags);
290 * RegExp instance properties.
292 #define DEFINE_GETTER(name, code) \
293 static JSBool \
294 name(JSContext *cx, JSObject *obj, jsid id, Value *vp) \
296 while (obj->getClass() != &js_RegExpClass) { \
297 obj = obj->getProto(); \
298 if (!obj) \
299 return true; \
301 RegExp *re = RegExp::extractFrom(obj); \
302 code; \
303 return true; \
306 /* lastIndex is stored in the object, re = re silences the compiler warning. */
307 DEFINE_GETTER(lastIndex_getter, re = re; *vp = obj->getRegExpLastIndex())
308 DEFINE_GETTER(source_getter, *vp = StringValue(re->getSource()))
309 DEFINE_GETTER(global_getter, *vp = BooleanValue(re->global()))
310 DEFINE_GETTER(ignoreCase_getter, *vp = BooleanValue(re->ignoreCase()))
311 DEFINE_GETTER(multiline_getter, *vp = BooleanValue(re->multiline()))
312 DEFINE_GETTER(sticky_getter, *vp = BooleanValue(re->sticky()))
314 static JSBool
315 lastIndex_setter(JSContext *cx, JSObject *obj, jsid id, Value *vp)
317 while (obj->getClass() != &js_RegExpClass) {
318 obj = obj->getProto();
319 if (!obj)
320 return true;
322 obj->setRegExpLastIndex(*vp);
323 return true;
326 static const struct LazyProp {
327 const char *name;
328 uint16 atomOffset;
329 PropertyOp getter;
330 } lazyRegExpProps[] = {
331 { js_source_str, ATOM_OFFSET(source), source_getter },
332 { js_global_str, ATOM_OFFSET(global), global_getter },
333 { js_ignoreCase_str, ATOM_OFFSET(ignoreCase), ignoreCase_getter },
334 { js_multiline_str, ATOM_OFFSET(multiline), multiline_getter },
335 { js_sticky_str, ATOM_OFFSET(sticky), sticky_getter }
338 static JSBool
339 regexp_resolve(JSContext *cx, JSObject *obj, jsid id, uint32 flags, JSObject **objp)
341 JS_ASSERT(obj->isRegExp());
343 if (!JSID_IS_ATOM(id))
344 return JS_TRUE;
346 if (id == ATOM_TO_JSID(cx->runtime->atomState.lastIndexAtom)) {
347 if (!js_DefineNativeProperty(cx, obj, id, UndefinedValue(),
348 lastIndex_getter, lastIndex_setter,
349 JSPROP_PERMANENT | JSPROP_SHARED,
350 0, 0, NULL)) {
351 return false;
353 *objp = obj;
354 return true;
357 for (size_t i = 0; i < JS_ARRAY_LENGTH(lazyRegExpProps); i++) {
358 const LazyProp &lazy = lazyRegExpProps[i];
359 JSAtom *atom = OFFSET_TO_ATOM(cx->runtime, lazy.atomOffset);
360 if (id == ATOM_TO_JSID(atom)) {
361 if (!js_DefineNativeProperty(cx, obj, id, UndefinedValue(),
362 lazy.getter, NULL,
363 JSPROP_PERMANENT | JSPROP_SHARED | JSPROP_READONLY,
364 0, 0, NULL)) {
365 return false;
367 *objp = obj;
368 return true;
372 return true;
376 * RegExp static properties.
378 * RegExp class static properties and their Perl counterparts:
380 * RegExp.input $_
381 * RegExp.multiline $*
382 * RegExp.lastMatch $&
383 * RegExp.lastParen $+
384 * RegExp.leftContext $`
385 * RegExp.rightContext $'
388 #define DEFINE_STATIC_GETTER(name, code) \
389 static JSBool \
390 name(JSContext *cx, JSObject *obj, jsid id, jsval *vp) \
392 RegExpStatics *res = cx->regExpStatics(); \
393 code; \
396 DEFINE_STATIC_GETTER(static_input_getter, return res->createPendingInput(cx, Valueify(vp)))
397 DEFINE_STATIC_GETTER(static_multiline_getter, *vp = BOOLEAN_TO_JSVAL(res->multiline());
398 return true)
399 DEFINE_STATIC_GETTER(static_lastMatch_getter, return res->createLastMatch(cx, Valueify(vp)))
400 DEFINE_STATIC_GETTER(static_lastParen_getter, return res->createLastParen(cx, Valueify(vp)))
401 DEFINE_STATIC_GETTER(static_leftContext_getter, return res->createLeftContext(cx, Valueify(vp)))
402 DEFINE_STATIC_GETTER(static_rightContext_getter, return res->createRightContext(cx, Valueify(vp)))
404 DEFINE_STATIC_GETTER(static_paren1_getter, return res->createParen(cx, 0, Valueify(vp)))
405 DEFINE_STATIC_GETTER(static_paren2_getter, return res->createParen(cx, 1, Valueify(vp)))
406 DEFINE_STATIC_GETTER(static_paren3_getter, return res->createParen(cx, 2, Valueify(vp)))
407 DEFINE_STATIC_GETTER(static_paren4_getter, return res->createParen(cx, 3, Valueify(vp)))
408 DEFINE_STATIC_GETTER(static_paren5_getter, return res->createParen(cx, 4, Valueify(vp)))
409 DEFINE_STATIC_GETTER(static_paren6_getter, return res->createParen(cx, 5, Valueify(vp)))
410 DEFINE_STATIC_GETTER(static_paren7_getter, return res->createParen(cx, 6, Valueify(vp)))
411 DEFINE_STATIC_GETTER(static_paren8_getter, return res->createParen(cx, 7, Valueify(vp)))
412 DEFINE_STATIC_GETTER(static_paren9_getter, return res->createParen(cx, 8, Valueify(vp)))
414 #define DEFINE_STATIC_SETTER(name, code) \
415 static JSBool \
416 name(JSContext *cx, JSObject *obj, jsid id, jsval *vp) \
418 RegExpStatics *res = cx->regExpStatics(); \
419 code; \
420 return true; \
423 DEFINE_STATIC_SETTER(static_input_setter,
424 if (!JSVAL_IS_STRING(*vp) && !JS_ConvertValue(cx, *vp, JSTYPE_STRING, vp))
425 return false;
426 res->setPendingInput(JSVAL_TO_STRING(*vp)))
427 DEFINE_STATIC_SETTER(static_multiline_setter,
428 if (!JSVAL_IS_BOOLEAN(*vp) && !JS_ConvertValue(cx, *vp, JSTYPE_BOOLEAN, vp))
429 return false;
430 res->setMultiline(!!JSVAL_TO_BOOLEAN(*vp)))
432 const uint8 REGEXP_STATIC_PROP_ATTRS = JSPROP_PERMANENT | JSPROP_SHARED | JSPROP_ENUMERATE;
433 const uint8 RO_REGEXP_STATIC_PROP_ATTRS = REGEXP_STATIC_PROP_ATTRS | JSPROP_READONLY;
435 static JSPropertySpec regexp_static_props[] = {
436 {"input", 0, REGEXP_STATIC_PROP_ATTRS, static_input_getter, static_input_setter},
437 {"multiline", 0, REGEXP_STATIC_PROP_ATTRS, static_multiline_getter,
438 static_multiline_setter},
439 {"lastMatch", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_lastMatch_getter, NULL},
440 {"lastParen", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_lastParen_getter, NULL},
441 {"leftContext", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_leftContext_getter, NULL},
442 {"rightContext", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_rightContext_getter, NULL},
443 {"$1", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren1_getter, NULL},
444 {"$2", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren2_getter, NULL},
445 {"$3", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren3_getter, NULL},
446 {"$4", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren4_getter, NULL},
447 {"$5", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren5_getter, NULL},
448 {"$6", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren6_getter, NULL},
449 {"$7", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren7_getter, NULL},
450 {"$8", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren8_getter, NULL},
451 {"$9", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren9_getter, NULL},
452 {0,0,0,0,0}
455 static void
456 regexp_finalize(JSContext *cx, JSObject *obj)
458 RegExp *re = RegExp::extractFrom(obj);
459 if (!re)
460 return;
461 re->decref(cx);
464 /* Forward static prototype. */
465 static JSBool
466 regexp_exec_sub(JSContext *cx, JSObject *obj, uintN argc, Value *argv, JSBool test, Value *rval);
468 static JSBool
469 regexp_call(JSContext *cx, uintN argc, Value *vp)
471 return regexp_exec_sub(cx, &JS_CALLEE(cx, vp).toObject(), argc, JS_ARGV(cx, vp), false, vp);
474 #if JS_HAS_XDR
476 #include "jsxdrapi.h"
478 JSBool
479 js_XDRRegExpObject(JSXDRState *xdr, JSObject **objp)
481 JSString *source = 0;
482 uint32 flagsword = 0;
484 if (xdr->mode == JSXDR_ENCODE) {
485 JS_ASSERT(objp);
486 RegExp *re = RegExp::extractFrom(*objp);
487 if (!re)
488 return false;
489 source = re->getSource();
490 flagsword = re->getFlags();
492 if (!JS_XDRString(xdr, &source) || !JS_XDRUint32(xdr, &flagsword))
493 return false;
494 if (xdr->mode == JSXDR_DECODE) {
495 JSObject *obj = NewBuiltinClassInstance(xdr->cx, &js_RegExpClass);
496 if (!obj)
497 return false;
498 obj->clearParent();
499 obj->clearProto();
500 RegExp *re = RegExp::create(xdr->cx, source, flagsword);
501 if (!re)
502 return false;
503 obj->setPrivate(re);
504 obj->zeroRegExpLastIndex();
505 *objp = obj;
507 return true;
510 #else /* !JS_HAS_XDR */
512 #define js_XDRRegExpObject NULL
514 #endif /* !JS_HAS_XDR */
516 static void
517 regexp_trace(JSTracer *trc, JSObject *obj)
519 RegExp *re = RegExp::extractFrom(obj);
520 if (re && re->getSource())
521 MarkString(trc, re->getSource(), "source");
524 static JSBool
525 regexp_enumerate(JSContext *cx, JSObject *obj)
527 JS_ASSERT(obj->isRegExp());
529 jsval v;
530 if (!JS_LookupPropertyById(cx, obj, ATOM_TO_JSID(cx->runtime->atomState.lastIndexAtom), &v))
531 return false;
533 for (size_t i = 0; i < JS_ARRAY_LENGTH(lazyRegExpProps); i++) {
534 const LazyProp &lazy = lazyRegExpProps[i];
535 jsid id = ATOM_TO_JSID(OFFSET_TO_ATOM(cx->runtime, lazy.atomOffset));
536 if (!JS_LookupPropertyById(cx, obj, id, &v))
537 return false;
540 return true;
543 js::Class js_RegExpClass = {
544 js_RegExp_str,
545 JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
546 JSCLASS_HAS_RESERVED_SLOTS(JSObject::REGEXP_CLASS_RESERVED_SLOTS) |
547 JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_RegExp),
548 PropertyStub, /* addProperty */
549 PropertyStub, /* delProperty */
550 PropertyStub, /* getProperty */
551 PropertyStub, /* setProperty */
552 regexp_enumerate,
553 reinterpret_cast<JSResolveOp>(regexp_resolve),
554 ConvertStub,
555 regexp_finalize,
556 NULL, /* reserved0 */
557 NULL, /* checkAccess */
558 regexp_call,
559 NULL, /* construct */
560 js_XDRRegExpObject,
561 NULL, /* hasInstance */
562 JS_CLASS_TRACE(regexp_trace)
566 * RegExp instance methods.
569 JSBool
570 js_regexp_toString(JSContext *cx, JSObject *obj, Value *vp)
572 static const jschar empty_regexp_ucstr[] = {'(', '?', ':', ')', 0};
573 if (!InstanceOf(cx, obj, &js_RegExpClass, vp + 2))
574 return false;
575 RegExp *re = RegExp::extractFrom(obj);
576 if (!re) {
577 *vp = StringValue(cx->runtime->emptyString);
578 return true;
581 const jschar *source;
582 size_t length;
583 re->getSource()->getCharsAndLength(source, length);
584 if (length == 0) {
585 source = empty_regexp_ucstr;
586 length = JS_ARRAY_LENGTH(empty_regexp_ucstr) - 1;
588 length += 2;
589 uint32 nflags = re->flagCount();
590 jschar *chars = (jschar*) cx->malloc((length + nflags + 1) * sizeof(jschar));
591 if (!chars) {
592 return false;
595 chars[0] = '/';
596 js_strncpy(&chars[1], source, length - 2);
597 chars[length - 1] = '/';
598 if (nflags) {
599 if (re->global())
600 chars[length++] = 'g';
601 if (re->ignoreCase())
602 chars[length++] = 'i';
603 if (re->multiline())
604 chars[length++] = 'm';
605 if (re->sticky())
606 chars[length++] = 'y';
608 chars[length] = 0;
610 JSString *str = js_NewString(cx, chars, length);
611 if (!str) {
612 cx->free(chars);
613 return false;
615 *vp = StringValue(str);
616 return true;
619 static JSBool
620 regexp_toString(JSContext *cx, uintN argc, Value *vp)
622 JSObject *obj = JS_THIS_OBJECT(cx, Jsvalify(vp));
623 return obj && js_regexp_toString(cx, obj, vp);
627 * Return:
628 * - The original if no escaping need be performed.
629 * - A new string if escaping need be performed.
630 * - NULL on error.
632 static JSString *
633 EscapeNakedForwardSlashes(JSContext *cx, JSString *unescaped)
635 const jschar *oldChars;
636 size_t oldLen;
637 unescaped->getCharsAndLength(oldChars, oldLen);
638 js::Vector<jschar, 128> newChars(cx);
639 for (const jschar *it = oldChars; it < oldChars + oldLen; ++it) {
640 if (*it == '/' && (it == oldChars || it[-1] != '\\')) {
641 if (!newChars.length()) {
642 if (!newChars.reserve(oldLen + 1))
643 return NULL;
644 newChars.append(oldChars, size_t(it - oldChars));
646 newChars.append('\\');
649 if (newChars.length())
650 newChars.append(*it);
653 if (newChars.length()) {
654 size_t len = newChars.length();
655 jschar *chars = newChars.extractRawBuffer();
656 if (!chars)
657 return NULL;
658 JSString *escaped = js_NewString(cx, chars, len);
659 if (!escaped)
660 cx->free(chars);
661 return escaped;
663 return unescaped;
666 static bool
667 regexp_compile_sub_tail(JSContext *cx, JSObject *obj, Value *rval, JSString *str, uint32 flags = 0)
669 flags |= cx->regExpStatics()->getFlags();
670 RegExp *re = RegExp::create(cx, str, flags);
671 if (!re)
672 return false;
673 SwapObjectRegExp(cx, obj, *re);
674 *rval = ObjectValue(*obj);
675 return true;
678 static JSBool
679 regexp_compile_sub(JSContext *cx, JSObject *obj, uintN argc, Value *argv, Value *rval)
681 if (!InstanceOf(cx, obj, &js_RegExpClass, argv))
682 return false;
684 if (argc == 0)
685 return regexp_compile_sub_tail(cx, obj, rval, cx->runtime->emptyString);
687 Value sourceValue = argv[0];
688 if (sourceValue.isObject() && sourceValue.toObject().getClass() == &js_RegExpClass) {
690 * If we get passed in a RegExp object we construct a new
691 * RegExp that is a duplicate of it by re-compiling the
692 * original source code. ECMA requires that it be an error
693 * here if the flags are specified. (We must use the flags
694 * from the original RegExp also).
696 JSObject &sourceObj = sourceValue.toObject();
697 if (argc >= 2 && !argv[1].isUndefined()) {
698 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NEWREGEXP_FLAGGED);
699 return false;
701 RegExp *clone;
703 RegExp *re = RegExp::extractFrom(&sourceObj);
704 if (!re)
705 return false;
706 clone = RegExp::clone(cx, *re);
708 if (!clone)
709 return false;
710 SwapObjectRegExp(cx, obj, *clone);
711 *rval = ObjectValue(*obj);
712 return true;
715 /* Coerce to string and compile. */
716 JSString *sourceStr = js_ValueToString(cx, sourceValue);
717 if (!sourceStr)
718 return false;
719 argv[0] = StringValue(sourceStr);
720 uint32 flags = 0;
721 if (argc > 1 && !argv[1].isUndefined()) {
722 JSString *flagStr = js_ValueToString(cx, argv[1]);
723 if (!flagStr)
724 return false;
725 argv[1] = StringValue(flagStr);
726 if (!RegExp::parseFlags(cx, flagStr, flags))
727 return false;
730 JSString *escapedSourceStr = EscapeNakedForwardSlashes(cx, sourceStr);
731 if (!escapedSourceStr)
732 return false;
733 argv[0] = StringValue(escapedSourceStr);
735 return regexp_compile_sub_tail(cx, obj, rval, escapedSourceStr, flags);
738 static JSBool
739 regexp_compile(JSContext *cx, uintN argc, Value *vp)
741 JSObject *obj = JS_THIS_OBJECT(cx, Jsvalify(vp));
742 return obj && regexp_compile_sub(cx, obj, argc, vp + 2, vp);
745 static JSBool
746 regexp_exec_sub(JSContext *cx, JSObject *obj, uintN argc, Value *argv, JSBool test, Value *rval)
748 bool ok = InstanceOf(cx, obj, &js_RegExpClass, argv);
749 if (!ok)
750 return JS_FALSE;
752 RegExp *re = RegExp::extractFrom(obj);
753 if (!re)
754 return JS_TRUE;
756 /* NB: we must reach out: after this paragraph, in order to drop re. */
757 re->incref(cx);
758 jsdouble lastIndex;
759 if (re->global() || re->sticky()) {
760 const Value v = obj->getRegExpLastIndex();
761 if (v.isInt32()) {
762 lastIndex = v.toInt32();
763 } else {
764 if (v.isDouble())
765 lastIndex = v.toDouble();
766 else if (!ValueToNumber(cx, v, &lastIndex))
767 return JS_FALSE;
768 lastIndex = js_DoubleToInteger(lastIndex);
770 } else {
771 lastIndex = 0;
774 /* Now that obj is unlocked, it's safe to (potentially) grab the GC lock. */
775 RegExpStatics *res = cx->regExpStatics();
776 JSString *str;
777 if (argc) {
778 str = js_ValueToString(cx, argv[0]);
779 if (!str) {
780 ok = JS_FALSE;
781 goto out;
783 argv[0] = StringValue(str);
784 } else {
785 /* Need to grab input from statics. */
786 str = res->getPendingInput();
787 if (!str) {
788 const char *sourceBytes = js_GetStringBytes(cx, re->getSource());
789 if (sourceBytes) {
790 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NO_INPUT, sourceBytes,
791 re->global() ? "g" : "",
792 re->ignoreCase() ? "i" : "",
793 re->multiline() ? "m" : "",
794 re->sticky() ? "y" : "");
796 ok = false;
797 goto out;
801 if (lastIndex < 0 || str->length() < lastIndex) {
802 obj->zeroRegExpLastIndex();
803 *rval = NullValue();
804 } else {
805 size_t lastIndexInt = (size_t) lastIndex;
806 ok = re->execute(cx, res, str, &lastIndexInt, !!test, rval);
807 if (ok && (re->global() || (!rval->isNull() && re->sticky()))) {
808 if (rval->isNull())
809 obj->zeroRegExpLastIndex();
810 else
811 obj->setRegExpLastIndex(lastIndexInt);
815 out:
816 re->decref(cx);
817 return ok;
820 JSBool
821 js_regexp_exec(JSContext *cx, uintN argc, Value *vp)
823 return regexp_exec_sub(cx, JS_THIS_OBJECT(cx, Jsvalify(vp)), argc, vp + 2, JS_FALSE, vp);
826 JSBool
827 js_regexp_test(JSContext *cx, uintN argc, Value *vp)
829 if (!regexp_exec_sub(cx, JS_THIS_OBJECT(cx, Jsvalify(vp)), argc, vp + 2, JS_TRUE, vp))
830 return false;
831 if (!vp->isTrue())
832 vp->setBoolean(false);
833 return true;
836 static JSFunctionSpec regexp_methods[] = {
837 #if JS_HAS_TOSOURCE
838 JS_FN(js_toSource_str, regexp_toString, 0,0),
839 #endif
840 JS_FN(js_toString_str, regexp_toString, 0,0),
841 JS_FN("compile", regexp_compile, 2,0),
842 JS_FN("exec", js_regexp_exec, 1,0),
843 JS_FN("test", js_regexp_test, 1,0),
844 JS_FS_END
847 static JSBool
848 regexp_construct(JSContext *cx, uintN argc, Value *vp)
850 Value *argv = JS_ARGV(cx, vp);
851 if (!IsConstructing(vp)) {
853 * If first arg is regexp and no flags are given, just return the arg.
854 * (regexp_compile_sub detects the regexp + flags case and throws a
855 * TypeError.) See 15.10.3.1.
857 if (argc >= 1 && argv[0].isObject() && argv[0].toObject().isRegExp() &&
858 (argc == 1 || argv[1].isUndefined()))
860 *vp = argv[0];
861 return true;
865 /* Otherwise, replace obj with a new RegExp object. */
866 JSObject *obj = NewBuiltinClassInstance(cx, &js_RegExpClass);
867 if (!obj)
868 return false;
870 return regexp_compile_sub(cx, obj, argc, argv, vp);
873 /* Similar to regexp_compile_sub_tail. */
874 static bool
875 InitRegExpClassCompile(JSContext *cx, JSObject *obj)
877 RegExp *re = RegExp::create(cx, cx->runtime->emptyString, 0);
878 if (!re)
879 return false;
880 SwapObjectRegExp(cx, obj, *re);
881 return true;
884 JSObject *
885 js_InitRegExpClass(JSContext *cx, JSObject *obj)
887 JSObject *proto = js_InitClass(cx, obj, NULL, &js_RegExpClass, regexp_construct, 1,
888 NULL, regexp_methods, regexp_static_props, NULL);
889 if (!proto)
890 return NULL;
892 JSObject *ctor = JS_GetConstructor(cx, proto);
893 if (!ctor)
894 return NULL;
896 /* Give RegExp.prototype private data so it matches the empty string. */
897 if (!JS_AliasProperty(cx, ctor, "input", "$_") ||
898 !JS_AliasProperty(cx, ctor, "multiline", "$*") ||
899 !JS_AliasProperty(cx, ctor, "lastMatch", "$&") ||
900 !JS_AliasProperty(cx, ctor, "lastParen", "$+") ||
901 !JS_AliasProperty(cx, ctor, "leftContext", "$`") ||
902 !JS_AliasProperty(cx, ctor, "rightContext", "$'") ||
903 !InitRegExpClassCompile(cx, proto)) {
904 return NULL;
907 return proto;