Skip to content

Commit

Permalink
Merge pull request jruby#8273 from enebo/argf_fix
Browse files Browse the repository at this point in the history
ARGF spec fixes.
  • Loading branch information
enebo authored May 31, 2024
2 parents 29eb639 + 8fce5cf commit 0fcbf68
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 12 deletions.
14 changes: 8 additions & 6 deletions core/src/main/java/org/jruby/RubyArgsFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.internal.runtime.GlobalVariable;
import org.jruby.internal.runtime.ReadonlyAccessor;
import org.jruby.internal.runtime.ValueAccessor;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.CallSite;
Expand Down Expand Up @@ -166,14 +168,14 @@ public boolean next_argv(ThreadContext context) {
}
}

IRubyObject $FILENAME = runtime.getGlobalVariables().get("$FILENAME");
GlobalVariable $FILENAME = runtime.getGlobalVariables().getVariable("$FILENAME");

if (next_p == NextFile) {
if (argv.getLength() > 0) {
RubyString filename = TypeConverter.convertToType(argv.shift(context), context.runtime.getString(), "to_path").convertToString();
StringSupport.checkStringSafety(runtime, filename);
if ( ! filename.op_equal(context, $FILENAME).isTrue() ) {
runtime.defineReadonlyVariable("$FILENAME", filename, GlobalVariable.Scope.GLOBAL);
if ( ! filename.op_equal(context, $FILENAME.getAccessor().getValue()).isTrue() ) {
$FILENAME.forceValue(filename);
}

if (filenameEqlDash(filename)) {
Expand All @@ -199,8 +201,8 @@ public boolean next_argv(ThreadContext context) {
}
} else if (next_p == Stream) {
currentFile = runtime.getGlobalVariables().get("$stdin");
if (!filenameEqlDash((RubyString) $FILENAME)) {
runtime.defineReadonlyVariable("$FILENAME", runtime.newString("-"), GlobalVariable.Scope.GLOBAL);
if (!filenameEqlDash((RubyString) $FILENAME.getAccessor().getValue())) {
$FILENAME.forceValue(runtime.newString("-"));
}
}

Expand Down Expand Up @@ -540,7 +542,7 @@ public static IRubyObject each_codepoint(ThreadContext context, IRubyObject recv
data.next_p = NextFile;
}

return context.nil;
return recv;
}

@JRubyMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ public void trace(IRubyObject value) {
context.setWithinTrace(false);
}
}

/**
* read-only variables like $FILENAME are ony read-only to the user. We need to change them behind the curtain.
* @param newValue
*/
public void forceValue(IRubyObject newValue) {
accessor.forceValue(newValue);
invalidate();
}

public Invalidator getInvalidator() {
return invalidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ public IRubyObject setValue(IRubyObject newValue) {

throw newValue.getRuntime().newNameError(name + " is a read-only variable", name);
}

public void forceValue(IRubyObject newValue) {
accessor.setValue(newValue);
}
}
3 changes: 3 additions & 0 deletions core/src/main/java/org/jruby/runtime/IAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@
public interface IAccessor {
public IRubyObject getValue();
public IRubyObject setValue(IRubyObject newValue);
default void forceValue(IRubyObject newValue) {
setValue(newValue);
}
}
2 changes: 0 additions & 2 deletions spec/tags/ruby/core/argf/codepoints_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/tags/ruby/core/argf/each_codepoint_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/tags/ruby/core/argf/filename_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/tags/ruby/core/argf/lines_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/tags/ruby/core/argf/path_tags.txt

This file was deleted.

0 comments on commit 0fcbf68

Please sign in to comment.