OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/ast.h" | 7 #include "src/ast.h" |
8 #include "src/ast-value-factory.h" | |
9 #include "src/func-name-inferrer.h" | 8 #include "src/func-name-inferrer.h" |
10 #include "src/list-inl.h" | 9 #include "src/list-inl.h" |
11 | 10 |
12 namespace v8 { | 11 namespace v8 { |
13 namespace internal { | 12 namespace internal { |
14 | 13 |
15 FuncNameInferrer::FuncNameInferrer(AstValueFactory* ast_value_factory, | 14 FuncNameInferrer::FuncNameInferrer(Isolate* isolate, Zone* zone) |
16 Zone* zone) | 15 : isolate_(isolate), |
17 : ast_value_factory_(ast_value_factory), | |
18 entries_stack_(10, zone), | 16 entries_stack_(10, zone), |
19 names_stack_(5, zone), | 17 names_stack_(5, zone), |
20 funcs_to_infer_(4, zone), | 18 funcs_to_infer_(4, zone), |
21 zone_(zone) { | 19 zone_(zone) { |
22 } | 20 } |
23 | 21 |
24 | 22 |
25 void FuncNameInferrer::PushEnclosingName(const AstString* name) { | 23 void FuncNameInferrer::PushEnclosingName(Handle<String> name) { |
26 // Enclosing name is a name of a constructor function. To check | 24 // Enclosing name is a name of a constructor function. To check |
27 // that it is really a constructor, we check that it is not empty | 25 // that it is really a constructor, we check that it is not empty |
28 // and starts with a capital letter. | 26 // and starts with a capital letter. |
29 if (!name->IsEmpty() && unibrow::Uppercase::Is(name->FirstCharacter())) { | 27 if (name->length() > 0 && Runtime::IsUpperCaseChar( |
| 28 isolate()->runtime_state(), name->Get(0))) { |
30 names_stack_.Add(Name(name, kEnclosingConstructorName), zone()); | 29 names_stack_.Add(Name(name, kEnclosingConstructorName), zone()); |
31 } | 30 } |
32 } | 31 } |
33 | 32 |
34 | 33 |
35 void FuncNameInferrer::PushLiteralName(const AstString* name) { | 34 void FuncNameInferrer::PushLiteralName(Handle<String> name) { |
36 if (IsOpen() && name != ast_value_factory_->prototype_string()) { | 35 if (IsOpen() && |
| 36 !String::Equals(isolate()->factory()->prototype_string(), name)) { |
37 names_stack_.Add(Name(name, kLiteralName), zone()); | 37 names_stack_.Add(Name(name, kLiteralName), zone()); |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 | 41 |
42 void FuncNameInferrer::PushVariableName(const AstString* name) { | 42 void FuncNameInferrer::PushVariableName(Handle<String> name) { |
43 if (IsOpen() && name != ast_value_factory_->dot_result_string()) { | 43 if (IsOpen() && |
| 44 !String::Equals(isolate()->factory()->dot_result_string(), name)) { |
44 names_stack_.Add(Name(name, kVariableName), zone()); | 45 names_stack_.Add(Name(name, kVariableName), zone()); |
45 } | 46 } |
46 } | 47 } |
47 | 48 |
48 | 49 |
49 const AstString* FuncNameInferrer::MakeNameFromStack() { | 50 Handle<String> FuncNameInferrer::MakeNameFromStack() { |
50 // First see how many names we will use. | 51 return MakeNameFromStackHelper(0, isolate()->factory()->empty_string()); |
51 int length = 0; | 52 } |
52 bool one_byte = true; | 53 |
53 int pos = 0; | 54 |
54 while (pos < names_stack_.length()) { | 55 Handle<String> FuncNameInferrer::MakeNameFromStackHelper(int pos, |
55 if (pos < names_stack_.length() - 1 && | 56 Handle<String> prev) { |
56 names_stack_.at(pos).type == kVariableName && | 57 if (pos >= names_stack_.length()) return prev; |
57 names_stack_.at(pos + 1).type == kVariableName) { | 58 if (pos < names_stack_.length() - 1 && |
58 // Skip consecutive variable declarations. | 59 names_stack_.at(pos).type == kVariableName && |
59 ++pos; | 60 names_stack_.at(pos + 1).type == kVariableName) { |
60 continue; | 61 // Skip consecutive variable declarations. |
| 62 return MakeNameFromStackHelper(pos + 1, prev); |
| 63 } else { |
| 64 if (prev->length() > 0) { |
| 65 Handle<String> name = names_stack_.at(pos).name; |
| 66 if (prev->length() + name->length() + 1 > String::kMaxLength) return prev; |
| 67 Factory* factory = isolate()->factory(); |
| 68 Handle<String> curr = |
| 69 factory->NewConsString(factory->dot_string(), name).ToHandleChecked(); |
| 70 curr = factory->NewConsString(prev, curr).ToHandleChecked(); |
| 71 return MakeNameFromStackHelper(pos + 1, curr); |
| 72 } else { |
| 73 return MakeNameFromStackHelper(pos + 1, names_stack_.at(pos).name); |
61 } | 74 } |
62 int cur_length = names_stack_.at(pos).name->length(); | |
63 if (length + 1 + cur_length > String::kMaxLength) { | |
64 break; | |
65 } | |
66 if (length == 0) { | |
67 length = cur_length; | |
68 } else { // Add the . between names. | |
69 length += (1 + cur_length); | |
70 } | |
71 one_byte = one_byte && names_stack_.at(pos).name->is_one_byte(); | |
72 ++pos; | |
73 } | 75 } |
74 const AstString* to_return = NULL; | |
75 const char* dot = "."; | |
76 if (one_byte) { | |
77 Vector<uint8_t> new_name = Vector<uint8_t>::New(length); | |
78 int name_pos = 0; | |
79 for (int i = 0; i < pos; ++i) { | |
80 if (i < names_stack_.length() - 1 && | |
81 names_stack_.at(i).type == kVariableName && | |
82 names_stack_.at(i + 1).type == kVariableName) { | |
83 // Skip consecutive variable declarations. | |
84 continue; | |
85 } | |
86 if (name_pos != 0) { | |
87 CopyChars(new_name.start() + name_pos, dot, 1); | |
88 ++name_pos; | |
89 } | |
90 CopyChars(new_name.start() + name_pos, | |
91 names_stack_.at(i).name->raw_data(), | |
92 names_stack_.at(i).name->length()); | |
93 name_pos += names_stack_.at(i).name->length(); | |
94 } | |
95 to_return = ast_value_factory_->GetOneByteString(Vector<const uint8_t>( | |
96 reinterpret_cast<const uint8_t*>(new_name.start()), | |
97 new_name.length())); | |
98 new_name.Dispose(); | |
99 } else { | |
100 Vector<uint16_t> new_name = Vector<uint16_t>::New(length); | |
101 int name_pos = 0; | |
102 for (int i = 0; i < pos; ++i) { | |
103 if (i < names_stack_.length() - 1 && | |
104 names_stack_.at(i).type == kVariableName && | |
105 names_stack_.at(i + 1).type == kVariableName) { | |
106 // Skip consecutive variable declarations. | |
107 continue; | |
108 } | |
109 if (name_pos != 0) { | |
110 CopyChars(new_name.start() + name_pos, dot, 1); | |
111 ++name_pos; | |
112 } | |
113 if (names_stack_.at(i).name->is_one_byte()) { | |
114 CopyChars(new_name.start() + name_pos, | |
115 names_stack_.at(i).name->raw_data(), | |
116 names_stack_.at(i).name->length()); | |
117 } else { | |
118 CopyChars(new_name.start() + name_pos, | |
119 reinterpret_cast<const uint16_t*>( | |
120 names_stack_.at(i).name->raw_data()), | |
121 names_stack_.at(i).name->length()); | |
122 } | |
123 name_pos += names_stack_.at(i).name->length(); | |
124 } | |
125 to_return = ast_value_factory_->GetTwoByteString(Vector<const uint16_t>( | |
126 reinterpret_cast<const uint16_t*>(new_name.start()), | |
127 new_name.length())); | |
128 new_name.Dispose(); | |
129 } | |
130 return to_return; | |
131 } | 76 } |
132 | 77 |
133 | 78 |
134 void FuncNameInferrer::InferFunctionsNames() { | 79 void FuncNameInferrer::InferFunctionsNames() { |
135 const AstString* func_name = MakeNameFromStack(); | 80 Handle<String> func_name = MakeNameFromStack(); |
136 for (int i = 0; i < funcs_to_infer_.length(); ++i) { | 81 for (int i = 0; i < funcs_to_infer_.length(); ++i) { |
137 funcs_to_infer_[i]->set_raw_inferred_name(func_name); | 82 funcs_to_infer_[i]->set_inferred_name(func_name); |
138 } | 83 } |
139 funcs_to_infer_.Rewind(0); | 84 funcs_to_infer_.Rewind(0); |
140 } | 85 } |
141 | 86 |
142 | 87 |
143 } } // namespace v8::internal | 88 } } // namespace v8::internal |
OLD | NEW |