summaryrefslogtreecommitdiff
path: root/deps/v8/src/wasm/wasm-result.h
blob: 004ac22d330b0809b0df95b979bd7df5a714d776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_WASM_RESULT_H_
#define V8_WASM_RESULT_H_

#include <memory>

#include "src/base/compiler-specific.h"

#include "src/handles.h"
#include "src/globals.h"

namespace v8 {
namespace internal {

class Isolate;

namespace wasm {

// Error codes for programmatic checking of the decoder's verification.
enum ErrorCode {
  kSuccess,
  kError,  // TODO(titzer): introduce real error codes
};

// The overall result of decoding a function or a module.
template <typename T>
struct Result {
  Result() : val(), error_code(kSuccess), start(nullptr), error_pc(nullptr) {}
  Result(Result&& other) { *this = std::move(other); }
  Result& operator=(Result&& other) {
    MoveFrom(other);
    val = other.val;
    return *this;
  }

  T val;
  ErrorCode error_code;
  const byte* start;
  const byte* error_pc;
  const byte* error_pt;
  std::unique_ptr<char[]> error_msg;

  bool ok() const { return error_code == kSuccess; }
  bool failed() const { return error_code != kSuccess; }

  template <typename V>
  void MoveFrom(Result<V>& that) {
    error_code = that.error_code;
    start = that.start;
    error_pc = that.error_pc;
    error_pt = that.error_pt;
    error_msg = std::move(that.error_msg);
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(Result);
};

template <typename T>
std::ostream& operator<<(std::ostream& os, const Result<T>& result) {
  os << "Result = ";
  if (result.ok()) {
    if (result.val != nullptr) {
      os << *result.val;
    } else {
      os << "success (no value)";
    }
  } else if (result.error_msg.get() != nullptr) {
    ptrdiff_t offset = result.error_pc - result.start;
    if (offset < 0) {
      os << result.error_msg.get() << " @" << offset;
    } else {
      os << result.error_msg.get() << " @+" << offset;
    }
  } else {
    os << result.error_code;
  }
  os << std::endl;
  return os;
}

V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
                                           const ErrorCode& error_code);

// A helper for generating error messages that bubble up to JS exceptions.
class V8_EXPORT_PRIVATE ErrorThrower {
 public:
  ErrorThrower(i::Isolate* isolate, const char* context)
      : isolate_(isolate), context_(context) {}
  ~ErrorThrower();

  PRINTF_FORMAT(2, 3) void TypeError(const char* fmt, ...);
  PRINTF_FORMAT(2, 3) void RangeError(const char* fmt, ...);
  PRINTF_FORMAT(2, 3) void CompileError(const char* fmt, ...);
  PRINTF_FORMAT(2, 3) void LinkError(const char* fmt, ...);
  PRINTF_FORMAT(2, 3) void RuntimeError(const char* fmt, ...);

  template <typename T>
  void CompileFailed(const char* error, Result<T>& result) {
    std::ostringstream str;
    str << error << result;
    CompileError("%s", str.str().c_str());
  }

  i::Handle<i::Object> Reify() {
    i::Handle<i::Object> result = exception_;
    exception_ = i::Handle<i::Object>::null();
    return result;
  }

  bool error() const { return !exception_.is_null(); }
  bool wasm_error() { return wasm_error_; }

 private:
  void Format(i::Handle<i::JSFunction> constructor, const char* fmt, va_list);

  i::Isolate* isolate_;
  const char* context_;
  i::Handle<i::Object> exception_;
  bool wasm_error_ = false;
};
}  // namespace wasm
}  // namespace internal
}  // namespace v8

#endif