LCOV - code coverage report
Current view: top level - src/wasm - wasm-result.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 73 78 93.6 %
Date: 2019-01-20 Functions: 14 15 93.3 %

          Line data    Source code
       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/heap/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             : namespace {
      19             : 
      20             : PRINTF_FORMAT(3, 0)
      21       61945 : void VPrintFToString(std::string& str, size_t str_offset, const char* format,
      22             :                      va_list args) {
      23             :   DCHECK_LE(str_offset, str.size());
      24       61945 :   size_t len = str_offset + strlen(format);
      25             :   // Allocate increasingly large buffers until the message fits.
      26      153568 :   for (;; len = base::bits::RoundUpToPowerOfTwo64(len + 1)) {
      27             :     DCHECK_GE(kMaxInt, len);
      28             :     str.resize(len);
      29             :     va_list args_copy;
      30      215513 :     va_copy(args_copy, args);
      31             :     int written = VSNPrintF(Vector<char>(&str.front() + str_offset,
      32             :                                          static_cast<int>(len - str_offset)),
      33      646539 :                             format, args_copy);
      34      215513 :     va_end(args_copy);
      35      369081 :     if (written < 0) continue;  // not enough space.
      36       61945 :     str.resize(str_offset + written);
      37       61945 :     return;
      38      153568 :   }
      39             : }
      40             : 
      41             : PRINTF_FORMAT(3, 4)
      42       30877 : void PrintFToString(std::string& str, size_t str_offset, const char* format,
      43             :                     ...) {
      44             :   va_list args;
      45       30877 :   va_start(args, format);
      46       30877 :   VPrintFToString(str, str_offset, format, args);
      47       30877 :   va_end(args);
      48       30877 : }
      49             : 
      50             : }  // namespace
      51             : 
      52             : // static
      53         191 : std::string WasmError::FormatError(const char* format, va_list args) {
      54             :   std::string result;
      55         191 :   VPrintFToString(result, 0, format, args);
      56         191 :   return result;
      57             : }
      58             : 
      59       38397 : void ErrorThrower::Format(ErrorType type, const char* format, va_list args) {
      60             :   DCHECK_NE(kNone, type);
      61             :   // Only report the first error.
      62       76794 :   if (error()) return;
      63             : 
      64             :   size_t context_len = 0;
      65       30877 :   if (context_) {
      66       30877 :     PrintFToString(error_msg_, 0, "%s: ", context_);
      67             :     context_len = error_msg_.size();
      68             :   }
      69       30877 :   VPrintFToString(error_msg_, context_len, format, args);
      70       30877 :   error_type_ = type;
      71             : }
      72             : 
      73        9709 : void ErrorThrower::TypeError(const char* format, ...) {
      74             :   va_list arguments;
      75        9709 :   va_start(arguments, format);
      76        9709 :   Format(kTypeError, format, arguments);
      77        9709 :   va_end(arguments);
      78        9709 : }
      79             : 
      80        1152 : void ErrorThrower::RangeError(const char* format, ...) {
      81             :   va_list arguments;
      82        1152 :   va_start(arguments, format);
      83        1152 :   Format(kRangeError, format, arguments);
      84        1152 :   va_end(arguments);
      85        1152 : }
      86             : 
      87       24590 : void ErrorThrower::CompileError(const char* format, ...) {
      88             :   va_list arguments;
      89       24590 :   va_start(arguments, format);
      90       24590 :   Format(kCompileError, format, arguments);
      91       24590 :   va_end(arguments);
      92       24590 : }
      93             : 
      94        2896 : void ErrorThrower::LinkError(const char* format, ...) {
      95             :   va_list arguments;
      96        2896 :   va_start(arguments, format);
      97        2896 :   Format(kLinkError, format, arguments);
      98        2896 :   va_end(arguments);
      99        2896 : }
     100             : 
     101          50 : void ErrorThrower::RuntimeError(const char* format, ...) {
     102             :   va_list arguments;
     103          50 :   va_start(arguments, format);
     104          50 :   Format(kRuntimeError, format, arguments);
     105          50 :   va_end(arguments);
     106          50 : }
     107             : 
     108       30643 : Handle<Object> ErrorThrower::Reify() {
     109             :   Handle<JSFunction> constructor;
     110       30643 :   switch (error_type_) {
     111             :     case kNone:
     112           0 :       UNREACHABLE();
     113             :     case kTypeError:
     114        9612 :       constructor = isolate_->type_error_function();
     115        9612 :       break;
     116             :     case kRangeError:
     117        1152 :       constructor = isolate_->range_error_function();
     118        1152 :       break;
     119             :     case kCompileError:
     120       17389 :       constructor = isolate_->wasm_compile_error_function();
     121       17389 :       break;
     122             :     case kLinkError:
     123        2440 :       constructor = isolate_->wasm_link_error_function();
     124        2440 :       break;
     125             :     case kRuntimeError:
     126          50 :       constructor = isolate_->wasm_runtime_error_function();
     127          50 :       break;
     128             :   }
     129             :   Handle<String> message = isolate_->factory()
     130       30643 :                                ->NewStringFromUtf8(VectorOf(error_msg_))
     131       61286 :                                .ToHandleChecked();
     132             :   Reset();
     133       30643 :   return isolate_->factory()->NewError(constructor, message);
     134             : }
     135             : 
     136        2300 : void ErrorThrower::Reset() {
     137       32943 :   error_type_ = kNone;
     138       32943 :   error_msg_.clear();
     139        2300 : }
     140             : 
     141           0 : ErrorThrower::ErrorThrower(ErrorThrower&& other) V8_NOEXCEPT
     142             :     : isolate_(other.isolate_),
     143             :       context_(other.context_),
     144             :       error_type_(other.error_type_),
     145           0 :       error_msg_(std::move(other.error_msg_)) {
     146           0 :   other.error_type_ = kNone;
     147           0 : }
     148             : 
     149    24340150 : ErrorThrower::~ErrorThrower() {
     150    24340150 :   if (error() && !isolate_->has_pending_exception()) {
     151             :     // We don't want to mix pending exceptions and scheduled exceptions, hence
     152             :     // an existing exception should be pending, never scheduled.
     153             :     DCHECK(!isolate_->has_scheduled_exception());
     154         134 :     isolate_->Throw(*Reify());
     155             :   }
     156    24340152 : }
     157             : 
     158             : }  // namespace wasm
     159             : }  // namespace internal
     160      183867 : }  // namespace v8

Generated by: LCOV version 1.10