jdk.nashorn.internal.runtime.ConsString Java Examples

The following examples show how to use jdk.nashorn.internal.runtime.ConsString. 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: JavaArgumentConverters.java    From openjdk-jdk8u with 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 #2
Source File: ConsStringTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test charAt
 */
@Test
public void testConsStringCharAt() {
    final ConsString cs1 = new ConsString("b", "c");
    final ConsString cs2 = new ConsString("d", "e");
    final ConsString cs3 = new ConsString(cs1, cs2);
    final ConsString cs4 = new ConsString(cs3, "f");
    final ConsString cs5 = new ConsString("a", cs4);
    assertEquals(cs1.charAt(1), 'c');
    assertEquals(cs2.charAt(0), 'd');
    assertEquals(cs3.charAt(3), 'e');
    assertEquals(cs4.charAt(1), 'c');
    assertEquals(cs5.charAt(2), 'c');
    // ConsStrings should be flattened now
    assertEquals(cs1.getComponents()[0], "bc");
    assertEquals(cs1.getComponents()[1], "");
    assertEquals(cs2.getComponents()[0], "de");
    assertEquals(cs2.getComponents()[1], "");
    assertEquals(cs3.getComponents()[0], "bcde");
    assertEquals(cs3.getComponents()[1], "");
    assertEquals(cs4.getComponents()[0], "bcdef");
    assertEquals(cs4.getComponents()[1], "");
    assertEquals(cs5.getComponents()[0], "abcdef");
    assertEquals(cs5.getComponents()[1], "");
}
 
Example #3
Source File: JavaArgumentConverters.java    From hottub with 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 #4
Source File: JavaArgumentConverters.java    From openjdk-jdk8u-backup with 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 #5
Source File: JavaArgumentConverters.java    From openjdk-jdk8u-backup with 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 #6
Source File: ConsStringTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test toString conversion
 */
@Test
public void testConsStringToString() {
    final ConsString cs1 = new ConsString("b", "c");
    final ConsString cs2 = new ConsString("d", "e");
    final ConsString cs3 = new ConsString(cs1, cs2);
    final ConsString cs4 = new ConsString(cs3, "f");
    final ConsString cs5 = new ConsString("a", cs4);
    assertEquals(cs5.toString(), "abcdef");
    assertEquals(cs4.toString(), "bcdef");
    assertEquals(cs3.toString(), "bcde");
    assertEquals(cs2.toString(), "de");
    assertEquals(cs1.toString(), "bc");
    // ConsStrings should be flattened now
    assertEquals(cs1.getComponents()[0], "bc");
    assertEquals(cs1.getComponents()[1], "");
    assertEquals(cs2.getComponents()[0], "de");
    assertEquals(cs2.getComponents()[1], "");
    assertEquals(cs3.getComponents()[0], "bcde");
    assertEquals(cs3.getComponents()[1], "");
    assertEquals(cs4.getComponents()[0], "bcdef");
    assertEquals(cs4.getComponents()[1], "");
    assertEquals(cs5.getComponents()[0], "abcdef");
    assertEquals(cs5.getComponents()[1], "");
}
 
Example #7
Source File: JavaArgumentConverters.java    From openjdk-8 with 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 #8
Source File: Global.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
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 (obj instanceof String || obj instanceof ConsString) {
        return new NativeString((CharSequence)obj, this);
    } else if (obj instanceof Object[]) { // extension
        return new NativeArray((Object[])obj);
    } else if (obj instanceof double[]) { // extension
        return new NativeArray((double[])obj);
    } else if (obj instanceof long[]) {
        return new NativeArray((long[])obj);
    } else if (obj instanceof int[]) {
        return new NativeArray((int[])obj);
    } else {
        // FIXME: more special cases? Map? List?
        return obj;
    }
}
 
Example #9
Source File: ScriptObjectMirror.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Make a script object mirror on given object if needed.
 *
 * @param obj object to be wrapped/converted
 * @param homeGlobal global to which this object belongs.
 * @param jsonCompatible if true, the created wrapper will implement the Java {@code List} interface if
 * {@code obj} is a JavaScript {@code Array} object. Arrays retrieved through its properties (transitively)
 * will also implement the list interface.
 * @return wrapped/converted object
 */
