package jdk.nashorn.internal.objects;
import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import jdk.dynalink.CallSiteDescriptor;
import jdk.dynalink.linker.GuardedInvocation;
import jdk.dynalink.linker.LinkRequest;
import jdk.nashorn.internal.objects.annotations.Attribute;
import jdk.nashorn.internal.objects.annotations.Getter;
import jdk.nashorn.internal.objects.annotations.ScriptClass;
import jdk.nashorn.internal.runtime.JSType;
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.arrays.TypedArrayData;
@ScriptClass("ArrayBufferView")
public abstract class ArrayBufferView extends ScriptObject {
private final NativeArrayBuffer buffer;
private final int byteOffset;
private static PropertyMap $nasgenmap$;
private ArrayBufferView(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength, final Global global) {
super($nasgenmap$);
final int bytesPerElement = bytesPerElement();
checkConstructorArgs(buffer.getByteLength(), bytesPerElement, byteOffset, elementLength);
setProto(getPrototype(global));
this.buffer = buffer;
this.byteOffset = byteOffset;
assert byteOffset % bytesPerElement == 0;
final int start = byteOffset / bytesPerElement;
final ByteBuffer newNioBuffer = buffer.getNioBuffer().duplicate().order(ByteOrder.nativeOrder());
final ArrayData data = factory().createArrayData(newNioBuffer, start, start + elementLength);
setArray(data);
}
protected ArrayBufferView(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength) {
this(buffer, byteOffset, elementLength, Global.instance());
}
private static void checkConstructorArgs(final int byteLength, final int bytesPerElement, final int byteOffset, final int elementLength) {
if (byteOffset < 0 || elementLength < 0) {
throw new RuntimeException("byteOffset or length must not be negative, byteOffset=" + byteOffset + ", elementLength=" + elementLength + ", bytesPerElement=" + bytesPerElement);
} else if (byteOffset + elementLength * bytesPerElement > byteLength) {
throw new RuntimeException("byteOffset + byteLength out of range, byteOffset=" + byteOffset + ", elementLength=" + elementLength + ", bytesPerElement=" + bytesPerElement);
} else if (byteOffset % bytesPerElement != 0) {
throw new RuntimeException("byteOffset must be a multiple of the element size, byteOffset=" + byteOffset + " bytesPerElement=" + bytesPerElement);
}
}
private int bytesPerElement() {
return factory().bytesPerElement;
}
@Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE)
public static Object buffer(final Object self) {
return ((ArrayBufferView)self).buffer;
}
@Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE)
public static int byteOffset(final Object self) {
return ((ArrayBufferView)self).byteOffset;
}
@Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE)
public static int byteLength(final Object self) {
final ArrayBufferView view = (ArrayBufferView)self;
return ((TypedArrayData<?>)view.getArray()).getElementLength() * view.bytesPerElement();
}
@Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE)
public static int length(final Object self) {
return ((ArrayBufferView)self).elementLength();
}
@Override
public final Object getLength() {
return elementLength();
}
private int elementLength() {
return ((TypedArrayData<?>)getArray()).getElementLength();
}
protected static abstract class Factory {
final int bytesPerElement;
final int maxElementLength;
public Factory(final int bytesPerElement) {
this.bytesPerElement = bytesPerElement;
this.maxElementLength = Integer.MAX_VALUE / bytesPerElement;
}
public final ArrayBufferView construct(final int elementLength) {
if (elementLength > maxElementLength) {
throw rangeError("inappropriate.array.buffer.length", JSType.toString(elementLength));
}
return construct(new NativeArrayBuffer(elementLength * bytesPerElement), 0, elementLength);
}
public abstract ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength);
public abstract TypedArrayData<?> createArrayData(final ByteBuffer nb, final int start, final int end);
public abstract String getClassName();
}
protected abstract Factory factory();
protected abstract ScriptObject getPrototype(final Global global);
@Override
public final String getClassName() {
return factory().getClassName();
}
protected boolean isFloatArray() {
return false;
}
protected static ArrayBufferView constructorImpl(final boolean newObj, final Object[] args, final Factory factory) {
final Object arg0 = args.length != 0 ? args[0] : 0;
final ArrayBufferView dest;
final int length;
if (!newObj) {
throw typeError("constructor.requires.new", factory.getClassName());
}
if (arg0 instanceof NativeArrayBuffer) {
final NativeArrayBuffer buffer = (NativeArrayBuffer)arg0;
final int byteOffset = args.length > 1 ? JSType.toInt32(args[1]) : 0;
if (args.length > 2) {
length = JSType.toInt32(args[2]);
} else {
if ((buffer.getByteLength() - byteOffset) % factory.bytesPerElement != 0) {
throw new RuntimeException("buffer.byteLength - byteOffset must be a multiple of the element size");
}
length = (buffer.getByteLength() - byteOffset) / factory.bytesPerElement;
}
return factory.construct(buffer, byteOffset, length);
} else if (arg0 instanceof ArrayBufferView) {
length = ((ArrayBufferView)arg0).elementLength();
dest = factory.construct(length);
} else if (arg0 instanceof NativeArray) {
length = lengthToInt(((NativeArray) arg0).getArray().length());
dest = factory.construct(length);
} else {
final double dlen = JSType.toNumber(arg0);
length = lengthToInt(Double.isInfinite(dlen) ? 0L : JSType.toLong(dlen));
return factory.construct(length);
}
copyElements(dest, length, (ScriptObject)arg0, 0);
return dest;
}
protected static Object setImpl(final Object self, final Object array, final Object offset0) {
final ArrayBufferView dest = (ArrayBufferView)self;
final int length;
if (array instanceof ArrayBufferView) {
length = ((ArrayBufferView)array).elementLength();
} else if (array instanceof NativeArray) {
length = (int) (((NativeArray) array).getArray().length() & 0x7fff_ffff);
} else {
throw new RuntimeException("argument is not of array type");
}
final ScriptObject source = (ScriptObject)array;
final int offset = JSType.toInt32(offset0);
if (dest.elementLength() < length + offset || offset < 0) {
throw new RuntimeException("offset or array length out of bounds");
}
copyElements(dest, length, source, offset);
return ScriptRuntime.UNDEFINED;
}
private static void copyElements(final ArrayBufferView dest, final int length, final ScriptObject source, final int offset) {
if (!dest.isFloatArray()) {
for (int i = 0, j = offset; i < length; i++, j++) {
dest.set(j, source.getInt(i, INVALID_PROGRAM_POINT), 0);
}
} else {
for (int i = 0, j = offset; i < length; i++, j++) {
dest.set(j, source.getDouble(i, INVALID_PROGRAM_POINT), 0);
}
}
}
private static int lengthToInt(final long length) {
if (length > Integer.MAX_VALUE || length < 0) {
throw rangeError("inappropriate.array.buffer.length", JSType.toString(length));
}
return (int)(length & Integer.MAX_VALUE);
}
protected static ScriptObject subarrayImpl(final Object self, final Object begin0, final Object end0) {
final ArrayBufferView arrayView = (ArrayBufferView)self;
final int byteOffset = arrayView.byteOffset;
final int bytesPerElement = arrayView.bytesPerElement();
final int elementLength = arrayView.elementLength();
final int begin = NativeArrayBuffer.adjustIndex(JSType.toInt32(begin0), elementLength);
final int end = NativeArrayBuffer.adjustIndex(end0 != ScriptRuntime.UNDEFINED ? JSType.toInt32(end0) : elementLength, elementLength);
final int length = Math.max(end - begin, 0);
assert byteOffset % bytesPerElement == 0;
return arrayView.factory().construct(arrayView.buffer, begin * bytesPerElement + byteOffset, length);
}
@Override
protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
final GuardedInvocation inv = getArray().findFastGetIndexMethod(getArray().getClass(), desc, request);
if (inv != null) {
return inv;
}
return super.findGetIndexMethod(desc, request);
}
@Override
protected GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
final GuardedInvocation inv = getArray().findFastSetIndexMethod(getArray().getClass(), desc, request);
if (inv != null) {
return inv;
}
return super.findSetIndexMethod(desc, request);
}
}