Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions runtime/internal/clite/syscall/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package syscall

import (
errorsPkg "errors"
_ "unsafe"

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

var (
ErrInvalid = errorsPkg.New("invalid argument")
ErrPermission = errorsPkg.New("permission denied")
ErrExist = errorsPkg.New("file already exists")
ErrNotExist = errorsPkg.New("file does not exist")
ErrClosed = errorsPkg.New("file already closed")
ErrUnsupported = errorsPkg.New("operation not supported")
)

// Nano returns the time stored in ts as nanoseconds.
func (ts *Timespec) Nano() int64 {
return int64(ts.Sec)*1e9 + int64(ts.Nsec)
Expand All @@ -59,7 +49,7 @@ func (tv *Timeval) Unix() (sec int64, nsec int64) {
//go:linkname c_getpid C.getpid
func c_getpid() c.Int

func Kill(pid int, signum Signal) error {
func Kill(pid int, signum Signal) Errno {
// WASI does not have the notion of processes nor signal handlers.
//
// Any signal that the application raises to the process itself will
Expand All @@ -68,7 +58,7 @@ func Kill(pid int, signum Signal) error {
return ESRCH
}
ProcExit(128 + int32(signum))
return nil
return 0
}

func ProcExit(code int32) {
Expand Down
24 changes: 1 addition & 23 deletions runtime/internal/clite/syscall/syscall_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package syscall

type Errno uintptr

func (e Errno) Error() string {
func Error(e Errno) string {
if 0 <= int(e) && int(e) < len(errors) {
s := errors[e]
if s != "" {
Expand All @@ -14,28 +14,6 @@ func (e Errno) Error() string {
return "errno " + utoa(uint64(e))
}

func (e Errno) Is(target error) bool {
switch target {
case ErrPermission:
return e == EACCES || e == EPERM
case ErrExist:
return e == EEXIST || e == ENOTEMPTY
case ErrNotExist:
return e == ENOENT
case ErrUnsupported:
return e == ENOSYS || e == ENOTSUP || e == EOPNOTSUPP
}
return false
}

func (e Errno) Temporary() bool {
return e == EINTR || e == EMFILE || e == ENFILE || e.Timeout()
}

func (e Errno) Timeout() bool {
return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
}

// A Signal is a number describing a process signal.
// It implements the [os.Signal] interface.
type Signal int
Expand Down
46 changes: 12 additions & 34 deletions runtime/internal/clite/syscall/syscall_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func direntNamlen(buf []byte) (uint64, bool) {
// }
type Errno uint32

func (e Errno) Error() string {
func Error(e Errno) string {
if 0 <= int(e) && int(e) < len(errorstr) {
s := errorstr[e]
if s != "" {
Expand All @@ -81,28 +81,6 @@ func (e Errno) Error() string {
return "errno " + utoa(uint64(e))
}

func (e Errno) Is(target error) bool {
switch target {
case ErrPermission:
return e == EACCES || e == EPERM
case ErrExist:
return e == EEXIST || e == ENOTEMPTY
case ErrNotExist:
return e == ENOENT
case ErrUnsupported:
return e == ENOSYS
}
return false
}

func (e Errno) Temporary() bool {
return e == EINTR || e == EMFILE || e.Timeout()
}

func (e Errno) Timeout() bool {
return e == EAGAIN || e == ETIMEDOUT
}

// A Signal is a number describing a process signal.
// It implements the [os.Signal] interface.
type Signal uint8
Expand Down Expand Up @@ -353,9 +331,9 @@ func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errn
return 0, 0, ENOSYS
}

func Sysctl(key string) (string, error) {
func Sysctl(key string) (string, Errno) {
if key == "kern.hostname" {
return "wasip1", nil
return "wasip1", 0
}
return "", ENOSYS
}
Expand All @@ -376,8 +354,8 @@ func Getegid() int {
return 1
}

func Getgroups() ([]int, error) {
return []int{1}, nil
func Getgroups() ([]int, Errno) {
return []int{1}, 0
}

func Getpid() int {
Expand All @@ -388,24 +366,24 @@ func Getppid() int {
return 2
}

// func Gettimeofday(tv *Timeval) error {
// func Gettimeofday(tv *Timeval) Errno {
// var time timestamp
// if errno := clock_time_get(clockRealtime, 1e3, &time); errno != 0 {
// return errno
// }
// tv.setTimestamp(time)
// return nil
// return 0
// }

func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err Errno) {
return 0, ENOSYS
}

func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err Errno) {
return 0, 0, ENOSYS
}

func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err Errno) {
return 0, ENOSYS
}

Expand Down Expand Up @@ -440,7 +418,7 @@ const (
clockThreadCPUTimeID
)

func SetNonblock(fd int, nonblocking bool) error {
func SetNonblock(fd int, nonblocking bool) Errno {
panic("todo: syscall.SetNonblock")
}

Expand All @@ -453,7 +431,7 @@ const (
RLIMIT_NOFILE = iota
)

func Getrlimit(which int, lim *Rlimit) error {
func Getrlimit(which int, lim *Rlimit) Errno {
return ENOSYS
}

Expand Down
33 changes: 0 additions & 33 deletions runtime/internal/clite/syscall/tables_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,36 +170,3 @@ var errorstr = [...]string{
EXDEV: "Cross-device link",
ENOTCAPABLE: "Capabilities insufficient",
}

// Do the interface allocations only once for common
// Errno values.
var (
errEAGAIN error = EAGAIN
errEINVAL error = EINVAL
errENOENT error = ENOENT
)

// errnoErr returns common boxed Errno values, to prevent
// allocations at runtime.
//
// We set both noinline and nosplit to reduce code size, this function has many
// call sites in the syscall package, inlining it causes a significant increase
// of the compiled code; the function call ultimately does not make a difference
// in the performance of syscall functions since the time is dominated by calls
// to the imports and path resolution.
//
//go:noinline
//go:nosplit
func errnoErr(e Errno) error {
switch e {
case 0:
return nil
case EAGAIN:
return errEAGAIN
case EINVAL:
return errEINVAL
case ENOENT:
return errENOENT
}
return e
}
12 changes: 6 additions & 6 deletions runtime/internal/lib/internal/oserror/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
package oserror

import (
"github.com/goplus/llgo/runtime/internal/clite/syscall"
"errors"
)

// llgo:skipall
type _oserror struct{}

var (
ErrInvalid = syscall.ErrInvalid
ErrPermission = syscall.ErrPermission
ErrExist = syscall.ErrExist
ErrNotExist = syscall.ErrNotExist
ErrClosed = syscall.ErrClosed
ErrInvalid = errors.New("invalid argument")
ErrPermission = errors.New("permission denied")
ErrExist = errors.New("file already exists")
ErrNotExist = errors.New("file does not exist")
ErrClosed = errors.New("file already closed")
)
2 changes: 1 addition & 1 deletion runtime/internal/lib/os/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (f *File) ReadDir(n int) (dirents []DirEntry, err error) {
// Open directory using file descriptor
dir := os.Fdopendir(c.Int(f.fd))
if dir == nil {
return nil, syscall.Errno(os.Errno())
return nil, origSyscall.Errno(os.Errno())
}
defer os.Closedir(dir)

Expand Down
6 changes: 5 additions & 1 deletion runtime/internal/lib/syscall/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,9 @@ func Accept(fd int) (nfd int, sa origSyscall.Sockaddr, err error) {
}

func Kill(pid int, signum Signal) error {
return syscall.Kill(pid, syscall.Signal(signum))
errno := syscall.Kill(pid, syscall.Signal(signum))
if errno == 0 {
return nil
}
return Errno(errno)
}
11 changes: 4 additions & 7 deletions runtime/internal/lib/syscall/syscall_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
package syscall

import (
"unsafe"
"errors"

c "github.com/goplus/llgo/runtime/internal/clite"
"github.com/goplus/llgo/runtime/internal/clite/syscall"
"github.com/goplus/llgo/runtime/internal/lib/internal/oserror"
)
Expand All @@ -25,8 +24,7 @@ type Errno uintptr
type Dirent = syscall.Dirent

func (e Errno) Error() string {
ret := c.Strerror(c.Int(e))
return unsafe.String((*byte)(unsafe.Pointer(ret)), c.Strlen(ret))
return syscall.Error(syscall.Errno(e))
}

func (e Errno) Is(target error) bool {
Expand All @@ -37,9 +35,8 @@ func (e Errno) Is(target error) bool {
return e == Errno(syscall.EEXIST) || e == Errno(syscall.ENOTEMPTY)
case oserror.ErrNotExist:
return e == Errno(syscall.ENOENT)
// TODO(xsw): go1.21
// case errors.ErrUnsupported:
// return e == ENOSYS || e == ENOTSUP || e == EOPNOTSUPP
case errors.ErrUnsupported:
return e == Errno(syscall.ENOSYS) || e == Errno(syscall.ENOTSUP) || e == Errno(syscall.EOPNOTSUPP)
}
return false
}
Expand Down
25 changes: 23 additions & 2 deletions runtime/internal/lib/syscall/syscall_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
package syscall

import (
"errors"
"strconv"

"github.com/goplus/llgo/runtime/internal/lib/internal/oserror"

"github.com/goplus/llgo/runtime/internal/clite/syscall"
)

Expand Down Expand Up @@ -63,11 +66,29 @@ type Dirent struct {
type Errno syscall.Errno

func (e Errno) Error() string {
return syscall.Errno(e).Error()
return syscall.Error(syscall.Errno(e))
}

func (e Errno) Is(target error) bool {
return syscall.Errno(e).Is(target)
switch target {
case oserror.ErrPermission:
return e == Errno(syscall.EACCES) || e == Errno(syscall.EPERM)
case oserror.ErrExist:
return e == Errno(syscall.EEXIST) || e == Errno(syscall.ENOTEMPTY)
case oserror.ErrNotExist:
return e == Errno(syscall.ENOENT)
case errors.ErrUnsupported:
return e == Errno(syscall.ENOSYS)
}
return false
}

func (e Errno) Temporary() bool {
return e == Errno(syscall.EINTR) || e == Errno(syscall.EMFILE) || e.Timeout()
}

func (e Errno) Timeout() bool {
return e == Errno(syscall.EAGAIN) || e == Errno(syscall.ETIMEDOUT)
}

// A Signal is a number describing a process signal.
Expand Down
Loading