private static Object wrap(final Object obj, final Object homeGlobal, final boolean jsonCompatible) {
    if(obj instanceof ScriptObject) {
        if (!(homeGlobal instanceof Global)) {
            return obj;
        }
        final ScriptObject sobj = (ScriptObject)obj;
        final Global global = (Global)homeGlobal;
        final ScriptObjectMirror mirror = new ScriptObjectMirror(sobj, global, jsonCompatible);
        if (jsonCompatible && sobj.isArray()) {
            return new JSONListAdapter(mirror, global);
        }
        return mirror;
    } else if(obj instanceof ConsString) {
        return obj.toString();
    } else if (jsonCompatible && obj instanceof ScriptObjectMirror) {
        // Since choosing JSON compatible representation is an explicit decision on user's part, if we're asked to
        // wrap a mirror that was not JSON compatible, explicitly create its compatible counterpart following the
        // principle of least surprise.
        return ((ScriptObjectMirror)obj).asJSONCompatible();
    }
    return obj;
}
 
Example #10
Source File: Global.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
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 (obj instanceof String || obj instanceof ConsString) {
        return new NativeString((CharSequence)obj, this);
    } else if (obj instanceof Object[]) { // extension
        return new NativeArray((Object[])obj);
    } else if (obj instanceof double[]) { // extension
        return new NativeArray((double[])obj);
    } else if (obj instanceof long[]) {
        return new NativeArray((long[])obj);
    } else if (obj instanceof int[]) {
        return new NativeArray((int[])obj);
    } else {
        // FIXME: more special cases? Map? List?
        return obj;
    }
}
 
Example #11
Source File: JavaArgumentConverters.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static String toString(final Object obj0) {
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof String) {
            return (String) obj;
        } else if (obj instanceof ConsString) {
            return obj.toString();
        } else if (obj instanceof Number) {
            return JSType.toString(((Number)obj).doubleValue());
        } else if (obj instanceof Boolean) {
            return ((Boolean) obj).toString();
        } else if (obj == UNDEFINED) {
            return "undefined";
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, String.class);
            continue;
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example #12
Source File: JavaArgumentConverters.java    From jdk8u_nashorn with 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 #13
Source File: JavaArgumentConverters.java    From openjdk-8-source with 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 #14
Source File: JavaArgumentConverters.java    From openjdk-8 with 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 #15
Source File: JavaArgumentConverters.java    From openjdk-jdk8u with 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 #16
Source File: JavaArgumentConverters.java    From jdk8u_nashorn with 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 #17
Source File: JavaArgumentConverters.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static String toString(final Object obj0) {
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof String) {
            return (String) obj;
        } else if (obj instanceof ConsString) {
            return obj.toString();
        } else if (obj instanceof Number) {
            return JSType.toString(((Number)obj).doubleValue());
        } else if (obj instanceof Boolean) {
            return ((Boolean) obj).toString();
        } else if (obj == UNDEFINED) {
            return "undefined";
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, String.class);
            continue;
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example #18
Source File: ConsStringTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test toString conversion
 */
@Test
public void testConsStringToString() {
    final ConsString cs1 = new ConsString("b", "c");
    final ConsString cs2 = new ConsString("d", "e");
    final ConsString cs3 = new ConsString(cs1, cs2);
    final ConsString cs4 = new ConsString(cs3, "f");
    final ConsString cs5 = new ConsString("a", cs4);
    assertEquals(cs5.toString(), "abcdef");
    assertEquals(cs4.toString(), "bcdef");
    assertEquals(cs3.toString(), "bcde");
    assertEquals(cs2.toString(), "de");
    assertEquals(cs1.toString(), "bc");
    // ConsStrings should be flattened now
    assertEquals(cs1.getComponents()[0], "bc");
    assertEquals(cs1.getComponents()[1], "");
    assertEquals(cs2.getComponents()[0], "de");
    assertEquals(cs2.getComponents()[1], "");
    assertEquals(cs3.getComponents()[0], "bcde");
    assertEquals(cs3.getComponents()[1], "");
    assertEquals(cs4.getComponents()[0], "bcdef");
    assertEquals(cs4.getComponents()[1], "");
    assertEquals(cs5.getComponents()[0], "abcdef");
    assertEquals(cs5.getComponents()[1], "");
}
 
Example #19
Source File: ConsStringTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test charAt
 */
@Test
public void testConsStringCharAt() {
    final ConsString cs1 = new ConsString("b", "c");
    final ConsString cs2 = new ConsString("d", "e");
    final ConsString cs3 = new ConsString(cs1, cs2);
    final ConsString cs4 = new ConsString(cs3, "f");
    final ConsString cs5 = new ConsString("a", cs4);
    assertEquals(cs1.charAt(1), 'c');
    assertEquals(cs2.charAt(0), 'd');
    assertEquals(cs3.charAt(3), 'e');
    assertEquals(cs4.charAt(1), 'c');
    assertEquals(cs5.charAt(2), 'c');
    // ConsStrings should be flattened now
    assertEquals(cs1.getComponents()[0], "bc");
    assertEquals(cs1.getComponents()[1], "");
    assertEquals(cs2.getComponents()[0], "de");
    assertEquals(cs2.getComponents()[1], "");
    assertEquals(cs3.getComponents()[0], "bcde");
    assertEquals(cs3.getComponents()[1], "");
    assertEquals(cs4.getComponents()[0], "bcdef");
    assertEquals(cs4.getComponents()[1], "");
    assertEquals(cs5.getComponents()[0], "abcdef");
    assertEquals(cs5.getComponents()[1], "");
}
 
