Re-added Sun Rhino implementation.

This commit is contained in:
sk89q 2011-01-30 19:49:57 -08:00
parent a4e143ded6
commit 2bc75dd5db
8 changed files with 214 additions and 4 deletions

View File

@ -17,6 +17,7 @@ Some dependencies are required:
permission system for Bukkit
- [Permissions](http://forums.bukkit.org/threads/1403/) provides an
permission system for Bukkit
- worldeditsunrhino.jar is included
For links to downloads, check out
[http://wiki.sk89q.com/wiki/WorldEdit/Development](http://wiki.sk89q.com/wiki/WorldEdit/Development)

View File

@ -16,6 +16,7 @@
<include name="GroupUsers.jar" />
<include name="Permissions.jar" />
<include name="js.jar" />
<include name="worldeditsunrhino.jar" />
</fileset>
<target name="init">
@ -46,7 +47,9 @@
<replace file="${build.dir}/plugin.yml" token="WEVERSIONMACRO" value="${version}"/>
<mkdir dir="${build.dir}/defaults"/>
<copy tofile="${build.dir}/defaults/config.yml" file="config.yml"/>
<jar jarfile="${dist.dir}/WorldEdit.jar" basedir="${build.dir}" manifest="manifest.mf" />
<jar jarfile="${dist.dir}/WorldEdit.jar" basedir="${build.dir}" manifest="manifest.mf">
<zipgroupfileset dir="lib" includes="worldeditsunrhino.jar" />
</jar>
</target>
<!-- Create the .jar -->

View File

@ -0,0 +1,3 @@
This contains sources for an implementation of the Sun Rhino engine for
WorldEdit. However, this Rhino version is an internal component of
the Sun JVM and thus it is implementation specific.

View File

@ -0,0 +1,47 @@
<project name="WorldEditSunRhino" default="dist" basedir=".">
<description>Sun Rhino implementation.</description>
<property name="src.dir" location="src"/>
<property name="build.dir" location="build"/>
<property name="dist.dir" location="dist"/>
<property name="release.dir" location="release"/>
<property name="lib.dir" location="lib"/>
<fileset dir="${lib.dir}" id="libs">
<include name="WorldEdit.jar" />
</fileset>
<target name="init">
<mkdir dir="${build.dir}"/>
</target>
<!-- Compile the source files -->
<target name="compile" depends="init">
<javac srcdir="${src.dir}" deprecation="true" includeantruntime="true" destdir="${build.dir}" debug="true">
<classpath>
<fileset refid="libs"/>
</classpath>
</javac>
</target>
<!-- Build the .jar -->
<target name="jar" depends="compile">
<mkdir dir="${dist.dir}"/>
<manifest file="manifest.mf" mode="replace">
</manifest>
<jar jarfile="${dist.dir}/worldeditsunrhino.jar" basedir="${build.dir}" manifest="manifest.mf" />
</target>
<!-- Create the .jar -->
<target name="dist">
<property name="version" value="nightly"/>
<antcall target="jar"/>
</target>
<!-- Clean the output -->
<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
<delete dir="${release.dir}"/>
</target>
</project>

View File

@ -0,0 +1,61 @@
// $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

@ -0,0 +1,90 @@
// $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;
}
}
}

BIN
lib/worldeditsunrhino.jar Normal file

Binary file not shown.

View File

@ -856,12 +856,17 @@ public class WorldEdit {
new CraftScriptContext(this, server, config, session, player, args);
CraftScriptEngine engine = null;
try {
engine = new RhinoCraftScriptEngine();
} catch (NoClassDefFoundError e) {
player.printError("Please install a scripting engine.");
return;
try {
engine = new SunRhinoCraftScriptEngine();
} catch (NoClassDefFoundError e2) {
player.printError("Failed to find an installed script engine.");
player.printError("Please see http://wiki.sk89q.com/wiki/WorldEdit/Installation");
return;
}
}
engine.setTimeLimit(config.scriptTimeout);