property.h 12 KB
Newer Older
1 2 3
// Copyright 2014 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.
4 5 6 7

#ifndef V8_PROPERTY_H_
#define V8_PROPERTY_H_

8
#include "isolate.h"
9

10 11
namespace v8 {
namespace internal {
12 13 14 15 16 17 18 19

// 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:
20 21
  MUST_USE_RESULT MaybeObject* KeyToUniqueName() {
    if (!key_->IsUniqueName()) {
22 23
      MaybeObject* maybe_result =
          key_->GetIsolate()->heap()->InternalizeString(String::cast(key_));
24
      if (!maybe_result->To(&key_)) return maybe_result;
25 26 27 28
    }
    return key_;
  }

29
  Name* GetKey() { return key_; }
30 31 32
  Object* GetValue() { return value_; }
  PropertyDetails GetDetails() { return details_; }

33 34
#ifdef OBJECT_PRINT
  void Print(FILE* out);
35 36
#endif

37
  void SetSortedKeyIndex(int index) { details_ = details_.set_pointer(index); }
38

39
 private:
40
  Name* key_;
41 42 43 44 45 46
  Object* value_;
  PropertyDetails details_;

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

47
  void Init(Name* key, Object* value, PropertyDetails details) {
48 49 50 51 52
    key_ = key;
    value_ = value;
    details_ = details;
  }

53
  Descriptor(Name* key, Object* value, PropertyDetails details)
54 55 56 57
      : key_(key),
        value_(value),
        details_(details) { }

58
  Descriptor(Name* key,
59 60 61
             Object* value,
             PropertyAttributes attributes,
             PropertyType type,
62 63
             Representation representation,
             int field_index = 0)
64 65
      : key_(key),
        value_(value),
66
        details_(attributes, type, representation, field_index) { }
67 68 69 70 71

  friend class DescriptorArray;
};


72
class FieldDescriptor V8_FINAL : public Descriptor {
73
 public:
74
  FieldDescriptor(Name* key,
75 76
                  int field_index,
                  PropertyAttributes attributes,
77
                  Representation representation)
78 79
      : Descriptor(key, Smi::FromInt(0), attributes,
                   FIELD, representation, field_index) {}
80 81 82
};


83
class ConstantDescriptor V8_FINAL : public Descriptor {
84
 public:
85 86 87 88 89
  ConstantDescriptor(Name* key,
                     Object* value,
                     PropertyAttributes attributes)
      : Descriptor(key, value, attributes, CONSTANT,
                   value->OptimalRepresentation()) {}
90 91 92
};


93
class CallbacksDescriptor V8_FINAL : public Descriptor {
94
 public:
95
  CallbacksDescriptor(Name* key,
96
                      Object* foreign,
97
                      PropertyAttributes attributes)
98
      : Descriptor(key, foreign, attributes, CALLBACKS,
99
                   Representation::Tagged()) {}
100 101 102
};


103 104
// Holds a property index value distinguishing if it is a field index or an
// index inside the object header.
105
class PropertyIndex V8_FINAL {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
 public:
  static PropertyIndex NewFieldIndex(int index) {
    return PropertyIndex(index, false);
  }
  static PropertyIndex NewHeaderIndex(int index) {
    return PropertyIndex(index, true);
  }

  bool is_field_index() { return (index_ & kHeaderIndexBit) == 0; }
  bool is_header_index() { return (index_ & kHeaderIndexBit) != 0; }

  int field_index() {
    ASSERT(is_field_index());
    return value();
  }
  int header_index() {
    ASSERT(is_header_index());
    return value();
  }

126 127 128 129 130 131 132 133 134 135 136 137
  bool is_inobject(Handle<JSObject> holder) {
    if (is_header_index()) return true;
    return field_index() < holder->map()->inobject_properties();
  }

