From c27da0c9b21343b2fa5e76ab8cc2dfccf5572715 Mon Sep 17 00:00:00 2001 From: "jochen@chromium.org" Date: Fri, 25 Apr 2014 13:49:22 +0000 Subject: [PATCH] Remove static CallCompletedCallback handlers BUG=none R=svenpanne@chromium.org LOG=y Review URL: https://codereview.chromium.org/249313002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20985 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- include/v8.h | 18 ------------------ src/api.cc | 21 +++++---------------- src/isolate.cc | 33 +++++++++++++++++++++++++++++++++ src/isolate.h | 7 +++++++ src/v8.cc | 46 ---------------------------------------------- src/v8.h | 6 ------ test/cctest/test-api.cc | 12 ++++++------ 7 files changed, 51 insertions(+), 92 deletions(-) diff --git a/include/v8.h b/include/v8.h index 1289730..ca34a52 100644 --- a/include/v8.h +++ b/include/v8.h @@ -4746,24 +4746,6 @@ class V8_EXPORT V8 { static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback); /** - * Adds a callback to notify the host application when a script finished - * running. If a script re-enters the runtime during executing, the - * CallCompletedCallback is only invoked when the outer-most script - * execution ends. Executing scripts inside the callback do not trigger - * further callbacks. - * - * Will be deprecated soon. Use Isolate::AddCallCompletedCallback. - */ - static void AddCallCompletedCallback(CallCompletedCallback callback); - - /** - * Removes callback that was installed by AddCallCompletedCallback. - * - * Will be deprecated soon. Use Isolate::RemoveCallCompletedCallback. - */ - static void RemoveCallCompletedCallback(CallCompletedCallback callback); - - /** * Experimental: Runs the Microtask Work Queue until empty */ static void RunMicrotasks(Isolate* isolate); diff --git a/src/api.cc b/src/api.cc index ec6fab4..43856f6 100644 --- a/src/api.cc +++ b/src/api.cc @@ -106,7 +106,7 @@ namespace v8 { #define EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, value) \ EXCEPTION_BAILOUT_CHECK_GENERIC( \ - isolate, value, i::V8::FireCallCompletedCallback(isolate);) + isolate, value, isolate->FireCallCompletedCallback();) #define EXCEPTION_BAILOUT_CHECK(isolate, value) \ @@ -6491,12 +6491,6 @@ void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) { } -void V8::AddCallCompletedCallback(CallCompletedCallback callback) { - if (callback == NULL) return; - i::V8::AddCallCompletedCallback(callback); -} - - void V8::RunMicrotasks(Isolate* isolate) { i::Isolate* i_isolate = reinterpret_cast(isolate); i::HandleScope scope(i_isolate); @@ -6516,11 +6510,6 @@ void V8::SetAutorunMicrotasks(Isolate* isolate, bool autorun) { } -void V8::RemoveCallCompletedCallback(CallCompletedCallback callback) { - i::V8::RemoveCallCompletedCallback(callback); -} - - void V8::TerminateExecution(Isolate* isolate) { reinterpret_cast(isolate)->stack_guard()->TerminateExecution(); } @@ -6669,14 +6658,14 @@ void Isolate::SetEventLogger(LogEventCallback that) { void Isolate::AddCallCompletedCallback(CallCompletedCallback callback) { if (callback == NULL) return; - // TODO(jochen): Make this per isolate. - i::V8::AddCallCompletedCallback(callback); + i::Isolate* isolate = reinterpret_cast(this); + isolate->AddCallCompletedCallback(callback); } void Isolate::RemoveCallCompletedCallback(CallCompletedCallback callback) { - // TODO(jochen): Make this per isolate. - i::V8::RemoveCallCompletedCallback(callback); + i::Isolate* isolate = reinterpret_cast(this); + isolate->RemoveCallCompletedCallback(callback); } diff --git a/src/isolate.cc b/src/isolate.cc index c678085..8f5b968 100644 --- a/src/isolate.cc +++ b/src/isolate.cc @@ -2243,4 +2243,37 @@ Handle Isolate::GetSymbolRegistry() { } +void Isolate::AddCallCompletedCallback(CallCompletedCallback callback) { + for (int i = 0; i < call_completed_callbacks_.length(); i++) { + if (callback == call_completed_callbacks_.at(i)) return; + } + call_completed_callbacks_.Add(callback); +} + + +void Isolate::RemoveCallCompletedCallback(CallCompletedCallback callback) { + for (int i = 0; i < call_completed_callbacks_.length(); i++) { + if (callback == call_completed_callbacks_.at(i)) { + call_completed_callbacks_.Remove(i); + } + } +} + + +void Isolate::FireCallCompletedCallback() { + bool has_call_completed_callbacks = !call_completed_callbacks_.is_empty(); + bool run_microtasks = autorun_microtasks() && microtask_pending(); + if (!has_call_completed_callbacks && !run_microtasks) return; + + if (!handle_scope_implementer()->CallDepthIsZero()) return; + // Fire callbacks. Increase call depth to prevent recursive callbacks. + handle_scope_implementer()->IncrementCallDepth(); + if (run_microtasks) Execution::RunMicrotasks(this); + for (int i = 0; i < call_completed_callbacks_.length(); i++) { + call_completed_callbacks_.at(i)(); + } + handle_scope_implementer()->DecrementCallDepth(); +} + + } } // namespace v8::internal diff --git a/src/isolate.h b/src/isolate.h index 9731356..216bf1e 100644 --- a/src/isolate.h +++ b/src/isolate.h @@ -1096,6 +1096,10 @@ class Isolate { // Get (and lazily initialize) the registry for per-isolate symbols. Handle GetSymbolRegistry(); + void AddCallCompletedCallback(CallCompletedCallback callback); + void RemoveCallCompletedCallback(CallCompletedCallback callback); + void FireCallCompletedCallback(); + private: Isolate(); @@ -1314,6 +1318,9 @@ class Isolate { int next_optimization_id_; + // List of callbacks when a Call completes. + List call_completed_callbacks_; + friend class ExecutionAccess; friend class HandleScopeImplementer; friend class IsolateInitializer; diff --git a/src/v8.cc b/src/v8.cc index afe8e4b..70a4983 100644 --- a/src/v8.cc +++ b/src/v8.cc @@ -53,7 +53,6 @@ namespace internal { V8_DECLARE_ONCE(init_once); -List* V8::call_completed_callbacks_ = NULL; v8::ArrayBuffer::Allocator* V8::array_buffer_allocator_ = NULL; v8::Platform* V8::platform_ = NULL; @@ -94,9 +93,6 @@ void V8::TearDown() { RegisteredExtension::UnregisterAll(); Isolate::GlobalTearDown(); - delete call_completed_callbacks_; - call_completed_callbacks_ = NULL; - Sampler::TearDown(); Serializer::TearDown(); @@ -114,48 +110,6 @@ void V8::SetReturnAddressLocationResolver( } -void V8::AddCallCompletedCallback(CallCompletedCallback callback) { - if (call_completed_callbacks_ == NULL) { // Lazy init. - call_completed_callbacks_ = new List(); - } - for (int i = 0; i < call_completed_callbacks_->length(); i++) { - if (callback == call_completed_callbacks_->at(i)) return; - } - call_completed_callbacks_->Add(callback); -} - - -void V8::RemoveCallCompletedCallback(CallCompletedCallback callback) { - if (call_completed_callbacks_ == NULL) return; - for (int i = 0; i < call_completed_callbacks_->length(); i++) { - if (callback == call_completed_callbacks_->at(i)) { - call_completed_callbacks_->Remove(i); - } - } -} - - -void V8::FireCallCompletedCallback(Isolate* isolate) { - bool has_call_completed_callbacks = call_completed_callbacks_ != NULL; - bool run_microtasks = isolate->autorun_microtasks() && - isolate->microtask_pending(); - if (!has_call_completed_callbacks && !run_microtasks) return; - - HandleScopeImplementer* handle_scope_implementer = - isolate->handle_scope_implementer(); - if (!handle_scope_implementer->CallDepthIsZero()) return; - // Fire callbacks. Increase call depth to prevent recursive callbacks. - handle_scope_implementer->IncrementCallDepth(); - if (run_microtasks) Execution::RunMicrotasks(isolate); - if (has_call_completed_callbacks) { - for (int i = 0; i < call_completed_callbacks_->length(); i++) { - call_completed_callbacks_->at(i)(); - } - } - handle_scope_implementer->DecrementCallDepth(); -} - - void V8::RunMicrotasks(Isolate* isolate) { if (!isolate->microtask_pending()) return; diff --git a/src/v8.h b/src/v8.h index 4477dad..2b65f67 100644 --- a/src/v8.h +++ b/src/v8.h @@ -98,10 +98,6 @@ class V8 : public AllStatic { // Support for entry hooking JITed code. static void SetFunctionEntryHook(FunctionEntryHook entry_hook); - static void AddCallCompletedCallback(CallCompletedCallback callback); - static void RemoveCallCompletedCallback(CallCompletedCallback callback); - static void FireCallCompletedCallback(Isolate* isolate); - static void RunMicrotasks(Isolate* isolate); static v8::ArrayBuffer::Allocator* ArrayBufferAllocator() { @@ -121,8 +117,6 @@ class V8 : public AllStatic { static void InitializeOncePerProcessImpl(); static void InitializeOncePerProcess(); - // List of callbacks when a Call completes. - static List* call_completed_callbacks_; // Allocator for external array buffers. static v8::ArrayBuffer::Allocator* array_buffer_allocator_; // v8::Platform to use. diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index b60ee42..eb9db87 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -20643,9 +20643,9 @@ TEST(CallCompletedCallback) { env->Global()->Set(v8_str("recursion"), recursive_runtime->GetFunction()); // Adding the same callback a second time has no effect. - v8::V8::AddCallCompletedCallback(CallCompletedCallback1); - v8::V8::AddCallCompletedCallback(CallCompletedCallback1); - v8::V8::AddCallCompletedCallback(CallCompletedCallback2); + env->GetIsolate()->AddCallCompletedCallback(CallCompletedCallback1); + env->GetIsolate()->AddCallCompletedCallback(CallCompletedCallback1); + env->GetIsolate()->AddCallCompletedCallback(CallCompletedCallback2); i::OS::Print("--- Script (1) ---\n"); Local