Java Code Examples for jdk.nashorn.internal.lookup.Lookup

The following are top voted examples for showing how to use jdk.nashorn.internal.lookup.Lookup. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: OpenJSharp   File: ContinuousArrayData.java   Source Code and License 6 votes vote down vote up
/**
 * Return element getter for a {@link ContinuousArrayData}
 * @param clazz        clazz for exact type guard
 * @param getHas       has getter
 * @param returnType   return type
 * @param programPoint program point
 * @return method handle for element setter
 */
protected MethodHandle getContinuousElementGetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle getHas, final Class<?> returnType, final int programPoint) {
    final boolean isOptimistic = isValid(programPoint);
    final int     fti          = getAccessorTypeIndex(getHas.type().returnType());
    final int     ti           = getAccessorTypeIndex(returnType);
    MethodHandle  mh           = getHas;

    if (isOptimistic) {
        if (ti < fti) {
            mh = MH.insertArguments(ArrayData.THROW_UNWARRANTED.methodHandle(), 1, programPoint);
        }
    }
    mh = MH.asType(mh, mh.type().changeReturnType(returnType).changeParameterType(0, clazz));

    if (!isOptimistic) {
        //for example a & array[17];
        return Lookup.filterReturnType(mh, returnType);
    }
    return mh;
}
 
Example 2
Project: OpenJSharp   File: AccessorProperty.java   Source Code and License 6 votes vote down vote up
private void initGetterSetter(final Class<?> structure) {
    final int slot = getSlot();
    /*
     * primitiveGetter and primitiveSetter are only used in dual fields mode. Setting them to null also
     * works in dual field mode, it only means that the property never has a primitive
     * representation.
     */

    if (isParameter() && hasArguments()) {
        //parameters are always stored in an object array, which may or may not be a good idea
        final MethodHandle arguments = MH.getter(LOOKUP, structure, "arguments", ScriptObject.class);
        objectGetter = MH.asType(MH.insertArguments(MH.filterArguments(ScriptObject.GET_ARGUMENT.methodHandle(), 0, arguments), 1, slot), Lookup.GET_OBJECT_TYPE);
        objectSetter = MH.asType(MH.insertArguments(MH.filterArguments(ScriptObject.SET_ARGUMENT.methodHandle(), 0, arguments), 1, slot), Lookup.SET_OBJECT_TYPE);
        primitiveGetter = null;
        primitiveSetter = null;
    } else {
        final Accessors gs = GETTERS_SETTERS.get(structure);
        objectGetter    = gs.objectGetters[slot];
        primitiveGetter = gs.primitiveGetters[slot];
        objectSetter    = gs.objectSetters[slot];
        primitiveSetter = gs.primitiveSetters[slot];
    }
}
 
Example 3
Project: openjdk-jdk10   File: ContinuousArrayData.java   Source Code and License 6 votes vote down vote up
/**
 * Return element getter for a {@link ContinuousArrayData}
 * @param clazz        clazz for exact type guard
 * @param getHas       has getter
 * @param returnType   return type
 * @param programPoint program point
 * @return method handle for element setter
 */
protected MethodHandle getContinuousElementGetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle getHas, final Class<?> returnType, final int programPoint) {
    final boolean isOptimistic = isValid(programPoint);
    final int     fti          = getAccessorTypeIndex(getHas.type().returnType());
    final int     ti           = getAccessorTypeIndex(returnType);
    MethodHandle  mh           = getHas;

    if (isOptimistic) {
        if (ti < fti) {
            mh = MH.insertArguments(ArrayData.THROW_UNWARRANTED.methodHandle(), 1, programPoint);
        }
    }
    mh = MH.asType(mh, mh.type().changeReturnType(returnType).changeParameterType(0, clazz));

    if (!isOptimistic) {
        //for example a & array[17];
        return Lookup.filterReturnType(mh, returnType);
    }
    return mh;
}
 
