src: consolidate exit codes in the code base

Add an ExitCode enum class and use it throughout the code base
instead of hard-coding the exit codes everywhere. At the moment,
the exit codes used in many places do not actually conform to
what the documentation describes. With the new enums (which
are also available to the JS land as constants in an internal
binding) we could migrate to a more consistent usage of the
codes, and eventually expose the constants to the user land
when they are stable enough.

PR-URL: https://github.com/nodejs/node/pull/44746
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
This commit is contained in:
Joyee Cheung authored and GitHub committed 2022-10-06 12:41:16 +00:00
1 parent 66cedb4600
commit be525d7d04
36 files changed
+381 -177

No files matched your search

+12 -4
View File
@@ -7,6 +7,7 @@ using v8::Function;
using v8::Global;
using v8::HandleScope;
using v8::Isolate;
using v8::Just;
using v8::Local;
using v8::Locker;
using v8::Maybe;
@@ -15,7 +16,7 @@ using v8::SealHandleScope;
namespace node {
Maybe<int> SpinEventLoop(Environment* env) {
Maybe<ExitCode> SpinEventLoopInternal(Environment* env) {
CHECK_NOT_NULL(env);
MultiIsolatePlatform* platform = GetMultiIsolatePlatform(env);
CHECK_NOT_NULL(platform);
@@ -25,7 +26,7 @@ Maybe<int> SpinEventLoop(Environment* env) {
Context::Scope context_scope(env->context());
SealHandleScope seal(isolate);
if (env->is_stopping()) return Nothing<int>();
if (env->is_stopping()) return Nothing<ExitCode>();
env->set_trace_sync_io(env->options()->trace_sync_io);
{
@@ -59,7 +60,7 @@ Maybe<int> SpinEventLoop(Environment* env) {
env->performance_state()->Mark(
node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
}
if (env->is_stopping()) return Nothing<int>();
if (env->is_stopping()) return Nothing<ExitCode>();
env->set_trace_sync_io(false);
// Clear the serialize callback even though the JS-land queue should
@@ -69,7 +70,7 @@ Maybe<int> SpinEventLoop(Environment* env) {
env->PrintInfoForSnapshotIfDebug();
env->ForEachRealm([](Realm* realm) { realm->VerifyNoStrongBaseObjects(); });
return EmitProcessExit(env);
return EmitProcessExitInternal(env);
}
struct CommonEnvironmentSetup::Impl {
@@ -155,6 +156,13 @@ CommonEnvironmentSetup::~CommonEnvironmentSetup() {
delete impl_;
}
Maybe<int> SpinEventLoop(Environment* env) {
Maybe<ExitCode> result = SpinEventLoopInternal(env);
if (result.IsNothing()) {
return Nothing<int>();
}
return Just(static_cast<int>(result.FromJust()));
}
uv_loop_t* CommonEnvironmentSetup::event_loop() const {
return &impl_->loop;
+22 -4
View File
@@ -1,7 +1,9 @@
#include <cstdlib>
#include "node.h"
#include "node_builtins.h"
#include "node_context_data.h"
#include "node_errors.h"
#include "node_exit_code.h"
#include "node_internals.h"
#include "node_options-inl.h"
#include "node_platform.h"
@@ -368,6 +370,7 @@ Environment* CreateEnvironment(
Environment* env = new Environment(
isolate_data, context, args, exec_args, nullptr, flags, thread_id);
#if HAVE_INSPECTOR
// TODO(joyeecheung): handle the exit code returned by InitializeInspector().
if (env->should_create_inspector()) {
if (inspector_parent_handle) {
env->InitializeInspector(
@@ -763,19 +766,34 @@ ThreadId AllocateEnvironmentThreadId() {
return ThreadId { next_thread_id++ };
}
void DefaultProcessExitHandler(Environment* env, int exit_code) {
[[noreturn]] void Exit(ExitCode exit_code) {
exit(static_cast<int>(exit_code));
}
void DefaultProcessExitHandlerInternal(Environment* env, ExitCode exit_code) {
env->set_can_call_into_js(false);
env->stop_sub_worker_contexts();
env->isolate()->DumpAndResetStats();
DisposePlatform();
uv_library_shutdown();
exit(exit_code);
Exit(exit_code);
}
void DefaultProcessExitHandler(Environment* env, int exit_code) {
DefaultProcessExitHandlerInternal(env, static_cast<ExitCode>(exit_code));
}
void SetProcessExitHandler(Environment* env,
std::function<void(Environment*, int)>&& handler) {
void SetProcessExitHandler(
Environment* env, std::function<void(Environment*, ExitCode)>&& handler) {
env->set_process_exit_handler(std::move(handler));
}
void SetProcessExitHandler(Environment* env,
std::function<void(Environment*, int)>&& handler) {
auto movedHandler = std::move(handler);
env->set_process_exit_handler([=](Environment* env, ExitCode exit_code) {
movedHandler(env, static_cast<int>(exit_code));
});
}
} // namespace node
+17 -5
View File
@@ -53,11 +53,15 @@ Maybe<bool> EmitProcessBeforeExit(Environment* env) {
Nothing<bool>() : Just(true);
}
int EmitExit(Environment* env) {
return EmitProcessExit(env).FromMaybe(1);
static ExitCode EmitExitInternal(Environment* env) {
return EmitProcessExitInternal(env).FromMaybe(ExitCode::kGenericUserError);
}
Maybe<int> EmitProcessExit(Environment* env) {
int EmitExit(Environment* env) {
return static_cast<int>(EmitExitInternal(env));
}
Maybe<ExitCode> EmitProcessExitInternal(Environment* env) {
// process.emit('exit')
Isolate* isolate = env->isolate();
HandleScope handle_scope(isolate);
@@ -80,10 +84,18 @@ Maybe<int> EmitProcessExit(Environment* env) {
// Reload exit code, it may be changed by `emit('exit')`
!process_object->Get(context, exit_code).ToLocal(&code_v) ||
!code_v->Int32Value(context).To(&code)) {
return Nothing<int>();
return Nothing<ExitCode>();
}
return Just(code);
return Just(static_cast<ExitCode>(code));
}
Maybe<int> EmitProcessExit(Environment* env) {
Maybe<ExitCode> result = EmitProcessExitInternal(env);
if (result.IsNothing()) {
return Nothing<int>();
}
return Just(static_cast<int>(result.FromJust()));
}
typedef void (*CleanupHook)(void* arg);