Java Code Examples for org.mozilla.javascript.ScriptRuntime#toNumber()

The following examples show how to use org.mozilla.javascript.ScriptRuntime#toNumber() . 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: NativeDataView.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
private void js_setFloat(int bytes, Object[] args)
{
    checkOffset(args, 0);
    checkValue(args, 1);

    int pos = ScriptRuntime.toInt32(args[0]);
    rangeCheck(pos, bytes);

    boolean littleEndian =
        (isArg(args, 2) && (bytes > 1) && ScriptRuntime.toBoolean(args[2]));
    double val = ScriptRuntime.toNumber(args[1]);

    switch (bytes) {
    case 4:
        ByteIo.writeFloat32(arrayBuffer.buffer, offset + pos, val, littleEndian);
        break;
    case 8:
        ByteIo.writeFloat64(arrayBuffer.buffer, offset + pos, val, littleEndian);
        break;
    default:
        throw new AssertionError();
    }
}
 
Example 2
Source File: Conversions.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
public static int toUint8Clamp(Object arg)
{
    double d = ScriptRuntime.toNumber(arg);
    if (d <= 0.0) {
        return 0;
    }
    if (d >= 255.0) {
        return 255;
    }

    // Complex rounding behavior -- see 7.1.11
    double f = Math.floor(d);
    if ((f + 0.5) < d) {
        return (int)(f + 1.0);
    }
    if (d < (f + 0.5)) {
        return (int)f;
    }
    if (((int)f % 2) != 0) {
        return (int)f + 1;
    }
    return (int)f;
}
 
Example 3
Source File: NativeFloat64Array.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected Object js_set(int index, Object c)
{
    if (checkIndex(index)) {
        return Undefined.instance;
    }
    double val = ScriptRuntime.toNumber(c);
    long base = Double.doubleToLongBits(val);
    ByteIo.writeUint64(arrayBuffer.buffer, (index * BYTES_PER_ELEMENT) + offset, base, false);
    return null;
}
 
Example 4
Source File: NativeFloat32Array.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected Object js_set(int index, Object c)
{
    if (checkIndex(index)) {
        return Undefined.instance;
    }
    double val = ScriptRuntime.toNumber(c);
    ByteIo.writeFloat32(arrayBuffer.buffer, (index * BYTES_PER_ELEMENT) + offset, val, false);
    return null;
}
 
Example 5
Source File: NativeRegExp.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void setInstanceIdValue(int id, Object value)
{
    switch (id) {
      case Id_lastIndex:
        lastIndex = ScriptRuntime.toNumber(value);
        return;
      case Id_source:
      case Id_global:
      case Id_ignoreCase:
      case Id_multiline:
        return;
    }
    super.setInstanceIdValue(id, value);
}
 
Example 6
Source File: Conversions.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
public static int toInt32(Object arg)
{
    long lv = (long)ScriptRuntime.toNumber(arg);
    long int32Bit = lv % THIRTYTWO_BIT;
    return (int)((int32Bit >= (1L << 31L)) ? (int32Bit - THIRTYTWO_BIT) : int32Bit);
}
 
Example 7
Source File: Conversions.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
public static long toUint32(Object arg)
{
    long lv = (long)ScriptRuntime.toNumber(arg);
    return lv % THIRTYTWO_BIT;
}