property.h 11.2 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_PROPERTY_H_
#define V8_PROPERTY_H_

31
#include "allocation.h"
32
#include "transitions.h"
33

34 35
namespace v8 {
namespace internal {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50


// Abstraction for elements in instance-descriptor arrays.
//
// Each descriptor has a key, property attributes, property type,
// property index (in the actual instance-descriptor array) and
// optionally a piece of data.
//

class Descriptor BASE_EMBEDDED {
 public:
  static int IndexFromValue(Object* value) {
    return Smi::cast(value)->value();
  }

51
  MUST_USE_RESULT MaybeObject* KeyToSymbol() {
52
    if (!StringShape(key_).IsSymbol()) {
53 54
      MaybeObject* maybe_result = HEAP->LookupSymbol(key_);
      if (!maybe_result->To(&key_)) return maybe_result;
55 56 57 58 59 60 61 62
    }
    return key_;
  }

  String* GetKey() { return key_; }
  Object* GetValue() { return value_; }
  PropertyDetails GetDetails() { return details_; }

63 64
#ifdef OBJECT_PRINT
  void Print(FILE* out);
65 66 67 68 69 70
#endif

  void SetEnumerationIndex(int index) {
    details_ = PropertyDetails(details_.attributes(), details_.type(), index);
  }

71
  void SetSortedKeyIndex(int index) { details_ = details_.set_pointer(index); }
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 private:
  String* key_;
  Object* value_;
  PropertyDetails details_;

 protected:
  Descriptor() : details_(Smi::FromInt(0)) {}

  void Init(String* key, Object* value, PropertyDetails details) {
    key_ = key;
    value_ = value;
    details_ = details;
  }

  Descriptor(String* key, Object* value, PropertyDetails details)
      : key_(key),
        value_(value),
        details_(details) { }

  Descriptor(String* key,
             Object* value,
             PropertyAttributes attributes,
             PropertyType type,
96
             int index)
97 98 99 100 101 102 103 104 105 106 107 108 109
      : key_(key),
        value_(value),
        details_(attributes, type, index) { }

  friend class DescriptorArray;
};


class FieldDescriptor: public Descriptor {
 public:
  FieldDescriptor(String* key,
                  int field_index,
                  PropertyAttributes attributes,
110
                  int index = 0)
111 112 113 114 115 116 117 118 119
      : Descriptor(key, Smi::FromInt(field_index), attributes, FIELD, index) {}
};


class ConstantFunctionDescriptor: public Descriptor {
 public:
  ConstantFunctionDescriptor(String* key,
                             JSFunction* function,
                             PropertyAttributes attributes,
120
                             int index)
121 122 123 124 125 126 127
      : Descriptor(key, function, attributes, CONSTANT_FUNCTION, index) {}
};


class CallbacksDescriptor:  public Descriptor {
 public:
  CallbacksDescriptor(String* key,
128
                      Object* foreign,
129
                      PropertyAttributes attributes,
130
                      int index = 0)
131
      : Descriptor(key, foreign, attributes, CALLBACKS, index) {}
132 133 134 135 136
};


class LookupResult BASE_EMBEDDED {
 public:
137 138 139 140 141
  explicit LookupResult(Isolate* isolate)
      : isolate_(isolate),
        next_(isolate->top_lookup_result()),
        lookup_type_(NOT_FOUND),
        holder_(NULL),
142
        cacheable_(true),
143
        details_(NONE, NONEXISTENT) {
144 145 146 147 148 149 150
    isolate->SetTopLookupResult(this);
  }

  ~LookupResult() {
    ASSERT(isolate_->top_lookup_result() == this);
    isolate_->SetTopLookupResult(next_);
  }
151 152 153 154 155 156 157 158

  void DescriptorResult(JSObject* holder, PropertyDetails details, int number) {
    lookup_type_ = DESCRIPTOR_TYPE;
    holder_ = holder;
    details_ = details;
    number_ = number;
  }

159 160 161 162 163 164 165
  void TransitionResult(JSObject* holder, int number) {
    lookup_type_ = TRANSITION_TYPE;
    details_ = PropertyDetails(NONE, TRANSITION);
    holder_ = holder;
    number_ = number;
  }

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  void ConstantResult(JSObject* holder) {
    lookup_type_ = CONSTANT_TYPE;
    holder_ = holder;
    details_ =
        PropertyDetails(static_cast<PropertyAttributes>(DONT_ENUM |
                                                        DONT_DELETE),
                        CALLBACKS);
    number_ = -1;
  }

