8011964: need indexed access to externally-managed ByteBuffer

Reviewed-by: lagergren, hannesw
This commit is contained in:
Athijegannathan Sundararajan 2014-02-14 19:02:02 +05:30
parent 615623c473
commit 1dd97ac6e7
8 changed files with 360 additions and 0 deletions

View File

@ -25,6 +25,7 @@
package jdk.nashorn.api.scripting; package jdk.nashorn.api.scripting;
import java.nio.ByteBuffer;
import java.security.AccessControlContext; import java.security.AccessControlContext;
import java.security.AccessController; import java.security.AccessController;
import java.security.Permissions; import java.security.Permissions;
@ -41,6 +42,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import javax.script.Bindings; import javax.script.Bindings;
import jdk.nashorn.internal.runtime.arrays.ArrayData;
import jdk.nashorn.internal.runtime.ConsString; import jdk.nashorn.internal.runtime.ConsString;
import jdk.nashorn.internal.runtime.Context; import jdk.nashorn.internal.runtime.Context;
import jdk.nashorn.internal.runtime.GlobalObject; import jdk.nashorn.internal.runtime.GlobalObject;
@ -259,6 +261,22 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
}); });
} }
/**
* Nashorn extension: setIndexedPropertiesToExternalArrayData.
* set indexed properties be exposed from a given nio ByteBuffer.
*
* @param buf external buffer - should be a nio ByteBuffer
*/
public void setIndexedPropertiesToExternalArrayData(final ByteBuffer buf) {
inGlobal(new Callable<Void>() {
@Override public Void call() {
sobj.setArray(ArrayData.allocate(buf));
return null;
}
});
}
@Override @Override
public boolean isInstance(final Object obj) { public boolean isInstance(final Object obj) {
if (! (obj instanceof ScriptObjectMirror)) { if (! (obj instanceof ScriptObjectMirror)) {

View File

@ -31,6 +31,7 @@ import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType; import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
@ -58,6 +59,7 @@ import jdk.nashorn.internal.runtime.Property;
import jdk.nashorn.internal.runtime.PropertyMap; import jdk.nashorn.internal.runtime.PropertyMap;
import jdk.nashorn.internal.runtime.ScriptObject; import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime; import jdk.nashorn.internal.runtime.ScriptRuntime;
import jdk.nashorn.internal.runtime.arrays.ArrayData;
import jdk.nashorn.internal.runtime.linker.Bootstrap; import jdk.nashorn.internal.runtime.linker.Bootstrap;
import jdk.nashorn.internal.runtime.linker.InvokeByName; import jdk.nashorn.internal.runtime.linker.InvokeByName;
import jdk.nashorn.internal.runtime.linker.NashornBeansLinker; import jdk.nashorn.internal.runtime.linker.NashornBeansLinker;
@ -100,6 +102,27 @@ public final class NativeObject {
return typeError("not.an.object", ScriptRuntime.safeToString(obj)); return typeError("not.an.object", ScriptRuntime.safeToString(obj));
} }
/**
* Nashorn extension: setIndexedPropertiesToExternalArrayData
*
* @param self self reference
* @param obj object whose index properties are backed by buffer
* @param buf external buffer - should be a nio ByteBuffer
* @return the 'obj' object
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object setIndexedPropertiesToExternalArrayData(final Object self, final Object obj, final Object buf) {
Global.checkObject(obj);
final ScriptObject sobj = (ScriptObject)obj;
if (buf instanceof ByteBuffer) {
sobj.setArray(ArrayData.allocate((ByteBuffer)buf));
} else {
throw typeError("not.a.bytebuffer", "setIndexedPropertiesToExternalArrayData's buf argument");
}
return sobj;
}
/** /**
* ECMA 15.2.3.2 Object.getPrototypeOf ( O ) * ECMA 15.2.3.2 Object.getPrototypeOf ( O )
* *

View File

@ -26,6 +26,7 @@
package jdk.nashorn.internal.runtime.arrays; package jdk.nashorn.internal.runtime.arrays;
import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandle;
import java.nio.ByteBuffer;
import jdk.nashorn.internal.runtime.GlobalObject; import jdk.nashorn.internal.runtime.GlobalObject;
import jdk.nashorn.internal.runtime.JSType; import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.PropertyDescriptor; import jdk.nashorn.internal.runtime.PropertyDescriptor;
@ -143,6 +144,16 @@ public abstract class ArrayData {
return new ObjectArrayData(array, array.length); return new ObjectArrayData(array, array.length);
} }
/**
* Allocate an ArrayData wrapping a given nio ByteBuffer
*
* @param buf the nio ByteBuffer to wrap
* @return the ArrayData
*/
public static ArrayData allocate(final ByteBuffer buf) {
return new ByteBufferArrayData((ByteBuffer)buf);
}
/** /**
* Apply a freeze filter to an ArrayData. * Apply a freeze filter to an ArrayData.
* *

View File

@ -0,0 +1,203 @@
/*
* 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
package jdk.nashorn.internal.runtime.arrays;
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
import java.nio.ByteBuffer;
import jdk.nashorn.internal.runtime.GlobalObject;
import jdk.nashorn.internal.runtime.PropertyDescriptor;
import jdk.nashorn.internal.runtime.ScriptRuntime;
/**
* Implementation of {@link ArrayData} that wraps a nio ByteBuffer
*/
final class ByteBufferArrayData extends ArrayData {
private final ByteBuffer buf;
ByteBufferArrayData(final int length) {
super(length);
this.buf = ByteBuffer.allocateDirect(length);
}
/**
* Constructor
*
* @param buf ByteBuffer to create array data with.
*/
ByteBufferArrayData(final ByteBuffer buf) {
super(buf.capacity());
this.buf = buf;
}
/**
* Returns property descriptor for element at a given index
*
* @param global the global object
* @param index the index
*
* @return property descriptor for element
*/
public PropertyDescriptor getDescriptor(final GlobalObject global, final int index) {
// make the index properties not configurable
return global.newDataDescriptor(getObject(index), false, true, true);
}
@Override
public ArrayData copy() {
throw unsupported("copy");
}
@Override
public Object[] asObjectArray() {
throw unsupported("asObjectArray");
}
@Override
public void setLength(final long length) {
throw new UnsupportedOperationException("setLength");
}
@Override
public void shiftLeft(int by) {
throw unsupported("shiftLeft");
}
@Override
public ArrayData shiftRight(int by) {
throw unsupported("shiftRight");
}
@Override
public ArrayData ensure(long safeIndex) {
if (safeIndex < buf.capacity()) {
return this;
}
throw unsupported("ensure");
}
@Override
public ArrayData shrink(long newLength) {
throw unsupported("shrink");
}
@Override
public ArrayData set(int index, Object value, boolean strict) {
if (value instanceof Number) {
buf.put(index, ((Number)value).byteValue());
return this;
}
throw typeError("not.a.number", ScriptRuntime.safeToString(value));
}
@Override
public ArrayData set(int index, int value, boolean strict) {
buf.put(index, (byte)value);
return this;
}
@Override
public ArrayData set(int index, long value, boolean strict) {
buf.put(index, (byte)value);
return this;
}
@Override
public ArrayData set(int index, double value, boolean strict) {
buf.put(index, (byte)value);
return this;
}
@Override
public int getInt(int index) {
return 0x0ff & buf.get(index);
}
@Override
public long getLong(int index) {
return 0x0ff & buf.get(index);
}
@Override
public double getDouble(int index) {
return 0x0ff & buf.get(index);
}
@Override
public Object getObject(int index) {
return (int)(0x0ff & buf.get(index));
}
@Override
public boolean has(int index) {
return index > -1 && index < buf.capacity();
}
@Override
public boolean canDelete(final int index, final boolean strict) {
return false;
}
@Override
public boolean canDelete(final long fromIndex, final long toIndex, final boolean strict) {
return false;
}
@Override
public ArrayData delete(int index) {
throw unsupported("delete");
}
@Override
public ArrayData delete(long fromIndex, long toIndex) {
throw unsupported("delete");
}
@Override
public ArrayData push(final boolean strict, final Object... items) {
throw unsupported("push");
}
@Override
public Object pop() {
throw unsupported("pop");
}
@Override
public ArrayData slice(long from, long to) {
throw unsupported("slice");
}
@Override
public ArrayData convert(final Class<?> type) {
throw unsupported("convert");
}
private UnsupportedOperationException unsupported(final String method) {
return new UnsupportedOperationException(method);
}
}

View File

@ -78,6 +78,7 @@ type.error.not.a.string={0} is not a String
type.error.not.a.function={0} is not a function 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.constructor={0} is not a constructor function
type.error.not.a.file={0} is not a File type.error.not.a.file={0} is not a File
type.error.not.a.bytebuffer={0} is not a java.nio.ByteBuffer
# operations not permitted on undefined # operations not permitted on undefined
type.error.cant.call.undefined=Cannot call undefined type.error.cant.call.undefined=Cannot call undefined

View File

@ -0,0 +1,60 @@
/*
* 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-8011964: need indexed access to externally-managed ByteBuffer
*
* @test
* @run
*/
var ByteBuffer = Java.type("java.nio.ByteBuffer");
var buf = ByteBuffer.allocate(5);
var obj = {}
Object.setIndexedPropertiesToExternalArrayData(obj, buf);
obj[0] = 'A'.charCodeAt(0);
obj[1] = 'B'.charCodeAt(0);
obj[2] = 'C'.charCodeAt(0);
obj[3] = 'D'.charCodeAt(0);
obj[4] = 'E'.charCodeAt(0);
for (var i = 0; i < buf.capacity(); i++) {
print("obj[" + i + "] = " + obj[i]);
print("buf.get(" + i + ") = " + buf.get(i));
}
var arr = [];
Object.setIndexedPropertiesToExternalArrayData(arr, buf);
obj[0] = 'a'.charCodeAt(0);
obj[1] = 'b'.charCodeAt(0);
obj[2] = 'c'.charCodeAt(0);
obj[3] = 'd'.charCodeAt(0);
obj[4] = 'e'.charCodeAt(0);
for (var i in arr) {
print("arr[" + i + "] = " + arr[i]);
print("buf.get(" + i + ") = " + buf.get(i));
}

View File

@ -0,0 +1,20 @@
obj[0] = 65
buf.get(0) = 65
obj[1] = 66
buf.get(1) = 66
obj[2] = 67
buf.get(2) = 67
obj[3] = 68
buf.get(3) = 68
obj[4] = 69
buf.get(4) = 69
arr[0] = 97
buf.get(0) = 97
arr[1] = 98
buf.get(1) = 98
arr[2] = 99
buf.get(2) = 99
arr[3] = 100
buf.get(3) = 100
arr[4] = 101
buf.get(4) = 101

View File

@ -25,6 +25,7 @@
package jdk.nashorn.api.scripting; package jdk.nashorn.api.scripting;
import java.nio.ByteBuffer;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -229,6 +230,29 @@ public class ScriptObjectMirrorTest {
assertTrue(newObj instanceof ScriptObjectMirror); assertTrue(newObj instanceof ScriptObjectMirror);
} }
@Test
public void indexPropertiesExternalBufferTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final ScriptObjectMirror obj = (ScriptObjectMirror)e.eval("var obj = {}; obj");
final ByteBuffer buf = ByteBuffer.allocate(5);
int i;
for (i = 0; i < 5; i++) {
buf.put(i, (byte)(i+10));
}
obj.setIndexedPropertiesToExternalArrayData(buf);
for (i = 0; i < 5; i++) {
assertEquals((byte)(i+10), ((Number)e.eval("obj[" + i + "]")).byteValue());
}
e.eval("for (i = 0; i < 5; i++) obj[i] = 0");
for (i = 0; i < 5; i++) {
assertEquals((byte)0, ((Number)e.eval("obj[" + i + "]")).byteValue());
assertEquals((byte)0, buf.get(i));
}
}
@Test @Test
public void conversionTest() throws ScriptException { public void conversionTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngineManager m = new ScriptEngineManager();