Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

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

Issue 2581683003: Introduce {ConstantElementsPair} struct for type safety. (Closed)
Patch Set: Rebased. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
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"
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 if (should_transform) { 106 if (should_transform) {
107 JSObject::MigrateSlowToFast(boilerplate, 107 JSObject::MigrateSlowToFast(boilerplate,
108 boilerplate->map()->unused_property_fields(), 108 boilerplate->map()->unused_property_fields(),
109 "FastLiteral"); 109 "FastLiteral");
110 } 110 }
111 return boilerplate; 111 return boilerplate;
112 } 112 }
113 113
114 static MaybeHandle<Object> CreateArrayLiteralBoilerplate( 114 static MaybeHandle<Object> CreateArrayLiteralBoilerplate(
115 Isolate* isolate, Handle<LiteralsArray> literals, 115 Isolate* isolate, Handle<LiteralsArray> literals,
116 Handle<FixedArray> elements) { 116 Handle<ConstantElementsPair> elements) {
117 // Create the JSArray. 117 // Create the JSArray.
118 Handle<JSFunction> constructor = isolate->array_function(); 118 Handle<JSFunction> constructor = isolate->array_function();
119 119
120 PretenureFlag pretenure_flag = 120 PretenureFlag pretenure_flag =
121 isolate->heap()->InNewSpace(*literals) ? NOT_TENURED : TENURED; 121 isolate->heap()->InNewSpace(*literals) ? NOT_TENURED : TENURED;
122 122
123 Handle<JSArray> object = Handle<JSArray>::cast( 123 Handle<JSArray> object = Handle<JSArray>::cast(
124 isolate->factory()->NewJSObject(constructor, pretenure_flag)); 124 isolate->factory()->NewJSObject(constructor, pretenure_flag));
125 125
126 ElementsKind constant_elements_kind = 126 ElementsKind constant_elements_kind =
127 static_cast<ElementsKind>(Smi::cast(elements->get(0))->value()); 127 static_cast<ElementsKind>(elements->elements_kind());
128 Handle<FixedArrayBase> constant_elements_values( 128 Handle<FixedArrayBase> constant_elements_values(elements->constant_values());
129 FixedArrayBase::cast(elements->get(1)));
130 129
131 { 130 {
132 DisallowHeapAllocation no_gc; 131 DisallowHeapAllocation no_gc;
133 DCHECK(IsFastElementsKind(constant_elements_kind)); 132 DCHECK(IsFastElementsKind(constant_elements_kind));
134 Context* native_context = isolate->context()->native_context(); 133 Context* native_context = isolate->context()->native_context();
135 Object* map = 134 Object* map =
136 native_context->get(Context::ArrayMapIndex(constant_elements_kind)); 135 native_context->get(Context::ArrayMapIndex(constant_elements_kind));
137 object->set_map(Map::cast(map)); 136 object->set_map(Map::cast(map));
138 } 137 }
139 138
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 object->set_elements(*copied_elements_values); 178 object->set_elements(*copied_elements_values);
180 object->set_length(Smi::FromInt(copied_elements_values->length())); 179 object->set_length(Smi::FromInt(copied_elements_values->length()));
181 180
182 JSObject::ValidateElements(object); 181 JSObject::ValidateElements(object);
183 return object; 182 return object;
184 } 183 }
185 184
186 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate( 185 MUST_USE_RESULT static MaybeHandle<Object> CreateLiteralBoilerplate(
187 Isolate* isolate, Handle<LiteralsArray> literals, 186 Isolate* isolate, Handle<LiteralsArray> literals,
188 Handle<FixedArray> array) { 187 Handle<FixedArray> array) {
189 Handle<FixedArray> elements = CompileTimeValue::GetElements(array); 188 Handle<HeapObject> elements = CompileTimeValue::GetElements(array);
190 switch (CompileTimeValue::GetLiteralType(array)) { 189 switch (CompileTimeValue::GetLiteralType(array)) {
191 case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: 190 case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: {
192 return CreateObjectLiteralBoilerplate(isolate, literals, elements, true); 191 Handle<FixedArray> props = Handle<FixedArray>::cast(elements);
193 case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: 192 return CreateObjectLiteralBoilerplate(isolate, literals, props, true);
194 return CreateObjectLiteralBoilerplate(isolate, literals, elements, false); 193 }
195 case CompileTimeValue::ARRAY_LITERAL: 194 case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: {
196 return CreateArrayLiteralBoilerplate(isolate, literals, elements); 195 Handle<FixedArray> props = Handle<FixedArray>::cast(elements);
196 return CreateObjectLiteralBoilerplate(isolate, literals, props, false);
197 }
198 case CompileTimeValue::ARRAY_LITERAL: {
199 Handle<ConstantElementsPair> elems =
200 Handle<ConstantElementsPair>::cast(elements);
201 return CreateArrayLiteralBoilerplate(isolate, literals, elems);
202 }
197 default: 203 default:
198 UNREACHABLE(); 204 UNREACHABLE();
199 return MaybeHandle<Object>(); 205 return MaybeHandle<Object>();
200 } 206 }
201 } 207 }
202 208
203 209
204 RUNTIME_FUNCTION(Runtime_CreateRegExpLiteral) { 210 RUNTIME_FUNCTION(Runtime_CreateRegExpLiteral) {
205 HandleScope scope(isolate); 211 HandleScope scope(isolate);
206 DCHECK_EQ(4, args.length()); 212 DCHECK_EQ(4, args.length());
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 AllocationSiteUsageContext usage_context(isolate, site, enable_mementos); 269 AllocationSiteUsageContext usage_context(isolate, site, enable_mementos);
264 usage_context.EnterNewScope(); 270 usage_context.EnterNewScope();
265 MaybeHandle<Object> maybe_copy = 271 MaybeHandle<Object> maybe_copy =
266 JSObject::DeepCopy(boilerplate, &usage_context); 272 JSObject::DeepCopy(boilerplate, &usage_context);
267 usage_context.ExitScope(site, boilerplate); 273 usage_context.ExitScope(site, boilerplate);
268 RETURN_RESULT_OR_FAILURE(isolate, maybe_copy); 274 RETURN_RESULT_OR_FAILURE(isolate, maybe_copy);
269 } 275 }
270 276
271 MUST_USE_RESULT static MaybeHandle<AllocationSite> GetLiteralAllocationSite( 277 MUST_USE_RESULT static MaybeHandle<AllocationSite> GetLiteralAllocationSite(
272 Isolate* isolate, Handle<LiteralsArray> literals, int literals_index, 278 Isolate* isolate, Handle<LiteralsArray> literals, int literals_index,
273 Handle<FixedArray> elements) { 279 Handle<ConstantElementsPair> elements) {
274 // Check if boilerplate exists. If not, create it first. 280 // Check if boilerplate exists. If not, create it first.
275 Handle<Object> literal_site(literals->literal(literals_index), isolate); 281 Handle<Object> literal_site(literals->literal(literals_index), isolate);
276 Handle<AllocationSite> site; 282 Handle<AllocationSite> site;
277 if (literal_site->IsUndefined(isolate)) { 283 if (literal_site->IsUndefined(isolate)) {
278 DCHECK(*elements != isolate->heap()->empty_fixed_array());
279 Handle<Object> boilerplate; 284 Handle<Object> boilerplate;
280 ASSIGN_RETURN_ON_EXCEPTION( 285 ASSIGN_RETURN_ON_EXCEPTION(
281 isolate, boilerplate, 286 isolate, boilerplate,
282 CreateArrayLiteralBoilerplate(isolate, literals, elements), 287 CreateArrayLiteralBoilerplate(isolate, literals, elements),
283 AllocationSite); 288 AllocationSite);
284 289
285 AllocationSiteCreationContext creation_context(isolate); 290 AllocationSiteCreationContext creation_context(isolate);
286 site = creation_context.EnterNewScope(); 291 site = creation_context.EnterNewScope();
287 if (JSObject::DeepWalk(Handle<JSObject>::cast(boilerplate), 292 if (JSObject::DeepWalk(Handle<JSObject>::cast(boilerplate),
288 &creation_context).is_null()) { 293 &creation_context).is_null()) {
289 return Handle<AllocationSite>::null(); 294 return Handle<AllocationSite>::null();
290 } 295 }
291 creation_context.ExitScope(site, Handle<JSObject>::cast(boilerplate)); 296 creation_context.ExitScope(site, Handle<JSObject>::cast(boilerplate));
292 297
293 literals->set_literal(literals_index, *site); 298 literals->set_literal(literals_index, *site);
294 } else { 299 } else {
295 site = Handle<AllocationSite>::cast(literal_site); 300 site = Handle<AllocationSite>::cast(literal_site);
296 } 301 }
297 302
298 return site; 303 return site;
299 } 304 }
300 305
301
302 static MaybeHandle<JSObject> CreateArrayLiteralImpl( 306 static MaybeHandle<JSObject> CreateArrayLiteralImpl(
303 Isolate* isolate, Handle<LiteralsArray> literals, int literals_index, 307 Isolate* isolate, Handle<LiteralsArray> literals, int literals_index,
304 Handle<FixedArray> elements, int flags) { 308 Handle<ConstantElementsPair> elements, int flags) {
305 CHECK(literals_index >= 0 && literals_index < literals->literals_count()); 309 CHECK(literals_index >= 0 && literals_index < literals->literals_count());
306 Handle<AllocationSite> site; 310 Handle<AllocationSite> site;
307 ASSIGN_RETURN_ON_EXCEPTION( 311 ASSIGN_RETURN_ON_EXCEPTION(
308 isolate, site, 312 isolate, site,
309 GetLiteralAllocationSite(isolate, literals, literals_index, elements), 313 GetLiteralAllocationSite(isolate, literals, literals_index, elements),
310 JSObject); 314 JSObject);
311 315
312 bool enable_mementos = (flags & ArrayLiteral::kDisableMementos) == 0; 316 bool enable_mementos = (flags & ArrayLiteral::kDisableMementos) == 0;
313 Handle<JSObject> boilerplate(JSObject::cast(site->transition_info())); 317 Handle<JSObject> boilerplate(JSObject::cast(site->transition_info()));
314 AllocationSiteUsageContext usage_context(isolate, site, enable_mementos); 318 AllocationSiteUsageContext usage_context(isolate, site, enable_mementos);
315 usage_context.EnterNewScope(); 319 usage_context.EnterNewScope();
316 JSObject::DeepCopyHints hints = (flags & ArrayLiteral::kShallowElements) == 0 320 JSObject::DeepCopyHints hints = (flags & ArrayLiteral::kShallowElements) == 0
317 ? JSObject::kNoHints 321 ? JSObject::kNoHints
318 : JSObject::kObjectIsShallow; 322 : JSObject::kObjectIsShallow;
319 MaybeHandle<JSObject> copy = 323 MaybeHandle<JSObject> copy =
320 JSObject::DeepCopy(boilerplate, &usage_context, hints); 324 JSObject::DeepCopy(boilerplate, &usage_context, hints);
321 usage_context.ExitScope(site, boilerplate); 325 usage_context.ExitScope(site, boilerplate);
322 return copy; 326 return copy;
323 } 327 }
324 328
325 329
326 RUNTIME_FUNCTION(Runtime_CreateArrayLiteral) { 330 RUNTIME_FUNCTION(Runtime_CreateArrayLiteral) {
327 HandleScope scope(isolate); 331 HandleScope scope(isolate);
328 DCHECK_EQ(4, args.length()); 332 DCHECK_EQ(4, args.length());
329 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0); 333 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0);
330 CONVERT_SMI_ARG_CHECKED(literals_index, 1); 334 CONVERT_SMI_ARG_CHECKED(literals_index, 1);
331 CONVERT_ARG_HANDLE_CHECKED(FixedArray, elements, 2); 335 CONVERT_ARG_HANDLE_CHECKED(ConstantElementsPair, elements, 2);
332 CONVERT_SMI_ARG_CHECKED(flags, 3); 336 CONVERT_SMI_ARG_CHECKED(flags, 3);
333 337
334 Handle<LiteralsArray> literals(closure->literals(), isolate); 338 Handle<LiteralsArray> literals(closure->literals(), isolate);
335 RETURN_RESULT_OR_FAILURE( 339 RETURN_RESULT_OR_FAILURE(
336 isolate, CreateArrayLiteralImpl(isolate, literals, literals_index, 340 isolate, CreateArrayLiteralImpl(isolate, literals, literals_index,
337 elements, flags)); 341 elements, flags));
338 } 342 }
339 343
340 344
341 RUNTIME_FUNCTION(Runtime_CreateArrayLiteralStubBailout) { 345 RUNTIME_FUNCTION(Runtime_CreateArrayLiteralStubBailout) {
342 HandleScope scope(isolate); 346 HandleScope scope(isolate);
343 DCHECK_EQ(3, args.length()); 347 DCHECK_EQ(3, args.length());
344 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0); 348 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 0);
345 CONVERT_SMI_ARG_CHECKED(literals_index, 1); 349 CONVERT_SMI_ARG_CHECKED(literals_index, 1);
346 CONVERT_ARG_HANDLE_CHECKED(FixedArray, elements, 2); 350 CONVERT_ARG_HANDLE_CHECKED(ConstantElementsPair, elements, 2);
347 351
348 Handle<LiteralsArray> literals(closure->literals(), isolate); 352 Handle<LiteralsArray> literals(closure->literals(), isolate);
349 RETURN_RESULT_OR_FAILURE( 353 RETURN_RESULT_OR_FAILURE(
350 isolate, 354 isolate,
351 CreateArrayLiteralImpl(isolate, literals, literals_index, elements, 355 CreateArrayLiteralImpl(isolate, literals, literals_index, elements,
352 ArrayLiteral::kShallowElements)); 356 ArrayLiteral::kShallowElements));
353 } 357 }
354 358
355 } // namespace internal 359 } // namespace internal
356 } // namespace v8 360 } // namespace v8
OLDNEW
« no previous file with comments | « src/objects-printer.cc ('k') | test/cctest/interpreter/bytecode_expectations/ArrayLiterals.golden » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698