| 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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 result->set_table(holder->table()); | 220 result->set_table(holder->table()); |
| 221 result->set_index(Smi::FromInt(Smi::cast(holder->index())->value())); | 221 result->set_index(Smi::FromInt(Smi::cast(holder->index())->value())); |
| 222 result->set_kind(Smi::FromInt(Smi::cast(holder->kind())->value())); | 222 result->set_kind(Smi::FromInt(Smi::cast(holder->kind())->value())); |
| 223 | 223 |
| 224 return *result; | 224 return *result; |
| 225 } | 225 } |
| 226 | 226 |
| 227 | 227 |
| 228 RUNTIME_FUNCTION(Runtime_GetWeakMapEntries) { | 228 RUNTIME_FUNCTION(Runtime_GetWeakMapEntries) { |
| 229 HandleScope scope(isolate); | 229 HandleScope scope(isolate); |
| 230 DCHECK(args.length() == 1); | 230 DCHECK(args.length() == 2); |
| 231 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); | 231 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); |
| 232 CONVERT_NUMBER_CHECKED(int, max_entries, Int32, args[1]); |
| 233 RUNTIME_ASSERT(max_entries >= 0); |
| 234 |
| 232 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); | 235 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); |
| 236 if (max_entries == 0 || max_entries > table->NumberOfElements()) { |
| 237 max_entries = table->NumberOfElements(); |
| 238 } |
| 233 Handle<FixedArray> entries = | 239 Handle<FixedArray> entries = |
| 234 isolate->factory()->NewFixedArray(table->NumberOfElements() * 2); | 240 isolate->factory()->NewFixedArray(max_entries * 2); |
| 235 { | 241 { |
| 236 DisallowHeapAllocation no_gc; | 242 DisallowHeapAllocation no_gc; |
| 237 int number_of_non_hole_elements = 0; | 243 int count = 0; |
| 238 for (int i = 0; i < table->Capacity(); i++) { | 244 for (int i = 0; count / 2 < max_entries && i < table->Capacity(); i++) { |
| 239 Handle<Object> key(table->KeyAt(i), isolate); | 245 Handle<Object> key(table->KeyAt(i), isolate); |
| 240 if (table->IsKey(*key)) { | 246 if (table->IsKey(*key)) { |
| 241 entries->set(number_of_non_hole_elements++, *key); | 247 entries->set(count++, *key); |
| 242 Object* value = table->Lookup(key); | 248 Object* value = table->Lookup(key); |
| 243 entries->set(number_of_non_hole_elements++, value); | 249 entries->set(count++, value); |
| 244 } | 250 } |
| 245 } | 251 } |
| 246 DCHECK_EQ(table->NumberOfElements() * 2, number_of_non_hole_elements); | 252 DCHECK_EQ(max_entries * 2, count); |
| 247 } | 253 } |
| 248 return *isolate->factory()->NewJSArrayWithElements(entries); | 254 return *isolate->factory()->NewJSArrayWithElements(entries); |
| 249 } | 255 } |
| 250 | 256 |
| 251 | 257 |
| 252 RUNTIME_FUNCTION(Runtime_MapIteratorNext) { | 258 RUNTIME_FUNCTION(Runtime_MapIteratorNext) { |
| 253 SealHandleScope shs(isolate); | 259 SealHandleScope shs(isolate); |
| 254 DCHECK(args.length() == 2); | 260 DCHECK(args.length() == 2); |
| 255 CONVERT_ARG_CHECKED(JSMapIterator, holder, 0); | 261 CONVERT_ARG_CHECKED(JSMapIterator, holder, 0); |
| 256 CONVERT_ARG_CHECKED(JSArray, value_array, 1); | 262 CONVERT_ARG_CHECKED(JSArray, value_array, 1); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 if (*table != *new_table) { | 345 if (*table != *new_table) { |
| 340 // Zap the old table since we didn't record slots for its elements. | 346 // Zap the old table since we didn't record slots for its elements. |
| 341 table->FillWithHoles(0, table->length()); | 347 table->FillWithHoles(0, table->length()); |
| 342 } | 348 } |
| 343 return *weak_collection; | 349 return *weak_collection; |
| 344 } | 350 } |
| 345 | 351 |
| 346 | 352 |
| 347 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) { | 353 RUNTIME_FUNCTION(Runtime_GetWeakSetValues) { |
| 348 HandleScope scope(isolate); | 354 HandleScope scope(isolate); |
| 349 DCHECK(args.length() == 1); | 355 DCHECK(args.length() == 2); |
| 350 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); | 356 CONVERT_ARG_HANDLE_CHECKED(JSWeakCollection, holder, 0); |
| 357 CONVERT_NUMBER_CHECKED(int, max_values, Int32, args[1]); |
| 358 RUNTIME_ASSERT(max_values >= 0); |
| 359 |
| 351 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); | 360 Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); |
| 352 Handle<FixedArray> values = | 361 if (max_values == 0 || max_values > table->NumberOfElements()) { |
| 353 isolate->factory()->NewFixedArray(table->NumberOfElements()); | 362 max_values = table->NumberOfElements(); |
| 363 } |
| 364 Handle<FixedArray> values = isolate->factory()->NewFixedArray(max_values); |
| 354 { | 365 { |
| 355 DisallowHeapAllocation no_gc; | 366 DisallowHeapAllocation no_gc; |
| 356 int number_of_non_hole_elements = 0; | 367 int count = 0; |
| 357 for (int i = 0; i < table->Capacity(); i++) { | 368 for (int i = 0; count < max_values && i < table->Capacity(); i++) { |
| 358 Handle<Object> key(table->KeyAt(i), isolate); | 369 Handle<Object> key(table->KeyAt(i), isolate); |
| 359 if (table->IsKey(*key)) { | 370 if (table->IsKey(*key)) values->set(count++, *key); |
| 360 values->set(number_of_non_hole_elements++, *key); | |
| 361 } | |
| 362 } | 371 } |
| 363 DCHECK_EQ(table->NumberOfElements(), number_of_non_hole_elements); | 372 DCHECK_EQ(max_values, count); |
| 364 } | 373 } |
| 365 return *isolate->factory()->NewJSArrayWithElements(values); | 374 return *isolate->factory()->NewJSArrayWithElements(values); |
| 366 } | 375 } |
| 367 | 376 |
| 368 | 377 |
| 369 RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) { | 378 RUNTIME_FUNCTION(Runtime_ObservationWeakMapCreate) { |
| 370 HandleScope scope(isolate); | 379 HandleScope scope(isolate); |
| 371 DCHECK(args.length() == 0); | 380 DCHECK(args.length() == 0); |
| 372 // TODO(adamk): Currently this runtime function is only called three times per | 381 // TODO(adamk): Currently this runtime function is only called three times per |
| 373 // isolate. If it's called more often, the map should be moved into the | 382 // isolate. If it's called more often, the map should be moved into the |
| 374 // strong root list. | 383 // strong root list. |
| 375 Handle<Map> map = | 384 Handle<Map> map = |
| 376 isolate->factory()->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); | 385 isolate->factory()->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); |
| 377 Handle<JSWeakMap> weakmap = | 386 Handle<JSWeakMap> weakmap = |
| 378 Handle<JSWeakMap>::cast(isolate->factory()->NewJSObjectFromMap(map)); | 387 Handle<JSWeakMap>::cast(isolate->factory()->NewJSObjectFromMap(map)); |
| 379 return *WeakCollectionInitialize(isolate, weakmap); | 388 return *WeakCollectionInitialize(isolate, weakmap); |
| 380 } | 389 } |
| 381 } | 390 } |
| 382 } // namespace v8::internal | 391 } // namespace v8::internal |
| OLD | NEW |