runtime: implement environment variables for Linux
Этот коммит содержится в:
родитель
768a15c1dd
коммит
c47cdfa66f
5 изменённых файлов: 40 добавлений и 11 удалений
|
@ -163,7 +163,7 @@ func runPlatTests(target string, tests []string, t *testing.T) {
|
|||
runTest(name, target, t, nil)
|
||||
})
|
||||
}
|
||||
if target == "wasi" || target == "" && runtime.GOOS == "darwin" {
|
||||
if target == "wasi" || target == "" {
|
||||
t.Run("filesystem.go", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
runTest("filesystem.go", target, t, nil)
|
||||
|
|
|
@ -89,8 +89,3 @@ func AdjustTimeOffset(offset int64) {
|
|||
func os_sigpipe() {
|
||||
runtimePanic("too many writes on closed pipe")
|
||||
}
|
||||
|
||||
//go:linkname syscall_runtime_envs syscall.runtime_envs
|
||||
func syscall_runtime_envs() []string {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -62,6 +62,40 @@ func runMain() {
|
|||
run()
|
||||
}
|
||||
|
||||
//go:extern environ
|
||||
var environ *unsafe.Pointer
|
||||
|
||||
//export strlen
|
||||
func strlen(ptr unsafe.Pointer) uintptr
|
||||
|
||||
//go:linkname syscall_runtime_envs syscall.runtime_envs
|
||||
func syscall_runtime_envs() []string {
|
||||
// Count how many environment variables there are.
|
||||
env := environ
|
||||
numEnvs := 0
|
||||
for *env != nil {
|
||||
numEnvs++
|
||||
env = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(env)) + unsafe.Sizeof(environ)))
|
||||
}
|
||||
|
||||
// Create a string slice of all environment variables.
|
||||
// This requires just a single heap allocation.
|
||||
env = environ
|
||||
envs := make([]string, 0, numEnvs)
|
||||
for *env != nil {
|
||||
ptr := *env
|
||||
length := strlen(ptr)
|
||||
s := _string{
|
||||
ptr: (*byte)(ptr),
|
||||
length: length,
|
||||
}
|
||||
envs = append(envs, *(*string)(unsafe.Pointer(&s)))
|
||||
env = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(env)) + unsafe.Sizeof(environ)))
|
||||
}
|
||||
|
||||
return envs
|
||||
}
|
||||
|
||||
func putchar(c byte) {
|
||||
_putchar(int(c))
|
||||
}
|
||||
|
|
6
testdata/env.go
предоставленный
6
testdata/env.go
предоставленный
|
@ -5,10 +5,10 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
println(os.Getenv("ENV1"))
|
||||
println("ENV1:", os.Getenv("ENV1"))
|
||||
v, ok := os.LookupEnv("ENV2")
|
||||
if !ok {
|
||||
panic("ENV2 not found")
|
||||
println("ENV2 not found")
|
||||
}
|
||||
println(v)
|
||||
println("ENV2:", v)
|
||||
}
|
||||
|
|
4
testdata/env.txt
предоставленный
4
testdata/env.txt
предоставленный
|
@ -1,2 +1,2 @@
|
|||
VALUE1
|
||||
VALUE2
|
||||
ENV1: VALUE1
|
||||
ENV2: VALUE2
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче