OLD | NEW |
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 Loading... |
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 void Runtime::WeakCollectionInitialize( | 299 static Handle<JSWeakCollection> 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; |
304 } | 305 } |
305 | 306 |
306 | 307 |
307 RUNTIME_FUNCTION(Runtime_WeakCollectionInitialize) { | 308 RUNTIME_FUNCTION(Runtime_WeakCollectionInitialize) { |
308 HandleScope scope(isolate); | 309 HandleScope scope(isolate); |
309 DCHECK(args.length() == 1); | 310 DCHECK(args.length() == 1); |
310 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); | 311 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
311 Runtime::WeakCollectionInitialize(isolate, weak_collection); | 312 return *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 Loading... |
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, | 344 RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) { |
345 Handle<Object> key) { | 345 HandleScope scope(isolate); |
346 DCHECK(key->IsJSReceiver() || key->IsSymbol()); | 346 DCHECK(args.length() == 2); |
| 347 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
| 348 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 349 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); |
347 Handle<ObjectHashTable> table( | 350 Handle<ObjectHashTable> table( |
348 ObjectHashTable::cast(weak_collection->table())); | 351 ObjectHashTable::cast(weak_collection->table())); |
349 DCHECK(table->IsKey(*key)); | 352 RUNTIME_ASSERT(table->IsKey(*key)); |
350 bool was_present = false; | 353 bool was_present = false; |
351 Handle<ObjectHashTable> new_table = | 354 Handle<ObjectHashTable> new_table = |
352 ObjectHashTable::Remove(table, key, &was_present); | 355 ObjectHashTable::Remove(table, key, &was_present); |
353 weak_collection->set_table(*new_table); | 356 weak_collection->set_table(*new_table); |
354 if (*table != *new_table) { | 357 if (*table != *new_table) { |
355 // Zap the old table since we didn't record slots for its elements. | 358 // Zap the old table since we didn't record slots for its elements. |
356 table->FillWithHoles(0, table->length()); | 359 table->FillWithHoles(0, table->length()); |
357 } | 360 } |
358 return was_present; | |
359 } | |
360 | |
361 | |
362 RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) { | |
363 HandleScope scope(isolate); | |
364 DCHECK(args.length() == 2); | |
365 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); | |
366 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); | |
367 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); | |
368 Handle<ObjectHashTable> table( | |
369 ObjectHashTable::cast(weak_collection->table())); | |
370 RUNTIME_ASSERT(table->IsKey(*key)); | |
371 bool was_present = Runtime::WeakCollectionDelete(weak_collection, key); | |
372 return isolate->heap()->ToBoolean(was_present); | 361 return isolate->heap()->ToBoolean(was_present); |
373 } | 362 } |
374 | 363 |
375 | 364 |
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); | |
383 weak_collection->set_table(*new_table); | |
384 if (*table != *new_table) { | |
385 // Zap the old table since we didn't record slots for its elements. | |
386 table->FillWithHoles(0, table->length()); | |
387 } | |
388 } | |
389 | |
390 | |
391 RUNTIME_FUNCTION(Runtime_WeakCollectionSet) { | 365 RUNTIME_FUNCTION(Runtime_WeakCollectionSet) { |
392 HandleScope scope(isolate); | 366 HandleScope scope(isolate); |
393 DCHECK(args.length() == 3); | 367 DCHECK(args.length() == 3); |
394 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); | 368 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
395 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); | 369 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
396 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); | 370 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); |
397 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); | 371 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
398 Handle<ObjectHashTable> table( | 372 Handle<ObjectHashTable> table( |
399 ObjectHashTable::cast(weak_collection->table())); | 373 ObjectHashTable::cast(weak_collection->table())); |
400 RUNTIME_ASSERT(table->IsKey(*key)); | 374 RUNTIME_ASSERT(table->IsKey(*key)); |
401 Runtime::WeakCollectionSet(weak_collection, key, value); | 375 Handle<ObjectHashTable> new_table = ObjectHashTable::Put(table, 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 } |
402 return *weak_collection; | 381 return *weak_collection; |
403 } | 382 } |
404 | 383 |
405 | 384 |
406 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) { | 385 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) { |
407 HandleScope scope(isolate); | 386 HandleScope scope(isolate); |
408 DCHECK(args.length() == 2); | 387 DCHECK(args.length() == 2); |
409 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); | 388 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); |
410 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]); | 389 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]); |
411 RUNTIME_ASSERT(max_values >= 0); | 390 RUNTIME_ASSERT(max_values >= 0); |
(...skipping 16 matching lines...) Expand all Loading... |
428 } | 407 } |
429 DCHECK_EQ(max_values, count); | 408 DCHECK_EQ(max_values, count); |
430 } | 409 } |
431 return *isolate->factory()->NewJSArrayWithElements(values); | 410 return *isolate->factory()->NewJSArrayWithElements(values); |
432 } | 411 } |
433 | 412 |
434 | 413 |
435 RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) { | 414 RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) { |
436 HandleScope scope(isolate); | 415 HandleScope scope(isolate); |
437 DCHECK(args.length() == 0); | 416 DCHECK(args.length() == 0); |
438 Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap(); | 417 // TODO(adamk): Currently this runtime function is only called three times per |
439 Runtime::WeakCollectionInitialize(isolate, weakmap); | 418 // isolate. If it's called more often, the map should be moved into the |
440 return *weakmap; | 419 // strong root list. |
| 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); |
441 } | 425 } |
442 } | 426 } |
443 } // namespace v8::internal | 427 } // namespace v8::internal |
OLD | NEW |