2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2010-08-11 13:30:07 -07:00
|
|
|
* vim: set sw=4 ts=8 et tw=99:
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS regular expressions, after Perl.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "jstypes.h"
|
2009-03-18 11:38:16 -07:00
|
|
|
#include "jsstdint.h"
|
2010-08-11 13:30:07 -07:00
|
|
|
#include "jsutil.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "jsgc.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsregexp.h"
|
|
|
|
#include "jsstr.h"
|
2009-07-16 17:17:35 -07:00
|
|
|
#include "jsvector.h"
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
#include "jsobjinlines.h"
|
|
|
|
#include "jsregexpinlines.h"
|
|
|
|
|
2008-11-04 16:03:57 -08:00
|
|
|
#ifdef JS_TRACER
|
2008-11-04 14:51:51 -08:00
|
|
|
#include "jstracer.h"
|
|
|
|
using namespace avmplus;
|
|
|
|
using namespace nanojit;
|
2008-11-04 16:03:57 -08:00
|
|
|
#endif
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2010-01-22 14:49:18 -08:00
|
|
|
using namespace js;
|
2010-09-24 10:54:39 -07:00
|
|
|
using namespace js::gc;
|
2010-01-22 14:49:18 -08:00
|
|
|
|
2010-09-13 15:53:50 -07:00
|
|
|
/*
|
|
|
|
* RegExpStatics allocates memory -- in order to keep the statics stored
|
|
|
|
* per-global and not leak, we create a js::Class to wrap the C++ instance and
|
|
|
|
* provide an appropriate finalizer. We store an instance of that js::Class in
|
|
|
|
* a global reserved slot.
|
|
|
|
*/
|
2007-03-22 20:35:42 -07:00
|
|
|
|
2010-09-13 15:53:50 -07:00
|
|
|
static void
|
|
|
|
resc_finalize(JSContext *cx, JSObject *obj)
|
2007-03-22 20:35:42 -07:00
|
|
|
{
|
2010-09-13 15:53:50 -07:00
|
|
|
RegExpStatics *res = static_cast<RegExpStatics *>(obj->getPrivate());
|
|
|
|
cx->destroy<RegExpStatics>(res);
|
|
|
|
}
|
2007-03-22 20:35:42 -07:00
|
|
|
|
2010-09-13 15:53:50 -07:00
|
|
|
static void
|
|
|
|
resc_trace(JSTracer *trc, JSObject *obj)
|
|
|
|
{
|
|
|
|
void *pdata = obj->getPrivate();
|
|
|
|
JS_ASSERT(pdata);
|
|
|
|
RegExpStatics *res = static_cast<RegExpStatics *>(pdata);
|
|
|
|
res->mark(trc);
|
|
|
|
}
|
2007-03-22 20:35:42 -07:00
|
|
|
|
2010-09-13 15:53:50 -07:00
|
|
|
Class js::regexp_statics_class = {
|
|
|
|
"RegExpStatics",
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_MARK_IS_TRACE,
|
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
PropertyStub, /* setProperty */
|
|
|
|
EnumerateStub,
|
|
|
|
ResolveStub,
|
|
|
|
ConvertStub,
|
|
|
|
resc_finalize,
|
|
|
|
NULL, /* reserved0 */
|
|
|
|
NULL, /* checkAccess */
|
|
|
|
NULL, /* call */
|
|
|
|
NULL, /* construct */
|
|
|
|
NULL, /* xdrObject */
|
|
|
|
NULL, /* hasInstance */
|
|
|
|
JS_CLASS_TRACE(resc_trace)
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2010-08-11 13:30:07 -07:00
|
|
|
* Lock obj and replace its regexp internals with |newRegExp|.
|
|
|
|
* Decref the replaced regexp internals.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-08-11 13:30:07 -07:00
|
|
|
static void
|
|
|
|
SwapObjectRegExp(JSContext *cx, JSObject *obj, RegExp &newRegExp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-10-22 17:04:22 -07:00
|
|
|
RegExp *oldRegExp = RegExp::extractFrom(obj);
|
|
|
|
obj->setPrivate(&newRegExp);
|
|
|
|
obj->zeroRegExpLastIndex();
|
2010-08-11 13:30:07 -07:00
|
|
|
if (oldRegExp)
|
|
|
|
oldRegExp->decref(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
JSObject * JS_FASTCALL
|
|
|
|
js_CloneRegExpObject(JSContext *cx, JSObject *obj, JSObject *proto)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
JS_ASSERT(obj->getClass() == &js_RegExpClass);
|
|
|
|
JS_ASSERT(proto);
|
|
|
|
JS_ASSERT(proto->getClass() == &js_RegExpClass);
|
|
|
|
JSObject *clone = NewNativeClassInstance(cx, &js_RegExpClass, proto, proto->getParent());
|
|
|
|
if (!clone)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2010-09-13 15:53:50 -07:00
|
|
|
RegExpStatics *res = cx->regExpStatics();
|
2010-08-11 13:30:07 -07:00
|
|
|
RegExp *re = RegExp::extractFrom(obj);
|
|
|
|
{
|
|
|
|
uint32 origFlags = re->getFlags();
|
2010-09-13 15:53:50 -07:00
|
|
|
uint32 staticsFlags = res->getFlags();
|
2010-08-11 13:30:07 -07:00
|
|
|
if ((origFlags & staticsFlags) != staticsFlags) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2010-08-11 13:30:07 -07:00
|
|
|
* This regex is lacking flags from the statics, so we must recompile with the new
|
|
|
|
* flags instead of increffing.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-08-11 13:30:07 -07:00
|
|
|
re = RegExp::create(cx, re->getSource(), origFlags | staticsFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-08-11 13:30:07 -07:00
|
|
|
re->incref(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2010-08-11 13:30:07 -07:00
|
|
|
clone->setPrivate(re);
|
|
|
|
clone->zeroRegExpLastIndex();
|
|
|
|
return clone;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
#ifdef JS_TRACER
|
|
|
|
JS_DEFINE_CALLINFO_3(extern, OBJECT, js_CloneRegExpObject, CONTEXT, OBJECT, OBJECT, 0,
|
|
|
|
ACCSET_STORE_ANY)
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
JSBool
|
|
|
|
js_ObjectIsRegExp(JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
return obj->isRegExp();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-08-11 13:30:07 -07:00
|
|
|
* js::RegExp
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
void
|
|
|
|
RegExp::handleYarrError(JSContext *cx, int error)
|
|
|
|
{
|
|
|
|
/* Hack: duplicated from yarr/yarr/RegexParser.h */
|
|
|
|
enum ErrorCode {
|
|
|
|
NoError,
|
|
|
|
PatternTooLarge,
|
|
|
|
QuantifierOutOfOrder,
|
|
|
|
QuantifierWithoutAtom,
|
|
|
|
MissingParentheses,
|
|
|
|
ParenthesesUnmatched,
|
|
|
|
ParenthesesTypeInvalid, /* "(?" with bad next char or end of pattern. */
|
|
|
|
CharacterClassUnmatched,
|
|
|
|
CharacterClassOutOfOrder,
|
|
|
|
QuantifierTooLarge,
|
|
|
|
EscapeUnterminated
|
|
|
|
};
|
|
|
|
switch (error) {
|
|
|
|
case NoError:
|
|
|
|
JS_NOT_REACHED("Precondition violation: an error must have occurred.");
|
|
|
|
return;
|
|
|
|
#define COMPILE_EMSG(__code, __msg) \
|
|
|
|
case __code: \
|
|
|
|
JS_ReportErrorFlagsAndNumberUC(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL, __msg); \
|
|
|
|
return
|
|
|
|
COMPILE_EMSG(PatternTooLarge, JSMSG_REGEXP_TOO_COMPLEX);
|
|
|
|
COMPILE_EMSG(QuantifierOutOfOrder, JSMSG_BAD_QUANTIFIER);
|
|
|
|
COMPILE_EMSG(QuantifierWithoutAtom, JSMSG_BAD_QUANTIFIER);
|
|
|
|
COMPILE_EMSG(MissingParentheses, JSMSG_MISSING_PAREN);
|
|
|
|
COMPILE_EMSG(ParenthesesUnmatched, JSMSG_UNMATCHED_RIGHT_PAREN);
|
|
|
|
COMPILE_EMSG(ParenthesesTypeInvalid, JSMSG_BAD_QUANTIFIER);
|
|
|
|
COMPILE_EMSG(CharacterClassUnmatched, JSMSG_BAD_CLASS_RANGE);
|
|
|
|
COMPILE_EMSG(CharacterClassOutOfOrder, JSMSG_BAD_CLASS_RANGE);
|
|
|
|
COMPILE_EMSG(EscapeUnterminated, JSMSG_TRAILING_SLASH);
|
|
|
|
COMPILE_EMSG(QuantifierTooLarge, JSMSG_BAD_QUANTIFIER);
|
|
|
|
#undef COMPILE_EMSG
|
|
|
|
default:
|
|
|
|
JS_NOT_REACHED("Precondition violation: unknown Yarr error code.");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-11 13:30:07 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
void
|
|
|
|
RegExp::handlePCREError(JSContext *cx, int error)
|
|
|
|
{
|
|
|
|
#define REPORT(__msg) \
|
|
|
|
JS_ReportErrorFlagsAndNumberUC(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL, __msg); \
|
|
|
|
return
|
|
|
|
switch (error) {
|
|
|
|
case 0: JS_NOT_REACHED("Precondition violation: an error must have occurred.");
|
|
|
|
case 1: REPORT(JSMSG_TRAILING_SLASH);
|
|
|
|
case 2: REPORT(JSMSG_TRAILING_SLASH);
|
|
|
|
case 3: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
|
|
|
|
case 4: REPORT(JSMSG_BAD_QUANTIFIER);
|
|
|
|
case 5: REPORT(JSMSG_BAD_QUANTIFIER);
|
|
|
|
case 6: REPORT(JSMSG_BAD_CLASS_RANGE);
|
|
|
|
case 7: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
|
|
|
|
case 8: REPORT(JSMSG_BAD_CLASS_RANGE);
|
|
|
|
case 9: REPORT(JSMSG_BAD_QUANTIFIER);
|
|
|
|
case 10: REPORT(JSMSG_UNMATCHED_RIGHT_PAREN);
|
|
|
|
case 11: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
|
|
|
|
case 12: REPORT(JSMSG_UNMATCHED_RIGHT_PAREN);
|
|
|
|
case 13: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
|
|
|
|
case 14: REPORT(JSMSG_MISSING_PAREN);
|
|
|
|
case 15: REPORT(JSMSG_BAD_BACKREF);
|
|
|
|
case 16: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
|
|
|
|
case 17: REPORT(JSMSG_REGEXP_TOO_COMPLEX);
|
|
|
|
default:
|
|
|
|
JS_NOT_REACHED("Precondition violation: unknown PCRE error code.");
|
|
|
|
}
|
|
|
|
#undef REPORT
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
bool
|
|
|
|
RegExp::parseFlags(JSContext *cx, JSString *flagStr, uint32 &flagsOut)
|
|
|
|
{
|
|
|
|
const jschar *s;
|
|
|
|
size_t n;
|
|
|
|
flagStr->getCharsAndLength(s, n);
|
|
|
|
flagsOut = 0;
|
|
|
|
for (size_t i = 0; i < n; i++) {
|
|
|
|
#define HANDLE_FLAG(__name) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
if (flagsOut & (__name)) \
|
|
|
|
goto bad_flag; \
|
|
|
|
flagsOut |= (__name); \
|
|
|
|
JS_END_MACRO
|
|
|
|
switch (s[i]) {
|
|
|
|
case 'i': HANDLE_FLAG(JSREG_FOLD); break;
|
|
|
|
case 'g': HANDLE_FLAG(JSREG_GLOB); break;
|
|
|
|
case 'm': HANDLE_FLAG(JSREG_MULTILINE); break;
|
|
|
|
case 'y': HANDLE_FLAG(JSREG_STICKY); break;
|
|
|
|
default:
|
|
|
|
bad_flag:
|
|
|
|
{
|
|
|
|
char charBuf[2];
|
|
|
|
charBuf[0] = char(s[i]);
|
|
|
|
charBuf[1] = '\0';
|
|
|
|
JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_REGEXP_FLAG, charBuf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef HANDLE_FLAG
|
|
|
|
}
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
RegExp *
|
|
|
|
RegExp::createFlagged(JSContext *cx, JSString *str, JSString *opt)
|
|
|
|
{
|
|
|
|
if (!opt)
|
|
|
|
return create(cx, str, 0);
|
|
|
|
uint32 flags = 0;
|
|
|
|
if (!parseFlags(cx, opt, flags))
|
|
|
|
return false;
|
|
|
|
return create(cx, str, flags);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
/*
|
|
|
|
* RegExp instance properties.
|
|
|
|
*/
|
2010-05-30 18:31:04 -07:00
|
|
|
#define DEFINE_GETTER(name, code) \
|
|
|
|
static JSBool \
|
2010-07-14 23:19:36 -07:00
|
|
|
name(JSContext *cx, JSObject *obj, jsid id, Value *vp) \
|
2010-05-30 18:31:04 -07:00
|
|
|
{ \
|
|
|
|
while (obj->getClass() != &js_RegExpClass) { \
|
|
|
|
obj = obj->getProto(); \
|
|
|
|
if (!obj) \
|
|
|
|
return true; \
|
|
|
|
} \
|
2010-08-11 13:30:07 -07:00
|
|
|
RegExp *re = RegExp::extractFrom(obj); \
|
2010-05-30 18:31:04 -07:00
|
|
|
code; \
|
|
|
|
return true; \
|
2009-08-22 09:59:55 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-30 18:31:04 -07:00
|
|
|
/* lastIndex is stored in the object, re = re silences the compiler warning. */
|
|
|
|
DEFINE_GETTER(lastIndex_getter, re = re; *vp = obj->getRegExpLastIndex())
|
2010-08-11 13:30:07 -07:00
|
|
|
DEFINE_GETTER(source_getter, *vp = StringValue(re->getSource()))
|
|
|
|
DEFINE_GETTER(global_getter, *vp = BooleanValue(re->global()))
|
|
|
|
DEFINE_GETTER(ignoreCase_getter, *vp = BooleanValue(re->ignoreCase()))
|
|
|
|
DEFINE_GETTER(multiline_getter, *vp = BooleanValue(re->multiline()))
|
|
|
|
DEFINE_GETTER(sticky_getter, *vp = BooleanValue(re->sticky()))
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
lastIndex_setter(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-07 16:09:49 -07:00
|
|
|
while (obj->getClass() != &js_RegExpClass) {
|
2010-03-04 20:44:09 -08:00
|
|
|
obj = obj->getProto();
|
2008-07-07 14:00:56 -07:00
|
|
|
if (!obj)
|
2010-05-30 18:31:04 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-12 13:40:44 -07:00
|
|
|
obj->setRegExpLastIndex(*vp);
|
2010-07-14 23:19:36 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
static const struct LazyProp {
|
|
|
|
const char *name;
|
|
|
|
uint16 atomOffset;
|
2010-07-14 23:19:36 -07:00
|
|
|
PropertyOp getter;
|
2009-09-24 14:33:14 -07:00
|
|
|
} lazyRegExpProps[] = {
|
|
|
|
{ js_source_str, ATOM_OFFSET(source), source_getter },
|
|
|
|
{ js_global_str, ATOM_OFFSET(global), global_getter },
|
|
|
|
{ js_ignoreCase_str, ATOM_OFFSET(ignoreCase), ignoreCase_getter },
|
|
|
|
{ js_multiline_str, ATOM_OFFSET(multiline), multiline_getter },
|
|
|
|
{ js_sticky_str, ATOM_OFFSET(sticky), sticky_getter }
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
2010-08-11 13:30:07 -07:00
|
|
|
regexp_resolve(JSContext *cx, JSObject *obj, jsid id, uint32 flags, JSObject **objp)
|
2010-06-05 18:16:03 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(obj->isRegExp());
|
|
|
|
|
|
|
|
if (!JSID_IS_ATOM(id))
|
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
if (id == ATOM_TO_JSID(cx->runtime->atomState.lastIndexAtom)) {
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!js_DefineNativeProperty(cx, obj, id, UndefinedValue(),
|
2010-06-05 18:16:03 -07:00
|
|
|
lastIndex_getter, lastIndex_setter,
|
2010-08-11 13:30:07 -07:00
|
|
|
JSPROP_PERMANENT | JSPROP_SHARED,
|
|
|
|
0, 0, NULL)) {
|
|
|
|
return false;
|
2010-06-05 18:16:03 -07:00
|
|
|
}
|
|
|
|
*objp = obj;
|
2010-08-11 13:30:07 -07:00
|
|
|
return true;
|
2010-06-05 18:16:03 -07:00
|
|
|
}
|
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
for (size_t i = 0; i < JS_ARRAY_LENGTH(lazyRegExpProps); i++) {
|
|
|
|
const LazyProp &lazy = lazyRegExpProps[i];
|
2010-06-05 18:16:03 -07:00
|
|
|
JSAtom *atom = OFFSET_TO_ATOM(cx->runtime, lazy.atomOffset);
|
|
|
|
if (id == ATOM_TO_JSID(atom)) {
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!js_DefineNativeProperty(cx, obj, id, UndefinedValue(),
|
2010-06-05 18:16:03 -07:00
|
|
|
lazy.getter, NULL,
|
|
|
|
JSPROP_PERMANENT | JSPROP_SHARED | JSPROP_READONLY,
|
|
|
|
0, 0, NULL)) {
|
2010-08-11 13:30:07 -07:00
|
|
|
return false;
|
2010-06-05 18:16:03 -07:00
|
|
|
}
|
|
|
|
*objp = obj;
|
2010-08-11 13:30:07 -07:00
|
|
|
return true;
|
2010-06-05 18:16:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
return true;
|
2010-06-05 18:16:03 -07:00
|
|
|
}
|
2009-07-15 17:48:22 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2010-08-11 13:30:07 -07:00
|
|
|
* RegExp static properties.
|
|
|
|
*
|
2007-03-22 10:30:00 -07:00
|
|
|
* RegExp class static properties and their Perl counterparts:
|
|
|
|
*
|
|
|
|
* RegExp.input $_
|
|
|
|
* RegExp.multiline $*
|
|
|
|
* RegExp.lastMatch $&
|
|
|
|
* RegExp.lastParen $+
|
|
|
|
* RegExp.leftContext $`
|
|
|
|
* RegExp.rightContext $'
|
|
|
|
*/
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
#define DEFINE_STATIC_GETTER(name, code) \
|
|
|
|
static JSBool \
|
|
|
|
name(JSContext *cx, JSObject *obj, jsid id, jsval *vp) \
|
|
|
|
{ \
|
2010-09-13 15:53:50 -07:00
|
|
|
RegExpStatics *res = cx->regExpStatics(); \
|
2010-08-11 13:30:07 -07:00
|
|
|
code; \
|
|
|
|
}
|
|
|
|
|
2010-09-13 15:53:50 -07:00
|
|
|
DEFINE_STATIC_GETTER(static_input_getter, return res->createInput(cx, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_multiline_getter, *vp = BOOLEAN_TO_JSVAL(res->multiline());
|
2010-08-11 13:30:07 -07:00
|
|
|
return true)
|
2010-09-13 15:53:50 -07:00
|
|
|
DEFINE_STATIC_GETTER(static_lastMatch_getter, return res->createLastMatch(cx, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_lastParen_getter, return res->createLastParen(cx, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_leftContext_getter, return res->createLeftContext(cx, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_rightContext_getter, return res->createRightContext(cx, Valueify(vp)))
|
|
|
|
|
|
|
|
DEFINE_STATIC_GETTER(static_paren1_getter, return res->createParen(cx, 0, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_paren2_getter, return res->createParen(cx, 1, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_paren3_getter, return res->createParen(cx, 2, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_paren4_getter, return res->createParen(cx, 3, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_paren5_getter, return res->createParen(cx, 4, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_paren6_getter, return res->createParen(cx, 5, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_paren7_getter, return res->createParen(cx, 6, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_paren8_getter, return res->createParen(cx, 7, Valueify(vp)))
|
|
|
|
DEFINE_STATIC_GETTER(static_paren9_getter, return res->createParen(cx, 8, Valueify(vp)))
|
2010-08-11 13:30:07 -07:00
|
|
|
|
|
|
|
#define DEFINE_STATIC_SETTER(name, code) \
|
|
|
|
static JSBool \
|
|
|
|
name(JSContext *cx, JSObject *obj, jsid id, jsval *vp) \
|
|
|
|
{ \
|
2010-09-13 15:53:50 -07:00
|
|
|
RegExpStatics *res = cx->regExpStatics(); \
|
2010-08-11 13:30:07 -07:00
|
|
|
code; \
|
|
|
|
return true; \
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-30 18:31:04 -07:00
|
|
|
|
|
|
|
DEFINE_STATIC_SETTER(static_input_setter,
|
|
|
|
if (!JSVAL_IS_STRING(*vp) && !JS_ConvertValue(cx, *vp, JSTYPE_STRING, vp))
|
|
|
|
return false;
|
2010-09-13 15:53:50 -07:00
|
|
|
res->setInput(JSVAL_TO_STRING(*vp)))
|
2010-05-30 18:31:04 -07:00
|
|
|
DEFINE_STATIC_SETTER(static_multiline_setter,
|
|
|
|
if (!JSVAL_IS_BOOLEAN(*vp) && !JS_ConvertValue(cx, *vp, JSTYPE_BOOLEAN, vp))
|
|
|
|
return false;
|
2010-09-13 15:53:50 -07:00
|
|
|
res->setMultiline(!!JSVAL_TO_BOOLEAN(*vp)))
|
2010-05-30 18:31:04 -07:00
|
|
|
|
2010-06-05 18:16:03 -07:00
|
|
|
const uint8 REGEXP_STATIC_PROP_ATTRS = JSPROP_PERMANENT | JSPROP_SHARED | JSPROP_ENUMERATE;
|
|
|
|
const uint8 RO_REGEXP_STATIC_PROP_ATTRS = REGEXP_STATIC_PROP_ATTRS | JSPROP_READONLY;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static JSPropertySpec regexp_static_props[] = {
|
2010-06-05 18:16:03 -07:00
|
|
|
{"input", 0, REGEXP_STATIC_PROP_ATTRS, static_input_getter, static_input_setter},
|
2010-05-30 18:31:04 -07:00
|
|
|
{"multiline", 0, REGEXP_STATIC_PROP_ATTRS, static_multiline_getter,
|
|
|
|
static_multiline_setter},
|
|
|
|
{"lastMatch", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_lastMatch_getter, NULL},
|
|
|
|
{"lastParen", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_lastParen_getter, NULL},
|
|
|
|
{"leftContext", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_leftContext_getter, NULL},
|
|
|
|
{"rightContext", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_rightContext_getter, NULL},
|
|
|
|
{"$1", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren1_getter, NULL},
|
|
|
|
{"$2", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren2_getter, NULL},
|
|
|
|
{"$3", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren3_getter, NULL},
|
|
|
|
{"$4", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren4_getter, NULL},
|
|
|
|
{"$5", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren5_getter, NULL},
|
|
|
|
{"$6", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren6_getter, NULL},
|
|
|
|
{"$7", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren7_getter, NULL},
|
|
|
|
{"$8", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren8_getter, NULL},
|
|
|
|
{"$9", 0, RO_REGEXP_STATIC_PROP_ATTRS, static_paren9_getter, NULL},
|
2007-03-22 10:30:00 -07:00
|
|
|
{0,0,0,0,0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
regexp_finalize(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
RegExp *re = RegExp::extractFrom(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re)
|
|
|
|
return;
|
2010-08-11 13:30:07 -07:00
|
|
|
re->decref(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Forward static prototype. */
|
|
|
|
static JSBool
|
2010-08-11 13:30:07 -07:00
|
|
|
regexp_exec_sub(JSContext *cx, JSObject *obj, uintN argc, Value *argv, JSBool test, Value *rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static JSBool
|
2010-08-16 12:35:04 -07:00
|
|
|
regexp_call(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-16 12:35:04 -07:00
|
|
|
return regexp_exec_sub(cx, &JS_CALLEE(cx, vp).toObject(), argc, JS_ARGV(cx, vp), false, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_XDR
|
|
|
|
|
|
|
|
#include "jsxdrapi.h"
|
|
|
|
|
2009-05-06 16:03:10 -07:00
|
|
|
JSBool
|
|
|
|
js_XDRRegExpObject(JSXDRState *xdr, JSObject **objp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
JSString *source = 0;
|
|
|
|
uint32 flagsword = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
2010-08-11 13:30:07 -07:00
|
|
|
JS_ASSERT(objp);
|
|
|
|
RegExp *re = RegExp::extractFrom(*objp);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re)
|
2010-08-11 13:30:07 -07:00
|
|
|
return false;
|
|
|
|
source = re->getSource();
|
|
|
|
flagsword = re->getFlags();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-11 13:30:07 -07:00
|
|
|
if (!JS_XDRString(xdr, &source) || !JS_XDRUint32(xdr, &flagsword))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
2010-08-11 13:30:07 -07:00
|
|
|
JSObject *obj = NewBuiltinClassInstance(xdr->cx, &js_RegExpClass);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
2010-08-11 13:30:07 -07:00
|
|
|
return false;
|
2010-03-04 23:52:52 -08:00
|
|
|
obj->clearParent();
|
2010-03-04 20:44:09 -08:00
|
|
|
obj->clearProto();
|
2010-08-11 13:30:07 -07:00
|
|
|
RegExp *re = RegExp::create(xdr->cx, source, flagsword);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re)
|
2010-08-11 13:30:07 -07:00
|
|
|
return false;
|
2009-08-04 14:06:55 -07:00
|
|
|
obj->setPrivate(re);
|
2010-04-14 18:57:30 -07:00
|
|
|
obj->zeroRegExpLastIndex();
|
2007-03-22 10:30:00 -07:00
|
|
|
*objp = obj;
|
|
|
|
}
|
2010-08-11 13:30:07 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !JS_HAS_XDR */
|
|
|
|
|
2009-05-06 16:03:10 -07:00
|
|
|
#define js_XDRRegExpObject NULL
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#endif /* !JS_HAS_XDR */
|
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
static void
|
|
|
|
regexp_trace(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
RegExp *re = RegExp::extractFrom(obj);
|
|
|
|
if (re && re->getSource())
|
2010-09-24 10:54:39 -07:00
|
|
|
MarkString(trc, re->getSource(), "source");
|
2010-08-11 13:30:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
regexp_enumerate(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JS_ASSERT(obj->isRegExp());
|
|
|
|
|
|
|
|
jsval v;
|
|
|
|
if (!JS_LookupPropertyById(cx, obj, ATOM_TO_JSID(cx->runtime->atomState.lastIndexAtom), &v))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < JS_ARRAY_LENGTH(lazyRegExpProps); i++) {
|
|
|
|
const LazyProp &lazy = lazyRegExpProps[i];
|
|
|
|
jsid id = ATOM_TO_JSID(OFFSET_TO_ATOM(cx->runtime, lazy.atomOffset));
|
|
|
|
if (!JS_LookupPropertyById(cx, obj, id, &v))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
js::Class js_RegExpClass = {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_RegExp_str,
|
2010-06-05 18:16:03 -07:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
|
2010-08-29 11:57:08 -07:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(JSObject::REGEXP_CLASS_RESERVED_SLOTS) |
|
2007-12-20 15:29:31 -08:00
|
|
|
JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_RegExp),
|
2010-06-12 09:29:04 -07:00
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
PropertyStub, /* setProperty */
|
|
|
|
regexp_enumerate,
|
|
|
|
reinterpret_cast<JSResolveOp>(regexp_resolve),
|
|
|
|
ConvertStub,
|
|
|
|
regexp_finalize,
|
2010-08-11 13:30:07 -07:00
|
|
|
NULL, /* reserved0 */
|
2010-06-12 09:29:04 -07:00
|
|
|
NULL, /* checkAccess */
|
|
|
|
regexp_call,
|
2010-08-11 13:30:07 -07:00
|
|
|
NULL, /* construct */
|
2010-06-12 09:29:04 -07:00
|
|
|
js_XDRRegExpObject,
|
|
|
|
NULL, /* hasInstance */
|
|
|
|
JS_CLASS_TRACE(regexp_trace)
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
/*
|
|
|
|
* RegExp instance methods.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_regexp_toString(JSContext *cx, JSObject *obj, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
static const jschar empty_regexp_ucstr[] = {'(', '?', ':', ')', 0};
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!InstanceOf(cx, obj, &js_RegExpClass, vp + 2))
|
2010-08-11 13:30:07 -07:00
|
|
|
return false;
|
|
|
|
RegExp *re = RegExp::extractFrom(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re) {
|
2010-08-11 13:30:07 -07:00
|
|
|
*vp = StringValue(cx->runtime->emptyString);
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
const jschar *source;
|
|
|
|
size_t length;
|
|
|
|
re->getSource()->getCharsAndLength(source, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (length == 0) {
|
|
|
|
source = empty_regexp_ucstr;
|
2007-11-26 07:18:43 -08:00
|
|
|
length = JS_ARRAY_LENGTH(empty_regexp_ucstr) - 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
length += 2;
|
2010-08-11 13:30:07 -07:00
|
|
|
uint32 nflags = re->flagCount();
|
|
|
|
jschar *chars = (jschar*) cx->malloc((length + nflags + 1) * sizeof(jschar));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!chars) {
|
2010-08-11 13:30:07 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
chars[0] = '/';
|
|
|
|
js_strncpy(&chars[1], source, length - 2);
|
2010-08-11 13:30:07 -07:00
|
|
|
chars[length - 1] = '/';
|
2007-03-22 10:30:00 -07:00
|
|
|
if (nflags) {
|
2010-08-11 13:30:07 -07:00
|
|
|
if (re->global())
|
2007-03-22 10:30:00 -07:00
|
|
|
chars[length++] = 'g';
|
2010-08-11 13:30:07 -07:00
|
|
|
if (re->ignoreCase())
|
2007-03-22 10:30:00 -07:00
|
|
|
chars[length++] = 'i';
|
2010-08-11 13:30:07 -07:00
|
|
|
if (re->multiline())
|
2007-03-22 10:30:00 -07:00
|
|
|
chars[length++] = 'm';
|
2010-08-11 13:30:07 -07:00
|
|
|
if (re->sticky())
|
2007-03-22 10:30:00 -07:00
|
|
|
chars[length++] = 'y';
|
|
|
|
}
|
|
|
|
chars[length] = 0;
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
JSString *str = js_NewString(cx, chars, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(chars);
|
2010-08-11 13:30:07 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-11 13:30:07 -07:00
|
|
|
*vp = StringValue(str);
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
regexp_toString(JSContext *cx, uintN argc, Value *vp)
|
2007-08-01 21:33:52 -07:00
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, Jsvalify(vp));
|
2008-02-17 16:12:33 -08:00
|
|
|
return obj && js_regexp_toString(cx, obj, vp);
|
2007-08-01 21:33:52 -07:00
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
/*
|
|
|
|
* Return:
|
|
|
|
* - The original if no escaping need be performed.
|
|
|
|
* - A new string if escaping need be performed.
|
|
|
|
* - NULL on error.
|
|
|
|
*/
|
|
|
|
static JSString *
|
|
|
|
EscapeNakedForwardSlashes(JSContext *cx, JSString *unescaped)
|
|
|
|
{
|
|
|
|
const jschar *oldChars;
|
|
|
|
size_t oldLen;
|
|
|
|
unescaped->getCharsAndLength(oldChars, oldLen);
|
|
|
|
js::Vector<jschar, 128> newChars(cx);
|
|
|
|
for (const jschar *it = oldChars; it < oldChars + oldLen; ++it) {
|
|
|
|
if (*it == '/' && (it == oldChars || it[-1] != '\\')) {
|
|
|
|
if (!newChars.length()) {
|
|
|
|
if (!newChars.reserve(oldLen + 1))
|
|
|
|
return NULL;
|
|
|
|
newChars.append(oldChars, size_t(it - oldChars));
|
|
|
|
}
|
|
|
|
newChars.append('\\');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newChars.length())
|
|
|
|
newChars.append(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newChars.length()) {
|
|
|
|
size_t len = newChars.length();
|
|
|
|
jschar *chars = newChars.extractRawBuffer();
|
|
|
|
if (!chars)
|
|
|
|
return NULL;
|
|
|
|
JSString *escaped = js_NewString(cx, chars, len);
|
|
|
|
if (!escaped)
|
|
|
|
cx->free(chars);
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
return unescaped;
|
|
|
|
}
|
|
|
|
|
2010-09-13 15:53:50 -07:00
|
|
|
static bool
|
2010-08-11 13:30:07 -07:00
|
|
|
regexp_compile_sub_tail(JSContext *cx, JSObject *obj, Value *rval, JSString *str, uint32 flags = 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-13 15:53:50 -07:00
|
|
|
flags |= cx->regExpStatics()->getFlags();
|
2010-08-11 13:30:07 -07:00
|
|
|
RegExp *re = RegExp::create(cx, str, flags);
|
|
|
|
if (!re)
|
|
|
|
return false;
|
|
|
|
SwapObjectRegExp(cx, obj, *re);
|
|
|
|
*rval = ObjectValue(*obj);
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
static JSBool
|
|
|
|
regexp_compile_sub(JSContext *cx, JSObject *obj, uintN argc, Value *argv, Value *rval)
|
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!InstanceOf(cx, obj, &js_RegExpClass, argv))
|
2010-08-11 13:30:07 -07:00
|
|
|
return false;
|
2010-09-13 15:53:50 -07:00
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
if (argc == 0)
|
|
|
|
return regexp_compile_sub_tail(cx, obj, rval, cx->runtime->emptyString);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
Value sourceValue = argv[0];
|
|
|
|
if (sourceValue.isObject() && sourceValue.toObject().getClass() == &js_RegExpClass) {
|
|
|
|
/*
|
|
|
|
* If we get passed in a RegExp object we construct a new
|
|
|
|
* RegExp that is a duplicate of it by re-compiling the
|
|
|
|
* original source code. ECMA requires that it be an error
|
|
|
|
* here if the flags are specified. (We must use the flags
|
|
|
|
* from the original RegExp also).
|
|
|
|
*/
|
|
|
|
JSObject &sourceObj = sourceValue.toObject();
|
|
|
|
if (argc >= 2 && !argv[1].isUndefined()) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NEWREGEXP_FLAGGED);
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-11 13:30:07 -07:00
|
|
|
RegExp *clone;
|
|
|
|
{
|
|
|
|
RegExp *re = RegExp::extractFrom(&sourceObj);
|
|
|
|
if (!re)
|
|
|
|
return false;
|
|
|
|
clone = RegExp::clone(cx, *re);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-11 13:30:07 -07:00
|
|
|
if (!clone)
|
|
|
|
return false;
|
|
|
|
SwapObjectRegExp(cx, obj, *clone);
|
|
|
|
*rval = ObjectValue(*obj);
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
/* Coerce to string and compile. */
|
|
|
|
JSString *sourceStr = js_ValueToString(cx, sourceValue);
|
|
|
|
if (!sourceStr)
|
|
|
|
return false;
|
|
|
|
argv[0] = StringValue(sourceStr);
|
|
|
|
uint32 flags = 0;
|
|
|
|
if (argc > 1 && !argv[1].isUndefined()) {
|
|
|
|
JSString *flagStr = js_ValueToString(cx, argv[1]);
|
|
|
|
if (!flagStr)
|
|
|
|
return false;
|
|
|
|
argv[1] = StringValue(flagStr);
|
|
|
|
if (!RegExp::parseFlags(cx, flagStr, flags))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString *escapedSourceStr = EscapeNakedForwardSlashes(cx, sourceStr);
|
|
|
|
if (!escapedSourceStr)
|
|
|
|
return false;
|
|
|
|
argv[0] = StringValue(escapedSourceStr);
|
2010-09-13 15:53:50 -07:00
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
return regexp_compile_sub_tail(cx, obj, rval, escapedSourceStr, flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
regexp_compile(JSContext *cx, uintN argc, Value *vp)
|
2007-08-01 21:33:52 -07:00
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, Jsvalify(vp));
|
2008-02-17 16:12:33 -08:00
|
|
|
return obj && regexp_compile_sub(cx, obj, argc, vp + 2, vp);
|
2007-08-01 21:33:52 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2010-08-11 13:30:07 -07:00
|
|
|
regexp_exec_sub(JSContext *cx, JSObject *obj, uintN argc, Value *argv, JSBool test, Value *rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
bool ok = InstanceOf(cx, obj, &js_RegExpClass, argv);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
return JS_FALSE;
|
2010-10-22 17:04:22 -07:00
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
RegExp *re = RegExp::extractFrom(obj);
|
2010-10-22 17:04:22 -07:00
|
|
|
if (!re)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
/* NB: we must reach out: after this paragraph, in order to drop re. */
|
2010-08-11 13:30:07 -07:00
|
|
|
re->incref(cx);
|
|
|
|
jsdouble lastIndex;
|
|
|
|
if (re->global() || re->sticky()) {
|
|
|
|
const Value v = obj->getRegExpLastIndex();
|
2010-07-23 16:30:34 -07:00
|
|
|
if (v.isInt32()) {
|
|
|
|
lastIndex = v.toInt32();
|
2010-07-12 13:40:44 -07:00
|
|
|
} else {
|
2010-07-23 16:30:34 -07:00
|
|
|
if (v.isDouble())
|
|
|
|
lastIndex = v.toDouble();
|
|
|
|
else if (!ValueToNumber(cx, v, &lastIndex))
|
2010-07-12 13:40:44 -07:00
|
|
|
return JS_FALSE;
|
2010-07-23 16:30:34 -07:00
|
|
|
lastIndex = js_DoubleToInteger(lastIndex);
|
2010-07-12 13:40:44 -07:00
|
|
|
}
|
2010-04-14 18:57:30 -07:00
|
|
|
} else {
|
|
|
|
lastIndex = 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Now that obj is unlocked, it's safe to (potentially) grab the GC lock. */
|
2010-09-13 15:53:50 -07:00
|
|
|
RegExpStatics *res = cx->regExpStatics();
|
2010-08-11 13:30:07 -07:00
|
|
|
JSString *str;
|
|
|
|
if (argc) {
|
|
|
|
str = js_ValueToString(cx, argv[0]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-08-11 13:30:07 -07:00
|
|
|
argv[0] = StringValue(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-08-11 13:30:07 -07:00
|
|
|
/* Need to grab input from statics. */
|
2010-09-13 15:53:50 -07:00
|
|
|
str = res->getInput();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str) {
|
2010-08-11 13:30:07 -07:00
|
|
|
const char *sourceBytes = js_GetStringBytes(cx, re->getSource());
|
|
|
|
if (sourceBytes) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NO_INPUT, sourceBytes,
|
|
|
|
re->global() ? "g" : "",
|
|
|
|
re->ignoreCase() ? "i" : "",
|
|
|
|
re->multiline() ? "m" : "",
|
|
|
|
re->sticky() ? "y" : "");
|
|
|
|
}
|
|
|
|
ok = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
if (lastIndex < 0 || str->length() < lastIndex) {
|
2010-04-14 18:57:30 -07:00
|
|
|
obj->zeroRegExpLastIndex();
|
2010-08-11 13:30:07 -07:00
|
|
|
*rval = NullValue();
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-08-11 13:30:07 -07:00
|
|
|
size_t lastIndexInt = (size_t) lastIndex;
|
2010-09-13 15:53:50 -07:00
|
|
|
ok = re->execute(cx, res, str, &lastIndexInt, !!test, rval);
|
2010-08-11 13:30:07 -07:00
|
|
|
if (ok && (re->global() || (!rval->isNull() && re->sticky()))) {
|
2010-07-14 23:19:36 -07:00
|
|
|
if (rval->isNull())
|
2010-04-14 18:57:30 -07:00
|
|
|
obj->zeroRegExpLastIndex();
|
2009-08-22 09:59:55 -07:00
|
|
|
else
|
2010-08-11 13:30:07 -07:00
|
|
|
obj->setRegExpLastIndex(lastIndexInt);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
out:
|
|
|
|
re->decref(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2010-09-01 16:39:56 -07:00
|
|
|
JSBool
|
|
|
|
js_regexp_exec(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
return regexp_exec_sub(cx, JS_THIS_OBJECT(cx, Jsvalify(vp)), argc, vp + 2, JS_FALSE, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-09-01 16:39:56 -07:00
|
|
|
JSBool
|
|
|
|
js_regexp_test(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
if (!regexp_exec_sub(cx, JS_THIS_OBJECT(cx, Jsvalify(vp)), argc, vp + 2, JS_TRUE, vp))
|
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!vp->isTrue())
|
|
|
|
vp->setBoolean(false);
|
2010-08-11 13:30:07 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSFunctionSpec regexp_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_toSource_str, regexp_toString, 0,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_toString_str, regexp_toString, 0,0),
|
|
|
|
JS_FN("compile", regexp_compile, 2,0),
|
2010-09-01 16:39:56 -07:00
|
|
|
JS_FN("exec", js_regexp_exec, 1,0),
|
|
|
|
JS_FN("test", js_regexp_test, 1,0),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
2010-08-16 12:35:04 -07:00
|
|
|
regexp_construct(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-06 17:25:00 -07:00
|
|
|
Value *argv = JS_ARGV(cx, vp);
|
2010-09-06 21:00:08 -07:00
|
|
|
if (!IsConstructing(vp)) {
|
2010-09-06 17:25:00 -07:00
|
|
|
/*
|
|
|
|
* If first arg is regexp and no flags are given, just return the arg.
|
|
|
|
* (regexp_compile_sub detects the regexp + flags case and throws a
|
|
|
|
* TypeError.) See 15.10.3.1.
|
|
|
|
*/
|
|
|
|
if (argc >= 1 && argv[0].isObject() && argv[0].toObject().isRegExp() &&
|
|
|
|
(argc == 1 || argv[1].isUndefined()))
|
|
|
|
{
|
|
|
|
*vp = argv[0];
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-16 12:35:04 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-16 12:35:04 -07:00
|
|
|
/* Otherwise, replace obj with a new RegExp object. */
|
|
|
|
JSObject *obj = NewBuiltinClassInstance(cx, &js_RegExpClass);
|
|
|
|
if (!obj)
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-16 12:35:04 -07:00
|
|
|
return regexp_compile_sub(cx, obj, argc, argv, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-09-13 15:53:50 -07:00
|
|
|
/* Similar to regexp_compile_sub_tail. */
|
|
|
|
static bool
|
|
|
|
InitRegExpClassCompile(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
RegExp *re = RegExp::create(cx, cx->runtime->emptyString, 0);
|
|
|
|
if (!re)
|
|
|
|
return false;
|
|
|
|
SwapObjectRegExp(cx, obj, *re);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *
|
|
|
|
js_InitRegExpClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2010-08-11 13:30:07 -07:00
|
|
|
JSObject *proto = js_InitClass(cx, obj, NULL, &js_RegExpClass, regexp_construct, 1,
|
2010-06-05 18:16:03 -07:00
|
|
|
NULL, regexp_methods, regexp_static_props, NULL);
|
2009-03-04 19:26:16 -08:00
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-03-04 19:26:16 -08:00
|
|
|
JSObject *ctor = JS_GetConstructor(cx, proto);
|
|
|
|
if (!ctor)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2009-03-04 19:26:16 -08:00
|
|
|
|
|
|
|
/* Give RegExp.prototype private data so it matches the empty string. */
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!JS_AliasProperty(cx, ctor, "input", "$_") ||
|
|
|
|
!JS_AliasProperty(cx, ctor, "multiline", "$*") ||
|
|
|
|
!JS_AliasProperty(cx, ctor, "lastMatch", "$&") ||
|
|
|
|
!JS_AliasProperty(cx, ctor, "lastParen", "$+") ||
|
|
|
|
!JS_AliasProperty(cx, ctor, "leftContext", "$`") ||
|
2009-03-04 19:26:16 -08:00
|
|
|
!JS_AliasProperty(cx, ctor, "rightContext", "$'") ||
|
2010-09-13 15:53:50 -07:00
|
|
|
!InitRegExpClassCompile(cx, proto)) {
|
2009-03-04 19:26:16 -08:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
}
|