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