changeset 30830:9d3a0827accd

8036743: need ArrayBuffer constructor with specified data Reviewed-by: attila, hannesw, lagergren
author sundar
date Tue, 26 May 2015 13:49:45 +0530
parents ff3fc75f3214
children f733f1de0166
files nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArrayBuffer.java nashorn/test/script/basic/JDK-8036743.js nashorn/test/script/basic/JDK-8036743.js.EXPECTED
diffstat 3 files changed, 112 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArrayBuffer.java	Wed Jul 05 20:35:22 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArrayBuffer.java	Tue May 26 13:49:45 2015 +0530
@@ -106,7 +106,12 @@
             return new NativeArrayBuffer(0);
         }
 
-        return new NativeArrayBuffer(JSType.toInt32(args[0]));
+        final Object arg0 = args[0];
+        if (arg0 instanceof ByteBuffer) {
+            return new NativeArrayBuffer((ByteBuffer)arg0);
+        } else {
+            return new NativeArrayBuffer(JSType.toInt32(arg0));
+        }
     }
 
     private static ByteBuffer cloneBuffer(final ByteBuffer original, final int begin, final int end) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8036743.js	Tue May 26 13:49:45 2015 +0530
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2015 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-8036743: need ArrayBuffer constructor with specified data
+ *
+ * @test
+ * @run
+ */
+
+var ByteArray = Java.type("byte[]");
+var ByteBuffer = Java.type("java.nio.ByteBuffer");
+
+var ba = new ByteArray(4);
+// use constructor that accepts ByteBuffer as first arg.
+var abuf = new ArrayBuffer(ByteBuffer.wrap(ba));
+print("abuf.byteLength = " + abuf.byteLength);
+var view = new DataView(abuf);
+
+function setAndPrint(value, endian) {
+    view.setInt32(0, value, endian);
+    print("Little endian? " + endian);
+    print("view[0] = " + view.getInt32(0, endian));
+    print("ba[0] = " + ba[0]);
+    print("ba[1] = " + ba[1]);
+    print("ba[2] = " + ba[2]);
+    print("ba[3] = " + ba[3]);
+}
+
+setAndPrint(42, true);
+setAndPrint(42, false);
+setAndPrint(java.lang.Byte.MAX_VALUE, true);
+setAndPrint(java.lang.Byte.MAX_VALUE, false);
+setAndPrint(java.lang.Short.MAX_VALUE, true);
+setAndPrint(java.lang.Short.MAX_VALUE, false);
+setAndPrint(java.lang.Integer.MAX_VALUE, true);
+setAndPrint(java.lang.Integer.MAX_VALUE, false);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8036743.js.EXPECTED	Tue May 26 13:49:45 2015 +0530
@@ -0,0 +1,49 @@
+abuf.byteLength = 4
+Little endian? true
+view[0] = 42
+ba[0] = 42
+ba[1] = 0
+ba[2] = 0
+ba[3] = 0
+Little endian? false
+view[0] = 42
+ba[0] = 0
+ba[1] = 0
+ba[2] = 0
+ba[3] = 42
+Little endian? true
+view[0] = 127
+ba[0] = 127
+ba[1] = 0
+ba[2] = 0
+ba[3] = 0
+Little endian? false
+view[0] = 127
+ba[0] = 0
+ba[1] = 0
+ba[2] = 0
+ba[3] = 127
+Little endian? true
+view[0] = 32767
+ba[0] = -1
+ba[1] = 127
+ba[2] = 0
+ba[3] = 0
+Little endian? false
+view[0] = 32767
+ba[0] = 0
+ba[1] = 0
+ba[2] = 127
+ba[3] = -1
+Little endian? true
+view[0] = 2147483647
+ba[0] = -1
+ba[1] = -1
+ba[2] = -1
+ba[3] = 127
+Little endian? false
+view[0] = 2147483647
+ba[0] = 127
+ba[1] = -1
+ba[2] = -1
+ba[3] = -1