V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
wasm-result.cc
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/wasm/wasm-result.h"
6 
7 #include "src/heap/factory.h"
8 #include "src/heap/heap.h"
9 #include "src/isolate-inl.h"
10 #include "src/objects.h"
11 
12 #include "src/base/platform/platform.h"
13 
14 namespace v8 {
15 namespace internal {
16 namespace wasm {
17 
18 namespace {
19 
20 PRINTF_FORMAT(3, 0)
21 void VPrintFToString(std::string& str, size_t str_offset, const char* format,
22  va_list args) {
23  DCHECK_LE(str_offset, str.size());
24  size_t len = str_offset + strlen(format);
25  // Allocate increasingly large buffers until the message fits.
26  for (;; len = base::bits::RoundUpToPowerOfTwo64(len + 1)) {
27  DCHECK_GE(kMaxInt, len);
28  str.resize(len);
29  va_list args_copy;
30  va_copy(args_copy, args);
31  int written = VSNPrintF(Vector<char>(&str.front() + str_offset,
32  static_cast<int>(len - str_offset)),
33  format, args_copy);
34  va_end(args_copy);
35  if (written < 0) continue; // not enough space.
36  str.resize(str_offset + written);
37  return;
38  }
39 }
40 
41 PRINTF_FORMAT(3, 4)
42 void PrintFToString(std::string& str, size_t str_offset, const char* format,
43  ...) {
44  va_list args;
45  va_start(args, format);
46  VPrintFToString(str, str_offset, format, args);
47  va_end(args);
48 }
49 
50 } // namespace
51 
52 // static
53 std::string ResultBase::FormatError(const char* format, va_list args) {
54  std::string result;
55  VPrintFToString(result, 0, format, args);
56  return result;
57 }
58 
59 void ErrorThrower::Format(ErrorType type, const char* format, va_list args) {
60  DCHECK_NE(kNone, type);
61  // Only report the first error.
62  if (error()) return;
63 
64  size_t context_len = 0;
65  if (context_) {
66  PrintFToString(error_msg_, 0, "%s: ", context_);
67  context_len = error_msg_.size();
68  }
69  VPrintFToString(error_msg_, context_len, format, args);
70  error_type_ = type;
71 }
72 
73 void ErrorThrower::TypeError(const char* format, ...) {
74  va_list arguments;
75  va_start(arguments, format);
76  Format(kTypeError, format, arguments);
77  va_end(arguments);
78 }
79 
80 void ErrorThrower::RangeError(const char* format, ...) {
81  va_list arguments;
82  va_start(arguments, format);
83  Format(kRangeError, format, arguments);
84  va_end(arguments);
85 }
86 
87 void ErrorThrower::CompileError(const char* format, ...) {
88  va_list arguments;
89  va_start(arguments, format);
90  Format(kCompileError, format, arguments);
91  va_end(arguments);
92 }
93 
94 void ErrorThrower::LinkError(const char* format, ...) {
95  va_list arguments;
96  va_start(arguments, format);
97  Format(kLinkError, format, arguments);
98  va_end(arguments);
99 }
100 
101 void ErrorThrower::RuntimeError(const char* format, ...) {
102  va_list arguments;
103  va_start(arguments, format);
104  Format(kRuntimeError, format, arguments);
105  va_end(arguments);
106 }
107 
108 Handle<Object> ErrorThrower::Reify() {
109  Handle<JSFunction> constructor;
110  switch (error_type_) {
111  case kNone:
112  UNREACHABLE();
113  case kTypeError:
114  constructor = isolate_->type_error_function();
115  break;
116  case kRangeError:
117  constructor = isolate_->range_error_function();
118  break;
119  case kCompileError:
120  constructor = isolate_->wasm_compile_error_function();
121  break;
122  case kLinkError:
123  constructor = isolate_->wasm_link_error_function();
124  break;
125  case kRuntimeError:
126  constructor = isolate_->wasm_runtime_error_function();
127  break;
128  }
129  Handle<String> message = isolate_->factory()
130  ->NewStringFromUtf8(VectorOf(error_msg_))
131  .ToHandleChecked();
132  Reset();
133  return isolate_->factory()->NewError(constructor, message);
134 }
135 
136 void ErrorThrower::Reset() {
137  error_type_ = kNone;
138  error_msg_.clear();
139 }
140 
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;
147 }
148 
149 ErrorThrower::~ErrorThrower() {
150  if (error() && !isolate_->has_pending_exception()) {
151  // We don't want to mix pending exceptions and scheduled exceptions, hence
152  // an existing exception should be pending, never scheduled.
153  DCHECK(!isolate_->has_scheduled_exception());
154  isolate_->Throw(*Reify());
155  }
156 }
157 
158 } // namespace wasm
159 } // namespace internal
160 } // namespace v8
STL namespace.
Definition: libplatform.h:13