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 4, 2024
1 parent 125a4a8 commit 14c0058
Show file tree
Hide file tree
Showing 10 changed files with 533 additions and 342 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
59 changes: 57 additions & 2 deletions std/src/std/libc.inko
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ let SO_REUSEADDR = sys.SO_REUSEADDR
let SO_REUSEPORT = sys.SO_REUSEPORT
let SO_SNDBUF = sys.SO_SNDBUF
let TCP_NODELAY = sys.TCP_NODELAY
let WNOHANG = sys.WNOHANG

fn opendir(path: Pointer[UInt8]) -> Pointer[UInt8] {
sys.opendir(path)
Expand Down Expand Up @@ -123,12 +124,62 @@ fn extern isatty(fd: Int32) -> Int32

fn extern strlen(pointer: Pointer[UInt8]) -> UInt64

fn extern posix_spawnp(
pid: Pointer[Int32],
file: Pointer[UInt8],
file_actions: Pointer[sys.PosixSpawnFileActions],
attrp: Pointer[sys.PosixSpawnAttrs],
argv: Pointer[UInt64],
envp: Pointer[UInt64],
) -> Int32

fn extern posix_spawn_file_actions_init(
actions: Pointer[sys.PosixSpawnFileActions],
) -> Int32

fn extern posix_spawn_file_actions_destroy(
actions: Pointer[sys.PosixSpawnFileActions],
) -> Int32

fn extern posix_spawn_file_actions_adddup2(
actions: Pointer[sys.PosixSpawnFileActions],
fd: Int32,
new_fd: Int32,
) -> Int32

fn extern posix_spawnattr_init(attr: mut Pointer[sys.PosixSpawnAttrs]) -> Int32

fn extern posix_spawnattr_destroy(
attr: mut Pointer[sys.PosixSpawnAttrs],
) -> Int32

fn extern posix_spawnattr_setflags(
attr: mut Pointer[sys.PosixSpawnAttrs],
flags: Int16,
) -> Int32

fn extern posix_spawnattr_setsigdefault(
attr: mut Pointer[sys.PosixSpawnAttrs],
mask: mut Pointer[sys.SigSet],
) -> Int32

fn extern posix_spawnattr_setsigmask(
attr: mut Pointer[sys.PosixSpawnAttrs],
mask: mut Pointer[sys.SigSet],
) -> Int32

fn extern sigemptyset(set: mut Pointer[sys.SigSet]) -> Int32

fn extern sigfillset(set: mut Pointer[sys.SigSet]) -> Int32

fn extern waitpid(pid: Int32, status: Pointer[Int32], options: Int32) -> Int32

fn extern realloc(pointer: Pointer[UInt8], size: Int) -> Pointer[UInt8]

fn extern memmove(
to: Pointer[UInt8],
from: Pointer[UInt8],
size: Int,
size: UInt64,
) -> Pointer[UInt8]

fn extern free(pointer: Pointer[UInt8])
Expand All @@ -151,7 +202,7 @@ fn resize[T](buffer: Pointer[T], size: Int) -> Pointer[T] {
}

fn copy[T](from: Pointer[T], to: Pointer[T], size: Int) {
memmove(to as Pointer[UInt8], from as Pointer[UInt8], size)
memmove(to as Pointer[UInt8], from as Pointer[UInt8], size as UInt64)
}

# Returns the type of a directory entry.
Expand All @@ -163,3 +214,7 @@ fn dirent_type(pointer: Pointer[sys.Dirent]) -> Int {
fn dirent_name(pointer: Pointer[sys.Dirent]) -> Pointer[UInt8] {
sys.dirent_name(pointer)
}

fn pipes -> Result[(Int32, Int32), Error] {
sys.pipes
}
20 changes: 20 additions & 0 deletions std/src/std/libc/freebsd.inko
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import std.io (Error)

let DT_DIR = 4
let DT_LNK = 10
let DT_REG = 8
Expand Down Expand Up @@ -65,6 +67,7 @@ let S_IFLNK = 0xA000
let S_IFMT = 0xF000
let S_IFREG = 0x8000
let TCP_NODELAY = 1
let WNOHANG = 0x00000001

# FreeBSD doesn't define this constant, but we still define it here to make it
# easier to handle platform differences.
Expand Down Expand Up @@ -115,6 +118,11 @@ class extern StatBuf {
let @st_spare9: Int64
}

class extern Pipes {
let @reader: Int32
let @writer: Int32
}

fn extern fchmod(fd: Int32, mode: UInt16) -> Int32

fn extern fstat(fd: Int32, buf: Pointer[StatBuf]) -> Int32
Expand All @@ -136,6 +144,8 @@ fn extern copy_file_range(
flags: UInt32,
) -> Int64

fn extern pipe2(pipes: Pointer[Pipes], flags: Int32) -> Int32

fn flush(fd: Int32) -> Int32 {
fsync(fd)
}
Expand All @@ -147,3 +157,13 @@ fn dirent_type(pointer: Pointer[Dirent]) -> Int {
fn dirent_name(pointer: Pointer[Dirent]) -> Pointer[UInt8] {
pointer as Int + 24 as Pointer[UInt8]
}

fn pipes -> Result[(Int32, Int32), Error] {
let pipes = Pipes()

if pipe2(mut pipes, O_CLOEXEC as Int32) as Int != 0 {
throw Error.last_os_error
}

Result.Ok((pipes.reader, pipes.writer))
}
Loading

0 comments on commit 14c0058

Please sign in to comment.