Skip to content

Commit

Permalink
src: convert more Maybe<bool> to Maybe<void>
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Sep 7, 2024
1 parent 70dcde2 commit a0c36cb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 60 deletions.
20 changes: 13 additions & 7 deletions src/crypto/crypto_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,8 @@ class CipherJob final : public CryptoJob<CipherTraits> {
}
}

v8::Maybe<bool> ToResult(
v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) override {
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) override {
Environment* env = AsyncWrap::env();
CryptoErrorStore* errors = CryptoJob<CipherTraits>::errors();

Expand All @@ -262,11 +261,18 @@ class CipherJob final : public CryptoJob<CipherTraits> {
CHECK(errors->Empty());
*err = v8::Undefined(env->isolate());
*result = out_.ToArrayBuffer(env);
return v8::Just(!result->IsEmpty());
if (result->IsEmpty()) {
return v8::Nothing<void>();
}
} else {
*result = v8::Undefined(env->isolate());
if (!errors->ToException(env).ToLocal(err)) {
return v8::Nothing<void>();
}
}

*result = v8::Undefined(env->isolate());
return v8::Just(errors->ToException(env).ToLocal(err));
CHECK(!result->IsEmpty());
CHECK(!err->IsEmpty());
return v8::JustVoid();
}

SET_SELF_SIZE(CipherJob)
Expand Down
3 changes: 1 addition & 2 deletions src/crypto/crypto_keygen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace node {

using v8::FunctionCallbackInfo;
using v8::Int32;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
Expand Down Expand Up @@ -81,7 +80,7 @@ KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(Environment* env,
}

MaybeLocal<Value> SecretKeyGenTraits::EncodeKey(Environment* env,
SecretKeyGenConfig* params) {
SecretKeyGenConfig* params) {
std::shared_ptr<KeyObjectData> data =
KeyObjectData::CreateSecret(std::move(params->out));
Local<Value> ret;
Expand Down
32 changes: 16 additions & 16 deletions src/crypto/crypto_keygen.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ class KeyGenJob final : public CryptoJob<KeyGenTraits> {
}
}

v8::Maybe<bool> ToResult(
v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) override {
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) override {
Environment* env = AsyncWrap::env();
CryptoErrorStore* errors = CryptoJob<KeyGenTraits>::errors();
AdditionalParams* params = CryptoJob<KeyGenTraits>::params();
Expand All @@ -108,14 +107,17 @@ class KeyGenJob final : public CryptoJob<KeyGenTraits> {
*result = Undefined(env->isolate());
*err = try_catch.Exception();
}
return v8::Just(true);
} else {
if (errors->Empty()) errors->Capture();
CHECK(!errors->Empty());
*result = Undefined(env->isolate());
if (!errors->ToException(env).ToLocal(err)) {
return v8::Nothing<void>();
}
}

if (errors->Empty())
errors->Capture();
CHECK(!errors->Empty());
*result = Undefined(env->isolate());
return v8::Just(errors->ToException(env).ToLocal(err));
CHECK(!result->IsEmpty());
CHECK(!err->IsEmpty());
return v8::JustVoid();
}

SET_SELF_SIZE(KeyGenJob)
Expand Down Expand Up @@ -183,9 +185,8 @@ struct KeyPairGenTraits final {
return KeyGenJobStatus::OK;
}

