runtime-collections.cc 3.75 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/arguments-inl.h"
6
#include "src/conversions-inl.h"
7
#include "src/counters.h"
8
#include "src/heap/factory.h"
9
#include "src/objects/hash-table-inl.h"
10
#include "src/objects/js-collection-inl.h"
11
#include "src/runtime/runtime-utils.h"
12 13 14 15

namespace v8 {
namespace internal {

16 17
RUNTIME_FUNCTION(Runtime_TheHole) {
  SealHandleScope shs(isolate);
18
  DCHECK_EQ(0, args.length());
19
  return ReadOnlyRoots(isolate).the_hole_value();
20 21 22
}

RUNTIME_FUNCTION(Runtime_SetGrow) {
23
  HandleScope scope(isolate);
24
  DCHECK_EQ(1, args.length());
25
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
26
  Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table()), isolate);
27
  table = OrderedHashSet::EnsureGrowable(isolate, table);
28
  holder->set_table(*table);
29
  return ReadOnlyRoots(isolate).undefined_value();
30 31 32
}


33
RUNTIME_FUNCTION(Runtime_SetShrink) {
34
  HandleScope scope(isolate);
35
  DCHECK_EQ(1, args.length());
36
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
37
  Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table()), isolate);
38
  table = OrderedHashSet::Shrink(isolate, table);
39
  holder->set_table(*table);
40
  return ReadOnlyRoots(isolate).undefined_value();
41 42
}

43
RUNTIME_FUNCTION(Runtime_MapShrink) {
44
  HandleScope scope(isolate);
45
  DCHECK_EQ(1, args.length());
46
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
47
  Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table()), isolate);
48
  table = OrderedHashMap::Shrink(isolate, table);
49
  holder->set_table(*table);
50
  return ReadOnlyRoots(isolate).undefined_value();
51 52
}

53
RUNTIME_FUNCTION(Runtime_MapGrow) {
54
  HandleScope scope(isolate);
55
  DCHECK_EQ(1, args.length());
56
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
57
  Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table()), isolate);
58
  table = OrderedHashMap::EnsureGrowable(isolate, table);
59
  holder->set_table(*table);
60
  return ReadOnlyRoots(isolate).undefined_value();
61 62
}

63 64 65 66 67 68
RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) {
  HandleScope scope(isolate);
  DCHECK_EQ(3, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
  CONVERT_SMI_ARG_CHECKED(hash, 2)
69 70 71

#ifdef DEBUG
  DCHECK(key->IsJSReceiver());
72
  DCHECK(EphemeronHashTableShape::IsLive(ReadOnlyRoots(isolate), *key));
73
  Handle<EphemeronHashTable> table(
74
      EphemeronHashTable::cast(weak_collection->table()), isolate);
75 76 77 78 79 80
  // Should only be called when shrinking the table is necessary. See
  // HashTable::Shrink().
  DCHECK(table->NumberOfElements() - 1 <= (table->Capacity() >> 2) &&
         table->NumberOfElements() - 1 >= 16);
#endif

81 82 83 84 85 86 87 88 89 90 91
  bool was_present = JSWeakCollection::Delete(weak_collection, key, hash);
  return isolate->heap()->ToBoolean(was_present);
}

RUNTIME_FUNCTION(Runtime_WeakCollectionSet) {
  HandleScope scope(isolate);
  DCHECK_EQ(4, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
  CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
  CONVERT_SMI_ARG_CHECKED(hash, 3)
92 93 94

#ifdef DEBUG
  DCHECK(key->IsJSReceiver());
95
  DCHECK(EphemeronHashTableShape::IsLive(ReadOnlyRoots(isolate), *key));
96
  Handle<EphemeronHashTable> table(
97
      EphemeronHashTable::cast(weak_collection->table()), isolate);
98
  // Should only be called when rehashing or resizing the table is necessary.
99
  // See EphemeronHashTable::Put() and HashTable::HasSufficientCapacityToAdd().
100 101 102 103
  DCHECK((table->NumberOfDeletedElements() << 1) > table->NumberOfElements() ||
         !table->HasSufficientCapacityToAdd(1));
#endif

104 105 106 107
  JSWeakCollection::Set(weak_collection, key, value, hash);
  return *weak_collection;
}

108 109
}  // namespace internal
}  // namespace v8