node/test/addons/async-hooks-promise/binding.cc
Trevor Norris a2fdb76677
async_wrap: use kTotals to enable PromiseHook
Keep a total of enabled hook callbacks in kTotals. This value is used to
track whether node::PromiseHook (src/async-wrap.cc) should be enabled or
disabled.

Don't enable node::PromiseHook, using enablePromiseHook(), until a hook
has been added. Then, using disablePromiseHook(), disable
node::PromiseHook when all hooks have been disabled.

Need to use a native test in order to check the internal field of the
Promise and check for a PromiseWrap.

PR-URL: https://github.com/nodejs/node/pull/13509
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-06-10 11:38:05 +02:00

44 lines
1.0 KiB
C++

#include <node.h>
#include <v8.h>
namespace {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::NewStringType;
using v8::Object;
using v8::Promise;
using v8::String;
using v8::Value;
static void ThrowError(Isolate* isolate, const char* err_msg) {
Local<String> str = String::NewFromOneByte(
isolate,
reinterpret_cast<const uint8_t*>(err_msg),
NewStringType::kNormal).ToLocalChecked();
isolate->ThrowException(str);
}
static void GetPromiseField(const FunctionCallbackInfo<Value>& args) {
auto isolate = args.GetIsolate();
if (!args[0]->IsPromise())
return ThrowError(isolate, "arg is not an Promise");
auto p = args[0].As<Promise>();
if (p->InternalFieldCount() < 1)
return ThrowError(isolate, "Promise has no internal field");
auto l = p->GetInternalField(0);
args.GetReturnValue().Set(l);
}
inline void Initialize(v8::Local<v8::Object> binding) {
NODE_SET_METHOD(binding, "getPromiseField", GetPromiseField);
}
NODE_MODULE(binding, Initialize)
} // anonymous namespace