Skip to content

Commit

Permalink
shuffle code around to accommodate tinygo
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <[email protected]>
  • Loading branch information
evacchi committed Mar 27, 2024
1 parent ebbaf27 commit 15c25d0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 90 deletions.
2 changes: 1 addition & 1 deletion internal/sysfs/dirfs_link_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import (

// Link implements the same method as documented on sys.FS
func (d *dirFS) Link(oldName, newName string) experimentalsys.Errno {
panic("Link is not supported by TinyGo")
return experimentalsys.ENOSYS
}
56 changes: 0 additions & 56 deletions internal/sysfs/sock_big.go

This file was deleted.

50 changes: 49 additions & 1 deletion internal/sysfs/sock_supported.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,61 @@
//go:build linux || darwin || windows
//go:build (linux || darwin || windows) && !tinygo

package sysfs

import (
"net"
"syscall"

experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/internal/fsapi"
socketapi "github.com/tetratelabs/wazero/internal/sock"
)

// Accept implements the same method as documented on socketapi.TCPSock
func (f *tcpListenerFile) Accept() (socketapi.TCPConn, experimentalsys.Errno) {
// Ensure we have an incoming connection, otherwise return immediately.
if f.nonblock {
if ready, errno := _pollSock(f.tl, fsapi.POLLIN, 0); !ready || errno != 0 {
return nil, experimentalsys.EAGAIN
}
}

// Accept normally blocks goroutines, but we
// made sure that we have an incoming connection,
// so we should be safe.
if conn, err := f.tl.Accept(); err != nil {
return nil, experimentalsys.UnwrapOSError(err)
} else {
return newTcpConn(conn.(*net.TCPConn)), 0
}
}

// SetNonblock implements the same method as documented on fsapi.File
func (f *tcpListenerFile) SetNonblock(enabled bool) (errno experimentalsys.Errno) {
f.nonblock = enabled
_, errno = syscallConnControl(f.tl, func(fd uintptr) (int, experimentalsys.Errno) {
return 0, setNonblockSocket(fd, enabled)
})
return
}

// Shutdown implements the same method as documented on experimentalsys.Conn
func (f *tcpConnFile) Shutdown(how int) experimentalsys.Errno {
// FIXME: can userland shutdown listeners?
var err error
switch how {
case socketapi.SHUT_RD:
err = f.tc.CloseRead()
case socketapi.SHUT_WR:
err = f.tc.CloseWrite()
case socketapi.SHUT_RDWR:
return f.close()
default:
return experimentalsys.EINVAL
}
return experimentalsys.UnwrapOSError(err)
}

// syscallConnControl extracts a syscall.RawConn from the given syscall.Conn and applies
// the given fn to a file descriptor, returning an integer or a nonzero syscall.Errno on failure.
//
Expand Down
30 changes: 0 additions & 30 deletions internal/sysfs/sock_tiny.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/sysfs/sock_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build linux || darwin
//go:build (linux || darwin) && !tinygo

package sysfs

Expand Down
24 changes: 23 additions & 1 deletion internal/sysfs/sock_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !linux && !darwin && !windows
//go:build (!linux && !darwin && !windows) || tinygo

package sysfs

Expand Down Expand Up @@ -57,3 +57,25 @@ func recvfrom(fd uintptr, buf []byte, flags int32) (n int, errno sys.Errno) {
func syscallConnControl(conn syscall.Conn, fn func(fd uintptr) (int, experimentalsys.Errno)) (n int, errno sys.Errno) {
return -1, sys.ENOTSUP
}

// Accept implements the same method as documented on socketapi.TCPSock
func (f *tcpListenerFile) Accept() (socketapi.TCPConn, experimentalsys.Errno) {
panic("TCPSock.Accept is not implemented for TinyGo")
}

// Shutdown implements the same method as documented on experimentalsys.Conn
func (f *tcpConnFile) Shutdown(how int) experimentalsys.Errno {
// FIXME: can userland shutdown listeners?
var err error
switch how {
case socketapi.SHUT_RD:
err = f.tc.Close()
case socketapi.SHUT_WR:
err = f.tc.Close()
case socketapi.SHUT_RDWR:
return f.close()
default:
return experimentalsys.EINVAL
}
return experimentalsys.UnwrapOSError(err)
}

0 comments on commit 15c25d0

Please sign in to comment.