* Reapply revisions 1383, 1384, 1391, 1398, 1401, 1402,
1418, and 1419 from bleeding_edge, reverted in 1429. * Fix of $1 accessor on sliced strings. * Fix of lastParen method when last parenthesis did not match. Review URL: http://codereview.chromium.org/43075 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1491 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
3c41aab15d
commit
912c8eb03a
@ -851,12 +851,13 @@ Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Factory::SetRegExpData(Handle<JSRegExp> regexp,
|
void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
|
||||||
JSRegExp::Type type,
|
JSRegExp::Type type,
|
||||||
Handle<String> source,
|
Handle<String> source,
|
||||||
JSRegExp::Flags flags,
|
JSRegExp::Flags flags,
|
||||||
Handle<Object> data) {
|
Handle<Object> data) {
|
||||||
Handle<FixedArray> store = NewFixedArray(JSRegExp::kDataSize);
|
Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
|
||||||
|
|
||||||
store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
|
store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
|
||||||
store->set(JSRegExp::kSourceIndex, *source);
|
store->set(JSRegExp::kSourceIndex, *source);
|
||||||
store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
|
store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
|
||||||
@ -864,6 +865,25 @@ void Factory::SetRegExpData(Handle<JSRegExp> regexp,
|
|||||||
regexp->set_data(*store);
|
regexp->set_data(*store);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
|
||||||
|
JSRegExp::Type type,
|
||||||
|
Handle<String> source,
|
||||||
|
JSRegExp::Flags flags,
|
||||||
|
int capture_count) {
|
||||||
|
Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
|
||||||
|
|
||||||
|
store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
|
||||||
|
store->set(JSRegExp::kSourceIndex, *source);
|
||||||
|
store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
|
||||||
|
store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
|
||||||
|
store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
|
||||||
|
store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
|
||||||
|
store->set(JSRegExp::kIrregexpCaptureCountIndex,
|
||||||
|
Smi::FromInt(capture_count));
|
||||||
|
regexp->set_data(*store);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
|
void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
|
||||||
Handle<JSObject> instance,
|
Handle<JSObject> instance,
|
||||||
|
@ -317,12 +317,20 @@ class Factory : public AllStatic {
|
|||||||
Handle<FixedArray> keys);
|
Handle<FixedArray> keys);
|
||||||
|
|
||||||
// Creates a new FixedArray that holds the data associated with the
|
// Creates a new FixedArray that holds the data associated with the
|
||||||
// regexp and stores it in the regexp.
|
// atom regexp and stores it in the regexp.
|
||||||
static void SetRegExpData(Handle<JSRegExp> regexp,
|
static void SetRegExpAtomData(Handle<JSRegExp> regexp,
|
||||||
JSRegExp::Type type,
|
JSRegExp::Type type,
|
||||||
Handle<String> source,
|
Handle<String> source,
|
||||||
JSRegExp::Flags flags,
|
JSRegExp::Flags flags,
|
||||||
Handle<Object> data);
|
Handle<Object> match_pattern);
|
||||||
|
|
||||||
|
// Creates a new FixedArray that holds the data associated with the
|
||||||
|
// irregexp regexp and stores it in the regexp.
|
||||||
|
static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
|
||||||
|
JSRegExp::Type type,
|
||||||
|
Handle<String> source,
|
||||||
|
JSRegExp::Flags flags,
|
||||||
|
int capture_count);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
|
static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
|
||||||
|
393
src/jsregexp.cc
393
src/jsregexp.cc
@ -213,8 +213,8 @@ Handle<Object> RegExpImpl::Compile(Handle<JSRegExp> re,
|
|||||||
Handle<Object> result;
|
Handle<Object> result;
|
||||||
if (in_cache) {
|
if (in_cache) {
|
||||||
re->set_data(*cached);
|
re->set_data(*cached);
|
||||||
result = re;
|
return re;
|
||||||
} else {
|
}
|
||||||
FlattenString(pattern);
|
FlattenString(pattern);
|
||||||
ZoneScope zone_scope(DELETE_ON_EXIT);
|
ZoneScope zone_scope(DELETE_ON_EXIT);
|
||||||
RegExpCompileData parse_result;
|
RegExpCompileData parse_result;
|
||||||
@ -230,38 +230,37 @@ Handle<Object> RegExpImpl::Compile(Handle<JSRegExp> re,
|
|||||||
|
|
||||||
if (parse_result.simple && !flags.is_ignore_case()) {
|
if (parse_result.simple && !flags.is_ignore_case()) {
|
||||||
// Parse-tree is a single atom that is equal to the pattern.
|
// Parse-tree is a single atom that is equal to the pattern.
|
||||||
result = AtomCompile(re, pattern, flags, pattern);
|
AtomCompile(re, pattern, flags, pattern);
|
||||||
} else if (parse_result.tree->IsAtom() &&
|
} else if (parse_result.tree->IsAtom() &&
|
||||||
!flags.is_ignore_case() &&
|
!flags.is_ignore_case() &&
|
||||||
parse_result.capture_count == 0) {
|
parse_result.capture_count == 0) {
|
||||||
RegExpAtom* atom = parse_result.tree->AsAtom();
|
RegExpAtom* atom = parse_result.tree->AsAtom();
|
||||||
Vector<const uc16> atom_pattern = atom->data();
|
Vector<const uc16> atom_pattern = atom->data();
|
||||||
Handle<String> atom_string = Factory::NewStringFromTwoByte(atom_pattern);
|
Handle<String> atom_string = Factory::NewStringFromTwoByte(atom_pattern);
|
||||||
result = AtomCompile(re, pattern, flags, atom_string);
|
AtomCompile(re, pattern, flags, atom_string);
|
||||||
} else {
|
} else {
|
||||||
result = IrregexpPrepare(re, pattern, flags);
|
IrregexpPrepare(re, pattern, flags, parse_result.capture_count);
|
||||||
}
|
}
|
||||||
Object* data = re->data();
|
ASSERT(re->data()->IsFixedArray());
|
||||||
if (data->IsFixedArray()) {
|
// Compilation succeeded so the data is set on the regexp
|
||||||
// If compilation succeeded then the data is set on the regexp
|
|
||||||
// and we can store it in the cache.
|
// and we can store it in the cache.
|
||||||
Handle<FixedArray> data(FixedArray::cast(re->data()));
|
Handle<FixedArray> data(FixedArray::cast(re->data()));
|
||||||
CompilationCache::PutRegExp(pattern, flags, data);
|
CompilationCache::PutRegExp(pattern, flags, data);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return re;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<Object> RegExpImpl::Exec(Handle<JSRegExp> regexp,
|
Handle<Object> RegExpImpl::Exec(Handle<JSRegExp> regexp,
|
||||||
Handle<String> subject,
|
Handle<String> subject,
|
||||||
Handle<Object> index) {
|
int index,
|
||||||
|
Handle<JSArray> last_match_info) {
|
||||||
switch (regexp->TypeTag()) {
|
switch (regexp->TypeTag()) {
|
||||||
case JSRegExp::ATOM:
|
case JSRegExp::ATOM:
|
||||||
return AtomExec(regexp, subject, index);
|
return AtomExec(regexp, subject, index, last_match_info);
|
||||||
case JSRegExp::IRREGEXP: {
|
case JSRegExp::IRREGEXP: {
|
||||||
Handle<Object> result = IrregexpExec(regexp, subject, index);
|
Handle<Object> result =
|
||||||
|
IrregexpExec(regexp, subject, index, last_match_info);
|
||||||
ASSERT(!result.is_null() || Top::has_pending_exception());
|
ASSERT(!result.is_null() || Top::has_pending_exception());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -273,12 +272,14 @@ Handle<Object> RegExpImpl::Exec(Handle<JSRegExp> regexp,
|
|||||||
|
|
||||||
|
|
||||||
Handle<Object> RegExpImpl::ExecGlobal(Handle<JSRegExp> regexp,
|
Handle<Object> RegExpImpl::ExecGlobal(Handle<JSRegExp> regexp,
|
||||||
Handle<String> subject) {
|
Handle<String> subject,
|
||||||
|
Handle<JSArray> last_match_info) {
|
||||||
switch (regexp->TypeTag()) {
|
switch (regexp->TypeTag()) {
|
||||||
case JSRegExp::ATOM:
|
case JSRegExp::ATOM:
|
||||||
return AtomExecGlobal(regexp, subject);
|
return AtomExecGlobal(regexp, subject, last_match_info);
|
||||||
case JSRegExp::IRREGEXP: {
|
case JSRegExp::IRREGEXP: {
|
||||||
Handle<Object> result = IrregexpExecGlobal(regexp, subject);
|
Handle<Object> result =
|
||||||
|
IrregexpExecGlobal(regexp, subject, last_match_info);
|
||||||
ASSERT(!result.is_null() || Top::has_pending_exception());
|
ASSERT(!result.is_null() || Top::has_pending_exception());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -292,60 +293,95 @@ Handle<Object> RegExpImpl::ExecGlobal(Handle<JSRegExp> regexp,
|
|||||||
// RegExp Atom implementation: Simple string search using indexOf.
|
// RegExp Atom implementation: Simple string search using indexOf.
|
||||||
|
|
||||||
|
|
||||||
Handle<Object> RegExpImpl::AtomCompile(Handle<JSRegExp> re,
|
void RegExpImpl::AtomCompile(Handle<JSRegExp> re,
|
||||||
Handle<String> pattern,
|
Handle<String> pattern,
|
||||||
JSRegExp::Flags flags,
|
JSRegExp::Flags flags,
|
||||||
Handle<String> match_pattern) {
|
Handle<String> match_pattern) {
|
||||||
Factory::SetRegExpData(re, JSRegExp::ATOM, pattern, flags, match_pattern);
|
Factory::SetRegExpAtomData(re,
|
||||||
return re;
|
JSRegExp::ATOM,
|
||||||
|
pattern,
|
||||||
|
flags,
|
||||||
|
match_pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void SetAtomLastCapture(FixedArray* array,
|
||||||
|
String* subject,
|
||||||
|
int from,
|
||||||
|
int to) {
|
||||||
|
NoHandleAllocation no_handles;
|
||||||
|
RegExpImpl::SetLastCaptureCount(array, 2);
|
||||||
|
RegExpImpl::SetLastSubject(array, subject);
|
||||||
|
RegExpImpl::SetLastInput(array, subject);
|
||||||
|
RegExpImpl::SetCapture(array, 0, from);
|
||||||
|
RegExpImpl::SetCapture(array, 1, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<Object> RegExpImpl::AtomExec(Handle<JSRegExp> re,
|
Handle<Object> RegExpImpl::AtomExec(Handle<JSRegExp> re,
|
||||||
Handle<String> subject,
|
Handle<String> subject,
|
||||||
Handle<Object> index) {
|
int index,
|
||||||
|
Handle<JSArray> last_match_info) {
|
||||||
Handle<String> needle(String::cast(re->DataAt(JSRegExp::kAtomPatternIndex)));
|
Handle<String> needle(String::cast(re->DataAt(JSRegExp::kAtomPatternIndex)));
|
||||||
|
|
||||||
uint32_t start_index;
|
uint32_t start_index = index;
|
||||||
if (!Array::IndexFromObject(*index, &start_index)) {
|
|
||||||
return Handle<Smi>(Smi::FromInt(-1));
|
|
||||||
}
|
|
||||||
|
|
||||||
int value = Runtime::StringMatch(subject, needle, start_index);
|
int value = Runtime::StringMatch(subject, needle, start_index);
|
||||||
if (value == -1) return Factory::null_value();
|
if (value == -1) return Factory::null_value();
|
||||||
|
ASSERT(last_match_info->HasFastElements());
|
||||||
|
|
||||||
Handle<FixedArray> array = Factory::NewFixedArray(2);
|
{
|
||||||
array->set(0, Smi::FromInt(value));
|
NoHandleAllocation no_handles;
|
||||||
array->set(1, Smi::FromInt(value + needle->length()));
|
FixedArray* array = last_match_info->elements();
|
||||||
return Factory::NewJSArrayWithElements(array);
|
SetAtomLastCapture(array, *subject, value, value + needle->length());
|
||||||
|
}
|
||||||
|
return last_match_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<Object> RegExpImpl::AtomExecGlobal(Handle<JSRegExp> re,
|
Handle<Object> RegExpImpl::AtomExecGlobal(Handle<JSRegExp> re,
|
||||||
Handle<String> subject) {
|
Handle<String> subject,
|
||||||
|
Handle<JSArray> last_match_info) {
|
||||||
Handle<String> needle(String::cast(re->DataAt(JSRegExp::kAtomPatternIndex)));
|
Handle<String> needle(String::cast(re->DataAt(JSRegExp::kAtomPatternIndex)));
|
||||||
|
ASSERT(last_match_info->HasFastElements());
|
||||||
Handle<JSArray> result = Factory::NewJSArray(1);
|
Handle<JSArray> result = Factory::NewJSArray(1);
|
||||||
int index = 0;
|
int index = 0;
|
||||||
int match_count = 0;
|
int match_count = 0;
|
||||||
int subject_length = subject->length();
|
int subject_length = subject->length();
|
||||||
int needle_length = needle->length();
|
int needle_length = needle->length();
|
||||||
|
int last_value = -1;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
HandleScope scope;
|
||||||
int value = -1;
|
int value = -1;
|
||||||
if (index + needle_length <= subject_length) {
|
if (index + needle_length <= subject_length) {
|
||||||
value = Runtime::StringMatch(subject, needle, index);
|
value = Runtime::StringMatch(subject, needle, index);
|
||||||
}
|
}
|
||||||
if (value == -1) break;
|
if (value == -1) {
|
||||||
HandleScope scope;
|
if (last_value != -1) {
|
||||||
|
Handle<FixedArray> array(last_match_info->elements());
|
||||||
|
SetAtomLastCapture(*array,
|
||||||
|
*subject,
|
||||||
|
last_value,
|
||||||
|
last_value + needle->length());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
int end = value + needle_length;
|
int end = value + needle_length;
|
||||||
|
|
||||||
Handle<FixedArray> array = Factory::NewFixedArray(2);
|
// Create an array that looks like the static last_match_info array
|
||||||
array->set(0, Smi::FromInt(value));
|
// that is attached to the global RegExp object. We will be returning
|
||||||
array->set(1, Smi::FromInt(end));
|
// an array of these.
|
||||||
|
Handle<FixedArray> array = Factory::NewFixedArray(kFirstCapture + 2);
|
||||||
|
SetCapture(*array, 0, value);
|
||||||
|
SetCapture(*array, 1, end);
|
||||||
|
SetLastCaptureCount(*array, 2);
|
||||||
Handle<JSArray> pair = Factory::NewJSArrayWithElements(array);
|
Handle<JSArray> pair = Factory::NewJSArrayWithElements(array);
|
||||||
SetElement(result, match_count, pair);
|
SetElement(result, match_count, pair);
|
||||||
match_count++;
|
match_count++;
|
||||||
index = end;
|
index = end;
|
||||||
if (needle_length == 0) index++;
|
if (needle_length == 0) index++;
|
||||||
|
last_value = value;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -354,23 +390,29 @@ Handle<Object> RegExpImpl::AtomExecGlobal(Handle<JSRegExp> re,
|
|||||||
// Irregexp implementation.
|
// Irregexp implementation.
|
||||||
|
|
||||||
|
|
||||||
// Retrieves a compiled version of the regexp for either ASCII or non-ASCII
|
// Ensures that the regexp object contains a compiled version of the
|
||||||
// strings. If the compiled version doesn't already exist, it is compiled
|
// source for either ASCII or non-ASCII strings.
|
||||||
|
// If the compiled version doesn't already exist, it is compiled
|
||||||
// from the source pattern.
|
// from the source pattern.
|
||||||
// Irregexp is not feature complete yet. If there is something in the
|
// If compilation fails, an exception is thrown and this function
|
||||||
// regexp that the compiler cannot currently handle, an empty
|
// returns false.
|
||||||
// handle is returned, but no exception is thrown.
|
bool RegExpImpl::EnsureCompiledIrregexp(Handle<JSRegExp> re,
|
||||||
static Handle<FixedArray> GetCompiledIrregexp(Handle<JSRegExp> re,
|
|
||||||
bool is_ascii) {
|
bool is_ascii) {
|
||||||
ASSERT(re->DataAt(JSRegExp::kIrregexpDataIndex)->IsFixedArray());
|
int index;
|
||||||
Handle<FixedArray> alternatives(
|
if (is_ascii) {
|
||||||
FixedArray::cast(re->DataAt(JSRegExp::kIrregexpDataIndex)));
|
index = JSRegExp::kIrregexpASCIICodeIndex;
|
||||||
ASSERT_EQ(2, alternatives->length());
|
} else {
|
||||||
|
index = JSRegExp::kIrregexpUC16CodeIndex;
|
||||||
int index = is_ascii ? 0 : 1;
|
}
|
||||||
Object* entry = alternatives->get(index);
|
Object* entry = re->DataAt(index);
|
||||||
if (!entry->IsNull()) {
|
if (!entry->IsTheHole()) {
|
||||||
return Handle<FixedArray>(FixedArray::cast(entry));
|
// A value has already been compiled.
|
||||||
|
if (entry->IsJSObject()) {
|
||||||
|
// If it's a JS value, it's an error.
|
||||||
|
Top::Throw(entry);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compile the RegExp.
|
// Compile the RegExp.
|
||||||
@ -392,77 +434,115 @@ static Handle<FixedArray> GetCompiledIrregexp(Handle<JSRegExp> re,
|
|||||||
pattern,
|
pattern,
|
||||||
compile_data.error,
|
compile_data.error,
|
||||||
"malformed_regexp");
|
"malformed_regexp");
|
||||||
return Handle<FixedArray>::null();
|
return false;
|
||||||
}
|
}
|
||||||
Handle<FixedArray> compiled_entry =
|
RegExpEngine::CompilationResult result =
|
||||||
RegExpEngine::Compile(&compile_data,
|
RegExpEngine::Compile(&compile_data,
|
||||||
flags.is_ignore_case(),
|
flags.is_ignore_case(),
|
||||||
flags.is_multiline(),
|
flags.is_multiline(),
|
||||||
pattern,
|
pattern,
|
||||||
is_ascii);
|
is_ascii);
|
||||||
if (!compiled_entry.is_null()) {
|
if (result.error_message != NULL) {
|
||||||
alternatives->set(index, *compiled_entry);
|
// Unable to compile regexp.
|
||||||
|
Handle<JSArray> array = Factory::NewJSArray(2);
|
||||||
|
SetElement(array, 0, pattern);
|
||||||
|
SetElement(array,
|
||||||
|
1,
|
||||||
|
Factory::NewStringFromUtf8(CStrVector(result.error_message)));
|
||||||
|
Handle<Object> regexp_err =
|
||||||
|
Factory::NewSyntaxError("malformed_regexp", array);
|
||||||
|
Top::Throw(*regexp_err);
|
||||||
|
re->SetDataAt(index, *regexp_err);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return compiled_entry;
|
|
||||||
|
NoHandleAllocation no_handles;
|
||||||
|
|
||||||
|
FixedArray* data = FixedArray::cast(re->data());
|
||||||
|
data->set(index, result.code);
|
||||||
|
int register_max = IrregexpMaxRegisterCount(data);
|
||||||
|
if (result.num_registers > register_max) {
|
||||||
|
SetIrregexpMaxRegisterCount(data, result.num_registers);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int RegExpImpl::IrregexpNumberOfCaptures(Handle<FixedArray> irre) {
|
int RegExpImpl::IrregexpMaxRegisterCount(FixedArray* re) {
|
||||||
return Smi::cast(irre->get(kIrregexpNumberOfCapturesIndex))->value();
|
return Smi::cast(
|
||||||
|
re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int RegExpImpl::IrregexpNumberOfRegisters(Handle<FixedArray> irre) {
|
void RegExpImpl::SetIrregexpMaxRegisterCount(FixedArray* re, int value) {
|
||||||
return Smi::cast(irre->get(kIrregexpNumberOfRegistersIndex))->value();
|
re->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<ByteArray> RegExpImpl::IrregexpByteCode(Handle<FixedArray> irre) {
|
int RegExpImpl::IrregexpNumberOfCaptures(FixedArray* re) {
|
||||||
ASSERT(Smi::cast(irre->get(kIrregexpImplementationIndex))->value()
|
return Smi::cast(re->get(JSRegExp::kIrregexpCaptureCountIndex))->value();
|
||||||
== RegExpMacroAssembler::kBytecodeImplementation);
|
|
||||||
return Handle<ByteArray>(ByteArray::cast(irre->get(kIrregexpCodeIndex)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<Code> RegExpImpl::IrregexpNativeCode(Handle<FixedArray> irre) {
|
int RegExpImpl::IrregexpNumberOfRegisters(FixedArray* re) {
|
||||||
ASSERT(Smi::cast(irre->get(kIrregexpImplementationIndex))->value()
|
return Smi::cast(re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value();
|
||||||
!= RegExpMacroAssembler::kBytecodeImplementation);
|
|
||||||
return Handle<Code>(Code::cast(irre->get(kIrregexpCodeIndex)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<Object>RegExpImpl::IrregexpPrepare(Handle<JSRegExp> re,
|
ByteArray* RegExpImpl::IrregexpByteCode(FixedArray* re, bool is_ascii) {
|
||||||
|
int index;
|
||||||
|
if (is_ascii) {
|
||||||
|
index = JSRegExp::kIrregexpASCIICodeIndex;
|
||||||
|
} else {
|
||||||
|
index = JSRegExp::kIrregexpUC16CodeIndex;
|
||||||
|
}
|
||||||
|
return ByteArray::cast(re->get(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Code* RegExpImpl::IrregexpNativeCode(FixedArray* re, bool is_ascii) {
|
||||||
|
int index;
|
||||||
|
if (is_ascii) {
|
||||||
|
index = JSRegExp::kIrregexpASCIICodeIndex;
|
||||||
|
} else {
|
||||||
|
index = JSRegExp::kIrregexpUC16CodeIndex;
|
||||||
|
}
|
||||||
|
return Code::cast(re->get(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void RegExpImpl::IrregexpPrepare(Handle<JSRegExp> re,
|
||||||
Handle<String> pattern,
|
Handle<String> pattern,
|
||||||
JSRegExp::Flags flags) {
|
JSRegExp::Flags flags,
|
||||||
// Make space for ASCII and UC16 versions.
|
int capture_count) {
|
||||||
Handle<FixedArray> alternatives = Factory::NewFixedArray(2);
|
// Initialize compiled code entries to null.
|
||||||
alternatives->set_null(0);
|
Factory::SetRegExpIrregexpData(re,
|
||||||
alternatives->set_null(1);
|
JSRegExp::IRREGEXP,
|
||||||
Factory::SetRegExpData(re, JSRegExp::IRREGEXP, pattern, flags, alternatives);
|
pattern,
|
||||||
return re;
|
flags,
|
||||||
|
capture_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp,
|
Handle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp,
|
||||||
Handle<String> subject,
|
Handle<String> subject,
|
||||||
Handle<Object> index) {
|
int index,
|
||||||
|
Handle<JSArray> last_match_info) {
|
||||||
ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP);
|
ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP);
|
||||||
ASSERT(regexp->DataAt(JSRegExp::kIrregexpDataIndex)->IsFixedArray());
|
|
||||||
|
|
||||||
bool is_ascii = StringShape(*subject).IsAsciiRepresentation();
|
bool is_ascii = StringShape(*subject).IsAsciiRepresentation();
|
||||||
Handle<FixedArray> irregexp = GetCompiledIrregexp(regexp, is_ascii);
|
if (!EnsureCompiledIrregexp(regexp, is_ascii)) {
|
||||||
if (irregexp.is_null()) {
|
|
||||||
// We can't handle the RegExp with IRRegExp.
|
|
||||||
return Handle<Object>::null();
|
return Handle<Object>::null();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare space for the return values.
|
// Prepare space for the return values.
|
||||||
int number_of_registers = IrregexpNumberOfRegisters(irregexp);
|
Handle<FixedArray> re_data(FixedArray::cast(regexp->data()));
|
||||||
OffsetsVector offsets(number_of_registers);
|
int number_of_capture_registers =
|
||||||
|
(IrregexpNumberOfCaptures(*re_data) + 1) * 2;
|
||||||
|
OffsetsVector offsets(number_of_capture_registers);
|
||||||
|
|
||||||
int num_captures = IrregexpNumberOfCaptures(irregexp);
|
int previous_index = index;
|
||||||
|
|
||||||
int previous_index = static_cast<int>(DoubleToInteger(index->Number()));
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (FLAG_trace_regexp_bytecodes) {
|
if (FLAG_trace_regexp_bytecodes) {
|
||||||
@ -476,8 +556,11 @@ Handle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp,
|
|||||||
FlattenString(subject);
|
FlattenString(subject);
|
||||||
}
|
}
|
||||||
|
|
||||||
return IrregexpExecOnce(irregexp,
|
last_match_info->EnsureSize(number_of_capture_registers + kLastMatchOverhead);
|
||||||
num_captures,
|
|
||||||
|
return IrregexpExecOnce(re_data,
|
||||||
|
number_of_capture_registers,
|
||||||
|
last_match_info,
|
||||||
subject,
|
subject,
|
||||||
previous_index,
|
previous_index,
|
||||||
offsets.vector(),
|
offsets.vector(),
|
||||||
@ -486,29 +569,33 @@ Handle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp,
|
|||||||
|
|
||||||
|
|
||||||
Handle<Object> RegExpImpl::IrregexpExecGlobal(Handle<JSRegExp> regexp,
|
Handle<Object> RegExpImpl::IrregexpExecGlobal(Handle<JSRegExp> regexp,
|
||||||
Handle<String> subject) {
|
Handle<String> subject,
|
||||||
|
Handle<JSArray> last_match_info) {
|
||||||
ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP);
|
ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP);
|
||||||
|
Handle<FixedArray> irregexp(FixedArray::cast(regexp->data()));
|
||||||
|
|
||||||
bool is_ascii = StringShape(*subject).IsAsciiRepresentation();
|
bool is_ascii = StringShape(*subject).IsAsciiRepresentation();
|
||||||
Handle<FixedArray> irregexp = GetCompiledIrregexp(regexp, is_ascii);
|
if (!EnsureCompiledIrregexp(regexp, is_ascii)) {
|
||||||
if (irregexp.is_null()) {
|
|
||||||
return Handle<Object>::null();
|
return Handle<Object>::null();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare space for the return values.
|
// Prepare space for the return values.
|
||||||
int number_of_registers = IrregexpNumberOfRegisters(irregexp);
|
int number_of_capture_registers =
|
||||||
OffsetsVector offsets(number_of_registers);
|
(IrregexpNumberOfCaptures(*irregexp) + 1) * 2;
|
||||||
|
OffsetsVector offsets(number_of_capture_registers);
|
||||||
|
|
||||||
int previous_index = 0;
|
int previous_index = 0;
|
||||||
|
|
||||||
Handle<JSArray> result = Factory::NewJSArray(0);
|
Handle<JSArray> result = Factory::NewJSArray(0);
|
||||||
int i = 0;
|
int result_length = 0;
|
||||||
Handle<Object> matches;
|
Handle<Object> matches;
|
||||||
|
|
||||||
if (!subject->IsFlat(StringShape(*subject))) {
|
if (!subject->IsFlat(StringShape(*subject))) {
|
||||||
FlattenString(subject);
|
FlattenString(subject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
last_match_info->EnsureSize(number_of_capture_registers + kLastMatchOverhead);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (previous_index > subject->length() || previous_index < 0) {
|
if (previous_index > subject->length() || previous_index < 0) {
|
||||||
// Per ECMA-262 15.10.6.2, if the previous index is greater than the
|
// Per ECMA-262 15.10.6.2, if the previous index is greater than the
|
||||||
@ -523,8 +610,10 @@ Handle<Object> RegExpImpl::IrregexpExecGlobal(Handle<JSRegExp> regexp,
|
|||||||
PrintF("\n\nSubject string: '%s'\n\n", *(subject->ToCString()));
|
PrintF("\n\nSubject string: '%s'\n\n", *(subject->ToCString()));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
HandleScope scope;
|
||||||
matches = IrregexpExecOnce(irregexp,
|
matches = IrregexpExecOnce(irregexp,
|
||||||
IrregexpNumberOfCaptures(irregexp),
|
number_of_capture_registers,
|
||||||
|
last_match_info,
|
||||||
subject,
|
subject,
|
||||||
previous_index,
|
previous_index,
|
||||||
offsets.vector(),
|
offsets.vector(),
|
||||||
@ -536,12 +625,25 @@ Handle<Object> RegExpImpl::IrregexpExecGlobal(Handle<JSRegExp> regexp,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (matches->IsJSArray()) {
|
if (matches->IsJSArray()) {
|
||||||
SetElement(result, i, matches);
|
// Create an array that looks like the static last_match_info array
|
||||||
i++;
|
// that is attached to the global RegExp object. We will be returning
|
||||||
previous_index = offsets.vector()[1];
|
// an array of these.
|
||||||
if (offsets.vector()[0] == offsets.vector()[1]) {
|
Handle<FixedArray> matches_array(JSArray::cast(*matches)->elements());
|
||||||
previous_index++;
|
Handle<JSArray> latest_match =
|
||||||
|
Factory::NewJSArray(kFirstCapture + number_of_capture_registers);
|
||||||
|
Handle<FixedArray> latest_match_array(latest_match->elements());
|
||||||
|
|
||||||
|
for (int i = 0; i < number_of_capture_registers; i++) {
|
||||||
|
SetCapture(*latest_match_array, i, GetCapture(*matches_array, i));
|
||||||
}
|
}
|
||||||
|
SetLastCaptureCount(*latest_match_array, number_of_capture_registers);
|
||||||
|
|
||||||
|
SetElement(result, result_length, latest_match);
|
||||||
|
result_length++;
|
||||||
|
previous_index = GetCapture(*matches_array, 1);
|
||||||
|
if (GetCapture(*matches_array, 0) == previous_index)
|
||||||
|
previous_index++;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
ASSERT(matches->IsNull());
|
ASSERT(matches->IsNull());
|
||||||
return result;
|
return result;
|
||||||
@ -551,23 +653,22 @@ Handle<Object> RegExpImpl::IrregexpExecGlobal(Handle<JSRegExp> regexp,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<Object> RegExpImpl::IrregexpExecOnce(Handle<FixedArray> irregexp,
|
Handle<Object> RegExpImpl::IrregexpExecOnce(Handle<FixedArray> regexp,
|
||||||
int num_captures,
|
int number_of_capture_registers,
|
||||||
|
Handle<JSArray> last_match_info,
|
||||||
Handle<String> subject,
|
Handle<String> subject,
|
||||||
int previous_index,
|
int previous_index,
|
||||||
int* offsets_vector,
|
int* offsets_vector,
|
||||||
int offsets_vector_length) {
|
int offsets_vector_length) {
|
||||||
ASSERT(subject->IsFlat(StringShape(*subject)));
|
StringShape shape(*subject);
|
||||||
|
ASSERT(subject->IsFlat(shape));
|
||||||
|
bool is_ascii = shape.IsAsciiRepresentation();
|
||||||
bool rc;
|
bool rc;
|
||||||
|
|
||||||
int tag = Smi::cast(irregexp->get(kIrregexpImplementationIndex))->value();
|
Handle<String> original_subject = subject;
|
||||||
|
if (FLAG_regexp_native) {
|
||||||
switch (tag) {
|
|
||||||
case RegExpMacroAssembler::kIA32Implementation: {
|
|
||||||
#ifndef ARM
|
#ifndef ARM
|
||||||
Handle<Code> code = IrregexpNativeCode(irregexp);
|
Handle<Code> code(IrregexpNativeCode(*regexp, is_ascii));
|
||||||
|
|
||||||
StringShape shape(*subject);
|
|
||||||
|
|
||||||
// Character offsets into string.
|
// Character offsets into string.
|
||||||
int start_offset = previous_index;
|
int start_offset = previous_index;
|
||||||
@ -634,48 +735,43 @@ Handle<Object> RegExpImpl::IrregexpExecOnce(Handle<FixedArray> irregexp,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
} else {
|
||||||
#else
|
#else
|
||||||
UNIMPLEMENTED();
|
// Unimplemented on ARM, fall through to bytecode.
|
||||||
rc = false;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
case RegExpMacroAssembler::kBytecodeImplementation: {
|
{
|
||||||
for (int i = (num_captures + 1) * 2 - 1; i >= 0; i--) {
|
#endif
|
||||||
|
for (int i = number_of_capture_registers - 1; i >= 0; i--) {
|
||||||
offsets_vector[i] = -1;
|
offsets_vector[i] = -1;
|
||||||
}
|
}
|
||||||
Handle<ByteArray> byte_codes = IrregexpByteCode(irregexp);
|
Handle<ByteArray> byte_codes(IrregexpByteCode(*regexp, is_ascii));
|
||||||
|
|
||||||
rc = IrregexpInterpreter::Match(byte_codes,
|
rc = IrregexpInterpreter::Match(byte_codes,
|
||||||
subject,
|
subject,
|
||||||
offsets_vector,
|
offsets_vector,
|
||||||
previous_index);
|
previous_index);
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RegExpMacroAssembler::kARMImplementation:
|
|
||||||
default:
|
|
||||||
UNREACHABLE();
|
|
||||||
rc = false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!rc) {
|
if (!rc) {
|
||||||
return Factory::null_value();
|
return Factory::null_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
Handle<FixedArray> array = Factory::NewFixedArray(2 * (num_captures+1));
|
FixedArray* array = last_match_info->elements();
|
||||||
|
ASSERT(array->length() >= number_of_capture_registers + kLastMatchOverhead);
|
||||||
// The captures come in (start, end+1) pairs.
|
// The captures come in (start, end+1) pairs.
|
||||||
for (int i = 0; i < 2 * (num_captures + 1); i += 2) {
|
for (int i = 0; i < number_of_capture_registers; i += 2) {
|
||||||
array->set(i, Smi::FromInt(offsets_vector[i]));
|
SetCapture(array, i, offsets_vector[i]);
|
||||||
array->set(i + 1, Smi::FromInt(offsets_vector[i + 1]));
|
SetCapture(array, i + 1, offsets_vector[i + 1]);
|
||||||
}
|
}
|
||||||
return Factory::NewJSArrayWithElements(array);
|
SetLastCaptureCount(array, number_of_capture_registers);
|
||||||
|
SetLastSubject(array, *original_subject);
|
||||||
|
SetLastInput(array, *original_subject);
|
||||||
|
return last_match_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
// Implmentation of the Irregexp regular expression engine.
|
// Implementation of the Irregexp regular expression engine.
|
||||||
//
|
//
|
||||||
// The Irregexp regular expression engine is intended to be a complete
|
// The Irregexp regular expression engine is intended to be a complete
|
||||||
// implementation of ECMAScript regular expressions. It generates either
|
// implementation of ECMAScript regular expressions. It generates either
|
||||||
@ -892,7 +988,7 @@ class RegExpCompiler {
|
|||||||
return next_register_++;
|
return next_register_++;
|
||||||
}
|
}
|
||||||
|
|
||||||
Handle<FixedArray> Assemble(RegExpMacroAssembler* assembler,
|
RegExpEngine::CompilationResult Assemble(RegExpMacroAssembler* assembler,
|
||||||
RegExpNode* start,
|
RegExpNode* start,
|
||||||
int capture_count,
|
int capture_count,
|
||||||
Handle<String> pattern);
|
Handle<String> pattern);
|
||||||
@ -940,15 +1036,8 @@ class RecursionCheck {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static Handle<FixedArray> IrregexpRegExpTooBig(Handle<String> pattern) {
|
static RegExpEngine::CompilationResult IrregexpRegExpTooBig() {
|
||||||
Handle<JSArray> array = Factory::NewJSArray(2);
|
return RegExpEngine::CompilationResult("RegExp too big");
|
||||||
SetElement(array, 0, pattern);
|
|
||||||
const char* message = "RegExp too big";
|
|
||||||
SetElement(array, 1, Factory::NewStringFromUtf8(CStrVector(message)));
|
|
||||||
Handle<Object> regexp_err =
|
|
||||||
Factory::NewSyntaxError("malformed_regexp", array);
|
|
||||||
Top::Throw(*regexp_err);
|
|
||||||
return Handle<FixedArray>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -966,7 +1055,7 @@ RegExpCompiler::RegExpCompiler(int capture_count, bool ignore_case, bool ascii)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<FixedArray> RegExpCompiler::Assemble(
|
RegExpEngine::CompilationResult RegExpCompiler::Assemble(
|
||||||
RegExpMacroAssembler* macro_assembler,
|
RegExpMacroAssembler* macro_assembler,
|
||||||
RegExpNode* start,
|
RegExpNode* start,
|
||||||
int capture_count,
|
int capture_count,
|
||||||
@ -988,24 +1077,17 @@ Handle<FixedArray> RegExpCompiler::Assemble(
|
|||||||
while (!work_list.is_empty()) {
|
while (!work_list.is_empty()) {
|
||||||
work_list.RemoveLast()->Emit(this, &new_trace);
|
work_list.RemoveLast()->Emit(this, &new_trace);
|
||||||
}
|
}
|
||||||
if (reg_exp_too_big_) return IrregexpRegExpTooBig(pattern);
|
if (reg_exp_too_big_) return IrregexpRegExpTooBig();
|
||||||
Handle<FixedArray> array =
|
|
||||||
Factory::NewFixedArray(RegExpImpl::kIrregexpDataLength);
|
|
||||||
array->set(RegExpImpl::kIrregexpImplementationIndex,
|
|
||||||
Smi::FromInt(macro_assembler_->Implementation()));
|
|
||||||
array->set(RegExpImpl::kIrregexpNumberOfRegistersIndex,
|
|
||||||
Smi::FromInt(next_register_));
|
|
||||||
array->set(RegExpImpl::kIrregexpNumberOfCapturesIndex,
|
|
||||||
Smi::FromInt(capture_count));
|
|
||||||
Handle<Object> code = macro_assembler_->GetCode(pattern);
|
Handle<Object> code = macro_assembler_->GetCode(pattern);
|
||||||
array->set(RegExpImpl::kIrregexpCodeIndex, *code);
|
|
||||||
work_list_ = NULL;
|
work_list_ = NULL;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (FLAG_trace_regexp_assembler) {
|
if (FLAG_trace_regexp_assembler) {
|
||||||
delete macro_assembler_;
|
delete macro_assembler_;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return array;
|
return RegExpEngine::CompilationResult(*code, next_register_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -3723,9 +3805,6 @@ RegExpNode* RegExpQuantifier::ToNode(int min,
|
|||||||
// |
|
// |
|
||||||
// [if r >= f] \----> ...
|
// [if r >= f] \----> ...
|
||||||
//
|
//
|
||||||
//
|
|
||||||
// TODO(someone): clear captures on repetition and handle empty
|
|
||||||
// matches.
|
|
||||||
|
|
||||||
// 15.10.2.5 RepeatMatcher algorithm.
|
// 15.10.2.5 RepeatMatcher algorithm.
|
||||||
// The parser has already eliminated the case where max is 0. In the case
|
// The parser has already eliminated the case where max is 0. In the case
|
||||||
@ -4592,13 +4671,13 @@ void DispatchTableConstructor::VisitAction(ActionNode* that) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<FixedArray> RegExpEngine::Compile(RegExpCompileData* data,
|
RegExpEngine::CompilationResult RegExpEngine::Compile(RegExpCompileData* data,
|
||||||
bool ignore_case,
|
bool ignore_case,
|
||||||
bool is_multiline,
|
bool is_multiline,
|
||||||
Handle<String> pattern,
|
Handle<String> pattern,
|
||||||
bool is_ascii) {
|
bool is_ascii) {
|
||||||
if ((data->capture_count + 1) * 2 - 1 > RegExpMacroAssembler::kMaxRegister) {
|
if ((data->capture_count + 1) * 2 - 1 > RegExpMacroAssembler::kMaxRegister) {
|
||||||
return IrregexpRegExpTooBig(pattern);
|
return IrregexpRegExpTooBig();
|
||||||
}
|
}
|
||||||
RegExpCompiler compiler(data->capture_count, ignore_case, is_ascii);
|
RegExpCompiler compiler(data->capture_count, ignore_case, is_ascii);
|
||||||
// Wrap the body of the regexp in capture #0.
|
// Wrap the body of the regexp in capture #0.
|
||||||
|
@ -51,6 +51,7 @@ class RegExpImpl {
|
|||||||
// Parses the RegExp pattern and prepares the JSRegExp object with
|
// Parses the RegExp pattern and prepares the JSRegExp object with
|
||||||
// generic data and choice of implementation - as well as what
|
// generic data and choice of implementation - as well as what
|
||||||
// the implementation wants to store in the data field.
|
// the implementation wants to store in the data field.
|
||||||
|
// Returns false if compilation fails.
|
||||||
static Handle<Object> Compile(Handle<JSRegExp> re,
|
static Handle<Object> Compile(Handle<JSRegExp> re,
|
||||||
Handle<String> pattern,
|
Handle<String> pattern,
|
||||||
Handle<String> flags);
|
Handle<String> flags);
|
||||||
@ -59,38 +60,46 @@ 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 void IrregexpPrepare(Handle<JSRegExp> re,
|
||||||
Handle<String> pattern,
|
Handle<String> pattern,
|
||||||
JSRegExp::Flags flags);
|
JSRegExp::Flags flags,
|
||||||
|
int capture_register_count);
|
||||||
|
|
||||||
|
|
||||||
static Handle<Object> AtomCompile(Handle<JSRegExp> re,
|
static void AtomCompile(Handle<JSRegExp> re,
|
||||||
Handle<String> pattern,
|
Handle<String> pattern,
|
||||||
JSRegExp::Flags flags,
|
JSRegExp::Flags flags,
|
||||||
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();
|
||||||
@ -101,26 +110,54 @@ class RegExpImpl {
|
|||||||
static Handle<String> StringToTwoByte(Handle<String> pattern);
|
static Handle<String> StringToTwoByte(Handle<String> pattern);
|
||||||
static Handle<String> CachedStringToTwoByte(Handle<String> pattern);
|
static Handle<String> CachedStringToTwoByte(Handle<String> pattern);
|
||||||
|
|
||||||
static const int kIrregexpImplementationIndex = 0;
|
// Offsets in the lastMatchInfo array.
|
||||||
static const int kIrregexpNumberOfCapturesIndex = 1;
|
static const int kLastCaptureCount = 0;
|
||||||
static const int kIrregexpNumberOfRegistersIndex = 2;
|
static const int kLastSubject = 1;
|
||||||
static const int kIrregexpCodeIndex = 3;
|
static const int kLastInput = 2;
|
||||||
static const int kIrregexpDataLength = 4;
|
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_;
|
||||||
|
|
||||||
static int IrregexpNumberOfCaptures(Handle<FixedArray> re);
|
static bool EnsureCompiledIrregexp(Handle<JSRegExp> re, bool is_ascii);
|
||||||
static int IrregexpNumberOfRegisters(Handle<FixedArray> re);
|
|
||||||
static Handle<ByteArray> IrregexpByteCode(Handle<FixedArray> re);
|
static int IrregexpMaxRegisterCount(FixedArray* re);
|
||||||
static Handle<Code> IrregexpNativeCode(Handle<FixedArray> re);
|
static void SetIrregexpMaxRegisterCount(FixedArray* re, int value);
|
||||||
|
static int IrregexpNumberOfCaptures(FixedArray* re);
|
||||||
|
static int IrregexpNumberOfRegisters(FixedArray* re);
|
||||||
|
static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii);
|
||||||
|
static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii);
|
||||||
|
|
||||||
// On a successful match, the result is a JSArray containing
|
// On a successful match, the result is a JSArray containing
|
||||||
// captured positions. On a failure, the result is the null value.
|
// captured positions. On a failure, the result is the null value.
|
||||||
// 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 +171,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.
|
||||||
@ -1319,7 +1360,21 @@ struct RegExpCompileData {
|
|||||||
|
|
||||||
class RegExpEngine: public AllStatic {
|
class RegExpEngine: public AllStatic {
|
||||||
public:
|
public:
|
||||||
static Handle<FixedArray> Compile(RegExpCompileData* input,
|
struct CompilationResult {
|
||||||
|
explicit CompilationResult(const char* error_message)
|
||||||
|
: error_message(error_message),
|
||||||
|
code(Heap::the_hole_value()),
|
||||||
|
num_registers(0) {}
|
||||||
|
CompilationResult(Object* code, int registers)
|
||||||
|
: error_message(NULL),
|
||||||
|
code(code),
|
||||||
|
num_registers(registers) {}
|
||||||
|
const char* error_message;
|
||||||
|
Object* code;
|
||||||
|
int num_registers;
|
||||||
|
};
|
||||||
|
|
||||||
|
static CompilationResult Compile(RegExpCompileData* input,
|
||||||
bool ignore_case,
|
bool ignore_case,
|
||||||
bool multiline,
|
bool multiline,
|
||||||
Handle<String> pattern,
|
Handle<String> pattern,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# 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]);
|
|||||||
# 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;
|
||||||
|
@ -696,9 +696,20 @@ void JSRegExp::JSRegExpVerify() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case JSRegExp::IRREGEXP: {
|
case JSRegExp::IRREGEXP: {
|
||||||
|
bool is_native = FLAG_regexp_native;
|
||||||
|
#ifdef ARM
|
||||||
|
// No native regexp on arm yet.
|
||||||
|
is_native = false;
|
||||||
|
#endif
|
||||||
FixedArray* arr = FixedArray::cast(data());
|
FixedArray* arr = FixedArray::cast(data());
|
||||||
Object* irregexp_data = arr->get(JSRegExp::kIrregexpDataIndex);
|
Object* ascii_data = arr->get(JSRegExp::kIrregexpASCIICodeIndex);
|
||||||
ASSERT(irregexp_data->IsFixedArray());
|
ASSERT(ascii_data->IsTheHole()
|
||||||
|
|| (is_native ? ascii_data->IsCode() : ascii_data->IsByteArray()));
|
||||||
|
Object* uc16_data = arr->get(JSRegExp::kIrregexpUC16CodeIndex);
|
||||||
|
ASSERT(uc16_data->IsTheHole()
|
||||||
|
|| (is_native ? uc16_data->IsCode() : uc16_data->IsByteArray()));
|
||||||
|
ASSERT(arr->get(JSRegExp::kIrregexpCaptureCountIndex)->IsSmi());
|
||||||
|
ASSERT(arr->get(JSRegExp::kIrregexpMaxRegisterCountIndex)->IsSmi());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
@ -2338,6 +2338,13 @@ Object* JSRegExp::DataAt(int index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void JSRegExp::SetDataAt(int index, Object* value) {
|
||||||
|
ASSERT(TypeTag() != NOT_COMPILED);
|
||||||
|
ASSERT(index >= kDataIndex); // Only implementation data can be set this way.
|
||||||
|
FixedArray::cast(data())->set(index, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool JSObject::HasFastElements() {
|
bool JSObject::HasFastElements() {
|
||||||
return !elements()->IsDictionary();
|
return !elements()->IsDictionary();
|
||||||
}
|
}
|
||||||
|
@ -4888,6 +4888,22 @@ Object* JSArray::Initialize(int capacity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void JSArray::EnsureSize(int required_size) {
|
||||||
|
Handle<JSArray> self(this);
|
||||||
|
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);
|
||||||
|
// Can't use this any more now because we may have had a GC!
|
||||||
|
for (int i = 0; i < old_size; i++) new_backing->set(i, old_backing->get(i));
|
||||||
|
self->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
|
||||||
|
@ -2966,6 +2966,19 @@ class JSValue: public JSObject {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Regular expressions
|
// Regular expressions
|
||||||
|
// The regular expression holds a single reference to a FixedArray in
|
||||||
|
// the kDataOffset field.
|
||||||
|
// The FixedArray contains the following data:
|
||||||
|
// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
|
||||||
|
// - reference to the original source string
|
||||||
|
// - reference to the original flag string
|
||||||
|
// If it is an atom regexp
|
||||||
|
// - a reference to a literal string to search for
|
||||||
|
// If it is an irregexp regexp:
|
||||||
|
// - a reference to code for ASCII inputs (bytecode or compiled).
|
||||||
|
// - a reference to code for UC16 inputs (bytecode or compiled).
|
||||||
|
// - max number of registers used by irregexp implementations.
|
||||||
|
// - number of capture registers (output values) of the regexp.
|
||||||
class JSRegExp: public JSObject {
|
class JSRegExp: public JSObject {
|
||||||
public:
|
public:
|
||||||
// Meaning of Type:
|
// Meaning of Type:
|
||||||
@ -2993,6 +3006,8 @@ class JSRegExp: public JSObject {
|
|||||||
inline Flags GetFlags();
|
inline Flags GetFlags();
|
||||||
inline String* Pattern();
|
inline String* Pattern();
|
||||||
inline Object* DataAt(int index);
|
inline Object* DataAt(int index);
|
||||||
|
// Set implementation data after the object has been prepared.
|
||||||
|
inline void SetDataAt(int index, Object* value);
|
||||||
|
|
||||||
static inline JSRegExp* cast(Object* obj);
|
static inline JSRegExp* cast(Object* obj);
|
||||||
|
|
||||||
@ -3004,14 +3019,29 @@ class JSRegExp: public JSObject {
|
|||||||
static const int kDataOffset = JSObject::kHeaderSize;
|
static const int kDataOffset = JSObject::kHeaderSize;
|
||||||
static const int kSize = kDataOffset + kIntSize;
|
static const int kSize = kDataOffset + kIntSize;
|
||||||
|
|
||||||
|
// Indices in the data array.
|
||||||
static const int kTagIndex = 0;
|
static const int kTagIndex = 0;
|
||||||
static const int kSourceIndex = kTagIndex + 1;
|
static const int kSourceIndex = kTagIndex + 1;
|
||||||
static const int kFlagsIndex = kSourceIndex + 1;
|
static const int kFlagsIndex = kSourceIndex + 1;
|
||||||
// These two are the same since the same entry is shared for
|
static const int kDataIndex = kFlagsIndex + 1;
|
||||||
// different purposes in different types of regexps.
|
// The data fields are used in different ways depending on the
|
||||||
static const int kAtomPatternIndex = kFlagsIndex + 1;
|
// value of the tag.
|
||||||
static const int kIrregexpDataIndex = kFlagsIndex + 1;
|
// Atom regexps (literal strings).
|
||||||
static const int kDataSize = kAtomPatternIndex + 1;
|
static const int kAtomPatternIndex = kDataIndex;
|
||||||
|
|
||||||
|
static const int kAtomDataSize = kAtomPatternIndex + 1;
|
||||||
|
|
||||||
|
// Irregexp compiled code or bytecode for ASCII.
|
||||||
|
static const int kIrregexpASCIICodeIndex = kDataIndex;
|
||||||
|
// Irregexp compiled code or bytecode for UC16.
|
||||||
|
static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
|
||||||
|
// Maximal number of registers used by either ASCII or UC16.
|
||||||
|
// Only used to check that there is enough stack space
|
||||||
|
static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
|
||||||
|
// Number of captures in the compiled regexp.
|
||||||
|
static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
|
||||||
|
|
||||||
|
static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -3806,6 +3836,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();
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// 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) {
|
|||||||
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) {
|
|||||||
|
|
||||||
// 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,35 +135,24 @@ 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 (!IS_REGEXP(this)) {
|
if (!IS_REGEXP(this)) {
|
||||||
throw MakeTypeError('method_called_on_incompatible', ['RegExp.prototype.exec', this]);
|
throw MakeTypeError('method_called_on_incompatible',
|
||||||
|
['RegExp.prototype.exec', this]);
|
||||||
}
|
}
|
||||||
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]);
|
||||||
}
|
}
|
||||||
@ -180,23 +169,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.
|
||||||
@ -205,16 +192,50 @@ 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 the method is
|
||||||
|
// called on a non-RegExp object. However, it probably means the original
|
||||||
|
// value of String.prototype.exec, which is what everybody else implements.
|
||||||
function RegExpTest(string) {
|
function RegExpTest(string) {
|
||||||
var result = (%_ArgumentsLength() == 0) ? this.exec() : this.exec(string);
|
if (!IS_REGEXP(this)) {
|
||||||
return result != null;
|
throw MakeTypeError('method_called_on_incompatible',
|
||||||
|
['RegExp.prototype.test', this]);
|
||||||
|
}
|
||||||
|
if (%_ArgumentsLength() == 0) {
|
||||||
|
var regExpInput = LAST_INPUT(lastMatchInfo);
|
||||||
|
if (IS_UNDEFINED(regExpInput)) {
|
||||||
|
throw MakeError('no_input_to_regexp', [this]);
|
||||||
|
}
|
||||||
|
string = regExpInput;
|
||||||
|
}
|
||||||
|
var s = ToString(string);
|
||||||
|
var length = s.length;
|
||||||
|
var lastIndex = this.lastIndex;
|
||||||
|
var i = this.global ? TO_INTEGER(lastIndex) : 0;
|
||||||
|
|
||||||
|
if (i < 0 || i > s.length) {
|
||||||
|
this.lastIndex = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
%_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]);
|
||||||
|
// matchIndices is either null or the lastMatchInfo array.
|
||||||
|
var matchIndices = %RegExpExec(this, s, i, lastMatchInfo);
|
||||||
|
|
||||||
|
if (matchIndices == null) {
|
||||||
|
if (this.global) this.lastIndex = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.global) this.lastIndex = lastMatchInfo[CAPTURE1];
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -239,56 +260,72 @@ 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]);
|
var start = lastMatchInfo[CAPTURE(length - 2)];
|
||||||
|
var end = lastMatchInfo[CAPTURE(length - 1)];
|
||||||
|
if (start != -1 && end != -1) {
|
||||||
|
return SubString(regExpSubject, start, end);
|
||||||
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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.
|
||||||
|
];
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
@ -312,9 +349,13 @@ function SetupRegExp() {
|
|||||||
// 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] =
|
||||||
|
ToString(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);
|
||||||
|
@ -930,14 +930,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;
|
||||||
}
|
}
|
||||||
@ -945,12 +952,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 {
|
|||||||
\
|
\
|
||||||
/* 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) \
|
||||||
|
156
src/string.js
156
src/string.js
@ -165,8 +165,9 @@ function StringMatch(regexp) {
|
|||||||
// Build the result array.
|
// Build the result array.
|
||||||
var result = new $Array(match_string);
|
var result = new $Array(match_string);
|
||||||
for (var i = 0; i < matches.length; ++i) {
|
for (var i = 0; i < matches.length; ++i) {
|
||||||
var match = matches[i];
|
var matchInfo = matches[i];
|
||||||
var match_string = subject.slice(match[0], match[1]);
|
var match_string = subject.slice(matchInfo[CAPTURE0],
|
||||||
|
matchInfo[CAPTURE1]);
|
||||||
result[i] = match_string;
|
result[i] = match_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,7 +219,9 @@ function StringReplace(search, replace) {
|
|||||||
if (IS_FUNCTION(replace)) {
|
if (IS_FUNCTION(replace)) {
|
||||||
builder.add(replace.call(null, search, start, subject));
|
builder.add(replace.call(null, search, start, subject));
|
||||||
} else {
|
} else {
|
||||||
ExpandReplacement(ToString(replace), subject, [ start, end ], builder);
|
reusableMatchInfo[CAPTURE0] = start;
|
||||||
|
reusableMatchInfo[CAPTURE1] = end;
|
||||||
|
ExpandReplacement(ToString(replace), subject, reusableMatchInfo, builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
// suffix
|
// suffix
|
||||||
@ -228,6 +231,15 @@ function StringReplace(search, replace) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// This has the same size as the lastMatchInfo array, and can be used for
|
||||||
|
// functions that expect that structure to be returned. It is used when the
|
||||||
|
// needle is a string rather than a regexp. In this case we can't update
|
||||||
|
// lastMatchArray without erroneously affecting the properties on the global
|
||||||
|
// RegExp object.
|
||||||
|
var reusableMatchInfo = [2, -1, -1, "", ""];
|
||||||
|
var reusableMatchArray = [ void 0 ];
|
||||||
|
|
||||||
|
|
||||||
// Helper function for regular expressions in String.prototype.replace.
|
// Helper function for regular expressions in String.prototype.replace.
|
||||||
function StringReplaceRegExp(subject, regexp, replace) {
|
function StringReplaceRegExp(subject, regexp, replace) {
|
||||||
// Compute an array of matches; each match is really a list of
|
// Compute an array of matches; each match is really a list of
|
||||||
@ -237,9 +249,10 @@ function StringReplaceRegExp(subject, regexp, replace) {
|
|||||||
matches = DoRegExpExecGlobal(regexp, subject);
|
matches = DoRegExpExecGlobal(regexp, subject);
|
||||||
if (matches.length == 0) return subject;
|
if (matches.length == 0) return subject;
|
||||||
} else {
|
} else {
|
||||||
var captures = DoRegExpExec(regexp, subject, 0);
|
var lastMatchInfo = DoRegExpExec(regexp, subject, 0);
|
||||||
if (IS_NULL(captures)) return subject;
|
if (IS_NULL(lastMatchInfo)) return subject;
|
||||||
matches = [ captures ];
|
reusableMatchArray[0] = lastMatchInfo;
|
||||||
|
matches = reusableMatchArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the number of matches.
|
// Determine the number of matches.
|
||||||
@ -253,17 +266,17 @@ function StringReplaceRegExp(subject, regexp, replace) {
|
|||||||
replace = ToString(replace);
|
replace = ToString(replace);
|
||||||
if (%StringIndexOf(replace, "$", 0) < 0) {
|
if (%StringIndexOf(replace, "$", 0) < 0) {
|
||||||
for (var i = 0; i < length; i++) {
|
for (var i = 0; i < length; i++) {
|
||||||
var captures = matches[i];
|
var matchInfo = matches[i];
|
||||||
result.addSpecialSlice(previous, captures[0]);
|
result.addSpecialSlice(previous, matchInfo[CAPTURE0]);
|
||||||
result.add(replace);
|
result.add(replace);
|
||||||
previous = captures[1]; // continue after match
|
previous = matchInfo[CAPTURE1]; // continue after match
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (var i = 0; i < length; i++) {
|
for (var i = 0; i < length; i++) {
|
||||||
var captures = matches[i];
|
var matchInfo = matches[i];
|
||||||
result.addSpecialSlice(previous, captures[0]);
|
result.addSpecialSlice(previous, matchInfo[CAPTURE0]);
|
||||||
ExpandReplacement(replace, subject, captures, result);
|
ExpandReplacement(replace, subject, matchInfo, result);
|
||||||
previous = captures[1]; // continue after match
|
previous = matchInfo[CAPTURE1]; // continue after match
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.addSpecialSlice(previous, subject.length);
|
result.addSpecialSlice(previous, subject.length);
|
||||||
@ -273,7 +286,7 @@ function StringReplaceRegExp(subject, regexp, replace) {
|
|||||||
|
|
||||||
// Expand the $-expressions in the string and return a new string with
|
// Expand the $-expressions in the string and return a new string with
|
||||||
// the result.
|
// the result.
|
||||||
function ExpandReplacement(string, subject, captures, builder) {
|
function ExpandReplacement(string, subject, matchInfo, builder) {
|
||||||
var next = %StringIndexOf(string, '$', 0);
|
var next = %StringIndexOf(string, '$', 0);
|
||||||
if (next < 0) {
|
if (next < 0) {
|
||||||
builder.add(string);
|
builder.add(string);
|
||||||
@ -281,7 +294,7 @@ function ExpandReplacement(string, subject, captures, builder) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Compute the number of captures; see ECMA-262, 15.5.4.11, p. 102.
|
// Compute the number of captures; see ECMA-262, 15.5.4.11, p. 102.
|
||||||
var m = captures.length >> 1; // includes the match
|
var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; // Includes the match.
|
||||||
|
|
||||||
if (next > 0) builder.add(SubString(string, 0, next));
|
if (next > 0) builder.add(SubString(string, 0, next));
|
||||||
var length = string.length;
|
var length = string.length;
|
||||||
@ -299,13 +312,14 @@ function ExpandReplacement(string, subject, captures, builder) {
|
|||||||
builder.add('$');
|
builder.add('$');
|
||||||
} else if (peek == 38) { // $& - match
|
} else if (peek == 38) { // $& - match
|
||||||
++position;
|
++position;
|
||||||
builder.addSpecialSlice(captures[0], captures[1]);
|
builder.addSpecialSlice(matchInfo[CAPTURE0],
|
||||||
|
matchInfo[CAPTURE1]);
|
||||||
} else if (peek == 96) { // $` - prefix
|
} else if (peek == 96) { // $` - prefix
|
||||||
++position;
|
++position;
|
||||||
builder.addSpecialSlice(0, captures[0]);
|
builder.addSpecialSlice(0, matchInfo[CAPTURE0]);
|
||||||
} else if (peek == 39) { // $' - suffix
|
} else if (peek == 39) { // $' - suffix
|
||||||
++position;
|
++position;
|
||||||
builder.addSpecialSlice(captures[1], subject.length);
|
builder.addSpecialSlice(matchInfo[CAPTURE1], subject.length);
|
||||||
} else if (peek >= 48 && peek <= 57) { // $n, 0 <= n <= 9
|
} else if (peek >= 48 && peek <= 57) { // $n, 0 <= n <= 9
|
||||||
++position;
|
++position;
|
||||||
var n = peek - 48;
|
var n = peek - 48;
|
||||||
@ -329,7 +343,7 @@ function ExpandReplacement(string, subject, captures, builder) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (0 < n && n < m) {
|
if (0 < n && n < m) {
|
||||||
addCaptureString(builder, captures, n);
|
addCaptureString(builder, matchInfo, n);
|
||||||
} else {
|
} else {
|
||||||
// Because of the captures range check in the parsing of two
|
// Because of the captures range check in the parsing of two
|
||||||
// digit capture references, we can only enter here when a
|
// digit capture references, we can only enter here when a
|
||||||
@ -361,26 +375,27 @@ function ExpandReplacement(string, subject, captures, builder) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Compute the string of a given PCRE capture.
|
// Compute the string of a given regular expression capture.
|
||||||
function CaptureString(string, captures, index) {
|
function CaptureString(string, lastCaptureInfo, index) {
|
||||||
// Scale the index.
|
// Scale the index.
|
||||||
var scaled = index << 1;
|
var scaled = index << 1;
|
||||||
// Compute start and end.
|
// Compute start and end.
|
||||||
var start = captures[scaled];
|
var start = lastCaptureInfo[CAPTURE(scaled)];
|
||||||
var end = captures[scaled + 1];
|
var end = lastCaptureInfo[CAPTURE(scaled + 1)];
|
||||||
// If either start or end is missing return undefined.
|
// If either start or end is missing return undefined.
|
||||||
if (start < 0 || end < 0) return;
|
if (start < 0 || end < 0) return;
|
||||||
return SubString(string, start, end);
|
return SubString(string, start, end);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Add the string of a given PCRE capture to the ReplaceResultBuilder
|
// Add the string of a given regular expression capture to the
|
||||||
function addCaptureString(builder, captures, index) {
|
// ReplaceResultBuilder
|
||||||
|
function addCaptureString(builder, matchInfo, index) {
|
||||||
// Scale the index.
|
// Scale the index.
|
||||||
var scaled = index << 1;
|
var scaled = index << 1;
|
||||||
// Compute start and end.
|
// Compute start and end.
|
||||||
var start = captures[scaled];
|
var start = matchInfo[CAPTURE(scaled)];
|
||||||
var end = captures[scaled + 1];
|
var end = matchInfo[CAPTURE(scaled + 1)];
|
||||||
// If either start or end is missing return.
|
// If either start or end is missing return.
|
||||||
if (start < 0 || end <= start) return;
|
if (start < 0 || end <= start) return;
|
||||||
builder.addSpecialSlice(start, end);
|
builder.addSpecialSlice(start, end);
|
||||||
@ -396,10 +411,8 @@ function addCaptureString(builder, captures, index) {
|
|||||||
// should be 'abcd' and not 'dddd' (or anything else).
|
// should be 'abcd' and not 'dddd' (or anything else).
|
||||||
function StringReplaceRegExpWithFunction(subject, regexp, replace) {
|
function StringReplaceRegExpWithFunction(subject, regexp, replace) {
|
||||||
var result = new ReplaceResultBuilder(subject);
|
var result = new ReplaceResultBuilder(subject);
|
||||||
// Captures is an array of pairs of (start, end) indices for the match and
|
var lastMatchInfo = DoRegExpExec(regexp, subject, 0);
|
||||||
// any captured substrings.
|
if (IS_NULL(lastMatchInfo)) return subject;
|
||||||
var captures = DoRegExpExec(regexp, subject, 0);
|
|
||||||
if (IS_NULL(captures)) return subject;
|
|
||||||
|
|
||||||
// There's at least one match. If the regexp is global, we have to loop
|
// There's at least one match. If the regexp is global, we have to loop
|
||||||
// over all matches. The loop is not in C++ code here like the one in
|
// over all matches. The loop is not in C++ code here like the one in
|
||||||
@ -409,13 +422,16 @@ function StringReplaceRegExpWithFunction(subject, regexp, replace) {
|
|||||||
if (regexp.global) {
|
if (regexp.global) {
|
||||||
var previous = 0;
|
var previous = 0;
|
||||||
do {
|
do {
|
||||||
result.addSpecialSlice(previous, captures[0]);
|
result.addSpecialSlice(previous, lastMatchInfo[CAPTURE0]);
|
||||||
result.add(ApplyReplacementFunction(replace, captures, subject));
|
var startOfMatch = lastMatchInfo[CAPTURE0];
|
||||||
|
previous = lastMatchInfo[CAPTURE1];
|
||||||
|
result.add(ApplyReplacementFunction(replace, lastMatchInfo, subject));
|
||||||
|
// Can't use lastMatchInfo any more from here, since the function could
|
||||||
|
// overwrite it.
|
||||||
// Continue with the next match.
|
// Continue with the next match.
|
||||||
previous = captures[1];
|
|
||||||
// Increment previous if we matched an empty string, as per ECMA-262
|
// Increment previous if we matched an empty string, as per ECMA-262
|
||||||
// 15.5.4.10.
|
// 15.5.4.10.
|
||||||
if (previous == captures[0]) {
|
if (previous == startOfMatch) {
|
||||||
// Add the skipped character to the output, if any.
|
// Add the skipped character to the output, if any.
|
||||||
if (previous < subject.length) {
|
if (previous < subject.length) {
|
||||||
result.addSpecialSlice(previous, previous + 1);
|
result.addSpecialSlice(previous, previous + 1);
|
||||||
@ -425,19 +441,22 @@ function StringReplaceRegExpWithFunction(subject, regexp, replace) {
|
|||||||
|
|
||||||
// Per ECMA-262 15.10.6.2, if the previous index is greater than the
|
// Per ECMA-262 15.10.6.2, if the previous index is greater than the
|
||||||
// string length, there is no match
|
// string length, there is no match
|
||||||
captures = (previous > subject.length)
|
lastMatchInfo = (previous > subject.length)
|
||||||
? null
|
? null
|
||||||
: DoRegExpExec(regexp, subject, previous);
|
: DoRegExpExec(regexp, subject, previous);
|
||||||
} while (!IS_NULL(captures));
|
} while (!IS_NULL(lastMatchInfo));
|
||||||
|
|
||||||
// Tack on the final right substring after the last match, if necessary.
|
// Tack on the final right substring after the last match, if necessary.
|
||||||
if (previous < subject.length) {
|
if (previous < subject.length) {
|
||||||
result.addSpecialSlice(previous, subject.length);
|
result.addSpecialSlice(previous, subject.length);
|
||||||
}
|
}
|
||||||
} else { // Not a global regexp, no need to loop.
|
} else { // Not a global regexp, no need to loop.
|
||||||
result.addSpecialSlice(0, captures[0]);
|
result.addSpecialSlice(0, lastMatchInfo[CAPTURE0]);
|
||||||
result.add(ApplyReplacementFunction(replace, captures, subject));
|
var endOfMatch = lastMatchInfo[CAPTURE1];
|
||||||
result.addSpecialSlice(captures[1], subject.length);
|
result.add(ApplyReplacementFunction(replace, lastMatchInfo, subject));
|
||||||
|
// Can't use lastMatchInfo any more from here, since the function could
|
||||||
|
// overwrite it.
|
||||||
|
result.addSpecialSlice(endOfMatch, subject.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.generate();
|
return result.generate();
|
||||||
@ -445,20 +464,20 @@ function StringReplaceRegExpWithFunction(subject, regexp, replace) {
|
|||||||
|
|
||||||
|
|
||||||
// Helper function to apply a string replacement function once.
|
// Helper function to apply a string replacement function once.
|
||||||
function ApplyReplacementFunction(replace, captures, subject) {
|
function ApplyReplacementFunction(replace, lastMatchInfo, subject) {
|
||||||
// Compute the parameter list consisting of the match, captures, index,
|
// Compute the parameter list consisting of the match, captures, index,
|
||||||
// and subject for the replace function invocation.
|
// and subject for the replace function invocation.
|
||||||
var index = captures[0];
|
var index = lastMatchInfo[CAPTURE0];
|
||||||
// The number of captures plus one for the match.
|
// The number of captures plus one for the match.
|
||||||
var m = captures.length >> 1;
|
var m = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1;
|
||||||
if (m == 1) {
|
if (m == 1) {
|
||||||
var s = CaptureString(subject, captures, 0);
|
var s = CaptureString(subject, lastMatchInfo, 0);
|
||||||
// Don't call directly to avoid exposing the built-in global object.
|
// Don't call directly to avoid exposing the built-in global object.
|
||||||
return ToString(replace.call(null, s, index, subject));
|
return ToString(replace.call(null, s, index, subject));
|
||||||
}
|
}
|
||||||
var parameters = $Array(m + 2);
|
var parameters = $Array(m + 2);
|
||||||
for (var j = 0; j < m; j++) {
|
for (var j = 0; j < m; j++) {
|
||||||
parameters[j] = CaptureString(subject, captures, j);
|
parameters[j] = CaptureString(subject, lastMatchInfo, j);
|
||||||
}
|
}
|
||||||
parameters[j] = index;
|
parameters[j] = index;
|
||||||
parameters[j + 1] = subject;
|
parameters[j + 1] = subject;
|
||||||
@ -559,14 +578,14 @@ function StringSplit(separator, limit) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
var match = splitMatch(sep, subject, currentIndex, startIndex);
|
var lastMatchInfo = splitMatch(sep, subject, currentIndex, startIndex);
|
||||||
|
|
||||||
if (IS_NULL(match)) {
|
if (IS_NULL(lastMatchInfo)) {
|
||||||
result[result.length] = subject.slice(currentIndex, length);
|
result[result.length] = subject.slice(currentIndex, length);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
var endIndex = match[0];
|
var endIndex = lastMatchInfo[CAPTURE1];
|
||||||
|
|
||||||
// We ignore a zero-length match at the currentIndex.
|
// We ignore a zero-length match at the currentIndex.
|
||||||
if (startIndex === endIndex && endIndex === currentIndex) {
|
if (startIndex === endIndex && endIndex === currentIndex) {
|
||||||
@ -574,11 +593,20 @@ function StringSplit(separator, limit) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
result[result.length] = match[1];
|
result[result.length] =
|
||||||
|
SubString(subject, currentIndex, lastMatchInfo[CAPTURE0]);
|
||||||
if (result.length === lim) return result;
|
if (result.length === lim) return result;
|
||||||
|
|
||||||
for (var i = 2; i < match.length; i++) {
|
for (var i = 2; i < NUMBER_OF_CAPTURES(lastMatchInfo); i += 2) {
|
||||||
result[result.length] = match[i];
|
var start = lastMatchInfo[CAPTURE(i)];
|
||||||
|
var end = lastMatchInfo[CAPTURE(i + 1)];
|
||||||
|
if (start != -1 && end != -1) {
|
||||||
|
result[result.length] = SubString(subject,
|
||||||
|
lastMatchInfo[CAPTURE(i)],
|
||||||
|
lastMatchInfo[CAPTURE(i + 1)]);
|
||||||
|
} else {
|
||||||
|
result[result.length] = void 0;
|
||||||
|
}
|
||||||
if (result.length === lim) return result;
|
if (result.length === lim) return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -588,32 +616,24 @@ function StringSplit(separator, limit) {
|
|||||||
|
|
||||||
|
|
||||||
// ECMA-262 section 15.5.4.14
|
// ECMA-262 section 15.5.4.14
|
||||||
// Helper function used by split.
|
// Helper function used by split. This version returns the lastMatchInfo
|
||||||
|
// instead of allocating a new array with basically the same information.
|
||||||
function splitMatch(separator, subject, current_index, start_index) {
|
function splitMatch(separator, subject, current_index, start_index) {
|
||||||
if (IS_REGEXP(separator)) {
|
if (IS_REGEXP(separator)) {
|
||||||
var ovector = DoRegExpExec(separator, subject, start_index);
|
var lastMatchInfo = DoRegExpExec(separator, subject, start_index);
|
||||||
if (ovector == null) return null;
|
if (lastMatchInfo == null) return null;
|
||||||
var nof_results = ovector.length >> 1;
|
|
||||||
var result = new $Array(nof_results + 1);
|
|
||||||
// Section 15.5.4.14 paragraph two says that we do not allow zero length
|
// Section 15.5.4.14 paragraph two says that we do not allow zero length
|
||||||
// matches at the end of the string.
|
// matches at the end of the string.
|
||||||
if (ovector[0] === subject.length) return null;
|
if (lastMatchInfo[CAPTURE0] === subject.length) return null;
|
||||||
result[0] = ovector[1];
|
return lastMatchInfo;
|
||||||
result[1] = subject.slice(current_index, ovector[0]);
|
|
||||||
for (var i = 1; i < nof_results; i++) {
|
|
||||||
var matching_start = ovector[2*i];
|
|
||||||
var matching_end = ovector[2*i + 1];
|
|
||||||
if (matching_start != -1 && matching_end != -1) {
|
|
||||||
result[i + 1] = subject.slice(matching_start, matching_end);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var separatorIndex = subject.indexOf(separator, start_index);
|
var separatorIndex = subject.indexOf(separator, start_index);
|
||||||
if (separatorIndex === -1) return null;
|
if (separatorIndex === -1) return null;
|
||||||
|
|
||||||
return [ separatorIndex + separator.length, subject.slice(current_index, separatorIndex) ];
|
reusableMatchInfo[CAPTURE0] = separatorIndex;
|
||||||
|
reusableMatchInfo[CAPTURE1] = separatorIndex + separator.length;
|
||||||
|
return reusableMatchInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -154,3 +154,14 @@ for (var i = 1; i <= 9; i++) {
|
|||||||
}
|
}
|
||||||
assertEquals("", RegExp['$' + (i)], "$" + i);
|
assertEquals("", RegExp['$' + (i)], "$" + i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RegExp.multiline = "foo";
|
||||||
|
assertTrue(typeof RegExp.multiline == typeof Boolean(), "RegExp.multiline coerces values to booleans");
|
||||||
|
RegExp.input = Number();
|
||||||
|
assertTrue(typeof RegExp.input == typeof String(), "RegExp.input coerces values to booleans");
|
||||||
|
|
||||||
|
// Ensure that we save the correct string as the last subject when
|
||||||
|
// we do a match on a sliced string (the top one not the underlying).
|
||||||
|
var foo = "lsdfj sldkfj sdklfj læsdfjl sdkfjlsdk fjsdl fjsdljskdj flsj flsdkj flskd regexp: /foobar/\nldkfj sdlkfj sdkl";
|
||||||
|
assertTrue(/^([a-z]+): (.*)/.test(foo.substring(foo.indexOf("regexp:"))), "regexp: setup");
|
||||||
|
assertEquals("regexp", RegExp.$1, "RegExp.$1");
|
||||||
|
51
test/mjsunit/regexp-string-methods.js
Normal file
51
test/mjsunit/regexp-string-methods.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
||||||
|
// Redistribution and use in source and binary forms, with or without
|
||||||
|
// modification, are permitted provided that the following conditions are
|
||||||
|
// met:
|
||||||
|
//
|
||||||
|
// * Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
// * Redistributions in binary form must reproduce the above
|
||||||
|
// copyright notice, this list of conditions and the following
|
||||||
|
// disclaimer in the documentation and/or other materials provided
|
||||||
|
// with the distribution.
|
||||||
|
// * Neither the name of Google Inc. nor the names of its
|
||||||
|
// contributors may be used to endorse or promote products derived
|
||||||
|
// from this software without specific prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// We match other browsers in using the original value of RegExp.prototype.exec.
|
||||||
|
// I.e., RegExp.prototype.test shouldn't use the current value of
|
||||||
|
// RegExp.prototype.exec.
|
||||||
|
RegExp.prototype.exec = function(string) { return 'x'; }
|
||||||
|
assertFalse(/f/.test('x'));
|
@ -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):
|
|||||||
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:
|
||||||
|
Loading…
Reference in New Issue
Block a user