Skip to content

Commit

Permalink
Merge branch 'master' into truffle-head
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Feb 2, 2015
2 parents b35d34d + c420b1d commit b158469
Show file tree
Hide file tree
Showing 29 changed files with 814 additions and 484 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -70,7 +70,6 @@ matrix:
- env: PHASE='-Prake -Dtask=spec:jrubyc'
- env: PHASE='-Prake -Dtask=spec:profiler'
- env: COMMAND=tool/truffle-findbugs.sh
- env: PHASE='-Pmain'

branches:
only:
Expand Down
127 changes: 80 additions & 47 deletions core/src/main/java/org/jruby/Ruby.java
Expand Up @@ -45,14 +45,14 @@
import org.jruby.ast.FCallNode;
import org.jruby.ast.GlobalAsgnNode;
import org.jruby.ast.GlobalVarNode;
import org.jruby.ast.NewlineNode;
import org.jruby.ast.VCallNode;
import org.jruby.ast.WhileNode;
import org.jruby.compiler.Constantizable;
import org.jruby.compiler.NotCompilableException;
import org.jruby.ext.jruby.JRubyLibrary;
import org.jruby.ext.thread.ThreadLibrary;
import org.jruby.ir.IRScriptBody;
import org.jruby.javasupport.JavaSupport;
import org.jruby.javasupport.JavaSupportImpl;
import org.jruby.lexer.yacc.ISourcePosition;
import org.jruby.parser.StaticScope;
import org.objectweb.asm.util.TraceClassVisitor;
Expand Down Expand Up @@ -98,7 +98,6 @@
import org.jruby.ir.persistence.IRReader;
import org.jruby.ir.persistence.IRReaderFile;
import org.jruby.ir.persistence.util.IRFileExpert;
import org.jruby.javasupport.JavaSupport;
import org.jruby.javasupport.proxy.JavaProxyClassFactory;
import org.jruby.management.BeanManager;
import org.jruby.management.BeanManagerFactory;
Expand Down Expand Up @@ -365,6 +364,10 @@ public static boolean isGlobalRuntimeReady() {
return globalRuntime != null;
}

public static boolean isSubstrateVM() {
return false;
}

/**
* Set the global runtime to the given runtime only if it has no been set.
*
Expand Down Expand Up @@ -733,21 +736,7 @@ public IRubyObject runNormally(Node scriptNode) {
ScriptAndCode scriptAndCode = null;
boolean compile = getInstanceConfig().getCompileMode().shouldPrecompileCLI();
if (compile || config.isShowBytecode()) {
// IR JIT does not handle all scripts yet, so let those that fail run in interpreter instead
// FIXME: restore error once JIT should handle everything
try {
scriptAndCode = tryCompile(scriptNode, new ClassDefininngJRubyClassLoader(getJRubyClassLoader()));
if (scriptAndCode != null && Options.JIT_LOGGING.load()) {
LOG.info("done compiling target script: " + scriptNode.getPosition().getFile());
}
} catch (Exception e) {
if (Options.JIT_LOGGING.load()) {
LOG.error("failed to compile target script '" + scriptNode.getPosition().getFile() + "'");
if (Options.JIT_LOGGING_VERBOSE.load()) {
e.printStackTrace();
}
}
}
scriptAndCode = precompileCLI(scriptNode);
}

if (scriptAndCode != null) {
Expand All @@ -767,6 +756,27 @@ public IRubyObject runNormally(Node scriptNode) {
}
}

private ScriptAndCode precompileCLI(Node scriptNode) {
ScriptAndCode scriptAndCode = null;

// IR JIT does not handle all scripts yet, so let those that fail run in interpreter instead
// FIXME: restore error once JIT should handle everything
try {
scriptAndCode = tryCompile(scriptNode, new ClassDefininngJRubyClassLoader(getJRubyClassLoader()));
if (scriptAndCode != null && Options.JIT_LOGGING.load()) {
LOG.info("done compiling target script: " + scriptNode.getPosition().getFile());
}
} catch (Exception e) {
if (Options.JIT_LOGGING.load()) {
LOG.error("failed to compile target script '" + scriptNode.getPosition().getFile() + "'");
if (Options.JIT_LOGGING_VERBOSE.load()) {
e.printStackTrace();
}
}
}
return scriptAndCode;
}

/**
* Try to compile the code associated with the given Node, returning an
* instance of the successfully-compiled Script or null if the script could
Expand Down Expand Up @@ -899,29 +909,37 @@ public JITCompiler getJITCompiler() {

public synchronized TruffleBridge getTruffleBridge() {
if (truffleBridge == null) {
/*
* It's possible to remove Truffle classes from the JRuby distribution, so we provide a sensible
* explanation when the classes are not found.
*/
truffleBridge = loadTruffleBridge();
}

final Class<?> clazz;
return truffleBridge;
}

try {
clazz = getJRubyClassLoader().loadClass("org.jruby.truffle.TruffleBridgeImpl");
} catch (Exception e) {
throw new UnsupportedOperationException("Support for Truffle has been removed from this distribution", e);
}
private TruffleBridge loadTruffleBridge() {
/*
* It's possible to remove Truffle classes from the JRuby distribution, so we provide a sensible
* explanation when the classes are not found.
*/

try {
Constructor<?> con = clazz.getConstructor(Ruby.class);
truffleBridge = (TruffleBridge) con.newInstance(this);
} catch (Exception e) {
throw new UnsupportedOperationException("Error while calling the constructor of Truffle Bridge", e);
}
final Class<?> clazz;

try {
clazz = getJRubyClassLoader().loadClass("org.jruby.truffle.TruffleBridgeImpl");
} catch (Exception e) {
throw new UnsupportedOperationException("Support for Truffle has been removed from this distribution", e);
}

final TruffleBridge truffleBridge;

truffleBridge.init();
try {
Constructor<?> con = clazz.getConstructor(Ruby.class);
truffleBridge = (TruffleBridge) con.newInstance(this);
} catch (Exception e) {
throw new UnsupportedOperationException("Error while calling the constructor of Truffle Bridge", e);
}

truffleBridge.init();

return truffleBridge;
}

