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: 6598331f4f45
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 39d1ba335d27
Choose a head ref
  • 5 commits
  • 8 files changed
  • 1 contributor

Commits on Jun 28, 2018

  1. [refactor] cleanup and review RubyIO internals

    - review internals to cast less - return concrete types
    - non-boxing IO helpers for In/Out Java Stream wrappers
    - deprecate un-used over-load JRuby methods
    - make var-args readlines delegating to io.readlines
    - use helper for (small) array copy-ing
    - handle several (IDEA) code warnings
    kares committed Jun 28, 2018
    Copy the full SHA
    adccc0c View commit details
  2. lets use AssertionError on unexpected outcomes

    ... managed to fool myself with a catch RuntimeException already
    kares committed Jun 28, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    16ecc3c View commit details
  3. [refactor] split getline internal into 2 methods

    ... also removed byte-list cache handling for now - never used
    kares committed Jun 28, 2018
    Copy the full SHA
    99a13a8 View commit details
  4. [refactor] cleanup RubyArgsFile - prefer getx(ctx) impl

    although weird getc does getbyte but specs covered, thus fine
    kares committed Jun 28, 2018
    Copy the full SHA
    c10d686 View commit details
  5. [fix] exec/spawn invalid opts expected to raise ArgumentError

    ... and deprecate the old public static helpers on RubyIO's end
    kares committed Jun 28, 2018
    Copy the full SHA
    39d1ba3 View commit details
