Bug 974350 part 2 - Cleanup CreateRegExpMatchResult. r=h4writer

--HG--
extra : rebase_source : 69492994f03253cd88a5f2b30a13170129b6c1ef
This commit is contained in:
Jan de Mooij 2014-02-19 17:37:24 +01:00
parent f35b41773e
commit 96e9365432
3 changed files with 19 additions and 33 deletions

View File

@ -19,10 +19,10 @@ using namespace js::types;
using mozilla::ArrayLength;
bool
js::CreateRegExpMatchResult(JSContext *cx, HandleString input_, const jschar *chars, size_t length,
MatchPairs &matches, MutableHandleValue rval)
js::CreateRegExpMatchResult(JSContext *cx, HandleString input, const MatchPairs &matches,
MutableHandleValue rval)
{
RootedString input(cx, input_);
JS_ASSERT(input);
/*
* Create the (slow) result array for a match.
@ -33,11 +33,6 @@ js::CreateRegExpMatchResult(JSContext *cx, HandleString input_, const jschar *ch
* input: input string
* index: start index for the match
*/
if (!input) {
input = js_NewStringCopyN<CanGC>(cx, chars, length);
if (!input)
return false;
}
/* Get the templateObject that defines the shape and type of the output object */
JSObject *templateObject = cx->compartment()->regExps.getOrCreateMatchResultTemplateObject(cx);
@ -69,39 +64,27 @@ js::CreateRegExpMatchResult(JSContext *cx, HandleString input_, const jschar *ch
}
/* Set the |index| property. (TemplateObject positions it in slot 0) */
RootedValue index(cx, Int32Value(matches[0].start));
arr->nativeSetSlot(0, index);
arr->nativeSetSlot(0, Int32Value(matches[0].start));
/* Set the |input| property. (TemplateObject positions it in slot 1) */
RootedValue inputVal(cx, StringValue(input));
arr->nativeSetSlot(1, inputVal);
arr->nativeSetSlot(1, StringValue(input));
#ifdef DEBUG
RootedValue test(cx);
RootedId id(cx, NameToId(cx->names().index));
if (!baseops::GetProperty(cx, arr, id, &test))
return false;
JS_ASSERT(test == index);
JS_ASSERT(test == arr->nativeGetSlot(0));
id = NameToId(cx->names().input);
if (!baseops::GetProperty(cx, arr, id, &test))
return false;
JS_ASSERT(test == inputVal);
JS_ASSERT(test == arr->nativeGetSlot(1));
#endif
rval.setObject(*arr);
return true;
}
bool
js::CreateRegExpMatchResult(JSContext *cx, HandleString string, MatchPairs &matches,
MutableHandleValue rval)
{
Rooted<JSLinearString*> input(cx, string->ensureLinear(cx));
if (!input)
return false;
return CreateRegExpMatchResult(cx, input, input->chars(), input->length(), matches, rval);
}
static RegExpRunStatus
ExecuteRegExpImpl(JSContext *cx, RegExpStatics *res, RegExpShared &re,
Handle<JSLinearString*> input, const jschar *chars, size_t length,
@ -129,7 +112,7 @@ ExecuteRegExpImpl(JSContext *cx, RegExpStatics *res, RegExpShared &re,
/* Legacy ExecuteRegExp behavior is baked into the JSAPI. */
bool
js::ExecuteRegExpLegacy(JSContext *cx, RegExpStatics *res, RegExpObject &reobj,
Handle<JSLinearString*> input, const jschar *chars, size_t length,
Handle<JSLinearString*> input_, const jschar *chars, size_t length,
size_t *lastIndex, bool test, MutableHandleValue rval)
{
RegExpGuard shared(cx);
@ -140,7 +123,7 @@ js::ExecuteRegExpLegacy(JSContext *cx, RegExpStatics *res, RegExpObject &reobj,
MatchConduit conduit(&matches);
RegExpRunStatus status =
ExecuteRegExpImpl(cx, res, *shared, input, chars, length, lastIndex, conduit);
ExecuteRegExpImpl(cx, res, *shared, input_, chars, length, lastIndex, conduit);
if (status == RegExpRunStatus_Error)
return false;
@ -157,7 +140,14 @@ js::ExecuteRegExpLegacy(JSContext *cx, RegExpStatics *res, RegExpObject &reobj,
return true;
}
return CreateRegExpMatchResult(cx, input, chars, length, matches, rval);
RootedString input(cx, input_);
if (!input) {
input = js_NewStringCopyN<CanGC>(cx, chars, length);
if (!input)
return false;
}
return CreateRegExpMatchResult(cx, input, matches, rval);
}
/* Note: returns the original if no escaping need be performed. */

View File

@ -41,13 +41,9 @@ ExecuteRegExpLegacy(JSContext *cx, RegExpStatics *res, RegExpObject &reobj,
/* Translation from MatchPairs to a JS array in regexp_exec()'s output format. */
bool
CreateRegExpMatchResult(JSContext *cx, HandleString string, MatchPairs &matches,
CreateRegExpMatchResult(JSContext *cx, HandleString input, const MatchPairs &matches,
MutableHandleValue rval);
bool
CreateRegExpMatchResult(JSContext *cx, HandleString input, const jschar *chars, size_t length,
MatchPairs &matches, MutableHandleValue rval);
extern bool
regexp_exec_raw(JSContext *cx, HandleObject regexp, HandleString input, Value *vp);

View File

@ -1863,7 +1863,7 @@ DoMatchLocal(JSContext *cx, CallArgs args, RegExpStatics *res, Handle<JSLinearSt
res->updateFromMatchPairs(cx, input, matches);
RootedValue rval(cx);
if (!CreateRegExpMatchResult(cx, input, chars, charsLen, matches, &rval))
if (!CreateRegExpMatchResult(cx, input, matches, &rval))
return false;
args.rval().set(rval);