Home | History | Annotate | Download | only in src
      1 // Copyright 2012 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #ifndef V8_PROPERTY_DETAILS_H_
     29 #define V8_PROPERTY_DETAILS_H_
     30 
     31 #include "../include/v8.h"
     32 #include "allocation.h"
     33 #include "utils.h"
     34 
     35 // Ecma-262 3rd 8.6.1
     36 enum PropertyAttributes {
     37   NONE              = v8::None,
     38   READ_ONLY         = v8::ReadOnly,
     39   DONT_ENUM         = v8::DontEnum,
     40   DONT_DELETE       = v8::DontDelete,
     41   ABSENT            = 16  // Used in runtime to indicate a property is absent.
     42   // ABSENT can never be stored in or returned from a descriptor's attributes
     43   // bitfield.  It is only used as a return value meaning the attributes of
     44   // a non-existent property.
     45 };
     46 
     47 
     48 namespace v8 {
     49 namespace internal {
     50 
     51 class Smi;
     52 
     53 // Type of properties.
     54 // Order of properties is significant.
     55 // Must fit in the BitField PropertyDetails::TypeField.
     56 // A copy of this is in mirror-debugger.js.
     57 enum PropertyType {
     58   NORMAL                    = 0,  // only in slow mode
     59   FIELD                     = 1,  // only in fast mode
     60   CONSTANT_FUNCTION         = 2,  // only in fast mode
     61   CALLBACKS                 = 3,
     62   HANDLER                   = 4,  // only in lookup results, not in descriptors
     63   INTERCEPTOR               = 5,  // only in lookup results, not in descriptors
     64   // All properties before MAP_TRANSITION are real.
     65   MAP_TRANSITION            = 6,  // only in fast mode
     66   ELEMENTS_TRANSITION       = 7,
     67   CONSTANT_TRANSITION       = 8,  // only in fast mode
     68   NULL_DESCRIPTOR           = 9,  // only in fast mode
     69   // There are no IC stubs for NULL_DESCRIPTORS. Therefore,
     70   // NULL_DESCRIPTOR can be used as the type flag for IC stubs for
     71   // nonexistent properties.
     72   NONEXISTENT = NULL_DESCRIPTOR
     73 };
     74 
     75 
     76 // PropertyDetails captures type and attributes for a property.
     77 // They are used both in property dictionaries and instance descriptors.
     78 class PropertyDetails BASE_EMBEDDED {
     79  public:
     80   PropertyDetails(PropertyAttributes attributes,
     81                   PropertyType type,
     82                   int index = 0) {
     83     ASSERT(TypeField::is_valid(type));
     84     ASSERT(AttributesField::is_valid(attributes));
     85     ASSERT(StorageField::is_valid(index));
     86 
     87     value_ = TypeField::encode(type)
     88         | AttributesField::encode(attributes)
     89         | StorageField::encode(index);
     90 
     91     ASSERT(type == this->type());
     92     ASSERT(attributes == this->attributes());
     93     ASSERT(index == this->index());
     94   }
     95 
     96   // Conversion for storing details as Object*.
     97   explicit inline PropertyDetails(Smi* smi);
     98   inline Smi* AsSmi();
     99 
    100   PropertyType type() { return TypeField::decode(value_); }
    101 
    102   PropertyAttributes attributes() { return AttributesField::decode(value_); }
    103 
    104   int index() { return StorageField::decode(value_); }
    105 
    106   inline PropertyDetails AsDeleted();
    107 
    108   static bool IsValidIndex(int index) {
    109     return StorageField::is_valid(index);
    110   }
    111 
    112   bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
    113   bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
    114   bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
    115   bool IsDeleted() { return DeletedField::decode(value_) != 0;}
    116 
    117   // Bit fields in value_ (type, shift, size). Must be public so the
    118   // constants can be embedded in generated code.
    119   class TypeField:       public BitField<PropertyType,       0, 4> {};
    120   class AttributesField: public BitField<PropertyAttributes, 4, 3> {};
    121   class DeletedField:    public BitField<uint32_t,           7, 1> {};
    122   class StorageField:    public BitField<uint32_t,           8, 32-8> {};
    123 
    124   static const int kInitialIndex = 1;
    125 
    126  private:
    127   uint32_t value_;
    128 };
    129 
    130 } }  // namespace v8::internal
    131 
    132 #endif  // V8_PROPERTY_DETAILS_H_
    133