@Test
public void invokeFunctionInGlobalScopeTest2() throws Exception {
final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
// create a new ScriptContext instance
final ScriptContext ctxt = new SimpleScriptContext();
// set it as 'default' ScriptContext
engine.setContext(ctxt);
// create a new Bindings and set as ENGINE_SCOPE
ctxt.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
// define a function called "func"
engine.eval("func = function() { return 42 }");
// move ENGINE_SCOPE Bindings to GLOBAL_SCOPE
ctxt.setBindings(ctxt.getBindings(ScriptContext.ENGINE_SCOPE), ScriptContext.GLOBAL_SCOPE);
// create a new Bindings and set as ENGINE_SCOPE
ctxt.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
// define new function that calls "func" now in GLOBAL_SCOPE
engine.eval("newfunc = function() { return func() }");
// call "newfunc" and check the return value
final Object value = ((Invocable)engine).invokeFunction("newfunc");
assertTrue(((Number)value).intValue() == 42);
}
java类javax.script.ScriptContext的实例源码
ScopeTest.java 文件源码
项目:openjdk-jdk10
阅读 18
收藏 0
点赞 0
评论 0
HelperScript.java 文件源码
项目:helper
阅读 16
收藏 0
点赞 0
评论 0
@Override
public void run() {
ScriptEngine engine = loader.getScriptEngine();
try {
// create bindings
Bindings bindings = engine.createBindings();
// provide an export for this scripts attributes
bindings.put("loader", loader);
bindings.put("registry", terminableRegistry);
bindings.put("logger", logger);
// the path of the script file (current working directory)
bindings.put("cwd", path.normalize().toString().replace("\\", "/"));
// the root scripts directory
bindings.put("rsd", loader.getDirectory().normalize().toString().replace("\\", "/") + "/");
// function to depend on another script
bindings.put("depend", (Consumer<String>) this::depend);
// append the global helper bindings to this instance
systemBindings.appendTo(bindings);
// create a new script context, and attach our bindings
ScriptContext context = new SimpleScriptContext();
context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
// evaluate the header
engine.eval(systemBindings.getPlugin().getScriptHeader(), context);
// resolve the load path, relative to the loader directory.
Path loadPath = loader.getDirectory().normalize().resolve(path);
engine.eval("__load(\"" + loadPath.toString().replace("\\", "/") + "\");", context);
} catch (Throwable t) {
t.printStackTrace();
}
}
Worker.java 文件源码
项目:scijava-jupyter-kernel
阅读 32
收藏 0
点赞 0
评论 0
private void syncBindings(ScriptEngine scriptEngine, ScriptLanguage scriptLanguage) {
Bindings currentBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
this.scriptEngines.forEach((String name, ScriptEngine engine) -> {
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
currentBindings.keySet().forEach((String key) -> {
bindings.put(key, scriptLanguage.decode(currentBindings.get(key)));
});
});
}
ScijavaEvaluator.java 文件源码
项目:scijava-jupyter-kernel
阅读 19
收藏 0
点赞 0
评论 0
private void addLanguage(String langName) {
if (scriptService.getLanguageByName(langName) == null) {
log.error("Script Language for '" + langName + "' not found.");
System.exit(1);
}
if (!this.scriptLanguages.keySet().contains(langName)) {
Bindings bindings = null;
if (!this.scriptEngines.isEmpty()) {
String firstLanguage = this.scriptEngines.keySet().iterator().next();
bindings = this.scriptEngines.get(firstLanguage).getBindings(ScriptContext.ENGINE_SCOPE);
}
log.info("Script Language for '" + langName + "' found.");
ScriptLanguage scriptLanguage = scriptService.getLanguageByName(langName);
this.scriptLanguages.put(langName, scriptLanguage);
ScriptEngine engine = this.scriptLanguages.get(langName).getScriptEngine();
this.scriptEngines.put(langName, engine);
AutoCompleter completer = scriptLanguage.getAutoCompleter();
this.completers.put(languageName, completer);
// Not implemented yet
//engine.setBindings(this.bindings, ScriptContext.ENGINE_SCOPE);
if (bindings != null) {
this.initBindings(bindings, engine, scriptLanguage);
}
}
log.debug("Script Language found for '" + langName + "'");
}
ScijavaEvaluator.java 文件源码
项目:scijava-jupyter-kernel
阅读 15
收藏 0
点赞 0
评论 0
private void initBindings(Bindings bindings, ScriptEngine scriptEngine, ScriptLanguage scriptLanguage) {
Bindings currentBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.keySet().forEach((String key) -> {
currentBindings.put(key, scriptLanguage.decode(bindings.get(key)));
});
}
ScopeTest.java 文件源码
项目:openjdk-jdk10
阅读 20
收藏 0
点赞 0
评论 0
ScriptRunner(final ScriptEngine engine, final ScriptContext context, final String source, final Object expected, final int iterations) {
this.engine = engine;
this.context = context;
this.source = source;
this.expected = expected;
this.iterations = iterations;
}
ScopeTest.java 文件源码
项目:openjdk-jdk10
阅读 21
收藏 0
点赞 0
评论 0
/**
* Test multi-threaded scope function invocation for shared script classes with multiple globals.
*/
@Test
public static void multiThreadedFunctionTest() throws ScriptException, InterruptedException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final Bindings b = e.createBindings();
final ScriptContext origContext = e.getContext();
final ScriptContext newCtxt = new SimpleScriptContext();
newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), origContext);
assertEquals(origContext.getAttribute("scopeVar"), 1);
assertEquals(e.eval("scopeTest()"), 1);
e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), newCtxt);
assertEquals(newCtxt.getAttribute("scopeVar"), 1);
assertEquals(e.eval("scopeTest();", newCtxt), 1);
assertEquals(e.eval("scopeVar = 3;", newCtxt), 3);
assertEquals(newCtxt.getAttribute("scopeVar"), 3);
final Thread t1 = new Thread(new ScriptRunner(e, origContext, "scopeTest()", 1, 1000));
final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, "scopeTest()", 3, 1000));
t1.start();
t2.start();
t1.join();
t2.join();
}
ScriptExecutor.java 文件源码
项目:neoscada
阅读 33
收藏 0
点赞 0
评论 0
public Object execute ( final ScriptContext scriptContext, final Map<String, Object> scriptObjects ) throws Exception
{
return Scripts.executeWithClassLoader ( this.classLoader, new Callable<Object> () {
@Override
public Object call () throws Exception
{
return executeScript ( scriptContext, scriptObjects );
}
} );
}
SymbolController.java 文件源码
项目:neoscada
阅读 19
收藏 0
点赞 0
评论 0
private void assignConsole ( final ScriptContext scriptContext )
{
this.console = createOrGetConsole ();
// scriptContext.setReader ( new InputStreamReader ( this.console.getInputStream () ) );
/* wrapping into a PrintWriter is necessary due to
* http://bugs.sun.com/view_bug.do?bug_id=6759414
* */
this.console.applyTo ( scriptContext );
}
FormulaDataSource.java 文件源码
项目:neoscada
阅读 17
收藏 0
点赞 0
评论 0
/**
* Handle data change
*/
@Override
protected synchronized void handleChange ( final Map<String, DataSourceHandler> sources )
{
if ( this.inputFormula == null || this.inputFormula.isEmpty () )
{
updateData ( null );
return;
}
try
{
final Map<String, Integer> flags = new HashMap<String, Integer> ( 4 );
final Map<String, Object> values = new HashMap<String, Object> ( sources.size () );
gatherData ( sources, flags, values );
for ( final Map.Entry<String, Object> entry : values.entrySet () )
{
this.scriptContext.setAttribute ( entry.getKey (), entry.getValue (), ScriptContext.ENGINE_SCOPE );
}
// execute inputFormula
executeScript ( this.inputFormula, this.inputScript, flags );
}
catch ( final Throwable e )
{
logger.info ( "Failed to evaluate", e );
logger.debug ( "Failed script: {}", this.inputFormula );
setError ( e );
}
}