Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: src/objects.cc

Issue 21041: Refactor code for determining line position in a source file.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/messages.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 6763 matching lines...) Expand 10 before | Expand all | Expand 10 after
6774 return obj; 6774 return obj;
6775 } 6775 }
6776 6776
6777 6777
6778 // Init line_ends array with code positions of line ends inside script source 6778 // Init line_ends array with code positions of line ends inside script source
6779 void Script::InitLineEnds() { 6779 void Script::InitLineEnds() {
6780 if (!line_ends()->IsUndefined()) return; 6780 if (!line_ends()->IsUndefined()) return;
6781 6781
6782 Handle<String> src(String::cast(source())); 6782 Handle<String> src(String::cast(source()));
6783 const int src_len = src->length(); 6783 const int src_len = src->length();
6784 Handle<JSArray> array = Factory::NewJSArray(0);
6785 int array_index = 0;
6786 Handle<String> new_line = Factory::NewStringFromAscii(CStrVector("\n")); 6784 Handle<String> new_line = Factory::NewStringFromAscii(CStrVector("\n"));
6785
6786 // Pass 1: Identify line count
6787 int line_count = 0;
6787 int position = 0; 6788 int position = 0;
6788 while (position < src_len) { 6789 while (position != -1 && position < src_len) {
6789 position = Runtime::StringMatch(src, new_line, position); 6790 position = Runtime::StringMatch(src, new_line, position);
6790 if (position != -1) { 6791 if (position != -1) {
6791 SetElement(array, array_index++, Handle<Smi>(Smi::FromInt(position++))); 6792 position++;
6792 } else {
6793 break;
6794 } 6793 }
6794 // Even if the last line misses a line end, it is counted
6795 line_count++;
6795 } 6796 }
6796 6797
6797 // If the script does not end with a line ending add the final end position 6798 // Pass 2: Fill in line ends positions
6798 // as just past the last line ending. 6799 Handle<FixedArray> array = Factory::NewFixedArray(line_count);
6799 if (array_index == 0 || 6800 int array_index = 0;
6800 (Smi::cast(array->GetElement(array_index - 1))->value() != src_len - 1)) { 6801 position = 0;
6801 SetElement(array, array_index++, Handle<Smi>(Smi::FromInt(src_len))); 6802 while (position != -1 && position < src_len) {
6803 position = Runtime::StringMatch(src, new_line, position);
6804 // If the script does not end with a line ending add the final end position
6805 // as just past the last line ending.
6806 array->set(array_index++,
6807 Smi::FromInt(position != -1 ? position++ : src_len));
6802 } 6808 }
6809 ASSERT(array_index == line_count);
6803 6810
6804 Handle<FixedArray> fixed_array = Factory::NewFixedArray(0); 6811 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
6805 set_line_ends(*AddKeysFromJSArray(fixed_array, array)); 6812 set_line_ends(*object);
6806 ASSERT(line_ends()->IsFixedArray()); 6813 ASSERT(line_ends()->IsJSArray());
6807 } 6814 }
6808 6815
6809 6816
6810 // Convert code position into line number 6817 // Convert code position into line number
6811 int Script::GetLineNumber(int code_pos) { 6818 int Script::GetLineNumber(int code_pos) {
6812 InitLineEnds(); 6819 InitLineEnds();
6813 FixedArray* line_ends_array = FixedArray::cast(line_ends()); 6820 JSArray* line_ends_array = JSArray::cast(line_ends());
6821 const int line_ends_len = (Smi::cast(line_ends_array->length()))->value();
6814 6822
6815 int line = -1; 6823 int line = -1;
6816 if (code_pos <= (Smi::cast(line_ends_array->get(0)))->value()) { 6824 if (line_ends_len > 0 &&
6825 code_pos <= (Smi::cast(line_ends_array->GetElement(0)))->value()) {
6817 line = 0; 6826 line = 0;
6818 } else { 6827 } else {
6819 const int line_ends_length = line_ends_array->length(); 6828 for (int i = 1; i < line_ends_len; ++i) {
6820 for (int i = 1; i < line_ends_length; ++i) { 6829 if ((Smi::cast(line_ends_array->GetElement(i - 1)))->value() < code_pos &&
6821 if ((Smi::cast(line_ends_array->get(i - 1)))->value() < code_pos && 6830 code_pos <= (Smi::cast(line_ends_array->GetElement(i)))->value()) {
6822 code_pos <= (Smi::cast(line_ends_array->get(i)))->value()) {
6823 line = i; 6831 line = i;
6824 break; 6832 break;
6825 } 6833 }
6826 } 6834 }
6827 } 6835 }
6828 6836
6829 return line != -1 ? line + line_offset()->value() : line; 6837 return line != -1 ? line + line_offset()->value() : line;
6830 } 6838 }
6831 6839
6832 6840
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
7069 // No break point. 7077 // No break point.
7070 if (break_point_objects()->IsUndefined()) return 0; 7078 if (break_point_objects()->IsUndefined()) return 0;
7071 // Single beak point. 7079 // Single beak point.
7072 if (!break_point_objects()->IsFixedArray()) return 1; 7080 if (!break_point_objects()->IsFixedArray()) return 1;
7073 // Multiple break points. 7081 // Multiple break points.
7074 return FixedArray::cast(break_point_objects())->length(); 7082 return FixedArray::cast(break_point_objects())->length();
7075 } 7083 }
7076 7084
7077 7085
7078 } } // namespace v8::internal 7086 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/messages.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698