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