Skip to content

Commit 9d7ed37

Browse files
committed
[Truffle] Refactor Errno handling node a bit.
1 parent e24b79b commit 9d7ed37

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

truffle/src/main/java/org/jruby/truffle/nodes/rubinius/ExceptionPrimitiveNodes.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ public abstract class ExceptionPrimitiveNodes {
2525
@RubiniusPrimitive(name = "exception_errno_error", needsSelf = false)
2626
public static abstract class ExceptionErrnoErrorPrimitiveNode extends RubiniusPrimitiveNode {
2727

28-
protected final int ENOENT = Errno.ENOENT.intValue();
29-
protected final int EBADF = Errno.EBADF.intValue();
30-
protected final int EEXIST = Errno.EEXIST.intValue();
31-
protected final int EACCES = Errno.EACCES.intValue();
32-
protected final int ENOTDIR = Errno.ENOTDIR.intValue();
28+
// If you add a constant here, add it below in isExceptionSupported() too.
29+
protected final static int ENOENT = Errno.ENOENT.intValue();
30+
protected final static int EBADF = Errno.EBADF.intValue();
31+
protected final static int EEXIST = Errno.EEXIST.intValue();
32+
protected final static int EACCES = Errno.EACCES.intValue();
33+
protected final static int ENOTDIR = Errno.ENOTDIR.intValue();
34+
35+
public static boolean isExceptionSupported(int errno) {
36+
return errno == ENOENT || errno == EBADF || errno == EEXIST || errno == EACCES || errno == ENOTDIR;
37+
}
3338

3439
public ExceptionErrnoErrorPrimitiveNode(RubyContext context, SourceSection sourceSection) {
3540
super(context, sourceSection);
@@ -40,12 +45,12 @@ public RubyException enoent(RubyString message, int errno) {
4045
return getContext().getCoreLibrary().fileNotFoundError(message.toString(), this);
4146
}
4247

43-
@Specialization(guards = {"isNil(message)", "errno == ENOENT"})
48+
@Specialization(guards = { "errno == ENOENT", "isNil(message)" })
4449
public RubyException enoent(Object message, int errno) {
4550
return getContext().getCoreLibrary().fileNotFoundError("nil", this);
4651
}
4752

48-
@Specialization(guards = {"isNil(message)", "errno == EBADF"})
53+
@Specialization(guards = { "errno == EBADF", "isNil(message)" })
4954
public RubyException ebadf(Object message, int errno) {
5055
return getContext().getCoreLibrary().badFileDescriptor(this);
5156
}
@@ -55,7 +60,7 @@ public RubyException eexist(RubyString message, int errno) {
5560
return getContext().getCoreLibrary().fileExistsError(message.toString(), this);
5661
}
5762

58-
@Specialization(guards = {"isNil(message)", "errno == EEXIST"})
63+
@Specialization(guards = { "errno == EEXIST", "isNil(message)" })
5964
public RubyException eexist(Object message, int errno) {
6065
return getContext().getCoreLibrary().fileExistsError("nil", this);
6166
}
@@ -72,32 +77,25 @@ public RubyException enotdir(RubyString message, int errno) {
7277

7378
@CompilerDirectives.TruffleBoundary
7479
@Specialization(guards = "!isExceptionSupported(errno)")
75-
public RubyException unsupported(RubyString message, int errno) {
80+
public RubyException unsupported(Object message, int errno) {
7681
final Errno errnoObject = Errno.valueOf(errno);
7782

78-
if (errnoObject == null) {
79-
throw new UnsupportedOperationException("errno: " + errno + " " + message);
83+
final String messageString;
84+
if (message instanceof RubyString) {
85+
messageString = message.toString();
86+
} else if (message == nil()) {
87+
messageString = "nil";
8088
} else {
81-
throw new UnsupportedOperationException("errno: " + errnoObject.name());
89+
messageString = "unsupported message type";
8290
}
83-
}
84-
85-
@CompilerDirectives.TruffleBoundary
86-
@Specialization(guards = {"isNil(message)", "!isExceptionSupported(errno)"})
87-
public RubyException unsupported(Object message, int errno) {
88-
final Errno errnoObject = Errno.valueOf(errno);
8991

9092
if (errnoObject == null) {
91-
throw new UnsupportedOperationException("errno: " + errno + " nil");
93+
throw new UnsupportedOperationException("errno: " + errno + " " + messageString);
9294
} else {
9395
throw new UnsupportedOperationException("errno: " + errnoObject.name());
9496
}
9597
}
9698

97-
public static boolean isExceptionSupported(int errno) {
98-
return Errno.ENOENT.intValue() == errno || Errno.EBADF.intValue() == errno || Errno.EEXIST.intValue() == errno || Errno.EACCES.intValue() == errno;
99-
}
100-
10199
}
102100

103101
}

0 commit comments

Comments
 (0)