-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) Impl script engine SPI and refactor AviatorScriptEngine etc., #…
- Loading branch information
1 parent
1e7d8f8
commit ae675ab
Showing
9 changed files
with
150 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.googlecode.aviator.script; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import javax.script.AbstractScriptEngine; | ||
import javax.script.Bindings; | ||
|
@@ -11,38 +12,57 @@ | |
import javax.script.SimpleBindings; | ||
import com.googlecode.aviator.AviatorEvaluator; | ||
import com.googlecode.aviator.AviatorEvaluatorInstance; | ||
import com.googlecode.aviator.utils.Utils; | ||
|
||
|
||
/** | ||
* Aviator Expression engine | ||
* Aviator script engine | ||
* | ||
* @author [email protected] | ||
* @author dennis([email protected]) | ||
* @date 2011-1-18 上午11:03:34 | ||
* @version | ||
*/ | ||
public class AviatorScriptEngine extends AbstractScriptEngine implements Compilable { | ||
|
||
// 缓存编译结果 | ||
private boolean cached = true; | ||
private final AviatorScriptEngineFactory factory; | ||
private AviatorEvaluatorInstance evaluator; | ||
private final AviatorEvaluatorInstance engine; | ||
|
||
|
||
public AviatorScriptEngine(AviatorScriptEngineFactory factory) { | ||
public AviatorScriptEngine() { | ||
super(); | ||
this.factory = AviatorScriptEngineFactory.newInstance(); | ||
this.engine = AviatorEvaluator.newInstance(); | ||
} | ||
|
||
|
||
public AviatorScriptEngine(final Bindings n) { | ||
super(n); | ||
this.factory = AviatorScriptEngineFactory.newInstance(); | ||
this.engine = AviatorEvaluator.newInstance(); | ||
} | ||
|
||
|
||
public AviatorScriptEngine(final AviatorScriptEngineFactory factory) { | ||
this.factory = factory; | ||
this.evaluator = AviatorEvaluator.newInstance(); | ||
this.engine = AviatorEvaluator.newInstance(); | ||
} | ||
|
||
|
||
@Override | ||
public CompiledScript compile(String script) throws ScriptException { | ||
return new CompiledAviatorScript(this, evaluator.compile(script, this.cached)); | ||
public CompiledScript compile(final String script) throws ScriptException { | ||
return new CompiledAviatorScript(this, this.engine.compile(script, this.cached)); | ||
} | ||
|
||
|
||
@Override | ||
public CompiledScript compile(Reader script) throws ScriptException { | ||
throw new UnsupportedOperationException(); | ||
public CompiledScript compile(final Reader script) throws ScriptException { | ||
try { | ||
return this.compile(Utils.readFully(script)); | ||
} catch (IOException e) { | ||
throw new ScriptException(e); | ||
} | ||
} | ||
|
||
|
||
|
@@ -51,16 +71,19 @@ public Bindings createBindings() { | |
return new SimpleBindings(); | ||
} | ||
|
||
|
||
@Override | ||
public Object eval(String script, ScriptContext context) throws ScriptException { | ||
public Object eval(final String script, final ScriptContext context) throws ScriptException { | ||
return this.compile(script).eval(context); | ||
} | ||
|
||
|
||
@Override | ||
public Object eval(Reader reader, ScriptContext context) throws ScriptException { | ||
throw new UnsupportedOperationException(); | ||
public Object eval(final Reader reader, final ScriptContext context) throws ScriptException { | ||
try { | ||
return eval(Utils.readFully(reader), context); | ||
} catch (IOException e) { | ||
throw new ScriptException(e); | ||
} | ||
} | ||
|
||
|
||
|
@@ -75,7 +98,12 @@ public boolean isCached() { | |
} | ||
|
||
|
||
public void setCached(boolean cached) { | ||
/** | ||
* Setting whether to cache the compiled exprssion, defualt is true(caching). | ||
* | ||
* @param cached true means enable caching. | ||
*/ | ||
public void setCached(final boolean cached) { | ||
this.cached = cached; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
|
||
/** | ||
* Aviator script engine factory. | ||
* | ||
* @author [email protected] | ||
* @author dennis | ||
|
@@ -19,22 +20,27 @@ | |
*/ | ||
public class AviatorScriptEngineFactory implements ScriptEngineFactory { | ||
|
||
private static final List<String> extensions = | ||
Collections.unmodifiableList(Arrays.asList(new String[] {})); | ||
private static final List<String> extensions = Collections.unmodifiableList(Arrays.asList(".av")); | ||
private static final List<String> mimeTypes = | ||
Collections.unmodifiableList(Arrays.asList(new String[] {"text/aviator"})); | ||
private static final List<String> names = | ||
Collections.unmodifiableList(Arrays.asList(new String[] {"Aviator", "aviator"})); | ||
Collections.unmodifiableList(Arrays.asList("text/aviator")); | ||
private static final List<String> names = Collections | ||
.unmodifiableList(Arrays.asList("Aviator", "aviator", "aviatorscript", "AviatorScript")); | ||
|
||
private static final Map<String, String> parameterMap = new HashMap<String, String>(); | ||
static { | ||
parameterMap.put(ScriptEngine.ENGINE, "Aviator"); | ||
parameterMap.put(ScriptEngine.ENGINE_VERSION, AviatorEvaluator.VERSION); | ||
parameterMap.put(ScriptEngine.LANGUAGE, "A high performance expression evaluator for java"); | ||
parameterMap.put(ScriptEngine.LANGUAGE, | ||
"A high performance scripting language hosted on the JVM"); | ||
parameterMap.put(ScriptEngine.LANGUAGE_VERSION, AviatorEvaluator.VERSION); | ||
} | ||
|
||
|
||
public static final AviatorScriptEngineFactory newInstance() { | ||
return new AviatorScriptEngineFactory(); | ||
} | ||
|
||
|
||
@Override | ||
public String getEngineName() { | ||
return parameterMap.get(ScriptEngine.ENGINE); | ||
|
@@ -66,7 +72,7 @@ public String getLanguageVersion() { | |
|
||
|
||
@Override | ||
public String getMethodCallSyntax(String obj, String m, String... args) { | ||
public String getMethodCallSyntax(final String obj, final String m, final String... args) { | ||
StringBuilder sb = new StringBuilder(m); | ||
sb.append("(").append(obj); | ||
if (args != null) { | ||
|
@@ -93,20 +99,24 @@ public List<String> getNames() { | |
|
||
|
||
@Override | ||
public String getOutputStatement(String toDisplay) { | ||
public String getOutputStatement(final String toDisplay) { | ||
return "print(+" + toDisplay + ")"; | ||
} | ||
|
||
|
||
@Override | ||
public Object getParameter(String key) { | ||
public Object getParameter(final String key) { | ||
return parameterMap.get(key); | ||
} | ||
|
||
|
||
@Override | ||
public String getProgram(String... statements) { | ||
return null; | ||
public String getProgram(final String... statements) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String stmt : statements) { | ||
sb.append(stmt).append(";"); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package com.googlecode.aviator.script; | ||
|
||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
@@ -13,8 +12,10 @@ | |
|
||
|
||
/** | ||
* | ||
* A compiled aviator script. | ||
* | ||
* @author [email protected] | ||
* @author dennis([email protected]) | ||
* @date 2011-1-18 上午11:03:34 | ||
* @version | ||
*/ | ||
|
@@ -24,27 +25,26 @@ public class CompiledAviatorScript extends CompiledScript { | |
private final Expression expression; | ||
|
||
|
||
CompiledAviatorScript(AviatorScriptEngine engine, Expression expression) { | ||
CompiledAviatorScript(final AviatorScriptEngine engine, final Expression expression) { | ||
this.engine = engine; | ||
this.expression = expression; | ||
} | ||
|
||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Object eval(ScriptContext context) throws ScriptException { | ||
public Object eval(final ScriptContext context) throws ScriptException { | ||
try { | ||
Map<String, Object> map = new HashMap<String, Object>(); | ||
for (Iterator it = context.getScopes().iterator(); it.hasNext();) { | ||
int scope = ((Integer) it.next()).intValue(); | ||
Map<String, Object> env = this.expression.newEnv(); | ||
for (Iterator<Integer> it = context.getScopes().iterator(); it.hasNext();) { | ||
int scope = it.next().intValue(); | ||
Bindings bindings = context.getBindings(scope); | ||
Set keys = bindings.keySet(); | ||
Set<String> keys = bindings.keySet(); | ||
|
||
for (Object key : keys) { | ||
map.put((String) key, bindings.get(key)); | ||
for (String key : keys) { | ||
env.put(key, bindings.get(key)); | ||
} | ||
} | ||
return this.expression.execute(map); | ||
return this.expression.execute(env); | ||
} catch (Exception e) { | ||
throw new ScriptException(e); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.googlecode.aviator.utils; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
|
||
/** | ||
* Some helper methods. | ||
* | ||
* @author dennis([email protected]) | ||
* | ||
*/ | ||
public class Utils { | ||
private Utils() { | ||
|
||
} | ||
|
||
public static String readFully(final Reader reader) throws IOException { | ||
final char[] arr = new char[16 * 1024]; | ||
final StringBuilder buf = new StringBuilder(); | ||
int numChars; | ||
|
||
while ((numChars = reader.read(arr, 0, arr.length)) > 0) { | ||
buf.append(arr, 0, numChars); | ||
} | ||
|
||
return buf.toString(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/javax.script.ScriptEngineFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.googlecode.aviator.script.AviatorScriptEngineFactory |
20 changes: 20 additions & 0 deletions
20
src/test/java/com/googlecode/aviator/ScriptEngineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.googlecode.aviator; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
import org.junit.Test; | ||
|
||
public class ScriptEngineTest { | ||
|
||
@Test | ||
public void testScriptEngine() throws Exception { | ||
final ScriptEngineManager m = new ScriptEngineManager(); | ||
final ScriptEngine engine = m.getEngineByName("aviator"); | ||
assertNotNull(engine); | ||
|
||
assertEquals(3, engine.eval("1+2")); | ||
assertEquals(6, engine.eval("square = lambda(x) -> x*2 end; square(3)")); | ||
} | ||
} |