script.h 9.24 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_SCRIPT_H_
#define V8_OBJECTS_SCRIPT_H_

8 9
#include <memory>

10
#include "include/v8-script.h"
11
#include "src/base/export-template.h"
12
#include "src/objects/fixed-array.h"
13
#include "src/objects/objects.h"
14
#include "src/objects/struct.h"
15
#include "torque-generated/bit-fields.h"
16 17 18 19 20

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
21

22 23
namespace internal {

24
class FunctionLiteral;
25
class StructBodyDescriptor;
26

27 28 29 30
namespace wasm {
class NativeModule;
}  // namespace wasm

31 32
#include "torque-generated/src/objects/script-tq.inc"

33
// Script describes a script which has been added to the VM.
34
class Script : public TorqueGeneratedScript<Script, Struct> {
35
 public:
36 37 38 39
  // Script ID used for temporary scripts, which shouldn't be added to the
  // script list.
  static constexpr int kTemporaryScriptId = -2;

40
  NEVER_READ_ONLY_SPACE
41 42 43 44 45
  // Script types.
  enum Type {
    TYPE_NATIVE = 0,
    TYPE_EXTENSION = 1,
    TYPE_NORMAL = 2,
46
#if V8_ENABLE_WEBASSEMBLY
47
    TYPE_WASM = 3,
48
#endif  // V8_ENABLE_WEBASSEMBLY
49 50
    TYPE_INSPECTOR = 4,
    TYPE_WEB_SNAPSHOT = 5
51 52 53 54 55 56 57 58 59 60 61 62 63 64
  };

  // Script compilation types.
  enum CompilationType { COMPILATION_TYPE_HOST = 0, COMPILATION_TYPE_EVAL = 1 };

  // Script compilation state.
  enum CompilationState {
    COMPILATION_STATE_INITIAL = 0,
    COMPILATION_STATE_COMPILED = 1
  };

  // [type]: the script type.
  DECL_INT_ACCESSORS(type)

65
  DECL_ACCESSORS(eval_from_shared_or_wrapped_arguments_or_sfi_table, Object)
66

67 68
  // [eval_from_shared]: for eval scripts the shared function info for the
  // 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 75 76 77 78 79
  // For web snapshots: a hash table mapping function positions to indices in
  // shared_function_infos.
  // TODO(v8:11525): Replace with a more efficient data structure mapping
  // function positions to weak pointers to SharedFunctionInfos directly.
  DECL_ACCESSORS(shared_function_info_table, ObjectHashTable)

80 81 82 83 84 85
  // Whether the script is implicitly wrapped in a function.
  inline bool is_wrapped() const;

  // Whether the eval_from_shared field is set with a shared function info
  // for the eval site.
  inline bool has_eval_from_shared() const;
86 87 88 89 90 91 92 93

  // [eval_from_position]: the source position in the code for the function
  // from which eval was called, as positive integer. Or the code offset in the
  // code from which eval was called, as negative integer.
  DECL_INT_ACCESSORS(eval_from_position)

  // [shared_function_infos]: weak fixed array containing all shared
  // function infos created from this script.
94
  DECL_ACCESSORS(shared_function_infos, WeakFixedArray)
95

96 97
  inline int shared_function_info_count() const;

98
#if V8_ENABLE_WEBASSEMBLY
99 100
  // [wasm_breakpoint_infos]: the list of {BreakPointInfo} objects describing
  // all WebAssembly breakpoints for modules/instances managed via this script.
101
  // This must only be called if the type of this script is TYPE_WASM.
102 103
  DECL_ACCESSORS(wasm_breakpoint_infos, FixedArray)
  inline bool has_wasm_breakpoint_infos() const;
104

105 106 107 108 109
  // [wasm_native_module]: the wasm {NativeModule} this script belongs to.
  // This must only be called if the type of this script is TYPE_WASM.
  DECL_ACCESSORS(wasm_managed_native_module, Object)
  inline wasm::NativeModule* wasm_native_module() const;

110 111 112 113 114
  // [wasm_weak_instance_list]: the list of all {WasmInstanceObject} being
  // affected by breakpoints that are managed via this script.
  // This must only be called if the type of this script is TYPE_WASM.
  DECL_ACCESSORS(wasm_weak_instance_list, WeakArrayList)

115 116 117 118 119 120 121 122 123 124 125
  // [break_on_entry] (wasm only): whether an instrumentation breakpoint is set
  // for this script; this information will be transferred to existing and
  // future instances to make sure that we stop before executing any code in
  // this wasm module.
  inline bool break_on_entry() const;
  inline void set_break_on_entry(bool value);

  // Check if the script contains any Asm modules.
  bool ContainsAsmModule();
#endif  // V8_ENABLE_WEBASSEMBLY

126 127 128 129 130 131 132 133 134 135
  // [compilation_type]: how the the script was compiled. Encoded in the
  // 'flags' field.
  inline CompilationType compilation_type();
  inline void set_compilation_type(CompilationType type);

