diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-07-15 23:17:45 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-08-01 01:51:43 +0300 |
commit | 5207dec0175de92116262e8382d6ac57def3a203 (patch) | |
tree | e90e855f7a8a228e00fe0d49fb311b319de74c3d /src/stream_pipe.cc | |
parent | 61f3a5c60ad78506e9e0caae061a04ccab878ca1 (diff) |
src: allow generic C++ callables in SetImmediate()
Modify the native `SetImmediate()` functions to take generic C++
callables as arguments. This makes passing arguments to the callback
easier, and in particular, it allows passing `std::unique_ptr`s
directly, which in turn makes sure that the data they point to is
deleted if the `Environment` is torn down before the callback can run.
PR-URL: https://github.com/nodejs/node/pull/28704
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/stream_pipe.cc')
-rw-r--r-- | src/stream_pipe.cc | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/src/stream_pipe.cc b/src/stream_pipe.cc index a6b975ab26a..be7fc882ea5 100644 --- a/src/stream_pipe.cc +++ b/src/stream_pipe.cc @@ -71,18 +71,16 @@ void StreamPipe::Unpipe() { // Delay the JS-facing part with SetImmediate, because this might be from // inside the garbage collector, so we can’t run JS here. HandleScope handle_scope(env()->isolate()); - env()->SetImmediate([](Environment* env, void* data) { - StreamPipe* pipe = static_cast<StreamPipe*>(data); - + env()->SetImmediate([this](Environment* env) { HandleScope handle_scope(env->isolate()); Context::Scope context_scope(env->context()); - Local<Object> object = pipe->object(); + Local<Object> object = this->object(); Local<Value> onunpipe; if (!object->Get(env->context(), env->onunpipe_string()).ToLocal(&onunpipe)) return; if (onunpipe->IsFunction() && - pipe->MakeCallback(onunpipe.As<Function>(), 0, nullptr).IsEmpty()) { + MakeCallback(onunpipe.As<Function>(), 0, nullptr).IsEmpty()) { return; } @@ -107,7 +105,7 @@ void StreamPipe::Unpipe() { .IsNothing()) { return; } - }, static_cast<void*>(this), object()); + }, object()); } uv_buf_t StreamPipe::ReadableListener::OnStreamAlloc(size_t suggested_size) { |