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

The following examples show how to use jdk.nashorn.internal.runtime.ConsString. 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
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 2
@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 3
Source Project: TencentKona-8   Source File: ConsStringTest.java    License: 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 4
Source Project: jdk8u60   Source File: ConsStringTest.java    License: 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 5
Source Project: openjdk-8-source   Source File: Global.java    License: 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 6
@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 7
@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
@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 9
@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: openjdk-jdk8u   Source File: ConsStringTest.java    License: 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 11
Source Project: openjdk-jdk8u   Source File: ConsStringTest.java    License: 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 12
Source Project: jdk8u_nashorn   Source File: ConsStringTest.java    License: 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 13
/**
 * 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 14
@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 15
@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 16
@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
/**
 * 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 18
@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 19
Source Project: openjdk-8   Source File: Global.java    License: 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 20
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 21
@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 22
@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
@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 24
Source Project: openjdk-jdk9   Source File: ConsStringTest.java    License: 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
/**
 * 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 26
Source Project: openjdk-8   Source File: NativeString.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static CharSequence getCharSequence(final Object self) {
    if (self instanceof String || self instanceof ConsString) {
        return (CharSequence)self;
    } else if (self instanceof NativeString) {
        return ((NativeString)self).getValue();
    } else if (self != null && self == Global.instance().getStringPrototype()) {
        return "";
    } else {
        throw typeError("not.a.string", ScriptRuntime.safeToString(self));
    }
}
 
Example 27
Source Project: TencentKona-8   Source File: NativeString.java    License: 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 Project: TencentKona-8   Source File: NativeString.java    License: 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
static Object exportArgument(final Object arg, final boolean mirrorAlways) {
    if (arg instanceof ConsString) {
        return arg.toString();
    } else if (mirrorAlways && arg instanceof ScriptObject) {
        return ScriptUtils.wrap((ScriptObject)arg);
    } else {
        return arg;
    }
}
 
Example 30
@Override
public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) {
    if (sourceType == ConsString.class) {
        if (String.class == targetType1 || CharSequence.class == targetType1) {
            return Comparison.TYPE_1_BETTER;
        }

        if (String.class == targetType2 || CharSequence.class == targetType2) {
            return Comparison.TYPE_2_BETTER;
        }
    }
    return linkerServices.compareConversion(sourceType, targetType1, targetType2);
}