Small patches for timed-calc post-1.12-merge

Dieser Commit ist enthalten in:
Kenzie Togami 2018-10-01 16:04:48 -07:00
Ursprung 776eb24c0e
Commit e16dacc11e
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 5D200B325E157A81
4 geänderte Dateien mit 30 neuen und 108 gelöschten Zeilen

Datei anzeigen

@ -181,20 +181,20 @@ public class Expression {
} }
private void pushInstance() { private void pushInstance() {
Stack<Expression> foo = instance.get(); Stack<Expression> threadLocalExprStack = instance.get();
if (foo == null) { if (threadLocalExprStack == null) {
instance.set(foo = new Stack<>()); instance.set(threadLocalExprStack = new Stack<>());
} }
foo.push(this); threadLocalExprStack.push(this);
} }
private void popInstance() { private void popInstance() {
Stack<Expression> foo = instance.get(); Stack<Expression> threadLocalExprStack = instance.get();
foo.pop(); threadLocalExprStack.pop();
if (foo.isEmpty()) { if (threadLocalExprStack.isEmpty()) {
instance.set(null); instance.set(null);
} }
} }

Datei anzeigen

@ -1,96 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.internal.expression;
import com.google.common.collect.ImmutableMap;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.AbstractPlatform;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Preference;
import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.world.World;
import java.util.Map;
final class ExpressionPlatform extends AbstractPlatform {
@Override
public int resolveItem(String name) {
return 0;
}
@Override
public boolean isValidMobType(String type) {
return false;
}
@Override
public void reload() {
}
@Override
public Player matchPlayer(Player player) {
return null;
}
@Override
public World matchWorld(World world) {
return null;
}
@Override
public void registerCommands(Dispatcher dispatcher) {
}
@Override
public void registerGameHooks() {
}
@Override
public LocalConfiguration getConfiguration() {
return new LocalConfiguration() {
@Override
public void load() {
}
};
}
@Override
public String getVersion() {
return "INVALID";
}
@Override
public String getPlatformName() {
return "Expression Test";
}
@Override
public String getPlatformVersion() {
return "INVALID";
}
@Override
public Map<Capability, Preference> getCapabilities() {
return ImmutableMap.of(Capability.CONFIGURATION, Preference.PREFER_OTHERS);
}
}

Datei anzeigen

@ -22,13 +22,16 @@ package com.sk89q.worldedit.internal.expression;
import static java.lang.Math.atan2; import static java.lang.Math.atan2;
import static java.lang.Math.sin; import static java.lang.Math.sin;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.event.platform.PlatformReadyEvent; import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.internal.expression.lexer.LexerException; import com.sk89q.worldedit.internal.expression.lexer.LexerException;
import com.sk89q.worldedit.internal.expression.parser.ParserException; import com.sk89q.worldedit.internal.expression.parser.ParserException;
import com.sk89q.worldedit.internal.expression.runtime.EvaluationException; import com.sk89q.worldedit.internal.expression.runtime.EvaluationException;
@ -37,8 +40,13 @@ import com.sk89q.worldedit.internal.expression.runtime.ExpressionEnvironment;
public class ExpressionTest { public class ExpressionTest {
@Before @Before
public void setup() { public void setup() {
WorldEdit.getInstance().getPlatformManager().register(new ExpressionPlatform()); Platform mockPlat = Mockito.mock(Platform.class);
WorldEdit.getInstance().getPlatformManager().handlePlatformReady(new PlatformReadyEvent()); Mockito.when(mockPlat.getConfiguration()).thenReturn(new LocalConfiguration() {
@Override
public void load() {
}
});
WorldEdit.getInstance().getPlatformManager().register(mockPlat);
} }
@Test @Test
@ -172,6 +180,16 @@ public class ExpressionTest {
assertEquals(1, simpleEval("!queryRel(3,4,5,100,200)"), 0); assertEquals(1, simpleEval("!queryRel(3,4,5,100,200)"), 0);
} }
@Test
public void testTimeout() throws Exception {
try {
simpleEval("for(i=0;i<256;i++){for(j=0;j<256;j++){for(k=0;k<256;k++){for(l=0;l<256;l++){ln(pi)}}}}");
fail("Loop was not stopped.");
} catch (EvaluationException e) {
assertTrue(e.getMessage().contains("Calculations exceeded time limit"));
}
}
private double simpleEval(String expressionString) throws ExpressionException { private double simpleEval(String expressionString) throws ExpressionException {
final Expression expression = compile(expressionString); final Expression expression = compile(expressionString);