Skip to content

Commit

Permalink
[GR-18163] [GR-59866 Follow-up to recently merged PRs
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4454
  • Loading branch information
andrykonchin committed Jan 17, 2025
2 parents 85b1c41 + 848ac54 commit 9c3cae2
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 127 deletions.
2 changes: 1 addition & 1 deletion lib/cext/ABI_check.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7
9
4 changes: 2 additions & 2 deletions lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2336,7 +2336,7 @@ def rb_eval_cmd_kw(cmd, args, kw_splat)

def rb_tr_warn(message)
location = caller_locations(1, 1)[0]
message_with_prefix = location.label + ': warning: ' + message
message_with_prefix = "#{location.label}: warning: #{message}"
Warning.warn(message_with_prefix)
end

Expand All @@ -2346,7 +2346,7 @@ def rb_warning_category_enabled_p(category)

def rb_tr_warn_category(message, category)
location = caller_locations(1, 1)[0]
message_with_prefix = location.label + ': warning: ' + message
message_with_prefix = "#{location.label}: warning: #{message}"
Warning.warn(message_with_prefix, category: category)
end

Expand Down
39 changes: 19 additions & 20 deletions spec/ruby/core/dir/chdir_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ def to_str; DirSpecs.mock_dir; end
end

it "raises an Errno::ENOENT if the original directory no longer exists" do
dir1 = tmp('/testdir1')
dir2 = tmp('/testdir2')
File.should_not.exist?(dir1)
File.should_not.exist?(dir2)
dir1 = tmp('testdir1')
dir2 = tmp('testdir2')
Dir.should_not.exist?(dir1)
Dir.should_not.exist?(dir2)
Dir.mkdir dir1
Dir.mkdir dir2
begin
Expand All @@ -108,8 +108,8 @@ def to_str; DirSpecs.mock_dir; end
end
}.should raise_error(Errno::ENOENT)
ensure
Dir.unlink dir1 if File.exist?(dir1)
Dir.unlink dir2 if File.exist?(dir2)
Dir.unlink dir1 if Dir.exist?(dir1)
Dir.unlink dir2 if Dir.exist?(dir2)
end
end

Expand Down Expand Up @@ -177,28 +177,27 @@ def to_str; DirSpecs.mock_dir; end
dir.close
end

it "raises an Errno::ENOENT if the original directory no longer exists" do
dir_name1 = tmp('/testdir1')
dir_name2 = tmp('/testdir2')
File.should_not.exist?(dir_name1)
File.should_not.exist?(dir_name2)
it "does not raise an Errno::ENOENT if the original directory no longer exists" do
dir_name1 = tmp('testdir1')
dir_name2 = tmp('testdir2')
Dir.should_not.exist?(dir_name1)
Dir.should_not.exist?(dir_name2)
Dir.mkdir dir_name1
Dir.mkdir dir_name2

dir1 = Dir.new(dir_name1)
dir2 = Dir.new(dir_name2)

begin
-> {
dir1.chdir do
Dir.chdir(dir_name2) { Dir.unlink dir_name1 }
end
}.should raise_error(Errno::ENOENT)
Dir.chdir(dir_name1) do
dir2.chdir { Dir.unlink dir_name1 }
end
Dir.pwd.should == @original
ensure
Dir.unlink dir_name1 if File.exist?(dir_name1)
Dir.unlink dir_name2 if File.exist?(dir_name2)
Dir.unlink dir_name1 if Dir.exist?(dir_name1)
Dir.unlink dir_name2 if Dir.exist?(dir_name2)
end
ensure
dir1.close
dir2.close
end

it "always returns to the original directory when given a block" do
Expand Down
4 changes: 2 additions & 2 deletions spec/ruby/core/dir/for_fd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

it "calls #to_int to convert a value to an Integer" do
dir = Dir.new(DirSpecs.mock_dir)
obj = Object.new
obj.singleton_class.define_method(:to_int) { dir.fileno }
obj = mock("fd")
obj.should_receive(:to_int).and_return(dir.fileno)

dir_new = Dir.for_fd(obj)
dir_new.fileno.should == dir.fileno
Expand Down
4 changes: 2 additions & 2 deletions spec/ruby/core/proc/clone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
end

ruby_version_is "3.3" do
it "calls #initialize_copy on subclass" do
it "calls #initialize_clone on subclass" do
obj = ProcSpecs::MyProc2.new(:a, 2) { }
dup = obj.clone

Expand All @@ -24,7 +24,7 @@

dup.first.should == :a
dup.second.should == 2
dup.initializer.should == :copy
dup.initializer.should == :clone
end
end
end
7 changes: 7 additions & 0 deletions spec/ruby/core/proc/fixtures/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def initialize_dup(other)
@first = other.first
@second = other.second
end

