v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
runtime-collections.cc
Go to the documentation of this file.
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
6#include "src/heap/factory.h"
7#include "src/heap/heap-inl.h" // For ToBoolean. TODO(jkummerow): Drop.
10
11namespace v8 {
12namespace internal {
13
14RUNTIME_FUNCTION(Runtime_TheHole) {
15 SealHandleScope shs(isolate);
16 DCHECK_EQ(0, args.length());
17 return ReadOnlyRoots(isolate).the_hole_value();
18}
19
20RUNTIME_FUNCTION(Runtime_OrderedHashSetGrow) {
21 HandleScope scope(isolate);
22 DCHECK_EQ(2, args.length());
24 Handle<String> method_name = args.at<String>(1);
25 MaybeHandle<OrderedHashSet> table_candidate =
27 if (!table_candidate.ToHandle(&table)) {
28 // Replace generic RangeError exception with a more descriptive one.
29 DCHECK(isolate->has_exception());
30 isolate->clear_exception();
32 isolate, NewRangeError(MessageTemplate::kOutOfMemory, method_name));
33 }
34 return *table;
35}
36
37RUNTIME_FUNCTION(Runtime_SetGrow) {
38 HandleScope scope(isolate);
39 DCHECK_EQ(1, args.length());
40 DirectHandle<JSSet> holder = args.at<JSSet>(0);
41 Handle<OrderedHashSet> table(Cast<OrderedHashSet>(holder->table()), isolate);
42 MaybeHandle<OrderedHashSet> table_candidate =
44 if (!table_candidate.ToHandle(&table)) {
45 // Replace generic RangeError exception with a more descriptive one.
46 DCHECK(isolate->has_exception());
47 isolate->clear_exception();
49 isolate,
50 NewRangeError(MessageTemplate::kCollectionGrowFailed,
51 isolate->factory()->NewStringFromAsciiChecked("Set")));
52 }
53 holder->set_table(*table);
54 return ReadOnlyRoots(isolate).undefined_value();
55}
56
57RUNTIME_FUNCTION(Runtime_SetShrink) {
58 HandleScope scope(isolate);
59 DCHECK_EQ(1, args.length());
60 DirectHandle<JSSet> holder = args.at<JSSet>(0);
61 Handle<OrderedHashSet> table(Cast<OrderedHashSet>(holder->table()), isolate);
62 table = OrderedHashSet::Shrink(isolate, table);
63 holder->set_table(*table);
64 return ReadOnlyRoots(isolate).undefined_value();
65}
66
67RUNTIME_FUNCTION(Runtime_OrderedHashSetShrink) {
68 HandleScope scope(isolate);
69 DCHECK_EQ(1, args.length());
71 table = OrderedHashSet::Shrink(isolate, table);
72 return *table;
73}
74
75RUNTIME_FUNCTION(Runtime_MapShrink) {
76 HandleScope scope(isolate);
77 DCHECK_EQ(1, args.length());
78 DirectHandle<JSMap> holder = args.at<JSMap>(0);
79 Handle<OrderedHashMap> table(Cast<OrderedHashMap>(holder->table()), isolate);
80 table = OrderedHashMap::Shrink(isolate, table);
81 holder->set_table(*table);
82 return ReadOnlyRoots(isolate).undefined_value();
83}
84
85RUNTIME_FUNCTION(Runtime_MapGrow) {
86 HandleScope scope(isolate);
87 DCHECK_EQ(1, args.length());
88 DirectHandle<JSMap> holder = args.at<JSMap>(0);
89 Handle<OrderedHashMap> table(Cast<OrderedHashMap>(holder->table()), isolate);
90 MaybeHandle<OrderedHashMap> table_candidate =
92 if (!table_candidate.ToHandle(&table)) {
93 // Replace generic RangeError exception with a more descriptive one.
94 DCHECK(isolate->has_exception());
95 isolate->clear_exception();
97 isolate,
98 NewRangeError(MessageTemplate::kCollectionGrowFailed,
99 isolate->factory()->NewStringFromAsciiChecked("Map")));
100 }
101 holder->set_table(*table);
102 return ReadOnlyRoots(isolate).undefined_value();
103}
104
105RUNTIME_FUNCTION(Runtime_OrderedHashMapGrow) {
106 HandleScope scope(isolate);
107 DCHECK_EQ(2, args.length());
109 Handle<String> methodName = args.at<String>(1);
110 MaybeHandle<OrderedHashMap> table_candidate =
112 if (!table_candidate.ToHandle(&table)) {
113 // Replace generic RangeError exception with a more descriptive one.
114 DCHECK(isolate->has_exception());
115 isolate->clear_exception();
117 isolate, NewRangeError(MessageTemplate::kOutOfMemory, methodName));
118 }
119 return *table;
120}
121
122RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) {
123 HandleScope scope(isolate);
124 DCHECK_EQ(3, args.length());
127 int hash = args.smi_value_at(2);
128
129#ifdef DEBUG
131 DCHECK(EphemeronHashTable::IsKey(ReadOnlyRoots(isolate), *key));
133 Cast<EphemeronHashTable>(weak_collection->table()), isolate);
134 // Should only be called when shrinking the table is necessary. See
135 // HashTable::Shrink().
136 DCHECK(table->NumberOfElements() - 1 <= (table->Capacity() >> 2) &&
137 table->NumberOfElements() - 1 >= 16);
138#endif
139
140 bool was_present = JSWeakCollection::Delete(weak_collection, key, hash);
141 return isolate->heap()->ToBoolean(was_present);
142}
143
144RUNTIME_FUNCTION(Runtime_WeakCollectionSet) {
145 HandleScope scope(isolate);
146 DCHECK_EQ(4, args.length());
149 DirectHandle<Object> value = args.at(2);
150 int hash = args.smi_value_at(3);
151
152#ifdef DEBUG
154 DCHECK(EphemeronHashTable::IsKey(ReadOnlyRoots(isolate), *key));
156 Cast<EphemeronHashTable>(weak_collection->table()), isolate);
157 // Should only be called when rehashing or resizing the table is necessary.
158 // See EphemeronHashTable::Put() and HashTable::HasSufficientCapacityToAdd().
159 DCHECK((table->NumberOfDeletedElements() << 1) > table->NumberOfElements() ||
160 !table->HasSufficientCapacityToAdd(1));
161#endif
162
163 JSWeakCollection::Set(weak_collection, key, value, hash);
164 return *weak_collection;
165}
166
167} // namespace internal
168} // namespace v8
static bool Delete(DirectHandle< JSWeakCollection > collection, DirectHandle< Object > key, int32_t hash)
Definition objects.cc:6330
static V8_EXPORT_PRIVATE void Set(DirectHandle< JSWeakCollection > collection, DirectHandle< Object > key, DirectHandle< Object > value, int32_t hash)
Definition objects.cc:6313
V8_WARN_UNUSED_RESULT V8_INLINE bool ToHandle(Handle< S > *out) const
static bool CanBeHeldWeakly(Tagged< Object > obj)
static HandleType< OrderedHashSet > Shrink(Isolate *isolate, HandleType< OrderedHashSet > table)
static HandleType< OrderedHashSet >::MaybeType EnsureCapacityForAdding(Isolate *isolate, HandleType< OrderedHashSet > table)
#define RUNTIME_FUNCTION(Name)
Definition arguments.h:162
#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call)
Definition isolate.h:294
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485