• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
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 
operator <<(std::ostream & os,const ErrorCode & error_code)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 
Format(i::Handle<i::JSFunction> constructor,const char * format,va_list args)30 void ErrorThrower::Format(i::Handle<i::JSFunction> constructor,
31                           const char* format, va_list args) {
32   // Only report the first error.
33   if (error()) return;
34 
35   char buffer[256];
36   base::OS::VSNPrintF(buffer, 255, format, args);
37 
38   std::ostringstream str;
39   if (context_ != nullptr) {
40     str << context_ << ": ";
41   }
42   str << buffer;
43 
44   i::Handle<i::String> message =
45       isolate_->factory()->NewStringFromAsciiChecked(str.str().c_str());
46   exception_ = isolate_->factory()->NewError(constructor, message);
47 }
48 
TypeError(const char * format,...)49 void ErrorThrower::TypeError(const char* format, ...) {
50   if (error()) return;
51   va_list arguments;
52   va_start(arguments, format);
53   Format(isolate_->type_error_function(), format, arguments);
54   va_end(arguments);
55 }
56 
RangeError(const char * format,...)57 void ErrorThrower::RangeError(const char* format, ...) {
58   if (error()) return;
59   va_list arguments;
60   va_start(arguments, format);
61   Format(isolate_->range_error_function(), format, arguments);
62   va_end(arguments);
63 }
64 
CompileError(const char * format,...)65 void ErrorThrower::CompileError(const char* format, ...) {
66   if (error()) return;
67   va_list arguments;
68   va_start(arguments, format);
69   Format(isolate_->wasm_compile_error_function(), format, arguments);
70   va_end(arguments);
71 }
72 
RuntimeError(const char * format,...)73 void ErrorThrower::RuntimeError(const char* format, ...) {
74   if (error()) return;
75   va_list arguments;
76   va_start(arguments, format);
77   Format(isolate_->wasm_runtime_error_function(), format, arguments);
78   va_end(arguments);
79 }
80 
~ErrorThrower()81 ErrorThrower::~ErrorThrower() {
82   if (error() && !isolate_->has_pending_exception()) {
83     isolate_->ScheduleThrow(*exception_);
84   }
85 }
86 }  // namespace wasm
87 }  // namespace internal
88 }  // namespace v8
89