github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/env.go (about)

     1  //go:build linux || darwin || windows || wasip1
     2  
     3  package runtime
     4  
     5  // Update the C environment if cgo is loaded.
     6  // Called from Go 1.20 and above.
     7  //
     8  //go:linkname syscallSetenv syscall.runtimeSetenv
     9  func syscallSetenv(key, value string) {
    10  	keydata := cstring(key)
    11  	valdata := cstring(value)
    12  	setenv(&keydata[0], &valdata[0])
    13  	if key == "GODEBUG" && godebugUpdate != nil {
    14  		// Starting with Go 1.20, we need to call a callback (set by
    15  		// internal/godebug) to notify the GODEBUG environment variable has
    16  		// changed. This is necessary to get archive/zip to pass tests.
    17  		godebugUpdate(key, value)
    18  	}
    19  }
    20  
    21  // Update the C environment if cgo is loaded.
    22  // Called from Go 1.20 and above.
    23  //
    24  //go:linkname syscallUnsetenv syscall.runtimeUnsetenv
    25  func syscallUnsetenv(key string) {
    26  	keydata := cstring(key)
    27  	unsetenv(&keydata[0])
    28  }
    29  
    30  // Compatibility with Go 1.19 and below.
    31  //
    32  //go:linkname syscall_setenv_c syscall.setenv_c
    33  func syscall_setenv_c(key string, val string) {
    34  	syscallSetenv(key, val)
    35  }
    36  
    37  // Compatibility with Go 1.19 and below.
    38  //
    39  //go:linkname syscall_unsetenv_c syscall.unsetenv_c
    40  func syscall_unsetenv_c(key string) {
    41  	syscallUnsetenv(key)
    42  }
    43  
    44  // cstring converts a Go string to a C string.
    45  // borrowed from syscall
    46  func cstring(s string) []byte {
    47  	data := make([]byte, len(s)+1)
    48  	copy(data, s)
    49  	// final byte should be zero from the initial allocation
    50  	return data
    51  }