Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[squash] bnoordhuis comments
  • Loading branch information
addaleax committed Nov 18, 2017
commit 421f9d1d178e426b773cac15e9e1a1f34830b23b
6 changes: 5 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4420,7 +4420,11 @@ void RunAtExit(Environment* env) {


uv_loop_t* GetCurrentEventLoop(v8::Isolate* isolate) {
return Environment::GetCurrent(isolate)->event_loop();
HandleScope handle_scope(isolate);
auto context = isolate->GetCurrentContext();
if (context.IsEmpty())
return nullptr;
return Environment::GetCurrent(context)->event_loop();
}


Expand Down
2 changes: 2 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ NODE_EXTERN void EmitBeforeExit(Environment* env);
NODE_EXTERN int EmitExit(Environment* env);
NODE_EXTERN void RunAtExit(Environment* env);

// This may return nullptr if the current v8::Context is not associated
// with a Node instance.
NODE_EXTERN struct uv_loop_s* GetCurrentEventLoop(v8::Isolate* isolate);

/* Converts a unixtime to V8 Date */
Expand Down
11 changes: 7 additions & 4 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ static
napi_status napi_clear_last_error(napi_env env);

struct napi_env__ {
explicit napi_env__(v8::Isolate* _isolate): isolate(_isolate),
last_error() {}
explicit napi_env__(v8::Isolate* _isolate, uv_loop_t* _loop)
: isolate(_isolate),
last_error(),
loop(_loop) {}
~napi_env__() {
last_exception.Reset();
wrap_template.Reset();
Expand All @@ -43,6 +45,7 @@ struct napi_env__ {
v8::Persistent<v8::ObjectTemplate> accessor_data_template;
napi_extended_error_info last_error;
int open_handle_scopes = 0;
uv_loop_t* loop = nullptr;
};

#define ENV_OBJECT_TEMPLATE(env, prefix, destination, field_count) \
Expand Down Expand Up @@ -771,7 +774,7 @@ napi_env GetEnv(v8::Local<v8::Context> context) {
if (value->IsExternal()) {
result = static_cast<napi_env>(value.As<v8::External>()->Value());
} else {
result = new napi_env__(isolate);
result = new napi_env__(isolate, node::GetCurrentEventLoop(isolate));
auto external = v8::External::New(isolate, result);

// We must also stop hard if the result of assigning the env to the global
Expand Down Expand Up @@ -3404,7 +3407,7 @@ napi_status napi_delete_async_work(napi_env env, napi_async_work work) {
napi_status napi_get_uv_event_loop(napi_env env, uv_loop_t** loop) {
CHECK_ENV(env);
CHECK_ARG(env, loop);
*loop = node::GetCurrentEventLoop(env->isolate);
*loop = env->loop;
return napi_clear_last_error(env);
}

Expand Down
5 changes: 2 additions & 3 deletions test/addons-napi/test_uv_loop/test_uv_loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
#include <assert.h>
#include "../common.h"

template<typename T>
template <typename T>
void* SetImmediate(napi_env env, T&& cb) {
T* ptr = new T(std::move(cb));
uv_loop_t* loop = nullptr;
uv_check_t* check = new uv_check_t;
check->data = static_cast<void*>(ptr);
check->data = ptr;
NAPI_ASSERT(env,
napi_get_uv_event_loop(env, &loop) == napi_ok,
"can get event loop");
uv_check_init(loop, check);
uv_check_start(check, [](uv_check_t* check) {
std::unique_ptr<T> ptr {static_cast<T*>(check->data)};
T cb = std::move(*ptr);
uv_check_stop(check);
uv_close(reinterpret_cast<uv_handle_t*>(check), [](uv_handle_t* handle) {
delete reinterpret_cast<uv_check_t*>(handle);
});
Expand Down