Skip to content

Commit

Permalink
Add os.process_id
Browse files Browse the repository at this point in the history
  • Loading branch information
irh committed Dec 15, 2024
1 parent e07a2b0 commit cd54179
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The Koto project adheres to
- Supports checking if (e.g.) `io.stdin()` is connected to a terminal.
- `os.command`
- Supports executing and spawning system commands.
- `os.process_id`
- `random.shuffle`
- `string.repeat`
- `tuple.sort_copy` now supports sorting with a key function, following the
Expand Down
8 changes: 8 additions & 0 deletions crates/cli/docs/core_lib/os.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ check! ...
Returns a string containing the name of the current operating system, e.g.
"linux", "macos", "windows", etc.

## process_id

```kototype
|| -> Number
```

Returns the ID associated with the current process.

## start_timer

```kototype
Expand Down
16 changes: 16 additions & 0 deletions crates/runtime/src/core_lib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ pub fn make_module() -> KMap {
unexpected => unexpected_args("||", unexpected),
});

result.add_fn("process_id", |ctx| match ctx.args() {
[] => {
#[cfg(target_arch = "wasm32")]
{
// process::id() panics on wasm targets
return runtime_error!(ErrorKind::UnsupportedPlatform);
}

#[cfg(not(target_arch = "wasm32"))]
{
Ok(std::process::id().into())
}
}
unexpected => unexpected_args("||", unexpected),
});

result.add_fn("start_timer", |ctx| match ctx.args() {
[] => Ok(Timer::now()),
unexpected => unexpected_args("||", unexpected),
Expand Down
2 changes: 2 additions & 0 deletions crates/runtime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub enum ErrorKind {
MissingSequenceBuilder,
#[error("Missing string builder")]
MissingStringBuilder,
#[error("This operation is unsupported on this platform")]
UnsupportedPlatform,
#[error("An unexpected error occurred, please report this as a bug at https://github.com/koto-lang/koto/issues")]
UnexpectedError,
}
Expand Down
3 changes: 3 additions & 0 deletions koto/tests/os.koto
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@test process_id = ||
assert os.process_id() > 0

@test wait_for_command_output = ||
output = os.command('echo')
.args 'testing', 'one', 'two', 'three'
Expand Down

0 comments on commit cd54179

Please sign in to comment.