diff --git a/rt/src/runtime/general.rs b/rt/src/runtime/general.rs index 5f558447..b46c09a8 100644 --- a/rt/src/runtime/general.rs +++ b/rt/src/runtime/general.rs @@ -2,28 +2,6 @@ use crate::mem::{header_of, ClassPointer}; use crate::process::ProcessPointer; use crate::runtime::process::panic; use std::alloc::handle_alloc_error; -use std::io::Error; - -// Taken from Rust's standard library, with some removals of platforms we don't -// support. -extern "C" { - #[cfg_attr(any(target_os = "linux",), link_name = "__errno_location")] - #[cfg_attr( - any(target_os = "netbsd", target_os = "android",), - link_name = "__errno" - )] - #[cfg_attr( - any( - target_os = "macos", - target_os = "ios", - target_os = "tvos", - target_os = "freebsd", - target_os = "watchos" - ), - link_name = "__error" - )] - fn errno_location() -> *mut i32; -} #[no_mangle] pub unsafe extern "system" fn inko_reference_count_error( @@ -45,13 +23,3 @@ pub unsafe extern "system" fn inko_reference_count_error( pub unsafe extern "system" fn inko_alloc_error(class: ClassPointer) -> ! { handle_alloc_error(class.instance_layout()); } - -#[no_mangle] -pub unsafe extern "system" fn inko_last_error() -> i64 { - Error::last_os_error().raw_os_error().unwrap_or(-1) as i64 -} - -#[no_mangle] -pub unsafe extern "system" fn inko_reset_error() { - *errno_location() = 0; -} diff --git a/std/src/std/io.inko b/std/src/std/io.inko index 5ff8d32c..f23cf23a 100644 --- a/std/src/std/io.inko +++ b/std/src/std/io.inko @@ -30,10 +30,6 @@ let INVALID_DATA = -2 # The error code used when encountering an unexpected end of the input. let UNEXPECTED_EOF = -3 -fn extern inko_last_error -> Int - -fn extern inko_reset_error - fn extern inko_process_start_blocking(process: Pointer[UInt8]) fn extern inko_process_stop_blocking(process: Pointer[UInt8]) @@ -91,7 +87,7 @@ fn pub start_blocking { # rescheduled the current process may run on a different OS thread and thus read # a different state. fn pub stop_blocking -> Int { - let err = inko_last_error + let err = libc.errno_location.0 as Int # If the operation took too long, this reschedules the current process. This # means that any global state read after this point may not be from the same @@ -101,7 +97,7 @@ fn pub stop_blocking -> Int { } fn reset_os_error { - inko_reset_error + libc.errno_location.0 = 0 as Int32 } # An error type for I/O operations. @@ -279,7 +275,7 @@ class pub enum Error { # Returns the last OS error produced by the current OS thread. fn pub static last_os_error -> Error { - from_os_error(inko_last_error) + from_os_error(libc.errno_location.0 as Int) } } diff --git a/std/src/std/libc.inko b/std/src/std/libc.inko index aab557c0..20620195 100644 --- a/std/src/std/libc.inko +++ b/std/src/std/libc.inko @@ -216,3 +216,7 @@ fn dirent_name(pointer: Pointer[sys.Dirent]) -> Pointer[UInt8] { fn pipes -> Result[(Int32, Int32), Error] { sys.pipes } + +fn errno_location -> Pointer[Int32] { + sys.errno_location +} diff --git a/std/src/std/libc/freebsd.inko b/std/src/std/libc/freebsd.inko index ddeab416..768a1f72 100644 --- a/std/src/std/libc/freebsd.inko +++ b/std/src/std/libc/freebsd.inko @@ -163,6 +163,8 @@ fn extern copy_file_range( fn extern pipe2(pipes: Pointer[Pipes], flags: Int32) -> Int32 +fn extern __error -> Pointer[Int32] + fn flush(fd: Int32) -> Int32 { fsync(fd) } @@ -184,3 +186,7 @@ fn pipes -> Result[(Int32, Int32), Error] { Result.Ok((pipes.reader, pipes.writer)) } + +fn errno_location -> Pointer[Int32] { + __error +} diff --git a/std/src/std/libc/linux.inko b/std/src/std/libc/linux.inko index 12bdb2f7..a6e4528e 100644 --- a/std/src/std/libc/linux.inko +++ b/std/src/std/libc/linux.inko @@ -228,6 +228,8 @@ fn extern copy_file_range( fn extern pipe2(pipes: Pointer[Pipes], flags: Int32) -> Int32 +fn extern __errno_location -> Pointer[Int32] + fn flush(fd: Int32) -> Int32 { fsync(fd) } @@ -272,3 +274,7 @@ fn pipes -> Result[(Int32, Int32), Error] { Result.Ok((pipes.reader, pipes.writer)) } + +fn errno_location -> Pointer[Int32] { + __errno_location +} diff --git a/std/src/std/libc/mac.inko b/std/src/std/libc/mac.inko index 5a18bdda..5a2e8e87 100644 --- a/std/src/std/libc/mac.inko +++ b/std/src/std/libc/mac.inko @@ -156,6 +156,8 @@ fn extern fcopyfile( fn extern pipe(pipes: Pointer[Pipes]) -> Int32 +fn extern __error -> Pointer[Int32] + fn fstat(fd: Int32, buf: Pointer[StatBuf]) -> Int32 { sys.fstat(fd, buf) } @@ -223,3 +225,7 @@ fn pipes -> Result[(Int32, Int32), Error] { Result.Ok((pipes.reader, pipes.writer)) } + +fn errno_location -> Pointer[Int32] { + __error +}