Skip to content

Commit 97a082d

Browse files
committed
src: convert more Maybe<bool> to Maybe<void>
1 parent 0511389 commit 97a082d

File tree

5 files changed

+72
-59
lines changed

5 files changed

+72
-59
lines changed

src/crypto/crypto_cipher.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,8 @@ class CipherJob final : public CryptoJob<CipherTraits> {
245245
}
246246
}
247247

248-
v8::Maybe<bool> ToResult(
249-
v8::Local<v8::Value>* err,
250-
v8::Local<v8::Value>* result) override {
248+
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
249+
v8::Local<v8::Value>* result) override {
251250
Environment* env = AsyncWrap::env();
252251
CryptoErrorStore* errors = CryptoJob<CipherTraits>::errors();
253252

@@ -258,11 +257,18 @@ class CipherJob final : public CryptoJob<CipherTraits> {
258257
CHECK(errors->Empty());
259258
*err = v8::Undefined(env->isolate());
260259
*result = out_.ToArrayBuffer(env);
261-
return v8::Just(!result->IsEmpty());
260+
if (result->IsEmpty()) {
261+
return v8::Nothing<void>();
262+
}
263+
} else {
264+
*result = v8::Undefined(env->isolate());
265+
if (!errors->ToException(env).ToLocal(err)) {
266+
return v8::Nothing<void>();
267+
}
262268
}
263-
264-
*result = v8::Undefined(env->isolate());
265-
return v8::Just(errors->ToException(env).ToLocal(err));
269+
CHECK(!result->IsEmpty());
270+
CHECK(!err->IsEmpty());
271+
return v8::JustVoid();
266272
}
267273

268274
SET_SELF_SIZE(CipherJob)

src/crypto/crypto_keygen.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace node {
1414

1515
using v8::FunctionCallbackInfo;
1616
using v8::Int32;
17-
using v8::Just;
1817
using v8::JustVoid;
1918
using v8::Local;
2019
using v8::Maybe;

src/crypto/crypto_keygen.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ class KeyGenJob final : public CryptoJob<KeyGenTraits> {
9191
}
9292
}
9393

94-
v8::Maybe<bool> ToResult(
95-
v8::Local<v8::Value>* err,
96-
v8::Local<v8::Value>* result) override {
94+
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
95+
v8::Local<v8::Value>* result) override {
9796
Environment* env = AsyncWrap::env();
9897
CryptoErrorStore* errors = CryptoJob<KeyGenTraits>::errors();
9998
AdditionalParams* params = CryptoJob<KeyGenTraits>::params();
@@ -108,14 +107,17 @@ class KeyGenJob final : public CryptoJob<KeyGenTraits> {
108107
*result = Undefined(env->isolate());
109108
*err = try_catch.Exception();
110109
}
111-
return v8::Just(true);
110+
} else {
111+
if (errors->Empty()) errors->Capture();
112+
CHECK(!errors->Empty());
113+
*result = Undefined(env->isolate());
114+
if (!errors->ToException(env).ToLocal(err)) {
115+
return v8::Nothing<void>();
116+
}
112117
}
113-
114-
if (errors->Empty())
115-
errors->Capture();
116-
CHECK(!errors->Empty());
117-
*result = Undefined(env->isolate());
118-
return v8::Just(errors->ToException(env).ToLocal(err));
118+
CHECK(!result->IsEmpty());
119+
CHECK(!err->IsEmpty());
120+
return v8::JustVoid();
119121
}
120122

121123
SET_SELF_SIZE(KeyGenJob)
@@ -181,9 +183,8 @@ struct KeyPairGenTraits final {
181183
return KeyGenJobStatus::OK;
182184
}
183185

