runtime-forin.cc 5.4 KB
Newer Older
1 2 3 4 5
// Copyright 2015 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.

#include "src/runtime/runtime-utils.h"
6

7 8
#include "src/execution/arguments-inl.h"
#include "src/execution/isolate-inl.h"
9
#include "src/heap/factory.h"
10
#include "src/heap/heap-inl.h"  // For ToBoolean. TODO(jkummerow): Drop.
11
#include "src/logging/counters.h"
12
#include "src/objects/elements.h"
13
#include "src/objects/keys.h"
14
#include "src/objects/module.h"
15
#include "src/objects/objects-inl.h"
16 17 18 19

namespace v8 {
namespace internal {

20 21 22 23 24 25
namespace {

// Returns either a FixedArray or, if the given {receiver} has an enum cache
// that contains all enumerable properties of the {receiver} and its prototypes
// have none, the map of the {receiver}. This is used to speed up the check for
// deletions during a for-in.
26 27
MaybeHandle<HeapObject> Enumerate(Isolate* isolate,
                                  Handle<JSReceiver> receiver) {
28
  JSObject::MakePrototypesFast(receiver, kStartAtReceiver, isolate);
29 30
  FastKeyAccumulator accumulator(isolate, receiver,
                                 KeyCollectionMode::kIncludePrototypes,
31
                                 ENUMERABLE_STRINGS, true);
32
  // Test if we have an enum cache for {receiver}.
33
  if (!accumulator.is_receiver_simple_enum()) {
34
    Handle<FixedArray> keys;
35
    ASSIGN_RETURN_ON_EXCEPTION(
36 37 38 39
        isolate, keys,
        accumulator.GetKeys(accumulator.may_have_elements()
                                ? GetKeysConversion::kConvertToString
                                : GetKeysConversion::kNoNumbers),
40
        HeapObject);
41
    // Test again, since cache may have been built by GetKeys() calls above.
42
    if (!accumulator.is_receiver_simple_enum()) return keys;
43
  }
44
  DCHECK(!receiver->IsJSModuleNamespace());
45 46 47
  return handle(receiver->map(), isolate);
}

48
// This is a slight modification of JSReceiver::HasProperty, dealing with
49
// the oddities of JSProxy and JSModuleNamespace in for-in filter.
50 51 52 53 54
MaybeHandle<Object> HasEnumerableProperty(Isolate* isolate,
                                          Handle<JSReceiver> receiver,
                                          Handle<Object> key) {
  bool success = false;
  Maybe<PropertyAttributes> result = Just(ABSENT);
55
  PropertyKey lookup_key(isolate, key, &success);
56
  if (!success) return isolate->factory()->undefined_value();
57
  LookupIterator it(isolate, receiver, lookup_key);
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  for (; it.IsFound(); it.Next()) {
    switch (it.state()) {
      case LookupIterator::NOT_FOUND:
      case LookupIterator::TRANSITION:
        UNREACHABLE();
      case LookupIterator::JSPROXY: {
        // For proxies we have to invoke the [[GetOwnProperty]] trap.
        result = JSProxy::GetPropertyAttributes(&it);
        if (result.IsNothing()) return MaybeHandle<Object>();
        if (result.FromJust() == ABSENT) {
          // Continue lookup on the proxy's prototype.
          Handle<JSProxy> proxy = it.GetHolder<JSProxy>();
          Handle<Object> prototype;
          ASSIGN_RETURN_ON_EXCEPTION(isolate, prototype,
                                     JSProxy::GetPrototype(proxy), Object);
73 74 75
          if (prototype->IsNull(isolate)) {
            return isolate->factory()->undefined_value();
          }
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
          // We already have a stack-check in JSProxy::GetPrototype.
          return HasEnumerableProperty(
              isolate, Handle<JSReceiver>::cast(prototype), key);
        } else if (result.FromJust() & DONT_ENUM) {
          return isolate->factory()->undefined_value();
        } else {
          return it.GetName();
        }
      }
      case LookupIterator::INTERCEPTOR: {
        result = JSObject::GetPropertyAttributesWithInterceptor(&it);
        if (result.IsNothing()) return MaybeHandle<Object>();
        if (result.FromJust() != ABSENT) return it.GetName();
        continue;
      }
      case LookupIterator::ACCESS_CHECK: {
        if (it.HasAccess()) continue;
        result = JSObject::GetPropertyAttributesWithFailedAccessCheck(&it);
        if (result.IsNothing()) return MaybeHandle<Object>();
        if (result.FromJust() != ABSENT) return it.GetName();
        return isolate->factory()->undefined_value();
      }
      case LookupIterator::INTEGER_INDEXED_EXOTIC:
        // TypedArray out-of-bounds access.
        return isolate->factory()->undefined_value();
101 102 103 104 105 106 107 108
      case LookupIterator::ACCESSOR: {
        if (it.GetHolder<Object>()->IsJSModuleNamespace()) {
          result = JSModuleNamespace::GetPropertyAttributes(&it);
          if (result.IsNothing()) return MaybeHandle<Object>();
          DCHECK_EQ(0, result.FromJust() & DONT_ENUM);
        }
        return it.GetName();
      }
109 110
      case LookupIterator::DATA:
        return it.GetName();
111 112
    }
  }
113 114 115 116 117 118 119
  return isolate->factory()->undefined_value();
}

}  // namespace


RUNTIME_FUNCTION(Runtime_ForInEnumerate) {
120 121
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
122
  Handle<JSReceiver> receiver = args.at<JSReceiver>(0);
123
  RETURN_RESULT_OR_FAILURE(isolate, Enumerate(isolate, receiver));
124
}
125 126


127 128 129
RUNTIME_FUNCTION(Runtime_ForInHasProperty) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
130 131
  Handle<JSReceiver> receiver = args.at<JSReceiver>(0);
  Handle<Object> key = args.at(1);
132 133 134 135 136
  Handle<Object> result;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      isolate, result, HasEnumerableProperty(isolate, receiver, key));
  return isolate->heap()->ToBoolean(!result->IsUndefined(isolate));
}
137 138 139

}  // namespace internal
}  // namespace v8