Example usage for jdk.nashorn.internal.runtime ScriptFunction getBoundInvokeHandle

List of usage examples for jdk.nashorn.internal.runtime ScriptFunction getBoundInvokeHandle

Introduction

In this page you can find the example usage for jdk.nashorn.internal.runtime ScriptFunction getBoundInvokeHandle.

Prototype

public final MethodHandle getBoundInvokeHandle(final Object self) 

Source Link

Document

Return the invoke handle bound to a given ScriptObject self reference.

Usage

From source file:net.orzo.data.Web.java

License:Apache License

/**
 *
 */// ww w.jav  a 2  s .c o m
public List<Element> queryPage(Element root, String select, ScriptFunction fn) {
    MethodHandle mh;
    Element curr;
    List<Element> ans = null; // returns null in case fn is null

    try {
        if (fn != null) {
            for (Iterator<Element> iter = root.select(select).iterator(); iter.hasNext();) {
                curr = iter.next();
                mh = fn.getBoundInvokeHandle(curr);
                mh.invoke(curr);
            }

        } else {
            ans = new ArrayList<>();
            Iterators.addAll(ans, root.select(select).iterator());
        }
        return ans;

    } catch (Throwable ex) {
        throw new RuntimeException(ex);
    }
}

From source file:net.orzo.lib.DataStructures.java

License:Apache License

/**
 * From a JavaScript array it creates a new one with unique occurrence of
 * items./* w  w w.  j a  va2  s  .  c om*/
 *
 * @param jsArray
 *            a JavaScript or Java array
 * @param key
 *            a JavaScript function to access values to be considered; null
 *            is also possible (in such case, the value itself is used)
 * @return a JavaScript array with unique items
 */
public Object uniq(Object jsArray, ScriptFunction key) {
    Set<Object> set = new HashSet<>();
    Collection<?> origData;

    if (jsArray.getClass().isArray()) {
        origData = Arrays.asList(jsArray);

    } else {
        NativeArray tmp = (NativeArray) ScriptUtils.unwrap(jsArray);
        origData = Arrays.asList(tmp.asObjectArray());
    }

    try {
        if (key == null) {
            set.addAll(origData);

        } else {
            for (Object item : origData) {
                MethodHandle mh = key.getBoundInvokeHandle(item);
                set.add(mh.invoke(item));
            }
        }
        return set; // TODO wrapping???

    } catch (Throwable ex) {
        throw new LibException(ex);
    }
}

From source file:net.orzo.lib.Lib.java

License:Apache License

/**
 * Measures the execution time of the provided function. Please note that in
 * case of asynchronous code you may not obtain the value you have been
 * expecting.//from   ww w.  j  a va  2 s .com
 *
 * @param function
 * @return time in milliseconds
 */
public long measureTime(ScriptFunction function) {
    long startTime = System.currentTimeMillis();
    MethodHandle mh = function.getBoundInvokeHandle(Context.getContext());

    try {
        mh.invoke();

    } catch (Throwable e) {
        throw new LibException(e);
    }
    return System.currentTimeMillis() - startTime;
}