jdk.nashorn.internal.runtime.arrays.ArrayData Java Examples

The following examples show how to use jdk.nashorn.internal.runtime.arrays.ArrayData. 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: NativeArray.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.7 Array.prototype.push (args...) specialized for single object argument
 *
 * @param self self reference
 * @param arg argument to push
 * @return array after pushes
 */
@SpecializedFunction
public static double push(final Object self, final Object arg) {
    try {
        final ScriptObject sobj = (ScriptObject)self;
        final ArrayData arrayData = sobj.getArray();
        final long length = arrayData.length();
        if (bulkable(sobj) && length < JSType.MAX_UINT) {
            sobj.setArray(arrayData.push(true, arg));
            return length + 1;
        }

        long len = JSType.toUint32(sobj.getLength());
        sobj.set(len++, arg, CALLSITE_STRICT);
        sobj.set("length", len, CALLSITE_STRICT);
        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example #2
Source File: ScriptObject.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final int key, final int value, final int callSiteFlags) {
    final int index = getArrayIndex(key);
    if (isValidArrayIndex(index)) {
        if (getArray().has(index)) {
            final ArrayData data = getArray();
            setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
        } else {
            doesNotHave(index, value, callSiteFlags);
        }
        return;
    }

    final String propName = JSType.toString(key);
    setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
}
 
Example #3
Source File: ScriptObject.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final double key, final double value, final int callSiteFlags) {
    final int index = getArrayIndex(key);

    if (isValidArrayIndex(index)) {
        final ArrayData data = getArray();
        if (data.has(index)) {
            setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
        } else {
            doesNotHave(index, value, callSiteFlags);
        }

        return;
    }

    final String propName = JSType.toString(key);
    setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
}
 
Example #4
Source File: Global.java    From hottub 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 #5
Source File: ScriptObject.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final Object key, final double value, final int callSiteFlags) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int    index        = getArrayIndex(primitiveKey);

    if (isValidArrayIndex(index)) {
        final ArrayData data = getArray();
        if (data.has(index)) {
            setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
        } else {
            doesNotHave(index, value, callSiteFlags);
        }

        return;
    }

    final String propName = JSType.toString(primitiveKey);
    setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
}
 
Example #6
Source File: NativeArray.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.7 Array.prototype.push (args...) specialized for single object argument
 *
 * @param self self reference
 * @param arg argument to push
 * @return array after pushes
 */
@SpecializedFunction
public static double push(final Object self, final Object arg) {
    try {
        final ScriptObject sobj = (ScriptObject)self;
        final ArrayData arrayData = sobj.getArray();
        final long length = arrayData.length();
        if (bulkable(sobj) && length < JSType.MAX_UINT) {
            sobj.setArray(arrayData.push(true, arg));
            return length + 1;
        }

        long len = JSType.toUint32(sobj.getLength());
        sobj.set(len++, arg, CALLSITE_STRICT);
        sobj.set("length", len, CALLSITE_STRICT);
        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example #7
Source File: ScriptObject.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final int key, final double value, final int callSiteFlags) {
    final int index = getArrayIndex(key);

    if (isValidArrayIndex(index)) {
        final ArrayData data = getArray();
        if (data.has(index)) {
            setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
        } else {
            doesNotHave(index, value, callSiteFlags);
        }

        return;
    }

    final String propName = JSType.toString(key);
    setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
}
 
Example #8
Source File: NativeArray.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.7 Array.prototype.push (args...)
 *
 * @param self self reference
 * @param args arguments to push
 * @return array length after pushes
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object push(final Object self, final Object... args) {
    try {
        final ScriptObject sobj   = (ScriptObject)self;

        if (bulkable(sobj) && sobj.getArray().length() + args.length <= JSType.MAX_UINT) {
            final ArrayData newData = sobj.getArray().push(true, args);
            sobj.setArray(newData);
            return JSType.toNarrowestNumber(newData.length());
        }

        long len = JSType.toUint32(sobj.getLength());
        for (final Object element : args) {
            sobj.set(len++, element, CALLSITE_STRICT);
        }
        sobj.set("length", len, CALLSITE_STRICT);

        return JSType.toNarrowestNumber(len);
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError(Context.getGlobal(), e, "not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example #9
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 #10
Source File: NativeArray.java    From openjdk-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();
    arrayData.ensure(array.length - 1);

    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 #11
Source File: ScriptObject.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final Object key, final Object value, final int callSiteFlags) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int    index        = getArrayIndex(primitiveKey);

    if (isValidArrayIndex(index)) {
        final ArrayData data = getArray();
        if (data.has(index)) {
            setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
        } else {
            doesNotHave(index, value, callSiteFlags);
        }

        return;
    }

    final String propName = JSType.toString(primitiveKey);
    setObject(findProperty(propName, true), callSiteFlags, propName, value);
}
 
Example #12
Source File: ScriptObject.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public double getDouble(final int key, final int programPoint) {
    final int       index = getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return isValid(programPoint) ? array.getDoubleOptimistic(key, programPoint) : array.getDouble(key);
    }

    return getDouble(index, JSType.toString(key), programPoint);
}
 
Example #13
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getLong(final double key) {
    final int index = ArrayIndex.getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return array.getLong(index);
    }

    return getLong(index, JSType.toString(key));
}
 
