HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Lollipop
|
5.0.1_r1
下载
查看原文件
收藏
根目录
external
chromium_org
v8
src
accessors.cc
// 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/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/api.h" namespace v8 { namespace internal { // 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 even have callbacks on entities which are // higher in the hierarchy, so we can only return i::Object here, not // i::JSObject. Handle
GetThisFrom(const v8::PropertyCallbackInfo
& info) { return Utils::OpenHandle(*v8::Local
(info.This())); } Handle
Accessors::MakeAccessor( Isolate* isolate, Handle
name, AccessorGetterCallback getter, AccessorSetterCallback 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 (Object* cur = obj; !cur->IsNull(); cur = cur->GetPrototype(isolate)) { if (Is
(cur)) return C::cast(cur); } return NULL; } static V8_INLINE bool CheckForName(Handle
name, Handle
property_name, int offset, int* object_offset) { if (String::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); // // 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); ASSERT(wrapper->GetIsolate()->context()->native_context()->number_function()-> has_initial_map()); if (wrapper->map() == isolate->context()->native_context()->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); Object* object = *GetThisFrom(info); // Traverse the prototype chain until we reach an array. JSArray* holder = FindInstanceOf
(isolate, object); Object* result; if (holder != NULL) { result = holder->length(); } else { result = Smi::FromInt(0); } 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 = Handle
::cast( Utils::OpenHandle(*info.This())); Handle
value = Utils::OpenHandle(*val); // This means one of the object's prototypes is a JSArray and the // object does not have a 'length' property. Calling SetProperty // causes an infinite loop. if (!object->IsJSArray()) { MaybeHandle
maybe_result = JSObject::SetOwnPropertyIgnoreAttributes( object, isolate->factory()->length_string(), value, NONE); maybe_result.Check(); 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; } isolate->ScheduleThrow( *isolate->factory()->NewRangeError("invalid_array_length", HandleVector
(NULL, 0))); } 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); Object* value = *GetThisFrom(info); Object* result; if (value->IsJSValue()) value = JSValue::cast(value)->value(); if (value->IsString()) { result = Smi::FromInt(String::cast(value)->length()); } else { // If object is not a string we return 0 to be compatible with WebKit. // Note: Firefox returns the length of ToString(object). result = Smi::FromInt(0); } 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_ASCII_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_ASCII_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_ASCII_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_ASCII_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_ASCII_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
登录后可以享受更多权益
您还没有登录,登录后您可以:
收藏Android系统代码
收藏喜欢的文章
多个平台共享账号
去登录
首次使用?从这里
注册