Java Code Examples for jdk.nashorn.internal.runtime.arrays.ArrayData#allocate()

The following examples show how to use jdk.nashorn.internal.runtime.arrays.ArrayData#allocate() . 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: Global.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Wrap a Java object as corresponding script object
 *
 * @param obj object to wrap
 * @return    wrapped object
 */
public Object wrapAsObject(final Object obj) {
    if (obj instanceof Boolean) {
        return new NativeBoolean((Boolean)obj, this);
    } else if (obj instanceof Number) {
        return new NativeNumber(((Number)obj).doubleValue(), this);
    } else if (isString(obj)) {
        return new NativeString((CharSequence)obj, this);
    } else if (obj instanceof Object[]) { // extension
        return new NativeArray(ArrayData.allocate((Object[])obj), this);
    } else if (obj instanceof double[]) { // extension
        return new NativeArray(ArrayData.allocate((double[])obj), this);
    } else if (obj instanceof int[]) {
        return new NativeArray(ArrayData.allocate((int[]) obj), this);
    } else if (obj instanceof ArrayData) {
        return new NativeArray((ArrayData) obj, this);
    } else {
        // FIXME: more special cases? Map? List?
        return obj;
    }
}
 
Example 2
Source File: NativeArray.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
NativeArray(final Object[] array) {
    this(ArrayData.allocate(array.length));

    ArrayData arrayData = this.getArray();

    for (int index = 0; index < array.length; index++) {
        final Object value = array[index];

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

    this.setArray(arrayData);
}
 
Example 3
Source File: NativeArray.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
NativeArray(final long[] array) {
    this(ArrayData.allocate(array.length));

    ArrayData arrayData = this.getArray();
    Class<?> widest = int.class;

    for (int index = 0; index < array.length; index++) {
        final long value = array[index];

        if (widest == int.class && JSType.isRepresentableAsInt(value)) {
            arrayData = arrayData.set(index, (int) value, false);
        } else if (widest != Object.class && JSType.isRepresentableAsDouble(value)) {
            arrayData = arrayData.set(index, (double) value, false);
            widest = double.class;
        } else {
            arrayData = arrayData.set(index, (Object) value, false);
            widest = Object.class;
        }
    }

    this.setArray(arrayData);
}
 
Example 4
Source File: NativeArray.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
NativeArray(final Object[] array) {
    this(ArrayData.allocate(array.length));

    ArrayData arrayData = this.getArray();

    for (int index = 0; index < array.length; index++) {
        final Object value = array[index];

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

    this.setArray(arrayData);
}
 
Example 5
Source File: NativeArray.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
NativeArray(final long[] array) {
    this(ArrayData.allocate(array.length));

    ArrayData arrayData = this.getArray();
    Class<?> widest = int.class;

    for (int index = 0; index < array.length; index++) {
        final long value = array[index];

        if (widest == int.class && JSType.isRepresentableAsInt(value)) {
            arrayData = arrayData.set(index, (int) value, false);
        } else if (widest != Object.class && JSType.isRepresentableAsDouble(value)) {
            arrayData = arrayData.set(index, (double) value, false);
            widest = double.class;
        } else {
            arrayData = arrayData.set(index, (Object) value, false);
            widest = Object.class;
        }
    }

    this.setArray(arrayData);
}
 
Example 6
Source File: NativeArray.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
NativeArray(final Object[] array) {
    this(ArrayData.allocate(array.length));

    ArrayData arrayData = this.getArray();

    for (int index = 0; index < array.length; index++) {
        final Object value = array[index];

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

    this.setArray(arrayData);
}
 
Example 7
Source File: Global.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Wrap a Java object as corresponding script object
 *
 * @param obj object to wrap
 * @return    wrapped object
 */
public Object wrapAsObject(final Object obj) {
    if (obj instanceof Boolean) {
        return new NativeBoolean((Boolean)obj, this);
    } else if (obj instanceof Number) {
        return new NativeNumber(((Number)obj).doubleValue(), this);
    } else if (isString(obj)) {
        return new NativeString((CharSequence)obj, this);
    } else if (obj instanceof Object[]) { // extension
        return new NativeArray(ArrayData.allocate((Object[])obj), this);
    } else if (obj instanceof double[]) { // extension
        return new NativeArray(ArrayData.allocate((double[])obj), this);
    } else if (obj instanceof long[]) {
        return new NativeArray(ArrayData.allocate((long[])obj), this);
    } else if (obj instanceof int[]) {
        return new NativeArray(ArrayData.allocate((int[]) obj), this);
    } else if (obj instanceof ArrayData) {
        return new NativeArray((ArrayData) obj, this);
    } else {
        // FIXME: more special cases? Map? List?
        return obj;
    }
}
 
Example 8
Source File: NativeArray.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
NativeArray(final Object[] array) {
    this(ArrayData.allocate(array.length));

    ArrayData arrayData = this.getArray();

    for (int index = 0; index < array.length; index++) {
        final Object value = array[index];

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

    this.setArray(arrayData);
}
 
Example 9
Source File: Global.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allocate a new object array.
 *
 * @param initial object values.
 * @return the new array
 */
public static NativeArray allocate(final Object[] initial) {
    ArrayData arrayData = ArrayData.allocate(initial);

    for (int index = 0; index < initial.length; index++) {
        final Object value = initial[index];

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        }
    }

    return new NativeArray(arrayData);
}
 
Example 10
Source File: Global.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allocate a new object array.
 *
 * @param initial object values.
 * @return the new array
 */
public static NativeArray allocate(final Object[] initial) {
    ArrayData arrayData = ArrayData.allocate(initial);

    for (int index = 0; index < initial.length; index++) {
        final Object value = initial[index];

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        }
    }

    return new NativeArray(arrayData);
}
 
Example 11
Source File: Global.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allocate a new object array.
 *
 * @param initial object values.
 * @return the new array
 */
public static NativeArray allocate(final Object[] initial) {
    ArrayData arrayData = ArrayData.allocate(initial);

    for (int index = 0; index < initial.length; index++) {
        final Object value = initial[index];

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        }
    }

    return new NativeArray(arrayData);
}
 
Example 12
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
NativeArray(final double[] array) {
    this(ArrayData.allocate(array));
}
 
Example 13
Source File: NativeArray.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
NativeArray(final long length) {
    // TODO assert valid index in long before casting
    this(ArrayData.allocate((int) length));
}
 
Example 14
Source File: NativeArray.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
NativeArray(final int[] array) {
    this(ArrayData.allocate(array));
}
 
Example 15
Source File: NativeArray.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
NativeArray(final int[] array) {
    this(ArrayData.allocate(array));
}
 
Example 16
Source File: NativeArray.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
NativeArray(final int[] array) {
    this(ArrayData.allocate(array));
}
 
Example 17
Source File: NativeArray.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
NativeArray(final double[] array) {
    this(ArrayData.allocate(array));
}
 
Example 18
Source File: NativeArray.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
NativeArray(final long length) {
    this(ArrayData.allocate(length));
}
 
Example 19
Source File: Global.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Allocate a new number array.
 *
 * @param initial number values.
 * @return the new array
 */
public static NativeArray allocate(final double[] initial) {
    return new NativeArray(ArrayData.allocate(initial));
}
 
Example 20
Source File: Global.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Allocate a new integer array.
 *
 * @param initial number values.
 * @return the new array
 */
public static NativeArray allocate(final int[] initial) {
    return new NativeArray(ArrayData.allocate(initial));
}