Skip to content
Merged
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
45 changes: 29 additions & 16 deletions core/src/main/java/org/jruby/RubySignal.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,19 @@ public static void createSignal(Ruby runtime) {
mSignal.defineAnnotatedMethods(RubySignal.class);
//registerThreadDumpSignalHandler(runtime);
}

public static Map<String, Integer> list() {
Map<String, Integer> signals = new HashMap<>();

private static final String SIGNAME_PREFIX = "SIG";
private static final Map<String, Integer> SIGNAME_MAP;
private static final Map<Integer, String> SIGNUM_MAP;

static {
HashMap<String, Integer> signals = new HashMap<>();
HashMap<Integer, String> signums = new HashMap<>();

for (Signal s : Signal.values()) {
// Skip signals not defined on this platform
if (!s.defined()) continue;

String desc = s.description();
if (!desc.startsWith(SIGNAME_PREFIX)) continue;

Expand All @@ -90,9 +98,20 @@ public static Map<String, Integer> list() {
if (signo >= 20000) continue;

signals.put(desc, signo);
signums.put(signo, desc);
}

// We always define KILL as 9 on Windows
if (Platform.IS_WINDOWS) {
signals.put("KILL", 9);
}

return signals;
SIGNAME_MAP = Collections.unmodifiableMap(signals);
SIGNUM_MAP = Collections.unmodifiableMap(signums);
}

public static Map<String, Integer> list() {
return SIGNAME_MAP;
}

@JRubyMethod(meta = true)
Expand Down Expand Up @@ -153,20 +172,16 @@ public static IRubyObject signame(ThreadContext context, final IRubyObject recv,

// MRI: signo2signm
public static String signo2signm(long no) {
for (Signal s : Signal.values()) {
if (s.intValue() == no) {
return signmWithoutPrefix(s.name());
}
}
return null;
return SIGNUM_MAP.get((int) no);
}

// MRI: signm2signo
public static long signm2signo(String nm) {
for (Signal s : Signal.values()) {
if (signmWithoutPrefix(s.name()).equals(nm)) return s.longValue();
}
return 0;
Integer signo = SIGNAME_MAP.get(nm);

if (signo == null) return 0;

return signo;
}

public static String signmWithPrefix(String nm) {
Expand Down Expand Up @@ -229,6 +244,4 @@ private static boolean SIGNAME(final String name) {
}
}

private static final String SIGNAME_PREFIX = "SIG";

}
4 changes: 4 additions & 0 deletions spec/ruby/core/signal/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@
it "includes the EXIT key with a value of zero" do
Signal.list["EXIT"].should == 0
end

it "includes the KILL key with a value of nine" do
Signal.list["KILL"].should == 9
end
end