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_LITERAL_OBJECTS_H_
      6 #define V8_OBJECTS_LITERAL_OBJECTS_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 class ClassLiteral;
     18 
     19 // ObjectBoilerplateDescription is a list of properties consisting of name value
     20 // pairs. In addition to the properties, it provides the projected number
     21 // of properties in the backing store. This number includes properties with
     22 // computed names that are not
     23 // in the list.
     24 class ObjectBoilerplateDescription : public FixedArray {
     25  public:
     26   Object* name(int index) const;
     27   Object* value(int index) const;
     28 
     29   void set_key_value(int index, Object* key, Object* value);
     30 
     31   // The number of boilerplate properties.
     32   int size() const;
     33 
     34   // Number of boilerplate properties and properties with computed names.
     35   int backing_store_size() const;
     36 
     37   void set_backing_store_size(Isolate* isolate, int backing_store_size);
     38 
     39   // Used to encode ObjectLiteral::Flags for nested object literals
     40   // Stored as the first element of the fixed array
     41   DECL_INT_ACCESSORS(flags)
     42   static const int kLiteralTypeOffset = 0;
     43   static const int kDescriptionStartIndex = 1;
     44 
     45   DECL_CAST(ObjectBoilerplateDescription)
     46   DECL_VERIFIER(ObjectBoilerplateDescription)
     47   DECL_PRINTER(ObjectBoilerplateDescription)
     48 
     49  private:
     50   bool has_number_of_properties() const;
     51 };
     52 
     53 class ArrayBoilerplateDescription : public Struct {
     54  public:
     55   // store constant_elements of a fixed array
     56   DECL_ACCESSORS(constant_elements, FixedArrayBase)
     57 
     58   inline ElementsKind elements_kind() const;
     59   inline void set_elements_kind(ElementsKind kind);
     60 
     61   inline bool is_empty() const;
     62 
     63   DECL_CAST(ArrayBoilerplateDescription)
     64   // Dispatched behavior.
     65   DECL_PRINTER(ArrayBoilerplateDescription)
     66   DECL_VERIFIER(ArrayBoilerplateDescription)
     67   void BriefPrintDetails(std::ostream& os);
     68 
     69 #define ARRAY_BOILERPLATE_DESCRIPTION_FIELDS(V) \
     70   V(kFlagsOffset, kPointerSize)                 \
     71   V(kConstantElementsOffset, kPointerSize)      \
     72   V(kSize, 0)
     73 
     74   DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
     75                                 ARRAY_BOILERPLATE_DESCRIPTION_FIELDS)
     76 #undef ARRAY_BOILERPLATE_DESCRIPTION_FIELDS
     77 
     78  private:
     79   DECL_INT_ACCESSORS(flags)
     80   DISALLOW_IMPLICIT_CONSTRUCTORS(ArrayBoilerplateDescription);
     81 };
     82 
     83 class ClassBoilerplate : public FixedArray {
     84  public:
     85   enum ValueKind { kData, kGetter, kSetter };
     86 
     87   struct Flags {
     88 #define FLAGS_BIT_FIELDS(V, _)               \
     89   V(InstallClassNameAccessorBit, bool, 1, _) \
     90   V(ArgumentsCountBits, int, 30, _)
     91     DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
     92 #undef FLAGS_BIT_FIELDS
     93   };
     94 
     95   struct ComputedEntryFlags {
     96 #define COMPUTED_ENTRY_BIT_FIELDS(V, _) \
     97   V(ValueKindBits, ValueKind, 2, _)     \
     98   V(KeyIndexBits, unsigned, 29, _)
     99     DEFINE_BIT_FIELDS(COMPUTED_ENTRY_BIT_FIELDS)
    100 #undef COMPUTED_ENTRY_BIT_FIELDS
    101   };
    102 
    103   enum DefineClassArgumentsIndices {
    104     kConstructorArgumentIndex = 1,
    105     kPrototypeArgumentIndex = 2,
    106     // The index of a first dynamic argument passed to Runtime::kDefineClass
    107     // function. The dynamic arguments are consist of method closures and
    108     // computed property names.
    109     kFirstDynamicArgumentIndex = 3,
    110   };
    111 
    112   static const int kMinimumClassPropertiesCount = 6;
    113   static const int kMinimumPrototypePropertiesCount = 1;
    114 
    115   DECL_CAST(ClassBoilerplate)
    116 
    117   DECL_BOOLEAN_ACCESSORS(install_class_name_accessor)
    118   DECL_INT_ACCESSORS(arguments_count)
    119   DECL_ACCESSORS(static_properties_template, Object)
    120   DECL_ACCESSORS(static_elements_template, Object)
    121   DECL_ACCESSORS(static_computed_properties, FixedArray)
    122   DECL_ACCESSORS(instance_properties_template, Object)
    123   DECL_ACCESSORS(instance_elements_template, Object)
    124   DECL_ACCESSORS(instance_computed_properties, FixedArray)
    125 
    126   static void AddToPropertiesTemplate(Isolate* isolate,
    127                                       Handle<NameDictionary> dictionary,
    128                                       Handle<Name> name, int key_index,
    129                                       ValueKind value_kind, Object* value);
    130 
    131   static void AddToElementsTemplate(Isolate* isolate,
    132                                     Handle<NumberDictionary> dictionary,
    133                                     uint32_t key, int key_index,
    134                                     ValueKind value_kind, Object* value);
    135 
    136   static Handle<ClassBoilerplate> BuildClassBoilerplate(Isolate* isolate,
    137                                                         ClassLiteral* expr);
    138 
    139   enum {
    140     kFlagsIndex,
    141     kClassPropertiesTemplateIndex,
    142     kClassElementsTemplateIndex,
    143     kClassComputedPropertiesIndex,
    144     kPrototypePropertiesTemplateIndex,
    145     kPrototypeElementsTemplateIndex,
    146     kPrototypeComputedPropertiesIndex,
    147     kBoileplateLength  // last element
    148   };
    149 
    150   static const int kFullComputedEntrySize = 2;
    151 
    152  private:
    153   DECL_INT_ACCESSORS(flags)
    154 };
    155 
    156 }  // namespace internal
    157 }  // namespace v8
    158 
    159 #include "src/objects/object-macros-undef.h"
    160 
    161 #endif  // V8_OBJECTS_LITERAL_OBJECTS_H_
    162