Index: src/objects.cc |
diff --git a/src/objects.cc b/src/objects.cc |
index 01b4a1aca09f61e9504b67a41f7f6abb66971e39..13952e838b0d6637c047000ae02872ee8da5d214 100644 |
--- a/src/objects.cc |
+++ b/src/objects.cc |
@@ -9932,27 +9932,31 @@ int Script::GetColumnNumber(Handle<Script> script, int code_pos) { |
} |
-int Script::GetLineNumberWithArray(int code_pos) { |
+int Script::GetLineNumberWithArray(int position) { |
DisallowHeapAllocation no_allocation; |
- DCHECK(line_ends()->IsFixedArray()); |
- FixedArray* line_ends_array = FixedArray::cast(line_ends()); |
- int line_ends_len = line_ends_array->length(); |
- if (line_ends_len == 0) return -1; |
+ FixedArray* line_ends = FixedArray::cast(this->line_ends()); |
+ int upper = line_ends->length() - 1; |
+ if (upper < 0) return -1; |
+ int offset = line_offset()->value(); |
- if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) { |
- return line_offset()->value(); |
+ if (position > Smi::cast(line_ends->get(upper))->value()) { |
+ return upper + 1 + offset; |
} |
+ if (position <= Smi::cast(line_ends->get(0))->value()) return offset; |
- int left = 0; |
- int right = line_ends_len; |
- while (int half = (right - left) / 2) { |
- if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) { |
- right -= half; |
+ int lower = 1; |
+ // Binary search. |
+ while (true) { |
+ int mid = (lower + upper) / 2; |
+ if (position <= Smi::cast(line_ends->get(mid - 1))->value()) { |
+ upper = mid - 1; |
+ } else if (position > Smi::cast(line_ends->get(mid))->value()) { |
+ lower = mid + 1; |
} else { |
- left += half; |
+ return mid + offset; |
} |
} |
- return right + line_offset()->value(); |
+ return -1; |
} |