Example 4
Project: openjdk9   File: ContinuousArrayData.java   Source Code and License 6 votes vote down vote up
/**
 * Return element getter for a {@link ContinuousArrayData}
 * @param clazz        clazz for exact type guard
 * @param getHas       has getter
 * @param returnType   return type
 * @param programPoint program point
 * @return method handle for element setter
 */
protected MethodHandle getContinuousElementGetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle getHas, final Class<?> returnType, final int programPoint) {
    final boolean isOptimistic = isValid(programPoint);
    final int     fti          = getAccessorTypeIndex(getHas.type().returnType());
    final int     ti           = getAccessorTypeIndex(returnType);
    MethodHandle  mh           = getHas;

    if (isOptimistic) {
        if (ti < fti) {
            mh = MH.insertArguments(ArrayData.THROW_UNWARRANTED.methodHandle(), 1, programPoint);
        }
    }
    mh = MH.asType(mh, mh.type().changeReturnType(returnType).changeParameterType(0, clazz));

    if (!isOptimistic) {
        //for example a & array[17];
        return Lookup.filterReturnType(mh, returnType);
    }
    return mh;
}
 
Example 5
Project: kaziranga   File: ContinuousArrayData.java   Source Code and License 6 votes vote down vote up
/**
 * Return element getter for a {@link ContinuousArrayData}
 * @param clazz        clazz for exact type guard
 * @param getHas       has getter
 * @param returnType   return type
 * @param programPoint program point
 * @return method handle for element setter
 */
protected MethodHandle getContinuousElementGetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle getHas, final Class<?> returnType, final int programPoint) {
    final boolean isOptimistic = isValid(programPoint);
    final int     fti          = getAccessorTypeIndex(getHas.type().returnType());
    final int     ti           = getAccessorTypeIndex(returnType);
    MethodHandle  mh           = getHas;

    if (isOptimistic) {
        if (ti < fti) {
            mh = MH.insertArguments(ArrayData.THROW_UNWARRANTED.methodHandle(), 1, programPoint);
        }
    }
    mh = MH.asType(mh, mh.type().changeReturnType(returnType).changeParameterType(0, clazz));

    if (!isOptimistic) {
        //for example a & array[17];
        return Lookup.filterReturnType(mh, returnType);
    }
    return mh;
}
 
Example 6
Project: lookaside_java-1.8.0-openjdk   File: ContinuousArrayData.java   Source Code and License 6 votes vote down vote up
/**
 * Return element getter for a {@link ContinuousArrayData}
 * @param clazz        clazz for exact type guard
 * @param getHas       has getter
 * @param returnType   return type
 * @param programPoint program point
 * @return method handle for element setter
 */
protected MethodHandle getContinuousElementGetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle getHas, final Class<?> returnType, final int programPoint) {
    final boolean isOptimistic = isValid(programPoint);
    final int     fti          = getAccessorTypeIndex(getHas.type().returnType());
    final int     ti           = getAccessorTypeIndex(returnType);
    MethodHandle  mh           = getHas;

    if (isOptimistic) {
        if (ti < fti) {
            mh = MH.insertArguments(ArrayData.THROW_UNWARRANTED.methodHandle(), 1, programPoint);
        }
    }
    mh = MH.asType(mh, mh.type().changeReturnType(returnType).changeParameterType(0, clazz));

    if (!isOptimistic) {
        //for example a & array[17];
        return Lookup.filterReturnType(mh, returnType);
    }
    return mh;
}
 
Example 7
Project: jdk8u_nashorn   File: ContinuousArrayData.java   Source Code and License 6 votes vote down vote up
/**
 * Return element getter for a {@link ContinuousArrayData}
 * @param clazz        clazz for exact type guard
 * @param getHas       has getter
 * @param returnType   return type
 * @param programPoint program point
 * @return method handle for element setter
 */
