Skip to content

Commit 5add2b5

Browse files
addaleaxtargos
authored andcommittedJan 29, 2019
src: pass along errors from process obj instantiation
PR-URL: #25734 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gus Caplan <[email protected]>
1 parent 2928672 commit 5add2b5

7 files changed

+64
-49
lines changed
 

‎src/env.cc

+27-23
Original file line numberDiff line numberDiff line change
@@ -289,29 +289,10 @@ Environment::~Environment() {
289289
}
290290
}
291291

292-
void Environment::Start(const std::vector<std::string>& args,
293-
const std::vector<std::string>& exec_args,
294-
bool start_profiler_idle_notifier) {
292+
void Environment::Start(bool start_profiler_idle_notifier) {
295293
HandleScope handle_scope(isolate());
296294
Context::Scope context_scope(context());
297295

298-
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
299-
TRACING_CATEGORY_NODE1(environment)) != 0) {
300-
auto traced_value = tracing::TracedValue::Create();
301-
traced_value->BeginArray("args");
302-
for (const std::string& arg : args)
303-
traced_value->AppendString(arg);
304-
traced_value->EndArray();
305-
traced_value->BeginArray("exec_args");
306-
for (const std::string& arg : exec_args)
307-
traced_value->AppendString(arg);
308-
traced_value->EndArray();
309-
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(
310-
TRACING_CATEGORY_NODE1(environment),
311-
"Environment", this,
312-
"args", std::move(traced_value));
313-
}
314-
315296
CHECK_EQ(0, uv_timer_init(event_loop(), timer_handle()));
316297
uv_unref(reinterpret_cast<uv_handle_t*>(timer_handle()));
317298

@@ -346,14 +327,37 @@ void Environment::Start(const std::vector<std::string>& args,
346327
StartProfilerIdleNotifier();
347328
}
348329

349-
Local<Object> process_object = CreateProcessObject(this, args, exec_args);
350-
set_process_object(process_object);
351-
352330
static uv_once_t init_once = UV_ONCE_INIT;
353331
uv_once(&init_once, InitThreadLocalOnce);
354332
uv_key_set(&thread_local_env, this);
355333
}
356334

335+
MaybeLocal<Object> Environment::CreateProcessObject(
336+
const std::vector<std::string>& args,
337+
const std::vector<std::string>& exec_args) {
338+
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
339+
TRACING_CATEGORY_NODE1(environment)) != 0) {
340+
auto traced_value = tracing::TracedValue::Create();
341+
traced_value->BeginArray("args");
342+
for (const std::string& arg : args) traced_value->AppendString(arg);
343+
traced_value->EndArray();
344+
traced_value->BeginArray("exec_args");
345+
for (const std::string& arg : exec_args) traced_value->AppendString(arg);
346+
traced_value->EndArray();
347+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(environment),
348+
"Environment",
349+
this,
350+
"args",
351+
std::move(traced_value));
352+
}
353+
354+
Local<Object> process_object =
355+
node::CreateProcessObject(this, args, exec_args)
356+
.FromMaybe(Local<Object>());
357+
set_process_object(process_object);
358+
return process_object;
359+
}
360+
357361
void Environment::RegisterHandleCleanups() {
358362
HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
359363
void* arg) {

‎src/env.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,10 @@ class Environment {
610610
v8::Local<v8::Context> context);
611611
~Environment();
612612

613-
void Start(const std::vector<std::string>& args,
614-
const std::vector<std::string>& exec_args,
615-
bool start_profiler_idle_notifier);
613+
void Start(bool start_profiler_idle_notifier);
614+
v8::MaybeLocal<v8::Object> CreateProcessObject(
615+
const std::vector<std::string>& args,
616+
const std::vector<std::string>& exec_args);
616617

617618
typedef void (*HandleCleanupCb)(Environment* env,
618619
uv_handle_t* handle,

‎src/node.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,8 @@ Environment* CreateEnvironment(IsolateData* isolate_data,
11701170
std::vector<std::string> args(argv, argv + argc);
11711171
std::vector<std::string> exec_args(exec_argv, exec_argv + exec_argc);
11721172
Environment* env = new Environment(isolate_data, context);
1173-
env->Start(args, exec_args, per_process::v8_is_profiling);
1173+
env->Start(per_process::v8_is_profiling);
1174+
env->CreateProcessObject(args, exec_args);
11741175
return env;
11751176
}
11761177

@@ -1244,7 +1245,8 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
12441245
Local<Context> context = NewContext(isolate);
12451246
Context::Scope context_scope(context);
12461247
Environment env(isolate_data, context);
1247-
env.Start(args, exec_args, per_process::v8_is_profiling);
1248+
env.Start(per_process::v8_is_profiling);
1249+
env.CreateProcessObject(args, exec_args);
12481250

12491251
#if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
12501252
CHECK(!env.inspector_agent()->IsListening());

‎src/node_env_var.cc

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ using v8::EscapableHandleScope;
1717
using v8::Integer;
1818
using v8::Isolate;
1919
using v8::Local;
20+
using v8::MaybeLocal;
2021
using v8::Name;
2122
using v8::NamedPropertyHandlerConfiguration;
2223
using v8::NewStringType;
@@ -209,15 +210,13 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
209210
info.GetReturnValue().Set(envarr);
210211
}
211212

212-
Local<Object> CreateEnvVarProxy(Local<Context> context,
213-
Isolate* isolate,
214-
Local<Value> data) {
213+
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context,
214+
Isolate* isolate,
215+
Local<Value> data) {
215216
EscapableHandleScope scope(isolate);
216217
Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New(isolate);
217218
env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration(
218219
EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, data));
219-
Local<Object> env_proxy =
220-
env_proxy_template->NewInstance(context).ToLocalChecked();
221-
return scope.Escape(env_proxy);
220+
return scope.EscapeMaybe(env_proxy_template->NewInstance(context));
222221
}
223222
} // namespace node

