Skip to content

Commit 1f0278c

Browse files
committed
(fix) some corner cases
1 parent e1752cb commit 1f0278c

File tree

9 files changed

+63
-35
lines changed

9 files changed

+63
-35
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ public class InterpretExpression extends BaseExpression {
1515

1616
private final List<IR> instruments;
1717

18+
private final boolean unboxObject;
19+
1820
public InterpretExpression(final AviatorEvaluatorInstance instance, final List<VariableMeta> vars,
19-
final SymbolTable symbolTable, final List<IR> instruments) {
21+
final SymbolTable symbolTable, final List<IR> instruments, final boolean unboxObject) {
2022
super(instance, vars, symbolTable);
2123
this.instruments = instruments;
24+
this.unboxObject = unboxObject;
2225
}
2326

2427
@Override
@@ -50,10 +53,17 @@ public Object executeDirectly(final Map<String, Object> env) {
5053
break;
5154
}
5255
}
53-
final AviatorObject top = ctx.peek();
54-
if (top == null) {
56+
57+
assert (ctx.getOperands().size() <= 1);
58+
AviatorObject result = ctx.peek();
59+
if (result == null) {
5560
return null;
5661
}
57-
return top.getValue(env);
62+
63+
if (this.unboxObject) {
64+
return result.getValue(env);
65+
} else {
66+
return result.deref(env);
67+
}
5868
}
5969
}

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ public InterpretCodeGenerator(final AviatorEvaluatorInstance instance, final Str
142142

143143
@Override
144144
public void onAssignment(final Token<?> lookhead) {
145-
this.instruments.add(OperatorIR.ASSIGN);
145+
if (lookhead.getMeta(Constants.DEFINE_META, false)) {
146+
this.instruments.add(OperatorIR.DEF);
147+
} else {
148+
this.instruments.add(OperatorIR.ASSIGN);
149+
}
146150
}
147151

148152
@Override
@@ -331,7 +335,7 @@ public Expression getResult(final boolean unboxObject) {
331335
resolveLabels(instruments);
332336

333337
final InterpretExpression exp = new InterpretExpression(this.instance,
334-
Collections.<VariableMeta>emptyList(), null, instruments);
338+
Collections.<VariableMeta>emptyList(), null, instruments, unboxObject);
335339
exp.setLambdaBootstraps(this.lambdaBootstraps);
336340
exp.setSourceFile(this.sourceFile);
337341
exp.setFuncsArgs(this.funcsArgs);
@@ -378,15 +382,14 @@ private void resolveLabels(final List<IR> instruments) {
378382
@Override
379383
public void onConstant(final Token<?> lookhead) {
380384
if (LOAD_CONSTANTS_TYPE.contains(lookhead.getType())) {
381-
this.instruments.add(new LoadIR(lookhead));
385+
this.instruments.add(new LoadIR(this.sourceFile, lookhead));
382386
}
383387
}
384388

385389
@Override
386390
public void onMethodName(final Token<?> lookhead) {
387-
final MethodMetaData metadata =
388-
new MethodMetaData(lookhead.getType() == TokenType.Delegate ? null : lookhead.getLexeme());
389-
metadata.unpackArgs = lookhead.getMeta(Constants.UNPACK_ARGS, false);
391+
final MethodMetaData metadata = new MethodMetaData(lookhead,
392+
lookhead.getType() == TokenType.Delegate ? null : lookhead.getLexeme());
390393
this.methodMetaDataStack.push(metadata);
391394
}
392395

@@ -412,7 +415,8 @@ public void onMethodInvoke(final Token<?> lookhead) {
412415
}
413416

414417
this.instruments.add(new SendIR(methodMetaData.methodName, methodMetaData.parameterCount,
415-
methodMetaData.unpackArgs, methodMetaData.funcId));
418+
methodMetaData.token.getMeta(Constants.UNPACK_ARGS, false), methodMetaData.funcId,
419+
this.sourceFile, methodMetaData.token.getLineNo()));
416420
}
417421

418422
@Override

