mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-03 10:37:46 +00:00
runtime: implement environment variables for Linux
This commit is contained in:
committed by
Ron Evans
parent
768a15c1dd
commit
c47cdfa66f
+1
-1
@@ -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))
|
||||
}
|
||||
|
||||
Vendored
+3
-3
@@ -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)
|
||||
}
|
||||
|
||||
Vendored
+2
-2
@@ -1,2 +1,2 @@
|
||||
VALUE1
|
||||
VALUE2
|
||||
ENV1: VALUE1
|
||||
ENV2: VALUE2
|
||||
|
||||
Reference in New Issue
Block a user