Added maximum script execution time of 3 seconds.

This commit is contained in:
sk89q 2010-10-03 16:12:29 -07:00
parent ddfb7f21be
commit fc2a827c07
2 changed files with 79 additions and 4 deletions

View File

@ -631,6 +631,7 @@ public class WorldEdit extends Plugin {
* @param player
* @param filename
* @param args
* @return Whether the file was attempted execution
*/
private boolean runScript(Player player, WorldEditSession session,
EditSession editSession, String filename, String[] args) throws
@ -659,7 +660,8 @@ public class WorldEdit extends Plugin {
String code = buffer.toString();
// Evaluate
Context cx = Context.enter();
ScriptContextFactory factory = new ScriptContextFactory();
Context cx = factory.enterContext();
try {
ScriptableObject scope = cx.initStandardObjects();
@ -682,18 +684,22 @@ public class WorldEdit extends Plugin {
ScriptableObject.putProperty(scope, "minecraft",
Context.javaToJS(minecraft, scope));
logger.log(Level.INFO, player.getName() + ": executing " + filename + "...");
cx.evaluateString(scope, code, filename, 1, null);
logger.log(Level.INFO, player.getName() + ": script " + filename + " executed successfully.");
player.sendMessage(Colors.LightPurple + filename + " executed successfully.");
return true;
} catch (RhinoException re) {
player.sendMessage(Colors.Rose + "JS error: " + re.getMessage());
player.sendMessage(Colors.Rose + filename + ": JS error: " + re.getMessage());
re.printStackTrace();
} catch (Error err) {
player.sendMessage(Colors.Rose + filename + ": execution error: " + err.getMessage());
} finally {
Context.exit();
session.remember(editSession);
}
}
return true;
} catch (IOException e) {
player.sendMessage(Colors.Rose + "Script could not read or it does not exist.");
}

View File

@ -0,0 +1,69 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
import org.mozilla.javascript.*;
/**
* Context factory for the JavaScript engine.
*
* @author Albert
*/
public class ScriptContextFactory extends ContextFactory {
/**
* Context that will be used to store start time.
*/
private static class ScriptContext extends Context {
long startTime;
}
static {
ContextFactory.initGlobal(new ScriptContextFactory());
}
@Override
protected Context makeContext()
{
ScriptContext ctx = new ScriptContext();
ctx.setInstructionObserverThreshold(10000);
return ctx;
}
@Override
protected void observeInstructionCount(Context ctx, int instructionCount)
{
ScriptContext sctx = (ScriptContext)ctx;
long currentTime = System.currentTimeMillis();
if (currentTime - sctx.startTime > 3 * 1000) {
throw new Error("Exceeded 3 seconds");
}
}
@Override
protected Object doTopCall(Callable callable,
Context ctx, Scriptable scope,
Scriptable thisObj, Object[] args)
{
ScriptContext sctx = (ScriptContext)ctx;
sctx.startTime = System.currentTimeMillis();
return super.doTopCall(callable, ctx, scope, thisObj, args);
}
}