From ee28f4fc32c9c859a0e258f3a26813c030757399 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 24 Jun 2016 14:45:31 +0900 Subject: [PATCH] Handle v8::MicrotasksScope in the main process --- atom/browser/atom_browser_main_parts.cc | 4 ++++ atom/browser/javascript_environment.cc | 9 ++++++++ atom/browser/javascript_environment.h | 3 +++ atom/common/api/event_emitter_caller.cc | 7 ++----- atom/common/native_mate_converters/callback.h | 21 ++++++------------- atom/common/node_bindings.cc | 6 ++---- vendor/native_mate | 2 +- 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/atom/browser/atom_browser_main_parts.cc b/atom/browser/atom_browser_main_parts.cc index 75f71d42314f..b933d1073bb6 100644 --- a/atom/browser/atom_browser_main_parts.cc +++ b/atom/browser/atom_browser_main_parts.cc @@ -124,6 +124,8 @@ void AtomBrowserMainParts::PostEarlyInitialization() { } void AtomBrowserMainParts::PreMainMessageLoopRun() { + js_env_->OnMessageLoopCreated(); + // Run user's main script before most things get initialized, so we can have // a chance to setup everything. node_bindings_->PrepareMessageLoop(); @@ -169,6 +171,8 @@ void AtomBrowserMainParts::PostMainMessageLoopStart() { void AtomBrowserMainParts::PostMainMessageLoopRun() { brightray::BrowserMainParts::PostMainMessageLoopRun(); + js_env_->OnMessageLoopDestroying(); + #if defined(OS_MACOSX) FreeAppDelegate(); #endif diff --git a/atom/browser/javascript_environment.cc b/atom/browser/javascript_environment.cc index 970132b47c15..0f87a2ca32fd 100644 --- a/atom/browser/javascript_environment.cc +++ b/atom/browser/javascript_environment.cc @@ -7,6 +7,7 @@ #include #include "base/command_line.h" +#include "base/message_loop/message_loop.h" #include "content/public/common/content_switches.h" #include "gin/array_buffer.h" #include "gin/v8_initializer.h" @@ -23,6 +24,14 @@ JavascriptEnvironment::JavascriptEnvironment() context_scope_(v8::Local::New(isolate_, context_)) { } +void JavascriptEnvironment::OnMessageLoopCreated() { + isolate_holder_.AddRunMicrotasksObserver(); +} + +void JavascriptEnvironment::OnMessageLoopDestroying() { + isolate_holder_.RemoveRunMicrotasksObserver(); +} + bool JavascriptEnvironment::Initialize() { auto cmd = base::CommandLine::ForCurrentProcess(); if (cmd->HasSwitch("debug-brk")) { diff --git a/atom/browser/javascript_environment.h b/atom/browser/javascript_environment.h index 07cd602cf00d..1f4d2f453478 100644 --- a/atom/browser/javascript_environment.h +++ b/atom/browser/javascript_environment.h @@ -14,6 +14,9 @@ class JavascriptEnvironment { public: JavascriptEnvironment(); + void OnMessageLoopCreated(); + void OnMessageLoopDestroying(); + v8::Isolate* isolate() const { return isolate_; } v8::Local context() const { return v8::Local::New(isolate_, context_); diff --git a/atom/common/api/event_emitter_caller.cc b/atom/common/api/event_emitter_caller.cc index 271ea705e09c..ac6c9c213f7b 100644 --- a/atom/common/api/event_emitter_caller.cc +++ b/atom/common/api/event_emitter_caller.cc @@ -16,11 +16,8 @@ v8::Local CallEmitWithArgs(v8::Isolate* isolate, v8::Local obj, ValueVector* args) { // Perform microtask checkpoint after running JavaScript. - std::unique_ptr script_scope( - Locker::IsBrowserProcess() ? - nullptr : - new v8::MicrotasksScope(isolate, - v8::MicrotasksScope::kRunMicrotasks)); + v8::MicrotasksScope script_scope( + isolate, v8::MicrotasksScope::kRunMicrotasks); // Use node::MakeCallback to call the callback, and it will also run pending // tasks in Node.js. return node::MakeCallback( diff --git a/atom/common/native_mate_converters/callback.h b/atom/common/native_mate_converters/callback.h index 43a1baf25fa8..decc36eb5767 100644 --- a/atom/common/native_mate_converters/callback.h +++ b/atom/common/native_mate_converters/callback.h @@ -48,11 +48,8 @@ struct V8FunctionInvoker(ArgTypes...)> { v8::EscapableHandleScope handle_scope(isolate); if (!function.IsAlive()) return v8::Null(isolate); - std::unique_ptr script_scope( - Locker::IsBrowserProcess() ? - nullptr : - new v8::MicrotasksScope(isolate, - v8::MicrotasksScope::kRunMicrotasks)); + v8::MicrotasksScope script_scope(isolate, + v8::MicrotasksScope::kRunMicrotasks); v8::Local holder = function.NewHandle(isolate); v8::Local context = holder->CreationContext(); v8::Context::Scope context_scope(context); @@ -71,11 +68,8 @@ struct V8FunctionInvoker { v8::HandleScope handle_scope(isolate); if (!function.IsAlive()) return; - std::unique_ptr script_scope( - Locker::IsBrowserProcess() ? - nullptr : - new v8::MicrotasksScope(isolate, - v8::MicrotasksScope::kRunMicrotasks)); + v8::MicrotasksScope script_scope(isolate, + v8::MicrotasksScope::kRunMicrotasks); v8::Local holder = function.NewHandle(isolate); v8::Local context = holder->CreationContext(); v8::Context::Scope context_scope(context); @@ -94,11 +88,8 @@ struct V8FunctionInvoker { ReturnType ret = ReturnType(); if (!function.IsAlive()) return ret; - std::unique_ptr script_scope( - Locker::IsBrowserProcess() ? - nullptr : - new v8::MicrotasksScope(isolate, - v8::MicrotasksScope::kRunMicrotasks)); + v8::MicrotasksScope script_scope(isolate, + v8::MicrotasksScope::kRunMicrotasks); v8::Local holder = function.NewHandle(isolate); v8::Local context = holder->CreationContext(); v8::Context::Scope context_scope(context); diff --git a/atom/common/node_bindings.cc b/atom/common/node_bindings.cc index ed2ea01675a1..40fa6bdb6e28 100644 --- a/atom/common/node_bindings.cc +++ b/atom/common/node_bindings.cc @@ -226,10 +226,8 @@ void NodeBindings::UvRunOnce() { v8::Context::Scope context_scope(env->context()); // Perform microtask checkpoint after running JavaScript. - std::unique_ptr script_scope(is_browser_ ? - nullptr : - new v8::MicrotasksScope(env->isolate(), - v8::MicrotasksScope::kRunMicrotasks)); + v8::MicrotasksScope script_scope(env->isolate(), + v8::MicrotasksScope::kRunMicrotasks); // Deal with uv events. int r = uv_run(uv_loop_, UV_RUN_NOWAIT); diff --git a/vendor/native_mate b/vendor/native_mate index e75f2aa087db..a1efa285204c 160000 --- a/vendor/native_mate +++ b/vendor/native_mate @@ -1 +1 @@ -Subproject commit e75f2aa087db346efc4b530f9e1ce7d3a72a3434 +Subproject commit a1efa285204cb2fbbed450c317fb535a38ea8480