// 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->NewExecutableAccessorInfo(); 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); Handle set = v8::FromCData(isolate, setter); info->set_getter(*get); info->set_setter(*set); return info; } Handle Accessors::CloneAccessor( Isolate* isolate, Handle accessor) { Factory* factory = isolate->factory(); Handle info = factory->NewExecutableAccessorInfo(); info->set_name(accessor->name()); info->set_flag(accessor->flag()); info->set_expected_receiver_type(accessor->expected_receiver_type()); info->set_getter(accessor->getter()); info->set_setter(accessor->setter()); info->set_data(accessor->data()); 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; } } // // 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))); } void Accessors::ArgumentsIteratorSetter( v8::Local name, v8::Local val, const v8::PropertyCallbackInfo& info) { i::Isolate* isolate = reinterpret_cast(info.GetIsolate()); HandleScope scope(isolate); Handle object_handle = Handle::cast(Utils::OpenHandle(*info.This())); Handle value_handle = Utils::OpenHandle(*val); Handle name_handle = Utils::OpenHandle(*name); if (JSObject::DefinePropertyOrElementIgnoreAttributes( object_handle, name_handle, value_handle, NONE) .is_null()) { isolate->OptionalRescheduleException(false); } } Handle Accessors::ArgumentsIteratorInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name = isolate->factory()->iterator_symbol(); return MakeAccessor(isolate, name, &ArgumentsIteratorGetter, &ArgumentsIteratorSetter, 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); } } 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))); } void Accessors::StringLengthSetter( v8::Local name, v8::Local value, const v8::PropertyCallbackInfo& info) { UNREACHABLE(); } Handle Accessors::StringLengthInfo( Isolate* isolate, PropertyAttributes attributes) { return MakeAccessor(isolate, isolate->factory()->length_string(), &StringLengthGetter, &StringLengthSetter, 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))); } void Accessors::ScriptColumnOffsetSetter( v8::Local name, v8::Local value, const v8::PropertyCallbackInfo& info) { UNREACHABLE(); } Handle Accessors::ScriptColumnOffsetInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name(isolate->factory()->InternalizeOneByteString( STATIC_CHAR_VECTOR("column_offset"))); return MakeAccessor(isolate, name, &ScriptColumnOffsetGetter, &ScriptColumnOffsetSetter, 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))); } void Accessors::ScriptIdSetter( v8::Local name, v8::Local value, const v8::PropertyCallbackInfo& info) { UNREACHABLE(); } Handle Accessors::ScriptIdInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name( isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("id"))); return MakeAccessor(isolate, name, &ScriptIdGetter, &ScriptIdSetter, 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))); } void Accessors::ScriptNameSetter( v8::Local name, v8::Local value, const v8::PropertyCallbackInfo& info) { UNREACHABLE(); } Handle Accessors::ScriptNameInfo( Isolate* isolate, PropertyAttributes attributes) { return MakeAccessor(isolate, isolate->factory()->name_string(), &ScriptNameGetter, &ScriptNameSetter, 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))); } void Accessors::ScriptSourceSetter( v8::Local name, v8::Local value, const v8::PropertyCallbackInfo& info) { UNREACHABLE(); } Handle Accessors::ScriptSourceInfo( Isolate* isolate, PropertyAttributes attributes) { return MakeAccessor(isolate, isolate->factory()->source_string(), &ScriptSourceGetter, &ScriptSourceSetter, 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))); } void Accessors::ScriptLineOffsetSetter( v8::Local name, v8::Local value, const v8::PropertyCallbackInfo& info) { UNREACHABLE(); } Handle Accessors::ScriptLineOffsetInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name(isolate->factory()->InternalizeOneByteString( STATIC_CHAR_VECTOR("line_offset"))); return MakeAccessor(isolate, name, &ScriptLineOffsetGetter, &ScriptLineOffsetSetter, 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))); } void Accessors::ScriptTypeSetter( v8::Local name, v8::Local value, const v8::PropertyCallbackInfo& info) { UNREACHABLE(); } Handle Accessors::ScriptTypeInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name( isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("type"))); return MakeAccessor(isolate, name, &ScriptTypeGetter, &ScriptTypeSetter, 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))); } void Accessors::ScriptCompilationTypeSetter( v8::Local name, v8::Local value, const v8::PropertyCallbackInfo& info) { UNREACHABLE(); } Handle Accessors::ScriptCompilationTypeInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name(isolate->factory()->InternalizeOneByteString( STATIC_CHAR_VECTOR("compilation_type"))); return MakeAccessor(isolate, name, &ScriptCompilationTypeGetter, &ScriptCompilationTypeSetter, 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