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/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/conversions-inl.h" | 8 #include "src/conversions-inl.h" |
9 #include "src/factory.h" | 9 #include "src/factory.h" |
10 | 10 |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 // Allocation can cause GC can delete weak elements. Reload. | 243 // Allocation can cause GC can delete weak elements. Reload. |
244 if (max_entries > table->NumberOfElements()) { | 244 if (max_entries > table->NumberOfElements()) { |
245 max_entries = table->NumberOfElements(); | 245 max_entries = table->NumberOfElements(); |
246 } | 246 } |
247 | 247 |
248 { | 248 { |
249 DisallowHeapAllocation no_gc; | 249 DisallowHeapAllocation no_gc; |
250 int count = 0; | 250 int count = 0; |
251 for (int i = 0; count / 2 < max_entries && i < table->Capacity(); i++) { | 251 for (int i = 0; count / 2 < max_entries && i < table->Capacity(); i++) { |
252 Handle<Object> key(table->KeyAt(i), isolate); | 252 Handle<Object> key(table->KeyAt(i), isolate); |
253 if (table->IsKey(*key)) { | 253 if (table->IsKey(isolate, *key)) { |
254 entries->set(count++, *key); | 254 entries->set(count++, *key); |
255 Object* value = table->Lookup(key); | 255 Object* value = table->Lookup(key); |
256 entries->set(count++, value); | 256 entries->set(count++, value); |
257 } | 257 } |
258 } | 258 } |
259 DCHECK_EQ(max_entries * 2, count); | 259 DCHECK_EQ(max_entries * 2, count); |
260 } | 260 } |
261 return *isolate->factory()->NewJSArrayWithElements(entries); | 261 return *isolate->factory()->NewJSArrayWithElements(entries); |
262 } | 262 } |
263 | 263 |
(...skipping 18 matching lines...) Expand all Loading... |
282 | 282 |
283 RUNTIME_FUNCTION(Runtime_WeakCollectionGet) { | 283 RUNTIME_FUNCTION(Runtime_WeakCollectionGet) { |
284 HandleScope scope(isolate); | 284 HandleScope scope(isolate); |
285 DCHECK(args.length() == 3); | 285 DCHECK(args.length() == 3); |
286 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); | 286 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
287 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); | 287 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
288 CONVERT_SMI_ARG_CHECKED(hash, 2) | 288 CONVERT_SMI_ARG_CHECKED(hash, 2) |
289 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); | 289 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); |
290 Handle<ObjectHashTable> table( | 290 Handle<ObjectHashTable> table( |
291 ObjectHashTable::cast(weak_collection->table())); | 291 ObjectHashTable::cast(weak_collection->table())); |
292 RUNTIME_ASSERT(table->IsKey(*key)); | 292 RUNTIME_ASSERT(table->IsKey(isolate, *key)); |
293 Handle<Object> lookup(table->Lookup(key, hash), isolate); | 293 Handle<Object> lookup(table->Lookup(key, hash), isolate); |
294 return lookup->IsTheHole(isolate) ? isolate->heap()->undefined_value() | 294 return lookup->IsTheHole(isolate) ? isolate->heap()->undefined_value() |
295 : *lookup; | 295 : *lookup; |
296 } | 296 } |
297 | 297 |
298 | 298 |
299 RUNTIME_FUNCTION(Runtime_WeakCollectionHas) { | 299 RUNTIME_FUNCTION(Runtime_WeakCollectionHas) { |
300 HandleScope scope(isolate); | 300 HandleScope scope(isolate); |
301 DCHECK(args.length() == 3); | 301 DCHECK(args.length() == 3); |
302 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); | 302 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
303 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); | 303 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
304 CONVERT_SMI_ARG_CHECKED(hash, 2) | 304 CONVERT_SMI_ARG_CHECKED(hash, 2) |
305 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); | 305 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); |
306 Handle<ObjectHashTable> table( | 306 Handle<ObjectHashTable> table( |
307 ObjectHashTable::cast(weak_collection->table())); | 307 ObjectHashTable::cast(weak_collection->table())); |
308 RUNTIME_ASSERT(table->IsKey(*key)); | 308 RUNTIME_ASSERT(table->IsKey(isolate, *key)); |
309 Handle<Object> lookup(table->Lookup(key, hash), isolate); | 309 Handle<Object> lookup(table->Lookup(key, hash), isolate); |
310 return isolate->heap()->ToBoolean(!lookup->IsTheHole(isolate)); | 310 return isolate->heap()->ToBoolean(!lookup->IsTheHole(isolate)); |
311 } | 311 } |
312 | 312 |
313 | 313 |
314 RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) { | 314 RUNTIME_FUNCTION(Runtime_WeakCollectionDelete) { |
315 HandleScope scope(isolate); | 315 HandleScope scope(isolate); |
316 DCHECK(args.length() == 3); | 316 DCHECK(args.length() == 3); |
317 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); | 317 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
318 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); | 318 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
319 CONVERT_SMI_ARG_CHECKED(hash, 2) | 319 CONVERT_SMI_ARG_CHECKED(hash, 2) |
320 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); | 320 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); |
321 Handle<ObjectHashTable> table( | 321 Handle<ObjectHashTable> table( |
322 ObjectHashTable::cast(weak_collection->table())); | 322 ObjectHashTable::cast(weak_collection->table())); |
323 RUNTIME_ASSERT(table->IsKey(*key)); | 323 RUNTIME_ASSERT(table->IsKey(isolate, *key)); |
324 bool was_present = JSWeakCollection::Delete(weak_collection, key, hash); | 324 bool was_present = JSWeakCollection::Delete(weak_collection, key, hash); |
325 return isolate->heap()->ToBoolean(was_present); | 325 return isolate->heap()->ToBoolean(was_present); |
326 } | 326 } |
327 | 327 |
328 | 328 |
329 RUNTIME_FUNCTION(Runtime_WeakCollectionSet) { | 329 RUNTIME_FUNCTION(Runtime_WeakCollectionSet) { |
330 HandleScope scope(isolate); | 330 HandleScope scope(isolate); |
331 DCHECK(args.length() == 4); | 331 DCHECK(args.length() == 4); |
332 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); | 332 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, weak_collection, 0); |
333 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); | 333 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
334 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); | 334 RUNTIME_ASSERT(key->IsJSReceiver() || key->IsSymbol()); |
335 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); | 335 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
336 CONVERT_SMI_ARG_CHECKED(hash, 3) | 336 CONVERT_SMI_ARG_CHECKED(hash, 3) |
337 Handle<ObjectHashTable> table( | 337 Handle<ObjectHashTable> table( |
338 ObjectHashTable::cast(weak_collection->table())); | 338 ObjectHashTable::cast(weak_collection->table())); |
339 RUNTIME_ASSERT(table->IsKey(*key)); | 339 RUNTIME_ASSERT(table->IsKey(isolate, *key)); |
340 JSWeakCollection::Set(weak_collection, key, value, hash); | 340 JSWeakCollection::Set(weak_collection, key, value, hash); |
341 return *weak_collection; | 341 return *weak_collection; |
342 } | 342 } |
343 | 343 |
344 | 344 |
345 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) { | 345 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) { |
346 HandleScope scope(isolate); | 346 HandleScope scope(isolate); |
347 DCHECK(args.length() == 2); | 347 DCHECK(args.length() == 2); |
348 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); | 348 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); |
349 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]); | 349 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]); |
350 RUNTIME_ASSERT(max_values >= 0); | 350 RUNTIME_ASSERT(max_values >= 0); |
351 | 351 |
352 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); | 352 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); |
353 if (max_values == 0 || max_values > table->NumberOfElements()) { | 353 if (max_values == 0 || max_values > table->NumberOfElements()) { |
354 max_values = table->NumberOfElements(); | 354 max_values = table->NumberOfElements(); |
355 } | 355 } |
356 Handle<FixedArray> values = isolate->factory()->NewFixedArray(max_values); | 356 Handle<FixedArray> values = isolate->factory()->NewFixedArray(max_values); |
357 // Recompute max_values because GC could have removed elements from the table. | 357 // Recompute max_values because GC could have removed elements from the table. |
358 if (max_values > table->NumberOfElements()) { | 358 if (max_values > table->NumberOfElements()) { |
359 max_values = table->NumberOfElements(); | 359 max_values = table->NumberOfElements(); |
360 } | 360 } |
361 { | 361 { |
362 DisallowHeapAllocation no_gc; | 362 DisallowHeapAllocation no_gc; |
363 int count = 0; | 363 int count = 0; |
364 for (int i = 0; count < max_values && i < table->Capacity(); i++) { | 364 for (int i = 0; count < max_values && i < table->Capacity(); i++) { |
365 Handle<Object> key(table->KeyAt(i), isolate); | 365 Object* key = table->KeyAt(i); |
366 if (table->IsKey(*key)) values->set(count++, *key); | 366 if (table->IsKey(isolate, key)) values->set(count++, key); |
367 } | 367 } |
368 DCHECK_EQ(max_values, count); | 368 DCHECK_EQ(max_values, count); |
369 } | 369 } |
370 return *isolate->factory()->NewJSArrayWithElements(values); | 370 return *isolate->factory()->NewJSArrayWithElements(values); |
371 } | 371 } |
372 } // namespace internal | 372 } // namespace internal |
373 } // namespace v8 | 373 } // namespace v8 |
OLD | NEW |