use of javax.script.Bindings in project es6draft by anba.
the class ScriptEngineScopeTest method globalScopeAccessNewContextWithGlobalSimpleBindings.
@Test
public void globalScopeAccessNewContextWithGlobalSimpleBindings() throws ScriptException {
Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
globalScope.put("globalVar", "Sigma Sagittarii");
ScriptContext context = new SimpleScriptContext();
context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
context.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
assertThat(engine.eval("globalVar", context), instanceOfWith(String.class, is("Sigma Sagittarii")));
}
use of javax.script.Bindings in project es6draft by anba.
the class ScriptEngineScopeTest method globalScopeAccess.
@Test
public void globalScopeAccess() throws ScriptException {
Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
globalScope.put("globalVar", "Aldebaran");
assertThat(engine.eval("globalVar"), instanceOfWith(String.class, is("Aldebaran")));
}
use of javax.script.Bindings in project frames by tinkerpop.
the class GremlinGroovyAnnotationHandler method processVertex.
public Object processVertex(final GremlinGroovy annotation, final Method method, final Object[] arguments, final FramedGraph framedGraph, final Vertex vertex) {
try {
final CompiledScript script = this.engine.compile(annotation.value());
final Bindings bindings = getBindings(method, arguments);
bindings.put(IT, vertex);
bindings.put(G, framedGraph);
final Object result = script.eval(bindings);
// TODO: Deprecate the use of _() and replace with it
if (result instanceof Pipe & annotation.value().startsWith(PIPE)) {
LOGGER.warning("_() is deprecated in favor of using 'it' to represent the framed vertex");
((Pipe) result).setStarts(new SingleIterator<Element>(vertex));
}
if (annotation.frame()) {
if (result instanceof Iterable) {
final FramedVertexIterable r = new FramedVertexIterable(framedGraph, (Iterable) result, ClassUtilities.getGenericClass(method));
return (ClassUtilities.returnsIterable(method)) ? r : r.iterator().hasNext() ? r.iterator().next() : null;
} else if (ClassUtilities.returnsMap(method)) {
return new FramedVertexMap(framedGraph, (Map) result, ClassUtilities.getGenericClass(method));
} else if (result instanceof Vertex) {
return framedGraph.frame((Vertex) result, ClassUtilities.getGenericClass(method));
} else {
throw new IllegalStateException("The returned object can not be framed: " + result.getClass());
}
} else {
return result;
}
} catch (ScriptException e) {
//Preserve original exception functionality.
ExceptionUtils.sneakyThrow(e);
return null;
}
}
use of javax.script.Bindings in project gremlin by tinkerpop.
the class GremlinGroovyScriptEngineTest method testBlowTheHeap.
/**
* Tries to force the out of memory exceptions that commonly seem to happen with scriptengine.
*/
public void testBlowTheHeap() throws ScriptException {
final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
final Graph g = TinkerGraphFactory.createTinkerGraph();
final String[] gremlins = new String[] { "g.v(xxx).out.toList()", "g.v(xxx).in.toList()", "g.v(xxx).out.loop(1){true}{true}.toList()", "g.v(xxx).groupCount.cap.next()" };
/******************START PARAMETERIZE GREMLIN***************
* parameterized gremlin doesn't blow the heap
*/
long parameterizedStartTime = System.currentTimeMillis();
System.out.println("Try to blow the heap with parameterized Gremlin.");
try {
for (int ix = 0; ix < 50001; ix++) {
final Bindings bindings = engine.createBindings();
bindings.put("g", g);
bindings.put("xxx", ((ix % 4) + 1));
Object x = engine.eval(gremlins[ix % 4], bindings);
if (ix > 0 && ix % 5000 == 0) {
System.out.println(String.format("%s scripts processed in %s (ms) - rate %s (ms/q).", ix, System.currentTimeMillis() - parameterizedStartTime, Double.valueOf(System.currentTimeMillis() - parameterizedStartTime) / Double.valueOf(ix)));
}
}
} catch (OutOfMemoryError oome) {
assertTrue(false);
}
/*************************DONE PARAMETERIZE GREMLIN*****************/
long notParameterizedStartTime = System.currentTimeMillis();
System.out.println("Try to blow the heap with non-parameterized Gremlin.");
try {
for (int ix = 0; ix < 15001; ix++) {
final Bindings bindings = engine.createBindings();
bindings.put("g", g);
engine.eval(String.format("g.v(%s)", ix), bindings);
if (ix > 0 && ix % 5000 == 0) {
System.out.println(String.format("%s scripts processed in %s (ms) - rate %s (ms/q).", ix, System.currentTimeMillis() - notParameterizedStartTime, Double.valueOf(System.currentTimeMillis() - notParameterizedStartTime) / Double.valueOf(ix)));
}
}
} catch (OutOfMemoryError oome) {
assertTrue(false);
}
}
use of javax.script.Bindings in project gremlin by tinkerpop.
the class GremlinGroovyScriptEngineTest method testFunctionsUsedInClosure.
public void testFunctionsUsedInClosure() throws ScriptException {
GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
final Graph g = TinkerGraphFactory.createTinkerGraph();
final Bindings bindings = engine.createBindings();
bindings.put("g", g);
// this works on its own when the function and the line that uses it is in one "script". this is the
// current workaround
assertEquals(g.getVertex(2), engine.eval("def isVadas(v){v.name=='vadas'};g.V.filter{isVadas(it)}.next()", 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("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 function separately on its own in one script
engine.eval("def isVadas(v){v.name=='vadas'}", bindings);
// make sure the function works on its own...no problem
assertEquals(true, engine.eval("isVadas(g.v(2))", bindings));
// make sure the function works in a closure...this generates a StackOverflowError
assertEquals(g.getVertex(2), engine.eval("g.V.filter{isVadas(it)}.next()", bindings));
}
Aggregations