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 AstRawString* 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 AstRawString* 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 AstRawString* 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 return MakeNameFromStackHelper(0, ast_value_factory_->empty_string()); |
52 } | 51 } |
53 | 52 |
54 | 53 const AstString* FuncNameInferrer::MakeNameFromStackHelper( |
55 Handle<String> FuncNameInferrer::MakeNameFromStackHelper(int pos, | 54 int pos, const AstString* prev) { |
56 Handle<String> prev) { | |
57 if (pos >= names_stack_.length()) return prev; | 55 if (pos >= names_stack_.length()) return prev; |
58 if (pos < names_stack_.length() - 1 && | 56 if (pos < names_stack_.length() - 1 && |
59 names_stack_.at(pos).type == kVariableName && | 57 names_stack_.at(pos).type == kVariableName && |
60 names_stack_.at(pos + 1).type == kVariableName) { | 58 names_stack_.at(pos + 1).type == kVariableName) { |
61 // Skip consecutive variable declarations. | 59 // Skip consecutive variable declarations. |
62 return MakeNameFromStackHelper(pos + 1, prev); | 60 return MakeNameFromStackHelper(pos + 1, prev); |
63 } else { | 61 } else { |
64 if (prev->length() > 0) { | 62 if (prev->length() > 0) { |
65 Handle<String> name = names_stack_.at(pos).name; | 63 const AstRawString* name = names_stack_.at(pos).name; |
66 if (prev->length() + name->length() + 1 > String::kMaxLength) return prev; | 64 if (prev->length() + name->length() + 1 > String::kMaxLength) return prev; |
67 Factory* factory = isolate()->factory(); | 65 const AstConsString* curr = ast_value_factory_->NewConsString( |
68 Handle<String> curr = | 66 ast_value_factory_->dot_string(), name); |
69 factory->NewConsString(factory->dot_string(), name).ToHandleChecked(); | 67 curr = ast_value_factory_->NewConsString(prev, curr); |
70 curr = factory->NewConsString(prev, curr).ToHandleChecked(); | |
71 return MakeNameFromStackHelper(pos + 1, curr); | 68 return MakeNameFromStackHelper(pos + 1, curr); |
72 } else { | 69 } else { |
73 return MakeNameFromStackHelper(pos + 1, names_stack_.at(pos).name); | 70 return MakeNameFromStackHelper(pos + 1, names_stack_.at(pos).name); |
74 } | 71 } |
75 } | 72 } |
76 } | 73 } |
77 | 74 |
78 | 75 |
79 void FuncNameInferrer::InferFunctionsNames() { | 76 void FuncNameInferrer::InferFunctionsNames() { |
80 Handle<String> func_name = MakeNameFromStack(); | 77 const AstString* func_name = MakeNameFromStack(); |
81 for (int i = 0; i < funcs_to_infer_.length(); ++i) { | 78 for (int i = 0; i < funcs_to_infer_.length(); ++i) { |
82 funcs_to_infer_[i]->set_inferred_name(func_name); | 79 funcs_to_infer_[i]->set_raw_inferred_name(func_name); |
83 } | 80 } |
84 funcs_to_infer_.Rewind(0); | 81 funcs_to_infer_.Rewind(0); |
85 } | 82 } |
86 | 83 |
87 | 84 |
88 } } // namespace v8::internal | 85 } } // namespace v8::internal |
OLD | NEW |