• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MESSAGE_H_
6 #define INCLUDE_V8_MESSAGE_H_
7 
8 #include <stdio.h>
9 
10 #include <iosfwd>
11 
12 #include "v8-local-handle.h"  // NOLINT(build/include_directory)
13 #include "v8-maybe.h"         // NOLINT(build/include_directory)
14 #include "v8-primitive.h"     // NOLINT(build/include_directory)
15 #include "v8config.h"         // NOLINT(build/include_directory)
16 
17 namespace v8 {
18 
19 class Integer;
20 class PrimitiveArray;
21 class StackTrace;
22 class String;
23 class Value;
24 
25 /**
26  * The optional attributes of ScriptOrigin.
27  */
28 class ScriptOriginOptions {
29  public:
30   V8_INLINE ScriptOriginOptions(bool is_shared_cross_origin = false,
31                                 bool is_opaque = false, bool is_wasm = false,
32                                 bool is_module = false)
33       : flags_((is_shared_cross_origin ? kIsSharedCrossOrigin : 0) |
34                (is_wasm ? kIsWasm : 0) | (is_opaque ? kIsOpaque : 0) |
35                (is_module ? kIsModule : 0)) {}
ScriptOriginOptions(int flags)36   V8_INLINE ScriptOriginOptions(int flags)
37       : flags_(flags &
38                (kIsSharedCrossOrigin | kIsOpaque | kIsWasm | kIsModule)) {}
39 
IsSharedCrossOrigin()40   bool IsSharedCrossOrigin() const {
41     return (flags_ & kIsSharedCrossOrigin) != 0;
42   }
IsOpaque()43   bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; }
IsWasm()44   bool IsWasm() const { return (flags_ & kIsWasm) != 0; }
IsModule()45   bool IsModule() const { return (flags_ & kIsModule) != 0; }
46 
Flags()47   int Flags() const { return flags_; }
48 
49  private:
50   enum {
51     kIsSharedCrossOrigin = 1,
52     kIsOpaque = 1 << 1,
53     kIsWasm = 1 << 2,
54     kIsModule = 1 << 3
55   };
56   const int flags_;
57 };
58 
59 /**
60  * The origin, within a file, of a script.
61  */
62 class V8_EXPORT ScriptOrigin {
63  public:
64   V8_INLINE ScriptOrigin(Isolate* isolate, Local<Value> resource_name,
65                          int resource_line_offset = 0,
66                          int resource_column_offset = 0,
67                          bool resource_is_shared_cross_origin = false,
68                          int script_id = -1,
69                          Local<Value> source_map_url = Local<Value>(),
70                          bool resource_is_opaque = false, bool is_wasm = false,
71                          bool is_module = false,
72                          Local<Data> host_defined_options = Local<Data>())
isolate_(isolate)73       : isolate_(isolate),
74         resource_name_(resource_name),
75         resource_line_offset_(resource_line_offset),
76         resource_column_offset_(resource_column_offset),
77         options_(resource_is_shared_cross_origin, resource_is_opaque, is_wasm,
78                  is_module),
79         script_id_(script_id),
80         source_map_url_(source_map_url),
81         host_defined_options_(host_defined_options) {
82     VerifyHostDefinedOptions();
83   }
84 
85   V8_INLINE Local<Value> ResourceName() const;
86   V8_INLINE int LineOffset() const;
87   V8_INLINE int ColumnOffset() const;
88   V8_INLINE int ScriptId() const;
89   V8_INLINE Local<Value> SourceMapUrl() const;
90   V8_DEPRECATE_SOON("Use GetHostDefinedOptions")
91   Local<PrimitiveArray> HostDefinedOptions() const;
92   V8_INLINE Local<Data> GetHostDefinedOptions() const;
Options()93   V8_INLINE ScriptOriginOptions Options() const { return options_; }
94 
95  private:
96   void VerifyHostDefinedOptions() const;
97   Isolate* isolate_;
98   Local<Value> resource_name_;
99   int resource_line_offset_;
100   int resource_column_offset_;
101   ScriptOriginOptions options_;
102   int script_id_;
103   Local<Value> source_map_url_;
104   Local<Data> host_defined_options_;
105 };
106 
107 /**
108  * An error message.
109  */
110 class V8_EXPORT Message {
111  public:
112   Local<String> Get() const;
113 
114   /**
115    * Return the isolate to which the Message belongs.
116    */
117   Isolate* GetIsolate() const;
118 
119   V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSource(
120       Local<Context> context) const;
121   V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSourceLine(
122       Local<Context> context) const;
123 
124   /**
125    * Returns the origin for the script from where the function causing the
126    * error originates.
127    */
128   ScriptOrigin GetScriptOrigin() const;
129 
130   /**
131    * Returns the resource name for the script from where the function causing
132    * the error originates.
133    */
134   Local<Value> GetScriptResourceName() const;
135 
136   /**
137    * Exception stack trace. By default stack traces are not captured for
138    * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
139    * to change this option.
140    */
141   Local<StackTrace> GetStackTrace() const;
142 
143   /**
144    * Returns the number, 1-based, of the line where the error occurred.
145    */
146   V8_WARN_UNUSED_RESULT Maybe<int> GetLineNumber(Local<Context> context) const;
147 
148   /**
149    * Returns the index within the script of the first character where
150    * the error occurred.
151    */
152   int GetStartPosition() const;
153 
154   /**
155    * Returns the index within the script of the last character where
156    * the error occurred.
157    */
158   int GetEndPosition() const;
159 
160   /**
161    * Returns the Wasm function index where the error occurred. Returns -1 if
162    * message is not from a Wasm script.
163    */
164   int GetWasmFunctionIndex() const;
165 
166   /**
167    * Returns the error level of the message.
168    */
169   int ErrorLevel() const;
170 
171   /**
172    * Returns the index within the line of the first character where
173    * the error occurred.
174    */
175   int GetStartColumn() const;
176   V8_WARN_UNUSED_RESULT Maybe<int> GetStartColumn(Local<Context> context) const;
177 
178   /**
179    * Returns the index within the line of the last character where
180    * the error occurred.
181    */
182   int GetEndColumn() const;
183   V8_WARN_UNUSED_RESULT Maybe<int> GetEndColumn(Local<Context> context) const;
184 
185   /**
186    * Passes on the value set by the embedder when it fed the script from which
187    * this Message was generated to V8.
188    */
189   bool IsSharedCrossOrigin() const;
190   bool IsOpaque() const;
191 
192   static void PrintCurrentStackTrace(Isolate* isolate, std::ostream& out);
193 
194   static const int kNoLineNumberInfo = 0;
195   static const int kNoColumnInfo = 0;
196   static const int kNoScriptIdInfo = 0;
197   static const int kNoWasmFunctionIndexInfo = -1;
198 };
199 
ResourceName()200 Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; }
201 
GetHostDefinedOptions()202 Local<Data> ScriptOrigin::GetHostDefinedOptions() const {
203   return host_defined_options_;
204 }
205 
LineOffset()206 int ScriptOrigin::LineOffset() const { return resource_line_offset_; }
207 
ColumnOffset()208 int ScriptOrigin::ColumnOffset() const { return resource_column_offset_; }
209 
ScriptId()210 int ScriptOrigin::ScriptId() const { return script_id_; }
211 
SourceMapUrl()212 Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; }
213 
214 }  // namespace v8
215 
216 #endif  // INCLUDE_V8_MESSAGE_H_
217