Implement some more environment functions for WASI.

This commit is contained in:
Brendan Burns
2023-01-06 08:40:16 -08:00
committed by GitHub
parent 7a2d7ff628
commit 24b4e643f4
3 changed files with 35 additions and 18 deletions

View File

@@ -369,6 +369,11 @@ pub fn getEnvVarOwned(allocator: Allocator, key: []const u8) GetEnvVarOwnedError
error.UnexpectedSecondSurrogateHalf => return error.InvalidUtf8,
else => |e| return e,
};
} else if (builtin.os.tag == .wasi and !builtin.link_libc) {
var envmap = getEnvMap(allocator) catch return error.OutOfMemory;
defer envmap.deinit();
const val = envmap.get(key) orelse return error.EnvironmentVariableNotFound;
return allocator.dupe(u8, val);
} else {
const result = os.getenv(key) orelse return error.EnvironmentVariableNotFound;
return allocator.dupe(u8, result);
@@ -379,6 +384,8 @@ pub fn hasEnvVarConstant(comptime key: []const u8) bool {
if (builtin.os.tag == .windows) {
const key_w = comptime std.unicode.utf8ToUtf16LeStringLiteral(key);
return std.os.getenvW(key_w) != null;
} else if (builtin.os.tag == .wasi and !builtin.link_libc) {
@compileError("hasEnvVarConstant is not supported for WASI without libc");
} else {
return os.getenv(key) != null;
}
@@ -390,6 +397,10 @@ pub fn hasEnvVar(allocator: Allocator, key: []const u8) error{OutOfMemory}!bool
const key_w = try std.unicode.utf8ToUtf16LeWithNull(stack_alloc.get(), key);
defer stack_alloc.allocator.free(key_w);
return std.os.getenvW(key_w) != null;
} else if (builtin.os.tag == .wasi and !builtin.link_libc) {
var envmap = getEnvMap(allocator) catch return error.OutOfMemory;
defer envmap.deinit();
return envmap.getPtr(key) != null;
} else {
return os.getenv(key) != null;
}