Skip to content

Commit cb4c675

Browse files
authored
os: add support for signal handling on JS backend (#12818)
1 parent d5c0bdf commit cb4c675

File tree

5 files changed

+184
-42
lines changed

5 files changed

+184
-42
lines changed

vlib/os/os_js.js.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,7 @@ pub fn getuid() int {
136136
pub fn execvp(cmd string, args []string) ? {
137137
panic('os.execvp() is not available on JS backend')
138138
}
139+
140+
pub fn stdin_resume() {
141+
#$process.stdin.resume();
142+
}

vlib/os/process.js.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ pub fn (mut p Process) stdin_write(s string) {
9494
#p.val.pid.stdin.write(s)
9595
}
9696

97+
pub fn (mut p Process) stdin_resume() {
98+
#p.val.pid.stdin.resume()
99+
}
100+
97101
// todo(playX): probably does not work
98102

99103
// will read from stdout pipe, will only return when EOF (end of file) or data

vlib/os/signal.c.v

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,6 @@ module os
22

33
#include <signal.h>
44

5-
// os.Signal - enumerate possible POSIX signals and
6-
// their integer codes.
7-
// NB: the integer codes are given here explicitly,
8-
// to make it easier to lookup, without needing to
9-
// consult man pages / signal.h .
10-
11-
pub enum Signal {
12-
hup = 1
13-
int = 2
14-
quit = 3
15-
ill = 4
16-
trap = 5
17-
abrt = 6
18-
bus = 7
19-
fpe = 8
20-
kill = 9
21-
usr1 = 10
22-
segv = 11
23-
usr2 = 12
24-
pipe = 13
25-
alrm = 14
26-
term = 15
27-
stkflt = 16
28-
chld = 17
29-
cont = 18
30-
stop = 19
31-
tstp = 20
32-
ttin = 21
33-
ttou = 22
34-
urg = 23
35-
xcpu = 24
36-
xfsz = 25
37-
vtalrm = 26
38-
prof = 27
39-
winch = 28
40-
poll = 29
41-
pwr = 30
42-
sys = 31
43-
}
44-
45-
type SignalHandler = fn (Signal)
46-
475
fn C.signal(signal int, handlercb SignalHandler) voidptr
486

497
// signal will assign `handler` callback to be called when `signum` signal is received.

vlib/os/signal.js.v

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
module os
2+
3+
fn signal_str(signal Signal) string {
4+
mut result := signal.str().to_upper()
5+
result = 'SIG$result'
6+
return result
7+
}
8+
9+
fn signal_from_str(str JS.String) Signal {
10+
s := string(str)
11+
return match s {
12+
'SIGHUP' {
13+
Signal.hup
14+
}
15+
'SIGINT' {
16+
Signal.int
17+
}
18+
'SIGQUIT' {
19+
Signal.quit
20+
}
21+
'SIGILL' {
22+
Signal.ill
23+
}
24+
'SIGTRAP' {
25+
Signal.trap
26+
}
27+
'SIGABRT' {
28+
Signal.abrt
29+
}
30+
'SIGBUS' {
31+
Signal.bus
32+
}
33+
'SIGFPE' {
34+
Signal.fpe
35+
}
36+
'SIGKILL' {
37+
Signal.kill
38+
}
39+
'SIGUSR1' {
40+
Signal.usr1
41+
}
42+
'SIGSEGV' {
43+
Signal.segv
44+
}
45+
'SIGUSR2' {
46+
Signal.usr2
47+
}
48+
'SIGPIPE' {
49+
Signal.pipe
50+
}
51+
'SIGALRM' {
52+
Signal.alrm
53+
}
54+
'SIGTERM' {
55+
Signal.term
56+
}
57+
'SIGSTKFLT' {
58+
Signal.stkflt
59+
}
60+
'SIGCHLD' {
61+
Signal.chld
62+
}
63+
'SIGCONT' {
64+
Signal.cont
65+
}
66+
'SIGSTOP' {
67+
Signal.stop
68+
}
69+
'SIGTSTP' {
70+
Signal.tstp
71+
}
72+
'SIGTTIN' {
73+
Signal.ttin
74+
}
75+
'SIGTTOU' {
76+
Signal.ttou
77+
}
78+
'SIGURG' {
79+
Signal.urg
80+
}
81+
'SIGXCPU' {
82+
Signal.xcpu
83+
}
84+
'SIGXFSZ' {
85+
Signal.xfsz
86+
}
87+
'SIGVTALRM' {
88+
Signal.vtalrm
89+
}
90+
'SIGPROF' {
91+
Signal.prof
92+
}
93+
'SIGWINCH' {
94+
Signal.winch
95+
}
96+
'SIGPOLL' {
97+
Signal.poll
98+
}
99+
'SIGPWR' {
100+
Signal.pwr
101+
}
102+
'SIGSYS' {
103+
Signal.sys
104+
}
105+
else {
106+
panic('unknown signal: $s')
107+
}
108+
}
109+
}
110+
111+
// signal will assign `handler` callback to be called when `signum` signal is received.
112+
//
113+
// # Behaviour on different backends:
114+
// - NodeJS: Will use `process.on` and add `handler` to the listeners list for `signum` to happen
115+
// - Browser: Will use `window.addEventListener` for handling signal
116+
//
117+
// TODO: Add signal events implementation for browser backend
118+
pub fn signal_opt(signum Signal, handler SignalHandler) ?SignalHandler {
119+
signame := signal_str(signum)
120+
_ := signame
121+
$if js_node {
122+
#$process.on(signame.str,function (sig) { handler(os__signal_from_str(sig));});
123+
124+
return handler
125+
} $else $if js_browser {
126+
#let event = new CustomEvent(signame.str, {detail: signum});
127+
#window.addEventListener(signame.str, function (e) { handler(e.detail); });
128+
129+
return handler
130+
} $else {
131+
return error('signal handlers are not supported on bare JS')
132+
}
133+
}

vlib/os/signal.v

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module os
2+
3+
// os.Signal - enumerate possible POSIX signals and
4+
// their integer codes.
5+
// NB: the integer codes are given here explicitly,
6+
// to make it easier to lookup, without needing to
7+
// consult man pages / signal.h .
8+
9+
pub enum Signal {
10+
hup = 1
11+
int = 2
12+
quit = 3
13+
ill = 4
14+
trap = 5
15+
abrt = 6
16+
bus = 7
17+
fpe = 8
18+
kill = 9
19+
usr1 = 10
20+
segv = 11
21+
usr2 = 12
22+
pipe = 13
23+
alrm = 14
24+
term = 15
25+
stkflt = 16
26+
chld = 17
27+
cont = 18
28+
stop = 19
29+
tstp = 20
30+
ttin = 21
31+
ttou = 22
32+
urg = 23
33+
xcpu = 24
34+
xfsz = 25
35+
vtalrm = 26
36+
prof = 27
37+
winch = 28
38+
poll = 29
39+
pwr = 30
40+
sys = 31
41+
}
42+
43+
type SignalHandler = fn (Signal)

0 commit comments

Comments
 (0)