// 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, AccessorNameSetterCallback 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_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); 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); case JS_ARRAY_BUFFER_TYPE: return CheckForName(name, isolate->factory()->byte_length_string(), JSArrayBuffer::kByteLengthOffset, object_offset); default: if (map->instance_type() < FIRST_NONSTRING_TYPE) { return CheckForName(name, isolate->factory()->length_string(), String::kLengthOffset, object_offset); } return false; } } bool Accessors::IsJSArrayBufferViewFieldAccessor(Handle map, Handle name, int* object_offset) { Isolate* isolate = name->GetIsolate(); switch (map->instance_type()) { case JS_TYPED_ARRAY_TYPE: { if (!CheckForName(name, isolate->factory()->length_string(), JSTypedArray::kLengthOffset, object_offset) && !CheckForName(name, isolate->factory()->byte_length_string(), JSTypedArray::kByteLengthOffset, object_offset) && !CheckForName(name, isolate->factory()->byte_offset_string(), JSTypedArray::kByteOffsetOffset, object_offset)) { return false; } if (map->is_dictionary_map()) return false; // Check if the property is overridden on the instance. DescriptorArray* descriptors = map->instance_descriptors(); int descriptor = descriptors->SearchWithCache(*name, *map); if (descriptor != DescriptorArray::kNotFound) return false; Handle proto = Handle(map->prototype(), isolate); if (!proto->IsJSReceiver()) return false; // Check if the property is defined in the prototype chain. LookupIterator it(proto, name); if (!it.IsFound()) return false; Object* original_proto = JSFunction::cast(map->GetConstructor())->prototype(); // Property is not configurable. It is enough to verify that // the holder is the same. return *it.GetHolder() == original_proto; } case JS_DATA_VIEW_TYPE: return CheckForName(name, isolate->factory()->byte_length_string(), JSDataView::kByteLengthOffset, object_offset) || CheckForName(name, isolate->factory()->byte_offset_string(), JSDataView::kByteOffsetOffset, object_offset); default: return false; } } MUST_USE_RESULT static MaybeHandle ReplaceAccessorWithDataProperty( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle value, bool observe) { 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(); } CHECK_EQ(LookupIterator::ACCESSOR, it.state()); Handle old_value; bool is_observed = observe && receiver->map()->is_observed(); if (is_observed) { MaybeHandle maybe_old = Object::GetPropertyWithAccessor(&it, SLOPPY); if (!maybe_old.ToHandle(&old_value)) return maybe_old; } it.ReconfigureDataProperty(value, it.property_attributes()); if (is_observed && !old_value->SameValue(*value)) { return JSObject::EnqueueChangeRecord(receiver, "update", name, old_value); } return value; } 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 = Handle::cast(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, false); if (result.is_null()) isolate->OptionalRescheduleException(false); } // // 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()); 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); Handle object = Utils::OpenHandle(*info.This()); Handle array = Handle::cast(object); Handle length_obj = Utils::OpenHandle(*val); uint32_t length = 0; if (!JSArray::AnythingToArrayLength(isolate, length_obj, &length)) { isolate->OptionalRescheduleException(false); return; } if (JSArray::ObservableSetLength(array, length).is_null()) { isolate->OptionalRescheduleException(false); } if (info.ShouldThrowOnError()) { uint32_t actual_new_len = 0; CHECK(array->length()->ToArrayLength(&actual_new_len)); // Throw TypeError if there were non-deletable elements. if (actual_new_len != length) { Factory* factory = isolate->factory(); isolate->Throw(*factory->NewTypeError( MessageTemplate::kStrictDeleteProperty, factory->NewNumberFromUint(actual_new_len - 1), array)); isolate->OptionalRescheduleException(false); } } } Handle Accessors::ArrayLengthInfo( Isolate* isolate, PropertyAttributes attributes) { return MakeAccessor(isolate, isolate->factory()->length_string(), &ArrayLengthGetter, &ArrayLengthSetter, attributes); } // // Accessors::StringLength // void Accessors::StringLengthGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); 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.This()); 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.This()); 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.This()); 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.This()); 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.This()); 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.This()); 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.This()); 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::ScriptGetLineEnds // void Accessors::ScriptLineEndsGetter( v8::Local name, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); HandleScope scope(isolate); Handle object = Utils::OpenHandle(*info.This()); Handle