Java Code Examples for jdk.nashorn.internal.runtime.linker.Bootstrap#isCallable()

The following examples show how to use jdk.nashorn.internal.runtime.linker.Bootstrap#isCallable() . 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: NativeObject.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.2.4.3 Object.prototype.toLocaleString ( )
 *
 * @param self self reference
 * @return localized ToString
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toLocaleString(final Object self) {
    final Object obj = JSType.toScriptObject(self);
    if (obj instanceof ScriptObject) {
        final InvokeByName toStringInvoker = getTO_STRING();
        final ScriptObject sobj = (ScriptObject)obj;
        try {
            final Object toString = toStringInvoker.getGetter().invokeExact(sobj);

            if (Bootstrap.isCallable(toString)) {
                return toStringInvoker.getInvoker().invokeExact(toString, sobj);
            }
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }

        throw typeError("not.a.function", "toString");
    }

    return ScriptRuntime.builtinObjectToString(self);
}
 
Example 2
Source File: NativeArray.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.2 Array.prototype.toString ( )
 *
 * @param self self reference
 * @return string representation of array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toString(final Object self) {
    final Object obj = Global.toObject(self);
    if (obj instanceof ScriptObject) {
        final InvokeByName joinInvoker = getJOIN();
        final ScriptObject sobj = (ScriptObject)obj;
        try {
            final Object join = joinInvoker.getGetter().invokeExact(sobj);
            if (Bootstrap.isCallable(join)) {
                return joinInvoker.getInvoker().invokeExact(join, sobj);
            }
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }
    }

    // FIXME: should lookup Object.prototype.toString and call that?
    return ScriptRuntime.builtinObjectToString(self);
}
 
Example 3
Source File: NativeString.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.5.4.11 String.prototype.replace (searchValue, replaceValue)
 * @param self        self reference
 * @param string      item to replace
 * @param replacement item to replace it with
 * @return string after replacement
 * @throws Throwable if replacement fails
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String replace(final Object self, final Object string, final Object replacement) throws Throwable {

    final String str = checkObjectToString(self);

    final NativeRegExp nativeRegExp;
    if (string instanceof NativeRegExp) {
        nativeRegExp = (NativeRegExp) string;
    } else {
        nativeRegExp = NativeRegExp.flatRegExp(JSType.toString(string));
    }

    if (Bootstrap.isCallable(replacement)) {
        return nativeRegExp.replace(str, "", replacement);
    }

    return nativeRegExp.replace(str, JSType.toString(replacement), null);
}
 
Example 4
Source File: NativeObject.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.2.4.3 Object.prototype.toLocaleString ( )
 *
 * @param self self reference
 * @return localized ToString
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toLocaleString(final Object self) {
    final Object obj = JSType.toScriptObject(self);
    if (obj instanceof ScriptObject) {
        final InvokeByName toStringInvoker = getTO_STRING();
        final ScriptObject sobj = (ScriptObject)obj;
        try {
            final Object toString = toStringInvoker.getGetter().invokeExact(sobj);

            if (Bootstrap.isCallable(toString)) {
                return toStringInvoker.getInvoker().invokeExact(toString, sobj);
            }
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }

        throw typeError("not.a.function", "toString");
    }

    return ScriptRuntime.builtinObjectToString(self);
}
 
Example 5
Source File: NativeObject.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.2.4.3 Object.prototype.toLocaleString ( )
 *
 * @param self self reference
 * @return localized ToString
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toLocaleString(final Object self) {
    final Object obj = JSType.toScriptObject(self);
    if (obj instanceof ScriptObject) {
        final InvokeByName toStringInvoker = getTO_STRING();
        final ScriptObject sobj = (ScriptObject)obj;
        try {
            final Object toString = toStringInvoker.getGetter().invokeExact(sobj);

            if (Bootstrap.isCallable(toString)) {
                return toStringInvoker.getInvoker().invokeExact(toString, sobj);
            }
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }

        throw typeError("not.a.function", "toString");
    }

    return ScriptRuntime.builtinObjectToString(self);
}
 
Example 6
Source File: NativeObject.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.2.4.3 Object.prototype.toLocaleString ( )
 *
 * @param self self reference
 * @return localized ToString
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toLocaleString(final Object self) {
    final Object obj = JSType.toScriptObject(self);
    if (obj instanceof ScriptObject) {
        final InvokeByName toStringInvoker = getTO_STRING();
        final ScriptObject sobj = (ScriptObject)self;
        try {
            final Object toString = toStringInvoker.getGetter().invokeExact(sobj);

            if (Bootstrap.isCallable(toString)) {
                return toStringInvoker.getInvoker().invokeExact(toString, sobj);
            }
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }

        throw typeError("not.a.function", "toString");
    }

    return ScriptRuntime.builtinObjectToString(self);
}
 
