本文整理汇总了Java中javax.script.ScriptEngine.createBindings方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptEngine.createBindings方法的具体用法?Java ScriptEngine.createBindings怎么用?Java ScriptEngine.createBindings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.ScriptEngine
的用法示例。
在下文中一共展示了ScriptEngine.createBindings方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createBindingsTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void createBindingsTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final Bindings b = e.createBindings();
b.put("foo", 42.0);
Object res = null;
try {
res = e.eval("foo == 42.0", b);
} catch (final ScriptException | NullPointerException se) {
se.printStackTrace();
fail(se.getMessage());
}
assertEquals(res, Boolean.TRUE);
}
示例2: multiThreadedPrimitiveTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
* Test multi-threaded access to primitive prototype properties for shared script classes with multiple globals.
*/
@Test
public static void multiThreadedPrimitiveTest() 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);
final Object obj1 = e.eval("String.prototype.foo = 'original context';", origContext);
final Object obj2 = e.eval("String.prototype.foo = 'new context';", newCtxt);
assertEquals(obj1, "original context");
assertEquals(obj2, "new context");
final String sharedScript = "''.foo";
final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
t1.start();
t2.start();
t1.join();
t2.join();
final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt);
assertEquals(obj3, "newer context");
final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));
t3.start();
t4.start();
t3.join();
t4.join();
Assert.assertEquals(e.eval(sharedScript), "original context");
Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context");
}
示例3: testSlowScope
import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
* Test "slow" scopes involving {@code with} and {@code eval} statements for shared script classes with multiple globals.
* @throws ScriptException
* @throws InterruptedException
*/
@Test
public static void testSlowScope() throws ScriptException, InterruptedException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
for (int i = 0; i < 100; i++) {
final Bindings b = e.createBindings();
final ScriptContext ctxt = new SimpleScriptContext();
ctxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
e.eval(new URLReader(ScopeTest.class.getResource("resources/witheval.js")), ctxt);
assertEquals(e.eval("a", ctxt), 1);
assertEquals(b.get("a"), 1);
assertEquals(e.eval("b", ctxt), 3);
assertEquals(b.get("b"), 3);
assertEquals(e.eval("c", ctxt), 10);
assertEquals(b.get("c"), 10);
}
}
示例4: mapScriptObjectMirrorCallsiteTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void mapScriptObjectMirrorCallsiteTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine engine = m.getEngineByName("nashorn");
final String TEST_SCRIPT = "typeof obj.foo";
final Bindings global = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
engine.eval("var obj = java.util.Collections.emptyMap()");
// this will drive callsite "obj.foo" of TEST_SCRIPT
// to use "obj instanceof Map" as it's guard
engine.eval(TEST_SCRIPT, global);
// redefine 'obj' to be a script object
engine.eval("obj = {}");
final Bindings newGlobal = engine.createBindings();
// transfer 'obj' from default global to new global
// new global will get a ScriptObjectMirror wrapping 'obj'
newGlobal.put("obj", global.get("obj"));
// Every ScriptObjectMirror is a Map! If callsite "obj.foo"
// does not see the new 'obj' is a ScriptObjectMirror, it'll
// continue to use Map's get("obj.foo") instead of ScriptObjectMirror's
// getMember("obj.foo") - thereby getting null instead of undefined
assertEquals("undefined", engine.eval(TEST_SCRIPT, newGlobal));
}
示例5: getScriptEngine
import javax.script.ScriptEngine; //导入方法依赖的package包/类
private PacScriptEngine getScriptEngine(String pacSource) throws PacParsingException {
try {
String helperJSScript = getHelperJsScriptSource();
LOGGER.log(Level.FINER, "PAC Helper JavaScript :\n{0}", helperJSScript);
ScriptEngine engine;
if (nashornJava8u40Available) {
engine = getNashornJSScriptEngine();
} else {
engine = getGenericJSScriptEngine();
}
LOGGER.log(Level.FINE, "PAC script evaluator using: {0}", getEngineInfo(engine));
PacHelperMethods pacHelpers = Lookup.getDefault().lookup(PacHelperMethods.class);
if (pacHelpers == null) { // this should be redundant but we take no chances
pacHelpers = new NbPacHelperMethods();
}
Bindings b = engine.createBindings();
b.put(JS_HELPER_METHODS_INSTANCE_NAME, pacHelpers);
engine.setBindings(b, ScriptContext.ENGINE_SCOPE);
engine.eval(pacSource);
engine.eval(helperJSScript);
// Do some minimal testing of the validity of the PAC Script.
final PacJsEntryFunction jsMainFunction;
if (nashornJava8u40Available) {
jsMainFunction = testScriptEngine(engine, true);
} else {
jsMainFunction = testScriptEngine(engine, false);
}
return new PacScriptEngine(engine, jsMainFunction);
} catch (ScriptException ex) {
throw new PacParsingException(ex);
}
}
示例6: run
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@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();
}
}
示例7: getInfoFor
import javax.script.ScriptEngine; //导入方法依赖的package包/类
public String getInfoFor(Player player, String string) {
string = string
.replaceAll(Variables.statueVariable,
AuthMeApi.getInstance().isRegistered(player.getName()) ? Variables.registeredStatueMessage
: Variables.unregisteredStatueMessage)
.replaceAll(Variables.playerVariable, player.getName())
.replaceAll(Variables.playerCustomVariable, player.getCustomName())
.replaceAll(Variables.playerDisplayVariable, player.getDisplayName())
.replaceAll(Variables.playerIpVariable, player.getAddress().getHostString())
.replaceAll(Variables.serverIpVariable, Bukkit.getIp())
.replaceAll(Variables.playerLevelVariable, player.getExpToLevel() + "");
if (Variables.script.isEmpty()) {
return string;
}
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(Variables.scriptEngine);
Bindings bindings = engine.createBindings();
bindings.put("line", string);
bindings.put("player", player);
bindings.put("plugin", plugin);
try {
string = engine.eval(Variables.script, bindings).toString();
} catch (ScriptException e) {
e.printStackTrace();
}
return string;
}
示例8: main
import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine jsengine = Helper.getJsEngine(manager);
if (jsengine == null) {
System.out.println("Warning: No js engine found; test vacuously passes.");
return;
}
jsengine.eval("var v = 'hello';");
// Create a new scope
Bindings b = jsengine.createBindings();
// b is newly created scope. We don't expect 'v' there.
// we expect b to be empty...
if (b.keySet().size() != 0) {
throw new RuntimeException("no variables expected in new scope");
}
// verify that we can create new variable from Java
jsengine.put("fromJava", "hello world");
// below should execute without problems..
jsengine.eval(" if (fromJava != 'hello world') throw 'unexpected'");
// verify that script globals are exposed to Java
// we have created 'v' and 'fromJava' already.
if (! jsengine.get("v").equals("hello")) {
throw new RuntimeException("unexpected value of 'v'");
}
if (! jsengine.get("fromJava").equals("hello world")) {
throw new RuntimeException("unexpected value of 'fromJava'");
}
}
示例9: multiThreadedVarTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
* Test multi-threaded access to defined global variables for shared script classes with multiple globals.
*/
@Test
public static void multiThreadedVarTest() 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);
final String sharedScript = "foo";
assertEquals(e.eval("var foo = 'original context';", origContext), null);
assertEquals(e.eval("var foo = 'new context';", newCtxt), null);
final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
t1.start();
t2.start();
t1.join();
t2.join();
assertEquals(e.eval("var foo = 'newer context';", newCtxt), null);
final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));
t3.start();
t4.start();
t3.join();
t4.join();
assertEquals(e.eval(sharedScript), "original context");
assertEquals(e.eval(sharedScript, newCtxt), "newer context");
}
示例10: multiThreadedGlobalTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
* Test multi-threaded access to undefined global variables for shared script classes with multiple globals.
*/
@Test
public static void multiThreadedGlobalTest() 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);
assertEquals(e.eval("foo = 'original context';", origContext), "original context");
assertEquals(e.eval("foo = 'new context';", newCtxt), "new context");
final String sharedScript = "foo";
final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
t1.start();
t2.start();
t1.join();
t2.join();
final Object obj3 = e.eval("delete foo; foo = 'newer context';", newCtxt);
assertEquals(obj3, "newer context");
final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));
t3.start();
t4.start();
t3.join();
t4.join();
Assert.assertEquals(e.eval(sharedScript), "original context");
Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context");
}
示例11: multiThreadedAccessorTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
* Test multi-threaded access to prototype user accessor properties for shared script classes with multiple globals.
*/
@Test
public static void multiThreadedAccessorTest() 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("Object.defineProperty(Object.prototype, 'foo', { get: function() 'original context' })", origContext);
e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'new context', configurable: true })", newCtxt);
final String sharedScript = "({}).foo";
final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
t1.start();
t2.start();
t1.join();
t2.join();
final Object obj3 = e.eval("delete Object.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt);
assertEquals(obj3, "newer context");
final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));
t3.start();
t4.start();
t3.join();
t4.join();
}
示例12: multiThreadedPrimitiveAccessorTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
* Test multi-threaded access to primitive prototype user accessor properties for shared script classes with multiple globals.
*/
@Test
public static void multiThreadedPrimitiveAccessorTest() 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("Object.defineProperty(String.prototype, 'foo', { get: function() 'original context' })", origContext);
e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'new context' })", newCtxt);
final String sharedScript = "''.foo";
final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
t1.start();
t2.start();
t1.join();
t2.join();
final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt);
assertEquals(obj3, "newer context");
final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));
t3.start();
t4.start();
t3.join();
t4.join();
}
示例13: multiThreadedFunctionTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
* 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();
}
示例14: getterSetterTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
* Test multi-threaded access to global getters and setters for shared script classes with multiple globals.
*/
@Test
public static void getterSetterTest() 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);
final String sharedScript = "accessor1";
e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext);
assertEquals(e.eval("accessor1 = 1;"), 1);
assertEquals(e.eval(sharedScript), 1);
e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt);
assertEquals(e.eval("accessor1 = 2;", newCtxt), 2);
assertEquals(e.eval(sharedScript, newCtxt), 2);
final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000));
final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000));
t1.start();
t2.start();
t1.join();
t2.join();
assertEquals(e.eval(sharedScript), 1);
assertEquals(e.eval(sharedScript, newCtxt), 2);
assertEquals(e.eval("v"), 1);
assertEquals(e.eval("v", newCtxt), 2);
}
示例15: getterSetter2Test
import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
* Test multi-threaded access to global getters and setters for shared script classes with multiple globals.
*/
@Test
public static void getterSetter2Test() 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);
final String sharedScript = "accessor2";
e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext);
assertEquals(e.eval("accessor2 = 1;"), 1);
assertEquals(e.eval(sharedScript), 1);
e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt);
assertEquals(e.eval("accessor2 = 2;", newCtxt), 2);
assertEquals(e.eval(sharedScript, newCtxt), 2);
final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000));
final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000));
t1.start();
t2.start();
t1.join();
t2.join();
assertEquals(e.eval(sharedScript), 1);
assertEquals(e.eval(sharedScript, newCtxt), 2);
assertEquals(e.eval("x"), 1);
assertEquals(e.eval("x", newCtxt), 2);
}