Skip to content

Commit 42b8354

Browse files
DavenportEmmacodebytere
authored andcommitted
src: remove duplicate field env in CryptoJob class
Removed field env from cryptojob class, replaced with function env() inherited from ThreadPoolWork PR-URL: #31554 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 9fd1e71 commit 42b8354

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

src/node_crypto.cc

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6197,9 +6197,8 @@ bool ECDH::IsKeyPairValid() {
61976197
// TODO(addaleax): If there is an `AsyncWrap`, it currently has no access to
61986198
// this object. This makes proper reporting of memory usage impossible.
61996199
struct CryptoJob : public ThreadPoolWork {
6200-
Environment* const env;
62016200
std::unique_ptr<AsyncWrap> async_wrap;
6202-
inline explicit CryptoJob(Environment* env) : ThreadPoolWork(env), env(env) {}
6201+
inline explicit CryptoJob(Environment* env) : ThreadPoolWork(env) {}
62036202
inline void AfterThreadPoolWork(int status) final;
62046203
virtual void AfterThreadPoolWork() = 0;
62056204
static inline void Run(std::unique_ptr<CryptoJob> job, Local<Value> wrap);
@@ -6210,8 +6209,8 @@ void CryptoJob::AfterThreadPoolWork(int status) {
62106209
CHECK(status == 0 || status == UV_ECANCELED);
62116210
std::unique_ptr<CryptoJob> job(this);
62126211
if (status == UV_ECANCELED) return;
6213-
HandleScope handle_scope(env->isolate());
6214-
Context::Scope context_scope(env->context());
6212+
HandleScope handle_scope(env()->isolate());
6213+
Context::Scope context_scope(env()->context());
62156214
CHECK_EQ(false, async_wrap->persistent().IsWeak());
62166215
AfterThreadPoolWork();
62176216
}
@@ -6252,12 +6251,12 @@ struct RandomBytesJob : public CryptoJob {
62526251

62536252
inline void AfterThreadPoolWork() override {
62546253
Local<Value> arg = ToResult();
6255-
async_wrap->MakeCallback(env->ondone_string(), 1, &arg);
6254+
async_wrap->MakeCallback(env()->ondone_string(), 1, &arg);
62566255
}
62576256

62586257
inline Local<Value> ToResult() const {
6259-
if (errors.empty()) return Undefined(env->isolate());
6260-
return errors.ToException(env).ToLocalChecked();
6258+
if (errors.empty()) return Undefined(env()->isolate());
6259+
return errors.ToException(env()).ToLocalChecked();
62616260
}
62626261
};
62636262

@@ -6309,11 +6308,11 @@ struct PBKDF2Job : public CryptoJob {
63096308

63106309
inline void AfterThreadPoolWork() override {
63116310
Local<Value> arg = ToResult();
6312-
async_wrap->MakeCallback(env->ondone_string(), 1, &arg);
6311+
async_wrap->MakeCallback(env()->ondone_string(), 1, &arg);
63136312
}
63146313

63156314
inline Local<Value> ToResult() const {
6316-
return Boolean::New(env->isolate(), success.FromJust());
6315+
return Boolean::New(env()->isolate(), success.FromJust());
63176316
}
63186317

63196318
inline void Cleanse() {
@@ -6389,12 +6388,12 @@ struct ScryptJob : public CryptoJob {
63896388

63906389
inline void AfterThreadPoolWork() override {
63916390
Local<Value> arg = ToResult();
6392-
async_wrap->MakeCallback(env->ondone_string(), 1, &arg);
6391+
async_wrap->MakeCallback(env()->ondone_string(), 1, &arg);
63936392
}
63946393

63956394
inline Local<Value> ToResult() const {
6396-
if (errors.empty()) return Undefined(env->isolate());
6397-
return errors.ToException(env).ToLocalChecked();
6395+
if (errors.empty()) return Undefined(env()->isolate());
6396+
return errors.ToException(env()).ToLocalChecked();
63986397
}
63996398

64006399
inline void Cleanse() {
@@ -6723,22 +6722,22 @@ class GenerateKeyPairJob : public CryptoJob {
67236722
inline void AfterThreadPoolWork() override {
67246723
Local<Value> args[3];
67256724
ToResult(&args[0], &args[1], &args[2]);
6726-
async_wrap->MakeCallback(env->ondone_string(), 3, args);
6725+
async_wrap->MakeCallback(env()->ondone_string(), 3, args);
67276726
}
67286727

67296728
inline void ToResult(Local<Value>* err,
67306729
Local<Value>* pubkey,
67316730
Local<Value>* privkey) {
67326731
if (pkey_ && EncodeKeys(pubkey, privkey)) {
67336732
CHECK(errors_.empty());
6734-
*err = Undefined(env->isolate());
6733+
*err = Undefined(env()->isolate());
67356734
} else {
67366735
if (errors_.empty())
67376736
errors_.Capture();
67386737
CHECK(!errors_.empty());
6739-
*err = errors_.ToException(env).ToLocalChecked();
6740-
*pubkey = Undefined(env->isolate());
6741-
*privkey = Undefined(env->isolate());
6738+
*err = errors_.ToException(env()).ToLocalChecked();
6739+
*pubkey = Undefined(env()->isolate());
6740+
*privkey = Undefined(env()->isolate());
67426741
}
67436742
}
67446743

@@ -6747,20 +6746,21 @@ class GenerateKeyPairJob : public CryptoJob {
67476746
if (public_key_encoding_.output_key_object_) {
67486747
// Note that this has the downside of containing sensitive data of the
67496748
// private key.
6750-
if (!KeyObject::Create(env, kKeyTypePublic, pkey_).ToLocal(pubkey))
6749+
if (!KeyObject::Create(env(), kKeyTypePublic, pkey_).ToLocal(pubkey))
67516750
return false;
67526751
} else {
6753-
if (!WritePublicKey(env, pkey_.get(), public_key_encoding_)
6752+
if (!WritePublicKey(env(), pkey_.get(), public_key_encoding_)
67546753
.ToLocal(pubkey))
67556754
return false;
67566755
}
67576756

67586757
// Now do the same for the private key.
67596758
if (private_key_encoding_.output_key_object_) {
6760-
if (!KeyObject::Create(env, kKeyTypePrivate, pkey_).ToLocal(privkey))
6759+
if (!KeyObject::Create(env(), kKeyTypePrivate, pkey_)
6760+
.ToLocal(privkey))
67616761
return false;
67626762
} else {
6763-
if (!WritePrivateKey(env, pkey_.get(), private_key_encoding_)
6763+
if (!WritePrivateKey(env(), pkey_.get(), private_key_encoding_)
67646764
.ToLocal(privkey))
67656765
return false;
67666766
}

src/node_internals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ class ThreadPoolWork {
268268
virtual void DoThreadPoolWork() = 0;
269269
virtual void AfterThreadPoolWork(int status) = 0;
270270

271+
Environment* env() const { return env_; }
272+
271273
private:
272274
Environment* env_;
273275
uv_work_t work_req_;

src/node_zlib.cc

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
348348

349349
if (!async) {
350350
// sync version
351-
env()->PrintSyncTrace();
351+
AsyncWrap::env()->PrintSyncTrace();
352352
DoThreadPoolWork();
353353
if (CheckError()) {
354354
UpdateWriteResult();
@@ -397,16 +397,17 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
397397

398398
CHECK_EQ(status, 0);
399399

400-
HandleScope handle_scope(env()->isolate());
401-
Context::Scope context_scope(env()->context());
400+
Environment* env = AsyncWrap::env();
401+
HandleScope handle_scope(env->isolate());
402+
Context::Scope context_scope(env->context());
402403

403404
if (!CheckError())
404405
return;
405406

406407
UpdateWriteResult();
407408

408409
// call the write() cb
409-
Local<Function> cb = PersistentToLocal::Default(env()->isolate(),
410+
Local<Function> cb = PersistentToLocal::Default(env->isolate(),
410411
write_js_callback_);
411412
MakeCallback(cb, 0, nullptr);
412413

@@ -416,16 +417,17 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
416417

417418
// TODO(addaleax): Switch to modern error system (node_errors.h).
418419
void EmitError(const CompressionError& err) {
420+
Environment* env = AsyncWrap::env();
419421
// If you hit this assertion, you forgot to enter the v8::Context first.
420-
CHECK_EQ(env()->context(), env()->isolate()->GetCurrentContext());
422+
CHECK_EQ(env->context(), env->isolate()->GetCurrentContext());
421423

422-
HandleScope scope(env()->isolate());
424+
HandleScope scope(env->isolate());
423425
Local<Value> args[3] = {
424-
OneByteString(env()->isolate(), err.message),
425-
Integer::New(env()->isolate(), err.err),
426-
OneByteString(env()->isolate(), err.code)
426+
OneByteString(env->isolate(), err.message),
427+
Integer::New(env->isolate(), err.err),
428+
OneByteString(env->isolate(), err.code)
427429
};
428-
MakeCallback(env()->onerror_string(), arraysize(args), args);
430+
MakeCallback(env->onerror_string(), arraysize(args), args);
429431

430432
// no hope of rescue.
431433
write_in_progress_ = false;
@@ -454,7 +456,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
454456

455457
void InitStream(uint32_t* write_result, Local<Function> write_js_callback) {
456458
write_result_ = write_result;
457-
write_js_callback_.Reset(env()->isolate(), write_js_callback);
459+
write_js_callback_.Reset(AsyncWrap::env()->isolate(), write_js_callback);
458460
init_done_ = true;
459461
}
460462

@@ -500,7 +502,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
500502
if (report == 0) return;
501503
CHECK_IMPLIES(report < 0, zlib_memory_ >= static_cast<size_t>(-report));
502504
zlib_memory_ += report;
503-
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(report);
505+
AsyncWrap::env()->isolate()->AdjustAmountOfExternalAllocatedMemory(report);
504506
}
505507

506508
struct AllocScope {

0 commit comments

Comments
 (0)