Plex-FAWE/src/main/java/com/sk89q/worldedit/scripting/RhinoCraftScriptEngine.java

88 lines
3.1 KiB
Java
Raw Normal View History

2011-01-22 21:34:05 +00:00
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
2011-01-22 21:34:05 +00:00
*
* 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;
2011-12-25 23:36:23 +00:00
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.RhinoException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.WrappedException;
2011-01-23 00:23:04 +00:00
import com.sk89q.worldedit.WorldEditException;
2011-01-22 21:34:05 +00:00
public class RhinoCraftScriptEngine implements CraftScriptEngine {
private int timeLimit;
public void setTimeLimit(int milliseconds) {
timeLimit = milliseconds;
}
public int getTimeLimit() {
return timeLimit;
}
public Object evaluate(String script, String filename, Map<String, Object> args)
2011-01-23 00:23:04 +00:00
throws ScriptException, Throwable {
2011-01-22 21:34:05 +00:00
RhinoContextFactory factory = new RhinoContextFactory(timeLimit);
Context cx = factory.enterContext();
ScriptableObject scriptable = new ImporterTopLevel(cx);
Scriptable scope = cx.initStandardObjects(scriptable);
2011-11-23 01:29:48 +00:00
2011-01-22 21:34:05 +00:00
for (Map.Entry<String, Object> entry : args.entrySet()) {
ScriptableObject.putProperty(scope, entry.getKey(),
Context.javaToJS(entry.getValue(), scope));
}
try {
return cx.evaluateString(scope, script, filename, 1, null);
} catch (Error e) {
throw new ScriptException(e.getMessage());
} catch (RhinoException e) {
2011-01-23 00:23:04 +00:00
if (e instanceof WrappedException) {
2011-09-24 19:24:10 +00:00
Throwable cause = ((WrappedException) e).getCause();
2011-01-23 00:23:04 +00:00
if (cause instanceof WorldEditException) {
throw cause;
2011-01-23 00:23:04 +00:00
}
}
2011-11-23 01:29:48 +00:00
2011-01-22 21:34:05 +00:00
String msg;
int line = (line = e.lineNumber()) == 0 ? -1 : line;
2011-11-23 01:29:48 +00:00
2011-01-22 21:34:05 +00:00
if (e instanceof JavaScriptException) {
2011-09-24 19:24:10 +00:00
msg = String.valueOf(((JavaScriptException) e).getValue());
2011-01-22 21:34:05 +00:00
} else {
msg = e.getMessage();
}
2011-11-23 01:29:48 +00:00
2011-01-22 21:34:05 +00:00
ScriptException scriptException =
2011-11-23 01:29:48 +00:00
new ScriptException(msg, e.sourceName(), line);
2011-01-22 21:34:05 +00:00
scriptException.initCause(e);
2011-11-23 01:29:48 +00:00
2011-01-22 21:34:05 +00:00
throw scriptException;
} finally {
Context.exit();
}
}
}