Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasi: add getEnvMap #2390

Merged
merged 3 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion std/os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,37 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {

try result.setMove(key, value);
}
} else if (builtin.os == Os.wasi) {
var environ_count: usize = undefined;
var environ_buf_size: usize = undefined;

const environ_sizes_get_ret = std.os.wasi.environ_sizes_get(&environ_count, &environ_buf_size);
if (environ_sizes_get_ret != os.wasi.ESUCCESS) {
return unexpectedErrorPosix(environ_sizes_get_ret);
}

// TODO: Verify that the documentation is incorrect
// https://github.com/WebAssembly/WASI/issues/27
var environ = try allocator.alloc(?[*]u8, environ_count + 1);
defer allocator.free(environ);
var environ_buf = try std.heap.wasm_allocator.alloc(u8, environ_buf_size);
defer allocator.free(environ_buf);

const environ_get_ret = std.os.wasi.environ_get(environ.ptr, environ_buf.ptr);
if (environ_get_ret != os.wasi.ESUCCESS) {
return unexpectedErrorPosix(environ_get_ret);
}

for (environ) |env| {
if (env) |ptr| {
const pair = mem.toSlice(u8, ptr);
var parts = mem.separate(pair, "=");
const key = parts.next().?;
const value = parts.next().?;
try result.set(key, value);
}
}
return result;
} else {
for (posix_environ_raw) |ptr| {
var line_i: usize = 0;
Expand Down Expand Up @@ -2190,7 +2221,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 {
}

var result_slice = try allocator.alloc([]u8, count);

var i: usize = 0;
while (i < count) : (i += 1) {
result_slice[i] = mem.toSlice(u8, argv[i]);
Expand Down
4 changes: 3 additions & 1 deletion std/os/wasi/core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ pub const SIGABRT: signal_t = 6;
pub extern "wasi_unstable" fn args_get(argv: [*][*]u8, argv_buf: [*]u8) errno_t;
pub extern "wasi_unstable" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t;

pub extern "wasi_unstable" fn proc_raise(sig: signal_t) errno_t;
pub extern "wasi_unstable" fn environ_get(environ: [*]?[*]u8, environ_buf: [*]u8) errno_t;
pub extern "wasi_unstable" fn environ_sizes_get(environ_count: *usize, environ_buf_size: *usize) errno_t;

pub extern "wasi_unstable" fn proc_raise(sig: signal_t) errno_t;
pub extern "wasi_unstable" fn proc_exit(rval: exitcode_t) noreturn;

pub extern "wasi_unstable" fn fd_write(fd: fd_t, iovs: *const ciovec_t, iovs_len: usize, nwritten: *usize) errno_t;