diff --git a/src/node.cc b/src/node.cc index 48e8f0a2497700..f6ec033bbb1901 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1078,12 +1078,6 @@ static void DLOpen(const FunctionCallbackInfo& args) { // coverity[leaked_storage] } -static void OnMessage(Local message, Local error) { - // The current version of V8 sends messages for errors only - // (thus `error` is always set). - FatalException(Isolate::GetCurrent(), error, message); -} - static Maybe ProcessEmitWarningGeneric(Environment* env, const char* warning, const char* type = nullptr, @@ -1160,6 +1154,33 @@ Maybe ProcessEmitDeprecationWarning(Environment* env, deprecation_code); } +static void OnMessage(Local message, Local 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; + } +} + static Local InitModule(Environment* env, node_module* mod, @@ -2583,7 +2604,9 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) { v8_platform.Platform()->RegisterIsolate(isolate, event_loop); Isolate::Initialize(isolate, params); - isolate->AddMessageListener(OnMessage); + isolate->AddMessageListenerWithErrorLevel(OnMessage, + Isolate::MessageErrorLevel::kMessageError | + Isolate::MessageErrorLevel::kMessageWarning); isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException); isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit); isolate->SetFatalErrorHandler(OnFatalError); diff --git a/test/message/v8_warning.js b/test/message/v8_warning.js new file mode 100644 index 00000000000000..e3394cdd7094e8 --- /dev/null +++ b/test/message/v8_warning.js @@ -0,0 +1,19 @@ +'use strict'; + +require('../common'); + +function AsmModule() { + 'use asm'; + + function add(a, b) { + a = a | 0; + b = b | 0; + + // should be `return (a + b) | 0;` + return a + b; + } + + return { add: add }; +} + +AsmModule(); diff --git a/test/message/v8_warning.out b/test/message/v8_warning.out new file mode 100644 index 00000000000000..7943d0e9d64dfe --- /dev/null +++ b/test/message/v8_warning.out @@ -0,0 +1 @@ +(node:*) V8: *v8_warning.js:* Invalid asm.js: Invalid return type