protected MethodHandle getContinuousElementGetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle getHas, final Class<?> returnType, final int programPoint) {
    final boolean isOptimistic = isValid(programPoint);
    final int     fti          = getAccessorTypeIndex(getHas.type().returnType());
    final int     ti           = getAccessorTypeIndex(returnType);
    MethodHandle  mh           = getHas;

    if (isOptimistic) {
        if (ti < fti) {
            mh = MH.insertArguments(ArrayData.THROW_UNWARRANTED.methodHandle(), 1, programPoint);
        }
    }
    mh = MH.asType(mh, mh.type().changeReturnType(returnType).changeParameterType(0, clazz));

    if (!isOptimistic) {
        //for example a & array[17];
        return Lookup.filterReturnType(mh, returnType);
    }
    return mh;
}
 
Example 8
Project: infobip-open-jdk-8   File: ContinuousArrayData.java   Source Code and License 6 votes vote down vote up
/**
 * Return element getter for a {@link ContinuousArrayData}
 * @param clazz        clazz for exact type guard
 * @param getHas       has getter
 * @param returnType   return type
 * @param programPoint program point
 * @return method handle for element setter
 */
protected MethodHandle getContinuousElementGetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle getHas, final Class<?> returnType, final int programPoint) {
    final boolean isOptimistic = isValid(programPoint);
    final int     fti          = getAccessorTypeIndex(getHas.type().returnType());
    final int     ti           = getAccessorTypeIndex(returnType);
    MethodHandle  mh           = getHas;

    if (isOptimistic) {
        if (ti < fti) {
            mh = MH.insertArguments(ArrayData.THROW_UNWARRANTED.methodHandle(), 1, programPoint);
        }
    }
    mh = MH.asType(mh, mh.type().changeReturnType(returnType).changeParameterType(0, clazz));

    if (!isOptimistic) {
        //for example a & array[17];
        return Lookup.filterReturnType(mh, returnType);
    }
    return mh;
}
 
Example 9
Project: infobip-open-jdk-8   File: AccessorProperty.java   Source Code and License 6 votes vote down vote up
private void initGetterSetter(final Class<?> structure) {
    final int slot = getSlot();
    /*
     * primitiveGetter and primitiveSetter are only used in dual fields mode. Setting them to null also
     * works in dual field mode, it only means that the property never has a primitive
     * representation.
     */

    if (isParameter() && hasArguments()) {
        //parameters are always stored in an object array, which may or may not be a good idea
        final MethodHandle arguments = MH.getter(LOOKUP, structure, "arguments", ScriptObject.class);
        objectGetter = MH.asType(MH.insertArguments(MH.filterArguments(ScriptObject.GET_ARGUMENT.methodHandle(), 0, arguments), 1, slot), Lookup.GET_OBJECT_TYPE);
        objectSetter = MH.asType(MH.insertArguments(MH.filterArguments(ScriptObject.SET_ARGUMENT.methodHandle(), 0, arguments), 1, slot), Lookup.SET_OBJECT_TYPE);
        primitiveGetter = null;
        primitiveSetter = null;
    } else {
        final Accessors gs = GETTERS_SETTERS.get(structure);
        objectGetter    = gs.objectGetters[slot];
        primitiveGetter = gs.primitiveGetters[slot];
        objectSetter    = gs.objectSetters[slot];
        primitiveSetter = gs.primitiveSetters[slot];
    }
}
 
Example 10
Project: OpenJSharp   File: TypedArrayData.java   Source Code and License 5 votes vote down vote up
@Override
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
    final MethodHandle getter = getContinuousElementGetter(getClass(), getGetElem(), returnType, programPoint);
    if (getter != null) {
        return Lookup.filterReturnType(getter, returnType);
    }
    return getter;
}
 
Example 11
Project: OpenJSharp   File: ScriptObject.java   Source Code and License 5 votes vote down vote up
private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String name) {
    if (NashornCallSiteDescriptor.isOptimistic(desc)) {
        throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
    }

    return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()),
            NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), getProtoSwitchPoint(name, null),
            explicitInstanceOfCheck ? null : ClassCastException.class);
}
 
