mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 06:23:39 +00:00
wasi preview 2 support (#4027)
* all: wasip2 support Co-authored-by: Randy Reddig <randy.reddig@fastly.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
//go:build darwin || nintendoswitch || wasi || wasip1
|
||||
//go:build darwin || nintendoswitch || wasip1 || wasip2
|
||||
|
||||
package syscall
|
||||
|
||||
@@ -260,55 +260,6 @@ func Mprotect(b []byte, prot int) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func Environ() []string {
|
||||
|
||||
// This function combines all the environment into a single allocation.
|
||||
// While this optimizes for memory usage and garbage collector
|
||||
// overhead, it does run the risk of potentially pinning a "large"
|
||||
// allocation if a user holds onto a single environment variable or
|
||||
// value. Having each variable be its own allocation would make the
|
||||
// trade-off in the other direction.
|
||||
|
||||
// calculate total memory required
|
||||
var length uintptr
|
||||
var vars int
|
||||
for environ := libc_environ; *environ != nil; {
|
||||
length += libc_strlen(*environ)
|
||||
vars++
|
||||
environ = (*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(environ), unsafe.Sizeof(environ)))
|
||||
}
|
||||
|
||||
// allocate our backing slice for the strings
|
||||
b := make([]byte, length)
|
||||
// and the slice we're going to return
|
||||
envs := make([]string, 0, vars)
|
||||
|
||||
// loop over the environment again, this time copying over the data to the backing slice
|
||||
for environ := libc_environ; *environ != nil; {
|
||||
length = libc_strlen(*environ)
|
||||
// construct a Go string pointing at the libc-allocated environment variable data
|
||||
var envVar string
|
||||
rawEnvVar := (*struct {
|
||||
ptr unsafe.Pointer
|
||||
length uintptr
|
||||
})(unsafe.Pointer(&envVar))
|
||||
rawEnvVar.ptr = *environ
|
||||
rawEnvVar.length = length
|
||||
// pull off the number of bytes we need for this environment variable
|
||||
var bs []byte
|
||||
bs, b = b[:length], b[length:]
|
||||
// copy over the bytes to the Go heap
|
||||
copy(bs, envVar)
|
||||
// convert trimmed slice to string
|
||||
s := *(*string)(unsafe.Pointer(&bs))
|
||||
// add s to our list of environment variables
|
||||
envs = append(envs, s)
|
||||
// environ++
|
||||
environ = (*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(environ), unsafe.Sizeof(environ)))
|
||||
}
|
||||
return envs
|
||||
}
|
||||
|
||||
// BytePtrFromString returns a pointer to a NUL-terminated array of
|
||||
// bytes containing the text of s. If s contains a NUL byte at any
|
||||
// location, it returns (nil, EINVAL).
|
||||
@@ -445,6 +396,3 @@ func libc_readlink(path *byte, buf *byte, count uint) int
|
||||
//
|
||||
//export unlink
|
||||
func libc_unlink(pathname *byte) int32
|
||||
|
||||
//go:extern environ
|
||||
var libc_environ *unsafe.Pointer
|
||||
|
||||
Reference in New Issue
Block a user