| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import org.mozilla.javascript.*; |
| 8 |
import java.io.*; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class Shell extends ScriptableObject |
| 19 |
{ |
| 20 |
private static final long serialVersionUID = -5638074146250193112L; |
| 21 |
|
| 22 |
@Override |
| 23 |
public String getClassName() |
| 24 |
{ |
| 25 |
return "global"; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static void main(String args[]) { |
| 37 |
|
| 38 |
Context cx = Context.enter(); |
| 39 |
try { |
| 40 |
|
| 41 |
|
| 42 |
Shell shell = new Shell(); |
| 43 |
cx.initStandardObjects(shell); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
String[] names = { "print", "quit", "version", "load", "help" }; |
| 48 |
shell.defineFunctionProperties(names, Shell.class, |
| 49 |
ScriptableObject.DONTENUM); |
| 50 |
|
| 51 |
args = processOptions(cx, args); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
Object[] array; |
| 56 |
if (args.length == 0) { |
| 57 |
array = new Object[0]; |
| 58 |
} else { |
| 59 |
int length = args.length - 1; |
| 60 |
array = new Object[length]; |
| 61 |
System.arraycopy(args, 1, array, 0, length); |
| 62 |
} |
| 63 |
Scriptable argsObj = cx.newArray(shell, array); |
| 64 |
shell.defineProperty("arguments", argsObj, |
| 65 |
ScriptableObject.DONTENUM); |
| 66 |
|
| 67 |
shell.processSource(cx, args.length == 0 ? null : args[0]); |
| 68 |
} finally { |
| 69 |
Context.exit(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static String[] processOptions(Context cx, String args[]) { |
| 77 |
for (int i=0; i < args.length; i++) { |
| 78 |
String arg = args[i]; |
| 79 |
if (!arg.startsWith("-")) { |
| 80 |
String[] result = new String[args.length - i]; |
| 81 |
for (int j=i; j < args.length; j++) |
| 82 |
result[j-i] = args[j]; |
| 83 |
return result; |
| 84 |
} |
| 85 |
if (arg.equals("-version")) { |
| 86 |
if (++i == args.length) |
| 87 |
usage(arg); |
| 88 |
double d = Context.toNumber(args[i]); |
| 89 |
if (d != d) |
| 90 |
usage(arg); |
| 91 |
cx.setLanguageVersion((int) d); |
| 92 |
continue; |
| 93 |
} |
| 94 |
usage(arg); |
| 95 |
} |
| 96 |
return new String[0]; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private static void usage(String s) { |
| 103 |
p("Didn't understand \"" + s + "\"."); |
| 104 |
p("Valid arguments are:"); |
| 105 |
p("-version 100|110|120|130|140|150|160|170"); |
| 106 |
System.exit(1); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public void help() { |
| 115 |
p(""); |
| 116 |
p("Command Description"); |
| 117 |
p("======= ==========="); |
| 118 |
p("help() Display usage and help messages. "); |
| 119 |
p("defineClass(className) Define an extension using the Java class"); |
| 120 |
p(" named with the string argument. "); |
| 121 |
p(" Uses ScriptableObject.defineClass(). "); |
| 122 |
p("load(['foo.js', ...]) Load JavaScript source files named by "); |
| 123 |
p(" string arguments. "); |
| 124 |
p("loadClass(className) Load a class named by a string argument."); |
| 125 |
p(" The class must be a script compiled to a"); |
| 126 |
p(" class file. "); |
| 127 |
p("print([expr ...]) Evaluate and print expressions. "); |
| 128 |
p("quit() Quit the shell. "); |
| 129 |
p("version([number]) Get or set the JavaScript version number."); |
| 130 |
p(""); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public static void print(Context cx, Scriptable thisObj, |
| 143 |
Object[] args, Function funObj) |
| 144 |
{ |
| 145 |
for (int i=0; i < args.length; i++) { |
| 146 |
if (i > 0) |
| 147 |
System.out.print(" "); |
| 148 |
|
| 149 |
|
| 150 |
String s = Context.toString(args[i]); |
| 151 |
|
| 152 |
System.out.print(s); |
| 153 |
} |
| 154 |
System.out.println(); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public void quit() |
| 165 |
{ |
| 166 |
quitting = true; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public static double version(Context cx, Scriptable thisObj, |
| 175 |
Object[] args, Function funObj) |
| 176 |
{ |
| 177 |
double result = cx.getLanguageVersion(); |
| 178 |
if (args.length > 0) { |
| 179 |
double d = Context.toNumber(args[0]); |
| 180 |
cx.setLanguageVersion((int) d); |
| 181 |
} |
| 182 |
return result; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public static void load(Context cx, Scriptable thisObj, |
| 192 |
Object[] args, Function funObj) |
| 193 |
{ |
| 194 |
Shell shell = (Shell)getTopLevelScope(thisObj); |
| 195 |
for (int i = 0; i < args.length; i++) { |
| 196 |
shell.processSource(cx, Context.toString(args[i])); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
private void processSource(Context cx, String filename) |
| 209 |
{ |
| 210 |
if (filename == null) { |
| 211 |
BufferedReader in = new BufferedReader |
| 212 |
(new InputStreamReader(System.in)); |
| 213 |
String sourceName = "<stdin>"; |
| 214 |
int lineno = 1; |
| 215 |
boolean hitEOF = false; |
| 216 |
do { |
| 217 |
int startline = lineno; |
| 218 |
System.err.print("js> "); |
| 219 |
System.err.flush(); |
| 220 |
try { |
| 221 |
String source = ""; |
| 222 |
|
| 223 |
while(true) { |
| 224 |
String newline; |
| 225 |
newline = in.readLine(); |
| 226 |
if (newline == null) { |
| 227 |
hitEOF = true; |
| 228 |
break; |
| 229 |
} |
| 230 |
source = source + newline + "\n"; |
| 231 |
lineno++; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
if (cx.stringIsCompilableUnit(source)) |
| 239 |
break; |
| 240 |
} |
| 241 |
Object result = cx.evaluateString(this, source, |
| 242 |
sourceName, startline, |
| 243 |
null); |
| 244 |
if (result != Context.getUndefinedValue()) { |
| 245 |
System.err.println(Context.toString(result)); |
| 246 |
} |
| 247 |
} |
| 248 |
catch (WrappedException we) { |
| 249 |
|
| 250 |
|
| 251 |
System.err.println(we.getWrappedException().toString()); |
| 252 |
we.printStackTrace(); |
| 253 |
} |
| 254 |
catch (EvaluatorException ee) { |
| 255 |
|
| 256 |
System.err.println("js: " + ee.getMessage()); |
| 257 |
} |
| 258 |
catch (JavaScriptException jse) { |
| 259 |
|
| 260 |
System.err.println("js: " + jse.getMessage()); |
| 261 |
} |
| 262 |
catch (IOException ioe) { |
| 263 |
System.err.println(ioe.toString()); |
| 264 |
} |
| 265 |
if (quitting) { |
| 266 |
|
| 267 |
break; |
| 268 |
} |
| 269 |
} while (!hitEOF); |
| 270 |
System.err.println(); |
| 271 |
} else { |
| 272 |
FileReader in = null; |
| 273 |
try { |
| 274 |
in = new FileReader(filename); |
| 275 |
} |
| 276 |
catch (FileNotFoundException ex) { |
| 277 |
Context.reportError("Couldn't open file \"" + filename + "\"."); |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
try { |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
cx.evaluateReader(this, in, filename, 1, null); |
| 286 |
} |
| 287 |
catch (WrappedException we) { |
| 288 |
System.err.println(we.getWrappedException().toString()); |
| 289 |
we.printStackTrace(); |
| 290 |
} |
| 291 |
catch (EvaluatorException ee) { |
| 292 |
System.err.println("js: " + ee.getMessage()); |
| 293 |
} |
| 294 |
catch (JavaScriptException jse) { |
| 295 |
System.err.println("js: " + jse.getMessage()); |
| 296 |
} |
| 297 |
catch (IOException ioe) { |
| 298 |
System.err.println(ioe.toString()); |
| 299 |
} |
| 300 |
finally { |
| 301 |
try { |
| 302 |
in.close(); |
| 303 |
} |
| 304 |
catch (IOException ioe) { |
| 305 |
System.err.println(ioe.toString()); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
private static void p(String s) { |
| 312 |
System.out.println(s); |
| 313 |
} |
| 314 |
|
| 315 |
private boolean quitting; |
| 316 |
} |
| 317 |
|