diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 51648b9e4ca999..dd961b86b49eb7 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -61,6 +61,7 @@ using v8::DontDelete; using v8::EscapableHandleScope; using v8::Exception; using v8::External; +using v8::FunctionCallback; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; @@ -3923,67 +3924,44 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo& args) { void DiffieHellman::Initialize(Environment* env, Local target) { - Local t = env->NewFunctionTemplate(New); - - const PropertyAttribute attributes = - static_cast(v8::ReadOnly | v8::DontDelete); - - t->InstanceTemplate()->SetInternalFieldCount(1); - - env->SetProtoMethod(t, "generateKeys", GenerateKeys); - env->SetProtoMethod(t, "computeSecret", ComputeSecret); - env->SetProtoMethodNoSideEffect(t, "getPrime", GetPrime); - env->SetProtoMethodNoSideEffect(t, "getGenerator", GetGenerator); - env->SetProtoMethodNoSideEffect(t, "getPublicKey", GetPublicKey); - env->SetProtoMethodNoSideEffect(t, "getPrivateKey", GetPrivateKey); - env->SetProtoMethod(t, "setPublicKey", SetPublicKey); - env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey); - - Local verify_error_getter_templ = - FunctionTemplate::New(env->isolate(), - DiffieHellman::VerifyErrorGetter, - env->as_external(), - Signature::New(env->isolate(), t), - /* length */ 0, - ConstructorBehavior::kThrow, - SideEffectType::kHasNoSideEffect); - - t->InstanceTemplate()->SetAccessorProperty( - env->verify_error_string(), - verify_error_getter_templ, - Local(), - attributes); - - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"), - t->GetFunction(env->context()).ToLocalChecked()); - - Local t2 = env->NewFunctionTemplate(DiffieHellmanGroup); - t2->InstanceTemplate()->SetInternalFieldCount(1); - - env->SetProtoMethod(t2, "generateKeys", GenerateKeys); - env->SetProtoMethod(t2, "computeSecret", ComputeSecret); - env->SetProtoMethodNoSideEffect(t2, "getPrime", GetPrime); - env->SetProtoMethodNoSideEffect(t2, "getGenerator", GetGenerator); - env->SetProtoMethodNoSideEffect(t2, "getPublicKey", GetPublicKey); - env->SetProtoMethodNoSideEffect(t2, "getPrivateKey", GetPrivateKey); - - Local verify_error_getter_templ2 = - FunctionTemplate::New(env->isolate(), - DiffieHellman::VerifyErrorGetter, - env->as_external(), - Signature::New(env->isolate(), t2), - /* length */ 0, - ConstructorBehavior::kThrow, - SideEffectType::kHasNoSideEffect); - - t2->InstanceTemplate()->SetAccessorProperty( - env->verify_error_string(), - verify_error_getter_templ2, - Local(), - attributes); + auto make = [&] (Local name, FunctionCallback callback) { + Local t = env->NewFunctionTemplate(callback); + + const PropertyAttribute attributes = + static_cast(v8::ReadOnly | v8::DontDelete); + + t->InstanceTemplate()->SetInternalFieldCount(1); + + env->SetProtoMethod(t, "generateKeys", GenerateKeys); + env->SetProtoMethod(t, "computeSecret", ComputeSecret); + env->SetProtoMethodNoSideEffect(t, "getPrime", GetPrime); + env->SetProtoMethodNoSideEffect(t, "getGenerator", GetGenerator); + env->SetProtoMethodNoSideEffect(t, "getPublicKey", GetPublicKey); + env->SetProtoMethodNoSideEffect(t, "getPrivateKey", GetPrivateKey); + env->SetProtoMethod(t, "setPublicKey", SetPublicKey); + env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey); + + Local verify_error_getter_templ = + FunctionTemplate::New(env->isolate(), + DiffieHellman::VerifyErrorGetter, + env->as_external(), + Signature::New(env->isolate(), t), + /* length */ 0, + ConstructorBehavior::kThrow, + SideEffectType::kHasNoSideEffect); + + t->InstanceTemplate()->SetAccessorProperty( + env->verify_error_string(), + verify_error_getter_templ, + Local(), + attributes); + + target->Set(name, t->GetFunction(env->context()).ToLocalChecked()); + }; - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"), - t2->GetFunction(env->context()).ToLocalChecked()); + make(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"), New); + make(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"), + DiffieHellmanGroup); }