Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d344f52cd461
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1b7c3308ba68
Choose a head ref
  • 7 commits
  • 10 files changed
  • 1 contributor

Commits on Mar 27, 2018

  1. Verified

    This commit was signed with the committer’s verified signature.
    headius Charles Oliver Nutter
    Copy the full SHA
    2e850a5 View commit details
  2. Copy the full SHA
    9802921 View commit details
  3. Rogue copy got left behind.

    headius committed Mar 27, 2018
    Copy the full SHA
    b4b381b View commit details
  4. New Integer#sqrt. #4876

    headius committed Mar 27, 2018
    Copy the full SHA
    81a2507 View commit details
  5. Copy the full SHA
    75c4db9 View commit details
  6. Copy the full SHA
    177a51d View commit details
  7. Copy the full SHA
    1b7c330 View commit details
18 changes: 14 additions & 4 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -2820,16 +2820,26 @@ public void setDefaultFilesystemEncoding(Encoding defaultFilesystemEncoding) {
* Get the default java.nio.charset.Charset for the current default internal encoding.
*/
public Charset getDefaultCharset() {
Encoding enc = getDefaultInternalEncoding();
if (enc == null) {
enc = UTF8Encoding.INSTANCE;
}
Encoding enc = getDefaultEncoding();

Charset charset = EncodingUtils.charsetForEncoding(enc);

return charset;
}

/**
* Return the default internal encoding, if set, or UTF-8 by default.
*
* @return the default encoding used for new Ruby strings
*/
public Encoding getDefaultEncoding() {
Encoding enc = getDefaultInternalEncoding();
if (enc == null) {
enc = UTF8Encoding.INSTANCE;
}
return enc;
}

public EncodingService getEncodingService() {
return encodingService;
}
33 changes: 33 additions & 0 deletions core/src/main/java/org/jruby/RubyBignum.java
Original file line number Diff line number Diff line change
@@ -1203,6 +1203,39 @@ public RubyRational convertToRational() {
return RubyRational.newRationalRaw(runtime, this, RubyFixnum.one(runtime));
}

// MRI: rb_int_s_isqrt, Fixnum portion
@Override
public IRubyObject sqrt(ThreadContext context) {
Ruby runtime = context.runtime;

// TODO: this not exactly the same as MRI

if (isNegative()) {
throw runtime.newMathDomainError("Numerical argument is out of domain - isqrt");
}
return bignorm(runtime, sqrt(value));
}

/**
* A simple public-domain iterative BigInteger sqrt.
*
* @param x
* @return
*/
public static BigInteger sqrt(BigInteger x) {
BigInteger div = BigInteger.ZERO.setBit(x.bitLength()/2);
BigInteger div2 = div;
// Loop until we hit the same value twice in a row, or wind
// up alternating.
for(;;) {
BigInteger y = div.add(x.divide(div)).shiftRight(1);
if (y.equals(div) || y.equals(div2))
return y;
div2 = div;
div = y;
}
}

private static JavaSites.BignumSites sites(ThreadContext context) {
return context.sites.Bignum;
}
75 changes: 55 additions & 20 deletions core/src/main/java/org/jruby/RubyDir.java
Original file line number Diff line number Diff line change
@@ -266,35 +266,57 @@ public RubyArray entries() {
return RubyArray.newArrayMayCopy(getRuntime(), JavaUtil.convertJavaArrayToRuby(getRuntime(), snapshot));
}

/**
* Returns an array containing all of the filenames in the given directory.
*/
public static RubyArray entries(IRubyObject recv, IRubyObject path) {
return entries19(recv.getRuntime().getCurrentContext(), recv, path);
return entries(recv.getRuntime().getCurrentContext(), recv, path);
}

public static RubyArray entries(IRubyObject recv, IRubyObject path, IRubyObject arg, IRubyObject opts) {
return entries(recv.getRuntime().getCurrentContext(), recv, path, opts);
}

/**
* Returns an array containing all of the filenames in the given directory.
*/
@JRubyMethod(name = "entries", meta = true)
public static RubyArray entries19(ThreadContext context, IRubyObject recv, IRubyObject arg) {
RubyString path = StringSupport.checkEmbeddedNulls(context.runtime, RubyFile.get_path(context, arg));
return entriesCommon(context, path.asJavaString());
public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyObject arg) {
Ruby runtime = context.runtime;

RubyString path = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, arg));

return entriesCommon(context, path.asJavaString(), runtime.getDefaultEncoding());
}

@JRubyMethod(name = "entries", meta = true)
public static RubyArray entries19(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject opts) {
RubyString path = StringSupport.checkEmbeddedNulls(context.runtime, RubyFile.get_path(context, arg));
// FIXME: do something with opts
return entriesCommon(context, path.asJavaString());
public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject opts) {
Ruby runtime = context.runtime;
Encoding encoding = runtime.getDefaultInternalEncoding();

RubyString path = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, arg));
IRubyObject maybeHash = ArgsUtil.getOptionsArg(runtime, opts);

