Home | History | Annotate | Download | only in src
      1 // Copyright 2015 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/pending-compilation-error-handler.h"
      6 
      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"
     13 
     14 namespace v8 {
     15 namespace internal {
     16 
     17 Handle<String> PendingCompilationErrorHandler::ArgumentString(
     18     Isolate* isolate) {
     19   if (arg_ != NULL) return arg_->string();
     20   if (char_arg_ != NULL) {
     21     return isolate->factory()
     22         ->NewStringFromUtf8(CStrVector(char_arg_))
     23         .ToHandleChecked();
     24   }
     25   return isolate->factory()->undefined_string();
     26 }
     27 
     28 Handle<String> PendingCompilationErrorHandler::FormatMessage(Isolate* isolate) {
     29   return MessageTemplate::FormatMessage(isolate, message_,
     30                                         ArgumentString(isolate));
     31 }
     32 
     33 void PendingCompilationErrorHandler::ThrowPendingError(Isolate* isolate,
     34                                                        Handle<Script> script) {
     35   if (!has_pending_error_) return;
     36   MessageLocation location(script, start_position_, end_position_);
     37   Factory* factory = isolate->factory();
     38   Handle<String> argument = ArgumentString(isolate);
     39   isolate->debug()->OnCompileError(script);
     40 
     41   Handle<Object> error;
     42   switch (error_type_) {
     43     case kReferenceError:
     44       error = factory->NewReferenceError(message_, argument);
     45       break;
     46     case kSyntaxError:
     47       error = factory->NewSyntaxError(message_, argument);
     48       break;
     49     default:
     50       UNREACHABLE();
     51       break;
     52   }
     53 
     54   if (!error->IsJSObject()) {
     55     isolate->Throw(*error, &location);
     56     return;
     57   }
     58 
     59   Handle<JSObject> jserror = Handle<JSObject>::cast(error);
     60 
     61   Handle<Name> key_start_pos = factory->error_start_pos_symbol();
     62   JSObject::SetProperty(jserror, key_start_pos,
     63                         handle(Smi::FromInt(location.start_pos()), isolate),
     64                         SLOPPY).Check();
     65 
     66   Handle<Name> key_end_pos = factory->error_end_pos_symbol();
     67   JSObject::SetProperty(jserror, key_end_pos,
     68                         handle(Smi::FromInt(location.end_pos()), isolate),
     69                         SLOPPY).Check();
     70 
     71   Handle<Name> key_script = factory->error_script_symbol();
     72   JSObject::SetProperty(jserror, key_script, script, SLOPPY).Check();
     73 
     74   isolate->Throw(*error, &location);
     75 }
     76 }  // namespace internal
     77 }  // namespace v8
     78