summaryrefslogtreecommitdiff
path: root/deps/v8/src/wasm/wasm-result.cc
blob: 6d535e3f576a87fa093d1af9a482ca39c9ed6772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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"
#include "src/isolate-inl.h"
#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;
}

void ErrorThrower::Format(i::Handle<i::JSFunction> constructor,
                          const char* format, va_list args) {
  // Only report the first error.
  if (error()) return;

  char buffer[256];
  base::OS::VSNPrintF(buffer, 255, format, args);

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

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

void ErrorThrower::TypeError(const char* format, ...) {
  if (error()) return;
  va_list arguments;
  va_start(arguments, format);
  Format(isolate_->type_error_function(), format, arguments);
  va_end(arguments);
}

void ErrorThrower::RangeError(const char* format, ...) {
  if (error()) return;
  va_list arguments;
  va_start(arguments, format);
  Format(isolate_->range_error_function(), format, arguments);
  va_end(arguments);
}

void ErrorThrower::CompileError(const char* format, ...) {
  if (error()) return;
  va_list arguments;
  va_start(arguments, format);
  Format(isolate_->wasm_compile_error_function(), format, arguments);
  va_end(arguments);
}

void ErrorThrower::RuntimeError(const char* format, ...) {
  if (error()) return;
  va_list arguments;
  va_start(arguments, format);
  Format(isolate_->wasm_runtime_error_function(), format, arguments);
  va_end(arguments);
}

ErrorThrower::~ErrorThrower() {
  if (error() && !isolate_->has_pending_exception()) {
    isolate_->ScheduleThrow(*exception_);
  }
}
}  // namespace wasm
}  // namespace internal
}  // namespace v8