-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the `Process` type out of the prelude and into its own module which contains top-level exported functions rather than having them live as static methods on a `Process` type (this was always weird).
- Loading branch information
Showing
13 changed files
with
116 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
// This file is run with arguments passed to it, and environment variables present | ||
import "process" as process | ||
|
||
val args = Process.args() | ||
val args = process.args() | ||
|
||
/// Expect: [-f, bar, --baz, qux] | ||
println(args[1:]) | ||
|
||
val bogusEnvVar = Process.getEnvVar("BOGUS") | ||
val bogusEnvVar = process.getEnvVar("BOGUS") | ||
/// Expect: Option.None | ||
println(bogusEnvVar) | ||
|
||
val fooEnvVar = Process.getEnvVar("FOO") | ||
val fooEnvVar = process.getEnvVar("FOO") | ||
/// Expect: Option.Some(value: "bar") | ||
println(fooEnvVar) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import "./_intrinsics" as intrinsics | ||
import Pointer, Byte from "./_intrinsics" | ||
import "libc" as libc | ||
|
||
export type Uname { | ||
sysname: String | ||
nodename: String | ||
release: String | ||
version: String | ||
machine: String | ||
} | ||
|
||
var _args: String[]? = None | ||
export func args(): String[] { | ||
if _args |args| return args | ||
|
||
val argc = intrinsics.argc() | ||
val argv = intrinsics.argv() | ||
|
||
val args: String[] = Array.withCapacity(argc) | ||
for i in range(0, argc) { | ||
val str = argv.offset(i).load() | ||
val len = libc.strlen(str) | ||
args.push(String(length: len, _buffer: str)) | ||
} | ||
|
||
_args = Some(args) | ||
args | ||
} | ||
|
||
export func getEnvVar(name: String): String? { | ||
val str = libc.getenv(name._buffer) | ||
if str.isNullPtr() return None | ||
|
||
val len = libc.strlen(str) | ||
Some(String(length: len, _buffer: str)) | ||
} | ||
|
||
var _uname: Uname? = None | ||
export func uname(): Uname { | ||
if _uname |uname| return uname | ||
|
||
// On macOS, each field in the utsname struct is a char[256]; on other platforms it's different, so | ||
// this can be wasteful of memory. | ||
var utsnameBuf = Pointer.malloc<Byte>(256 * 5) | ||
val res = libc.uname(utsnameBuf) | ||
if res != 0 { /* todo: handle error code */ } | ||
|
||
val sysnameLen = libc.strlen(utsnameBuf) | ||
val sysname = String.withLength(sysnameLen) | ||
sysname._buffer.copyFrom(utsnameBuf, sysnameLen) | ||
|
||
// The utsname struct has 5 fields (6 on some platforms), of constant and equal sizes, but the size of | ||
// each field differs per platform. After extracting the first string, skip over the \0 bytes until we | ||
// reach the start of the next string. From that offset, we can determine the size of each field, and | ||
// extract the remaining fields more efficiently. | ||
var offset = sysnameLen | ||
utsnameBuf = utsnameBuf.offset(offset) | ||
while utsnameBuf.load().asInt() == 0 { | ||
utsnameBuf = utsnameBuf.offset(1) | ||
offset += 1 | ||
} | ||
|
||
val nodenameLen = libc.strlen(utsnameBuf) | ||
val nodename = String.withLength(nodenameLen) | ||
nodename._buffer.copyFrom(utsnameBuf, nodenameLen) | ||
utsnameBuf = utsnameBuf.offset(offset) | ||
|
||
val releaseLen = libc.strlen(utsnameBuf) | ||
val release = String.withLength(releaseLen) | ||
release._buffer.copyFrom(utsnameBuf, releaseLen) | ||
utsnameBuf = utsnameBuf.offset(offset) | ||
|
||
val versionLen = libc.strlen(utsnameBuf) | ||
val version = String.withLength(versionLen) | ||
version._buffer.copyFrom(utsnameBuf, versionLen) | ||
utsnameBuf = utsnameBuf.offset(offset) | ||
|
||
val machineLen = libc.strlen(utsnameBuf) | ||
val machine = String.withLength(machineLen) | ||
machine._buffer.copyFrom(utsnameBuf, machineLen) | ||
|
||
val uname = Uname(sysname: sysname, nodename: nodename, release: release, version: version, machine: machine) | ||
_uname = Some(uname) | ||
uname | ||
} | ||
|
||
@noreturn | ||
export func exit(status = 1) = libc.exit(status) |