runtime-collections.cc 14.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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.

#include "src/v8.h"

#include "src/arguments.h"
#include "src/runtime/runtime-utils.h"


namespace v8 {
namespace internal {

14 15

RUNTIME_FUNCTION(Runtime_StringGetRawHashField) {
16 17
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
18 19
  CONVERT_ARG_HANDLE_CHECKED(String, string, 0);
  return *isolate->factory()->NewNumberFromUint(string->hash_field());
20 21 22
}


23 24 25 26 27 28 29 30 31 32 33 34 35
RUNTIME_FUNCTION(Runtime_TheHole) {
  SealHandleScope shs(isolate);
  DCHECK(args.length() == 0);
  return isolate->heap()->the_hole_value();
}


RUNTIME_FUNCTION(Runtime_JSCollectionGetTable) {
  SealHandleScope shs(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_CHECKED(JSObject, object, 0);
  RUNTIME_ASSERT(object->IsJSSet() || object->IsJSMap());
  return static_cast<JSCollection*>(object)->table();
36 37 38
}


39
RUNTIME_FUNCTION(Runtime_GenericHash) {
40
  HandleScope scope(isolate);
41 42 43 44 45 46 47
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
  Handle<Smi> hash = Object::GetOrCreateHash(isolate, object);
  return *hash;
}


48 49 50 51 52 53
void Runtime::JSSetInitialize(Isolate* isolate, Handle<JSSet> set) {
  Handle<OrderedHashSet> table = isolate->factory()->NewOrderedHashSet();
  set->set_table(*table);
}


54 55 56
RUNTIME_FUNCTION(Runtime_SetInitialize) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
57
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
58
  Runtime::JSSetInitialize(isolate, holder);
59
  return *holder;
60 61 62
}


63
RUNTIME_FUNCTION(Runtime_SetGrow) {
64
  HandleScope scope(isolate);
65
  DCHECK(args.length() == 1);
66 67
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
  Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table()));
68
  table = OrderedHashSet::EnsureGrowable(table);
69
  holder->set_table(*table);
70
  return isolate->heap()->undefined_value();
71 72 73
}


74
RUNTIME_FUNCTION(Runtime_SetShrink) {
75 76 77 78
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
  Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table()));
79
  table = OrderedHashSet::Shrink(table);
80 81 82 83 84
  holder->set_table(*table);
  return isolate->heap()->undefined_value();
}


85 86 87 88 89 90 91
void Runtime::JSSetClear(Isolate* isolate, Handle<JSSet> set) {
  Handle<OrderedHashSet> table(OrderedHashSet::cast(set->table()));
  table = OrderedHashSet::Clear(table);
  set->set_table(*table);
}


92
RUNTIME_FUNCTION(Runtime_SetClear) {
93 94 95
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
96
  Runtime::JSSetClear(isolate, holder);
97
  return isolate->heap()->undefined_value();
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}


RUNTIME_FUNCTION(Runtime_SetIteratorInitialize) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 3);
  CONVERT_ARG_HANDLE_CHECKED(JSSetIterator, holder, 0);
  CONVERT_ARG_HANDLE_CHECKED(JSSet, set, 1);
  CONVERT_SMI_ARG_CHECKED(kind, 2)
  RUNTIME_ASSERT(kind == JSSetIterator::kKindValues ||
                 kind == JSSetIterator::kKindEntries);
  Handle<OrderedHashSet> table(OrderedHashSet::cast(set->table()));
  holder->set_table(*table);
  holder->set_index(Smi::FromInt(0));
  holder->set_kind(Smi::FromInt(kind));
  return isolate->heap()->undefined_value();
}


117 118 119 120 121 122 123 124 125 126 127 128 129 130
RUNTIME_FUNCTION(Runtime_SetIteratorClone) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSSetIterator, holder, 0);

  Handle<JSSetIterator> result = isolate->factory()->NewJSSetIterator();
  result->set_table(holder->table());
  result->set_index(Smi::FromInt(Smi::cast(holder->index())->value()));
  result->set_kind(Smi::FromInt(Smi::cast(holder->kind())->value()));

  return *result;
}