  void DictionaryResult(JSObject* holder, int entry) {
    lookup_type_ = DICTIONARY_TYPE;
    holder_ = holder;
    details_ = holder->property_dictionary()->DetailsAt(entry);
    number_ = entry;
  }

183
  void HandlerResult(JSProxy* proxy) {
184
    lookup_type_ = HANDLER_TYPE;
185
    holder_ = proxy;
186
    details_ = PropertyDetails(NONE, HANDLER);
187
    cacheable_ = false;
188 189
  }

190 191 192 193 194 195 196 197
  void InterceptorResult(JSObject* holder) {
    lookup_type_ = INTERCEPTOR_TYPE;
    holder_ = holder;
    details_ = PropertyDetails(NONE, INTERCEPTOR);
  }

  void NotFound() {
    lookup_type_ = NOT_FOUND;
198
    details_ = PropertyDetails(NONE, NONEXISTENT);
199
    holder_ = NULL;
200 201 202
  }

  JSObject* holder() {
203
    ASSERT(IsFound());
204 205 206 207 208 209
    return JSObject::cast(holder_);
  }

  JSProxy* proxy() {
    ASSERT(IsFound());
    return JSProxy::cast(holder_);
210 211 212
  }

  PropertyType type() {
213
    ASSERT(IsFound());
214 215 216 217
    return details_.type();
  }

  PropertyAttributes GetAttributes() {
218
    ASSERT(!IsTransition());
219
    ASSERT(IsFound());
220
    ASSERT(details_.type() != NONEXISTENT);
221 222 223 224
    return details_.attributes();
  }

  PropertyDetails GetPropertyDetails() {
225
    ASSERT(!IsTransition());
226 227 228
    return details_;
  }

229 230
  bool IsFastPropertyType() {
    ASSERT(IsFound());
231
    return IsTransition() || type() != NORMAL;
232 233
  }

234 235 236 237
  // Property callbacks does not include transitions to callbacks.
  bool IsPropertyCallbacks() {
    ASSERT(!(details_.type() == CALLBACKS && !IsFound()));
    return details_.type() == CALLBACKS;
238 239
  }

240 241 242 243 244
  bool IsReadOnly() {
    ASSERT(IsFound());
    ASSERT(!IsTransition());
    ASSERT(details_.type() != NONEXISTENT);
    return details_.IsReadOnly();
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  }

  bool IsField() {
    ASSERT(!(details_.type() == FIELD && !IsFound()));
    return details_.type() == FIELD;
  }

  bool IsNormal() {
    ASSERT(!(details_.type() == NORMAL && !IsFound()));
    return details_.type() == NORMAL;
  }

  bool IsConstantFunction() {
    ASSERT(!(details_.type() == CONSTANT_FUNCTION && !IsFound()));
    return details_.type() == CONSTANT_FUNCTION;
  }

262 263
  bool IsDontDelete() { return details_.IsDontDelete(); }
  bool IsDontEnum() { return details_.IsDontEnum(); }
264
  bool IsDeleted() { return details_.IsDeleted(); }
265
  bool IsFound() { return lookup_type_ != NOT_FOUND; }
266
  bool IsTransition() { return lookup_type_ == TRANSITION_TYPE; }
267
  bool IsHandler() { return lookup_type_ == HANDLER_TYPE; }
268
  bool IsInterceptor() { return lookup_type_ == INTERCEPTOR_TYPE; }
269

270
  // Is the result is a property excluding transitions and the null descriptor?
271
  bool IsProperty() {
272
    return IsFound() && !IsTransition();
273 274
  }

275 276 277
  bool IsCacheable() { return cacheable_; }
  void DisallowCaching() { cacheable_ = false; }

278 279 280 281
  Object* GetLazyValue() {
    switch (type()) {
      case FIELD:
        return holder()->FastPropertyAt(GetFieldIndex());
282 283 284
      case NORMAL: {
        Object* value;
        value = holder()->property_dictionary()->ValueAt(GetDictionaryEntry());
285
        if (holder()->IsGlobalObject()) {
286 287 288 289
          value = JSGlobalPropertyCell::cast(value)->value();
        }
        return value;
      }
290 291 292
      case CONSTANT_FUNCTION:
        return GetConstantFunction();
      default:
293
        return Isolate::Current()->heap()->the_hole_value();
294 295 296
    }
  }

297
  Map* GetTransitionTarget() {
298 299
    ASSERT(IsTransition());
    TransitionArray* transitions = holder()->map()->transitions();
300
    return transitions->GetTarget(number_);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
  }

