Skip to content

Commit 60240e9

Browse files
committed
runtime/internal/clite/syscall: remove deps errors
1 parent 34b872c commit 60240e9

9 files changed

Lines changed: 43 additions & 132 deletions

File tree

runtime/internal/clite/syscall/syscall.go

Lines changed: 11 additions & 11 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,14 +26,15 @@ 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-
)
29+
func Error(e Errno) string {
30+
if 0 <= int(e) && int(e) < len(errorstr) {
31+
s := errorstr[e]
32+
if s != "" {
33+
return s
34+
}
35+
}
36+
return "errno " + utoa(uint64(e))
37+
}
3838

3939
// Nano returns the time stored in ts as nanoseconds.
4040
func (ts *Timespec) Nano() int64 {
@@ -59,7 +59,7 @@ func (tv *Timeval) Unix() (sec int64, nsec int64) {
5959
//go:linkname c_getpid C.getpid
6060
func c_getpid() c.Int
6161

62-
func Kill(pid int, signum Signal) error {
62+
func Kill(pid int, signum Signal) Errno {
6363
// WASI does not have the notion of processes nor signal handlers.
6464
//
6565
// Any signal that the application raises to the process itself will
@@ -68,7 +68,7 @@ func Kill(pid int, signum Signal) error {
6868
return ESRCH
6969
}
7070
ProcExit(128 + int32(signum))
71-
return nil
71+
return 0
7272
}
7373

7474
func ProcExit(code int32) {

runtime/internal/clite/syscall/syscall_unix.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,6 @@ package syscall
44

55
type Errno uintptr
66

7-
func (e Errno) Error() string {
8-
if 0 <= int(e) && int(e) < len(errors) {
9-
s := errors[e]
10-
if s != "" {
11-
return s
12-
}
13-
}
14-
return "errno " + utoa(uint64(e))
15-
}
16-
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-
397
// A Signal is a number describing a process signal.
408
// It implements the [os.Signal] interface.
419
type Signal int

runtime/internal/clite/syscall/syscall_wasm.go

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

74-
func (e Errno) Error() string {
75-
if 0 <= int(e) && int(e) < len(errorstr) {
76-
s := errorstr[e]
77-
if s != "" {
78-
return s
79-
}
80-
}
81-
return "errno " + utoa(uint64(e))
82-
}
83-
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-
10674
// A Signal is a number describing a process signal.
10775
// It implements the [os.Signal] interface.
10876
type Signal uint8
@@ -353,7 +321,7 @@ func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errn
353321
return 0, 0, ENOSYS
354322
}
355323

356-
func Sysctl(key string) (string, error) {
324+
func Sysctl(key string) (string, Errno) {
357325
if key == "kern.hostname" {
358326
return "wasip1", nil
359327
}
@@ -376,7 +344,7 @@ func Getegid() int {
376344
return 1
377345
}
378346

379-
func Getgroups() ([]int, error) {
347+
func Getgroups() ([]int, Errno) {
380348
return []int{1}, nil
381349
}
382350

@@ -388,24 +356,24 @@ func Getppid() int {
388356
return 2
389357
}
390358

391-
// func Gettimeofday(tv *Timeval) error {
359+
// func Gettimeofday(tv *Timeval) Errno {
392360
// var time timestamp
393361
// if errno := clock_time_get(clockRealtime, 1e3, &time); errno != 0 {
394362
// return errno
395363
// }
396364
// tv.setTimestamp(time)
397-
// return nil
365+
// return 0
398366
// }
399367

400-
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
368+
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err Errno) {
401369
return 0, ENOSYS
402370
}
403371

404-
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
372+
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err Errno) {
405373
return 0, 0, ENOSYS
406374
}
407375

408-
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
376+
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err Errno) {
409377
return 0, ENOSYS
410378
}
411379

@@ -440,7 +408,7 @@ const (
440408
clockThreadCPUTimeID
441409
)
442410

443-
func SetNonblock(fd int, nonblocking bool) error {
411+
func SetNonblock(fd int, nonblocking bool) Errno {
444412
panic("todo: syscall.SetNonblock")
445413
}
446414

@@ -453,7 +421,7 @@ const (
453421
RLIMIT_NOFILE = iota
454422
)
455423

456-
func Getrlimit(which int, lim *Rlimit) error {
424+
func Getrlimit(which int, lim *Rlimit) Errno {
457425
return ENOSYS
458426
}
459427

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,5 @@ 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+
return Errno(syscall.Kill(pid, syscall.Signal(signum)))
167167
}

runtime/internal/lib/syscall/syscall_unix.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
package syscall
88

99
import (
10-
"unsafe"
11-
12-
c "github.com/goplus/llgo/runtime/internal/clite"
1310
"github.com/goplus/llgo/runtime/internal/clite/syscall"
1411
"github.com/goplus/llgo/runtime/internal/lib/internal/oserror"
1512
)
@@ -25,8 +22,7 @@ type Errno uintptr
2522
type Dirent = syscall.Dirent
2623

2724
func (e Errno) Error() string {
28-
ret := c.Strerror(c.Int(e))
29-
return unsafe.String((*byte)(unsafe.Pointer(ret)), c.Strlen(ret))
25+
return syscall.Error(syscall.Errno(e))
3026
}
3127

3228
func (e Errno) Is(target error) bool {

runtime/internal/lib/syscall/syscall_wasm.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ package syscall
99
import (
1010
"strconv"
1111

12+
"github.com/goplus/llgo/runtime/internal/lib/internal/oserror"
13+
1214
"github.com/goplus/llgo/runtime/internal/clite/syscall"
1315
)
1416

@@ -63,11 +65,21 @@ type Dirent struct {
6365
type Errno syscall.Errno
6466

6567
func (e Errno) Error() string {
66-
return syscall.Errno(e).Error()
68+
return syscall.Error(syscall.Errno(e))
6769
}
6870

6971
func (e Errno) Is(target error) bool {
70-
return syscall.Errno(e).Is(target)
72+
switch target {
73+
case oserror.ErrPermission:
74+
return e == Errno(syscall.EACCES) || e == Errno(syscall.EPERM)
75+
case oserror.ErrExist:
76+
return e == Errno(syscall.EEXIST) || e == Errno(syscall.ENOTEMPTY)
77+
case oserror.ErrNotExist:
78+
return e == Errno(syscall.ENOENT)
79+
// case ErrUnsupported:
80+
// return e == Errno(syscall.ENOSYS)
81+
}
82+
return false
7183
}
7284

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

0 commit comments

Comments
 (0)