changeset 1802:1272bfe44562

8168140: TypedArrays should implement ES6 iterator protocol Reviewed-by: sundar, lagergren
author hannesw
date Fri, 21 Oct 2016 10:02:09 +0200
parents ac561634520e
children d3f5d7311a1a
files src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/ArrayIterator.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArray.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat32Array.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat64Array.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt16Array.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt32Array.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt8Array.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint16Array.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint32Array.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8Array.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java test/script/basic/es6/JDK-8168140.js
diffstat 12 files changed, 184 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/ArrayIterator.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/ArrayIterator.java	Fri Oct 21 10:02:09 2016 +0200
@@ -47,13 +47,25 @@
     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$);
         this.iteratedObject = iteratedObject instanceof ScriptObject ? (ScriptObject) iteratedObject : null;
         this.iterationKind = iterationKind;
         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()
      *
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArray.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArray.java	Fri Oct 21 10:02:09 2016 +0200
@@ -1726,7 +1726,7 @@
      */
     @Function(attributes = Attribute.NOT_ENUMERABLE)
     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 @@
      */
     @Function(attributes = Attribute.NOT_ENUMERABLE)
     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 @@
      */
     @Function(attributes = Attribute.NOT_ENUMERABLE)
     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 @@
      */
     @Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
     public static Object getIterator(final Object self) {
-        return new ArrayIterator(Global.toObject(self), AbstractIterator.IterationKind.VALUE, Global.instance());
+        return ArrayIterator.newArrayValueIterator(self);
     }
 
     /**
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat32Array.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat32Array.java	Fri Oct 21 10:02:09 2016 +0200
@@ -232,6 +232,17 @@
         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
     protected ScriptObject getPrototype(final Global global) {
         return global.getFloat32ArrayPrototype();
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat64Array.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat64Array.java	Fri Oct 21 10:02:09 2016 +0200
@@ -232,6 +232,17 @@
         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
     protected ScriptObject getPrototype(final Global global) {
         return global.getFloat64ArrayPrototype();
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt16Array.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt16Array.java	Fri Oct 21 10:02:09 2016 +0200
@@ -225,6 +225,17 @@
         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
     protected ScriptObject getPrototype(final Global global) {
         return global.getInt16ArrayPrototype();
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt32Array.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt32Array.java	Fri Oct 21 10:02:09 2016 +0200
@@ -224,6 +224,17 @@
         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
     protected ScriptObject getPrototype(final Global global) {
         return global.getInt32ArrayPrototype();
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt8Array.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt8Array.java	Fri Oct 21 10:02:09 2016 +0200
@@ -224,6 +224,17 @@
         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
     protected ScriptObject getPrototype(final Global global) {
         return global.getInt8ArrayPrototype();
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint16Array.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint16Array.java	Fri Oct 21 10:02:09 2016 +0200
@@ -229,6 +229,17 @@
         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
     protected ScriptObject getPrototype(final Global global) {
         return global.getUint16ArrayPrototype();
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint32Array.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint32Array.java	Fri Oct 21 10:02:09 2016 +0200
@@ -244,6 +244,17 @@
         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
     protected ScriptObject getPrototype(final Global global) {
         return global.getUint32ArrayPrototype();
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8Array.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8Array.java	Fri Oct 21 10:02:09 2016 +0200
@@ -230,6 +230,17 @@
         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
     protected ScriptObject getPrototype(final Global global) {
         return global.getUint8ArrayPrototype();
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java	Fri Oct 21 09:43:11 2016 +0200
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java	Fri Oct 21 10:02:09 2016 +0200
@@ -266,6 +266,17 @@
         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
     protected ScriptObject getPrototype(final Global global) {
         return global.getUint8ClampedArrayPrototype();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/es6/JDK-8168140.js	Fri Oct 21 10:02:09 2016 +0200
@@ -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);