Java Code Examples for jdk.nashorn.internal.runtime.JSType

The following examples show how to use jdk.nashorn.internal.runtime.JSType. These examples are extracted from open source projects. 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 Project: jdk8u_nashorn   Author: JetBrains   File: JSTypeTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of toString method, of class Runtime.
 */
@Test
public void testToString_Object() {
    assertEquals(JSType.toString(ScriptRuntime.UNDEFINED), "undefined");
    assertEquals(JSType.toString(null), "null");
    assertEquals(JSType.toString(Boolean.TRUE), "true");
    assertEquals(JSType.toString(Boolean.FALSE), "false");
    assertEquals(JSType.toString(""), "");
    assertEquals(JSType.toString("nashorn"), "nashorn");
    assertEquals(JSType.toString(Double.NaN), "NaN");
    assertEquals(JSType.toString(Double.POSITIVE_INFINITY), "Infinity");
    assertEquals(JSType.toString(Double.NEGATIVE_INFINITY), "-Infinity");
    assertEquals(JSType.toString(0.0), "0");
    // FIXME: add more number-to-string test cases
    // FIXME: add case for Object type (JSObject with getDefaultValue)
}
 
Example #2
Source Project: hottub   Author: dsrg-uoft   File: NativeArray.java    License: 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 #3
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: NativeDate.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static double[] convertCtorArgs(final Object[] args) {
    final double[] d = new double[7];
    boolean nullReturn = false;

    // should not bailout on first NaN or infinite. Need to convert all
    // subsequent args for possible side-effects via valueOf/toString overrides
    // on argument objects.
    for (int i = 0; i < d.length; i++) {
        if (i < args.length) {
            final double darg = JSType.toNumber(args[i]);
            if (isNaN(darg) || isInfinite(darg)) {
                nullReturn = true;
            }

            d[i] = (long)darg;
        } else {
            d[i] = i == 2 ? 1 : 0; // day in month defaults to 1
        }
    }

    if (0 <= d[0] && d[0] <= 99) {
        d[0] += 1900;
    }

    return nullReturn? null : d;
}
 
Example #4
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: NativeDate.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static double[] convertCtorArgs(final Object[] args) {
    final double[] d = new double[7];
    boolean nullReturn = false;

    // should not bailout on first NaN or infinite. Need to convert all
    // subsequent args for possible side-effects via valueOf/toString overrides
    // on argument objects.
    for (int i = 0; i < d.length; i++) {
        if (i < args.length) {
            final double darg = JSType.toNumber(args[i]);
            if (isNaN(darg) || isInfinite(darg)) {
                nullReturn = true;
            }

            d[i] = (long)darg;
        } else {
            d[i] = i == 2 ? 1 : 0; // day in month defaults to 1
        }
    }

    if (0 <= d[0] && d[0] <= 99) {
        d[0] += 1900;
    }

    return nullReturn? null : d;
}
 
Example #5
Source Project: jdk8u60   Author: chenghanpeng   File: NativeArray.java    License: 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 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 len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError(Context.getGlobal(), e, "not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example #6
Source Project: jdk8u60   Author: chenghanpeng   File: NativeArray.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.5 Array.prototype.join (separator)
 *
 * @param self      self reference
 * @param separator element separator
 * @return string representation after join
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String join(final Object self, final Object separator) {
    final StringBuilder    sb   = new StringBuilder();
    final Iterator<Object> iter = arrayLikeIterator(self, true);
    final String           sep  = separator == ScriptRuntime.UNDEFINED ? "," : JSType.toString(separator);

    while (iter.hasNext()) {
        final Object obj = iter.next();

        if (obj != null && obj != ScriptRuntime.UNDEFINED) {
            sb.append(JSType.toString(obj));
        }

        if (iter.hasNext()) {
            sb.append(sep);
        }
    }

    return sb.toString();
}
 
Example #7
Source Project: openjdk-8-source   Author: keerath   File: NativeObject.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.2.3.2 Object.getPrototypeOf ( O )
 *
 * @param  self self reference
 * @param  obj object to get prototype from
 * @return the prototype of an object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object getPrototypeOf(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).getProto();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).getProto();
    } else {
        final JSType type = JSType.of(obj);
        if (type == JSType.OBJECT) {
            // host (Java) objects have null __proto__
            return null;
        }

        // must be some JS primitive
        throw notAnObject(obj);
    }
}
 
Example #8
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: JSTypeTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of JSType.toUint32(double)
 */
