Skip to content

Commit

Permalink
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 21 additions & 3 deletions core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
6 changes: 6 additions & 0 deletions core/src/main/java/org/jruby/truffle/runtime/RubyContext.java
Original file line number Diff line number Diff line change
@@ -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.