Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/object.h" | 5 #include "vm/object.h" |
| 6 | 6 |
| 7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
| 8 #include "vm/assembler.h" | 8 #include "vm/assembler.h" |
| 9 #include "vm/bigint_operations.h" | 9 #include "vm/bigint_operations.h" |
| 10 #include "vm/bootstrap.h" | 10 #include "vm/bootstrap.h" |
| (...skipping 1566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1577 if (function_name.Equals(name) || MatchesPrivateName(function_name, name)) { | 1577 if (function_name.Equals(name) || MatchesPrivateName(function_name, name)) { |
| 1578 return function.raw(); | 1578 return function.raw(); |
| 1579 } | 1579 } |
| 1580 } | 1580 } |
| 1581 | 1581 |
| 1582 // No function found. | 1582 // No function found. |
| 1583 return Function::null(); | 1583 return Function::null(); |
| 1584 } | 1584 } |
| 1585 | 1585 |
| 1586 | 1586 |
| 1587 RawFunction* Class::LookupFunctionAtToken(intptr_t token_index) const { | |
| 1588 // TODO(hausner): we can shortcut the negative case if we knew the | |
| 1589 // beginning and end token position of the class. | |
| 1590 Array& funcs = Array::Handle(functions()); | |
| 1591 Function& func = Function::Handle(); | |
| 1592 intptr_t len = funcs.Length(); | |
| 1593 for (intptr_t i = 0; i < len; i++) { | |
| 1594 func ^= funcs.At(i); | |
| 1595 if ((func.token_index() <= token_index) && | |
| 1596 (token_index < func.end_token_index())) { | |
|
siva
2012/01/18 01:32:03
Maybe function could have a method
bool containsTo
hausner
2012/01/18 23:26:38
Yes, but I don't think the code becomes more reada
| |
| 1597 return func.raw(); | |
| 1598 } | |
| 1599 } | |
| 1600 | |
| 1601 // No function found. | |
| 1602 return Function::null(); | |
| 1603 } | |
| 1604 | |
| 1605 | |
| 1587 RawField* Class::LookupInstanceField(const String& name) const { | 1606 RawField* Class::LookupInstanceField(const String& name) const { |
| 1588 ASSERT(is_finalized()); | 1607 ASSERT(is_finalized()); |
| 1589 const Field& field = Field::Handle(LookupField(name)); | 1608 const Field& field = Field::Handle(LookupField(name)); |
| 1590 if (!field.IsNull()) { | 1609 if (!field.IsNull()) { |
| 1591 if (field.is_static()) { | 1610 if (field.is_static()) { |
| 1592 // Name matches but it is not of the correct kind, return NULL. | 1611 // Name matches but it is not of the correct kind, return NULL. |
| 1593 return Field::null(); | 1612 return Field::null(); |
| 1594 } | 1613 } |
| 1595 return field.raw(); | 1614 return field.raw(); |
| 1596 } | 1615 } |
| (...skipping 1636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3233 bool is_const, | 3252 bool is_const, |
| 3234 intptr_t token_index) { | 3253 intptr_t token_index) { |
| 3235 const Function& result = Function::Handle(Function::New()); | 3254 const Function& result = Function::Handle(Function::New()); |
| 3236 result.set_parameter_types(Array::Handle(Array::Empty())); | 3255 result.set_parameter_types(Array::Handle(Array::Empty())); |
| 3237 result.set_parameter_names(Array::Handle(Array::Empty())); | 3256 result.set_parameter_names(Array::Handle(Array::Empty())); |
| 3238 result.set_name(name); | 3257 result.set_name(name); |
| 3239 result.set_kind(kind); | 3258 result.set_kind(kind); |
| 3240 result.set_is_static(is_static); | 3259 result.set_is_static(is_static); |
| 3241 result.set_is_const(is_const); | 3260 result.set_is_const(is_const); |
| 3242 result.set_token_index(token_index); | 3261 result.set_token_index(token_index); |
| 3262 result.set_end_token_index(token_index); | |
| 3243 result.set_num_fixed_parameters(0); | 3263 result.set_num_fixed_parameters(0); |
| 3244 result.set_num_optional_parameters(0); | 3264 result.set_num_optional_parameters(0); |
| 3245 result.set_invocation_counter(0); | 3265 result.set_invocation_counter(0); |
| 3246 result.set_deoptimization_counter(0); | 3266 result.set_deoptimization_counter(0); |
| 3247 result.set_is_optimizable(true); | 3267 result.set_is_optimizable(true); |
| 3248 return result.raw(); | 3268 return result.raw(); |
| 3249 } | 3269 } |
| 3250 | 3270 |
| 3251 | 3271 |
| 3252 RawFunction* Function::NewClosureFunction(const String& name, | 3272 RawFunction* Function::NewClosureFunction(const String& name, |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3695 intptr_t* column) const { | 3715 intptr_t* column) const { |
| 3696 const String& src = String::Handle(source()); | 3716 const String& src = String::Handle(source()); |
| 3697 const String& dummy_key = String::Handle(String::New("")); | 3717 const String& dummy_key = String::Handle(String::New("")); |
| 3698 Scanner scanner(src, dummy_key); | 3718 Scanner scanner(src, dummy_key); |
| 3699 scanner.ScanTo(token_index); | 3719 scanner.ScanTo(token_index); |
| 3700 *line = scanner.CurrentPosition().line; | 3720 *line = scanner.CurrentPosition().line; |
| 3701 *column = scanner.CurrentPosition().column; | 3721 *column = scanner.CurrentPosition().column; |
| 3702 } | 3722 } |
| 3703 | 3723 |
| 3704 | 3724 |
| 3725 intptr_t Script::TokenIndexAtLine(intptr_t line_number) const { | |
| 3726 const String& src = String::Handle(source()); | |
| 3727 const String& dummy_key = String::Handle(String::New("")); | |
| 3728 Scanner scanner(src, dummy_key); | |
| 3729 return scanner.TokenIndexAtLine(line_number); | |
| 3730 } | |
| 3731 | |
| 3732 | |
| 3705 RawString* Script::GetLine(intptr_t line_number) const { | 3733 RawString* Script::GetLine(intptr_t line_number) const { |
| 3706 const String& src = String::Handle(source()); | 3734 const String& src = String::Handle(source()); |
| 3707 intptr_t current_line = 1; | 3735 intptr_t current_line = 1; |
| 3708 intptr_t line_start = -1; | 3736 intptr_t line_start = -1; |
| 3709 intptr_t last_char = -1; | 3737 intptr_t last_char = -1; |
| 3710 for (intptr_t ix = 0; | 3738 for (intptr_t ix = 0; |
| 3711 (ix < src.Length()) && (current_line <= line_number); | 3739 (ix < src.Length()) && (current_line <= line_number); |
| 3712 ix++) { | 3740 ix++) { |
| 3713 if ((current_line == line_number) && (line_start < 0)) { | 3741 if ((current_line == line_number) && (line_start < 0)) { |
| 3714 line_start = ix; | 3742 line_start = ix; |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3974 } | 4002 } |
| 3975 | 4003 |
| 3976 | 4004 |
| 3977 void Library::AddClass(const Class& cls) const { | 4005 void Library::AddClass(const Class& cls) const { |
| 3978 AddObject(cls, String::Handle(cls.Name())); | 4006 AddObject(cls, String::Handle(cls.Name())); |
| 3979 // Link class to this library. | 4007 // Link class to this library. |
| 3980 cls.set_library(*this); | 4008 cls.set_library(*this); |
| 3981 } | 4009 } |
| 3982 | 4010 |
| 3983 | 4011 |
| 4012 // TODO(hausner): we might want to add a script dictionary to the | |
| 4013 // library class to make this lookup less cumbersome. | |
| 4014 RawScript* Library::LookupScript(const String& url) const { | |
| 4015 Object& entry = Object::Handle(); | |
| 4016 Class& cls = Class::Handle(); | |
| 4017 Function& func = Function::Handle(); | |
| 4018 Field& field = Field::Handle(); | |
| 4019 Script& owner_script = Script::Handle(); | |
| 4020 String& owner_url = String::Handle(); | |
| 4021 | |
| 4022 DictionaryIterator it(*this); | |
| 4023 while (it.HasNext()) { | |
| 4024 entry = it.GetNext(); | |
| 4025 if (entry.IsClass()) { | |
| 4026 cls ^= entry.raw(); | |
| 4027 } else if (entry.IsFunction()) { | |
| 4028 func ^= entry.raw(); | |
| 4029 cls = func.owner(); | |
| 4030 } else if (entry.IsField()) { | |
| 4031 field ^= entry.raw(); | |
| 4032 cls = field.owner(); | |
| 4033 } else { | |
| 4034 continue; | |
| 4035 } | |
| 4036 owner_script = cls.script(); | |
| 4037 if (owner_script.IsNull()) { | |
| 4038 continue; | |
| 4039 } | |
| 4040 owner_url = owner_script.url(); | |
| 4041 if (owner_url.Equals(url)) { | |
| 4042 return owner_script.raw(); | |
| 4043 } | |
| 4044 } | |
| 4045 return Script::null(); | |
| 4046 } | |
| 4047 | |
| 4048 | |
| 4049 RawFunction* Library::LookupFunctionInSource(const String& script_url, | |
| 4050 intptr_t line_number) const { | |
| 4051 Script& script = Script::Handle(LookupScript(script_url)); | |
| 4052 if (script.IsNull()) { | |
| 4053 // The given script url is not loaded into this library. | |
| 4054 return Function::null(); | |
| 4055 } | |
| 4056 | |
| 4057 // Determine token position at given line number. | |
| 4058 intptr_t token_index_at_line = script.TokenIndexAtLine(line_number); | |
| 4059 if (token_index_at_line < 0) { | |
| 4060 // Script does not contain the given line number. | |
| 4061 return Function::null(); | |
| 4062 } | |
| 4063 | |
| 4064 return LookupFunctionInScript(script, token_index_at_line); | |
| 4065 } | |
|
siva
2012/01/18 01:32:03
It might be worthwhile to add an unit test for thi
hausner
2012/01/18 23:26:38
Will do in a later change. I gave it a try but ran
| |
| 4066 | |
| 4067 | |
| 4068 RawFunction* Library::LookupFunctionInScript(const Script& script, | |
| 4069 intptr_t token_index) const { | |
| 4070 Object& entry = Object::Handle(); | |
| 4071 Class& cls = Class::Handle(); | |
| 4072 Function& func = Function::Handle(); | |
| 4073 DictionaryIterator it(*this); | |
| 4074 while (it.HasNext()) { | |
| 4075 entry = it.GetNext(); | |
| 4076 if (entry.IsFunction()) { | |
| 4077 func ^= entry.raw(); | |
| 4078 cls = func.owner(); | |
| 4079 if (script.raw() == cls.script()) { | |
| 4080 if ((func.token_index() <= token_index) && | |
| 4081 (token_index < func.end_token_index())) { | |
| 4082 return func.raw(); | |
| 4083 } | |
| 4084 } | |
| 4085 } else if (entry.IsClass()) { | |
| 4086 cls ^= entry.raw(); | |
| 4087 if (script.raw() == cls.script()) { | |
| 4088 func = cls.LookupFunctionAtToken(token_index); | |
| 4089 if (!func.IsNull()) { | |
| 4090 return func.raw(); | |
| 4091 } | |
| 4092 } | |
| 4093 } | |
| 4094 } | |
| 4095 return Function::null(); | |
| 4096 } | |
| 4097 | |
| 4098 | |
| 3984 RawObject* Library::LookupLocalObject(const String& name) const { | 4099 RawObject* Library::LookupLocalObject(const String& name) const { |
| 3985 const Array& dict = Array::Handle(dictionary()); | 4100 const Array& dict = Array::Handle(dictionary()); |
| 3986 intptr_t dict_size = dict.Length() - 1; | 4101 intptr_t dict_size = dict.Length() - 1; |
| 3987 intptr_t index = name.Hash() % dict_size; | 4102 intptr_t index = name.Hash() % dict_size; |
| 3988 | 4103 |
| 3989 Object& entry = Object::Handle(); | 4104 Object& entry = Object::Handle(); |
| 3990 Class& cls = Class::Handle(); | 4105 Class& cls = Class::Handle(); |
| 3991 Function& func = Function::Handle(); | 4106 Function& func = Function::Handle(); |
| 3992 Field& field = Field::Handle(); | 4107 Field& field = Field::Handle(); |
| 3993 LibraryPrefix& library_prefix = LibraryPrefix::Handle(); | 4108 LibraryPrefix& library_prefix = LibraryPrefix::Handle(); |
| (...skipping 3615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 7609 const String& str = String::Handle(pattern()); | 7724 const String& str = String::Handle(pattern()); |
| 7610 const char* format = "JSRegExp: pattern=%s flags=%s"; | 7725 const char* format = "JSRegExp: pattern=%s flags=%s"; |
| 7611 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); | 7726 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); |
| 7612 char* chars = reinterpret_cast<char*>( | 7727 char* chars = reinterpret_cast<char*>( |
| 7613 Isolate::Current()->current_zone()->Allocate(len + 1)); | 7728 Isolate::Current()->current_zone()->Allocate(len + 1)); |
| 7614 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); | 7729 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); |
| 7615 return chars; | 7730 return chars; |
| 7616 } | 7731 } |
| 7617 | 7732 |
| 7618 } // namespace dart | 7733 } // namespace dart |
| OLD | NEW |