Chromium Code Reviews| 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( | 17 static Handle<Map> ComputeObjectLiteralMap( |
| 18 Handle<Context> context, Handle<FixedArray> constant_properties, | 18 Handle<Context> context, |
| 19 Handle<BoilerplateDescription> boilerplate_description, | |
| 19 bool* is_result_from_cache) { | 20 bool* is_result_from_cache) { |
| 20 int properties_length = constant_properties->length(); | 21 int number_of_properties = |
|
gsathya
2017/01/18 17:48:18
Just something to keep in mind, this is going to b
| |
| 21 int number_of_properties = properties_length / 2; | 22 boilerplate_description->number_of_all_properties(); |
| 22 | 23 |
| 23 for (int p = 0; p != properties_length; p += 2) { | 24 for (int index = 0; index < boilerplate_description->size(); index++) { |
| 24 Object* key = constant_properties->get(p); | 25 Object* key = boilerplate_description->name(index); |
| 25 uint32_t element_index = 0; | 26 uint32_t element_index = 0; |
| 26 if (key->ToArrayIndex(&element_index)) { | 27 if (key->ToArrayIndex(&element_index)) { |
| 27 // An index key does not require space in the property backing store. | 28 // An index key does not require space in the property backing store. |
| 28 number_of_properties--; | 29 number_of_properties--; |
| 29 } | 30 } |
| 30 } | 31 } |
| 31 Isolate* isolate = context->GetIsolate(); | 32 Isolate* isolate = context->GetIsolate(); |
| 32 return isolate->factory()->ObjectLiteralMapFromCache( | 33 return isolate->factory()->ObjectLiteralMapFromCache( |
| 33 context, number_of_properties, is_result_from_cache); | 34 context, number_of_properties, is_result_from_cache); |
| 34 } | 35 } |
| 35 | 36 |
| 36 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate( | 37 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate( |
| 37 Isolate* isolate, Handle<LiteralsArray> literals, | 38 Isolate* isolate, Handle<LiteralsArray> literals, |
| 38 Handle<FixedArray> constant_properties); | 39 Handle<BoilerplateDescription> boilerplate_description); |
| 39 | 40 |
| 40 MUST_USE_RESULT static MaybeHandle<Object> CreateObjectLiteralBoilerplate( | 41 MUST_USE_RESULT static MaybeHandle<Object> CreateObjectLiteralBoilerplate( |
| 41 Isolate* isolate, Handle<LiteralsArray> literals, | 42 Isolate* isolate, Handle<LiteralsArray> literals, |
| 42 Handle<FixedArray> constant_properties, bool should_have_fast_elements) { | 43 Handle<BoilerplateDescription> boilerplate_description, |
| 44 bool should_have_fast_elements) { | |
| 43 Handle<Context> context = isolate->native_context(); | 45 Handle<Context> context = isolate->native_context(); |
| 44 | 46 |
| 45 // In case we have function literals, we want the object to be in | 47 // In case we have function literals, we want the object to be in |
| 46 // slow properties mode for now. We don't go in the map cache because | 48 // slow properties mode for now. We don't go in the map cache because |
| 47 // maps with constant functions can't be shared if the functions are | 49 // maps with constant functions can't be shared if the functions are |
| 48 // not the same (which is the common case). | 50 // not the same (which is the common case). |
| 49 bool is_result_from_cache = false; | 51 bool is_result_from_cache = false; |
| 50 Handle<Map> map = ComputeObjectLiteralMap(context, constant_properties, | 52 Handle<Map> map = ComputeObjectLiteralMap(context, boilerplate_description, |
| 51 &is_result_from_cache); | 53 &is_result_from_cache); |
| 52 | 54 |
| 53 PretenureFlag pretenure_flag = | 55 PretenureFlag pretenure_flag = |
| 54 isolate->heap()->InNewSpace(*literals) ? NOT_TENURED : TENURED; | 56 isolate->heap()->InNewSpace(*literals) ? NOT_TENURED : TENURED; |
| 55 | 57 |
| 56 Handle<JSObject> boilerplate = | 58 Handle<JSObject> boilerplate = |
| 57 isolate->factory()->NewJSObjectFromMap(map, pretenure_flag); | 59 isolate->factory()->NewJSObjectFromMap(map, pretenure_flag); |
| 58 | 60 |
| 59 // Normalize the elements of the boilerplate to save space if needed. | 61 // Normalize the elements of the boilerplate to save space if needed. |
| 60 if (!should_have_fast_elements) JSObject::NormalizeElements(boilerplate); | 62 if (!should_have_fast_elements) JSObject::NormalizeElements(boilerplate); |
| 61 | 63 |
| 62 // Add the constant properties to the boilerplate. | 64 // Add the constant properties to the boilerplate. |
| 63 int length = constant_properties->length(); | 65 int length = boilerplate_description->size(); |
| 64 bool should_transform = | 66 bool should_transform = |
| 65 !is_result_from_cache && boilerplate->HasFastProperties(); | 67 !is_result_from_cache && boilerplate->HasFastProperties(); |
| 66 bool should_normalize = should_transform; | 68 bool should_normalize = should_transform; |
| 67 if (should_normalize) { | 69 if (should_normalize) { |
| 68 // TODO(verwaest): We might not want to ever normalize here. | 70 // TODO(verwaest): We might not want to ever normalize here. |
| 69 JSObject::NormalizeProperties(boilerplate, KEEP_INOBJECT_PROPERTIES, | 71 JSObject::NormalizeProperties(boilerplate, KEEP_INOBJECT_PROPERTIES, length, |
| 70 length / 2, "Boilerplate"); | 72 "Boilerplate"); |
| 71 } | 73 } |
| 72 // TODO(verwaest): Support tracking representations in the boilerplate. | 74 // TODO(verwaest): Support tracking representations in the boilerplate. |
| 73 for (int index = 0; index < length; index += 2) { | 75 for (int index = 0; index < length; index++) { |
| 74 Handle<Object> key(constant_properties->get(index + 0), isolate); | 76 Handle<Object> key(boilerplate_description->name(index), isolate); |
| 75 Handle<Object> value(constant_properties->get(index + 1), isolate); | 77 Handle<Object> value(boilerplate_description->value(index), isolate); |
| 76 if (value->IsFixedArray()) { | 78 if (value->IsFixedArray()) { |
| 77 // The value contains the constant_properties of a | 79 // The value contains the boilerplate properties of a |
| 78 // simple object or array literal. | 80 // simple object or array literal. |
| 79 Handle<FixedArray> array = Handle<FixedArray>::cast(value); | 81 Handle<BoilerplateDescription> array = |
| 82 Handle<BoilerplateDescription>::cast(value); | |
| 80 ASSIGN_RETURN_ON_EXCEPTION( | 83 ASSIGN_RETURN_ON_EXCEPTION( |
| 81 isolate, value, CreateLiteralBoilerplate(isolate, literals, array), | 84 isolate, value, CreateLiteralBoilerplate(isolate, literals, array), |
| 82 Object); | 85 Object); |
| 83 } | 86 } |
| 84 MaybeHandle<Object> maybe_result; | 87 MaybeHandle<Object> maybe_result; |
| 85 uint32_t element_index = 0; | 88 uint32_t element_index = 0; |
| 86 if (key->ToArrayIndex(&element_index)) { | 89 if (key->ToArrayIndex(&element_index)) { |
| 87 // Array index (uint32). | 90 // Array index (uint32). |
| 88 if (value->IsUninitialized(isolate)) { | 91 if (value->IsUninitialized(isolate)) { |
| 89 value = handle(Smi::kZero, isolate); | 92 value = handle(Smi::kZero, isolate); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 #endif | 158 #endif |
| 156 } else { | 159 } else { |
| 157 Handle<FixedArray> fixed_array_values = | 160 Handle<FixedArray> fixed_array_values = |
| 158 Handle<FixedArray>::cast(constant_elements_values); | 161 Handle<FixedArray>::cast(constant_elements_values); |
| 159 Handle<FixedArray> fixed_array_values_copy = | 162 Handle<FixedArray> fixed_array_values_copy = |
| 160 isolate->factory()->CopyFixedArray(fixed_array_values); | 163 isolate->factory()->CopyFixedArray(fixed_array_values); |
| 161 copied_elements_values = fixed_array_values_copy; | 164 copied_elements_values = fixed_array_values_copy; |
| 162 FOR_WITH_HANDLE_SCOPE( | 165 FOR_WITH_HANDLE_SCOPE( |
| 163 isolate, int, i = 0, i, i < fixed_array_values->length(), i++, { | 166 isolate, int, i = 0, i, i < fixed_array_values->length(), i++, { |
| 164 if (fixed_array_values->get(i)->IsFixedArray()) { | 167 if (fixed_array_values->get(i)->IsFixedArray()) { |
| 165 // The value contains the constant_properties of a | 168 // The value contains the boilerplate properties of a |
| 166 // simple object or array literal. | 169 // simple object or array literal. |
| 167 Handle<FixedArray> fa( | 170 Handle<BoilerplateDescription> fa( |
| 168 FixedArray::cast(fixed_array_values->get(i))); | 171 BoilerplateDescription::cast(fixed_array_values->get(i))); |
| 169 Handle<Object> result; | 172 Handle<Object> result; |
| 170 ASSIGN_RETURN_ON_EXCEPTION( | 173 ASSIGN_RETURN_ON_EXCEPTION( |
| 171 isolate, result, | 174 isolate, result, |
| 172 CreateLiteralBoilerplate(isolate, literals, fa), Object); | 175 CreateLiteralBoilerplate(isolate, literals, fa), Object); |
| 173 fixed_array_values_copy->set(i, *result); | 176 fixed_array_values_copy->set(i, *result); |
| 174 } | 177 } |
| 175 }); | 178 }); |
| 176 } | 179 } |
| 177 } | 180 } |
| 178 object->set_elements(*copied_elements_values); | 181 object->set_elements(*copied_elements_values); |
| 179 object->set_length(Smi::FromInt(copied_elements_values->length())); | 182 object->set_length(Smi::FromInt(copied_elements_values->length())); |
| 180 | 183 |
| 181 JSObject::ValidateElements(object); | 184 JSObject::ValidateElements(object); |
| 182 return object; | 185 return object; |
| 183 } | 186 } |
| 184 | 187 |
| 185 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate( | 188 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate( |
| 186 Isolate* isolate, Handle<LiteralsArray> literals, | 189 Isolate* isolate, Handle<LiteralsArray> literals, |
| 187 Handle<FixedArray> array) { | 190 Handle<BoilerplateDescription> array) { |
| 188 Handle<HeapObject> elements = CompileTimeValue::GetElements(array); | 191 Handle<HeapObject> elements = CompileTimeValue::GetElements(array); |
| 189 switch (CompileTimeValue::GetLiteralType(array)) { | 192 switch (CompileTimeValue::GetLiteralType(array)) { |
| 190 case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: { | 193 case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: { |
| 191 Handle<FixedArray> props = Handle<FixedArray>::cast(elements); | 194 Handle<BoilerplateDescription> props = |
| 195 Handle<BoilerplateDescription>::cast(elements); | |
| 192 return CreateObjectLiteralBoilerplate(isolate, literals, props, true); | 196 return CreateObjectLiteralBoilerplate(isolate, literals, props, true); |
| 193 } | 197 } |
| 194 case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: { | 198 case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: { |
| 195 Handle<FixedArray> props = Handle<FixedArray>::cast(elements); | 199 Handle<BoilerplateDescription> props = |
| 200 Handle<BoilerplateDescription>::cast(elements); | |
| 196 return CreateObjectLiteralBoilerplate(isolate, literals, props, false); | 201 return CreateObjectLiteralBoilerplate(isolate, literals, props, false); |
| 197 } | 202 } |
| 198 case CompileTimeValue::ARRAY_LITERAL: { | 203 case CompileTimeValue::ARRAY_LITERAL: { |
| 199 Handle<ConstantElementsPair> elems = | 204 Handle<ConstantElementsPair> elems = |
| 200 Handle<ConstantElementsPair>::cast(elements); | 205 Handle<ConstantElementsPair>::cast(elements); |
| 201 return CreateArrayLiteralBoilerplate(isolate, literals, elems); | 206 return CreateArrayLiteralBoilerplate(isolate, literals, elems); |
| 202 } | 207 } |
| 203 default: | 208 default: |
| 204 UNREACHABLE(); | 209 UNREACHABLE(); |
| 205 return MaybeHandle<Object>(); | 210 return MaybeHandle<Object>(); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 224 } | 229 } |
| 225 return *JSRegExp::Copy(Handle<JSRegExp>::cast(boilerplate)); | 230 return *JSRegExp::Copy(Handle<JSRegExp>::cast(boilerplate)); |
| 226 } | 231 } |
| 227 | 232 |
| 228 | 233 |
| 229 RUNTIME_FUNCTION(Runtime_CreateObjectLiteral) { | 234 RUNTIME_FUNCTION(Runtime_CreateObjectLiteral) { |
| 230 HandleScope scope(isolate); | 235 HandleScope scope(isolate); |
| 231 DCHECK_EQ(4, args.length()); | 236 DCHECK_EQ(4, args.length()); |
| 232 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0); | 237 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0); |
| 233 CONVERT_SMI_ARG_CHECKED(literals_index, 1); | 238 CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
| 234 CONVERT_ARG_HANDLE_CHECKED(FixedArray, constant_properties, 2); | 239 CONVERT_ARG_HANDLE_CHECKED(BoilerplateDescription, boilerplate_description, |
| 240 2); | |
| 235 CONVERT_SMI_ARG_CHECKED(flags, 3); | 241 CONVERT_SMI_ARG_CHECKED(flags, 3); |
| 236 Handle<LiteralsArray> literals(closure->literals(), isolate); | 242 Handle<LiteralsArray> literals(closure->literals(), isolate); |
| 237 bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; | 243 bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 238 bool enable_mementos = (flags & ObjectLiteral::kDisableMementos) == 0; | 244 bool enable_mementos = (flags & ObjectLiteral::kDisableMementos) == 0; |
| 239 | 245 |
| 240 CHECK(literals_index >= 0); | 246 CHECK(literals_index >= 0); |
| 241 CHECK(literals_index < literals->literals_count()); | 247 CHECK(literals_index < literals->literals_count()); |
| 242 | 248 |
| 243 // Check if boilerplate exists. If not, create it first. | 249 // Check if boilerplate exists. If not, create it first. |
| 244 Handle<Object> literal_site(literals->literal(literals_index), isolate); | 250 Handle<Object> literal_site(literals->literal(literals_index), isolate); |
| 245 Handle<AllocationSite> site; | 251 Handle<AllocationSite> site; |
| 246 Handle<JSObject> boilerplate; | 252 Handle<JSObject> boilerplate; |
| 247 if (literal_site->IsUndefined(isolate)) { | 253 if (literal_site->IsUndefined(isolate)) { |
| 248 Handle<Object> raw_boilerplate; | 254 Handle<Object> raw_boilerplate; |
| 249 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 255 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 250 isolate, raw_boilerplate, | 256 isolate, raw_boilerplate, |
| 251 CreateObjectLiteralBoilerplate(isolate, literals, constant_properties, | 257 CreateObjectLiteralBoilerplate(isolate, literals, |
| 258 boilerplate_description, | |
| 252 should_have_fast_elements)); | 259 should_have_fast_elements)); |
| 253 boilerplate = Handle<JSObject>::cast(raw_boilerplate); | 260 boilerplate = Handle<JSObject>::cast(raw_boilerplate); |
| 254 | 261 |
| 255 AllocationSiteCreationContext creation_context(isolate); | 262 AllocationSiteCreationContext creation_context(isolate); |
| 256 site = creation_context.EnterNewScope(); | 263 site = creation_context.EnterNewScope(); |
| 257 RETURN_FAILURE_ON_EXCEPTION( | 264 RETURN_FAILURE_ON_EXCEPTION( |
| 258 isolate, JSObject::DeepWalk(boilerplate, &creation_context)); | 265 isolate, JSObject::DeepWalk(boilerplate, &creation_context)); |
| 259 creation_context.ExitScope(site, boilerplate); | 266 creation_context.ExitScope(site, boilerplate); |
| 260 | 267 |
| 261 // Update the functions literal and return the boilerplate. | 268 // Update the functions literal and return the boilerplate. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 | 358 |
| 352 Handle<LiteralsArray> literals(closure->literals(), isolate); | 359 Handle<LiteralsArray> literals(closure->literals(), isolate); |
| 353 RETURN_RESULT_OR_FAILURE( | 360 RETURN_RESULT_OR_FAILURE( |
| 354 isolate, | 361 isolate, |
| 355 CreateArrayLiteralImpl(isolate, literals, literals_index, elements, | 362 CreateArrayLiteralImpl(isolate, literals, literals_index, elements, |
| 356 ArrayLiteral::kShallowElements)); | 363 ArrayLiteral::kShallowElements)); |
| 357 } | 364 } |
| 358 | 365 |
| 359 } // namespace internal | 366 } // namespace internal |
| 360 } // namespace v8 | 367 } // namespace v8 |
| OLD | NEW |