Skip to content

Commit

Permalink
src: move OnMessage to node_errors.cc
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
joyeecheung authored and targos committed Apr 27, 2019
1 parent 8302148 commit 071300b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
29 changes: 1 addition & 28 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "node_internals.h"
#include "node_native_module_env.h"
#include "node_platform.h"
#include "node_process.h"
#include "node_v8_platform-inl.h"
#include "uv.h"

Expand Down Expand Up @@ -46,32 +45,6 @@ static bool ShouldAbortOnUncaughtException(Isolate* isolate) {
!env->inside_should_not_abort_on_uncaught_scope();
}

static void OnMessage(Local<Message> message, Local<Value> error) {
Isolate* isolate = message->GetIsolate();
switch (message->ErrorLevel()) {
case Isolate::MessageErrorLevel::kMessageWarning: {
Environment* env = Environment::GetCurrent(isolate);
if (!env) {
break;
}
Utf8Value filename(isolate, message->GetScriptOrigin().ResourceName());
// (filename):(line) (message)
std::stringstream warning;
warning << *filename;
warning << ":";
warning << message->GetLineNumber(env->context()).FromMaybe(-1);
warning << " ";
v8::String::Utf8Value msg(isolate, message->Get());
warning << *msg;
USE(ProcessEmitWarningGeneric(env, warning.str().c_str(), "V8"));
break;
}
case Isolate::MessageErrorLevel::kMessageError:
FatalException(isolate, error, message);
break;
}
}

void* NodeArrayBufferAllocator::Allocate(size_t size) {
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
return UncheckedCalloc(size);
Expand Down Expand Up @@ -187,7 +160,7 @@ void SetIsolateUpForNode(v8::Isolate* isolate, IsolateSettingCategories cat) {
switch (cat) {
case IsolateSettingCategories::kErrorHandlers:
isolate->AddMessageListenerWithErrorLevel(
OnMessage,
errors::PerIsolateMessageListener,
Isolate::MessageErrorLevel::kMessageError |
Isolate::MessageErrorLevel::kMessageWarning);
isolate->SetAbortOnUncaughtExceptionCallback(
Expand Down
27 changes: 27 additions & 0 deletions src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifdef NODE_REPORT
#include "node_report.h"
#endif
#include "node_process.h"
#include "node_v8_platform-inl.h"

namespace node {
Expand Down Expand Up @@ -739,6 +740,32 @@ const char* errno_string(int errorno) {
}
}

void PerIsolateMessageListener(Local<Message> message, Local<Value> error) {
Isolate* isolate = message->GetIsolate();
switch (message->ErrorLevel()) {
case Isolate::MessageErrorLevel::kMessageWarning: {
Environment* env = Environment::GetCurrent(isolate);
if (!env) {
break;
}
Utf8Value filename(isolate, message->GetScriptOrigin().ResourceName());
// (filename):(line) (message)
std::stringstream warning;
warning << *filename;
warning << ":";
warning << message->GetLineNumber(env->context()).FromMaybe(-1);
warning << " ";
v8::String::Utf8Value msg(isolate, message->Get());
warning << *msg;
USE(ProcessEmitWarningGeneric(env, warning.str().c_str(), "V8"));
break;
}
case Isolate::MessageErrorLevel::kMessageError:
FatalException(isolate, error, message);
break;
}
}

} // namespace errors

void DecorateErrorStack(Environment* env,
Expand Down
2 changes: 2 additions & 0 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class TryCatchScope : public v8::TryCatch {
};

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

} // namespace errors

Expand Down

0 comments on commit 071300b

Please sign in to comment.