Example usage for jdk.nashorn.api.scripting ScriptObjectMirror call

List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror call

Introduction

In this page you can find the example usage for jdk.nashorn.api.scripting ScriptObjectMirror call.

Prototype

@Override
    public Object call(final Object thiz, final Object... args) 

Source Link

Usage

From source file:com.intuit.karate.core.ScriptBridge.java

License:Open Source License

public Object repeat(int n, ScriptObjectMirror som) {
    if (!som.isFunction()) {
        throw new RuntimeException("not a JS function: " + som);
    }//from w w  w .  j ava 2 s  . com
    List res = new ArrayList();
    for (int i = 0; i < n; i++) {
        Object o = som.call(som, i);
        res.add(o);
    }
    return res;
}

From source file:com.intuit.karate.Script.java

License:Open Source License

public static ScriptValue evalJsFunctionCall(ScriptObjectMirror som, Object callArg, ScenarioContext context) {
    Object result;/*from   w w  w  . j  a v a  2s.c o  m*/
    try {
        if (callArg != null) {
            result = som.call(som, callArg);
        } else {
            result = som.call(som);
        }
        return new ScriptValue(result);
    } catch (Exception e) {
        String message = "javascript function call failed: " + e.getMessage();
        context.logger.error(message);
        context.logger.error("failed function body: " + som);
        throw new KarateException(message);
    }
}

From source file:com.viddu.handlebars.HandlebarsNashorn.java

License:Apache License

@Override
public String render(String templateString, String contextJson) throws HandlebarsException {
    logger.debug("Context JSON={}", contextJson);
    logger.debug("templateString={}", templateString);
    Invocable render = (Invocable) engine;
    try {//from  ww  w .  j  a v a2  s  .  co  m
        Object context = render.invokeMethod(JSON, "parse", contextJson);
        ScriptObjectMirror obj = (ScriptObjectMirror) render.invokeMethod(handlebars, "compile",
                templateString);
        return (String) obj.call(null, context);
    } catch (NoSuchMethodException | ScriptException e) {
        throw new HandlebarsException(e);
    }
}

From source file:io.jeo.cli.command.TransformCmd.java

License:Open Source License

FeatureCursor runScript(FeatureCursor cursor, String script) throws IOException, ScriptException {
    ScriptEngine eng = scriptMgr.getEngineByName("nashorn");
    try (Reader in = Files.newBufferedReader(Paths.get(script))) {
        eng.eval(in);//from   www .j  av a 2  s .c  o  m

        Object obj = eng.get("transform");
        if (obj == null) {
            throw new IllegalStateException("Script must define a 'transform' function");
        }

        if (!(obj instanceof ScriptObjectMirror)) {
            throw new IllegalStateException("Unrecognized object: " + obj);
        }

        ScriptObjectMirror scriptObj = (ScriptObjectMirror) obj;
        if (!scriptObj.isFunction()) {
            throw new IllegalStateException("transform must be a function");
        }

        Object result = scriptObj.call(null, cursor);
        if (!(result instanceof Cursor)) {
            throw new IllegalStateException("transform function must return a cursor");
        }

        return FeatureCursor.wrap((Cursor) result);
    }
}