| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 (function(global, utils) { | |
| 6 | |
| 7 "use strict"; | |
| 8 | |
| 9 %CheckIsBootstrapping(); | |
| 10 | |
| 11 // ----------------------------------------------------------------------- | |
| 12 // Imports | |
| 13 | |
| 14 var arrayIterationKindSymbol = | |
| 15 utils.ImportNow("array_iteration_kind_symbol"); | |
| 16 var arrayIteratorNextIndexSymbol = | |
| 17 utils.ImportNow("array_iterator_next_symbol"); | |
| 18 var arrayIteratorObjectSymbol = | |
| 19 utils.ImportNow("array_iterator_object_symbol"); | |
| 20 var GlobalArray = global.Array; | |
| 21 var IteratorPrototype = utils.ImportNow("IteratorPrototype"); | |
| 22 var iteratorSymbol = utils.ImportNow("iterator_symbol"); | |
| 23 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | |
| 24 var GlobalTypedArray = %object_get_prototype_of(global.Uint8Array); | |
| 25 | |
| 26 // ----------------------------------------------------------------------- | |
| 27 | |
| 28 function ArrayIterator() {} | |
| 29 | |
| 30 | |
| 31 // TODO(wingo): Update section numbers when ES6 has stabilized. The | |
| 32 // section numbers below are already out of date as of the May 2014 | |
| 33 // draft. | |
| 34 | |
| 35 | |
| 36 // 15.4.5.1 CreateArrayIterator Abstract Operation | |
| 37 function CreateArrayIterator(array, kind) { | |
| 38 var object = TO_OBJECT(array); | |
| 39 var iterator = new ArrayIterator; | |
| 40 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object); | |
| 41 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, 0); | |
| 42 SET_PRIVATE(iterator, arrayIterationKindSymbol, kind); | |
| 43 return iterator; | |
| 44 } | |
| 45 | |
| 46 | |
| 47 // 22.1.5.2.2 %ArrayIteratorPrototype%[@@iterator] | |
| 48 function ArrayIteratorIterator() { | |
| 49 return this; | |
| 50 } | |
| 51 | |
| 52 | |
| 53 // ES6 section 22.1.5.2.1 %ArrayIteratorPrototype%.next( ) | |
| 54 function ArrayIteratorNext() { | |
| 55 var iterator = this; | |
| 56 var value = UNDEFINED; | |
| 57 var done = true; | |
| 58 | |
| 59 if (!IS_RECEIVER(iterator) || | |
| 60 !HAS_DEFINED_PRIVATE(iterator, arrayIteratorNextIndexSymbol)) { | |
| 61 throw %make_type_error(kIncompatibleMethodReceiver, | |
| 62 'Array Iterator.prototype.next', this); | |
| 63 } | |
| 64 | |
| 65 var array = GET_PRIVATE(iterator, arrayIteratorObjectSymbol); | |
| 66 if (!IS_UNDEFINED(array)) { | |
| 67 var index = GET_PRIVATE(iterator, arrayIteratorNextIndexSymbol); | |
| 68 var itemKind = GET_PRIVATE(iterator, arrayIterationKindSymbol); | |
| 69 var length = TO_UINT32(array.length); | |
| 70 | |
| 71 // "sparse" is never used. | |
| 72 | |
| 73 if (index >= length) { | |
| 74 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, UNDEFINED); | |
| 75 } else { | |
| 76 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, index + 1); | |
| 77 | |
| 78 if (itemKind == ITERATOR_KIND_VALUES) { | |
| 79 value = array[index]; | |
| 80 } else if (itemKind == ITERATOR_KIND_ENTRIES) { | |
| 81 value = [index, array[index]]; | |
| 82 } else { | |
| 83 value = index; | |
| 84 } | |
| 85 done = false; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 return %_CreateIterResultObject(value, done); | |
| 90 } | |
| 91 | |
| 92 | |
| 93 function ArrayEntries() { | |
| 94 return CreateArrayIterator(this, ITERATOR_KIND_ENTRIES); | |
| 95 } | |
| 96 | |
| 97 | |
| 98 function ArrayValues() { | |
| 99 return CreateArrayIterator(this, ITERATOR_KIND_VALUES); | |
| 100 } | |
| 101 | |
| 102 | |
| 103 function ArrayKeys() { | |
| 104 return CreateArrayIterator(this, ITERATOR_KIND_KEYS); | |
| 105 } | |
| 106 | |
| 107 // TODO(littledan): Check for detached TypedArray in these three methods | |
| 108 function TypedArrayEntries() { | |
| 109 if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray); | |
| 110 return %_Call(ArrayEntries, this); | |
| 111 } | |
| 112 | |
| 113 | |
| 114 function TypedArrayValues() { | |
| 115 if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray); | |
| 116 return %_Call(ArrayValues, this); | |
| 117 } | |
| 118 | |
| 119 | |
| 120 function TypedArrayKeys() { | |
| 121 if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray); | |
| 122 return %_Call(ArrayKeys, this); | |
| 123 } | |
| 124 | |
| 125 | |
| 126 %FunctionSetPrototype(ArrayIterator, {__proto__: IteratorPrototype}); | |
| 127 %FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator'); | |
| 128 | |
| 129 utils.InstallFunctions(ArrayIterator.prototype, DONT_ENUM, [ | |
| 130 'next', ArrayIteratorNext | |
| 131 ]); | |
| 132 utils.SetFunctionName(ArrayIteratorIterator, iteratorSymbol); | |
| 133 %AddNamedProperty(ArrayIterator.prototype, toStringTagSymbol, | |
| 134 "Array Iterator", READ_ONLY | DONT_ENUM); | |
| 135 | |
| 136 utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [ | |
| 137 // No 'values' since it breaks webcompat: http://crbug.com/409858 | |
| 138 'entries', ArrayEntries, | |
| 139 'keys', ArrayKeys | |
| 140 ]); | |
| 141 | |
| 142 // TODO(adam): Remove these calls once 'values' is in the above | |
| 143 // InstallFunctions block, as they'll be redundant. | |
| 144 utils.SetFunctionName(ArrayValues, 'values'); | |
| 145 %FunctionRemovePrototype(ArrayValues); | |
| 146 %SetNativeFlag(ArrayValues); | |
| 147 | |
| 148 %AddNamedProperty(GlobalArray.prototype, iteratorSymbol, ArrayValues, | |
| 149 DONT_ENUM); | |
| 150 | |
| 151 utils.InstallFunctions(GlobalTypedArray.prototype, DONT_ENUM, [ | |
| 152 'entries', TypedArrayEntries, | |
| 153 'keys', TypedArrayKeys, | |
| 154 'values', TypedArrayValues | |
| 155 ]); | |
| 156 %AddNamedProperty(GlobalTypedArray.prototype, | |
| 157 iteratorSymbol, TypedArrayValues, DONT_ENUM); | |
| 158 | |
| 159 // ------------------------------------------------------------------- | |
| 160 // Exports | |
| 161 | |
| 162 utils.Export(function(to) { | |
| 163 to.ArrayValues = ArrayValues; | |
| 164 }); | |
| 165 | |
| 166 %InstallToContext(["array_values_iterator", ArrayValues]); | |
| 167 | |
| 168 }) | |
| OLD | NEW |