0,0 → 1,182 |
package net.outlyer.plugins; |
|
/* |
* Copyright (c) 2008, Toni Corvera. All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions are met: |
* |
* * Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* * Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* |
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
// $Id$ |
|
import java.io.File; |
import javax.script.ScriptEngine; |
import javax.script.ScriptException; |
import net.outlyer.plugins.utils.*; |
|
import static net.outlyer.plugins.Internal.getInternalObjectFieldName; |
|
/** |
* Sample shell for tests, or scripts. |
* Files passed as arguments will be loaded and executed. |
*/ |
public final class Shell { |
|
public static void interactive(final PluginEnvironment pe) { |
try { |
final Sandbox sandbox = pe.createSandbox(null); |
final ScriptEngine rhino = ((SandboxImpl) sandbox).executeAndGetEngine(); |
|
final Input in = new Input(); |
final Output out = new Output(); |
System.err.printf("pluggablejsShell v%d.%d.%d\n", API.VERSION, API.MIN_SUPPORTED_VERSION, API.REVISION); |
final boolean isWin = System.getProperty("os.name").toLowerCase().startsWith("windows"); |
// apparently windows doesn't get EOF until enter is pressed |
System.err.printf(" use EOF (%s) or break (CTRL+C) to exit\n", (isWin) ? "CTRL+Z,Enter" : "CTRL+D"); |
|
// This object is used as a template to identify JavaScript arrays; |
// since the sun.org.mozilla.javascript package is forbidden a more |
// direct check can't be used |
Object jsArray; |
|
// This is the name of the internal function used to dump |
// javascript arrays |
final String arrayDumpFn = getInternalObjectFieldName("shell.arrayDump"); |
|
try { |
// undocumented features: when |
// $net_outlyer.runtime.internal.shell.autoEOL is true an extra println() is |
// issued after each eval |
// and when |
// $net_outlyer.runtime.internal.shell.inspect is true the result |
// of each line is automatically printed |
// arrayDump is used internally to print JavaScript arrays |
rhino.eval(getInternalObjectFieldName("shell")+" = { " + |
" autoEOL: false, inspect: true, arrayDump: null };"); |
jsArray = rhino.eval("new Array()"); |
|
// Define the arrayDump internal function |
rhino.eval(arrayDumpFn+ |
" = function(arr) {\n" + |
" if (0==arr.length) { java.lang.System.out.println('[]'); return; }\n"+ |
" var s = new String('[');" + |
" for (var i=0;i<arr.length;++i) {\n" + |
" s += arr[i]+',';\n" + |
" }\n" + |
" java.lang.System.out.println(s.substring(0,s.length-1)+']');\n" + |
"};"); |
} |
catch (final ScriptException e) { |
e.printStackTrace(); |
throw new IllegalStateException("Failed to initialise interactive shell"); |
} |
while (true) { |
out.print("pjsh% : "); |
final String line = in.readline(); |
if (null == line) { |
break; |
} |
if (line.isEmpty()) { |
continue; |
} |
try { |
final Object result = rhino.eval(line); |
final boolean eol = (Boolean) rhino.eval(getInternalObjectFieldName("shell.autoEOL")); |
if (eol) { |
out.println(); |
} |
final boolean insp = (Boolean) rhino.eval(getInternalObjectFieldName("shell.inspect")); |
if (insp) { |
if (null==result) { |
out.println("null"); |
} |
else if (result.getClass() == jsArray.getClass()) { // JavaScript array |
// Store... |
Internal.setInternalValue(rhino, "result", result); |
// Dump |
rhino.eval(String.format("%s(%s);", |
arrayDumpFn, |
getInternalObjectFieldName("get")+"('result')")); |
Internal.setInternalValue(rhino, "result", null); |
} |
else { |
out.println(result.toString()); |
} |
} |
} |
catch (final ScriptException e) { |
System.err.println("//[E] Error: " + e.getMessage()); |
} |
} |
// EOF |
System.exit(0); |
} |
catch (final PluginException e) { |
System.err.printf("Exception while running on-the-fly " + |
"interpreted script", e.getLocalizedMessage()); |
System.exit(5); |
} |
} |
|
public static void main(String... args) { |
if (args.length == 0) { |
System.err.printf("Usage: \n" + |
" java [...] %s <file1.js [file2.js [...]]>\n" + |
" or\n" + |
" java [...] %1$s <-i|--interactive>\n", |
Shell.class.getName()); |
System.exit(1); |
} |
|
final PluginEnvironment pe = new PluginEnvironment(); |
pe.exportObject("input", new Input()); |
pe.exportObject("out", new Output()); |
pe.exportObject("err", new Output(System.err)); |
pe.exportObject("hooks", new Hooks()); |
// Generally either UI or GUI should be used but not both |
// since GUI falls-back to UI if appropriate (so the ui object |
// will use the "best" implementation) |
pe.exportObject("ui", new GUI()); |
pe.exportObject("gui", new GUI()); |
pe.exportObject("cui", new UI()); // cui stands for Console UI |
pe.exportObject("lang", new RedundantLanguageExtensions()); |
final PluginObject po = new PluginObject(); |
pe.setPluginObject(po); |
|
if (args.length == 1 && (args[0].equals("-i") || args[0].equals("--interactive"))) { |
interactive(pe); |
assert false; |
} |
|
// Non-interactive mode |
|
for (final String path : args) { |
try { |
final Sandbox sandbox = pe.createSandbox(new File(path).toURI()); |
sandbox.execute(); |
} |
catch (final PluginException e) { |
System.err.printf("Exception while running plugin %s: %s\n", |
path, e.getLocalizedMessage()); |
System.exit(2); |
} |
} |
} |
} |
Property changes: |
Added: svn:keywords |
+Rev Id Date |
\ No newline at end of property |