131 132 133 134 135 136 137 138 139
RUNTIME_FUNCTION(Runtime_SetIteratorNext) {
  SealHandleScope shs(isolate);
  DCHECK(args.length() == 2);
  CONVERT_ARG_CHECKED(JSSetIterator, holder, 0);
  CONVERT_ARG_CHECKED(JSArray, value_array, 1);
  return holder->Next(value_array);
}


140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
// The array returned contains the following information:
// 0: HasMore flag
// 1: Iteration index
// 2: Iteration kind
RUNTIME_FUNCTION(Runtime_SetIteratorDetails) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSSetIterator, holder, 0);
  Handle<FixedArray> details = isolate->factory()->NewFixedArray(4);
  details->set(0, isolate->heap()->ToBoolean(holder->HasMore()));
  details->set(1, holder->index());
  details->set(2, holder->kind());
  return *isolate->factory()->NewJSArrayWithElements(details);
}


156 157 158 159 160 161
void Runtime::JSMapInitialize(Isolate* isolate, Handle<JSMap> map) {
  Handle<OrderedHashMap> table = isolate->factory()->NewOrderedHashMap();
  map->set_table(*table);
}


162 163 164 165
RUNTIME_FUNCTION(Runtime_MapInitialize) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
166
  Runtime::JSMapInitialize(isolate, holder);
167 168 169 170
  return *holder;
}


171
RUNTIME_FUNCTION(Runtime_MapShrink) {
172
  HandleScope scope(isolate);
173
  DCHECK(args.length() == 1);
174 175
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
  Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table()));
176 177 178
  table = OrderedHashMap::Shrink(table);
  holder->set_table(*table);
  return isolate->heap()->undefined_value();
179 180 181
}


182 183 184 185 186 187 188
void Runtime::JSMapClear(Isolate* isolate, Handle<JSMap> map) {
  Handle<OrderedHashMap> table(OrderedHashMap::cast(map->table()));
  table = OrderedHashMap::Clear(table);
  map->set_table(*table);
}


189 190 191 192
RUNTIME_FUNCTION(Runtime_MapClear) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
193
  Runtime::JSMapClear(isolate, holder);
194 195 196 197
  return isolate->heap()->undefined_value();
}


198
RUNTIME_FUNCTION(Runtime_MapGrow) {
199 200 201 202
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
  Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table()));
203 204 205
  table = OrderedHashMap::EnsureGrowable(table);
  holder->set_table(*table);
  return isolate->heap()->undefined_value();
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
}


RUNTIME_FUNCTION(Runtime_MapIteratorInitialize) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 3);
  CONVERT_ARG_HANDLE_CHECKED(JSMapIterator, holder, 0);
  CONVERT_ARG_HANDLE_CHECKED(JSMap, map, 1);
  CONVERT_SMI_ARG_CHECKED(kind, 2)
  RUNTIME_ASSERT(kind == JSMapIterator::kKindKeys ||
                 kind == JSMapIterator::kKindValues ||
                 kind == JSMapIterator::kKindEntries);
  Handle<OrderedHashMap> table(OrderedHashMap::cast(map->table()));
  holder->set_table(*table);
  holder->set_index(Smi::FromInt(0));
  holder->set_kind(Smi::FromInt(kind));
  return isolate->heap()->undefined_value();
}


226 227 228 229 230 231 232 233 234 235 236 237 238 239
RUNTIME_FUNCTION(Runtime_MapIteratorClone) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSMapIterator, holder, 0);

  Handle<JSMapIterator> result = isolate->factory()->NewJSMapIterator();
  result->set_table(holder->table());
  result->set_index(Smi::FromInt(Smi::cast(holder->index())->value()));
  result->set_kind(Smi::FromInt(Smi::cast(holder->kind())->value()));

  return *result;
}


240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
// The array returned contains the following information:
// 0: HasMore flag
// 1: Iteration index
// 2: Iteration kind
RUNTIME_FUNCTION(Runtime_MapIteratorDetails) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSMapIterator, holder, 0);
  Handle<FixedArray> details = isolate->factory()->NewFixedArray(4);
  details->set(0, isolate->heap()->ToBoolean(holder->HasMore()));
  details->set(1, holder->index());
  details->set(2, holder->kind());
  return *isolate->factory()->NewJSArrayWithElements(details);
}


