jdk.nashorn.internal.lookup.Lookup Java Examples

The following examples show how to use jdk.nashorn.internal.lookup.Lookup. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContinuousArrayData.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 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
Source File: ContinuousArrayData.java    From TencentKona-8 with GNU General Public License v2.0 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 #3
Source File: ContinuousArrayData.java    From jdk8u_nashorn with GNU General Public License v2.0 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
Source File: ContinuousArrayData.java    From hottub with GNU General Public License v2.0 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
Source File: ContinuousArrayData.java    From openjdk-jdk8u with GNU General Public License v2.0 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
Source File: ContinuousArrayData.java    From jdk8u60 with GNU General Public License v2.0 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
Source File: ContinuousArrayData.java    From openjdk-jdk9 with GNU General Public License v2.0 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
Source File: ScriptFunctionImpl.java    From nashorn with GNU General Public License v2.0 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 #9
Source File: ScriptObject.java    From openjdk-jdk8u with GNU General Public License v2.0 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 #10
Source File: ScriptObject.java    From jdk8u60 with GNU General Public License v2.0 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 #11
Source File: TypedArrayData.java    From jdk8u60 with GNU General Public License v2.0 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 #12
Source File: AccessorProperty.java    From nashorn with GNU General Public License v2.0 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 #13
Source File: ScriptObject.java    From hottub with GNU General Public License v2.0 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
Source File: ScriptObject.java    From TencentKona-8 with GNU General Public License v2.0 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 #15
Source File: ScriptObject.java    From jdk8u_nashorn with GNU General Public License v2.0 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
Source File: TypedArrayData.java    From TencentKona-8 with GNU General Public License v2.0 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 #17
Source File: TypedArrayData.java    From openjdk-jdk8u with GNU General Public License v2.0 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
Source File: SetMethodCreator.java    From nashorn with GNU General Public License v2.0 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 #19
Source File: TypedArrayData.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 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
Source File: SetMethodCreator.java    From openjdk-8 with GNU General Public License v2.0 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 #21
Source File: ScriptObject.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 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 #22
Source File: TypedArrayData.java    From openjdk-jdk9 with GNU General Public License v2.0 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 #23
Source File: AccessorProperty.java    From openjdk-8 with GNU General Public License v2.0 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 #24
Source File: ScriptObject.java    From openjdk-jdk9 with GNU General Public License v2.0 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 #25
Source File: SetMethodCreator.java    From openjdk-8-source with GNU General Public License v2.0 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 #26
Source File: TypedArrayData.java    From hottub with GNU General Public License v2.0 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 #27
Source File: AccessorProperty.java    From openjdk-8-source with GNU General Public License v2.0 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 #28
Source File: TypedArrayData.java    From jdk8u_nashorn with GNU General Public License v2.0 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 #29
Source File: AccessorProperty.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor for dual field AccessorPropertys.
 *
 * @param key              property key
 * @param flags            property flags
 * @param structure        structure for objects associated with this property
 * @param slot             property field number or spill slot
 */
public AccessorProperty(final String key, final int flags, final Class<?> structure, final int slot) {
    super(key, flags, slot);

    /*
     * 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.
     */
    primitiveGetter = null;
    primitiveSetter = null;

    if (isParameter() && hasArguments()) {
        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);
    } else {
        final GettersSetters gs = GETTERS_SETTERS.get(structure);
        objectGetter = gs.getters[slot];
        objectSetter = gs.setters[slot];

        if (!OBJECT_FIELDS_ONLY) {
            final String fieldNamePrimitive = ObjectClassGenerator.getFieldName(slot, PRIMITIVE_TYPE);
            final Class<?> typeClass = PRIMITIVE_TYPE.getTypeClass();
            primitiveGetter = MH.asType(MH.getter(lookup, structure, fieldNamePrimitive, typeClass), ACCESSOR_GETTER_PRIMITIVE_TYPE);
            primitiveSetter = MH.asType(MH.setter(lookup, structure, fieldNamePrimitive, typeClass), ACCESSOR_SETTER_PRIMITIVE_TYPE);
        }
    }

    Class<?> initialType = null;

    if (OBJECT_FIELDS_ONLY || isAlwaysObject()) {
        initialType = Object.class;
    } else if (!canBePrimitive()) {
        info(key + " cannot be primitive");
        initialType = Object.class;
    } else {
        info(key + " CAN be primitive");
        if (!canBeUndefined()) {
            info(key + " is always defined");
            initialType = int.class; //double works too for less type invalidation, but this requires experimentation, e.g. var x = 17; x += 2 will turn it into double now because of lack of range analysis
        }
    }

    // is always object means "is never initialized to undefined, and always of object type
    setCurrentType(initialType);
}
 
Example #30
Source File: AccessorProperty.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor for dual field AccessorPropertys.
 *
 * @param key              property key
 * @param flags            property flags
 * @param structure        structure for objects associated with this property
 * @param slot             property field number or spill slot
 */
public AccessorProperty(final String key, final int flags, final Class<?> structure, final int slot) {
    super(key, flags, slot);

    /*
     * 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.
     */
    primitiveGetter = null;
    primitiveSetter = null;

    if (isParameter() && hasArguments()) {
        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);
    } else {
        final GettersSetters gs = GETTERS_SETTERS.get(structure);
        objectGetter = gs.getters[slot];
        objectSetter = gs.setters[slot];

        if (!OBJECT_FIELDS_ONLY) {
            final String fieldNamePrimitive = ObjectClassGenerator.getFieldName(slot, PRIMITIVE_TYPE);
            final Class<?> typeClass = PRIMITIVE_TYPE.getTypeClass();
            primitiveGetter = MH.asType(MH.getter(lookup, structure, fieldNamePrimitive, typeClass), ACCESSOR_GETTER_PRIMITIVE_TYPE);
            primitiveSetter = MH.asType(MH.setter(lookup, structure, fieldNamePrimitive, typeClass), ACCESSOR_SETTER_PRIMITIVE_TYPE);
        }
    }

    Class<?> initialType = null;

    if (OBJECT_FIELDS_ONLY || isAlwaysObject()) {
        initialType = Object.class;
    } else if (!canBePrimitive()) {
        info(key + " cannot be primitive");
        initialType = Object.class;
    } else {
        info(key + " CAN be primitive");
        if (!canBeUndefined()) {
            info(key + " is always defined");
            initialType = int.class; //double works too for less type invalidation, but this requires experimentation, e.g. var x = 17; x += 2 will turn it into double now because of lack of range analysis
        }
    }

    // is always object means "is never initialized to undefined, and always of object type
    setCurrentType(initialType);
}