From 5d67eeca1a9f9c033b55f49c115e77b693ca4c07 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Wed, 14 Nov 2018 09:06:18 -0600 Subject: [PATCH] src: emit warnings from V8 PR-URL: https://github.com/nodejs/node/pull/24365 Reviewed-By: Anna Henningsen Reviewed-By: Joyee Cheung Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Refael Ackermann Reviewed-By: Colin Ihrig --- src/node.cc | 37 ++++++++++++++++++++++++++++++------- test/message/v8_warning.js | 19 +++++++++++++++++++ test/message/v8_warning.out | 1 + 3 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 test/message/v8_warning.js create mode 100644 test/message/v8_warning.out diff --git a/src/node.cc b/src/node.cc index 4c84f781707b01..153399f1b61777 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1074,12 +1074,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, @@ -1156,6 +1150,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, @@ -2580,7 +2601,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