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