Skip to content

Commit e1aa730

Browse files
committed
src: emit warnings from V8
PR-URL: #24365 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 5f9b624 commit e1aa730

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

src/node.cc

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,12 +1078,6 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
10781078
// coverity[leaked_storage]
10791079
}
10801080

1081-
static void OnMessage(Local<Message> message, Local<Value> error) {
1082-
// The current version of V8 sends messages for errors only
1083-
// (thus `error` is always set).
1084-
FatalException(Isolate::GetCurrent(), error, message);
1085-
}
1086-
10871081
static Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
10881082
const char* warning,
10891083
const char* type = nullptr,
@@ -1160,6 +1154,33 @@ Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
11601154
deprecation_code);
11611155
}
11621156

1157+
static void OnMessage(Local<Message> message, Local<Value> error) {
1158+
Isolate* isolate = message->GetIsolate();
1159+
switch (message->ErrorLevel()) {
1160+
case Isolate::MessageErrorLevel::kMessageWarning: {
1161+
Environment* env = Environment::GetCurrent(isolate);
1162+
if (!env) {
1163+
break;
1164+
}
1165+
Utf8Value filename(isolate,
1166+
message->GetScriptOrigin().ResourceName());
1167+
// (filename):(line) (message)
1168+
std::stringstream warning;
1169+
warning << *filename;
1170+
warning << ":";
1171+
warning << message->GetLineNumber(env->context()).FromMaybe(-1);
1172+
warning << " ";
1173+
v8::String::Utf8Value msg(isolate, message->Get());
1174+
warning << *msg;
1175+
USE(ProcessEmitWarningGeneric(env, warning.str().c_str(), "V8"));
1176+
break;
1177+
}
1178+
case Isolate::MessageErrorLevel::kMessageError:
1179+
FatalException(isolate, error, message);
1180+
break;
1181+
}
1182+
}
1183+
11631184

11641185
static Local<Object> InitModule(Environment* env,
11651186
node_module* mod,
@@ -2583,7 +2604,9 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
25832604
v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
25842605
Isolate::Initialize(isolate, params);
25852606

2586-
isolate->AddMessageListener(OnMessage);
2607+
isolate->AddMessageListenerWithErrorLevel(OnMessage,
2608+
Isolate::MessageErrorLevel::kMessageError |
2609+
Isolate::MessageErrorLevel::kMessageWarning);
25872610
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
25882611
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
25892612
isolate->SetFatalErrorHandler(OnFatalError);

test/message/v8_warning.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
function AsmModule() {
6+
'use asm';
7+
8+
function add(a, b) {
9+
a = a | 0;
10+
b = b | 0;
11+
12+
// should be `return (a + b) | 0;`
13+
return a + b;
14+
}
15+
16+
return { add: add };
17+
}
18+
19+
AsmModule();

test/message/v8_warning.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(node:*) V8: *v8_warning.js:* Invalid asm.js: Invalid return type

0 commit comments

Comments
 (0)