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, return the literal value; | 161 // If the expression is a literal, return the literal value; |
162 // if the property is OBJECT_LITERAL and the object literal is | 162 // if the expression is a materialized literal and is simple return a |
163 // simple return a fixed array containing the keys and values of the | 163 // compile time value as encoded by CompileTimeValue::GetValue(). |
164 // object literal. | |
165 // Otherwise, return undefined literal as the placeholder | 164 // Otherwise, return undefined literal as the placeholder |
166 // in the object literal boilerplate. | 165 // in the object literal boilerplate. |
167 Handle<Object> GetBoilerplateValue(ObjectLiteral::Property* property); | 166 Handle<Object> GetBoilerplateValue(Expression* expression); |
168 | 167 |
169 enum FunctionLiteralType { | 168 enum FunctionLiteralType { |
170 EXPRESSION, | 169 EXPRESSION, |
171 DECLARATION, | 170 DECLARATION, |
172 NESTED | 171 NESTED |
173 }; | 172 }; |
174 | 173 |
175 ZoneList<Expression*>* ParseArguments(bool* ok); | 174 ZoneList<Expression*>* ParseArguments(bool* ok); |
176 FunctionLiteral* ParseFunctionLiteral(Handle<String> var_name, | 175 FunctionLiteral* ParseFunctionLiteral(Handle<String> var_name, |
177 int function_token_position, | 176 int function_token_position, |
(...skipping 2874 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3052 } | 3051 } |
3053 values.Add(elem); | 3052 values.Add(elem); |
3054 if (peek() != Token::RBRACK) { | 3053 if (peek() != Token::RBRACK) { |
3055 Expect(Token::COMMA, CHECK_OK); | 3054 Expect(Token::COMMA, CHECK_OK); |
3056 } | 3055 } |
3057 } | 3056 } |
3058 Expect(Token::RBRACK, CHECK_OK); | 3057 Expect(Token::RBRACK, CHECK_OK); |
3059 | 3058 |
3060 // Update the scope information before the pre-parsing bailout. | 3059 // Update the scope information before the pre-parsing bailout. |
3061 temp_scope_->set_contains_array_literal(); | 3060 temp_scope_->set_contains_array_literal(); |
| 3061 int literal_index = temp_scope_->NextMaterializedLiteralIndex(); |
3062 | 3062 |
3063 if (is_pre_parsing_) return NULL; | 3063 if (is_pre_parsing_) return NULL; |
3064 | 3064 |
3065 // Allocate a fixed array with all the literals. | 3065 // Allocate a fixed array with all the literals. |
3066 Handle<FixedArray> literals = | 3066 Handle<FixedArray> literals = |
3067 Factory::NewFixedArray(values.length(), TENURED); | 3067 Factory::NewFixedArray(values.length(), TENURED); |
3068 | 3068 |
3069 // Fill in the literals. | 3069 // Fill in the literals. |
| 3070 bool is_simple = true; |
3070 for (int i = 0; i < values.length(); i++) { | 3071 for (int i = 0; i < values.length(); i++) { |
3071 Literal* literal = values.at(i)->AsLiteral(); | 3072 Handle<Object> boilerplate_value = GetBoilerplateValue(values.at(i)); |
3072 if (literal == NULL) { | 3073 if (boilerplate_value->IsUndefined()) { |
3073 literals->set_the_hole(i); | 3074 literals->set_the_hole(i); |
| 3075 is_simple = false; |
3074 } else { | 3076 } else { |
3075 literals->set(i, *literal->handle()); | 3077 literals->set(i, *boilerplate_value); |
3076 } | 3078 } |
3077 } | 3079 } |
3078 | 3080 |
3079 return NEW(ArrayLiteral(literals, values.elements())); | 3081 return NEW(ArrayLiteral(literals, values.elements(), |
| 3082 literal_index, is_simple)); |
3080 } | 3083 } |
3081 | 3084 |
3082 | 3085 |
3083 bool Parser::IsBoilerplateProperty(ObjectLiteral::Property* property) { | 3086 bool Parser::IsBoilerplateProperty(ObjectLiteral::Property* property) { |
3084 return property != NULL && | 3087 return property != NULL && |
3085 property->kind() != ObjectLiteral::Property::PROTOTYPE; | 3088 property->kind() != ObjectLiteral::Property::PROTOTYPE; |
3086 } | 3089 } |
3087 | 3090 |
3088 | 3091 |
3089 Handle<Object> Parser::GetBoilerplateValue(ObjectLiteral::Property* property) { | 3092 bool CompileTimeValue::IsCompileTimeValue(Expression* expression) { |
3090 if (property->kind() == ObjectLiteral::Property::CONSTANT) | 3093 MaterializedLiteral* lit = expression->AsMaterializedLiteral(); |
3091 return property->value()->AsLiteral()->handle(); | 3094 return lit != NULL && lit->is_simple(); |
3092 if (property->kind() == ObjectLiteral::Property::OBJECT_LITERAL) { | 3095 } |
3093 ObjectLiteral* object_literal = property->value()->AsObjectLiteral(); | 3096 |
3094 if (object_literal->is_simple()) { | 3097 Handle<FixedArray> CompileTimeValue::GetValue(Expression* expression) { |
3095 return object_literal->constant_properties(); | 3098 ASSERT(IsCompileTimeValue(expression)); |
3096 } | 3099 Handle<FixedArray> result = Factory::NewFixedArray(2, TENURED); |
| 3100 ObjectLiteral* object_literal = expression->AsObjectLiteral(); |
| 3101 if (object_literal != NULL) { |
| 3102 ASSERT(object_literal->is_simple()); |
| 3103 result->set(kTypeSlot, Smi::FromInt(OBJECT_LITERAL)); |
| 3104 result->set(kElementsSlot, *object_literal->constant_properties()); |
| 3105 } else { |
| 3106 ArrayLiteral* array_literal = expression->AsArrayLiteral(); |
| 3107 ASSERT(array_literal != NULL && array_literal->is_simple()); |
| 3108 result->set(kTypeSlot, Smi::FromInt(ARRAY_LITERAL)); |
| 3109 result->set(kElementsSlot, *array_literal->literals()); |
| 3110 } |
| 3111 return result; |
| 3112 } |
| 3113 |
| 3114 |
| 3115 CompileTimeValue::Type CompileTimeValue::GetType(Handle<FixedArray> value) { |
| 3116 Smi* type_value = Smi::cast(value->get(kTypeSlot)); |
| 3117 return static_cast<Type>(type_value->value()); |
| 3118 } |
| 3119 |
| 3120 |
| 3121 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { |
| 3122 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); |
| 3123 } |
| 3124 |
| 3125 |
| 3126 Handle<Object> Parser::GetBoilerplateValue(Expression* expression) { |
| 3127 if (expression->AsLiteral() != NULL) { |
| 3128 return expression->AsLiteral()->handle(); |
| 3129 } |
| 3130 if (CompileTimeValue::IsCompileTimeValue(expression)) { |
| 3131 return CompileTimeValue::GetValue(expression); |
3097 } | 3132 } |
3098 return Factory::undefined_value(); | 3133 return Factory::undefined_value(); |
3099 } | 3134 } |
3100 | 3135 |
3101 | 3136 |
3102 Expression* Parser::ParseObjectLiteral(bool* ok) { | 3137 Expression* Parser::ParseObjectLiteral(bool* ok) { |
3103 // ObjectLiteral :: | 3138 // ObjectLiteral :: |
3104 // '{' ( | 3139 // '{' ( |
3105 // ((Identifier | String | Number) ':' AssignmentExpression) | 3140 // ((Identifier | String | Number) ':' AssignmentExpression) |
3106 // | (('get' | 'set') FunctionLiteral) | 3141 // | (('get' | 'set') FunctionLiteral) |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3195 ObjectLiteral::Property* property = properties.at(i); | 3230 ObjectLiteral::Property* property = properties.at(i); |
3196 if (!IsBoilerplateProperty(property)) { | 3231 if (!IsBoilerplateProperty(property)) { |
3197 is_simple = false; | 3232 is_simple = false; |
3198 continue; | 3233 continue; |
3199 } | 3234 } |
3200 | 3235 |
3201 // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined | 3236 // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined |
3202 // value for COMPUTED properties, the real value is filled in at | 3237 // value for COMPUTED properties, the real value is filled in at |
3203 // runtime. The enumeration order is maintained. | 3238 // runtime. The enumeration order is maintained. |
3204 Handle<Object> key = property->key()->handle(); | 3239 Handle<Object> key = property->key()->handle(); |
3205 Handle<Object> value = GetBoilerplateValue(property); | 3240 Handle<Object> value = GetBoilerplateValue(property->value()); |
3206 is_simple = is_simple && !value->IsUndefined(); | 3241 is_simple = is_simple && !value->IsUndefined(); |
3207 | 3242 |
3208 // Add name, value pair to the fixed array. | 3243 // Add name, value pair to the fixed array. |
3209 constant_properties->set(position++, *key); | 3244 constant_properties->set(position++, *key); |
3210 constant_properties->set(position++, *value); | 3245 constant_properties->set(position++, *value); |
3211 } | 3246 } |
3212 | 3247 |
3213 return new ObjectLiteral(constant_properties, | 3248 return new ObjectLiteral(constant_properties, |
3214 properties.elements(), | 3249 properties.elements(), |
3215 literal_index, | 3250 literal_index, |
(...skipping 1312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4528 start_position, | 4563 start_position, |
4529 is_expression); | 4564 is_expression); |
4530 return result; | 4565 return result; |
4531 } | 4566 } |
4532 | 4567 |
4533 | 4568 |
4534 #undef NEW | 4569 #undef NEW |
4535 | 4570 |
4536 | 4571 |
4537 } } // namespace v8::internal | 4572 } } // namespace v8::internal |
OLD | NEW |