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 17namespace v8 { 18 19class Integer; 20class PrimitiveArray; 21class StackTrace; 22class String; 23class Value; 24 25/** 26 * The optional attributes of ScriptOrigin. 27 */ 28class 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)) {} 36 V8_INLINE ScriptOriginOptions(int flags) 37 : flags_(flags & 38 (kIsSharedCrossOrigin | kIsOpaque | kIsWasm | kIsModule)) {} 39 40 bool IsSharedCrossOrigin() const { 41 return (flags_ & kIsSharedCrossOrigin) != 0; 42 } 43 bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; } 44 bool IsWasm() const { return (flags_ & kIsWasm) != 0; } 45 bool IsModule() const { return (flags_ & kIsModule) != 0; } 46 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 */ 62class 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>()) 73 : v8_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_INLINE Local<Data> GetHostDefinedOptions() const; 91 V8_INLINE ScriptOriginOptions Options() const { return options_; } 92 93 private: 94 void VerifyHostDefinedOptions() const; 95 Isolate* v8_isolate_; 96 Local<Value> resource_name_; 97 int resource_line_offset_; 98 int resource_column_offset_; 99 ScriptOriginOptions options_; 100 int script_id_; 101 Local<Value> source_map_url_; 102 Local<Data> host_defined_options_; 103}; 104 105/** 106 * An error message. 107 */ 108class V8_EXPORT Message { 109 public: 110 Local<String> Get() const; 111 112 /** 113 * Return the isolate to which the Message belongs. 114 */ 115 Isolate* GetIsolate() const; 116 117 V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSource( 118 Local<Context> context) const; 119 V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSourceLine( 120 Local<Context> context) const; 121 122 /** 123 * Returns the origin for the script from where the function causing the 124 * error originates. 125 */ 126 ScriptOrigin GetScriptOrigin() const; 127 128 /** 129 * Returns the resource name for the script from where the function causing 130 * the error originates. 131 */ 132 Local<Value> GetScriptResourceName() const; 133 134 /** 135 * Exception stack trace. By default stack traces are not captured for 136 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows 137 * to change this option. 138 */ 139 Local<StackTrace> GetStackTrace() const; 140 141 /** 142 * Returns the number, 1-based, of the line where the error occurred. 143 */ 144 V8_WARN_UNUSED_RESULT Maybe<int> GetLineNumber(Local<Context> context) const; 145 146 /** 147 * Returns the index within the script of the first character where 148 * the error occurred. 149 */ 150 int GetStartPosition() const; 151 152 /** 153 * Returns the index within the script of the last character where 154 * the error occurred. 155 */ 156 int GetEndPosition() const; 157 158 /** 159 * Returns the Wasm function index where the error occurred. Returns -1 if 160 * message is not from a Wasm script. 161 */ 162 int GetWasmFunctionIndex() const; 163 164 /** 165 * Returns the error level of the message. 166 */ 167 int ErrorLevel() const; 168 169 /** 170 * Returns the index within the line of the first character where 171 * the error occurred. 172 */ 173 int GetStartColumn() const; 174 V8_WARN_UNUSED_RESULT Maybe<int> GetStartColumn(Local<Context> context) const; 175 176 /** 177 * Returns the index within the line of the last character where 178 * the error occurred. 179 */ 180 int GetEndColumn() const; 181 V8_WARN_UNUSED_RESULT Maybe<int> GetEndColumn(Local<Context> context) const; 182 183 /** 184 * Passes on the value set by the embedder when it fed the script from which 185 * this Message was generated to V8. 186 */ 187 bool IsSharedCrossOrigin() const; 188 bool IsOpaque() const; 189 190 static void PrintCurrentStackTrace(Isolate* isolate, std::ostream& out); 191 192 static const int kNoLineNumberInfo = 0; 193 static const int kNoColumnInfo = 0; 194 static const int kNoScriptIdInfo = 0; 195 static const int kNoWasmFunctionIndexInfo = -1; 196}; 197 198Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; } 199 200Local<Data> ScriptOrigin::GetHostDefinedOptions() const { 201 return host_defined_options_; 202} 203 204int ScriptOrigin::LineOffset() const { return resource_line_offset_; } 205 206int ScriptOrigin::ColumnOffset() const { return resource_column_offset_; } 207 208int ScriptOrigin::ScriptId() const { return script_id_; } 209 210Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; } 211 212} // namespace v8 213 214#endif // INCLUDE_V8_MESSAGE_H_ 215