Java Code Examples for jdk.nashorn.internal.codegen.types.Type#typeFor()

The following examples show how to use jdk.nashorn.internal.codegen.types.Type#typeFor() . 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: SharedScopeCall.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private String getStaticSignature() {
    if (staticSignature == null) {
        if (paramTypes == null) {
            staticSignature = Type.getMethodDescriptor(returnType, Type.typeFor(ScriptObject.class), Type.INT);
        } else {
            final Type[] params = new Type[paramTypes.length + 2];
            params[0] = Type.typeFor(ScriptObject.class);
            params[1] = Type.INT;
            int i = 2;
            for (Type type : paramTypes)  {
                if (type.isObject()) {
                    type = Type.OBJECT;
                }
                params[i++] = type;
            }
            staticSignature = Type.getMethodDescriptor(returnType, params);
        }
    }
    return staticSignature;
}
 
Example 2
Source File: SharedScopeCall.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private String getStaticSignature() {
    if (staticSignature == null) {
        if (paramTypes == null) {
            staticSignature = Type.getMethodDescriptor(returnType, Type.typeFor(ScriptObject.class), Type.INT);
        } else {
            final Type[] params = new Type[paramTypes.length + 2];
            params[0] = Type.typeFor(ScriptObject.class);
            params[1] = Type.INT;
            System.arraycopy(paramTypes, 0, params, 2, paramTypes.length);
            staticSignature = Type.getMethodDescriptor(returnType, params);
        }
    }
    return staticSignature;
}
 
Example 3
Source File: SharedScopeCall.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private String getStaticSignature() {
    if (staticSignature == null) {
        if (paramTypes == null) {
            staticSignature = Type.getMethodDescriptor(returnType, Type.typeFor(ScriptObject.class), Type.INT);
        } else {
            final Type[] params = new Type[paramTypes.length + 2];
            params[0] = Type.typeFor(ScriptObject.class);
            params[1] = Type.INT;
            System.arraycopy(paramTypes, 0, params, 2, paramTypes.length);
            staticSignature = Type.getMethodDescriptor(returnType, params);
        }
    }
    return staticSignature;
}
 
Example 4
Source File: TypeEvaluator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static Type getPropertyType(final ScriptObject sobj, final String name) {
    final FindProperty find = sobj.findProperty(name, true);
    if (find == null) {
        return null;
    }

    final Property property      = find.getProperty();
    final Class<?> propertyClass = property.getType();
    if (propertyClass == null) {
        // propertyClass == null means its value is Undefined. It is probably not initialized yet, so we won't make
        // a type assumption yet.
        return null;
    } else if (propertyClass.isPrimitive()) {
        return Type.typeFor(propertyClass);
    }

    final ScriptObject owner = find.getOwner();
    if (property.hasGetterFunction(owner)) {
        // Can have side effects, so we can't safely evaluate it; since !propertyClass.isPrimitive(), it's Object.
        return Type.OBJECT;
    }

    // Safely evaluate the property, and return the narrowest type for the actual value (e.g. Type.INT for a boxed
    // integer).
    final Object value = property.needsDeclaration() ? ScriptRuntime.UNDEFINED : property.getObjectValue(owner, owner);
    if (value == ScriptRuntime.UNDEFINED) {
        return null;
    }
    return Type.typeFor(JSType.unboxedFieldType(value));
}
 
Example 5
Source File: CompiledFunction.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static Type[] toTypeWithoutCallee(final MethodType type, final int thisIndex) {
    final int paramCount = type.parameterCount();
    final Type[] t = new Type[paramCount - thisIndex];
    for(int i = thisIndex; i < paramCount; ++i) {
        t[i - thisIndex] = Type.typeFor(type.parameterType(i));
    }
    return t;
}
 
Example 6
Source File: CompiledFunction.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static Type[] toTypeWithoutCallee(final MethodType type, final int thisIndex) {
    final int paramCount = type.parameterCount();
    final Type[] t = new Type[paramCount - thisIndex];
    for(int i = thisIndex; i < paramCount; ++i) {
        t[i - thisIndex] = Type.typeFor(type.parameterType(i));
    }
    return t;
}
 
