Java Code Examples for jdk.nashorn.internal.runtime.JSType#toPrimitive()

The following examples show how to use jdk.nashorn.internal.runtime.JSType#toPrimitive() . 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: JavaArgumentConverters.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Double toDouble(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Double) {
            return (Double) obj;
        } else if (obj instanceof Number) {
            return ((Number)obj).doubleValue();
        } else if (obj instanceof String) {
            return JSType.toNumber((String) obj);
        } else if (obj instanceof ConsString) {
            return JSType.toNumber(obj.toString());
        } else if (obj instanceof Boolean) {
            return (Boolean) obj ? 1 : +0.0;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return Double.NaN;
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 2
Source File: JavaArgumentConverters.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static String toString(final Object obj0) {
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof String) {
            return (String) obj;
        } else if (obj instanceof ConsString) {
            return obj.toString();
        } else if (obj instanceof Number) {
            return JSType.toString(((Number)obj).doubleValue());
        } else if (obj instanceof Boolean) {
            return ((Boolean) obj).toString();
        } else if (obj == UNDEFINED) {
            return "undefined";
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, String.class);
            continue;
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 3
Source File: JavaArgumentConverters.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Number toNumber(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Number) {
            return (Number) obj;
        } else if (obj instanceof String) {
            return JSType.toNumber((String) obj);
        } else if (obj instanceof ConsString) {
            return JSType.toNumber(obj.toString());
        } else if (obj instanceof Boolean) {
            return (Boolean) obj ? 1 : +0.0;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return Double.NaN;
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 4
Source File: JavaArgumentConverters.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Double toDouble(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Double) {
            return (Double) obj;
        } else if (obj instanceof Number) {
            return ((Number)obj).doubleValue();
        } else if (obj instanceof String) {
            return JSType.toNumber((String) obj);
        } else if (obj instanceof ConsString) {
            return JSType.toNumber(obj.toString());
        } else if (obj instanceof Boolean) {
            return (Boolean) obj ? 1 : +0.0;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return Double.NaN;
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 5
Source File: JavaArgumentConverters.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Number toNumber(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Number) {
            return (Number) obj;
        } else if (obj instanceof String) {
            return JSType.toNumber((String) obj);
        } else if (obj instanceof ConsString) {
            return JSType.toNumber(obj.toString());
        } else if (obj instanceof Boolean) {
            return (Boolean) obj ? 1 : +0.0;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return Double.NaN;
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 6
Source File: NativeObject.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.4.5 Object.prototype.hasOwnProperty (V)
 *
 * @param self self reference
 * @param v property to check for
 * @return true if property exists in object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static boolean hasOwnProperty(final Object self, final Object v) {
    // Convert ScriptObjects to primitive with String.class hint
    // but no need to convert other primitives to string.
    final Object key = JSType.toPrimitive(v, String.class);
    final Object obj = Global.toObject(self);

    return obj instanceof ScriptObject && ((ScriptObject)obj).hasOwnProperty(key);
}
 
Example 7
Source File: NativeObject.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.4.5 Object.prototype.hasOwnProperty (V)
 *
 * @param self self reference
 * @param v property to check for
 * @return true if property exists in object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static boolean hasOwnProperty(final Object self, final Object v) {
    // Convert ScriptObjects to primitive with String.class hint
    // but no need to convert other primitives to string.
    final Object key = JSType.toPrimitive(v, String.class);
    final Object obj = Global.toObject(self);

    return obj instanceof ScriptObject && ((ScriptObject)obj).hasOwnProperty(key);
}
 
Example 8
Source File: JavaArgumentConverters.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Long toLong(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Long) {
            return (Long) obj;
        } else if (obj instanceof Integer) {
            return ((Integer)obj).longValue();
        } else if (obj instanceof Double) {
            final Double d = (Double)obj;
            if(Double.isInfinite(d)) {
                return 0L;
            }
            return d.longValue();
        } else if (obj instanceof Float) {
            final Float f = (Float)obj;
            if(Float.isInfinite(f)) {
                return 0L;
            }
            return f.longValue();
        } else if (obj instanceof Number) {
            return ((Number)obj).longValue();
        } else if (isString(obj)) {
            return JSType.toLong(obj);
        } else if (obj instanceof Boolean) {
            return (Boolean)obj ? 1L : 0L;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return null; // null or 0L?
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 9
Source File: NativeString.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object get(final Object key) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    if (index >= 0 && index < value.length()) {
        return String.valueOf(value.charAt(index));
    }
    return super.get(primitiveKey);
}
 
Example 10
Source File: NativeDate.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor - ECMA 15.9.3.1 new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )
 *
 * @param isNew is this Date constructed with the new operator
 * @param self  self reference
 * @param args  arguments
 * @return new Date
 */
@Constructor(arity = 7)
public static Object construct(final boolean isNew, final Object self, final Object... args) {
    if (! isNew) {
        return toStringImpl(new NativeDate(), FORMAT_DATE_TIME);
    }

    NativeDate result;
    switch (args.length) {
    case 0:
        result = new NativeDate();
        break;

    case 1:
        double num;
        final Object arg = JSType.toPrimitive(args[0]);
        if (JSType.isString(arg)) {
            num = parseDateString(arg.toString());
        } else {
            num = timeClip(JSType.toNumber(args[0]));
        }
        result = new NativeDate(num);
        break;

    default:
        result = new NativeDate(0);
        final double[] d = convertCtorArgs(args);
        if (d == null) {
            result.setTime(Double.NaN);
        } else {
            final double time = timeClip(utc(makeDate(d), result.getTimeZone()));
            result.setTime(time);
        }
        break;
     }

     return result;
}
 
Example 11
Source File: JavaArgumentConverters.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static Long toLong(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Long) {
            return (Long) obj;
        } else if (obj instanceof Integer) {
            return ((Integer)obj).longValue();
        } else if (obj instanceof Double) {
            final Double d = (Double)obj;
            if(Double.isInfinite(d.doubleValue())) {
                return 0L;
            }
            return d.longValue();
        } else if (obj instanceof Float) {
            final Float f = (Float)obj;
            if(Float.isInfinite(f.floatValue())) {
                return 0L;
            }
            return f.longValue();
        } else if (obj instanceof Number) {
            return ((Number)obj).longValue();
        } else if (isString(obj)) {
            return JSType.toLong(obj);
        } else if (obj instanceof Boolean) {
            return (Boolean)obj ? 1L : 0L;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return null; // null or 0L?
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 12
Source File: JavaArgumentConverters.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static Long toLong(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Long) {
            return (Long) obj;
        } else if (obj instanceof Integer) {
            return ((Integer)obj).longValue();
        } else if (obj instanceof Double) {
            final Double d = (Double)obj;
            if(Double.isInfinite(d.doubleValue())) {
                return 0L;
            }
            return d.longValue();
        } else if (obj instanceof Float) {
            final Float f = (Float)obj;
            if(Float.isInfinite(f.floatValue())) {
                return 0L;
            }
            return f.longValue();
        } else if (obj instanceof Number) {
            return ((Number)obj).longValue();
        } else if (isString(obj)) {
            return JSType.toLong(obj);
        } else if (obj instanceof Boolean) {
            return (Boolean)obj ? 1L : 0L;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return null; // null or 0L?
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 13
Source File: NativeString.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean delete(final Object key, final boolean strict) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    return checkDeleteIndex(index, strict)? false : super.delete(primitiveKey, strict);
}
 
Example 14
Source File: NativeString.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean has(final Object key) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    return isValid(index) || super.has(primitiveKey);
}
 
Example 15
Source File: NativeArguments.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean delete(final Object key, final boolean strict) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    return isMapped(index) ? deleteMapped(index, strict) : super.delete(primitiveKey, strict);
}
 
Example 16
Source File: NativeString.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean hasOwnProperty(final Object key) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    return isValidStringIndex(index) || super.hasOwnProperty(primitiveKey);
}
 
Example 17
Source File: NativeString.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean hasOwnProperty(final Object key) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    return isValid(index) || super.hasOwnProperty(primitiveKey);
}
 
Example 18
Source File: NativeString.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean hasOwnProperty(final Object key) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    return isValidStringIndex(index) || super.hasOwnProperty(primitiveKey);
}
 
Example 19
Source File: NativeString.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean hasOwnProperty(final Object key) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    return isValidStringIndex(index) || super.hasOwnProperty(primitiveKey);
}
 
Example 20
Source File: NativeString.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean hasOwnProperty(final Object key) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    return isValid(index) || super.hasOwnProperty(primitiveKey);
}