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

The following examples show how to use org.mozilla.javascript.ScriptRuntime#toInt32() . 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: NativeArrayBuffer.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
                         Scriptable thisObj, Object[] args)
{
    if (!f.hasTag(CLASS_NAME)) {
        return super.execIdCall(f, cx, scope, thisObj, args);
    }
    int id = f.methodId();
    switch (id) {
    case ConstructorId_isView:
        return (isArg(args, 0) && (args[0] instanceof NativeArrayBufferView));

    case Id_constructor:
        int length = isArg(args, 0) ? ScriptRuntime.toInt32(args[0]) : 0;
        return new NativeArrayBuffer(length);

    case Id_slice:
        NativeArrayBuffer self = realThis(thisObj, f);
        int start = isArg(args, 0) ? ScriptRuntime.toInt32(args[0]) : 0;
        int end = isArg(args, 1) ? ScriptRuntime.toInt32(args[1]) : self.buffer.length;
        return self.slice(start, end);
    }
    throw new IllegalArgumentException(String.valueOf(id));
}
 
Example 2
Source File: NativeDataView.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
private Object js_getInt(int bytes, boolean signed, Object[] args)
{
    checkOffset(args, 0);

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

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

    switch (bytes) {
    case 1:
        return (signed ? ByteIo.readInt8(arrayBuffer.buffer, offset + pos) :
                         ByteIo.readUint8(arrayBuffer.buffer, offset + pos));
    case 2:
        return (signed ? ByteIo.readInt16(arrayBuffer.buffer, offset + pos, littleEndian) :
                         ByteIo.readUint16(arrayBuffer.buffer, offset + pos, littleEndian));
    case 4:
        return (signed ? ByteIo.readInt32(arrayBuffer.buffer, offset + pos, littleEndian) :
                         ByteIo.readUint32(arrayBuffer.buffer, offset + pos, littleEndian));
    default:
        throw new AssertionError();
    }
}
 
Example 3
Source File: NativeDataView.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
private Object js_getFloat(int bytes, Object[] args)
{
    checkOffset(args, 0);

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

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

    switch (bytes) {
    case 4:
        return ByteIo.readFloat32(arrayBuffer.buffer, offset + pos, littleEndian);
    case 8:
        return ByteIo.readFloat64(arrayBuffer.buffer, offset + pos, littleEndian);
    default:
        throw new AssertionError();
    }
}
 
Example 4
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 5
Source File: NativeDataView.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private void js_setInt(int bytes, boolean signed, 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]));

    switch (bytes) {
    case 1:
        if (signed) {
            ByteIo.writeInt8(arrayBuffer.buffer, offset + pos, Conversions.toInt8(args[1]));
        } else {
            ByteIo.writeUint8(arrayBuffer.buffer, offset + pos, Conversions.toUint8(args[1]));
        }
        break;
    case 2:
        if (signed) {
            ByteIo.writeInt16(arrayBuffer.buffer, offset + pos, Conversions.toInt16(args[1]), littleEndian);
        } else {
            ByteIo.writeUint16(arrayBuffer.buffer, offset + pos, Conversions.toUint16(args[1]), littleEndian);
        }
        break;
    case 4:
        if (signed) {
            ByteIo.writeInt32(arrayBuffer.buffer, offset + pos, Conversions.toInt32(args[1]), littleEndian);
        } else {
            ByteIo.writeUint32(arrayBuffer.buffer, offset + pos, Conversions.toUint32(args[1]), littleEndian);
        }
        break;
    default:
        throw new AssertionError();
    }
}
 
Example 6
Source File: Conversions.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public static int toInt8(Object arg)
{
    int iv;
    if (arg instanceof Integer) {
        iv = (Integer)arg;
    } else {
        iv = ScriptRuntime.toInt32(arg);
    }

    int int8Bit = iv % EIGHT_BIT;
    return (int8Bit >= (1 << 7)) ? (int8Bit - EIGHT_BIT) : int8Bit;
}
 
