mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 06:23: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 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
|
||||
|
||||
Reference in New Issue
Block a user