Bug 1108949 - RegExp(RegExp object, flags) no longer throws. r=till

This commit is contained in:
ziyunfei 2015-03-10 21:36:00 +01:00
parent dfb04afbed
commit 5870724746
4 changed files with 53 additions and 133 deletions

View File

@ -141,18 +141,10 @@ js::ExecuteRegExpLegacy(JSContext *cx, RegExpStatics *res, RegExpObject &reobj,
/*
* Compile a new |RegExpShared| for the |RegExpObject|.
*
* Per ECMAv5 15.10.4.1, we act on combinations of (pattern, flags) as
* arguments:
*
* RegExp, undefined => flags := pattern.flags
* RegExp, _ => throw TypeError
* _ => pattern := ToString(pattern) if defined(pattern) else ''
* flags := ToString(flags) if defined(flags) else ''
*/
static bool
CompileRegExpObject(JSContext *cx, RegExpObjectBuilder &builder, CallArgs args,
RegExpStaticsUse staticsUse)
RegExpStaticsUse staticsUse, RegExpCreationMode creationMode)
{
if (args.length() == 0) {
MOZ_ASSERT(staticsUse == UseRegExpStatics);
@ -169,11 +161,16 @@ CompileRegExpObject(JSContext *cx, RegExpObjectBuilder &builder, CallArgs args,
RootedValue sourceValue(cx, args[0]);
/*
* If we get passed in an object whose internal [[Class]] property is
* "RegExp", return a new object with the same source/flags.
*/
if (IsObjectWithClass(sourceValue, ESClass_RegExp, cx)) {
/*
* For RegExp.prototype.compile, if the first argument is a RegExp object,
* the second argument must be undefined. Otherwise, throw a TypeError.
*/
if (args.hasDefined(1) && creationMode == CreateForCompile) {
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_NEWREGEXP_FLAGGED);
return false;
}
/*
* Beware, sourceObj may be a (transparent) proxy to a RegExp, so only
* use generic (proxyable) operations on sourceObj that do not assume
@ -181,24 +178,32 @@ CompileRegExpObject(JSContext *cx, RegExpObjectBuilder &builder, CallArgs args,
*/
RootedObject sourceObj(cx, &sourceValue.toObject());
if (args.hasDefined(1)) {
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_NEWREGEXP_FLAGGED);
return false;
}
/*
* Extract the 'source' and the 'flags' out of sourceObj; do not reuse
* the RegExpShared since it may be from a different compartment.
*/
RootedAtom sourceAtom(cx);
RegExpFlag flags;
{
/*
* Extract the 'source' from sourceObj; do not reuse the RegExpShared
* since it may be from a different compartment.
*/
RegExpGuard g(cx);
if (!RegExpToShared(cx, sourceObj, &g))
return false;
sourceAtom = g->getSource();
flags = g->getFlags();
/*
* If args[1] is not undefined, then parse the 'flags' from args[1].
* Otherwise, extract the 'flags' from sourceObj.
*/
if (args.hasDefined(1)) {
flags = RegExpFlag(0);
RootedString flagStr(cx, ToString<CanGC>(cx, args[1]));
if (!flagStr)
return false;
if (!ParseRegExpFlags(cx, flagStr, &flags))
return false;
} else {
flags = g->getFlags();
}
}
RegExpObject *reobj = builder.build(sourceAtom, flags);
@ -224,7 +229,6 @@ CompileRegExpObject(JSContext *cx, RegExpObjectBuilder &builder, CallArgs args,
RootedString flagStr(cx, ToString<CanGC>(cx, args[1]));
if (!flagStr)
return false;
args[1].setString(flagStr);
if (!ParseRegExpFlags(cx, flagStr, &flags))
return false;
}
@ -260,7 +264,7 @@ regexp_compile_impl(JSContext *cx, CallArgs args)
{
MOZ_ASSERT(IsRegExp(args.thisv()));
RegExpObjectBuilder builder(cx, &args.thisv().toObject().as<RegExpObject>());
return CompileRegExpObject(cx, builder, args, UseRegExpStatics);
return CompileRegExpObject(cx, builder, args, UseRegExpStatics, CreateForCompile);
}
static bool
@ -291,7 +295,7 @@ regexp_construct(JSContext *cx, unsigned argc, Value *vp)
}
RegExpObjectBuilder builder(cx);
return CompileRegExpObject(cx, builder, args, UseRegExpStatics);
return CompileRegExpObject(cx, builder, args, UseRegExpStatics, CreateForConstruct);
}
bool
@ -305,7 +309,7 @@ js::regexp_construct_no_statics(JSContext *cx, unsigned argc, Value *vp)
MOZ_ASSERT(!args.isConstructing());
RegExpObjectBuilder builder(cx);
return CompileRegExpObject(cx, builder, args, DontUseRegExpStatics);
return CompileRegExpObject(cx, builder, args, DontUseRegExpStatics, CreateForConstruct);
}
MOZ_ALWAYS_INLINE bool

