stub; TRACE_HANDLER_STATS(isolate(), LoadIC_FunctionPrototypeStub); return BUILTIN_CODE(isolate(), LoadIC_FunctionPrototype); } Handle map = receiver_map(); Handle holder; bool receiver_is_holder; if (lookup->state() != LookupIterator::JSPROXY) { holder = lookup->GetHolder(); receiver_is_holder = receiver.is_identical_to(holder); } switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { Handle smi_handler = LoadHandler::LoadInterceptor(isolate()); if (holder->GetNamedInterceptor()->non_masking()) { MaybeObjectHandle holder_ref(isolate()->factory()->null_value()); if (!receiver_is_holder || IsLoadGlobalIC()) { holder_ref = MaybeObjectHandle::Weak(holder); } TRACE_HANDLER_STATS(isolate(), LoadIC_LoadNonMaskingInterceptorDH); return LoadHandler::LoadFullChain(isolate(), map, holder_ref, smi_handler); } if (receiver_is_holder) { DCHECK(map->has_named_interceptor()); TRACE_HANDLER_STATS(isolate(), LoadIC_LoadInterceptorDH); return smi_handler; } TRACE_HANDLER_STATS(isolate(), LoadIC_LoadInterceptorFromPrototypeDH); return LoadHandler::LoadFromPrototype(isolate(), map, holder, smi_handler); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. FieldIndex index; if (Accessors::IsJSObjectFieldAccessor(isolate(), map, lookup->name(), &index)) { TRACE_HANDLER_STATS(isolate(), LoadIC_LoadFieldDH); return LoadHandler::LoadField(isolate(), index); } if (holder->IsJSModuleNamespace()) { Handle exports( Handle::cast(holder)->module()->exports(), isolate()); int entry = exports->FindEntry(roots, lookup->name(), Smi::ToInt(lookup->name()->GetHash())); // We found the accessor, so the entry must exist. DCHECK_NE(entry, ObjectHashTable::kNotFound); int index = ObjectHashTable::EntryToValueIndex(entry); return LoadHandler::LoadModuleExport(isolate(), index); } Handle accessors = lookup->GetAccessors(); if (accessors->IsAccessorPair()) { if (lookup->TryLookupCachedProperty()) { DCHECK_EQ(LookupIterator::DATA, lookup->state()); return ComputeHandler(lookup); } Handle getter(AccessorPair::cast(*accessors)->getter(), isolate()); if (!getter->IsJSFunction() && !getter->IsFunctionTemplateInfo()) { TRACE_HANDLER_STATS(isolate(), LoadIC_SlowStub); return slow_stub(); } if (getter->IsFunctionTemplateInfo() && FunctionTemplateInfo::cast(*getter)->BreakAtEntry()) { // Do not install an IC if the api function has a breakpoint. TRACE_HANDLER_STATS(isolate(), LoadIC_SlowStub); return slow_stub(); } Handle smi_handler; CallOptimization call_optimization(isolate(), getter); if (call_optimization.is_simple_api_call()) { if (!call_optimization.IsCompatibleReceiverMap(map, holder) || !holder->HasFastProperties()) { TRACE_HANDLER_STATS(isolate(), LoadIC_SlowStub); return slow_stub(); } CallOptimization::HolderLookup holder_lookup; call_optimization.LookupHolderOfExpectedType(map, &holder_lookup); smi_handler = LoadHandler::LoadApiGetter( isolate(), holder_lookup == CallOptimization::kHolderIsReceiver); Handle context( call_optimization.GetAccessorContext(holder->map()), isolate()); TRACE_HANDLER_STATS(isolate(), LoadIC_LoadApiGetterFromPrototypeDH); return LoadHandler::LoadFromPrototype( isolate(), map, holder, smi_handler, MaybeObjectHandle::Weak(call_optimization.api_call_info()), MaybeObjectHandle::Weak(context)); } if (holder->HasFastProperties()) { smi_handler = LoadHandler::LoadAccessor(isolate(), lookup->GetAccessorIndex()); TRACE_HANDLER_STATS(isolate(), LoadIC_LoadAccessorDH); if (receiver_is_holder) return smi_handler; TRACE_HANDLER_STATS(isolate(), LoadIC_LoadAccessorFromPrototypeDH); } else if (holder->IsJSGlobalObject()) { TRACE_HANDLER_STATS(isolate(), LoadIC_LoadGlobalFromPrototypeDH); smi_handler = LoadHandler::LoadGlobal(isolate()); return LoadHandler::LoadFromPrototype( isolate(), map, holder, smi_handler, MaybeObjectHandle::Weak(lookup->GetPropertyCell())); } else { smi_handler = LoadHandler::LoadNormal(isolate()); TRACE_HANDLER_STATS(isolate(), LoadIC_LoadNormalDH); if (receiver_is_holder) return smi_handler; TRACE_HANDLER_STATS(isolate(), LoadIC_LoadNormalFromPrototypeDH); } return LoadHandler::LoadFromPrototype(isolate(), map, holder, smi_handler); } Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == kNullAddress || !AccessorInfo::IsCompatibleReceiverMap(info, map) || !holder->HasFastProperties() || (info->is_sloppy() && !receiver->IsJSReceiver())) { TRACE_HANDLER_STATS(isolate(), LoadIC_SlowStub); return slow_stub(); } Handle smi_handler = LoadHandler::LoadNativeDataProperty( isolate(), lookup->GetAccessorIndex()); TRACE_HANDLER_STATS(isolate(), LoadIC_LoadNativeDataPropertyDH); if (receiver_is_holder) return smi_handler; TRACE_HANDLER_STATS(isolate(), LoadIC_LoadNativeDataPropertyFromPrototypeDH); return LoadHandler::LoadFromPrototype(isolate(), map, holder, smi_handler); } case LookupIterator::DATA: { DCHECK_EQ(kData, lookup->property_details().kind()); Handle smi_handler; if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { // TODO(verwaest): Also supporting the global object as receiver is a // workaround for code that leaks the global object. TRACE_HANDLER_STATS(isolate(), LoadIC_LoadGlobalDH); smi_handler = LoadHandler::LoadGlobal(isolate()); return LoadHandler::LoadFromPrototype( isolate(), map, holder, smi_handler, MaybeObjectHandle::Weak(lookup->GetPropertyCell())); } smi_handler = LoadHandler::LoadNormal(isolate()); TRACE_HANDLER_STATS(isolate(), LoadIC_LoadNormalDH); if (receiver_is_holder) return smi_handler; TRACE_HANDLER_STATS(isolate(), LoadIC_LoadNormalFromPrototypeDH); } else if (lookup->property_details().location() == kField) { FieldIndex field = lookup->GetFieldIndex(); smi_handler = LoadHandler::LoadField(isolate(), field); TRACE_HANDLER_STATS(isolate(), LoadIC_LoadFieldDH); if (receiver_is_holder) return smi_handler; TRACE_HANDLER_STATS(isolate(), LoadIC_LoadFieldFromPrototypeDH); } else { DCHECK_EQ(kDescriptor, lookup->property_details().location()); smi_handler = LoadHandler::LoadConstant(isolate(), lookup->GetConstantIndex()); TRACE_HANDLER_STATS(isolate(), LoadIC_LoadConstantDH); if (receiver_is_holder) return smi_handler; TRACE_HANDLER_STATS(isolate(), LoadIC_LoadConstantFromPrototypeDH); } return LoadHandler::LoadFromPrototype(isolate(), map, holder, smi_handler); } case LookupIterator::INTEGER_INDEXED_EXOTIC: TRACE_HANDLER_STATS(isolate(), LoadIC_LoadIntegerIndexedExoticDH); return LoadHandler::LoadNonExistent(isolate()); case LookupIterator::JSPROXY: { Handle holder_proxy = lookup->GetHolder(); bool receiver_is_holder_proxy = receiver.is_identical_to(holder_proxy); Handle smi_handler = LoadHandler::LoadProxy(isolate()); if (receiver_is_holder_proxy) { return smi_handler; } return LoadHandler::LoadFromPrototype(isolate(), map, holder_proxy, smi_handler); } case LookupIterator::ACCESS_CHECK: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return Handle::null(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->NaN_string(); } else { // Check bounds first to avoid undefined behavior in the conversion // to int. if (value <= Smi::kMaxValue && value >= Smi::kMinValue) { int int_value = FastD2I(value); if (value == int_value) { key = handle(Smi::FromInt(int_value), isolate); } } } } else if (key->IsString()) { key = isolate->factory()->InternalizeString(Handle::cast(key)); } return key; } bool KeyedLoadIC::CanChangeToAllowOutOfBounds(Handle receiver_map) { const MaybeObjectHandle& handler = nexus()->FindHandlerForMap(receiver_map); if (handler.is_null()) return false; return LoadHandler::GetKeyedAccessLoadMode(*handler) == STANDARD_LOAD; } void KeyedLoadIC::UpdateLoadElement(Handle receiver, KeyedAccessLoadMode load_mode) { Handle receiver_map(receiver->map(), isolate()); DCHECK(receiver_map->instance_type() != JS_VALUE_TYPE); // Checked by caller. MapHandles target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.empty()) { Handle handler = LoadElementHandler(receiver_map, load_mode); return ConfigureVectorState(Handle(), receiver_map, handler); } for (Handle map : target_receiver_maps) { if (map.is_null()) continue; if (map->instance_type() == JS_VALUE_TYPE) { set_slow_stub_reason("JSValue"); return; } if (map->instance_type() == JS_PROXY_TYPE) { set_slow_stub_reason("JSProxy"); return; } } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && !receiver->IsJSProxy() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = LoadElementHandler(receiver_map, load_mode); return ConfigureVectorState(Handle(), receiver_map, handler); } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the {receiver_map} previously had a handler that didn't handle // out-of-bounds access, but can generally handle it, we can just go // on and update the handler appropriately below. if (load_mode != LOAD_IGNORE_OUT_OF_BOUNDS || !CanChangeToAllowOutOfBounds(receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. set_slow_stub_reason("same map added twice"); return; } } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.size() > kMaxKeyedPolymorphism) { set_slow_stub_reason("max polymorph exceeded"); return; } MaybeObjectHandles handlers; handlers.reserve(target_receiver_maps.size()); LoadElementPolymorphicHandlers(&target_receiver_maps, &handlers, load_mode); DCHECK_LE(1, target_receiver_maps.size()); if (target_receiver_maps.size() == 1) { ConfigureVectorState(Handle(), target_receiver_maps[0], handlers[0]); } else { ConfigureVectorState(Handle(), target_receiver_maps, &handlers); } } Handle KeyedLoadIC::LoadElementHandler(Handle receiver_map, KeyedAccessLoadMode load_mode) { if (receiver_map->has_indexed_interceptor() && !receiver_map->GetIndexedInterceptor()->getter()->IsUndefined( isolate()) && !receiver_map->GetIndexedInterceptor()->non_masking()) { TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_LoadIndexedInterceptorStub); return LoadIndexedInterceptorStub(isolate()).GetCode(); } InstanceType instance_type = receiver_map->instance_type(); if (instance_type < FIRST_NONSTRING_TYPE) { TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_LoadIndexedStringDH); return LoadHandler::LoadIndexedString(isolate(), load_mode); } if (instance_type < FIRST_JS_RECEIVER_TYPE) { TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_SlowStub); return BUILTIN_CODE(isolate(), KeyedLoadIC_Slow); } if (instance_type == JS_PROXY_TYPE) { return LoadHandler::LoadProxy(isolate()); } ElementsKind elements_kind = receiver_map->elements_kind(); if (IsSloppyArgumentsElementsKind(elements_kind)) { TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_KeyedLoadSloppyArgumentsStub); return KeyedLoadSloppyArgumentsStub(isolate()).GetCode(); } bool is_js_array = instance_type == JS_ARRAY_TYPE; if (elements_kind == DICTIONARY_ELEMENTS) { TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_LoadElementDH); return LoadHandler::LoadElement(isolate(), elements_kind, false, is_js_array, load_mode); } DCHECK(IsFastElementsKind(elements_kind) || IsFixedTypedArrayElementsKind(elements_kind)); // TODO(jkummerow): Use IsHoleyOrDictionaryElementsKind(elements_kind). bool convert_hole_to_undefined = is_js_array && elements_kind == HOLEY_ELEMENTS && *receiver_map == isolate()->raw_native_context()->GetInitialJSArrayMap(elements_kind); TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_LoadElementDH); return LoadHandler::LoadElement(isolate(), elements_kind, convert_hole_to_undefined, is_js_array, load_mode); } void KeyedLoadIC::LoadElementPolymorphicHandlers( MapHandles* receiver_maps, MaybeObjectHandles* handlers, KeyedAccessLoadMode load_mode) { // Filter out deprecated maps to ensure their instances get migrated. receiver_maps->erase( std::remove_if( receiver_maps->begin(), receiver_maps->end(), [](const Handle& map) { return map->is_deprecated(); }), receiver_maps->end()); for (Handle receiver_map : *receiver_maps) { // Mark all stable receiver maps that have elements kind transition map // among receiver_maps as unstable because the optimizing compilers may // generate an elements kind transition for this kind of receivers. if (receiver_map->is_stable()) { Map* tmap = receiver_map->FindElementsKindTransitionedMap(isolate(), *receiver_maps); if (tmap != nullptr) { receiver_map->NotifyLeafMapLayoutChange(isolate()); } } handlers->push_back( MaybeObjectHandle(LoadElementHandler(receiver_map, load_mode))); } } namespace { bool ConvertKeyToIndex(Handle receiver, Handle key, uint32_t* index) { if (!FLAG_use_ic) return false; if (receiver->IsAccessCheckNeeded() || receiver->IsJSValue()) return false; // For regular JSReceiver or String receivers, the {key} must be a positive // array index. if (receiver->IsJSReceiver() || receiver->IsString()) { if (key->ToArrayIndex(index)) return true; } // For JSTypedArray receivers, we can also support negative keys, which we // just map into the [2**31, 2**32 - 1] range via a bit_cast. This is valid // because JSTypedArray::length is always a Smi, so such keys will always // be detected as OOB. if (receiver->IsJSTypedArray()) { int32_t signed_index; if (key->ToInt32(&signed_index)) { *index = bit_cast(signed_index); return true; } } return false; } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else if (receiver->IsString()) { length = String::cast(*receiver)->length(); } else if (receiver->IsJSObject()) { length = JSObject::cast(*receiver)->elements()->length(); } else { return false; } return index >= length; } KeyedAccessLoadMode GetLoadMode(Isolate* isolate, Handle receiver, uint32_t index) { if (IsOutOfBoundsAccess(receiver, index)) { if (receiver->IsJSTypedArray()) { // For JSTypedArray we never lookup elements in the prototype chain. return LOAD_IGNORE_OUT_OF_BOUNDS; } // For other {receiver}s we need to check the "no elements" protector. if (isolate->IsNoElementsProtectorIntact()) { if (receiver->IsString()) { // ToObject(receiver) will have the initial String.prototype. return LOAD_IGNORE_OUT_OF_BOUNDS; } if (receiver->IsJSObject()) { // For other JSObjects (including JSArrays) we can only continue if // the {receiver}s prototype is either the initial Object.prototype // or the initial Array.prototype, which are both guarded by the // "no elements" protector checked above. Handle receiver_prototype( JSObject::cast(*receiver)->map()->prototype(), isolate); if (isolate->IsInAnyContext(*receiver_prototype, Context::INITIAL_ARRAY_PROTOTYPE_INDEX) || isolate->IsInAnyContext(*receiver_prototype, Context::INITIAL_OBJECT_PROTOTYPE_INDEX)) { return LOAD_IGNORE_OUT_OF_BOUNDS; } } } } return STANDARD_LOAD; } } // namespace MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (ConvertKeyToIndex(object, key, &index)) { KeyedAccessLoadMode load_mode = GetLoadMode(isolate(), object, index); UpdateLoadElement(Handle::cast(object), load_mode); if (is_vector_set()) { TraceIC("LoadIC", key); } } if (vector_needs_update()) { ConfigureVectorState(MEGAMORPHIC, key); TraceIC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION(isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle object = it->GetReceiver(); if (object->IsJSProxy()) return true; if (!object->IsJSObject()) return false; Handle receiver = Handle::cast(object); DCHECK(!receiver->map()->is_deprecated()); if (it->state() != LookupIterator::TRANSITION) { for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return true; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { return !info->non_masking() && receiver.is_identical_to(holder) && !info->setter()->IsUndefined(isolate()); } else if (!info->getter()->IsUndefined(isolate()) || !info->query()->IsUndefined(isolate())) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. if (receiver->IsJSGlobalProxy()) { PrototypeIterator iter(isolate(), receiver); return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; if (it->ExtendingNonExtensible(receiver)) return false; it->PrepareTransitionToDataProperty(receiver, value, NONE, store_mode); return it->IsCacheableTransition(); } } } } receiver = it->GetStoreTarget(); if (it->ExtendingNonExtensible(receiver)) return false; it->PrepareTransitionToDataProperty(receiver, value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreGlobalIC::Store(Handle name, Handle value) { DCHECK(name->IsString()); // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = isolate()->global_object(); Handle script_contexts( global->native_context()->script_context_table(), isolate()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(isolate(), script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( isolate(), script_contexts, lookup_result.context_index); if (lookup_result.mode == VariableMode::kConst) { return TypeError(MessageTemplate::kConstAssign, global, name); } Handle previous_value = FixedArray::get(*script_context, lookup_result.slot_index, isolate()); if (previous_value->IsTheHole(isolate())) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic) { if (nexus()->ConfigureLexicalVarMode(lookup_result.context_index, lookup_result.slot_index)) { TRACE_HANDLER_STATS(isolate(), StoreGlobalIC_StoreScriptContextField); } else { // Given combination of indices can't be encoded, so use slow stub. TRACE_HANDLER_STATS(isolate(), StoreGlobalIC_SlowStub); PatchCache(name, slow_stub()); } TraceIC("StoreGlobalIC", name); } script_context->set(lookup_result.slot_index, *value); return value; } return StoreIC::Store(global, name, value); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(isolate(), object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsNullOrUndefined(isolate())) { if (FLAG_use_ic && state() != PREMONOMORPHIC) { // Ensure the IC state progresses. TRACE_HANDLER_STATS(isolate(), StoreIC_NonReceiver); update_receiver_map(object); PatchCache(name, slow_stub()); TraceIC("StoreIC", name); } return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } if (state() != UNINITIALIZED) { JSObject::MakePrototypesFast(object, kStartAtPrototype, isolate()); } LookupIterator it(isolate(), object, name); bool use_ic = FLAG_use_ic; if (name->IsPrivate()) { if (name->IsPrivateField() && !it.IsFound()) { return TypeError(MessageTemplate::kInvalidPrivateFieldAccess, object, name); } // IC handling of private fields/symbols stores on JSProxy is not // supported. if (object->IsJSProxy()) { use_ic = false; } } if (use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED && !IsStoreGlobalIC()) { // This is the first time we execute this inline cache. Transition // to premonomorphic state to delay setting the monomorphic state. TRACE_HANDLER_STATS(isolate(), StoreIC_Premonomorphic); ConfigureVectorState(receiver_map()); TraceIC("StoreIC", lookup->name()); return; } MaybeObjectHandle handler; if (LookupForWrite(lookup, value, store_mode)) { if (IsStoreGlobalIC()) { if (lookup->state() == LookupIterator::DATA && lookup->GetReceiver().is_identical_to(lookup->GetHolder())) { DCHECK(lookup->GetReceiver()->IsJSGlobalObject()); // Now update the cell in the feedback vector. nexus()->ConfigurePropertyCellMode(lookup->GetPropertyCell()); TraceIC("StoreGlobalIC", lookup->name()); return; } } handler = ComputeHandler(lookup); } else { set_slow_stub_reason("LookupForWrite said 'false'"); // TODO(marja): change slow_stub to return MaybeObjectHandle. handler = MaybeObjectHandle(slow_stub()); } PatchCache(lookup->name(), handler); TraceIC("StoreIC", lookup->name()); } MaybeObjectHandle StoreIC::ComputeHandler(LookupIterator* lookup) { switch (lookup->state()) { case LookupIterator::TRANSITION: { Handle store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { TRACE_HANDLER_STATS(isolate(), StoreIC_StoreGlobalTransitionDH); if (receiver_map()->IsJSGlobalObject()) { DCHECK(IsStoreGlobalIC()); #ifdef DEBUG Handle holder = lookup->GetHolder(); DCHECK_EQ(*lookup->GetReceiver(), *holder); DCHECK_EQ(*store_target, *holder); #endif return StoreHandler::StoreGlobal(lookup->transition_cell()); } Handle smi_handler = StoreHandler::StoreGlobalProxy(isolate()); Handle handler = StoreHandler::StoreThroughPrototype( isolate(), receiver_map(), store_target, smi_handler, MaybeObjectHandle::Weak(lookup->transition_cell())); return MaybeObjectHandle(handler); } // Dictionary-to-fast transitions are not expected and not supported. DCHECK_IMPLIES(!lookup->transition_map()->is_dictionary_map(), !receiver_map()->is_dictionary_map()); DCHECK(lookup->IsCacheableTransition()); return StoreHandler::StoreTransition(isolate(), lookup->transition_map()); } case LookupIterator::INTERCEPTOR: { Handle holder = lookup->GetHolder(); USE(holder); DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined(isolate())); TRACE_HANDLER_STATS(isolate(), StoreIC_StoreInterceptorStub); StoreInterceptorStub stub(isolate()); return MaybeObjectHandle(stub.GetCode()); } case LookupIterator::ACCESSOR: { // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || lookup->name()->IsPrivate()); if (!holder->HasFastProperties()) { set_slow_stub_reason("accessor on slow map"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } Handle accessors = lookup->GetAccessors(); if (accessors->IsAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == kNullAddress) { set_slow_stub_reason("setter == kNullAddress"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { set_slow_stub_reason("special data property in prototype chain"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } if (!AccessorInfo::IsCompatibleReceiverMap(info, receiver_map())) { set_slow_stub_reason("incompatible receiver type"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } Handle smi_handler = StoreHandler::StoreNativeDataProperty( isolate(), lookup->GetAccessorIndex()); TRACE_HANDLER_STATS(isolate(), StoreIC_StoreNativeDataPropertyDH); if (receiver.is_identical_to(holder)) { return MaybeObjectHandle(smi_handler); } TRACE_HANDLER_STATS(isolate(), StoreIC_StoreNativeDataPropertyOnPrototypeDH); return MaybeObjectHandle(StoreHandler::StoreThroughPrototype( isolate(), receiver_map(), holder, smi_handler)); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction() && !setter->IsFunctionTemplateInfo()) { set_slow_stub_reason("setter not a function"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } if (setter->IsFunctionTemplateInfo() && FunctionTemplateInfo::cast(*setter)->BreakAtEntry()) { // Do not install an IC if the api function has a breakpoint. TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } CallOptimization call_optimization(isolate(), setter); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { CallOptimization::HolderLookup holder_lookup; call_optimization.LookupHolderOfExpectedType(receiver_map(), &holder_lookup); Handle smi_handler = StoreHandler::StoreApiSetter( isolate(), holder_lookup == CallOptimization::kHolderIsReceiver); Handle context( call_optimization.GetAccessorContext(holder->map()), isolate()); TRACE_HANDLER_STATS(isolate(), StoreIC_StoreApiSetterOnPrototypeDH); return MaybeObjectHandle(StoreHandler::StoreThroughPrototype( isolate(), receiver_map(), holder, smi_handler, MaybeObjectHandle::Weak(call_optimization.api_call_info()), MaybeObjectHandle::Weak(context))); } set_slow_stub_reason("incompatible receiver"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } else if (setter->IsFunctionTemplateInfo()) { set_slow_stub_reason("setter non-simple template"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } Handle smi_handler = StoreHandler::StoreAccessor(isolate(), lookup->GetAccessorIndex()); TRACE_HANDLER_STATS(isolate(), StoreIC_StoreAccessorDH); if (receiver.is_identical_to(holder)) { return MaybeObjectHandle(smi_handler); } TRACE_HANDLER_STATS(isolate(), StoreIC_StoreAccessorOnPrototypeDH); return MaybeObjectHandle(StoreHandler::StoreThroughPrototype( isolate(), receiver_map(), holder, smi_handler)); } TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } case LookupIterator::DATA: { // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); USE(receiver); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || lookup->name()->IsPrivate()); DCHECK_EQ(kData, lookup->property_details().kind()); if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { TRACE_HANDLER_STATS(isolate(), StoreIC_StoreGlobalDH); return MaybeObjectHandle( StoreHandler::StoreGlobal(lookup->GetPropertyCell())); } TRACE_HANDLER_STATS(isolate(), StoreIC_StoreNormalDH); DCHECK(holder.is_identical_to(receiver)); return MaybeObjectHandle(StoreHandler::StoreNormal(isolate())); } // -------------- Fields -------------- if (lookup->property_details().location() == kField) { TRACE_HANDLER_STATS(isolate(), StoreIC_StoreFieldDH); int descriptor = lookup->GetFieldDescriptorIndex(); FieldIndex index = lookup->GetFieldIndex(); PropertyConstness constness = lookup->constness(); if (constness == PropertyConstness::kConst && IsStoreOwnICKind(nexus()->kind())) { // StoreOwnICs are used for initializing object literals therefore // we must store the value unconditionally even to // VariableMode::kConst fields. constness = PropertyConstness::kMutable; } return MaybeObjectHandle(StoreHandler::StoreField( isolate(), descriptor, index, constness, lookup->representation())); } // -------------- Constant properties -------------- DCHECK_EQ(kDescriptor, lookup->property_details().location()); set_slow_stub_reason("constant property"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } case LookupIterator::JSPROXY: { Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); return MaybeObjectHandle(StoreHandler::StoreProxy( isolate(), receiver_map(), holder, receiver)); } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return MaybeObjectHandle(); } void KeyedStoreIC::UpdateStoreElement(Handle receiver_map, KeyedAccessStoreMode store_mode, bool receiver_was_cow) { MapHandles target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.empty()) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode, receiver_was_cow); Handle handler = StoreElementHandler(monomorphic_map, store_mode); return ConfigureVectorState(Handle(), monomorphic_map, handler); } for (Handle map : target_receiver_maps) { if (!map.is_null() && map->instance_type() == JS_VALUE_TYPE) { DCHECK(!IsStoreInArrayLiteralICKind(kind())); set_slow_stub_reason("JSValue"); return; } } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode; old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode, receiver_was_cow); Handle handler = StoreElementHandler(transitioned_receiver_map, store_mode); ConfigureVectorState(Handle(), transitioned_receiver_map, handler); return; } if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION_HANDLE_COW || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = StoreElementHandler(receiver_map, store_mode); return ConfigureVectorState(Handle(), receiver_map, handler); } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. set_slow_stub_reason("same map added twice"); return; } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.size() > kMaxKeyedPolymorphism) return; // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode, receiver_was_cow); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { set_slow_stub_reason("store mode mismatch"); return; } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { size_t external_arrays = 0; for (Handle map : target_receiver_maps) { if (map->has_fixed_typed_array_elements()) { DCHECK(!IsStoreInArrayLiteralICKind(kind())); external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.size()) { DCHECK(!IsStoreInArrayLiteralICKind(kind())); set_slow_stub_reason( "unsupported combination of external and normal arrays"); return; } } MaybeObjectHandles handlers; handlers.reserve(target_receiver_maps.size()); StoreElementPolymorphicHandlers(&target_receiver_maps, &handlers, store_mode); if (target_receiver_maps.size() == 0) { // Transition to PREMONOMORPHIC state here and remember a weak-reference // to the {receiver_map} in case TurboFan sees this function before the // IC can transition further. ConfigureVectorState(receiver_map); } else if (target_receiver_maps.size() == 1) { ConfigureVectorState(Handle(), target_receiver_maps[0], handlers[0]); } else { ConfigureVectorState(Handle(), target_receiver_maps, &handlers); } } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsHoleyElementsKind(map->elements_kind()) ? HOLEY_ELEMENTS : PACKED_ELEMENTS; return Map::TransitionElementsTo(isolate(), map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsHoleyElementsKind(map->elements_kind()) ? HOLEY_DOUBLE_ELEMENTS : PACKED_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(isolate(), map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); V8_FALLTHROUGH; case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION_HANDLE_COW: return map; } UNREACHABLE(); } Handle KeyedStoreIC::StoreElementHandler( Handle receiver_map, KeyedAccessStoreMode store_mode) { DCHECK(store_mode == STANDARD_STORE || store_mode == STORE_AND_GROW_NO_TRANSITION_HANDLE_COW || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW); DCHECK_IMPLIES( receiver_map->DictionaryElementsInPrototypeChainOnly(isolate()), IsStoreInArrayLiteralICKind(kind())); if (receiver_map->IsJSProxyMap()) { return StoreHandler::StoreProxy(isolate()); } // TODO(ishell): move to StoreHandler::StoreElement(). ElementsKind elements_kind = receiver_map->elements_kind(); bool is_jsarray = receiver_map->instance_type() == JS_ARRAY_TYPE; Handle stub; if (receiver_map->has_sloppy_arguments_elements()) { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_KeyedStoreSloppyArgumentsStub); stub = KeyedStoreSloppyArgumentsStub(isolate(), store_mode).GetCode(); } else if (receiver_map->has_fast_elements() || receiver_map->has_fixed_typed_array_elements()) { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_StoreFastElementStub); stub = StoreFastElementStub(isolate(), is_jsarray, elements_kind, store_mode) .GetCode(); if (receiver_map->has_fixed_typed_array_elements()) return stub; } else if (IsStoreInArrayLiteralICKind(kind())) { TRACE_HANDLER_STATS(isolate(), StoreInArrayLiteralIC_SlowStub); stub = StoreInArrayLiteralSlowStub(isolate(), store_mode).GetCode(); } else { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_StoreElementStub); DCHECK_EQ(DICTIONARY_ELEMENTS, elements_kind); stub = StoreSlowElementStub(isolate(), store_mode).GetCode(); } if (IsStoreInArrayLiteralICKind(kind())) return stub; Handle validity_cell = Map::GetOrCreatePrototypeChainValidityCell(receiver_map, isolate()); if (validity_cell->IsSmi()) { // There's no prototype validity cell to check, so we can just use the stub. return stub; } Handle handler = isolate()->factory()->NewStoreHandler(0); handler->set_validity_cell(*validity_cell); handler->set_smi_handler(*stub); return handler; } void KeyedStoreIC::StoreElementPolymorphicHandlers( MapHandles* receiver_maps, MaybeObjectHandles* handlers, KeyedAccessStoreMode store_mode) { DCHECK(store_mode == STANDARD_STORE || store_mode == STORE_AND_GROW_NO_TRANSITION_HANDLE_COW || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW); // Filter out deprecated maps to ensure their instances get migrated. receiver_maps->erase( std::remove_if( receiver_maps->begin(), receiver_maps->end(), [](const Handle& map) { return map->is_deprecated(); }), receiver_maps->end()); for (Handle receiver_map : *receiver_maps) { Handle handler; Handle transition; if (receiver_map->instance_type() < FIRST_JS_RECEIVER_TYPE || receiver_map->DictionaryElementsInPrototypeChainOnly(isolate())) { // TODO(mvstanton): Consider embedding store_mode in the state of the slow // keyed store ic for uniformity. TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_SlowStub); handler = slow_stub(); } else { { Map* tmap = receiver_map->FindElementsKindTransitionedMap( isolate(), *receiver_maps); if (tmap != nullptr) { if (receiver_map->is_stable()) { receiver_map->NotifyLeafMapLayoutChange(isolate()); } transition = handle(tmap, isolate()); } } // TODO(mvstanton): The code below is doing pessimistic elements // transitions. I would like to stop doing that and rely on Allocation // Site Tracking to do a better job of ensuring the data types are what // they need to be. Not all the elements are in place yet, pessimistic // elements transitions are still important for performance. if (!transition.is_null()) { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_ElementsTransitionAndStoreStub); handler = StoreHandler::StoreElementTransition(isolate(), receiver_map, transition, store_mode); } else { handler = StoreElementHandler(receiver_map, store_mode); } } DCHECK(!handler.is_null()); handlers->push_back(MaybeObjectHandle(handler)); } } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION_HANDLE_COW; } else { // Handle only in-bounds elements accesses. if (receiver->HasSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } return receiver->elements()->IsCowArray() ? STORE_NO_TRANSITION_HANDLE_COW : STANDARD_STORE; } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (vector_needs_update()) { if (ConfigureVectorState(MEGAMORPHIC, key)) { set_slow_stub_reason("unhandled internalized string key"); TraceIC("StoreIC", key); } } return store_handle; } JSObject::MakePrototypesFast(object, kStartAtPrototype, isolate()); bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy(); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain(isolate())) { set_slow_stub_reason("map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool is_arguments = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSReceiver()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); is_arguments = receiver->IsJSArgumentsObject(); bool is_proxy = receiver->IsJSProxy(); // For JSTypedArray {object}s we can handle negative indices as OOB // accesses, since integer indexed properties are never looked up // on the prototype chain. For this we simply map the negative {key}s // to the [2**31,2**32-1] range, which is safe since JSTypedArray::length // is always an unsigned Smi. key_is_valid_index = key->IsSmi() && (Smi::ToInt(*key) >= 0 || object->IsJSTypedArray()); if (!is_arguments && !is_proxy) { if (key_is_valid_index) { uint32_t index = static_cast(Smi::ToInt(*key)); Handle receiver_object = Handle::cast(object); store_mode = GetStoreMode(receiver_object, index, value); } } } DCHECK(store_handle.is_null()); bool receiver_was_cow = object->IsJSArray() && Handle::cast(object)->elements()->IsCowArray(); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (is_arguments) { set_slow_stub_reason("arguments receiver"); } else if (key_is_valid_index) { if (old_receiver_map->is_abandoned_prototype_map()) { set_slow_stub_reason("receiver with prototype map"); } else if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly( isolate())) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. UpdateStoreElement(old_receiver_map, store_mode, receiver_was_cow); } else { set_slow_stub_reason("dictionary or proxy prototype"); } } else { set_slow_stub_reason("non-smi-like key"); } } else { set_slow_stub_reason("non-JSObject receiver"); } } if (vector_needs_update()) { ConfigureVectorState(MEGAMORPHIC, key); } TraceIC("StoreIC", key); return store_handle; } namespace { void StoreOwnElement(Isolate* isolate, Handle array, Handle index, Handle value) { DCHECK(index->IsNumber()); bool success = false; LookupIterator it = LookupIterator::PropertyOrElement( isolate, array, index, &success, LookupIterator::OWN); DCHECK(success); CHECK(JSObject::DefineOwnPropertyIgnoreAttributes(&it, value, NONE, kThrowOnError) .FromJust()); } } // namespace void StoreInArrayLiteralIC::Store(Handle array, Handle index, Handle value) { DCHECK(!array->map()->IsMapInArrayPrototypeChain(isolate())); DCHECK(index->IsNumber()); if (!FLAG_use_ic || MigrateDeprecated(array)) { StoreOwnElement(isolate(), array, index, value); TraceIC("StoreInArrayLiteralIC", index); return; } // TODO(neis): Convert HeapNumber to Smi if possible? KeyedAccessStoreMode store_mode = STANDARD_STORE; if (index->IsSmi()) { DCHECK_GE(Smi::ToInt(*index), 0); uint32_t index32 = static_cast(Smi::ToInt(*index)); store_mode = GetStoreMode(array, index32, value); } Handle old_array_map(array->map(), isolate()); bool array_was_cow = array->elements()->IsCowArray(); StoreOwnElement(isolate(), array, index, value); if (index->IsSmi()) { DCHECK(!old_array_map->is_abandoned_prototype_map()); UpdateStoreElement(old_array_map, store_mode, array_was_cow); } else { set_slow_stub_reason("index out of Smi range"); } if (vector_needs_update()) { ConfigureVectorState(MEGAMORPHIC, index); } TraceIC("StoreInArrayLiteralIC", index); } // ---------------------------------------------------------------------------- // Static IC stub generators. // RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { HandleScope scope(isolate); DCHECK_EQ(4, args.length()); // Runtime functions don't follow the IC's calling convention. Handle receiver = args.at(0); Handle key = args.at(1); Handle slot = args.at(2); Handle vector = args.at(3); FeedbackSlot vector_slot = FeedbackVector::ToSlot(slot->value()); // A monomorphic or polymorphic KeyedLoadIC with a string key can call the // LoadIC miss handler if the handler misses. Since the vector Nexus is // set up outside the IC, handle that here. FeedbackSlotKind kind = vector->GetKind(vector_slot); if (IsLoadICKind(kind)) { LoadIC ic(isolate, vector, vector_slot); ic.UpdateState(receiver, key); RETURN_RESULT_OR_FAILURE(isolate, ic.Load(receiver, key)); } else if (IsLoadGlobalICKind(kind)) { DCHECK_EQ(isolate->native_context()->global_proxy(), *receiver); receiver = isolate->global_object(); LoadGlobalIC ic(isolate, vector, vector_slot); ic.UpdateState(receiver, key); RETURN_RESULT_OR_FAILURE(isolate, ic.Load(key)); } else { DCHECK(IsKeyedLoadICKind(kind)); KeyedLoadIC ic(isolate, vector, vector_slot); ic.UpdateState(receiver, key); RETURN_RESULT_OR_FAILURE(isolate, ic.Load(receiver, key)); } } RUNTIME_FUNCTION(Runtime_LoadGlobalIC_Miss) { HandleScope scope(isolate); DCHECK_EQ(3, args.length()); // Runtime functions don't follow the IC's calling convention. Handle global = isolate->global_object(); Handle name = args.at(0); Handle slot = args.at(1); Handle vector = args.at(2); FeedbackSlot vector_slot = FeedbackVector::ToSlot(slot->value()); LoadGlobalIC ic(isolate, vector, vector_slot); ic.UpdateState(global, name); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(name)); return *result; } RUNTIME_FUNCTION(Runtime_LoadGlobalIC_Slow) { HandleScope scope(isolate); DCHECK_EQ(3, args.length()); CONVERT_ARG_HANDLE_CHECKED(String, name, 0); Handle native_context = isolate->native_context(); Handle script_contexts( native_context->script_context_table(), isolate); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(isolate, script_contexts, name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( isolate, script_contexts, lookup_result.context_index); Handle result = FixedArray::get(*script_context, lookup_result.slot_index, isolate); if (*result == ReadOnlyRoots(isolate).the_hole_value()) { THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewReferenceError(MessageTemplate::kNotDefined, name)); } return *result; } Handle global(native_context->global_object(), isolate); Handle result; bool is_found = false; ASSIGN_RETURN_FAILURE_ON_EXCEPTION( isolate, result, Runtime::GetObjectProperty(isolate, global, name, &is_found)); if (!is_found) { Handle
::null(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->NaN_string(); } else { // Check bounds first to avoid undefined behavior in the conversion // to int. if (value <= Smi::kMaxValue && value >= Smi::kMinValue) { int int_value = FastD2I(value); if (value == int_value) { key = handle(Smi::FromInt(int_value), isolate); } } } } else if (key->IsString()) { key = isolate->factory()->InternalizeString(Handle::cast(key)); } return key; } bool KeyedLoadIC::CanChangeToAllowOutOfBounds(Handle receiver_map) { const MaybeObjectHandle& handler = nexus()->FindHandlerForMap(receiver_map); if (handler.is_null()) return false; return LoadHandler::GetKeyedAccessLoadMode(*handler) == STANDARD_LOAD; } void KeyedLoadIC::UpdateLoadElement(Handle receiver, KeyedAccessLoadMode load_mode) { Handle receiver_map(receiver->map(), isolate()); DCHECK(receiver_map->instance_type() != JS_VALUE_TYPE); // Checked by caller. MapHandles target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.empty()) { Handle handler = LoadElementHandler(receiver_map, load_mode); return ConfigureVectorState(Handle(), receiver_map, handler); } for (Handle map : target_receiver_maps) { if (map.is_null()) continue; if (map->instance_type() == JS_VALUE_TYPE) { set_slow_stub_reason("JSValue"); return; } if (map->instance_type() == JS_PROXY_TYPE) { set_slow_stub_reason("JSProxy"); return; } } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && !receiver->IsJSProxy() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = LoadElementHandler(receiver_map, load_mode); return ConfigureVectorState(Handle(), receiver_map, handler); } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the {receiver_map} previously had a handler that didn't handle // out-of-bounds access, but can generally handle it, we can just go // on and update the handler appropriately below. if (load_mode != LOAD_IGNORE_OUT_OF_BOUNDS || !CanChangeToAllowOutOfBounds(receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. set_slow_stub_reason("same map added twice"); return; } } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.size() > kMaxKeyedPolymorphism) { set_slow_stub_reason("max polymorph exceeded"); return; } MaybeObjectHandles handlers; handlers.reserve(target_receiver_maps.size()); LoadElementPolymorphicHandlers(&target_receiver_maps, &handlers, load_mode); DCHECK_LE(1, target_receiver_maps.size()); if (target_receiver_maps.size() == 1) { ConfigureVectorState(Handle(), target_receiver_maps[0], handlers[0]); } else { ConfigureVectorState(Handle(), target_receiver_maps, &handlers); } } Handle KeyedLoadIC::LoadElementHandler(Handle receiver_map, KeyedAccessLoadMode load_mode) { if (receiver_map->has_indexed_interceptor() && !receiver_map->GetIndexedInterceptor()->getter()->IsUndefined( isolate()) && !receiver_map->GetIndexedInterceptor()->non_masking()) { TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_LoadIndexedInterceptorStub); return LoadIndexedInterceptorStub(isolate()).GetCode(); } InstanceType instance_type = receiver_map->instance_type(); if (instance_type < FIRST_NONSTRING_TYPE) { TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_LoadIndexedStringDH); return LoadHandler::LoadIndexedString(isolate(), load_mode); } if (instance_type < FIRST_JS_RECEIVER_TYPE) { TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_SlowStub); return BUILTIN_CODE(isolate(), KeyedLoadIC_Slow); } if (instance_type == JS_PROXY_TYPE) { return LoadHandler::LoadProxy(isolate()); } ElementsKind elements_kind = receiver_map->elements_kind(); if (IsSloppyArgumentsElementsKind(elements_kind)) { TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_KeyedLoadSloppyArgumentsStub); return KeyedLoadSloppyArgumentsStub(isolate()).GetCode(); } bool is_js_array = instance_type == JS_ARRAY_TYPE; if (elements_kind == DICTIONARY_ELEMENTS) { TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_LoadElementDH); return LoadHandler::LoadElement(isolate(), elements_kind, false, is_js_array, load_mode); } DCHECK(IsFastElementsKind(elements_kind) || IsFixedTypedArrayElementsKind(elements_kind)); // TODO(jkummerow): Use IsHoleyOrDictionaryElementsKind(elements_kind). bool convert_hole_to_undefined = is_js_array && elements_kind == HOLEY_ELEMENTS && *receiver_map == isolate()->raw_native_context()->GetInitialJSArrayMap(elements_kind); TRACE_HANDLER_STATS(isolate(), KeyedLoadIC_LoadElementDH); return LoadHandler::LoadElement(isolate(), elements_kind, convert_hole_to_undefined, is_js_array, load_mode); } void KeyedLoadIC::LoadElementPolymorphicHandlers( MapHandles* receiver_maps, MaybeObjectHandles* handlers, KeyedAccessLoadMode load_mode) { // Filter out deprecated maps to ensure their instances get migrated. receiver_maps->erase( std::remove_if( receiver_maps->begin(), receiver_maps->end(), [](const Handle& map) { return map->is_deprecated(); }), receiver_maps->end()); for (Handle receiver_map : *receiver_maps) { // Mark all stable receiver maps that have elements kind transition map // among receiver_maps as unstable because the optimizing compilers may // generate an elements kind transition for this kind of receivers. if (receiver_map->is_stable()) { Map* tmap = receiver_map->FindElementsKindTransitionedMap(isolate(), *receiver_maps); if (tmap != nullptr) { receiver_map->NotifyLeafMapLayoutChange(isolate()); } } handlers->push_back( MaybeObjectHandle(LoadElementHandler(receiver_map, load_mode))); } } namespace { bool ConvertKeyToIndex(Handle receiver, Handle key, uint32_t* index) { if (!FLAG_use_ic) return false; if (receiver->IsAccessCheckNeeded() || receiver->IsJSValue()) return false; // For regular JSReceiver or String receivers, the {key} must be a positive // array index. if (receiver->IsJSReceiver() || receiver->IsString()) { if (key->ToArrayIndex(index)) return true; } // For JSTypedArray receivers, we can also support negative keys, which we // just map into the [2**31, 2**32 - 1] range via a bit_cast. This is valid // because JSTypedArray::length is always a Smi, so such keys will always // be detected as OOB. if (receiver->IsJSTypedArray()) { int32_t signed_index; if (key->ToInt32(&signed_index)) { *index = bit_cast(signed_index); return true; } } return false; } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else if (receiver->IsString()) { length = String::cast(*receiver)->length(); } else if (receiver->IsJSObject()) { length = JSObject::cast(*receiver)->elements()->length(); } else { return false; } return index >= length; } KeyedAccessLoadMode GetLoadMode(Isolate* isolate, Handle receiver, uint32_t index) { if (IsOutOfBoundsAccess(receiver, index)) { if (receiver->IsJSTypedArray()) { // For JSTypedArray we never lookup elements in the prototype chain. return LOAD_IGNORE_OUT_OF_BOUNDS; } // For other {receiver}s we need to check the "no elements" protector. if (isolate->IsNoElementsProtectorIntact()) { if (receiver->IsString()) { // ToObject(receiver) will have the initial String.prototype. return LOAD_IGNORE_OUT_OF_BOUNDS; } if (receiver->IsJSObject()) { // For other JSObjects (including JSArrays) we can only continue if // the {receiver}s prototype is either the initial Object.prototype // or the initial Array.prototype, which are both guarded by the // "no elements" protector checked above. Handle receiver_prototype( JSObject::cast(*receiver)->map()->prototype(), isolate); if (isolate->IsInAnyContext(*receiver_prototype, Context::INITIAL_ARRAY_PROTOTYPE_INDEX) || isolate->IsInAnyContext(*receiver_prototype, Context::INITIAL_OBJECT_PROTOTYPE_INDEX)) { return LOAD_IGNORE_OUT_OF_BOUNDS; } } } } return STANDARD_LOAD; } } // namespace MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (ConvertKeyToIndex(object, key, &index)) { KeyedAccessLoadMode load_mode = GetLoadMode(isolate(), object, index); UpdateLoadElement(Handle::cast(object), load_mode); if (is_vector_set()) { TraceIC("LoadIC", key); } } if (vector_needs_update()) { ConfigureVectorState(MEGAMORPHIC, key); TraceIC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION(isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle object = it->GetReceiver(); if (object->IsJSProxy()) return true; if (!object->IsJSObject()) return false; Handle receiver = Handle::cast(object); DCHECK(!receiver->map()->is_deprecated()); if (it->state() != LookupIterator::TRANSITION) { for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return true; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { return !info->non_masking() && receiver.is_identical_to(holder) && !info->setter()->IsUndefined(isolate()); } else if (!info->getter()->IsUndefined(isolate()) || !info->query()->IsUndefined(isolate())) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. if (receiver->IsJSGlobalProxy()) { PrototypeIterator iter(isolate(), receiver); return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; if (it->ExtendingNonExtensible(receiver)) return false; it->PrepareTransitionToDataProperty(receiver, value, NONE, store_mode); return it->IsCacheableTransition(); } } } } receiver = it->GetStoreTarget(); if (it->ExtendingNonExtensible(receiver)) return false; it->PrepareTransitionToDataProperty(receiver, value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreGlobalIC::Store(Handle name, Handle value) { DCHECK(name->IsString()); // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = isolate()->global_object(); Handle script_contexts( global->native_context()->script_context_table(), isolate()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(isolate(), script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( isolate(), script_contexts, lookup_result.context_index); if (lookup_result.mode == VariableMode::kConst) { return TypeError(MessageTemplate::kConstAssign, global, name); } Handle previous_value = FixedArray::get(*script_context, lookup_result.slot_index, isolate()); if (previous_value->IsTheHole(isolate())) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic) { if (nexus()->ConfigureLexicalVarMode(lookup_result.context_index, lookup_result.slot_index)) { TRACE_HANDLER_STATS(isolate(), StoreGlobalIC_StoreScriptContextField); } else { // Given combination of indices can't be encoded, so use slow stub. TRACE_HANDLER_STATS(isolate(), StoreGlobalIC_SlowStub); PatchCache(name, slow_stub()); } TraceIC("StoreGlobalIC", name); } script_context->set(lookup_result.slot_index, *value); return value; } return StoreIC::Store(global, name, value); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(isolate(), object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsNullOrUndefined(isolate())) { if (FLAG_use_ic && state() != PREMONOMORPHIC) { // Ensure the IC state progresses. TRACE_HANDLER_STATS(isolate(), StoreIC_NonReceiver); update_receiver_map(object); PatchCache(name, slow_stub()); TraceIC("StoreIC", name); } return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } if (state() != UNINITIALIZED) { JSObject::MakePrototypesFast(object, kStartAtPrototype, isolate()); } LookupIterator it(isolate(), object, name); bool use_ic = FLAG_use_ic; if (name->IsPrivate()) { if (name->IsPrivateField() && !it.IsFound()) { return TypeError(MessageTemplate::kInvalidPrivateFieldAccess, object, name); } // IC handling of private fields/symbols stores on JSProxy is not // supported. if (object->IsJSProxy()) { use_ic = false; } } if (use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED && !IsStoreGlobalIC()) { // This is the first time we execute this inline cache. Transition // to premonomorphic state to delay setting the monomorphic state. TRACE_HANDLER_STATS(isolate(), StoreIC_Premonomorphic); ConfigureVectorState(receiver_map()); TraceIC("StoreIC", lookup->name()); return; } MaybeObjectHandle handler; if (LookupForWrite(lookup, value, store_mode)) { if (IsStoreGlobalIC()) { if (lookup->state() == LookupIterator::DATA && lookup->GetReceiver().is_identical_to(lookup->GetHolder())) { DCHECK(lookup->GetReceiver()->IsJSGlobalObject()); // Now update the cell in the feedback vector. nexus()->ConfigurePropertyCellMode(lookup->GetPropertyCell()); TraceIC("StoreGlobalIC", lookup->name()); return; } } handler = ComputeHandler(lookup); } else { set_slow_stub_reason("LookupForWrite said 'false'"); // TODO(marja): change slow_stub to return MaybeObjectHandle. handler = MaybeObjectHandle(slow_stub()); } PatchCache(lookup->name(), handler); TraceIC("StoreIC", lookup->name()); } MaybeObjectHandle StoreIC::ComputeHandler(LookupIterator* lookup) { switch (lookup->state()) { case LookupIterator::TRANSITION: { Handle store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { TRACE_HANDLER_STATS(isolate(), StoreIC_StoreGlobalTransitionDH); if (receiver_map()->IsJSGlobalObject()) { DCHECK(IsStoreGlobalIC()); #ifdef DEBUG Handle holder = lookup->GetHolder(); DCHECK_EQ(*lookup->GetReceiver(), *holder); DCHECK_EQ(*store_target, *holder); #endif return StoreHandler::StoreGlobal(lookup->transition_cell()); } Handle smi_handler = StoreHandler::StoreGlobalProxy(isolate()); Handle handler = StoreHandler::StoreThroughPrototype( isolate(), receiver_map(), store_target, smi_handler, MaybeObjectHandle::Weak(lookup->transition_cell())); return MaybeObjectHandle(handler); } // Dictionary-to-fast transitions are not expected and not supported. DCHECK_IMPLIES(!lookup->transition_map()->is_dictionary_map(), !receiver_map()->is_dictionary_map()); DCHECK(lookup->IsCacheableTransition()); return StoreHandler::StoreTransition(isolate(), lookup->transition_map()); } case LookupIterator::INTERCEPTOR: { Handle holder = lookup->GetHolder(); USE(holder); DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined(isolate())); TRACE_HANDLER_STATS(isolate(), StoreIC_StoreInterceptorStub); StoreInterceptorStub stub(isolate()); return MaybeObjectHandle(stub.GetCode()); } case LookupIterator::ACCESSOR: { // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || lookup->name()->IsPrivate()); if (!holder->HasFastProperties()) { set_slow_stub_reason("accessor on slow map"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } Handle accessors = lookup->GetAccessors(); if (accessors->IsAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == kNullAddress) { set_slow_stub_reason("setter == kNullAddress"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { set_slow_stub_reason("special data property in prototype chain"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } if (!AccessorInfo::IsCompatibleReceiverMap(info, receiver_map())) { set_slow_stub_reason("incompatible receiver type"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } Handle smi_handler = StoreHandler::StoreNativeDataProperty( isolate(), lookup->GetAccessorIndex()); TRACE_HANDLER_STATS(isolate(), StoreIC_StoreNativeDataPropertyDH); if (receiver.is_identical_to(holder)) { return MaybeObjectHandle(smi_handler); } TRACE_HANDLER_STATS(isolate(), StoreIC_StoreNativeDataPropertyOnPrototypeDH); return MaybeObjectHandle(StoreHandler::StoreThroughPrototype( isolate(), receiver_map(), holder, smi_handler)); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction() && !setter->IsFunctionTemplateInfo()) { set_slow_stub_reason("setter not a function"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } if (setter->IsFunctionTemplateInfo() && FunctionTemplateInfo::cast(*setter)->BreakAtEntry()) { // Do not install an IC if the api function has a breakpoint. TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } CallOptimization call_optimization(isolate(), setter); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { CallOptimization::HolderLookup holder_lookup; call_optimization.LookupHolderOfExpectedType(receiver_map(), &holder_lookup); Handle smi_handler = StoreHandler::StoreApiSetter( isolate(), holder_lookup == CallOptimization::kHolderIsReceiver); Handle context( call_optimization.GetAccessorContext(holder->map()), isolate()); TRACE_HANDLER_STATS(isolate(), StoreIC_StoreApiSetterOnPrototypeDH); return MaybeObjectHandle(StoreHandler::StoreThroughPrototype( isolate(), receiver_map(), holder, smi_handler, MaybeObjectHandle::Weak(call_optimization.api_call_info()), MaybeObjectHandle::Weak(context))); } set_slow_stub_reason("incompatible receiver"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } else if (setter->IsFunctionTemplateInfo()) { set_slow_stub_reason("setter non-simple template"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } Handle smi_handler = StoreHandler::StoreAccessor(isolate(), lookup->GetAccessorIndex()); TRACE_HANDLER_STATS(isolate(), StoreIC_StoreAccessorDH); if (receiver.is_identical_to(holder)) { return MaybeObjectHandle(smi_handler); } TRACE_HANDLER_STATS(isolate(), StoreIC_StoreAccessorOnPrototypeDH); return MaybeObjectHandle(StoreHandler::StoreThroughPrototype( isolate(), receiver_map(), holder, smi_handler)); } TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } case LookupIterator::DATA: { // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); USE(receiver); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || lookup->name()->IsPrivate()); DCHECK_EQ(kData, lookup->property_details().kind()); if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { TRACE_HANDLER_STATS(isolate(), StoreIC_StoreGlobalDH); return MaybeObjectHandle( StoreHandler::StoreGlobal(lookup->GetPropertyCell())); } TRACE_HANDLER_STATS(isolate(), StoreIC_StoreNormalDH); DCHECK(holder.is_identical_to(receiver)); return MaybeObjectHandle(StoreHandler::StoreNormal(isolate())); } // -------------- Fields -------------- if (lookup->property_details().location() == kField) { TRACE_HANDLER_STATS(isolate(), StoreIC_StoreFieldDH); int descriptor = lookup->GetFieldDescriptorIndex(); FieldIndex index = lookup->GetFieldIndex(); PropertyConstness constness = lookup->constness(); if (constness == PropertyConstness::kConst && IsStoreOwnICKind(nexus()->kind())) { // StoreOwnICs are used for initializing object literals therefore // we must store the value unconditionally even to // VariableMode::kConst fields. constness = PropertyConstness::kMutable; } return MaybeObjectHandle(StoreHandler::StoreField( isolate(), descriptor, index, constness, lookup->representation())); } // -------------- Constant properties -------------- DCHECK_EQ(kDescriptor, lookup->property_details().location()); set_slow_stub_reason("constant property"); TRACE_HANDLER_STATS(isolate(), StoreIC_SlowStub); return MaybeObjectHandle(slow_stub()); } case LookupIterator::JSPROXY: { Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); return MaybeObjectHandle(StoreHandler::StoreProxy( isolate(), receiver_map(), holder, receiver)); } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return MaybeObjectHandle(); } void KeyedStoreIC::UpdateStoreElement(Handle receiver_map, KeyedAccessStoreMode store_mode, bool receiver_was_cow) { MapHandles target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.empty()) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode, receiver_was_cow); Handle handler = StoreElementHandler(monomorphic_map, store_mode); return ConfigureVectorState(Handle(), monomorphic_map, handler); } for (Handle map : target_receiver_maps) { if (!map.is_null() && map->instance_type() == JS_VALUE_TYPE) { DCHECK(!IsStoreInArrayLiteralICKind(kind())); set_slow_stub_reason("JSValue"); return; } } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode; old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode, receiver_was_cow); Handle handler = StoreElementHandler(transitioned_receiver_map, store_mode); ConfigureVectorState(Handle(), transitioned_receiver_map, handler); return; } if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION_HANDLE_COW || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = StoreElementHandler(receiver_map, store_mode); return ConfigureVectorState(Handle(), receiver_map, handler); } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. set_slow_stub_reason("same map added twice"); return; } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.size() > kMaxKeyedPolymorphism) return; // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode, receiver_was_cow); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { set_slow_stub_reason("store mode mismatch"); return; } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { size_t external_arrays = 0; for (Handle map : target_receiver_maps) { if (map->has_fixed_typed_array_elements()) { DCHECK(!IsStoreInArrayLiteralICKind(kind())); external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.size()) { DCHECK(!IsStoreInArrayLiteralICKind(kind())); set_slow_stub_reason( "unsupported combination of external and normal arrays"); return; } } MaybeObjectHandles handlers; handlers.reserve(target_receiver_maps.size()); StoreElementPolymorphicHandlers(&target_receiver_maps, &handlers, store_mode); if (target_receiver_maps.size() == 0) { // Transition to PREMONOMORPHIC state here and remember a weak-reference // to the {receiver_map} in case TurboFan sees this function before the // IC can transition further. ConfigureVectorState(receiver_map); } else if (target_receiver_maps.size() == 1) { ConfigureVectorState(Handle(), target_receiver_maps[0], handlers[0]); } else { ConfigureVectorState(Handle(), target_receiver_maps, &handlers); } } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsHoleyElementsKind(map->elements_kind()) ? HOLEY_ELEMENTS : PACKED_ELEMENTS; return Map::TransitionElementsTo(isolate(), map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsHoleyElementsKind(map->elements_kind()) ? HOLEY_DOUBLE_ELEMENTS : PACKED_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(isolate(), map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); V8_FALLTHROUGH; case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION_HANDLE_COW: return map; } UNREACHABLE(); } Handle KeyedStoreIC::StoreElementHandler( Handle receiver_map, KeyedAccessStoreMode store_mode) { DCHECK(store_mode == STANDARD_STORE || store_mode == STORE_AND_GROW_NO_TRANSITION_HANDLE_COW || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW); DCHECK_IMPLIES( receiver_map->DictionaryElementsInPrototypeChainOnly(isolate()), IsStoreInArrayLiteralICKind(kind())); if (receiver_map->IsJSProxyMap()) { return StoreHandler::StoreProxy(isolate()); } // TODO(ishell): move to StoreHandler::StoreElement(). ElementsKind elements_kind = receiver_map->elements_kind(); bool is_jsarray = receiver_map->instance_type() == JS_ARRAY_TYPE; Handle stub; if (receiver_map->has_sloppy_arguments_elements()) { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_KeyedStoreSloppyArgumentsStub); stub = KeyedStoreSloppyArgumentsStub(isolate(), store_mode).GetCode(); } else if (receiver_map->has_fast_elements() || receiver_map->has_fixed_typed_array_elements()) { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_StoreFastElementStub); stub = StoreFastElementStub(isolate(), is_jsarray, elements_kind, store_mode) .GetCode(); if (receiver_map->has_fixed_typed_array_elements()) return stub; } else if (IsStoreInArrayLiteralICKind(kind())) { TRACE_HANDLER_STATS(isolate(), StoreInArrayLiteralIC_SlowStub); stub = StoreInArrayLiteralSlowStub(isolate(), store_mode).GetCode(); } else { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_StoreElementStub); DCHECK_EQ(DICTIONARY_ELEMENTS, elements_kind); stub = StoreSlowElementStub(isolate(), store_mode).GetCode(); } if (IsStoreInArrayLiteralICKind(kind())) return stub; Handle validity_cell = Map::GetOrCreatePrototypeChainValidityCell(receiver_map, isolate()); if (validity_cell->IsSmi()) { // There's no prototype validity cell to check, so we can just use the stub. return stub; } Handle handler = isolate()->factory()->NewStoreHandler(0); handler->set_validity_cell(*validity_cell); handler->set_smi_handler(*stub); return handler; } void KeyedStoreIC::StoreElementPolymorphicHandlers( MapHandles* receiver_maps, MaybeObjectHandles* handlers, KeyedAccessStoreMode store_mode) { DCHECK(store_mode == STANDARD_STORE || store_mode == STORE_AND_GROW_NO_TRANSITION_HANDLE_COW || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW); // Filter out deprecated maps to ensure their instances get migrated. receiver_maps->erase( std::remove_if( receiver_maps->begin(), receiver_maps->end(), [](const Handle& map) { return map->is_deprecated(); }), receiver_maps->end()); for (Handle receiver_map : *receiver_maps) { Handle handler; Handle transition; if (receiver_map->instance_type() < FIRST_JS_RECEIVER_TYPE || receiver_map->DictionaryElementsInPrototypeChainOnly(isolate())) { // TODO(mvstanton): Consider embedding store_mode in the state of the slow // keyed store ic for uniformity. TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_SlowStub); handler = slow_stub(); } else { { Map* tmap = receiver_map->FindElementsKindTransitionedMap( isolate(), *receiver_maps); if (tmap != nullptr) { if (receiver_map->is_stable()) { receiver_map->NotifyLeafMapLayoutChange(isolate()); } transition = handle(tmap, isolate()); } } // TODO(mvstanton): The code below is doing pessimistic elements // transitions. I would like to stop doing that and rely on Allocation // Site Tracking to do a better job of ensuring the data types are what // they need to be. Not all the elements are in place yet, pessimistic // elements transitions are still important for performance. if (!transition.is_null()) { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_ElementsTransitionAndStoreStub); handler = StoreHandler::StoreElementTransition(isolate(), receiver_map, transition, store_mode); } else { handler = StoreElementHandler(receiver_map, store_mode); } } DCHECK(!handler.is_null()); handlers->push_back(MaybeObjectHandle(handler)); } } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION_HANDLE_COW; } else { // Handle only in-bounds elements accesses. if (receiver->HasSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } return receiver->elements()->IsCowArray() ? STORE_NO_TRANSITION_HANDLE_COW : STANDARD_STORE; } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (vector_needs_update()) { if (ConfigureVectorState(MEGAMORPHIC, key)) { set_slow_stub_reason("unhandled internalized string key"); TraceIC("StoreIC", key); } } return store_handle; } JSObject::MakePrototypesFast(object, kStartAtPrototype, isolate()); bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy(); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain(isolate())) { set_slow_stub_reason("map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool is_arguments = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSReceiver()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); is_arguments = receiver->IsJSArgumentsObject(); bool is_proxy = receiver->IsJSProxy(); // For JSTypedArray {object}s we can handle negative indices as OOB // accesses, since integer indexed properties are never looked up // on the prototype chain. For this we simply map the negative {key}s // to the [2**31,2**32-1] range, which is safe since JSTypedArray::length // is always an unsigned Smi. key_is_valid_index = key->IsSmi() && (Smi::ToInt(*key) >= 0 || object->IsJSTypedArray()); if (!is_arguments && !is_proxy) { if (key_is_valid_index) { uint32_t index = static_cast(Smi::ToInt(*key)); Handle receiver_object = Handle::cast(object); store_mode = GetStoreMode(receiver_object, index, value); } } } DCHECK(store_handle.is_null()); bool receiver_was_cow = object->IsJSArray() && Handle::cast(object)->elements()->IsCowArray(); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (is_arguments) { set_slow_stub_reason("arguments receiver"); } else if (key_is_valid_index) { if (old_receiver_map->is_abandoned_prototype_map()) { set_slow_stub_reason("receiver with prototype map"); } else if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly( isolate())) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. UpdateStoreElement(old_receiver_map, store_mode, receiver_was_cow); } else { set_slow_stub_reason("dictionary or proxy prototype"); } } else { set_slow_stub_reason("non-smi-like key"); } } else { set_slow_stub_reason("non-JSObject receiver"); } } if (vector_needs_update()) { ConfigureVectorState(MEGAMORPHIC, key); } TraceIC("StoreIC", key); return store_handle; } namespace { void StoreOwnElement(Isolate* isolate, Handle array, Handle index, Handle value) { DCHECK(index->IsNumber()); bool success = false; LookupIterator it = LookupIterator::PropertyOrElement( isolate, array, index, &success, LookupIterator::OWN); DCHECK(success); CHECK(JSObject::DefineOwnPropertyIgnoreAttributes(&it, value, NONE, kThrowOnError) .FromJust()); } } // namespace void StoreInArrayLiteralIC::Store(Handle array, Handle index, Handle value) { DCHECK(!array->map()->IsMapInArrayPrototypeChain(isolate())); DCHECK(index->IsNumber()); if (!FLAG_use_ic || MigrateDeprecated(array)) { StoreOwnElement(isolate(), array, index, value); TraceIC("StoreInArrayLiteralIC", index); return; } // TODO(neis): Convert HeapNumber to Smi if possible? KeyedAccessStoreMode store_mode = STANDARD_STORE; if (index->IsSmi()) { DCHECK_GE(Smi::ToInt(*index), 0); uint32_t index32 = static_cast(Smi::ToInt(*index)); store_mode = GetStoreMode(array, index32, value); } Handle old_array_map(array->map(), isolate()); bool array_was_cow = array->elements()->IsCowArray(); StoreOwnElement(isolate(), array, index, value); if (index->IsSmi()) { DCHECK(!old_array_map->is_abandoned_prototype_map()); UpdateStoreElement(old_array_map, store_mode, array_was_cow); } else { set_slow_stub_reason("index out of Smi range"); } if (vector_needs_update()) { ConfigureVectorState(MEGAMORPHIC, index); } TraceIC("StoreInArrayLiteralIC", index); } // ---------------------------------------------------------------------------- // Static IC stub generators. // RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { HandleScope scope(isolate); DCHECK_EQ(4, args.length()); // Runtime functions don't follow the IC's calling convention. Handle receiver = args.at(0); Handle key = args.at(1); Handle slot = args.at(2); Handle vector = args.at(3); FeedbackSlot vector_slot = FeedbackVector::ToSlot(slot->value()); // A monomorphic or polymorphic KeyedLoadIC with a string key can call the // LoadIC miss handler if the handler misses. Since the vector Nexus is // set up outside the IC, handle that here. FeedbackSlotKind kind = vector->GetKind(vector_slot); if (IsLoadICKind(kind)) { LoadIC ic(isolate, vector, vector_slot); ic.UpdateState(receiver, key); RETURN_RESULT_OR_FAILURE(isolate, ic.Load(receiver, key)); } else if (IsLoadGlobalICKind(kind)) { DCHECK_EQ(isolate->native_context()->global_proxy(), *receiver); receiver = isolate->global_object(); LoadGlobalIC ic(isolate, vector, vector_slot); ic.UpdateState(receiver, key); RETURN_RESULT_OR_FAILURE(isolate, ic.Load(key)); } else { DCHECK(IsKeyedLoadICKind(kind)); KeyedLoadIC ic(isolate, vector, vector_slot); ic.UpdateState(receiver, key); RETURN_RESULT_OR_FAILURE(isolate, ic.Load(receiver, key)); } } RUNTIME_FUNCTION(Runtime_LoadGlobalIC_Miss) { HandleScope scope(isolate); DCHECK_EQ(3, args.length()); // Runtime functions don't follow the IC's calling convention. Handle global = isolate->global_object(); Handle name = args.at(0); Handle slot = args.at(1); Handle vector = args.at(2); FeedbackSlot vector_slot = FeedbackVector::ToSlot(slot->value()); LoadGlobalIC ic(isolate, vector, vector_slot); ic.UpdateState(global, name); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(name)); return *result; } RUNTIME_FUNCTION(Runtime_LoadGlobalIC_Slow) { HandleScope scope(isolate); DCHECK_EQ(3, args.length()); CONVERT_ARG_HANDLE_CHECKED(String, name, 0); Handle native_context = isolate->native_context(); Handle script_contexts( native_context->script_context_table(), isolate); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(isolate, script_contexts, name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( isolate, script_contexts, lookup_result.context_index); Handle result = FixedArray::get(*script_context, lookup_result.slot_index, isolate); if (*result == ReadOnlyRoots(isolate).the_hole_value()) { THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewReferenceError(MessageTemplate::kNotDefined, name)); } return *result; } Handle global(native_context->global_object(), isolate); Handle result; bool is_found = false; ASSIGN_RETURN_FAILURE_ON_EXCEPTION( isolate, result, Runtime::GetObjectProperty(isolate, global, name, &is_found)); if (!is_found) { Handle
stub; if (receiver_map->has_sloppy_arguments_elements()) { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_KeyedStoreSloppyArgumentsStub); stub = KeyedStoreSloppyArgumentsStub(isolate(), store_mode).GetCode(); } else if (receiver_map->has_fast_elements() || receiver_map->has_fixed_typed_array_elements()) { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_StoreFastElementStub); stub = StoreFastElementStub(isolate(), is_jsarray, elements_kind, store_mode) .GetCode(); if (receiver_map->has_fixed_typed_array_elements()) return stub; } else if (IsStoreInArrayLiteralICKind(kind())) { TRACE_HANDLER_STATS(isolate(), StoreInArrayLiteralIC_SlowStub); stub = StoreInArrayLiteralSlowStub(isolate(), store_mode).GetCode(); } else { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_StoreElementStub); DCHECK_EQ(DICTIONARY_ELEMENTS, elements_kind); stub = StoreSlowElementStub(isolate(), store_mode).GetCode(); } if (IsStoreInArrayLiteralICKind(kind())) return stub; Handle validity_cell = Map::GetOrCreatePrototypeChainValidityCell(receiver_map, isolate()); if (validity_cell->IsSmi()) { // There's no prototype validity cell to check, so we can just use the stub. return stub; } Handle handler = isolate()->factory()->NewStoreHandler(0); handler->set_validity_cell(*validity_cell); handler->set_smi_handler(*stub); return handler; } void KeyedStoreIC::StoreElementPolymorphicHandlers( MapHandles* receiver_maps, MaybeObjectHandles* handlers, KeyedAccessStoreMode store_mode) { DCHECK(store_mode == STANDARD_STORE || store_mode == STORE_AND_GROW_NO_TRANSITION_HANDLE_COW || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW); // Filter out deprecated maps to ensure their instances get migrated. receiver_maps->erase( std::remove_if( receiver_maps->begin(), receiver_maps->end(), [](const Handle& map) { return map->is_deprecated(); }), receiver_maps->end()); for (Handle receiver_map : *receiver_maps) { Handle handler; Handle transition; if (receiver_map->instance_type() < FIRST_JS_RECEIVER_TYPE || receiver_map->DictionaryElementsInPrototypeChainOnly(isolate())) { // TODO(mvstanton): Consider embedding store_mode in the state of the slow // keyed store ic for uniformity. TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_SlowStub); handler = slow_stub(); } else { { Map* tmap = receiver_map->FindElementsKindTransitionedMap( isolate(), *receiver_maps); if (tmap != nullptr) { if (receiver_map->is_stable()) { receiver_map->NotifyLeafMapLayoutChange(isolate()); } transition = handle(tmap, isolate()); } } // TODO(mvstanton): The code below is doing pessimistic elements // transitions. I would like to stop doing that and rely on Allocation // Site Tracking to do a better job of ensuring the data types are what // they need to be. Not all the elements are in place yet, pessimistic // elements transitions are still important for performance. if (!transition.is_null()) { TRACE_HANDLER_STATS(isolate(), KeyedStoreIC_ElementsTransitionAndStoreStub); handler = StoreHandler::StoreElementTransition(isolate(), receiver_map, transition, store_mode); } else { handler = StoreElementHandler(receiver_map, store_mode); } } DCHECK(!handler.is_null()); handlers->push_back(MaybeObjectHandle(handler)); } } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION_HANDLE_COW; } else { // Handle only in-bounds elements accesses. if (receiver->HasSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } return receiver->elements()->IsCowArray() ? STORE_NO_TRANSITION_HANDLE_COW : STANDARD_STORE; } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (vector_needs_update()) { if (ConfigureVectorState(MEGAMORPHIC, key)) { set_slow_stub_reason("unhandled internalized string key"); TraceIC("StoreIC", key); } } return store_handle; } JSObject::MakePrototypesFast(object, kStartAtPrototype, isolate()); bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy(); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain(isolate())) { set_slow_stub_reason("map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool is_arguments = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSReceiver()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); is_arguments = receiver->IsJSArgumentsObject(); bool is_proxy = receiver->IsJSProxy(); // For JSTypedArray {object}s we can handle negative indices as OOB // accesses, since integer indexed properties are never looked up // on the prototype chain. For this we simply map the negative {key}s // to the [2**31,2**32-1] range, which is safe since JSTypedArray::length // is always an unsigned Smi. key_is_valid_index = key->IsSmi() && (Smi::ToInt(*key) >= 0 || object->IsJSTypedArray()); if (!is_arguments && !is_proxy) { if (key_is_valid_index) { uint32_t index = static_cast(Smi::ToInt(*key)); Handle receiver_object = Handle::cast(object); store_mode = GetStoreMode(receiver_object, index, value); } } } DCHECK(store_handle.is_null()); bool receiver_was_cow = object->IsJSArray() && Handle::cast(object)->elements()->IsCowArray(); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (is_arguments) { set_slow_stub_reason("arguments receiver"); } else if (key_is_valid_index) { if (old_receiver_map->is_abandoned_prototype_map()) { set_slow_stub_reason("receiver with prototype map"); } else if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly( isolate())) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. UpdateStoreElement(old_receiver_map, store_mode, receiver_was_cow); } else { set_slow_stub_reason("dictionary or proxy prototype"); } } else { set_slow_stub_reason("non-smi-like key"); } } else { set_slow_stub_reason("non-JSObject receiver"); } } if (vector_needs_update()) { ConfigureVectorState(MEGAMORPHIC, key); } TraceIC("StoreIC", key); return store_handle; } namespace { void StoreOwnElement(Isolate* isolate, Handle array, Handle index, Handle value) { DCHECK(index->IsNumber()); bool success = false; LookupIterator it = LookupIterator::PropertyOrElement( isolate, array, index, &success, LookupIterator::OWN); DCHECK(success); CHECK(JSObject::DefineOwnPropertyIgnoreAttributes(&it, value, NONE, kThrowOnError) .FromJust()); } } // namespace void StoreInArrayLiteralIC::Store(Handle array, Handle index, Handle value) { DCHECK(!array->map()->IsMapInArrayPrototypeChain(isolate())); DCHECK(index->IsNumber()); if (!FLAG_use_ic || MigrateDeprecated(array)) { StoreOwnElement(isolate(), array, index, value); TraceIC("StoreInArrayLiteralIC", index); return; } // TODO(neis): Convert HeapNumber to Smi if possible? KeyedAccessStoreMode store_mode = STANDARD_STORE; if (index->IsSmi()) { DCHECK_GE(Smi::ToInt(*index), 0); uint32_t index32 = static_cast(Smi::ToInt(*index)); store_mode = GetStoreMode(array, index32, value); } Handle old_array_map(array->map(), isolate()); bool array_was_cow = array->elements()->IsCowArray(); StoreOwnElement(isolate(), array, index, value); if (index->IsSmi()) { DCHECK(!old_array_map->is_abandoned_prototype_map()); UpdateStoreElement(old_array_map, store_mode, array_was_cow); } else { set_slow_stub_reason("index out of Smi range"); } if (vector_needs_update()) { ConfigureVectorState(MEGAMORPHIC, index); } TraceIC("StoreInArrayLiteralIC", index); } // ---------------------------------------------------------------------------- // Static IC stub generators. // RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { HandleScope scope(isolate); DCHECK_EQ(4, args.length()); // Runtime functions don't follow the IC's calling convention. Handle receiver = args.at(0); Handle key = args.at(1); Handle slot = args.at(2); Handle vector = args.at(3); FeedbackSlot vector_slot = FeedbackVector::ToSlot(slot->value()); // A monomorphic or polymorphic KeyedLoadIC with a string key can call the // LoadIC miss handler if the handler misses. Since the vector Nexus is // set up outside the IC, handle that here. FeedbackSlotKind kind = vector->GetKind(vector_slot); if (IsLoadICKind(kind)) { LoadIC ic(isolate, vector, vector_slot); ic.UpdateState(receiver, key); RETURN_RESULT_OR_FAILURE(isolate, ic.Load(receiver, key)); } else if (IsLoadGlobalICKind(kind)) { DCHECK_EQ(isolate->native_context()->global_proxy(), *receiver); receiver = isolate->global_object(); LoadGlobalIC ic(isolate, vector, vector_slot); ic.UpdateState(receiver, key); RETURN_RESULT_OR_FAILURE(isolate, ic.Load(key)); } else { DCHECK(IsKeyedLoadICKind(kind)); KeyedLoadIC ic(isolate, vector, vector_slot); ic.UpdateState(receiver, key); RETURN_RESULT_OR_FAILURE(isolate, ic.Load(receiver, key)); } } RUNTIME_FUNCTION(Runtime_LoadGlobalIC_Miss) { HandleScope scope(isolate); DCHECK_EQ(3, args.length()); // Runtime functions don't follow the IC's calling convention. Handle global = isolate->global_object(); Handle name = args.at(0); Handle slot = args.at(1); Handle vector = args.at(2); FeedbackSlot vector_slot = FeedbackVector::ToSlot(slot->value()); LoadGlobalIC ic(isolate, vector, vector_slot); ic.UpdateState(global, name); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(name)); return *result; } RUNTIME_FUNCTION(Runtime_LoadGlobalIC_Slow) { HandleScope scope(isolate); DCHECK_EQ(3, args.length()); CONVERT_ARG_HANDLE_CHECKED(String, name, 0); Handle native_context = isolate->native_context(); Handle script_contexts( native_context->script_context_table(), isolate); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(isolate, script_contexts, name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( isolate, script_contexts, lookup_result.context_index); Handle result = FixedArray::get(*script_context, lookup_result.slot_index, isolate); if (*result == ReadOnlyRoots(isolate).the_hole_value()) { THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewReferenceError(MessageTemplate::kNotDefined, name)); } return *result; } Handle global(native_context->global_object(), isolate); Handle result; bool is_found = false; ASSIGN_RETURN_FAILURE_ON_EXCEPTION( isolate, result, Runtime::GetObjectProperty(isolate, global, name, &is_found)); if (!is_found) { Handle