8168140: TypedArrays should implement ES6 iterator protocol

Reviewed-by: sundar, lagergren
This commit is contained in:
Hannes Wallnöfer 2016-10-21 10:02:09 +02:00
parent 6c3ae31f78
commit da7114d2a6
12 changed files with 184 additions and 5 deletions

View File

@ -47,13 +47,25 @@ public class ArrayIterator extends AbstractIterator {
private final Global global; private final Global global;
ArrayIterator(final Object iteratedObject, final IterationKind iterationKind, final Global global) { private ArrayIterator(final Object iteratedObject, final IterationKind iterationKind, final Global global) {
super(global.getArrayIteratorPrototype(), $nasgenmap$); super(global.getArrayIteratorPrototype(), $nasgenmap$);
this.iteratedObject = iteratedObject instanceof ScriptObject ? (ScriptObject) iteratedObject : null; this.iteratedObject = iteratedObject instanceof ScriptObject ? (ScriptObject) iteratedObject : null;
this.iterationKind = iterationKind; this.iterationKind = iterationKind;
this.global = global; this.global = global;
} }
static ArrayIterator newArrayValueIterator(final Object iteratedObject) {
return new ArrayIterator(Global.toObject(iteratedObject), IterationKind.VALUE, Global.instance());
}
static ArrayIterator newArrayKeyIterator(final Object iteratedObject) {
return new ArrayIterator(Global.toObject(iteratedObject), IterationKind.KEY, Global.instance());
}
static ArrayIterator newArrayKeyValueIterator(final Object iteratedObject) {
return new ArrayIterator(Global.toObject(iteratedObject), IterationKind.KEY_VALUE, Global.instance());
}
/** /**
* 22.1.5.2.1 %ArrayIteratorPrototype%.next() * 22.1.5.2.1 %ArrayIteratorPrototype%.next()
* *

View File

@ -1726,7 +1726,7 @@ public final class NativeArray extends ScriptObject implements OptimisticBuiltin
*/ */
@Function(attributes = Attribute.NOT_ENUMERABLE) @Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object entries(final Object self) { public static Object entries(final Object self) {
return new ArrayIterator(Global.toObject(self), AbstractIterator.IterationKind.KEY_VALUE, Global.instance()); return ArrayIterator.newArrayKeyValueIterator(self);
} }
/** /**
@ -1737,7 +1737,7 @@ public final class NativeArray extends ScriptObject implements OptimisticBuiltin
*/ */
@Function(attributes = Attribute.NOT_ENUMERABLE) @Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object keys(final Object self) { public static Object keys(final Object self) {
return new ArrayIterator(Global.toObject(self), AbstractIterator.IterationKind.KEY, Global.instance()); return ArrayIterator.newArrayKeyIterator(self);
} }
/** /**
@ -1748,7 +1748,7 @@ public final class NativeArray extends ScriptObject implements OptimisticBuiltin
*/ */
@Function(attributes = Attribute.NOT_ENUMERABLE) @Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object values(final Object self) { public static Object values(final Object self) {
return new ArrayIterator(Global.toObject(self), AbstractIterator.IterationKind.VALUE, Global.instance()); return ArrayIterator.newArrayValueIterator(self);
} }
/** /**
@ -1759,7 +1759,7 @@ public final class NativeArray extends ScriptObject implements OptimisticBuiltin
*/ */
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator") @Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) { public static Object getIterator(final Object self) {
return new ArrayIterator(Global.toObject(self), AbstractIterator.IterationKind.VALUE, Global.instance()); return ArrayIterator.newArrayValueIterator(self);
} }
/** /**

View File

@ -232,6 +232,17 @@ public final class NativeFloat32Array extends ArrayBufferView {
return (NativeFloat32Array)ArrayBufferView.subarrayImpl(self, begin, end); return (NativeFloat32Array)ArrayBufferView.subarrayImpl(self, begin, end);
} }
/**
* ECMA 6 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )
*
* @param self the self reference
* @return an iterator over the array's values
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) {
return ArrayIterator.newArrayValueIterator(self);
}
@Override @Override
protected ScriptObject getPrototype(final Global global) { protected ScriptObject getPrototype(final Global global) {
return global.getFloat32ArrayPrototype(); return global.getFloat32ArrayPrototype();

View File

@ -232,6 +232,17 @@ public final class NativeFloat64Array extends ArrayBufferView {
return (NativeFloat64Array)ArrayBufferView.subarrayImpl(self, begin, end); return (NativeFloat64Array)ArrayBufferView.subarrayImpl(self, begin, end);
} }
/**
* ECMA 6 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )
*
* @param self the self reference
* @return an iterator over the array's values
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) {
return ArrayIterator.newArrayValueIterator(self);
}
@Override @Override
protected ScriptObject getPrototype(final Global global) { protected ScriptObject getPrototype(final Global global) {
return global.getFloat64ArrayPrototype(); return global.getFloat64ArrayPrototype();

View File

@ -225,6 +225,17 @@ public final class NativeInt16Array extends ArrayBufferView {
return (NativeInt16Array)ArrayBufferView.subarrayImpl(self, begin, end); return (NativeInt16Array)ArrayBufferView.subarrayImpl(self, begin, end);
} }
/**
* ECMA 6 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )
*
* @param self the self reference
* @return an iterator over the array's values
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) {
return ArrayIterator.newArrayValueIterator(self);
}
@Override @Override
protected ScriptObject getPrototype(final Global global) { protected ScriptObject getPrototype(final Global global) {
return global.getInt16ArrayPrototype(); return global.getInt16ArrayPrototype();

View File

@ -224,6 +224,17 @@ public final class NativeInt32Array extends ArrayBufferView {
return (NativeInt32Array)ArrayBufferView.subarrayImpl(self, begin, end); return (NativeInt32Array)ArrayBufferView.subarrayImpl(self, begin, end);
} }
/**
* ECMA 6 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )
*
* @param self the self reference
* @return an iterator over the array's values
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) {
return ArrayIterator.newArrayValueIterator(self);
}
@Override @Override
protected ScriptObject getPrototype(final Global global) { protected ScriptObject getPrototype(final Global global) {
return global.getInt32ArrayPrototype(); return global.getInt32ArrayPrototype();

View File

@ -224,6 +224,17 @@ public final class NativeInt8Array extends ArrayBufferView {
return (NativeInt8Array)ArrayBufferView.subarrayImpl(self, begin, end); return (NativeInt8Array)ArrayBufferView.subarrayImpl(self, begin, end);
} }
/**
* ECMA 6 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )
*
* @param self the self reference
* @return an iterator over the array's values
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) {
return ArrayIterator.newArrayValueIterator(self);
}
@Override @Override
protected ScriptObject getPrototype(final Global global) { protected ScriptObject getPrototype(final Global global) {
return global.getInt8ArrayPrototype(); return global.getInt8ArrayPrototype();

View File

@ -229,6 +229,17 @@ public final class NativeUint16Array extends ArrayBufferView {
return (NativeUint16Array)ArrayBufferView.subarrayImpl(self, begin, end); return (NativeUint16Array)ArrayBufferView.subarrayImpl(self, begin, end);
} }
/**
* ECMA 6 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )
*
* @param self the self reference
* @return an iterator over the array's values
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) {
return ArrayIterator.newArrayValueIterator(self);
}
@Override @Override
protected ScriptObject getPrototype(final Global global) { protected ScriptObject getPrototype(final Global global) {
return global.getUint16ArrayPrototype(); return global.getUint16ArrayPrototype();

View File

@ -244,6 +244,17 @@ public final class NativeUint32Array extends ArrayBufferView {
return (NativeUint32Array)ArrayBufferView.subarrayImpl(self, begin, end); return (NativeUint32Array)ArrayBufferView.subarrayImpl(self, begin, end);
} }
/**
* ECMA 6 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )
*
* @param self the self reference
* @return an iterator over the array's values
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) {
return ArrayIterator.newArrayValueIterator(self);
}
@Override @Override
protected ScriptObject getPrototype(final Global global) { protected ScriptObject getPrototype(final Global global) {
return global.getUint32ArrayPrototype(); return global.getUint32ArrayPrototype();

View File

@ -230,6 +230,17 @@ public final class NativeUint8Array extends ArrayBufferView {
return (NativeUint8Array)ArrayBufferView.subarrayImpl(self, begin, end); return (NativeUint8Array)ArrayBufferView.subarrayImpl(self, begin, end);
} }
/**
* ECMA 6 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )
*
* @param self the self reference
* @return an iterator over the array's values
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) {
return ArrayIterator.newArrayValueIterator(self);
}
@Override @Override
protected ScriptObject getPrototype(final Global global) { protected ScriptObject getPrototype(final Global global) {
return global.getUint8ArrayPrototype(); return global.getUint8ArrayPrototype();

View File

@ -266,6 +266,17 @@ public final class NativeUint8ClampedArray extends ArrayBufferView {
return (NativeUint8ClampedArray)ArrayBufferView.subarrayImpl(self, begin, end); return (NativeUint8ClampedArray)ArrayBufferView.subarrayImpl(self, begin, end);
} }
/**
* ECMA 6 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )
*
* @param self the self reference
* @return an iterator over the array's values
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) {
return ArrayIterator.newArrayValueIterator(self);
}
@Override @Override
protected ScriptObject getPrototype(final Global global) { protected ScriptObject getPrototype(final Global global) {
return global.getUint8ClampedArrayPrototype(); return global.getUint8ClampedArrayPrototype();

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* JDK-8168140: TypedArrays should implement ES6 iterator protocol
*
* @test
* @run
* @option --language=es6
*/
let TypedArrayTypes = [
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array
];
let arrays = [];
let sum = 0;
TypedArrayTypes.forEach(function(ArrayType) {
var a = new ArrayType(10);
for (let i = 0; i < a.length; i++) {
a[i] = i;
}
arrays.push(a);
});
Assert.assertTrue(arrays.length === 9);
for (let array of arrays) {
Assert.assertTrue(array.length === 10);
let count = 0;
for (let value of array) {
Assert.assertTrue(value === count++);
sum += value;
}
}
Assert.assertTrue(sum === 405);