Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into ruby-2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Nov 29, 2016
2 parents ab2d094 + e56920c commit e8b8bb6
Show file tree
Hide file tree
Showing 1,069 changed files with 189,914 additions and 3,783 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Expand Up @@ -16,6 +16,13 @@
*.tokens
*.su
*.ll
*.bc
extconf.h
mkmf.log
Makefile
cext-output.txt
bench-results.json
bench-results-processed.json

.DS_Store
.debug.properties
Expand Down
3 changes: 2 additions & 1 deletion bin/jruby-truffle
@@ -1,5 +1,6 @@
#!/usr/bin/env bash

bin="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root="$(dirname "$bin")"

exec "$bin"/jruby -X+T "$@"
exec "$root"/tool/jt.rb ruby --no-print-cmd "$@"
1 change: 1 addition & 0 deletions bin/jruby.bash
Expand Up @@ -231,6 +231,7 @@ do
-*)
opt="${opt:1}=false" ;;
esac
echo "$1 is deprecated - use -J-Dgraal.$opt instead" >&2
java_args=("${java_args[@]}" "-Dgraal.$opt")
else
if [ "${val:0:3}" = "-ea" ]; then
Expand Down
2 changes: 1 addition & 1 deletion core/pom.rb
Expand Up @@ -50,7 +50,7 @@
jar 'com.github.jnr:jnr-x86asm:1.0.2', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-unixsocket:0.14', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-posix:3.0.32', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-constants:0.9.5', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-constants:0.9.6', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-ffi:2.1.1'
jar 'com.github.jnr:jffi:${jffi.version}'
jar 'com.github.jnr:jffi:${jffi.version}:native'
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Expand Up @@ -148,7 +148,7 @@ DO NOT MODIFIY - GENERATED CODE
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-constants</artifactId>
<version>0.9.5</version>
<version>0.9.6</version>
<exclusions>
<exclusion>
<artifactId>jnr-ffi</artifactId>
Expand Down
102 changes: 91 additions & 11 deletions core/src/main/java/org/jruby/Main.java
Expand Up @@ -60,6 +60,10 @@
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -219,7 +223,7 @@ public static void main(String[] args) {
catch (Throwable t) {
// If a Truffle exception gets this far it's a hard failure - don't try and dress it up as a Ruby exception

if (main.config.getCompileMode() == RubyInstanceConfig.CompileMode.TRUFFLE) {
if (main.isTruffle()) {
System.err.println("Truffle internal error: " + t);
t.printStackTrace(System.err);
} else {
Expand Down Expand Up @@ -273,18 +277,22 @@ private Status internalRun() {

Ruby _runtime;

if (DripMain.DRIP_RUNTIME != null) {
// use drip's runtime, reinitializing config
_runtime = DripMain.DRIP_RUNTIME;
_runtime.reinitialize(true);
if (isTruffle()) {
_runtime = null;
} else {
_runtime = Ruby.newInstance(config);
if (DripMain.DRIP_RUNTIME != null) {
// use drip's runtime, reinitializing config
_runtime = DripMain.DRIP_RUNTIME;
_runtime.reinitialize(true);
} else {
_runtime = Ruby.newInstance(config);
}
}

final Ruby runtime = _runtime;
final AtomicBoolean didTeardown = new AtomicBoolean();

if (config.isHardExit()) {
if (runtime != null && config.isHardExit()) {
// we're the command-line JRuby, and should set a shutdown hook for
// teardown.
Runtime.getRuntime().addShutdownHook(new Thread() {
Expand All @@ -297,7 +305,9 @@ public void run() {
}

try {
doSetContextClassLoader(runtime);
if (runtime != null) {
doSetContextClassLoader(runtime);
}

if (in == null) {
// no script to run, return success
Expand All @@ -307,13 +317,38 @@ public void run() {
throw new MainExitException(1, "jruby: no Ruby script found in input (LoadError)");
} else if (config.getShouldCheckSyntax()) {
// check syntax only and exit
return doCheckSyntax(runtime, in, filename);
if (isTruffle()) {
final TruffleRubyEngineInterface truffle = loadTruffle();

try {
final int exitCode = truffle.doCheckSyntax(in, filename);
return new Status(exitCode);
} finally {
truffle.dispose();
}
} else {
return doCheckSyntax(runtime, in, filename);
}
} else {
// proceed to run the script
return doRunFromMain(runtime, in, filename);
if (isTruffle()) {
final TruffleRubyEngineInterface truffle = loadTruffle();

printTruffleTimeMetric("before-run");

try {
final int exitCode = truffle.execute(filename);
return new Status(exitCode);
} finally {
printTruffleTimeMetric("after-run");
truffle.dispose();
}
} else {
return doRunFromMain(runtime, in, filename);
}
}
} finally {
if (didTeardown.compareAndSet(false, true)) {
if (runtime != null && didTeardown.compareAndSet(false, true)) {
runtime.tearDown();
}
}
Expand Down Expand Up @@ -576,6 +611,51 @@ else if ( ex instanceof JumpException.FlowControlException ) {
// TODO: should match MRI (>= 2.2.3) exit status - @see ruby/test_enum.rb#test_first
return 2;
}

private boolean isTruffle() {
return config.getCompileMode().isTruffle();
}

private TruffleRubyEngineInterface loadTruffle() {
Main.printTruffleTimeMetric("before-load-context");

String javaVersion = System.getProperty("java.version");
String[] parts = javaVersion.split("\\D+");
int firstPart = Integer.valueOf(parts[0]);
if (!(firstPart >= 9 || Integer.valueOf(parts[1]) >= 8)) {
System.err.println("JRuby+Truffle needs Java 8 to run (found " + javaVersion + ").");
System.exit(1);
}

final Class<?> truffleRubyEngineClass;

try {
truffleRubyEngineClass = Class.forName("org.jruby.truffle.RubyEngine");
} catch (Exception e) {
throw new RuntimeException("JRuby's Truffle backend not available - either it was not compiled because JRuby was built with Java 7, or it has been removed", e);
}

final TruffleRubyEngineInterface truffleEngine;

try {
final Object truffleEngineInstance = truffleRubyEngineClass.getConstructor(RubyInstanceConfig.class).newInstance(config);

truffleEngine = (TruffleRubyEngineInterface) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{TruffleRubyEngineInterface.class}, new InvocationHandler() {

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return truffleRubyEngineClass.getMethod(method.getName(), method.getParameterTypes()).invoke(truffleEngineInstance, args);
}

});
} catch (Exception e) {
throw new RuntimeException("Error while calling the constructor of Truffle's RubyContext", e);
}

Main.printTruffleTimeMetric("after-load-context");

return truffleEngine;
}

public static void printTruffleTimeMetric(String id) {
if (Options.TRUFFLE_METRICS_TIME.load()) {
Expand Down
114 changes: 19 additions & 95 deletions core/src/main/java/org/jruby/Ruby.java
Expand Up @@ -240,6 +240,10 @@ public final class Ruby implements Constantizable {
* @see org.jruby.RubyInstanceConfig
*/
private Ruby(RubyInstanceConfig config) {
if (config.getCompileMode().isTruffle()) {
throw new UnsupportedOperationException("Truffle isn't supported using a classic context - use PolyglotEngine instead.");
}

this.config = config;
this.threadService = new ThreadService(this);

Expand Down Expand Up @@ -556,20 +560,6 @@ public void runFromMain(InputStream inputStream, String filename) {
return;
}

if (getInstanceConfig().getCompileMode() == CompileMode.TRUFFLE) {
final JRubyTruffleInterface truffleContext = getTruffleContext();
Main.printTruffleTimeMetric("before-run");
int exitCode;
try {
exitCode = truffleContext.execute(filename);
} finally {
Main.printTruffleTimeMetric("after-run");
shutdownTruffleContextIfRunning();
}

throw new MainExitException(exitCode);
}

ParseResult parseResult = parseFromMain(filename, inputStream);

// if no DATA, we're done with the stream, shut it down
Expand Down Expand Up @@ -841,10 +831,6 @@ public IRubyObject runScript(Script script) {
}

public IRubyObject runScript(Script script, boolean wrap) {
if (getInstanceConfig().getCompileMode() == CompileMode.TRUFFLE) {
throw new UnsupportedOperationException();
}

return script.load(getCurrentContext(), getTopSelf(), wrap);
}

Expand All @@ -857,10 +843,6 @@ public IRubyObject runScriptBody(Script script) {
}

public IRubyObject runInterpreter(ThreadContext context, ParseResult parseResult, IRubyObject self) {
if (getInstanceConfig().getCompileMode() == CompileMode.TRUFFLE) {
throw new UnsupportedOperationException();
}

return interpreter.execute(this, parseResult, self);
}

Expand Down Expand Up @@ -900,56 +882,6 @@ public JITCompiler getJITCompiler() {
return jitCompiler;
}

public JRubyTruffleInterface getTruffleContext() {
synchronized (truffleContextMonitor) {
if (truffleContext == null) {
truffleContext = loadTruffle();
}
return truffleContext;
}
}

private JRubyTruffleInterface loadTruffle() {
Main.printTruffleTimeMetric("before-load-context");

String javaVersion = System.getProperty("java.version");
String[] parts = javaVersion.split("\\D+");
int firstPart = Integer.valueOf(parts[0]);
if (!(firstPart >= 9 || Integer.valueOf(parts[1]) >= 8)) {
System.err.println("JRuby+Truffle needs Java 8 to run (found " + javaVersion + ").");
System.exit(1);
}

final Class<?> clazz;

try {
clazz = getJRubyClassLoader().loadClass("org.jruby.truffle.JRubyTruffleImpl");
} catch (Exception e) {
throw new RuntimeException("JRuby's Truffle backend not available - either it was not compiled because JRuby was built with Java 7, or it has been removed", e);
}

final JRubyTruffleInterface truffleContext;

try {
Constructor<?> con = clazz.getConstructor(RubyInstanceConfig.class);
truffleContext = (JRubyTruffleInterface) con.newInstance(config);
} catch (Exception e) {
throw new RuntimeException("Error while calling the constructor of Truffle's RubyContext", e);
}

Main.printTruffleTimeMetric("after-load-context");

return truffleContext;
}

public void shutdownTruffleContextIfRunning() {
synchronized (truffleContextMonitor) {
if (truffleContext != null) {
truffleContext.dispose();
}
}
}

/**
* @deprecated use #newInstance()
*/
Expand Down Expand Up @@ -1278,8 +1210,7 @@ private void init() {
// if we can't use reflection, 'jruby' and 'java' won't work; no load.
boolean reflectionWorks = doesReflectionWork();

if (!RubyInstanceConfig.DEBUG_PARSER && reflectionWorks
&& getInstanceConfig().getCompileMode() != CompileMode.TRUFFLE) {
if (!RubyInstanceConfig.DEBUG_PARSER && reflectionWorks) {
loadService.require("jruby");
}

Expand All @@ -1288,23 +1219,21 @@ && getInstanceConfig().getCompileMode() != CompileMode.TRUFFLE) {
// out of base boot mode
bootingCore = false;

if (getInstanceConfig().getCompileMode() != CompileMode.TRUFFLE) {
// init Ruby-based kernel
initRubyKernel();
// init Ruby-based kernel
initRubyKernel();

// Define blank modules for feature detection in preludes
if (!config.isDisableGems()) {
defineModule("Gem");
}
if (!config.isDisableDidYouMean()) {
defineModule("DidYouMean");
}
// Define blank modules for feature detection in preludes
if (!config.isDisableGems()) {
defineModule("Gem");
}
if (!config.isDisableDidYouMean()) {
defineModule("DidYouMean");
}

initRubyPreludes();
initRubyPreludes();

// everything booted, so SizedQueue should be available; set up root fiber
ThreadFiber.initRootFiber(context);
}
// everything booted, so SizedQueue should be available; set up root fiber
ThreadFiber.initRootFiber(context);

if(config.isProfiling()) {
// additional twiddling for profiled mode
Expand All @@ -1326,10 +1255,8 @@ && getInstanceConfig().getCompileMode() != CompileMode.TRUFFLE) {
bootingRuntime = false;

// Require in all libraries specified on command line
if (getInstanceConfig().getCompileMode() != CompileMode.TRUFFLE) {
for (String scriptName : config.getRequiredLibraries()) {
topSelf.callMethod(context, "require", RubyString.newString(this, scriptName));
}
for (String scriptName : config.getRequiredLibraries()) {
topSelf.callMethod(context, "require", RubyString.newString(this, scriptName));
}
}

Expand Down Expand Up @@ -5174,9 +5101,6 @@ public IRubyObject call(ThreadContext context, RecursiveFunction func, IRubyObje
// Compilation
private final JITCompiler jitCompiler;

private JRubyTruffleInterface truffleContext;
private final Object truffleContextMonitor = new Object();

// Note: this field and the following static initializer
// must be located be in this order!
private volatile static boolean securityRestricted = false;
Expand Down

0 comments on commit e8b8bb6

Please sign in to comment.