def initialize_clone(other, **options)
super
@initializer = :clone
@first = other.first
@second = other.second
end
end

class Arity
Expand Down
4 changes: 4 additions & 0 deletions src/main/c/cext/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ bool rb_warning_category_enabled_p(rb_warning_category_t category) {
return true;
}
}

void rb_tr_warn_va_list(const char *fmt, va_list args) {
RUBY_CEXT_INVOKE("rb_tr_warn", rb_vsprintf(fmt, args));
}
4 changes: 0 additions & 4 deletions src/main/c/cext/truffleruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ int rb_tr_obj_equal(VALUE first, VALUE second) {
return RTEST(RUBY_CEXT_INVOKE("rb_tr_obj_equal", first, second));
}

void rb_tr_warn_va_list(const char *fmt, va_list args) {
RUBY_CEXT_INVOKE("rb_tr_warn", rb_vsprintf(fmt, args));
}

VALUE rb_tr_zlib_crc_table(void) {
return RUBY_CEXT_INVOKE("zlib_get_crc_table");
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/truffleruby/core/proc/ProcNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ RubyProc procSpecial(VirtualFrame frame, RubyClass procClass, Object[] args, Rub
@Cached DispatchNode initialize) {
// Instantiate a new instance of procClass as classes do not correspond

final RubyProc proc = ProcOperations.duplicate(procClass, getLanguage().procShape, block, this);
final RubyProc proc = block.duplicate(procClass, getLanguage().procShape, this);
initialize.callWithDescriptor(proc, "initialize", block, RubyArguments.getDescriptor(frame), args);
return proc;
}
Expand All @@ -101,9 +101,9 @@ public abstract static class CloneNode extends CoreMethodArrayArgumentsNode {

@Specialization
RubyProc clone(RubyProc proc,
@Cached DispatchNode initializeCopyNode) {
final RubyProc copy = ProcOperations.duplicate(proc.getLogicalClass(), getLanguage().procShape, proc, this);
initializeCopyNode.call(copy, "initialize_copy", proc);
@Cached DispatchNode initializeCloneNode) {
final RubyProc copy = proc.duplicate(getLanguage().procShape, this);
initializeCloneNode.call(copy, "initialize_clone", proc);
return copy;
}
}
Expand All @@ -114,7 +114,7 @@ public abstract static class DupNode extends CoreMethodArrayArgumentsNode {
@Specialization
RubyProc dup(RubyProc proc,
@Cached DispatchNode initializeDupNode) {
final RubyProc copy = ProcOperations.duplicate(proc.getLogicalClass(), getLanguage().procShape, proc, this);
final RubyProc copy = proc.duplicate(getLanguage().procShape, this);
initializeDupNode.call(copy, "initialize_dup", proc);
return copy;
}
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/org/truffleruby/core/proc/ProcOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package org.truffleruby.core.proc;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.Shape;
import org.truffleruby.RubyContext;
import org.truffleruby.RubyLanguage;
Expand All @@ -21,7 +20,6 @@
import org.truffleruby.language.methods.DeclarationContext;
import org.truffleruby.language.methods.InternalMethod;
import org.truffleruby.language.methods.SharedMethodInfo;
import org.truffleruby.language.objects.AllocationTracing;
import org.truffleruby.language.threadlocal.SpecialVariableStorage;

import com.oracle.truffle.api.RootCallTarget;
Expand Down Expand Up @@ -125,24 +123,6 @@ public static RubyProc createProcFromBlock(RubyContext context, RubyLanguage lan
return convertBlock(context, language, block, ProcType.PROC);
}

public static RubyProc duplicate(RubyClass procClass, Shape shape, RubyProc proc, Node node) {
final RubyProc copy = new RubyProc(
procClass,
shape,
proc.type,
proc.arity,
proc.argumentDescriptors,
proc.callTargets,
proc.callTarget,
proc.declarationFrame,
proc.declarationVariables,
proc.declaringMethod,
proc.frameOnStackMarker,
proc.declarationContext);
AllocationTracing.trace(copy, node);
return copy;
}

public static Object getSelf(RubyProc proc) {
return RubyArguments.getSelf(proc.declarationFrame);
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/truffleruby/core/proc/RubyProc.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.truffleruby.language.methods.DeclarationContext;
import org.truffleruby.language.methods.InternalMethod;
import org.truffleruby.language.methods.SharedMethodInfo;
import org.truffleruby.language.objects.AllocationTracing;
import org.truffleruby.language.objects.ObjectGraph;
import org.truffleruby.language.objects.ObjectGraphNode;
import org.truffleruby.language.threadlocal.SpecialVariableStorage;
Expand Down Expand Up @@ -98,6 +99,28 @@ public RubyProc withArity(Arity newArity, ArgumentDescriptor[] newArgumentDescri
declarationContext);
}

public RubyProc duplicate(RubyClass procClass, Shape shape, Node node) {
final RubyProc copy = new RubyProc(
procClass,
shape,
this.type,
this.arity,
this.argumentDescriptors,
this.callTargets,
this.callTarget,
this.declarationFrame,
this.declarationVariables,
this.declaringMethod,
this.frameOnStackMarker,
this.declarationContext);
AllocationTracing.trace(copy, node);
return copy;
}

public RubyProc duplicate(Shape shape, Node node) {
return duplicate(this.getLogicalClass(), shape, node);
}

@Override
public void getAdjacentObjects(Set<Object> reachable) {
ObjectGraph.getObjectsInFrame(declarationFrame, reachable);
Expand Down
64 changes: 0 additions & 64 deletions src/main/java/org/truffleruby/core/range/RangeNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,70 +418,6 @@ public RubyNode cloneUninitialized() {
}
}

@CoreMethod(names = "reverse_each", needsBlock = true, enumeratorSize = "size")
public abstract static class ReverseEachNode extends CoreMethodArrayArgumentsNode {

@Child private DispatchNode reverseEachInternalCall;

@Specialization
RubyIntRange reverseEachInt(RubyIntRange range, RubyProc block,
@Cached @Shared InlinedConditionProfile excludedEndProfile,
@Cached @Exclusive InlinedLoopConditionProfile loopProfile,
@Cached @Shared CallBlockNode yieldNode) {
final int end;
if (excludedEndProfile.profile(this, range.excludedEnd)) {
end = range.end - 1;
} else {
end = range.end;
}

int n = end;
try {
for (; loopProfile.inject(this, n >= range.begin); n--) {
yieldNode.yield(this, block, n);
}
} finally {
profileAndReportLoopCount(this, loopProfile, end - range.begin + 1);
}

return range;
}

@Specialization
RubyLongRange reverseEachLong(RubyLongRange range, RubyProc block,
@Cached @Shared InlinedConditionProfile excludedEndProfile,
@Cached @Exclusive InlinedLoopConditionProfile loopProfile,
@Cached @Shared CallBlockNode yieldNode) {
final long end;
if (excludedEndProfile.profile(this, range.excludedEnd)) {
end = range.end - 1;
} else {
end = range.end;
}

long n = end;
try {
for (; loopProfile.inject(this, n >= range.begin); n--) {
yieldNode.yield(this, block, n);
}
} finally {
profileAndReportLoopCount(this, loopProfile, end - range.begin + 1);
}

return range;
}

@Specialization
Object reverseEachObject(RubyObjectRange range, RubyProc block) {
if (reverseEachInternalCall == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
reverseEachInternalCall = insert(DispatchNode.create());
}

return reverseEachInternalCall.callWithBlock(range, "reverse_each_internal", block);
}
}

@GenerateCached(false)
@GenerateInline
public abstract static class NewRangeNode extends RubyBaseNode {
Expand Down
12 changes: 8 additions & 4 deletions src/main/ruby/truffleruby/core/dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,14 @@ def fchdir(fd)
begin
yield
ensure
Primitive.dir_set_truffle_working_directory(original_path)
ret = Truffle::POSIX.fchdir original_dir.fileno
Errno.handle('fchdir') if ret != 0
original_dir.close
begin
ret = Truffle::POSIX.fchdir original_dir.fileno
Errno.handle('fchdir') if ret != 0

Primitive.dir_set_truffle_working_directory(original_path)
ensure
original_dir.close
end
end
else
ret = Truffle::POSIX.fchdir fd
Expand Down
4 changes: 2 additions & 2 deletions src/main/ruby/truffleruby/core/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ def %(n)
Truffle::RangeOperations.step_no_block(self, n)
end

private def reverse_each_internal(&block)
return to_enum { size } unless block_given?
def reverse_each(&block)
return to_enum(:reverse_each) { size } unless block_given?

if Primitive.nil?(self.end)
raise TypeError, "can't iterate from NilClass"
Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestRange.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
exclude :test_reverse_each_for_beginless_range, "TypeError: can't iterate from NilClass"
exclude :test_reverse_each_for_empty_range, "RangeError: long too big to convert into `int'"
exclude :test_reverse_each_for_endless_range, "[TypeError] exception expected, not #<RangeError: cannot convert endless range to an array>."
exclude :test_reverse_each_for_single_point_range, "<no message> (java.lang.NegativeArraySizeException)"
exclude :test_size, "<3> expected but was <(31/10)>."
exclude :test_step, "ArgumentError expected but nothing was raised."
exclude :test_step_with_succ, "NoMethodError: undefined method `i' for nil:NilClass"

0 comments on commit 9c3cae2

Please sign in to comment.