‎src/node_process.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
namespace node {
99

10-
v8::Local<v8::Object> CreateEnvVarProxy(v8::Local<v8::Context> context,
11-
v8::Isolate* isolate,
12-
v8::Local<v8::Value> data);
10+
v8::MaybeLocal<v8::Object> CreateEnvVarProxy(v8::Local<v8::Context> context,
11+
v8::Isolate* isolate,
12+
v8::Local<v8::Value> data);
1313

1414
// Most of the time, it's best to use `console.error` to write
1515
// to the process.stderr stream. However, in some cases, such as
@@ -31,7 +31,7 @@ v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
3131
const char* warning,
3232
const char* deprecation_code);
3333

34-
v8::Local<v8::Object> CreateProcessObject(
34+
v8::MaybeLocal<v8::Object> CreateProcessObject(
3535
Environment* env,
3636
const std::vector<std::string>& args,
3737
const std::vector<std::string>& exec_args);

‎src/node_process_object.cc

+17-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ using v8::Integer;
1919
using v8::Isolate;
2020
using v8::Just;
2121
using v8::Local;
22+
using v8::MaybeLocal;
2223
using v8::Name;
2324
using v8::NewStringType;
2425
using v8::None;
@@ -66,19 +67,22 @@ static void GetParentProcessId(Local<Name> property,
6667
info.GetReturnValue().Set(uv_os_getppid());
6768
}
6869

69-
Local<Object> CreateProcessObject(Environment* env,
70-
const std::vector<std::string>& args,
71-
const std::vector<std::string>& exec_args) {
70+
MaybeLocal<Object> CreateProcessObject(
71+
Environment* env,
72+
const std::vector<std::string>& args,
73+
const std::vector<std::string>& exec_args) {
7274
Isolate* isolate = env->isolate();
7375
EscapableHandleScope scope(isolate);
7476
Local<Context> context = env->context();
7577

7678
Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
7779
process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "process"));
78-
Local<Object> process = process_template->GetFunction(context)
79-
.ToLocalChecked()
80-
->NewInstance(context)
81-
.ToLocalChecked();
80+
Local<Function> process_ctor;
81+
Local<Object> process;
82+
if (!process_template->GetFunction(context).ToLocal(&process_ctor) ||
83+
!process_ctor->NewInstance(context).ToLocal(&process)) {
84+
return MaybeLocal<Object>();
85+
}
8286

8387
// process.title
8488
auto title_string = FIXED_ONE_BYTE_STRING(env->isolate(), "title");
@@ -145,11 +149,16 @@ Local<Object> CreateProcessObject(Environment* env,
145149
ToV8Value(env->context(), exec_args)
146150
.ToLocalChecked()).FromJust();
147151

152+
Local<Object> env_var_proxy;
153+
if (!CreateEnvVarProxy(context, isolate, env->as_external())
154+
.ToLocal(&env_var_proxy))
155+
return MaybeLocal<Object>();
156+
148157
// process.env
149158
process
150159
->Set(env->context(),
151160
FIXED_ONE_BYTE_STRING(env->isolate(), "env"),
152-
CreateEnvVarProxy(context, isolate, env->as_external()))
161+
env_var_proxy)
153162
.FromJust();
154163

155164
READONLY_PROPERTY(process, "pid",

‎src/node_worker.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ Worker::Worker(Environment* env,
132132
env_->set_worker_context(this);
133133
env_->set_thread_id(thread_id_);
134134

135-
env_->Start(std::vector<std::string>{},
136-
std::vector<std::string>{},
137-
env->profiler_idle_notifier_started());
135+
env_->Start(env->profiler_idle_notifier_started());
136+
env_->CreateProcessObject(std::vector<std::string>{},
137+
std::vector<std::string>{});
138138
// Done while on the parent thread
139139
AddWorkerInspector(env, env_.get(), thread_id_, url_);
140140
}

0 commit comments

Comments
 (0)
Please sign in to comment.