Added maximum script execution time of 3 seconds.

Dieser Commit ist enthalten in:
sk89q 2010-10-03 16:12:29 -07:00
Ursprung ddfb7f21be
Commit fc2a827c07
2 geänderte Dateien mit 79 neuen und 4 gelöschten Zeilen

Datei anzeigen

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

Datei anzeigen

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