Added compiling information, removed dependency on internal Sun components.

This commit is contained in:
sk89q
2011-01-30 15:10:48 -08:00
parent c37ccb8df4
commit 9fed59cf63
4 changed files with 38 additions and 157 deletions

View File

@ -860,12 +860,8 @@ public class WorldEdit {
try {
engine = new RhinoCraftScriptEngine();
} catch (NoClassDefFoundError e) {
try {
engine = new SunRhinoCraftScriptEngine();
} catch (NoClassDefFoundError e2) {
player.printError("Failed to find an installed script engine.");
return;
}
player.printError("Please install a scripting engine.");
return;
}
engine.setTimeLimit(config.scriptTimeout);

View File

@ -1,61 +0,0 @@
// $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.scripting;
import sun.org.mozilla.javascript.internal.*;
public class SunRhinoContextFactory extends ContextFactory {
protected int timeLimit;
public SunRhinoContextFactory(int timeLimit) {
this.timeLimit = timeLimit;
}
protected Context makeContext() {
RhinoContext cx = new RhinoContext(this);
cx.setInstructionObserverThreshold(10000);
return cx;
}
protected void observeInstructionCount(Context cx, int instructionCount) {
RhinoContext mcx = (RhinoContext)cx;
long currentTime = System.currentTimeMillis();
if (currentTime - mcx.startTime > timeLimit) {
throw new Error("Script timed out (" + timeLimit + "ms)");
}
}
protected Object doTopCall(Callable callable, Context cx, Scriptable scope,
Scriptable thisObj, Object[] args) {
RhinoContext mcx = (RhinoContext)cx;
mcx.startTime = System.currentTimeMillis();
return super.doTopCall(callable, cx, scope, thisObj, args);
}
private static class RhinoContext extends Context {
long startTime;
public RhinoContext(ContextFactory factory) {
super();
}
}
}

View File

@ -1,90 +0,0 @@
// $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.scripting;
import java.util.Map;
import javax.script.ScriptException;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import sun.org.mozilla.javascript.internal.*;
public class SunRhinoCraftScriptEngine implements CraftScriptEngine {
private int timeLimit;
@Override
public void setTimeLimit(int milliseconds) {
timeLimit = milliseconds;
}
@Override
public int getTimeLimit() {
return timeLimit;
}
@Override
public Object evaluate(final String script, final String filename,
final Map<String, Object> args)
throws ScriptException, Throwable {
SunRhinoContextFactory factory = new SunRhinoContextFactory(timeLimit);
factory.initApplicationClassLoader(WorldEdit.class.getClassLoader());
try {
return factory.call(new ContextAction() {
public Object run(Context cx) {
Scriptable topLevel = new ImporterTopLevel(cx);
Scriptable scope = cx.initStandardObjects();
topLevel.setParentScope(scope);
for (Map.Entry<String, Object> entry : args.entrySet()) {
ScriptableObject.putProperty(scope, entry.getKey(),
Context.javaToJS(entry.getValue(), scope));
}
return cx.evaluateString(topLevel, script, filename, 1, null);
}
});
} catch (Error e) {
throw new ScriptException(e.getMessage());
} catch (RhinoException e) {
if (e instanceof WrappedException) {
Throwable cause = ((WrappedException)e).getCause();
if (cause instanceof WorldEditException) {
throw ((WrappedException)e).getCause();
}
}
String msg;
int line = (line = e.lineNumber()) == 0 ? -1 : line;
if (e instanceof JavaScriptException) {
msg = String.valueOf(((JavaScriptException)e).getValue());
} else {
msg = e.getMessage();
}
ScriptException scriptException =
new ScriptException(msg, e.sourceName(), line);
scriptException.initCause(e);
throw scriptException;
}
}
}