31,11 → 31,14 |
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); |
48,14 → 51,40 |
// 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 feature: when |
// $net.outlyer.runtime.internal.shell is true an extra println() is |
// undocumented features: when |
// $net_outlyer.runtime.internal.shell,autoEOL is true an extra println() is |
// issued after each eval |
rhino.eval("$net.outlyer.runtime.internal.shell = { autoEOL: false, };"); |
// and when |
// $net_outlyer.runtime.internal.shell.inspect is true the result |
// of each line is automatically printed |
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" + |
" var s = new String();" + |
" s += '[';\n" + |
" 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% : "); |
63,13 → 92,34 |
if (null == line) { |
break; |
} |
if (line.isEmpty()) { |
continue; |
} |
try { |
rhino.eval(line); |
final boolean eol = (Boolean) rhino.eval("$net.outlyer.runtime.internal.shell.autoEOL"); |
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()); |
} |