public int run() {
if (config.isShowVersion()) {
showVersion();
}
if (config.isShowCopyright()) {
showCopyright();
}
if (!config.shouldRunInterpreter() ) {
if (config.shouldPrintUsage()) {
printUsage();
}
if (config.shouldPrintProperties()) {
printProperties();
}
return 0;
}
InputStream in = config.getScriptSource();
String filename = config.displayedFileName();
Ruby runtime = Ruby.newInstance(config);
// set thread context JRuby classloader here, for the main thread
try {
Thread.currentThread().setContextClassLoader(runtime.getJRubyClassLoader());
} catch (SecurityException se) {
// can't set TC classloader
if (runtime.getInstanceConfig().isVerbose()) {
System.err.println("WARNING: Security restrictions disallowed setting context classloader for main thread.");
}
}
if (in == null) {
// no script to run, return success below
} else if (config.isShouldCheckSyntax()) {
runtime.parseFromMain(in, filename);
config.getOutput().println("Syntax OK");
} else {
long now = -1;
try {
if (config.isBenchmarking()) {
now = System.currentTimeMillis();
}
if (config.isSamplingEnabled()) {
SimpleSampler.startSampleThread();
}
try {
runtime.runFromMain(in, filename);
} finally {
runtime.tearDown();
if (config.isBenchmarking()) {
config.getOutput().println("Runtime: " + (System.currentTimeMillis() - now) + " ms");
}
if (config.isSamplingEnabled()) {
org.jruby.util.SimpleSampler.report();
}
}
} catch (RaiseException rj) {
RubyException raisedException = rj.getException();
if (runtime.getSystemExit().isInstance(raisedException)) {
IRubyObject status = raisedException.callMethod(runtime.getCurrentContext(), "status");
if (status != null && !status.isNil()) {
return RubyNumeric.fix2int(status);
}
} else {
runtime.printError(raisedException);
return 1;
}
}
}
return 0;
}
|