src: add helper for addons to get the event loop
Add a utility functions for addons to use when they need a reference to the current event loop. Currently, `uv_default_loop()` works if the embedder is the single-threaded default node executable, but even without the presence of e.g. workers that might not really an API guarantee for when Node is embedded. PR-URL: https://github.com/nodejs/node/pull/17109 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
804eb3cd73
commit
c0f3bc24de
@ -4332,6 +4332,15 @@ void RunAtExit(Environment* env) {
|
||||
}
|
||||
|
||||
|
||||
uv_loop_t* GetCurrentEventLoop(v8::Isolate* isolate) {
|
||||
HandleScope handle_scope(isolate);
|
||||
auto context = isolate->GetCurrentContext();
|
||||
if (context.IsEmpty())
|
||||
return nullptr;
|
||||
return Environment::GetCurrent(context)->event_loop();
|
||||
}
|
||||
|
||||
|
||||
static uv_key_t thread_local_env;
|
||||
|
||||
|
||||
|
@ -248,6 +248,10 @@ 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 */
|
||||
#define NODE_UNIXTIME_V8(t) v8::Date::New(v8::Isolate::GetCurrent(), \
|
||||
1000 * static_cast<double>(t))
|
||||
|
@ -77,7 +77,7 @@ void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
|
||||
req->callback.Reset(isolate, callback);
|
||||
|
||||
uv_queue_work(uv_default_loop(),
|
||||
uv_queue_work(node::GetCurrentEventLoop(isolate),
|
||||
&req->req,
|
||||
DoAsync,
|
||||
(uv_after_work_cb)AfterAsync<use_makecallback>);
|
||||
|
@ -52,7 +52,10 @@ static void TestResolveAsync(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
|
||||
uv_work_t* req = new uv_work_t;
|
||||
|
||||
uv_queue_work(uv_default_loop(), req, [](uv_work_t*) {}, Callback);
|
||||
uv_queue_work(node::GetCurrentEventLoop(isolate),
|
||||
req,
|
||||
[](uv_work_t*) {},
|
||||
Callback);
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise::Resolver> local =
|
||||
|
Loading…
x
Reference in New Issue
Block a user