Commit 5b8c63f9 authored by erik.corry@gmail.com's avatar erik.corry@gmail.com

Avoids allocating a JSArray of capture information on each non-global

regular expression match.
Also moves all last-match information into one place where it can be
updated from C++ code (this will be used in another afsnit).
Review URL: http://codereview.chromium.org/28184

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1383 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 0864b2d6
This diff is collapsed.
...@@ -59,13 +59,15 @@ class RegExpImpl { ...@@ -59,13 +59,15 @@ class RegExpImpl {
// This function calls the garbage collector if necessary. // This function calls the garbage collector if necessary.
static Handle<Object> Exec(Handle<JSRegExp> regexp, static Handle<Object> Exec(Handle<JSRegExp> regexp,
Handle<String> subject, Handle<String> subject,
Handle<Object> index); int index,
Handle<JSArray> lastMatchInfo);
// Call RegExp.prototyp.exec(string) in a loop. // Call RegExp.prototyp.exec(string) in a loop.
// Used by String.prototype.match and String.prototype.replace. // Used by String.prototype.match and String.prototype.replace.
// This function calls the garbage collector if necessary. // This function calls the garbage collector if necessary.
static Handle<Object> ExecGlobal(Handle<JSRegExp> regexp, static Handle<Object> ExecGlobal(Handle<JSRegExp> regexp,
Handle<String> subject); Handle<String> subject,
Handle<JSArray> lastMatchInfo);
// Prepares a JSRegExp object with Irregexp-specific data. // Prepares a JSRegExp object with Irregexp-specific data.
static Handle<Object> IrregexpPrepare(Handle<JSRegExp> re, static Handle<Object> IrregexpPrepare(Handle<JSRegExp> re,
...@@ -79,18 +81,22 @@ class RegExpImpl { ...@@ -79,18 +81,22 @@ class RegExpImpl {
Handle<String> match_pattern); Handle<String> match_pattern);
static Handle<Object> AtomExec(Handle<JSRegExp> regexp, static Handle<Object> AtomExec(Handle<JSRegExp> regexp,
Handle<String> subject, Handle<String> subject,
Handle<Object> index); int index,
Handle<JSArray> lastMatchInfo);
static Handle<Object> AtomExecGlobal(Handle<JSRegExp> regexp, static Handle<Object> AtomExecGlobal(Handle<JSRegExp> regexp,
Handle<String> subject); Handle<String> subject,
Handle<JSArray> lastMatchInfo);
// Execute an Irregexp bytecode pattern. // Execute an Irregexp bytecode pattern.
static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp, static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp,
Handle<String> subject, Handle<String> subject,
Handle<Object> index); int index,
Handle<JSArray> lastMatchInfo);
static Handle<Object> IrregexpExecGlobal(Handle<JSRegExp> regexp, static Handle<Object> IrregexpExecGlobal(Handle<JSRegExp> regexp,
Handle<String> subject); Handle<String> subject,
Handle<JSArray> lastMatchInfo);
static void NewSpaceCollectionPrologue(); static void NewSpaceCollectionPrologue();
static void OldSpaceCollectionPrologue(); static void OldSpaceCollectionPrologue();
...@@ -107,6 +113,30 @@ class RegExpImpl { ...@@ -107,6 +113,30 @@ class RegExpImpl {
static const int kIrregexpCodeIndex = 3; static const int kIrregexpCodeIndex = 3;
static const int kIrregexpDataLength = 4; static const int kIrregexpDataLength = 4;
// Offsets in the lastMatchInfo array.
static const int kLastCaptureCount = 0;
static const int kLastSubject = 1;
static const int kLastInput = 2;
static const int kFirstCapture = 1;
static const int kLastMatchOverhead = 3;
static int GetCapture(FixedArray* array, int index) {
return Smi::cast(array->get(index + kFirstCapture))->value();
}
static void SetLastCaptureCount(FixedArray* array, int to) {
array->set(kLastCaptureCount, Smi::FromInt(to));
}
static void SetLastSubject(FixedArray* array, String* to) {
int capture_count = GetLastCaptureCount(array);
array->set(capture_count + kLastSubject, to);
}
static void SetLastInput(FixedArray* array, String* to) {
int capture_count = GetLastCaptureCount(array);
array->set(capture_count + kLastInput, to);
}
static void SetCapture(FixedArray* array, int index, int to) {
array->set(index + kFirstCapture, Smi::FromInt(to));
}
private: private:
static String* last_ascii_string_; static String* last_ascii_string_;
static String* two_byte_cached_string_; static String* two_byte_cached_string_;
...@@ -121,6 +151,7 @@ class RegExpImpl { ...@@ -121,6 +151,7 @@ class RegExpImpl {
// Returns an empty handle in case of an exception. // Returns an empty handle in case of an exception.
static Handle<Object> IrregexpExecOnce(Handle<FixedArray> regexp, static Handle<Object> IrregexpExecOnce(Handle<FixedArray> regexp,
int num_captures, int num_captures,
Handle<JSArray> lastMatchInfo,
Handle<String> subject16, Handle<String> subject16,
int previous_index, int previous_index,
int* ovector, int* ovector,
...@@ -134,6 +165,10 @@ class RegExpImpl { ...@@ -134,6 +165,10 @@ class RegExpImpl {
int character_position, int character_position,
int utf8_position); int utf8_position);
// Used to access the lastMatchInfo array.
static int GetLastCaptureCount(FixedArray* array) {
return Smi::cast(array->get(kLastCaptureCount))->value();
}
// A one element cache of the last utf8_subject string and its length. The // A one element cache of the last utf8_subject string and its length. The
// subject JS String object is cached in the heap. We also cache a // subject JS String object is cached in the heap. We also cache a
// translation between position and utf8 position. // translation between position and utf8 position.
......
# Copyright 2006-2008 the V8 project authors. All rights reserved. # Copyright 2006-2009 the V8 project authors. All rights reserved.
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are # modification, are permitted provided that the following conditions are
# met: # met:
...@@ -99,3 +99,22 @@ python macro CHAR_CODE(str) = ord(str[1]); ...@@ -99,3 +99,22 @@ python macro CHAR_CODE(str) = ord(str[1]);
# Accessors for original global properties that ensure they have been loaded. # Accessors for original global properties that ensure they have been loaded.
const ORIGINAL_REGEXP = (global.RegExp, $RegExp); const ORIGINAL_REGEXP = (global.RegExp, $RegExp);
const ORIGINAL_DATE = (global.Date, $Date); const ORIGINAL_DATE = (global.Date, $Date);
# Constants used on an array to implement the properties of the RegExp object.
const REGEXP_NUMBER_OF_CAPTURES = 0;
const REGEXP_FIRST_CAPTURE = 1;
# We can't put macros in macros so we use constants here.
# REGEXP_NUMBER_OF_CAPTURES
macro NUMBER_OF_CAPTURES(array) = ((array)[0]);
# Last input and last subject are after the captures so we can omit them on
# results returned from global searches. Beware - these evaluate their
# arguments twice.
macro LAST_SUBJECT(array) = ((array)[(array)[0] + 1]);
macro LAST_INPUT(array) = ((array)[(array)[0] + 2]);
# REGEXP_FIRST_CAPTURE
macro CAPTURE(index) = (1 + (index));
const CAPTURE0 = 1;
const CAPTURE1 = 2;
...@@ -4879,6 +4879,20 @@ Object* JSArray::Initialize(int capacity) { ...@@ -4879,6 +4879,20 @@ Object* JSArray::Initialize(int capacity) {
} }
void JSArray::EnsureSize(int required_size) {
ASSERT(HasFastElements());
if (elements()->length() >= required_size) return;
Handle<FixedArray> old_backing(elements());
int old_size = old_backing->length();
// Doubling in size would be overkill, but leave some slack to avoid
// constantly growing.
int new_size = required_size + (required_size >> 3);
Handle<FixedArray> new_backing = Factory::NewFixedArray(new_size);
for (int i = 0; i < old_size; i++) new_backing->set(i, old_backing->get(i));
SetContent(*new_backing);
}
// Computes the new capacity when expanding the elements of a JSObject. // Computes the new capacity when expanding the elements of a JSObject.
static int NewElementsCapacity(int old_capacity) { static int NewElementsCapacity(int old_capacity) {
// (old_capacity + 50%) + 16 // (old_capacity + 50%) + 16
......
...@@ -3779,6 +3779,10 @@ class JSArray: public JSObject { ...@@ -3779,6 +3779,10 @@ class JSArray: public JSObject {
// Casting. // Casting.
static inline JSArray* cast(Object* obj); static inline JSArray* cast(Object* obj);
// Uses handles. Ensures that the fixed array backing the JSArray has at
// least the stated size.
void EnsureSize(int minimum_size_of_backing_fixed_array);
// Dispatched behavior. // Dispatched behavior.
#ifdef DEBUG #ifdef DEBUG
void JSArrayPrint(); void JSArrayPrint();
......
// Copyright 2006-2008 the V8 project authors. All rights reserved. // Copyright 2006-2009 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are // modification, are permitted provided that the following conditions are
// met: // met:
...@@ -52,7 +52,7 @@ function DoConstructRegExp(object, pattern, flags, isConstructorCall) { ...@@ -52,7 +52,7 @@ function DoConstructRegExp(object, pattern, flags, isConstructorCall) {
var multiline = false; var multiline = false;
for (var i = 0; i < flags.length; i++) { for (var i = 0; i < flags.length; i++) {
var c = flags.charAt(i); var c = StringCharAt.call(flags, i);
switch (c) { switch (c) {
case 'g': case 'g':
// Allow duplicate flags to be consistent with JSC and others. // Allow duplicate flags to be consistent with JSC and others.
...@@ -117,15 +117,15 @@ function RegExpConstructor(pattern, flags) { ...@@ -117,15 +117,15 @@ function RegExpConstructor(pattern, flags) {
// Deprecated RegExp.prototype.compile method. We behave like the constructor // Deprecated RegExp.prototype.compile method. We behave like the constructor
// were called again. In SpiderMonkey, this method returns the regexp object. // were called again. In SpiderMonkey, this method returns the regexp object.
// In KJS, it returns undefined. For compatibility with KJS, we match their // In JSC, it returns undefined. For compatibility with JSC, we match their
// behavior. // behavior.
function CompileRegExp(pattern, flags) { function CompileRegExp(pattern, flags) {
// Both KJS and SpiderMonkey treat a missing pattern argument as the // Both JSC and SpiderMonkey treat a missing pattern argument as the
// empty subject string, and an actual undefined value passed as the // empty subject string, and an actual undefined value passed as the
// patter as the string 'undefined'. Note that KJS is inconsistent // pattern as the string 'undefined'. Note that JSC is inconsistent
// here, treating undefined values differently in // here, treating undefined values differently in
// RegExp.prototype.compile and in the constructor, where they are // RegExp.prototype.compile and in the constructor, where they are
// the empty string. For compatibility with KJS, we match their // the empty string. For compatibility with JSC, we match their
// behavior. // behavior.
if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) { if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) {
DoConstructRegExp(this, 'undefined', flags, false); DoConstructRegExp(this, 'undefined', flags, false);
...@@ -135,32 +135,20 @@ function CompileRegExp(pattern, flags) { ...@@ -135,32 +135,20 @@ function CompileRegExp(pattern, flags) {
} }
// DoRegExpExec and DoRegExpExecGlobal are wrappers around the runtime
// %RegExp and %RegExpGlobal functions that ensure that the static
// properties of the RegExp constructor are set.
function DoRegExpExec(regexp, string, index) { function DoRegExpExec(regexp, string, index) {
var matchIndices = %RegExpExec(regexp, string, index); return %RegExpExec(regexp, string, index, lastMatchInfo);
if (!IS_NULL(matchIndices)) {
regExpCaptures = matchIndices;
regExpSubject = regExpInput = string;
}
return matchIndices;
} }
function DoRegExpExecGlobal(regexp, string) { function DoRegExpExecGlobal(regexp, string) {
// Here, matchIndices is an array of arrays of substring indices. // Returns an array of arrays of substring indices.
var matchIndices = %RegExpExecGlobal(regexp, string); return %RegExpExecGlobal(regexp, string, lastMatchInfo);
if (matchIndices.length != 0) {
regExpCaptures = matchIndices[matchIndices.length - 1];
regExpSubject = regExpInput = string;
}
return matchIndices;
} }
function RegExpExec(string) { function RegExpExec(string) {
if (%_ArgumentsLength() == 0) { if (%_ArgumentsLength() == 0) {
var regExpInput = LAST_INPUT(lastMatchInfo);
if (IS_UNDEFINED(regExpInput)) { if (IS_UNDEFINED(regExpInput)) {
throw MakeError('no_input_to_regexp', [this]); throw MakeError('no_input_to_regexp', [this]);
} }
...@@ -177,23 +165,21 @@ function RegExpExec(string) { ...@@ -177,23 +165,21 @@ function RegExpExec(string) {
} }
%_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]);
// matchIndices is an array of integers with length of captures*2, // matchIndices is either null or the lastMatchInfo array.
// each pair of integers specified the start and the end of index var matchIndices = %RegExpExec(this, s, i, lastMatchInfo);
// in the string.
var matchIndices = DoRegExpExec(this, s, i);
if (matchIndices == null) { if (matchIndices == null) {
if (this.global) this.lastIndex = 0; if (this.global) this.lastIndex = 0;
return matchIndices; // no match return matchIndices; // no match
} }
var numResults = matchIndices.length >> 1; var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1;
var result = new $Array(numResults); var result = new $Array(numResults);
for (var i = 0; i < numResults; i++) { for (var i = 0; i < numResults; i++) {
var matchStart = matchIndices[2*i]; var matchStart = lastMatchInfo[CAPTURE(i << 1)];
var matchEnd = matchIndices[2*i + 1]; var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)];
if (matchStart != -1 && matchEnd != -1) { if (matchStart != -1 && matchEnd != -1) {
result[i] = s.slice(matchStart, matchEnd); result[i] = SubString(s, matchStart, matchEnd);
} else { } else {
// Make sure the element is present. Avoid reading the undefined // Make sure the element is present. Avoid reading the undefined
// property from the global object since this may change. // property from the global object since this may change.
...@@ -202,13 +188,15 @@ function RegExpExec(string) { ...@@ -202,13 +188,15 @@ function RegExpExec(string) {
} }
if (this.global) if (this.global)
this.lastIndex = matchIndices[1]; this.lastIndex = lastMatchInfo[CAPTURE1];
result.index = matchIndices[0]; result.index = lastMatchInfo[CAPTURE0];
result.input = s; result.input = s;
return result; return result;
} }
// Section 15.10.6.3 doesn't actually make sense, but the intention seems to be
// that test is defined in terms of String.prototype.exec even if it changes.
function RegExpTest(string) { function RegExpTest(string) {
var result = (%_ArgumentsLength() == 0) ? this.exec() : this.exec(string); var result = (%_ArgumentsLength() == 0) ? this.exec() : this.exec(string);
return result != null; return result != null;
...@@ -236,56 +224,69 @@ function RegExpToString() { ...@@ -236,56 +224,69 @@ function RegExpToString() {
// on the captures array of the last successful match and the subject string // on the captures array of the last successful match and the subject string
// of the last successful match. // of the last successful match.
function RegExpGetLastMatch() { function RegExpGetLastMatch() {
return regExpSubject.slice(regExpCaptures[0], regExpCaptures[1]); var regExpSubject = LAST_SUBJECT(lastMatchInfo);
return SubString(regExpSubject,
lastMatchInfo[CAPTURE0],
lastMatchInfo[CAPTURE1]);
} }
function RegExpGetLastParen() { function RegExpGetLastParen() {
var length = regExpCaptures.length; var length = NUMBER_OF_CAPTURES(lastMatchInfo);
if (length <= 2) return ''; // There were no captures. if (length <= 2) return ''; // There were no captures.
// We match the SpiderMonkey behavior: return the substring defined by the // We match the SpiderMonkey behavior: return the substring defined by the
// last pair (after the first pair) of elements of the capture array even if // last pair (after the first pair) of elements of the capture array even if
// it is empty. // it is empty.
return regExpSubject.slice(regExpCaptures[length - 2], var regExpSubject = LAST_SUBJECT(lastMatchInfo);
regExpCaptures[length - 1]); return SubString(regExpSubject,
lastMatchInfo[CAPTURE(length - 2)],
lastMatchInfo[CAPTURE(length - 1)]);
} }
function RegExpGetLeftContext() { function RegExpGetLeftContext() {
return regExpSubject.slice(0, regExpCaptures[0]); return SubString(LAST_SUBJECT(lastMatchInfo),
0,
lastMatchInfo[CAPTURE0]);
} }
function RegExpGetRightContext() { function RegExpGetRightContext() {
return regExpSubject.slice(regExpCaptures[1], regExpSubject.length); var subject = LAST_SUBJECT(lastMatchInfo);
return SubString(subject,
lastMatchInfo[CAPTURE1],
subject.length);
} }
// The properties $1..$9 are the first nine capturing substrings of the last // The properties $1..$9 are the first nine capturing substrings of the last
// successful match, or ''. The function RegExpMakeCaptureGetter will be // successful match, or ''. The function RegExpMakeCaptureGetter will be
// called with an index greater than or equal to 1 but it actually works for // called with indeces from 1 to 9.
// any non-negative index.
function RegExpMakeCaptureGetter(n) { function RegExpMakeCaptureGetter(n) {
return function() { return function() {
var index = n * 2; var index = n * 2;
if (index >= regExpCaptures.length) return ''; if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return '';
var matchStart = regExpCaptures[index]; var matchStart = lastMatchInfo[CAPTURE(index)];
var matchEnd = regExpCaptures[index + 1]; var matchEnd = lastMatchInfo[CAPTURE(index + 1)];
if (matchStart == -1 || matchEnd == -1) return ''; if (matchStart == -1 || matchEnd == -1) return '';
return regExpSubject.slice(matchStart, matchEnd); return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd);
}; };
} }
// Properties of the builtins object for recording the result of the last // Property of the builtins object for recording the result of the last
// regexp match. The property regExpCaptures is the matchIndices array of the // regexp match. The property lastMatchInfo includes the matchIndices
// last successful regexp match (an array of start/end index pairs for the // array of the last successful regexp match (an array of start/end index
// match and all the captured substrings), the invariant is that there is at // pairs for the match and all the captured substrings), the invariant is
// least two elements. The property regExpSubject is the subject string for // that there are at least two capture indeces. The array also contains
// the last successful match. // the subject string for the last successful match.
var regExpCaptures = [0, 0]; var lastMatchInfo = [
var regExpSubject = ''; 2, // REGEXP_NUMBER_OF_CAPTURES
var regExpInput; 0, // REGEXP_FIRST_CAPTURE + 0
0, // REGEXP_FIRST_CAPTURE + 1
"", // Last subject.
void 0, // Last input - settable with RegExpSetInput.
];
// ------------------------------------------------------------------- // -------------------------------------------------------------------
...@@ -303,19 +304,22 @@ function SetupRegExp() { ...@@ -303,19 +304,22 @@ function SetupRegExp() {
)); ));
// The spec says nothing about the length of exec and test, but // The spec says nothing about the length of exec and test, but
// SpiderMonkey and KJS have length equal to 0. // SpiderMonkey and JSC have length equal to 0.
%FunctionSetLength($RegExp.prototype.exec, 0); %FunctionSetLength($RegExp.prototype.exec, 0);
%FunctionSetLength($RegExp.prototype.test, 0); %FunctionSetLength($RegExp.prototype.test, 0);
// The length of compile is 1 in SpiderMonkey. // The length of compile is 1 in SpiderMonkey.
%FunctionSetLength($RegExp.prototype.compile, 1); %FunctionSetLength($RegExp.prototype.compile, 1);
// The properties input, $input, and $_ are aliases for each other. When this // The properties input, $input, and $_ are aliases for each other. When this
// value is set the value it is set to is coerced to a string. // value is set the value it is set to is coerced to a string.
// Getter and setter for the input. // Getter and setter for the input.
function RegExpGetInput() { function RegExpGetInput() {
var regExpInput = LAST_INPUT(lastMatchInfo);
return IS_UNDEFINED(regExpInput) ? "" : regExpInput; return IS_UNDEFINED(regExpInput) ? "" : regExpInput;
} }
function RegExpSetInput(string) { regExpInput = ToString(string); } function RegExpSetInput(string) {
lastMatchInfo[lastMatchInfo[REGEXP_NUMBER_OF_CAPTURES] + 2] = string;
};
%DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE); %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE);
%DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE); %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE);
......
...@@ -858,14 +858,21 @@ static Object* Runtime_InitializeConstContextSlot(Arguments args) { ...@@ -858,14 +858,21 @@ static Object* Runtime_InitializeConstContextSlot(Arguments args) {
static Object* Runtime_RegExpExec(Arguments args) { static Object* Runtime_RegExpExec(Arguments args) {
HandleScope scope; HandleScope scope;
ASSERT(args.length() == 3); ASSERT(args.length() == 4);
CONVERT_CHECKED(JSRegExp, raw_regexp, args[0]); CONVERT_CHECKED(JSRegExp, raw_regexp, args[0]);
Handle<JSRegExp> regexp(raw_regexp); Handle<JSRegExp> regexp(raw_regexp);
CONVERT_CHECKED(String, raw_subject, args[1]); CONVERT_CHECKED(String, raw_subject, args[1]);
Handle<String> subject(raw_subject); Handle<String> subject(raw_subject);
Handle<Object> index(args[2]); // Due to the way the JS files are constructed this must be less than the
ASSERT(index->IsNumber()); // length of a string, i.e. it is always a Smi. We check anyway for security.
Handle<Object> result = RegExpImpl::Exec(regexp, subject, index); CONVERT_CHECKED(Smi, index, args[2]);
CONVERT_CHECKED(JSArray, raw_last_match_info, args[3]);
Handle<JSArray> last_match_info(raw_last_match_info);
CHECK(last_match_info->HasFastElements());
Handle<Object> result = RegExpImpl::Exec(regexp,
subject,
index->value(),
last_match_info);
if (result.is_null()) return Failure::Exception(); if (result.is_null()) return Failure::Exception();
return *result; return *result;
} }
...@@ -873,12 +880,16 @@ static Object* Runtime_RegExpExec(Arguments args) { ...@@ -873,12 +880,16 @@ static Object* Runtime_RegExpExec(Arguments args) {
static Object* Runtime_RegExpExecGlobal(Arguments args) { static Object* Runtime_RegExpExecGlobal(Arguments args) {
HandleScope scope; HandleScope scope;
ASSERT(args.length() == 2); ASSERT(args.length() == 3);
CONVERT_CHECKED(JSRegExp, raw_regexp, args[0]); CONVERT_CHECKED(JSRegExp, raw_regexp, args[0]);
Handle<JSRegExp> regexp(raw_regexp); Handle<JSRegExp> regexp(raw_regexp);
CONVERT_CHECKED(String, raw_subject, args[1]); CONVERT_CHECKED(String, raw_subject, args[1]);
Handle<String> subject(raw_subject); Handle<String> subject(raw_subject);
Handle<Object> result = RegExpImpl::ExecGlobal(regexp, subject); CONVERT_CHECKED(JSArray, raw_last_match_info, args[2]);
Handle<JSArray> last_match_info(raw_last_match_info);
CHECK(last_match_info->HasFastElements());
Handle<Object> result =
RegExpImpl::ExecGlobal(regexp, subject, last_match_info);
if (result.is_null()) return Failure::Exception(); if (result.is_null()) return Failure::Exception();
return *result; return *result;
} }
......
...@@ -137,8 +137,8 @@ namespace v8 { namespace internal { ...@@ -137,8 +137,8 @@ namespace v8 { namespace internal {
\ \
/* Regular expressions */ \ /* Regular expressions */ \
F(RegExpCompile, 3) \ F(RegExpCompile, 3) \
F(RegExpExec, 3) \ F(RegExpExec, 4) \
F(RegExpExecGlobal, 2) \ F(RegExpExecGlobal, 3) \
\ \
/* Strings */ \ /* Strings */ \
F(StringCharCodeAt, 2) \ F(StringCharCodeAt, 2) \
......
This diff is collapsed.
// Regexp shouldn't use String.prototype.slice()
var s = new String("foo");
assertEquals("f", s.slice(0,1));
String.prototype.slice = function() { return "x"; }
assertEquals("x", s.slice(0,1));
assertEquals("g", /g/.exec("gg"));
// Regexp shouldn't use String.prototype.charAt()
var f1 = new RegExp("f", "i");
assertEquals("F", f1.exec("F"));
assertEquals("f", "foo".charAt(0));
String.prototype.charAt = function(idx) { return 'g'; };
assertEquals("g", "foo".charAt(0));
var f2 = new RegExp("[g]", "i");
assertEquals("G", f2.exec("G"));
assertTrue(f2.ignoreCase);
// On the other hand test is defined in a semi-coherent way as a call to exec.
// 15.10.6.3
// SpiderMonkey fails this one.
RegExp.prototype.exec = function(string) { return 'x'; }
assertTrue(/f/.test('x'));
...@@ -104,7 +104,7 @@ def ExpandConstants(lines, constants): ...@@ -104,7 +104,7 @@ def ExpandConstants(lines, constants):
def ExpandMacros(lines, macros): def ExpandMacros(lines, macros):
for name, macro in macros.items(): for name, macro in macros.items():
start = lines.find(name, 0) start = lines.find(name + '(', 0)
while start != -1: while start != -1:
# Scan over the arguments # Scan over the arguments
assert lines[start + len(name)] == '(' assert lines[start + len(name)] == '('
...@@ -132,7 +132,7 @@ def ExpandMacros(lines, macros): ...@@ -132,7 +132,7 @@ def ExpandMacros(lines, macros):
result = macro.expand(mapping) result = macro.expand(mapping)
# Replace the occurrence of the macro with the expansion # Replace the occurrence of the macro with the expansion
lines = lines[:start] + result + lines[end:] lines = lines[:start] + result + lines[end:]
start = lines.find(name, end) start = lines.find(name + '(', end)
return lines return lines
class TextMacro: class TextMacro:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment