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