SHARE
TWEET

Untitled

a guest Oct 21st, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private static class BiInliningCache extends MutableCallSite {
  2.         private static final MethodHandle SLOW_PATH, TEST;
  3.         private int depth;
  4.         static {
  5.           var lookup = MethodHandles.lookup();
  6.           try {
  7.             SLOW_PATH = lookup.findVirtual(BiInliningCache.class, "slowPath", methodType(Object.class, Object.class, Object.class, Object[].class));
  8.             TEST = lookup.findStatic(RT.BiInliningCache.class, "booleanTest", MethodType.methodType(boolean.class, Object.class, Object.class));
  9.           } catch (NoSuchMethodException | IllegalAccessException e) {
  10.             throw new AssertionError(e);
  11.           }
  12.         }
  13.        
  14.         public BiInliningCache(MethodType type) {
  15.           super(type);
  16.           setTarget(SLOW_PATH.bindTo(this).asCollector(Object[].class, type.parameterCount() - 2));
  17.         }
  18.         private static boolean booleanTest(Object o1, Object o2) {
  19.             return o1 == o2;
  20.         }
  21.         private Object slowPath(Object qualifier, Object receiver, Object[] args) {
  22.          
  23.            var jsObject = (JSObject)qualifier;
  24.            
  25.            if (depth++ == 2 ) {
  26.                setTarget(INVOKER
  27.                        .asCollector(Object[].class, type().parameterCount() - 2)
  28.                        .asType(type()));
  29. //              // return a constant callsite
  30.                 return jsObject.invoke(receiver, args);
  31.            }
  32.            var mh = jsObject.getMethodHandle();
  33.            if (!mh.isVarargsCollector() && args.length != mh.type().parameterCount() - 1) {
  34.                throw new Failure("arguments doesn't match parameters count " + args.length + " " + (mh.type().parameterCount() - 1));
  35.            }
  36.            
  37.            
  38.            
  39.            MethodHandle guard = MethodHandles.guardWithTest(
  40.                    TEST.bindTo(qualifier),
  41.                    MethodHandles.dropArguments(mh.asType(MethodType.genericMethodType(args.length + 1)), 0, Object.class),
  42.                    getTarget());
  43.            setTarget(guard);
  44.            return jsObject.invoke(receiver, args);
  45.            
  46.            
  47.         }
  48.       }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top