Example #20
Source File: ConsStringTest.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test charAt
 */
@Test
public void testConsStringCharAt() {
    final ConsString cs1 = new ConsString("b", "c");
    final ConsString cs2 = new ConsString("d", "e");
    final ConsString cs3 = new ConsString(cs1, cs2);
    final ConsString cs4 = new ConsString(cs3, "f");
    final ConsString cs5 = new ConsString("a", cs4);
    assertEquals(cs1.charAt(1), 'c');
    assertEquals(cs2.charAt(0), 'd');
    assertEquals(cs3.charAt(3), 'e');
    assertEquals(cs4.charAt(1), 'c');
    assertEquals(cs5.charAt(2), 'c');
    // ConsStrings should be flattened now
    assertEquals(cs1.getComponents()[0], "bc");
    assertEquals(cs1.getComponents()[1], "");
    assertEquals(cs2.getComponents()[0], "de");
    assertEquals(cs2.getComponents()[1], "");
    assertEquals(cs3.getComponents()[0], "bcde");
    assertEquals(cs3.getComponents()[1], "");
    assertEquals(cs4.getComponents()[0], "bcdef");
    assertEquals(cs4.getComponents()[1], "");
    assertEquals(cs5.getComponents()[0], "abcdef");
    assertEquals(cs5.getComponents()[1], "");
}
 
Example #21
Source File: ScriptObjectMirror.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Make a script object mirror on given object if needed.
 *
 * @param obj object to be wrapped/converted
 * @param homeGlobal global to which this object belongs.
 * @param jsonCompatible if true, the created wrapper will implement the Java {@code List} interface if
 * {@code obj} is a JavaScript {@code Array} object. Arrays retrieved through its properties (transitively)
 * will also implement the list interface.
 * @return wrapped/converted object
 */
private static Object wrap(final Object obj, final Object homeGlobal, final boolean jsonCompatible) {
    if(obj instanceof ScriptObject) {
        if (!(homeGlobal instanceof Global)) {
            return obj;
        }
        final ScriptObject sobj = (ScriptObject)obj;
        final Global global = (Global)homeGlobal;
        final ScriptObjectMirror mirror = new ScriptObjectMirror(sobj, global, jsonCompatible);
        if (jsonCompatible && sobj.isArray()) {
            return new JSONListAdapter(mirror, global);
        }
        return mirror;
    } else if(obj instanceof ConsString) {
        return obj.toString();
    } else if (jsonCompatible && obj instanceof ScriptObjectMirror) {
        // Since choosing JSON compatible representation is an explicit decision on user's part, if we're asked to
        // wrap a mirror that was not JSON compatible, explicitly create its compatible counterpart following the
        // principle of least surprise.
        return ((ScriptObjectMirror)obj).asJSONCompatible();
    }
    return obj;
}
 
Example #22
Source File: JavaArgumentConverters.java    From TencentKona-8 with 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 #23
Source File: ConsStringTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test toString conversion
 */
@Test
public void testConsStringToString() {
    final ConsString cs1 = new ConsString("b", "c");
    final ConsString cs2 = new ConsString("d", "e");
    final ConsString cs3 = new ConsString(cs1, cs2);
    final ConsString cs4 = new ConsString(cs3, "f");
    final ConsString cs5 = new ConsString("a", cs4);
    assertEquals(cs5.toString(), "abcdef");
    assertEquals(cs4.toString(), "bcdef");
    assertEquals(cs3.toString(), "bcde");
    assertEquals(cs2.toString(), "de");
    assertEquals(cs1.toString(), "bc");
    // ConsStrings should be flattened now
    assertEquals(cs1.getComponents()[0], "bc");
    assertEquals(cs1.getComponents()[1], "");
    assertEquals(cs2.getComponents()[0], "de");
    assertEquals(cs2.getComponents()[1], "");
    assertEquals(cs3.getComponents()[0], "bcde");
    assertEquals(cs3.getComponents()[1], "");
    assertEquals(cs4.getComponents()[0], "bcdef");
    assertEquals(cs4.getComponents()[1], "");
    assertEquals(cs5.getComponents()[0], "abcdef");
    assertEquals(cs5.getComponents()[1], "");
}
 
