Java Code Examples for jdk.nashorn.internal.runtime.JSType#isString()

The following examples show how to use jdk.nashorn.internal.runtime.JSType#isString() . 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: NativeString.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static CharSequence getCharSequence(final Object self) {
    if (JSType.isString(self)) {
        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 2
Source File: NativeDate.java    From hottub with 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;
}
 
Example 3
Source File: NativeString.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static CharSequence getCharSequence(final Object self) {
    if (JSType.isString(self)) {
        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 4
Source File: NativeDate.java    From openjdk-jdk8u with 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;
}
 
Example 5
Source File: NativeDate.java    From jdk8u_nashorn with 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;
}
 
Example 6
Source File: NativeString.java    From openjdk-jdk9 with 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.getStringGuard());
        } catch (final LookupException e) {
            //empty. Shouldn't happen. Fall back to super
        }
    }
    return super.findGetIndexMethod(desc, request);
}
 
Example 7
Source File: NativeString.java    From openjdk-jdk8u with 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.getStringGuard());
        } catch (final LookupException e) {
            //empty. Shouldn't happen. Fall back to super
        }
    }
    return super.findGetIndexMethod(desc, request);
}
 
Example 8
Source File: NativeDate.java    From jdk8u60 with 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;
}
 
Example 9
Source File: NativeString.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static CharSequence getCharSequence(final Object self) {
    if (JSType.isString(self)) {
        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 10
Source File: NativeDate.java    From TencentKona-8 with 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;
}
 
Example 11
Source File: NativeString.java    From TencentKona-8 with 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.getStringGuard());
        } catch (final LookupException e) {
            //empty. Shouldn't happen. Fall back to super
        }
    }
    return super.findGetIndexMethod(desc, request);
}
 
Example 12
Source File: NativeJSON.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (replacer instanceof ScriptFunction) {
        state.replacerFunction = (ScriptFunction) replacer;
    } else if (isArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        final int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (JSType.isString(modSpace)) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, 0);

    return str("", wrapper, state);
}
 
Example 13
Source File: NativeJSON.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (Bootstrap.isCallable(replacer)) {
        state.replacerFunction = replacer;
    } else if (isArray(replacer) ||
            isJSObjectArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        final int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (JSType.isString(modSpace)) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, 0);

    return str("", wrapper, state);
}
 
Example 14
Source File: NashornPrimitiveLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unused")
private static boolean isJavaScriptPrimitive(final Object o) {
    return JSType.isString(o) || o instanceof Boolean || JSType.isNumber(o) || o == null;
}
 
Example 15
Source File: NashornPrimitiveLinker.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unused")
private static boolean isJavaScriptPrimitive(final Object o) {
    return JSType.isString(o) || o instanceof Boolean || JSType.isNumber(o) || o == null;
}
 
Example 16
Source File: NashornPrimitiveLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unused")
private static boolean isJavaScriptPrimitive(final Object o) {
    return JSType.isString(o) || o instanceof Boolean || JSType.isNumber(o) || o == null;
}
 
Example 17
Source File: NativeJSON.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (Bootstrap.isCallable(replacer)) {
        state.replacerFunction = replacer;
    } else if (isArray(replacer) ||
            isJSObjectArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        final int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (JSType.isString(modSpace)) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, 0);

    return str("", wrapper, state);
}
 
Example 18
Source File: NativeJSON.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (replacer instanceof ScriptFunction) {
        state.replacerFunction = (ScriptFunction) replacer;
    } else if (isArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        final int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (JSType.isString(modSpace)) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, 0);

    return str("", wrapper, state);
}
 
Example 19
Source File: NashornPrimitiveLinker.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unused")
private static boolean isJavaScriptPrimitive(final Object o) {
    return JSType.isString(o) || o instanceof Boolean || JSType.isNumber(o) || o == null || o instanceof Symbol;
}
 
Example 20
Source File: NashornPrimitiveLinker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unused")
private static boolean isJavaScriptPrimitive(final Object o) {
    return JSType.isString(o) || o instanceof Boolean || JSType.isNumber(o) || o == null;
}