提交 7224aa68 编写于 作者: S sundar

8015958: DataView constructor is not defined

Reviewed-by: attila, hannesw, lagergren
上级 abc57db2
......@@ -226,6 +226,10 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
@Property(name = "ArrayBuffer", attributes = Attribute.NOT_ENUMERABLE)
public volatile Object arrayBuffer;
/** DataView object */
@Property(name = "DataView", attributes = Attribute.NOT_ENUMERABLE)
public volatile Object dataView;
/** TypedArray (int8) */
@Property(name = "Int8Array", attributes = Attribute.NOT_ENUMERABLE)
public volatile Object int8Array;
......@@ -352,6 +356,7 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
private ScriptObject builtinJavaImporter;
private ScriptObject builtinJavaApi;
private ScriptObject builtinArrayBuffer;
private ScriptObject builtinDataView;
private ScriptObject builtinInt8Array;
private ScriptObject builtinUint8Array;
private ScriptObject builtinUint8ClampedArray;
......@@ -866,6 +871,10 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
return ScriptFunction.getPrototype(builtinArrayBuffer);
}
ScriptObject getDataViewPrototype() {
return ScriptFunction.getPrototype(builtinDataView);
}
ScriptObject getInt8ArrayPrototype() {
return ScriptFunction.getPrototype(builtinInt8Array);
}
......@@ -1634,6 +1643,7 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
private void initTypedArray() {
this.builtinArrayBuffer = initConstructor("ArrayBuffer");
this.builtinDataView = initConstructor("DataView");
this.builtinInt8Array = initConstructor("Int8Array");
this.builtinUint8Array = initConstructor("Uint8Array");
this.builtinUint8ClampedArray = initConstructor("Uint8ClampedArray");
......@@ -1674,6 +1684,7 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
this.typeError = this.builtinTypeError;
this.uriError = this.builtinURIError;
this.arrayBuffer = this.builtinArrayBuffer;
this.dataView = this.builtinDataView;
this.int8Array = this.builtinInt8Array;
this.uint8Array = this.builtinUint8Array;
this.uint8ClampedArray = this.builtinUint8ClampedArray;
......
......@@ -25,6 +25,7 @@
package jdk.nashorn.internal.objects;
import java.nio.ByteBuffer;
import java.util.Arrays;
import jdk.nashorn.internal.objects.annotations.Attribute;
import jdk.nashorn.internal.objects.annotations.Constructor;
......@@ -128,4 +129,16 @@ final class NativeArrayBuffer extends ScriptObject {
public int getByteLength() {
return buffer.length;
}
ByteBuffer getBuffer() {
return ByteBuffer.wrap(buffer);
}
ByteBuffer getBuffer(final int offset) {
return ByteBuffer.wrap(buffer, offset, buffer.length - offset);
}
ByteBuffer getBuffer(final int offset, final int length) {
return ByteBuffer.wrap(buffer, offset, length);
}
}
此差异已折叠。
......@@ -649,7 +649,7 @@ public final class Context {
* Checks that the given Class can be accessed from no permissions context.
*
* @param clazz Class object
* @throw SecurityException if not accessible
* @throws SecurityException if not accessible
*/
public static void checkPackageAccess(final Class<?> clazz) {
final SecurityManager sm = System.getSecurityManager();
......@@ -666,7 +666,7 @@ public final class Context {
* Checks that the given package name can be accessed from no permissions context.
*
* @param pkgName package name
* @throw SecurityException if not accessible
* @throws SecurityException if not accessible
*/
public static void checkPackageAccess(final String pkgName) {
final SecurityManager sm = System.getSecurityManager();
......
......@@ -79,6 +79,7 @@ type.error.not.a.function={0} is not a function
type.error.not.a.constructor={0} is not a constructor function
type.error.not.a.file={0} is not a File
type.error.not.a.bytebuffer={0} is not a java.nio.ByteBuffer
type.error.not.an.arraybuffer.in.dataview=First arg to DataView constructor must be an ArrayBuffer
# operations not permitted on undefined
type.error.cant.call.undefined=Cannot call undefined
......@@ -137,6 +138,9 @@ type.error.no.method.matches.args=Can not invoke method {0} with the passed argu
type.error.method.not.constructor=Java method {0} can't be used as a constructor.
type.error.env.not.object=$ENV must be an Object.
type.error.unsupported.java.to.type=Unsupported Java.to target type {0}.
range.error.dataview.constructor.offset=Wrong offset or length in DataView constructor
range.error.dataview.offset=Offset is outside the bounds of the DataView
range.error.inappropriate.array.length=inappropriate array length: {0}
range.error.inappropriate.array.buffer.length=inappropriate array buffer length: {0}
range.error.invalid.fraction.digits=fractionDigits argument to {0} must be in [0, 20]
......
/*
* Copyright (c) 2014, 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-8015958: DataView constructor is not defined
*
* @test
* @run
*/
// set/get endianess checks
var buffer = new ArrayBuffer(4);
var dv = new DataView(buffer);
// write (default) big endian, read big/little endian
dv.setUint16(0, 0xABCD);
Assert.assertEquals(dv.getUint16(0), 0xABCD);
Assert.assertEquals(dv.getUint16(0, false), 0xABCD);
Assert.assertEquals(dv.getUint16(0, true), 0xCDAB);
// write little endian, read big/little endian
dv.setUint16(0, 0xABCD, true);
Assert.assertEquals(dv.getUint16(0), 0xCDAB);
Assert.assertEquals(dv.getUint16(0, false), 0xCDAB);
Assert.assertEquals(dv.getUint16(0, true), 0xABCD);
// write explicit big endian, read big/little endian
dv.setUint16(0, 0xABCD, false);
Assert.assertEquals(dv.getUint16(0), 0xABCD);
Assert.assertEquals(dv.getUint16(0, false), 0xABCD);
Assert.assertEquals(dv.getUint16(0, true), 0xCDAB);
// write (default) big endian, read big/little endian
dv.setUint32(0, 0xABCDEF89);
Assert.assertEquals(dv.getUint32(0), 0xABCDEF89);
Assert.assertEquals(dv.getUint32(0, false), 0xABCDEF89);
Assert.assertEquals(dv.getUint32(0, true), 0x89EFCDAB);
// write little endian, read big/little endian
dv.setUint32(0, 0xABCDEF89, true);
Assert.assertEquals(dv.getUint32(0), 0x89EFCDAB);
Assert.assertEquals(dv.getUint32(0, false), 0x89EFCDAB);
Assert.assertEquals(dv.getUint32(0, true), 0xABCDEF89);
// write explicit big endian, read big/little endian
dv.setUint32(0, 0xABCDEF89, false);
Assert.assertEquals(dv.getUint32(0), 0xABCDEF89);
Assert.assertEquals(dv.getUint32(0, false), 0xABCDEF89);
Assert.assertEquals(dv.getUint32(0, true), 0x89EFCDAB);
/*
* Copyright (c) 2014, 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-8015958: DataView constructor is not defined
*
* @test
* @run
*/
// checking get/set of values of various types
// Also basic endianess check.
var Float = Java.type("java.lang.Float");
var Double = Java.type("java.lang.Double");
var DOUBLE_MIN = Double.MIN_VALUE;
var DOUBLE_MIN_NORMAL = Double.MIN_NORMAL;
var FLOAT_MIN = Float.MIN_VALUE;
var FLOAT_MIN_NORMAL = Float.MIN_NORMAL;
var buffer = new ArrayBuffer(12);
var dv = new DataView(buffer);
dv.setInt8(1, 123);
Assert.assertEquals(dv.getInt8(1), 123);
dv.setInt8(1, 123, true);
Assert.assertEquals(dv.getInt8(1, true), 123);
dv.setUint8(1, 255);
Assert.assertEquals(dv.getUint8(1), 255);
dv.setUint8(1, 255, true);
Assert.assertEquals(dv.getUint8(1, true), 255);
dv.setInt16(1, 1234);
Assert.assertEquals(dv.getInt16(1), 1234);
dv.setInt16(1, 1234, true);
Assert.assertEquals(dv.getInt16(1, true), 1234);
dv.setUint16(1, 65535);
Assert.assertEquals(dv.getUint16(1), 65535);
dv.setUint16(1, 65535, true);
Assert.assertEquals(dv.getUint16(1, true), 65535);
dv.setInt32(1, 1234);
Assert.assertEquals(dv.getInt32(1), 1234);
dv.setInt32(1, 1234, true);
Assert.assertEquals(dv.getInt32(1, true), 1234);
dv.setUint32(1, 4294967295);
Assert.assertEquals(dv.getUint32(1), 4294967295);
dv.setUint32(1, 4294967295, true);
Assert.assertEquals(dv.getUint32(1, true), 4294967295);
dv.setFloat64(1, Math.PI);
Assert.assertEquals(dv.getFloat64(1), Math.PI, DOUBLE_MIN);
dv.setFloat64(1, Math.PI, true);
Assert.assertEquals(dv.getFloat64(1, true), Math.PI, DOUBLE_MIN);
dv.setFloat64(1, DOUBLE_MIN_NORMAL);
Assert.assertEquals(dv.getFloat64(1), DOUBLE_MIN_NORMAL, DOUBLE_MIN);
dv.setFloat64(1, DOUBLE_MIN_NORMAL, true);
Assert.assertEquals(dv.getFloat64(1, true), DOUBLE_MIN_NORMAL, DOUBLE_MIN);
dv.setFloat32(1, 1.414);
Assert["assertEquals(float, float, float)"](dv.getFloat32(1), 1.414, FLOAT_MIN);
dv.setFloat32(1, 1.414, true);
Assert["assertEquals(float, float, float)"](dv.getFloat32(1, true), 1.414, FLOAT_MIN);
dv.setFloat32(1, FLOAT_MIN_NORMAL);
Assert["assertEquals(float, float, float)"](dv.getFloat32(1), FLOAT_MIN_NORMAL, FLOAT_MIN);
dv.setFloat32(1, FLOAT_MIN_NORMAL, true);
Assert["assertEquals(float, float, float)"](dv.getFloat32(1, true), FLOAT_MIN_NORMAL, FLOAT_MIN);
/*
* Copyright (c) 2014, 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-8015958: DataView constructor is not defined
*
* @test
* @run
*/
// basic DataView constructor checks.
// check ArrayBufferView property values of DataView instance
function check(dv, buf, offset, length) {
if (dv.buffer !== buf) {
fail("DataView.buffer is wrong");
}
if (dv.byteOffset != offset) {
fail("DataView.byteOffset = " + dv.byteOffset + ", expected " + offset);
}
if (dv.byteLength != length) {
fail("DataView.byteLength = " + dv.byteLength + ", expected " + length);
}
}
var buffer = new ArrayBuffer(12);
check(new DataView(buffer), buffer, 0, 12);
check(new DataView(buffer, 2), buffer, 2, 10);
check(new DataView(buffer, 4, 8), buffer, 4, 8);
// make sure expected error is thrown
function checkError(callback, ErrorType) {
try {
callback();
fail("Should have thrown " + ErrorType.name);
} catch (e) {
if (! (e instanceof ErrorType)) {
fail("Expected " + ErrorType.name + " got " + e);
}
}
}
// non ArrayBuffer as first arg
checkError(function() { new DataView(344) }, TypeError);
// illegal offset/length values
checkError(function() { new DataView(buffer, -1) }, RangeError);
checkError(function() { new DataView(buffer, 15) }, RangeError);
checkError(function() { new DataView(buffer, 1, 32) }, RangeError);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册