OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // 'AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 'use strict'; |
| 29 |
| 30 // This file relies on the fact that the following declaration has been made |
| 31 // in runtime.js: |
| 32 // var $Array = global.Array; |
| 33 |
| 34 var ARRAY_ITERATOR_KIND_KEYS = 1; |
| 35 var ARRAY_ITERATOR_KIND_VALUES = 2; |
| 36 var ARRAY_ITERATOR_KIND_ENTRIES = 3; |
| 37 // The spec draft also has "sparse" but it is never used. |
| 38 |
| 39 var iteratorObjectSymbol = %CreateSymbol(void 0); |
| 40 var arrayIteratorNextIndexSymbol = %CreateSymbol(void 0); |
| 41 var arrayIterationKindSymbol = %CreateSymbol(void 0); |
| 42 |
| 43 function ArrayIterator() {} |
| 44 |
| 45 // 15.4.5.1 CreateArrayIterator Abstract Operation |
| 46 function CreateArrayIterator(array, kind) { |
| 47 var object = ToObject(array); |
| 48 var iterator = new ArrayIterator; |
| 49 iterator[iteratorObjectSymbol] = object; |
| 50 iterator[arrayIteratorNextIndexSymbol] = 0; |
| 51 iterator[arrayIterationKindSymbol] = kind; |
| 52 return iterator; |
| 53 } |
| 54 |
| 55 // 15.19.4.3.4 CreateItrResultObject |
| 56 function CreateIteratorResultObject(value, done) { |
| 57 return {value: value, done: done}; |
| 58 } |
| 59 |
| 60 // 15.4.5.2.2 ArrayIterator.prototype.next( ) |
| 61 function ArrayIteratorNext() { |
| 62 var iterator = ToObject(this); |
| 63 var array = iterator[iteratorObjectSymbol]; |
| 64 if (!array) { |
| 65 throw MakeTypeError('incompatible_method_receiver', |
| 66 ['Array Iterator.prototype.next']); |
| 67 } |
| 68 |
| 69 var index = iterator[arrayIteratorNextIndexSymbol]; |
| 70 var itemKind = iterator[arrayIterationKindSymbol]; |
| 71 var length = TO_UINT32(array.length); |
| 72 |
| 73 // "sparse" is never used. |
| 74 |
| 75 if (index >= length) { |
| 76 iterator[arrayIteratorNextIndexSymbol] = 1 / 0; // Infinity |
| 77 return CreateIteratorResultObject(void 0, true); |
| 78 } |
| 79 |
| 80 var elementKey = ToString(index); |
| 81 iterator[arrayIteratorNextIndexSymbol] = index + 1; |
| 82 |
| 83 if (itemKind == ARRAY_ITERATOR_KIND_VALUES) |
| 84 return CreateIteratorResultObject(array[elementKey], false); |
| 85 |
| 86 if (itemKind == ARRAY_ITERATOR_KIND_ENTRIES) |
| 87 return CreateIteratorResultObject([elementKey, array[elementKey]], false); |
| 88 |
| 89 return CreateIteratorResultObject(elementKey, false); |
| 90 } |
| 91 |
| 92 function ArrayEntries() { |
| 93 return CreateArrayIterator(this, ARRAY_ITERATOR_KIND_ENTRIES); |
| 94 } |
| 95 |
| 96 function ArrayValues() { |
| 97 return CreateArrayIterator(this, ARRAY_ITERATOR_KIND_VALUES); |
| 98 } |
| 99 |
| 100 function ArrayKeys() { |
| 101 return CreateArrayIterator(this, ARRAY_ITERATOR_KIND_KEYS); |
| 102 } |
| 103 |
| 104 function SetUpArrayIterator() { |
| 105 %CheckIsBootstrapping(); |
| 106 |
| 107 %FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator'); |
| 108 %FunctionSetReadOnlyPrototype(ArrayIterator); |
| 109 |
| 110 InstallFunctions(ArrayIterator.prototype, DONT_ENUM, $Array( |
| 111 'next', ArrayIteratorNext |
| 112 )); |
| 113 } |
| 114 |
| 115 SetUpArrayIterator(); |
| 116 |
| 117 function ExtendArrayPrototype() { |
| 118 %CheckIsBootstrapping(); |
| 119 |
| 120 InstallFunctions($Array.prototype, DONT_ENUM, $Array( |
| 121 'entries', ArrayEntries, |
| 122 'values', ArrayValues, |
| 123 'keys', ArrayKeys |
| 124 )); |
| 125 } |
| 126 |
| 127 ExtendArrayPrototype(); |
OLD | NEW |