Skip to content

Commit

Permalink
🆙 update: repo maintenance (#3)
Browse files Browse the repository at this point in the history
* 🔧 config: bump Go/TinyGo version

Signed-off-by: Gaukas Wang <i@gaukas.wang>

* 👕 refactor: code formatting

Signed-off-by: Gaukas Wang <i@gaukas.wang>

* 📝 docs: support Go 1.22 with TinyGo v0.31.0

Signed-off-by: Gaukas Wang <i@gaukas.wang>

---------

Signed-off-by: Gaukas Wang <i@gaukas.wang>
  • Loading branch information
gaukas committed Mar 11, 2024
1 parent ad284bc commit 11a16e6
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 177 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.21.x"
go-version: "1.22.x"
- uses: acifani/setup-tinygo@v2
with:
tinygo-version: '0.30.0'
tinygo-version: '0.31.1'
- name: Build WATM Artifacts
run: tinygo build -o ../../../${{ matrix.watm.name }}.v0.tinygo.wasm
-target=wasi -no-debug -scheduler=${{ matrix.watm.scheduler }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/watm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ jobs:
strategy:
fail-fast: true
matrix:
tinygo: [ "0.30.0" ] # latest tinygo version ONLY (1)
go: [ "1.20.x", "1.21.x" ] # latest 2 stable versions of Go. TODO: bump to 1.22.x once tinygo support added.
tinygo: [ "0.31.1" ] # latest tinygo version ONLY (1)
go: [ "1.21.x", "1.22.x" ] # latest 2 stable versions of Go. TODO: bump to 1.22.x once tinygo support added.
examples: [ "plain", "reverse", "utls" ] # Add examples here per ones under tinygo/v0/examples
steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

# Go workspace file
go.work
go.work.sum

# Generated by Cargo
# will have compiled files and executables
Expand Down
50 changes: 0 additions & 50 deletions tinygo/v0/dialer.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package v0

import (
"log"
"syscall"

v0net "github.com/refraction-networking/watm/tinygo/v0/net"
"github.com/refraction-networking/watm/wasip1"
)

type dialer struct {
wt WrappingTransport
// dt DialingTransport
Expand Down Expand Up @@ -58,45 +50,3 @@ func BuildDialerWithDialingTransport(dt DialingTransport) {
// d.wt = nil
panic("BuildDialerWithDialingTransport: not implemented")
}

//export _water_dial
func _water_dial(internalFd int32) (networkFd int32) {
if workerIdentity != identity_uninitialized {
return wasip1.EncodeWATERError(syscall.EBUSY) // device or resource busy (worker already initialized)
}

// wrap the internalFd into a v0net.Conn
sourceConn = v0net.RebuildTCPConn(internalFd)
err := sourceConn.(*v0net.TCPConn).SetNonBlock(true)
if err != nil {
log.Printf("dial: sourceConn.SetNonblock: %v", err)
return wasip1.EncodeWATERError(err.(syscall.Errno))
}

if d.wt != nil {
// call v0net.Dial
rawNetworkConn, err := v0net.Dial("", "")
if err != nil {
log.Printf("dial: v0net.Dial: %v", err)
return wasip1.EncodeWATERError(err.(syscall.Errno))
}
networkFd = rawNetworkConn.Fd()

// Note: here we are not setting nonblocking mode on the
// networkConn -- it depends on the WrappingTransport to
// determine whether to set nonblocking mode or not.

// wrap
remoteConn, err = d.wt.Wrap(rawNetworkConn)
if err != nil {
log.Printf("dial: d.wt.Wrap: %v", err)
return wasip1.EncodeWATERError(syscall.EPROTO) // protocol error
}
// TODO: implement _water_dial with DialingTransport
} else {
return wasip1.EncodeWATERError(syscall.EPERM) // operation not permitted
}

workerIdentity = identity_dialer
return networkFd
}
2 changes: 1 addition & 1 deletion tinygo/v0/examples/plain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This example shows how to build a minimal WATM with TinyGo which passes through

## Build

Go 1.20/1.21 is required to build this example until TinyGo supports Go 1.22.
Go 1.20/1.21/1.22 is required to build this example. TinyGo started to support Go 1.22 in v0.31.0.

### Debug

Expand Down
2 changes: 1 addition & 1 deletion tinygo/v0/examples/reverse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This example shows how to build a minimal WATM with TinyGo which reverse the rec

## Build

Go 1.20/1.21 is required to build this example until TinyGo supports Go 1.22.
Go 1.20/1.21/1.22 is required to build this example. TinyGo started to support Go 1.22 in v0.31.0.

### Debug

Expand Down
2 changes: 1 addition & 1 deletion tinygo/v0/examples/utls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This example shows how to build a fully functional TLS client with TinyGo from [

## Build

Go 1.20/1.21 is required to build this example until TinyGo supports Go 1.22.
Go 1.20/1.21/1.22 is required to build this example. TinyGo started to support Go 1.22 in v0.31.0.

### Debug

Expand Down
139 changes: 139 additions & 0 deletions tinygo/v0/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,142 @@ func _water_cancel_with(cancelFd int32) int32 {

return 0 // ESUCCESS
}

//export _water_dial
func _water_dial(internalFd int32) (networkFd int32) {
if workerIdentity != identity_uninitialized {
return wasip1.EncodeWATERError(syscall.EBUSY) // device or resource busy (worker already initialized)
}

// wrap the internalFd into a v0net.Conn
sourceConn = v0net.RebuildTCPConn(internalFd)
err := sourceConn.(*v0net.TCPConn).SetNonBlock(true)
if err != nil {
log.Printf("dial: sourceConn.SetNonblock: %v", err)
return wasip1.EncodeWATERError(err.(syscall.Errno))
}

if d.wt != nil {
// call v0net.Dial
rawNetworkConn, err := v0net.Dial("", "")
if err != nil {
log.Printf("dial: v0net.Dial: %v", err)
return wasip1.EncodeWATERError(err.(syscall.Errno))
}
networkFd = rawNetworkConn.Fd()

// Note: here we are not setting nonblocking mode on the
// networkConn -- it depends on the WrappingTransport to
// determine whether to set nonblocking mode or not.

// wrap
remoteConn, err = d.wt.Wrap(rawNetworkConn)
if err != nil {
log.Printf("dial: d.wt.Wrap: %v", err)
return wasip1.EncodeWATERError(syscall.EPROTO) // protocol error
}
// TODO: implement _water_dial with DialingTransport
} else {
return wasip1.EncodeWATERError(syscall.EPERM) // operation not permitted
}

workerIdentity = identity_dialer
return networkFd
}

//export _water_accept
func _water_accept(internalFd int32) (networkFd int32) {
if workerIdentity != identity_uninitialized {
return wasip1.EncodeWATERError(syscall.EBUSY) // device or resource busy (worker already initialized)
}

// wrap the internalFd into a v0net.Conn
sourceConn = v0net.RebuildTCPConn(internalFd)
err := sourceConn.(*v0net.TCPConn).SetNonBlock(true)
if err != nil {
log.Printf("dial: sourceConn.SetNonblock: %v", err)
return wasip1.EncodeWATERError(err.(syscall.Errno))
}

if d.wt != nil {
var lis v0net.Listener = &v0net.TCPListener{}
// call v0net.Listener.Accept
rawNetworkConn, err := lis.Accept()
if err != nil {
log.Printf("dial: v0net.Listener.Accept: %v", err)
return wasip1.EncodeWATERError(err.(syscall.Errno))
}
networkFd = rawNetworkConn.Fd()

// Note: here we are not setting nonblocking mode on the
// networkConn -- it depends on the WrappingTransport to
// determine whether to set nonblocking mode or not.

// wrap
remoteConn, err = d.wt.Wrap(rawNetworkConn)
if err != nil {
log.Printf("dial: d.wt.Wrap: %v", err)
return wasip1.EncodeWATERError(syscall.EPROTO) // protocol error
}
// TODO: implement _water_accept with ListeningTransport
} else {
return wasip1.EncodeWATERError(syscall.EPERM) // operation not permitted
}

workerIdentity = identity_listener
return networkFd
}

//export _water_associate
func _water_associate() int32 {
if workerIdentity != identity_uninitialized {
return wasip1.EncodeWATERError(syscall.EBUSY) // device or resource busy (worker already initialized)
}

if r.wt != nil {
var err error
var lis v0net.Listener = &v0net.TCPListener{}
sourceConn, err = lis.Accept()
if err != nil {
log.Printf("dial: v0net.Listener.Accept: %v", err)
return wasip1.EncodeWATERError(err.(syscall.Errno))
}

remoteConn, err = v0net.Dial("", "")
if err != nil {
log.Printf("dial: v0net.Dial: %v", err)
return wasip1.EncodeWATERError(err.(syscall.Errno))
}

if r.wrapSelection == RelayWrapRemote {
// wrap remoteConn
remoteConn, err = r.wt.Wrap(remoteConn.(*v0net.TCPConn))
// set sourceConn, the not-wrapped one, to non-blocking mode
sourceConn.(*v0net.TCPConn).SetNonBlock(true)
} else {
// wrap sourceConn
sourceConn, err = r.wt.Wrap(sourceConn.(*v0net.TCPConn))
// set remoteConn, the not-wrapped one, to non-blocking mode
remoteConn.(*v0net.TCPConn).SetNonBlock(true)
}
if err != nil {
log.Printf("dial: r.wt.Wrap: %v", err)
return wasip1.EncodeWATERError(syscall.EPROTO) // protocol error
}
} else {
return wasip1.EncodeWATERError(syscall.EPERM) // operation not permitted
}

workerIdentity = identity_relay
return 0
}

//export _water_worker
func _water_worker() int32 {
if workerIdentity == identity_uninitialized {
log.Println("worker: uninitialized")
return wasip1.EncodeWATERError(syscall.ENOTCONN) // socket not connected
}
log.Printf("worker: working as %s", identityStrings[workerIdentity])
return worker()
}
51 changes: 0 additions & 51 deletions tinygo/v0/listener.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package v0

import (
"log"
"syscall"

v0net "github.com/refraction-networking/watm/tinygo/v0/net"
"github.com/refraction-networking/watm/wasip1"
)

type listener struct {
wt WrappingTransport
// lt ListeningTransport
Expand Down Expand Up @@ -58,46 +50,3 @@ func BuildListenerWithListeningTransport(lt ListeningTransport) {
// l.wt = nil
panic("BuildListenerWithListeningTransport: not implemented")
}

//export _water_accept
func _water_accept(internalFd int32) (networkFd int32) {
if workerIdentity != identity_uninitialized {
return wasip1.EncodeWATERError(syscall.EBUSY) // device or resource busy (worker already initialized)
}

// wrap the internalFd into a v0net.Conn
sourceConn = v0net.RebuildTCPConn(internalFd)
err := sourceConn.(*v0net.TCPConn).SetNonBlock(true)
if err != nil {
log.Printf("dial: sourceConn.SetNonblock: %v", err)
return wasip1.EncodeWATERError(err.(syscall.Errno))
}

if d.wt != nil {
var lis v0net.Listener = &v0net.TCPListener{}
// call v0net.Listener.Accept
rawNetworkConn, err := lis.Accept()
if err != nil {
log.Printf("dial: v0net.Listener.Accept: %v", err)
return wasip1.EncodeWATERError(err.(syscall.Errno))
}
networkFd = rawNetworkConn.Fd()

// Note: here we are not setting nonblocking mode on the
// networkConn -- it depends on the WrappingTransport to
// determine whether to set nonblocking mode or not.

// wrap
remoteConn, err = d.wt.Wrap(rawNetworkConn)
if err != nil {
log.Printf("dial: d.wt.Wrap: %v", err)
return wasip1.EncodeWATERError(syscall.EPROTO) // protocol error
}
// TODO: implement _water_accept with ListeningTransport
} else {
return wasip1.EncodeWATERError(syscall.EPERM) // operation not permitted
}

workerIdentity = identity_listener
return networkFd
}
10 changes: 5 additions & 5 deletions tinygo/v0/net/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c *TCPConn) Read(b []byte) (n int, err error) {
// if no deadline set, behavior depends on blocking mode of the
// underlying file descriptor.
return syscallFnFd(c.rawConn, func(fd uintptr) (int, error) {
n, err := syscall.Read(int(fd), b)
n, err := syscall.Read(syscallFd(fd), b)
if n == 0 && err == nil {
err = io.EOF
}
Expand All @@ -60,7 +60,7 @@ func (c *TCPConn) Read(b []byte) (n int, err error) {
// we retry until the deadline is reached.
for {
if n, err = syscallFnFd(c.rawConn, func(fd uintptr) (int, error) {
n, err := syscall.Read(int(fd), b)
n, err := syscall.Read(syscallFd(fd), b)
if n == 0 && err == nil {
err = io.EOF
}
Expand All @@ -84,13 +84,13 @@ func (c *TCPConn) Write(b []byte) (n int, writeErr error) {
// if no deadline set, behavior depends on blocking mode of the
// underlying file descriptor.
return syscallFnFd(c.rawConn, func(fd uintptr) (int, error) {
return syscall.Write(int(fd), b)
return syscall.Write(syscallFd(fd), b)
})
} else {
// writeDeadline is set, if EAGAIN/EWOULDBLOCK is returned,
// we retry until the deadline is reached.
if err := c.rawConn.Write(func(fd uintptr) (done bool) {
n, writeErr = syscall.Write(int(fd), b)
n, writeErr = syscall.Write(syscallFd(fd), b)
if errors.Is(writeErr, syscall.EAGAIN) {
if time.Now().Before(wdl) {
return false
Expand All @@ -117,7 +117,7 @@ func (c *TCPConn) Close() error {
// })
// }
return syscallControlFd(c.rawConn, func(fd uintptr) error {
return syscall.Close(int(fd))
return syscall.Close(syscallFd(fd))
})
}

Expand Down
3 changes: 1 addition & 2 deletions tinygo/v0/net/syscall.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package net

import (
"fmt"
"syscall"
)

func syscallControlFd(rawConn syscall.RawConn, f func(fd uintptr) error) (err error) {
if controlErr := rawConn.Control(func(fd uintptr) {
err = f(fd)
}); controlErr != nil {
panic(fmt.Sprintf("controlErr = %v", controlErr))
// panic(fmt.Sprintf("controlErr = %v", controlErr))
return controlErr
}
return err
Expand Down
2 changes: 2 additions & 0 deletions tinygo/v0/net/syscall_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ import "syscall"
func syscallSetNonblock(fd uintptr, nonblocking bool) (err error) {
return syscall.SetNonblock(int(fd), nonblocking)
}

type syscallFd = int
2 changes: 2 additions & 0 deletions tinygo/v0/net/syscall_wasi.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ const (
FDFLAG_RSYNC = 0x0008
FDFLAG_SYNC = 0x0010
)

type syscallFd = int

0 comments on commit 11a16e6

Please sign in to comment.