-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
WASI: implement argsAlloc and argsFree #2364
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
Conversation
std/os.zig
Outdated
var count: usize = undefined; | ||
var buf_size: usize = undefined; | ||
|
||
_ = os.wasi.args_sizes_get(&count, &buf_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should assert that there is no error, or handle it. If it asserts that there is no error, it should explain why the error is impossible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return values are not documented at all. I looked at the source code of wasmtime and Lucet and they are not consistent. I have introduced error.ArgsSizesGetFailed and error.ArgsGetFailed in the meantime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case we can follow the pattern set by std/os.zig where an unexpected/undocumented error code is returned. Look for unexpectedErrorPosix and unexpectedErrorWindows.
Generally it's accepted that an undocumented error code might be returned from the OS which translates to error.Unexpected and has a mechanism for logging error codes with a build option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed them to unexpectedErrorPosix. Most of WASI functions (if they are applicable), are similar to POSIX.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ready to merge this, but I might have noticed a problem - I'll give you a chance to double-check and tell me it's ok.
I'm looking forward to being able to build & run WASI targets so we can start adding test coverage for this stuff!
@@ -2203,6 +2235,16 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { | |||
} | |||
|
|||
pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void { | |||
if (builtin.os == Os.wasi) { | |||
const last_item = args_alloc[args_alloc.len - 1]; | |||
const last_byte_addr = @ptrToInt(last_item.ptr) + last_item.len + 1; // null terminated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this has a +1 doesn't the other function need a -1?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The strings in argv_buf
, that is written to by the args_get
function, are contiguous null terminated c-strings. My assumption here is that mem.toSlice
doesn't include the final null-terminated character and I've added it myself here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see.
Implementing ArgIterator for WASI would require introducing a
free
function to the internally allocated buffer so I've simply introduced a compileError for WASI.