package jdk.nashorn.internal.objects;
import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall;
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
import static jdk.nashorn.internal.lookup.Lookup.MH;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;
import jdk.nashorn.internal.objects.annotations.Attribute;
import jdk.nashorn.internal.objects.annotations.Constructor;
import jdk.nashorn.internal.objects.annotations.Function;
import jdk.nashorn.internal.objects.annotations.Property;
import jdk.nashorn.internal.objects.annotations.ScriptClass;
import jdk.nashorn.internal.objects.annotations.Where;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.PropertyMap;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.arrays.ArrayData;
import jdk.nashorn.internal.runtime.arrays.TypedArrayData;
@ScriptClass("Uint8ClampedArray")
public final class NativeUint8ClampedArray extends ArrayBufferView {
@Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR)
public static final int BYTES_PER_ELEMENT = 1;
@SuppressWarnings("unused")
private static PropertyMap $nasgenmap$;
private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
@Override
public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
return new NativeUint8ClampedArray(buffer, byteOffset, length);
}
@Override
public Uint8ClampedArrayData createArrayData(final ByteBuffer nb, final int start, final int end) {
return new Uint8ClampedArrayData(nb, start, end);
}
@Override
public String getClassName() {
return "Uint8ClampedArray";
}
};
private static final class Uint8ClampedArrayData extends TypedArrayData<ByteBuffer> {
private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "getElem", int.class, int.class).methodHandle();
private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "setElem", void.class, int.class, int.class).methodHandle();
private static final MethodHandle RINT_D = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "rint", double.class, double.class).methodHandle();
private static final MethodHandle RINT_O = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "rint", Object.class, Object.class).methodHandle();
private Uint8ClampedArrayData(final ByteBuffer nb, final int start, final int end) {
super((nb.position(start).limit(end)).slice(), end - start);
}
@Override
protected MethodHandle getGetElem() {
return GET_ELEM;
}
@Override
protected MethodHandle getSetElem() {
return SET_ELEM;
}
@Override
public Class<?> getElementType() {
return int.class;
}
@Override
public Class<?> getBoxedElementType() {
return int.class;
}
private int getElem(final int index) {
try {
return nb.get(index) & 0xff;
} catch (final IndexOutOfBoundsException e) {
throw new ClassCastException();
}
}
@Override
public MethodHandle getElementSetter(final Class<?> elementType) {
final MethodHandle setter = super.getElementSetter(elementType);
if (setter != null) {
if (elementType == Object.class) {
return MH.filterArguments(setter, 2, RINT_O);
} else if (elementType == double.class) {
return MH.filterArguments(setter, 2, RINT_D);
}
}
return setter;
}
private void setElem(final int index, final int elem) {
try {
if (index < nb.limit()) {
final byte clamped;
if ((elem & 0xffff_ff00) == 0) {
clamped = (byte) elem;
} else {
clamped = elem < 0 ? 0 : (byte) 0xff;
}
nb.put(index, clamped);
}
} catch (final IndexOutOfBoundsException e) {
throw new ClassCastException();
}
}
@Override
public boolean isClamped() {
return true;
}
@Override
public boolean isUnsigned() {
return true;
}
@Override
public int getInt(final int index) {
return getElem(index);
}
@Override
public int getIntOptimistic(final int index, final int programPoint) {
return getElem(index);
}
@Override
public double getDouble(final int index) {
return getInt(index);
}
@Override
public double getDoubleOptimistic(final int index, final int programPoint) {
return getElem(index);
}
@Override
public Object getObject(final int index) {
return getInt(index);
}
@Override
public ArrayData set(final int index, final Object value, final boolean strict) {
return set(index, JSType.toNumber(value), strict);
}
@Override
public ArrayData set(final int index, final int value, final boolean strict) {
setElem(index, value);
return this;
}
@Override
public ArrayData set(final int index, final double value, final boolean strict) {
return set(index, (int) rint(value), strict);
}
private static double rint(final double rint) {
return (int)Math.rint(rint);
}
@SuppressWarnings("unused")
private static Object rint(final Object rint) {
return rint(JSType.toNumber(rint));
}
}
@Constructor(arity = 1)
public static NativeUint8ClampedArray constructor(final boolean newObj, final Object self, final Object... args) {
return (NativeUint8ClampedArray)constructorImpl(newObj, args, FACTORY);
}
NativeUint8ClampedArray(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
super(buffer, byteOffset, length);
}
@Override
protected Factory factory() {
return FACTORY;
}
@Function(attributes = Attribute.NOT_ENUMERABLE)
protected static Object set(final Object self, final Object array, final Object offset) {
return ArrayBufferView.setImpl(self, array, offset);
}
@Function(attributes = Attribute.NOT_ENUMERABLE)
protected static NativeUint8ClampedArray subarray(final Object self, final Object begin, final Object end) {
return (NativeUint8ClampedArray)ArrayBufferView.subarrayImpl(self, begin, end);
}
@Function(attributes = Attribute.NOT_ENUMERABLE, name = "@@iterator")
public static Object getIterator(final Object self) {
return ArrayIterator.newArrayValueIterator(self);
}
@Override
protected ScriptObject getPrototype(final Global global) {
return global.getUint8ClampedArrayPrototype();
}
}