lookup.cc 26.1 KB
Newer Older
1 2 3 4
// 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.

5
#include "src/lookup.h"
6 7

#include "src/bootstrapper.h"
8
#include "src/deoptimizer.h"
9
#include "src/elements.h"
10
#include "src/field-type.h"
11
#include "src/isolate-inl.h"
12 13 14 15 16

namespace v8 {
namespace internal {


17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// static
LookupIterator LookupIterator::PropertyOrElement(Isolate* isolate,
                                                 Handle<Object> receiver,
                                                 Handle<Object> key,
                                                 bool* success,
                                                 Configuration configuration) {
  uint32_t index = 0;
  if (key->ToArrayIndex(&index)) {
    *success = true;
    return LookupIterator(isolate, receiver, index, configuration);
  }

  Handle<Name> name;
  *success = Object::ToName(isolate, key).ToHandle(&name);
  if (!*success) {
    DCHECK(isolate->has_pending_exception());
    // Return an unusable dummy.
    return LookupIterator(receiver, isolate->factory()->empty_string());
  }

  if (name->AsArrayIndex(&index)) {
    LookupIterator it(isolate, receiver, index, configuration);
    // Here we try to avoid having to rebuild the string later
    // by storing it on the indexed LookupIterator.
41
    it.name_ = name;
42 43 44 45 46 47
    return it;
  }

  return LookupIterator(receiver, name, configuration);
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
template <bool is_element>
void LookupIterator::Start() {
  DisallowHeapAllocation no_gc;

  has_property_ = false;
  state_ = NOT_FOUND;
  holder_ = initial_holder_;

  JSReceiver* holder = *holder_;
  Map* map = holder->map();

  state_ = LookupInHolder<is_element>(map, holder);
  if (IsFound()) return;

  NextInternal<is_element>(map, holder);
}

template void LookupIterator::Start<true>();
template void LookupIterator::Start<false>();
67

68
void LookupIterator::Next() {
69 70
  DCHECK_NE(JSPROXY, state_);
  DCHECK_NE(TRANSITION, state_);
71
  DisallowHeapAllocation no_gc;
72
  has_property_ = false;
73

74
  JSReceiver* holder = *holder_;
75
  Map* map = holder->map();
76

77 78 79 80 81 82 83 84 85
  if (map->instance_type() <= LAST_SPECIAL_RECEIVER_TYPE) {
    state_ = IsElement() ? LookupInSpecialHolder<true>(map, holder)
                         : LookupInSpecialHolder<false>(map, holder);
    if (IsFound()) return;
  }

  IsElement() ? NextInternal<true>(map, holder)
              : NextInternal<false>(map, holder);
}
86

87 88
template <bool is_element>
void LookupIterator::NextInternal(Map* map, JSReceiver* holder) {
89
  do {
90
    JSReceiver* maybe_holder = NextHolder(map);
91 92
    if (maybe_holder == nullptr) {
      if (interceptor_state_ == InterceptorState::kSkipNonMasking) {
93
        RestartLookupForNonMaskingInterceptors<is_element>();
94 95
        return;
      }
96 97 98
      state_ = NOT_FOUND;
      if (holder != *holder_) holder_ = handle(holder, isolate_);
      return;
99
    }
100 101
    holder = maybe_holder;
    map = holder->map();
102
    state_ = LookupInHolder<is_element>(map, holder);
103
  } while (!IsFound());
104

105
  holder_ = handle(holder, isolate_);
106 107
}

108
template <bool is_element>
109 110
void LookupIterator::RestartInternal(InterceptorState interceptor_state) {
  interceptor_state_ = interceptor_state;
111
  property_details_ = PropertyDetails::Empty();
112
  number_ = DescriptorArray::kNotFound;
113
  Start<is_element>();
114 115
}

116 117
template void LookupIterator::RestartInternal<true>(InterceptorState);
template void LookupIterator::RestartInternal<false>(InterceptorState);
118

119
// static
120 121
Handle<JSReceiver> LookupIterator::GetRootForNonJSReceiver(
    Isolate* isolate, Handle<Object> receiver, uint32_t index) {
122 123 124 125 126 127 128 129 130 131 132
  // Strings are the only objects with properties (only elements) directly on
  // the wrapper. Hence we can skip generating the wrapper for all other cases.
  if (index != kMaxUInt32 && receiver->IsString() &&
      index < static_cast<uint32_t>(String::cast(*receiver)->length())) {
    // TODO(verwaest): Speed this up. Perhaps use a cached wrapper on the native
    // context, ensuring that we don't leak it into JS?
    Handle<JSFunction> constructor = isolate->string_function();
    Handle<JSObject> result = isolate->factory()->NewJSObject(constructor);
    Handle<JSValue>::cast(result)->set_value(*receiver);
    return result;
  }
133
  auto root = handle(receiver->GetRootMap(isolate)->prototype(), isolate);
134
  if (root->IsNull(isolate)) {
135 136 137
    unsigned int magic = 0xbbbbbbbb;
    isolate->PushStackTraceAndDie(magic, *receiver, NULL, magic);
  }
138
  return Handle<JSReceiver>::cast(root);
139 140 141 142
}


Handle<Map> LookupIterator::GetReceiverMap() const {
143
  if (receiver_->IsNumber()) return factory()->heap_number_map();
144
  return handle(Handle<HeapObject>::cast(receiver_)->map(), isolate_);
145 146
}

147
bool LookupIterator::HasAccess() const {
148
  DCHECK_EQ(ACCESS_CHECK, state_);
149 150
  return isolate_->MayAccess(handle(isolate_->context()),
                             GetHolder<JSObject>());
151 152
}

153
template <bool is_element>
154 155
void LookupIterator::ReloadPropertyInformation() {
  state_ = BEFORE_PROPERTY;
156
  interceptor_state_ = InterceptorState::kUninitialized;
157
  state_ = LookupInHolder<is_element>(holder_->map(), *holder_);
158
  DCHECK(IsFound() || !holder_->HasFastProperties());
159 160
}

161
void LookupIterator::InternalUpdateProtector() {
162 163
  if (isolate_->bootstrapper()->IsActive()) return;

164
  if (*name_ == heap()->constructor_string()) {
165
    if (!isolate_->IsArraySpeciesLookupChainIntact()) return;
166 167 168 169 170 171
    // Setting the constructor property could change an instance's @@species
    if (holder_->IsJSArray()) {
      isolate_->CountUsage(
          v8::Isolate::UseCounterFeature::kArrayInstanceConstructorModified);
      isolate_->InvalidateArraySpeciesProtector();
    } else if (holder_->map()->is_prototype_map()) {
172
      DisallowHeapAllocation no_gc;
173 174
      // Setting the constructor of Array.prototype of any realm also needs
      // to invalidate the species protector
175 176
      if (isolate_->IsInAnyContext(*holder_,
                                   Context::INITIAL_ARRAY_PROTOTYPE_INDEX)) {
177 178 179 180 181
        isolate_->CountUsage(v8::Isolate::UseCounterFeature::
                                 kArrayPrototypeConstructorModified);
        isolate_->InvalidateArraySpeciesProtector();
      }
    }
182
  } else if (*name_ == heap()->species_symbol()) {
183
    if (!isolate_->IsArraySpeciesLookupChainIntact()) return;
184 185
    // Setting the Symbol.species property of any Array constructor invalidates
    // the species protector
186
    if (isolate_->IsInAnyContext(*holder_, Context::ARRAY_FUNCTION_INDEX)) {
187 188 189 190
      isolate_->CountUsage(
          v8::Isolate::UseCounterFeature::kArraySpeciesModified);
      isolate_->InvalidateArraySpeciesProtector();
    }
191 192 193
  } else if (*name_ == heap()->is_concat_spreadable_symbol()) {
    if (!isolate_->IsIsConcatSpreadableLookupChainIntact()) return;
    isolate_->InvalidateIsConcatSpreadableProtector();
194 195 196
  } else if (*name_ == heap()->has_instance_symbol()) {
    if (!isolate_->IsHasInstanceLookupChainIntact()) return;
    isolate_->InvalidateHasInstanceProtector();
197 198
  }
}
199

200
void LookupIterator::PrepareForDataProperty(Handle<Object> value) {
201
  DCHECK(state_ == DATA || state_ == ACCESSOR);
202
  DCHECK(HolderIsReceiverOrHiddenPrototype());
203 204 205 206

  Handle<JSObject> holder = GetHolder<JSObject>();

  if (IsElement()) {
207
    ElementsKind kind = holder->GetElementsKind();
208 209
    ElementsKind to = value->OptimalElementsKind();
    if (IsHoleyElementsKind(kind)) to = GetHoleyElementsKind(to);
210
    to = GetMoreGeneralElementsKind(kind, to);
211 212 213 214

    if (kind != to) {
      JSObject::TransitionElementsKind(holder, to);
    }
215 216

    // Copy the backing store if it is copy-on-write.
217
    if (IsFastSmiOrObjectElementsKind(to)) {
218 219
      JSObject::EnsureWritableFastElements(holder);
    }
220 221
    return;
  }
222

223
  if (!holder->HasFastProperties()) return;
224

225 226 227 228 229 230 231 232 233
  Handle<Map> old_map(holder->map(), isolate_);
  Handle<Map> new_map =
      Map::PrepareForDataProperty(old_map, descriptor_number(), value);

  if (old_map.is_identical_to(new_map)) {
    // Update the property details if the representation was None.
    if (representation().IsNone()) {
      property_details_ =
          new_map->instance_descriptors()->GetDetails(descriptor_number());
234
    }
235
    return;
236 237
  }

238
  JSObject::MigrateToMap(holder, new_map);
239
  ReloadPropertyInformation<false>();
240 241 242
}


243 244
void LookupIterator::ReconfigureDataProperty(Handle<Object> value,
                                             PropertyAttributes attributes) {
245
  DCHECK(state_ == DATA || state_ == ACCESSOR);
246 247
  DCHECK(HolderIsReceiverOrHiddenPrototype());
  Handle<JSObject> holder = GetHolder<JSObject>();
248
  if (IsElement()) {
249
    DCHECK(!holder->HasFixedTypedArrayElements());
250
    DCHECK(attributes != NONE || !holder->HasFastElements());
251 252 253
    Handle<FixedArrayBase> elements(holder->elements());
    holder->GetElementsAccessor()->Reconfigure(holder, elements, number_, value,
                                               attributes);
254
    ReloadPropertyInformation<true>();
255
  } else {
256 257 258 259 260 261 262 263 264 265 266 267 268
    if (!holder->HasFastProperties()) {
      PropertyDetails details(attributes, v8::internal::DATA, 0,
                              PropertyCellType::kMutable);
      JSObject::SetNormalizedProperty(holder, name(), value, details);
    } else {
      Handle<Map> old_map(holder->map(), isolate_);
      Handle<Map> new_map = Map::ReconfigureExistingProperty(
          old_map, descriptor_number(), i::kData, attributes);
      new_map =
          Map::PrepareForDataProperty(new_map, descriptor_number(), value);
      JSObject::MigrateToMap(holder, new_map);
    }
    ReloadPropertyInformation<false>();
269 270
  }

271 272 273 274 275 276 277
  WriteDataValue(value);

#if VERIFY_HEAP
  if (FLAG_verify_heap) {
    holder->JSObjectVerify();
  }
#endif
278 279
}

280 281
// Can only be called when the receiver is a JSObject. JSProxy has to be handled
// via a trap. Adding properties to primitive values is not observable.
282
void LookupIterator::PrepareTransitionToDataProperty(
283 284 285
    Handle<JSObject> receiver, Handle<Object> value,
    PropertyAttributes attributes, Object::StoreFromKeyed store_mode) {
  DCHECK(receiver.is_identical_to(GetStoreTarget()));
286
  if (state_ == TRANSITION) return;
287 288 289
  DCHECK(state_ != LookupIterator::ACCESSOR ||
         (GetAccessors()->IsAccessorInfo() &&
          AccessorInfo::cast(*GetAccessors())->is_special_data_property()));
290
  DCHECK_NE(INTEGER_INDEXED_EXOTIC, state_);
291
  DCHECK(state_ == NOT_FOUND || !HolderIsReceiverOrHiddenPrototype());
292

293 294 295 296 297 298 299 300 301
  Handle<Map> map(receiver->map(), isolate_);

  // Dictionary maps can always have additional data properties.
  if (map->is_dictionary_map()) {
    state_ = TRANSITION;
    if (map->IsJSGlobalObjectMap()) {
      // Install a property cell.
      auto cell = JSGlobalObject::EnsurePropertyCell(
          Handle<JSGlobalObject>::cast(receiver), name());
302
      DCHECK(cell->value()->IsTheHole(isolate_));
303 304 305 306 307 308 309 310 311
      transition_ = cell;
    } else {
      transition_ = map;
    }
    return;
  }

  Handle<Map> transition =
      Map::TransitionToDataProperty(map, name_, value, attributes, store_mode);
312
  state_ = TRANSITION;
313 314
  transition_ = transition;

315
  if (!transition->is_dictionary_map()) {
316 317 318
    property_details_ = transition->GetLastDescriptorDetails();
    has_property_ = true;
  }
319 320
}

321
void LookupIterator::ApplyTransitionToDataProperty(Handle<JSObject> receiver) {
322 323
  DCHECK_EQ(TRANSITION, state_);

324 325
  DCHECK(receiver.is_identical_to(GetStoreTarget()));

326
  if (receiver->IsJSGlobalObject()) return;
327
  holder_ = receiver;
328 329
  Handle<Map> transition = transition_map();
  bool simple_transition = transition->GetBackPointer() == receiver->map();
330
  JSObject::MigrateToMap(receiver, transition);
331 332 333 334 335 336 337

  if (simple_transition) {
    int number = transition->LastAdded();
    number_ = static_cast<uint32_t>(number);
    property_details_ = transition->GetLastDescriptorDetails();
    state_ = DATA;
  } else {
338
    ReloadPropertyInformation<false>();
339
  }
340 341 342
}


343
void LookupIterator::Delete() {
344
  Handle<JSReceiver> holder = Handle<JSReceiver>::cast(holder_);
345
  if (IsElement()) {
346 347 348
    Handle<JSObject> object = Handle<JSObject>::cast(holder);
    ElementsAccessor* accessor = object->GetElementsAccessor();
    accessor->Delete(object, number_);
349
  } else {
350 351 352 353 354 355 356 357
    bool is_prototype_map = holder->map()->is_prototype_map();
    RuntimeCallTimerScope stats_scope(
        isolate_, is_prototype_map
                      ? &RuntimeCallStats::PrototypeObject_DeleteProperty
                      : &RuntimeCallStats::Object_DeleteProperty);

    PropertyNormalizationMode mode =
        is_prototype_map ? KEEP_INOBJECT_PROPERTIES : CLEAR_INOBJECT_PROPERTIES;
358 359

    if (holder->HasFastProperties()) {
360 361
      JSObject::NormalizeProperties(Handle<JSObject>::cast(holder), mode, 0,
                                    "DeletingProperty");
362
      ReloadPropertyInformation<false>();
363 364
    }
    // TODO(verwaest): Get rid of the name_ argument.
365 366 367 368
    JSReceiver::DeleteNormalizedProperty(holder, name_, number_);
    if (holder->IsJSObject()) {
      JSObject::ReoptimizeIfPrototype(Handle<JSObject>::cast(holder));
    }
369
  }
370
  state_ = NOT_FOUND;
371 372
}

373
void LookupIterator::TransitionToAccessorProperty(
374
    Handle<Object> getter, Handle<Object> setter,
375
    PropertyAttributes attributes) {
376
  DCHECK(!getter->IsNull(isolate_) || !setter->IsNull(isolate_));
377 378 379
  // Can only be called when the receiver is a JSObject. JSProxy has to be
  // handled via a trap. Adding properties to primitive values is not
  // observable.
380
  Handle<JSObject> receiver = GetStoreTarget();
381

382
  if (!IsElement() && !receiver->map()->is_dictionary_map()) {
383
    Handle<Map> old_map(receiver->map(), isolate_);
384 385 386 387 388 389 390 391 392 393

    if (!holder_.is_identical_to(receiver)) {
      holder_ = receiver;
      state_ = NOT_FOUND;
    } else if (state_ == INTERCEPTOR) {
      LookupInRegularHolder<false>(*old_map, *holder_);
    }
    int descriptor =
        IsFound() ? static_cast<int>(number_) : DescriptorArray::kNotFound;

394
    Handle<Map> new_map = Map::TransitionToAccessorProperty(
395
        isolate_, old_map, name_, descriptor, getter, setter, attributes);
396
    bool simple_transition = new_map->GetBackPointer() == receiver->map();
397
    JSObject::MigrateToMap(receiver, new_map);
398

399 400 401 402 403 404 405
    if (simple_transition) {
      int number = new_map->LastAdded();
      number_ = static_cast<uint32_t>(number);
      property_details_ = new_map->GetLastDescriptorDetails();
      state_ = ACCESSOR;
      return;
    }
406

407
    ReloadPropertyInformation<false>();
408
    if (!new_map->is_dictionary_map()) return;
409
  }
410 411

  Handle<AccessorPair> pair;
412
  if (state() == ACCESSOR && GetAccessors()->IsAccessorPair()) {
413 414
    pair = Handle<AccessorPair>::cast(GetAccessors());
    // If the component and attributes are identical, nothing has to be done.
415
    if (pair->Equals(*getter, *setter)) {
416 417 418 419
      if (property_details().attributes() == attributes) {
        if (!IsElement()) JSObject::ReoptimizeIfPrototype(receiver);
        return;
      }
420 421
    } else {
      pair = AccessorPair::Copy(pair);
422
      pair->SetComponents(*getter, *setter);
423 424
    }
  } else {
425
    pair = factory()->NewAccessorPair();
426
    pair->SetComponents(*getter, *setter);
427 428
  }

429
  TransitionToAccessorPair(pair, attributes);
430 431 432 433 434 435

#if VERIFY_HEAP
  if (FLAG_verify_heap) {
    receiver->JSObjectVerify();
  }
#endif
436 437 438 439 440 441 442 443 444 445 446
}


void LookupIterator::TransitionToAccessorPair(Handle<Object> pair,
                                              PropertyAttributes attributes) {
  Handle<JSObject> receiver = GetStoreTarget();
  holder_ = receiver;

  PropertyDetails details(attributes, ACCESSOR_CONSTANT, 0,
                          PropertyCellType::kMutable);

447 448 449 450 451
  if (IsElement()) {
    // TODO(verwaest): Move code into the element accessor.
    Handle<SeededNumberDictionary> dictionary =
        JSObject::NormalizeElements(receiver);

452 453 454 455 456
    // We unconditionally pass used_as_prototype=false here because the call
    // to RequireSlowElements takes care of the required IC clearing and
    // we don't want to walk the heap twice.
    dictionary =
        SeededNumberDictionary::Set(dictionary, index_, pair, details, false);
457
    receiver->RequireSlowElements(*dictionary);
458 459 460 461 462 463 464 465 466 467 468

    if (receiver->HasSlowArgumentsElements()) {
      FixedArray* parameter_map = FixedArray::cast(receiver->elements());
      uint32_t length = parameter_map->length() - 2;
      if (number_ < length) {
        parameter_map->set(number_ + 2, heap()->the_hole_value());
      }
      FixedArray::cast(receiver->elements())->set(1, *dictionary);
    } else {
      receiver->set_elements(*dictionary);
    }
469 470

    ReloadPropertyInformation<true>();
471
  } else {
472 473 474 475 476 477 478
    PropertyNormalizationMode mode = receiver->map()->is_prototype_map()
                                         ? KEEP_INOBJECT_PROPERTIES
                                         : CLEAR_INOBJECT_PROPERTIES;
    // Normalize object to make this operation simple.
    JSObject::NormalizeProperties(receiver, mode, 0,
                                  "TransitionToAccessorPair");

479 480
    JSObject::SetNormalizedProperty(receiver, name_, pair, details);
    JSObject::ReoptimizeIfPrototype(receiver);
481

482 483
    ReloadPropertyInformation<false>();
  }
484 485 486
}


487
bool LookupIterator::HolderIsReceiverOrHiddenPrototype() const {
488
  DCHECK(has_property_ || state_ == INTERCEPTOR || state_ == JSPROXY);
489
  // Optimization that only works if configuration_ is not mutable.
490
  if (!check_prototype_chain()) return true;
491
  DisallowHeapAllocation no_gc;
492
  if (*receiver_ == *holder_) return true;
493
  if (!receiver_->IsJSReceiver()) return false;
494
  JSReceiver* current = JSReceiver::cast(*receiver_);
495
  JSReceiver* object = *holder_;
496
  if (!current->map()->has_hidden_prototype()) return false;
497
  // JSProxy do not occur as hidden prototypes.
498
  if (object->IsJSProxy()) return false;
499
  PrototypeIterator iter(isolate(), current, kStartAtPrototype,
500 501
                         PrototypeIterator::END_AT_NON_HIDDEN);
  while (!iter.IsAtEnd()) {
502
    if (iter.GetCurrent<JSReceiver>() == object) return true;
503
    iter.Advance();
504
  }
505 506 507 508
  return false;
}


509 510
Handle<Object> LookupIterator::FetchValue() const {
  Object* result = NULL;
511
  if (IsElement()) {
512
    Handle<JSObject> holder = GetHolder<JSObject>();
513
    ElementsAccessor* accessor = holder->GetElementsAccessor();
514
    return accessor->Get(holder, number_);
515
  } else if (holder_->IsJSGlobalObject()) {
516
    Handle<JSObject> holder = GetHolder<JSObject>();
517 518 519
    result = holder->global_dictionary()->ValueAt(number_);
    DCHECK(result->IsPropertyCell());
    result = PropertyCell::cast(result)->value();
520
  } else if (!holder_->HasFastProperties()) {
521
    result = holder_->property_dictionary()->ValueAt(number_);
522
  } else if (property_details_.type() == v8::internal::DATA) {
523
    Handle<JSObject> holder = GetHolder<JSObject>();
524
    FieldIndex field_index = FieldIndex::ForDescriptor(holder->map(), number_);
525 526 527
    return JSObject::FastPropertyAt(holder, property_details_.representation(),
                                    field_index);
  } else {
528
    result = holder_->map()->instance_descriptors()->GetValue(number_);
529 530 531 532 533
  }
  return handle(result, isolate_);
}


534 535
int LookupIterator::GetAccessorIndex() const {
  DCHECK(has_property_);
536
  DCHECK(holder_->HasFastProperties());
537 538 539 540 541
  DCHECK_EQ(v8::internal::ACCESSOR_CONSTANT, property_details_.type());
  return descriptor_number();
}


542 543
int LookupIterator::GetConstantIndex() const {
  DCHECK(has_property_);
544
  DCHECK(holder_->HasFastProperties());
545
  DCHECK_EQ(v8::internal::DATA_CONSTANT, property_details_.type());
546
  DCHECK(!IsElement());
547 548 549 550
  return descriptor_number();
}


551
FieldIndex LookupIterator::GetFieldIndex() const {
552
  DCHECK(has_property_);
553
  DCHECK(holder_->HasFastProperties());
554
  DCHECK_EQ(v8::internal::DATA, property_details_.type());
555
  DCHECK(!IsElement());
556
  Map* holder_map = holder_->map();
557
  int index =
558
      holder_map->instance_descriptors()->GetFieldIndex(descriptor_number());
559
  bool is_double = representation().IsDouble();
560
  return FieldIndex::ForPropertyIndex(holder_map, index, is_double);
561 562
}

563
Handle<FieldType> LookupIterator::GetFieldType() const {
564
  DCHECK(has_property_);
565
  DCHECK(holder_->HasFastProperties());
566
  DCHECK_EQ(v8::internal::DATA, property_details_.type());
567
  return handle(
568
      holder_->map()->instance_descriptors()->GetFieldType(descriptor_number()),
569 570 571 572
      isolate_);
}


573
Handle<PropertyCell> LookupIterator::GetPropertyCell() const {
574
  DCHECK(!IsElement());
575
  Handle<JSObject> holder = GetHolder<JSObject>();
576
  Handle<JSGlobalObject> global = Handle<JSGlobalObject>::cast(holder);
577
  Object* value = global->global_dictionary()->ValueAt(dictionary_entry());
578
  DCHECK(value->IsPropertyCell());
579
  return handle(PropertyCell::cast(value));
580 581 582
}


583
Handle<Object> LookupIterator::GetAccessors() const {
584
  DCHECK_EQ(ACCESSOR, state_);
585 586 587 588 589
  return FetchValue();
}


Handle<Object> LookupIterator::GetDataValue() const {
590
  DCHECK_EQ(DATA, state_);
591 592 593 594 595
  Handle<Object> value = FetchValue();
  return value;
}


596
void LookupIterator::WriteDataValue(Handle<Object> value) {
597
  DCHECK_EQ(DATA, state_);
598
  Handle<JSReceiver> holder = GetHolder<JSReceiver>();
599
  if (IsElement()) {
600 601
    Handle<JSObject> object = Handle<JSObject>::cast(holder);
    ElementsAccessor* accessor = object->GetElementsAccessor();
602
    accessor->Set(object, number_, *value);
603 604 605 606 607 608 609
  } else if (holder->HasFastProperties()) {
    if (property_details_.type() == v8::internal::DATA) {
      JSObject::cast(*holder)->WriteToField(descriptor_number(),
                                            property_details_, *value);
    } else {
      DCHECK_EQ(v8::internal::DATA_CONSTANT, property_details_.type());
    }
610
  } else if (holder->IsJSGlobalObject()) {
611
    Handle<GlobalDictionary> property_dictionary =
612
        handle(JSObject::cast(*holder)->global_dictionary());
613 614
    PropertyCell::UpdateCell(property_dictionary, dictionary_entry(), value,
                             property_details_);
615
  } else {
616
    NameDictionary* property_dictionary = holder->property_dictionary();
617
    property_dictionary->ValueAtPut(dictionary_entry(), *value);
618 619 620
  }
}

621
template <bool is_element>
622
bool LookupIterator::SkipInterceptor(JSObject* holder) {
623
  auto info = GetInterceptor<is_element>(holder);
624 625 626 627 628 629 630 631 632 633 634 635 636 637
  // TODO(dcarney): check for symbol/can_intercept_symbols here as well.
  if (info->non_masking()) {
    switch (interceptor_state_) {
      case InterceptorState::kUninitialized:
        interceptor_state_ = InterceptorState::kSkipNonMasking;
      // Fall through.
      case InterceptorState::kSkipNonMasking:
        return true;
      case InterceptorState::kProcessNonMasking:
        return false;
    }
  }
  return interceptor_state_ == InterceptorState::kProcessNonMasking;
}
638 639 640

JSReceiver* LookupIterator::NextHolder(Map* map) {
  DisallowHeapAllocation no_gc;
641
  if (map->prototype() == heap()->null_value()) return NULL;
642
  if (!check_prototype_chain() && !map->has_hidden_prototype()) return NULL;
643
  return JSReceiver::cast(map->prototype());
644 645
}

646 647 648 649 650 651 652 653 654 655 656
LookupIterator::State LookupIterator::NotFound(JSReceiver* const holder) const {
  DCHECK(!IsElement());
  if (!holder->IsJSTypedArray() || !name_->IsString()) return NOT_FOUND;

  Handle<String> name_string = Handle<String>::cast(name_);
  if (name_string->length() == 0) return NOT_FOUND;

  return IsSpecialIndex(isolate_->unicode_cache(), *name_string)
             ? INTEGER_INDEXED_EXOTIC
             : NOT_FOUND;
}
657

658 659 660 661 662 663 664 665 666 667 668 669 670
namespace {

template <bool is_element>
bool HasInterceptor(Map* map) {
  return is_element ? map->has_indexed_interceptor()
                    : map->has_named_interceptor();
}

}  // namespace

template <bool is_element>
LookupIterator::State LookupIterator::LookupInSpecialHolder(
    Map* const map, JSReceiver* const holder) {
671 672 673
  STATIC_ASSERT(INTERCEPTOR == BEFORE_PROPERTY);
  switch (state_) {
    case NOT_FOUND:
674
      if (map->IsJSProxyMap()) {
675
        if (is_element || !name_->IsPrivate()) return JSPROXY;
676
      }
677
      if (map->is_access_check_needed()) {
678
        if (is_element || !name_->IsPrivate()) return ACCESS_CHECK;
679 680 681
      }
    // Fall through.
    case ACCESS_CHECK:
682
      if (check_interceptor() && HasInterceptor<is_element>(map) &&
683
          !SkipInterceptor<is_element>(JSObject::cast(holder))) {
684
        if (is_element || !name_->IsPrivate()) return INTERCEPTOR;
685 686 687
      }
    // Fall through.
    case INTERCEPTOR:
688
      if (!is_element && map->IsJSGlobalObjectMap()) {
689 690 691 692 693 694
        GlobalDictionary* dict = JSObject::cast(holder)->global_dictionary();
        int number = dict->FindEntry(name_);
        if (number == GlobalDictionary::kNotFound) return NOT_FOUND;
        number_ = static_cast<uint32_t>(number);
        DCHECK(dict->ValueAt(number_)->IsPropertyCell());
        PropertyCell* cell = PropertyCell::cast(dict->ValueAt(number_));
695
        if (cell->value()->IsTheHole(isolate_)) return NOT_FOUND;
696
        property_details_ = cell->property_details();
697 698 699 700 701 702 703
        has_property_ = true;
        switch (property_details_.kind()) {
          case v8::internal::kData:
            return DATA;
          case v8::internal::kAccessor:
            return ACCESSOR;
        }
704
      }
705
      return LookupInRegularHolder<is_element>(map, holder);
706 707 708 709 710 711 712 713 714
    case ACCESSOR:
    case DATA:
      return NOT_FOUND;
    case INTEGER_INDEXED_EXOTIC:
    case JSPROXY:
    case TRANSITION:
      UNREACHABLE();
  }
  UNREACHABLE();
715
  return NOT_FOUND;
716 717
}

718 719
template <bool is_element>
LookupIterator::State LookupIterator::LookupInRegularHolder(
720
    Map* const map, JSReceiver* const holder) {
721 722 723
  DisallowHeapAllocation no_gc;
  if (interceptor_state_ == InterceptorState::kProcessNonMasking) {
    return NOT_FOUND;
verwaest's avatar
verwaest committed
724
  }
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

  if (is_element) {
    JSObject* js_object = JSObject::cast(holder);
    ElementsAccessor* accessor = js_object->GetElementsAccessor();
    FixedArrayBase* backing_store = js_object->elements();
    number_ = accessor->GetEntryForIndex(js_object, backing_store, index_);
    if (number_ == kMaxUInt32) {
      return holder->IsJSTypedArray() ? INTEGER_INDEXED_EXOTIC : NOT_FOUND;
    }
    property_details_ = accessor->GetDetails(js_object, number_);
  } else if (!map->is_dictionary_map()) {
    DescriptorArray* descriptors = map->instance_descriptors();
    int number = descriptors->SearchWithCache(isolate_, *name_, map);
    if (number == DescriptorArray::kNotFound) return NotFound(holder);
    number_ = static_cast<uint32_t>(number);
    property_details_ = descriptors->GetDetails(number_);
  } else {
    NameDictionary* dict = holder->property_dictionary();
    int number = dict->FindEntry(name_);
    if (number == NameDictionary::kNotFound) return NotFound(holder);
    number_ = static_cast<uint32_t>(number);
    property_details_ = dict->DetailsAt(number_);
  }
  has_property_ = true;
  switch (property_details_.kind()) {
    case v8::internal::kData:
      return DATA;
    case v8::internal::kAccessor:
      return ACCESSOR;
  }

756 757 758 759
  UNREACHABLE();
  return state_;
}

760 761
}  // namespace internal
}  // namespace v8