Skip to content
Permalink
Browse files
Merge branch 'master' into truffle-head
  • Loading branch information
chrisseaton committed Nov 2, 2014
2 parents 6b14aa4 + 8dc5296 commit 0e6ced5182928c11b1890816173280e4c05ad2ec
Showing 3 changed files with 27 additions and 6 deletions.
@@ -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;
}
@@ -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) {
@@ -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;
}

}
@@ -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;
@@ -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;
@@ -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.