Skip to content

Commit 071300b

Browse files
joyeecheungtargos
authored andcommitted
src: move OnMessage to node_errors.cc
Rename the per-isolate message listener to `PerIsolateMessageListener` and move it to `node_errors.cc` since it's part of the error handling process. It also creates an external reference so it needs to be exposed in `node_errors.h` for a snapshot builder to know. PR-URL: #27304 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 8302148 commit 071300b

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

src/api/environment.cc

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "node_internals.h"
66
#include "node_native_module_env.h"
77
#include "node_platform.h"
8-
#include "node_process.h"
98
#include "node_v8_platform-inl.h"
109
#include "uv.h"
1110

@@ -46,32 +45,6 @@ static bool ShouldAbortOnUncaughtException(Isolate* isolate) {
4645
!env->inside_should_not_abort_on_uncaught_scope();
4746
}
4847

49-
static void OnMessage(Local<Message> message, Local<Value> error) {
50-
Isolate* isolate = message->GetIsolate();
51-
switch (message->ErrorLevel()) {
52-
case Isolate::MessageErrorLevel::kMessageWarning: {
53-
Environment* env = Environment::GetCurrent(isolate);
54-
if (!env) {
55-
break;
56-
}
57-
Utf8Value filename(isolate, message->GetScriptOrigin().ResourceName());
58-
// (filename):(line) (message)
59-
std::stringstream warning;
60-
warning << *filename;
61-
warning << ":";
62-
warning << message->GetLineNumber(env->context()).FromMaybe(-1);
63-
warning << " ";
64-
v8::String::Utf8Value msg(isolate, message->Get());
65-
warning << *msg;
66-
USE(ProcessEmitWarningGeneric(env, warning.str().c_str(), "V8"));
67-
break;
68-
}
69-
case Isolate::MessageErrorLevel::kMessageError:
70-
FatalException(isolate, error, message);
71-
break;
72-
}
73-
}
74-
7548
void* NodeArrayBufferAllocator::Allocate(size_t size) {
7649
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
7750
return UncheckedCalloc(size);
@@ -187,7 +160,7 @@ void SetIsolateUpForNode(v8::Isolate* isolate, IsolateSettingCategories cat) {
187160
switch (cat) {
188161
case IsolateSettingCategories::kErrorHandlers:
189162
isolate->AddMessageListenerWithErrorLevel(
190-
OnMessage,
163+
errors::PerIsolateMessageListener,
191164
Isolate::MessageErrorLevel::kMessageError |
192165
Isolate::MessageErrorLevel::kMessageWarning);
193166
isolate->SetAbortOnUncaughtExceptionCallback(

src/node_errors.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifdef NODE_REPORT
77
#include "node_report.h"
88
#endif
9+
#include "node_process.h"
910
#include "node_v8_platform-inl.h"
1011

1112
namespace node {
@@ -739,6 +740,32 @@ const char* errno_string(int errorno) {
739740
}
740741
}
741742

743+
void PerIsolateMessageListener(Local<Message> message, Local<Value> error) {
744+
Isolate* isolate = message->GetIsolate();
745+
switch (message->ErrorLevel()) {
746+
case Isolate::MessageErrorLevel::kMessageWarning: {
747+
Environment* env = Environment::GetCurrent(isolate);
748+
if (!env) {
749+
break;
750+
}
751+
Utf8Value filename(isolate, message->GetScriptOrigin().ResourceName());
752+
// (filename):(line) (message)
753+
std::stringstream warning;
754+
warning << *filename;
755+
warning << ":";
756+
warning << message->GetLineNumber(env->context()).FromMaybe(-1);
757+
warning << " ";
758+
v8::String::Utf8Value msg(isolate, message->Get());
759+
warning << *msg;
760+
USE(ProcessEmitWarningGeneric(env, warning.str().c_str(), "V8"));
761+
break;
762+
}
763+
case Isolate::MessageErrorLevel::kMessageError:
764+
FatalException(isolate, error, message);
765+
break;
766+
}
767+
}
768+
742769
} // namespace errors
743770

744771
void DecorateErrorStack(Environment* env,

src/node_errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ class TryCatchScope : public v8::TryCatch {
191191
};
192192

193193
const char* errno_string(int errorno);
194+
void PerIsolateMessageListener(v8::Local<v8::Message> message,
195+
v8::Local<v8::Value> error);
194196

195197
} // namespace errors
196198

0 commit comments

Comments
 (0)