Skip to content

Commit 8b43388

Browse files
jasnelldanielleadams
authored andcommitted
src: reduce duplicated boilerplate with new env utility fn
Signed-off-by: James M Snell <[email protected]> PR-URL: #36536 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 214dbac commit 8b43388

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+95
-312
lines changed

src/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,7 @@ void Initialize(Local<Object> target,
405405

406406
env->SetProtoMethodNoSideEffect(channel_wrap, "getServers", GetServers);
407407

408-
Local<String> channel_wrap_string =
409-
FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap");
410-
channel_wrap->SetClassName(channel_wrap_string);
411-
target->Set(env->context(), channel_wrap_string,
412-
channel_wrap->GetFunction(context).ToLocalChecked()).Check();
408+
env->SetConstructorFunction(target, "ChannelWrap", channel_wrap);
413409
}
414410

415411
// Run the `Initialize` function when loading this module through

src/cares_wrap.cc

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,32 +2345,17 @@ void Initialize(Local<Object> target,
23452345
Local<FunctionTemplate> aiw =
23462346
BaseObject::MakeLazilyInitializedJSTemplate(env);
23472347
aiw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2348-
Local<String> addrInfoWrapString =
2349-
FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap");
2350-
aiw->SetClassName(addrInfoWrapString);
2351-
target->Set(env->context(),
2352-
addrInfoWrapString,
2353-
aiw->GetFunction(context).ToLocalChecked()).Check();
2348+
env->SetConstructorFunction(target, "GetAddrInfoReqWrap", aiw);
23542349

23552350
Local<FunctionTemplate> niw =
23562351
BaseObject::MakeLazilyInitializedJSTemplate(env);
23572352
niw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2358-
Local<String> nameInfoWrapString =
2359-
FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap");
2360-
niw->SetClassName(nameInfoWrapString);
2361-
target->Set(env->context(),
2362-
nameInfoWrapString,
2363-
niw->GetFunction(context).ToLocalChecked()).Check();
2353+
env->SetConstructorFunction(target, "GetNameInfoReqWrap", niw);
23642354

23652355
Local<FunctionTemplate> qrw =
23662356
BaseObject::MakeLazilyInitializedJSTemplate(env);
23672357
qrw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2368-
Local<String> queryWrapString =
2369-
FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap");
2370-
qrw->SetClassName(queryWrapString);
2371-
target->Set(env->context(),
2372-
queryWrapString,
2373-
qrw->GetFunction(context).ToLocalChecked()).Check();
2358+
env->SetConstructorFunction(target, "QueryReqWrap", qrw);
23742359

23752360
Local<FunctionTemplate> channel_wrap =
23762361
env->NewFunctionTemplate(ChannelWrap::New);
@@ -2397,11 +2382,7 @@ void Initialize(Local<Object> target,
23972382
env->SetProtoMethod(channel_wrap, "setLocalAddress", SetLocalAddress);
23982383
env->SetProtoMethod(channel_wrap, "cancel", Cancel);
23992384

2400-
Local<String> channelWrapString =
2401-
FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap");
2402-
channel_wrap->SetClassName(channelWrapString);
2403-
target->Set(env->context(), channelWrapString,
2404-
channel_wrap->GetFunction(context).ToLocalChecked()).Check();
2385+
env->SetConstructorFunction(target, "ChannelWrap", channel_wrap);
24052386
}
24062387

24072388
} // anonymous namespace

src/crypto/crypto_cipher.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {
265265
env->SetProtoMethodNoSideEffect(t, "getAuthTag", GetAuthTag);
266266
env->SetProtoMethod(t, "setAuthTag", SetAuthTag);
267267
env->SetProtoMethod(t, "setAAD", SetAAD);
268-
269-
target->Set(env->context(),
270-
FIXED_ONE_BYTE_STRING(env->isolate(), "CipherBase"),
271-
t->GetFunction(env->context()).ToLocalChecked()).Check();
268+
env->SetConstructorFunction(target, "CipherBase", t);
272269

273270
env->SetMethodNoSideEffect(target, "getSSLCiphers", GetSSLCiphers);
274271
env->SetMethodNoSideEffect(target, "getCiphers", GetCiphers);

src/crypto/crypto_context.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,6 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
252252
t->InstanceTemplate()->SetInternalFieldCount(
253253
SecureContext::kInternalFieldCount);
254254
t->Inherit(BaseObject::GetConstructorTemplate(env));
255-
Local<String> secureContextString =
256-
FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext");
257-
t->SetClassName(secureContextString);
258255

259256
env->SetProtoMethod(t, "init", Init);
260257
env->SetProtoMethod(t, "setKey", SetKey);
@@ -313,8 +310,8 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
313310
Local<FunctionTemplate>(),
314311
static_cast<PropertyAttribute>(ReadOnly | DontDelete));
315312

