Subversion Repositories pub

Compare Revisions

Ignore whitespace Rev 64 → Rev 65

/pluggablejs/trunk/src/net/outlyer/plugins/Shell.java
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);
43,19 → 46,45
 
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);
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");
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,12 → 92,33
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());
83,14 → 133,14
System.exit(5);
}
}
public static void main(String...args) {
 
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());
" java [...] %s <file1.js [file2.js [...]]>\n" +
" or\n" +
" java [...] %1$s <-i|--interactive>\n",
Shell.class.getName());
System.exit(1);
}
 
108,14 → 158,14
pe.exportObject("lang", new LanguageExtensions());
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());