1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "src/accessors.h"
11 #include "src/allocation-site-scopes.h"
13 #include "src/arguments.h"
14 #include "src/base/bits.h"
15 #include "src/base/utils/random-number-generator.h"
16 #include "src/bootstrapper.h"
17 #include "src/code-stubs.h"
18 #include "src/codegen.h"
19 #include "src/compilation-dependencies.h"
20 #include "src/compiler.h"
21 #include "src/cpu-profiler.h"
23 #include "src/debug/debug.h"
24 #include "src/deoptimizer.h"
25 #include "src/elements.h"
26 #include "src/execution.h"
27 #include "src/field-index-inl.h"
28 #include "src/field-index.h"
29 #include "src/full-codegen/full-codegen.h"
30 #include "src/hydrogen.h"
31 #include "src/ic/ic.h"
32 #include "src/interpreter/bytecodes.h"
34 #include "src/lookup.h"
35 #include "src/macro-assembler.h"
36 #include "src/messages.h"
37 #include "src/objects-inl.h"
38 #include "src/prototype.h"
39 #include "src/safepoint-table.h"
40 #include "src/string-search.h"
41 #include "src/string-stream.h"
42 #include "src/utils.h"
44 #ifdef ENABLE_DISASSEMBLER
45 #include "src/disasm.h"
46 #include "src/disassembler.h"
52 Handle<HeapType> Object::OptimalType(Isolate* isolate,
53 Representation representation) {
54 if (representation.IsNone()) return HeapType::None(isolate);
55 if (FLAG_track_field_types) {
56 if (representation.IsHeapObject() && IsHeapObject()) {
57 // We can track only JavaScript objects with stable maps.
58 Handle<Map> map(HeapObject::cast(this)->map(), isolate);
59 if (map->is_stable() &&
60 map->instance_type() >= FIRST_NONCALLABLE_SPEC_OBJECT_TYPE &&
61 map->instance_type() <= LAST_NONCALLABLE_SPEC_OBJECT_TYPE) {
62 return HeapType::Class(map, isolate);
66 return HeapType::Any(isolate);
70 MaybeHandle<JSReceiver> Object::ToObject(Isolate* isolate,
71 Handle<Object> object,
72 Handle<Context> native_context) {
73 if (object->IsJSReceiver()) return Handle<JSReceiver>::cast(object);
74 Handle<JSFunction> constructor;
75 if (object->IsNumber()) {
76 constructor = handle(native_context->number_function(), isolate);
77 } else if (object->IsBoolean()) {
78 constructor = handle(native_context->boolean_function(), isolate);
79 } else if (object->IsString()) {
80 constructor = handle(native_context->string_function(), isolate);
81 } else if (object->IsSymbol()) {
82 constructor = handle(native_context->symbol_function(), isolate);
83 } else if (object->IsSimd128Value()) {
84 if (object->IsFloat32x4()) {
85 constructor = handle(native_context->float32x4_function(), isolate);
86 } else if (object->IsInt32x4()) {
87 constructor = handle(native_context->int32x4_function(), isolate);
88 } else if (object->IsBool32x4()) {
89 constructor = handle(native_context->bool32x4_function(), isolate);
90 } else if (object->IsInt16x8()) {
91 constructor = handle(native_context->int16x8_function(), isolate);
92 } else if (object->IsBool16x8()) {
93 constructor = handle(native_context->bool16x8_function(), isolate);
94 } else if (object->IsInt8x16()) {
95 constructor = handle(native_context->int8x16_function(), isolate);
96 } else if (object->IsBool8x16()) {
97 constructor = handle(native_context->bool8x16_function(), isolate);
102 return MaybeHandle<JSReceiver>();
104 Handle<JSObject> result = isolate->factory()->NewJSObject(constructor);
105 Handle<JSValue>::cast(result)->set_value(*object);
110 bool Object::BooleanValue() {
111 if (IsBoolean()) return IsTrue();
112 if (IsSmi()) return Smi::cast(this)->value() != 0;
113 if (IsUndefined() || IsNull()) return false;
114 if (IsUndetectableObject()) return false; // Undetectable object is false.
115 if (IsString()) return String::cast(this)->length() != 0;
116 if (IsHeapNumber()) return HeapNumber::cast(this)->HeapNumberBooleanValue();
117 if (IsSimd128Value()) return true; // Simd value types evaluate to true.
122 bool Object::IsCallable() const {
123 const Object* fun = this;
124 while (fun->IsJSFunctionProxy()) {
125 fun = JSFunctionProxy::cast(fun)->call_trap();
127 return fun->IsJSFunction() ||
128 (fun->IsHeapObject() &&
129 HeapObject::cast(fun)->map()->has_instance_call_handler());
133 bool Object::IsPromise(Handle<Object> object) {
134 if (!object->IsJSObject()) return false;
135 auto js_object = Handle<JSObject>::cast(object);
136 // Promises can't have access checks.
137 if (js_object->map()->is_access_check_needed()) return false;
138 auto isolate = js_object->GetIsolate();
139 // TODO(dcarney): this should just be read from the symbol registry so as not
140 // to be context dependent.
141 auto key = isolate->promise_status();
142 // Shouldn't be possible to throw here.
143 return JSObject::HasRealNamedProperty(js_object, key).FromJust();
147 MaybeHandle<Object> Object::GetProperty(LookupIterator* it,
148 LanguageMode language_mode) {
149 for (; it->IsFound(); it->Next()) {
150 switch (it->state()) {
151 case LookupIterator::NOT_FOUND:
152 case LookupIterator::TRANSITION:
154 case LookupIterator::JSPROXY:
155 return JSProxy::GetPropertyWithHandler(
156 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName());
157 case LookupIterator::INTERCEPTOR: {
159 Handle<Object> result;
160 ASSIGN_RETURN_ON_EXCEPTION(
161 it->isolate(), result,
162 JSObject::GetPropertyWithInterceptor(it, &done), Object);
163 if (done) return result;
166 case LookupIterator::ACCESS_CHECK:
167 if (it->HasAccess()) break;
168 return JSObject::GetPropertyWithFailedAccessCheck(it);
169 case LookupIterator::ACCESSOR:
170 return GetPropertyWithAccessor(it, language_mode);
171 case LookupIterator::INTEGER_INDEXED_EXOTIC:
172 return ReadAbsentProperty(it, language_mode);
173 case LookupIterator::DATA:
174 return it->GetDataValue();
177 return ReadAbsentProperty(it, language_mode);
181 Handle<Object> JSReceiver::GetDataProperty(Handle<JSReceiver> object,
183 LookupIterator it(object, name,
184 LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
185 return GetDataProperty(&it);
189 Handle<Object> JSReceiver::GetDataProperty(LookupIterator* it) {
190 for (; it->IsFound(); it->Next()) {
191 switch (it->state()) {
192 case LookupIterator::INTERCEPTOR:
193 case LookupIterator::NOT_FOUND:
194 case LookupIterator::TRANSITION:
196 case LookupIterator::ACCESS_CHECK:
197 if (it->HasAccess()) continue;
199 case LookupIterator::JSPROXY:
201 return it->isolate()->factory()->undefined_value();
202 case LookupIterator::ACCESSOR:
203 // TODO(verwaest): For now this doesn't call into
204 // ExecutableAccessorInfo, since clients don't need it. Update once
207 return it->isolate()->factory()->undefined_value();
208 case LookupIterator::INTEGER_INDEXED_EXOTIC:
209 return it->isolate()->factory()->undefined_value();
210 case LookupIterator::DATA:
211 return it->GetDataValue();
214 return it->isolate()->factory()->undefined_value();
218 bool Object::ToInt32(int32_t* value) {
220 *value = Smi::cast(this)->value();
223 if (IsHeapNumber()) {
224 double num = HeapNumber::cast(this)->value();
225 if (FastI2D(FastD2I(num)) == num) {
226 *value = FastD2I(num);
234 bool Object::ToUint32(uint32_t* value) {
236 int num = Smi::cast(this)->value();
238 *value = static_cast<uint32_t>(num);
242 if (IsHeapNumber()) {
243 double num = HeapNumber::cast(this)->value();
244 if (num >= 0 && FastUI2D(FastD2UI(num)) == num) {
245 *value = FastD2UI(num);
253 bool FunctionTemplateInfo::IsTemplateFor(Object* object) {
254 if (!object->IsHeapObject()) return false;
255 return IsTemplateFor(HeapObject::cast(object)->map());
259 bool FunctionTemplateInfo::IsTemplateFor(Map* map) {
260 // There is a constraint on the object; check.
261 if (!map->IsJSObjectMap()) return false;
262 // Fetch the constructor function of the object.
263 Object* cons_obj = map->GetConstructor();
264 if (!cons_obj->IsJSFunction()) return false;
265 JSFunction* fun = JSFunction::cast(cons_obj);
266 // Iterate through the chain of inheriting function templates to
267 // see if the required one occurs.
268 for (Object* type = fun->shared()->function_data();
269 type->IsFunctionTemplateInfo();
270 type = FunctionTemplateInfo::cast(type)->parent_template()) {
271 if (type == this) return true;
273 // Didn't find the required type in the inheritance chain.
278 // TODO(dcarney): CallOptimization duplicates this logic, merge.
279 Object* FunctionTemplateInfo::GetCompatibleReceiver(Isolate* isolate,
281 // API calls are only supported with JSObject receivers.
282 if (!receiver->IsJSObject()) return isolate->heap()->null_value();
283 Object* recv_type = this->signature();
284 // No signature, return holder.
285 if (recv_type->IsUndefined()) return receiver;
286 FunctionTemplateInfo* signature = FunctionTemplateInfo::cast(recv_type);
287 // Check the receiver.
288 for (PrototypeIterator iter(isolate, receiver,
289 PrototypeIterator::START_AT_RECEIVER);
290 !iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN); iter.Advance()) {
291 if (signature->IsTemplateFor(iter.GetCurrent())) return iter.GetCurrent();
293 return isolate->heap()->null_value();
297 Handle<FixedArray> JSObject::EnsureWritableFastElements(
298 Handle<JSObject> object) {
299 DCHECK(object->HasFastSmiOrObjectElements());
300 Isolate* isolate = object->GetIsolate();
301 Handle<FixedArray> elems(FixedArray::cast(object->elements()), isolate);
302 if (elems->map() != isolate->heap()->fixed_cow_array_map()) return elems;
303 Handle<FixedArray> writable_elems = isolate->factory()->CopyFixedArrayWithMap(
304 elems, isolate->factory()->fixed_array_map());
305 object->set_elements(*writable_elems);
306 isolate->counters()->cow_arrays_converted()->Increment();
307 return writable_elems;
311 MaybeHandle<Object> JSProxy::GetPropertyWithHandler(Handle<JSProxy> proxy,
312 Handle<Object> receiver,
314 Isolate* isolate = proxy->GetIsolate();
316 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
317 if (name->IsSymbol()) return isolate->factory()->undefined_value();
319 Handle<Object> args[] = { receiver, name };
321 proxy, "get", isolate->derived_get_trap(), arraysize(args), args);
325 MaybeHandle<Object> Object::GetPropertyWithAccessor(
326 LookupIterator* it, LanguageMode language_mode) {
327 Isolate* isolate = it->isolate();
328 Handle<Object> structure = it->GetAccessors();
329 Handle<Object> receiver = it->GetReceiver();
331 // We should never get here to initialize a const with the hole value since a
332 // const declaration would conflict with the getter.
333 DCHECK(!structure->IsForeign());
335 // API style callbacks.
336 if (structure->IsAccessorInfo()) {
337 Handle<JSObject> holder = it->GetHolder<JSObject>();
338 Handle<Name> name = it->GetName();
339 Handle<ExecutableAccessorInfo> info =
340 Handle<ExecutableAccessorInfo>::cast(structure);
341 if (!info->IsCompatibleReceiver(*receiver)) {
342 THROW_NEW_ERROR(isolate,
343 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
348 v8::AccessorNameGetterCallback call_fun =
349 v8::ToCData<v8::AccessorNameGetterCallback>(info->getter());
350 if (call_fun == nullptr) return isolate->factory()->undefined_value();
352 LOG(isolate, ApiNamedPropertyAccess("load", *holder, *name));
353 PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder);
354 v8::Local<v8::Value> result = args.Call(call_fun, v8::Utils::ToLocal(name));
355 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
356 if (result.IsEmpty()) {
357 return ReadAbsentProperty(isolate, receiver, name, language_mode);
359 Handle<Object> return_value = v8::Utils::OpenHandle(*result);
360 return_value->VerifyApiCallResultType();
361 // Rebox handle before return.
362 return handle(*return_value, isolate);
366 Handle<Object> getter(AccessorPair::cast(*structure)->getter(), isolate);
367 if (getter->IsSpecFunction()) {
368 // TODO(rossberg): nicer would be to cast to some JSCallable here...
369 return Object::GetPropertyWithDefinedGetter(
370 receiver, Handle<JSReceiver>::cast(getter));
372 // Getter is not a function.
373 return ReadAbsentProperty(isolate, receiver, it->GetName(), language_mode);
377 bool AccessorInfo::IsCompatibleReceiverMap(Isolate* isolate,
378 Handle<AccessorInfo> info,
380 if (!info->HasExpectedReceiverType()) return true;
381 if (!map->IsJSObjectMap()) return false;
382 return FunctionTemplateInfo::cast(info->expected_receiver_type())
383 ->IsTemplateFor(*map);
387 MaybeHandle<Object> Object::SetPropertyWithAccessor(
388 LookupIterator* it, Handle<Object> value, LanguageMode language_mode) {
389 Isolate* isolate = it->isolate();
390 Handle<Object> structure = it->GetAccessors();
391 Handle<Object> receiver = it->GetReceiver();
393 // We should never get here to initialize a const with the hole value since a
394 // const declaration would conflict with the setter.
395 DCHECK(!structure->IsForeign());
397 // API style callbacks.
398 if (structure->IsExecutableAccessorInfo()) {
399 Handle<JSObject> holder = it->GetHolder<JSObject>();
400 Handle<Name> name = it->GetName();
401 Handle<ExecutableAccessorInfo> info =
402 Handle<ExecutableAccessorInfo>::cast(structure);
403 if (!info->IsCompatibleReceiver(*receiver)) {
404 THROW_NEW_ERROR(isolate,
405 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
410 v8::AccessorNameSetterCallback call_fun =
411 v8::ToCData<v8::AccessorNameSetterCallback>(info->setter());
412 if (call_fun == nullptr) return value;
414 LOG(isolate, ApiNamedPropertyAccess("store", *holder, *name));
415 PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder);
416 args.Call(call_fun, v8::Utils::ToLocal(name), v8::Utils::ToLocal(value));
417 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
422 Handle<Object> setter(AccessorPair::cast(*structure)->setter(), isolate);
423 if (setter->IsSpecFunction()) {
424 // TODO(rossberg): nicer would be to cast to some JSCallable here...
425 return SetPropertyWithDefinedSetter(
426 receiver, Handle<JSReceiver>::cast(setter), value);
429 if (is_sloppy(language_mode)) return value;
431 THROW_NEW_ERROR(isolate,
432 NewTypeError(MessageTemplate::kNoSetterInCallback,
433 it->GetName(), it->GetHolder<JSObject>()),
438 MaybeHandle<Object> Object::GetPropertyWithDefinedGetter(
439 Handle<Object> receiver,
440 Handle<JSReceiver> getter) {
441 Isolate* isolate = getter->GetIsolate();
443 // Platforms with simulators like arm/arm64 expose a funny issue. If the
444 // simulator has a separate JS stack pointer from the C++ stack pointer, it
445 // can miss C++ stack overflows in the stack guard at the start of JavaScript
446 // functions. It would be very expensive to check the C++ stack pointer at
447 // that location. The best solution seems to be to break the impasse by
448 // adding checks at possible recursion points. What's more, we don't put
449 // this stack check behind the USE_SIMULATOR define in order to keep
450 // behavior the same between hardware and simulators.
451 StackLimitCheck check(isolate);
452 if (check.JsHasOverflowed()) {
453 isolate->StackOverflow();
454 return MaybeHandle<Object>();
457 Debug* debug = isolate->debug();
458 // Handle stepping into a getter if step into is active.
459 // TODO(rossberg): should this apply to getters that are function proxies?
460 if (debug->is_active()) debug->HandleStepIn(getter, false);
462 return Execution::Call(isolate, getter, receiver, 0, NULL, true);
466 MaybeHandle<Object> Object::SetPropertyWithDefinedSetter(
467 Handle<Object> receiver,
468 Handle<JSReceiver> setter,
469 Handle<Object> value) {
470 Isolate* isolate = setter->GetIsolate();
472 Debug* debug = isolate->debug();
473 // Handle stepping into a setter if step into is active.
474 // TODO(rossberg): should this apply to getters that are function proxies?
475 if (debug->is_active()) debug->HandleStepIn(setter, false);
477 Handle<Object> argv[] = { value };
478 RETURN_ON_EXCEPTION(isolate, Execution::Call(isolate, setter, receiver,
479 arraysize(argv), argv, true),
486 bool JSObject::AllCanRead(LookupIterator* it) {
487 // Skip current iteration, it's in state ACCESS_CHECK or INTERCEPTOR, both of
488 // which have already been checked.
489 DCHECK(it->state() == LookupIterator::ACCESS_CHECK ||
490 it->state() == LookupIterator::INTERCEPTOR);
491 for (it->Next(); it->IsFound(); it->Next()) {
492 if (it->state() == LookupIterator::ACCESSOR) {
493 auto accessors = it->GetAccessors();
494 if (accessors->IsAccessorInfo()) {
495 if (AccessorInfo::cast(*accessors)->all_can_read()) return true;
497 } else if (it->state() == LookupIterator::INTERCEPTOR) {
498 if (it->GetInterceptor()->all_can_read()) return true;
505 MaybeHandle<Object> JSObject::GetPropertyWithFailedAccessCheck(
506 LookupIterator* it) {
507 Handle<JSObject> checked = it->GetHolder<JSObject>();
508 while (AllCanRead(it)) {
509 if (it->state() == LookupIterator::ACCESSOR) {
510 return GetPropertyWithAccessor(it, SLOPPY);
512 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
514 Handle<Object> result;
515 ASSIGN_RETURN_ON_EXCEPTION(it->isolate(), result,
516 GetPropertyWithInterceptor(it, &done), Object);
517 if (done) return result;
519 it->isolate()->ReportFailedAccessCheck(checked);
520 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
521 return it->factory()->undefined_value();
525 Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithFailedAccessCheck(
526 LookupIterator* it) {
527 Handle<JSObject> checked = it->GetHolder<JSObject>();
528 while (AllCanRead(it)) {
529 if (it->state() == LookupIterator::ACCESSOR) {
530 return Just(it->property_details().attributes());
532 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
533 auto result = GetPropertyAttributesWithInterceptor(it);
534 if (it->isolate()->has_scheduled_exception()) break;
535 if (result.IsJust() && result.FromJust() != ABSENT) return result;
537 it->isolate()->ReportFailedAccessCheck(checked);
538 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(),
539 Nothing<PropertyAttributes>());
545 bool JSObject::AllCanWrite(LookupIterator* it) {
546 for (; it->IsFound(); it->Next()) {
547 if (it->state() == LookupIterator::ACCESSOR) {
548 Handle<Object> accessors = it->GetAccessors();
549 if (accessors->IsAccessorInfo()) {
550 if (AccessorInfo::cast(*accessors)->all_can_write()) return true;
558 MaybeHandle<Object> JSObject::SetPropertyWithFailedAccessCheck(
559 LookupIterator* it, Handle<Object> value) {
560 Handle<JSObject> checked = it->GetHolder<JSObject>();
561 if (AllCanWrite(it)) {
562 // The supplied language-mode is ignored by SetPropertyWithAccessor.
563 return SetPropertyWithAccessor(it, value, SLOPPY);
566 it->isolate()->ReportFailedAccessCheck(checked);
567 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
572 void JSObject::SetNormalizedProperty(Handle<JSObject> object,
574 Handle<Object> value,
575 PropertyDetails details) {
576 DCHECK(!object->HasFastProperties());
577 if (!name->IsUniqueName()) {
578 name = object->GetIsolate()->factory()->InternalizeString(
579 Handle<String>::cast(name));
582 if (object->IsGlobalObject()) {
583 Handle<GlobalDictionary> property_dictionary(object->global_dictionary());
585 int entry = property_dictionary->FindEntry(name);
586 if (entry == GlobalDictionary::kNotFound) {
587 auto cell = object->GetIsolate()->factory()->NewPropertyCell();
588 cell->set_value(*value);
589 auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined
590 : PropertyCellType::kConstant;
591 details = details.set_cell_type(cell_type);
593 property_dictionary =
594 GlobalDictionary::Add(property_dictionary, name, value, details);
595 object->set_properties(*property_dictionary);
597 PropertyCell::UpdateCell(property_dictionary, entry, value, details);
600 Handle<NameDictionary> property_dictionary(object->property_dictionary());
602 int entry = property_dictionary->FindEntry(name);
603 if (entry == NameDictionary::kNotFound) {
604 property_dictionary =
605 NameDictionary::Add(property_dictionary, name, value, details);
606 object->set_properties(*property_dictionary);
608 PropertyDetails original_details = property_dictionary->DetailsAt(entry);
609 int enumeration_index = original_details.dictionary_index();
610 DCHECK(enumeration_index > 0);
611 details = details.set_index(enumeration_index);
612 property_dictionary->SetEntry(entry, name, value, details);
618 Map* Object::GetRootMap(Isolate* isolate) {
619 DisallowHeapAllocation no_alloc;
621 Context* context = isolate->context()->native_context();
622 return context->number_function()->initial_map();
625 HeapObject* heap_object = HeapObject::cast(this);
627 // The object is either a number, a string, a symbol, a boolean, a SIMD value,
628 // a real JS object, or a Harmony proxy.
629 if (heap_object->IsJSReceiver()) {
630 return heap_object->map();
632 Context* context = isolate->context()->native_context();
634 if (heap_object->IsHeapNumber()) {
635 return context->number_function()->initial_map();
637 if (heap_object->IsString()) {
638 return context->string_function()->initial_map();
640 if (heap_object->IsSymbol()) {
641 return context->symbol_function()->initial_map();
643 if (heap_object->IsBoolean()) {
644 return context->boolean_function()->initial_map();
646 if (heap_object->IsSimd128Value()) {
647 if (heap_object->IsFloat32x4()) {
648 return context->float32x4_function()->initial_map();
649 } else if (heap_object->IsInt32x4()) {
650 return context->int32x4_function()->initial_map();
651 } else if (heap_object->IsBool32x4()) {
652 return context->bool32x4_function()->initial_map();
653 } else if (heap_object->IsInt16x8()) {
654 return context->int16x8_function()->initial_map();
655 } else if (heap_object->IsBool16x8()) {
656 return context->bool16x8_function()->initial_map();
657 } else if (heap_object->IsInt8x16()) {
658 return context->int8x16_function()->initial_map();
659 } else if (heap_object->IsBool8x16()) {
660 return context->bool8x16_function()->initial_map();
665 return isolate->heap()->null_value()->map();
669 Object* Object::GetHash() {
670 Object* hash = GetSimpleHash();
671 if (hash->IsSmi()) return hash;
673 DCHECK(IsJSReceiver());
674 return JSReceiver::cast(this)->GetIdentityHash();
678 Object* Object::GetSimpleHash() {
679 // The object is either a Smi, a HeapNumber, a name, an odd-ball,
680 // a SIMD value type, a real JS object, or a Harmony proxy.
682 uint32_t hash = ComputeIntegerHash(Smi::cast(this)->value(), kZeroHashSeed);
683 return Smi::FromInt(hash & Smi::kMaxValue);
685 if (IsHeapNumber()) {
686 double num = HeapNumber::cast(this)->value();
687 if (std::isnan(num)) return Smi::FromInt(Smi::kMaxValue);
688 if (i::IsMinusZero(num)) num = 0;
689 if (IsSmiDouble(num)) {
690 return Smi::FromInt(FastD2I(num))->GetHash();
692 uint32_t hash = ComputeLongHash(double_to_uint64(num));
693 return Smi::FromInt(hash & Smi::kMaxValue);
696 uint32_t hash = Name::cast(this)->Hash();
697 return Smi::FromInt(hash);
700 uint32_t hash = Oddball::cast(this)->to_string()->Hash();
701 return Smi::FromInt(hash);
703 if (IsSimd128Value()) {
704 uint32_t hash = Simd128Value::cast(this)->Hash();
705 return Smi::FromInt(hash & Smi::kMaxValue);
707 DCHECK(IsJSReceiver());
708 JSReceiver* receiver = JSReceiver::cast(this);
709 return receiver->GetHeap()->undefined_value();
713 Handle<Smi> Object::GetOrCreateHash(Isolate* isolate, Handle<Object> object) {
714 Handle<Object> hash(object->GetSimpleHash(), isolate);
715 if (hash->IsSmi()) return Handle<Smi>::cast(hash);
717 DCHECK(object->IsJSReceiver());
718 return JSReceiver::GetOrCreateIdentityHash(Handle<JSReceiver>::cast(object));
722 bool Object::SameValue(Object* other) {
723 if (other == this) return true;
725 // The object is either a number, a name, an odd-ball,
726 // a real JS object, or a Harmony proxy.
727 if (IsNumber() && other->IsNumber()) {
728 double this_value = Number();
729 double other_value = other->Number();
730 // SameValue(NaN, NaN) is true.
731 if (this_value != other_value) {
732 return std::isnan(this_value) && std::isnan(other_value);
734 // SameValue(0.0, -0.0) is false.
735 return (std::signbit(this_value) == std::signbit(other_value));
737 if (IsString() && other->IsString()) {
738 return String::cast(this)->Equals(String::cast(other));
740 if (IsSimd128Value() && other->IsSimd128Value()) {
741 if (IsFloat32x4() && other->IsFloat32x4()) {
742 Float32x4* a = Float32x4::cast(this);
743 Float32x4* b = Float32x4::cast(other);
744 for (int i = 0; i < 4; i++) {
745 float x = a->get_lane(i);
746 float y = b->get_lane(i);
747 // Implements the ES5 SameValue operation for floating point types.
748 // http://www.ecma-international.org/ecma-262/6.0/#sec-samevalue
749 if (x != y && !(std::isnan(x) && std::isnan(y))) return false;
750 if (std::signbit(x) != std::signbit(y)) return false;
754 Simd128Value* a = Simd128Value::cast(this);
755 Simd128Value* b = Simd128Value::cast(other);
756 return a->map()->instance_type() == b->map()->instance_type() &&
764 bool Object::SameValueZero(Object* other) {
765 if (other == this) return true;
767 // The object is either a number, a name, an odd-ball,
768 // a real JS object, or a Harmony proxy.
769 if (IsNumber() && other->IsNumber()) {
770 double this_value = Number();
771 double other_value = other->Number();
773 return this_value == other_value ||
774 (std::isnan(this_value) && std::isnan(other_value));
776 if (IsString() && other->IsString()) {
777 return String::cast(this)->Equals(String::cast(other));
779 if (IsSimd128Value() && other->IsSimd128Value()) {
780 if (IsFloat32x4() && other->IsFloat32x4()) {
781 Float32x4* a = Float32x4::cast(this);
782 Float32x4* b = Float32x4::cast(other);
783 for (int i = 0; i < 4; i++) {
784 float x = a->get_lane(i);
785 float y = b->get_lane(i);
786 // Implements the ES6 SameValueZero operation for floating point types.
787 // http://www.ecma-international.org/ecma-262/6.0/#sec-samevaluezero
788 if (x != y && !(std::isnan(x) && std::isnan(y))) return false;
789 // SameValueZero doesn't distinguish between 0 and -0.
793 Simd128Value* a = Simd128Value::cast(this);
794 Simd128Value* b = Simd128Value::cast(other);
795 return a->map()->instance_type() == b->map()->instance_type() &&
803 void Object::ShortPrint(FILE* out) {
809 void Object::ShortPrint(StringStream* accumulator) {
810 std::ostringstream os;
812 accumulator->Add(os.str().c_str());
816 void Object::ShortPrint(std::ostream& os) { os << Brief(this); }
819 std::ostream& operator<<(std::ostream& os, const Brief& v) {
820 if (v.value->IsSmi()) {
821 Smi::cast(v.value)->SmiPrint(os);
823 // TODO(svenpanne) Const-correct HeapObjectShortPrint!
824 HeapObject* obj = const_cast<HeapObject*>(HeapObject::cast(v.value));
825 obj->HeapObjectShortPrint(os);
831 void Smi::SmiPrint(std::ostream& os) const { // NOLINT
836 // Should a word be prefixed by 'a' or 'an' in order to read naturally in
837 // English? Returns false for non-ASCII or words that don't start with
838 // a capital letter. The a/an rule follows pronunciation in English.
839 // We don't use the BBC's overcorrect "an historic occasion" though if
840 // you speak a dialect you may well say "an 'istoric occasion".
841 static bool AnWord(String* str) {
842 if (str->length() == 0) return false; // A nothing.
843 int c0 = str->Get(0);
844 int c1 = str->length() > 1 ? str->Get(1) : 0;
847 return true; // An Umpire, but a UTF8String, a U.
849 } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') {
850 return true; // An Ape, an ABCBook.
851 } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) &&
852 (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' ||
853 c0 == 'S' || c0 == 'X')) {
854 return true; // An MP3File, an M.
860 Handle<String> String::SlowFlatten(Handle<ConsString> cons,
861 PretenureFlag pretenure) {
862 DCHECK(AllowHeapAllocation::IsAllowed());
863 DCHECK(cons->second()->length() != 0);
864 Isolate* isolate = cons->GetIsolate();
865 int length = cons->length();
866 PretenureFlag tenure = isolate->heap()->InNewSpace(*cons) ? pretenure
868 Handle<SeqString> result;
869 if (cons->IsOneByteRepresentation()) {
870 Handle<SeqOneByteString> flat = isolate->factory()->NewRawOneByteString(
871 length, tenure).ToHandleChecked();
872 DisallowHeapAllocation no_gc;
873 WriteToFlat(*cons, flat->GetChars(), 0, length);
876 Handle<SeqTwoByteString> flat = isolate->factory()->NewRawTwoByteString(
877 length, tenure).ToHandleChecked();
878 DisallowHeapAllocation no_gc;
879 WriteToFlat(*cons, flat->GetChars(), 0, length);
882 cons->set_first(*result);
883 cons->set_second(isolate->heap()->empty_string());
884 DCHECK(result->IsFlat());
890 bool String::MakeExternal(v8::String::ExternalStringResource* resource) {
891 // Externalizing twice leaks the external resource, so it's
892 // prohibited by the API.
893 DCHECK(!this->IsExternalString());
894 #ifdef ENABLE_SLOW_DCHECKS
895 if (FLAG_enable_slow_asserts) {
896 // Assert that the resource and the string are equivalent.
897 DCHECK(static_cast<size_t>(this->length()) == resource->length());
898 ScopedVector<uc16> smart_chars(this->length());
899 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
900 DCHECK(memcmp(smart_chars.start(),
902 resource->length() * sizeof(smart_chars[0])) == 0);
905 int size = this->Size(); // Byte size of the original string.
906 // Abort if size does not allow in-place conversion.
907 if (size < ExternalString::kShortSize) return false;
908 Heap* heap = GetHeap();
909 bool is_one_byte = this->IsOneByteRepresentation();
910 bool is_internalized = this->IsInternalizedString();
912 // Morph the string to an external string by replacing the map and
913 // reinitializing the fields. This won't work if the space the existing
914 // string occupies is too small for a regular external string.
915 // Instead, we resort to a short external string instead, omitting
916 // the field caching the address of the backing store. When we encounter
917 // short external strings in generated code, we need to bailout to runtime.
919 if (size < ExternalString::kSize) {
920 new_map = is_internalized
922 ? heap->short_external_internalized_string_with_one_byte_data_map()
923 : heap->short_external_internalized_string_map())
924 : (is_one_byte ? heap->short_external_string_with_one_byte_data_map()
925 : heap->short_external_string_map());
927 new_map = is_internalized
929 ? heap->external_internalized_string_with_one_byte_data_map()
930 : heap->external_internalized_string_map())
931 : (is_one_byte ? heap->external_string_with_one_byte_data_map()
932 : heap->external_string_map());
935 // Byte size of the external String object.
936 int new_size = this->SizeFromMap(new_map);
937 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size);
939 // We are storing the new map using release store after creating a filler for
940 // the left-over space to avoid races with the sweeper thread.
941 this->synchronized_set_map(new_map);
943 ExternalTwoByteString* self = ExternalTwoByteString::cast(this);
944 self->set_resource(resource);
945 if (is_internalized) self->Hash(); // Force regeneration of the hash value.
947 heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER);
952 bool String::MakeExternal(v8::String::ExternalOneByteStringResource* resource) {
953 // Externalizing twice leaks the external resource, so it's
954 // prohibited by the API.
955 DCHECK(!this->IsExternalString());
956 #ifdef ENABLE_SLOW_DCHECKS
957 if (FLAG_enable_slow_asserts) {
958 // Assert that the resource and the string are equivalent.
959 DCHECK(static_cast<size_t>(this->length()) == resource->length());
960 if (this->IsTwoByteRepresentation()) {
961 ScopedVector<uint16_t> smart_chars(this->length());
962 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
963 DCHECK(String::IsOneByte(smart_chars.start(), this->length()));
965 ScopedVector<char> smart_chars(this->length());
966 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
967 DCHECK(memcmp(smart_chars.start(),
969 resource->length() * sizeof(smart_chars[0])) == 0);
972 int size = this->Size(); // Byte size of the original string.
973 // Abort if size does not allow in-place conversion.
974 if (size < ExternalString::kShortSize) return false;
975 Heap* heap = GetHeap();
976 bool is_internalized = this->IsInternalizedString();
978 // Morph the string to an external string by replacing the map and
979 // reinitializing the fields. This won't work if the space the existing
980 // string occupies is too small for a regular external string.
981 // Instead, we resort to a short external string instead, omitting
982 // the field caching the address of the backing store. When we encounter
983 // short external strings in generated code, we need to bailout to runtime.
985 if (size < ExternalString::kSize) {
986 new_map = is_internalized
987 ? heap->short_external_one_byte_internalized_string_map()
988 : heap->short_external_one_byte_string_map();
990 new_map = is_internalized
991 ? heap->external_one_byte_internalized_string_map()
992 : heap->external_one_byte_string_map();
995 // Byte size of the external String object.
996 int new_size = this->SizeFromMap(new_map);
997 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size);
999 // We are storing the new map using release store after creating a filler for
1000 // the left-over space to avoid races with the sweeper thread.
1001 this->synchronized_set_map(new_map);
1003 ExternalOneByteString* self = ExternalOneByteString::cast(this);
1004 self->set_resource(resource);
1005 if (is_internalized) self->Hash(); // Force regeneration of the hash value.
1007 heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER);
1012 void String::StringShortPrint(StringStream* accumulator) {
1014 if (len > kMaxShortPrintLength) {
1015 accumulator->Add("<Very long string[%u]>", len);
1019 if (!LooksValid()) {
1020 accumulator->Add("<Invalid String>");
1024 StringCharacterStream stream(this);
1026 bool truncated = false;
1027 if (len > kMaxShortPrintLength) {
1028 len = kMaxShortPrintLength;
1031 bool one_byte = true;
1032 for (int i = 0; i < len; i++) {
1033 uint16_t c = stream.GetNext();
1035 if (c < 32 || c >= 127) {
1041 accumulator->Add("<String[%u]: ", length());
1042 for (int i = 0; i < len; i++) {
1043 accumulator->Put(static_cast<char>(stream.GetNext()));
1045 accumulator->Put('>');
1047 // Backslash indicates that the string contains control
1048 // characters and that backslashes are therefore escaped.
1049 accumulator->Add("<String[%u]\\: ", length());
1050 for (int i = 0; i < len; i++) {
1051 uint16_t c = stream.GetNext();
1053 accumulator->Add("\\n");
1054 } else if (c == '\r') {
1055 accumulator->Add("\\r");
1056 } else if (c == '\\') {
1057 accumulator->Add("\\\\");
1058 } else if (c < 32 || c > 126) {
1059 accumulator->Add("\\x%02x", c);
1061 accumulator->Put(static_cast<char>(c));
1065 accumulator->Put('.');
1066 accumulator->Put('.');
1067 accumulator->Put('.');
1069 accumulator->Put('>');
1075 void String::PrintUC16(std::ostream& os, int start, int end) { // NOLINT
1076 if (end < 0) end = length();
1077 StringCharacterStream stream(this, start);
1078 for (int i = start; i < end && stream.HasMore(); i++) {
1079 os << AsUC16(stream.GetNext());
1084 void JSObject::JSObjectShortPrint(StringStream* accumulator) {
1085 switch (map()->instance_type()) {
1086 case JS_ARRAY_TYPE: {
1087 double length = JSArray::cast(this)->length()->IsUndefined()
1089 : JSArray::cast(this)->length()->Number();
1090 accumulator->Add("<JS Array[%u]>", static_cast<uint32_t>(length));
1093 case JS_WEAK_MAP_TYPE: {
1094 accumulator->Add("<JS WeakMap>");
1097 case JS_WEAK_SET_TYPE: {
1098 accumulator->Add("<JS WeakSet>");
1101 case JS_REGEXP_TYPE: {
1102 accumulator->Add("<JS RegExp>");
1105 case JS_FUNCTION_TYPE: {
1106 JSFunction* function = JSFunction::cast(this);
1107 Object* fun_name = function->shared()->DebugName();
1108 bool printed = false;
1109 if (fun_name->IsString()) {
1110 String* str = String::cast(fun_name);
1111 if (str->length() > 0) {
1112 accumulator->Add("<JS Function ");
1113 accumulator->Put(str);
1118 accumulator->Add("<JS Function");
1120 accumulator->Add(" (SharedFunctionInfo %p)",
1121 reinterpret_cast<void*>(function->shared()));
1122 accumulator->Put('>');
1125 case JS_GENERATOR_OBJECT_TYPE: {
1126 accumulator->Add("<JS Generator>");
1129 case JS_MODULE_TYPE: {
1130 accumulator->Add("<JS Module>");
1133 // All other JSObjects are rather similar to each other (JSObject,
1134 // JSGlobalProxy, JSGlobalObject, JSUndetectableObject, JSValue).
1136 Map* map_of_this = map();
1137 Heap* heap = GetHeap();
1138 Object* constructor = map_of_this->GetConstructor();
1139 bool printed = false;
1140 if (constructor->IsHeapObject() &&
1141 !heap->Contains(HeapObject::cast(constructor))) {
1142 accumulator->Add("!!!INVALID CONSTRUCTOR!!!");
1144 bool global_object = IsJSGlobalProxy();
1145 if (constructor->IsJSFunction()) {
1146 if (!heap->Contains(JSFunction::cast(constructor)->shared())) {
1147 accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!");
1149 Object* constructor_name =
1150 JSFunction::cast(constructor)->shared()->name();
1151 if (constructor_name->IsString()) {
1152 String* str = String::cast(constructor_name);
1153 if (str->length() > 0) {
1154 bool vowel = AnWord(str);
1155 accumulator->Add("<%sa%s ",
1156 global_object ? "Global Object: " : "",
1158 accumulator->Put(str);
1159 accumulator->Add(" with %smap %p",
1160 map_of_this->is_deprecated() ? "deprecated " : "",
1168 accumulator->Add("<JS %sObject", global_object ? "Global " : "");
1172 accumulator->Add(" value = ");
1173 JSValue::cast(this)->value()->ShortPrint(accumulator);
1175 accumulator->Put('>');
1182 void JSObject::PrintElementsTransition(
1183 FILE* file, Handle<JSObject> object,
1184 ElementsKind from_kind, Handle<FixedArrayBase> from_elements,
1185 ElementsKind to_kind, Handle<FixedArrayBase> to_elements) {
1186 if (from_kind != to_kind) {
1188 os << "elements transition [" << ElementsKindToString(from_kind) << " -> "
1189 << ElementsKindToString(to_kind) << "] in ";
1190 JavaScriptFrame::PrintTop(object->GetIsolate(), file, false, true);
1191 PrintF(file, " for ");
1192 object->ShortPrint(file);
1193 PrintF(file, " from ");
1194 from_elements->ShortPrint(file);
1195 PrintF(file, " to ");
1196 to_elements->ShortPrint(file);
1202 void Map::PrintReconfiguration(FILE* file, int modify_index, PropertyKind kind,
1203 PropertyAttributes attributes) {
1205 os << "[reconfiguring ";
1206 constructor_name()->PrintOn(file);
1208 Name* name = instance_descriptors()->GetKey(modify_index);
1209 if (name->IsString()) {
1210 String::cast(name)->PrintOn(file);
1212 os << "{symbol " << static_cast<void*>(name) << "}";
1214 os << ": " << (kind == kData ? "kData" : "ACCESSORS") << ", attrs: ";
1215 os << attributes << " [";
1216 JavaScriptFrame::PrintTop(GetIsolate(), file, false, true);
1221 void Map::PrintGeneralization(FILE* file,
1226 bool constant_to_field,
1227 Representation old_representation,
1228 Representation new_representation,
1229 HeapType* old_field_type,
1230 HeapType* new_field_type) {
1232 os << "[generalizing ";
1233 constructor_name()->PrintOn(file);
1235 Name* name = instance_descriptors()->GetKey(modify_index);
1236 if (name->IsString()) {
1237 String::cast(name)->PrintOn(file);
1239 os << "{symbol " << static_cast<void*>(name) << "}";
1242 if (constant_to_field) {
1245 os << old_representation.Mnemonic() << "{";
1246 old_field_type->PrintTo(os, HeapType::SEMANTIC_DIM);
1249 os << "->" << new_representation.Mnemonic() << "{";
1250 new_field_type->PrintTo(os, HeapType::SEMANTIC_DIM);
1252 if (strlen(reason) > 0) {
1255 os << "+" << (descriptors - split) << " maps";
1258 JavaScriptFrame::PrintTop(GetIsolate(), file, false, true);
1263 void JSObject::PrintInstanceMigration(FILE* file,
1266 PrintF(file, "[migrating ");
1267 map()->constructor_name()->PrintOn(file);
1269 DescriptorArray* o = original_map->instance_descriptors();
1270 DescriptorArray* n = new_map->instance_descriptors();
1271 for (int i = 0; i < original_map->NumberOfOwnDescriptors(); i++) {
1272 Representation o_r = o->GetDetails(i).representation();
1273 Representation n_r = n->GetDetails(i).representation();
1274 if (!o_r.Equals(n_r)) {
1275 String::cast(o->GetKey(i))->PrintOn(file);
1276 PrintF(file, ":%s->%s ", o_r.Mnemonic(), n_r.Mnemonic());
1277 } else if (o->GetDetails(i).type() == DATA_CONSTANT &&
1278 n->GetDetails(i).type() == DATA) {
1279 Name* name = o->GetKey(i);
1280 if (name->IsString()) {
1281 String::cast(name)->PrintOn(file);
1283 PrintF(file, "{symbol %p}", static_cast<void*>(name));
1292 void HeapObject::HeapObjectShortPrint(std::ostream& os) { // NOLINT
1293 Heap* heap = GetHeap();
1294 if (!heap->Contains(this)) {
1295 os << "!!!INVALID POINTER!!!";
1298 if (!heap->Contains(map())) {
1299 os << "!!!INVALID MAP!!!";
1306 HeapStringAllocator allocator;
1307 StringStream accumulator(&allocator);
1308 String::cast(this)->StringShortPrint(&accumulator);
1309 os << accumulator.ToCString().get();
1313 HeapStringAllocator allocator;
1314 StringStream accumulator(&allocator);
1315 JSObject::cast(this)->JSObjectShortPrint(&accumulator);
1316 os << accumulator.ToCString().get();
1319 switch (map()->instance_type()) {
1321 os << "<Map(" << ElementsKindToString(Map::cast(this)->elements_kind())
1324 case FIXED_ARRAY_TYPE:
1325 os << "<FixedArray[" << FixedArray::cast(this)->length() << "]>";
1327 case FIXED_DOUBLE_ARRAY_TYPE:
1328 os << "<FixedDoubleArray[" << FixedDoubleArray::cast(this)->length()
1331 case BYTE_ARRAY_TYPE:
1332 os << "<ByteArray[" << ByteArray::cast(this)->length() << "]>";
1334 case BYTECODE_ARRAY_TYPE:
1335 os << "<BytecodeArray[" << BytecodeArray::cast(this)->length() << "]>";
1337 case FREE_SPACE_TYPE:
1338 os << "<FreeSpace[" << FreeSpace::cast(this)->Size() << "]>";
1340 #define TYPED_ARRAY_SHORT_PRINT(Type, type, TYPE, ctype, size) \
1341 case FIXED_##TYPE##_ARRAY_TYPE: \
1342 os << "<Fixed" #Type "Array[" << Fixed##Type##Array::cast(this)->length() \
1346 TYPED_ARRAYS(TYPED_ARRAY_SHORT_PRINT)
1347 #undef TYPED_ARRAY_SHORT_PRINT
1349 case SHARED_FUNCTION_INFO_TYPE: {
1350 SharedFunctionInfo* shared = SharedFunctionInfo::cast(this);
1351 base::SmartArrayPointer<char> debug_name =
1352 shared->DebugName()->ToCString();
1353 if (debug_name[0] != 0) {
1354 os << "<SharedFunctionInfo " << debug_name.get() << ">";
1356 os << "<SharedFunctionInfo>";
1360 case JS_MESSAGE_OBJECT_TYPE:
1361 os << "<JSMessageObject>";
1363 #define MAKE_STRUCT_CASE(NAME, Name, name) \
1365 os << "<" #Name ">"; \
1367 STRUCT_LIST(MAKE_STRUCT_CASE)
1368 #undef MAKE_STRUCT_CASE
1370 Code* code = Code::cast(this);
1371 os << "<Code: " << Code::Kind2String(code->kind()) << ">";
1374 case ODDBALL_TYPE: {
1375 if (IsUndefined()) {
1376 os << "<undefined>";
1377 } else if (IsTheHole()) {
1379 } else if (IsNull()) {
1381 } else if (IsTrue()) {
1383 } else if (IsFalse()) {
1386 os << "<Odd Oddball>";
1391 Symbol* symbol = Symbol::cast(this);
1392 symbol->SymbolShortPrint(os);
1395 case HEAP_NUMBER_TYPE: {
1397 HeapNumber::cast(this)->HeapNumberPrint(os);
1401 case MUTABLE_HEAP_NUMBER_TYPE: {
1402 os << "<MutableNumber: ";
1403 HeapNumber::cast(this)->HeapNumberPrint(os);
1407 case SIMD128_VALUE_TYPE: {
1408 #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
1410 os << "<" #Type ">"; \
1413 SIMD128_TYPES(SIMD128_TYPE)
1421 case JS_FUNCTION_PROXY_TYPE:
1422 os << "<JSFunctionProxy>";
1429 HeapStringAllocator allocator;
1430 StringStream accumulator(&allocator);
1431 Cell::cast(this)->value()->ShortPrint(&accumulator);
1432 os << accumulator.ToCString().get();
1435 case PROPERTY_CELL_TYPE: {
1436 os << "PropertyCell for ";
1437 HeapStringAllocator allocator;
1438 StringStream accumulator(&allocator);
1439 PropertyCell* cell = PropertyCell::cast(this);
1440 cell->value()->ShortPrint(&accumulator);
1441 os << accumulator.ToCString().get() << " " << cell->property_details();
1444 case WEAK_CELL_TYPE: {
1445 os << "WeakCell for ";
1446 HeapStringAllocator allocator;
1447 StringStream accumulator(&allocator);
1448 WeakCell::cast(this)->value()->ShortPrint(&accumulator);
1449 os << accumulator.ToCString().get();
1453 os << "<Other heap object (" << map()->instance_type() << ")>";
1459 void HeapObject::Iterate(ObjectVisitor* v) {
1461 IteratePointer(v, kMapOffset);
1462 // Handle object body
1464 IterateBody(m->instance_type(), SizeFromMap(m), v);
1468 bool HeapNumber::HeapNumberBooleanValue() {
1469 return DoubleToBoolean(value());
1473 void HeapNumber::HeapNumberPrint(std::ostream& os) { // NOLINT
1478 #define FIELD_ADDR_CONST(p, offset) \
1479 (reinterpret_cast<const byte*>(p) + offset - kHeapObjectTag)
1481 #define READ_INT32_FIELD(p, offset) \
1482 (*reinterpret_cast<const int32_t*>(FIELD_ADDR_CONST(p, offset)))
1484 #define READ_INT64_FIELD(p, offset) \
1485 (*reinterpret_cast<const int64_t*>(FIELD_ADDR_CONST(p, offset)))
1487 #define READ_BYTE_FIELD(p, offset) \
1488 (*reinterpret_cast<const byte*>(FIELD_ADDR_CONST(p, offset)))
1491 bool Simd128Value::BitwiseEquals(const Simd128Value* other) const {
1492 return READ_INT64_FIELD(this, kValueOffset) ==
1493 READ_INT64_FIELD(other, kValueOffset) &&
1494 READ_INT64_FIELD(this, kValueOffset + kInt64Size) ==
1495 READ_INT64_FIELD(other, kValueOffset + kInt64Size);
1499 uint32_t Simd128Value::Hash() const {
1500 uint32_t seed = v8::internal::kZeroHashSeed;
1502 hash = ComputeIntegerHash(READ_INT32_FIELD(this, kValueOffset), seed);
1503 hash = ComputeIntegerHash(
1504 READ_INT32_FIELD(this, kValueOffset + 1 * kInt32Size), hash * 31);
1505 hash = ComputeIntegerHash(
1506 READ_INT32_FIELD(this, kValueOffset + 2 * kInt32Size), hash * 31);
1507 hash = ComputeIntegerHash(
1508 READ_INT32_FIELD(this, kValueOffset + 3 * kInt32Size), hash * 31);
1513 void Simd128Value::CopyBits(void* destination) const {
1514 memcpy(destination, &READ_BYTE_FIELD(this, kValueOffset), kSimd128Size);
1518 String* JSReceiver::class_name() {
1519 if (IsJSFunction() || IsJSFunctionProxy()) {
1520 return GetHeap()->Function_string();
1522 Object* maybe_constructor = map()->GetConstructor();
1523 if (maybe_constructor->IsJSFunction()) {
1524 JSFunction* constructor = JSFunction::cast(maybe_constructor);
1525 return String::cast(constructor->shared()->instance_class_name());
1527 // If the constructor is not present, return "Object".
1528 return GetHeap()->Object_string();
1532 String* Map::constructor_name() {
1533 if (is_prototype_map() && prototype_info()->IsPrototypeInfo()) {
1534 PrototypeInfo* proto_info = PrototypeInfo::cast(prototype_info());
1535 if (proto_info->constructor_name()->IsString()) {
1536 return String::cast(proto_info->constructor_name());
1539 Object* maybe_constructor = GetConstructor();
1540 if (maybe_constructor->IsJSFunction()) {
1541 JSFunction* constructor = JSFunction::cast(maybe_constructor);
1542 String* name = String::cast(constructor->shared()->name());
1543 if (name->length() > 0) return name;
1544 String* inferred_name = constructor->shared()->inferred_name();
1545 if (inferred_name->length() > 0) return inferred_name;
1546 Object* proto = prototype();
1547 if (proto->IsJSObject()) return JSObject::cast(proto)->constructor_name();
1549 // TODO(rossberg): what about proxies?
1550 // If the constructor is not present, return "Object".
1551 return GetHeap()->Object_string();
1555 String* JSReceiver::constructor_name() {
1556 return map()->constructor_name();
1560 static Handle<Object> WrapType(Handle<HeapType> type) {
1561 if (type->IsClass()) return Map::WeakCellForMap(type->AsClass()->Map());
1566 MaybeHandle<Map> Map::CopyWithField(Handle<Map> map,
1568 Handle<HeapType> type,
1569 PropertyAttributes attributes,
1570 Representation representation,
1571 TransitionFlag flag) {
1572 DCHECK(DescriptorArray::kNotFound ==
1573 map->instance_descriptors()->Search(
1574 *name, map->NumberOfOwnDescriptors()));
1576 // Ensure the descriptor array does not get too big.
1577 if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) {
1578 return MaybeHandle<Map>();
1581 Isolate* isolate = map->GetIsolate();
1583 // Compute the new index for new field.
1584 int index = map->NextFreePropertyIndex();
1586 if (map->instance_type() == JS_CONTEXT_EXTENSION_OBJECT_TYPE) {
1587 representation = Representation::Tagged();
1588 type = HeapType::Any(isolate);
1591 Handle<Object> wrapped_type(WrapType(type));
1593 DataDescriptor new_field_desc(name, index, wrapped_type, attributes,
1595 Handle<Map> new_map = Map::CopyAddDescriptor(map, &new_field_desc, flag);
1596 int unused_property_fields = new_map->unused_property_fields() - 1;
1597 if (unused_property_fields < 0) {
1598 unused_property_fields += JSObject::kFieldsAdded;
1600 new_map->set_unused_property_fields(unused_property_fields);
1605 MaybeHandle<Map> Map::CopyWithConstant(Handle<Map> map,
1607 Handle<Object> constant,
1608 PropertyAttributes attributes,
1609 TransitionFlag flag) {
1610 // Ensure the descriptor array does not get too big.
1611 if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) {
1612 return MaybeHandle<Map>();
1615 // Allocate new instance descriptors with (name, constant) added.
1616 DataConstantDescriptor new_constant_desc(name, constant, attributes);
1617 return Map::CopyAddDescriptor(map, &new_constant_desc, flag);
1621 void JSObject::AddSlowProperty(Handle<JSObject> object,
1623 Handle<Object> value,
1624 PropertyAttributes attributes) {
1625 DCHECK(!object->HasFastProperties());
1626 Isolate* isolate = object->GetIsolate();
1627 if (object->IsGlobalObject()) {
1628 Handle<GlobalDictionary> dict(object->global_dictionary());
1629 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
1630 int entry = dict->FindEntry(name);
1631 // If there's a cell there, just invalidate and set the property.
1632 if (entry != GlobalDictionary::kNotFound) {
1633 PropertyCell::UpdateCell(dict, entry, value, details);
1634 // TODO(ishell): move this to UpdateCell.
1635 // Need to adjust the details.
1636 int index = dict->NextEnumerationIndex();
1637 dict->SetNextEnumerationIndex(index + 1);
1638 PropertyCell* cell = PropertyCell::cast(dict->ValueAt(entry));
1639 details = cell->property_details().set_index(index);
1640 cell->set_property_details(details);
1643 auto cell = isolate->factory()->NewPropertyCell();
1644 cell->set_value(*value);
1645 auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined
1646 : PropertyCellType::kConstant;
1647 details = details.set_cell_type(cell_type);
1650 Handle<GlobalDictionary> result =
1651 GlobalDictionary::Add(dict, name, value, details);
1652 if (*dict != *result) object->set_properties(*result);
1655 Handle<NameDictionary> dict(object->property_dictionary());
1656 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
1657 Handle<NameDictionary> result =
1658 NameDictionary::Add(dict, name, value, details);
1659 if (*dict != *result) object->set_properties(*result);
1664 Context* JSObject::GetCreationContext() {
1665 Object* constructor = this->map()->GetConstructor();
1666 JSFunction* function;
1667 if (!constructor->IsJSFunction()) {
1668 // Functions have null as a constructor,
1669 // but any JSFunction knows its context immediately.
1670 function = JSFunction::cast(this);
1672 function = JSFunction::cast(constructor);
1675 return function->context()->native_context();
1679 MaybeHandle<Object> JSObject::EnqueueChangeRecord(Handle<JSObject> object,
1680 const char* type_str,
1682 Handle<Object> old_value) {
1683 DCHECK(!object->IsJSGlobalProxy());
1684 DCHECK(!object->IsJSGlobalObject());
1685 Isolate* isolate = object->GetIsolate();
1686 HandleScope scope(isolate);
1687 Handle<String> type = isolate->factory()->InternalizeUtf8String(type_str);
1688 Handle<Object> args[] = { type, object, name, old_value };
1689 int argc = name.is_null() ? 2 : old_value->IsTheHole() ? 3 : 4;
1691 return Execution::Call(isolate,
1692 Handle<JSFunction>(isolate->observers_notify_change()),
1693 isolate->factory()->undefined_value(), argc, args);
1697 const char* Representation::Mnemonic() const {
1699 case kNone: return "v";
1700 case kTagged: return "t";
1701 case kSmi: return "s";
1702 case kDouble: return "d";
1703 case kInteger32: return "i";
1704 case kHeapObject: return "h";
1705 case kExternal: return "x";
1713 bool Map::InstancesNeedRewriting(Map* target, int target_number_of_fields,
1714 int target_inobject, int target_unused,
1715 int* old_number_of_fields) {
1716 // If fields were added (or removed), rewrite the instance.
1717 *old_number_of_fields = NumberOfFields();
1718 DCHECK(target_number_of_fields >= *old_number_of_fields);
1719 if (target_number_of_fields != *old_number_of_fields) return true;
1721 // If smi descriptors were replaced by double descriptors, rewrite.
1722 DescriptorArray* old_desc = instance_descriptors();
1723 DescriptorArray* new_desc = target->instance_descriptors();
1724 int limit = NumberOfOwnDescriptors();
1725 for (int i = 0; i < limit; i++) {
1726 if (new_desc->GetDetails(i).representation().IsDouble() !=
1727 old_desc->GetDetails(i).representation().IsDouble()) {
1732 // If no fields were added, and no inobject properties were removed, setting
1733 // the map is sufficient.
1734 if (target_inobject == inobject_properties()) return false;
1735 // In-object slack tracking may have reduced the object size of the new map.
1736 // In that case, succeed if all existing fields were inobject, and they still
1737 // fit within the new inobject size.
1738 DCHECK(target_inobject < inobject_properties());
1739 if (target_number_of_fields <= target_inobject) {
1740 DCHECK(target_number_of_fields + target_unused == target_inobject);
1743 // Otherwise, properties will need to be moved to the backing store.
1748 static void UpdatePrototypeUserRegistration(Handle<Map> old_map,
1749 Handle<Map> new_map,
1751 if (!FLAG_track_prototype_users) return;
1752 if (!old_map->is_prototype_map()) return;
1753 DCHECK(new_map->is_prototype_map());
1754 bool was_registered = JSObject::UnregisterPrototypeUser(old_map, isolate);
1755 new_map->set_prototype_info(old_map->prototype_info());
1756 old_map->set_prototype_info(Smi::FromInt(0));
1757 if (FLAG_trace_prototype_users) {
1758 PrintF("Moving prototype_info %p from map %p to map %p.\n",
1759 reinterpret_cast<void*>(new_map->prototype_info()),
1760 reinterpret_cast<void*>(*old_map),
1761 reinterpret_cast<void*>(*new_map));
1763 if (was_registered) {
1764 if (new_map->prototype_info()->IsPrototypeInfo()) {
1765 // The new map isn't registered with its prototype yet; reflect this fact
1766 // in the PrototypeInfo it just inherited from the old map.
1767 PrototypeInfo::cast(new_map->prototype_info())
1768 ->set_registry_slot(PrototypeInfo::UNREGISTERED);
1770 JSObject::LazyRegisterPrototypeUser(new_map, isolate);
1775 void JSObject::MigrateToMap(Handle<JSObject> object, Handle<Map> new_map,
1776 int expected_additional_properties) {
1777 if (object->map() == *new_map) return;
1778 // If this object is a prototype (the callee will check), invalidate any
1779 // prototype chains involving it.
1780 InvalidatePrototypeChains(object->map());
1781 Handle<Map> old_map(object->map());
1783 // If the map was registered with its prototype before, ensure that it
1784 // registers with its new prototype now. This preserves the invariant that
1785 // when a map on a prototype chain is registered with its prototype, then
1786 // all prototypes further up the chain are also registered with their
1787 // respective prototypes.
1788 UpdatePrototypeUserRegistration(old_map, new_map, new_map->GetIsolate());
1790 if (object->HasFastProperties()) {
1791 if (!new_map->is_dictionary_map()) {
1792 MigrateFastToFast(object, new_map);
1793 if (old_map->is_prototype_map()) {
1794 // Clear out the old descriptor array to avoid problems to sharing
1795 // the descriptor array without using an explicit.
1796 old_map->InitializeDescriptors(
1797 old_map->GetHeap()->empty_descriptor_array(),
1798 LayoutDescriptor::FastPointerLayout());
1799 // Ensure that no transition was inserted for prototype migrations.
1800 DCHECK_EQ(0, TransitionArray::NumberOfTransitions(
1801 old_map->raw_transitions()));
1802 DCHECK(new_map->GetBackPointer()->IsUndefined());
1805 MigrateFastToSlow(object, new_map, expected_additional_properties);
1808 // For slow-to-fast migrations JSObject::MigrateSlowToFast()
1809 // must be used instead.
1810 CHECK(new_map->is_dictionary_map());
1812 // Slow-to-slow migration is trivial.
1813 object->set_map(*new_map);
1816 // Careful: Don't allocate here!
1817 // For some callers of this method, |object| might be in an inconsistent
1818 // state now: the new map might have a new elements_kind, but the object's
1819 // elements pointer hasn't been updated yet. Callers will fix this, but in
1820 // the meantime, (indirectly) calling JSObjectVerify() must be avoided.
1821 // When adding code here, add a DisallowHeapAllocation too.
1825 // To migrate a fast instance to a fast map:
1826 // - First check whether the instance needs to be rewritten. If not, simply
1828 // - Otherwise, allocate a fixed array large enough to hold all fields, in
1829 // addition to unused space.
1830 // - Copy all existing properties in, in the following order: backing store
1831 // properties, unused fields, inobject properties.
1832 // - If all allocation succeeded, commit the state atomically:
1833 // * Copy inobject properties from the backing store back into the object.
1834 // * Trim the difference in instance size of the object. This also cleanly
1835 // frees inobject properties that moved to the backing store.
1836 // * If there are properties left in the backing store, trim of the space used
1837 // to temporarily store the inobject properties.
1838 // * If there are properties left in the backing store, install the backing
1840 void JSObject::MigrateFastToFast(Handle<JSObject> object, Handle<Map> new_map) {
1841 Isolate* isolate = object->GetIsolate();
1842 Handle<Map> old_map(object->map());
1843 int old_number_of_fields;
1844 int number_of_fields = new_map->NumberOfFields();
1845 int inobject = new_map->inobject_properties();
1846 int unused = new_map->unused_property_fields();
1848 // Nothing to do if no functions were converted to fields and no smis were
1849 // converted to doubles.
1850 if (!old_map->InstancesNeedRewriting(*new_map, number_of_fields, inobject,
1851 unused, &old_number_of_fields)) {
1852 object->synchronized_set_map(*new_map);
1856 int total_size = number_of_fields + unused;
1857 int external = total_size - inobject;
1859 if (number_of_fields != old_number_of_fields &&
1860 new_map->GetBackPointer() == *old_map) {
1861 PropertyDetails details = new_map->GetLastDescriptorDetails();
1863 if (old_map->unused_property_fields() > 0) {
1864 if (details.representation().IsDouble()) {
1866 FieldIndex::ForDescriptor(*new_map, new_map->LastAdded());
1867 if (new_map->IsUnboxedDoubleField(index)) {
1868 object->RawFastDoublePropertyAtPut(index, 0);
1870 Handle<Object> value = isolate->factory()->NewHeapNumber(0, MUTABLE);
1871 object->RawFastPropertyAtPut(index, *value);
1874 object->synchronized_set_map(*new_map);
1878 DCHECK(number_of_fields == old_number_of_fields + 1);
1879 // This migration is a transition from a map that has run out of property
1880 // space. Therefore it could be done by extending the backing store.
1881 int grow_by = external - object->properties()->length();
1882 Handle<FixedArray> old_storage = handle(object->properties(), isolate);
1883 Handle<FixedArray> new_storage =
1884 isolate->factory()->CopyFixedArrayAndGrow(old_storage, grow_by);
1886 // Properly initialize newly added property.
1887 Handle<Object> value;
1888 if (details.representation().IsDouble()) {
1889 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
1891 value = isolate->factory()->uninitialized_value();
1893 DCHECK(details.type() == DATA);
1894 int target_index = details.field_index() - inobject;
1895 DCHECK(target_index >= 0); // Must be a backing store index.
1896 new_storage->set(target_index, *value);
1898 // From here on we cannot fail and we shouldn't GC anymore.
1899 DisallowHeapAllocation no_allocation;
1901 // Set the new property value and do the map transition.
1902 object->set_properties(*new_storage);
1903 object->synchronized_set_map(*new_map);
1906 Handle<FixedArray> array = isolate->factory()->NewFixedArray(total_size);
1908 Handle<DescriptorArray> old_descriptors(old_map->instance_descriptors());
1909 Handle<DescriptorArray> new_descriptors(new_map->instance_descriptors());
1910 int old_nof = old_map->NumberOfOwnDescriptors();
1911 int new_nof = new_map->NumberOfOwnDescriptors();
1913 // This method only supports generalizing instances to at least the same
1914 // number of properties.
1915 DCHECK(old_nof <= new_nof);
1917 for (int i = 0; i < old_nof; i++) {
1918 PropertyDetails details = new_descriptors->GetDetails(i);
1919 if (details.type() != DATA) continue;
1920 PropertyDetails old_details = old_descriptors->GetDetails(i);
1921 Representation old_representation = old_details.representation();
1922 Representation representation = details.representation();
1923 Handle<Object> value;
1924 if (old_details.type() == ACCESSOR_CONSTANT) {
1925 // In case of kAccessor -> kData property reconfiguration, the property
1926 // must already be prepared for data or certain type.
1927 DCHECK(!details.representation().IsNone());
1928 if (details.representation().IsDouble()) {
1929 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
1931 value = isolate->factory()->uninitialized_value();
1933 } else if (old_details.type() == DATA_CONSTANT) {
1934 value = handle(old_descriptors->GetValue(i), isolate);
1935 DCHECK(!old_representation.IsDouble() && !representation.IsDouble());
1937 FieldIndex index = FieldIndex::ForDescriptor(*old_map, i);
1938 if (object->IsUnboxedDoubleField(index)) {
1939 double old = object->RawFastDoublePropertyAt(index);
1940 value = isolate->factory()->NewHeapNumber(
1941 old, representation.IsDouble() ? MUTABLE : IMMUTABLE);
1944 value = handle(object->RawFastPropertyAt(index), isolate);
1945 if (!old_representation.IsDouble() && representation.IsDouble()) {
1946 if (old_representation.IsNone()) {
1947 value = handle(Smi::FromInt(0), isolate);
1949 value = Object::NewStorageFor(isolate, value, representation);
1950 } else if (old_representation.IsDouble() &&
1951 !representation.IsDouble()) {
1952 value = Object::WrapForRead(isolate, value, old_representation);
1956 DCHECK(!(representation.IsDouble() && value->IsSmi()));
1957 int target_index = new_descriptors->GetFieldIndex(i) - inobject;
1958 if (target_index < 0) target_index += total_size;
1959 array->set(target_index, *value);
1962 for (int i = old_nof; i < new_nof; i++) {
1963 PropertyDetails details = new_descriptors->GetDetails(i);
1964 if (details.type() != DATA) continue;
1965 Handle<Object> value;
1966 if (details.representation().IsDouble()) {
1967 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
1969 value = isolate->factory()->uninitialized_value();
1971 int target_index = new_descriptors->GetFieldIndex(i) - inobject;
1972 if (target_index < 0) target_index += total_size;
1973 array->set(target_index, *value);
1976 // From here on we cannot fail and we shouldn't GC anymore.
1977 DisallowHeapAllocation no_allocation;
1979 // Copy (real) inobject properties. If necessary, stop at number_of_fields to
1980 // avoid overwriting |one_pointer_filler_map|.
1981 int limit = Min(inobject, number_of_fields);
1982 for (int i = 0; i < limit; i++) {
1983 FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i);
1984 Object* value = array->get(external + i);
1985 // Can't use JSObject::FastPropertyAtPut() because proper map was not set
1987 if (new_map->IsUnboxedDoubleField(index)) {
1988 DCHECK(value->IsMutableHeapNumber());
1989 object->RawFastDoublePropertyAtPut(index,
1990 HeapNumber::cast(value)->value());
1992 object->RawFastPropertyAtPut(index, value);
1996 Heap* heap = isolate->heap();
1998 // If there are properties in the new backing store, trim it to the correct
1999 // size and install the backing store into the object.
2001 heap->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(*array, inobject);
2002 object->set_properties(*array);
2005 // Create filler object past the new instance size.
2006 int new_instance_size = new_map->instance_size();
2007 int instance_size_delta = old_map->instance_size() - new_instance_size;
2008 DCHECK(instance_size_delta >= 0);
2010 if (instance_size_delta > 0) {
2011 Address address = object->address();
2012 heap->CreateFillerObjectAt(
2013 address + new_instance_size, instance_size_delta);
2014 heap->AdjustLiveBytes(*object, -instance_size_delta,
2015 Heap::CONCURRENT_TO_SWEEPER);
2018 // We are storing the new map using release store after creating a filler for
2019 // the left-over space to avoid races with the sweeper thread.
2020 object->synchronized_set_map(*new_map);
2024 int Map::NumberOfFields() {
2025 DescriptorArray* descriptors = instance_descriptors();
2027 for (int i = 0; i < NumberOfOwnDescriptors(); i++) {
2028 if (descriptors->GetDetails(i).location() == kField) result++;
2034 Handle<Map> Map::CopyGeneralizeAllRepresentations(
2035 Handle<Map> map, int modify_index, StoreMode store_mode, PropertyKind kind,
2036 PropertyAttributes attributes, const char* reason) {
2037 Isolate* isolate = map->GetIsolate();
2038 Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate);
2039 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
2040 Handle<DescriptorArray> descriptors =
2041 DescriptorArray::CopyUpTo(old_descriptors, number_of_own_descriptors);
2043 for (int i = 0; i < number_of_own_descriptors; i++) {
2044 descriptors->SetRepresentation(i, Representation::Tagged());
2045 if (descriptors->GetDetails(i).type() == DATA) {
2046 descriptors->SetValue(i, HeapType::Any());
2050 Handle<LayoutDescriptor> new_layout_descriptor(
2051 LayoutDescriptor::FastPointerLayout(), isolate);
2052 Handle<Map> new_map = CopyReplaceDescriptors(
2053 map, descriptors, new_layout_descriptor, OMIT_TRANSITION,
2054 MaybeHandle<Name>(), reason, SPECIAL_TRANSITION);
2056 // Unless the instance is being migrated, ensure that modify_index is a field.
2057 if (modify_index >= 0) {
2058 PropertyDetails details = descriptors->GetDetails(modify_index);
2059 if (store_mode == FORCE_FIELD &&
2060 (details.type() != DATA || details.attributes() != attributes)) {
2061 int field_index = details.type() == DATA ? details.field_index()
2062 : new_map->NumberOfFields();
2063 DataDescriptor d(handle(descriptors->GetKey(modify_index), isolate),
2064 field_index, attributes, Representation::Tagged());
2065 descriptors->Replace(modify_index, &d);
2066 if (details.type() != DATA) {
2067 int unused_property_fields = new_map->unused_property_fields() - 1;
2068 if (unused_property_fields < 0) {
2069 unused_property_fields += JSObject::kFieldsAdded;
2071 new_map->set_unused_property_fields(unused_property_fields);
2074 DCHECK(details.attributes() == attributes);
2077 if (FLAG_trace_generalization) {
2078 HeapType* field_type =
2079 (details.type() == DATA)
2080 ? map->instance_descriptors()->GetFieldType(modify_index)
2082 map->PrintGeneralization(
2083 stdout, reason, modify_index, new_map->NumberOfOwnDescriptors(),
2084 new_map->NumberOfOwnDescriptors(),
2085 details.type() == DATA_CONSTANT && store_mode == FORCE_FIELD,
2086 details.representation(), Representation::Tagged(), field_type,
2094 void Map::DeprecateTransitionTree() {
2095 if (is_deprecated()) return;
2096 Object* transitions = raw_transitions();
2097 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
2098 for (int i = 0; i < num_transitions; ++i) {
2099 TransitionArray::GetTarget(transitions, i)->DeprecateTransitionTree();
2102 dependent_code()->DeoptimizeDependentCodeGroup(
2103 GetIsolate(), DependentCode::kTransitionGroup);
2104 NotifyLeafMapLayoutChange();
2108 static inline bool EqualImmutableValues(Object* obj1, Object* obj2) {
2109 if (obj1 == obj2) return true; // Valid for both kData and kAccessor kinds.
2110 // TODO(ishell): compare AccessorPairs.
2115 // Invalidates a transition target at |key|, and installs |new_descriptors| over
2116 // the current instance_descriptors to ensure proper sharing of descriptor
2118 // Returns true if the transition target at given key was deprecated.
2119 bool Map::DeprecateTarget(PropertyKind kind, Name* key,
2120 PropertyAttributes attributes,
2121 DescriptorArray* new_descriptors,
2122 LayoutDescriptor* new_layout_descriptor) {
2123 bool transition_target_deprecated = false;
2124 Map* maybe_transition =
2125 TransitionArray::SearchTransition(this, kind, key, attributes);
2126 if (maybe_transition != NULL) {
2127 maybe_transition->DeprecateTransitionTree();
2128 transition_target_deprecated = true;
2131 // Don't overwrite the empty descriptor array.
2132 if (NumberOfOwnDescriptors() == 0) return transition_target_deprecated;
2134 DescriptorArray* to_replace = instance_descriptors();
2135 Map* current = this;
2136 GetHeap()->incremental_marking()->RecordWrites(to_replace);
2137 while (current->instance_descriptors() == to_replace) {
2138 current->SetEnumLength(kInvalidEnumCacheSentinel);
2139 current->UpdateDescriptors(new_descriptors, new_layout_descriptor);
2140 Object* next = current->GetBackPointer();
2141 if (next->IsUndefined()) break;
2142 current = Map::cast(next);
2145 set_owns_descriptors(false);
2146 return transition_target_deprecated;
2150 Map* Map::FindRootMap() {
2153 Object* back = result->GetBackPointer();
2154 if (back->IsUndefined()) return result;
2155 result = Map::cast(back);
2160 Map* Map::FindLastMatchMap(int verbatim,
2162 DescriptorArray* descriptors) {
2163 DisallowHeapAllocation no_allocation;
2165 // This can only be called on roots of transition trees.
2166 DCHECK_EQ(verbatim, NumberOfOwnDescriptors());
2168 Map* current = this;
2170 for (int i = verbatim; i < length; i++) {
2171 Name* name = descriptors->GetKey(i);
2172 PropertyDetails details = descriptors->GetDetails(i);
2173 Map* next = TransitionArray::SearchTransition(current, details.kind(), name,
2174 details.attributes());
2175 if (next == NULL) break;
2176 DescriptorArray* next_descriptors = next->instance_descriptors();
2178 PropertyDetails next_details = next_descriptors->GetDetails(i);
2179 DCHECK_EQ(details.kind(), next_details.kind());
2180 DCHECK_EQ(details.attributes(), next_details.attributes());
2181 if (details.location() != next_details.location()) break;
2182 if (!details.representation().Equals(next_details.representation())) break;
2184 if (next_details.location() == kField) {
2185 HeapType* next_field_type = next_descriptors->GetFieldType(i);
2186 if (!descriptors->GetFieldType(i)->NowIs(next_field_type)) {
2190 if (!EqualImmutableValues(descriptors->GetValue(i),
2191 next_descriptors->GetValue(i))) {
2201 Map* Map::FindFieldOwner(int descriptor) {
2202 DisallowHeapAllocation no_allocation;
2203 DCHECK_EQ(DATA, instance_descriptors()->GetDetails(descriptor).type());
2206 Object* back = result->GetBackPointer();
2207 if (back->IsUndefined()) break;
2208 Map* parent = Map::cast(back);
2209 if (parent->NumberOfOwnDescriptors() <= descriptor) break;
2216 void Map::UpdateFieldType(int descriptor, Handle<Name> name,
2217 Representation new_representation,
2218 Handle<Object> new_wrapped_type) {
2219 DCHECK(new_wrapped_type->IsSmi() || new_wrapped_type->IsWeakCell());
2220 DisallowHeapAllocation no_allocation;
2221 PropertyDetails details = instance_descriptors()->GetDetails(descriptor);
2222 if (details.type() != DATA) return;
2223 Object* transitions = raw_transitions();
2224 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
2225 for (int i = 0; i < num_transitions; ++i) {
2226 Map* target = TransitionArray::GetTarget(transitions, i);
2227 target->UpdateFieldType(descriptor, name, new_representation,
2230 // It is allowed to change representation here only from None to something.
2231 DCHECK(details.representation().Equals(new_representation) ||
2232 details.representation().IsNone());
2234 // Skip if already updated the shared descriptor.
2235 if (instance_descriptors()->GetValue(descriptor) == *new_wrapped_type) return;
2236 DataDescriptor d(name, instance_descriptors()->GetFieldIndex(descriptor),
2237 new_wrapped_type, details.attributes(), new_representation);
2238 instance_descriptors()->Replace(descriptor, &d);
2243 Handle<HeapType> Map::GeneralizeFieldType(Handle<HeapType> type1,
2244 Handle<HeapType> type2,
2246 if (type1->NowIs(type2)) return type2;
2247 if (type2->NowIs(type1)) return type1;
2248 return HeapType::Any(isolate);
2253 void Map::GeneralizeFieldType(Handle<Map> map, int modify_index,
2254 Representation new_representation,
2255 Handle<HeapType> new_field_type) {
2256 Isolate* isolate = map->GetIsolate();
2258 // Check if we actually need to generalize the field type at all.
2259 Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate);
2260 Representation old_representation =
2261 old_descriptors->GetDetails(modify_index).representation();
2262 Handle<HeapType> old_field_type(old_descriptors->GetFieldType(modify_index),
2265 if (old_representation.Equals(new_representation) &&
2266 new_field_type->NowIs(old_field_type)) {
2267 DCHECK(Map::GeneralizeFieldType(old_field_type,
2269 isolate)->NowIs(old_field_type));
2273 // Determine the field owner.
2274 Handle<Map> field_owner(map->FindFieldOwner(modify_index), isolate);
2275 Handle<DescriptorArray> descriptors(
2276 field_owner->instance_descriptors(), isolate);
2277 DCHECK_EQ(*old_field_type, descriptors->GetFieldType(modify_index));
2278 bool old_field_type_was_cleared =
2279 old_field_type->Is(HeapType::None()) && old_representation.IsHeapObject();
2281 // Determine the generalized new field type. Conservatively assume type Any
2282 // for cleared field types because the cleared type could have been a
2283 // deprecated map and there still could be live instances with a non-
2284 // deprecated version of the map.
2286 old_field_type_was_cleared
2287 ? HeapType::Any(isolate)
2288 : Map::GeneralizeFieldType(old_field_type, new_field_type, isolate);
2290 PropertyDetails details = descriptors->GetDetails(modify_index);
2291 Handle<Name> name(descriptors->GetKey(modify_index));
2293 Handle<Object> wrapped_type(WrapType(new_field_type));
2294 field_owner->UpdateFieldType(modify_index, name, new_representation,
2296 field_owner->dependent_code()->DeoptimizeDependentCodeGroup(
2297 isolate, DependentCode::kFieldTypeGroup);
2299 if (FLAG_trace_generalization) {
2300 map->PrintGeneralization(
2301 stdout, "field type generalization",
2302 modify_index, map->NumberOfOwnDescriptors(),
2303 map->NumberOfOwnDescriptors(), false,
2304 details.representation(), details.representation(),
2305 *old_field_type, *new_field_type);
2310 static inline Handle<HeapType> GetFieldType(Isolate* isolate,
2311 Handle<DescriptorArray> descriptors,
2313 PropertyLocation location,
2314 Representation representation) {
2316 PropertyDetails details = descriptors->GetDetails(descriptor);
2317 DCHECK_EQ(kData, details.kind());
2318 DCHECK_EQ(details.location(), location);
2320 if (location == kField) {
2321 return handle(descriptors->GetFieldType(descriptor), isolate);
2323 return descriptors->GetValue(descriptor)
2324 ->OptimalType(isolate, representation);
2329 // Reconfigures property at |modify_index| with |new_kind|, |new_attributes|,
2330 // |store_mode| and/or |new_representation|/|new_field_type|.
2331 // If |modify_index| is negative then no properties are reconfigured but the
2332 // map is migrated to the up-to-date non-deprecated state.
2334 // This method rewrites or completes the transition tree to reflect the new
2335 // change. To avoid high degrees over polymorphism, and to stabilize quickly,
2336 // on every rewrite the new type is deduced by merging the current type with
2337 // any potential new (partial) version of the type in the transition tree.
2338 // To do this, on each rewrite:
2339 // - Search the root of the transition tree using FindRootMap.
2340 // - Find |target_map|, the newest matching version of this map using the
2341 // virtually "enhanced" |old_map|'s descriptor array (i.e. whose entry at
2342 // |modify_index| is considered to be of |new_kind| and having
2343 // |new_attributes|) to walk the transition tree.
2344 // - Merge/generalize the "enhanced" descriptor array of the |old_map| and
2345 // descriptor array of the |target_map|.
2346 // - Generalize the |modify_index| descriptor using |new_representation| and
2347 // |new_field_type|.
2348 // - Walk the tree again starting from the root towards |target_map|. Stop at
2349 // |split_map|, the first map who's descriptor array does not match the merged
2350 // descriptor array.
2351 // - If |target_map| == |split_map|, |target_map| is in the expected state.
2353 // - Otherwise, invalidate the outdated transition target from |target_map|, and
2354 // replace its transition tree with a new branch for the updated descriptors.
2355 Handle<Map> Map::ReconfigureProperty(Handle<Map> old_map, int modify_index,
2356 PropertyKind new_kind,
2357 PropertyAttributes new_attributes,
2358 Representation new_representation,
2359 Handle<HeapType> new_field_type,
2360 StoreMode store_mode) {
2361 DCHECK_NE(kAccessor, new_kind); // TODO(ishell): not supported yet.
2362 DCHECK(store_mode != FORCE_FIELD || modify_index >= 0);
2363 Isolate* isolate = old_map->GetIsolate();
2365 Handle<DescriptorArray> old_descriptors(
2366 old_map->instance_descriptors(), isolate);
2367 int old_nof = old_map->NumberOfOwnDescriptors();
2369 // If it's just a representation generalization case (i.e. property kind and
2370 // attributes stays unchanged) it's fine to transition from None to anything
2371 // but double without any modification to the object, because the default
2372 // uninitialized value for representation None can be overwritten by both
2373 // smi and tagged values. Doubles, however, would require a box allocation.
2374 if (modify_index >= 0 && !new_representation.IsNone() &&
2375 !new_representation.IsDouble()) {
2376 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
2377 Representation old_representation = old_details.representation();
2379 if (old_representation.IsNone()) {
2380 DCHECK_EQ(new_kind, old_details.kind());
2381 DCHECK_EQ(new_attributes, old_details.attributes());
2382 DCHECK_EQ(DATA, old_details.type());
2383 if (FLAG_trace_generalization) {
2384 old_map->PrintGeneralization(
2385 stdout, "uninitialized field", modify_index,
2386 old_map->NumberOfOwnDescriptors(),
2387 old_map->NumberOfOwnDescriptors(), false, old_representation,
2388 new_representation, old_descriptors->GetFieldType(modify_index),
2391 Handle<Map> field_owner(old_map->FindFieldOwner(modify_index), isolate);
2393 GeneralizeFieldType(field_owner, modify_index, new_representation,
2395 DCHECK(old_descriptors->GetDetails(modify_index)
2397 .Equals(new_representation));
2399 old_descriptors->GetFieldType(modify_index)->NowIs(new_field_type));
2404 // Check the state of the root map.
2405 Handle<Map> root_map(old_map->FindRootMap(), isolate);
2406 if (!old_map->EquivalentToForTransition(*root_map)) {
2407 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2408 new_kind, new_attributes,
2409 "GenAll_NotEquivalent");
2412 ElementsKind from_kind = root_map->elements_kind();
2413 ElementsKind to_kind = old_map->elements_kind();
2414 // TODO(ishell): Add a test for SLOW_SLOPPY_ARGUMENTS_ELEMENTS.
2415 if (from_kind != to_kind && to_kind != DICTIONARY_ELEMENTS &&
2416 to_kind != SLOW_SLOPPY_ARGUMENTS_ELEMENTS &&
2417 !(IsTransitionableFastElementsKind(from_kind) &&
2418 IsMoreGeneralElementsKindTransition(from_kind, to_kind))) {
2419 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2420 new_kind, new_attributes,
2421 "GenAll_InvalidElementsTransition");
2423 int root_nof = root_map->NumberOfOwnDescriptors();
2424 if (modify_index >= 0 && modify_index < root_nof) {
2425 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
2426 if (old_details.kind() != new_kind ||
2427 old_details.attributes() != new_attributes) {
2428 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2429 new_kind, new_attributes,
2430 "GenAll_RootModification1");
2432 if ((old_details.type() != DATA && store_mode == FORCE_FIELD) ||
2433 (old_details.type() == DATA &&
2434 (!new_field_type->NowIs(old_descriptors->GetFieldType(modify_index)) ||
2435 !new_representation.fits_into(old_details.representation())))) {
2436 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2437 new_kind, new_attributes,
2438 "GenAll_RootModification2");
2442 // From here on, use the map with correct elements kind as root map.
2443 if (from_kind != to_kind) {
2444 root_map = Map::AsElementsKind(root_map, to_kind);
2447 Handle<Map> target_map = root_map;
2448 for (int i = root_nof; i < old_nof; ++i) {
2449 PropertyDetails old_details = old_descriptors->GetDetails(i);
2450 PropertyKind next_kind;
2451 PropertyLocation next_location;
2452 PropertyAttributes next_attributes;
2453 Representation next_representation;
2454 bool property_kind_reconfiguration = false;
2456 if (modify_index == i) {
2457 DCHECK_EQ(FORCE_FIELD, store_mode);
2458 property_kind_reconfiguration = old_details.kind() != new_kind;
2460 next_kind = new_kind;
2461 next_location = kField;
2462 next_attributes = new_attributes;
2463 // If property kind is not reconfigured merge the result with
2464 // representation/field type from the old descriptor.
2465 next_representation = new_representation;
2466 if (!property_kind_reconfiguration) {
2467 next_representation =
2468 next_representation.generalize(old_details.representation());
2472 next_kind = old_details.kind();
2473 next_location = old_details.location();
2474 next_attributes = old_details.attributes();
2475 next_representation = old_details.representation();
2477 Map* transition = TransitionArray::SearchTransition(
2478 *target_map, next_kind, old_descriptors->GetKey(i), next_attributes);
2479 if (transition == NULL) break;
2480 Handle<Map> tmp_map(transition, isolate);
2482 Handle<DescriptorArray> tmp_descriptors = handle(
2483 tmp_map->instance_descriptors(), isolate);
2485 // Check if target map is incompatible.
2486 PropertyDetails tmp_details = tmp_descriptors->GetDetails(i);
2487 DCHECK_EQ(next_kind, tmp_details.kind());
2488 DCHECK_EQ(next_attributes, tmp_details.attributes());
2489 if (next_kind == kAccessor &&
2490 !EqualImmutableValues(old_descriptors->GetValue(i),
2491 tmp_descriptors->GetValue(i))) {
2492 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2493 new_kind, new_attributes,
2494 "GenAll_Incompatible");
2496 if (next_location == kField && tmp_details.location() == kDescriptor) break;
2498 Representation tmp_representation = tmp_details.representation();
2499 if (!next_representation.fits_into(tmp_representation)) break;
2501 PropertyLocation old_location = old_details.location();
2502 PropertyLocation tmp_location = tmp_details.location();
2503 if (tmp_location == kField) {
2504 if (next_kind == kData) {
2505 Handle<HeapType> next_field_type;
2506 if (modify_index == i) {
2507 next_field_type = new_field_type;
2508 if (!property_kind_reconfiguration) {
2509 Handle<HeapType> old_field_type =
2510 GetFieldType(isolate, old_descriptors, i,
2511 old_details.location(), tmp_representation);
2513 GeneralizeFieldType(next_field_type, old_field_type, isolate);
2516 Handle<HeapType> old_field_type =
2517 GetFieldType(isolate, old_descriptors, i, old_details.location(),
2518 tmp_representation);
2519 next_field_type = old_field_type;
2521 GeneralizeFieldType(tmp_map, i, tmp_representation, next_field_type);
2523 } else if (old_location == kField ||
2524 !EqualImmutableValues(old_descriptors->GetValue(i),
2525 tmp_descriptors->GetValue(i))) {
2528 DCHECK(!tmp_map->is_deprecated());
2529 target_map = tmp_map;
2532 // Directly change the map if the target map is more general.
2533 Handle<DescriptorArray> target_descriptors(
2534 target_map->instance_descriptors(), isolate);
2535 int target_nof = target_map->NumberOfOwnDescriptors();
2536 if (target_nof == old_nof &&
2537 (store_mode != FORCE_FIELD ||
2538 (modify_index >= 0 &&
2539 target_descriptors->GetDetails(modify_index).location() == kField))) {
2541 if (modify_index >= 0) {
2542 PropertyDetails details = target_descriptors->GetDetails(modify_index);
2543 DCHECK_EQ(new_kind, details.kind());
2544 DCHECK_EQ(new_attributes, details.attributes());
2545 DCHECK(new_representation.fits_into(details.representation()));
2546 DCHECK(details.location() != kField ||
2547 new_field_type->NowIs(
2548 target_descriptors->GetFieldType(modify_index)));
2551 if (*target_map != *old_map) {
2552 old_map->NotifyLeafMapLayoutChange();
2557 // Find the last compatible target map in the transition tree.
2558 for (int i = target_nof; i < old_nof; ++i) {
2559 PropertyDetails old_details = old_descriptors->GetDetails(i);
2560 PropertyKind next_kind;
2561 PropertyAttributes next_attributes;
2562 if (modify_index == i) {
2563 next_kind = new_kind;
2564 next_attributes = new_attributes;
2566 next_kind = old_details.kind();
2567 next_attributes = old_details.attributes();
2569 Map* transition = TransitionArray::SearchTransition(
2570 *target_map, next_kind, old_descriptors->GetKey(i), next_attributes);
2571 if (transition == NULL) break;
2572 Handle<Map> tmp_map(transition, isolate);
2573 Handle<DescriptorArray> tmp_descriptors(
2574 tmp_map->instance_descriptors(), isolate);
2576 // Check if target map is compatible.
2578 PropertyDetails tmp_details = tmp_descriptors->GetDetails(i);
2579 DCHECK_EQ(next_kind, tmp_details.kind());
2580 DCHECK_EQ(next_attributes, tmp_details.attributes());
2582 if (next_kind == kAccessor &&
2583 !EqualImmutableValues(old_descriptors->GetValue(i),
2584 tmp_descriptors->GetValue(i))) {
2585 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2586 new_kind, new_attributes,
2587 "GenAll_Incompatible");
2589 DCHECK(!tmp_map->is_deprecated());
2590 target_map = tmp_map;
2592 target_nof = target_map->NumberOfOwnDescriptors();
2593 target_descriptors = handle(target_map->instance_descriptors(), isolate);
2595 // Allocate a new descriptor array large enough to hold the required
2596 // descriptors, with minimally the exact same size as the old descriptor
2598 int new_slack = Max(
2599 old_nof, old_descriptors->number_of_descriptors()) - old_nof;
2600 Handle<DescriptorArray> new_descriptors = DescriptorArray::Allocate(
2601 isolate, old_nof, new_slack);
2602 DCHECK(new_descriptors->length() > target_descriptors->length() ||
2603 new_descriptors->NumberOfSlackDescriptors() > 0 ||
2604 new_descriptors->number_of_descriptors() ==
2605 old_descriptors->number_of_descriptors());
2606 DCHECK(new_descriptors->number_of_descriptors() == old_nof);
2609 int current_offset = 0;
2610 for (int i = 0; i < root_nof; ++i) {
2611 PropertyDetails old_details = old_descriptors->GetDetails(i);
2612 if (old_details.location() == kField) {
2613 current_offset += old_details.field_width_in_words();
2615 Descriptor d(handle(old_descriptors->GetKey(i), isolate),
2616 handle(old_descriptors->GetValue(i), isolate),
2618 new_descriptors->Set(i, &d);
2621 // |root_nof| -> |target_nof|
2622 for (int i = root_nof; i < target_nof; ++i) {
2623 Handle<Name> target_key(target_descriptors->GetKey(i), isolate);
2624 PropertyDetails old_details = old_descriptors->GetDetails(i);
2625 PropertyDetails target_details = target_descriptors->GetDetails(i);
2627 PropertyKind next_kind;
2628 PropertyAttributes next_attributes;
2629 PropertyLocation next_location;
2630 Representation next_representation;
2631 bool property_kind_reconfiguration = false;
2633 if (modify_index == i) {
2634 DCHECK_EQ(FORCE_FIELD, store_mode);
2635 property_kind_reconfiguration = old_details.kind() != new_kind;
2637 next_kind = new_kind;
2638 next_attributes = new_attributes;
2639 next_location = kField;
2641 // Merge new representation/field type with ones from the target
2642 // descriptor. If property kind is not reconfigured merge the result with
2643 // representation/field type from the old descriptor.
2644 next_representation =
2645 new_representation.generalize(target_details.representation());
2646 if (!property_kind_reconfiguration) {
2647 next_representation =
2648 next_representation.generalize(old_details.representation());
2651 // Merge old_descriptor and target_descriptor entries.
2652 DCHECK_EQ(target_details.kind(), old_details.kind());
2653 next_kind = target_details.kind();
2654 next_attributes = target_details.attributes();
2656 old_details.location() == kField ||
2657 target_details.location() == kField ||
2658 !EqualImmutableValues(target_descriptors->GetValue(i),
2659 old_descriptors->GetValue(i))
2663 next_representation = old_details.representation().generalize(
2664 target_details.representation());
2666 DCHECK_EQ(next_kind, target_details.kind());
2667 DCHECK_EQ(next_attributes, target_details.attributes());
2669 if (next_location == kField) {
2670 if (next_kind == kData) {
2671 Handle<HeapType> target_field_type =
2672 GetFieldType(isolate, target_descriptors, i,
2673 target_details.location(), next_representation);
2675 Handle<HeapType> next_field_type;
2676 if (modify_index == i) {
2678 GeneralizeFieldType(target_field_type, new_field_type, isolate);
2679 if (!property_kind_reconfiguration) {
2680 Handle<HeapType> old_field_type =
2681 GetFieldType(isolate, old_descriptors, i,
2682 old_details.location(), next_representation);
2684 GeneralizeFieldType(next_field_type, old_field_type, isolate);
2687 Handle<HeapType> old_field_type =
2688 GetFieldType(isolate, old_descriptors, i, old_details.location(),
2689 next_representation);
2691 GeneralizeFieldType(target_field_type, old_field_type, isolate);
2693 Handle<Object> wrapped_type(WrapType(next_field_type));
2694 DataDescriptor d(target_key, current_offset, wrapped_type,
2695 next_attributes, next_representation);
2696 current_offset += d.GetDetails().field_width_in_words();
2697 new_descriptors->Set(i, &d);
2699 UNIMPLEMENTED(); // TODO(ishell): implement.
2702 PropertyDetails details(next_attributes, next_kind, next_location,
2703 next_representation);
2704 Descriptor d(target_key, handle(target_descriptors->GetValue(i), isolate),
2706 new_descriptors->Set(i, &d);
2710 // |target_nof| -> |old_nof|
2711 for (int i = target_nof; i < old_nof; ++i) {
2712 PropertyDetails old_details = old_descriptors->GetDetails(i);
2713 Handle<Name> old_key(old_descriptors->GetKey(i), isolate);
2715 // Merge old_descriptor entry and modified details together.
2716 PropertyKind next_kind;
2717 PropertyAttributes next_attributes;
2718 PropertyLocation next_location;
2719 Representation next_representation;
2720 bool property_kind_reconfiguration = false;
2722 if (modify_index == i) {
2723 DCHECK_EQ(FORCE_FIELD, store_mode);
2724 // In case of property kind reconfiguration it is not necessary to
2725 // take into account representation/field type of the old descriptor.
2726 property_kind_reconfiguration = old_details.kind() != new_kind;
2728 next_kind = new_kind;
2729 next_attributes = new_attributes;
2730 next_location = kField;
2731 next_representation = new_representation;
2732 if (!property_kind_reconfiguration) {
2733 next_representation =
2734 next_representation.generalize(old_details.representation());
2737 next_kind = old_details.kind();
2738 next_attributes = old_details.attributes();
2739 next_location = old_details.location();
2740 next_representation = old_details.representation();
2743 if (next_location == kField) {
2744 if (next_kind == kData) {
2745 Handle<HeapType> next_field_type;
2746 if (modify_index == i) {
2747 next_field_type = new_field_type;
2748 if (!property_kind_reconfiguration) {
2749 Handle<HeapType> old_field_type =
2750 GetFieldType(isolate, old_descriptors, i,
2751 old_details.location(), next_representation);
2753 GeneralizeFieldType(next_field_type, old_field_type, isolate);
2756 Handle<HeapType> old_field_type =
2757 GetFieldType(isolate, old_descriptors, i, old_details.location(),
2758 next_representation);
2759 next_field_type = old_field_type;
2762 Handle<Object> wrapped_type(WrapType(next_field_type));
2764 DataDescriptor d(old_key, current_offset, wrapped_type, next_attributes,
2765 next_representation);
2766 current_offset += d.GetDetails().field_width_in_words();
2767 new_descriptors->Set(i, &d);
2769 UNIMPLEMENTED(); // TODO(ishell): implement.
2772 PropertyDetails details(next_attributes, next_kind, next_location,
2773 next_representation);
2774 Descriptor d(old_key, handle(old_descriptors->GetValue(i), isolate),
2776 new_descriptors->Set(i, &d);
2780 new_descriptors->Sort();
2782 DCHECK(store_mode != FORCE_FIELD ||
2783 new_descriptors->GetDetails(modify_index).location() == kField);
2785 Handle<Map> split_map(root_map->FindLastMatchMap(
2786 root_nof, old_nof, *new_descriptors), isolate);
2787 int split_nof = split_map->NumberOfOwnDescriptors();
2788 DCHECK_NE(old_nof, split_nof);
2790 Handle<LayoutDescriptor> new_layout_descriptor =
2791 LayoutDescriptor::New(split_map, new_descriptors, old_nof);
2793 PropertyKind split_kind;
2794 PropertyAttributes split_attributes;
2795 if (modify_index == split_nof) {
2796 split_kind = new_kind;
2797 split_attributes = new_attributes;
2799 PropertyDetails split_prop_details = old_descriptors->GetDetails(split_nof);
2800 split_kind = split_prop_details.kind();
2801 split_attributes = split_prop_details.attributes();
2803 bool transition_target_deprecated = split_map->DeprecateTarget(
2804 split_kind, old_descriptors->GetKey(split_nof), split_attributes,
2805 *new_descriptors, *new_layout_descriptor);
2807 // If |transition_target_deprecated| is true then the transition array
2808 // already contains entry for given descriptor. This means that the transition
2809 // could be inserted regardless of whether transitions array is full or not.
2810 if (!transition_target_deprecated &&
2811 !TransitionArray::CanHaveMoreTransitions(split_map)) {
2812 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2813 new_kind, new_attributes,
2814 "GenAll_CantHaveMoreTransitions");
2817 old_map->NotifyLeafMapLayoutChange();
2819 if (FLAG_trace_generalization && modify_index >= 0) {
2820 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
2821 PropertyDetails new_details = new_descriptors->GetDetails(modify_index);
2822 Handle<HeapType> old_field_type =
2823 (old_details.type() == DATA)
2824 ? handle(old_descriptors->GetFieldType(modify_index), isolate)
2825 : HeapType::Constant(
2826 handle(old_descriptors->GetValue(modify_index), isolate),
2828 Handle<HeapType> new_field_type =
2829 (new_details.type() == DATA)
2830 ? handle(new_descriptors->GetFieldType(modify_index), isolate)
2831 : HeapType::Constant(
2832 handle(new_descriptors->GetValue(modify_index), isolate),
2834 old_map->PrintGeneralization(
2835 stdout, "", modify_index, split_nof, old_nof,
2836 old_details.location() == kDescriptor && store_mode == FORCE_FIELD,
2837 old_details.representation(), new_details.representation(),
2838 *old_field_type, *new_field_type);
2841 // Add missing transitions.
2842 Handle<Map> new_map = split_map;
2843 for (int i = split_nof; i < old_nof; ++i) {
2844 new_map = CopyInstallDescriptors(new_map, i, new_descriptors,
2845 new_layout_descriptor);
2847 new_map->set_owns_descriptors(true);
2852 // Generalize the representation of all DATA descriptors.
2853 Handle<Map> Map::GeneralizeAllFieldRepresentations(
2855 Handle<DescriptorArray> descriptors(map->instance_descriptors());
2856 for (int i = 0; i < map->NumberOfOwnDescriptors(); ++i) {
2857 PropertyDetails details = descriptors->GetDetails(i);
2858 if (details.type() == DATA) {
2859 map = ReconfigureProperty(map, i, kData, details.attributes(),
2860 Representation::Tagged(),
2861 HeapType::Any(map->GetIsolate()), FORCE_FIELD);
2869 MaybeHandle<Map> Map::TryUpdate(Handle<Map> old_map) {
2870 DisallowHeapAllocation no_allocation;
2871 DisallowDeoptimization no_deoptimization(old_map->GetIsolate());
2873 if (!old_map->is_deprecated()) return old_map;
2875 // Check the state of the root map.
2876 Map* root_map = old_map->FindRootMap();
2877 if (!old_map->EquivalentToForTransition(root_map)) return MaybeHandle<Map>();
2879 ElementsKind from_kind = root_map->elements_kind();
2880 ElementsKind to_kind = old_map->elements_kind();
2881 if (from_kind != to_kind) {
2882 // Try to follow existing elements kind transitions.
2883 root_map = root_map->LookupElementsTransitionMap(to_kind);
2884 if (root_map == NULL) return MaybeHandle<Map>();
2885 // From here on, use the map with correct elements kind as root map.
2887 int root_nof = root_map->NumberOfOwnDescriptors();
2889 int old_nof = old_map->NumberOfOwnDescriptors();
2890 DescriptorArray* old_descriptors = old_map->instance_descriptors();
2892 Map* new_map = root_map;
2893 for (int i = root_nof; i < old_nof; ++i) {
2894 PropertyDetails old_details = old_descriptors->GetDetails(i);
2895 Map* transition = TransitionArray::SearchTransition(
2896 new_map, old_details.kind(), old_descriptors->GetKey(i),
2897 old_details.attributes());
2898 if (transition == NULL) return MaybeHandle<Map>();
2899 new_map = transition;
2900 DescriptorArray* new_descriptors = new_map->instance_descriptors();
2902 PropertyDetails new_details = new_descriptors->GetDetails(i);
2903 DCHECK_EQ(old_details.kind(), new_details.kind());
2904 DCHECK_EQ(old_details.attributes(), new_details.attributes());
2905 if (!old_details.representation().fits_into(new_details.representation())) {
2906 return MaybeHandle<Map>();
2908 switch (new_details.type()) {
2910 HeapType* new_type = new_descriptors->GetFieldType(i);
2911 PropertyType old_property_type = old_details.type();
2912 if (old_property_type == DATA) {
2913 HeapType* old_type = old_descriptors->GetFieldType(i);
2914 if (!old_type->NowIs(new_type)) {
2915 return MaybeHandle<Map>();
2918 DCHECK(old_property_type == DATA_CONSTANT);
2919 Object* old_value = old_descriptors->GetValue(i);
2920 if (!new_type->NowContains(old_value)) {
2921 return MaybeHandle<Map>();
2928 HeapType* new_type = new_descriptors->GetFieldType(i);
2929 DCHECK(HeapType::Any()->Is(new_type));
2935 case ACCESSOR_CONSTANT: {
2936 Object* old_value = old_descriptors->GetValue(i);
2937 Object* new_value = new_descriptors->GetValue(i);
2938 if (old_details.location() == kField || old_value != new_value) {
2939 return MaybeHandle<Map>();
2945 if (new_map->NumberOfOwnDescriptors() != old_nof) return MaybeHandle<Map>();
2946 return handle(new_map);
2951 Handle<Map> Map::Update(Handle<Map> map) {
2952 if (!map->is_deprecated()) return map;
2953 return ReconfigureProperty(map, -1, kData, NONE, Representation::None(),
2954 HeapType::None(map->GetIsolate()),
2955 ALLOW_IN_DESCRIPTOR);
2959 MaybeHandle<Object> JSObject::SetPropertyWithInterceptor(LookupIterator* it,
2960 Handle<Object> value) {
2961 Isolate* isolate = it->isolate();
2962 // Make sure that the top context does not change when doing callbacks or
2963 // interceptor calls.
2964 AssertNoContextChange ncc(isolate);
2966 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
2967 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
2968 if (interceptor->setter()->IsUndefined()) return MaybeHandle<Object>();
2970 Handle<JSObject> holder = it->GetHolder<JSObject>();
2971 v8::Local<v8::Value> result;
2972 PropertyCallbackArguments args(isolate, interceptor->data(),
2973 *it->GetReceiver(), *holder);
2975 if (it->IsElement()) {
2976 uint32_t index = it->index();
2977 v8::IndexedPropertySetterCallback setter =
2978 v8::ToCData<v8::IndexedPropertySetterCallback>(interceptor->setter());
2980 ApiIndexedPropertyAccess("interceptor-indexed-set", *holder, index));
2981 result = args.Call(setter, index, v8::Utils::ToLocal(value));
2983 Handle<Name> name = it->name();
2985 if (name->IsSymbol() && !interceptor->can_intercept_symbols()) {
2986 return MaybeHandle<Object>();
2989 v8::GenericNamedPropertySetterCallback setter =
2990 v8::ToCData<v8::GenericNamedPropertySetterCallback>(
2991 interceptor->setter());
2993 ApiNamedPropertyAccess("interceptor-named-set", *holder, *name));
2995 args.Call(setter, v8::Utils::ToLocal(name), v8::Utils::ToLocal(value));
2998 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
2999 if (result.IsEmpty()) return MaybeHandle<Object>();
3001 Handle<Object> result_internal = v8::Utils::OpenHandle(*result);
3002 result_internal->VerifyApiCallResultType();
3008 MaybeHandle<Object> Object::SetProperty(Handle<Object> object,
3009 Handle<Name> name, Handle<Object> value,
3010 LanguageMode language_mode,
3011 StoreFromKeyed store_mode) {
3012 LookupIterator it(object, name);
3013 return SetProperty(&it, value, language_mode, store_mode);
3017 MaybeHandle<Object> Object::SetPropertyInternal(LookupIterator* it,
3018 Handle<Object> value,
3019 LanguageMode language_mode,
3020 StoreFromKeyed store_mode,
3022 // Make sure that the top context does not change when doing callbacks or
3023 // interceptor calls.
3024 AssertNoContextChange ncc(it->isolate());
3029 for (; it->IsFound(); it->Next()) {
3030 switch (it->state()) {
3031 case LookupIterator::NOT_FOUND:
3034 case LookupIterator::ACCESS_CHECK:
3035 if (it->HasAccess()) break;
3036 // Check whether it makes sense to reuse the lookup iterator. Here it
3037 // might still call into setters up the prototype chain.
3038 return JSObject::SetPropertyWithFailedAccessCheck(it, value);
3040 case LookupIterator::JSPROXY:
3041 if (it->HolderIsReceiverOrHiddenPrototype()) {
3042 return JSProxy::SetPropertyWithHandler(
3043 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName(), value,
3046 // TODO(verwaest): Use the MaybeHandle to indicate result.
3047 bool has_result = false;
3048 MaybeHandle<Object> maybe_result =
3049 JSProxy::SetPropertyViaPrototypesWithHandler(
3050 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName(),
3051 value, language_mode, &has_result);
3052 if (has_result) return maybe_result;
3057 case LookupIterator::INTERCEPTOR:
3058 if (it->HolderIsReceiverOrHiddenPrototype()) {
3059 MaybeHandle<Object> maybe_result =
3060 JSObject::SetPropertyWithInterceptor(it, value);
3061 if (!maybe_result.is_null()) return maybe_result;
3062 if (it->isolate()->has_pending_exception()) return maybe_result;
3064 Maybe<PropertyAttributes> maybe_attributes =
3065 JSObject::GetPropertyAttributesWithInterceptor(it);
3066 if (!maybe_attributes.IsJust()) return MaybeHandle<Object>();
3067 done = maybe_attributes.FromJust() != ABSENT;
3068 if (done && (maybe_attributes.FromJust() & READ_ONLY) != 0) {
3069 return WriteToReadOnlyProperty(it, value, language_mode);
3074 case LookupIterator::ACCESSOR: {
3075 if (it->IsReadOnly()) {
3076 return WriteToReadOnlyProperty(it, value, language_mode);
3078 Handle<Object> accessors = it->GetAccessors();
3079 if (accessors->IsAccessorInfo() &&
3080 !it->HolderIsReceiverOrHiddenPrototype() &&
3081 AccessorInfo::cast(*accessors)->is_special_data_property()) {
3085 return SetPropertyWithAccessor(it, value, language_mode);
3087 case LookupIterator::INTEGER_INDEXED_EXOTIC:
3088 // TODO(verwaest): We should throw an exception.
3091 case LookupIterator::DATA:
3092 if (it->IsReadOnly()) {
3093 return WriteToReadOnlyProperty(it, value, language_mode);
3095 if (it->HolderIsReceiverOrHiddenPrototype()) {
3096 return SetDataProperty(it, value);
3101 case LookupIterator::TRANSITION:
3109 // If the receiver is the JSGlobalObject, the store was contextual. In case
3110 // the property did not exist yet on the global object itself, we have to
3111 // throw a reference error in strict mode.
3112 if (it->GetReceiver()->IsJSGlobalObject() && is_strict(language_mode)) {
3113 THROW_NEW_ERROR(it->isolate(),
3114 NewReferenceError(MessageTemplate::kNotDefined, it->name()),
3119 return MaybeHandle<Object>();
3123 MaybeHandle<Object> Object::SetProperty(LookupIterator* it,
3124 Handle<Object> value,
3125 LanguageMode language_mode,
3126 StoreFromKeyed store_mode) {
3128 MaybeHandle<Object> result =
3129 SetPropertyInternal(it, value, language_mode, store_mode, &found);
3130 if (found) return result;
3131 return AddDataProperty(it, value, NONE, language_mode, store_mode);
3135 MaybeHandle<Object> Object::SetSuperProperty(LookupIterator* it,
3136 Handle<Object> value,
3137 LanguageMode language_mode,
3138 StoreFromKeyed store_mode) {
3140 MaybeHandle<Object> result =
3141 SetPropertyInternal(it, value, language_mode, store_mode, &found);
3142 if (found) return result;
3144 if (!it->GetReceiver()->IsJSReceiver()) {
3145 return WriteToReadOnlyProperty(it->isolate(), it->GetReceiver(),
3146 it->GetName(), value, language_mode);
3149 LookupIterator::Configuration c = LookupIterator::OWN;
3150 LookupIterator own_lookup =
3152 ? LookupIterator(it->isolate(), it->GetReceiver(), it->index(), c)
3153 : LookupIterator(it->GetReceiver(), it->name(), c);
3155 for (; own_lookup.IsFound(); own_lookup.Next()) {
3156 switch (own_lookup.state()) {
3157 case LookupIterator::ACCESS_CHECK:
3158 if (!own_lookup.HasAccess()) {
3159 return JSObject::SetPropertyWithFailedAccessCheck(&own_lookup, value);
3163 case LookupIterator::INTEGER_INDEXED_EXOTIC:
3164 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
3165 value, language_mode);
3167 case LookupIterator::DATA: {
3168 PropertyDetails details = own_lookup.property_details();
3169 if (details.IsConfigurable() || !details.IsReadOnly()) {
3170 return JSObject::DefineOwnPropertyIgnoreAttributes(
3171 &own_lookup, value, details.attributes());
3173 return WriteToReadOnlyProperty(&own_lookup, value, language_mode);
3176 case LookupIterator::ACCESSOR: {
3177 PropertyDetails details = own_lookup.property_details();
3178 if (details.IsConfigurable()) {
3179 return JSObject::DefineOwnPropertyIgnoreAttributes(
3180 &own_lookup, value, details.attributes());
3183 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
3184 value, language_mode);
3187 case LookupIterator::INTERCEPTOR:
3188 case LookupIterator::JSPROXY: {
3190 MaybeHandle<Object> result = SetPropertyInternal(
3191 &own_lookup, value, language_mode, store_mode, &found);
3192 if (found) return result;
3196 case LookupIterator::NOT_FOUND:
3197 case LookupIterator::TRANSITION:
3202 return JSObject::AddDataProperty(&own_lookup, value, NONE, language_mode,
3207 MaybeHandle<Object> Object::ReadAbsentProperty(LookupIterator* it,
3208 LanguageMode language_mode) {
3209 if (is_strong(language_mode)) {
3210 THROW_NEW_ERROR(it->isolate(),
3211 NewTypeError(MessageTemplate::kStrongPropertyAccess,
3212 it->GetName(), it->GetReceiver()),
3215 return it->isolate()->factory()->undefined_value();
3218 MaybeHandle<Object> Object::ReadAbsentProperty(Isolate* isolate,
3219 Handle<Object> receiver,
3220 Handle<Object> name,
3221 LanguageMode language_mode) {
3222 if (is_strong(language_mode)) {
3225 NewTypeError(MessageTemplate::kStrongPropertyAccess, name, receiver),
3228 return isolate->factory()->undefined_value();
3232 MaybeHandle<Object> Object::WriteToReadOnlyProperty(
3233 LookupIterator* it, Handle<Object> value, LanguageMode language_mode) {
3234 return WriteToReadOnlyProperty(it->isolate(), it->GetReceiver(),
3235 it->GetName(), value, language_mode);
3239 MaybeHandle<Object> Object::WriteToReadOnlyProperty(
3240 Isolate* isolate, Handle<Object> receiver, Handle<Object> name,
3241 Handle<Object> value, LanguageMode language_mode) {
3242 if (is_sloppy(language_mode)) return value;
3245 NewTypeError(MessageTemplate::kStrictReadOnlyProperty, name, receiver),
3250 MaybeHandle<Object> Object::RedefineNonconfigurableProperty(
3251 Isolate* isolate, Handle<Object> name, Handle<Object> value,
3252 LanguageMode language_mode) {
3253 if (is_sloppy(language_mode)) return value;
3254 THROW_NEW_ERROR(isolate,
3255 NewTypeError(MessageTemplate::kRedefineDisallowed, name),
3260 MaybeHandle<Object> Object::SetDataProperty(LookupIterator* it,
3261 Handle<Object> value) {
3262 // Proxies are handled on the WithHandler path. Other non-JSObjects cannot
3263 // have own properties.
3264 Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver());
3266 // Store on the holder which may be hidden behind the receiver.
3267 DCHECK(it->HolderIsReceiverOrHiddenPrototype());
3269 // Old value for the observation change record.
3270 // Fetch before transforming the object since the encoding may become
3271 // incompatible with what's cached in |it|.
3272 bool is_observed = receiver->map()->is_observed() &&
3274 !it->isolate()->IsInternallyUsedPropertyName(it->name()));
3275 MaybeHandle<Object> maybe_old;
3276 if (is_observed) maybe_old = it->GetDataValue();
3278 Handle<Object> to_assign = value;
3279 // Convert the incoming value to a number for storing into typed arrays.
3280 if (it->IsElement() && receiver->HasFixedTypedArrayElements()) {
3281 if (!value->IsNumber() && !value->IsUndefined()) {
3282 ASSIGN_RETURN_ON_EXCEPTION(it->isolate(), to_assign,
3283 Execution::ToNumber(it->isolate(), value),
3285 // ToNumber above might modify the receiver, causing the cached
3286 // holder_map to mismatch the actual holder->map() after this point.
3287 // Reload the map to be in consistent state. Other cached state cannot
3288 // have been invalidated since typed array elements cannot be reconfigured
3290 it->ReloadHolderMap();
3292 // We have to recheck the length. However, it can only change if the
3293 // underlying buffer was neutered, so just check that.
3294 if (Handle<JSArrayBufferView>::cast(receiver)->WasNeutered()) {
3300 // Possibly migrate to the most up-to-date map that will be able to store
3301 // |value| under it->name().
3302 it->PrepareForDataProperty(to_assign);
3304 // Write the property value.
3305 it->WriteDataValue(to_assign);
3307 // Send the change record if there are observers.
3308 if (is_observed && !value->SameValue(*maybe_old.ToHandleChecked())) {
3309 RETURN_ON_EXCEPTION(it->isolate(), JSObject::EnqueueChangeRecord(
3310 receiver, "update", it->GetName(),
3311 maybe_old.ToHandleChecked()),
3319 MUST_USE_RESULT static MaybeHandle<Object> BeginPerformSplice(
3320 Handle<JSArray> object) {
3321 Isolate* isolate = object->GetIsolate();
3322 HandleScope scope(isolate);
3323 Handle<Object> args[] = {object};
3325 return Execution::Call(
3326 isolate, Handle<JSFunction>(isolate->observers_begin_perform_splice()),
3327 isolate->factory()->undefined_value(), arraysize(args), args);
3331 MUST_USE_RESULT static MaybeHandle<Object> EndPerformSplice(
3332 Handle<JSArray> object) {
3333 Isolate* isolate = object->GetIsolate();
3334 HandleScope scope(isolate);
3335 Handle<Object> args[] = {object};
3337 return Execution::Call(
3338 isolate, Handle<JSFunction>(isolate->observers_end_perform_splice()),
3339 isolate->factory()->undefined_value(), arraysize(args), args);
3343 MUST_USE_RESULT static MaybeHandle<Object> EnqueueSpliceRecord(
3344 Handle<JSArray> object, uint32_t index, Handle<JSArray> deleted,
3345 uint32_t add_count) {
3346 Isolate* isolate = object->GetIsolate();
3347 HandleScope scope(isolate);
3348 Handle<Object> index_object = isolate->factory()->NewNumberFromUint(index);
3349 Handle<Object> add_count_object =
3350 isolate->factory()->NewNumberFromUint(add_count);
3352 Handle<Object> args[] = {object, index_object, deleted, add_count_object};
3354 return Execution::Call(
3355 isolate, Handle<JSFunction>(isolate->observers_enqueue_splice()),
3356 isolate->factory()->undefined_value(), arraysize(args), args);
3360 MaybeHandle<Object> Object::AddDataProperty(LookupIterator* it,
3361 Handle<Object> value,
3362 PropertyAttributes attributes,
3363 LanguageMode language_mode,
3364 StoreFromKeyed store_mode) {
3365 DCHECK(!it->GetReceiver()->IsJSProxy());
3366 if (!it->GetReceiver()->IsJSObject()) {
3367 // TODO(verwaest): Throw a TypeError with a more specific message.
3368 return WriteToReadOnlyProperty(it, value, language_mode);
3371 DCHECK_NE(LookupIterator::INTEGER_INDEXED_EXOTIC, it->state());
3373 Handle<JSObject> receiver = it->GetStoreTarget();
3375 // If the receiver is a JSGlobalProxy, store on the prototype (JSGlobalObject)
3376 // instead. If the prototype is Null, the proxy is detached.
3377 if (receiver->IsJSGlobalProxy()) return value;
3379 Isolate* isolate = it->isolate();
3381 if (!receiver->map()->is_extensible() &&
3382 (it->IsElement() || !isolate->IsInternallyUsedPropertyName(it->name()))) {
3383 if (is_sloppy(language_mode)) return value;
3384 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kObjectNotExtensible,
3389 if (it->IsElement()) {
3390 if (receiver->IsJSArray()) {
3391 Handle<JSArray> array = Handle<JSArray>::cast(receiver);
3392 if (JSArray::WouldChangeReadOnlyLength(array, it->index())) {
3393 if (is_sloppy(language_mode)) return value;
3394 return JSArray::ReadOnlyLengthError(array);
3397 if (FLAG_trace_external_array_abuse &&
3398 array->HasFixedTypedArrayElements()) {
3399 CheckArrayAbuse(array, "typed elements write", it->index(), true);
3402 if (FLAG_trace_js_array_abuse && !array->HasFixedTypedArrayElements()) {
3403 CheckArrayAbuse(array, "elements write", it->index(), false);
3407 MaybeHandle<Object> result =
3408 JSObject::AddDataElement(receiver, it->index(), value, attributes);
3409 JSObject::ValidateElements(receiver);
3412 // Migrate to the most up-to-date map that will be able to store |value|
3413 // under it->name() with |attributes|.
3414 it->PrepareTransitionToDataProperty(value, attributes, store_mode);
3415 DCHECK_EQ(LookupIterator::TRANSITION, it->state());
3416 it->ApplyTransitionToDataProperty();
3418 // TODO(verwaest): Encapsulate dictionary handling better.
3419 if (receiver->map()->is_dictionary_map()) {
3420 // TODO(verwaest): Probably should ensure this is done beforehand.
3421 it->InternalizeName();
3422 // TODO(dcarney): just populate TransitionPropertyCell here?
3423 JSObject::AddSlowProperty(receiver, it->name(), value, attributes);
3425 // Write the property value.
3426 it->WriteDataValue(value);
3429 // Send the change record if there are observers.
3430 if (receiver->map()->is_observed() &&
3431 !isolate->IsInternallyUsedPropertyName(it->name())) {
3432 RETURN_ON_EXCEPTION(isolate, JSObject::EnqueueChangeRecord(
3433 receiver, "add", it->name(),
3434 it->factory()->the_hole_value()),
3443 void Map::EnsureDescriptorSlack(Handle<Map> map, int slack) {
3444 // Only supports adding slack to owned descriptors.
3445 DCHECK(map->owns_descriptors());
3447 Handle<DescriptorArray> descriptors(map->instance_descriptors());
3448 int old_size = map->NumberOfOwnDescriptors();
3449 if (slack <= descriptors->NumberOfSlackDescriptors()) return;
3451 Handle<DescriptorArray> new_descriptors = DescriptorArray::CopyUpTo(
3452 descriptors, old_size, slack);
3454 DisallowHeapAllocation no_allocation;
3455 // The descriptors are still the same, so keep the layout descriptor.
3456 LayoutDescriptor* layout_descriptor = map->GetLayoutDescriptor();
3458 if (old_size == 0) {
3459 map->UpdateDescriptors(*new_descriptors, layout_descriptor);
3463 // If the source descriptors had an enum cache we copy it. This ensures
3464 // that the maps to which we push the new descriptor array back can rely
3465 // on a cache always being available once it is set. If the map has more
3466 // enumerated descriptors than available in the original cache, the cache
3467 // will be lazily replaced by the extended cache when needed.
3468 if (descriptors->HasEnumCache()) {
3469 new_descriptors->CopyEnumCacheFrom(*descriptors);
3472 // Replace descriptors by new_descriptors in all maps that share it.
3473 map->GetHeap()->incremental_marking()->RecordWrites(*descriptors);
3476 for (Object* current = map->GetBackPointer();
3477 !current->IsUndefined();
3478 current = walk_map->GetBackPointer()) {
3479 walk_map = Map::cast(current);
3480 if (walk_map->instance_descriptors() != *descriptors) break;
3481 walk_map->UpdateDescriptors(*new_descriptors, layout_descriptor);
3484 map->UpdateDescriptors(*new_descriptors, layout_descriptor);
3489 static int AppendUniqueCallbacks(NeanderArray* callbacks,
3490 Handle<typename T::Array> array,
3491 int valid_descriptors) {
3492 int nof_callbacks = callbacks->length();
3494 Isolate* isolate = array->GetIsolate();
3495 // Ensure the keys are unique names before writing them into the
3496 // instance descriptor. Since it may cause a GC, it has to be done before we
3497 // temporarily put the heap in an invalid state while appending descriptors.
3498 for (int i = 0; i < nof_callbacks; ++i) {
3499 Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i)));
3500 if (entry->name()->IsUniqueName()) continue;
3501 Handle<String> key =
3502 isolate->factory()->InternalizeString(
3503 Handle<String>(String::cast(entry->name())));
3504 entry->set_name(*key);
3507 // Fill in new callback descriptors. Process the callbacks from
3508 // back to front so that the last callback with a given name takes
3509 // precedence over previously added callbacks with that name.
3510 for (int i = nof_callbacks - 1; i >= 0; i--) {
3511 Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i)));
3512 Handle<Name> key(Name::cast(entry->name()));
3513 // Check if a descriptor with this name already exists before writing.
3514 if (!T::Contains(key, entry, valid_descriptors, array)) {
3515 T::Insert(key, entry, valid_descriptors, array);
3516 valid_descriptors++;
3520 return valid_descriptors;
3523 struct DescriptorArrayAppender {
3524 typedef DescriptorArray Array;
3525 static bool Contains(Handle<Name> key,
3526 Handle<AccessorInfo> entry,
3527 int valid_descriptors,
3528 Handle<DescriptorArray> array) {
3529 DisallowHeapAllocation no_gc;
3530 return array->Search(*key, valid_descriptors) != DescriptorArray::kNotFound;
3532 static void Insert(Handle<Name> key,
3533 Handle<AccessorInfo> entry,
3534 int valid_descriptors,
3535 Handle<DescriptorArray> array) {
3536 DisallowHeapAllocation no_gc;
3537 AccessorConstantDescriptor desc(key, entry, entry->property_attributes());
3538 array->Append(&desc);
3543 struct FixedArrayAppender {
3544 typedef FixedArray Array;
3545 static bool Contains(Handle<Name> key,
3546 Handle<AccessorInfo> entry,
3547 int valid_descriptors,
3548 Handle<FixedArray> array) {
3549 for (int i = 0; i < valid_descriptors; i++) {
3550 if (*key == AccessorInfo::cast(array->get(i))->name()) return true;
3554 static void Insert(Handle<Name> key,
3555 Handle<AccessorInfo> entry,
3556 int valid_descriptors,
3557 Handle<FixedArray> array) {
3558 DisallowHeapAllocation no_gc;
3559 array->set(valid_descriptors, *entry);
3564 void Map::AppendCallbackDescriptors(Handle<Map> map,
3565 Handle<Object> descriptors) {
3566 int nof = map->NumberOfOwnDescriptors();
3567 Handle<DescriptorArray> array(map->instance_descriptors());
3568 NeanderArray callbacks(descriptors);
3569 DCHECK(array->NumberOfSlackDescriptors() >= callbacks.length());
3570 nof = AppendUniqueCallbacks<DescriptorArrayAppender>(&callbacks, array, nof);
3571 map->SetNumberOfOwnDescriptors(nof);
3575 int AccessorInfo::AppendUnique(Handle<Object> descriptors,
3576 Handle<FixedArray> array,
3577 int valid_descriptors) {
3578 NeanderArray callbacks(descriptors);
3579 DCHECK(array->length() >= callbacks.length() + valid_descriptors);
3580 return AppendUniqueCallbacks<FixedArrayAppender>(&callbacks,
3586 static bool ContainsMap(MapHandleList* maps, Map* map) {
3587 DCHECK_NOT_NULL(map);
3588 for (int i = 0; i < maps->length(); ++i) {
3589 if (!maps->at(i).is_null() && *maps->at(i) == map) return true;
3595 Handle<Map> Map::FindTransitionedMap(Handle<Map> map,
3596 MapHandleList* candidates) {
3597 ElementsKind kind = map->elements_kind();
3598 bool packed = IsFastPackedElementsKind(kind);
3600 Map* transition = nullptr;
3601 if (IsTransitionableFastElementsKind(kind)) {
3602 for (Map* current = map->ElementsTransitionMap();
3603 current != nullptr && current->has_fast_elements();
3604 current = current->ElementsTransitionMap()) {
3605 if (ContainsMap(candidates, current) &&
3606 (packed || !IsFastPackedElementsKind(current->elements_kind()))) {
3607 transition = current;
3608 packed = packed && IsFastPackedElementsKind(current->elements_kind());
3612 return transition == nullptr ? Handle<Map>() : handle(transition);
3616 static Map* FindClosestElementsTransition(Map* map, ElementsKind to_kind) {
3617 Map* current_map = map;
3619 ElementsKind kind = map->elements_kind();
3620 while (kind != to_kind) {
3621 Map* next_map = current_map->ElementsTransitionMap();
3622 if (next_map == nullptr) return current_map;
3623 kind = next_map->elements_kind();
3624 current_map = next_map;
3627 DCHECK_EQ(to_kind, current_map->elements_kind());
3632 Map* Map::LookupElementsTransitionMap(ElementsKind to_kind) {
3633 Map* to_map = FindClosestElementsTransition(this, to_kind);
3634 if (to_map->elements_kind() == to_kind) return to_map;
3639 bool Map::IsMapInArrayPrototypeChain() {
3640 Isolate* isolate = GetIsolate();
3641 if (isolate->initial_array_prototype()->map() == this) {
3645 if (isolate->initial_object_prototype()->map() == this) {
3653 Handle<WeakCell> Map::WeakCellForMap(Handle<Map> map) {
3654 Isolate* isolate = map->GetIsolate();
3655 if (map->weak_cell_cache()->IsWeakCell()) {
3656 return Handle<WeakCell>(WeakCell::cast(map->weak_cell_cache()));
3658 Handle<WeakCell> weak_cell = isolate->factory()->NewWeakCell(map);
3659 map->set_weak_cell_cache(*weak_cell);
3664 static Handle<Map> AddMissingElementsTransitions(Handle<Map> map,
3665 ElementsKind to_kind) {
3666 DCHECK(IsTransitionElementsKind(map->elements_kind()));
3668 Handle<Map> current_map = map;
3670 ElementsKind kind = map->elements_kind();
3671 TransitionFlag flag;
3672 if (map->is_prototype_map()) {
3673 flag = OMIT_TRANSITION;
3675 flag = INSERT_TRANSITION;
3676 if (IsFastElementsKind(kind)) {
3677 while (kind != to_kind && !IsTerminalElementsKind(kind)) {
3678 kind = GetNextTransitionElementsKind(kind);
3679 current_map = Map::CopyAsElementsKind(current_map, kind, flag);
3684 // In case we are exiting the fast elements kind system, just add the map in
3686 if (kind != to_kind) {
3687 current_map = Map::CopyAsElementsKind(current_map, to_kind, flag);
3690 DCHECK(current_map->elements_kind() == to_kind);
3695 Handle<Map> Map::TransitionElementsTo(Handle<Map> map,
3696 ElementsKind to_kind) {
3697 ElementsKind from_kind = map->elements_kind();
3698 if (from_kind == to_kind) return map;
3700 Isolate* isolate = map->GetIsolate();
3701 Context* native_context = isolate->context()->native_context();
3702 if (from_kind == FAST_SLOPPY_ARGUMENTS_ELEMENTS) {
3703 if (*map == native_context->fast_aliased_arguments_map()) {
3704 DCHECK_EQ(SLOW_SLOPPY_ARGUMENTS_ELEMENTS, to_kind);
3705 return handle(native_context->slow_aliased_arguments_map());
3707 } else if (from_kind == SLOW_SLOPPY_ARGUMENTS_ELEMENTS) {
3708 if (*map == native_context->slow_aliased_arguments_map()) {
3709 DCHECK_EQ(FAST_SLOPPY_ARGUMENTS_ELEMENTS, to_kind);
3710 return handle(native_context->fast_aliased_arguments_map());
3713 Object* maybe_array_maps = map->is_strong()
3714 ? native_context->js_array_strong_maps()
3715 : native_context->js_array_maps();
3716 if (maybe_array_maps->IsFixedArray()) {
3717 DisallowHeapAllocation no_gc;
3718 FixedArray* array_maps = FixedArray::cast(maybe_array_maps);
3719 if (array_maps->get(from_kind) == *map) {
3720 Object* maybe_transitioned_map = array_maps->get(to_kind);
3721 if (maybe_transitioned_map->IsMap()) {
3722 return handle(Map::cast(maybe_transitioned_map));
3728 DCHECK(!map->IsUndefined());
3729 bool allow_store_transition = IsTransitionElementsKind(from_kind);
3730 // Only store fast element maps in ascending generality.
3731 if (IsFastElementsKind(to_kind)) {
3732 allow_store_transition =
3733 allow_store_transition && IsTransitionableFastElementsKind(from_kind) &&
3734 IsMoreGeneralElementsKindTransition(from_kind, to_kind);
3737 if (!allow_store_transition) {
3738 return Map::CopyAsElementsKind(map, to_kind, OMIT_TRANSITION);
3741 return Map::AsElementsKind(map, to_kind);
3746 Handle<Map> Map::AsElementsKind(Handle<Map> map, ElementsKind kind) {
3747 Handle<Map> closest_map(FindClosestElementsTransition(*map, kind));
3749 if (closest_map->elements_kind() == kind) {
3753 return AddMissingElementsTransitions(closest_map, kind);
3757 Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object,
3758 ElementsKind to_kind) {
3759 Handle<Map> map(object->map());
3760 return Map::TransitionElementsTo(map, to_kind);
3764 Maybe<bool> JSProxy::HasPropertyWithHandler(Handle<JSProxy> proxy,
3765 Handle<Name> name) {
3766 Isolate* isolate = proxy->GetIsolate();
3768 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3769 if (name->IsSymbol()) return Just(false);
3771 Handle<Object> args[] = { name };
3772 Handle<Object> result;
3773 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
3774 isolate, result, CallTrap(proxy, "has", isolate->derived_has_trap(),
3775 arraysize(args), args),
3778 return Just(result->BooleanValue());
3782 MaybeHandle<Object> JSProxy::SetPropertyWithHandler(
3783 Handle<JSProxy> proxy, Handle<Object> receiver, Handle<Name> name,
3784 Handle<Object> value, LanguageMode language_mode) {
3785 Isolate* isolate = proxy->GetIsolate();
3787 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3788 if (name->IsSymbol()) return value;
3790 Handle<Object> args[] = { receiver, name, value };
3791 RETURN_ON_EXCEPTION(
3795 isolate->derived_set_trap(),
3804 MaybeHandle<Object> JSProxy::SetPropertyViaPrototypesWithHandler(
3805 Handle<JSProxy> proxy, Handle<Object> receiver, Handle<Name> name,
3806 Handle<Object> value, LanguageMode language_mode, bool* done) {
3807 Isolate* isolate = proxy->GetIsolate();
3808 Handle<Object> handler(proxy->handler(), isolate); // Trap might morph proxy.
3810 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3811 if (name->IsSymbol()) {
3813 return isolate->factory()->the_hole_value();
3816 *done = true; // except where redefined...
3817 Handle<Object> args[] = { name };
3818 Handle<Object> result;
3819 ASSIGN_RETURN_ON_EXCEPTION(
3822 "getPropertyDescriptor",
3828 if (result->IsUndefined()) {
3830 return isolate->factory()->the_hole_value();
3833 // Emulate [[GetProperty]] semantics for proxies.
3834 Handle<Object> argv[] = { result };
3835 Handle<Object> desc;
3836 ASSIGN_RETURN_ON_EXCEPTION(
3838 Execution::Call(isolate,
3839 isolate->to_complete_property_descriptor(),
3845 // [[GetProperty]] requires to check that all properties are configurable.
3846 Handle<String> configurable_name =
3847 isolate->factory()->InternalizeOneByteString(
3848 STATIC_CHAR_VECTOR("configurable_"));
3849 Handle<Object> configurable =
3850 Object::GetProperty(desc, configurable_name).ToHandleChecked();
3851 DCHECK(configurable->IsBoolean());
3852 if (configurable->IsFalse()) {
3853 Handle<String> trap = isolate->factory()->InternalizeOneByteString(
3854 STATIC_CHAR_VECTOR("getPropertyDescriptor"));
3855 THROW_NEW_ERROR(isolate,
3856 NewTypeError(MessageTemplate::kProxyPropNotConfigurable,
3857 handler, name, trap),
3860 DCHECK(configurable->IsTrue());
3862 // Check for DataDescriptor.
3863 Handle<String> hasWritable_name =
3864 isolate->factory()->InternalizeOneByteString(
3865 STATIC_CHAR_VECTOR("hasWritable_"));
3866 Handle<Object> hasWritable =
3867 Object::GetProperty(desc, hasWritable_name).ToHandleChecked();
3868 DCHECK(hasWritable->IsBoolean());
3869 if (hasWritable->IsTrue()) {
3870 Handle<String> writable_name = isolate->factory()->InternalizeOneByteString(
3871 STATIC_CHAR_VECTOR("writable_"));
3872 Handle<Object> writable =
3873 Object::GetProperty(desc, writable_name).ToHandleChecked();
3874 DCHECK(writable->IsBoolean());
3875 *done = writable->IsFalse();
3876 if (!*done) return isolate->factory()->the_hole_value();
3877 return WriteToReadOnlyProperty(isolate, receiver, name, value,
3881 // We have an AccessorDescriptor.
3882 Handle<String> set_name =
3883 isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("set_"));
3884 Handle<Object> setter = Object::GetProperty(desc, set_name).ToHandleChecked();
3885 if (!setter->IsUndefined()) {
3886 // TODO(rossberg): nicer would be to cast to some JSCallable here...
3887 return SetPropertyWithDefinedSetter(
3888 receiver, Handle<JSReceiver>::cast(setter), value);
3891 if (is_sloppy(language_mode)) return value;
3893 isolate, NewTypeError(MessageTemplate::kNoSetterInCallback, name, proxy),
3898 MaybeHandle<Object> JSProxy::DeletePropertyWithHandler(
3899 Handle<JSProxy> proxy, Handle<Name> name, LanguageMode language_mode) {
3900 Isolate* isolate = proxy->GetIsolate();
3902 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3903 if (name->IsSymbol()) return isolate->factory()->false_value();
3905 Handle<Object> args[] = { name };
3906 Handle<Object> result;
3907 ASSIGN_RETURN_ON_EXCEPTION(
3916 bool result_bool = result->BooleanValue();
3917 if (is_strict(language_mode) && !result_bool) {
3918 Handle<Object> handler(proxy->handler(), isolate);
3921 NewTypeError(MessageTemplate::kProxyHandlerDeleteFailed, handler),
3924 return isolate->factory()->ToBoolean(result_bool);
3928 Maybe<PropertyAttributes> JSProxy::GetPropertyAttributesWithHandler(
3929 Handle<JSProxy> proxy, Handle<Object> receiver, Handle<Name> name) {
3930 Isolate* isolate = proxy->GetIsolate();
3931 HandleScope scope(isolate);
3933 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3934 if (name->IsSymbol()) return Just(ABSENT);
3936 Handle<Object> args[] = { name };
3937 Handle<Object> result;
3938 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
3939 isolate, result, proxy->CallTrap(proxy, "getPropertyDescriptor",
3940 Handle<Object>(), arraysize(args), args),
3941 Nothing<PropertyAttributes>());
3943 if (result->IsUndefined()) return Just(ABSENT);
3945 Handle<Object> argv[] = { result };
3946 Handle<Object> desc;
3947 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
3949 Execution::Call(isolate, isolate->to_complete_property_descriptor(),
3950 result, arraysize(argv), argv),
3951 Nothing<PropertyAttributes>());
3953 // Convert result to PropertyAttributes.
3954 Handle<String> enum_n = isolate->factory()->InternalizeOneByteString(
3955 STATIC_CHAR_VECTOR("enumerable_"));
3956 Handle<Object> enumerable;
3957 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, enumerable,
3958 Object::GetProperty(desc, enum_n),
3959 Nothing<PropertyAttributes>());
3960 Handle<String> conf_n = isolate->factory()->InternalizeOneByteString(
3961 STATIC_CHAR_VECTOR("configurable_"));
3962 Handle<Object> configurable;
3963 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, configurable,
3964 Object::GetProperty(desc, conf_n),
3965 Nothing<PropertyAttributes>());
3966 Handle<String> writ_n = isolate->factory()->InternalizeOneByteString(
3967 STATIC_CHAR_VECTOR("writable_"));
3968 Handle<Object> writable;
3969 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, writable,
3970 Object::GetProperty(desc, writ_n),
3971 Nothing<PropertyAttributes>());
3972 if (!writable->BooleanValue()) {
3973 Handle<String> set_n = isolate->factory()->InternalizeOneByteString(
3974 STATIC_CHAR_VECTOR("set_"));
3975 Handle<Object> setter;
3976 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, setter,
3977 Object::GetProperty(desc, set_n),
3978 Nothing<PropertyAttributes>());
3979 writable = isolate->factory()->ToBoolean(!setter->IsUndefined());
3982 if (configurable->IsFalse()) {
3983 Handle<Object> handler(proxy->handler(), isolate);
3984 Handle<String> trap = isolate->factory()->InternalizeOneByteString(
3985 STATIC_CHAR_VECTOR("getPropertyDescriptor"));
3986 Handle<Object> error = isolate->factory()->NewTypeError(
3987 MessageTemplate::kProxyPropNotConfigurable, handler, name, trap);
3988 isolate->Throw(*error);
3989 return Nothing<PropertyAttributes>();
3992 int attributes = NONE;
3993 if (!enumerable->BooleanValue()) attributes |= DONT_ENUM;
3994 if (!configurable->BooleanValue()) attributes |= DONT_DELETE;
3995 if (!writable->BooleanValue()) attributes |= READ_ONLY;
3996 return Just(static_cast<PropertyAttributes>(attributes));
4000 void JSProxy::Fix(Handle<JSProxy> proxy) {
4001 Isolate* isolate = proxy->GetIsolate();
4003 // Save identity hash.
4004 Handle<Object> hash(proxy->GetIdentityHash(), isolate);
4006 if (proxy->IsJSFunctionProxy()) {
4007 isolate->factory()->BecomeJSFunction(proxy);
4008 // Code will be set on the JavaScript side.
4010 isolate->factory()->BecomeJSObject(proxy);
4012 DCHECK(proxy->IsJSObject());
4014 // Inherit identity, if it was present.
4015 if (hash->IsSmi()) {
4016 JSObject::SetIdentityHash(Handle<JSObject>::cast(proxy),
4017 Handle<Smi>::cast(hash));
4022 MaybeHandle<Object> JSProxy::CallTrap(Handle<JSProxy> proxy,
4024 Handle<Object> derived,
4026 Handle<Object> argv[]) {
4027 Isolate* isolate = proxy->GetIsolate();
4028 Handle<Object> handler(proxy->handler(), isolate);
4030 Handle<String> trap_name = isolate->factory()->InternalizeUtf8String(name);
4031 Handle<Object> trap;
4032 ASSIGN_RETURN_ON_EXCEPTION(
4034 Object::GetPropertyOrElement(handler, trap_name),
4037 if (trap->IsUndefined()) {
4038 if (derived.is_null()) {
4039 THROW_NEW_ERROR(isolate,
4040 NewTypeError(MessageTemplate::kProxyHandlerTrapMissing,
4041 handler, trap_name),
4044 trap = Handle<Object>(derived);
4047 return Execution::Call(isolate, trap, handler, argc, argv);
4051 void JSObject::AllocateStorageForMap(Handle<JSObject> object, Handle<Map> map) {
4052 DCHECK(object->map()->inobject_properties() == map->inobject_properties());
4053 ElementsKind obj_kind = object->map()->elements_kind();
4054 ElementsKind map_kind = map->elements_kind();
4055 if (map_kind != obj_kind) {
4056 ElementsKind to_kind = map_kind;
4057 if (IsMoreGeneralElementsKindTransition(map_kind, obj_kind) ||
4058 IsDictionaryElementsKind(obj_kind)) {
4061 if (IsDictionaryElementsKind(to_kind)) {
4062 NormalizeElements(object);
4064 TransitionElementsKind(object, to_kind);
4066 map = Map::AsElementsKind(map, to_kind);
4068 JSObject::MigrateToMap(object, map);
4072 void JSObject::MigrateInstance(Handle<JSObject> object) {
4073 Handle<Map> original_map(object->map());
4074 Handle<Map> map = Map::Update(original_map);
4075 map->set_migration_target(true);
4076 MigrateToMap(object, map);
4077 if (FLAG_trace_migration) {
4078 object->PrintInstanceMigration(stdout, *original_map, *map);
4084 bool JSObject::TryMigrateInstance(Handle<JSObject> object) {
4085 Isolate* isolate = object->GetIsolate();
4086 DisallowDeoptimization no_deoptimization(isolate);
4087 Handle<Map> original_map(object->map(), isolate);
4088 Handle<Map> new_map;
4089 if (!Map::TryUpdate(original_map).ToHandle(&new_map)) {
4092 JSObject::MigrateToMap(object, new_map);
4093 if (FLAG_trace_migration) {
4094 object->PrintInstanceMigration(stdout, *original_map, object->map());
4100 void JSObject::AddProperty(Handle<JSObject> object, Handle<Name> name,
4101 Handle<Object> value,
4102 PropertyAttributes attributes) {
4103 LookupIterator it(object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
4104 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
4107 DCHECK(!object->IsJSProxy());
4108 DCHECK(!name->AsArrayIndex(&index));
4109 Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it);
4110 DCHECK(maybe.IsJust());
4111 DCHECK(!it.IsFound());
4112 DCHECK(object->map()->is_extensible() ||
4113 it.isolate()->IsInternallyUsedPropertyName(name));
4115 AddDataProperty(&it, value, attributes, STRICT,
4116 CERTAINLY_NOT_STORE_FROM_KEYED).Check();
4121 void ExecutableAccessorInfo::ClearSetter(Handle<ExecutableAccessorInfo> info) {
4122 Handle<Object> object = v8::FromCData(info->GetIsolate(), nullptr);
4123 info->set_setter(*object);
4127 // Reconfigures a property to a data property with attributes, even if it is not
4129 // Requires a LookupIterator that does not look at the prototype chain beyond
4130 // hidden prototypes.
4131 MaybeHandle<Object> JSObject::DefineOwnPropertyIgnoreAttributes(
4132 LookupIterator* it, Handle<Object> value, PropertyAttributes attributes,
4133 ExecutableAccessorInfoHandling handling) {
4134 Handle<JSObject> object = Handle<JSObject>::cast(it->GetReceiver());
4135 bool is_observed = object->map()->is_observed() &&
4137 !it->isolate()->IsInternallyUsedPropertyName(it->name()));
4139 for (; it->IsFound(); it->Next()) {
4140 switch (it->state()) {
4141 case LookupIterator::JSPROXY:
4142 case LookupIterator::NOT_FOUND:
4143 case LookupIterator::TRANSITION:
4146 case LookupIterator::ACCESS_CHECK:
4147 if (!it->HasAccess()) {
4148 it->isolate()->ReportFailedAccessCheck(it->GetHolder<JSObject>());
4149 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
4154 // If there's an interceptor, try to store the property with the
4156 // In case of success, the attributes will have been reset to the default
4157 // attributes of the interceptor, rather than the incoming attributes.
4159 // TODO(verwaest): JSProxy afterwards verify the attributes that the
4160 // JSProxy claims it has, and verifies that they are compatible. If not,
4161 // they throw. Here we should do the same.
4162 case LookupIterator::INTERCEPTOR:
4163 if (handling == DONT_FORCE_FIELD) {
4164 MaybeHandle<Object> maybe_result =
4165 JSObject::SetPropertyWithInterceptor(it, value);
4166 if (!maybe_result.is_null()) return maybe_result;
4167 if (it->isolate()->has_pending_exception()) return maybe_result;
4171 case LookupIterator::ACCESSOR: {
4172 Handle<Object> accessors = it->GetAccessors();
4174 // Special handling for ExecutableAccessorInfo, which behaves like a
4176 if (accessors->IsExecutableAccessorInfo() &&
4177 handling == DONT_FORCE_FIELD) {
4178 PropertyDetails details = it->property_details();
4179 // Ensure the context isn't changed after calling into accessors.
4180 AssertNoContextChange ncc(it->isolate());
4182 Handle<Object> result;
4183 ASSIGN_RETURN_ON_EXCEPTION(
4184 it->isolate(), result,
4185 JSObject::SetPropertyWithAccessor(it, value, STRICT), Object);
4186 DCHECK(result->SameValue(*value));
4188 if (details.attributes() == attributes) return value;
4190 // Reconfigure the accessor if attributes mismatch.
4191 Handle<ExecutableAccessorInfo> new_data = Accessors::CloneAccessor(
4192 it->isolate(), Handle<ExecutableAccessorInfo>::cast(accessors));
4193 new_data->set_property_attributes(attributes);
4194 // By clearing the setter we don't have to introduce a lookup to
4195 // the setter, simply make it unavailable to reflect the
4197 if (attributes & READ_ONLY) {
4198 ExecutableAccessorInfo::ClearSetter(new_data);
4201 it->TransitionToAccessorPair(new_data, attributes);
4203 it->ReconfigureDataProperty(value, attributes);
4204 it->WriteDataValue(value);
4208 RETURN_ON_EXCEPTION(
4210 EnqueueChangeRecord(object, "reconfigure", it->GetName(),
4211 it->factory()->the_hole_value()),
4217 case LookupIterator::INTEGER_INDEXED_EXOTIC:
4218 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
4221 case LookupIterator::DATA: {
4222 PropertyDetails details = it->property_details();
4223 Handle<Object> old_value = it->factory()->the_hole_value();
4224 // Regular property update if the attributes match.
4225 if (details.attributes() == attributes) {
4226 return SetDataProperty(it, value);
4229 // Special case: properties of typed arrays cannot be reconfigured to
4230 // non-writable nor to non-enumerable.
4231 if (it->IsElement() && object->HasFixedTypedArrayElements()) {
4232 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
4236 // Reconfigure the data property if the attributes mismatch.
4237 if (is_observed) old_value = it->GetDataValue();
4239 it->ReconfigureDataProperty(value, attributes);
4240 it->WriteDataValue(value);
4243 if (old_value->SameValue(*value)) {
4244 old_value = it->factory()->the_hole_value();
4246 RETURN_ON_EXCEPTION(it->isolate(),
4247 EnqueueChangeRecord(object, "reconfigure",
4248 it->GetName(), old_value),
4256 return AddDataProperty(it, value, attributes, STRICT,
4257 CERTAINLY_NOT_STORE_FROM_KEYED);
4261 MaybeHandle<Object> JSObject::SetOwnPropertyIgnoreAttributes(
4262 Handle<JSObject> object, Handle<Name> name, Handle<Object> value,
4263 PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) {
4264 DCHECK(!value->IsTheHole());
4265 LookupIterator it(object, name, LookupIterator::OWN);
4266 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling);
4270 MaybeHandle<Object> JSObject::SetOwnElementIgnoreAttributes(
4271 Handle<JSObject> object, uint32_t index, Handle<Object> value,
4272 PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) {
4273 Isolate* isolate = object->GetIsolate();
4274 LookupIterator it(isolate, object, index, LookupIterator::OWN);
4275 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling);
4279 MaybeHandle<Object> JSObject::DefinePropertyOrElementIgnoreAttributes(
4280 Handle<JSObject> object, Handle<Name> name, Handle<Object> value,
4281 PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) {
4282 Isolate* isolate = object->GetIsolate();
4283 LookupIterator it = LookupIterator::PropertyOrElement(isolate, object, name,
4284 LookupIterator::OWN);
4285 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling);
4289 Maybe<bool> JSObject::CreateDataProperty(LookupIterator* it,
4290 Handle<Object> value) {
4291 DCHECK(it->GetReceiver()->IsJSObject());
4292 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(it);
4293 if (maybe.IsNothing()) return Nothing<bool>();
4295 if (it->IsFound()) {
4296 if (!it->IsConfigurable()) return Just(false);
4298 if (!JSObject::cast(*it->GetReceiver())->IsExtensible()) return Just(false);
4301 RETURN_ON_EXCEPTION_VALUE(
4303 DefineOwnPropertyIgnoreAttributes(it, value, NONE, DONT_FORCE_FIELD),
4310 Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithInterceptor(
4311 LookupIterator* it) {
4312 Isolate* isolate = it->isolate();
4313 // Make sure that the top context does not change when doing
4314 // callbacks or interceptor calls.
4315 AssertNoContextChange ncc(isolate);
4316 HandleScope scope(isolate);
4318 Handle<JSObject> holder = it->GetHolder<JSObject>();
4319 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
4320 if (!it->IsElement() && it->name()->IsSymbol() &&
4321 !interceptor->can_intercept_symbols()) {
4322 return Just(ABSENT);
4324 PropertyCallbackArguments args(isolate, interceptor->data(),
4325 *it->GetReceiver(), *holder);
4326 if (!interceptor->query()->IsUndefined()) {
4327 v8::Local<v8::Integer> result;
4328 if (it->IsElement()) {
4329 uint32_t index = it->index();
4330 v8::IndexedPropertyQueryCallback query =
4331 v8::ToCData<v8::IndexedPropertyQueryCallback>(interceptor->query());
4333 ApiIndexedPropertyAccess("interceptor-indexed-has", *holder, index));
4334 result = args.Call(query, index);
4336 Handle<Name> name = it->name();
4337 v8::GenericNamedPropertyQueryCallback query =
4338 v8::ToCData<v8::GenericNamedPropertyQueryCallback>(
4339 interceptor->query());
4341 ApiNamedPropertyAccess("interceptor-named-has", *holder, *name));
4342 result = args.Call(query, v8::Utils::ToLocal(name));
4344 if (!result.IsEmpty()) {
4345 DCHECK(result->IsInt32());
4346 return Just(static_cast<PropertyAttributes>(
4347 result->Int32Value(reinterpret_cast<v8::Isolate*>(isolate)
4348 ->GetCurrentContext()).FromJust()));
4350 } else if (!interceptor->getter()->IsUndefined()) {
4351 // TODO(verwaest): Use GetPropertyWithInterceptor?
4352 v8::Local<v8::Value> result;
4353 if (it->IsElement()) {
4354 uint32_t index = it->index();
4355 v8::IndexedPropertyGetterCallback getter =
4356 v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter());
4357 LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-get-has",
4359 result = args.Call(getter, index);
4361 Handle<Name> name = it->name();
4363 v8::GenericNamedPropertyGetterCallback getter =
4364 v8::ToCData<v8::GenericNamedPropertyGetterCallback>(
4365 interceptor->getter());
4367 ApiNamedPropertyAccess("interceptor-named-get-has", *holder, *name));
4368 result = args.Call(getter, v8::Utils::ToLocal(name));
4370 if (!result.IsEmpty()) return Just(DONT_ENUM);
4373 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<PropertyAttributes>());
4374 return Just(ABSENT);
4378 Maybe<PropertyAttributes> JSReceiver::GetPropertyAttributes(
4379 LookupIterator* it) {
4380 for (; it->IsFound(); it->Next()) {
4381 switch (it->state()) {
4382 case LookupIterator::NOT_FOUND:
4383 case LookupIterator::TRANSITION:
4385 case LookupIterator::JSPROXY:
4386 return JSProxy::GetPropertyAttributesWithHandler(
4387 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName());
4388 case LookupIterator::INTERCEPTOR: {
4389 Maybe<PropertyAttributes> result =
4390 JSObject::GetPropertyAttributesWithInterceptor(it);
4391 if (!result.IsJust()) return result;
4392 if (result.FromJust() != ABSENT) return result;
4395 case LookupIterator::ACCESS_CHECK:
4396 if (it->HasAccess()) break;
4397 return JSObject::GetPropertyAttributesWithFailedAccessCheck(it);
4398 case LookupIterator::INTEGER_INDEXED_EXOTIC:
4399 return Just(ABSENT);
4400 case LookupIterator::ACCESSOR:
4401 case LookupIterator::DATA:
4402 return Just(it->property_details().attributes());
4405 return Just(ABSENT);
4409 Handle<NormalizedMapCache> NormalizedMapCache::New(Isolate* isolate) {
4410 Handle<FixedArray> array(
4411 isolate->factory()->NewFixedArray(kEntries, TENURED));
4412 return Handle<NormalizedMapCache>::cast(array);
4416 MaybeHandle<Map> NormalizedMapCache::Get(Handle<Map> fast_map,
4417 PropertyNormalizationMode mode) {
4418 DisallowHeapAllocation no_gc;
4419 Object* value = FixedArray::get(GetIndex(fast_map));
4420 if (!value->IsMap() ||
4421 !Map::cast(value)->EquivalentToForNormalization(*fast_map, mode)) {
4422 return MaybeHandle<Map>();
4424 return handle(Map::cast(value));
4428 void NormalizedMapCache::Set(Handle<Map> fast_map,
4429 Handle<Map> normalized_map) {
4430 DisallowHeapAllocation no_gc;
4431 DCHECK(normalized_map->is_dictionary_map());
4432 FixedArray::set(GetIndex(fast_map), *normalized_map);
4436 void NormalizedMapCache::Clear() {
4437 int entries = length();
4438 for (int i = 0; i != entries; i++) {
4444 void HeapObject::UpdateMapCodeCache(Handle<HeapObject> object,
4446 Handle<Code> code) {
4447 Handle<Map> map(object->map());
4448 Map::UpdateCodeCache(map, name, code);
4452 void JSObject::NormalizeProperties(Handle<JSObject> object,
4453 PropertyNormalizationMode mode,
4454 int expected_additional_properties,
4455 const char* reason) {
4456 if (!object->HasFastProperties()) return;
4458 Handle<Map> map(object->map());
4459 Handle<Map> new_map = Map::Normalize(map, mode, reason);
4461 MigrateToMap(object, new_map, expected_additional_properties);
4465 void JSObject::MigrateFastToSlow(Handle<JSObject> object,
4466 Handle<Map> new_map,
4467 int expected_additional_properties) {
4468 // The global object is always normalized.
4469 DCHECK(!object->IsGlobalObject());
4470 // JSGlobalProxy must never be normalized
4471 DCHECK(!object->IsJSGlobalProxy());
4473 Isolate* isolate = object->GetIsolate();
4474 HandleScope scope(isolate);
4475 Handle<Map> map(object->map());
4477 // Allocate new content.
4478 int real_size = map->NumberOfOwnDescriptors();
4479 int property_count = real_size;
4480 if (expected_additional_properties > 0) {
4481 property_count += expected_additional_properties;
4483 property_count += 2; // Make space for two more properties.
4485 Handle<NameDictionary> dictionary =
4486 NameDictionary::New(isolate, property_count);
4488 Handle<DescriptorArray> descs(map->instance_descriptors());
4489 for (int i = 0; i < real_size; i++) {
4490 PropertyDetails details = descs->GetDetails(i);
4491 Handle<Name> key(descs->GetKey(i));
4492 switch (details.type()) {
4493 case DATA_CONSTANT: {
4494 Handle<Object> value(descs->GetConstant(i), isolate);
4495 PropertyDetails d(details.attributes(), DATA, i + 1,
4496 PropertyCellType::kNoCell);
4497 dictionary = NameDictionary::Add(dictionary, key, value, d);
4501 FieldIndex index = FieldIndex::ForDescriptor(*map, i);
4502 Handle<Object> value;
4503 if (object->IsUnboxedDoubleField(index)) {
4504 double old_value = object->RawFastDoublePropertyAt(index);
4505 value = isolate->factory()->NewHeapNumber(old_value);
4507 value = handle(object->RawFastPropertyAt(index), isolate);
4508 if (details.representation().IsDouble()) {
4509 DCHECK(value->IsMutableHeapNumber());
4510 Handle<HeapNumber> old = Handle<HeapNumber>::cast(value);
4511 value = isolate->factory()->NewHeapNumber(old->value());
4514 PropertyDetails d(details.attributes(), DATA, i + 1,
4515 PropertyCellType::kNoCell);
4516 dictionary = NameDictionary::Add(dictionary, key, value, d);
4520 FieldIndex index = FieldIndex::ForDescriptor(*map, i);
4521 Handle<Object> value(object->RawFastPropertyAt(index), isolate);
4522 PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
4523 PropertyCellType::kNoCell);
4524 dictionary = NameDictionary::Add(dictionary, key, value, d);
4527 case ACCESSOR_CONSTANT: {
4528 Handle<Object> value(descs->GetCallbacksObject(i), isolate);
4529 PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
4530 PropertyCellType::kNoCell);
4531 dictionary = NameDictionary::Add(dictionary, key, value, d);
4537 // Copy the next enumeration index from instance descriptor.
4538 dictionary->SetNextEnumerationIndex(real_size + 1);
4540 // From here on we cannot fail and we shouldn't GC anymore.
4541 DisallowHeapAllocation no_allocation;
4543 // Resize the object in the heap if necessary.
4544 int new_instance_size = new_map->instance_size();
4545 int instance_size_delta = map->instance_size() - new_instance_size;
4546 DCHECK(instance_size_delta >= 0);
4548 if (instance_size_delta > 0) {
4549 Heap* heap = isolate->heap();
4550 heap->CreateFillerObjectAt(object->address() + new_instance_size,
4551 instance_size_delta);
4552 heap->AdjustLiveBytes(*object, -instance_size_delta,
4553 Heap::CONCURRENT_TO_SWEEPER);
4556 // We are storing the new map using release store after creating a filler for
4557 // the left-over space to avoid races with the sweeper thread.
4558 object->synchronized_set_map(*new_map);
4560 object->set_properties(*dictionary);
4562 // Ensure that in-object space of slow-mode object does not contain random
4564 int inobject_properties = new_map->inobject_properties();
4565 for (int i = 0; i < inobject_properties; i++) {
4566 FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i);
4567 object->RawFastPropertyAtPut(index, Smi::FromInt(0));
4570 isolate->counters()->props_to_dictionary()->Increment();
4573 if (FLAG_trace_normalization) {
4574 OFStream os(stdout);
4575 os << "Object properties have been normalized:\n";
4582 void JSObject::MigrateSlowToFast(Handle<JSObject> object,
4583 int unused_property_fields,
4584 const char* reason) {
4585 if (object->HasFastProperties()) return;
4586 DCHECK(!object->IsGlobalObject());
4587 Isolate* isolate = object->GetIsolate();
4588 Factory* factory = isolate->factory();
4589 Handle<NameDictionary> dictionary(object->property_dictionary());
4591 // Make sure we preserve dictionary representation if there are too many
4593 int number_of_elements = dictionary->NumberOfElements();
4594 if (number_of_elements > kMaxNumberOfDescriptors) return;
4596 Handle<FixedArray> iteration_order;
4597 if (number_of_elements != dictionary->NextEnumerationIndex()) {
4599 NameDictionary::DoGenerateNewEnumerationIndices(dictionary);
4601 iteration_order = NameDictionary::BuildIterationIndicesArray(dictionary);
4604 int instance_descriptor_length = iteration_order->length();
4605 int number_of_fields = 0;
4607 // Compute the length of the instance descriptor.
4608 for (int i = 0; i < instance_descriptor_length; i++) {
4609 int index = Smi::cast(iteration_order->get(i))->value();
4610 DCHECK(dictionary->IsKey(dictionary->KeyAt(index)));
4612 Object* value = dictionary->ValueAt(index);
4613 PropertyType type = dictionary->DetailsAt(index).type();
4614 if (type == DATA && !value->IsJSFunction()) {
4615 number_of_fields += 1;
4619 Handle<Map> old_map(object->map(), isolate);
4621 int inobject_props = old_map->inobject_properties();
4623 // Allocate new map.
4624 Handle<Map> new_map = Map::CopyDropDescriptors(old_map);
4625 new_map->set_dictionary_map(false);
4627 UpdatePrototypeUserRegistration(old_map, new_map, isolate);
4630 if (FLAG_trace_maps) {
4631 PrintF("[TraceMaps: SlowToFast from= %p to= %p reason= %s ]\n",
4632 reinterpret_cast<void*>(*old_map), reinterpret_cast<void*>(*new_map),
4637 if (instance_descriptor_length == 0) {
4638 DisallowHeapAllocation no_gc;
4639 DCHECK_LE(unused_property_fields, inobject_props);
4640 // Transform the object.
4641 new_map->set_unused_property_fields(inobject_props);
4642 object->synchronized_set_map(*new_map);
4643 object->set_properties(isolate->heap()->empty_fixed_array());
4644 // Check that it really works.
4645 DCHECK(object->HasFastProperties());
4649 // Allocate the instance descriptor.
4650 Handle<DescriptorArray> descriptors = DescriptorArray::Allocate(
4651 isolate, instance_descriptor_length);
4653 int number_of_allocated_fields =
4654 number_of_fields + unused_property_fields - inobject_props;
4655 if (number_of_allocated_fields < 0) {
4656 // There is enough inobject space for all fields (including unused).
4657 number_of_allocated_fields = 0;
4658 unused_property_fields = inobject_props - number_of_fields;
4661 // Allocate the fixed array for the fields.
4662 Handle<FixedArray> fields = factory->NewFixedArray(
4663 number_of_allocated_fields);
4665 // Fill in the instance descriptor and the fields.
4666 int current_offset = 0;
4667 for (int i = 0; i < instance_descriptor_length; i++) {
4668 int index = Smi::cast(iteration_order->get(i))->value();
4669 Object* k = dictionary->KeyAt(index);
4670 DCHECK(dictionary->IsKey(k));
4672 Object* value = dictionary->ValueAt(index);
4674 if (k->IsSymbol()) {
4675 key = handle(Symbol::cast(k));
4677 // Ensure the key is a unique name before writing into the
4678 // instance descriptor.
4679 key = factory->InternalizeString(handle(String::cast(k)));
4682 PropertyDetails details = dictionary->DetailsAt(index);
4683 int enumeration_index = details.dictionary_index();
4684 PropertyType type = details.type();
4686 if (value->IsJSFunction()) {
4687 DataConstantDescriptor d(key, handle(value, isolate),
4688 details.attributes());
4689 descriptors->Set(enumeration_index - 1, &d);
4690 } else if (type == DATA) {
4691 if (current_offset < inobject_props) {
4692 object->InObjectPropertyAtPut(current_offset, value,
4693 UPDATE_WRITE_BARRIER);
4695 int offset = current_offset - inobject_props;
4696 fields->set(offset, value);
4698 DataDescriptor d(key, current_offset, details.attributes(),
4699 // TODO(verwaest): value->OptimalRepresentation();
4700 Representation::Tagged());
4701 current_offset += d.GetDetails().field_width_in_words();
4702 descriptors->Set(enumeration_index - 1, &d);
4703 } else if (type == ACCESSOR_CONSTANT) {
4704 AccessorConstantDescriptor d(key, handle(value, isolate),
4705 details.attributes());
4706 descriptors->Set(enumeration_index - 1, &d);
4711 DCHECK(current_offset == number_of_fields);
4713 descriptors->Sort();
4715 Handle<LayoutDescriptor> layout_descriptor = LayoutDescriptor::New(
4716 new_map, descriptors, descriptors->number_of_descriptors());
4718 DisallowHeapAllocation no_gc;
4719 new_map->InitializeDescriptors(*descriptors, *layout_descriptor);
4720 new_map->set_unused_property_fields(unused_property_fields);
4722 // Transform the object.
4723 object->synchronized_set_map(*new_map);
4725 object->set_properties(*fields);
4726 DCHECK(object->IsJSObject());
4728 // Check that it really works.
4729 DCHECK(object->HasFastProperties());
4733 void JSObject::ResetElements(Handle<JSObject> object) {
4734 Isolate* isolate = object->GetIsolate();
4735 CHECK(object->map() != isolate->heap()->sloppy_arguments_elements_map());
4736 if (object->map()->has_dictionary_elements()) {
4737 Handle<SeededNumberDictionary> new_elements =
4738 SeededNumberDictionary::New(isolate, 0);
4739 object->set_elements(*new_elements);
4741 object->set_elements(object->map()->GetInitialElements());
4746 static Handle<SeededNumberDictionary> CopyFastElementsToDictionary(
4747 Handle<FixedArrayBase> array, int length,
4748 Handle<SeededNumberDictionary> dictionary, bool used_as_prototype) {
4749 Isolate* isolate = array->GetIsolate();
4750 Factory* factory = isolate->factory();
4751 bool has_double_elements = array->IsFixedDoubleArray();
4752 for (int i = 0; i < length; i++) {
4753 Handle<Object> value;
4754 if (has_double_elements) {
4755 Handle<FixedDoubleArray> double_array =
4756 Handle<FixedDoubleArray>::cast(array);
4757 if (double_array->is_the_hole(i)) {
4758 value = factory->the_hole_value();
4760 value = factory->NewHeapNumber(double_array->get_scalar(i));
4763 value = handle(Handle<FixedArray>::cast(array)->get(i), isolate);
4765 if (!value->IsTheHole()) {
4766 PropertyDetails details = PropertyDetails::Empty();
4767 dictionary = SeededNumberDictionary::AddNumberEntry(
4768 dictionary, i, value, details, used_as_prototype);
4775 void JSObject::RequireSlowElements(SeededNumberDictionary* dictionary) {
4776 if (dictionary->requires_slow_elements()) return;
4777 dictionary->set_requires_slow_elements();
4778 // TODO(verwaest): Remove this hack.
4779 if (map()->is_prototype_map()) {
4780 GetHeap()->ClearAllICsByKind(Code::KEYED_STORE_IC);
4785 Handle<SeededNumberDictionary> JSObject::GetNormalizedElementDictionary(
4786 Handle<JSObject> object, Handle<FixedArrayBase> elements) {
4787 DCHECK(!object->HasDictionaryElements());
4788 DCHECK(!object->HasSlowArgumentsElements());
4789 Isolate* isolate = object->GetIsolate();
4790 // Ensure that notifications fire if the array or object prototypes are
4792 isolate->UpdateArrayProtectorOnNormalizeElements(object);
4793 int length = object->IsJSArray()
4794 ? Smi::cast(Handle<JSArray>::cast(object)->length())->value()
4795 : elements->length();
4796 int used = object->GetFastElementsUsage();
4797 Handle<SeededNumberDictionary> dictionary =
4798 SeededNumberDictionary::New(isolate, used);
4799 return CopyFastElementsToDictionary(elements, length, dictionary,
4800 object->map()->is_prototype_map());
4804 Handle<SeededNumberDictionary> JSObject::NormalizeElements(
4805 Handle<JSObject> object) {
4806 DCHECK(!object->HasFixedTypedArrayElements());
4807 Isolate* isolate = object->GetIsolate();
4809 // Find the backing store.
4810 Handle<FixedArrayBase> elements(object->elements(), isolate);
4811 bool is_arguments = object->HasSloppyArgumentsElements();
4813 FixedArray* parameter_map = FixedArray::cast(*elements);
4814 elements = handle(FixedArrayBase::cast(parameter_map->get(1)), isolate);
4817 if (elements->IsDictionary()) {
4818 return Handle<SeededNumberDictionary>::cast(elements);
4821 DCHECK(object->HasFastSmiOrObjectElements() ||
4822 object->HasFastDoubleElements() ||
4823 object->HasFastArgumentsElements());
4825 Handle<SeededNumberDictionary> dictionary =
4826 GetNormalizedElementDictionary(object, elements);
4828 // Switch to using the dictionary as the backing storage for elements.
4829 ElementsKind target_kind =
4830 is_arguments ? SLOW_SLOPPY_ARGUMENTS_ELEMENTS : DICTIONARY_ELEMENTS;
4831 Handle<Map> new_map = JSObject::GetElementsTransitionMap(object, target_kind);
4832 // Set the new map first to satify the elements type assert in set_elements().
4833 JSObject::MigrateToMap(object, new_map);
4836 FixedArray::cast(object->elements())->set(1, *dictionary);
4838 object->set_elements(*dictionary);
4841 isolate->counters()->elements_to_dictionary()->Increment();
4844 if (FLAG_trace_normalization) {
4845 OFStream os(stdout);
4846 os << "Object elements have been normalized:\n";
4851 DCHECK(object->HasDictionaryElements() || object->HasSlowArgumentsElements());
4856 static Smi* GenerateIdentityHash(Isolate* isolate) {
4860 // Generate a random 32-bit hash value but limit range to fit
4862 hash_value = isolate->random_number_generator()->NextInt() & Smi::kMaxValue;
4864 } while (hash_value == 0 && attempts < 30);
4865 hash_value = hash_value != 0 ? hash_value : 1; // never return 0
4867 return Smi::FromInt(hash_value);
4871 void JSObject::SetIdentityHash(Handle<JSObject> object, Handle<Smi> hash) {
4872 DCHECK(!object->IsJSGlobalProxy());
4873 Isolate* isolate = object->GetIsolate();
4874 Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol());
4875 JSObject::AddProperty(object, hash_code_symbol, hash, NONE);
4879 template<typename ProxyType>
4880 static Handle<Smi> GetOrCreateIdentityHashHelper(Handle<ProxyType> proxy) {
4881 Isolate* isolate = proxy->GetIsolate();
4883 Handle<Object> maybe_hash(proxy->hash(), isolate);
4884 if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash);
4886 Handle<Smi> hash(GenerateIdentityHash(isolate), isolate);
4887 proxy->set_hash(*hash);
4892 Object* JSObject::GetIdentityHash() {
4893 DisallowHeapAllocation no_gc;
4894 Isolate* isolate = GetIsolate();
4895 if (IsJSGlobalProxy()) {
4896 return JSGlobalProxy::cast(this)->hash();
4898 Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol());
4899 Handle<Object> stored_value =
4900 Object::GetPropertyOrElement(Handle<Object>(this, isolate),
4901 hash_code_symbol).ToHandleChecked();
4902 return stored_value->IsSmi() ? *stored_value
4903 : isolate->heap()->undefined_value();
4907 Handle<Smi> JSObject::GetOrCreateIdentityHash(Handle<JSObject> object) {
4908 if (object->IsJSGlobalProxy()) {
4909 return GetOrCreateIdentityHashHelper(Handle<JSGlobalProxy>::cast(object));
4912 Isolate* isolate = object->GetIsolate();
4914 Handle<Object> maybe_hash(object->GetIdentityHash(), isolate);
4915 if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash);
4917 Handle<Smi> hash(GenerateIdentityHash(isolate), isolate);
4918 Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol());
4919 JSObject::AddProperty(object, hash_code_symbol, hash, NONE);
4924 Object* JSProxy::GetIdentityHash() {
4925 return this->hash();
4929 Handle<Smi> JSProxy::GetOrCreateIdentityHash(Handle<JSProxy> proxy) {
4930 return GetOrCreateIdentityHashHelper(proxy);
4934 Object* JSObject::GetHiddenProperty(Handle<Name> key) {
4935 DisallowHeapAllocation no_gc;
4936 DCHECK(key->IsUniqueName());
4937 if (IsJSGlobalProxy()) {
4938 // For a proxy, use the prototype as target object.
4939 PrototypeIterator iter(GetIsolate(), this);
4940 // If the proxy is detached, return undefined.
4941 if (iter.IsAtEnd()) return GetHeap()->the_hole_value();
4942 DCHECK(iter.GetCurrent()->IsJSGlobalObject());
4943 return JSObject::cast(iter.GetCurrent())->GetHiddenProperty(key);
4945 DCHECK(!IsJSGlobalProxy());
4946 Object* inline_value = GetHiddenPropertiesHashTable();
4948 if (inline_value->IsUndefined()) return GetHeap()->the_hole_value();
4950 ObjectHashTable* hashtable = ObjectHashTable::cast(inline_value);
4951 Object* entry = hashtable->Lookup(key);
4956 Handle<Object> JSObject::SetHiddenProperty(Handle<JSObject> object,
4958 Handle<Object> value) {
4959 Isolate* isolate = object->GetIsolate();
4961 DCHECK(key->IsUniqueName());
4962 if (object->IsJSGlobalProxy()) {
4963 // For a proxy, use the prototype as target object.
4964 PrototypeIterator iter(isolate, object);
4965 // If the proxy is detached, return undefined.
4966 if (iter.IsAtEnd()) return isolate->factory()->undefined_value();
4967 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
4968 return SetHiddenProperty(
4969 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)), key,
4972 DCHECK(!object->IsJSGlobalProxy());
4974 Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate);
4976 Handle<ObjectHashTable> hashtable =
4977 GetOrCreateHiddenPropertiesHashtable(object);
4979 // If it was found, check if the key is already in the dictionary.
4980 Handle<ObjectHashTable> new_table = ObjectHashTable::Put(hashtable, key,
4982 if (*new_table != *hashtable) {
4983 // If adding the key expanded the dictionary (i.e., Add returned a new
4984 // dictionary), store it back to the object.
4985 SetHiddenPropertiesHashTable(object, new_table);
4988 // Return this to mark success.
4993 void JSObject::DeleteHiddenProperty(Handle<JSObject> object, Handle<Name> key) {
4994 Isolate* isolate = object->GetIsolate();
4995 DCHECK(key->IsUniqueName());
4997 if (object->IsJSGlobalProxy()) {
4998 PrototypeIterator iter(isolate, object);
4999 if (iter.IsAtEnd()) return;
5000 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
5001 return DeleteHiddenProperty(
5002 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)), key);
5005 Object* inline_value = object->GetHiddenPropertiesHashTable();
5007 if (inline_value->IsUndefined()) return;
5009 Handle<ObjectHashTable> hashtable(ObjectHashTable::cast(inline_value));
5010 bool was_present = false;
5011 ObjectHashTable::Remove(hashtable, key, &was_present);
5015 bool JSObject::HasHiddenProperties(Handle<JSObject> object) {
5016 Handle<Name> hidden = object->GetIsolate()->factory()->hidden_string();
5017 LookupIterator it(object, hidden, LookupIterator::OWN_SKIP_INTERCEPTOR);
5018 Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it);
5019 // Cannot get an exception since the hidden_string isn't accessible to JS.
5020 DCHECK(maybe.IsJust());
5021 return maybe.FromJust() != ABSENT;
5025 Object* JSObject::GetHiddenPropertiesHashTable() {
5026 DCHECK(!IsJSGlobalProxy());
5027 if (HasFastProperties()) {
5028 // If the object has fast properties, check whether the first slot
5029 // in the descriptor array matches the hidden string. Since the
5030 // hidden strings hash code is zero (and no other name has hash
5031 // code zero) it will always occupy the first entry if present.
5032 DescriptorArray* descriptors = this->map()->instance_descriptors();
5033 if (descriptors->number_of_descriptors() > 0) {
5034 int sorted_index = descriptors->GetSortedKeyIndex(0);
5035 if (descriptors->GetKey(sorted_index) == GetHeap()->hidden_string() &&
5036 sorted_index < map()->NumberOfOwnDescriptors()) {
5037 DCHECK(descriptors->GetType(sorted_index) == DATA);
5038 DCHECK(descriptors->GetDetails(sorted_index).representation().
5039 IsCompatibleForLoad(Representation::Tagged()));
5040 FieldIndex index = FieldIndex::ForDescriptor(this->map(),
5042 return this->RawFastPropertyAt(index);
5044 return GetHeap()->undefined_value();
5047 return GetHeap()->undefined_value();
5050 Isolate* isolate = GetIsolate();
5051 LookupIterator it(handle(this), isolate->factory()->hidden_string(),
5052 LookupIterator::OWN_SKIP_INTERCEPTOR);
5053 // Access check is always skipped for the hidden string anyways.
5054 return *GetDataProperty(&it);
5058 Handle<ObjectHashTable> JSObject::GetOrCreateHiddenPropertiesHashtable(
5059 Handle<JSObject> object) {
5060 Isolate* isolate = object->GetIsolate();
5062 static const int kInitialCapacity = 4;
5063 Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate);
5064 if (inline_value->IsHashTable()) {
5065 return Handle<ObjectHashTable>::cast(inline_value);
5068 Handle<ObjectHashTable> hashtable = ObjectHashTable::New(
5069 isolate, kInitialCapacity, USE_CUSTOM_MINIMUM_CAPACITY);
5071 DCHECK(inline_value->IsUndefined());
5072 SetHiddenPropertiesHashTable(object, hashtable);
5077 Handle<Object> JSObject::SetHiddenPropertiesHashTable(Handle<JSObject> object,
5078 Handle<Object> value) {
5079 DCHECK(!object->IsJSGlobalProxy());
5080 Isolate* isolate = object->GetIsolate();
5081 Handle<Name> name = isolate->factory()->hidden_string();
5082 SetOwnPropertyIgnoreAttributes(object, name, value, DONT_ENUM).Assert();
5087 MaybeHandle<Object> JSObject::DeletePropertyWithInterceptor(
5088 LookupIterator* it) {
5089 Isolate* isolate = it->isolate();
5090 // Make sure that the top context does not change when doing callbacks or
5091 // interceptor calls.
5092 AssertNoContextChange ncc(isolate);
5094 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
5095 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
5096 if (interceptor->deleter()->IsUndefined()) return MaybeHandle<Object>();
5098 Handle<JSObject> holder = it->GetHolder<JSObject>();
5100 PropertyCallbackArguments args(isolate, interceptor->data(),
5101 *it->GetReceiver(), *holder);
5102 v8::Local<v8::Boolean> result;
5103 if (it->IsElement()) {
5104 uint32_t index = it->index();
5105 v8::IndexedPropertyDeleterCallback deleter =
5106 v8::ToCData<v8::IndexedPropertyDeleterCallback>(interceptor->deleter());
5108 ApiIndexedPropertyAccess("interceptor-indexed-delete", *holder, index));
5109 result = args.Call(deleter, index);
5110 } else if (it->name()->IsSymbol() && !interceptor->can_intercept_symbols()) {
5111 return MaybeHandle<Object>();
5113 Handle<Name> name = it->name();
5114 v8::GenericNamedPropertyDeleterCallback deleter =
5115 v8::ToCData<v8::GenericNamedPropertyDeleterCallback>(
5116 interceptor->deleter());
5118 ApiNamedPropertyAccess("interceptor-named-delete", *holder, *name));
5119 result = args.Call(deleter, v8::Utils::ToLocal(name));
5122 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5123 if (result.IsEmpty()) return MaybeHandle<Object>();
5125 DCHECK(result->IsBoolean());
5126 Handle<Object> result_internal = v8::Utils::OpenHandle(*result);
5127 result_internal->VerifyApiCallResultType();
5128 // Rebox CustomArguments::kReturnValueOffset before returning.
5129 return handle(*result_internal, isolate);
5133 void JSObject::DeleteNormalizedProperty(Handle<JSObject> object,
5134 Handle<Name> name, int entry) {
5135 DCHECK(!object->HasFastProperties());
5136 Isolate* isolate = object->GetIsolate();
5138 if (object->IsGlobalObject()) {
5139 // If we have a global object, invalidate the cell and swap in a new one.
5140 Handle<GlobalDictionary> dictionary(object->global_dictionary());
5141 DCHECK_NE(GlobalDictionary::kNotFound, entry);
5143 auto cell = PropertyCell::InvalidateEntry(dictionary, entry);
5144 cell->set_value(isolate->heap()->the_hole_value());
5145 // TODO(ishell): InvalidateForDelete
5146 cell->set_property_details(
5147 cell->property_details().set_cell_type(PropertyCellType::kInvalidated));
5149 Handle<NameDictionary> dictionary(object->property_dictionary());
5150 DCHECK_NE(NameDictionary::kNotFound, entry);
5152 NameDictionary::DeleteProperty(dictionary, entry);
5153 Handle<NameDictionary> new_properties =
5154 NameDictionary::Shrink(dictionary, name);
5155 object->set_properties(*new_properties);
5160 // ECMA-262, 3rd, 8.6.2.5
5161 MaybeHandle<Object> JSReceiver::DeleteProperty(LookupIterator* it,
5162 LanguageMode language_mode) {
5163 Isolate* isolate = it->isolate();
5164 if (it->state() == LookupIterator::JSPROXY) {
5165 return JSProxy::DeletePropertyWithHandler(it->GetHolder<JSProxy>(),
5166 it->GetName(), language_mode);
5169 Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver());
5172 receiver->map()->is_observed() &&
5173 (it->IsElement() || !isolate->IsInternallyUsedPropertyName(it->name()));
5175 Handle<Object> old_value = it->factory()->the_hole_value();
5177 for (; it->IsFound(); it->Next()) {
5178 switch (it->state()) {
5179 case LookupIterator::JSPROXY:
5180 case LookupIterator::NOT_FOUND:
5181 case LookupIterator::TRANSITION:
5183 case LookupIterator::ACCESS_CHECK:
5184 if (it->HasAccess()) break;
5185 isolate->ReportFailedAccessCheck(it->GetHolder<JSObject>());
5186 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5187 return it->factory()->false_value();
5188 case LookupIterator::INTERCEPTOR: {
5189 MaybeHandle<Object> maybe_result =
5190 JSObject::DeletePropertyWithInterceptor(it);
5191 // Delete with interceptor succeeded. Return result.
5192 if (!maybe_result.is_null()) return maybe_result;
5193 // An exception was thrown in the interceptor. Propagate.
5194 if (isolate->has_pending_exception()) return maybe_result;
5197 case LookupIterator::INTEGER_INDEXED_EXOTIC:
5198 return it->factory()->true_value();
5199 case LookupIterator::DATA:
5201 old_value = it->GetDataValue();
5204 case LookupIterator::ACCESSOR: {
5205 if (!it->IsConfigurable() || receiver->map()->is_strong()) {
5206 // Fail if the property is not configurable, or on a strong object.
5207 if (is_strict(language_mode)) {
5208 MessageTemplate::Template templ =
5209 receiver->map()->is_strong()
5210 ? MessageTemplate::kStrongDeleteProperty
5211 : MessageTemplate::kStrictDeleteProperty;
5213 isolate, NewTypeError(templ, it->GetName(), receiver), Object);
5215 return it->factory()->false_value();
5221 RETURN_ON_EXCEPTION(isolate,
5222 JSObject::EnqueueChangeRecord(
5223 receiver, "delete", it->GetName(), old_value),
5227 return it->factory()->true_value();
5232 return it->factory()->true_value();
5236 MaybeHandle<Object> JSReceiver::DeleteElement(Handle<JSReceiver> object,
5238 LanguageMode language_mode) {
5239 LookupIterator it(object->GetIsolate(), object, index,
5240 LookupIterator::HIDDEN);
5241 return DeleteProperty(&it, language_mode);
5245 MaybeHandle<Object> JSReceiver::DeleteProperty(Handle<JSReceiver> object,
5247 LanguageMode language_mode) {
5248 LookupIterator it(object, name, LookupIterator::HIDDEN);
5249 return JSObject::DeleteProperty(&it, language_mode);
5253 MaybeHandle<Object> JSReceiver::DeletePropertyOrElement(
5254 Handle<JSReceiver> object, Handle<Name> name, LanguageMode language_mode) {
5255 LookupIterator it = LookupIterator::PropertyOrElement(
5256 name->GetIsolate(), object, name, LookupIterator::HIDDEN);
5257 return JSObject::DeleteProperty(&it, language_mode);
5261 bool JSObject::ReferencesObjectFromElements(FixedArray* elements,
5264 DCHECK(IsFastObjectElementsKind(kind) ||
5265 kind == DICTIONARY_ELEMENTS);
5266 if (IsFastObjectElementsKind(kind)) {
5267 int length = IsJSArray()
5268 ? Smi::cast(JSArray::cast(this)->length())->value()
5269 : elements->length();
5270 for (int i = 0; i < length; ++i) {
5271 Object* element = elements->get(i);
5272 if (!element->IsTheHole() && element == object) return true;
5276 SeededNumberDictionary::cast(elements)->SlowReverseLookup(object);
5277 if (!key->IsUndefined()) return true;
5283 // Check whether this object references another object.
5284 bool JSObject::ReferencesObject(Object* obj) {
5285 Map* map_of_this = map();
5286 Heap* heap = GetHeap();
5287 DisallowHeapAllocation no_allocation;
5289 // Is the object the constructor for this object?
5290 if (map_of_this->GetConstructor() == obj) {
5294 // Is the object the prototype for this object?
5295 if (map_of_this->prototype() == obj) {
5299 // Check if the object is among the named properties.
5300 Object* key = SlowReverseLookup(obj);
5301 if (!key->IsUndefined()) {
5305 // Check if the object is among the indexed properties.
5306 ElementsKind kind = GetElementsKind();
5308 // Raw pixels and external arrays do not reference other
5310 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
5311 case TYPE##_ELEMENTS: \
5314 TYPED_ARRAYS(TYPED_ARRAY_CASE)
5315 #undef TYPED_ARRAY_CASE
5317 case FAST_DOUBLE_ELEMENTS:
5318 case FAST_HOLEY_DOUBLE_ELEMENTS:
5320 case FAST_SMI_ELEMENTS:
5321 case FAST_HOLEY_SMI_ELEMENTS:
5324 case FAST_HOLEY_ELEMENTS:
5325 case DICTIONARY_ELEMENTS: {
5326 FixedArray* elements = FixedArray::cast(this->elements());
5327 if (ReferencesObjectFromElements(elements, kind, obj)) return true;
5330 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
5331 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: {
5332 FixedArray* parameter_map = FixedArray::cast(elements());
5333 // Check the mapped parameters.
5334 int length = parameter_map->length();
5335 for (int i = 2; i < length; ++i) {
5336 Object* value = parameter_map->get(i);
5337 if (!value->IsTheHole() && value == obj) return true;
5339 // Check the arguments.
5340 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
5341 kind = arguments->IsDictionary() ? DICTIONARY_ELEMENTS :
5342 FAST_HOLEY_ELEMENTS;
5343 if (ReferencesObjectFromElements(arguments, kind, obj)) return true;
5348 // For functions check the context.
5349 if (IsJSFunction()) {
5350 // Get the constructor function for arguments array.
5351 Map* arguments_map =
5352 heap->isolate()->context()->native_context()->sloppy_arguments_map();
5353 JSFunction* arguments_function =
5354 JSFunction::cast(arguments_map->GetConstructor());
5356 // Get the context and don't check if it is the native context.
5357 JSFunction* f = JSFunction::cast(this);
5358 Context* context = f->context();
5359 if (context->IsNativeContext()) {
5363 // Check the non-special context slots.
5364 for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) {
5365 // Only check JS objects.
5366 if (context->get(i)->IsJSObject()) {
5367 JSObject* ctxobj = JSObject::cast(context->get(i));
5368 // If it is an arguments array check the content.
5369 if (ctxobj->map()->GetConstructor() == arguments_function) {
5370 if (ctxobj->ReferencesObject(obj)) {
5373 } else if (ctxobj == obj) {
5379 // Check the context extension (if any) if it can have references.
5380 if (context->has_extension() && !context->IsCatchContext()) {
5381 // With harmony scoping, a JSFunction may have a global context.
5382 // TODO(mvstanton): walk into the ScopeInfo.
5383 if (context->IsScriptContext()) {
5387 return JSObject::cast(context->extension())->ReferencesObject(obj);
5391 // No references to object.
5396 MaybeHandle<Object> JSObject::PreventExtensions(Handle<JSObject> object) {
5397 if (!object->map()->is_extensible()) return object;
5399 if (!object->HasSloppyArgumentsElements() && !object->map()->is_observed()) {
5400 return PreventExtensionsWithTransition<NONE>(object);
5403 Isolate* isolate = object->GetIsolate();
5405 if (object->IsAccessCheckNeeded() && !isolate->MayAccess(object)) {
5406 isolate->ReportFailedAccessCheck(object);
5407 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5408 return isolate->factory()->false_value();
5411 if (object->IsJSGlobalProxy()) {
5412 PrototypeIterator iter(isolate, object);
5413 if (iter.IsAtEnd()) return object;
5414 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
5415 return PreventExtensions(
5416 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)));
5419 // It's not possible to seal objects with external array elements
5420 if (object->HasFixedTypedArrayElements()) {
5422 isolate, NewTypeError(MessageTemplate::kCannotPreventExtExternalArray),
5426 // If there are fast elements we normalize.
5427 Handle<SeededNumberDictionary> dictionary = NormalizeElements(object);
5428 DCHECK(object->HasDictionaryElements() || object->HasSlowArgumentsElements());
5430 // Make sure that we never go back to fast case.
5431 object->RequireSlowElements(*dictionary);
5433 // Do a map transition, other objects with this map may still
5435 // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps.
5436 Handle<Map> new_map = Map::Copy(handle(object->map()), "PreventExtensions");
5438 new_map->set_is_extensible(false);
5439 JSObject::MigrateToMap(object, new_map);
5440 DCHECK(!object->map()->is_extensible());
5442 if (object->map()->is_observed()) {
5443 RETURN_ON_EXCEPTION(
5445 EnqueueChangeRecord(object, "preventExtensions", Handle<Name>(),
5446 isolate->factory()->the_hole_value()),
5453 bool JSObject::IsExtensible() {
5454 if (IsJSGlobalProxy()) {
5455 PrototypeIterator iter(GetIsolate(), this);
5456 if (iter.IsAtEnd()) return false;
5457 DCHECK(iter.GetCurrent()->IsJSGlobalObject());
5458 return JSObject::cast(iter.GetCurrent())->map()->is_extensible();
5460 return map()->is_extensible();
5464 template <typename Dictionary>
5465 static void ApplyAttributesToDictionary(Dictionary* dictionary,
5466 const PropertyAttributes attributes) {
5467 int capacity = dictionary->Capacity();
5468 for (int i = 0; i < capacity; i++) {
5469 Object* k = dictionary->KeyAt(i);
5470 if (dictionary->IsKey(k) &&
5471 !(k->IsSymbol() && Symbol::cast(k)->is_private())) {
5472 PropertyDetails details = dictionary->DetailsAt(i);
5473 int attrs = attributes;
5474 // READ_ONLY is an invalid attribute for JS setters/getters.
5475 if ((attributes & READ_ONLY) && details.type() == ACCESSOR_CONSTANT) {
5476 Object* v = dictionary->ValueAt(i);
5477 if (v->IsPropertyCell()) v = PropertyCell::cast(v)->value();
5478 if (v->IsAccessorPair()) attrs &= ~READ_ONLY;
5480 details = details.CopyAddAttributes(
5481 static_cast<PropertyAttributes>(attrs));
5482 dictionary->DetailsAtPut(i, details);
5488 template <PropertyAttributes attrs>
5489 MaybeHandle<Object> JSObject::PreventExtensionsWithTransition(
5490 Handle<JSObject> object) {
5491 STATIC_ASSERT(attrs == NONE || attrs == SEALED || attrs == FROZEN);
5493 // Sealing/freezing sloppy arguments should be handled elsewhere.
5494 DCHECK(!object->HasSloppyArgumentsElements());
5495 DCHECK(!object->map()->is_observed());
5497 Isolate* isolate = object->GetIsolate();
5498 if (object->IsAccessCheckNeeded() && !isolate->MayAccess(object)) {
5499 isolate->ReportFailedAccessCheck(object);
5500 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5501 return isolate->factory()->false_value();
5504 if (object->IsJSGlobalProxy()) {
5505 PrototypeIterator iter(isolate, object);
5506 if (iter.IsAtEnd()) return object;
5507 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
5508 return PreventExtensionsWithTransition<attrs>(
5509 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)));
5512 // It's not possible to seal or freeze objects with external array elements
5513 if (object->HasFixedTypedArrayElements()) {
5515 isolate, NewTypeError(MessageTemplate::kCannotPreventExtExternalArray),
5519 Handle<SeededNumberDictionary> new_element_dictionary;
5520 if (!object->HasDictionaryElements()) {
5523 ? Smi::cast(Handle<JSArray>::cast(object)->length())->value()
5524 : object->elements()->length();
5525 new_element_dictionary =
5526 length == 0 ? isolate->factory()->empty_slow_element_dictionary()
5527 : GetNormalizedElementDictionary(
5528 object, handle(object->elements()));
5531 Handle<Symbol> transition_marker;
5532 if (attrs == NONE) {
5533 transition_marker = isolate->factory()->nonextensible_symbol();
5534 } else if (attrs == SEALED) {
5535 transition_marker = isolate->factory()->sealed_symbol();
5537 DCHECK(attrs == FROZEN);
5538 transition_marker = isolate->factory()->frozen_symbol();
5541 Handle<Map> old_map(object->map(), isolate);
5543 TransitionArray::SearchSpecial(*old_map, *transition_marker);
5544 if (transition != NULL) {
5545 Handle<Map> transition_map(transition, isolate);
5546 DCHECK(transition_map->has_dictionary_elements());
5547 DCHECK(!transition_map->is_extensible());
5548 JSObject::MigrateToMap(object, transition_map);
5549 } else if (TransitionArray::CanHaveMoreTransitions(old_map)) {
5550 // Create a new descriptor array with the appropriate property attributes
5551 Handle<Map> new_map = Map::CopyForPreventExtensions(
5552 old_map, attrs, transition_marker, "CopyForPreventExtensions");
5553 JSObject::MigrateToMap(object, new_map);
5555 DCHECK(old_map->is_dictionary_map() || !old_map->is_prototype_map());
5556 // Slow path: need to normalize properties for safety
5557 NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0,
5558 "SlowPreventExtensions");
5560 // Create a new map, since other objects with this map may be extensible.
5561 // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps.
5562 Handle<Map> new_map =
5563 Map::Copy(handle(object->map()), "SlowCopyForPreventExtensions");
5564 new_map->set_is_extensible(false);
5565 new_map->set_elements_kind(DICTIONARY_ELEMENTS);
5566 JSObject::MigrateToMap(object, new_map);
5568 if (attrs != NONE) {
5569 if (object->IsGlobalObject()) {
5570 ApplyAttributesToDictionary(object->global_dictionary(), attrs);
5572 ApplyAttributesToDictionary(object->property_dictionary(), attrs);
5577 DCHECK(object->map()->has_dictionary_elements());
5578 if (!new_element_dictionary.is_null()) {
5579 object->set_elements(*new_element_dictionary);
5582 if (object->elements() != isolate->heap()->empty_slow_element_dictionary()) {
5583 SeededNumberDictionary* dictionary = object->element_dictionary();
5584 // Make sure we never go back to the fast case
5585 object->RequireSlowElements(dictionary);
5586 if (attrs != NONE) {
5587 ApplyAttributesToDictionary(dictionary, attrs);
5595 MaybeHandle<Object> JSObject::Freeze(Handle<JSObject> object) {
5596 return PreventExtensionsWithTransition<FROZEN>(object);
5600 MaybeHandle<Object> JSObject::Seal(Handle<JSObject> object) {
5601 return PreventExtensionsWithTransition<SEALED>(object);
5605 void JSObject::SetObserved(Handle<JSObject> object) {
5606 DCHECK(!object->IsJSGlobalProxy());
5607 DCHECK(!object->IsJSGlobalObject());
5608 Isolate* isolate = object->GetIsolate();
5609 Handle<Map> new_map;
5610 Handle<Map> old_map(object->map(), isolate);
5611 DCHECK(!old_map->is_observed());
5612 Map* transition = TransitionArray::SearchSpecial(
5613 *old_map, isolate->heap()->observed_symbol());
5614 if (transition != NULL) {
5615 new_map = handle(transition, isolate);
5616 DCHECK(new_map->is_observed());
5617 } else if (TransitionArray::CanHaveMoreTransitions(old_map)) {
5618 new_map = Map::CopyForObserved(old_map);
5620 new_map = Map::Copy(old_map, "SlowObserved");
5621 new_map->set_is_observed();
5623 JSObject::MigrateToMap(object, new_map);
5627 Handle<Object> JSObject::FastPropertyAt(Handle<JSObject> object,
5628 Representation representation,
5630 Isolate* isolate = object->GetIsolate();
5631 if (object->IsUnboxedDoubleField(index)) {
5632 double value = object->RawFastDoublePropertyAt(index);
5633 return isolate->factory()->NewHeapNumber(value);
5635 Handle<Object> raw_value(object->RawFastPropertyAt(index), isolate);
5636 return Object::WrapForRead(isolate, raw_value, representation);
5640 template<class ContextObject>
5641 class JSObjectWalkVisitor {
5643 JSObjectWalkVisitor(ContextObject* site_context, bool copying,
5644 JSObject::DeepCopyHints hints)
5645 : site_context_(site_context),
5649 MUST_USE_RESULT MaybeHandle<JSObject> StructureWalk(Handle<JSObject> object);
5652 MUST_USE_RESULT inline MaybeHandle<JSObject> VisitElementOrProperty(
5653 Handle<JSObject> object,
5654 Handle<JSObject> value) {
5655 Handle<AllocationSite> current_site = site_context()->EnterNewScope();
5656 MaybeHandle<JSObject> copy_of_value = StructureWalk(value);
5657 site_context()->ExitScope(current_site, value);
5658 return copy_of_value;
5661 inline ContextObject* site_context() { return site_context_; }
5662 inline Isolate* isolate() { return site_context()->isolate(); }
5664 inline bool copying() const { return copying_; }
5667 ContextObject* site_context_;
5668 const bool copying_;
5669 const JSObject::DeepCopyHints hints_;
5673 template <class ContextObject>
5674 MaybeHandle<JSObject> JSObjectWalkVisitor<ContextObject>::StructureWalk(
5675 Handle<JSObject> object) {
5676 Isolate* isolate = this->isolate();
5677 bool copying = this->copying();
5678 bool shallow = hints_ == JSObject::kObjectIsShallow;
5681 StackLimitCheck check(isolate);
5683 if (check.HasOverflowed()) {
5684 isolate->StackOverflow();
5685 return MaybeHandle<JSObject>();
5689 if (object->map()->is_deprecated()) {
5690 JSObject::MigrateInstance(object);
5693 Handle<JSObject> copy;
5695 Handle<AllocationSite> site_to_pass;
5696 if (site_context()->ShouldCreateMemento(object)) {
5697 site_to_pass = site_context()->current();
5699 copy = isolate->factory()->CopyJSObjectWithAllocationSite(
5700 object, site_to_pass);
5705 DCHECK(copying || copy.is_identical_to(object));
5707 ElementsKind kind = copy->GetElementsKind();
5708 if (copying && IsFastSmiOrObjectElementsKind(kind) &&
5709 FixedArray::cast(copy->elements())->map() ==
5710 isolate->heap()->fixed_cow_array_map()) {
5711 isolate->counters()->cow_arrays_created_runtime()->Increment();
5715 HandleScope scope(isolate);
5717 // Deep copy own properties.
5718 if (copy->HasFastProperties()) {
5719 Handle<DescriptorArray> descriptors(copy->map()->instance_descriptors());
5720 int limit = copy->map()->NumberOfOwnDescriptors();
5721 for (int i = 0; i < limit; i++) {
5722 PropertyDetails details = descriptors->GetDetails(i);
5723 if (details.type() != DATA) continue;
5724 FieldIndex index = FieldIndex::ForDescriptor(copy->map(), i);
5725 if (object->IsUnboxedDoubleField(index)) {
5727 double value = object->RawFastDoublePropertyAt(index);
5728 copy->RawFastDoublePropertyAtPut(index, value);
5731 Handle<Object> value(object->RawFastPropertyAt(index), isolate);
5732 if (value->IsJSObject()) {
5733 ASSIGN_RETURN_ON_EXCEPTION(
5735 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5738 copy->FastPropertyAtPut(index, *value);
5742 Representation representation = details.representation();
5743 value = Object::NewStorageFor(isolate, value, representation);
5744 copy->FastPropertyAtPut(index, *value);
5750 Handle<FixedArray> names =
5751 isolate->factory()->NewFixedArray(copy->NumberOfOwnProperties());
5752 copy->GetOwnPropertyNames(*names, 0);
5753 for (int i = 0; i < names->length(); i++) {
5754 DCHECK(names->get(i)->IsString());
5755 Handle<String> key_string(String::cast(names->get(i)));
5756 Maybe<PropertyAttributes> maybe =
5757 JSReceiver::GetOwnPropertyAttributes(copy, key_string);
5758 DCHECK(maybe.IsJust());
5759 PropertyAttributes attributes = maybe.FromJust();
5760 // Only deep copy fields from the object literal expression.
5761 // In particular, don't try to copy the length attribute of
5763 if (attributes != NONE) continue;
5764 Handle<Object> value =
5765 Object::GetProperty(copy, key_string).ToHandleChecked();
5766 if (value->IsJSObject()) {
5767 Handle<JSObject> result;
5768 ASSIGN_RETURN_ON_EXCEPTION(
5770 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5773 // Creating object copy for literals. No strict mode needed.
5774 JSObject::SetProperty(copy, key_string, result, SLOPPY).Assert();
5780 // Deep copy own elements.
5781 // Pixel elements cannot be created using an object literal.
5782 DCHECK(!copy->HasFixedTypedArrayElements());
5784 case FAST_SMI_ELEMENTS:
5786 case FAST_HOLEY_SMI_ELEMENTS:
5787 case FAST_HOLEY_ELEMENTS: {
5788 Handle<FixedArray> elements(FixedArray::cast(copy->elements()));
5789 if (elements->map() == isolate->heap()->fixed_cow_array_map()) {
5791 for (int i = 0; i < elements->length(); i++) {
5792 DCHECK(!elements->get(i)->IsJSObject());
5796 for (int i = 0; i < elements->length(); i++) {
5797 Handle<Object> value(elements->get(i), isolate);
5798 DCHECK(value->IsSmi() ||
5799 value->IsTheHole() ||
5800 (IsFastObjectElementsKind(copy->GetElementsKind())));
5801 if (value->IsJSObject()) {
5802 Handle<JSObject> result;
5803 ASSIGN_RETURN_ON_EXCEPTION(
5805 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5808 elements->set(i, *result);
5815 case DICTIONARY_ELEMENTS: {
5816 Handle<SeededNumberDictionary> element_dictionary(
5817 copy->element_dictionary());
5818 int capacity = element_dictionary->Capacity();
5819 for (int i = 0; i < capacity; i++) {
5820 Object* k = element_dictionary->KeyAt(i);
5821 if (element_dictionary->IsKey(k)) {
5822 Handle<Object> value(element_dictionary->ValueAt(i), isolate);
5823 if (value->IsJSObject()) {
5824 Handle<JSObject> result;
5825 ASSIGN_RETURN_ON_EXCEPTION(
5827 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5830 element_dictionary->ValueAtPut(i, *result);
5837 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
5838 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
5843 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
5844 case TYPE##_ELEMENTS: \
5846 TYPED_ARRAYS(TYPED_ARRAY_CASE)
5847 #undef TYPED_ARRAY_CASE
5849 case FAST_DOUBLE_ELEMENTS:
5850 case FAST_HOLEY_DOUBLE_ELEMENTS:
5851 // No contained objects, nothing to do.
5860 MaybeHandle<JSObject> JSObject::DeepWalk(
5861 Handle<JSObject> object,
5862 AllocationSiteCreationContext* site_context) {
5863 JSObjectWalkVisitor<AllocationSiteCreationContext> v(site_context, false,
5865 MaybeHandle<JSObject> result = v.StructureWalk(object);
5866 Handle<JSObject> for_assert;
5867 DCHECK(!result.ToHandle(&for_assert) || for_assert.is_identical_to(object));
5872 MaybeHandle<JSObject> JSObject::DeepCopy(
5873 Handle<JSObject> object,
5874 AllocationSiteUsageContext* site_context,
5875 DeepCopyHints hints) {
5876 JSObjectWalkVisitor<AllocationSiteUsageContext> v(site_context, true, hints);
5877 MaybeHandle<JSObject> copy = v.StructureWalk(object);
5878 Handle<JSObject> for_assert;
5879 DCHECK(!copy.ToHandle(&for_assert) || !for_assert.is_identical_to(object));
5884 // Tests for the fast common case for property enumeration:
5885 // - This object and all prototypes has an enum cache (which means that
5886 // it is no proxy, has no interceptors and needs no access checks).
5887 // - This object has no elements.
5888 // - No prototype has enumerable properties/elements.
5889 bool JSReceiver::IsSimpleEnum() {
5890 for (PrototypeIterator iter(GetIsolate(), this,
5891 PrototypeIterator::START_AT_RECEIVER);
5892 !iter.IsAtEnd(); iter.Advance()) {
5893 if (!iter.GetCurrent()->IsJSObject()) return false;
5894 JSObject* curr = JSObject::cast(iter.GetCurrent());
5895 int enum_length = curr->map()->EnumLength();
5896 if (enum_length == kInvalidEnumCacheSentinel) return false;
5897 if (curr->IsAccessCheckNeeded()) return false;
5898 DCHECK(!curr->HasNamedInterceptor());
5899 DCHECK(!curr->HasIndexedInterceptor());
5900 if (curr->NumberOfEnumElements() > 0) return false;
5901 if (curr != this && enum_length != 0) return false;
5907 static bool FilterKey(Object* key, PropertyAttributes filter) {
5908 if ((filter & SYMBOLIC) && key->IsSymbol()) {
5912 if ((filter & PRIVATE_SYMBOL) &&
5913 key->IsSymbol() && Symbol::cast(key)->is_private()) {
5917 if ((filter & STRING) && !key->IsSymbol()) {
5925 int Map::NumberOfDescribedProperties(DescriptorFlag which,
5926 PropertyAttributes filter) {
5928 DescriptorArray* descs = instance_descriptors();
5929 int limit = which == ALL_DESCRIPTORS
5930 ? descs->number_of_descriptors()
5931 : NumberOfOwnDescriptors();
5932 for (int i = 0; i < limit; i++) {
5933 if ((descs->GetDetails(i).attributes() & filter) == 0 &&
5934 !FilterKey(descs->GetKey(i), filter)) {
5942 int Map::NextFreePropertyIndex() {
5944 int number_of_own_descriptors = NumberOfOwnDescriptors();
5945 DescriptorArray* descs = instance_descriptors();
5946 for (int i = 0; i < number_of_own_descriptors; i++) {
5947 PropertyDetails details = descs->GetDetails(i);
5948 if (details.location() == kField) {
5949 int candidate = details.field_index() + details.field_width_in_words();
5950 if (candidate > free_index) free_index = candidate;
5957 static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
5958 int len = array->length();
5959 for (int i = 0; i < len; i++) {
5960 Object* e = array->get(i);
5961 if (!(e->IsName() || e->IsNumber())) return false;
5967 static Handle<FixedArray> ReduceFixedArrayTo(
5968 Handle<FixedArray> array, int length) {
5969 DCHECK(array->length() >= length);
5970 if (array->length() == length) return array;
5972 Handle<FixedArray> new_array =
5973 array->GetIsolate()->factory()->NewFixedArray(length);
5974 for (int i = 0; i < length; ++i) new_array->set(i, array->get(i));
5979 Handle<FixedArray> JSObject::GetEnumPropertyKeys(Handle<JSObject> object,
5980 bool cache_result) {
5981 Isolate* isolate = object->GetIsolate();
5982 if (object->HasFastProperties()) {
5983 int own_property_count = object->map()->EnumLength();
5984 // If the enum length of the given map is set to kInvalidEnumCache, this
5985 // means that the map itself has never used the present enum cache. The
5986 // first step to using the cache is to set the enum length of the map by
5987 // counting the number of own descriptors that are not DONT_ENUM or
5989 if (own_property_count == kInvalidEnumCacheSentinel) {
5990 own_property_count = object->map()->NumberOfDescribedProperties(
5991 OWN_DESCRIPTORS, DONT_SHOW);
5993 DCHECK(own_property_count == object->map()->NumberOfDescribedProperties(
5994 OWN_DESCRIPTORS, DONT_SHOW));
5997 if (object->map()->instance_descriptors()->HasEnumCache()) {
5998 DescriptorArray* desc = object->map()->instance_descriptors();
5999 Handle<FixedArray> keys(desc->GetEnumCache(), isolate);
6001 // In case the number of properties required in the enum are actually
6002 // present, we can reuse the enum cache. Otherwise, this means that the
6003 // enum cache was generated for a previous (smaller) version of the
6004 // Descriptor Array. In that case we regenerate the enum cache.
6005 if (own_property_count <= keys->length()) {
6006 if (cache_result) object->map()->SetEnumLength(own_property_count);
6007 isolate->counters()->enum_cache_hits()->Increment();
6008 return ReduceFixedArrayTo(keys, own_property_count);
6012 Handle<Map> map(object->map());
6014 if (map->instance_descriptors()->IsEmpty()) {
6015 isolate->counters()->enum_cache_hits()->Increment();
6016 if (cache_result) map->SetEnumLength(0);
6017 return isolate->factory()->empty_fixed_array();
6020 isolate->counters()->enum_cache_misses()->Increment();
6022 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(
6023 own_property_count);
6024 Handle<FixedArray> indices = isolate->factory()->NewFixedArray(
6025 own_property_count);
6027 Handle<DescriptorArray> descs =
6028 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate);
6030 int size = map->NumberOfOwnDescriptors();
6033 for (int i = 0; i < size; i++) {
6034 PropertyDetails details = descs->GetDetails(i);
6035 Object* key = descs->GetKey(i);
6036 if (!(details.IsDontEnum() || key->IsSymbol())) {
6037 storage->set(index, key);
6038 if (!indices.is_null()) {
6039 if (details.type() != DATA) {
6040 indices = Handle<FixedArray>();
6042 FieldIndex field_index = FieldIndex::ForDescriptor(*map, i);
6043 int load_by_field_index = field_index.GetLoadByFieldIndex();
6044 indices->set(index, Smi::FromInt(load_by_field_index));
6050 DCHECK(index == storage->length());
6052 Handle<FixedArray> bridge_storage =
6053 isolate->factory()->NewFixedArray(
6054 DescriptorArray::kEnumCacheBridgeLength);
6055 DescriptorArray* desc = object->map()->instance_descriptors();
6056 desc->SetEnumCache(*bridge_storage,
6058 indices.is_null() ? Object::cast(Smi::FromInt(0))
6059 : Object::cast(*indices));
6061 object->map()->SetEnumLength(own_property_count);
6064 } else if (object->IsGlobalObject()) {
6065 Handle<GlobalDictionary> dictionary(object->global_dictionary());
6066 int length = dictionary->NumberOfEnumElements();
6068 return Handle<FixedArray>(isolate->heap()->empty_fixed_array());
6070 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length);
6071 dictionary->CopyEnumKeysTo(*storage);
6074 Handle<NameDictionary> dictionary(object->property_dictionary());
6075 int length = dictionary->NumberOfEnumElements();
6077 return Handle<FixedArray>(isolate->heap()->empty_fixed_array());
6079 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length);
6080 dictionary->CopyEnumKeysTo(*storage);
6086 MaybeHandle<FixedArray> JSReceiver::GetKeys(Handle<JSReceiver> object,
6087 KeyCollectionType type) {
6088 USE(ContainsOnlyValidKeys);
6089 Isolate* isolate = object->GetIsolate();
6090 Handle<FixedArray> content = isolate->factory()->empty_fixed_array();
6091 Handle<JSFunction> arguments_function(
6092 JSFunction::cast(isolate->sloppy_arguments_map()->GetConstructor()));
6094 PrototypeIterator::WhereToEnd end = type == OWN_ONLY
6095 ? PrototypeIterator::END_AT_NON_HIDDEN
6096 : PrototypeIterator::END_AT_NULL;
6097 // Only collect keys if access is permitted.
6098 for (PrototypeIterator iter(isolate, object,
6099 PrototypeIterator::START_AT_RECEIVER);
6100 !iter.IsAtEnd(end); iter.Advance()) {
6101 if (PrototypeIterator::GetCurrent(iter)->IsJSProxy()) {
6102 Handle<JSProxy> proxy(JSProxy::cast(*PrototypeIterator::GetCurrent(iter)),
6104 Handle<Object> args[] = { proxy };
6105 Handle<Object> names;
6106 ASSIGN_RETURN_ON_EXCEPTION(
6108 Execution::Call(isolate,
6109 isolate->proxy_enumerate(),
6114 ASSIGN_RETURN_ON_EXCEPTION(
6116 FixedArray::AddKeysFromArrayLike(
6117 content, Handle<JSObject>::cast(names)),
6122 Handle<JSObject> current =
6123 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
6125 // Check access rights if required.
6126 if (current->IsAccessCheckNeeded() && !isolate->MayAccess(current)) {
6127 if (iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN)) {
6128 isolate->ReportFailedAccessCheck(current);
6129 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, FixedArray);
6134 // Compute the element keys.
6135 Handle<FixedArray> element_keys =
6136 isolate->factory()->NewFixedArray(current->NumberOfEnumElements());
6137 current->GetEnumElementKeys(*element_keys);
6138 ASSIGN_RETURN_ON_EXCEPTION(
6140 FixedArray::UnionOfKeys(content, element_keys),
6142 DCHECK(ContainsOnlyValidKeys(content));
6144 // Add the element keys from the interceptor.
6145 if (current->HasIndexedInterceptor()) {
6146 Handle<JSObject> result;
6147 if (JSObject::GetKeysForIndexedInterceptor(
6148 current, object).ToHandle(&result)) {
6149 ASSIGN_RETURN_ON_EXCEPTION(
6151 FixedArray::AddKeysFromArrayLike(content, result),
6154 DCHECK(ContainsOnlyValidKeys(content));
6157 // We can cache the computed property keys if access checks are
6158 // not needed and no interceptors are involved.
6160 // We do not use the cache if the object has elements and
6161 // therefore it does not make sense to cache the property names
6162 // for arguments objects. Arguments objects will always have
6164 // Wrapped strings have elements, but don't have an elements
6165 // array or dictionary. So the fast inline test for whether to
6166 // use the cache says yes, so we should not create a cache.
6167 bool cache_enum_keys =
6168 ((current->map()->GetConstructor() != *arguments_function) &&
6169 !current->IsJSValue() && !current->IsAccessCheckNeeded() &&
6170 !current->HasNamedInterceptor() && !current->HasIndexedInterceptor());
6171 // Compute the property keys and cache them if possible.
6172 ASSIGN_RETURN_ON_EXCEPTION(
6174 FixedArray::UnionOfKeys(
6175 content, JSObject::GetEnumPropertyKeys(current, cache_enum_keys)),
6177 DCHECK(ContainsOnlyValidKeys(content));
6179 // Add the non-symbol property keys from the interceptor.
6180 if (current->HasNamedInterceptor()) {
6181 Handle<JSObject> result;
6182 if (JSObject::GetKeysForNamedInterceptor(
6183 current, object).ToHandle(&result)) {
6184 ASSIGN_RETURN_ON_EXCEPTION(
6185 isolate, content, FixedArray::AddKeysFromArrayLike(
6186 content, result, FixedArray::NON_SYMBOL_KEYS),
6189 DCHECK(ContainsOnlyValidKeys(content));
6196 bool Map::DictionaryElementsInPrototypeChainOnly() {
6197 if (IsDictionaryElementsKind(elements_kind())) {
6201 for (PrototypeIterator iter(this); !iter.IsAtEnd(); iter.Advance()) {
6202 // Be conservative, don't walk into proxies.
6203 if (iter.GetCurrent()->IsJSProxy()) return true;
6204 // String wrappers have non-configurable, non-writable elements.
6205 if (iter.GetCurrent()->IsStringWrapper()) return true;
6206 JSObject* current = JSObject::cast(iter.GetCurrent());
6208 if (current->HasDictionaryElements() &&
6209 current->element_dictionary()->requires_slow_elements()) {
6213 if (current->HasSlowArgumentsElements()) {
6214 FixedArray* parameter_map = FixedArray::cast(current->elements());
6215 Object* arguments = parameter_map->get(1);
6216 if (SeededNumberDictionary::cast(arguments)->requires_slow_elements()) {
6226 MaybeHandle<Object> JSObject::DefineAccessor(Handle<JSObject> object,
6228 Handle<Object> getter,
6229 Handle<Object> setter,
6230 PropertyAttributes attributes) {
6231 Isolate* isolate = object->GetIsolate();
6233 LookupIterator it = LookupIterator::PropertyOrElement(
6234 isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR);
6236 if (it.state() == LookupIterator::ACCESS_CHECK) {
6237 if (!it.HasAccess()) {
6238 isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>());
6239 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
6240 return isolate->factory()->undefined_value();
6245 // Ignore accessors on typed arrays.
6246 if (it.IsElement() && object->HasFixedTypedArrayElements()) {
6247 return it.factory()->undefined_value();
6250 Handle<Object> old_value = isolate->factory()->the_hole_value();
6251 bool is_observed = object->map()->is_observed() &&
6252 !isolate->IsInternallyUsedPropertyName(name);
6253 bool preexists = false;
6255 CHECK(GetPropertyAttributes(&it).IsJust());
6256 preexists = it.IsFound();
6257 if (preexists && (it.state() == LookupIterator::DATA ||
6258 it.GetAccessors()->IsAccessorInfo())) {
6259 old_value = GetProperty(&it).ToHandleChecked();
6263 DCHECK(getter->IsSpecFunction() || getter->IsUndefined() || getter->IsNull());
6264 DCHECK(setter->IsSpecFunction() || setter->IsUndefined() || setter->IsNull());
6265 // At least one of the accessors needs to be a new value.
6266 DCHECK(!getter->IsNull() || !setter->IsNull());
6267 if (!getter->IsNull()) {
6268 it.TransitionToAccessorProperty(ACCESSOR_GETTER, getter, attributes);
6270 if (!setter->IsNull()) {
6271 it.TransitionToAccessorProperty(ACCESSOR_SETTER, setter, attributes);
6275 // Make sure the top context isn't changed.
6276 AssertNoContextChange ncc(isolate);
6277 const char* type = preexists ? "reconfigure" : "add";
6278 RETURN_ON_EXCEPTION(
6279 isolate, EnqueueChangeRecord(object, type, name, old_value), Object);
6282 return isolate->factory()->undefined_value();
6286 MaybeHandle<Object> JSObject::SetAccessor(Handle<JSObject> object,
6287 Handle<AccessorInfo> info) {
6288 Isolate* isolate = object->GetIsolate();
6289 Handle<Name> name(Name::cast(info->name()), isolate);
6291 LookupIterator it = LookupIterator::PropertyOrElement(
6292 isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR);
6294 // Duplicate ACCESS_CHECK outside of GetPropertyAttributes for the case that
6295 // the FailedAccessCheckCallbackFunction doesn't throw an exception.
6297 // TODO(verwaest): Force throw an exception if the callback doesn't, so we can
6298 // remove reliance on default return values.
6299 if (it.state() == LookupIterator::ACCESS_CHECK) {
6300 if (!it.HasAccess()) {
6301 isolate->ReportFailedAccessCheck(object);
6302 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
6303 return it.factory()->undefined_value();
6308 // Ignore accessors on typed arrays.
6309 if (it.IsElement() && object->HasFixedTypedArrayElements()) {
6310 return it.factory()->undefined_value();
6313 CHECK(GetPropertyAttributes(&it).IsJust());
6315 // ES5 forbids turning a property into an accessor if it's not
6316 // configurable. See 8.6.1 (Table 5).
6317 if (it.IsFound() && !it.IsConfigurable()) {
6318 return it.factory()->undefined_value();
6321 it.TransitionToAccessorPair(info, info->property_attributes());
6327 MaybeHandle<Object> JSObject::GetAccessor(Handle<JSObject> object,
6329 AccessorComponent component) {
6330 Isolate* isolate = object->GetIsolate();
6332 // Make sure that the top context does not change when doing callbacks or
6333 // interceptor calls.
6334 AssertNoContextChange ncc(isolate);
6336 LookupIterator it = LookupIterator::PropertyOrElement(
6337 isolate, object, name, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
6339 for (; it.IsFound(); it.Next()) {
6340 switch (it.state()) {
6341 case LookupIterator::INTERCEPTOR:
6342 case LookupIterator::NOT_FOUND:
6343 case LookupIterator::TRANSITION:
6346 case LookupIterator::ACCESS_CHECK:
6347 if (it.HasAccess()) continue;
6348 isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>());
6349 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
6350 return isolate->factory()->undefined_value();
6352 case LookupIterator::JSPROXY:
6353 return isolate->factory()->undefined_value();
6355 case LookupIterator::INTEGER_INDEXED_EXOTIC:
6356 return isolate->factory()->undefined_value();
6357 case LookupIterator::DATA:
6359 case LookupIterator::ACCESSOR: {
6360 Handle<Object> maybe_pair = it.GetAccessors();
6361 if (maybe_pair->IsAccessorPair()) {
6363 AccessorPair::cast(*maybe_pair)->GetComponent(component),
6370 return isolate->factory()->undefined_value();
6374 Object* JSObject::SlowReverseLookup(Object* value) {
6375 if (HasFastProperties()) {
6376 int number_of_own_descriptors = map()->NumberOfOwnDescriptors();
6377 DescriptorArray* descs = map()->instance_descriptors();
6378 bool value_is_number = value->IsNumber();
6379 for (int i = 0; i < number_of_own_descriptors; i++) {
6380 if (descs->GetType(i) == DATA) {
6381 FieldIndex field_index = FieldIndex::ForDescriptor(map(), i);
6382 if (IsUnboxedDoubleField(field_index)) {
6383 if (value_is_number) {
6384 double property = RawFastDoublePropertyAt(field_index);
6385 if (property == value->Number()) {
6386 return descs->GetKey(i);
6390 Object* property = RawFastPropertyAt(field_index);
6391 if (field_index.is_double()) {
6392 DCHECK(property->IsMutableHeapNumber());
6393 if (value_is_number && property->Number() == value->Number()) {
6394 return descs->GetKey(i);
6396 } else if (property == value) {
6397 return descs->GetKey(i);
6400 } else if (descs->GetType(i) == DATA_CONSTANT) {
6401 if (descs->GetConstant(i) == value) {
6402 return descs->GetKey(i);
6406 return GetHeap()->undefined_value();
6407 } else if (IsGlobalObject()) {
6408 return global_dictionary()->SlowReverseLookup(value);
6410 return property_dictionary()->SlowReverseLookup(value);
6415 Handle<Map> Map::RawCopy(Handle<Map> map, int instance_size) {
6416 Isolate* isolate = map->GetIsolate();
6417 Handle<Map> result =
6418 isolate->factory()->NewMap(map->instance_type(), instance_size);
6419 Handle<Object> prototype(map->prototype(), isolate);
6420 Map::SetPrototype(result, prototype);
6421 result->set_constructor_or_backpointer(map->GetConstructor());
6422 result->set_bit_field(map->bit_field());
6423 result->set_bit_field2(map->bit_field2());
6424 int new_bit_field3 = map->bit_field3();
6425 new_bit_field3 = OwnsDescriptors::update(new_bit_field3, true);
6426 new_bit_field3 = NumberOfOwnDescriptorsBits::update(new_bit_field3, 0);
6427 new_bit_field3 = EnumLengthBits::update(new_bit_field3,
6428 kInvalidEnumCacheSentinel);
6429 new_bit_field3 = Deprecated::update(new_bit_field3, false);
6430 if (!map->is_dictionary_map()) {
6431 new_bit_field3 = IsUnstable::update(new_bit_field3, false);
6433 new_bit_field3 = Counter::update(new_bit_field3, kRetainingCounterStart);
6434 result->set_bit_field3(new_bit_field3);
6439 Handle<Map> Map::Normalize(Handle<Map> fast_map, PropertyNormalizationMode mode,
6440 const char* reason) {
6441 DCHECK(!fast_map->is_dictionary_map());
6443 Isolate* isolate = fast_map->GetIsolate();
6444 Handle<Object> maybe_cache(isolate->native_context()->normalized_map_cache(),
6446 bool use_cache = !fast_map->is_prototype_map() && !maybe_cache->IsUndefined();
6447 Handle<NormalizedMapCache> cache;
6448 if (use_cache) cache = Handle<NormalizedMapCache>::cast(maybe_cache);
6450 Handle<Map> new_map;
6451 if (use_cache && cache->Get(fast_map, mode).ToHandle(&new_map)) {
6453 if (FLAG_verify_heap) new_map->DictionaryMapVerify();
6455 #ifdef ENABLE_SLOW_DCHECKS
6456 if (FLAG_enable_slow_asserts) {
6457 // The cached map should match newly created normalized map bit-by-bit,
6458 // except for the code cache, which can contain some ics which can be
6459 // applied to the shared map, dependent code and weak cell cache.
6460 Handle<Map> fresh = Map::CopyNormalized(fast_map, mode);
6462 if (new_map->is_prototype_map()) {
6463 // For prototype maps, the PrototypeInfo is not copied.
6464 DCHECK(memcmp(fresh->address(), new_map->address(),
6465 kTransitionsOrPrototypeInfoOffset) == 0);
6466 DCHECK(fresh->raw_transitions() == Smi::FromInt(0));
6467 STATIC_ASSERT(kDescriptorsOffset ==
6468 kTransitionsOrPrototypeInfoOffset + kPointerSize);
6469 DCHECK(memcmp(HeapObject::RawField(*fresh, kDescriptorsOffset),
6470 HeapObject::RawField(*new_map, kDescriptorsOffset),
6471 kCodeCacheOffset - kDescriptorsOffset) == 0);
6473 DCHECK(memcmp(fresh->address(), new_map->address(),
6474 Map::kCodeCacheOffset) == 0);
6476 STATIC_ASSERT(Map::kDependentCodeOffset ==
6477 Map::kCodeCacheOffset + kPointerSize);
6478 STATIC_ASSERT(Map::kWeakCellCacheOffset ==
6479 Map::kDependentCodeOffset + kPointerSize);
6480 int offset = Map::kWeakCellCacheOffset + kPointerSize;
6481 DCHECK(memcmp(fresh->address() + offset,
6482 new_map->address() + offset,
6483 Map::kSize - offset) == 0);
6487 new_map = Map::CopyNormalized(fast_map, mode);
6489 cache->Set(fast_map, new_map);
6490 isolate->counters()->normalized_maps()->Increment();
6493 if (FLAG_trace_maps) {
6494 PrintF("[TraceMaps: Normalize from= %p to= %p reason= %s ]\n",
6495 reinterpret_cast<void*>(*fast_map),
6496 reinterpret_cast<void*>(*new_map), reason);
6500 fast_map->NotifyLeafMapLayoutChange();
6505 Handle<Map> Map::CopyNormalized(Handle<Map> map,
6506 PropertyNormalizationMode mode) {
6507 int new_instance_size = map->instance_size();
6508 if (mode == CLEAR_INOBJECT_PROPERTIES) {
6509 new_instance_size -= map->inobject_properties() * kPointerSize;
6512 Handle<Map> result = RawCopy(map, new_instance_size);
6514 if (mode != CLEAR_INOBJECT_PROPERTIES) {
6515 result->set_inobject_properties(map->inobject_properties());
6518 result->set_dictionary_map(true);
6519 result->set_migration_target(false);
6522 if (FLAG_verify_heap) result->DictionaryMapVerify();
6529 Handle<Map> Map::CopyDropDescriptors(Handle<Map> map) {
6530 Handle<Map> result = RawCopy(map, map->instance_size());
6532 // Please note instance_type and instance_size are set when allocated.
6533 result->set_inobject_properties(map->inobject_properties());
6534 result->set_unused_property_fields(map->unused_property_fields());
6536 result->ClearCodeCache(map->GetHeap());
6537 map->NotifyLeafMapLayoutChange();
6542 Handle<Map> Map::ShareDescriptor(Handle<Map> map,
6543 Handle<DescriptorArray> descriptors,
6544 Descriptor* descriptor) {
6545 // Sanity check. This path is only to be taken if the map owns its descriptor
6546 // array, implying that its NumberOfOwnDescriptors equals the number of
6547 // descriptors in the descriptor array.
6548 DCHECK(map->NumberOfOwnDescriptors() ==
6549 map->instance_descriptors()->number_of_descriptors());
6551 Handle<Map> result = CopyDropDescriptors(map);
6552 Handle<Name> name = descriptor->GetKey();
6554 // Ensure there's space for the new descriptor in the shared descriptor array.
6555 if (descriptors->NumberOfSlackDescriptors() == 0) {
6556 int old_size = descriptors->number_of_descriptors();
6557 if (old_size == 0) {
6558 descriptors = DescriptorArray::Allocate(map->GetIsolate(), 0, 1);
6560 int slack = SlackForArraySize(old_size, kMaxNumberOfDescriptors);
6561 EnsureDescriptorSlack(map, slack);
6562 descriptors = handle(map->instance_descriptors());
6566 Handle<LayoutDescriptor> layout_descriptor =
6567 FLAG_unbox_double_fields
6568 ? LayoutDescriptor::ShareAppend(map, descriptor->GetDetails())
6569 : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate());
6572 DisallowHeapAllocation no_gc;
6573 descriptors->Append(descriptor);
6574 result->InitializeDescriptors(*descriptors, *layout_descriptor);
6577 DCHECK(result->NumberOfOwnDescriptors() == map->NumberOfOwnDescriptors() + 1);
6578 ConnectTransition(map, result, name, SIMPLE_PROPERTY_TRANSITION);
6587 void Map::TraceTransition(const char* what, Map* from, Map* to, Name* name) {
6588 if (FLAG_trace_maps) {
6589 PrintF("[TraceMaps: %s from= %p to= %p name= ", what,
6590 reinterpret_cast<void*>(from), reinterpret_cast<void*>(to));
6591 name->NameShortPrint();
6598 void Map::TraceAllTransitions(Map* map) {
6599 Object* transitions = map->raw_transitions();
6600 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
6601 for (int i = -0; i < num_transitions; ++i) {
6602 Map* target = TransitionArray::GetTarget(transitions, i);
6603 Name* key = TransitionArray::GetKey(transitions, i);
6604 Map::TraceTransition("Transition", map, target, key);
6605 Map::TraceAllTransitions(target);
6609 #endif // TRACE_MAPS
6612 void Map::ConnectTransition(Handle<Map> parent, Handle<Map> child,
6613 Handle<Name> name, SimpleTransitionFlag flag) {
6614 parent->set_owns_descriptors(false);
6615 if (parent->is_prototype_map()) {
6616 DCHECK(child->is_prototype_map());
6618 Map::TraceTransition("NoTransition", *parent, *child, *name);
6621 TransitionArray::Insert(parent, name, child, flag);
6623 Map::TraceTransition("Transition", *parent, *child, *name);
6629 Handle<Map> Map::CopyReplaceDescriptors(
6630 Handle<Map> map, Handle<DescriptorArray> descriptors,
6631 Handle<LayoutDescriptor> layout_descriptor, TransitionFlag flag,
6632 MaybeHandle<Name> maybe_name, const char* reason,
6633 SimpleTransitionFlag simple_flag) {
6634 DCHECK(descriptors->IsSortedNoDuplicates());
6636 Handle<Map> result = CopyDropDescriptors(map);
6638 if (!map->is_prototype_map()) {
6639 if (flag == INSERT_TRANSITION &&
6640 TransitionArray::CanHaveMoreTransitions(map)) {
6641 result->InitializeDescriptors(*descriptors, *layout_descriptor);
6644 CHECK(maybe_name.ToHandle(&name));
6645 ConnectTransition(map, result, name, simple_flag);
6647 int length = descriptors->number_of_descriptors();
6648 for (int i = 0; i < length; i++) {
6649 descriptors->SetRepresentation(i, Representation::Tagged());
6650 if (descriptors->GetDetails(i).type() == DATA) {
6651 descriptors->SetValue(i, HeapType::Any());
6654 result->InitializeDescriptors(*descriptors,
6655 LayoutDescriptor::FastPointerLayout());
6658 result->InitializeDescriptors(*descriptors, *layout_descriptor);
6661 if (FLAG_trace_maps &&
6662 // Mirror conditions above that did not call ConnectTransition().
6663 (map->is_prototype_map() ||
6664 !(flag == INSERT_TRANSITION &&
6665 TransitionArray::CanHaveMoreTransitions(map)))) {
6666 PrintF("[TraceMaps: ReplaceDescriptors from= %p to= %p reason= %s ]\n",
6667 reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*result),
6676 // Since this method is used to rewrite an existing transition tree, it can
6677 // always insert transitions without checking.
6678 Handle<Map> Map::CopyInstallDescriptors(
6679 Handle<Map> map, int new_descriptor, Handle<DescriptorArray> descriptors,
6680 Handle<LayoutDescriptor> full_layout_descriptor) {
6681 DCHECK(descriptors->IsSortedNoDuplicates());
6683 Handle<Map> result = CopyDropDescriptors(map);
6685 result->set_instance_descriptors(*descriptors);
6686 result->SetNumberOfOwnDescriptors(new_descriptor + 1);
6688 int unused_property_fields = map->unused_property_fields();
6689 PropertyDetails details = descriptors->GetDetails(new_descriptor);
6690 if (details.location() == kField) {
6691 unused_property_fields = map->unused_property_fields() - 1;
6692 if (unused_property_fields < 0) {
6693 unused_property_fields += JSObject::kFieldsAdded;
6696 result->set_unused_property_fields(unused_property_fields);
6698 if (FLAG_unbox_double_fields) {
6699 Handle<LayoutDescriptor> layout_descriptor =
6700 LayoutDescriptor::AppendIfFastOrUseFull(map, details,
6701 full_layout_descriptor);
6702 result->set_layout_descriptor(*layout_descriptor);
6704 // TODO(ishell): remove these checks from VERIFY_HEAP mode.
6705 if (FLAG_verify_heap) {
6706 CHECK(result->layout_descriptor()->IsConsistentWithMap(*result));
6709 SLOW_DCHECK(result->layout_descriptor()->IsConsistentWithMap(*result));
6711 result->set_visitor_id(StaticVisitorBase::GetVisitorId(*result));
6714 Handle<Name> name = handle(descriptors->GetKey(new_descriptor));
6715 ConnectTransition(map, result, name, SIMPLE_PROPERTY_TRANSITION);
6721 Handle<Map> Map::CopyAsElementsKind(Handle<Map> map, ElementsKind kind,
6722 TransitionFlag flag) {
6723 Map* maybe_elements_transition_map = NULL;
6724 if (flag == INSERT_TRANSITION) {
6725 maybe_elements_transition_map = map->ElementsTransitionMap();
6726 DCHECK(maybe_elements_transition_map == NULL ||
6727 (maybe_elements_transition_map->elements_kind() ==
6728 DICTIONARY_ELEMENTS &&
6729 kind == DICTIONARY_ELEMENTS));
6730 DCHECK(!IsFastElementsKind(kind) ||
6731 IsMoreGeneralElementsKindTransition(map->elements_kind(), kind));
6732 DCHECK(kind != map->elements_kind());
6735 bool insert_transition = flag == INSERT_TRANSITION &&
6736 TransitionArray::CanHaveMoreTransitions(map) &&
6737 maybe_elements_transition_map == NULL;
6739 if (insert_transition) {
6740 Handle<Map> new_map = CopyForTransition(map, "CopyAsElementsKind");
6741 new_map->set_elements_kind(kind);
6743 Isolate* isolate = map->GetIsolate();
6744 Handle<Name> name = isolate->factory()->elements_transition_symbol();
6745 ConnectTransition(map, new_map, name, SPECIAL_TRANSITION);
6749 // Create a new free-floating map only if we are not allowed to store it.
6750 Handle<Map> new_map = Copy(map, "CopyAsElementsKind");
6751 new_map->set_elements_kind(kind);
6756 Handle<Map> Map::CopyForObserved(Handle<Map> map) {
6757 DCHECK(!map->is_observed());
6759 Isolate* isolate = map->GetIsolate();
6761 bool insert_transition =
6762 TransitionArray::CanHaveMoreTransitions(map) && !map->is_prototype_map();
6764 if (insert_transition) {
6765 Handle<Map> new_map = CopyForTransition(map, "CopyForObserved");
6766 new_map->set_is_observed();
6768 Handle<Name> name = isolate->factory()->observed_symbol();
6769 ConnectTransition(map, new_map, name, SPECIAL_TRANSITION);
6773 // Create a new free-floating map only if we are not allowed to store it.
6774 Handle<Map> new_map = Map::Copy(map, "CopyForObserved");
6775 new_map->set_is_observed();
6780 Handle<Map> Map::CopyForTransition(Handle<Map> map, const char* reason) {
6781 DCHECK(!map->is_prototype_map());
6782 Handle<Map> new_map = CopyDropDescriptors(map);
6784 if (map->owns_descriptors()) {
6785 // In case the map owned its own descriptors, share the descriptors and
6786 // transfer ownership to the new map.
6787 // The properties did not change, so reuse descriptors.
6788 new_map->InitializeDescriptors(map->instance_descriptors(),
6789 map->GetLayoutDescriptor());
6791 // In case the map did not own its own descriptors, a split is forced by
6792 // copying the map; creating a new descriptor array cell.
6793 Handle<DescriptorArray> descriptors(map->instance_descriptors());
6794 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
6795 Handle<DescriptorArray> new_descriptors =
6796 DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors);
6797 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
6799 new_map->InitializeDescriptors(*new_descriptors, *new_layout_descriptor);
6803 if (FLAG_trace_maps) {
6804 PrintF("[TraceMaps: CopyForTransition from= %p to= %p reason= %s ]\n",
6805 reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*new_map),
6814 Handle<Map> Map::Copy(Handle<Map> map, const char* reason) {
6815 Handle<DescriptorArray> descriptors(map->instance_descriptors());
6816 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
6817 Handle<DescriptorArray> new_descriptors =
6818 DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors);
6819 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
6821 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
6822 OMIT_TRANSITION, MaybeHandle<Name>(), reason,
6823 SPECIAL_TRANSITION);
6827 Handle<Map> Map::Create(Isolate* isolate, int inobject_properties) {
6829 Copy(handle(isolate->object_function()->initial_map()), "MapCreate");
6831 // Check that we do not overflow the instance size when adding the extra
6832 // inobject properties. If the instance size overflows, we allocate as many
6833 // properties as we can as inobject properties.
6834 int max_extra_properties =
6835 (JSObject::kMaxInstanceSize - JSObject::kHeaderSize) >> kPointerSizeLog2;
6837 if (inobject_properties > max_extra_properties) {
6838 inobject_properties = max_extra_properties;
6841 int new_instance_size =
6842 JSObject::kHeaderSize + kPointerSize * inobject_properties;
6844 // Adjust the map with the extra inobject properties.
6845 copy->set_inobject_properties(inobject_properties);
6846 copy->set_unused_property_fields(inobject_properties);
6847 copy->set_instance_size(new_instance_size);
6848 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
6853 Handle<Map> Map::CopyForPreventExtensions(Handle<Map> map,
6854 PropertyAttributes attrs_to_add,
6855 Handle<Symbol> transition_marker,
6856 const char* reason) {
6857 int num_descriptors = map->NumberOfOwnDescriptors();
6858 Isolate* isolate = map->GetIsolate();
6859 Handle<DescriptorArray> new_desc = DescriptorArray::CopyUpToAddAttributes(
6860 handle(map->instance_descriptors(), isolate), num_descriptors,
6862 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
6864 Handle<Map> new_map = CopyReplaceDescriptors(
6865 map, new_desc, new_layout_descriptor, INSERT_TRANSITION,
6866 transition_marker, reason, SPECIAL_TRANSITION);
6867 new_map->set_is_extensible(false);
6868 new_map->set_elements_kind(DICTIONARY_ELEMENTS);
6873 Handle<Map> Map::FixProxy(Handle<Map> map, InstanceType type, int size) {
6874 DCHECK(type == JS_OBJECT_TYPE || type == JS_FUNCTION_TYPE);
6875 DCHECK(map->IsJSProxyMap());
6877 Isolate* isolate = map->GetIsolate();
6879 // Allocate fresh map.
6880 // TODO(rossberg): Once we optimize proxies, cache these maps.
6881 Handle<Map> new_map = isolate->factory()->NewMap(type, size);
6883 Handle<Object> prototype(map->prototype(), isolate);
6884 Map::SetPrototype(new_map, prototype);
6886 map->NotifyLeafMapLayoutChange();
6892 bool DescriptorArray::CanHoldValue(int descriptor, Object* value) {
6893 PropertyDetails details = GetDetails(descriptor);
6894 switch (details.type()) {
6896 return value->FitsRepresentation(details.representation()) &&
6897 GetFieldType(descriptor)->NowContains(value);
6900 DCHECK(GetConstant(descriptor) != value ||
6901 value->FitsRepresentation(details.representation()));
6902 return GetConstant(descriptor) == value;
6905 case ACCESSOR_CONSTANT:
6915 Handle<Map> Map::PrepareForDataProperty(Handle<Map> map, int descriptor,
6916 Handle<Object> value) {
6917 // Dictionaries can store any property value.
6918 if (map->is_dictionary_map()) return map;
6920 // Migrate to the newest map before storing the property.
6923 Handle<DescriptorArray> descriptors(map->instance_descriptors());
6925 if (descriptors->CanHoldValue(descriptor, *value)) return map;
6927 Isolate* isolate = map->GetIsolate();
6928 PropertyAttributes attributes =
6929 descriptors->GetDetails(descriptor).attributes();
6930 Representation representation = value->OptimalRepresentation();
6931 Handle<HeapType> type = value->OptimalType(isolate, representation);
6933 return ReconfigureProperty(map, descriptor, kData, attributes, representation,
6938 Handle<Map> Map::TransitionToDataProperty(Handle<Map> map, Handle<Name> name,
6939 Handle<Object> value,
6940 PropertyAttributes attributes,
6941 StoreFromKeyed store_mode) {
6942 // Dictionary maps can always have additional data properties.
6943 if (map->is_dictionary_map()) return map;
6945 // Migrate to the newest map before storing the property.
6948 Map* maybe_transition =
6949 TransitionArray::SearchTransition(*map, kData, *name, attributes);
6950 if (maybe_transition != NULL) {
6951 Handle<Map> transition(maybe_transition);
6952 int descriptor = transition->LastAdded();
6954 DCHECK_EQ(attributes, transition->instance_descriptors()
6955 ->GetDetails(descriptor)
6958 return Map::PrepareForDataProperty(transition, descriptor, value);
6961 TransitionFlag flag = INSERT_TRANSITION;
6962 MaybeHandle<Map> maybe_map;
6963 if (value->IsJSFunction()) {
6964 maybe_map = Map::CopyWithConstant(map, name, value, attributes, flag);
6965 } else if (!map->TooManyFastProperties(store_mode)) {
6966 Isolate* isolate = name->GetIsolate();
6967 Representation representation = value->OptimalRepresentation();
6968 Handle<HeapType> type = value->OptimalType(isolate, representation);
6970 Map::CopyWithField(map, name, type, attributes, representation, flag);
6974 if (!maybe_map.ToHandle(&result)) {
6976 if (FLAG_trace_maps) {
6977 Vector<char> name_buffer = Vector<char>::New(100);
6978 name->NameShortPrint(name_buffer);
6979 Vector<char> buffer = Vector<char>::New(128);
6980 SNPrintF(buffer, "TooManyFastProperties %s", name_buffer.start());
6981 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, buffer.start());
6984 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES,
6985 "TooManyFastProperties");
6992 Handle<Map> Map::ReconfigureExistingProperty(Handle<Map> map, int descriptor,
6994 PropertyAttributes attributes) {
6995 // Dictionaries have to be reconfigured in-place.
6996 DCHECK(!map->is_dictionary_map());
6998 if (!map->GetBackPointer()->IsMap()) {
6999 // There is no benefit from reconstructing transition tree for maps without
7001 return CopyGeneralizeAllRepresentations(
7002 map, descriptor, FORCE_FIELD, kind, attributes,
7003 "GenAll_AttributesMismatchProtoMap");
7006 if (FLAG_trace_generalization) {
7007 map->PrintReconfiguration(stdout, descriptor, kind, attributes);
7010 Isolate* isolate = map->GetIsolate();
7011 Handle<Map> new_map = ReconfigureProperty(
7012 map, descriptor, kind, attributes, Representation::None(),
7013 HeapType::None(isolate), FORCE_FIELD);
7018 Handle<Map> Map::TransitionToAccessorProperty(Handle<Map> map,
7020 AccessorComponent component,
7021 Handle<Object> accessor,
7022 PropertyAttributes attributes) {
7023 Isolate* isolate = name->GetIsolate();
7025 // Dictionary maps can always have additional data properties.
7026 if (map->is_dictionary_map()) return map;
7028 // Migrate to the newest map before transitioning to the new property.
7031 PropertyNormalizationMode mode = map->is_prototype_map()
7032 ? KEEP_INOBJECT_PROPERTIES
7033 : CLEAR_INOBJECT_PROPERTIES;
7035 Map* maybe_transition =
7036 TransitionArray::SearchTransition(*map, kAccessor, *name, attributes);
7037 if (maybe_transition != NULL) {
7038 Handle<Map> transition(maybe_transition, isolate);
7039 DescriptorArray* descriptors = transition->instance_descriptors();
7040 int descriptor = transition->LastAdded();
7041 DCHECK(descriptors->GetKey(descriptor)->Equals(*name));
7043 DCHECK_EQ(kAccessor, descriptors->GetDetails(descriptor).kind());
7044 DCHECK_EQ(attributes, descriptors->GetDetails(descriptor).attributes());
7046 Handle<Object> maybe_pair(descriptors->GetValue(descriptor), isolate);
7047 if (!maybe_pair->IsAccessorPair()) {
7048 return Map::Normalize(map, mode, "TransitionToAccessorFromNonPair");
7051 Handle<AccessorPair> pair = Handle<AccessorPair>::cast(maybe_pair);
7052 if (pair->get(component) != *accessor) {
7053 return Map::Normalize(map, mode, "TransitionToDifferentAccessor");
7059 Handle<AccessorPair> pair;
7060 DescriptorArray* old_descriptors = map->instance_descriptors();
7061 int descriptor = old_descriptors->SearchWithCache(*name, *map);
7062 if (descriptor != DescriptorArray::kNotFound) {
7063 if (descriptor != map->LastAdded()) {
7064 return Map::Normalize(map, mode, "AccessorsOverwritingNonLast");
7066 PropertyDetails old_details = old_descriptors->GetDetails(descriptor);
7067 if (old_details.type() != ACCESSOR_CONSTANT) {
7068 return Map::Normalize(map, mode, "AccessorsOverwritingNonAccessors");
7071 if (old_details.attributes() != attributes) {
7072 return Map::Normalize(map, mode, "AccessorsWithAttributes");
7075 Handle<Object> maybe_pair(old_descriptors->GetValue(descriptor), isolate);
7076 if (!maybe_pair->IsAccessorPair()) {
7077 return Map::Normalize(map, mode, "AccessorsOverwritingNonPair");
7080 Object* current = Handle<AccessorPair>::cast(maybe_pair)->get(component);
7081 if (current == *accessor) return map;
7083 if (!current->IsTheHole()) {
7084 return Map::Normalize(map, mode, "AccessorsOverwritingAccessors");
7087 pair = AccessorPair::Copy(Handle<AccessorPair>::cast(maybe_pair));
7088 } else if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors ||
7089 map->TooManyFastProperties(CERTAINLY_NOT_STORE_FROM_KEYED)) {
7090 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, "TooManyAccessors");
7092 pair = isolate->factory()->NewAccessorPair();
7095 pair->set(component, *accessor);
7096 TransitionFlag flag = INSERT_TRANSITION;
7097 AccessorConstantDescriptor new_desc(name, pair, attributes);
7098 return Map::CopyInsertDescriptor(map, &new_desc, flag);
7102 Handle<Map> Map::CopyAddDescriptor(Handle<Map> map,
7103 Descriptor* descriptor,
7104 TransitionFlag flag) {
7105 Handle<DescriptorArray> descriptors(map->instance_descriptors());
7107 // Ensure the key is unique.
7108 descriptor->KeyToUniqueName();
7110 if (flag == INSERT_TRANSITION && map->owns_descriptors() &&
7111 TransitionArray::CanHaveMoreTransitions(map)) {
7112 return ShareDescriptor(map, descriptors, descriptor);
7115 int nof = map->NumberOfOwnDescriptors();
7116 Handle<DescriptorArray> new_descriptors =
7117 DescriptorArray::CopyUpTo(descriptors, nof, 1);
7118 new_descriptors->Append(descriptor);
7120 Handle<LayoutDescriptor> new_layout_descriptor =
7121 FLAG_unbox_double_fields
7122 ? LayoutDescriptor::New(map, new_descriptors, nof + 1)
7123 : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate());
7125 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
7126 flag, descriptor->GetKey(), "CopyAddDescriptor",
7127 SIMPLE_PROPERTY_TRANSITION);
7131 Handle<Map> Map::CopyInsertDescriptor(Handle<Map> map,
7132 Descriptor* descriptor,
7133 TransitionFlag flag) {
7134 Handle<DescriptorArray> old_descriptors(map->instance_descriptors());
7136 // Ensure the key is unique.
7137 descriptor->KeyToUniqueName();
7139 // We replace the key if it is already present.
7140 int index = old_descriptors->SearchWithCache(*descriptor->GetKey(), *map);
7141 if (index != DescriptorArray::kNotFound) {
7142 return CopyReplaceDescriptor(map, old_descriptors, descriptor, index, flag);
7144 return CopyAddDescriptor(map, descriptor, flag);
7148 Handle<DescriptorArray> DescriptorArray::CopyUpTo(
7149 Handle<DescriptorArray> desc,
7150 int enumeration_index,
7152 return DescriptorArray::CopyUpToAddAttributes(
7153 desc, enumeration_index, NONE, slack);
7157 Handle<DescriptorArray> DescriptorArray::CopyUpToAddAttributes(
7158 Handle<DescriptorArray> desc,
7159 int enumeration_index,
7160 PropertyAttributes attributes,
7162 if (enumeration_index + slack == 0) {
7163 return desc->GetIsolate()->factory()->empty_descriptor_array();
7166 int size = enumeration_index;
7168 Handle<DescriptorArray> descriptors =
7169 DescriptorArray::Allocate(desc->GetIsolate(), size, slack);
7170 DescriptorArray::WhitenessWitness witness(*descriptors);
7172 if (attributes != NONE) {
7173 for (int i = 0; i < size; ++i) {
7174 Object* value = desc->GetValue(i);
7175 Name* key = desc->GetKey(i);
7176 PropertyDetails details = desc->GetDetails(i);
7177 // Bulk attribute changes never affect private properties.
7178 if (!key->IsSymbol() || !Symbol::cast(key)->is_private()) {
7179 int mask = DONT_DELETE | DONT_ENUM;
7180 // READ_ONLY is an invalid attribute for JS setters/getters.
7181 if (details.type() != ACCESSOR_CONSTANT || !value->IsAccessorPair()) {
7184 details = details.CopyAddAttributes(
7185 static_cast<PropertyAttributes>(attributes & mask));
7187 Descriptor inner_desc(
7188 handle(key), handle(value, desc->GetIsolate()), details);
7189 descriptors->Set(i, &inner_desc, witness);
7192 for (int i = 0; i < size; ++i) {
7193 descriptors->CopyFrom(i, *desc, witness);
7197 if (desc->number_of_descriptors() != enumeration_index) descriptors->Sort();
7203 Handle<Map> Map::CopyReplaceDescriptor(Handle<Map> map,
7204 Handle<DescriptorArray> descriptors,
7205 Descriptor* descriptor,
7206 int insertion_index,
7207 TransitionFlag flag) {
7208 // Ensure the key is unique.
7209 descriptor->KeyToUniqueName();
7211 Handle<Name> key = descriptor->GetKey();
7212 DCHECK(*key == descriptors->GetKey(insertion_index));
7214 Handle<DescriptorArray> new_descriptors = DescriptorArray::CopyUpTo(
7215 descriptors, map->NumberOfOwnDescriptors());
7217 new_descriptors->Replace(insertion_index, descriptor);
7218 Handle<LayoutDescriptor> new_layout_descriptor = LayoutDescriptor::New(
7219 map, new_descriptors, new_descriptors->number_of_descriptors());
7221 SimpleTransitionFlag simple_flag =
7222 (insertion_index == descriptors->number_of_descriptors() - 1)
7223 ? SIMPLE_PROPERTY_TRANSITION
7224 : PROPERTY_TRANSITION;
7225 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
7226 flag, key, "CopyReplaceDescriptor",
7231 void Map::UpdateCodeCache(Handle<Map> map,
7233 Handle<Code> code) {
7234 Isolate* isolate = map->GetIsolate();
7235 HandleScope scope(isolate);
7236 // Allocate the code cache if not present.
7237 if (map->code_cache()->IsFixedArray()) {
7238 Handle<Object> result = isolate->factory()->NewCodeCache();
7239 map->set_code_cache(*result);
7242 // Update the code cache.
7243 Handle<CodeCache> code_cache(CodeCache::cast(map->code_cache()), isolate);
7244 CodeCache::Update(code_cache, name, code);
7248 Object* Map::FindInCodeCache(Name* name, Code::Flags flags) {
7249 // Do a lookup if a code cache exists.
7250 if (!code_cache()->IsFixedArray()) {
7251 return CodeCache::cast(code_cache())->Lookup(name, flags);
7253 return GetHeap()->undefined_value();
7258 int Map::IndexInCodeCache(Object* name, Code* code) {
7259 // Get the internal index if a code cache exists.
7260 if (!code_cache()->IsFixedArray()) {
7261 return CodeCache::cast(code_cache())->GetIndex(name, code);
7267 void Map::RemoveFromCodeCache(Name* name, Code* code, int index) {
7268 // No GC is supposed to happen between a call to IndexInCodeCache and
7269 // RemoveFromCodeCache so the code cache must be there.
7270 DCHECK(!code_cache()->IsFixedArray());
7271 CodeCache::cast(code_cache())->RemoveByIndex(name, code, index);
7275 void CodeCache::Update(
7276 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
7277 // The number of monomorphic stubs for normal load/store/call IC's can grow to
7278 // a large number and therefore they need to go into a hash table. They are
7279 // used to load global properties from cells.
7280 if (code->type() == Code::NORMAL) {
7281 // Make sure that a hash table is allocated for the normal load code cache.
7282 if (code_cache->normal_type_cache()->IsUndefined()) {
7283 Handle<Object> result =
7284 CodeCacheHashTable::New(code_cache->GetIsolate(),
7285 CodeCacheHashTable::kInitialSize);
7286 code_cache->set_normal_type_cache(*result);
7288 UpdateNormalTypeCache(code_cache, name, code);
7290 DCHECK(code_cache->default_cache()->IsFixedArray());
7291 UpdateDefaultCache(code_cache, name, code);
7296 void CodeCache::UpdateDefaultCache(
7297 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
7298 // When updating the default code cache we disregard the type encoded in the
7299 // flags. This allows call constant stubs to overwrite call field
7301 Code::Flags flags = Code::RemoveTypeFromFlags(code->flags());
7303 // First check whether we can update existing code cache without
7305 Handle<FixedArray> cache = handle(code_cache->default_cache());
7306 int length = cache->length();
7308 DisallowHeapAllocation no_alloc;
7309 int deleted_index = -1;
7310 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
7311 Object* key = cache->get(i);
7312 if (key->IsNull()) {
7313 if (deleted_index < 0) deleted_index = i;
7316 if (key->IsUndefined()) {
7317 if (deleted_index >= 0) i = deleted_index;
7318 cache->set(i + kCodeCacheEntryNameOffset, *name);
7319 cache->set(i + kCodeCacheEntryCodeOffset, *code);
7322 if (name->Equals(Name::cast(key))) {
7324 Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags();
7325 if (Code::RemoveTypeFromFlags(found) == flags) {
7326 cache->set(i + kCodeCacheEntryCodeOffset, *code);
7332 // Reached the end of the code cache. If there were deleted
7333 // elements, reuse the space for the first of them.
7334 if (deleted_index >= 0) {
7335 cache->set(deleted_index + kCodeCacheEntryNameOffset, *name);
7336 cache->set(deleted_index + kCodeCacheEntryCodeOffset, *code);
7341 // Extend the code cache with some new entries (at least one). Must be a
7342 // multiple of the entry size.
7343 Isolate* isolate = cache->GetIsolate();
7344 int new_length = length + (length >> 1) + kCodeCacheEntrySize;
7345 new_length = new_length - new_length % kCodeCacheEntrySize;
7346 DCHECK((new_length % kCodeCacheEntrySize) == 0);
7347 cache = isolate->factory()->CopyFixedArrayAndGrow(cache, new_length - length);
7349 // Add the (name, code) pair to the new cache.
7350 cache->set(length + kCodeCacheEntryNameOffset, *name);
7351 cache->set(length + kCodeCacheEntryCodeOffset, *code);
7352 code_cache->set_default_cache(*cache);
7356 void CodeCache::UpdateNormalTypeCache(
7357 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
7358 // Adding a new entry can cause a new cache to be allocated.
7359 Handle<CodeCacheHashTable> cache(
7360 CodeCacheHashTable::cast(code_cache->normal_type_cache()));
7361 Handle<Object> new_cache = CodeCacheHashTable::Put(cache, name, code);
7362 code_cache->set_normal_type_cache(*new_cache);
7366 Object* CodeCache::Lookup(Name* name, Code::Flags flags) {
7367 Object* result = LookupDefaultCache(name, Code::RemoveTypeFromFlags(flags));
7368 if (result->IsCode()) {
7369 if (Code::cast(result)->flags() == flags) return result;
7370 return GetHeap()->undefined_value();
7372 return LookupNormalTypeCache(name, flags);
7376 Object* CodeCache::LookupDefaultCache(Name* name, Code::Flags flags) {
7377 FixedArray* cache = default_cache();
7378 int length = cache->length();
7379 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
7380 Object* key = cache->get(i + kCodeCacheEntryNameOffset);
7381 // Skip deleted elements.
7382 if (key->IsNull()) continue;
7383 if (key->IsUndefined()) return key;
7384 if (name->Equals(Name::cast(key))) {
7385 Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset));
7386 if (Code::RemoveTypeFromFlags(code->flags()) == flags) {
7391 return GetHeap()->undefined_value();
7395 Object* CodeCache::LookupNormalTypeCache(Name* name, Code::Flags flags) {
7396 if (!normal_type_cache()->IsUndefined()) {
7397 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
7398 return cache->Lookup(name, flags);
7400 return GetHeap()->undefined_value();
7405 int CodeCache::GetIndex(Object* name, Code* code) {
7406 if (code->type() == Code::NORMAL) {
7407 if (normal_type_cache()->IsUndefined()) return -1;
7408 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
7409 return cache->GetIndex(Name::cast(name), code->flags());
7412 FixedArray* array = default_cache();
7413 int len = array->length();
7414 for (int i = 0; i < len; i += kCodeCacheEntrySize) {
7415 if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1;
7421 void CodeCache::RemoveByIndex(Object* name, Code* code, int index) {
7422 if (code->type() == Code::NORMAL) {
7423 DCHECK(!normal_type_cache()->IsUndefined());
7424 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
7425 DCHECK(cache->GetIndex(Name::cast(name), code->flags()) == index);
7426 cache->RemoveByIndex(index);
7428 FixedArray* array = default_cache();
7429 DCHECK(array->length() >= index && array->get(index)->IsCode());
7430 // Use null instead of undefined for deleted elements to distinguish
7431 // deleted elements from unused elements. This distinction is used
7432 // when looking up in the cache and when updating the cache.
7433 DCHECK_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset);
7434 array->set_null(index - 1); // Name.
7435 array->set_null(index); // Code.
7440 // The key in the code cache hash table consists of the property name and the
7441 // code object. The actual match is on the name and the code flags. If a key
7442 // is created using the flags and not a code object it can only be used for
7443 // lookup not to create a new entry.
7444 class CodeCacheHashTableKey : public HashTableKey {
7446 CodeCacheHashTableKey(Handle<Name> name, Code::Flags flags)
7447 : name_(name), flags_(flags), code_() { }
7449 CodeCacheHashTableKey(Handle<Name> name, Handle<Code> code)
7450 : name_(name), flags_(code->flags()), code_(code) { }
7452 bool IsMatch(Object* other) override {
7453 if (!other->IsFixedArray()) return false;
7454 FixedArray* pair = FixedArray::cast(other);
7455 Name* name = Name::cast(pair->get(0));
7456 Code::Flags flags = Code::cast(pair->get(1))->flags();
7457 if (flags != flags_) {
7460 return name_->Equals(name);
7463 static uint32_t NameFlagsHashHelper(Name* name, Code::Flags flags) {
7464 return name->Hash() ^ flags;
7467 uint32_t Hash() override { return NameFlagsHashHelper(*name_, flags_); }
7469 uint32_t HashForObject(Object* obj) override {
7470 FixedArray* pair = FixedArray::cast(obj);
7471 Name* name = Name::cast(pair->get(0));
7472 Code* code = Code::cast(pair->get(1));
7473 return NameFlagsHashHelper(name, code->flags());
7476 MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override {
7477 Handle<Code> code = code_.ToHandleChecked();
7478 Handle<FixedArray> pair = isolate->factory()->NewFixedArray(2);
7479 pair->set(0, *name_);
7480 pair->set(1, *code);
7487 // TODO(jkummerow): We should be able to get by without this.
7488 MaybeHandle<Code> code_;
7492 Object* CodeCacheHashTable::Lookup(Name* name, Code::Flags flags) {
7493 DisallowHeapAllocation no_alloc;
7494 CodeCacheHashTableKey key(handle(name), flags);
7495 int entry = FindEntry(&key);
7496 if (entry == kNotFound) return GetHeap()->undefined_value();
7497 return get(EntryToIndex(entry) + 1);
7501 Handle<CodeCacheHashTable> CodeCacheHashTable::Put(
7502 Handle<CodeCacheHashTable> cache, Handle<Name> name, Handle<Code> code) {
7503 CodeCacheHashTableKey key(name, code);
7505 Handle<CodeCacheHashTable> new_cache = EnsureCapacity(cache, 1, &key);
7507 int entry = new_cache->FindInsertionEntry(key.Hash());
7508 Handle<Object> k = key.AsHandle(cache->GetIsolate());
7510 new_cache->set(EntryToIndex(entry), *k);
7511 new_cache->set(EntryToIndex(entry) + 1, *code);
7512 new_cache->ElementAdded();
7517 int CodeCacheHashTable::GetIndex(Name* name, Code::Flags flags) {
7518 DisallowHeapAllocation no_alloc;
7519 CodeCacheHashTableKey key(handle(name), flags);
7520 int entry = FindEntry(&key);
7521 return (entry == kNotFound) ? -1 : entry;
7525 void CodeCacheHashTable::RemoveByIndex(int index) {
7527 Heap* heap = GetHeap();
7528 set(EntryToIndex(index), heap->the_hole_value());
7529 set(EntryToIndex(index) + 1, heap->the_hole_value());
7534 void PolymorphicCodeCache::Update(Handle<PolymorphicCodeCache> code_cache,
7535 MapHandleList* maps,
7537 Handle<Code> code) {
7538 Isolate* isolate = code_cache->GetIsolate();
7539 if (code_cache->cache()->IsUndefined()) {
7540 Handle<PolymorphicCodeCacheHashTable> result =
7541 PolymorphicCodeCacheHashTable::New(
7543 PolymorphicCodeCacheHashTable::kInitialSize);
7544 code_cache->set_cache(*result);
7546 // This entry shouldn't be contained in the cache yet.
7547 DCHECK(PolymorphicCodeCacheHashTable::cast(code_cache->cache())
7548 ->Lookup(maps, flags)->IsUndefined());
7550 Handle<PolymorphicCodeCacheHashTable> hash_table =
7551 handle(PolymorphicCodeCacheHashTable::cast(code_cache->cache()));
7552 Handle<PolymorphicCodeCacheHashTable> new_cache =
7553 PolymorphicCodeCacheHashTable::Put(hash_table, maps, flags, code);
7554 code_cache->set_cache(*new_cache);
7558 Handle<Object> PolymorphicCodeCache::Lookup(MapHandleList* maps,
7559 Code::Flags flags) {
7560 if (!cache()->IsUndefined()) {
7561 PolymorphicCodeCacheHashTable* hash_table =
7562 PolymorphicCodeCacheHashTable::cast(cache());
7563 return Handle<Object>(hash_table->Lookup(maps, flags), GetIsolate());
7565 return GetIsolate()->factory()->undefined_value();
7570 // Despite their name, object of this class are not stored in the actual
7571 // hash table; instead they're temporarily used for lookups. It is therefore
7572 // safe to have a weak (non-owning) pointer to a MapList as a member field.
7573 class PolymorphicCodeCacheHashTableKey : public HashTableKey {
7575 // Callers must ensure that |maps| outlives the newly constructed object.
7576 PolymorphicCodeCacheHashTableKey(MapHandleList* maps, int code_flags)
7578 code_flags_(code_flags) {}
7580 bool IsMatch(Object* other) override {
7581 MapHandleList other_maps(kDefaultListAllocationSize);
7583 FromObject(other, &other_flags, &other_maps);
7584 if (code_flags_ != other_flags) return false;
7585 if (maps_->length() != other_maps.length()) return false;
7586 // Compare just the hashes first because it's faster.
7587 int this_hash = MapsHashHelper(maps_, code_flags_);
7588 int other_hash = MapsHashHelper(&other_maps, other_flags);
7589 if (this_hash != other_hash) return false;
7591 // Full comparison: for each map in maps_, look for an equivalent map in
7592 // other_maps. This implementation is slow, but probably good enough for
7593 // now because the lists are short (<= 4 elements currently).
7594 for (int i = 0; i < maps_->length(); ++i) {
7595 bool match_found = false;
7596 for (int j = 0; j < other_maps.length(); ++j) {
7597 if (*(maps_->at(i)) == *(other_maps.at(j))) {
7602 if (!match_found) return false;
7607 static uint32_t MapsHashHelper(MapHandleList* maps, int code_flags) {
7608 uint32_t hash = code_flags;
7609 for (int i = 0; i < maps->length(); ++i) {
7610 hash ^= maps->at(i)->Hash();
7615 uint32_t Hash() override { return MapsHashHelper(maps_, code_flags_); }
7617 uint32_t HashForObject(Object* obj) override {
7618 MapHandleList other_maps(kDefaultListAllocationSize);
7620 FromObject(obj, &other_flags, &other_maps);
7621 return MapsHashHelper(&other_maps, other_flags);
7624 MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override {
7625 // The maps in |maps_| must be copied to a newly allocated FixedArray,
7626 // both because the referenced MapList is short-lived, and because C++
7627 // objects can't be stored in the heap anyway.
7628 Handle<FixedArray> list =
7629 isolate->factory()->NewUninitializedFixedArray(maps_->length() + 1);
7630 list->set(0, Smi::FromInt(code_flags_));
7631 for (int i = 0; i < maps_->length(); ++i) {
7632 list->set(i + 1, *maps_->at(i));
7638 static MapHandleList* FromObject(Object* obj,
7640 MapHandleList* maps) {
7641 FixedArray* list = FixedArray::cast(obj);
7643 *code_flags = Smi::cast(list->get(0))->value();
7644 for (int i = 1; i < list->length(); ++i) {
7645 maps->Add(Handle<Map>(Map::cast(list->get(i))));
7650 MapHandleList* maps_; // weak.
7652 static const int kDefaultListAllocationSize = kMaxKeyedPolymorphism + 1;
7656 Object* PolymorphicCodeCacheHashTable::Lookup(MapHandleList* maps,
7658 DisallowHeapAllocation no_alloc;
7659 PolymorphicCodeCacheHashTableKey key(maps, code_kind);
7660 int entry = FindEntry(&key);
7661 if (entry == kNotFound) return GetHeap()->undefined_value();
7662 return get(EntryToIndex(entry) + 1);
7666 Handle<PolymorphicCodeCacheHashTable> PolymorphicCodeCacheHashTable::Put(
7667 Handle<PolymorphicCodeCacheHashTable> hash_table,
7668 MapHandleList* maps,
7670 Handle<Code> code) {
7671 PolymorphicCodeCacheHashTableKey key(maps, code_kind);
7672 Handle<PolymorphicCodeCacheHashTable> cache =
7673 EnsureCapacity(hash_table, 1, &key);
7674 int entry = cache->FindInsertionEntry(key.Hash());
7676 Handle<Object> obj = key.AsHandle(hash_table->GetIsolate());
7677 cache->set(EntryToIndex(entry), *obj);
7678 cache->set(EntryToIndex(entry) + 1, *code);
7679 cache->ElementAdded();
7684 void FixedArray::Shrink(int new_length) {
7685 DCHECK(0 <= new_length && new_length <= length());
7686 if (new_length < length()) {
7687 GetHeap()->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(
7688 this, length() - new_length);
7693 MaybeHandle<FixedArray> FixedArray::AddKeysFromArrayLike(
7694 Handle<FixedArray> content, Handle<JSObject> array, KeyFilter filter) {
7695 DCHECK(array->IsJSArray() || array->HasSloppyArgumentsElements());
7696 ElementsAccessor* accessor = array->GetElementsAccessor();
7697 Handle<FixedArray> result =
7698 accessor->AddElementsToFixedArray(array, content, filter);
7700 #ifdef ENABLE_SLOW_DCHECKS
7701 if (FLAG_enable_slow_asserts) {
7702 DisallowHeapAllocation no_allocation;
7703 for (int i = 0; i < result->length(); i++) {
7704 Object* current = result->get(i);
7705 DCHECK(current->IsNumber() || current->IsName());
7713 MaybeHandle<FixedArray> FixedArray::UnionOfKeys(Handle<FixedArray> first,
7714 Handle<FixedArray> second) {
7715 if (second->length() == 0) return first;
7716 if (first->length() == 0) return second;
7717 Isolate* isolate = first->GetIsolate();
7718 Handle<FixedArray> result =
7719 isolate->factory()->NewFixedArray(first->length() + second->length());
7720 for (int i = 0; i < first->length(); i++) {
7721 result->set(i, first->get(i));
7723 int pos = first->length();
7724 for (int j = 0; j < second->length(); j++) {
7725 Object* current = second->get(j);
7727 for (i = 0; i < first->length(); i++) {
7728 if (current->KeyEquals(first->get(i))) break;
7730 if (i == first->length()) {
7731 result->set(pos++, current);
7735 result->Shrink(pos);
7740 void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) {
7741 DisallowHeapAllocation no_gc;
7742 WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc);
7743 for (int index = 0; index < len; index++) {
7744 dest->set(dest_pos+index, get(pos+index), mode);
7750 bool FixedArray::IsEqualTo(FixedArray* other) {
7751 if (length() != other->length()) return false;
7752 for (int i = 0 ; i < length(); ++i) {
7753 if (get(i) != other->get(i)) return false;
7761 void WeakFixedArray::Set(Handle<WeakFixedArray> array, int index,
7762 Handle<HeapObject> value) {
7763 DCHECK(array->IsEmptySlot(index)); // Don't overwrite anything.
7764 Handle<WeakCell> cell =
7765 value->IsMap() ? Map::WeakCellForMap(Handle<Map>::cast(value))
7766 : array->GetIsolate()->factory()->NewWeakCell(value);
7767 Handle<FixedArray>::cast(array)->set(index + kFirstIndex, *cell);
7768 if (FLAG_trace_weak_arrays) {
7769 PrintF("[WeakFixedArray: storing at index %d ]\n", index);
7771 array->set_last_used_index(index);
7776 Handle<WeakFixedArray> WeakFixedArray::Add(Handle<Object> maybe_array,
7777 Handle<HeapObject> value,
7778 int* assigned_index) {
7779 Handle<WeakFixedArray> array =
7780 (maybe_array.is_null() || !maybe_array->IsWeakFixedArray())
7781 ? Allocate(value->GetIsolate(), 1, Handle<WeakFixedArray>::null())
7782 : Handle<WeakFixedArray>::cast(maybe_array);
7783 // Try to store the new entry if there's room. Optimize for consecutive
7785 int first_index = array->last_used_index();
7786 int length = array->Length();
7788 for (int i = first_index;;) {
7789 if (array->IsEmptySlot((i))) {
7790 WeakFixedArray::Set(array, i, value);
7791 if (assigned_index != NULL) *assigned_index = i;
7794 if (FLAG_trace_weak_arrays) {
7795 PrintF("[WeakFixedArray: searching for free slot]\n");
7797 i = (i + 1) % length;
7798 if (i == first_index) break;
7802 // No usable slot found, grow the array.
7803 int new_length = length == 0 ? 1 : length + (length >> 1) + 4;
7804 Handle<WeakFixedArray> new_array =
7805 Allocate(array->GetIsolate(), new_length, array);
7806 if (FLAG_trace_weak_arrays) {
7807 PrintF("[WeakFixedArray: growing to size %d ]\n", new_length);
7809 WeakFixedArray::Set(new_array, length, value);
7810 if (assigned_index != NULL) *assigned_index = length;
7815 template <class CompactionCallback>
7816 void WeakFixedArray::Compact() {
7817 FixedArray* array = FixedArray::cast(this);
7818 int new_length = kFirstIndex;
7819 for (int i = kFirstIndex; i < array->length(); i++) {
7820 Object* element = array->get(i);
7821 if (element->IsSmi()) continue;
7822 if (WeakCell::cast(element)->cleared()) continue;
7823 Object* value = WeakCell::cast(element)->value();
7824 CompactionCallback::Callback(value, i - kFirstIndex,
7825 new_length - kFirstIndex);
7826 array->set(new_length++, element);
7828 array->Shrink(new_length);
7829 set_last_used_index(0);
7833 void JSObject::PrototypeRegistryCompactionCallback::Callback(Object* value,
7836 DCHECK(value->IsMap() && Map::cast(value)->is_prototype_map());
7837 Map* map = Map::cast(value);
7838 DCHECK(map->prototype_info()->IsPrototypeInfo());
7839 PrototypeInfo* proto_info = PrototypeInfo::cast(map->prototype_info());
7840 DCHECK_EQ(old_index, proto_info->registry_slot());
7841 proto_info->set_registry_slot(new_index);
7845 template void WeakFixedArray::Compact<WeakFixedArray::NullCallback>();
7847 WeakFixedArray::Compact<JSObject::PrototypeRegistryCompactionCallback>();
7850 bool WeakFixedArray::Remove(Handle<HeapObject> value) {
7851 if (Length() == 0) return false;
7852 // Optimize for the most recently added element to be removed again.
7853 int first_index = last_used_index();
7854 for (int i = first_index;;) {
7855 if (Get(i) == *value) {
7857 // Users of WeakFixedArray should make sure that there are no duplicates.
7860 i = (i + 1) % Length();
7861 if (i == first_index) return false;
7868 Handle<WeakFixedArray> WeakFixedArray::Allocate(
7869 Isolate* isolate, int size, Handle<WeakFixedArray> initialize_from) {
7871 Handle<FixedArray> result =
7872 isolate->factory()->NewUninitializedFixedArray(size + kFirstIndex);
7873 Handle<WeakFixedArray> casted_result = Handle<WeakFixedArray>::cast(result);
7874 if (initialize_from.is_null()) {
7875 for (int i = 0; i < result->length(); ++i) {
7876 result->set(i, Smi::FromInt(0));
7879 DCHECK(initialize_from->Length() <= size);
7880 Handle<FixedArray> raw_source = Handle<FixedArray>::cast(initialize_from);
7881 int target_index = kFirstIndex;
7882 for (int source_index = kFirstIndex; source_index < raw_source->length();
7884 // The act of allocating might have caused entries in the source array
7885 // to be cleared. Copy only what's needed.
7886 if (initialize_from->IsEmptySlot(source_index - kFirstIndex)) continue;
7887 result->set(target_index++, raw_source->get(source_index));
7889 casted_result->set_last_used_index(target_index - 1 - kFirstIndex);
7890 for (; target_index < result->length(); ++target_index) {
7891 result->set(target_index, Smi::FromInt(0));
7894 return casted_result;
7898 Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj,
7900 int length = array->Length();
7901 array = EnsureSpace(array, length + 1);
7902 if (mode == kReloadLengthAfterAllocation) {
7903 DCHECK(array->Length() <= length);
7904 length = array->Length();
7906 array->Set(length, *obj);
7907 array->SetLength(length + 1);
7912 Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj1,
7913 Handle<Object> obj2, AddMode mode) {
7914 int length = array->Length();
7915 array = EnsureSpace(array, length + 2);
7916 if (mode == kReloadLengthAfterAllocation) {
7917 length = array->Length();
7919 array->Set(length, *obj1);
7920 array->Set(length + 1, *obj2);
7921 array->SetLength(length + 2);
7926 Handle<ArrayList> ArrayList::EnsureSpace(Handle<ArrayList> array, int length) {
7927 int capacity = array->length();
7928 bool empty = (capacity == 0);
7929 if (capacity < kFirstIndex + length) {
7930 Isolate* isolate = array->GetIsolate();
7931 int new_capacity = kFirstIndex + length;
7932 new_capacity = new_capacity + Max(new_capacity / 2, 2);
7933 int grow_by = new_capacity - capacity;
7934 array = Handle<ArrayList>::cast(
7935 isolate->factory()->CopyFixedArrayAndGrow(array, grow_by));
7936 if (empty) array->SetLength(0);
7942 Handle<DescriptorArray> DescriptorArray::Allocate(Isolate* isolate,
7943 int number_of_descriptors,
7945 DCHECK(0 <= number_of_descriptors);
7946 Factory* factory = isolate->factory();
7947 // Do not use DescriptorArray::cast on incomplete object.
7948 int size = number_of_descriptors + slack;
7949 if (size == 0) return factory->empty_descriptor_array();
7950 // Allocate the array of keys.
7951 Handle<FixedArray> result = factory->NewFixedArray(LengthFor(size));
7953 result->set(kDescriptorLengthIndex, Smi::FromInt(number_of_descriptors));
7954 result->set(kEnumCacheIndex, Smi::FromInt(0));
7955 return Handle<DescriptorArray>::cast(result);
7959 void DescriptorArray::ClearEnumCache() {
7960 set(kEnumCacheIndex, Smi::FromInt(0));
7964 void DescriptorArray::Replace(int index, Descriptor* descriptor) {
7965 descriptor->SetSortedKeyIndex(GetSortedKeyIndex(index));
7966 Set(index, descriptor);
7970 void DescriptorArray::SetEnumCache(FixedArray* bridge_storage,
7971 FixedArray* new_cache,
7972 Object* new_index_cache) {
7973 DCHECK(bridge_storage->length() >= kEnumCacheBridgeLength);
7974 DCHECK(new_index_cache->IsSmi() || new_index_cache->IsFixedArray());
7976 DCHECK(!HasEnumCache() || new_cache->length() > GetEnumCache()->length());
7977 FixedArray::cast(bridge_storage)->
7978 set(kEnumCacheBridgeCacheIndex, new_cache);
7979 FixedArray::cast(bridge_storage)->
7980 set(kEnumCacheBridgeIndicesCacheIndex, new_index_cache);
7981 set(kEnumCacheIndex, bridge_storage);
7985 void DescriptorArray::CopyFrom(int index, DescriptorArray* src,
7986 const WhitenessWitness& witness) {
7987 Object* value = src->GetValue(index);
7988 PropertyDetails details = src->GetDetails(index);
7989 Descriptor desc(handle(src->GetKey(index)),
7990 handle(value, src->GetIsolate()),
7992 Set(index, &desc, witness);
7996 // We need the whiteness witness since sort will reshuffle the entries in the
7997 // descriptor array. If the descriptor array were to be black, the shuffling
7998 // would move a slot that was already recorded as pointing into an evacuation
7999 // candidate. This would result in missing updates upon evacuation.
8000 void DescriptorArray::Sort() {
8001 // In-place heap sort.
8002 int len = number_of_descriptors();
8003 // Reset sorting since the descriptor array might contain invalid pointers.
8004 for (int i = 0; i < len; ++i) SetSortedKey(i, i);
8005 // Bottom-up max-heap construction.
8006 // Index of the last node with children
8007 const int max_parent_index = (len / 2) - 1;
8008 for (int i = max_parent_index; i >= 0; --i) {
8009 int parent_index = i;
8010 const uint32_t parent_hash = GetSortedKey(i)->Hash();
8011 while (parent_index <= max_parent_index) {
8012 int child_index = 2 * parent_index + 1;
8013 uint32_t child_hash = GetSortedKey(child_index)->Hash();
8014 if (child_index + 1 < len) {
8015 uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash();
8016 if (right_child_hash > child_hash) {
8018 child_hash = right_child_hash;
8021 if (child_hash <= parent_hash) break;
8022 SwapSortedKeys(parent_index, child_index);
8023 // Now element at child_index could be < its children.
8024 parent_index = child_index; // parent_hash remains correct.
8028 // Extract elements and create sorted array.
8029 for (int i = len - 1; i > 0; --i) {
8030 // Put max element at the back of the array.
8031 SwapSortedKeys(0, i);
8032 // Shift down the new top element.
8033 int parent_index = 0;
8034 const uint32_t parent_hash = GetSortedKey(parent_index)->Hash();
8035 const int max_parent_index = (i / 2) - 1;
8036 while (parent_index <= max_parent_index) {
8037 int child_index = parent_index * 2 + 1;
8038 uint32_t child_hash = GetSortedKey(child_index)->Hash();
8039 if (child_index + 1 < i) {
8040 uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash();
8041 if (right_child_hash > child_hash) {
8043 child_hash = right_child_hash;
8046 if (child_hash <= parent_hash) break;
8047 SwapSortedKeys(parent_index, child_index);
8048 parent_index = child_index;
8051 DCHECK(IsSortedNoDuplicates());
8055 Handle<AccessorPair> AccessorPair::Copy(Handle<AccessorPair> pair) {
8056 Handle<AccessorPair> copy = pair->GetIsolate()->factory()->NewAccessorPair();
8057 copy->set_getter(pair->getter());
8058 copy->set_setter(pair->setter());
8063 Object* AccessorPair::GetComponent(AccessorComponent component) {
8064 Object* accessor = get(component);
8065 return accessor->IsTheHole() ? GetHeap()->undefined_value() : accessor;
8069 Handle<DeoptimizationInputData> DeoptimizationInputData::New(
8070 Isolate* isolate, int deopt_entry_count, PretenureFlag pretenure) {
8071 return Handle<DeoptimizationInputData>::cast(
8072 isolate->factory()->NewFixedArray(LengthFor(deopt_entry_count),
8077 Handle<DeoptimizationOutputData> DeoptimizationOutputData::New(
8079 int number_of_deopt_points,
8080 PretenureFlag pretenure) {
8081 Handle<FixedArray> result;
8082 if (number_of_deopt_points == 0) {
8083 result = isolate->factory()->empty_fixed_array();
8085 result = isolate->factory()->NewFixedArray(
8086 LengthOfFixedArray(number_of_deopt_points), pretenure);
8088 return Handle<DeoptimizationOutputData>::cast(result);
8092 int HandlerTable::LookupRange(int pc_offset, int* stack_depth_out,
8093 CatchPrediction* prediction_out) {
8094 int innermost_handler = -1, innermost_start = -1;
8095 for (int i = 0; i < length(); i += kRangeEntrySize) {
8096 int start_offset = Smi::cast(get(i + kRangeStartIndex))->value();
8097 int end_offset = Smi::cast(get(i + kRangeEndIndex))->value();
8098 int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value();
8099 int handler_offset = HandlerOffsetField::decode(handler_field);
8100 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
8101 int stack_depth = Smi::cast(get(i + kRangeDepthIndex))->value();
8102 if (pc_offset > start_offset && pc_offset <= end_offset) {
8103 DCHECK_NE(start_offset, innermost_start);
8104 if (start_offset < innermost_start) continue;
8105 innermost_handler = handler_offset;
8106 innermost_start = start_offset;
8107 *stack_depth_out = stack_depth;
8108 if (prediction_out) *prediction_out = prediction;
8111 return innermost_handler;
8115 // TODO(turbofan): Make sure table is sorted and use binary search.
8116 int HandlerTable::LookupReturn(int pc_offset, CatchPrediction* prediction_out) {
8117 for (int i = 0; i < length(); i += kReturnEntrySize) {
8118 int return_offset = Smi::cast(get(i + kReturnOffsetIndex))->value();
8119 int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value();
8120 if (pc_offset == return_offset) {
8121 if (prediction_out) {
8122 *prediction_out = HandlerPredictionField::decode(handler_field);
8124 return HandlerOffsetField::decode(handler_field);
8132 bool DescriptorArray::IsEqualTo(DescriptorArray* other) {
8133 if (IsEmpty()) return other->IsEmpty();
8134 if (other->IsEmpty()) return false;
8135 if (length() != other->length()) return false;
8136 for (int i = 0; i < length(); ++i) {
8137 if (get(i) != other->get(i)) return false;
8144 bool String::LooksValid() {
8145 if (!GetIsolate()->heap()->Contains(this)) return false;
8150 String::FlatContent String::GetFlatContent() {
8151 DCHECK(!AllowHeapAllocation::IsAllowed());
8152 int length = this->length();
8153 StringShape shape(this);
8154 String* string = this;
8156 if (shape.representation_tag() == kConsStringTag) {
8157 ConsString* cons = ConsString::cast(string);
8158 if (cons->second()->length() != 0) {
8159 return FlatContent();
8161 string = cons->first();
8162 shape = StringShape(string);
8164 if (shape.representation_tag() == kSlicedStringTag) {
8165 SlicedString* slice = SlicedString::cast(string);
8166 offset = slice->offset();
8167 string = slice->parent();
8168 shape = StringShape(string);
8169 DCHECK(shape.representation_tag() != kConsStringTag &&
8170 shape.representation_tag() != kSlicedStringTag);
8172 if (shape.encoding_tag() == kOneByteStringTag) {
8173 const uint8_t* start;
8174 if (shape.representation_tag() == kSeqStringTag) {
8175 start = SeqOneByteString::cast(string)->GetChars();
8177 start = ExternalOneByteString::cast(string)->GetChars();
8179 return FlatContent(start + offset, length);
8181 DCHECK(shape.encoding_tag() == kTwoByteStringTag);
8183 if (shape.representation_tag() == kSeqStringTag) {
8184 start = SeqTwoByteString::cast(string)->GetChars();
8186 start = ExternalTwoByteString::cast(string)->GetChars();
8188 return FlatContent(start + offset, length);
8193 base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
8194 RobustnessFlag robust_flag,
8195 int offset, int length,
8196 int* length_return) {
8197 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
8198 return base::SmartArrayPointer<char>(NULL);
8200 // Negative length means the to the end of the string.
8201 if (length < 0) length = kMaxInt - offset;
8203 // Compute the size of the UTF-8 string. Start at the specified offset.
8204 StringCharacterStream stream(this, offset);
8205 int character_position = offset;
8207 int last = unibrow::Utf16::kNoPreviousCharacter;
8208 while (stream.HasMore() && character_position++ < offset + length) {
8209 uint16_t character = stream.GetNext();
8210 utf8_bytes += unibrow::Utf8::Length(character, last);
8214 if (length_return) {
8215 *length_return = utf8_bytes;
8218 char* result = NewArray<char>(utf8_bytes + 1);
8220 // Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset.
8221 stream.Reset(this, offset);
8222 character_position = offset;
8223 int utf8_byte_position = 0;
8224 last = unibrow::Utf16::kNoPreviousCharacter;
8225 while (stream.HasMore() && character_position++ < offset + length) {
8226 uint16_t character = stream.GetNext();
8227 if (allow_nulls == DISALLOW_NULLS && character == 0) {
8230 utf8_byte_position +=
8231 unibrow::Utf8::Encode(result + utf8_byte_position, character, last);
8234 result[utf8_byte_position] = 0;
8235 return base::SmartArrayPointer<char>(result);
8239 base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
8240 RobustnessFlag robust_flag,
8241 int* length_return) {
8242 return ToCString(allow_nulls, robust_flag, 0, -1, length_return);
8246 const uc16* String::GetTwoByteData(unsigned start) {
8247 DCHECK(!IsOneByteRepresentationUnderneath());
8248 switch (StringShape(this).representation_tag()) {
8250 return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start);
8251 case kExternalStringTag:
8252 return ExternalTwoByteString::cast(this)->
8253 ExternalTwoByteStringGetData(start);
8254 case kSlicedStringTag: {
8255 SlicedString* slice = SlicedString::cast(this);
8256 return slice->parent()->GetTwoByteData(start + slice->offset());
8258 case kConsStringTag:
8267 base::SmartArrayPointer<uc16> String::ToWideCString(
8268 RobustnessFlag robust_flag) {
8269 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
8270 return base::SmartArrayPointer<uc16>();
8272 StringCharacterStream stream(this);
8274 uc16* result = NewArray<uc16>(length() + 1);
8277 while (stream.HasMore()) {
8278 uint16_t character = stream.GetNext();
8279 result[i++] = character;
8282 return base::SmartArrayPointer<uc16>(result);
8286 const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) {
8287 return reinterpret_cast<uc16*>(
8288 reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start;
8292 void Relocatable::PostGarbageCollectionProcessing(Isolate* isolate) {
8293 Relocatable* current = isolate->relocatable_top();
8294 while (current != NULL) {
8295 current->PostGarbageCollection();
8296 current = current->prev_;
8301 // Reserve space for statics needing saving and restoring.
8302 int Relocatable::ArchiveSpacePerThread() {
8303 return sizeof(Relocatable*); // NOLINT
8307 // Archive statics that are thread-local.
8308 char* Relocatable::ArchiveState(Isolate* isolate, char* to) {
8309 *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top();
8310 isolate->set_relocatable_top(NULL);
8311 return to + ArchiveSpacePerThread();
8315 // Restore statics that are thread-local.
8316 char* Relocatable::RestoreState(Isolate* isolate, char* from) {
8317 isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from));
8318 return from + ArchiveSpacePerThread();
8322 char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) {
8323 Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage);
8325 return thread_storage + ArchiveSpacePerThread();
8329 void Relocatable::Iterate(Isolate* isolate, ObjectVisitor* v) {
8330 Iterate(v, isolate->relocatable_top());
8334 void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) {
8335 Relocatable* current = top;
8336 while (current != NULL) {
8337 current->IterateInstance(v);
8338 current = current->prev_;
8343 FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str)
8344 : Relocatable(isolate),
8345 str_(str.location()),
8346 length_(str->length()) {
8347 PostGarbageCollection();
8351 FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input)
8352 : Relocatable(isolate),
8355 length_(input.length()),
8356 start_(input.start()) {}
8359 void FlatStringReader::PostGarbageCollection() {
8360 if (str_ == NULL) return;
8361 Handle<String> str(str_);
8362 DCHECK(str->IsFlat());
8363 DisallowHeapAllocation no_gc;
8364 // This does not actually prevent the vector from being relocated later.
8365 String::FlatContent content = str->GetFlatContent();
8366 DCHECK(content.IsFlat());
8367 is_one_byte_ = content.IsOneByte();
8369 start_ = content.ToOneByteVector().start();
8371 start_ = content.ToUC16Vector().start();
8376 void ConsStringIterator::Initialize(ConsString* cons_string, int offset) {
8377 DCHECK(cons_string != NULL);
8378 root_ = cons_string;
8380 // Force stack blown condition to trigger restart.
8382 maximum_depth_ = kStackSize + depth_;
8383 DCHECK(StackBlown());
8387 String* ConsStringIterator::Continue(int* offset_out) {
8388 DCHECK(depth_ != 0);
8389 DCHECK_EQ(0, *offset_out);
8390 bool blew_stack = StackBlown();
8391 String* string = NULL;
8392 // Get the next leaf if there is one.
8393 if (!blew_stack) string = NextLeaf(&blew_stack);
8394 // Restart search from root.
8396 DCHECK(string == NULL);
8397 string = Search(offset_out);
8399 // Ensure future calls return null immediately.
8400 if (string == NULL) Reset(NULL);
8405 String* ConsStringIterator::Search(int* offset_out) {
8406 ConsString* cons_string = root_;
8407 // Reset the stack, pushing the root string.
8410 frames_[0] = cons_string;
8411 const int consumed = consumed_;
8414 // Loop until the string is found which contains the target offset.
8415 String* string = cons_string->first();
8416 int length = string->length();
8418 if (consumed < offset + length) {
8419 // Target offset is in the left branch.
8420 // Keep going if we're still in a ConString.
8421 type = string->map()->instance_type();
8422 if ((type & kStringRepresentationMask) == kConsStringTag) {
8423 cons_string = ConsString::cast(string);
8424 PushLeft(cons_string);
8427 // Tell the stack we're done descending.
8428 AdjustMaximumDepth();
8431 // Update progress through the string.
8433 // Keep going if we're still in a ConString.
8434 string = cons_string->second();
8435 type = string->map()->instance_type();
8436 if ((type & kStringRepresentationMask) == kConsStringTag) {
8437 cons_string = ConsString::cast(string);
8438 PushRight(cons_string);
8441 // Need this to be updated for the current string.
8442 length = string->length();
8443 // Account for the possibility of an empty right leaf.
8444 // This happens only if we have asked for an offset outside the string.
8446 // Reset so future operations will return null immediately.
8450 // Tell the stack we're done descending.
8451 AdjustMaximumDepth();
8452 // Pop stack so next iteration is in correct place.
8455 DCHECK(length != 0);
8456 // Adjust return values and exit.
8457 consumed_ = offset + length;
8458 *offset_out = consumed - offset;
8466 String* ConsStringIterator::NextLeaf(bool* blew_stack) {
8468 // Tree traversal complete.
8470 *blew_stack = false;
8473 // We've lost track of higher nodes.
8479 ConsString* cons_string = frames_[OffsetForDepth(depth_ - 1)];
8480 String* string = cons_string->second();
8481 int32_t type = string->map()->instance_type();
8482 if ((type & kStringRepresentationMask) != kConsStringTag) {
8483 // Pop stack so next iteration is in correct place.
8485 int length = string->length();
8486 // Could be a flattened ConsString.
8487 if (length == 0) continue;
8488 consumed_ += length;
8491 cons_string = ConsString::cast(string);
8492 PushRight(cons_string);
8493 // Need to traverse all the way left.
8496 string = cons_string->first();
8497 type = string->map()->instance_type();
8498 if ((type & kStringRepresentationMask) != kConsStringTag) {
8499 AdjustMaximumDepth();
8500 int length = string->length();
8501 DCHECK(length != 0);
8502 consumed_ += length;
8505 cons_string = ConsString::cast(string);
8506 PushLeft(cons_string);
8514 uint16_t ConsString::ConsStringGet(int index) {
8515 DCHECK(index >= 0 && index < this->length());
8517 // Check for a flattened cons string
8518 if (second()->length() == 0) {
8519 String* left = first();
8520 return left->Get(index);
8523 String* string = String::cast(this);
8526 if (StringShape(string).IsCons()) {
8527 ConsString* cons_string = ConsString::cast(string);
8528 String* left = cons_string->first();
8529 if (left->length() > index) {
8532 index -= left->length();
8533 string = cons_string->second();
8536 return string->Get(index);
8545 uint16_t SlicedString::SlicedStringGet(int index) {
8546 return parent()->Get(offset() + index);
8550 template <typename sinkchar>
8551 void String::WriteToFlat(String* src,
8555 String* source = src;
8559 DCHECK(0 <= from && from <= to && to <= source->length());
8560 switch (StringShape(source).full_representation_tag()) {
8561 case kOneByteStringTag | kExternalStringTag: {
8562 CopyChars(sink, ExternalOneByteString::cast(source)->GetChars() + from,
8566 case kTwoByteStringTag | kExternalStringTag: {
8568 ExternalTwoByteString::cast(source)->GetChars();
8574 case kOneByteStringTag | kSeqStringTag: {
8576 SeqOneByteString::cast(source)->GetChars() + from,
8580 case kTwoByteStringTag | kSeqStringTag: {
8582 SeqTwoByteString::cast(source)->GetChars() + from,
8586 case kOneByteStringTag | kConsStringTag:
8587 case kTwoByteStringTag | kConsStringTag: {
8588 ConsString* cons_string = ConsString::cast(source);
8589 String* first = cons_string->first();
8590 int boundary = first->length();
8591 if (to - boundary >= boundary - from) {
8592 // Right hand side is longer. Recurse over left.
8593 if (from < boundary) {
8594 WriteToFlat(first, sink, from, boundary);
8595 sink += boundary - from;
8601 source = cons_string->second();
8603 // Left hand side is longer. Recurse over right.
8604 if (to > boundary) {
8605 String* second = cons_string->second();
8606 // When repeatedly appending to a string, we get a cons string that
8607 // is unbalanced to the left, a list, essentially. We inline the
8608 // common case of sequential one-byte right child.
8609 if (to - boundary == 1) {
8610 sink[boundary - from] = static_cast<sinkchar>(second->Get(0));
8611 } else if (second->IsSeqOneByteString()) {
8612 CopyChars(sink + boundary - from,
8613 SeqOneByteString::cast(second)->GetChars(),
8617 sink + boundary - from,
8627 case kOneByteStringTag | kSlicedStringTag:
8628 case kTwoByteStringTag | kSlicedStringTag: {
8629 SlicedString* slice = SlicedString::cast(source);
8630 unsigned offset = slice->offset();
8631 WriteToFlat(slice->parent(), sink, from + offset, to + offset);
8640 template <typename SourceChar>
8641 static void CalculateLineEndsImpl(Isolate* isolate,
8642 List<int>* line_ends,
8643 Vector<const SourceChar> src,
8644 bool include_ending_line) {
8645 const int src_len = src.length();
8646 UnicodeCache* cache = isolate->unicode_cache();
8647 for (int i = 0; i < src_len - 1; i++) {
8648 SourceChar current = src[i];
8649 SourceChar next = src[i + 1];
8650 if (cache->IsLineTerminatorSequence(current, next)) line_ends->Add(i);
8653 if (src_len > 0 && cache->IsLineTerminatorSequence(src[src_len - 1], 0)) {
8654 line_ends->Add(src_len - 1);
8655 } else if (include_ending_line) {
8656 // Even if the last line misses a line end, it is counted.
8657 line_ends->Add(src_len);
8662 Handle<FixedArray> String::CalculateLineEnds(Handle<String> src,
8663 bool include_ending_line) {
8665 // Rough estimate of line count based on a roughly estimated average
8666 // length of (unpacked) code.
8667 int line_count_estimate = src->length() >> 4;
8668 List<int> line_ends(line_count_estimate);
8669 Isolate* isolate = src->GetIsolate();
8670 { DisallowHeapAllocation no_allocation; // ensure vectors stay valid.
8671 // Dispatch on type of strings.
8672 String::FlatContent content = src->GetFlatContent();
8673 DCHECK(content.IsFlat());
8674 if (content.IsOneByte()) {
8675 CalculateLineEndsImpl(isolate,
8677 content.ToOneByteVector(),
8678 include_ending_line);
8680 CalculateLineEndsImpl(isolate,
8682 content.ToUC16Vector(),
8683 include_ending_line);
8686 int line_count = line_ends.length();
8687 Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count);
8688 for (int i = 0; i < line_count; i++) {
8689 array->set(i, Smi::FromInt(line_ends[i]));
8695 // Compares the contents of two strings by reading and comparing
8696 // int-sized blocks of characters.
8697 template <typename Char>
8698 static inline bool CompareRawStringContents(const Char* const a,
8699 const Char* const b,
8701 return CompareChars(a, b, length) == 0;
8705 template<typename Chars1, typename Chars2>
8706 class RawStringComparator : public AllStatic {
8708 static inline bool compare(const Chars1* a, const Chars2* b, int len) {
8709 DCHECK(sizeof(Chars1) != sizeof(Chars2));
8710 for (int i = 0; i < len; i++) {
8721 class RawStringComparator<uint16_t, uint16_t> {
8723 static inline bool compare(const uint16_t* a, const uint16_t* b, int len) {
8724 return CompareRawStringContents(a, b, len);
8730 class RawStringComparator<uint8_t, uint8_t> {
8732 static inline bool compare(const uint8_t* a, const uint8_t* b, int len) {
8733 return CompareRawStringContents(a, b, len);
8738 class StringComparator {
8741 State() : is_one_byte_(true), length_(0), buffer8_(NULL) {}
8743 void Init(String* string) {
8744 ConsString* cons_string = String::VisitFlat(this, string);
8745 iter_.Reset(cons_string);
8746 if (cons_string != NULL) {
8748 string = iter_.Next(&offset);
8749 String::VisitFlat(this, string, offset);
8753 inline void VisitOneByteString(const uint8_t* chars, int length) {
8754 is_one_byte_ = true;
8759 inline void VisitTwoByteString(const uint16_t* chars, int length) {
8760 is_one_byte_ = false;
8765 void Advance(int consumed) {
8766 DCHECK(consumed <= length_);
8768 if (length_ != consumed) {
8770 buffer8_ += consumed;
8772 buffer16_ += consumed;
8774 length_ -= consumed;
8779 String* next = iter_.Next(&offset);
8780 DCHECK_EQ(0, offset);
8781 DCHECK(next != NULL);
8782 String::VisitFlat(this, next);
8785 ConsStringIterator iter_;
8789 const uint8_t* buffer8_;
8790 const uint16_t* buffer16_;
8794 DISALLOW_COPY_AND_ASSIGN(State);
8798 inline StringComparator() {}
8800 template<typename Chars1, typename Chars2>
8801 static inline bool Equals(State* state_1, State* state_2, int to_check) {
8802 const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_);
8803 const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_);
8804 return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check);
8807 bool Equals(String* string_1, String* string_2) {
8808 int length = string_1->length();
8809 state_1_.Init(string_1);
8810 state_2_.Init(string_2);
8812 int to_check = Min(state_1_.length_, state_2_.length_);
8813 DCHECK(to_check > 0 && to_check <= length);
8815 if (state_1_.is_one_byte_) {
8816 if (state_2_.is_one_byte_) {
8817 is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check);
8819 is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check);
8822 if (state_2_.is_one_byte_) {
8823 is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check);
8825 is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check);
8829 if (!is_equal) return false;
8831 // Exit condition. Strings are equal.
8832 if (length == 0) return true;
8833 state_1_.Advance(to_check);
8834 state_2_.Advance(to_check);
8842 DISALLOW_COPY_AND_ASSIGN(StringComparator);
8846 bool String::SlowEquals(String* other) {
8847 DisallowHeapAllocation no_gc;
8848 // Fast check: negative check with lengths.
8850 if (len != other->length()) return false;
8851 if (len == 0) return true;
8853 // Fast check: if hash code is computed for both strings
8854 // a fast negative check can be performed.
8855 if (HasHashCode() && other->HasHashCode()) {
8856 #ifdef ENABLE_SLOW_DCHECKS
8857 if (FLAG_enable_slow_asserts) {
8858 if (Hash() != other->Hash()) {
8859 bool found_difference = false;
8860 for (int i = 0; i < len; i++) {
8861 if (Get(i) != other->Get(i)) {
8862 found_difference = true;
8866 DCHECK(found_difference);
8870 if (Hash() != other->Hash()) return false;
8873 // We know the strings are both non-empty. Compare the first chars
8874 // before we try to flatten the strings.
8875 if (this->Get(0) != other->Get(0)) return false;
8877 if (IsSeqOneByteString() && other->IsSeqOneByteString()) {
8878 const uint8_t* str1 = SeqOneByteString::cast(this)->GetChars();
8879 const uint8_t* str2 = SeqOneByteString::cast(other)->GetChars();
8880 return CompareRawStringContents(str1, str2, len);
8883 StringComparator comparator;
8884 return comparator.Equals(this, other);
8888 bool String::SlowEquals(Handle<String> one, Handle<String> two) {
8889 // Fast check: negative check with lengths.
8890 int one_length = one->length();
8891 if (one_length != two->length()) return false;
8892 if (one_length == 0) return true;
8894 // Fast check: if hash code is computed for both strings
8895 // a fast negative check can be performed.
8896 if (one->HasHashCode() && two->HasHashCode()) {
8897 #ifdef ENABLE_SLOW_DCHECKS
8898 if (FLAG_enable_slow_asserts) {
8899 if (one->Hash() != two->Hash()) {
8900 bool found_difference = false;
8901 for (int i = 0; i < one_length; i++) {
8902 if (one->Get(i) != two->Get(i)) {
8903 found_difference = true;
8907 DCHECK(found_difference);
8911 if (one->Hash() != two->Hash()) return false;
8914 // We know the strings are both non-empty. Compare the first chars
8915 // before we try to flatten the strings.
8916 if (one->Get(0) != two->Get(0)) return false;
8918 one = String::Flatten(one);
8919 two = String::Flatten(two);
8921 DisallowHeapAllocation no_gc;
8922 String::FlatContent flat1 = one->GetFlatContent();
8923 String::FlatContent flat2 = two->GetFlatContent();
8925 if (flat1.IsOneByte() && flat2.IsOneByte()) {
8926 return CompareRawStringContents(flat1.ToOneByteVector().start(),
8927 flat2.ToOneByteVector().start(),
8930 for (int i = 0; i < one_length; i++) {
8931 if (flat1.Get(i) != flat2.Get(i)) return false;
8938 bool String::IsUtf8EqualTo(Vector<const char> str, bool allow_prefix_match) {
8939 int slen = length();
8940 // Can't check exact length equality, but we can check bounds.
8941 int str_len = str.length();
8942 if (!allow_prefix_match &&
8944 str_len > slen*static_cast<int>(unibrow::Utf8::kMaxEncodedSize))) {
8948 size_t remaining_in_str = static_cast<size_t>(str_len);
8949 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(str.start());
8950 for (i = 0; i < slen && remaining_in_str > 0; i++) {
8952 uint32_t r = unibrow::Utf8::ValueOf(utf8_data, remaining_in_str, &cursor);
8953 DCHECK(cursor > 0 && cursor <= remaining_in_str);
8954 if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) {
8955 if (i > slen - 1) return false;
8956 if (Get(i++) != unibrow::Utf16::LeadSurrogate(r)) return false;
8957 if (Get(i) != unibrow::Utf16::TrailSurrogate(r)) return false;
8959 if (Get(i) != r) return false;
8961 utf8_data += cursor;
8962 remaining_in_str -= cursor;
8964 return (allow_prefix_match || i == slen) && remaining_in_str == 0;
8968 bool String::IsOneByteEqualTo(Vector<const uint8_t> str) {
8969 int slen = length();
8970 if (str.length() != slen) return false;
8971 DisallowHeapAllocation no_gc;
8972 FlatContent content = GetFlatContent();
8973 if (content.IsOneByte()) {
8974 return CompareChars(content.ToOneByteVector().start(),
8975 str.start(), slen) == 0;
8977 for (int i = 0; i < slen; i++) {
8978 if (Get(i) != static_cast<uint16_t>(str[i])) return false;
8984 bool String::IsTwoByteEqualTo(Vector<const uc16> str) {
8985 int slen = length();
8986 if (str.length() != slen) return false;
8987 DisallowHeapAllocation no_gc;
8988 FlatContent content = GetFlatContent();
8989 if (content.IsTwoByte()) {
8990 return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0;
8992 for (int i = 0; i < slen; i++) {
8993 if (Get(i) != str[i]) return false;
8999 uint32_t String::ComputeAndSetHash() {
9000 // Should only be called if hash code has not yet been computed.
9001 DCHECK(!HasHashCode());
9003 // Store the hash code in the object.
9004 uint32_t field = IteratingStringHasher::Hash(this, GetHeap()->HashSeed());
9005 set_hash_field(field);
9007 // Check the hash code is there.
9008 DCHECK(HasHashCode());
9009 uint32_t result = field >> kHashShift;
9010 DCHECK(result != 0); // Ensure that the hash value of 0 is never computed.
9015 bool String::ComputeArrayIndex(uint32_t* index) {
9016 int length = this->length();
9017 if (length == 0 || length > kMaxArrayIndexSize) return false;
9018 StringCharacterStream stream(this);
9019 return StringToArrayIndex(&stream, index);
9023 bool String::SlowAsArrayIndex(uint32_t* index) {
9024 if (length() <= kMaxCachedArrayIndexLength) {
9025 Hash(); // force computation of hash code
9026 uint32_t field = hash_field();
9027 if ((field & kIsNotArrayIndexMask) != 0) return false;
9028 // Isolate the array index form the full hash field.
9029 *index = ArrayIndexValueBits::decode(field);
9032 return ComputeArrayIndex(index);
9037 Handle<String> SeqString::Truncate(Handle<SeqString> string, int new_length) {
9038 int new_size, old_size;
9039 int old_length = string->length();
9040 if (old_length <= new_length) return string;
9042 if (string->IsSeqOneByteString()) {
9043 old_size = SeqOneByteString::SizeFor(old_length);
9044 new_size = SeqOneByteString::SizeFor(new_length);
9046 DCHECK(string->IsSeqTwoByteString());
9047 old_size = SeqTwoByteString::SizeFor(old_length);
9048 new_size = SeqTwoByteString::SizeFor(new_length);
9051 int delta = old_size - new_size;
9053 Address start_of_string = string->address();
9054 DCHECK_OBJECT_ALIGNED(start_of_string);
9055 DCHECK_OBJECT_ALIGNED(start_of_string + new_size);
9057 Heap* heap = string->GetHeap();
9058 NewSpace* newspace = heap->new_space();
9059 if (newspace->Contains(start_of_string) &&
9060 newspace->top() == start_of_string + old_size) {
9061 // Last allocated object in new space. Simply lower allocation top.
9062 newspace->set_top(start_of_string + new_size);
9064 // Sizes are pointer size aligned, so that we can use filler objects
9065 // that are a multiple of pointer size.
9066 heap->CreateFillerObjectAt(start_of_string + new_size, delta);
9068 heap->AdjustLiveBytes(*string, -delta, Heap::CONCURRENT_TO_SWEEPER);
9070 // We are storing the new length using release store after creating a filler
9071 // for the left-over space to avoid races with the sweeper thread.
9072 string->synchronized_set_length(new_length);
9074 if (new_length == 0) return heap->isolate()->factory()->empty_string();
9079 uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) {
9080 // For array indexes mix the length into the hash as an array index could
9083 DCHECK(length <= String::kMaxArrayIndexSize);
9084 DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
9085 (1 << String::kArrayIndexValueBits));
9087 value <<= String::ArrayIndexValueBits::kShift;
9088 value |= length << String::ArrayIndexLengthBits::kShift;
9090 DCHECK((value & String::kIsNotArrayIndexMask) == 0);
9091 DCHECK((length > String::kMaxCachedArrayIndexLength) ||
9092 (value & String::kContainsCachedArrayIndexMask) == 0);
9097 uint32_t StringHasher::GetHashField() {
9098 if (length_ <= String::kMaxHashCalcLength) {
9099 if (is_array_index_) {
9100 return MakeArrayIndexHash(array_index_, length_);
9102 return (GetHashCore(raw_running_hash_) << String::kHashShift) |
9103 String::kIsNotArrayIndexMask;
9105 return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask;
9110 uint32_t StringHasher::ComputeUtf8Hash(Vector<const char> chars,
9112 int* utf16_length_out) {
9113 int vector_length = chars.length();
9114 // Handle some edge cases
9115 if (vector_length <= 1) {
9116 DCHECK(vector_length == 0 ||
9117 static_cast<uint8_t>(chars.start()[0]) <=
9118 unibrow::Utf8::kMaxOneByteChar);
9119 *utf16_length_out = vector_length;
9120 return HashSequentialString(chars.start(), vector_length, seed);
9122 // Start with a fake length which won't affect computation.
9123 // It will be updated later.
9124 StringHasher hasher(String::kMaxArrayIndexSize, seed);
9125 size_t remaining = static_cast<size_t>(vector_length);
9126 const uint8_t* stream = reinterpret_cast<const uint8_t*>(chars.start());
9127 int utf16_length = 0;
9128 bool is_index = true;
9129 DCHECK(hasher.is_array_index_);
9130 while (remaining > 0) {
9131 size_t consumed = 0;
9132 uint32_t c = unibrow::Utf8::ValueOf(stream, remaining, &consumed);
9133 DCHECK(consumed > 0 && consumed <= remaining);
9135 remaining -= consumed;
9136 bool is_two_characters = c > unibrow::Utf16::kMaxNonSurrogateCharCode;
9137 utf16_length += is_two_characters ? 2 : 1;
9138 // No need to keep hashing. But we do need to calculate utf16_length.
9139 if (utf16_length > String::kMaxHashCalcLength) continue;
9140 if (is_two_characters) {
9141 uint16_t c1 = unibrow::Utf16::LeadSurrogate(c);
9142 uint16_t c2 = unibrow::Utf16::TrailSurrogate(c);
9143 hasher.AddCharacter(c1);
9144 hasher.AddCharacter(c2);
9145 if (is_index) is_index = hasher.UpdateIndex(c1);
9146 if (is_index) is_index = hasher.UpdateIndex(c2);
9148 hasher.AddCharacter(c);
9149 if (is_index) is_index = hasher.UpdateIndex(c);
9152 *utf16_length_out = static_cast<int>(utf16_length);
9153 // Must set length here so that hash computation is correct.
9154 hasher.length_ = utf16_length;
9155 return hasher.GetHashField();
9159 void IteratingStringHasher::VisitConsString(ConsString* cons_string) {
9160 // Run small ConsStrings through ConsStringIterator.
9161 if (cons_string->length() < 64) {
9162 ConsStringIterator iter(cons_string);
9165 while (nullptr != (string = iter.Next(&offset))) {
9166 DCHECK_EQ(0, offset);
9167 String::VisitFlat(this, string, 0);
9172 const int max_length = String::kMaxHashCalcLength;
9173 int length = std::min(cons_string->length(), max_length);
9174 if (cons_string->HasOnlyOneByteChars()) {
9175 uint8_t* buffer = new uint8_t[length];
9176 String::WriteToFlat(cons_string, buffer, 0, length);
9177 AddCharacters(buffer, length);
9180 uint16_t* buffer = new uint16_t[length];
9181 String::WriteToFlat(cons_string, buffer, 0, length);
9182 AddCharacters(buffer, length);
9188 void String::PrintOn(FILE* file) {
9189 int length = this->length();
9190 for (int i = 0; i < length; i++) {
9191 PrintF(file, "%c", Get(i));
9196 inline static uint32_t ObjectAddressForHashing(Object* object) {
9197 uint32_t value = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(object));
9198 return value & MemoryChunk::kAlignmentMask;
9203 // For performance reasons we only hash the 3 most variable fields of a map:
9204 // constructor, prototype and bit_field2. For predictability reasons we
9205 // use objects' offsets in respective pages for hashing instead of raw
9208 // Shift away the tag.
9209 int hash = ObjectAddressForHashing(GetConstructor()) >> 2;
9211 // XOR-ing the prototype and constructor directly yields too many zero bits
9212 // when the two pointers are close (which is fairly common).
9213 // To avoid this we shift the prototype bits relatively to the constructor.
9214 hash ^= ObjectAddressForHashing(prototype()) << (32 - kPageSizeBits);
9216 return hash ^ (hash >> 16) ^ bit_field2();
9220 static bool CheckEquivalent(Map* first, Map* second) {
9221 return first->GetConstructor() == second->GetConstructor() &&
9222 first->prototype() == second->prototype() &&
9223 first->instance_type() == second->instance_type() &&
9224 first->bit_field() == second->bit_field() &&
9225 first->is_extensible() == second->is_extensible() &&
9226 first->is_strong() == second->is_strong() &&
9227 first->has_instance_call_handler() ==
9228 second->has_instance_call_handler();
9232 bool Map::EquivalentToForTransition(Map* other) {
9233 return CheckEquivalent(this, other);
9237 bool Map::EquivalentToForNormalization(Map* other,
9238 PropertyNormalizationMode mode) {
9239 int properties = mode == CLEAR_INOBJECT_PROPERTIES
9240 ? 0 : other->inobject_properties();
9241 return CheckEquivalent(this, other) && bit_field2() == other->bit_field2() &&
9242 inobject_properties() == properties;
9246 void JSFunction::JSFunctionIterateBody(int object_size, ObjectVisitor* v) {
9247 // Iterate over all fields in the body but take care in dealing with
9249 IteratePointers(v, kPropertiesOffset, kCodeEntryOffset);
9250 v->VisitCodeEntry(this->address() + kCodeEntryOffset);
9251 IteratePointers(v, kCodeEntryOffset + kPointerSize, object_size);
9255 bool JSFunction::Inlines(SharedFunctionInfo* candidate) {
9256 DisallowHeapAllocation no_gc;
9257 if (shared() == candidate) return true;
9258 if (code()->kind() != Code::OPTIMIZED_FUNCTION) return false;
9259 DeoptimizationInputData* const data =
9260 DeoptimizationInputData::cast(code()->deoptimization_data());
9261 if (data->length() == 0) return false;
9262 FixedArray* const literals = data->LiteralArray();
9263 int const inlined_count = data->InlinedFunctionCount()->value();
9264 for (int i = 0; i < inlined_count; ++i) {
9265 if (SharedFunctionInfo::cast(literals->get(i)) == candidate) {
9273 void JSFunction::MarkForOptimization() {
9274 Isolate* isolate = GetIsolate();
9275 // Do not optimize if function contains break points.
9276 if (shared()->HasDebugInfo()) return;
9277 DCHECK(!IsOptimized());
9278 DCHECK(shared()->allows_lazy_compilation() ||
9279 !shared()->optimization_disabled());
9280 DCHECK(!shared()->HasDebugInfo());
9281 set_code_no_write_barrier(
9282 isolate->builtins()->builtin(Builtins::kCompileOptimized));
9283 // No write barrier required, since the builtin is part of the root set.
9287 void JSFunction::AttemptConcurrentOptimization() {
9288 Isolate* isolate = GetIsolate();
9289 if (!isolate->concurrent_recompilation_enabled() ||
9290 isolate->bootstrapper()->IsActive()) {
9291 MarkForOptimization();
9294 if (isolate->concurrent_osr_enabled() &&
9295 isolate->optimizing_compile_dispatcher()->IsQueuedForOSR(this)) {
9296 // Do not attempt regular recompilation if we already queued this for OSR.
9297 // TODO(yangguo): This is necessary so that we don't install optimized
9298 // code on a function that is already optimized, since OSR and regular
9299 // recompilation race. This goes away as soon as OSR becomes one-shot.
9302 DCHECK(!IsInOptimizationQueue());
9303 DCHECK(!IsOptimized());
9304 DCHECK(shared()->allows_lazy_compilation() ||
9305 !shared()->optimization_disabled());
9306 DCHECK(isolate->concurrent_recompilation_enabled());
9307 if (FLAG_trace_concurrent_recompilation) {
9308 PrintF(" ** Marking ");
9310 PrintF(" for concurrent recompilation.\n");
9312 set_code_no_write_barrier(
9313 isolate->builtins()->builtin(Builtins::kCompileOptimizedConcurrent));
9314 // No write barrier required, since the builtin is part of the root set.
9318 Handle<JSFunction> JSFunction::CloneClosure(Handle<JSFunction> function) {
9319 Isolate* isolate = function->GetIsolate();
9320 Handle<Map> map(function->map());
9321 Handle<SharedFunctionInfo> shared(function->shared());
9322 Handle<Context> context(function->context());
9323 Handle<JSFunction> clone =
9324 isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, context);
9326 if (shared->bound()) {
9327 clone->set_function_bindings(function->function_bindings());
9330 // In typical case, __proto__ of ``function`` is the default Function
9331 // prototype, which means that SetPrototype below is a no-op.
9332 // In rare cases when that is not true, we mutate the clone's __proto__.
9333 Handle<Object> original_prototype(map->prototype(), isolate);
9334 if (*original_prototype != clone->map()->prototype()) {
9335 JSObject::SetPrototype(clone, original_prototype, false).Assert();
9342 void SharedFunctionInfo::AddSharedCodeToOptimizedCodeMap(
9343 Handle<SharedFunctionInfo> shared, Handle<Code> code) {
9344 Isolate* isolate = shared->GetIsolate();
9345 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
9346 Handle<Object> value(shared->optimized_code_map(), isolate);
9347 if (value->IsSmi()) return; // Empty code maps are unsupported.
9348 Handle<FixedArray> code_map = Handle<FixedArray>::cast(value);
9349 code_map->set(kSharedCodeIndex, *code);
9353 void SharedFunctionInfo::AddToOptimizedCodeMap(
9354 Handle<SharedFunctionInfo> shared,
9355 Handle<Context> native_context,
9357 Handle<FixedArray> literals,
9358 BailoutId osr_ast_id) {
9359 Isolate* isolate = shared->GetIsolate();
9360 DCHECK(!shared->SearchOptimizedCodeMap(*native_context, osr_ast_id).code);
9361 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
9362 DCHECK(native_context->IsNativeContext());
9363 STATIC_ASSERT(kEntryLength == 4);
9364 Handle<FixedArray> new_code_map;
9365 Handle<Object> value(shared->optimized_code_map(), isolate);
9367 if (value->IsSmi()) {
9368 // No optimized code map.
9369 DCHECK_EQ(0, Smi::cast(*value)->value());
9370 new_code_map = isolate->factory()->NewFixedArray(kInitialLength, TENURED);
9371 old_length = kEntriesStart;
9373 // Copy old optimized code map and append one new entry.
9374 Handle<FixedArray> old_code_map = Handle<FixedArray>::cast(value);
9375 new_code_map = isolate->factory()->CopyFixedArrayAndGrow(
9376 old_code_map, kEntryLength, TENURED);
9377 old_length = old_code_map->length();
9378 // Zap the old map to avoid any stale entries. Note that this is required
9379 // for correctness because entries are being treated weakly by the GC.
9380 MemsetPointer(old_code_map->data_start(), isolate->heap()->the_hole_value(),
9383 new_code_map->set(old_length + kContextOffset, *native_context);
9384 new_code_map->set(old_length + kCachedCodeOffset, *code);
9385 new_code_map->set(old_length + kLiteralsOffset, *literals);
9386 new_code_map->set(old_length + kOsrAstIdOffset,
9387 Smi::FromInt(osr_ast_id.ToInt()));
9390 for (int i = kEntriesStart; i < new_code_map->length(); i += kEntryLength) {
9391 DCHECK(new_code_map->get(i + kContextOffset)->IsNativeContext());
9392 DCHECK(new_code_map->get(i + kCachedCodeOffset)->IsCode());
9393 DCHECK(Code::cast(new_code_map->get(i + kCachedCodeOffset))->kind() ==
9394 Code::OPTIMIZED_FUNCTION);
9395 DCHECK(new_code_map->get(i + kLiteralsOffset)->IsFixedArray());
9396 DCHECK(new_code_map->get(i + kOsrAstIdOffset)->IsSmi());
9399 shared->set_optimized_code_map(*new_code_map);
9403 void SharedFunctionInfo::ClearOptimizedCodeMap() {
9404 FixedArray* code_map = FixedArray::cast(optimized_code_map());
9406 // If the next map link slot is already used then the function was
9407 // enqueued with code flushing and we remove it now.
9408 if (!code_map->get(kNextMapIndex)->IsUndefined()) {
9409 CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher();
9410 flusher->EvictOptimizedCodeMap(this);
9413 DCHECK(code_map->get(kNextMapIndex)->IsUndefined());
9414 set_optimized_code_map(Smi::FromInt(0));
9418 void SharedFunctionInfo::EvictFromOptimizedCodeMap(Code* optimized_code,
9419 const char* reason) {
9420 DisallowHeapAllocation no_gc;
9421 if (optimized_code_map()->IsSmi()) return;
9423 FixedArray* code_map = FixedArray::cast(optimized_code_map());
9424 int dst = kEntriesStart;
9425 int length = code_map->length();
9426 for (int src = kEntriesStart; src < length; src += kEntryLength) {
9427 DCHECK(code_map->get(src)->IsNativeContext());
9428 if (Code::cast(code_map->get(src + kCachedCodeOffset)) == optimized_code) {
9429 // Evict the src entry by not copying it to the dst entry.
9430 if (FLAG_trace_opt) {
9431 PrintF("[evicting entry from optimizing code map (%s) for ", reason);
9433 BailoutId osr(Smi::cast(code_map->get(src + kOsrAstIdOffset))->value());
9437 PrintF(" (osr ast id %d)]\n", osr.ToInt());
9441 // Keep the src entry by copying it to the dst entry.
9443 code_map->set(dst + kContextOffset,
9444 code_map->get(src + kContextOffset));
9445 code_map->set(dst + kCachedCodeOffset,
9446 code_map->get(src + kCachedCodeOffset));
9447 code_map->set(dst + kLiteralsOffset,
9448 code_map->get(src + kLiteralsOffset));
9449 code_map->set(dst + kOsrAstIdOffset,
9450 code_map->get(src + kOsrAstIdOffset));
9452 dst += kEntryLength;
9455 if (code_map->get(kSharedCodeIndex) == optimized_code) {
9456 // Evict context-independent code as well.
9457 code_map->set_undefined(kSharedCodeIndex);
9458 if (FLAG_trace_opt) {
9459 PrintF("[evicting entry from optimizing code map (%s) for ", reason);
9461 PrintF(" (context-independent code)]\n");
9464 if (dst != length) {
9465 // Always trim even when array is cleared because of heap verifier.
9466 GetHeap()->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(code_map,
9468 if (code_map->length() == kEntriesStart &&
9469 code_map->get(kSharedCodeIndex)->IsUndefined()) {
9470 ClearOptimizedCodeMap();
9476 void SharedFunctionInfo::TrimOptimizedCodeMap(int shrink_by) {
9477 FixedArray* code_map = FixedArray::cast(optimized_code_map());
9478 DCHECK(shrink_by % kEntryLength == 0);
9479 DCHECK(shrink_by <= code_map->length() - kEntriesStart);
9480 // Always trim even when array is cleared because of heap verifier.
9481 GetHeap()->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>(code_map,
9483 if (code_map->length() == kEntriesStart &&
9484 code_map->get(kSharedCodeIndex)->IsUndefined()) {
9485 ClearOptimizedCodeMap();
9490 static void GetMinInobjectSlack(Map* map, void* data) {
9491 int slack = map->unused_property_fields();
9492 if (*reinterpret_cast<int*>(data) > slack) {
9493 *reinterpret_cast<int*>(data) = slack;
9498 static void ShrinkInstanceSize(Map* map, void* data) {
9499 int slack = *reinterpret_cast<int*>(data);
9500 map->set_inobject_properties(map->inobject_properties() - slack);
9501 map->set_unused_property_fields(map->unused_property_fields() - slack);
9502 map->set_instance_size(map->instance_size() - slack * kPointerSize);
9504 // Visitor id might depend on the instance size, recalculate it.
9505 map->set_visitor_id(StaticVisitorBase::GetVisitorId(map));
9509 void JSFunction::CompleteInobjectSlackTracking() {
9510 DCHECK(has_initial_map());
9511 Map* map = initial_map();
9513 DCHECK(map->counter() >= Map::kSlackTrackingCounterEnd - 1);
9514 map->set_counter(Map::kRetainingCounterStart);
9516 int slack = map->unused_property_fields();
9517 TransitionArray::TraverseTransitionTree(map, &GetMinInobjectSlack, &slack);
9519 // Resize the initial map and all maps in its transition tree.
9520 TransitionArray::TraverseTransitionTree(map, &ShrinkInstanceSize, &slack);
9525 static bool PrototypeBenefitsFromNormalization(Handle<JSObject> object) {
9526 DisallowHeapAllocation no_gc;
9527 if (!object->HasFastProperties()) return false;
9528 Map* map = object->map();
9529 if (map->is_prototype_map()) return false;
9530 DescriptorArray* descriptors = map->instance_descriptors();
9531 for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) {
9532 PropertyDetails details = descriptors->GetDetails(i);
9533 if (details.location() == kDescriptor) continue;
9534 if (details.representation().IsHeapObject() ||
9535 details.representation().IsTagged()) {
9536 FieldIndex index = FieldIndex::ForDescriptor(map, i);
9537 if (object->RawFastPropertyAt(index)->IsJSFunction()) return true;
9545 void JSObject::OptimizeAsPrototype(Handle<JSObject> object,
9546 PrototypeOptimizationMode mode) {
9547 if (object->IsGlobalObject()) return;
9548 if (object->IsJSGlobalProxy()) return;
9549 if (mode == FAST_PROTOTYPE && PrototypeBenefitsFromNormalization(object)) {
9550 // First normalize to ensure all JSFunctions are DATA_CONSTANT.
9551 JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, 0,
9552 "NormalizeAsPrototype");
9554 Handle<Map> previous_map(object->map());
9555 if (!object->HasFastProperties()) {
9556 JSObject::MigrateSlowToFast(object, 0, "OptimizeAsPrototype");
9558 if (!object->map()->is_prototype_map()) {
9559 if (object->map() == *previous_map) {
9560 Handle<Map> new_map = Map::Copy(handle(object->map()), "CopyAsPrototype");
9561 JSObject::MigrateToMap(object, new_map);
9563 object->map()->set_is_prototype_map(true);
9565 // Replace the pointer to the exact constructor with the Object function
9566 // from the same context if undetectable from JS. This is to avoid keeping
9567 // memory alive unnecessarily.
9568 Object* maybe_constructor = object->map()->GetConstructor();
9569 if (maybe_constructor->IsJSFunction()) {
9570 JSFunction* constructor = JSFunction::cast(maybe_constructor);
9571 Isolate* isolate = object->GetIsolate();
9572 if (!constructor->shared()->IsApiFunction() &&
9573 object->class_name() == isolate->heap()->Object_string()) {
9574 Handle<String> constructor_name(object->constructor_name(), isolate);
9575 Context* context = constructor->context()->native_context();
9576 JSFunction* object_function = context->object_function();
9577 object->map()->SetConstructor(object_function);
9578 Handle<PrototypeInfo> proto_info =
9579 Map::GetOrCreatePrototypeInfo(object, isolate);
9580 proto_info->set_constructor_name(*constructor_name);
9588 void JSObject::ReoptimizeIfPrototype(Handle<JSObject> object) {
9589 if (!object->map()->is_prototype_map()) return;
9590 OptimizeAsPrototype(object, FAST_PROTOTYPE);
9595 void JSObject::LazyRegisterPrototypeUser(Handle<Map> user, Isolate* isolate) {
9596 DCHECK(FLAG_track_prototype_users);
9597 // Contract: In line with InvalidatePrototypeChains()'s requirements,
9598 // leaf maps don't need to register as users, only prototypes do.
9599 DCHECK(user->is_prototype_map());
9601 Handle<Map> current_user = user;
9602 Handle<PrototypeInfo> current_user_info =
9603 Map::GetOrCreatePrototypeInfo(user, isolate);
9604 for (PrototypeIterator iter(user); !iter.IsAtEnd(); iter.Advance()) {
9605 // Walk up the prototype chain as far as links haven't been registered yet.
9606 if (current_user_info->registry_slot() != PrototypeInfo::UNREGISTERED) {
9609 Handle<Object> maybe_proto = PrototypeIterator::GetCurrent(iter);
9610 if (maybe_proto->IsJSGlobalProxy()) continue;
9611 // Proxies on the prototype chain are not supported.
9612 if (maybe_proto->IsJSProxy()) return;
9613 Handle<JSObject> proto = Handle<JSObject>::cast(maybe_proto);
9614 Handle<PrototypeInfo> proto_info =
9615 Map::GetOrCreatePrototypeInfo(proto, isolate);
9616 Handle<Object> maybe_registry(proto_info->prototype_users(), isolate);
9618 Handle<WeakFixedArray> new_array =
9619 WeakFixedArray::Add(maybe_registry, current_user, &slot);
9620 current_user_info->set_registry_slot(slot);
9621 if (!maybe_registry.is_identical_to(new_array)) {
9622 proto_info->set_prototype_users(*new_array);
9624 if (FLAG_trace_prototype_users) {
9625 PrintF("Registering %p as a user of prototype %p (map=%p).\n",
9626 reinterpret_cast<void*>(*current_user),
9627 reinterpret_cast<void*>(*proto),
9628 reinterpret_cast<void*>(proto->map()));
9631 current_user = handle(proto->map(), isolate);
9632 current_user_info = proto_info;
9637 // Can be called regardless of whether |user| was actually registered with
9638 // |prototype|. Returns true when there was a registration.
9640 bool JSObject::UnregisterPrototypeUser(Handle<Map> user, Isolate* isolate) {
9641 DCHECK(user->is_prototype_map());
9642 // If it doesn't have a PrototypeInfo, it was never registered.
9643 if (!user->prototype_info()->IsPrototypeInfo()) return false;
9644 // If it doesn't have a prototype, it can't be registered.
9645 if (!user->prototype()->IsJSObject()) return false;
9646 Handle<JSObject> prototype(JSObject::cast(user->prototype()), isolate);
9647 Handle<PrototypeInfo> user_info =
9648 Map::GetOrCreatePrototypeInfo(user, isolate);
9649 int slot = user_info->registry_slot();
9650 if (slot == PrototypeInfo::UNREGISTERED) return false;
9651 if (prototype->IsJSGlobalProxy()) {
9652 PrototypeIterator iter(isolate, prototype);
9653 prototype = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
9655 DCHECK(prototype->map()->is_prototype_map());
9656 Object* maybe_proto_info = prototype->map()->prototype_info();
9657 // User knows its registry slot, prototype info and user registry must exist.
9658 DCHECK(maybe_proto_info->IsPrototypeInfo());
9659 Handle<PrototypeInfo> proto_info(PrototypeInfo::cast(maybe_proto_info),
9661 Object* maybe_registry = proto_info->prototype_users();
9662 DCHECK(maybe_registry->IsWeakFixedArray());
9663 DCHECK(WeakFixedArray::cast(maybe_registry)->Get(slot) == *user);
9664 WeakFixedArray::cast(maybe_registry)->Clear(slot);
9665 if (FLAG_trace_prototype_users) {
9666 PrintF("Unregistering %p as a user of prototype %p.\n",
9667 reinterpret_cast<void*>(*user), reinterpret_cast<void*>(*prototype));
9673 static void InvalidatePrototypeChainsInternal(Map* map) {
9674 if (!map->is_prototype_map()) return;
9675 if (FLAG_trace_prototype_users) {
9676 PrintF("Invalidating prototype map %p 's cell\n",
9677 reinterpret_cast<void*>(map));
9679 Object* maybe_proto_info = map->prototype_info();
9680 if (!maybe_proto_info->IsPrototypeInfo()) return;
9681 PrototypeInfo* proto_info = PrototypeInfo::cast(maybe_proto_info);
9682 Object* maybe_cell = proto_info->validity_cell();
9683 if (maybe_cell->IsCell()) {
9684 // Just set the value; the cell will be replaced lazily.
9685 Cell* cell = Cell::cast(maybe_cell);
9686 cell->set_value(Smi::FromInt(Map::kPrototypeChainInvalid));
9689 Object* maybe_array = proto_info->prototype_users();
9690 if (!maybe_array->IsWeakFixedArray()) return;
9692 WeakFixedArray* users = WeakFixedArray::cast(maybe_array);
9693 for (int i = 0; i < users->Length(); ++i) {
9694 Object* maybe_user = users->Get(i);
9695 if (maybe_user->IsSmi()) continue;
9697 // For now, only maps register themselves as users.
9698 Map* user = Map::cast(maybe_user);
9699 // Walk the prototype chain (backwards, towards leaf objects) if necessary.
9700 InvalidatePrototypeChainsInternal(user);
9706 void JSObject::InvalidatePrototypeChains(Map* map) {
9707 if (!FLAG_eliminate_prototype_chain_checks) return;
9708 DisallowHeapAllocation no_gc;
9709 if (map->IsJSGlobalProxyMap()) {
9710 PrototypeIterator iter(map);
9711 map = JSObject::cast(iter.GetCurrent())->map();
9713 InvalidatePrototypeChainsInternal(map);
9718 Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<JSObject> prototype,
9720 Object* maybe_proto_info = prototype->map()->prototype_info();
9721 if (maybe_proto_info->IsPrototypeInfo()) {
9722 return handle(PrototypeInfo::cast(maybe_proto_info), isolate);
9724 Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo();
9725 prototype->map()->set_prototype_info(*proto_info);
9731 Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<Map> prototype_map,
9733 Object* maybe_proto_info = prototype_map->prototype_info();
9734 if (maybe_proto_info->IsPrototypeInfo()) {
9735 return handle(PrototypeInfo::cast(maybe_proto_info), isolate);
9737 Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo();
9738 prototype_map->set_prototype_info(*proto_info);
9744 Handle<Cell> Map::GetOrCreatePrototypeChainValidityCell(Handle<Map> map,
9746 Handle<Object> maybe_prototype(map->prototype(), isolate);
9747 if (!maybe_prototype->IsJSObject()) return Handle<Cell>::null();
9748 Handle<JSObject> prototype = Handle<JSObject>::cast(maybe_prototype);
9749 if (prototype->IsJSGlobalProxy()) {
9750 PrototypeIterator iter(isolate, prototype);
9751 prototype = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
9753 // Ensure the prototype is registered with its own prototypes so its cell
9754 // will be invalidated when necessary.
9755 JSObject::LazyRegisterPrototypeUser(handle(prototype->map(), isolate),
9757 Handle<PrototypeInfo> proto_info =
9758 GetOrCreatePrototypeInfo(prototype, isolate);
9759 Object* maybe_cell = proto_info->validity_cell();
9760 // Return existing cell if it's still valid.
9761 if (maybe_cell->IsCell()) {
9762 Handle<Cell> cell(Cell::cast(maybe_cell), isolate);
9763 if (cell->value() == Smi::FromInt(Map::kPrototypeChainValid)) {
9767 // Otherwise create a new cell.
9768 Handle<Cell> cell = isolate->factory()->NewCell(
9769 handle(Smi::FromInt(Map::kPrototypeChainValid), isolate));
9770 proto_info->set_validity_cell(*cell);
9776 void Map::SetPrototype(Handle<Map> map, Handle<Object> prototype,
9777 PrototypeOptimizationMode proto_mode) {
9778 if (prototype->IsJSObject()) {
9779 Handle<JSObject> prototype_jsobj = Handle<JSObject>::cast(prototype);
9780 JSObject::OptimizeAsPrototype(prototype_jsobj, proto_mode);
9782 WriteBarrierMode wb_mode =
9783 prototype->IsNull() ? SKIP_WRITE_BARRIER : UPDATE_WRITE_BARRIER;
9784 map->set_prototype(*prototype, wb_mode);
9788 Handle<Object> CacheInitialJSArrayMaps(
9789 Handle<Context> native_context, Handle<Map> initial_map) {
9790 // Replace all of the cached initial array maps in the native context with
9791 // the appropriate transitioned elements kind maps.
9792 Factory* factory = native_context->GetIsolate()->factory();
9793 Handle<FixedArray> maps = factory->NewFixedArrayWithHoles(
9794 kElementsKindCount, TENURED);
9796 Handle<Map> current_map = initial_map;
9797 ElementsKind kind = current_map->elements_kind();
9798 DCHECK(kind == GetInitialFastElementsKind());
9799 maps->set(kind, *current_map);
9800 for (int i = GetSequenceIndexFromFastElementsKind(kind) + 1;
9801 i < kFastElementsKindCount; ++i) {
9802 Handle<Map> new_map;
9803 ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(i);
9804 Map* maybe_elements_transition = current_map->ElementsTransitionMap();
9805 if (maybe_elements_transition != NULL) {
9806 new_map = handle(maybe_elements_transition);
9807 DCHECK(new_map->elements_kind() == next_kind);
9809 new_map = Map::CopyAsElementsKind(
9810 current_map, next_kind, INSERT_TRANSITION);
9812 maps->set(next_kind, *new_map);
9813 current_map = new_map;
9815 if (initial_map->is_strong())
9816 native_context->set_js_array_strong_maps(*maps);
9818 native_context->set_js_array_maps(*maps);
9823 void JSFunction::SetInstancePrototype(Handle<JSFunction> function,
9824 Handle<Object> value) {
9825 Isolate* isolate = function->GetIsolate();
9827 DCHECK(value->IsJSReceiver());
9829 // Now some logic for the maps of the objects that are created by using this
9830 // function as a constructor.
9831 if (function->has_initial_map()) {
9832 // If the function has allocated the initial map replace it with a
9833 // copy containing the new prototype. Also complete any in-object
9834 // slack tracking that is in progress at this point because it is
9835 // still tracking the old copy.
9836 if (function->IsInobjectSlackTrackingInProgress()) {
9837 function->CompleteInobjectSlackTracking();
9840 Handle<Map> initial_map(function->initial_map(), isolate);
9842 if (!initial_map->GetIsolate()->bootstrapper()->IsActive() &&
9843 initial_map->instance_type() == JS_OBJECT_TYPE) {
9844 // Put the value in the initial map field until an initial map is needed.
9845 // At that point, a new initial map is created and the prototype is put
9846 // into the initial map where it belongs.
9847 function->set_prototype_or_initial_map(*value);
9849 Handle<Map> new_map = Map::Copy(initial_map, "SetInstancePrototype");
9850 JSFunction::SetInitialMap(function, new_map, value);
9852 // If the function is used as the global Array function, cache the
9853 // updated initial maps (and transitioned versions) in the native context.
9854 Handle<Context> native_context(function->context()->native_context(),
9856 Handle<Object> array_function(
9857 native_context->get(Context::ARRAY_FUNCTION_INDEX), isolate);
9858 if (array_function->IsJSFunction() &&
9859 *function == JSFunction::cast(*array_function)) {
9860 CacheInitialJSArrayMaps(native_context, new_map);
9861 Handle<Map> new_strong_map = Map::Copy(new_map, "SetInstancePrototype");
9862 new_strong_map->set_is_strong();
9863 CacheInitialJSArrayMaps(native_context, new_strong_map);
9867 // Deoptimize all code that embeds the previous initial map.
9868 initial_map->dependent_code()->DeoptimizeDependentCodeGroup(
9869 isolate, DependentCode::kInitialMapChangedGroup);
9871 // Put the value in the initial map field until an initial map is
9872 // needed. At that point, a new initial map is created and the
9873 // prototype is put into the initial map where it belongs.
9874 function->set_prototype_or_initial_map(*value);
9875 if (value->IsJSObject()) {
9876 // Optimize as prototype to detach it from its transition tree.
9877 JSObject::OptimizeAsPrototype(Handle<JSObject>::cast(value),
9881 isolate->heap()->ClearInstanceofCache();
9885 void JSFunction::SetPrototype(Handle<JSFunction> function,
9886 Handle<Object> value) {
9887 DCHECK(function->should_have_prototype());
9888 Handle<Object> construct_prototype = value;
9890 // If the value is not a JSReceiver, store the value in the map's
9891 // constructor field so it can be accessed. Also, set the prototype
9892 // used for constructing objects to the original object prototype.
9893 // See ECMA-262 13.2.2.
9894 if (!value->IsJSReceiver()) {
9895 // Copy the map so this does not affect unrelated functions.
9896 // Remove map transitions because they point to maps with a
9897 // different prototype.
9898 Handle<Map> new_map = Map::Copy(handle(function->map()), "SetPrototype");
9900 JSObject::MigrateToMap(function, new_map);
9901 new_map->SetConstructor(*value);
9902 new_map->set_non_instance_prototype(true);
9903 Isolate* isolate = new_map->GetIsolate();
9904 construct_prototype = handle(
9905 isolate->context()->native_context()->initial_object_prototype(),
9908 function->map()->set_non_instance_prototype(false);
9911 return SetInstancePrototype(function, construct_prototype);
9915 bool JSFunction::RemovePrototype() {
9916 Context* native_context = context()->native_context();
9917 Map* no_prototype_map =
9918 is_strict(shared()->language_mode())
9919 ? native_context->strict_function_without_prototype_map()
9920 : native_context->sloppy_function_without_prototype_map();
9922 if (map() == no_prototype_map) return true;
9925 if (map() != (is_strict(shared()->language_mode())
9926 ? native_context->strict_function_map()
9927 : native_context->sloppy_function_map())) {
9932 set_map(no_prototype_map);
9933 set_prototype_or_initial_map(no_prototype_map->GetHeap()->the_hole_value());
9938 void JSFunction::SetInitialMap(Handle<JSFunction> function, Handle<Map> map,
9939 Handle<Object> prototype) {
9940 if (map->prototype() != *prototype) {
9941 Map::SetPrototype(map, prototype, FAST_PROTOTYPE);
9943 function->set_prototype_or_initial_map(*map);
9944 map->SetConstructor(*function);
9946 if (FLAG_trace_maps) {
9947 PrintF("[TraceMaps: InitialMap map= %p SFI= %d_%s ]\n",
9948 reinterpret_cast<void*>(*map), function->shared()->unique_id(),
9949 function->shared()->DebugName()->ToCString().get());
9955 void JSFunction::EnsureHasInitialMap(Handle<JSFunction> function) {
9956 if (function->has_initial_map()) return;
9957 Isolate* isolate = function->GetIsolate();
9959 // First create a new map with the size and number of in-object properties
9960 // suggested by the function.
9961 InstanceType instance_type;
9963 int in_object_properties;
9964 if (function->shared()->is_generator()) {
9965 instance_type = JS_GENERATOR_OBJECT_TYPE;
9966 instance_size = JSGeneratorObject::kSize;
9967 in_object_properties = 0;
9969 instance_type = JS_OBJECT_TYPE;
9970 instance_size = function->shared()->CalculateInstanceSize();
9971 in_object_properties = function->shared()->CalculateInObjectProperties();
9973 Handle<Map> map = isolate->factory()->NewMap(instance_type, instance_size);
9975 // Fetch or allocate prototype.
9976 Handle<Object> prototype;
9977 if (function->has_instance_prototype()) {
9978 prototype = handle(function->instance_prototype(), isolate);
9980 prototype = isolate->factory()->NewFunctionPrototype(function);
9982 map->set_inobject_properties(in_object_properties);
9983 map->set_unused_property_fields(in_object_properties);
9984 DCHECK(map->has_fast_object_elements());
9986 // Finally link initial map and constructor function.
9987 JSFunction::SetInitialMap(function, map, Handle<JSReceiver>::cast(prototype));
9989 if (!function->shared()->is_generator()) {
9990 function->StartInobjectSlackTracking();
9995 void JSFunction::SetInstanceClassName(String* name) {
9996 shared()->set_instance_class_name(name);
10000 void JSFunction::PrintName(FILE* out) {
10001 base::SmartArrayPointer<char> name = shared()->DebugName()->ToCString();
10002 PrintF(out, "%s", name.get());
10006 // The filter is a pattern that matches function names in this way:
10007 // "*" all; the default
10008 // "-" all but the top-level function
10009 // "-name" all but the function "name"
10010 // "" only the top-level function
10011 // "name" only the function "name"
10012 // "name*" only functions starting with "name"
10013 // "~" none; the tilde is not an identifier
10014 bool JSFunction::PassesFilter(const char* raw_filter) {
10015 if (*raw_filter == '*') return true;
10016 String* name = shared()->DebugName();
10017 Vector<const char> filter = CStrVector(raw_filter);
10018 if (filter.length() == 0) return name->length() == 0;
10019 if (filter[0] == '-') {
10020 // Negative filter.
10021 if (filter.length() == 1) {
10022 return (name->length() != 0);
10023 } else if (name->IsUtf8EqualTo(filter.SubVector(1, filter.length()))) {
10026 if (filter[filter.length() - 1] == '*' &&
10027 name->IsUtf8EqualTo(filter.SubVector(1, filter.length() - 1), true)) {
10032 } else if (name->IsUtf8EqualTo(filter)) {
10035 if (filter[filter.length() - 1] == '*' &&
10036 name->IsUtf8EqualTo(filter.SubVector(0, filter.length() - 1), true)) {
10043 Handle<String> JSFunction::GetDebugName(Handle<JSFunction> function) {
10044 Isolate* isolate = function->GetIsolate();
10045 Handle<Object> name =
10046 JSReceiver::GetDataProperty(function, isolate->factory()->name_string());
10047 if (name->IsString()) return Handle<String>::cast(name);
10048 return handle(function->shared()->DebugName(), isolate);
10052 void Oddball::Initialize(Isolate* isolate, Handle<Oddball> oddball,
10053 const char* to_string, Handle<Object> to_number,
10054 const char* type_of, byte kind) {
10055 Handle<String> internalized_to_string =
10056 isolate->factory()->InternalizeUtf8String(to_string);
10057 Handle<String> internalized_type_of =
10058 isolate->factory()->InternalizeUtf8String(type_of);
10059 oddball->set_to_number(*to_number);
10060 oddball->set_to_string(*internalized_to_string);
10061 oddball->set_type_of(*internalized_type_of);
10062 oddball->set_kind(kind);
10066 void Script::InitLineEnds(Handle<Script> script) {
10067 if (!script->line_ends()->IsUndefined()) return;
10069 Isolate* isolate = script->GetIsolate();
10071 if (!script->source()->IsString()) {
10072 DCHECK(script->source()->IsUndefined());
10073 Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0);
10074 script->set_line_ends(*empty);
10075 DCHECK(script->line_ends()->IsFixedArray());
10079 Handle<String> src(String::cast(script->source()), isolate);
10081 Handle<FixedArray> array = String::CalculateLineEnds(src, true);
10083 if (*array != isolate->heap()->empty_fixed_array()) {
10084 array->set_map(isolate->heap()->fixed_cow_array_map());
10087 script->set_line_ends(*array);
10088 DCHECK(script->line_ends()->IsFixedArray());
10092 int Script::GetColumnNumber(Handle<Script> script, int code_pos) {
10093 int line_number = GetLineNumber(script, code_pos);
10094 if (line_number == -1) return -1;
10096 DisallowHeapAllocation no_allocation;
10097 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
10098 line_number = line_number - script->line_offset()->value();
10099 if (line_number == 0) return code_pos + script->column_offset()->value();
10100 int prev_line_end_pos =
10101 Smi::cast(line_ends_array->get(line_number - 1))->value();
10102 return code_pos - (prev_line_end_pos + 1);
10106 int Script::GetLineNumberWithArray(int code_pos) {
10107 DisallowHeapAllocation no_allocation;
10108 DCHECK(line_ends()->IsFixedArray());
10109 FixedArray* line_ends_array = FixedArray::cast(line_ends());
10110 int line_ends_len = line_ends_array->length();
10111 if (line_ends_len == 0) return -1;
10113 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
10114 return line_offset()->value();
10118 int right = line_ends_len;
10119 while (int half = (right - left) / 2) {
10120 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
10126 return right + line_offset()->value();
10130 int Script::GetLineNumber(Handle<Script> script, int code_pos) {
10131 InitLineEnds(script);
10132 return script->GetLineNumberWithArray(code_pos);
10136 int Script::GetLineNumber(int code_pos) {
10137 DisallowHeapAllocation no_allocation;
10138 if (!line_ends()->IsUndefined()) return GetLineNumberWithArray(code_pos);
10140 // Slow mode: we do not have line_ends. We have to iterate through source.
10141 if (!source()->IsString()) return -1;
10143 String* source_string = String::cast(source());
10145 int len = source_string->length();
10146 for (int pos = 0; pos < len; pos++) {
10147 if (pos == code_pos) break;
10148 if (source_string->Get(pos) == '\n') line++;
10154 Handle<Object> Script::GetNameOrSourceURL(Handle<Script> script) {
10155 Isolate* isolate = script->GetIsolate();
10156 Handle<String> name_or_source_url_key =
10157 isolate->factory()->InternalizeOneByteString(
10158 STATIC_CHAR_VECTOR("nameOrSourceURL"));
10159 Handle<JSObject> script_wrapper = Script::GetWrapper(script);
10160 Handle<Object> property = Object::GetProperty(
10161 script_wrapper, name_or_source_url_key).ToHandleChecked();
10162 DCHECK(property->IsJSFunction());
10163 Handle<JSFunction> method = Handle<JSFunction>::cast(property);
10164 Handle<Object> result;
10165 // Do not check against pending exception, since this function may be called
10166 // when an exception has already been pending.
10167 if (!Execution::TryCall(method, script_wrapper, 0, NULL).ToHandle(&result)) {
10168 return isolate->factory()->undefined_value();
10174 Handle<JSObject> Script::GetWrapper(Handle<Script> script) {
10175 Isolate* isolate = script->GetIsolate();
10176 if (!script->wrapper()->IsUndefined()) {
10177 DCHECK(script->wrapper()->IsWeakCell());
10178 Handle<WeakCell> cell(WeakCell::cast(script->wrapper()));
10179 if (!cell->cleared()) {
10180 // Return a handle for the existing script wrapper from the cache.
10181 return handle(JSObject::cast(cell->value()));
10183 // If we found an empty WeakCell, that means the script wrapper was
10184 // GCed. We are not notified directly of that, so we decrement here
10185 // so that we at least don't count double for any given script.
10186 isolate->counters()->script_wrappers()->Decrement();
10188 // Construct a new script wrapper.
10189 isolate->counters()->script_wrappers()->Increment();
10190 Handle<JSFunction> constructor = isolate->script_function();
10191 Handle<JSValue> result =
10192 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor));
10193 result->set_value(*script);
10194 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(result);
10195 script->set_wrapper(*cell);
10200 MaybeHandle<SharedFunctionInfo> Script::FindSharedFunctionInfo(
10201 FunctionLiteral* fun) {
10202 if (shared_function_infos()->IsWeakFixedArray()) {
10203 WeakFixedArray* array = WeakFixedArray::cast(shared_function_infos());
10204 for (int i = 0; i < array->Length(); i++) {
10205 Object* obj = array->Get(i);
10206 if (!obj->IsSharedFunctionInfo()) continue;
10207 SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj);
10208 if (fun->function_token_position() == shared->function_token_position() &&
10209 fun->start_position() == shared->start_position()) {
10210 return Handle<SharedFunctionInfo>(shared);
10214 return MaybeHandle<SharedFunctionInfo>();
10218 void SharedFunctionInfo::SetScript(Handle<SharedFunctionInfo> shared,
10219 Handle<Object> script_object) {
10220 if (shared->script() == *script_object) return;
10221 // Remove shared function info from old script's list.
10222 if (shared->script()->IsScript()) {
10223 Script* old_script = Script::cast(shared->script());
10224 if (old_script->shared_function_infos()->IsWeakFixedArray()) {
10225 WeakFixedArray* list =
10226 WeakFixedArray::cast(old_script->shared_function_infos());
10227 list->Remove(shared);
10230 // Add shared function info to new script's list.
10231 if (script_object->IsScript()) {
10232 Handle<Script> script = Handle<Script>::cast(script_object);
10233 Handle<Object> list(script->shared_function_infos(), shared->GetIsolate());
10235 if (list->IsWeakFixedArray()) {
10236 Handle<WeakFixedArray> array = Handle<WeakFixedArray>::cast(list);
10237 for (int i = 0; i < array->Length(); ++i) {
10238 DCHECK(array->Get(i) != *shared);
10242 list = WeakFixedArray::Add(list, shared);
10243 script->set_shared_function_infos(*list);
10245 // Finally set new script.
10246 shared->set_script(*script_object);
10250 String* SharedFunctionInfo::DebugName() {
10251 Object* n = name();
10252 if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name();
10253 return String::cast(n);
10257 bool SharedFunctionInfo::HasSourceCode() const {
10258 return !script()->IsUndefined() &&
10259 !reinterpret_cast<Script*>(script())->source()->IsUndefined();
10263 Handle<Object> SharedFunctionInfo::GetSourceCode() {
10264 if (!HasSourceCode()) return GetIsolate()->factory()->undefined_value();
10265 Handle<String> source(String::cast(Script::cast(script())->source()));
10266 return GetIsolate()->factory()->NewSubString(
10267 source, start_position(), end_position());
10271 bool SharedFunctionInfo::IsInlineable() {
10272 // Check that the function has a script associated with it.
10273 if (!script()->IsScript()) return false;
10274 return !optimization_disabled();
10278 int SharedFunctionInfo::SourceSize() {
10279 return end_position() - start_position();
10283 int SharedFunctionInfo::CalculateInstanceSize() {
10284 int instance_size =
10285 JSObject::kHeaderSize +
10286 expected_nof_properties() * kPointerSize;
10287 if (instance_size > JSObject::kMaxInstanceSize) {
10288 instance_size = JSObject::kMaxInstanceSize;
10290 return instance_size;
10294 int SharedFunctionInfo::CalculateInObjectProperties() {
10295 return (CalculateInstanceSize() - JSObject::kHeaderSize) / kPointerSize;
10299 // Output the source code without any allocation in the heap.
10300 std::ostream& operator<<(std::ostream& os, const SourceCodeOf& v) {
10301 const SharedFunctionInfo* s = v.value;
10302 // For some native functions there is no source.
10303 if (!s->HasSourceCode()) return os << "<No Source>";
10305 // Get the source for the script which this function came from.
10306 // Don't use String::cast because we don't want more assertion errors while
10307 // we are already creating a stack dump.
10308 String* script_source =
10309 reinterpret_cast<String*>(Script::cast(s->script())->source());
10311 if (!script_source->LooksValid()) return os << "<Invalid Source>";
10313 if (!s->is_toplevel()) {
10315 Object* name = s->name();
10316 if (name->IsString() && String::cast(name)->length() > 0) {
10317 String::cast(name)->PrintUC16(os);
10321 int len = s->end_position() - s->start_position();
10322 if (len <= v.max_length || v.max_length < 0) {
10323 script_source->PrintUC16(os, s->start_position(), s->end_position());
10326 script_source->PrintUC16(os, s->start_position(),
10327 s->start_position() + v.max_length);
10328 return os << "...\n";
10333 static bool IsCodeEquivalent(Code* code, Code* recompiled) {
10334 if (code->instruction_size() != recompiled->instruction_size()) return false;
10335 ByteArray* code_relocation = code->relocation_info();
10336 ByteArray* recompiled_relocation = recompiled->relocation_info();
10337 int length = code_relocation->length();
10338 if (length != recompiled_relocation->length()) return false;
10339 int compare = memcmp(code_relocation->GetDataStartAddress(),
10340 recompiled_relocation->GetDataStartAddress(),
10342 return compare == 0;
10346 void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) {
10347 DCHECK(!has_deoptimization_support());
10348 DisallowHeapAllocation no_allocation;
10349 Code* code = this->code();
10350 if (IsCodeEquivalent(code, recompiled)) {
10351 // Copy the deoptimization data from the recompiled code.
10352 code->set_deoptimization_data(recompiled->deoptimization_data());
10353 code->set_has_deoptimization_support(true);
10355 // TODO(3025757): In case the recompiled isn't equivalent to the
10356 // old code, we have to replace it. We should try to avoid this
10357 // altogether because it flushes valuable type feedback by
10358 // effectively resetting all IC state.
10359 ReplaceCode(recompiled);
10361 DCHECK(has_deoptimization_support());
10365 void SharedFunctionInfo::DisableOptimization(BailoutReason reason) {
10366 // Disable optimization for the shared function info and mark the
10367 // code as non-optimizable. The marker on the shared function info
10368 // is there because we flush non-optimized code thereby loosing the
10369 // non-optimizable information for the code. When the code is
10370 // regenerated and set on the shared function info it is marked as
10371 // non-optimizable if optimization is disabled for the shared
10373 DCHECK(reason != kNoReason);
10374 set_optimization_disabled(true);
10375 set_disable_optimization_reason(reason);
10376 // Code should be the lazy compilation stub or else unoptimized.
10377 DCHECK(code()->kind() == Code::FUNCTION || code()->kind() == Code::BUILTIN);
10378 PROFILE(GetIsolate(), CodeDisableOptEvent(code(), this));
10379 if (FLAG_trace_opt) {
10380 PrintF("[disabled optimization for ");
10382 PrintF(", reason: %s]\n", GetBailoutReason(reason));
10387 void SharedFunctionInfo::InitFromFunctionLiteral(
10388 Handle<SharedFunctionInfo> shared_info, FunctionLiteral* lit) {
10389 shared_info->set_length(lit->scope()->default_function_length());
10390 shared_info->set_internal_formal_parameter_count(lit->parameter_count());
10391 shared_info->set_function_token_position(lit->function_token_position());
10392 shared_info->set_start_position(lit->start_position());
10393 shared_info->set_end_position(lit->end_position());
10394 shared_info->set_is_expression(lit->is_expression());
10395 shared_info->set_is_anonymous(lit->is_anonymous());
10396 shared_info->set_inferred_name(*lit->inferred_name());
10397 shared_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
10398 shared_info->set_allows_lazy_compilation_without_context(
10399 lit->AllowsLazyCompilationWithoutContext());
10400 shared_info->set_language_mode(lit->language_mode());
10401 shared_info->set_uses_arguments(lit->scope()->arguments() != NULL);
10402 shared_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
10403 shared_info->set_ast_node_count(lit->ast_node_count());
10404 shared_info->set_is_function(lit->is_function());
10405 if (lit->dont_optimize_reason() != kNoReason) {
10406 shared_info->DisableOptimization(lit->dont_optimize_reason());
10408 shared_info->set_dont_crankshaft(lit->flags() &
10409 AstProperties::kDontCrankshaft);
10410 shared_info->set_kind(lit->kind());
10411 shared_info->set_needs_home_object(lit->scope()->NeedsHomeObject());
10412 shared_info->set_asm_function(lit->scope()->asm_function());
10416 bool SharedFunctionInfo::VerifyBailoutId(BailoutId id) {
10417 DCHECK(!id.IsNone());
10418 Code* unoptimized = code();
10419 DeoptimizationOutputData* data =
10420 DeoptimizationOutputData::cast(unoptimized->deoptimization_data());
10421 unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this);
10423 return true; // Return true if there was no DCHECK.
10427 void JSFunction::StartInobjectSlackTracking() {
10428 DCHECK(has_initial_map() && !IsInobjectSlackTrackingInProgress());
10430 Map* map = initial_map();
10432 // No tracking during the snapshot construction phase.
10433 Isolate* isolate = GetIsolate();
10434 if (isolate->serializer_enabled()) return;
10436 if (map->unused_property_fields() == 0) return;
10438 map->set_counter(Map::kSlackTrackingCounterStart);
10442 void SharedFunctionInfo::ResetForNewContext(int new_ic_age) {
10443 code()->ClearInlineCaches();
10444 // If we clear ICs, we need to clear the type feedback vector too, since
10445 // CallICs are synced with a feedback vector slot.
10446 ClearTypeFeedbackInfo();
10447 set_ic_age(new_ic_age);
10448 if (code()->kind() == Code::FUNCTION) {
10449 code()->set_profiler_ticks(0);
10450 if (optimization_disabled() &&
10451 opt_count() >= FLAG_max_opt_count) {
10452 // Re-enable optimizations if they were disabled due to opt_count limit.
10453 set_optimization_disabled(false);
10456 set_deopt_count(0);
10461 CodeAndLiterals SharedFunctionInfo::SearchOptimizedCodeMap(
10462 Context* native_context, BailoutId osr_ast_id) {
10463 DisallowHeapAllocation no_gc;
10464 DCHECK(native_context->IsNativeContext());
10465 Object* value = optimized_code_map();
10466 if (!value->IsSmi()) {
10467 FixedArray* optimized_code_map = FixedArray::cast(value);
10468 int length = optimized_code_map->length();
10469 Smi* osr_ast_id_smi = Smi::FromInt(osr_ast_id.ToInt());
10470 for (int i = kEntriesStart; i < length; i += kEntryLength) {
10471 if (optimized_code_map->get(i + kContextOffset) == native_context &&
10472 optimized_code_map->get(i + kOsrAstIdOffset) == osr_ast_id_smi) {
10473 return {Code::cast(optimized_code_map->get(i + kCachedCodeOffset)),
10474 FixedArray::cast(optimized_code_map->get(i + kLiteralsOffset))};
10477 Object* shared_code = optimized_code_map->get(kSharedCodeIndex);
10478 if (shared_code->IsCode() && osr_ast_id.IsNone()) {
10479 return {Code::cast(shared_code), nullptr};
10481 if (FLAG_trace_opt) {
10482 PrintF("[didn't find optimized code in optimized code map for ");
10487 return {nullptr, nullptr};
10491 #define DECLARE_TAG(ignore1, name, ignore2) name,
10492 const char* const VisitorSynchronization::kTags[
10493 VisitorSynchronization::kNumberOfSyncTags] = {
10494 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG)
10499 #define DECLARE_TAG(ignore1, ignore2, name) name,
10500 const char* const VisitorSynchronization::kTagNames[
10501 VisitorSynchronization::kNumberOfSyncTags] = {
10502 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG)
10507 void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) {
10508 DCHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
10509 Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
10510 Object* old_target = target;
10511 VisitPointer(&target);
10512 CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target.
10516 void ObjectVisitor::VisitCodeAgeSequence(RelocInfo* rinfo) {
10517 DCHECK(RelocInfo::IsCodeAgeSequence(rinfo->rmode()));
10518 Object* stub = rinfo->code_age_stub();
10520 VisitPointer(&stub);
10525 void ObjectVisitor::VisitCodeEntry(Address entry_address) {
10526 Object* code = Code::GetObjectFromEntryAddress(entry_address);
10527 Object* old_code = code;
10528 VisitPointer(&code);
10529 if (code != old_code) {
10530 Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry();
10535 void ObjectVisitor::VisitCell(RelocInfo* rinfo) {
10536 DCHECK(rinfo->rmode() == RelocInfo::CELL);
10537 Object* cell = rinfo->target_cell();
10538 Object* old_cell = cell;
10539 VisitPointer(&cell);
10540 if (cell != old_cell) {
10541 rinfo->set_target_cell(reinterpret_cast<Cell*>(cell));
10546 void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) {
10547 DCHECK(RelocInfo::IsDebugBreakSlot(rinfo->rmode()) &&
10548 rinfo->IsPatchedDebugBreakSlotSequence());
10549 Object* target = Code::GetCodeFromTargetAddress(rinfo->debug_call_address());
10550 Object* old_target = target;
10551 VisitPointer(&target);
10552 CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target.
10556 void ObjectVisitor::VisitEmbeddedPointer(RelocInfo* rinfo) {
10557 DCHECK(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT);
10558 Object* p = rinfo->target_object();
10563 void ObjectVisitor::VisitExternalReference(RelocInfo* rinfo) {
10564 Address p = rinfo->target_external_reference();
10565 VisitExternalReference(&p);
10569 void Code::InvalidateRelocation() {
10570 InvalidateEmbeddedObjects();
10571 set_relocation_info(GetHeap()->empty_byte_array());
10575 void Code::InvalidateEmbeddedObjects() {
10576 Object* undefined = GetHeap()->undefined_value();
10577 Cell* undefined_cell = GetHeap()->undefined_cell();
10578 int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
10579 RelocInfo::ModeMask(RelocInfo::CELL);
10580 for (RelocIterator it(this, mode_mask); !it.done(); it.next()) {
10581 RelocInfo::Mode mode = it.rinfo()->rmode();
10582 if (mode == RelocInfo::EMBEDDED_OBJECT) {
10583 it.rinfo()->set_target_object(undefined, SKIP_WRITE_BARRIER);
10584 } else if (mode == RelocInfo::CELL) {
10585 it.rinfo()->set_target_cell(undefined_cell, SKIP_WRITE_BARRIER);
10591 void Code::Relocate(intptr_t delta) {
10592 for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) {
10593 it.rinfo()->apply(delta);
10595 CpuFeatures::FlushICache(instruction_start(), instruction_size());
10599 void Code::CopyFrom(const CodeDesc& desc) {
10600 DCHECK(Marking::Color(this) == Marking::WHITE_OBJECT);
10603 CopyBytes(instruction_start(), desc.buffer,
10604 static_cast<size_t>(desc.instr_size));
10607 CopyBytes(relocation_start(),
10608 desc.buffer + desc.buffer_size - desc.reloc_size,
10609 static_cast<size_t>(desc.reloc_size));
10611 // unbox handles and relocate
10612 intptr_t delta = instruction_start() - desc.buffer;
10613 int mode_mask = RelocInfo::kCodeTargetMask |
10614 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
10615 RelocInfo::ModeMask(RelocInfo::CELL) |
10616 RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) |
10617 RelocInfo::kApplyMask;
10618 // Needed to find target_object and runtime_entry on X64
10619 Assembler* origin = desc.origin;
10620 AllowDeferredHandleDereference embedding_raw_address;
10621 for (RelocIterator it(this, mode_mask); !it.done(); it.next()) {
10622 RelocInfo::Mode mode = it.rinfo()->rmode();
10623 if (mode == RelocInfo::EMBEDDED_OBJECT) {
10624 Handle<Object> p = it.rinfo()->target_object_handle(origin);
10625 it.rinfo()->set_target_object(*p, SKIP_WRITE_BARRIER, SKIP_ICACHE_FLUSH);
10626 } else if (mode == RelocInfo::CELL) {
10627 Handle<Cell> cell = it.rinfo()->target_cell_handle();
10628 it.rinfo()->set_target_cell(*cell, SKIP_WRITE_BARRIER, SKIP_ICACHE_FLUSH);
10629 } else if (RelocInfo::IsCodeTarget(mode)) {
10630 // rewrite code handles in inline cache targets to direct
10631 // pointers to the first instruction in the code object
10632 Handle<Object> p = it.rinfo()->target_object_handle(origin);
10633 Code* code = Code::cast(*p);
10634 it.rinfo()->set_target_address(code->instruction_start(),
10635 SKIP_WRITE_BARRIER,
10636 SKIP_ICACHE_FLUSH);
10637 } else if (RelocInfo::IsRuntimeEntry(mode)) {
10638 Address p = it.rinfo()->target_runtime_entry(origin);
10639 it.rinfo()->set_target_runtime_entry(p, SKIP_WRITE_BARRIER,
10640 SKIP_ICACHE_FLUSH);
10641 } else if (mode == RelocInfo::CODE_AGE_SEQUENCE) {
10642 Handle<Object> p = it.rinfo()->code_age_stub_handle(origin);
10643 Code* code = Code::cast(*p);
10644 it.rinfo()->set_code_age_stub(code, SKIP_ICACHE_FLUSH);
10646 it.rinfo()->apply(delta);
10649 CpuFeatures::FlushICache(instruction_start(), instruction_size());
10653 // Locate the source position which is closest to the address in the code. This
10654 // is using the source position information embedded in the relocation info.
10655 // The position returned is relative to the beginning of the script where the
10656 // source for this function is found.
10657 int Code::SourcePosition(Address pc) {
10658 int distance = kMaxInt;
10659 int position = RelocInfo::kNoPosition; // Initially no position found.
10660 // Run through all the relocation info to find the best matching source
10661 // position. All the code needs to be considered as the sequence of the
10662 // instructions in the code does not necessarily follow the same order as the
10664 RelocIterator it(this, RelocInfo::kPositionMask);
10665 while (!it.done()) {
10666 // Only look at positions after the current pc.
10667 if (it.rinfo()->pc() < pc) {
10668 // Get position and distance.
10670 int dist = static_cast<int>(pc - it.rinfo()->pc());
10671 int pos = static_cast<int>(it.rinfo()->data());
10672 // If this position is closer than the current candidate or if it has the
10673 // same distance as the current candidate and the position is higher then
10674 // this position is the new candidate.
10675 if ((dist < distance) ||
10676 (dist == distance && pos > position)) {
10687 // Same as Code::SourcePosition above except it only looks for statement
10689 int Code::SourceStatementPosition(Address pc) {
10690 // First find the position as close as possible using all position
10692 int position = SourcePosition(pc);
10693 // Now find the closest statement position before the position.
10694 int statement_position = 0;
10695 RelocIterator it(this, RelocInfo::kPositionMask);
10696 while (!it.done()) {
10697 if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) {
10698 int p = static_cast<int>(it.rinfo()->data());
10699 if (statement_position < p && p <= position) {
10700 statement_position = p;
10705 return statement_position;
10709 SafepointEntry Code::GetSafepointEntry(Address pc) {
10710 SafepointTable table(this);
10711 return table.FindEntry(pc);
10715 Object* Code::FindNthObject(int n, Map* match_map) {
10716 DCHECK(is_inline_cache_stub());
10717 DisallowHeapAllocation no_allocation;
10718 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10719 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10720 RelocInfo* info = it.rinfo();
10721 Object* object = info->target_object();
10722 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
10723 if (object->IsHeapObject()) {
10724 if (HeapObject::cast(object)->map() == match_map) {
10725 if (--n == 0) return object;
10733 AllocationSite* Code::FindFirstAllocationSite() {
10734 Object* result = FindNthObject(1, GetHeap()->allocation_site_map());
10735 return (result != NULL) ? AllocationSite::cast(result) : NULL;
10739 Map* Code::FindFirstMap() {
10740 Object* result = FindNthObject(1, GetHeap()->meta_map());
10741 return (result != NULL) ? Map::cast(result) : NULL;
10745 void Code::FindAndReplace(const FindAndReplacePattern& pattern) {
10746 DCHECK(is_inline_cache_stub() || is_handler());
10747 DisallowHeapAllocation no_allocation;
10748 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10749 STATIC_ASSERT(FindAndReplacePattern::kMaxCount < 32);
10750 int current_pattern = 0;
10751 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10752 RelocInfo* info = it.rinfo();
10753 Object* object = info->target_object();
10754 if (object->IsHeapObject()) {
10755 if (object->IsWeakCell()) {
10756 object = HeapObject::cast(WeakCell::cast(object)->value());
10758 Map* map = HeapObject::cast(object)->map();
10759 if (map == *pattern.find_[current_pattern]) {
10760 info->set_target_object(*pattern.replace_[current_pattern]);
10761 if (++current_pattern == pattern.count_) return;
10769 void Code::FindAllMaps(MapHandleList* maps) {
10770 DCHECK(is_inline_cache_stub());
10771 DisallowHeapAllocation no_allocation;
10772 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10773 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10774 RelocInfo* info = it.rinfo();
10775 Object* object = info->target_object();
10776 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
10777 if (object->IsMap()) maps->Add(handle(Map::cast(object)));
10782 Code* Code::FindFirstHandler() {
10783 DCHECK(is_inline_cache_stub());
10784 DisallowHeapAllocation no_allocation;
10785 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
10786 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10787 bool skip_next_handler = false;
10788 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10789 RelocInfo* info = it.rinfo();
10790 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
10791 Object* obj = info->target_object();
10792 skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared();
10794 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
10795 if (code->kind() == Code::HANDLER) {
10796 if (!skip_next_handler) return code;
10797 skip_next_handler = false;
10805 bool Code::FindHandlers(CodeHandleList* code_list, int length) {
10806 DCHECK(is_inline_cache_stub());
10807 DisallowHeapAllocation no_allocation;
10808 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
10809 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10810 bool skip_next_handler = false;
10812 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10813 if (i == length) return true;
10814 RelocInfo* info = it.rinfo();
10815 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
10816 Object* obj = info->target_object();
10817 skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared();
10819 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
10820 // IC stubs with handlers never contain non-handler code objects before
10821 // handler targets.
10822 if (code->kind() != Code::HANDLER) break;
10823 if (!skip_next_handler) {
10824 code_list->Add(Handle<Code>(code));
10827 skip_next_handler = false;
10830 return i == length;
10834 MaybeHandle<Code> Code::FindHandlerForMap(Map* map) {
10835 DCHECK(is_inline_cache_stub());
10836 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
10837 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10838 bool return_next = false;
10839 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10840 RelocInfo* info = it.rinfo();
10841 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
10842 Object* object = info->target_object();
10843 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
10844 if (object == map) return_next = true;
10845 } else if (return_next) {
10846 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
10847 DCHECK(code->kind() == Code::HANDLER);
10848 return handle(code);
10851 return MaybeHandle<Code>();
10855 Name* Code::FindFirstName() {
10856 DCHECK(is_inline_cache_stub());
10857 DisallowHeapAllocation no_allocation;
10858 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10859 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10860 RelocInfo* info = it.rinfo();
10861 Object* object = info->target_object();
10862 if (object->IsName()) return Name::cast(object);
10868 void Code::ClearInlineCaches() {
10869 ClearInlineCaches(NULL);
10873 void Code::ClearInlineCaches(Code::Kind kind) {
10874 ClearInlineCaches(&kind);
10878 void Code::ClearInlineCaches(Code::Kind* kind) {
10879 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
10880 RelocInfo::ModeMask(RelocInfo::CONSTRUCT_CALL) |
10881 RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID);
10882 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10883 RelocInfo* info = it.rinfo();
10884 Code* target(Code::GetCodeFromTargetAddress(info->target_address()));
10885 if (target->is_inline_cache_stub()) {
10886 if (kind == NULL || *kind == target->kind()) {
10887 IC::Clear(this->GetIsolate(), info->pc(),
10888 info->host()->constant_pool());
10895 void SharedFunctionInfo::ClearTypeFeedbackInfo() {
10896 feedback_vector()->ClearSlots(this);
10897 feedback_vector()->ClearICSlots(this);
10901 void SharedFunctionInfo::ClearTypeFeedbackInfoAtGCTime() {
10902 feedback_vector()->ClearSlotsAtGCTime(this);
10903 feedback_vector()->ClearICSlotsAtGCTime(this);
10907 BailoutId Code::TranslatePcOffsetToAstId(uint32_t pc_offset) {
10908 DisallowHeapAllocation no_gc;
10909 DCHECK(kind() == FUNCTION);
10910 BackEdgeTable back_edges(this, &no_gc);
10911 for (uint32_t i = 0; i < back_edges.length(); i++) {
10912 if (back_edges.pc_offset(i) == pc_offset) return back_edges.ast_id(i);
10914 return BailoutId::None();
10918 uint32_t Code::TranslateAstIdToPcOffset(BailoutId ast_id) {
10919 DisallowHeapAllocation no_gc;
10920 DCHECK(kind() == FUNCTION);
10921 BackEdgeTable back_edges(this, &no_gc);
10922 for (uint32_t i = 0; i < back_edges.length(); i++) {
10923 if (back_edges.ast_id(i) == ast_id) return back_edges.pc_offset(i);
10925 UNREACHABLE(); // We expect to find the back edge.
10930 void Code::MakeCodeAgeSequenceYoung(byte* sequence, Isolate* isolate) {
10931 PatchPlatformCodeAge(isolate, sequence, kNoAgeCodeAge, NO_MARKING_PARITY);
10935 void Code::MarkCodeAsExecuted(byte* sequence, Isolate* isolate) {
10936 PatchPlatformCodeAge(isolate, sequence, kExecutedOnceCodeAge,
10937 NO_MARKING_PARITY);
10941 // NextAge defines the Code::Age state transitions during a GC cycle.
10942 static Code::Age NextAge(Code::Age age) {
10944 case Code::kNotExecutedCodeAge: // Keep, until we've been executed.
10945 case Code::kToBeExecutedOnceCodeAge: // Keep, until we've been executed.
10946 case Code::kLastCodeAge: // Clamp at last Code::Age value.
10948 case Code::kExecutedOnceCodeAge:
10949 // Pre-age code that has only been executed once.
10950 return static_cast<Code::Age>(Code::kPreAgedCodeAge + 1);
10952 return static_cast<Code::Age>(age + 1); // Default case: Increase age.
10957 // IsOldAge defines the collection criteria for a Code object.
10958 static bool IsOldAge(Code::Age age) {
10959 return age >= Code::kIsOldCodeAge || age == Code::kNotExecutedCodeAge;
10963 void Code::MakeYoung(Isolate* isolate) {
10964 byte* sequence = FindCodeAgeSequence();
10965 if (sequence != NULL) MakeCodeAgeSequenceYoung(sequence, isolate);
10969 void Code::MarkToBeExecutedOnce(Isolate* isolate) {
10970 byte* sequence = FindCodeAgeSequence();
10971 if (sequence != NULL) {
10972 PatchPlatformCodeAge(isolate, sequence, kToBeExecutedOnceCodeAge,
10973 NO_MARKING_PARITY);
10978 void Code::MakeOlder(MarkingParity current_parity) {
10979 byte* sequence = FindCodeAgeSequence();
10980 if (sequence != NULL) {
10982 MarkingParity code_parity;
10983 Isolate* isolate = GetIsolate();
10984 GetCodeAgeAndParity(isolate, sequence, &age, &code_parity);
10985 Age next_age = NextAge(age);
10986 if (age != next_age && code_parity != current_parity) {
10987 PatchPlatformCodeAge(isolate, sequence, next_age, current_parity);
10993 bool Code::IsOld() {
10994 return IsOldAge(GetAge());
10998 byte* Code::FindCodeAgeSequence() {
10999 return FLAG_age_code &&
11000 prologue_offset() != Code::kPrologueOffsetNotSet &&
11001 (kind() == OPTIMIZED_FUNCTION ||
11002 (kind() == FUNCTION && !has_debug_break_slots()))
11003 ? instruction_start() + prologue_offset()
11008 Code::Age Code::GetAge() {
11009 byte* sequence = FindCodeAgeSequence();
11010 if (sequence == NULL) {
11011 return kNoAgeCodeAge;
11014 MarkingParity parity;
11015 GetCodeAgeAndParity(GetIsolate(), sequence, &age, &parity);
11020 void Code::GetCodeAgeAndParity(Code* code, Age* age,
11021 MarkingParity* parity) {
11022 Isolate* isolate = code->GetIsolate();
11023 Builtins* builtins = isolate->builtins();
11025 #define HANDLE_CODE_AGE(AGE) \
11026 stub = *builtins->Make##AGE##CodeYoungAgainEvenMarking(); \
11027 if (code == stub) { \
11028 *age = k##AGE##CodeAge; \
11029 *parity = EVEN_MARKING_PARITY; \
11032 stub = *builtins->Make##AGE##CodeYoungAgainOddMarking(); \
11033 if (code == stub) { \
11034 *age = k##AGE##CodeAge; \
11035 *parity = ODD_MARKING_PARITY; \
11038 CODE_AGE_LIST(HANDLE_CODE_AGE)
11039 #undef HANDLE_CODE_AGE
11040 stub = *builtins->MarkCodeAsExecutedOnce();
11041 if (code == stub) {
11042 *age = kNotExecutedCodeAge;
11043 *parity = NO_MARKING_PARITY;
11046 stub = *builtins->MarkCodeAsExecutedTwice();
11047 if (code == stub) {
11048 *age = kExecutedOnceCodeAge;
11049 *parity = NO_MARKING_PARITY;
11052 stub = *builtins->MarkCodeAsToBeExecutedOnce();
11053 if (code == stub) {
11054 *age = kToBeExecutedOnceCodeAge;
11055 *parity = NO_MARKING_PARITY;
11062 Code* Code::GetCodeAgeStub(Isolate* isolate, Age age, MarkingParity parity) {
11063 Builtins* builtins = isolate->builtins();
11065 #define HANDLE_CODE_AGE(AGE) \
11066 case k##AGE##CodeAge: { \
11067 Code* stub = parity == EVEN_MARKING_PARITY \
11068 ? *builtins->Make##AGE##CodeYoungAgainEvenMarking() \
11069 : *builtins->Make##AGE##CodeYoungAgainOddMarking(); \
11072 CODE_AGE_LIST(HANDLE_CODE_AGE)
11073 #undef HANDLE_CODE_AGE
11074 case kNotExecutedCodeAge: {
11075 DCHECK(parity == NO_MARKING_PARITY);
11076 return *builtins->MarkCodeAsExecutedOnce();
11078 case kExecutedOnceCodeAge: {
11079 DCHECK(parity == NO_MARKING_PARITY);
11080 return *builtins->MarkCodeAsExecutedTwice();
11082 case kToBeExecutedOnceCodeAge: {
11083 DCHECK(parity == NO_MARKING_PARITY);
11084 return *builtins->MarkCodeAsToBeExecutedOnce();
11094 void Code::PrintDeoptLocation(FILE* out, Address pc) {
11095 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(this, pc);
11096 class SourcePosition pos = info.position;
11097 if (info.deopt_reason != Deoptimizer::kNoReason || !pos.IsUnknown()) {
11098 if (FLAG_hydrogen_track_positions) {
11099 PrintF(out, " ;;; deoptimize at %d_%d: %s\n",
11100 pos.inlining_id(), pos.position(),
11101 Deoptimizer::GetDeoptReason(info.deopt_reason));
11103 PrintF(out, " ;;; deoptimize at %d: %s\n", pos.raw(),
11104 Deoptimizer::GetDeoptReason(info.deopt_reason));
11110 bool Code::CanDeoptAt(Address pc) {
11111 DeoptimizationInputData* deopt_data =
11112 DeoptimizationInputData::cast(deoptimization_data());
11113 Address code_start_address = instruction_start();
11114 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
11115 if (deopt_data->Pc(i)->value() == -1) continue;
11116 Address address = code_start_address + deopt_data->Pc(i)->value();
11117 if (address == pc) return true;
11123 // Identify kind of code.
11124 const char* Code::Kind2String(Kind kind) {
11126 #define CASE(name) case name: return #name;
11127 CODE_KIND_LIST(CASE)
11129 case NUMBER_OF_KINDS: break;
11136 Handle<WeakCell> Code::WeakCellFor(Handle<Code> code) {
11137 DCHECK(code->kind() == OPTIMIZED_FUNCTION);
11138 WeakCell* raw_cell = code->CachedWeakCell();
11139 if (raw_cell != NULL) return Handle<WeakCell>(raw_cell);
11140 Handle<WeakCell> cell = code->GetIsolate()->factory()->NewWeakCell(code);
11141 DeoptimizationInputData::cast(code->deoptimization_data())
11142 ->SetWeakCellCache(*cell);
11147 WeakCell* Code::CachedWeakCell() {
11148 DCHECK(kind() == OPTIMIZED_FUNCTION);
11149 Object* weak_cell_cache =
11150 DeoptimizationInputData::cast(deoptimization_data())->WeakCellCache();
11151 if (weak_cell_cache->IsWeakCell()) {
11152 DCHECK(this == WeakCell::cast(weak_cell_cache)->value());
11153 return WeakCell::cast(weak_cell_cache);
11159 #ifdef ENABLE_DISASSEMBLER
11161 void DeoptimizationInputData::DeoptimizationInputDataPrint(
11162 std::ostream& os) { // NOLINT
11163 disasm::NameConverter converter;
11164 int const inlined_function_count = InlinedFunctionCount()->value();
11165 os << "Inlined functions (count = " << inlined_function_count << ")\n";
11166 for (int id = 0; id < inlined_function_count; ++id) {
11167 Object* info = LiteralArray()->get(id);
11168 os << " " << Brief(SharedFunctionInfo::cast(info)) << "\n";
11171 int deopt_count = DeoptCount();
11172 os << "Deoptimization Input Data (deopt points = " << deopt_count << ")\n";
11173 if (0 != deopt_count) {
11174 os << " index ast id argc pc";
11175 if (FLAG_print_code_verbose) os << " commands";
11178 for (int i = 0; i < deopt_count; i++) {
11179 os << std::setw(6) << i << " " << std::setw(6) << AstId(i).ToInt() << " "
11180 << std::setw(6) << ArgumentsStackHeight(i)->value() << " "
11181 << std::setw(6) << Pc(i)->value();
11183 if (!FLAG_print_code_verbose) {
11187 // Print details of the frame translation.
11188 int translation_index = TranslationIndex(i)->value();
11189 TranslationIterator iterator(TranslationByteArray(), translation_index);
11190 Translation::Opcode opcode =
11191 static_cast<Translation::Opcode>(iterator.Next());
11192 DCHECK(Translation::BEGIN == opcode);
11193 int frame_count = iterator.Next();
11194 int jsframe_count = iterator.Next();
11195 os << " " << Translation::StringFor(opcode)
11196 << " {frame count=" << frame_count
11197 << ", js frame count=" << jsframe_count << "}\n";
11199 while (iterator.HasNext() &&
11200 Translation::BEGIN !=
11201 (opcode = static_cast<Translation::Opcode>(iterator.Next()))) {
11202 os << std::setw(31) << " " << Translation::StringFor(opcode) << " ";
11205 case Translation::BEGIN:
11209 case Translation::JS_FRAME: {
11210 int ast_id = iterator.Next();
11211 int shared_info_id = iterator.Next();
11212 unsigned height = iterator.Next();
11213 Object* shared_info = LiteralArray()->get(shared_info_id);
11214 os << "{ast_id=" << ast_id << ", function="
11215 << Brief(SharedFunctionInfo::cast(shared_info)->DebugName())
11216 << ", height=" << height << "}";
11220 case Translation::JS_FRAME_FUNCTION: {
11221 os << "{function}";
11225 case Translation::COMPILED_STUB_FRAME: {
11226 Code::Kind stub_kind = static_cast<Code::Kind>(iterator.Next());
11227 os << "{kind=" << stub_kind << "}";
11231 case Translation::ARGUMENTS_ADAPTOR_FRAME:
11232 case Translation::CONSTRUCT_STUB_FRAME: {
11233 int shared_info_id = iterator.Next();
11234 Object* shared_info = LiteralArray()->get(shared_info_id);
11235 unsigned height = iterator.Next();
11237 << Brief(SharedFunctionInfo::cast(shared_info)->DebugName())
11238 << ", height=" << height << "}";
11242 case Translation::GETTER_STUB_FRAME:
11243 case Translation::SETTER_STUB_FRAME: {
11244 int shared_info_id = iterator.Next();
11245 Object* shared_info = LiteralArray()->get(shared_info_id);
11246 os << "{function=" << Brief(SharedFunctionInfo::cast(shared_info)
11247 ->DebugName()) << "}";
11251 case Translation::REGISTER: {
11252 int reg_code = iterator.Next();
11253 os << "{input=" << converter.NameOfCPURegister(reg_code) << "}";
11257 case Translation::INT32_REGISTER: {
11258 int reg_code = iterator.Next();
11259 os << "{input=" << converter.NameOfCPURegister(reg_code) << "}";
11263 case Translation::UINT32_REGISTER: {
11264 int reg_code = iterator.Next();
11265 os << "{input=" << converter.NameOfCPURegister(reg_code)
11270 case Translation::BOOL_REGISTER: {
11271 int reg_code = iterator.Next();
11272 os << "{input=" << converter.NameOfCPURegister(reg_code)
11277 case Translation::DOUBLE_REGISTER: {
11278 int reg_code = iterator.Next();
11279 os << "{input=" << DoubleRegister::AllocationIndexToString(reg_code)
11284 case Translation::STACK_SLOT: {
11285 int input_slot_index = iterator.Next();
11286 os << "{input=" << input_slot_index << "}";
11290 case Translation::INT32_STACK_SLOT: {
11291 int input_slot_index = iterator.Next();
11292 os << "{input=" << input_slot_index << "}";
11296 case Translation::UINT32_STACK_SLOT: {
11297 int input_slot_index = iterator.Next();
11298 os << "{input=" << input_slot_index << " (unsigned)}";
11302 case Translation::BOOL_STACK_SLOT: {
11303 int input_slot_index = iterator.Next();
11304 os << "{input=" << input_slot_index << " (bool)}";
11308 case Translation::DOUBLE_STACK_SLOT: {
11309 int input_slot_index = iterator.Next();
11310 os << "{input=" << input_slot_index << "}";
11314 case Translation::LITERAL: {
11315 unsigned literal_index = iterator.Next();
11316 os << "{literal_id=" << literal_index << "}";
11320 case Translation::DUPLICATED_OBJECT: {
11321 int object_index = iterator.Next();
11322 os << "{object_index=" << object_index << "}";
11326 case Translation::ARGUMENTS_OBJECT:
11327 case Translation::CAPTURED_OBJECT: {
11328 int args_length = iterator.Next();
11329 os << "{length=" << args_length << "}";
11339 void DeoptimizationOutputData::DeoptimizationOutputDataPrint(
11340 std::ostream& os) { // NOLINT
11341 os << "Deoptimization Output Data (deopt points = " << this->DeoptPoints()
11343 if (this->DeoptPoints() == 0) return;
11345 os << "ast id pc state\n";
11346 for (int i = 0; i < this->DeoptPoints(); i++) {
11347 int pc_and_state = this->PcAndState(i)->value();
11348 os << std::setw(6) << this->AstId(i).ToInt() << " " << std::setw(8)
11349 << FullCodeGenerator::PcField::decode(pc_and_state) << " "
11350 << FullCodeGenerator::State2String(
11351 FullCodeGenerator::StateField::decode(pc_and_state)) << "\n";
11356 void HandlerTable::HandlerTableRangePrint(std::ostream& os) {
11357 os << " from to hdlr\n";
11358 for (int i = 0; i < length(); i += kRangeEntrySize) {
11359 int pc_start = Smi::cast(get(i + kRangeStartIndex))->value();
11360 int pc_end = Smi::cast(get(i + kRangeEndIndex))->value();
11361 int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value();
11362 int handler_offset = HandlerOffsetField::decode(handler_field);
11363 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
11364 int depth = Smi::cast(get(i + kRangeDepthIndex))->value();
11365 os << " (" << std::setw(4) << pc_start << "," << std::setw(4) << pc_end
11366 << ") -> " << std::setw(4) << handler_offset
11367 << " (prediction=" << prediction << ", depth=" << depth << ")\n";
11372 void HandlerTable::HandlerTableReturnPrint(std::ostream& os) {
11373 os << " off hdlr (c)\n";
11374 for (int i = 0; i < length(); i += kReturnEntrySize) {
11375 int pc_offset = Smi::cast(get(i + kReturnOffsetIndex))->value();
11376 int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value();
11377 int handler_offset = HandlerOffsetField::decode(handler_field);
11378 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
11379 os << " " << std::setw(4) << pc_offset << " -> " << std::setw(4)
11380 << handler_offset << " (prediction=" << prediction << ")\n";
11385 const char* Code::ICState2String(InlineCacheState state) {
11387 case UNINITIALIZED: return "UNINITIALIZED";
11388 case PREMONOMORPHIC: return "PREMONOMORPHIC";
11389 case MONOMORPHIC: return "MONOMORPHIC";
11390 case PROTOTYPE_FAILURE:
11391 return "PROTOTYPE_FAILURE";
11392 case POLYMORPHIC: return "POLYMORPHIC";
11393 case MEGAMORPHIC: return "MEGAMORPHIC";
11394 case GENERIC: return "GENERIC";
11395 case DEBUG_STUB: return "DEBUG_STUB";
11404 const char* Code::StubType2String(StubType type) {
11406 case NORMAL: return "NORMAL";
11407 case FAST: return "FAST";
11409 UNREACHABLE(); // keep the compiler happy
11414 void Code::PrintExtraICState(std::ostream& os, // NOLINT
11415 Kind kind, ExtraICState extra) {
11416 os << "extra_ic_state = ";
11417 if ((kind == STORE_IC || kind == KEYED_STORE_IC) &&
11418 is_strict(static_cast<LanguageMode>(extra))) {
11421 os << extra << "\n";
11426 void Code::Disassemble(const char* name, std::ostream& os) { // NOLINT
11427 os << "kind = " << Kind2String(kind()) << "\n";
11428 if (IsCodeStubOrIC()) {
11429 const char* n = CodeStub::MajorName(CodeStub::GetMajorKey(this), true);
11430 os << "major_key = " << (n == NULL ? "null" : n) << "\n";
11432 if (is_inline_cache_stub()) {
11433 os << "ic_state = " << ICState2String(ic_state()) << "\n";
11434 PrintExtraICState(os, kind(), extra_ic_state());
11435 if (ic_state() == MONOMORPHIC) {
11436 os << "type = " << StubType2String(type()) << "\n";
11438 if (is_compare_ic_stub()) {
11439 DCHECK(CodeStub::GetMajorKey(this) == CodeStub::CompareIC);
11440 CompareICStub stub(stub_key(), GetIsolate());
11441 os << "compare_state = " << CompareICState::GetStateName(stub.left())
11442 << "*" << CompareICState::GetStateName(stub.right()) << " -> "
11443 << CompareICState::GetStateName(stub.state()) << "\n";
11444 os << "compare_operation = " << Token::Name(stub.op()) << "\n";
11447 if ((name != NULL) && (name[0] != '\0')) {
11448 os << "name = " << name << "\n";
11450 if (kind() == OPTIMIZED_FUNCTION) {
11451 os << "stack_slots = " << stack_slots() << "\n";
11453 os << "compiler = " << (is_turbofanned()
11455 : is_crankshafted() ? "crankshaft"
11456 : kind() == Code::FUNCTION
11458 : "unknown") << "\n";
11460 os << "Instructions (size = " << instruction_size() << ")\n";
11462 Isolate* isolate = GetIsolate();
11463 int size = instruction_size();
11464 int safepoint_offset =
11465 is_crankshafted() ? static_cast<int>(safepoint_table_offset()) : size;
11466 int back_edge_offset = (kind() == Code::FUNCTION)
11467 ? static_cast<int>(back_edge_table_offset())
11469 int constant_pool_offset = FLAG_enable_embedded_constant_pool
11470 ? this->constant_pool_offset()
11473 // Stop before reaching any embedded tables
11474 int code_size = Min(safepoint_offset, back_edge_offset);
11475 code_size = Min(code_size, constant_pool_offset);
11476 byte* begin = instruction_start();
11477 byte* end = begin + code_size;
11478 Disassembler::Decode(isolate, &os, begin, end, this);
11480 if (constant_pool_offset < size) {
11481 int constant_pool_size = size - constant_pool_offset;
11482 DCHECK((constant_pool_size & kPointerAlignmentMask) == 0);
11483 os << "\nConstant Pool (size = " << constant_pool_size << ")\n";
11484 Vector<char> buf = Vector<char>::New(50);
11485 intptr_t* ptr = reinterpret_cast<intptr_t*>(begin + constant_pool_offset);
11486 for (int i = 0; i < constant_pool_size; i += kPointerSize, ptr++) {
11487 SNPrintF(buf, "%4d %08" V8PRIxPTR, i, *ptr);
11488 os << static_cast<const void*>(ptr) << " " << buf.start() << "\n";
11494 if (kind() == FUNCTION) {
11495 DeoptimizationOutputData* data =
11496 DeoptimizationOutputData::cast(this->deoptimization_data());
11497 data->DeoptimizationOutputDataPrint(os);
11498 } else if (kind() == OPTIMIZED_FUNCTION) {
11499 DeoptimizationInputData* data =
11500 DeoptimizationInputData::cast(this->deoptimization_data());
11501 data->DeoptimizationInputDataPrint(os);
11505 if (is_crankshafted()) {
11506 SafepointTable table(this);
11507 os << "Safepoints (size = " << table.size() << ")\n";
11508 for (unsigned i = 0; i < table.length(); i++) {
11509 unsigned pc_offset = table.GetPcOffset(i);
11510 os << static_cast<const void*>(instruction_start() + pc_offset) << " ";
11511 os << std::setw(4) << pc_offset << " ";
11512 table.PrintEntry(i, os);
11513 os << " (sp -> fp) ";
11514 SafepointEntry entry = table.GetEntry(i);
11515 if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) {
11516 os << std::setw(6) << entry.deoptimization_index();
11520 if (entry.argument_count() > 0) {
11521 os << " argc: " << entry.argument_count();
11526 } else if (kind() == FUNCTION) {
11527 unsigned offset = back_edge_table_offset();
11528 // If there is no back edge table, the "table start" will be at or after
11529 // (due to alignment) the end of the instruction stream.
11530 if (static_cast<int>(offset) < instruction_size()) {
11531 DisallowHeapAllocation no_gc;
11532 BackEdgeTable back_edges(this, &no_gc);
11534 os << "Back edges (size = " << back_edges.length() << ")\n";
11535 os << "ast_id pc_offset loop_depth\n";
11537 for (uint32_t i = 0; i < back_edges.length(); i++) {
11538 os << std::setw(6) << back_edges.ast_id(i).ToInt() << " "
11539 << std::setw(9) << back_edges.pc_offset(i) << " " << std::setw(10)
11540 << back_edges.loop_depth(i) << "\n";
11545 #ifdef OBJECT_PRINT
11546 if (!type_feedback_info()->IsUndefined()) {
11547 OFStream os(stdout);
11548 TypeFeedbackInfo::cast(type_feedback_info())->TypeFeedbackInfoPrint(os);
11554 if (handler_table()->length() > 0) {
11555 os << "Handler Table (size = " << handler_table()->Size() << ")\n";
11556 if (kind() == FUNCTION) {
11557 HandlerTable::cast(handler_table())->HandlerTableRangePrint(os);
11558 } else if (kind() == OPTIMIZED_FUNCTION) {
11559 HandlerTable::cast(handler_table())->HandlerTableReturnPrint(os);
11564 os << "RelocInfo (size = " << relocation_size() << ")\n";
11565 for (RelocIterator it(this); !it.done(); it.next()) {
11566 it.rinfo()->Print(GetIsolate(), os);
11570 #endif // ENABLE_DISASSEMBLER
11573 void BytecodeArray::Disassemble(std::ostream& os) {
11574 os << "Frame size " << frame_size() << "\n";
11575 Vector<char> buf = Vector<char>::New(50);
11577 const uint8_t* first_bytecode_address = GetFirstBytecodeAddress();
11578 int bytecode_size = 0;
11579 for (int i = 0; i < this->length(); i += bytecode_size) {
11580 const uint8_t* bytecode_start = &first_bytecode_address[i];
11581 interpreter::Bytecode bytecode =
11582 interpreter::Bytecodes::FromByte(bytecode_start[0]);
11583 bytecode_size = interpreter::Bytecodes::Size(bytecode);
11585 SNPrintF(buf, "%p", bytecode_start);
11586 os << buf.start() << " : ";
11587 interpreter::Bytecodes::Decode(os, bytecode_start);
11594 void JSArray::Initialize(Handle<JSArray> array, int capacity, int length) {
11595 DCHECK(capacity >= 0);
11596 array->GetIsolate()->factory()->NewJSArrayStorage(
11597 array, length, capacity, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
11601 // Returns false if the passed-in index is marked non-configurable, which will
11602 // cause the truncation operation to halt, and thus no further old values need
11604 static bool GetOldValue(Isolate* isolate,
11605 Handle<JSObject> object,
11607 List<Handle<Object> >* old_values,
11608 List<uint32_t>* indices) {
11609 LookupIterator it(isolate, object, index, LookupIterator::HIDDEN);
11610 CHECK(JSReceiver::GetPropertyAttributes(&it).IsJust());
11611 DCHECK(it.IsFound());
11612 if (!it.IsConfigurable()) return false;
11613 Handle<Object> value =
11614 it.state() == LookupIterator::ACCESSOR
11615 ? Handle<Object>::cast(isolate->factory()->the_hole_value())
11616 : JSReceiver::GetDataProperty(&it);
11617 old_values->Add(value);
11618 indices->Add(index);
11623 void JSArray::SetLength(Handle<JSArray> array, uint32_t new_length) {
11624 // We should never end in here with a pixel or external array.
11625 DCHECK(array->AllowsSetLength());
11626 if (array->SetLengthWouldNormalize(new_length)) {
11627 JSObject::NormalizeElements(array);
11629 array->GetElementsAccessor()->SetLength(array, new_length);
11633 MaybeHandle<Object> JSArray::ObservableSetLength(Handle<JSArray> array,
11634 uint32_t new_length) {
11635 if (!array->map()->is_observed()) {
11636 SetLength(array, new_length);
11640 Isolate* isolate = array->GetIsolate();
11641 List<uint32_t> indices;
11642 List<Handle<Object> > old_values;
11643 Handle<Object> old_length_handle(array->length(), isolate);
11644 uint32_t old_length = 0;
11645 CHECK(old_length_handle->ToArrayLength(&old_length));
11647 static const PropertyAttributes kNoAttrFilter = NONE;
11648 int num_elements = array->NumberOfOwnElements(kNoAttrFilter);
11649 if (num_elements > 0) {
11650 if (old_length == static_cast<uint32_t>(num_elements)) {
11651 // Simple case for arrays without holes.
11652 for (uint32_t i = old_length - 1; i + 1 > new_length; --i) {
11653 if (!GetOldValue(isolate, array, i, &old_values, &indices)) break;
11656 // For sparse arrays, only iterate over existing elements.
11657 // TODO(rafaelw): For fast, sparse arrays, we can avoid iterating over
11658 // the to-be-removed indices twice.
11659 Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements);
11660 array->GetOwnElementKeys(*keys, kNoAttrFilter);
11661 while (num_elements-- > 0) {
11662 uint32_t index = NumberToUint32(keys->get(num_elements));
11663 if (index < new_length) break;
11664 if (!GetOldValue(isolate, array, index, &old_values, &indices)) break;
11669 SetLength(array, new_length);
11671 CHECK(array->length()->ToArrayLength(&new_length));
11672 if (old_length == new_length) return array;
11674 RETURN_ON_EXCEPTION(isolate, BeginPerformSplice(array), Object);
11676 for (int i = 0; i < indices.length(); ++i) {
11677 // For deletions where the property was an accessor, old_values[i]
11678 // will be the hole, which instructs EnqueueChangeRecord to elide
11679 // the "oldValue" property.
11680 RETURN_ON_EXCEPTION(
11682 JSObject::EnqueueChangeRecord(
11683 array, "delete", isolate->factory()->Uint32ToString(indices[i]),
11688 RETURN_ON_EXCEPTION(isolate,
11689 JSObject::EnqueueChangeRecord(
11690 array, "update", isolate->factory()->length_string(),
11691 old_length_handle),
11694 RETURN_ON_EXCEPTION(isolate, EndPerformSplice(array), Object);
11696 uint32_t index = Min(old_length, new_length);
11697 uint32_t add_count = new_length > old_length ? new_length - old_length : 0;
11698 uint32_t delete_count = new_length < old_length ? old_length - new_length : 0;
11699 Handle<JSArray> deleted = isolate->factory()->NewJSArray(0);
11700 if (delete_count > 0) {
11701 for (int i = indices.length() - 1; i >= 0; i--) {
11702 // Skip deletions where the property was an accessor, leaving holes
11703 // in the array of old values.
11704 if (old_values[i]->IsTheHole()) continue;
11705 JSObject::AddDataElement(deleted, indices[i] - index, old_values[i], NONE)
11709 JSArray::SetLength(deleted, delete_count);
11712 RETURN_ON_EXCEPTION(
11713 isolate, EnqueueSpliceRecord(array, index, deleted, add_count), Object);
11720 void Map::AddDependentCode(Handle<Map> map,
11721 DependentCode::DependencyGroup group,
11722 Handle<Code> code) {
11723 Handle<WeakCell> cell = Code::WeakCellFor(code);
11724 Handle<DependentCode> codes = DependentCode::InsertWeakCode(
11725 Handle<DependentCode>(map->dependent_code()), group, cell);
11726 if (*codes != map->dependent_code()) map->set_dependent_code(*codes);
11730 DependentCode::GroupStartIndexes::GroupStartIndexes(DependentCode* entries) {
11731 Recompute(entries);
11735 void DependentCode::GroupStartIndexes::Recompute(DependentCode* entries) {
11736 start_indexes_[0] = 0;
11737 for (int g = 1; g <= kGroupCount; g++) {
11738 int count = entries->number_of_entries(static_cast<DependencyGroup>(g - 1));
11739 start_indexes_[g] = start_indexes_[g - 1] + count;
11744 Handle<DependentCode> DependentCode::InsertCompilationDependencies(
11745 Handle<DependentCode> entries, DependencyGroup group,
11746 Handle<Foreign> info) {
11747 return Insert(entries, group, info);
11751 Handle<DependentCode> DependentCode::InsertWeakCode(
11752 Handle<DependentCode> entries, DependencyGroup group,
11753 Handle<WeakCell> code_cell) {
11754 return Insert(entries, group, code_cell);
11758 Handle<DependentCode> DependentCode::Insert(Handle<DependentCode> entries,
11759 DependencyGroup group,
11760 Handle<Object> object) {
11761 GroupStartIndexes starts(*entries);
11762 int start = starts.at(group);
11763 int end = starts.at(group + 1);
11764 int number_of_entries = starts.number_of_entries();
11765 // Check for existing entry to avoid duplicates.
11766 for (int i = start; i < end; i++) {
11767 if (entries->object_at(i) == *object) return entries;
11769 if (entries->length() < kCodesStartIndex + number_of_entries + 1) {
11770 entries = EnsureSpace(entries);
11771 // The number of codes can change after Compact and GC.
11772 starts.Recompute(*entries);
11773 start = starts.at(group);
11774 end = starts.at(group + 1);
11777 entries->ExtendGroup(group);
11778 entries->set_object_at(end, *object);
11779 entries->set_number_of_entries(group, end + 1 - start);
11784 Handle<DependentCode> DependentCode::EnsureSpace(
11785 Handle<DependentCode> entries) {
11786 Isolate* isolate = entries->GetIsolate();
11787 if (entries->length() == 0) {
11788 entries = Handle<DependentCode>::cast(
11789 isolate->factory()->NewFixedArray(kCodesStartIndex + 1, TENURED));
11790 for (int g = 0; g < kGroupCount; g++) {
11791 entries->set_number_of_entries(static_cast<DependencyGroup>(g), 0);
11795 if (entries->Compact()) return entries;
11796 GroupStartIndexes starts(*entries);
11798 kCodesStartIndex + DependentCode::Grow(starts.number_of_entries());
11799 int grow_by = capacity - entries->length();
11800 return Handle<DependentCode>::cast(
11801 isolate->factory()->CopyFixedArrayAndGrow(entries, grow_by, TENURED));
11805 bool DependentCode::Compact() {
11806 GroupStartIndexes starts(this);
11808 for (int g = 0; g < kGroupCount; g++) {
11809 int start = starts.at(g);
11810 int end = starts.at(g + 1);
11812 DCHECK(start >= n);
11813 for (int i = start; i < end; i++) {
11814 Object* obj = object_at(i);
11815 if (!obj->IsWeakCell() || !WeakCell::cast(obj)->cleared()) {
11816 if (i != n + count) {
11817 copy(i, n + count);
11822 if (count != end - start) {
11823 set_number_of_entries(static_cast<DependencyGroup>(g), count);
11827 return n < starts.number_of_entries();
11831 void DependentCode::UpdateToFinishedCode(DependencyGroup group, Foreign* info,
11832 WeakCell* code_cell) {
11833 DisallowHeapAllocation no_gc;
11834 GroupStartIndexes starts(this);
11835 int start = starts.at(group);
11836 int end = starts.at(group + 1);
11837 for (int i = start; i < end; i++) {
11838 if (object_at(i) == info) {
11839 set_object_at(i, code_cell);
11845 for (int i = start; i < end; i++) {
11846 DCHECK(object_at(i) != info);
11852 void DependentCode::RemoveCompilationDependencies(
11853 DependentCode::DependencyGroup group, Foreign* info) {
11854 DisallowHeapAllocation no_allocation;
11855 GroupStartIndexes starts(this);
11856 int start = starts.at(group);
11857 int end = starts.at(group + 1);
11858 // Find compilation info wrapper.
11860 for (int i = start; i < end; i++) {
11861 if (object_at(i) == info) {
11866 if (info_pos == -1) return; // Not found.
11867 int gap = info_pos;
11868 // Use the last of each group to fill the gap in the previous group.
11869 for (int i = group; i < kGroupCount; i++) {
11870 int last_of_group = starts.at(i + 1) - 1;
11871 DCHECK(last_of_group >= gap);
11872 if (last_of_group == gap) continue;
11873 copy(last_of_group, gap);
11874 gap = last_of_group;
11876 DCHECK(gap == starts.number_of_entries() - 1);
11877 clear_at(gap); // Clear last gap.
11878 set_number_of_entries(group, end - start - 1);
11881 for (int i = start; i < end - 1; i++) {
11882 DCHECK(object_at(i) != info);
11888 bool DependentCode::Contains(DependencyGroup group, WeakCell* code_cell) {
11889 GroupStartIndexes starts(this);
11890 int start = starts.at(group);
11891 int end = starts.at(group + 1);
11892 for (int i = start; i < end; i++) {
11893 if (object_at(i) == code_cell) return true;
11899 bool DependentCode::MarkCodeForDeoptimization(
11901 DependentCode::DependencyGroup group) {
11902 DisallowHeapAllocation no_allocation_scope;
11903 DependentCode::GroupStartIndexes starts(this);
11904 int start = starts.at(group);
11905 int end = starts.at(group + 1);
11906 int code_entries = starts.number_of_entries();
11907 if (start == end) return false;
11909 // Mark all the code that needs to be deoptimized.
11910 bool marked = false;
11911 bool invalidate_embedded_objects = group == kWeakCodeGroup;
11912 for (int i = start; i < end; i++) {
11913 Object* obj = object_at(i);
11914 if (obj->IsWeakCell()) {
11915 WeakCell* cell = WeakCell::cast(obj);
11916 if (cell->cleared()) continue;
11917 Code* code = Code::cast(cell->value());
11918 if (!code->marked_for_deoptimization()) {
11919 SetMarkedForDeoptimization(code, group);
11920 if (invalidate_embedded_objects) {
11921 code->InvalidateEmbeddedObjects();
11926 DCHECK(obj->IsForeign());
11927 CompilationDependencies* info =
11928 reinterpret_cast<CompilationDependencies*>(
11929 Foreign::cast(obj)->foreign_address());
11933 // Compact the array by moving all subsequent groups to fill in the new holes.
11934 for (int src = end, dst = start; src < code_entries; src++, dst++) {
11937 // Now the holes are at the end of the array, zap them for heap-verifier.
11938 int removed = end - start;
11939 for (int i = code_entries - removed; i < code_entries; i++) {
11942 set_number_of_entries(group, 0);
11947 void DependentCode::DeoptimizeDependentCodeGroup(
11949 DependentCode::DependencyGroup group) {
11950 DCHECK(AllowCodeDependencyChange::IsAllowed());
11951 DisallowHeapAllocation no_allocation_scope;
11952 bool marked = MarkCodeForDeoptimization(isolate, group);
11953 if (marked) Deoptimizer::DeoptimizeMarkedCode(isolate);
11957 void DependentCode::SetMarkedForDeoptimization(Code* code,
11958 DependencyGroup group) {
11959 code->set_marked_for_deoptimization(true);
11960 if (FLAG_trace_deopt &&
11961 (code->deoptimization_data() != code->GetHeap()->empty_fixed_array())) {
11962 DeoptimizationInputData* deopt_data =
11963 DeoptimizationInputData::cast(code->deoptimization_data());
11964 CodeTracer::Scope scope(code->GetHeap()->isolate()->GetCodeTracer());
11965 PrintF(scope.file(), "[marking dependent code 0x%08" V8PRIxPTR
11966 " (opt #%d) for deoptimization, reason: %s]\n",
11967 reinterpret_cast<intptr_t>(code),
11968 deopt_data->OptimizationId()->value(), DependencyGroupName(group));
11973 const char* DependentCode::DependencyGroupName(DependencyGroup group) {
11975 case kWeakCodeGroup:
11976 return "weak-code";
11977 case kTransitionGroup:
11978 return "transition";
11979 case kPrototypeCheckGroup:
11980 return "prototype-check";
11981 case kPropertyCellChangedGroup:
11982 return "property-cell-changed";
11983 case kFieldTypeGroup:
11984 return "field-type";
11985 case kInitialMapChangedGroup:
11986 return "initial-map-changed";
11987 case kAllocationSiteTenuringChangedGroup:
11988 return "allocation-site-tenuring-changed";
11989 case kAllocationSiteTransitionChangedGroup:
11990 return "allocation-site-transition-changed";
11997 Handle<Map> Map::TransitionToPrototype(Handle<Map> map,
11998 Handle<Object> prototype,
11999 PrototypeOptimizationMode mode) {
12000 Handle<Map> new_map = TransitionArray::GetPrototypeTransition(map, prototype);
12001 if (new_map.is_null()) {
12002 new_map = Copy(map, "TransitionToPrototype");
12003 TransitionArray::PutPrototypeTransition(map, prototype, new_map);
12004 Map::SetPrototype(new_map, prototype, mode);
12010 MaybeHandle<Object> JSObject::SetPrototype(Handle<JSObject> object,
12011 Handle<Object> value,
12012 bool from_javascript) {
12014 int size = object->Size();
12017 Isolate* isolate = object->GetIsolate();
12018 // Strong objects may not have their prototype set via __proto__ or
12020 if (from_javascript && object->map()->is_strong()) {
12021 THROW_NEW_ERROR(isolate,
12022 NewTypeError(MessageTemplate::kStrongSetProto, object),
12025 Heap* heap = isolate->heap();
12026 // Silently ignore the change if value is not a JSObject or null.
12027 // SpiderMonkey behaves this way.
12028 if (!value->IsJSReceiver() && !value->IsNull()) return value;
12030 // From 8.6.2 Object Internal Methods
12032 // In addition, if [[Extensible]] is false the value of the [[Class]] and
12033 // [[Prototype]] internal properties of the object may not be modified.
12035 // Implementation specific extensions that modify [[Class]], [[Prototype]]
12036 // or [[Extensible]] must not violate the invariants defined in the preceding
12038 if (!object->map()->is_extensible()) {
12039 THROW_NEW_ERROR(isolate,
12040 NewTypeError(MessageTemplate::kNonExtensibleProto, object),
12044 // Before we can set the prototype we need to be sure
12045 // prototype cycles are prevented.
12046 // It is sufficient to validate that the receiver is not in the new prototype
12048 for (PrototypeIterator iter(isolate, *value,
12049 PrototypeIterator::START_AT_RECEIVER);
12050 !iter.IsAtEnd(); iter.Advance()) {
12051 if (JSReceiver::cast(iter.GetCurrent()) == *object) {
12053 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kCyclicProto),
12058 bool dictionary_elements_in_chain =
12059 object->map()->DictionaryElementsInPrototypeChainOnly();
12060 Handle<JSObject> real_receiver = object;
12062 if (from_javascript) {
12063 // Find the first object in the chain whose prototype object is not
12064 // hidden and set the new prototype on that object.
12065 PrototypeIterator iter(isolate, real_receiver);
12066 while (!iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN)) {
12068 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
12070 if (!real_receiver->map()->is_extensible()) {
12072 isolate, NewTypeError(MessageTemplate::kNonExtensibleProto, object),
12078 // Set the new prototype of the object.
12079 Handle<Map> map(real_receiver->map());
12081 // Nothing to do if prototype is already set.
12082 if (map->prototype() == *value) return value;
12084 isolate->UpdateArrayProtectorOnSetPrototype(real_receiver);
12086 PrototypeOptimizationMode mode =
12087 from_javascript ? REGULAR_PROTOTYPE : FAST_PROTOTYPE;
12088 Handle<Map> new_map = Map::TransitionToPrototype(map, value, mode);
12089 DCHECK(new_map->prototype() == *value);
12090 JSObject::MigrateToMap(real_receiver, new_map);
12092 if (from_javascript && !dictionary_elements_in_chain &&
12093 new_map->DictionaryElementsInPrototypeChainOnly()) {
12094 // If the prototype chain didn't previously have element callbacks, then
12095 // KeyedStoreICs need to be cleared to ensure any that involve this
12097 object->GetHeap()->ClearAllICsByKind(Code::KEYED_STORE_IC);
12100 heap->ClearInstanceofCache();
12101 DCHECK(size == object->Size());
12106 void JSObject::EnsureCanContainElements(Handle<JSObject> object,
12108 uint32_t first_arg,
12109 uint32_t arg_count,
12110 EnsureElementsMode mode) {
12111 // Elements in |Arguments| are ordered backwards (because they're on the
12112 // stack), but the method that's called here iterates over them in forward
12114 return EnsureCanContainElements(
12115 object, args->arguments() - first_arg - (arg_count - 1), arg_count, mode);
12119 ElementsAccessor* JSObject::GetElementsAccessor() {
12120 return ElementsAccessor::ForKind(GetElementsKind());
12124 void JSObject::ValidateElements(Handle<JSObject> object) {
12125 #ifdef ENABLE_SLOW_DCHECKS
12126 if (FLAG_enable_slow_asserts) {
12127 ElementsAccessor* accessor = object->GetElementsAccessor();
12128 accessor->Validate(object);
12134 static bool ShouldConvertToSlowElements(JSObject* object, uint32_t capacity,
12136 uint32_t* new_capacity) {
12137 STATIC_ASSERT(JSObject::kMaxUncheckedOldFastElementsLength <=
12138 JSObject::kMaxUncheckedFastElementsLength);
12139 if (index < capacity) {
12140 *new_capacity = capacity;
12143 if (index - capacity >= JSObject::kMaxGap) return true;
12144 *new_capacity = JSObject::NewElementsCapacity(index + 1);
12145 DCHECK_LT(index, *new_capacity);
12146 if (*new_capacity <= JSObject::kMaxUncheckedOldFastElementsLength ||
12147 (*new_capacity <= JSObject::kMaxUncheckedFastElementsLength &&
12148 object->GetHeap()->InNewSpace(object))) {
12151 // If the fast-case backing storage takes up roughly three times as
12152 // much space (in machine words) as a dictionary backing storage
12153 // would, the object should have slow elements.
12154 int used_elements = object->GetFastElementsUsage();
12155 int dictionary_size = SeededNumberDictionary::ComputeCapacity(used_elements) *
12156 SeededNumberDictionary::kEntrySize;
12157 return 3 * static_cast<uint32_t>(dictionary_size) <= *new_capacity;
12161 bool JSObject::WouldConvertToSlowElements(uint32_t index) {
12162 if (HasFastElements()) {
12163 Handle<FixedArrayBase> backing_store(FixedArrayBase::cast(elements()));
12164 uint32_t capacity = static_cast<uint32_t>(backing_store->length());
12165 uint32_t new_capacity;
12166 return ShouldConvertToSlowElements(this, capacity, index, &new_capacity);
12172 static ElementsKind BestFittingFastElementsKind(JSObject* object) {
12173 if (object->HasSloppyArgumentsElements()) {
12174 return FAST_SLOPPY_ARGUMENTS_ELEMENTS;
12176 DCHECK(object->HasDictionaryElements());
12177 SeededNumberDictionary* dictionary = object->element_dictionary();
12178 ElementsKind kind = FAST_HOLEY_SMI_ELEMENTS;
12179 for (int i = 0; i < dictionary->Capacity(); i++) {
12180 Object* key = dictionary->KeyAt(i);
12181 if (key->IsNumber()) {
12182 Object* value = dictionary->ValueAt(i);
12183 if (!value->IsNumber()) return FAST_HOLEY_ELEMENTS;
12184 if (!value->IsSmi()) {
12185 if (!FLAG_unbox_double_arrays) return FAST_HOLEY_ELEMENTS;
12186 kind = FAST_HOLEY_DOUBLE_ELEMENTS;
12194 static bool ShouldConvertToFastElements(JSObject* object,
12195 SeededNumberDictionary* dictionary,
12197 uint32_t* new_capacity) {
12198 // If properties with non-standard attributes or accessors were added, we
12199 // cannot go back to fast elements.
12200 if (dictionary->requires_slow_elements()) return false;
12202 // Adding a property with this index will require slow elements.
12203 if (index >= static_cast<uint32_t>(Smi::kMaxValue)) return false;
12205 if (object->IsJSArray()) {
12206 Object* length = JSArray::cast(object)->length();
12207 if (!length->IsSmi()) return false;
12208 *new_capacity = static_cast<uint32_t>(Smi::cast(length)->value());
12210 *new_capacity = dictionary->max_number_key() + 1;
12212 *new_capacity = Max(index + 1, *new_capacity);
12214 uint32_t dictionary_size = static_cast<uint32_t>(dictionary->Capacity()) *
12215 SeededNumberDictionary::kEntrySize;
12216 return 2 * dictionary_size >= *new_capacity;
12221 MaybeHandle<Object> JSObject::AddDataElement(Handle<JSObject> object,
12223 Handle<Object> value,
12224 PropertyAttributes attributes) {
12225 DCHECK(object->map()->is_extensible());
12227 Isolate* isolate = object->GetIsolate();
12229 uint32_t old_length = 0;
12230 uint32_t new_capacity = 0;
12232 Handle<Object> old_length_handle;
12233 if (object->IsJSArray()) {
12234 CHECK(JSArray::cast(*object)->length()->ToArrayLength(&old_length));
12235 if (object->map()->is_observed()) {
12236 old_length_handle = handle(JSArray::cast(*object)->length(), isolate);
12240 ElementsKind kind = object->GetElementsKind();
12241 FixedArrayBase* elements = object->elements();
12242 ElementsKind dictionary_kind = DICTIONARY_ELEMENTS;
12243 if (IsSloppyArgumentsElements(kind)) {
12244 elements = FixedArrayBase::cast(FixedArray::cast(elements)->get(1));
12245 dictionary_kind = SLOW_SLOPPY_ARGUMENTS_ELEMENTS;
12248 if (attributes != NONE) {
12249 kind = dictionary_kind;
12250 } else if (elements->IsSeededNumberDictionary()) {
12251 kind = ShouldConvertToFastElements(*object,
12252 SeededNumberDictionary::cast(elements),
12253 index, &new_capacity)
12254 ? BestFittingFastElementsKind(*object)
12255 : dictionary_kind; // Overwrite in case of arguments.
12256 } else if (ShouldConvertToSlowElements(
12257 *object, static_cast<uint32_t>(elements->length()), index,
12259 kind = dictionary_kind;
12262 ElementsKind to = value->OptimalElementsKind();
12263 if (IsHoleyElementsKind(kind) || !object->IsJSArray() || index > old_length) {
12264 to = GetHoleyElementsKind(to);
12265 kind = GetHoleyElementsKind(kind);
12267 to = IsMoreGeneralElementsKindTransition(kind, to) ? to : kind;
12268 ElementsAccessor* accessor = ElementsAccessor::ForKind(to);
12269 accessor->Add(object, index, value, attributes, new_capacity);
12271 uint32_t new_length = old_length;
12272 Handle<Object> new_length_handle;
12273 if (object->IsJSArray() && index >= old_length) {
12274 new_length = index + 1;
12275 new_length_handle = isolate->factory()->NewNumberFromUint(new_length);
12276 JSArray::cast(*object)->set_length(*new_length_handle);
12279 if (!old_length_handle.is_null() && new_length != old_length) {
12280 // |old_length_handle| is kept null above unless the object is observed.
12281 DCHECK(object->map()->is_observed());
12282 Handle<JSArray> array = Handle<JSArray>::cast(object);
12283 Handle<String> name = isolate->factory()->Uint32ToString(index);
12285 RETURN_ON_EXCEPTION(isolate, BeginPerformSplice(array), Object);
12286 RETURN_ON_EXCEPTION(
12287 isolate, EnqueueChangeRecord(array, "add", name,
12288 isolate->factory()->the_hole_value()),
12290 RETURN_ON_EXCEPTION(isolate,
12291 EnqueueChangeRecord(array, "update",
12292 isolate->factory()->length_string(),
12293 old_length_handle),
12295 RETURN_ON_EXCEPTION(isolate, EndPerformSplice(array), Object);
12296 Handle<JSArray> deleted = isolate->factory()->NewJSArray(0);
12297 RETURN_ON_EXCEPTION(isolate, EnqueueSpliceRecord(array, old_length, deleted,
12298 new_length - old_length),
12300 } else if (object->map()->is_observed()) {
12301 Handle<String> name = isolate->factory()->Uint32ToString(index);
12302 RETURN_ON_EXCEPTION(
12303 isolate, EnqueueChangeRecord(object, "add", name,
12304 isolate->factory()->the_hole_value()),
12312 bool JSArray::SetLengthWouldNormalize(uint32_t new_length) {
12313 if (!HasFastElements()) return false;
12314 uint32_t capacity = static_cast<uint32_t>(elements()->length());
12315 uint32_t new_capacity;
12316 return JSArray::SetLengthWouldNormalize(GetHeap(), new_length) &&
12317 ShouldConvertToSlowElements(this, capacity, new_length - 1,
12322 const double AllocationSite::kPretenureRatio = 0.85;
12325 void AllocationSite::ResetPretenureDecision() {
12326 set_pretenure_decision(kUndecided);
12327 set_memento_found_count(0);
12328 set_memento_create_count(0);
12332 PretenureFlag AllocationSite::GetPretenureMode() {
12333 PretenureDecision mode = pretenure_decision();
12334 // Zombie objects "decide" to be untenured.
12335 return mode == kTenure ? TENURED : NOT_TENURED;
12339 bool AllocationSite::IsNestedSite() {
12340 DCHECK(FLAG_trace_track_allocation_sites);
12341 Object* current = GetHeap()->allocation_sites_list();
12342 while (current->IsAllocationSite()) {
12343 AllocationSite* current_site = AllocationSite::cast(current);
12344 if (current_site->nested_site() == this) {
12347 current = current_site->weak_next();
12353 void AllocationSite::DigestTransitionFeedback(Handle<AllocationSite> site,
12354 ElementsKind to_kind) {
12355 Isolate* isolate = site->GetIsolate();
12357 if (site->SitePointsToLiteral() && site->transition_info()->IsJSArray()) {
12358 Handle<JSArray> transition_info =
12359 handle(JSArray::cast(site->transition_info()));
12360 ElementsKind kind = transition_info->GetElementsKind();
12361 // if kind is holey ensure that to_kind is as well.
12362 if (IsHoleyElementsKind(kind)) {
12363 to_kind = GetHoleyElementsKind(to_kind);
12365 if (IsMoreGeneralElementsKindTransition(kind, to_kind)) {
12366 // If the array is huge, it's not likely to be defined in a local
12367 // function, so we shouldn't make new instances of it very often.
12368 uint32_t length = 0;
12369 CHECK(transition_info->length()->ToArrayLength(&length));
12370 if (length <= kMaximumArrayBytesToPretransition) {
12371 if (FLAG_trace_track_allocation_sites) {
12372 bool is_nested = site->IsNestedSite();
12374 "AllocationSite: JSArray %p boilerplate %s updated %s->%s\n",
12375 reinterpret_cast<void*>(*site),
12376 is_nested ? "(nested)" : "",
12377 ElementsKindToString(kind),
12378 ElementsKindToString(to_kind));
12380 JSObject::TransitionElementsKind(transition_info, to_kind);
12381 site->dependent_code()->DeoptimizeDependentCodeGroup(
12382 isolate, DependentCode::kAllocationSiteTransitionChangedGroup);
12386 ElementsKind kind = site->GetElementsKind();
12387 // if kind is holey ensure that to_kind is as well.
12388 if (IsHoleyElementsKind(kind)) {
12389 to_kind = GetHoleyElementsKind(to_kind);
12391 if (IsMoreGeneralElementsKindTransition(kind, to_kind)) {
12392 if (FLAG_trace_track_allocation_sites) {
12393 PrintF("AllocationSite: JSArray %p site updated %s->%s\n",
12394 reinterpret_cast<void*>(*site),
12395 ElementsKindToString(kind),
12396 ElementsKindToString(to_kind));
12398 site->SetElementsKind(to_kind);
12399 site->dependent_code()->DeoptimizeDependentCodeGroup(
12400 isolate, DependentCode::kAllocationSiteTransitionChangedGroup);
12406 const char* AllocationSite::PretenureDecisionName(PretenureDecision decision) {
12407 switch (decision) {
12408 case kUndecided: return "undecided";
12409 case kDontTenure: return "don't tenure";
12410 case kMaybeTenure: return "maybe tenure";
12411 case kTenure: return "tenure";
12412 case kZombie: return "zombie";
12413 default: UNREACHABLE();
12419 void JSObject::UpdateAllocationSite(Handle<JSObject> object,
12420 ElementsKind to_kind) {
12421 if (!object->IsJSArray()) return;
12423 Heap* heap = object->GetHeap();
12424 if (!heap->InNewSpace(*object)) return;
12426 Handle<AllocationSite> site;
12428 DisallowHeapAllocation no_allocation;
12430 AllocationMemento* memento = heap->FindAllocationMemento(*object);
12431 if (memento == NULL) return;
12433 // Walk through to the Allocation Site
12434 site = handle(memento->GetAllocationSite());
12436 AllocationSite::DigestTransitionFeedback(site, to_kind);
12440 void JSObject::TransitionElementsKind(Handle<JSObject> object,
12441 ElementsKind to_kind) {
12442 ElementsKind from_kind = object->GetElementsKind();
12444 if (IsFastHoleyElementsKind(from_kind)) {
12445 to_kind = GetHoleyElementsKind(to_kind);
12448 if (from_kind == to_kind) return;
12450 // This method should never be called for any other case.
12451 DCHECK(IsFastElementsKind(from_kind));
12452 DCHECK(IsFastElementsKind(to_kind));
12453 DCHECK_NE(TERMINAL_FAST_ELEMENTS_KIND, from_kind);
12455 UpdateAllocationSite(object, to_kind);
12456 if (object->elements() == object->GetHeap()->empty_fixed_array() ||
12457 IsFastDoubleElementsKind(from_kind) ==
12458 IsFastDoubleElementsKind(to_kind)) {
12459 // No change is needed to the elements() buffer, the transition
12460 // only requires a map change.
12461 Handle<Map> new_map = GetElementsTransitionMap(object, to_kind);
12462 MigrateToMap(object, new_map);
12463 if (FLAG_trace_elements_transitions) {
12464 Handle<FixedArrayBase> elms(object->elements());
12465 PrintElementsTransition(stdout, object, from_kind, elms, to_kind, elms);
12468 DCHECK((IsFastSmiElementsKind(from_kind) &&
12469 IsFastDoubleElementsKind(to_kind)) ||
12470 (IsFastDoubleElementsKind(from_kind) &&
12471 IsFastObjectElementsKind(to_kind)));
12472 uint32_t c = static_cast<uint32_t>(object->elements()->length());
12473 ElementsAccessor::ForKind(to_kind)->GrowCapacityAndConvert(object, c);
12479 bool Map::IsValidElementsTransition(ElementsKind from_kind,
12480 ElementsKind to_kind) {
12481 // Transitions can't go backwards.
12482 if (!IsMoreGeneralElementsKindTransition(from_kind, to_kind)) {
12486 // Transitions from HOLEY -> PACKED are not allowed.
12487 return !IsFastHoleyElementsKind(from_kind) ||
12488 IsFastHoleyElementsKind(to_kind);
12492 bool JSArray::HasReadOnlyLength(Handle<JSArray> array) {
12493 LookupIterator it(array, array->GetIsolate()->factory()->length_string(),
12494 LookupIterator::OWN_SKIP_INTERCEPTOR);
12495 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
12496 CHECK(it.IsFound());
12497 CHECK_EQ(LookupIterator::ACCESSOR, it.state());
12498 return it.IsReadOnly();
12502 bool JSArray::WouldChangeReadOnlyLength(Handle<JSArray> array,
12504 uint32_t length = 0;
12505 CHECK(array->length()->ToArrayLength(&length));
12506 if (length <= index) return HasReadOnlyLength(array);
12511 MaybeHandle<Object> JSArray::ReadOnlyLengthError(Handle<JSArray> array) {
12512 Isolate* isolate = array->GetIsolate();
12513 Handle<Name> length = isolate->factory()->length_string();
12516 NewTypeError(MessageTemplate::kStrictReadOnlyProperty, length, array),
12521 template <typename BackingStore>
12522 static int FastHoleyElementsUsage(JSObject* object, BackingStore* store) {
12523 int limit = object->IsJSArray()
12524 ? Smi::cast(JSArray::cast(object)->length())->value()
12527 for (int i = 0; i < limit; ++i) {
12528 if (!store->is_the_hole(i)) ++used;
12534 int JSObject::GetFastElementsUsage() {
12535 FixedArrayBase* store = elements();
12536 switch (GetElementsKind()) {
12537 case FAST_SMI_ELEMENTS:
12538 case FAST_DOUBLE_ELEMENTS:
12539 case FAST_ELEMENTS:
12540 // Only JSArray have packed elements.
12541 return Smi::cast(JSArray::cast(this)->length())->value();
12542 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
12543 store = FixedArray::cast(FixedArray::cast(store)->get(1));
12545 case FAST_HOLEY_SMI_ELEMENTS:
12546 case FAST_HOLEY_ELEMENTS:
12547 return FastHoleyElementsUsage(this, FixedArray::cast(store));
12548 case FAST_HOLEY_DOUBLE_ELEMENTS:
12549 if (elements()->length() == 0) return 0;
12550 return FastHoleyElementsUsage(this, FixedDoubleArray::cast(store));
12552 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
12553 case DICTIONARY_ELEMENTS:
12554 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
12555 case TYPE##_ELEMENTS: \
12557 TYPED_ARRAYS(TYPED_ARRAY_CASE)
12558 #undef TYPED_ARRAY_CASE
12565 // Certain compilers request function template instantiation when they
12566 // see the definition of the other template functions in the
12567 // class. This requires us to have the template functions put
12568 // together, so even though this function belongs in objects-debug.cc,
12569 // we keep it here instead to satisfy certain compilers.
12570 #ifdef OBJECT_PRINT
12571 template <typename Derived, typename Shape, typename Key>
12572 void Dictionary<Derived, Shape, Key>::Print(std::ostream& os) { // NOLINT
12573 int capacity = this->Capacity();
12574 for (int i = 0; i < capacity; i++) {
12575 Object* k = this->KeyAt(i);
12576 if (this->IsKey(k)) {
12578 if (k->IsString()) {
12579 String::cast(k)->StringPrint(os);
12583 os << ": " << Brief(this->ValueAt(i)) << " " << this->DetailsAt(i)
12591 template<typename Derived, typename Shape, typename Key>
12592 void Dictionary<Derived, Shape, Key>::CopyValuesTo(FixedArray* elements) {
12594 int capacity = this->Capacity();
12595 DisallowHeapAllocation no_gc;
12596 WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc);
12597 for (int i = 0; i < capacity; i++) {
12598 Object* k = this->KeyAt(i);
12599 if (this->IsKey(k)) {
12600 elements->set(pos++, this->ValueAt(i), mode);
12603 DCHECK(pos == elements->length());
12607 InterceptorInfo* JSObject::GetNamedInterceptor() {
12608 DCHECK(map()->has_named_interceptor());
12609 JSFunction* constructor = JSFunction::cast(map()->GetConstructor());
12610 DCHECK(constructor->shared()->IsApiFunction());
12612 constructor->shared()->get_api_func_data()->named_property_handler();
12613 return InterceptorInfo::cast(result);
12617 InterceptorInfo* JSObject::GetIndexedInterceptor() {
12618 DCHECK(map()->has_indexed_interceptor());
12619 JSFunction* constructor = JSFunction::cast(map()->GetConstructor());
12620 DCHECK(constructor->shared()->IsApiFunction());
12622 constructor->shared()->get_api_func_data()->indexed_property_handler();
12623 return InterceptorInfo::cast(result);
12627 MaybeHandle<Object> JSObject::GetPropertyWithInterceptor(LookupIterator* it,
12630 Isolate* isolate = it->isolate();
12631 // Make sure that the top context does not change when doing callbacks or
12632 // interceptor calls.
12633 AssertNoContextChange ncc(isolate);
12635 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
12636 Handle<InterceptorInfo> interceptor = it->GetInterceptor();
12637 if (interceptor->getter()->IsUndefined()) {
12638 return isolate->factory()->undefined_value();
12641 Handle<JSObject> holder = it->GetHolder<JSObject>();
12642 v8::Local<v8::Value> result;
12643 PropertyCallbackArguments args(isolate, interceptor->data(),
12644 *it->GetReceiver(), *holder);
12646 if (it->IsElement()) {
12647 uint32_t index = it->index();
12648 v8::IndexedPropertyGetterCallback getter =
12649 v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter());
12651 ApiIndexedPropertyAccess("interceptor-indexed-get", *holder, index));
12652 result = args.Call(getter, index);
12654 Handle<Name> name = it->name();
12656 if (name->IsSymbol() && !interceptor->can_intercept_symbols()) {
12657 return isolate->factory()->undefined_value();
12660 v8::GenericNamedPropertyGetterCallback getter =
12661 v8::ToCData<v8::GenericNamedPropertyGetterCallback>(
12662 interceptor->getter());
12664 ApiNamedPropertyAccess("interceptor-named-get", *holder, *name));
12665 result = args.Call(getter, v8::Utils::ToLocal(name));
12668 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
12669 if (result.IsEmpty()) return isolate->factory()->undefined_value();
12670 Handle<Object> result_internal = v8::Utils::OpenHandle(*result);
12671 result_internal->VerifyApiCallResultType();
12673 // Rebox handle before return
12674 return handle(*result_internal, isolate);
12678 // Compute the property keys from the interceptor.
12679 MaybeHandle<JSObject> JSObject::GetKeysForNamedInterceptor(
12680 Handle<JSObject> object, Handle<JSReceiver> receiver) {
12681 Isolate* isolate = receiver->GetIsolate();
12682 Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor());
12683 PropertyCallbackArguments
12684 args(isolate, interceptor->data(), *receiver, *object);
12685 v8::Local<v8::Object> result;
12686 if (!interceptor->enumerator()->IsUndefined()) {
12687 v8::GenericNamedPropertyEnumeratorCallback enum_fun =
12688 v8::ToCData<v8::GenericNamedPropertyEnumeratorCallback>(
12689 interceptor->enumerator());
12690 LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object));
12691 result = args.Call(enum_fun);
12693 if (result.IsEmpty()) return MaybeHandle<JSObject>();
12694 DCHECK(v8::Utils::OpenHandle(*result)->IsJSArray() ||
12695 v8::Utils::OpenHandle(*result)->HasSloppyArgumentsElements());
12696 // Rebox before returning.
12697 return handle(*v8::Utils::OpenHandle(*result), isolate);
12701 // Compute the element keys from the interceptor.
12702 MaybeHandle<JSObject> JSObject::GetKeysForIndexedInterceptor(
12703 Handle<JSObject> object, Handle<JSReceiver> receiver) {
12704 Isolate* isolate = receiver->GetIsolate();
12705 Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor());
12706 PropertyCallbackArguments
12707 args(isolate, interceptor->data(), *receiver, *object);
12708 v8::Local<v8::Object> result;
12709 if (!interceptor->enumerator()->IsUndefined()) {
12710 v8::IndexedPropertyEnumeratorCallback enum_fun =
12711 v8::ToCData<v8::IndexedPropertyEnumeratorCallback>(
12712 interceptor->enumerator());
12713 LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object));
12714 result = args.Call(enum_fun);
12716 if (result.IsEmpty()) return MaybeHandle<JSObject>();
12717 DCHECK(v8::Utils::OpenHandle(*result)->IsJSArray() ||
12718 v8::Utils::OpenHandle(*result)->HasSloppyArgumentsElements());
12719 // Rebox before returning.
12720 return handle(*v8::Utils::OpenHandle(*result), isolate);
12724 Maybe<bool> JSObject::HasRealNamedProperty(Handle<JSObject> object,
12725 Handle<Name> name) {
12726 LookupIterator it = LookupIterator::PropertyOrElement(
12727 name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
12728 Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it);
12729 if (!maybe_result.IsJust()) return Nothing<bool>();
12730 return Just(it.IsFound());
12734 Maybe<bool> JSObject::HasRealElementProperty(Handle<JSObject> object,
12736 Isolate* isolate = object->GetIsolate();
12737 LookupIterator it(isolate, object, index,
12738 LookupIterator::OWN_SKIP_INTERCEPTOR);
12739 Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it);
12740 if (!maybe_result.IsJust()) return Nothing<bool>();
12741 return Just(it.IsFound());
12745 Maybe<bool> JSObject::HasRealNamedCallbackProperty(Handle<JSObject> object,
12746 Handle<Name> name) {
12747 LookupIterator it = LookupIterator::PropertyOrElement(
12748 name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
12749 Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it);
12750 return maybe_result.IsJust() ? Just(it.state() == LookupIterator::ACCESSOR)
12755 int JSObject::NumberOfOwnProperties(PropertyAttributes filter) {
12756 if (HasFastProperties()) {
12757 Map* map = this->map();
12758 if (filter == NONE) return map->NumberOfOwnDescriptors();
12759 if (filter & DONT_ENUM) {
12760 int result = map->EnumLength();
12761 if (result != kInvalidEnumCacheSentinel) return result;
12763 return map->NumberOfDescribedProperties(OWN_DESCRIPTORS, filter);
12764 } else if (IsGlobalObject()) {
12765 return global_dictionary()->NumberOfElementsFilterAttributes(filter);
12767 return property_dictionary()->NumberOfElementsFilterAttributes(filter);
12772 void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) {
12773 Object* temp = get(i);
12776 if (this != numbers) {
12777 temp = numbers->get(i);
12778 numbers->set(i, Smi::cast(numbers->get(j)));
12779 numbers->set(j, Smi::cast(temp));
12784 static void InsertionSortPairs(FixedArray* content,
12785 FixedArray* numbers,
12787 for (int i = 1; i < len; i++) {
12790 (NumberToUint32(numbers->get(j - 1)) >
12791 NumberToUint32(numbers->get(j)))) {
12792 content->SwapPairs(numbers, j - 1, j);
12799 void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) {
12800 // In-place heap sort.
12801 DCHECK(content->length() == numbers->length());
12803 // Bottom-up max-heap construction.
12804 for (int i = 1; i < len; ++i) {
12805 int child_index = i;
12806 while (child_index > 0) {
12807 int parent_index = ((child_index + 1) >> 1) - 1;
12808 uint32_t parent_value = NumberToUint32(numbers->get(parent_index));
12809 uint32_t child_value = NumberToUint32(numbers->get(child_index));
12810 if (parent_value < child_value) {
12811 content->SwapPairs(numbers, parent_index, child_index);
12815 child_index = parent_index;
12819 // Extract elements and create sorted array.
12820 for (int i = len - 1; i > 0; --i) {
12821 // Put max element at the back of the array.
12822 content->SwapPairs(numbers, 0, i);
12823 // Sift down the new top element.
12824 int parent_index = 0;
12826 int child_index = ((parent_index + 1) << 1) - 1;
12827 if (child_index >= i) break;
12828 uint32_t child1_value = NumberToUint32(numbers->get(child_index));
12829 uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1));
12830 uint32_t parent_value = NumberToUint32(numbers->get(parent_index));
12831 if (child_index + 1 >= i || child1_value > child2_value) {
12832 if (parent_value > child1_value) break;
12833 content->SwapPairs(numbers, parent_index, child_index);
12834 parent_index = child_index;
12836 if (parent_value > child2_value) break;
12837 content->SwapPairs(numbers, parent_index, child_index + 1);
12838 parent_index = child_index + 1;
12845 // Sort this array and the numbers as pairs wrt. the (distinct) numbers.
12846 void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) {
12847 DCHECK(this->length() == numbers->length());
12848 // For small arrays, simply use insertion sort.
12850 InsertionSortPairs(this, numbers, len);
12853 // Check the range of indices.
12854 uint32_t min_index = NumberToUint32(numbers->get(0));
12855 uint32_t max_index = min_index;
12857 for (i = 1; i < len; i++) {
12858 if (NumberToUint32(numbers->get(i)) < min_index) {
12859 min_index = NumberToUint32(numbers->get(i));
12860 } else if (NumberToUint32(numbers->get(i)) > max_index) {
12861 max_index = NumberToUint32(numbers->get(i));
12864 if (max_index - min_index + 1 == len) {
12865 // Indices form a contiguous range, unless there are duplicates.
12866 // Do an in-place linear time sort assuming distinct numbers, but
12867 // avoid hanging in case they are not.
12868 for (i = 0; i < len; i++) {
12871 // While the current element at i is not at its correct position p,
12872 // swap the elements at these two positions.
12873 while ((p = NumberToUint32(numbers->get(i)) - min_index) != i &&
12875 SwapPairs(numbers, i, p);
12879 HeapSortPairs(this, numbers, len);
12885 // Fill in the names of own properties into the supplied storage. The main
12886 // purpose of this function is to provide reflection information for the object
12888 int JSObject::GetOwnPropertyNames(FixedArray* storage, int index,
12889 PropertyAttributes filter) {
12890 DCHECK(storage->length() >= (NumberOfOwnProperties(filter) - index));
12891 if (HasFastProperties()) {
12892 int start_index = index;
12893 int real_size = map()->NumberOfOwnDescriptors();
12894 DescriptorArray* descs = map()->instance_descriptors();
12895 for (int i = 0; i < real_size; i++) {
12896 if ((descs->GetDetails(i).attributes() & filter) == 0 &&
12897 !FilterKey(descs->GetKey(i), filter)) {
12898 storage->set(index++, descs->GetKey(i));
12901 return index - start_index;
12902 } else if (IsGlobalObject()) {
12903 return global_dictionary()->CopyKeysTo(storage, index, filter,
12904 GlobalDictionary::UNSORTED);
12906 return property_dictionary()->CopyKeysTo(storage, index, filter,
12907 NameDictionary::UNSORTED);
12912 int JSObject::NumberOfOwnElements(PropertyAttributes filter) {
12913 return GetOwnElementKeys(NULL, filter);
12917 int JSObject::NumberOfEnumElements() {
12918 // Fast case for objects with no elements.
12919 if (!IsJSValue() && HasFastObjectElements()) {
12920 uint32_t length = IsJSArray() ?
12921 static_cast<uint32_t>(
12922 Smi::cast(JSArray::cast(this)->length())->value()) :
12923 static_cast<uint32_t>(FixedArray::cast(elements())->length());
12924 if (length == 0) return 0;
12926 // Compute the number of enumerable elements.
12927 return NumberOfOwnElements(static_cast<PropertyAttributes>(DONT_ENUM));
12931 int JSObject::GetOwnElementKeys(FixedArray* storage,
12932 PropertyAttributes filter) {
12935 // If this is a String wrapper, add the string indices first,
12936 // as they're guaranteed to preced the elements in numerical order
12937 // and ascending order is required by ECMA-262, 6th, 9.1.12.
12939 Object* val = JSValue::cast(this)->value();
12940 if (val->IsString()) {
12941 String* str = String::cast(val);
12943 for (int i = 0; i < str->length(); i++) {
12944 storage->set(counter + i, Smi::FromInt(i));
12947 counter += str->length();
12951 switch (GetElementsKind()) {
12952 case FAST_SMI_ELEMENTS:
12953 case FAST_ELEMENTS:
12954 case FAST_HOLEY_SMI_ELEMENTS:
12955 case FAST_HOLEY_ELEMENTS: {
12956 int length = IsJSArray() ?
12957 Smi::cast(JSArray::cast(this)->length())->value() :
12958 FixedArray::cast(elements())->length();
12959 for (int i = 0; i < length; i++) {
12960 if (!FixedArray::cast(elements())->get(i)->IsTheHole()) {
12961 if (storage != NULL) {
12962 storage->set(counter, Smi::FromInt(i));
12967 DCHECK(!storage || storage->length() >= counter);
12970 case FAST_DOUBLE_ELEMENTS:
12971 case FAST_HOLEY_DOUBLE_ELEMENTS: {
12972 int length = IsJSArray() ?
12973 Smi::cast(JSArray::cast(this)->length())->value() :
12974 FixedArrayBase::cast(elements())->length();
12975 for (int i = 0; i < length; i++) {
12976 if (!FixedDoubleArray::cast(elements())->is_the_hole(i)) {
12977 if (storage != NULL) {
12978 storage->set(counter, Smi::FromInt(i));
12983 DCHECK(!storage || storage->length() >= counter);
12987 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
12988 case TYPE##_ELEMENTS: \
12990 TYPED_ARRAYS(TYPED_ARRAY_CASE)
12991 #undef TYPED_ARRAY_CASE
12993 int length = FixedArrayBase::cast(elements())->length();
12994 while (counter < length) {
12995 if (storage != NULL) {
12996 storage->set(counter, Smi::FromInt(counter));
13000 DCHECK(!storage || storage->length() >= counter);
13004 case DICTIONARY_ELEMENTS: {
13005 if (storage != NULL) {
13006 element_dictionary()->CopyKeysTo(storage, counter, filter,
13007 SeededNumberDictionary::SORTED);
13009 counter += element_dictionary()->NumberOfElementsFilterAttributes(filter);
13012 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
13013 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: {
13014 FixedArray* parameter_map = FixedArray::cast(elements());
13015 int mapped_length = parameter_map->length() - 2;
13016 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
13017 if (arguments->IsDictionary()) {
13018 // Copy the keys from arguments first, because Dictionary::CopyKeysTo
13019 // will insert in storage starting at index 0.
13020 SeededNumberDictionary* dictionary =
13021 SeededNumberDictionary::cast(arguments);
13022 if (storage != NULL) {
13023 dictionary->CopyKeysTo(storage, counter, filter,
13024 SeededNumberDictionary::UNSORTED);
13026 counter += dictionary->NumberOfElementsFilterAttributes(filter);
13027 for (int i = 0; i < mapped_length; ++i) {
13028 if (!parameter_map->get(i + 2)->IsTheHole()) {
13029 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13033 if (storage != NULL) storage->SortPairs(storage, counter);
13036 int backing_length = arguments->length();
13038 for (; i < mapped_length; ++i) {
13039 if (!parameter_map->get(i + 2)->IsTheHole()) {
13040 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13042 } else if (i < backing_length && !arguments->get(i)->IsTheHole()) {
13043 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13047 for (; i < backing_length; ++i) {
13048 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13056 DCHECK(!storage || storage->length() == counter);
13061 int JSObject::GetEnumElementKeys(FixedArray* storage) {
13062 return GetOwnElementKeys(storage, static_cast<PropertyAttributes>(DONT_ENUM));
13066 const char* Symbol::PrivateSymbolToName() const {
13067 Heap* heap = GetIsolate()->heap();
13068 #define SYMBOL_CHECK_AND_PRINT(name) \
13069 if (this == heap->name()) return #name;
13070 PRIVATE_SYMBOL_LIST(SYMBOL_CHECK_AND_PRINT)
13071 #undef SYMBOL_CHECK_AND_PRINT
13076 void Symbol::SymbolShortPrint(std::ostream& os) {
13077 os << "<Symbol: " << Hash();
13078 if (!name()->IsUndefined()) {
13080 HeapStringAllocator allocator;
13081 StringStream accumulator(&allocator);
13082 String::cast(name())->StringShortPrint(&accumulator);
13083 os << accumulator.ToCString().get();
13085 os << " (" << PrivateSymbolToName() << ")";
13091 // StringSharedKeys are used as keys in the eval cache.
13092 class StringSharedKey : public HashTableKey {
13094 StringSharedKey(Handle<String> source, Handle<SharedFunctionInfo> shared,
13095 LanguageMode language_mode, int scope_position)
13098 language_mode_(language_mode),
13099 scope_position_(scope_position) {}
13101 bool IsMatch(Object* other) override {
13102 DisallowHeapAllocation no_allocation;
13103 if (!other->IsFixedArray()) {
13104 if (!other->IsNumber()) return false;
13105 uint32_t other_hash = static_cast<uint32_t>(other->Number());
13106 return Hash() == other_hash;
13108 FixedArray* other_array = FixedArray::cast(other);
13109 SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0));
13110 if (shared != *shared_) return false;
13111 int language_unchecked = Smi::cast(other_array->get(2))->value();
13112 DCHECK(is_valid_language_mode(language_unchecked));
13113 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
13114 if (language_mode != language_mode_) return false;
13115 int scope_position = Smi::cast(other_array->get(3))->value();
13116 if (scope_position != scope_position_) return false;
13117 String* source = String::cast(other_array->get(1));
13118 return source->Equals(*source_);
13121 static uint32_t StringSharedHashHelper(String* source,
13122 SharedFunctionInfo* shared,
13123 LanguageMode language_mode,
13124 int scope_position) {
13125 uint32_t hash = source->Hash();
13126 if (shared->HasSourceCode()) {
13127 // Instead of using the SharedFunctionInfo pointer in the hash
13128 // code computation, we use a combination of the hash of the
13129 // script source code and the start position of the calling scope.
13130 // We do this to ensure that the cache entries can survive garbage
13132 Script* script(Script::cast(shared->script()));
13133 hash ^= String::cast(script->source())->Hash();
13134 STATIC_ASSERT(LANGUAGE_END == 3);
13135 if (is_strict(language_mode)) hash ^= 0x8000;
13136 if (is_strong(language_mode)) hash ^= 0x10000;
13137 hash += scope_position;
13142 uint32_t Hash() override {
13143 return StringSharedHashHelper(*source_, *shared_, language_mode_,
13147 uint32_t HashForObject(Object* obj) override {
13148 DisallowHeapAllocation no_allocation;
13149 if (obj->IsNumber()) {
13150 return static_cast<uint32_t>(obj->Number());
13152 FixedArray* other_array = FixedArray::cast(obj);
13153 SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0));
13154 String* source = String::cast(other_array->get(1));
13155 int language_unchecked = Smi::cast(other_array->get(2))->value();
13156 DCHECK(is_valid_language_mode(language_unchecked));
13157 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
13158 int scope_position = Smi::cast(other_array->get(3))->value();
13159 return StringSharedHashHelper(source, shared, language_mode,
13164 Handle<Object> AsHandle(Isolate* isolate) override {
13165 Handle<FixedArray> array = isolate->factory()->NewFixedArray(4);
13166 array->set(0, *shared_);
13167 array->set(1, *source_);
13168 array->set(2, Smi::FromInt(language_mode_));
13169 array->set(3, Smi::FromInt(scope_position_));
13174 Handle<String> source_;
13175 Handle<SharedFunctionInfo> shared_;
13176 LanguageMode language_mode_;
13177 int scope_position_;
13181 // RegExpKey carries the source and flags of a regular expression as key.
13182 class RegExpKey : public HashTableKey {
13184 RegExpKey(Handle<String> string, JSRegExp::Flags flags)
13186 flags_(Smi::FromInt(flags.value())) { }
13188 // Rather than storing the key in the hash table, a pointer to the
13189 // stored value is stored where the key should be. IsMatch then
13190 // compares the search key to the found object, rather than comparing
13192 bool IsMatch(Object* obj) override {
13193 FixedArray* val = FixedArray::cast(obj);
13194 return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex)))
13195 && (flags_ == val->get(JSRegExp::kFlagsIndex));
13198 uint32_t Hash() override { return RegExpHash(*string_, flags_); }
13200 Handle<Object> AsHandle(Isolate* isolate) override {
13201 // Plain hash maps, which is where regexp keys are used, don't
13202 // use this function.
13204 return MaybeHandle<Object>().ToHandleChecked();
13207 uint32_t HashForObject(Object* obj) override {
13208 FixedArray* val = FixedArray::cast(obj);
13209 return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)),
13210 Smi::cast(val->get(JSRegExp::kFlagsIndex)));
13213 static uint32_t RegExpHash(String* string, Smi* flags) {
13214 return string->Hash() + flags->value();
13217 Handle<String> string_;
13222 Handle<Object> OneByteStringKey::AsHandle(Isolate* isolate) {
13223 if (hash_field_ == 0) Hash();
13224 return isolate->factory()->NewOneByteInternalizedString(string_, hash_field_);
13228 Handle<Object> TwoByteStringKey::AsHandle(Isolate* isolate) {
13229 if (hash_field_ == 0) Hash();
13230 return isolate->factory()->NewTwoByteInternalizedString(string_, hash_field_);
13234 Handle<Object> SeqOneByteSubStringKey::AsHandle(Isolate* isolate) {
13235 if (hash_field_ == 0) Hash();
13236 return isolate->factory()->NewOneByteInternalizedSubString(
13237 string_, from_, length_, hash_field_);
13241 bool SeqOneByteSubStringKey::IsMatch(Object* string) {
13242 Vector<const uint8_t> chars(string_->GetChars() + from_, length_);
13243 return String::cast(string)->IsOneByteEqualTo(chars);
13247 // InternalizedStringKey carries a string/internalized-string object as key.
13248 class InternalizedStringKey : public HashTableKey {
13250 explicit InternalizedStringKey(Handle<String> string)
13251 : string_(string) { }
13253 bool IsMatch(Object* string) override {
13254 return String::cast(string)->Equals(*string_);
13257 uint32_t Hash() override { return string_->Hash(); }
13259 uint32_t HashForObject(Object* other) override {
13260 return String::cast(other)->Hash();
13263 Handle<Object> AsHandle(Isolate* isolate) override {
13264 // Internalize the string if possible.
13265 MaybeHandle<Map> maybe_map =
13266 isolate->factory()->InternalizedStringMapForString(string_);
13268 if (maybe_map.ToHandle(&map)) {
13269 string_->set_map_no_write_barrier(*map);
13270 DCHECK(string_->IsInternalizedString());
13273 // Otherwise allocate a new internalized string.
13274 return isolate->factory()->NewInternalizedStringImpl(
13275 string_, string_->length(), string_->hash_field());
13278 static uint32_t StringHash(Object* obj) {
13279 return String::cast(obj)->Hash();
13282 Handle<String> string_;
13286 template<typename Derived, typename Shape, typename Key>
13287 void HashTable<Derived, Shape, Key>::IteratePrefix(ObjectVisitor* v) {
13288 IteratePointers(v, 0, kElementsStartOffset);
13292 template<typename Derived, typename Shape, typename Key>
13293 void HashTable<Derived, Shape, Key>::IterateElements(ObjectVisitor* v) {
13295 kElementsStartOffset,
13296 kHeaderSize + length() * kPointerSize);
13300 template<typename Derived, typename Shape, typename Key>
13301 Handle<Derived> HashTable<Derived, Shape, Key>::New(
13303 int at_least_space_for,
13304 MinimumCapacity capacity_option,
13305 PretenureFlag pretenure) {
13306 DCHECK(0 <= at_least_space_for);
13307 DCHECK(!capacity_option || base::bits::IsPowerOfTwo32(at_least_space_for));
13309 int capacity = (capacity_option == USE_CUSTOM_MINIMUM_CAPACITY)
13310 ? at_least_space_for
13311 : ComputeCapacity(at_least_space_for);
13312 if (capacity > HashTable::kMaxCapacity) {
13313 v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true);
13316 Factory* factory = isolate->factory();
13317 int length = EntryToIndex(capacity);
13318 Handle<FixedArray> array = factory->NewFixedArray(length, pretenure);
13319 array->set_map_no_write_barrier(*factory->hash_table_map());
13320 Handle<Derived> table = Handle<Derived>::cast(array);
13322 table->SetNumberOfElements(0);
13323 table->SetNumberOfDeletedElements(0);
13324 table->SetCapacity(capacity);
13329 // Find entry for key otherwise return kNotFound.
13330 template <typename Derived, typename Shape>
13331 int NameDictionaryBase<Derived, Shape>::FindEntry(Handle<Name> key) {
13332 if (!key->IsUniqueName()) {
13333 return DerivedDictionary::FindEntry(key);
13336 // Optimized for unique names. Knowledge of the key type allows:
13337 // 1. Move the check if the key is unique out of the loop.
13338 // 2. Avoid comparing hash codes in unique-to-unique comparison.
13339 // 3. Detect a case when a dictionary key is not unique but the key is.
13340 // In case of positive result the dictionary key may be replaced by the
13341 // internalized string with minimal performance penalty. It gives a chance
13342 // to perform further lookups in code stubs (and significant performance
13343 // boost a certain style of code).
13345 // EnsureCapacity will guarantee the hash table is never full.
13346 uint32_t capacity = this->Capacity();
13347 uint32_t entry = Derived::FirstProbe(key->Hash(), capacity);
13348 uint32_t count = 1;
13351 int index = Derived::EntryToIndex(entry);
13352 Object* element = this->get(index);
13353 if (element->IsUndefined()) break; // Empty entry.
13354 if (*key == element) return entry;
13355 if (!element->IsUniqueName() &&
13356 !element->IsTheHole() &&
13357 Name::cast(element)->Equals(*key)) {
13358 // Replace a key that is a non-internalized string by the equivalent
13359 // internalized string for faster further lookups.
13360 this->set(index, *key);
13363 DCHECK(element->IsTheHole() || !Name::cast(element)->Equals(*key));
13364 entry = Derived::NextProbe(entry, count++, capacity);
13366 return Derived::kNotFound;
13370 template<typename Derived, typename Shape, typename Key>
13371 void HashTable<Derived, Shape, Key>::Rehash(
13372 Handle<Derived> new_table,
13374 DCHECK(NumberOfElements() < new_table->Capacity());
13376 DisallowHeapAllocation no_gc;
13377 WriteBarrierMode mode = new_table->GetWriteBarrierMode(no_gc);
13379 // Copy prefix to new array.
13380 for (int i = kPrefixStartIndex;
13381 i < kPrefixStartIndex + Shape::kPrefixSize;
13383 new_table->set(i, get(i), mode);
13386 // Rehash the elements.
13387 int capacity = this->Capacity();
13388 for (int i = 0; i < capacity; i++) {
13389 uint32_t from_index = EntryToIndex(i);
13390 Object* k = this->get(from_index);
13392 uint32_t hash = this->HashForObject(key, k);
13393 uint32_t insertion_index =
13394 EntryToIndex(new_table->FindInsertionEntry(hash));
13395 for (int j = 0; j < Shape::kEntrySize; j++) {
13396 new_table->set(insertion_index + j, get(from_index + j), mode);
13400 new_table->SetNumberOfElements(NumberOfElements());
13401 new_table->SetNumberOfDeletedElements(0);
13405 template<typename Derived, typename Shape, typename Key>
13406 uint32_t HashTable<Derived, Shape, Key>::EntryForProbe(
13410 uint32_t expected) {
13411 uint32_t hash = this->HashForObject(key, k);
13412 uint32_t capacity = this->Capacity();
13413 uint32_t entry = FirstProbe(hash, capacity);
13414 for (int i = 1; i < probe; i++) {
13415 if (entry == expected) return expected;
13416 entry = NextProbe(entry, i, capacity);
13422 template<typename Derived, typename Shape, typename Key>
13423 void HashTable<Derived, Shape, Key>::Swap(uint32_t entry1,
13425 WriteBarrierMode mode) {
13426 int index1 = EntryToIndex(entry1);
13427 int index2 = EntryToIndex(entry2);
13428 Object* temp[Shape::kEntrySize];
13429 for (int j = 0; j < Shape::kEntrySize; j++) {
13430 temp[j] = get(index1 + j);
13432 for (int j = 0; j < Shape::kEntrySize; j++) {
13433 set(index1 + j, get(index2 + j), mode);
13435 for (int j = 0; j < Shape::kEntrySize; j++) {
13436 set(index2 + j, temp[j], mode);
13441 template<typename Derived, typename Shape, typename Key>
13442 void HashTable<Derived, Shape, Key>::Rehash(Key key) {
13443 DisallowHeapAllocation no_gc;
13444 WriteBarrierMode mode = GetWriteBarrierMode(no_gc);
13445 uint32_t capacity = Capacity();
13447 for (int probe = 1; !done; probe++) {
13448 // All elements at entries given by one of the first _probe_ probes
13449 // are placed correctly. Other elements might need to be moved.
13451 for (uint32_t current = 0; current < capacity; current++) {
13452 Object* current_key = get(EntryToIndex(current));
13453 if (IsKey(current_key)) {
13454 uint32_t target = EntryForProbe(key, current_key, probe, current);
13455 if (current == target) continue;
13456 Object* target_key = get(EntryToIndex(target));
13457 if (!IsKey(target_key) ||
13458 EntryForProbe(key, target_key, probe, target) != target) {
13459 // Put the current element into the correct position.
13460 Swap(current, target, mode);
13461 // The other element will be processed on the next iteration.
13464 // The place for the current element is occupied. Leave the element
13465 // for the next probe.
13474 template<typename Derived, typename Shape, typename Key>
13475 Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
13476 Handle<Derived> table,
13479 PretenureFlag pretenure) {
13480 Isolate* isolate = table->GetIsolate();
13481 int capacity = table->Capacity();
13482 int nof = table->NumberOfElements() + n;
13483 int nod = table->NumberOfDeletedElements();
13485 // 50% is still free after adding n elements and
13486 // at most 50% of the free elements are deleted elements.
13487 if (nod <= (capacity - nof) >> 1) {
13488 int needed_free = nof >> 1;
13489 if (nof + needed_free <= capacity) return table;
13492 const int kMinCapacityForPretenure = 256;
13493 bool should_pretenure = pretenure == TENURED ||
13494 ((capacity > kMinCapacityForPretenure) &&
13495 !isolate->heap()->InNewSpace(*table));
13496 Handle<Derived> new_table = HashTable::New(
13499 USE_DEFAULT_MINIMUM_CAPACITY,
13500 should_pretenure ? TENURED : NOT_TENURED);
13502 table->Rehash(new_table, key);
13507 template<typename Derived, typename Shape, typename Key>
13508 Handle<Derived> HashTable<Derived, Shape, Key>::Shrink(Handle<Derived> table,
13510 int capacity = table->Capacity();
13511 int nof = table->NumberOfElements();
13513 // Shrink to fit the number of elements if only a quarter of the
13514 // capacity is filled with elements.
13515 if (nof > (capacity >> 2)) return table;
13516 // Allocate a new dictionary with room for at least the current
13517 // number of elements. The allocation method will make sure that
13518 // there is extra room in the dictionary for additions. Don't go
13519 // lower than room for 16 elements.
13520 int at_least_room_for = nof;
13521 if (at_least_room_for < 16) return table;
13523 Isolate* isolate = table->GetIsolate();
13524 const int kMinCapacityForPretenure = 256;
13526 (at_least_room_for > kMinCapacityForPretenure) &&
13527 !isolate->heap()->InNewSpace(*table);
13528 Handle<Derived> new_table = HashTable::New(
13531 USE_DEFAULT_MINIMUM_CAPACITY,
13532 pretenure ? TENURED : NOT_TENURED);
13534 table->Rehash(new_table, key);
13539 template<typename Derived, typename Shape, typename Key>
13540 uint32_t HashTable<Derived, Shape, Key>::FindInsertionEntry(uint32_t hash) {
13541 uint32_t capacity = Capacity();
13542 uint32_t entry = FirstProbe(hash, capacity);
13543 uint32_t count = 1;
13544 // EnsureCapacity will guarantee the hash table is never full.
13546 Object* element = KeyAt(entry);
13547 if (element->IsUndefined() || element->IsTheHole()) break;
13548 entry = NextProbe(entry, count++, capacity);
13554 // Force instantiation of template instances class.
13555 // Please note this list is compiler dependent.
13557 template class HashTable<StringTable, StringTableShape, HashTableKey*>;
13559 template class HashTable<CompilationCacheTable,
13560 CompilationCacheShape,
13563 template class HashTable<ObjectHashTable,
13564 ObjectHashTableShape,
13567 template class HashTable<WeakHashTable, WeakHashTableShape<2>, Handle<Object> >;
13569 template class Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >;
13571 template class Dictionary<GlobalDictionary, GlobalDictionaryShape,
13574 template class Dictionary<SeededNumberDictionary,
13575 SeededNumberDictionaryShape,
13578 template class Dictionary<UnseededNumberDictionary,
13579 UnseededNumberDictionaryShape,
13582 template Handle<SeededNumberDictionary>
13583 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13584 New(Isolate*, int at_least_space_for, PretenureFlag pretenure);
13586 template Handle<UnseededNumberDictionary>
13587 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13588 New(Isolate*, int at_least_space_for, PretenureFlag pretenure);
13590 template Handle<NameDictionary>
13591 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
13592 New(Isolate*, int n, PretenureFlag pretenure);
13594 template Handle<GlobalDictionary>
13595 Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::New(
13596 Isolate*, int n, PretenureFlag pretenure);
13598 template Handle<SeededNumberDictionary>
13599 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13600 AtPut(Handle<SeededNumberDictionary>, uint32_t, Handle<Object>);
13602 template Handle<UnseededNumberDictionary>
13603 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13604 AtPut(Handle<UnseededNumberDictionary>, uint32_t, Handle<Object>);
13607 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13608 SlowReverseLookup(Object* value);
13611 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
13612 SlowReverseLookup(Object* value);
13614 template Handle<Object>
13615 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::DeleteProperty(
13616 Handle<NameDictionary>, int);
13618 template Handle<Object>
13619 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape,
13620 uint32_t>::DeleteProperty(Handle<SeededNumberDictionary>, int);
13622 template Handle<NameDictionary>
13623 HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >::
13624 New(Isolate*, int, MinimumCapacity, PretenureFlag);
13626 template Handle<NameDictionary>
13627 HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >::
13628 Shrink(Handle<NameDictionary>, Handle<Name>);
13630 template Handle<SeededNumberDictionary>
13631 HashTable<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13632 Shrink(Handle<SeededNumberDictionary>, uint32_t);
13634 template Handle<NameDictionary>
13635 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::Add(
13636 Handle<NameDictionary>, Handle<Name>, Handle<Object>, PropertyDetails);
13638 template Handle<GlobalDictionary>
13639 Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::Add(
13640 Handle<GlobalDictionary>, Handle<Name>, Handle<Object>,
13643 template Handle<FixedArray> Dictionary<
13644 NameDictionary, NameDictionaryShape,
13645 Handle<Name> >::BuildIterationIndicesArray(Handle<NameDictionary>);
13647 template Handle<FixedArray> Dictionary<
13648 NameDictionary, NameDictionaryShape,
13649 Handle<Name> >::GenerateNewEnumerationIndices(Handle<NameDictionary>);
13651 template Handle<SeededNumberDictionary>
13652 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13653 Add(Handle<SeededNumberDictionary>,
13658 template Handle<UnseededNumberDictionary>
13659 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13660 Add(Handle<UnseededNumberDictionary>,
13665 template Handle<SeededNumberDictionary>
13666 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13667 EnsureCapacity(Handle<SeededNumberDictionary>, int, uint32_t);
13669 template Handle<UnseededNumberDictionary>
13670 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13671 EnsureCapacity(Handle<UnseededNumberDictionary>, int, uint32_t);
13673 template Handle<NameDictionary>
13674 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
13675 EnsureCapacity(Handle<NameDictionary>, int, Handle<Name>);
13677 template bool Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape,
13678 uint32_t>::HasComplexElements();
13680 template int HashTable<SeededNumberDictionary, SeededNumberDictionaryShape,
13681 uint32_t>::FindEntry(uint32_t);
13683 template int NameDictionaryBase<NameDictionary, NameDictionaryShape>::FindEntry(
13687 Handle<Object> JSObject::PrepareSlowElementsForSort(
13688 Handle<JSObject> object, uint32_t limit) {
13689 DCHECK(object->HasDictionaryElements());
13690 Isolate* isolate = object->GetIsolate();
13691 // Must stay in dictionary mode, either because of requires_slow_elements,
13692 // or because we are not going to sort (and therefore compact) all of the
13694 Handle<SeededNumberDictionary> dict(object->element_dictionary(), isolate);
13695 Handle<SeededNumberDictionary> new_dict =
13696 SeededNumberDictionary::New(isolate, dict->NumberOfElements());
13699 uint32_t undefs = 0;
13700 int capacity = dict->Capacity();
13701 Handle<Smi> bailout(Smi::FromInt(-1), isolate);
13702 // Entry to the new dictionary does not cause it to grow, as we have
13703 // allocated one that is large enough for all entries.
13704 DisallowHeapAllocation no_gc;
13705 for (int i = 0; i < capacity; i++) {
13706 Object* k = dict->KeyAt(i);
13707 if (!dict->IsKey(k)) continue;
13709 DCHECK(k->IsNumber());
13710 DCHECK(!k->IsSmi() || Smi::cast(k)->value() >= 0);
13711 DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0);
13712 DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32);
13714 HandleScope scope(isolate);
13715 Handle<Object> value(dict->ValueAt(i), isolate);
13716 PropertyDetails details = dict->DetailsAt(i);
13717 if (details.type() == ACCESSOR_CONSTANT || details.IsReadOnly()) {
13718 // Bail out and do the sorting of undefineds and array holes in JS.
13719 // Also bail out if the element is not supposed to be moved.
13723 uint32_t key = NumberToUint32(k);
13725 if (value->IsUndefined()) {
13727 } else if (pos > static_cast<uint32_t>(Smi::kMaxValue)) {
13728 // Adding an entry with the key beyond smi-range requires
13729 // allocation. Bailout.
13732 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
13733 new_dict, pos, value, details, object->map()->is_prototype_map());
13734 DCHECK(result.is_identical_to(new_dict));
13738 } else if (key > static_cast<uint32_t>(Smi::kMaxValue)) {
13739 // Adding an entry with the key beyond smi-range requires
13740 // allocation. Bailout.
13743 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
13744 new_dict, key, value, details, object->map()->is_prototype_map());
13745 DCHECK(result.is_identical_to(new_dict));
13750 uint32_t result = pos;
13751 PropertyDetails no_details = PropertyDetails::Empty();
13752 while (undefs > 0) {
13753 if (pos > static_cast<uint32_t>(Smi::kMaxValue)) {
13754 // Adding an entry with the key beyond smi-range requires
13755 // allocation. Bailout.
13758 HandleScope scope(isolate);
13759 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
13760 new_dict, pos, isolate->factory()->undefined_value(), no_details,
13761 object->map()->is_prototype_map());
13762 DCHECK(result.is_identical_to(new_dict));
13768 object->set_elements(*new_dict);
13770 AllowHeapAllocation allocate_return_value;
13771 return isolate->factory()->NewNumberFromUint(result);
13775 // Collects all defined (non-hole) and non-undefined (array) elements at
13776 // the start of the elements array.
13777 // If the object is in dictionary mode, it is converted to fast elements
13779 Handle<Object> JSObject::PrepareElementsForSort(Handle<JSObject> object,
13781 Isolate* isolate = object->GetIsolate();
13782 if (object->HasSloppyArgumentsElements() ||
13783 object->map()->is_observed()) {
13784 return handle(Smi::FromInt(-1), isolate);
13787 if (object->HasDictionaryElements()) {
13788 // Convert to fast elements containing only the existing properties.
13789 // Ordering is irrelevant, since we are going to sort anyway.
13790 Handle<SeededNumberDictionary> dict(object->element_dictionary());
13791 if (object->IsJSArray() || dict->requires_slow_elements() ||
13792 dict->max_number_key() >= limit) {
13793 return JSObject::PrepareSlowElementsForSort(object, limit);
13795 // Convert to fast elements.
13797 Handle<Map> new_map =
13798 JSObject::GetElementsTransitionMap(object, FAST_HOLEY_ELEMENTS);
13800 PretenureFlag tenure = isolate->heap()->InNewSpace(*object) ?
13801 NOT_TENURED: TENURED;
13802 Handle<FixedArray> fast_elements =
13803 isolate->factory()->NewFixedArray(dict->NumberOfElements(), tenure);
13804 dict->CopyValuesTo(*fast_elements);
13805 JSObject::ValidateElements(object);
13807 JSObject::SetMapAndElements(object, new_map, fast_elements);
13808 } else if (object->HasFixedTypedArrayElements()) {
13809 // Typed arrays cannot have holes or undefined elements.
13810 return handle(Smi::FromInt(
13811 FixedArrayBase::cast(object->elements())->length()), isolate);
13812 } else if (!object->HasFastDoubleElements()) {
13813 EnsureWritableFastElements(object);
13815 DCHECK(object->HasFastSmiOrObjectElements() ||
13816 object->HasFastDoubleElements());
13818 // Collect holes at the end, undefined before that and the rest at the
13819 // start, and return the number of non-hole, non-undefined values.
13821 Handle<FixedArrayBase> elements_base(object->elements());
13822 uint32_t elements_length = static_cast<uint32_t>(elements_base->length());
13823 if (limit > elements_length) {
13824 limit = elements_length ;
13827 return handle(Smi::FromInt(0), isolate);
13830 uint32_t result = 0;
13831 if (elements_base->map() == isolate->heap()->fixed_double_array_map()) {
13832 FixedDoubleArray* elements = FixedDoubleArray::cast(*elements_base);
13833 // Split elements into defined and the_hole, in that order.
13834 unsigned int holes = limit;
13835 // Assume most arrays contain no holes and undefined values, so minimize the
13836 // number of stores of non-undefined, non-the-hole values.
13837 for (unsigned int i = 0; i < holes; i++) {
13838 if (elements->is_the_hole(i)) {
13843 // Position i needs to be filled.
13844 while (holes > i) {
13845 if (elements->is_the_hole(holes)) {
13848 elements->set(i, elements->get_scalar(holes));
13854 while (holes < limit) {
13855 elements->set_the_hole(holes);
13859 FixedArray* elements = FixedArray::cast(*elements_base);
13860 DisallowHeapAllocation no_gc;
13862 // Split elements into defined, undefined and the_hole, in that order. Only
13863 // count locations for undefined and the hole, and fill them afterwards.
13864 WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_gc);
13865 unsigned int undefs = limit;
13866 unsigned int holes = limit;
13867 // Assume most arrays contain no holes and undefined values, so minimize the
13868 // number of stores of non-undefined, non-the-hole values.
13869 for (unsigned int i = 0; i < undefs; i++) {
13870 Object* current = elements->get(i);
13871 if (current->IsTheHole()) {
13874 } else if (current->IsUndefined()) {
13879 // Position i needs to be filled.
13880 while (undefs > i) {
13881 current = elements->get(undefs);
13882 if (current->IsTheHole()) {
13885 } else if (current->IsUndefined()) {
13888 elements->set(i, current, write_barrier);
13894 while (undefs < holes) {
13895 elements->set_undefined(undefs);
13898 while (holes < limit) {
13899 elements->set_the_hole(holes);
13904 return isolate->factory()->NewNumberFromUint(result);
13908 ExternalArrayType JSTypedArray::type() {
13909 switch (elements()->map()->instance_type()) {
13910 #define INSTANCE_TYPE_TO_ARRAY_TYPE(Type, type, TYPE, ctype, size) \
13911 case FIXED_##TYPE##_ARRAY_TYPE: \
13912 return kExternal##Type##Array;
13914 TYPED_ARRAYS(INSTANCE_TYPE_TO_ARRAY_TYPE)
13915 #undef INSTANCE_TYPE_TO_ARRAY_TYPE
13919 return static_cast<ExternalArrayType>(-1);
13924 size_t JSTypedArray::element_size() {
13925 switch (elements()->map()->instance_type()) {
13926 #define INSTANCE_TYPE_TO_ELEMENT_SIZE(Type, type, TYPE, ctype, size) \
13927 case FIXED_##TYPE##_ARRAY_TYPE: \
13930 TYPED_ARRAYS(INSTANCE_TYPE_TO_ELEMENT_SIZE)
13931 #undef INSTANCE_TYPE_TO_ELEMENT_SIZE
13940 void FixedArray::SetValue(uint32_t index, Object* value) { set(index, value); }
13943 void FixedDoubleArray::SetValue(uint32_t index, Object* value) {
13944 set(index, value->Number());
13946 void GlobalObject::InvalidatePropertyCell(Handle<GlobalObject> global,
13947 Handle<Name> name) {
13948 DCHECK(!global->HasFastProperties());
13949 auto dictionary = handle(global->global_dictionary());
13950 int entry = dictionary->FindEntry(name);
13951 if (entry == GlobalDictionary::kNotFound) return;
13952 PropertyCell::InvalidateEntry(dictionary, entry);
13956 // TODO(ishell): rename to EnsureEmptyPropertyCell or something.
13957 Handle<PropertyCell> GlobalObject::EnsurePropertyCell(
13958 Handle<GlobalObject> global, Handle<Name> name) {
13959 DCHECK(!global->HasFastProperties());
13960 auto dictionary = handle(global->global_dictionary());
13961 int entry = dictionary->FindEntry(name);
13962 Handle<PropertyCell> cell;
13963 if (entry != GlobalDictionary::kNotFound) {
13964 // This call should be idempotent.
13965 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
13966 cell = handle(PropertyCell::cast(dictionary->ValueAt(entry)));
13967 DCHECK(cell->property_details().cell_type() ==
13968 PropertyCellType::kUninitialized ||
13969 cell->property_details().cell_type() ==
13970 PropertyCellType::kInvalidated);
13971 DCHECK(cell->value()->IsTheHole());
13974 Isolate* isolate = global->GetIsolate();
13975 cell = isolate->factory()->NewPropertyCell();
13976 PropertyDetails details(NONE, DATA, 0, PropertyCellType::kUninitialized);
13977 dictionary = GlobalDictionary::Add(dictionary, name, cell, details);
13978 global->set_properties(*dictionary);
13983 // This class is used for looking up two character strings in the string table.
13984 // If we don't have a hit we don't want to waste much time so we unroll the
13985 // string hash calculation loop here for speed. Doesn't work if the two
13986 // characters form a decimal integer, since such strings have a different hash
13988 class TwoCharHashTableKey : public HashTableKey {
13990 TwoCharHashTableKey(uint16_t c1, uint16_t c2, uint32_t seed)
13991 : c1_(c1), c2_(c2) {
13993 uint32_t hash = seed;
13995 hash += hash << 10;
13999 hash += hash << 10;
14003 hash ^= hash >> 11;
14004 hash += hash << 15;
14005 if ((hash & String::kHashBitMask) == 0) hash = StringHasher::kZeroHash;
14008 // If this assert fails then we failed to reproduce the two-character
14009 // version of the string hashing algorithm above. One reason could be
14010 // that we were passed two digits as characters, since the hash
14011 // algorithm is different in that case.
14012 uint16_t chars[2] = {c1, c2};
14013 uint32_t check_hash = StringHasher::HashSequentialString(chars, 2, seed);
14014 hash = (hash << String::kHashShift) | String::kIsNotArrayIndexMask;
14015 DCHECK_EQ(static_cast<int32_t>(hash), static_cast<int32_t>(check_hash));
14019 bool IsMatch(Object* o) override {
14020 if (!o->IsString()) return false;
14021 String* other = String::cast(o);
14022 if (other->length() != 2) return false;
14023 if (other->Get(0) != c1_) return false;
14024 return other->Get(1) == c2_;
14027 uint32_t Hash() override { return hash_; }
14028 uint32_t HashForObject(Object* key) override {
14029 if (!key->IsString()) return 0;
14030 return String::cast(key)->Hash();
14033 Handle<Object> AsHandle(Isolate* isolate) override {
14034 // The TwoCharHashTableKey is only used for looking in the string
14035 // table, not for adding to it.
14037 return MaybeHandle<Object>().ToHandleChecked();
14047 MaybeHandle<String> StringTable::InternalizeStringIfExists(
14049 Handle<String> string) {
14050 if (string->IsInternalizedString()) {
14053 return LookupStringIfExists(isolate, string);
14057 MaybeHandle<String> StringTable::LookupStringIfExists(
14059 Handle<String> string) {
14060 Handle<StringTable> string_table = isolate->factory()->string_table();
14061 InternalizedStringKey key(string);
14062 int entry = string_table->FindEntry(&key);
14063 if (entry == kNotFound) {
14064 return MaybeHandle<String>();
14066 Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate);
14067 DCHECK(StringShape(*result).IsInternalized());
14073 MaybeHandle<String> StringTable::LookupTwoCharsStringIfExists(
14077 Handle<StringTable> string_table = isolate->factory()->string_table();
14078 TwoCharHashTableKey key(c1, c2, isolate->heap()->HashSeed());
14079 int entry = string_table->FindEntry(&key);
14080 if (entry == kNotFound) {
14081 return MaybeHandle<String>();
14083 Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate);
14084 DCHECK(StringShape(*result).IsInternalized());
14090 void StringTable::EnsureCapacityForDeserialization(Isolate* isolate,
14092 Handle<StringTable> table = isolate->factory()->string_table();
14093 // We need a key instance for the virtual hash function.
14094 InternalizedStringKey dummy_key(Handle<String>::null());
14095 table = StringTable::EnsureCapacity(table, expected, &dummy_key);
14096 isolate->factory()->set_string_table(table);
14100 Handle<String> StringTable::LookupString(Isolate* isolate,
14101 Handle<String> string) {
14102 InternalizedStringKey key(string);
14103 return LookupKey(isolate, &key);
14107 Handle<String> StringTable::LookupKey(Isolate* isolate, HashTableKey* key) {
14108 Handle<StringTable> table = isolate->factory()->string_table();
14109 int entry = table->FindEntry(key);
14111 // String already in table.
14112 if (entry != kNotFound) {
14113 return handle(String::cast(table->KeyAt(entry)), isolate);
14116 // Adding new string. Grow table if needed.
14117 table = StringTable::EnsureCapacity(table, 1, key);
14119 // Create string object.
14120 Handle<Object> string = key->AsHandle(isolate);
14121 // There must be no attempts to internalize strings that could throw
14122 // InvalidStringLength error.
14123 CHECK(!string.is_null());
14125 // Add the new string and return it along with the string table.
14126 entry = table->FindInsertionEntry(key->Hash());
14127 table->set(EntryToIndex(entry), *string);
14128 table->ElementAdded();
14130 isolate->factory()->set_string_table(table);
14131 return Handle<String>::cast(string);
14135 String* StringTable::LookupKeyIfExists(Isolate* isolate, HashTableKey* key) {
14136 Handle<StringTable> table = isolate->factory()->string_table();
14137 int entry = table->FindEntry(key);
14138 if (entry != kNotFound) return String::cast(table->KeyAt(entry));
14143 Handle<Object> CompilationCacheTable::Lookup(Handle<String> src,
14144 Handle<Context> context,
14145 LanguageMode language_mode) {
14146 Isolate* isolate = GetIsolate();
14147 Handle<SharedFunctionInfo> shared(context->closure()->shared());
14148 StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition);
14149 int entry = FindEntry(&key);
14150 if (entry == kNotFound) return isolate->factory()->undefined_value();
14151 int index = EntryToIndex(entry);
14152 if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value();
14153 return Handle<Object>(get(index + 1), isolate);
14157 Handle<Object> CompilationCacheTable::LookupEval(
14158 Handle<String> src, Handle<SharedFunctionInfo> outer_info,
14159 LanguageMode language_mode, int scope_position) {
14160 Isolate* isolate = GetIsolate();
14161 // Cache key is the tuple (source, outer shared function info, scope position)
14162 // to unambiguously identify the context chain the cached eval code assumes.
14163 StringSharedKey key(src, outer_info, language_mode, scope_position);
14164 int entry = FindEntry(&key);
14165 if (entry == kNotFound) return isolate->factory()->undefined_value();
14166 int index = EntryToIndex(entry);
14167 if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value();
14168 return Handle<Object>(get(EntryToIndex(entry) + 1), isolate);
14172 Handle<Object> CompilationCacheTable::LookupRegExp(Handle<String> src,
14173 JSRegExp::Flags flags) {
14174 Isolate* isolate = GetIsolate();
14175 DisallowHeapAllocation no_allocation;
14176 RegExpKey key(src, flags);
14177 int entry = FindEntry(&key);
14178 if (entry == kNotFound) return isolate->factory()->undefined_value();
14179 return Handle<Object>(get(EntryToIndex(entry) + 1), isolate);
14183 Handle<CompilationCacheTable> CompilationCacheTable::Put(
14184 Handle<CompilationCacheTable> cache, Handle<String> src,
14185 Handle<Context> context, LanguageMode language_mode, Handle<Object> value) {
14186 Isolate* isolate = cache->GetIsolate();
14187 Handle<SharedFunctionInfo> shared(context->closure()->shared());
14188 StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition);
14190 Handle<Object> k = key.AsHandle(isolate);
14191 DisallowHeapAllocation no_allocation_scope;
14192 int entry = cache->FindEntry(&key);
14193 if (entry != kNotFound) {
14194 cache->set(EntryToIndex(entry), *k);
14195 cache->set(EntryToIndex(entry) + 1, *value);
14200 cache = EnsureCapacity(cache, 1, &key);
14201 int entry = cache->FindInsertionEntry(key.Hash());
14203 isolate->factory()->NewNumber(static_cast<double>(key.Hash()));
14204 cache->set(EntryToIndex(entry), *k);
14205 cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations));
14206 cache->ElementAdded();
14211 Handle<CompilationCacheTable> CompilationCacheTable::PutEval(
14212 Handle<CompilationCacheTable> cache, Handle<String> src,
14213 Handle<SharedFunctionInfo> outer_info, Handle<SharedFunctionInfo> value,
14214 int scope_position) {
14215 Isolate* isolate = cache->GetIsolate();
14216 StringSharedKey key(src, outer_info, value->language_mode(), scope_position);
14218 Handle<Object> k = key.AsHandle(isolate);
14219 DisallowHeapAllocation no_allocation_scope;
14220 int entry = cache->FindEntry(&key);
14221 if (entry != kNotFound) {
14222 cache->set(EntryToIndex(entry), *k);
14223 cache->set(EntryToIndex(entry) + 1, *value);
14228 cache = EnsureCapacity(cache, 1, &key);
14229 int entry = cache->FindInsertionEntry(key.Hash());
14231 isolate->factory()->NewNumber(static_cast<double>(key.Hash()));
14232 cache->set(EntryToIndex(entry), *k);
14233 cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations));
14234 cache->ElementAdded();
14239 Handle<CompilationCacheTable> CompilationCacheTable::PutRegExp(
14240 Handle<CompilationCacheTable> cache, Handle<String> src,
14241 JSRegExp::Flags flags, Handle<FixedArray> value) {
14242 RegExpKey key(src, flags);
14243 cache = EnsureCapacity(cache, 1, &key);
14244 int entry = cache->FindInsertionEntry(key.Hash());
14245 // We store the value in the key slot, and compare the search key
14246 // to the stored value with a custon IsMatch function during lookups.
14247 cache->set(EntryToIndex(entry), *value);
14248 cache->set(EntryToIndex(entry) + 1, *value);
14249 cache->ElementAdded();
14254 void CompilationCacheTable::Age() {
14255 DisallowHeapAllocation no_allocation;
14256 Object* the_hole_value = GetHeap()->the_hole_value();
14257 for (int entry = 0, size = Capacity(); entry < size; entry++) {
14258 int entry_index = EntryToIndex(entry);
14259 int value_index = entry_index + 1;
14261 if (get(entry_index)->IsNumber()) {
14262 Smi* count = Smi::cast(get(value_index));
14263 count = Smi::FromInt(count->value() - 1);
14264 if (count->value() == 0) {
14265 NoWriteBarrierSet(this, entry_index, the_hole_value);
14266 NoWriteBarrierSet(this, value_index, the_hole_value);
14269 NoWriteBarrierSet(this, value_index, count);
14271 } else if (get(entry_index)->IsFixedArray()) {
14272 SharedFunctionInfo* info = SharedFunctionInfo::cast(get(value_index));
14273 if (info->code()->kind() != Code::FUNCTION || info->code()->IsOld()) {
14274 NoWriteBarrierSet(this, entry_index, the_hole_value);
14275 NoWriteBarrierSet(this, value_index, the_hole_value);
14283 void CompilationCacheTable::Remove(Object* value) {
14284 DisallowHeapAllocation no_allocation;
14285 Object* the_hole_value = GetHeap()->the_hole_value();
14286 for (int entry = 0, size = Capacity(); entry < size; entry++) {
14287 int entry_index = EntryToIndex(entry);
14288 int value_index = entry_index + 1;
14289 if (get(value_index) == value) {
14290 NoWriteBarrierSet(this, entry_index, the_hole_value);
14291 NoWriteBarrierSet(this, value_index, the_hole_value);
14299 // StringsKey used for HashTable where key is array of internalized strings.
14300 class StringsKey : public HashTableKey {
14302 explicit StringsKey(Handle<FixedArray> strings) : strings_(strings) { }
14304 bool IsMatch(Object* strings) override {
14305 FixedArray* o = FixedArray::cast(strings);
14306 int len = strings_->length();
14307 if (o->length() != len) return false;
14308 for (int i = 0; i < len; i++) {
14309 if (o->get(i) != strings_->get(i)) return false;
14314 uint32_t Hash() override { return HashForObject(*strings_); }
14316 uint32_t HashForObject(Object* obj) override {
14317 FixedArray* strings = FixedArray::cast(obj);
14318 int len = strings->length();
14320 for (int i = 0; i < len; i++) {
14321 hash ^= String::cast(strings->get(i))->Hash();
14326 Handle<Object> AsHandle(Isolate* isolate) override { return strings_; }
14329 Handle<FixedArray> strings_;
14333 template<typename Derived, typename Shape, typename Key>
14334 Handle<Derived> Dictionary<Derived, Shape, Key>::New(
14336 int at_least_space_for,
14337 PretenureFlag pretenure) {
14338 DCHECK(0 <= at_least_space_for);
14339 Handle<Derived> dict = DerivedHashTable::New(isolate,
14340 at_least_space_for,
14341 USE_DEFAULT_MINIMUM_CAPACITY,
14344 // Initialize the next enumeration index.
14345 dict->SetNextEnumerationIndex(PropertyDetails::kInitialIndex);
14350 template <typename Derived, typename Shape, typename Key>
14351 Handle<FixedArray> Dictionary<Derived, Shape, Key>::BuildIterationIndicesArray(
14352 Handle<Derived> dictionary) {
14353 Factory* factory = dictionary->GetIsolate()->factory();
14354 int length = dictionary->NumberOfElements();
14356 Handle<FixedArray> iteration_order = factory->NewFixedArray(length);
14357 Handle<FixedArray> enumeration_order = factory->NewFixedArray(length);
14359 // Fill both the iteration order array and the enumeration order array
14360 // with property details.
14361 int capacity = dictionary->Capacity();
14363 for (int i = 0; i < capacity; i++) {
14364 if (dictionary->IsKey(dictionary->KeyAt(i))) {
14365 int index = dictionary->DetailsAt(i).dictionary_index();
14366 iteration_order->set(pos, Smi::FromInt(i));
14367 enumeration_order->set(pos, Smi::FromInt(index));
14371 DCHECK(pos == length);
14373 // Sort the arrays wrt. enumeration order.
14374 iteration_order->SortPairs(*enumeration_order, enumeration_order->length());
14375 return iteration_order;
14379 template <typename Derived, typename Shape, typename Key>
14381 Dictionary<Derived, Shape, Key>::GenerateNewEnumerationIndices(
14382 Handle<Derived> dictionary) {
14383 int length = dictionary->NumberOfElements();
14385 Handle<FixedArray> iteration_order = BuildIterationIndicesArray(dictionary);
14386 DCHECK(iteration_order->length() == length);
14388 // Iterate over the dictionary using the enumeration order and update
14389 // the dictionary with new enumeration indices.
14390 for (int i = 0; i < length; i++) {
14391 int index = Smi::cast(iteration_order->get(i))->value();
14392 DCHECK(dictionary->IsKey(dictionary->KeyAt(index)));
14394 int enum_index = PropertyDetails::kInitialIndex + i;
14396 PropertyDetails details = dictionary->DetailsAt(index);
14397 PropertyDetails new_details = details.set_index(enum_index);
14398 dictionary->DetailsAtPut(index, new_details);
14401 // Set the next enumeration index.
14402 dictionary->SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length);
14403 return iteration_order;
14407 template<typename Derived, typename Shape, typename Key>
14408 Handle<Derived> Dictionary<Derived, Shape, Key>::EnsureCapacity(
14409 Handle<Derived> dictionary, int n, Key key) {
14410 // Check whether there are enough enumeration indices to add n elements.
14411 if (Shape::kIsEnumerable &&
14412 !PropertyDetails::IsValidIndex(dictionary->NextEnumerationIndex() + n)) {
14413 // If not, we generate new indices for the properties.
14414 GenerateNewEnumerationIndices(dictionary);
14416 return DerivedHashTable::EnsureCapacity(dictionary, n, key);
14420 template <typename Derived, typename Shape, typename Key>
14421 Handle<Object> Dictionary<Derived, Shape, Key>::DeleteProperty(
14422 Handle<Derived> dictionary, int entry) {
14423 Factory* factory = dictionary->GetIsolate()->factory();
14424 PropertyDetails details = dictionary->DetailsAt(entry);
14425 if (!details.IsConfigurable()) return factory->false_value();
14427 dictionary->SetEntry(
14428 entry, factory->the_hole_value(), factory->the_hole_value());
14429 dictionary->ElementRemoved();
14430 return factory->true_value();
14434 template<typename Derived, typename Shape, typename Key>
14435 Handle<Derived> Dictionary<Derived, Shape, Key>::AtPut(
14436 Handle<Derived> dictionary, Key key, Handle<Object> value) {
14437 int entry = dictionary->FindEntry(key);
14439 // If the entry is present set the value;
14440 if (entry != Dictionary::kNotFound) {
14441 dictionary->ValueAtPut(entry, *value);
14445 // Check whether the dictionary should be extended.
14446 dictionary = EnsureCapacity(dictionary, 1, key);
14448 USE(Shape::AsHandle(dictionary->GetIsolate(), key));
14450 PropertyDetails details = PropertyDetails::Empty();
14452 AddEntry(dictionary, key, value, details, dictionary->Hash(key));
14457 template<typename Derived, typename Shape, typename Key>
14458 Handle<Derived> Dictionary<Derived, Shape, Key>::Add(
14459 Handle<Derived> dictionary,
14461 Handle<Object> value,
14462 PropertyDetails details) {
14463 // Valdate key is absent.
14464 SLOW_DCHECK((dictionary->FindEntry(key) == Dictionary::kNotFound));
14465 // Check whether the dictionary should be extended.
14466 dictionary = EnsureCapacity(dictionary, 1, key);
14468 AddEntry(dictionary, key, value, details, dictionary->Hash(key));
14473 // Add a key, value pair to the dictionary.
14474 template<typename Derived, typename Shape, typename Key>
14475 void Dictionary<Derived, Shape, Key>::AddEntry(
14476 Handle<Derived> dictionary,
14478 Handle<Object> value,
14479 PropertyDetails details,
14481 // Compute the key object.
14482 Handle<Object> k = Shape::AsHandle(dictionary->GetIsolate(), key);
14484 uint32_t entry = dictionary->FindInsertionEntry(hash);
14485 // Insert element at empty or deleted entry
14486 if (details.dictionary_index() == 0 && Shape::kIsEnumerable) {
14487 // Assign an enumeration index to the property and update
14488 // SetNextEnumerationIndex.
14489 int index = dictionary->NextEnumerationIndex();
14490 details = details.set_index(index);
14491 dictionary->SetNextEnumerationIndex(index + 1);
14493 dictionary->SetEntry(entry, k, value, details);
14494 DCHECK((dictionary->KeyAt(entry)->IsNumber() ||
14495 dictionary->KeyAt(entry)->IsName()));
14496 dictionary->ElementAdded();
14500 void SeededNumberDictionary::UpdateMaxNumberKey(uint32_t key,
14501 bool used_as_prototype) {
14502 DisallowHeapAllocation no_allocation;
14503 // If the dictionary requires slow elements an element has already
14504 // been added at a high index.
14505 if (requires_slow_elements()) return;
14506 // Check if this index is high enough that we should require slow
14508 if (key > kRequiresSlowElementsLimit) {
14509 if (used_as_prototype) {
14510 // TODO(verwaest): Remove this hack.
14511 GetHeap()->ClearAllICsByKind(Code::KEYED_STORE_IC);
14513 set_requires_slow_elements();
14516 // Update max key value.
14517 Object* max_index_object = get(kMaxNumberKeyIndex);
14518 if (!max_index_object->IsSmi() || max_number_key() < key) {
14519 FixedArray::set(kMaxNumberKeyIndex,
14520 Smi::FromInt(key << kRequiresSlowElementsTagSize));
14525 Handle<SeededNumberDictionary> SeededNumberDictionary::AddNumberEntry(
14526 Handle<SeededNumberDictionary> dictionary, uint32_t key,
14527 Handle<Object> value, PropertyDetails details, bool used_as_prototype) {
14528 dictionary->UpdateMaxNumberKey(key, used_as_prototype);
14529 SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound);
14530 return Add(dictionary, key, value, details);
14534 Handle<UnseededNumberDictionary> UnseededNumberDictionary::AddNumberEntry(
14535 Handle<UnseededNumberDictionary> dictionary,
14537 Handle<Object> value) {
14538 SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound);
14539 return Add(dictionary, key, value, PropertyDetails::Empty());
14543 Handle<SeededNumberDictionary> SeededNumberDictionary::AtNumberPut(
14544 Handle<SeededNumberDictionary> dictionary, uint32_t key,
14545 Handle<Object> value, bool used_as_prototype) {
14546 dictionary->UpdateMaxNumberKey(key, used_as_prototype);
14547 return AtPut(dictionary, key, value);
14551 Handle<UnseededNumberDictionary> UnseededNumberDictionary::AtNumberPut(
14552 Handle<UnseededNumberDictionary> dictionary,
14554 Handle<Object> value) {
14555 return AtPut(dictionary, key, value);
14559 Handle<SeededNumberDictionary> SeededNumberDictionary::Set(
14560 Handle<SeededNumberDictionary> dictionary, uint32_t key,
14561 Handle<Object> value, PropertyDetails details, bool used_as_prototype) {
14562 int entry = dictionary->FindEntry(key);
14563 if (entry == kNotFound) {
14564 return AddNumberEntry(dictionary, key, value, details, used_as_prototype);
14566 // Preserve enumeration index.
14567 details = details.set_index(dictionary->DetailsAt(entry).dictionary_index());
14568 Handle<Object> object_key =
14569 SeededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key);
14570 dictionary->SetEntry(entry, object_key, value, details);
14575 Handle<UnseededNumberDictionary> UnseededNumberDictionary::Set(
14576 Handle<UnseededNumberDictionary> dictionary,
14578 Handle<Object> value) {
14579 int entry = dictionary->FindEntry(key);
14580 if (entry == kNotFound) return AddNumberEntry(dictionary, key, value);
14581 Handle<Object> object_key =
14582 UnseededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key);
14583 dictionary->SetEntry(entry, object_key, value);
14588 template <typename Derived, typename Shape, typename Key>
14589 int Dictionary<Derived, Shape, Key>::NumberOfElementsFilterAttributes(
14590 PropertyAttributes filter) {
14591 int capacity = this->Capacity();
14593 for (int i = 0; i < capacity; i++) {
14594 Object* k = this->KeyAt(i);
14595 if (this->IsKey(k) && !FilterKey(k, filter)) {
14596 if (this->IsDeleted(i)) continue;
14597 PropertyDetails details = this->DetailsAt(i);
14598 PropertyAttributes attr = details.attributes();
14599 if ((attr & filter) == 0) result++;
14606 template <typename Derived, typename Shape, typename Key>
14607 bool Dictionary<Derived, Shape, Key>::HasComplexElements() {
14608 int capacity = this->Capacity();
14609 for (int i = 0; i < capacity; i++) {
14610 Object* k = this->KeyAt(i);
14611 if (this->IsKey(k) && !FilterKey(k, NONE)) {
14612 if (this->IsDeleted(i)) continue;
14613 PropertyDetails details = this->DetailsAt(i);
14614 if (details.type() == ACCESSOR_CONSTANT) return true;
14615 PropertyAttributes attr = details.attributes();
14616 if (attr & (READ_ONLY | DONT_DELETE | DONT_ENUM)) return true;
14623 template <typename Dictionary>
14624 struct EnumIndexComparator {
14625 explicit EnumIndexComparator(Dictionary* dict) : dict(dict) {}
14626 bool operator() (Smi* a, Smi* b) {
14627 PropertyDetails da(dict->DetailsAt(a->value()));
14628 PropertyDetails db(dict->DetailsAt(b->value()));
14629 return da.dictionary_index() < db.dictionary_index();
14635 template <typename Derived, typename Shape, typename Key>
14636 void Dictionary<Derived, Shape, Key>::CopyEnumKeysTo(FixedArray* storage) {
14637 int length = storage->length();
14638 int capacity = this->Capacity();
14639 int properties = 0;
14640 for (int i = 0; i < capacity; i++) {
14641 Object* k = this->KeyAt(i);
14642 if (this->IsKey(k) && !k->IsSymbol()) {
14643 PropertyDetails details = this->DetailsAt(i);
14644 if (details.IsDontEnum() || this->IsDeleted(i)) continue;
14645 storage->set(properties, Smi::FromInt(i));
14647 if (properties == length) break;
14650 CHECK_EQ(length, properties);
14651 EnumIndexComparator<Derived> cmp(static_cast<Derived*>(this));
14652 Smi** start = reinterpret_cast<Smi**>(storage->GetFirstElementAddress());
14653 std::sort(start, start + length, cmp);
14654 for (int i = 0; i < length; i++) {
14655 int index = Smi::cast(storage->get(i))->value();
14656 storage->set(i, this->KeyAt(index));
14661 template <typename Derived, typename Shape, typename Key>
14662 int Dictionary<Derived, Shape, Key>::CopyKeysTo(
14663 FixedArray* storage, int index, PropertyAttributes filter,
14664 typename Dictionary<Derived, Shape, Key>::SortMode sort_mode) {
14665 DCHECK(storage->length() >= NumberOfElementsFilterAttributes(filter));
14666 int start_index = index;
14667 int capacity = this->Capacity();
14668 for (int i = 0; i < capacity; i++) {
14669 Object* k = this->KeyAt(i);
14670 if (this->IsKey(k) && !FilterKey(k, filter)) {
14671 if (this->IsDeleted(i)) continue;
14672 PropertyDetails details = this->DetailsAt(i);
14673 PropertyAttributes attr = details.attributes();
14674 if ((attr & filter) == 0) storage->set(index++, k);
14677 if (sort_mode == Dictionary::SORTED) {
14678 storage->SortPairs(storage, index);
14680 DCHECK(storage->length() >= index);
14681 return index - start_index;
14685 // Backwards lookup (slow).
14686 template<typename Derived, typename Shape, typename Key>
14687 Object* Dictionary<Derived, Shape, Key>::SlowReverseLookup(Object* value) {
14688 int capacity = this->Capacity();
14689 for (int i = 0; i < capacity; i++) {
14690 Object* k = this->KeyAt(i);
14691 if (this->IsKey(k)) {
14692 Object* e = this->ValueAt(i);
14693 // TODO(dcarney): this should be templatized.
14694 if (e->IsPropertyCell()) {
14695 e = PropertyCell::cast(e)->value();
14697 if (e == value) return k;
14700 Heap* heap = Dictionary::GetHeap();
14701 return heap->undefined_value();
14705 Object* ObjectHashTable::Lookup(Isolate* isolate, Handle<Object> key,
14707 DisallowHeapAllocation no_gc;
14708 DCHECK(IsKey(*key));
14710 int entry = FindEntry(isolate, key, hash);
14711 if (entry == kNotFound) return isolate->heap()->the_hole_value();
14712 return get(EntryToIndex(entry) + 1);
14716 Object* ObjectHashTable::Lookup(Handle<Object> key) {
14717 DisallowHeapAllocation no_gc;
14718 DCHECK(IsKey(*key));
14720 Isolate* isolate = GetIsolate();
14722 // If the object does not have an identity hash, it was never used as a key.
14723 Object* hash = key->GetHash();
14724 if (hash->IsUndefined()) {
14725 return isolate->heap()->the_hole_value();
14727 return Lookup(isolate, key, Smi::cast(hash)->value());
14731 Object* ObjectHashTable::Lookup(Handle<Object> key, int32_t hash) {
14732 return Lookup(GetIsolate(), key, hash);
14736 Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
14737 Handle<Object> key,
14738 Handle<Object> value) {
14739 DCHECK(table->IsKey(*key));
14740 DCHECK(!value->IsTheHole());
14742 Isolate* isolate = table->GetIsolate();
14743 // Make sure the key object has an identity hash code.
14744 int32_t hash = Object::GetOrCreateHash(isolate, key)->value();
14746 return Put(table, key, value, hash);
14750 Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
14751 Handle<Object> key,
14752 Handle<Object> value,
14754 DCHECK(table->IsKey(*key));
14755 DCHECK(!value->IsTheHole());
14757 Isolate* isolate = table->GetIsolate();
14759 int entry = table->FindEntry(isolate, key, hash);
14761 // Key is already in table, just overwrite value.
14762 if (entry != kNotFound) {
14763 table->set(EntryToIndex(entry) + 1, *value);
14767 // Check whether the hash table should be extended.
14768 table = EnsureCapacity(table, 1, key);
14769 table->AddEntry(table->FindInsertionEntry(hash), *key, *value);
14774 Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table,
14775 Handle<Object> key,
14776 bool* was_present) {
14777 DCHECK(table->IsKey(*key));
14779 Object* hash = key->GetHash();
14780 if (hash->IsUndefined()) {
14781 *was_present = false;
14785 return Remove(table, key, was_present, Smi::cast(hash)->value());
14789 Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table,
14790 Handle<Object> key,
14793 DCHECK(table->IsKey(*key));
14795 int entry = table->FindEntry(table->GetIsolate(), key, hash);
14796 if (entry == kNotFound) {
14797 *was_present = false;
14801 *was_present = true;
14802 table->RemoveEntry(entry);
14803 return Shrink(table, key);
14807 void ObjectHashTable::AddEntry(int entry, Object* key, Object* value) {
14808 set(EntryToIndex(entry), key);
14809 set(EntryToIndex(entry) + 1, value);
14814 void ObjectHashTable::RemoveEntry(int entry) {
14815 set_the_hole(EntryToIndex(entry));
14816 set_the_hole(EntryToIndex(entry) + 1);
14821 Object* WeakHashTable::Lookup(Handle<HeapObject> key) {
14822 DisallowHeapAllocation no_gc;
14823 DCHECK(IsKey(*key));
14824 int entry = FindEntry(key);
14825 if (entry == kNotFound) return GetHeap()->the_hole_value();
14826 return get(EntryToValueIndex(entry));
14830 Handle<WeakHashTable> WeakHashTable::Put(Handle<WeakHashTable> table,
14831 Handle<HeapObject> key,
14832 Handle<HeapObject> value) {
14833 DCHECK(table->IsKey(*key));
14834 int entry = table->FindEntry(key);
14835 // Key is already in table, just overwrite value.
14836 if (entry != kNotFound) {
14837 table->set(EntryToValueIndex(entry), *value);
14841 Handle<WeakCell> key_cell = key->GetIsolate()->factory()->NewWeakCell(key);
14843 // Check whether the hash table should be extended.
14844 table = EnsureCapacity(table, 1, key, TENURED);
14846 table->AddEntry(table->FindInsertionEntry(table->Hash(key)), key_cell, value);
14851 void WeakHashTable::AddEntry(int entry, Handle<WeakCell> key_cell,
14852 Handle<HeapObject> value) {
14853 DisallowHeapAllocation no_allocation;
14854 set(EntryToIndex(entry), *key_cell);
14855 set(EntryToValueIndex(entry), *value);
14861 Object* WeakValueHashTable::LookupWeak(Handle<Object> key) {
14862 Object* value = Lookup(key);
14863 if (value->IsWeakCell() && !WeakCell::cast(value)->cleared()) {
14864 value = WeakCell::cast(value)->value();
14871 Handle<WeakValueHashTable> WeakValueHashTable::PutWeak(
14872 Handle<WeakValueHashTable> table, Handle<Object> key,
14873 Handle<HeapObject> value) {
14874 Handle<WeakCell> cell = value->GetIsolate()->factory()->NewWeakCell(value);
14875 return Handle<WeakValueHashTable>::cast(
14876 Put(Handle<ObjectHashTable>::cast(table), key, cell));
14880 Handle<FixedArray> WeakValueHashTable::GetWeakValues(
14881 Handle<WeakValueHashTable> table) {
14882 Isolate* isolate = table->GetIsolate();
14883 uint32_t capacity = table->Capacity();
14884 Handle<FixedArray> results = isolate->factory()->NewFixedArray(capacity);
14886 for (uint32_t i = 0; i < capacity; i++) {
14887 uint32_t key_index = table->EntryToIndex(i);
14888 Object* key = table->get(key_index);
14889 if (!table->IsKey(key)) continue;
14890 uint32_t value_index = table->EntryToValueIndex(i);
14891 WeakCell* value_cell = WeakCell::cast(table->get(value_index));
14892 if (value_cell->cleared()) {
14893 table->RemoveEntry(i);
14895 results->set(length++, value_cell->value());
14898 results->Shrink(length);
14903 template<class Derived, class Iterator, int entrysize>
14904 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Allocate(
14905 Isolate* isolate, int capacity, PretenureFlag pretenure) {
14906 // Capacity must be a power of two, since we depend on being able
14907 // to divide and multiple by 2 (kLoadFactor) to derive capacity
14908 // from number of buckets. If we decide to change kLoadFactor
14909 // to something other than 2, capacity should be stored as another
14910 // field of this object.
14911 capacity = base::bits::RoundUpToPowerOfTwo32(Max(kMinCapacity, capacity));
14912 if (capacity > kMaxCapacity) {
14913 v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true);
14915 int num_buckets = capacity / kLoadFactor;
14916 Handle<FixedArray> backing_store = isolate->factory()->NewFixedArray(
14917 kHashTableStartIndex + num_buckets + (capacity * kEntrySize), pretenure);
14918 backing_store->set_map_no_write_barrier(
14919 isolate->heap()->ordered_hash_table_map());
14920 Handle<Derived> table = Handle<Derived>::cast(backing_store);
14921 for (int i = 0; i < num_buckets; ++i) {
14922 table->set(kHashTableStartIndex + i, Smi::FromInt(kNotFound));
14924 table->SetNumberOfBuckets(num_buckets);
14925 table->SetNumberOfElements(0);
14926 table->SetNumberOfDeletedElements(0);
14931 template<class Derived, class Iterator, int entrysize>
14932 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::EnsureGrowable(
14933 Handle<Derived> table) {
14934 DCHECK(!table->IsObsolete());
14936 int nof = table->NumberOfElements();
14937 int nod = table->NumberOfDeletedElements();
14938 int capacity = table->Capacity();
14939 if ((nof + nod) < capacity) return table;
14940 // Don't need to grow if we can simply clear out deleted entries instead.
14941 // Note that we can't compact in place, though, so we always allocate
14943 return Rehash(table, (nod < (capacity >> 1)) ? capacity << 1 : capacity);
14947 template<class Derived, class Iterator, int entrysize>
14948 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Shrink(
14949 Handle<Derived> table) {
14950 DCHECK(!table->IsObsolete());
14952 int nof = table->NumberOfElements();
14953 int capacity = table->Capacity();
14954 if (nof >= (capacity >> 2)) return table;
14955 return Rehash(table, capacity / 2);
14959 template<class Derived, class Iterator, int entrysize>
14960 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Clear(
14961 Handle<Derived> table) {
14962 DCHECK(!table->IsObsolete());
14964 Handle<Derived> new_table =
14965 Allocate(table->GetIsolate(),
14967 table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED);
14969 table->SetNextTable(*new_table);
14970 table->SetNumberOfDeletedElements(kClearedTableSentinel);
14976 template<class Derived, class Iterator, int entrysize>
14977 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Rehash(
14978 Handle<Derived> table, int new_capacity) {
14979 DCHECK(!table->IsObsolete());
14981 Handle<Derived> new_table =
14982 Allocate(table->GetIsolate(),
14984 table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED);
14985 int nof = table->NumberOfElements();
14986 int nod = table->NumberOfDeletedElements();
14987 int new_buckets = new_table->NumberOfBuckets();
14989 int removed_holes_index = 0;
14991 for (int old_entry = 0; old_entry < (nof + nod); ++old_entry) {
14992 Object* key = table->KeyAt(old_entry);
14993 if (key->IsTheHole()) {
14994 table->SetRemovedIndexAt(removed_holes_index++, old_entry);
14998 Object* hash = key->GetHash();
14999 int bucket = Smi::cast(hash)->value() & (new_buckets - 1);
15000 Object* chain_entry = new_table->get(kHashTableStartIndex + bucket);
15001 new_table->set(kHashTableStartIndex + bucket, Smi::FromInt(new_entry));
15002 int new_index = new_table->EntryToIndex(new_entry);
15003 int old_index = table->EntryToIndex(old_entry);
15004 for (int i = 0; i < entrysize; ++i) {
15005 Object* value = table->get(old_index + i);
15006 new_table->set(new_index + i, value);
15008 new_table->set(new_index + kChainOffset, chain_entry);
15012 DCHECK_EQ(nod, removed_holes_index);
15014 new_table->SetNumberOfElements(nof);
15015 table->SetNextTable(*new_table);
15021 template Handle<OrderedHashSet>
15022 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Allocate(
15023 Isolate* isolate, int capacity, PretenureFlag pretenure);
15025 template Handle<OrderedHashSet>
15026 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::EnsureGrowable(
15027 Handle<OrderedHashSet> table);
15029 template Handle<OrderedHashSet>
15030 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Shrink(
15031 Handle<OrderedHashSet> table);
15033 template Handle<OrderedHashSet>
15034 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Clear(
15035 Handle<OrderedHashSet> table);
15038 template Handle<OrderedHashMap>
15039 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Allocate(
15040 Isolate* isolate, int capacity, PretenureFlag pretenure);
15042 template Handle<OrderedHashMap>
15043 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::EnsureGrowable(
15044 Handle<OrderedHashMap> table);
15046 template Handle<OrderedHashMap>
15047 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Shrink(
15048 Handle<OrderedHashMap> table);
15050 template Handle<OrderedHashMap>
15051 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Clear(
15052 Handle<OrderedHashMap> table);
15055 template<class Derived, class TableType>
15056 void OrderedHashTableIterator<Derived, TableType>::Transition() {
15057 DisallowHeapAllocation no_allocation;
15058 TableType* table = TableType::cast(this->table());
15059 if (!table->IsObsolete()) return;
15061 int index = Smi::cast(this->index())->value();
15062 while (table->IsObsolete()) {
15063 TableType* next_table = table->NextTable();
15066 int nod = table->NumberOfDeletedElements();
15068 if (nod == TableType::kClearedTableSentinel) {
15071 int old_index = index;
15072 for (int i = 0; i < nod; ++i) {
15073 int removed_index = table->RemovedIndexAt(i);
15074 if (removed_index >= old_index) break;
15080 table = next_table;
15084 set_index(Smi::FromInt(index));
15088 template<class Derived, class TableType>
15089 bool OrderedHashTableIterator<Derived, TableType>::HasMore() {
15090 DisallowHeapAllocation no_allocation;
15091 if (this->table()->IsUndefined()) return false;
15095 TableType* table = TableType::cast(this->table());
15096 int index = Smi::cast(this->index())->value();
15097 int used_capacity = table->UsedCapacity();
15099 while (index < used_capacity && table->KeyAt(index)->IsTheHole()) {
15103 set_index(Smi::FromInt(index));
15105 if (index < used_capacity) return true;
15107 set_table(GetHeap()->undefined_value());
15112 template<class Derived, class TableType>
15113 Smi* OrderedHashTableIterator<Derived, TableType>::Next(JSArray* value_array) {
15114 DisallowHeapAllocation no_allocation;
15116 FixedArray* array = FixedArray::cast(value_array->elements());
15117 static_cast<Derived*>(this)->PopulateValueArray(array);
15119 return Smi::cast(kind());
15121 return Smi::FromInt(0);
15126 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Next(
15127 JSArray* value_array);
15130 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::HasMore();
15133 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::MoveNext();
15136 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::CurrentKey();
15139 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Transition();
15143 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Next(
15144 JSArray* value_array);
15147 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::HasMore();
15150 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::MoveNext();
15153 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::CurrentKey();
15156 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Transition();
15159 // Check if there is a break point at this code position.
15160 bool DebugInfo::HasBreakPoint(int code_position) {
15161 // Get the break point info object for this code position.
15162 Object* break_point_info = GetBreakPointInfo(code_position);
15164 // If there is no break point info object or no break points in the break
15165 // point info object there is no break point at this code position.
15166 if (break_point_info->IsUndefined()) return false;
15167 return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0;
15171 // Get the break point info object for this code position.
15172 Object* DebugInfo::GetBreakPointInfo(int code_position) {
15173 // Find the index of the break point info object for this code position.
15174 int index = GetBreakPointInfoIndex(code_position);
15176 // Return the break point info object if any.
15177 if (index == kNoBreakPointInfo) return GetHeap()->undefined_value();
15178 return BreakPointInfo::cast(break_points()->get(index));
15182 // Clear a break point at the specified code position.
15183 void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info,
15185 Handle<Object> break_point_object) {
15186 Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position),
15187 debug_info->GetIsolate());
15188 if (break_point_info->IsUndefined()) return;
15189 BreakPointInfo::ClearBreakPoint(
15190 Handle<BreakPointInfo>::cast(break_point_info),
15191 break_point_object);
15195 void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info,
15197 int source_position,
15198 int statement_position,
15199 Handle<Object> break_point_object) {
15200 Isolate* isolate = debug_info->GetIsolate();
15201 Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position),
15203 if (!break_point_info->IsUndefined()) {
15204 BreakPointInfo::SetBreakPoint(
15205 Handle<BreakPointInfo>::cast(break_point_info),
15206 break_point_object);
15210 // Adding a new break point for a code position which did not have any
15211 // break points before. Try to find a free slot.
15212 int index = kNoBreakPointInfo;
15213 for (int i = 0; i < debug_info->break_points()->length(); i++) {
15214 if (debug_info->break_points()->get(i)->IsUndefined()) {
15219 if (index == kNoBreakPointInfo) {
15220 // No free slot - extend break point info array.
15221 Handle<FixedArray> old_break_points =
15222 Handle<FixedArray>(FixedArray::cast(debug_info->break_points()));
15223 Handle<FixedArray> new_break_points =
15224 isolate->factory()->NewFixedArray(
15225 old_break_points->length() +
15226 DebugInfo::kEstimatedNofBreakPointsInFunction);
15228 debug_info->set_break_points(*new_break_points);
15229 for (int i = 0; i < old_break_points->length(); i++) {
15230 new_break_points->set(i, old_break_points->get(i));
15232 index = old_break_points->length();
15234 DCHECK(index != kNoBreakPointInfo);
15236 // Allocate new BreakPointInfo object and set the break point.
15237 Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast(
15238 isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE));
15239 new_break_point_info->set_code_position(Smi::FromInt(code_position));
15240 new_break_point_info->set_source_position(Smi::FromInt(source_position));
15241 new_break_point_info->
15242 set_statement_position(Smi::FromInt(statement_position));
15243 new_break_point_info->set_break_point_objects(
15244 isolate->heap()->undefined_value());
15245 BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object);
15246 debug_info->break_points()->set(index, *new_break_point_info);
15250 // Get the break point objects for a code position.
15251 Handle<Object> DebugInfo::GetBreakPointObjects(int code_position) {
15252 Object* break_point_info = GetBreakPointInfo(code_position);
15253 if (break_point_info->IsUndefined()) {
15254 return GetIsolate()->factory()->undefined_value();
15256 return Handle<Object>(
15257 BreakPointInfo::cast(break_point_info)->break_point_objects(),
15262 // Get the total number of break points.
15263 int DebugInfo::GetBreakPointCount() {
15264 if (break_points()->IsUndefined()) return 0;
15266 for (int i = 0; i < break_points()->length(); i++) {
15267 if (!break_points()->get(i)->IsUndefined()) {
15268 BreakPointInfo* break_point_info =
15269 BreakPointInfo::cast(break_points()->get(i));
15270 count += break_point_info->GetBreakPointCount();
15277 Handle<Object> DebugInfo::FindBreakPointInfo(
15278 Handle<DebugInfo> debug_info, Handle<Object> break_point_object) {
15279 Isolate* isolate = debug_info->GetIsolate();
15280 if (!debug_info->break_points()->IsUndefined()) {
15281 for (int i = 0; i < debug_info->break_points()->length(); i++) {
15282 if (!debug_info->break_points()->get(i)->IsUndefined()) {
15283 Handle<BreakPointInfo> break_point_info = Handle<BreakPointInfo>(
15284 BreakPointInfo::cast(debug_info->break_points()->get(i)), isolate);
15285 if (BreakPointInfo::HasBreakPointObject(break_point_info,
15286 break_point_object)) {
15287 return break_point_info;
15292 return isolate->factory()->undefined_value();
15296 // Find the index of the break point info object for the specified code
15298 int DebugInfo::GetBreakPointInfoIndex(int code_position) {
15299 if (break_points()->IsUndefined()) return kNoBreakPointInfo;
15300 for (int i = 0; i < break_points()->length(); i++) {
15301 if (!break_points()->get(i)->IsUndefined()) {
15302 BreakPointInfo* break_point_info =
15303 BreakPointInfo::cast(break_points()->get(i));
15304 if (break_point_info->code_position()->value() == code_position) {
15309 return kNoBreakPointInfo;
15313 // Remove the specified break point object.
15314 void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info,
15315 Handle<Object> break_point_object) {
15316 Isolate* isolate = break_point_info->GetIsolate();
15317 // If there are no break points just ignore.
15318 if (break_point_info->break_point_objects()->IsUndefined()) return;
15319 // If there is a single break point clear it if it is the same.
15320 if (!break_point_info->break_point_objects()->IsFixedArray()) {
15321 if (break_point_info->break_point_objects() == *break_point_object) {
15322 break_point_info->set_break_point_objects(
15323 isolate->heap()->undefined_value());
15327 // If there are multiple break points shrink the array
15328 DCHECK(break_point_info->break_point_objects()->IsFixedArray());
15329 Handle<FixedArray> old_array =
15330 Handle<FixedArray>(
15331 FixedArray::cast(break_point_info->break_point_objects()));
15332 Handle<FixedArray> new_array =
15333 isolate->factory()->NewFixedArray(old_array->length() - 1);
15334 int found_count = 0;
15335 for (int i = 0; i < old_array->length(); i++) {
15336 if (old_array->get(i) == *break_point_object) {
15337 DCHECK(found_count == 0);
15340 new_array->set(i - found_count, old_array->get(i));
15343 // If the break point was found in the list change it.
15344 if (found_count > 0) break_point_info->set_break_point_objects(*new_array);
15348 // Add the specified break point object.
15349 void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info,
15350 Handle<Object> break_point_object) {
15351 Isolate* isolate = break_point_info->GetIsolate();
15353 // If there was no break point objects before just set it.
15354 if (break_point_info->break_point_objects()->IsUndefined()) {
15355 break_point_info->set_break_point_objects(*break_point_object);
15358 // If the break point object is the same as before just ignore.
15359 if (break_point_info->break_point_objects() == *break_point_object) return;
15360 // If there was one break point object before replace with array.
15361 if (!break_point_info->break_point_objects()->IsFixedArray()) {
15362 Handle<FixedArray> array = isolate->factory()->NewFixedArray(2);
15363 array->set(0, break_point_info->break_point_objects());
15364 array->set(1, *break_point_object);
15365 break_point_info->set_break_point_objects(*array);
15368 // If there was more than one break point before extend array.
15369 Handle<FixedArray> old_array =
15370 Handle<FixedArray>(
15371 FixedArray::cast(break_point_info->break_point_objects()));
15372 Handle<FixedArray> new_array =
15373 isolate->factory()->NewFixedArray(old_array->length() + 1);
15374 for (int i = 0; i < old_array->length(); i++) {
15375 // If the break point was there before just ignore.
15376 if (old_array->get(i) == *break_point_object) return;
15377 new_array->set(i, old_array->get(i));
15379 // Add the new break point.
15380 new_array->set(old_array->length(), *break_point_object);
15381 break_point_info->set_break_point_objects(*new_array);
15385 bool BreakPointInfo::HasBreakPointObject(
15386 Handle<BreakPointInfo> break_point_info,
15387 Handle<Object> break_point_object) {
15389 if (break_point_info->break_point_objects()->IsUndefined()) return false;
15390 // Single break point.
15391 if (!break_point_info->break_point_objects()->IsFixedArray()) {
15392 return break_point_info->break_point_objects() == *break_point_object;
15394 // Multiple break points.
15395 FixedArray* array = FixedArray::cast(break_point_info->break_point_objects());
15396 for (int i = 0; i < array->length(); i++) {
15397 if (array->get(i) == *break_point_object) {
15405 // Get the number of break points.
15406 int BreakPointInfo::GetBreakPointCount() {
15408 if (break_point_objects()->IsUndefined()) return 0;
15409 // Single break point.
15410 if (!break_point_objects()->IsFixedArray()) return 1;
15411 // Multiple break points.
15412 return FixedArray::cast(break_point_objects())->length();
15416 Object* JSDate::GetField(Object* object, Smi* index) {
15417 return JSDate::cast(object)->DoGetField(
15418 static_cast<FieldIndex>(index->value()));
15422 Object* JSDate::DoGetField(FieldIndex index) {
15423 DCHECK(index != kDateValue);
15425 DateCache* date_cache = GetIsolate()->date_cache();
15427 if (index < kFirstUncachedField) {
15428 Object* stamp = cache_stamp();
15429 if (stamp != date_cache->stamp() && stamp->IsSmi()) {
15430 // Since the stamp is not NaN, the value is also not NaN.
15431 int64_t local_time_ms =
15432 date_cache->ToLocal(static_cast<int64_t>(value()->Number()));
15433 SetCachedFields(local_time_ms, date_cache);
15436 case kYear: return year();
15437 case kMonth: return month();
15438 case kDay: return day();
15439 case kWeekday: return weekday();
15440 case kHour: return hour();
15441 case kMinute: return min();
15442 case kSecond: return sec();
15443 default: UNREACHABLE();
15447 if (index >= kFirstUTCField) {
15448 return GetUTCField(index, value()->Number(), date_cache);
15451 double time = value()->Number();
15452 if (std::isnan(time)) return GetIsolate()->heap()->nan_value();
15454 int64_t local_time_ms = date_cache->ToLocal(static_cast<int64_t>(time));
15455 int days = DateCache::DaysFromTime(local_time_ms);
15457 if (index == kDays) return Smi::FromInt(days);
15459 int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days);
15460 if (index == kMillisecond) return Smi::FromInt(time_in_day_ms % 1000);
15461 DCHECK(index == kTimeInDay);
15462 return Smi::FromInt(time_in_day_ms);
15466 Object* JSDate::GetUTCField(FieldIndex index,
15468 DateCache* date_cache) {
15469 DCHECK(index >= kFirstUTCField);
15471 if (std::isnan(value)) return GetIsolate()->heap()->nan_value();
15473 int64_t time_ms = static_cast<int64_t>(value);
15475 if (index == kTimezoneOffset) {
15476 return Smi::FromInt(date_cache->TimezoneOffset(time_ms));
15479 int days = DateCache::DaysFromTime(time_ms);
15481 if (index == kWeekdayUTC) return Smi::FromInt(date_cache->Weekday(days));
15483 if (index <= kDayUTC) {
15484 int year, month, day;
15485 date_cache->YearMonthDayFromDays(days, &year, &month, &day);
15486 if (index == kYearUTC) return Smi::FromInt(year);
15487 if (index == kMonthUTC) return Smi::FromInt(month);
15488 DCHECK(index == kDayUTC);
15489 return Smi::FromInt(day);
15492 int time_in_day_ms = DateCache::TimeInDay(time_ms, days);
15494 case kHourUTC: return Smi::FromInt(time_in_day_ms / (60 * 60 * 1000));
15495 case kMinuteUTC: return Smi::FromInt((time_in_day_ms / (60 * 1000)) % 60);
15496 case kSecondUTC: return Smi::FromInt((time_in_day_ms / 1000) % 60);
15497 case kMillisecondUTC: return Smi::FromInt(time_in_day_ms % 1000);
15498 case kDaysUTC: return Smi::FromInt(days);
15499 case kTimeInDayUTC: return Smi::FromInt(time_in_day_ms);
15500 default: UNREACHABLE();
15508 void JSDate::SetValue(Object* value, bool is_value_nan) {
15510 if (is_value_nan) {
15511 HeapNumber* nan = GetIsolate()->heap()->nan_value();
15512 set_cache_stamp(nan, SKIP_WRITE_BARRIER);
15513 set_year(nan, SKIP_WRITE_BARRIER);
15514 set_month(nan, SKIP_WRITE_BARRIER);
15515 set_day(nan, SKIP_WRITE_BARRIER);
15516 set_hour(nan, SKIP_WRITE_BARRIER);
15517 set_min(nan, SKIP_WRITE_BARRIER);
15518 set_sec(nan, SKIP_WRITE_BARRIER);
15519 set_weekday(nan, SKIP_WRITE_BARRIER);
15521 set_cache_stamp(Smi::FromInt(DateCache::kInvalidStamp), SKIP_WRITE_BARRIER);
15526 void JSDate::SetCachedFields(int64_t local_time_ms, DateCache* date_cache) {
15527 int days = DateCache::DaysFromTime(local_time_ms);
15528 int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days);
15529 int year, month, day;
15530 date_cache->YearMonthDayFromDays(days, &year, &month, &day);
15531 int weekday = date_cache->Weekday(days);
15532 int hour = time_in_day_ms / (60 * 60 * 1000);
15533 int min = (time_in_day_ms / (60 * 1000)) % 60;
15534 int sec = (time_in_day_ms / 1000) % 60;
15535 set_cache_stamp(date_cache->stamp());
15536 set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER);
15537 set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER);
15538 set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER);
15539 set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER);
15540 set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER);
15541 set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER);
15542 set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER);
15546 void JSArrayBuffer::Neuter() {
15547 CHECK(is_neuterable());
15548 CHECK(is_external());
15549 set_backing_store(NULL);
15550 set_byte_length(Smi::FromInt(0));
15551 set_was_neutered(true);
15555 Handle<JSArrayBuffer> JSTypedArray::MaterializeArrayBuffer(
15556 Handle<JSTypedArray> typed_array) {
15558 Handle<Map> map(typed_array->map());
15559 Isolate* isolate = typed_array->GetIsolate();
15561 DCHECK(IsFixedTypedArrayElementsKind(map->elements_kind()));
15563 Handle<FixedTypedArrayBase> fixed_typed_array(
15564 FixedTypedArrayBase::cast(typed_array->elements()));
15566 Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(typed_array->buffer()),
15568 void* backing_store =
15569 isolate->array_buffer_allocator()->AllocateUninitialized(
15570 fixed_typed_array->DataSize());
15571 buffer->set_backing_store(backing_store);
15572 buffer->set_is_external(false);
15573 isolate->heap()->RegisterNewArrayBuffer(isolate->heap()->InNewSpace(*buffer),
15575 fixed_typed_array->DataSize());
15576 memcpy(buffer->backing_store(),
15577 fixed_typed_array->DataPtr(),
15578 fixed_typed_array->DataSize());
15579 Handle<FixedTypedArrayBase> new_elements =
15580 isolate->factory()->NewFixedTypedArrayWithExternalPointer(
15581 fixed_typed_array->length(), typed_array->type(),
15582 static_cast<uint8_t*>(buffer->backing_store()));
15584 typed_array->set_elements(*new_elements);
15590 Handle<JSArrayBuffer> JSTypedArray::GetBuffer() {
15591 Handle<JSArrayBuffer> array_buffer(JSArrayBuffer::cast(buffer()),
15593 if (array_buffer->was_neutered() ||
15594 array_buffer->backing_store() != nullptr) {
15595 return array_buffer;
15597 Handle<JSTypedArray> self(this);
15598 return MaterializeArrayBuffer(self);
15602 Handle<PropertyCell> PropertyCell::InvalidateEntry(
15603 Handle<GlobalDictionary> dictionary, int entry) {
15604 Isolate* isolate = dictionary->GetIsolate();
15605 // Swap with a copy.
15606 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
15607 Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry)));
15608 auto new_cell = isolate->factory()->NewPropertyCell();
15609 new_cell->set_value(cell->value());
15610 dictionary->ValueAtPut(entry, *new_cell);
15611 bool is_the_hole = cell->value()->IsTheHole();
15612 // Cell is officially mutable henceforth.
15613 PropertyDetails details = cell->property_details();
15614 details = details.set_cell_type(is_the_hole ? PropertyCellType::kInvalidated
15615 : PropertyCellType::kMutable);
15616 new_cell->set_property_details(details);
15617 // Old cell is ready for invalidation.
15619 cell->set_value(isolate->heap()->undefined_value());
15621 cell->set_value(isolate->heap()->the_hole_value());
15623 details = details.set_cell_type(PropertyCellType::kInvalidated);
15624 cell->set_property_details(details);
15625 cell->dependent_code()->DeoptimizeDependentCodeGroup(
15626 isolate, DependentCode::kPropertyCellChangedGroup);
15631 PropertyCellConstantType PropertyCell::GetConstantType() {
15632 if (value()->IsSmi()) return PropertyCellConstantType::kSmi;
15633 return PropertyCellConstantType::kStableMap;
15637 static bool RemainsConstantType(Handle<PropertyCell> cell,
15638 Handle<Object> value) {
15639 // TODO(dcarney): double->smi and smi->double transition from kConstant
15640 if (cell->value()->IsSmi() && value->IsSmi()) {
15642 } else if (cell->value()->IsHeapObject() && value->IsHeapObject()) {
15643 return HeapObject::cast(cell->value())->map() ==
15644 HeapObject::cast(*value)->map() &&
15645 HeapObject::cast(*value)->map()->is_stable();
15651 PropertyCellType PropertyCell::UpdatedType(Handle<PropertyCell> cell,
15652 Handle<Object> value,
15653 PropertyDetails details) {
15654 PropertyCellType type = details.cell_type();
15655 DCHECK(!value->IsTheHole());
15656 if (cell->value()->IsTheHole()) {
15658 // Only allow a cell to transition once into constant state.
15659 case PropertyCellType::kUninitialized:
15660 if (value->IsUndefined()) return PropertyCellType::kUndefined;
15661 return PropertyCellType::kConstant;
15662 case PropertyCellType::kInvalidated:
15663 return PropertyCellType::kMutable;
15666 return PropertyCellType::kMutable;
15670 case PropertyCellType::kUndefined:
15671 return PropertyCellType::kConstant;
15672 case PropertyCellType::kConstant:
15673 if (*value == cell->value()) return PropertyCellType::kConstant;
15675 case PropertyCellType::kConstantType:
15676 if (RemainsConstantType(cell, value)) {
15677 return PropertyCellType::kConstantType;
15680 case PropertyCellType::kMutable:
15681 return PropertyCellType::kMutable;
15684 return PropertyCellType::kMutable;
15688 void PropertyCell::UpdateCell(Handle<GlobalDictionary> dictionary, int entry,
15689 Handle<Object> value, PropertyDetails details) {
15690 DCHECK(!value->IsTheHole());
15691 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
15692 Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry)));
15693 const PropertyDetails original_details = cell->property_details();
15694 // Data accesses could be cached in ics or optimized code.
15696 original_details.kind() == kData && details.kind() == kAccessor;
15697 int index = original_details.dictionary_index();
15698 PropertyCellType old_type = original_details.cell_type();
15699 // Preserve the enumeration index unless the property was deleted or never
15701 if (cell->value()->IsTheHole()) {
15702 index = dictionary->NextEnumerationIndex();
15703 dictionary->SetNextEnumerationIndex(index + 1);
15704 // Negative lookup cells must be invalidated.
15708 details = details.set_index(index);
15710 PropertyCellType new_type = UpdatedType(cell, value, original_details);
15711 if (invalidate) cell = PropertyCell::InvalidateEntry(dictionary, entry);
15713 // Install new property details and cell value.
15714 details = details.set_cell_type(new_type);
15715 cell->set_property_details(details);
15716 cell->set_value(*value);
15718 // Deopt when transitioning from a constant type.
15719 if (!invalidate && (old_type != new_type ||
15720 original_details.IsReadOnly() != details.IsReadOnly())) {
15721 Isolate* isolate = dictionary->GetIsolate();
15722 cell->dependent_code()->DeoptimizeDependentCodeGroup(
15723 isolate, DependentCode::kPropertyCellChangedGroup);
15729 void PropertyCell::SetValueWithInvalidation(Handle<PropertyCell> cell,
15730 Handle<Object> new_value) {
15731 if (cell->value() != *new_value) {
15732 cell->set_value(*new_value);
15733 Isolate* isolate = cell->GetIsolate();
15734 cell->dependent_code()->DeoptimizeDependentCodeGroup(
15735 isolate, DependentCode::kPropertyCellChangedGroup);
15739 } // namespace internal