| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
| 6 | 6 |
| 7 #include "src/allocation-site-scopes.h" | 7 #include "src/allocation-site-scopes.h" |
| 8 #include "src/arguments.h" | 8 #include "src/arguments.h" |
| 9 #include "src/ast/ast.h" | 9 #include "src/ast/ast.h" |
| 10 #include "src/ast/compile-time-value.h" | 10 #include "src/ast/compile-time-value.h" |
| 11 #include "src/isolate-inl.h" | 11 #include "src/isolate-inl.h" |
| 12 #include "src/runtime/runtime.h" | 12 #include "src/runtime/runtime.h" |
| 13 | 13 |
| 14 namespace v8 { | 14 namespace v8 { |
| 15 namespace internal { | 15 namespace internal { |
| 16 | 16 |
| 17 static Handle<Map> ComputeObjectLiteralMap( | |
| 18 Handle<Context> context, | |
| 19 Handle<BoilerplateDescription> boilerplate_description, | |
| 20 bool* is_result_from_cache) { | |
| 21 int number_of_properties = boilerplate_description->backing_store_size(); | |
| 22 Isolate* isolate = context->GetIsolate(); | |
| 23 return isolate->factory()->ObjectLiteralMapFromCache( | |
| 24 context, number_of_properties, is_result_from_cache); | |
| 25 } | |
| 26 | |
| 27 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate( | 17 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate( |
| 28 Isolate* isolate, Handle<FeedbackVector> vector, | 18 Isolate* isolate, Handle<FeedbackVector> vector, |
| 29 Handle<BoilerplateDescription> boilerplate_description); | 19 Handle<FixedArray> compile_time_value); |
| 30 | 20 |
| 31 MUST_USE_RESULT static MaybeHandle<Object> CreateObjectLiteralBoilerplate( | 21 MUST_USE_RESULT static MaybeHandle<Object> CreateObjectLiteralBoilerplate( |
| 32 Isolate* isolate, Handle<FeedbackVector> vector, | 22 Isolate* isolate, Handle<FeedbackVector> vector, |
| 33 Handle<BoilerplateDescription> boilerplate_description, | 23 Handle<BoilerplateDescription> boilerplate_description, |
| 34 bool should_have_fast_elements) { | 24 bool use_fast_elements, bool has_null_prototype) { |
| 35 Handle<Context> context = isolate->native_context(); | 25 Handle<Context> context = isolate->native_context(); |
| 36 | 26 |
| 37 // In case we have function literals, we want the object to be in | 27 // In case we have function literals, we want the object to be in |
| 38 // slow properties mode for now. We don't go in the map cache because | 28 // slow properties mode for now. We don't go in the map cache because |
| 39 // maps with constant functions can't be shared if the functions are | 29 // maps with constant functions can't be shared if the functions are |
| 40 // not the same (which is the common case). | 30 // not the same (which is the common case). |
| 41 bool is_result_from_cache = false; | 31 int number_of_properties = boilerplate_description->backing_store_size(); |
| 42 Handle<Map> map = ComputeObjectLiteralMap(context, boilerplate_description, | 32 Handle<Map> map = isolate->factory()->ObjectLiteralMapFromCache( |
| 43 &is_result_from_cache); | 33 context, number_of_properties, has_null_prototype); |
| 44 | 34 |
| 45 PretenureFlag pretenure_flag = | 35 PretenureFlag pretenure_flag = |
| 46 isolate->heap()->InNewSpace(*vector) ? NOT_TENURED : TENURED; | 36 isolate->heap()->InNewSpace(*vector) ? NOT_TENURED : TENURED; |
| 47 | 37 |
| 48 Handle<JSObject> boilerplate = | 38 Handle<JSObject> boilerplate; |
| 49 isolate->factory()->NewJSObjectFromMap(map, pretenure_flag); | 39 if (map->is_dictionary_map()) { |
| 40 boilerplate = isolate->factory()->NewSlowJSObjectFromMap( |
| 41 map, number_of_properties, pretenure_flag); |
| 42 } else { |
| 43 boilerplate = isolate->factory()->NewJSObjectFromMap(map, pretenure_flag); |
| 44 } |
| 50 | 45 |
| 51 // Normalize the elements of the boilerplate to save space if needed. | 46 // Normalize the elements of the boilerplate to save space if needed. |
| 52 if (!should_have_fast_elements) JSObject::NormalizeElements(boilerplate); | 47 if (!use_fast_elements) JSObject::NormalizeElements(boilerplate); |
| 53 | 48 |
| 54 // Add the constant properties to the boilerplate. | 49 // Add the constant properties to the boilerplate. |
| 55 int length = boilerplate_description->size(); | 50 int length = boilerplate_description->size(); |
| 56 bool should_transform = | |
| 57 !is_result_from_cache && boilerplate->HasFastProperties(); | |
| 58 bool should_normalize = should_transform; | |
| 59 if (should_normalize) { | |
| 60 // TODO(verwaest): We might not want to ever normalize here. | |
| 61 JSObject::NormalizeProperties(boilerplate, KEEP_INOBJECT_PROPERTIES, length, | |
| 62 "Boilerplate"); | |
| 63 } | |
| 64 // TODO(verwaest): Support tracking representations in the boilerplate. | 51 // TODO(verwaest): Support tracking representations in the boilerplate. |
| 65 for (int index = 0; index < length; index++) { | 52 for (int index = 0; index < length; index++) { |
| 66 Handle<Object> key(boilerplate_description->name(index), isolate); | 53 Handle<Object> key(boilerplate_description->name(index), isolate); |
| 67 Handle<Object> value(boilerplate_description->value(index), isolate); | 54 Handle<Object> value(boilerplate_description->value(index), isolate); |
| 68 if (value->IsBoilerplateDescription()) { | 55 if (value->IsFixedArray()) { |
| 69 // The value contains the boilerplate properties of a | 56 // The value contains the CompileTimeValue with the boilerplate properties |
| 70 // simple object or array literal. | 57 // of a simple object or array literal. |
| 71 Handle<BoilerplateDescription> boilerplate = | 58 Handle<FixedArray> compile_time_value = Handle<FixedArray>::cast(value); |
| 72 Handle<BoilerplateDescription>::cast(value); | |
| 73 ASSIGN_RETURN_ON_EXCEPTION( | 59 ASSIGN_RETURN_ON_EXCEPTION( |
| 74 isolate, value, | 60 isolate, value, |
| 75 CreateLiteralBoilerplate(isolate, vector, boilerplate), Object); | 61 CreateLiteralBoilerplate(isolate, vector, compile_time_value), |
| 62 Object); |
| 76 } | 63 } |
| 77 MaybeHandle<Object> maybe_result; | 64 MaybeHandle<Object> maybe_result; |
| 78 uint32_t element_index = 0; | 65 uint32_t element_index = 0; |
| 79 if (key->ToArrayIndex(&element_index)) { | 66 if (key->ToArrayIndex(&element_index)) { |
| 80 // Array index (uint32). | 67 // Array index (uint32). |
| 81 if (value->IsUninitialized(isolate)) { | 68 if (value->IsUninitialized(isolate)) { |
| 82 value = handle(Smi::kZero, isolate); | 69 value = handle(Smi::kZero, isolate); |
| 83 } | 70 } |
| 84 maybe_result = JSObject::SetOwnElementIgnoreAttributes( | 71 maybe_result = JSObject::SetOwnElementIgnoreAttributes( |
| 85 boilerplate, element_index, value, NONE); | 72 boilerplate, element_index, value, NONE); |
| 86 } else { | 73 } else { |
| 87 Handle<String> name = Handle<String>::cast(key); | 74 Handle<String> name = Handle<String>::cast(key); |
| 88 DCHECK(!name->AsArrayIndex(&element_index)); | 75 DCHECK(!name->AsArrayIndex(&element_index)); |
| 89 maybe_result = JSObject::SetOwnPropertyIgnoreAttributes(boilerplate, name, | 76 maybe_result = JSObject::SetOwnPropertyIgnoreAttributes(boilerplate, name, |
| 90 value, NONE); | 77 value, NONE); |
| 91 } | 78 } |
| 92 RETURN_ON_EXCEPTION(isolate, maybe_result, Object); | 79 RETURN_ON_EXCEPTION(isolate, maybe_result, Object); |
| 93 } | 80 } |
| 94 | 81 |
| 95 // Transform to fast properties if necessary. For object literals with | 82 if (map->is_dictionary_map() && !has_null_prototype) { |
| 96 // containing function literals we defer this operation until after all | 83 // TODO(cbruni): avoid making the boilerplate fast again, the clone stub |
| 97 // computed properties have been assigned so that we can generate | 84 // supports dict-mode objects directly. |
| 98 // constant function properties. | |
| 99 if (should_transform) { | |
| 100 JSObject::MigrateSlowToFast(boilerplate, | 85 JSObject::MigrateSlowToFast(boilerplate, |
| 101 boilerplate->map()->unused_property_fields(), | 86 boilerplate->map()->unused_property_fields(), |
| 102 "FastLiteral"); | 87 "FastLiteral"); |
| 103 } | 88 } |
| 104 return boilerplate; | 89 return boilerplate; |
| 105 } | 90 } |
| 106 | 91 |
| 107 static MaybeHandle<Object> CreateArrayLiteralBoilerplate( | 92 static MaybeHandle<Object> CreateArrayLiteralBoilerplate( |
| 108 Isolate* isolate, Handle<FeedbackVector> vector, | 93 Isolate* isolate, Handle<FeedbackVector> vector, |
| 109 Handle<ConstantElementsPair> elements) { | 94 Handle<ConstantElementsPair> elements) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 } | 132 } |
| 148 #endif | 133 #endif |
| 149 } else { | 134 } else { |
| 150 Handle<FixedArray> fixed_array_values = | 135 Handle<FixedArray> fixed_array_values = |
| 151 Handle<FixedArray>::cast(constant_elements_values); | 136 Handle<FixedArray>::cast(constant_elements_values); |
| 152 Handle<FixedArray> fixed_array_values_copy = | 137 Handle<FixedArray> fixed_array_values_copy = |
| 153 isolate->factory()->CopyFixedArray(fixed_array_values); | 138 isolate->factory()->CopyFixedArray(fixed_array_values); |
| 154 copied_elements_values = fixed_array_values_copy; | 139 copied_elements_values = fixed_array_values_copy; |
| 155 FOR_WITH_HANDLE_SCOPE( | 140 FOR_WITH_HANDLE_SCOPE( |
| 156 isolate, int, i = 0, i, i < fixed_array_values->length(), i++, { | 141 isolate, int, i = 0, i, i < fixed_array_values->length(), i++, { |
| 157 if (fixed_array_values->get(i)->IsBoilerplateDescription()) { | 142 if (fixed_array_values->get(i)->IsFixedArray()) { |
| 158 // The value contains the boilerplate properties of a | 143 // The value contains the CompileTimeValue with the |
| 159 // simple object or array literal. | 144 // boilerplate description of a simple object or |
| 160 Handle<BoilerplateDescription> boilerplate( | 145 // array literal. |
| 161 BoilerplateDescription::cast(fixed_array_values->get(i))); | 146 Handle<FixedArray> compile_time_value( |
| 147 FixedArray::cast(fixed_array_values->get(i))); |
| 162 Handle<Object> result; | 148 Handle<Object> result; |
| 163 ASSIGN_RETURN_ON_EXCEPTION( | 149 ASSIGN_RETURN_ON_EXCEPTION( |
| 164 isolate, result, | 150 isolate, result, |
| 165 CreateLiteralBoilerplate(isolate, vector, boilerplate), | 151 CreateLiteralBoilerplate(isolate, vector, compile_time_value), |
| 166 Object); | 152 Object); |
| 167 fixed_array_values_copy->set(i, *result); | 153 fixed_array_values_copy->set(i, *result); |
| 168 } | 154 } |
| 169 }); | 155 }); |
| 170 } | 156 } |
| 171 } | 157 } |
| 172 object->set_elements(*copied_elements_values); | 158 object->set_elements(*copied_elements_values); |
| 173 object->set_length(Smi::FromInt(copied_elements_values->length())); | 159 object->set_length(Smi::FromInt(copied_elements_values->length())); |
| 174 | 160 |
| 175 JSObject::ValidateElements(object); | 161 JSObject::ValidateElements(object); |
| 176 return object; | 162 return object; |
| 177 } | 163 } |
| 178 | 164 |
| 179 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate( | 165 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate( |
| 180 Isolate* isolate, Handle<FeedbackVector> vector, | 166 Isolate* isolate, Handle<FeedbackVector> vector, |
| 181 Handle<BoilerplateDescription> array) { | 167 Handle<FixedArray> compile_time_value) { |
| 182 Handle<HeapObject> elements = CompileTimeValue::GetElements(array); | 168 Handle<HeapObject> elements = |
| 183 switch (CompileTimeValue::GetLiteralType(array)) { | 169 CompileTimeValue::GetElements(compile_time_value); |
| 184 case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: { | 170 int flags = CompileTimeValue::GetLiteralTypeFlags(compile_time_value); |
| 185 Handle<BoilerplateDescription> props = | 171 if (flags == CompileTimeValue::kArrayLiteralFlag) { |
| 186 Handle<BoilerplateDescription>::cast(elements); | 172 Handle<ConstantElementsPair> elems = |
| 187 return CreateObjectLiteralBoilerplate(isolate, vector, props, true); | 173 Handle<ConstantElementsPair>::cast(elements); |
| 188 } | 174 return CreateArrayLiteralBoilerplate(isolate, vector, elems); |
| 189 case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: { | |
| 190 Handle<BoilerplateDescription> props = | |
| 191 Handle<BoilerplateDescription>::cast(elements); | |
| 192 return CreateObjectLiteralBoilerplate(isolate, vector, props, false); | |
| 193 } | |
| 194 case CompileTimeValue::ARRAY_LITERAL: { | |
| 195 Handle<ConstantElementsPair> elems = | |
| 196 Handle<ConstantElementsPair>::cast(elements); | |
| 197 return CreateArrayLiteralBoilerplate(isolate, vector, elems); | |
| 198 } | |
| 199 default: | |
| 200 UNREACHABLE(); | |
| 201 return MaybeHandle<Object>(); | |
| 202 } | 175 } |
| 176 Handle<BoilerplateDescription> props = |
| 177 Handle<BoilerplateDescription>::cast(elements); |
| 178 bool use_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 179 bool has_null_prototype = (flags & ObjectLiteral::kHasNullPrototype) != 0; |
| 180 return CreateObjectLiteralBoilerplate(isolate, vector, props, |
| 181 use_fast_elements, has_null_prototype); |
| 203 } | 182 } |
| 204 | 183 |
| 205 | 184 |
| 206 RUNTIME_FUNCTION(Runtime_CreateRegExpLiteral) { | 185 RUNTIME_FUNCTION(Runtime_CreateRegExpLiteral) { |
| 207 HandleScope scope(isolate); | 186 HandleScope scope(isolate); |
| 208 DCHECK_EQ(4, args.length()); | 187 DCHECK_EQ(4, args.length()); |
| 209 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0); | 188 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0); |
| 210 CONVERT_SMI_ARG_CHECKED(index, 1); | 189 CONVERT_SMI_ARG_CHECKED(index, 1); |
| 211 CONVERT_ARG_HANDLE_CHECKED(String, pattern, 2); | 190 CONVERT_ARG_HANDLE_CHECKED(String, pattern, 2); |
| 212 CONVERT_SMI_ARG_CHECKED(flags, 3); | 191 CONVERT_SMI_ARG_CHECKED(flags, 3); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 226 | 205 |
| 227 RUNTIME_FUNCTION(Runtime_CreateObjectLiteral) { | 206 RUNTIME_FUNCTION(Runtime_CreateObjectLiteral) { |
| 228 HandleScope scope(isolate); | 207 HandleScope scope(isolate); |
| 229 DCHECK_EQ(4, args.length()); | 208 DCHECK_EQ(4, args.length()); |
| 230 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0); | 209 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0); |
| 231 CONVERT_SMI_ARG_CHECKED(literals_index, 1); | 210 CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
| 232 CONVERT_ARG_HANDLE_CHECKED(BoilerplateDescription, boilerplate_description, | 211 CONVERT_ARG_HANDLE_CHECKED(BoilerplateDescription, boilerplate_description, |
| 233 2); | 212 2); |
| 234 CONVERT_SMI_ARG_CHECKED(flags, 3); | 213 CONVERT_SMI_ARG_CHECKED(flags, 3); |
| 235 Handle<FeedbackVector> vector(closure->feedback_vector(), isolate); | 214 Handle<FeedbackVector> vector(closure->feedback_vector(), isolate); |
| 236 bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; | 215 bool use_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 237 bool enable_mementos = (flags & ObjectLiteral::kDisableMementos) == 0; | 216 bool enable_mementos = (flags & ObjectLiteral::kDisableMementos) == 0; |
| 217 bool has_null_prototype = (flags & ObjectLiteral::kHasNullPrototype) != 0; |
| 238 | 218 |
| 239 FeedbackSlot literals_slot(FeedbackVector::ToSlot(literals_index)); | 219 FeedbackSlot literals_slot(FeedbackVector::ToSlot(literals_index)); |
| 240 CHECK(literals_slot.ToInt() < vector->slot_count()); | 220 CHECK(literals_slot.ToInt() < vector->slot_count()); |
| 241 | 221 |
| 242 // Check if boilerplate exists. If not, create it first. | 222 // Check if boilerplate exists. If not, create it first. |
| 243 Handle<Object> literal_site(vector->Get(literals_slot), isolate); | 223 Handle<Object> literal_site(vector->Get(literals_slot), isolate); |
| 244 Handle<AllocationSite> site; | 224 Handle<AllocationSite> site; |
| 245 Handle<JSObject> boilerplate; | 225 Handle<JSObject> boilerplate; |
| 246 if (literal_site->IsUndefined(isolate)) { | 226 if (literal_site->IsUndefined(isolate)) { |
| 247 Handle<Object> raw_boilerplate; | 227 Handle<Object> raw_boilerplate; |
| 248 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 228 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 249 isolate, raw_boilerplate, | 229 isolate, raw_boilerplate, |
| 250 CreateObjectLiteralBoilerplate(isolate, vector, boilerplate_description, | 230 CreateObjectLiteralBoilerplate(isolate, vector, boilerplate_description, |
| 251 should_have_fast_elements)); | 231 use_fast_elements, has_null_prototype)); |
| 252 boilerplate = Handle<JSObject>::cast(raw_boilerplate); | 232 boilerplate = Handle<JSObject>::cast(raw_boilerplate); |
| 253 | 233 |
| 254 AllocationSiteCreationContext creation_context(isolate); | 234 AllocationSiteCreationContext creation_context(isolate); |
| 255 site = creation_context.EnterNewScope(); | 235 site = creation_context.EnterNewScope(); |
| 256 RETURN_FAILURE_ON_EXCEPTION( | 236 RETURN_FAILURE_ON_EXCEPTION( |
| 257 isolate, JSObject::DeepWalk(boilerplate, &creation_context)); | 237 isolate, JSObject::DeepWalk(boilerplate, &creation_context)); |
| 258 creation_context.ExitScope(site, boilerplate); | 238 creation_context.ExitScope(site, boilerplate); |
| 259 | 239 |
| 260 // Update the functions literal and return the boilerplate. | 240 // Update the functions literal and return the boilerplate. |
| 261 vector->Set(literals_slot, *site); | 241 vector->Set(literals_slot, *site); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 | 331 |
| 352 Handle<FeedbackVector> vector(closure->feedback_vector(), isolate); | 332 Handle<FeedbackVector> vector(closure->feedback_vector(), isolate); |
| 353 FeedbackSlot literals_slot(FeedbackVector::ToSlot(literals_index)); | 333 FeedbackSlot literals_slot(FeedbackVector::ToSlot(literals_index)); |
| 354 RETURN_RESULT_OR_FAILURE( | 334 RETURN_RESULT_OR_FAILURE( |
| 355 isolate, CreateArrayLiteralImpl(isolate, vector, literals_slot, elements, | 335 isolate, CreateArrayLiteralImpl(isolate, vector, literals_slot, elements, |
| 356 ArrayLiteral::kShallowElements)); | 336 ArrayLiteral::kShallowElements)); |
| 357 } | 337 } |
| 358 | 338 |
| 359 } // namespace internal | 339 } // namespace internal |
| 360 } // namespace v8 | 340 } // namespace v8 |
| OLD | NEW |