Home | History | Annotate | Download | only in src
      1 // Copyright 2011 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "src/messages.h"
      6 
      7 #include <memory>
      8 
      9 #include "src/api-inl.h"
     10 #include "src/execution.h"
     11 #include "src/isolate-inl.h"
     12 #include "src/keys.h"
     13 #include "src/objects/frame-array-inl.h"
     14 #include "src/objects/js-array-inl.h"
     15 #include "src/string-builder-inl.h"
     16 #include "src/wasm/wasm-code-manager.h"
     17 #include "src/wasm/wasm-objects.h"
     18 
     19 namespace v8 {
     20 namespace internal {
     21 
     22 MessageLocation::MessageLocation(Handle<Script> script, int start_pos,
     23                                  int end_pos)
     24     : script_(script), start_pos_(start_pos), end_pos_(end_pos) {}
     25 MessageLocation::MessageLocation(Handle<Script> script, int start_pos,
     26                                  int end_pos, Handle<SharedFunctionInfo> shared)
     27     : script_(script),
     28       start_pos_(start_pos),
     29       end_pos_(end_pos),
     30       shared_(shared) {}
     31 MessageLocation::MessageLocation() : start_pos_(-1), end_pos_(-1) {}
     32 
     33 // If no message listeners have been registered this one is called
     34 // by default.
     35 void MessageHandler::DefaultMessageReport(Isolate* isolate,
     36                                           const MessageLocation* loc,
     37                                           Handle<Object> message_obj) {
     38   std::unique_ptr<char[]> str = GetLocalizedMessage(isolate, message_obj);
     39   if (loc == nullptr) {
     40     PrintF("%s\n", str.get());
     41   } else {
     42     HandleScope scope(isolate);
     43     Handle<Object> data(loc->script()->name(), isolate);
     44     std::unique_ptr<char[]> data_str;
     45     if (data->IsString())
     46       data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
     47     PrintF("%s:%i: %s\n", data_str.get() ? data_str.get() : "<unknown>",
     48            loc->start_pos(), str.get());
     49   }
     50 }
     51 
     52 Handle<JSMessageObject> MessageHandler::MakeMessageObject(
     53     Isolate* isolate, MessageTemplate::Template message,
     54     const MessageLocation* location, Handle<Object> argument,
     55     Handle<FixedArray> stack_frames) {
     56   Factory* factory = isolate->factory();
     57 
     58   int start = -1;
     59   int end = -1;
     60   Handle<Script> script_handle = isolate->factory()->empty_script();
     61   if (location != nullptr) {
     62     start = location->start_pos();
     63     end = location->end_pos();
     64     script_handle = location->script();
     65   }
     66 
     67   Handle<Object> stack_frames_handle = stack_frames.is_null()
     68       ? Handle<Object>::cast(factory->undefined_value())
     69       : Handle<Object>::cast(stack_frames);
     70 
     71   Handle<JSMessageObject> message_obj = factory->NewJSMessageObject(
     72       message, argument, start, end, script_handle, stack_frames_handle);
     73 
     74   return message_obj;
     75 }
     76 
     77 void MessageHandler::ReportMessage(Isolate* isolate, const MessageLocation* loc,
     78                                    Handle<JSMessageObject> message) {
     79   v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
     80 
     81   if (api_message_obj->ErrorLevel() == v8::Isolate::kMessageError) {
     82     // We are calling into embedder's code which can throw exceptions.
     83     // Thus we need to save current exception state, reset it to the clean one
     84     // and ignore scheduled exceptions callbacks can throw.
     85 
     86     // We pass the exception object into the message handler callback though.
     87     Object* exception_object = ReadOnlyRoots(isolate).undefined_value();
     88     if (isolate->has_pending_exception()) {
     89       exception_object = isolate->pending_exception();
     90     }
     91     Handle<Object> exception(exception_object, isolate);
     92 
     93     Isolate::ExceptionScope exception_scope(isolate);
     94     isolate->clear_pending_exception();
     95     isolate->set_external_caught_exception(false);
     96 
     97     // Turn the exception on the message into a string if it is an object.
     98     if (message->argument()->IsJSObject()) {
     99       HandleScope scope(isolate);
    100       Handle<Object> argument(message->argument(), isolate);
    101 
    102       MaybeHandle<Object> maybe_stringified;
    103       Handle<Object> stringified;
    104       // Make sure we don't leak uncaught internally generated Error objects.
    105       if (argument->IsJSError()) {
    106         maybe_stringified = Object::NoSideEffectsToString(isolate, argument);
    107       } else {
    108         v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
    109         catcher.SetVerbose(false);
    110         catcher.SetCaptureMessage(false);
    111 
    112         maybe_stringified = Object::ToString(isolate, argument);
    113       }
    114 
    115       if (!maybe_stringified.ToHandle(&stringified)) {
    116         DCHECK(isolate->has_pending_exception());
    117         isolate->clear_pending_exception();
    118         isolate->set_external_caught_exception(false);
    119         stringified =
    120             isolate->factory()->NewStringFromAsciiChecked("exception");
    121       }
    122       message->set_argument(*stringified);
    123     }
    124 
    125     v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception);
    126     ReportMessageNoExceptions(isolate, loc, message, api_exception_obj);
    127   } else {
    128     ReportMessageNoExceptions(isolate, loc, message, v8::Local<v8::Value>());
    129   }
    130 }
    131 
    132 void MessageHandler::ReportMessageNoExceptions(
    133     Isolate* isolate, const MessageLocation* loc, Handle<Object> message,
    134     v8::Local<v8::Value> api_exception_obj) {
    135   v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
    136   int error_level = api_message_obj->ErrorLevel();
    137 
    138   Handle<TemplateList> global_listeners =
    139       isolate->factory()->message_listeners();
    140   int global_length = global_listeners->length();
    141   if (global_length == 0) {
    142     DefaultMessageReport(isolate, loc, message);
    143     if (isolate->has_scheduled_exception()) {
    144       isolate->clear_scheduled_exception();
    145     }
    146   } else {
    147     for (int i = 0; i < global_length; i++) {
    148       HandleScope scope(isolate);
    149       if (global_listeners->get(i)->IsUndefined(isolate)) continue;
    150       FixedArray* listener = FixedArray::cast(global_listeners->get(i));
    151       Foreign* callback_obj = Foreign::cast(listener->get(0));
    152       int32_t message_levels =
    153           static_cast<int32_t>(Smi::ToInt(listener->get(2)));
    154       if (!(message_levels & error_level)) {
    155         continue;
    156       }
    157       v8::MessageCallback callback =
    158           FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address());
    159       Handle<Object> callback_data(listener->get(1), isolate);
    160       {
    161         // Do not allow exceptions to propagate.
    162         v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
    163         callback(api_message_obj, callback_data->IsUndefined(isolate)
    164                                       ? api_exception_obj
    165                                       : v8::Utils::ToLocal(callback_data));
    166       }
    167       if (isolate->has_scheduled_exception()) {
    168         isolate->clear_scheduled_exception();
    169       }
    170     }
    171   }
    172 }
    173 
    174 
    175 Handle<String> MessageHandler::GetMessage(Isolate* isolate,
    176                                           Handle<Object> data) {
    177   Handle<JSMessageObject> message = Handle<JSMessageObject>::cast(data);
    178   Handle<Object> arg = Handle<Object>(message->argument(), isolate);
    179   return MessageTemplate::FormatMessage(isolate, message->type(), arg);
    180 }
    181 
    182 std::unique_ptr<char[]> MessageHandler::GetLocalizedMessage(
    183     Isolate* isolate, Handle<Object> data) {
    184   HandleScope scope(isolate);
    185   return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS);
    186 }
    187 
    188 namespace {
    189 
    190 Object* EvalFromFunctionName(Isolate* isolate, Handle<Script> script) {
    191   if (!script->has_eval_from_shared())
    192     return ReadOnlyRoots(isolate).undefined_value();
    193 
    194   Handle<SharedFunctionInfo> shared(script->eval_from_shared(), isolate);
    195   // Find the name of the function calling eval.
    196   if (shared->Name()->BooleanValue(isolate)) {
    197     return shared->Name();
    198   }
    199 
    200   return shared->inferred_name();
    201 }
    202 
    203 Object* EvalFromScript(Isolate* isolate, Handle<Script> script) {
    204   if (!script->has_eval_from_shared())
    205     return ReadOnlyRoots(isolate).undefined_value();
    206 
    207   Handle<SharedFunctionInfo> eval_from_shared(script->eval_from_shared(),
    208                                               isolate);
    209   return eval_from_shared->script()->IsScript()
    210              ? eval_from_shared->script()
    211              : ReadOnlyRoots(isolate).undefined_value();
    212 }
    213 
    214 MaybeHandle<String> FormatEvalOrigin(Isolate* isolate, Handle<Script> script) {
    215   Handle<Object> sourceURL(script->GetNameOrSourceURL(), isolate);
    216   if (!sourceURL->IsUndefined(isolate)) {
    217     DCHECK(sourceURL->IsString());
    218     return Handle<String>::cast(sourceURL);
    219   }
    220 
    221   IncrementalStringBuilder builder(isolate);
    222   builder.AppendCString("eval at ");
    223 
    224   Handle<Object> eval_from_function_name =
    225       handle(EvalFromFunctionName(isolate, script), isolate);
    226   if (eval_from_function_name->BooleanValue(isolate)) {
    227     Handle<String> str;
    228     ASSIGN_RETURN_ON_EXCEPTION(
    229         isolate, str, Object::ToString(isolate, eval_from_function_name),
    230         String);
    231     builder.AppendString(str);
    232   } else {
    233     builder.AppendCString("<anonymous>");
    234   }
    235 
    236   Handle<Object> eval_from_script_obj =
    237       handle(EvalFromScript(isolate, script), isolate);
    238   if (eval_from_script_obj->IsScript()) {
    239     Handle<Script> eval_from_script =
    240         Handle<Script>::cast(eval_from_script_obj);
    241     builder.AppendCString(" (");
    242     if (eval_from_script->compilation_type() == Script::COMPILATION_TYPE_EVAL) {
    243       // Eval script originated from another eval.
    244       Handle<String> str;
    245       ASSIGN_RETURN_ON_EXCEPTION(
    246           isolate, str, FormatEvalOrigin(isolate, eval_from_script), String);
    247       builder.AppendString(str);
    248     } else {
    249       DCHECK(eval_from_script->compilation_type() !=
    250              Script::COMPILATION_TYPE_EVAL);
    251       // eval script originated from "real" source.
    252       Handle<Object> name_obj = handle(eval_from_script->name(), isolate);
    253       if (eval_from_script->name()->IsString()) {
    254         builder.AppendString(Handle<String>::cast(name_obj));
    255 
    256         Script::PositionInfo info;
    257         if (Script::GetPositionInfo(eval_from_script, script->GetEvalPosition(),
    258                                     &info, Script::NO_OFFSET)) {
    259           builder.AppendCString(":");
    260 
    261           Handle<String> str = isolate->factory()->NumberToString(
    262               handle(Smi::FromInt(info.line + 1), isolate));
    263           builder.AppendString(str);
    264 
    265           builder.AppendCString(":");
    266 
    267           str = isolate->factory()->NumberToString(
    268               handle(Smi::FromInt(info.column + 1), isolate));
    269           builder.AppendString(str);
    270         }
    271       } else {
    272         DCHECK(!eval_from_script->name()->IsString());
    273         builder.AppendCString("unknown source");
    274       }
    275     }
    276     builder.AppendCString(")");
    277   }
    278 
    279   Handle<String> result;
    280   ASSIGN_RETURN_ON_EXCEPTION(isolate, result, builder.Finish(), String);
    281   return result;
    282 }
    283 
    284 }  // namespace
    285 
    286 Handle<Object> StackFrameBase::GetEvalOrigin() {
    287   if (!HasScript()) return isolate_->factory()->undefined_value();
    288   return FormatEvalOrigin(isolate_, GetScript()).ToHandleChecked();
    289 }
    290 
    291 bool StackFrameBase::IsEval() {
    292   return HasScript() &&
    293          GetScript()->compilation_type() == Script::COMPILATION_TYPE_EVAL;
    294 }
    295 
    296 void JSStackFrame::FromFrameArray(Isolate* isolate, Handle<FrameArray> array,
    297                                   int frame_ix) {
    298   DCHECK(!array->IsWasmFrame(frame_ix));
    299   isolate_ = isolate;
    300   receiver_ = handle(array->Receiver(frame_ix), isolate);
    301   function_ = handle(array->Function(frame_ix), isolate);
    302   code_ = handle(array->Code(frame_ix), isolate);
    303   offset_ = array->Offset(frame_ix)->value();
    304 
    305   const int flags = array->Flags(frame_ix)->value();
    306   is_constructor_ = (flags & FrameArray::kIsConstructor) != 0;
    307   is_strict_ = (flags & FrameArray::kIsStrict) != 0;
    308 }
    309 
    310 JSStackFrame::JSStackFrame() {}
    311 
    312 JSStackFrame::JSStackFrame(Isolate* isolate, Handle<Object> receiver,
    313                            Handle<JSFunction> function,
    314                            Handle<AbstractCode> code, int offset)
    315     : StackFrameBase(isolate),
    316       receiver_(receiver),
    317       function_(function),
    318       code_(code),
    319       offset_(offset),
    320       is_constructor_(false),
    321       is_strict_(false) {}
    322 
    323 Handle<Object> JSStackFrame::GetFunction() const {
    324   return Handle<Object>::cast(function_);
    325 }
    326 
    327 Handle<Object> JSStackFrame::GetFileName() {
    328   if (!HasScript()) return isolate_->factory()->null_value();
    329   return handle(GetScript()->name(), isolate_);
    330 }
    331 
    332 Handle<Object> JSStackFrame::GetFunctionName() {
    333   Handle<String> result = JSFunction::GetName(function_);
    334   if (result->length() != 0) return result;
    335 
    336   if (HasScript() &&
    337       GetScript()->compilation_type() == Script::COMPILATION_TYPE_EVAL) {
    338     return isolate_->factory()->eval_string();
    339   }
    340   return isolate_->factory()->null_value();
    341 }
    342 
    343 namespace {
    344 
    345 bool CheckMethodName(Isolate* isolate, Handle<JSReceiver> receiver,
    346                      Handle<Name> name, Handle<JSFunction> fun,
    347                      LookupIterator::Configuration config) {
    348   LookupIterator iter =
    349       LookupIterator::PropertyOrElement(isolate, receiver, name, config);
    350   if (iter.state() == LookupIterator::DATA) {
    351     return iter.GetDataValue().is_identical_to(fun);
    352   } else if (iter.state() == LookupIterator::ACCESSOR) {
    353     Handle<Object> accessors = iter.GetAccessors();
    354     if (accessors->IsAccessorPair()) {
    355       Handle<AccessorPair> pair = Handle<AccessorPair>::cast(accessors);
    356       return pair->getter() == *fun || pair->setter() == *fun;
    357     }
    358   }
    359   return false;
    360 }
    361 
    362 Handle<Object> ScriptNameOrSourceUrl(Handle<Script> script, Isolate* isolate) {
    363   Object* name_or_url = script->source_url();
    364   if (!name_or_url->IsString()) name_or_url = script->name();
    365   return handle(name_or_url, isolate);
    366 }
    367 
    368 }  // namespace
    369 
    370 Handle<Object> JSStackFrame::GetScriptNameOrSourceUrl() {
    371   if (!HasScript()) return isolate_->factory()->null_value();
    372   return ScriptNameOrSourceUrl(GetScript(), isolate_);
    373 }
    374 
    375 Handle<Object> JSStackFrame::GetMethodName() {
    376   if (receiver_->IsNullOrUndefined(isolate_)) {
    377     return isolate_->factory()->null_value();
    378   }
    379 
    380   Handle<JSReceiver> receiver;
    381   if (!Object::ToObject(isolate_, receiver_).ToHandle(&receiver)) {
    382     DCHECK(isolate_->has_pending_exception());
    383     isolate_->clear_pending_exception();
    384     isolate_->set_external_caught_exception(false);
    385     return isolate_->factory()->null_value();
    386   }
    387 
    388   Handle<String> name(function_->shared()->Name(), isolate_);
    389   // ES2015 gives getters and setters name prefixes which must
    390   // be stripped to find the property name.
    391   if (name->IsUtf8EqualTo(CStrVector("get "), true) ||
    392       name->IsUtf8EqualTo(CStrVector("set "), true)) {
    393     name = isolate_->factory()->NewProperSubString(name, 4, name->length());
    394   }
    395   if (CheckMethodName(isolate_, receiver, name, function_,
    396                       LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR)) {
    397     return name;
    398   }
    399 
    400   HandleScope outer_scope(isolate_);
    401   Handle<Object> result;
    402   for (PrototypeIterator iter(isolate_, receiver, kStartAtReceiver);
    403        !iter.IsAtEnd(); iter.Advance()) {
    404     Handle<Object> current = PrototypeIterator::GetCurrent(iter);
    405     if (!current->IsJSObject()) break;
    406     Handle<JSObject> current_obj = Handle<JSObject>::cast(current);
    407     if (current_obj->IsAccessCheckNeeded()) break;
    408     Handle<FixedArray> keys =
    409         KeyAccumulator::GetOwnEnumPropertyKeys(isolate_, current_obj);
    410     for (int i = 0; i < keys->length(); i++) {
    411       HandleScope inner_scope(isolate_);
    412       if (!keys->get(i)->IsName()) continue;
    413       Handle<Name> name_key(Name::cast(keys->get(i)), isolate_);
    414       if (!CheckMethodName(isolate_, current_obj, name_key, function_,
    415                            LookupIterator::OWN_SKIP_INTERCEPTOR))
    416         continue;
    417       // Return null in case of duplicates to avoid confusion.
    418       if (!result.is_null()) return isolate_->factory()->null_value();
    419       result = inner_scope.CloseAndEscape(name_key);
    420     }
    421   }
    422 
    423   if (!result.is_null()) return outer_scope.CloseAndEscape(result);
    424   return isolate_->factory()->null_value();
    425 }
    426 
    427 Handle<Object> JSStackFrame::GetTypeName() {
    428   // TODO(jgruber): Check for strict/constructor here as in
    429   // CallSitePrototypeGetThis.
    430 
    431   if (receiver_->IsNullOrUndefined(isolate_)) {
    432     return isolate_->factory()->null_value();
    433   } else if (receiver_->IsJSProxy()) {
    434     return isolate_->factory()->Proxy_string();
    435   }
    436 
    437   Handle<JSReceiver> receiver;
    438   if (!Object::ToObject(isolate_, receiver_).ToHandle(&receiver)) {
    439     DCHECK(isolate_->has_pending_exception());
    440     isolate_->clear_pending_exception();
    441     isolate_->set_external_caught_exception(false);
    442     return isolate_->factory()->null_value();
    443   }
    444 
    445   return JSReceiver::GetConstructorName(receiver);
    446 }
    447 
    448 int JSStackFrame::GetLineNumber() {
    449   DCHECK_LE(0, GetPosition());
    450   if (HasScript()) return Script::GetLineNumber(GetScript(), GetPosition()) + 1;
    451   return -1;
    452 }
    453 
    454 int JSStackFrame::GetColumnNumber() {
    455   DCHECK_LE(0, GetPosition());
    456   if (HasScript()) {
    457     return Script::GetColumnNumber(GetScript(), GetPosition()) + 1;
    458   }
    459   return -1;
    460 }
    461 
    462 bool JSStackFrame::IsNative() {
    463   return HasScript() && GetScript()->type() == Script::TYPE_NATIVE;
    464 }
    465 
    466 bool JSStackFrame::IsToplevel() {
    467   return receiver_->IsJSGlobalProxy() || receiver_->IsNullOrUndefined(isolate_);
    468 }
    469 
    470 namespace {
    471 
    472 bool IsNonEmptyString(Handle<Object> object) {
    473   return (object->IsString() && String::cast(*object)->length() > 0);
    474 }
    475 
    476 void AppendFileLocation(Isolate* isolate, StackFrameBase* call_site,
    477                         IncrementalStringBuilder* builder) {
    478   if (call_site->IsNative()) {
    479     builder->AppendCString("native");
    480     return;
    481   }
    482 
    483   Handle<Object> file_name = call_site->GetScriptNameOrSourceUrl();
    484   if (!file_name->IsString() && call_site->IsEval()) {
    485     Handle<Object> eval_origin = call_site->GetEvalOrigin();
    486     DCHECK(eval_origin->IsString());
    487     builder->AppendString(Handle<String>::cast(eval_origin));
    488     builder->AppendCString(", ");  // Expecting source position to follow.
    489   }
    490 
    491   if (IsNonEmptyString(file_name)) {
    492     builder->AppendString(Handle<String>::cast(file_name));
    493   } else {
    494     // Source code does not originate from a file and is not native, but we
    495     // can still get the source position inside the source string, e.g. in
    496     // an eval string.
    497     builder->AppendCString("<anonymous>");
    498   }
    499 
    500   int line_number = call_site->GetLineNumber();
    501   if (line_number != -1) {
    502     builder->AppendCharacter(':');
    503     Handle<String> line_string = isolate->factory()->NumberToString(
    504         handle(Smi::FromInt(line_number), isolate), isolate);
    505     builder->AppendString(line_string);
    506 
    507     int column_number = call_site->GetColumnNumber();
    508     if (column_number != -1) {
    509       builder->AppendCharacter(':');
    510       Handle<String> column_string = isolate->factory()->NumberToString(
    511           handle(Smi::FromInt(column_number), isolate), isolate);
    512       builder->AppendString(column_string);
    513     }
    514   }
    515 }
    516 
    517 int StringIndexOf(Isolate* isolate, Handle<String> subject,
    518                   Handle<String> pattern) {
    519   if (pattern->length() > subject->length()) return -1;
    520   return String::IndexOf(isolate, subject, pattern, 0);
    521 }
    522 
    523 // Returns true iff
    524 // 1. the subject ends with '.' + pattern, or
    525 // 2. subject == pattern.
    526 bool StringEndsWithMethodName(Isolate* isolate, Handle<String> subject,
    527                               Handle<String> pattern) {
    528   if (String::Equals(isolate, subject, pattern)) return true;
    529 
    530   FlatStringReader subject_reader(isolate, String::Flatten(isolate, subject));
    531   FlatStringReader pattern_reader(isolate, String::Flatten(isolate, pattern));
    532 
    533   int pattern_index = pattern_reader.length() - 1;
    534   int subject_index = subject_reader.length() - 1;
    535   for (int i = 0; i <= pattern_reader.length(); i++) {  // Iterate over len + 1.
    536     if (subject_index < 0) {
    537       return false;
    538     }
    539 
    540     const uc32 subject_char = subject_reader.Get(subject_index);
    541     if (i == pattern_reader.length()) {
    542       if (subject_char != '.') return false;
    543     } else if (subject_char != pattern_reader.Get(pattern_index)) {
    544       return false;
    545     }
    546 
    547     pattern_index--;
    548     subject_index--;
    549   }
    550 
    551   return true;
    552 }
    553 
    554 void AppendMethodCall(Isolate* isolate, JSStackFrame* call_site,
    555                       IncrementalStringBuilder* builder) {
    556   Handle<Object> type_name = call_site->GetTypeName();
    557   Handle<Object> method_name = call_site->GetMethodName();
    558   Handle<Object> function_name = call_site->GetFunctionName();
    559 
    560   if (IsNonEmptyString(function_name)) {
    561     Handle<String> function_string = Handle<String>::cast(function_name);
    562     if (IsNonEmptyString(type_name)) {
    563       Handle<String> type_string = Handle<String>::cast(type_name);
    564       bool starts_with_type_name =
    565           (StringIndexOf(isolate, function_string, type_string) == 0);
    566       if (!starts_with_type_name) {
    567         builder->AppendString(type_string);
    568         builder->AppendCharacter('.');
    569       }
    570     }
    571     builder->AppendString(function_string);
    572 
    573     if (IsNonEmptyString(method_name)) {
    574       Handle<String> method_string = Handle<String>::cast(method_name);
    575       if (!StringEndsWithMethodName(isolate, function_string, method_string)) {
    576         builder->AppendCString(" [as ");
    577         builder->AppendString(method_string);
    578         builder->AppendCharacter(']');
    579       }
    580     }
    581   } else {
    582     if (IsNonEmptyString(type_name)) {
    583       builder->AppendString(Handle<String>::cast(type_name));
    584       builder->AppendCharacter('.');
    585     }
    586     if (IsNonEmptyString(method_name)) {
    587       builder->AppendString(Handle<String>::cast(method_name));
    588     } else {
    589       builder->AppendCString("<anonymous>");
    590     }
    591   }
    592 }
    593 
    594 }  // namespace
    595 
    596 MaybeHandle<String> JSStackFrame::ToString() {
    597   IncrementalStringBuilder builder(isolate_);
    598 
    599   Handle<Object> function_name = GetFunctionName();
    600 
    601   const bool is_toplevel = IsToplevel();
    602   const bool is_constructor = IsConstructor();
    603   const bool is_method_call = !(is_toplevel || is_constructor);
    604 
    605   if (is_method_call) {
    606     AppendMethodCall(isolate_, this, &builder);
    607   } else if (is_constructor) {
    608     builder.AppendCString("new ");
    609     if (IsNonEmptyString(function_name)) {
    610       builder.AppendString(Handle<String>::cast(function_name));
    611     } else {
    612       builder.AppendCString("<anonymous>");
    613     }
    614   } else if (IsNonEmptyString(function_name)) {
    615     builder.AppendString(Handle<String>::cast(function_name));
    616   } else {
    617     AppendFileLocation(isolate_, this, &builder);
    618     return builder.Finish();
    619   }
    620 
    621   builder.AppendCString(" (");
    622   AppendFileLocation(isolate_, this, &builder);
    623   builder.AppendCString(")");
    624 
    625   return builder.Finish();
    626 }
    627 
    628 int JSStackFrame::GetPosition() const { return code_->SourcePosition(offset_); }
    629 
    630 bool JSStackFrame::HasScript() const {
    631   return function_->shared()->script()->IsScript();
    632 }
    633 
    634 Handle<Script> JSStackFrame::GetScript() const {
    635   return handle(Script::cast(function_->shared()->script()), isolate_);
    636 }
    637 
    638 WasmStackFrame::WasmStackFrame() {}
    639 
    640 void WasmStackFrame::FromFrameArray(Isolate* isolate, Handle<FrameArray> array,
    641                                     int frame_ix) {
    642   // This function is called for compiled and interpreted wasm frames, and for
    643   // asm.js->wasm frames.
    644   DCHECK(array->IsWasmFrame(frame_ix) ||
    645          array->IsWasmInterpretedFrame(frame_ix) ||
    646          array->IsAsmJsWasmFrame(frame_ix));
    647   isolate_ = isolate;
    648   wasm_instance_ = handle(array->WasmInstance(frame_ix), isolate);
    649   wasm_func_index_ = array->WasmFunctionIndex(frame_ix)->value();
    650   if (array->IsWasmInterpretedFrame(frame_ix)) {
    651     code_ = nullptr;
    652   } else {
    653     code_ = reinterpret_cast<wasm::WasmCode*>(
    654         array->WasmCodeObject(frame_ix)->foreign_address());
    655   }
    656   offset_ = array->Offset(frame_ix)->value();
    657 }
    658 
    659 Handle<Object> WasmStackFrame::GetReceiver() const { return wasm_instance_; }
    660 
    661 Handle<Object> WasmStackFrame::GetFunction() const {
    662   return handle(Smi::FromInt(wasm_func_index_), isolate_);
    663 }
    664 
    665 Handle<Object> WasmStackFrame::GetFunctionName() {
    666   Handle<Object> name;
    667   Handle<WasmModuleObject> module_object(wasm_instance_->module_object(),
    668                                          isolate_);
    669   if (!WasmModuleObject::GetFunctionNameOrNull(isolate_, module_object,
    670                                                wasm_func_index_)
    671            .ToHandle(&name)) {
    672     name = isolate_->factory()->null_value();
    673   }
    674   return name;
    675 }
    676 
    677 MaybeHandle<String> WasmStackFrame::ToString() {
    678   IncrementalStringBuilder builder(isolate_);
    679 
    680   Handle<WasmModuleObject> module_object(wasm_instance_->module_object(),
    681                                          isolate_);
    682   MaybeHandle<String> module_name =
    683       WasmModuleObject::GetModuleNameOrNull(isolate_, module_object);
    684   MaybeHandle<String> function_name = WasmModuleObject::GetFunctionNameOrNull(
    685       isolate_, module_object, wasm_func_index_);
    686   bool has_name = !module_name.is_null() || !function_name.is_null();
    687   if (has_name) {
    688     if (module_name.is_null()) {
    689       builder.AppendString(function_name.ToHandleChecked());
    690     } else {
    691       builder.AppendString(module_name.ToHandleChecked());
    692       if (!function_name.is_null()) {
    693         builder.AppendCString(".");
    694         builder.AppendString(function_name.ToHandleChecked());
    695       }
    696     }
    697     builder.AppendCString(" (");
    698   }
    699 
    700   builder.AppendCString("wasm-function[");
    701 
    702   char buffer[16];
    703   SNPrintF(ArrayVector(buffer), "%u]", wasm_func_index_);
    704   builder.AppendCString(buffer);
    705 
    706   SNPrintF(ArrayVector(buffer), ":%d", GetPosition());
    707   builder.AppendCString(buffer);
    708 
    709   if (has_name) builder.AppendCString(")");
    710 
    711   return builder.Finish();
    712 }
    713 
    714 int WasmStackFrame::GetPosition() const {
    715   return IsInterpreted()
    716              ? offset_
    717              : FrameSummary::WasmCompiledFrameSummary::GetWasmSourcePosition(
    718                    code_, offset_);
    719 }
    720 
    721 Handle<Object> WasmStackFrame::Null() const {
    722   return isolate_->factory()->null_value();
    723 }
    724 
    725 bool WasmStackFrame::HasScript() const { return true; }
    726 
    727 Handle<Script> WasmStackFrame::GetScript() const {
    728   return handle(wasm_instance_->module_object()->script(), isolate_);
    729 }
    730 
    731 AsmJsWasmStackFrame::AsmJsWasmStackFrame() {}
    732 
    733 void AsmJsWasmStackFrame::FromFrameArray(Isolate* isolate,
    734                                          Handle<FrameArray> array,
    735                                          int frame_ix) {
    736   DCHECK(array->IsAsmJsWasmFrame(frame_ix));
    737   WasmStackFrame::FromFrameArray(isolate, array, frame_ix);
    738   is_at_number_conversion_ =
    739       array->Flags(frame_ix)->value() & FrameArray::kAsmJsAtNumberConversion;
    740 }
    741 
    742 Handle<Object> AsmJsWasmStackFrame::GetReceiver() const {
    743   return isolate_->global_proxy();
    744 }
    745 
    746 Handle<Object> AsmJsWasmStackFrame::GetFunction() const {
    747   // TODO(clemensh): Return lazily created JSFunction.
    748   return Null();
    749 }
    750 
    751 Handle<Object> AsmJsWasmStackFrame::GetFileName() {
    752   Handle<Script> script(wasm_instance_->module_object()->script(), isolate_);
    753   DCHECK(script->IsUserJavaScript());
    754   return handle(script->name(), isolate_);
    755 }
    756 
    757 Handle<Object> AsmJsWasmStackFrame::GetScriptNameOrSourceUrl() {
    758   Handle<Script> script(wasm_instance_->module_object()->script(), isolate_);
    759   DCHECK_EQ(Script::TYPE_NORMAL, script->type());
    760   return ScriptNameOrSourceUrl(script, isolate_);
    761 }
    762 
    763 int AsmJsWasmStackFrame::GetPosition() const {
    764   DCHECK_LE(0, offset_);
    765   int byte_offset =
    766       FrameSummary::WasmCompiledFrameSummary::GetWasmSourcePosition(code_,
    767                                                                     offset_);
    768   Handle<WasmModuleObject> module_object(wasm_instance_->module_object(),
    769                                          isolate_);
    770   DCHECK_LE(0, byte_offset);
    771   return WasmModuleObject::GetSourcePosition(module_object, wasm_func_index_,
    772                                              static_cast<uint32_t>(byte_offset),
    773                                              is_at_number_conversion_);
    774 }
    775 
    776 int AsmJsWasmStackFrame::GetLineNumber() {
    777   DCHECK_LE(0, GetPosition());
    778   Handle<Script> script(wasm_instance_->module_object()->script(), isolate_);
    779   DCHECK(script->IsUserJavaScript());
    780   return Script::GetLineNumber(script, GetPosition()) + 1;
    781 }
    782 
    783 int AsmJsWasmStackFrame::GetColumnNumber() {
    784   DCHECK_LE(0, GetPosition());
    785   Handle<Script> script(wasm_instance_->module_object()->script(), isolate_);
    786   DCHECK(script->IsUserJavaScript());
    787   return Script::GetColumnNumber(script, GetPosition()) + 1;
    788 }
    789 
    790 MaybeHandle<String> AsmJsWasmStackFrame::ToString() {
    791   // The string should look exactly as the respective javascript frame string.
    792   // Keep this method in line to JSStackFrame::ToString().
    793 
    794   IncrementalStringBuilder builder(isolate_);
    795 
    796   Handle<Object> function_name = GetFunctionName();
    797 
    798   if (IsNonEmptyString(function_name)) {
    799     builder.AppendString(Handle<String>::cast(function_name));
    800     builder.AppendCString(" (");
    801   }
    802 
    803   AppendFileLocation(isolate_, this, &builder);
    804 
    805   if (IsNonEmptyString(function_name)) builder.AppendCString(")");
    806 
    807   return builder.Finish();
    808 }
    809 
    810 FrameArrayIterator::FrameArrayIterator(Isolate* isolate,
    811                                        Handle<FrameArray> array, int frame_ix)
    812     : isolate_(isolate), array_(array), next_frame_ix_(frame_ix) {}
    813 
    814 bool FrameArrayIterator::HasNext() const {
    815   return (next_frame_ix_ < array_->FrameCount());
    816 }
    817 
    818 void FrameArrayIterator::Next() { next_frame_ix_++; }
    819 
    820 StackFrameBase* FrameArrayIterator::Frame() {
    821   DCHECK(HasNext());
    822   const int flags = array_->Flags(next_frame_ix_)->value();
    823   int flag_mask = FrameArray::kIsWasmFrame |
    824                   FrameArray::kIsWasmInterpretedFrame |
    825                   FrameArray::kIsAsmJsWasmFrame;
    826   switch (flags & flag_mask) {
    827     case 0:
    828       // JavaScript Frame.
    829       js_frame_.FromFrameArray(isolate_, array_, next_frame_ix_);
    830       return &js_frame_;
    831     case FrameArray::kIsWasmFrame:
    832     case FrameArray::kIsWasmInterpretedFrame:
    833       // Wasm Frame:
    834       wasm_frame_.FromFrameArray(isolate_, array_, next_frame_ix_);
    835       return &wasm_frame_;
    836     case FrameArray::kIsAsmJsWasmFrame:
    837       // Asm.js Wasm Frame:
    838       asm_wasm_frame_.FromFrameArray(isolate_, array_, next_frame_ix_);
    839       return &asm_wasm_frame_;
    840     default:
    841       UNREACHABLE();
    842   }
    843 }
    844 
    845 namespace {
    846 
    847 MaybeHandle<Object> ConstructCallSite(Isolate* isolate,
    848                                       Handle<FrameArray> frame_array,
    849                                       int frame_index) {
    850   Handle<JSFunction> target =
    851       handle(isolate->native_context()->callsite_function(), isolate);
    852 
    853   Handle<JSObject> obj;
    854   ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, JSObject::New(target, target),
    855                              Object);
    856 
    857   Handle<Symbol> key = isolate->factory()->call_site_frame_array_symbol();
    858   RETURN_ON_EXCEPTION(isolate, JSObject::SetOwnPropertyIgnoreAttributes(
    859                                    obj, key, frame_array, DONT_ENUM),
    860                       Object);
    861 
    862   key = isolate->factory()->call_site_frame_index_symbol();
    863   Handle<Object> value(Smi::FromInt(frame_index), isolate);
    864   RETURN_ON_EXCEPTION(isolate, JSObject::SetOwnPropertyIgnoreAttributes(
    865                                    obj, key, value, DONT_ENUM),
    866                       Object);
    867 
    868   return obj;
    869 }
    870 
    871 // Convert the raw frames as written by Isolate::CaptureSimpleStackTrace into
    872 // a JSArray of JSCallSite objects.
    873 MaybeHandle<JSArray> GetStackFrames(Isolate* isolate,
    874                                     Handle<FrameArray> elems) {
    875   const int frame_count = elems->FrameCount();
    876 
    877   Handle<FixedArray> frames = isolate->factory()->NewFixedArray(frame_count);
    878   for (int i = 0; i < frame_count; i++) {
    879     Handle<Object> site;
    880     ASSIGN_RETURN_ON_EXCEPTION(isolate, site,
    881                                ConstructCallSite(isolate, elems, i), JSArray);
    882     frames->set(i, *site);
    883   }
    884 
    885   return isolate->factory()->NewJSArrayWithElements(frames);
    886 }
    887 
    888 MaybeHandle<Object> AppendErrorString(Isolate* isolate, Handle<Object> error,
    889                                       IncrementalStringBuilder* builder) {
    890   MaybeHandle<String> err_str =
    891       ErrorUtils::ToString(isolate, Handle<Object>::cast(error));
    892   if (err_str.is_null()) {
    893     // Error.toString threw. Try to return a string representation of the thrown
    894     // exception instead.
    895 
    896     DCHECK(isolate->has_pending_exception());
    897     Handle<Object> pending_exception =
    898         handle(isolate->pending_exception(), isolate);
    899     isolate->clear_pending_exception();
    900     isolate->set_external_caught_exception(false);
    901 
    902     err_str = ErrorUtils::ToString(isolate, pending_exception);
    903     if (err_str.is_null()) {
    904       // Formatting the thrown exception threw again, give up.
    905       DCHECK(isolate->has_pending_exception());
    906       isolate->clear_pending_exception();
    907       isolate->set_external_caught_exception(false);
    908       builder->AppendCString("<error>");
    909     } else {
    910       // Formatted thrown exception successfully, append it.
    911       builder->AppendCString("<error: ");
    912       builder->AppendString(err_str.ToHandleChecked());
    913       builder->AppendCharacter('>');
    914     }
    915   } else {
    916     builder->AppendString(err_str.ToHandleChecked());
    917   }
    918 
    919   return error;
    920 }
    921 
    922 class PrepareStackTraceScope {
    923  public:
    924   explicit PrepareStackTraceScope(Isolate* isolate) : isolate_(isolate) {
    925     DCHECK(!isolate_->formatting_stack_trace());
    926     isolate_->set_formatting_stack_trace(true);
    927   }
    928 
    929   ~PrepareStackTraceScope() { isolate_->set_formatting_stack_trace(false); }
    930 
    931  private:
    932   Isolate* isolate_;
    933 
    934   DISALLOW_COPY_AND_ASSIGN(PrepareStackTraceScope);
    935 };
    936 
    937 }  // namespace
    938 
    939 // static
    940 MaybeHandle<Object> ErrorUtils::FormatStackTrace(Isolate* isolate,
    941                                                  Handle<JSObject> error,
    942                                                  Handle<Object> raw_stack) {
    943   DCHECK(raw_stack->IsJSArray());
    944   Handle<JSArray> raw_stack_array = Handle<JSArray>::cast(raw_stack);
    945 
    946   DCHECK(raw_stack_array->elements()->IsFixedArray());
    947   Handle<FrameArray> elems(FrameArray::cast(raw_stack_array->elements()),
    948                            isolate);
    949 
    950   // If there's a user-specified "prepareStackFrames" function, call it on the
    951   // frames and use its result.
    952 
    953   Handle<JSFunction> global_error = isolate->error_function();
    954   Handle<Object> prepare_stack_trace;
    955   ASSIGN_RETURN_ON_EXCEPTION(
    956       isolate, prepare_stack_trace,
    957       JSFunction::GetProperty(isolate, global_error, "prepareStackTrace"),
    958       Object);
    959 
    960   const bool in_recursion = isolate->formatting_stack_trace();
    961   if (prepare_stack_trace->IsJSFunction() && !in_recursion) {
    962     PrepareStackTraceScope scope(isolate);
    963 
    964     isolate->CountUsage(v8::Isolate::kErrorPrepareStackTrace);
    965 
    966     Handle<JSArray> sites;
    967     ASSIGN_RETURN_ON_EXCEPTION(isolate, sites, GetStackFrames(isolate, elems),
    968                                Object);
    969 
    970     const int argc = 2;
    971     ScopedVector<Handle<Object>> argv(argc);
    972 
    973     argv[0] = error;
    974     argv[1] = sites;
    975 
    976     Handle<Object> result;
    977     ASSIGN_RETURN_ON_EXCEPTION(
    978         isolate, result, Execution::Call(isolate, prepare_stack_trace,
    979                                          global_error, argc, argv.start()),
    980         Object);
    981 
    982     return result;
    983   }
    984 
    985   // Otherwise, run our internal formatting logic.
    986 
    987   IncrementalStringBuilder builder(isolate);
    988 
    989   RETURN_ON_EXCEPTION(isolate, AppendErrorString(isolate, error, &builder),
    990                       Object);
    991 
    992   for (FrameArrayIterator it(isolate, elems); it.HasNext(); it.Next()) {
    993     builder.AppendCString("\n    at ");
    994 
    995     StackFrameBase* frame = it.Frame();
    996     MaybeHandle<String> maybe_frame_string = frame->ToString();
    997     if (maybe_frame_string.is_null()) {
    998       // CallSite.toString threw. Try to return a string representation of the
    999       // thrown exception instead.
   1000 
   1001       DCHECK(isolate->has_pending_exception());
   1002       Handle<Object> pending_exception =
   1003           handle(isolate->pending_exception(), isolate);
   1004       isolate->clear_pending_exception();
   1005       isolate->set_external_caught_exception(false);
   1006 
   1007       maybe_frame_string = ErrorUtils::ToString(isolate, pending_exception);
   1008       if (maybe_frame_string.is_null()) {
   1009         // Formatting the thrown exception threw again, give up.
   1010 
   1011         builder.AppendCString("<error>");
   1012       } else {
   1013         // Formatted thrown exception successfully, append it.
   1014         builder.AppendCString("<error: ");
   1015         builder.AppendString(maybe_frame_string.ToHandleChecked());
   1016         builder.AppendCString("<error>");
   1017       }
   1018     } else {
   1019       // CallSite.toString completed without throwing.
   1020       builder.AppendString(maybe_frame_string.ToHandleChecked());
   1021     }
   1022   }
   1023 
   1024   return builder.Finish();
   1025 }
   1026 
   1027 Handle<String> MessageTemplate::FormatMessage(Isolate* isolate,
   1028                                               int template_index,
   1029                                               Handle<Object> arg) {
   1030   Factory* factory = isolate->factory();
   1031   Handle<String> result_string = Object::NoSideEffectsToString(isolate, arg);
   1032   MaybeHandle<String> maybe_result_string = MessageTemplate::FormatMessage(
   1033       isolate, template_index, result_string, factory->empty_string(),
   1034       factory->empty_string());
   1035   if (!maybe_result_string.ToHandle(&result_string)) {
   1036     DCHECK(isolate->has_pending_exception());
   1037     isolate->clear_pending_exception();
   1038     return factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("<error>"));
   1039   }
   1040   // A string that has been obtained from JS code in this way is
   1041   // likely to be a complicated ConsString of some sort.  We flatten it
   1042   // here to improve the efficiency of converting it to a C string and
   1043   // other operations that are likely to take place (see GetLocalizedMessage
   1044   // for example).
   1045   return String::Flatten(isolate, result_string);
   1046 }
   1047 
   1048 
   1049 const char* MessageTemplate::TemplateString(int template_index) {
   1050   switch (template_index) {
   1051 #define CASE(NAME, STRING) \
   1052   case k##NAME:            \
   1053     return STRING;
   1054     MESSAGE_TEMPLATES(CASE)
   1055 #undef CASE
   1056     case kLastMessage:
   1057     default:
   1058       return nullptr;
   1059   }
   1060 }
   1061 
   1062 MaybeHandle<String> MessageTemplate::FormatMessage(Isolate* isolate,
   1063                                                    int template_index,
   1064                                                    Handle<String> arg0,
   1065                                                    Handle<String> arg1,
   1066                                                    Handle<String> arg2) {
   1067   const char* template_string = TemplateString(template_index);
   1068   if (template_string == nullptr) {
   1069     isolate->ThrowIllegalOperation();
   1070     return MaybeHandle<String>();
   1071   }
   1072 
   1073   IncrementalStringBuilder builder(isolate);
   1074 
   1075   unsigned int i = 0;
   1076   Handle<String> args[] = {arg0, arg1, arg2};
   1077   for (const char* c = template_string; *c != '\0'; c++) {
   1078     if (*c == '%') {
   1079       // %% results in verbatim %.
   1080       if (*(c + 1) == '%') {
   1081         c++;
   1082         builder.AppendCharacter('%');
   1083       } else {
   1084         DCHECK(i < arraysize(args));
   1085         Handle<String> arg = args[i++];
   1086         builder.AppendString(arg);
   1087       }
   1088     } else {
   1089       builder.AppendCharacter(*c);
   1090     }
   1091   }
   1092 
   1093   return builder.Finish();
   1094 }
   1095 
   1096 MaybeHandle<Object> ErrorUtils::Construct(
   1097     Isolate* isolate, Handle<JSFunction> target, Handle<Object> new_target,
   1098     Handle<Object> message, FrameSkipMode mode, Handle<Object> caller,
   1099     bool suppress_detailed_trace) {
   1100   // 1. If NewTarget is undefined, let newTarget be the active function object,
   1101   // else let newTarget be NewTarget.
   1102 
   1103   Handle<JSReceiver> new_target_recv =
   1104       new_target->IsJSReceiver() ? Handle<JSReceiver>::cast(new_target)
   1105                                  : Handle<JSReceiver>::cast(target);
   1106 
   1107   // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%ErrorPrototype%",
   1108   //     [[ErrorData]] ).
   1109   Handle<JSObject> err;
   1110   ASSIGN_RETURN_ON_EXCEPTION(isolate, err,
   1111                              JSObject::New(target, new_target_recv), Object);
   1112 
   1113   // 3. If message is not undefined, then
   1114   //  a. Let msg be ? ToString(message).
   1115   //  b. Let msgDesc be the PropertyDescriptor{[[Value]]: msg, [[Writable]]:
   1116   //     true, [[Enumerable]]: false, [[Configurable]]: true}.
   1117   //  c. Perform ! DefinePropertyOrThrow(O, "message", msgDesc).
   1118   // 4. Return O.
   1119 
   1120   if (!message->IsUndefined(isolate)) {
   1121     Handle<String> msg_string;
   1122     ASSIGN_RETURN_ON_EXCEPTION(isolate, msg_string,
   1123                                Object::ToString(isolate, message), Object);
   1124     RETURN_ON_EXCEPTION(isolate, JSObject::SetOwnPropertyIgnoreAttributes(
   1125                                      err, isolate->factory()->message_string(),
   1126                                      msg_string, DONT_ENUM),
   1127                         Object);
   1128   }
   1129 
   1130   // Optionally capture a more detailed stack trace for the message.
   1131   if (!suppress_detailed_trace) {
   1132     RETURN_ON_EXCEPTION(isolate, isolate->CaptureAndSetDetailedStackTrace(err),
   1133                         Object);
   1134   }
   1135 
   1136   // Capture a simple stack trace for the stack property.
   1137   RETURN_ON_EXCEPTION(isolate,
   1138                       isolate->CaptureAndSetSimpleStackTrace(err, mode, caller),
   1139                       Object);
   1140 
   1141   return err;
   1142 }
   1143 
   1144 namespace {
   1145 
   1146 MaybeHandle<String> GetStringPropertyOrDefault(Isolate* isolate,
   1147                                                Handle<JSReceiver> recv,
   1148                                                Handle<String> key,
   1149                                                Handle<String> default_str) {
   1150   Handle<Object> obj;
   1151   ASSIGN_RETURN_ON_EXCEPTION(isolate, obj,
   1152                              JSObject::GetProperty(isolate, recv, key), String);
   1153 
   1154   Handle<String> str;
   1155   if (obj->IsUndefined(isolate)) {
   1156     str = default_str;
   1157   } else {
   1158     ASSIGN_RETURN_ON_EXCEPTION(isolate, str, Object::ToString(isolate, obj),
   1159                                String);
   1160   }
   1161 
   1162   return str;
   1163 }
   1164 
   1165 }  // namespace
   1166 
   1167 // ES6 section 19.5.3.4 Error.prototype.toString ( )
   1168 MaybeHandle<String> ErrorUtils::ToString(Isolate* isolate,
   1169                                          Handle<Object> receiver) {
   1170   // 1. Let O be the this value.
   1171   // 2. If Type(O) is not Object, throw a TypeError exception.
   1172   if (!receiver->IsJSReceiver()) {
   1173     return isolate->Throw<String>(isolate->factory()->NewTypeError(
   1174         MessageTemplate::kIncompatibleMethodReceiver,
   1175         isolate->factory()->NewStringFromAsciiChecked(
   1176             "Error.prototype.toString"),
   1177         receiver));
   1178   }
   1179   Handle<JSReceiver> recv = Handle<JSReceiver>::cast(receiver);
   1180 
   1181   // 3. Let name be ? Get(O, "name").
   1182   // 4. If name is undefined, let name be "Error"; otherwise let name be
   1183   // ? ToString(name).
   1184   Handle<String> name_key = isolate->factory()->name_string();
   1185   Handle<String> name_default = isolate->factory()->Error_string();
   1186   Handle<String> name;
   1187   ASSIGN_RETURN_ON_EXCEPTION(
   1188       isolate, name,
   1189       GetStringPropertyOrDefault(isolate, recv, name_key, name_default),
   1190       String);
   1191 
   1192   // 5. Let msg be ? Get(O, "message").
   1193   // 6. If msg is undefined, let msg be the empty String; otherwise let msg be
   1194   // ? ToString(msg).
   1195   Handle<String> msg_key = isolate->factory()->message_string();
   1196   Handle<String> msg_default = isolate->factory()->empty_string();
   1197   Handle<String> msg;
   1198   ASSIGN_RETURN_ON_EXCEPTION(
   1199       isolate, msg,
   1200       GetStringPropertyOrDefault(isolate, recv, msg_key, msg_default), String);
   1201 
   1202   // 7. If name is the empty String, return msg.
   1203   // 8. If msg is the empty String, return name.
   1204   if (name->length() == 0) return msg;
   1205   if (msg->length() == 0) return name;
   1206 
   1207   // 9. Return the result of concatenating name, the code unit 0x003A (COLON),
   1208   // the code unit 0x0020 (SPACE), and msg.
   1209   IncrementalStringBuilder builder(isolate);
   1210   builder.AppendString(name);
   1211   builder.AppendCString(": ");
   1212   builder.AppendString(msg);
   1213 
   1214   Handle<String> result;
   1215   ASSIGN_RETURN_ON_EXCEPTION(isolate, result, builder.Finish(), String);
   1216   return result;
   1217 }
   1218 
   1219 namespace {
   1220 
   1221 Handle<String> FormatMessage(Isolate* isolate, int template_index,
   1222                              Handle<Object> arg0, Handle<Object> arg1,
   1223                              Handle<Object> arg2) {
   1224   Handle<String> arg0_str = Object::NoSideEffectsToString(isolate, arg0);
   1225   Handle<String> arg1_str = Object::NoSideEffectsToString(isolate, arg1);
   1226   Handle<String> arg2_str = Object::NoSideEffectsToString(isolate, arg2);
   1227 
   1228   isolate->native_context()->IncrementErrorsThrown();
   1229 
   1230   Handle<String> msg;
   1231   if (!MessageTemplate::FormatMessage(isolate, template_index, arg0_str,
   1232                                       arg1_str, arg2_str)
   1233            .ToHandle(&msg)) {
   1234     DCHECK(isolate->has_pending_exception());
   1235     isolate->clear_pending_exception();
   1236     isolate->set_external_caught_exception(false);
   1237     return isolate->factory()->NewStringFromAsciiChecked("<error>");
   1238   }
   1239 
   1240   return msg;
   1241 }
   1242 
   1243 }  // namespace
   1244 
   1245 // static
   1246 MaybeHandle<Object> ErrorUtils::MakeGenericError(
   1247     Isolate* isolate, Handle<JSFunction> constructor, int template_index,
   1248     Handle<Object> arg0, Handle<Object> arg1, Handle<Object> arg2,
   1249     FrameSkipMode mode) {
   1250   if (FLAG_clear_exceptions_on_js_entry) {
   1251     // This function used to be implemented in JavaScript, and JSEntryStub
   1252     // clears
   1253     // any pending exceptions - so whenever we'd call this from C++, pending
   1254     // exceptions would be cleared. Preserve this behavior.
   1255     isolate->clear_pending_exception();
   1256   }
   1257 
   1258   DCHECK(mode != SKIP_UNTIL_SEEN);
   1259 
   1260   Handle<Object> no_caller;
   1261   Handle<String> msg = FormatMessage(isolate, template_index, arg0, arg1, arg2);
   1262   return ErrorUtils::Construct(isolate, constructor, constructor, msg, mode,
   1263                                no_caller, false);
   1264 }
   1265 
   1266 }  // namespace internal
   1267 }  // namespace v8
   1268