Add ability for Function to generate source

BUG=
R=hausner@google.com

Review URL: https://codereview.chromium.org//60993002

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@30239 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
johnmccutchan@google.com 2013-11-13 18:09:41 +00:00
parent 6f3b8e6493
commit 70483ee91e
3 changed files with 26 additions and 2 deletions

View file

@ -50,7 +50,8 @@ TEST_CASE(CompileFunction) {
Function& function_foo =
Function::Handle(cls.LookupStaticFunction(function_foo_name));
EXPECT(!function_foo.IsNull());
String& function_source = String::Handle(function_foo.GetSource());
EXPECT_STREQ("static foo() { return 42; }\n ", function_source.ToCString());
EXPECT(CompilerTest::TestCompileFunction(function_foo));
EXPECT(function_foo.HasCode());
@ -61,6 +62,9 @@ TEST_CASE(CompileFunction) {
EXPECT(CompilerTest::TestCompileFunction(function_moo));
EXPECT(function_moo.HasCode());
function_source = function_moo.GetSource();
EXPECT_STREQ("static moo() {\n // A.foo();\n }\n",
function_source.ToCString());
}

View file

@ -5447,6 +5447,13 @@ RawString* Function::QualifiedUserVisibleName() const {
}
RawString* Function::GetSource() {
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);
}
// Construct fingerprint from token stream. The token stream contains also
// arguments.
int32_t Function::SourceFingerprint() const {
@ -6968,6 +6975,16 @@ 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,

View file

@ -1443,6 +1443,8 @@ class Function : public Object {
RawString* QualifiedUserVisibleName() const;
virtual RawString* DictionaryName() const { return name(); }
RawString* GetSource();
// Build a string of the form 'C<T, R>(T, {b: B, c: C}) => R' representing the
// internal signature of the given function. In this example, T and R are
// type parameters of class C, the owner of the function.
@ -2368,7 +2370,8 @@ class Script : public Object {
void Tokenize(const String& private_key) const;
RawString* GetLine(intptr_t line_number) const;
RawString* GetSnippet(intptr_t from_token_pos,
intptr_t to_token_pos) const;
RawString* GetSnippet(intptr_t from_line,
intptr_t from_column,
intptr_t to_line,