diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 7f9818bba33c2a..92760fb8c8577b 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -6197,9 +6197,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 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 job, Local wrap); @@ -6210,8 +6209,8 @@ void CryptoJob::AfterThreadPoolWork(int status) { CHECK(status == 0 || status == UV_ECANCELED); std::unique_ptr job(this); if (status == UV_ECANCELED) return; - HandleScope handle_scope(env->isolate()); - Context::Scope context_scope(env->context()); + HandleScope handle_scope(env()->isolate()); + Context::Scope context_scope(env()->context()); CHECK_EQ(false, async_wrap->persistent().IsWeak()); AfterThreadPoolWork(); } @@ -6252,12 +6251,12 @@ struct RandomBytesJob : public CryptoJob { inline void AfterThreadPoolWork() override { Local arg = ToResult(); - async_wrap->MakeCallback(env->ondone_string(), 1, &arg); + async_wrap->MakeCallback(env()->ondone_string(), 1, &arg); } inline Local ToResult() const { - if (errors.empty()) return Undefined(env->isolate()); - return errors.ToException(env).ToLocalChecked(); + if (errors.empty()) return Undefined(env()->isolate()); + return errors.ToException(env()).ToLocalChecked(); } }; @@ -6309,11 +6308,11 @@ struct PBKDF2Job : public CryptoJob { inline void AfterThreadPoolWork() override { Local arg = ToResult(); - async_wrap->MakeCallback(env->ondone_string(), 1, &arg); + async_wrap->MakeCallback(env()->ondone_string(), 1, &arg); } inline Local ToResult() const { - return Boolean::New(env->isolate(), success.FromJust()); + return Boolean::New(env()->isolate(), success.FromJust()); } inline void Cleanse() { @@ -6389,12 +6388,12 @@ struct ScryptJob : public CryptoJob { inline void AfterThreadPoolWork() override { Local arg = ToResult(); - async_wrap->MakeCallback(env->ondone_string(), 1, &arg); + async_wrap->MakeCallback(env()->ondone_string(), 1, &arg); } inline Local ToResult() const { - if (errors.empty()) return Undefined(env->isolate()); - return errors.ToException(env).ToLocalChecked(); + if (errors.empty()) return Undefined(env()->isolate()); + return errors.ToException(env()).ToLocalChecked(); } inline void Cleanse() { @@ -6723,7 +6722,7 @@ class GenerateKeyPairJob : public CryptoJob { inline void AfterThreadPoolWork() override { Local args[3]; ToResult(&args[0], &args[1], &args[2]); - async_wrap->MakeCallback(env->ondone_string(), 3, args); + async_wrap->MakeCallback(env()->ondone_string(), 3, args); } inline void ToResult(Local* err, @@ -6731,14 +6730,14 @@ class GenerateKeyPairJob : public CryptoJob { Local* privkey) { if (pkey_ && EncodeKeys(pubkey, privkey)) { CHECK(errors_.empty()); - *err = Undefined(env->isolate()); + *err = Undefined(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(env()).ToLocalChecked(); + *pubkey = Undefined(env()->isolate()); + *privkey = Undefined(env()->isolate()); } } @@ -6747,20 +6746,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(env(), kKeyTypePublic, pkey_).ToLocal(pubkey)) return false; } else { - if (!WritePublicKey(env, pkey_.get(), public_key_encoding_) + if (!WritePublicKey(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(env(), kKeyTypePrivate, pkey_) + .ToLocal(privkey)) return false; } else { - if (!WritePrivateKey(env, pkey_.get(), private_key_encoding_) + if (!WritePrivateKey(env(), pkey_.get(), private_key_encoding_) .ToLocal(privkey)) return false; } diff --git a/src/node_internals.h b/src/node_internals.h index 6e50b22af2f915..e01d703a580bfe 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -268,6 +268,8 @@ class ThreadPoolWork { virtual void DoThreadPoolWork() = 0; virtual void AfterThreadPoolWork(int status) = 0; + Environment* env() const { return env_; } + private: Environment* env_; uv_work_t work_req_; diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 739e36d69911b2..1b7fd788b959f9 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -348,7 +348,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { if (!async) { // sync version - env()->PrintSyncTrace(); + AsyncWrap::env()->PrintSyncTrace(); DoThreadPoolWork(); if (CheckError()) { UpdateWriteResult(); @@ -397,8 +397,9 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { CHECK_EQ(status, 0); - HandleScope handle_scope(env()->isolate()); - Context::Scope context_scope(env()->context()); + Environment* env = AsyncWrap::env(); + HandleScope handle_scope(env->isolate()); + Context::Scope context_scope(env->context()); if (!CheckError()) return; @@ -406,7 +407,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { UpdateWriteResult(); // call the write() cb - Local cb = PersistentToLocal::Default(env()->isolate(), + Local cb = PersistentToLocal::Default(env->isolate(), write_js_callback_); MakeCallback(cb, 0, nullptr); @@ -416,16 +417,17 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { // TODO(addaleax): Switch to modern error system (node_errors.h). void EmitError(const CompressionError& err) { + Environment* env = AsyncWrap::env(); // If you hit this assertion, you forgot to enter the v8::Context first. - CHECK_EQ(env()->context(), env()->isolate()->GetCurrentContext()); + CHECK_EQ(env->context(), env->isolate()->GetCurrentContext()); - HandleScope scope(env()->isolate()); + HandleScope scope(env->isolate()); Local args[3] = { - OneByteString(env()->isolate(), err.message), - Integer::New(env()->isolate(), err.err), - OneByteString(env()->isolate(), err.code) + OneByteString(env->isolate(), err.message), + Integer::New(env->isolate(), err.err), + OneByteString(env->isolate(), err.code) }; - MakeCallback(env()->onerror_string(), arraysize(args), args); + MakeCallback(env->onerror_string(), arraysize(args), args); // no hope of rescue. write_in_progress_ = false; @@ -454,7 +456,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { void InitStream(uint32_t* write_result, Local write_js_callback) { write_result_ = write_result; - write_js_callback_.Reset(env()->isolate(), write_js_callback); + write_js_callback_.Reset(AsyncWrap::env()->isolate(), write_js_callback); init_done_ = true; } @@ -500,7 +502,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { if (report == 0) return; CHECK_IMPLIES(report < 0, zlib_memory_ >= static_cast(-report)); zlib_memory_ += report; - env()->isolate()->AdjustAmountOfExternalAllocatedMemory(report); + AsyncWrap::env()->isolate()->AdjustAmountOfExternalAllocatedMemory(report); } struct AllocScope {