Skip to content

Commit 49ac968

Browse files
joyeecheungBridgeAR
authored andcommitted
src: move process object creation into node_process_object.cc
Changes `SetupProcessObject` to `CreateProessObject` which creates the process object from scratch and return it to `Environment::Start` to be stored in the Environment object. PR-URL: #25397 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 299aefd commit 49ac968

6 files changed

+265
-240
lines changed

node.gyp

+2-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@
371371
'src/node_perf.cc',
372372
'src/node_platform.cc',
373373
'src/node_postmortem_metadata.cc',
374-
'src/node_process.cc',
374+
'src/node_process_methods.cc',
375+
'src/node_process_object.cc',
375376
'src/node_serdes.cc',
376377
'src/node_stat_watcher.cc',
377378
'src/node_symbols.cc',

src/env.cc

+1-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ using v8::Context;
2222
using v8::EmbedderGraph;
2323
using v8::External;
2424
using v8::Function;
25-
using v8::FunctionTemplate;
2625
using v8::HandleScope;
2726
using v8::Integer;
2827
using v8::Isolate;
@@ -339,17 +338,9 @@ void Environment::Start(const std::vector<std::string>& args,
339338
StartProfilerIdleNotifier();
340339
}
341340

342-
auto process_template = FunctionTemplate::New(isolate());
343-
process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "process"));
344-
345-
auto process_object = process_template->GetFunction(context())
346-
.ToLocalChecked()
347-
->NewInstance(context())
348-
.ToLocalChecked();
341+
Local<Object> process_object = CreateProcessObject(this, args, exec_args);
349342
set_process_object(process_object);
350343

351-
SetupProcessObject(this, args, exec_args);
352-
353344
static uv_once_t init_once = UV_ONCE_INIT;
354345
uv_once(&init_once, InitThreadLocalOnce);
355346
uv_key_set(&thread_local_env, this);

src/node.cc

-226
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,12 @@ using v8::MaybeLocal;
124124
using v8::Message;
125125
using v8::MicrotasksPolicy;
126126
using v8::NewStringType;
127-
using v8::None;
128127
using v8::Nothing;
129128
using v8::Object;
130129
using v8::ObjectTemplate;
131130
using v8::Script;
132131
using v8::ScriptOrigin;
133132
using v8::SealHandleScope;
134-
using v8::SideEffectType;
135133
using v8::String;
136134
using v8::TracingController;
137135
using v8::Undefined;
@@ -690,230 +688,6 @@ static void OnMessage(Local<Message> message, Local<Value> error) {
690688
}
691689
}
692690

