pending-compilation-error-handler.cc 2.42 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/pending-compilation-error-handler.h"

7
#include "src/ast/ast-value-factory.h"
8
#include "src/debug/debug.h"
9 10 11
#include "src/handles.h"
#include "src/isolate.h"
#include "src/messages.h"
12
#include "src/objects-inl.h"
13 14 15 16

namespace v8 {
namespace internal {

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Handle<String> PendingCompilationErrorHandler::ArgumentString(
    Isolate* isolate) {
  if (arg_ != NULL) return arg_->string();
  if (char_arg_ != NULL) {
    return isolate->factory()
        ->NewStringFromUtf8(CStrVector(char_arg_))
        .ToHandleChecked();
  }
  return isolate->factory()->undefined_string();
}

Handle<String> PendingCompilationErrorHandler::FormatMessage(Isolate* isolate) {
  return MessageTemplate::FormatMessage(isolate, message_,
                                        ArgumentString(isolate));
}

33 34 35 36 37
void PendingCompilationErrorHandler::ThrowPendingError(Isolate* isolate,
                                                       Handle<Script> script) {
  if (!has_pending_error_) return;
  MessageLocation location(script, start_position_, end_position_);
  Factory* factory = isolate->factory();
38
  Handle<String> argument = ArgumentString(isolate);
39 40 41 42 43
  isolate->debug()->OnCompileError(script);

  Handle<Object> error;
  switch (error_type_) {
    case kReferenceError:
44
      error = factory->NewReferenceError(message_, argument);
45 46
      break;
    case kSyntaxError:
47
      error = factory->NewSyntaxError(message_, argument);
48 49 50
      break;
    default:
      UNREACHABLE();
51 52 53
      break;
  }

54 55 56 57 58
  if (!error->IsJSObject()) {
    isolate->Throw(*error, &location);
    return;
  }

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  Handle<JSObject> jserror = Handle<JSObject>::cast(error);

  Handle<Name> key_start_pos = factory->error_start_pos_symbol();
  JSObject::SetProperty(jserror, key_start_pos,
                        handle(Smi::FromInt(location.start_pos()), isolate),
                        SLOPPY).Check();

  Handle<Name> key_end_pos = factory->error_end_pos_symbol();
  JSObject::SetProperty(jserror, key_end_pos,
                        handle(Smi::FromInt(location.end_pos()), isolate),
                        SLOPPY).Check();

  Handle<Name> key_script = factory->error_script_symbol();
  JSObject::SetProperty(jserror, key_script, script, SLOPPY).Check();

  isolate->Throw(*error, &location);
}
76 77
}  // namespace internal
}  // namespace v8