mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-09 13:33:39 +00:00
WASI & darwin: support env variables based on libc
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
committed by
Ron Evans
parent
c299386906
commit
9841ac4acd
@@ -1,4 +1,4 @@
|
||||
// +build baremetal wasi
|
||||
// +build baremetal
|
||||
|
||||
package syscall
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// +build darwin nintendoswitch
|
||||
// +build darwin nintendoswitch wasi
|
||||
|
||||
package syscall
|
||||
|
||||
@@ -6,6 +6,12 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type sliceHeader struct {
|
||||
buf *byte
|
||||
len uintptr
|
||||
cap uintptr
|
||||
}
|
||||
|
||||
func Close(fd int) (err error) {
|
||||
return ENOSYS // TODO
|
||||
}
|
||||
@@ -48,18 +54,32 @@ func Getpid() (pid int) {
|
||||
}
|
||||
|
||||
func Getenv(key string) (value string, found bool) {
|
||||
return "", false // TODO
|
||||
data := append([]byte(key), 0)
|
||||
raw := libc_getenv(&data[0])
|
||||
if raw == nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
ptr := uintptr(unsafe.Pointer(raw))
|
||||
for size := uintptr(0); ; size++ {
|
||||
v := *(*byte)(unsafe.Pointer(ptr))
|
||||
if v == 0 {
|
||||
src := *(*[]byte)(unsafe.Pointer(&sliceHeader{buf: raw, len: size, cap: size}))
|
||||
return string(src), true
|
||||
}
|
||||
ptr += unsafe.Sizeof(byte(0))
|
||||
}
|
||||
}
|
||||
|
||||
func splitSlice(p []byte) (buf *byte, len uintptr) {
|
||||
slice := (*struct {
|
||||
buf *byte
|
||||
len uintptr
|
||||
cap uintptr
|
||||
})(unsafe.Pointer(&p))
|
||||
slice := (*sliceHeader)(unsafe.Pointer(&p))
|
||||
return slice.buf, slice.len
|
||||
}
|
||||
|
||||
// ssize_t write(int fd, const void *buf, size_t count)
|
||||
//export write
|
||||
func libc_write(fd int32, buf *byte, count uint) int
|
||||
|
||||
// char *getenv(const char *name);
|
||||
//export getenv
|
||||
func libc_getenv(name *byte) *byte
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// +build wasi
|
||||
|
||||
package syscall
|
||||
|
||||
// https://github.com/WebAssembly/wasi-libc/blob/main/expected/wasm32-wasi/predefined-macros.txt
|
||||
|
||||
type Signal int
|
||||
|
||||
const (
|
||||
SIGCHLD = 16
|
||||
SIGINT = 2
|
||||
SIGKILL = 9
|
||||
SIGTRAP = 5
|
||||
SIGQUIT = 3
|
||||
SIGTERM = 15
|
||||
)
|
||||
|
||||
const (
|
||||
Stdin = 0
|
||||
Stdout = 1
|
||||
Stderr = 2
|
||||
)
|
||||
|
||||
const (
|
||||
__WASI_OFLAGS_CREAT = 1
|
||||
__WASI_FDFLAGS_APPEND = 1
|
||||
__WASI_OFLAGS_EXCL = 4
|
||||
__WASI_OFLAGS_TRUNC = 8
|
||||
__WASI_FDFLAGS_SYNC = 16
|
||||
|
||||
O_RDONLY = 0x04000000
|
||||
O_WRONLY = 0x10000000
|
||||
O_RDWR = O_RDONLY | O_WRONLY
|
||||
|
||||
O_CREAT = __WASI_OFLAGS_CREAT << 12
|
||||
O_CREATE = O_CREAT
|
||||
O_TRUNC = __WASI_OFLAGS_TRUNC << 12
|
||||
O_APPEND = __WASI_FDFLAGS_APPEND
|
||||
O_EXCL = __WASI_OFLAGS_EXCL << 12
|
||||
O_SYNC = __WASI_FDFLAGS_SYNC
|
||||
|
||||
O_CLOEXEC = 0
|
||||
)
|
||||
|
||||
//go:extern errno
|
||||
var libcErrno uintptr
|
||||
|
||||
func getErrno() error {
|
||||
return Errno(libcErrno)
|
||||
}
|
||||
Reference in New Issue
Block a user