wasm-result.cc 4.61 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/wasm/wasm-result.h"

7
#include "src/heap/factory.h"
8
#include "src/heap/heap.h"
9
#include "src/isolate-inl.h"
10 11 12 13 14 15 16 17
#include "src/objects.h"

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

namespace v8 {
namespace internal {
namespace wasm {

18 19 20 21 22 23 24 25 26 27 28
namespace {

PRINTF_FORMAT(3, 0)
void VPrintFToString(std::string& str, size_t str_offset, const char* format,
                     va_list args) {
  DCHECK_LE(str_offset, str.size());
  size_t len = str_offset + strlen(format);
  // Allocate increasingly large buffers until the message fits.
  for (;; len = base::bits::RoundUpToPowerOfTwo64(len + 1)) {
    DCHECK_GE(kMaxInt, len);
    str.resize(len);
29 30
    va_list args_copy;
    va_copy(args_copy, args);
31 32
    int written = VSNPrintF(Vector<char>(&str.front() + str_offset,
                                         static_cast<int>(len - str_offset)),
33 34
                            format, args_copy);
    va_end(args_copy);
35 36 37 38 39 40
    if (written < 0) continue;  // not enough space.
    str.resize(str_offset + written);
    return;
  }
}

41 42 43 44 45 46 47 48 49
PRINTF_FORMAT(3, 4)
void PrintFToString(std::string& str, size_t str_offset, const char* format,
                    ...) {
  va_list args;
  va_start(args, format);
  VPrintFToString(str, str_offset, format, args);
  va_end(args);
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}  // namespace

void ResultBase::error(uint32_t offset, std::string error_msg) {
  // The error message must not be empty, otherwise Result::failed() will be
  // false.
  DCHECK(!error_msg.empty());
  error_offset_ = offset;
  error_msg_ = std::move(error_msg);
}

void ResultBase::verror(const char* format, va_list args) {
  VPrintFToString(error_msg_, 0, format, args);
  // Assign default message such that ok() and failed() work.
  if (error_msg_.empty() == 0) error_msg_.assign("Error");
}

66 67
void ErrorThrower::Format(ErrorType type, const char* format, va_list args) {
  DCHECK_NE(kNone, type);
rossberg's avatar
rossberg committed
68 69
  // Only report the first error.
  if (error()) return;
70

71
  size_t context_len = 0;
72
  if (context_) {
73 74
    PrintFToString(error_msg_, 0, "%s: ", context_);
    context_len = error_msg_.size();
75
  }
76 77
  VPrintFToString(error_msg_, context_len, format, args);
  error_type_ = type;
78 79
}

80
void ErrorThrower::TypeError(const char* format, ...) {
81 82
  va_list arguments;
  va_start(arguments, format);
83
  Format(kTypeError, format, arguments);
84 85 86
  va_end(arguments);
}

87
void ErrorThrower::RangeError(const char* format, ...) {
88 89
  va_list arguments;
  va_start(arguments, format);
90
  Format(kRangeError, format, arguments);
91 92 93
  va_end(arguments);
}

94
void ErrorThrower::CompileError(const char* format, ...) {
95 96
  va_list arguments;
  va_start(arguments, format);
97
  Format(kCompileError, format, arguments);
98 99 100
  va_end(arguments);
}

101 102 103
void ErrorThrower::LinkError(const char* format, ...) {
  va_list arguments;
  va_start(arguments, format);
104
  Format(kLinkError, format, arguments);
105 106 107
  va_end(arguments);
}

108 109 110
void ErrorThrower::RuntimeError(const char* format, ...) {
  va_list arguments;
  va_start(arguments, format);
111
  Format(kRuntimeError, format, arguments);
112
  va_end(arguments);
rossberg's avatar
rossberg committed
113 114
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
Handle<Object> ErrorThrower::Reify() {
  Handle<JSFunction> constructor;
  switch (error_type_) {
    case kNone:
      UNREACHABLE();
    case kTypeError:
      constructor = isolate_->type_error_function();
      break;
    case kRangeError:
      constructor = isolate_->range_error_function();
      break;
    case kCompileError:
      constructor = isolate_->wasm_compile_error_function();
      break;
    case kLinkError:
      constructor = isolate_->wasm_link_error_function();
      break;
    case kRuntimeError:
      constructor = isolate_->wasm_runtime_error_function();
      break;
  }
136
  Vector<const char> msg_vec(error_msg_.data(), error_msg_.size());
137
  Handle<String> message =
138
      isolate_->factory()->NewStringFromUtf8(msg_vec).ToHandleChecked();
139 140
  Reset();
  return isolate_->factory()->NewError(constructor, message);
141 142
}

143 144 145 146 147
void ErrorThrower::Reset() {
  error_type_ = kNone;
  error_msg_.clear();
}

148 149 150 151 152 153 154 155
ErrorThrower::ErrorThrower(ErrorThrower&& other)
    : isolate_(other.isolate_),
      context_(other.context_),
      error_type_(other.error_type_),
      error_msg_(other.error_msg_) {
  other.error_type_ = kNone;
}

rossberg's avatar
rossberg committed
156 157
ErrorThrower::~ErrorThrower() {
  if (error() && !isolate_->has_pending_exception()) {
158 159 160 161
    // We don't want to mix pending exceptions and scheduled exceptions, hence
    // an existing exception should be pending, never scheduled.
    DCHECK(!isolate_->has_scheduled_exception());
    isolate_->Throw(*Reify());
rossberg's avatar
rossberg committed
162
  }
163
}
164

165 166 167
}  // namespace wasm
}  // namespace internal
}  // namespace v8