diff --git a/bosk-core/src/main/java/io/vena/bosk/bytecode/ClassBuilder.java b/bosk-core/src/main/java/io/vena/bosk/bytecode/ClassBuilder.java index 19744fcc..eead0dcc 100644 --- a/bosk-core/src/main/java/io/vena/bosk/bytecode/ClassBuilder.java +++ b/bosk-core/src/main/java/io/vena/bosk/bytecode/ClassBuilder.java @@ -2,7 +2,6 @@ import java.lang.invoke.CallSite; import java.lang.invoke.ConstantCallSite; -import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.lang.reflect.Constructor; @@ -187,7 +186,18 @@ public LocalVariable popToLocal(Type type) { * @param type */ public void pushObject(String name, Object object, Class type) { - CurriedValue field = curry(name, object, type); + type.cast(object); + + String fullName = "CallSite_" + CALL_SITE_COUNT.incrementAndGet() + "_" + name; + CurriedValue result = new CurriedValue( + fullName, + Type.getDescriptor(type), + object); + curriedValues.add(result); + + CALL_SITES_BY_NAME.put(fullName, new ConstantCallSite(MethodHandles.constant(type, object))); + LOGGER.warn("{} = {} {}", fullName, type.getSimpleName(), object); + beginPush(); /* // Dynamic constants aren't supported in Java 8 @@ -198,33 +208,12 @@ public void pushObject(String name, Object object, Class type) { )); */ methodVisitor().visitInvokeDynamicInsn( - field.name(), - "()" + field.typeDescriptor(), + result.name(), + "()" + result.typeDescriptor(), CONSTANT_CALL_SITE ); } - private CurriedValue curry(String name, Object object, Class type) { - type.cast(object); - for (CurriedValue candidate: curriedValues) { - if (candidate.value() == object) { - return candidate; - } - } - - String fullName = "CURRIED_" + METHOD_HANDLE_COUNT.incrementAndGet() + "_" + name; - CurriedValue result = new CurriedValue( - fullName, - Type.getDescriptor(type), - object); - curriedValues.add(result); - - METHOD_HANDLES_BY_NAME.put(fullName, MethodHandles.constant(type, object)); - LOGGER.warn("curry({}) = {} {}", fullName, type.getSimpleName(), object); - - return result; - } - /** * Emit LDC: ... */ @@ -400,12 +389,12 @@ public Class loadThemBytes(String dottyName, byte[] b) { * These need to be static because they need to be accessible from static initializers * without having any object to start from. */ - private static final AtomicLong METHOD_HANDLE_COUNT = new AtomicLong(0); - private static final Map METHOD_HANDLES_BY_NAME = new ConcurrentHashMap<>(); + private static final AtomicLong CALL_SITE_COUNT = new AtomicLong(0); + private static final Map CALL_SITES_BY_NAME = new ConcurrentHashMap<>(); public static CallSite constantCallSite(MethodHandles.Lookup lookup, String name, MethodType methodType) { LOGGER.warn("constantCallSite({})", name); - return new ConstantCallSite(METHOD_HANDLES_BY_NAME.remove(name).asType(methodType)); + return CALL_SITES_BY_NAME.remove(name); } private static final Method CONSTANT_CALL_SITE_METHOD;