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

The following examples show how to use jdk.nashorn.internal.runtime.JSType#toString() . 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: NativeNumber.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.7.4.7 Number.prototype.toPrecision (precision)
 *
 * @param self      self reference
 * @param precision use {@code precision - 1} digits after the significand's decimal point or call {@link NativeDate#toString} if undefined
 *
 * @return number in decimal exponentiation notation or decimal fixed notation depending on {@code precision}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toPrecision(final Object self, final Object precision) {
    final double x = getNumberValue(self);
    if (precision == UNDEFINED) {
        return JSType.toString(x);
    }

    final int p = JSType.toInteger(precision);
    if (Double.isNaN(x)) {
        return "NaN";
    } else if (Double.isInfinite(x)) {
        return x > 0? "Infinity" : "-Infinity";
    }

    if (p < 1 || p > 21) {
        throw rangeError("invalid.precision");
    }

    // workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6469160
    if (x == 0.0 && p <= 1) {
        return "0";
    }

    return fixExponent(String.format(Locale.US, "%." + p + "g", x), false);
}
 
Example 2
Source File: NativeNumber.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.7.4.5 Number.prototype.toFixed (fractionDigits) specialized for int fractionDigits
 *
 * @param self           self reference
 * @param fractionDigits how many digits should be after the decimal point, 0 if undefined
 *
 * @return number in decimal fixed point notation
 */
@SpecializedFunction
public static String toFixed(final Object self, final int fractionDigits) {
    if (fractionDigits < 0 || fractionDigits > 20) {
        throw rangeError("invalid.fraction.digits", "toFixed");
    }

    final double x = getNumberValue(self);
    if (Double.isNaN(x)) {
        return "NaN";
    }

    if (Math.abs(x) >= 1e21) {
        return JSType.toString(x);
    }

    return DoubleConversion.toFixed(x, fractionDigits);
}
 
Example 3
Source File: NativeString.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.5.4.8 String.prototype.lastIndexOf (searchString, position)
 * @param self   self reference
 * @param search string to search for
 * @param pos    position to start search
 * @return last position of match or -1
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object lastIndexOf(final Object self, final Object search, final Object pos) {

    final String str       = checkObjectToString(self);
    final String searchStr = JSType.toString(search);
    final int length       = str.length();

    int end;

    if (pos == UNDEFINED) {
        end = length;
    } else {
        final double numPos = JSType.toNumber(pos);
        end = Double.isNaN(numPos) ? length : (int)numPos;
        if (end < 0) {
            end = 0;
        } else if (end > length) {
            end = length;
        }
    }


    return str.lastIndexOf(searchStr, end);
}
 
Example 4
Source File: PrimitiveLookup.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static void primitiveSetter(final ScriptObject wrappedSelf, final Object self, final Object key,
                                    final boolean strict, final Object value) {
    // See ES5.1 8.7.2 PutValue (V, W)
    final String name = JSType.toString(key);
    final FindProperty find = wrappedSelf.findProperty(name, true);
    if (find == null || !find.getProperty().isAccessorProperty() || !find.getProperty().hasNativeSetter()) {
        if (strict) {
            if (find == null || !find.getProperty().isAccessorProperty()) {
                throw typeError("property.not.writable", name, ScriptRuntime.safeToString(self));
            } else {
                throw typeError("property.has.no.setter", name, ScriptRuntime.safeToString(self));
            }
        }
        return;
    }
    // property found and is a UserAccessorProperty
    find.setValue(value, strict);
}
 
Example 5
Source File: NativeError.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
private NativeError(final Object msg, final ScriptObject proto, final PropertyMap map) {
    super(proto, map);
    if (msg != UNDEFINED) {
        this.instMessage = JSType.toString(msg);
    } else {
        this.delete(NativeError.MESSAGE, false);
    }
    initException(this);
}
 
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.7 Object.prototype.propertyIsEnumerable (V)
 *
 * @param self self reference
 * @param v property to check if enumerable
 * @return true if property is enumerable
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static boolean propertyIsEnumerable(final Object self, final Object v) {
    final String str = JSType.toString(v);
    final Object obj = Global.toObject(self);

    if (obj instanceof ScriptObject) {
        final jdk.nashorn.internal.runtime.Property property = ((ScriptObject)obj).getMap().findProperty(str);
        return property != null && property.isEnumerable();
    }

    return false;
}
 
Example 7
Source File: NativeString.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA B.2.3 String.prototype.substr (start, length)
 *
 * @param self   self reference
 * @param start  start position
 * @param length length of section
 * @return substring given start and length of section
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String substr(final Object self, final Object start, final Object length) {
    final String str       = JSType.toString(self);
    final int    strLength = str.length();

    int intStart = JSType.toInteger(start);
    if (intStart < 0) {
        intStart = Math.max(intStart + strLength, 0);
    }

    final int intLen = Math.min(Math.max(length == UNDEFINED ? Integer.MAX_VALUE : JSType.toInteger(length), 0), strLength - intStart);

    return intLen <= 0 ? "" : str.substring(intStart, intStart + intLen);
}
 
