Skip to content

Commit

Permalink
src: use v8::Boolean(b) over b ? True() : False()
Browse files Browse the repository at this point in the history
Simplify existing code by using v8::Boolean::New() instead of equivalent
expressions that contain ternary operators.

PR-URL: #47554
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jiawen Geng <[email protected]>
Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
  • Loading branch information
tniessen authored and targos committed May 2, 2023
1 parent ba255ed commit cf48db0
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace node {
using errors::TryCatchScope;
using v8::Array;
using v8::Boolean;
using v8::Context;
using v8::EscapableHandleScope;
using v8::Function;
Expand Down Expand Up @@ -667,7 +668,7 @@ Maybe<bool> InitializeContextRuntime(Local<Context> context) {
context->AllowCodeGenerationFromStrings(false);
context->SetEmbedderData(
ContextEmbedderIndex::kAllowCodeGenerationFromStrings,
is_code_generation_from_strings_allowed ? True(isolate) : False(isolate));
Boolean::New(isolate, is_code_generation_from_strings_allowed));

if (per_process::cli_options->disable_proto == "") {
return Just(true);
Expand Down
3 changes: 2 additions & 1 deletion src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace node {

using v8::Array;
using v8::ArrayBufferView;
using v8::Boolean;
using v8::Context;
using v8::DontDelete;
using v8::Exception;
Expand Down Expand Up @@ -1191,7 +1192,7 @@ int SecureContext::TicketKeyCallback(SSL* ssl,
return -1;
}

argv[2] = enc != 0 ? v8::True(env->isolate()) : v8::False(env->isolate());
argv[2] = Boolean::New(env->isolate(), enc != 0);

Local<Value> ret;
if (!node::MakeCallback(
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/crypto_hmac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace node {

using v8::Boolean;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
Expand Down Expand Up @@ -264,11 +265,10 @@ Maybe<bool> HmacTraits::EncodeOutput(
*result = out->ToArrayBuffer(env);
break;
case SignConfiguration::kVerify:
*result =
*result = Boolean::New(
env->isolate(),
out->size() > 0 && out->size() == params.signature.size() &&
memcmp(out->data(), params.signature.data(), out->size()) == 0
? v8::True(env->isolate())
: v8::False(env->isolate());
memcmp(out->data(), params.signature.data(), out->size()) == 0);
break;
default:
UNREACHABLE();
Expand Down
5 changes: 2 additions & 3 deletions src/crypto/crypto_random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ namespace node {

using v8::ArrayBuffer;
using v8::BackingStore;
using v8::False;
using v8::Boolean;
using v8::FunctionCallbackInfo;
using v8::Int32;
using v8::Just;
using v8::Local;
using v8::Maybe;
using v8::Nothing;
using v8::Object;
using v8::True;
using v8::Uint32;
using v8::Value;

Expand Down Expand Up @@ -225,7 +224,7 @@ Maybe<bool> CheckPrimeTraits::EncodeOutput(
const CheckPrimeConfig& params,
ByteSource* out,
v8::Local<v8::Value>* result) {
*result = out->data<char>()[0] ? True(env->isolate()) : False(env->isolate());
*result = Boolean::New(env->isolate(), out->data<char>()[0] != 0);
return Just(true);
}

Expand Down
4 changes: 2 additions & 2 deletions src/crypto/crypto_sig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace node {

using v8::ArrayBuffer;
using v8::BackingStore;
using v8::Boolean;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
Expand Down Expand Up @@ -816,8 +817,7 @@ Maybe<bool> SignTraits::EncodeOutput(
*result = out->ToArrayBuffer(env);
break;
case SignConfiguration::kVerify:
*result = out->data<char>()[0] == 1 ? v8::True(env->isolate())
: v8::False(env->isolate());
*result = Boolean::New(env->isolate(), out->data<char>()[0] == 1);
break;
default:
UNREACHABLE();
Expand Down
20 changes: 9 additions & 11 deletions src/crypto/crypto_tls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ using v8::Array;
using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::Boolean;
using v8::Context;
using v8::DontDelete;
using v8::Exception;
Expand All @@ -57,7 +58,6 @@ using v8::PropertyAttribute;
using v8::ReadOnly;
using v8::Signature;
using v8::String;
using v8::True;
using v8::Uint32;
using v8::Value;

Expand Down Expand Up @@ -96,15 +96,14 @@ void OnClientHello(

if ((buf.IsEmpty() ||
hello_obj->Set(env->context(), env->session_id_string(), buf)
.IsNothing()) ||
.IsNothing()) ||
hello_obj->Set(env->context(), env->servername_string(), servername)
.IsNothing() ||
hello_obj->Set(
env->context(),
env->tls_ticket_string(),
hello.has_ticket()
? True(env->isolate())
: False(env->isolate())).IsNothing()) {
hello_obj
->Set(env->context(),
env->tls_ticket_string(),
Boolean::New(env->isolate(), hello.has_ticket()))
.IsNothing()) {
return;
}

Expand Down Expand Up @@ -202,9 +201,8 @@ int SSLCertCallback(SSL* s, void* arg) {
? String::Empty(env->isolate())
: OneByteString(env->isolate(), servername, strlen(servername));

Local<Value> ocsp = (SSL_get_tlsext_status_type(s) == TLSEXT_STATUSTYPE_ocsp)
? True(env->isolate())
: False(env->isolate());
Local<Value> ocsp = Boolean::New(
env->isolate(), SSL_get_tlsext_status_type(s) == TLSEXT_STATUSTYPE_ocsp);

if (info->Set(env->context(), env->servername_string(), servername_str)
.IsNothing() ||
Expand Down
13 changes: 3 additions & 10 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ using v8::BackingStore;
using v8::Boolean;
using v8::Context;
using v8::EscapableHandleScope;
using v8::False;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
Expand All @@ -42,7 +41,6 @@ using v8::Number;
using v8::Object;
using v8::ObjectTemplate;
using v8::String;
using v8::True;
using v8::Uint8Array;
using v8::Undefined;
using v8::Value;
Expand Down Expand Up @@ -332,10 +330,8 @@ void Http2Settings::Done(bool ack) {
uint64_t end = uv_hrtime();
double duration = (end - startTime_) / 1e6;

Local<Value> argv[] = {
ack ? True(env()->isolate()) : False(env()->isolate()),
Number::New(env()->isolate(), duration)
};
Local<Value> argv[] = {Boolean::New(env()->isolate(), ack),
Number::New(env()->isolate(), duration)};
MakeCallback(callback(), arraysize(argv), argv);
}

Expand Down Expand Up @@ -3131,10 +3127,7 @@ void Http2Ping::Done(bool ack, const uint8_t* payload) {
}

Local<Value> argv[] = {
ack ? True(isolate) : False(isolate),
Number::New(isolate, duration_ms),
buf
};
Boolean::New(isolate, ack), Number::New(isolate, duration_ms), buf};
MakeCallback(callback(), arraysize(argv), argv);
}

Expand Down
2 changes: 1 addition & 1 deletion src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
result.emplace_back(family);
result.emplace_back(FIXED_ONE_BYTE_STRING(isolate, mac));
result.emplace_back(
interfaces[i].is_internal ? True(isolate) : False(isolate));
Boolean::New(env->isolate(), interfaces[i].is_internal));
if (interfaces[i].address.address4.sin_family == AF_INET6) {
uint32_t scopeid = interfaces[i].address.address6.sin6_scope_id;
result.emplace_back(Integer::NewFromUnsigned(isolate, scopeid));
Expand Down

0 comments on commit cf48db0

Please sign in to comment.