windows: add support for syscall.runtimeSetenv

I missed this in https://github.com/tinygo-org/tinygo/pull/3391 (because
I didn't test on Windows, my fault).
Этот коммит содержится в:
Ayke van Laethem 2023-02-02 21:07:38 +01:00 коммит произвёл Ayke
родитель 7fe996e7ba
коммит 1996fad075
3 изменённых файлов: 84 добавлений и 50 удалений

51
src/runtime/env.go Обычный файл
Просмотреть файл

@ -0,0 +1,51 @@
//go:build linux || darwin || windows
package runtime
// Update the C environment if cgo is loaded.
// Called from Go 1.20 and above.
//
//go:linkname syscallSetenv syscall.runtimeSetenv
func syscallSetenv(key, value string) {
keydata := cstring(key)
valdata := cstring(value)
setenv(&keydata[0], &valdata[0])
if key == "GODEBUG" && godebugUpdate != nil {
// Starting with Go 1.20, we need to call a callback (set by
// internal/godebug) to notify the GODEBUG environment variable has
// changed. This is necessary to get archive/zip to pass tests.
godebugUpdate(key, value)
}
}
// Update the C environment if cgo is loaded.
// Called from Go 1.20 and above.
//
//go:linkname syscallUnsetenv syscall.runtimeUnsetenv
func syscallUnsetenv(key string) {
keydata := cstring(key)
unsetenv(&keydata[0])
}
// Compatibility with Go 1.19 and below.
//
//go:linkname syscall_setenv_c syscall.setenv_c
func syscall_setenv_c(key string, val string) {
syscallSetenv(key, val)
}
// Compatibility with Go 1.19 and below.
//
//go:linkname syscall_unsetenv_c syscall.unsetenv_c
func syscall_unsetenv_c(key string) {
syscallUnsetenv(key)
}
// cstring converts a Go string to a C string.
// borrowed from syscall
func cstring(s string) []byte {
data := make([]byte, len(s)+1)
copy(data, s)
// final byte should be zero from the initial allocation
return data
}

Просмотреть файл

@ -2,56 +2,6 @@
package runtime
// Update the C environment if cgo is loaded.
// Called from Go 1.20 and above.
//
//go:linkname syscallSetenv syscall.runtimeSetenv
func syscallSetenv(key, value string) {
keydata := cstring(key)
valdata := cstring(value)
// ignore any errors
libc_setenv(&keydata[0], &valdata[0], 1)
if key == "GODEBUG" && godebugUpdate != nil {
// Starting with Go 1.20, we need to call a callback (set by
// internal/godebug) to notify the GODEBUG environment variable has
// changed. This is necessary to get archive/zip to pass tests.
godebugUpdate(key, value)
}
}
// Update the C environment if cgo is loaded.
// Called from Go 1.20 and above.
//
//go:linkname syscallUnsetenv syscall.runtimeUnsetenv
func syscallUnsetenv(key string) {
keydata := cstring(key)
// ignore any errors
libc_unsetenv(&keydata[0])
}
// Compatibility with Go 1.19 and below.
//
//go:linkname syscall_setenv_c syscall.setenv_c
func syscall_setenv_c(key string, val string) {
syscallSetenv(key, val)
}
// Compatibility with Go 1.19 and below.
//
//go:linkname syscall_unsetenv_c syscall.unsetenv_c
func syscall_unsetenv_c(key string) {
syscallUnsetenv(key)
}
// cstring converts a Go string to a C string.
// borrowed from syscall
func cstring(s string) []byte {
data := make([]byte, len(s)+1)
copy(data, s)
// final byte should be zero from the initial allocation
return data
}
// int setenv(const char *name, const char *val, int replace);
//
//export setenv
@ -61,3 +11,13 @@ func libc_setenv(name *byte, val *byte, replace int32) int32
//
//export unsetenv
func libc_unsetenv(name *byte) int32
func setenv(key, val *byte) {
// ignore any errors
libc_setenv(key, val, 1)
}
func unsetenv(key *byte) {
// ignore any errors
libc_unsetenv(key)
}

23
src/runtime/env_windows.go Обычный файл
Просмотреть файл

@ -0,0 +1,23 @@
//go:build windows
package runtime
// Set environment variable in Windows:
//
// BOOL SetEnvironmentVariableA(
// [in] LPCSTR lpName,
// [in, optional] LPCSTR lpValue
// );
//
//export SetEnvironmentVariableA
func _SetEnvironmentVariableA(key, val *byte) bool
func setenv(key, val *byte) {
// ignore any errors
_SetEnvironmentVariableA(key, val)
}
func unsetenv(key *byte) {
// ignore any errors
_SetEnvironmentVariableA(key, nil)
}