import org.mozilla.javascript.*;

public class Repl {

	public static void main(String[] args) {
		System.out.println("OK.");
	}
	
	public static Object evaluate(Scriptable repl, Object object, String code,
			java.io.PrintWriter out) {

		try {
			
			Context context = Context.enter(); //engine.getContextFactory().enterContext()
            //context.setErrorReporter(new ToolErrorReporter(false, System.out));
            context.setOptimizationLevel(-1);
			Scriptable scope = context.initStandardObjects();
            scope = Context.toObject(object, scope);
            scope.put("repl", scope, repl);
            scope.put("$", scope, repl);
            Object result = context.evaluateString(scope, code, "", -1, null);
            return result;
            //return Context.toString(result);

		} catch (Exception ex) {

			return ex.toString();

		} finally {

			Context.exit();

		}
	}
}
