V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
runtime-collections.cc
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
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 namespace v8 {
14 namespace internal {
15 
16 RUNTIME_FUNCTION(Runtime_TheHole) {
17  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 RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) {
64  HandleScope scope(isolate);
65  DCHECK_EQ(3, args.length());
66  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
67  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
68  CONVERT_SMI_ARG_CHECKED(hash, 2)
69 
70 #ifdef DEBUG
71  DCHECK(key->IsJSReceiver());
72  DCHECK(EphemeronHashTableShape::IsLive(ReadOnlyRoots(isolate), *key));
73  Handle<EphemeronHashTable> table(
74  EphemeronHashTable::cast(weak_collection->table()), isolate);
75  // Should only be called when shrinking the table is necessary. See
76  // HashTable::Shrink().
77  DCHECK(table->NumberOfElements() - 1 <= (table->Capacity() >> 2) &&
78  table->NumberOfElements() - 1 >= 16);
79 #endif
80 
81  bool was_present = JSWeakCollection::Delete(weak_collection, key, hash);
82  return isolate->heap()->ToBoolean(was_present);
83 }
84 
85 RUNTIME_FUNCTION(Runtime_WeakCollectionSet) {
86  HandleScope scope(isolate);
87  DCHECK_EQ(4, args.length());
88  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
89  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
90  CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
91  CONVERT_SMI_ARG_CHECKED(hash, 3)
92 
93 #ifdef DEBUG
94  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  DCHECK((table->NumberOfDeletedElements() << 1) > table->NumberOfElements() ||
101  !table->HasSufficientCapacityToAdd(1));
102 #endif
103 
104  JSWeakCollection::Set(weak_collection, key, value, hash);
105  return *weak_collection;
106 }
107 
108 } // namespace internal
109 } // namespace v8
Definition: libplatform.h:13