| package org.mozilla.javascript.optimizer; | |
| import org.mozilla.javascript.Context; | |
| import org.mozilla.javascript.ScriptRuntime; | |
| import org.mozilla.javascript.Scriptable; | |
| import org.mozilla.javascript.Wrapper; | |
| import java.lang.invoke.CallSite; | |
| import java.lang.invoke.MethodHandle; | |
| import java.lang.invoke.MethodHandles; | |
| import java.lang.invoke.MethodHandles.Lookup; | |
| import java.lang.invoke.MethodType; | |
| import java.lang.invoke.MutableCallSite; | |
| /** | |
| * This class contains methods that are called by generated bytecode that | |
| * sets up and uses the InvokeDynamic instruction. | |
| */ | |
| public class InvokeDynamicSupport { | |
| static class CachingCallSite extends MutableCallSite { | |
| final Lookup lookup; | |
| CachingCallSite(Lookup lookup, MethodType type) { | |
| super(type); | |
| this.lookup = lookup; | |
| } | |
| } | |
| /* | |
| public static CallSite bootstrapProp0Call(MethodHandles.Lookup lookup, | |
| String name, MethodType type) { | |
| CachingCallSite callSite = new CachingCallSite(lookup, type); | |
| MethodHandle check = INITCALL.bindTo(callSite); | |
| check = check.asType(type); | |
| callSite.setTarget(check); | |
| return callSite; | |
| } | |
| */ | |
| /** | |
| * Begin the process of setting up a CallSite to get the property of an object. | |
| */ | |
| @SuppressWarnings("unused") | |
| public static CallSite bootstrapGetObjectProp(MethodHandles.Lookup lookup, | |
| String name, MethodType type) { | |
| CachingCallSite callSite = new CachingCallSite(lookup, type); | |
| // The first call will be to getObjectProp in this class. | |
| MethodHandle check = INITOBJPROP.bindTo(callSite); | |
| check = check.asType(type); | |
| callSite.setTarget(check); | |
| return callSite; | |
| } | |
| /* | |
| public static CallSite bootstrapSetObjectProp(MethodHandles.Lookup lookup, | |
| String name, MethodType type) { | |
| CachingCallSite callSite = new CachingCallSite(lookup, type); | |
| MethodHandle check = SETOBJPROP.bindTo(callSite); | |
| check = check.asType(type); | |
| callSite.setTarget(check); | |
| return callSite; | |
| } | |
| */ | |
| /* | |
| public static boolean checkClass(Class<?> clazz, Object receiver) { | |
| return receiver instanceof NativeJavaObject && | |
| ((NativeJavaObject)receiver).unwrap().getClass() == clazz; | |
| } | |
| public static boolean checkLayout(ClassyLayout layout, Object receiver) { | |
| return receiver instanceof ClassyScriptable && | |
| ((ClassyScriptable)receiver).getLayout() == layout; | |
| } | |
| */ | |
| public static Object unwrapObject(Object obj) { | |
| return ((Wrapper)obj).unwrap(); | |
| } | |
| /* | |
| public static Object callProp0(CachingCallSite callSite, Object value, | |
| String property, Context cx, Scriptable scope) | |
| throws Throwable { | |
| if (value.getClass() != NativeJavaObject.class) { | |
| callSite.setTarget(CALLPROP0_FALLBACK); | |
| return CALLPROP0_FALLBACK.invoke(value, property, cx, scope); | |
| } | |
| Object javaObject = ((NativeJavaObject)value).unwrap(); | |
| Class<?> javaClass = javaObject.getClass(); | |
| MethodHandle target, localTarget; | |
| target = callSite.lookup.unreflect(javaClass.getMethod(property)); | |
| target = localTarget = target.asType(MethodType.genericMethodType(1)); | |
| target = MethodHandles.filterArguments(target, 0, UNWRAP); | |
| target = MethodHandles.dropArguments(target, 1, | |
| String.class, Context.class, Scriptable.class); | |
| MethodHandle test = CHECK_CLASS.bindTo(javaClass); | |
| test = test.asType(MethodType.methodType(boolean.class, Object.class)); | |
| MethodHandle guard = MethodHandles.guardWithTest(test, target, callSite.getTarget()); | |
| callSite.setTarget(guard); | |
| return localTarget.invoke(javaObject); | |
| } | |
| */ | |
| public static Object getObjectProp(CachingCallSite callSite, Object value, | |
| String property, Context cx, Scriptable scope) | |
| throws Throwable { | |
| // For now, always point this call to ScriptRuntime.getObjectProp | |
| callSite.setTarget(GETOBJPROP_FALLBACK); | |
| return GETOBJPROP_FALLBACK.invoke(value, property, cx, scope); | |
| } | |
| /* | |
| if (!(value instanceof ClassyScriptable)) { | |
| callSite.setTarget(GETOBJPROP_FALLBACK); | |
| return GETOBJPROP_FALLBACK.invoke(value, property, cx, scope); | |
| } | |
| ClassyScriptable classy = (ClassyScriptable) value; | |
| ClassyLayout layout = classy.getLayout(); | |
| ClassyLayout.Mapping mapping = layout.findMapping(property); | |
| if (mapping == null) { | |
| callSite.setTarget(GETOBJPROP_FALLBACK); | |
| return GETOBJPROP_FALLBACK.invoke(value, property, cx, scope); | |
| } | |
| int offset = mapping.offset(); | |
| MethodHandle target = MethodHandles.insertArguments(GETFASTOBJPROP, 1, | |
| Integer.valueOf(offset)); | |
| target = MethodHandles.dropArguments(target, 1, String.class, | |
| Context.class, Scriptable.class); | |
| MethodHandle test = CHECK_LAYOUT.bindTo(layout); | |
| test = test.asType(MethodType.methodType(boolean.class, Object.class)); | |
| MethodHandle guard = MethodHandles.guardWithTest(test, target, callSite.getTarget()); | |
| callSite.setTarget(guard); | |
| */ | |
| /* if (callSite.layout != layout) { | |
| ClassyLayout.Mapping mapping = layout.findMapping(property); | |
| if (mapping == null) { | |
| callSite.setTarget(GETOBJPROP_FALLBACK); | |
| return GETOBJPROP_FALLBACK.invoke(value, property, cx, scope); | |
| } else { | |
| callSite.layout = layout; | |
| callSite.offset = mapping.offset(); | |
| } | |
| } */ | |
| /* | |
| return classy.getValueAtOffset(offset, classy); | |
| } | |
| /* | |
| public static Object setObjectProp(CachingCallSite callSite, Object obj, | |
| String property, Object value, Context cx) | |
| throws Throwable { | |
| if (!(obj instanceof ClassyScriptable)) { | |
| callSite.setTarget(SETOBJPROP_FALLBACK); | |
| return SETOBJPROP_FALLBACK.invoke(obj, property, value, cx); | |
| } | |
| ClassyScriptable classy = (ClassyScriptable) obj; | |
| ClassyLayout layout = classy.getLayout(); | |
| Mapping mapping = layout.findMapping(property); | |
| int offset; | |
| if (mapping == null) { | |
| classy.put(property, classy, value); | |
| layout = classy.getLayout(); | |
| mapping = layout.findMapping(property); | |
| offset = mapping.offset(); | |
| } else { | |
| offset = mapping.offset(); | |
| classy.putValueAtOffset(offset, classy, value); | |
| } | |
| MethodHandle target = MethodHandles.insertArguments(SETFASTOBJPROP, 1, | |
| Integer.valueOf(offset)); | |
| target = MethodHandles.dropArguments(target, 1, String.class); | |
| target = MethodHandles.dropArguments(target, 3, Context.class); | |
| MethodHandle test = CHECK_LAYOUT.bindTo(layout); | |
| test = test.asType(MethodType.methodType(boolean.class, Object.class)); | |
| MethodHandle guard = MethodHandles.guardWithTest(test, target, callSite.getTarget()); | |
| callSite.setTarget(guard); | |
| return value; | |
| } | |
| */ | |
| //private static final MethodHandle CHECK_CLASS; | |
| //private static final MethodHandle CHECK_LAYOUT; | |
| //private static final MethodHandle INITCALL; | |
| private static final MethodHandle INITOBJPROP; | |
| //private static final MethodHandle CALLPROP0_FALLBACK; | |
| private static final MethodHandle GETOBJPROP_FALLBACK; | |
| //private static final MethodHandle GETFASTOBJPROP; | |
| //private static final MethodHandle UNWRAP; | |
| //private static final MethodHandle SETOBJPROP_FALLBACK; | |
| //private static final MethodHandle SETOBJPROP; | |
| //private static final MethodHandle SETFASTOBJPROP; | |
| static { | |
| MethodHandles.Lookup lookup = MethodHandles.lookup(); | |
| try { | |
| INITOBJPROP = lookup.findStatic(InvokeDynamicSupport.class, "getObjectProp", | |
| MethodType.methodType(Object.class, CachingCallSite.class, | |
| Object.class, String.class, Context.class, | |
| Scriptable.class)); | |
| GETOBJPROP_FALLBACK = lookup.findStatic(ScriptRuntime.class, "getObjectProp", | |
| MethodType.methodType(Object.class, Object.class, | |
| String.class, Context.class, Scriptable.class)); | |
| /* | |
| CHECK_CLASS = lookup.findStatic(InvokeDynamicSupport.class, "checkClass", | |
| MethodType.methodType(boolean.class, Class.class, Object.class)); | |
| CHECK_LAYOUT = lookup.findStatic(InvokeDynamicSupport.class, "checkLayout", | |
| MethodType.methodType(boolean.class, ClassyLayout.class, Object.class)); | |
| CALLPROP0_FALLBACK = lookup.findStatic(OptRuntime.class, "callProp0", | |
| MethodType.methodType(Object.class, Object.class, | |
| String.class, Context.class, Scriptable.class)); | |
| GETFASTOBJPROP = lookup.findStatic(InvokeDynamicSupport.class, "getFastObjectProp", | |
| MethodType.methodType(Object.class, Object.class, Integer.TYPE)); | |
| UNWRAP = lookup.findStatic(InvokeDynamicSupport.class, "unwrapObject", | |
| MethodType.methodType(Object.class, Object.class)); | |
| SETOBJPROP_FALLBACK = lookup.findStatic(ScriptRuntime.class, "setObjectProp", | |
| MethodType.methodType(Object.class, Object.class, | |
| String.class, Object.class, Context.class)); | |
| SETOBJPROP = lookup.findStatic(InvokeDynamicSupport.class, "setObjectProp", | |
| MethodType.methodType(Object.class, CachingCallSite.class, | |
| Object.class, String.class, Object.class, Context.class)); | |
| SETFASTOBJPROP = lookup.findStatic(InvokeDynamicSupport.class, "setFastObjectProp", | |
| MethodType.methodType(Object.class, Object.class, Integer.TYPE, Object.class)); | |
| */ | |
| } catch (ReflectiveOperationException e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| } |