OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 Expression* ParseMemberExpression(bool* ok); | 151 Expression* ParseMemberExpression(bool* ok); |
152 Expression* ParseMemberWithNewPrefixesExpression(List<int>* new_prefixes, | 152 Expression* ParseMemberWithNewPrefixesExpression(List<int>* new_prefixes, |
153 bool* ok); | 153 bool* ok); |
154 Expression* ParsePrimaryExpression(bool* ok); | 154 Expression* ParsePrimaryExpression(bool* ok); |
155 Expression* ParseArrayLiteral(bool* ok); | 155 Expression* ParseArrayLiteral(bool* ok); |
156 Expression* ParseObjectLiteral(bool* ok); | 156 Expression* ParseObjectLiteral(bool* ok); |
157 Expression* ParseRegExpLiteral(bool seen_equal, bool* ok); | 157 Expression* ParseRegExpLiteral(bool seen_equal, bool* ok); |
158 | 158 |
159 // Decide if a property should be the object boilerplate. | 159 // Decide if a property should be the object boilerplate. |
160 bool IsBoilerplateProperty(ObjectLiteral::Property* property); | 160 bool IsBoilerplateProperty(ObjectLiteral::Property* property); |
161 // If the property is CONSTANT type, it returns the literal value, | 161 // If the expression is a literal, return the literal value; |
162 // otherwise, it return undefined literal as the placeholder | 162 // if the expression is a materialized literal and is simple return a |
| 163 // compile time value as encoded by CompileTimeValue::GetValue(). |
| 164 // Otherwise, return undefined literal as the placeholder |
163 // in the object literal boilerplate. | 165 // in the object literal boilerplate. |
164 Literal* GetBoilerplateValue(ObjectLiteral::Property* property); | 166 Handle<Object> GetBoilerplateValue(Expression* expression); |
165 | 167 |
166 enum FunctionLiteralType { | 168 enum FunctionLiteralType { |
167 EXPRESSION, | 169 EXPRESSION, |
168 DECLARATION, | 170 DECLARATION, |
169 NESTED | 171 NESTED |
170 }; | 172 }; |
171 | 173 |
172 ZoneList<Expression*>* ParseArguments(bool* ok); | 174 ZoneList<Expression*>* ParseArguments(bool* ok); |
173 FunctionLiteral* ParseFunctionLiteral(Handle<String> var_name, | 175 FunctionLiteral* ParseFunctionLiteral(Handle<String> var_name, |
174 int function_token_position, | 176 int function_token_position, |
(...skipping 2872 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3047 } | 3049 } |
3048 values.Add(elem); | 3050 values.Add(elem); |
3049 if (peek() != Token::RBRACK) { | 3051 if (peek() != Token::RBRACK) { |
3050 Expect(Token::COMMA, CHECK_OK); | 3052 Expect(Token::COMMA, CHECK_OK); |
3051 } | 3053 } |
3052 } | 3054 } |
3053 Expect(Token::RBRACK, CHECK_OK); | 3055 Expect(Token::RBRACK, CHECK_OK); |
3054 | 3056 |
3055 // Update the scope information before the pre-parsing bailout. | 3057 // Update the scope information before the pre-parsing bailout. |
3056 temp_scope_->set_contains_array_literal(); | 3058 temp_scope_->set_contains_array_literal(); |
| 3059 int literal_index = temp_scope_->NextMaterializedLiteralIndex(); |
3057 | 3060 |
3058 if (is_pre_parsing_) return NULL; | 3061 if (is_pre_parsing_) return NULL; |
3059 | 3062 |
3060 // Allocate a fixed array with all the literals. | 3063 // Allocate a fixed array with all the literals. |
3061 Handle<FixedArray> literals = | 3064 Handle<FixedArray> literals = |
3062 Factory::NewFixedArray(values.length(), TENURED); | 3065 Factory::NewFixedArray(values.length(), TENURED); |
3063 | 3066 |
3064 // Fill in the literals. | 3067 // Fill in the literals. |
| 3068 bool is_simple = true; |
| 3069 int depth = 1; |
3065 for (int i = 0; i < values.length(); i++) { | 3070 for (int i = 0; i < values.length(); i++) { |
3066 Literal* literal = values.at(i)->AsLiteral(); | 3071 MaterializedLiteral* m_literal = values.at(i)->AsMaterializedLiteral(); |
3067 if (literal == NULL) { | 3072 if (m_literal != NULL && m_literal->depth() + 1 > depth) { |
| 3073 depth = m_literal->depth() + 1; |
| 3074 } |
| 3075 Handle<Object> boilerplate_value = GetBoilerplateValue(values.at(i)); |
| 3076 if (boilerplate_value->IsUndefined()) { |
3068 literals->set_the_hole(i); | 3077 literals->set_the_hole(i); |
| 3078 is_simple = false; |
3069 } else { | 3079 } else { |
3070 literals->set(i, *literal->handle()); | 3080 literals->set(i, *boilerplate_value); |
3071 } | 3081 } |
3072 } | 3082 } |
3073 | 3083 |
3074 return NEW(ArrayLiteral(literals, values.elements())); | 3084 return NEW(ArrayLiteral(literals, values.elements(), |
| 3085 literal_index, is_simple, depth)); |
3075 } | 3086 } |
3076 | 3087 |
3077 | 3088 |
3078 bool Parser::IsBoilerplateProperty(ObjectLiteral::Property* property) { | 3089 bool Parser::IsBoilerplateProperty(ObjectLiteral::Property* property) { |
3079 return property != NULL && | 3090 return property != NULL && |
3080 property->kind() != ObjectLiteral::Property::PROTOTYPE; | 3091 property->kind() != ObjectLiteral::Property::PROTOTYPE; |
3081 } | 3092 } |
3082 | 3093 |
3083 | 3094 |
3084 Literal* Parser::GetBoilerplateValue(ObjectLiteral::Property* property) { | 3095 bool CompileTimeValue::IsCompileTimeValue(Expression* expression) { |
3085 if (property->kind() == ObjectLiteral::Property::CONSTANT) | 3096 MaterializedLiteral* lit = expression->AsMaterializedLiteral(); |
3086 return property->value()->AsLiteral(); | 3097 return lit != NULL && lit->is_simple(); |
3087 return GetLiteralUndefined(); | 3098 } |
| 3099 |
| 3100 Handle<FixedArray> CompileTimeValue::GetValue(Expression* expression) { |
| 3101 ASSERT(IsCompileTimeValue(expression)); |
| 3102 Handle<FixedArray> result = Factory::NewFixedArray(2, TENURED); |
| 3103 ObjectLiteral* object_literal = expression->AsObjectLiteral(); |
| 3104 if (object_literal != NULL) { |
| 3105 ASSERT(object_literal->is_simple()); |
| 3106 result->set(kTypeSlot, Smi::FromInt(OBJECT_LITERAL)); |
| 3107 result->set(kElementsSlot, *object_literal->constant_properties()); |
| 3108 } else { |
| 3109 ArrayLiteral* array_literal = expression->AsArrayLiteral(); |
| 3110 ASSERT(array_literal != NULL && array_literal->is_simple()); |
| 3111 result->set(kTypeSlot, Smi::FromInt(ARRAY_LITERAL)); |
| 3112 result->set(kElementsSlot, *array_literal->literals()); |
| 3113 } |
| 3114 return result; |
| 3115 } |
| 3116 |
| 3117 |
| 3118 CompileTimeValue::Type CompileTimeValue::GetType(Handle<FixedArray> value) { |
| 3119 Smi* type_value = Smi::cast(value->get(kTypeSlot)); |
| 3120 return static_cast<Type>(type_value->value()); |
| 3121 } |
| 3122 |
| 3123 |
| 3124 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { |
| 3125 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); |
| 3126 } |
| 3127 |
| 3128 |
| 3129 Handle<Object> Parser::GetBoilerplateValue(Expression* expression) { |
| 3130 if (expression->AsLiteral() != NULL) { |
| 3131 return expression->AsLiteral()->handle(); |
| 3132 } |
| 3133 if (CompileTimeValue::IsCompileTimeValue(expression)) { |
| 3134 return CompileTimeValue::GetValue(expression); |
| 3135 } |
| 3136 return Factory::undefined_value(); |
3088 } | 3137 } |
3089 | 3138 |
3090 | 3139 |
3091 Expression* Parser::ParseObjectLiteral(bool* ok) { | 3140 Expression* Parser::ParseObjectLiteral(bool* ok) { |
3092 // ObjectLiteral :: | 3141 // ObjectLiteral :: |
3093 // '{' ( | 3142 // '{' ( |
3094 // ((Identifier | String | Number) ':' AssignmentExpression) | 3143 // ((Identifier | String | Number) ':' AssignmentExpression) |
3095 // | (('get' | 'set') FunctionLiteral) | 3144 // | (('get' | 'set') FunctionLiteral) |
3096 // )*[','] '}' | 3145 // )*[','] '}' |
3097 | 3146 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3172 if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK); | 3221 if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK); |
3173 } | 3222 } |
3174 Expect(Token::RBRACE, CHECK_OK); | 3223 Expect(Token::RBRACE, CHECK_OK); |
3175 // Computation of literal_index must happen before pre parse bailout. | 3224 // Computation of literal_index must happen before pre parse bailout. |
3176 int literal_index = temp_scope_->NextMaterializedLiteralIndex(); | 3225 int literal_index = temp_scope_->NextMaterializedLiteralIndex(); |
3177 if (is_pre_parsing_) return NULL; | 3226 if (is_pre_parsing_) return NULL; |
3178 | 3227 |
3179 Handle<FixedArray> constant_properties = | 3228 Handle<FixedArray> constant_properties = |
3180 Factory::NewFixedArray(number_of_boilerplate_properties * 2, TENURED); | 3229 Factory::NewFixedArray(number_of_boilerplate_properties * 2, TENURED); |
3181 int position = 0; | 3230 int position = 0; |
| 3231 bool is_simple = true; |
| 3232 int depth = 1; |
3182 for (int i = 0; i < properties.length(); i++) { | 3233 for (int i = 0; i < properties.length(); i++) { |
3183 ObjectLiteral::Property* property = properties.at(i); | 3234 ObjectLiteral::Property* property = properties.at(i); |
3184 if (!IsBoilerplateProperty(property)) continue; | 3235 if (!IsBoilerplateProperty(property)) { |
| 3236 is_simple = false; |
| 3237 continue; |
| 3238 } |
| 3239 MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral(); |
| 3240 if (m_literal != NULL && m_literal->depth() + 1 > depth) { |
| 3241 depth = m_literal->depth() + 1; |
| 3242 } |
3185 | 3243 |
3186 // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined | 3244 // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined |
3187 // value for COMPUTED properties, the real value is filled in at | 3245 // value for COMPUTED properties, the real value is filled in at |
3188 // runtime. The enumeration order is maintained. | 3246 // runtime. The enumeration order is maintained. |
3189 Handle<Object> key = property->key()->handle(); | 3247 Handle<Object> key = property->key()->handle(); |
3190 Literal* literal = GetBoilerplateValue(property); | 3248 Handle<Object> value = GetBoilerplateValue(property->value()); |
| 3249 is_simple = is_simple && !value->IsUndefined(); |
3191 | 3250 |
3192 // Add name, value pair to the fixed array. | 3251 // Add name, value pair to the fixed array. |
3193 constant_properties->set(position++, *key); | 3252 constant_properties->set(position++, *key); |
3194 constant_properties->set(position++, *literal->handle()); | 3253 constant_properties->set(position++, *value); |
3195 } | 3254 } |
3196 | 3255 |
3197 return new ObjectLiteral(constant_properties, | 3256 return new ObjectLiteral(constant_properties, |
3198 properties.elements(), | 3257 properties.elements(), |
3199 literal_index); | 3258 literal_index, |
| 3259 is_simple, |
| 3260 depth); |
3200 } | 3261 } |
3201 | 3262 |
3202 | 3263 |
3203 Expression* Parser::ParseRegExpLiteral(bool seen_equal, bool* ok) { | 3264 Expression* Parser::ParseRegExpLiteral(bool seen_equal, bool* ok) { |
3204 if (!scanner_.ScanRegExpPattern(seen_equal)) { | 3265 if (!scanner_.ScanRegExpPattern(seen_equal)) { |
3205 Next(); | 3266 Next(); |
3206 ReportMessage("unterminated_regexp", Vector<const char*>::empty()); | 3267 ReportMessage("unterminated_regexp", Vector<const char*>::empty()); |
3207 *ok = false; | 3268 *ok = false; |
3208 return NULL; | 3269 return NULL; |
3209 } | 3270 } |
(...skipping 1299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4509 start_position, | 4570 start_position, |
4510 is_expression); | 4571 is_expression); |
4511 return result; | 4572 return result; |
4512 } | 4573 } |
4513 | 4574 |
4514 | 4575 |
4515 #undef NEW | 4576 #undef NEW |
4516 | 4577 |
4517 | 4578 |
4518 } } // namespace v8::internal | 4579 } } // namespace v8::internal |
OLD | NEW |