src/main/java/com/googlecode/aviator/code/asm/ASMCodeGenerator.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,15 +1213,17 @@ public static class MethodMetaData {
12131213

12141214
public int variadicListIndex = -1;
12151215

1216-
public String methodName;
1216+
public final Token<?> token;
12171217

1218-
public boolean unpackArgs;
1218+
public final String methodName;
12191219

12201220
public int funcId = -1;
12211221

12221222

1223-
public MethodMetaData(final String methodName) {
1223+
1224+
public MethodMetaData(final Token<?> token, final String methodName) {
12241225
super();
1226+
this.token = token;
12251227
this.methodName = methodName;
12261228
}
12271229
}
@@ -1340,14 +1342,14 @@ public void onMethodName(final Token<?> lookhead) {
13401342
"wrapTrace",
13411343
"(Lcom/googlecode/aviator/runtime/type/AviatorFunction;)Lcom/googlecode/aviator/runtime/type/AviatorFunction;");
13421344
}
1343-
1345+
// FIXME it will not work in compile mode.
13441346
if (lookhead.getMeta(Constants.UNPACK_ARGS, false)) {
13451347
this.mv.visitMethodInsn(INVOKESTATIC, RUNTIME_UTILS, "unpackArgsFunction",
13461348
"(Lcom/googlecode/aviator/runtime/type/AviatorFunction;)Lcom/googlecode/aviator/runtime/type/AviatorFunction;");
13471349
}
13481350

13491351
loadEnv();
1350-
this.methodMetaDataStack.push(new MethodMetaData(outtterMethodName));
1352+
this.methodMetaDataStack.push(new MethodMetaData(lookhead, outtterMethodName));
13511353
}
13521354

