From 2bc75dd5db7b81d97b662113d3091e85f532c2d0 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 30 Jan 2011 19:49:57 -0800 Subject: [PATCH] Re-added Sun Rhino implementation. --- README.md | 1 + build.xml | 5 +- contrib/sunrhino/README.txt | 3 + contrib/sunrhino/build.xml | 47 +++++++++ .../scripting/SunRhinoContextFactory.java | 61 ++++++++++++ .../scripting/SunRhinoCraftScriptEngine.java | 90 ++++++++++++++++++ lib/worldeditsunrhino.jar | Bin 0 -> 5345 bytes src/com/sk89q/worldedit/WorldEdit.java | 11 ++- 8 files changed, 214 insertions(+), 4 deletions(-) create mode 100644 contrib/sunrhino/README.txt create mode 100644 contrib/sunrhino/build.xml create mode 100644 contrib/sunrhino/src/com/sk89q/worldedit/scripting/SunRhinoContextFactory.java create mode 100644 contrib/sunrhino/src/com/sk89q/worldedit/scripting/SunRhinoCraftScriptEngine.java create mode 100644 lib/worldeditsunrhino.jar diff --git a/README.md b/README.md index cb06d1c82..aacf53c97 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/build.xml b/build.xml index 82d814618..45cd0dde9 100644 --- a/build.xml +++ b/build.xml @@ -16,6 +16,7 @@ + @@ -46,7 +47,9 @@ - + + + diff --git a/contrib/sunrhino/README.txt b/contrib/sunrhino/README.txt new file mode 100644 index 000000000..29dd20bd6 --- /dev/null +++ b/contrib/sunrhino/README.txt @@ -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. \ No newline at end of file diff --git a/contrib/sunrhino/build.xml b/contrib/sunrhino/build.xml new file mode 100644 index 000000000..f710301e6 --- /dev/null +++ b/contrib/sunrhino/build.xml @@ -0,0 +1,47 @@ + + Sun Rhino implementation. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/contrib/sunrhino/src/com/sk89q/worldedit/scripting/SunRhinoContextFactory.java b/contrib/sunrhino/src/com/sk89q/worldedit/scripting/SunRhinoContextFactory.java new file mode 100644 index 000000000..aad0fa2ae --- /dev/null +++ b/contrib/sunrhino/src/com/sk89q/worldedit/scripting/SunRhinoContextFactory.java @@ -0,0 +1,61 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . + */ + +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(); + } + } +} \ No newline at end of file diff --git a/contrib/sunrhino/src/com/sk89q/worldedit/scripting/SunRhinoCraftScriptEngine.java b/contrib/sunrhino/src/com/sk89q/worldedit/scripting/SunRhinoCraftScriptEngine.java new file mode 100644 index 000000000..4706b69e4 --- /dev/null +++ b/contrib/sunrhino/src/com/sk89q/worldedit/scripting/SunRhinoCraftScriptEngine.java @@ -0,0 +1,90 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +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 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 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; + } + } + +} diff --git a/lib/worldeditsunrhino.jar b/lib/worldeditsunrhino.jar new file mode 100644 index 0000000000000000000000000000000000000000..f5d9f656b73ea08bb2e45d08db7eddbfb4009120 GIT binary patch literal 5345 zcmbVQ2UJtb)(#yZND-t;kMt5cBE7eO2{km48tD+FgCNob0w^7n-U(Ge1VIt0(gh^) zL+DXNY0^X};Zo;jbPE&(AK;M7R66cqlr`Lj6#AOz^E z8z~EELo`Ib_5lEff4Y(ah)-NUXfWJ`pSU`noU|wBU#|Mf5N!>0n31r)#+Lp-zn-p; z@Cb#j5dYx7P(4(1TzqcHdq9=OOHfO1P|Fw)iwAnO1N48KeHVI87Yak>@Q1w%<809; zeo4mktjM42X-2R5epgBtsO)hU*#rOHfXr6QR?ENo^6%XyJn4&_hnvXv-u}DCX}YH# zJ}xq{UjM=GG^-zez8>DL_73(4NLr9^YOm8I+gzHn~NMK}l`=Aa`c(-hqH=IJsg6ibi})xW&`XYH|7Pz3f1Po}+`W zdxSNp8}#OavSwa}AYP3~#27UP4|^KnopT&_WVvKY{jUg?jS?`Gn^F_T{a>Bt`F;3X zX8uAfyzZ7%iuzD-S5U#WuH6&eM3?fXRVJS!7EQgT7Fs$3!W{S?Y?=={z`6I+2Qu=a zNp&VwIbJts>WdUEwlLQ4&VO#e?eiCo%wR_df}xjIx1Ne37u9?4+`7G?iZ|;ednHM; zv#1RWA1=EQx?6NjuLvbE%ev4FB!9PrseH+zYNOtWDzmax*=hkQ#wrN30{wY2ClJcU120ILxNu$iKdm) zI5L8o6~BK+-)8J82ldn>0RWIj3IJUCA7|{(NiueWF&oo{xYya=OKha#b<2gvrd-52 z@KqJv4{xkByAE*UJiAchP)}eX^<-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