693-
void SetupProcessObject(Environment* env,
694-
const std::vector<std::string>& args,
695-
const std::vector<std::string>& exec_args) {
696-
Isolate* isolate = env->isolate();
697-
HandleScope scope(isolate);
698-
Local<Context> context = env->context();
699-
700-
Local<Object> process = env->process_object();
701-
702-
auto title_string = FIXED_ONE_BYTE_STRING(env->isolate(), "title");
703-
CHECK(process->SetAccessor(
704-
env->context(),
705-
title_string,
706-
ProcessTitleGetter,
707-
env->is_main_thread() ? ProcessTitleSetter : nullptr,
708-
env->as_external(),
709-
DEFAULT,
710-
None,
711-
SideEffectType::kHasNoSideEffect).FromJust());
712-
713-
// process.version
714-
READONLY_PROPERTY(process,
715-
"version",
716-
FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));
717-
718-
// process.versions
719-
Local<Object> versions = Object::New(env->isolate());
720-
READONLY_PROPERTY(process, "versions", versions);
721-
722-
#define V(key) \
723-
if (!per_process::metadata.versions.key.empty()) { \
724-
READONLY_STRING_PROPERTY( \
725-
versions, #key, per_process::metadata.versions.key); \
726-
}
727-
NODE_VERSIONS_KEYS(V)
728-
#undef V
729-
730-
// process.arch
731-
READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);
732-
733-
// process.platform
734-
READONLY_STRING_PROPERTY(process, "platform", per_process::metadata.platform);
735-
736-
// process.release
737-
Local<Object> release = Object::New(env->isolate());
738-
READONLY_PROPERTY(process, "release", release);
739-
READONLY_STRING_PROPERTY(release, "name", per_process::metadata.release.name);
740-
#if NODE_VERSION_IS_LTS
741-
READONLY_STRING_PROPERTY(release, "lts", per_process::metadata.release.lts);
742-
#endif // NODE_VERSION_IS_LTS
743-
744-
#ifdef NODE_HAS_RELEASE_URLS
745-
READONLY_STRING_PROPERTY(
746-
release, "sourceUrl", per_process::metadata.release.source_url);
747-
READONLY_STRING_PROPERTY(
748-
release, "headersUrl", per_process::metadata.release.headers_url);
749-
#ifdef _WIN32
750-
READONLY_STRING_PROPERTY(
751-
release, "libUrl", per_process::metadata.release.lib_url);
752-
#endif // _WIN32
753-
#endif // NODE_HAS_RELEASE_URLS
754-
755-
// process.argv
756-
process->Set(env->context(),
757-
FIXED_ONE_BYTE_STRING(env->isolate(), "argv"),
758-
ToV8Value(env->context(), args).ToLocalChecked()).FromJust();
759-
760-
// process.execArgv
761-
process->Set(env->context(),
762-
FIXED_ONE_BYTE_STRING(env->isolate(), "execArgv"),
763-
ToV8Value(env->context(), exec_args)
764-
.ToLocalChecked()).FromJust();
765-
766-
// create process.env
767-
process
768-
->Set(env->context(),
769-
FIXED_ONE_BYTE_STRING(env->isolate(), "env"),
770-
CreateEnvVarProxy(context, isolate, env->as_external()))
771-
.FromJust();
772-
773-
READONLY_PROPERTY(process, "pid",
774-
Integer::New(env->isolate(), uv_os_getpid()));
775-
776-
CHECK(process->SetAccessor(env->context(),
777-
FIXED_ONE_BYTE_STRING(env->isolate(), "ppid"),
778-
GetParentProcessId).FromJust());
779-
780-
// -e, --eval
781-
// TODO(addaleax): Remove this.
782-
if (env->options()->has_eval_string) {
783-
READONLY_PROPERTY(process,
784-
"_eval",
785-
String::NewFromUtf8(
786-
env->isolate(),
787-
env->options()->eval_string.c_str(),
788-
NewStringType::kNormal).ToLocalChecked());
789-
}
790-
791-
// -p, --print
792-
// TODO(addaleax): Remove this.
793-
if (env->options()->print_eval) {
794-
READONLY_PROPERTY(process, "_print_eval", True(env->isolate()));
795-
}
796-
797-
// -c, --check
798-
// TODO(addaleax): Remove this.
799-
if (env->options()->syntax_check_only) {
800-
READONLY_PROPERTY(process, "_syntax_check_only", True(env->isolate()));
801-
}
802-
803-
// -i, --interactive
804-
// TODO(addaleax): Remove this.
805-
if (env->options()->force_repl) {
806-
READONLY_PROPERTY(process, "_forceRepl", True(env->isolate()));
807-
}
808-
809-
// -r, --require
810-
// TODO(addaleax): Remove this.
811-
const std::vector<std::string>& preload_modules =
812-
env->options()->preload_modules;
813-
if (!preload_modules.empty()) {
814-
READONLY_PROPERTY(process,
815-
"_preload_modules",
816-
ToV8Value(env->context(), preload_modules)
817-
.ToLocalChecked());
818-
}
819-
820-
// --no-deprecation
821-
if (env->options()->no_deprecation) {
822-
READONLY_PROPERTY(process, "noDeprecation", True(env->isolate()));
823-
}
824-
825-
// --no-warnings
826-
if (env->options()->no_warnings) {
827-
READONLY_PROPERTY(process, "noProcessWarnings", True(env->isolate()));
828-
}
829-
830-
// --trace-warnings
831-
if (env->options()->trace_warnings) {
832-
READONLY_PROPERTY(process, "traceProcessWarnings", True(env->isolate()));
833-
}
834-
835-
// --throw-deprecation
836-
if (env->options()->throw_deprecation) {
837-
READONLY_PROPERTY(process, "throwDeprecation", True(env->isolate()));
838-
}
839-
840-
#ifdef NODE_NO_BROWSER_GLOBALS
841-
// configure --no-browser-globals
842-
READONLY_PROPERTY(process, "_noBrowserGlobals", True(env->isolate()));
843-
#endif // NODE_NO_BROWSER_GLOBALS
844-
845-
// --prof-process
846-
// TODO(addaleax): Remove this.
847-
if (env->options()->prof_process) {
848-
READONLY_PROPERTY(process, "profProcess", True(env->isolate()));
849-
}
850-
851-
// --trace-deprecation
852-
if (env->options()->trace_deprecation) {
853-
READONLY_PROPERTY(process, "traceDeprecation", True(env->isolate()));
854-
}
855-
856-
// TODO(refack): move the following 4 to `node_config`
857-
// --inspect-brk
858-
if (env->options()->debug_options().wait_for_connect()) {
859-
READONLY_DONT_ENUM_PROPERTY(process,
860-
"_breakFirstLine", True(env->isolate()));
861-
}
862-
863-
if (env->options()->debug_options().break_node_first_line) {
864-
READONLY_DONT_ENUM_PROPERTY(process,
865-
"_breakNodeFirstLine", True(env->isolate()));
866-
}
867-
868-
// --inspect --debug-brk
869-
if (env->options()->debug_options().deprecated_invocation()) {
870-
READONLY_DONT_ENUM_PROPERTY(process,
871-
"_deprecatedDebugBrk", True(env->isolate()));
872-
}
873-
874-
// --debug or, --debug-brk without --inspect
875-
if (env->options()->debug_options().invalid_invocation()) {
876-
READONLY_DONT_ENUM_PROPERTY(process,
877-
"_invalidDebug", True(env->isolate()));
878-
}
879-
880-
// --security-revert flags
881-
#define V(code, _, __) \
882-
do { \
883-
if (IsReverted(SECURITY_REVERT_ ## code)) { \
884-
READONLY_PROPERTY(process, "REVERT_" #code, True(env->isolate())); \
885-
} \
886-
} while (0);
887-
SECURITY_REVERSIONS(V)
888-
#undef V
889-
890-
{
891-
size_t exec_path_len = 2 * PATH_MAX;
892-
std::vector<char> exec_path(exec_path_len);
893-
Local<String> exec_path_value;
894-
if (uv_exepath(exec_path.data(), &exec_path_len) == 0) {
895-
exec_path_value = String::NewFromUtf8(env->isolate(),
896-
exec_path.data(),
897-
NewStringType::kInternalized,
898-
exec_path_len).ToLocalChecked();
899-
} else {
900-
exec_path_value = String::NewFromUtf8(env->isolate(), args[0].c_str(),
901-
NewStringType::kInternalized).ToLocalChecked();
902-
}
903-
process->Set(env->context(),
904-
FIXED_ONE_BYTE_STRING(env->isolate(), "execPath"),
905-
exec_path_value).FromJust();
906-
}
907-
908-
auto debug_port_string = FIXED_ONE_BYTE_STRING(env->isolate(), "debugPort");
909-
CHECK(process->SetAccessor(env->context(),
910-
debug_port_string,
911-
DebugPortGetter,
912-
env->is_main_thread() ? DebugPortSetter : nullptr,
913-
env->as_external()).FromJust());
914-
}
915-
916-
917691
void SignalExit(int signo) {
918692
uv_tty_reset_mode();
919693
#ifdef __FreeBSD__

src/node_internals.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
185185
const char* warning,
186186
const char* deprecation_code);
187187

188-
void SetupProcessObject(Environment* env,
189-
const std::vector<std::string>& args,
190-
const std::vector<std::string>& exec_args);
188+
v8::Local<v8::Object> CreateProcessObject(
189+
Environment* env,
190+
const std::vector<std::string>& args,
191+
const std::vector<std::string>& exec_args);
191192

192193
enum Endianness {
193194
kLittleEndian, // _Not_ LITTLE_ENDIAN, clashes with endian.h.
File renamed without changes.

0 commit comments

Comments
 (0)