aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsundar <none@none>2014-02-14 19:02:02 +0530
committersundar <none@none>2014-02-14 19:02:02 +0530
commit583306df186bb54ee1201185d20a4a7075c6e065 (patch)
treeaf1a0387c4db5ba28a3fa19905356189e880fe22 /src
parent8e0f393275ddaad74c323d58caed97c93b7f91b1 (diff)
downloadnashorn-583306df186bb54ee1201185d20a4a7075c6e065.tar.gz
8011964: need indexed access to externally-managed ByteBuffer
Reviewed-by: lagergren, hannesw
Diffstat (limited to 'src')
-rw-r--r--src/jdk/nashorn/api/scripting/ScriptObjectMirror.java18
-rw-r--r--src/jdk/nashorn/internal/objects/NativeObject.java23
-rw-r--r--src/jdk/nashorn/internal/runtime/arrays/ArrayData.java11
-rw-r--r--src/jdk/nashorn/internal/runtime/arrays/ByteBufferArrayData.java203
-rw-r--r--src/jdk/nashorn/internal/runtime/resources/Messages.properties1
5 files changed, 256 insertions, 0 deletions
diff --git a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
index 01f72c00..7a9792df 100644
--- a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
+++ b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
@@ -25,6 +25,7 @@
package jdk.nashorn.api.scripting;
+import java.nio.ByteBuffer;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.Permissions;
@@ -41,6 +42,7 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import javax.script.Bindings;
+import jdk.nashorn.internal.runtime.arrays.ArrayData;
import jdk.nashorn.internal.runtime.ConsString;
import jdk.nashorn.internal.runtime.Context;
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
public boolean isInstance(final Object obj) {
if (! (obj instanceof ScriptObjectMirror)) {
diff --git a/src/jdk/nashorn/internal/objects/NativeObject.java b/src/jdk/nashorn/internal/objects/NativeObject.java
index d102c0fb..26335f63 100644
--- a/src/jdk/nashorn/internal/objects/NativeObject.java
+++ b/src/jdk/nashorn/internal/objects/NativeObject.java
@@ -31,6 +31,7 @@ import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
+import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
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.ScriptObject;
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.InvokeByName;
import jdk.nashorn.internal.runtime.linker.NashornBeansLinker;
@@ -101,6 +103,27 @@ public final class NativeObject {
}
/**
+ * 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 )
*
* @param self self reference
diff --git a/src/jdk/nashorn/internal/runtime/arrays/ArrayData.java b/src/jdk/nashorn/internal/runtime/arrays/ArrayData.java
index 5c0c0476..0aa1d9eb 100644
--- a/src/jdk/nashorn/internal/runtime/arrays/ArrayData.java
+++ b/src/jdk/nashorn/internal/runtime/arrays/ArrayData.java
@@ -26,6 +26,7 @@
package jdk.nashorn.internal.runtime.arrays;
import java.lang.invoke.MethodHandle;
+import java.nio.ByteBuffer;
import jdk.nashorn.internal.runtime.GlobalObject;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.PropertyDescriptor;
@@ -144,6 +145,16 @@ public abstract class ArrayData {
}
/**
+ * 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.
*
* @param underlying the underlying ArrayData to wrap in the freeze filter
diff --git a/src/jdk/nashorn/internal/runtime/arrays/ByteBufferArrayData.java b/src/jdk/nashorn/internal/runtime/arrays/ByteBufferArrayData.java
new file mode 100644
index 00000000..58ea1318
--- /dev/null
+++ b/src/jdk/nashorn/internal/runtime/arrays/ByteBufferArrayData.java
@@ -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);
+ }
+}
diff --git a/src/jdk/nashorn/internal/runtime/resources/Messages.properties b/src/jdk/nashorn/internal/runtime/resources/Messages.properties
index 95993c9f..e1c4d1f1 100644
--- a/src/jdk/nashorn/internal/runtime/resources/Messages.properties
+++ b/src/jdk/nashorn/internal/runtime/resources/Messages.properties
@@ -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.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
# operations not permitted on undefined
type.error.cant.call.undefined=Cannot call undefined