Skip to content

Commit 96c3224

Browse files
committedMar 21, 2019
src: remove AddPromiseHook()
Remove this, as the underlying `Isolate::SetPromiseHook()` may be removed as well in its current form in the future, and `async_hooks` also serves this use case. Refs: https://docs.google.com/document/d/1g8OrG5lMIUhRn1zbkutgY83MiTSMx-0NHDs8Bf-nXxM/ Refs: #26529 PR-URL: #26574 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]>
1 parent 1500e5d commit 96c3224

File tree

7 files changed

+35
-113
lines changed

7 files changed

+35
-113
lines changed
 

‎src/api/hooks.cc

-6
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ int EmitExit(Environment* env) {
6565
.ToChecked();
6666
}
6767

68-
void AddPromiseHook(Isolate* isolate, promise_hook_func fn, void* arg) {
69-
Environment* env = Environment::GetCurrent(isolate);
70-
CHECK_NOT_NULL(env);
71-
env->AddPromiseHook(fn, arg);
72-
}
73-
7468
void AddEnvironmentCleanupHook(Isolate* isolate,
7569
void (*fun)(void* arg),
7670
void* arg) {

‎src/async_wrap.cc

+17-9
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,14 @@ static PromiseWrap* extractPromiseWrap(Local<Promise> promise) {
227227
}
228228

229229
static void PromiseHook(PromiseHookType type, Local<Promise> promise,
230-
Local<Value> parent, void* arg) {
231-
Environment* env = static_cast<Environment*>(arg);
230+
Local<Value> parent) {
231+
Local<Context> context = promise->CreationContext();
232+
233+
Environment* env = Environment::GetCurrent(context);
234+
if (env == nullptr) return;
235+
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
236+
"EnvPromiseHook", env);
237+
232238
PromiseWrap* wrap = extractPromiseWrap(promise);
233239
if (type == PromiseHookType::kInit || wrap == nullptr) {
234240
bool silent = type != PromiseHookType::kInit;
@@ -318,20 +324,22 @@ static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
318324

319325

320326
static void EnablePromiseHook(const FunctionCallbackInfo<Value>& args) {
321-
Environment* env = Environment::GetCurrent(args);
322-
env->AddPromiseHook(PromiseHook, static_cast<void*>(env));
327+
args.GetIsolate()->SetPromiseHook(PromiseHook);
323328
}
324329

325330

326331
static void DisablePromiseHook(const FunctionCallbackInfo<Value>& args) {
327-
Environment* env = Environment::GetCurrent(args);
332+
Isolate* isolate = args.GetIsolate();
328333

329334
// Delay the call to `RemovePromiseHook` because we might currently be
330335
// between the `before` and `after` calls of a Promise.
331-
env->isolate()->EnqueueMicrotask([](void* data) {
332-
Environment* env = static_cast<Environment*>(data);
333-
env->RemovePromiseHook(PromiseHook, data);
334-
}, static_cast<void*>(env));
336+
isolate->EnqueueMicrotask([](void* data) {
337+
// The per-Isolate API provides no way of knowing whether there are multiple
338+
// users of the PromiseHook. That hopefully goes away when V8 introduces
339+
// a per-context API.
340+
Isolate* isolate = static_cast<Isolate*>(data);
341+
isolate->SetPromiseHook(nullptr);
342+
}, static_cast<void*>(isolate));
335343
}
336344

337345

‎src/env-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ inline void Environment::AssignToContext(v8::Local<v8::Context> context,
286286
const ContextInfo& info) {
287287
context->SetAlignedPointerInEmbedderData(
288288
ContextEmbedderIndex::kEnvironment, this);
289-
// Used by EnvPromiseHook to know that we are on a node context.
289+
// Used by Environment::GetCurrent to know that we are on a node context.
290290
context->SetAlignedPointerInEmbedderData(
291291
ContextEmbedderIndex::kContextTag, Environment::kNodeContextTagPtr);
292292
#if HAVE_INSPECTOR

‎src/env.cc

-71
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ using v8::NewStringType;
3939
using v8::Number;
4040
using v8::Object;
4141
using v8::Private;
42-
using v8::Promise;
43-
using v8::PromiseHookType;
4442
using v8::StackFrame;
4543
using v8::StackTrace;
4644
using v8::String;
@@ -50,25 +48,6 @@ using v8::Undefined;
5048
using v8::Value;
5149
using worker::Worker;
5250

53-
// TODO(@jasnell): Likely useful to move this to util or node_internal to
54-
// allow reuse. But since we're not reusing it yet...
55-
class TraceEventScope {
56-
public:
57-
TraceEventScope(const char* category,
58-
const char* name,
59-
void* id) : category_(category), name_(name), id_(id) {
60-
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(category_, name_, id_);
61-
}
62-
~TraceEventScope() {
63-
TRACE_EVENT_NESTABLE_ASYNC_END0(category_, name_, id_);
64-
}
65-
66-
private:
67-
const char* category_;
68-
const char* name_;
69-
void* id_;
70-
};
71-
7251
int const Environment::kNodeContextTag = 0x6e6f64;
7352
void* const Environment::kNodeContextTagPtr = const_cast<void*>(
7453
static_cast<const void*>(&Environment::kNodeContextTag));
@@ -590,56 +569,6 @@ void Environment::AtExit(void (*cb)(void* arg), void* arg) {
590569
at_exit_functions_.push_back(ExitCallback{cb, arg});
591570
}
592571

593-
void Environment::AddPromiseHook(promise_hook_func fn, void* arg) {
594-
auto it = std::find_if(
595-
promise_hooks_.begin(), promise_hooks_.end(),
596-
[&](const PromiseHookCallback& hook) {
597-
return hook.cb_ == fn && hook.arg_ == arg;
598-
});
599-
if (it != promise_hooks_.end()) {
600-
it->enable_count_++;
601-
return;
602-
}
603-
promise_hooks_.push_back(PromiseHookCallback{fn, arg, 1});
604-
605-
if (promise_hooks_.size() == 1) {
606-
isolate_->SetPromiseHook(EnvPromiseHook);
607-
}
608-
}
609-
610-
bool Environment::RemovePromiseHook(promise_hook_func fn, void* arg) {
611-
auto it = std::find_if(
612-
promise_hooks_.begin(), promise_hooks_.end(),
613-
[&](const PromiseHookCallback& hook) {
614-
return hook.cb_ == fn && hook.arg_ == arg;
615-
});
616-
617-
if (it == promise_hooks_.end()) return false;
618-
619-
if (--it->enable_count_ > 0) return true;
620-
621-
promise_hooks_.erase(it);
622-
if (promise_hooks_.empty()) {
623-
isolate_->SetPromiseHook(nullptr);
624-
}
625-
626-
return true;
627-
}
628-
629-
void Environment::EnvPromiseHook(PromiseHookType type,
630-
Local<Promise> promise,
631-
Local<Value> parent) {
632-
Local<Context> context = promise->CreationContext();
633-
634-
Environment* env = Environment::GetCurrent(context);
635-
if (env == nullptr) return;
636-
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
637-
"EnvPromiseHook", env);
638-
for (const PromiseHookCallback& hook : env->promise_hooks_) {
639-
hook.cb_(type, promise, parent, hook.arg_);
640-
}
641-
}
642-
643572
void Environment::RunAndClearNativeImmediates() {
644573
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
645574
"RunAndClearNativeImmediates", this);

‎src/env.h

-13
Original file line numberDiff line numberDiff line change
@@ -959,8 +959,6 @@ class Environment {
959959
inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; }
960960
inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; }
961961

962-
void AddPromiseHook(promise_hook_func fn, void* arg);
963-
bool RemovePromiseHook(promise_hook_func fn, void* arg);
964962
inline bool EmitProcessEnvWarning() {
965963
bool current_value = emit_env_nonstring_warning_;
966964
emit_env_nonstring_warning_ = false;
@@ -1164,13 +1162,6 @@ class Environment {
11641162

11651163
std::list<ExitCallback> at_exit_functions_;
11661164

1167-
struct PromiseHookCallback {
1168-
promise_hook_func cb_;
1169-
void* arg_;
1170-
size_t enable_count_;
1171-
};
1172-
std::vector<PromiseHookCallback> promise_hooks_;
1173-
11741165
struct NativeImmediateCallback {
11751166
native_immediate_callback cb_;
11761167
void* data_;
@@ -1214,10 +1205,6 @@ class Environment {
12141205
// Used by embedders to shutdown running Node instance.
12151206
AsyncRequest thread_stopper_;
12161207

1217-
static void EnvPromiseHook(v8::PromiseHookType type,
1218-
v8::Local<v8::Promise> promise,
1219-
v8::Local<v8::Value> parent);
1220-
12211208
template <typename T>
12221209
void ForEachBaseObject(T&& iterator);
12231210

‎src/node.h

-12
Original file line numberDiff line numberDiff line change
@@ -639,24 +639,12 @@ NODE_EXTERN void AtExit(Environment* env,
639639
void (*cb)(void* arg),
640640
void* arg = nullptr);
641641

642-
typedef void (*promise_hook_func) (v8::PromiseHookType type,
643-
v8::Local<v8::Promise> promise,
644-
v8::Local<v8::Value> parent,
645-
void* arg);
646-
647642
typedef double async_id;
648643
struct async_context {
649644
::node::async_id async_id;
650645
::node::async_id trigger_async_id;
651646
};
652647

653-
/* Registers an additional v8::PromiseHook wrapper. This API exists because V8
654-
* itself supports only a single PromiseHook. */
655-
NODE_DEPRECATED("Use async_hooks directly instead",
656-
NODE_EXTERN void AddPromiseHook(v8::Isolate* isolate,
657-
promise_hook_func fn,
658-
void* arg));
659-
660648
/* This is a lot like node::AtExit, except that the hooks added via this
661649
* function are run before the AtExit ones and will always be registered
662650
* for the current Environment instance.

‎src/node_internals.h

+17-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ v8::MaybeLocal<v8::Object> GetPerContextExports(v8::Local<v8::Context> context);
301301
namespace profiler {
302302
void StartCoverageCollection(Environment* env);
303303
}
304-
305304
#ifdef _WIN32
306305
typedef SYSTEMTIME TIME_TYPE;
307306
#else // UNIX, OSX
@@ -336,6 +335,23 @@ class DiagnosticFilename {
336335
std::string filename_;
337336
};
338337

338+
class TraceEventScope {
339+
public:
340+
TraceEventScope(const char* category,
341+
const char* name,
342+
void* id) : category_(category), name_(name), id_(id) {
343+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(category_, name_, id_);
344+
}
345+
~TraceEventScope() {
346+
TRACE_EVENT_NESTABLE_ASYNC_END0(category_, name_, id_);
347+
}
348+
349+
private:
350+
const char* category_;
351+
const char* name_;
352+
void* id_;
353+
};
354+
339355
} // namespace node
340356

341357
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)
Please sign in to comment.