Fix variable argument functions in expressions

Fixes #1531
This commit is contained in:
Octavia Togami 2020-09-19 18:57:53 -07:00
parent 3094e7a6fc
commit 204ef7a708
5 changed files with 30 additions and 3 deletions

View File

@ -527,7 +527,11 @@ public void calc(Actor actor,
try {
expression = Expression.compile(String.join(" ", input));
} catch (ExpressionException e) {
actor.printError(TranslatableComponent.of("worldedit.calc.invalid", TextComponent.of(String.join(" ", input))));
actor.printError(TranslatableComponent.of(
"worldedit.calc.invalid.with-error",
TextComponent.of(String.join(" ", input)),
TextComponent.of(e.getMessage())
));
return;
}
WorldEditAsyncCommandBuilder.createAndSendMessage(actor, () -> {

View File

@ -65,9 +65,21 @@ public static MethodHandle resolveFunction(Functions functions,
Set<MethodHandle> matchingFns = functions.getMap().get(fnName);
check(!matchingFns.isEmpty(), ctx, "Unknown function '" + fnName + "'");
for (MethodHandle function : matchingFns) {
if (function.isVarargsCollector()) {
int nParams = function.type().parameterCount();
// last param is the array, turn that varargs
int keptParams = nParams - 1;
function = function.asCollector(
// collect into the last array
function.type().parameterType(nParams - 1),
// collect the variable args (args over kept)
ctx.args.size() - keptParams
);
// re-wrap it for the inner arguments
function = function.asType(function.type().wrap());
}
MethodType type = function.type();
// Validate argc if not varargs
if (!function.isVarargsCollector() && type.parameterCount() != ctx.args.size()) {
if (type.parameterCount() != ctx.args.size()) {
// skip non-matching function
continue;
}

View File

@ -63,6 +63,7 @@ static Functions create() {
}
private static MethodHandle clean(MethodHandle handle) {
boolean wasVarargs = handle.isVarargsCollector();
// box it all first
handle = handle.asType(handle.type().wrap());
if (handle.type().returnType() != Double.class) {
@ -72,6 +73,12 @@ private static MethodHandle clean(MethodHandle handle) {
handle = handle.asType(handle.type().changeReturnType(Number.class));
handle = filterReturnValue(handle, DOUBLE_VALUE);
}
// return vararg-ity
if (wasVarargs) {
handle = handle.asVarargsCollector(
handle.type().parameterType(handle.type().parameterCount() - 1)
);
}
return handle;
}

View File

@ -174,6 +174,7 @@
"worldedit.remove.removed": "{0} entities have been marked for removal.",
"worldedit.remove.explain-all": "Use -1 to remove all entities in loaded chunks",
"worldedit.calc.invalid": "'{0}' could not be parsed as a valid expression",
"worldedit.calc.invalid.with-error": "'{0}' could not be parsed as a valid expression: '{1}'",
"worldedit.paste.pasted": "The clipboard has been pasted at {0}",
"worldedit.paste.selected": "Selected clipboard paste region.",

View File

@ -51,6 +51,9 @@ public Stream<DynamicNode> testEvaluate() throws ExpressionException {
// check functions
testCase("sin(5)", sin(5)),
testCase("atan2(3, 4)", atan2(3, 4)),
testCase("min(1, 2)", 1),
testCase("max(1, 2)", 2),
testCase("max(1, 2, 3, 4, 5)", 5),
// check conditionals
testCase("0 || 5", 5),
testCase("2 || 5", 2),