Skip to content

Commit a7fa588

Browse files
committed
print warning and return error instead of panic if syscalls not available (closes #32)
1 parent 95213f3 commit a7fa588

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

translator/natives/syscall/syscall_unix.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ func init() {
2121
}
2222

2323
var syscallModule js.Object
24+
var alreadyTriedToLoad = false
2425

2526
func syscall(name string) js.Object {
2627
defer func() {
2728
if err := recover(); err != nil {
28-
panic("system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md")
29+
println("warning: system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md")
2930
}
3031
}()
3132
if syscallModule == nil {
33+
if alreadyTriedToLoad {
34+
return nil
35+
}
36+
alreadyTriedToLoad = true
3237
require := js.Global.Get("require")
3338
if require.IsUndefined() {
3439
syscallHandler := js.Global.Get("$syscall")
@@ -43,23 +48,35 @@ func syscall(name string) js.Object {
4348
}
4449

4550
func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
46-
r := syscall("Syscall").Invoke(trap, a1, a2, a3)
47-
return uintptr(r.Index(0).Int()), uintptr(r.Index(1).Int()), Errno(r.Index(2).Int())
51+
if f := syscall("Syscall"); f != nil {
52+
r := f.Invoke(trap, a1, a2, a3)
53+
return uintptr(r.Index(0).Int()), uintptr(r.Index(1).Int()), Errno(r.Index(2).Int())
54+
}
55+
return 0, 0, EACCES
4856
}
4957

5058
func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
51-
r := syscall("Syscall6").Invoke(trap, a1, a2, a3, a4, a5, a6)
52-
return uintptr(r.Index(0).Int()), uintptr(r.Index(1).Int()), Errno(r.Index(2).Int())
59+
if f := syscall("Syscall6"); f != nil {
60+
r := f.Invoke(trap, a1, a2, a3, a4, a5, a6)
61+
return uintptr(r.Index(0).Int()), uintptr(r.Index(1).Int()), Errno(r.Index(2).Int())
62+
}
63+
return 0, 0, EACCES
5364
}
5465

5566
func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
56-
r := syscall("Syscall").Invoke(trap, a1, a2, a3)
57-
return uintptr(r.Index(0).Int()), uintptr(r.Index(1).Int()), Errno(r.Index(2).Int())
67+
if f := syscall("Syscall"); f != nil {
68+
r := f.Invoke(trap, a1, a2, a3)
69+
return uintptr(r.Index(0).Int()), uintptr(r.Index(1).Int()), Errno(r.Index(2).Int())
70+
}
71+
return 0, 0, EACCES
5872
}
5973

6074
func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
61-
r := syscall("Syscall6").Invoke(trap, a1, a2, a3, a4, a5, a6)
62-
return uintptr(r.Index(0).Int()), uintptr(r.Index(1).Int()), Errno(r.Index(2).Int())
75+
if f := syscall("Syscall6"); f != nil {
76+
r := f.Invoke(trap, a1, a2, a3, a4, a5, a6)
77+
return uintptr(r.Index(0).Int()), uintptr(r.Index(1).Int()), Errno(r.Index(2).Int())
78+
}
79+
return 0, 0, EACCES
6380
}
6481

6582
func BytePtrFromString(s string) (*byte, error) {

translator/natives/syscall/syscall_windows.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,48 @@
22

33
package syscall
44

5+
var warningPrinted = false
6+
7+
func printWarning() {
8+
if !warningPrinted {
9+
println("warning: system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md")
10+
}
11+
warningPrinted = true
12+
}
13+
514
func Syscall(trap, nargs, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
6-
panic("system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md")
15+
printWarning()
16+
return 0, 0, EACCES
717
}
818

919
func Syscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
10-
panic("system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md")
20+
printWarning()
21+
return 0, 0, EACCES
1122
}
1223

1324
func Syscall9(trap, nargs, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err Errno) {
14-
panic("system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md")
25+
printWarning()
26+
return 0, 0, EACCES
1527
}
1628

1729
func Syscall12(trap, nargs, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12 uintptr) (r1, r2 uintptr, err Errno) {
18-
panic("system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md")
30+
printWarning()
31+
return 0, 0, EACCES
1932
}
2033

2134
func Syscall15(trap, nargs, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 uintptr) (r1, r2 uintptr, err Errno) {
22-
panic("system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md")
35+
printWarning()
36+
return 0, 0, EACCES
2337
}
2438

2539
func loadlibrary(filename *uint16) (handle uintptr, err Errno) {
26-
panic("system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md")
40+
printWarning()
41+
return 0, EACCES
2742
}
2843

2944
func getprocaddress(handle uintptr, procname *uint8) (proc uintptr, err Errno) {
30-
panic("system calls not available, see https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md")
45+
printWarning()
46+
return 0, EACCES
3147
}
3248

3349
func getStdHandle(h int) (fd Handle) {

0 commit comments

Comments
 (0)