Home | History | Annotate | Download | only in wasm
      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 
     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 
     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 
     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 
     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 
     65 void ErrorThrower::CompileError(const char* format, ...) {
     66   if (error()) return;
     67   wasm_error_ = true;
     68   va_list arguments;
     69   va_start(arguments, format);
     70   Format(isolate_->wasm_compile_error_function(), format, arguments);
     71   va_end(arguments);
     72 }
     73 
     74 void ErrorThrower::LinkError(const char* format, ...) {
     75   if (error()) return;
     76   wasm_error_ = true;
     77   va_list arguments;
     78   va_start(arguments, format);
     79   Format(isolate_->wasm_link_error_function(), format, arguments);
     80   va_end(arguments);
     81 }
     82 
     83 void ErrorThrower::RuntimeError(const char* format, ...) {
     84   if (error()) return;
     85   wasm_error_ = true;
     86   va_list arguments;
     87   va_start(arguments, format);
     88   Format(isolate_->wasm_runtime_error_function(), format, arguments);
     89   va_end(arguments);
     90 }
     91 
     92 ErrorThrower::~ErrorThrower() {
     93   if (error() && !isolate_->has_pending_exception()) {
     94     isolate_->ScheduleThrow(*exception_);
     95   }
     96 }
     97 }  // namespace wasm
     98 }  // namespace internal
     99 }  // namespace v8
    100