Skip to content

Commit d54d719

Browse files
committed
Add rudimentary signal workaround.
This is the best we can do considering the mechanism of supporting signal() on the JVM. The supported signals could probably be expanded to "everything that ends the receiving process", but I don't know enough about POSIX signals to decide which signals belong into that group.
1 parent 9f8a952 commit d54d719

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,10 @@ my %const_map := nqp::hash(
20142014
'PIPE_IGNORE_ERR', 128,
20152015
'PIPE_CAPTURE_ERR', 256,
20162016

2017+
# could probably support a few more...
2018+
'SIG_INT', 2,
2019+
'SIG_KILL', 9,
2020+
20172021
'TYPE_CHECK_CACHE_DEFINITIVE', 0,
20182022
'TYPE_CHECK_CACHE_THEN_METHOD', 1,
20192023
'TYPE_CHECK_NEEDS_ACCEPTS', 2,
@@ -2723,7 +2727,7 @@ QAST::OperationsJAST.map_classlib_core_op('queuepoll', $TYPE_OPS, 'queuepoll', [
27232727
# asynchrony related ops
27242728
QAST::OperationsJAST.map_classlib_core_op('timer', $TYPE_OPS, 'timer', [$RT_OBJ, $RT_OBJ, $RT_INT, $RT_INT, $RT_OBJ], $RT_OBJ, :tc);
27252729
QAST::OperationsJAST.map_classlib_core_op('cancel', $TYPE_OPS, 'cancel', [$RT_OBJ], $RT_OBJ, :tc);
2726-
QAST::OperationsJAST.map_classlib_core_op('signal', $TYPE_IO_OPS, 'signal', [$RT_OBJ, $RT_OBJ, $RT_INT, $RT_INT, $RT_OBJ], $RT_OBJ, :tc);
2730+
QAST::OperationsJAST.map_classlib_core_op('signal', $TYPE_IO_OPS, 'signal', [$RT_OBJ, $RT_OBJ, $RT_INT, $RT_OBJ], $RT_OBJ, :tc);
27272731
QAST::OperationsJAST.map_classlib_core_op('watchfile', $TYPE_IO_OPS, 'watchfile', [$RT_OBJ, $RT_OBJ, $RT_STR, $RT_OBJ], $RT_OBJ, :tc);
27282732
QAST::OperationsJAST.map_classlib_core_op('asyncconnect', $TYPE_IO_OPS, 'asyncconnect', [$RT_OBJ, $RT_OBJ, $RT_STR, $RT_INT, $RT_OBJ], $RT_OBJ, :tc);
27292733
QAST::OperationsJAST.map_classlib_core_op('asynclisten', $TYPE_IO_OPS, 'asynclisten', [$RT_OBJ, $RT_OBJ, $RT_STR, $RT_INT, $RT_INT, $RT_OBJ], $RT_OBJ, :tc);

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,44 @@
55
import org.perl6.nqp.sixmodel.SixModelObject;
66
import org.perl6.nqp.sixmodel.reprs.AsyncTaskInstance;
77
import org.perl6.nqp.sixmodel.reprs.IOHandleInstance;
8+
import org.perl6.nqp.runtime.Ops;
89

910
public final class IOOps {
1011

12+
private static class SigRunnable implements Runnable {
13+
public static ThreadContext tc;
14+
public static SixModelObject schedulee;
15+
public static long signum;
16+
17+
public SigRunnable(ThreadContext tc, SixModelObject schedulee, long signum) {
18+
this.tc = tc;
19+
this.schedulee = schedulee;
20+
this.signum = signum;
21+
}
22+
23+
public void run() {
24+
Ops.invokeDirect(tc, schedulee, Ops.invocantCallSite,
25+
new Object[] { Ops.box_i(signum, tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.intBoxType, tc) });
26+
}
27+
}
1128
public static SixModelObject signal(SixModelObject queue, SixModelObject schedulee,
1229
long signalNum, SixModelObject asyncType, ThreadContext tc) {
13-
throw new UnsupportedOperationException("signal is not available on JVM.");
30+
switch((int)signalNum) {
31+
case 2: // INT
32+
break;
33+
case 9: // KILL
34+
break;
35+
default:
36+
ExceptionHandling.dieInternal(tc, "Unsupported signal " + signalNum);
37+
}
38+
AsyncTaskInstance task = (AsyncTaskInstance) asyncType.st.REPR.allocate(tc, asyncType.st);
39+
task.queue = queue;
40+
task.schedulee = schedulee;
41+
42+
Runnable sigrun = new SigRunnable(tc, schedulee, signalNum);
43+
Runtime.getRuntime().addShutdownHook(new Thread(sigrun));
44+
return task;
45+
// throw new UnsupportedOperationException("signal is not available on JVM.");
1446
}
1547

1648
public static SixModelObject watchfile(SixModelObject queue, SixModelObject schedulee,

0 commit comments

Comments
 (0)