Example 7
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.2 Array.prototype.toString ( )
 *
 * @param self self reference
 * @return string representation of array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toString(final Object self) {
    final Object obj = Global.toObject(self);
    if (obj instanceof ScriptObject) {
        final InvokeByName joinInvoker = getJOIN();
        final ScriptObject sobj = (ScriptObject)obj;
        try {
            final Object join = joinInvoker.getGetter().invokeExact(sobj);
            if (Bootstrap.isCallable(join)) {
                return joinInvoker.getInvoker().invokeExact(join, sobj);
            }
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }
    }

    // FIXME: should lookup Object.prototype.toString and call that?
    return ScriptRuntime.builtinObjectToString(self);
}
 
Example 8
Source File: NativeObject.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.2.4.3 Object.prototype.toLocaleString ( )
 *
 * @param self self reference
 * @return localized ToString
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toLocaleString(final Object self) {
    final Object obj = JSType.toScriptObject(self);
    if (obj instanceof ScriptObject) {
        final InvokeByName toStringInvoker = getTO_STRING();
        final ScriptObject sobj = (ScriptObject)obj;
        try {
            final Object toString = toStringInvoker.getGetter().invokeExact(sobj);

            if (Bootstrap.isCallable(toString)) {
                return toStringInvoker.getInvoker().invokeExact(toString, sobj);
            }
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }

        throw typeError("not.a.function", "toString");
    }

    return ScriptRuntime.builtinObjectToString(self);
}
 
Example 9
Source File: NativeString.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.5.4.11 String.prototype.replace (searchValue, replaceValue)
 * @param self        self reference
 * @param string      item to replace
 * @param replacement item to replace it with
 * @return string after replacement
 * @throws Throwable if replacement fails
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String replace(final Object self, final Object string, final Object replacement) throws Throwable {

    final String str = checkObjectToString(self);

    final NativeRegExp nativeRegExp;
    if (string instanceof NativeRegExp) {
        nativeRegExp = (NativeRegExp) string;
    } else {
        nativeRegExp = NativeRegExp.flatRegExp(JSType.toString(string));
    }

    if (Bootstrap.isCallable(replacement)) {
        return nativeRegExp.replace(str, "", replacement);
    }

    return nativeRegExp.replace(str, JSType.toString(replacement), null);
}
 