Example 8
Source File: NativeNumber.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.2 Number.prototype.toString ( [ radix ] )
 *
 * @param self  self reference
 * @param radix radix to use for string conversion
 * @return string representation of this Number in the given radix
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toString(final Object self, final Object radix) {
    if (radix != UNDEFINED) {
        final int intRadix = JSType.toInteger(radix);
        if (intRadix != 10) {
            if (intRadix < 2 || intRadix > 36) {
                throw rangeError("invalid.radix");
            }
            return JSType.toString(getNumberValue(self), intRadix);
        }
    }

    return JSType.toString(getNumberValue(self));
}
 
Example 9
Source File: NativeObject.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 5.2.3.7 Object.defineProperties ( O, Properties )
 *
 * @param self  self reference
 * @param obj   object in which to define properties
 * @param props properties
 * @return object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static ScriptObject defineProperties(final Object self, final Object obj, final Object props) {
    final ScriptObject sobj     = Global.checkObject(obj);
    final Object       propsObj = Global.toObject(props);

    if (propsObj instanceof ScriptObject) {
        final Object[] keys = ((ScriptObject)propsObj).getOwnKeys(false);
        for (final Object key : keys) {
            final String prop = JSType.toString(key);
            sobj.defineOwnProperty(prop, ((ScriptObject)propsObj).get(prop), true);
        }
    }
    return sobj;
}
 
Example 10
Source File: NativeNumber.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.7 Number.prototype.toPrecision (precision)
 *
 * @param self      self reference
 * @param precision use {@code precision - 1} digits after the significand's decimal point or call {@link JSType#toString} if undefined
 *
 * @return number in decimal exponentiation notation or decimal fixed notation depending on {@code precision}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toPrecision(final Object self, final Object precision) {
    final double x = getNumberValue(self);
    if (precision == UNDEFINED) {
        return JSType.toString(x);
    }
    return (toPrecision(x, JSType.toInteger(precision)));
}
 
Example 11
Source File: NativeObject.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.4.7 Object.prototype.propertyIsEnumerable (V)
 *
 * @param self self reference
 * @param v property to check if enumerable
 * @return true if property is enumerable
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static boolean propertyIsEnumerable(final Object self, final Object v) {
    final String str = JSType.toString(v);
    final Object obj = Global.toObject(self);

    if (obj instanceof ScriptObject) {
        final jdk.nashorn.internal.runtime.Property property = ((ScriptObject)obj).getMap().findProperty(str);
        return property != null && property.isEnumerable();
    }

    return false;
}
 
Example 12
Source File: NativeURIError.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
NativeURIError(final Object msg, final Global global) {
    super(global.getURIErrorPrototype(), global.getURIErrorMap());
    if (msg != UNDEFINED) {
        this.instMessage = JSType.toString(msg);
    } else {
        this.delete(NativeError.MESSAGE, false);
    }
}
 
Example 13
Source File: NativeString.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA B.2.3 String.prototype.substr (start, length)
 *
 * @param self   self reference
 * @param start  start position
 * @param length length of section
 * @return substring given start and length of section
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String substr(final Object self, final Object start, final Object length) {
    final String str       = JSType.toString(self);
    final int    strLength = str.length();

    int intStart = JSType.toInteger(start);
    if (intStart < 0) {
        intStart = Math.max(intStart + strLength, 0);
    }

    final int intLen = Math.min(Math.max(length == UNDEFINED ? Integer.MAX_VALUE : JSType.toInteger(length), 0), strLength - intStart);

    return intLen <= 0 ? "" : str.substring(intStart, intStart + intLen);
}
 
Example 14
Source File: LiteralNode.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getPropertyName() {
    return JSType.toString(getObject());
}
 
Example 15
Source File: LiteralNode.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getPropertyName() {
    return JSType.toString(getObject());
}
 
Example 16
Source File: LiteralNode.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getPropertyName() {
    return JSType.toString(getObject());
}
 
Example 17
Source File: NativeNumber.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * ECMA 15.7.4.3 Number.prototype.toLocaleString()
 *
 * @param self self reference
 * @return localized string for this Number
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toLocaleString(final Object self) {
    return JSType.toString(getNumberValue(self));
}
 
Example 18
Source File: NativeNumber.java    From jdk8u_nashorn with GNU General Public License v2.0 2 votes vote down vote up
/**
 * ECMA 15.7.4.3 Number.prototype.toLocaleString()
 *
 * @param self self reference
 * @return localized string for this Number
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toLocaleString(final Object self) {
    return JSType.toString(getNumberValue(self));
}
 
Example 19
Source File: NativeString.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * ECMA 15.5.2.1 new String ( [ value ] ) - special version with exactly one {@code double} arg
 *
 * Constructor
 *
 * @param newObj is this constructor invoked with the new operator
 * @param self   self reference
 * @param arg    the arg
 *
 * @return new NativeString containing the string representation of the arg
 */
@SpecializedFunction(isConstructor=true)
public static Object constructor(final boolean newObj, final Object self, final double arg) {
    final String str = JSType.toString(arg);
    return newObj ? newObj(str) : str;
}
 
Example 20
Source File: NativeString.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * ECMA 15.5.2.1 new String ( [ value ] ) - special version with exactly one {@code int} arg
 *
 * Constructor
 *
 * @param newObj is this constructor invoked with the new operator
 * @param self   self reference
 * @param arg    the arg
 *
 * @return new NativeString containing the string representation of the arg
 */
@SpecializedFunction(isConstructor=true)
public static Object constructor(final boolean newObj, final Object self, final double arg) {
    final String str = JSType.toString(arg);
    return newObj ? newObj(str) : str;
}