Skip to content

Commit

Permalink
src: split property helpers from node::Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Jul 30, 2022
1 parent 327030e commit b3d85b7
Show file tree
Hide file tree
Showing 77 changed files with 1,500 additions and 1,281 deletions.
21 changes: 11 additions & 10 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,32 +390,33 @@ void Initialize(Local<Object> target,
void* priv) {
Environment* env = Environment::GetCurrent(context);

env->SetMethod(target, "getaddrinfo", GetAddrInfo);
env->SetMethod(target, "getnameinfo", GetNameInfo);
SetMethod(context, target, "getaddrinfo", GetAddrInfo);
SetMethod(context, target, "getnameinfo", GetNameInfo);

// 'SetMethodNoSideEffect' means that debuggers can safely execute this
// function for e.g. previews.
env->SetMethodNoSideEffect(target, "canonicalizeIP", CanonicalizeIP);
SetMethodNoSideEffect(context, target, "canonicalizeIP", CanonicalizeIP);

// ... more code ...

Isolate* isolate = env->isolate();
// Building the `ChannelWrap` class for JS:
Local<FunctionTemplate> channel_wrap =
env->NewFunctionTemplate(ChannelWrap::New);
NewFunctionTemplate(isolate, ChannelWrap::New);
// Allow for 1 internal field, see `BaseObject` for details on this:
channel_wrap->InstanceTemplate()->SetInternalFieldCount(1);
channel_wrap->Inherit(AsyncWrap::GetConstructorTemplate(env));

// Set various methods on the class (i.e. on the prototype):
env->SetProtoMethod(channel_wrap, "queryAny", Query<QueryAnyWrap>);
env->SetProtoMethod(channel_wrap, "queryA", Query<QueryAWrap>);
SetProtoMethod(isolate, channel_wrap, "queryAny", Query<QueryAnyWrap>);
SetProtoMethod(isolate, channel_wrap, "queryA", Query<QueryAWrap>);
// ...
env->SetProtoMethod(channel_wrap, "querySoa", Query<QuerySoaWrap>);
env->SetProtoMethod(channel_wrap, "getHostByAddr", Query<GetHostByAddrWrap>);
SetProtoMethod(isolate, channel_wrap, "querySoa", Query<QuerySoaWrap>);
SetProtoMethod(isolate, channel_wrap, "getHostByAddr", Query<GetHostByAddrWrap>);

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

env->SetConstructorFunction(target, "ChannelWrap", channel_wrap);
SetConstructorFunction(isolate, target, "ChannelWrap", channel_wrap);
}

