Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Truffle] All #throw specs passing.
  • Loading branch information
chrisseaton committed Oct 9, 2014
1 parent c72837d commit 48601fd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 53 deletions.
43 changes: 18 additions & 25 deletions core/src/main/java/org/jruby/truffle/TruffleBridgeImpl.java
Expand Up @@ -95,37 +95,30 @@ public Object execute(final TranslatorDriver.ParserContext parserContext, final

@Override
public Object get() {
return truffleContext.handlingTopLevelThrow(new Supplier<Object>() {
final String inputFile = rootNode.getPosition().getFile();

@Override
public Object get() {
final String inputFile = rootNode.getPosition().getFile();
final Source source;

final Source source;
if (inputFile.equals("-e")) {
// Assume UTF-8 for the moment
source = Source.fromBytes(runtime.getInstanceConfig().inlineScript(), "-e", new BytesDecoder.UTF8BytesDecoder());
} else {
final byte[] bytes;

if (inputFile.equals("-e")) {
// Assume UTF-8 for the moment
source = Source.fromBytes(runtime.getInstanceConfig().inlineScript(), "-e", new BytesDecoder.UTF8BytesDecoder());
} else {
final byte[] bytes;

try {
bytes = Files.readAllBytes(Paths.get(inputFile));
} catch (IOException e) {
throw new RuntimeException(e);
}

// Assume UTF-8 for the moment
try {
bytes = Files.readAllBytes(Paths.get(inputFile));
} catch (IOException e) {
throw new RuntimeException(e);
}

source = Source.fromBytes(bytes, inputFile, new BytesDecoder.UTF8BytesDecoder());
}
// Assume UTF-8 for the moment

final RubyRootNode parsedRootNode = truffleContext.getTranslator().parse(truffleContext, source, parserContext, parentFrame, null);
final CallTarget callTarget = Truffle.getRuntime().createCallTarget(parsedRootNode);
return callTarget.call(RubyArguments.pack(null, parentFrame, self, null, new Object[]{}));
}
source = Source.fromBytes(bytes, inputFile, new BytesDecoder.UTF8BytesDecoder());
}

});
final RubyRootNode parsedRootNode = truffleContext.getTranslator().parse(truffleContext, source, parserContext, parentFrame, null);
final CallTarget callTarget = Truffle.getRuntime().createCallTarget(parsedRootNode);
return callTarget.call(RubyArguments.pack(null, parentFrame, self, null, new Object[]{}));
}

}, NilPlaceholder.INSTANCE);
Expand Down
15 changes: 13 additions & 2 deletions core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Expand Up @@ -392,6 +392,8 @@ public Object doCatch(VirtualFrame frame, Object tag, RubyProc block) {
notDesignedForCompilation();

try {
getContext().getThrowTags().add(tag);

return yield(frame, block);
} catch (ThrowException e) {
if (e.getTag().equals(tag)) {
Expand All @@ -401,6 +403,8 @@ public Object doCatch(VirtualFrame frame, Object tag, RubyProc block) {
} else {
throw e;
}
} finally {
getContext().getThrowTags().remove();
}
}
}
Expand Down Expand Up @@ -1791,10 +1795,17 @@ public Object doThrow(Object tag, UndefinedPlaceholder value) {
public Object doThrow(Object tag, Object value) {
notDesignedForCompilation();

if (!getContext().getThrowTags().contains(tag)) {
throw new RaiseException(new RubyException(
getContext().getCoreLibrary().getArgumentErrorClass(),
getContext().makeString(String.format("uncaught throw \"%s\"", tag)),
RubyCallStack.getBacktrace(this)));
}

if (value instanceof UndefinedPlaceholder) {
throw new ThrowException(tag, NilPlaceholder.INSTANCE, RubyCallStack.getBacktrace(this));
throw new ThrowException(tag, NilPlaceholder.INSTANCE);
} else {
throw new ThrowException(tag, value, RubyCallStack.getBacktrace(this));
throw new ThrowException(tag, value);
}
}

Expand Down
23 changes: 15 additions & 8 deletions core/src/main/java/org/jruby/truffle/runtime/RubyContext.java
Expand Up @@ -14,7 +14,9 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.concurrent.atomic.*;

import com.oracle.truffle.api.instrument.SourceCallback;
Expand Down Expand Up @@ -78,6 +80,15 @@ public class RubyContext extends ExecutionContext {

private final AtomicLong nextObjectID = new AtomicLong(0);

private final ThreadLocal<Queue<Object>> throwTags = new ThreadLocal<Queue<Object>>() {

@Override
protected Queue<Object> initialValue() {
return new ArrayDeque<>();
}

};

public RubyContext(Ruby runtime) {
assert runtime != null;

Expand Down Expand Up @@ -378,14 +389,6 @@ public Warnings getWarnings() {
return warnings;
}

public <T> T handlingTopLevelThrow(Supplier<T> run) {
try {
return run.get();
} catch (ThrowException e) {
throw new RaiseException(new RubyException(coreLibrary.getArgumentErrorClass(), makeString(String.format("uncaught throw \"%s\"", e.getTag())), e.getBacktrace()));
}
}

public <T> T handlingTopLevelRaise(Supplier<T> run, T defaultValue) {
try {
return run.get();
Expand All @@ -401,4 +404,8 @@ public <T> T handlingTopLevelRaise(Supplier<T> run, T defaultValue) {
}
}

public Queue<Object> getThrowTags() {
return throwTags.get();
}

}
Expand Up @@ -21,15 +21,13 @@ public class ThrowException extends ControlFlowException {

private final Object tag;
private final Object value;
private final Backtrace backtrace;

public ThrowException(Object tag, Object value, Backtrace backtrace) {
public ThrowException(Object tag, Object value) {
assert tag != null;
assert RubyContext.shouldObjectBeVisible(value);

this.tag = tag;
this.value = value;
this.backtrace = backtrace;
}

public Object getTag() {
Expand All @@ -40,10 +38,6 @@ public Object getValue() {
return value;
}

public Backtrace getBacktrace() {
return backtrace;
}

private static final long serialVersionUID = 8693305627979840677L;

}
Expand Up @@ -80,15 +80,7 @@ public void run() {
finalThread.manager.enterGlobalLock(finalThread);

try {
getContext().handlingTopLevelThrow(new Supplier<Void>() {

@Override
public Void get() {
finalRunnable.run();
return null;
}

});
finalRunnable.run();
} catch (RaiseException e) {
exception = (RubyException) e.getRubyException();
} catch (ReturnException e) {
Expand Down
2 changes: 0 additions & 2 deletions spec/truffle/tags/language/throw_tags.txt

This file was deleted.

0 comments on commit 48601fd

Please sign in to comment.