// 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/v8.h" #include "src/accessors.h" #include "src/api.h" #include "src/compiler.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.h" #include "src/list-inl.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_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; } template static C* FindInstanceOf(Isolate* isolate, Object* obj) { for (PrototypeIterator iter(isolate, obj, PrototypeIterator::START_AT_RECEIVER); !iter.IsAtEnd(); iter.Advance()) { if (Is(iter.GetCurrent())) return C::cast(iter.GetCurrent()); } return NULL; } 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. template bool Accessors::IsJSObjectFieldAccessor(typename T::TypeHandle type, Handle name, int* object_offset) { Isolate* isolate = name->GetIsolate(); if (type->Is(T::String())) { return CheckForName(name, isolate->factory()->length_string(), String::kLengthOffset, object_offset); } if (!type->IsClass()) return false; Handle map = type->AsClass()->Map(); switch (map->instance_type()) { case JS_ARRAY_TYPE: return CheckForName(name, isolate->factory()->length_string(), JSArray::kLengthOffset, object_offset); case JS_TYPED_ARRAY_TYPE: return 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); case JS_ARRAY_BUFFER_TYPE: return CheckForName(name, isolate->factory()->byte_length_string(), JSArrayBuffer::kByteLengthOffset, object_offset); 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; } } template bool Accessors::IsJSObjectFieldAccessor(Type* type, Handle name, int* object_offset); template bool Accessors::IsJSObjectFieldAccessor(Handle type, Handle name, int* object_offset); bool SetPropertyOnInstanceIfInherited( Isolate* isolate, const v8::PropertyCallbackInfo& info, v8::Local name, Handle value) { Handle holder = Utils::OpenHandle(*info.Holder()); Handle receiver = Utils::OpenHandle(*info.This()); if (*holder == *receiver) return false; if (receiver->IsJSObject()) { Handle object = Handle::cast(receiver); // This behaves sloppy since we lost the actual strict-mode. // TODO(verwaest): Fix by making ExecutableAccessorInfo behave like data // properties. if (!object->map()->is_extensible()) return true; JSObject::SetOwnPropertyIgnoreAttributes(object, Utils::OpenHandle(*name), value, NONE).Check(); } return 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))); } 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 = Utils::OpenHandle(*info.This()); Handle value = Utils::OpenHandle(*val); if (SetPropertyOnInstanceIfInherited(isolate, info, name, value)) return; LookupIterator it(object, Utils::OpenHandle(*name)); CHECK_EQ(LookupIterator::ACCESSOR, it.state()); DCHECK(it.HolderIsReceiverOrHiddenPrototype()); Object::SetDataProperty(&it, value); } Handle Accessors::ArgumentsIteratorInfo( Isolate* isolate, PropertyAttributes attributes) { Handle name(isolate->native_context()->iterator_symbol(), isolate); return MakeAccessor(isolate, name, &ArgumentsIteratorGetter, &ArgumentsIteratorSetter, attributes); } // // Accessors::ArrayLength // // The helper function will 'flatten' Number objects. Handle Accessors::FlattenNumber(Isolate* isolate, Handle value) { if (value->IsNumber() || !value->IsJSValue()) return value; Handle wrapper = Handle::cast(value); DCHECK(wrapper->GetIsolate()->native_context()->number_function()-> has_initial_map()); if (wrapper->map() == isolate->number_function()->initial_map()) { return handle(wrapper->value(), isolate); } return value; } 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 value = Utils::OpenHandle(*val); if (SetPropertyOnInstanceIfInherited(isolate, info, name, value)) { return; } value = FlattenNumber(isolate, value); Handle array_handle = Handle::cast(object); MaybeHandle maybe; Handle uint32_v; maybe = Execution::ToUint32(isolate, value); if (!maybe.ToHandle(&uint32_v)) { isolate->OptionalRescheduleException(false); return; } Handle number_v; maybe = Execution::ToNumber(isolate, value); if (!maybe.ToHandle(&number_v)) { isolate->OptionalRescheduleException(false); return; } if (uint32_v->Number() == number_v->Number()) { maybe = JSArray::SetElementsLength(array_handle, uint32_v); maybe.Check(); return; } Handle exception; maybe = isolate->factory()->NewRangeError("invalid_array_length", HandleVector(NULL, 0)); if (!maybe.ToHandle(&exception)) { isolate->OptionalRescheduleException(false); return; } isolate->ScheduleThrow(*exception); } 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 = 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 = 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 = 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 = 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