Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: register external references of more modules #39961

Closed
wants to merge 10 commits into from
16 changes: 9 additions & 7 deletions src/base_object-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,17 @@ bool BaseObject::IsWeakOrDetached() const {
return pd->wants_weak_jsobj || pd->is_detached;
}

void BaseObject::LazilyInitializedJSTemplateConstructor(
const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK(args.IsConstructCall());
DCHECK_GT(args.This()->InternalFieldCount(), 0);
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
}

v8::Local<v8::FunctionTemplate>
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK(args.IsConstructCall());
DCHECK_GT(args.This()->InternalFieldCount(), 0);
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
};

v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
v8::Local<v8::FunctionTemplate> t =
env->NewFunctionTemplate(LazilyInitializedJSTemplateConstructor);
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(
BaseObject::kInternalFieldCount);
Expand Down
2 changes: 2 additions & 0 deletions src/base_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class BaseObject : public MemoryRetainer {
// was also passed to the `BaseObject()` constructor initially.
// This may return `nullptr` if the C++ object has not been constructed yet,
// e.g. when the JS object used `MakeLazilyInitializedJSTemplate`.
static inline void LazilyInitializedJSTemplateConstructor(
const v8::FunctionCallbackInfo<v8::Value>& args);
static inline BaseObject* FromJSObject(v8::Local<v8::Value> object);
template <typename T>
static inline T* FromJSObject(v8::Local<v8::Value> object);
Expand Down
31 changes: 22 additions & 9 deletions src/node_dtrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include "env-inl.h"
#include "node_errors.h"
#include "node_external_reference.h"

#include <cstring>

Expand Down Expand Up @@ -288,23 +289,35 @@ void InitDTrace(Environment* env) {
}, env);
}

#define NODE_PROBES(V) \
V(DTRACE_NET_SERVER_CONNECTION) \
V(DTRACE_NET_STREAM_END) \
V(DTRACE_HTTP_SERVER_REQUEST) \
V(DTRACE_HTTP_SERVER_RESPONSE) \
V(DTRACE_HTTP_CLIENT_REQUEST) \
V(DTRACE_HTTP_CLIENT_RESPONSE)

void InitializeDTrace(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);

#if defined HAVE_DTRACE || defined HAVE_ETW
# define NODE_PROBE(name) env->SetMethod(target, #name, name);
NODE_PROBE(DTRACE_NET_SERVER_CONNECTION)
NODE_PROBE(DTRACE_NET_STREAM_END)
NODE_PROBE(DTRACE_HTTP_SERVER_REQUEST)
NODE_PROBE(DTRACE_HTTP_SERVER_RESPONSE)
NODE_PROBE(DTRACE_HTTP_CLIENT_REQUEST)
NODE_PROBE(DTRACE_HTTP_CLIENT_RESPONSE)
# undef NODE_PROBE
#endif
#define V(name) env->SetMethod(target, #name, name);
NODE_PROBES(V)
#undef V
#endif // defined HAVE_DTRACE || defined HAVE_ETW
}

void RegisterDtraceExternalReferences(ExternalReferenceRegistry* registry) {
#if defined HAVE_DTRACE || defined HAVE_ETW
#define V(name) registry->Register(name);
NODE_PROBES(V)
#undef V
#endif // defined HAVE_DTRACE || defined HAVE_ETW
}

} // namespace node
NODE_MODULE_CONTEXT_AWARE_INTERNAL(dtrace, node::InitializeDTrace)
NODE_MODULE_EXTERNAL_REFERENCE(dtrace, node::RegisterDtraceExternalReferences)
3 changes: 3 additions & 0 deletions src/node_external_reference.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "node_external_reference.h"
#include <cinttypes>
#include <vector>
#include "base_object-inl.h"
#include "util.h"

