Skip to content

Commit b956ac4

Browse files
committed
src: split property helpers from node::Environment
PR-URL: nodejs#44056 Refs: nodejs#42528 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Feng Yu <F3n67u@outlook.com>
1 parent 96c3373 commit b956ac4

Some content is hidden

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

79 files changed

+1503
-1292
lines changed

src/README.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -390,32 +390,33 @@ void Initialize(Local<Object> target,
390390
void* priv) {
391391
Environment* env = Environment::GetCurrent(context);
392392

393-
env->SetMethod(target, "getaddrinfo", GetAddrInfo);
394-
env->SetMethod(target, "getnameinfo", GetNameInfo);
393+
SetMethod(context, target, "getaddrinfo", GetAddrInfo);
394+
SetMethod(context, target, "getnameinfo", GetNameInfo);
395395

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

400400
// ... more code ...
401401

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

409410
// Set various methods on the class (i.e. on the prototype):
410-
env->SetProtoMethod(channel_wrap, "queryAny", Query<QueryAnyWrap>);
411-
env->SetProtoMethod(channel_wrap, "queryA", Query<QueryAWrap>);
411+
SetProtoMethod(isolate, channel_wrap, "queryAny", Query<QueryAnyWrap>);
412+
SetProtoMethod(isolate, channel_wrap, "queryA", Query<QueryAWrap>);
412413
// ...
413-
env->SetProtoMethod(channel_wrap, "querySoa", Query<QuerySoaWrap>);
414-
env->SetProtoMethod(channel_wrap, "getHostByAddr", Query<GetHostByAddrWrap>);
414+
SetProtoMethod(isolate, channel_wrap, "querySoa", Query<QuerySoaWrap>);
415+
SetProtoMethod(isolate, channel_wrap, "getHostByAddr", Query<GetHostByAddrWrap>);
415416

416-
env->SetProtoMethodNoSideEffect(channel_wrap, "getServers", GetServers);
417+
SetProtoMethodNoSideEffect(isolate, channel_wrap, "getServers", GetServers);
417418

418-
env->SetConstructorFunction(target, "ChannelWrap", channel_wrap);
419+
SetConstructorFunction(context, target, "ChannelWrap", channel_wrap);
419420
}
420421

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

src/async_wrap.cc