  // [compilation_state]: determines whether the script has already been
  // compiled. Encoded in the 'flags' field.
  inline CompilationState compilation_state();
  inline void set_compilation_state(CompilationState state);

Simon Zünd's avatar
Simon Zünd committed
136 137 138 139 140
  // [is_repl_mode]: whether this script originated from a REPL via debug
  // evaluate and therefore has different semantics, e.g. re-declaring let.
  inline bool is_repl_mode() const;
  inline void set_is_repl_mode(bool value);

141 142 143 144 145 146 147 148 149 150
  // [origin_options]: optional attributes set by the embedder via ScriptOrigin,
  // and used by the embedder to make decisions about the script. V8 just passes
  // this through. Encoded in the 'flags' field.
  inline v8::ScriptOriginOptions origin_options();
  inline void set_origin_options(ScriptOriginOptions origin_options);

  // If script source is an external string, check that the underlying
  // resource is accessible. Otherwise, always return true.
  inline bool HasValidSource();

151 152 153
  // If the script has a non-empty sourceURL comment.
  inline bool HasSourceURLComment() const;

154 155 156 157 158
  // Streaming compilation only attaches the source to the Script upon
  // finalization. This predicate returns true, if this script may still be
  // unfinalized.
  inline bool IsMaybeUnfinalized(Isolate* isolate) const;

159
  Object GetNameOrSourceURL();
160 161
  static Handle<String> GetScriptHash(Isolate* isolate, Handle<Script> script,
                                      bool forceForInspector);
162 163

  // Retrieve source position from where eval was called.
164
  static int GetEvalPosition(Isolate* isolate, Handle<Script> script);
165 166

  // Init line_ends array with source code positions of line ends.
167
  template <typename IsolateT>
168
  EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
169
  static void InitLineEnds(IsolateT* isolate, Handle<Script> script);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

  // Carries information about a source position.
  struct PositionInfo {
    PositionInfo() : line(-1), column(-1), line_start(-1), line_end(-1) {}

    int line;        // Zero-based line number.
    int column;      // Zero-based column number.
    int line_start;  // Position of first character in line.
    int line_end;    // Position of final linebreak character in line.
  };

  // Specifies whether to add offsets to position infos.
  enum OffsetFlag { NO_OFFSET = 0, WITH_OFFSET = 1 };

  // Retrieves information about the given position, optionally with an offset.
  // Returns false on failure, and otherwise writes into the given info object
  // on success.
  // The static method should is preferable for handlified callsites because it
  // initializes the line ends array, avoiding expensive recomputations.
  // The non-static version is not allocating and safe for unhandlified
  // callsites.
  static bool GetPositionInfo(Handle<Script> script, int position,
                              PositionInfo* info, OffsetFlag offset_flag);
193 194
  V8_EXPORT_PRIVATE bool GetPositionInfo(int position, PositionInfo* info,
                                         OffsetFlag offset_flag) const;
195

196 197 198 199 200 201 202
  // Tells whether this script should be subject to debugging, e.g. for
  // - scope inspection
  // - internal break points
  // - coverage and type profile
  // - error stack trace
  bool IsSubjectToDebugging() const;

203
  bool IsUserJavaScript() const;
204 205 206 207

  // Wrappers for GetPositionInfo
  static int GetColumnNumber(Handle<Script> script, int code_offset);
  int GetColumnNumber(int code_pos) const;
208 209
  V8_EXPORT_PRIVATE static int GetLineNumber(Handle<Script> script,
                                             int code_offset);
210 211 212
  int GetLineNumber(int code_pos) const;

  // Look through the list of existing shared function infos to find one
213
  // that matches the function literal. Return empty handle if not found.
214
  template <typename IsolateT>
215
  static MaybeHandle<SharedFunctionInfo> FindSharedFunctionInfo(
216
      Handle<Script> script, IsolateT* isolate,
217 218 219 220 221 222 223 224 225
      FunctionLiteral* function_literal);

  static MaybeHandle<SharedFunctionInfo> FindWebSnapshotSharedFunctionInfo(
      Handle<Script> script, Isolate* isolate,
      FunctionLiteral* function_literal);

  static MaybeHandle<SharedFunctionInfo> FindWebSnapshotSharedFunctionInfo(
      Handle<Script> script, LocalIsolate* isolate,
      FunctionLiteral* function_literal);
226 227

  // Iterate over all script objects on the heap.
228
  class V8_EXPORT_PRIVATE Iterator {
229 230
   public:
    explicit Iterator(Isolate* isolate);
231 232
    Iterator(const Iterator&) = delete;
    Iterator& operator=(const Iterator&) = delete;
233
    Script Next();
234 235

   private:
236
    WeakArrayList::Iterator iterator_;
237 238 239
  };

  // Dispatched behavior.
240 241
  DECL_PRINTER(Script)
  DECL_VERIFIER(Script)
242

243 244
  using BodyDescriptor = StructBodyDescriptor;

245 246
 private:
  // Bit positions in the flags field.
247 248 249
  DEFINE_TORQUE_GENERATED_SCRIPT_FLAGS()

  TQ_OBJECT_CONSTRUCTORS(Script)
250 251 252 253 254 255 256 257
};

}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_SCRIPT_H_