Search in sources :

Example 16 with CompiledScript

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))));
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 17 with CompiledScript

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;
    }
}
Also used : CompiledScript(javax.script.CompiledScript) Vertex(com.tinkerpop.blueprints.Vertex) FramedVertexIterable(com.tinkerpop.frames.structures.FramedVertexIterable) Element(com.tinkerpop.blueprints.Element) FramedVertexMap(com.tinkerpop.frames.structures.FramedVertexMap) Pipe(com.tinkerpop.pipes.Pipe) Bindings(javax.script.Bindings) ScriptException(javax.script.ScriptException) FramedVertexIterable(com.tinkerpop.frames.structures.FramedVertexIterable) Map(java.util.Map) FramedVertexMap(com.tinkerpop.frames.structures.FramedVertexMap)

Example 18 with CompiledScript

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();
}
Also used : CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) CountDownLatch(java.util.concurrent.CountDownLatch) Bindings(javax.script.Bindings)

Example 19 with CompiledScript

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);
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) Script(org.mozilla.javascript.Script) CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) RhinoException(org.mozilla.javascript.RhinoException) IOException(java.io.IOException)

Example 20 with CompiledScript

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();
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Aggregations

CompiledScript (javax.script.CompiledScript)25 Bindings (javax.script.Bindings)14 ScriptException (javax.script.ScriptException)9 ScriptContext (javax.script.ScriptContext)8 Test (org.junit.Test)8 SimpleBindings (javax.script.SimpleBindings)6 SimpleScriptContext (javax.script.SimpleScriptContext)5 Compilable (javax.script.Compilable)4 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)3 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 CachedScript (org.apache.sling.scripting.api.CachedScript)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 ArrayList (java.util.ArrayList)2 ScriptNameAwareReader (org.apache.sling.scripting.core.ScriptNameAwareReader)2 Context (org.mozilla.javascript.Context)2 Script (org.mozilla.javascript.Script)2 Invoker (com.alibaba.dubbo.rpc.Invoker)1