Skip to content

Commit

Permalink
(fix) Stackoverflow when calling AviatorFunction#toString, #184
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed Dec 23, 2019
1 parent 98769b6 commit 104505b
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@
*
*/
public abstract class AbstractFunction extends AviatorObject implements AviatorFunction {
public AviatorObject throwArity(int n) {
String name = this.getName();
public AviatorObject throwArity(final int n) {
String name = getName();
throw new IllegalArgumentException("Wrong number of args (" + n + ") passed to: " + name);
}

@Override
public String desc(final Map<String, Object> env) {
return "<" + getAviatorType() + ", " + getName() + ">";

}

@Override
public AviatorObject call(Map<String, Object> env) {
return this.throwArity(0);
public AviatorObject call(final Map<String, Object> env) {
return throwArity(0);
}


@Override
public int compare(AviatorObject other, Map<String, Object> env) {
public int compare(final AviatorObject other, final Map<String, Object> env) {
throw new UnsupportedOperationException("Lambda function can't be compared.");
}

Expand All @@ -38,188 +43,210 @@ public AviatorType getAviatorType() {
}

@Override
public Object getValue(Map<String, Object> env) {
public Object getValue(final Map<String, Object> env) {
return this;
}

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
return this.throwArity(1);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
return throwArity(1);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
return this.throwArity(2);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2) {
return throwArity(2);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3) {
return this.throwArity(3);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3) {
return throwArity(3);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4) {
return this.throwArity(4);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4) {
return throwArity(4);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5) {
return this.throwArity(5);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5) {
return throwArity(5);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6) {
return this.throwArity(6);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6) {
return throwArity(6);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7) {
return this.throwArity(7);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7) {
return throwArity(7);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8) {
return this.throwArity(8);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8) {
return throwArity(8);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9) {
return this.throwArity(9);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9) {
return throwArity(9);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10) {
return this.throwArity(10);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10) {
return throwArity(10);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11) {
return this.throwArity(11);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11) {
return throwArity(11);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11, AviatorObject arg12) {
return this.throwArity(12);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11, final AviatorObject arg12) {
return throwArity(12);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11, AviatorObject arg12, AviatorObject arg13) {
return this.throwArity(13);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13) {
return throwArity(13);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14) {
return this.throwArity(14);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13,
final AviatorObject arg14) {
return throwArity(14);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14,
AviatorObject arg15) {
return this.throwArity(15);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13,
final AviatorObject arg14, final AviatorObject arg15) {
return throwArity(15);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14,
AviatorObject arg15, AviatorObject arg16) {
return this.throwArity(16);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13,
final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16) {
return throwArity(16);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14,
AviatorObject arg15, AviatorObject arg16, AviatorObject arg17) {
return this.throwArity(17);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13,
final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16,
final AviatorObject arg17) {
return throwArity(17);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14,
AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18) {
return this.throwArity(18);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13,
final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16,
final AviatorObject arg17, final AviatorObject arg18) {
return throwArity(18);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14,
AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18,
AviatorObject arg19) {
return this.throwArity(19);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13,
final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16,
final AviatorObject arg17, final AviatorObject arg18, final AviatorObject arg19) {
return throwArity(19);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14,
AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18,
AviatorObject arg19, AviatorObject arg20) {
return this.throwArity(20);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13,
final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16,
final AviatorObject arg17, final AviatorObject arg18, final AviatorObject arg19,
final AviatorObject arg20) {
return throwArity(20);
}


@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2,
AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6,
AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10,
AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14,
AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18,
AviatorObject arg19, AviatorObject arg20, AviatorObject... args) {
return this.throwArity(21);
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4,
final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7,
final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10,
final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13,
final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16,
final AviatorObject arg17, final AviatorObject arg18, final AviatorObject arg19,
final AviatorObject arg20, final AviatorObject... args) {
return throwArity(21);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ public AviatorObject not(final Map<String, Object> env) {
}

public String desc(final Map<String, Object> env) {
return "<" + getAviatorType() + ", " + getValue(env) + ">";
Object val = getValue(env);
if (val != this) {
return "<" + getAviatorType() + ", " + getValue(env) + ">";
} else {
return "<" + getAviatorType() + ", this>";
}
}

public abstract Object getValue(Map<String, Object> env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public void testIssue77() {
AviatorEvaluator.setOption(Options.ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL, false);
}

@Test
public void testFunctionToString() {
AviatorEvaluatorInstance instance = AviatorEvaluator.newInstance();
String expression = "(lambda (x) -> lambda(y) -> lambda(z) -> x + y + z end end end)(appName)";
HashMap<String, Object> env = new HashMap<>();
String s = instance.execute(expression, env, true).toString();
assertTrue(s.startsWith("<Lambda,"));
}

@Test(expected = ExpressionRuntimeException.class)
public void testIssue181() {
Map<String, Object> env = new HashMap<>();
Expand Down

0 comments on commit 104505b

Please sign in to comment.