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: d30e1eb56963
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 87f45ab4853c
Choose a head ref
  • 8 commits
  • 3 files changed
  • 1 contributor

Commits on Apr 27, 2016

  1. Copy the full SHA
    a3dd720 View commit details
  2. Copy the full SHA
    5dbdad5 View commit details
  3. [Truffle] Dead code.

    chrisseaton committed Apr 27, 2016
    Copy the full SHA
    a816f74 View commit details
  4. Copy the full SHA
    a9a3fa6 View commit details
  5. Copy the full SHA
    7a0911c View commit details
  6. Copy the full SHA
    06c94c4 View commit details
  7. Copy the full SHA
    d73d035 View commit details
  8. Copy the full SHA
    87f45ab View commit details
Showing with 97 additions and 73 deletions.
  1. +78 −73 truffle/src/test/java/org/jruby/truffle/tck/RubyDebugTest.java
  2. +17 −0 truffle/src/test/ruby/factorial.rb
  3. +2 −0 truffle/src/test/ruby/init.rb
151 changes: 78 additions & 73 deletions truffle/src/test/java/org/jruby/truffle/tck/RubyDebugTest.java
Original file line number Diff line number Diff line change
@@ -9,19 +9,6 @@
*/
package org.jruby.truffle.tck;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.LinkedList;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.oracle.truffle.api.debug.Debugger;
import com.oracle.truffle.api.debug.ExecutionEvent;
import com.oracle.truffle.api.debug.SuspendedEvent;
@@ -33,97 +20,78 @@
import com.oracle.truffle.api.vm.EventConsumer;
import com.oracle.truffle.api.vm.PolyglotEngine;
import com.oracle.truffle.api.vm.PolyglotEngine.Value;
import java.util.Objects;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.RubyLanguage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Objects;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class RubyDebugTest {

private Debugger debugger;
private final LinkedList<Runnable> run = new LinkedList<>();
private SuspendedEvent suspendedEvent;
private Throwable ex;
private ExecutionEvent executionEvent;
protected PolyglotEngine engine;
protected final ByteArrayOutputStream out = new ByteArrayOutputStream();
protected final ByteArrayOutputStream err = new ByteArrayOutputStream();
private PolyglotEngine engine;
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private final ByteArrayOutputStream err = new ByteArrayOutputStream();

@Before
public void before() throws IOException {
suspendedEvent = null;
executionEvent = null;
engine = PolyglotEngine.newBuilder().setOut(out).setErr(err).onEvent(new EventConsumer<ExecutionEvent>(ExecutionEvent.class) {

engine = PolyglotEngine.newBuilder().setOut(out).setErr(err)
.onEvent(new EventConsumer<ExecutionEvent>(ExecutionEvent.class) {

@Override
protected void on(ExecutionEvent event) {
executionEvent = event;
debugger = executionEvent.getDebugger();
performWork();
executionEvent = null;
}

}).onEvent(new EventConsumer<SuspendedEvent>(SuspendedEvent.class) {

@Override
protected void on(SuspendedEvent event) {
suspendedEvent = event;
performWork();
suspendedEvent = null;
}

}).build();
engine.eval(Source.fromText(
"def nothing(n)\n" +
"end\n",
"init.rb").withMimeType("application/x-ruby")
);

engine.eval(Source.fromFileName("src/test/ruby/init.rb"));

run.clear();
}

@After
public void dispose() {
if (engine != null) {
// engine.dispose();
//engine.dispose();
}
}

private static Source createFactorial() {
return Source.fromText(
"def fac(n)\n" +
" if n <= 1\n" +
" 1\n" +
" else\n" +
" nMinusOne = n - 1\n" +
" nMOFact = fac(nMinusOne)\n" +
" res = n * nMOFact\n" +
" res\n" +
" end\n" +
"end\n" +
"\n" +
"def main\n" +
" res = fac(2)\n" +
" puts res\n" +
" res\n" +
"end\n" +
"Truffle::Interop.export_method(:main)\n" +
"\n",
"factorial.rb").withMimeType(
"application/x-ruby");
}

protected final String getOut() {
return new String(out.toByteArray());
}

protected final String getErr() {
try {
err.flush();
} catch (IOException e) {
}
return new String(err.toByteArray());
}

@Test
public void testBreakpoint() throws Throwable {
final Source factorial = createFactorial();

run.addLast(new Runnable() {

@Override
public void run() {
try {
@@ -136,24 +104,32 @@ public void run() {
throw new RuntimeException(e);
}
}

});

engine.eval(factorial);

assertExecutedOK("Algorithm loaded");

run.addLast(new Runnable() {

@Override
public void run() {
// the breakpoint should hit instead
//fail("the breakpoint should hit instead");
}

});

assertLocation(3, "1",
"n", 1,
"nMinusOne", null,
"nMOFact", null,
"res", null);

continueExecution();

final Value main = engine.findGlobalSymbol("main");
Assert.assertNotNull( "main method found", main);
assertNotNull( "main method found", main);
Value value = main.execute();
Number n = value.as(Number.class);
assertNotNull(n);
@@ -167,12 +143,14 @@ public void stepInStepOver() throws Throwable {
engine.eval(factorial);

run.addLast(new Runnable() {

@Override
public void run() {
assertNull(suspendedEvent);
assertNotNull(executionEvent);
executionEvent.prepareStepInto();
}

});

assertLocation(13, "res = fac(2)", "res", null);
@@ -205,17 +183,14 @@ public void run() {
+ " res", // wrong!?
"res", 2);

continueExecution();
// stepOver(1);
// assertLocation(15, "puts res", "res", 2);
// stepOut();
continueExecution();

Value value = engine.findGlobalSymbol("main").execute();

Number n = value.as(Number.class);

assertNotNull(n);
assertEquals("Factorial computed OK", 2, n.intValue());

assertExecutedOK("Stepping went OK");
}

@@ -232,78 +207,108 @@ private void performWork() {

private void stepOver(final int size) {
run.addLast(new Runnable() {

public void run() {
suspendedEvent.prepareStepOver(size);
}

});
}

private void stepOut() {
run.addLast(new Runnable() {

public void run() {
suspendedEvent.prepareStepOut();
}

});
}

private void continueExecution() {
run.addLast(new Runnable() {

public void run() {
suspendedEvent.prepareContinue();
}

});
}

private void stepInto(final int size) {
run.addLast(new Runnable() {

public void run() {
suspendedEvent.prepareStepInto(size);
}

});
}

private void assertLocation(final int line, final String code, final Object... expectedFrame) {
run.addLast(new Runnable() {

public void run() {
assertNotNull(suspendedEvent);
final int currentLine = suspendedEvent.getNode().getSourceSection().getLineLocation().getLineNumber();
Assert.assertEquals(line, currentLine);
assertEquals(line, currentLine);
final String currentCode = suspendedEvent.getNode().getSourceSection().getCode().trim();
Assert.assertEquals(code, currentCode);
assertEquals(code, currentCode);
final MaterializedFrame frame = suspendedEvent.getFrame();

Assert.assertEquals(expectedFrame.length / 2, frame.getFrameDescriptor().getSize());
assertEquals(expectedFrame.length / 2, frame.getFrameDescriptor().getSize());

for (int i = 0; i < expectedFrame.length; i = i + 2) {
String expectedIdentifier = (String) expectedFrame[i];
Object expectedValue = expectedFrame[i + 1];
FrameSlot slot = frame.getFrameDescriptor().findFrameSlot(expectedIdentifier);
Assert.assertNotNull(slot);
assertNotNull(slot);
Object value = frame.getValue(slot);

if (Objects.equals(expectedValue, value)) {
continue;
}

Node findContextNode = RubyLanguage.INSTANCE.unprotectedCreateFindContextNode();
RubyContext context = RubyLanguage.INSTANCE.unprotectedFindContext(findContextNode);

if (value == context.getCoreLibrary().getNilObject()) {
value = null;
}
Assert.assertEquals(expectedValue, value);

assertEquals(expectedValue, value);
}

run.removeFirst().run();
}

});
}

private void assertExecutedOK(String msg) throws Throwable {
Assert.assertTrue(getErr(), getErr().isEmpty());
assertTrue(getErr(), getErr().isEmpty());

if (ex != null) {
if (ex instanceof AssertionError) {
throw ex;
} else {
throw new AssertionError(msg + ". Error during execution ", ex);
}
}

assertTrue(msg + ". Assuming all requests processed: " + run, run.isEmpty());
}

private static Source createFactorial() throws IOException {
return Source.fromFileName("src/test/ruby/factorial.rb");
}

private final String getErr() {
try {
err.flush();
} catch (IOException e) {
}
return new String(err.toByteArray());
}

}
17 changes: 17 additions & 0 deletions truffle/src/test/ruby/factorial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def fac(n)
if n <= 1
1
else
nMinusOne = n - 1
nMOFact = fac(nMinusOne)
res = n * nMOFact
res
end
end

def main
res = fac(2)
puts res
res
end
Truffle::Interop.export_method(:main)
2 changes: 2 additions & 0 deletions truffle/src/test/ruby/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def nothing(n)
end