@Override public Object eval(Reader reader, ScriptContext context) throws ScriptException { try { // Get engine bindings and send them to Clojure Bindings engineBindings = context.getBindings(ScriptContext.ENGINE_SCOPE); engineBindings.entrySet().forEach((entry) -> Var.intern(Namespace.findOrCreate(NAMESPACE_SYMBOL), Symbol.create(entry.getKey().intern()), entry.getValue(), true)); Var.pushThreadBindings( RT.map(RT.CURRENT_NS, RT.CURRENT_NS.deref(), RT.IN, new LineNumberingPushbackReader(context.getReader()), RT.OUT, context.getWriter(), RT.ERR, context.getErrorWriter())); Object result = Compiler.load(reader); return result; } catch (Exception e) { throw new ScriptException(e); } finally { Namespace.remove(NAMESPACE_SYMBOL); } }
@Override public Object getVariable(String name) { synchronized (context) { int scope = context.getAttributesScope(name); if (scope != -1) { return context.getAttribute(name, scope); } // Redirect script output to context writer, if out var is not already provided if ("out".equals(name)) { final Writer writer = context.getWriter(); if (writer != null) { return (writer instanceof PrintWriter) ? (PrintWriter) writer : new PrintWriter(writer, true); } } // Provide access to engine context, if context var is not already provided if ("context".equals(name)) { return context; } } throw new MissingPropertyException(name, getClass()); }
@Override public Object getVariable(String name) { synchronized (ctx) { int scope = ctx.getAttributesScope(name); if (scope != -1) { return ctx.getAttribute(name, scope); } // Redirect script output to context writer, if out var is not already provided if ("out".equals(name)) { Writer writer = ctx.getWriter(); if (writer != null) { return (writer instanceof PrintWriter) ? (PrintWriter) writer : new PrintWriter(writer, true); } } // Provide access to engine context, if context var is not already provided if ("context".equals(name)) { return ctx; } } throw new MissingPropertyException(name, getClass()); }
/** * Creates the {@code ScriptContext} using a {@link GremlinScriptContext} which avoids a significant amount of * additional object creation on script evaluation. */ @Override protected ScriptContext getScriptContext(final Bindings nn) { final GremlinScriptContext ctxt = new GremlinScriptContext(context.getReader(), context.getWriter(), context.getErrorWriter()); final Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE); if (gs != null) ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE); if (nn != null) { ctxt.setBindings(nn, ScriptContext.ENGINE_SCOPE); } else { throw new NullPointerException("Engine scope Bindings may not be null."); } return ctxt; }
@Override public Writer getWriter() { return context.getWriter(); }
/** * Gives access to the engine scope output writer (defaults to System.out). * @return the engine output writer */ public PrintWriter getOut() { final Writer out = context.getWriter(); if (out instanceof PrintWriter) { return (PrintWriter) out; } else if (out != null) { return new PrintWriter(out, true); } else { return null; } }
/** * Gives access to the engine scope output writer (defaults to System.out). * @return the engine output writer */ public PrintWriter getOut() { final Writer out = context.getWriter(); if (out instanceof PrintWriter) { return (PrintWriter) out; } else if (out != null) { return new PrintWriter(out, true); } else { return null; } }
/** * Gives access to the engine scope output writer (defaults to System.out). * @return the engine output writer */ public PrintWriter getOut() { final Writer out = context.getWriter(); if (out instanceof PrintWriter) { return (PrintWriter) out; } else if (out != null) { return new PrintWriter(out, true); } else { return null; } }
private void writeStdout(String msg) { try { context.getWriter().write(msg); } catch (IOException e) { logger.error(e.getMessage()); throw new RuntimeException(e); } }
@Override public void close() throws IOException { scriptEngine.getContext().getWriter().close(); } }
@Override public void stopStreaming() { JupyterStreamWriter streamWriter = ((JupyterStreamWriter) this.engine.getContext().getWriter()); if (streamWriter != null) { streamWriter.stopStreaming(); } }
ScriptingConsole(ScriptContext context) { this.reader = new BufferedReader(context.getReader()); this.writer = printWriter(context.getWriter()); this.errorWriter = printWriter(context.getErrorWriter()); }
private JsContext(ScriptEngine engine) { ScriptContext sc = engine.getContext(); this.reader = sc.getReader(); this.writer = sc.getWriter(); this.errorWriter = sc.getErrorWriter(); }
public ScriptContextImpl(ScriptContext parent, Map<String, Object> model) { this.engineScope = new BindingsImpl(model); this.errorWriter = parent.getErrorWriter(); this.globalScope = parent.getBindings(GLOBAL_SCOPE); this.reader = parent.getReader(); this.writer = parent.getWriter(); }
/** * Sets the IOLibray's standard input/output with the streams specified in the ScriptContext * @param sc the ScriptContext to use for the next evaluation */ private void setupStandardIO(ScriptContext sc) { IOLibrary ioLib = (IOLibrary) prolog.getLibrary("alice.tuprolog.lib.IOLibrary"); if(ioLib != null) { ioLib.setStandardInput(new InputStreamAdapter(sc.getReader())); outputWriter = sc.getWriter(); errorWriter = sc.getErrorWriter(); } }
protected void setup() { final ScriptContext context = getContext(); final Reader reader = context.getReader(); if (reader != null) { log().warn("Beanshell does not support redirecting the input"); } final Writer writer = context.getWriter(); if (writer != null) interpreter.setOut(new PrintStream( new WriterOutputStream(writer))); final Writer errorWriter = context.getErrorWriter(); if (errorWriter != null) interpreter.setErr(new PrintStream( new WriterOutputStream(errorWriter))); }
protected ScriptContext getScriptContext(Bindings bindings) { if (bindings == null) { throw new NullPointerException("null bindings in engine scope"); } ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); Bindings global = getBindings(ScriptContext.GLOBAL_SCOPE); if (global != null) { newContext.setBindings(global, ScriptContext.GLOBAL_SCOPE); } newContext.setReader(context.getReader()); newContext.setWriter(context.getWriter()); newContext.setErrorWriter(context.getErrorWriter()); return newContext; }
private Object eval(PyCode code, ScriptContext context) throws ScriptException { try { interp.setIn(context.getReader()); interp.setOut(context.getWriter()); interp.setErr(context.getErrorWriter()); interp.setLocals(new PyScriptEngineScope(this, context)); return interp.eval(code).__tojava__(Object.class); } catch (PyException pye) { throw scriptException(pye); } }
Object eval(Function evaluator, LispObject code, ScriptContext ctx) throws ScriptException { LispObject retVal = null; Stream outStream = new Stream(Symbol.SYSTEM_STREAM, ctx.getWriter()); Stream inStream = new Stream(Symbol.SYSTEM_STREAM, ctx.getReader()); retVal = evaluator.execute(makeBindings(ctx.getBindings(ScriptContext.GLOBAL_SCOPE)), makeBindings(ctx.getBindings(ScriptContext.ENGINE_SCOPE)), inStream, outStream, code, new JavaObject(ctx)); return retVal.javaInstance(); }
public Object eval(Bindings bindings) throws ScriptException { ScriptEngine engine = getEngine(); SimpleScriptContext context = new SimpleScriptContext(); context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); context.setBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE); context.setReader(engine.getContext().getReader()); context.setWriter(engine.getContext().getWriter()); return eval(context); }