Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions core/src/main/java/org/jruby/RubySystemExit.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;

import static org.jruby.api.Access.integerClass;
import static org.jruby.api.Convert.asFixnum;
import static org.jruby.api.Create.newString;
import static org.jruby.api.Define.defineClass;
import static org.jruby.runtime.Visibility.*;

import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.TypeConverter;

/**
* The Java representation of a Ruby SystemExit.
Expand Down Expand Up @@ -90,15 +92,21 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block b

if (argc > 0) {
final IRubyObject arg = args[0];
if (arg instanceof RubyFixnum) {
this.status = arg;
if (argc > 1) this.message = args[1]; // (status, message)
} else if (arg instanceof RubyBoolean) {
if (arg instanceof RubyBoolean) {
this.status = asFixnum(context, arg == context.tru ? 0 : 1 );
if (argc > 1) this.message = args[1]; // (status, message)
} else {
this.message = arg;
this.status = RubyFixnum.zero(context.runtime);
// like exit_initialize() in CRuby, the first argument is the status if #to_int converts it to an
// Integer (rb_check_to_int), and the message otherwise
final IRubyObject status = arg instanceof RubyInteger ? arg :
TypeConverter.convertToType(context, arg, integerClass(context), context.sites.TypeConverter.to_int_checked, false);
if (!(status instanceof RubyInteger)) {
this.message = arg;
this.status = RubyFixnum.zero(context.runtime);
} else {
this.status = status;
if (argc > 1) this.message = args[1]; // (status, message)
}
}
} else {
this.status = RubyFixnum.zero(context.runtime);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/runtime/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,9 @@
String message = null;

if (args.length > 0) {
RubyObject argument = (RubyObject) args[0];
IRubyObject argument = args[0];
if (argument instanceof RubyBoolean) {
status = argument.isFalse() ? 1 : 0;
status = argument.isTrue() ? 0 : 1;
} else {
status = toInt(context, argument);
}
Expand Down Expand Up @@ -1846,7 +1846,7 @@

public static RubyHash constructHash(Ruby runtime,
IRubyObject key1, IRubyObject value1, boolean prepareString1) {
RubyHash hash = RubyHash.newHash(runtime);

Check warning on line 1849 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Dependency check

[deprecation] newHash(Ruby) in RubyHash has been deprecated

Check warning on line 1849 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Verify build artifacts

[deprecation] newHash(Ruby) in RubyHash has been deprecated
hash.fastASet(runtime, key1, value1, prepareString1);
return hash;
}
Expand All @@ -1854,7 +1854,7 @@
public static RubyHash constructHash(Ruby runtime,
IRubyObject key1, IRubyObject value1, boolean prepareString1,
IRubyObject key2, IRubyObject value2, boolean prepareString2) {
RubyHash hash = RubyHash.newHash(runtime);

Check warning on line 1857 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Dependency check

[deprecation] newHash(Ruby) in RubyHash has been deprecated

Check warning on line 1857 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Verify build artifacts

[deprecation] newHash(Ruby) in RubyHash has been deprecated
hash.fastASet(runtime, key1, value1, prepareString1);
hash.fastASet(runtime, key2, value2, prepareString2);
return hash;
Expand All @@ -1864,7 +1864,7 @@
IRubyObject key1, IRubyObject value1, boolean prepareString1,
IRubyObject key2, IRubyObject value2, boolean prepareString2,
IRubyObject key3, IRubyObject value3, boolean prepareString3) {
RubyHash hash = RubyHash.newHash(runtime);

Check warning on line 1867 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Dependency check

[deprecation] newHash(Ruby) in RubyHash has been deprecated

Check warning on line 1867 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Verify build artifacts

[deprecation] newHash(Ruby) in RubyHash has been deprecated
hash.fastASet(runtime, key1, value1, prepareString1);
hash.fastASet(runtime, key2, value2, prepareString2);
hash.fastASet(runtime, key3, value3, prepareString3);
Expand All @@ -1876,7 +1876,7 @@
IRubyObject key2, IRubyObject value2, boolean prepareString2,
IRubyObject key3, IRubyObject value3, boolean prepareString3,
IRubyObject key4, IRubyObject value4, boolean prepareString4) {
RubyHash hash = RubyHash.newHash(runtime);

Check warning on line 1879 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Dependency check

[deprecation] newHash(Ruby) in RubyHash has been deprecated

Check warning on line 1879 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Verify build artifacts

[deprecation] newHash(Ruby) in RubyHash has been deprecated
hash.fastASet(runtime, key1, value1, prepareString1);
hash.fastASet(runtime, key2, value2, prepareString2);
hash.fastASet(runtime, key3, value3, prepareString3);
Expand All @@ -1890,7 +1890,7 @@
IRubyObject key3, IRubyObject value3, boolean prepareString3,
IRubyObject key4, IRubyObject value4, boolean prepareString4,
IRubyObject key5, IRubyObject value5, boolean prepareString5) {
RubyHash hash = RubyHash.newHash(runtime);

Check warning on line 1893 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Dependency check

[deprecation] newHash(Ruby) in RubyHash has been deprecated

Check warning on line 1893 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Verify build artifacts

[deprecation] newHash(Ruby) in RubyHash has been deprecated
hash.fastASet(runtime, key1, value1, prepareString1);
hash.fastASet(runtime, key2, value2, prepareString2);
hash.fastASet(runtime, key3, value3, prepareString3);
Expand Down Expand Up @@ -3066,7 +3066,7 @@
return safeHashLong(simpleHash);
}

return safeHash(context, obj).getLongValue();

Check warning on line 3069 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Dependency check

[deprecation] getLongValue() in RubyFixnum has been deprecated

Check warning on line 3069 in core/src/main/java/org/jruby/runtime/Helpers.java

View workflow job for this annotation

GitHub Actions / Verify build artifacts

[deprecation] getLongValue() in RubyFixnum has been deprecated
}

// MRI: rb_hash but only for simple hashable objects
Expand Down
22 changes: 22 additions & 0 deletions spec/ruby/core/exception/system_exit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@
exc.message.should == "SystemExit"
end

it "accepts a status that converts with #to_int" do
status = mock('status')
status.should_receive(:to_int).twice.and_return(42)

exc = SystemExit.new(status, "message")
exc.status.should == 42
exc.message.should == "message"

exc = SystemExit.new(status)
exc.status.should == 42
exc.message.should == "SystemExit"
end

it "uses the argument as the message if #to_int does not return an Integer" do
status = mock('status')
status.should_receive(:to_int).and_return("42")

exc = SystemExit.new(status)
exc.status.should == 0
exc.message.should == status.to_s
end

it "accepts a message only" do
exc = SystemExit.new("message")
exc.status.should == 0
Expand Down
16 changes: 16 additions & 0 deletions spec/ruby/shared/process/exit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
}
end

it "converts an argument that is not an Object with #to_int" do
obj = BasicObject.new
def obj.to_int
5
end
-> { @object.exit(obj) }.should.raise(SystemExit) { |e|
e.status.should == 5
}
end

it "converts the passed Float argument to an Integer" do
{ -2.2 => -2, -0.1 => 0, 5.5 => 5, 827.999 => 827 }.each do |value, status|
-> { @object.exit(value) }.should.raise(SystemExit) { |e|
Expand Down Expand Up @@ -86,6 +96,12 @@
$?.exitstatus.should == 21
end

it "converts an argument that is not an Object with #to_int" do
out = ruby_exe("status = BasicObject.new; def status.to_int; 21; end; #{@object}.send(:exit!, status)", args: '2>&1', exit_status: 21)
out.should == ""
$?.exitstatus.should == 21
end

it "exits when called from a thread" do
out = ruby_exe("Thread.new { #{@object}.send(:exit!, 21) }.join; sleep", args: '2>&1', exit_status: 21)
out.should == ""
Expand Down
Loading