126 changes: 70 additions & 56 deletions core/src/main/java/org/jruby/RubyArgsFile.java
Original file line number Diff line number Diff line change
@@ -377,24 +377,26 @@ public static IRubyObject to_a(ThreadContext context, IRubyObject recv, IRubyObj

RubyArray ary = runtime.newArray();
IRubyObject line;
while(!(line = argf_getline(context, recv, args)).isNil()) {
while ((line = argf_getline(context, recv, args)) != context.nil) {
ary.append(line);
}
return ary;
}

public static IRubyObject each_byte(ThreadContext context, IRubyObject recv, Block block) {
IRubyObject bt;
@JRubyMethod
public static IRubyObject each_byte(final ThreadContext context, IRubyObject recv, final Block block) {
if (!block.isGiven()) return enumeratorize(context.runtime, recv, "each_byte");

while(!(bt = getc(context, recv)).isNil()) {
IRubyObject bt;
while ((bt = getc(context, recv)) != context.nil) {
block.yield(context, bt);
}

return recv;
}

@JRubyMethod(optional = 1)
public static IRubyObject each_byte(final ThreadContext context, IRubyObject recv, IRubyObject[] args, final Block block) {
@JRubyMethod
public static IRubyObject each_byte(final ThreadContext context, IRubyObject recv, IRubyObject arg, final Block block) {
return block.isGiven() ? each_byte(context, recv, block) : enumeratorize(context.runtime, recv, "each_byte");
}

@@ -414,22 +416,24 @@ public static IRubyObject chars(final ThreadContext context, IRubyObject recv, B
}

public static IRubyObject each_charCommon(ThreadContext context, IRubyObject recv, Block block) {
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);
Ruby runtime = context.runtime;
final Ruby runtime = context.runtime;

ArgsFileData data = ArgsFileData.getArgsFileData(runtime);

IRubyObject ch;
while(!(ch = getc(context, recv)).isNil()) {
while ((ch = getc(context, recv)) != context.nil) {
boolean cont = true;
while(cont) {
while (cont) {
cont = false;
byte c = (byte)RubyNumeric.fix2int(ch);
byte c = (byte) RubyNumeric.fix2int(ch);
int n = runtime.getKCode().getEncoding().length(c);
IRubyObject file = data.currentFile;
RubyString str = runtime.newString();
str.setTaint(true);
str.cat(c);

while(--n > 0) {
if((ch = getc(context, recv)).isNil()) {
if ((ch = getc(context, recv)) == context.nil) {
block.yield(context, str);
return recv;
}
@@ -438,7 +442,7 @@ public static IRubyObject each_charCommon(ThreadContext context, IRubyObject rec
cont = true;
continue;
}
c = (byte)RubyNumeric.fix2int(ch);
c = (byte) RubyNumeric.fix2int(ch);
str.cat(c);
}
block.yield(context, str);
@@ -472,8 +476,10 @@ public static IRubyObject codepoints(ThreadContext context, IRubyObject recv, Bl
/** Invoke a block for each line.
*
*/
@JRubyMethod(name = "each_line", optional = 1)
public static IRubyObject each_line(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
if (!block.isGiven()) return RubyEnumerator.enumeratorize(context.runtime, recv, "each_line");
if (!block.isGiven()) return enumeratorize(context.runtime, recv, "each_line", args);

ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

if (!data.next_argv(context)) return context.nil;
@@ -484,29 +490,37 @@ public static IRubyObject each_line(ThreadContext context, IRubyObject recv, IRu
data.currentFile.callMethod(context, "each", NULL_ARRAY, block);
data.next_p = 1;
}

IRubyObject str;
while(!(str = argf_getline(context, recv, args)).isNil()) {
while ((str = argf_getline(context, recv, args)) != context.nil) {
block.yield(context, str);
}

return recv;
}

@Deprecated // TODO "warning: ARGF#lines is deprecated; use #each_line instead"
@JRubyMethod(optional = 1)
public static IRubyObject lines(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
if (!block.isGiven()) return RubyEnumerator.enumeratorize(context.runtime, recv, "each_line");
return each_line(context, recv, args, block);
}

@JRubyMethod(name = "each_line", optional = 1)
@Deprecated
public static IRubyObject each_line19(final ThreadContext context, IRubyObject recv, IRubyObject[] args, final Block block) {
return block.isGiven() ? each_line(context, recv, args, block) : enumeratorize(context.runtime, recv, "each_line", args);
return each_line(context, recv, args, block);
}

@JRubyMethod(name = "each", optional = 1)
public static IRubyObject each19(final ThreadContext context, IRubyObject recv, IRubyObject[] args, final Block block) {
public static IRubyObject each(final ThreadContext context, IRubyObject recv, IRubyObject[] args, final Block block) {
return block.isGiven() ? each_line(context, recv, args, block) : enumeratorize(context.runtime, recv, "each", args);
}

@Deprecated
public static IRubyObject each19(final ThreadContext context, IRubyObject recv, IRubyObject[] args, final Block block) {
return each(context, recv, args, block);
}

@JRubyMethod(name = "file")
public static IRubyObject file(ThreadContext context, IRubyObject recv) {
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);
@@ -530,7 +544,7 @@ public static IRubyObject skip(IRubyObject recv) {
}

public static void argf_close(ThreadContext context, IRubyObject file) {
if(file instanceof RubyIO) {
if (file instanceof RubyIO) {
((RubyIO) file).rbIoClose(context);
} else {
file.callMethod(context, "close");
@@ -574,7 +588,7 @@ public static IRubyObject binmode(ThreadContext context, IRubyObject recv) {
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

data.binmode = true;
if (!data.currentFile.isNil()) ((RubyIO) data.currentFile).binmode();
if (data.currentFile != context.nil) ((RubyIO) data.currentFile).binmode();

return recv;
}
@@ -593,7 +607,7 @@ public static IRubyObject lineno(ThreadContext context, IRubyObject recv) {
public static IRubyObject lineno_set(ThreadContext context, IRubyObject recv, IRubyObject line) {
context.runtime.setCurrentLine(RubyNumeric.fix2int(line));

return recv.getRuntime().getNil();
return context.nil;
}

@JRubyMethod(name = "tell", alias = {"pos"})
@@ -651,7 +665,7 @@ public static IRubyObject seek(ThreadContext context, IRubyObject recv, IRubyObj
public static IRubyObject readchar(ThreadContext context, IRubyObject recv) {
IRubyObject c = getc(context, recv);

if (c.isNil()) throw context.runtime.newEOFError();
if (c == context.nil) throw context.runtime.newEOFError();

return c;
}
@@ -660,17 +674,17 @@ public static IRubyObject readchar(ThreadContext context, IRubyObject recv) {
public static IRubyObject getbyte(ThreadContext context, IRubyObject recv) {
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

while(true) {
while (true) {
if (!data.next_argv(context)) return context.nil;

IRubyObject bt;
if (!(data.currentFile instanceof RubyFile)) {
bt = data.currentFile.callMethod(context, "getbyte");
if (data.currentFile instanceof RubyFile) {
bt = ((RubyIO) data.currentFile).getbyte(context);
} else {
bt = ((RubyIO)data.currentFile).getbyte(context);
bt = data.currentFile.callMethod(context, "getbyte");
}

if (!bt.isNil()) return bt;
if (bt != context.nil) return bt;

argf_close(context, data.currentFile);
data.next_p = 1;
@@ -693,12 +707,12 @@ private static IRubyObject getPartial(ThreadContext context, IRubyObject recv, I
RubyString str = null;
if ( args.length > 1 ) {
IRubyObject opts = TypeConverter.checkHashType(runtime, args[args.length - 1]);
if ( ! opts.isNil() &&
runtime.getFalse() == ((RubyHash) opts).op_aref(context, runtime.newSymbol("exception")) ) {
if ( opts != context.nil &&
context.fals == ((RubyHash) opts).op_aref(context, runtime.newSymbol("exception")) ) {
noException = true;
}
if (args.length > 2 || opts.isNil()) {
if (!args[1].isNil()) {
if (args.length > 2 || opts == context.nil) {
if (args[1] != context.nil) {
args[1] = args[1].convertToString();
str = (RubyString) args[1];
}
@@ -714,15 +728,15 @@ private static IRubyObject getPartial(ThreadContext context, IRubyObject recv, I
}

IRubyObject res = ((RubyIO) data.currentFile).getPartial(context, args, nonBlocking, noException);
if (res.isNil()) {
if (res == context.nil) {
if (data.next_p == -1) return RubyIO.nonblockEOF(runtime, noException);

argf_close(context, data.currentFile);
data.next_p = 1;

if (data.argv.isEmpty()) return RubyIO.nonblockEOF(runtime, noException);

if (args.length > 1 && args[1] instanceof RubyString )return args[1];
if (args.length > 1 && args[1] instanceof RubyString ) return args[1];
return RubyString.newEmptyString(runtime);
}

@@ -742,17 +756,17 @@ public static IRubyObject readbyte(ThreadContext context, IRubyObject recv) {
public static IRubyObject getc(ThreadContext context, IRubyObject recv) {
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

while(true) {
while (true) {
if (!data.next_argv(context)) return context.nil;

IRubyObject bt;
if (!(data.currentFile instanceof RubyFile)) {
bt = data.currentFile.callMethod(context,"getc");
if (data.currentFile instanceof RubyFile) {
bt = ((RubyIO) data.currentFile).getbyte(context);
} else {
bt = ((RubyIO)data.currentFile).getc();
bt = data.currentFile.callMethod(context,"getc");
}

if (!bt.isNil()) return bt;
if (bt != context.nil) return bt;

argf_close(context, data.currentFile);
data.next_p = 1;
@@ -768,45 +782,45 @@ public static IRubyObject read(ThreadContext context, IRubyObject recv, IRubyObj

if (args.length > 0) {
length = args[0];
str = args.length > 1 ? args[1] : runtime.getNil();
str = args.length > 1 ? args[1] : context.nil;
} else {
length = runtime.getNil();
str = runtime.getNil();
str = length = context.nil;
}

if (!length.isNil()) len = RubyNumeric.num2long(length);
if (length != context.nil) len = RubyNumeric.num2long(length);

if (!str.isNil()) {
if (str != context.nil) {
str = str.convertToString();
((RubyString)str).modify();
((RubyString)str).getByteList().length(0);
args[1] = runtime.getNil();
((RubyString) str).modify();
((RubyString) str).getByteList().length(0);
args[1] = context.nil;
}

while(true) {
while (true) {
if (!data.next_argv(context)) return str;

if (!(data.currentFile instanceof RubyIO)) {
tmp = data.currentFile.callMethod(context, "read", args);
if (data.currentFile instanceof RubyIO) {
tmp = ((RubyIO) data.currentFile).read(args);
} else {
tmp = ((RubyIO)data.currentFile).read(args);
tmp = data.currentFile.callMethod(context, "read", args);
}

if (str.isNil()) {
if (str == context.nil) {
str = tmp;
} else if (!tmp.isNil()) {
((RubyString)str).append(tmp);
} else if (tmp != context.nil) {
((RubyString) str).append(tmp);
}

if (tmp.isNil() || length.isNil()) {
if (tmp == context.nil || length == context.nil) {
if(data.next_p != -1) {
argf_close(context, data.currentFile);
data.next_p = 1;
continue;
}
} else if(args.length >= 1) {
if (((RubyString)str).getByteList().length() < len) {
len -= ((RubyString)str).getByteList().length();
final int strLen = ((RubyString) str).getByteList().length();
if (strLen < len) {
len -= strLen;
args[0] = runtime.newFixnum(len);
continue;
}
12 changes: 6 additions & 6 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
@@ -1340,17 +1340,17 @@ public static IRubyObject unlink(ThreadContext context, IRubyObject... args) {
// rb_file_size but not using stat
@JRubyMethod
public IRubyObject size(ThreadContext context) {
OpenFile fptr;
long size;
return RubyFixnum.newFixnum(context.runtime, getSize(context));
}

final long getSize(ThreadContext context) {
OpenFile fptr = getOpenFileChecked();

fptr = getOpenFileChecked();
if ((fptr.getMode() & OpenFile.WRITABLE) != 0) {
flushRaw(context, false);
}

size = fptr.posix.size(fptr.fd());

return RubyFixnum.newFixnum(context.runtime, size);
return fptr.posix.size(fptr.fd());
}

@JRubyMethod(meta = true)
Loading