use of javax.script.Invocable in project omegat by omegat-org.
the class ScriptRunner method executeScript.
/**
* Execute a script with a given engine and bindings.
*
* @param script
* The script in string form
* @param engine
* The engine
* @param additionalBindings
* A map of bindings that will be included along with other
* bindings
* @return The evaluation result
* @throws ScriptException
*/
public static Object executeScript(String script, ScriptEngine engine, Map<String, Object> additionalBindings) throws ScriptException {
// logResult(StaticUtils.format(OStrings.getString("SCW_SELECTED_LANGUAGE"),
// engine.getFactory().getEngineName()));
Bindings bindings = engine.createBindings();
bindings.put(VAR_PROJECT, Core.getProject());
bindings.put(VAR_EDITOR, Core.getEditor());
bindings.put(VAR_GLOSSARY, Core.getGlossary());
bindings.put(VAR_MAINWINDOW, Core.getMainWindow());
bindings.put(VAR_CORE, Core.class);
if (additionalBindings != null) {
bindings.putAll(additionalBindings);
}
engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
Object result = engine.eval(script);
if (engine instanceof Invocable) {
invokeGuiScript((Invocable) engine);
}
return result;
}
use of javax.script.Invocable in project CitizensAPI by CitizensDev.
the class ScriptCompiler method loadEngine.
private ScriptEngine loadEngine(String extension) {
ScriptEngine engine = engines.get(extension);
if (engine != null)
return engine;
ScriptEngine search = null;
if (extension.equals("js") || extension.equals("javascript")) {
search = engineManager.getEngineByName("graal.js");
if (search == null) {
search = engineManager.getEngineByName("nashorn");
}
}
if (search == null) {
search = engineManager.getEngineByExtension(extension);
}
if (search != null && (!(search instanceof Compilable) || !(search instanceof Invocable))) {
search = null;
} else if (search != null) {
search = tryUpdateClassLoader(search);
}
engines.put(extension, search);
ClassLoader cl = classLoader.get();
if (cl != null) {
updateSunClassLoader(cl);
}
return search;
}
use of javax.script.Invocable in project jgnash by ccavanaugh.
the class ImportFilter method processPayee.
public String processPayee(final String payee) {
try {
final Invocable invocable = (Invocable) scriptEngine;
final Object result = invocable.invokeFunction("processPayee", payee);
return result.toString();
} catch (final ScriptException | NoSuchMethodException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return payee;
}
use of javax.script.Invocable in project jgnash by ccavanaugh.
the class ImportFilter method processMemo.
public String processMemo(final String memo) {
try {
final Invocable invocable = (Invocable) scriptEngine;
final Object result = invocable.invokeFunction("processMemo", memo);
return result.toString();
} catch (final ScriptException | NoSuchMethodException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return memo;
}
use of javax.script.Invocable in project es6draft by anba.
the class InvocableTest method setUp.
@Before
public void setUp() {
manager = new ScriptEngineManager();
engine = manager.getEngineByName("es6draft");
assertThat(engine, notNullValue());
assertThat(engine, instanceOf(Invocable.class));
invocable = (Invocable) engine;
}
Aggregations