View File

@ -29,6 +29,9 @@ enum RegExpStaticsUpdate { UpdateRegExpStatics, DontUpdateRegExpStatics };
// Whether RegExp statics should be used to create a RegExp instance.
enum RegExpStaticsUse { UseRegExpStatics, DontUseRegExpStatics };
// This enum is used to indicate whether 'CompileRegExpObject' is called from 'regexp_compile'.
enum RegExpCreationMode { CreateForCompile, CreateForConstruct };
RegExpRunStatus
ExecuteRegExp(JSContext *cx, HandleObject regexp, HandleString string,
MatchPairs *matches, RegExpStaticsUpdate staticsUpdate);

View File

@ -1,105 +0,0 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
*
* Date: 26 November 2000
*
*
*SUMMARY: Passing a RegExp object to a RegExp() constructor.
*This test arose from Bugzilla bug 61266. The ECMA3 section is:
*
* 15.10.4.1 new RegExp(pattern, flags)
*
* If pattern is an object R whose [[Class]] property is "RegExp" and
* flags is undefined, then let P be the pattern used to construct R
* and let F be the flags used to construct R. If pattern is an object R
* whose [[Class]] property is "RegExp" and flags is not undefined,
* then throw a TypeError exception. Otherwise, let P be the empty string
* if pattern is undefined and ToString(pattern) otherwise, and let F be
* the empty string if flags is undefined and ToString(flags) otherwise.
*
*
*The current test will check the second scenario outlined above:
*
* "pattern" is itself a RegExp object R
* "flags" is NOT undefined
*
* This should throw an exception ... we test for this.
*
*/
//-------------------------------------------------------------------------------------------------
var BUGNUMBER = '61266';
var summary = 'Negative test: Passing (RegExp object, flag) to RegExp() constructor';
var statprefix = 'Passing RegExp object on pattern ';
var statsuffix = '; passing flag ';
var cnFAILURE = 'Expected an exception to be thrown, but none was -';
var singlequote = "'";
var i = -1; var j = -1; var s = ''; var f = '';
var obj1 = {}; var obj2 = {};
var patterns = new Array();
var flags = new Array();
// various regular expressions to try -
patterns[0] = '';
patterns[1] = 'abc';
patterns[2] = '(.*)(3-1)\s\w';
patterns[3] = '(.*)(...)\\s\\w';
patterns[4] = '[^A-Za-z0-9_]';
patterns[5] = '[^\f\n\r\t\v](123.5)([4 - 8]$)';
// various flags to try -
flags[0] = 'i';
flags[1] = 'g';
flags[2] = 'm';
DESCRIPTION = "Negative test: Passing (RegExp object, flag) to RegExp() constructor"
EXPECTED = "error";
//-------------------------------------------------------------------------------------------------
test();
//-------------------------------------------------------------------------------------------------
function test()
{
enterFunc ('test');
printBugNumber(BUGNUMBER);
printStatus (summary);
for (i in patterns)
{
s = patterns[i];
for (j in flags)
{
f = flags[j];
printStatus(getStatus(s, f));
obj1 = new RegExp(s, f);
obj2 = new RegExp(obj1, f); // this should cause an exception
// WE SHOULD NEVER REACH THIS POINT -
reportCompare('PASS', 'FAIL', cnFAILURE);
}
}
exitFunc ('test');
}
function getStatus(regexp, flag)
{
return (statprefix + quote(regexp) + statsuffix + flag);
}
function quote(text)
{
return (singlequote + text + singlequote);
}

View File

@ -0,0 +1,18 @@
assertEq(RegExp(/foo/my).flags, "my");
assertEq(RegExp(/foo/, "gi").flags, "gi");
assertEq(RegExp(/foo/my, "gi").flags, "gi");
assertEq(RegExp(/foo/my, "").flags, "");
assertEq(RegExp(/foo/my, undefined).flags, "my");
assertThrowsInstanceOf(() => RegExp(/foo/my, null), SyntaxError);
assertThrowsInstanceOf(() => RegExp(/foo/my, "foo"), SyntaxError);
assertEq(/a/.compile("b", "gi").flags, "gi");
assertEq(/a/.compile(/b/my).flags, "my");
assertEq(/a/.compile(/b/my, undefined).flags, "my");
assertThrowsInstanceOf(() => /a/.compile(/b/my, "gi"), TypeError);
assertThrowsInstanceOf(() => /a/.compile(/b/my, ""), TypeError);
assertThrowsInstanceOf(() => /a/.compile(/b/my, null), TypeError);
assertThrowsInstanceOf(() => /a/.compile(/b/my, "foo"), TypeError);
if (typeof reportCompare === "function")
reportCompare(true, true);