Skip to content

Commit 8f168b0

Browse files
committed
(feat) Impl variable pool
1 parent 4214cce commit 8f168b0

File tree

11 files changed

+93
-22
lines changed

11 files changed

+93
-22
lines changed

src/main/java/com/googlecode/aviator/AviatorEvaluator.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,27 @@ public final class AviatorEvaluator {
7979
public static int BYTECODE_VER = getInstance().getBytecodeVersion();
8080

8181
/**
82-
* Create a aviator evaluator instance.
82+
* Create a aviator script engine instance with eval mode
83+
*
84+
* @since 5.3
85+
* @return the script engine
86+
*/
87+
public static AviatorEvaluatorInstance newInstance(final EvalMode evalMode) {
88+
return new AviatorEvaluatorInstance(evalMode);
89+
}
90+
91+
/**
92+
* Create a aviator script engine instance.
8393
*
84-
* @return
94+
* @return the script engine
8595
*/
8696
public static AviatorEvaluatorInstance newInstance() {
87-
return new AviatorEvaluatorInstance();
97+
return newInstance(Options.EVAL_MODE.getDefaultValueObject().evalMode);
8898
}
8999

90100
private static class StaticHolder {
91-
private static AviatorEvaluatorInstance INSTANCE = new AviatorEvaluatorInstance();
101+
private static AviatorEvaluatorInstance INSTANCE =
102+
new AviatorEvaluatorInstance(Options.EVAL_MODE.getDefaultValueObject().evalMode);
92103
}
93104

94105
/**

src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,9 @@ private void loadInternalLibs() {
10681068
/**
10691069
* Create a aviator evaluator instance.
10701070
*/
1071-
AviatorEvaluatorInstance() {
1071+
AviatorEvaluatorInstance(final EvalMode evalMode) {
10721072
fillDefaultOpts();
1073+
setOption(Options.EVAL_MODE, evalMode);
10731074
loadFeatureFunctions();
10741075
loadLib();
10751076
loadModule();

src/main/java/com/googlecode/aviator/InterpretExpression.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.googlecode.aviator;
22

3+
import java.util.IdentityHashMap;
34
import java.util.List;
45
import java.util.Map;
56
import com.googlecode.aviator.code.interpreter.IR;
@@ -8,6 +9,7 @@
89
import com.googlecode.aviator.lexer.SymbolTable;
910
import com.googlecode.aviator.parser.VariableMeta;
1011
import com.googlecode.aviator.runtime.RuntimeUtils;
12+
import com.googlecode.aviator.runtime.type.AviatorJavaType;
1113
import com.googlecode.aviator.runtime.type.AviatorObject;
1214
import com.googlecode.aviator.utils.Env;
1315

@@ -17,11 +19,22 @@ public class InterpretExpression extends BaseExpression {
1719

1820
private final boolean unboxObject;
1921

22+
private final Map<VariableMeta, AviatorJavaType> variables =
23+
new IdentityHashMap<VariableMeta, AviatorJavaType>();
24+
25+
2026
public InterpretExpression(final AviatorEvaluatorInstance instance, final List<VariableMeta> vars,
2127
final SymbolTable symbolTable, final List<IR> instruments, final boolean unboxObject) {
2228
super(instance, vars, symbolTable);
2329
this.instruments = instruments;
2430
this.unboxObject = unboxObject;
31+
for (VariableMeta v : vars) {
32+
this.variables.put(v, new AviatorJavaType(v.getName(), this.symbolTable));
33+
}
34+
}
35+
36+
public AviatorJavaType loadVar(final VariableMeta v) {
37+
return this.variables.get(v);
2538
}
2639

2740
@Override
@@ -37,6 +50,7 @@ public Object executeDirectly(final Map<String, Object> env) {
3750
RuntimeUtils.printlnTrace(env, "Execute instruments: ");
3851
}
3952

53+
4054
InterpretContext ctx = new InterpretContext(this, this.instruments, (Env) env);
4155
IR ir = null;
4256
while ((ir = ctx.getPc()) != null) {

src/main/java/com/googlecode/aviator/Options.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.math.MathContext;
44
import java.util.Set;
5+
import com.googlecode.aviator.utils.Utils;
56

67

78
/**
@@ -309,7 +310,6 @@ public Object getDefaultValue() {
309310
return intoObject(getDefaultValueObject());
310311
}
311312

312-
313313
/**
314314
* Returns the default value object of option.
315315
*
@@ -344,7 +344,7 @@ public Value getDefaultValueObject() {
344344
case ASSIGNABLE_ALLOWED_CLASS_SET:
345345
return NULL_CLASS_SET;
346346
case EVAL_MODE:
347-
return ASM_MODE;
347+
return Utils.isAndroid() ? INTERPRETER_MODE : ASM_MODE;
348348
}
349349
return null;
350350
}

src/main/java/com/googlecode/aviator/code/InterpretCodeGenerator.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.googlecode.aviator.code.interpreter.ir.SourceInfo;
3434
import com.googlecode.aviator.code.interpreter.ir.VisitLabelIR;
3535
import com.googlecode.aviator.exception.CompileExpressionErrorException;
36+
import com.googlecode.aviator.lexer.SymbolTable;
3637
import com.googlecode.aviator.lexer.token.Token;
3738
import com.googlecode.aviator.lexer.token.Token.TokenType;
3839
import com.googlecode.aviator.parser.AviatorClassLoader;
@@ -54,6 +55,10 @@ public class InterpretCodeGenerator implements EvalCodeGenerator {
5455
private final List<IR> instruments = new ArrayList<>();
5556
private final AviatorEvaluatorInstance instance;
5657

58+
private Map<String, VariableMeta/* metadata */> variables = Collections.emptyMap();
59+
60+
private final Map<Token<?>/* constant token */, String/* field name */> constantPool =
61+
Collections.emptyMap();
5762

5863
private final String sourceFile;
5964

@@ -63,6 +68,8 @@ public class InterpretCodeGenerator implements EvalCodeGenerator {
6368

6469
private Parser parser;
6570

71+
private SymbolTable symbolTable;
72+
6673
/**
6774
* parent code generator when compiling lambda.
6875
*/
@@ -139,19 +146,19 @@ private Label makeLabel() {
139146

140147
@Override
141148
public void start() {
142-
// TODO Auto-generated method stub
143-
149+
// not implemented yet
144150
}
145151

146152
@Override
147153
public void initVariables(final Map<String, VariableMeta> vars) {
148-
// TODO Auto-generated method stub
149-
154+
this.variables = vars;
150155
}
151156

152157
@Override
153158
public void initConstants(final Set<Token<?>> constants) {
154-
// TODO Auto-generated method stub
159+
if (constants.isEmpty()) {
160+
return;
161+
}
155162

156163
}
157164

@@ -196,6 +203,7 @@ public void onAssignment(final Token<?> lookhead) {
196203
@Override
197204
public void setParser(final Parser parser) {
198205
this.parser = parser;
206+
this.symbolTable = this.parser.getSymbolTable();
199207
}
200208

201209
@Override
@@ -392,8 +400,9 @@ public Expression getResult(final boolean unboxObject) {
392400
optimize(instruments);
393401
resolveLabels(instruments);
394402

395-
final InterpretExpression exp = new InterpretExpression(this.instance,
396-
Collections.<VariableMeta>emptyList(), null, instruments, unboxObject);
403+
final InterpretExpression exp =
404+
new InterpretExpression(this.instance, new ArrayList<VariableMeta>(this.variables.values()),
405+
this.symbolTable, instruments, unboxObject);
397406
exp.setLambdaBootstraps(this.lambdaBootstraps);
398407
exp.setSourceFile(this.sourceFile);
399408
exp.setFuncsArgs(this.funcsArgs);
@@ -440,7 +449,13 @@ private void resolveLabels(final List<IR> instruments) {
440449
@Override
441450
public void onConstant(final Token<?> lookhead) {
442451
if (LOAD_CONSTANTS_TYPE.contains(lookhead.getType())) {
443-
emit(new LoadIR(this.sourceFile, lookhead));
452+
VariableMeta meta = null;
453+
454+
if (lookhead.getType() == TokenType.Variable) {
455+
meta = this.variables.get(lookhead.getLexeme());
456+
}
457+
458+
emit(new LoadIR(this.sourceFile, lookhead, meta));
444459
}
445460
}
446461

src/main/java/com/googlecode/aviator/code/interpreter/InterpretContext.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Collections;
55
import java.util.List;
66
import com.googlecode.aviator.InterpretExpression;
7+
import com.googlecode.aviator.parser.VariableMeta;
8+
import com.googlecode.aviator.runtime.type.AviatorJavaType;
79
import com.googlecode.aviator.runtime.type.AviatorObject;
810
import com.googlecode.aviator.utils.Env;
911

@@ -30,6 +32,10 @@ public InterpretContext(final InterpretExpression exp, final List<IR> instrument
3032
next();
3133
}
3234

35+
public AviatorJavaType loadVar(final VariableMeta v) {
36+
return this.expression.loadVar(v);
37+
}
38+
3339
public boolean isReachEnd() {
3440
return this.reachEnd;
3541
}

src/main/java/com/googlecode/aviator/code/interpreter/ir/LoadIR.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.googlecode.aviator.lexer.token.NumberToken;
88
import com.googlecode.aviator.lexer.token.Token;
99
import com.googlecode.aviator.lexer.token.Variable;
10+
import com.googlecode.aviator.parser.VariableMeta;
1011
import com.googlecode.aviator.runtime.type.AviatorBigInt;
1112
import com.googlecode.aviator.runtime.type.AviatorBoolean;
1213
import com.googlecode.aviator.runtime.type.AviatorDecimal;
@@ -27,12 +28,14 @@
2728
*/
2829
public class LoadIR implements IR {
2930
private final Token<?> token;
31+
private final VariableMeta meta;
3032
private final String sourceFile;
3133

32-
public LoadIR(final String sourceFile, final Token<?> token) {
34+
public LoadIR(final String sourceFile, final Token<?> token, final VariableMeta meta) {
3335
super();
3436
this.token = token;
3537
this.sourceFile = sourceFile;
38+
this.meta = meta;
3639
}
3740

3841
@Override
@@ -73,7 +76,14 @@ public void eval(final InterpretContext context) {
7376
} else if (this.token == Variable.NIL) {
7477
context.push(AviatorNil.NIL);
7578
} else {
76-
context.push(new AviatorJavaType(this.token.getLexeme()));
79+
AviatorJavaType var;
80+
if (this.meta != null) {
81+
var = context.loadVar(this.meta);
82+
assert (var != null);
83+
} else {
84+
var = new AviatorJavaType(this.token.getLexeme());
85+
}
86+
context.push(var);
7787
}
7888
break;
7989
default:

src/main/java/com/googlecode/aviator/lexer/token/OperatorType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.googlecode.aviator.lexer.token;
1717

1818
import java.util.Map;
19+
import com.googlecode.aviator.exception.ExpressionRuntimeException;
1920
import com.googlecode.aviator.exception.IllegalArityException;
2021
import com.googlecode.aviator.runtime.type.AviatorBoolean;
2122
import com.googlecode.aviator.runtime.type.AviatorJavaType;
@@ -117,7 +118,8 @@ public AviatorObject eval(final AviatorObject[] args, final Map<String, Object>
117118
return args[1];
118119
case ASSIGNMENT:
119120
if (!(args[0] instanceof AviatorJavaType)) {
120-
throw new IllegalArgumentException(args[0].desc(env) + " can't be as a left value.");
121+
throw new ExpressionRuntimeException(
122+
args[0].desc(env) + " can't be a left value for assignment.");
121123
}
122124
args[0].setValue(args[1], env);
123125
return args[1];

src/main/java/com/googlecode/aviator/utils/Utils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,13 @@ public static String getAviatorScriptVersion() {
109109
}
110110
return CURRENT_VERSION;
111111
}
112+
113+
public static boolean isAndroid() {
114+
try {
115+
Class.forName("android.os.Build");
116+
return true;
117+
} catch (ClassNotFoundException e) {
118+
return false;
119+
}
120+
}
112121
}

src/test/java/com/googlecode/aviator/example/SimpleExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class SimpleExample {
99
public static void main(final String[] args) throws Exception {
1010
AviatorEvaluator.setOption(Options.EVAL_MODE, EvalMode.INTERPRETER);
1111
AviatorEvaluator.setOption(Options.TRACE_EVAL, true);
12-
Object result = AviatorEvaluator.execute("true || p(1)");
12+
Object result = AviatorEvaluator.execute("a=1; '#{a+100}'");
1313
System.out.println(result);
1414
System.out.println(1 == 1 && 2 != 2 ? 1 : 2 == 3 ? 4 : 5);
1515

0 commit comments

Comments
 (0)