public void timeOut(final long delay, final EventInstanceManager eim) {
if (disposed || eim == null) {
return;
}
eventTimer = EventTimer.getInstance().schedule(new Runnable() {
@Override
public void run() {
if (disposed || eim == null || em == null) {
return;
}
try {
em.getIv().invokeFunction("scheduledTimeout", eim);
} catch (NoSuchMethodException | ScriptException ex) {
System.out.println("Event name" + em.getName() + ", Instance name : " + name + ", method Name : scheduledTimeout:\n" + ex);
}
}
}, delay);
}
java类javax.script.ScriptException的实例源码
EventInstanceManager.java 文件源码
项目:Lucid2.0
阅读 16
收藏 0
点赞 0
评论 0
LexicalBindingTest.java 文件源码
项目:openjdk-jdk10
阅读 18
收藏 0
点赞 0
评论 0
/**
* Make sure lexically defined variables are accessible in other scripts.
*/
@Test
public void lexicalScopeTest() throws ScriptException {
final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6);
e.eval("let x; const y = 'world';");
assertEquals(e.eval("x = 'hello'"), "hello");
assertEquals(e.eval("typeof x"), "string");
assertEquals(e.eval("typeof y"), "string");
assertEquals(e.eval("x"), "hello");
assertEquals(e.eval("y"), "world");
assertEquals(e.eval("typeof this.x"), "undefined");
assertEquals(e.eval("typeof this.y"), "undefined");
assertEquals(e.eval("this.x"), null);
assertEquals(e.eval("this.y"), null);
}
JSONCompatibleTest.java 文件源码
项目:openjdk-jdk10
阅读 18
收藏 0
点赞 0
评论 0
/**
* Ensure that the old behaviour (every object is a Map) is unchanged.
*/
@Test
public void testNonWrapping() throws ScriptException {
final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
final Object val = engine.eval("({x: [1, {y: [2, {z: [3]}]}, [4, 5]]})");
final Map<String, Object> root = asMap(val);
final Map<String, Object> x = asMap(root.get("x"));
assertEquals(x.get("0"), 1);
final Map<String, Object> x1 = asMap(x.get("1"));
final Map<String, Object> y = asMap(x1.get("y"));
assertEquals(y.get("0"), 2);
final Map<String, Object> y1 = asMap(y.get("1"));
final Map<String, Object> z = asMap(y1.get("z"));
assertEquals(z.get("0"), 3);
final Map<String, Object> x2 = asMap(x.get("2"));
assertEquals(x2.get("0"), 4);
assertEquals(x2.get("1"), 5);
}
TrustedScriptEngineTest.java 文件源码
项目:openjdk-jdk10
阅读 22
收藏 0
点赞 0
评论 0
@Test
public void classFilterTest2() throws ScriptException {
final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
final ScriptEngine e = fac.getScriptEngine(new String[0], Thread.currentThread().getContextClassLoader(),
new ClassFilter() {
@Override
public boolean exposeToScripts(final String fullName) {
// don't allow anything that is not "java."
return fullName.startsWith("java.");
}
});
assertEquals(e.eval("typeof javax.script.ScriptEngine"), "object");
assertEquals(e.eval("typeof java.util.Vector"), "function");
try {
e.eval("Java.type('javax.script.ScriptContext')");
fail("should not reach here");
} catch (final ScriptException | RuntimeException se) {
if (! (se.getCause() instanceof ClassNotFoundException)) {
fail("ClassNotFoundException expected");
}
}
}
TrustedScriptEngineTest.java 文件源码
项目:openjdk-jdk10
阅读 18
收藏 0
点赞 0
评论 0
@Test
public void globalPerEngineTest() throws ScriptException {
final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
final String[] options = new String[] { "--global-per-engine" };
final ScriptEngine e = fac.getScriptEngine(options);
e.eval("function foo() {}");
final ScriptContext newCtx = new SimpleScriptContext();
newCtx.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
// all global definitions shared and so 'foo' should be
// visible in new Bindings as well.
assertTrue(e.eval("typeof foo", newCtx).equals("function"));
e.eval("function bar() {}", newCtx);
// bar should be visible in default context
assertTrue(e.eval("typeof bar").equals("function"));
}
NPCScriptManager.java 文件源码
项目:Lucid2.0
阅读 25
收藏 0
点赞 0
评论 0
public final void action(final MapleClient c, final byte mode, final byte type, final int selection) {
if (mode != -1) {
final NPCConversationManager cm = cms.get(c);
if (cm == null || cm.getLastMsg() > -1) {
return;
}
final Lock lock = c.getNPCLock();
lock.lock();
try {
if (cm.pendingDisposal) {
dispose(c);
} else {
c.setClickedNPC();
cm.getIv().invokeFunction("action", mode, type, selection);
}
} catch (final ScriptException | NoSuchMethodException e) {
System.err.println("Error executing NPC script. NPC ID : " + cm.getNpc() + ":" + e);
dispose(c);
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
InvocableTest.java 文件源码
项目:openjdk-jdk10
阅读 24
收藏 0
点赞 0
评论 0
@Test
/**
* Tests whether invocation of a JavaScript method through a variable arity
* Java method will pass the vararg array. Both non-vararg and vararg
* JavaScript methods are tested.
*
* @throws ScriptException
*/
public void variableArityInterfaceTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
e.eval(
"function test1(i, strings) {"
+ " return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)"
+ "}"
+ "function test2() {"
+ " return 'arguments[0] == ' + arguments[0] + ', arguments[1] instanceof java.lang.String[] == ' + (arguments[1] instanceof Java.type('java.lang.String[]')) + ', arguments[1] == ' + java.util.Arrays.toString(arguments[1])"
+ "}");
final VariableArityTestInterface itf = ((Invocable) e).getInterface(VariableArityTestInterface.class);
Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]");
Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]");
}
CommandJS.java 文件源码
项目:Kineticraft
阅读 17
收藏 0
点赞 0
评论 0
/**
* Setup javascript shortcuts such as 'server'.
*/
private void initJS() {
// Allow JS to load java classes.
Thread currentThread = Thread.currentThread();
ClassLoader previousClassLoader = currentThread.getContextClassLoader();
currentThread.setContextClassLoader(Core.getInstance().getClass().getClassLoader());
try {
this.engine = new ScriptEngineManager().getEngineByName("JavaScript");
engine.put("engine", engine); // Allow JS to do consistent eval.
engine.eval(new InputStreamReader(Core.getInstance().getResource("boot.js"))); // Run JS startup.
MechanicManager.getMechanics().forEach(this::bindObject); // Create shortcuts for all mechanics.
bindClass(Utils.class);
bindClass(KCPlayer.class);
bindClass(ReflectionUtil.class);
} catch (ScriptException ex) {
ex.printStackTrace();
Core.warn("Failed to initialize JS shortcuts.");
} finally {
// Set back the previous class loader.
currentThread.setContextClassLoader(previousClassLoader);
}
}
RenjinScriptTaskResultsR.java 文件源码
项目:invesdwin-context-r
阅读 19
收藏 0
点赞 0
评论 0
@Override
public int[] getIntegerVector(final String variable) {
if (isNull(variable)) {
return null;
} else {
try {
final Vector sexp = (Vector) engine.unwrap().eval(variable);
final int[] array = new int[sexp.length()];
for (int i = 0; i < array.length; i++) {
array[i] = sexp.getElementAsInt(i);
}
return array;
} catch (final ScriptException e) {
throw new RuntimeException(e);
}
}
}
RenjinScriptTaskResultsR.java 文件源码
项目:invesdwin-context-r
阅读 19
收藏 0
点赞 0
评论 0
@Override
public boolean[][] getBooleanMatrix(final String variable) {
if (isNull(variable)) {
return null;
} else {
try {
final Matrix sexp = new Matrix((Vector) engine.unwrap().eval(variable));
final boolean[][] matrix = new boolean[sexp.getNumRows()][];
for (int row = 0; row < matrix.length; row++) {
final boolean[] vector = new boolean[sexp.getNumCols()];
for (int col = 0; col < vector.length; col++) {
vector[col] = sexp.getElementAsInt(row, col) > 0;
}
matrix[row] = vector;
}
return matrix;
} catch (final ScriptException e) {
throw new RuntimeException(e);
}
}
}
StateMachine.java 文件源码
项目:UaicNlpToolkit
阅读 24
收藏 0
点赞 0
评论 0
protected void setJsSentence(INlpSentence s) throws CoreCriticalException {
try {
jsEngine.eval("var sentence = [];");
if ((Boolean) jsEngine.eval(String.format("sentence.positionInSentence == %d", s.getSentenceIndexInCorpus())))
return;
jsEngine.eval(String.format("sentence.positionInSentence = %d", s.getSentenceIndexInCorpus()));
for (int i = 0; i< s.getTokenCount(); i++) {
Token token = s.getToken(i);
jsEngine.eval(String.format("sentence[%d] = %s;", token.getTokenIndexInSentence(), tokenToJson(token)));
}
jsEngine.eval("sentence.spanAnnotations = []");
for (SpanAnnotation annotation : s.getSpanAnnotations()) {
jsEngine.eval(String.format("sentence.spanAnnotations.push(%s);", annotationToJson(annotation)));
for (int i = annotation.getStartTokenIndex(); i <= annotation.getEndTokenIndex(); i++) {
jsEngine.eval(String.format("sentence[%s].parentAnnotations.push(sentence.spanAnnotations[sentence.spanAnnotations.length - 1]);", i));
}
}
} catch (ScriptException e) {
throw new CoreCriticalException(e);
}
}
JSJavaScriptEngine.java 文件源码
项目:openjdk-jdk10
阅读 21
收藏 0
点赞 0
评论 0
private Object evalReader(Reader in, String filename) {
try {
engine.put(ScriptEngine.FILENAME, filename);
return engine.eval(in);
} catch (ScriptException sexp) {
System.err.println(sexp);
printError(sexp.toString(), sexp);
} finally {
try {
in.close();
} catch (IOException ioe) {
printError(ioe.toString(), ioe);
}
}
return null;
}
EventInstanceManager.java 文件源码
项目:Lucid2.0
阅读 16
收藏 0
点赞 0
评论 0
public final void schedule(final String methodName, final long delay) {
if (disposed) {
return;
}
EventTimer.getInstance().schedule(new Runnable() {
@Override
public void run() {
if (disposed || EventInstanceManager.this == null || em == null) {
return;
}
try {
em.getIv().invokeFunction(methodName, EventInstanceManager.this);
} catch (NullPointerException npe) {
} catch (NoSuchMethodException | ScriptException ex) {
System.out.println("Event name" + em.getName() + ", Instance name : " + name + ", method Name : " + methodName + ":\n" + ex);
}
}
}, delay);
}
ScriptEngineTest.java 文件源码
项目:openjdk-jdk10
阅读 21
收藏 0
点赞 0
评论 0
@Test
public void scriptObjectAutoConversionTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
e.eval("obj = { foo: 'hello' }");
e.put("Window", e.eval("Packages.jdk.nashorn.api.scripting.test.Window"));
assertEquals(e.eval("Window.funcJSObject(obj)"), "hello");
assertEquals(e.eval("Window.funcScriptObjectMirror(obj)"), "hello");
assertEquals(e.eval("Window.funcMap(obj)"), "hello");
assertEquals(e.eval("Window.funcJSObject(obj)"), "hello");
}
StringAccessTest.java 文件源码
项目:openjdk-jdk10
阅读 19
收藏 0
点赞 0
评论 0
@Test
public void accessStaticFieldString() throws ScriptException {
e.eval("var ps_string = SharedObject.publicStaticString;");
assertEquals(SharedObject.publicStaticString, e.get("ps_string"));
assertEquals("string", e.eval("typeof ps_string;"));
e.eval("SharedObject.publicStaticString = 'changedString';");
assertEquals("changedString", SharedObject.publicStaticString);
}
FieldCalculation.java 文件源码
项目:ctsms
阅读 16
收藏 0
点赞 0
评论 0
private static Invocable createEngine() throws ScriptException, IOException {
ScriptEngine engine = CoreUtil.getJsEngine();
//engine.put(ScriptEngine.FILENAME, FIELD_CALCULATION_JS_FILE_NAME);
//ScriptContext context = engine.getContext();
//context.setAttribute("window", file.getParent(), ScriptContext.ENGINE_SCOPE);//$NON-NLS-1$
//context.setAttribute("items", this.items.toArray(), ScriptContext.ENGINE_SCOPE); //$NON-NLS-1$
//context.setAttribute("baseDir", file.getParent(), ScriptContext.ENGINE_SCOPE);//$NON-NLS-1$
// context.setBindings(new Bindings(ScriptEngine.FILENAME, FIELD_CALCULATION_JS_FILE_NAME),ScriptContext.ENGINE_SCOPE));
// try {
// engine.eval(new FileReader(ENV_JS_FILE_NAME));
// engine.eval(new FileReader(SPRINTF_JS_FILE_NAME));
// engine.eval(new FileReader(DATE_JS_FILE_NAME));
// engine.eval(new FileReader(TIME_JS_FILE_NAME));
// engine.eval(new FileReader(STRIP_COMMENTS_JS_FILE_NAME));
// engine.eval(new FileReader(JQUERY_BASE64_JS_FILE_NAME));
// engine.eval(new FileReader(REST_API_SHIM_JS_FILE_NAME));
// engine.eval(new FileReader(LOCATION_DISTANCE_SHIM_JS_FILE_NAME));
// engine.eval(new FileReader(FIELD_CALCULATION_JS_FILE_NAME));
engine.eval(getJsFile(ENV_JS_FILE_NAME));
engine.eval(getJsFile(SPRINTF_JS_FILE_NAME));
engine.eval(getJsFile(JSON2_JS_FILE_NAME));
engine.eval(getJsFile(DATE_JS_FILE_NAME));
engine.eval(getJsFile(TIME_JS_FILE_NAME));
engine.eval(getJsFile(STRIP_COMMENTS_JS_FILE_NAME));
engine.eval(getJsFile(JQUERY_BASE64_JS_FILE_NAME));
engine.eval(getJsFile(REST_API_JS_FILE_NAME));
engine.eval(getJsFile(LOCATION_DISTANCE_JS_FILE_NAME));
engine.eval(getJsFile(FIELD_CALCULATION_JS_FILE_NAME));
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
return (Invocable) engine;
}
NumberBoxingTest.java 文件源码
项目:openjdk-jdk10
阅读 17
收藏 0
点赞 0
评论 0
@Test
public void accessFieldByteBoxing() throws ScriptException {
e.eval("var p_byte = o.publicByteBox;");
assertEqualsDouble(o.publicByteBox, "p_byte");
e.eval("o.publicByteBox = 16;");
assertEquals(Byte.valueOf((byte)16), o.publicByteBox);
}
BittrexExchange.java 文件源码
项目:bittrex4j
阅读 19
收藏 0
点赞 0
评论 0
private void performCloudFlareAuthorization() throws IOException {
try {
httpClientContext = httpFactory.createClientContext();
CloudFlareAuthorizer cloudFlareAuthorizer = new CloudFlareAuthorizer(httpClient,httpClientContext);
cloudFlareAuthorizer.getAuthorizationResult("https://bittrex.com");
} catch (ScriptException e) {
log.error("Failed to perform CloudFlare authorization",e);
}
}
MethodAccessTest.java 文件源码
项目:openjdk-jdk10
阅读 19
收藏 0
点赞 0
评论 0
@Test
public void accessMethodFloat() throws ScriptException {
assertEquals(0.0f, e.eval("o.floatMethod(0.0);"));
assertEquals(4.2f, e.eval("o.floatMethod(2.1);"));
assertEquals(0.0f, e.eval("o.floatMethod('0.0');"));
assertEquals(4.2f, e.eval("o.floatMethod('2.1');"));
}
JSONCompatibleTest.java 文件源码
项目:openjdk-jdk10
阅读 19
收藏 0
点赞 0
评论 0
/**
* Wrap an embedded array as a list.
*/
@Test
public void testWrapObjectWithArray() throws ScriptException {
final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
final Object val = engine.eval("Java.asJSONCompatible({x: [1, 2, 3]})");
assertEquals(asList(asMap(val).get("x")), Arrays.asList(1, 2, 3));
}
ReactorScriptManager.java 文件源码
项目:AeroStory
阅读 18
收藏 0
点赞 0
评论 0
public void act(MapleClient c, MapleReactor reactor) {
try {
ReactorActionManager rm = new ReactorActionManager(c, reactor);
Invocable iv = getInvocable("reactor/" + reactor.getId() + ".js", c);
// System.out.println(reactor.getId());
if (iv == null) {
return;
}
engine.put("rm", rm);
iv.invokeFunction("act");
} catch (final ScriptException | NoSuchMethodException | NullPointerException e) {
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", e);
}
}
ScopeTest.java 文件源码
项目:openjdk-jdk10
阅读 18
收藏 0
点赞 0
评论 0
/**
* 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);
}
ScopeTest.java 文件源码
项目:openjdk-jdk10
阅读 20
收藏 0
点赞 0
评论 0
public void method() throws ScriptException {
// a context with a new global bindings, same engine bindings
final ScriptContext sc = new SimpleScriptContext();
final Bindings global = new SimpleBindings();
sc.setBindings(global, ScriptContext.GLOBAL_SCOPE);
sc.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE);
global.put("text", "methodText");
final String value = engine.eval("text", sc).toString();
Assert.assertEquals(value, "methodText");
}
NumberAccessTest.java 文件源码
项目:openjdk-jdk10
阅读 18
收藏 0
点赞 0
评论 0
@Test
public void accessStaticFinalFieldChar() throws ScriptException {
e.eval("var psf_char = SharedObject.publicStaticFinalChar;");
assertEquals(SharedObject.publicStaticFinalChar, e.get("psf_char"));
e.eval("SharedObject.publicStaticFinalChar = 'Z';");
assertEquals('K', SharedObject.publicStaticFinalChar);
}
EventManager.java 文件源码
项目:Lucid2.0
阅读 24
收藏 0
点赞 0
评论 0
public void startInstance(MapleCharacter character, String leader) {
try {
EventInstanceManager eim = (EventInstanceManager) (iv.invokeFunction("setup", (Object) null));
eim.registerPlayer(character);
eim.setProperty("leader", leader);
eim.setProperty("guildid", String.valueOf(character.getGuildId()));
setProperty("guildid", String.valueOf(character.getGuildId()));
} catch (NoSuchMethodException | ScriptException ex) {
System.out.println("Event name : " + name + ", method Name : setup-Guild:\n" + ex);
}
}
JShellScriptEngineTest.java 文件源码
项目:JShellScriptEngine
阅读 26
收藏 0
点赞 0
评论 0
@Test(expected=NullPointerException.class)
public void throwExceptionNoCause() throws Throwable {
try {
scriptEngine.eval("throw new NullPointerException();");
} catch(ScriptException e) {
throw e.getCause();
}
}
MethodAccessTest.java 文件源码
项目:openjdk-jdk10
阅读 84
收藏 0
点赞 0
评论 0
@Test
public void accessMethodLong() throws ScriptException {
assertEquals((long)0, e.eval("o.longMethod(0);"));
assertEquals((long)400, e.eval("o.longMethod(200);"));
assertEquals((long) 0, e.eval("o.longMethod('0');"));
assertEquals((long) 400, e.eval("o.longMethod('200');"));
}
JsonParser.java 文件源码
项目:sonar-analyzer-commons
阅读 25
收藏 0
点赞 0
评论 0
JsonParser() {
try {
nashornParser = (JSObject) new ScriptEngineManager().getEngineByName("nashorn").eval("JSON.parse");
} catch (ScriptException e) {
throw new IllegalStateException("Can not get 'JSON.parse' from 'nashorn' script engine.", e);
}
}
Maths.java 文件源码
项目:betterQuizlit
阅读 17
收藏 0
点赞 0
评论 0
public void formatFunction() {
Double lower;
Double upper;
while(function.indexOf(']')>0) {
lower = Double.parseDouble(function.substring((function.indexOf('[')+1),function.indexOf(':')));
upper = Double.parseDouble(function.substring((function.indexOf(':')+1),function.indexOf(']')));
function = function.substring(0, function.indexOf('v'))+variableMaker(lower,upper)+function.substring(function.indexOf(']')+1);
}
try {
findAnswer();
} catch (ScriptException e) {
e.printStackTrace();
}
}
MultipleEngineTest.java 文件源码
项目:openjdk-jdk10
阅读 16
收藏 0
点赞 0
评论 0
@Test
public void createAndUseManyEngine() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e1 = m.getEngineByName("nashorn");
e1.eval("var x = 33; print(x);");
final ScriptEngine e2 = m.getEngineByName("nashorn");
e2.eval("try { print(x) } catch(e) { print(e); }");
}