Example 12
Project: openjdk-jdk10   File: TypedArrayData.java   Source Code and License 5 votes vote down vote up
@Override
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
    final MethodHandle getter = getContinuousElementGetter(getClass(), getGetElem(), returnType, programPoint);
    if (getter != null) {
        return Lookup.filterReturnType(getter, returnType);
    }
    return getter;
}
 
Example 13
Project: openjdk-jdk10   File: ScriptObject.java   Source Code and License 5 votes vote down vote up
private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String name) {
    if (NashornCallSiteDescriptor.isOptimistic(desc)) {
        throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
    }

    return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()),
            NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), getProtoSwitchPoints(name, null),
            explicitInstanceOfCheck ? null : ClassCastException.class);
}
 
Example 14
Project: openjdk9   File: TypedArrayData.java   Source Code and License 5 votes vote down vote up
@Override
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
    final MethodHandle getter = getContinuousElementGetter(getClass(), getGetElem(), returnType, programPoint);
    if (getter != null) {
        return Lookup.filterReturnType(getter, returnType);
    }
    return getter;
}
 
Example 15
Project: openjdk9   File: ScriptObject.java   Source Code and License 5 votes vote down vote up
private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String name) {
    if (NashornCallSiteDescriptor.isOptimistic(desc)) {
        throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
    }

    return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()),
            NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), getProtoSwitchPoints(name, null),
            explicitInstanceOfCheck ? null : ClassCastException.class);
}
 
Example 16
Project: stallion-core   File: EvalLoop.java   Source Code and License 5 votes vote down vote up
public static MethodHandle getCalcHandle2(Global global, Context context) {
    EvalLoop.global = global;
    EvalLoop.context = context;
    return Lookup.MH.findStatic(
            MethodHandles.lookup(),
            EvalLoop.class, "calc",
            Lookup.MH.type(Object.class, new Class[]{Object.class, String.class, Object.class}
            ));
}
 
Example 17
Project: kaziranga   File: TypedArrayData.java   Source Code and License 5 votes vote down vote up
@Override
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
    final MethodHandle getter = getContinuousElementGetter(getClass(), getGetElem(), returnType, programPoint);
    if (getter != null) {
        return Lookup.filterReturnType(getter, returnType);
    }
    return getter;
}
 
Example 18
Project: kaziranga   File: ScriptObject.java   Source Code and License 5 votes vote down vote up
private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String name) {
    if (NashornCallSiteDescriptor.isOptimistic(desc)) {
        throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
    }

    return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()),
            NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), getProtoSwitchPoint(name, null),
            explicitInstanceOfCheck ? null : ClassCastException.class);
}
 
Example 19
Project: lookaside_java-1.8.0-openjdk   File: TypedArrayData.java   Source Code and License 5 votes vote down vote up
@Override
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
    final MethodHandle getter = getContinuousElementGetter(getClass(), getGetElem(), returnType, programPoint);
    if (getter != null) {
        return Lookup.filterReturnType(getter, returnType);
    }
    return getter;
}
 
Example 20
Project: lookaside_java-1.8.0-openjdk   File: ScriptObject.java   Source Code and License 5 votes vote down vote up
private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String name) {
    if (NashornCallSiteDescriptor.isOptimistic(desc)) {
        throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
    }

    return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()),
            NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), getProtoSwitchPoints(name, null),
            explicitInstanceOfCheck ? null : ClassCastException.class);
}
 
Example 21
Project: jdk8u_nashorn   File: TypedArrayData.java   Source Code and License 5 votes vote down vote up
@Override
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
    final MethodHandle getter = getContinuousElementGetter(getClass(), getGetElem(), returnType, programPoint);
    if (getter != null) {
        return Lookup.filterReturnType(getter, returnType);
    }
    return getter;
}
 
