1 // Copyright 2017 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 V8_OBJECTS_SCRIPT_H_ 6 #define V8_OBJECTS_SCRIPT_H_ 7 8 #include <memory> 9 10 #include "include/v8-script.h" 11 #include "src/base/export-template.h" 12 #include "src/objects/fixed-array.h" 13 #include "src/objects/objects.h" 14 #include "src/objects/struct.h" 15 #include "torque-generated/bit-fields.h" 16 17 // Has to be the last include (doesn't have include guards): 18 #include "src/objects/object-macros.h" 19 20 namespace v8 { 21 22 namespace internal { 23 24 class FunctionLiteral; 25 class StructBodyDescriptor; 26 27 namespace wasm { 28 class NativeModule; 29 } // namespace wasm 30 31 #include "torque-generated/src/objects/script-tq.inc" 32 33 // Script describes a script which has been added to the VM. 34 class Script : public TorqueGeneratedScript<Script, Struct> { 35 public: 36 // Script ID used for temporary scripts, which shouldn't be added to the 37 // script list. 38 static constexpr int kTemporaryScriptId = -2; 39 40 NEVER_READ_ONLY_SPACE 41 // Script types. 42 enum Type { 43 TYPE_NATIVE = 0, 44 TYPE_EXTENSION = 1, 45 TYPE_NORMAL = 2, 46 #if V8_ENABLE_WEBASSEMBLY 47 TYPE_WASM = 3, 48 #endif // V8_ENABLE_WEBASSEMBLY 49 TYPE_INSPECTOR = 4, 50 TYPE_WEB_SNAPSHOT = 5 51 }; 52 53 // Script compilation types. 54 enum CompilationType { COMPILATION_TYPE_HOST = 0, COMPILATION_TYPE_EVAL = 1 }; 55 56 // Script compilation state. 57 enum CompilationState { 58 COMPILATION_STATE_INITIAL = 0, 59 COMPILATION_STATE_COMPILED = 1 60 }; 61 62 // [type]: the script type. 63 DECL_INT_ACCESSORS(type) 64 65 DECL_ACCESSORS(eval_from_shared_or_wrapped_arguments_or_sfi_table, Object) 66 67 // [eval_from_shared]: for eval scripts the shared function info for the 68 // function from which eval was called. 69 DECL_ACCESSORS(eval_from_shared, SharedFunctionInfo) 70 71 // [wrapped_arguments]: for the list of arguments in a wrapped script. 72 DECL_ACCESSORS(wrapped_arguments, FixedArray) 73 74 // For web snapshots: a hash table mapping function positions to indices in 75 // shared_function_infos. 76 // TODO(v8:11525): Replace with a more efficient data structure mapping 77 // function positions to weak pointers to SharedFunctionInfos directly. 78 DECL_ACCESSORS(shared_function_info_table, ObjectHashTable) 79 80 // Whether the script is implicitly wrapped in a function. 81 inline bool is_wrapped() const; 82 83 // Whether the eval_from_shared field is set with a shared function info 84 // for the eval site. 85 inline bool has_eval_from_shared() const; 86 87 // [eval_from_position]: the source position in the code for the function 88 // from which eval was called, as positive integer. Or the code offset in the 89 // code from which eval was called, as negative integer. 90 DECL_INT_ACCESSORS(eval_from_position) 91 92 // [shared_function_infos]: weak fixed array containing all shared 93 // function infos created from this script. 94 DECL_ACCESSORS(shared_function_infos, WeakFixedArray) 95 96 inline int shared_function_info_count() const; 97 98 #if V8_ENABLE_WEBASSEMBLY 99 // [wasm_breakpoint_infos]: the list of {BreakPointInfo} objects describing 100 // all WebAssembly breakpoints for modules/instances managed via this script. 101 // This must only be called if the type of this script is TYPE_WASM. 102 DECL_ACCESSORS(wasm_breakpoint_infos, FixedArray) 103 inline bool has_wasm_breakpoint_infos() const; 104 105 // [wasm_native_module]: the wasm {NativeModule} this script belongs to. 106 // This must only be called if the type of this script is TYPE_WASM. 107 DECL_ACCESSORS(wasm_managed_native_module, Object) 108 inline wasm::NativeModule* wasm_native_module() const; 109 110 // [wasm_weak_instance_list]: the list of all {WasmInstanceObject} being 111 // affected by breakpoints that are managed via this script. 112 // This must only be called if the type of this script is TYPE_WASM. 113 DECL_ACCESSORS(wasm_weak_instance_list, WeakArrayList) 114 115 // [break_on_entry] (wasm only): whether an instrumentation breakpoint is set 116 // for this script; this information will be transferred to existing and 117 // future instances to make sure that we stop before executing any code in 118 // this wasm module. 119 inline bool break_on_entry() const; 120 inline void set_break_on_entry(bool value); 121 122 // Check if the script contains any Asm modules. 123 bool ContainsAsmModule(); 124 #endif // V8_ENABLE_WEBASSEMBLY 125 126 // [compilation_type]: how the the script was compiled. Encoded in the 127 // 'flags' field. 128 inline CompilationType compilation_type(); 129 inline void set_compilation_type(CompilationType type); 130 131 // [compilation_state]: determines whether the script has already been 132 // compiled. Encoded in the 'flags' field. 133 inline CompilationState compilation_state(); 134 inline void set_compilation_state(CompilationState state); 135 136 // [is_repl_mode]: whether this script originated from a REPL via debug 137 // evaluate and therefore has different semantics, e.g. re-declaring let. 138 inline bool is_repl_mode() const; 139 inline void set_is_repl_mode(bool value); 140 141 // [origin_options]: optional attributes set by the embedder via ScriptOrigin, 142 // and used by the embedder to make decisions about the script. V8 just passes 143 // this through. Encoded in the 'flags' field. 144 inline v8::ScriptOriginOptions origin_options(); 145 inline void set_origin_options(ScriptOriginOptions origin_options); 146 147 // If script source is an external string, check that the underlying 148 // resource is accessible. Otherwise, always return true. 149 inline bool HasValidSource(); 150 151 // If the script has a non-empty sourceURL comment. 152 inline bool HasSourceURLComment() const; 153 154 // Streaming compilation only attaches the source to the Script upon 155 // finalization. This predicate returns true, if this script may still be 156 // unfinalized. 157 inline bool IsMaybeUnfinalized(Isolate* isolate) const; 158 159 Object GetNameOrSourceURL(); 160 161 // Retrieve source position from where eval was called. 162 static int GetEvalPosition(Isolate* isolate, Handle<Script> script); 163 164 // Init line_ends array with source code positions of line ends. 165 template <typename IsolateT> 166 EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) 167 static void InitLineEnds(IsolateT* isolate, Handle<Script> script); 168 169 // Carries information about a source position. 170 struct PositionInfo { PositionInfoPositionInfo171 PositionInfo() : line(-1), column(-1), line_start(-1), line_end(-1) {} 172 173 int line; // Zero-based line number. 174 int column; // Zero-based column number. 175 int line_start; // Position of first character in line. 176 int line_end; // Position of final linebreak character in line. 177 }; 178 179 // Specifies whether to add offsets to position infos. 180 enum OffsetFlag { NO_OFFSET = 0, WITH_OFFSET = 1 }; 181 182 // Retrieves information about the given position, optionally with an offset. 183 // Returns false on failure, and otherwise writes into the given info object 184 // on success. 185 // The static method should is preferable for handlified callsites because it 186 // initializes the line ends array, avoiding expensive recomputations. 187 // The non-static version is not allocating and safe for unhandlified 188 // callsites. 189 static bool GetPositionInfo(Handle<Script> script, int position, 190 PositionInfo* info, OffsetFlag offset_flag); 191 V8_EXPORT_PRIVATE bool GetPositionInfo(int position, PositionInfo* info, 192 OffsetFlag offset_flag) const; 193 194 // Tells whether this script should be subject to debugging, e.g. for 195 // - scope inspection 196 // - internal break points 197 // - coverage and type profile 198 // - error stack trace 199 bool IsSubjectToDebugging() const; 200 201 bool IsUserJavaScript() const; 202 203 // Wrappers for GetPositionInfo 204 static int GetColumnNumber(Handle<Script> script, int code_offset); 205 int GetColumnNumber(int code_pos) const; 206 V8_EXPORT_PRIVATE static int GetLineNumber(Handle<Script> script, 207 int code_offset); 208 int GetLineNumber(int code_pos) const; 209 210 // Look through the list of existing shared function infos to find one 211 // that matches the function literal. Return empty handle if not found. 212 template <typename IsolateT> 213 static MaybeHandle<SharedFunctionInfo> FindSharedFunctionInfo( 214 Handle<Script> script, IsolateT* isolate, 215 FunctionLiteral* function_literal); 216 217 static MaybeHandle<SharedFunctionInfo> FindWebSnapshotSharedFunctionInfo( 218 Handle<Script> script, Isolate* isolate, 219 FunctionLiteral* function_literal); 220 221 static MaybeHandle<SharedFunctionInfo> FindWebSnapshotSharedFunctionInfo( 222 Handle<Script> script, LocalIsolate* isolate, 223 FunctionLiteral* function_literal); 224 225 // Iterate over all script objects on the heap. 226 class V8_EXPORT_PRIVATE Iterator { 227 public: 228 explicit Iterator(Isolate* isolate); 229 Iterator(const Iterator&) = delete; 230 Iterator& operator=(const Iterator&) = delete; 231 Script Next(); 232 233 private: 234 WeakArrayList::Iterator iterator_; 235 }; 236 237 // Dispatched behavior. 238 DECL_PRINTER(Script) 239 DECL_VERIFIER(Script) 240 241 using BodyDescriptor = StructBodyDescriptor; 242 243 private: 244 // Bit positions in the flags field. 245 DEFINE_TORQUE_GENERATED_SCRIPT_FLAGS() 246 247 TQ_OBJECT_CONSTRUCTORS(Script) 248 }; 249 250 } // namespace internal 251 } // namespace v8 252 253 #include "src/objects/object-macros-undef.h" 254 255 #endif // V8_OBJECTS_SCRIPT_H_ 256