Chromium Code Reviews

Side by Side Diff: src/runtime/runtime-literals.cc

Issue 2445333002: Ensure slow properties for simple {__proto__:null} literals. (Closed)
Patch Set: addressing nits Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
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<BoilerplateDescription> boilerplate_description);
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).
31 int number_of_properties = boilerplate_description->backing_store_size();
41 bool is_result_from_cache = false; 32 bool is_result_from_cache = false;
42 Handle<Map> map = ComputeObjectLiteralMap(context, boilerplate_description, 33 Handle<Map> map = isolate->factory()->ObjectLiteralMapFromCache(
43 &is_result_from_cache); 34 context, number_of_properties, has_null_prototype, &is_result_from_cache);
44 35
45 PretenureFlag pretenure_flag = 36 PretenureFlag pretenure_flag =
46 isolate->heap()->InNewSpace(*vector) ? NOT_TENURED : TENURED; 37 isolate->heap()->InNewSpace(*vector) ? NOT_TENURED : TENURED;
47 38
48 Handle<JSObject> boilerplate = 39 Handle<JSObject> boilerplate;
49 isolate->factory()->NewJSObjectFromMap(map, pretenure_flag); 40 if (map->is_dictionary_map()) {
41 boilerplate = isolate->factory()->NewSlowJSObjectFromMap(
42 map, number_of_properties, pretenure_flag);
43 } else {
44 boilerplate = isolate->factory()->NewJSObjectFromMap(map, pretenure_flag);
45 }
50 46
51 // Normalize the elements of the boilerplate to save space if needed. 47 // Normalize the elements of the boilerplate to save space if needed.
52 if (!should_have_fast_elements) JSObject::NormalizeElements(boilerplate); 48 if (!use_fast_elements) JSObject::NormalizeElements(boilerplate);
53 49
54 // Add the constant properties to the boilerplate. 50 // Add the constant properties to the boilerplate.
55 int length = boilerplate_description->size(); 51 int length = boilerplate_description->size();
56 bool should_transform = 52 bool should_transform =
57 !is_result_from_cache && boilerplate->HasFastProperties(); 53 !is_result_from_cache && boilerplate->HasFastProperties();
Toon Verwaest 2017/03/14 13:27:47 This code is a little weird. I think we can even C
Camillo Bruni 2017/03/17 16:40:55 added separate initial slow map for this case.
58 bool should_normalize = should_transform; 54 if (should_transform) {
59 if (should_normalize) {
60 // TODO(verwaest): We might not want to ever normalize here.
Toon Verwaest 2017/03/14 13:27:48 Why are you removing this?
Camillo Bruni 2017/03/17 16:40:55 readded.
61 JSObject::NormalizeProperties(boilerplate, KEEP_INOBJECT_PROPERTIES, length, 55 JSObject::NormalizeProperties(boilerplate, KEEP_INOBJECT_PROPERTIES, length,
62 "Boilerplate"); 56 "Boilerplate");
63 } 57 }
64 // TODO(verwaest): Support tracking representations in the boilerplate. 58 // TODO(verwaest): Support tracking representations in the boilerplate.
65 for (int index = 0; index < length; index++) { 59 for (int index = 0; index < length; index++) {
66 Handle<Object> key(boilerplate_description->name(index), isolate); 60 Handle<Object> key(boilerplate_description->name(index), isolate);
67 Handle<Object> value(boilerplate_description->value(index), isolate); 61 Handle<Object> value(boilerplate_description->value(index), isolate);
68 if (value->IsBoilerplateDescription()) { 62 if (value->IsBoilerplateDescription()) {
69 // The value contains the boilerplate properties of a 63 // The value contains the boilerplate properties of a
70 // simple object or array literal. 64 // simple object or array literal.
(...skipping 17 matching lines...)
88 DCHECK(!name->AsArrayIndex(&element_index)); 82 DCHECK(!name->AsArrayIndex(&element_index));
89 maybe_result = JSObject::SetOwnPropertyIgnoreAttributes(boilerplate, name, 83 maybe_result = JSObject::SetOwnPropertyIgnoreAttributes(boilerplate, name,
90 value, NONE); 84 value, NONE);
91 } 85 }
92 RETURN_ON_EXCEPTION(isolate, maybe_result, Object); 86 RETURN_ON_EXCEPTION(isolate, maybe_result, Object);
93 } 87 }
94 88
95 // Transform to fast properties if necessary. For object literals with 89 // Transform to fast properties if necessary. For object literals with
96 // containing function literals we defer this operation until after all 90 // containing function literals we defer this operation until after all
97 // computed properties have been assigned so that we can generate 91 // computed properties have been assigned so that we can generate
98 // constant function properties. 92 // constant function properties.
Toon Verwaest 2017/03/14 13:27:47 This comment doesn't make sense anymore afaict. We
Camillo Bruni 2017/03/17 16:40:55 removed.
99 if (should_transform) { 93 if (should_transform) {
Toon Verwaest 2017/03/14 13:27:48 I'm not actually sure whether it makes sense to ma
Camillo Bruni 2017/03/17 16:40:55 added TODO
100 JSObject::MigrateSlowToFast(boilerplate, 94 JSObject::MigrateSlowToFast(boilerplate,
101 boilerplate->map()->unused_property_fields(), 95 boilerplate->map()->unused_property_fields(),
102 "FastLiteral"); 96 "FastLiteral");
103 } 97 }
104 return boilerplate; 98 return boilerplate;
105 } 99 }
106 100
107 static MaybeHandle<Object> CreateArrayLiteralBoilerplate( 101 static MaybeHandle<Object> CreateArrayLiteralBoilerplate(
108 Isolate* isolate, Handle<FeedbackVector> vector, 102 Isolate* isolate, Handle<FeedbackVector> vector,
109 Handle<ConstantElementsPair> elements) { 103 Handle<ConstantElementsPair> elements) {
(...skipping 66 matching lines...)
176 return object; 170 return object;
177 } 171 }
178 172
179 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate( 173 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate(
180 Isolate* isolate, Handle<FeedbackVector> vector, 174 Isolate* isolate, Handle<FeedbackVector> vector,
181 Handle<BoilerplateDescription> array) { 175 Handle<BoilerplateDescription> array) {
182 Handle<HeapObject> elements = CompileTimeValue::GetElements(array); 176 Handle<HeapObject> elements = CompileTimeValue::GetElements(array);
183 switch (CompileTimeValue::GetLiteralType(array)) { 177 switch (CompileTimeValue::GetLiteralType(array)) {
184 case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: { 178 case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: {
185 Handle<BoilerplateDescription> props = 179 Handle<BoilerplateDescription> props =
186 Handle<BoilerplateDescription>::cast(elements); 180 Handle<BoilerplateDescription>::cast(elements);
Toon Verwaest 2017/03/14 13:27:48 If we make sure that props[0] contains __proto__:n
Camillo Bruni 2017/03/17 16:40:55 ack.
187 return CreateObjectLiteralBoilerplate(isolate, vector, props, true); 181 return CreateObjectLiteralBoilerplate(isolate, vector, props, true,
182 false);
188 } 183 }
189 case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: { 184 case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: {
190 Handle<BoilerplateDescription> props = 185 Handle<BoilerplateDescription> props =
191 Handle<BoilerplateDescription>::cast(elements); 186 Handle<BoilerplateDescription>::cast(elements);
192 return CreateObjectLiteralBoilerplate(isolate, vector, props, false); 187 return CreateObjectLiteralBoilerplate(isolate, vector, props, false,
188 false);
193 } 189 }
194 case CompileTimeValue::ARRAY_LITERAL: { 190 case CompileTimeValue::ARRAY_LITERAL: {
195 Handle<ConstantElementsPair> elems = 191 Handle<ConstantElementsPair> elems =
196 Handle<ConstantElementsPair>::cast(elements); 192 Handle<ConstantElementsPair>::cast(elements);
197 return CreateArrayLiteralBoilerplate(isolate, vector, elems); 193 return CreateArrayLiteralBoilerplate(isolate, vector, elems);
198 } 194 }
199 default: 195 default:
200 UNREACHABLE(); 196 UNREACHABLE();
201 return MaybeHandle<Object>(); 197 return MaybeHandle<Object>();
202 } 198 }
(...skipping 23 matching lines...)
226 222
227 RUNTIME_FUNCTION(Runtime_CreateObjectLiteral) { 223 RUNTIME_FUNCTION(Runtime_CreateObjectLiteral) {
228 HandleScope scope(isolate); 224 HandleScope scope(isolate);
229 DCHECK_EQ(4, args.length()); 225 DCHECK_EQ(4, args.length());
230 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0); 226 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0);
231 CONVERT_SMI_ARG_CHECKED(literals_index, 1); 227 CONVERT_SMI_ARG_CHECKED(literals_index, 1);
232 CONVERT_ARG_HANDLE_CHECKED(BoilerplateDescription, boilerplate_description, 228 CONVERT_ARG_HANDLE_CHECKED(BoilerplateDescription, boilerplate_description,
233 2); 229 2);
234 CONVERT_SMI_ARG_CHECKED(flags, 3); 230 CONVERT_SMI_ARG_CHECKED(flags, 3);
235 Handle<FeedbackVector> vector(closure->feedback_vector(), isolate); 231 Handle<FeedbackVector> vector(closure->feedback_vector(), isolate);
236 bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; 232 bool use_fast_elements = (flags & ObjectLiteral::kFastElements) != 0;
237 bool enable_mementos = (flags & ObjectLiteral::kDisableMementos) == 0; 233 bool enable_mementos = (flags & ObjectLiteral::kDisableMementos) == 0;
234 bool has_null_prototype = (flags & ObjectLiteral::kHasNullPrototype) != 0;
238 235
239 FeedbackSlot literals_slot(FeedbackVector::ToSlot(literals_index)); 236 FeedbackSlot literals_slot(FeedbackVector::ToSlot(literals_index));
240 CHECK(literals_slot.ToInt() < vector->slot_count()); 237 CHECK(literals_slot.ToInt() < vector->slot_count());
241 238
242 // Check if boilerplate exists. If not, create it first. 239 // Check if boilerplate exists. If not, create it first.
243 Handle<Object> literal_site(vector->Get(literals_slot), isolate); 240 Handle<Object> literal_site(vector->Get(literals_slot), isolate);
244 Handle<AllocationSite> site; 241 Handle<AllocationSite> site;
245 Handle<JSObject> boilerplate; 242 Handle<JSObject> boilerplate;
246 if (literal_site->IsUndefined(isolate)) { 243 if (literal_site->IsUndefined(isolate)) {
247 Handle<Object> raw_boilerplate; 244 Handle<Object> raw_boilerplate;
248 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 245 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
249 isolate, raw_boilerplate, 246 isolate, raw_boilerplate,
250 CreateObjectLiteralBoilerplate(isolate, vector, boilerplate_description, 247 CreateObjectLiteralBoilerplate(isolate, vector, boilerplate_description,
251 should_have_fast_elements)); 248 use_fast_elements, has_null_prototype));
252 boilerplate = Handle<JSObject>::cast(raw_boilerplate); 249 boilerplate = Handle<JSObject>::cast(raw_boilerplate);
253 250
254 AllocationSiteCreationContext creation_context(isolate); 251 AllocationSiteCreationContext creation_context(isolate);
255 site = creation_context.EnterNewScope(); 252 site = creation_context.EnterNewScope();
256 RETURN_FAILURE_ON_EXCEPTION( 253 RETURN_FAILURE_ON_EXCEPTION(
257 isolate, JSObject::DeepWalk(boilerplate, &creation_context)); 254 isolate, JSObject::DeepWalk(boilerplate, &creation_context));
258 creation_context.ExitScope(site, boilerplate); 255 creation_context.ExitScope(site, boilerplate);
259 256
260 // Update the functions literal and return the boilerplate. 257 // Update the functions literal and return the boilerplate.
261 vector->Set(literals_slot, *site); 258 vector->Set(literals_slot, *site);
(...skipping 89 matching lines...)
351 348
352 Handle<FeedbackVector> vector(closure->feedback_vector(), isolate); 349 Handle<FeedbackVector> vector(closure->feedback_vector(), isolate);
353 FeedbackSlot literals_slot(FeedbackVector::ToSlot(literals_index)); 350 FeedbackSlot literals_slot(FeedbackVector::ToSlot(literals_index));
354 RETURN_RESULT_OR_FAILURE( 351 RETURN_RESULT_OR_FAILURE(
355 isolate, CreateArrayLiteralImpl(isolate, vector, literals_slot, elements, 352 isolate, CreateArrayLiteralImpl(isolate, vector, literals_slot, elements,
356 ArrayLiteral::kShallowElements)); 353 ArrayLiteral::kShallowElements));
357 } 354 }
358 355
359 } // namespace internal 356 } // namespace internal
360 } // namespace v8 357 } // namespace v8
OLDNEW

Powered by Google App Engine