namespace node {
Expand All @@ -13,6 +14,8 @@ const std::vector<intptr_t>& ExternalReferenceRegistry::external_references() {
}

ExternalReferenceRegistry::ExternalReferenceRegistry() {
this->Register(BaseObject::LazilyInitializedJSTemplateConstructor);

#define V(modname) _register_external_reference_##modname(this);
EXTERNAL_REFERENCE_BINDING_LIST(V)
#undef V
Expand Down
14 changes: 13 additions & 1 deletion src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ class ExternalReferenceRegistry {
V(performance) \
V(process_methods) \
V(process_object) \
V(report) \
V(task_queue) \
V(tcp_wrap) \
V(tty_wrap) \
V(url) \
V(util) \
V(pipe_wrap) \
V(serdes) \
V(string_decoder) \
V(stream_wrap) \
V(signal_wrap) \
V(trace_events) \
V(timers) \
V(types) \
Expand All @@ -92,10 +97,17 @@ class ExternalReferenceRegistry {
#define EXTERNAL_REFERENCE_BINDING_LIST_INSPECTOR(V)
#endif // HAVE_INSPECTOR

#if HAVE_DTRACE || HAVE_ETW
#define EXTERNAL_REFERENCE_BINDING_LIST_DTRACE(V) V(dtrace)
#else
#define EXTERNAL_REFERENCE_BINDING_LIST_DTRACE(V)
#endif

#define EXTERNAL_REFERENCE_BINDING_LIST(V) \
EXTERNAL_REFERENCE_BINDING_LIST_BASE(V) \
EXTERNAL_REFERENCE_BINDING_LIST_INSPECTOR(V) \
EXTERNAL_REFERENCE_BINDING_LIST_I18N(V)
EXTERNAL_REFERENCE_BINDING_LIST_I18N(V) \
EXTERNAL_REFERENCE_BINDING_LIST_DTRACE(V)

} // namespace node

Expand Down
5 changes: 5 additions & 0 deletions src/node_process_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ void PatchProcessObject(const FunctionCallbackInfo<Value>& args) {

void RegisterProcessExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(RawDebug);
registry->Register(GetParentProcessId);
registry->Register(DebugPortSetter);
registry->Register(DebugPortGetter);
registry->Register(ProcessTitleSetter);
registry->Register(ProcessTitleGetter);
}

} // namespace node
Expand Down
21 changes: 21 additions & 0 deletions src/node_report_module.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "env.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "node_options.h"
#include "node_report.h"
Expand Down Expand Up @@ -196,6 +197,26 @@ static void Initialize(Local<Object> exports,
SetReportOnUncaughtException);
}

void RegisterExternalReferences(node::ExternalReferenceRegistry* registry) {
registry->Register(WriteReport);
registry->Register(GetReport);
registry->Register(GetCompact);
registry->Register(SetCompact);
registry->Register(GetDirectory);
registry->Register(SetDirectory);
registry->Register(GetFilename);
registry->Register(SetFilename);
registry->Register(GetSignal);
registry->Register(SetSignal);
registry->Register(ShouldReportOnFatalError);
registry->Register(SetReportOnFatalError);
registry->Register(ShouldReportOnSignal);
registry->Register(SetReportOnSignal);
registry->Register(ShouldReportOnUncaughtException);
registry->Register(SetReportOnUncaughtException);
}

} // namespace report

NODE_MODULE_CONTEXT_AWARE_INTERNAL(report, report::Initialize)
NODE_MODULE_EXTERNAL_REFERENCE(report, report::RegisterExternalReferences)
16 changes: 15 additions & 1 deletion src/pipe_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
#include "pipe_wrap.h"

#include "async_wrap.h"
#include "connect_wrap.h"
#include "connection_wrap.h"
#include "env-inl.h"
#include "handle_wrap.h"
#include "node.h"
#include "node_buffer.h"
#include "connect_wrap.h"
#include "node_external_reference.h"
#include "stream_base-inl.h"
#include "stream_wrap.h"
#include "util-inl.h"
Expand Down Expand Up @@ -104,6 +105,17 @@ void PipeWrap::Initialize(Local<Object> target,
constants).Check();
}

void PipeWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(Bind);
registry->Register(Listen);
registry->Register(Connect);
registry->Register(Open);
#ifdef _WIN32
registry->Register(SetPendingInstances);
#endif
registry->Register(Fchmod);
}

void PipeWrap::New(const FunctionCallbackInfo<Value>& args) {
// This constructor should not be exposed to public javascript.
Expand Down Expand Up @@ -236,3 +248,5 @@ void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
} // namespace node

