Coverage Report

Created: 2025-07-04 09:33

/src/node/deps/v8/include/v8-exception.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2021 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
#ifndef INCLUDE_V8_EXCEPTION_H_
6
#define INCLUDE_V8_EXCEPTION_H_
7
8
#include <stddef.h>
9
10
#include "v8-local-handle.h"  // NOLINT(build/include_directory)
11
#include "v8config.h"         // NOLINT(build/include_directory)
12
13
namespace v8 {
14
15
class Context;
16
class Isolate;
17
class Message;
18
class StackTrace;
19
class String;
20
class Value;
21
22
namespace internal {
23
class Isolate;
24
class ThreadLocalTop;
25
}  // namespace internal
26
27
/**
28
 * Create new error objects by calling the corresponding error object
29
 * constructor with the message.
30
 */
31
class V8_EXPORT Exception {
32
 public:
33
  static Local<Value> RangeError(Local<String> message,
34
                                 Local<Value> options = {});
35
  static Local<Value> ReferenceError(Local<String> message,
36
                                     Local<Value> options = {});
37
  static Local<Value> SyntaxError(Local<String> message,
38
                                  Local<Value> options = {});
39
  static Local<Value> TypeError(Local<String> message,
40
                                Local<Value> options = {});
41
  static Local<Value> WasmCompileError(Local<String> message,
42
                                       Local<Value> options = {});
43
  static Local<Value> WasmLinkError(Local<String> message,
44
                                    Local<Value> options = {});
45
  static Local<Value> WasmRuntimeError(Local<String> message,
46
                                       Local<Value> options = {});
47
  static Local<Value> Error(Local<String> message, Local<Value> options = {});
48
49
  /**
50
   * Creates an error message for the given exception.
51
   * Will try to reconstruct the original stack trace from the exception value,
52
   * or capture the current stack trace if not available.
53
   */
54
  static Local<Message> CreateMessage(Isolate* isolate, Local<Value> exception);
55
56
  /**
57
   * Returns the original stack trace that was captured at the creation time
58
   * of a given exception, or an empty handle if not available.
59
   */
60
  static Local<StackTrace> GetStackTrace(Local<Value> exception);
61
};
62
63
/**
64
 * An external exception handler.
65
 */
66
class V8_EXPORT TryCatch {
67
 public:
68
  /**
69
   * Creates a new try/catch block and registers it with v8.  Note that
70
   * all TryCatch blocks should be stack allocated because the memory
71
   * location itself is compared against JavaScript try/catch blocks.
72
   */
73
  explicit TryCatch(Isolate* isolate);
74
75
  /**
76
   * Unregisters and deletes this try/catch block.
77
   */
78
  ~TryCatch();
79
80
  /**
81
   * Returns true if an exception has been caught by this try/catch block.
82
   */
83
  bool HasCaught() const;
84
85
  /**
86
   * For certain types of exceptions, it makes no sense to continue execution.
87
   *
88
   * If CanContinue returns false, the correct action is to perform any C++
89
   * cleanup needed and then return.  If CanContinue returns false and
90
   * HasTerminated returns true, it is possible to call
91
   * CancelTerminateExecution in order to continue calling into the engine.
92
   */
93
  bool CanContinue() const;
94
95
  /**
96
   * Returns true if an exception has been caught due to script execution
97
   * being terminated.
98
   *
99
   * There is no JavaScript representation of an execution termination
100
   * exception.  Such exceptions are thrown when the TerminateExecution
101
   * methods are called to terminate a long-running script.
102
   *
103
   * If such an exception has been thrown, HasTerminated will return true,
104
   * indicating that it is possible to call CancelTerminateExecution in order
105
   * to continue calling into the engine.
106
   */
107
  bool HasTerminated() const;
108
109
  /**
110
   * Throws the exception caught by this TryCatch in a way that avoids
111
   * it being caught again by this same TryCatch.  As with ThrowException
112
   * it is illegal to execute any JavaScript operations after calling
113
   * ReThrow; the caller must return immediately to where the exception
114
   * is caught.
115
   */
116
  Local<Value> ReThrow();
117
118
  /**
119
   * Returns the exception caught by this try/catch block.  If no exception has
120
   * been caught an empty handle is returned.
121
   */
122
  Local<Value> Exception() const;
123
124
  /**
125
   * Returns the .stack property of an object.  If no .stack
126
   * property is present an empty handle is returned.
127
   */
128
  V8_WARN_UNUSED_RESULT static MaybeLocal<Value> StackTrace(
129
      Local<Context> context, Local<Value> exception);
130
131
  /**
132
   * Returns the .stack property of the thrown object.  If no .stack property is
133
   * present or if this try/catch block has not caught an exception, an empty
134
   * handle is returned.
135
   */
136
  V8_WARN_UNUSED_RESULT MaybeLocal<Value> StackTrace(
137
      Local<Context> context) const;
138
139
  /**
140
   * Returns the message associated with this exception.  If there is
141
   * no message associated an empty handle is returned.
142
   */
143
  Local<v8::Message> Message() const;
144
145
  /**
146
   * Clears any exceptions that may have been caught by this try/catch block.
147
   * After this method has been called, HasCaught() will return false. Cancels
148
   * the scheduled exception if it is caught and ReThrow() is not called before.
149
   *
150
   * It is not necessary to clear a try/catch block before using it again; if
151
   * another exception is thrown the previously caught exception will just be
152
   * overwritten.  However, it is often a good idea since it makes it easier
153
   * to determine which operation threw a given exception.
154
   */
155
  void Reset();
156
157
  /**
158
   * Set verbosity of the external exception handler.
159
   *
160
   * By default, exceptions that are caught by an external exception
161
   * handler are not reported.  Call SetVerbose with true on an
162
   * external exception handler to have exceptions caught by the
163
   * handler reported as if they were not caught.
164
   */
165
  void SetVerbose(bool value);
166
167
  /**
168
   * Returns true if verbosity is enabled.
169
   */
170
  bool IsVerbose() const;
171
172
  /**
173
   * Set whether or not this TryCatch should capture a Message object
174
   * which holds source information about where the exception
175
   * occurred.  True by default.
176
   */
177
  void SetCaptureMessage(bool value);
178
179
  TryCatch(const TryCatch&) = delete;
180
  void operator=(const TryCatch&) = delete;
181
182
 private:
183
  // Declaring operator new and delete as deleted is not spec compliant.
184
  // Therefore declare them private instead to disable dynamic alloc
185
  void* operator new(size_t size);
186
  void* operator new[](size_t size);
187
  void operator delete(void*, size_t);
188
  void operator delete[](void*, size_t);
189
190
  /**
191
   * There are cases when the raw address of C++ TryCatch object cannot be
192
   * used for comparisons with addresses into the JS stack. The cases are:
193
   * 1) ARM, ARM64 and MIPS simulators which have separate JS stack.
194
   * 2) Address sanitizer allocates local C++ object in the heap when
195
   *    UseAfterReturn mode is enabled.
196
   * This method returns address that can be used for comparisons with
197
   * addresses into the JS stack. When neither simulator nor ASAN's
198
   * UseAfterReturn is enabled, then the address returned will be the address
199
   * of the C++ try catch handler itself.
200
   */
201
0
  internal::Address JSStackComparableAddressPrivate() {
202
0
    return js_stack_comparable_address_;
203
0
  }
204
205
  void ResetInternal();
206
207
  internal::Isolate* i_isolate_;
208
  TryCatch* next_;
209
  void* exception_;
210
  void* message_obj_;
211
  internal::Address js_stack_comparable_address_;
212
  bool is_verbose_ : 1;
213
  bool can_continue_ : 1;
214
  bool capture_message_ : 1;
215
  bool rethrow_ : 1;
216
217
  friend class internal::Isolate;
218
  friend class internal::ThreadLocalTop;
219
};
220
221
}  // namespace v8
222
223
#endif  // INCLUDE_V8_EXCEPTION_H_