From ad605de2b54fcfe310354293cf22a88b21673c7b Mon Sep 17 00:00:00 2001 From: "svenpanne@chromium.org" Date: Mon, 13 Jan 2014 09:42:23 +0000 Subject: [PATCH] Various ApiCheck-related cleanups. R=mstarzinger@chromium.org Review URL: https://codereview.chromium.org/130933003 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18551 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/api.cc | 588 +++++++++++++++++++++----------------------- src/api.h | 10 +- src/bootstrapper.cc | 12 +- src/handles.cc | 6 +- 4 files changed, 297 insertions(+), 319 deletions(-) diff --git a/src/api.cc b/src/api.cc index db2045f8d..70b243ba5 100644 --- a/src/api.cc +++ b/src/api.cc @@ -121,9 +121,9 @@ namespace v8 { #define API_ENTRY_CHECK(isolate, msg) \ do { \ if (v8::Locker::IsActive()) { \ - ApiCheck(isolate->thread_manager()->IsLockedByCurrentThread(), \ - msg, \ - "Entering the V8 API without proper locking in place"); \ + Utils::ApiCheck(isolate->thread_manager()->IsLockedByCurrentThread(), \ + msg, \ + "Entering the V8 API without proper locking in place"); \ } \ } while (false) @@ -143,15 +143,6 @@ static void DefaultFatalErrorHandler(const char* location, } -static FatalErrorCallback GetFatalErrorHandler() { - i::Isolate* isolate = i::Isolate::Current(); - if (isolate->exception_behavior() == NULL) { - isolate->set_exception_behavior(DefaultFatalErrorHandler); - } - return isolate->exception_behavior(); -} - - void i::FatalProcessOutOfMemory(const char* location) { i::V8::FatalProcessOutOfMemory(location, false); } @@ -221,21 +212,19 @@ void i::V8::FatalProcessOutOfMemory(const char* location, bool take_snapshot) { // HeapIterator here without doing a special GC. isolate->heap()->RecordStats(&heap_stats, false); } - isolate->SignalFatalError(); - FatalErrorCallback callback = GetFatalErrorHandler(); - const char* message = "Allocation failed - process out of memory"; - callback(location, message); - // If the callback returns, we stop execution. + Utils::ApiCheck(false, location, "Allocation failed - process out of memory"); + // If the fatal error handler returns, we stop execution. FATAL("API fatal error handler returned after process out of memory"); } -bool Utils::ReportApiFailure(const char* location, const char* message) { - FatalErrorCallback callback = GetFatalErrorHandler(); - callback(location, message); +void Utils::ReportApiFailure(const char* location, const char* message) { i::Isolate* isolate = i::Isolate::Current(); + FatalErrorCallback callback = isolate->exception_behavior() == NULL + ? DefaultFatalErrorHandler + : isolate->exception_behavior(); + callback(location, message); isolate->SignalFatalError(); - return false; } @@ -245,20 +234,6 @@ bool V8::IsDead() { } -static inline bool ApiCheck(bool condition, - const char* location, - const char* message) { - return condition ? true : Utils::ReportApiFailure(location, message); -} - - -static bool ReportEmptyHandle(const char* location) { - FatalErrorCallback callback = GetFatalErrorHandler(); - callback(location, "Reading from empty handle"); - return true; -} - - static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) { if (!isolate->IsInitialized()) return false; if (isolate->has_scheduled_exception()) { @@ -269,16 +244,6 @@ static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) { } -static inline bool EmptyCheck(const char* location, v8::Handle obj) { - return obj.IsEmpty() ? ReportEmptyHandle(location) : false; -} - - -static inline bool EmptyCheck(const char* location, const v8::Data* obj) { - return (obj == 0) ? ReportEmptyHandle(location) : false; -} - - // --- S t a t i c s --- @@ -295,11 +260,10 @@ static bool InitializeHelper(i::Isolate* isolate) { static inline bool EnsureInitializedForIsolate(i::Isolate* isolate, const char* location) { - if (isolate != NULL) { - if (isolate->IsInitialized()) return true; - } - ASSERT(isolate == i::Isolate::Current()); - return ApiCheck(InitializeHelper(isolate), location, "Error initializing V8"); + return (isolate != NULL && isolate->IsInitialized()) || + Utils::ApiCheck(InitializeHelper(isolate), + location, + "Error initializing V8"); } @@ -519,11 +483,11 @@ Extension::Extension(const char* name, ResourceConstraints::ResourceConstraints() - : max_young_space_size_(0), - max_old_space_size_(0), - max_executable_size_(0), - stack_limit_(NULL), - max_available_threads_(0) { } + : max_young_space_size_(0), + max_old_space_size_(0), + max_executable_size_(0), + stack_limit_(NULL), + max_available_threads_(0) { } void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory, uint32_t number_of_processors) { @@ -688,9 +652,9 @@ EscapableHandleScope::EscapableHandleScope(Isolate* v8_isolate) { i::Object** EscapableHandleScope::Escape(i::Object** escape_value) { - ApiCheck(*escape_slot_ == isolate_->heap()->the_hole_value(), - "EscapeableHandleScope::Escape", - "Escape value set twice"); + Utils::ApiCheck(*escape_slot_ == isolate_->heap()->the_hole_value(), + "EscapeableHandleScope::Escape", + "Escape value set twice"); if (escape_value == NULL) { *escape_slot_ = isolate_->heap()->undefined_value(); return NULL; @@ -714,27 +678,25 @@ void Context::Exit() { i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); i::Handle context = i::Handle::null(); ENTER_V8(isolate); - if (!ApiCheck(isolate->handle_scope_implementer()->LeaveContext(context), - "v8::Context::Exit()", - "Cannot exit non-entered context")) { + i::HandleScopeImplementer* impl = isolate->handle_scope_implementer(); + if (!Utils::ApiCheck(impl->LeaveContext(context), + "v8::Context::Exit()", + "Cannot exit non-entered context")) { return; } - // Content of 'last_context' could be NULL. - i::Context* last_context = - isolate->handle_scope_implementer()->RestoreContext(); - isolate->set_context(last_context); + isolate->set_context(impl->RestoreContext()); } static void* DecodeSmiToAligned(i::Object* value, const char* location) { - ApiCheck(value->IsSmi(), location, "Not a Smi"); + Utils::ApiCheck(value->IsSmi(), location, "Not a Smi"); return reinterpret_cast(value); } static i::Smi* EncodeAlignedAsSmi(void* value, const char* location) { i::Smi* smi = reinterpret_cast(value); - ApiCheck(smi->IsSmi(), location, "Pointer is not aligned"); + Utils::ApiCheck(smi->IsSmi(), location, "Pointer is not aligned"); return smi; } @@ -745,13 +707,14 @@ static i::Handle EmbedderDataFor(Context* context, const char* location) { i::Handle env = Utils::OpenHandle(context); bool ok = - ApiCheck(env->IsNativeContext(), location, "Not a native context") && - ApiCheck(index >= 0, location, "Negative index"); + Utils::ApiCheck(env->IsNativeContext(), + location, + "Not a native context") && + Utils::ApiCheck(index >= 0, location, "Negative index"); if (!ok) return i::Handle(); i::Handle data(env->embedder_data()); if (index < data->length()) return data; - if (!can_grow) { - Utils::ReportApiFailure(location, "Index too large"); + if (!Utils::ApiCheck(can_grow, location, "Index too large")) { return i::Handle(); } int new_size = i::Max(index, data->length() << 1) + 1; @@ -899,9 +862,9 @@ void Template::Set(v8::Handle name, const int kSize = 3; v8::Isolate* v8_isolate = reinterpret_cast(isolate); v8::Handle data[kSize] = { - name, - value, - v8::Integer::New(v8_isolate, attribute)}; + name, + value, + v8::Integer::New(v8_isolate, attribute)}; TemplateSet(isolate, this, kSize, data); } @@ -920,18 +883,18 @@ void Template::SetAccessorProperty( const int kSize = 5; v8::Isolate* v8_isolate = reinterpret_cast(isolate); v8::Handle data[kSize] = { - name, - getter, - setter, - v8::Integer::New(v8_isolate, attribute), - v8::Integer::New(v8_isolate, access_control)}; + name, + getter, + setter, + v8::Integer::New(v8_isolate, attribute), + v8::Integer::New(v8_isolate, access_control)}; TemplateSet(isolate, this, kSize, data); } // --- F u n c t i o n T e m p l a t e --- static void InitializeFunctionTemplate( - i::Handle info) { + i::Handle info) { info->set_tag(i::Smi::FromInt(Consts::FUNCTION_TEMPLATE)); info->set_flag(0); } @@ -1031,8 +994,8 @@ Local Signature::New(Isolate* isolate, Local AccessorSignature::New( - Isolate* isolate, - Handle receiver) { + Isolate* isolate, + Handle receiver) { return Utils::AccessorSignatureToLocal(Utils::OpenHandle(*receiver)); } @@ -1047,7 +1010,7 @@ static Local NewDescriptor( i::Handle(); if (previous_descriptor != NULL) { previous = Utils::OpenHandle( - static_cast(previous_descriptor)); + static_cast(previous_descriptor)); } i::Handle descriptor = i::DeclaredAccessorDescriptor::Create(internal_isolate, data, previous); @@ -1056,7 +1019,7 @@ static Local NewDescriptor( Local - ObjectOperationDescriptor::NewInternalFieldDereference( +ObjectOperationDescriptor::NewInternalFieldDereference( Isolate* isolate, int internal_field) { i::DeclaredAccessorDescriptorData data; @@ -1191,9 +1154,9 @@ int TypeSwitch::match(v8::Handle value) { } -#define SET_FIELD_WRAPPED(obj, setter, cdata) do { \ - i::Handle foreign = FromCData(obj->GetIsolate(), cdata); \ - (obj)->setter(*foreign); \ +#define SET_FIELD_WRAPPED(obj, setter, cdata) do { \ + i::Handle foreign = FromCData(obj->GetIsolate(), cdata); \ + (obj)->setter(*foreign); \ } while (false) @@ -1235,13 +1198,13 @@ static i::Handle SetAccessorInfoProperties( template static i::Handle MakeAccessorInfo( - v8::Handle name, - Getter getter, - Setter setter, - v8::Handle data, - v8::AccessControl settings, - v8::PropertyAttribute attributes, - v8::Handle signature) { + v8::Handle name, + Getter getter, + Setter setter, + v8::Handle data, + v8::AccessControl settings, + v8::PropertyAttribute attributes, + v8::Handle signature) { i::Isolate* isolate = Utils::OpenHandle(*name)->GetIsolate(); i::Handle obj = isolate->factory()->NewExecutableAccessorInfo(); @@ -1256,13 +1219,13 @@ static i::Handle MakeAccessorInfo( static i::Handle MakeAccessorInfo( - v8::Handle name, - v8::Handle descriptor, - void* setter_ignored, - void* data_ignored, - v8::AccessControl settings, - v8::PropertyAttribute attributes, - v8::Handle signature) { + v8::Handle name, + v8::Handle descriptor, + void* setter_ignored, + void* data_ignored, + v8::AccessControl settings, + v8::PropertyAttribute attributes, + v8::Handle signature) { i::Isolate* isolate = Utils::OpenHandle(*name)->GetIsolate(); if (descriptor.IsEmpty()) return i::Handle(); i::Handle obj = @@ -1274,8 +1237,11 @@ static i::Handle MakeAccessorInfo( Local FunctionTemplate::InstanceTemplate() { i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); - if (EmptyCheck("v8::FunctionTemplate::InstanceTemplate()", this)) + if (!Utils::ApiCheck(this != NULL, + "v8::FunctionTemplate::InstanceTemplate()", + "Reading from empty handle")) { return Local(); + } ENTER_V8(isolate); i::Handle handle = Utils::OpenHandle(this); if (handle->instance_template()->IsUndefined()) { @@ -1338,8 +1304,8 @@ Local ObjectTemplate::New() { Local ObjectTemplate::New( - i::Isolate* isolate, - v8::Handle constructor) { + i::Isolate* isolate, + v8::Handle constructor) { EnsureInitializedForIsolate(isolate, "v8::ObjectTemplate::New()"); LOG_API(isolate, "ObjectTemplate::New"); ENTER_V8(isolate); @@ -1508,10 +1474,10 @@ void ObjectTemplate::MarkAsUndetectable() { void ObjectTemplate::SetAccessCheckCallbacks( - NamedSecurityCallback named_callback, - IndexedSecurityCallback indexed_callback, - Handle data, - bool turned_on_by_default) { + NamedSecurityCallback named_callback, + IndexedSecurityCallback indexed_callback, + Handle data, + bool turned_on_by_default) { i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); ENTER_V8(isolate); i::HandleScope scope(isolate); @@ -1539,12 +1505,12 @@ void ObjectTemplate::SetAccessCheckCallbacks( void ObjectTemplate::SetIndexedPropertyHandler( - IndexedPropertyGetterCallback getter, - IndexedPropertySetterCallback setter, - IndexedPropertyQueryCallback query, - IndexedPropertyDeleterCallback remover, - IndexedPropertyEnumeratorCallback enumerator, - Handle data) { + IndexedPropertyGetterCallback getter, + IndexedPropertySetterCallback setter, + IndexedPropertyQueryCallback query, + IndexedPropertyDeleterCallback remover, + IndexedPropertyEnumeratorCallback enumerator, + Handle data) { i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); ENTER_V8(isolate); i::HandleScope scope(isolate); @@ -1600,9 +1566,9 @@ int ObjectTemplate::InternalFieldCount() { void ObjectTemplate::SetInternalFieldCount(int value) { i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); - if (!ApiCheck(i::Smi::IsValid(value), - "v8::ObjectTemplate::SetInternalFieldCount()", - "Invalid internal field count")) { + if (!Utils::ApiCheck(i::Smi::IsValid(value), + "v8::ObjectTemplate::SetInternalFieldCount()", + "Invalid internal field count")) { return; } ENTER_V8(isolate); @@ -1634,7 +1600,7 @@ ScriptData* ScriptData::PreCompile(v8::Handle source) { i::Isolate* isolate = str->GetIsolate(); if (str->IsExternalTwoByteString()) { i::ExternalTwoByteStringUtf16CharacterStream stream( - i::Handle::cast(str), 0, str->length()); + i::Handle::cast(str), 0, str->length()); return i::PreParserApi::PreParse(isolate, &stream); } else { i::GenericStringUtf16CharacterStream stream(str, 0, str->length()); @@ -1711,16 +1677,16 @@ Local