Skip to content

Commit da80b36

Browse files
committed
Make socket family handling portable
MoarVM requires that the socket family be a separate parameter for nqp::bind_sk and nqp::connect_sk from the port; update the JVM accordingly and use the same platform-independent constants here as in MoarVM. Related to rakudo/rakudo#3007
1 parent 4c5ec49 commit da80b36

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,8 +2218,8 @@ QAST::OperationsJAST.map_classlib_core_op('linesasync', $TYPE_OPS, 'linesasync',
22182218
QAST::OperationsJAST.map_classlib_core_op('spurtasync', $TYPE_OPS, 'spurtasync', [$RT_OBJ, $RT_OBJ, $RT_OBJ, $RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
22192219

22202220
QAST::OperationsJAST.map_classlib_core_op('socket', $TYPE_OPS, 'socket', [$RT_INT], $RT_OBJ, :tc);
2221-
QAST::OperationsJAST.map_classlib_core_op('connect', $TYPE_OPS, 'connect', [$RT_OBJ, $RT_STR, $RT_INT], $RT_OBJ, :tc);
2222-
QAST::OperationsJAST.map_classlib_core_op('bindsock', $TYPE_OPS, 'bindsock', [$RT_OBJ, $RT_STR, $RT_INT, $RT_INT], $RT_OBJ, :tc);
2221+
QAST::OperationsJAST.map_classlib_core_op('connect', $TYPE_OPS, 'connect', [$RT_OBJ, $RT_STR, $RT_INT, $RT_INT], $RT_OBJ, :tc);
2222+
QAST::OperationsJAST.map_classlib_core_op('bindsock', $TYPE_OPS, 'bindsock', [$RT_OBJ, $RT_STR, $RT_INT, $RT_INT, $RT_INT], $RT_OBJ, :tc);
22232223
QAST::OperationsJAST.map_classlib_core_op('accept', $TYPE_OPS, 'accept', [$RT_OBJ], $RT_OBJ, :tc);
22242224
QAST::OperationsJAST.map_classlib_core_op('getport', $TYPE_OPS, 'getport', [$RT_OBJ], $RT_INT, :tc);
22252225

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -436,25 +436,62 @@ public static SixModelObject socket(long listener, ThreadContext tc) {
436436
return h;
437437
}
438438

439-
public static SixModelObject connect(SixModelObject obj, String host, long port, ThreadContext tc) {
439+
public static final int SOCKET_FAMILY_UNSPEC = 0;
440+
public static final int SOCKET_FAMILY_INET = 1;
441+
public static final int SOCKET_FAMILY_INET6 = 2;
442+
public static final int SOCKET_FAMILY_UNIX = 3;
443+
444+
public static SixModelObject connect(SixModelObject obj, String host, long port, long family, ThreadContext tc) {
440445
IOHandleInstance h = (IOHandleInstance)obj;
441-
if (h.handle instanceof SocketHandle) {
442-
((SocketHandle)h.handle).connect(tc, host, (int) port);
443-
} else {
444-
ExceptionHandling.dieInternal(tc,
445-
"This handle does not support connect");
446-
}
446+
447+
switch ((int) family) {
448+
case SOCKET_FAMILY_UNSPEC:
449+
case SOCKET_FAMILY_INET:
450+
case SOCKET_FAMILY_INET6:
451+
if (h.handle instanceof SocketHandle) {
452+
((SocketHandle)h.handle).connect(tc, host, (int) port);
453+
} else {
454+
ExceptionHandling.dieInternal(tc,
455+
"This handle does not support connect");
456+
}
457+
break;
458+
case SOCKET_FAMILY_UNIX:
459+
ExceptionHandling.dieInternal(tc,
460+
"UNIX sockets are not supported on the JVM");
461+
break;
462+
default:
463+
ExceptionHandling.dieInternal(tc,
464+
"Unsupported socket family: " + Long.toString(family));
465+
break;
466+
}
467+
447468
return obj;
448469
}
449470

450-
public static SixModelObject bindsock(SixModelObject obj, String host, long port, long backlog, ThreadContext tc) {
471+
public static SixModelObject bindsock(SixModelObject obj, String host, long port, long family, long backlog, ThreadContext tc) {
451472
IOHandleInstance h = (IOHandleInstance)obj;
452-
if (h.handle instanceof IIOBindable) {
453-
((IIOBindable)h.handle).bind(tc, host, (int) port, (int)backlog);
454-
} else {
455-
ExceptionHandling.dieInternal(tc,
456-
"This handle does not support bind");
457-
}
473+
474+
switch ((int) family) {
475+
case SOCKET_FAMILY_UNSPEC:
476+
case SOCKET_FAMILY_INET:
477+
case SOCKET_FAMILY_INET6:
478+
if (h.handle instanceof IIOBindable) {
479+
((IIOBindable)h.handle).bind(tc, host, (int) port, (int) backlog);
480+
} else {
481+
ExceptionHandling.dieInternal(tc,
482+
"This handle does not support bind");
483+
}
484+
break;
485+
case SOCKET_FAMILY_UNIX:
486+
ExceptionHandling.dieInternal(tc,
487+
"UNIX sockets are not supported on the JVM");
488+
break;
489+
default:
490+
ExceptionHandling.dieInternal(tc,
491+
"Unsupported socket family: " + Long.toString(family));
492+
break;
493+
}
494+
458495
return obj;
459496
}
460497

0 commit comments

Comments
 (0)