Java Code Examples for jdk.nashorn.internal.runtime.ScriptRuntime#apply()

The following examples show how to use jdk.nashorn.internal.runtime.ScriptRuntime#apply() . 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: NativeJSAdapter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Iterator<String> propertyIterator() {
    // Try __getIds__ first, if not found then try __getKeys__
    // In jdk6, we had added "__getIds__" so this is just for compatibility.
    Object func = adaptee.get(__getIds__);
    if (!(func instanceof ScriptFunction)) {
        func = adaptee.get(__getKeys__);
    }

    Object obj;
    if (func instanceof ScriptFunction) {
        obj = ScriptRuntime.apply((ScriptFunction)func, this);
    } else {
        obj = new NativeArray(0);
    }

    final List<String> array = new ArrayList<>();
    for (final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(obj); iter.hasNext(); ) {
        array.add((String)iter.next());
    }

    return array.iterator();
}
 
Example 2
Source File: NativeFunction.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.3.4.4 Function.prototype.call (thisArg [ , arg1 [ , arg2, ... ] ] )
 *
 * @param self self reference
 * @param args arguments for call
 * @return result of call
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object call(final Object self, final Object... args) {
    if (!(self instanceof ScriptFunction) && !(self instanceof JSObject)) {
        throw typeError("not.a.function", ScriptRuntime.safeToString(self));
    }

    Object thiz = (args.length == 0) ? UNDEFINED : args[0];
    Object[] arguments;

    if (args.length > 1) {
        arguments = new Object[args.length - 1];
        System.arraycopy(args, 1, arguments, 0, arguments.length);
    } else {
        arguments = ScriptRuntime.EMPTY_ARRAY;
    }

    if (self instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)self, thiz, arguments);
    } else if (self instanceof JSObject) {
        return ((JSObject)self).call(thiz, arguments);
    }

    throw new AssertionError("should not reach here");
}
 
Example 3
Source File: NativeFunction.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.3.4.4 Function.prototype.call (thisArg [ , arg1 [ , arg2, ... ] ] )
 *
 * @param self self reference
 * @param args arguments for call
 * @return result of call
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object call(final Object self, final Object... args) {
    checkCallable(self);

    final Object thiz = (args.length == 0) ? UNDEFINED : args[0];
    Object[] arguments;

    if (args.length > 1) {
        arguments = new Object[args.length - 1];
        System.arraycopy(args, 1, arguments, 0, arguments.length);
    } else {
        arguments = ScriptRuntime.EMPTY_ARRAY;
    }

    if (self instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)self, thiz, arguments);
    } else if (self instanceof JSObject) {
        return ((JSObject)self).call(thiz, arguments);
    }

    throw new AssertionError("should not reach here");
}
 
Example 4
Source File: NativeFunction.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.3.4.4 Function.prototype.call (thisArg [ , arg1 [ , arg2, ... ] ] )
 *
 * @param self self reference
 * @param args arguments for call
 * @return result of call
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object call(final Object self, final Object... args) {
    checkCallable(self);

    final Object thiz = (args.length == 0) ? UNDEFINED : args[0];
    Object[] arguments;

    if (args.length > 1) {
        arguments = new Object[args.length - 1];
        System.arraycopy(args, 1, arguments, 0, arguments.length);
    } else {
        arguments = ScriptRuntime.EMPTY_ARRAY;
    }

    if (self instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)self, thiz, arguments);
    } else if (self instanceof JSObject) {
        return ((JSObject)self).call(thiz, arguments);
    }

    throw new AssertionError("should not reach here");
}
 
Example 5
Source File: DebugLogger.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void logEvent(final RuntimeEvent<?> event) {
    if (event != null) {
        final Global global = Context.getGlobal();
        if (global.has("Debug")) {
            final ScriptObject debug = (ScriptObject)global.get("Debug");
            final ScriptFunction addRuntimeEvent = (ScriptFunction)debug.get("addRuntimeEvent");
            ScriptRuntime.apply(addRuntimeEvent, debug, event);
        }
    }
}
 
