use of javax.script.Bindings in project es6draft by anba.
the class ScriptEngineScopeTest method explicitDefaultBindingsAndEval.
@Test
public void explicitDefaultBindingsAndEval() throws ScriptException {
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
assertThat(bindings, notNullValue());
assertThat(bindings.entrySet(), is(empty()));
bindings.put("nullValue", null);
bindings.put("intValue", 1);
bindings.put("doubleValue", 1.5);
bindings.put("booleanValue", true);
bindings.put("stringValue", "Cepheus");
Object nullValue = engine.eval("nullValue", bindings);
Object intValue = engine.eval("intValue", bindings);
Object doubleValue = engine.eval("doubleValue", bindings);
Object stringValue = engine.eval("stringValue", bindings);
Object booleanValue = engine.eval("booleanValue", bindings);
assertThat(nullValue, nullValue());
assertThat(intValue, instanceOfWith(Number.class, is(numberCloseTo(1))));
assertThat(doubleValue, instanceOfWith(Number.class, is(numberCloseTo(1.5))));
assertThat(stringValue, instanceOfWith(String.class, is("Cepheus")));
assertThat(booleanValue, instanceOfWith(Boolean.class, sameInstance(Boolean.TRUE)));
}
use of javax.script.Bindings in project gremlin by tinkerpop.
the class GremlinGroovyScriptEngineTest method untestUsingClassesIsGood.
public void untestUsingClassesIsGood() throws ScriptException {
GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
final Graph g = TinkerGraphFactory.createTinkerGraph();
final Bindings bindings = engine.createBindings();
bindings.put("g", g);
// works when it's all defined together
assertEquals(true, engine.eval("class c { static def isVadas(v){v.name=='vadas'}};c.isVadas(g.v(2))", bindings));
// let's reset this piece and make sure isVadas is not hanging around.
engine = new GremlinGroovyScriptEngine();
// validate that isVadas throws an exception since it is not defined
try {
engine.eval("c.isVadas(g.v(2))", bindings);
// fail the test if the above doesn't throw an exception
fail();
} catch (Exception ex) {
// this is good...we want this. it means isVadas isn't hanging about
}
// now...define the class separately on its own in one script...
// HERE'S and AWKWARD BIT.........
// YOU HAVE TO END WITH: null;
// ....OR ELSE YOU GET:
// javax.script.ScriptException: javax.script.ScriptException:
// org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: c.main()
// is applicable for argument types: ([Ljava.lang.String;) values: [[]]
// WOULD BE NICE IF WE DIDN'T HAVE TO DO THAT
engine.eval("class c { static def isVadas(v){v.name=='vadas'}};null;", bindings);
// make sure the class works on its own...this generates: groovy.lang.MissingPropertyException: No such property: c for class: Script2
assertEquals(true, engine.eval("c.isVadas(g.v(2))", bindings));
}
use of javax.script.Bindings in project gremlin by tinkerpop.
the class GremlinGroovyScriptEngineTest method testThreadSafetyOnEngine.
public void testThreadSafetyOnEngine() throws Exception {
final ScriptEngine engine = new GremlinGroovyScriptEngine(10);
int runs = 500;
final CountDownLatch latch = new CountDownLatch(runs);
final List<String> names = Arrays.asList("marko", "peter", "josh", "vadas", "stephen", "pavel", "matthias");
final Random random = new Random();
for (int i = 0; i < runs; i++) {
new Thread() {
public void run() {
String name = names.get(random.nextInt(names.size() - 1));
try {
final Bindings bindings = engine.createBindings();
bindings.put("g", TinkerGraphFactory.createTinkerGraph());
bindings.put("name", name);
final Object result = engine.eval("pipe = g.V('name',name); if(pipe.hasNext()) { pipe.out.count() } else { null }", bindings);
if (name.equals("stephen") || name.equals("pavel") || name.equals("matthias"))
assertNull(result);
else
assertNotNull(result);
} catch (ScriptException e) {
//System.out.println(e);
assertFalse(true);
}
latch.countDown();
}
}.start();
}
latch.await();
}
use of javax.script.Bindings in project gremlin by tinkerpop.
the class GremlinGroovyScriptEngineTest method testEngineVsCompiledCosts.
public void testEngineVsCompiledCosts() throws Exception {
final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
Bindings bindings = engine.createBindings();
bindings.put("g", TinkerGraphFactory.createTinkerGraph());
int runs = 1000;
long totalTime = 0l;
for (int i = 0; i < runs; i++) {
long time = System.currentTimeMillis();
CompiledScript script = engine.compile("g.v(1).out().count()");
script.eval(bindings);
totalTime += System.currentTimeMillis() - time;
}
System.out.println("Multi-compiled script runtime for " + runs + " runs: " + totalTime);
totalTime = 0l;
for (int i = 0; i < runs; i++) {
long time = System.currentTimeMillis();
engine.eval("g.v(1).out().count()", bindings);
totalTime += System.currentTimeMillis() - time;
}
System.out.println("Evaluated script runtime for " + runs + " runs: " + totalTime);
totalTime = 0l;
CompiledScript script = engine.compile("g.v(1).out().count()");
for (int i = 0; i < runs; i++) {
long time = System.currentTimeMillis();
script.eval(bindings);
totalTime += System.currentTimeMillis() - time;
}
System.out.println("Compiled script runtime for " + runs + " runs: " + totalTime);
}
use of javax.script.Bindings in project frames by tinkerpop.
the class GremlinGroovyAnnotationHandler method getBindings.
private Bindings getBindings(final Method method, final Object[] arguments) {
Bindings bindings = engine.createBindings();
Annotation[][] allParameterAnnotations = method.getParameterAnnotations();
for (int pCount = 0; pCount < allParameterAnnotations.length; pCount++) {
Annotation[] parameterAnnotations = allParameterAnnotations[pCount];
for (int aCount = 0; aCount < parameterAnnotations.length; aCount++) {
Annotation paramAnnotation = parameterAnnotations[aCount];
if (paramAnnotation instanceof GremlinParam) {
bindings.put(((GremlinParam) paramAnnotation).value(), arguments[pCount]);
break;
}
}
}
return bindings;
}
Aggregations