@@ -16,7 +16,6 @@ using v8::NewStringType;
1616using v8::Nothing;
1717using v8::Object;
1818using v8::String;
19- using v8::Value;
2019
2120void RunAtExit (Environment* env) {
2221 env->RunAtExitCallbacks ();
@@ -36,18 +35,14 @@ Maybe<bool> EmitProcessBeforeExit(Environment* env) {
3635 if (!env->destroy_async_id_list ()->empty ())
3736 AsyncWrap::DestroyAsyncIdsCallback (env);
3837
39- HandleScope handle_scope (env->isolate ());
38+ Isolate* isolate = env->isolate ();
39+ HandleScope handle_scope (isolate);
4040 Local<Context> context = env->context ();
4141 Context::Scope context_scope (context);
4242
43- Local<Value> exit_code_v;
44- if (!env->process_object ()->Get (context, env->exit_code_string ())
45- .ToLocal (&exit_code_v)) return Nothing<bool >();
46-
47- Local<Integer> exit_code;
48- if (!exit_code_v->ToInteger (context).ToLocal (&exit_code)) {
49- return Nothing<bool >();
50- }
43+ Local<Integer> exit_code = v8::Integer::New (
44+ isolate,
45+ env->exit_code ().value_or (static_cast <int32_t >(ExitCode::kNoFailure )));
5146
5247 return ProcessEmit (env, " beforeExit" , exit_code).IsEmpty () ?
5348 Nothing<bool >() : Just (true );
@@ -67,27 +62,19 @@ Maybe<ExitCode> EmitProcessExitInternal(Environment* env) {
6762 HandleScope handle_scope (isolate);
6863 Local<Context> context = env->context ();
6964 Context::Scope context_scope (context);
70- Local<Object> process_object = env->process_object ();
7165
72- // TODO(addaleax): It might be nice to share process.exitCode via
73- // getter/setter pairs that pass data directly to the native side, so that we
74- // don't manually have to read and write JS properties here. These getters
75- // could use e.g. a typed array for performance.
7666 env->set_exiting (true );
7767
78- Local<String> exit_code = env->exit_code_string ();
79- Local<Value> code_v;
80- int code;
81- if (!process_object->Get (context, exit_code).ToLocal (&code_v) ||
82- !code_v->Int32Value (context).To (&code) ||
83- ProcessEmit (env, " exit" , Integer::New (isolate, code)).IsEmpty () ||
84- // Reload exit code, it may be changed by `emit('exit')`
85- !process_object->Get (context, exit_code).ToLocal (&code_v) ||
86- !code_v->Int32Value (context).To (&code)) {
68+ const std::optional<int32_t >& exit_code = env->exit_code ();
69+ const int no_failure = static_cast <int32_t >(ExitCode::kNoFailure );
70+
71+ if (ProcessEmit (
72+ env, " exit" , Integer::New (isolate, exit_code.value_or (no_failure)))
73+ .IsEmpty ()) {
8774 return Nothing<ExitCode>();
8875 }
89-
90- return Just (static_cast <ExitCode>(code ));
76+ // Reload exit code, it may be changed by `emit('exit')`
77+ return Just (static_cast <ExitCode>(exit_code. value_or (no_failure) ));
9178}
9279
9380Maybe<int > EmitProcessExit (Environment* env) {
0 commit comments