Example 10
Source File: NativeDate.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.9.5.44 Date.prototype.toJSON ( key )
 *
 * Provides a string representation of this Date for use by {@link NativeJSON#stringify(Object, Object, Object, Object)}
 *
 * @param self self reference
 * @param key ignored
 * @return JSON representation of this date
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toJSON(final Object self, final Object key) {
    // NOTE: Date.prototype.toJSON is generic. Accepts other objects as well.
    final Object selfObj = Global.toObject(self);
    if (!(selfObj instanceof ScriptObject)) {
        return null;
    }
    final ScriptObject sobj  = (ScriptObject)selfObj;
    final Object       value = sobj.getDefaultValue(Number.class);
    if (value instanceof Number) {
        final double num = ((Number)value).doubleValue();
        if (isInfinite(num) || isNaN(num)) {
            return null;
        }
    }

    try {
        final InvokeByName toIsoString = getTO_ISO_STRING();
        final Object func = toIsoString.getGetter().invokeExact(sobj);
        if (Bootstrap.isCallable(func)) {
            return toIsoString.getInvoker().invokeExact(func, sobj, key);
        }
        throw typeError("not.a.function", ScriptRuntime.safeToString(func));
    } catch (final RuntimeException | Error e) {
        throw e;
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
}
 
Example 11
Source File: JSONFunctions.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static Object applyReviver(final Global global, final Object unfiltered, final Object reviver) {
    if (Bootstrap.isCallable(reviver)) {
        final ScriptObject root = global.newObject();
        root.addOwnProperty("", Property.WRITABLE_ENUMERABLE_CONFIGURABLE, unfiltered);
        return walk(root, "", reviver);
    }
    return unfiltered;
}
 
Example 12
Source File: ListAdapter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Object getFunction(final String name) {
    final Object fn = obj.getMember(name);
    if(!(Bootstrap.isCallable(fn))) {
        throw new UnsupportedOperationException("The script object doesn't have a function named " + name);
    }
    return fn;
}
 
Example 13
Source File: ListAdapter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private Object getFunction(final String name) {
    final Object fn = obj.getMember(name);
    if(!(Bootstrap.isCallable(fn))) {
        throw new UnsupportedOperationException("The script object doesn't have a function named " + name);
    }
    return fn;
}
 
Example 14
Source File: NativeDate.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.9.5.44 Date.prototype.toJSON ( key )
 *
 * Provides a string representation of this Date for use by {@link NativeJSON#stringify(Object, Object, Object, Object)}
 *
 * @param self self reference
 * @param key ignored
 * @return JSON representation of this date
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toJSON(final Object self, final Object key) {
    // NOTE: Date.prototype.toJSON is generic. Accepts other objects as well.
    final Object selfObj = Global.toObject(self);
    if (!(selfObj instanceof ScriptObject)) {
        return null;
    }
    final ScriptObject sobj  = (ScriptObject)selfObj;
    final Object       value = sobj.getDefaultValue(Number.class);
    if (value instanceof Number) {
        final double num = ((Number)value).doubleValue();
        if (isInfinite(num) || isNaN(num)) {
            return null;
        }
    }

    try {
        final InvokeByName toIsoString = getTO_ISO_STRING();
        final Object func = toIsoString.getGetter().invokeExact(sobj);
        if (Bootstrap.isCallable(func)) {
            return toIsoString.getInvoker().invokeExact(func, sobj, key);
        }
        throw typeError("not.a.function", ScriptRuntime.safeToString(func));
    } catch (final RuntimeException | Error e) {
        throw e;
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
}
 
Example 15
Source File: NativeArray.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static Object compareFunction(final Object comparefn) {
    if (comparefn == ScriptRuntime.UNDEFINED) {
        return null;
    }

    if (!Bootstrap.isCallable(comparefn)) {
        throw typeError("not.a.function", ScriptRuntime.safeToString(comparefn));
    }

    return comparefn;
}
 
Example 16
Source File: NativeDate.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.9.5.44 Date.prototype.toJSON ( key )
 *
 * Provides a string representation of this Date for use by {@link NativeJSON#stringify(Object, Object, Object, Object)}
 *
 * @param self self reference
 * @param key ignored
 * @return JSON representation of this date
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toJSON(final Object self, final Object key) {
    // NOTE: Date.prototype.toJSON is generic. Accepts other objects as well.
    final Object selfObj = Global.toObject(self);
    if (!(selfObj instanceof ScriptObject)) {
        return null;
    }
    final ScriptObject sobj  = (ScriptObject)selfObj;
    final Object       value = sobj.getDefaultValue(Number.class);
    if (value instanceof Number) {
        final double num = ((Number)value).doubleValue();
        if (isInfinite(num) || isNaN(num)) {
            return null;
        }
    }

    try {
        final InvokeByName toIsoString = getTO_ISO_STRING();
        final Object func = toIsoString.getGetter().invokeExact(sobj);
        if (Bootstrap.isCallable(func)) {
            return toIsoString.getInvoker().invokeExact(func, sobj, key);
        }
        throw typeError("not.a.function", ScriptRuntime.safeToString(func));
    } catch (final RuntimeException | Error e) {
        throw e;
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
}
 
Example 17
Source File: ListAdapter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private Object getFunction(final String name) {
    final Object fn = obj.getMember(name);
    if(!(Bootstrap.isCallable(fn))) {
        throw new UnsupportedOperationException("The script object doesn't have a function named " + name);
    }
    return fn;
}
 
Example 18
Source File: ListAdapter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private static void checkFunction(final Object fn, final InvokeByName invoke) {
    if(!(Bootstrap.isCallable(fn))) {
        throw new UnsupportedOperationException("The script object doesn't have a function named " + invoke.getName());
    }
}
 
Example 19
Source File: NativeJava.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns true if the specified object is a java function (but not script function)
 * @param self not used
 * @param obj the object that is checked if it is a Java function or not
 * @return tells whether given object is a Java function or not
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static boolean isJavaFunction(final Object self, final Object obj) {
    return Bootstrap.isCallable(obj) && !(obj instanceof ScriptFunction);
}
 
Example 20
Source File: NativeJava.java    From hottub with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns true if the specified object is a java function (but not script function)
 * @param self not used
 * @param obj the object that is checked if it is a Java function or not
 * @return tells whether given object is a Java function or not
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static boolean isJavaFunction(final Object self, final Object obj) {
    return Bootstrap.isCallable(obj) && !(obj instanceof ScriptFunction);
}