// Run the `Initialize` function when loading this module through
Expand Down
28 changes: 15 additions & 13 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,14 @@ void AsyncWrap::SetCallbackTrampoline(const FunctionCallbackInfo<Value>& args) {
Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(Environment* env) {
Local<FunctionTemplate> tmpl = env->async_wrap_ctor_template();
if (tmpl.IsEmpty()) {
tmpl = env->NewFunctionTemplate(nullptr);
Isolate* isolate = env->isolate();
tmpl = NewFunctionTemplate(isolate, nullptr);
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap"));
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
env->SetProtoMethod(tmpl, "getAsyncId", AsyncWrap::GetAsyncId);
env->SetProtoMethod(tmpl, "asyncReset", AsyncWrap::AsyncReset);
env->SetProtoMethod(tmpl, "getProviderType", AsyncWrap::GetProviderType);
SetProtoMethod(isolate, tmpl, "getAsyncId", AsyncWrap::GetAsyncId);
SetProtoMethod(isolate, tmpl, "asyncReset", AsyncWrap::AsyncReset);
SetProtoMethod(
isolate, tmpl, "getProviderType", AsyncWrap::GetProviderType);
env->set_async_wrap_ctor_template(tmpl);
}
return tmpl;
Expand All @@ -356,15 +358,15 @@ void AsyncWrap::Initialize(Local<Object> target,
Isolate* isolate = env->isolate();
HandleScope scope(isolate);

env->SetMethod(target, "setupHooks", SetupHooks);
env->SetMethod(target, "setCallbackTrampoline", SetCallbackTrampoline);
env->SetMethod(target, "pushAsyncContext", PushAsyncContext);
env->SetMethod(target, "popAsyncContext", PopAsyncContext);
env->SetMethod(target, "executionAsyncResource", ExecutionAsyncResource);
env->SetMethod(target, "clearAsyncIdStack", ClearAsyncIdStack);
env->SetMethod(target, "queueDestroyAsyncId", QueueDestroyAsyncId);
env->SetMethod(target, "setPromiseHooks", SetPromiseHooks);
env->SetMethod(target, "registerDestroyHook", RegisterDestroyHook);
SetMethod(context, target, "setupHooks", SetupHooks);
SetMethod(context, target, "setCallbackTrampoline", SetCallbackTrampoline);
SetMethod(context, target, "pushAsyncContext", PushAsyncContext);
SetMethod(context, target, "popAsyncContext", PopAsyncContext);
SetMethod(context, target, "executionAsyncResource", ExecutionAsyncResource);
SetMethod(context, target, "clearAsyncIdStack", ClearAsyncIdStack);
SetMethod(context, target, "queueDestroyAsyncId", QueueDestroyAsyncId);
SetMethod(context, target, "setPromiseHooks", SetPromiseHooks);
SetMethod(context, target, "registerDestroyHook", RegisterDestroyHook);

PropertyAttribute ReadOnlyDontDelete =
static_cast<PropertyAttribute>(ReadOnly | DontDelete);
Expand Down
58 changes: 30 additions & 28 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1886,12 +1886,13 @@ void Initialize(Local<Object> target,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();

env->SetMethod(target, "getaddrinfo", GetAddrInfo);
env->SetMethod(target, "getnameinfo", GetNameInfo);
env->SetMethodNoSideEffect(target, "canonicalizeIP", CanonicalizeIP);
SetMethod(context, target, "getaddrinfo", GetAddrInfo);
SetMethod(context, target, "getnameinfo", GetNameInfo);
SetMethodNoSideEffect(context, target, "canonicalizeIP", CanonicalizeIP);

env->SetMethod(target, "strerror", StrError);
SetMethod(context, target, "strerror", StrError);

target->Set(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "AF_INET"),
Integer::New(env->isolate(), AF_INET)).Check();
Expand All @@ -1913,44 +1914,45 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> aiw =
BaseObject::MakeLazilyInitializedJSTemplate(env);
aiw->Inherit(AsyncWrap::GetConstructorTemplate(env));
env->SetConstructorFunction(target, "GetAddrInfoReqWrap", aiw);
SetConstructorFunction(context, target, "GetAddrInfoReqWrap", aiw);

Local<FunctionTemplate> niw =
BaseObject::MakeLazilyInitializedJSTemplate(env);
niw->Inherit(AsyncWrap::GetConstructorTemplate(env));
env->SetConstructorFunction(target, "GetNameInfoReqWrap", niw);
SetConstructorFunction(context, target, "GetNameInfoReqWrap", niw);

Local<FunctionTemplate> qrw =
BaseObject::MakeLazilyInitializedJSTemplate(env);
qrw->Inherit(AsyncWrap::GetConstructorTemplate(env));
env->SetConstructorFunction(target, "QueryReqWrap", qrw);
SetConstructorFunction(context, target, "QueryReqWrap", qrw);

Local<FunctionTemplate> channel_wrap =
env->NewFunctionTemplate(ChannelWrap::New);
NewFunctionTemplate(isolate, ChannelWrap::New);
channel_wrap->InstanceTemplate()->SetInternalFieldCount(
ChannelWrap::kInternalFieldCount);
channel_wrap->Inherit(AsyncWrap::GetConstructorTemplate(env));

env->SetProtoMethod(channel_wrap, "queryAny", Query<QueryAnyWrap>);
env->SetProtoMethod(channel_wrap, "queryA", Query<QueryAWrap>);
env->SetProtoMethod(channel_wrap, "queryAaaa", Query<QueryAaaaWrap>);
env->SetProtoMethod(channel_wrap, "queryCaa", Query<QueryCaaWrap>);
env->SetProtoMethod(channel_wrap, "queryCname", Query<QueryCnameWrap>);
env->SetProtoMethod(channel_wrap, "queryMx", Query<QueryMxWrap>);
env->SetProtoMethod(channel_wrap, "queryNs", Query<QueryNsWrap>);
env->SetProtoMethod(channel_wrap, "queryTxt", Query<QueryTxtWrap>);
env->SetProtoMethod(channel_wrap, "querySrv", Query<QuerySrvWrap>);
env->SetProtoMethod(channel_wrap, "queryPtr", Query<QueryPtrWrap>);
env->SetProtoMethod(channel_wrap, "queryNaptr", Query<QueryNaptrWrap>);
env->SetProtoMethod(channel_wrap, "querySoa", Query<QuerySoaWrap>);
env->SetProtoMethod(channel_wrap, "getHostByAddr", Query<GetHostByAddrWrap>);

env->SetProtoMethodNoSideEffect(channel_wrap, "getServers", GetServers);
env->SetProtoMethod(channel_wrap, "setServers", SetServers);
env->SetProtoMethod(channel_wrap, "setLocalAddress", SetLocalAddress);
env->SetProtoMethod(channel_wrap, "cancel", Cancel);

env->SetConstructorFunction(target, "ChannelWrap", channel_wrap);
SetProtoMethod(isolate, channel_wrap, "queryAny", Query<QueryAnyWrap>);
SetProtoMethod(isolate, channel_wrap, "queryA", Query<QueryAWrap>);
SetProtoMethod(isolate, channel_wrap, "queryAaaa", Query<QueryAaaaWrap>);
SetProtoMethod(isolate, channel_wrap, "queryCaa", Query<QueryCaaWrap>);
SetProtoMethod(isolate, channel_wrap, "queryCname", Query<QueryCnameWrap>);
SetProtoMethod(isolate, channel_wrap, "queryMx", Query<QueryMxWrap>);
SetProtoMethod(isolate, channel_wrap, "queryNs", Query<QueryNsWrap>);
SetProtoMethod(isolate, channel_wrap, "queryTxt", Query<QueryTxtWrap>);
SetProtoMethod(isolate, channel_wrap, "querySrv", Query<QuerySrvWrap>);
SetProtoMethod(isolate, channel_wrap, "queryPtr", Query<QueryPtrWrap>);
SetProtoMethod(isolate, channel_wrap, "queryNaptr", Query<QueryNaptrWrap>);
SetProtoMethod(isolate, channel_wrap, "querySoa", Query<QuerySoaWrap>);
SetProtoMethod(
isolate, channel_wrap, "getHostByAddr", Query<GetHostByAddrWrap>);

SetProtoMethodNoSideEffect(isolate, channel_wrap, "getServers", GetServers);
SetProtoMethod(isolate, channel_wrap, "setServers", SetServers);
SetProtoMethod(isolate, channel_wrap, "setLocalAddress", SetLocalAddress);
SetProtoMethod(isolate, channel_wrap, "cancel", Cancel);

SetConstructorFunction(context, target, "ChannelWrap", channel_wrap);
}

} // namespace cares_wrap
Expand Down
77 changes: 45 additions & 32 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ namespace node {
using v8::Array;
using v8::ArrayBuffer;
using v8::BackingStore;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Int32;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::Uint32;
Expand Down Expand Up @@ -270,43 +272,54 @@ void CipherBase::MemoryInfo(MemoryTracker* tracker) const {
}

void CipherBase::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
Isolate* isolate = env->isolate();
Local<Context> context = env->context();

Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);

t->InstanceTemplate()->SetInternalFieldCount(
CipherBase::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));

