8023784: Object.prototype.toString should contain the class name for all instances
Reviewed-by: lagergren, jlaskey
--- a/nashorn/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java Tue Aug 27 18:57:50 2013 +0530
@@ -391,6 +391,15 @@
}
/**
+ * ECMA [[Class]] property
+ *
+ * @return ECMA [[Class]] property value of this object
+ */
+ public String getClassName() {
+ return sobj.getClassName();
+ }
+
+ /**
* ECMA 8.12.1 [[GetOwnProperty]] (P)
*
* @param key property key
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeArrayBuffer.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeArrayBuffer.java Tue Aug 27 18:57:50 2013 +0530
@@ -73,6 +73,11 @@
this(Arrays.copyOfRange(other.buffer, begin, end));
}
+ @Override
+ public String getClassName() {
+ return "ArrayBuffer";
+ }
+
@Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE)
public static Object byteLength(final Object self) {
return ((NativeArrayBuffer)self).buffer.length;
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeFloat32Array.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeFloat32Array.java Tue Aug 27 18:57:50 2013 +0530
@@ -145,6 +145,11 @@
}
@Override
+ public String getClassName() {
+ return "Float32Array";
+ }
+
+ @Override
protected Factory factory() {
return FACTORY;
}
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeFloat64Array.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeFloat64Array.java Tue Aug 27 18:57:50 2013 +0530
@@ -155,6 +155,11 @@
}
@Override
+ public String getClassName() {
+ return "Float64Array";
+ }
+
+ @Override
protected Factory factory() {
return FACTORY;
}
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeInt16Array.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeInt16Array.java Tue Aug 27 18:57:50 2013 +0530
@@ -109,6 +109,11 @@
}
@Override
+ public String getClassName() {
+ return "Int16Array";
+ }
+
+ @Override
protected Factory factory() {
return FACTORY;
}
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeInt32Array.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeInt32Array.java Tue Aug 27 18:57:50 2013 +0530
@@ -112,6 +112,11 @@
}
@Override
+ public String getClassName() {
+ return "Int32Array";
+ }
+
+ @Override
protected Factory factory() {
return FACTORY;
}
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeInt8Array.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeInt8Array.java Tue Aug 27 18:57:50 2013 +0530
@@ -102,6 +102,11 @@
}
@Override
+ public String getClassName() {
+ return "Int8Array";
+ }
+
+ @Override
protected Factory factory() {
return FACTORY;
}
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeUint16Array.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeUint16Array.java Tue Aug 27 18:57:50 2013 +0530
@@ -108,6 +108,11 @@
}
@Override
+ public String getClassName() {
+ return "Uint16Array";
+ }
+
+ @Override
protected Factory factory() {
return FACTORY;
}
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeUint32Array.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeUint32Array.java Tue Aug 27 18:57:50 2013 +0530
@@ -127,6 +127,11 @@
}
@Override
+ public String getClassName() {
+ return "Uint32Array";
+ }
+
+ @Override
protected Factory factory() {
return FACTORY;
}
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeUint8Array.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeUint8Array.java Tue Aug 27 18:57:50 2013 +0530
@@ -101,6 +101,11 @@
}
@Override
+ public String getClassName() {
+ return "Uint8Array";
+ }
+
+ @Override
protected Factory factory() {
return FACTORY;
}
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java Tue Aug 27 18:57:50 2013 +0530
@@ -118,6 +118,11 @@
}
@Override
+ public String getClassName() {
+ return "Uint8ClampedArray";
+ }
+
+ @Override
protected Factory factory() {
return FACTORY;
}
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptRuntime.java Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptRuntime.java Tue Aug 27 18:57:50 2013 +0530
@@ -190,6 +190,8 @@
case FUNCTION:
if (self instanceof ScriptObject) {
className = ((ScriptObject)self).getClassName();
+ } else if (self instanceof ScriptObjectMirror) {
+ className = ((ScriptObjectMirror)self).getClassName();
} else {
className = self.getClass().getName();
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8023784.js Tue Aug 27 18:57:50 2013 +0530
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2010, 2013, 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-8023784: Object.prototype.toString should contain the class name for all instances
+ *
+ * @test
+ * @run
+ */
+
+// two parts to this bug -- typed array don't have proper [[Class]] property
+
+print(Object.prototype.toString.call(new ArrayBuffer(1)));
+print(Object.prototype.toString.call(new Int8Array(1)));
+print(Object.prototype.toString.call(new Int16Array(1)));
+print(Object.prototype.toString.call(new Int32Array(1)));
+print(Object.prototype.toString.call(new Uint8Array(1)));
+print(Object.prototype.toString.call(new Uint8ClampedArray(1)));
+print(Object.prototype.toString.call(new Uint16Array(1)));
+print(Object.prototype.toString.call(new Uint32Array(1)));
+print(Object.prototype.toString.call(new Float32Array(1)));
+print(Object.prototype.toString.call(new Float64Array(1)));
+
+// second part is that Object.prototype.toString does not handle mirror
+// in the manner expected.
+
+var global = loadWithNewGlobal({
+ name: "test",
+ script: "this"
+});
+
+print(Object.prototype.toString.call(new global.Object()));
+print(Object.prototype.toString.call(new global.Array()));
+print(Object.prototype.toString.call(new global.RegExp()));
+print(Object.prototype.toString.call(new global.Error("error!")));
+print(Object.prototype.toString.call(global.Object));
+print(Object.prototype.toString.call(new global.ArrayBuffer(1)));
+print(Object.prototype.toString.call(new global.Int8Array(1)));
+print(Object.prototype.toString.call(new global.Int16Array(1)));
+print(Object.prototype.toString.call(new global.Int32Array(1)));
+print(Object.prototype.toString.call(new global.Uint8Array(1)));
+print(Object.prototype.toString.call(new global.Uint8ClampedArray(1)));
+print(Object.prototype.toString.call(new global.Uint16Array(1)));
+print(Object.prototype.toString.call(new global.Uint32Array(1)));
+print(Object.prototype.toString.call(new global.Float32Array(1)));
+print(Object.prototype.toString.call(new global.Float64Array(1)));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8023784.js.EXPECTED Tue Aug 27 18:57:50 2013 +0530
@@ -0,0 +1,25 @@
+[object ArrayBuffer]
+[object Int8Array]
+[object Int16Array]
+[object Int32Array]
+[object Uint8Array]
+[object Uint8ClampedArray]
+[object Uint16Array]
+[object Uint32Array]
+[object Float32Array]
+[object Float64Array]
+[object Object]
+[object Array]
+[object RegExp]
+[object Error]
+[object Function]
+[object ArrayBuffer]
+[object Int8Array]
+[object Int16Array]
+[object Int32Array]
+[object Uint8Array]
+[object Uint8ClampedArray]
+[object Uint16Array]
+[object Uint32Array]
+[object Float32Array]
+[object Float64Array]
--- a/nashorn/test/script/basic/NASHORN-377.js.EXPECTED Tue Aug 27 13:17:00 2013 +0200
+++ b/nashorn/test/script/basic/NASHORN-377.js.EXPECTED Tue Aug 27 18:57:50 2013 +0530
@@ -1,5 +1,5 @@
8 8 true undefined
-[object Object] [object Object] [object Object]
+[object ArrayBuffer] [object ArrayBuffer] [object Int8Array]
0 8 8 1
0 8 8 1
0 8 8 1