Example 7
Source File: CompiledFunction.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
boolean matchesCallSite(final MethodType other, final boolean pickVarArg) {
    if (other.equals(this.callSiteType)) {
        return true;
    }
    final MethodType type  = type();
    final int fnParamCount = getParamCount(type);
    final boolean isVarArg = fnParamCount == Integer.MAX_VALUE;
    if (isVarArg) {
        return pickVarArg;
    }

    final int csParamCount = getParamCount(other);
    final boolean csIsVarArg = csParamCount == Integer.MAX_VALUE;
    final int thisThisIndex = needsCallee() ? 1 : 0; // Index of "this" parameter in this function's type

    final int fnParamCountNoCallee = fnParamCount - thisThisIndex;
    final int minParams = Math.min(csParamCount - 1, fnParamCountNoCallee); // callSiteType always has callee, so subtract 1
    // We must match all incoming parameters, including "this". "this" will usually be Object, but there
    // are exceptions, e.g. when calling functions with primitive "this" in strict mode or through call/apply.
    for(int i = 0; i < minParams; ++i) {
        final Type fnType = Type.typeFor(type.parameterType(i + thisThisIndex));
        final Type csType = csIsVarArg ? Type.OBJECT : Type.typeFor(other.parameterType(i + 1));
        if(!fnType.isEquivalentTo(csType)) {
            return false;
        }
    }

    // Must match any undefined parameters to Object type.
    for(int i = minParams; i < fnParamCountNoCallee; ++i) {
        if(!Type.typeFor(type.parameterType(i + thisThisIndex)).isEquivalentTo(Type.OBJECT)) {
            return false;
        }
    }

    return true;
}
 
Example 8
Source File: CompiledFunction.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
boolean matchesCallSite(final MethodType other, final boolean pickVarArg) {
    if (other.equals(this.callSiteType)) {
        return true;
    }
    final MethodType type  = type();
    final int fnParamCount = getParamCount(type);
    final boolean isVarArg = fnParamCount == Integer.MAX_VALUE;
    if (isVarArg) {
        return pickVarArg;
    }

    final int csParamCount = getParamCount(other);
    final boolean csIsVarArg = csParamCount == Integer.MAX_VALUE;
    final int thisThisIndex = needsCallee() ? 1 : 0; // Index of "this" parameter in this function's type

    final int fnParamCountNoCallee = fnParamCount - thisThisIndex;
    final int minParams = Math.min(csParamCount - 1, fnParamCountNoCallee); // callSiteType always has callee, so subtract 1
    // We must match all incoming parameters, including "this". "this" will usually be Object, but there
    // are exceptions, e.g. when calling functions with primitive "this" in strict mode or through call/apply.
    for(int i = 0; i < minParams; ++i) {
        final Type fnType = Type.typeFor(type.parameterType(i + thisThisIndex));
        final Type csType = csIsVarArg ? Type.OBJECT : Type.typeFor(other.parameterType(i + 1));
        if(!fnType.isEquivalentTo(csType)) {
            return false;
        }
    }

    // Must match any undefined parameters to Object type.
    for(int i = minParams; i < fnParamCountNoCallee; ++i) {
        if(!Type.typeFor(type.parameterType(i + thisThisIndex)).isEquivalentTo(Type.OBJECT)) {
            return false;
        }
    }

    return true;
}
 
Example 9
Source File: TypeEvaluator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static Type getPropertyType(final ScriptObject sobj, final String name) {
    final FindProperty find = sobj.findProperty(name, true);
    if (find == null) {
        return null;
    }

    final Property property      = find.getProperty();
    final Class<?> propertyClass = property.getType();
    if (propertyClass == null) {
        // propertyClass == null means its value is Undefined. It is probably not initialized yet, so we won't make
        // a type assumption yet.
        return null;
    } else if (propertyClass.isPrimitive()) {
        return Type.typeFor(propertyClass);
    }

    final ScriptObject owner = find.getOwner();
    if (property.hasGetterFunction(owner)) {
        // Can have side effects, so we can't safely evaluate it; since !propertyClass.isPrimitive(), it's Object.
        return Type.OBJECT;
    }

    // Safely evaluate the property, and return the narrowest type for the actual value (e.g. Type.INT for a boxed
    // integer).
    final Object value = property.needsDeclaration() ? ScriptRuntime.UNDEFINED : property.getObjectValue(owner, owner);
    if (value == ScriptRuntime.UNDEFINED) {
        return null;
    }
    return Type.typeFor(JSType.unboxedFieldType(value));
}
 
Example 10
Source File: TypeMap.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 * @param functionNodeId function node id
 * @param type           method type found at runtime corresponding to parameter guess
 * @param needsCallee    does the function using this type map need a callee
 */
public TypeMap(final int functionNodeId, final MethodType type, final boolean needsCallee) {
    final Type[] types = new Type[type.parameterCount()];
    int pos = 0;
    for (final Class<?> p : type.parameterArray()) {
        types[pos++] = Type.typeFor(p);
    }

    this.functionNodeId = functionNodeId;
    this.paramTypes = types;
    this.returnType = Type.typeFor(type.returnType());
    this.needsCallee = needsCallee;
}
 