Example 6
Source File: NativeJSAdapter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private Object callAdaptee(final Object retValue, final String name, final Object... args) {
    final Object func = adaptee.get(name);
    if (func instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)func, adaptee, args);
    }
    return retValue;
}
 
Example 7
Source File: NativeFunction.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.4.3 Function.prototype.apply (thisArg, argArray)
 *
 * @param self   self reference
 * @param thiz   {@code this} arg for apply
 * @param array  array of argument for apply
 * @return result of apply
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object apply(final Object self, final Object thiz, final Object array) {
    checkCallable(self);

    final Object[] args = toApplyArgs(array);

    if (self instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)self, thiz, args);
    } else if (self instanceof JSObject) {
        return ((JSObject)self).call(thiz, args);
    }
    throw new AssertionError("Should not reach here");
}
 
Example 8
Source File: NativeJSAdapter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private Object callAdaptee(final Object retValue, final String name, final Object... args) {
    final Object func = adaptee.get(name);
    if (func instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)func, adaptee, args);
    }
    return retValue;
}
 
Example 9
Source File: NativeJSAdapter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private Object callAdaptee(final Object retValue, final String name, final Object... args) {
    final Object func = adaptee.get(name);
    if (func instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)func, adaptee, args);
    }
    return retValue;
}
 
Example 10
Source File: NativeFunction.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.4.3 Function.prototype.apply (thisArg, argArray)
 *
 * @param self   self reference
 * @param thiz   {@code this} arg for apply
 * @param array  array of argument for apply
 * @return result of apply
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object apply(final Object self, final Object thiz, final Object array) {
    checkCallable(self);

    final Object[] args = toApplyArgs(array);

    if (self instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)self, thiz, args);
    } else if (self instanceof JSObject) {
        return ((JSObject)self).call(thiz, args);
    }
    throw new AssertionError("Should not reach here");
}
 
Example 11
Source File: NativeJSAdapter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private Object callAdaptee(final Object retValue, final String name, final Object... args) {
    final Object func = adaptee.get(name);
    if (func instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)func, adaptee, args);
    }
    return retValue;
}
 
Example 12
Source File: NativeJSAdapter.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private Object callAdaptee(final Object retValue, final String name, final Object... args) {
    final Object func = adaptee.get(name);
    if (func instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)func, adaptee, args);
    }
    return retValue;
}
 
