Search in sources :

Example 1 with BindingsAdapter

use of org.whole.lang.scripts.bindings.BindingsAdapter in project whole by wholeplatform.

the class ScriptsInterpreterVisitor method visit.

@Override
public void visit(Script entity) {
    Writer outputWriter = op().getWriter();
    IBindingManager bm = getBindings();
    BindingsAdapter bindingsAdapter = new BindingsAdapter(bm, true);
    LanguageName languageName = entity.getLanguageName();
    /*
		 * TODO
		 * if contains an upper level fragment fail
		 */
    Source source = entity.getSource();
    IEntity sourceRoot = EntityUtils.isFragment(source) ? source.wGetRoot() : source;
    IEntity result0 = bm.getResult();
    IVisitor ov = op().setVisitor(sourceRoot, 0, op().getVisitor(sourceRoot, 1));
    op().stagedVisit(source);
    IEntity sourceModel = bm.getResult();
    // workaround restore previous value (also for self and queryIterator?)
    bm.setResult(result0);
    op().setVisitor(sourceRoot, 0, ov);
    String sourceString = toPrettyPrintString(sourceModel);
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(languageName.getValue());
    if (scriptEngine == null)
        throw new VisitException("Cannot find an interpreter for the language: " + languageName);
    scriptEngine.setBindings(bindingsAdapter, ScriptContext.ENGINE_SCOPE);
    PrintWriter printWriter = new PrintWriter(outputWriter);
    scriptEngine.getContext().setWriter(printWriter);
    scriptEngine.getContext().setErrorWriter(printWriter);
    Object result = null;
    bm.wEnterScope();
    do {
        try {
            result = scriptEngine.eval(sourceString);
            break;
        } catch (ScriptException e) {
            bm.wExitScope();
            if (!bindingsAdapter.isAutoboxing())
                throw new VisitException("Script Interpreter failure.", e);
            bindingsAdapter.setAutoboxing(false);
            bm.wEnterScope();
        }
    } while (true);
    bm.wExitScope(true);
    if (result instanceof IEntity)
        bm.setResult((IEntity) result);
    else if (result != null)
        bm.setResult(BindingManagerFactory.instance.createSpecificValue(result));
}
Also used : IEntity(org.whole.lang.model.IEntity) IVisitor(org.whole.lang.visitors.IVisitor) VisitException(org.whole.lang.visitors.VisitException) ScriptEngineManager(javax.script.ScriptEngineManager) BindingsAdapter(org.whole.lang.scripts.bindings.BindingsAdapter) PrettyPrinterOperation.toPrettyPrintString(org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString) Source(org.whole.lang.scripts.model.Source) ScriptEngine(javax.script.ScriptEngine) ScriptException(javax.script.ScriptException) IBindingManager(org.whole.lang.bindings.IBindingManager) LanguageName(org.whole.lang.scripts.model.LanguageName) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Aggregations

PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 ScriptException (javax.script.ScriptException)1 IBindingManager (org.whole.lang.bindings.IBindingManager)1 IEntity (org.whole.lang.model.IEntity)1 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)1 BindingsAdapter (org.whole.lang.scripts.bindings.BindingsAdapter)1 LanguageName (org.whole.lang.scripts.model.LanguageName)1 Source (org.whole.lang.scripts.model.Source)1 IVisitor (org.whole.lang.visitors.IVisitor)1 VisitException (org.whole.lang.visitors.VisitException)1