src: fix abort when two Environments share an IsolateData

A second Environment created from the same `IsolateData`, which
embedding.md allows and the cctest fixture does, aborted as soon as it
loaded `stream_wrap`, `tcp_wrap`, `pipe_wrap`, `tty_wrap`, `http2` or
the crypto key classes, i.e. on `require('net')`: those bindings
created their per-isolate templates from the per-Environment binding
initializer and stored them with a setter that CHECKs the slot is
empty. Templates that were already cached per isolate (ffi, sqlite,
dtls) instead hit "FunctionTemplate already instantiated" when
`SetConstructorFunction()` renamed them.

Build these templates once per isolate and reuse them, set class names
when the template is created, and store the `ShutdownWrap`, `WriteWrap`
and `Http2Stream` function templates rather than their instance
templates so the second Environment can expose the same constructors.

Refs: https://github.com/nodejs/node/pull/43802
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65978
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
This commit is contained in:
Shelley Vohr authored and Antoine du Hamel committed 2026-09-29 00:33:14 +02:00
1 parent 886fd32693
commit caa7858e4c
14 files changed
+217 -143

No files matched your search

+14 -12
View File
@@ -1692,12 +1692,13 @@ void NativeKeyObject::CreateNativeKeyObjectClass(
Local<Value> callback = args[0];
CHECK(callback->IsFunction());
Local<FunctionTemplate> 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<FunctionTemplate> 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<Value> ctor;
if (!t->GetFunction(env->context()).ToLocal(&ctor))
@@ -1929,12 +1930,13 @@ void NativeCryptoKey::CreateCryptoKeyClass(
Local<Value> callback = args[0];
CHECK(callback->IsFunction());
Local<FunctionTemplate> 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<FunctionTemplate> 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<Value> ctor;
if (!t->GetFunction(env->context()).ToLocal(&ctor)) return;
+5 -2
View File
@@ -170,8 +170,11 @@ Local<FunctionTemplate> DTLSContext::GetConstructorTemplate(Environment* env) {
void DTLSContext::InitPerContext(Local<Object> target,
Local<Context> context,
Environment* env) {
SetConstructorFunction(
context, target, "DTLSContext", GetConstructorTemplate(env));
SetConstructorFunction(context,
target,
"DTLSContext",
GetConstructorTemplate(env),
SetConstructorFunctionFlag::NONE);
}
void DTLSContext::RegisterExternalReferences(
+5 -2
View File
@@ -108,8 +108,11 @@ Local<FunctionTemplate> DTLSEndpoint::GetConstructorTemplate(Environment* env) {
void DTLSEndpoint::InitPerContext(Local<Object> target,
Local<Context> context,
Environment* env) {
SetConstructorFunction(
context, target, "DTLSEndpoint", GetConstructorTemplate(env));
SetConstructorFunction(context,
target,
"DTLSEndpoint",
GetConstructorTemplate(env),
SetConstructorFunctionFlag::NONE);
}
void DTLSEndpoint::RegisterExternalReferences(
+5 -2
View File
@@ -204,8 +204,11 @@ Local<FunctionTemplate> DTLSSession::GetConstructorTemplate(Environment* env) {
void DTLSSession::InitPerContext(Local<Object> target,
Local<Context> context,
Environment* env) {
SetConstructorFunction(
context, target, "DTLSSession", GetConstructorTemplate(env));
SetConstructorFunction(context,
target,
"DTLSSession",
GetConstructorTemplate(env),
SetConstructorFunctionFlag::NONE);
}
void DTLSSession::RegisterExternalReferences(
+3 -3
View File
@@ -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) \
+6 -1
View File
@@ -1276,6 +1276,7 @@ Local<FunctionTemplate> DynamicLibrary::GetConstructorTemplate(
static_cast<PropertyAttribute>(ReadOnly | DontDelete);
tmpl = NewFunctionTemplate(isolate, DynamicLibrary::New);
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "DynamicLibrary"));
tmpl->InstanceTemplate()->SetInternalFieldCount(
DynamicLibrary::kInternalFieldCount);
Local<Signature> signature = Signature::New(isolate, tmpl);
@@ -1347,7 +1348,11 @@ static void Initialize(Local<Object> target,
// Create the DynamicLibrary template
Local<FunctionTemplate> 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);
+37 -27
View File
@@ -2252,6 +2252,7 @@ Http2Stream* Http2Stream::New(Http2Session* session,
Local<Object> obj;
if (!session->env()
->http2stream_constructor_template()
->InstanceTemplate()
->NewInstance(session->env()->context())
.ToLocal(&obj)) {
return nullptr;
@@ -3568,35 +3569,44 @@ void Initialize(Local<Object> target,
SetMethod(context, target, "packSettings", PackSettings);
SetMethod(context, target, "setCallbackFunctions", SetCallbackFunctions);
Local<FunctionTemplate> ping = FunctionTemplate::New(env->isolate());
ping->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Ping"));
ping->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<ObjectTemplate> pingt = ping->InstanceTemplate();
pingt->SetInternalFieldCount(Http2Ping::kInternalFieldCount);
env->set_http2ping_constructor_template(pingt);
if (env->http2ping_constructor_template().IsEmpty()) {
Local<FunctionTemplate> ping = FunctionTemplate::New(env->isolate());
ping->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Ping"));
ping->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<ObjectTemplate> pingt = ping->InstanceTemplate();
pingt->SetInternalFieldCount(Http2Ping::kInternalFieldCount);
env->set_http2ping_constructor_template(pingt);
}
Local<FunctionTemplate> setting = FunctionTemplate::New(env->isolate());
setting->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<ObjectTemplate> settingt = setting->InstanceTemplate();
settingt->SetInternalFieldCount(Http2Settings::kInternalFieldCount);
env->set_http2settings_constructor_template(settingt);
if (env->http2settings_constructor_template().IsEmpty()) {
Local<FunctionTemplate> setting = FunctionTemplate::New(env->isolate());
setting->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<ObjectTemplate> settingt = setting->InstanceTemplate();
settingt->SetInternalFieldCount(Http2Settings::kInternalFieldCount);
env->set_http2settings_constructor_template(settingt);
}
Local<FunctionTemplate> 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<ObjectTemplate> streamt = stream->InstanceTemplate();
streamt->SetInternalFieldCount(Http2Stream::kInternalFieldCount);
env->set_http2stream_constructor_template(streamt);
SetConstructorFunction(context, target, "Http2Stream", stream);
Local<FunctionTemplate> 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<FunctionTemplate> session =
NewFunctionTemplate(isolate, Http2Session::New);
+7 -3
View File
@@ -5372,9 +5372,13 @@ static void Initialize(Local<Object> 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();
+16 -12
View File
@@ -71,24 +71,28 @@ void PipeWrap::Initialize(Local<Object> target,
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
t->InstanceTemplate()->SetInternalFieldCount(PipeWrap::kInternalFieldCount);
Local<FunctionTemplate> 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);
+4
View File
@@ -48,6 +48,7 @@ int StreamBase::Shutdown(v8::Local<v8::Object> 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<Value>& 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<Value>& 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;
+46 -39
View File
@@ -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<Object> target,
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();
Local<FunctionTemplate> sw =
NewFunctionTemplate(isolate, IsConstructCallCallback);
sw->InstanceTemplate()->SetInternalFieldCount(
ShutdownWrap::kInternalFieldCount);
Local<FunctionTemplate> 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<FunctionTemplate> 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<FunctionTemplate> ww = env->write_wrap_template();
if (ww.IsEmpty()) {
ww = FunctionTemplate::New(isolate, IsConstructCallCallback);
Local<ObjectTemplate> 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);
+36 -30
View File
@@ -88,43 +88,49 @@ void TCPWrap::Initialize(Local<Object> target,
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
t->InstanceTemplate()->SetInternalFieldCount(TCPWrap::kInternalFieldCount);
Local<FunctionTemplate> 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<TCPWrap, uv_tcp_getsockname>);
SetProtoMethod(isolate,
t,
"getpeername",
GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>);
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<TCPWrap, uv_tcp_getsockname>);
SetProtoMethod(isolate,
t,
"getpeername",
GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>);
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<FunctionTemplate> cwt = AsyncWrap::MakeLazilyInitializedJSTemplate(env);
+13 -10
View File
@@ -58,14 +58,18 @@ void TTYWrap::Initialize(Local<Object> target,
Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
t->SetClassName(ttyString);
t->InstanceTemplate()->SetInternalFieldCount(TTYWrap::kInternalFieldCount);
t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
Local<FunctionTemplate> 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<Object> target,
NODE_DEFINE_CONSTANT(target, UV_TTY_MODE_RAW_VT);
Local<Value> 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();
}
}
+20
View File
@@ -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<v8::Context> context = v8::Context::New(isolate_);