Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: remove duplicate field env in CryptoJob class #31554

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6194,9 +6194,8 @@ bool ECDH::IsKeyPairValid() {
// TODO(addaleax): If there is an `AsyncWrap`, it currently has no access to
// this object. This makes proper reporting of memory usage impossible.
struct CryptoJob : public ThreadPoolWork {
Environment* const env;
std::unique_ptr<AsyncWrap> async_wrap;
inline explicit CryptoJob(Environment* env) : ThreadPoolWork(env), env(env) {}
inline explicit CryptoJob(Environment* env) : ThreadPoolWork(env) {}
inline void AfterThreadPoolWork(int status) final;
virtual void AfterThreadPoolWork() = 0;
static inline void Run(std::unique_ptr<CryptoJob> job, Local<Value> wrap);
Expand All @@ -6207,8 +6206,8 @@ void CryptoJob::AfterThreadPoolWork(int status) {
CHECK(status == 0 || status == UV_ECANCELED);
std::unique_ptr<CryptoJob> job(this);
if (status == UV_ECANCELED) return;
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
HandleScope handle_scope(get_env()->isolate());
Context::Scope context_scope(get_env()->context());
CHECK_EQ(false, async_wrap->persistent().IsWeak());
AfterThreadPoolWork();
}
Expand Down Expand Up @@ -6249,12 +6248,12 @@ struct RandomBytesJob : public CryptoJob {

inline void AfterThreadPoolWork() override {
Local<Value> arg = ToResult();
async_wrap->MakeCallback(env->ondone_string(), 1, &arg);
async_wrap->MakeCallback(get_env()->ondone_string(), 1, &arg);
}

inline Local<Value> ToResult() const {
if (errors.empty()) return Undefined(env->isolate());
return errors.ToException(env).ToLocalChecked();
if (errors.empty()) return Undefined(get_env()->isolate());
return errors.ToException(get_env()).ToLocalChecked();
}
};

Expand Down Expand Up @@ -6306,11 +6305,11 @@ struct PBKDF2Job : public CryptoJob {

inline void AfterThreadPoolWork() override {
Local<Value> arg = ToResult();
async_wrap->MakeCallback(env->ondone_string(), 1, &arg);
async_wrap->MakeCallback(get_env()->ondone_string(), 1, &arg);
}

inline Local<Value> ToResult() const {
return Boolean::New(env->isolate(), success.FromJust());
return Boolean::New(get_env()->isolate(), success.FromJust());
}

inline void Cleanse() {
Expand Down Expand Up @@ -6386,12 +6385,12 @@ struct ScryptJob : public CryptoJob {

inline void AfterThreadPoolWork() override {
Local<Value> arg = ToResult();
async_wrap->MakeCallback(env->ondone_string(), 1, &arg);
async_wrap->MakeCallback(get_env()->ondone_string(), 1, &arg);
}

inline Local<Value> ToResult() const {
if (errors.empty()) return Undefined(env->isolate());
return errors.ToException(env).ToLocalChecked();
if (errors.empty()) return Undefined(get_env()->isolate());
return errors.ToException(get_env()).ToLocalChecked();
}

inline void Cleanse() {
Expand Down Expand Up @@ -6720,22 +6719,22 @@ class GenerateKeyPairJob : public CryptoJob {
inline void AfterThreadPoolWork() override {
Local<Value> args[3];
ToResult(&args[0], &args[1], &args[2]);
async_wrap->MakeCallback(env->ondone_string(), 3, args);
async_wrap->MakeCallback(get_env()->ondone_string(), 3, args);
}

inline void ToResult(Local<Value>* err,
Local<Value>* pubkey,
Local<Value>* privkey) {
if (pkey_ && EncodeKeys(pubkey, privkey)) {
CHECK(errors_.empty());
*err = Undefined(env->isolate());
*err = Undefined(get_env()->isolate());
} else {
if (errors_.empty())
errors_.Capture();
CHECK(!errors_.empty());
*err = errors_.ToException(env).ToLocalChecked();
*pubkey = Undefined(env->isolate());
*privkey = Undefined(env->isolate());
*err = errors_.ToException(get_env()).ToLocalChecked();
*pubkey = Undefined(get_env()->isolate());
*privkey = Undefined(get_env()->isolate());
}
}

Expand All @@ -6744,20 +6743,21 @@ class GenerateKeyPairJob : public CryptoJob {
if (public_key_encoding_.output_key_object_) {
// Note that this has the downside of containing sensitive data of the
// private key.
if (!KeyObject::Create(env, kKeyTypePublic, pkey_).ToLocal(pubkey))
if (!KeyObject::Create(get_env(), kKeyTypePublic, pkey_).ToLocal(pubkey))
return false;
} else {
if (!WritePublicKey(env, pkey_.get(), public_key_encoding_)
if (!WritePublicKey(get_env(), pkey_.get(), public_key_encoding_)
.ToLocal(pubkey))
return false;
}

// Now do the same for the private key.
if (private_key_encoding_.output_key_object_) {
if (!KeyObject::Create(env, kKeyTypePrivate, pkey_).ToLocal(privkey))
if (!KeyObject::Create(get_env(), kKeyTypePrivate, pkey_)
.ToLocal(privkey))
return false;
} else {
if (!WritePrivateKey(env, pkey_.get(), private_key_encoding_)
if (!WritePrivateKey(get_env(), pkey_.get(), private_key_encoding_)
.ToLocal(privkey))
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ class ThreadPoolWork {
virtual void DoThreadPoolWork() = 0;
virtual void AfterThreadPoolWork(int status) = 0;

Environment* get_env() const {
jasnell marked this conversation as resolved.
Show resolved Hide resolved
return env_;
}

private:
Environment* env_;
uv_work_t work_req_;
Expand Down