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