Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💥 update: use syscall.Errno-based error code #32

Merged
merged 4 commits into from
Jan 16, 2024
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
11 changes: 5 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/gaukas/water/configbuilder"
"github.com/gaukas/water/internal/log"
"github.com/gaukas/water/internal/wasm"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -46,7 +45,7 @@ type Config struct {
// Caller is supposed to call c.ModuleConfig() to get the pointer to the
// ModuleConfigFactory. If the pointer is nil, a new ModuleConfigFactory will
// be created and returned.
ModuleConfigFactory *wasm.ModuleConfigFactory
ModuleConfigFactory *WazeroModuleConfigFactory

OverrideLogger *log.Logger // essentially a *slog.Logger, currently using an alias to flatten the version discrepancy
}
Expand Down Expand Up @@ -100,9 +99,9 @@ func (c *Config) WATMBinOrPanic() []byte {

// ModuleConfig returns the ModuleConfigFactory. If the pointer is
// nil, a new ModuleConfigFactory will be created and returned.
func (c *Config) ModuleConfig() *wasm.ModuleConfigFactory {
func (c *Config) ModuleConfig() *WazeroModuleConfigFactory {
if c.ModuleConfigFactory == nil {
c.ModuleConfigFactory = wasm.NewModuleConfigFactory()
c.ModuleConfigFactory = NewWazeroModuleConfigFactory()

// by default, stdout and stderr are inherited
c.ModuleConfigFactory.InheritStdout()
Expand Down Expand Up @@ -161,7 +160,7 @@ func (c *Config) UnmarshalJSON(data []byte) error {
}
}

c.ModuleConfigFactory = wasm.NewModuleConfigFactory()
c.ModuleConfigFactory = NewWazeroModuleConfigFactory()
if len(confJson.Module.Argv) > 0 {
c.ModuleConfigFactory.SetArgv(confJson.Module.Argv)
}
Expand Down Expand Up @@ -228,7 +227,7 @@ func (c *Config) UnmarshalProto(b []byte) error {
}

// Parse ModuleConfigFactory
c.ModuleConfigFactory = wasm.NewModuleConfigFactory()
c.ModuleConfigFactory = NewWazeroModuleConfigFactory()
if len(confProto.GetModule().GetArgv()) > 0 {
c.ModuleConfigFactory.SetArgv(confProto.Module.Argv)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/wasip1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `wasip1` package

This package is mostly copied from Go standard library `syscall` package for `wasip1` target, and is a duplicate to `watm/wasip1` package. We choose to maintain a separate package for `wasip1` target to avoid import cycle (`watm` may import `water` for some example/demo code).
51 changes: 51 additions & 0 deletions internal/wasip1/errno.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package wasip1

import (
"fmt"
"syscall"
)

// errno is just a copy of syscall.Errno from the Go standard library.
//
// The values are defined in syscall/tables_wasip1.go.
type errno syscall.Errno

// DecodeWATERError converts a error code returned by WATER API
// into a syscall.Errno or a higher-level error in Go.
//
// It automatically detects whether the error code is a WATER error
// or a success code (positive). In case of a success code, it
// returns the code itself and a nil error.
func DecodeWATERError(errorCode int32) (n int32, err error) {
if errorCode >= 0 {
n = errorCode // such that when error code is 0, it will return 0, nil
} else {
errorCode = -errorCode // flip the sign
if errno, ok := mapErrno2Syscall[errno(errorCode)]; ok {
// if the negative of the error code is a valid Errno, then it is a valid WATERErrno.
err = errno

// TODO: convert some special error codes to higher-level errors.
} else {
// otherwise, it is an unknown error code.
err = fmt.Errorf("unknown WATERErrno %d", errorCode)
}
}
return
}

func EncodeWATERError(errno syscall.Errno) int32 {
if errno == 0 {
return 0
}

// first find the corresponding Errno (there might
// be missing Errno in the map, which means they
// are not supported)
if foundErrno, ok := mapSyscall2Errno[errno]; ok {
// then convert it to the negative value of itself
return -int32(foundErrno)
}
// if the errno is not found, then it is an unknown error
return -int32(syscall.ENOSYS)
}
Loading
Loading