5 #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 #include "src/objects.h" 12 #include "src/base/platform/platform.h" 21 void VPrintFToString(
std::
string& str,
size_t str_offset, const
char* format,
23 DCHECK_LE(str_offset, str.size());
24 size_t len = str_offset + strlen(format);
26 for (;; len = base::bits::RoundUpToPowerOfTwo64(len + 1)) {
27 DCHECK_GE(kMaxInt, len);
30 va_copy(args_copy, args);
31 int written = VSNPrintF(Vector<char>(&str.front() + str_offset,
32 static_cast<int>(len - str_offset)),
35 if (written < 0)
continue;
36 str.resize(str_offset + written);
42 void PrintFToString(
std::
string& str,
size_t str_offset, const
char* format,
45 va_start(args, format);
46 VPrintFToString(str, str_offset, format, args);
53 std::string ResultBase::FormatError(
const char* format, va_list args) {
55 VPrintFToString(result, 0, format, args);
59 void ErrorThrower::Format(ErrorType type,
const char* format, va_list args) {
60 DCHECK_NE(kNone, type);
64 size_t context_len = 0;
66 PrintFToString(error_msg_, 0,
"%s: ", context_);
67 context_len = error_msg_.size();
69 VPrintFToString(error_msg_, context_len, format, args);
73 void ErrorThrower::TypeError(
const char* format, ...) {
75 va_start(arguments, format);
76 Format(kTypeError, format, arguments);
80 void ErrorThrower::RangeError(
const char* format, ...) {
82 va_start(arguments, format);
83 Format(kRangeError, format, arguments);
87 void ErrorThrower::CompileError(
const char* format, ...) {
89 va_start(arguments, format);
90 Format(kCompileError, format, arguments);
94 void ErrorThrower::LinkError(
const char* format, ...) {
96 va_start(arguments, format);
97 Format(kLinkError, format, arguments);
101 void ErrorThrower::RuntimeError(
const char* format, ...) {
103 va_start(arguments, format);
104 Format(kRuntimeError, format, arguments);
108 Handle<Object> ErrorThrower::Reify() {
109 Handle<JSFunction> constructor;
110 switch (error_type_) {
114 constructor = isolate_->type_error_function();
117 constructor = isolate_->range_error_function();
120 constructor = isolate_->wasm_compile_error_function();
123 constructor = isolate_->wasm_link_error_function();
126 constructor = isolate_->wasm_runtime_error_function();
129 Handle<String> message = isolate_->factory()
130 ->NewStringFromUtf8(VectorOf(error_msg_))
133 return isolate_->factory()->NewError(constructor, message);
136 void ErrorThrower::Reset() {
141 ErrorThrower::ErrorThrower(ErrorThrower&& other) V8_NOEXCEPT
142 : isolate_(other.isolate_),
143 context_(other.context_),
144 error_type_(other.error_type_),
145 error_msg_(std::move(other.error_msg_)) {
146 other.error_type_ =
kNone;
149 ErrorThrower::~ErrorThrower() {
150 if (error() && !isolate_->has_pending_exception()) {
153 DCHECK(!isolate_->has_scheduled_exception());
154 isolate_->Throw(*Reify());