Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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/factory.h" |
| 8 | #include "src/heap/heap.h" |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 9 | #include "src/isolate-inl.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | std::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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 30 | void ErrorThrower::Error(const char* format, ...) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 31 | // Only report the first error. |
| 32 | if (error()) return; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 33 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 34 | char buffer[256]; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 35 | 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 Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 46 | message_ = isolate_->factory()->NewStringFromAsciiChecked(str.str().c_str()); |
| 47 | } |
| 48 | |
| 49 | ErrorThrower::~ErrorThrower() { |
| 50 | if (error() && !isolate_->has_pending_exception()) { |
| 51 | isolate_->ScheduleThrow(*message_); |
| 52 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 | } |
| 54 | } // namespace wasm |
| 55 | } // namespace internal |
| 56 | } // namespace v8 |