Browse Source

Handle v8::MicrotasksScope in the main process

Cheng Zhao 8 years ago
parent
commit
ee28f4fc32

+ 4 - 0
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

+ 9 - 0
atom/browser/javascript_environment.cc

@@ -7,6 +7,7 @@
 #include <string>
 
 #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<v8::Context>::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")) {

+ 3 - 0
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<v8::Context> context() const {
     return v8::Local<v8::Context>::New(isolate_, context_);

+ 2 - 5
atom/common/api/event_emitter_caller.cc

@@ -16,11 +16,8 @@ v8::Local<v8::Value> CallEmitWithArgs(v8::Isolate* isolate,
                                       v8::Local<v8::Object> obj,
                                       ValueVector* args) {
   // Perform microtask checkpoint after running JavaScript.
-  std::unique_ptr<v8::MicrotasksScope> 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(

+ 6 - 15
atom/common/native_mate_converters/callback.h

@@ -48,11 +48,8 @@ struct V8FunctionInvoker<v8::Local<v8::Value>(ArgTypes...)> {
     v8::EscapableHandleScope handle_scope(isolate);
     if (!function.IsAlive())
       return v8::Null(isolate);
-    std::unique_ptr<v8::MicrotasksScope> script_scope(
-        Locker::IsBrowserProcess() ?
-            nullptr :
-            new v8::MicrotasksScope(isolate,
-                                    v8::MicrotasksScope::kRunMicrotasks));
+    v8::MicrotasksScope script_scope(isolate,
+                                     v8::MicrotasksScope::kRunMicrotasks);
     v8::Local<v8::Function> holder = function.NewHandle(isolate);
     v8::Local<v8::Context> context = holder->CreationContext();
     v8::Context::Scope context_scope(context);
@@ -71,11 +68,8 @@ struct V8FunctionInvoker<void(ArgTypes...)> {
     v8::HandleScope handle_scope(isolate);
     if (!function.IsAlive())
       return;
-    std::unique_ptr<v8::MicrotasksScope> script_scope(
-        Locker::IsBrowserProcess() ?
-            nullptr :
-            new v8::MicrotasksScope(isolate,
-                                    v8::MicrotasksScope::kRunMicrotasks));
+    v8::MicrotasksScope script_scope(isolate,
+                                     v8::MicrotasksScope::kRunMicrotasks);
     v8::Local<v8::Function> holder = function.NewHandle(isolate);
     v8::Local<v8::Context> context = holder->CreationContext();
     v8::Context::Scope context_scope(context);
@@ -94,11 +88,8 @@ struct V8FunctionInvoker<ReturnType(ArgTypes...)> {
     ReturnType ret = ReturnType();
     if (!function.IsAlive())
       return ret;
-    std::unique_ptr<v8::MicrotasksScope> script_scope(
-        Locker::IsBrowserProcess() ?
-            nullptr :
-            new v8::MicrotasksScope(isolate,
-                                    v8::MicrotasksScope::kRunMicrotasks));
+    v8::MicrotasksScope script_scope(isolate,
+                                     v8::MicrotasksScope::kRunMicrotasks);
     v8::Local<v8::Function> holder = function.NewHandle(isolate);
     v8::Local<v8::Context> context = holder->CreationContext();
     v8::Context::Scope context_scope(context);

+ 2 - 4
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<v8::MicrotasksScope> 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);

+ 1 - 1
vendor/native_mate

@@ -1 +1 @@
-Subproject commit e75f2aa087db346efc4b530f9e1ce7d3a72a3434
+Subproject commit a1efa285204cb2fbbed450c317fb535a38ea8480