@@ -64,6 +64,21 @@ var tests = [
6464 } ;
6565 }
6666
67+ function getIterableObjNextDesc ( array )
68+ {
69+ return {
70+ get : function ( ) {
71+ array . shift ( ) ; // side effect
72+ return function ( ) {
73+ return {
74+ value : array . shift ( ) ,
75+ done : array . length == 0
76+ } ;
77+ } ;
78+ }
79+ } ;
80+ }
81+
6782 var TypedArray = [
6883 'Int8Array' ,
6984 'Uint8Array' ,
@@ -84,36 +99,74 @@ var tests = [
8499 assert . areEqual ( 3 , arr [ 2 ] , "TypedArray " + t + " created from iterable has element #2 == 3" ) ;
85100 }
86101
87- for ( var t of TypedArray ) {
88- var a = [ 1 , 2 , 3 , 4 ] ;
89- a [ Symbol . iterator ] = getIterableObj ( [ 99 , 0 ] ) [ Symbol . iterator ] ;
90- var arr = new this [ t ] ( a ) ;
91- assert . areEqual ( 1 , arr . length , "TypedArray " + t + " created from array with user-defined iterator has length == 1" ) ;
92- assert . areEqual ( 99 , arr [ 0 ] , "TypedArray " + t + " created from array with user-defined iterator has element #0 == 99" ) ;
93- }
102+ // change array's iterator
103+ ( function ( ) {
104+ for ( var t of TypedArray ) {
105+ var a = [ 1 , 2 , 3 , 4 ] ;
106+ a [ Symbol . iterator ] = getIterableObj ( [ 99 , 0 ] ) [ Symbol . iterator ] ;
107+ var arr = new this [ t ] ( a ) ;
108+ assert . areEqual ( 1 , arr . length , "TypedArray " + t + " created from array with user-defined iterator has length == 1" ) ;
109+ assert . areEqual ( 99 , arr [ 0 ] , "TypedArray " + t + " created from array with user-defined iterator has element #0 == 99" ) ;
110+ }
111+ } ) ( ) ;
94112
95- function testTypedArrayConstructorWithIterableArray ( t ) {
96- Array . prototype [ Symbol . iterator ] = getIterableObj ( [ 99 , 0 ] ) [ Symbol . iterator ] ;
97- var arr = new this [ t ] ( a ) ;
98- assert . areEqual ( 1 , arr . length , "TypedArray " + t + " created from array with Array.prototype overridden has length == 1" ) ;
99- assert . areEqual ( 99 , arr [ 0 ] , "TypedArray " + t + " created from array with Array.prototype overriden has element #0 == 99" ) ;
113+ // helpers for testing all typed arrays when built-in array iterator is changed
114+ function testTypedArrayConstructorWithIterableArray ( inputarray , t , func , text ) {
115+ func ( ) ;
116+ var arr = new this [ t ] ( inputarray ) ;
117+ assert . areEqual ( 1 , arr . length , "TypedArray " + t + " created from array with " + text + " has length == 1" ) ;
118+ assert . areEqual ( 99 , arr [ 0 ] , "TypedArray " + t + " created from array with " + text + " has element #0 == 99" ) ;
100119 }
120+ function testAllTypedArrayConstructorsWithIterableArray ( inputarray , func , text ) {
121+ testTypedArrayConstructorWithIterableArray ( inputarray , 'Int8Array' , func , text ) ;
122+ testTypedArrayConstructorWithIterableArray ( inputarray , 'Uint8Array' , func , text ) ;
123+ testTypedArrayConstructorWithIterableArray ( inputarray , 'Uint8ClampedArray' , func , text ) ;
124+ testTypedArrayConstructorWithIterableArray ( inputarray , 'Int16Array' , func , text ) ;
125+ testTypedArrayConstructorWithIterableArray ( inputarray , 'Uint16Array' , func , text ) ;
126+ testTypedArrayConstructorWithIterableArray ( inputarray , 'Int32Array' , func , text ) ;
127+ testTypedArrayConstructorWithIterableArray ( inputarray , 'Uint32Array' , func , text ) ;
128+ testTypedArrayConstructorWithIterableArray ( inputarray , 'Float32Array' , func , text ) ;
129+ testTypedArrayConstructorWithIterableArray ( inputarray , 'Float64Array' , func , text ) ;
130+ }
131+
132+ // change built-in Array prototype's iterator
133+ ( function ( ) {
134+ var builtinArrayPrototypeIteratorDesc = Object . getOwnPropertyDescriptor ( Array . prototype , Symbol . iterator ) ;
135+ var a = [ 1 , 2 , 3 , 4 ] ;
136+ Object . defineProperty ( Array . prototype , Symbol . iterator , { enumerable : false , configurable : true , writable : true } ) ;
137+
138+ var overrideBuiltinArrayPrototypeIterator = function ( ) {
139+ Array . prototype [ Symbol . iterator ] = getIterableObj ( [ 99 , 0 ] ) [ Symbol . iterator ] ;
140+ } ;
141+ testAllTypedArrayConstructorsWithIterableArray ( a , overrideBuiltinArrayPrototypeIterator , "Array.prototype overriden" ) ;
142+ Object . defineProperty ( Array . prototype , Symbol . iterator , builtinArrayPrototypeIteratorDesc ) ;
143+ } ) ( ) ;
144+
145+ // change built-in array iterator's next function
146+ ( function ( ) {
147+ var arrayIteratorProto = Object . getPrototypeOf ( [ ] [ Symbol . iterator ] ( ) ) ;
148+ var builtinArrayPrototypeIteratorNext = arrayIteratorProto . next ;
149+ var overrideBuiltinArrayIteratorNext = function ( ) {
150+ arrayIteratorProto . next = getIterableObj ( [ 99 , 0 ] ) [ Symbol . iterator ] ( ) . next ;
151+ }
152+ var a = [ 1 , 2 , 3 , 4 ] ;
153+ testAllTypedArrayConstructorsWithIterableArray ( a , overrideBuiltinArrayIteratorNext , "%ArrayIteratorPrototype%.next overriden" ) ;
154+ arrayIteratorProto . next = builtinArrayPrototypeIteratorNext ;
155+ } ) ( ) ;
101156
102- var builtinArrayPrototypeIteratorDesc = Object . getOwnPropertyDescriptor ( Array . prototype , Symbol . iterator ) ;
103- var a = [ 1 , 2 , 3 , 4 ] ;
104- Object . defineProperty ( Array . prototype , Symbol . iterator , { enumerable : false , configurable : true , writable : true } ) ;
105- testTypedArrayConstructorWithIterableArray ( 'Int8Array' ) ;
106- testTypedArrayConstructorWithIterableArray ( 'Uint8Array' ) ;
107- testTypedArrayConstructorWithIterableArray ( 'Uint8ClampedArray' ) ;
108- testTypedArrayConstructorWithIterableArray ( 'Int16Array' ) ;
109- testTypedArrayConstructorWithIterableArray ( 'Uint16Array' ) ;
110- testTypedArrayConstructorWithIterableArray ( 'Int32Array' ) ;
111- testTypedArrayConstructorWithIterableArray ( 'Uint32Array' ) ;
112- testTypedArrayConstructorWithIterableArray ( 'Float32Array' ) ;
113- testTypedArrayConstructorWithIterableArray ( 'Float64Array' ) ;
114- Object . defineProperty ( Array . prototype , Symbol . iterator , builtinArrayPrototypeIteratorDesc ) ;
157+ // change built-in array iterator's next getter function
158+ ( function ( ) {
159+ var arrayIteratorProto = Object . getPrototypeOf ( [ ] [ Symbol . iterator ] ( ) ) ;
160+ var builtinArrayPrototypeIteratorNextDesc = Object . getOwnPropertyDescriptor ( arrayIteratorProto , "next" ) ;
161+ var overrideBuiltinArrayIteratorNext = function ( ) {
162+ Object . defineProperty ( arrayIteratorProto , "next" , getIterableObjNextDesc ( [ 0 , 99 , 0 ] ) ) ;
163+ }
164+ var a = [ 1 , 2 , 3 , 4 ] ;
165+ testAllTypedArrayConstructorsWithIterableArray ( a , overrideBuiltinArrayIteratorNext , "%ArrayIteratorPrototype%.next overriden by getter" ) ;
166+ Object . defineProperty ( arrayIteratorProto , "next" , builtinArrayPrototypeIteratorNextDesc ) ;
167+ } ) ( ) ;
115168 }
116169 }
117170] ;
118171
119- testRunner . runTests ( tests ) ;
172+ testRunner . runTests ( tests , { verbose : WScript . Arguments [ 0 ] != "summary" } ) ;
0 commit comments