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

fix(stdlib): Return early from Process.argv() if length is zero #1817

Merged
merged 2 commits into from
Apr 19, 2023
Merged
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
14 changes: 9 additions & 5 deletions stdlib/sys/process.gr
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,27 @@ provide let argv = () => {
}

let argc = WasmI32.load(argcPtr, 0n)
Memory.free(argcPtr)

let argsLength = argc * 4n
let arr = allocateArray(argc)

if (WasmI32.eqz(argsLength)) {
return Ok(WasmI32.toGrain(arr): Array<String>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Ok(WasmI32.toGrain(arr): Array<String>)
Memory.free(argcPtr)
return Ok(WasmI32.toGrain(arr): Array<String>)

could also refactor a bit to do the free after we read argc so we don't need to do it in multiple places.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do the refactor. Let me know if it is correct!

}

let argvBufSize = WasmI32.load(argvBufSizePtr, 0n)

let argvPtr = Memory.malloc(argc * 4n)
let argvBufPtr = Memory.malloc(argvBufSize)

let err = Wasi.args_get(argvPtr, argvBufPtr)
if (err != Wasi._ESUCCESS) {
Memory.free(argcPtr)
Memory.free(argvPtr)
Memory.free(argvBufPtr)
return Err(Wasi.SystemError(tagSimpleNumber(err)))
}

let arr = allocateArray(argc)

let argsLength = argc * 4n
for (let mut i = 0n; i < argsLength; i += 4n) {
let strPtr = WasmI32.load(argvPtr + i, 0n)
let mut strLength = 0n
Expand All @@ -123,7 +128,6 @@ provide let argv = () => {
WasmI32.store(arr + i, grainStrPtr, 8n)
}

Memory.free(argcPtr)
Memory.free(argvPtr)
Memory.free(argvBufPtr)

Expand Down