NODE_MODULE_CONTEXT_AWARE_INTERNAL(pipe_wrap, node::PipeWrap::Initialize)
NODE_MODULE_EXTERNAL_REFERENCE(pipe_wrap,
node::PipeWrap::RegisterExternalReferences)
2 changes: 2 additions & 0 deletions src/pipe_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace node {

class ExternalReferenceRegistry;
class Environment;

class PipeWrap : public ConnectionWrap<PipeWrap, uv_pipe_t> {
Expand All @@ -47,6 +48,7 @@ class PipeWrap : public ConnectionWrap<PipeWrap, uv_pipe_t> {
v8::Local<v8::Context> context,
void* priv);

static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
SET_NO_MEMORY_INFO()
SET_MEMORY_INFO_NAME(PipeWrap)
SET_SELF_SIZE(PipeWrap)
Expand Down
9 changes: 9 additions & 0 deletions src/signal_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "async_wrap-inl.h"
#include "env-inl.h"
#include "handle_wrap.h"
#include "node_external_reference.h"
#include "node_process-inl.h"
#include "util-inl.h"
#include "v8.h"
Expand Down Expand Up @@ -62,6 +63,12 @@ class SignalWrap : public HandleWrap {
env->SetConstructorFunction(target, "Signal", constructor);
}

static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(Start);
registry->Register(Stop);
}

SET_NO_MEMORY_INFO()
SET_MEMORY_INFO_NAME(SignalWrap)
SET_SELF_SIZE(SignalWrap)
Expand Down Expand Up @@ -167,3 +174,5 @@ bool HasSignalJSHandler(int signum) {


NODE_MODULE_CONTEXT_AWARE_INTERNAL(signal_wrap, node::SignalWrap::Initialize)
NODE_MODULE_EXTERNAL_REFERENCE(signal_wrap,
node::SignalWrap::RegisterExternalReferences)
4 changes: 4 additions & 0 deletions src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ void LibuvStreamWrap::Initialize(Local<Object> target,
void LibuvStreamWrap::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(IsConstructCallCallback);
registry->Register(GetWriteQueueSize);
registry->Register(SetBlocking);
// TODO(joyee): StreamBase::RegisterExternalReferences() is called somewhere
// else but we may want to do it here too and guard it with a static flag.
}

LibuvStreamWrap::LibuvStreamWrap(Environment* env,
Expand Down
22 changes: 21 additions & 1 deletion src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@

#include "tcp_wrap.h"

#include "connect_wrap.h"
#include "connection_wrap.h"
#include "env-inl.h"
#include "handle_wrap.h"
#include "node_buffer.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "connect_wrap.h"
#include "stream_base-inl.h"
#include "stream_wrap.h"
#include "util-inl.h"
Expand Down Expand Up @@ -120,6 +121,23 @@ void TCPWrap::Initialize(Local<Object> target,
constants).Check();
}

void TCPWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(Open);
registry->Register(Bind);
registry->Register(Listen);
registry->Register(Connect);
registry->Register(Bind6);
registry->Register(Connect6);

registry->Register(GetSockOrPeerName<TCPWrap, uv_tcp_getsockname>);
registry->Register(GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>);
registry->Register(SetNoDelay);
registry->Register(SetKeepAlive);
#ifdef _WIN32
registry->Register(SetSimultaneousAccepts);
#endif
}

void TCPWrap::New(const FunctionCallbackInfo<Value>& args) {
// This constructor should not be exposed to public javascript.
Expand Down Expand Up @@ -393,3 +411,5 @@ Local<Object> AddressToJS(Environment* env,
} // namespace node

NODE_MODULE_CONTEXT_AWARE_INTERNAL(tcp_wrap, node::TCPWrap::Initialize)
NODE_MODULE_EXTERNAL_REFERENCE(tcp_wrap,
node::TCPWrap::RegisterExternalReferences)
2 changes: 2 additions & 0 deletions src/tcp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace node {

class ExternalReferenceRegistry;
class Environment;

class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
Expand All @@ -45,6 +46,7 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

SET_NO_MEMORY_INFO()
SET_SELF_SIZE(TCPWrap)
Expand Down
10 changes: 10 additions & 0 deletions src/tty_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "env-inl.h"
#include "handle_wrap.h"
#include "node_buffer.h"
#include "node_external_reference.h"
#include "stream_base-inl.h"
#include "stream_wrap.h"
#include "util-inl.h"
Expand All @@ -40,6 +41,13 @@ using v8::Object;
using v8::String;
using v8::Value;

void TTYWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(GetWindowSize);
registry->Register(SetRawMode);
registry->Register(IsTTY);
}

void TTYWrap::Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
Expand Down Expand Up @@ -146,3 +154,5 @@ TTYWrap::TTYWrap(Environment* env,
} // namespace node

NODE_MODULE_CONTEXT_AWARE_INTERNAL(tty_wrap, node::TTYWrap::Initialize)
NODE_MODULE_EXTERNAL_REFERENCE(tty_wrap,
node::TTYWrap::RegisterExternalReferences)
2 changes: 2 additions & 0 deletions src/tty_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
namespace node {

class Environment;
class ExternalReferenceRegistry;

class TTYWrap : public LibuvStreamWrap {
public:
static void Initialize(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

SET_NO_MEMORY_INFO()
SET_MEMORY_INFO_NAME(TTYWrap)
Expand Down