package org.csstudio.opibuilder.script;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;

import org.csstudio.opibuilder.editparts.AbstractBaseEditPart;
import org.csstudio.simplepv.IPV;

/**
 * This is the implementation of {@link AbstractScriptStore} for the default javascript script engine embedded in Java. 
 * The default javascript engine is Rhino for Java 7, Nashorn for Java 8.
 */
public class JavaScriptStore extends AbstractScriptStore {

	private ScriptEngine engine;
	private Bindings bindings;
	private CompiledScript script;

	public JavaScriptStore(final ScriptData scriptData, final AbstractBaseEditPart editpart,
			final IPV[] pvArray) throws Exception {		
		super(scriptData, editpart, pvArray);		
		
	}
	
	@Override
	protected void initScriptEngine() throws Exception {
		engine = ScriptStoreFactory.getJavaScriptEngine();
		bindings = engine.createBindings();
		bindings.put(ScriptService.WIDGET, getEditPart());
		bindings.put(ScriptService.PVS, getPvArray());
		bindings.put(ScriptService.DISPLAY, getDisplayEditPart());
		bindings.put(ScriptService.WIDGET_CONTROLLER_DEPRECIATED, getEditPart());
		bindings.put(ScriptService.PV_ARRAY_DEPRECIATED, getPvArray());
	}

	@Override
	protected void compileString(String string) throws Exception {
		((Compilable) engine).compile(string);
	}

	@Override
	protected void compileInputStream(InputStream s) throws Exception {
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(s))) {
			((Compilable) engine).compile(reader);
		}
	}

	@Override
	protected void execScript(IPV triggerPV) throws Exception {
		bindings.put(ScriptService.TRIGGER_PV, triggerPV);
		script.eval(bindings);
	}
}

@@ -1440,16 +1440,25 @@
 ion %7B%0A%09%09
+script = 
 ((Compil
@@ -1654,16 +1654,25 @@
 )) %7B%0A%09%09%09
+script = 
 ((Compil
@@ -1704,17 +1704,53 @@
 der);%0A%09%09
-%7D
+%09s.close();%0A%09%09%09reader.close();%0A%09%09%7D%0A%09%09
 %0A%09%7D%0A%0A%09@O

fix from merge #528