| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 932 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 943 | 943 |
| 944 const bool enabled_; | 944 const bool enabled_; |
| 945 Assignment* first_in_block_; | 945 Assignment* first_in_block_; |
| 946 Assignment* last_in_block_; | 946 Assignment* last_in_block_; |
| 947 int block_size_; | 947 int block_size_; |
| 948 | 948 |
| 949 DISALLOW_COPY_AND_ASSIGN(InitializationBlockFinder); | 949 DISALLOW_COPY_AND_ASSIGN(InitializationBlockFinder); |
| 950 }; | 950 }; |
| 951 | 951 |
| 952 | 952 |
| 953 // A ThisNamedPropertyAssigmentFinder finds and marks statements of the form | 953 // A ThisNamedPropertyAssignmentFinder finds and marks statements of the form |
| 954 // this.x = ...;, where x is a named property. It also determines whether a | 954 // this.x = ...;, where x is a named property. It also determines whether a |
| 955 // function contains only assignments of this type. | 955 // function contains only assignments of this type. |
| 956 class ThisNamedPropertyAssigmentFinder : public ParserFinder { | 956 class ThisNamedPropertyAssignmentFinder : public ParserFinder { |
| 957 public: | 957 public: |
| 958 explicit ThisNamedPropertyAssigmentFinder(Isolate* isolate) | 958 explicit ThisNamedPropertyAssignmentFinder(Isolate* isolate) |
| 959 : isolate_(isolate), | 959 : isolate_(isolate), |
| 960 only_simple_this_property_assignments_(true), | 960 only_simple_this_property_assignments_(true), |
| 961 names_(NULL), | 961 names_(0), |
| 962 assigned_arguments_(NULL), | 962 assigned_arguments_(0), |
| 963 assigned_constants_(NULL) {} | 963 assigned_constants_(0) { |
| 964 } |
| 964 | 965 |
| 965 void Update(Scope* scope, Statement* stat) { | 966 void Update(Scope* scope, Statement* stat) { |
| 966 // Bail out if function already has property assignment that are | 967 // Bail out if function already has property assignment that are |
| 967 // not simple this property assignments. | 968 // not simple this property assignments. |
| 968 if (!only_simple_this_property_assignments_) { | 969 if (!only_simple_this_property_assignments_) { |
| 969 return; | 970 return; |
| 970 } | 971 } |
| 971 | 972 |
| 972 // Check whether this statement is of the form this.x = ...; | 973 // Check whether this statement is of the form this.x = ...; |
| 973 Assignment* assignment = AsAssignment(stat); | 974 Assignment* assignment = AsAssignment(stat); |
| 974 if (IsThisPropertyAssignment(assignment)) { | 975 if (IsThisPropertyAssignment(assignment)) { |
| 975 HandleThisPropertyAssignment(scope, assignment); | 976 HandleThisPropertyAssignment(scope, assignment); |
| 976 } else { | 977 } else { |
| 977 only_simple_this_property_assignments_ = false; | 978 only_simple_this_property_assignments_ = false; |
| 978 } | 979 } |
| 979 } | 980 } |
| 980 | 981 |
| 981 // Returns whether only statements of the form this.x = y; where y is either a | 982 // Returns whether only statements of the form this.x = y; where y is either a |
| 982 // constant or a function argument was encountered. | 983 // constant or a function argument was encountered. |
| 983 bool only_simple_this_property_assignments() { | 984 bool only_simple_this_property_assignments() { |
| 984 return only_simple_this_property_assignments_; | 985 return only_simple_this_property_assignments_; |
| 985 } | 986 } |
| 986 | 987 |
| 987 // Returns a fixed array containing three elements for each assignment of the | 988 // Returns a fixed array containing three elements for each assignment of the |
| 988 // form this.x = y; | 989 // form this.x = y; |
| 989 Handle<FixedArray> GetThisPropertyAssignments() { | 990 Handle<FixedArray> GetThisPropertyAssignments() { |
| 990 if (names_ == NULL) { | 991 if (names_.is_empty()) { |
| 991 return isolate_->factory()->empty_fixed_array(); | 992 return isolate_->factory()->empty_fixed_array(); |
| 992 } | 993 } |
| 993 ASSERT(names_ != NULL); | 994 ASSERT_EQ(names_.length(), assigned_arguments_.length()); |
| 994 ASSERT(assigned_arguments_ != NULL); | 995 ASSERT_EQ(names_.length(), assigned_constants_.length()); |
| 995 ASSERT_EQ(names_->length(), assigned_arguments_->length()); | |
| 996 ASSERT_EQ(names_->length(), assigned_constants_->length()); | |
| 997 Handle<FixedArray> assignments = | 996 Handle<FixedArray> assignments = |
| 998 isolate_->factory()->NewFixedArray(names_->length() * 3); | 997 isolate_->factory()->NewFixedArray(names_.length() * 3); |
| 999 for (int i = 0; i < names_->length(); i++) { | 998 for (int i = 0; i < names_.length(); ++i) { |
| 1000 assignments->set(i * 3, *names_->at(i)); | 999 assignments->set(i * 3, *names_[i]); |
| 1001 assignments->set(i * 3 + 1, Smi::FromInt(assigned_arguments_->at(i))); | 1000 assignments->set(i * 3 + 1, Smi::FromInt(assigned_arguments_[i])); |
| 1002 assignments->set(i * 3 + 2, *assigned_constants_->at(i)); | 1001 assignments->set(i * 3 + 2, *assigned_constants_[i]); |
| 1003 } | 1002 } |
| 1004 return assignments; | 1003 return assignments; |
| 1005 } | 1004 } |
| 1006 | 1005 |
| 1007 private: | 1006 private: |
| 1008 bool IsThisPropertyAssignment(Assignment* assignment) { | 1007 bool IsThisPropertyAssignment(Assignment* assignment) { |
| 1009 if (assignment != NULL) { | 1008 if (assignment != NULL) { |
| 1010 Property* property = assignment->target()->AsProperty(); | 1009 Property* property = assignment->target()->AsProperty(); |
| 1011 return assignment->op() == Token::ASSIGN | 1010 return assignment->op() == Token::ASSIGN |
| 1012 && property != NULL | 1011 && property != NULL |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1049 return; | 1048 return; |
| 1050 } | 1049 } |
| 1051 } | 1050 } |
| 1052 } | 1051 } |
| 1053 } | 1052 } |
| 1054 // It is not a simple "this.x = value;" assignment with a constant | 1053 // It is not a simple "this.x = value;" assignment with a constant |
| 1055 // or parameter value. | 1054 // or parameter value. |
| 1056 AssignmentFromSomethingElse(); | 1055 AssignmentFromSomethingElse(); |
| 1057 } | 1056 } |
| 1058 | 1057 |
| 1058 |
| 1059 |
| 1060 |
| 1061 // We will potentially reorder the property assignments, so they must be |
| 1062 // simple enough that the ordering does not matter. |
| 1059 void AssignmentFromParameter(Handle<String> name, int index) { | 1063 void AssignmentFromParameter(Handle<String> name, int index) { |
| 1060 EnsureAllocation(); | 1064 EnsureInitialized(); |
| 1061 names_->Add(name); | 1065 for (int i = 0; i < names_.length(); ++i) { |
| 1062 assigned_arguments_->Add(index); | 1066 if (name->Equals(*names_[i])) { |
| 1063 assigned_constants_->Add(isolate_->factory()->undefined_value()); | 1067 assigned_arguments_[i] = index; |
| 1068 assigned_constants_[i] = isolate_->factory()->undefined_value(); |
| 1069 return; |
| 1070 } |
| 1071 } |
| 1072 names_.Add(name); |
| 1073 assigned_arguments_.Add(index); |
| 1074 assigned_constants_.Add(isolate_->factory()->undefined_value()); |
| 1064 } | 1075 } |
| 1065 | 1076 |
| 1066 void AssignmentFromConstant(Handle<String> name, Handle<Object> value) { | 1077 void AssignmentFromConstant(Handle<String> name, Handle<Object> value) { |
| 1067 EnsureAllocation(); | 1078 EnsureInitialized(); |
| 1068 names_->Add(name); | 1079 for (int i = 0; i < names_.length(); ++i) { |
| 1069 assigned_arguments_->Add(-1); | 1080 if (name->Equals(*names_[i])) { |
| 1070 assigned_constants_->Add(value); | 1081 assigned_arguments_[i] = -1; |
| 1082 assigned_constants_[i] = value; |
| 1083 return; |
| 1084 } |
| 1085 } |
| 1086 names_.Add(name); |
| 1087 assigned_arguments_.Add(-1); |
| 1088 assigned_constants_.Add(value); |
| 1071 } | 1089 } |
| 1072 | 1090 |
| 1073 void AssignmentFromSomethingElse() { | 1091 void AssignmentFromSomethingElse() { |
| 1074 // The this assignment is not a simple one. | 1092 // The this assignment is not a simple one. |
| 1075 only_simple_this_property_assignments_ = false; | 1093 only_simple_this_property_assignments_ = false; |
| 1076 } | 1094 } |
| 1077 | 1095 |
| 1078 void EnsureAllocation() { | 1096 void EnsureInitialized() { |
| 1079 if (names_ == NULL) { | 1097 if (names_.capacity() == 0) { |
| 1080 ASSERT(assigned_arguments_ == NULL); | 1098 ASSERT(assigned_arguments_.capacity() == 0); |
| 1081 ASSERT(assigned_constants_ == NULL); | 1099 ASSERT(assigned_constants_.capacity() == 0); |
| 1082 Zone* zone = isolate_->zone(); | 1100 names_.Initialize(4); |
| 1083 names_ = new(zone) ZoneStringList(4); | 1101 assigned_arguments_.Initialize(4); |
| 1084 assigned_arguments_ = new(zone) ZoneList<int>(4); | 1102 assigned_constants_.Initialize(4); |
| 1085 assigned_constants_ = new(zone) ZoneObjectList(4); | |
| 1086 } | 1103 } |
| 1087 } | 1104 } |
| 1088 | 1105 |
| 1089 Isolate* isolate_; | 1106 Isolate* isolate_; |
| 1090 bool only_simple_this_property_assignments_; | 1107 bool only_simple_this_property_assignments_; |
| 1091 ZoneStringList* names_; | 1108 ZoneStringList names_; |
| 1092 ZoneList<int>* assigned_arguments_; | 1109 ZoneList<int> assigned_arguments_; |
| 1093 ZoneObjectList* assigned_constants_; | 1110 ZoneObjectList assigned_constants_; |
| 1094 }; | 1111 }; |
| 1095 | 1112 |
| 1096 | 1113 |
| 1097 Statement* Parser::ParseSourceElement(ZoneStringList* labels, | 1114 Statement* Parser::ParseSourceElement(ZoneStringList* labels, |
| 1098 bool* ok) { | 1115 bool* ok) { |
| 1099 if (peek() == Token::FUNCTION) { | 1116 if (peek() == Token::FUNCTION) { |
| 1100 // FunctionDeclaration is only allowed in the context of SourceElements | 1117 // FunctionDeclaration is only allowed in the context of SourceElements |
| 1101 // (Ecma 262 5th Edition, clause 14): | 1118 // (Ecma 262 5th Edition, clause 14): |
| 1102 // SourceElement: | 1119 // SourceElement: |
| 1103 // Statement | 1120 // Statement |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1120 // (Statement)* <end_token> | 1137 // (Statement)* <end_token> |
| 1121 | 1138 |
| 1122 // Allocate a target stack to use for this set of source | 1139 // Allocate a target stack to use for this set of source |
| 1123 // elements. This way, all scripts and functions get their own | 1140 // elements. This way, all scripts and functions get their own |
| 1124 // target stack thus avoiding illegal breaks and continues across | 1141 // target stack thus avoiding illegal breaks and continues across |
| 1125 // functions. | 1142 // functions. |
| 1126 TargetScope scope(&this->target_stack_); | 1143 TargetScope scope(&this->target_stack_); |
| 1127 | 1144 |
| 1128 ASSERT(processor != NULL); | 1145 ASSERT(processor != NULL); |
| 1129 InitializationBlockFinder block_finder(top_scope_, target_stack_); | 1146 InitializationBlockFinder block_finder(top_scope_, target_stack_); |
| 1130 ThisNamedPropertyAssigmentFinder this_property_assignment_finder(isolate()); | 1147 ThisNamedPropertyAssignmentFinder this_property_assignment_finder(isolate()); |
| 1131 bool directive_prologue = true; // Parsing directive prologue. | 1148 bool directive_prologue = true; // Parsing directive prologue. |
| 1132 | 1149 |
| 1133 while (peek() != end_token) { | 1150 while (peek() != end_token) { |
| 1134 if (directive_prologue && peek() != Token::STRING) { | 1151 if (directive_prologue && peek() != Token::STRING) { |
| 1135 directive_prologue = false; | 1152 directive_prologue = false; |
| 1136 } | 1153 } |
| 1137 | 1154 |
| 1138 Scanner::Location token_loc = scanner().peek_location(); | 1155 Scanner::Location token_loc = scanner().peek_location(); |
| 1139 Statement* stat = ParseSourceElement(NULL, CHECK_OK); | 1156 Statement* stat = ParseSourceElement(NULL, CHECK_OK); |
| 1140 if (stat == NULL || stat->IsEmpty()) { | 1157 if (stat == NULL || stat->IsEmpty()) { |
| (...skipping 4042 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5183 result = parser.ParseProgram(source, | 5200 result = parser.ParseProgram(source, |
| 5184 info->is_global(), | 5201 info->is_global(), |
| 5185 info->StrictMode()); | 5202 info->StrictMode()); |
| 5186 } | 5203 } |
| 5187 } | 5204 } |
| 5188 info->SetFunction(result); | 5205 info->SetFunction(result); |
| 5189 return (result != NULL); | 5206 return (result != NULL); |
| 5190 } | 5207 } |
| 5191 | 5208 |
| 5192 } } // namespace v8::internal | 5209 } } // namespace v8::internal |
| OLD | NEW |