Example 13
Source File: SharedContextEvaluator.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int run(final OutputStream out, final OutputStream err, final String[] args) throws IOException {
    final Global oldGlobal = Context.getGlobal();
    try {
        ctxOut.setDelegatee(out);
        ctxErr.setDelegatee(err);
        final ErrorManager errors = context.getErrorManager();
        final Global global = context.createGlobal();
        Context.setGlobal(global);

        // For each file on the command line.
        for (final String fileName : args) {
            if (fileName.startsWith("-")) {
                // ignore options in shared context mode (which was initialized upfront!)
                continue;
            }
            final File file = new File(fileName);
            final ScriptFunction script = context.compileScript(sourceFor(fileName, file.toURI().toURL()), global);

            if (script == null || errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            try {
                ScriptRuntime.apply(script, global);
            } catch (final NashornException e) {
                errors.error(e.toString());
                if (context.getEnv()._dump_on_error) {
                    e.printStackTrace(context.getErr());
                }

                return RUNTIME_ERROR;
            }
        }
    } finally {
        context.getOut().flush();
        context.getErr().flush();
        Context.setGlobal(oldGlobal);
    }

    return SUCCESS;
}
 
Example 14
Source File: SharedContextEvaluator.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int run(final OutputStream out, final OutputStream err, final String[] args) throws IOException {
    final Global oldGlobal = Context.getGlobal();
    try {
        ctxOut.setDelegatee(out);
        ctxErr.setDelegatee(err);
        final ErrorManager errors = context.getErrorManager();
        final Global global = context.createGlobal();
        Context.setGlobal(global);

        // For each file on the command line.
        for (final String fileName : args) {
            if (fileName.startsWith("-")) {
                // ignore options in shared context mode (which was initialized upfront!)
                continue;
            }
            final File file = new File(fileName);
            final ScriptFunction script = context.compileScript(sourceFor(fileName, file.toURI().toURL()), global);

            if (script == null || errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            try {
                ScriptRuntime.apply(script, global);
            } catch (final NashornException e) {
                errors.error(e.toString());
                if (context.getEnv()._dump_on_error) {
                    e.printStackTrace(context.getErr());
                }

                return RUNTIME_ERROR;
            }
        }
    } finally {
        context.getOut().flush();
        context.getErr().flush();
        Context.setGlobal(oldGlobal);
    }

    return SUCCESS;
}
 
Example 15
Source File: ContextTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static Object eval(final Context cx, final String name, final String code) {
    final Source source = sourceFor(name, code);
    final ScriptObject global = Context.getGlobal();
    final ScriptFunction func = cx.compileScript(source, global);
    return func != null ? ScriptRuntime.apply(func, global) : null;
}
 
Example 16
Source File: ContextTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static Object eval(final Context cx, final String name, final String code) {
    final Source source = sourceFor(name, code);
    final ScriptObject global = Context.getGlobal();
    final ScriptFunction func = cx.compileScript(source, global);
    return func != null ? ScriptRuntime.apply(func, global) : null;
}
 
Example 17
Source File: ContextTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static Object eval(final Context cx, final String name, final String code) {
    final Source source = sourceFor(name, code);
    final ScriptObject global = Context.getGlobal();
    final ScriptFunction func = cx.compileScript(source, global);
    return func != null ? ScriptRuntime.apply(func, global) : null;
}
 
Example 18
Source File: SharedContextEvaluator.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int run(final OutputStream out, final OutputStream err, final String[] args) throws IOException {
    final Global oldGlobal = Context.getGlobal();
    try {
        ctxOut.setDelegatee(out);
        ctxErr.setDelegatee(err);
        final ErrorManager errors = context.getErrorManager();
        final Global global = context.createGlobal();
        Context.setGlobal(global);

        // For each file on the command line.
        for (final String fileName : args) {
            if (fileName.startsWith("-")) {
                // ignore options in shared context mode (which was initialized upfront!)
                continue;
            }
            final File file = new File(fileName);
            final ScriptFunction script = context.compileScript(sourceFor(fileName, file.toURI().toURL()), global);

            if (script == null || errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            try {
                ScriptRuntime.apply(script, global);
            } catch (final NashornException e) {
                errors.error(e.toString());
                if (context.getEnv()._dump_on_error) {
                    e.printStackTrace(context.getErr());
                }

                return RUNTIME_ERROR;
            }
        }
    } finally {
        context.getOut().flush();
        context.getErr().flush();
        Context.setGlobal(oldGlobal);
    }

    return SUCCESS;
}
 
Example 19
Source File: Shell.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Hook to ScriptFunction "apply". A performance metering shell may
 * introduce enter/exit timing here.
 *
 * @param target target function for apply
 * @param self self reference for apply
 *
 * @return result of the function apply
 */
protected Object apply(final ScriptFunction target, final Object self) {
    return ScriptRuntime.apply(target, self);
}
 
Example 20
Source File: Shell.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Hook to ScriptFunction "apply". A performance metering shell may
 * introduce enter/exit timing here.
 *
 * @param target target function for apply
 * @param self self reference for apply
 *
 * @return result of the function apply
 */
protected Object apply(final ScriptFunction target, final Object self) {
    return ScriptRuntime.apply(target, self);
}