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 "src/objects.h" |
9 | #include "src/objects/fixed-array.h" |
10 | #include "src/objects/struct.h" |
11 | |
12 | // Has to be the last include (doesn't have include guards): |
13 | #include "src/objects/object-macros.h" |
14 | |
15 | namespace v8 { |
16 | |
17 | namespace tracing { |
18 | class TracedValue; |
19 | } |
20 | |
21 | namespace internal { |
22 | |
23 | // Script describes a script which has been added to the VM. |
24 | class Script : public Struct { |
25 | public: |
26 | NEVER_READ_ONLY_SPACE |
27 | // Script types. |
28 | enum Type { |
29 | TYPE_NATIVE = 0, |
30 | TYPE_EXTENSION = 1, |
31 | TYPE_NORMAL = 2, |
32 | TYPE_WASM = 3, |
33 | TYPE_INSPECTOR = 4 |
34 | }; |
35 | |
36 | // Script compilation types. |
37 | enum CompilationType { COMPILATION_TYPE_HOST = 0, COMPILATION_TYPE_EVAL = 1 }; |
38 | |
39 | // Script compilation state. |
40 | enum CompilationState { |
41 | COMPILATION_STATE_INITIAL = 0, |
42 | COMPILATION_STATE_COMPILED = 1 |
43 | }; |
44 | |
45 | // [source]: the script source. |
46 | DECL_ACCESSORS(source, Object) |
47 | |
48 | // [name]: the script name. |
49 | DECL_ACCESSORS(name, Object) |
50 | |
51 | // [id]: the script id. |
52 | DECL_INT_ACCESSORS(id) |
53 | |
54 | // [line_offset]: script line offset in resource from where it was extracted. |
55 | DECL_INT_ACCESSORS(line_offset) |
56 | |
57 | // [column_offset]: script column offset in resource from where it was |
58 | // extracted. |
59 | DECL_INT_ACCESSORS(column_offset) |
60 | |
61 | // [context_data]: context data for the context this script was compiled in. |
62 | DECL_ACCESSORS(context_data, Object) |
63 | |
64 | // [type]: the script type. |
65 | DECL_INT_ACCESSORS(type) |
66 | |
67 | // [line_ends]: FixedArray of line ends positions. |
68 | DECL_ACCESSORS(line_ends, Object) |
69 | |
70 | DECL_ACCESSORS(eval_from_shared_or_wrapped_arguments, Object) |
71 | |
72 | // [eval_from_shared]: for eval scripts the shared function info for the |
73 | // function from which eval was called. |
74 | DECL_ACCESSORS(eval_from_shared, SharedFunctionInfo) |
75 | |
76 | // [wrapped_arguments]: for the list of arguments in a wrapped script. |
77 | DECL_ACCESSORS(wrapped_arguments, FixedArray) |
78 | |
79 | // Whether the script is implicitly wrapped in a function. |
80 | inline bool is_wrapped() const; |
81 | |
82 | // Whether the eval_from_shared field is set with a shared function info |
83 | // for the eval site. |
84 | inline bool has_eval_from_shared() const; |
85 | |
86 | // [eval_from_position]: the source position in the code for the function |
87 | // from which eval was called, as positive integer. Or the code offset in the |
88 | // code from which eval was called, as negative integer. |
89 | DECL_INT_ACCESSORS(eval_from_position) |
90 | |
91 | // [shared_function_infos]: weak fixed array containing all shared |
92 | // function infos created from this script. |
93 | DECL_ACCESSORS(shared_function_infos, WeakFixedArray) |
94 | |
95 | // [flags]: Holds an exciting bitfield. |
96 | DECL_INT_ACCESSORS(flags) |
97 | |
98 | // [source_url]: sourceURL from magic comment |
99 | DECL_ACCESSORS(source_url, Object) |
100 | |
101 | // [source_mapping_url]: sourceMappingURL magic comment |
102 | DECL_ACCESSORS(source_mapping_url, Object) |
103 | |
104 | // [wasm_module_object]: the wasm module object this script belongs to. |
105 | // This must only be called if the type of this script is TYPE_WASM. |
106 | DECL_ACCESSORS(wasm_module_object, Object) |
107 | |
108 | // [host_defined_options]: Options defined by the embedder. |
109 | DECL_ACCESSORS(host_defined_options, FixedArray) |
110 | |
111 | // [compilation_type]: how the the script was compiled. Encoded in the |
112 | // 'flags' field. |
113 | inline CompilationType compilation_type(); |
114 | inline void set_compilation_type(CompilationType type); |
115 | |
116 | // [compilation_state]: determines whether the script has already been |
117 | // compiled. Encoded in the 'flags' field. |
118 | inline CompilationState compilation_state(); |
119 | inline void set_compilation_state(CompilationState state); |
120 | |
121 | // [origin_options]: optional attributes set by the embedder via ScriptOrigin, |
122 | // and used by the embedder to make decisions about the script. V8 just passes |
123 | // this through. Encoded in the 'flags' field. |
124 | inline v8::ScriptOriginOptions origin_options(); |
125 | inline void set_origin_options(ScriptOriginOptions origin_options); |
126 | |
127 | DECL_CAST(Script) |
128 | |
129 | // If script source is an external string, check that the underlying |
130 | // resource is accessible. Otherwise, always return true. |
131 | inline bool HasValidSource(); |
132 | |
133 | Object GetNameOrSourceURL(); |
134 | |
135 | // Retrieve source position from where eval was called. |
136 | static int GetEvalPosition(Isolate* isolate, Handle<Script> script); |
137 | |
138 | // Check if the script contains any Asm modules. |
139 | bool ContainsAsmModule(); |
140 | |
141 | // Init line_ends array with source code positions of line ends. |
142 | V8_EXPORT_PRIVATE static void InitLineEnds(Handle<Script> script); |
143 | |
144 | // Carries information about a source position. |
145 | struct PositionInfo { |
146 | PositionInfo() : line(-1), column(-1), line_start(-1), line_end(-1) {} |
147 | |
148 | int line; // Zero-based line number. |
149 | int column; // Zero-based column number. |
150 | int line_start; // Position of first character in line. |
151 | int line_end; // Position of final linebreak character in line. |
152 | }; |
153 | |
154 | // Specifies whether to add offsets to position infos. |
155 | enum OffsetFlag { NO_OFFSET = 0, WITH_OFFSET = 1 }; |
156 | |
157 | // Retrieves information about the given position, optionally with an offset. |
158 | // Returns false on failure, and otherwise writes into the given info object |
159 | // on success. |
160 | // The static method should is preferable for handlified callsites because it |
161 | // initializes the line ends array, avoiding expensive recomputations. |
162 | // The non-static version is not allocating and safe for unhandlified |
163 | // callsites. |
164 | static bool GetPositionInfo(Handle<Script> script, int position, |
165 | PositionInfo* info, OffsetFlag offset_flag); |
166 | V8_EXPORT_PRIVATE bool GetPositionInfo(int position, PositionInfo* info, |
167 | OffsetFlag offset_flag) const; |
168 | |
169 | bool IsUserJavaScript(); |
170 | |
171 | // Wrappers for GetPositionInfo |
172 | static int GetColumnNumber(Handle<Script> script, int code_offset); |
173 | int GetColumnNumber(int code_pos) const; |
174 | V8_EXPORT_PRIVATE static int GetLineNumber(Handle<Script> script, |
175 | int code_offset); |
176 | int GetLineNumber(int code_pos) const; |
177 | |
178 | // Look through the list of existing shared function infos to find one |
179 | // that matches the function literal. Return empty handle if not found. |
180 | MaybeHandle<SharedFunctionInfo> FindSharedFunctionInfo( |
181 | Isolate* isolate, const FunctionLiteral* fun); |
182 | |
183 | // Returns the Script in a format tracing can support. |
184 | std::unique_ptr<v8::tracing::TracedValue> ToTracedValue(); |
185 | |
186 | // The tracing scope for Script objects. |
187 | static const char* kTraceScope; |
188 | |
189 | // Returns the unique TraceID for this Script (within the kTraceScope). |
190 | uint64_t TraceID() const; |
191 | |
192 | // Returns the unique trace ID reference for this Script. |
193 | std::unique_ptr<v8::tracing::TracedValue> TraceIDRef() const; |
194 | |
195 | // Iterate over all script objects on the heap. |
196 | class V8_EXPORT_PRIVATE Iterator { |
197 | public: |
198 | explicit Iterator(Isolate* isolate); |
199 | Script Next(); |
200 | |
201 | private: |
202 | WeakArrayList::Iterator iterator_; |
203 | DISALLOW_COPY_AND_ASSIGN(Iterator); |
204 | }; |
205 | |
206 | // Dispatched behavior. |
207 | DECL_PRINTER(Script) |
208 | DECL_VERIFIER(Script) |
209 | |
210 | DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize, |
211 | TORQUE_GENERATED_SCRIPT_FIELDS) |
212 | |
213 | private: |
214 | // Bit positions in the flags field. |
215 | static const int kCompilationTypeBit = 0; |
216 | static const int kCompilationStateBit = 1; |
217 | static const int kOriginOptionsShift = 2; |
218 | static const int kOriginOptionsSize = 4; |
219 | static const int kOriginOptionsMask = ((1 << kOriginOptionsSize) - 1) |
220 | << kOriginOptionsShift; |
221 | |
222 | OBJECT_CONSTRUCTORS(Script, Struct); |
223 | }; |
224 | |
225 | } // namespace internal |
226 | } // namespace v8 |
227 | |
228 | #include "src/objects/object-macros-undef.h" |
229 | |
230 | #endif // V8_OBJECTS_SCRIPT_H_ |
231 | |