  int translate(Handle<JSObject> holder) {
    if (is_header_index()) return header_index();
    int index = field_index() - holder->map()->inobject_properties();
    if (index >= 0) return index;
    return index + holder->map()->instance_size() / kPointerSize;
  }

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
 private:
  static const int kHeaderIndexBit = 1 << 31;
  static const int kIndexMask = ~kHeaderIndexBit;

  int value() { return index_ & kIndexMask; }

  PropertyIndex(int index, bool is_header_based)
      : index_(index | (is_header_based ? kHeaderIndexBit : 0)) {
    ASSERT(index <= kIndexMask);
  }

  int index_;
};


153
class LookupResult V8_FINAL BASE_EMBEDDED {
154
 public:
155 156 157 158 159
  explicit LookupResult(Isolate* isolate)
      : isolate_(isolate),
        next_(isolate->top_lookup_result()),
        lookup_type_(NOT_FOUND),
        holder_(NULL),
160
        transition_(NULL),
161
        cacheable_(true),
162
        details_(NONE, NONEXISTENT, Representation::None()) {
163
    isolate->set_top_lookup_result(this);
164 165 166
  }

  ~LookupResult() {
167
    ASSERT(isolate()->top_lookup_result() == this);
168
    isolate()->set_top_lookup_result(next_);
169
  }
170

171 172
  Isolate* isolate() const { return isolate_; }

173 174 175
  void DescriptorResult(JSObject* holder, PropertyDetails details, int number) {
    lookup_type_ = DESCRIPTOR_TYPE;
    holder_ = holder;
176
    transition_ = NULL;
177 178 179 180
    details_ = details;
    number_ = number;
  }

181
  bool CanHoldValue(Handle<Object> value) {
182 183
    if (IsNormal()) return true;
    ASSERT(!IsTransition());
184 185 186
    return value->FitsRepresentation(details_.representation());
  }

187
  void TransitionResult(JSObject* holder, Map* target) {
188
    lookup_type_ = TRANSITION_TYPE;
189
    details_ = PropertyDetails(NONE, TRANSITION, Representation::None());
190
    holder_ = holder;
191 192
    transition_ = target;
    number_ = 0xAAAA;
193 194
  }

195 196 197
  void DictionaryResult(JSObject* holder, int entry) {
    lookup_type_ = DICTIONARY_TYPE;
    holder_ = holder;
198
    transition_ = NULL;
199 200 201 202
    details_ = holder->property_dictionary()->DetailsAt(entry);
    number_ = entry;
  }

203
  void HandlerResult(JSProxy* proxy) {
204
    lookup_type_ = HANDLER_TYPE;
205
    holder_ = proxy;
206
    transition_ = NULL;
207
    details_ = PropertyDetails(NONE, HANDLER, Representation::Tagged());
208
    cacheable_ = false;
209 210
  }

211 212 213
  void InterceptorResult(JSObject* holder) {
    lookup_type_ = INTERCEPTOR_TYPE;
    holder_ = holder;
214
    transition_ = NULL;
215
    details_ = PropertyDetails(NONE, INTERCEPTOR, Representation::Tagged());
216 217 218 219
  }

