Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: src/runtime/runtime-collections.cc

Issue 891473005: Add WeakKeyMap to v8.h (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Renamed WeakKeyMap to NativeWeakMap as suggested in review comments Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/runtime/runtime.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/arguments.h" 7 #include "src/arguments.h"
8 #include "src/runtime/runtime-utils.h" 8 #include "src/runtime/runtime-utils.h"
9 9
10 10
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 289
290 RUNTIME_FUNCTION(Runtime_MapIteratorNext) { 290 RUNTIME_FUNCTION(Runtime_MapIteratorNext) {
291 SealHandleScope shs(isolate); 291 SealHandleScope shs(isolate);
292 DCHECK(args.length() == 2); 292 DCHECK(args.length() == 2);
293 CONVERT_ARG_CHECKED(JSMapIterator, holder, 0); 293 CONVERT_ARG_CHECKED(JSMapIterator, holder, 0);
294 CONVERT_ARG_CHECKED(JSArray, value_array, 1); 294 CONVERT_ARG_CHECKED(JSArray, value_array, 1);
295 return holder->Next(value_array); 295 return holder->Next(value_array);
296 } 296 }
297 297
298 298
299 static Handle<JSWeakCollection> WeakCollectionInitialize( 299 void Runtime::WeakCollectionInitialize(
300 Isolate* isolate, Handle<JSWeakCollection> weak_collection) { 300 Isolate* isolate, Handle<JSWeakCollection> weak_collection) {
301 DCHECK(weak_collection->map()->inobject_properties() == 0); 301 DCHECK(weak_collection->map()->inobject_properties() == 0);
302 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 0); 302 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 0);
303 weak_collection->set_table(*table); 303 weak_collection->set_table(*table);
304 return weak_collection;
305 } 304 }
306 305
307 306
308 RUNTIME_FUNCTION(Runtime_WeakCollectionInitialize) { 307 RUNTIME_FUNCTION(Runtime_WeakCollectionInitialize) {
309 HandleScope scope(isolate); 308 HandleScope scope(isolate);
310 DCHECK(args.length() == 1); 309 DCHECK(args.length() == 1);
311 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); 310 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
312 return *WeakCollectionInitialize(isolate, weak_collection); 311 Runtime::WeakCollectionInitialize(isolate, weak_collection);
312 return *weak_collection;
313 } 313 }
314 314
315 315
316 RUNTIME_FUNCTION(Runtime_WeakCollectionGet) { 316 RUNTIME_FUNCTION(Runtime_WeakCollectionGet) {
317 HandleScope scope(isolate); 317 HandleScope scope(isolate);
318 DCHECK(args.length() == 2); 318 DCHECK(args.length() == 2);
319 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); 319 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
320 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); 320 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
321 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); 321 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
322 Handle<ObjectHashTable> table( 322 Handle<ObjectHashTable> table(
(...skipping 11 matching lines...) Expand all
334 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); 334 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
335 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); 335 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
336 Handle<ObjectHashTable> table( 336 Handle<ObjectHashTable> table(
337 ObjectHashTable::cast(weak_collection->table())); 337 ObjectHashTable::cast(weak_collection->table()));
338 RUNTIME_ASSERT(table->IsKey(*key)); 338 RUNTIME_ASSERT(table->IsKey(*key));
339 Handle<Object> lookup(table->Lookup(key), isolate); 339 Handle<Object> lookup(table->Lookup(key), isolate);
340 return isolate->heap()->ToBoolean(!lookup->IsTheHole()); 340 return isolate->heap()->ToBoolean(!lookup->IsTheHole());
341 } 341 }
342 342
343 343
344 bool Runtime::WeakCollectionDelete(Handle<JSWeakCollection> weak_collection,
345 Handle<Object> key) {
346 DCHECK(key->IsJSReceiver() || key->IsSymbol());
347 Handle<ObjectHashTable> table(
348 ObjectHashTable::cast(weak_collection->table()));
349 DCHECK(table->IsKey(*key));
350 bool was_present = false;
351 Handle<ObjectHashTable> new_table =
352 ObjectHashTable::Remove(table, key, &was_present);
353 weak_collection->set_table(*new_table);
354 if (*table != *new_table) {
355 // Zap the old table since we didn't record slots for its elements.
356 table->FillWithHoles(0, table->length());
357 }
358 return was_present;
359 }
360
361
344 RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) { 362 RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) {
345 HandleScope scope(isolate); 363 HandleScope scope(isolate);
346 DCHECK(args.length() == 2); 364 DCHECK(args.length() == 2);
347 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); 365 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
348 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); 366 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
349 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); 367 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
350 Handle<ObjectHashTable> table( 368 Handle<ObjectHashTable> table(
351 ObjectHashTable::cast(weak_collection->table())); 369 ObjectHashTable::cast(weak_collection->table()));
352 RUNTIME_ASSERT(table->IsKey(*key)); 370 RUNTIME_ASSERT(table->IsKey(*key));
353 bool was_present = false; 371 bool was_present = Runtime::WeakCollectionDelete(weak_collection, key);
354 Handle<ObjectHashTable> new_table = 372 return isolate->heap()->ToBoolean(was_present);
355 ObjectHashTable::Remove(table, key, &was_present); 373 }
374
375
376 void Runtime::WeakCollectionSet(Handle<JSWeakCollection> weak_collection,
377 Handle<Object> key, Handle<Object> value) {
378 DCHECK(key->IsJSReceiver() || key->IsSymbol());
379 Handle<ObjectHashTable> table(
380 ObjectHashTable::cast(weak_collection->table()));
381 DCHECK(table->IsKey(*key));
382 Handle<ObjectHashTable> new_table = ObjectHashTable::Put(table, key, value);
356 weak_collection->set_table(*new_table); 383 weak_collection->set_table(*new_table);
357 if (*table != *new_table) { 384 if (*table != *new_table) {
358 // Zap the old table since we didn't record slots for its elements. 385 // Zap the old table since we didn't record slots for its elements.
359 table->FillWithHoles(0, table->length()); 386 table->FillWithHoles(0, table->length());
360 } 387 }
361 return isolate->heap()->ToBoolean(was_present);
362 } 388 }
363 389
364 390
365 RUNTIME_FUNCTION(Runtime_WeakCollectionSet) { 391 RUNTIME_FUNCTION(Runtime_WeakCollectionSet) {
366 HandleScope scope(isolate); 392 HandleScope scope(isolate);
367 DCHECK(args.length() == 3); 393 DCHECK(args.length() == 3);
368 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); 394 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0);
369 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); 395 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
370 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); 396 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol());
371 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); 397 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
372 Handle<ObjectHashTable> table( 398 Handle<ObjectHashTable> table(
373 ObjectHashTable::cast(weak_collection->table())); 399 ObjectHashTable::cast(weak_collection->table()));
374 RUNTIME_ASSERT(table->IsKey(*key)); 400 RUNTIME_ASSERT(table->IsKey(*key));
375 Handle<ObjectHashTable> new_table = ObjectHashTable::Put(table, key, value); 401 Runtime::WeakCollectionSet(weak_collection, key, value);
376 weak_collection->set_table(*new_table);
377 if (*table != *new_table) {
378 // Zap the old table since we didn't record slots for its elements.
379 table->FillWithHoles(0, table->length());
380 }
381 return *weak_collection; 402 return *weak_collection;
382 } 403 }
383 404
384 405
385 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) { 406 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) {
386 HandleScope scope(isolate); 407 HandleScope scope(isolate);
387 DCHECK(args.length() == 2); 408 DCHECK(args.length() == 2);
388 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); 409 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0);
389 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]); 410 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]);
390 RUNTIME_ASSERT(max_values >= 0); 411 RUNTIME_ASSERT(max_values >= 0);
(...skipping 16 matching lines...) Expand all
407 } 428 }
408 DCHECK_EQ(max_values, count); 429 DCHECK_EQ(max_values, count);
409 } 430 }
410 return *isolate->factory()->NewJSArrayWithElements(values); 431 return *isolate->factory()->NewJSArrayWithElements(values);
411 } 432 }
412 433
413 434
414 RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) { 435 RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) {
415 HandleScope scope(isolate); 436 HandleScope scope(isolate);
416 DCHECK(args.length() == 0); 437 DCHECK(args.length() == 0);
417 // TODO(adamk): Currently this runtime function is only called three times per 438 Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
418 // isolate. If it's called more often, the map should be moved into the 439 Runtime::WeakCollectionInitialize(isolate, weakmap);
419 // strong root list. 440 return *weakmap;
420 Handle<Map> map =
421 isolate->factory()->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize);
422 Handle<JSWeakMap> weakmap =
423 Handle<JSWeakMap>::cast(isolate->factory()->NewJSObjectFromMap(map));
424 return *WeakCollectionInitialize(isolate, weakmap);
425 } 441 }
426 } 442 }
427 } // namespace v8::internal 443 } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime/runtime.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698