Skip to content

Commit 8dd6234

Browse files
authored
Merge pull request #1543 from visualfc/syscall_error
runtime/internal/clite/syscall: remove deps errors
2 parents 34b872c + f43a8c4 commit 8dd6234

9 files changed

Lines changed: 54 additions & 119 deletions

File tree

runtime/internal/clite/syscall/syscall.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package syscall
1818

1919
import (
20-
errorsPkg "errors"
2120
_ "unsafe"
2221

2322
c "github.com/goplus/llgo/runtime/internal/clite"
@@ -27,15 +26,6 @@ const (
2726
LLGoPackage = true
2827
)
2928

30-
var (
31-
ErrInvalid = errorsPkg.New("invalid argument")
32-
ErrPermission = errorsPkg.New("permission denied")
33-
ErrExist = errorsPkg.New("file already exists")
34-
ErrNotExist = errorsPkg.New("file does not exist")
35-
ErrClosed = errorsPkg.New("file already closed")
36-
ErrUnsupported = errorsPkg.New("operation not supported")
37-
)
38-
3929
// Nano returns the time stored in ts as nanoseconds.
4030
func (ts *Timespec) Nano() int64 {
4131
return int64(ts.Sec)*1e9 + int64(ts.Nsec)
@@ -59,7 +49,7 @@ func (tv *Timeval) Unix() (sec int64, nsec int64) {
5949
//go:linkname c_getpid C.getpid
6050
func c_getpid() c.Int
6151

62-
func Kill(pid int, signum Signal) error {
52+
func Kill(pid int, signum Signal) Errno {
6353
// WASI does not have the notion of processes nor signal handlers.
6454
//
6555
// Any signal that the application raises to the process itself will
@@ -68,7 +58,7 @@ func Kill(pid int, signum Signal) error {
6858
return ESRCH
6959
}
7060
ProcExit(128 + int32(signum))
71-
return nil
61+
return 0
7262
}
7363

7464
func ProcExit(code int32) {

runtime/internal/clite/syscall/syscall_unix.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package syscall
44

55
type Errno uintptr
66

7-
func (e Errno) Error() string {
7+
func Error(e Errno) string {
88
if 0 <= int(e) && int(e) < len(errors) {
99
s := errors[e]
1010
if s != "" {
@@ -14,28 +14,6 @@ func (e Errno) Error() string {
1414
return "errno " + utoa(uint64(e))
1515
}
1616

17-
func (e Errno) Is(target error) bool {
18-
switch target {
19-
case ErrPermission:
20-
return e == EACCES || e == EPERM
21-
case ErrExist:
22-
return e == EEXIST || e == ENOTEMPTY
23-
case ErrNotExist:
24-
return e == ENOENT
25-
case ErrUnsupported:
26-
return e == ENOSYS || e == ENOTSUP || e == EOPNOTSUPP
27-
}
28-
return false
29-
}
30-
31-
func (e Errno) Temporary() bool {
32-
return e == EINTR || e == EMFILE || e == ENFILE || e.Timeout()
33-
}
34-
35-
func (e Errno) Timeout() bool {
36-
return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
37-
}
38-
3917
// A Signal is a number describing a process signal.
4018
// It implements the [os.Signal] interface.
4119
type Signal int

runtime/internal/clite/syscall/syscall_wasm.go

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func direntNamlen(buf []byte) (uint64, bool) {
7171
// }
7272
type Errno uint32
7373

74-
func (e Errno) Error() string {
74+
func Error(e Errno) string {
7575
if 0 <= int(e) && int(e) < len(errorstr) {
7676
s := errorstr[e]
7777
if s != "" {
@@ -81,28 +81,6 @@ func (e Errno) Error() string {
8181
return "errno " + utoa(uint64(e))
8282
}
8383

84-
func (e Errno) Is(target error) bool {
85-
switch target {
86-
case ErrPermission:
87-
return e == EACCES || e == EPERM
88-
case ErrExist:
89-
return e == EEXIST || e == ENOTEMPTY
90-
case ErrNotExist:
91-
return e == ENOENT
92-
case ErrUnsupported:
93-
return e == ENOSYS
94-
}
95-
return false
96-
}
97-
98-
func (e Errno) Temporary() bool {
99-
return e == EINTR || e == EMFILE || e.Timeout()
100-
}
101-
102-
func (e Errno) Timeout() bool {
103-
return e == EAGAIN || e == ETIMEDOUT
104-
}
105-
10684
// A Signal is a number describing a process signal.
10785
// It implements the [os.Signal] interface.
10886
type Signal uint8
@@ -353,9 +331,9 @@ func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errn
353331
return 0, 0, ENOSYS
354332
}
355333

356-
func Sysctl(key string) (string, error) {
334+
func Sysctl(key string) (string, Errno) {
357335
if key == "kern.hostname" {
358-
return "wasip1", nil
336+
return "wasip1", 0
359337
}
360338
return "", ENOSYS
361339
}
@@ -376,8 +354,8 @@ func Getegid() int {
376354
return 1
377355
}
378356

379-
func Getgroups() ([]int, error) {
380-
return []int{1}, nil
357+
func Getgroups() ([]int, Errno) {
358+
return []int{1}, 0
381359
}
382360

383361
func Getpid() int {
@@ -388,24 +366,24 @@ func Getppid() int {
388366
return 2
389367
}
390368

391-
// func Gettimeofday(tv *Timeval) error {
369+
// func Gettimeofday(tv *Timeval) Errno {
392370
// var time timestamp
393371
// if errno := clock_time_get(clockRealtime, 1e3, &time); errno != 0 {
394372
// return errno
395373
// }
396374
// tv.setTimestamp(time)
397-
// return nil
375+
// return 0
398376
// }
399377

400-
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
378+
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err Errno) {
401379
return 0, ENOSYS
402380
}
403381

404-
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
382+
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err Errno) {
405383
return 0, 0, ENOSYS
406384
}
407385

408-
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
386+
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err Errno) {
409387
return 0, ENOSYS
410388
}
411389

@@ -440,7 +418,7 @@ const (
440418
clockThreadCPUTimeID
441419
)
442420

443-
func SetNonblock(fd int, nonblocking bool) error {
421+
func SetNonblock(fd int, nonblocking bool) Errno {
444422
panic("todo: syscall.SetNonblock")
445423
}
446424

@@ -453,7 +431,7 @@ const (
453431
RLIMIT_NOFILE = iota
454432
)
455433

456-
func Getrlimit(which int, lim *Rlimit) error {
434+
func Getrlimit(which int, lim *Rlimit) Errno {
457435
return ENOSYS
458436
}
459437

runtime/internal/clite/syscall/tables_wasm.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -170,36 +170,3 @@ var errorstr = [...]string{
170170
EXDEV: "Cross-device link",
171171
ENOTCAPABLE: "Capabilities insufficient",
172172
}
173-
174-
// Do the interface allocations only once for common
175-
// Errno values.
176-
var (
177-
errEAGAIN error = EAGAIN
178-
errEINVAL error = EINVAL
179-
errENOENT error = ENOENT
180-
)
181-
182-
// errnoErr returns common boxed Errno values, to prevent
183-
// allocations at runtime.
184-
//
185-
// We set both noinline and nosplit to reduce code size, this function has many
186-
// call sites in the syscall package, inlining it causes a significant increase
187-
// of the compiled code; the function call ultimately does not make a difference
188-
// in the performance of syscall functions since the time is dominated by calls
189-
// to the imports and path resolution.
190-
//
191-
//go:noinline
192-
//go:nosplit
193-
func errnoErr(e Errno) error {
194-
switch e {
195-
case 0:
196-
return nil
197-
case EAGAIN:
198-
return errEAGAIN
199-
case EINVAL:
200-
return errEINVAL
201-
case ENOENT:
202-
return errENOENT
203-
}
204-
return e
205-
}

runtime/internal/lib/internal/oserror/errors.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
package oserror
99

1010
import (
11-
"github.com/goplus/llgo/runtime/internal/clite/syscall"
11+
"errors"
1212
)
1313

1414
// llgo:skipall
1515
type _oserror struct{}
1616

1717
var (
18-
ErrInvalid = syscall.ErrInvalid
19-
ErrPermission = syscall.ErrPermission
20-
ErrExist = syscall.ErrExist
21-
ErrNotExist = syscall.ErrNotExist
22-
ErrClosed = syscall.ErrClosed
18+
ErrInvalid = errors.New("invalid argument")
19+
ErrPermission = errors.New("permission denied")
20+
ErrExist = errors.New("file already exists")
21+
ErrNotExist = errors.New("file does not exist")
22+
ErrClosed = errors.New("file already closed")
2323
)

runtime/internal/lib/os/dir.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (f *File) ReadDir(n int) (dirents []DirEntry, err error) {
121121
// Open directory using file descriptor
122122
dir := os.Fdopendir(c.Int(f.fd))
123123
if dir == nil {
124-
return nil, syscall.Errno(os.Errno())
124+
return nil, origSyscall.Errno(os.Errno())
125125
}
126126
defer os.Closedir(dir)
127127

runtime/internal/lib/syscall/syscall.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,9 @@ func Accept(fd int) (nfd int, sa origSyscall.Sockaddr, err error) {
163163
}
164164

165165
func Kill(pid int, signum Signal) error {
166-
return syscall.Kill(pid, syscall.Signal(signum))
166+
errno := syscall.Kill(pid, syscall.Signal(signum))
167+
if errno == 0 {
168+
return nil
169+
}
170+
return Errno(errno)
167171
}

runtime/internal/lib/syscall/syscall_unix.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
package syscall
88

99
import (
10-
"unsafe"
10+
"errors"
1111

12-
c "github.com/goplus/llgo/runtime/internal/clite"
1312
"github.com/goplus/llgo/runtime/internal/clite/syscall"
1413
"github.com/goplus/llgo/runtime/internal/lib/internal/oserror"
1514
)
@@ -25,8 +24,7 @@ type Errno uintptr
2524
type Dirent = syscall.Dirent
2625

2726
func (e Errno) Error() string {
28-
ret := c.Strerror(c.Int(e))
29-
return unsafe.String((*byte)(unsafe.Pointer(ret)), c.Strlen(ret))
27+
return syscall.Error(syscall.Errno(e))
3028
}
3129

3230
func (e Errno) Is(target error) bool {
@@ -37,9 +35,8 @@ func (e Errno) Is(target error) bool {
3735
return e == Errno(syscall.EEXIST) || e == Errno(syscall.ENOTEMPTY)
3836
case oserror.ErrNotExist:
3937
return e == Errno(syscall.ENOENT)
40-
// TODO(xsw): go1.21
41-
// case errors.ErrUnsupported:
42-
// return e == ENOSYS || e == ENOTSUP || e == EOPNOTSUPP
38+
case errors.ErrUnsupported:
39+
return e == Errno(syscall.ENOSYS) || e == Errno(syscall.ENOTSUP) || e == Errno(syscall.EOPNOTSUPP)
4340
}
4441
return false
4542
}

runtime/internal/lib/syscall/syscall_wasm.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
package syscall
88

99
import (
10+
"errors"
1011
"strconv"
1112

13+
"github.com/goplus/llgo/runtime/internal/lib/internal/oserror"
14+
1215
"github.com/goplus/llgo/runtime/internal/clite/syscall"
1316
)
1417

@@ -63,11 +66,29 @@ type Dirent struct {
6366
type Errno syscall.Errno
6467

6568
func (e Errno) Error() string {
66-
return syscall.Errno(e).Error()
69+
return syscall.Error(syscall.Errno(e))
6770
}
6871

6972
func (e Errno) Is(target error) bool {
70-
return syscall.Errno(e).Is(target)
73+
switch target {
74+
case oserror.ErrPermission:
75+
return e == Errno(syscall.EACCES) || e == Errno(syscall.EPERM)
76+
case oserror.ErrExist:
77+
return e == Errno(syscall.EEXIST) || e == Errno(syscall.ENOTEMPTY)
78+
case oserror.ErrNotExist:
79+
return e == Errno(syscall.ENOENT)
80+
case errors.ErrUnsupported:
81+
return e == Errno(syscall.ENOSYS)
82+
}
83+
return false
84+
}
85+
86+
func (e Errno) Temporary() bool {
87+
return e == Errno(syscall.EINTR) || e == Errno(syscall.EMFILE) || e.Timeout()
88+
}
89+
90+
func (e Errno) Timeout() bool {
91+
return e == Errno(syscall.EAGAIN) || e == Errno(syscall.ETIMEDOUT)
7192
}
7293

7394
// A Signal is a number describing a process signal.

0 commit comments

Comments
 (0)