Skip to content

Commit

Permalink
Merge branch 'master' into truffle-head
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Nov 2, 2014
2 parents 6b14aa4 + 8dc5296 commit 0e6ced5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Expand Up @@ -1779,9 +1779,6 @@ public boolean includeObject(VirtualFrame frame, RubyArray array, Object value)
for (int n = 0; n < array.getSize(); n++) {
final Object stored = store[n];

// TODO(CS): cast node around the dispatch
notDesignedForCompilation();

if (equalNode.executeSameOrEqual(frame, stored, value)) {
return true;
}
Expand Down
24 changes: 21 additions & 3 deletions core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Expand Up @@ -1393,7 +1393,7 @@ public Object raise(VirtualFrame frame, RubyClass exceptionClass, RubyString mes

}

@CoreMethod(names = "rand", isModuleFunction = true)
@CoreMethod(names = "rand", isModuleFunction = true, optional = 1)
public abstract static class RandNode extends CoreMethodNode {

public RandNode(RubyContext context, SourceSection sourceSection) {
Expand All @@ -1405,8 +1405,26 @@ public RandNode(RandNode prev) {
}

@Specialization
public double rand() {
return Math.random();
public double rand(UndefinedPlaceholder undefined) {
return getContext().getRandom().nextDouble();
}

@Specialization(guards = "isZero")
public double randZero(int max) {
return getContext().getRandom().nextDouble();
}

@Specialization(guards = "isNonZero")
public int randNonZero(int max) {
return getContext().getRandom().nextInt(max);
}

protected boolean isZero(int max) {
return max == 0;
}

protected boolean isNonZero(int max) {
return max != 0;
}

}
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/org/jruby/truffle/runtime/RubyContext.java
Expand Up @@ -14,6 +14,7 @@
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.atomic.*;

import com.oracle.truffle.api.instrument.SourceCallback;
Expand Down Expand Up @@ -60,6 +61,7 @@ public class RubyContext extends ExecutionContext {
private final RubySymbol.SymbolTable symbolTable = new RubySymbol.SymbolTable(this);
private final Warnings warnings;
private final SafepointManager safepointManager;
private final Random random = new Random();

// TODO: Wrong place for this, only during parsing but be practical for now
private LexicalScope lexicalScope;
Expand Down Expand Up @@ -390,4 +392,8 @@ public Queue<Object> getThrowTags() {
public SafepointManager getSafepointManager() {
return safepointManager;
}

public Random getRandom() {
return random;
}
}

0 comments on commit 0e6ced5

Please sign in to comment.