From f23729acdb83f16fb6b7ca6b73d1d514124ebba9 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 27 Mar 2016 14:47:01 +0900 Subject: [PATCH 1/7] Upgrade to node v5.9.1 --- vendor/node | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/node b/vendor/node index a507a3c3816d..12524cebe232 160000 --- a/vendor/node +++ b/vendor/node @@ -1 +1 @@ -Subproject commit a507a3c3816d6ac085ed46250c489a3d76ab8b3c +Subproject commit 12524cebe23226df1f621ffc12e44a62582ae352 From d83cb53997e8e3a4fdf856df770a2c2e8fd296b7 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 27 Mar 2016 14:54:26 +0900 Subject: [PATCH 2/7] Type of native array has changed --- atom/common/api/atom_api_asar.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/atom/common/api/atom_api_asar.cc b/atom/common/api/atom_api_asar.cc index 8f5190fecaac..4bfb0ed4c1be 100644 --- a/atom/common/api/atom_api_asar.cc +++ b/atom/common/api/atom_api_asar.cc @@ -129,9 +129,11 @@ void InitAsarSupport(v8::Isolate* isolate, v8::Local process, v8::Local require) { // Evaluate asar_init.coffee. + const char* asar_init_native = reinterpret_cast( + static_cast(node::asar_init_native)); v8::Local asar_init = v8::Script::Compile(v8::String::NewFromUtf8( isolate, - node::asar_init_native, + asar_init_native, v8::String::kNormalString, sizeof(node::asar_init_native) -1)); v8::Local result = asar_init->Run(); @@ -141,9 +143,11 @@ void InitAsarSupport(v8::Isolate* isolate, v8::Local, std::string)> init; if (mate::ConvertFromV8(isolate, result, &init)) { + const char* asar_native = reinterpret_cast( + static_cast(node::asar_native)); init.Run(process, require, - std::string(node::asar_native, sizeof(node::asar_native) - 1)); + std::string(asar_native, sizeof(node::asar_native) - 1)); } } From 70f9cb098ff24cd6a06b65843471a0231a66206c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 27 Mar 2016 15:03:53 +0900 Subject: [PATCH 3/7] Use Environment::KickNextTick --- atom/common/api/atom_bindings.cc | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/atom/common/api/atom_bindings.cc b/atom/common/api/atom_bindings.cc index a000f6fc743a..fe53d8793f2a 100644 --- a/atom/common/api/atom_bindings.cc +++ b/atom/common/api/atom_bindings.cc @@ -98,24 +98,8 @@ void AtomBindings::OnCallNextTick(uv_async_t* handle) { self->pending_next_ticks_.begin(); it != self->pending_next_ticks_.end(); ++it) { node::Environment* env = *it; - node::Environment::TickInfo* tick_info = env->tick_info(); - - v8::Context::Scope context_scope(env->context()); - if (tick_info->in_tick()) - continue; - - if (tick_info->length() == 0) { - env->isolate()->RunMicrotasks(); - } - - if (tick_info->length() == 0) { - tick_info->set_index(0); - continue; - } - - tick_info->set_in_tick(true); - env->tick_callback_function()->Call(env->process_object(), 0, NULL); - tick_info->set_in_tick(false); + node::Environment::AsyncCallbackScope callback_scope(env); + env->KickNextTick(&callback_scope); } self->pending_next_ticks_.clear(); From 896ea7b79dab9e698b8fc916f8f51e79b5e0e6d5 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 27 Mar 2016 18:09:21 +0900 Subject: [PATCH 4/7] Do not create dummy node environment There is a bug in V8 that using Private in a dummy environment would result in crash. --- atom/renderer/atom_renderer_client.cc | 30 +++++++++++++-------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index 50ff109a2fc6..99696cc8282b 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -90,18 +90,6 @@ void AtomRendererClient::WebKitInitialized() { blink::WebCustomElement::addEmbedderCustomElementName("browserplugin"); OverrideNodeArrayBuffer(); - - node_bindings_->Initialize(); - node_bindings_->PrepareMessageLoop(); - - DCHECK(!global_env); - - // Create a default empty environment which would be used when we need to - // run V8 code out of a window context (like running a uv callback). - v8::Isolate* isolate = blink::mainThreadIsolate(); - v8::HandleScope handle_scope(isolate); - v8::Local context = v8::Context::New(isolate); - global_env = node::Environment::New(context, uv_default_loop()); } void AtomRendererClient::RenderThreadStarted() { @@ -160,8 +148,14 @@ bool AtomRendererClient::OverrideCreatePlugin( void AtomRendererClient::DidCreateScriptContext( v8::Handle context) { - // Give the node loop a run to make sure everything is ready. - node_bindings_->RunMessageLoop(); + // Whether the node binding has been initialized. + bool first_time = node_bindings_->uv_env() == nullptr; + + // Prepare the node bindings. + if (first_time) { + node_bindings_->Initialize(); + node_bindings_->PrepareMessageLoop(); + } // Setup node environment for each window. node::Environment* env = node_bindings_->CreateEnvironment(context); @@ -169,10 +163,14 @@ void AtomRendererClient::DidCreateScriptContext( // Add atom-shell extended APIs. atom_bindings_->BindTo(env->isolate(), env->process_object()); - // Make uv loop being wrapped by window context. - if (node_bindings_->uv_env() == nullptr) + if (first_time) { + // Make uv loop being wrapped by window context. node_bindings_->set_uv_env(env); + // Give the node loop a run to make sure everything is ready. + node_bindings_->RunMessageLoop(); + } + // Load everything. node_bindings_->LoadEnvironment(env); } From e5886dda9b3ae8a857a069a9c65737f8e66f2c5c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 27 Mar 2016 19:13:15 +0900 Subject: [PATCH 5/7] Avoid re-evaluating internal modules --- lib/common/asar_init.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/common/asar_init.js b/lib/common/asar_init.js index 49b57907c8f9..85bb828583bc 100644 --- a/lib/common/asar_init.js +++ b/lib/common/asar_init.js @@ -9,6 +9,12 @@ // Make graceful-fs work with asar. var source = process.binding('natives'); source['original-fs'] = source.fs; - return source['fs'] = "var src = '(function (exports, require, module, __filename, __dirname) { ' +\n process.binding('natives')['original-fs'] +\n ' });';\nvar vm = require('vm');\nvar fn = vm.runInThisContext(src, { filename: 'fs.js' });\nfn(exports, require, module);\nvar asar = require('ATOM_SHELL_ASAR');\nasar.wrapFsWithAsar(exports);"; + return source['fs'] = ` +var nativeModule = new process.NativeModule('original-fs'); +nativeModule.cache(); +nativeModule.compile(); +var asar = require('ATOM_SHELL_ASAR'); +asar.wrapFsWithAsar(nativeModule.exports); +module.exports = nativeModule.exports`; }; })(); From e401335ebb2fed1bd224a4be8f305e593f8a2c8c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 27 Mar 2016 19:21:12 +0900 Subject: [PATCH 6/7] Get rid of the global_env --- atom/browser/atom_browser_main_parts.cc | 12 ++++++++---- atom/common/node_bindings.cc | 8 ++------ atom/common/node_includes.h | 7 ------- atom/renderer/atom_renderer_client.cc | 6 +++--- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/atom/browser/atom_browser_main_parts.cc b/atom/browser/atom_browser_main_parts.cc index a711c1c8d2b6..f45f6492a849 100644 --- a/atom/browser/atom_browser_main_parts.cc +++ b/atom/browser/atom_browser_main_parts.cc @@ -106,17 +106,21 @@ void AtomBrowserMainParts::PostEarlyInitialization() { node_debugger_.reset(new NodeDebugger(js_env_->isolate())); // Create the global environment. - global_env = node_bindings_->CreateEnvironment(js_env_->context()); + node::Environment* env = + node_bindings_->CreateEnvironment(js_env_->context()); // Make sure node can get correct environment when debugging. if (node_debugger_->IsRunning()) - global_env->AssignToContext(v8::Debug::GetDebugContext()); + env->AssignToContext(v8::Debug::GetDebugContext()); // Add atom-shell extended APIs. - atom_bindings_->BindTo(js_env_->isolate(), global_env->process_object()); + atom_bindings_->BindTo(js_env_->isolate(), env->process_object()); // Load everything. - node_bindings_->LoadEnvironment(global_env); + node_bindings_->LoadEnvironment(env); + + // Wrap the uv loop with global env. + node_bindings_->set_uv_env(env); } void AtomBrowserMainParts::PreMainMessageLoopRun() { diff --git a/atom/common/node_bindings.cc b/atom/common/node_bindings.cc index 950a2cd786a6..b0b4148c7baf 100644 --- a/atom/common/node_bindings.cc +++ b/atom/common/node_bindings.cc @@ -106,8 +106,6 @@ base::FilePath GetResourcesPath(bool is_browser) { } // namespace -node::Environment* global_env = nullptr; - NodeBindings::NodeBindings(bool is_browser) : is_browser_(is_browser), message_loop_(nullptr), @@ -214,10 +212,8 @@ void NodeBindings::RunMessageLoop() { void NodeBindings::UvRunOnce() { DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI)); - // By default the global env would be used unless user specified another one - // (this happens for renderer process, which wraps the uv loop with web page - // context). - node::Environment* env = uv_env() ? uv_env() : global_env; + node::Environment* env = uv_env(); + CHECK(env); // Use Locker in browser process. mate::Locker locker(env->isolate()); diff --git a/atom/common/node_includes.h b/atom/common/node_includes.h index 3876d8622913..bb76afb54db9 100644 --- a/atom/common/node_includes.h +++ b/atom/common/node_includes.h @@ -28,11 +28,4 @@ #include "vendor/node/src/node_buffer.h" #include "vendor/node/src/node_internals.h" -namespace atom { -// Defined in node_bindings.cc. -// For renderer it's created in atom_renderer_client.cc. -// For browser it's created in atom_browser_main_parts.cc. -extern node::Environment* global_env; -} - #endif // ATOM_COMMON_NODE_INCLUDES_H_ diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index 99696cc8282b..7746ce123e4f 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -163,6 +163,9 @@ void AtomRendererClient::DidCreateScriptContext( // Add atom-shell extended APIs. atom_bindings_->BindTo(env->isolate(), env->process_object()); + // Load everything. + node_bindings_->LoadEnvironment(env); + if (first_time) { // Make uv loop being wrapped by window context. node_bindings_->set_uv_env(env); @@ -170,9 +173,6 @@ void AtomRendererClient::DidCreateScriptContext( // Give the node loop a run to make sure everything is ready. node_bindings_->RunMessageLoop(); } - - // Load everything. - node_bindings_->LoadEnvironment(env); } void AtomRendererClient::WillReleaseScriptContext( From a4d2dd9b4d65f923f508f5b1509b3da40dad47c0 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 27 Mar 2016 19:50:34 +0900 Subject: [PATCH 7/7] Export symbols of node::Environment --- common.gypi | 1 + vendor/node | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/common.gypi b/common.gypi index 6eacdc93a024..a9f067bcc54e 100644 --- a/common.gypi +++ b/common.gypi @@ -230,6 +230,7 @@ 'msvs_cygwin_shell': 0, # Strangely setting it to 1 would make building under cygwin fail. 'msvs_disabled_warnings': [ 4005, # (node.h) macro redefinition + 4091, # (node_extern.h) '__declspec(dllimport)' : ignored on left of 'node::Environment' when no variable is declared 4189, # local variable is initialized but not referenced 4201, # (uv.h) nameless struct/union 4267, # conversion from 'size_t' to 'int', possible loss of data diff --git a/vendor/node b/vendor/node index 12524cebe232..d8e7d3e76cb3 160000 --- a/vendor/node +++ b/vendor/node @@ -1 +1 @@ -Subproject commit 12524cebe23226df1f621ffc12e44a62582ae352 +Subproject commit d8e7d3e76cb3c6e709449d181ddc2af8c4859303