Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into truffle-head
Browse files Browse the repository at this point in the history
Conflicts:
	truffle/src/main/java/org/jruby/truffle/nodes/control/TraceNode.java
	truffle/src/main/java/org/jruby/truffle/nodes/core/ThreadBacktraceLocationNodes.java
	truffle/src/main/java/org/jruby/truffle/nodes/core/TimeNodes.java
	truffle/src/main/java/org/jruby/truffle/nodes/rubinius/TimePrimitiveNodes.java
nirvdrum committed Sep 28, 2015
2 parents 91befe3 + f733a03 commit 1ff0604
Showing 119 changed files with 1,334 additions and 682 deletions.
84 changes: 54 additions & 30 deletions core/src/main/java/org/jruby/RubyRandom.java
Original file line number Diff line number Diff line change
@@ -56,39 +56,55 @@ public static class RandomType {
RandomType(IRubyObject vseed) {
this.seed = vseed.convertToInteger();
if (seed instanceof RubyFixnum) {
long v = Math.abs(RubyNumeric.num2long(seed));
if (v == (v & 0xffffffffL)) {
this.mt = new Random((int) v);
} else {
int[] ints = new int[2];
ints[0] = (int) v;
ints[1] = (int) (v >> 32);
this.mt = new Random(ints);
}
this.mt = randomFromFixnum((RubyFixnum) seed);
} else if (seed instanceof RubyBignum) {
BigInteger big = ((RubyBignum) seed).getBigIntegerValue();
if (big.signum() < 0) {
big = big.abs();
}
byte[] buf = big.toByteArray();
int buflen = buf.length;
if (buf[0] == 0) {
buflen -= 1;
}
int len = Math.min((buflen + 3) / 4, Random.N);
int[] ints = bigEndianToInts(buf, len);
if (len <= 1) {
this.mt = new Random(ints[0]);
} else {
this.mt = new Random(ints);
}
this.mt = randomFromBignum((RubyBignum) seed);
} else {
throw vseed.getRuntime().newTypeError(
String.format("failed to convert %s into Integer", vseed.getMetaClass()
.getName()));
}
}

public static Random randomFromFixnum(RubyFixnum seed) {
return randomFromLong(RubyNumeric.num2long(seed));
}

public static Random randomFromLong(long seed) {
long v = Math.abs(seed);
if (v == (v & 0xffffffffL)) {
return new Random((int) v);
} else {
int[] ints = new int[2];
ints[0] = (int) v;
ints[1] = (int) (v >> 32);
return new Random(ints);
}
}

public static Random randomFromBignum(RubyBignum seed) {
BigInteger big = seed.getBigIntegerValue();
return randomFromBigInteger(big);
}

public static Random randomFromBigInteger(BigInteger big) {
if (big.signum() < 0) {
big = big.abs();
}
byte[] buf = big.toByteArray();
int buflen = buf.length;
if (buf[0] == 0) {
buflen -= 1;
}
int len = Math.min((buflen + 3) / 4, Random.N);
int[] ints = bigEndianToInts(buf, len);
if (len <= 1) {
return new Random(ints[0]);
} else {
return new Random(ints);
}
}

RandomType(IRubyObject vseed, RubyBignum state, int left) {
this.seed = vseed.convertToInteger();
byte[] bytes = state.getBigIntegerValue().toByteArray();
@@ -152,7 +168,7 @@ int getLeft() {
}

// big endian of bytes to reversed ints
private int[] bigEndianToInts(byte[] buf, int initKeyLen) {
private static int[] bigEndianToInts(byte[] buf, int initKeyLen) {
int[] initKey = new int[initKeyLen];
for (int idx = 0; idx < initKey.length; ++idx) {
initKey[idx] = getIntBigIntegerBuffer(buf, idx);
@@ -197,11 +213,15 @@ static void setIntBigIntegerBuffer(byte[] dest, int loc, int value) {

private static final int DEFAULT_SEED_CNT = 4;

public static BigInteger randomSeedBigInteger(java.util.Random random) {
byte[] seed = new byte[DEFAULT_SEED_CNT * 4];
random.nextBytes(seed);
return new BigInteger(seed).abs();
}

// c: random_seed
public static RubyBignum randomSeed(Ruby runtime) {
byte[] seed = new byte[DEFAULT_SEED_CNT * 4];
runtime.getRandom().nextBytes(seed);
return RubyBignum.newBignum(runtime, (new BigInteger(seed)).abs());
return RubyBignum.newBignum(runtime, randomSeedBigInteger(runtime.getRandom()));
}

public static RubyClass createRandomClass(Ruby runtime) {
@@ -358,6 +378,10 @@ public static RubyFloat randFloat(ThreadContext context, RandomType random) {
// limited_rand gets/returns ulong but we do this in signed long only.
private static IRubyObject randLimitedFixnum(ThreadContext context, RandomType random,
long limit) {
return RubyFixnum.newFixnum(context.getRuntime(), randLimitedFixnumInner(random.mt, limit));
}

public static long randLimitedFixnumInner(Random random, long limit) {
long val;
if (limit == 0) {
val = 0;
@@ -378,7 +402,7 @@ private static IRubyObject randLimitedFixnum(ThreadContext context, RandomType r
break;
}
}
return context.runtime.newFixnum(val);
return val;
}

// c: limited_big_rand
74 changes: 20 additions & 54 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
@@ -1674,10 +1674,12 @@ public IRubyObject str_eql_p19(ThreadContext context, IRubyObject other) {
/** rb_str_upcase / rb_str_upcase_bang
*
*/
@Deprecated
public RubyString upcase(ThreadContext context) {
return upcase19(context);
}

@Deprecated
public IRubyObject upcase_bang(ThreadContext context) {
return upcase_bang19(context);
}
@@ -1713,47 +1715,30 @@ public IRubyObject upcase_bang19(ThreadContext context) {
}

private IRubyObject singleByteUpcase(Ruby runtime, byte[]bytes, int s, int end) {
boolean modify = false;
while (s < end) {
int c = bytes[s] & 0xff;
if (ASCII.isLower(c)) {
bytes[s] = AsciiTables.ToUpperCaseTable[c];
modify = true;
}
s++;
}
boolean modify = StringSupport.singleByteUpcase(bytes, s, end);

return modify ? this : runtime.getNil();
}

private IRubyObject multiByteUpcase(Ruby runtime, Encoding enc, byte[]bytes, int s, int end) {
boolean modify = false;
int c;
while (s < end) {
if (enc.isAsciiCompatible() && Encoding.isAscii(c = bytes[s] & 0xff)) {
if (ASCII.isLower(c)) {
bytes[s] = AsciiTables.ToUpperCaseTable[c];
modify = true;
}
s++;
} else {
c = codePoint(runtime, enc, bytes, s, end);
if (enc.isLower(c)) {
enc.codeToMbc(toUpper(enc, c), bytes, s);
modify = true;
}
s += codeLength(enc, c);
}
try {
boolean modify = StringSupport.multiByteUpcase(enc, bytes, s, end);

return modify ? this : runtime.getNil();
} catch (IllegalArgumentException e) {
throw runtime.newArgumentError(e.getMessage());
}
return modify ? this : runtime.getNil();
}

/** rb_str_downcase / rb_str_downcase_bang
*
*/
@Deprecated
public RubyString downcase(ThreadContext context) {
return downcase19(context);
}

@Deprecated
public IRubyObject downcase_bang(ThreadContext context) {
return downcase_bang19(context);
}
@@ -1789,38 +1774,19 @@ public IRubyObject downcase_bang19(ThreadContext context) {
}

private IRubyObject singleByteDowncase(Ruby runtime, byte[]bytes, int s, int end) {
boolean modify = false;
while (s < end) {
int c = bytes[s] & 0xff;
if (ASCII.isUpper(c)) {
bytes[s] = AsciiTables.ToLowerCaseTable[c];
modify = true;
}
s++;
}
boolean modify = StringSupport.singleByteDowncase(bytes, s, end);

return modify ? this : runtime.getNil();
}

private IRubyObject multiByteDowncase(Ruby runtime, Encoding enc, byte[]bytes, int s, int end) {
boolean modify = false;
int c;
while (s < end) {
if (enc.isAsciiCompatible() && Encoding.isAscii(c = bytes[s] & 0xff)) {
if (ASCII.isUpper(c)) {
bytes[s] = AsciiTables.ToLowerCaseTable[c];
modify = true;
}
s++;
} else {
c = codePoint(runtime, enc, bytes, s, end);
if (enc.isUpper(c)) {
enc.codeToMbc(toLower(enc, c), bytes, s);
modify = true;
}
s += codeLength(enc, c);
}
try {
boolean modify = StringSupport.multiByteDowncase(enc, bytes, s, end);

return modify ? this : runtime.getNil();
} catch (IllegalArgumentException e) {
throw runtime.newArgumentError(e.getMessage());
}
return modify ? this : runtime.getNil();
}


2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/BignumNode.java
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@
/**
* Represents a big integer literal.
*/
public class BignumNode extends NumericNode {
public class BignumNode extends NumericNode implements SideEffectFree {
private BigInteger value;

public BignumNode(ISourcePosition position, BigInteger value) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/ClassVarNode.java
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
/**
* Access to a class variable.
*/
public class ClassVarNode extends Node implements INameNode {
public class ClassVarNode extends Node implements INameNode, SideEffectFree {
private String name;

public ClassVarNode(ISourcePosition position, String name) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/ComplexNode.java
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
*
* @author enebo
*/
public class ComplexNode extends NumericNode {
public class ComplexNode extends NumericNode implements SideEffectFree {
private NumericNode y;

public ComplexNode(ISourcePosition position, NumericNode y) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/FalseNode.java
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
/**
* Represents a false literal.
*/
public class FalseNode extends Node implements INameNode {
public class FalseNode extends Node implements INameNode, SideEffectFree {
public FalseNode(ISourcePosition position) {
super(position, false);
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/FileNode.java
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@
/**
* Represents __FILE__ nodes
*/
public class FileNode extends StrNode {
public class FileNode extends StrNode implements SideEffectFree {
public FileNode(ISourcePosition position, ByteList value) {
super(position, value);
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/FixnumNode.java
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
/**
* Represents an integer literal.
*/
public class FixnumNode extends NumericNode implements ILiteralNode {
public class FixnumNode extends NumericNode implements ILiteralNode, SideEffectFree {
private long value;

public FixnumNode(ISourcePosition position, long value) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/FloatNode.java
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
/**
* Represents a float literal.
*/
public class FloatNode extends NumericNode implements ILiteralNode {
public class FloatNode extends NumericNode implements ILiteralNode, SideEffectFree {
private double value;

public FloatNode(ISourcePosition position, double value) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/GlobalVarNode.java
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
/**
* access to a global variable.
*/
public class GlobalVarNode extends Node implements INameNode {
public class GlobalVarNode extends Node implements INameNode, SideEffectFree {
private String name;

public GlobalVarNode(ISourcePosition position, String name) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/InstVarNode.java
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@
/**
* Represents an instance variable accessor.
*/
public class InstVarNode extends Node implements INameNode {
public class InstVarNode extends Node implements INameNode, SideEffectFree {
private String name;

public InstVarNode(ISourcePosition position, String name) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/LocalVarNode.java
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
/**
* Access a local variable
*/
public class LocalVarNode extends Node implements INameNode, IScopedNode {
public class LocalVarNode extends Node implements INameNode, IScopedNode, SideEffectFree {
// The name of the variable
private String name;

2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/NilNode.java
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
/**
* represents 'nil'
*/
public class NilNode extends Node implements INameNode {
public class NilNode extends Node implements INameNode, SideEffectFree {
public NilNode(ISourcePosition position) {
super(position, false);
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/RationalNode.java
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
*
* @author enebo
*/
public class RationalNode extends NumericNode {
public class RationalNode extends NumericNode implements SideEffectFree {
private final long numerator;
private final long denominator;

12 changes: 12 additions & 0 deletions core/src/main/java/org/jruby/ast/RescueModNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jruby.ast;

import org.jruby.lexer.yacc.ISourcePosition;

/**
* f rescue nil
*/
public class RescueModNode extends RescueNode {
public RescueModNode(ISourcePosition position, Node bodyNode, RescueBodyNode rescueNode) {
super(position, bodyNode, rescueNode, null /* else */);
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/SelfNode.java
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
/**
* Represents 'self' keyword
*/
public class SelfNode extends Node implements INameNode {
public class SelfNode extends Node implements INameNode, SideEffectFree {
public SelfNode(ISourcePosition position) {
super(position, false);
}
7 changes: 7 additions & 0 deletions core/src/main/java/org/jruby/ast/SideEffectFree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jruby.ast;

/**
* Created by enebo on 9/26/15.
*/
public interface SideEffectFree {
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/StrNode.java
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@
/**
* Representing a simple String literal.
*/
public class StrNode extends Node implements ILiteralNode {
public class StrNode extends Node implements ILiteralNode, SideEffectFree {
private final ByteList value;
private final int codeRange;

Loading

0 comments on commit 1ff0604

Please sign in to comment.