v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
script.h
Go to the documentation of this file.
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"
13#include "src/heap/factory.h"
16#include "src/objects/objects.h"
17#include "src/objects/string.h"
18#include "src/objects/struct.h"
19#include "torque-generated/bit-fields.h"
20
21// Has to be the last include (doesn't have include guards):
23
24namespace v8 {
25
26namespace internal {
27
28class FunctionLiteral;
29class StructBodyDescriptor;
30
31namespace wasm {
32class NativeModule;
33} // namespace wasm
34
35#include "torque-generated/src/objects/script-tq.inc"
36
37// Script describes a script which has been added to the VM.
38class Script : public TorqueGeneratedScript<Script, Struct> {
39 public:
40 // Script ID used for temporary scripts, which shouldn't be added to the
41 // script list.
42 static constexpr int kTemporaryScriptId = -2;
43
45 // Script types.
46 enum class Type {
47 kNative = 0,
48 kExtension = 1,
49 kNormal = 2,
50#if V8_ENABLE_WEBASSEMBLY
51 kWasm = 3,
52#endif // V8_ENABLE_WEBASSEMBLY
53 kInspector = 4
54 };
55
56 // Script compilation types.
57 enum class CompilationType { kHost = 0, kEval = 1 };
58
59 // Script compilation state.
60 enum class CompilationState { kInitial = 0, kCompiled = 1 };
61
62 // [type]: the script type.
64
65 DECL_ACCESSORS(eval_from_shared_or_wrapped_arguments, Tagged<Object>)
66
67 // [eval_from_shared]: for eval scripts the shared function info for the
68 // function from which eval was called.
70
71 // [wrapped_arguments]: for the list of arguments in a wrapped script.
72 DECL_ACCESSORS(wrapped_arguments, Tagged<FixedArray>)
73
74 // Whether the script is implicitly wrapped in a function.
75 inline bool is_wrapped() const;
76
77 // Whether the eval_from_shared field is set with a shared function info
78 // for the eval site.
79 inline bool has_eval_from_shared() const;
80
81 // [eval_from_position]: the source position in the code for the function
82 // from which eval was called, as positive integer. Or the code offset in the
83 // code from which eval was called, as negative integer.
85
86 // [infos]: weak fixed array containing all shared function infos and scope
87 // infos for eval created from this script.
89
90#if V8_ENABLE_WEBASSEMBLY
91 // [wasm_breakpoint_infos]: the list of {BreakPointInfo} objects describing
92 // all WebAssembly breakpoints for modules/instances managed via this script.
93 // This must only be called if the type of this script is TYPE_WASM.
94 DECL_ACCESSORS(wasm_breakpoint_infos, Tagged<FixedArray>)
95 inline bool has_wasm_breakpoint_infos() const;
96
97 // [wasm_native_module]: the wasm {NativeModule} this script belongs to.
98 // This must only be called if the type of this script is TYPE_WASM.
99 DECL_ACCESSORS(wasm_managed_native_module, Tagged<Object>)
100 inline wasm::NativeModule* wasm_native_module() const;
101
102 // [wasm_weak_instance_list]: the list of all {WasmInstanceObject} being
103 // affected by breakpoints that are managed via this script.
104 // This must only be called if the type of this script is TYPE_WASM.
105 DECL_ACCESSORS(wasm_weak_instance_list, Tagged<WeakArrayList>)
106
107 // [break_on_entry] (wasm only): whether an instrumentation breakpoint is set
108 // for this script; this information will be transferred to existing and
109 // future instances to make sure that we stop before executing any code in
110 // this wasm module.
111 inline bool break_on_entry() const;
112 inline void set_break_on_entry(bool value);
113
114 // Check if the script contains any Asm modules.
115 bool ContainsAsmModule();
116#endif // V8_ENABLE_WEBASSEMBLY
117
118 // Read/write the raw 'flags' field. This uses relaxed atomic loads/stores
119 // because the flags are read by background compile threads and updated by the
120 // main thread.
121 inline uint32_t flags() const;
122 inline void set_flags(uint32_t new_flags);
123
124 // [compilation_type]: how the the script was compiled. Encoded in the
125 // 'flags' field.
126 inline CompilationType compilation_type() const;
127 inline void set_compilation_type(CompilationType type);
128
129 inline bool produce_compile_hints() const;
131
132 inline bool deserialized() const;
133 inline void set_deserialized(bool value);
134
135 // [compilation_state]: determines whether the script has already been
136 // compiled. Encoded in the 'flags' field.
138 inline void set_compilation_state(CompilationState state);
139
140 // [is_repl_mode]: whether this script originated from a REPL via debug
141 // evaluate and therefore has different semantics, e.g. re-declaring let.
142 inline bool is_repl_mode() const;
143 inline void set_is_repl_mode(bool value);
144
145 // [origin_options]: optional attributes set by the embedder via ScriptOrigin,
146 // and used by the embedder to make decisions about the script. V8 just passes
147 // this through. Encoded in the 'flags' field.
150
151 DECL_ACCESSORS(compiled_lazy_function_positions, Tagged<Object>)
152
153 // If script source is an external string, check that the underlying
154 // resource is accessible. Otherwise, always return true.
155 inline bool HasValidSource();
156
157 // If the script has a non-empty sourceURL comment.
158 inline bool HasSourceURLComment() const;
159
160 // If the script has a non-empty sourceMappingURL comment.
161 inline bool HasSourceMappingURLComment() const;
162
163 // Streaming compilation only attaches the source to the Script upon
164 // finalization. This predicate returns true, if this script may still be
165 // unfinalized.
166 inline bool IsMaybeUnfinalized(Isolate* isolate) const;
167
171 bool forceForInspector);
172
173 // Retrieve source position from where eval was called.
174 static int GetEvalPosition(Isolate* isolate, DirectHandle<Script> script);
175
177
178 // Initialize line_ends array with source code positions of line ends if
179 // it doesn't exist yet.
180 static inline void InitLineEnds(Isolate* isolate,
181 DirectHandle<Script> script);
182 static inline void InitLineEnds(LocalIsolate* isolate,
183 DirectHandle<Script> script);
184
185 // Obtain line ends as a vector, without modifying the script object
187 Isolate* isolate, DirectHandle<Script> script);
188
189 inline bool has_line_ends() const;
190
191 // Will initialize the line ends if required.
192 static void SetSource(Isolate* isolate, DirectHandle<Script> script,
193 DirectHandle<String> source);
194
195 bool inline CanHaveLineEnds() const;
196
197 // Carries information about a source position.
199 PositionInfo() : line(-1), column(-1), line_start(-1), line_end(-1) {}
200
201 int line; // Zero-based line number.
202 int column; // Zero-based column number.
203 int line_start; // Position of first character in line.
204 int line_end; // Position of final linebreak character in line.
205 };
206
207 // Specifies whether to add offsets to position infos.
209
210 // Retrieves information about the given position, optionally with an offset.
211 // Returns false on failure, and otherwise writes into the given info object
212 // on success.
213 // The static method should is preferable for handlified callsites because it
214 // initializes the line ends array, avoiding expensive recomputations.
215 // The non-static version is not allocating and safe for unhandlified
216 // callsites.
217 static bool GetPositionInfo(DirectHandle<Script> script, int position,
218 PositionInfo* info,
219 OffsetFlag offset_flag = OffsetFlag::kWithOffset);
220 static bool GetLineColumnWithLineEnds(
221 int position, int& line, int& column,
222 const String::LineEndsVector& line_ends);
224 int position, PositionInfo* info,
225 OffsetFlag offset_flag = OffsetFlag::kWithOffset) const;
227 int position, PositionInfo* info, const String::LineEndsVector& line_ends,
228 OffsetFlag offset_flag = OffsetFlag::kWithOffset) const;
230 PositionInfo* info,
231 OffsetFlag offset_flag = OffsetFlag::kWithOffset) const;
232
233 // Tells whether this script should be subject to debugging, e.g. for
234 // - scope inspection
235 // - internal break points
236 // - coverage and type profile
237 // - error stack trace
238 bool IsSubjectToDebugging() const;
239
240 bool IsUserJavaScript() const;
241
242 void TraceScriptRundown();
244
245 // Wrappers for GetPositionInfo
246 static int GetColumnNumber(DirectHandle<Script> script, int code_offset);
247 int GetColumnNumber(int code_pos) const;
249 int code_offset);
250 int GetLineNumber(int code_pos) const;
251
252 // Look through the list of existing shared function infos to find one
253 // that matches the function literal. Return empty handle if not found.
254 template <typename IsolateT>
256 DirectHandle<Script> script, IsolateT* isolate,
257 FunctionLiteral* function_literal);
258
259 // Iterate over all script objects on the heap.
261 public:
262 explicit Iterator(Isolate* isolate);
263 Iterator(const Iterator&) = delete;
264 Iterator& operator=(const Iterator&) = delete;
265 Tagged<Script> Next();
266
267 private:
269 };
270
271 // Dispatched behavior.
274
276
277 private:
278 template <typename LineEndsContainer>
279 bool GetPositionInfoInternal(const LineEndsContainer& ends, int position,
280 Script::PositionInfo* info,
281 const DisallowGarbageCollection& no_gc) const;
282
283 friend Factory;
286
287 // Hide torque-generated accessor, use Script::SetSource instead.
288 using TorqueGeneratedScript::set_source;
289
290 // Bit positions in the flags field.
291 DEFINE_TORQUE_GENERATED_SCRIPT_FLAGS()
292
294
295 template <typename IsolateT>
297 static void V8_PRESERVE_MOST
298 InitLineEndsInternal(IsolateT* isolate, DirectHandle<Script> script);
299};
300
301} // namespace internal
302} // namespace v8
303
304#include "src/objects/object-macros-undef.h"
305
306#endif // V8_OBJECTS_SCRIPT_H_
WeakArrayList::Iterator iterator_
Definition script.h:268
Iterator & operator=(const Iterator &)=delete
Iterator(const Iterator &)=delete
V8_EXPORT_PRIVATE bool GetPositionInfoWithLineEnds(int position, PositionInfo *info, const String::LineEndsVector &line_ends, OffsetFlag offset_flag=OffsetFlag::kWithOffset) const
Definition objects.cc:4658
void set_origin_options(ScriptOriginOptions origin_options)
Definition script-inl.h:172
bool CanHaveLineEnds() const
Definition script-inl.h:192
CompilationState compilation_state()
Definition script-inl.h:142
void set_is_repl_mode(bool value)
Definition script-inl.h:165
void set_deserialized(bool value)
Definition script-inl.h:159
static void InitLineEnds(Isolate *isolate, DirectHandle< Script > script)
Definition script-inl.h:201
static V8_EXPORT_PRIVATE String::LineEndsVector GetLineEnds(Isolate *isolate, DirectHandle< Script > script)
Definition objects.cc:4316
static DirectHandle< String > GetScriptHash(Isolate *isolate, DirectHandle< Script > script, bool forceForInspector)
Definition objects.cc:4717
bool produce_compile_hints() const
Definition script-inl.h:149
CompilationType compilation_type() const
Definition script-inl.h:136
V8_EXPORT_PRIVATE void AddPositionInfoOffset(PositionInfo *info, OffsetFlag offset_flag=OffsetFlag::kWithOffset) const
Definition objects.cc:4581
void set_produce_compile_hints(bool produce_compile_hints)
Definition script-inl.h:153
bool IsMaybeUnfinalized(Isolate *isolate) const
Definition script-inl.h:220
bool IsSubjectToDebugging() const
Definition objects.cc:4383
bool GetPositionInfoInternal(const LineEndsContainer &ends, int position, Script::PositionInfo *info, const DisallowGarbageCollection &no_gc) const
Definition objects.cc:4595
static constexpr int kTemporaryScriptId
Definition script.h:42
static V8_EXPORT_PRIVATE int GetLineNumber(DirectHandle< Script > script, int code_offset)
Definition objects.cc:4698
void TraceScriptRundownSources()
Definition objects.cc:4443
bool has_line_ends() const
Definition script-inl.h:190
bool HasSourceURLComment() const
Definition script-inl.h:211
bool is_wrapped() const
static MaybeHandle< SharedFunctionInfo > FindSharedFunctionInfo(DirectHandle< Script > script, IsolateT *isolate, FunctionLiteral *function_literal)
Definition objects.cc:4763
static void SetSource(Isolate *isolate, DirectHandle< Script > script, DirectHandle< String > source)
Definition objects.cc:4349
static void V8_PRESERVE_MOST InitLineEndsInternal(IsolateT *isolate, DirectHandle< Script > script)
Tagged< Object > GetNameOrSourceURL()
Definition objects.cc:4710
bool deserialized() const
Definition script-inl.h:157
void set_flags(uint32_t new_flags)
Definition script-inl.h:131
v8::ScriptOriginOptions origin_options()
Definition script-inl.h:169
bool IsUserJavaScript() const
Definition objects.cc:4398
uint32_t flags() const
Definition script-inl.h:124
static bool GetPositionInfo(DirectHandle< Script > script, int position, PositionInfo *info, OffsetFlag offset_flag=OffsetFlag::kWithOffset)
Definition objects.cc:4367
void set_compilation_type(CompilationType type)
Definition script-inl.h:139
bool HasSourceMappingURLComment() const
Definition script-inl.h:215
static int GetColumnNumber(DirectHandle< Script > script, int code_offset)
Definition objects.cc:4686
void set_compilation_state(CompilationState state)
Definition script-inl.h:145
bool has_eval_from_shared() const
static int GetEvalPosition(Isolate *isolate, DirectHandle< Script > script)
Definition objects.cc:4294
static bool GetLineColumnWithLineEnds(int position, int &line, int &column, const String::LineEndsVector &line_ends)
Definition objects.cc:4669
bool is_repl_mode() const
Definition script-inl.h:163
Tagged< Script > GetEvalOrigin()
Definition script-inl.h:227
#define EXPORT_TEMPLATE_DECLARE(export)
int position
Definition liveedit.cc:290
Definition c-api.cc:87
#define DECL_ACCESSORS(name,...)
#define DECL_VERIFIER(Name)
#define NEVER_READ_ONLY_SPACE
#define DECL_PRIMITIVE_ACCESSORS(name, type)
#define DECL_PRINTER(Name)
#define DECL_INT_ACCESSORS(name)
#define TQ_OBJECT_CONSTRUCTORS(Type)
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_PRESERVE_MOST
Definition v8config.h:598