async_hooks: rename PromiseWrap.parentId
Rename the `parentId` property on the PromiseWrap object to a `isChainedPromise` property. The former wasn't quite useful as it was always defined to be the same value as the trigger id available in the init hook. Instead rename the property to be closer to the information it communicates: whether the promise is a chained promise or not. PR-URL: https://github.com/nodejs/node/pull/18633 Fixes: https://github.com/nodejs/node/issues/18470 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
This commit is contained in:
parent
bd4773a043
commit
9f6a565901
@ -301,10 +301,10 @@ and document their own resource objects. For example, such a resource object
|
|||||||
could contain the SQL query being executed.
|
could contain the SQL query being executed.
|
||||||
|
|
||||||
In the case of Promises, the `resource` object will have `promise` property
|
In the case of Promises, the `resource` object will have `promise` property
|
||||||
that refers to the Promise that is being initialized, and a `parentId` property
|
that refers to the Promise that is being initialized, and a `isChainedPromise`
|
||||||
set to the `asyncId` of a parent Promise, if there is one, and `undefined`
|
property, set to `true` if the promise has a parent promise, and `false`
|
||||||
otherwise. For example, in the case of `b = a.then(handler)`, `a` is considered
|
otherwise. For example, in the case of `b = a.then(handler)`, `a` is considered
|
||||||
a parent Promise of `b`.
|
a parent Promise of `b`. Here, `b` is considered a chained promise.
|
||||||
|
|
||||||
In some cases the resource object is reused for performance reasons, it is
|
In some cases the resource object is reused for performance reasons, it is
|
||||||
thus not safe to use it as a key in a `WeakMap` or add properties to it.
|
thus not safe to use it as a key in a `WeakMap` or add properties to it.
|
||||||
|
@ -245,7 +245,7 @@ class PromiseWrap : public AsyncWrap {
|
|||||||
size_t self_size() const override { return sizeof(*this); }
|
size_t self_size() const override { return sizeof(*this); }
|
||||||
|
|
||||||
static constexpr int kPromiseField = 1;
|
static constexpr int kPromiseField = 1;
|
||||||
static constexpr int kParentAsyncIdField = 2;
|
static constexpr int kIsChainedPromiseField = 2;
|
||||||
static constexpr int kInternalFieldCount = 3;
|
static constexpr int kInternalFieldCount = 3;
|
||||||
|
|
||||||
static PromiseWrap* New(Environment* env,
|
static PromiseWrap* New(Environment* env,
|
||||||
@ -254,7 +254,7 @@ class PromiseWrap : public AsyncWrap {
|
|||||||
bool silent);
|
bool silent);
|
||||||
static void GetPromise(Local<String> property,
|
static void GetPromise(Local<String> property,
|
||||||
const PropertyCallbackInfo<Value>& info);
|
const PropertyCallbackInfo<Value>& info);
|
||||||
static void getParentAsyncId(Local<String> property,
|
static void getIsChainedPromise(Local<String> property,
|
||||||
const PropertyCallbackInfo<Value>& info);
|
const PropertyCallbackInfo<Value>& info);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -265,11 +265,10 @@ PromiseWrap* PromiseWrap::New(Environment* env,
|
|||||||
Local<Object> object = env->promise_wrap_template()
|
Local<Object> object = env->promise_wrap_template()
|
||||||
->NewInstance(env->context()).ToLocalChecked();
|
->NewInstance(env->context()).ToLocalChecked();
|
||||||
object->SetInternalField(PromiseWrap::kPromiseField, promise);
|
object->SetInternalField(PromiseWrap::kPromiseField, promise);
|
||||||
if (parent_wrap != nullptr) {
|
object->SetInternalField(PromiseWrap::kIsChainedPromiseField,
|
||||||
object->SetInternalField(PromiseWrap::kParentAsyncIdField,
|
parent_wrap != nullptr ?
|
||||||
Number::New(env->isolate(),
|
v8::True(env->isolate()) :
|
||||||
parent_wrap->get_async_id()));
|
v8::False(env->isolate()));
|
||||||
}
|
|
||||||
CHECK_EQ(promise->GetAlignedPointerFromInternalField(0), nullptr);
|
CHECK_EQ(promise->GetAlignedPointerFromInternalField(0), nullptr);
|
||||||
promise->SetInternalField(0, object);
|
promise->SetInternalField(0, object);
|
||||||
return new PromiseWrap(env, object, silent);
|
return new PromiseWrap(env, object, silent);
|
||||||
@ -280,10 +279,10 @@ void PromiseWrap::GetPromise(Local<String> property,
|
|||||||
info.GetReturnValue().Set(info.Holder()->GetInternalField(kPromiseField));
|
info.GetReturnValue().Set(info.Holder()->GetInternalField(kPromiseField));
|
||||||
}
|
}
|
||||||
|
|
||||||
void PromiseWrap::getParentAsyncId(Local<String> property,
|
void PromiseWrap::getIsChainedPromise(Local<String> property,
|
||||||
const PropertyCallbackInfo<Value>& info) {
|
const PropertyCallbackInfo<Value>& info) {
|
||||||
info.GetReturnValue().Set(
|
info.GetReturnValue().Set(
|
||||||
info.Holder()->GetInternalField(kParentAsyncIdField));
|
info.Holder()->GetInternalField(kIsChainedPromiseField));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PromiseHook(PromiseHookType type, Local<Promise> promise,
|
static void PromiseHook(PromiseHookType type, Local<Promise> promise,
|
||||||
@ -383,8 +382,8 @@ static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
|
|||||||
FIXED_ONE_BYTE_STRING(env->isolate(), "promise"),
|
FIXED_ONE_BYTE_STRING(env->isolate(), "promise"),
|
||||||
PromiseWrap::GetPromise);
|
PromiseWrap::GetPromise);
|
||||||
promise_wrap_template->SetAccessor(
|
promise_wrap_template->SetAccessor(
|
||||||
FIXED_ONE_BYTE_STRING(env->isolate(), "parentId"),
|
FIXED_ONE_BYTE_STRING(env->isolate(), "isChainedPromise"),
|
||||||
PromiseWrap::getParentAsyncId);
|
PromiseWrap::getIsChainedPromise);
|
||||||
env->set_promise_wrap_template(promise_wrap_template);
|
env->set_promise_wrap_template(promise_wrap_template);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,8 @@ const a = Promise.resolve(42);
|
|||||||
const b = a.then(common.mustCall());
|
const b = a.then(common.mustCall());
|
||||||
|
|
||||||
assert.strictEqual(initCalls[0].triggerId, 1);
|
assert.strictEqual(initCalls[0].triggerId, 1);
|
||||||
assert.strictEqual(initCalls[0].resource.parentId, undefined);
|
assert.strictEqual(initCalls[0].resource.isChainedPromise, false);
|
||||||
assert.strictEqual(initCalls[0].resource.promise, a);
|
assert.strictEqual(initCalls[0].resource.promise, a);
|
||||||
assert.strictEqual(initCalls[1].triggerId, initCalls[0].id);
|
assert.strictEqual(initCalls[1].triggerId, initCalls[0].id);
|
||||||
assert.strictEqual(initCalls[1].resource.parentId, initCalls[0].id);
|
assert.strictEqual(initCalls[1].resource.isChainedPromise, true);
|
||||||
assert.strictEqual(initCalls[1].resource.promise, b);
|
assert.strictEqual(initCalls[1].resource.promise, b);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user