From 2ed3fa187e6fad84e52075c72250e6894291ba2d Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 31 Dec 2018 23:18:08 +0800 Subject: [PATCH] src: declare process-related C++ methods in node_process.h Instead of in node_internals.h. Also move process property accessors that are not reused into node_process_object.cc and make them static. PR-URL: https://github.com/nodejs/node/pull/25397 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- node.gyp | 1 + src/env.cc | 1 + src/inspector_agent.cc | 1 + src/node.cc | 9 +++--- src/node_crypto.cc | 9 +++--- src/node_env_var.cc | 3 +- src/node_file.cc | 4 ++- src/node_internals.h | 31 --------------------- src/node_messaging.cc | 11 ++++---- src/node_process.h | 32 +++++++++++++++++++++ src/node_process_methods.cc | 52 ++++------------------------------- src/node_process_object.cc | 55 +++++++++++++++++++++++++++++++++++-- src/signal_wrap.cc | 1 + src/uv.cc | 3 +- 14 files changed, 116 insertions(+), 97 deletions(-) create mode 100644 src/node_process.h diff --git a/node.gyp b/node.gyp index c23b2c504ec624..e0e1449bfa680f 100644 --- a/node.gyp +++ b/node.gyp @@ -450,6 +450,7 @@ 'src/node_perf_common.h', 'src/node_persistent.h', 'src/node_platform.h', + 'src/node_process.h', 'src/node_revert.h', 'src/node_root_certs.h', 'src/node_stat_watcher.h', diff --git a/src/env.cc b/src/env.cc index 0ebcc53c213944..2cbbd14e713b9b 100644 --- a/src/env.cc +++ b/src/env.cc @@ -7,6 +7,7 @@ #include "node_native_module.h" #include "node_options-inl.h" #include "node_platform.h" +#include "node_process.h" #include "node_worker.h" #include "tracing/agent.h" #include "tracing/traced_value.h" diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index 8710b8569072a1..d9a252bbe88140 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -9,6 +9,7 @@ #include "node/inspector/protocol/Protocol.h" #include "node_errors.h" #include "node_internals.h" +#include "node_process.h" #include "node_url.h" #include "v8-inspector.h" #include "v8-platform.h" diff --git a/src/node.cc b/src/node.cc index 375ee7fd6edbeb..8e5dbd1516235d 100644 --- a/src/node.cc +++ b/src/node.cc @@ -30,6 +30,7 @@ #include "node_options-inl.h" #include "node_perf.h" #include "node_platform.h" +#include "node_process.h" #include "node_revert.h" #include "node_version.h" #include "tracing/traced_value.h" @@ -586,9 +587,9 @@ void Exit(const FunctionCallbackInfo& args) { } static Maybe ProcessEmitWarningGeneric(Environment* env, - const char* warning, - const char* type = nullptr, - const char* code = nullptr) { + const char* warning, + const char* type = nullptr, + const char* code = nullptr) { HandleScope handle_scope(env->isolate()); Context::Scope context_scope(env->context()); @@ -746,12 +747,10 @@ void RunBootstrapping(Environment* env) { // Setting global properties for the bootstrappers to use: // - global - // - process._rawDebug // Expose the global object as a property on itself // (Allows you to set stuff on `global` from anywhere in JavaScript.) global->Set(context, FIXED_ONE_BYTE_STRING(env->isolate(), "global"), global) .FromJust(); - env->SetMethod(process, "_rawDebug", RawDebug); // Create binding loaders std::vector> loaders_params = { diff --git a/src/node_crypto.cc b/src/node_crypto.cc index cc4431b8138810..71eb3880100ea0 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -19,16 +19,17 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. +#include "node_crypto.h" #include "node.h" #include "node_buffer.h" -#include "node_errors.h" #include "node_constants.h" -#include "node_crypto.h" #include "node_crypto_bio.h" -#include "node_crypto_groups.h" #include "node_crypto_clienthello-inl.h" -#include "node_mutex.h" +#include "node_crypto_groups.h" +#include "node_errors.h" #include "node_internals.h" +#include "node_mutex.h" +#include "node_process.h" #include "tls_wrap.h" // TLSWrap #include "async_wrap-inl.h" diff --git a/src/node_env_var.cc b/src/node_env_var.cc index 8cbfa22973c65d..d1ebb064e2417e 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -1,5 +1,6 @@ -#include "node_internals.h" #include "node_errors.h" +#include "node_internals.h" +#include "node_process.h" #ifdef __APPLE__ #include diff --git a/src/node_file.cc b/src/node_file.cc index b66c0fe6d20915..8757f6bf0d8921 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -19,11 +19,13 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. +#include "node_file.h" #include "aliased_buffer.h" #include "node_buffer.h" #include "node_internals.h" +#include "node_process.h" #include "node_stat_watcher.h" -#include "node_file.h" + #include "tracing/trace_event.h" #include "req_wrap-inl.h" diff --git a/src/node_internals.h b/src/node_internals.h index afdffe10de4d9c..c7f98245443490 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -123,10 +123,6 @@ void RegisterSignalHandler(int signal, bool reset_handler = false); #endif -v8::Local CreateEnvVarProxy(v8::Local context, - v8::Isolate* isolate, - v8::Local data); - std::string GetHumanReadableProcessName(); void GetHumanReadableProcessName(char (*name)[1024]); @@ -180,16 +176,6 @@ namespace task_queue { void PromiseRejectCallback(v8::PromiseRejectMessage message); } // namespace task_queue -v8::Maybe ProcessEmitWarning(Environment* env, const char* fmt, ...); -v8::Maybe ProcessEmitDeprecationWarning(Environment* env, - const char* warning, - const char* deprecation_code); - -v8::Local CreateProcessObject( - Environment* env, - const std::vector& args, - const std::vector& exec_args); - enum Endianness { kLittleEndian, // _Not_ LITTLE_ENDIAN, clashes with endian.h. kBigEndian @@ -371,23 +357,6 @@ void DisposePlatform(); // Functions defined in node.cc that are exposed via the bootstrapper object -void RawDebug(const v8::FunctionCallbackInfo& args); - -void DebugPortGetter(v8::Local property, - const v8::PropertyCallbackInfo& info); -void DebugPortSetter(v8::Local property, - v8::Local value, - const v8::PropertyCallbackInfo& info); - -void GetParentProcessId(v8::Local property, - const v8::PropertyCallbackInfo& info); - -void ProcessTitleGetter(v8::Local property, - const v8::PropertyCallbackInfo& info); -void ProcessTitleSetter(v8::Local property, - v8::Local value, - const v8::PropertyCallbackInfo& info); - #if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__) #define NODE_IMPLEMENTS_POSIX_CREDENTIALS 1 #endif // __POSIX__ && !defined(__ANDROID__) && !defined(__CloudABI__) diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 6036648dd4c527..a538839bcc6fcb 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -1,12 +1,13 @@ -#include "debug_utils.h" #include "node_messaging.h" -#include "node_internals.h" +#include "async_wrap-inl.h" +#include "async_wrap.h" +#include "debug_utils.h" #include "node_buffer.h" #include "node_errors.h" -#include "util.h" +#include "node_internals.h" +#include "node_process.h" #include "util-inl.h" -#include "async_wrap.h" -#include "async_wrap-inl.h" +#include "util.h" using v8::Array; using v8::ArrayBuffer; diff --git a/src/node_process.h b/src/node_process.h new file mode 100644 index 00000000000000..35667ca1b9de40 --- /dev/null +++ b/src/node_process.h @@ -0,0 +1,32 @@ +#ifndef SRC_NODE_PROCESS_H_ +#define SRC_NODE_PROCESS_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include "v8.h" + +namespace node { + +v8::Local CreateEnvVarProxy(v8::Local context, + v8::Isolate* isolate, + v8::Local data); + +// Most of the time, it's best to use `console.error` to write +// to the process.stderr stream. However, in some cases, such as +// when debugging the stream.Writable class or the process.nextTick +// function, it is useful to bypass JavaScript entirely. +void RawDebug(const v8::FunctionCallbackInfo& args); + +v8::Maybe ProcessEmitWarning(Environment* env, const char* fmt, ...); +v8::Maybe ProcessEmitDeprecationWarning(Environment* env, + const char* warning, + const char* deprecation_code); + +v8::Local CreateProcessObject( + Environment* env, + const std::vector& args, + const std::vector& exec_args); + +} // namespace node +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#endif // SRC_NODE_PROCESS_H_ diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 69f08a219b67e6..c9abe19b7e0b68 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -1,9 +1,10 @@ -#include "node.h" -#include "node_internals.h" -#include "node_errors.h" -#include "base_object.h" #include "base_object-inl.h" +#include "base_object.h" #include "env-inl.h" +#include "node.h" +#include "node_errors.h" +#include "node_internals.h" +#include "node_process.h" #include "util-inl.h" #include "uv.h" #include "v8.h" @@ -44,7 +45,6 @@ using v8::Local; using v8::Name; using v8::NewStringType; using v8::Object; -using v8::PropertyCallbackInfo; using v8::String; using v8::Uint32; using v8::Uint32Array; @@ -197,10 +197,6 @@ static void MemoryUsage(const FunctionCallbackInfo& args) { fields[3] = v8_heap_stats.external_memory(); } -// Most of the time, it's best to use `console.error` to write -// to the process.stderr stream. However, in some cases, such as -// when debugging the stream.Writable class or the process.nextTick -// function, it is useful to bypass JavaScript entirely. void RawDebug(const FunctionCallbackInfo& args) { CHECK(args.Length() == 1 && args[0]->IsString() && "must be called with a single string"); @@ -246,28 +242,6 @@ static void Uptime(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(uptime / 1000); } -void ProcessTitleGetter(Local property, - const PropertyCallbackInfo& info) { - char buffer[512]; - uv_get_process_title(buffer, sizeof(buffer)); - info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buffer, - NewStringType::kNormal).ToLocalChecked()); -} - -void ProcessTitleSetter(Local property, - Local value, - const PropertyCallbackInfo& info) { - node::Utf8Value title(info.GetIsolate(), value); - TRACE_EVENT_METADATA1("__metadata", "process_name", "name", - TRACE_STR_COPY(*title)); - uv_set_process_title(*title); -} - -void GetParentProcessId(Local property, - const PropertyCallbackInfo& info) { - info.GetReturnValue().Set(uv_os_getppid()); -} - static void GetActiveRequests(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); @@ -297,22 +271,6 @@ void GetActiveHandles(const FunctionCallbackInfo& args) { Array::New(env->isolate(), handle_v.data(), handle_v.size())); } -void DebugPortGetter(Local property, - const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - int port = env->inspector_host_port()->port(); - info.GetReturnValue().Set(port); -} - - -void DebugPortSetter(Local property, - Local value, - const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - int32_t port = value->Int32Value(env->context()).FromMaybe(0); - env->inspector_host_port()->set_port(static_cast(port)); -} - #ifdef __POSIX__ static void DebugProcess(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); diff --git a/src/node_process_object.cc b/src/node_process_object.cc index 07f28653deeb38..980b5002b69b0b 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -4,6 +4,7 @@ #include "node_internals.h" #include "node_options-inl.h" #include "node_metadata.h" +#include "node_process.h" #include "node_revert.h" #include "util-inl.h" @@ -18,13 +19,53 @@ using v8::Integer; using v8::Isolate; using v8::Just; using v8::Local; +using v8::Name; using v8::NewStringType; using v8::None; using v8::Object; +using v8::PropertyCallbackInfo; using v8::SideEffectType; using v8::String; using v8::Value; +static void ProcessTitleGetter(Local property, + const PropertyCallbackInfo& info) { + char buffer[512]; + uv_get_process_title(buffer, sizeof(buffer)); + info.GetReturnValue().Set( + String::NewFromUtf8(info.GetIsolate(), buffer, NewStringType::kNormal) + .ToLocalChecked()); +} + +static void ProcessTitleSetter(Local property, + Local value, + const PropertyCallbackInfo& info) { + node::Utf8Value title(info.GetIsolate(), value); + TRACE_EVENT_METADATA1( + "__metadata", "process_name", "name", TRACE_STR_COPY(*title)); + uv_set_process_title(*title); +} + +static void DebugPortGetter(Local property, + const PropertyCallbackInfo& info) { + Environment* env = Environment::GetCurrent(info); + int port = env->inspector_host_port()->port(); + info.GetReturnValue().Set(port); +} + +static void DebugPortSetter(Local property, + Local value, + const PropertyCallbackInfo& info) { + Environment* env = Environment::GetCurrent(info); + int32_t port = value->Int32Value(env->context()).FromMaybe(0); + env->inspector_host_port()->set_port(static_cast(port)); +} + +static void GetParentProcessId(Local property, + const PropertyCallbackInfo& info) { + info.GetReturnValue().Set(uv_os_getppid()); +} + Local CreateProcessObject(Environment* env, const std::vector& args, const std::vector& exec_args) { @@ -39,6 +80,7 @@ Local CreateProcessObject(Environment* env, ->NewInstance(context) .ToLocalChecked(); + // process.title auto title_string = FIXED_ONE_BYTE_STRING(env->isolate(), "title"); CHECK(process->SetAccessor( env->context(), @@ -103,7 +145,7 @@ Local CreateProcessObject(Environment* env, ToV8Value(env->context(), exec_args) .ToLocalChecked()).FromJust(); - // create process.env + // process.env process ->Set(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "env"), @@ -117,6 +159,9 @@ Local CreateProcessObject(Environment* env, FIXED_ONE_BYTE_STRING(env->isolate(), "ppid"), GetParentProcessId).FromJust()); + // TODO(joyeecheung): the following process properties that are set using + // parsed CLI flags should be migrated to `internal/options` in JS land. + // -e, --eval // TODO(addaleax): Remove this. if (env->options()->has_eval_string) { @@ -193,13 +238,13 @@ Local CreateProcessObject(Environment* env, READONLY_PROPERTY(process, "traceDeprecation", True(env->isolate())); } - // TODO(refack): move the following 4 to `node_config` // --inspect-brk if (env->options()->debug_options().wait_for_connect()) { READONLY_DONT_ENUM_PROPERTY(process, "_breakFirstLine", True(env->isolate())); } + // --inspect-brk-node if (env->options()->debug_options().break_node_first_line) { READONLY_DONT_ENUM_PROPERTY(process, "_breakNodeFirstLine", True(env->isolate())); @@ -227,6 +272,7 @@ Local CreateProcessObject(Environment* env, SECURITY_REVERSIONS(V) #undef V + // process.execPath { size_t exec_path_len = 2 * PATH_MAX; std::vector exec_path(exec_path_len); @@ -245,6 +291,7 @@ Local CreateProcessObject(Environment* env, exec_path_value).FromJust(); } + // process.debugPort auto debug_port_string = FIXED_ONE_BYTE_STRING(env->isolate(), "debugPort"); CHECK(process->SetAccessor(env->context(), debug_port_string, @@ -252,6 +299,10 @@ Local CreateProcessObject(Environment* env, env->is_main_thread() ? DebugPortSetter : nullptr, env->as_external()).FromJust()); + // process._rawDebug: may be overwritten later in JS land, but should be + // availbale from the begining for debugging purposes + env->SetMethod(process, "_rawDebug", RawDebug); + return scope.Escape(process); } diff --git a/src/signal_wrap.cc b/src/signal_wrap.cc index 93f86f5d790aa7..ca08342ec63bb1 100644 --- a/src/signal_wrap.cc +++ b/src/signal_wrap.cc @@ -22,6 +22,7 @@ #include "async_wrap-inl.h" #include "env-inl.h" #include "handle_wrap.h" +#include "node_process.h" #include "util-inl.h" #include "v8.h" diff --git a/src/uv.cc b/src/uv.cc index a7d0b1012ce2f4..5422b0d2403d39 100644 --- a/src/uv.cc +++ b/src/uv.cc @@ -20,9 +20,10 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. #include "uv.h" +#include "env-inl.h" #include "node.h" #include "node_internals.h" -#include "env-inl.h" +#include "node_process.h" namespace node {