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

Commit

Permalink
src: add process._uvHandleCloseCallback property
Browse files Browse the repository at this point in the history
Called whenever a libuv handle is closed.  The callback receives a
single argument, the id of the handle that was closed.
  • Loading branch information
bnoordhuis committed Aug 10, 2014
1 parent bc0415d commit af669a5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ namespace node {
V(domain_array, v8::Array) \
V(fs_stats_constructor_function, v8::Function) \
V(gc_info_callback_function, v8::Function) \
V(handle_close_callback, v8::Function) \
V(module_load_list_array, v8::Array) \
V(pipe_constructor_template, v8::FunctionTemplate) \
V(process_object, v8::Object) \
Expand Down
17 changes: 17 additions & 0 deletions src/handle_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
namespace node {

using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Handle;
using v8::HandleScope;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::TryCatch;
using v8::Undefined;
using v8::Value;

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

object->SetAlignedPointerInInternalField(0, NULL);
wrap->persistent().Reset();

Local<Function> handle_close_callback = env->handle_close_callback();
if (handle_close_callback.IsEmpty() == false) {
Local<Value> arg = Number::New(env->isolate(),
static_cast<double>(wrap->id()));
TryCatch try_catch;
handle_close_callback->Call(Undefined(env->isolate()), 1, &arg);
if (try_catch.HasCaught()) {
FatalException(env->isolate(), try_catch);
UNREACHABLE();
}
}

delete wrap;
}

Expand Down
26 changes: 26 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2269,6 +2269,27 @@ static void ProcessTitleSetter(Local<String> property,
}


static void GetHandleCloseCallback(Local<String>,
const PropertyCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info.GetIsolate());
HandleScope scope(env->isolate());
Local<Function> handle_close_callback = env->handle_close_callback();
if (handle_close_callback.IsEmpty()) return;
info.GetReturnValue().Set(handle_close_callback);
}



static void SetHandleCloseCallback(Local<String>,
Local<Value> value,
const PropertyCallbackInfo<void>& info) {
Environment* env = Environment::GetCurrent(info.GetIsolate());
HandleScope scope(env->isolate());
env->set_handle_close_callback(
value->IsFunction() ? value.As<Function>() : Local<Function>());
}


static void EnvGetter(Local<String> property,
const PropertyCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info.GetIsolate());
Expand Down Expand Up @@ -2595,6 +2616,11 @@ void SetupProcessObject(Environment* env,
env->uv_context_id_pointer(), kExternalFloat64Array, 1);
READONLY_PROPERTY(process, "_uvContextId", uv_context_id_obj);

process->SetAccessor(
FIXED_ONE_BYTE_STRING(env->isolate(), "_uvHandleCloseCallback"),
GetHandleCloseCallback,
SetHandleCloseCallback);

process->SetAccessor(env->title_string(),
ProcessTitleGetter,
ProcessTitleSetter);
Expand Down

0 comments on commit af669a5

Please sign in to comment.