Skip to content

Commit

Permalink
WIP: implement std.sys in Inko
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickpeterse committed Sep 3, 2024
1 parent 47907c4 commit 73f31dc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 73 deletions.
13 changes: 0 additions & 13 deletions rt/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,19 +746,6 @@ impl ProcessPointer {
self.as_ptr() as usize
}

// TODO: remove
pub(crate) fn blocking<R>(mut self, function: impl FnOnce() -> R) -> R {
// Safety: threads are stored in processes before running them.
let thread = unsafe { self.thread() };

thread.start_blocking();

let res = function();

thread.stop_blocking(self);
res
}

pub(crate) fn start_blocking(mut self) {
// Safety: threads are stored in processes before running them.
unsafe { self.thread() }.start_blocking();
Expand Down
24 changes: 7 additions & 17 deletions rt/src/runtime/sys.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::mem::{ByteArray, String as InkoString};
use crate::process::ProcessPointer;
use crate::result::Result as InkoResult;
use crate::runtime::helpers::read_into;
use std::io::Write;
Expand All @@ -16,7 +15,6 @@ fn stdio_for(value: i64) -> Stdio {

#[no_mangle]
pub(crate) unsafe extern "system" fn inko_child_process_spawn(
process: ProcessPointer,
program: *const InkoString,
args: *const *const InkoString,
args_length: i64,
Expand Down Expand Up @@ -52,19 +50,15 @@ pub(crate) unsafe extern "system" fn inko_child_process_spawn(
cmd.current_dir(directory);
}

process
.blocking(|| cmd.spawn())
.map(InkoResult::ok_boxed)
.unwrap_or_else(InkoResult::io_error)
cmd.spawn().map(InkoResult::ok_boxed).unwrap_or_else(InkoResult::io_error)
}

#[no_mangle]
pub(crate) unsafe extern "system" fn inko_child_process_wait(
process: ProcessPointer,
child: *mut Child,
) -> InkoResult {
process
.blocking(|| (*child).wait())
(*child)
.wait()
.map(|status| status.code().unwrap_or(0) as i64)
.map(|status| InkoResult::ok(status as _))
.unwrap_or_else(InkoResult::io_error)
Expand All @@ -88,7 +82,6 @@ pub(crate) unsafe extern "system" fn inko_child_process_try_wait(

#[no_mangle]
pub(crate) unsafe extern "system" fn inko_child_process_stdout_read(
process: ProcessPointer,
child: *mut Child,
buffer: *mut ByteArray,
size: i64,
Expand All @@ -99,15 +92,14 @@ pub(crate) unsafe extern "system" fn inko_child_process_stdout_read(
child
.stdout
.as_mut()
.map(|stream| process.blocking(|| read_into(stream, buff, size)))
.map(|stream| read_into(stream, buff, size))
.unwrap_or(Ok(0))
.map(|size| InkoResult::ok(size as _))
.unwrap_or_else(InkoResult::io_error)
}

#[no_mangle]
pub(crate) unsafe extern "system" fn inko_child_process_stderr_read(
process: ProcessPointer,
child: *mut Child,
buffer: *mut ByteArray,
size: i64,
Expand All @@ -118,15 +110,14 @@ pub(crate) unsafe extern "system" fn inko_child_process_stderr_read(
child
.stderr
.as_mut()
.map(|stream| process.blocking(|| read_into(stream, buff, size)))
.map(|stream| read_into(stream, buff, size))
.unwrap_or(Ok(0))
.map(|size| InkoResult::ok(size as _))
.unwrap_or_else(InkoResult::io_error)
}

#[no_mangle]
pub(crate) unsafe extern "system" fn inko_child_process_stdin_write(
process: ProcessPointer,
child: *mut Child,
data: *mut u8,
size: i64,
Expand All @@ -137,23 +128,22 @@ pub(crate) unsafe extern "system" fn inko_child_process_stdin_write(
child
.stdin
.as_mut()
.map(|stream| process.blocking(|| stream.write(slice)))
.map(|stream| stream.write(slice))
.unwrap_or(Ok(0))
.map(|size| InkoResult::ok(size as _))
.unwrap_or_else(InkoResult::io_error)
}

#[no_mangle]
pub(crate) unsafe extern "system" fn inko_child_process_stdin_flush(
process: ProcessPointer,
child: *mut Child,
) -> InkoResult {
let child = &mut *child;

child
.stdin
.as_mut()
.map(|stream| process.blocking(|| stream.flush()))
.map(|stream| stream.flush())
.unwrap_or(Ok(()))
.map(|_| InkoResult::none())
.unwrap_or_else(InkoResult::io_error)
Expand Down
18 changes: 13 additions & 5 deletions rt/src/rustls_platform_verifier/verification/apple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,19 @@ impl Verifier {
// Safety: well, technically none, but due to the way the runtime uses
// the verifier this should never misbehave.
let process = unsafe { ProcessPointer::new(CURRENT_PROCESS.get()) };
let trust_error =
match process.blocking(|| trust_evaluation.evaluate_with_error()) {
Ok(()) => return Ok(()),
Err(e) => e,
};

process.start_blocking();

let trust_error = match trust_evaluation.evaluate_with_error() {
Ok(()) => {
process.stop_blocking();
return Ok(());
}
Err(e) => {
process.stop_blocking();
e
}
};

let err_code = trust_error.code();

Expand Down
93 changes: 55 additions & 38 deletions std/src/std/sys.inko
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import std.drop (Drop)
import std.fs.path (Path)
import std.int (ToInt)
import std.io (Error, Read, Write, WriteInternal)
import std.io (Error, Read, Write, WriteInternal, start_blocking, stop_blocking)
import std.string (ToString)

class extern AnyResult {
Expand All @@ -16,7 +16,6 @@ class extern IntResult {
}

fn extern inko_child_process_spawn(
process: Pointer[UInt8],
program: String,
args: Pointer[String],
args_size: Int,
Expand All @@ -37,37 +36,28 @@ fn extern inko_child_process_stderr_close(child: Pointer[UInt8])
fn extern inko_child_process_stdin_close(child: Pointer[UInt8])

fn extern inko_child_process_stderr_read(
process: Pointer[UInt8],
child: Pointer[UInt8],
buffer: mut ByteArray,
size: Int,
) -> IntResult

fn extern inko_child_process_stdout_read(
process: Pointer[UInt8],
child: Pointer[UInt8],
buffer: mut ByteArray,
size: Int,
) -> IntResult

fn extern inko_child_process_stdin_flush(
process: Pointer[UInt8],
child: Pointer[UInt8],
) -> IntResult
fn extern inko_child_process_stdin_flush(child: Pointer[UInt8]) -> IntResult

fn extern inko_child_process_stdin_write(
process: Pointer[UInt8],
child: Pointer[UInt8],
data: Pointer[UInt8],
size: Int,
) -> IntResult

fn extern inko_child_process_try_wait(child: Pointer[UInt8]) -> IntResult

fn extern inko_child_process_wait(
process: Pointer[UInt8],
child: Pointer[UInt8],
) -> IntResult
fn extern inko_child_process_wait(child: Pointer[UInt8]) -> IntResult

fn extern inko_exit(status: Int) -> Never

Expand Down Expand Up @@ -366,20 +356,23 @@ class pub Command {
vars.push(entry.value)
})

match
inko_child_process_spawn(
_INKO.process,
@program.to_string,
@arguments.to_pointer,
@arguments.size,
vars.to_pointer,
vars.size,
@stdin.to_int,
@stdout.to_int,
@stderr.to_int,
@directory.as_ref.or(ref ''),
)
{
start_blocking

let res = inko_child_process_spawn(
@program.to_string,
@arguments.to_pointer,
@arguments.size,
vars.to_pointer,
vars.size,
@stdin.to_int,
@stdout.to_int,
@stderr.to_int,
@directory.as_ref.or(ref ''),
)

stop_blocking

match res {
case { @tag = 0, @value = v } -> {
Result.Ok(ChildProcess(v as Pointer[UInt8]))
}
Expand Down Expand Up @@ -438,9 +431,13 @@ impl Drop for Stdin {

impl WriteInternal for Stdin {
fn mut write_internal(data: Pointer[UInt8], size: Int) -> Result[Int, Error] {
match
inko_child_process_stdin_write(_INKO.process, @process.raw, data, size)
{
start_blocking

let res = inko_child_process_stdin_write(@process.raw, data, size)

stop_blocking

match res {
case { @tag = 0, @value = n } -> Result.Ok(n)
case { @value = e } -> Result.Error(Error.from_os_error(e))
}
Expand All @@ -457,7 +454,13 @@ impl Write for Stdin {
}

fn pub mut flush -> Result[Nil, Error] {
match inko_child_process_stdin_flush(_INKO.process, @process.raw) {
start_blocking

let res = inko_child_process_stdin_flush(@process.raw)

stop_blocking

match res {
case { @tag = 1, @value = _ } -> Result.Ok(nil)
case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e))
}
Expand All @@ -482,9 +485,13 @@ impl Drop for Stdout {

impl Read for Stdout {
fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error] {
match
inko_child_process_stdout_read(_INKO.process, @process.raw, into, size)
{
start_blocking

let res = inko_child_process_stdout_read(@process.raw, into, size)

stop_blocking

match res {
case { @tag = 0, @value = v } -> Result.Ok(v)
case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e))
}
Expand All @@ -509,9 +516,13 @@ impl Drop for Stderr {

impl Read for Stderr {
fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error] {
match
inko_child_process_stderr_read(_INKO.process, @process.raw, into, size)
{
start_blocking

let res = inko_child_process_stderr_read(@process.raw, into, size)

stop_blocking

match res {
case { @tag = 0, @value = v } -> Result.Ok(v)
case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e))
}
Expand Down Expand Up @@ -542,7 +553,13 @@ class pub ChildProcess {
#
# The STDIN stream is closed before waiting.
fn pub wait -> Result[ExitStatus, Error] {
match inko_child_process_wait(_INKO.process, @raw) {
start_blocking

let res = inko_child_process_wait(@raw)

stop_blocking

match res {
case { @tag = 0, @value = v } -> Result.Ok(ExitStatus.new(v))
case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e))
}
Expand Down

0 comments on commit 73f31dc

Please sign in to comment.