Handle v8::MicrotasksScope in the main process

This commit is contained in:
Cheng Zhao 2016-06-24 14:45:31 +09:00
parent 5826a8f9a9
commit ee28f4fc32
7 changed files with 27 additions and 25 deletions

View file

@ -124,6 +124,8 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
} }
void AtomBrowserMainParts::PreMainMessageLoopRun() { void AtomBrowserMainParts::PreMainMessageLoopRun() {
js_env_->OnMessageLoopCreated();
// Run user's main script before most things get initialized, so we can have // Run user's main script before most things get initialized, so we can have
// a chance to setup everything. // a chance to setup everything.
node_bindings_->PrepareMessageLoop(); node_bindings_->PrepareMessageLoop();
@ -169,6 +171,8 @@ void AtomBrowserMainParts::PostMainMessageLoopStart() {
void AtomBrowserMainParts::PostMainMessageLoopRun() { void AtomBrowserMainParts::PostMainMessageLoopRun() {
brightray::BrowserMainParts::PostMainMessageLoopRun(); brightray::BrowserMainParts::PostMainMessageLoopRun();
js_env_->OnMessageLoopDestroying();
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
FreeAppDelegate(); FreeAppDelegate();
#endif #endif

View file

@ -7,6 +7,7 @@
#include <string> #include <string>
#include "base/command_line.h" #include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "gin/array_buffer.h" #include "gin/array_buffer.h"
#include "gin/v8_initializer.h" #include "gin/v8_initializer.h"
@ -23,6 +24,14 @@ JavascriptEnvironment::JavascriptEnvironment()
context_scope_(v8::Local<v8::Context>::New(isolate_, context_)) { context_scope_(v8::Local<v8::Context>::New(isolate_, context_)) {
} }
void JavascriptEnvironment::OnMessageLoopCreated() {
isolate_holder_.AddRunMicrotasksObserver();
}
void JavascriptEnvironment::OnMessageLoopDestroying() {
isolate_holder_.RemoveRunMicrotasksObserver();
}
bool JavascriptEnvironment::Initialize() { bool JavascriptEnvironment::Initialize() {
auto cmd = base::CommandLine::ForCurrentProcess(); auto cmd = base::CommandLine::ForCurrentProcess();
if (cmd->HasSwitch("debug-brk")) { if (cmd->HasSwitch("debug-brk")) {

View file

@ -14,6 +14,9 @@ class JavascriptEnvironment {
public: public:
JavascriptEnvironment(); JavascriptEnvironment();
void OnMessageLoopCreated();
void OnMessageLoopDestroying();
v8::Isolate* isolate() const { return isolate_; } v8::Isolate* isolate() const { return isolate_; }
v8::Local<v8::Context> context() const { v8::Local<v8::Context> context() const {
return v8::Local<v8::Context>::New(isolate_, context_); return v8::Local<v8::Context>::New(isolate_, context_);

View file

@ -16,11 +16,8 @@ v8::Local<v8::Value> CallEmitWithArgs(v8::Isolate* isolate,
v8::Local<v8::Object> obj, v8::Local<v8::Object> obj,
ValueVector* args) { ValueVector* args) {
// Perform microtask checkpoint after running JavaScript. // Perform microtask checkpoint after running JavaScript.
std::unique_ptr<v8::MicrotasksScope> script_scope( v8::MicrotasksScope script_scope(
Locker::IsBrowserProcess() ? isolate, v8::MicrotasksScope::kRunMicrotasks);
nullptr :
new v8::MicrotasksScope(isolate,
v8::MicrotasksScope::kRunMicrotasks));
// Use node::MakeCallback to call the callback, and it will also run pending // Use node::MakeCallback to call the callback, and it will also run pending
// tasks in Node.js. // tasks in Node.js.
return node::MakeCallback( return node::MakeCallback(

View file

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

View file

@ -226,10 +226,8 @@ void NodeBindings::UvRunOnce() {
v8::Context::Scope context_scope(env->context()); v8::Context::Scope context_scope(env->context());
// Perform microtask checkpoint after running JavaScript. // Perform microtask checkpoint after running JavaScript.
std::unique_ptr<v8::MicrotasksScope> script_scope(is_browser_ ? v8::MicrotasksScope script_scope(env->isolate(),
nullptr : v8::MicrotasksScope::kRunMicrotasks);
new v8::MicrotasksScope(env->isolate(),
v8::MicrotasksScope::kRunMicrotasks));
// Deal with uv events. // Deal with uv events.
int r = uv_run(uv_loop_, UV_RUN_NOWAIT); int r = uv_run(uv_loop_, UV_RUN_NOWAIT);

2
vendor/native_mate vendored

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