Example 22
Project: jdk8u_nashorn   File: ScriptObject.java   Source Code and License 5 votes vote down vote up
private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String name) {
    if (NashornCallSiteDescriptor.isOptimistic(desc)) {
        throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
    }

    return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()),
            NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), getProtoSwitchPoints(name, null),
            explicitInstanceOfCheck ? null : ClassCastException.class);
}
 
Example 23
Project: infobip-open-jdk-8   File: TypedArrayData.java   Source Code and License 5 votes vote down vote up
@Override
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
    final MethodHandle getter = getContinuousElementGetter(getClass(), getGetElem(), returnType, programPoint);
    if (getter != null) {
        return Lookup.filterReturnType(getter, returnType);
    }
    return getter;
}
 
Example 24
Project: infobip-open-jdk-8   File: ScriptObject.java   Source Code and License 5 votes vote down vote up
private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String name) {
    if (NashornCallSiteDescriptor.isOptimistic(desc)) {
        throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
    }

    return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()),
            NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), getProtoSwitchPoint(name, null),
            explicitInstanceOfCheck ? null : ClassCastException.class);
}
 
Example 25
Project: OLD-OpenJDK8   File: AccessorProperty.java   Source Code and License 5 votes vote down vote up
public GettersSetters(Class<?> structure) {
    final int fieldCount = ObjectClassGenerator.getFieldCount(structure);
    getters = new MethodHandle[fieldCount];
    setters = new MethodHandle[fieldCount];
    for(int i = 0; i < fieldCount; ++i) {
        final String fieldName = ObjectClassGenerator.getFieldName(i, Type.OBJECT);
        getters[i] = MH.asType(MH.getter(lookup, structure, fieldName, Type.OBJECT.getTypeClass()), Lookup.GET_OBJECT_TYPE);
        setters[i] = MH.asType(MH.setter(lookup, structure, fieldName, Type.OBJECT.getTypeClass()), Lookup.SET_OBJECT_TYPE);
    }
}
 
Example 26
Project: OLD-OpenJDK8   File: SetMethodCreator.java   Source Code and License 5 votes vote down vote up
private SetMethod createNewFieldSetter() {
    final PropertyMap oldMap = getMap();
    final Property property = new AccessorProperty(getName(), 0, sobj.getClass(), oldMap.getFieldCount());
    final PropertyMap newMap = oldMap.addProperty(property);
    MethodHandle setter = MH.insertArguments(ScriptObject.SETFIELD, 0, desc, oldMap, newMap, property.getSetter(Object.class, newMap));

    return new SetMethod(MH.asType(setter, Lookup.SET_OBJECT_TYPE), property);
}
 
Example 27
Project: nashorn-backport   File: ScriptFunctionImpl.java   Source Code and License 5 votes vote down vote up
static synchronized ScriptFunction getTypeErrorThrower() {
    if (typeErrorThrower == null) {
        // use "getter" so that [[ThrowTypeError]] function's arity is 0 - as specified in step 10 of section 13.2.3
        final ScriptFunctionImpl func = new ScriptFunctionImpl("TypeErrorThrower", Lookup.TYPE_ERROR_THROWER_GETTER, null, null, false, false, false);
        func.setPrototype(UNDEFINED);
        // Non-constructor built-in functions do not have "prototype" property
        func.deleteOwnProperty(func.getMap().findProperty("prototype"));
        func.preventExtensions();
        typeErrorThrower = func;
    }

    return typeErrorThrower;
}
 
Example 28
Project: nashorn-backport   File: AccessorProperty.java   Source Code and License 5 votes vote down vote up
public GettersSetters(Class<?> structure) {
    final int fieldCount = ObjectClassGenerator.getFieldCount(structure);
    getters = new MethodHandle[fieldCount];
    setters = new MethodHandle[fieldCount];
    for(int i = 0; i < fieldCount; ++i) {
        final String fieldName = ObjectClassGenerator.getFieldName(i, Type.OBJECT);
        getters[i] = MH.asType(MH.getter(lookup, structure, fieldName, Type.OBJECT.getTypeClass()), Lookup.GET_OBJECT_TYPE);
        setters[i] = MH.asType(MH.setter(lookup, structure, fieldName, Type.OBJECT.getTypeClass()), Lookup.SET_OBJECT_TYPE);
    }
}
 
