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