blob: 30268ac8ad52d4ee03ee3b80e99393283e77d2f7 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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/factory.h"
8#include "src/heap/heap.h"
Ben Murdoch61f157c2016-09-16 13:49:30 +01009#include "src/isolate-inl.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include "src/objects.h"
11
12#include "src/base/platform/platform.h"
13
14namespace v8 {
15namespace internal {
16namespace wasm {
17
18std::ostream& operator<<(std::ostream& os, const ErrorCode& error_code) {
19 switch (error_code) {
20 case kSuccess:
21 os << "Success";
22 break;
23 default: // TODO(titzer): render error codes
24 os << "Error";
25 break;
26 }
27 return os;
28}
29
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030void ErrorThrower::Error(const char* format, ...) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010031 // Only report the first error.
32 if (error()) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033
Ben Murdoch61f157c2016-09-16 13:49:30 +010034 char buffer[256];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035 va_list arguments;
36 va_start(arguments, format);
37 base::OS::VSNPrintF(buffer, 255, format, arguments);
38 va_end(arguments);
39
40 std::ostringstream str;
41 if (context_ != nullptr) {
42 str << context_ << ": ";
43 }
44 str << buffer;
45
Ben Murdoch61f157c2016-09-16 13:49:30 +010046 message_ = isolate_->factory()->NewStringFromAsciiChecked(str.str().c_str());
47}
48
49ErrorThrower::~ErrorThrower() {
50 if (error() && !isolate_->has_pending_exception()) {
51 isolate_->ScheduleThrow(*message_);
52 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053}
54} // namespace wasm
55} // namespace internal
56} // namespace v8