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 env field from CryptoJob #31588

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/js_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class JSStream : public AsyncWrap, public StreamBase {
SET_MEMORY_INFO_NAME(JSStream)
SET_SELF_SIZE(JSStream)

Environment* env() const { return AsyncWrap::env(); }

protected:
JSStream(Environment* env, v8::Local<v8::Object> obj);

Expand Down
41 changes: 20 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(env()->isolate());
Context::Scope context_scope(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(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 @@ -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(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 @@ -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(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 @@ -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(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 @@ -6744,20 +6743,20 @@ 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_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ class FileHandle final : public AsyncWrap, public StreamBase {

static void New(const v8::FunctionCallbackInfo<v8::Value>& args);

Environment* env() const { return AsyncWrap::env(); }

int GetFD() override { return fd_; }

// Will asynchronously close the FD and return a Promise that will
Expand Down
1 change: 1 addition & 0 deletions src/node_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ class Http2Stream : public AsyncWrap,

nghttp2_stream* operator*();

Environment* env() const { return AsyncWrap::env(); }
Http2Session* session() { return session_.get(); }
const Http2Session* session() const { return session_.get(); }

Expand Down
2 changes: 2 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ class ThreadPoolWork {
virtual void DoThreadPoolWork() = 0;
virtual void AfterThreadPoolWork(int status) = 0;

inline Environment* env() const { return env_; }

private:
Environment* env_;
uv_work_t work_req_;
Expand Down
2 changes: 2 additions & 0 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
CHECK_EQ(unreported_allocations_, 0);
}

Environment* env() const { return AsyncWrap::env(); }

void Close() {
if (write_in_progress_) {
pending_close_ = true;
Expand Down
39 changes: 16 additions & 23 deletions src/stream_base-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,17 @@ inline StreamBase::StreamBase(Environment* env) : env_(env) {
PushStreamListener(&default_listener_);
}

inline Environment* StreamBase::stream_env() const {
inline Environment* StreamBase::env() const {
return env_;
}

inline int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
Environment* env = stream_env();

HandleScope handle_scope(env->isolate());
HandleScope handle_scope(env()->isolate());

if (req_wrap_obj.IsEmpty()) {
if (!env->shutdown_wrap_template()
->NewInstance(env->context())
.ToLocal(&req_wrap_obj)) {
if (!env()->shutdown_wrap_template()
->NewInstance(env()->context())
.ToLocal(&req_wrap_obj)) {
return UV_EBUSY;
}
StreamReq::ResetObject(req_wrap_obj);
Expand All @@ -169,8 +167,8 @@ inline int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) {
const char* msg = Error();
if (msg != nullptr) {
req_wrap_obj->Set(
env->context(),
env->error_string(), OneByteString(env->isolate(), msg)).Check();
env()->context(),
env()->error_string(), OneByteString(env()->isolate(), msg)).Check();
ClearError();
}

Expand All @@ -182,7 +180,6 @@ inline StreamWriteResult StreamBase::Write(
size_t count,
uv_stream_t* send_handle,
v8::Local<v8::Object> req_wrap_obj) {
Environment* env = stream_env();
int err;

size_t total_bytes = 0;
Expand All @@ -197,12 +194,12 @@ inline StreamWriteResult StreamBase::Write(
}
}

HandleScope handle_scope(env->isolate());
HandleScope handle_scope(env()->isolate());

if (req_wrap_obj.IsEmpty()) {
if (!env->write_wrap_template()
->NewInstance(env->context())
.ToLocal(&req_wrap_obj)) {
if (!env()->write_wrap_template()
->NewInstance(env()->context())
.ToLocal(&req_wrap_obj)) {
return StreamWriteResult { false, UV_EBUSY, nullptr, 0 };
}
StreamReq::ResetObject(req_wrap_obj);
Expand All @@ -221,9 +218,9 @@ inline StreamWriteResult StreamBase::Write(

const char* msg = Error();
if (msg != nullptr) {
req_wrap_obj->Set(env->context(),
env->error_string(),
OneByteString(env->isolate(), msg)).Check();
req_wrap_obj->Set(env()->context(),
env()->error_string(),
OneByteString(env()->isolate(), msg)).Check();
ClearError();
}

Expand All @@ -235,9 +232,7 @@ SimpleShutdownWrap<OtherBase>::SimpleShutdownWrap(
StreamBase* stream,
v8::Local<v8::Object> req_wrap_obj)
: ShutdownWrap(stream, req_wrap_obj),
OtherBase(stream->stream_env(),
req_wrap_obj,
AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
OtherBase(stream->env(), req_wrap_obj, AsyncWrap::PROVIDER_SHUTDOWNWRAP) {
}

inline ShutdownWrap* StreamBase::CreateShutdownWrap(
Expand All @@ -250,9 +245,7 @@ SimpleWriteWrap<OtherBase>::SimpleWriteWrap(
StreamBase* stream,
v8::Local<v8::Object> req_wrap_obj)
: WriteWrap(stream, req_wrap_obj),
OtherBase(stream->stream_env(),
req_wrap_obj,
AsyncWrap::PROVIDER_WRITEWRAP) {
OtherBase(stream->env(), req_wrap_obj, AsyncWrap::PROVIDER_WRITEWRAP) {
}

inline WriteWrap* StreamBase::CreateWriteWrap(
Expand Down
Loading