256 257
RUNTIME_FUNCTION(Runtime_GetWeakMapEntries) {
  HandleScope scope(isolate);
258
  DCHECK(args.length() == 2);
259
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0);
260 261 262
  CONVERT_NUMBER_CHECKED(int, max_entries, Int32, args[1]);
  RUNTIME_ASSERT(max_entries >= 0);

263
  Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table()));
264 265 266
  if (max_entries == 0 || max_entries > table->NumberOfElements()) {
    max_entries = table->NumberOfElements();
  }
267
  Handle<FixedArray> entries =
268
      isolate->factory()->NewFixedArray(max_entries * 2);
269 270 271 272 273
  // Allocation can cause GC can delete weak elements. Reload.
  if (max_entries > table->NumberOfElements()) {
    max_entries = table->NumberOfElements();
  }

274 275
  {
    DisallowHeapAllocation no_gc;
276 277
    int count = 0;
    for (int i = 0; count / 2 < max_entries && i < table->Capacity(); i++) {
278 279
      Handle<Object> key(table->KeyAt(i), isolate);
      if (table->IsKey(*key)) {
280
        entries->set(count++, *key);
281
        Object* value = table->Lookup(key);
282
        entries->set(count++, value);
283 284
      }
    }
285
    DCHECK_EQ(max_entries * 2, count);
286 287 288 289 290 291 292 293 294 295 296 297 298 299
  }
  return *isolate->factory()->NewJSArrayWithElements(entries);
}


RUNTIME_FUNCTION(Runtime_MapIteratorNext) {
  SealHandleScope shs(isolate);
  DCHECK(args.length() == 2);
  CONVERT_ARG_CHECKED(JSMapIterator, holder, 0);
  CONVERT_ARG_CHECKED(JSArray, value_array, 1);
  return holder->Next(value_array);
}


yurys's avatar
yurys committed
300
void Runtime::WeakCollectionInitialize(
301 302 303 304 305 306 307 308 309 310 311
    Isolate* isolate, Handle<JSWeakCollection> weak_collection) {
  DCHECK(weak_collection->map()->inobject_properties() == 0);
  Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 0);
  weak_collection->set_table(*table);
}


RUNTIME_FUNCTION(Runtime_WeakCollectionInitialize) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
yurys's avatar
yurys committed
312 313
  Runtime::WeakCollectionInitialize(isolate, weak_collection);
  return *weak_collection;
314 315 316 317 318
}


RUNTIME_FUNCTION(Runtime_WeakCollectionGet) {
  HandleScope scope(isolate);
319
  DCHECK(args.length() == 3);
320 321
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
322
  CONVERT_SMI_ARG_CHECKED(hash, 2)
323 324 325 326
  RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
  Handle<ObjectHashTable> table(
      ObjectHashTable::cast(weak_collection->table()));
  RUNTIME_ASSERT(table->IsKey(*key));
327
  Handle<Object> lookup(table->Lookup(key, hash), isolate);
328 329 330 331 332 333
  return lookup->IsTheHole() ? isolate->heap()->undefined_value() : *lookup;
}


RUNTIME_FUNCTION(Runtime_WeakCollectionHas) {
  HandleScope scope(isolate);
334
  DCHECK(args.length() == 3);
335 336
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
337
  CONVERT_SMI_ARG_CHECKED(hash, 2)
338 339 340 341
  RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
  Handle<ObjectHashTable> table(
      ObjectHashTable::cast(weak_collection->table()));
  RUNTIME_ASSERT(table->IsKey(*key));
342
  Handle<Object> lookup(table->Lookup(key, hash), isolate);
343 344 345 346
  return isolate->heap()->ToBoolean(!lookup->IsTheHole());
}


yurys's avatar
yurys committed
347 348
bool Runtime::WeakCollectionDelete(Handle<JSWeakCollection> weak_collection,
                                   Handle<Object> key) {
349 350 351 352 353 354 355 356
  int32_t hash =
      Object::GetOrCreateHash(weak_collection->GetIsolate(), key)->value();
  return WeakCollectionDelete(weak_collection, key, hash);
}


