OLD | NEW |
(Empty) | |
| 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/v8.h" |
| 6 |
| 7 #include "src/arguments.h" |
| 8 #include "src/runtime/runtime.h" |
| 9 #include "src/runtime/runtime-utils.h" |
| 10 |
| 11 |
| 12 namespace v8 { |
| 13 namespace internal { |
| 14 |
| 15 RUNTIME_FUNCTION(Runtime_SetInitialize) { |
| 16 HandleScope scope(isolate); |
| 17 DCHECK(args.length() == 1); |
| 18 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 19 Handle<OrderedHashSet> table = isolate->factory()->NewOrderedHashSet(); |
| 20 holder->set_table(*table); |
| 21 return *holder; |
| 22 } |
| 23 |
| 24 |
| 25 RUNTIME_FUNCTION(Runtime_SetAdd) { |
| 26 HandleScope scope(isolate); |
| 27 DCHECK(args.length() == 2); |
| 28 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 29 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 30 Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table())); |
| 31 table = OrderedHashSet::Add(table, key); |
| 32 holder->set_table(*table); |
| 33 return *holder; |
| 34 } |
| 35 |
| 36 |
| 37 RUNTIME_FUNCTION(Runtime_SetHas) { |
| 38 HandleScope scope(isolate); |
| 39 DCHECK(args.length() == 2); |
| 40 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 41 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 42 Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table())); |
| 43 return isolate->heap()->ToBoolean(table->Contains(key)); |
| 44 } |
| 45 |
| 46 |
| 47 RUNTIME_FUNCTION(Runtime_SetDelete) { |
| 48 HandleScope scope(isolate); |
| 49 DCHECK(args.length() == 2); |
| 50 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 51 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 52 Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table())); |
| 53 bool was_present = false; |
| 54 table = OrderedHashSet::Remove(table, key, &was_present); |
| 55 holder->set_table(*table); |
| 56 return isolate->heap()->ToBoolean(was_present); |
| 57 } |
| 58 |
| 59 |
| 60 RUNTIME_FUNCTION(Runtime_SetClear) { |
| 61 HandleScope scope(isolate); |
| 62 DCHECK(args.length() == 1); |
| 63 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 64 Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table())); |
| 65 table = OrderedHashSet::Clear(table); |
| 66 holder->set_table(*table); |
| 67 return isolate->heap()->undefined_value(); |
| 68 } |
| 69 |
| 70 |
| 71 RUNTIME_FUNCTION(Runtime_SetGetSize) { |
| 72 HandleScope scope(isolate); |
| 73 DCHECK(args.length() == 1); |
| 74 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 75 Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table())); |
| 76 return Smi::FromInt(table->NumberOfElements()); |
| 77 } |
| 78 |
| 79 |
| 80 RUNTIME_FUNCTION(Runtime_SetIteratorInitialize) { |
| 81 HandleScope scope(isolate); |
| 82 DCHECK(args.length() == 3); |
| 83 CONVERT_ARG_HANDLE_CHECKED(JSSetIterator, holder, 0); |
| 84 CONVERT_ARG_HANDLE_CHECKED(JSSet, set, 1); |
| 85 CONVERT_SMI_ARG_CHECKED(kind, 2) |
| 86 RUNTIME_ASSERT(kind == JSSetIterator::kKindValues || |
| 87 kind == JSSetIterator::kKindEntries); |
| 88 Handle<OrderedHashSet> table(OrderedHashSet::cast(set->table())); |
| 89 holder->set_table(*table); |
| 90 holder->set_index(Smi::FromInt(0)); |
| 91 holder->set_kind(Smi::FromInt(kind)); |
| 92 return isolate->heap()->undefined_value(); |
| 93 } |
| 94 |
| 95 |
| 96 RUNTIME_FUNCTION(Runtime_SetIteratorNext) { |
| 97 SealHandleScope shs(isolate); |
| 98 DCHECK(args.length() == 2); |
| 99 CONVERT_ARG_CHECKED(JSSetIterator, holder, 0); |
| 100 CONVERT_ARG_CHECKED(JSArray, value_array, 1); |
| 101 return holder->Next(value_array); |
| 102 } |
| 103 |
| 104 |
| 105 RUNTIME_FUNCTION(Runtime_MapInitialize) { |
| 106 HandleScope scope(isolate); |
| 107 DCHECK(args.length() == 1); |
| 108 CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
| 109 Handle<OrderedHashMap> table = isolate->factory()->NewOrderedHashMap(); |
| 110 holder->set_table(*table); |
| 111 return *holder; |
| 112 } |
| 113 |
| 114 |
| 115 RUNTIME_FUNCTION(Runtime_MapGet) { |
| 116 HandleScope scope(isolate); |
| 117 DCHECK(args.length() == 2); |
| 118 CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
| 119 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 120 Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table())); |
| 121 Handle<Object> lookup(table->Lookup(key), isolate); |
| 122 return lookup->IsTheHole() ? isolate->heap()->undefined_value() : *lookup; |
| 123 } |
| 124 |
| 125 |
| 126 RUNTIME_FUNCTION(Runtime_MapHas) { |
| 127 HandleScope scope(isolate); |
| 128 DCHECK(args.length() == 2); |
| 129 CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
| 130 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 131 Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table())); |
| 132 Handle<Object> lookup(table->Lookup(key), isolate); |
| 133 return isolate->heap()->ToBoolean(!lookup->IsTheHole()); |
| 134 } |
| 135 |
| 136 |
| 137 RUNTIME_FUNCTION(Runtime_MapDelete) { |
| 138 HandleScope scope(isolate); |
| 139 DCHECK(args.length() == 2); |
| 140 CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
| 141 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 142 Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table())); |
| 143 bool was_present = false; |
| 144 Handle<OrderedHashMap> new_table = |
| 145 OrderedHashMap::Remove(table, key, &was_present); |
| 146 holder->set_table(*new_table); |
| 147 return isolate->heap()->ToBoolean(was_present); |
| 148 } |
| 149 |
| 150 |
| 151 RUNTIME_FUNCTION(Runtime_MapClear) { |
| 152 HandleScope scope(isolate); |
| 153 DCHECK(args.length() == 1); |
| 154 CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
| 155 Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table())); |
| 156 table = OrderedHashMap::Clear(table); |
| 157 holder->set_table(*table); |
| 158 return isolate->heap()->undefined_value(); |
| 159 } |
| 160 |
| 161 |
| 162 RUNTIME_FUNCTION(Runtime_MapSet) { |
| 163 HandleScope scope(isolate); |
| 164 DCHECK(args.length() == 3); |
| 165 CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
| 166 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 167 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
| 168 Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table())); |
| 169 Handle<OrderedHashMap> new_table = OrderedHashMap::Put(table, key, value); |
| 170 holder->set_table(*new_table); |
| 171 return *holder; |
| 172 } |
| 173 |
| 174 |
| 175 RUNTIME_FUNCTION(Runtime_MapGetSize) { |
| 176 HandleScope scope(isolate); |
| 177 DCHECK(args.length() == 1); |
| 178 CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
| 179 Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table())); |
| 180 return Smi::FromInt(table->NumberOfElements()); |
| 181 } |
| 182 |
| 183 |
| 184 RUNTIME_FUNCTION(Runtime_MapIteratorInitialize) { |
| 185 HandleScope scope(isolate); |
| 186 DCHECK(args.length() == 3); |
| 187 CONVERT_ARG_HANDLE_CHECKED(JSMapIterator, holder, 0); |
| 188 CONVERT_ARG_HANDLE_CHECKED(JSMap, map, 1); |
| 189 CONVERT_SMI_ARG_CHECKED(kind, 2) |
| 190 RUNTIME_ASSERT(kind == JSMapIterator::kKindKeys || |
| 191 kind == JSMapIterator::kKindValues || |
| 192 kind == JSMapIterator::kKindEntries); |
| 193 Handle<OrderedHashMap> table(OrderedHashMap::cast(map->table())); |
| 194 holder->set_table(*table); |
| 195 holder->set_index(Smi::FromInt(0)); |
| 196 holder->set_kind(Smi::FromInt(kind)); |
| 197 return isolate->heap()->undefined_value(); |
| 198 } |
| 199 |
| 200 |
| 201 RUNTIME_FUNCTION(Runtime_GetWeakMapEntries) { |
| 202 HandleScope scope(isolate); |
| 203 DCHECK(args.length() == 1); |
| 204 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); |
| 205 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); |
| 206 Handle<FixedArray> entries = |
| 207 isolate->factory()->NewFixedArray(table->NumberOfElements() * 2); |
| 208 { |
| 209 DisallowHeapAllocation no_gc; |
| 210 int number_of_non_hole_elements = 0; |
| 211 for (int i = 0; i < table->Capacity(); i++) { |
| 212 Handle<Object> key(table->KeyAt(i), isolate); |
| 213 if (table->IsKey(*key)) { |
| 214 entries->set(number_of_non_hole_elements++, *key); |
| 215 Object* value = table->Lookup(key); |
| 216 entries->set(number_of_non_hole_elements++, value); |
| 217 } |
| 218 } |
| 219 DCHECK_EQ(table->NumberOfElements() * 2, number_of_non_hole_elements); |
| 220 } |
| 221 return *isolate->factory()->NewJSArrayWithElements(entries); |
| 222 } |
| 223 |
| 224 |
| 225 RUNTIME_FUNCTION(Runtime_MapIteratorNext) { |
| 226 SealHandleScope shs(isolate); |
| 227 DCHECK(args.length() == 2); |
| 228 CONVERT_ARG_CHECKED(JSMapIterator, holder, 0); |
| 229 CONVERT_ARG_CHECKED(JSArray, value_array, 1); |
| 230 return holder->Next(value_array); |
| 231 } |
| 232 |
| 233 |
| 234 static Handle<JSWeakCollection> WeakCollectionInitialize( |
| 235 Isolate* isolate, Handle<JSWeakCollection> weak_collection) { |
| 236 DCHECK(weak_collection->map()->inobject_properties() == 0); |
| 237 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 0); |
| 238 weak_collection->set_table(*table); |
| 239 return weak_collection; |
| 240 } |
| 241 |
| 242 |
| 243 RUNTIME_FUNCTION(Runtime_WeakCollectionInitialize) { |
| 244 HandleScope scope(isolate); |
| 245 DCHECK(args.length() == 1); |
| 246 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
| 247 return *WeakCollectionInitialize(isolate, weak_collection); |
| 248 } |
| 249 |
| 250 |
| 251 RUNTIME_FUNCTION(Runtime_WeakCollectionGet) { |
| 252 HandleScope scope(isolate); |
| 253 DCHECK(args.length() == 2); |
| 254 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
| 255 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 256 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); |
| 257 Handle<ObjectHashTable> table( |
| 258 ObjectHashTable::cast(weak_collection->table())); |
| 259 RUNTIME_ASSERT(table->IsKey(*key)); |
| 260 Handle<Object> lookup(table->Lookup(key), isolate); |
| 261 return lookup->IsTheHole() ? isolate->heap()->undefined_value() : *lookup; |
| 262 } |
| 263 |
| 264 |
| 265 RUNTIME_FUNCTION(Runtime_WeakCollectionHas) { |
| 266 HandleScope scope(isolate); |
| 267 DCHECK(args.length() == 2); |
| 268 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
| 269 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 270 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); |
| 271 Handle<ObjectHashTable> table( |
| 272 ObjectHashTable::cast(weak_collection->table())); |
| 273 RUNTIME_ASSERT(table->IsKey(*key)); |
| 274 Handle<Object> lookup(table->Lookup(key), isolate); |
| 275 return isolate->heap()->ToBoolean(!lookup->IsTheHole()); |
| 276 } |
| 277 |
| 278 |
| 279 RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) { |
| 280 HandleScope scope(isolate); |
| 281 DCHECK(args.length() == 2); |
| 282 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
| 283 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 284 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); |
| 285 Handle<ObjectHashTable> table( |
| 286 ObjectHashTable::cast(weak_collection->table())); |
| 287 RUNTIME_ASSERT(table->IsKey(*key)); |
| 288 bool was_present = false; |
| 289 Handle<ObjectHashTable> new_table = |
| 290 ObjectHashTable::Remove(table, key, &was_present); |
| 291 weak_collection->set_table(*new_table); |
| 292 return isolate->heap()->ToBoolean(was_present); |
| 293 } |
| 294 |
| 295 |
| 296 RUNTIME_FUNCTION(Runtime_WeakCollectionSet) { |
| 297 HandleScope scope(isolate); |
| 298 DCHECK(args.length() == 3); |
| 299 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
| 300 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 301 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); |
| 302 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
| 303 Handle<ObjectHashTable> table( |
| 304 ObjectHashTable::cast(weak_collection->table())); |
| 305 RUNTIME_ASSERT(table->IsKey(*key)); |
| 306 Handle<ObjectHashTable> new_table = ObjectHashTable::Put(table, key, value); |
| 307 weak_collection->set_table(*new_table); |
| 308 return *weak_collection; |
| 309 } |
| 310 |
| 311 |
| 312 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) { |
| 313 HandleScope scope(isolate); |
| 314 DCHECK(args.length() == 1); |
| 315 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); |
| 316 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); |
| 317 Handle<FixedArray> values = |
| 318 isolate->factory()->NewFixedArray(table->NumberOfElements()); |
| 319 { |
| 320 DisallowHeapAllocation no_gc; |
| 321 int number_of_non_hole_elements = 0; |
| 322 for (int i = 0; i < table->Capacity(); i++) { |
| 323 Handle<Object> key(table->KeyAt(i), isolate); |
| 324 if (table->IsKey(*key)) { |
| 325 values->set(number_of_non_hole_elements++, *key); |
| 326 } |
| 327 } |
| 328 DCHECK_EQ(table->NumberOfElements(), number_of_non_hole_elements); |
| 329 } |
| 330 return *isolate->factory()->NewJSArrayWithElements(values); |
| 331 } |
| 332 |
| 333 |
| 334 RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) { |
| 335 HandleScope scope(isolate); |
| 336 DCHECK(args.length() == 0); |
| 337 // TODO(adamk): Currently this runtime function is only called three times per |
| 338 // isolate. If it's called more often, the map should be moved into the |
| 339 // strong root list. |
| 340 Handle<Map> map = |
| 341 isolate->factory()->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); |
| 342 Handle<JSWeakMap> weakmap = |
| 343 Handle<JSWeakMap>::cast(isolate->factory()->NewJSObjectFromMap(map)); |
| 344 return *WeakCollectionInitialize(isolate, weakmap); |
| 345 } |
| 346 } |
| 347 } // namespace v8::internal |
OLD | NEW |