Skip to content

Commit 5ed3a1d

Browse files
committed
src: convert more Maybe<bool> to Maybe<void>
1 parent 09650bc commit 5ed3a1d

File tree

5 files changed

+73
-60
lines changed

5 files changed

+73
-60
lines changed

src/crypto/crypto_cipher.h

+13-7
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,8 @@ class CipherJob final : public CryptoJob<CipherTraits> {
249249
}
250250
}
251251

252-
v8::Maybe<bool> ToResult(
253-
v8::Local<v8::Value>* err,
254-
v8::Local<v8::Value>* result) override {
252+
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
253+
v8::Local<v8::Value>* result) override {
255254
Environment* env = AsyncWrap::env();
256255
CryptoErrorStore* errors = CryptoJob<CipherTraits>::errors();
257256

@@ -262,11 +261,18 @@ class CipherJob final : public CryptoJob<CipherTraits> {
262261
CHECK(errors->Empty());
263262
*err = v8::Undefined(env->isolate());
264263
*result = out_.ToArrayBuffer(env);
265-
return v8::Just(!result->IsEmpty());
264+
if (result->IsEmpty()) {
265+
return v8::Nothing<void>();
266+
}
267+
} else {
268+
*result = v8::Undefined(env->isolate());
269+
if (!errors->ToException(env).ToLocal(err)) {
270+
return v8::Nothing<void>();
271+
}
266272
}
267-
268-
*result = v8::Undefined(env->isolate());
269-
return v8::Just(errors->ToException(env).ToLocal(err));
273+
CHECK(!result->IsEmpty());
274+
CHECK(!err->IsEmpty());
275+
return v8::JustVoid();
270276
}
271277

272278
SET_SELF_SIZE(CipherJob)

src/crypto/crypto_keygen.cc

+1-2
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;
@@ -81,7 +80,7 @@ KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(Environment* env,
8180
}
8281

8382
MaybeLocal<Value> SecretKeyGenTraits::EncodeKey(Environment* env,
84-
SecretKeyGenConfig* params) {
83+
SecretKeyGenConfig* params) {
8584
std::shared_ptr<KeyObjectData> data =
8685
KeyObjectData::CreateSecret(std::move(params->out));
8786
Local<Value> ret;

src/crypto/crypto_keygen.h

+16-16
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)
@@ -183,9 +185,8 @@ struct KeyPairGenTraits final {
183185
return KeyGenJobStatus::OK;
184186
}
185187

186-
static v8::MaybeLocal<v8::Value> EncodeKey(
187-
Environment* env,
188-
AdditionalParameters* params) {
188+
static v8::MaybeLocal<v8::Value> EncodeKey(Environment* env,
189+
AdditionalParameters* params) {
189190
v8::Local<v8::Value> keys[2];
190191
if (params->key
191192
.ToEncodedPublicKey(env, params->public_key_encoding, &keys[0])
@@ -224,9 +225,8 @@ struct SecretKeyGenTraits final {
224225
Environment* env,
225226
SecretKeyGenConfig* params);
226227

227-
static v8::MaybeLocal<v8::Value> EncodeKey(
228-
Environment* env,
229-
SecretKeyGenConfig* params);
228+
static v8::MaybeLocal<v8::Value> EncodeKey(Environment* env,
229+
SecretKeyGenConfig* params);
230230
};
231231

232232
template <typename AlgorithmParams>

src/crypto/crypto_keys.h

+15-10
Original file line numberDiff line numberDiff line change
@@ -369,23 +369,28 @@ class KeyExportJob final : public CryptoJob<KeyExportTraits> {
369369
}
370370
}
371371

372-
v8::Maybe<bool> ToResult(
373-
v8::Local<v8::Value>* err,
374-
v8::Local<v8::Value>* result) override {
372+
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
373+
v8::Local<v8::Value>* result) override {
375374
Environment* env = AsyncWrap::env();
376375
CryptoErrorStore* errors = CryptoJob<KeyExportTraits>::errors();
377376
if (out_.size() > 0) {
378377
CHECK(errors->Empty());
379378
*err = v8::Undefined(env->isolate());
380379
*result = out_.ToArrayBuffer(env);
381-
return v8::Just(!result->IsEmpty());
380+
if (result->IsEmpty()) {
381+
return v8::Nothing<void>();
382+
}
383+
} else {
384+
if (errors->Empty()) errors->Capture();
385+
CHECK(!errors->Empty());
386+
*result = v8::Undefined(env->isolate());
387+
if (!errors->ToException(env).ToLocal(err)) {
388+
return v8::Nothing<void>();
389+
}
382390
}
383-
384-
if (errors->Empty())
385-
errors->Capture();
386-
CHECK(!errors->Empty());
387-
*result = v8::Undefined(env->isolate());
388-
return v8::Just(errors->ToException(env).ToLocal(err));
391+
CHECK(!result->IsEmpty());
392+
CHECK(!err->IsEmpty());
393+
return v8::JustVoid();
389394
}
390395

391396
SET_SELF_SIZE(KeyExportJob)

src/crypto/crypto_util.h

+28-25
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)