Example 7
Source File: Conversions.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public static int toUint8(Object arg)
{
    int iv;
    if (arg instanceof Integer) {
        iv = ((Integer)arg);
    } else {
        iv = ScriptRuntime.toInt32(arg);
    }

    return iv % EIGHT_BIT;
}
 
Example 8
Source File: Conversions.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public static int toInt16(Object arg)
{
    int iv;
    if (arg instanceof Integer) {
        iv = ((Integer)arg);
    } else {
        iv = ScriptRuntime.toInt32(arg);
    }

    int int16Bit = iv % SIXTEEN_BIT;
    return (int16Bit >= (1 << 15)) ? (int16Bit - SIXTEEN_BIT) : int16Bit;
}
 
Example 9
Source File: Conversions.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public static int toUint16(Object arg)
{
    int iv;
    if (arg instanceof Integer) {
        iv = ((Integer)arg);
    } else {
        iv = ScriptRuntime.toInt32(arg);
    }

    return iv % SIXTEEN_BIT;
}
 
Example 10
Source File: NativeInt32Array.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;
    }
    int val = ScriptRuntime.toInt32(c);
    ByteIo.writeInt32(arrayBuffer.buffer, (index * BYTES_PER_ELEMENT) + offset, val, false);
    return null;
}
 
Example 11
Source File: NativeDataView.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
                         Scriptable thisObj, Object[] args)
{
    if (!f.hasTag(getClassName())) {
        return super.execIdCall(f, cx, scope, thisObj, args);
    }
    int id = f.methodId();
    switch (id) {
    case Id_constructor:
        if (isArg(args, 0) && (args[0] instanceof NativeArrayBuffer)) {
            NativeArrayBuffer ab = (NativeArrayBuffer)args[0];
            int off = isArg(args, 1) ? ScriptRuntime.toInt32(args[1]) : 0;
            int len = isArg(args, 2) ? ScriptRuntime.toInt32(args[2]) : ab.getLength() - off;
            return js_constructor(ab, off, len);
        } else {
            throw ScriptRuntime.constructError("TypeError", "Missing parameters");
        }
    case Id_getInt8:
        return realThis(thisObj, f).js_getInt(1, true, args);
    case Id_getUint8:
        return realThis(thisObj, f).js_getInt(1, false, args);
    case Id_getInt16:
        return realThis(thisObj, f).js_getInt(2, true, args);
    case Id_getUint16:
        return realThis(thisObj, f).js_getInt(2, false, args);
    case Id_getInt32:
        return realThis(thisObj, f).js_getInt(4, true, args);
    case Id_getUint32:
        return realThis(thisObj, f).js_getInt(4, false, args);
    case Id_getFloat32:
        return realThis(thisObj, f).js_getFloat(4, args);
    case Id_getFloat64:
        return realThis(thisObj, f).js_getFloat(8, args);
    case Id_setInt8:
        realThis(thisObj, f).js_setInt(1, true, args);
        return Undefined.instance;
    case Id_setUint8:
        realThis(thisObj, f).js_setInt(1, false, args);
        return Undefined.instance;
    case Id_setInt16:
        realThis(thisObj, f).js_setInt(2, true, args);
        return Undefined.instance;
    case Id_setUint16:
        realThis(thisObj, f).js_setInt(2, false, args);
        return Undefined.instance;
    case Id_setInt32:
        realThis(thisObj, f).js_setInt(4, true, args);
        return Undefined.instance;
    case Id_setUint32:
        realThis(thisObj, f).js_setInt(4, false, args);
        return Undefined.instance;
    case Id_setFloat32:
        realThis(thisObj, f).js_setFloat(4, args);
        return Undefined.instance;
    case Id_setFloat64:
        realThis(thisObj, f).js_setFloat(8, args);
        return Undefined.instance;
    }
    throw new IllegalArgumentException(String.valueOf(id));
}