Example #14
Source File: ScriptObject.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private void checkIntegerKey(final String key) {
    final int index = getArrayIndex(key);

    if (isValidArrayIndex(index)) {
        final ArrayData data = getArray();

        if (data.has(index)) {
            setArray(data.delete(index));
        }
    }
}
 
Example #15
Source File: ScriptObject.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public double getDouble(final int key, final int programPoint) {
    final int       index = getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return isValid(programPoint) ? array.getDoubleOptimistic(key, programPoint) : array.getDouble(key);
    }

    return getDouble(index, JSType.toString(key), programPoint);
}
 
Example #16
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getInt(final long key) {
    final int index = ArrayIndex.getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return array.getInt(index);
    }

    return getInt(index, JSType.toString(key));
}
 
Example #17
Source File: ScriptObject.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
* Constructor
*
* @param map {@link PropertyMap} used to create the initial object
*/
public ScriptObject(final PropertyMap map) {
    if (Context.DEBUG) {
        ScriptObject.count.increment();
    }
    this.arrayData = ArrayData.EMPTY_ARRAY;
    this.setMap(map == null ? PropertyMap.newMap() : map);
}
 
Example #18
Source File: ScriptObject.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public double getDouble(final Object key, final int programPoint) {
    final Object    primitiveKey = JSType.toPrimitive(key, String.class);
    final int       index        = getArrayIndex(primitiveKey);
    final ArrayData array        = getArray();

    if (array.has(index)) {
        return isValid(programPoint) ? array.getDoubleOptimistic(index, programPoint) : array.getDouble(index);
    }

    return getDouble(index, JSType.toString(primitiveKey), programPoint);
}
 
Example #19
Source File: ScriptObject.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean delete(final double key, final boolean strict) {
    final int index = getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        if (array.canDelete(index, strict)) {
            setArray(array.delete(index));
            return true;
        }
        return false;
    }

    return deleteObject(JSType.toObject(key), strict);
}
 
Example #20
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getInt(final int key) {
    final ArrayData array = getArray();

    if (array.has(key)) {
        return array.getInt(key);
    }

    return getInt(key, JSType.toString(key));
}
 
Example #21
Source File: ScriptObject.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getLong(final Object key, final int programPoint) {
    final Object    primitiveKey = JSType.toPrimitive(key, String.class);
    final int       index        = getArrayIndex(primitiveKey);
    final ArrayData array        = getArray();

    if (array.has(index)) {
        return isValid(programPoint) ? array.getLongOptimistic(index, programPoint) : array.getLong(index);
    }

    return getLong(index, JSType.toString(primitiveKey), programPoint);
}
 
Example #22
Source File: JSONParser.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static ArrayData addArrayElement(final ArrayData arrayData, final int index, final Object value) {
    final long oldLength = arrayData.length();
    final long longIndex = ArrayIndex.toLongIndex(index);
    ArrayData newArrayData = arrayData;
    if (longIndex >= oldLength) {
        newArrayData = newArrayData.ensure(longIndex);
        if (longIndex > oldLength) {
            newArrayData = newArrayData.delete(oldLength, longIndex - 1);
        }
    }
    return newArrayData.set(index, value, false);
}
 
