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 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 return *isolate->factory()->NewJSArrayWithElements(details); | 226 return *isolate->factory()->NewJSArrayWithElements(details); |
227 } | 227 } |
228 | 228 |
229 | 229 |
230 RUNTIME_FUNCTION(Runtime_GetWeakMapEntries) { | 230 RUNTIME_FUNCTION(Runtime_GetWeakMapEntries) { |
231 HandleScope scope(isolate); | 231 HandleScope scope(isolate); |
232 DCHECK_EQ(2, args.length()); | 232 DCHECK_EQ(2, args.length()); |
233 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); | 233 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); |
234 CONVERT_NUMBER_CHECKED(int, max_entries, Int32, args[1]); | 234 CONVERT_NUMBER_CHECKED(int, max_entries, Int32, args[1]); |
235 CHECK(max_entries >= 0); | 235 CHECK(max_entries >= 0); |
236 | 236 return *JSWeakCollection::GetEntries(holder, max_entries); |
237 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); | |
238 if (max_entries == 0 || max_entries > table->NumberOfElements()) { | |
239 max_entries = table->NumberOfElements(); | |
240 } | |
241 Handle<FixedArray> entries = | |
242 isolate->factory()->NewFixedArray(max_entries * 2); | |
243 // Allocation can cause GC can delete weak elements. Reload. | |
244 if (max_entries > table->NumberOfElements()) { | |
245 max_entries = table->NumberOfElements(); | |
246 } | |
247 | |
248 { | |
249 DisallowHeapAllocation no_gc; | |
250 int count = 0; | |
251 for (int i = 0; count / 2 < max_entries && i < table->Capacity(); i++) { | |
252 Handle<Object> key(table->KeyAt(i), isolate); | |
253 if (table->IsKey(isolate, *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 return *isolate->factory()->NewJSArrayWithElements(entries); | |
262 } | 237 } |
263 | 238 |
264 | 239 |
265 RUNTIME_FUNCTION(Runtime_MapIteratorNext) { | 240 RUNTIME_FUNCTION(Runtime_MapIteratorNext) { |
266 SealHandleScope shs(isolate); | 241 SealHandleScope shs(isolate); |
267 DCHECK_EQ(2, args.length()); | 242 DCHECK_EQ(2, args.length()); |
268 CONVERT_ARG_CHECKED(JSMapIterator, holder, 0); | 243 CONVERT_ARG_CHECKED(JSMapIterator, holder, 0); |
269 CONVERT_ARG_CHECKED(JSArray, value_array, 1); | 244 CONVERT_ARG_CHECKED(JSArray, value_array, 1); |
270 return holder->Next(value_array); | 245 return holder->Next(value_array); |
271 } | 246 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 return *weak_collection; | 316 return *weak_collection; |
342 } | 317 } |
343 | 318 |
344 | 319 |
345 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) { | 320 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) { |
346 HandleScope scope(isolate); | 321 HandleScope scope(isolate); |
347 DCHECK_EQ(2, args.length()); | 322 DCHECK_EQ(2, args.length()); |
348 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); | 323 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); |
349 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]); | 324 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]); |
350 CHECK(max_values >= 0); | 325 CHECK(max_values >= 0); |
351 | 326 return *JSWeakCollection::GetEntries(holder, max_values); |
352 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); | |
353 if (max_values == 0 || max_values > table->NumberOfElements()) { | |
354 max_values = table->NumberOfElements(); | |
355 } | |
356 Handle<FixedArray> values = isolate->factory()->NewFixedArray(max_values); | |
357 // Recompute max_values because GC could have removed elements from the table. | |
358 if (max_values > table->NumberOfElements()) { | |
359 max_values = table->NumberOfElements(); | |
360 } | |
361 { | |
362 DisallowHeapAllocation no_gc; | |
363 int count = 0; | |
364 for (int i = 0; count < max_values && i < table->Capacity(); i++) { | |
365 Object* key = table->KeyAt(i); | |
366 if (table->IsKey(isolate, key)) values->set(count++, key); | |
367 } | |
368 DCHECK_EQ(max_values, count); | |
369 } | |
370 return *isolate->factory()->NewJSArrayWithElements(values); | |
371 } | 327 } |
372 } // namespace internal | 328 } // namespace internal |
373 } // namespace v8 | 329 } // namespace v8 |
OLD | NEW |