5 #include "src/pending-compilation-error-handler.h" 7 #include "src/ast/ast-value-factory.h" 8 #include "src/debug/debug.h" 9 #include "src/handles.h" 10 #include "src/isolate.h" 11 #include "src/messages.h" 12 #include "src/objects-inl.h" 17 Handle<String> PendingCompilationErrorHandler::MessageDetails::ArgumentString(
18 Isolate* isolate)
const {
19 if (arg_ !=
nullptr)
return arg_->string();
20 if (char_arg_ !=
nullptr) {
21 return isolate->factory()
22 ->NewStringFromUtf8(CStrVector(char_arg_))
25 return isolate->factory()->undefined_string();
28 MessageLocation PendingCompilationErrorHandler::MessageDetails::GetLocation(
29 Handle<Script> script)
const {
30 return MessageLocation(script, start_position_, end_position_);
33 void PendingCompilationErrorHandler::ReportMessageAt(
34 int start_position,
int end_position, MessageTemplate message,
35 const char* arg, ParseErrorType error_type) {
36 if (has_pending_error_)
return;
37 has_pending_error_ =
true;
40 MessageDetails(start_position, end_position, message,
nullptr, arg);
41 error_type_ = error_type;
44 void PendingCompilationErrorHandler::ReportMessageAt(
45 int start_position,
int end_position, MessageTemplate message,
46 const AstRawString* arg, ParseErrorType error_type) {
47 if (has_pending_error_)
return;
48 has_pending_error_ =
true;
51 MessageDetails(start_position, end_position, message, arg,
nullptr);
52 error_type_ = error_type;
55 void PendingCompilationErrorHandler::ReportWarningAt(
int start_position,
57 MessageTemplate message,
59 warning_messages_.emplace_front(
60 MessageDetails(start_position, end_position, message,
nullptr, arg));
63 void PendingCompilationErrorHandler::ReportWarnings(Isolate* isolate,
64 Handle<Script> script) {
65 DCHECK(!has_pending_error());
67 for (
const MessageDetails& warning : warning_messages_) {
68 MessageLocation location = warning.GetLocation(script);
69 Handle<String> argument = warning.ArgumentString(isolate);
70 Handle<JSMessageObject> message =
71 MessageHandler::MakeMessageObject(isolate, warning.message(), &location,
72 argument, Handle<FixedArray>::null());
73 message->set_error_level(v8::Isolate::kMessageWarning);
74 MessageHandler::ReportMessage(isolate, &location, message);
78 void PendingCompilationErrorHandler::ReportErrors(
79 Isolate* isolate, Handle<Script> script,
80 AstValueFactory* ast_value_factory) {
81 if (stack_overflow()) {
82 isolate->StackOverflow();
84 DCHECK(has_pending_error());
86 ast_value_factory->Internalize(isolate);
87 ThrowPendingError(isolate, script);
91 void PendingCompilationErrorHandler::ThrowPendingError(Isolate* isolate,
92 Handle<Script> script) {
93 if (!has_pending_error_)
return;
95 MessageLocation location = error_details_.GetLocation(script);
96 Handle<String> argument = error_details_.ArgumentString(isolate);
97 isolate->debug()->OnCompileError(script);
99 Factory* factory = isolate->factory();
100 Handle<Object> error;
101 switch (error_type_) {
102 case kReferenceError:
103 error = factory->NewReferenceError(error_details_.message(), argument);
106 error = factory->NewSyntaxError(error_details_.message(), argument);
113 if (!error->IsJSObject()) {
114 isolate->Throw(*error, &location);
118 Handle<JSObject> jserror = Handle<JSObject>::cast(error);
120 Handle<Name> key_start_pos = factory->error_start_pos_symbol();
121 JSObject::SetProperty(isolate, jserror, key_start_pos,
122 handle(Smi::FromInt(location.start_pos()), isolate),
123 LanguageMode::kSloppy)
126 Handle<Name> key_end_pos = factory->error_end_pos_symbol();
127 JSObject::SetProperty(isolate, jserror, key_end_pos,
128 handle(Smi::FromInt(location.end_pos()), isolate),
129 LanguageMode::kSloppy)
132 Handle<Name> key_script = factory->error_script_symbol();
133 JSObject::SetProperty(isolate, jserror, key_script, script,
134 LanguageMode::kSloppy)
137 isolate->Throw(*error, &location);
140 Handle<String> PendingCompilationErrorHandler::FormatErrorMessageForTest(
141 Isolate* isolate)
const {
142 return MessageFormatter::FormatMessage(
143 isolate, error_details_.message(),
144 error_details_.ArgumentString(isolate));