  PropertyDetails GetTransitionDetails(Map* map) {
    ASSERT(IsTransition());
    TransitionArray* transitions = map->transitions();
    return transitions->GetTargetDetails(number_);
  }

  PropertyDetails GetTransitionDetails() {
    return GetTransitionDetails(holder()->map());
  }

  bool IsTransitionToField(Map* map) {
    return IsTransition() && GetTransitionDetails(map).type() == FIELD;
  }

317
  Map* GetTransitionMap() {
318
    ASSERT(IsTransition());
319 320 321
    return Map::cast(GetValue());
  }

322
  Map* GetTransitionMapFromMap(Map* map) {
323
    ASSERT(IsTransition());
324
    return map->transitions()->GetTarget(number_);
325 326
  }

327 328 329 330 331
  int GetTransitionIndex() {
    ASSERT(IsTransition());
    return number_;
  }

332 333 334 335 336
  int GetDescriptorIndex() {
    ASSERT(lookup_type_ == DESCRIPTOR_TYPE);
    return number_;
  }

337 338
  int GetFieldIndex() {
    ASSERT(lookup_type_ == DESCRIPTOR_TYPE);
339
    ASSERT(IsField());
340 341 342
    return Descriptor::IndexFromValue(GetValue());
  }

343
  int GetLocalFieldIndexFromMap(Map* map) {
344
    ASSERT(IsField());
345
    return Descriptor::IndexFromValue(GetValueFromMap(map)) -
346 347 348
        map->inobject_properties();
  }

349 350 351 352 353 354 355 356 357 358
  int GetDictionaryEntry() {
    ASSERT(lookup_type_ == DICTIONARY_TYPE);
    return number_;
  }

  JSFunction* GetConstantFunction() {
    ASSERT(type() == CONSTANT_FUNCTION);
    return JSFunction::cast(GetValue());
  }

359 360
  JSFunction* GetConstantFunctionFromMap(Map* map) {
    ASSERT(type() == CONSTANT_FUNCTION);
361
    return JSFunction::cast(GetValueFromMap(map));
362 363
  }

364
  Object* GetCallbackObject() {
365 366
    if (lookup_type_ == CONSTANT_TYPE) {
      return HEAP->prototype_accessors();
367
    }
368 369
    ASSERT(!IsTransition());
    return GetValue();
370 371
  }

372 373
#ifdef OBJECT_PRINT
  void Print(FILE* out);
374 375 376 377
#endif

  Object* GetValue() {
    if (lookup_type_ == DESCRIPTOR_TYPE) {
378
      return GetValueFromMap(holder()->map());
379 380 381
    }
    // In the dictionary case, the data is held in the value field.
    ASSERT(lookup_type_ == DICTIONARY_TYPE);
382
    return holder()->GetNormalizedProperty(this);
383 384
  }

385 386
  Object* GetValueFromMap(Map* map) const {
    ASSERT(lookup_type_ == DESCRIPTOR_TYPE);
387
    ASSERT(number_ < map->NumberOfOwnDescriptors());
388 389 390
    return map->instance_descriptors()->GetValue(number_);
  }

391 392
  void Iterate(ObjectVisitor* visitor);

393
 private:
394 395 396
  Isolate* isolate_;
  LookupResult* next_;

397 398 399 400
  // Where did we find the result;
  enum {
    NOT_FOUND,
    DESCRIPTOR_TYPE,
401
    TRANSITION_TYPE,
402 403 404 405 406 407
    DICTIONARY_TYPE,
    HANDLER_TYPE,
    INTERCEPTOR_TYPE,
    CONSTANT_TYPE
  } lookup_type_;

408
  JSReceiver* holder_;
409 410 411 412 413 414 415 416 417
  int number_;
  bool cacheable_;
  PropertyDetails details_;
};


} }  // namespace v8::internal

#endif  // V8_PROPERTY_H_