runtime-collections.cc 11.9 KB
Newer Older
1 2 3 4 5 6
// 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/runtime/runtime-utils.h"

7 8 9
#include "src/arguments.h"
#include "src/conversions-inl.h"
#include "src/factory.h"
10 11 12 13

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 48 49 50
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
  Handle<Smi> hash = Object::GetOrCreateHash(isolate, object);
  return *hash;
}


RUNTIME_FUNCTION(Runtime_SetInitialize) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
51
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
52
  JSSet::Initialize(holder, isolate);
53
  return *holder;
54 55 56
}


57
RUNTIME_FUNCTION(Runtime_SetGrow) {
58
  HandleScope scope(isolate);
59
  DCHECK(args.length() == 1);
60 61
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
  Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table()));
62
  table = OrderedHashSet::EnsureGrowable(table);
63
  holder->set_table(*table);
64
  return isolate->heap()->undefined_value();
65 66 67
}


68
RUNTIME_FUNCTION(Runtime_SetShrink) {
69 70 71 72
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
  Handle<OrderedHashSet> table(OrderedHashSet::cast(holder->table()));
73
  table = OrderedHashSet::Shrink(table);
74 75 76 77 78
  holder->set_table(*table);
  return isolate->heap()->undefined_value();
}


79
RUNTIME_FUNCTION(Runtime_SetClear) {
80 81 82
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
83
  JSSet::Clear(holder);
84
  return isolate->heap()->undefined_value();
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
}


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();
}


104 105 106 107 108 109 110 111 112 113 114 115 116 117
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;
}


118 119 120 121 122 123 124 125 126
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);
}


127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
// 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);
}


143 144 145 146
RUNTIME_FUNCTION(Runtime_MapInitialize) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
147
  JSMap::Initialize(holder, isolate);
148 149 150 151
  return *holder;
}


152
RUNTIME_FUNCTION(Runtime_MapShrink) {
153
  HandleScope scope(isolate);
154
  DCHECK(args.length() == 1);
155 156
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
  Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table()));
157 158 159
  table = OrderedHashMap::Shrink(table);
  holder->set_table(*table);
  return isolate->heap()->undefined_value();
160 161 162 163 164 165 166
}


RUNTIME_FUNCTION(Runtime_MapClear) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
167
  JSMap::Clear(holder);
168 169 170 171
  return isolate->heap()->undefined_value();
}


172
RUNTIME_FUNCTION(Runtime_MapGrow) {
173 174 175 176
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0);
  Handle<OrderedHashMap> table(OrderedHashMap::cast(holder->table()));
177 178 179
  table = OrderedHashMap::EnsureGrowable(table);
  holder->set_table(*table);
  return isolate->heap()->undefined_value();
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
}


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();
}


200 201 202 203 204 205 206 207 208 209 210 211 212 213
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;
}


214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
// 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);
}


230 231
RUNTIME_FUNCTION(Runtime_GetWeakMapEntries) {
  HandleScope scope(isolate);
232
  DCHECK(args.length() == 2);
233
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0);
234 235 236
  CONVERT_NUMBER_CHECKED(int, max_entries, Int32, args[1]);
  RUNTIME_ASSERT(max_entries >= 0);

237
  Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table()));
238 239 240
  if (max_entries == 0 || max_entries > table->NumberOfElements()) {
    max_entries = table->NumberOfElements();
  }
241
  Handle<FixedArray> entries =
242
      isolate->factory()->NewFixedArray(max_entries * 2);
243 244 245 246 247
  // Allocation can cause GC can delete weak elements. Reload.
  if (max_entries > table->NumberOfElements()) {
    max_entries = table->NumberOfElements();
  }

248 249
  {
    DisallowHeapAllocation no_gc;
250 251
    int count = 0;
    for (int i = 0; count / 2 < max_entries && i < table->Capacity(); i++) {
252 253
      Handle<Object> key(table->KeyAt(i), isolate);
      if (table->IsKey(*key)) {
254
        entries->set(count++, *key);
255
        Object* value = table->Lookup(key);
256
        entries->set(count++, value);
257 258
      }
    }
259
    DCHECK_EQ(max_entries * 2, count);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  }
  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);
}


