// Copyright 2012 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "src/accessors.h" #include "src/api.h" #include "src/contexts.h" #include "src/deoptimizer.h" #include "src/execution.h" #include "src/factory.h" #include "src/frames-inl.h" #include "src/isolate-inl.h" #include "src/list-inl.h" #include "src/messages.h" #include "src/property-details.h" #include "src/prototype.h" namespace v8 { namespace internal { Handle Accessors::MakeAccessor( Isolate* isolate, Handle name, AccessorNameGetterCallback getter, AccessorNameBooleanSetterCallback setter, PropertyAttributes attributes) { Factory* factory = isolate->factory(); Handle info = factory->NewAccessorInfo(); info->set_property_attributes(attributes); info->set_all_can_read(false); info->set_all_can_write(false); info->set_is_special_data_property(true); info->set_is_sloppy(false); info->set_replace_on_access(false); name = factory->InternalizeName(name); info->set_name(*name); Handle get = v8::FromCData(isolate, getter); if (setter == nullptr) setter = &ReconfigureToDataProperty; Handle set = v8::FromCData(isolate, setter); info->set_getter(*get); info->set_setter(*set); Address redirected = info->redirected_getter(); if (redirected != nullptr) { Handle js_get = v8::FromCData(isolate, redirected); info->set_js_getter(*js_get); } return info; } static V8_INLINE bool CheckForName(Handle name, Handle property_name, int offset, int* object_offset) { if (Name::Equals(name, property_name)) { *object_offset = offset; return true; } return false; } // Returns true for properties that are accessors to object fields. // If true, *object_offset contains offset of object field. bool Accessors::IsJSObjectFieldAccessor(Handle map, Handle name, int* object_offset) { Isolate* isolate = name->GetIsolate(); switch (map->instance_type()) { case JS_ARRAY_TYPE: return CheckForName(name, isolate->factory()->length_string(), JSArray::kLengthOffset, object_offset); default: if (map->instance_type() < FIRST_NONSTRING_TYPE) { return CheckForName(name, isolate->factory()->length_string(), String::kLengthOffset, object_offset); } return false; } } namespace { MUST_USE_RESULT MaybeHandle ReplaceAccessorWithDataProperty( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle value) { LookupIterator it(receiver, name, holder, LookupIterator::OWN_SKIP_INTERCEPTOR); // Skip any access checks we might hit. This accessor should never hit in a // situation where the caller does not have access. if (it.state() == LookupIterator::ACCESS_CHECK) { CHECK(it.HasAccess()); it.Next(); } DCHECK(holder.is_identical_to(it.GetHolder())); CHECK_EQ(LookupIterator::ACCESSOR, it.state()); it.ReconfigureDataProperty(value, it.property_attributes()); return value; } } // namespace void Accessors::ReconfigureToDataProperty( v8::Local key, v8::Local val, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); HandleScope scope(isolate); Handle receiver = Utils::OpenHandle(*info.This()); Handle holder = Handle::cast(Utils::OpenHandle(*info.Holder())); Handle name = Utils::OpenHandle(*key); Handle value = Utils::OpenHandle(*val); MaybeHandle result = ReplaceAccessorWithDataProperty(isolate, receiver, holder, name, value); if (result.is_null()) { isolate->OptionalRescheduleException(false); } else { info.GetReturnValue().Set(true); } } // // Accessors::ArgumentsIterator // void Accessors::ArgumentsIteratorGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* result = isolate->native_context()->array_values_iterator(); info.GetReturnValue().Set(Utils::ToLocal(Handle(result, isolate))); } Handle Accessors::ArgumentsIteratorInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name = isolate->factory()->iterator_symbol(); return MakeAccessor(isolate, name, &ArgumentsIteratorGetter, nullptr, attributes); } // // Accessors::ArrayLength // void Accessors::ArrayLengthGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); RuntimeCallTimerScope timer( isolate, &RuntimeCallStats::AccessorNameGetterCallback_ArrayLength); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); JSArray* holder = JSArray::cast(*Utils::OpenHandle(*info.Holder())); Object* result = holder->length(); info.GetReturnValue().Set(Utils::ToLocal(Handle(result, isolate))); } void Accessors::ArrayLengthSetter( v8::Local name, v8::Local val, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); HandleScope scope(isolate); DCHECK(Utils::OpenHandle(*name)->SameValue(isolate->heap()->length_string())); Handle object = Utils::OpenHandle(*info.Holder()); Handle array = Handle::cast(object); Handle length_obj = Utils::OpenHandle(*val); bool was_readonly = JSArray::HasReadOnlyLength(array); uint32_t length = 0; if (!JSArray::AnythingToArrayLength(isolate, length_obj, &length)) { isolate->OptionalRescheduleException(false); return; } if (!was_readonly && V8_UNLIKELY(JSArray::HasReadOnlyLength(array)) && length != array->length()->Number()) { // AnythingToArrayLength() may have called setter re-entrantly and modified // its property descriptor. Don't perform this check if "length" was // previously readonly, as this may have been called during // DefineOwnPropertyIgnoreAttributes(). if (info.ShouldThrowOnError()) { Factory* factory = isolate->factory(); isolate->Throw(*factory->NewTypeError( MessageTemplate::kStrictReadOnlyProperty, Utils::OpenHandle(*name), i::Object::TypeOf(isolate, object), object)); isolate->OptionalRescheduleException(false); } else { info.GetReturnValue().Set(false); } return; } JSArray::SetLength(array, length); uint32_t actual_new_len = 0; CHECK(array->length()->ToArrayLength(&actual_new_len)); // Fail if there were non-deletable elements. if (actual_new_len != length) { if (info.ShouldThrowOnError()) { Factory* factory = isolate->factory(); isolate->Throw(*factory->NewTypeError( MessageTemplate::kStrictDeleteProperty, factory->NewNumberFromUint(actual_new_len - 1), array)); isolate->OptionalRescheduleException(false); } else { info.GetReturnValue().Set(false); } } else { info.GetReturnValue().Set(true); } } Handle Accessors::ArrayLengthInfo( Isolate* isolate, PropertyAttributes attributes) { return MakeAccessor(isolate, isolate->factory()->length_string(), &ArrayLengthGetter, &ArrayLengthSetter, attributes); } // // Accessors::ModuleNamespaceEntry // void Accessors::ModuleNamespaceEntryGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); HandleScope scope(isolate); JSModuleNamespace* holder = JSModuleNamespace::cast(*Utils::OpenHandle(*info.Holder())); Handle result; if (!holder->GetExport(Handle::cast(Utils::OpenHandle(*name))) .ToHandle(&result)) { isolate->OptionalRescheduleException(false); } else { info.GetReturnValue().Set(Utils::ToLocal(result)); } } void Accessors::ModuleNamespaceEntrySetter( v8::Local name, v8::Local val, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); HandleScope scope(isolate); Factory* factory = isolate->factory(); Handle holder = Handle::cast(Utils::OpenHandle(*info.Holder())); if (info.ShouldThrowOnError()) { isolate->Throw(*factory->NewTypeError( MessageTemplate::kStrictReadOnlyProperty, Utils::OpenHandle(*name), i::Object::TypeOf(isolate, holder), holder)); isolate->OptionalRescheduleException(false); } else { info.GetReturnValue().Set(false); } } Handle Accessors::ModuleNamespaceEntryInfo( Isolate* isolate, Handle name, PropertyAttributes attributes) { return MakeAccessor(isolate, name, &ModuleNamespaceEntryGetter, &ModuleNamespaceEntrySetter, attributes); } // // Accessors::StringLength // void Accessors::StringLengthGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); RuntimeCallTimerScope timer( isolate, &RuntimeCallStats::AccessorNameGetterCallback_StringLength); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); // We have a slight impedance mismatch between the external API and the way we // use callbacks internally: Externally, callbacks can only be used with // v8::Object, but internally we have callbacks on entities which are higher // in the hierarchy, in this case for String values. Object* value = *Utils::OpenHandle(*v8::Local(info.This())); if (!value->IsString()) { // Not a string value. That means that we either got a String wrapper or // a Value with a String wrapper in its prototype chain. value = JSValue::cast(*Utils::OpenHandle(*info.Holder()))->value(); } Object* result = Smi::FromInt(String::cast(value)->length()); info.GetReturnValue().Set(Utils::ToLocal(Handle(result, isolate))); } Handle Accessors::StringLengthInfo( Isolate* isolate, PropertyAttributes attributes) { return MakeAccessor(isolate, isolate->factory()->length_string(), &StringLengthGetter, nullptr, attributes); } // // Accessors::ScriptColumnOffset // void Accessors::ScriptColumnOffsetGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.Holder()); Object* res = Smi::FromInt( Script::cast(JSValue::cast(object)->value())->column_offset()); info.GetReturnValue().Set(Utils::ToLocal(Handle(res, isolate))); } Handle Accessors::ScriptColumnOffsetInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name(isolate->factory()->InternalizeOneByteString( STATIC_CHAR_VECTOR("column_offset"))); return MakeAccessor(isolate, name, &ScriptColumnOffsetGetter, nullptr, attributes); } // // Accessors::ScriptId // void Accessors::ScriptIdGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.Holder()); Object* id = Smi::FromInt(Script::cast(JSValue::cast(object)->value())->id()); info.GetReturnValue().Set(Utils::ToLocal(Handle(id, isolate))); } Handle Accessors::ScriptIdInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name( isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("id"))); return MakeAccessor(isolate, name, &ScriptIdGetter, nullptr, attributes); } // // Accessors::ScriptName // void Accessors::ScriptNameGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.Holder()); Object* source = Script::cast(JSValue::cast(object)->value())->name(); info.GetReturnValue().Set(Utils::ToLocal(Handle(source, isolate))); } Handle Accessors::ScriptNameInfo( Isolate* isolate, PropertyAttributes attributes) { return MakeAccessor(isolate, isolate->factory()->name_string(), &ScriptNameGetter, nullptr, attributes); } // // Accessors::ScriptSource // void Accessors::ScriptSourceGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.Holder()); Object* source = Script::cast(JSValue::cast(object)->value())->source(); info.GetReturnValue().Set(Utils::ToLocal(Handle(source, isolate))); } Handle Accessors::ScriptSourceInfo( Isolate* isolate, PropertyAttributes attributes) { return MakeAccessor(isolate, isolate->factory()->source_string(), &ScriptSourceGetter, nullptr, attributes); } // // Accessors::ScriptLineOffset // void Accessors::ScriptLineOffsetGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.Holder()); Object* res = Smi::FromInt(Script::cast(JSValue::cast(object)->value())->line_offset()); info.GetReturnValue().Set(Utils::ToLocal(Handle(res, isolate))); } Handle Accessors::ScriptLineOffsetInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name(isolate->factory()->InternalizeOneByteString( STATIC_CHAR_VECTOR("line_offset"))); return MakeAccessor(isolate, name, &ScriptLineOffsetGetter, nullptr, attributes); } // // Accessors::ScriptType // void Accessors::ScriptTypeGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.Holder()); Object* res = Smi::FromInt(Script::cast(JSValue::cast(object)->value())->type()); info.GetReturnValue().Set(Utils::ToLocal(Handle(res, isolate))); } Handle Accessors::ScriptTypeInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name( isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("type"))); return MakeAccessor(isolate, name, &ScriptTypeGetter, nullptr, attributes); } // // Accessors::ScriptCompilationType // void Accessors::ScriptCompilationTypeGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.Holder()); Object* res = Smi::FromInt( Script::cast(JSValue::cast(object)->value())->compilation_type()); info.GetReturnValue().Set(Utils::ToLocal(Handle(res, isolate))); } Handle Accessors::ScriptCompilationTypeInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name(isolate->factory()->InternalizeOneByteString( STATIC_CHAR_VECTOR("compilation_type"))); return MakeAccessor(isolate, name, &ScriptCompilationTypeGetter, nullptr, attributes); } // // Accessors::ScriptSourceUrl // void Accessors::ScriptSourceUrlGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.Holder()); Object* url = Script::cast(JSValue::cast(object)->value())->source_url(); info.GetReturnValue().Set(Utils::ToLocal(Handle(url, isolate))); } Handle Accessors::ScriptSourceUrlInfo( Isolate* isolate, PropertyAttributes attributes) { return MakeAccessor(isolate, isolate->factory()->source_url_string(), &ScriptSourceUrlGetter, nullptr, attributes); } // // Accessors::ScriptSourceMappingUrl // void Accessors::ScriptSourceMappingUrlGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.Holder()); Object* url = Script::cast(JSValue::cast(object)->value())->source_mapping_url(); info.GetReturnValue().Set(Utils::ToLocal(Handle(url, isolate))); } Handle Accessors::ScriptSourceMappingUrlInfo( Isolate* isolate, PropertyAttributes attributes) { return MakeAccessor(isolate, isolate->factory()->source_mapping_url_string(), &ScriptSourceMappingUrlGetter, nullptr, attributes); } // // Accessors::ScriptGetContextData // void Accessors::ScriptContextDataGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); DisallowHeapAllocation no_allocation; HandleScope scope(isolate); Object* object = *Utils::OpenHandle(*info.Holder()); Object* res = Script::cast(JSValue::cast(object)->value())->context_data(); info.GetReturnValue().Set(Utils::ToLocal(Handle(res, isolate))); } Handle Accessors::ScriptContextDataInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name(isolate->factory()->InternalizeOneByteString( STATIC_CHAR_VECTOR("context_data"))); return MakeAccessor(isolate, name, &ScriptContextDataGetter, nullptr, attributes); } // // Accessors::ScriptGetEvalFromScript // void Accessors::ScriptEvalFromScriptGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); HandleScope scope(isolate); Handle object = Utils::OpenHandle(*info.Holder()); Handle