Skip to content

Commit

Permalink
[Truffle] Thread#raise.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Nov 9, 2014
1 parent 0e93ab9 commit 928acd1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions core/src/main/java/org/jruby/truffle/nodes/core/ThreadNodes.java
Expand Up @@ -9,9 +9,11 @@
*/
package org.jruby.truffle.nodes.core;

import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.*;
import com.oracle.truffle.api.dsl.*;
import org.jruby.RubyThread.Status;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.control.ThreadExitException;
Expand Down Expand Up @@ -195,6 +197,48 @@ public RubyNilClass pass() {

}

@CoreMethod(names = "raise", required = 1, optional = 1)
public abstract static class RaiseNode extends CoreMethodNode {

@Child protected DispatchHeadNode initialize;

public RaiseNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
initialize = new DispatchHeadNode(context);
}

public RaiseNode(RaiseNode prev) {
super(prev);
initialize = prev.initialize;
}

@Specialization
public RubyNilClass raise(VirtualFrame frame, RubyThread thread, RubyString message, UndefinedPlaceholder undefined) {
return raise(frame, thread, getContext().getCoreLibrary().getRuntimeErrorClass(), message);
}

@Specialization
public RubyNilClass raise(VirtualFrame frame, final RubyThread thread, RubyClass exceptionClass, RubyString message) {
final RubyBasicObject exception = exceptionClass.newInstance(this);
initialize.call(frame, exception, "initialize", null, message);
final RaiseException exceptionWrapper = new RaiseException(exception);

getContext().getSafepointManager().pauseAllThreadsAndExecute(new Consumer<Boolean>() {

@Override
public void accept(Boolean isPausingThread) {
if (getContext().getThreadManager().getCurrentThread() == thread) {
throw exceptionWrapper;
}
}

});

return getContext().getCoreLibrary().getNilObject();
}

}

@CoreMethod(names = "status")
public abstract static class StatusNode extends CoreMethodNode {

Expand Down
Expand Up @@ -77,7 +77,7 @@ public void poll() {
}

public void pauseAllThreadsAndExecute(final Consumer<Boolean> action) {
CompilerAsserts.neverPartOfCompilation();
CompilerDirectives.transferToInterpreter();

try {
lock.lock();
Expand Down

0 comments on commit 928acd1

Please sign in to comment.