if (!maybeHash.isNil()) {
IRubyObject maybeEncoding = ArgsUtil.extractKeywordArg(context, "encoding", maybeHash);
if (!maybeEncoding.isNil()) {
encoding = runtime.getEncodingService().getEncodingFromObject(maybeEncoding);
}
}
return entriesCommon(context, path.asJavaString(), encoding);
}

private static RubyArray entriesCommon(ThreadContext context, String path) {
private static RubyArray entriesCommon(ThreadContext context, String path, Encoding encoding) {
Ruby runtime = context.runtime;
String adjustedPath = RubyFile.adjustRootPathOnWindows(runtime, path, null);
checkDirIsTwoSlashesOnWindows(runtime, adjustedPath);

FileResource directory = JRubyFile.createResource(context, path);
String[] files = getEntries(context, directory, adjustedPath);

return RubyArray.newArrayMayCopy(runtime, JavaUtil.convertJavaArrayToRuby(runtime, files));
return RubyArray.newArrayMayCopy(runtime, JavaUtil.convertJavaArrayToRuby(runtime, files, new JavaUtil.StringConverter() {
public IRubyObject convert(Ruby runtime, Object object) {
if (object == null) return runtime.getNil();
return RubyString.newString(runtime, (String)object, encoding);
}
}));
}

private static final String[] NO_FILES = StringSupport.EMPTY_STRING_ARRAY;
@@ -367,15 +389,18 @@ public static IRubyObject chroot(IRubyObject recv, IRubyObject path) {
*/
@JRubyMethod(name = "children", meta = true)
public static RubyArray children(ThreadContext context, IRubyObject recv, IRubyObject arg) {
RubyArray entries = entries19(context, recv, arg);
entries.removeIf(f -> f.equals(".") || f.equals(".."));
return entries;
return childrenCommon(context, recv, arg, context.nil);
}

@JRubyMethod(name = "children", meta = true)
public static RubyArray children(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject encoding) {
// TODO: encoding unused due to lack of encoding support in entries
return children(context, recv, arg);
public static RubyArray children(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject opts) {
return childrenCommon(context, recv, arg, opts);
}

private static RubyArray childrenCommon(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject opts) {
RubyArray entries = entries(context, recv, arg, opts);
entries.removeIf(f -> f.equals(".") || f.equals(".."));
return entries;
}

/**
@@ -738,7 +763,7 @@ public static IRubyObject empty_p(ThreadContext context, IRubyObject recv, IRuby
RubyString path = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, arg));
RubyFileStat fileStat = runtime.newFileStat(path.asJavaString(), false);
boolean isDirectory = fileStat.directory_p().isTrue();
return runtime.newBoolean(isDirectory && entries19(context, recv, arg).getLength() <= 2);
return runtime.newBoolean(isDirectory && entries(context, recv, arg).getLength() <= 2);
}

@JRubyMethod(name = "exist?", meta = true)
@@ -955,4 +980,14 @@ static RubyString getHomeDirectoryPath(ThreadContext context, IRubyObject home)
return (RubyString) home;
}

@Deprecated
public static RubyArray entries19(ThreadContext context, IRubyObject recv, IRubyObject arg) {
return entries(context, recv, arg);
}

@Deprecated
public static RubyArray entries19(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject opts) {
return entries(context, recv, arg, opts);
}

}
5 changes: 5 additions & 0 deletions core/src/main/java/org/jruby/RubyException.java
Original file line number Diff line number Diff line change
@@ -211,6 +211,11 @@ public IRubyObject message(ThreadContext context) {
return callMethod(context, "to_s");
}

@JRubyMethod(name = "full_message")
public IRubyObject full_message(ThreadContext context) {
return RubyString.newString(context.runtime, TraceType.Format.MRI.printBacktrace(this, false));
}

/** inspects an object and return a kind of debug information
*
*@return A RubyString containing the debug information.
35 changes: 15 additions & 20 deletions core/src/main/java/org/jruby/RubyFixnum.java
Original file line number Diff line number Diff line change
@@ -934,26 +934,6 @@ protected IRubyObject intPowTmp2(ThreadContext context, IRubyObject y, long mm,
return runtime.newFixnum(tmp);
}

protected IRubyObject intPowTmp3(ThreadContext context, RubyInteger y, RubyBignum m, boolean negaFlg) {
Ruby runtime = context.runtime;

BigInteger xn, yn, mn, zn;

xn = BigInteger.valueOf(this.getLongValue());
if (y instanceof RubyFixnum) {
yn = BigInteger.valueOf(y.getLongValue());
} else {
yn = y.getBigIntegerValue();
}
mn = m.getBigIntegerValue();

zn = xn.modPow(yn, mn);
if (negaFlg & zn.signum() == 1) {
zn = zn.negate();
}
return RubyBignum.bignorm(runtime, zn);
}

// MRI: MUL_MODULO macro defined within int_pow_tmp2 in numeric.c
private static RubyFixnum mulModulo(ThreadContext context, RubyFixnum a, RubyFixnum b, IRubyObject c) {
return (RubyFixnum) ((RubyInteger) a.op_mul(context, b.getLongValue())).modulo(context, c);
@@ -1476,6 +1456,21 @@ public IRubyObject remainder(ThreadContext context, IRubyObject y) {
return numRemainder(context, y);
}

// MRI: rb_int_s_isqrt, Fixnum portion
@Override
public IRubyObject sqrt(ThreadContext context) {
Ruby runtime = context.runtime;

// TODO: this not exactly the same as MRI

if (isNegative()) {
throw runtime.newMathDomainError("Numerical argument is out of domain - isqrt");
}
long n = value;
long sq = (long) Math.sqrt(n);
return RubyFixnum.newFixnum(runtime, sq);
}

private static JavaSites.FixnumSites sites(ThreadContext context) {
return context.sites.Fixnum;
}
34 changes: 29 additions & 5 deletions core/src/main/java/org/jruby/RubyHash.java
Original file line number Diff line number Diff line change
@@ -62,6 +62,7 @@
import java.io.IOException;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
@@ -1459,7 +1460,27 @@ public IRubyObject each_key(final ThreadContext context, final Block block) {

@JRubyMethod(name = "transform_keys")
public IRubyObject transform_keys(final ThreadContext context, final Block block) {
return ((RubyHash)dup()).transform_keys_bang(context, block);
if (block.isGiven()) {
RubyHash result = newHash(context.runtime);
visitAll(context, new TransformKeysVisitor(block), result);
return result;
}

return enumeratorizeWithSize(context, this, "transform_keys", enumSizeFn());
}

private static class TransformKeysVisitor extends VisitorWithState<RubyHash> {
private final Block block;

public TransformKeysVisitor(Block block) {
this.block = block;
}

@Override
public void visit(ThreadContext context, RubyHash self, IRubyObject key, IRubyObject value, int index, RubyHash result) {
IRubyObject newKey = block.yield(context, key);
result.fastASet(newKey, value);
}
}

@JRubyMethod(name = "transform_values")
@@ -1471,10 +1492,9 @@ public IRubyObject transform_values(final ThreadContext context, final Block blo
public IRubyObject transform_keys_bang(final ThreadContext context, final Block block) {
if (block.isGiven()) {
testFrozen("Hash");
RubyHash bak = (RubyHash)dup();
rb_clear();
bak.keys().forEach((k) -> {
op_aset(context, block.yield(context, (IRubyObject)k), bak.op_aref(context, (IRubyObject)k));
RubyArray keys = keys(context);
Arrays.stream(keys.toJavaArrayMaybeUnsafe()).forEach((key) -> {
op_aset(context, block.yield(context, key), delete(context, key));
});
return this;
}
@@ -1684,6 +1704,10 @@ public IRubyObject delete(ThreadContext context, IRubyObject key, Block block) {
return context.nil;
}

public IRubyObject delete(ThreadContext context, IRubyObject key) {
return delete(context, key, Block.NULL_BLOCK);
}

/** rb_hash_select
*
*/
6 changes: 4 additions & 2 deletions core/src/main/java/org/jruby/RubyInteger.java
Original file line number Diff line number Diff line change
@@ -144,10 +144,12 @@ public boolean isPositive() {
*
*/
@JRubyMethod(meta = true)
public static IRubyObject sqrt(ThreadContext context, IRubyObject self, IRubyObject arg) {
return RubyMath.sqrt(context, self, arg.convertToInteger()).convertToInteger();
public static IRubyObject sqrt(ThreadContext context, IRubyObject self, IRubyObject num) {
return num.convertToInteger().sqrt(context);
}

public abstract IRubyObject sqrt(ThreadContext context);

/* ================
* Instance Methods
* ================
8 changes: 2 additions & 6 deletions core/src/main/java/org/jruby/RubyRange.java
Original file line number Diff line number Diff line change
@@ -256,12 +256,8 @@ final int[] begLenInt(int len, int err) {

private void init(ThreadContext context, IRubyObject begin, IRubyObject end, boolean isExclusive) {
if (!(begin instanceof RubyFixnum && end instanceof RubyFixnum)) {
try {
IRubyObject result = invokedynamic(context, begin, MethodNames.OP_CMP, end);
if (result.isNil()) {
throw context.runtime.newArgumentError("bad value for range");
}
} catch (RaiseException re) {
IRubyObject result = invokedynamic(context, begin, MethodNames.OP_CMP, end);
if (result.isNil()) {
throw context.runtime.newArgumentError("bad value for range");
}
}
Loading