Expand Down Expand Up @@ -1196,7 +1214,7 @@ private void init() {
// Construct key services
loadService = config.createLoadService(this);
posix = POSIXFactory.getPOSIX(new JRubyPOSIXHandler(this), config.isNativeEnabled());
javaSupport = new JavaSupport(this);
javaSupport = loadJavaSupport();

executor = new ThreadPoolExecutor(
RubyInstanceConfig.POOL_MIN,
Expand Down Expand Up @@ -1257,13 +1275,7 @@ private void init() {

// load JRuby internals, which loads Java support
// if we can't use reflection, 'jruby' and 'java' won't work; no load.
boolean reflectionWorks;
try {
ClassLoader.class.getDeclaredMethod("getResourceAsStream", String.class);
reflectionWorks = true;
} catch (Exception e) {
reflectionWorks = false;
}
boolean reflectionWorks = doesReflectionWork();

if (!RubyInstanceConfig.DEBUG_PARSER && reflectionWorks
&& getInstanceConfig().getCompileMode() != CompileMode.TRUFFLE) {
Expand Down Expand Up @@ -1294,7 +1306,7 @@ && getInstanceConfig().getCompileMode() != CompileMode.TRUFFLE) {
}

if (config.getLoadGemfile()) {
loadService.loadFromClassLoader(getClassLoader(), "jruby/bundler/startup.rb", false);
loadBundler();
}

setNetworkStack();
Expand All @@ -1308,6 +1320,23 @@ && getInstanceConfig().getCompileMode() != CompileMode.TRUFFLE) {
}
}

public JavaSupport loadJavaSupport() {
return new JavaSupportImpl(this);
}

private void loadBundler() {
loadService.loadFromClassLoader(getClassLoader(), "jruby/bundler/startup.rb", false);
}

private boolean doesReflectionWork() {
try {
ClassLoader.class.getDeclaredMethod("getResourceAsStream", String.class);
return true;
} catch (Exception e) {
return false;
}
}

private void bootstrap() {
initCore();
initExceptions();
Expand Down Expand Up @@ -3269,9 +3298,7 @@ public void tearDown(boolean systemExit) {

getSelectorPool().cleanup();

if (getJRubyClassLoader() != null) {
getJRubyClassLoader().tearDown(isDebug());
}
tearDownClassLoader();

if (config.isProfilingEntireRun()) {
// not using logging because it's formatted
Expand All @@ -3296,6 +3323,12 @@ public void tearDown(boolean systemExit) {
}
}

private void tearDownClassLoader() {
if (getJRubyClassLoader() != null) {
getJRubyClassLoader().tearDown(isDebug());
}
}

/**
* TDOD remove the synchronized. Synchronization should be a implementation detail of the ProfilingService.
* @param profileData
Expand Down
12 changes: 10 additions & 2 deletions core/src/main/java/org/jruby/RubyInstanceConfig.java
Expand Up @@ -390,7 +390,7 @@ public InputStream getScriptSource() {
if (script.startsWith("file:") && script.indexOf(".jar!/") != -1) {
stream = new URL("jar:" + script).openStream();
} else if (script.startsWith("classpath:")) {
stream = Ruby.getClassLoader().getResourceAsStream(script.substring("classpath:".length()));
stream = getScriptSourceFromJar(script);
} else {
File file = JRubyFile.create(getCurrentDirectory(), getScriptFileName());
if (isXFlag()) {
Expand All @@ -414,6 +414,10 @@ public InputStream getScriptSource() {
}
}

private InputStream getScriptSourceFromJar(String script) {
return Ruby.getClassLoader().getResourceAsStream(script.substring("classpath:".length()));
}

private static InputStream findScript(File file) throws IOException {
StringBuffer buf = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader(file));
Expand Down Expand Up @@ -1401,6 +1405,10 @@ public String getProfilingService() {
public void setProfilingService( String service ) {
this.profilingService = service;
}

private static ClassLoader setupLoader() {
return RubyInstanceConfig.class.getClassLoader();
}

////////////////////////////////////////////////////////////////////////////
// Configuration fields.
Expand Down Expand Up @@ -1446,7 +1454,7 @@ public void setProfilingService( String service ) {
private ProfileOutput profileOutput = new ProfileOutput(System.err);
private String profilingService;

private ClassLoader thisLoader = RubyInstanceConfig.class.getClassLoader();
private ClassLoader thisLoader = setupLoader();
// thisLoader can be null for example when jruby comes from the boot-classLoader
private ClassLoader loader = thisLoader == null ? Thread.currentThread().getContextClassLoader() : thisLoader;

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/javasupport/Java.java
Expand Up @@ -37,7 +37,7 @@
import org.jruby.java.util.SystemPropertiesMap;
import org.jruby.java.proxies.JavaInterfaceTemplate;
import org.jruby.java.addons.KernelJavaAddons;
import java.io.IOException;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down

0 comments on commit b158469

Please sign in to comment.