Example #23
Source File: ScriptObject.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getInt(final int key, final int programPoint) {
    final int       index = getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return isValid(programPoint) ? array.getIntOptimistic(key, programPoint) : array.getInt(key);
    }

    return getInt(index, JSType.toString(key), programPoint);
}
 
Example #24
Source File: NativeRegExpExecResult.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
NativeRegExpExecResult(final RegExpResult result, final Global global) {
    super(global.getArrayPrototype(), global.getRegExpExecResultMap());
    setIsArray();
    this.setArray(ArrayData.allocate(result.getGroups().clone()));
    this.index = result.getIndex();
    this.input = result.getInput();
}
 
Example #25
Source File: CodeGenerator.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load a constant from the constant array. This is only public to be callable from the objects
 * subpackage. Do not call directly.
 *
 * @param object object to load
 */
void loadConstant(final Object object) {
    final String       unitClassName = unit.getUnitClassName();
    final ClassEmitter classEmitter  = unit.getClassEmitter();
    final int          index         = compiler.getConstantData().add(object);
    final Class<?>     cls           = object.getClass();

    if (cls == PropertyMap.class) {
        method.load(index);
        method.invokestatic(unitClassName, GET_MAP.symbolName(), methodDescriptor(PropertyMap.class, int.class));
        classEmitter.needGetConstantMethod(PropertyMap.class);
    } else if (cls.isArray()) {
        method.load(index);
        final String methodName = ClassEmitter.getArrayMethodName(cls);
        method.invokestatic(unitClassName, methodName, methodDescriptor(cls, int.class));
        classEmitter.needGetConstantMethod(cls);
    } else {
        method.loadConstants().load(index).arrayload();
        if (object instanceof ArrayData) {
            // avoid cast to non-public ArrayData subclass
            method.checkcast(ArrayData.class);
            method.invoke(virtualCallNoLookup(ArrayData.class, "copy", ArrayData.class));
        } else if (cls != Object.class) {
            method.checkcast(cls);
        }
    }
}
 
Example #26
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private void removeArraySlot(final String key) {
    final int index = ArrayIndex.getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        setArray(array.delete(index));
    }
}
 
Example #27
Source File: ScriptObject.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 8.12.1 [[GetOwnProperty]] (P)
 *
 * @param key property key
 *
 * @return Returns the Property Descriptor of the named own property of this
 * object, or undefined if absent.
 */
public Object getOwnPropertyDescriptor(final String key) {
    final Property property = getMap().findProperty(key);

    final GlobalObject global = (GlobalObject)Context.getGlobalTrusted();

    if (property != null) {
        final ScriptFunction get   = property.getGetterFunction(this);
        final ScriptFunction set   = property.getSetterFunction(this);

        final boolean configurable = property.isConfigurable();
        final boolean enumerable   = property.isEnumerable();
        final boolean writable     = property.isWritable();

        if (property instanceof UserAccessorProperty) {
            return global.newAccessorDescriptor(
                (get != null) ?
                    get :
                    UNDEFINED,
                (set != null) ?
                    set :
                    UNDEFINED,
                configurable,
                enumerable);
        }

        return global.newDataDescriptor(getWithProperty(property), configurable, enumerable, writable);
    }

    final int index = getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return array.getDescriptor(global, index);
    }

    return UNDEFINED;
}
 
Example #28
Source File: ScriptObject.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getInt(final Object key) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = getArrayIndex(primitiveKey);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return array.getInt(index);
    }

    return getInt(index, JSType.toString(primitiveKey));
}
 
Example #29
Source File: ScriptObject.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getLong(final Object key) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = getArrayIndex(primitiveKey);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return array.getLong(index);
    }

    return getLong(index, JSType.toString(primitiveKey));
}
 
Example #30
Source File: ScriptObject.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getInt(final int key) {
    final int index = getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return array.getInt(index);
    }

    return getInt(index, JSType.toString(key));
}