Small patches for timed-calc post-1.12-merge

This commit is contained in:
Kenzie Togami 2018-10-01 16:04:48 -07:00
parent 776eb24c0e
commit e16dacc11e
No known key found for this signature in database
GPG Key ID: 5D200B325E157A81
4 changed files with 30 additions and 108 deletions

View File

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

View File

@ -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);
}
}

View File

@ -22,13 +22,16 @@ package com.sk89q.worldedit.internal.expression;
import static java.lang.Math.atan2;
import static java.lang.Math.sin;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import com.sk89q.worldedit.LocalConfiguration;
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.parser.ParserException;
import com.sk89q.worldedit.internal.expression.runtime.EvaluationException;
@ -37,8 +40,13 @@ import com.sk89q.worldedit.internal.expression.runtime.ExpressionEnvironment;
public class ExpressionTest {
@Before
public void setup() {
WorldEdit.getInstance().getPlatformManager().register(new ExpressionPlatform());
WorldEdit.getInstance().getPlatformManager().handlePlatformReady(new PlatformReadyEvent());
Platform mockPlat = Mockito.mock(Platform.class);
Mockito.when(mockPlat.getConfiguration()).thenReturn(new LocalConfiguration() {
@Override
public void load() {
}
});
WorldEdit.getInstance().getPlatformManager().register(mockPlat);
}
@Test
@ -172,6 +180,16 @@ public class ExpressionTest {
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 {
final Expression expression = compile(expressionString);