@Test
public void testToUint32() {
    assertEquals(JSType.toUint32(+0.0), 0);
    assertEquals(JSType.toUint32(-0.0), 0);
    assertEquals(JSType.toUint32(Double.NaN), 0);
    assertEquals(JSType.toUint32(Double.POSITIVE_INFINITY), 0);
    assertEquals(JSType.toUint32(Double.NEGATIVE_INFINITY), 0);
    assertEquals(JSType.toUint32(9223372036854775807.0d), 0);
    assertEquals(JSType.toUint32(-9223372036854775807.0d), 0);
    assertEquals(JSType.toUint32(1099511627776.0d), 0);
    assertEquals(JSType.toUint32(-1099511627776.0d), 0);
    assertEquals(JSType.toUint32(4294967295.0d), 4294967295l);
    assertEquals(JSType.toUint32(4294967296.0d), 0);
    assertEquals(JSType.toUint32(4294967297.0d), 1);
    assertEquals(JSType.toUint32(-4294967295.0d), 1);
    assertEquals(JSType.toUint32(-4294967296.0d), 0);
    assertEquals(JSType.toUint32(-4294967297.0d), 4294967295l);
    assertEquals(JSType.toUint32(4294967295.6d), 4294967295l);
    assertEquals(JSType.toUint32(4294967296.6d), 0);
    assertEquals(JSType.toUint32(4294967297.6d), 1);
    assertEquals(JSType.toUint32(-4294967295.6d), 1);
    assertEquals(JSType.toUint32(-4294967296.6d), 0);
    assertEquals(JSType.toUint32(-4294967297.6d), 4294967295l);
}
 
Example #9
Source Project: hottub   Author: dsrg-uoft   File: JavaArgumentConverters.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Number toNumber(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Number) {
            return (Number) obj;
        } else if (obj instanceof String) {
            return JSType.toNumber((String) obj);
        } else if (obj instanceof ConsString) {
            return JSType.toNumber(obj.toString());
        } else if (obj instanceof Boolean) {
            return (Boolean) obj ? 1 : +0.0;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return Double.NaN;
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example #10
Source Project: jdk8u60   Author: chenghanpeng   File: JavaArgumentConverters.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Number toNumber(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Number) {
            return (Number) obj;
        } else if (obj instanceof String) {
            return JSType.toNumber((String) obj);
        } else if (obj instanceof ConsString) {
            return JSType.toNumber(obj.toString());
        } else if (obj instanceof Boolean) {
            return (Boolean) obj ? 1 : +0.0;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return Double.NaN;
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example #11
Source Project: hottub   Author: dsrg-uoft   File: JavaArgumentConverters.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Double toDouble(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Double) {
            return (Double) obj;
        } else if (obj instanceof Number) {
            return ((Number)obj).doubleValue();
        } else if (obj instanceof String) {
            return JSType.toNumber((String) obj);
        } else if (obj instanceof ConsString) {
            return JSType.toNumber(obj.toString());
        } else if (obj instanceof Boolean) {
            return (Boolean) obj ? 1 : +0.0;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return Double.NaN;
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example #12
Source Project: nashorn   Author: ynsn   File: NativeArray.java    License: 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 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)) {
            if (sobj.getArray().length() + args.length <= JSType.MAX_UINT) {
                final ArrayData newData = sobj.getArray().push(true, args);
                sobj.setArray(newData);
                return newData.length();
            }
            //fallthru
        }

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

        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example #13
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: NativeArray.java    License: 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 #14
Source Project: TencentKona-8   Author: Tencent   File: NativeArray.java    License: 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 #15
Source Project: jdk8u_nashorn   Author: JetBrains   File: NativeArray.java    License: 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 #16
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: NativeDataView.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set 8-bit unsigned int at the given byteOffset
 *
 * @param self DataView object
 * @param byteOffset byte offset to write at
 * @param value byte value to set
 * @return undefined
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2)
public static Object setUint8(final Object self, final Object byteOffset, final Object value) {
    try {
        getBuffer(self).put(JSType.toInt32(byteOffset), (byte)JSType.toInt32(value));
        return UNDEFINED;
    } catch (final IllegalArgumentException iae) {
        throw rangeError(iae, "dataview.offset");
    }
}
 
