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 "src/base/export-template.h" 11 #include "src/objects/fixed-array.h" 12 #include "src/objects/objects.h" 13 #include "src/objects/struct.h" 14 #include "torque-generated/bit-fields.h" 15 16 // Has to be the last include (doesn't have include guards): 17 #include "src/objects/object-macros.h" 18 19 namespace v8 { 20 21 namespace internal { 22 23 #include "torque-generated/src/objects/script-tq.inc" 24 25 // Script describes a script which has been added to the VM. 26 class Script : public TorqueGeneratedScript<Script, Struct> { 27 public: 28 // Script ID used for temporary scripts, which shouldn't be added to the 29 // script list. 30 static constexpr int kTemporaryScriptId = -2; 31 32 NEVER_READ_ONLY_SPACE 33 // Script types. 34 enum Type { 35 TYPE_NATIVE = 0, 36 TYPE_EXTENSION = 1, 37 TYPE_NORMAL = 2, 38 TYPE_WASM = 3, 39 TYPE_INSPECTOR = 4 40 }; 41 42 // Script compilation types. 43 enum CompilationType { COMPILATION_TYPE_HOST = 0, COMPILATION_TYPE_EVAL = 1 }; 44 45 // Script compilation state. 46 enum CompilationState { 47 COMPILATION_STATE_INITIAL = 0, 48 COMPILATION_STATE_COMPILED = 1 49 }; 50 51 // [type]: the script type. 52 DECL_INT_ACCESSORS(type) 53 54 DECL_ACCESSORS(eval_from_shared_or_wrapped_arguments, Object) 55 56 // [eval_from_shared]: for eval scripts the shared function info for the 57 // function from which eval was called. 58 DECL_ACCESSORS(eval_from_shared, SharedFunctionInfo) 59 60 // [wrapped_arguments]: for the list of arguments in a wrapped script. 61 DECL_ACCESSORS(wrapped_arguments, FixedArray) 62 63 // Whether the script is implicitly wrapped in a function. 64 inline bool is_wrapped() const; 65 66 // Whether the eval_from_shared field is set with a shared function info 67 // for the eval site. 68 inline bool has_eval_from_shared() const; 69 70 // [eval_from_position]: the source position in the code for the function 71 // from which eval was called, as positive integer. Or the code offset in the 72 // code from which eval was called, as negative integer. 73 DECL_INT_ACCESSORS(eval_from_position) 74 75 // [shared_function_infos]: weak fixed array containing all shared 76 // function infos created from this script. 77 DECL_ACCESSORS(shared_function_infos, WeakFixedArray) 78 79 // [wasm_breakpoint_infos]: the list of {BreakPointInfo} objects describing 80 // all WebAssembly breakpoints for modules/instances managed via this script. 81 // This must only be called if the type of this script is TYPE_WASM. 82 DECL_ACCESSORS(wasm_breakpoint_infos, FixedArray) 83 inline bool has_wasm_breakpoint_infos() const; 84 85 // [wasm_native_module]: the wasm {NativeModule} this script belongs to. 86 // This must only be called if the type of this script is TYPE_WASM. 87 DECL_ACCESSORS(wasm_managed_native_module, Object) 88 inline wasm::NativeModule* wasm_native_module() const; 89 90 // [wasm_weak_instance_list]: the list of all {WasmInstanceObject} being 91 // affected by breakpoints that are managed via this script. 92 // This must only be called if the type of this script is TYPE_WASM. 93 DECL_ACCESSORS(wasm_weak_instance_list, WeakArrayList) 94 95 // [compilation_type]: how the the script was compiled. Encoded in the 96 // 'flags' field. 97 inline CompilationType compilation_type(); 98 inline void set_compilation_type(CompilationType type); 99 100 // [compilation_state]: determines whether the script has already been 101 // compiled. Encoded in the 'flags' field. 102 inline CompilationState compilation_state(); 103 inline void set_compilation_state(CompilationState state); 104 105 // [is_repl_mode]: whether this script originated from a REPL via debug 106 // evaluate and therefore has different semantics, e.g. re-declaring let. 107 inline bool is_repl_mode() const; 108 inline void set_is_repl_mode(bool value); 109 110 // [origin_options]: optional attributes set by the embedder via ScriptOrigin, 111 // and used by the embedder to make decisions about the script. V8 just passes 112 // this through. Encoded in the 'flags' field. 113 inline v8::ScriptOriginOptions origin_options(); 114 inline void set_origin_options(ScriptOriginOptions origin_options); 115 116 // If script source is an external string, check that the underlying 117 // resource is accessible. Otherwise, always return true. 118 inline bool HasValidSource(); 119 120 Object GetNameOrSourceURL(); 121 122 // Retrieve source position from where eval was called. 123 static int GetEvalPosition(Isolate* isolate, Handle<Script> script); 124 125 // Check if the script contains any Asm modules. 126 bool ContainsAsmModule(); 127 128 // Init line_ends array with source code positions of line ends. 129 template <typename LocalIsolate> 130 EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) 131 static void InitLineEnds(LocalIsolate* isolate, Handle<Script> script); 132 133 // Carries information about a source position. 134 struct PositionInfo { PositionInfoPositionInfo135 PositionInfo() : line(-1), column(-1), line_start(-1), line_end(-1) {} 136 137 int line; // Zero-based line number. 138 int column; // Zero-based column number. 139 int line_start; // Position of first character in line. 140 int line_end; // Position of final linebreak character in line. 141 }; 142 143 // Specifies whether to add offsets to position infos. 144 enum OffsetFlag { NO_OFFSET = 0, WITH_OFFSET = 1 }; 145 146 // Retrieves information about the given position, optionally with an offset. 147 // Returns false on failure, and otherwise writes into the given info object 148 // on success. 149 // The static method should is preferable for handlified callsites because it 150 // initializes the line ends array, avoiding expensive recomputations. 151 // The non-static version is not allocating and safe for unhandlified 152 // callsites. 153 static bool GetPositionInfo(Handle<Script> script, int position, 154 PositionInfo* info, OffsetFlag offset_flag); 155 V8_EXPORT_PRIVATE bool GetPositionInfo(int position, PositionInfo* info, 156 OffsetFlag offset_flag) const; 157 158 bool IsUserJavaScript() const; 159 160 // Wrappers for GetPositionInfo 161 static int GetColumnNumber(Handle<Script> script, int code_offset); 162 int GetColumnNumber(int code_pos) const; 163 V8_EXPORT_PRIVATE static int GetLineNumber(Handle<Script> script, 164 int code_offset); 165 int GetLineNumber(int code_pos) const; 166 167 // Look through the list of existing shared function infos to find one 168 // that matches the function literal. Return empty handle if not found. 169 template <typename LocalIsolate> 170 MaybeHandle<SharedFunctionInfo> FindSharedFunctionInfo( 171 LocalIsolate* isolate, int function_literal_id); 172 173 // Iterate over all script objects on the heap. 174 class V8_EXPORT_PRIVATE Iterator { 175 public: 176 explicit Iterator(Isolate* isolate); 177 Iterator(const Iterator&) = delete; 178 Iterator& operator=(const Iterator&) = delete; 179 Script Next(); 180 181 private: 182 WeakArrayList::Iterator iterator_; 183 }; 184 185 // Dispatched behavior. 186 DECL_PRINTER(Script) 187 DECL_VERIFIER(Script) 188 189 private: 190 // Bit positions in the flags field. 191 DEFINE_TORQUE_GENERATED_SCRIPT_FLAGS() 192 193 TQ_OBJECT_CONSTRUCTORS(Script) 194 }; 195 196 } // namespace internal 197 } // namespace v8 198 199 #include "src/objects/object-macros-undef.h" 200 201 #endif // V8_OBJECTS_SCRIPT_H_ 202