Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9a4dcb28bec5
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9d90efa25449
Choose a head ref
  • 4 commits
  • 1 file changed
  • 2 contributors

Commits on Jun 18, 2015

  1. Copy the full SHA
    c61096f View commit details
  2. Fix formatting issues

    Change tabs to spaces and capitalize comments.
    bbelleville committed Jun 18, 2015
    Copy the full SHA
    8d66179 View commit details
  3. Add license header.

    bbelleville committed Jun 18, 2015
    Copy the full SHA
    40a0206 View commit details
  4. Merge pull request #3066 from bbelleville/truffle-main

    [Truffle] Create Truffle specific main function.
    chrisseaton committed Jun 18, 2015
    Copy the full SHA
    9d90efa View commit details
Showing with 105 additions and 0 deletions.
  1. +105 −0 truffle/src/main/java/org/jruby/truffle/Main.java
105 changes: 105 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.ast.RootNode;
import org.jruby.internal.runtime.GlobalVariable;
import org.jruby.internal.runtime.ValueAccessor;
import org.jruby.runtime.IAccessor;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

public class Main {

public static void main(String[] args) {
final RubyInstanceConfig config = new RubyInstanceConfig();
config.setHardExit(true);
config.processArguments(args);

InputStream in = config.getScriptSource();
String filename = config.displayedFileName();

final Ruby runtime = Ruby.newInstance(config);
final AtomicBoolean didTeardown = new AtomicBoolean();

config.setCompileMode(RubyInstanceConfig.CompileMode.TRUFFLE);

if (config.isHardExit()) {
// We're the command-line JRuby, and should set a shutdown hook for
// teardown.
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
if (didTeardown.compareAndSet(false, true)) {
runtime.tearDown();
}
}
});
}

if (in == null) {
return;
} else {

// Global variables
IAccessor d = new ValueAccessor(runtime.newString(filename));
runtime.getGlobalVariables().define("$PROGRAM_NAME", d,
GlobalVariable.Scope.GLOBAL);
runtime.getGlobalVariables().define("$0", d,
GlobalVariable.Scope.GLOBAL);

for (Iterator i = config.getOptionGlobals().entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
Object value = entry.getValue();
IRubyObject varvalue;
if (value != null) {
varvalue = runtime.newString(value.toString());
} else {
varvalue = runtime.getTrue();
}
runtime.getGlobalVariables().set(
"$" + entry.getKey().toString(), varvalue);
}

RootNode scriptNode = (RootNode) runtime
.parseFromMain(filename, in);

// If no DATA, we're done with the stream, shut it down
if (runtime.fetchGlobalConstant("DATA") == null) {
try {
in.close();
} catch (IOException ioe) {
}
}
ThreadContext context = runtime.getCurrentContext();

String oldFile = context.getFile();
int oldLine = context.getLine();

try {
context.setFileAndLine(scriptNode.getPosition());
runtime.getTruffleContext().execute(scriptNode);
} finally {
context.setFileAndLine(oldFile, oldLine);
runtime.shutdownTruffleContextIfRunning();
}

}

}

}