  void NotFound() {
    lookup_type_ = NOT_FOUND;
220
    details_ = PropertyDetails(NONE, NONEXISTENT, Representation::None());
221
    holder_ = NULL;
222
    transition_ = NULL;
223 224
  }

225
  JSObject* holder() const {
226
    ASSERT(IsFound());
227 228 229
    return JSObject::cast(holder_);
  }

230
  JSProxy* proxy() const {
231
    ASSERT(IsHandler());
232
    return JSProxy::cast(holder_);
233 234
  }

235
  PropertyType type() const {
236
    ASSERT(IsFound());
237 238 239
    return details_.type();
  }

240
  Representation representation() const {
241
    ASSERT(IsFound());
242 243
    ASSERT(!IsTransition());
    ASSERT(details_.type() != NONEXISTENT);
244 245 246
    return details_.representation();
  }

247
  PropertyAttributes GetAttributes() const {
248
    ASSERT(!IsTransition());
249
    ASSERT(IsFound());
250
    ASSERT(details_.type() != NONEXISTENT);
251 252 253
    return details_.attributes();
  }

254
  PropertyDetails GetPropertyDetails() const {
255
    ASSERT(!IsTransition());
256 257 258
    return details_;
  }

259
  bool IsFastPropertyType() const {
260
    ASSERT(IsFound());
261
    return IsTransition() || type() != NORMAL;
262 263
  }

264
  // Property callbacks does not include transitions to callbacks.
265
  bool IsPropertyCallbacks() const {
266 267
    ASSERT(!(details_.type() == CALLBACKS && !IsFound()));
    return details_.type() == CALLBACKS;
268 269
  }

270
  bool IsReadOnly() const {
271 272 273 274
    ASSERT(IsFound());
    ASSERT(!IsTransition());
    ASSERT(details_.type() != NONEXISTENT);
    return details_.IsReadOnly();
275 276
  }

277
  bool IsField() const {
278 279 280 281
    ASSERT(!(details_.type() == FIELD && !IsFound()));
    return details_.type() == FIELD;
  }

282
  bool IsNormal() const {
283 284 285 286
    ASSERT(!(details_.type() == NORMAL && !IsFound()));
    return details_.type() == NORMAL;
  }

287
  bool IsConstant() const {
288 289 290 291
    ASSERT(!(details_.type() == CONSTANT && !IsFound()));
    return details_.type() == CONSTANT;
  }

292
  bool IsConstantFunction() const {
293
    return IsConstant() && GetValue()->IsJSFunction();
294 295
  }

296 297 298 299 300 301
  bool IsDontDelete() const { return details_.IsDontDelete(); }
  bool IsDontEnum() const { return details_.IsDontEnum(); }
  bool IsFound() const { return lookup_type_ != NOT_FOUND; }
  bool IsTransition() const { return lookup_type_ == TRANSITION_TYPE; }
  bool IsHandler() const { return lookup_type_ == HANDLER_TYPE; }
  bool IsInterceptor() const { return lookup_type_ == INTERCEPTOR_TYPE; }
302

303
  // Is the result is a property excluding transitions and the null descriptor?
304
  bool IsProperty() const {
305
    return IsFound() && !IsTransition();
306 307
  }

308
  bool IsDataProperty() const {
309 310 311
    switch (type()) {
      case FIELD:
      case NORMAL:
312
      case CONSTANT:
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        return true;
      case CALLBACKS: {
        Object* callback = GetCallbackObject();
        return callback->IsAccessorInfo() || callback->IsForeign();
      }
      case HANDLER:
      case INTERCEPTOR:
      case TRANSITION:
      case NONEXISTENT:
        return false;
    }
    UNREACHABLE();
    return false;
  }

328
  bool IsCacheable() const { return cacheable_; }
329 330
  void DisallowCaching() { cacheable_ = false; }

331
  Object* GetLazyValue() const {
332 333
    switch (type()) {
      case FIELD:
334
        return holder()->RawFastPropertyAt(GetFieldIndex().field_index());
335 336 337
      case NORMAL: {
        Object* value;
        value = holder()->property_dictionary()->ValueAt(GetDictionaryEntry());
338
        if (holder()->IsGlobalObject()) {
339
          value = PropertyCell::cast(value)->value();
340 341 342
        }
        return value;
      }
343 344
      case CONSTANT:
        return GetConstant();
345 346 347 348 349
      case CALLBACKS:
      case HANDLER:
      case INTERCEPTOR:
      case TRANSITION:
      case NONEXISTENT:
350
        return isolate()->heap()->the_hole_value();
351
    }
352 353
    UNREACHABLE();
    return NULL;
354 355
  }

356
  Map* GetTransitionTarget() const {
357
    return transition_;
358 359
  }

360
  PropertyDetails GetTransitionDetails() const {
361
    ASSERT(IsTransition());
362
    return transition_->GetLastDescriptorDetails();
363 364
  }

365 366
  bool IsTransitionToField() const {
    return IsTransition() && GetTransitionDetails().type() == FIELD;
367 368
  }

369 370
  bool IsTransitionToConstant() const {
    return IsTransition() && GetTransitionDetails().type() == CONSTANT;
371 372
  }

373
  int GetDescriptorIndex() const {
374 375 376 377
    ASSERT(lookup_type_ == DESCRIPTOR_TYPE);
    return number_;
  }

378
  PropertyIndex GetFieldIndex() const {
379
    ASSERT(lookup_type_ == DESCRIPTOR_TYPE);
380
    return PropertyIndex::NewFieldIndex(GetFieldIndexFromMap(holder()->map()));
381 382
  }

383
  int GetLocalFieldIndexFromMap(Map* map) const {
384
    return GetFieldIndexFromMap(map) - map->inobject_properties();
385 386
  }

387
  int GetDictionaryEntry() const {
388 389 390 391
    ASSERT(lookup_type_ == DICTIONARY_TYPE);
    return number_;
  }

392
  JSFunction* GetConstantFunction() const {
393
    ASSERT(type() == CONSTANT);
394 395 396
    return JSFunction::cast(GetValue());
  }

397
  Object* GetConstantFromMap(Map* map) const {
398 399 400 401
    ASSERT(type() == CONSTANT);
    return GetValueFromMap(map);
  }

402
  JSFunction* GetConstantFunctionFromMap(Map* map) const {
403 404 405
    return JSFunction::cast(GetConstantFromMap(map));
  }

406
  Object* GetConstant() const {
407 408
    ASSERT(type() == CONSTANT);
    return GetValue();
409 410
  }

411
  Object* GetCallbackObject() const {
412
    ASSERT(type() == CALLBACKS && !IsTransition());
413
    return GetValue();
414 415
  }

416 417
#ifdef OBJECT_PRINT
  void Print(FILE* out);
418 419
#endif

420
  Object* GetValue() const {
421
    if (lookup_type_ == DESCRIPTOR_TYPE) {
422
      return GetValueFromMap(holder()->map());
423 424 425
    }
    // In the dictionary case, the data is held in the value field.
    ASSERT(lookup_type_ == DICTIONARY_TYPE);
426
    return holder()->GetNormalizedProperty(this);
427 428
  }

429 430
  Object* GetValueFromMap(Map* map) const {
    ASSERT(lookup_type_ == DESCRIPTOR_TYPE);
431
    ASSERT(number_ < map->NumberOfOwnDescriptors());
432 433 434
    return map->instance_descriptors()->GetValue(number_);
  }

435 436 437 438 439 440
  int GetFieldIndexFromMap(Map* map) const {
    ASSERT(lookup_type_ == DESCRIPTOR_TYPE);
    ASSERT(number_ < map->NumberOfOwnDescriptors());
    return map->instance_descriptors()->GetFieldIndex(number_);
  }

441 442
  void Iterate(ObjectVisitor* visitor);

443
 private:
444 445 446
  Isolate* isolate_;
  LookupResult* next_;

447 448 449 450
  // Where did we find the result;
  enum {
    NOT_FOUND,
    DESCRIPTOR_TYPE,
451
    TRANSITION_TYPE,
452 453
    DICTIONARY_TYPE,
    HANDLER_TYPE,
454
    INTERCEPTOR_TYPE
455 456
  } lookup_type_;

457
  JSReceiver* holder_;
458
  Map* transition_;
459 460 461 462 463 464 465 466
  int number_;
  bool cacheable_;
  PropertyDetails details_;
};

} }  // namespace v8::internal

#endif  // V8_PROPERTY_H_