Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit af669a5

Browse files
committed
src: add process._uvHandleCloseCallback property
Called whenever a libuv handle is closed. The callback receives a single argument, the id of the handle that was closed.
1 parent bc0415d commit af669a5

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ namespace node {
252252
V(domain_array, v8::Array) \
253253
V(fs_stats_constructor_function, v8::Function) \
254254
V(gc_info_callback_function, v8::Function) \
255+
V(handle_close_callback, v8::Function) \
255256
V(module_load_list_array, v8::Array) \
256257
V(pipe_constructor_template, v8::FunctionTemplate) \
257258
V(process_object, v8::Object) \

src/handle_wrap.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@
3232
namespace node {
3333

3434
using v8::Context;
35+
using v8::Function;
3536
using v8::FunctionCallbackInfo;
3637
using v8::Handle;
3738
using v8::HandleScope;
3839
using v8::Local;
40+
using v8::Number;
3941
using v8::Object;
42+
using v8::TryCatch;
43+
using v8::Undefined;
4044
using v8::Value;
4145

4246
// defined in node.cc
@@ -130,6 +134,19 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
130134

131135
object->SetAlignedPointerInInternalField(0, NULL);
132136
wrap->persistent().Reset();
137+
138+
Local<Function> handle_close_callback = env->handle_close_callback();
139+
if (handle_close_callback.IsEmpty() == false) {
140+
Local<Value> arg = Number::New(env->isolate(),
141+
static_cast<double>(wrap->id()));
142+
TryCatch try_catch;
143+
handle_close_callback->Call(Undefined(env->isolate()), 1, &arg);
144+
if (try_catch.HasCaught()) {
145+
FatalException(env->isolate(), try_catch);
146+
UNREACHABLE();
147+
}
148+
}
149+
133150
delete wrap;
134151
}
135152

src/node.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,27 @@ static void ProcessTitleSetter(Local<String> property,
22692269
}
22702270

22712271

2272+
static void GetHandleCloseCallback(Local<String>,
2273+
const PropertyCallbackInfo<Value>& info) {
2274+
Environment* env = Environment::GetCurrent(info.GetIsolate());
2275+
HandleScope scope(env->isolate());
2276+
Local<Function> handle_close_callback = env->handle_close_callback();
2277+
if (handle_close_callback.IsEmpty()) return;
2278+
info.GetReturnValue().Set(handle_close_callback);
2279+
}
2280+
2281+
2282+
2283+
static void SetHandleCloseCallback(Local<String>,
2284+
Local<Value> value,
2285+
const PropertyCallbackInfo<void>& info) {
2286+
Environment* env = Environment::GetCurrent(info.GetIsolate());
2287+
HandleScope scope(env->isolate());
2288+
env->set_handle_close_callback(
2289+
value->IsFunction() ? value.As<Function>() : Local<Function>());
2290+
}
2291+
2292+
22722293
static void EnvGetter(Local<String> property,
22732294
const PropertyCallbackInfo<Value>& info) {
22742295
Environment* env = Environment::GetCurrent(info.GetIsolate());
@@ -2595,6 +2616,11 @@ void SetupProcessObject(Environment* env,
25952616
env->uv_context_id_pointer(), kExternalFloat64Array, 1);
25962617
READONLY_PROPERTY(process, "_uvContextId", uv_context_id_obj);
25972618

2619+
process->SetAccessor(
2620+
FIXED_ONE_BYTE_STRING(env->isolate(), "_uvHandleCloseCallback"),
2621+
GetHandleCloseCallback,
2622+
SetHandleCloseCallback);
2623+
25982624
process->SetAccessor(env->title_string(),
25992625
ProcessTitleGetter,
26002626
ProcessTitleSetter);

0 commit comments

Comments
 (0)