RUNTIME_FUNCTION(Runtime_WeakCollectionInitialize) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
278
  JSWeakCollection::Initialize(weak_collection, isolate);
yurys's avatar
yurys committed
279
  return *weak_collection;
280 281 282 283 284
}


RUNTIME_FUNCTION(Runtime_WeakCollectionGet) {
  HandleScope scope(isolate);
285
  DCHECK(args.length() == 3);
286 287
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
288
  CONVERT_SMI_ARG_CHECKED(hash, 2)
289 290 291 292
  RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
  Handle<ObjectHashTable> table(
      ObjectHashTable::cast(weak_collection->table()));
  RUNTIME_ASSERT(table->IsKey(*key));
293
  Handle<Object> lookup(table->Lookup(key, hash), isolate);
294 295 296 297 298 299
  return lookup->IsTheHole() ? isolate->heap()->undefined_value() : *lookup;
}


RUNTIME_FUNCTION(Runtime_WeakCollectionHas) {
  HandleScope scope(isolate);
300
  DCHECK(args.length() == 3);
301 302
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
303
  CONVERT_SMI_ARG_CHECKED(hash, 2)
304 305 306 307
  RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
  Handle<ObjectHashTable> table(
      ObjectHashTable::cast(weak_collection->table()));
  RUNTIME_ASSERT(table->IsKey(*key));
308
  Handle<Object> lookup(table->Lookup(key, hash), isolate);
309 310 311 312 313 314
  return isolate->heap()->ToBoolean(!lookup->IsTheHole());
}


RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) {
  HandleScope scope(isolate);
315
  DCHECK(args.length() == 3);
316 317
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
318
  CONVERT_SMI_ARG_CHECKED(hash, 2)
319 320 321 322
  RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
  Handle<ObjectHashTable> table(
      ObjectHashTable::cast(weak_collection->table()));
  RUNTIME_ASSERT(table->IsKey(*key));
323
  bool was_present = JSWeakCollection::Delete(weak_collection, key, hash);
yurys's avatar
yurys committed
324 325 326 327
  return isolate->heap()->ToBoolean(was_present);
}


328 329
RUNTIME_FUNCTION(Runtime_WeakCollectionSet) {
  HandleScope scope(isolate);
330
  DCHECK(args.length() == 4);
331 332 333 334
  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);
335
  CONVERT_SMI_ARG_CHECKED(hash, 3)
336 337 338
  Handle<ObjectHashTable> table(
      ObjectHashTable::cast(weak_collection->table()));
  RUNTIME_ASSERT(table->IsKey(*key));
339
  JSWeakCollection::Set(weak_collection, key, value, hash);
340 341 342 343 344 345
  return *weak_collection;
}


RUNTIME_FUNCTION(Runtime_GetWeakSetValues) {
  HandleScope scope(isolate);
346
  DCHECK(args.length() == 2);
347
  CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0);
348 349 350
  CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]);
  RUNTIME_ASSERT(max_values >= 0);

351
  Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table()));
352 353 354 355
  if (max_values == 0 || max_values > table->NumberOfElements()) {
    max_values = table->NumberOfElements();
  }
  Handle<FixedArray> values = isolate->factory()->NewFixedArray(max_values);
356 357 358 359
  // Recompute max_values because GC could have removed elements from the table.
  if (max_values > table->NumberOfElements()) {
    max_values = table->NumberOfElements();
  }
360 361
  {
    DisallowHeapAllocation no_gc;
362 363
    int count = 0;
    for (int i = 0; count < max_values && i < table->Capacity(); i++) {
364
      Handle<Object> key(table->KeyAt(i), isolate);
365
      if (table->IsKey(*key)) values->set(count++, *key);
366
    }
367
    DCHECK_EQ(max_values, count);
368 369 370 371 372 373 374 375
  }
  return *isolate->factory()->NewJSArrayWithElements(values);
}


RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 0);
yurys's avatar
yurys committed
376
  Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
377
  JSWeakCollection::Initialize(weakmap, isolate);
yurys's avatar
yurys committed
378
  return *weakmap;
379
}
380 381
}  // namespace internal
}  // namespace v8