static v8::MaybeLocal<v8::Value> EncodeKey(
Environment* env,
AdditionalParameters* params) {
static v8::MaybeLocal<v8::Value> EncodeKey(Environment* env,
AdditionalParameters* params) {
v8::Local<v8::Value> keys[2];
if (params->key
.ToEncodedPublicKey(env, params->public_key_encoding, &keys[0])
Expand Down Expand Up @@ -224,9 +225,8 @@ struct SecretKeyGenTraits final {
Environment* env,
SecretKeyGenConfig* params);

static v8::MaybeLocal<v8::Value> EncodeKey(
Environment* env,
SecretKeyGenConfig* params);
static v8::MaybeLocal<v8::Value> EncodeKey(Environment* env,
SecretKeyGenConfig* params);
};

template <typename AlgorithmParams>
Expand Down
25 changes: 15 additions & 10 deletions src/crypto/crypto_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,23 +369,28 @@ class KeyExportJob final : public CryptoJob<KeyExportTraits> {
}
}

v8::Maybe<bool> ToResult(
v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) override {
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) override {
Environment* env = AsyncWrap::env();
CryptoErrorStore* errors = CryptoJob<KeyExportTraits>::errors();
if (out_.size() > 0) {
CHECK(errors->Empty());
*err = v8::Undefined(env->isolate());
*result = out_.ToArrayBuffer(env);
return v8::Just(!result->IsEmpty());
if (result->IsEmpty()) {
return v8::Nothing<void>();
}
} else {
if (errors->Empty()) errors->Capture();
CHECK(!errors->Empty());
*result = v8::Undefined(env->isolate());
if (!errors->ToException(env).ToLocal(err)) {
return v8::Nothing<void>();
}
}

if (errors->Empty())
errors->Capture();
CHECK(!errors->Empty());
*result = v8::Undefined(env->isolate());
return v8::Just(errors->ToException(env).ToLocal(err));
CHECK(!result->IsEmpty());
CHECK(!err->IsEmpty());
return v8::JustVoid();
}

SET_SELF_SIZE(KeyExportJob)
Expand Down
53 changes: 28 additions & 25 deletions src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,31 +361,31 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
v8::HandleScope handle_scope(env->isolate());
v8::Context::Scope context_scope(env->context());

// TODO(tniessen): Remove the exception handling logic here as soon as we
// can verify that no code path in ToResult will ever throw an exception.
v8::Local<v8::Value> exception;
v8::Local<v8::Value> args[2];
{
node::errors::TryCatchScope try_catch(env);
v8::Maybe<bool> ret = ptr->ToResult(&args[0], &args[1]);
if (!ret.IsJust()) {
// If ToResult returns Nothing, then an exception should have been
// thrown and we should have caught it. Otherwise, args[0] and args[1]
// both should have been set to a value, even if the value is undefined.
if (ptr->ToResult(&args[0], &args[1]).IsNothing()) {
CHECK(try_catch.HasCaught());
CHECK(try_catch.CanContinue());
exception = try_catch.Exception();
} else if (!ret.FromJust()) {
return;
}
}

if (exception.IsEmpty()) {
ptr->MakeCallback(env->ondone_string(), arraysize(args), args);
} else {
if (!exception.IsEmpty()) {
ptr->MakeCallback(env->ondone_string(), 1, &exception);
} else {
CHECK(!args[0].IsEmpty());
CHECK(!args[1].IsEmpty());
ptr->MakeCallback(env->ondone_string(), arraysize(args), args);
}
}

virtual v8::Maybe<bool> ToResult(
v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) = 0;
virtual v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) = 0;

CryptoJobMode mode() const { return mode_; }

Expand Down Expand Up @@ -413,8 +413,9 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
v8::Local<v8::Value> ret[2];
env->PrintSyncTrace();
job->DoThreadPoolWork();
v8::Maybe<bool> result = job->ToResult(&ret[0], &ret[1]);
if (result.IsJust() && result.FromJust()) {
if (job->ToResult(&ret[0], &ret[1]).IsJust()) {
CHECK(!ret[0].IsEmpty());
CHECK(!ret[1].IsEmpty());
args.GetReturnValue().Set(
v8::Array::New(env->isolate(), ret, arraysize(ret)));
}
Expand Down Expand Up @@ -504,9 +505,8 @@ class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> {
success_ = true;
}

v8::Maybe<bool> ToResult(
v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) override {
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) override {
Environment* env = AsyncWrap::env();
CryptoErrorStore* errors = CryptoJob<DeriveBitsTraits>::errors();
if (success_) {
Expand All @@ -515,16 +515,19 @@ class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> {
if (!DeriveBitsTraits::EncodeOutput(
env, *CryptoJob<DeriveBitsTraits>::params(), &out_)
.ToLocal(result)) {
return v8::Nothing<bool>();
return v8::Nothing<void>();
}
} else {
if (errors->Empty()) errors->Capture();
CHECK(!errors->Empty());
*result = v8::Undefined(env->isolate());
if (!errors->ToException(env).ToLocal(err)) {
return v8::Nothing<void>();
}
return v8::Just(true);
}

if (errors->Empty())
errors->Capture();
CHECK(!errors->Empty());
*result = v8::Undefined(env->isolate());
return v8::Just(errors->ToException(env).ToLocal(err));
CHECK(!result->IsEmpty());
CHECK(!err->IsEmpty());
return v8::JustVoid();
}

SET_SELF_SIZE(DeriveBitsJob)
Expand Down

0 comments on commit a0c36cb

Please sign in to comment.