184-
static v8::MaybeLocal<v8::Value> EncodeKey(
185-
Environment* env,
186-
AdditionalParameters* params) {
186+
static v8::MaybeLocal<v8::Value> EncodeKey(Environment* env,
187+
AdditionalParameters* params) {
187188
v8::Local<v8::Value> keys[2];
188189
if (params->key
189190
.ToEncodedPublicKey(env, params->public_key_encoding, &keys[0])
@@ -222,9 +223,8 @@ struct SecretKeyGenTraits final {
222223
Environment* env,
223224
SecretKeyGenConfig* params);
224225

225-
static v8::MaybeLocal<v8::Value> EncodeKey(
226-
Environment* env,
227-
SecretKeyGenConfig* params);
226+
static v8::MaybeLocal<v8::Value> EncodeKey(Environment* env,
227+
SecretKeyGenConfig* params);
228228
};
229229

230230
template <typename AlgorithmParams>

src/crypto/crypto_keys.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,23 +362,28 @@ class KeyExportJob final : public CryptoJob<KeyExportTraits> {
362362
}
363363
}
364364

365-
v8::Maybe<bool> ToResult(
366-
v8::Local<v8::Value>* err,
367-
v8::Local<v8::Value>* result) override {
365+
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
366+
v8::Local<v8::Value>* result) override {
368367
Environment* env = AsyncWrap::env();
369368
CryptoErrorStore* errors = CryptoJob<KeyExportTraits>::errors();
370369
if (out_.size() > 0) {
371370
CHECK(errors->Empty());
372371
*err = v8::Undefined(env->isolate());
373372
*result = out_.ToArrayBuffer(env);
374-
return v8::Just(!result->IsEmpty());
373+
if (result->IsEmpty()) {
374+
return v8::Nothing<void>();
375+
}
376+
} else {
377+
if (errors->Empty()) errors->Capture();
378+
CHECK(!errors->Empty());
379+
*result = v8::Undefined(env->isolate());
380+
if (!errors->ToException(env).ToLocal(err)) {
381+
return v8::Nothing<void>();
382+
}
375383
}
376-
377-
if (errors->Empty())
378-
errors->Capture();
379-
CHECK(!errors->Empty());
380-
*result = v8::Undefined(env->isolate());
381-
return v8::Just(errors->ToException(env).ToLocal(err));
384+
CHECK(!result->IsEmpty());
385+
CHECK(!err->IsEmpty());
386+
return v8::JustVoid();
382387
}
383388

384389
SET_SELF_SIZE(KeyExportJob)

src/crypto/crypto_util.h

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -361,31 +361,31 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
361361
v8::HandleScope handle_scope(env->isolate());
362362
v8::Context::Scope context_scope(env->context());
363363

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

379-
if (exception.IsEmpty()) {
380-
ptr->MakeCallback(env->ondone_string(), arraysize(args), args);
381-
} else {
378+
if (!exception.IsEmpty()) {
382379
ptr->MakeCallback(env->ondone_string(), 1, &exception);
380+
} else {
381+
CHECK(!args[0].IsEmpty());
382+
CHECK(!args[1].IsEmpty());
383+
ptr->MakeCallback(env->ondone_string(), arraysize(args), args);
383384
}
384385
}
385386

386-
virtual v8::Maybe<bool> ToResult(
387-
v8::Local<v8::Value>* err,
388-
v8::Local<v8::Value>* result) = 0;
387+
virtual v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
388+
v8::Local<v8::Value>* result) = 0;
389389

390390
CryptoJobMode mode() const { return mode_; }
391391

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

507-
v8::Maybe<bool> ToResult(
508-
v8::Local<v8::Value>* err,
509-
v8::Local<v8::Value>* result) override {
508+
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
509+
v8::Local<v8::Value>* result) override {
510510
Environment* env = AsyncWrap::env();
511511
CryptoErrorStore* errors = CryptoJob<DeriveBitsTraits>::errors();
512512
if (success_) {
@@ -515,16 +515,19 @@ class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> {
515515
if (!DeriveBitsTraits::EncodeOutput(
516516
env, *CryptoJob<DeriveBitsTraits>::params(), &out_)
517517
.ToLocal(result)) {
518-
return v8::Nothing<bool>();
518+
return v8::Nothing<void>();
519+
}
520+
} else {
521+
if (errors->Empty()) errors->Capture();
522+
CHECK(!errors->Empty());
523+
*result = v8::Undefined(env->isolate());
524+
if (!errors->ToException(env).ToLocal(err)) {
525+
return v8::Nothing<void>();
519526
}
520-
return v8::Just(true);
521527
}
522-
523-
if (errors->Empty())
524-
errors->Capture();
525-
CHECK(!errors->Empty());
526-
*result = v8::Undefined(env->isolate());
527-
return v8::Just(errors->ToException(env).ToLocal(err));
528+
CHECK(!result->IsEmpty());
529+
CHECK(!err->IsEmpty());
530+
return v8::JustVoid();
528531
}
529532

530533
SET_SELF_SIZE(DeriveBitsJob)

0 commit comments

Comments
 (0)