Example #17
Source Project: TencentKona-8   Author: Tencent   File: NativeDataView.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set 16-bit signed int at the given byteOffset
 *
 * @param self DataView object
 * @param byteOffset byte offset to write at
 * @param value short value to set
 * @param littleEndian (optional) flag indicating whether to write in little endian order
 * @return undefined
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2)
public static Object setInt16(final Object self, final Object byteOffset, final Object value, final Object littleEndian) {
    try {
        getBuffer(self, littleEndian).putShort(JSType.toInt32(byteOffset), (short)JSType.toInt32(value));
        return UNDEFINED;
    } catch (final IllegalArgumentException iae) {
        throw rangeError(iae, "dataview.offset");
    }
}
 
Example #18
Source Project: jdk8u60   Author: chenghanpeng   File: NativeString.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final Object self = request.getReceiver();
    final Class<?> returnType = desc.getMethodType().returnType();

    if (returnType == Object.class && JSType.isString(self)) {
        try {
            return new GuardedInvocation(MH.findStatic(MethodHandles.lookup(), NativeString.class, "get", desc.getMethodType()), NashornGuards.getInstanceOf2Guard(String.class, ConsString.class));
        } catch (final LookupException e) {
            //empty. Shouldn't happen. Fall back to super
        }
    }
    return super.findGetIndexMethod(desc, request);
}
 
Example #19
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: NativeMap.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a canonicalized key object by converting numbers to their narrowest representation and
 * ConsStrings to strings. Conversion of Double to Integer also takes care of converting -0 to 0
 * as required by step 6 of ECMA6 23.1.3.9.
 *
 * @param key a key
 * @return the canonical key
 */
static Object convertKey(final Object key) {
    if (key instanceof ConsString) {
        return key.toString();
    }
    if (key instanceof Double) {
        final Double d = (Double) key;
        if (JSType.isRepresentableAsInt(d.doubleValue())) {
            return d.intValue();
        }
    }
    return key;
}
 
Example #20
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: NativeURIError.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
NativeURIError(final Object msg, final Global global) {
    super(global.getURIErrorPrototype(), $nasgenmap$);
    if (msg != UNDEFINED) {
        this.instMessage = JSType.toString(msg);
    } else {
        this.delete(NativeError.MESSAGE, false);
    }
    NativeError.initException(this);
}
 
Example #21
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeArrayBuffer.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 * @param newObj is this invoked with new
 * @param self   self reference
 * @param args   arguments to constructor
 * @return new NativeArrayBuffer
 */
@Constructor(arity = 1)
public static NativeArrayBuffer constructor(final boolean newObj, final Object self, final Object... args) {
    if (!newObj) {
        throw typeError("constructor.requires.new", "ArrayBuffer");
    }

    if (args.length == 0) {
        return new NativeArrayBuffer(0);
    }

    return new NativeArrayBuffer(JSType.toInt32(args[0]));
}
 
Example #22
Source Project: hottub   Author: dsrg-uoft   File: NativeDataView.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set 64-bit float at the given byteOffset
 *
 * @param self DataView object
 * @param byteOffset byte offset to write at
 * @param value double value to set
 * @param littleEndian (optional) flag indicating whether to write in little endian order
 * @return undefined
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2)
public static Object setFloat64(final Object self, final Object byteOffset, final Object value, final Object littleEndian) {
    try {
        getBuffer(self, littleEndian).putDouble((int)JSType.toUint32(byteOffset), JSType.toNumber(value));
        return UNDEFINED;
    } catch (final IllegalArgumentException iae) {
        throw rangeError(iae, "dataview.offset");
    }
}
 
Example #23
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeJSAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean has(final int key) {
    if (overrides && super.hasOwnProperty(key)) {
        return true;
    }

    return JSType.toBoolean(callAdaptee(Boolean.FALSE, __has__, key));
}
 
Example #24
Source Project: openjdk-8   Author: bpupadhyaya   File: ScriptObjectMirror.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public double toNumber() {
    return inGlobal(new Callable<Double>() {
        @Override public Double call() {
            return JSType.toNumber(sobj);
        }
    });
}
 
Example #25
Source Project: jdk8u_nashorn   Author: JetBrains   File: NativeDataView.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set 32-bit unsigned int at the given byteOffset
 *
 * @param self DataView object
 * @param byteOffset byte offset to write at
 * @param value int value to set
 * @return undefined
 */
@SpecializedFunction
public static Object setUint32(final Object self, final int byteOffset, final double value) {
    try {
        getBuffer(self, false).putInt(byteOffset, (int) JSType.toUint32(value));
        return UNDEFINED;
    } catch (final IllegalArgumentException iae) {
        throw rangeError(iae, "dataview.offset");
    }
}
 