Example #24
Source File: ConsStringTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test toString conversion
 */
@Test
public void testConsStringToString() {
    final ConsString cs1 = new ConsString("b", "c");
    final ConsString cs2 = new ConsString("d", "e");
    final ConsString cs3 = new ConsString(cs1, cs2);
    final ConsString cs4 = new ConsString(cs3, "f");
    final ConsString cs5 = new ConsString("a", cs4);
    assertEquals(cs5.toString(), "abcdef");
    assertEquals(cs4.toString(), "bcdef");
    assertEquals(cs3.toString(), "bcde");
    assertEquals(cs2.toString(), "de");
    assertEquals(cs1.toString(), "bc");
    // ConsStrings should be flattened now
    assertEquals(cs1.getComponents()[0], "bc");
    assertEquals(cs1.getComponents()[1], "");
    assertEquals(cs2.getComponents()[0], "de");
    assertEquals(cs2.getComponents()[1], "");
    assertEquals(cs3.getComponents()[0], "bcde");
    assertEquals(cs3.getComponents()[1], "");
    assertEquals(cs4.getComponents()[0], "bcdef");
    assertEquals(cs4.getComponents()[1], "");
    assertEquals(cs5.getComponents()[0], "abcdef");
    assertEquals(cs5.getComponents()[1], "");
}
 
Example #25
Source File: JavaArgumentConverters.java    From openjdk-jdk9 with 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 #26
Source File: NativeString.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static String getString(final Object self) {
    if (self instanceof String) {
        return (String)self;
    } else if (self instanceof ConsString) {
        return self.toString();
    } else if (self instanceof NativeString) {
        return ((NativeString)self).getStringValue();
    } else if (self != null && self == Global.instance().getStringPrototype()) {
        return "";
    } else {
        throw typeError("not.a.string", ScriptRuntime.safeToString(self));
    }
}
 
Example #27
Source File: NativeString.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static String getString(final Object self) {
    if (self instanceof String) {
        return (String)self;
    } else if (self instanceof ConsString) {
        return self.toString();
    } else if (self instanceof NativeString) {
        return ((NativeString)self).getStringValue();
    } else if (self != null && self == Global.instance().getStringPrototype()) {
        return "";
    } else {
        throw typeError("not.a.string", ScriptRuntime.safeToString(self));
    }
}
 
Example #28
Source File: NativeString.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Combines ECMA 9.10 CheckObjectCoercible and ECMA 9.8 ToString with a shortcut for strings.
 *
 * @param self the object
 * @return the object as string
 */
private static String checkObjectToString(final Object self) {
    if (self instanceof String) {
        return (String)self;
    } else if (self instanceof ConsString) {
        return self.toString();
    } else {
        Global.checkObjectCoercible(self);
        return JSType.toString(self);
    }
}
 
Example #29
Source File: JavaArgumentConverters.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private static Boolean toBoolean(final Object obj) {
    if (obj instanceof Boolean) {
        return (Boolean) obj;
    }

    if (obj == null) {
        // NOTE: FindBugs complains here about the NP_BOOLEAN_RETURN_NULL pattern: we're returning null from a
        // method that has a return type of Boolean, as it is worried about a NullPointerException if there's a
        // conversion to a primitive boolean. We know what we're doing, though. We're using a separate method when
        // we're converting Object to a primitive boolean - see how the CONVERTERS map is populated. We specifically
        // want to have null and Undefined to be converted to a (Boolean)null when being passed to a Java method
        // that expects a Boolean argument.
        // TODO: if/when we're allowed to use FindBugs at build time, we can use annotations to disable this warning
        return null;
    }

    if (obj == UNDEFINED) {
        // NOTE: same reasoning for FindBugs NP_BOOLEAN_RETURN_NUL warning as in the preceding comment.
        return null;
    }

    if (obj instanceof Number) {
        final double num = ((Number) obj).doubleValue();
        return num != 0 && !Double.isNaN(num);
    }

    if (obj instanceof String || obj instanceof ConsString) {
        return ((CharSequence) obj).length() > 0;
    }

    if (obj instanceof ScriptObject) {
        return true;
    }

    throw assertUnexpectedType(obj);
}
 
Example #30
Source File: NativeString.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static String getString(final Object self) {
    if (self instanceof String) {
        return (String)self;
    } else if (self instanceof ConsString) {
        return self.toString();
    } else if (self instanceof NativeString) {
        return ((NativeString)self).getStringValue();
    } else if (self != null && self == Global.instance().getStringPrototype()) {
        return "";
    } else {
        throw typeError("not.a.string", ScriptRuntime.safeToString(self));
    }
}