Index: src/objects.cc |
=================================================================== |
--- src/objects.cc (revision 1220) |
+++ src/objects.cc (working copy) |
@@ -6781,45 +6781,53 @@ |
Handle<String> src(String::cast(source())); |
const int src_len = src->length(); |
- Handle<JSArray> array = Factory::NewJSArray(0); |
- int array_index = 0; |
Handle<String> new_line = Factory::NewStringFromAscii(CStrVector("\n")); |
+ |
+ // Pass 1: Identify line count |
+ int line_count = 0; |
int position = 0; |
- while (position < src_len) { |
+ while (position != -1 && position < src_len) { |
position = Runtime::StringMatch(src, new_line, position); |
if (position != -1) { |
- SetElement(array, array_index++, Handle<Smi>(Smi::FromInt(position++))); |
- } else { |
- break; |
+ position++; |
} |
+ // Even if the last line misses a line end, it is counted |
+ line_count++; |
} |
- // If the script does not end with a line ending add the final end position |
- // as just past the last line ending. |
- if (array_index == 0 || |
- (Smi::cast(array->GetElement(array_index - 1))->value() != src_len - 1)) { |
- SetElement(array, array_index++, Handle<Smi>(Smi::FromInt(src_len))); |
+ // Pass 2: Fill in line ends positions |
+ Handle<FixedArray> array = Factory::NewFixedArray(line_count); |
+ int array_index = 0; |
+ position = 0; |
+ while (position != -1 && position < src_len) { |
+ position = Runtime::StringMatch(src, new_line, position); |
+ // If the script does not end with a line ending add the final end position |
+ // as just past the last line ending. |
+ array->set(array_index++, |
+ Smi::FromInt(position != -1 ? position++ : src_len)); |
} |
+ ASSERT(array_index == line_count); |
- Handle<FixedArray> fixed_array = Factory::NewFixedArray(0); |
- set_line_ends(*AddKeysFromJSArray(fixed_array, array)); |
- ASSERT(line_ends()->IsFixedArray()); |
+ Handle<JSArray> object = Factory::NewJSArrayWithElements(array); |
+ set_line_ends(*object); |
+ ASSERT(line_ends()->IsJSArray()); |
} |
// Convert code position into line number |
int Script::GetLineNumber(int code_pos) { |
InitLineEnds(); |
- FixedArray* line_ends_array = FixedArray::cast(line_ends()); |
+ JSArray* line_ends_array = JSArray::cast(line_ends()); |
+ const int line_ends_len = (Smi::cast(line_ends_array->length()))->value(); |
int line = -1; |
- if (code_pos <= (Smi::cast(line_ends_array->get(0)))->value()) { |
+ if (line_ends_len > 0 && |
+ code_pos <= (Smi::cast(line_ends_array->GetElement(0)))->value()) { |
line = 0; |
} else { |
- const int line_ends_length = line_ends_array->length(); |
- for (int i = 1; i < line_ends_length; ++i) { |
- if ((Smi::cast(line_ends_array->get(i - 1)))->value() < code_pos && |
- code_pos <= (Smi::cast(line_ends_array->get(i)))->value()) { |
+ for (int i = 1; i < line_ends_len; ++i) { |
+ if ((Smi::cast(line_ends_array->GetElement(i - 1)))->value() < code_pos && |
+ code_pos <= (Smi::cast(line_ends_array->GetElement(i)))->value()) { |
line = i; |
break; |
} |