diff --git a/src/crypto/crypto_keys.cc b/src/crypto/crypto_keys.cc index 5b11b125830..580f9ac009d 100644 --- a/src/crypto/crypto_keys.cc +++ b/src/crypto/crypto_keys.cc @@ -1692,12 +1692,13 @@ void NativeKeyObject::CreateNativeKeyObjectClass( Local callback = args[0]; CHECK(callback->IsFunction()); - Local t = - NewFunctionTemplate(isolate, NativeKeyObject::New); - t->InstanceTemplate()->SetInternalFieldCount( - NativeKeyObject::kInternalFieldCount); - CHECK(env->crypto_key_object_constructor_template().IsEmpty()); - env->set_crypto_key_object_constructor_template(t); + Local t = env->crypto_key_object_constructor_template(); + if (t.IsEmpty()) { + t = NewFunctionTemplate(isolate, NativeKeyObject::New); + t->InstanceTemplate()->SetInternalFieldCount( + NativeKeyObject::kInternalFieldCount); + env->set_crypto_key_object_constructor_template(t); + } Local ctor; if (!t->GetFunction(env->context()).ToLocal(&ctor)) @@ -1929,12 +1930,13 @@ void NativeCryptoKey::CreateCryptoKeyClass( Local callback = args[0]; CHECK(callback->IsFunction()); - Local t = - NewFunctionTemplate(isolate, NativeCryptoKey::New); - t->InstanceTemplate()->SetInternalFieldCount( - NativeCryptoKey::kInternalFieldCount); - CHECK(env->crypto_cryptokey_constructor_template().IsEmpty()); - env->set_crypto_cryptokey_constructor_template(t); + Local t = env->crypto_cryptokey_constructor_template(); + if (t.IsEmpty()) { + t = NewFunctionTemplate(isolate, NativeCryptoKey::New); + t->InstanceTemplate()->SetInternalFieldCount( + NativeCryptoKey::kInternalFieldCount); + env->set_crypto_cryptokey_constructor_template(t); + } Local ctor; if (!t->GetFunction(env->context()).ToLocal(&ctor)) return; diff --git a/src/dtls/dtls_context.cc b/src/dtls/dtls_context.cc index 7b1c3efcfb9..baf8647dda7 100644 --- a/src/dtls/dtls_context.cc +++ b/src/dtls/dtls_context.cc @@ -170,8 +170,11 @@ Local DTLSContext::GetConstructorTemplate(Environment* env) { void DTLSContext::InitPerContext(Local target, Local context, Environment* env) { - SetConstructorFunction( - context, target, "DTLSContext", GetConstructorTemplate(env)); + SetConstructorFunction(context, + target, + "DTLSContext", + GetConstructorTemplate(env), + SetConstructorFunctionFlag::NONE); } void DTLSContext::RegisterExternalReferences( diff --git a/src/dtls/dtls_endpoint.cc b/src/dtls/dtls_endpoint.cc index 59b4d723383..7f85c13fc33 100644 --- a/src/dtls/dtls_endpoint.cc +++ b/src/dtls/dtls_endpoint.cc @@ -108,8 +108,11 @@ Local DTLSEndpoint::GetConstructorTemplate(Environment* env) { void DTLSEndpoint::InitPerContext(Local target, Local context, Environment* env) { - SetConstructorFunction( - context, target, "DTLSEndpoint", GetConstructorTemplate(env)); + SetConstructorFunction(context, + target, + "DTLSEndpoint", + GetConstructorTemplate(env), + SetConstructorFunctionFlag::NONE); } void DTLSEndpoint::RegisterExternalReferences( diff --git a/src/dtls/dtls_session.cc b/src/dtls/dtls_session.cc index a410b5f6e2a..d2fc9cd8639 100644 --- a/src/dtls/dtls_session.cc +++ b/src/dtls/dtls_session.cc @@ -204,8 +204,11 @@ Local DTLSSession::GetConstructorTemplate(Environment* env) { void DTLSSession::InitPerContext(Local target, Local context, Environment* env) { - SetConstructorFunction( - context, target, "DTLSSession", GetConstructorTemplate(env)); + SetConstructorFunction(context, + target, + "DTLSSession", + GetConstructorTemplate(env), + SetConstructorFunctionFlag::NONE); } void DTLSSession::RegisterExternalReferences( diff --git a/src/env_properties.h b/src/env_properties.h index 10838f9fd06..69cc0d2ea93 100644 --- a/src/env_properties.h +++ b/src/env_properties.h @@ -424,7 +424,7 @@ V(v8_heap_statistics_template, v8::DictionaryTemplate) \ V(histogram_ctor_template, v8::FunctionTemplate) \ V(http2settings_constructor_template, v8::ObjectTemplate) \ - V(http2stream_constructor_template, v8::ObjectTemplate) \ + V(http2stream_constructor_template, v8::FunctionTemplate) \ V(http2ping_constructor_template, v8::ObjectTemplate) \ V(i18n_converter_template, v8::ObjectTemplate) \ V(intervalhistogram_constructor_template, v8::FunctionTemplate) \ @@ -445,7 +445,7 @@ V(pipe_constructor_template, v8::FunctionTemplate) \ V(script_context_constructor_template, v8::FunctionTemplate) \ V(secure_context_constructor_template, v8::FunctionTemplate) \ - V(shutdown_wrap_template, v8::ObjectTemplate) \ + V(shutdown_wrap_template, v8::FunctionTemplate) \ V(soa_record_template, v8::DictionaryTemplate) \ V(socketaddress_constructor_template, v8::FunctionTemplate) \ V(space_stats_template, v8::DictionaryTemplate) \ @@ -464,7 +464,7 @@ V(urlpatterncomponentresult_template, v8::DictionaryTemplate) \ V(urlpatterninit_template, v8::DictionaryTemplate) \ V(urlpatternresult_template, v8::DictionaryTemplate) \ - V(write_wrap_template, v8::ObjectTemplate) \ + V(write_wrap_template, v8::FunctionTemplate) \ V(worker_cpu_profile_taker_template, v8::ObjectTemplate) \ V(worker_cpu_usage_taker_template, v8::ObjectTemplate) \ V(worker_heap_profile_taker_template, v8::ObjectTemplate) \ diff --git a/src/node_ffi.cc b/src/node_ffi.cc index 2748458104f..732657a368e 100644 --- a/src/node_ffi.cc +++ b/src/node_ffi.cc @@ -1276,6 +1276,7 @@ Local DynamicLibrary::GetConstructorTemplate( static_cast(ReadOnly | DontDelete); tmpl = NewFunctionTemplate(isolate, DynamicLibrary::New); + tmpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "DynamicLibrary")); tmpl->InstanceTemplate()->SetInternalFieldCount( DynamicLibrary::kInternalFieldCount); Local signature = Signature::New(isolate, tmpl); @@ -1347,7 +1348,11 @@ static void Initialize(Local target, // Create the DynamicLibrary template Local dl_tmpl = DynamicLibrary::GetConstructorTemplate(env); - SetConstructorFunction(context, target, "DynamicLibrary", dl_tmpl); + SetConstructorFunction(context, + target, + "DynamicLibrary", + dl_tmpl, + SetConstructorFunctionFlag::NONE); SetMethod(context, target, "toString", ToString); SetMethod(context, target, "toBuffer", ToBuffer); SetMethod(context, target, "toArrayBuffer", ToArrayBuffer); diff --git a/src/node_http2.cc b/src/node_http2.cc index e4b191d3ea2..9aae3846bab 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -2252,6 +2252,7 @@ Http2Stream* Http2Stream::New(Http2Session* session, Local obj; if (!session->env() ->http2stream_constructor_template() + ->InstanceTemplate() ->NewInstance(session->env()->context()) .ToLocal(&obj)) { return nullptr; @@ -3568,35 +3569,44 @@ void Initialize(Local target, SetMethod(context, target, "packSettings", PackSettings); SetMethod(context, target, "setCallbackFunctions", SetCallbackFunctions); - Local ping = FunctionTemplate::New(env->isolate()); - ping->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Ping")); - ping->Inherit(AsyncWrap::GetConstructorTemplate(env)); - Local pingt = ping->InstanceTemplate(); - pingt->SetInternalFieldCount(Http2Ping::kInternalFieldCount); - env->set_http2ping_constructor_template(pingt); + if (env->http2ping_constructor_template().IsEmpty()) { + Local ping = FunctionTemplate::New(env->isolate()); + ping->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Ping")); + ping->Inherit(AsyncWrap::GetConstructorTemplate(env)); + Local pingt = ping->InstanceTemplate(); + pingt->SetInternalFieldCount(Http2Ping::kInternalFieldCount); + env->set_http2ping_constructor_template(pingt); + } - Local setting = FunctionTemplate::New(env->isolate()); - setting->Inherit(AsyncWrap::GetConstructorTemplate(env)); - Local settingt = setting->InstanceTemplate(); - settingt->SetInternalFieldCount(Http2Settings::kInternalFieldCount); - env->set_http2settings_constructor_template(settingt); + if (env->http2settings_constructor_template().IsEmpty()) { + Local setting = FunctionTemplate::New(env->isolate()); + setting->Inherit(AsyncWrap::GetConstructorTemplate(env)); + Local settingt = setting->InstanceTemplate(); + settingt->SetInternalFieldCount(Http2Settings::kInternalFieldCount); + env->set_http2settings_constructor_template(settingt); + } - Local stream = FunctionTemplate::New(env->isolate()); - SetProtoMethod(isolate, stream, "id", Http2Stream::GetID); - SetProtoMethod(isolate, stream, "destroy", Http2Stream::Destroy); - SetProtoMethod(isolate, stream, "priority", Http2Stream::Priority); - SetProtoMethod(isolate, stream, "pushPromise", Http2Stream::PushPromise); - SetProtoMethod(isolate, stream, "info", Http2Stream::Info); - SetProtoMethod(isolate, stream, "trailers", Http2Stream::Trailers); - SetProtoMethod(isolate, stream, "respond", Http2Stream::Respond); - SetProtoMethod(isolate, stream, "rstStream", Http2Stream::RstStream); - SetProtoMethod(isolate, stream, "refreshState", Http2Stream::RefreshState); - stream->Inherit(AsyncWrap::GetConstructorTemplate(env)); - StreamBase::AddMethods(env, stream); - Local streamt = stream->InstanceTemplate(); - streamt->SetInternalFieldCount(Http2Stream::kInternalFieldCount); - env->set_http2stream_constructor_template(streamt); - SetConstructorFunction(context, target, "Http2Stream", stream); + Local stream = env->http2stream_constructor_template(); + if (stream.IsEmpty()) { + stream = FunctionTemplate::New(env->isolate()); + SetProtoMethod(isolate, stream, "id", Http2Stream::GetID); + SetProtoMethod(isolate, stream, "destroy", Http2Stream::Destroy); + SetProtoMethod(isolate, stream, "priority", Http2Stream::Priority); + SetProtoMethod(isolate, stream, "pushPromise", Http2Stream::PushPromise); + SetProtoMethod(isolate, stream, "info", Http2Stream::Info); + SetProtoMethod(isolate, stream, "trailers", Http2Stream::Trailers); + SetProtoMethod(isolate, stream, "respond", Http2Stream::Respond); + SetProtoMethod(isolate, stream, "rstStream", Http2Stream::RstStream); + SetProtoMethod(isolate, stream, "refreshState", Http2Stream::RefreshState); + stream->Inherit(AsyncWrap::GetConstructorTemplate(env)); + StreamBase::AddMethods(env, stream); + stream->InstanceTemplate()->SetInternalFieldCount( + Http2Stream::kInternalFieldCount); + stream->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "Http2Stream")); + env->set_http2stream_constructor_template(stream); + } + SetConstructorFunction( + context, target, "Http2Stream", stream, SetConstructorFunctionFlag::NONE); Local session = NewFunctionTemplate(isolate, Http2Session::New); diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 91ad0dc1db4..0e9691a13b2 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -5372,9 +5372,13 @@ static void Initialize(Local target, SetConstructorFunction(context, target, "StatementSync", - StatementSync::GetConstructorTemplate(env)); - SetConstructorFunction( - context, target, "Session", Session::GetConstructorTemplate(env)); + StatementSync::GetConstructorTemplate(env), + SetConstructorFunctionFlag::NONE); + SetConstructorFunction(context, + target, + "Session", + Session::GetConstructorTemplate(env), + SetConstructorFunctionFlag::NONE); target->Set(context, env->constants_string(), constants).Check(); diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 7fbdd0a0556..4021a59ecb2 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -71,24 +71,28 @@ void PipeWrap::Initialize(Local target, Environment* env = Environment::GetCurrent(context); Isolate* isolate = env->isolate(); - Local t = NewFunctionTemplate(isolate, New); - t->InstanceTemplate()->SetInternalFieldCount(PipeWrap::kInternalFieldCount); + Local t = env->pipe_constructor_template(); + if (t.IsEmpty()) { + t = NewFunctionTemplate(isolate, New); + t->InstanceTemplate()->SetInternalFieldCount(PipeWrap::kInternalFieldCount); - t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env)); + t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env)); - SetProtoMethod(isolate, t, "bind", Bind); - SetProtoMethod(isolate, t, "listen", Listen); - SetProtoMethod(isolate, t, "connect", Connect); - SetProtoMethod(isolate, t, "open", Open); + SetProtoMethod(isolate, t, "bind", Bind); + SetProtoMethod(isolate, t, "listen", Listen); + SetProtoMethod(isolate, t, "connect", Connect); + SetProtoMethod(isolate, t, "open", Open); #ifdef _WIN32 - SetProtoMethod(isolate, t, "setPendingInstances", SetPendingInstances); + SetProtoMethod(isolate, t, "setPendingInstances", SetPendingInstances); #endif - SetProtoMethod(isolate, t, "fchmod", Fchmod); - - SetConstructorFunction(context, target, "Pipe", t); - env->set_pipe_constructor_template(t); + SetProtoMethod(isolate, t, "fchmod", Fchmod); + t->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "Pipe")); + env->set_pipe_constructor_template(t); + } + SetConstructorFunction( + context, target, "Pipe", t, SetConstructorFunctionFlag::NONE); // Create FunctionTemplate for PipeConnectWrap. auto cwt = AsyncWrap::MakeLazilyInitializedJSTemplate(env); diff --git a/src/stream_base.cc b/src/stream_base.cc index 8b94f9eb422..817b5d00a9f 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -48,6 +48,7 @@ int StreamBase::Shutdown(v8::Local req_wrap_obj) { if (req_wrap_obj.IsEmpty()) { if (!env->shutdown_wrap_template() + ->InstanceTemplate() ->NewInstance(env->context()) .ToLocal(&req_wrap_obj)) { return UV_EBUSY; @@ -103,6 +104,7 @@ StreamWriteResult StreamBase::Write(uv_buf_t* bufs, if (req_wrap_obj.IsEmpty()) { if (!env->write_wrap_template() + ->InstanceTemplate() ->NewInstance(env->context()) .ToLocal(&req_wrap_obj)) { return StreamWriteResult{false, UV_EBUSY, nullptr, 0, {}}; @@ -332,6 +334,7 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo& args) { if (lazy_req) { // Sending a handle requires a request object up front to reference it. if (!env->write_wrap_template() + ->InstanceTemplate() ->NewInstance(env->context()) .ToLocal(&req_wrap_obj)) { return UV_EBUSY; @@ -447,6 +450,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo& args) { if (lazy_req && req_wrap_obj.IsEmpty()) { // Sending a handle requires a request object up front to reference it. if (!env->write_wrap_template() + ->InstanceTemplate() ->NewInstance(env->context()) .ToLocal(&req_wrap_obj)) { return UV_EBUSY; diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index ac80044de4c..e2ffaa0f4e1 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -53,6 +53,7 @@ using v8::Maybe; using v8::MaybeLocal; using v8::Nothing; using v8::Object; +using v8::ObjectTemplate; using v8::PropertyAttribute; using v8::ReadOnly; using v8::Signature; @@ -70,48 +71,54 @@ void LibuvStreamWrap::Initialize(Local target, Environment* env = Environment::GetCurrent(context); Isolate* isolate = env->isolate(); - Local sw = - NewFunctionTemplate(isolate, IsConstructCallCallback); - sw->InstanceTemplate()->SetInternalFieldCount( - ShutdownWrap::kInternalFieldCount); + Local sw = env->shutdown_wrap_template(); + if (sw.IsEmpty()) { + sw = NewFunctionTemplate(isolate, IsConstructCallCallback); + sw->InstanceTemplate()->SetInternalFieldCount( + ShutdownWrap::kInternalFieldCount); - // we need to set handle and callback to null, - // so that those fields are created and functions - // do not become megamorphic - // Fields: - // - oncomplete - // - callback - // - handle - sw->InstanceTemplate()->Set(env->oncomplete_string(), v8::Null(isolate)); - sw->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(isolate, "callback"), - v8::Null(isolate)); - sw->InstanceTemplate()->Set(env->handle_string(), v8::Null(isolate)); + // we need to set handle and callback to null, + // so that those fields are created and functions + // do not become megamorphic + // Fields: + // - oncomplete + // - callback + // - handle + sw->InstanceTemplate()->Set(env->oncomplete_string(), v8::Null(isolate)); + sw->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(isolate, "callback"), + v8::Null(isolate)); + sw->InstanceTemplate()->Set(env->handle_string(), v8::Null(isolate)); - sw->Inherit(AsyncWrap::GetConstructorTemplate(env)); + sw->Inherit(AsyncWrap::GetConstructorTemplate(env)); + sw->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "ShutdownWrap")); + env->set_shutdown_wrap_template(sw); + } + SetConstructorFunction( + context, target, "ShutdownWrap", sw, SetConstructorFunctionFlag::NONE); - SetConstructorFunction(context, target, "ShutdownWrap", sw); - env->set_shutdown_wrap_template(sw->InstanceTemplate()); - - Local ww = - FunctionTemplate::New(isolate, IsConstructCallCallback); - ww->InstanceTemplate()->SetInternalFieldCount(WriteWrap::kInternalFieldCount); - // Pre-create the fields that JS attaches to write requests, so that they - // are in-object properties and the object shape stays monomorphic. - ww->InstanceTemplate()->Set(env->oncomplete_string(), v8::Null(isolate)); - ww->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(isolate, "callback"), - v8::Null(isolate)); - ww->InstanceTemplate()->Set(env->handle_string(), v8::Null(isolate)); - ww->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(isolate, "async"), - v8::False(isolate)); - ww->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(isolate, "bytes"), - v8::Integer::New(isolate, 0)); - ww->InstanceTemplate()->Set(env->buffer_string(), v8::Null(isolate)); - // Slot for a completion that fires before JS attaches `oncomplete`; see - // ReportWritesToJSStreamListener::OnStreamAfterReqFinished(). - ww->InstanceTemplate()->Set(env->write_status_string(), v8::Null(isolate)); - ww->Inherit(AsyncWrap::GetConstructorTemplate(env)); - SetConstructorFunction(context, target, "WriteWrap", ww); - env->set_write_wrap_template(ww->InstanceTemplate()); + Local ww = env->write_wrap_template(); + if (ww.IsEmpty()) { + ww = FunctionTemplate::New(isolate, IsConstructCallCallback); + Local wwt = ww->InstanceTemplate(); + wwt->SetInternalFieldCount(WriteWrap::kInternalFieldCount); + // Pre-create the fields that JS attaches to write requests, so that they + // are in-object properties and the object shape stays monomorphic. + wwt->Set(env->oncomplete_string(), v8::Null(isolate)); + wwt->Set(FIXED_ONE_BYTE_STRING(isolate, "callback"), v8::Null(isolate)); + wwt->Set(env->handle_string(), v8::Null(isolate)); + wwt->Set(FIXED_ONE_BYTE_STRING(isolate, "async"), v8::False(isolate)); + wwt->Set(FIXED_ONE_BYTE_STRING(isolate, "bytes"), + v8::Integer::New(isolate, 0)); + wwt->Set(env->buffer_string(), v8::Null(isolate)); + // Slot for a completion that fires before JS attaches `oncomplete`; see + // ReportWritesToJSStreamListener::OnStreamAfterReqFinished(). + wwt->Set(env->write_status_string(), v8::Null(isolate)); + ww->Inherit(AsyncWrap::GetConstructorTemplate(env)); + ww->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "WriteWrap")); + env->set_write_wrap_template(ww); + } + SetConstructorFunction( + context, target, "WriteWrap", ww, SetConstructorFunctionFlag::NONE); NODE_DEFINE_CONSTANT(target, kReadBytesOrError); NODE_DEFINE_CONSTANT(target, kArrayBufferOffset); diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index c5621b6c8c8..68439487700 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -88,43 +88,49 @@ void TCPWrap::Initialize(Local target, Environment* env = Environment::GetCurrent(context); Isolate* isolate = env->isolate(); - Local t = NewFunctionTemplate(isolate, New); - t->InstanceTemplate()->SetInternalFieldCount(TCPWrap::kInternalFieldCount); + Local t = env->tcp_constructor_template(); + if (t.IsEmpty()) { + t = NewFunctionTemplate(isolate, New); + t->InstanceTemplate()->SetInternalFieldCount(TCPWrap::kInternalFieldCount); - // Init properties - t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"), - Boolean::New(env->isolate(), false)); - t->InstanceTemplate()->Set(env->owner_symbol(), Null(env->isolate())); - t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate())); + // Init properties + t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(isolate, "reading"), + Boolean::New(isolate, false)); + t->InstanceTemplate()->Set(env->owner_symbol(), Null(isolate)); + t->InstanceTemplate()->Set(env->onconnection_string(), Null(isolate)); - t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env)); + t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env)); - SetProtoMethod(isolate, t, "open", Open); - SetProtoMethod(isolate, t, "bind", Bind); - SetProtoMethod(isolate, t, "listen", Listen); - SetProtoMethod(isolate, t, "connect", Connect); - SetProtoMethod(isolate, t, "bind6", Bind6); - SetProtoMethod(isolate, t, "connect6", Connect6); - SetProtoMethod(isolate, - t, - "getsockname", - GetSockOrPeerName); - SetProtoMethod(isolate, - t, - "getpeername", - GetSockOrPeerName); - SetProtoMethod(isolate, t, "setNoDelay", SetNoDelay); - SetProtoMethod(isolate, t, "setKeepAlive", SetKeepAlive); - SetProtoMethod(isolate, t, "setTypeOfService", SetTypeOfService); - SetProtoMethod(isolate, t, "getTypeOfService", GetTypeOfService); - SetProtoMethod(isolate, t, "reset", Reset); + SetProtoMethod(isolate, t, "open", Open); + SetProtoMethod(isolate, t, "bind", Bind); + SetProtoMethod(isolate, t, "listen", Listen); + SetProtoMethod(isolate, t, "connect", Connect); + SetProtoMethod(isolate, t, "bind6", Bind6); + SetProtoMethod(isolate, t, "connect6", Connect6); + SetProtoMethod(isolate, + t, + "getsockname", + GetSockOrPeerName); + SetProtoMethod(isolate, + t, + "getpeername", + GetSockOrPeerName); + SetProtoMethod(isolate, t, "setNoDelay", SetNoDelay); + SetProtoMethod(isolate, t, "setKeepAlive", SetKeepAlive); + SetProtoMethod(isolate, t, "setTypeOfService", SetTypeOfService); + SetProtoMethod(isolate, t, "getTypeOfService", GetTypeOfService); + SetProtoMethod(isolate, t, "reset", Reset); #ifdef _WIN32 - SetProtoMethod(isolate, t, "setSimultaneousAccepts", SetSimultaneousAccepts); + SetProtoMethod( + isolate, t, "setSimultaneousAccepts", SetSimultaneousAccepts); #endif - SetConstructorFunction(context, target, "TCP", t); - env->set_tcp_constructor_template(t); + t->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "TCP")); + env->set_tcp_constructor_template(t); + } + SetConstructorFunction( + context, target, "TCP", t, SetConstructorFunctionFlag::NONE); // Create FunctionTemplate for TCPConnectWrap. Local cwt = AsyncWrap::MakeLazilyInitializedJSTemplate(env); diff --git a/src/tty_wrap.cc b/src/tty_wrap.cc index c605ab2fcc1..0c4c02f6aa4 100644 --- a/src/tty_wrap.cc +++ b/src/tty_wrap.cc @@ -58,14 +58,18 @@ void TTYWrap::Initialize(Local target, Local ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"); - Local t = NewFunctionTemplate(isolate, New); - t->SetClassName(ttyString); - t->InstanceTemplate()->SetInternalFieldCount(TTYWrap::kInternalFieldCount); - t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env)); + Local t = env->tty_constructor_template(); + if (t.IsEmpty()) { + t = NewFunctionTemplate(isolate, New); + t->SetClassName(ttyString); + t->InstanceTemplate()->SetInternalFieldCount(TTYWrap::kInternalFieldCount); + t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env)); - SetProtoMethodNoSideEffect( - isolate, t, "getWindowSize", TTYWrap::GetWindowSize); - SetProtoMethod(isolate, t, "setRawMode", SetRawMode); + SetProtoMethodNoSideEffect( + isolate, t, "getWindowSize", TTYWrap::GetWindowSize); + SetProtoMethod(isolate, t, "setRawMode", SetRawMode); + env->set_tty_constructor_template(t); + } SetMethodNoSideEffect(context, target, "isTTY", IsTTY); NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_NORMAL); @@ -73,9 +77,8 @@ void TTYWrap::Initialize(Local target, NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT); Local func; - if (t->GetFunction(context).ToLocal(&func) && - target->Set(context, ttyString, func).IsJust()) { - env->set_tty_constructor_template(t); + if (t->GetFunction(context).ToLocal(&func)) { + target->Set(context, ttyString, func).Check(); } } diff --git a/test/cctest/test_environment.cc b/test/cctest/test_environment.cc index d79da2baba2..1bfd55d9856 100644 --- a/test/cctest/test_environment.cc +++ b/test/cctest/test_environment.cc @@ -446,6 +446,26 @@ TEST_F(EnvironmentTest, CollectExternalReferencesFromSeveralThreads) { EXPECT_EQ(node::SnapshotBuilder::CollectExternalReferences().back(), 0); } +TEST_F(EnvironmentTest, SharedIsolateDataLoadsBindingsTwice) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + const char* script = + "for (const m of require('module').builtinModules) {" + " try { require(m); } catch {}" + "}" + "new (require('net').Socket)();" +#if HAVE_OPENSSL + "require('crypto').createSecretKey(Buffer.alloc(8));" +#endif + "new (require('worker_threads').MessageChannel)().port1.close();"; + Env env1{handle_scope, argv}; + node::LoadEnvironment(*env1, script).ToLocalChecked(); + EXPECT_EQ(node::SpinEventLoop(*env1).FromJust(), 0); + Env env2{handle_scope, argv, node::EnvironmentFlags::kNoCreateInspector}; + node::LoadEnvironment(*env2, script).ToLocalChecked(); + EXPECT_EQ(node::SpinEventLoop(*env2).FromJust(), 0); +} + TEST_F(EnvironmentTest, NoEnvironmentSanity) { const v8::HandleScope handle_scope(isolate_); v8::Local context = v8::Context::New(isolate_);