wasm-result.cc 2.52 KB
Newer Older
1 2 3 4 5 6 7 8
// 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/wasm/wasm-result.h"

#include "src/factory.h"
#include "src/heap/heap.h"
9
#include "src/isolate-inl.h"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include "src/objects.h"

#include "src/base/platform/platform.h"

namespace v8 {
namespace internal {
namespace wasm {

std::ostream& operator<<(std::ostream& os, const ErrorCode& error_code) {
  switch (error_code) {
    case kSuccess:
      os << "Success";
      break;
    default:  // TODO(titzer): render error codes
      os << "Error";
      break;
  }
  return os;
}

30 31
void ErrorThrower::Format(i::Handle<i::JSFunction> constructor,
                          const char* format, va_list args) {
rossberg's avatar
rossberg committed
32 33
  // Only report the first error.
  if (error()) return;
34

rossberg's avatar
rossberg committed
35
  char buffer[256];
36
  base::OS::VSNPrintF(buffer, 255, format, args);
37 38 39 40 41 42 43

  std::ostringstream str;
  if (context_ != nullptr) {
    str << context_ << ": ";
  }
  str << buffer;

44 45 46 47 48
  i::Handle<i::String> message =
      isolate_->factory()->NewStringFromAsciiChecked(str.str().c_str());
  exception_ = isolate_->factory()->NewError(constructor, message);
}

49
void ErrorThrower::TypeError(const char* format, ...) {
50 51 52
  if (error()) return;
  va_list arguments;
  va_start(arguments, format);
53
  Format(isolate_->type_error_function(), format, arguments);
54 55 56
  va_end(arguments);
}

57
void ErrorThrower::RangeError(const char* format, ...) {
58 59 60
  if (error()) return;
  va_list arguments;
  va_start(arguments, format);
61
  Format(isolate_->range_error_function(), format, arguments);
62 63 64
  va_end(arguments);
}

65
void ErrorThrower::CompileError(const char* format, ...) {
66
  if (error()) return;
67
  wasm_error_ = true;
68 69
  va_list arguments;
  va_start(arguments, format);
70 71 72 73
  Format(isolate_->wasm_compile_error_function(), format, arguments);
  va_end(arguments);
}

74 75
void ErrorThrower::LinkError(const char* format, ...) {
  if (error()) return;
76
  wasm_error_ = true;
77 78 79 80 81 82
  va_list arguments;
  va_start(arguments, format);
  Format(isolate_->wasm_link_error_function(), format, arguments);
  va_end(arguments);
}

83 84
void ErrorThrower::RuntimeError(const char* format, ...) {
  if (error()) return;
85
  wasm_error_ = true;
86 87 88
  va_list arguments;
  va_start(arguments, format);
  Format(isolate_->wasm_runtime_error_function(), format, arguments);
89
  va_end(arguments);
rossberg's avatar
rossberg committed
90 91 92 93
}

ErrorThrower::~ErrorThrower() {
  if (error() && !isolate_->has_pending_exception()) {
94
    isolate_->ScheduleThrow(*exception_);
rossberg's avatar
rossberg committed
95
  }
96 97 98 99
}
}  // namespace wasm
}  // namespace internal
}  // namespace v8