bool Runtime::WeakCollectionDelete(Handle<JSWeakCollection> weak_collection,
                                   Handle<Object> key, int32_t hash) {
yurys's avatar
yurys committed
357 358 359 360 361 362
  DCHECK(key->IsJSReceiver() || key->IsSymbol());
  Handle<ObjectHashTable> table(
      ObjectHashTable::cast(weak_collection->table()));
  DCHECK(table->IsKey(*key));
  bool was_present = false;
  Handle<ObjectHashTable> new_table =
363
      ObjectHashTable::Remove(table, key, &was_present, hash);
yurys's avatar
yurys committed
364 365 366 367 368 369 370 371 372
  weak_collection->set_table(*new_table);
  if (*table != *new_table) {
    // Zap the old table since we didn't record slots for its elements.
    table->FillWithHoles(0, table->length());
  }
  return was_present;
}


373 374
RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) {
  HandleScope scope(isolate);
375
  DCHECK(args.length() == 3);
376 377
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
378
  CONVERT_SMI_ARG_CHECKED(hash, 2)
379 380 381 382
  RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
  Handle<ObjectHashTable> table(
      ObjectHashTable::cast(weak_collection->table()));
  RUNTIME_ASSERT(table->IsKey(*key));
383
  bool was_present = Runtime::WeakCollectionDelete(weak_collection, key, hash);
yurys's avatar
yurys committed
384 385 386 387 388
  return isolate->heap()->ToBoolean(was_present);
}


void Runtime::WeakCollectionSet(Handle<JSWeakCollection> weak_collection,
389 390
                                Handle<Object> key, Handle<Object> value,
                                int32_t hash) {
yurys's avatar
yurys committed
391 392 393 394
  DCHECK(key->IsJSReceiver() || key->IsSymbol());
  Handle<ObjectHashTable> table(
      ObjectHashTable::cast(weak_collection->table()));
  DCHECK(table->IsKey(*key));
395 396
  Handle<ObjectHashTable> new_table =
      ObjectHashTable::Put(table, key, value, hash);
397
  weak_collection->set_table(*new_table);
398 399 400 401
  if (*table != *new_table) {
    // Zap the old table since we didn't record slots for its elements.
    table->FillWithHoles(0, table->length());
  }
402 403 404 405 406
}


RUNTIME_FUNCTION(Runtime_WeakCollectionSet) {
  HandleScope scope(isolate);
407
  DCHECK(args.length() == 4);
408 409 410 411
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
  RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
  CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
412
  CONVERT_SMI_ARG_CHECKED(hash, 3)
413 414 415
  Handle<ObjectHashTable> table(
      ObjectHashTable::cast(weak_collection->table()));
  RUNTIME_ASSERT(table->IsKey(*key));
416
  Runtime::WeakCollectionSet(weak_collection, key, value, hash);
417 418 419 420 421 422
  return *weak_collection;
}


RUNTIME_FUNCTION(Runtime_GetWeakSetValues) {
  HandleScope scope(isolate);
423
  DCHECK(args.length() == 2);
424
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0);
425 426 427
  CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]);
  RUNTIME_ASSERT(max_values >= 0);

428
  Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table()));
429 430 431 432
  if (max_values == 0 || max_values > table->NumberOfElements()) {
    max_values = table->NumberOfElements();
  }
  Handle<FixedArray> values = isolate->factory()->NewFixedArray(max_values);
433 434 435 436
  // Recompute max_values because GC could have removed elements from the table.
  if (max_values > table->NumberOfElements()) {
    max_values = table->NumberOfElements();
  }
437 438
  {
    DisallowHeapAllocation no_gc;
439 440
    int count = 0;
    for (int i = 0; count < max_values && i < table->Capacity(); i++) {
441
      Handle<Object> key(table->KeyAt(i), isolate);
442
      if (table->IsKey(*key)) values->set(count++, *key);
443
    }
444
    DCHECK_EQ(max_values, count);
445 446 447 448 449 450 451 452
  }
  return *isolate->factory()->NewJSArrayWithElements(values);
}


RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 0);
yurys's avatar
yurys committed
453 454 455
  Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
  Runtime::WeakCollectionInitialize(isolate, weakmap);
  return *weakmap;
456
}
457 458
}  // namespace internal
}  // namespace v8