Skip to content

Commit

Permalink
Use Define API for all socket constants
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Feb 12, 2025
1 parent 24a6c33 commit a0e93d5
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 27 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/ext/socket/Addrinfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import static org.jruby.api.Convert.asFixnum;
import static org.jruby.api.Convert.toInt;
import static org.jruby.api.Create.*;
import static org.jruby.api.Define.defineClass;
import static org.jruby.ext.socket.SocketUtils.IP_V4_MAPPED_ADDRESS_PREFIX;
import static org.jruby.ext.socket.SocketUtils.sockerr;

Expand All @@ -64,7 +65,7 @@ public class Addrinfo extends RubyObject {
final byte PACKET_HOST = 0; // host packet type (if_packet.h)

public static void createAddrinfo(ThreadContext context, RubyClass Object) {
Define.defineClass(context, "Addrinfo", Object, Addrinfo::new).defineMethods(context, Addrinfo.class);
defineClass(context, "Addrinfo", Object, Addrinfo::new).defineMethods(context, Addrinfo.class);
}

public Addrinfo(Ruby runtime, RubyClass cls) {
Expand Down
7 changes: 3 additions & 4 deletions core/src/main/java/org/jruby/ext/socket/RubyTCPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@
*/
@JRubyClass(name="TCPServer", parent="TCPSocket")
public class RubyTCPServer extends RubyTCPSocket {
static void createTCPServer(ThreadContext context, RubyClass TCPSocket, RubyClass Object) {
Object.defineConstant(context, "TCPServer",
defineClass(context, "TCPServer", TCPSocket, RubyTCPServer::new).
defineMethods(context, RubyTCPServer.class));
static void createTCPServer(ThreadContext context, RubyClass TCPSocket) {
defineClass(context, "TCPServer", TCPSocket, RubyTCPServer::new).
defineMethods(context, RubyTCPServer.class);
}

public RubyTCPServer(Ruby runtime, RubyClass type) {
Expand Down
7 changes: 3 additions & 4 deletions core/src/main/java/org/jruby/ext/socket/RubyTCPSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@
import static org.jruby.api.Define.defineClass;

public class RubyTCPSocket extends RubyIPSocket {
static RubyClass createTCPSocket(ThreadContext context, RubyClass IPSocket, RubyClass Object) {
return (RubyClass) Object.setConstant(context, "TCPsocket",
defineClass(context, "TCPSocket", IPSocket, RubyTCPSocket::new).
defineMethods(context, RubyTCPSocket.class));
static RubyClass createTCPSocket(ThreadContext context, RubyClass IPSocket) {
return defineClass(context, "TCPSocket", IPSocket, RubyTCPSocket::new).
defineMethods(context, RubyTCPSocket.class);
}

public RubyTCPSocket(Ruby runtime, RubyClass type) {
Expand Down
9 changes: 4 additions & 5 deletions core/src/main/java/org/jruby/ext/socket/RubyUDPSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ public class RubyUDPSocket extends RubyIPSocket {

public static final double RECV_BUFFER_COPY_SCALE = 1.5;

static void createUDPSocket(ThreadContext context, RubyClass IPSocket, RubyClass Socket, RubyClass Object) {
Object.defineConstant(context, "UDPsocket",
defineClass(context, "UDPSocket", IPSocket, RubyUDPSocket::new).
include(context, (RubyModule) Socket.getConstant(context, "Constants")).
defineMethods(context, RubyUDPSocket.class));
static void createUDPSocket(ThreadContext context, RubyClass IPSocket, RubyClass Socket) {
defineClass(context, "UDPSocket", IPSocket, RubyUDPSocket::new).
include(context, (RubyModule) Socket.getConstant(context, "Constants")).
defineMethods(context, RubyUDPSocket.class);
}

public RubyUDPSocket(Ruby runtime, RubyClass type) {
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/ext/socket/RubyUNIXServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.api.Access;
import org.jruby.api.Define;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
Expand All @@ -54,10 +55,9 @@

@JRubyClass(name="UNIXServer", parent="UNIXSocket")
public class RubyUNIXServer extends RubyUNIXSocket {
static void createUNIXServer(ThreadContext context, RubyClass UNIXSocket, RubyClass Object) {
Object.defineConstant(context, "UNIXserver",
defineClass(context, "UNIXServer", UNIXSocket, RubyUNIXServer::new).
defineMethods(context, RubyUNIXServer.class));
static void createUNIXServer(ThreadContext context, RubyClass UNIXSocket) {
defineClass(context, "UNIXServer", UNIXSocket, RubyUNIXServer::new).
defineMethods(context, RubyUNIXServer.class);
}

public RubyUNIXServer(Ruby runtime, RubyClass type) {
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.api.Access;
import org.jruby.api.Define;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
Expand Down Expand Up @@ -80,10 +81,9 @@

@JRubyClass(name="UNIXSocket", parent="BasicSocket")
public class RubyUNIXSocket extends RubyBasicSocket {
static RubyClass createUNIXSocket(ThreadContext context, RubyClass BasicSocket, RubyClass Object) {
return (RubyClass) Object.setConstant(context, "UNIXsocket",
defineClass(context, "UNIXSocket", BasicSocket, RubyUNIXSocket::new).
defineMethods(context, RubyUNIXSocket.class));
static RubyClass createUNIXSocket(ThreadContext context, RubyClass BasicSocket) {
return Define.defineClass(context, "UNIXSocket", BasicSocket, RubyUNIXSocket::new).
defineMethods(context, RubyUNIXSocket.class);
}

public RubyUNIXSocket(Ruby runtime, RubyClass type) {
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/org/jruby/ext/socket/SocketLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public void load(final Ruby runtime, boolean wrap) throws IOException {
RubyServerSocket.createServerSocket(context, Socket);

if (instanceConfig(context).isNativeEnabled() && !Platform.IS_WINDOWS) {
var UNIXSocket = RubyUNIXSocket.createUNIXSocket(context, BasicSocket, Object);
RubyUNIXServer.createUNIXServer(context, UNIXSocket, Object);
var UNIXSocket = RubyUNIXSocket.createUNIXSocket(context, BasicSocket);
RubyUNIXServer.createUNIXServer(context, UNIXSocket);
}

var IPSocket = RubyIPSocket.createIPSocket(context, BasicSocket);
var TCPSocket = RubyTCPSocket.createTCPSocket(context, IPSocket, Object);
RubyTCPServer.createTCPServer(context, TCPSocket, Object);
RubyUDPSocket.createUDPSocket(context, IPSocket, Socket, Object);
var TCPSocket = RubyTCPSocket.createTCPSocket(context, IPSocket);
RubyTCPServer.createTCPServer(context, TCPSocket);
RubyUDPSocket.createUDPSocket(context, IPSocket, Socket);

Addrinfo.createAddrinfo(context, Object);
Option.createOption(context, Object, Socket);
Expand Down

0 comments on commit a0e93d5

Please sign in to comment.