env->SetProtoMethod(t, "init", Init);
env->SetProtoMethod(t, "initiv", InitIv);
env->SetProtoMethod(t, "update", Update);
env->SetProtoMethod(t, "final", Final);
env->SetProtoMethod(t, "setAutoPadding", SetAutoPadding);
env->SetProtoMethodNoSideEffect(t, "getAuthTag", GetAuthTag);
env->SetProtoMethod(t, "setAuthTag", SetAuthTag);
env->SetProtoMethod(t, "setAAD", SetAAD);
env->SetConstructorFunction(target, "CipherBase", t);

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

env->SetMethod(target, "publicEncrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
EVP_PKEY_encrypt_init,
EVP_PKEY_encrypt>);
env->SetMethod(target, "privateDecrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
EVP_PKEY_decrypt_init,
EVP_PKEY_decrypt>);
env->SetMethod(target, "privateEncrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
EVP_PKEY_sign_init,
EVP_PKEY_sign>);
env->SetMethod(target, "publicDecrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
EVP_PKEY_verify_recover_init,
EVP_PKEY_verify_recover>);

env->SetMethodNoSideEffect(target, "getCipherInfo", GetCipherInfo);
SetProtoMethod(isolate, t, "init", Init);
SetProtoMethod(isolate, t, "initiv", InitIv);
SetProtoMethod(isolate, t, "update", Update);
SetProtoMethod(isolate, t, "final", Final);
SetProtoMethod(isolate, t, "setAutoPadding", SetAutoPadding);
SetProtoMethodNoSideEffect(isolate, t, "getAuthTag", GetAuthTag);
SetProtoMethod(isolate, t, "setAuthTag", SetAuthTag);
SetProtoMethod(isolate, t, "setAAD", SetAAD);
SetConstructorFunction(context, target, "CipherBase", t);