Example 29
Project: nashorn-backport   File: SetMethodCreator.java   Source Code and License 5 votes vote down vote up
private SetMethod createNewFieldSetter() {
    final PropertyMap oldMap = getMap();
    final Property property = new AccessorProperty(getName(), 0, sobj.getClass(), oldMap.getFieldCount());
    final PropertyMap newMap = oldMap.addProperty(property);
    MethodHandle setter = MH.insertArguments(ScriptObject.SETFIELD, 0, desc, oldMap, newMap, property.getSetter(Object.class, newMap));

    return new SetMethod(MH.asType(setter, Lookup.SET_OBJECT_TYPE), property);
}
 
Example 30
Project: nashorn   File: ScriptFunctionImpl.java   Source Code and License 5 votes vote down vote up
static synchronized ScriptFunction getTypeErrorThrower() {
    if (typeErrorThrower == null) {
        // use "getter" so that [[ThrowTypeError]] function's arity is 0 - as specified in step 10 of section 13.2.3
        final ScriptFunctionImpl func = new ScriptFunctionImpl("TypeErrorThrower", Lookup.TYPE_ERROR_THROWER_GETTER, null, null, false, false, false);
        func.setPrototype(UNDEFINED);
        // Non-constructor built-in functions do not have "prototype" property
        func.deleteOwnProperty(func.getMap().findProperty("prototype"));
        func.preventExtensions();
        typeErrorThrower = func;
    }

    return typeErrorThrower;
}
 
Example 31
Project: nashorn   File: AccessorProperty.java   Source Code and License 5 votes vote down vote up
public GettersSetters(Class<?> structure) {
    final int fieldCount = ObjectClassGenerator.getFieldCount(structure);
    getters = new MethodHandle[fieldCount];
    setters = new MethodHandle[fieldCount];
    for(int i = 0; i < fieldCount; ++i) {
        final String fieldName = ObjectClassGenerator.getFieldName(i, Type.OBJECT);
        getters[i] = MH.asType(MH.getter(lookup, structure, fieldName, Type.OBJECT.getTypeClass()), Lookup.GET_OBJECT_TYPE);
        setters[i] = MH.asType(MH.setter(lookup, structure, fieldName, Type.OBJECT.getTypeClass()), Lookup.SET_OBJECT_TYPE);
    }
}
 
Example 32
Project: nashorn   File: SetMethodCreator.java   Source Code and License 5 votes vote down vote up
private SetMethod createNewFieldSetter() {
    final PropertyMap oldMap = getMap();
    final Property property = new AccessorProperty(getName(), 0, sobj.getClass(), oldMap.getFieldCount());
    final PropertyMap newMap = oldMap.addProperty(property);
    MethodHandle setter = MH.insertArguments(ScriptObject.SETFIELD, 0, desc, oldMap, newMap, property.getSetter(Object.class, newMap));

    return new SetMethod(MH.asType(setter, Lookup.SET_OBJECT_TYPE), property);
}
 
Example 33
Project: OpenJSharp   File: TypedArrayData.java   Source Code and License 4 votes vote down vote up
@Override
public MethodHandle getElementSetter(final Class<?> elementType) {
    return getContinuousElementSetter(getClass(), Lookup.filterArgumentType(getSetElem(), 2, elementType), elementType);
}
 
Example 34
Project: OpenJSharp   File: TypedArrayData.java   Source Code and License 4 votes vote down vote up
@Override
protected MethodHandle getContinuousElementSetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle setHas, final Class<?> elementType) {
    final MethodHandle mh = Lookup.filterArgumentType(setHas, 2, elementType);
    return MH.asType(mh, mh.type().changeParameterType(0, clazz));
}
 
