Skip to content

Commit

Permalink
src: make AsyncWrap constructors delegate
Browse files Browse the repository at this point in the history
Currently, there is an AsyncWrap constructor that is only used by
PromiseWrap. This constructor has a body which is very similar
to the other AsyncWrap constructor.

This commit suggests updating the private constructor that is used
by PromiseWrap and also have the second constructor delegate to this
one to avoid the code duplication.

PR-URL: #19366
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
danbev authored and MylesBorins committed Mar 20, 2018
1 parent f490421 commit 054dd28
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
23 changes: 7 additions & 16 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void AsyncWrap::EmitAfter(Environment* env, double async_id) {
class PromiseWrap : public AsyncWrap {
public:
PromiseWrap(Environment* env, Local<Object> object, bool silent)
: AsyncWrap(env, object, silent) {
: AsyncWrap(env, object, PROVIDER_PROMISE, -1, silent) {
MakeWeak(this);
}
size_t self_size() const override { return sizeof(*this); }
Expand Down Expand Up @@ -602,32 +602,23 @@ AsyncWrap::AsyncWrap(Environment* env,
Local<Object> object,
ProviderType provider,
double execution_async_id)
: BaseObject(env, object),
provider_type_(provider) {
CHECK_NE(provider, PROVIDER_NONE);
CHECK_GE(object->InternalFieldCount(), 1);
: AsyncWrap(env, object, provider, execution_async_id, false) {}

// Shift provider value over to prevent id collision.
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider);

// Use AsyncReset() call to execute the init() callbacks.
AsyncReset(execution_async_id);
}


// This is specifically used by the PromiseWrap constructor.
AsyncWrap::AsyncWrap(Environment* env,
Local<Object> object,
ProviderType provider,
double execution_async_id,
bool silent)
: BaseObject(env, object),
provider_type_(PROVIDER_PROMISE) {
provider_type_(provider) {
CHECK_NE(provider, PROVIDER_NONE);
CHECK_GE(object->InternalFieldCount(), 1);

// Shift provider value over to prevent id collision.
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider_type_);

// Use AsyncReset() call to execute the init() callbacks.
AsyncReset(-1, silent);
AsyncReset(execution_async_id, silent);
}


Expand Down
7 changes: 5 additions & 2 deletions src/async_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,11 @@ class AsyncWrap : public BaseObject {
private:
friend class PromiseWrap;

// This is specifically used by the PromiseWrap constructor.
AsyncWrap(Environment* env, v8::Local<v8::Object> promise, bool silent);
AsyncWrap(Environment* env,
v8::Local<v8::Object> promise,
ProviderType provider,
double execution_async_id,
bool silent);
inline AsyncWrap();
const ProviderType provider_type_;
// Because the values may be Reset(), cannot be made const.
Expand Down

0 comments on commit 054dd28

Please sign in to comment.