diff --git a/common/api/atom_bindings.cc b/common/api/atom_bindings.cc index f2a88a7d2748..8dc3b85a45a1 100644 --- a/common/api/atom_bindings.cc +++ b/common/api/atom_bindings.cc @@ -17,7 +17,7 @@ namespace { static int kMaxCallStackSize = 200; // Same with WebKit. // Async handle to wake up uv loop. -static uv_async_t g_dummy_uv_handle; +static uv_async_t g_next_tick_uv_handle; // Async handle to execute the stored v8 callback. static uv_async_t g_callback_uv_handle; @@ -28,8 +28,22 @@ RefCountedV8Function g_v8_callback; // Dummy class type that used for crashing the program. struct DummyClass { bool crash; }; -// Dummy async handler that does nothing. -void UvNoOp(uv_async_t* handle, int status) { +// Async handler to call next process.nextTick callbacks. +void UvCallNextTick(uv_async_t* handle, int status) { + node::Environment* env = node::Environment::GetCurrent(node_isolate); + node::Environment::TickInfo* tick_info = env->tick_info(); + + if (tick_info->in_tick()) + return; + + if (tick_info->length() == 0) { + tick_info->set_index(0); + return; + } + + tick_info->set_in_tick(true); + env->tick_callback_function()->Call(env->process_object(), 0, NULL); + tick_info->set_in_tick(false); } // Async handler to execute the stored v8 callback. @@ -60,7 +74,7 @@ v8::Handle DumpStackFrame(v8::Handle stack_frame) { node::node_module_struct* GetBuiltinModule(const char *name, bool is_browser); AtomBindings::AtomBindings() { - uv_async_init(uv_default_loop(), &g_dummy_uv_handle, UvNoOp); + uv_async_init(uv_default_loop(), &g_next_tick_uv_handle, UvCallNextTick); uv_async_init(uv_default_loop(), &g_callback_uv_handle, UvOnCallback); } @@ -133,7 +147,7 @@ void AtomBindings::Crash(const v8::FunctionCallbackInfo& args) { // static void AtomBindings::ActivateUVLoop( const v8::FunctionCallbackInfo& args) { - uv_async_send(&g_dummy_uv_handle); + uv_async_send(&g_next_tick_uv_handle); } // static diff --git a/common/node_bindings.cc b/common/node_bindings.cc index 247ca0700ca1..e36ba9baff4d 100644 --- a/common/node_bindings.cc +++ b/common/node_bindings.cc @@ -199,7 +199,7 @@ void NodeBindings::UvRunOnce() { // Enter node context while dealing with uv events, 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 = get_uv_env() ? get_uv_env() : global_env; + node::Environment* env = uv_env() ? uv_env() : global_env; v8::Context::Scope context_scope(env->context()); // Deal with uv events. diff --git a/common/node_bindings.h b/common/node_bindings.h index 737049559754..7f61ed4832ed 100644 --- a/common/node_bindings.h +++ b/common/node_bindings.h @@ -40,7 +40,7 @@ class NodeBindings { // Gets/sets the environment to wrap uv loop. void set_uv_env(node::Environment* env) { uv_env_ = env; } - node::Environment* get_uv_env() const { return uv_env_; } + node::Environment* uv_env() const { return uv_env_; } protected: explicit NodeBindings(bool is_browser); diff --git a/package.json b/package.json index 7658c557e351..825295123f31 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "coffeelint": "~0.6.1", "mocha": "~1.13.0", "pathwatcher": "0.13.0", + "q": "0.9.7", "walkdir": "~0.0.7", "runas": "0.3.0", "formidable": "~1.0.14", diff --git a/renderer/atom_renderer_client.cc b/renderer/atom_renderer_client.cc index 770a449d3eba..176ca280efbc 100644 --- a/renderer/atom_renderer_client.cc +++ b/renderer/atom_renderer_client.cc @@ -62,7 +62,7 @@ void AtomRendererClient::DidCreateScriptContext(WebKit::WebFrame* frame, web_page_envs_.push_back(env); // Make uv loop being wrapped by window context. - if (node_bindings_->get_uv_env() == NULL) + if (node_bindings_->uv_env() == NULL) node_bindings_->set_uv_env(env); } @@ -91,7 +91,7 @@ void AtomRendererClient::WillReleaseScriptContext( // env->Dispose(); // Wrap the uv loop with another environment. - if (env == node_bindings_->get_uv_env()) { + if (env == node_bindings_->uv_env()) { node::Environment* env = web_page_envs_.size() > 0 ? web_page_envs_[0] : NULL; node_bindings_->set_uv_env(env); diff --git a/spec/modules-spec.coffee b/spec/modules-spec.coffee index ef4d742b17db..165449638e2f 100644 --- a/spec/modules-spec.coffee +++ b/spec/modules-spec.coffee @@ -30,3 +30,13 @@ describe 'third-party module', -> watcher.close() done() fs.writeFileSync file, 'content2' + + describe 'q', -> + Q = require 'q' + + describe 'Q.when', -> + it 'emits the fullfil callback', (done) -> + Q(true).then (val) -> + assert.equal val, true + console.log 'test' + done()