Example 11
Source File: CompiledFunction.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
boolean matchesCallSite(final MethodType other, final boolean pickVarArg) {
    if (other.equals(this.callSiteType)) {
        return true;
    }
    final MethodType type  = type();
    final int fnParamCount = getParamCount(type);
    final boolean isVarArg = fnParamCount == Integer.MAX_VALUE;
    if (isVarArg) {
        return pickVarArg;
    }

    final int csParamCount = getParamCount(other);
    final boolean csIsVarArg = csParamCount == Integer.MAX_VALUE;
    final int thisThisIndex = needsCallee() ? 1 : 0; // Index of "this" parameter in this function's type

    final int fnParamCountNoCallee = fnParamCount - thisThisIndex;
    final int minParams = Math.min(csParamCount - 1, fnParamCountNoCallee); // callSiteType always has callee, so subtract 1
    // We must match all incoming parameters, including "this". "this" will usually be Object, but there
    // are exceptions, e.g. when calling functions with primitive "this" in strict mode or through call/apply.
    for(int i = 0; i < minParams; ++i) {
        final Type fnType = Type.typeFor(type.parameterType(i + thisThisIndex));
        final Type csType = csIsVarArg ? Type.OBJECT : Type.typeFor(other.parameterType(i + 1));
        if(!fnType.isEquivalentTo(csType)) {
            return false;
        }
    }

    // Must match any undefined parameters to Object type.
    for(int i = minParams; i < fnParamCountNoCallee; ++i) {
        if(!Type.typeFor(type.parameterType(i + thisThisIndex)).isEquivalentTo(Type.OBJECT)) {
            return false;
        }
    }

    return true;
}
 
Example 12
Source File: CompiledFunction.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Type[] toTypeWithoutCallee(final MethodType type, final int thisIndex) {
    final int paramCount = type.parameterCount();
    final Type[] t = new Type[paramCount - thisIndex];
    for(int i = thisIndex; i < paramCount; ++i) {
        t[i - thisIndex] = Type.typeFor(type.parameterType(i));
    }
    return t;
}
 
Example 13
Source File: SharedScopeCall.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private String getStaticSignature() {
    if (staticSignature == null) {
        if (paramTypes == null) {
            staticSignature = Type.getMethodDescriptor(returnType, Type.typeFor(ScriptObject.class), Type.INT);
        } else {
            final Type[] params = new Type[paramTypes.length + 2];
            params[0] = Type.typeFor(ScriptObject.class);
            params[1] = Type.INT;
            System.arraycopy(paramTypes, 0, params, 2, paramTypes.length);
            staticSignature = Type.getMethodDescriptor(returnType, params);
        }
    }
    return staticSignature;
}
 
Example 14
Source File: LiteralNode.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.typeFor(value.getClass());
}
 
Example 15
Source File: LiteralNode.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.typeFor(value.getClass());
}
 
Example 16
Source File: CompiledFunction.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Check whether a given method descriptor is compatible with this invocation.
 * It is compatible if the types are narrower than the invocation type so that
 * a semantically equivalent linkage can be performed.
 *
 * @param mt type to check against
 * @return
 */
boolean typeCompatible(final MethodType mt) {
    final Class<?>[] wantedParams   = mt.parameterArray();
    final Class<?>[] existingParams = type().parameterArray();

    //if we are not examining a varargs type, the number of parameters must be the same
    if (wantedParams.length != existingParams.length && !isVarArgsType(mt)) {
        return false;
    }

    //we only go as far as the shortest array. the only chance to make this work if
    //parameters lengths do not match is if our type ends with a varargs argument.
    //then every trailing parameter in the given callsite can be folded into it, making
    //us compatible (albeit slower than a direct specialization)
    final int lastParamIndex = Math.min(wantedParams.length, existingParams.length);
    for (int i = 0; i < lastParamIndex; i++) {
        final Type w = Type.typeFor(wantedParams[i]);
        final Type e = Type.typeFor(existingParams[i]);

        //don't specialize on booleans, we have the "true" vs int 1 ambiguity in resolution
        //we also currently don't support boolean as a javascript function callsite type.
        //it will always box.
        if (w.isBoolean()) {
            return false;
        }

        //This callsite type has a vararg here. it will swallow all remaining args.
        //for consistency, check that it's the last argument
        if (e.isArray()) {
            return true;
        }

        //Our arguments must be at least as wide as the wanted one, if not wider
        if (Type.widest(w, e) != e) {
            //e.g. this invocation takes double and callsite says "object". reject. won't fit
            //but if invocation takes a double and callsite says "int" or "long" or "double", that's fine
            return false;
        }
    }

    return true; // anything goes for return type, take the convenient one and it will be upcasted thru dynalink magic.
}
 
Example 17
Source File: ContinuousArrayData.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type getOptimisticType() {
    return Type.typeFor(getElementType());
}
 
Example 18
Source File: LiteralNode.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.typeFor(NativeArray.class);
}
 
Example 19
Source File: ContinuousArrayData.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type getOptimisticType() {
    return Type.typeFor(getElementType());
}
 
Example 20
Source File: LiteralNode.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.typeFor(NativeArray.class);
}