use of javax.script.CompiledScript in project es6draft by anba.
the class CompilableTest method compileStringWithContext.
@Test
public void compileStringWithContext() throws ScriptException {
CompiledScript script = compilable.compile("numberVal * 2");
ScriptContext context = new SimpleScriptContext();
context.setAttribute("numberVal", 7, ScriptContext.ENGINE_SCOPE);
assertThat(script.eval(context), instanceOfWith(Number.class, is(numberCloseTo(14))));
}
use of javax.script.CompiledScript 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.CompiledScript in project gremlin by tinkerpop.
the class GremlinGroovyScriptEngineTest method testThreadSafetyOnCompiledScript.
public void testThreadSafetyOnCompiledScript() throws Exception {
final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(10);
final CompiledScript script = engine.compile("pipe = g.V('name',name); if(pipe.hasNext()) { pipe.out.count() } else { null }");
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);
Object result = script.eval(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.CompiledScript in project OpenAM by OpenRock.
the class RhinoScriptEngine method compile.
/**
* {@inheritDoc}
*/
@Override
public CompiledScript compile(final Reader reader) throws ScriptException {
Reject.ifNull(reader);
final Context context = factory.getContext();
try {
// Use configured ScriptContext from parent class, if specified
final String filename = getFilename(getContext());
final Script compiledScript = context.compileReader(reader, filename, 1, null);
return new RhinoCompiledScript(this, compiledScript);
} catch (RhinoException ex) {
throw convertException(ex);
} catch (IOException ex) {
throw new ScriptException(ex);
} finally {
factory.releaseContext(context);
}
}
use of javax.script.CompiledScript in project OpenAM by OpenRock.
the class SandboxedGroovyScriptEngineTest method shouldApplySandboxToCompiledScripts.
@Test
public void shouldApplySandboxToCompiledScripts() throws Exception {
// Given
String script = "1 + 1";
ScriptContext context = new SimpleScriptContext();
CompiledScript mockCompiledScript = mock(CompiledScript.class);
given(mockScriptEngine.compile(script)).willReturn(mockCompiledScript);
// When
CompiledScript compiledScript = testEngine.compile(script);
compiledScript.eval(context);
// Then
verify(mockValueFilter).register();
verify(mockCompiledScript).eval(context);
verify(mockValueFilter).unregister();
}
Aggregations