13531355

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
*/
2828
public class LoadIR implements IR {
2929
private final Token<?> token;
30+
private final String sourceFile;
3031

31-
public LoadIR(final Token<?> token) {
32+
public LoadIR(final String sourceFile, final Token<?> token) {
3233
super();
3334
this.token = token;
35+
this.sourceFile = sourceFile;
3436
}
3537

3638
@Override
@@ -81,6 +83,7 @@ public void eval(final InterpretContext context) {
8183

8284
@Override
8385
public String toString() {
84-
return "load " + this.token.getLexeme() + " [" + this.token.getType() + "]";
86+
return "load " + this.token.getLexeme() + " [" + this.token.getType() + "] ("
87+
+ this.sourceFile + ":" + this.token.getLineNo() + ")";
8588
}
8689
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ public class SendIR implements IR {
1515
private final int arity;
1616
private final boolean unpackArgs;
1717
private int funcId = -1;
18+
private final String sourceFile;
19+
private final int lineNo;
1820

1921

20-
public SendIR(final String name, final int arity, final boolean unpackArgs, final int funcId) {
22+
public SendIR(final String name, final int arity, final boolean unpackArgs, final int funcId,
23+
final String sourceFile, final int lineNo) {
2124
super();
2225
this.name = name;
2326
this.arity = arity;
2427
this.unpackArgs = unpackArgs;
2528
this.funcId = funcId;
29+
this.sourceFile = sourceFile;
30+
this.lineNo = lineNo;
2631
}
2732

2833
private AviatorObject callFn(final AviatorFunction fn, final AviatorObject[] args,
@@ -98,11 +103,8 @@ private AviatorObject callFn(final AviatorFunction fn, final AviatorObject[] arg
98103

99104
@Override
100105
public void eval(final InterpretContext context) {
101-
AviatorFunction fn;
102-
if (this.name == null) {
103-
// anonymous function, pop from stack
104-
fn = (AviatorFunction) context.pop();
105-
} else {
106+
AviatorFunction fn = null;
107+
if (this.name != null) {
106108
fn = RuntimeUtils.getFunction(context.getEnv(), this.name);
107109
}
108110

@@ -113,6 +115,10 @@ public void eval(final InterpretContext context) {
113115
args[i] = context.pop();
114116
}
115117

118+
if (this.name == null) {
119+
fn = (AviatorFunction) context.pop();
120+
}
121+
116122

117123
if (RuntimeUtils.isTracedEval(context.getEnv())) {
118124
fn = TraceFunction.wrapTrace(fn);
@@ -126,13 +132,14 @@ public void eval(final InterpretContext context) {
126132
// put function arguments ref id to env.
127133
context.getEnv().put(ASMCodeGenerator.FUNC_ARGS_INNER_VAR, this.funcId);
128134
}
129-
135+
System.out.println(this);
130136
context.push(callFn(fn, args, this.arity, context.getEnv()));
131137
}
132138

133139
@Override
134140
public String toString() {
135-
return "send " + this.name + ", " + this.arity;
141+
return "send " + (this.name == null ? "<top>" : this.name) + ", " + this.arity + ", "
142+
+ this.unpackArgs + " (" + this.sourceFile + ":" + this.lineNo + ")";
136143
}
137144

138145
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public DelegateToken(final Token<?> token, final DelegateTokenType type) {
5252
token != null ? token.getStartIndex() : -1);
5353
this.token = token;
5454
this.delegateTokenType = type;
55+
if (token != null) {
56+
setMetaMap(token.getMetaMap());
57+
}
5558
}
5659

5760

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.googlecode.aviator.runtime.type.AviatorBoolean;
2121
import com.googlecode.aviator.runtime.type.AviatorJavaType;
2222
import com.googlecode.aviator.runtime.type.AviatorObject;
23-
import com.googlecode.aviator.utils.Env;
2423

2524

2625
/**
@@ -114,13 +113,13 @@ public AviatorObject eval(final AviatorObject[] args, final Map<String, Object>
114113
if (!(args[0] instanceof AviatorJavaType)) {
115114
throw new IllegalArgumentException(args[0].desc(env) + " can't be as a left value.");
116115
}
117-
((Env) env).override(((AviatorJavaType) args[0]).getName(), args[1].getValue(env));
116+
args[0].defineValue(args[1], env);
118117
return args[1];
119118
case ASSIGNMENT:
120119
if (!(args[0] instanceof AviatorJavaType)) {
121120
throw new IllegalArgumentException(args[0].desc(env) + " can't be as a left value.");
122121
}
123-
env.put(((AviatorJavaType) args[0]).getName(), args[1].getValue(env));
122+
args[0].setValue(args[1], env);
124123
return args[1];
125124
case DIV:
126125
return args[0].div(args[1], env);

src/main/java/com/googlecode/aviator/runtime/function/internal/ReducerFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.googlecode.aviator.runtime.function.AbstractFunction;
88
import com.googlecode.aviator.runtime.type.AviatorFunction;
99
import com.googlecode.aviator.runtime.type.AviatorLong;
10+
import com.googlecode.aviator.runtime.type.AviatorNil;
1011
import com.googlecode.aviator.runtime.type.AviatorObject;
1112
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;
1213
import com.googlecode.aviator.runtime.type.Range;
@@ -41,7 +42,7 @@ public final AviatorObject call(final Map<String, Object> env, final AviatorObje
4142
AviatorFunction iteratorFn = (AviatorFunction) arg2;
4243

4344
int maxLoopCount = RuntimeUtils.getInstance(env).getOptionValue(Options.MAX_LOOP_COUNT).number;
44-
AviatorObject result = null;
45+
AviatorObject result = AviatorNil.NIL;
4546
long c = 0;
4647

4748
if (coll != Range.LOOP) {

src/test/java/com/googlecode/aviator/test/function/GrammarUnitTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.HashSet;
2929
import java.util.List;
3030
import java.util.Map;
31-
import org.junit.Before;
3231
import org.junit.Test;
3332
import com.googlecode.aviator.AviatorEvaluator;
3433
import com.googlecode.aviator.AviatorEvaluatorInstance;
@@ -54,10 +53,10 @@
5453
*/
5554
public class GrammarUnitTest {
5655

57-
@Before
58-
public void setup() {
59-
AviatorEvaluator.setOption(Options.OPTIMIZE_LEVEL, AviatorEvaluator.INTERPRET);
60-
}
56+
// @Before
57+
// public void setup() {
58+
// AviatorEvaluator.setOption(Options.OPTIMIZE_LEVEL, AviatorEvaluator.INTERPRET);
59+
// }
6160

6261
@Test
6362
public void testMultilineExpressions() {

0 commit comments

Comments
 (0)