package jdk.nashorn.internal.runtime;
import static jdk.nashorn.internal.lookup.Lookup.MH;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
public class SpillProperty extends AccessorProperty {
private static final long serialVersionUID = 3028496245198669460L;
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final MethodHandle PARRAY_GETTER = MH.asType(MH.getter(LOOKUP, ScriptObject.class, "primitiveSpill", long[].class), MH.type(long[].class, Object.class));
private static final MethodHandle OARRAY_GETTER = MH.asType(MH.getter(LOOKUP, ScriptObject.class, "objectSpill", Object[].class), MH.type(Object[].class, Object.class));
private static final MethodHandle OBJECT_GETTER = MH.filterArguments(MH.arrayElementGetter(Object[].class), 0, OARRAY_GETTER);
private static final MethodHandle PRIMITIVE_GETTER = MH.filterArguments(MH.arrayElementGetter(long[].class), 0, PARRAY_GETTER);
private static final MethodHandle OBJECT_SETTER = MH.filterArguments(MH.arrayElementSetter(Object[].class), 0, OARRAY_GETTER);
private static final MethodHandle PRIMITIVE_SETTER = MH.filterArguments(MH.arrayElementSetter(long[].class), 0, PARRAY_GETTER);
private static class Accessors {
private MethodHandle objectGetter;
private MethodHandle objectSetter;
private MethodHandle primitiveGetter;
private MethodHandle primitiveSetter;
private final int slot;
private final MethodHandle ensureSpillSize;
private static Accessors ACCESSOR_CACHE[] = new Accessors[512];
Accessors(final int slot) {
assert slot >= 0;
this.slot = slot;
this.ensureSpillSize = MH.asType(MH.insertArguments(ScriptObject.ENSURE_SPILL_SIZE, 1, slot), MH.type(Object.class, Object.class));
}
private static void ensure(final int slot) {
int len = ACCESSOR_CACHE.length;
if (slot >= len) {
do {
len *= 2;
} while (slot >= len);
final Accessors newCache[] = new Accessors[len];
System.arraycopy(ACCESSOR_CACHE, 0, newCache, 0, ACCESSOR_CACHE.length);
ACCESSOR_CACHE = newCache;
}
}
static MethodHandle getCached(final int slot, final boolean isPrimitive, final boolean isGetter) {
ensure(slot);
Accessors acc = ACCESSOR_CACHE[slot];
if (acc == null) {
acc = new Accessors(slot);
ACCESSOR_CACHE[slot] = acc;
}
return acc.getOrCreate(isPrimitive, isGetter);
}
private static MethodHandle primordial(final boolean isPrimitive, final boolean isGetter) {
if (isPrimitive) {
return isGetter ? PRIMITIVE_GETTER : PRIMITIVE_SETTER;
}
return isGetter ? OBJECT_GETTER : OBJECT_SETTER;
}
MethodHandle getOrCreate(final boolean isPrimitive, final boolean isGetter) {
MethodHandle accessor;
accessor = getInner(isPrimitive, isGetter);
if (accessor != null) {
return accessor;
}
accessor = primordial(isPrimitive, isGetter);
accessor = MH.insertArguments(accessor, 1, slot);
if (!isGetter) {
accessor = MH.filterArguments(accessor, 0, ensureSpillSize);
}
setInner(isPrimitive, isGetter, accessor);
return accessor;
}
void setInner(final boolean isPrimitive, final boolean isGetter, final MethodHandle mh) {
if (isPrimitive) {
if (isGetter) {
primitiveGetter = mh;
} else {
primitiveSetter = mh;
}
} else {
if (isGetter) {
objectGetter = mh;
} else {
objectSetter = mh;
}
}
}
MethodHandle getInner(final boolean isPrimitive, final boolean isGetter) {
if (isPrimitive) {
return isGetter ? primitiveGetter : primitiveSetter;
}
return isGetter ? objectGetter : objectSetter;
}
}
private static MethodHandle primitiveGetter(final int slot, final int flags) {
return (flags & DUAL_FIELDS) == DUAL_FIELDS ? Accessors.getCached(slot, true, true) : null;
}
private static MethodHandle primitiveSetter(final int slot, final int flags) {
return (flags & DUAL_FIELDS) == DUAL_FIELDS ? Accessors.getCached(slot, true, false) : null;
}
private static MethodHandle objectGetter(final int slot) {
return Accessors.getCached(slot, false, true);
}
private static MethodHandle objectSetter(final int slot) {
return Accessors.getCached(slot, false, false);
}
public SpillProperty(final Object key, final int flags, final int slot) {
super(key, flags, slot, primitiveGetter(slot, flags), primitiveSetter(slot, flags), objectGetter(slot), objectSetter(slot));
}
public SpillProperty(final String key, final int flags, final int slot, final Class<?> initialType) {
this(key, flags, slot);
setType(hasDualFields() ? initialType : Object.class);
}
SpillProperty(final Object key, final int flags, final int slot, final ScriptObject owner, final Object initialValue) {
this(key, flags, slot);
setInitialValue(owner, initialValue);
}
protected SpillProperty(final SpillProperty property) {
super(property);
}
protected SpillProperty(final SpillProperty property, final Class<?> newType) {
super(property, newType);
}
@Override
public Property copy() {
return new SpillProperty(this);
}
@Override
public Property copy(final Class<?> newType) {
return new SpillProperty(this, newType);
}
@Override
public boolean isSpill() {
return true;
}
@Override
void initMethodHandles(final Class<?> structure) {
final int slot = getSlot();
primitiveGetter = primitiveGetter(slot, getFlags());
primitiveSetter = primitiveSetter(slot, getFlags());
objectGetter = objectGetter(slot);
objectSetter = objectSetter(slot);
}
}