SetMethodNoSideEffect(context, target, "getSSLCiphers", GetSSLCiphers);
SetMethodNoSideEffect(context, target, "getCiphers", GetCiphers);

SetMethod(context,
target,
"publicEncrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
EVP_PKEY_encrypt_init,
EVP_PKEY_encrypt>);
SetMethod(context,
target,
"privateDecrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
EVP_PKEY_decrypt_init,
EVP_PKEY_decrypt>);
SetMethod(context,
target,
"privateEncrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
EVP_PKEY_sign_init,
EVP_PKEY_sign>);
SetMethod(context,
target,
"publicDecrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
EVP_PKEY_verify_recover_init,
EVP_PKEY_verify_recover>);

SetMethodNoSideEffect(context, target, "getCipherInfo", GetCipherInfo);

NODE_DEFINE_CONSTANT(target, kWebCryptoCipherEncrypt);
NODE_DEFINE_CONSTANT(target, kWebCryptoCipherDecrypt);
Expand Down
97 changes: 51 additions & 46 deletions src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Int32;
using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::PropertyAttribute;
Expand Down Expand Up @@ -256,47 +257,48 @@ Local<FunctionTemplate> SecureContext::GetConstructorTemplate(
Environment* env) {
Local<FunctionTemplate> tmpl = env->secure_context_constructor_template();
if (tmpl.IsEmpty()) {
tmpl = env->NewFunctionTemplate(New);
Isolate* isolate = env->isolate();
tmpl = NewFunctionTemplate(isolate, New);
tmpl->InstanceTemplate()->SetInternalFieldCount(
SecureContext::kInternalFieldCount);
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"));

env->SetProtoMethod(tmpl, "init", Init);
env->SetProtoMethod(tmpl, "setKey", SetKey);
env->SetProtoMethod(tmpl, "setCert", SetCert);
env->SetProtoMethod(tmpl, "addCACert", AddCACert);
env->SetProtoMethod(tmpl, "addCRL", AddCRL);
env->SetProtoMethod(tmpl, "addRootCerts", AddRootCerts);
env->SetProtoMethod(tmpl, "setCipherSuites", SetCipherSuites);
env->SetProtoMethod(tmpl, "setCiphers", SetCiphers);
env->SetProtoMethod(tmpl, "setSigalgs", SetSigalgs);
env->SetProtoMethod(tmpl, "setECDHCurve", SetECDHCurve);
env->SetProtoMethod(tmpl, "setDHParam", SetDHParam);
env->SetProtoMethod(tmpl, "setMaxProto", SetMaxProto);
env->SetProtoMethod(tmpl, "setMinProto", SetMinProto);
env->SetProtoMethod(tmpl, "getMaxProto", GetMaxProto);
env->SetProtoMethod(tmpl, "getMinProto", GetMinProto);
env->SetProtoMethod(tmpl, "setOptions", SetOptions);
env->SetProtoMethod(tmpl, "setSessionIdContext", SetSessionIdContext);
env->SetProtoMethod(tmpl, "setSessionTimeout", SetSessionTimeout);
env->SetProtoMethod(tmpl, "close", Close);
env->SetProtoMethod(tmpl, "loadPKCS12", LoadPKCS12);
env->SetProtoMethod(tmpl, "setTicketKeys", SetTicketKeys);
env->SetProtoMethod(tmpl, "setFreeListLength", SetFreeListLength);
env->SetProtoMethod(tmpl, "enableTicketKeyCallback",
EnableTicketKeyCallback);

env->SetProtoMethodNoSideEffect(tmpl, "getTicketKeys", GetTicketKeys);
env->SetProtoMethodNoSideEffect(tmpl, "getCertificate",
GetCertificate<true>);
env->SetProtoMethodNoSideEffect(tmpl, "getIssuer",
GetCertificate<false>);

#ifndef OPENSSL_NO_ENGINE
env->SetProtoMethod(tmpl, "setEngineKey", SetEngineKey);
env->SetProtoMethod(tmpl, "setClientCertEngine", SetClientCertEngine);
#endif // !OPENSSL_NO_ENGINE
SetProtoMethod(isolate, tmpl, "init", Init);
SetProtoMethod(isolate, tmpl, "setKey", SetKey);
SetProtoMethod(isolate, tmpl, "setCert", SetCert);
SetProtoMethod(isolate, tmpl, "addCACert", AddCACert);
SetProtoMethod(isolate, tmpl, "addCRL", AddCRL);
SetProtoMethod(isolate, tmpl, "addRootCerts", AddRootCerts);
SetProtoMethod(isolate, tmpl, "setCipherSuites", SetCipherSuites);
SetProtoMethod(isolate, tmpl, "setCiphers", SetCiphers);
SetProtoMethod(isolate, tmpl, "setSigalgs", SetSigalgs);
SetProtoMethod(isolate, tmpl, "setECDHCurve", SetECDHCurve);
SetProtoMethod(isolate, tmpl, "setDHParam", SetDHParam);
SetProtoMethod(isolate, tmpl, "setMaxProto", SetMaxProto);
SetProtoMethod(isolate, tmpl, "setMinProto", SetMinProto);
SetProtoMethod(isolate, tmpl, "getMaxProto", GetMaxProto);
SetProtoMethod(isolate, tmpl, "getMinProto", GetMinProto);
SetProtoMethod(isolate, tmpl, "setOptions", SetOptions);
SetProtoMethod(isolate, tmpl, "setSessionIdContext", SetSessionIdContext);
SetProtoMethod(isolate, tmpl, "setSessionTimeout", SetSessionTimeout);
SetProtoMethod(isolate, tmpl, "close", Close);
SetProtoMethod(isolate, tmpl, "loadPKCS12", LoadPKCS12);
SetProtoMethod(isolate, tmpl, "setTicketKeys", SetTicketKeys);
SetProtoMethod(isolate, tmpl, "setFreeListLength", SetFreeListLength);
SetProtoMethod(
isolate, tmpl, "enableTicketKeyCallback", EnableTicketKeyCallback);

SetProtoMethodNoSideEffect(isolate, tmpl, "getTicketKeys", GetTicketKeys);
SetProtoMethodNoSideEffect(
isolate, tmpl, "getCertificate", GetCertificate<true>);
SetProtoMethodNoSideEffect(
isolate, tmpl, "getIssuer", GetCertificate<false>);

#ifndef OPENSSL_NO_ENGINE
SetProtoMethod(isolate, tmpl, "setEngineKey", SetEngineKey);
SetProtoMethod(isolate, tmpl, "setClientCertEngine", SetClientCertEngine);
#endif // !OPENSSL_NO_ENGINE

#define SET_INTEGER_CONSTANTS(name, value) \
tmpl->Set(FIXED_ONE_BYTE_STRING(env->isolate(), name), \
Expand Down Expand Up @@ -326,17 +328,20 @@ Local<FunctionTemplate> SecureContext::GetConstructorTemplate(
}

void SecureContext::Initialize(Environment* env, Local<Object> target) {
env->SetConstructorFunction(
target,
"SecureContext",
GetConstructorTemplate(env),
Environment::SetConstructorFunctionFlag::NONE);

env->SetMethodNoSideEffect(target, "getRootCertificates",
GetRootCertificates);
Local<Context> context = env->context();
SetConstructorFunction(context,
target,
"SecureContext",
GetConstructorTemplate(env),
SetConstructorFunctionFlag::NONE);

SetMethodNoSideEffect(
context, target, "getRootCertificates", GetRootCertificates);
// Exposed for testing purposes only.
env->SetMethodNoSideEffect(target, "isExtraRootCertsFileLoaded",
IsExtraRootCertsFileLoaded);
SetMethodNoSideEffect(context,
target,
"isExtraRootCertsFileLoaded",
IsExtraRootCertsFileLoaded);
}

void SecureContext::RegisterExternalReferences(
Expand Down
Loading

0 comments on commit b3d85b7

Please sign in to comment.