github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/gojs/errno.go (about)

     1  package gojs
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/bananabytelabs/wazero/experimental/sys"
     7  )
     8  
     9  // Errno is a (GOARCH=wasm) error, which must match a key in mapJSError.
    10  //
    11  // See https://github.com/golang/go/blob/go1.20/src/syscall/tables_js.go#L371-L494
    12  type Errno struct {
    13  	s string
    14  }
    15  
    16  // Error implements error.
    17  func (e *Errno) Error() string {
    18  	return e.s
    19  }
    20  
    21  // This order match constants from wasi_snapshot_preview1.ErrnoSuccess for
    22  // easier maintenance.
    23  var (
    24  	// ErrnoAcces Permission denied.
    25  	ErrnoAcces = &Errno{"EACCES"}
    26  	// ErrnoAgain Resource unavailable, or operation would block.
    27  	ErrnoAgain = &Errno{"EAGAIN"}
    28  	// ErrnoBadf Bad file descriptor.
    29  	ErrnoBadf = &Errno{"EBADF"}
    30  	// ErrnoExist File exists.
    31  	ErrnoExist = &Errno{"EEXIST"}
    32  	// ErrnoFault Bad address.
    33  	ErrnoFault = &Errno{"EFAULT"}
    34  	// ErrnoIntr Interrupted function.
    35  	ErrnoIntr = &Errno{"EINTR"}
    36  	// ErrnoInval Invalid argument.
    37  	ErrnoInval = &Errno{"EINVAL"}
    38  	// ErrnoIo I/O error.
    39  	ErrnoIo = &Errno{"EIO"}
    40  	// ErrnoIsdir Is a directory.
    41  	ErrnoIsdir = &Errno{"EISDIR"}
    42  	// ErrnoLoop Too many levels of symbolic links.
    43  	ErrnoLoop = &Errno{"ELOOP"}
    44  	// ErrnoNametoolong Filename too long.
    45  	ErrnoNametoolong = &Errno{"ENAMETOOLONG"}
    46  	// ErrnoNoent No such file or directory.
    47  	ErrnoNoent = &Errno{"ENOENT"}
    48  	// ErrnoNosys function not supported.
    49  	ErrnoNosys = &Errno{"ENOSYS"}
    50  	// ErrnoNotdir Not a directory or a symbolic link to a directory.
    51  	ErrnoNotdir = &Errno{"ENOTDIR"}
    52  	// ErrnoNotempty Directory not empty.
    53  	ErrnoNotempty = &Errno{"ENOTEMPTY"}
    54  	// ErrnoNotsup Not supported, or operation not supported on socket.
    55  	ErrnoNotsup = &Errno{"ENOTSUP"}
    56  	// ErrnoPerm Operation not permitted.
    57  	ErrnoPerm = &Errno{"EPERM"}
    58  	// ErrnoRofs read-only file system.
    59  	ErrnoRofs = &Errno{"EROFS"}
    60  )
    61  
    62  // ToErrno maps I/O errors as the message must be the code, ex. "EINVAL", not
    63  // the message, e.g. "invalid argument".
    64  func ToErrno(err error) *Errno {
    65  	if err == nil || err == io.EOF {
    66  		return nil // io.EOF has no value in GOOS=js, and isn't an error.
    67  	}
    68  	errno, ok := err.(sys.Errno)
    69  	if !ok {
    70  		return ErrnoIo
    71  	}
    72  	switch errno {
    73  	case sys.EACCES:
    74  		return ErrnoAcces
    75  	case sys.EAGAIN:
    76  		return ErrnoAgain
    77  	case sys.EBADF:
    78  		return ErrnoBadf
    79  	case sys.EEXIST:
    80  		return ErrnoExist
    81  	case sys.EFAULT:
    82  		return ErrnoFault
    83  	case sys.EINTR:
    84  		return ErrnoIntr
    85  	case sys.EINVAL:
    86  		return ErrnoInval
    87  	case sys.EIO:
    88  		return ErrnoIo
    89  	case sys.EISDIR:
    90  		return ErrnoIsdir
    91  	case sys.ELOOP:
    92  		return ErrnoLoop
    93  	case sys.ENAMETOOLONG:
    94  		return ErrnoNametoolong
    95  	case sys.ENOENT:
    96  		return ErrnoNoent
    97  	case sys.ENOSYS:
    98  		return ErrnoNosys
    99  	case sys.ENOTDIR:
   100  		return ErrnoNotdir
   101  	case sys.ENOTEMPTY:
   102  		return ErrnoNotempty
   103  	case sys.ENOTSUP:
   104  		return ErrnoNotsup
   105  	case sys.EPERM:
   106  		return ErrnoPerm
   107  	case sys.EROFS:
   108  		return ErrnoRofs
   109  	default:
   110  		return ErrnoIo
   111  	}
   112  }