316-
target->Set(env->context(), secureContextString,
317-
t->GetFunction(env->context()).ToLocalChecked()).Check();
313+
env->SetConstructorFunction(target, "SecureContext", t);
314+
318315
env->set_secure_context_constructor_template(t);
319316

320317
env->SetMethodNoSideEffect(target, "getRootCertificates",

src/crypto/crypto_dh.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
9393
Local<FunctionTemplate>(),
9494
attributes);
9595

96-
target->Set(env->context(),
97-
name,
98-
t->GetFunction(env->context()).ToLocalChecked()).Check();
96+
env->SetConstructorFunction(target, name, t);
9997
};
10098

10199
make(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"), New);

src/crypto/crypto_ecdh.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
5252
env->SetProtoMethod(t, "setPublicKey", SetPublicKey);
5353
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);
5454

55-
target->Set(env->context(),
56-
FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH"),
57-
t->GetFunction(env->context()).ToLocalChecked()).Check();
55+
env->SetConstructorFunction(target, "ECDH", t);
5856

5957
env->SetMethodNoSideEffect(target, "ECDHConvertKey", ECDH::ConvertKey);
6058
env->SetMethodNoSideEffect(target, "getCurves", ECDH::GetCurves);

src/crypto/crypto_hash.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ void Hash::Initialize(Environment* env, Local<Object> target) {
5050
env->SetProtoMethod(t, "update", HashUpdate);
5151
env->SetProtoMethod(t, "digest", HashDigest);
5252

53-
target->Set(env->context(),
54-
FIXED_ONE_BYTE_STRING(env->isolate(), "Hash"),
55-
t->GetFunction(env->context()).ToLocalChecked()).Check();
53+
env->SetConstructorFunction(target, "Hash", t);
5654

5755
env->SetMethodNoSideEffect(target, "getHashes", GetHashes);
5856

src/crypto/crypto_hmac.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ void Hmac::Initialize(Environment* env, Local<Object> target) {
4848
env->SetProtoMethod(t, "update", HmacUpdate);
4949
env->SetProtoMethod(t, "digest", HmacDigest);
5050

51-
target->Set(env->context(),
52-
FIXED_ONE_BYTE_STRING(env->isolate(), "Hmac"),
53-
t->GetFunction(env->context()).ToLocalChecked()).Check();
51+
env->SetConstructorFunction(target, "Hmac", t);
5452

5553
HmacJob::Initialize(env, target);
5654
}

src/crypto/crypto_sig.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,7 @@ void Sign::Initialize(Environment* env, Local<Object> target) {
276276
env->SetProtoMethod(t, "update", SignUpdate);
277277
env->SetProtoMethod(t, "sign", SignFinal);
278278

279-
target->Set(env->context(),
280-
FIXED_ONE_BYTE_STRING(env->isolate(), "Sign"),
281-
t->GetFunction(env->context()).ToLocalChecked()).Check();
279+
env->SetConstructorFunction(target, "Sign", t);
282280

283281
env->SetMethod(target, "signOneShot", Sign::SignSync);
284282

@@ -396,9 +394,7 @@ void Verify::Initialize(Environment* env, Local<Object> target) {
396394
env->SetProtoMethod(t, "update", VerifyUpdate);
397395
env->SetProtoMethod(t, "verify", VerifyFinal);
398396

399-
target->Set(env->context(),
400-
FIXED_ONE_BYTE_STRING(env->isolate(), "Verify"),
401-
t->GetFunction(env->context()).ToLocalChecked()).Check();
397+
env->SetConstructorFunction(target, "Verify", t);
402398

403399
env->SetMethod(target, "verifyOneShot", Verify::VerifySync);
404400
}

src/crypto/crypto_util.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,11 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
349349
Environment* env,
350350
v8::Local<v8::Object> target) {
351351
v8::Local<v8::FunctionTemplate> job = env->NewFunctionTemplate(new_fn);
352-
v8::Local<v8::String> class_name =
353-
OneByteString(env->isolate(), CryptoJobTraits::JobName);
354-
job->SetClassName(class_name);
355352
job->Inherit(AsyncWrap::GetConstructorTemplate(env));
356353
job->InstanceTemplate()->SetInternalFieldCount(
357354
AsyncWrap::kInternalFieldCount);
358355
env->SetProtoMethod(job, "run", Run);
359-
target->Set(
360-
env->context(),
361-
class_name,
362-
job->GetFunction(env->context()).ToLocalChecked()).Check();
356+
env->SetConstructorFunction(target, CryptoJobTraits::JobName, job);
363357
}
364358

365359
private:

0 commit comments

Comments
 (0)