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" |
8 #include "src/func-name-inferrer.h" | 9 #include "src/func-name-inferrer.h" |
9 #include "src/list-inl.h" | 10 #include "src/list-inl.h" |
10 | 11 |
11 namespace v8 { | 12 namespace v8 { |
12 namespace internal { | 13 namespace internal { |
13 | 14 |
14 FuncNameInferrer::FuncNameInferrer(Isolate* isolate, Zone* zone) | 15 FuncNameInferrer::FuncNameInferrer(AstValueFactory* ast_value_factory, |
15 : isolate_(isolate), | 16 Zone* zone) |
| 17 : ast_value_factory_(ast_value_factory), |
16 entries_stack_(10, zone), | 18 entries_stack_(10, zone), |
17 names_stack_(5, zone), | 19 names_stack_(5, zone), |
18 funcs_to_infer_(4, zone), | 20 funcs_to_infer_(4, zone), |
19 zone_(zone) { | 21 zone_(zone) { |
20 } | 22 } |
21 | 23 |
22 | 24 |
23 void FuncNameInferrer::PushEnclosingName(Handle<String> name) { | 25 void FuncNameInferrer::PushEnclosingName(const AstString* name) { |
24 // Enclosing name is a name of a constructor function. To check | 26 // Enclosing name is a name of a constructor function. To check |
25 // that it is really a constructor, we check that it is not empty | 27 // that it is really a constructor, we check that it is not empty |
26 // and starts with a capital letter. | 28 // and starts with a capital letter. |
27 if (name->length() > 0 && Runtime::IsUpperCaseChar( | 29 if (!name->IsEmpty() && unibrow::Uppercase::Is(name->FirstCharacter())) { |
28 isolate()->runtime_state(), name->Get(0))) { | |
29 names_stack_.Add(Name(name, kEnclosingConstructorName), zone()); | 30 names_stack_.Add(Name(name, kEnclosingConstructorName), zone()); |
30 } | 31 } |
31 } | 32 } |
32 | 33 |
33 | 34 |
34 void FuncNameInferrer::PushLiteralName(Handle<String> name) { | 35 void FuncNameInferrer::PushLiteralName(const AstString* name) { |
35 if (IsOpen() && | 36 if (IsOpen() && name != ast_value_factory_->prototype_string()) { |
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(Handle<String> name) { | 42 void FuncNameInferrer::PushVariableName(const AstString* name) { |
43 if (IsOpen() && | 43 if (IsOpen() && name != ast_value_factory_->dot_result_string()) { |
44 !String::Equals(isolate()->factory()->dot_result_string(), name)) { | |
45 names_stack_.Add(Name(name, kVariableName), zone()); | 44 names_stack_.Add(Name(name, kVariableName), zone()); |
46 } | 45 } |
47 } | 46 } |
48 | 47 |
49 | 48 |
50 Handle<String> FuncNameInferrer::MakeNameFromStack() { | 49 const AstString* FuncNameInferrer::MakeNameFromStack() { |
51 return MakeNameFromStackHelper(0, isolate()->factory()->empty_string()); | 50 // First see how many names we will use. |
52 } | 51 int length = 0; |
53 | 52 bool one_byte = true; |
54 | 53 int pos = 0; |
55 Handle<String> FuncNameInferrer::MakeNameFromStackHelper(int pos, | 54 while (pos < names_stack_.length()) { |
56 Handle<String> prev) { | 55 if (pos < names_stack_.length() - 1 && |
57 if (pos >= names_stack_.length()) return prev; | 56 names_stack_.at(pos).type == kVariableName && |
58 if (pos < names_stack_.length() - 1 && | 57 names_stack_.at(pos + 1).type == kVariableName) { |
59 names_stack_.at(pos).type == kVariableName && | 58 // Skip consecutive variable declarations. |
60 names_stack_.at(pos + 1).type == kVariableName) { | 59 ++pos; |
61 // Skip consecutive variable declarations. | 60 continue; |
62 return MakeNameFromStackHelper(pos + 1, prev); | 61 } |
| 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 } |
| 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(); |
63 } else { | 99 } else { |
64 if (prev->length() > 0) { | 100 Vector<uint16_t> new_name = Vector<uint16_t>::New(length); |
65 Handle<String> name = names_stack_.at(pos).name; | 101 int name_pos = 0; |
66 if (prev->length() + name->length() + 1 > String::kMaxLength) return prev; | 102 for (int i = 0; i < pos; ++i) { |
67 Factory* factory = isolate()->factory(); | 103 if (i < names_stack_.length() - 1 && |
68 Handle<String> curr = | 104 names_stack_.at(i).type == kVariableName && |
69 factory->NewConsString(factory->dot_string(), name).ToHandleChecked(); | 105 names_stack_.at(i + 1).type == kVariableName) { |
70 curr = factory->NewConsString(prev, curr).ToHandleChecked(); | 106 // Skip consecutive variable declarations. |
71 return MakeNameFromStackHelper(pos + 1, curr); | 107 continue; |
72 } else { | 108 } |
73 return MakeNameFromStackHelper(pos + 1, names_stack_.at(pos).name); | 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(); |
74 } | 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(); |
75 } | 129 } |
| 130 return to_return; |
76 } | 131 } |
77 | 132 |
78 | 133 |
79 void FuncNameInferrer::InferFunctionsNames() { | 134 void FuncNameInferrer::InferFunctionsNames() { |
80 Handle<String> func_name = MakeNameFromStack(); | 135 const AstString* func_name = MakeNameFromStack(); |
81 for (int i = 0; i < funcs_to_infer_.length(); ++i) { | 136 for (int i = 0; i < funcs_to_infer_.length(); ++i) { |
82 funcs_to_infer_[i]->set_inferred_name(func_name); | 137 funcs_to_infer_[i]->set_raw_inferred_name(func_name); |
83 } | 138 } |
84 funcs_to_infer_.Rewind(0); | 139 funcs_to_infer_.Rewind(0); |
85 } | 140 } |
86 | 141 |
87 | 142 |
88 } } // namespace v8::internal | 143 } } // namespace v8::internal |
OLD | NEW |