Skip to content

Commit

Permalink
src: remove duplicate field env in CryptoJob class
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
DavenportEmma authored and codebytere committed Mar 17, 2020
1 parent 29d1cc6 commit 9e2ceab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
42 changes: 21 additions & 21 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6192,9 +6192,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 @@ -6205,8 +6204,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(env()->isolate());
Context::Scope context_scope(env()->context());
CHECK_EQ(false, async_wrap->persistent().IsWeak());
AfterThreadPoolWork();
}
Expand Down Expand Up @@ -6247,12 +6246,12 @@ struct RandomBytesJob : public CryptoJob {

inline void AfterThreadPoolWork() override {
Local<Value> arg = ToResult();
async_wrap->MakeCallback(env->ondone_string(), 1, &arg);
async_wrap->MakeCallback(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(env()->isolate());
return errors.ToException(env()).ToLocalChecked();
}
};

Expand Down Expand Up @@ -6304,11 +6303,11 @@ struct PBKDF2Job : public CryptoJob {

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

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

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

inline void AfterThreadPoolWork() override {
Local<Value> arg = ToResult();
async_wrap->MakeCallback(env->ondone_string(), 1, &arg);
async_wrap->MakeCallback(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(env()->isolate());
return errors.ToException(env()).ToLocalChecked();
}

inline void Cleanse() {
Expand Down Expand Up @@ -6653,22 +6652,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(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(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());
}
}

Expand All @@ -6677,20 +6676,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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,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_;
Expand Down
26 changes: 14 additions & 12 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {

if (!async) {
// sync version
env()->PrintSyncTrace();
AsyncWrap::env()->PrintSyncTrace();
DoThreadPoolWork();
if (CheckError()) {
UpdateWriteResult();
Expand Down Expand Up @@ -397,16 +397,17 @@ 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;

UpdateWriteResult();

// call the write() cb
Local<Function> cb = PersistentToLocal::Default(env()->isolate(),
Local<Function> cb = PersistentToLocal::Default(env->isolate(),
write_js_callback_);
MakeCallback(cb, 0, nullptr);

Expand All @@ -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<Value> 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;
Expand Down Expand Up @@ -454,7 +456,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {

void InitStream(uint32_t* write_result, Local<Function> 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;
}

Expand Down Expand Up @@ -500,7 +502,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
if (report == 0) return;
CHECK_IMPLIES(report < 0, zlib_memory_ >= static_cast<size_t>(-report));
zlib_memory_ += report;
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(report);
AsyncWrap::env()->isolate()->AdjustAmountOfExternalAllocatedMemory(report);
}

struct AllocScope {
Expand Down

0 comments on commit 9e2ceab

Please sign in to comment.