runtime-collections.cc 4.39 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/execution/arguments-inl.h"
6
#include "src/heap/factory.h"
7
#include "src/heap/heap-inl.h"  // For ToBoolean. TODO(jkummerow): Drop.
8
#include "src/logging/counters.h"
9
#include "src/numbers/conversions-inl.h"
10
#include "src/objects/hash-table-inl.h"
11
#include "src/objects/js-collection-inl.h"
12
#include "src/runtime/runtime-utils.h"
13 14 15 16

namespace v8 {
namespace internal {

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

RUNTIME_FUNCTION(Runtime_SetGrow) {
24
  HandleScope scope(isolate);
25
  DCHECK_EQ(1, args.length());
26
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
27
  Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table()), isolate);
28 29 30 31
  MaybeHandle<OrderedHashSet> table_candidate =
      OrderedHashSet::EnsureGrowable(isolate, table);
  if (!table_candidate.ToHandle(&table)) {
    THROW_NEW_ERROR_RETURN_FAILURE(
32 33 34
        isolate,
        NewRangeError(MessageTemplate::kCollectionGrowFailed,
                      isolate->factory()->NewStringFromAsciiChecked("Set")));
35
  }
36
  holder->set_table(*table);
37
  return ReadOnlyRoots(isolate).undefined_value();
38 39
}

40
RUNTIME_FUNCTION(Runtime_SetShrink) {
41
  HandleScope scope(isolate);
42
  DCHECK_EQ(1, args.length());
43
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
44
  Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table()), isolate);
45
  table = OrderedHashSet::Shrink(isolate, table);
46
  holder->set_table(*table);
47
  return ReadOnlyRoots(isolate).undefined_value();
48 49
}

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

60
RUNTIME_FUNCTION(Runtime_MapGrow) {
61
  HandleScope scope(isolate);
62
  DCHECK_EQ(1, args.length());
63
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
64
  Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table()), isolate);
65 66 67 68
  MaybeHandle<OrderedHashMap> table_candidate =
      OrderedHashMap::EnsureGrowable(isolate, table);
  if (!table_candidate.ToHandle(&table)) {
    THROW_NEW_ERROR_RETURN_FAILURE(
69 70 71
        isolate,
        NewRangeError(MessageTemplate::kCollectionGrowFailed,
                      isolate->factory()->NewStringFromAsciiChecked("Map")));
72
  }
73
  holder->set_table(*table);
74
  return ReadOnlyRoots(isolate).undefined_value();
75 76
}

77 78 79 80 81 82
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)
83 84 85

#ifdef DEBUG
  DCHECK(key->IsJSReceiver());
86
  DCHECK(EphemeronHashTable::IsKey(ReadOnlyRoots(isolate), *key));
87
  Handle<EphemeronHashTable> table(
88
      EphemeronHashTable::cast(weak_collection->table()), isolate);
89 90 91 92 93 94
  // 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

95 96 97 98 99 100 101 102 103 104 105
  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)
106 107 108

#ifdef DEBUG
  DCHECK(key->IsJSReceiver());
109
  DCHECK(EphemeronHashTable::IsKey(ReadOnlyRoots(isolate), *key));
110
  Handle<EphemeronHashTable> table(
111
      EphemeronHashTable::cast(weak_collection->table()), isolate);
112
  // Should only be called when rehashing or resizing the table is necessary.
113
  // See EphemeronHashTable::Put() and HashTable::HasSufficientCapacityToAdd().
114 115 116 117
  DCHECK((table->NumberOfDeletedElements() << 1) > table->NumberOfElements() ||
         !table->HasSufficientCapacityToAdd(1));
#endif

118 119 120 121
  JSWeakCollection::Set(weak_collection, key, value, hash);
  return *weak_collection;
}

122 123
}  // namespace internal
}  // namespace v8