First start at resurrecting invokedynamic.
diff --git a/src/org/mozilla/classfile/ByteCode.java b/src/org/mozilla/classfile/ByteCode.java index df264b2..8a2ad06 100644 --- a/src/org/mozilla/classfile/ByteCode.java +++ b/src/org/mozilla/classfile/ByteCode.java
@@ -205,6 +205,7 @@ INVOKESPECIAL = 0xB7, INVOKESTATIC = 0xB8, INVOKEINTERFACE = 0xB9, + INVOKEDYNAMIC = 0xBA, NEW = 0xBB, NEWARRAY = 0xBC, ANEWARRAY = 0xBD, @@ -238,5 +239,17 @@ T_INT = 10, T_LONG = 11; - + /* + * Types for MethodHandle + */ + public static final byte + MH_GETFIELD = 1, + MH_GETSTATIC = 2, + MH_PUTFIELD = 3, + MH_PUTSTATIC = 4, + MH_INVOKEVIRTUAL = 5, + MH_INVOKESTATIC = 6, + MH_INVOKESPECIAL = 7, + MH_NEWINVOKESPECIAL = 8, + MH_INVOKEINTERFACE = 9; }
diff --git a/src/org/mozilla/classfile/ClassFileWriter.java b/src/org/mozilla/classfile/ClassFileWriter.java index 5fb1913..b7fe21b 100644 --- a/src/org/mozilla/classfile/ClassFileWriter.java +++ b/src/org/mozilla/classfile/ClassFileWriter.java
@@ -865,6 +865,51 @@ } } + public void addInvokeDynamic(String methodName, String methodType, + MHandle bsm, Object... bsmArgs) { + if (DEBUGCODE) { + System.out.println("Add invokedynamic, " + methodName + ", " + methodType); + } + // JDK 1.7 major class file version is required for invokedynamic + if (MajorVersion < 51) { + throw new RuntimeException( + "Please build and run with JDK 1.7 for invokedynamic support"); + } + + int parameterInfo = sizeOfParameters(methodType); + // int parameterCount = parameterInfo >>> 16; + int stackDiff = (short)parameterInfo; + + int newStack = itsStackTop + stackDiff; + if (newStack < 0 || Short.MAX_VALUE < newStack) badStack(newStack); + + BootstrapEntry bsmEntry = new BootstrapEntry(bsm, bsmArgs); + + if (itsBootstrapMethods == null) { + itsBootstrapMethods = new ObjArray(); + } + int bootstrapIndex = itsBootstrapMethods.indexOf(bsmEntry); + if (bootstrapIndex == -1) { + bootstrapIndex = itsBootstrapMethods.size(); + itsBootstrapMethods.add(bsmEntry); + itsBootstrapMethodsLength += bsmEntry.code.length; + } + + short invokedynamicIndex = itsConstantPool.addInvokeDynamic( + methodName, methodType, bootstrapIndex); + + addToCodeBuffer(ByteCode.INVOKEDYNAMIC); + addToCodeInt16(invokedynamicIndex); + addToCodeInt16(0); + + itsStackTop = (short)newStack; + if (newStack > itsMaxStack) itsMaxStack = (short)newStack; + if (DEBUGSTACK) { + System.out.println("After invokedynamic stack = " + itsStackTop); + } + + } + /** * Generate code to load the given integer on stack. * @@ -2197,6 +2242,20 @@ push(TypeInfo.fromType(returnType, itsConstantPool)); } break; + case ByteCode.INVOKEDYNAMIC: + index = getOperand(bci + 1, 2); + methodType = (String) itsConstantPool.getConstantData(index); + parameterCount = sizeOfParameters(methodType) >>> 16; + for (int i = 0; i < parameterCount; i++) { + pop(); + } + rParen = methodType.indexOf(')'); + returnType = methodType.substring(rParen + 1); + returnType = descriptorToInternalName(returnType); + if (!returnType.equals("V")) { + push(TypeInfo.fromType(returnType, itsConstantPool)); + } + break; case ByteCode.GETFIELD: pop(); // fallthru @@ -2773,13 +2832,17 @@ size += ((ClassFileMethod)(itsMethods.get(i))).getWriteSize(); } + size += 2; //writeShort(1); attributes count, could be zero if (itsSourceFileNameIndex != 0) { - size += 2; //writeShort(1); attributes count size += 2; //writeShort(sourceFileAttributeNameIndex); size += 4; //writeInt(2); size += 2; //writeShort(itsSourceFileNameIndex); - }else { - size += 2; //out.writeShort(0); no attributes + } + if (itsBootstrapMethods != null) { + size += 2; //writeShort(bootstrapMethodsAttrNameIndex); + size += 4; //writeInt(itsBootstrapMethodsLength); + size += 2; //writeShort(bootstrapMethods.size()); + size += itsBootstrapMethodsLength; } return size; @@ -2790,16 +2853,27 @@ */ public byte[] toByteArray() { - int dataSize = getWriteSize(); - byte[] data = new byte[dataSize]; - int offset = 0; + short bootstrapMethodsAttrNameIndex = 0; + int attributeCount = 0; short sourceFileAttributeNameIndex = 0; + if (itsBootstrapMethods != null) { + ++attributeCount; + bootstrapMethodsAttrNameIndex = itsConstantPool.addUtf8("BootstrapMethods"); + } + if (itsSourceFileNameIndex != 0) { + ++attributeCount; sourceFileAttributeNameIndex = itsConstantPool.addUtf8( "SourceFile"); } + // Don't calculate the data size until we know how many bootstrap + // methods there will be. + int offset = 0; + int dataSize = getWriteSize(); + byte[] data = new byte[dataSize]; + offset = putInt32(FileHeaderConstant, data, offset); offset = putInt16(MinorVersion, data, offset); offset = putInt16(MajorVersion, data, offset); @@ -2822,13 +2896,21 @@ ClassFileMethod method = (ClassFileMethod)itsMethods.get(i); offset = method.write(data, offset); } + offset = putInt16(attributeCount, data, offset); // attributes count + if (itsBootstrapMethods != null) { + offset = putInt16(bootstrapMethodsAttrNameIndex, data, offset); + offset = putInt32(itsBootstrapMethodsLength + 2, data, offset); + offset = putInt16(itsBootstrapMethods.size(), data, offset); + for (int i = 0; i < itsBootstrapMethods.size(); i++) { + BootstrapEntry entry = (BootstrapEntry) itsBootstrapMethods.get(i); + System.arraycopy(entry.code, 0, data, offset, entry.code.length); + offset += entry.code.length; + } + } if (itsSourceFileNameIndex != 0) { - offset = putInt16(1, data, offset); // attributes count offset = putInt16(sourceFileAttributeNameIndex, data, offset); offset = putInt32(2, data, offset); offset = putInt16(itsSourceFileNameIndex, data, offset); - } else { - offset = putInt16(0, data, offset); // no attributes } if (offset != dataSize) { @@ -3202,6 +3284,7 @@ case ByteCode.GOTO_W: case ByteCode.INVOKEINTERFACE: + case ByteCode.INVOKEDYNAMIC: case ByteCode.JSR_W: return 5; @@ -3585,6 +3668,7 @@ case ByteCode.INEG: case ByteCode.INSTANCEOF: case ByteCode.INVOKESTATIC: + case ByteCode.INVOKEDYNAMIC: case ByteCode.L2D: case ByteCode.LALOAD: case ByteCode.LNEG: @@ -4082,6 +4166,7 @@ case ByteCode.INVOKESPECIAL: return "invokespecial"; case ByteCode.INVOKESTATIC: return "invokestatic"; case ByteCode.INVOKEINTERFACE: return "invokeinterface"; + case ByteCode.INVOKEDYNAMIC: return "invokedynamic"; case ByteCode.NEW: return "new"; case ByteCode.NEWARRAY: return "newarray"; case ByteCode.ANEWARRAY: return "anewarray"; @@ -4234,6 +4319,70 @@ } } + final class BootstrapEntry { + + final byte[] code; + + BootstrapEntry(ClassFileWriter.MHandle bsm, Object... bsmArgs) { + int length = 2 + 2 + bsmArgs.length * 2; + code = new byte[length]; + putInt16(itsConstantPool.addMethodHandle(bsm), code, 0); + putInt16(bsmArgs.length, code, 2); + for (int i = 0; i < bsmArgs.length; i++) { + putInt16(itsConstantPool.addConstant(bsmArgs[i]), code, 4 + i * 2); + } + } + + @Override + public boolean equals(Object obj) { + return obj instanceof BootstrapEntry + && Arrays.equals(code, ((BootstrapEntry)obj).code); + } + + @Override + public int hashCode() { + return ~Arrays.hashCode(code); + } + } + + public static final class MHandle { + + final byte tag; + final String owner; + final String name; + final String desc; + + public MHandle(byte tag, String owner, String name, String desc) { + this.tag = tag; + this.owner = owner; + this.name = name; + this.desc = desc; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof MHandle)) { + return false; + } + MHandle mh = (MHandle)obj; + return tag == mh.tag && owner.equals(mh.owner) + && name.equals(mh.name) && desc.equals(mh.desc); + } + + @Override + public int hashCode() { + return tag + owner.hashCode() * name.hashCode() * desc.hashCode(); + } + + @Override + public String toString() { + return owner + '.' + name + desc + " (" + tag + ')'; + } + } + private final static int FileHeaderConstant = 0xCAFEBABE; // Set DEBUG flags to true to get better checking and progress info. private static final boolean DEBUGSTACK = false; @@ -4277,6 +4426,8 @@ private long[] itsFixupTable; private int itsFixupTableTop; private ObjArray itsVarDescriptors; + private ObjArray itsBootstrapMethods; + private int itsBootstrapMethodsLength = 0; private char[] tmpCharBuffer = new char[64]; } @@ -4439,7 +4590,10 @@ CONSTANT_Long = 5, CONSTANT_Double = 6, CONSTANT_NameAndType = 12, - CONSTANT_Utf8 = 1; + CONSTANT_Utf8 = 1, + CONSTANT_MethodType = 16, + CONSTANT_MethodHandle = 15, + CONSTANT_InvokeDynamic = 18; int write(byte[] data, int offset) { @@ -4511,6 +4665,32 @@ return theIndex; } + int addConstant(Object value) { + if (value instanceof Integer || value instanceof Byte + || value instanceof Short) { + return addConstant(((Number) value).intValue()); + } else if (value instanceof Character) { + return addConstant(((Character) value).charValue()); + } else if (value instanceof Boolean) { + return addConstant(((Boolean) value).booleanValue() ? 1 : 0); + } else if (value instanceof Float) { + return addConstant(((Float) value).floatValue()); + } else if (value instanceof Long) { + return addConstant(((Long) value).longValue()); + } else if (value instanceof Double) { + return addConstant(((Double) value).doubleValue()); + } else if (value instanceof String) { + return addConstant((String) value); + //} else if (value instanceof ClassFileWriter.MethodType) { + // return addMethodType((ClassFileWriter.MethodType) value); + } else if (value instanceof ClassFileWriter.MHandle) { + return addMethodHandle((ClassFileWriter.MHandle) value); + } else { + throw new IllegalArgumentException("value " + value); + } + } + + boolean isUnderUtfEncodingLimit(String s) { int strLen = s.length(); @@ -4705,6 +4885,51 @@ return (short)(itsTopIndex++); } + short addInvokeDynamic(String methodName, String methodType, int bootstrapIndex) + { + ConstantEntry entry = new ConstantEntry(CONSTANT_InvokeDynamic, + bootstrapIndex, methodName, methodType); + int theIndex = itsConstantHash.get(entry, -1); + + if (theIndex == -1) { + short nameTypeIndex = addNameAndType(methodName, methodType); + ensure(5); + itsPool[itsTop++] = CONSTANT_InvokeDynamic; + itsTop = ClassFileWriter.putInt16(bootstrapIndex, itsPool, itsTop); + itsTop = ClassFileWriter.putInt16(nameTypeIndex, itsPool, itsTop); + theIndex = itsTopIndex++; + itsConstantHash.put(entry, theIndex); + setConstantData(theIndex, methodType); + itsPoolTypes.put(theIndex, CONSTANT_InvokeDynamic); + } + return (short)(theIndex); + } + + short addMethodHandle(ClassFileWriter.MHandle mh) + { + int theIndex = itsConstantHash.get(mh, -1); + + if (theIndex == -1) { + short ref; + if (mh.tag <= ByteCode.MH_PUTSTATIC) { + ref = addFieldRef(mh.owner, mh.name, mh.desc); + } else if (mh.tag == ByteCode.MH_INVOKEINTERFACE) { + ref = addInterfaceMethodRef(mh.owner, mh.name, mh.desc); + } else { + ref = addMethodRef(mh.owner, mh.name, mh.desc); + } + + ensure(4); + itsPool[itsTop++] = CONSTANT_MethodHandle; + itsPool[itsTop++] = mh.tag; + itsTop = ClassFileWriter.putInt16(ref, itsPool, itsTop); + theIndex = itsTopIndex++; + itsConstantHash.put(mh, theIndex); + itsPoolTypes.put(theIndex, CONSTANT_MethodHandle); + } + return (short)(theIndex); + } + Object getConstantData(int index) { return itsConstantData.getObject(index); @@ -4742,6 +4967,7 @@ private ObjToIntMap itsFieldRefHash = new ObjToIntMap(); private ObjToIntMap itsMethodRefHash = new ObjToIntMap(); private ObjToIntMap itsClassHash = new ObjToIntMap(); + private ObjToIntMap itsConstantHash = new ObjToIntMap(); private int itsTop; private int itsTopIndex; @@ -5196,3 +5422,73 @@ System.out.println(); } } + +final class ConstantEntry { + + int type; + int intval; + long longval; + String str1; + String str2; + int hashcode; + + ConstantEntry(int type, int intval) { + this.type = type; + this.intval = intval; + hashcode = type + intval; + } + + ConstantEntry(int type, long longval) { + this.type = type; + this.longval = longval; + hashcode = type ^ (int)longval; + } + + ConstantEntry(int type,String str1, String str2) { + this.type = type; + this.str1 = str1; + this.str2 = str2; + hashcode = type ^ intval + str1.hashCode() * str2.hashCode(); + } + + ConstantEntry(int type, int intval, String str1, String str2) { + this.type = type; + this.intval = intval; + this.str1 = str1; + this.str2 = str2; + hashcode = type ^ intval + str1.hashCode() * str2.hashCode(); + } + + @Override + public int hashCode() { + return hashcode; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ConstantEntry)) { + return false; + } + ConstantEntry entry = (ConstantEntry) obj; + if (type != entry.type) { + return false; + } + switch (type) { + case ConstantPool.CONSTANT_Integer: + case ConstantPool.CONSTANT_Float: + return intval == entry.intval; + case ConstantPool.CONSTANT_Long: + case ConstantPool.CONSTANT_Double: + return longval == entry.longval; + case ConstantPool.CONSTANT_NameAndType: + return str1.equals(entry.str1) && str2.equals(entry.str2); + case ConstantPool.CONSTANT_InvokeDynamic: + return intval == entry.intval + && str1.equals(entry.str1) + && str2.equals(entry.str2); + default: + throw new RuntimeException("unsupported constant type"); + + } + } +}
diff --git a/src/org/mozilla/javascript/optimizer/Codegen.java b/src/org/mozilla/javascript/optimizer/Codegen.java index 5858fd6..b1f77a0 100644 --- a/src/org/mozilla/javascript/optimizer/Codegen.java +++ b/src/org/mozilla/javascript/optimizer/Codegen.java
@@ -7,15 +7,40 @@ package org.mozilla.javascript.optimizer; -import org.mozilla.javascript.*; +import org.mozilla.classfile.ByteCode; +import org.mozilla.classfile.ClassFileWriter; +import org.mozilla.javascript.CompilerEnvirons; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Evaluator; +import org.mozilla.javascript.Function; +import org.mozilla.javascript.GeneratedClassLoader; +import org.mozilla.javascript.Kit; +import org.mozilla.javascript.NativeFunction; +import org.mozilla.javascript.NativeGenerator; +import org.mozilla.javascript.Node; +import org.mozilla.javascript.ObjArray; +import org.mozilla.javascript.ObjToIntMap; +import org.mozilla.javascript.RhinoException; +import org.mozilla.javascript.Script; +import org.mozilla.javascript.ScriptRuntime; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.SecurityController; +import org.mozilla.javascript.Token; import org.mozilla.javascript.ast.FunctionNode; import org.mozilla.javascript.ast.Jump; import org.mozilla.javascript.ast.Name; import org.mozilla.javascript.ast.ScriptNode; -import org.mozilla.classfile.*; -import java.util.*; +import java.lang.invoke.CallSite; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; import static org.mozilla.classfile.ClassFileWriter.ACC_FINAL; import static org.mozilla.classfile.ClassFileWriter.ACC_PRIVATE; @@ -5100,10 +5125,25 @@ +")Ljava/lang/Object;"); return; } + // generate invokedynamic instruction for foo.bar + cfw.addALoad(contextLocal); + cfw.addALoad(variableObjectLocal); + ClassFileWriter.MHandle bootstrap = new ClassFileWriter.MHandle(ByteCode.MH_INVOKESTATIC, + "org/mozilla/javascript/optimizer/InvokeDynamicSupport", + "bootstrapGetObjectProp", + MethodType.methodType( + CallSite.class, MethodHandles.Lookup.class, + String.class, MethodType.class).toMethodDescriptorString()); + cfw.addInvokeDynamic("getObjectProp", "(Ljava/lang/Object;" + +"Ljava/lang/String;" + +"Lorg/mozilla/javascript/Context;" + +"Lorg/mozilla/javascript/Scriptable;" + +")Ljava/lang/Object;", bootstrap); /* for 'this.foo' we call getObjectProp(Scriptable...) which can skip some casting overhead. */ + /* int childType = child.getType(); if (childType == Token.THIS && nameChild.getType() == Token.STRING) { cfw.addALoad(contextLocal); @@ -5123,7 +5163,7 @@ +"Lorg/mozilla/javascript/Context;" +"Lorg/mozilla/javascript/Scriptable;" +")Ljava/lang/Object;"); - } + }*/ } private void visitSetProp(int type, Node node, Node child)
diff --git a/src/org/mozilla/javascript/optimizer/InvokeDynamicSupport.java b/src/org/mozilla/javascript/optimizer/InvokeDynamicSupport.java new file mode 100644 index 0000000..ac06023 --- /dev/null +++ b/src/org/mozilla/javascript/optimizer/InvokeDynamicSupport.java
@@ -0,0 +1,249 @@ +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); + } + } +}