Example 35
Project: OpenJSharp   File: GlobalConstants.java   Source Code and License 4 votes vote down vote up
/**
 * Try to turn a getter into a MethodHandle.constant, if possible
 *
 * @param find      property lookup
 * @param receiver  receiver
 * @param desc      callsite descriptor
 *
 * @return resulting getter, or null if failed to create constant
 */
GuardedInvocation findGetMethod(final FindProperty find, final ScriptObject receiver, final CallSiteDescriptor desc) {
    // Only use constant getter for fast scope access, because the receiver may change between invocations
    // for slow-scope and non-scope callsites.
    // Also return null for user accessor properties as they may have side effects.
    if (invalidatedForever.get() || !NashornCallSiteDescriptor.isFastScope(desc)
            || (GLOBAL_ONLY && !find.getOwner().isGlobal())
            || find.getProperty() instanceof UserAccessorProperty) {
        return null;
    }

    final boolean  isOptimistic = NashornCallSiteDescriptor.isOptimistic(desc);
    final int      programPoint = isOptimistic ? getProgramPoint(desc) : INVALID_PROGRAM_POINT;
    final Class<?> retType      = desc.getMethodType().returnType();
    final String   name         = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);

    synchronized (this) {
        final Access acc = getOrCreateSwitchPoint(name);

        log.fine("Starting to look up object value " + name);
        final Object c = find.getObjectValue();

        if (log.isEnabled()) {
            log.fine("Trying to link constant GETTER " + acc + " value = " + c);
        }

        if (acc.hasBeenInvalidated() || acc.guardFailed() || invalidatedForever.get()) {
            if (log.isEnabled()) {
                log.info("*** GET: Giving up on " + quote(name) + " - retry count has exceeded " + DynamicLinker.getLinkedCallSiteLocation());
            }
            return null;
        }

        final MethodHandle cmh = constantGetter(c);

        MethodHandle mh;
        MethodHandle guard;

        if (isOptimistic) {
            if (JSType.getAccessorTypeIndex(cmh.type().returnType()) <= JSType.getAccessorTypeIndex(retType)) {
                //widen return type - this is pessimistic, so it will always work
                mh = MH.asType(cmh, cmh.type().changeReturnType(retType));
            } else {
                //immediately invalidate - we asked for a too wide constant as a narrower one
                mh = MH.dropArguments(MH.insertArguments(JSType.THROW_UNWARRANTED.methodHandle(), 0, c, programPoint), 0, Object.class);
            }
        } else {
            //pessimistic return type filter
            mh = Lookup.filterReturnType(cmh, retType);
        }

        if (find.getOwner().isGlobal()) {
            guard = null;
        } else {
            guard = MH.insertArguments(RECEIVER_GUARD, 0, acc, receiver);
        }

        if (log.isEnabled()) {
            log.info("Linked getter " + quote(name) + " as MethodHandle.constant() -> " + c + " " + acc.getSwitchPoint());
            mh = MethodHandleFactory.addDebugPrintout(log, Level.FINE, mh, "get const " + acc);
        }

        return new GuardedInvocation(mh, guard, acc.getSwitchPoint(), null);
    }
}
 
Example 36
Project: OpenJSharp   File: UserAccessorProperty.java   Source Code and License 4 votes vote down vote up
@Override
public MethodHandle getGetter(final Class<?> type) {
    //this returns a getter on the format (Accessors, Object receiver)
    return Lookup.filterReturnType(INVOKE_OBJECT_GETTER, type);
}
 
Example 37
Project: openjdk-jdk10   File: TypedArrayData.java   Source Code and License 4 votes vote down vote up
@Override
public MethodHandle getElementSetter(final Class<?> elementType) {
    return getContinuousElementSetter(getClass(), Lookup.filterArgumentType(getSetElem(), 2, elementType), elementType);
}
 
