package net.greghaines.arithmeticparser.js;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import net.greghaines.arithmeticparser.ArithmeticParser;

/**
 * An implementation that uses Java's built-in JavaScript parser to evaluate the expression.
 * 
 * @author Greg Haines
 */
public class JavaScriptArithmeticParser implements ArithmeticParser {

    /**
     * {@inheritDoc}
     */
    @Override
    public double evaluate(final String expression) {
        final double result;
        try {
            final ScriptEngineManager factory = new ScriptEngineManager();
            final ScriptEngine engine = factory.getEngineByName("JavaScript");
            final Object resultObj = engine.eval(expression);
            if (resultObj instanceof Number) {
                result = ((Number) resultObj).doubleValue();
            } else if (resultObj instanceof String) {
                result = Double.parseDouble((String) resultObj);
            } else {
                throw new IllegalStateException("Unknown return type: " + resultObj);
            }
        } catch (ScriptException se) {
            throw new RuntimeException(se);
        }
        return result;
    }
}

@@ -411,202 +411,222 @@
 r %7B%0A
-%0A
     
-/**
 %0A    
- * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public double evaluate(final String expression) %7B%0A        final double result;%0A        try %7B
+private final ScriptEngine engine;%0A    %0A    public JavaScriptArithmeticParser() %7B%0A        this(%22JavaScript%22);%0A    %7D
 %0A    
+%0A
     
-    
+public JavaScriptArithmeticParser(
 final S
-criptE
+tring e
 ngine
-Manager factory
+Name) %7B%0A        this.engine
  = n
@@ -653,88 +653,192 @@
 er()
-;%0A            
+.getEngineByName(engineName);%0A    %7D%0A%0A    /**%0A     * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public double evaluate(
 final S
-criptEngine engine = factory.getEngineByName(%22JavaScript%22);
+tring expression) %7B%0A        final double result;%0A        try %7B
 %0A   
@@ -871,16 +871,21 @@
 ltObj = 
+this.
 engine.e

Tweak the JavaScript implementation for ~4x faster execution.