Example #26
Source Project: TencentKona-8   Author: Tencent   File: NativeDataView.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get 32-bit signed int from given byteOffset
 *
 * @param self DataView object
 * @param byteOffset byte offset to read from
 * @param littleEndian (optional) flag indicating whether to read in little endian order
 * @return 32-bit signed int value at the byteOffset
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static int getInt32(final Object self, final Object byteOffset, final Object littleEndian) {
    try {
        return getBuffer(self, littleEndian).getInt(JSType.toInt32(byteOffset));
    } catch (final IllegalArgumentException iae) {
        throw rangeError(iae, "dataview.offset");
    }
}
 
Example #27
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: NativeString.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.15 String.prototype.substring (start, end)
 *
 * @param self  self reference
 * @param start start position of substring
 * @param end   end position of substring
 * @return substring given start and end indexes
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String substring(final Object self, final Object start, final Object end) {

    final String str = checkObjectToString(self);
    if (end == UNDEFINED) {
        return substring(str, JSType.toInteger(start));
    }
    return substring(str, JSType.toInteger(start), JSType.toInteger(end));
}
 
Example #28
Source Project: TencentKona-8   Author: Tencent   File: ArrayLikeIterator.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ArrayLikeIterator factory (reverse order)
 * @param object object over which to do reverse element iteration
 * @param includeUndefined should undefined elements be included in the iteration
 * @return iterator
 */
public static ArrayLikeIterator<Object> reverseArrayLikeIterator(final Object object, final boolean includeUndefined) {
    Object obj = object;

    if (ScriptObject.isArray(obj)) {
        return new ReverseScriptArrayIterator((ScriptObject) obj, includeUndefined);
    }

    obj = JSType.toScriptObject(obj);
    if (obj instanceof ScriptObject) {
        return new ReverseScriptObjectIterator((ScriptObject)obj, includeUndefined);
    }

    if (obj instanceof JSObject) {
        return new ReverseJSObjectIterator((JSObject)obj, includeUndefined);
    }

    if (obj instanceof List) {
        return new ReverseJavaListIterator((List<?>)obj, includeUndefined);
    }

    if (obj != null && obj.getClass().isArray()) {
        return new ReverseJavaArrayIterator(obj, includeUndefined);
    }

    return new EmptyArrayLikeIterator();
}
 
Example #29
Source Project: openjdk-8   Author: bpupadhyaya   File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.2.1 , 15.2.1.1 new Object([value]) and Object([value])
 *
 * Constructor
 *
 * @param newObj is the new object instantiated with the new operator
 * @param self   self reference
 * @param value  value of object to be instantiated
 * @return the new NativeObject
 */
@Constructor
public static Object construct(final boolean newObj, final Object self, final Object value) {
    final JSType type = JSType.of(value);

    // Object(null), Object(undefined), Object() are same as "new Object()"

    if (newObj || (type == JSType.NULL || type == JSType.UNDEFINED)) {
        switch (type) {
        case BOOLEAN:
        case NUMBER:
        case STRING:
            return Global.toObject(value);
        case OBJECT:
        case FUNCTION:
            return value;
        case NULL:
        case UNDEFINED:
            // fall through..
        default:
            break;
        }

        return Global.newEmptyInstance();
    }

    return Global.toObject(value);
}
 
Example #30
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeDate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor - ECMA 15.9.3.1 new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )
 *
 * @param isNew is this Date constructed with the new operator
 * @param self  self reference
 * @param args  arguments
 * @return new Date
 */
@Constructor(arity = 7)
public static Object construct(final boolean isNew, final Object self, final Object... args) {
    if (! isNew) {
        return toStringImpl(new NativeDate(), FORMAT_DATE_TIME);
    }

    NativeDate result;
    switch (args.length) {
    case 0:
        result = new NativeDate();
        break;

    case 1:
        double num;
        final Object arg = JSType.toPrimitive(args[0]);
        if (JSType.isString(arg)) {
            num = parseDateString(arg.toString());
        } else {
            num = timeClip(JSType.toNumber(args[0]));
        }
        result = new NativeDate(num);
        break;

    default:
        result = new NativeDate(0);
        final double[] d = convertCtorArgs(args);
        if (d == null) {
            result.setTime(Double.NaN);
        } else {
            final double time = timeClip(utc(makeDate(d), result.getTimeZone()));
            result.setTime(time);
        }
        break;
     }

     return result;
}