Example 38
Project: openjdk-jdk10   File: TypedArrayData.java   Source Code and License 4 votes vote down vote up
@Override
protected MethodHandle getContinuousElementSetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle setHas, final Class<?> elementType) {
    final MethodHandle mh = Lookup.filterArgumentType(setHas, 2, elementType);
    return MH.asType(mh, mh.type().changeParameterType(0, clazz));
}
 
Example 39
Project: openjdk-jdk10   File: GlobalConstants.java   Source Code and License 4 votes vote down vote up
/**
 * Try to turn a getter into a MethodHandle.constant, if possible
 *
 * @param find      property lookup
 * @param receiver  receiver
 * @param desc      callsite descriptor
 *
 * @return resulting getter, or null if failed to create constant
 */
GuardedInvocation findGetMethod(final FindProperty find, final ScriptObject receiver, final CallSiteDescriptor desc) {
    // Only use constant getter for fast scope access, because the receiver may change between invocations
    // for slow-scope and non-scope callsites.
    // Also return null for user accessor properties as they may have side effects.
    if (invalidatedForever.get() || !NashornCallSiteDescriptor.isFastScope(desc)
            || (GLOBAL_ONLY && !find.getOwner().isGlobal())
            || find.getProperty() instanceof UserAccessorProperty) {
        return null;
    }

    final boolean  isOptimistic = NashornCallSiteDescriptor.isOptimistic(desc);
    final int      programPoint = isOptimistic ? getProgramPoint(desc) : INVALID_PROGRAM_POINT;
    final Class<?> retType      = desc.getMethodType().returnType();
    final String   name         = NashornCallSiteDescriptor.getOperand(desc);

    synchronized (this) {
        final Access acc = getOrCreateSwitchPoint(name);

        log.fine("Starting to look up object value " + name);
        final Object c = find.getObjectValue();

        if (log.isEnabled()) {
            log.fine("Trying to link constant GETTER " + acc + " value = " + c);
        }

        if (acc.hasBeenInvalidated() || acc.guardFailed() || invalidatedForever.get()) {
            if (log.isEnabled()) {
                log.info("*** GET: Giving up on " + quote(name) + " - retry count has exceeded " + DynamicLinker.getLinkedCallSiteLocation());
            }
            return null;
        }

        final MethodHandle cmh = constantGetter(c);

        MethodHandle mh;
        MethodHandle guard;

        if (isOptimistic) {
            if (JSType.getAccessorTypeIndex(cmh.type().returnType()) <= JSType.getAccessorTypeIndex(retType)) {
                //widen return type - this is pessimistic, so it will always work
                mh = MH.asType(cmh, cmh.type().changeReturnType(retType));
            } else {
                //immediately invalidate - we asked for a too wide constant as a narrower one
                mh = MH.dropArguments(MH.insertArguments(JSType.THROW_UNWARRANTED.methodHandle(), 0, c, programPoint), 0, Object.class);
            }
        } else {
            //pessimistic return type filter
            mh = Lookup.filterReturnType(cmh, retType);
        }

        if (find.getOwner().isGlobal()) {
            guard = null;
        } else {
            guard = MH.insertArguments(RECEIVER_GUARD, 0, acc, receiver);
        }

        if (log.isEnabled()) {
            log.info("Linked getter " + quote(name) + " as MethodHandle.constant() -> " + c + " " + acc.getSwitchPoint());
            mh = MethodHandleFactory.addDebugPrintout(log, Level.FINE, mh, "get const " + acc);
        }

        return new GuardedInvocation(mh, guard, acc.getSwitchPoint(), null);
    }
}
 
Example 40
Project: openjdk-jdk10   File: UserAccessorProperty.java   Source Code and License 4 votes vote down vote up
@Override
public MethodHandle getGetter(final Class<?> type) {
    //this returns a getter on the format (Accessors, Object receiver)
    return Lookup.filterReturnType(INVOKE_OBJECT_GETTER, type);
}