LCOV - code coverage report
Current view: top level - src/wasm - wasm-result.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 73 81 90.1 %
Date: 2019-04-17 Functions: 13 14 92.9 %

          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       61572 : void VPrintFToString(std::string& str, size_t str_offset, const char* format,
      22             :                      va_list args) {
      23             :   DCHECK_LE(str_offset, str.size());
      24       61572 :   size_t len = str_offset + strlen(format);
      25             :   // Allocate increasingly large buffers until the message fits.
      26      145073 :   for (;; len = base::bits::RoundUpToPowerOfTwo64(len + 1)) {
      27             :     DCHECK_GE(kMaxInt, len);
      28             :     str.resize(len);
      29             :     va_list args_copy;
      30      206645 :     va_copy(args_copy, args);
      31      413290 :     int written = VSNPrintF(Vector<char>(&str.front() + str_offset,
      32             :                                          static_cast<int>(len - str_offset)),
      33      206645 :                             format, args_copy);
      34      206645 :     va_end(args_copy);
      35      351718 :     if (written < 0) continue;  // not enough space.
      36       61572 :     str.resize(str_offset + written);
      37       61572 :     return;
      38             :   }
      39             : }
      40             : 
      41             : PRINTF_FORMAT(3, 4)
      42       30498 : void PrintFToString(std::string& str, size_t str_offset, const char* format,
      43             :                     ...) {
      44             :   va_list args;
      45       30498 :   va_start(args, format);
      46       30498 :   VPrintFToString(str, str_offset, format, args);
      47       30498 :   va_end(args);
      48       30498 : }
      49             : 
      50             : }  // namespace
      51             : 
      52             : // static
      53         576 : std::string WasmError::FormatError(const char* format, va_list args) {
      54             :   std::string result;
      55         576 :   VPrintFToString(result, 0, format, args);
      56         576 :   return result;
      57             : }
      58             : 
      59       31524 : void ErrorThrower::Format(ErrorType type, const char* format, va_list args) {
      60             :   DCHECK_NE(kNone, type);
      61             :   // Only report the first error.
      62       31524 :   if (error()) return;
      63             : 
      64             :   size_t context_len = 0;
      65       30498 :   if (context_) {
      66       30498 :     PrintFToString(error_msg_, 0, "%s: ", context_);
      67             :     context_len = error_msg_.size();
      68             :   }
      69       30498 :   VPrintFToString(error_msg_, context_len, format, args);
      70       30498 :   error_type_ = type;
      71             : }
      72             : 
      73        9903 : void ErrorThrower::TypeError(const char* format, ...) {
      74             :   va_list arguments;
      75        9903 :   va_start(arguments, format);
      76        9903 :   Format(kTypeError, format, arguments);
      77        9903 :   va_end(arguments);
      78        9903 : }
      79             : 
      80        1056 : void ErrorThrower::RangeError(const char* format, ...) {
      81             :   va_list arguments;
      82        1056 :   va_start(arguments, format);
      83        1056 :   Format(kRangeError, format, arguments);
      84        1056 :   va_end(arguments);
      85        1056 : }
      86             : 
      87       17896 : void ErrorThrower::CompileError(const char* format, ...) {
      88             :   va_list arguments;
      89       17896 :   va_start(arguments, format);
      90       17896 :   Format(kCompileError, format, arguments);
      91       17896 :   va_end(arguments);
      92       17896 : }
      93             : 
      94        2629 : void ErrorThrower::LinkError(const char* format, ...) {
      95             :   va_list arguments;
      96        2629 :   va_start(arguments, format);
      97        2629 :   Format(kLinkError, format, arguments);
      98        2629 :   va_end(arguments);
      99        2629 : }
     100             : 
     101          40 : void ErrorThrower::RuntimeError(const char* format, ...) {
     102             :   va_list arguments;
     103          40 :   va_start(arguments, format);
     104          40 :   Format(kRuntimeError, format, arguments);
     105          40 :   va_end(arguments);
     106          40 : }
     107             : 
     108       30278 : Handle<Object> ErrorThrower::Reify() {
     109             :   Handle<JSFunction> constructor;
     110       30278 :   switch (error_type_) {
     111             :     case kNone:
     112           0 :       UNREACHABLE();
     113             :     case kTypeError:
     114        9802 :       constructor = isolate_->type_error_function();
     115        9802 :       break;
     116             :     case kRangeError:
     117        1056 :       constructor = isolate_->range_error_function();
     118        1056 :       break;
     119             :     case kCompileError:
     120       17184 :       constructor = isolate_->wasm_compile_error_function();
     121       17184 :       break;
     122             :     case kLinkError:
     123        2196 :       constructor = isolate_->wasm_link_error_function();
     124        2196 :       break;
     125             :     case kRuntimeError:
     126          40 :       constructor = isolate_->wasm_runtime_error_function();
     127          40 :       break;
     128             :   }
     129       30278 :   Handle<String> message = isolate_->factory()
     130       60556 :                                ->NewStringFromUtf8(VectorOf(error_msg_))
     131       30278 :                                .ToHandleChecked();
     132             :   Reset();
     133       30278 :   return isolate_->factory()->NewError(constructor, message);
     134             : }
     135             : 
     136        2004 : void ErrorThrower::Reset() {
     137       32282 :   error_type_ = kNone;
     138             :   error_msg_.clear();
     139        2004 : }
     140             : 
     141           0 : ErrorThrower::ErrorThrower(ErrorThrower&& other) V8_NOEXCEPT
     142           0 :     : isolate_(other.isolate_),
     143           0 :       context_(other.context_),
     144           0 :       error_type_(other.error_type_),
     145           0 :       error_msg_(std::move(other.error_msg_)) {
     146           0 :   other.error_type_ = kNone;
     147           0 : }
     148             : 
     149   109849648 : ErrorThrower::~ErrorThrower() {
     150    54924883 :   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         118 :     isolate_->Throw(*Reify());
     155             :   }
     156    54924824 : }
     157             : 
     158             : }  // namespace wasm
     159             : }  // namespace internal
     160      122004 : }  // namespace v8

Generated by: LCOV version 1.10