danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 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 | |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 80 | iterator[arrayIteratorNextIndexSymbol] = index + 1; |
| 81 | |
| 82 | if (itemKind == ARRAY_ITERATOR_KIND_VALUES) |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 83 | return CreateIteratorResultObject(array[index], false); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 84 | |
| 85 | if (itemKind == ARRAY_ITERATOR_KIND_ENTRIES) |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 86 | return CreateIteratorResultObject([index, array[index]], false); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 87 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 88 | return CreateIteratorResultObject(index, false); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | function ArrayEntries() { |
| 92 | return CreateArrayIterator(this, ARRAY_ITERATOR_KIND_ENTRIES); |
| 93 | } |
| 94 | |
| 95 | function ArrayValues() { |
| 96 | return CreateArrayIterator(this, ARRAY_ITERATOR_KIND_VALUES); |
| 97 | } |
| 98 | |
| 99 | function ArrayKeys() { |
| 100 | return CreateArrayIterator(this, ARRAY_ITERATOR_KIND_KEYS); |
| 101 | } |
| 102 | |
| 103 | function SetUpArrayIterator() { |
| 104 | %CheckIsBootstrapping(); |
| 105 | |
| 106 | %FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator'); |
| 107 | %FunctionSetReadOnlyPrototype(ArrayIterator); |
| 108 | |
| 109 | InstallFunctions(ArrayIterator.prototype, DONT_ENUM, $Array( |
| 110 | 'next', ArrayIteratorNext |
| 111 | )); |
| 112 | } |
| 113 | |
| 114 | SetUpArrayIterator(); |
| 115 | |
| 116 | function ExtendArrayPrototype() { |
| 117 | %CheckIsBootstrapping(); |
| 118 | |
| 119 | InstallFunctions($Array.prototype, DONT_ENUM, $Array( |
| 120 | 'entries', ArrayEntries, |
| 121 | 'values', ArrayValues, |
| 122 | 'keys', ArrayKeys |
| 123 | )); |
| 124 | } |
| 125 | |
| 126 | ExtendArrayPrototype(); |