+15-13
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,14 @@ void AsyncWrap::SetCallbackTrampoline(const FunctionCallbackInfo<Value>& args) {
337337
Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(Environment* env) {
338338
Local<FunctionTemplate> tmpl = env->async_wrap_ctor_template();
339339
if (tmpl.IsEmpty()) {
340-
tmpl = env->NewFunctionTemplate(nullptr);
340+
Isolate* isolate = env->isolate();
341+
tmpl = NewFunctionTemplate(isolate, nullptr);
341342
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap"));
342343
tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
343-
env->SetProtoMethod(tmpl, "getAsyncId", AsyncWrap::GetAsyncId);
344-
env->SetProtoMethod(tmpl, "asyncReset", AsyncWrap::AsyncReset);
345-
env->SetProtoMethod(tmpl, "getProviderType", AsyncWrap::GetProviderType);
344+
SetProtoMethod(isolate, tmpl, "getAsyncId", AsyncWrap::GetAsyncId);
345+
SetProtoMethod(isolate, tmpl, "asyncReset", AsyncWrap::AsyncReset);
346+
SetProtoMethod(
347+
isolate, tmpl, "getProviderType", AsyncWrap::GetProviderType);
346348
env->set_async_wrap_ctor_template(tmpl);
347349
}
348350
return tmpl;
@@ -356,15 +358,15 @@ void AsyncWrap::Initialize(Local<Object> target,
356358
Isolate* isolate = env->isolate();
357359
HandleScope scope(isolate);
358360

359-
env->SetMethod(target, "setupHooks", SetupHooks);
360-
env->SetMethod(target, "setCallbackTrampoline", SetCallbackTrampoline);
361-
env->SetMethod(target, "pushAsyncContext", PushAsyncContext);
362-
env->SetMethod(target, "popAsyncContext", PopAsyncContext);
363-
env->SetMethod(target, "executionAsyncResource", ExecutionAsyncResource);
364-
env->SetMethod(target, "clearAsyncIdStack", ClearAsyncIdStack);
365-
env->SetMethod(target, "queueDestroyAsyncId", QueueDestroyAsyncId);
366-
env->SetMethod(target, "setPromiseHooks", SetPromiseHooks);
367-
env->SetMethod(target, "registerDestroyHook", RegisterDestroyHook);
361+
SetMethod(context, target, "setupHooks", SetupHooks);
362+
SetMethod(context, target, "setCallbackTrampoline", SetCallbackTrampoline);
363+
SetMethod(context, target, "pushAsyncContext", PushAsyncContext);
364+
SetMethod(context, target, "popAsyncContext", PopAsyncContext);
365+
SetMethod(context, target, "executionAsyncResource", ExecutionAsyncResource);
366+
SetMethod(context, target, "clearAsyncIdStack", ClearAsyncIdStack);
367+
SetMethod(context, target, "queueDestroyAsyncId", QueueDestroyAsyncId);
368+
SetMethod(context, target, "setPromiseHooks", SetPromiseHooks);
369+
SetMethod(context, target, "registerDestroyHook", RegisterDestroyHook);
368370

369371
PropertyAttribute ReadOnlyDontDelete =
370372
static_cast<PropertyAttribute>(ReadOnly | DontDelete);

src/cares_wrap.cc

+30-28
Original file line numberDiff line numberDiff line change
@@ -1886,12 +1886,13 @@ void Initialize(Local<Object> target,
18861886
Local<Context> context,
18871887
void* priv) {
18881888
Environment* env = Environment::GetCurrent(context);
1889+
Isolate* isolate = env->isolate();
18891890

1890-
env->SetMethod(target, "getaddrinfo", GetAddrInfo);
1891-
env->SetMethod(target, "getnameinfo", GetNameInfo);
1892-
env->SetMethodNoSideEffect(target, "canonicalizeIP", CanonicalizeIP);
1891+
SetMethod(context, target, "getaddrinfo", GetAddrInfo);
1892+
SetMethod(context, target, "getnameinfo", GetNameInfo);
1893+
SetMethodNoSideEffect(context, target, "canonicalizeIP", CanonicalizeIP);
18931894

1894-
env->SetMethod(target, "strerror", StrError);
1895+
SetMethod(context, target, "strerror", StrError);
18951896

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

19181919
Local<FunctionTemplate> niw =
19191920
BaseObject::MakeLazilyInitializedJSTemplate(env);
19201921
niw->Inherit(AsyncWrap::GetConstructorTemplate(env));
1921-
env->SetConstructorFunction(target, "GetNameInfoReqWrap", niw);
1922+
SetConstructorFunction(context, target, "GetNameInfoReqWrap", niw);
19221923

19231924
Local<FunctionTemplate> qrw =
19241925
BaseObject::MakeLazilyInitializedJSTemplate(env);
19251926
qrw->Inherit(AsyncWrap::GetConstructorTemplate(env));
1926-
env->SetConstructorFunction(target, "QueryReqWrap", qrw);
1927+
SetConstructorFunction(context, target, "QueryReqWrap", qrw);
19271928

19281929
Local<FunctionTemplate> channel_wrap =
1929-
env->NewFunctionTemplate(ChannelWrap::New);
1930+
NewFunctionTemplate(isolate, ChannelWrap::New);
19301931
channel_wrap->InstanceTemplate()->SetInternalFieldCount(
19311932
ChannelWrap::kInternalFieldCount);
19321933
channel_wrap->Inherit(AsyncWrap::GetConstructorTemplate(env));
19331934

1934-
env->SetProtoMethod(channel_wrap, "queryAny", Query<QueryAnyWrap>);
1935-
env->SetProtoMethod(channel_wrap, "queryA", Query<QueryAWrap>);
1936-
env->SetProtoMethod(channel_wrap, "queryAaaa", Query<QueryAaaaWrap>);
1937-
env->SetProtoMethod(channel_wrap, "queryCaa", Query<QueryCaaWrap>);
1938-
env->SetProtoMethod(channel_wrap, "queryCname", Query<QueryCnameWrap>);
1939-
env->SetProtoMethod(channel_wrap, "queryMx", Query<QueryMxWrap>);
1940-
env->SetProtoMethod(channel_wrap, "queryNs", Query<QueryNsWrap>);
1941-
env->SetProtoMethod(channel_wrap, "queryTxt", Query<QueryTxtWrap>);
1942-
env->SetProtoMethod(channel_wrap, "querySrv", Query<QuerySrvWrap>);
1943-
env->SetProtoMethod(channel_wrap, "queryPtr", Query<QueryPtrWrap>);
1944-
env->SetProtoMethod(channel_wrap, "queryNaptr", Query<QueryNaptrWrap>);
1945-
env->SetProtoMethod(channel_wrap, "querySoa", Query<QuerySoaWrap>);
1946-
env->SetProtoMethod(channel_wrap, "getHostByAddr", Query<GetHostByAddrWrap>);
1947-
1948-
env->SetProtoMethodNoSideEffect(channel_wrap, "getServers", GetServers);
1949-
env->SetProtoMethod(channel_wrap, "setServers", SetServers);
1950-
env->SetProtoMethod(channel_wrap, "setLocalAddress", SetLocalAddress);
1951-
env->SetProtoMethod(channel_wrap, "cancel", Cancel);
1952-
1953-
env->SetConstructorFunction(target, "ChannelWrap", channel_wrap);
1935+
SetProtoMethod(isolate, channel_wrap, "queryAny", Query<QueryAnyWrap>);
1936+
SetProtoMethod(isolate, channel_wrap, "queryA", Query<QueryAWrap>);
1937+
SetProtoMethod(isolate, channel_wrap, "queryAaaa", Query<QueryAaaaWrap>);
1938+
SetProtoMethod(isolate, channel_wrap, "queryCaa", Query<QueryCaaWrap>);
1939+
SetProtoMethod(isolate, channel_wrap, "queryCname", Query<QueryCnameWrap>);
1940+
SetProtoMethod(isolate, channel_wrap, "queryMx", Query<QueryMxWrap>);
1941+
SetProtoMethod(isolate, channel_wrap, "queryNs", Query<QueryNsWrap>);
1942+
SetProtoMethod(isolate, channel_wrap, "queryTxt", Query<QueryTxtWrap>);
1943+
SetProtoMethod(isolate, channel_wrap, "querySrv", Query<QuerySrvWrap>);
1944+
SetProtoMethod(isolate, channel_wrap, "queryPtr", Query<QueryPtrWrap>);
1945+
SetProtoMethod(isolate, channel_wrap, "queryNaptr", Query<QueryNaptrWrap>);
1946+
SetProtoMethod(isolate, channel_wrap, "querySoa", Query<QuerySoaWrap>);
1947+
SetProtoMethod(
1948+
isolate, channel_wrap, "getHostByAddr", Query<GetHostByAddrWrap>);
1949+
1950+
SetProtoMethodNoSideEffect(isolate, channel_wrap, "getServers", GetServers);
1951+
SetProtoMethod(isolate, channel_wrap, "setServers", SetServers);
1952+
SetProtoMethod(isolate, channel_wrap, "setLocalAddress", SetLocalAddress);
1953+
SetProtoMethod(isolate, channel_wrap, "cancel", Cancel);
1954+
1955+
SetConstructorFunction(context, target, "ChannelWrap", channel_wrap);
19541956
}
19551957

19561958
} // namespace cares_wrap

src/crypto/crypto_cipher.cc

+45-32
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ namespace node {
1313
using v8::Array;
1414
using v8::ArrayBuffer;
1515
using v8::BackingStore;
16+
using v8::Context;
1617
using v8::FunctionCallbackInfo;
1718
using v8::FunctionTemplate;
1819
using v8::HandleScope;
1920
using v8::Int32;
21+
using v8::Isolate;
2022
using v8::Local;
2123
using v8::Object;
2224
using v8::Uint32;
@@ -270,43 +272,54 @@ void CipherBase::MemoryInfo(MemoryTracker* tracker) const {
270272
}
271273

272274
void CipherBase::Initialize(Environment* env, Local<Object> target) {
273-
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
275+
Isolate* isolate = env->isolate();
276+
Local<Context> context = env->context();
277+
278+
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
274279

275280
t->InstanceTemplate()->SetInternalFieldCount(
276281
CipherBase::kInternalFieldCount);
277282
t->Inherit(BaseObject::GetConstructorTemplate(env));
278283

279-
env->SetProtoMethod(t, "init", Init);
280-
env->SetProtoMethod(t, "initiv", InitIv);
281-
env->SetProtoMethod(t, "update", Update);
282-
env->SetProtoMethod(t, "final", Final);
283-
env->SetProtoMethod(t, "setAutoPadding", SetAutoPadding);
284-
env->SetProtoMethodNoSideEffect(t, "getAuthTag", GetAuthTag);
285-
env->SetProtoMethod(t, "setAuthTag", SetAuthTag);
286-
env->SetProtoMethod(t, "setAAD", SetAAD);
287-
env->SetConstructorFunction(target, "CipherBase", t);
288-
289-
env->SetMethodNoSideEffect(target, "getSSLCiphers", GetSSLCiphers);
290-
env->SetMethodNoSideEffect(target, "getCiphers", GetCiphers);
291-
292-
env->SetMethod(target, "publicEncrypt",
293-
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
294-
EVP_PKEY_encrypt_init,
295-
EVP_PKEY_encrypt>);
296-
env->SetMethod(target, "privateDecrypt",
297-
PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
298-
EVP_PKEY_decrypt_init,
299-
EVP_PKEY_decrypt>);
300-
env->SetMethod(target, "privateEncrypt",
301-
PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
302-
EVP_PKEY_sign_init,
303-
EVP_PKEY_sign>);
304-
env->SetMethod(target, "publicDecrypt",
305-
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
306-
EVP_PKEY_verify_recover_init,
307-
EVP_PKEY_verify_recover>);
308-
309-
env->SetMethodNoSideEffect(target, "getCipherInfo", GetCipherInfo);
284+
SetProtoMethod(isolate, t, "init", Init);
285+
SetProtoMethod(isolate, t, "initiv", InitIv);
286+
SetProtoMethod(isolate, t, "update", Update);
287+
SetProtoMethod(isolate, t, "final", Final);
288+
SetProtoMethod(isolate, t, "setAutoPadding", SetAutoPadding);
289+
SetProtoMethodNoSideEffect(isolate, t, "getAuthTag", GetAuthTag);
290+
SetProtoMethod(isolate, t, "setAuthTag", SetAuthTag);
291+
SetProtoMethod(isolate, t, "setAAD", SetAAD);
292+
SetConstructorFunction(context, target, "CipherBase", t);
293+
294+
SetMethodNoSideEffect(context, target, "getSSLCiphers", GetSSLCiphers);
295+
SetMethodNoSideEffect(context, target, "getCiphers", GetCiphers);
296+
297+
SetMethod(context,
298+
target,
299+
"publicEncrypt",
300+
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
301+
EVP_PKEY_encrypt_init,
302+
EVP_PKEY_encrypt>);
303+
SetMethod(context,
304+
target,
305+
"privateDecrypt",
306+
PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
307+
EVP_PKEY_decrypt_init,
308+
EVP_PKEY_decrypt>);
309+
SetMethod(context,
310+
target,
311+
"privateEncrypt",
312+
PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
313+
EVP_PKEY_sign_init,
314+
EVP_PKEY_sign>);
315+
SetMethod(context,
316+
target,
317+
"publicDecrypt",
318+
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
319+
EVP_PKEY_verify_recover_init,
320+
EVP_PKEY_verify_recover>);
321+
322+
SetMethodNoSideEffect(context, target, "getCipherInfo", GetCipherInfo);
310323

311324
NODE_DEFINE_CONSTANT(target, kWebCryptoCipherEncrypt);
312325
NODE_DEFINE_CONSTANT(target, kWebCryptoCipherDecrypt);

0 commit comments

Comments
 (0)