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

Unified Diff: runtime/vm/object.cc

Issue 1071703002: Remove some duplication in code that extracts the source for a function. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 8 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 side-by-side diff with in-line comments
Download patch
« runtime/vm/compiler_test.cc ('K') | « runtime/vm/object.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/object.cc
diff --git a/runtime/vm/object.cc b/runtime/vm/object.cc
index 0fda4e48c5fdea78e20feddb11eb607bde8ba93b..684f3eb746f3d71fd6b4f8c58ac6e65a1875ee73 100644
--- a/runtime/vm/object.cc
+++ b/runtime/vm/object.cc
@@ -6598,10 +6598,46 @@ RawString* Function::QualifiedUserVisibleName() const {
}
-RawString* Function::GetSource() {
+RawString* Function::GetSource() const {
+ if (IsImplicitConstructor() || IsSignatureFunction()) {
+ // We may need to handle more cases when the restrictions on mixins are
+ // relaxed. In particular we might start associating some source with the
+ // forwarding constructors when it becomes possible to specify a particular
+ // constructor from the mixin to use.
+ return String::null();
+ }
const Script& func_script = Script::Handle(script());
- // Without the + 1 the final "}" is not included.
- return func_script.GetSnippet(token_pos(), end_token_pos() + 1);
+ const TokenStream& stream = TokenStream::Handle(func_script.tokens());
+ if (!func_script.HasSource()) {
+ // When source is not available, avoid printing the whole token stream and
+ // doing expensive position calculations.
+ return stream.GenerateSource(token_pos(), end_token_pos() + 1);
+ }
+
+ const TokenStream::Iterator tkit(stream, end_token_pos());
+ intptr_t from_line;
+ intptr_t from_col;
+ intptr_t to_line;
+ intptr_t to_col;
+ func_script.GetTokenLocation(token_pos(), &from_line, &from_col);
+ func_script.GetTokenLocation(end_token_pos(), &to_line, &to_col);
+ intptr_t last_tok_len = String::Handle(tkit.CurrentLiteral()).Length();
+ // Handle special cases for end tokens of closures (where we exclude the last
+ // token):
+ // (1) "foo(() => null, bar);": End token is `,', but we don't print it.
+ // (2) "foo(() => null);": End token is ')`, but we don't print it.
+ // (3) "var foo = () => null;": End token is `;', but in this case the token
+ // semicolon belongs to the assignment so we skip it.
+ if ((tkit.CurrentTokenKind() == Token::kCOMMA) || // Case 1.
+ (tkit.CurrentTokenKind() == Token::kRPAREN) || // Case 2.
+ (tkit.CurrentTokenKind() == Token::kSEMICOLON &&
+ String::Handle(name()).Equals("<anonymous closure>"))) { // Case 3.
+ last_tok_len = 0;
+ }
+ const String& result = String::Handle(func_script.GetSnippet(
+ from_line, from_col, to_line, to_col + last_tok_len));
+ ASSERT(!result.IsNull());
+ return result.raw();
}
@@ -8401,16 +8437,6 @@ RawString* Script::GetLine(intptr_t line_number) const {
}
-RawString* Script::GetSnippet(intptr_t from_token_pos,
- intptr_t to_token_pos) const {
- intptr_t from_line, from_column;
- intptr_t to_line, to_column;
- GetTokenLocation(from_token_pos, &from_line, &from_column);
- GetTokenLocation(to_token_pos, &to_line, &to_column);
- return GetSnippet(from_line, from_column, to_line, to_column);
-}
-
-
RawString* Script::GetSnippet(intptr_t from_line,
intptr_t from_column,
intptr_t to_line,
« runtime/vm/compiler_test.cc ('K') | « runtime/vm/object.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698